mic_device.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Disclaimer: The codes contained in these modules may be specific to
  19. * the Intel Software Development Platform codenamed: Knights Ferry, and
  20. * the Intel product codenamed: Knights Corner, and are not backward
  21. * compatible with other Intel products. Additionally, Intel will NOT
  22. * support the codes or instruction set in future products.
  23. *
  24. * Intel MIC Card driver.
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/reboot.h>
  31. #include <linux/dmaengine.h>
  32. #include <linux/kmod.h>
  33. #include <linux/mic_common.h>
  34. #include "../common/mic_dev.h"
  35. #include "mic_device.h"
  36. static struct mic_driver *g_drv;
  37. static int __init mic_dp_init(void)
  38. {
  39. struct mic_driver *mdrv = g_drv;
  40. struct mic_device *mdev = &mdrv->mdev;
  41. struct mic_bootparam __iomem *bootparam;
  42. u64 lo, hi, dp_dma_addr;
  43. u32 magic;
  44. lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
  45. hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
  46. dp_dma_addr = lo | (hi << 32);
  47. mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
  48. if (!mdrv->dp) {
  49. dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
  50. return -ENOMEM;
  51. }
  52. bootparam = mdrv->dp;
  53. magic = ioread32(&bootparam->magic);
  54. if (MIC_MAGIC != magic) {
  55. dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
  56. return -EIO;
  57. }
  58. return 0;
  59. }
  60. /* Uninitialize the device page */
  61. static void mic_dp_uninit(void)
  62. {
  63. mic_card_unmap(&g_drv->mdev, g_drv->dp);
  64. }
  65. /**
  66. * mic_request_card_irq - request an irq.
  67. *
  68. * @handler: interrupt handler passed to request_threaded_irq.
  69. * @thread_fn: thread fn. passed to request_threaded_irq.
  70. * @name: The ASCII name of the callee requesting the irq.
  71. * @data: private data that is returned back when calling the
  72. * function handler.
  73. * @index: The doorbell index of the requester.
  74. *
  75. * returns: The cookie that is transparent to the caller. Passed
  76. * back when calling mic_free_irq. An appropriate error code
  77. * is returned on failure. Caller needs to use IS_ERR(return_val)
  78. * to check for failure and PTR_ERR(return_val) to obtained the
  79. * error code.
  80. *
  81. */
  82. struct mic_irq *
  83. mic_request_card_irq(irq_handler_t handler,
  84. irq_handler_t thread_fn, const char *name,
  85. void *data, int index)
  86. {
  87. int rc = 0;
  88. unsigned long cookie;
  89. struct mic_driver *mdrv = g_drv;
  90. rc = request_threaded_irq(mic_db_to_irq(mdrv, index), handler,
  91. thread_fn, 0, name, data);
  92. if (rc) {
  93. dev_err(mdrv->dev, "request_threaded_irq failed rc = %d\n", rc);
  94. goto err;
  95. }
  96. mdrv->irq_info.irq_usage_count[index]++;
  97. cookie = index;
  98. return (struct mic_irq *)cookie;
  99. err:
  100. return ERR_PTR(rc);
  101. }
  102. /**
  103. * mic_free_card_irq - free irq.
  104. *
  105. * @cookie: cookie obtained during a successful call to mic_request_threaded_irq
  106. * @data: private data specified by the calling function during the
  107. * mic_request_threaded_irq
  108. *
  109. * returns: none.
  110. */
  111. void mic_free_card_irq(struct mic_irq *cookie, void *data)
  112. {
  113. int index;
  114. struct mic_driver *mdrv = g_drv;
  115. index = (unsigned long)cookie & 0xFFFFU;
  116. free_irq(mic_db_to_irq(mdrv, index), data);
  117. mdrv->irq_info.irq_usage_count[index]--;
  118. }
  119. /**
  120. * mic_next_card_db - Get the doorbell with minimum usage count.
  121. *
  122. * Returns the irq index.
  123. */
  124. int mic_next_card_db(void)
  125. {
  126. int i;
  127. int index = 0;
  128. struct mic_driver *mdrv = g_drv;
  129. for (i = 0; i < mdrv->intr_info.num_intr; i++) {
  130. if (mdrv->irq_info.irq_usage_count[i] <
  131. mdrv->irq_info.irq_usage_count[index])
  132. index = i;
  133. }
  134. return index;
  135. }
  136. /**
  137. * mic_init_irq - Initialize irq information.
  138. *
  139. * Returns 0 in success. Appropriate error code on failure.
  140. */
  141. static int mic_init_irq(void)
  142. {
  143. struct mic_driver *mdrv = g_drv;
  144. mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
  145. mdrv->intr_info.num_intr),
  146. GFP_KERNEL);
  147. if (!mdrv->irq_info.irq_usage_count)
  148. return -ENOMEM;
  149. return 0;
  150. }
  151. /**
  152. * mic_uninit_irq - Uninitialize irq information.
  153. *
  154. * None.
  155. */
  156. static void mic_uninit_irq(void)
  157. {
  158. struct mic_driver *mdrv = g_drv;
  159. kfree(mdrv->irq_info.irq_usage_count);
  160. }
  161. static inline struct mic_driver *scdev_to_mdrv(struct scif_hw_dev *scdev)
  162. {
  163. return dev_get_drvdata(scdev->dev.parent);
  164. }
  165. static struct mic_irq *
  166. ___mic_request_irq(struct scif_hw_dev *scdev,
  167. irqreturn_t (*func)(int irq, void *data),
  168. const char *name, void *data,
  169. int db)
  170. {
  171. return mic_request_card_irq(func, NULL, name, data, db);
  172. }
  173. static void
  174. ___mic_free_irq(struct scif_hw_dev *scdev,
  175. struct mic_irq *cookie, void *data)
  176. {
  177. return mic_free_card_irq(cookie, data);
  178. }
  179. static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
  180. {
  181. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  182. mic_ack_interrupt(&mdrv->mdev);
  183. }
  184. static int ___mic_next_db(struct scif_hw_dev *scdev)
  185. {
  186. return mic_next_card_db();
  187. }
  188. static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
  189. {
  190. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  191. mic_send_intr(&mdrv->mdev, db);
  192. }
  193. static void ___mic_send_p2p_intr(struct scif_hw_dev *scdev, int db,
  194. struct mic_mw *mw)
  195. {
  196. mic_send_p2p_intr(db, mw);
  197. }
  198. static void __iomem *
  199. ___mic_ioremap(struct scif_hw_dev *scdev,
  200. phys_addr_t pa, size_t len)
  201. {
  202. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  203. return mic_card_map(&mdrv->mdev, pa, len);
  204. }
  205. static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
  206. {
  207. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  208. mic_card_unmap(&mdrv->mdev, va);
  209. }
  210. static struct scif_hw_ops scif_hw_ops = {
  211. .request_irq = ___mic_request_irq,
  212. .free_irq = ___mic_free_irq,
  213. .ack_interrupt = ___mic_ack_interrupt,
  214. .next_db = ___mic_next_db,
  215. .send_intr = ___mic_send_intr,
  216. .send_p2p_intr = ___mic_send_p2p_intr,
  217. .ioremap = ___mic_ioremap,
  218. .iounmap = ___mic_iounmap,
  219. };
  220. static inline struct mic_driver *vpdev_to_mdrv(struct vop_device *vpdev)
  221. {
  222. return dev_get_drvdata(vpdev->dev.parent);
  223. }
  224. static struct mic_irq *
  225. __mic_request_irq(struct vop_device *vpdev,
  226. irqreturn_t (*func)(int irq, void *data),
  227. const char *name, void *data, int intr_src)
  228. {
  229. return mic_request_card_irq(func, NULL, name, data, intr_src);
  230. }
  231. static void __mic_free_irq(struct vop_device *vpdev,
  232. struct mic_irq *cookie, void *data)
  233. {
  234. return mic_free_card_irq(cookie, data);
  235. }
  236. static void __mic_ack_interrupt(struct vop_device *vpdev, int num)
  237. {
  238. struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
  239. mic_ack_interrupt(&mdrv->mdev);
  240. }
  241. static int __mic_next_db(struct vop_device *vpdev)
  242. {
  243. return mic_next_card_db();
  244. }
  245. static void __iomem *__mic_get_remote_dp(struct vop_device *vpdev)
  246. {
  247. struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
  248. return mdrv->dp;
  249. }
  250. static void __mic_send_intr(struct vop_device *vpdev, int db)
  251. {
  252. struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
  253. mic_send_intr(&mdrv->mdev, db);
  254. }
  255. static void __iomem *__mic_ioremap(struct vop_device *vpdev,
  256. dma_addr_t pa, size_t len)
  257. {
  258. struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
  259. return mic_card_map(&mdrv->mdev, pa, len);
  260. }
  261. static void __mic_iounmap(struct vop_device *vpdev, void __iomem *va)
  262. {
  263. struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
  264. mic_card_unmap(&mdrv->mdev, va);
  265. }
  266. static struct vop_hw_ops vop_hw_ops = {
  267. .request_irq = __mic_request_irq,
  268. .free_irq = __mic_free_irq,
  269. .ack_interrupt = __mic_ack_interrupt,
  270. .next_db = __mic_next_db,
  271. .get_remote_dp = __mic_get_remote_dp,
  272. .send_intr = __mic_send_intr,
  273. .ioremap = __mic_ioremap,
  274. .iounmap = __mic_iounmap,
  275. };
  276. static int mic_request_dma_chans(struct mic_driver *mdrv)
  277. {
  278. dma_cap_mask_t mask;
  279. struct dma_chan *chan;
  280. dma_cap_zero(mask);
  281. dma_cap_set(DMA_MEMCPY, mask);
  282. do {
  283. chan = dma_request_channel(mask, NULL, NULL);
  284. if (chan) {
  285. mdrv->dma_ch[mdrv->num_dma_ch++] = chan;
  286. if (mdrv->num_dma_ch >= MIC_MAX_DMA_CHAN)
  287. break;
  288. }
  289. } while (chan);
  290. dev_info(mdrv->dev, "DMA channels # %d\n", mdrv->num_dma_ch);
  291. return mdrv->num_dma_ch;
  292. }
  293. static void mic_free_dma_chans(struct mic_driver *mdrv)
  294. {
  295. int i = 0;
  296. for (i = 0; i < mdrv->num_dma_ch; i++) {
  297. dma_release_channel(mdrv->dma_ch[i]);
  298. mdrv->dma_ch[i] = NULL;
  299. }
  300. mdrv->num_dma_ch = 0;
  301. }
  302. /*
  303. * mic_driver_init - MIC driver initialization tasks.
  304. *
  305. * Returns 0 in success. Appropriate error code on failure.
  306. */
  307. int __init mic_driver_init(struct mic_driver *mdrv)
  308. {
  309. int rc;
  310. struct mic_bootparam __iomem *bootparam;
  311. u8 node_id;
  312. g_drv = mdrv;
  313. /* Unloading the card module is not supported. */
  314. if (!try_module_get(mdrv->dev->driver->owner)) {
  315. rc = -ENODEV;
  316. goto done;
  317. }
  318. rc = mic_dp_init();
  319. if (rc)
  320. goto put;
  321. rc = mic_init_irq();
  322. if (rc)
  323. goto dp_uninit;
  324. if (!mic_request_dma_chans(mdrv)) {
  325. rc = -ENODEV;
  326. goto irq_uninit;
  327. }
  328. mdrv->vpdev = vop_register_device(mdrv->dev, VOP_DEV_TRNSP,
  329. NULL, &vop_hw_ops, 0,
  330. NULL, mdrv->dma_ch[0]);
  331. if (IS_ERR(mdrv->vpdev)) {
  332. rc = PTR_ERR(mdrv->vpdev);
  333. goto dma_free;
  334. }
  335. bootparam = mdrv->dp;
  336. node_id = ioread8(&bootparam->node_id);
  337. mdrv->scdev = scif_register_device(mdrv->dev, MIC_SCIF_DEV,
  338. NULL, &scif_hw_ops,
  339. 0, node_id, &mdrv->mdev.mmio, NULL,
  340. NULL, mdrv->dp, mdrv->dma_ch,
  341. mdrv->num_dma_ch, true);
  342. if (IS_ERR(mdrv->scdev)) {
  343. rc = PTR_ERR(mdrv->scdev);
  344. goto vop_remove;
  345. }
  346. mic_create_card_debug_dir(mdrv);
  347. done:
  348. return rc;
  349. vop_remove:
  350. vop_unregister_device(mdrv->vpdev);
  351. dma_free:
  352. mic_free_dma_chans(mdrv);
  353. irq_uninit:
  354. mic_uninit_irq();
  355. dp_uninit:
  356. mic_dp_uninit();
  357. put:
  358. module_put(mdrv->dev->driver->owner);
  359. return rc;
  360. }
  361. /*
  362. * mic_driver_uninit - MIC driver uninitialization tasks.
  363. *
  364. * Returns None
  365. */
  366. void mic_driver_uninit(struct mic_driver *mdrv)
  367. {
  368. mic_delete_card_debug_dir(mdrv);
  369. scif_unregister_device(mdrv->scdev);
  370. vop_unregister_device(mdrv->vpdev);
  371. mic_free_dma_chans(mdrv);
  372. mic_uninit_irq();
  373. mic_dp_uninit();
  374. module_put(mdrv->dev->driver->owner);
  375. }