intel_guc_fw.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 GLK_FW_MAJOR 10
  38. #define GLK_FW_MINOR 56
  39. #define GUC_FW_PATH(platform, major, minor) \
  40. "i915/" __stringify(platform) "_guc_ver" __stringify(major) "_" __stringify(minor) ".bin"
  41. #define I915_SKL_GUC_UCODE GUC_FW_PATH(skl, SKL_FW_MAJOR, SKL_FW_MINOR)
  42. MODULE_FIRMWARE(I915_SKL_GUC_UCODE);
  43. #define I915_BXT_GUC_UCODE GUC_FW_PATH(bxt, BXT_FW_MAJOR, BXT_FW_MINOR)
  44. MODULE_FIRMWARE(I915_BXT_GUC_UCODE);
  45. #define I915_KBL_GUC_UCODE GUC_FW_PATH(kbl, KBL_FW_MAJOR, KBL_FW_MINOR)
  46. MODULE_FIRMWARE(I915_KBL_GUC_UCODE);
  47. #define I915_GLK_GUC_UCODE GUC_FW_PATH(glk, GLK_FW_MAJOR, GLK_FW_MINOR)
  48. static void guc_fw_select(struct intel_uc_fw *guc_fw)
  49. {
  50. struct intel_guc *guc = container_of(guc_fw, struct intel_guc, fw);
  51. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  52. GEM_BUG_ON(guc_fw->type != INTEL_UC_FW_TYPE_GUC);
  53. if (!HAS_GUC(dev_priv))
  54. return;
  55. if (i915_modparams.guc_firmware_path) {
  56. guc_fw->path = i915_modparams.guc_firmware_path;
  57. guc_fw->major_ver_wanted = 0;
  58. guc_fw->minor_ver_wanted = 0;
  59. } else if (IS_SKYLAKE(dev_priv)) {
  60. guc_fw->path = I915_SKL_GUC_UCODE;
  61. guc_fw->major_ver_wanted = SKL_FW_MAJOR;
  62. guc_fw->minor_ver_wanted = SKL_FW_MINOR;
  63. } else if (IS_BROXTON(dev_priv)) {
  64. guc_fw->path = I915_BXT_GUC_UCODE;
  65. guc_fw->major_ver_wanted = BXT_FW_MAJOR;
  66. guc_fw->minor_ver_wanted = BXT_FW_MINOR;
  67. } else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
  68. guc_fw->path = I915_KBL_GUC_UCODE;
  69. guc_fw->major_ver_wanted = KBL_FW_MAJOR;
  70. guc_fw->minor_ver_wanted = KBL_FW_MINOR;
  71. } else if (IS_GEMINILAKE(dev_priv)) {
  72. guc_fw->path = I915_GLK_GUC_UCODE;
  73. guc_fw->major_ver_wanted = GLK_FW_MAJOR;
  74. guc_fw->minor_ver_wanted = GLK_FW_MINOR;
  75. } else {
  76. DRM_WARN("%s: No firmware known for this platform!\n",
  77. intel_uc_fw_type_repr(guc_fw->type));
  78. }
  79. }
  80. /**
  81. * intel_guc_fw_init_early() - initializes GuC firmware struct
  82. * @guc: intel_guc struct
  83. *
  84. * On platforms with GuC selects firmware for uploading
  85. */
  86. void intel_guc_fw_init_early(struct intel_guc *guc)
  87. {
  88. struct intel_uc_fw *guc_fw = &guc->fw;
  89. intel_uc_fw_init(guc_fw, INTEL_UC_FW_TYPE_GUC);
  90. guc_fw_select(guc_fw);
  91. }
  92. static void guc_prepare_xfer(struct intel_guc *guc)
  93. {
  94. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  95. /* Must program this register before loading the ucode with DMA */
  96. I915_WRITE(GUC_SHIM_CONTROL, GUC_DISABLE_SRAM_INIT_TO_ZEROES |
  97. GUC_ENABLE_READ_CACHE_LOGIC |
  98. GUC_ENABLE_MIA_CACHING |
  99. GUC_ENABLE_READ_CACHE_FOR_SRAM_DATA |
  100. GUC_ENABLE_READ_CACHE_FOR_WOPCM_DATA |
  101. GUC_ENABLE_MIA_CLOCK_GATING);
  102. if (IS_GEN9_LP(dev_priv))
  103. I915_WRITE(GEN9LP_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  104. else
  105. I915_WRITE(GEN9_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  106. if (IS_GEN9(dev_priv)) {
  107. /* DOP Clock Gating Enable for GuC clocks */
  108. I915_WRITE(GEN7_MISCCPCTL, (GEN8_DOP_CLOCK_GATE_GUC_ENABLE |
  109. I915_READ(GEN7_MISCCPCTL)));
  110. /* allows for 5us (in 10ns units) before GT can go to RC6 */
  111. I915_WRITE(GUC_ARAT_C6DIS, 0x1FF);
  112. }
  113. }
  114. /* Copy RSA signature from the fw image to HW for verification */
  115. static int guc_xfer_rsa(struct intel_guc *guc, struct i915_vma *vma)
  116. {
  117. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  118. struct intel_uc_fw *guc_fw = &guc->fw;
  119. struct sg_table *sg = vma->pages;
  120. u32 rsa[UOS_RSA_SCRATCH_COUNT];
  121. int i;
  122. if (sg_pcopy_to_buffer(sg->sgl, sg->nents, rsa, sizeof(rsa),
  123. guc_fw->rsa_offset) != sizeof(rsa))
  124. return -EINVAL;
  125. for (i = 0; i < UOS_RSA_SCRATCH_COUNT; i++)
  126. I915_WRITE(UOS_RSA_SCRATCH(i), rsa[i]);
  127. return 0;
  128. }
  129. /*
  130. * Transfer the firmware image to RAM for execution by the microcontroller.
  131. *
  132. * Architecturally, the DMA engine is bidirectional, and can potentially even
  133. * transfer between GTT locations. This functionality is left out of the API
  134. * for now as there is no need for it.
  135. */
  136. static int guc_xfer_ucode(struct intel_guc *guc, struct i915_vma *vma)
  137. {
  138. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  139. struct intel_uc_fw *guc_fw = &guc->fw;
  140. unsigned long offset;
  141. u32 status;
  142. int ret;
  143. /*
  144. * The header plus uCode will be copied to WOPCM via DMA, excluding any
  145. * other components
  146. */
  147. I915_WRITE(DMA_COPY_SIZE, guc_fw->header_size + guc_fw->ucode_size);
  148. /* Set the source address for the new blob */
  149. offset = guc_ggtt_offset(vma) + guc_fw->header_offset;
  150. I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
  151. I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
  152. /*
  153. * Set the DMA destination. Current uCode expects the code to be
  154. * loaded at 8k; locations below this are used for the stack.
  155. */
  156. I915_WRITE(DMA_ADDR_1_LOW, 0x2000);
  157. I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
  158. /* Finally start the DMA */
  159. I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));
  160. /* Wait for DMA to finish */
  161. ret = __intel_wait_for_register_fw(dev_priv, DMA_CTRL, START_DMA, 0,
  162. 2, 100, &status);
  163. DRM_DEBUG_DRIVER("GuC DMA status %#x\n", status);
  164. return ret;
  165. }
  166. /*
  167. * Read the GuC status register (GUC_STATUS) and store it in the
  168. * specified location; then return a boolean indicating whether
  169. * the value matches either of two values representing completion
  170. * of the GuC boot process.
  171. *
  172. * This is used for polling the GuC status in a wait_for()
  173. * loop below.
  174. */
  175. static inline bool guc_ready(struct intel_guc *guc, u32 *status)
  176. {
  177. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  178. u32 val = I915_READ(GUC_STATUS);
  179. u32 uk_val = val & GS_UKERNEL_MASK;
  180. *status = val;
  181. return (uk_val == GS_UKERNEL_READY) ||
  182. ((val & GS_MIA_CORE_STATE) && (uk_val == GS_UKERNEL_LAPIC_DONE));
  183. }
  184. static int guc_wait_ucode(struct intel_guc *guc)
  185. {
  186. u32 status;
  187. int ret;
  188. /*
  189. * Wait for the GuC to start up.
  190. * NB: Docs recommend not using the interrupt for completion.
  191. * Measurements indicate this should take no more than 20ms, so a
  192. * timeout here indicates that the GuC has failed and is unusable.
  193. * (Higher levels of the driver will attempt to fall back to
  194. * execlist mode if this happens.)
  195. */
  196. ret = wait_for(guc_ready(guc, &status), 100);
  197. DRM_DEBUG_DRIVER("GuC status %#x\n", status);
  198. if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
  199. DRM_ERROR("GuC firmware signature verification failed\n");
  200. ret = -ENOEXEC;
  201. }
  202. return ret;
  203. }
  204. /*
  205. * Load the GuC firmware blob into the MinuteIA.
  206. */
  207. static int guc_fw_xfer(struct intel_uc_fw *guc_fw, struct i915_vma *vma)
  208. {
  209. struct intel_guc *guc = container_of(guc_fw, struct intel_guc, fw);
  210. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  211. int ret;
  212. GEM_BUG_ON(guc_fw->type != INTEL_UC_FW_TYPE_GUC);
  213. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  214. guc_prepare_xfer(guc);
  215. /*
  216. * Note that GuC needs the CSS header plus uKernel code to be copied
  217. * by the DMA engine in one operation, whereas the RSA signature is
  218. * loaded via MMIO.
  219. */
  220. ret = guc_xfer_rsa(guc, vma);
  221. if (ret)
  222. DRM_WARN("GuC firmware signature xfer error %d\n", ret);
  223. ret = guc_xfer_ucode(guc, vma);
  224. if (ret)
  225. DRM_WARN("GuC firmware code xfer error %d\n", ret);
  226. ret = guc_wait_ucode(guc);
  227. if (ret)
  228. DRM_ERROR("GuC firmware xfer error %d\n", ret);
  229. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  230. return ret;
  231. }
  232. /**
  233. * intel_guc_fw_upload() - finish preparing the GuC for activity
  234. * @guc: intel_guc structure
  235. *
  236. * Called during driver loading and also after a GPU reset.
  237. *
  238. * The main action required here it to load the GuC uCode into the device.
  239. * The firmware image should have already been fetched into memory by the
  240. * earlier call to intel_guc_init(), so here we need only check that
  241. * worked, and then transfer the image to the h/w.
  242. *
  243. * Return: non-zero code on error
  244. */
  245. int intel_guc_fw_upload(struct intel_guc *guc)
  246. {
  247. return intel_uc_fw_upload(&guc->fw, guc_fw_xfer);
  248. }