blob: 72f49e679707cad9c1b836f1d027b5fcaa080860 [file] [log] [blame]
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +01001/*
2 * Copyright © 2016 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 */
24
25#include "i915_drv.h"
26#include "intel_uc.h"
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +010027#include <linux/firmware.h>
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +010028
Michal Wajdeczko00bbb722017-03-30 11:21:13 +000029/* Cleans up uC firmware by releasing the firmware GEM obj.
30 */
31static void __intel_uc_fw_fini(struct intel_uc_fw *uc_fw)
32{
33 struct drm_i915_gem_object *obj;
34
35 obj = fetch_and_zero(&uc_fw->obj);
36 if (obj)
37 i915_gem_object_put(obj);
38
39 uc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
40}
41
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +010042/* Reset GuC providing us with fresh state for both GuC and HuC.
43 */
44static int __intel_uc_reset_hw(struct drm_i915_private *dev_priv)
45{
46 int ret;
47 u32 guc_status;
48
49 ret = intel_guc_reset(dev_priv);
50 if (ret) {
51 DRM_ERROR("GuC reset failed, ret = %d\n", ret);
52 return ret;
53 }
54
55 guc_status = I915_READ(GUC_STATUS);
56 WARN(!(guc_status & GS_MIA_IN_RESET),
57 "GuC status: 0x%x, MIA core expected to be in reset\n",
58 guc_status);
59
60 return ret;
61}
62
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010063void intel_uc_sanitize_options(struct drm_i915_private *dev_priv)
64{
65 if (!HAS_GUC(dev_priv)) {
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000066 if (i915.enable_guc_loading > 0 ||
67 i915.enable_guc_submission > 0)
68 DRM_INFO("Ignoring GuC options, no hardware\n");
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010069
70 i915.enable_guc_loading = 0;
71 i915.enable_guc_submission = 0;
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000072 return;
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010073 }
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010074
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000075 /* A negative value means "use platform default" */
76 if (i915.enable_guc_loading < 0)
77 i915.enable_guc_loading = HAS_GUC_UCODE(dev_priv);
78
79 /* Verify firmware version */
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010080 if (i915.enable_guc_loading) {
81 if (HAS_HUC_UCODE(dev_priv))
82 intel_huc_select_fw(&dev_priv->huc);
83
84 if (intel_guc_select_fw(&dev_priv->guc))
85 i915.enable_guc_loading = 0;
86 }
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000087
88 /* Can't enable guc submission without guc loaded */
89 if (!i915.enable_guc_loading)
90 i915.enable_guc_submission = 0;
91
92 /* A negative value means "use platform default" */
93 if (i915.enable_guc_submission < 0)
94 i915.enable_guc_submission = HAS_GUC_SCHED(dev_priv);
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010095}
96
Michal Wajdeczkoa03aac42017-05-10 12:59:26 +000097static void guc_write_irq_trigger(struct intel_guc *guc)
98{
99 struct drm_i915_private *dev_priv = guc_to_i915(guc);
100
101 I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
102}
103
Arkadiusz Hiler413e8fd2016-11-25 18:59:36 +0100104void intel_uc_init_early(struct drm_i915_private *dev_priv)
105{
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700106 struct intel_guc *guc = &dev_priv->guc;
107
108 mutex_init(&guc->send_mutex);
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000109 guc->send = intel_guc_send_nop;
Michal Wajdeczkoa03aac42017-05-10 12:59:26 +0000110 guc->notify = guc_write_irq_trigger;
Arkadiusz Hiler413e8fd2016-11-25 18:59:36 +0100111}
112
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000113static void fetch_uc_fw(struct drm_i915_private *dev_priv,
114 struct intel_uc_fw *uc_fw)
115{
116 struct pci_dev *pdev = dev_priv->drm.pdev;
117 struct drm_i915_gem_object *obj;
118 const struct firmware *fw = NULL;
119 struct uc_css_header *css;
120 size_t size;
121 int err;
122
Michal Wajdeczkob57f7f72017-03-30 11:21:15 +0000123 if (!uc_fw->path)
124 return;
125
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000126 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
127
128 DRM_DEBUG_DRIVER("before requesting firmware: uC fw fetch status %s\n",
129 intel_uc_fw_status_repr(uc_fw->fetch_status));
130
131 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
132 if (err)
133 goto fail;
134 if (!fw)
135 goto fail;
136
137 DRM_DEBUG_DRIVER("fetch uC fw from %s succeeded, fw %p\n",
138 uc_fw->path, fw);
139
140 /* Check the size of the blob before examining buffer contents */
141 if (fw->size < sizeof(struct uc_css_header)) {
142 DRM_NOTE("Firmware header is missing\n");
143 goto fail;
144 }
145
146 css = (struct uc_css_header *)fw->data;
147
148 /* Firmware bits always start from header */
149 uc_fw->header_offset = 0;
150 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
151 css->key_size_dw - css->exponent_size_dw) * sizeof(u32);
152
153 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
154 DRM_NOTE("CSS header definition mismatch\n");
155 goto fail;
156 }
157
158 /* then, uCode */
159 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
160 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
161
162 /* now RSA */
163 if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
164 DRM_NOTE("RSA key size is bad\n");
165 goto fail;
166 }
167 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
168 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
169
170 /* At least, it should have header, uCode and RSA. Size of all three. */
171 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
172 if (fw->size < size) {
173 DRM_NOTE("Missing firmware components\n");
174 goto fail;
175 }
176
177 /*
178 * The GuC firmware image has the version number embedded at a
179 * well-known offset within the firmware blob; note that major / minor
180 * version are TWO bytes each (i.e. u16), although all pointers and
181 * offsets are defined in terms of bytes (u8).
182 */
183 switch (uc_fw->type) {
184 case INTEL_UC_FW_TYPE_GUC:
185 /* Header and uCode will be loaded to WOPCM. Size of the two. */
186 size = uc_fw->header_size + uc_fw->ucode_size;
187
188 /* Top 32k of WOPCM is reserved (8K stack + 24k RC6 context). */
189 if (size > intel_guc_wopcm_size(dev_priv)) {
190 DRM_ERROR("Firmware is too large to fit in WOPCM\n");
191 goto fail;
192 }
193 uc_fw->major_ver_found = css->guc.sw_version >> 16;
194 uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
195 break;
196
197 case INTEL_UC_FW_TYPE_HUC:
198 uc_fw->major_ver_found = css->huc.sw_version >> 16;
199 uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
200 break;
201
202 default:
203 DRM_ERROR("Unknown firmware type %d\n", uc_fw->type);
204 err = -ENOEXEC;
205 goto fail;
206 }
207
208 if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
Michal Wajdeczko5e065f12017-03-30 11:21:12 +0000209 DRM_NOTE("Skipping %s firmware version check\n",
210 intel_uc_fw_type_repr(uc_fw->type));
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000211 } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
212 uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
Michal Wajdeczko5e065f12017-03-30 11:21:12 +0000213 DRM_NOTE("%s firmware version %d.%d, required %d.%d\n",
214 intel_uc_fw_type_repr(uc_fw->type),
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000215 uc_fw->major_ver_found, uc_fw->minor_ver_found,
216 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
217 err = -ENOEXEC;
218 goto fail;
219 }
220
221 DRM_DEBUG_DRIVER("firmware version %d.%d OK (minimum %d.%d)\n",
222 uc_fw->major_ver_found, uc_fw->minor_ver_found,
223 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
224
225 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
226 if (IS_ERR(obj)) {
227 err = PTR_ERR(obj);
228 goto fail;
229 }
230
231 uc_fw->obj = obj;
232 uc_fw->size = fw->size;
233
234 DRM_DEBUG_DRIVER("uC fw fetch status SUCCESS, obj %p\n",
235 uc_fw->obj);
236
237 release_firmware(fw);
238 uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
239 return;
240
241fail:
242 DRM_WARN("Failed to fetch valid uC firmware from %s (error %d)\n",
243 uc_fw->path, err);
244 DRM_DEBUG_DRIVER("uC fw fetch status FAIL; err %d, fw %p, obj %p\n",
245 err, fw, uc_fw->obj);
246
247 release_firmware(fw); /* OK even if fw is NULL */
248 uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
249}
250
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100251void intel_uc_init_fw(struct drm_i915_private *dev_priv)
252{
Michal Wajdeczkob57f7f72017-03-30 11:21:15 +0000253 fetch_uc_fw(dev_priv, &dev_priv->huc.fw);
254 fetch_uc_fw(dev_priv, &dev_priv->guc.fw);
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100255}
256
Oscar Mateo3950bf32017-03-22 10:39:46 -0700257void intel_uc_fini_fw(struct drm_i915_private *dev_priv)
258{
Michal Wajdeczko00bbb722017-03-30 11:21:13 +0000259 __intel_uc_fw_fini(&dev_priv->guc.fw);
260 __intel_uc_fw_fini(&dev_priv->huc.fw);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700261}
262
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000263static int guc_enable_communication(struct intel_guc *guc)
264{
265 /* XXX: placeholder for alternate setup */
266 guc->send = intel_guc_send_mmio;
267 return 0;
268}
269
270static void guc_disable_communication(struct intel_guc *guc)
271{
272 guc->send = intel_guc_send_nop;
273}
274
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100275int intel_uc_init_hw(struct drm_i915_private *dev_priv)
276{
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000277 struct intel_guc *guc = &dev_priv->guc;
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100278 int ret, attempts;
279
Oscar Mateob8991402017-03-28 09:53:47 -0700280 if (!i915.enable_guc_loading)
281 return 0;
282
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000283 guc_disable_communication(guc);
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100284 gen9_reset_guc_interrupts(dev_priv);
285
286 /* We need to notify the guc whenever we change the GGTT */
287 i915_ggtt_enable_guc(dev_priv);
288
Oscar Mateo397fce82017-03-22 10:39:52 -0700289 if (i915.enable_guc_submission) {
290 /*
291 * This is stuff we need to have available at fw load time
292 * if we are planning to enable submission later
293 */
294 ret = i915_guc_submission_init(dev_priv);
295 if (ret)
296 goto err_guc;
297 }
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100298
daniele.ceraolospurio@intel.com13f6c712017-04-06 17:18:52 -0700299 /* init WOPCM */
300 I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
301 I915_WRITE(DMA_GUC_WOPCM_OFFSET,
302 GUC_WOPCM_OFFSET_VALUE | HUC_LOADING_AGENT_GUC);
303
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100304 /* WaEnableuKernelHeaderValidFix:skl */
305 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
306 if (IS_GEN9(dev_priv))
307 attempts = 3;
308 else
309 attempts = 1;
310
311 while (attempts--) {
312 /*
313 * Always reset the GuC just before (re)loading, so
314 * that the state and timing are fairly predictable
315 */
316 ret = __intel_uc_reset_hw(dev_priv);
317 if (ret)
318 goto err_submission;
319
320 intel_huc_init_hw(&dev_priv->huc);
321 ret = intel_guc_init_hw(&dev_priv->guc);
322 if (ret == 0 || ret != -EAGAIN)
323 break;
324
325 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
326 "retry %d more time(s)\n", ret, attempts);
327 }
328
329 /* Did we succeded or run out of retries? */
330 if (ret)
331 goto err_submission;
332
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000333 ret = guc_enable_communication(guc);
334 if (ret)
335 goto err_submission;
336
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100337 intel_guc_auth_huc(dev_priv);
338 if (i915.enable_guc_submission) {
339 if (i915.guc_log_level >= 0)
340 gen9_enable_guc_interrupts(dev_priv);
341
342 ret = i915_guc_submission_enable(dev_priv);
343 if (ret)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700344 goto err_interrupts;
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100345 }
346
347 return 0;
348
349 /*
350 * We've failed to load the firmware :(
351 *
352 * Decide whether to disable GuC submission and fall back to
353 * execlist mode, and whether to hide the error by returning
354 * zero or to return -EIO, which the caller will treat as a
355 * nonfatal error (i.e. it doesn't prevent driver load, but
356 * marks the GPU as wedged until reset).
357 */
Oscar Mateo3950bf32017-03-22 10:39:46 -0700358err_interrupts:
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000359 guc_disable_communication(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700360 gen9_disable_guc_interrupts(dev_priv);
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100361err_submission:
Oscar Mateo397fce82017-03-22 10:39:52 -0700362 if (i915.enable_guc_submission)
363 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700364err_guc:
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100365 i915_ggtt_disable_guc(dev_priv);
366
367 DRM_ERROR("GuC init failed\n");
368 if (i915.enable_guc_loading > 1 || i915.enable_guc_submission > 1)
369 ret = -EIO;
370 else
371 ret = 0;
372
373 if (i915.enable_guc_submission) {
374 i915.enable_guc_submission = 0;
375 DRM_NOTE("Falling back from GuC submission to execlist mode\n");
376 }
377
378 return ret;
379}
380
Oscar Mateo3950bf32017-03-22 10:39:46 -0700381void intel_uc_fini_hw(struct drm_i915_private *dev_priv)
382{
Oscar Mateob8991402017-03-28 09:53:47 -0700383 if (!i915.enable_guc_loading)
384 return;
385
Oscar Mateo3950bf32017-03-22 10:39:46 -0700386 if (i915.enable_guc_submission) {
387 i915_guc_submission_disable(dev_priv);
388 gen9_disable_guc_interrupts(dev_priv);
Oscar Mateo397fce82017-03-22 10:39:52 -0700389 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700390 }
Oscar Mateo3950bf32017-03-22 10:39:46 -0700391 i915_ggtt_disable_guc(dev_priv);
392}
393
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000394int intel_guc_send_nop(struct intel_guc *guc, const u32 *action, u32 len)
395{
396 WARN(1, "Unexpected send: action=%#x\n", *action);
397 return -ENODEV;
398}
399
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100400/*
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700401 * This function implements the MMIO based host to GuC interface.
402 */
403int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len)
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100404{
405 struct drm_i915_private *dev_priv = guc_to_i915(guc);
406 u32 status;
407 int i;
408 int ret;
409
410 if (WARN_ON(len < 1 || len > 15))
411 return -EINVAL;
412
413 mutex_lock(&guc->send_mutex);
Daniele Ceraolo Spurio5d64c122017-03-24 07:48:39 -0700414 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_BLITTER);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100415
416 dev_priv->guc.action_count += 1;
417 dev_priv->guc.action_cmd = action[0];
418
419 for (i = 0; i < len; i++)
420 I915_WRITE(SOFT_SCRATCH(i), action[i]);
421
422 POSTING_READ(SOFT_SCRATCH(i - 1));
423
Michal Wajdeczkoa03aac42017-05-10 12:59:26 +0000424 intel_guc_notify(guc);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100425
426 /*
Michal Wajdeczkobea4e4a2017-04-07 16:01:45 +0000427 * No GuC command should ever take longer than 10ms.
428 * Fast commands should still complete in 10us.
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100429 */
Michal Wajdeczkobea4e4a2017-04-07 16:01:45 +0000430 ret = __intel_wait_for_register_fw(dev_priv,
431 SOFT_SCRATCH(0),
432 INTEL_GUC_RECV_MASK,
433 INTEL_GUC_RECV_MASK,
434 10, 10, &status);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100435 if (status != INTEL_GUC_STATUS_SUCCESS) {
436 /*
437 * Either the GuC explicitly returned an error (which
438 * we convert to -EIO here) or no response at all was
439 * received within the timeout limit (-ETIMEDOUT)
440 */
441 if (ret != -ETIMEDOUT)
442 ret = -EIO;
443
444 DRM_WARN("INTEL_GUC_SEND: Action 0x%X failed;"
445 " ret=%d status=0x%08X response=0x%08X\n",
446 action[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
447
448 dev_priv->guc.action_fail += 1;
449 dev_priv->guc.action_err = ret;
450 }
451 dev_priv->guc.action_status = status;
452
Daniele Ceraolo Spurio5d64c122017-03-24 07:48:39 -0700453 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_BLITTER);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100454 mutex_unlock(&guc->send_mutex);
455
456 return ret;
457}
458
459int intel_guc_sample_forcewake(struct intel_guc *guc)
460{
461 struct drm_i915_private *dev_priv = guc_to_i915(guc);
462 u32 action[2];
463
464 action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
465 /* WaRsDisableCoarsePowerGating:skl,bxt */
466 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
467 action[1] = 0;
468 else
469 /* bit 0 and 1 are for Render and Media domain separately */
470 action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
471
472 return intel_guc_send(guc, action, ARRAY_SIZE(action));
473}