• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_gt_pagefault.h"
7 
8 #include <linux/bitfield.h>
9 #include <linux/circ_buf.h>
10 
11 #include <drm/drm_exec.h>
12 #include <drm/drm_managed.h>
13 #include <drm/ttm/ttm_execbuf_util.h>
14 
15 #include "abi/guc_actions_abi.h"
16 #include "xe_bo.h"
17 #include "xe_gt.h"
18 #include "xe_gt_tlb_invalidation.h"
19 #include "xe_guc.h"
20 #include "xe_guc_ct.h"
21 #include "xe_migrate.h"
22 #include "xe_trace_bo.h"
23 #include "xe_vm.h"
24 
25 struct pagefault {
26 	u64 page_addr;
27 	u32 asid;
28 	u16 pdata;
29 	u8 vfid;
30 	u8 access_type;
31 	u8 fault_type;
32 	u8 fault_level;
33 	u8 engine_class;
34 	u8 engine_instance;
35 	u8 fault_unsuccessful;
36 	bool trva_fault;
37 };
38 
39 enum access_type {
40 	ACCESS_TYPE_READ = 0,
41 	ACCESS_TYPE_WRITE = 1,
42 	ACCESS_TYPE_ATOMIC = 2,
43 	ACCESS_TYPE_RESERVED = 3,
44 };
45 
46 enum fault_type {
47 	NOT_PRESENT = 0,
48 	WRITE_ACCESS_VIOLATION = 1,
49 	ATOMIC_ACCESS_VIOLATION = 2,
50 };
51 
52 struct acc {
53 	u64 va_range_base;
54 	u32 asid;
55 	u32 sub_granularity;
56 	u8 granularity;
57 	u8 vfid;
58 	u8 access_type;
59 	u8 engine_class;
60 	u8 engine_instance;
61 };
62 
access_is_atomic(enum access_type access_type)63 static bool access_is_atomic(enum access_type access_type)
64 {
65 	return access_type == ACCESS_TYPE_ATOMIC;
66 }
67 
vma_is_valid(struct xe_tile * tile,struct xe_vma * vma)68 static bool vma_is_valid(struct xe_tile *tile, struct xe_vma *vma)
69 {
70 	return BIT(tile->id) & vma->tile_present &&
71 		!(BIT(tile->id) & vma->tile_invalidated);
72 }
73 
vma_matches(struct xe_vma * vma,u64 page_addr)74 static bool vma_matches(struct xe_vma *vma, u64 page_addr)
75 {
76 	if (page_addr > xe_vma_end(vma) - 1 ||
77 	    page_addr + SZ_4K - 1 < xe_vma_start(vma))
78 		return false;
79 
80 	return true;
81 }
82 
lookup_vma(struct xe_vm * vm,u64 page_addr)83 static struct xe_vma *lookup_vma(struct xe_vm *vm, u64 page_addr)
84 {
85 	struct xe_vma *vma = NULL;
86 
87 	if (vm->usm.last_fault_vma) {   /* Fast lookup */
88 		if (vma_matches(vm->usm.last_fault_vma, page_addr))
89 			vma = vm->usm.last_fault_vma;
90 	}
91 	if (!vma)
92 		vma = xe_vm_find_overlapping_vma(vm, page_addr, SZ_4K);
93 
94 	return vma;
95 }
96 
xe_pf_begin(struct drm_exec * exec,struct xe_vma * vma,bool atomic,unsigned int id)97 static int xe_pf_begin(struct drm_exec *exec, struct xe_vma *vma,
98 		       bool atomic, unsigned int id)
99 {
100 	struct xe_bo *bo = xe_vma_bo(vma);
101 	struct xe_vm *vm = xe_vma_vm(vma);
102 	int err;
103 
104 	err = xe_vm_lock_vma(exec, vma);
105 	if (err)
106 		return err;
107 
108 	if (atomic && IS_DGFX(vm->xe)) {
109 		if (xe_vma_is_userptr(vma)) {
110 			err = -EACCES;
111 			return err;
112 		}
113 
114 		/* Migrate to VRAM, move should invalidate the VMA first */
115 		err = xe_bo_migrate(bo, XE_PL_VRAM0 + id);
116 		if (err)
117 			return err;
118 	} else if (bo) {
119 		/* Create backing store if needed */
120 		err = xe_bo_validate(bo, vm, true);
121 		if (err)
122 			return err;
123 	}
124 
125 	return 0;
126 }
127 
handle_vma_pagefault(struct xe_tile * tile,struct pagefault * pf,struct xe_vma * vma)128 static int handle_vma_pagefault(struct xe_tile *tile, struct pagefault *pf,
129 				struct xe_vma *vma)
130 {
131 	struct xe_vm *vm = xe_vma_vm(vma);
132 	struct drm_exec exec;
133 	struct dma_fence *fence;
134 	ktime_t end = 0;
135 	int err;
136 	bool atomic;
137 
138 	trace_xe_vma_pagefault(vma);
139 	atomic = access_is_atomic(pf->access_type);
140 
141 	/* Check if VMA is valid */
142 	if (vma_is_valid(tile, vma) && !atomic)
143 		return 0;
144 
145 retry_userptr:
146 	if (xe_vma_is_userptr(vma) &&
147 	    xe_vma_userptr_check_repin(to_userptr_vma(vma))) {
148 		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
149 
150 		err = xe_vma_userptr_pin_pages(uvma);
151 		if (err)
152 			return err;
153 	}
154 
155 	/* Lock VM and BOs dma-resv */
156 	drm_exec_init(&exec, 0, 0);
157 	drm_exec_until_all_locked(&exec) {
158 		err = xe_pf_begin(&exec, vma, atomic, tile->id);
159 		drm_exec_retry_on_contention(&exec);
160 		if (xe_vm_validate_should_retry(&exec, err, &end))
161 			err = -EAGAIN;
162 		if (err)
163 			goto unlock_dma_resv;
164 
165 		/* Bind VMA only to the GT that has faulted */
166 		trace_xe_vma_pf_bind(vma);
167 		fence = xe_vma_rebind(vm, vma, BIT(tile->id));
168 		if (IS_ERR(fence)) {
169 			err = PTR_ERR(fence);
170 			if (xe_vm_validate_should_retry(&exec, err, &end))
171 				err = -EAGAIN;
172 			goto unlock_dma_resv;
173 		}
174 	}
175 
176 	dma_fence_wait(fence, false);
177 	dma_fence_put(fence);
178 	vma->tile_invalidated &= ~BIT(tile->id);
179 
180 unlock_dma_resv:
181 	drm_exec_fini(&exec);
182 	if (err == -EAGAIN)
183 		goto retry_userptr;
184 
185 	return err;
186 }
187 
asid_to_vm(struct xe_device * xe,u32 asid)188 static struct xe_vm *asid_to_vm(struct xe_device *xe, u32 asid)
189 {
190 	struct xe_vm *vm;
191 
192 	down_read(&xe->usm.lock);
193 	vm = xa_load(&xe->usm.asid_to_vm, asid);
194 	if (vm && xe_vm_in_fault_mode(vm))
195 		xe_vm_get(vm);
196 	else
197 		vm = ERR_PTR(-EINVAL);
198 	up_read(&xe->usm.lock);
199 
200 	return vm;
201 }
202 
handle_pagefault(struct xe_gt * gt,struct pagefault * pf)203 static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
204 {
205 	struct xe_device *xe = gt_to_xe(gt);
206 	struct xe_tile *tile = gt_to_tile(gt);
207 	struct xe_vm *vm;
208 	struct xe_vma *vma = NULL;
209 	int err;
210 
211 	/* SW isn't expected to handle TRTT faults */
212 	if (pf->trva_fault)
213 		return -EFAULT;
214 
215 	vm = asid_to_vm(xe, pf->asid);
216 	if (IS_ERR(vm))
217 		return PTR_ERR(vm);
218 
219 	/*
220 	 * TODO: Change to read lock? Using write lock for simplicity.
221 	 */
222 	down_write(&vm->lock);
223 
224 	if (xe_vm_is_closed(vm)) {
225 		err = -ENOENT;
226 		goto unlock_vm;
227 	}
228 
229 	vma = lookup_vma(vm, pf->page_addr);
230 	if (!vma) {
231 		err = -EINVAL;
232 		goto unlock_vm;
233 	}
234 
235 	err = handle_vma_pagefault(tile, pf, vma);
236 
237 unlock_vm:
238 	if (!err)
239 		vm->usm.last_fault_vma = vma;
240 	up_write(&vm->lock);
241 	xe_vm_put(vm);
242 
243 	return err;
244 }
245 
send_pagefault_reply(struct xe_guc * guc,struct xe_guc_pagefault_reply * reply)246 static int send_pagefault_reply(struct xe_guc *guc,
247 				struct xe_guc_pagefault_reply *reply)
248 {
249 	u32 action[] = {
250 		XE_GUC_ACTION_PAGE_FAULT_RES_DESC,
251 		reply->dw0,
252 		reply->dw1,
253 	};
254 
255 	return xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
256 }
257 
print_pagefault(struct xe_device * xe,struct pagefault * pf)258 static void print_pagefault(struct xe_device *xe, struct pagefault *pf)
259 {
260 	drm_dbg(&xe->drm, "\n\tASID: %d\n"
261 		 "\tVFID: %d\n"
262 		 "\tPDATA: 0x%04x\n"
263 		 "\tFaulted Address: 0x%08x%08x\n"
264 		 "\tFaultType: %d\n"
265 		 "\tAccessType: %d\n"
266 		 "\tFaultLevel: %d\n"
267 		 "\tEngineClass: %d\n"
268 		 "\tEngineInstance: %d\n",
269 		 pf->asid, pf->vfid, pf->pdata, upper_32_bits(pf->page_addr),
270 		 lower_32_bits(pf->page_addr),
271 		 pf->fault_type, pf->access_type, pf->fault_level,
272 		 pf->engine_class, pf->engine_instance);
273 }
274 
275 #define PF_MSG_LEN_DW	4
276 
get_pagefault(struct pf_queue * pf_queue,struct pagefault * pf)277 static bool get_pagefault(struct pf_queue *pf_queue, struct pagefault *pf)
278 {
279 	const struct xe_guc_pagefault_desc *desc;
280 	bool ret = false;
281 
282 	spin_lock_irq(&pf_queue->lock);
283 	if (pf_queue->tail != pf_queue->head) {
284 		desc = (const struct xe_guc_pagefault_desc *)
285 			(pf_queue->data + pf_queue->tail);
286 
287 		pf->fault_level = FIELD_GET(PFD_FAULT_LEVEL, desc->dw0);
288 		pf->trva_fault = FIELD_GET(XE2_PFD_TRVA_FAULT, desc->dw0);
289 		pf->engine_class = FIELD_GET(PFD_ENG_CLASS, desc->dw0);
290 		pf->engine_instance = FIELD_GET(PFD_ENG_INSTANCE, desc->dw0);
291 		pf->pdata = FIELD_GET(PFD_PDATA_HI, desc->dw1) <<
292 			PFD_PDATA_HI_SHIFT;
293 		pf->pdata |= FIELD_GET(PFD_PDATA_LO, desc->dw0);
294 		pf->asid = FIELD_GET(PFD_ASID, desc->dw1);
295 		pf->vfid = FIELD_GET(PFD_VFID, desc->dw2);
296 		pf->access_type = FIELD_GET(PFD_ACCESS_TYPE, desc->dw2);
297 		pf->fault_type = FIELD_GET(PFD_FAULT_TYPE, desc->dw2);
298 		pf->page_addr = (u64)(FIELD_GET(PFD_VIRTUAL_ADDR_HI, desc->dw3)) <<
299 			PFD_VIRTUAL_ADDR_HI_SHIFT;
300 		pf->page_addr |= FIELD_GET(PFD_VIRTUAL_ADDR_LO, desc->dw2) <<
301 			PFD_VIRTUAL_ADDR_LO_SHIFT;
302 
303 		pf_queue->tail = (pf_queue->tail + PF_MSG_LEN_DW) %
304 			pf_queue->num_dw;
305 		ret = true;
306 	}
307 	spin_unlock_irq(&pf_queue->lock);
308 
309 	return ret;
310 }
311 
pf_queue_full(struct pf_queue * pf_queue)312 static bool pf_queue_full(struct pf_queue *pf_queue)
313 {
314 	lockdep_assert_held(&pf_queue->lock);
315 
316 	return CIRC_SPACE(pf_queue->head, pf_queue->tail,
317 			  pf_queue->num_dw) <=
318 		PF_MSG_LEN_DW;
319 }
320 
xe_guc_pagefault_handler(struct xe_guc * guc,u32 * msg,u32 len)321 int xe_guc_pagefault_handler(struct xe_guc *guc, u32 *msg, u32 len)
322 {
323 	struct xe_gt *gt = guc_to_gt(guc);
324 	struct xe_device *xe = gt_to_xe(gt);
325 	struct pf_queue *pf_queue;
326 	unsigned long flags;
327 	u32 asid;
328 	bool full;
329 
330 	if (unlikely(len != PF_MSG_LEN_DW))
331 		return -EPROTO;
332 
333 	asid = FIELD_GET(PFD_ASID, msg[1]);
334 	pf_queue = gt->usm.pf_queue + (asid % NUM_PF_QUEUE);
335 
336 	/*
337 	 * The below logic doesn't work unless PF_QUEUE_NUM_DW % PF_MSG_LEN_DW == 0
338 	 */
339 	xe_gt_assert(gt, !(pf_queue->num_dw % PF_MSG_LEN_DW));
340 
341 	spin_lock_irqsave(&pf_queue->lock, flags);
342 	full = pf_queue_full(pf_queue);
343 	if (!full) {
344 		memcpy(pf_queue->data + pf_queue->head, msg, len * sizeof(u32));
345 		pf_queue->head = (pf_queue->head + len) %
346 			pf_queue->num_dw;
347 		queue_work(gt->usm.pf_wq, &pf_queue->worker);
348 	} else {
349 		drm_warn(&xe->drm, "PF Queue full, shouldn't be possible");
350 	}
351 	spin_unlock_irqrestore(&pf_queue->lock, flags);
352 
353 	return full ? -ENOSPC : 0;
354 }
355 
356 #define USM_QUEUE_MAX_RUNTIME_MS	20
357 
pf_queue_work_func(struct work_struct * w)358 static void pf_queue_work_func(struct work_struct *w)
359 {
360 	struct pf_queue *pf_queue = container_of(w, struct pf_queue, worker);
361 	struct xe_gt *gt = pf_queue->gt;
362 	struct xe_device *xe = gt_to_xe(gt);
363 	struct xe_guc_pagefault_reply reply = {};
364 	struct pagefault pf = {};
365 	unsigned long threshold;
366 	int ret;
367 
368 	threshold = jiffies + msecs_to_jiffies(USM_QUEUE_MAX_RUNTIME_MS);
369 
370 	while (get_pagefault(pf_queue, &pf)) {
371 		ret = handle_pagefault(gt, &pf);
372 		if (unlikely(ret)) {
373 			print_pagefault(xe, &pf);
374 			pf.fault_unsuccessful = 1;
375 			drm_dbg(&xe->drm, "Fault response: Unsuccessful %d\n", ret);
376 		}
377 
378 		reply.dw0 = FIELD_PREP(PFR_VALID, 1) |
379 			FIELD_PREP(PFR_SUCCESS, pf.fault_unsuccessful) |
380 			FIELD_PREP(PFR_REPLY, PFR_ACCESS) |
381 			FIELD_PREP(PFR_DESC_TYPE, FAULT_RESPONSE_DESC) |
382 			FIELD_PREP(PFR_ASID, pf.asid);
383 
384 		reply.dw1 = FIELD_PREP(PFR_VFID, pf.vfid) |
385 			FIELD_PREP(PFR_ENG_INSTANCE, pf.engine_instance) |
386 			FIELD_PREP(PFR_ENG_CLASS, pf.engine_class) |
387 			FIELD_PREP(PFR_PDATA, pf.pdata);
388 
389 		send_pagefault_reply(&gt->uc.guc, &reply);
390 
391 		if (time_after(jiffies, threshold) &&
392 		    pf_queue->tail != pf_queue->head) {
393 			queue_work(gt->usm.pf_wq, w);
394 			break;
395 		}
396 	}
397 }
398 
399 static void acc_queue_work_func(struct work_struct *w);
400 
pagefault_fini(void * arg)401 static void pagefault_fini(void *arg)
402 {
403 	struct xe_gt *gt = arg;
404 	struct xe_device *xe = gt_to_xe(gt);
405 
406 	if (!xe->info.has_usm)
407 		return;
408 
409 	destroy_workqueue(gt->usm.acc_wq);
410 	destroy_workqueue(gt->usm.pf_wq);
411 }
412 
xe_alloc_pf_queue(struct xe_gt * gt,struct pf_queue * pf_queue)413 static int xe_alloc_pf_queue(struct xe_gt *gt, struct pf_queue *pf_queue)
414 {
415 	struct xe_device *xe = gt_to_xe(gt);
416 	xe_dss_mask_t all_dss;
417 	int num_dss, num_eus;
418 
419 	bitmap_or(all_dss, gt->fuse_topo.g_dss_mask, gt->fuse_topo.c_dss_mask,
420 		  XE_MAX_DSS_FUSE_BITS);
421 
422 	num_dss = bitmap_weight(all_dss, XE_MAX_DSS_FUSE_BITS);
423 	num_eus = bitmap_weight(gt->fuse_topo.eu_mask_per_dss,
424 				XE_MAX_EU_FUSE_BITS) * num_dss;
425 
426 	/*
427 	 * user can issue separate page faults per EU and per CS
428 	 *
429 	 * XXX: Multiplier required as compute UMD are getting PF queue errors
430 	 * without it. Follow on why this multiplier is required.
431 	 */
432 #define PF_MULTIPLIER	8
433 	pf_queue->num_dw =
434 		(num_eus + XE_NUM_HW_ENGINES) * PF_MSG_LEN_DW * PF_MULTIPLIER;
435 	pf_queue->num_dw = roundup_pow_of_two(pf_queue->num_dw);
436 #undef PF_MULTIPLIER
437 
438 	pf_queue->gt = gt;
439 	pf_queue->data = devm_kcalloc(xe->drm.dev, pf_queue->num_dw,
440 				      sizeof(u32), GFP_KERNEL);
441 	if (!pf_queue->data)
442 		return -ENOMEM;
443 
444 	spin_lock_init(&pf_queue->lock);
445 	INIT_WORK(&pf_queue->worker, pf_queue_work_func);
446 
447 	return 0;
448 }
449 
xe_gt_pagefault_init(struct xe_gt * gt)450 int xe_gt_pagefault_init(struct xe_gt *gt)
451 {
452 	struct xe_device *xe = gt_to_xe(gt);
453 	int i, ret = 0;
454 
455 	if (!xe->info.has_usm)
456 		return 0;
457 
458 	for (i = 0; i < NUM_PF_QUEUE; ++i) {
459 		ret = xe_alloc_pf_queue(gt, &gt->usm.pf_queue[i]);
460 		if (ret)
461 			return ret;
462 	}
463 	for (i = 0; i < NUM_ACC_QUEUE; ++i) {
464 		gt->usm.acc_queue[i].gt = gt;
465 		spin_lock_init(&gt->usm.acc_queue[i].lock);
466 		INIT_WORK(&gt->usm.acc_queue[i].worker, acc_queue_work_func);
467 	}
468 
469 	gt->usm.pf_wq = alloc_workqueue("xe_gt_page_fault_work_queue",
470 					WQ_UNBOUND | WQ_HIGHPRI, NUM_PF_QUEUE);
471 	if (!gt->usm.pf_wq)
472 		return -ENOMEM;
473 
474 	gt->usm.acc_wq = alloc_workqueue("xe_gt_access_counter_work_queue",
475 					 WQ_UNBOUND | WQ_HIGHPRI,
476 					 NUM_ACC_QUEUE);
477 	if (!gt->usm.acc_wq) {
478 		destroy_workqueue(gt->usm.pf_wq);
479 		return -ENOMEM;
480 	}
481 
482 	return devm_add_action_or_reset(xe->drm.dev, pagefault_fini, gt);
483 }
484 
xe_gt_pagefault_reset(struct xe_gt * gt)485 void xe_gt_pagefault_reset(struct xe_gt *gt)
486 {
487 	struct xe_device *xe = gt_to_xe(gt);
488 	int i;
489 
490 	if (!xe->info.has_usm)
491 		return;
492 
493 	for (i = 0; i < NUM_PF_QUEUE; ++i) {
494 		spin_lock_irq(&gt->usm.pf_queue[i].lock);
495 		gt->usm.pf_queue[i].head = 0;
496 		gt->usm.pf_queue[i].tail = 0;
497 		spin_unlock_irq(&gt->usm.pf_queue[i].lock);
498 	}
499 
500 	for (i = 0; i < NUM_ACC_QUEUE; ++i) {
501 		spin_lock(&gt->usm.acc_queue[i].lock);
502 		gt->usm.acc_queue[i].head = 0;
503 		gt->usm.acc_queue[i].tail = 0;
504 		spin_unlock(&gt->usm.acc_queue[i].lock);
505 	}
506 }
507 
granularity_in_byte(int val)508 static int granularity_in_byte(int val)
509 {
510 	switch (val) {
511 	case 0:
512 		return SZ_128K;
513 	case 1:
514 		return SZ_2M;
515 	case 2:
516 		return SZ_16M;
517 	case 3:
518 		return SZ_64M;
519 	default:
520 		return 0;
521 	}
522 }
523 
sub_granularity_in_byte(int val)524 static int sub_granularity_in_byte(int val)
525 {
526 	return (granularity_in_byte(val) / 32);
527 }
528 
print_acc(struct xe_device * xe,struct acc * acc)529 static void print_acc(struct xe_device *xe, struct acc *acc)
530 {
531 	drm_warn(&xe->drm, "Access counter request:\n"
532 		 "\tType: %s\n"
533 		 "\tASID: %d\n"
534 		 "\tVFID: %d\n"
535 		 "\tEngine: %d:%d\n"
536 		 "\tGranularity: 0x%x KB Region/ %d KB sub-granularity\n"
537 		 "\tSub_Granularity Vector: 0x%08x\n"
538 		 "\tVA Range base: 0x%016llx\n",
539 		 acc->access_type ? "AC_NTFY_VAL" : "AC_TRIG_VAL",
540 		 acc->asid, acc->vfid, acc->engine_class, acc->engine_instance,
541 		 granularity_in_byte(acc->granularity) / SZ_1K,
542 		 sub_granularity_in_byte(acc->granularity) / SZ_1K,
543 		 acc->sub_granularity, acc->va_range_base);
544 }
545 
get_acc_vma(struct xe_vm * vm,struct acc * acc)546 static struct xe_vma *get_acc_vma(struct xe_vm *vm, struct acc *acc)
547 {
548 	u64 page_va = acc->va_range_base + (ffs(acc->sub_granularity) - 1) *
549 		sub_granularity_in_byte(acc->granularity);
550 
551 	return xe_vm_find_overlapping_vma(vm, page_va, SZ_4K);
552 }
553 
handle_acc(struct xe_gt * gt,struct acc * acc)554 static int handle_acc(struct xe_gt *gt, struct acc *acc)
555 {
556 	struct xe_device *xe = gt_to_xe(gt);
557 	struct xe_tile *tile = gt_to_tile(gt);
558 	struct drm_exec exec;
559 	struct xe_vm *vm;
560 	struct xe_vma *vma;
561 	int ret = 0;
562 
563 	/* We only support ACC_TRIGGER at the moment */
564 	if (acc->access_type != ACC_TRIGGER)
565 		return -EINVAL;
566 
567 	vm = asid_to_vm(xe, acc->asid);
568 	if (IS_ERR(vm))
569 		return PTR_ERR(vm);
570 
571 	down_read(&vm->lock);
572 
573 	/* Lookup VMA */
574 	vma = get_acc_vma(vm, acc);
575 	if (!vma) {
576 		ret = -EINVAL;
577 		goto unlock_vm;
578 	}
579 
580 	trace_xe_vma_acc(vma);
581 
582 	/* Userptr or null can't be migrated, nothing to do */
583 	if (xe_vma_has_no_bo(vma))
584 		goto unlock_vm;
585 
586 	/* Lock VM and BOs dma-resv */
587 	drm_exec_init(&exec, 0, 0);
588 	drm_exec_until_all_locked(&exec) {
589 		ret = xe_pf_begin(&exec, vma, true, tile->id);
590 		drm_exec_retry_on_contention(&exec);
591 		if (ret)
592 			break;
593 	}
594 
595 	drm_exec_fini(&exec);
596 unlock_vm:
597 	up_read(&vm->lock);
598 	xe_vm_put(vm);
599 
600 	return ret;
601 }
602 
603 #define make_u64(hi__, low__)  ((u64)(hi__) << 32 | (u64)(low__))
604 
605 #define ACC_MSG_LEN_DW        4
606 
get_acc(struct acc_queue * acc_queue,struct acc * acc)607 static bool get_acc(struct acc_queue *acc_queue, struct acc *acc)
608 {
609 	const struct xe_guc_acc_desc *desc;
610 	bool ret = false;
611 
612 	spin_lock(&acc_queue->lock);
613 	if (acc_queue->tail != acc_queue->head) {
614 		desc = (const struct xe_guc_acc_desc *)
615 			(acc_queue->data + acc_queue->tail);
616 
617 		acc->granularity = FIELD_GET(ACC_GRANULARITY, desc->dw2);
618 		acc->sub_granularity = FIELD_GET(ACC_SUBG_HI, desc->dw1) << 31 |
619 			FIELD_GET(ACC_SUBG_LO, desc->dw0);
620 		acc->engine_class = FIELD_GET(ACC_ENG_CLASS, desc->dw1);
621 		acc->engine_instance = FIELD_GET(ACC_ENG_INSTANCE, desc->dw1);
622 		acc->asid =  FIELD_GET(ACC_ASID, desc->dw1);
623 		acc->vfid =  FIELD_GET(ACC_VFID, desc->dw2);
624 		acc->access_type = FIELD_GET(ACC_TYPE, desc->dw0);
625 		acc->va_range_base = make_u64(desc->dw3 & ACC_VIRTUAL_ADDR_RANGE_HI,
626 					      desc->dw2 & ACC_VIRTUAL_ADDR_RANGE_LO);
627 
628 		acc_queue->tail = (acc_queue->tail + ACC_MSG_LEN_DW) %
629 				  ACC_QUEUE_NUM_DW;
630 		ret = true;
631 	}
632 	spin_unlock(&acc_queue->lock);
633 
634 	return ret;
635 }
636 
acc_queue_work_func(struct work_struct * w)637 static void acc_queue_work_func(struct work_struct *w)
638 {
639 	struct acc_queue *acc_queue = container_of(w, struct acc_queue, worker);
640 	struct xe_gt *gt = acc_queue->gt;
641 	struct xe_device *xe = gt_to_xe(gt);
642 	struct acc acc = {};
643 	unsigned long threshold;
644 	int ret;
645 
646 	threshold = jiffies + msecs_to_jiffies(USM_QUEUE_MAX_RUNTIME_MS);
647 
648 	while (get_acc(acc_queue, &acc)) {
649 		ret = handle_acc(gt, &acc);
650 		if (unlikely(ret)) {
651 			print_acc(xe, &acc);
652 			drm_warn(&xe->drm, "ACC: Unsuccessful %d\n", ret);
653 		}
654 
655 		if (time_after(jiffies, threshold) &&
656 		    acc_queue->tail != acc_queue->head) {
657 			queue_work(gt->usm.acc_wq, w);
658 			break;
659 		}
660 	}
661 }
662 
acc_queue_full(struct acc_queue * acc_queue)663 static bool acc_queue_full(struct acc_queue *acc_queue)
664 {
665 	lockdep_assert_held(&acc_queue->lock);
666 
667 	return CIRC_SPACE(acc_queue->head, acc_queue->tail, ACC_QUEUE_NUM_DW) <=
668 		ACC_MSG_LEN_DW;
669 }
670 
xe_guc_access_counter_notify_handler(struct xe_guc * guc,u32 * msg,u32 len)671 int xe_guc_access_counter_notify_handler(struct xe_guc *guc, u32 *msg, u32 len)
672 {
673 	struct xe_gt *gt = guc_to_gt(guc);
674 	struct acc_queue *acc_queue;
675 	u32 asid;
676 	bool full;
677 
678 	/*
679 	 * The below logic doesn't work unless ACC_QUEUE_NUM_DW % ACC_MSG_LEN_DW == 0
680 	 */
681 	BUILD_BUG_ON(ACC_QUEUE_NUM_DW % ACC_MSG_LEN_DW);
682 
683 	if (unlikely(len != ACC_MSG_LEN_DW))
684 		return -EPROTO;
685 
686 	asid = FIELD_GET(ACC_ASID, msg[1]);
687 	acc_queue = &gt->usm.acc_queue[asid % NUM_ACC_QUEUE];
688 
689 	spin_lock(&acc_queue->lock);
690 	full = acc_queue_full(acc_queue);
691 	if (!full) {
692 		memcpy(acc_queue->data + acc_queue->head, msg,
693 		       len * sizeof(u32));
694 		acc_queue->head = (acc_queue->head + len) % ACC_QUEUE_NUM_DW;
695 		queue_work(gt->usm.acc_wq, &acc_queue->worker);
696 	} else {
697 		drm_warn(&gt_to_xe(gt)->drm, "ACC Queue full, dropping ACC");
698 	}
699 	spin_unlock(&acc_queue->lock);
700 
701 	return full ? -ENOSPC : 0;
702 }
703