blob: 86530c92337a0f7b5b55b3a455e38865704706df [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{
86 mutex_init(&dev_priv->guc.send_mutex);
87}
88
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +010089void intel_uc_init_fw(struct drm_i915_private *dev_priv)
90{
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010091 if (dev_priv->huc.fw.path)
92 intel_uc_prepare_fw(dev_priv, &dev_priv->huc.fw);
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +010093
Arkadiusz Hilerb551f612017-03-14 15:28:13 +010094 if (dev_priv->guc.fw.path)
95 intel_uc_prepare_fw(dev_priv, &dev_priv->guc.fw);
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +010096}
97
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +010098int intel_uc_init_hw(struct drm_i915_private *dev_priv)
99{
100 int ret, attempts;
101
102 /* GuC not enabled, nothing to do */
103 if (!i915.enable_guc_loading)
104 return 0;
105
106 gen9_reset_guc_interrupts(dev_priv);
107
108 /* We need to notify the guc whenever we change the GGTT */
109 i915_ggtt_enable_guc(dev_priv);
110
111 if (i915.enable_guc_submission) {
112 ret = i915_guc_submission_init(dev_priv);
113 if (ret)
114 goto err;
115 }
116
117 /* WaEnableuKernelHeaderValidFix:skl */
118 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
119 if (IS_GEN9(dev_priv))
120 attempts = 3;
121 else
122 attempts = 1;
123
124 while (attempts--) {
125 /*
126 * Always reset the GuC just before (re)loading, so
127 * that the state and timing are fairly predictable
128 */
129 ret = __intel_uc_reset_hw(dev_priv);
130 if (ret)
131 goto err_submission;
132
133 intel_huc_init_hw(&dev_priv->huc);
134 ret = intel_guc_init_hw(&dev_priv->guc);
135 if (ret == 0 || ret != -EAGAIN)
136 break;
137
138 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
139 "retry %d more time(s)\n", ret, attempts);
140 }
141
142 /* Did we succeded or run out of retries? */
143 if (ret)
144 goto err_submission;
145
146 intel_guc_auth_huc(dev_priv);
147 if (i915.enable_guc_submission) {
148 if (i915.guc_log_level >= 0)
149 gen9_enable_guc_interrupts(dev_priv);
150
151 ret = i915_guc_submission_enable(dev_priv);
152 if (ret)
153 goto err_submission;
154 }
155
156 return 0;
157
158 /*
159 * We've failed to load the firmware :(
160 *
161 * Decide whether to disable GuC submission and fall back to
162 * execlist mode, and whether to hide the error by returning
163 * zero or to return -EIO, which the caller will treat as a
164 * nonfatal error (i.e. it doesn't prevent driver load, but
165 * marks the GPU as wedged until reset).
166 */
167err_submission:
168 if (i915.enable_guc_submission)
169 i915_guc_submission_fini(dev_priv);
170
171err:
172 i915_ggtt_disable_guc(dev_priv);
173
174 DRM_ERROR("GuC init failed\n");
175 if (i915.enable_guc_loading > 1 || i915.enable_guc_submission > 1)
176 ret = -EIO;
177 else
178 ret = 0;
179
180 if (i915.enable_guc_submission) {
181 i915.enable_guc_submission = 0;
182 DRM_NOTE("Falling back from GuC submission to execlist mode\n");
183 }
184
185 return ret;
186}
187
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100188/*
189 * Read GuC command/status register (SOFT_SCRATCH_0)
190 * Return true if it contains a response rather than a command
191 */
Michal Wajdeczkobae3fdc2016-12-20 11:55:31 +0000192static bool intel_guc_recv(struct intel_guc *guc, u32 *status)
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100193{
Michal Wajdeczkobae3fdc2016-12-20 11:55:31 +0000194 struct drm_i915_private *dev_priv = guc_to_i915(guc);
195
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100196 u32 val = I915_READ(SOFT_SCRATCH(0));
197 *status = val;
198 return INTEL_GUC_RECV_IS_RESPONSE(val);
199}
200
201int intel_guc_send(struct intel_guc *guc, const u32 *action, u32 len)
202{
203 struct drm_i915_private *dev_priv = guc_to_i915(guc);
204 u32 status;
205 int i;
206 int ret;
207
208 if (WARN_ON(len < 1 || len > 15))
209 return -EINVAL;
210
211 mutex_lock(&guc->send_mutex);
212 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
213
214 dev_priv->guc.action_count += 1;
215 dev_priv->guc.action_cmd = action[0];
216
217 for (i = 0; i < len; i++)
218 I915_WRITE(SOFT_SCRATCH(i), action[i]);
219
220 POSTING_READ(SOFT_SCRATCH(i - 1));
221
222 I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
223
224 /*
225 * Fast commands should complete in less than 10us, so sample quickly
226 * up to that length of time, then switch to a slower sleep-wait loop.
227 * No inte_guc_send command should ever take longer than 10ms.
228 */
Michal Wajdeczkobae3fdc2016-12-20 11:55:31 +0000229 ret = wait_for_us(intel_guc_recv(guc, &status), 10);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100230 if (ret)
Michal Wajdeczkobae3fdc2016-12-20 11:55:31 +0000231 ret = wait_for(intel_guc_recv(guc, &status), 10);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100232 if (status != INTEL_GUC_STATUS_SUCCESS) {
233 /*
234 * Either the GuC explicitly returned an error (which
235 * we convert to -EIO here) or no response at all was
236 * received within the timeout limit (-ETIMEDOUT)
237 */
238 if (ret != -ETIMEDOUT)
239 ret = -EIO;
240
241 DRM_WARN("INTEL_GUC_SEND: Action 0x%X failed;"
242 " ret=%d status=0x%08X response=0x%08X\n",
243 action[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
244
245 dev_priv->guc.action_fail += 1;
246 dev_priv->guc.action_err = ret;
247 }
248 dev_priv->guc.action_status = status;
249
250 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
251 mutex_unlock(&guc->send_mutex);
252
253 return ret;
254}
255
256int intel_guc_sample_forcewake(struct intel_guc *guc)
257{
258 struct drm_i915_private *dev_priv = guc_to_i915(guc);
259 u32 action[2];
260
261 action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
262 /* WaRsDisableCoarsePowerGating:skl,bxt */
263 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
264 action[1] = 0;
265 else
266 /* bit 0 and 1 are for Render and Media domain separately */
267 action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
268
269 return intel_guc_send(guc, action, ARRAY_SIZE(action));
270}
271
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100272void intel_uc_prepare_fw(struct drm_i915_private *dev_priv,
273 struct intel_uc_fw *uc_fw)
274{
275 struct pci_dev *pdev = dev_priv->drm.pdev;
276 struct drm_i915_gem_object *obj;
277 const struct firmware *fw = NULL;
278 struct uc_css_header *css;
279 size_t size;
280 int err;
281
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100282 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
283
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100284 DRM_DEBUG_DRIVER("before requesting firmware: uC fw fetch status %s\n",
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100285 intel_uc_fw_status_repr(uc_fw->fetch_status));
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100286
287 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
288 if (err)
289 goto fail;
290 if (!fw)
291 goto fail;
292
293 DRM_DEBUG_DRIVER("fetch uC fw from %s succeeded, fw %p\n",
294 uc_fw->path, fw);
295
296 /* Check the size of the blob before examining buffer contents */
297 if (fw->size < sizeof(struct uc_css_header)) {
298 DRM_NOTE("Firmware header is missing\n");
299 goto fail;
300 }
301
302 css = (struct uc_css_header *)fw->data;
303
304 /* Firmware bits always start from header */
305 uc_fw->header_offset = 0;
306 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
307 css->key_size_dw - css->exponent_size_dw) * sizeof(u32);
308
309 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
310 DRM_NOTE("CSS header definition mismatch\n");
311 goto fail;
312 }
313
314 /* then, uCode */
315 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
316 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
317
318 /* now RSA */
319 if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
320 DRM_NOTE("RSA key size is bad\n");
321 goto fail;
322 }
323 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
324 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
325
326 /* At least, it should have header, uCode and RSA. Size of all three. */
327 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
328 if (fw->size < size) {
329 DRM_NOTE("Missing firmware components\n");
330 goto fail;
331 }
332
333 /*
334 * The GuC firmware image has the version number embedded at a
335 * well-known offset within the firmware blob; note that major / minor
336 * version are TWO bytes each (i.e. u16), although all pointers and
337 * offsets are defined in terms of bytes (u8).
338 */
Arkadiusz Hiler6833b822017-03-15 14:34:15 +0100339 switch (uc_fw->type) {
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100340 case INTEL_UC_FW_TYPE_GUC:
341 /* Header and uCode will be loaded to WOPCM. Size of the two. */
342 size = uc_fw->header_size + uc_fw->ucode_size;
343
344 /* Top 32k of WOPCM is reserved (8K stack + 24k RC6 context). */
345 if (size > intel_guc_wopcm_size(dev_priv)) {
346 DRM_ERROR("Firmware is too large to fit in WOPCM\n");
347 goto fail;
348 }
349 uc_fw->major_ver_found = css->guc.sw_version >> 16;
350 uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
351 break;
352
353 case INTEL_UC_FW_TYPE_HUC:
354 uc_fw->major_ver_found = css->huc.sw_version >> 16;
355 uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
356 break;
357
358 default:
Arkadiusz Hiler6833b822017-03-15 14:34:15 +0100359 DRM_ERROR("Unknown firmware type %d\n", uc_fw->type);
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100360 err = -ENOEXEC;
361 goto fail;
362 }
363
Arkadiusz Hilerb3420dd2017-03-14 15:28:14 +0100364 if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
365 DRM_NOTE("Skipping uC firmware version check\n");
366 } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
367 uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100368 DRM_NOTE("uC firmware version %d.%d, required %d.%d\n",
369 uc_fw->major_ver_found, uc_fw->minor_ver_found,
370 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
371 err = -ENOEXEC;
372 goto fail;
373 }
374
375 DRM_DEBUG_DRIVER("firmware version %d.%d OK (minimum %d.%d)\n",
376 uc_fw->major_ver_found, uc_fw->minor_ver_found,
377 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
378
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100379 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
Arkadiusz Hiler4c0fed72017-03-14 15:28:08 +0100380 if (IS_ERR_OR_NULL(obj)) {
381 err = obj ? PTR_ERR(obj) : -ENOMEM;
382 goto fail;
383 }
384
385 uc_fw->obj = obj;
386 uc_fw->size = fw->size;
387
388 DRM_DEBUG_DRIVER("uC fw fetch status SUCCESS, obj %p\n",
389 uc_fw->obj);
390
391 release_firmware(fw);
392 uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
393 return;
394
395fail:
396 DRM_WARN("Failed to fetch valid uC firmware from %s (error %d)\n",
397 uc_fw->path, err);
398 DRM_DEBUG_DRIVER("uC fw fetch status FAIL; err %d, fw %p, obj %p\n",
399 err, fw, uc_fw->obj);
400
401 release_firmware(fw); /* OK even if fw is NULL */
402 uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
403}