1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "gem/i915_gem_lmem.h"
7 #include "gt/intel_engine_pm.h"
8 #include "gt/intel_gpu_commands.h"
9 #include "gt/intel_gt.h"
10 #include "gt/intel_gt_print.h"
11 #include "gt/intel_ring.h"
12 #include "intel_gsc_binary_headers.h"
13 #include "intel_gsc_fw.h"
14 #include "intel_gsc_uc_heci_cmd_submit.h"
15 #include "i915_reg.h"
16 
gsc_is_in_reset(struct intel_uncore * uncore)17 static bool gsc_is_in_reset(struct intel_uncore *uncore)
18 {
19 	u32 fw_status = intel_uncore_read(uncore, HECI_FWSTS(MTL_GSC_HECI1_BASE, 1));
20 
21 	return REG_FIELD_GET(HECI1_FWSTS1_CURRENT_STATE, fw_status) ==
22 			HECI1_FWSTS1_CURRENT_STATE_RESET;
23 }
24 
gsc_uc_get_fw_status(struct intel_uncore * uncore,bool needs_wakeref)25 static u32 gsc_uc_get_fw_status(struct intel_uncore *uncore, bool needs_wakeref)
26 {
27 	intel_wakeref_t wakeref;
28 	u32 fw_status = 0;
29 
30 	if (needs_wakeref)
31 		wakeref = intel_runtime_pm_get(uncore->rpm);
32 
33 	fw_status = intel_uncore_read(uncore, HECI_FWSTS(MTL_GSC_HECI1_BASE, 1));
34 
35 	if (needs_wakeref)
36 		intel_runtime_pm_put(uncore->rpm, wakeref);
37 	return fw_status;
38 }
39 
intel_gsc_uc_fw_proxy_init_done(struct intel_gsc_uc * gsc,bool needs_wakeref)40 bool intel_gsc_uc_fw_proxy_init_done(struct intel_gsc_uc *gsc, bool needs_wakeref)
41 {
42 	return REG_FIELD_GET(HECI1_FWSTS1_CURRENT_STATE,
43 			     gsc_uc_get_fw_status(gsc_uc_to_gt(gsc)->uncore,
44 						  needs_wakeref)) ==
45 	       HECI1_FWSTS1_PROXY_STATE_NORMAL;
46 }
47 
intel_gsc_uc_fw_proxy_get_status(struct intel_gsc_uc * gsc)48 int intel_gsc_uc_fw_proxy_get_status(struct intel_gsc_uc *gsc)
49 {
50 	if (!(IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY)))
51 		return -ENODEV;
52 	if (!intel_uc_fw_is_loadable(&gsc->fw))
53 		return -ENODEV;
54 	if (__intel_uc_fw_status(&gsc->fw) == INTEL_UC_FIRMWARE_LOAD_FAIL)
55 		return -ENOLINK;
56 	if (!intel_gsc_uc_fw_proxy_init_done(gsc, true))
57 		return -EAGAIN;
58 
59 	return 0;
60 }
61 
intel_gsc_uc_fw_init_done(struct intel_gsc_uc * gsc)62 bool intel_gsc_uc_fw_init_done(struct intel_gsc_uc *gsc)
63 {
64 	return gsc_uc_get_fw_status(gsc_uc_to_gt(gsc)->uncore, false) &
65 	       HECI1_FWSTS1_INIT_COMPLETE;
66 }
67 
cpd_entry_offset(const struct intel_gsc_cpd_entry * entry)68 static inline u32 cpd_entry_offset(const struct intel_gsc_cpd_entry *entry)
69 {
70 	return entry->offset & INTEL_GSC_CPD_ENTRY_OFFSET_MASK;
71 }
72 
intel_gsc_fw_get_binary_info(struct intel_uc_fw * gsc_fw,const void * data,size_t size)73 int intel_gsc_fw_get_binary_info(struct intel_uc_fw *gsc_fw, const void *data, size_t size)
74 {
75 	struct intel_gsc_uc *gsc = container_of(gsc_fw, struct intel_gsc_uc, fw);
76 	struct intel_gt *gt = gsc_uc_to_gt(gsc);
77 	const struct intel_gsc_layout_pointers *layout = data;
78 	const struct intel_gsc_bpdt_header *bpdt_header = NULL;
79 	const struct intel_gsc_bpdt_entry *bpdt_entry = NULL;
80 	const struct intel_gsc_cpd_header_v2 *cpd_header = NULL;
81 	const struct intel_gsc_cpd_entry *cpd_entry = NULL;
82 	const struct intel_gsc_manifest_header *manifest;
83 	size_t min_size = sizeof(*layout);
84 	int i;
85 
86 	if (size < min_size) {
87 		gt_err(gt, "GSC FW too small! %zu < %zu\n", size, min_size);
88 		return -ENODATA;
89 	}
90 
91 	/*
92 	 * The GSC binary starts with the pointer layout, which contains the
93 	 * locations of the various partitions of the binary. The one we're
94 	 * interested in to get the version is the boot1 partition, where we can
95 	 * find a BPDT header followed by entries, one of which points to the
96 	 * RBE sub-section of the partition. From here, we can parse the CPD
97 	 * header and the following entries to find the manifest location
98 	 * (entry identified by the "RBEP.man" name), from which we can finally
99 	 * extract the version.
100 	 *
101 	 * --------------------------------------------------
102 	 * [  intel_gsc_layout_pointers                     ]
103 	 * [      ...                                       ]
104 	 * [      boot1.offset  >---------------------------]------o
105 	 * [      ...                                       ]      |
106 	 * --------------------------------------------------      |
107 	 *                                                         |
108 	 * --------------------------------------------------      |
109 	 * [  intel_gsc_bpdt_header                         ]<-----o
110 	 * --------------------------------------------------
111 	 * [  intel_gsc_bpdt_entry[]                        ]
112 	 * [      entry1                                    ]
113 	 * [      ...                                       ]
114 	 * [      entryX                                    ]
115 	 * [          type == GSC_RBE                       ]
116 	 * [          offset  >-----------------------------]------o
117 	 * [      ...                                       ]      |
118 	 * --------------------------------------------------      |
119 	 *                                                         |
120 	 * --------------------------------------------------      |
121 	 * [  intel_gsc_cpd_header_v2                       ]<-----o
122 	 * --------------------------------------------------
123 	 * [  intel_gsc_cpd_entry[]                         ]
124 	 * [      entry1                                    ]
125 	 * [      ...                                       ]
126 	 * [      entryX                                    ]
127 	 * [          "RBEP.man"                            ]
128 	 * [           ...                                  ]
129 	 * [           offset  >----------------------------]------o
130 	 * [      ...                                       ]      |
131 	 * --------------------------------------------------      |
132 	 *                                                         |
133 	 * --------------------------------------------------      |
134 	 * [ intel_gsc_manifest_header                      ]<-----o
135 	 * [  ...                                           ]
136 	 * [  intel_gsc_version     fw_version              ]
137 	 * [  ...                                           ]
138 	 * --------------------------------------------------
139 	 */
140 
141 	min_size = layout->boot1.offset + layout->boot1.size;
142 	if (size < min_size) {
143 		gt_err(gt, "GSC FW too small for boot section! %zu < %zu\n",
144 		       size, min_size);
145 		return -ENODATA;
146 	}
147 
148 	min_size = sizeof(*bpdt_header);
149 	if (layout->boot1.size < min_size) {
150 		gt_err(gt, "GSC FW boot section too small for BPDT header: %u < %zu\n",
151 		       layout->boot1.size, min_size);
152 		return -ENODATA;
153 	}
154 
155 	bpdt_header = data + layout->boot1.offset;
156 	if (bpdt_header->signature != INTEL_GSC_BPDT_HEADER_SIGNATURE) {
157 		gt_err(gt, "invalid signature for BPDT header: 0x%08x!\n",
158 		       bpdt_header->signature);
159 		return -EINVAL;
160 	}
161 
162 	min_size += sizeof(*bpdt_entry) * bpdt_header->descriptor_count;
163 	if (layout->boot1.size < min_size) {
164 		gt_err(gt, "GSC FW boot section too small for BPDT entries: %u < %zu\n",
165 		       layout->boot1.size, min_size);
166 		return -ENODATA;
167 	}
168 
169 	bpdt_entry = (void *)bpdt_header + sizeof(*bpdt_header);
170 	for (i = 0; i < bpdt_header->descriptor_count; i++, bpdt_entry++) {
171 		if ((bpdt_entry->type & INTEL_GSC_BPDT_ENTRY_TYPE_MASK) !=
172 		    INTEL_GSC_BPDT_ENTRY_TYPE_GSC_RBE)
173 			continue;
174 
175 		cpd_header = (void *)bpdt_header + bpdt_entry->sub_partition_offset;
176 		min_size = bpdt_entry->sub_partition_offset + sizeof(*cpd_header);
177 		break;
178 	}
179 
180 	if (!cpd_header) {
181 		gt_err(gt, "couldn't find CPD header in GSC binary!\n");
182 		return -ENODATA;
183 	}
184 
185 	if (layout->boot1.size < min_size) {
186 		gt_err(gt, "GSC FW boot section too small for CPD header: %u < %zu\n",
187 		       layout->boot1.size, min_size);
188 		return -ENODATA;
189 	}
190 
191 	if (cpd_header->header_marker != INTEL_GSC_CPD_HEADER_MARKER) {
192 		gt_err(gt, "invalid marker for CPD header in GSC bin: 0x%08x!\n",
193 		       cpd_header->header_marker);
194 		return -EINVAL;
195 	}
196 
197 	min_size += sizeof(*cpd_entry) * cpd_header->num_of_entries;
198 	if (layout->boot1.size < min_size) {
199 		gt_err(gt, "GSC FW boot section too small for CPD entries: %u < %zu\n",
200 		       layout->boot1.size, min_size);
201 		return -ENODATA;
202 	}
203 
204 	cpd_entry = (void *)cpd_header + cpd_header->header_length;
205 	for (i = 0; i < cpd_header->num_of_entries; i++, cpd_entry++) {
206 		if (strcmp(cpd_entry->name, "RBEP.man") == 0) {
207 			manifest = (void *)cpd_header + cpd_entry_offset(cpd_entry);
208 			intel_uc_fw_version_from_gsc_manifest(&gsc->release,
209 							      manifest);
210 			gsc->security_version = manifest->security_version;
211 			break;
212 		}
213 	}
214 
215 	return 0;
216 }
217 
emit_gsc_fw_load(struct i915_request * rq,struct intel_gsc_uc * gsc)218 static int emit_gsc_fw_load(struct i915_request *rq, struct intel_gsc_uc *gsc)
219 {
220 	u32 offset = i915_ggtt_offset(gsc->local);
221 	u32 *cs;
222 
223 	cs = intel_ring_begin(rq, 4);
224 	if (IS_ERR(cs))
225 		return PTR_ERR(cs);
226 
227 	*cs++ = GSC_FW_LOAD;
228 	*cs++ = lower_32_bits(offset);
229 	*cs++ = upper_32_bits(offset);
230 	*cs++ = (gsc->local->size / SZ_4K) | HECI1_FW_LIMIT_VALID;
231 
232 	intel_ring_advance(rq, cs);
233 
234 	return 0;
235 }
236 
gsc_fw_load(struct intel_gsc_uc * gsc)237 static int gsc_fw_load(struct intel_gsc_uc *gsc)
238 {
239 	struct intel_context *ce = gsc->ce;
240 	struct i915_request *rq;
241 	int err;
242 
243 	if (!ce)
244 		return -ENODEV;
245 
246 	rq = i915_request_create(ce);
247 	if (IS_ERR(rq))
248 		return PTR_ERR(rq);
249 
250 	if (ce->engine->emit_init_breadcrumb) {
251 		err = ce->engine->emit_init_breadcrumb(rq);
252 		if (err)
253 			goto out_rq;
254 	}
255 
256 	err = emit_gsc_fw_load(rq, gsc);
257 	if (err)
258 		goto out_rq;
259 
260 	err = ce->engine->emit_flush(rq, 0);
261 
262 out_rq:
263 	i915_request_get(rq);
264 
265 	if (unlikely(err))
266 		i915_request_set_error_once(rq, err);
267 
268 	i915_request_add(rq);
269 
270 	if (!err && i915_request_wait(rq, 0, msecs_to_jiffies(500)) < 0)
271 		err = -ETIME;
272 
273 	i915_request_put(rq);
274 
275 	if (err)
276 		gt_err(gsc_uc_to_gt(gsc), "Request submission for GSC load failed %pe\n",
277 		       ERR_PTR(err));
278 
279 	return err;
280 }
281 
gsc_fw_load_prepare(struct intel_gsc_uc * gsc)282 static int gsc_fw_load_prepare(struct intel_gsc_uc *gsc)
283 {
284 	struct intel_gt *gt = gsc_uc_to_gt(gsc);
285 	void *src;
286 
287 	if (!gsc->local)
288 		return -ENODEV;
289 
290 	if (gsc->local->size < gsc->fw.size)
291 		return -ENOSPC;
292 
293 	src = i915_gem_object_pin_map_unlocked(gsc->fw.obj,
294 					       intel_gt_coherent_map_type(gt, gsc->fw.obj, true));
295 	if (IS_ERR(src))
296 		return PTR_ERR(src);
297 
298 	memcpy_toio(gsc->local_vaddr, src, gsc->fw.size);
299 	memset_io(gsc->local_vaddr + gsc->fw.size, 0, gsc->local->size - gsc->fw.size);
300 
301 	intel_guc_write_barrier(&gt->uc.guc);
302 
303 	i915_gem_object_unpin_map(gsc->fw.obj);
304 
305 	return 0;
306 }
307 
gsc_fw_wait(struct intel_gt * gt)308 static int gsc_fw_wait(struct intel_gt *gt)
309 {
310 	return intel_wait_for_register(gt->uncore,
311 				       HECI_FWSTS(MTL_GSC_HECI1_BASE, 1),
312 				       HECI1_FWSTS1_INIT_COMPLETE,
313 				       HECI1_FWSTS1_INIT_COMPLETE,
314 				       500);
315 }
316 
317 struct intel_gsc_mkhi_header {
318 	u8  group_id;
319 #define MKHI_GROUP_ID_GFX_SRV 0x30
320 
321 	u8  command;
322 #define MKHI_GFX_SRV_GET_HOST_COMPATIBILITY_VERSION (0x42)
323 
324 	u8  reserved;
325 	u8  result;
326 } __packed;
327 
328 struct mtl_gsc_ver_msg_in {
329 	struct intel_gsc_mtl_header header;
330 	struct intel_gsc_mkhi_header mkhi;
331 } __packed;
332 
333 struct mtl_gsc_ver_msg_out {
334 	struct intel_gsc_mtl_header header;
335 	struct intel_gsc_mkhi_header mkhi;
336 	u16 proj_major;
337 	u16 compat_major;
338 	u16 compat_minor;
339 	u16 reserved[5];
340 } __packed;
341 
342 #define GSC_VER_PKT_SZ SZ_4K
343 
gsc_fw_query_compatibility_version(struct intel_gsc_uc * gsc)344 static int gsc_fw_query_compatibility_version(struct intel_gsc_uc *gsc)
345 {
346 	struct intel_gt *gt = gsc_uc_to_gt(gsc);
347 	struct mtl_gsc_ver_msg_in *msg_in;
348 	struct mtl_gsc_ver_msg_out *msg_out;
349 	struct i915_vma *vma;
350 	u64 offset;
351 	void *vaddr;
352 	int err;
353 
354 	err = intel_guc_allocate_and_map_vma(&gt->uc.guc, GSC_VER_PKT_SZ * 2,
355 					     &vma, &vaddr);
356 	if (err) {
357 		gt_err(gt, "failed to allocate vma for GSC version query\n");
358 		return err;
359 	}
360 
361 	offset = i915_ggtt_offset(vma);
362 	msg_in = vaddr;
363 	msg_out = vaddr + GSC_VER_PKT_SZ;
364 
365 	intel_gsc_uc_heci_cmd_emit_mtl_header(&msg_in->header,
366 					      HECI_MEADDRESS_MKHI,
367 					      sizeof(*msg_in), 0);
368 	msg_in->mkhi.group_id = MKHI_GROUP_ID_GFX_SRV;
369 	msg_in->mkhi.command = MKHI_GFX_SRV_GET_HOST_COMPATIBILITY_VERSION;
370 
371 	err = intel_gsc_uc_heci_cmd_submit_packet(&gt->uc.gsc,
372 						  offset,
373 						  sizeof(*msg_in),
374 						  offset + GSC_VER_PKT_SZ,
375 						  GSC_VER_PKT_SZ);
376 	if (err) {
377 		gt_err(gt,
378 		       "failed to submit GSC request for compatibility version: %d\n",
379 		       err);
380 		goto out_vma;
381 	}
382 
383 	if (msg_out->header.message_size != sizeof(*msg_out)) {
384 		gt_err(gt, "invalid GSC reply length %u [expected %zu], s=0x%x, f=0x%x, r=0x%x\n",
385 		       msg_out->header.message_size, sizeof(*msg_out),
386 		       msg_out->header.status, msg_out->header.flags, msg_out->mkhi.result);
387 		err = -EPROTO;
388 		goto out_vma;
389 	}
390 
391 	gsc->fw.file_selected.ver.major = msg_out->compat_major;
392 	gsc->fw.file_selected.ver.minor = msg_out->compat_minor;
393 
394 out_vma:
395 	i915_vma_unpin_and_release(&vma, I915_VMA_RELEASE_MAP);
396 	return err;
397 }
398 
intel_gsc_uc_fw_upload(struct intel_gsc_uc * gsc)399 int intel_gsc_uc_fw_upload(struct intel_gsc_uc *gsc)
400 {
401 	struct intel_gt *gt = gsc_uc_to_gt(gsc);
402 	struct intel_uc_fw *gsc_fw = &gsc->fw;
403 	int err;
404 
405 	/* check current fw status */
406 	if (intel_gsc_uc_fw_init_done(gsc)) {
407 		if (GEM_WARN_ON(!intel_uc_fw_is_loaded(gsc_fw)))
408 			intel_uc_fw_change_status(gsc_fw, INTEL_UC_FIRMWARE_TRANSFERRED);
409 		return -EEXIST;
410 	}
411 
412 	if (!intel_uc_fw_is_loadable(gsc_fw))
413 		return -ENOEXEC;
414 
415 	/* FW blob is ok, so clean the status */
416 	intel_uc_fw_sanitize(&gsc->fw);
417 
418 	if (!gsc_is_in_reset(gt->uncore))
419 		return -EIO;
420 
421 	err = gsc_fw_load_prepare(gsc);
422 	if (err)
423 		goto fail;
424 
425 	/*
426 	 * GSC is only killed by an FLR, so we need to trigger one on unload to
427 	 * make sure we stop it. This is because we assign a chunk of memory to
428 	 * the GSC as part of the FW load , so we need to make sure it stops
429 	 * using it when we release it to the system on driver unload. Note that
430 	 * this is not a problem of the unload per-se, because the GSC will not
431 	 * touch that memory unless there are requests for it coming from the
432 	 * driver; therefore, no accesses will happen while i915 is not loaded,
433 	 * but if we re-load the driver then the GSC might wake up and try to
434 	 * access that old memory location again.
435 	 * Given that an FLR is a very disruptive action (see the FLR function
436 	 * for details), we want to do it as the last action before releasing
437 	 * the access to the MMIO bar, which means we need to do it as part of
438 	 * the primary uncore cleanup.
439 	 * An alternative approach to the FLR would be to use a memory location
440 	 * that survives driver unload, like e.g. stolen memory, and keep the
441 	 * GSC loaded across reloads. However, this requires us to make sure we
442 	 * preserve that memory location on unload and then determine and
443 	 * reserve its offset on each subsequent load, which is not trivial, so
444 	 * it is easier to just kill everything and start fresh.
445 	 */
446 	intel_uncore_set_flr_on_fini(&gt->i915->uncore);
447 
448 	err = gsc_fw_load(gsc);
449 	if (err)
450 		goto fail;
451 
452 	err = gsc_fw_wait(gt);
453 	if (err)
454 		goto fail;
455 
456 	err = gsc_fw_query_compatibility_version(gsc);
457 	if (err)
458 		goto fail;
459 
460 	/* we only support compatibility version 1.0 at the moment */
461 	err = intel_uc_check_file_version(gsc_fw, NULL);
462 	if (err)
463 		goto fail;
464 
465 	/* FW is not fully operational until we enable SW proxy */
466 	intel_uc_fw_change_status(gsc_fw, INTEL_UC_FIRMWARE_TRANSFERRED);
467 
468 	gt_info(gt, "Loaded GSC firmware %s (cv%u.%u, r%u.%u.%u.%u, svn %u)\n",
469 		gsc_fw->file_selected.path,
470 		gsc_fw->file_selected.ver.major, gsc_fw->file_selected.ver.minor,
471 		gsc->release.major, gsc->release.minor,
472 		gsc->release.patch, gsc->release.build,
473 		gsc->security_version);
474 
475 	return 0;
476 
477 fail:
478 	return intel_uc_fw_mark_load_failed(gsc_fw, err);
479 }
480