xref: /openbmc/linux/drivers/gpu/drm/i915/gt/uc/intel_huc.c (revision 1f0d40d8)
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_gt_print.h"
10 #include "intel_guc_reg.h"
11 #include "intel_huc.h"
12 #include "i915_drv.h"
13 
14 #include <linux/device/bus.h>
15 #include <linux/mei_aux.h>
16 
17 #define huc_printk(_huc, _level, _fmt, ...) \
18 	gt_##_level(huc_to_gt(_huc), "HuC: " _fmt, ##__VA_ARGS__)
19 #define huc_err(_huc, _fmt, ...)	huc_printk((_huc), err, _fmt, ##__VA_ARGS__)
20 #define huc_warn(_huc, _fmt, ...)	huc_printk((_huc), warn, _fmt, ##__VA_ARGS__)
21 #define huc_notice(_huc, _fmt, ...)	huc_printk((_huc), notice, _fmt, ##__VA_ARGS__)
22 #define huc_info(_huc, _fmt, ...)	huc_printk((_huc), info, _fmt, ##__VA_ARGS__)
23 #define huc_dbg(_huc, _fmt, ...)	huc_printk((_huc), dbg, _fmt, ##__VA_ARGS__)
24 #define huc_probe_error(_huc, _fmt, ...) huc_printk((_huc), probe_error, _fmt, ##__VA_ARGS__)
25 
26 /**
27  * DOC: HuC
28  *
29  * The HuC is a dedicated microcontroller for usage in media HEVC (High
30  * Efficiency Video Coding) operations. Userspace can directly use the firmware
31  * capabilities by adding HuC specific commands to batch buffers.
32  *
33  * The kernel driver is only responsible for loading the HuC firmware and
34  * triggering its security authentication, which is performed by the GuC on
35  * older platforms and by the GSC on newer ones. For the GuC to correctly
36  * perform the authentication, the HuC binary must be loaded before the GuC one.
37  * Loading the HuC is optional; however, not using the HuC might negatively
38  * impact power usage and/or performance of media workloads, depending on the
39  * use-cases.
40  * HuC must be reloaded on events that cause the WOPCM to lose its contents
41  * (S3/S4, FLR); GuC-authenticated HuC must also be reloaded on GuC/GT reset,
42  * while GSC-managed HuC will survive that.
43  *
44  * See https://github.com/intel/media-driver for the latest details on HuC
45  * functionality.
46  */
47 
48 /**
49  * DOC: HuC Memory Management
50  *
51  * Similarly to the GuC, the HuC can't do any memory allocations on its own,
52  * with the difference being that the allocations for HuC usage are handled by
53  * the userspace driver instead of the kernel one. The HuC accesses the memory
54  * via the PPGTT belonging to the context loaded on the VCS executing the
55  * HuC-specific commands.
56  */
57 
58 /*
59  * MEI-GSC load is an async process. The probing of the exposed aux device
60  * (see intel_gsc.c) usually happens a few seconds after i915 probe, depending
61  * on when the kernel schedules it. Unless something goes terribly wrong, we're
62  * guaranteed for this to happen during boot, so the big timeout is a safety net
63  * that we never expect to need.
64  * MEI-PXP + HuC load usually takes ~300ms, but if the GSC needs to be resumed
65  * and/or reset, this can take longer. Note that the kernel might schedule
66  * other work between the i915 init/resume and the MEI one, which can add to
67  * the delay.
68  */
69 #define GSC_INIT_TIMEOUT_MS 10000
70 #define PXP_INIT_TIMEOUT_MS 5000
71 
72 static int sw_fence_dummy_notify(struct i915_sw_fence *sf,
73 				 enum i915_sw_fence_notify state)
74 {
75 	return NOTIFY_DONE;
76 }
77 
78 static void __delayed_huc_load_complete(struct intel_huc *huc)
79 {
80 	if (!i915_sw_fence_done(&huc->delayed_load.fence))
81 		i915_sw_fence_complete(&huc->delayed_load.fence);
82 }
83 
84 static void delayed_huc_load_complete(struct intel_huc *huc)
85 {
86 	hrtimer_cancel(&huc->delayed_load.timer);
87 	__delayed_huc_load_complete(huc);
88 }
89 
90 static void __gsc_init_error(struct intel_huc *huc)
91 {
92 	huc->delayed_load.status = INTEL_HUC_DELAYED_LOAD_ERROR;
93 	__delayed_huc_load_complete(huc);
94 }
95 
96 static void gsc_init_error(struct intel_huc *huc)
97 {
98 	hrtimer_cancel(&huc->delayed_load.timer);
99 	__gsc_init_error(huc);
100 }
101 
102 static void gsc_init_done(struct intel_huc *huc)
103 {
104 	hrtimer_cancel(&huc->delayed_load.timer);
105 
106 	/* MEI-GSC init is done, now we wait for MEI-PXP to bind */
107 	huc->delayed_load.status = INTEL_HUC_WAITING_ON_PXP;
108 	if (!i915_sw_fence_done(&huc->delayed_load.fence))
109 		hrtimer_start(&huc->delayed_load.timer,
110 			      ms_to_ktime(PXP_INIT_TIMEOUT_MS),
111 			      HRTIMER_MODE_REL);
112 }
113 
114 static enum hrtimer_restart huc_delayed_load_timer_callback(struct hrtimer *hrtimer)
115 {
116 	struct intel_huc *huc = container_of(hrtimer, struct intel_huc, delayed_load.timer);
117 
118 	if (!intel_huc_is_authenticated(huc)) {
119 		if (huc->delayed_load.status == INTEL_HUC_WAITING_ON_GSC)
120 			huc_notice(huc, "timed out waiting for MEI GSC\n");
121 		else if (huc->delayed_load.status == INTEL_HUC_WAITING_ON_PXP)
122 			huc_notice(huc, "timed out waiting for MEI PXP\n");
123 		else
124 			MISSING_CASE(huc->delayed_load.status);
125 
126 		__gsc_init_error(huc);
127 	}
128 
129 	return HRTIMER_NORESTART;
130 }
131 
132 static void huc_delayed_load_start(struct intel_huc *huc)
133 {
134 	ktime_t delay;
135 
136 	GEM_BUG_ON(intel_huc_is_authenticated(huc));
137 
138 	/*
139 	 * On resume we don't have to wait for MEI-GSC to be re-probed, but we
140 	 * do need to wait for MEI-PXP to reset & re-bind
141 	 */
142 	switch (huc->delayed_load.status) {
143 	case INTEL_HUC_WAITING_ON_GSC:
144 		delay = ms_to_ktime(GSC_INIT_TIMEOUT_MS);
145 		break;
146 	case INTEL_HUC_WAITING_ON_PXP:
147 		delay = ms_to_ktime(PXP_INIT_TIMEOUT_MS);
148 		break;
149 	default:
150 		gsc_init_error(huc);
151 		return;
152 	}
153 
154 	/*
155 	 * This fence is always complete unless we're waiting for the
156 	 * GSC device to come up to load the HuC. We arm the fence here
157 	 * and complete it when we confirm that the HuC is loaded from
158 	 * the PXP bind callback.
159 	 */
160 	GEM_BUG_ON(!i915_sw_fence_done(&huc->delayed_load.fence));
161 	i915_sw_fence_fini(&huc->delayed_load.fence);
162 	i915_sw_fence_reinit(&huc->delayed_load.fence);
163 	i915_sw_fence_await(&huc->delayed_load.fence);
164 	i915_sw_fence_commit(&huc->delayed_load.fence);
165 
166 	hrtimer_start(&huc->delayed_load.timer, delay, HRTIMER_MODE_REL);
167 }
168 
169 static int gsc_notifier(struct notifier_block *nb, unsigned long action, void *data)
170 {
171 	struct device *dev = data;
172 	struct intel_huc *huc = container_of(nb, struct intel_huc, delayed_load.nb);
173 	struct intel_gsc_intf *intf = &huc_to_gt(huc)->gsc.intf[0];
174 
175 	if (!intf->adev || &intf->adev->aux_dev.dev != dev)
176 		return 0;
177 
178 	switch (action) {
179 	case BUS_NOTIFY_BOUND_DRIVER: /* mei driver bound to aux device */
180 		gsc_init_done(huc);
181 		break;
182 
183 	case BUS_NOTIFY_DRIVER_NOT_BOUND: /* mei driver fails to be bound */
184 	case BUS_NOTIFY_UNBIND_DRIVER: /* mei driver about to be unbound */
185 		huc_info(huc, "MEI driver not bound, disabling load\n");
186 		gsc_init_error(huc);
187 		break;
188 	}
189 
190 	return 0;
191 }
192 
193 void intel_huc_register_gsc_notifier(struct intel_huc *huc, struct bus_type *bus)
194 {
195 	int ret;
196 
197 	if (!intel_huc_is_loaded_by_gsc(huc))
198 		return;
199 
200 	huc->delayed_load.nb.notifier_call = gsc_notifier;
201 	ret = bus_register_notifier(bus, &huc->delayed_load.nb);
202 	if (ret) {
203 		huc_err(huc, "failed to register GSC notifier %pe\n", ERR_PTR(ret));
204 		huc->delayed_load.nb.notifier_call = NULL;
205 		gsc_init_error(huc);
206 	}
207 }
208 
209 void intel_huc_unregister_gsc_notifier(struct intel_huc *huc, struct bus_type *bus)
210 {
211 	if (!huc->delayed_load.nb.notifier_call)
212 		return;
213 
214 	delayed_huc_load_complete(huc);
215 
216 	bus_unregister_notifier(bus, &huc->delayed_load.nb);
217 	huc->delayed_load.nb.notifier_call = NULL;
218 }
219 
220 static void delayed_huc_load_init(struct intel_huc *huc)
221 {
222 	/*
223 	 * Initialize fence to be complete as this is expected to be complete
224 	 * unless there is a delayed HuC load in progress.
225 	 */
226 	i915_sw_fence_init(&huc->delayed_load.fence,
227 			   sw_fence_dummy_notify);
228 	i915_sw_fence_commit(&huc->delayed_load.fence);
229 
230 	hrtimer_init(&huc->delayed_load.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
231 	huc->delayed_load.timer.function = huc_delayed_load_timer_callback;
232 }
233 
234 static void delayed_huc_load_fini(struct intel_huc *huc)
235 {
236 	/*
237 	 * the fence is initialized in init_early, so we need to clean it up
238 	 * even if HuC loading is off.
239 	 */
240 	delayed_huc_load_complete(huc);
241 	i915_sw_fence_fini(&huc->delayed_load.fence);
242 }
243 
244 static bool vcs_supported(struct intel_gt *gt)
245 {
246 	intel_engine_mask_t mask = gt->info.engine_mask;
247 
248 	/*
249 	 * We reach here from i915_driver_early_probe for the primary GT before
250 	 * its engine mask is set, so we use the device info engine mask for it;
251 	 * this means we're not taking VCS fusing into account, but if the
252 	 * primary GT supports VCS engines we expect at least one of them to
253 	 * remain unfused so we're fine.
254 	 * For other GTs we expect the GT-specific mask to be set before we
255 	 * call this function.
256 	 */
257 	GEM_BUG_ON(!gt_is_root(gt) && !gt->info.engine_mask);
258 
259 	if (gt_is_root(gt))
260 		mask = RUNTIME_INFO(gt->i915)->platform_engine_mask;
261 	else
262 		mask = gt->info.engine_mask;
263 
264 	return __ENGINE_INSTANCES_MASK(mask, VCS0, I915_MAX_VCS);
265 }
266 
267 void intel_huc_init_early(struct intel_huc *huc)
268 {
269 	struct drm_i915_private *i915 = huc_to_gt(huc)->i915;
270 	struct intel_gt *gt = huc_to_gt(huc);
271 
272 	intel_uc_fw_init_early(&huc->fw, INTEL_UC_FW_TYPE_HUC);
273 
274 	/*
275 	 * we always init the fence as already completed, even if HuC is not
276 	 * supported. This way we don't have to distinguish between HuC not
277 	 * supported/disabled or already loaded, and can focus on if the load
278 	 * is currently in progress (fence not complete) or not, which is what
279 	 * we care about for stalling userspace submissions.
280 	 */
281 	delayed_huc_load_init(huc);
282 
283 	if (!vcs_supported(gt)) {
284 		intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_NOT_SUPPORTED);
285 		return;
286 	}
287 
288 	if (GRAPHICS_VER(i915) >= 11) {
289 		huc->status.reg = GEN11_HUC_KERNEL_LOAD_INFO;
290 		huc->status.mask = HUC_LOAD_SUCCESSFUL;
291 		huc->status.value = HUC_LOAD_SUCCESSFUL;
292 	} else {
293 		huc->status.reg = HUC_STATUS2;
294 		huc->status.mask = HUC_FW_VERIFIED;
295 		huc->status.value = HUC_FW_VERIFIED;
296 	}
297 }
298 
299 #define HUC_LOAD_MODE_STRING(x) (x ? "GSC" : "legacy")
300 static int check_huc_loading_mode(struct intel_huc *huc)
301 {
302 	struct intel_gt *gt = huc_to_gt(huc);
303 	bool fw_needs_gsc = intel_huc_is_loaded_by_gsc(huc);
304 	bool hw_uses_gsc = false;
305 
306 	/*
307 	 * The fuse for HuC load via GSC is only valid on platforms that have
308 	 * GuC deprivilege.
309 	 */
310 	if (HAS_GUC_DEPRIVILEGE(gt->i915))
311 		hw_uses_gsc = intel_uncore_read(gt->uncore, GUC_SHIM_CONTROL2) &
312 			      GSC_LOADS_HUC;
313 
314 	if (fw_needs_gsc != hw_uses_gsc) {
315 		huc_err(huc, "mismatch between FW (%s) and HW (%s) load modes\n",
316 			HUC_LOAD_MODE_STRING(fw_needs_gsc), HUC_LOAD_MODE_STRING(hw_uses_gsc));
317 		return -ENOEXEC;
318 	}
319 
320 	/* make sure we can access the GSC via the mei driver if we need it */
321 	if (!(IS_ENABLED(CONFIG_INTEL_MEI_PXP) && IS_ENABLED(CONFIG_INTEL_MEI_GSC)) &&
322 	    fw_needs_gsc) {
323 		huc_info(huc, "can't load due to missing MEI modules\n");
324 		return -EIO;
325 	}
326 
327 	huc_dbg(huc, "loaded by GSC = %s\n", str_yes_no(fw_needs_gsc));
328 
329 	return 0;
330 }
331 
332 int intel_huc_init(struct intel_huc *huc)
333 {
334 	int err;
335 
336 	err = check_huc_loading_mode(huc);
337 	if (err)
338 		goto out;
339 
340 	err = intel_uc_fw_init(&huc->fw);
341 	if (err)
342 		goto out;
343 
344 	intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_LOADABLE);
345 
346 	return 0;
347 
348 out:
349 	intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_INIT_FAIL);
350 	huc_info(huc, "initialization failed %pe\n", ERR_PTR(err));
351 	return err;
352 }
353 
354 void intel_huc_fini(struct intel_huc *huc)
355 {
356 	/*
357 	 * the fence is initialized in init_early, so we need to clean it up
358 	 * even if HuC loading is off.
359 	 */
360 	delayed_huc_load_fini(huc);
361 
362 	if (intel_uc_fw_is_loadable(&huc->fw))
363 		intel_uc_fw_fini(&huc->fw);
364 }
365 
366 void intel_huc_suspend(struct intel_huc *huc)
367 {
368 	if (!intel_uc_fw_is_loadable(&huc->fw))
369 		return;
370 
371 	/*
372 	 * in the unlikely case that we're suspending before the GSC has
373 	 * completed its loading sequence, just stop waiting. We'll restart
374 	 * on resume.
375 	 */
376 	delayed_huc_load_complete(huc);
377 }
378 
379 int intel_huc_wait_for_auth_complete(struct intel_huc *huc)
380 {
381 	struct intel_gt *gt = huc_to_gt(huc);
382 	int ret;
383 
384 	ret = __intel_wait_for_register(gt->uncore,
385 					huc->status.reg,
386 					huc->status.mask,
387 					huc->status.value,
388 					2, 50, NULL);
389 
390 	/* mark the load process as complete even if the wait failed */
391 	delayed_huc_load_complete(huc);
392 
393 	if (ret) {
394 		huc_err(huc, "firmware not verified %pe\n", ERR_PTR(ret));
395 		intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_LOAD_FAIL);
396 		return ret;
397 	}
398 
399 	intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_RUNNING);
400 	huc_info(huc, "authenticated!\n");
401 	return 0;
402 }
403 
404 /**
405  * intel_huc_auth() - Authenticate HuC uCode
406  * @huc: intel_huc structure
407  *
408  * Called after HuC and GuC firmware loading during intel_uc_init_hw().
409  *
410  * This function invokes the GuC action to authenticate the HuC firmware,
411  * passing the offset of the RSA signature to intel_guc_auth_huc(). It then
412  * waits for up to 50ms for firmware verification ACK.
413  */
414 int intel_huc_auth(struct intel_huc *huc)
415 {
416 	struct intel_gt *gt = huc_to_gt(huc);
417 	struct intel_guc *guc = &gt->uc.guc;
418 	int ret;
419 
420 	if (!intel_uc_fw_is_loaded(&huc->fw))
421 		return -ENOEXEC;
422 
423 	/* GSC will do the auth */
424 	if (intel_huc_is_loaded_by_gsc(huc))
425 		return -ENODEV;
426 
427 	ret = i915_inject_probe_error(gt->i915, -ENXIO);
428 	if (ret)
429 		goto fail;
430 
431 	GEM_BUG_ON(intel_uc_fw_is_running(&huc->fw));
432 
433 	ret = intel_guc_auth_huc(guc, intel_guc_ggtt_offset(guc, huc->fw.rsa_data));
434 	if (ret) {
435 		huc_err(huc, "authentication by GuC failed %pe\n", ERR_PTR(ret));
436 		goto fail;
437 	}
438 
439 	/* Check authentication status, it should be done by now */
440 	ret = intel_huc_wait_for_auth_complete(huc);
441 	if (ret)
442 		goto fail;
443 
444 	return 0;
445 
446 fail:
447 	huc_probe_error(huc, "authentication failed %pe\n", ERR_PTR(ret));
448 	return ret;
449 }
450 
451 bool intel_huc_is_authenticated(struct intel_huc *huc)
452 {
453 	struct intel_gt *gt = huc_to_gt(huc);
454 	intel_wakeref_t wakeref;
455 	u32 status = 0;
456 
457 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
458 		status = intel_uncore_read(gt->uncore, huc->status.reg);
459 
460 	return (status & huc->status.mask) == huc->status.value;
461 }
462 
463 /**
464  * intel_huc_check_status() - check HuC status
465  * @huc: intel_huc structure
466  *
467  * This function reads status register to verify if HuC
468  * firmware was successfully loaded.
469  *
470  * The return values match what is expected for the I915_PARAM_HUC_STATUS
471  * getparam.
472  */
473 int intel_huc_check_status(struct intel_huc *huc)
474 {
475 	switch (__intel_uc_fw_status(&huc->fw)) {
476 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
477 		return -ENODEV;
478 	case INTEL_UC_FIRMWARE_DISABLED:
479 		return -EOPNOTSUPP;
480 	case INTEL_UC_FIRMWARE_MISSING:
481 		return -ENOPKG;
482 	case INTEL_UC_FIRMWARE_ERROR:
483 		return -ENOEXEC;
484 	case INTEL_UC_FIRMWARE_INIT_FAIL:
485 		return -ENOMEM;
486 	case INTEL_UC_FIRMWARE_LOAD_FAIL:
487 		return -EIO;
488 	default:
489 		break;
490 	}
491 
492 	return intel_huc_is_authenticated(huc);
493 }
494 
495 static bool huc_has_delayed_load(struct intel_huc *huc)
496 {
497 	return intel_huc_is_loaded_by_gsc(huc) &&
498 	       (huc->delayed_load.status != INTEL_HUC_DELAYED_LOAD_ERROR);
499 }
500 
501 void intel_huc_update_auth_status(struct intel_huc *huc)
502 {
503 	if (!intel_uc_fw_is_loadable(&huc->fw))
504 		return;
505 
506 	if (intel_huc_is_authenticated(huc))
507 		intel_uc_fw_change_status(&huc->fw,
508 					  INTEL_UC_FIRMWARE_RUNNING);
509 	else if (huc_has_delayed_load(huc))
510 		huc_delayed_load_start(huc);
511 }
512 
513 /**
514  * intel_huc_load_status - dump information about HuC load status
515  * @huc: the HuC
516  * @p: the &drm_printer
517  *
518  * Pretty printer for HuC load status.
519  */
520 void intel_huc_load_status(struct intel_huc *huc, struct drm_printer *p)
521 {
522 	struct intel_gt *gt = huc_to_gt(huc);
523 	intel_wakeref_t wakeref;
524 
525 	if (!intel_huc_is_supported(huc)) {
526 		drm_printf(p, "HuC not supported\n");
527 		return;
528 	}
529 
530 	if (!intel_huc_is_wanted(huc)) {
531 		drm_printf(p, "HuC disabled\n");
532 		return;
533 	}
534 
535 	intel_uc_fw_dump(&huc->fw, p);
536 
537 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
538 		drm_printf(p, "HuC status: 0x%08x\n",
539 			   intel_uncore_read(gt->uncore, huc->status.reg));
540 }
541