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