mic_boot.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. * Intel MIC Host driver.
  19. *
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/firmware.h>
  23. #include <linux/pci.h>
  24. #include <linux/mic_common.h>
  25. #include <linux/mic_bus.h>
  26. #include "../common/mic_dev.h"
  27. #include "mic_device.h"
  28. #include "mic_smpt.h"
  29. #include "mic_virtio.h"
  30. static inline struct mic_device *mbdev_to_mdev(struct mbus_device *mbdev)
  31. {
  32. return dev_get_drvdata(mbdev->dev.parent);
  33. }
  34. static dma_addr_t
  35. mic_dma_map_page(struct device *dev, struct page *page,
  36. unsigned long offset, size_t size, enum dma_data_direction dir,
  37. struct dma_attrs *attrs)
  38. {
  39. void *va = phys_to_virt(page_to_phys(page)) + offset;
  40. struct mic_device *mdev = dev_get_drvdata(dev->parent);
  41. return mic_map_single(mdev, va, size);
  42. }
  43. static void
  44. mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  45. size_t size, enum dma_data_direction dir,
  46. struct dma_attrs *attrs)
  47. {
  48. struct mic_device *mdev = dev_get_drvdata(dev->parent);
  49. mic_unmap_single(mdev, dma_addr, size);
  50. }
  51. static struct dma_map_ops mic_dma_ops = {
  52. .map_page = mic_dma_map_page,
  53. .unmap_page = mic_dma_unmap_page,
  54. };
  55. static struct mic_irq *
  56. _mic_request_threaded_irq(struct mbus_device *mbdev,
  57. irq_handler_t handler, irq_handler_t thread_fn,
  58. const char *name, void *data, int intr_src)
  59. {
  60. return mic_request_threaded_irq(mbdev_to_mdev(mbdev), handler,
  61. thread_fn, name, data,
  62. intr_src, MIC_INTR_DMA);
  63. }
  64. static void _mic_free_irq(struct mbus_device *mbdev,
  65. struct mic_irq *cookie, void *data)
  66. {
  67. return mic_free_irq(mbdev_to_mdev(mbdev), cookie, data);
  68. }
  69. static void _mic_ack_interrupt(struct mbus_device *mbdev, int num)
  70. {
  71. struct mic_device *mdev = mbdev_to_mdev(mbdev);
  72. mdev->ops->intr_workarounds(mdev);
  73. }
  74. static struct mbus_hw_ops mbus_hw_ops = {
  75. .request_threaded_irq = _mic_request_threaded_irq,
  76. .free_irq = _mic_free_irq,
  77. .ack_interrupt = _mic_ack_interrupt,
  78. };
  79. /**
  80. * mic_reset - Reset the MIC device.
  81. * @mdev: pointer to mic_device instance
  82. */
  83. static void mic_reset(struct mic_device *mdev)
  84. {
  85. int i;
  86. #define MIC_RESET_TO (45)
  87. reinit_completion(&mdev->reset_wait);
  88. mdev->ops->reset_fw_ready(mdev);
  89. mdev->ops->reset(mdev);
  90. for (i = 0; i < MIC_RESET_TO; i++) {
  91. if (mdev->ops->is_fw_ready(mdev))
  92. goto done;
  93. /*
  94. * Resets typically take 10s of seconds to complete.
  95. * Since an MMIO read is required to check if the
  96. * firmware is ready or not, a 1 second delay works nicely.
  97. */
  98. msleep(1000);
  99. }
  100. mic_set_state(mdev, MIC_RESET_FAILED);
  101. done:
  102. complete_all(&mdev->reset_wait);
  103. }
  104. /* Initialize the MIC bootparams */
  105. void mic_bootparam_init(struct mic_device *mdev)
  106. {
  107. struct mic_bootparam *bootparam = mdev->dp;
  108. bootparam->magic = cpu_to_le32(MIC_MAGIC);
  109. bootparam->c2h_shutdown_db = mdev->shutdown_db;
  110. bootparam->h2c_shutdown_db = -1;
  111. bootparam->h2c_config_db = -1;
  112. bootparam->shutdown_status = 0;
  113. bootparam->shutdown_card = 0;
  114. }
  115. /**
  116. * mic_start - Start the MIC.
  117. * @mdev: pointer to mic_device instance
  118. * @buf: buffer containing boot string including firmware/ramdisk path.
  119. *
  120. * This function prepares an MIC for boot and initiates boot.
  121. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  122. */
  123. int mic_start(struct mic_device *mdev, const char *buf)
  124. {
  125. int rc;
  126. mutex_lock(&mdev->mic_mutex);
  127. retry:
  128. if (MIC_OFFLINE != mdev->state) {
  129. rc = -EINVAL;
  130. goto unlock_ret;
  131. }
  132. if (!mdev->ops->is_fw_ready(mdev)) {
  133. mic_reset(mdev);
  134. /*
  135. * The state will either be MIC_OFFLINE if the reset succeeded
  136. * or MIC_RESET_FAILED if the firmware reset failed.
  137. */
  138. goto retry;
  139. }
  140. mdev->dma_mbdev = mbus_register_device(mdev->sdev->parent,
  141. MBUS_DEV_DMA_HOST, &mic_dma_ops,
  142. &mbus_hw_ops, mdev->mmio.va);
  143. if (IS_ERR(mdev->dma_mbdev)) {
  144. rc = PTR_ERR(mdev->dma_mbdev);
  145. goto unlock_ret;
  146. }
  147. mdev->dma_ch = mic_request_dma_chan(mdev);
  148. if (!mdev->dma_ch) {
  149. rc = -ENXIO;
  150. goto dma_remove;
  151. }
  152. rc = mdev->ops->load_mic_fw(mdev, buf);
  153. if (rc)
  154. goto dma_release;
  155. mic_smpt_restore(mdev);
  156. mic_intr_restore(mdev);
  157. mdev->intr_ops->enable_interrupts(mdev);
  158. mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
  159. mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
  160. mdev->ops->send_firmware_intr(mdev);
  161. mic_set_state(mdev, MIC_ONLINE);
  162. goto unlock_ret;
  163. dma_release:
  164. dma_release_channel(mdev->dma_ch);
  165. dma_remove:
  166. mbus_unregister_device(mdev->dma_mbdev);
  167. unlock_ret:
  168. mutex_unlock(&mdev->mic_mutex);
  169. return rc;
  170. }
  171. /**
  172. * mic_stop - Prepare the MIC for reset and trigger reset.
  173. * @mdev: pointer to mic_device instance
  174. * @force: force a MIC to reset even if it is already offline.
  175. *
  176. * RETURNS: None.
  177. */
  178. void mic_stop(struct mic_device *mdev, bool force)
  179. {
  180. mutex_lock(&mdev->mic_mutex);
  181. if (MIC_OFFLINE != mdev->state || force) {
  182. mic_virtio_reset_devices(mdev);
  183. if (mdev->dma_ch) {
  184. dma_release_channel(mdev->dma_ch);
  185. mdev->dma_ch = NULL;
  186. }
  187. mbus_unregister_device(mdev->dma_mbdev);
  188. mic_bootparam_init(mdev);
  189. mic_reset(mdev);
  190. if (MIC_RESET_FAILED == mdev->state)
  191. goto unlock;
  192. mic_set_shutdown_status(mdev, MIC_NOP);
  193. if (MIC_SUSPENDED != mdev->state)
  194. mic_set_state(mdev, MIC_OFFLINE);
  195. }
  196. unlock:
  197. mutex_unlock(&mdev->mic_mutex);
  198. }
  199. /**
  200. * mic_shutdown - Initiate MIC shutdown.
  201. * @mdev: pointer to mic_device instance
  202. *
  203. * RETURNS: None.
  204. */
  205. void mic_shutdown(struct mic_device *mdev)
  206. {
  207. struct mic_bootparam *bootparam = mdev->dp;
  208. s8 db = bootparam->h2c_shutdown_db;
  209. mutex_lock(&mdev->mic_mutex);
  210. if (MIC_ONLINE == mdev->state && db != -1) {
  211. bootparam->shutdown_card = 1;
  212. mdev->ops->send_intr(mdev, db);
  213. mic_set_state(mdev, MIC_SHUTTING_DOWN);
  214. }
  215. mutex_unlock(&mdev->mic_mutex);
  216. }
  217. /**
  218. * mic_shutdown_work - Handle shutdown interrupt from MIC.
  219. * @work: The work structure.
  220. *
  221. * This work is scheduled whenever the host has received a shutdown
  222. * interrupt from the MIC.
  223. */
  224. void mic_shutdown_work(struct work_struct *work)
  225. {
  226. struct mic_device *mdev = container_of(work, struct mic_device,
  227. shutdown_work);
  228. struct mic_bootparam *bootparam = mdev->dp;
  229. mutex_lock(&mdev->mic_mutex);
  230. mic_set_shutdown_status(mdev, bootparam->shutdown_status);
  231. bootparam->shutdown_status = 0;
  232. /*
  233. * if state is MIC_SUSPENDED, OSPM suspend is in progress. We do not
  234. * change the state here so as to prevent users from booting the card
  235. * during and after the suspend operation.
  236. */
  237. if (MIC_SHUTTING_DOWN != mdev->state &&
  238. MIC_SUSPENDED != mdev->state)
  239. mic_set_state(mdev, MIC_SHUTTING_DOWN);
  240. mutex_unlock(&mdev->mic_mutex);
  241. }
  242. /**
  243. * mic_reset_trigger_work - Trigger MIC reset.
  244. * @work: The work structure.
  245. *
  246. * This work is scheduled whenever the host wants to reset the MIC.
  247. */
  248. void mic_reset_trigger_work(struct work_struct *work)
  249. {
  250. struct mic_device *mdev = container_of(work, struct mic_device,
  251. reset_trigger_work);
  252. mic_stop(mdev, false);
  253. }
  254. /**
  255. * mic_complete_resume - Complete MIC Resume after an OSPM suspend/hibernate
  256. * event.
  257. * @mdev: pointer to mic_device instance
  258. *
  259. * RETURNS: None.
  260. */
  261. void mic_complete_resume(struct mic_device *mdev)
  262. {
  263. if (mdev->state != MIC_SUSPENDED) {
  264. dev_warn(mdev->sdev->parent, "state %d should be %d\n",
  265. mdev->state, MIC_SUSPENDED);
  266. return;
  267. }
  268. /* Make sure firmware is ready */
  269. if (!mdev->ops->is_fw_ready(mdev))
  270. mic_stop(mdev, true);
  271. mutex_lock(&mdev->mic_mutex);
  272. mic_set_state(mdev, MIC_OFFLINE);
  273. mutex_unlock(&mdev->mic_mutex);
  274. }
  275. /**
  276. * mic_prepare_suspend - Handle suspend notification for the MIC device.
  277. * @mdev: pointer to mic_device instance
  278. *
  279. * RETURNS: None.
  280. */
  281. void mic_prepare_suspend(struct mic_device *mdev)
  282. {
  283. unsigned long timeout;
  284. #define MIC_SUSPEND_TIMEOUT (60 * HZ)
  285. mutex_lock(&mdev->mic_mutex);
  286. switch (mdev->state) {
  287. case MIC_OFFLINE:
  288. /*
  289. * Card is already offline. Set state to MIC_SUSPENDED
  290. * to prevent users from booting the card.
  291. */
  292. mic_set_state(mdev, MIC_SUSPENDED);
  293. mutex_unlock(&mdev->mic_mutex);
  294. break;
  295. case MIC_ONLINE:
  296. /*
  297. * Card is online. Set state to MIC_SUSPENDING and notify
  298. * MIC user space daemon which will issue card
  299. * shutdown and reset.
  300. */
  301. mic_set_state(mdev, MIC_SUSPENDING);
  302. mutex_unlock(&mdev->mic_mutex);
  303. timeout = wait_for_completion_timeout(&mdev->reset_wait,
  304. MIC_SUSPEND_TIMEOUT);
  305. /* Force reset the card if the shutdown completion timed out */
  306. if (!timeout) {
  307. mutex_lock(&mdev->mic_mutex);
  308. mic_set_state(mdev, MIC_SUSPENDED);
  309. mutex_unlock(&mdev->mic_mutex);
  310. mic_stop(mdev, true);
  311. }
  312. break;
  313. case MIC_SHUTTING_DOWN:
  314. /*
  315. * Card is shutting down. Set state to MIC_SUSPENDED
  316. * to prevent further boot of the card.
  317. */
  318. mic_set_state(mdev, MIC_SUSPENDED);
  319. mutex_unlock(&mdev->mic_mutex);
  320. timeout = wait_for_completion_timeout(&mdev->reset_wait,
  321. MIC_SUSPEND_TIMEOUT);
  322. /* Force reset the card if the shutdown completion timed out */
  323. if (!timeout)
  324. mic_stop(mdev, true);
  325. break;
  326. default:
  327. mutex_unlock(&mdev->mic_mutex);
  328. break;
  329. }
  330. }
  331. /**
  332. * mic_suspend - Initiate MIC suspend. Suspend merely issues card shutdown.
  333. * @mdev: pointer to mic_device instance
  334. *
  335. * RETURNS: None.
  336. */
  337. void mic_suspend(struct mic_device *mdev)
  338. {
  339. struct mic_bootparam *bootparam = mdev->dp;
  340. s8 db = bootparam->h2c_shutdown_db;
  341. mutex_lock(&mdev->mic_mutex);
  342. if (MIC_SUSPENDING == mdev->state && db != -1) {
  343. bootparam->shutdown_card = 1;
  344. mdev->ops->send_intr(mdev, db);
  345. mic_set_state(mdev, MIC_SUSPENDED);
  346. }
  347. mutex_unlock(&mdev->mic_mutex);
  348. }