mic_device.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/mic_common.h>
  32. #include "../common/mic_dev.h"
  33. #include "mic_device.h"
  34. #include "mic_virtio.h"
  35. static struct mic_driver *g_drv;
  36. static struct mic_irq *shutdown_cookie;
  37. static void mic_notify_host(u8 state)
  38. {
  39. struct mic_driver *mdrv = g_drv;
  40. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  41. iowrite8(state, &bootparam->shutdown_status);
  42. dev_dbg(mdrv->dev, "%s %d system_state %d\n",
  43. __func__, __LINE__, state);
  44. mic_send_intr(&mdrv->mdev, ioread8(&bootparam->c2h_shutdown_db));
  45. }
  46. static int mic_panic_event(struct notifier_block *this, unsigned long event,
  47. void *ptr)
  48. {
  49. struct mic_driver *mdrv = g_drv;
  50. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  51. iowrite8(-1, &bootparam->h2c_config_db);
  52. iowrite8(-1, &bootparam->h2c_shutdown_db);
  53. mic_notify_host(MIC_CRASHED);
  54. return NOTIFY_DONE;
  55. }
  56. static struct notifier_block mic_panic = {
  57. .notifier_call = mic_panic_event,
  58. };
  59. static irqreturn_t mic_shutdown_isr(int irq, void *data)
  60. {
  61. struct mic_driver *mdrv = g_drv;
  62. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  63. mic_ack_interrupt(&g_drv->mdev);
  64. if (ioread8(&bootparam->shutdown_card))
  65. orderly_poweroff(true);
  66. return IRQ_HANDLED;
  67. }
  68. static int mic_shutdown_init(void)
  69. {
  70. int rc = 0;
  71. struct mic_driver *mdrv = g_drv;
  72. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  73. int shutdown_db;
  74. shutdown_db = mic_next_card_db();
  75. shutdown_cookie = mic_request_card_irq(mic_shutdown_isr,
  76. "Shutdown", mdrv, shutdown_db);
  77. if (IS_ERR(shutdown_cookie))
  78. rc = PTR_ERR(shutdown_cookie);
  79. else
  80. iowrite8(shutdown_db, &bootparam->h2c_shutdown_db);
  81. return rc;
  82. }
  83. static void mic_shutdown_uninit(void)
  84. {
  85. struct mic_driver *mdrv = g_drv;
  86. struct mic_bootparam __iomem *bootparam = mdrv->dp;
  87. iowrite8(-1, &bootparam->h2c_shutdown_db);
  88. mic_free_card_irq(shutdown_cookie, mdrv);
  89. }
  90. static int __init mic_dp_init(void)
  91. {
  92. struct mic_driver *mdrv = g_drv;
  93. struct mic_device *mdev = &mdrv->mdev;
  94. struct mic_bootparam __iomem *bootparam;
  95. u64 lo, hi, dp_dma_addr;
  96. u32 magic;
  97. lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
  98. hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
  99. dp_dma_addr = lo | (hi << 32);
  100. mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
  101. if (!mdrv->dp) {
  102. dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
  103. return -ENOMEM;
  104. }
  105. bootparam = mdrv->dp;
  106. magic = ioread32(&bootparam->magic);
  107. if (MIC_MAGIC != magic) {
  108. dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
  109. return -EIO;
  110. }
  111. return 0;
  112. }
  113. /* Uninitialize the device page */
  114. static void mic_dp_uninit(void)
  115. {
  116. mic_card_unmap(&g_drv->mdev, g_drv->dp);
  117. }
  118. /**
  119. * mic_request_card_irq - request an irq.
  120. *
  121. * @func: The callback function that handles the interrupt.
  122. * @name: The ASCII name of the callee requesting the irq.
  123. * @data: private data that is returned back when calling the
  124. * function handler.
  125. * @index: The doorbell index of the requester.
  126. *
  127. * returns: The cookie that is transparent to the caller. Passed
  128. * back when calling mic_free_irq. An appropriate error code
  129. * is returned on failure. Caller needs to use IS_ERR(return_val)
  130. * to check for failure and PTR_ERR(return_val) to obtained the
  131. * error code.
  132. *
  133. */
  134. struct mic_irq *mic_request_card_irq(irqreturn_t (*func)(int irq, void *data),
  135. const char *name, void *data, int index)
  136. {
  137. int rc = 0;
  138. unsigned long cookie;
  139. struct mic_driver *mdrv = g_drv;
  140. rc = request_irq(mic_db_to_irq(mdrv, index), func,
  141. 0, name, data);
  142. if (rc) {
  143. dev_err(mdrv->dev, "request_irq failed rc = %d\n", rc);
  144. goto err;
  145. }
  146. mdrv->irq_info.irq_usage_count[index]++;
  147. cookie = index;
  148. return (struct mic_irq *)cookie;
  149. err:
  150. return ERR_PTR(rc);
  151. }
  152. /**
  153. * mic_free_card_irq - free irq.
  154. *
  155. * @cookie: cookie obtained during a successful call to mic_request_irq
  156. * @data: private data specified by the calling function during the
  157. * mic_request_irq
  158. *
  159. * returns: none.
  160. */
  161. void mic_free_card_irq(struct mic_irq *cookie, void *data)
  162. {
  163. int index;
  164. struct mic_driver *mdrv = g_drv;
  165. index = (unsigned long)cookie & 0xFFFFU;
  166. free_irq(mic_db_to_irq(mdrv, index), data);
  167. mdrv->irq_info.irq_usage_count[index]--;
  168. }
  169. /**
  170. * mic_next_card_db - Get the doorbell with minimum usage count.
  171. *
  172. * Returns the irq index.
  173. */
  174. int mic_next_card_db(void)
  175. {
  176. int i;
  177. int index = 0;
  178. struct mic_driver *mdrv = g_drv;
  179. for (i = 0; i < mdrv->intr_info.num_intr; i++) {
  180. if (mdrv->irq_info.irq_usage_count[i] <
  181. mdrv->irq_info.irq_usage_count[index])
  182. index = i;
  183. }
  184. return index;
  185. }
  186. /**
  187. * mic_init_irq - Initialize irq information.
  188. *
  189. * Returns 0 in success. Appropriate error code on failure.
  190. */
  191. static int mic_init_irq(void)
  192. {
  193. struct mic_driver *mdrv = g_drv;
  194. mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
  195. mdrv->intr_info.num_intr),
  196. GFP_KERNEL);
  197. if (!mdrv->irq_info.irq_usage_count)
  198. return -ENOMEM;
  199. return 0;
  200. }
  201. /**
  202. * mic_uninit_irq - Uninitialize irq information.
  203. *
  204. * None.
  205. */
  206. static void mic_uninit_irq(void)
  207. {
  208. struct mic_driver *mdrv = g_drv;
  209. kfree(mdrv->irq_info.irq_usage_count);
  210. }
  211. /*
  212. * mic_driver_init - MIC driver initialization tasks.
  213. *
  214. * Returns 0 in success. Appropriate error code on failure.
  215. */
  216. int __init mic_driver_init(struct mic_driver *mdrv)
  217. {
  218. int rc;
  219. g_drv = mdrv;
  220. /*
  221. * Unloading the card module is not supported. The MIC card module
  222. * handles fundamental operations like host/card initiated shutdowns
  223. * and informing the host about card crashes and cannot be unloaded.
  224. */
  225. if (!try_module_get(mdrv->dev->driver->owner)) {
  226. rc = -ENODEV;
  227. goto done;
  228. }
  229. rc = mic_dp_init();
  230. if (rc)
  231. goto put;
  232. rc = mic_init_irq();
  233. if (rc)
  234. goto dp_uninit;
  235. rc = mic_shutdown_init();
  236. if (rc)
  237. goto irq_uninit;
  238. rc = mic_devices_init(mdrv);
  239. if (rc)
  240. goto shutdown_uninit;
  241. mic_create_card_debug_dir(mdrv);
  242. atomic_notifier_chain_register(&panic_notifier_list, &mic_panic);
  243. done:
  244. return rc;
  245. shutdown_uninit:
  246. mic_shutdown_uninit();
  247. irq_uninit:
  248. mic_uninit_irq();
  249. dp_uninit:
  250. mic_dp_uninit();
  251. put:
  252. module_put(mdrv->dev->driver->owner);
  253. return rc;
  254. }
  255. /*
  256. * mic_driver_uninit - MIC driver uninitialization tasks.
  257. *
  258. * Returns None
  259. */
  260. void mic_driver_uninit(struct mic_driver *mdrv)
  261. {
  262. mic_delete_card_debug_dir(mdrv);
  263. mic_devices_uninit(mdrv);
  264. /*
  265. * Inform the host about the shutdown status i.e. poweroff/restart etc.
  266. * The module cannot be unloaded so the only code path to call
  267. * mic_devices_uninit(..) is the shutdown callback.
  268. */
  269. mic_notify_host(system_state);
  270. mic_shutdown_uninit();
  271. mic_uninit_irq();
  272. mic_dp_uninit();
  273. module_put(mdrv->dev->driver->owner);
  274. }