intel_guc_loader.c 24 KB

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