intel_guc_loader.c 19 KB

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