blob: 4a872cdf57e8181f0f1103824dfa5d3ceb117aa9 [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
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +010029/* Reset GuC providing us with fresh state for both GuC and HuC.
30 */
31static int __intel_uc_reset_hw(struct drm_i915_private *dev_priv)
32{
33 int ret;
34 u32 guc_status;
35
36 ret = intel_guc_reset(dev_priv);
37 if (ret) {
38 DRM_ERROR("GuC reset failed, ret = %d\n", ret);
39 return ret;
40 }
41
42 guc_status = I915_READ(GUC_STATUS);
43 WARN(!(guc_status & GS_MIA_IN_RESET),
44 "GuC status: 0x%x, MIA core expected to be in reset\n",
45 guc_status);
46
47 return ret;
48}
49
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010050void intel_uc_sanitize_options(struct drm_i915_private *dev_priv)
51{
52 if (!HAS_GUC(dev_priv)) {
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000053 if (i915.enable_guc_loading > 0 ||
54 i915.enable_guc_submission > 0)
55 DRM_INFO("Ignoring GuC options, no hardware\n");
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010056
57 i915.enable_guc_loading = 0;
58 i915.enable_guc_submission = 0;
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000059 return;
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010060 }
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010061
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000062 /* A negative value means "use platform default" */
63 if (i915.enable_guc_loading < 0)
64 i915.enable_guc_loading = HAS_GUC_UCODE(dev_priv);
65
66 /* Verify firmware version */
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010067 if (i915.enable_guc_loading) {
68 if (HAS_HUC_UCODE(dev_priv))
69 intel_huc_select_fw(&dev_priv->huc);
70
71 if (intel_guc_select_fw(&dev_priv->guc))
72 i915.enable_guc_loading = 0;
73 }
Michal Wajdeczkod4a70a12017-03-15 13:37:41 +000074
75 /* Can't enable guc submission without guc loaded */
76 if (!i915.enable_guc_loading)
77 i915.enable_guc_submission = 0;
78
79 /* A negative value means "use platform default" */
80 if (i915.enable_guc_submission < 0)
81 i915.enable_guc_submission = HAS_GUC_SCHED(dev_priv);
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010082}
83
Arkadiusz Hiler413e8fd2016-11-25 18:59:36 +010084void intel_uc_init_early(struct drm_i915_private *dev_priv)
85{
Oscar Mateo5e7cd372017-03-22 10:39:49 -070086 struct intel_guc *guc = &dev_priv->guc;
87
88 mutex_init(&guc->send_mutex);
89 guc->send = intel_guc_send_mmio;
Arkadiusz Hiler413e8fd2016-11-25 18:59:36 +010090}
91
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +010092void intel_uc_init_fw(struct drm_i915_private *dev_priv)
93{
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010094 if (dev_priv->huc.fw.path)
95 intel_uc_prepare_fw(dev_priv, &dev_priv->huc.fw);
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010096
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010097 if (dev_priv->guc.fw.path)
98 intel_uc_prepare_fw(dev_priv, &dev_priv->guc.fw);
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +010099}
100
Oscar Mateo3950bf32017-03-22 10:39:46 -0700101void intel_uc_fini_fw(struct drm_i915_private *dev_priv)
102{
103 struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
104 struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
105 struct drm_i915_gem_object *obj;
106
107 obj = fetch_and_zero(&guc_fw->obj);
108 if (obj)
109 i915_gem_object_put(obj);
110
111 guc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
112
113 obj = fetch_and_zero(&huc_fw->obj);
114 if (obj)
115 i915_gem_object_put(obj);
116
117 huc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
118}
119
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100120int intel_uc_init_hw(struct drm_i915_private *dev_priv)
121{
122 int ret, attempts;
123
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100124 gen9_reset_guc_interrupts(dev_priv);
125
126 /* We need to notify the guc whenever we change the GGTT */
127 i915_ggtt_enable_guc(dev_priv);
128
Oscar Mateo397fce82017-03-22 10:39:52 -0700129 if (i915.enable_guc_submission) {
130 /*
131 * This is stuff we need to have available at fw load time
132 * if we are planning to enable submission later
133 */
134 ret = i915_guc_submission_init(dev_priv);
135 if (ret)
136 goto err_guc;
137 }
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100138
139 /* WaEnableuKernelHeaderValidFix:skl */
140 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
141 if (IS_GEN9(dev_priv))
142 attempts = 3;
143 else
144 attempts = 1;
145
146 while (attempts--) {
147 /*
148 * Always reset the GuC just before (re)loading, so
149 * that the state and timing are fairly predictable
150 */
151 ret = __intel_uc_reset_hw(dev_priv);
152 if (ret)
153 goto err_submission;
154
155 intel_huc_init_hw(&dev_priv->huc);
156 ret = intel_guc_init_hw(&dev_priv->guc);
157 if (ret == 0 || ret != -EAGAIN)
158 break;
159
160 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
161 "retry %d more time(s)\n", ret, attempts);
162 }
163
164 /* Did we succeded or run out of retries? */
165 if (ret)
166 goto err_submission;
167
168 intel_guc_auth_huc(dev_priv);
169 if (i915.enable_guc_submission) {
170 if (i915.guc_log_level >= 0)
171 gen9_enable_guc_interrupts(dev_priv);
172
173 ret = i915_guc_submission_enable(dev_priv);
174 if (ret)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700175 goto err_interrupts;
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100176 }
177
178 return 0;
179
180 /*
181 * We've failed to load the firmware :(
182 *
183 * Decide whether to disable GuC submission and fall back to
184 * execlist mode, and whether to hide the error by returning
185 * zero or to return -EIO, which the caller will treat as a
186 * nonfatal error (i.e. it doesn't prevent driver load, but
187 * marks the GPU as wedged until reset).
188 */
Oscar Mateo3950bf32017-03-22 10:39:46 -0700189err_interrupts:
190 gen9_disable_guc_interrupts(dev_priv);
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100191err_submission:
Oscar Mateo397fce82017-03-22 10:39:52 -0700192 if (i915.enable_guc_submission)
193 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700194err_guc:
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100195 i915_ggtt_disable_guc(dev_priv);
196
197 DRM_ERROR("GuC init failed\n");
198 if (i915.enable_guc_loading > 1 || i915.enable_guc_submission > 1)
199 ret = -EIO;
200 else
201 ret = 0;
202
203 if (i915.enable_guc_submission) {
204 i915.enable_guc_submission = 0;
205 DRM_NOTE("Falling back from GuC submission to execlist mode\n");
206 }
207
208 return ret;
209}
210
Oscar Mateo3950bf32017-03-22 10:39:46 -0700211void intel_uc_fini_hw(struct drm_i915_private *dev_priv)
212{
213 if (i915.enable_guc_submission) {
214 i915_guc_submission_disable(dev_priv);
215 gen9_disable_guc_interrupts(dev_priv);
Oscar Mateo397fce82017-03-22 10:39:52 -0700216 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700217 }
Oscar Mateo3950bf32017-03-22 10:39:46 -0700218 i915_ggtt_disable_guc(dev_priv);
219}
220
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100221/*
222 * Read GuC command/status register (SOFT_SCRATCH_0)
223 * Return true if it contains a response rather than a command
224 */
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700225static bool guc_recv(struct intel_guc *guc, u32 *status)
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100226{
Michal Wajdeczkobae3fdc2016-12-20 11:55:31 +0000227 struct drm_i915_private *dev_priv = guc_to_i915(guc);
228
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100229 u32 val = I915_READ(SOFT_SCRATCH(0));
230 *status = val;
231 return INTEL_GUC_RECV_IS_RESPONSE(val);
232}
233
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700234/*
235 * This function implements the MMIO based host to GuC interface.
236 */
237int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len)
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100238{
239 struct drm_i915_private *dev_priv = guc_to_i915(guc);
240 u32 status;
241 int i;
242 int ret;
243
244 if (WARN_ON(len < 1 || len > 15))
245 return -EINVAL;
246
247 mutex_lock(&guc->send_mutex);
248 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
249
250 dev_priv->guc.action_count += 1;
251 dev_priv->guc.action_cmd = action[0];
252
253 for (i = 0; i < len; i++)
254 I915_WRITE(SOFT_SCRATCH(i), action[i]);
255
256 POSTING_READ(SOFT_SCRATCH(i - 1));
257
258 I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
259
260 /*
261 * Fast commands should complete in less than 10us, so sample quickly
262 * up to that length of time, then switch to a slower sleep-wait loop.
263 * No inte_guc_send command should ever take longer than 10ms.
264 */
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700265 ret = wait_for_us(guc_recv(guc, &status), 10);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100266 if (ret)
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700267 ret = wait_for(guc_recv(guc, &status), 10);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100268 if (status != INTEL_GUC_STATUS_SUCCESS) {
269 /*
270 * Either the GuC explicitly returned an error (which
271 * we convert to -EIO here) or no response at all was
272 * received within the timeout limit (-ETIMEDOUT)
273 */
274 if (ret != -ETIMEDOUT)
275 ret = -EIO;
276
277 DRM_WARN("INTEL_GUC_SEND: Action 0x%X failed;"
278 " ret=%d status=0x%08X response=0x%08X\n",
279 action[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
280
281 dev_priv->guc.action_fail += 1;
282 dev_priv->guc.action_err = ret;
283 }
284 dev_priv->guc.action_status = status;
285
286 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
287 mutex_unlock(&guc->send_mutex);
288
289 return ret;
290}
291
292int intel_guc_sample_forcewake(struct intel_guc *guc)
293{
294 struct drm_i915_private *dev_priv = guc_to_i915(guc);
295 u32 action[2];
296
297 action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
298 /* WaRsDisableCoarsePowerGating:skl,bxt */
299 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
300 action[1] = 0;
301 else
302 /* bit 0 and 1 are for Render and Media domain separately */
303 action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
304
305 return intel_guc_send(guc, action, ARRAY_SIZE(action));
306}
307
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100308void intel_uc_prepare_fw(struct drm_i915_private *dev_priv,
309 struct intel_uc_fw *uc_fw)
310{
311 struct pci_dev *pdev = dev_priv->drm.pdev;
312 struct drm_i915_gem_object *obj;
313 const struct firmware *fw = NULL;
314 struct uc_css_header *css;
315 size_t size;
316 int err;
317
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100318 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
319
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100320 DRM_DEBUG_DRIVER("before requesting firmware: uC fw fetch status %s\n",
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100321 intel_uc_fw_status_repr(uc_fw->fetch_status));
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100322
323 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
324 if (err)
325 goto fail;
326 if (!fw)
327 goto fail;
328
329 DRM_DEBUG_DRIVER("fetch uC fw from %s succeeded, fw %p\n",
330 uc_fw->path, fw);
331
332 /* Check the size of the blob before examining buffer contents */
333 if (fw->size < sizeof(struct uc_css_header)) {
334 DRM_NOTE("Firmware header is missing\n");
335 goto fail;
336 }
337
338 css = (struct uc_css_header *)fw->data;
339
340 /* Firmware bits always start from header */
341 uc_fw->header_offset = 0;
342 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
343 css->key_size_dw - css->exponent_size_dw) * sizeof(u32);
344
345 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
346 DRM_NOTE("CSS header definition mismatch\n");
347 goto fail;
348 }
349
350 /* then, uCode */
351 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
352 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
353
354 /* now RSA */
355 if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
356 DRM_NOTE("RSA key size is bad\n");
357 goto fail;
358 }
359 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
360 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
361
362 /* At least, it should have header, uCode and RSA. Size of all three. */
363 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
364 if (fw->size < size) {
365 DRM_NOTE("Missing firmware components\n");
366 goto fail;
367 }
368
369 /*
370 * The GuC firmware image has the version number embedded at a
371 * well-known offset within the firmware blob; note that major / minor
372 * version are TWO bytes each (i.e. u16), although all pointers and
373 * offsets are defined in terms of bytes (u8).
374 */
Arkadiusz Hiler6833b822017-03-15 14:34:15 +0100375 switch (uc_fw->type) {
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100376 case INTEL_UC_FW_TYPE_GUC:
377 /* Header and uCode will be loaded to WOPCM. Size of the two. */
378 size = uc_fw->header_size + uc_fw->ucode_size;
379
380 /* Top 32k of WOPCM is reserved (8K stack + 24k RC6 context). */
381 if (size > intel_guc_wopcm_size(dev_priv)) {
382 DRM_ERROR("Firmware is too large to fit in WOPCM\n");
383 goto fail;
384 }
385 uc_fw->major_ver_found = css->guc.sw_version >> 16;
386 uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
387 break;
388
389 case INTEL_UC_FW_TYPE_HUC:
390 uc_fw->major_ver_found = css->huc.sw_version >> 16;
391 uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
392 break;
393
394 default:
Arkadiusz Hiler6833b822017-03-15 14:34:15 +0100395 DRM_ERROR("Unknown firmware type %d\n", uc_fw->type);
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100396 err = -ENOEXEC;
397 goto fail;
398 }
399
Arkadiusz Hilerb3420dd2017-03-14 15:28:14 +0100400 if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
401 DRM_NOTE("Skipping uC firmware version check\n");
402 } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
403 uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100404 DRM_NOTE("uC firmware version %d.%d, required %d.%d\n",
405 uc_fw->major_ver_found, uc_fw->minor_ver_found,
406 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
407 err = -ENOEXEC;
408 goto fail;
409 }
410
411 DRM_DEBUG_DRIVER("firmware version %d.%d OK (minimum %d.%d)\n",
412 uc_fw->major_ver_found, uc_fw->minor_ver_found,
413 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
414
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100415 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
Chris Wilsonf3ddd2c2017-03-17 20:53:17 +0000416 if (IS_ERR(obj)) {
417 err = PTR_ERR(obj);
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100418 goto fail;
419 }
420
421 uc_fw->obj = obj;
422 uc_fw->size = fw->size;
423
424 DRM_DEBUG_DRIVER("uC fw fetch status SUCCESS, obj %p\n",
425 uc_fw->obj);
426
427 release_firmware(fw);
428 uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
429 return;
430
431fail:
432 DRM_WARN("Failed to fetch valid uC firmware from %s (error %d)\n",
433 uc_fw->path, err);
434 DRM_DEBUG_DRIVER("uC fw fetch status FAIL; err %d, fw %p, obj %p\n",
435 err, fw, uc_fw->obj);
436
437 release_firmware(fw); /* OK even if fw is NULL */
438 uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
439}