intel_guc_loader.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 GUC_FW_PATH(platform, major, minor) \
  60. "i915/" __stringify(platform) "_guc_ver" __stringify(major) "_" __stringify(minor) ".bin"
  61. #define I915_SKL_GUC_UCODE GUC_FW_PATH(skl, SKL_FW_MAJOR, SKL_FW_MINOR)
  62. MODULE_FIRMWARE(I915_SKL_GUC_UCODE);
  63. #define I915_BXT_GUC_UCODE GUC_FW_PATH(bxt, BXT_FW_MAJOR, BXT_FW_MINOR)
  64. MODULE_FIRMWARE(I915_BXT_GUC_UCODE);
  65. #define I915_KBL_GUC_UCODE GUC_FW_PATH(kbl, KBL_FW_MAJOR, KBL_FW_MINOR)
  66. MODULE_FIRMWARE(I915_KBL_GUC_UCODE);
  67. static u32 get_gttype(struct drm_i915_private *dev_priv)
  68. {
  69. /* XXX: GT type based on PCI device ID? field seems unused by fw */
  70. return 0;
  71. }
  72. static u32 get_core_family(struct drm_i915_private *dev_priv)
  73. {
  74. u32 gen = INTEL_GEN(dev_priv);
  75. switch (gen) {
  76. case 9:
  77. return GFXCORE_FAMILY_GEN9;
  78. default:
  79. WARN(1, "GEN%d does not support GuC operation!\n", gen);
  80. return GFXCORE_FAMILY_UNKNOWN;
  81. }
  82. }
  83. /*
  84. * Initialise the GuC parameter block before starting the firmware
  85. * transfer. These parameters are read by the firmware on startup
  86. * and cannot be changed thereafter.
  87. */
  88. static void guc_params_init(struct drm_i915_private *dev_priv)
  89. {
  90. struct intel_guc *guc = &dev_priv->guc;
  91. u32 params[GUC_CTL_MAX_DWORDS];
  92. int i;
  93. memset(&params, 0, sizeof(params));
  94. params[GUC_CTL_DEVICE_INFO] |=
  95. (get_gttype(dev_priv) << GUC_CTL_GTTYPE_SHIFT) |
  96. (get_core_family(dev_priv) << GUC_CTL_COREFAMILY_SHIFT);
  97. /*
  98. * GuC ARAT increment is 10 ns. GuC default scheduler quantum is one
  99. * second. This ARAR is calculated by:
  100. * Scheduler-Quantum-in-ns / ARAT-increment-in-ns = 1000000000 / 10
  101. */
  102. params[GUC_CTL_ARAT_HIGH] = 0;
  103. params[GUC_CTL_ARAT_LOW] = 100000000;
  104. params[GUC_CTL_WA] |= GUC_CTL_WA_UK_BY_DRIVER;
  105. params[GUC_CTL_FEATURE] |= GUC_CTL_DISABLE_SCHEDULER |
  106. GUC_CTL_VCS2_ENABLED;
  107. params[GUC_CTL_LOG_PARAMS] = guc->log.flags;
  108. if (i915.guc_log_level >= 0) {
  109. params[GUC_CTL_DEBUG] =
  110. i915.guc_log_level << GUC_LOG_VERBOSITY_SHIFT;
  111. } else
  112. params[GUC_CTL_DEBUG] = GUC_LOG_DISABLED;
  113. /* If GuC submission is enabled, set up additional parameters here */
  114. if (i915.enable_guc_submission) {
  115. u32 ads = guc_ggtt_offset(guc->ads_vma) >> PAGE_SHIFT;
  116. u32 pgs = guc_ggtt_offset(dev_priv->guc.stage_desc_pool);
  117. u32 ctx_in_16 = GUC_MAX_STAGE_DESCRIPTORS / 16;
  118. params[GUC_CTL_DEBUG] |= ads << GUC_ADS_ADDR_SHIFT;
  119. params[GUC_CTL_DEBUG] |= GUC_ADS_ENABLED;
  120. pgs >>= PAGE_SHIFT;
  121. params[GUC_CTL_CTXINFO] = (pgs << GUC_CTL_BASE_ADDR_SHIFT) |
  122. (ctx_in_16 << GUC_CTL_CTXNUM_IN16_SHIFT);
  123. params[GUC_CTL_FEATURE] |= GUC_CTL_KERNEL_SUBMISSIONS;
  124. /* Unmask this bit to enable the GuC's internal scheduler */
  125. params[GUC_CTL_FEATURE] &= ~GUC_CTL_DISABLE_SCHEDULER;
  126. }
  127. I915_WRITE(SOFT_SCRATCH(0), 0);
  128. for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
  129. I915_WRITE(SOFT_SCRATCH(1 + i), params[i]);
  130. }
  131. /*
  132. * Read the GuC status register (GUC_STATUS) and store it in the
  133. * specified location; then return a boolean indicating whether
  134. * the value matches either of two values representing completion
  135. * of the GuC boot process.
  136. *
  137. * This is used for polling the GuC status in a wait_for()
  138. * loop below.
  139. */
  140. static inline bool guc_ucode_response(struct drm_i915_private *dev_priv,
  141. u32 *status)
  142. {
  143. u32 val = I915_READ(GUC_STATUS);
  144. u32 uk_val = val & GS_UKERNEL_MASK;
  145. *status = val;
  146. return (uk_val == GS_UKERNEL_READY ||
  147. ((val & GS_MIA_CORE_STATE) && uk_val == GS_UKERNEL_LAPIC_DONE));
  148. }
  149. /*
  150. * Transfer the firmware image to RAM for execution by the microcontroller.
  151. *
  152. * Architecturally, the DMA engine is bidirectional, and can potentially even
  153. * transfer between GTT locations. This functionality is left out of the API
  154. * for now as there is no need for it.
  155. *
  156. * Note that GuC needs the CSS header plus uKernel code to be copied by the
  157. * DMA engine in one operation, whereas the RSA signature is loaded via MMIO.
  158. */
  159. static int guc_ucode_xfer_dma(struct drm_i915_private *dev_priv,
  160. struct i915_vma *vma)
  161. {
  162. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  163. unsigned long offset;
  164. struct sg_table *sg = vma->pages;
  165. u32 status, rsa[UOS_RSA_SCRATCH_MAX_COUNT];
  166. int i, ret = 0;
  167. /* where RSA signature starts */
  168. offset = guc_fw->rsa_offset;
  169. /* Copy RSA signature from the fw image to HW for verification */
  170. sg_pcopy_to_buffer(sg->sgl, sg->nents, rsa, sizeof(rsa), offset);
  171. for (i = 0; i < UOS_RSA_SCRATCH_MAX_COUNT; i++)
  172. I915_WRITE(UOS_RSA_SCRATCH(i), rsa[i]);
  173. /* The header plus uCode will be copied to WOPCM via DMA, excluding any
  174. * other components */
  175. I915_WRITE(DMA_COPY_SIZE, guc_fw->header_size + guc_fw->ucode_size);
  176. /* Set the source address for the new blob */
  177. offset = guc_ggtt_offset(vma) + guc_fw->header_offset;
  178. I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
  179. I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
  180. /*
  181. * Set the DMA destination. Current uCode expects the code to be
  182. * loaded at 8k; locations below this are used for the stack.
  183. */
  184. I915_WRITE(DMA_ADDR_1_LOW, 0x2000);
  185. I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
  186. /* Finally start the DMA */
  187. I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));
  188. /*
  189. * Wait for the DMA to complete & 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_ucode_response(dev_priv, &status), 100);
  197. DRM_DEBUG_DRIVER("DMA status 0x%x, GuC status 0x%x\n",
  198. I915_READ(DMA_CTRL), status);
  199. if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
  200. DRM_ERROR("GuC firmware signature verification failed\n");
  201. ret = -ENOEXEC;
  202. }
  203. DRM_DEBUG_DRIVER("returning %d\n", ret);
  204. return ret;
  205. }
  206. u32 intel_guc_wopcm_size(struct drm_i915_private *dev_priv)
  207. {
  208. u32 wopcm_size = GUC_WOPCM_TOP;
  209. /* On BXT, the top of WOPCM is reserved for RC6 context */
  210. if (IS_GEN9_LP(dev_priv))
  211. wopcm_size -= BXT_GUC_WOPCM_RC6_RESERVED;
  212. return wopcm_size;
  213. }
  214. /*
  215. * Load the GuC firmware blob into the MinuteIA.
  216. */
  217. static int guc_ucode_xfer(struct drm_i915_private *dev_priv)
  218. {
  219. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  220. struct i915_vma *vma;
  221. int ret;
  222. ret = i915_gem_object_set_to_gtt_domain(guc_fw->obj, false);
  223. if (ret) {
  224. DRM_DEBUG_DRIVER("set-domain failed %d\n", ret);
  225. return ret;
  226. }
  227. vma = i915_gem_object_ggtt_pin(guc_fw->obj, NULL, 0, 0,
  228. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  229. if (IS_ERR(vma)) {
  230. DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma));
  231. return PTR_ERR(vma);
  232. }
  233. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  234. /* init WOPCM */
  235. I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
  236. I915_WRITE(DMA_GUC_WOPCM_OFFSET, GUC_WOPCM_OFFSET_VALUE);
  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)) {
  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 {
  336. DRM_ERROR("No GuC firmware known for platform with GuC!\n");
  337. return -ENOENT;
  338. }
  339. return 0;
  340. }