mic_boot.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "../common/mic_dev.h"
  26. #include "mic_device.h"
  27. #include "mic_smpt.h"
  28. #include "mic_virtio.h"
  29. /**
  30. * mic_reset - Reset the MIC device.
  31. * @mdev: pointer to mic_device instance
  32. */
  33. static void mic_reset(struct mic_device *mdev)
  34. {
  35. int i;
  36. #define MIC_RESET_TO (45)
  37. reinit_completion(&mdev->reset_wait);
  38. mdev->ops->reset_fw_ready(mdev);
  39. mdev->ops->reset(mdev);
  40. for (i = 0; i < MIC_RESET_TO; i++) {
  41. if (mdev->ops->is_fw_ready(mdev))
  42. goto done;
  43. /*
  44. * Resets typically take 10s of seconds to complete.
  45. * Since an MMIO read is required to check if the
  46. * firmware is ready or not, a 1 second delay works nicely.
  47. */
  48. msleep(1000);
  49. }
  50. mic_set_state(mdev, MIC_RESET_FAILED);
  51. done:
  52. complete_all(&mdev->reset_wait);
  53. }
  54. /* Initialize the MIC bootparams */
  55. void mic_bootparam_init(struct mic_device *mdev)
  56. {
  57. struct mic_bootparam *bootparam = mdev->dp;
  58. bootparam->magic = cpu_to_le32(MIC_MAGIC);
  59. bootparam->c2h_shutdown_db = mdev->shutdown_db;
  60. bootparam->h2c_shutdown_db = -1;
  61. bootparam->h2c_config_db = -1;
  62. bootparam->shutdown_status = 0;
  63. bootparam->shutdown_card = 0;
  64. }
  65. /**
  66. * mic_start - Start the MIC.
  67. * @mdev: pointer to mic_device instance
  68. * @buf: buffer containing boot string including firmware/ramdisk path.
  69. *
  70. * This function prepares an MIC for boot and initiates boot.
  71. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  72. */
  73. int mic_start(struct mic_device *mdev, const char *buf)
  74. {
  75. int rc;
  76. mutex_lock(&mdev->mic_mutex);
  77. retry:
  78. if (MIC_OFFLINE != mdev->state) {
  79. rc = -EINVAL;
  80. goto unlock_ret;
  81. }
  82. if (!mdev->ops->is_fw_ready(mdev)) {
  83. mic_reset(mdev);
  84. /*
  85. * The state will either be MIC_OFFLINE if the reset succeeded
  86. * or MIC_RESET_FAILED if the firmware reset failed.
  87. */
  88. goto retry;
  89. }
  90. rc = mdev->ops->load_mic_fw(mdev, buf);
  91. if (rc)
  92. goto unlock_ret;
  93. mic_smpt_restore(mdev);
  94. mic_intr_restore(mdev);
  95. mdev->intr_ops->enable_interrupts(mdev);
  96. mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
  97. mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
  98. mdev->ops->send_firmware_intr(mdev);
  99. mic_set_state(mdev, MIC_ONLINE);
  100. unlock_ret:
  101. mutex_unlock(&mdev->mic_mutex);
  102. return rc;
  103. }
  104. /**
  105. * mic_stop - Prepare the MIC for reset and trigger reset.
  106. * @mdev: pointer to mic_device instance
  107. * @force: force a MIC to reset even if it is already offline.
  108. *
  109. * RETURNS: None.
  110. */
  111. void mic_stop(struct mic_device *mdev, bool force)
  112. {
  113. mutex_lock(&mdev->mic_mutex);
  114. if (MIC_OFFLINE != mdev->state || force) {
  115. mic_virtio_reset_devices(mdev);
  116. mic_bootparam_init(mdev);
  117. mic_reset(mdev);
  118. if (MIC_RESET_FAILED == mdev->state)
  119. goto unlock;
  120. mic_set_shutdown_status(mdev, MIC_NOP);
  121. if (MIC_SUSPENDED != mdev->state)
  122. mic_set_state(mdev, MIC_OFFLINE);
  123. }
  124. unlock:
  125. mutex_unlock(&mdev->mic_mutex);
  126. }
  127. /**
  128. * mic_shutdown - Initiate MIC shutdown.
  129. * @mdev: pointer to mic_device instance
  130. *
  131. * RETURNS: None.
  132. */
  133. void mic_shutdown(struct mic_device *mdev)
  134. {
  135. struct mic_bootparam *bootparam = mdev->dp;
  136. s8 db = bootparam->h2c_shutdown_db;
  137. mutex_lock(&mdev->mic_mutex);
  138. if (MIC_ONLINE == mdev->state && db != -1) {
  139. bootparam->shutdown_card = 1;
  140. mdev->ops->send_intr(mdev, db);
  141. mic_set_state(mdev, MIC_SHUTTING_DOWN);
  142. }
  143. mutex_unlock(&mdev->mic_mutex);
  144. }
  145. /**
  146. * mic_shutdown_work - Handle shutdown interrupt from MIC.
  147. * @work: The work structure.
  148. *
  149. * This work is scheduled whenever the host has received a shutdown
  150. * interrupt from the MIC.
  151. */
  152. void mic_shutdown_work(struct work_struct *work)
  153. {
  154. struct mic_device *mdev = container_of(work, struct mic_device,
  155. shutdown_work);
  156. struct mic_bootparam *bootparam = mdev->dp;
  157. mutex_lock(&mdev->mic_mutex);
  158. mic_set_shutdown_status(mdev, bootparam->shutdown_status);
  159. bootparam->shutdown_status = 0;
  160. /*
  161. * if state is MIC_SUSPENDED, OSPM suspend is in progress. We do not
  162. * change the state here so as to prevent users from booting the card
  163. * during and after the suspend operation.
  164. */
  165. if (MIC_SHUTTING_DOWN != mdev->state &&
  166. MIC_SUSPENDED != mdev->state)
  167. mic_set_state(mdev, MIC_SHUTTING_DOWN);
  168. mutex_unlock(&mdev->mic_mutex);
  169. }
  170. /**
  171. * mic_reset_trigger_work - Trigger MIC reset.
  172. * @work: The work structure.
  173. *
  174. * This work is scheduled whenever the host wants to reset the MIC.
  175. */
  176. void mic_reset_trigger_work(struct work_struct *work)
  177. {
  178. struct mic_device *mdev = container_of(work, struct mic_device,
  179. reset_trigger_work);
  180. mic_stop(mdev, false);
  181. }
  182. /**
  183. * mic_complete_resume - Complete MIC Resume after an OSPM suspend/hibernate
  184. * event.
  185. * @mdev: pointer to mic_device instance
  186. *
  187. * RETURNS: None.
  188. */
  189. void mic_complete_resume(struct mic_device *mdev)
  190. {
  191. if (mdev->state != MIC_SUSPENDED) {
  192. dev_warn(mdev->sdev->parent, "state %d should be %d\n",
  193. mdev->state, MIC_SUSPENDED);
  194. return;
  195. }
  196. /* Make sure firmware is ready */
  197. if (!mdev->ops->is_fw_ready(mdev))
  198. mic_stop(mdev, true);
  199. mutex_lock(&mdev->mic_mutex);
  200. mic_set_state(mdev, MIC_OFFLINE);
  201. mutex_unlock(&mdev->mic_mutex);
  202. }
  203. /**
  204. * mic_prepare_suspend - Handle suspend notification for the MIC device.
  205. * @mdev: pointer to mic_device instance
  206. *
  207. * RETURNS: None.
  208. */
  209. void mic_prepare_suspend(struct mic_device *mdev)
  210. {
  211. int rc;
  212. #define MIC_SUSPEND_TIMEOUT (60 * HZ)
  213. mutex_lock(&mdev->mic_mutex);
  214. switch (mdev->state) {
  215. case MIC_OFFLINE:
  216. /*
  217. * Card is already offline. Set state to MIC_SUSPENDED
  218. * to prevent users from booting the card.
  219. */
  220. mic_set_state(mdev, MIC_SUSPENDED);
  221. mutex_unlock(&mdev->mic_mutex);
  222. break;
  223. case MIC_ONLINE:
  224. /*
  225. * Card is online. Set state to MIC_SUSPENDING and notify
  226. * MIC user space daemon which will issue card
  227. * shutdown and reset.
  228. */
  229. mic_set_state(mdev, MIC_SUSPENDING);
  230. mutex_unlock(&mdev->mic_mutex);
  231. rc = wait_for_completion_timeout(&mdev->reset_wait,
  232. MIC_SUSPEND_TIMEOUT);
  233. /* Force reset the card if the shutdown completion timed out */
  234. if (!rc) {
  235. mutex_lock(&mdev->mic_mutex);
  236. mic_set_state(mdev, MIC_SUSPENDED);
  237. mutex_unlock(&mdev->mic_mutex);
  238. mic_stop(mdev, true);
  239. }
  240. break;
  241. case MIC_SHUTTING_DOWN:
  242. /*
  243. * Card is shutting down. Set state to MIC_SUSPENDED
  244. * to prevent further boot of the card.
  245. */
  246. mic_set_state(mdev, MIC_SUSPENDED);
  247. mutex_unlock(&mdev->mic_mutex);
  248. rc = wait_for_completion_timeout(&mdev->reset_wait,
  249. MIC_SUSPEND_TIMEOUT);
  250. /* Force reset the card if the shutdown completion timed out */
  251. if (!rc)
  252. mic_stop(mdev, true);
  253. break;
  254. default:
  255. mutex_unlock(&mdev->mic_mutex);
  256. break;
  257. }
  258. }
  259. /**
  260. * mic_suspend - Initiate MIC suspend. Suspend merely issues card shutdown.
  261. * @mdev: pointer to mic_device instance
  262. *
  263. * RETURNS: None.
  264. */
  265. void mic_suspend(struct mic_device *mdev)
  266. {
  267. struct mic_bootparam *bootparam = mdev->dp;
  268. s8 db = bootparam->h2c_shutdown_db;
  269. mutex_lock(&mdev->mic_mutex);
  270. if (MIC_SUSPENDING == mdev->state && db != -1) {
  271. bootparam->shutdown_card = 1;
  272. mdev->ops->send_intr(mdev, db);
  273. mic_set_state(mdev, MIC_SUSPENDED);
  274. }
  275. mutex_unlock(&mdev->mic_mutex);
  276. }