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, 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
xe_gsc_wait_for_proxy_init_done(struct xe_gsc * gsc)74 int xe_gsc_wait_for_proxy_init_done(struct xe_gsc *gsc)
75 {
76 struct xe_gt *gt = gsc_to_gt(gsc);
77
78 /* Proxy init can take up to 500ms, so wait double that for safety */
79 return xe_mmio_wait32(gt, HECI_FWSTS1(MTL_GSC_HECI1_BASE),
80 HECI1_FWSTS1_CURRENT_STATE,
81 HECI1_FWSTS1_PROXY_STATE_NORMAL,
82 USEC_PER_SEC, NULL, false);
83 }
84
__gsc_proxy_irq_rmw(struct xe_gsc * gsc,u32 clr,u32 set)85 static void __gsc_proxy_irq_rmw(struct xe_gsc *gsc, u32 clr, u32 set)
86 {
87 struct xe_gt *gt = gsc_to_gt(gsc);
88
89 /* make sure we never accidentally write the RST bit */
90 clr |= HECI_H_CSR_RST;
91
92 xe_mmio_rmw32(gt, HECI_H_CSR(MTL_GSC_HECI2_BASE), clr, set);
93 }
94
gsc_proxy_irq_clear(struct xe_gsc * gsc)95 static void gsc_proxy_irq_clear(struct xe_gsc *gsc)
96 {
97 /* The status bit is cleared by writing to it */
98 __gsc_proxy_irq_rmw(gsc, 0, HECI_H_CSR_IS);
99 }
100
gsc_proxy_irq_toggle(struct xe_gsc * gsc,bool enabled)101 static void gsc_proxy_irq_toggle(struct xe_gsc *gsc, bool enabled)
102 {
103 u32 set = enabled ? HECI_H_CSR_IE : 0;
104 u32 clr = enabled ? 0 : HECI_H_CSR_IE;
105
106 __gsc_proxy_irq_rmw(gsc, clr, set);
107 }
108
proxy_send_to_csme(struct xe_gsc * gsc,u32 size)109 static int proxy_send_to_csme(struct xe_gsc *gsc, u32 size)
110 {
111 struct xe_gt *gt = gsc_to_gt(gsc);
112 struct i915_gsc_proxy_component *comp = gsc->proxy.component;
113 int ret;
114
115 ret = comp->ops->send(comp->mei_dev, gsc->proxy.to_csme, size);
116 if (ret < 0) {
117 xe_gt_err(gt, "Failed to send CSME proxy message\n");
118 return ret;
119 }
120
121 ret = comp->ops->recv(comp->mei_dev, gsc->proxy.from_csme, GSC_PROXY_BUFFER_SIZE);
122 if (ret < 0) {
123 xe_gt_err(gt, "Failed to receive CSME proxy message\n");
124 return ret;
125 }
126
127 return ret;
128 }
129
proxy_send_to_gsc(struct xe_gsc * gsc,u32 size)130 static int proxy_send_to_gsc(struct xe_gsc *gsc, u32 size)
131 {
132 struct xe_gt *gt = gsc_to_gt(gsc);
133 u64 addr_in = xe_bo_ggtt_addr(gsc->proxy.bo);
134 u64 addr_out = addr_in + GSC_PROXY_BUFFER_SIZE;
135 int err;
136
137 /* the message must contain at least the gsc and proxy headers */
138 if (size > GSC_PROXY_BUFFER_SIZE) {
139 xe_gt_err(gt, "Invalid GSC proxy message size: %u\n", size);
140 return -EINVAL;
141 }
142
143 err = xe_gsc_pkt_submit_kernel(gsc, addr_in, size,
144 addr_out, GSC_PROXY_BUFFER_SIZE);
145 if (err) {
146 xe_gt_err(gt, "Failed to submit gsc proxy rq (%pe)\n", ERR_PTR(err));
147 return err;
148 }
149
150 return 0;
151 }
152
validate_proxy_header(struct xe_gsc_proxy_header * header,u32 source,u32 dest,u32 max_size)153 static int validate_proxy_header(struct xe_gsc_proxy_header *header,
154 u32 source, u32 dest, u32 max_size)
155 {
156 u32 type = FIELD_GET(GSC_PROXY_TYPE, header->hdr);
157 u32 length = FIELD_GET(GSC_PROXY_PAYLOAD_LENGTH, header->hdr);
158
159 if (header->destination != dest || header->source != source)
160 return -ENOEXEC;
161
162 if (length + PROXY_HDR_SIZE > max_size)
163 return -E2BIG;
164
165 switch (type) {
166 case GSC_PROXY_MSG_TYPE_PROXY_PAYLOAD:
167 if (length > 0)
168 break;
169 fallthrough;
170 case GSC_PROXY_MSG_TYPE_PROXY_INVALID:
171 return -EIO;
172 default:
173 break;
174 }
175
176 return 0;
177 }
178
179 #define proxy_header_wr(xe_, map_, offset_, field_, val_) \
180 xe_map_wr_field(xe_, map_, offset_, struct xe_gsc_proxy_header, field_, val_)
181
182 #define proxy_header_rd(xe_, map_, offset_, field_) \
183 xe_map_rd_field(xe_, map_, offset_, struct xe_gsc_proxy_header, field_)
184
emit_proxy_header(struct xe_device * xe,struct iosys_map * map,u32 offset)185 static u32 emit_proxy_header(struct xe_device *xe, struct iosys_map *map, u32 offset)
186 {
187 xe_map_memset(xe, map, offset, 0, PROXY_HDR_SIZE);
188
189 proxy_header_wr(xe, map, offset, hdr,
190 FIELD_PREP(GSC_PROXY_TYPE, GSC_PROXY_MSG_TYPE_PROXY_QUERY) |
191 FIELD_PREP(GSC_PROXY_PAYLOAD_LENGTH, 0));
192
193 proxy_header_wr(xe, map, offset, source, GSC_PROXY_ADDRESSING_KMD);
194 proxy_header_wr(xe, map, offset, destination, GSC_PROXY_ADDRESSING_GSC);
195 proxy_header_wr(xe, map, offset, status, 0);
196
197 return offset + PROXY_HDR_SIZE;
198 }
199
proxy_query(struct xe_gsc * gsc)200 static int proxy_query(struct xe_gsc *gsc)
201 {
202 struct xe_gt *gt = gsc_to_gt(gsc);
203 struct xe_device *xe = gt_to_xe(gt);
204 struct xe_gsc_proxy_header *to_csme_hdr = gsc->proxy.to_csme;
205 void *to_csme_payload = gsc->proxy.to_csme + PROXY_HDR_SIZE;
206 u32 wr_offset;
207 u32 reply_offset;
208 u32 size;
209 int ret;
210
211 wr_offset = xe_gsc_emit_header(xe, &gsc->proxy.to_gsc, 0,
212 HECI_MEADDRESS_PROXY, 0, PROXY_HDR_SIZE);
213 wr_offset = emit_proxy_header(xe, &gsc->proxy.to_gsc, wr_offset);
214
215 size = wr_offset;
216
217 while (1) {
218 /*
219 * Poison the GSC response header space to make sure we don't
220 * read a stale reply.
221 */
222 xe_gsc_poison_header(xe, &gsc->proxy.from_gsc, 0);
223
224 /* send proxy message to GSC */
225 ret = proxy_send_to_gsc(gsc, size);
226 if (ret)
227 goto proxy_error;
228
229 /* check the reply from GSC */
230 ret = xe_gsc_read_out_header(xe, &gsc->proxy.from_gsc, 0,
231 PROXY_HDR_SIZE, &reply_offset);
232 if (ret) {
233 xe_gt_err(gt, "Invalid gsc header in proxy reply (%pe)\n",
234 ERR_PTR(ret));
235 goto proxy_error;
236 }
237
238 /* copy the proxy header reply from GSC */
239 xe_map_memcpy_from(xe, to_csme_hdr, &gsc->proxy.from_gsc,
240 reply_offset, PROXY_HDR_SIZE);
241
242 /* stop if this was the last message */
243 if (FIELD_GET(GSC_PROXY_TYPE, to_csme_hdr->hdr) == GSC_PROXY_MSG_TYPE_PROXY_END)
244 break;
245
246 /* make sure the GSC-to-CSME proxy header is sane */
247 ret = validate_proxy_header(to_csme_hdr,
248 GSC_PROXY_ADDRESSING_GSC,
249 GSC_PROXY_ADDRESSING_CSME,
250 GSC_PROXY_BUFFER_SIZE - reply_offset);
251 if (ret) {
252 xe_gt_err(gt, "invalid GSC to CSME proxy header! (%pe)\n",
253 ERR_PTR(ret));
254 goto proxy_error;
255 }
256
257 /* copy the rest of the message */
258 size = FIELD_GET(GSC_PROXY_PAYLOAD_LENGTH, to_csme_hdr->hdr);
259 xe_map_memcpy_from(xe, to_csme_payload, &gsc->proxy.from_gsc,
260 reply_offset + PROXY_HDR_SIZE, size);
261
262 /* send the GSC message to the CSME */
263 ret = proxy_send_to_csme(gsc, size + PROXY_HDR_SIZE);
264 if (ret < 0)
265 goto proxy_error;
266
267 /* reply size from CSME, including the proxy header */
268 size = ret;
269 if (size < PROXY_HDR_SIZE) {
270 xe_gt_err(gt, "CSME to GSC proxy msg too small: 0x%x\n", size);
271 ret = -EPROTO;
272 goto proxy_error;
273 }
274
275 /* make sure the CSME-to-GSC proxy header is sane */
276 ret = validate_proxy_header(gsc->proxy.from_csme,
277 GSC_PROXY_ADDRESSING_CSME,
278 GSC_PROXY_ADDRESSING_GSC,
279 GSC_PROXY_BUFFER_SIZE - reply_offset);
280 if (ret) {
281 xe_gt_err(gt, "invalid CSME to GSC proxy header! %d\n", ret);
282 goto proxy_error;
283 }
284
285 /* Emit a new header for sending the reply to the GSC */
286 wr_offset = xe_gsc_emit_header(xe, &gsc->proxy.to_gsc, 0,
287 HECI_MEADDRESS_PROXY, 0, size);
288
289 /* copy the CSME reply and update the total msg size to include the GSC header */
290 xe_map_memcpy_to(xe, &gsc->proxy.to_gsc, wr_offset, gsc->proxy.from_csme, size);
291
292 size += wr_offset;
293 }
294
295 proxy_error:
296 return ret < 0 ? ret : 0;
297 }
298
xe_gsc_proxy_request_handler(struct xe_gsc * gsc)299 int xe_gsc_proxy_request_handler(struct xe_gsc *gsc)
300 {
301 struct xe_gt *gt = gsc_to_gt(gsc);
302 int slept;
303 int err;
304
305 if (!gsc->proxy.component_added)
306 return -ENODEV;
307
308 /* when GSC is loaded, we can queue this before the component is bound */
309 for (slept = 0; slept < GSC_PROXY_INIT_TIMEOUT_MS; slept += 100) {
310 if (gsc->proxy.component)
311 break;
312
313 msleep(100);
314 }
315
316 mutex_lock(&gsc->proxy.mutex);
317 if (!gsc->proxy.component) {
318 xe_gt_err(gt, "GSC proxy component not bound!\n");
319 err = -EIO;
320 } else {
321 /*
322 * clear the pending interrupt and allow new proxy requests to
323 * be generated while we handle the current one
324 */
325 gsc_proxy_irq_clear(gsc);
326 err = proxy_query(gsc);
327 }
328 mutex_unlock(&gsc->proxy.mutex);
329 return err;
330 }
331
xe_gsc_proxy_irq_handler(struct xe_gsc * gsc,u32 iir)332 void xe_gsc_proxy_irq_handler(struct xe_gsc *gsc, u32 iir)
333 {
334 struct xe_gt *gt = gsc_to_gt(gsc);
335
336 if (unlikely(!iir))
337 return;
338
339 if (!gsc->proxy.component) {
340 xe_gt_err(gt, "GSC proxy irq received without the component being bound!\n");
341 return;
342 }
343
344 spin_lock(&gsc->lock);
345 gsc->work_actions |= GSC_ACTION_SW_PROXY;
346 spin_unlock(&gsc->lock);
347
348 queue_work(gsc->wq, &gsc->work);
349 }
350
xe_gsc_proxy_component_bind(struct device * xe_kdev,struct device * mei_kdev,void * data)351 static int xe_gsc_proxy_component_bind(struct device *xe_kdev,
352 struct device *mei_kdev, void *data)
353 {
354 struct xe_device *xe = kdev_to_xe_device(xe_kdev);
355 struct xe_gt *gt = xe->tiles[0].media_gt;
356 struct xe_gsc *gsc = >->uc.gsc;
357
358 mutex_lock(&gsc->proxy.mutex);
359 gsc->proxy.component = data;
360 gsc->proxy.component->mei_dev = mei_kdev;
361 mutex_unlock(&gsc->proxy.mutex);
362
363 return 0;
364 }
365
xe_gsc_proxy_component_unbind(struct device * xe_kdev,struct device * mei_kdev,void * data)366 static void xe_gsc_proxy_component_unbind(struct device *xe_kdev,
367 struct device *mei_kdev, void *data)
368 {
369 struct xe_device *xe = kdev_to_xe_device(xe_kdev);
370 struct xe_gt *gt = xe->tiles[0].media_gt;
371 struct xe_gsc *gsc = >->uc.gsc;
372
373 xe_gsc_wait_for_worker_completion(gsc);
374
375 mutex_lock(&gsc->proxy.mutex);
376 gsc->proxy.component = NULL;
377 mutex_unlock(&gsc->proxy.mutex);
378 }
379
380 static const struct component_ops xe_gsc_proxy_component_ops = {
381 .bind = xe_gsc_proxy_component_bind,
382 .unbind = xe_gsc_proxy_component_unbind,
383 };
384
proxy_channel_alloc(struct xe_gsc * gsc)385 static int proxy_channel_alloc(struct xe_gsc *gsc)
386 {
387 struct xe_gt *gt = gsc_to_gt(gsc);
388 struct xe_tile *tile = gt_to_tile(gt);
389 struct xe_device *xe = gt_to_xe(gt);
390 struct xe_bo *bo;
391 void *csme;
392
393 csme = drmm_kzalloc(&xe->drm, GSC_PROXY_CHANNEL_SIZE, GFP_KERNEL);
394 if (!csme)
395 return -ENOMEM;
396
397 bo = xe_managed_bo_create_pin_map(xe, tile, GSC_PROXY_CHANNEL_SIZE,
398 XE_BO_FLAG_SYSTEM |
399 XE_BO_FLAG_GGTT);
400 if (IS_ERR(bo))
401 return PTR_ERR(bo);
402
403 gsc->proxy.bo = bo;
404 gsc->proxy.to_gsc = IOSYS_MAP_INIT_OFFSET(&bo->vmap, 0);
405 gsc->proxy.from_gsc = IOSYS_MAP_INIT_OFFSET(&bo->vmap, GSC_PROXY_BUFFER_SIZE);
406 gsc->proxy.to_csme = csme;
407 gsc->proxy.from_csme = csme + GSC_PROXY_BUFFER_SIZE;
408
409 return 0;
410 }
411
412 /**
413 * xe_gsc_proxy_init() - init objects and MEI component required by GSC proxy
414 * @gsc: the GSC uC
415 *
416 * Return: 0 if the initialization was successful, a negative errno otherwise.
417 */
xe_gsc_proxy_init(struct xe_gsc * gsc)418 int xe_gsc_proxy_init(struct xe_gsc *gsc)
419 {
420 int err;
421 struct xe_gt *gt = gsc_to_gt(gsc);
422 struct xe_tile *tile = gt_to_tile(gt);
423 struct xe_device *xe = tile_to_xe(tile);
424
425 mutex_init(&gsc->proxy.mutex);
426
427 if (!IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY)) {
428 xe_gt_info(gt, "can't init GSC proxy due to missing mei component\n");
429 return -ENODEV;
430 }
431
432 /* no multi-tile devices with this feature yet */
433 if (tile->id > 0) {
434 xe_gt_err(gt, "unexpected GSC proxy init on tile %u\n", tile->id);
435 return -EINVAL;
436 }
437
438 err = proxy_channel_alloc(gsc);
439 if (err)
440 return err;
441
442 err = component_add_typed(xe->drm.dev, &xe_gsc_proxy_component_ops,
443 I915_COMPONENT_GSC_PROXY);
444 if (err < 0) {
445 xe_gt_err(gt, "Failed to add GSC_PROXY component (%pe)\n", ERR_PTR(err));
446 return err;
447 }
448
449 gsc->proxy.component_added = true;
450
451 /* the component must be removed before unload, so can't use drmm for cleanup */
452
453 return 0;
454 }
455
456 /**
457 * xe_gsc_proxy_remove() - remove the GSC proxy MEI component
458 * @gsc: the GSC uC
459 */
xe_gsc_proxy_remove(struct xe_gsc * gsc)460 void xe_gsc_proxy_remove(struct xe_gsc *gsc)
461 {
462 struct xe_gt *gt = gsc_to_gt(gsc);
463 struct xe_device *xe = gt_to_xe(gt);
464 int err = 0;
465
466 if (!gsc->proxy.component_added)
467 return;
468
469 /* disable HECI2 IRQs */
470 xe_pm_runtime_get(xe);
471 err = xe_force_wake_get(gt_to_fw(gt), XE_FW_GSC);
472 if (err)
473 xe_gt_err(gt, "failed to get forcewake to disable GSC interrupts\n");
474
475 /* try do disable irq even if forcewake failed */
476 gsc_proxy_irq_toggle(gsc, false);
477
478 if (!err)
479 xe_force_wake_put(gt_to_fw(gt), XE_FW_GSC);
480 xe_pm_runtime_put(xe);
481
482 xe_gsc_wait_for_worker_completion(gsc);
483
484 component_del(xe->drm.dev, &xe_gsc_proxy_component_ops);
485 gsc->proxy.component_added = false;
486 }
487
488 /**
489 * xe_gsc_proxy_start() - start the proxy by submitting the first request
490 * @gsc: the GSC uC
491 *
492 * Return: 0 if the proxy are now enabled, a negative errno otherwise.
493 */
xe_gsc_proxy_start(struct xe_gsc * gsc)494 int xe_gsc_proxy_start(struct xe_gsc *gsc)
495 {
496 int err;
497
498 /* enable the proxy interrupt in the GSC shim layer */
499 gsc_proxy_irq_toggle(gsc, true);
500
501 /*
502 * The handling of the first proxy request must be manually triggered to
503 * notify the GSC that we're ready to support the proxy flow.
504 */
505 err = xe_gsc_proxy_request_handler(gsc);
506 if (err)
507 return err;
508
509 if (!xe_gsc_proxy_init_done(gsc)) {
510 xe_gt_err(gsc_to_gt(gsc), "GSC FW reports proxy init not completed\n");
511 return -EIO;
512 }
513
514 return 0;
515 }
516