intel_guc_loader.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "i915_drv.h"
  30. #include "intel_uc.h"
  31. /**
  32. * DOC: GuC-specific firmware loader
  33. *
  34. * intel_guc:
  35. * Top level structure of guc. It handles firmware loading and manages client
  36. * pool and doorbells. intel_guc owns a i915_guc_client to replace the legacy
  37. * ExecList submission.
  38. *
  39. * Firmware versioning:
  40. * The firmware build process will generate a version header file with major and
  41. * minor version defined. The versions are built into CSS header of firmware.
  42. * i915 kernel driver set the minimal firmware version required per platform.
  43. * The firmware installation package will install (symbolic link) proper version
  44. * of firmware.
  45. *
  46. * GuC address space:
  47. * GuC does not allow any gfx GGTT address that falls into range [0, WOPCM_TOP),
  48. * which is reserved for Boot ROM, SRAM and WOPCM. Currently this top address is
  49. * 512K. In order to exclude 0-512K address space from GGTT, all gfx objects
  50. * used by GuC is pinned with PIN_OFFSET_BIAS along with size of WOPCM.
  51. *
  52. */
  53. #define SKL_FW_MAJOR 6
  54. #define SKL_FW_MINOR 1
  55. #define BXT_FW_MAJOR 8
  56. #define BXT_FW_MINOR 7
  57. #define KBL_FW_MAJOR 9
  58. #define KBL_FW_MINOR 14
  59. #define GLK_FW_MAJOR 10
  60. #define GLK_FW_MINOR 56
  61. #define GUC_FW_PATH(platform, major, minor) \
  62. "i915/" __stringify(platform) "_guc_ver" __stringify(major) "_" __stringify(minor) ".bin"
  63. #define I915_SKL_GUC_UCODE GUC_FW_PATH(skl, SKL_FW_MAJOR, SKL_FW_MINOR)
  64. MODULE_FIRMWARE(I915_SKL_GUC_UCODE);
  65. #define I915_BXT_GUC_UCODE GUC_FW_PATH(bxt, BXT_FW_MAJOR, BXT_FW_MINOR)
  66. MODULE_FIRMWARE(I915_BXT_GUC_UCODE);
  67. #define I915_KBL_GUC_UCODE GUC_FW_PATH(kbl, KBL_FW_MAJOR, KBL_FW_MINOR)
  68. MODULE_FIRMWARE(I915_KBL_GUC_UCODE);
  69. #define I915_GLK_GUC_UCODE GUC_FW_PATH(glk, GLK_FW_MAJOR, GLK_FW_MINOR)
  70. static u32 get_gttype(struct drm_i915_private *dev_priv)
  71. {
  72. /* XXX: GT type based on PCI device ID? field seems unused by fw */
  73. return 0;
  74. }
  75. static u32 get_core_family(struct drm_i915_private *dev_priv)
  76. {
  77. u32 gen = INTEL_GEN(dev_priv);
  78. switch (gen) {
  79. case 9:
  80. return GUC_CORE_FAMILY_GEN9;
  81. default:
  82. MISSING_CASE(gen);
  83. return GUC_CORE_FAMILY_UNKNOWN;
  84. }
  85. }
  86. /*
  87. * Initialise the GuC parameter block before starting the firmware
  88. * transfer. These parameters are read by the firmware on startup
  89. * and cannot be changed thereafter.
  90. */
  91. static void guc_params_init(struct drm_i915_private *dev_priv)
  92. {
  93. struct intel_guc *guc = &dev_priv->guc;
  94. u32 params[GUC_CTL_MAX_DWORDS];
  95. int i;
  96. memset(&params, 0, sizeof(params));
  97. params[GUC_CTL_DEVICE_INFO] |=
  98. (get_gttype(dev_priv) << GUC_CTL_GTTYPE_SHIFT) |
  99. (get_core_family(dev_priv) << GUC_CTL_COREFAMILY_SHIFT);
  100. /*
  101. * GuC ARAT increment is 10 ns. GuC default scheduler quantum is one
  102. * second. This ARAR is calculated by:
  103. * Scheduler-Quantum-in-ns / ARAT-increment-in-ns = 1000000000 / 10
  104. */
  105. params[GUC_CTL_ARAT_HIGH] = 0;
  106. params[GUC_CTL_ARAT_LOW] = 100000000;
  107. params[GUC_CTL_WA] |= GUC_CTL_WA_UK_BY_DRIVER;
  108. params[GUC_CTL_FEATURE] |= GUC_CTL_DISABLE_SCHEDULER |
  109. GUC_CTL_VCS2_ENABLED;
  110. params[GUC_CTL_LOG_PARAMS] = guc->log.flags;
  111. if (i915.guc_log_level >= 0) {
  112. params[GUC_CTL_DEBUG] =
  113. i915.guc_log_level << GUC_LOG_VERBOSITY_SHIFT;
  114. } else
  115. params[GUC_CTL_DEBUG] = GUC_LOG_DISABLED;
  116. /* If GuC submission is enabled, set up additional parameters here */
  117. if (i915.enable_guc_submission) {
  118. u32 ads = guc_ggtt_offset(guc->ads_vma) >> PAGE_SHIFT;
  119. u32 pgs = guc_ggtt_offset(dev_priv->guc.stage_desc_pool);
  120. u32 ctx_in_16 = GUC_MAX_STAGE_DESCRIPTORS / 16;
  121. params[GUC_CTL_DEBUG] |= ads << GUC_ADS_ADDR_SHIFT;
  122. params[GUC_CTL_DEBUG] |= GUC_ADS_ENABLED;
  123. pgs >>= PAGE_SHIFT;
  124. params[GUC_CTL_CTXINFO] = (pgs << GUC_CTL_BASE_ADDR_SHIFT) |
  125. (ctx_in_16 << GUC_CTL_CTXNUM_IN16_SHIFT);
  126. params[GUC_CTL_FEATURE] |= GUC_CTL_KERNEL_SUBMISSIONS;
  127. /* Unmask this bit to enable the GuC's internal scheduler */
  128. params[GUC_CTL_FEATURE] &= ~GUC_CTL_DISABLE_SCHEDULER;
  129. }
  130. I915_WRITE(SOFT_SCRATCH(0), 0);
  131. for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
  132. I915_WRITE(SOFT_SCRATCH(1 + i), params[i]);
  133. }
  134. /*
  135. * Read the GuC status register (GUC_STATUS) and store it in the
  136. * specified location; then return a boolean indicating whether
  137. * the value matches either of two values representing completion
  138. * of the GuC boot process.
  139. *
  140. * This is used for polling the GuC status in a wait_for()
  141. * loop below.
  142. */
  143. static inline bool guc_ucode_response(struct drm_i915_private *dev_priv,
  144. u32 *status)
  145. {
  146. u32 val = I915_READ(GUC_STATUS);
  147. u32 uk_val = val & GS_UKERNEL_MASK;
  148. *status = val;
  149. return (uk_val == GS_UKERNEL_READY ||
  150. ((val & GS_MIA_CORE_STATE) && uk_val == GS_UKERNEL_LAPIC_DONE));
  151. }
  152. /*
  153. * Transfer the firmware image to RAM for execution by the microcontroller.
  154. *
  155. * Architecturally, the DMA engine is bidirectional, and can potentially even
  156. * transfer between GTT locations. This functionality is left out of the API
  157. * for now as there is no need for it.
  158. *
  159. * Note that GuC needs the CSS header plus uKernel code to be copied by the
  160. * DMA engine in one operation, whereas the RSA signature is loaded via MMIO.
  161. */
  162. static int guc_ucode_xfer_dma(struct drm_i915_private *dev_priv,
  163. struct i915_vma *vma)
  164. {
  165. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  166. unsigned long offset;
  167. struct sg_table *sg = vma->pages;
  168. u32 status, rsa[UOS_RSA_SCRATCH_MAX_COUNT];
  169. int i, ret = 0;
  170. /* where RSA signature starts */
  171. offset = guc_fw->rsa_offset;
  172. /* Copy RSA signature from the fw image to HW for verification */
  173. sg_pcopy_to_buffer(sg->sgl, sg->nents, rsa, sizeof(rsa), offset);
  174. for (i = 0; i < UOS_RSA_SCRATCH_MAX_COUNT; i++)
  175. I915_WRITE(UOS_RSA_SCRATCH(i), rsa[i]);
  176. /* The header plus uCode will be copied to WOPCM via DMA, excluding any
  177. * other components */
  178. I915_WRITE(DMA_COPY_SIZE, guc_fw->header_size + guc_fw->ucode_size);
  179. /* Set the source address for the new blob */
  180. offset = guc_ggtt_offset(vma) + guc_fw->header_offset;
  181. I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
  182. I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
  183. /*
  184. * Set the DMA destination. Current uCode expects the code to be
  185. * loaded at 8k; locations below this are used for the stack.
  186. */
  187. I915_WRITE(DMA_ADDR_1_LOW, 0x2000);
  188. I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
  189. /* Finally start the DMA */
  190. I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));
  191. /*
  192. * Wait for the DMA to complete & the GuC to start up.
  193. * NB: Docs recommend not using the interrupt for completion.
  194. * Measurements indicate this should take no more than 20ms, so a
  195. * timeout here indicates that the GuC has failed and is unusable.
  196. * (Higher levels of the driver will attempt to fall back to
  197. * execlist mode if this happens.)
  198. */
  199. ret = wait_for(guc_ucode_response(dev_priv, &status), 100);
  200. DRM_DEBUG_DRIVER("DMA status 0x%x, GuC status 0x%x\n",
  201. I915_READ(DMA_CTRL), status);
  202. if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
  203. DRM_ERROR("GuC firmware signature verification failed\n");
  204. ret = -ENOEXEC;
  205. }
  206. DRM_DEBUG_DRIVER("returning %d\n", ret);
  207. return ret;
  208. }
  209. u32 intel_guc_wopcm_size(struct drm_i915_private *dev_priv)
  210. {
  211. u32 wopcm_size = GUC_WOPCM_TOP;
  212. /* On BXT, the top of WOPCM is reserved for RC6 context */
  213. if (IS_GEN9_LP(dev_priv))
  214. wopcm_size -= BXT_GUC_WOPCM_RC6_RESERVED;
  215. return wopcm_size;
  216. }
  217. /*
  218. * Load the GuC firmware blob into the MinuteIA.
  219. */
  220. static int guc_ucode_xfer(struct drm_i915_private *dev_priv)
  221. {
  222. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  223. struct i915_vma *vma;
  224. int ret;
  225. ret = i915_gem_object_set_to_gtt_domain(guc_fw->obj, false);
  226. if (ret) {
  227. DRM_DEBUG_DRIVER("set-domain failed %d\n", ret);
  228. return ret;
  229. }
  230. vma = i915_gem_object_ggtt_pin(guc_fw->obj, NULL, 0, 0,
  231. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  232. if (IS_ERR(vma)) {
  233. DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma));
  234. return PTR_ERR(vma);
  235. }
  236. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  237. /* Enable MIA caching. GuC clock gating is disabled. */
  238. I915_WRITE(GUC_SHIM_CONTROL, GUC_SHIM_CONTROL_VALUE);
  239. /* WaDisableMinuteIaClockGating:bxt */
  240. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  241. I915_WRITE(GUC_SHIM_CONTROL, (I915_READ(GUC_SHIM_CONTROL) &
  242. ~GUC_ENABLE_MIA_CLOCK_GATING));
  243. }
  244. /* WaC6DisallowByGfxPause:bxt */
  245. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
  246. I915_WRITE(GEN6_GFXPAUSE, 0x30FFF);
  247. if (IS_GEN9_LP(dev_priv))
  248. I915_WRITE(GEN9LP_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  249. else
  250. I915_WRITE(GEN9_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  251. if (IS_GEN9(dev_priv)) {
  252. /* DOP Clock Gating Enable for GuC clocks */
  253. I915_WRITE(GEN7_MISCCPCTL, (GEN8_DOP_CLOCK_GATE_GUC_ENABLE |
  254. I915_READ(GEN7_MISCCPCTL)));
  255. /* allows for 5us (in 10ns units) before GT can go to RC6 */
  256. I915_WRITE(GUC_ARAT_C6DIS, 0x1FF);
  257. }
  258. guc_params_init(dev_priv);
  259. ret = guc_ucode_xfer_dma(dev_priv, vma);
  260. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  261. /*
  262. * We keep the object pages for reuse during resume. But we can unpin it
  263. * now that DMA has completed, so it doesn't continue to take up space.
  264. */
  265. i915_vma_unpin(vma);
  266. return ret;
  267. }
  268. /**
  269. * intel_guc_init_hw() - finish preparing the GuC for activity
  270. * @guc: intel_guc structure
  271. *
  272. * Called during driver loading and also after a GPU reset.
  273. *
  274. * The main action required here it to load the GuC uCode into the device.
  275. * The firmware image should have already been fetched into memory by the
  276. * earlier call to intel_guc_init(), so here we need only check that
  277. * worked, and then transfer the image to the h/w.
  278. *
  279. * Return: non-zero code on error
  280. */
  281. int intel_guc_init_hw(struct intel_guc *guc)
  282. {
  283. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  284. const char *fw_path = guc->fw.path;
  285. int ret;
  286. DRM_DEBUG_DRIVER("GuC fw status: path %s, fetch %s, load %s\n",
  287. fw_path,
  288. intel_uc_fw_status_repr(guc->fw.fetch_status),
  289. intel_uc_fw_status_repr(guc->fw.load_status));
  290. if (guc->fw.fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
  291. return -EIO;
  292. guc->fw.load_status = INTEL_UC_FIRMWARE_PENDING;
  293. DRM_DEBUG_DRIVER("GuC fw status: fetch %s, load %s\n",
  294. intel_uc_fw_status_repr(guc->fw.fetch_status),
  295. intel_uc_fw_status_repr(guc->fw.load_status));
  296. ret = guc_ucode_xfer(dev_priv);
  297. if (ret)
  298. return -EAGAIN;
  299. guc->fw.load_status = INTEL_UC_FIRMWARE_SUCCESS;
  300. DRM_INFO("GuC %s (firmware %s [version %u.%u])\n",
  301. i915.enable_guc_submission ? "submission enabled" : "loaded",
  302. guc->fw.path,
  303. guc->fw.major_ver_found, guc->fw.minor_ver_found);
  304. return 0;
  305. }
  306. /**
  307. * intel_guc_select_fw() - selects GuC firmware for loading
  308. * @guc: intel_guc struct
  309. *
  310. * Return: zero when we know firmware, non-zero in other case
  311. */
  312. int intel_guc_select_fw(struct intel_guc *guc)
  313. {
  314. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  315. guc->fw.path = NULL;
  316. guc->fw.fetch_status = INTEL_UC_FIRMWARE_NONE;
  317. guc->fw.load_status = INTEL_UC_FIRMWARE_NONE;
  318. guc->fw.type = INTEL_UC_FW_TYPE_GUC;
  319. if (i915.guc_firmware_path) {
  320. guc->fw.path = i915.guc_firmware_path;
  321. guc->fw.major_ver_wanted = 0;
  322. guc->fw.minor_ver_wanted = 0;
  323. } else if (IS_SKYLAKE(dev_priv)) {
  324. guc->fw.path = I915_SKL_GUC_UCODE;
  325. guc->fw.major_ver_wanted = SKL_FW_MAJOR;
  326. guc->fw.minor_ver_wanted = SKL_FW_MINOR;
  327. } else if (IS_BROXTON(dev_priv)) {
  328. guc->fw.path = I915_BXT_GUC_UCODE;
  329. guc->fw.major_ver_wanted = BXT_FW_MAJOR;
  330. guc->fw.minor_ver_wanted = BXT_FW_MINOR;
  331. } else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
  332. guc->fw.path = I915_KBL_GUC_UCODE;
  333. guc->fw.major_ver_wanted = KBL_FW_MAJOR;
  334. guc->fw.minor_ver_wanted = KBL_FW_MINOR;
  335. } else if (IS_GEMINILAKE(dev_priv)) {
  336. guc->fw.path = I915_GLK_GUC_UCODE;
  337. guc->fw.major_ver_wanted = GLK_FW_MAJOR;
  338. guc->fw.minor_ver_wanted = GLK_FW_MINOR;
  339. } else {
  340. DRM_ERROR("No GuC firmware known for platform with GuC!\n");
  341. return -ENOENT;
  342. }
  343. return 0;
  344. }