xref: /linux/drivers/gpu/drm/xe/xe_gsc_proxy.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include "xe_gsc_proxy.h"
7 
8 #include <linux/component.h>
9 #include <linux/delay.h>
10 
11 #include <drm/drm_managed.h>
12 #include <drm/intel/i915_component.h>
13 #include <drm/intel/i915_gsc_proxy_mei_interface.h>
14 
15 #include "abi/gsc_proxy_commands_abi.h"
16 #include "regs/xe_gsc_regs.h"
17 #include "xe_bo.h"
18 #include "xe_force_wake.h"
19 #include "xe_gsc.h"
20 #include "xe_gsc_submit.h"
21 #include "xe_gt.h"
22 #include "xe_gt_printk.h"
23 #include "xe_map.h"
24 #include "xe_mmio.h"
25 #include "xe_pm.h"
26 
27 /*
28  * GSC proxy:
29  * The GSC uC needs to communicate with the CSME to perform certain operations.
30  * Since the GSC can't perform this communication directly on platforms where it
31  * is integrated in GT, the graphics driver needs to transfer the messages from
32  * GSC to CSME and back. The proxy flow must be manually started after the GSC
33  * is loaded to signal to GSC that we're ready to handle its messages and allow
34  * it to query its init data from CSME; GSC will then trigger an HECI2 interrupt
35  * if it needs to send messages to CSME again.
36  * The proxy flow is as follow:
37  * 1 - Xe submits a request to GSC asking for the message to CSME
38  * 2 - GSC replies with the proxy header + payload for CSME
39  * 3 - Xe sends the reply from GSC as-is to CSME via the mei proxy component
40  * 4 - CSME replies with the proxy header + payload for GSC
41  * 5 - Xe submits a request to GSC with the reply from CSME
42  * 6 - GSC replies either with a new header + payload (same as step 2, so we
43  *     restart from there) or with an end message.
44  */
45 
46 /*
47  * The component should load quite quickly in most cases, but it could take
48  * a bit. Using a very big timeout just to cover the worst case scenario
49  */
50 #define GSC_PROXY_INIT_TIMEOUT_MS 20000
51 
52 /* shorthand define for code compactness */
53 #define PROXY_HDR_SIZE (sizeof(struct xe_gsc_proxy_header))
54 
55 /* the protocol supports up to 32K in each direction */
56 #define GSC_PROXY_BUFFER_SIZE SZ_32K
57 #define GSC_PROXY_CHANNEL_SIZE (GSC_PROXY_BUFFER_SIZE * 2)
58 
59 static struct xe_gt *
gsc_to_gt(struct xe_gsc * gsc)60 gsc_to_gt(struct xe_gsc *gsc)
61 {
62 	return container_of(gsc, struct xe_gt, uc.gsc);
63 }
64 
xe_gsc_proxy_init_done(struct xe_gsc * gsc)65 bool xe_gsc_proxy_init_done(struct xe_gsc *gsc)
66 {
67 	struct xe_gt *gt = gsc_to_gt(gsc);
68 	u32 fwsts1 = xe_mmio_read32(&gt->mmio, HECI_FWSTS1(MTL_GSC_HECI1_BASE));
69 
70 	return REG_FIELD_GET(HECI1_FWSTS1_CURRENT_STATE, fwsts1) ==
71 	       HECI1_FWSTS1_PROXY_STATE_NORMAL;
72 }
73 
__gsc_proxy_irq_rmw(struct xe_gsc * gsc,u32 clr,u32 set)74 static void __gsc_proxy_irq_rmw(struct xe_gsc *gsc, u32 clr, u32 set)
75 {
76 	struct xe_gt *gt = gsc_to_gt(gsc);
77 
78 	/* make sure we never accidentally write the RST bit */
79 	clr |= HECI_H_CSR_RST;
80 
81 	xe_mmio_rmw32(&gt->mmio, HECI_H_CSR(MTL_GSC_HECI2_BASE), clr, set);
82 }
83 
gsc_proxy_irq_clear(struct xe_gsc * gsc)84 static void gsc_proxy_irq_clear(struct xe_gsc *gsc)
85 {
86 	/* The status bit is cleared by writing to it */
87 	__gsc_proxy_irq_rmw(gsc, 0, HECI_H_CSR_IS);
88 }
89 
gsc_proxy_irq_toggle(struct xe_gsc * gsc,bool enabled)90 static void gsc_proxy_irq_toggle(struct xe_gsc *gsc, bool enabled)
91 {
92 	u32 set = enabled ? HECI_H_CSR_IE : 0;
93 	u32 clr = enabled ? 0 : HECI_H_CSR_IE;
94 
95 	__gsc_proxy_irq_rmw(gsc, clr, set);
96 }
97 
proxy_send_to_csme(struct xe_gsc * gsc,u32 size)98 static int proxy_send_to_csme(struct xe_gsc *gsc, u32 size)
99 {
100 	struct xe_gt *gt = gsc_to_gt(gsc);
101 	struct i915_gsc_proxy_component *comp = gsc->proxy.component;
102 	int ret;
103 
104 	ret = comp->ops->send(comp->mei_dev, gsc->proxy.to_csme, size);
105 	if (ret < 0) {
106 		xe_gt_err(gt, "Failed to send CSME proxy message\n");
107 		return ret;
108 	}
109 
110 	ret = comp->ops->recv(comp->mei_dev, gsc->proxy.from_csme, GSC_PROXY_BUFFER_SIZE);
111 	if (ret < 0) {
112 		xe_gt_err(gt, "Failed to receive CSME proxy message\n");
113 		return ret;
114 	}
115 
116 	return ret;
117 }
118 
proxy_send_to_gsc(struct xe_gsc * gsc,u32 size)119 static int proxy_send_to_gsc(struct xe_gsc *gsc, u32 size)
120 {
121 	struct xe_gt *gt = gsc_to_gt(gsc);
122 	u64 addr_in = xe_bo_ggtt_addr(gsc->proxy.bo);
123 	u64 addr_out = addr_in + GSC_PROXY_BUFFER_SIZE;
124 	int err;
125 
126 	/* the message must contain at least the gsc and proxy headers */
127 	if (size > GSC_PROXY_BUFFER_SIZE) {
128 		xe_gt_err(gt, "Invalid GSC proxy message size: %u\n", size);
129 		return -EINVAL;
130 	}
131 
132 	err = xe_gsc_pkt_submit_kernel(gsc, addr_in, size,
133 				       addr_out, GSC_PROXY_BUFFER_SIZE);
134 	if (err) {
135 		xe_gt_err(gt, "Failed to submit gsc proxy rq (%pe)\n", ERR_PTR(err));
136 		return err;
137 	}
138 
139 	return 0;
140 }
141 
validate_proxy_header(struct xe_gt * gt,struct xe_gsc_proxy_header * header,u32 source,u32 dest,u32 max_size)142 static int validate_proxy_header(struct xe_gt *gt,
143 				 struct xe_gsc_proxy_header *header,
144 				 u32 source, u32 dest, u32 max_size)
145 {
146 	u32 type = FIELD_GET(GSC_PROXY_TYPE, header->hdr);
147 	u32 length = FIELD_GET(GSC_PROXY_PAYLOAD_LENGTH, header->hdr);
148 	int ret = 0;
149 
150 	if (header->destination != dest || header->source != source) {
151 		ret = -ENOEXEC;
152 		goto out;
153 	}
154 
155 	if (length + PROXY_HDR_SIZE > max_size) {
156 		ret = -E2BIG;
157 		goto out;
158 	}
159 
160 	/* We only care about the status if this is a message for the driver */
161 	if (dest == GSC_PROXY_ADDRESSING_KMD && header->status != 0) {
162 		ret = -EIO;
163 		goto out;
164 	}
165 
166 	switch (type) {
167 	case GSC_PROXY_MSG_TYPE_PROXY_PAYLOAD:
168 		if (length > 0)
169 			break;
170 		fallthrough;
171 	case GSC_PROXY_MSG_TYPE_PROXY_INVALID:
172 		ret = -EIO;
173 		break;
174 	default:
175 		break;
176 	}
177 
178 out:
179 	if (ret)
180 		xe_gt_err(gt,
181 			  "GSC proxy error: s=0x%x[0x%x], d=0x%x[0x%x], t=%u, l=0x%x, st=0x%x\n",
182 			  header->source, source, header->destination, dest,
183 			  type, length, header->status);
184 
185 	return ret;
186 }
187 
188 #define proxy_header_wr(xe_, map_, offset_, field_, val_) \
189 	xe_map_wr_field(xe_, map_, offset_, struct xe_gsc_proxy_header, field_, val_)
190 
191 #define proxy_header_rd(xe_, map_, offset_, field_) \
192 	xe_map_rd_field(xe_, map_, offset_, struct xe_gsc_proxy_header, field_)
193 
emit_proxy_header(struct xe_device * xe,struct iosys_map * map,u32 offset)194 static u32 emit_proxy_header(struct xe_device *xe, struct iosys_map *map, u32 offset)
195 {
196 	xe_map_memset(xe, map, offset, 0, PROXY_HDR_SIZE);
197 
198 	proxy_header_wr(xe, map, offset, hdr,
199 			FIELD_PREP(GSC_PROXY_TYPE, GSC_PROXY_MSG_TYPE_PROXY_QUERY) |
200 			FIELD_PREP(GSC_PROXY_PAYLOAD_LENGTH, 0));
201 
202 	proxy_header_wr(xe, map, offset, source, GSC_PROXY_ADDRESSING_KMD);
203 	proxy_header_wr(xe, map, offset, destination, GSC_PROXY_ADDRESSING_GSC);
204 	proxy_header_wr(xe, map, offset, status, 0);
205 
206 	return offset + PROXY_HDR_SIZE;
207 }
208 
proxy_query(struct xe_gsc * gsc)209 static int proxy_query(struct xe_gsc *gsc)
210 {
211 	struct xe_gt *gt = gsc_to_gt(gsc);
212 	struct xe_device *xe = gt_to_xe(gt);
213 	struct xe_gsc_proxy_header *to_csme_hdr = gsc->proxy.to_csme;
214 	void *to_csme_payload = gsc->proxy.to_csme + PROXY_HDR_SIZE;
215 	u32 wr_offset;
216 	u32 reply_offset;
217 	u32 size;
218 	int ret;
219 
220 	wr_offset = xe_gsc_emit_header(xe, &gsc->proxy.to_gsc, 0,
221 				       HECI_MEADDRESS_PROXY, 0, PROXY_HDR_SIZE);
222 	wr_offset = emit_proxy_header(xe, &gsc->proxy.to_gsc, wr_offset);
223 
224 	size = wr_offset;
225 
226 	while (1) {
227 		/*
228 		 * Poison the GSC response header space to make sure we don't
229 		 * read a stale reply.
230 		 */
231 		xe_gsc_poison_header(xe, &gsc->proxy.from_gsc, 0);
232 
233 		/* send proxy message to GSC */
234 		ret = proxy_send_to_gsc(gsc, size);
235 		if (ret)
236 			goto proxy_error;
237 
238 		/* check the reply from GSC */
239 		ret = xe_gsc_read_out_header(xe, &gsc->proxy.from_gsc, 0,
240 					     PROXY_HDR_SIZE, &reply_offset);
241 		if (ret) {
242 			xe_gt_err(gt, "Invalid gsc header in proxy reply (%pe)\n",
243 				  ERR_PTR(ret));
244 			goto proxy_error;
245 		}
246 
247 		/* copy the proxy header reply from GSC */
248 		xe_map_memcpy_from(xe, to_csme_hdr, &gsc->proxy.from_gsc,
249 				   reply_offset, PROXY_HDR_SIZE);
250 
251 		/* Check the status and stop if this was the last message */
252 		if (FIELD_GET(GSC_PROXY_TYPE, to_csme_hdr->hdr) == GSC_PROXY_MSG_TYPE_PROXY_END) {
253 			ret = validate_proxy_header(gt, to_csme_hdr,
254 						    GSC_PROXY_ADDRESSING_GSC,
255 						    GSC_PROXY_ADDRESSING_KMD,
256 						    GSC_PROXY_BUFFER_SIZE - reply_offset);
257 			break;
258 		}
259 
260 		/* make sure the GSC-to-CSME proxy header is sane */
261 		ret = validate_proxy_header(gt, to_csme_hdr,
262 					    GSC_PROXY_ADDRESSING_GSC,
263 					    GSC_PROXY_ADDRESSING_CSME,
264 					    GSC_PROXY_BUFFER_SIZE - reply_offset);
265 		if (ret) {
266 			xe_gt_err(gt, "invalid GSC to CSME proxy header! (%pe)\n",
267 				  ERR_PTR(ret));
268 			goto proxy_error;
269 		}
270 
271 		/* copy the rest of the message */
272 		size = FIELD_GET(GSC_PROXY_PAYLOAD_LENGTH, to_csme_hdr->hdr);
273 		xe_map_memcpy_from(xe, to_csme_payload, &gsc->proxy.from_gsc,
274 				   reply_offset + PROXY_HDR_SIZE, size);
275 
276 		/* send the GSC message to the CSME */
277 		ret = proxy_send_to_csme(gsc, size + PROXY_HDR_SIZE);
278 		if (ret < 0)
279 			goto proxy_error;
280 
281 		/* reply size from CSME, including the proxy header */
282 		size = ret;
283 		if (size < PROXY_HDR_SIZE) {
284 			xe_gt_err(gt, "CSME to GSC proxy msg too small: 0x%x\n", size);
285 			ret = -EPROTO;
286 			goto proxy_error;
287 		}
288 
289 		/* make sure the CSME-to-GSC proxy header is sane */
290 		ret = validate_proxy_header(gt, gsc->proxy.from_csme,
291 					    GSC_PROXY_ADDRESSING_CSME,
292 					    GSC_PROXY_ADDRESSING_GSC,
293 					    GSC_PROXY_BUFFER_SIZE - reply_offset);
294 		if (ret) {
295 			xe_gt_err(gt, "invalid CSME to GSC proxy header! %d\n", ret);
296 			goto proxy_error;
297 		}
298 
299 		/* Emit a new header for sending the reply to the GSC */
300 		wr_offset = xe_gsc_emit_header(xe, &gsc->proxy.to_gsc, 0,
301 					       HECI_MEADDRESS_PROXY, 0, size);
302 
303 		/* copy the CSME reply and update the total msg size to include the GSC header */
304 		xe_map_memcpy_to(xe, &gsc->proxy.to_gsc, wr_offset, gsc->proxy.from_csme, size);
305 
306 		size += wr_offset;
307 	}
308 
309 proxy_error:
310 	return ret < 0 ? ret : 0;
311 }
312 
xe_gsc_proxy_request_handler(struct xe_gsc * gsc)313 int xe_gsc_proxy_request_handler(struct xe_gsc *gsc)
314 {
315 	struct xe_gt *gt = gsc_to_gt(gsc);
316 	int slept;
317 	int err;
318 
319 	if (!gsc->proxy.component_added)
320 		return -ENODEV;
321 
322 	/* when GSC is loaded, we can queue this before the component is bound */
323 	for (slept = 0; slept < GSC_PROXY_INIT_TIMEOUT_MS; slept += 100) {
324 		if (gsc->proxy.component)
325 			break;
326 
327 		msleep(100);
328 	}
329 
330 	mutex_lock(&gsc->proxy.mutex);
331 	if (!gsc->proxy.component) {
332 		xe_gt_err(gt, "GSC proxy component not bound!\n");
333 		err = -EIO;
334 	} else {
335 		/*
336 		 * clear the pending interrupt and allow new proxy requests to
337 		 * be generated while we handle the current one
338 		 */
339 		gsc_proxy_irq_clear(gsc);
340 		err = proxy_query(gsc);
341 	}
342 	mutex_unlock(&gsc->proxy.mutex);
343 	return err;
344 }
345 
xe_gsc_proxy_irq_handler(struct xe_gsc * gsc,u32 iir)346 void xe_gsc_proxy_irq_handler(struct xe_gsc *gsc, u32 iir)
347 {
348 	struct xe_gt *gt = gsc_to_gt(gsc);
349 
350 	if (unlikely(!iir))
351 		return;
352 
353 	if (!gsc->proxy.component) {
354 		xe_gt_err(gt, "GSC proxy irq received without the component being bound!\n");
355 		return;
356 	}
357 
358 	spin_lock(&gsc->lock);
359 	gsc->work_actions |= GSC_ACTION_SW_PROXY;
360 	spin_unlock(&gsc->lock);
361 
362 	queue_work(gsc->wq, &gsc->work);
363 }
364 
xe_gsc_proxy_component_bind(struct device * xe_kdev,struct device * mei_kdev,void * data)365 static int xe_gsc_proxy_component_bind(struct device *xe_kdev,
366 				       struct device *mei_kdev, void *data)
367 {
368 	struct xe_device *xe = kdev_to_xe_device(xe_kdev);
369 	struct xe_gt *gt = xe->tiles[0].media_gt;
370 	struct xe_gsc *gsc = &gt->uc.gsc;
371 
372 	mutex_lock(&gsc->proxy.mutex);
373 	gsc->proxy.component = data;
374 	gsc->proxy.component->mei_dev = mei_kdev;
375 	mutex_unlock(&gsc->proxy.mutex);
376 
377 	return 0;
378 }
379 
xe_gsc_proxy_component_unbind(struct device * xe_kdev,struct device * mei_kdev,void * data)380 static void xe_gsc_proxy_component_unbind(struct device *xe_kdev,
381 					  struct device *mei_kdev, void *data)
382 {
383 	struct xe_device *xe = kdev_to_xe_device(xe_kdev);
384 	struct xe_gt *gt = xe->tiles[0].media_gt;
385 	struct xe_gsc *gsc = &gt->uc.gsc;
386 
387 	xe_gsc_wait_for_worker_completion(gsc);
388 
389 	mutex_lock(&gsc->proxy.mutex);
390 	gsc->proxy.component = NULL;
391 	mutex_unlock(&gsc->proxy.mutex);
392 }
393 
394 static const struct component_ops xe_gsc_proxy_component_ops = {
395 	.bind   = xe_gsc_proxy_component_bind,
396 	.unbind = xe_gsc_proxy_component_unbind,
397 };
398 
proxy_channel_alloc(struct xe_gsc * gsc)399 static int proxy_channel_alloc(struct xe_gsc *gsc)
400 {
401 	struct xe_gt *gt = gsc_to_gt(gsc);
402 	struct xe_tile *tile = gt_to_tile(gt);
403 	struct xe_device *xe = gt_to_xe(gt);
404 	struct xe_bo *bo;
405 	void *csme;
406 
407 	csme = drmm_kzalloc(&xe->drm, GSC_PROXY_CHANNEL_SIZE, GFP_KERNEL);
408 	if (!csme)
409 		return -ENOMEM;
410 
411 	bo = xe_managed_bo_create_pin_map(xe, tile, GSC_PROXY_CHANNEL_SIZE,
412 					  XE_BO_FLAG_SYSTEM |
413 					  XE_BO_FLAG_GGTT);
414 	if (IS_ERR(bo))
415 		return PTR_ERR(bo);
416 
417 	gsc->proxy.bo = bo;
418 	gsc->proxy.to_gsc = IOSYS_MAP_INIT_OFFSET(&bo->vmap, 0);
419 	gsc->proxy.from_gsc = IOSYS_MAP_INIT_OFFSET(&bo->vmap, GSC_PROXY_BUFFER_SIZE);
420 	gsc->proxy.to_csme = csme;
421 	gsc->proxy.from_csme = csme + GSC_PROXY_BUFFER_SIZE;
422 
423 	return 0;
424 }
425 
426 /**
427  * xe_gsc_proxy_init() - init objects and MEI component required by GSC proxy
428  * @gsc: the GSC uC
429  *
430  * Return: 0 if the initialization was successful, a negative errno otherwise.
431  */
xe_gsc_proxy_init(struct xe_gsc * gsc)432 int xe_gsc_proxy_init(struct xe_gsc *gsc)
433 {
434 	int err;
435 	struct xe_gt *gt = gsc_to_gt(gsc);
436 	struct xe_tile *tile = gt_to_tile(gt);
437 	struct xe_device *xe = tile_to_xe(tile);
438 
439 	mutex_init(&gsc->proxy.mutex);
440 
441 	if (!IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY)) {
442 		xe_gt_info(gt, "can't init GSC proxy due to missing mei component\n");
443 		return -ENODEV;
444 	}
445 
446 	/* no multi-tile devices with this feature yet */
447 	if (tile->id > 0) {
448 		xe_gt_err(gt, "unexpected GSC proxy init on tile %u\n", tile->id);
449 		return -EINVAL;
450 	}
451 
452 	err = proxy_channel_alloc(gsc);
453 	if (err)
454 		return err;
455 
456 	err = component_add_typed(xe->drm.dev, &xe_gsc_proxy_component_ops,
457 				  I915_COMPONENT_GSC_PROXY);
458 	if (err < 0) {
459 		xe_gt_err(gt, "Failed to add GSC_PROXY component (%pe)\n", ERR_PTR(err));
460 		return err;
461 	}
462 
463 	gsc->proxy.component_added = true;
464 
465 	/* the component must be removed before unload, so can't use drmm for cleanup */
466 
467 	return 0;
468 }
469 
470 /**
471  * xe_gsc_proxy_remove() - remove the GSC proxy MEI component
472  * @gsc: the GSC uC
473  */
xe_gsc_proxy_remove(struct xe_gsc * gsc)474 void xe_gsc_proxy_remove(struct xe_gsc *gsc)
475 {
476 	struct xe_gt *gt = gsc_to_gt(gsc);
477 	struct xe_device *xe = gt_to_xe(gt);
478 	unsigned int fw_ref = 0;
479 
480 	if (!gsc->proxy.component_added)
481 		return;
482 
483 	/* disable HECI2 IRQs */
484 	xe_pm_runtime_get(xe);
485 	fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GSC);
486 	if (!fw_ref)
487 		xe_gt_err(gt, "failed to get forcewake to disable GSC interrupts\n");
488 
489 	/* try do disable irq even if forcewake failed */
490 	gsc_proxy_irq_toggle(gsc, false);
491 
492 	xe_force_wake_put(gt_to_fw(gt), fw_ref);
493 	xe_pm_runtime_put(xe);
494 
495 	xe_gsc_wait_for_worker_completion(gsc);
496 
497 	component_del(xe->drm.dev, &xe_gsc_proxy_component_ops);
498 	gsc->proxy.component_added = false;
499 }
500 
501 /**
502  * xe_gsc_proxy_start() - start the proxy by submitting the first request
503  * @gsc: the GSC uC
504  *
505  * Return: 0 if the proxy are now enabled, a negative errno otherwise.
506  */
xe_gsc_proxy_start(struct xe_gsc * gsc)507 int xe_gsc_proxy_start(struct xe_gsc *gsc)
508 {
509 	int err;
510 
511 	/* enable the proxy interrupt in the GSC shim layer */
512 	gsc_proxy_irq_toggle(gsc, true);
513 
514 	/*
515 	 * The handling of the first proxy request must be manually triggered to
516 	 * notify the GSC that we're ready to support the proxy flow.
517 	 */
518 	err = xe_gsc_proxy_request_handler(gsc);
519 	if (err)
520 		return err;
521 
522 	if (!xe_gsc_proxy_init_done(gsc)) {
523 		xe_gt_err(gsc_to_gt(gsc), "GSC FW reports proxy init not completed\n");
524 		return -EIO;
525 	}
526 
527 	return 0;
528 }
529