mic_boot.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/mic_common.h>
  24. #include "../common/mic_device.h"
  25. #include "mic_device.h"
  26. #include "mic_smpt.h"
  27. #include "mic_virtio.h"
  28. /**
  29. * mic_reset - Reset the MIC device.
  30. * @mdev: pointer to mic_device instance
  31. */
  32. static void mic_reset(struct mic_device *mdev)
  33. {
  34. int i;
  35. #define MIC_RESET_TO (45)
  36. mdev->ops->reset_fw_ready(mdev);
  37. mdev->ops->reset(mdev);
  38. for (i = 0; i < MIC_RESET_TO; i++) {
  39. if (mdev->ops->is_fw_ready(mdev))
  40. return;
  41. /*
  42. * Resets typically take 10s of seconds to complete.
  43. * Since an MMIO read is required to check if the
  44. * firmware is ready or not, a 1 second delay works nicely.
  45. */
  46. msleep(1000);
  47. }
  48. mic_set_state(mdev, MIC_RESET_FAILED);
  49. }
  50. /* Initialize the MIC bootparams */
  51. void mic_bootparam_init(struct mic_device *mdev)
  52. {
  53. struct mic_bootparam *bootparam = mdev->dp;
  54. bootparam->magic = MIC_MAGIC;
  55. bootparam->c2h_shutdown_db = mdev->shutdown_db;
  56. bootparam->h2c_shutdown_db = -1;
  57. bootparam->h2c_config_db = -1;
  58. bootparam->shutdown_status = 0;
  59. bootparam->shutdown_card = 0;
  60. }
  61. /**
  62. * mic_start - Start the MIC.
  63. * @mdev: pointer to mic_device instance
  64. * @buf: buffer containing boot string including firmware/ramdisk path.
  65. *
  66. * This function prepares an MIC for boot and initiates boot.
  67. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  68. */
  69. int mic_start(struct mic_device *mdev, const char *buf)
  70. {
  71. int rc;
  72. mutex_lock(&mdev->mic_mutex);
  73. retry:
  74. if (MIC_OFFLINE != mdev->state) {
  75. rc = -EINVAL;
  76. goto unlock_ret;
  77. }
  78. if (!mdev->ops->is_fw_ready(mdev)) {
  79. mic_reset(mdev);
  80. /*
  81. * The state will either be MIC_OFFLINE if the reset succeeded
  82. * or MIC_RESET_FAILED if the firmware reset failed.
  83. */
  84. goto retry;
  85. }
  86. rc = mdev->ops->load_mic_fw(mdev, buf);
  87. if (rc)
  88. goto unlock_ret;
  89. mic_smpt_restore(mdev);
  90. mic_intr_restore(mdev);
  91. mdev->intr_ops->enable_interrupts(mdev);
  92. mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
  93. mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
  94. mdev->ops->send_firmware_intr(mdev);
  95. mic_set_state(mdev, MIC_ONLINE);
  96. unlock_ret:
  97. mutex_unlock(&mdev->mic_mutex);
  98. return rc;
  99. }
  100. /**
  101. * mic_stop - Prepare the MIC for reset and trigger reset.
  102. * @mdev: pointer to mic_device instance
  103. * @force: force a MIC to reset even if it is already offline.
  104. *
  105. * RETURNS: None.
  106. */
  107. void mic_stop(struct mic_device *mdev, bool force)
  108. {
  109. mutex_lock(&mdev->mic_mutex);
  110. if (MIC_OFFLINE != mdev->state || force) {
  111. mic_virtio_reset_devices(mdev);
  112. mic_bootparam_init(mdev);
  113. mic_reset(mdev);
  114. if (MIC_RESET_FAILED == mdev->state)
  115. goto unlock;
  116. mic_set_shutdown_status(mdev, MIC_NOP);
  117. mic_set_state(mdev, MIC_OFFLINE);
  118. }
  119. unlock:
  120. mutex_unlock(&mdev->mic_mutex);
  121. }
  122. /**
  123. * mic_shutdown - Initiate MIC shutdown.
  124. * @mdev: pointer to mic_device instance
  125. *
  126. * RETURNS: None.
  127. */
  128. void mic_shutdown(struct mic_device *mdev)
  129. {
  130. struct mic_bootparam *bootparam = mdev->dp;
  131. s8 db = bootparam->h2c_shutdown_db;
  132. mutex_lock(&mdev->mic_mutex);
  133. if (MIC_ONLINE == mdev->state && db != -1) {
  134. bootparam->shutdown_card = 1;
  135. mdev->ops->send_intr(mdev, db);
  136. mic_set_state(mdev, MIC_SHUTTING_DOWN);
  137. }
  138. mutex_unlock(&mdev->mic_mutex);
  139. }
  140. /**
  141. * mic_shutdown_work - Handle shutdown interrupt from MIC.
  142. * @work: The work structure.
  143. *
  144. * This work is scheduled whenever the host has received a shutdown
  145. * interrupt from the MIC.
  146. */
  147. void mic_shutdown_work(struct work_struct *work)
  148. {
  149. struct mic_device *mdev = container_of(work, struct mic_device,
  150. shutdown_work);
  151. struct mic_bootparam *bootparam = mdev->dp;
  152. mutex_lock(&mdev->mic_mutex);
  153. mic_set_shutdown_status(mdev, bootparam->shutdown_status);
  154. bootparam->shutdown_status = 0;
  155. if (MIC_SHUTTING_DOWN != mdev->state)
  156. mic_set_state(mdev, MIC_SHUTTING_DOWN);
  157. mutex_unlock(&mdev->mic_mutex);
  158. }
  159. /**
  160. * mic_reset_trigger_work - Trigger MIC reset.
  161. * @work: The work structure.
  162. *
  163. * This work is scheduled whenever the host wants to reset the MIC.
  164. */
  165. void mic_reset_trigger_work(struct work_struct *work)
  166. {
  167. struct mic_device *mdev = container_of(work, struct mic_device,
  168. reset_trigger_work);
  169. mic_stop(mdev, false);
  170. }