intel_guc_fw.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Vinit Azad <vinit.azad@intel.com>
  25. * Ben Widawsky <ben@bwidawsk.net>
  26. * Dave Gordon <david.s.gordon@intel.com>
  27. * Alex Dai <yu.dai@intel.com>
  28. */
  29. #include "intel_guc_fw.h"
  30. #include "i915_drv.h"
  31. #define SKL_FW_MAJOR 9
  32. #define SKL_FW_MINOR 33
  33. #define BXT_FW_MAJOR 9
  34. #define BXT_FW_MINOR 29
  35. #define KBL_FW_MAJOR 9
  36. #define KBL_FW_MINOR 39
  37. #define GUC_FW_PATH(platform, major, minor) \
  38. "i915/" __stringify(platform) "_guc_ver" __stringify(major) "_" __stringify(minor) ".bin"
  39. #define I915_SKL_GUC_UCODE GUC_FW_PATH(skl, SKL_FW_MAJOR, SKL_FW_MINOR)
  40. MODULE_FIRMWARE(I915_SKL_GUC_UCODE);
  41. #define I915_BXT_GUC_UCODE GUC_FW_PATH(bxt, BXT_FW_MAJOR, BXT_FW_MINOR)
  42. MODULE_FIRMWARE(I915_BXT_GUC_UCODE);
  43. #define I915_KBL_GUC_UCODE GUC_FW_PATH(kbl, KBL_FW_MAJOR, KBL_FW_MINOR)
  44. MODULE_FIRMWARE(I915_KBL_GUC_UCODE);
  45. static void guc_fw_select(struct intel_uc_fw *guc_fw)
  46. {
  47. struct intel_guc *guc = container_of(guc_fw, struct intel_guc, fw);
  48. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  49. GEM_BUG_ON(guc_fw->type != INTEL_UC_FW_TYPE_GUC);
  50. if (!HAS_GUC(dev_priv))
  51. return;
  52. if (i915_modparams.guc_firmware_path) {
  53. guc_fw->path = i915_modparams.guc_firmware_path;
  54. guc_fw->major_ver_wanted = 0;
  55. guc_fw->minor_ver_wanted = 0;
  56. } else if (IS_SKYLAKE(dev_priv)) {
  57. guc_fw->path = I915_SKL_GUC_UCODE;
  58. guc_fw->major_ver_wanted = SKL_FW_MAJOR;
  59. guc_fw->minor_ver_wanted = SKL_FW_MINOR;
  60. } else if (IS_BROXTON(dev_priv)) {
  61. guc_fw->path = I915_BXT_GUC_UCODE;
  62. guc_fw->major_ver_wanted = BXT_FW_MAJOR;
  63. guc_fw->minor_ver_wanted = BXT_FW_MINOR;
  64. } else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
  65. guc_fw->path = I915_KBL_GUC_UCODE;
  66. guc_fw->major_ver_wanted = KBL_FW_MAJOR;
  67. guc_fw->minor_ver_wanted = KBL_FW_MINOR;
  68. } else {
  69. DRM_WARN("%s: No firmware known for this platform!\n",
  70. intel_uc_fw_type_repr(guc_fw->type));
  71. }
  72. }
  73. /**
  74. * intel_guc_fw_init_early() - initializes GuC firmware struct
  75. * @guc: intel_guc struct
  76. *
  77. * On platforms with GuC selects firmware for uploading
  78. */
  79. void intel_guc_fw_init_early(struct intel_guc *guc)
  80. {
  81. struct intel_uc_fw *guc_fw = &guc->fw;
  82. intel_uc_fw_init(guc_fw, INTEL_UC_FW_TYPE_GUC);
  83. guc_fw_select(guc_fw);
  84. }
  85. static void guc_prepare_xfer(struct intel_guc *guc)
  86. {
  87. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  88. /* Must program this register before loading the ucode with DMA */
  89. I915_WRITE(GUC_SHIM_CONTROL, GUC_DISABLE_SRAM_INIT_TO_ZEROES |
  90. GUC_ENABLE_READ_CACHE_LOGIC |
  91. GUC_ENABLE_MIA_CACHING |
  92. GUC_ENABLE_READ_CACHE_FOR_SRAM_DATA |
  93. GUC_ENABLE_READ_CACHE_FOR_WOPCM_DATA |
  94. GUC_ENABLE_MIA_CLOCK_GATING);
  95. if (IS_GEN9_LP(dev_priv))
  96. I915_WRITE(GEN9LP_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  97. else
  98. I915_WRITE(GEN9_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  99. if (IS_GEN9(dev_priv)) {
  100. /* DOP Clock Gating Enable for GuC clocks */
  101. I915_WRITE(GEN7_MISCCPCTL, (GEN8_DOP_CLOCK_GATE_GUC_ENABLE |
  102. I915_READ(GEN7_MISCCPCTL)));
  103. /* allows for 5us (in 10ns units) before GT can go to RC6 */
  104. I915_WRITE(GUC_ARAT_C6DIS, 0x1FF);
  105. }
  106. }
  107. /* Copy RSA signature from the fw image to HW for verification */
  108. static int guc_xfer_rsa(struct intel_guc *guc, struct i915_vma *vma)
  109. {
  110. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  111. struct intel_uc_fw *guc_fw = &guc->fw;
  112. struct sg_table *sg = vma->pages;
  113. u32 rsa[UOS_RSA_SCRATCH_COUNT];
  114. int i;
  115. if (sg_pcopy_to_buffer(sg->sgl, sg->nents, rsa, sizeof(rsa),
  116. guc_fw->rsa_offset) != sizeof(rsa))
  117. return -EINVAL;
  118. for (i = 0; i < UOS_RSA_SCRATCH_COUNT; i++)
  119. I915_WRITE(UOS_RSA_SCRATCH(i), rsa[i]);
  120. return 0;
  121. }
  122. /*
  123. * Transfer the firmware image to RAM for execution by the microcontroller.
  124. *
  125. * Architecturally, the DMA engine is bidirectional, and can potentially even
  126. * transfer between GTT locations. This functionality is left out of the API
  127. * for now as there is no need for it.
  128. */
  129. static int guc_xfer_ucode(struct intel_guc *guc, struct i915_vma *vma)
  130. {
  131. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  132. struct intel_uc_fw *guc_fw = &guc->fw;
  133. unsigned long offset;
  134. u32 status;
  135. int ret;
  136. /*
  137. * The header plus uCode will be copied to WOPCM via DMA, excluding any
  138. * other components
  139. */
  140. I915_WRITE(DMA_COPY_SIZE, guc_fw->header_size + guc_fw->ucode_size);
  141. /* Set the source address for the new blob */
  142. offset = intel_guc_ggtt_offset(guc, vma) + guc_fw->header_offset;
  143. I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
  144. I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
  145. /*
  146. * Set the DMA destination. Current uCode expects the code to be
  147. * loaded at 8k; locations below this are used for the stack.
  148. */
  149. I915_WRITE(DMA_ADDR_1_LOW, 0x2000);
  150. I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
  151. /* Finally start the DMA */
  152. I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));
  153. /* Wait for DMA to finish */
  154. ret = __intel_wait_for_register_fw(dev_priv, DMA_CTRL, START_DMA, 0,
  155. 2, 100, &status);
  156. DRM_DEBUG_DRIVER("GuC DMA status %#x\n", status);
  157. return ret;
  158. }
  159. /*
  160. * Read the GuC status register (GUC_STATUS) and store it in the
  161. * specified location; then return a boolean indicating whether
  162. * the value matches either of two values representing completion
  163. * of the GuC boot process.
  164. *
  165. * This is used for polling the GuC status in a wait_for()
  166. * loop below.
  167. */
  168. static inline bool guc_ready(struct intel_guc *guc, u32 *status)
  169. {
  170. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  171. u32 val = I915_READ(GUC_STATUS);
  172. u32 uk_val = val & GS_UKERNEL_MASK;
  173. *status = val;
  174. return (uk_val == GS_UKERNEL_READY) ||
  175. ((val & GS_MIA_CORE_STATE) && (uk_val == GS_UKERNEL_LAPIC_DONE));
  176. }
  177. static int guc_wait_ucode(struct intel_guc *guc)
  178. {
  179. u32 status;
  180. int ret;
  181. /*
  182. * Wait for the GuC to start up.
  183. * NB: Docs recommend not using the interrupt for completion.
  184. * Measurements indicate this should take no more than 20ms, so a
  185. * timeout here indicates that the GuC has failed and is unusable.
  186. * (Higher levels of the driver will attempt to fall back to
  187. * execlist mode if this happens.)
  188. */
  189. ret = wait_for(guc_ready(guc, &status), 100);
  190. DRM_DEBUG_DRIVER("GuC status %#x\n", status);
  191. if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
  192. DRM_ERROR("GuC firmware signature verification failed\n");
  193. ret = -ENOEXEC;
  194. }
  195. return ret;
  196. }
  197. /*
  198. * Load the GuC firmware blob into the MinuteIA.
  199. */
  200. static int guc_fw_xfer(struct intel_uc_fw *guc_fw, struct i915_vma *vma)
  201. {
  202. struct intel_guc *guc = container_of(guc_fw, struct intel_guc, fw);
  203. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  204. int ret;
  205. GEM_BUG_ON(guc_fw->type != INTEL_UC_FW_TYPE_GUC);
  206. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  207. guc_prepare_xfer(guc);
  208. /*
  209. * Note that GuC needs the CSS header plus uKernel code to be copied
  210. * by the DMA engine in one operation, whereas the RSA signature is
  211. * loaded via MMIO.
  212. */
  213. ret = guc_xfer_rsa(guc, vma);
  214. if (ret)
  215. DRM_WARN("GuC firmware signature xfer error %d\n", ret);
  216. ret = guc_xfer_ucode(guc, vma);
  217. if (ret)
  218. DRM_WARN("GuC firmware code xfer error %d\n", ret);
  219. ret = guc_wait_ucode(guc);
  220. if (ret)
  221. DRM_ERROR("GuC firmware xfer error %d\n", ret);
  222. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  223. return ret;
  224. }
  225. /**
  226. * intel_guc_fw_upload() - load GuC uCode to device
  227. * @guc: intel_guc structure
  228. *
  229. * Called from intel_uc_init_hw() during driver load, resume from sleep and
  230. * after a GPU reset.
  231. *
  232. * The firmware image should have already been fetched into memory, so only
  233. * check that fetch succeeded, and then transfer the image to the h/w.
  234. *
  235. * Return: non-zero code on error
  236. */
  237. int intel_guc_fw_upload(struct intel_guc *guc)
  238. {
  239. return intel_uc_fw_upload(&guc->fw, guc_fw_xfer);
  240. }