intel_guc_loader.c 14 KB

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