vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / intel_uc.c
blob7c95697e1a358d90a56509fa7d5e66ca2e6cb747
1 /*
2 * Copyright © 2016 Intel Corporation
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:
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.
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.
25 #include "intel_uc.h"
26 #include "intel_guc_submission.h"
27 #include "intel_guc.h"
28 #include "i915_drv.h"
30 static void guc_free_load_err_log(struct intel_guc *guc);
32 /* Reset GuC providing us with fresh state for both GuC and HuC.
34 static int __intel_uc_reset_hw(struct drm_i915_private *dev_priv)
36 int ret;
37 u32 guc_status;
39 ret = intel_reset_guc(dev_priv);
40 if (ret) {
41 DRM_ERROR("Failed to reset GuC, ret = %d\n", ret);
42 return ret;
45 guc_status = I915_READ(GUC_STATUS);
46 WARN(!(guc_status & GS_MIA_IN_RESET),
47 "GuC status: 0x%x, MIA core expected to be in reset\n",
48 guc_status);
50 return ret;
53 static int __get_platform_enable_guc(struct drm_i915_private *i915)
55 struct intel_uc_fw *guc_fw = &i915->guc.fw;
56 struct intel_uc_fw *huc_fw = &i915->huc.fw;
57 int enable_guc = 0;
59 /* Default is to enable GuC/HuC if we know their firmwares */
60 if (intel_uc_fw_is_selected(guc_fw))
61 enable_guc |= ENABLE_GUC_SUBMISSION;
62 if (intel_uc_fw_is_selected(huc_fw))
63 enable_guc |= ENABLE_GUC_LOAD_HUC;
65 /* Any platform specific fine-tuning can be done here */
67 return enable_guc;
70 static int __get_default_guc_log_level(struct drm_i915_private *i915)
72 int guc_log_level;
74 if (!HAS_GUC(i915) || !intel_uc_is_using_guc())
75 guc_log_level = GUC_LOG_LEVEL_DISABLED;
76 else if (IS_ENABLED(CONFIG_DRM_I915_DEBUG) ||
77 IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
78 guc_log_level = GUC_LOG_LEVEL_MAX;
79 else
80 guc_log_level = GUC_LOG_LEVEL_NON_VERBOSE;
82 /* Any platform specific fine-tuning can be done here */
84 return guc_log_level;
87 /**
88 * sanitize_options_early - sanitize uC related modparam options
89 * @i915: device private
91 * In case of "enable_guc" option this function will attempt to modify
92 * it only if it was initially set to "auto(-1)". Default value for this
93 * modparam varies between platforms and it is hardcoded in driver code.
94 * Any other modparam value is only monitored against availability of the
95 * related hardware or firmware definitions.
97 * In case of "guc_log_level" option this function will attempt to modify
98 * it only if it was initially set to "auto(-1)" or if initial value was
99 * "enable(1..4)" on platforms without the GuC. Default value for this
100 * modparam varies between platforms and is usually set to "disable(0)"
101 * unless GuC is enabled on given platform and the driver is compiled with
102 * debug config when this modparam will default to "enable(1..4)".
104 static void sanitize_options_early(struct drm_i915_private *i915)
106 struct intel_uc_fw *guc_fw = &i915->guc.fw;
107 struct intel_uc_fw *huc_fw = &i915->huc.fw;
109 /* A negative value means "use platform default" */
110 if (i915_modparams.enable_guc < 0)
111 i915_modparams.enable_guc = __get_platform_enable_guc(i915);
113 DRM_DEBUG_DRIVER("enable_guc=%d (submission:%s huc:%s)\n",
114 i915_modparams.enable_guc,
115 yesno(intel_uc_is_using_guc_submission()),
116 yesno(intel_uc_is_using_huc()));
118 /* Verify GuC firmware availability */
119 if (intel_uc_is_using_guc() && !intel_uc_fw_is_selected(guc_fw)) {
120 DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
121 "enable_guc", i915_modparams.enable_guc,
122 !HAS_GUC(i915) ? "no GuC hardware" :
123 "no GuC firmware");
126 /* Verify HuC firmware availability */
127 if (intel_uc_is_using_huc() && !intel_uc_fw_is_selected(huc_fw)) {
128 DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
129 "enable_guc", i915_modparams.enable_guc,
130 !HAS_HUC(i915) ? "no HuC hardware" :
131 "no HuC firmware");
134 /* A negative value means "use platform/config default" */
135 if (i915_modparams.guc_log_level < 0)
136 i915_modparams.guc_log_level =
137 __get_default_guc_log_level(i915);
139 if (i915_modparams.guc_log_level > 0 && !intel_uc_is_using_guc()) {
140 DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
141 "guc_log_level", i915_modparams.guc_log_level,
142 !HAS_GUC(i915) ? "no GuC hardware" :
143 "GuC not enabled");
144 i915_modparams.guc_log_level = 0;
147 if (i915_modparams.guc_log_level > GUC_LOG_LEVEL_MAX) {
148 DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
149 "guc_log_level", i915_modparams.guc_log_level,
150 "verbosity too high");
151 i915_modparams.guc_log_level = GUC_LOG_LEVEL_MAX;
154 DRM_DEBUG_DRIVER("guc_log_level=%d (enabled:%s, verbose:%s, verbosity:%d)\n",
155 i915_modparams.guc_log_level,
156 yesno(i915_modparams.guc_log_level),
157 yesno(GUC_LOG_LEVEL_IS_VERBOSE(i915_modparams.guc_log_level)),
158 GUC_LOG_LEVEL_TO_VERBOSITY(i915_modparams.guc_log_level));
160 /* Make sure that sanitization was done */
161 GEM_BUG_ON(i915_modparams.enable_guc < 0);
162 GEM_BUG_ON(i915_modparams.guc_log_level < 0);
165 void intel_uc_init_early(struct drm_i915_private *i915)
167 struct intel_guc *guc = &i915->guc;
168 struct intel_huc *huc = &i915->huc;
170 intel_guc_init_early(guc);
171 intel_huc_init_early(huc);
173 sanitize_options_early(i915);
176 void intel_uc_cleanup_early(struct drm_i915_private *i915)
178 struct intel_guc *guc = &i915->guc;
180 guc_free_load_err_log(guc);
184 * intel_uc_init_mmio - setup uC MMIO access
185 * @i915: device private
187 * Setup minimal state necessary for MMIO accesses later in the
188 * initialization sequence.
190 void intel_uc_init_mmio(struct drm_i915_private *i915)
192 intel_guc_init_send_regs(&i915->guc);
195 static void guc_capture_load_err_log(struct intel_guc *guc)
197 if (!guc->log.vma || !intel_guc_log_get_level(&guc->log))
198 return;
200 if (!guc->load_err_log)
201 guc->load_err_log = i915_gem_object_get(guc->log.vma->obj);
203 return;
206 static void guc_free_load_err_log(struct intel_guc *guc)
208 if (guc->load_err_log)
209 i915_gem_object_put(guc->load_err_log);
212 static int guc_enable_communication(struct intel_guc *guc)
214 struct drm_i915_private *i915 = guc_to_i915(guc);
216 gen9_enable_guc_interrupts(i915);
218 if (HAS_GUC_CT(i915))
219 return intel_guc_ct_enable(&guc->ct);
221 guc->send = intel_guc_send_mmio;
222 guc->handler = intel_guc_to_host_event_handler_mmio;
223 return 0;
226 static void guc_disable_communication(struct intel_guc *guc)
228 struct drm_i915_private *i915 = guc_to_i915(guc);
230 if (HAS_GUC_CT(i915))
231 intel_guc_ct_disable(&guc->ct);
233 gen9_disable_guc_interrupts(i915);
235 guc->send = intel_guc_send_nop;
236 guc->handler = intel_guc_to_host_event_handler_nop;
239 int intel_uc_init_misc(struct drm_i915_private *i915)
241 struct intel_guc *guc = &i915->guc;
242 struct intel_huc *huc = &i915->huc;
243 int ret;
245 if (!USES_GUC(i915))
246 return 0;
248 ret = intel_guc_init_misc(guc);
249 if (ret)
250 return ret;
252 if (USES_HUC(i915)) {
253 ret = intel_huc_init_misc(huc);
254 if (ret)
255 goto err_guc;
258 return 0;
260 err_guc:
261 intel_guc_fini_misc(guc);
262 return ret;
265 void intel_uc_fini_misc(struct drm_i915_private *i915)
267 struct intel_guc *guc = &i915->guc;
268 struct intel_huc *huc = &i915->huc;
270 if (!USES_GUC(i915))
271 return;
273 if (USES_HUC(i915))
274 intel_huc_fini_misc(huc);
276 intel_guc_fini_misc(guc);
279 int intel_uc_init(struct drm_i915_private *i915)
281 struct intel_guc *guc = &i915->guc;
282 int ret;
284 if (!USES_GUC(i915))
285 return 0;
287 if (!HAS_GUC(i915))
288 return -ENODEV;
290 ret = intel_guc_init(guc);
291 if (ret)
292 return ret;
294 if (USES_GUC_SUBMISSION(i915)) {
296 * This is stuff we need to have available at fw load time
297 * if we are planning to enable submission later
299 ret = intel_guc_submission_init(guc);
300 if (ret) {
301 intel_guc_fini(guc);
302 return ret;
306 return 0;
309 void intel_uc_fini(struct drm_i915_private *i915)
311 struct intel_guc *guc = &i915->guc;
313 if (!USES_GUC(i915))
314 return;
316 GEM_BUG_ON(!HAS_GUC(i915));
318 if (USES_GUC_SUBMISSION(i915))
319 intel_guc_submission_fini(guc);
321 intel_guc_fini(guc);
324 void intel_uc_sanitize(struct drm_i915_private *i915)
326 struct intel_guc *guc = &i915->guc;
327 struct intel_huc *huc = &i915->huc;
329 if (!USES_GUC(i915))
330 return;
332 GEM_BUG_ON(!HAS_GUC(i915));
334 guc_disable_communication(guc);
336 intel_huc_sanitize(huc);
337 intel_guc_sanitize(guc);
339 __intel_uc_reset_hw(i915);
342 int intel_uc_init_hw(struct drm_i915_private *i915)
344 struct intel_guc *guc = &i915->guc;
345 struct intel_huc *huc = &i915->huc;
346 int ret, attempts;
348 if (!USES_GUC(i915))
349 return 0;
351 GEM_BUG_ON(!HAS_GUC(i915));
353 gen9_reset_guc_interrupts(i915);
355 /* WaEnableuKernelHeaderValidFix:skl */
356 /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
357 if (IS_GEN9(i915))
358 attempts = 3;
359 else
360 attempts = 1;
362 while (attempts--) {
364 * Always reset the GuC just before (re)loading, so
365 * that the state and timing are fairly predictable
367 ret = __intel_uc_reset_hw(i915);
368 if (ret)
369 goto err_out;
371 if (USES_HUC(i915)) {
372 ret = intel_huc_fw_upload(huc);
373 if (ret)
374 goto err_out;
377 intel_guc_init_params(guc);
378 ret = intel_guc_fw_upload(guc);
379 if (ret == 0 || ret != -EAGAIN)
380 break;
382 DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
383 "retry %d more time(s)\n", ret, attempts);
386 /* Did we succeded or run out of retries? */
387 if (ret)
388 goto err_log_capture;
390 ret = guc_enable_communication(guc);
391 if (ret)
392 goto err_log_capture;
394 if (USES_HUC(i915)) {
395 ret = intel_huc_auth(huc);
396 if (ret)
397 goto err_communication;
400 if (USES_GUC_SUBMISSION(i915)) {
401 ret = intel_guc_submission_enable(guc);
402 if (ret)
403 goto err_communication;
406 dev_info(i915->drm.dev, "GuC firmware version %u.%u\n",
407 guc->fw.major_ver_found, guc->fw.minor_ver_found);
408 dev_info(i915->drm.dev, "GuC submission %s\n",
409 enableddisabled(USES_GUC_SUBMISSION(i915)));
410 dev_info(i915->drm.dev, "HuC %s\n",
411 enableddisabled(USES_HUC(i915)));
413 return 0;
416 * We've failed to load the firmware :(
418 err_communication:
419 guc_disable_communication(guc);
420 err_log_capture:
421 guc_capture_load_err_log(guc);
422 err_out:
424 * Note that there is no fallback as either user explicitly asked for
425 * the GuC or driver default option was to run with the GuC enabled.
427 if (GEM_WARN_ON(ret == -EIO))
428 ret = -EINVAL;
430 dev_err(i915->drm.dev, "GuC initialization failed %d\n", ret);
431 return ret;
434 void intel_uc_fini_hw(struct drm_i915_private *i915)
436 struct intel_guc *guc = &i915->guc;
438 if (!USES_GUC(i915))
439 return;
441 GEM_BUG_ON(!HAS_GUC(i915));
443 if (USES_GUC_SUBMISSION(i915))
444 intel_guc_submission_disable(guc);
446 guc_disable_communication(guc);
449 int intel_uc_suspend(struct drm_i915_private *i915)
451 struct intel_guc *guc = &i915->guc;
452 int err;
454 if (!USES_GUC(i915))
455 return 0;
457 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
458 return 0;
460 err = intel_guc_suspend(guc);
461 if (err) {
462 DRM_DEBUG_DRIVER("Failed to suspend GuC, err=%d", err);
463 return err;
466 gen9_disable_guc_interrupts(i915);
468 return 0;
471 int intel_uc_resume(struct drm_i915_private *i915)
473 struct intel_guc *guc = &i915->guc;
474 int err;
476 if (!USES_GUC(i915))
477 return 0;
479 if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
480 return 0;
482 gen9_enable_guc_interrupts(i915);
484 err = intel_guc_resume(guc);
485 if (err) {
486 DRM_DEBUG_DRIVER("Failed to resume GuC, err=%d", err);
487 return err;
490 return 0;