intel_guc_loader.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 <linux/firmware.h>
  30. #include "i915_drv.h"
  31. #include "intel_uc.h"
  32. /**
  33. * DOC: GuC-specific firmware loader
  34. *
  35. * intel_guc:
  36. * Top level structure of guc. It handles firmware loading and manages client
  37. * pool and doorbells. intel_guc owns a i915_guc_client to replace the legacy
  38. * ExecList submission.
  39. *
  40. * Firmware versioning:
  41. * The firmware build process will generate a version header file with major and
  42. * minor version defined. The versions are built into CSS header of firmware.
  43. * i915 kernel driver set the minimal firmware version required per platform.
  44. * The firmware installation package will install (symbolic link) proper version
  45. * of firmware.
  46. *
  47. * GuC address space:
  48. * GuC does not allow any gfx GGTT address that falls into range [0, WOPCM_TOP),
  49. * which is reserved for Boot ROM, SRAM and WOPCM. Currently this top address is
  50. * 512K. In order to exclude 0-512K address space from GGTT, all gfx objects
  51. * used by GuC is pinned with PIN_OFFSET_BIAS along with size of WOPCM.
  52. *
  53. */
  54. #define SKL_FW_MAJOR 6
  55. #define SKL_FW_MINOR 1
  56. #define BXT_FW_MAJOR 8
  57. #define BXT_FW_MINOR 7
  58. #define KBL_FW_MAJOR 9
  59. #define KBL_FW_MINOR 14
  60. #define GUC_FW_PATH(platform, major, minor) \
  61. "i915/" __stringify(platform) "_guc_ver" __stringify(major) "_" __stringify(minor) ".bin"
  62. #define I915_SKL_GUC_UCODE GUC_FW_PATH(skl, SKL_FW_MAJOR, SKL_FW_MINOR)
  63. MODULE_FIRMWARE(I915_SKL_GUC_UCODE);
  64. #define I915_BXT_GUC_UCODE GUC_FW_PATH(bxt, BXT_FW_MAJOR, BXT_FW_MINOR)
  65. MODULE_FIRMWARE(I915_BXT_GUC_UCODE);
  66. #define I915_KBL_GUC_UCODE GUC_FW_PATH(kbl, KBL_FW_MAJOR, KBL_FW_MINOR)
  67. MODULE_FIRMWARE(I915_KBL_GUC_UCODE);
  68. /* User-friendly representation of an enum */
  69. const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
  70. {
  71. switch (status) {
  72. case INTEL_UC_FIRMWARE_FAIL:
  73. return "FAIL";
  74. case INTEL_UC_FIRMWARE_NONE:
  75. return "NONE";
  76. case INTEL_UC_FIRMWARE_PENDING:
  77. return "PENDING";
  78. case INTEL_UC_FIRMWARE_SUCCESS:
  79. return "SUCCESS";
  80. default:
  81. return "UNKNOWN!";
  82. }
  83. };
  84. static void guc_interrupts_release(struct drm_i915_private *dev_priv)
  85. {
  86. struct intel_engine_cs *engine;
  87. enum intel_engine_id id;
  88. int irqs;
  89. /* tell all command streamers NOT to forward interrupts or vblank to GuC */
  90. irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
  91. irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
  92. for_each_engine(engine, dev_priv, id)
  93. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  94. /* route all GT interrupts to the host */
  95. I915_WRITE(GUC_BCS_RCS_IER, 0);
  96. I915_WRITE(GUC_VCS2_VCS1_IER, 0);
  97. I915_WRITE(GUC_WD_VECS_IER, 0);
  98. }
  99. static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
  100. {
  101. struct intel_engine_cs *engine;
  102. enum intel_engine_id id;
  103. int irqs;
  104. u32 tmp;
  105. /* tell all command streamers to forward interrupts (but not vblank) to GuC */
  106. irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
  107. for_each_engine(engine, dev_priv, id)
  108. I915_WRITE(RING_MODE_GEN7(engine), irqs);
  109. /* route USER_INTERRUPT to Host, all others are sent to GuC. */
  110. irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
  111. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  112. /* These three registers have the same bit definitions */
  113. I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
  114. I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
  115. I915_WRITE(GUC_WD_VECS_IER, ~irqs);
  116. /*
  117. * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
  118. * (unmasked) PM interrupts to the GuC. All other bits of this
  119. * register *disable* generation of a specific interrupt.
  120. *
  121. * 'pm_intr_keep' indicates bits that are NOT to be set when
  122. * writing to the PM interrupt mask register, i.e. interrupts
  123. * that must not be disabled.
  124. *
  125. * If the GuC is handling these interrupts, then we must not let
  126. * the PM code disable ANY interrupt that the GuC is expecting.
  127. * So for each ENABLED (0) bit in this register, we must SET the
  128. * bit in pm_intr_keep so that it's left enabled for the GuC.
  129. *
  130. * OTOH the REDIRECT_TO_GUC bit is initially SET in pm_intr_keep
  131. * (so interrupts go to the DISPLAY unit at first); but here we
  132. * need to CLEAR that bit, which will result in the register bit
  133. * being left SET!
  134. */
  135. tmp = I915_READ(GEN6_PMINTRMSK);
  136. if (tmp & GEN8_PMINTR_REDIRECT_TO_GUC) {
  137. dev_priv->rps.pm_intr_keep |= ~tmp;
  138. dev_priv->rps.pm_intr_keep &= ~GEN8_PMINTR_REDIRECT_TO_GUC;
  139. }
  140. }
  141. static u32 get_gttype(struct drm_i915_private *dev_priv)
  142. {
  143. /* XXX: GT type based on PCI device ID? field seems unused by fw */
  144. return 0;
  145. }
  146. static u32 get_core_family(struct drm_i915_private *dev_priv)
  147. {
  148. u32 gen = INTEL_GEN(dev_priv);
  149. switch (gen) {
  150. case 9:
  151. return GFXCORE_FAMILY_GEN9;
  152. default:
  153. WARN(1, "GEN%d does not support GuC operation!\n", gen);
  154. return GFXCORE_FAMILY_UNKNOWN;
  155. }
  156. }
  157. /*
  158. * Initialise the GuC parameter block before starting the firmware
  159. * transfer. These parameters are read by the firmware on startup
  160. * and cannot be changed thereafter.
  161. */
  162. static void guc_params_init(struct drm_i915_private *dev_priv)
  163. {
  164. struct intel_guc *guc = &dev_priv->guc;
  165. u32 params[GUC_CTL_MAX_DWORDS];
  166. int i;
  167. memset(&params, 0, sizeof(params));
  168. params[GUC_CTL_DEVICE_INFO] |=
  169. (get_gttype(dev_priv) << GUC_CTL_GTTYPE_SHIFT) |
  170. (get_core_family(dev_priv) << GUC_CTL_COREFAMILY_SHIFT);
  171. /*
  172. * GuC ARAT increment is 10 ns. GuC default scheduler quantum is one
  173. * second. This ARAR is calculated by:
  174. * Scheduler-Quantum-in-ns / ARAT-increment-in-ns = 1000000000 / 10
  175. */
  176. params[GUC_CTL_ARAT_HIGH] = 0;
  177. params[GUC_CTL_ARAT_LOW] = 100000000;
  178. params[GUC_CTL_WA] |= GUC_CTL_WA_UK_BY_DRIVER;
  179. params[GUC_CTL_FEATURE] |= GUC_CTL_DISABLE_SCHEDULER |
  180. GUC_CTL_VCS2_ENABLED;
  181. params[GUC_CTL_LOG_PARAMS] = guc->log.flags;
  182. if (i915.guc_log_level >= 0) {
  183. params[GUC_CTL_DEBUG] =
  184. i915.guc_log_level << GUC_LOG_VERBOSITY_SHIFT;
  185. } else
  186. params[GUC_CTL_DEBUG] = GUC_LOG_DISABLED;
  187. if (guc->ads_vma) {
  188. u32 ads = guc_ggtt_offset(guc->ads_vma) >> PAGE_SHIFT;
  189. params[GUC_CTL_DEBUG] |= ads << GUC_ADS_ADDR_SHIFT;
  190. params[GUC_CTL_DEBUG] |= GUC_ADS_ENABLED;
  191. }
  192. /* If GuC submission is enabled, set up additional parameters here */
  193. if (i915.enable_guc_submission) {
  194. u32 pgs = guc_ggtt_offset(dev_priv->guc.ctx_pool_vma);
  195. u32 ctx_in_16 = GUC_MAX_GPU_CONTEXTS / 16;
  196. pgs >>= PAGE_SHIFT;
  197. params[GUC_CTL_CTXINFO] = (pgs << GUC_CTL_BASE_ADDR_SHIFT) |
  198. (ctx_in_16 << GUC_CTL_CTXNUM_IN16_SHIFT);
  199. params[GUC_CTL_FEATURE] |= GUC_CTL_KERNEL_SUBMISSIONS;
  200. /* Unmask this bit to enable the GuC's internal scheduler */
  201. params[GUC_CTL_FEATURE] &= ~GUC_CTL_DISABLE_SCHEDULER;
  202. }
  203. I915_WRITE(SOFT_SCRATCH(0), 0);
  204. for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
  205. I915_WRITE(SOFT_SCRATCH(1 + i), params[i]);
  206. }
  207. /*
  208. * Read the GuC status register (GUC_STATUS) and store it in the
  209. * specified location; then return a boolean indicating whether
  210. * the value matches either of two values representing completion
  211. * of the GuC boot process.
  212. *
  213. * This is used for polling the GuC status in a wait_for()
  214. * loop below.
  215. */
  216. static inline bool guc_ucode_response(struct drm_i915_private *dev_priv,
  217. u32 *status)
  218. {
  219. u32 val = I915_READ(GUC_STATUS);
  220. u32 uk_val = val & GS_UKERNEL_MASK;
  221. *status = val;
  222. return (uk_val == GS_UKERNEL_READY ||
  223. ((val & GS_MIA_CORE_STATE) && uk_val == GS_UKERNEL_LAPIC_DONE));
  224. }
  225. /*
  226. * Transfer the firmware image to RAM for execution by the microcontroller.
  227. *
  228. * Architecturally, the DMA engine is bidirectional, and can potentially even
  229. * transfer between GTT locations. This functionality is left out of the API
  230. * for now as there is no need for it.
  231. *
  232. * Note that GuC needs the CSS header plus uKernel code to be copied by the
  233. * DMA engine in one operation, whereas the RSA signature is loaded via MMIO.
  234. */
  235. static int guc_ucode_xfer_dma(struct drm_i915_private *dev_priv,
  236. struct i915_vma *vma)
  237. {
  238. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  239. unsigned long offset;
  240. struct sg_table *sg = vma->pages;
  241. u32 status, rsa[UOS_RSA_SCRATCH_MAX_COUNT];
  242. int i, ret = 0;
  243. /* where RSA signature starts */
  244. offset = guc_fw->rsa_offset;
  245. /* Copy RSA signature from the fw image to HW for verification */
  246. sg_pcopy_to_buffer(sg->sgl, sg->nents, rsa, sizeof(rsa), offset);
  247. for (i = 0; i < UOS_RSA_SCRATCH_MAX_COUNT; i++)
  248. I915_WRITE(UOS_RSA_SCRATCH(i), rsa[i]);
  249. /* The header plus uCode will be copied to WOPCM via DMA, excluding any
  250. * other components */
  251. I915_WRITE(DMA_COPY_SIZE, guc_fw->header_size + guc_fw->ucode_size);
  252. /* Set the source address for the new blob */
  253. offset = guc_ggtt_offset(vma) + guc_fw->header_offset;
  254. I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
  255. I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
  256. /*
  257. * Set the DMA destination. Current uCode expects the code to be
  258. * loaded at 8k; locations below this are used for the stack.
  259. */
  260. I915_WRITE(DMA_ADDR_1_LOW, 0x2000);
  261. I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
  262. /* Finally start the DMA */
  263. I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));
  264. /*
  265. * Wait for the DMA to complete & the GuC to start up.
  266. * NB: Docs recommend not using the interrupt for completion.
  267. * Measurements indicate this should take no more than 20ms, so a
  268. * timeout here indicates that the GuC has failed and is unusable.
  269. * (Higher levels of the driver will attempt to fall back to
  270. * execlist mode if this happens.)
  271. */
  272. ret = wait_for(guc_ucode_response(dev_priv, &status), 100);
  273. DRM_DEBUG_DRIVER("DMA status 0x%x, GuC status 0x%x\n",
  274. I915_READ(DMA_CTRL), status);
  275. if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
  276. DRM_ERROR("GuC firmware signature verification failed\n");
  277. ret = -ENOEXEC;
  278. }
  279. DRM_DEBUG_DRIVER("returning %d\n", ret);
  280. return ret;
  281. }
  282. u32 intel_guc_wopcm_size(struct drm_i915_private *dev_priv)
  283. {
  284. u32 wopcm_size = GUC_WOPCM_TOP;
  285. /* On BXT, the top of WOPCM is reserved for RC6 context */
  286. if (IS_GEN9_LP(dev_priv))
  287. wopcm_size -= BXT_GUC_WOPCM_RC6_RESERVED;
  288. return wopcm_size;
  289. }
  290. /*
  291. * Load the GuC firmware blob into the MinuteIA.
  292. */
  293. static int guc_ucode_xfer(struct drm_i915_private *dev_priv)
  294. {
  295. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  296. struct i915_vma *vma;
  297. int ret;
  298. ret = i915_gem_object_set_to_gtt_domain(guc_fw->obj, false);
  299. if (ret) {
  300. DRM_DEBUG_DRIVER("set-domain failed %d\n", ret);
  301. return ret;
  302. }
  303. vma = i915_gem_object_ggtt_pin(guc_fw->obj, NULL, 0, 0,
  304. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  305. if (IS_ERR(vma)) {
  306. DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma));
  307. return PTR_ERR(vma);
  308. }
  309. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  310. /* init WOPCM */
  311. I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
  312. I915_WRITE(DMA_GUC_WOPCM_OFFSET, GUC_WOPCM_OFFSET_VALUE);
  313. /* Enable MIA caching. GuC clock gating is disabled. */
  314. I915_WRITE(GUC_SHIM_CONTROL, GUC_SHIM_CONTROL_VALUE);
  315. /* WaDisableMinuteIaClockGating:bxt */
  316. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  317. I915_WRITE(GUC_SHIM_CONTROL, (I915_READ(GUC_SHIM_CONTROL) &
  318. ~GUC_ENABLE_MIA_CLOCK_GATING));
  319. }
  320. /* WaC6DisallowByGfxPause:bxt */
  321. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
  322. I915_WRITE(GEN6_GFXPAUSE, 0x30FFF);
  323. if (IS_GEN9_LP(dev_priv))
  324. I915_WRITE(GEN9LP_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  325. else
  326. I915_WRITE(GEN9_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
  327. if (IS_GEN9(dev_priv)) {
  328. /* DOP Clock Gating Enable for GuC clocks */
  329. I915_WRITE(GEN7_MISCCPCTL, (GEN8_DOP_CLOCK_GATE_GUC_ENABLE |
  330. I915_READ(GEN7_MISCCPCTL)));
  331. /* allows for 5us (in 10ns units) before GT can go to RC6 */
  332. I915_WRITE(GUC_ARAT_C6DIS, 0x1FF);
  333. }
  334. guc_params_init(dev_priv);
  335. ret = guc_ucode_xfer_dma(dev_priv, vma);
  336. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  337. /*
  338. * We keep the object pages for reuse during resume. But we can unpin it
  339. * now that DMA has completed, so it doesn't continue to take up space.
  340. */
  341. i915_vma_unpin(vma);
  342. return ret;
  343. }
  344. static int guc_hw_reset(struct drm_i915_private *dev_priv)
  345. {
  346. int ret;
  347. u32 guc_status;
  348. ret = intel_guc_reset(dev_priv);
  349. if (ret) {
  350. DRM_ERROR("GuC reset failed, ret = %d\n", ret);
  351. return ret;
  352. }
  353. guc_status = I915_READ(GUC_STATUS);
  354. WARN(!(guc_status & GS_MIA_IN_RESET),
  355. "GuC status: 0x%x, MIA core expected to be in reset\n", guc_status);
  356. return ret;
  357. }
  358. /**
  359. * intel_guc_setup() - finish preparing the GuC for activity
  360. * @dev_priv: i915 device private
  361. *
  362. * Called from gem_init_hw() during driver loading and also after a GPU reset.
  363. *
  364. * The main action required here it to load the GuC uCode into the device.
  365. * The firmware image should have already been fetched into memory by the
  366. * earlier call to intel_guc_init(), so here we need only check that worked,
  367. * and then transfer the image to the h/w.
  368. *
  369. * Return: non-zero code on error
  370. */
  371. int intel_guc_setup(struct drm_i915_private *dev_priv)
  372. {
  373. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  374. const char *fw_path = guc_fw->path;
  375. int retries, ret, err;
  376. DRM_DEBUG_DRIVER("GuC fw status: path %s, fetch %s, load %s\n",
  377. fw_path,
  378. intel_uc_fw_status_repr(guc_fw->fetch_status),
  379. intel_uc_fw_status_repr(guc_fw->load_status));
  380. /* Loading forbidden, or no firmware to load? */
  381. if (!i915.enable_guc_loading) {
  382. err = 0;
  383. goto fail;
  384. } else if (fw_path == NULL) {
  385. /* Device is known to have no uCode (e.g. no GuC) */
  386. err = -ENXIO;
  387. goto fail;
  388. } else if (*fw_path == '\0') {
  389. /* Device has a GuC but we don't know what f/w to load? */
  390. WARN(1, "No GuC firmware known for this platform!\n");
  391. err = -ENODEV;
  392. goto fail;
  393. }
  394. /* Fetch failed, or already fetched but failed to load? */
  395. if (guc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS) {
  396. err = -EIO;
  397. goto fail;
  398. } else if (guc_fw->load_status == INTEL_UC_FIRMWARE_FAIL) {
  399. err = -ENOEXEC;
  400. goto fail;
  401. }
  402. guc_interrupts_release(dev_priv);
  403. gen9_reset_guc_interrupts(dev_priv);
  404. /* We need to notify the guc whenever we change the GGTT */
  405. i915_ggtt_enable_guc(dev_priv);
  406. guc_fw->load_status = INTEL_UC_FIRMWARE_PENDING;
  407. DRM_DEBUG_DRIVER("GuC fw status: fetch %s, load %s\n",
  408. intel_uc_fw_status_repr(guc_fw->fetch_status),
  409. intel_uc_fw_status_repr(guc_fw->load_status));
  410. err = i915_guc_submission_init(dev_priv);
  411. if (err)
  412. goto fail;
  413. /*
  414. * WaEnableuKernelHeaderValidFix:skl,bxt
  415. * For BXT, this is only upto B0 but below WA is required for later
  416. * steppings also so this is extended as well.
  417. */
  418. /* WaEnableGuCBootHashCheckNotSet:skl,bxt */
  419. for (retries = 3; ; ) {
  420. /*
  421. * Always reset the GuC just before (re)loading, so
  422. * that the state and timing are fairly predictable
  423. */
  424. err = guc_hw_reset(dev_priv);
  425. if (err)
  426. goto fail;
  427. intel_huc_load(dev_priv);
  428. err = guc_ucode_xfer(dev_priv);
  429. if (!err)
  430. break;
  431. if (--retries == 0)
  432. goto fail;
  433. DRM_INFO("GuC fw load failed: %d; will reset and "
  434. "retry %d more time(s)\n", err, retries);
  435. }
  436. guc_fw->load_status = INTEL_UC_FIRMWARE_SUCCESS;
  437. DRM_DEBUG_DRIVER("GuC fw status: fetch %s, load %s\n",
  438. intel_uc_fw_status_repr(guc_fw->fetch_status),
  439. intel_uc_fw_status_repr(guc_fw->load_status));
  440. intel_guc_auth_huc(dev_priv);
  441. if (i915.enable_guc_submission) {
  442. if (i915.guc_log_level >= 0)
  443. gen9_enable_guc_interrupts(dev_priv);
  444. err = i915_guc_submission_enable(dev_priv);
  445. if (err)
  446. goto fail;
  447. guc_interrupts_capture(dev_priv);
  448. }
  449. return 0;
  450. fail:
  451. if (guc_fw->load_status == INTEL_UC_FIRMWARE_PENDING)
  452. guc_fw->load_status = INTEL_UC_FIRMWARE_FAIL;
  453. guc_interrupts_release(dev_priv);
  454. i915_guc_submission_disable(dev_priv);
  455. i915_guc_submission_fini(dev_priv);
  456. i915_ggtt_disable_guc(dev_priv);
  457. /*
  458. * We've failed to load the firmware :(
  459. *
  460. * Decide whether to disable GuC submission and fall back to
  461. * execlist mode, and whether to hide the error by returning
  462. * zero or to return -EIO, which the caller will treat as a
  463. * nonfatal error (i.e. it doesn't prevent driver load, but
  464. * marks the GPU as wedged until reset).
  465. */
  466. if (i915.enable_guc_loading > 1) {
  467. ret = -EIO;
  468. } else if (i915.enable_guc_submission > 1) {
  469. ret = -EIO;
  470. } else {
  471. ret = 0;
  472. }
  473. if (err == 0 && !HAS_GUC_UCODE(dev_priv))
  474. ; /* Don't mention the GuC! */
  475. else if (err == 0)
  476. DRM_INFO("GuC firmware load skipped\n");
  477. else if (ret != -EIO)
  478. DRM_NOTE("GuC firmware load failed: %d\n", err);
  479. else
  480. DRM_WARN("GuC firmware load failed: %d\n", err);
  481. if (i915.enable_guc_submission) {
  482. if (fw_path == NULL)
  483. DRM_INFO("GuC submission without firmware not supported\n");
  484. if (ret == 0)
  485. DRM_NOTE("Falling back from GuC submission to execlist mode\n");
  486. else
  487. DRM_ERROR("GuC init failed: %d\n", ret);
  488. }
  489. i915.enable_guc_submission = 0;
  490. return ret;
  491. }
  492. void intel_uc_fw_fetch(struct drm_i915_private *dev_priv,
  493. struct intel_uc_fw *uc_fw)
  494. {
  495. struct pci_dev *pdev = dev_priv->drm.pdev;
  496. struct drm_i915_gem_object *obj;
  497. const struct firmware *fw = NULL;
  498. struct uc_css_header *css;
  499. size_t size;
  500. int err;
  501. DRM_DEBUG_DRIVER("before requesting firmware: uC fw fetch status %s\n",
  502. intel_uc_fw_status_repr(uc_fw->fetch_status));
  503. err = request_firmware(&fw, uc_fw->path, &pdev->dev);
  504. if (err)
  505. goto fail;
  506. if (!fw)
  507. goto fail;
  508. DRM_DEBUG_DRIVER("fetch uC fw from %s succeeded, fw %p\n",
  509. uc_fw->path, fw);
  510. /* Check the size of the blob before examining buffer contents */
  511. if (fw->size < sizeof(struct uc_css_header)) {
  512. DRM_NOTE("Firmware header is missing\n");
  513. goto fail;
  514. }
  515. css = (struct uc_css_header *)fw->data;
  516. /* Firmware bits always start from header */
  517. uc_fw->header_offset = 0;
  518. uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
  519. css->key_size_dw - css->exponent_size_dw) * sizeof(u32);
  520. if (uc_fw->header_size != sizeof(struct uc_css_header)) {
  521. DRM_NOTE("CSS header definition mismatch\n");
  522. goto fail;
  523. }
  524. /* then, uCode */
  525. uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
  526. uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
  527. /* now RSA */
  528. if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
  529. DRM_NOTE("RSA key size is bad\n");
  530. goto fail;
  531. }
  532. uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
  533. uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
  534. /* At least, it should have header, uCode and RSA. Size of all three. */
  535. size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
  536. if (fw->size < size) {
  537. DRM_NOTE("Missing firmware components\n");
  538. goto fail;
  539. }
  540. /*
  541. * The GuC firmware image has the version number embedded at a well-known
  542. * offset within the firmware blob; note that major / minor version are
  543. * TWO bytes each (i.e. u16), although all pointers and offsets are defined
  544. * in terms of bytes (u8).
  545. */
  546. switch (uc_fw->fw) {
  547. case INTEL_UC_FW_TYPE_GUC:
  548. /* Header and uCode will be loaded to WOPCM. Size of the two. */
  549. size = uc_fw->header_size + uc_fw->ucode_size;
  550. /* Top 32k of WOPCM is reserved (8K stack + 24k RC6 context). */
  551. if (size > intel_guc_wopcm_size(dev_priv)) {
  552. DRM_ERROR("Firmware is too large to fit in WOPCM\n");
  553. goto fail;
  554. }
  555. uc_fw->major_ver_found = css->guc.sw_version >> 16;
  556. uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
  557. break;
  558. case INTEL_UC_FW_TYPE_HUC:
  559. uc_fw->major_ver_found = css->huc.sw_version >> 16;
  560. uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
  561. break;
  562. default:
  563. DRM_ERROR("Unknown firmware type %d\n", uc_fw->fw);
  564. err = -ENOEXEC;
  565. goto fail;
  566. }
  567. if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
  568. uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
  569. DRM_NOTE("uC firmware version %d.%d, required %d.%d\n",
  570. uc_fw->major_ver_found, uc_fw->minor_ver_found,
  571. uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
  572. err = -ENOEXEC;
  573. goto fail;
  574. }
  575. DRM_DEBUG_DRIVER("firmware version %d.%d OK (minimum %d.%d)\n",
  576. uc_fw->major_ver_found, uc_fw->minor_ver_found,
  577. uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
  578. mutex_lock(&dev_priv->drm.struct_mutex);
  579. obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
  580. mutex_unlock(&dev_priv->drm.struct_mutex);
  581. if (IS_ERR_OR_NULL(obj)) {
  582. err = obj ? PTR_ERR(obj) : -ENOMEM;
  583. goto fail;
  584. }
  585. uc_fw->obj = obj;
  586. uc_fw->size = fw->size;
  587. DRM_DEBUG_DRIVER("uC fw fetch status SUCCESS, obj %p\n",
  588. uc_fw->obj);
  589. release_firmware(fw);
  590. uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
  591. return;
  592. fail:
  593. DRM_WARN("Failed to fetch valid uC firmware from %s (error %d)\n",
  594. uc_fw->path, err);
  595. DRM_DEBUG_DRIVER("uC fw fetch status FAIL; err %d, fw %p, obj %p\n",
  596. err, fw, uc_fw->obj);
  597. mutex_lock(&dev_priv->drm.struct_mutex);
  598. obj = uc_fw->obj;
  599. if (obj)
  600. i915_gem_object_put(obj);
  601. uc_fw->obj = NULL;
  602. mutex_unlock(&dev_priv->drm.struct_mutex);
  603. release_firmware(fw); /* OK even if fw is NULL */
  604. uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
  605. }
  606. /**
  607. * intel_guc_init() - define parameters and fetch firmware
  608. * @dev_priv: i915 device private
  609. *
  610. * Called early during driver load, but after GEM is initialised.
  611. *
  612. * The firmware will be transferred to the GuC's memory later,
  613. * when intel_guc_setup() is called.
  614. */
  615. void intel_guc_init(struct drm_i915_private *dev_priv)
  616. {
  617. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  618. const char *fw_path;
  619. if (!HAS_GUC(dev_priv)) {
  620. i915.enable_guc_loading = 0;
  621. i915.enable_guc_submission = 0;
  622. } else {
  623. /* A negative value means "use platform default" */
  624. if (i915.enable_guc_loading < 0)
  625. i915.enable_guc_loading = HAS_GUC_UCODE(dev_priv);
  626. if (i915.enable_guc_submission < 0)
  627. i915.enable_guc_submission = HAS_GUC_SCHED(dev_priv);
  628. }
  629. if (!HAS_GUC_UCODE(dev_priv)) {
  630. fw_path = NULL;
  631. } else if (IS_SKYLAKE(dev_priv)) {
  632. fw_path = I915_SKL_GUC_UCODE;
  633. guc_fw->major_ver_wanted = SKL_FW_MAJOR;
  634. guc_fw->minor_ver_wanted = SKL_FW_MINOR;
  635. } else if (IS_BROXTON(dev_priv)) {
  636. fw_path = I915_BXT_GUC_UCODE;
  637. guc_fw->major_ver_wanted = BXT_FW_MAJOR;
  638. guc_fw->minor_ver_wanted = BXT_FW_MINOR;
  639. } else if (IS_KABYLAKE(dev_priv)) {
  640. fw_path = I915_KBL_GUC_UCODE;
  641. guc_fw->major_ver_wanted = KBL_FW_MAJOR;
  642. guc_fw->minor_ver_wanted = KBL_FW_MINOR;
  643. } else {
  644. fw_path = ""; /* unknown device */
  645. }
  646. guc_fw->path = fw_path;
  647. guc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
  648. guc_fw->load_status = INTEL_UC_FIRMWARE_NONE;
  649. /* Early (and silent) return if GuC loading is disabled */
  650. if (!i915.enable_guc_loading)
  651. return;
  652. if (fw_path == NULL)
  653. return;
  654. if (*fw_path == '\0')
  655. return;
  656. guc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
  657. DRM_DEBUG_DRIVER("GuC firmware pending, path %s\n", fw_path);
  658. intel_uc_fw_fetch(dev_priv, guc_fw);
  659. /* status must now be FAIL or SUCCESS */
  660. }
  661. /**
  662. * intel_guc_fini() - clean up all allocated resources
  663. * @dev_priv: i915 device private
  664. */
  665. void intel_guc_fini(struct drm_i915_private *dev_priv)
  666. {
  667. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  668. mutex_lock(&dev_priv->drm.struct_mutex);
  669. guc_interrupts_release(dev_priv);
  670. i915_guc_submission_disable(dev_priv);
  671. i915_guc_submission_fini(dev_priv);
  672. if (guc_fw->obj)
  673. i915_gem_object_put(guc_fw->obj);
  674. guc_fw->obj = NULL;
  675. mutex_unlock(&dev_priv->drm.struct_mutex);
  676. guc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
  677. }