1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
6 #include <linux/types.h>
8 #include "gt/intel_gt.h"
9 #include "gt/intel_gt_print.h"
10 #include "intel_gsc_fw.h"
11 #include "intel_gsc_proxy.h"
12 #include "intel_gsc_uc.h"
16 static void gsc_work(struct work_struct *work)
18 struct intel_gsc_uc *gsc = container_of(work, typeof(*gsc), work);
19 struct intel_gt *gt = gsc_uc_to_gt(gsc);
20 intel_wakeref_t wakeref;
24 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
26 spin_lock_irq(gt->irq_lock);
27 actions = gsc->gsc_work_actions;
28 gsc->gsc_work_actions = 0;
29 spin_unlock_irq(gt->irq_lock);
31 if (actions & GSC_ACTION_FW_LOAD) {
32 ret = intel_gsc_uc_fw_upload(gsc);
34 /* setup proxy on a new load */
35 actions |= GSC_ACTION_SW_PROXY;
36 else if (ret != -EEXIST)
40 * The HuC auth can be done both before or after the proxy init;
41 * if done after, a proxy request will be issued and must be
42 * serviced before the authentication can complete.
43 * Since this worker also handles proxy requests, we can't
44 * perform an action that requires the proxy from within it and
45 * then stall waiting for it, because we'd be blocking the
46 * service path. Therefore, it is easier for us to load HuC
47 * first and do proxy later. The GSC will ack the HuC auth and
48 * then send the HuC proxy request as part of the proxy init
50 * Note that we can only do the GSC auth if the GuC auth was
53 if (intel_uc_uses_huc(>->uc) &&
54 intel_huc_is_authenticated(>->uc.huc, INTEL_HUC_AUTH_BY_GUC))
55 intel_huc_auth(>->uc.huc, INTEL_HUC_AUTH_BY_GSC);
58 if (actions & GSC_ACTION_SW_PROXY) {
59 if (!intel_gsc_uc_fw_init_done(gsc)) {
60 gt_err(gt, "Proxy request received with GSC not loaded!\n");
64 ret = intel_gsc_proxy_request_handler(gsc);
66 if (actions & GSC_ACTION_FW_LOAD) {
68 * A proxy failure right after firmware load means the proxy-init
69 * step has failed so mark GSC as not usable after this
71 gt_err(gt, "GSC proxy handler failed to init\n");
72 intel_uc_fw_change_status(&gsc->fw, INTEL_UC_FIRMWARE_LOAD_FAIL);
77 /* mark the GSC FW init as done the first time we run this */
78 if (actions & GSC_ACTION_FW_LOAD) {
80 * If there is a proxy establishment error, the GSC might still
81 * complete the request handling cleanly, so we need to check the
82 * status register to check if the proxy init was actually successful
84 if (intel_gsc_uc_fw_proxy_init_done(gsc, false)) {
85 gt_dbg(gt, "GSC Proxy initialized\n");
86 intel_uc_fw_change_status(&gsc->fw, INTEL_UC_FIRMWARE_RUNNING);
88 gt_err(gt, "GSC status reports proxy init not complete\n");
89 intel_uc_fw_change_status(&gsc->fw, INTEL_UC_FIRMWARE_LOAD_FAIL);
95 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
98 static bool gsc_engine_supported(struct intel_gt *gt)
100 intel_engine_mask_t mask;
103 * We reach here from i915_driver_early_probe for the primary GT before
104 * its engine mask is set, so we use the device info engine mask for it.
105 * For other GTs we expect the GT-specific mask to be set before we
106 * call this function.
108 GEM_BUG_ON(!gt_is_root(gt) && !gt->info.engine_mask);
111 mask = INTEL_INFO(gt->i915)->platform_engine_mask;
113 mask = gt->info.engine_mask;
115 return __HAS_ENGINE(mask, GSC0);
118 void intel_gsc_uc_init_early(struct intel_gsc_uc *gsc)
120 struct intel_gt *gt = gsc_uc_to_gt(gsc);
123 * GSC FW needs to be copied to a dedicated memory allocations for
124 * loading (see gsc->local), so we don't need to GGTT map the FW image
127 intel_uc_fw_init_early(&gsc->fw, INTEL_UC_FW_TYPE_GSC, false);
128 INIT_WORK(&gsc->work, gsc_work);
130 /* we can arrive here from i915_driver_early_probe for primary
131 * GT with it being not fully setup hence check device info's
134 if (!gsc_engine_supported(gt)) {
135 intel_uc_fw_change_status(&gsc->fw, INTEL_UC_FIRMWARE_NOT_SUPPORTED);
139 gsc->wq = alloc_ordered_workqueue("i915_gsc", 0);
141 gt_err(gt, "failed to allocate WQ for GSC, disabling FW\n");
142 intel_uc_fw_change_status(&gsc->fw, INTEL_UC_FIRMWARE_NOT_SUPPORTED);
146 static int gsc_allocate_and_map_vma(struct intel_gsc_uc *gsc, u32 size)
148 struct intel_gt *gt = gsc_uc_to_gt(gsc);
149 struct drm_i915_gem_object *obj;
150 struct i915_vma *vma;
155 * The GSC FW doesn't immediately suspend after becoming idle, so there
156 * is a chance that it could still be awake after we successfully
157 * return from the pci suspend function, even if there are no pending
159 * The FW might therefore try to access memory for its suspend operation
160 * after the kernel has completed the HW suspend flow; this can cause
161 * issues if the FW is mapped in normal RAM memory, as some of the
162 * involved HW units might've already lost power.
163 * The driver must therefore avoid this situation and the recommended
164 * way to do so is to use stolen memory for the GSC memory allocation,
165 * because stolen memory takes a different path in HW and it is
166 * guaranteed to always work as long as the GPU itself is awake (which
167 * it must be if the GSC is awake).
169 obj = i915_gem_object_create_stolen(gt->i915, size);
173 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
179 vaddr = i915_vma_pin_iomap(vma);
182 ret = PTR_ERR(vaddr);
186 i915_vma_make_unshrinkable(vma);
189 gsc->local_vaddr = vaddr;
194 i915_gem_object_put(obj);
198 static void gsc_unmap_and_free_vma(struct intel_gsc_uc *gsc)
200 struct i915_vma *vma = fetch_and_zero(&gsc->local);
205 gsc->local_vaddr = NULL;
206 i915_vma_unpin_iomap(vma);
207 i915_gem_object_put(vma->obj);
210 int intel_gsc_uc_init(struct intel_gsc_uc *gsc)
212 static struct lock_class_key gsc_lock;
213 struct intel_gt *gt = gsc_uc_to_gt(gsc);
214 struct intel_engine_cs *engine = gt->engine[GSC0];
215 struct intel_context *ce;
218 err = intel_uc_fw_init(&gsc->fw);
222 err = gsc_allocate_and_map_vma(gsc, SZ_4M);
226 ce = intel_engine_create_pinned_context(engine, engine->gt->vm, SZ_4K,
227 I915_GEM_HWS_GSC_ADDR,
228 &gsc_lock, "gsc_context");
230 gt_err(gt, "failed to create GSC CS ctx for FW communication\n");
237 /* if we fail to init proxy we still want to load GSC for PM */
238 intel_gsc_proxy_init(gsc);
240 intel_uc_fw_change_status(&gsc->fw, INTEL_UC_FIRMWARE_LOADABLE);
245 gsc_unmap_and_free_vma(gsc);
247 intel_uc_fw_fini(&gsc->fw);
249 gt_probe_error(gt, "GSC init failed %pe\n", ERR_PTR(err));
253 void intel_gsc_uc_fini(struct intel_gsc_uc *gsc)
255 if (!intel_uc_fw_is_loadable(&gsc->fw))
258 flush_work(&gsc->work);
260 destroy_workqueue(gsc->wq);
264 intel_gsc_proxy_fini(gsc);
267 intel_engine_destroy_pinned_context(fetch_and_zero(&gsc->ce));
269 gsc_unmap_and_free_vma(gsc);
271 intel_uc_fw_fini(&gsc->fw);
274 void intel_gsc_uc_flush_work(struct intel_gsc_uc *gsc)
276 if (!intel_uc_fw_is_loadable(&gsc->fw))
279 flush_work(&gsc->work);
282 void intel_gsc_uc_resume(struct intel_gsc_uc *gsc)
284 if (!intel_uc_fw_is_loadable(&gsc->fw))
288 * we only want to start the GSC worker from here in the actual resume
289 * flow and not during driver load. This is because GSC load is slow and
290 * therefore we want to make sure that the default state init completes
291 * first to not slow down the init thread. A separate call to
292 * intel_gsc_uc_load_start will ensure that the GSC is loaded during
295 if (!gsc_uc_to_gt(gsc)->engine[GSC0]->default_state)
298 intel_gsc_uc_load_start(gsc);
301 void intel_gsc_uc_load_start(struct intel_gsc_uc *gsc)
303 struct intel_gt *gt = gsc_uc_to_gt(gsc);
305 if (!intel_uc_fw_is_loadable(&gsc->fw))
308 if (intel_gsc_uc_fw_init_done(gsc))
311 spin_lock_irq(gt->irq_lock);
312 gsc->gsc_work_actions |= GSC_ACTION_FW_LOAD;
313 spin_unlock_irq(gt->irq_lock);
315 queue_work(gsc->wq, &gsc->work);
318 void intel_gsc_uc_load_status(struct intel_gsc_uc *gsc, struct drm_printer *p)
320 struct intel_gt *gt = gsc_uc_to_gt(gsc);
321 struct intel_uncore *uncore = gt->uncore;
322 intel_wakeref_t wakeref;
324 if (!intel_gsc_uc_is_supported(gsc)) {
325 drm_printf(p, "GSC not supported\n");
329 if (!intel_gsc_uc_is_wanted(gsc)) {
330 drm_printf(p, "GSC disabled\n");
334 drm_printf(p, "GSC firmware: %s\n", gsc->fw.file_selected.path);
335 if (gsc->fw.file_selected.path != gsc->fw.file_wanted.path)
336 drm_printf(p, "GSC firmware wanted: %s\n", gsc->fw.file_wanted.path);
337 drm_printf(p, "\tstatus: %s\n", intel_uc_fw_status_repr(gsc->fw.status));
339 drm_printf(p, "Release: %u.%u.%u.%u\n",
340 gsc->release.major, gsc->release.minor,
341 gsc->release.patch, gsc->release.build);
343 drm_printf(p, "Compatibility Version: %u.%u [min expected %u.%u]\n",
344 gsc->fw.file_selected.ver.major, gsc->fw.file_selected.ver.minor,
345 gsc->fw.file_wanted.ver.major, gsc->fw.file_wanted.ver.minor);
347 drm_printf(p, "SVN: %u\n", gsc->security_version);
349 with_intel_runtime_pm(uncore->rpm, wakeref) {
352 for (i = 1; i <= 6; i++) {
353 u32 status = intel_uncore_read(uncore,
354 HECI_FWSTS(MTL_GSC_HECI1_BASE, i));
355 drm_printf(p, "HECI1 FWSTST%u = 0x%08x\n", i, status);