intel_guc_loader.c 22 KB

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