mic_device.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. #include "mic_virtio.h"
  37. static struct mic_driver *g_drv;
  38. static struct mic_irq *shutdown_cookie;
  39. static void mic_notify_host(u8 state)
  40. {
  41. struct mic_driver *mdrv = g_drv;
  42. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  43. iowrite8(state, &bootparam->shutdown_status);
  44. dev_dbg(mdrv->dev, "%s %d system_state %d\n",
  45. __func__, __LINE__, state);
  46. mic_send_intr(&mdrv->mdev, ioread8(&bootparam->c2h_shutdown_db));
  47. }
  48. static int mic_panic_event(struct notifier_block *this, unsigned long event,
  49. void *ptr)
  50. {
  51. struct mic_driver *mdrv = g_drv;
  52. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  53. iowrite8(-1, &bootparam->h2c_config_db);
  54. iowrite8(-1, &bootparam->h2c_shutdown_db);
  55. mic_notify_host(MIC_CRASHED);
  56. return NOTIFY_DONE;
  57. }
  58. static struct notifier_block mic_panic = {
  59. .notifier_call = mic_panic_event,
  60. };
  61. static irqreturn_t mic_shutdown_isr(int irq, void *data)
  62. {
  63. struct mic_driver *mdrv = g_drv;
  64. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  65. mic_ack_interrupt(&g_drv->mdev);
  66. if (ioread8(&bootparam->shutdown_card))
  67. orderly_poweroff(true);
  68. return IRQ_HANDLED;
  69. }
  70. static int mic_shutdown_init(void)
  71. {
  72. int rc = 0;
  73. struct mic_driver *mdrv = g_drv;
  74. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  75. int shutdown_db;
  76. shutdown_db = mic_next_card_db();
  77. shutdown_cookie = mic_request_card_irq(mic_shutdown_isr, NULL,
  78. "Shutdown", mdrv, shutdown_db);
  79. if (IS_ERR(shutdown_cookie))
  80. rc = PTR_ERR(shutdown_cookie);
  81. else
  82. iowrite8(shutdown_db, &bootparam->h2c_shutdown_db);
  83. return rc;
  84. }
  85. static void mic_shutdown_uninit(void)
  86. {
  87. struct mic_driver *mdrv = g_drv;
  88. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  89. iowrite8(-1, &bootparam->h2c_shutdown_db);
  90. mic_free_card_irq(shutdown_cookie, mdrv);
  91. }
  92. static int __init mic_dp_init(void)
  93. {
  94. struct mic_driver *mdrv = g_drv;
  95. struct mic_device *mdev = &mdrv->mdev;
  96. struct mic_bootparam __iomem *bootparam;
  97. u64 lo, hi, dp_dma_addr;
  98. u32 magic;
  99. lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
  100. hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
  101. dp_dma_addr = lo | (hi << 32);
  102. mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
  103. if (!mdrv->dp) {
  104. dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
  105. return -ENOMEM;
  106. }
  107. bootparam = mdrv->dp;
  108. magic = ioread32(&bootparam->magic);
  109. if (MIC_MAGIC != magic) {
  110. dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
  111. return -EIO;
  112. }
  113. return 0;
  114. }
  115. /* Uninitialize the device page */
  116. static void mic_dp_uninit(void)
  117. {
  118. mic_card_unmap(&g_drv->mdev, g_drv->dp);
  119. }
  120. /**
  121. * mic_request_card_irq - request an irq.
  122. *
  123. * @handler: interrupt handler passed to request_threaded_irq.
  124. * @thread_fn: thread fn. passed to request_threaded_irq.
  125. * @name: The ASCII name of the callee requesting the irq.
  126. * @data: private data that is returned back when calling the
  127. * function handler.
  128. * @index: The doorbell index of the requester.
  129. *
  130. * returns: The cookie that is transparent to the caller. Passed
  131. * back when calling mic_free_irq. An appropriate error code
  132. * is returned on failure. Caller needs to use IS_ERR(return_val)
  133. * to check for failure and PTR_ERR(return_val) to obtained the
  134. * error code.
  135. *
  136. */
  137. struct mic_irq *
  138. mic_request_card_irq(irq_handler_t handler,
  139. irq_handler_t thread_fn, const char *name,
  140. void *data, int index)
  141. {
  142. int rc = 0;
  143. unsigned long cookie;
  144. struct mic_driver *mdrv = g_drv;
  145. rc = request_threaded_irq(mic_db_to_irq(mdrv, index), handler,
  146. thread_fn, 0, name, data);
  147. if (rc) {
  148. dev_err(mdrv->dev, "request_threaded_irq failed rc = %d\n", rc);
  149. goto err;
  150. }
  151. mdrv->irq_info.irq_usage_count[index]++;
  152. cookie = index;
  153. return (struct mic_irq *)cookie;
  154. err:
  155. return ERR_PTR(rc);
  156. }
  157. /**
  158. * mic_free_card_irq - free irq.
  159. *
  160. * @cookie: cookie obtained during a successful call to mic_request_threaded_irq
  161. * @data: private data specified by the calling function during the
  162. * mic_request_threaded_irq
  163. *
  164. * returns: none.
  165. */
  166. void mic_free_card_irq(struct mic_irq *cookie, void *data)
  167. {
  168. int index;
  169. struct mic_driver *mdrv = g_drv;
  170. index = (unsigned long)cookie & 0xFFFFU;
  171. free_irq(mic_db_to_irq(mdrv, index), data);
  172. mdrv->irq_info.irq_usage_count[index]--;
  173. }
  174. /**
  175. * mic_next_card_db - Get the doorbell with minimum usage count.
  176. *
  177. * Returns the irq index.
  178. */
  179. int mic_next_card_db(void)
  180. {
  181. int i;
  182. int index = 0;
  183. struct mic_driver *mdrv = g_drv;
  184. for (i = 0; i < mdrv->intr_info.num_intr; i++) {
  185. if (mdrv->irq_info.irq_usage_count[i] <
  186. mdrv->irq_info.irq_usage_count[index])
  187. index = i;
  188. }
  189. return index;
  190. }
  191. /**
  192. * mic_init_irq - Initialize irq information.
  193. *
  194. * Returns 0 in success. Appropriate error code on failure.
  195. */
  196. static int mic_init_irq(void)
  197. {
  198. struct mic_driver *mdrv = g_drv;
  199. mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
  200. mdrv->intr_info.num_intr),
  201. GFP_KERNEL);
  202. if (!mdrv->irq_info.irq_usage_count)
  203. return -ENOMEM;
  204. return 0;
  205. }
  206. /**
  207. * mic_uninit_irq - Uninitialize irq information.
  208. *
  209. * None.
  210. */
  211. static void mic_uninit_irq(void)
  212. {
  213. struct mic_driver *mdrv = g_drv;
  214. kfree(mdrv->irq_info.irq_usage_count);
  215. }
  216. static inline struct mic_driver *scdev_to_mdrv(struct scif_hw_dev *scdev)
  217. {
  218. return dev_get_drvdata(scdev->dev.parent);
  219. }
  220. static struct mic_irq *
  221. ___mic_request_irq(struct scif_hw_dev *scdev,
  222. irqreturn_t (*func)(int irq, void *data),
  223. const char *name, void *data,
  224. int db)
  225. {
  226. return mic_request_card_irq(func, NULL, name, data, db);
  227. }
  228. static void
  229. ___mic_free_irq(struct scif_hw_dev *scdev,
  230. struct mic_irq *cookie, void *data)
  231. {
  232. return mic_free_card_irq(cookie, data);
  233. }
  234. static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
  235. {
  236. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  237. mic_ack_interrupt(&mdrv->mdev);
  238. }
  239. static int ___mic_next_db(struct scif_hw_dev *scdev)
  240. {
  241. return mic_next_card_db();
  242. }
  243. static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
  244. {
  245. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  246. mic_send_intr(&mdrv->mdev, db);
  247. }
  248. static void ___mic_send_p2p_intr(struct scif_hw_dev *scdev, int db,
  249. struct mic_mw *mw)
  250. {
  251. mic_send_p2p_intr(db, mw);
  252. }
  253. static void __iomem *
  254. ___mic_ioremap(struct scif_hw_dev *scdev,
  255. phys_addr_t pa, size_t len)
  256. {
  257. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  258. return mic_card_map(&mdrv->mdev, pa, len);
  259. }
  260. static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
  261. {
  262. struct mic_driver *mdrv = scdev_to_mdrv(scdev);
  263. mic_card_unmap(&mdrv->mdev, va);
  264. }
  265. static struct scif_hw_ops scif_hw_ops = {
  266. .request_irq = ___mic_request_irq,
  267. .free_irq = ___mic_free_irq,
  268. .ack_interrupt = ___mic_ack_interrupt,
  269. .next_db = ___mic_next_db,
  270. .send_intr = ___mic_send_intr,
  271. .send_p2p_intr = ___mic_send_p2p_intr,
  272. .ioremap = ___mic_ioremap,
  273. .iounmap = ___mic_iounmap,
  274. };
  275. static int mic_request_dma_chans(struct mic_driver *mdrv)
  276. {
  277. dma_cap_mask_t mask;
  278. struct dma_chan *chan;
  279. request_module("mic_x100_dma");
  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. /*
  314. * Unloading the card module is not supported. The MIC card module
  315. * handles fundamental operations like host/card initiated shutdowns
  316. * and informing the host about card crashes and cannot be unloaded.
  317. */
  318. if (!try_module_get(mdrv->dev->driver->owner)) {
  319. rc = -ENODEV;
  320. goto done;
  321. }
  322. rc = mic_dp_init();
  323. if (rc)
  324. goto put;
  325. rc = mic_init_irq();
  326. if (rc)
  327. goto dp_uninit;
  328. rc = mic_shutdown_init();
  329. if (rc)
  330. goto irq_uninit;
  331. if (!mic_request_dma_chans(mdrv)) {
  332. rc = -ENODEV;
  333. goto shutdown_uninit;
  334. }
  335. rc = mic_devices_init(mdrv);
  336. if (rc)
  337. goto dma_free;
  338. bootparam = mdrv->dp;
  339. node_id = ioread8(&bootparam->node_id);
  340. mdrv->scdev = scif_register_device(mdrv->dev, MIC_SCIF_DEV,
  341. NULL, &scif_hw_ops,
  342. 0, node_id, &mdrv->mdev.mmio, NULL,
  343. NULL, mdrv->dp, mdrv->dma_ch,
  344. mdrv->num_dma_ch);
  345. if (IS_ERR(mdrv->scdev)) {
  346. rc = PTR_ERR(mdrv->scdev);
  347. goto device_uninit;
  348. }
  349. mic_create_card_debug_dir(mdrv);
  350. atomic_notifier_chain_register(&panic_notifier_list, &mic_panic);
  351. done:
  352. return rc;
  353. device_uninit:
  354. mic_devices_uninit(mdrv);
  355. dma_free:
  356. mic_free_dma_chans(mdrv);
  357. shutdown_uninit:
  358. mic_shutdown_uninit();
  359. irq_uninit:
  360. mic_uninit_irq();
  361. dp_uninit:
  362. mic_dp_uninit();
  363. put:
  364. module_put(mdrv->dev->driver->owner);
  365. return rc;
  366. }
  367. /*
  368. * mic_driver_uninit - MIC driver uninitialization tasks.
  369. *
  370. * Returns None
  371. */
  372. void mic_driver_uninit(struct mic_driver *mdrv)
  373. {
  374. mic_delete_card_debug_dir(mdrv);
  375. scif_unregister_device(mdrv->scdev);
  376. mic_devices_uninit(mdrv);
  377. mic_free_dma_chans(mdrv);
  378. /*
  379. * Inform the host about the shutdown status i.e. poweroff/restart etc.
  380. * The module cannot be unloaded so the only code path to call
  381. * mic_devices_uninit(..) is the shutdown callback.
  382. */
  383. mic_notify_host(system_state);
  384. mic_shutdown_uninit();
  385. mic_uninit_irq();
  386. mic_dp_uninit();
  387. module_put(mdrv->dev->driver->owner);
  388. }