1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2016-2019 Intel Corporation
4 */
5
6 #include <linux/circ_buf.h>
7 #include <linux/ktime.h>
8 #include <linux/string_helpers.h>
9 #include <linux/time64.h>
10 #include <linux/timekeeping.h>
11
12 #include "i915_drv.h"
13 #include "i915_wait_util.h"
14 #include "intel_guc_ct.h"
15 #include "intel_guc_print.h"
16
17 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
18 enum {
19 CT_DEAD_ALIVE = 0,
20 CT_DEAD_SETUP,
21 CT_DEAD_WRITE,
22 CT_DEAD_DEADLOCK,
23 CT_DEAD_H2G_HAS_ROOM,
24 CT_DEAD_READ,
25 CT_DEAD_PROCESS_FAILED,
26 };
27
28 static void ct_dead_ct_worker_func(struct work_struct *w);
29
30 #define CT_DEAD(ct, reason) \
31 do { \
32 if (!(ct)->dead_ct_reported) { \
33 (ct)->dead_ct_reason |= 1 << CT_DEAD_##reason; \
34 queue_work(system_unbound_wq, &(ct)->dead_ct_worker); \
35 } \
36 } while (0)
37 #else
38 #define CT_DEAD(ct, reason) do { } while (0)
39 #endif
40
ct_to_guc(struct intel_guc_ct * ct)41 static inline struct intel_guc *ct_to_guc(struct intel_guc_ct *ct)
42 {
43 return container_of(ct, struct intel_guc, ct);
44 }
45
46 #define CT_ERROR(_ct, _fmt, ...) \
47 guc_err(ct_to_guc(_ct), "CT: " _fmt, ##__VA_ARGS__)
48 #ifdef CONFIG_DRM_I915_DEBUG_GUC
49 #define CT_DEBUG(_ct, _fmt, ...) \
50 guc_dbg(ct_to_guc(_ct), "CT: " _fmt, ##__VA_ARGS__)
51 #else
52 #define CT_DEBUG(...) do { } while (0)
53 #endif
54 #define CT_PROBE_ERROR(_ct, _fmt, ...) \
55 guc_probe_error(ct_to_guc(ct), "CT: " _fmt, ##__VA_ARGS__)
56
57 /**
58 * DOC: CTB Blob
59 *
60 * We allocate single blob to hold both CTB descriptors and buffers:
61 *
62 * +--------+-----------------------------------------------+------+
63 * | offset | contents | size |
64 * +========+===============================================+======+
65 * | 0x0000 | H2G `CTB Descriptor`_ (send) | |
66 * +--------+-----------------------------------------------+ 4K |
67 * | 0x0800 | G2H `CTB Descriptor`_ (recv) | |
68 * +--------+-----------------------------------------------+------+
69 * | 0x1000 | H2G `CT Buffer`_ (send) | n*4K |
70 * | | | |
71 * +--------+-----------------------------------------------+------+
72 * | 0x1000 | G2H `CT Buffer`_ (recv) | m*4K |
73 * | + n*4K | | |
74 * +--------+-----------------------------------------------+------+
75 *
76 * Size of each `CT Buffer`_ must be multiple of 4K.
77 * We don't expect too many messages in flight at any time, unless we are
78 * using the GuC submission. In that case each request requires a minimum
79 * 2 dwords which gives us a maximum 256 queue'd requests. Hopefully this
80 * enough space to avoid backpressure on the driver. We increase the size
81 * of the receive buffer (relative to the send) to ensure a G2H response
82 * CTB has a landing spot.
83 */
84 #define CTB_DESC_SIZE ALIGN(sizeof(struct guc_ct_buffer_desc), SZ_2K)
85 #define CTB_H2G_BUFFER_SIZE (SZ_4K)
86 #define CTB_G2H_BUFFER_SIZE (4 * CTB_H2G_BUFFER_SIZE)
87 #define G2H_ROOM_BUFFER_SIZE (CTB_G2H_BUFFER_SIZE / 4)
88
89 struct ct_request {
90 struct list_head link;
91 u32 fence;
92 u32 status;
93 u32 response_len;
94 u32 *response_buf;
95 };
96
97 struct ct_incoming_msg {
98 struct list_head link;
99 u32 size;
100 u32 msg[] __counted_by(size);
101 };
102
103 enum { CTB_SEND = 0, CTB_RECV = 1 };
104
105 enum { CTB_OWNER_HOST = 0 };
106
107 /*
108 * Some H2G commands involve a synchronous response that the driver needs
109 * to wait for. In such cases, a timeout is required to prevent the driver
110 * from waiting forever in the case of an error (either no error response
111 * is defined in the protocol or something has died and requires a reset).
112 * The specific command may be defined as having a time bound response but
113 * the CT is a queue and that time guarantee only starts from the point
114 * when the command reaches the head of the queue and is processed by GuC.
115 *
116 * Ideally there would be a helper to report the progress of a given
117 * command through the CT. However, that would require a significant
118 * amount of work in the CT layer. In the meantime, provide a reasonable
119 * estimation of the worst case latency it should take for the entire
120 * queue to drain. And therefore, how long a caller should wait before
121 * giving up on their request. The current estimate is based on empirical
122 * measurement of a test that fills the buffer with context creation and
123 * destruction requests as they seem to be the slowest operation.
124 */
intel_guc_ct_max_queue_time_jiffies(void)125 long intel_guc_ct_max_queue_time_jiffies(void)
126 {
127 /*
128 * A 4KB buffer full of context destroy commands takes a little
129 * over a second to process so bump that to 2s to be super safe.
130 */
131 return (CTB_H2G_BUFFER_SIZE * HZ) / SZ_2K;
132 }
133
134 static void ct_receive_tasklet_func(struct tasklet_struct *t);
135 static void ct_incoming_request_worker_func(struct work_struct *w);
136
137 /**
138 * intel_guc_ct_init_early - Initialize CT state without requiring device access
139 * @ct: pointer to CT struct
140 */
intel_guc_ct_init_early(struct intel_guc_ct * ct)141 void intel_guc_ct_init_early(struct intel_guc_ct *ct)
142 {
143 spin_lock_init(&ct->ctbs.send.lock);
144 spin_lock_init(&ct->ctbs.recv.lock);
145 spin_lock_init(&ct->requests.lock);
146 INIT_LIST_HEAD(&ct->requests.pending);
147 INIT_LIST_HEAD(&ct->requests.incoming);
148 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
149 INIT_WORK(&ct->dead_ct_worker, ct_dead_ct_worker_func);
150 #endif
151 INIT_WORK(&ct->requests.worker, ct_incoming_request_worker_func);
152 tasklet_setup(&ct->receive_tasklet, ct_receive_tasklet_func);
153 init_waitqueue_head(&ct->wq);
154 }
155
guc_ct_buffer_desc_init(struct guc_ct_buffer_desc * desc)156 static void guc_ct_buffer_desc_init(struct guc_ct_buffer_desc *desc)
157 {
158 memset(desc, 0, sizeof(*desc));
159 }
160
guc_ct_buffer_reset(struct intel_guc_ct_buffer * ctb)161 static void guc_ct_buffer_reset(struct intel_guc_ct_buffer *ctb)
162 {
163 u32 space;
164
165 ctb->broken = false;
166 ctb->tail = 0;
167 ctb->head = 0;
168 space = CIRC_SPACE(ctb->tail, ctb->head, ctb->size) - ctb->resv_space;
169 atomic_set(&ctb->space, space);
170
171 guc_ct_buffer_desc_init(ctb->desc);
172 }
173
guc_ct_buffer_init(struct intel_guc_ct_buffer * ctb,struct guc_ct_buffer_desc * desc,u32 * cmds,u32 size_in_bytes,u32 resv_space)174 static void guc_ct_buffer_init(struct intel_guc_ct_buffer *ctb,
175 struct guc_ct_buffer_desc *desc,
176 u32 *cmds, u32 size_in_bytes, u32 resv_space)
177 {
178 GEM_BUG_ON(size_in_bytes % 4);
179
180 ctb->desc = desc;
181 ctb->cmds = cmds;
182 ctb->size = size_in_bytes / 4;
183 ctb->resv_space = resv_space / 4;
184
185 guc_ct_buffer_reset(ctb);
186 }
187
guc_action_control_ctb(struct intel_guc * guc,u32 control)188 static int guc_action_control_ctb(struct intel_guc *guc, u32 control)
189 {
190 u32 request[HOST2GUC_CONTROL_CTB_REQUEST_MSG_LEN] = {
191 FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
192 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) |
193 FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION, GUC_ACTION_HOST2GUC_CONTROL_CTB),
194 FIELD_PREP(HOST2GUC_CONTROL_CTB_REQUEST_MSG_1_CONTROL, control),
195 };
196 int ret;
197
198 GEM_BUG_ON(control != GUC_CTB_CONTROL_DISABLE && control != GUC_CTB_CONTROL_ENABLE);
199
200 /* CT control must go over MMIO */
201 ret = intel_guc_send_mmio(guc, request, ARRAY_SIZE(request), NULL, 0);
202
203 return ret > 0 ? -EPROTO : ret;
204 }
205
ct_control_enable(struct intel_guc_ct * ct,bool enable)206 static int ct_control_enable(struct intel_guc_ct *ct, bool enable)
207 {
208 int err;
209
210 err = guc_action_control_ctb(ct_to_guc(ct), enable ?
211 GUC_CTB_CONTROL_ENABLE : GUC_CTB_CONTROL_DISABLE);
212 if (unlikely(err))
213 CT_PROBE_ERROR(ct, "Failed to control/%s CTB (%pe)\n",
214 str_enable_disable(enable), ERR_PTR(err));
215
216 return err;
217 }
218
ct_register_buffer(struct intel_guc_ct * ct,bool send,u32 desc_addr,u32 buff_addr,u32 size)219 static int ct_register_buffer(struct intel_guc_ct *ct, bool send,
220 u32 desc_addr, u32 buff_addr, u32 size)
221 {
222 int err;
223
224 err = intel_guc_self_cfg64(ct_to_guc(ct), send ?
225 GUC_KLV_SELF_CFG_H2G_CTB_DESCRIPTOR_ADDR_KEY :
226 GUC_KLV_SELF_CFG_G2H_CTB_DESCRIPTOR_ADDR_KEY,
227 desc_addr);
228 if (unlikely(err))
229 goto failed;
230
231 err = intel_guc_self_cfg64(ct_to_guc(ct), send ?
232 GUC_KLV_SELF_CFG_H2G_CTB_ADDR_KEY :
233 GUC_KLV_SELF_CFG_G2H_CTB_ADDR_KEY,
234 buff_addr);
235 if (unlikely(err))
236 goto failed;
237
238 err = intel_guc_self_cfg32(ct_to_guc(ct), send ?
239 GUC_KLV_SELF_CFG_H2G_CTB_SIZE_KEY :
240 GUC_KLV_SELF_CFG_G2H_CTB_SIZE_KEY,
241 size);
242 if (unlikely(err))
243 failed:
244 CT_PROBE_ERROR(ct, "Failed to register %s buffer (%pe)\n",
245 send ? "SEND" : "RECV", ERR_PTR(err));
246
247 return err;
248 }
249
250 /**
251 * intel_guc_ct_init - Init buffer-based communication
252 * @ct: pointer to CT struct
253 *
254 * Allocate memory required for buffer-based communication.
255 *
256 * Return: 0 on success, a negative errno code on failure.
257 */
intel_guc_ct_init(struct intel_guc_ct * ct)258 int intel_guc_ct_init(struct intel_guc_ct *ct)
259 {
260 struct intel_guc *guc = ct_to_guc(ct);
261 struct guc_ct_buffer_desc *desc;
262 u32 blob_size;
263 u32 cmds_size;
264 u32 resv_space;
265 void *blob;
266 u32 *cmds;
267 int err;
268
269 GEM_BUG_ON(ct->vma);
270
271 blob_size = 2 * CTB_DESC_SIZE + CTB_H2G_BUFFER_SIZE + CTB_G2H_BUFFER_SIZE;
272 err = intel_guc_allocate_and_map_vma(guc, blob_size, &ct->vma, &blob);
273 if (unlikely(err)) {
274 CT_PROBE_ERROR(ct, "Failed to allocate %u for CTB data (%pe)\n",
275 blob_size, ERR_PTR(err));
276 return err;
277 }
278
279 CT_DEBUG(ct, "base=%#x size=%u\n", intel_guc_ggtt_offset(guc, ct->vma), blob_size);
280
281 /* store pointers to desc and cmds for send ctb */
282 desc = blob;
283 cmds = blob + 2 * CTB_DESC_SIZE;
284 cmds_size = CTB_H2G_BUFFER_SIZE;
285 resv_space = 0;
286 CT_DEBUG(ct, "%s desc %#tx cmds %#tx size %u/%u\n", "send",
287 ptrdiff(desc, blob), ptrdiff(cmds, blob), cmds_size,
288 resv_space);
289
290 guc_ct_buffer_init(&ct->ctbs.send, desc, cmds, cmds_size, resv_space);
291
292 /* store pointers to desc and cmds for recv ctb */
293 desc = blob + CTB_DESC_SIZE;
294 cmds = blob + 2 * CTB_DESC_SIZE + CTB_H2G_BUFFER_SIZE;
295 cmds_size = CTB_G2H_BUFFER_SIZE;
296 resv_space = G2H_ROOM_BUFFER_SIZE;
297 CT_DEBUG(ct, "%s desc %#tx cmds %#tx size %u/%u\n", "recv",
298 ptrdiff(desc, blob), ptrdiff(cmds, blob), cmds_size,
299 resv_space);
300
301 guc_ct_buffer_init(&ct->ctbs.recv, desc, cmds, cmds_size, resv_space);
302
303 return 0;
304 }
305 ALLOW_ERROR_INJECTION(intel_guc_ct_init, ERRNO);
306
307 /**
308 * intel_guc_ct_fini - Fini buffer-based communication
309 * @ct: pointer to CT struct
310 *
311 * Deallocate memory required for buffer-based communication.
312 */
intel_guc_ct_fini(struct intel_guc_ct * ct)313 void intel_guc_ct_fini(struct intel_guc_ct *ct)
314 {
315 GEM_BUG_ON(ct->enabled);
316
317 tasklet_kill(&ct->receive_tasklet);
318 i915_vma_unpin_and_release(&ct->vma, I915_VMA_RELEASE_MAP);
319 memset(ct, 0, sizeof(*ct));
320 }
321
322 /**
323 * intel_guc_ct_enable - Enable buffer based command transport.
324 * @ct: pointer to CT struct
325 *
326 * Return: 0 on success, a negative errno code on failure.
327 */
intel_guc_ct_enable(struct intel_guc_ct * ct)328 int intel_guc_ct_enable(struct intel_guc_ct *ct)
329 {
330 struct intel_guc *guc = ct_to_guc(ct);
331 u32 base, desc, cmds, size;
332 void *blob;
333 int err;
334
335 GEM_BUG_ON(ct->enabled);
336
337 /* vma should be already allocated and map'ed */
338 GEM_BUG_ON(!ct->vma);
339 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(ct->vma->obj));
340 base = intel_guc_ggtt_offset(guc, ct->vma);
341
342 /* blob should start with send descriptor */
343 blob = __px_vaddr(ct->vma->obj);
344 GEM_BUG_ON(blob != ct->ctbs.send.desc);
345
346 /* (re)initialize descriptors */
347 guc_ct_buffer_reset(&ct->ctbs.send);
348 guc_ct_buffer_reset(&ct->ctbs.recv);
349
350 /*
351 * Register both CT buffers starting with RECV buffer.
352 * Descriptors are in first half of the blob.
353 */
354 desc = base + ptrdiff(ct->ctbs.recv.desc, blob);
355 cmds = base + ptrdiff(ct->ctbs.recv.cmds, blob);
356 size = ct->ctbs.recv.size * 4;
357 err = ct_register_buffer(ct, false, desc, cmds, size);
358 if (unlikely(err))
359 goto err_out;
360
361 desc = base + ptrdiff(ct->ctbs.send.desc, blob);
362 cmds = base + ptrdiff(ct->ctbs.send.cmds, blob);
363 size = ct->ctbs.send.size * 4;
364 err = ct_register_buffer(ct, true, desc, cmds, size);
365 if (unlikely(err))
366 goto err_out;
367
368 err = ct_control_enable(ct, true);
369 if (unlikely(err))
370 goto err_out;
371
372 ct->enabled = true;
373 ct->stall_time = KTIME_MAX;
374 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
375 ct->dead_ct_reported = false;
376 ct->dead_ct_reason = CT_DEAD_ALIVE;
377 #endif
378
379 return 0;
380
381 err_out:
382 CT_PROBE_ERROR(ct, "Failed to enable CTB (%pe)\n", ERR_PTR(err));
383 CT_DEAD(ct, SETUP);
384 return err;
385 }
386
387 /**
388 * intel_guc_ct_disable - Disable buffer based command transport.
389 * @ct: pointer to CT struct
390 */
intel_guc_ct_disable(struct intel_guc_ct * ct)391 void intel_guc_ct_disable(struct intel_guc_ct *ct)
392 {
393 struct intel_guc *guc = ct_to_guc(ct);
394
395 GEM_BUG_ON(!ct->enabled);
396
397 ct->enabled = false;
398
399 if (intel_guc_is_fw_running(guc)) {
400 ct_control_enable(ct, false);
401 }
402 }
403
404 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
ct_track_lost_and_found(struct intel_guc_ct * ct,u32 fence,u32 action)405 static void ct_track_lost_and_found(struct intel_guc_ct *ct, u32 fence, u32 action)
406 {
407 unsigned int lost = fence % ARRAY_SIZE(ct->requests.lost_and_found);
408 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GUC)
409 unsigned long entries[SZ_32];
410 unsigned int n;
411
412 n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
413
414 /* May be called under spinlock, so avoid sleeping */
415 ct->requests.lost_and_found[lost].stack = stack_depot_save(entries, n, GFP_NOWAIT);
416 #endif
417 ct->requests.lost_and_found[lost].fence = fence;
418 ct->requests.lost_and_found[lost].action = action;
419 }
420 #endif
421
ct_get_next_fence(struct intel_guc_ct * ct)422 static u32 ct_get_next_fence(struct intel_guc_ct *ct)
423 {
424 /* For now it's trivial */
425 return ++ct->requests.last_fence;
426 }
427
ct_write(struct intel_guc_ct * ct,const u32 * action,u32 len,u32 fence,u32 flags)428 static int ct_write(struct intel_guc_ct *ct,
429 const u32 *action,
430 u32 len /* in dwords */,
431 u32 fence, u32 flags)
432 {
433 struct intel_guc_ct_buffer *ctb = &ct->ctbs.send;
434 struct guc_ct_buffer_desc *desc = ctb->desc;
435 u32 tail = ctb->tail;
436 u32 size = ctb->size;
437 u32 header;
438 u32 hxg;
439 u32 type;
440 u32 *cmds = ctb->cmds;
441 unsigned int i;
442
443 if (unlikely(desc->status))
444 goto corrupted;
445
446 GEM_BUG_ON(tail > size);
447
448 #ifdef CONFIG_DRM_I915_DEBUG_GUC
449 if (unlikely(tail != READ_ONCE(desc->tail))) {
450 CT_ERROR(ct, "Tail was modified %u != %u\n",
451 desc->tail, tail);
452 desc->status |= GUC_CTB_STATUS_MISMATCH;
453 goto corrupted;
454 }
455 if (unlikely(READ_ONCE(desc->head) >= size)) {
456 CT_ERROR(ct, "Invalid head offset %u >= %u)\n",
457 desc->head, size);
458 desc->status |= GUC_CTB_STATUS_OVERFLOW;
459 goto corrupted;
460 }
461 #endif
462
463 /*
464 * dw0: CT header (including fence)
465 * dw1: HXG header (including action code)
466 * dw2+: action data
467 */
468 header = FIELD_PREP(GUC_CTB_MSG_0_FORMAT, GUC_CTB_FORMAT_HXG) |
469 FIELD_PREP(GUC_CTB_MSG_0_NUM_DWORDS, len) |
470 FIELD_PREP(GUC_CTB_MSG_0_FENCE, fence);
471
472 type = (flags & INTEL_GUC_CT_SEND_NB) ? GUC_HXG_TYPE_FAST_REQUEST :
473 GUC_HXG_TYPE_REQUEST;
474 hxg = FIELD_PREP(GUC_HXG_MSG_0_TYPE, type) |
475 FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION |
476 GUC_HXG_REQUEST_MSG_0_DATA0, action[0]);
477
478 CT_DEBUG(ct, "writing (tail %u) %*ph %*ph %*ph\n",
479 tail, 4, &header, 4, &hxg, 4 * (len - 1), &action[1]);
480
481 cmds[tail] = header;
482 tail = (tail + 1) % size;
483
484 cmds[tail] = hxg;
485 tail = (tail + 1) % size;
486
487 for (i = 1; i < len; i++) {
488 cmds[tail] = action[i];
489 tail = (tail + 1) % size;
490 }
491 GEM_BUG_ON(tail > size);
492
493 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
494 ct_track_lost_and_found(ct, fence,
495 FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, action[0]));
496 #endif
497
498 /*
499 * make sure H2G buffer update and LRC tail update (if this triggering a
500 * submission) are visible before updating the descriptor tail
501 */
502 intel_guc_write_barrier(ct_to_guc(ct));
503
504 /* update local copies */
505 ctb->tail = tail;
506 GEM_BUG_ON(atomic_read(&ctb->space) < len + GUC_CTB_HDR_LEN);
507 atomic_sub(len + GUC_CTB_HDR_LEN, &ctb->space);
508
509 /* now update descriptor */
510 WRITE_ONCE(desc->tail, tail);
511
512 return 0;
513
514 corrupted:
515 CT_ERROR(ct, "Corrupted descriptor head=%u tail=%u status=%#x\n",
516 desc->head, desc->tail, desc->status);
517 CT_DEAD(ct, WRITE);
518 ctb->broken = true;
519 return -EPIPE;
520 }
521
522 /**
523 * wait_for_ct_request_update - Wait for CT request state update.
524 * @ct: pointer to CT
525 * @req: pointer to pending request
526 * @status: placeholder for status
527 *
528 * For each sent request, GuC shall send back CT response message.
529 * Our message handler will update status of tracked request once
530 * response message with given fence is received. Wait here and
531 * check for valid response status value.
532 *
533 * Return:
534 * * 0 response received (status is valid)
535 * * -ETIMEDOUT no response within hardcoded timeout
536 */
wait_for_ct_request_update(struct intel_guc_ct * ct,struct ct_request * req,u32 * status)537 static int wait_for_ct_request_update(struct intel_guc_ct *ct, struct ct_request *req, u32 *status)
538 {
539 int err;
540 bool ct_enabled;
541
542 /*
543 * Fast commands should complete in less than 10us, so sample quickly
544 * up to that length of time, then switch to a slower sleep-wait loop.
545 * No GuC command should ever take longer than 10ms but many GuC
546 * commands can be inflight at time, so use a 1s timeout on the slower
547 * sleep-wait loop.
548 */
549 #define GUC_CTB_RESPONSE_TIMEOUT_SHORT_MS 10
550 #define GUC_CTB_RESPONSE_TIMEOUT_LONG_MS 1000
551 #define done \
552 (!(ct_enabled = intel_guc_ct_enabled(ct)) || \
553 FIELD_GET(GUC_HXG_MSG_0_ORIGIN, READ_ONCE(req->status)) == \
554 GUC_HXG_ORIGIN_GUC)
555 err = wait_for_us(done, GUC_CTB_RESPONSE_TIMEOUT_SHORT_MS);
556 if (err)
557 err = wait_for(done, GUC_CTB_RESPONSE_TIMEOUT_LONG_MS);
558 #undef done
559 if (!ct_enabled)
560 err = -ENODEV;
561
562 *status = req->status;
563 return err;
564 }
565
566 #define GUC_CTB_TIMEOUT_MS 1500
ct_deadlocked(struct intel_guc_ct * ct)567 static inline bool ct_deadlocked(struct intel_guc_ct *ct)
568 {
569 long timeout = GUC_CTB_TIMEOUT_MS;
570 bool ret = ktime_ms_delta(ktime_get(), ct->stall_time) > timeout;
571
572 if (unlikely(ret)) {
573 struct guc_ct_buffer_desc *send = ct->ctbs.send.desc;
574 struct guc_ct_buffer_desc *recv = ct->ctbs.send.desc;
575
576 CT_ERROR(ct, "Communication stalled for %lld ms, desc status=%#x,%#x\n",
577 ktime_ms_delta(ktime_get(), ct->stall_time),
578 send->status, recv->status);
579 CT_ERROR(ct, "H2G Space: %u (Bytes)\n",
580 atomic_read(&ct->ctbs.send.space) * 4);
581 CT_ERROR(ct, "Head: %u (Dwords)\n", ct->ctbs.send.desc->head);
582 CT_ERROR(ct, "Tail: %u (Dwords)\n", ct->ctbs.send.desc->tail);
583 CT_ERROR(ct, "G2H Space: %u (Bytes)\n",
584 atomic_read(&ct->ctbs.recv.space) * 4);
585 CT_ERROR(ct, "Head: %u\n (Dwords)", ct->ctbs.recv.desc->head);
586 CT_ERROR(ct, "Tail: %u\n (Dwords)", ct->ctbs.recv.desc->tail);
587
588 CT_DEAD(ct, DEADLOCK);
589 ct->ctbs.send.broken = true;
590 }
591
592 return ret;
593 }
594
g2h_has_room(struct intel_guc_ct * ct,u32 g2h_len_dw)595 static inline bool g2h_has_room(struct intel_guc_ct *ct, u32 g2h_len_dw)
596 {
597 struct intel_guc_ct_buffer *ctb = &ct->ctbs.recv;
598
599 /*
600 * We leave a certain amount of space in the G2H CTB buffer for
601 * unexpected G2H CTBs (e.g. logging, engine hang, etc...)
602 */
603 return !g2h_len_dw || atomic_read(&ctb->space) >= g2h_len_dw;
604 }
605
g2h_reserve_space(struct intel_guc_ct * ct,u32 g2h_len_dw)606 static inline void g2h_reserve_space(struct intel_guc_ct *ct, u32 g2h_len_dw)
607 {
608 lockdep_assert_held(&ct->ctbs.send.lock);
609
610 GEM_BUG_ON(!g2h_has_room(ct, g2h_len_dw));
611
612 if (g2h_len_dw)
613 atomic_sub(g2h_len_dw, &ct->ctbs.recv.space);
614 }
615
g2h_release_space(struct intel_guc_ct * ct,u32 g2h_len_dw)616 static inline void g2h_release_space(struct intel_guc_ct *ct, u32 g2h_len_dw)
617 {
618 atomic_add(g2h_len_dw, &ct->ctbs.recv.space);
619 }
620
h2g_has_room(struct intel_guc_ct * ct,u32 len_dw)621 static inline bool h2g_has_room(struct intel_guc_ct *ct, u32 len_dw)
622 {
623 struct intel_guc_ct_buffer *ctb = &ct->ctbs.send;
624 struct guc_ct_buffer_desc *desc = ctb->desc;
625 u32 head;
626 u32 space;
627
628 if (atomic_read(&ctb->space) >= len_dw)
629 return true;
630
631 head = READ_ONCE(desc->head);
632 if (unlikely(head > ctb->size)) {
633 CT_ERROR(ct, "Invalid head offset %u >= %u)\n",
634 head, ctb->size);
635 desc->status |= GUC_CTB_STATUS_OVERFLOW;
636 ctb->broken = true;
637 CT_DEAD(ct, H2G_HAS_ROOM);
638 return false;
639 }
640
641 space = CIRC_SPACE(ctb->tail, head, ctb->size);
642 atomic_set(&ctb->space, space);
643
644 return space >= len_dw;
645 }
646
has_room_nb(struct intel_guc_ct * ct,u32 h2g_dw,u32 g2h_dw)647 static int has_room_nb(struct intel_guc_ct *ct, u32 h2g_dw, u32 g2h_dw)
648 {
649 bool h2g = h2g_has_room(ct, h2g_dw);
650 bool g2h = g2h_has_room(ct, g2h_dw);
651
652 lockdep_assert_held(&ct->ctbs.send.lock);
653
654 if (unlikely(!h2g || !g2h)) {
655 if (ct->stall_time == KTIME_MAX)
656 ct->stall_time = ktime_get();
657
658 /* Be paranoid and kick G2H tasklet to free credits */
659 if (!g2h)
660 tasklet_hi_schedule(&ct->receive_tasklet);
661
662 if (unlikely(ct_deadlocked(ct)))
663 return -EPIPE;
664 else
665 return -EBUSY;
666 }
667
668 ct->stall_time = KTIME_MAX;
669 return 0;
670 }
671
672 #define G2H_LEN_DW(f) ({ \
673 typeof(f) f_ = (f); \
674 FIELD_GET(INTEL_GUC_CT_SEND_G2H_DW_MASK, f_) ? \
675 FIELD_GET(INTEL_GUC_CT_SEND_G2H_DW_MASK, f_) + \
676 GUC_CTB_HXG_MSG_MIN_LEN : 0; \
677 })
ct_send_nb(struct intel_guc_ct * ct,const u32 * action,u32 len,u32 flags)678 static int ct_send_nb(struct intel_guc_ct *ct,
679 const u32 *action,
680 u32 len,
681 u32 flags)
682 {
683 struct intel_guc_ct_buffer *ctb = &ct->ctbs.send;
684 unsigned long spin_flags;
685 u32 g2h_len_dw = G2H_LEN_DW(flags);
686 u32 fence;
687 int ret;
688
689 spin_lock_irqsave(&ctb->lock, spin_flags);
690
691 ret = has_room_nb(ct, len + GUC_CTB_HDR_LEN, g2h_len_dw);
692 if (unlikely(ret))
693 goto out;
694
695 fence = ct_get_next_fence(ct);
696 ret = ct_write(ct, action, len, fence, flags);
697 if (unlikely(ret))
698 goto out;
699
700 g2h_reserve_space(ct, g2h_len_dw);
701 intel_guc_notify(ct_to_guc(ct));
702
703 out:
704 spin_unlock_irqrestore(&ctb->lock, spin_flags);
705
706 return ret;
707 }
708
ct_send(struct intel_guc_ct * ct,const u32 * action,u32 len,u32 * response_buf,u32 response_buf_size,u32 * status)709 static int ct_send(struct intel_guc_ct *ct,
710 const u32 *action,
711 u32 len,
712 u32 *response_buf,
713 u32 response_buf_size,
714 u32 *status)
715 {
716 struct intel_guc_ct_buffer *ctb = &ct->ctbs.send;
717 struct ct_request request;
718 unsigned long flags;
719 unsigned int sleep_period_ms = 1;
720 bool send_again;
721 u32 fence;
722 int err;
723
724 GEM_BUG_ON(!ct->enabled);
725 GEM_BUG_ON(!len);
726 GEM_BUG_ON(len > GUC_CTB_HXG_MSG_MAX_LEN - GUC_CTB_HDR_LEN);
727 GEM_BUG_ON(!response_buf && response_buf_size);
728 might_sleep();
729
730 resend:
731 send_again = false;
732
733 /*
734 * We use a lazy spin wait loop here as we believe that if the CT
735 * buffers are sized correctly the flow control condition should be
736 * rare. Reserving the maximum size in the G2H credits as we don't know
737 * how big the response is going to be.
738 */
739 retry:
740 spin_lock_irqsave(&ctb->lock, flags);
741 if (unlikely(!h2g_has_room(ct, len + GUC_CTB_HDR_LEN) ||
742 !g2h_has_room(ct, GUC_CTB_HXG_MSG_MAX_LEN))) {
743 if (ct->stall_time == KTIME_MAX)
744 ct->stall_time = ktime_get();
745 spin_unlock_irqrestore(&ctb->lock, flags);
746
747 if (unlikely(ct_deadlocked(ct)))
748 return -EPIPE;
749
750 if (msleep_interruptible(sleep_period_ms))
751 return -EINTR;
752 sleep_period_ms = sleep_period_ms << 1;
753
754 goto retry;
755 }
756
757 ct->stall_time = KTIME_MAX;
758
759 fence = ct_get_next_fence(ct);
760 request.fence = fence;
761 request.status = 0;
762 request.response_len = response_buf_size;
763 request.response_buf = response_buf;
764
765 spin_lock(&ct->requests.lock);
766 list_add_tail(&request.link, &ct->requests.pending);
767 spin_unlock(&ct->requests.lock);
768
769 err = ct_write(ct, action, len, fence, 0);
770 g2h_reserve_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
771
772 spin_unlock_irqrestore(&ctb->lock, flags);
773
774 if (unlikely(err))
775 goto unlink;
776
777 intel_guc_notify(ct_to_guc(ct));
778
779 err = wait_for_ct_request_update(ct, &request, status);
780 g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
781 if (unlikely(err)) {
782 if (err == -ENODEV)
783 /* wait_for_ct_request_update returns -ENODEV on reset/suspend in progress.
784 * In this case, output is debug rather than error info
785 */
786 CT_DEBUG(ct, "Request %#x (fence %u) cancelled as CTB is disabled\n",
787 action[0], request.fence);
788 else
789 CT_ERROR(ct, "No response for request %#x (fence %u)\n",
790 action[0], request.fence);
791 goto unlink;
792 }
793
794 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, *status) == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
795 CT_DEBUG(ct, "retrying request %#x (%u)\n", *action,
796 FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, *status));
797 send_again = true;
798 goto unlink;
799 }
800
801 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, *status) != GUC_HXG_TYPE_RESPONSE_SUCCESS) {
802 err = -EIO;
803 goto unlink;
804 }
805
806 if (response_buf) {
807 /* There shall be no data in the status */
808 WARN_ON(FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, request.status));
809 /* Return actual response len */
810 err = request.response_len;
811 } else {
812 /* There shall be no response payload */
813 WARN_ON(request.response_len);
814 /* Return data decoded from the status dword */
815 err = FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, *status);
816 }
817
818 unlink:
819 spin_lock_irqsave(&ct->requests.lock, flags);
820 list_del(&request.link);
821 spin_unlock_irqrestore(&ct->requests.lock, flags);
822
823 if (unlikely(send_again))
824 goto resend;
825
826 return err;
827 }
828
829 /*
830 * Command Transport (CT) buffer based GuC send function.
831 */
intel_guc_ct_send(struct intel_guc_ct * ct,const u32 * action,u32 len,u32 * response_buf,u32 response_buf_size,u32 flags)832 int intel_guc_ct_send(struct intel_guc_ct *ct, const u32 *action, u32 len,
833 u32 *response_buf, u32 response_buf_size, u32 flags)
834 {
835 u32 status = ~0; /* undefined */
836 int ret;
837
838 if (unlikely(!ct->enabled)) {
839 struct intel_guc *guc = ct_to_guc(ct);
840 struct intel_uc *uc = container_of(guc, struct intel_uc, guc);
841
842 WARN(!uc->reset_in_progress, "Unexpected send: action=%#x\n", *action);
843 return -ENODEV;
844 }
845
846 if (unlikely(ct->ctbs.send.broken))
847 return -EPIPE;
848
849 if (flags & INTEL_GUC_CT_SEND_NB)
850 return ct_send_nb(ct, action, len, flags);
851
852 ret = ct_send(ct, action, len, response_buf, response_buf_size, &status);
853 if (unlikely(ret < 0)) {
854 if (ret != -ENODEV)
855 CT_ERROR(ct, "Sending action %#x failed (%pe) status=%#X\n",
856 action[0], ERR_PTR(ret), status);
857 } else if (unlikely(ret)) {
858 CT_DEBUG(ct, "send action %#x returned %d (%#x)\n",
859 action[0], ret, ret);
860 }
861
862 return ret;
863 }
864
ct_alloc_msg(u32 num_dwords)865 static struct ct_incoming_msg *ct_alloc_msg(u32 num_dwords)
866 {
867 struct ct_incoming_msg *msg;
868
869 msg = kmalloc_flex(*msg, msg, num_dwords, GFP_ATOMIC);
870 if (msg)
871 msg->size = num_dwords;
872 return msg;
873 }
874
ct_free_msg(struct ct_incoming_msg * msg)875 static void ct_free_msg(struct ct_incoming_msg *msg)
876 {
877 kfree(msg);
878 }
879
880 /*
881 * Return: number available remaining dwords to read (0 if empty)
882 * or a negative error code on failure
883 */
ct_read(struct intel_guc_ct * ct,struct ct_incoming_msg ** msg)884 static int ct_read(struct intel_guc_ct *ct, struct ct_incoming_msg **msg)
885 {
886 struct intel_guc_ct_buffer *ctb = &ct->ctbs.recv;
887 struct guc_ct_buffer_desc *desc = ctb->desc;
888 u32 head = ctb->head;
889 u32 tail = READ_ONCE(desc->tail);
890 u32 size = ctb->size;
891 u32 *cmds = ctb->cmds;
892 s32 available;
893 unsigned int len;
894 unsigned int i;
895 u32 header;
896
897 if (unlikely(ctb->broken))
898 return -EPIPE;
899
900 if (unlikely(desc->status)) {
901 u32 status = desc->status;
902
903 if (status & GUC_CTB_STATUS_UNUSED) {
904 /*
905 * Potentially valid if a CLIENT_RESET request resulted in
906 * contexts/engines being reset. But should never happen as
907 * no contexts should be active when CLIENT_RESET is sent.
908 */
909 CT_ERROR(ct, "Unexpected G2H after GuC has stopped!\n");
910 status &= ~GUC_CTB_STATUS_UNUSED;
911 }
912
913 if (status)
914 goto corrupted;
915 }
916
917 GEM_BUG_ON(head > size);
918
919 #ifdef CONFIG_DRM_I915_DEBUG_GUC
920 if (unlikely(head != READ_ONCE(desc->head))) {
921 CT_ERROR(ct, "Head was modified %u != %u\n",
922 desc->head, head);
923 desc->status |= GUC_CTB_STATUS_MISMATCH;
924 goto corrupted;
925 }
926 #endif
927 if (unlikely(tail >= size)) {
928 CT_ERROR(ct, "Invalid tail offset %u >= %u)\n",
929 tail, size);
930 desc->status |= GUC_CTB_STATUS_OVERFLOW;
931 goto corrupted;
932 }
933
934 /* tail == head condition indicates empty */
935 available = tail - head;
936 if (unlikely(available == 0)) {
937 *msg = NULL;
938 return 0;
939 }
940
941 /* beware of buffer wrap case */
942 if (unlikely(available < 0))
943 available += size;
944 CT_DEBUG(ct, "available %d (%u:%u:%u)\n", available, head, tail, size);
945 GEM_BUG_ON(available < 0);
946
947 header = cmds[head];
948 head = (head + 1) % size;
949
950 /* message len with header */
951 len = FIELD_GET(GUC_CTB_MSG_0_NUM_DWORDS, header) + GUC_CTB_MSG_MIN_LEN;
952 if (unlikely(len > (u32)available)) {
953 CT_ERROR(ct, "Incomplete message %*ph %*ph %*ph\n",
954 4, &header,
955 4 * (head + available - 1 > size ?
956 size - head : available - 1), &cmds[head],
957 4 * (head + available - 1 > size ?
958 available - 1 - size + head : 0), &cmds[0]);
959 desc->status |= GUC_CTB_STATUS_UNDERFLOW;
960 goto corrupted;
961 }
962
963 *msg = ct_alloc_msg(len);
964 if (!*msg) {
965 CT_ERROR(ct, "No memory for message %*ph %*ph %*ph\n",
966 4, &header,
967 4 * (head + available - 1 > size ?
968 size - head : available - 1), &cmds[head],
969 4 * (head + available - 1 > size ?
970 available - 1 - size + head : 0), &cmds[0]);
971 return available;
972 }
973
974 (*msg)->msg[0] = header;
975
976 for (i = 1; i < len; i++) {
977 (*msg)->msg[i] = cmds[head];
978 head = (head + 1) % size;
979 }
980 CT_DEBUG(ct, "received %*ph\n", 4 * len, (*msg)->msg);
981
982 /* update local copies */
983 ctb->head = head;
984
985 /* now update descriptor */
986 WRITE_ONCE(desc->head, head);
987
988 intel_guc_write_barrier(ct_to_guc(ct));
989
990 return available - len;
991
992 corrupted:
993 CT_ERROR(ct, "Corrupted descriptor head=%u tail=%u status=%#x\n",
994 desc->head, desc->tail, desc->status);
995 ctb->broken = true;
996 CT_DEAD(ct, READ);
997 return -EPIPE;
998 }
999
1000 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
ct_check_lost_and_found(struct intel_guc_ct * ct,u32 fence)1001 static bool ct_check_lost_and_found(struct intel_guc_ct *ct, u32 fence)
1002 {
1003 unsigned int n;
1004 char *buf = NULL;
1005 bool found = false;
1006
1007 lockdep_assert_held(&ct->requests.lock);
1008
1009 for (n = 0; n < ARRAY_SIZE(ct->requests.lost_and_found); n++) {
1010 if (ct->requests.lost_and_found[n].fence != fence)
1011 continue;
1012 found = true;
1013
1014 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GUC)
1015 buf = kmalloc(SZ_4K, GFP_NOWAIT);
1016 if (buf && stack_depot_snprint(ct->requests.lost_and_found[n].stack,
1017 buf, SZ_4K, 0)) {
1018 CT_ERROR(ct, "Fence %u was used by action %#04x sent at\n%s",
1019 fence, ct->requests.lost_and_found[n].action, buf);
1020 break;
1021 }
1022 #endif
1023 CT_ERROR(ct, "Fence %u was used by action %#04x\n",
1024 fence, ct->requests.lost_and_found[n].action);
1025 break;
1026 }
1027 kfree(buf);
1028 return found;
1029 }
1030 #else
ct_check_lost_and_found(struct intel_guc_ct * ct,u32 fence)1031 static bool ct_check_lost_and_found(struct intel_guc_ct *ct, u32 fence)
1032 {
1033 return false;
1034 }
1035 #endif
1036
ct_handle_response(struct intel_guc_ct * ct,struct ct_incoming_msg * response)1037 static int ct_handle_response(struct intel_guc_ct *ct, struct ct_incoming_msg *response)
1038 {
1039 u32 len = FIELD_GET(GUC_CTB_MSG_0_NUM_DWORDS, response->msg[0]);
1040 u32 fence = FIELD_GET(GUC_CTB_MSG_0_FENCE, response->msg[0]);
1041 const u32 *hxg = &response->msg[GUC_CTB_MSG_MIN_LEN];
1042 const u32 *data = &hxg[GUC_HXG_MSG_MIN_LEN];
1043 u32 datalen = len - GUC_HXG_MSG_MIN_LEN;
1044 struct ct_request *req;
1045 unsigned long flags;
1046 bool found = false;
1047 int err = 0;
1048
1049 GEM_BUG_ON(len < GUC_HXG_MSG_MIN_LEN);
1050 GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_ORIGIN, hxg[0]) != GUC_HXG_ORIGIN_GUC);
1051 GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_RESPONSE_SUCCESS &&
1052 FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_NO_RESPONSE_RETRY &&
1053 FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_RESPONSE_FAILURE);
1054
1055 CT_DEBUG(ct, "response fence %u status %#x\n", fence, hxg[0]);
1056
1057 spin_lock_irqsave(&ct->requests.lock, flags);
1058 list_for_each_entry(req, &ct->requests.pending, link) {
1059 if (unlikely(fence != req->fence)) {
1060 CT_DEBUG(ct, "request %u awaits response\n",
1061 req->fence);
1062 continue;
1063 }
1064 if (unlikely(datalen > req->response_len)) {
1065 CT_ERROR(ct, "Response %u too long (datalen %u > %u)\n",
1066 req->fence, datalen, req->response_len);
1067 datalen = min(datalen, req->response_len);
1068 err = -EMSGSIZE;
1069 }
1070 if (datalen)
1071 memcpy(req->response_buf, data, 4 * datalen);
1072 req->response_len = datalen;
1073 WRITE_ONCE(req->status, hxg[0]);
1074 found = true;
1075 break;
1076 }
1077
1078 #ifdef CONFIG_DRM_I915_SELFTEST
1079 if (!found && ct_to_guc(ct)->fast_response_selftest) {
1080 CT_DEBUG(ct, "Assuming unsolicited response due to FAST_REQUEST selftest\n");
1081 ct_to_guc(ct)->fast_response_selftest++;
1082 found = true;
1083 }
1084 #endif
1085
1086 if (!found) {
1087 CT_ERROR(ct, "Unsolicited response message: len %u, data %#x (fence %u, last %u)\n",
1088 len, hxg[0], fence, ct->requests.last_fence);
1089 if (!ct_check_lost_and_found(ct, fence)) {
1090 list_for_each_entry(req, &ct->requests.pending, link)
1091 CT_ERROR(ct, "request %u awaits response\n",
1092 req->fence);
1093 }
1094 err = -ENOKEY;
1095 }
1096 spin_unlock_irqrestore(&ct->requests.lock, flags);
1097
1098 if (unlikely(err))
1099 return err;
1100
1101 ct_free_msg(response);
1102 return 0;
1103 }
1104
ct_process_request(struct intel_guc_ct * ct,struct ct_incoming_msg * request)1105 static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *request)
1106 {
1107 struct intel_guc *guc = ct_to_guc(ct);
1108 const u32 *hxg;
1109 const u32 *payload;
1110 u32 hxg_len, action, len;
1111 int ret;
1112
1113 hxg = &request->msg[GUC_CTB_MSG_MIN_LEN];
1114 hxg_len = request->size - GUC_CTB_MSG_MIN_LEN;
1115 payload = &hxg[GUC_HXG_MSG_MIN_LEN];
1116 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]);
1117 len = hxg_len - GUC_HXG_MSG_MIN_LEN;
1118
1119 CT_DEBUG(ct, "request %x %*ph\n", action, 4 * len, payload);
1120
1121 switch (action) {
1122 case INTEL_GUC_ACTION_DEFAULT:
1123 ret = intel_guc_to_host_process_recv_msg(guc, payload, len);
1124 break;
1125 case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
1126 ret = intel_guc_deregister_done_process_msg(guc, payload,
1127 len);
1128 break;
1129 case INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_DONE:
1130 ret = intel_guc_sched_done_process_msg(guc, payload, len);
1131 break;
1132 case INTEL_GUC_ACTION_CONTEXT_RESET_NOTIFICATION:
1133 ret = intel_guc_context_reset_process_msg(guc, payload, len);
1134 break;
1135 case INTEL_GUC_ACTION_STATE_CAPTURE_NOTIFICATION:
1136 ret = intel_guc_error_capture_process_msg(guc, payload, len);
1137 if (unlikely(ret))
1138 CT_ERROR(ct, "error capture notification failed %x %*ph\n",
1139 action, 4 * len, payload);
1140 break;
1141 case INTEL_GUC_ACTION_ENGINE_FAILURE_NOTIFICATION:
1142 ret = intel_guc_engine_failure_process_msg(guc, payload, len);
1143 break;
1144 case INTEL_GUC_ACTION_NOTIFY_FLUSH_LOG_BUFFER_TO_FILE:
1145 intel_guc_log_handle_flush_event(&guc->log);
1146 ret = 0;
1147 break;
1148 case INTEL_GUC_ACTION_NOTIFY_CRASH_DUMP_POSTED:
1149 case INTEL_GUC_ACTION_NOTIFY_EXCEPTION:
1150 ret = intel_guc_crash_process_msg(guc, action);
1151 break;
1152 case INTEL_GUC_ACTION_TLB_INVALIDATION_DONE:
1153 ret = intel_guc_tlb_invalidation_done(guc, payload, len);
1154 break;
1155 default:
1156 ret = -EOPNOTSUPP;
1157 break;
1158 }
1159
1160 if (unlikely(ret)) {
1161 CT_ERROR(ct, "Failed to process request %04x (%pe)\n",
1162 action, ERR_PTR(ret));
1163 return ret;
1164 }
1165
1166 ct_free_msg(request);
1167 return 0;
1168 }
1169
ct_process_incoming_requests(struct intel_guc_ct * ct)1170 static bool ct_process_incoming_requests(struct intel_guc_ct *ct)
1171 {
1172 unsigned long flags;
1173 struct ct_incoming_msg *request;
1174 bool done;
1175 int err;
1176
1177 spin_lock_irqsave(&ct->requests.lock, flags);
1178 request = list_first_entry_or_null(&ct->requests.incoming,
1179 struct ct_incoming_msg, link);
1180 if (request)
1181 list_del(&request->link);
1182 done = !!list_empty(&ct->requests.incoming);
1183 spin_unlock_irqrestore(&ct->requests.lock, flags);
1184
1185 if (!request)
1186 return true;
1187
1188 err = ct_process_request(ct, request);
1189 if (unlikely(err)) {
1190 CT_ERROR(ct, "Failed to process CT message (%pe) %*ph\n",
1191 ERR_PTR(err), 4 * request->size, request->msg);
1192 CT_DEAD(ct, PROCESS_FAILED);
1193 ct_free_msg(request);
1194 }
1195
1196 return done;
1197 }
1198
ct_incoming_request_worker_func(struct work_struct * w)1199 static void ct_incoming_request_worker_func(struct work_struct *w)
1200 {
1201 struct intel_guc_ct *ct =
1202 container_of(w, struct intel_guc_ct, requests.worker);
1203 bool done;
1204
1205 do {
1206 done = ct_process_incoming_requests(ct);
1207 } while (!done);
1208 }
1209
ct_handle_event(struct intel_guc_ct * ct,struct ct_incoming_msg * request)1210 static int ct_handle_event(struct intel_guc_ct *ct, struct ct_incoming_msg *request)
1211 {
1212 const u32 *hxg = &request->msg[GUC_CTB_MSG_MIN_LEN];
1213 u32 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]);
1214 unsigned long flags;
1215
1216 GEM_BUG_ON(FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_EVENT);
1217
1218 /*
1219 * Adjusting the space must be done in IRQ or deadlock can occur as the
1220 * CTB processing in the below workqueue can send CTBs which creates a
1221 * circular dependency if the space was returned there.
1222 */
1223 switch (action) {
1224 case INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_DONE:
1225 case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
1226 case INTEL_GUC_ACTION_TLB_INVALIDATION_DONE:
1227 g2h_release_space(ct, request->size);
1228 }
1229
1230 /*
1231 * TLB invalidation responses must be handled immediately as processing
1232 * of other G2H notifications may be blocked by an invalidation request.
1233 */
1234 if (action == INTEL_GUC_ACTION_TLB_INVALIDATION_DONE)
1235 return ct_process_request(ct, request);
1236
1237 spin_lock_irqsave(&ct->requests.lock, flags);
1238 list_add_tail(&request->link, &ct->requests.incoming);
1239 spin_unlock_irqrestore(&ct->requests.lock, flags);
1240
1241 queue_work(system_unbound_wq, &ct->requests.worker);
1242 return 0;
1243 }
1244
ct_handle_hxg(struct intel_guc_ct * ct,struct ct_incoming_msg * msg)1245 static int ct_handle_hxg(struct intel_guc_ct *ct, struct ct_incoming_msg *msg)
1246 {
1247 u32 origin, type;
1248 u32 *hxg;
1249 int err;
1250
1251 if (unlikely(msg->size < GUC_CTB_HXG_MSG_MIN_LEN))
1252 return -EBADMSG;
1253
1254 hxg = &msg->msg[GUC_CTB_MSG_MIN_LEN];
1255
1256 origin = FIELD_GET(GUC_HXG_MSG_0_ORIGIN, hxg[0]);
1257 if (unlikely(origin != GUC_HXG_ORIGIN_GUC)) {
1258 err = -EPROTO;
1259 goto failed;
1260 }
1261
1262 type = FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]);
1263 switch (type) {
1264 case GUC_HXG_TYPE_EVENT:
1265 err = ct_handle_event(ct, msg);
1266 break;
1267 case GUC_HXG_TYPE_RESPONSE_SUCCESS:
1268 case GUC_HXG_TYPE_RESPONSE_FAILURE:
1269 case GUC_HXG_TYPE_NO_RESPONSE_RETRY:
1270 err = ct_handle_response(ct, msg);
1271 break;
1272 default:
1273 err = -EOPNOTSUPP;
1274 }
1275
1276 if (unlikely(err)) {
1277 failed:
1278 CT_ERROR(ct, "Failed to handle HXG message (%pe) %*ph\n",
1279 ERR_PTR(err), 4 * GUC_HXG_MSG_MIN_LEN, hxg);
1280 }
1281 return err;
1282 }
1283
ct_handle_msg(struct intel_guc_ct * ct,struct ct_incoming_msg * msg)1284 static void ct_handle_msg(struct intel_guc_ct *ct, struct ct_incoming_msg *msg)
1285 {
1286 u32 format = FIELD_GET(GUC_CTB_MSG_0_FORMAT, msg->msg[0]);
1287 int err;
1288
1289 if (format == GUC_CTB_FORMAT_HXG)
1290 err = ct_handle_hxg(ct, msg);
1291 else
1292 err = -EOPNOTSUPP;
1293
1294 if (unlikely(err)) {
1295 CT_ERROR(ct, "Failed to process CT message (%pe) %*ph\n",
1296 ERR_PTR(err), 4 * msg->size, msg->msg);
1297 ct_free_msg(msg);
1298 }
1299 }
1300
1301 /*
1302 * Return: number available remaining dwords to read (0 if empty)
1303 * or a negative error code on failure
1304 */
ct_receive(struct intel_guc_ct * ct)1305 static int ct_receive(struct intel_guc_ct *ct)
1306 {
1307 struct ct_incoming_msg *msg = NULL;
1308 unsigned long flags;
1309 int ret;
1310
1311 spin_lock_irqsave(&ct->ctbs.recv.lock, flags);
1312 ret = ct_read(ct, &msg);
1313 spin_unlock_irqrestore(&ct->ctbs.recv.lock, flags);
1314 if (ret < 0)
1315 return ret;
1316
1317 if (msg)
1318 ct_handle_msg(ct, msg);
1319
1320 return ret;
1321 }
1322
ct_try_receive_message(struct intel_guc_ct * ct)1323 static void ct_try_receive_message(struct intel_guc_ct *ct)
1324 {
1325 struct intel_guc *guc = ct_to_guc(ct);
1326 int ret;
1327
1328 if (!ct->enabled) {
1329 GEM_WARN_ON(!guc_to_gt(guc)->uc.reset_in_progress);
1330 return;
1331 }
1332
1333 /* When interrupt disabled, message handling is not expected */
1334 if (!guc->interrupts.enabled)
1335 return;
1336
1337 ret = ct_receive(ct);
1338 if (ret > 0)
1339 tasklet_hi_schedule(&ct->receive_tasklet);
1340 }
1341
ct_receive_tasklet_func(struct tasklet_struct * t)1342 static void ct_receive_tasklet_func(struct tasklet_struct *t)
1343 {
1344 struct intel_guc_ct *ct = from_tasklet(ct, t, receive_tasklet);
1345
1346 ct_try_receive_message(ct);
1347 }
1348
1349 /*
1350 * When we're communicating with the GuC over CT, GuC uses events
1351 * to notify us about new messages being posted on the RECV buffer.
1352 */
intel_guc_ct_event_handler(struct intel_guc_ct * ct)1353 void intel_guc_ct_event_handler(struct intel_guc_ct *ct)
1354 {
1355 if (unlikely(!ct->enabled)) {
1356 WARN(1, "Unexpected GuC event received while CT disabled!\n");
1357 return;
1358 }
1359
1360 ct_try_receive_message(ct);
1361 }
1362
intel_guc_ct_print_info(struct intel_guc_ct * ct,struct drm_printer * p)1363 void intel_guc_ct_print_info(struct intel_guc_ct *ct,
1364 struct drm_printer *p)
1365 {
1366 drm_printf(p, "CT %s\n", str_enabled_disabled(ct->enabled));
1367
1368 if (!ct->enabled)
1369 return;
1370
1371 drm_printf(p, "H2G Space: %u\n",
1372 atomic_read(&ct->ctbs.send.space) * 4);
1373 drm_printf(p, "Head: %u\n",
1374 ct->ctbs.send.desc->head);
1375 drm_printf(p, "Tail: %u\n",
1376 ct->ctbs.send.desc->tail);
1377 drm_printf(p, "G2H Space: %u\n",
1378 atomic_read(&ct->ctbs.recv.space) * 4);
1379 drm_printf(p, "Head: %u\n",
1380 ct->ctbs.recv.desc->head);
1381 drm_printf(p, "Tail: %u\n",
1382 ct->ctbs.recv.desc->tail);
1383 }
1384
1385 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
ct_dead_ct_worker_func(struct work_struct * w)1386 static void ct_dead_ct_worker_func(struct work_struct *w)
1387 {
1388 struct intel_guc_ct *ct = container_of(w, struct intel_guc_ct, dead_ct_worker);
1389 struct intel_guc *guc = ct_to_guc(ct);
1390
1391 if (ct->dead_ct_reported)
1392 return;
1393
1394 ct->dead_ct_reported = true;
1395
1396 guc_info(guc, "CTB is dead - reason=0x%X\n", ct->dead_ct_reason);
1397 intel_klog_error_capture(guc_to_gt(guc), (intel_engine_mask_t)~0U);
1398 }
1399 #endif
1400