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