1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2016-2019 Intel Corporation
4 */
5
6 #include <linux/types.h>
7
8 #include "gt/intel_gt.h"
9 #include "gt/intel_rps.h"
10 #include "intel_guc_reg.h"
11 #include "intel_huc.h"
12 #include "intel_huc_print.h"
13 #include "i915_drv.h"
14 #include "i915_reg.h"
15 #include "pxp/intel_pxp_cmd_interface_43.h"
16
17 #include <linux/device/bus.h>
18 #include <linux/mei_aux.h>
19
20 /**
21 * DOC: HuC
22 *
23 * The HuC is a dedicated microcontroller for usage in media HEVC (High
24 * Efficiency Video Coding) operations. Userspace can directly use the firmware
25 * capabilities by adding HuC specific commands to batch buffers.
26 *
27 * The kernel driver is only responsible for loading the HuC firmware and
28 * triggering its security authentication. This is done differently depending
29 * on the platform:
30 *
31 * - older platforms (from Gen9 to most Gen12s): the load is performed via DMA
32 * and the authentication via GuC
33 * - DG2: load and authentication are both performed via GSC.
34 * - MTL and newer platforms: the load is performed via DMA (same as with
35 * not-DG2 older platforms), while the authentication is done in 2-steps,
36 * a first auth for clear-media workloads via GuC and a second one for all
37 * workloads via GSC.
38 *
39 * On platforms where the GuC does the authentication, to correctly do so the
40 * HuC binary must be loaded before the GuC one.
41 * Loading the HuC is optional; however, not using the HuC might negatively
42 * impact power usage and/or performance of media workloads, depending on the
43 * use-cases.
44 * HuC must be reloaded on events that cause the WOPCM to lose its contents
45 * (S3/S4, FLR); on older platforms the HuC must also be reloaded on GuC/GT
46 * reset, while on newer ones it will survive that.
47 *
48 * See https://github.com/intel/media-driver for the latest details on HuC
49 * functionality.
50 */
51
52 /**
53 * DOC: HuC Memory Management
54 *
55 * Similarly to the GuC, the HuC can't do any memory allocations on its own,
56 * with the difference being that the allocations for HuC usage are handled by
57 * the userspace driver instead of the kernel one. The HuC accesses the memory
58 * via the PPGTT belonging to the context loaded on the VCS executing the
59 * HuC-specific commands.
60 */
61
62 /*
63 * MEI-GSC load is an async process. The probing of the exposed aux device
64 * (see intel_gsc.c) usually happens a few seconds after i915 probe, depending
65 * on when the kernel schedules it. Unless something goes terribly wrong, we're
66 * guaranteed for this to happen during boot, so the big timeout is a safety net
67 * that we never expect to need.
68 * MEI-PXP + HuC load usually takes ~300ms, but if the GSC needs to be resumed
69 * and/or reset, this can take longer. Note that the kernel might schedule
70 * other work between the i915 init/resume and the MEI one, which can add to
71 * the delay.
72 */
73 #define GSC_INIT_TIMEOUT_MS 10000
74 #define PXP_INIT_TIMEOUT_MS 5000
75
sw_fence_dummy_notify(struct i915_sw_fence * sf,enum i915_sw_fence_notify state)76 static int sw_fence_dummy_notify(struct i915_sw_fence *sf,
77 enum i915_sw_fence_notify state)
78 {
79 return NOTIFY_DONE;
80 }
81
__delayed_huc_load_complete(struct intel_huc * huc)82 static void __delayed_huc_load_complete(struct intel_huc *huc)
83 {
84 if (!i915_sw_fence_done(&huc->delayed_load.fence))
85 i915_sw_fence_complete(&huc->delayed_load.fence);
86 }
87
delayed_huc_load_complete(struct intel_huc * huc)88 static void delayed_huc_load_complete(struct intel_huc *huc)
89 {
90 hrtimer_cancel(&huc->delayed_load.timer);
91 __delayed_huc_load_complete(huc);
92 }
93
__gsc_init_error(struct intel_huc * huc)94 static void __gsc_init_error(struct intel_huc *huc)
95 {
96 huc->delayed_load.status = INTEL_HUC_DELAYED_LOAD_ERROR;
97 __delayed_huc_load_complete(huc);
98 }
99
gsc_init_error(struct intel_huc * huc)100 static void gsc_init_error(struct intel_huc *huc)
101 {
102 hrtimer_cancel(&huc->delayed_load.timer);
103 __gsc_init_error(huc);
104 }
105
gsc_init_done(struct intel_huc * huc)106 static void gsc_init_done(struct intel_huc *huc)
107 {
108 hrtimer_cancel(&huc->delayed_load.timer);
109
110 /* MEI-GSC init is done, now we wait for MEI-PXP to bind */
111 huc->delayed_load.status = INTEL_HUC_WAITING_ON_PXP;
112 if (!i915_sw_fence_done(&huc->delayed_load.fence))
113 #ifdef __linux__
114 hrtimer_start(&huc->delayed_load.timer,
115 ms_to_ktime(PXP_INIT_TIMEOUT_MS),
116 HRTIMER_MODE_REL);
117 #else
118 timeout_add_msec(&huc->delayed_load.timer, PXP_INIT_TIMEOUT_MS);
119 #endif
120 }
121
huc_delayed_load_timer_callback(void * arg)122 static void huc_delayed_load_timer_callback(void *arg)
123 {
124 struct intel_huc *huc = arg;
125
126 if (!intel_huc_is_authenticated(huc, INTEL_HUC_AUTH_BY_GSC)) {
127 if (huc->delayed_load.status == INTEL_HUC_WAITING_ON_GSC)
128 huc_notice(huc, "timed out waiting for MEI GSC\n");
129 else if (huc->delayed_load.status == INTEL_HUC_WAITING_ON_PXP)
130 huc_notice(huc, "timed out waiting for MEI PXP\n");
131 else
132 MISSING_CASE(huc->delayed_load.status);
133
134 __gsc_init_error(huc);
135 }
136 }
137
huc_delayed_load_start(struct intel_huc * huc)138 static void huc_delayed_load_start(struct intel_huc *huc)
139 {
140 ktime_t delay;
141
142 GEM_BUG_ON(intel_huc_is_authenticated(huc, INTEL_HUC_AUTH_BY_GSC));
143
144 /*
145 * On resume we don't have to wait for MEI-GSC to be re-probed, but we
146 * do need to wait for MEI-PXP to reset & re-bind
147 */
148 switch (huc->delayed_load.status) {
149 case INTEL_HUC_WAITING_ON_GSC:
150 delay = ms_to_ktime(GSC_INIT_TIMEOUT_MS);
151 break;
152 case INTEL_HUC_WAITING_ON_PXP:
153 delay = ms_to_ktime(PXP_INIT_TIMEOUT_MS);
154 break;
155 default:
156 gsc_init_error(huc);
157 return;
158 }
159
160 /*
161 * This fence is always complete unless we're waiting for the
162 * GSC device to come up to load the HuC. We arm the fence here
163 * and complete it when we confirm that the HuC is loaded from
164 * the PXP bind callback.
165 */
166 GEM_BUG_ON(!i915_sw_fence_done(&huc->delayed_load.fence));
167 i915_sw_fence_fini(&huc->delayed_load.fence);
168 i915_sw_fence_reinit(&huc->delayed_load.fence);
169 i915_sw_fence_await(&huc->delayed_load.fence);
170 i915_sw_fence_commit(&huc->delayed_load.fence);
171
172 #ifdef __linux__
173 hrtimer_start(&huc->delayed_load.timer, delay, HRTIMER_MODE_REL);
174 #else
175 timeout_add_nsec(&huc->delayed_load.timer, ktime_to_ns(delay));
176 #endif
177 }
178
gsc_notifier(struct notifier_block * nb,unsigned long action,void * data)179 static int gsc_notifier(struct notifier_block *nb, unsigned long action, void *data)
180 {
181 STUB();
182 return -ENOSYS;
183 #ifdef notyet
184 struct device *dev = data;
185 struct intel_huc *huc = container_of(nb, struct intel_huc, delayed_load.nb);
186 struct intel_gsc_intf *intf = &huc_to_gt(huc)->gsc.intf[0];
187
188 if (!intf->adev || &intf->adev->aux_dev.dev != dev)
189 return 0;
190
191 switch (action) {
192 case BUS_NOTIFY_BOUND_DRIVER: /* mei driver bound to aux device */
193 gsc_init_done(huc);
194 break;
195
196 case BUS_NOTIFY_DRIVER_NOT_BOUND: /* mei driver fails to be bound */
197 case BUS_NOTIFY_UNBIND_DRIVER: /* mei driver about to be unbound */
198 huc_info(huc, "MEI driver not bound, disabling load\n");
199 gsc_init_error(huc);
200 break;
201 }
202
203 return 0;
204 #endif
205 }
206
intel_huc_register_gsc_notifier(struct intel_huc * huc,const struct bus_type * bus)207 void intel_huc_register_gsc_notifier(struct intel_huc *huc, const struct bus_type *bus)
208 {
209 int ret;
210
211 if (!intel_huc_is_loaded_by_gsc(huc))
212 return;
213
214 huc->delayed_load.nb.notifier_call = gsc_notifier;
215 ret = bus_register_notifier(bus, &huc->delayed_load.nb);
216 if (ret) {
217 huc_err(huc, "failed to register GSC notifier %pe\n", ERR_PTR(ret));
218 huc->delayed_load.nb.notifier_call = NULL;
219 gsc_init_error(huc);
220 }
221 }
222
intel_huc_unregister_gsc_notifier(struct intel_huc * huc,const struct bus_type * bus)223 void intel_huc_unregister_gsc_notifier(struct intel_huc *huc, const struct bus_type *bus)
224 {
225 if (!huc->delayed_load.nb.notifier_call)
226 return;
227
228 delayed_huc_load_complete(huc);
229
230 bus_unregister_notifier(bus, &huc->delayed_load.nb);
231 huc->delayed_load.nb.notifier_call = NULL;
232 }
233
delayed_huc_load_init(struct intel_huc * huc)234 static void delayed_huc_load_init(struct intel_huc *huc)
235 {
236 /*
237 * Initialize fence to be complete as this is expected to be complete
238 * unless there is a delayed HuC load in progress.
239 */
240 i915_sw_fence_init(&huc->delayed_load.fence,
241 sw_fence_dummy_notify);
242 i915_sw_fence_commit(&huc->delayed_load.fence);
243
244 #ifdef __linux__
245 hrtimer_init(&huc->delayed_load.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
246 huc->delayed_load.timer.function = huc_delayed_load_timer_callback;
247 #else
248 timeout_set(&huc->delayed_load.timer, huc_delayed_load_timer_callback,
249 huc);
250 #endif
251 }
252
delayed_huc_load_fini(struct intel_huc * huc)253 static void delayed_huc_load_fini(struct intel_huc *huc)
254 {
255 /*
256 * the fence is initialized in init_early, so we need to clean it up
257 * even if HuC loading is off.
258 */
259 delayed_huc_load_complete(huc);
260 i915_sw_fence_fini(&huc->delayed_load.fence);
261 }
262
intel_huc_sanitize(struct intel_huc * huc)263 int intel_huc_sanitize(struct intel_huc *huc)
264 {
265 delayed_huc_load_complete(huc);
266 intel_uc_fw_sanitize(&huc->fw);
267 return 0;
268 }
269
vcs_supported(struct intel_gt * gt)270 static bool vcs_supported(struct intel_gt *gt)
271 {
272 intel_engine_mask_t mask = gt->info.engine_mask;
273
274 /*
275 * We reach here from i915_driver_early_probe for the primary GT before
276 * its engine mask is set, so we use the device info engine mask for it;
277 * this means we're not taking VCS fusing into account, but if the
278 * primary GT supports VCS engines we expect at least one of them to
279 * remain unfused so we're fine.
280 * For other GTs we expect the GT-specific mask to be set before we
281 * call this function.
282 */
283 GEM_BUG_ON(!gt_is_root(gt) && !gt->info.engine_mask);
284
285 if (gt_is_root(gt))
286 mask = INTEL_INFO(gt->i915)->platform_engine_mask;
287 else
288 mask = gt->info.engine_mask;
289
290 return __ENGINE_INSTANCES_MASK(mask, VCS0, I915_MAX_VCS);
291 }
292
intel_huc_init_early(struct intel_huc * huc)293 void intel_huc_init_early(struct intel_huc *huc)
294 {
295 struct drm_i915_private *i915 = huc_to_gt(huc)->i915;
296 struct intel_gt *gt = huc_to_gt(huc);
297
298 intel_uc_fw_init_early(&huc->fw, INTEL_UC_FW_TYPE_HUC, true);
299
300 /*
301 * we always init the fence as already completed, even if HuC is not
302 * supported. This way we don't have to distinguish between HuC not
303 * supported/disabled or already loaded, and can focus on if the load
304 * is currently in progress (fence not complete) or not, which is what
305 * we care about for stalling userspace submissions.
306 */
307 delayed_huc_load_init(huc);
308
309 if (!vcs_supported(gt)) {
310 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_NOT_SUPPORTED);
311 return;
312 }
313
314 if (GRAPHICS_VER(i915) >= 11) {
315 huc->status[INTEL_HUC_AUTH_BY_GUC].reg = GEN11_HUC_KERNEL_LOAD_INFO;
316 huc->status[INTEL_HUC_AUTH_BY_GUC].mask = HUC_LOAD_SUCCESSFUL;
317 huc->status[INTEL_HUC_AUTH_BY_GUC].value = HUC_LOAD_SUCCESSFUL;
318 } else {
319 huc->status[INTEL_HUC_AUTH_BY_GUC].reg = HUC_STATUS2;
320 huc->status[INTEL_HUC_AUTH_BY_GUC].mask = HUC_FW_VERIFIED;
321 huc->status[INTEL_HUC_AUTH_BY_GUC].value = HUC_FW_VERIFIED;
322 }
323
324 if (IS_DG2(i915)) {
325 huc->status[INTEL_HUC_AUTH_BY_GSC].reg = GEN11_HUC_KERNEL_LOAD_INFO;
326 huc->status[INTEL_HUC_AUTH_BY_GSC].mask = HUC_LOAD_SUCCESSFUL;
327 huc->status[INTEL_HUC_AUTH_BY_GSC].value = HUC_LOAD_SUCCESSFUL;
328 } else {
329 huc->status[INTEL_HUC_AUTH_BY_GSC].reg = HECI_FWSTS(MTL_GSC_HECI1_BASE, 5);
330 huc->status[INTEL_HUC_AUTH_BY_GSC].mask = HECI1_FWSTS5_HUC_AUTH_DONE;
331 huc->status[INTEL_HUC_AUTH_BY_GSC].value = HECI1_FWSTS5_HUC_AUTH_DONE;
332 }
333 }
334
335 #define HUC_LOAD_MODE_STRING(x) (x ? "GSC" : "legacy")
check_huc_loading_mode(struct intel_huc * huc)336 static int check_huc_loading_mode(struct intel_huc *huc)
337 {
338 struct intel_gt *gt = huc_to_gt(huc);
339 bool gsc_enabled = huc->fw.has_gsc_headers;
340
341 /*
342 * The fuse for HuC load via GSC is only valid on platforms that have
343 * GuC deprivilege.
344 */
345 if (HAS_GUC_DEPRIVILEGE(gt->i915))
346 huc->loaded_via_gsc = intel_uncore_read(gt->uncore, GUC_SHIM_CONTROL2) &
347 GSC_LOADS_HUC;
348
349 if (huc->loaded_via_gsc && !gsc_enabled) {
350 huc_err(huc, "HW requires a GSC-enabled blob, but we found a legacy one\n");
351 return -ENOEXEC;
352 }
353
354 /*
355 * On newer platforms we have GSC-enabled binaries but we load the HuC
356 * via DMA. To do so we need to find the location of the legacy-style
357 * binary inside the GSC-enabled one, which we do at fetch time. Make
358 * sure that we were able to do so if the fuse says we need to load via
359 * DMA and the binary is GSC-enabled.
360 */
361 if (!huc->loaded_via_gsc && gsc_enabled && !huc->fw.dma_start_offset) {
362 huc_err(huc, "HW in DMA mode, but we have an incompatible GSC-enabled blob\n");
363 return -ENOEXEC;
364 }
365
366 /*
367 * If the HuC is loaded via GSC, we need to be able to access the GSC.
368 * On DG2 this is done via the mei components, while on newer platforms
369 * it is done via the GSCCS,
370 */
371 if (huc->loaded_via_gsc) {
372 if (IS_DG2(gt->i915)) {
373 if (!IS_ENABLED(CONFIG_INTEL_MEI_PXP) ||
374 !IS_ENABLED(CONFIG_INTEL_MEI_GSC)) {
375 huc_info(huc, "can't load due to missing mei modules\n");
376 return -EIO;
377 }
378 } else {
379 if (!HAS_ENGINE(gt, GSC0)) {
380 huc_info(huc, "can't load due to missing GSCCS\n");
381 return -EIO;
382 }
383 }
384 }
385
386 huc_dbg(huc, "loaded by GSC = %s\n", str_yes_no(huc->loaded_via_gsc));
387
388 return 0;
389 }
390
intel_huc_init(struct intel_huc * huc)391 int intel_huc_init(struct intel_huc *huc)
392 {
393 struct intel_gt *gt = huc_to_gt(huc);
394 int err;
395
396 err = check_huc_loading_mode(huc);
397 if (err)
398 goto out;
399
400 if (HAS_ENGINE(gt, GSC0)) {
401 struct i915_vma *vma;
402
403 vma = intel_guc_allocate_vma(>->uc.guc, PXP43_HUC_AUTH_INOUT_SIZE * 2);
404 if (IS_ERR(vma)) {
405 err = PTR_ERR(vma);
406 huc_info(huc, "Failed to allocate heci pkt\n");
407 goto out;
408 }
409
410 huc->heci_pkt = vma;
411 }
412
413 err = intel_uc_fw_init(&huc->fw);
414 if (err)
415 goto out_pkt;
416
417 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_LOADABLE);
418
419 return 0;
420
421 out_pkt:
422 if (huc->heci_pkt)
423 i915_vma_unpin_and_release(&huc->heci_pkt, 0);
424 out:
425 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_INIT_FAIL);
426 huc_info(huc, "initialization failed %pe\n", ERR_PTR(err));
427 return err;
428 }
429
intel_huc_fini(struct intel_huc * huc)430 void intel_huc_fini(struct intel_huc *huc)
431 {
432 /*
433 * the fence is initialized in init_early, so we need to clean it up
434 * even if HuC loading is off.
435 */
436 delayed_huc_load_fini(huc);
437
438 if (huc->heci_pkt)
439 i915_vma_unpin_and_release(&huc->heci_pkt, 0);
440
441 if (intel_uc_fw_is_loadable(&huc->fw))
442 intel_uc_fw_fini(&huc->fw);
443 }
444
intel_huc_suspend(struct intel_huc * huc)445 void intel_huc_suspend(struct intel_huc *huc)
446 {
447 if (!intel_uc_fw_is_loadable(&huc->fw))
448 return;
449
450 /*
451 * in the unlikely case that we're suspending before the GSC has
452 * completed its loading sequence, just stop waiting. We'll restart
453 * on resume.
454 */
455 delayed_huc_load_complete(huc);
456 }
457
auth_mode_string(struct intel_huc * huc,enum intel_huc_authentication_type type)458 static const char *auth_mode_string(struct intel_huc *huc,
459 enum intel_huc_authentication_type type)
460 {
461 bool partial = huc->fw.has_gsc_headers && type == INTEL_HUC_AUTH_BY_GUC;
462
463 return partial ? "clear media" : "all workloads";
464 }
465
466 /*
467 * Use a longer timeout for debug builds so that problems can be detected
468 * and analysed. But a shorter timeout for releases so that user's don't
469 * wait forever to find out there is a problem. Note that the only reason
470 * an end user should hit the timeout is in case of extreme thermal throttling.
471 * And a system that is that hot during boot is probably dead anyway!
472 */
473 #if defined(CONFIG_DRM_I915_DEBUG_GEM)
474 #define HUC_LOAD_RETRY_LIMIT 20
475 #else
476 #define HUC_LOAD_RETRY_LIMIT 3
477 #endif
478
intel_huc_wait_for_auth_complete(struct intel_huc * huc,enum intel_huc_authentication_type type)479 int intel_huc_wait_for_auth_complete(struct intel_huc *huc,
480 enum intel_huc_authentication_type type)
481 {
482 struct intel_gt *gt = huc_to_gt(huc);
483 struct intel_uncore *uncore = gt->uncore;
484 ktime_t before, after, delta;
485 int ret, count;
486 u64 delta_ms;
487 u32 before_freq;
488
489 /*
490 * The KMD requests maximum frequency during driver load, however thermal
491 * throttling can force the frequency down to minimum (although the board
492 * really should never get that hot in real life!). IFWI issues have been
493 * seen to cause sporadic failures to grant the higher frequency. And at
494 * minimum frequency, the authentication time can be in the seconds range.
495 * Note that there is a limit on how long an individual wait_for() can wait.
496 * So wrap it in a loop.
497 */
498 before_freq = intel_rps_read_actual_frequency(>->rps);
499 before = ktime_get();
500 for (count = 0; count < HUC_LOAD_RETRY_LIMIT; count++) {
501 ret = __intel_wait_for_register(gt->uncore,
502 huc->status[type].reg,
503 huc->status[type].mask,
504 huc->status[type].value,
505 2, 1000, NULL);
506 if (!ret)
507 break;
508
509 huc_dbg(huc, "auth still in progress, count = %d, freq = %dMHz, status = 0x%08X\n",
510 count, intel_rps_read_actual_frequency(>->rps),
511 huc->status[type].reg.reg);
512 }
513 after = ktime_get();
514 delta = ktime_sub(after, before);
515 delta_ms = ktime_to_ms(delta);
516
517 if (delta_ms > 50) {
518 huc_warn(huc, "excessive auth time: %lldms! [status = 0x%08X, count = %d, ret = %d]\n",
519 delta_ms, huc->status[type].reg.reg, count, ret);
520 huc_warn(huc, "excessive auth time: [freq = %dMHz, before = %dMHz, perf_limit_reasons = 0x%08X]\n",
521 intel_rps_read_actual_frequency(>->rps), before_freq,
522 intel_uncore_read(uncore, intel_gt_perf_limit_reasons_reg(gt)));
523 } else {
524 huc_dbg(huc, "auth took %lldms, freq = %dMHz, before = %dMHz, status = 0x%08X, count = %d, ret = %d\n",
525 delta_ms, intel_rps_read_actual_frequency(>->rps),
526 before_freq, huc->status[type].reg.reg, count, ret);
527 }
528
529 /* mark the load process as complete even if the wait failed */
530 delayed_huc_load_complete(huc);
531
532 if (ret) {
533 huc_err(huc, "firmware not verified for %s: %pe\n",
534 auth_mode_string(huc, type), ERR_PTR(ret));
535 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_LOAD_FAIL);
536 return ret;
537 }
538
539 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_RUNNING);
540 huc_info(huc, "authenticated for %s\n", auth_mode_string(huc, type));
541 return 0;
542 }
543
544 /**
545 * intel_huc_auth() - Authenticate HuC uCode
546 * @huc: intel_huc structure
547 * @type: authentication type (via GuC or via GSC)
548 *
549 * Called after HuC and GuC firmware loading during intel_uc_init_hw().
550 *
551 * This function invokes the GuC action to authenticate the HuC firmware,
552 * passing the offset of the RSA signature to intel_guc_auth_huc(). It then
553 * waits for up to 50ms for firmware verification ACK.
554 */
intel_huc_auth(struct intel_huc * huc,enum intel_huc_authentication_type type)555 int intel_huc_auth(struct intel_huc *huc, enum intel_huc_authentication_type type)
556 {
557 struct intel_gt *gt = huc_to_gt(huc);
558 struct intel_guc *guc = >->uc.guc;
559 int ret;
560
561 if (!intel_uc_fw_is_loaded(&huc->fw))
562 return -ENOEXEC;
563
564 /* GSC will do the auth with the load */
565 if (intel_huc_is_loaded_by_gsc(huc))
566 return -ENODEV;
567
568 if (intel_huc_is_authenticated(huc, type))
569 return -EEXIST;
570
571 ret = i915_inject_probe_error(gt->i915, -ENXIO);
572 if (ret)
573 goto fail;
574
575 switch (type) {
576 case INTEL_HUC_AUTH_BY_GUC:
577 ret = intel_guc_auth_huc(guc, intel_guc_ggtt_offset(guc, huc->fw.rsa_data));
578 break;
579 case INTEL_HUC_AUTH_BY_GSC:
580 ret = intel_huc_fw_auth_via_gsccs(huc);
581 break;
582 default:
583 MISSING_CASE(type);
584 ret = -EINVAL;
585 }
586 if (ret)
587 goto fail;
588
589 /* Check authentication status, it should be done by now */
590 ret = intel_huc_wait_for_auth_complete(huc, type);
591 if (ret)
592 goto fail;
593
594 return 0;
595
596 fail:
597 huc_probe_error(huc, "%s authentication failed %pe\n",
598 auth_mode_string(huc, type), ERR_PTR(ret));
599 return ret;
600 }
601
intel_huc_is_authenticated(struct intel_huc * huc,enum intel_huc_authentication_type type)602 bool intel_huc_is_authenticated(struct intel_huc *huc,
603 enum intel_huc_authentication_type type)
604 {
605 struct intel_gt *gt = huc_to_gt(huc);
606 intel_wakeref_t wakeref;
607 u32 status = 0;
608
609 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
610 status = intel_uncore_read(gt->uncore, huc->status[type].reg);
611
612 return (status & huc->status[type].mask) == huc->status[type].value;
613 }
614
huc_is_fully_authenticated(struct intel_huc * huc)615 static bool huc_is_fully_authenticated(struct intel_huc *huc)
616 {
617 struct intel_uc_fw *huc_fw = &huc->fw;
618
619 if (!huc_fw->has_gsc_headers)
620 return intel_huc_is_authenticated(huc, INTEL_HUC_AUTH_BY_GUC);
621 else if (intel_huc_is_loaded_by_gsc(huc) || HAS_ENGINE(huc_to_gt(huc), GSC0))
622 return intel_huc_is_authenticated(huc, INTEL_HUC_AUTH_BY_GSC);
623 else
624 return false;
625 }
626
627 /**
628 * intel_huc_check_status() - check HuC status
629 * @huc: intel_huc structure
630 *
631 * This function reads status register to verify if HuC
632 * firmware was successfully loaded.
633 *
634 * The return values match what is expected for the I915_PARAM_HUC_STATUS
635 * getparam.
636 */
intel_huc_check_status(struct intel_huc * huc)637 int intel_huc_check_status(struct intel_huc *huc)
638 {
639 struct intel_uc_fw *huc_fw = &huc->fw;
640
641 switch (__intel_uc_fw_status(huc_fw)) {
642 case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
643 return -ENODEV;
644 case INTEL_UC_FIRMWARE_DISABLED:
645 return -EOPNOTSUPP;
646 case INTEL_UC_FIRMWARE_MISSING:
647 return -ENOPKG;
648 case INTEL_UC_FIRMWARE_ERROR:
649 return -ENOEXEC;
650 case INTEL_UC_FIRMWARE_INIT_FAIL:
651 return -ENOMEM;
652 case INTEL_UC_FIRMWARE_LOAD_FAIL:
653 return -EIO;
654 default:
655 break;
656 }
657
658 /*
659 * GSC-enabled binaries loaded via DMA are first partially
660 * authenticated by GuC and then fully authenticated by GSC
661 */
662 if (huc_is_fully_authenticated(huc))
663 return 1; /* full auth */
664 else if (huc_fw->has_gsc_headers && !intel_huc_is_loaded_by_gsc(huc) &&
665 intel_huc_is_authenticated(huc, INTEL_HUC_AUTH_BY_GUC))
666 return 2; /* clear media only */
667 else
668 return 0;
669 }
670
huc_has_delayed_load(struct intel_huc * huc)671 static bool huc_has_delayed_load(struct intel_huc *huc)
672 {
673 return intel_huc_is_loaded_by_gsc(huc) &&
674 (huc->delayed_load.status != INTEL_HUC_DELAYED_LOAD_ERROR);
675 }
676
intel_huc_update_auth_status(struct intel_huc * huc)677 void intel_huc_update_auth_status(struct intel_huc *huc)
678 {
679 if (!intel_uc_fw_is_loadable(&huc->fw))
680 return;
681
682 if (!huc->fw.has_gsc_headers)
683 return;
684
685 if (huc_is_fully_authenticated(huc))
686 intel_uc_fw_change_status(&huc->fw,
687 INTEL_UC_FIRMWARE_RUNNING);
688 else if (huc_has_delayed_load(huc))
689 huc_delayed_load_start(huc);
690 }
691
692 /**
693 * intel_huc_load_status - dump information about HuC load status
694 * @huc: the HuC
695 * @p: the &drm_printer
696 *
697 * Pretty printer for HuC load status.
698 */
intel_huc_load_status(struct intel_huc * huc,struct drm_printer * p)699 void intel_huc_load_status(struct intel_huc *huc, struct drm_printer *p)
700 {
701 struct intel_gt *gt = huc_to_gt(huc);
702 intel_wakeref_t wakeref;
703
704 if (!intel_huc_is_supported(huc)) {
705 drm_printf(p, "HuC not supported\n");
706 return;
707 }
708
709 if (!intel_huc_is_wanted(huc)) {
710 drm_printf(p, "HuC disabled\n");
711 return;
712 }
713
714 intel_uc_fw_dump(&huc->fw, p);
715
716 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
717 drm_printf(p, "HuC status: 0x%08x\n",
718 intel_uncore_read(gt->uncore, huc->status[INTEL_HUC_AUTH_BY_GUC].reg));
719 }
720