blob: 7fd75ca031c3d3d92647b79e2e41e7261d65a601 [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
Arkadiusz Hiler413e8fd2016-11-25 18:59:36 +010097void intel_uc_init_early(struct drm_i915_private *dev_priv)
98{
Oscar Mateo5e7cd372017-03-22 10:39:49 -070099 struct intel_guc *guc = &dev_priv->guc;
100
101 mutex_init(&guc->send_mutex);
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000102 guc->send = intel_guc_send_nop;
Arkadiusz Hiler413e8fd2016-11-25 18:59:36 +0100103}
104
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000105static void fetch_uc_fw(struct drm_i915_private *dev_priv,
106 struct intel_uc_fw *uc_fw)
107{
108 struct pci_dev *pdev = dev_priv->drm.pdev;
109 struct drm_i915_gem_object *obj;
110 const struct firmware *fw = NULL;
111 struct uc_css_header *css;
112 size_t size;
113 int err;
114
Michal Wajdeczkob57f7f72017-03-30 11:21:15 +0000115 if (!uc_fw->path)
116 return;
117
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000118 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
119
120 DRM_DEBUG_DRIVER("before requesting firmware: uC fw fetch status %s\n",
121 intel_uc_fw_status_repr(uc_fw->fetch_status));
122
123 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
124 if (err)
125 goto fail;
126 if (!fw)
127 goto fail;
128
129 DRM_DEBUG_DRIVER("fetch uC fw from %s succeeded, fw %p\n",
130 uc_fw->path, fw);
131
132 /* Check the size of the blob before examining buffer contents */
133 if (fw->size < sizeof(struct uc_css_header)) {
134 DRM_NOTE("Firmware header is missing\n");
135 goto fail;
136 }
137
138 css = (struct uc_css_header *)fw->data;
139
140 /* Firmware bits always start from header */
141 uc_fw->header_offset = 0;
142 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
143 css->key_size_dw - css->exponent_size_dw) * sizeof(u32);
144
145 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
146 DRM_NOTE("CSS header definition mismatch\n");
147 goto fail;
148 }
149
150 /* then, uCode */
151 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
152 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
153
154 /* now RSA */
155 if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
156 DRM_NOTE("RSA key size is bad\n");
157 goto fail;
158 }
159 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
160 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
161
162 /* At least, it should have header, uCode and RSA. Size of all three. */
163 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
164 if (fw->size < size) {
165 DRM_NOTE("Missing firmware components\n");
166 goto fail;
167 }
168
169 /*
170 * The GuC firmware image has the version number embedded at a
171 * well-known offset within the firmware blob; note that major / minor
172 * version are TWO bytes each (i.e. u16), although all pointers and
173 * offsets are defined in terms of bytes (u8).
174 */
175 switch (uc_fw->type) {
176 case INTEL_UC_FW_TYPE_GUC:
177 /* Header and uCode will be loaded to WOPCM. Size of the two. */
178 size = uc_fw->header_size + uc_fw->ucode_size;
179
180 /* Top 32k of WOPCM is reserved (8K stack + 24k RC6 context). */
181 if (size > intel_guc_wopcm_size(dev_priv)) {
182 DRM_ERROR("Firmware is too large to fit in WOPCM\n");
183 goto fail;
184 }
185 uc_fw->major_ver_found = css->guc.sw_version >> 16;
186 uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
187 break;
188
189 case INTEL_UC_FW_TYPE_HUC:
190 uc_fw->major_ver_found = css->huc.sw_version >> 16;
191 uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
192 break;
193
194 default:
195 DRM_ERROR("Unknown firmware type %d\n", uc_fw->type);
196 err = -ENOEXEC;
197 goto fail;
198 }
199
200 if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
Michal Wajdeczko5e065f12017-03-30 11:21:12 +0000201 DRM_NOTE("Skipping %s firmware version check\n",
202 intel_uc_fw_type_repr(uc_fw->type));
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000203 } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
204 uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
Michal Wajdeczko5e065f12017-03-30 11:21:12 +0000205 DRM_NOTE("%s firmware version %d.%d, required %d.%d\n",
206 intel_uc_fw_type_repr(uc_fw->type),
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000207 uc_fw->major_ver_found, uc_fw->minor_ver_found,
208 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
209 err = -ENOEXEC;
210 goto fail;
211 }
212
213 DRM_DEBUG_DRIVER("firmware version %d.%d OK (minimum %d.%d)\n",
214 uc_fw->major_ver_found, uc_fw->minor_ver_found,
215 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
216
217 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
218 if (IS_ERR(obj)) {
219 err = PTR_ERR(obj);
220 goto fail;
221 }
222
223 uc_fw->obj = obj;
224 uc_fw->size = fw->size;
225
226 DRM_DEBUG_DRIVER("uC fw fetch status SUCCESS, obj %p\n",
227 uc_fw->obj);
228
229 release_firmware(fw);
230 uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
231 return;
232
233fail:
234 DRM_WARN("Failed to fetch valid uC firmware from %s (error %d)\n",
235 uc_fw->path, err);
236 DRM_DEBUG_DRIVER("uC fw fetch status FAIL; err %d, fw %p, obj %p\n",
237 err, fw, uc_fw->obj);
238
239 release_firmware(fw); /* OK even if fw is NULL */
240 uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
241}
242
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100243void intel_uc_init_fw(struct drm_i915_private *dev_priv)
244{
Michal Wajdeczkob57f7f72017-03-30 11:21:15 +0000245 fetch_uc_fw(dev_priv, &dev_priv->huc.fw);
246 fetch_uc_fw(dev_priv, &dev_priv->guc.fw);
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100247}
248
Oscar Mateo3950bf32017-03-22 10:39:46 -0700249void intel_uc_fini_fw(struct drm_i915_private *dev_priv)
250{
Michal Wajdeczko00bbb722017-03-30 11:21:13 +0000251 __intel_uc_fw_fini(&dev_priv->guc.fw);
252 __intel_uc_fw_fini(&dev_priv->huc.fw);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700253}
254
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000255static int guc_enable_communication(struct intel_guc *guc)
256{
257 /* XXX: placeholder for alternate setup */
258 guc->send = intel_guc_send_mmio;
259 return 0;
260}
261
262static void guc_disable_communication(struct intel_guc *guc)
263{
264 guc->send = intel_guc_send_nop;
265}
266
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100267int intel_uc_init_hw(struct drm_i915_private *dev_priv)
268{
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000269 struct intel_guc *guc = &dev_priv->guc;
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100270 int ret, attempts;
271
Oscar Mateob8991402017-03-28 09:53:47 -0700272 if (!i915.enable_guc_loading)
273 return 0;
274
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000275 guc_disable_communication(guc);
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100276 gen9_reset_guc_interrupts(dev_priv);
277
278 /* We need to notify the guc whenever we change the GGTT */
279 i915_ggtt_enable_guc(dev_priv);
280
Oscar Mateo397fce82017-03-22 10:39:52 -0700281 if (i915.enable_guc_submission) {
282 /*
283 * This is stuff we need to have available at fw load time
284 * if we are planning to enable submission later
285 */
286 ret = i915_guc_submission_init(dev_priv);
287 if (ret)
288 goto err_guc;
289 }
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100290
daniele.ceraolospurio@intel.com13f6c712017-04-06 17:18:52 -0700291 /* init WOPCM */
292 I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
293 I915_WRITE(DMA_GUC_WOPCM_OFFSET,
294 GUC_WOPCM_OFFSET_VALUE | HUC_LOADING_AGENT_GUC);
295
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100296 /* WaEnableuKernelHeaderValidFix:skl */
297 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
298 if (IS_GEN9(dev_priv))
299 attempts = 3;
300 else
301 attempts = 1;
302
303 while (attempts--) {
304 /*
305 * Always reset the GuC just before (re)loading, so
306 * that the state and timing are fairly predictable
307 */
308 ret = __intel_uc_reset_hw(dev_priv);
309 if (ret)
310 goto err_submission;
311
312 intel_huc_init_hw(&dev_priv->huc);
313 ret = intel_guc_init_hw(&dev_priv->guc);
314 if (ret == 0 || ret != -EAGAIN)
315 break;
316
317 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
318 "retry %d more time(s)\n", ret, attempts);
319 }
320
321 /* Did we succeded or run out of retries? */
322 if (ret)
323 goto err_submission;
324
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000325 ret = guc_enable_communication(guc);
326 if (ret)
327 goto err_submission;
328
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100329 intel_guc_auth_huc(dev_priv);
330 if (i915.enable_guc_submission) {
331 if (i915.guc_log_level >= 0)
332 gen9_enable_guc_interrupts(dev_priv);
333
334 ret = i915_guc_submission_enable(dev_priv);
335 if (ret)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700336 goto err_interrupts;
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100337 }
338
339 return 0;
340
341 /*
342 * We've failed to load the firmware :(
343 *
344 * Decide whether to disable GuC submission and fall back to
345 * execlist mode, and whether to hide the error by returning
346 * zero or to return -EIO, which the caller will treat as a
347 * nonfatal error (i.e. it doesn't prevent driver load, but
348 * marks the GPU as wedged until reset).
349 */
Oscar Mateo3950bf32017-03-22 10:39:46 -0700350err_interrupts:
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000351 guc_disable_communication(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700352 gen9_disable_guc_interrupts(dev_priv);
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100353err_submission:
Oscar Mateo397fce82017-03-22 10:39:52 -0700354 if (i915.enable_guc_submission)
355 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700356err_guc:
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100357 i915_ggtt_disable_guc(dev_priv);
358
359 DRM_ERROR("GuC init failed\n");
360 if (i915.enable_guc_loading > 1 || i915.enable_guc_submission > 1)
361 ret = -EIO;
362 else
363 ret = 0;
364
365 if (i915.enable_guc_submission) {
366 i915.enable_guc_submission = 0;
367 DRM_NOTE("Falling back from GuC submission to execlist mode\n");
368 }
369
370 return ret;
371}
372
Oscar Mateo3950bf32017-03-22 10:39:46 -0700373void intel_uc_fini_hw(struct drm_i915_private *dev_priv)
374{
Oscar Mateob8991402017-03-28 09:53:47 -0700375 if (!i915.enable_guc_loading)
376 return;
377
Oscar Mateo3950bf32017-03-22 10:39:46 -0700378 if (i915.enable_guc_submission) {
379 i915_guc_submission_disable(dev_priv);
380 gen9_disable_guc_interrupts(dev_priv);
Oscar Mateo397fce82017-03-22 10:39:52 -0700381 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700382 }
Oscar Mateo3950bf32017-03-22 10:39:46 -0700383 i915_ggtt_disable_guc(dev_priv);
384}
385
Michal Wajdeczko789a6252017-05-02 10:32:42 +0000386int intel_guc_send_nop(struct intel_guc *guc, const u32 *action, u32 len)
387{
388 WARN(1, "Unexpected send: action=%#x\n", *action);
389 return -ENODEV;
390}
391
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100392/*
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700393 * This function implements the MMIO based host to GuC interface.
394 */
395int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len)
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100396{
397 struct drm_i915_private *dev_priv = guc_to_i915(guc);
398 u32 status;
399 int i;
400 int ret;
401
402 if (WARN_ON(len < 1 || len > 15))
403 return -EINVAL;
404
405 mutex_lock(&guc->send_mutex);
Daniele Ceraolo Spurio5d64c122017-03-24 07:48:39 -0700406 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_BLITTER);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100407
408 dev_priv->guc.action_count += 1;
409 dev_priv->guc.action_cmd = action[0];
410
411 for (i = 0; i < len; i++)
412 I915_WRITE(SOFT_SCRATCH(i), action[i]);
413
414 POSTING_READ(SOFT_SCRATCH(i - 1));
415
416 I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
417
418 /*
Michal Wajdeczkobea4e4a2017-04-07 16:01:45 +0000419 * No GuC command should ever take longer than 10ms.
420 * Fast commands should still complete in 10us.
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100421 */
Michal Wajdeczkobea4e4a2017-04-07 16:01:45 +0000422 ret = __intel_wait_for_register_fw(dev_priv,
423 SOFT_SCRATCH(0),
424 INTEL_GUC_RECV_MASK,
425 INTEL_GUC_RECV_MASK,
426 10, 10, &status);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100427 if (status != INTEL_GUC_STATUS_SUCCESS) {
428 /*
429 * Either the GuC explicitly returned an error (which
430 * we convert to -EIO here) or no response at all was
431 * received within the timeout limit (-ETIMEDOUT)
432 */
433 if (ret != -ETIMEDOUT)
434 ret = -EIO;
435
436 DRM_WARN("INTEL_GUC_SEND: Action 0x%X failed;"
437 " ret=%d status=0x%08X response=0x%08X\n",
438 action[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
439
440 dev_priv->guc.action_fail += 1;
441 dev_priv->guc.action_err = ret;
442 }
443 dev_priv->guc.action_status = status;
444
Daniele Ceraolo Spurio5d64c122017-03-24 07:48:39 -0700445 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_BLITTER);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100446 mutex_unlock(&guc->send_mutex);
447
448 return ret;
449}
450
451int intel_guc_sample_forcewake(struct intel_guc *guc)
452{
453 struct drm_i915_private *dev_priv = guc_to_i915(guc);
454 u32 action[2];
455
456 action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
457 /* WaRsDisableCoarsePowerGating:skl,bxt */
458 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
459 action[1] = 0;
460 else
461 /* bit 0 and 1 are for Render and Media domain separately */
462 action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
463
464 return intel_guc_send(guc, action, ARRAY_SIZE(action));
465}