blob: c767dc351c6399772e5efac60aeeec7f19a1bece [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
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +000092static void fetch_uc_fw(struct drm_i915_private *dev_priv,
93 struct intel_uc_fw *uc_fw)
94{
95 struct pci_dev *pdev = dev_priv->drm.pdev;
96 struct drm_i915_gem_object *obj;
97 const struct firmware *fw = NULL;
98 struct uc_css_header *css;
99 size_t size;
100 int err;
101
102 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
103
104 DRM_DEBUG_DRIVER("before requesting firmware: uC fw fetch status %s\n",
105 intel_uc_fw_status_repr(uc_fw->fetch_status));
106
107 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
108 if (err)
109 goto fail;
110 if (!fw)
111 goto fail;
112
113 DRM_DEBUG_DRIVER("fetch uC fw from %s succeeded, fw %p\n",
114 uc_fw->path, fw);
115
116 /* Check the size of the blob before examining buffer contents */
117 if (fw->size < sizeof(struct uc_css_header)) {
118 DRM_NOTE("Firmware header is missing\n");
119 goto fail;
120 }
121
122 css = (struct uc_css_header *)fw->data;
123
124 /* Firmware bits always start from header */
125 uc_fw->header_offset = 0;
126 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
127 css->key_size_dw - css->exponent_size_dw) * sizeof(u32);
128
129 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
130 DRM_NOTE("CSS header definition mismatch\n");
131 goto fail;
132 }
133
134 /* then, uCode */
135 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
136 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
137
138 /* now RSA */
139 if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
140 DRM_NOTE("RSA key size is bad\n");
141 goto fail;
142 }
143 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
144 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
145
146 /* At least, it should have header, uCode and RSA. Size of all three. */
147 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
148 if (fw->size < size) {
149 DRM_NOTE("Missing firmware components\n");
150 goto fail;
151 }
152
153 /*
154 * The GuC firmware image has the version number embedded at a
155 * well-known offset within the firmware blob; note that major / minor
156 * version are TWO bytes each (i.e. u16), although all pointers and
157 * offsets are defined in terms of bytes (u8).
158 */
159 switch (uc_fw->type) {
160 case INTEL_UC_FW_TYPE_GUC:
161 /* Header and uCode will be loaded to WOPCM. Size of the two. */
162 size = uc_fw->header_size + uc_fw->ucode_size;
163
164 /* Top 32k of WOPCM is reserved (8K stack + 24k RC6 context). */
165 if (size > intel_guc_wopcm_size(dev_priv)) {
166 DRM_ERROR("Firmware is too large to fit in WOPCM\n");
167 goto fail;
168 }
169 uc_fw->major_ver_found = css->guc.sw_version >> 16;
170 uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
171 break;
172
173 case INTEL_UC_FW_TYPE_HUC:
174 uc_fw->major_ver_found = css->huc.sw_version >> 16;
175 uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
176 break;
177
178 default:
179 DRM_ERROR("Unknown firmware type %d\n", uc_fw->type);
180 err = -ENOEXEC;
181 goto fail;
182 }
183
184 if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
185 DRM_NOTE("Skipping uC firmware version check\n");
186 } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
187 uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
188 DRM_NOTE("uC firmware version %d.%d, required %d.%d\n",
189 uc_fw->major_ver_found, uc_fw->minor_ver_found,
190 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
191 err = -ENOEXEC;
192 goto fail;
193 }
194
195 DRM_DEBUG_DRIVER("firmware version %d.%d OK (minimum %d.%d)\n",
196 uc_fw->major_ver_found, uc_fw->minor_ver_found,
197 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
198
199 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
200 if (IS_ERR(obj)) {
201 err = PTR_ERR(obj);
202 goto fail;
203 }
204
205 uc_fw->obj = obj;
206 uc_fw->size = fw->size;
207
208 DRM_DEBUG_DRIVER("uC fw fetch status SUCCESS, obj %p\n",
209 uc_fw->obj);
210
211 release_firmware(fw);
212 uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
213 return;
214
215fail:
216 DRM_WARN("Failed to fetch valid uC firmware from %s (error %d)\n",
217 uc_fw->path, err);
218 DRM_DEBUG_DRIVER("uC fw fetch status FAIL; err %d, fw %p, obj %p\n",
219 err, fw, uc_fw->obj);
220
221 release_firmware(fw); /* OK even if fw is NULL */
222 uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
223}
224
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100225void intel_uc_init_fw(struct drm_i915_private *dev_priv)
226{
Arkadiusz Hilerb551f612017-03-14 15:28:13 +0100227 if (dev_priv->huc.fw.path)
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000228 fetch_uc_fw(dev_priv, &dev_priv->huc.fw);
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +0100229
Arkadiusz Hilerb551f612017-03-14 15:28:13 +0100230 if (dev_priv->guc.fw.path)
Michal Wajdeczko9d98af0b2017-03-27 09:45:10 +0000231 fetch_uc_fw(dev_priv, &dev_priv->guc.fw);
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100232}
233
Oscar Mateo3950bf32017-03-22 10:39:46 -0700234void intel_uc_fini_fw(struct drm_i915_private *dev_priv)
235{
236 struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
237 struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
238 struct drm_i915_gem_object *obj;
239
240 obj = fetch_and_zero(&guc_fw->obj);
241 if (obj)
242 i915_gem_object_put(obj);
243
244 guc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
245
246 obj = fetch_and_zero(&huc_fw->obj);
247 if (obj)
248 i915_gem_object_put(obj);
249
250 huc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
251}
252
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100253int intel_uc_init_hw(struct drm_i915_private *dev_priv)
254{
255 int ret, attempts;
256
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100257 gen9_reset_guc_interrupts(dev_priv);
258
259 /* We need to notify the guc whenever we change the GGTT */
260 i915_ggtt_enable_guc(dev_priv);
261
Oscar Mateo397fce82017-03-22 10:39:52 -0700262 if (i915.enable_guc_submission) {
263 /*
264 * This is stuff we need to have available at fw load time
265 * if we are planning to enable submission later
266 */
267 ret = i915_guc_submission_init(dev_priv);
268 if (ret)
269 goto err_guc;
270 }
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100271
272 /* WaEnableuKernelHeaderValidFix:skl */
273 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
274 if (IS_GEN9(dev_priv))
275 attempts = 3;
276 else
277 attempts = 1;
278
279 while (attempts--) {
280 /*
281 * Always reset the GuC just before (re)loading, so
282 * that the state and timing are fairly predictable
283 */
284 ret = __intel_uc_reset_hw(dev_priv);
285 if (ret)
286 goto err_submission;
287
288 intel_huc_init_hw(&dev_priv->huc);
289 ret = intel_guc_init_hw(&dev_priv->guc);
290 if (ret == 0 || ret != -EAGAIN)
291 break;
292
293 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
294 "retry %d more time(s)\n", ret, attempts);
295 }
296
297 /* Did we succeded or run out of retries? */
298 if (ret)
299 goto err_submission;
300
301 intel_guc_auth_huc(dev_priv);
302 if (i915.enable_guc_submission) {
303 if (i915.guc_log_level >= 0)
304 gen9_enable_guc_interrupts(dev_priv);
305
306 ret = i915_guc_submission_enable(dev_priv);
307 if (ret)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700308 goto err_interrupts;
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100309 }
310
311 return 0;
312
313 /*
314 * We've failed to load the firmware :(
315 *
316 * Decide whether to disable GuC submission and fall back to
317 * execlist mode, and whether to hide the error by returning
318 * zero or to return -EIO, which the caller will treat as a
319 * nonfatal error (i.e. it doesn't prevent driver load, but
320 * marks the GPU as wedged until reset).
321 */
Oscar Mateo3950bf32017-03-22 10:39:46 -0700322err_interrupts:
323 gen9_disable_guc_interrupts(dev_priv);
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100324err_submission:
Oscar Mateo397fce82017-03-22 10:39:52 -0700325 if (i915.enable_guc_submission)
326 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700327err_guc:
Arkadiusz Hiler6cd5a722017-03-14 15:28:11 +0100328 i915_ggtt_disable_guc(dev_priv);
329
330 DRM_ERROR("GuC init failed\n");
331 if (i915.enable_guc_loading > 1 || i915.enable_guc_submission > 1)
332 ret = -EIO;
333 else
334 ret = 0;
335
336 if (i915.enable_guc_submission) {
337 i915.enable_guc_submission = 0;
338 DRM_NOTE("Falling back from GuC submission to execlist mode\n");
339 }
340
341 return ret;
342}
343
Oscar Mateo3950bf32017-03-22 10:39:46 -0700344void intel_uc_fini_hw(struct drm_i915_private *dev_priv)
345{
346 if (i915.enable_guc_submission) {
347 i915_guc_submission_disable(dev_priv);
348 gen9_disable_guc_interrupts(dev_priv);
Oscar Mateo397fce82017-03-22 10:39:52 -0700349 i915_guc_submission_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700350 }
Oscar Mateo3950bf32017-03-22 10:39:46 -0700351 i915_ggtt_disable_guc(dev_priv);
352}
353
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100354/*
355 * Read GuC command/status register (SOFT_SCRATCH_0)
356 * Return true if it contains a response rather than a command
357 */
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700358static bool guc_recv(struct intel_guc *guc, u32 *status)
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100359{
Michal Wajdeczkobae3fdc2016-12-20 11:55:31 +0000360 struct drm_i915_private *dev_priv = guc_to_i915(guc);
361
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100362 u32 val = I915_READ(SOFT_SCRATCH(0));
363 *status = val;
364 return INTEL_GUC_RECV_IS_RESPONSE(val);
365}
366
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700367/*
368 * This function implements the MMIO based host to GuC interface.
369 */
370int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len)
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100371{
372 struct drm_i915_private *dev_priv = guc_to_i915(guc);
373 u32 status;
374 int i;
375 int ret;
376
377 if (WARN_ON(len < 1 || len > 15))
378 return -EINVAL;
379
380 mutex_lock(&guc->send_mutex);
Daniele Ceraolo Spurio5d64c122017-03-24 07:48:39 -0700381 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_BLITTER);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100382
383 dev_priv->guc.action_count += 1;
384 dev_priv->guc.action_cmd = action[0];
385
386 for (i = 0; i < len; i++)
387 I915_WRITE(SOFT_SCRATCH(i), action[i]);
388
389 POSTING_READ(SOFT_SCRATCH(i - 1));
390
391 I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
392
393 /*
394 * Fast commands should complete in less than 10us, so sample quickly
395 * up to that length of time, then switch to a slower sleep-wait loop.
396 * No inte_guc_send command should ever take longer than 10ms.
397 */
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700398 ret = wait_for_us(guc_recv(guc, &status), 10);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100399 if (ret)
Oscar Mateo5e7cd372017-03-22 10:39:49 -0700400 ret = wait_for(guc_recv(guc, &status), 10);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100401 if (status != INTEL_GUC_STATUS_SUCCESS) {
402 /*
403 * Either the GuC explicitly returned an error (which
404 * we convert to -EIO here) or no response at all was
405 * received within the timeout limit (-ETIMEDOUT)
406 */
407 if (ret != -ETIMEDOUT)
408 ret = -EIO;
409
410 DRM_WARN("INTEL_GUC_SEND: Action 0x%X failed;"
411 " ret=%d status=0x%08X response=0x%08X\n",
412 action[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
413
414 dev_priv->guc.action_fail += 1;
415 dev_priv->guc.action_err = ret;
416 }
417 dev_priv->guc.action_status = status;
418
Daniele Ceraolo Spurio5d64c122017-03-24 07:48:39 -0700419 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_BLITTER);
Arkadiusz Hiler2d803c22016-11-25 18:59:35 +0100420 mutex_unlock(&guc->send_mutex);
421
422 return ret;
423}
424
425int intel_guc_sample_forcewake(struct intel_guc *guc)
426{
427 struct drm_i915_private *dev_priv = guc_to_i915(guc);
428 u32 action[2];
429
430 action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
431 /* WaRsDisableCoarsePowerGating:skl,bxt */
432 if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
433 action[1] = 0;
434 else
435 /* bit 0 and 1 are for Render and Media domain separately */
436 action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
437
438 return intel_guc_send(guc, action, ARRAY_SIZE(action));
439}