xref: /openbmc/linux/drivers/accel/qaic/qaic_data.c (revision 840d9a813c8eaa5c55d86525e374a97ca5023b53)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved. */
4 /* Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved. */
5 
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/dma-buf.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/interrupt.h>
13 #include <linux/kref.h>
14 #include <linux/list.h>
15 #include <linux/math64.h>
16 #include <linux/mm.h>
17 #include <linux/moduleparam.h>
18 #include <linux/scatterlist.h>
19 #include <linux/spinlock.h>
20 #include <linux/srcu.h>
21 #include <linux/types.h>
22 #include <linux/uaccess.h>
23 #include <linux/wait.h>
24 #include <drm/drm_file.h>
25 #include <drm/drm_gem.h>
26 #include <drm/drm_prime.h>
27 #include <drm/drm_print.h>
28 #include <uapi/drm/qaic_accel.h>
29 
30 #include "qaic.h"
31 
32 #define SEM_VAL_MASK	GENMASK_ULL(11, 0)
33 #define SEM_INDEX_MASK	GENMASK_ULL(4, 0)
34 #define BULK_XFER	BIT(3)
35 #define GEN_COMPLETION	BIT(4)
36 #define INBOUND_XFER	1
37 #define OUTBOUND_XFER	2
38 #define REQHP_OFF	0x0 /* we read this */
39 #define REQTP_OFF	0x4 /* we write this */
40 #define RSPHP_OFF	0x8 /* we write this */
41 #define RSPTP_OFF	0xc /* we read this */
42 
43 #define ENCODE_SEM(val, index, sync, cmd, flags)			\
44 		({							\
45 			FIELD_PREP(GENMASK(11, 0), (val)) |		\
46 			FIELD_PREP(GENMASK(20, 16), (index)) |		\
47 			FIELD_PREP(BIT(22), (sync)) |			\
48 			FIELD_PREP(GENMASK(26, 24), (cmd)) |		\
49 			FIELD_PREP(GENMASK(30, 29), (flags)) |		\
50 			FIELD_PREP(BIT(31), (cmd) ? 1 : 0);		\
51 		})
52 #define NUM_EVENTS	128
53 #define NUM_DELAYS	10
54 
55 static unsigned int wait_exec_default_timeout_ms = 5000; /* 5 sec default */
56 module_param(wait_exec_default_timeout_ms, uint, 0600);
57 MODULE_PARM_DESC(wait_exec_default_timeout_ms, "Default timeout for DRM_IOCTL_QAIC_WAIT_BO");
58 
59 static unsigned int datapath_poll_interval_us = 100; /* 100 usec default */
60 module_param(datapath_poll_interval_us, uint, 0600);
61 MODULE_PARM_DESC(datapath_poll_interval_us,
62 		 "Amount of time to sleep between activity when datapath polling is enabled");
63 
64 struct dbc_req {
65 	/*
66 	 * A request ID is assigned to each memory handle going in DMA queue.
67 	 * As a single memory handle can enqueue multiple elements in DMA queue
68 	 * all of them will have the same request ID.
69 	 */
70 	__le16	req_id;
71 	/* Future use */
72 	__u8	seq_id;
73 	/*
74 	 * Special encoded variable
75 	 * 7	0 - Do not force to generate MSI after DMA is completed
76 	 *	1 - Force to generate MSI after DMA is completed
77 	 * 6:5	Reserved
78 	 * 4	1 - Generate completion element in the response queue
79 	 *	0 - No Completion Code
80 	 * 3	0 - DMA request is a Link list transfer
81 	 *	1 - DMA request is a Bulk transfer
82 	 * 2	Reserved
83 	 * 1:0	00 - No DMA transfer involved
84 	 *	01 - DMA transfer is part of inbound transfer
85 	 *	10 - DMA transfer has outbound transfer
86 	 *	11 - NA
87 	 */
88 	__u8	cmd;
89 	__le32	resv;
90 	/* Source address for the transfer */
91 	__le64	src_addr;
92 	/* Destination address for the transfer */
93 	__le64	dest_addr;
94 	/* Length of transfer request */
95 	__le32	len;
96 	__le32	resv2;
97 	/* Doorbell address */
98 	__le64	db_addr;
99 	/*
100 	 * Special encoded variable
101 	 * 7	1 - Doorbell(db) write
102 	 *	0 - No doorbell write
103 	 * 6:2	Reserved
104 	 * 1:0	00 - 32 bit access, db address must be aligned to 32bit-boundary
105 	 *	01 - 16 bit access, db address must be aligned to 16bit-boundary
106 	 *	10 - 8 bit access, db address must be aligned to 8bit-boundary
107 	 *	11 - Reserved
108 	 */
109 	__u8	db_len;
110 	__u8	resv3;
111 	__le16	resv4;
112 	/* 32 bit data written to doorbell address */
113 	__le32	db_data;
114 	/*
115 	 * Special encoded variable
116 	 * All the fields of sem_cmdX are passed from user and all are ORed
117 	 * together to form sem_cmd.
118 	 * 0:11		Semaphore value
119 	 * 15:12	Reserved
120 	 * 20:16	Semaphore index
121 	 * 21		Reserved
122 	 * 22		Semaphore Sync
123 	 * 23		Reserved
124 	 * 26:24	Semaphore command
125 	 * 28:27	Reserved
126 	 * 29		Semaphore DMA out bound sync fence
127 	 * 30		Semaphore DMA in bound sync fence
128 	 * 31		Enable semaphore command
129 	 */
130 	__le32	sem_cmd0;
131 	__le32	sem_cmd1;
132 	__le32	sem_cmd2;
133 	__le32	sem_cmd3;
134 } __packed;
135 
136 struct dbc_rsp {
137 	/* Request ID of the memory handle whose DMA transaction is completed */
138 	__le16	req_id;
139 	/* Status of the DMA transaction. 0 : Success otherwise failure */
140 	__le16	status;
141 } __packed;
142 
get_dbc_req_elem_size(void)143 inline int get_dbc_req_elem_size(void)
144 {
145 	return sizeof(struct dbc_req);
146 }
147 
get_dbc_rsp_elem_size(void)148 inline int get_dbc_rsp_elem_size(void)
149 {
150 	return sizeof(struct dbc_rsp);
151 }
152 
free_slice(struct kref * kref)153 static void free_slice(struct kref *kref)
154 {
155 	struct bo_slice *slice = container_of(kref, struct bo_slice, ref_count);
156 
157 	list_del(&slice->slice);
158 	drm_gem_object_put(&slice->bo->base);
159 	sg_free_table(slice->sgt);
160 	kfree(slice->sgt);
161 	kfree(slice->reqs);
162 	kfree(slice);
163 }
164 
clone_range_of_sgt_for_slice(struct qaic_device * qdev,struct sg_table ** sgt_out,struct sg_table * sgt_in,u64 size,u64 offset)165 static int clone_range_of_sgt_for_slice(struct qaic_device *qdev, struct sg_table **sgt_out,
166 					struct sg_table *sgt_in, u64 size, u64 offset)
167 {
168 	struct scatterlist *sg, *sgn, *sgf, *sgl;
169 	unsigned int len, nents, offf, offl;
170 	struct sg_table *sgt;
171 	size_t total_len;
172 	int ret, j;
173 
174 	/* find out number of relevant nents needed for this mem */
175 	total_len = 0;
176 	sgf = NULL;
177 	sgl = NULL;
178 	nents = 0;
179 	offf = 0;
180 	offl = 0;
181 
182 	size = size ? size : PAGE_SIZE;
183 	for_each_sgtable_dma_sg(sgt_in, sg, j) {
184 		len = sg_dma_len(sg);
185 
186 		if (!len)
187 			continue;
188 		if (offset >= total_len && offset < total_len + len) {
189 			sgf = sg;
190 			offf = offset - total_len;
191 		}
192 		if (sgf)
193 			nents++;
194 		if (offset + size >= total_len &&
195 		    offset + size <= total_len + len) {
196 			sgl = sg;
197 			offl = offset + size - total_len;
198 			break;
199 		}
200 		total_len += len;
201 	}
202 
203 	if (!sgf || !sgl) {
204 		ret = -EINVAL;
205 		goto out;
206 	}
207 
208 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
209 	if (!sgt) {
210 		ret = -ENOMEM;
211 		goto out;
212 	}
213 
214 	ret = sg_alloc_table(sgt, nents, GFP_KERNEL);
215 	if (ret)
216 		goto free_sgt;
217 
218 	/* copy relevant sg node and fix page and length */
219 	sgn = sgf;
220 	for_each_sgtable_dma_sg(sgt, sg, j) {
221 		memcpy(sg, sgn, sizeof(*sg));
222 		if (sgn == sgf) {
223 			sg_dma_address(sg) += offf;
224 			sg_dma_len(sg) -= offf;
225 			sg_set_page(sg, sg_page(sgn), sg_dma_len(sg), offf);
226 		} else {
227 			offf = 0;
228 		}
229 		if (sgn == sgl) {
230 			sg_dma_len(sg) = offl - offf;
231 			sg_set_page(sg, sg_page(sgn), offl - offf, offf);
232 			sg_mark_end(sg);
233 			break;
234 		}
235 		sgn = sg_next(sgn);
236 	}
237 
238 	*sgt_out = sgt;
239 	return ret;
240 
241 free_sgt:
242 	kfree(sgt);
243 out:
244 	*sgt_out = NULL;
245 	return ret;
246 }
247 
encode_reqs(struct qaic_device * qdev,struct bo_slice * slice,struct qaic_attach_slice_entry * req)248 static int encode_reqs(struct qaic_device *qdev, struct bo_slice *slice,
249 		       struct qaic_attach_slice_entry *req)
250 {
251 	__le64 db_addr = cpu_to_le64(req->db_addr);
252 	__le32 db_data = cpu_to_le32(req->db_data);
253 	struct scatterlist *sg;
254 	__u8 cmd = BULK_XFER;
255 	int presync_sem;
256 	u64 dev_addr;
257 	__u8 db_len;
258 	int i;
259 
260 	if (!slice->no_xfer)
261 		cmd |= (slice->dir == DMA_TO_DEVICE ? INBOUND_XFER : OUTBOUND_XFER);
262 
263 	if (req->db_len && !IS_ALIGNED(req->db_addr, req->db_len / 8))
264 		return -EINVAL;
265 
266 	presync_sem = req->sem0.presync + req->sem1.presync + req->sem2.presync + req->sem3.presync;
267 	if (presync_sem > 1)
268 		return -EINVAL;
269 
270 	presync_sem = req->sem0.presync << 0 | req->sem1.presync << 1 |
271 		      req->sem2.presync << 2 | req->sem3.presync << 3;
272 
273 	switch (req->db_len) {
274 	case 32:
275 		db_len = BIT(7);
276 		break;
277 	case 16:
278 		db_len = BIT(7) | 1;
279 		break;
280 	case 8:
281 		db_len = BIT(7) | 2;
282 		break;
283 	case 0:
284 		db_len = 0; /* doorbell is not active for this command */
285 		break;
286 	default:
287 		return -EINVAL; /* should never hit this */
288 	}
289 
290 	/*
291 	 * When we end up splitting up a single request (ie a buf slice) into
292 	 * multiple DMA requests, we have to manage the sync data carefully.
293 	 * There can only be one presync sem. That needs to be on every xfer
294 	 * so that the DMA engine doesn't transfer data before the receiver is
295 	 * ready. We only do the doorbell and postsync sems after the xfer.
296 	 * To guarantee previous xfers for the request are complete, we use a
297 	 * fence.
298 	 */
299 	dev_addr = req->dev_addr;
300 	for_each_sgtable_dma_sg(slice->sgt, sg, i) {
301 		slice->reqs[i].cmd = cmd;
302 		slice->reqs[i].src_addr = cpu_to_le64(slice->dir == DMA_TO_DEVICE ?
303 						      sg_dma_address(sg) : dev_addr);
304 		slice->reqs[i].dest_addr = cpu_to_le64(slice->dir == DMA_TO_DEVICE ?
305 						       dev_addr : sg_dma_address(sg));
306 		/*
307 		 * sg_dma_len(sg) returns size of a DMA segment, maximum DMA
308 		 * segment size is set to UINT_MAX by qaic and hence return
309 		 * values of sg_dma_len(sg) can never exceed u32 range. So,
310 		 * by down sizing we are not corrupting the value.
311 		 */
312 		slice->reqs[i].len = cpu_to_le32((u32)sg_dma_len(sg));
313 		switch (presync_sem) {
314 		case BIT(0):
315 			slice->reqs[i].sem_cmd0 = cpu_to_le32(ENCODE_SEM(req->sem0.val,
316 									 req->sem0.index,
317 									 req->sem0.presync,
318 									 req->sem0.cmd,
319 									 req->sem0.flags));
320 			break;
321 		case BIT(1):
322 			slice->reqs[i].sem_cmd1 = cpu_to_le32(ENCODE_SEM(req->sem1.val,
323 									 req->sem1.index,
324 									 req->sem1.presync,
325 									 req->sem1.cmd,
326 									 req->sem1.flags));
327 			break;
328 		case BIT(2):
329 			slice->reqs[i].sem_cmd2 = cpu_to_le32(ENCODE_SEM(req->sem2.val,
330 									 req->sem2.index,
331 									 req->sem2.presync,
332 									 req->sem2.cmd,
333 									 req->sem2.flags));
334 			break;
335 		case BIT(3):
336 			slice->reqs[i].sem_cmd3 = cpu_to_le32(ENCODE_SEM(req->sem3.val,
337 									 req->sem3.index,
338 									 req->sem3.presync,
339 									 req->sem3.cmd,
340 									 req->sem3.flags));
341 			break;
342 		}
343 		dev_addr += sg_dma_len(sg);
344 	}
345 	/* add post transfer stuff to last segment */
346 	i--;
347 	slice->reqs[i].cmd |= GEN_COMPLETION;
348 	slice->reqs[i].db_addr = db_addr;
349 	slice->reqs[i].db_len = db_len;
350 	slice->reqs[i].db_data = db_data;
351 	/*
352 	 * Add a fence if we have more than one request going to the hardware
353 	 * representing the entirety of the user request, and the user request
354 	 * has no presync condition.
355 	 * Fences are expensive, so we try to avoid them. We rely on the
356 	 * hardware behavior to avoid needing one when there is a presync
357 	 * condition. When a presync exists, all requests for that same
358 	 * presync will be queued into a fifo. Thus, since we queue the
359 	 * post xfer activity only on the last request we queue, the hardware
360 	 * will ensure that the last queued request is processed last, thus
361 	 * making sure the post xfer activity happens at the right time without
362 	 * a fence.
363 	 */
364 	if (i && !presync_sem)
365 		req->sem0.flags |= (slice->dir == DMA_TO_DEVICE ?
366 				    QAIC_SEM_INSYNCFENCE : QAIC_SEM_OUTSYNCFENCE);
367 	slice->reqs[i].sem_cmd0 = cpu_to_le32(ENCODE_SEM(req->sem0.val, req->sem0.index,
368 							 req->sem0.presync, req->sem0.cmd,
369 							 req->sem0.flags));
370 	slice->reqs[i].sem_cmd1 = cpu_to_le32(ENCODE_SEM(req->sem1.val, req->sem1.index,
371 							 req->sem1.presync, req->sem1.cmd,
372 							 req->sem1.flags));
373 	slice->reqs[i].sem_cmd2 = cpu_to_le32(ENCODE_SEM(req->sem2.val, req->sem2.index,
374 							 req->sem2.presync, req->sem2.cmd,
375 							 req->sem2.flags));
376 	slice->reqs[i].sem_cmd3 = cpu_to_le32(ENCODE_SEM(req->sem3.val, req->sem3.index,
377 							 req->sem3.presync, req->sem3.cmd,
378 							 req->sem3.flags));
379 
380 	return 0;
381 }
382 
qaic_map_one_slice(struct qaic_device * qdev,struct qaic_bo * bo,struct qaic_attach_slice_entry * slice_ent)383 static int qaic_map_one_slice(struct qaic_device *qdev, struct qaic_bo *bo,
384 			      struct qaic_attach_slice_entry *slice_ent)
385 {
386 	struct sg_table *sgt = NULL;
387 	struct bo_slice *slice;
388 	int ret;
389 
390 	ret = clone_range_of_sgt_for_slice(qdev, &sgt, bo->sgt, slice_ent->size, slice_ent->offset);
391 	if (ret)
392 		goto out;
393 
394 	slice = kmalloc(sizeof(*slice), GFP_KERNEL);
395 	if (!slice) {
396 		ret = -ENOMEM;
397 		goto free_sgt;
398 	}
399 
400 	slice->reqs = kcalloc(sgt->nents, sizeof(*slice->reqs), GFP_KERNEL);
401 	if (!slice->reqs) {
402 		ret = -ENOMEM;
403 		goto free_slice;
404 	}
405 
406 	slice->no_xfer = !slice_ent->size;
407 	slice->sgt = sgt;
408 	slice->nents = sgt->nents;
409 	slice->dir = bo->dir;
410 	slice->bo = bo;
411 	slice->size = slice_ent->size;
412 	slice->offset = slice_ent->offset;
413 
414 	ret = encode_reqs(qdev, slice, slice_ent);
415 	if (ret)
416 		goto free_req;
417 
418 	bo->total_slice_nents += sgt->nents;
419 	kref_init(&slice->ref_count);
420 	drm_gem_object_get(&bo->base);
421 	list_add_tail(&slice->slice, &bo->slices);
422 
423 	return 0;
424 
425 free_req:
426 	kfree(slice->reqs);
427 free_slice:
428 	kfree(slice);
429 free_sgt:
430 	sg_free_table(sgt);
431 	kfree(sgt);
432 out:
433 	return ret;
434 }
435 
create_sgt(struct qaic_device * qdev,struct sg_table ** sgt_out,u64 size)436 static int create_sgt(struct qaic_device *qdev, struct sg_table **sgt_out, u64 size)
437 {
438 	struct scatterlist *sg;
439 	struct sg_table *sgt;
440 	struct page **pages;
441 	int *pages_order;
442 	int buf_extra;
443 	int max_order;
444 	int nr_pages;
445 	int ret = 0;
446 	int i, j, k;
447 	int order;
448 
449 	if (size) {
450 		nr_pages = DIV_ROUND_UP(size, PAGE_SIZE);
451 		/*
452 		 * calculate how much extra we are going to allocate, to remove
453 		 * later
454 		 */
455 		buf_extra = (PAGE_SIZE - size % PAGE_SIZE) % PAGE_SIZE;
456 		max_order = min(MAX_ORDER - 1, get_order(size));
457 	} else {
458 		/* allocate a single page for book keeping */
459 		nr_pages = 1;
460 		buf_extra = 0;
461 		max_order = 0;
462 	}
463 
464 	pages = kvmalloc_array(nr_pages, sizeof(*pages) + sizeof(*pages_order), GFP_KERNEL);
465 	if (!pages) {
466 		ret = -ENOMEM;
467 		goto out;
468 	}
469 	pages_order = (void *)pages + sizeof(*pages) * nr_pages;
470 
471 	/*
472 	 * Allocate requested memory using alloc_pages. It is possible to allocate
473 	 * the requested memory in multiple chunks by calling alloc_pages
474 	 * multiple times. Use SG table to handle multiple allocated pages.
475 	 */
476 	i = 0;
477 	while (nr_pages > 0) {
478 		order = min(get_order(nr_pages * PAGE_SIZE), max_order);
479 		while (1) {
480 			pages[i] = alloc_pages(GFP_KERNEL | GFP_HIGHUSER |
481 					       __GFP_NOWARN | __GFP_ZERO |
482 					       (order ? __GFP_NORETRY : __GFP_RETRY_MAYFAIL),
483 					       order);
484 			if (pages[i])
485 				break;
486 			if (!order--) {
487 				ret = -ENOMEM;
488 				goto free_partial_alloc;
489 			}
490 		}
491 
492 		max_order = order;
493 		pages_order[i] = order;
494 
495 		nr_pages -= 1 << order;
496 		if (nr_pages <= 0)
497 			/* account for over allocation */
498 			buf_extra += abs(nr_pages) * PAGE_SIZE;
499 		i++;
500 	}
501 
502 	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
503 	if (!sgt) {
504 		ret = -ENOMEM;
505 		goto free_partial_alloc;
506 	}
507 
508 	if (sg_alloc_table(sgt, i, GFP_KERNEL)) {
509 		ret = -ENOMEM;
510 		goto free_sgt;
511 	}
512 
513 	/* Populate the SG table with the allocated memory pages */
514 	sg = sgt->sgl;
515 	for (k = 0; k < i; k++, sg = sg_next(sg)) {
516 		/* Last entry requires special handling */
517 		if (k < i - 1) {
518 			sg_set_page(sg, pages[k], PAGE_SIZE << pages_order[k], 0);
519 		} else {
520 			sg_set_page(sg, pages[k], (PAGE_SIZE << pages_order[k]) - buf_extra, 0);
521 			sg_mark_end(sg);
522 		}
523 	}
524 
525 	kvfree(pages);
526 	*sgt_out = sgt;
527 	return ret;
528 
529 free_sgt:
530 	kfree(sgt);
531 free_partial_alloc:
532 	for (j = 0; j < i; j++)
533 		__free_pages(pages[j], pages_order[j]);
534 	kvfree(pages);
535 out:
536 	*sgt_out = NULL;
537 	return ret;
538 }
539 
invalid_sem(struct qaic_sem * sem)540 static bool invalid_sem(struct qaic_sem *sem)
541 {
542 	if (sem->val & ~SEM_VAL_MASK || sem->index & ~SEM_INDEX_MASK ||
543 	    !(sem->presync == 0 || sem->presync == 1) || sem->pad ||
544 	    sem->flags & ~(QAIC_SEM_INSYNCFENCE | QAIC_SEM_OUTSYNCFENCE) ||
545 	    sem->cmd > QAIC_SEM_WAIT_GT_0)
546 		return true;
547 	return false;
548 }
549 
qaic_validate_req(struct qaic_device * qdev,struct qaic_attach_slice_entry * slice_ent,u32 count,u64 total_size)550 static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_entry *slice_ent,
551 			     u32 count, u64 total_size)
552 {
553 	u64 total;
554 	int i;
555 
556 	for (i = 0; i < count; i++) {
557 		if (!(slice_ent[i].db_len == 32 || slice_ent[i].db_len == 16 ||
558 		      slice_ent[i].db_len == 8 || slice_ent[i].db_len == 0) ||
559 		      invalid_sem(&slice_ent[i].sem0) || invalid_sem(&slice_ent[i].sem1) ||
560 		      invalid_sem(&slice_ent[i].sem2) || invalid_sem(&slice_ent[i].sem3))
561 			return -EINVAL;
562 
563 		if (check_add_overflow(slice_ent[i].offset, slice_ent[i].size, &total) ||
564 		    total > total_size)
565 			return -EINVAL;
566 	}
567 
568 	return 0;
569 }
570 
qaic_free_sgt(struct sg_table * sgt)571 static void qaic_free_sgt(struct sg_table *sgt)
572 {
573 	struct scatterlist *sg;
574 
575 	for (sg = sgt->sgl; sg; sg = sg_next(sg))
576 		if (sg_page(sg))
577 			__free_pages(sg_page(sg), get_order(sg->length));
578 	sg_free_table(sgt);
579 	kfree(sgt);
580 }
581 
qaic_gem_print_info(struct drm_printer * p,unsigned int indent,const struct drm_gem_object * obj)582 static void qaic_gem_print_info(struct drm_printer *p, unsigned int indent,
583 				const struct drm_gem_object *obj)
584 {
585 	struct qaic_bo *bo = to_qaic_bo(obj);
586 
587 	drm_printf_indent(p, indent, "user requested size=%llu\n", bo->size);
588 }
589 
590 static const struct vm_operations_struct drm_vm_ops = {
591 	.open = drm_gem_vm_open,
592 	.close = drm_gem_vm_close,
593 };
594 
qaic_gem_object_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)595 static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
596 {
597 	struct qaic_bo *bo = to_qaic_bo(obj);
598 	unsigned long offset = 0;
599 	struct scatterlist *sg;
600 	int ret = 0;
601 
602 	if (obj->import_attach)
603 		return -EINVAL;
604 
605 	for (sg = bo->sgt->sgl; sg; sg = sg_next(sg)) {
606 		if (sg_page(sg)) {
607 			ret = remap_pfn_range(vma, vma->vm_start + offset, page_to_pfn(sg_page(sg)),
608 					      sg->length, vma->vm_page_prot);
609 			if (ret)
610 				goto out;
611 			offset += sg->length;
612 		}
613 	}
614 
615 out:
616 	return ret;
617 }
618 
qaic_free_object(struct drm_gem_object * obj)619 static void qaic_free_object(struct drm_gem_object *obj)
620 {
621 	struct qaic_bo *bo = to_qaic_bo(obj);
622 
623 	if (obj->import_attach) {
624 		/* DMABUF/PRIME Path */
625 		drm_prime_gem_destroy(obj, NULL);
626 	} else {
627 		/* Private buffer allocation path */
628 		qaic_free_sgt(bo->sgt);
629 	}
630 
631 	drm_gem_object_release(obj);
632 	kfree(bo);
633 }
634 
635 static const struct drm_gem_object_funcs qaic_gem_funcs = {
636 	.free = qaic_free_object,
637 	.print_info = qaic_gem_print_info,
638 	.mmap = qaic_gem_object_mmap,
639 	.vm_ops = &drm_vm_ops,
640 };
641 
qaic_alloc_init_bo(void)642 static struct qaic_bo *qaic_alloc_init_bo(void)
643 {
644 	struct qaic_bo *bo;
645 
646 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
647 	if (!bo)
648 		return ERR_PTR(-ENOMEM);
649 
650 	INIT_LIST_HEAD(&bo->slices);
651 	init_completion(&bo->xfer_done);
652 	complete_all(&bo->xfer_done);
653 
654 	return bo;
655 }
656 
qaic_create_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)657 int qaic_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
658 {
659 	struct qaic_create_bo *args = data;
660 	int usr_rcu_id, qdev_rcu_id;
661 	struct drm_gem_object *obj;
662 	struct qaic_device *qdev;
663 	struct qaic_user *usr;
664 	struct qaic_bo *bo;
665 	size_t size;
666 	int ret;
667 
668 	if (args->pad)
669 		return -EINVAL;
670 
671 	size = PAGE_ALIGN(args->size);
672 	if (size == 0)
673 		return -EINVAL;
674 
675 	usr = file_priv->driver_priv;
676 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
677 	if (!usr->qddev) {
678 		ret = -ENODEV;
679 		goto unlock_usr_srcu;
680 	}
681 
682 	qdev = usr->qddev->qdev;
683 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
684 	if (qdev->in_reset) {
685 		ret = -ENODEV;
686 		goto unlock_dev_srcu;
687 	}
688 
689 	bo = qaic_alloc_init_bo();
690 	if (IS_ERR(bo)) {
691 		ret = PTR_ERR(bo);
692 		goto unlock_dev_srcu;
693 	}
694 	obj = &bo->base;
695 
696 	drm_gem_private_object_init(dev, obj, size);
697 
698 	obj->funcs = &qaic_gem_funcs;
699 	ret = create_sgt(qdev, &bo->sgt, size);
700 	if (ret)
701 		goto free_bo;
702 
703 	bo->size = args->size;
704 
705 	ret = drm_gem_handle_create(file_priv, obj, &args->handle);
706 	if (ret)
707 		goto free_sgt;
708 
709 	bo->handle = args->handle;
710 	drm_gem_object_put(obj);
711 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
712 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
713 
714 	return 0;
715 
716 free_sgt:
717 	qaic_free_sgt(bo->sgt);
718 free_bo:
719 	kfree(bo);
720 unlock_dev_srcu:
721 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
722 unlock_usr_srcu:
723 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
724 	return ret;
725 }
726 
qaic_mmap_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)727 int qaic_mmap_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
728 {
729 	struct qaic_mmap_bo *args = data;
730 	int usr_rcu_id, qdev_rcu_id;
731 	struct drm_gem_object *obj;
732 	struct qaic_device *qdev;
733 	struct qaic_user *usr;
734 	int ret;
735 
736 	usr = file_priv->driver_priv;
737 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
738 	if (!usr->qddev) {
739 		ret = -ENODEV;
740 		goto unlock_usr_srcu;
741 	}
742 
743 	qdev = usr->qddev->qdev;
744 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
745 	if (qdev->in_reset) {
746 		ret = -ENODEV;
747 		goto unlock_dev_srcu;
748 	}
749 
750 	obj = drm_gem_object_lookup(file_priv, args->handle);
751 	if (!obj) {
752 		ret = -ENOENT;
753 		goto unlock_dev_srcu;
754 	}
755 
756 	ret = drm_gem_create_mmap_offset(obj);
757 	if (ret == 0)
758 		args->offset = drm_vma_node_offset_addr(&obj->vma_node);
759 
760 	drm_gem_object_put(obj);
761 
762 unlock_dev_srcu:
763 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
764 unlock_usr_srcu:
765 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
766 	return ret;
767 }
768 
qaic_gem_prime_import(struct drm_device * dev,struct dma_buf * dma_buf)769 struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
770 {
771 	struct dma_buf_attachment *attach;
772 	struct drm_gem_object *obj;
773 	struct qaic_bo *bo;
774 	int ret;
775 
776 	bo = qaic_alloc_init_bo();
777 	if (IS_ERR(bo)) {
778 		ret = PTR_ERR(bo);
779 		goto out;
780 	}
781 
782 	obj = &bo->base;
783 	get_dma_buf(dma_buf);
784 
785 	attach = dma_buf_attach(dma_buf, dev->dev);
786 	if (IS_ERR(attach)) {
787 		ret = PTR_ERR(attach);
788 		goto attach_fail;
789 	}
790 
791 	if (!attach->dmabuf->size) {
792 		ret = -EINVAL;
793 		goto size_align_fail;
794 	}
795 
796 	drm_gem_private_object_init(dev, obj, attach->dmabuf->size);
797 	/*
798 	 * skipping dma_buf_map_attachment() as we do not know the direction
799 	 * just yet. Once the direction is known in the subsequent IOCTL to
800 	 * attach slicing, we can do it then.
801 	 */
802 
803 	obj->funcs = &qaic_gem_funcs;
804 	obj->import_attach = attach;
805 	obj->resv = dma_buf->resv;
806 
807 	return obj;
808 
809 size_align_fail:
810 	dma_buf_detach(dma_buf, attach);
811 attach_fail:
812 	dma_buf_put(dma_buf);
813 	kfree(bo);
814 out:
815 	return ERR_PTR(ret);
816 }
817 
qaic_prepare_import_bo(struct qaic_bo * bo,struct qaic_attach_slice_hdr * hdr)818 static int qaic_prepare_import_bo(struct qaic_bo *bo, struct qaic_attach_slice_hdr *hdr)
819 {
820 	struct drm_gem_object *obj = &bo->base;
821 	struct sg_table *sgt;
822 	int ret;
823 
824 	if (obj->import_attach->dmabuf->size < hdr->size)
825 		return -EINVAL;
826 
827 	sgt = dma_buf_map_attachment(obj->import_attach, hdr->dir);
828 	if (IS_ERR(sgt)) {
829 		ret = PTR_ERR(sgt);
830 		return ret;
831 	}
832 
833 	bo->sgt = sgt;
834 	bo->size = hdr->size;
835 
836 	return 0;
837 }
838 
qaic_prepare_export_bo(struct qaic_device * qdev,struct qaic_bo * bo,struct qaic_attach_slice_hdr * hdr)839 static int qaic_prepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo,
840 				  struct qaic_attach_slice_hdr *hdr)
841 {
842 	int ret;
843 
844 	if (bo->size != hdr->size)
845 		return -EINVAL;
846 
847 	ret = dma_map_sgtable(&qdev->pdev->dev, bo->sgt, hdr->dir, 0);
848 	if (ret)
849 		return -EFAULT;
850 
851 	return 0;
852 }
853 
qaic_prepare_bo(struct qaic_device * qdev,struct qaic_bo * bo,struct qaic_attach_slice_hdr * hdr)854 static int qaic_prepare_bo(struct qaic_device *qdev, struct qaic_bo *bo,
855 			   struct qaic_attach_slice_hdr *hdr)
856 {
857 	int ret;
858 
859 	if (bo->base.import_attach)
860 		ret = qaic_prepare_import_bo(bo, hdr);
861 	else
862 		ret = qaic_prepare_export_bo(qdev, bo, hdr);
863 
864 	if (ret == 0)
865 		bo->dir = hdr->dir;
866 
867 	return ret;
868 }
869 
qaic_unprepare_import_bo(struct qaic_bo * bo)870 static void qaic_unprepare_import_bo(struct qaic_bo *bo)
871 {
872 	dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, bo->dir);
873 	bo->sgt = NULL;
874 	bo->size = 0;
875 }
876 
qaic_unprepare_export_bo(struct qaic_device * qdev,struct qaic_bo * bo)877 static void qaic_unprepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo)
878 {
879 	dma_unmap_sgtable(&qdev->pdev->dev, bo->sgt, bo->dir, 0);
880 }
881 
qaic_unprepare_bo(struct qaic_device * qdev,struct qaic_bo * bo)882 static void qaic_unprepare_bo(struct qaic_device *qdev, struct qaic_bo *bo)
883 {
884 	if (bo->base.import_attach)
885 		qaic_unprepare_import_bo(bo);
886 	else
887 		qaic_unprepare_export_bo(qdev, bo);
888 
889 	bo->dir = 0;
890 }
891 
qaic_free_slices_bo(struct qaic_bo * bo)892 static void qaic_free_slices_bo(struct qaic_bo *bo)
893 {
894 	struct bo_slice *slice, *temp;
895 
896 	list_for_each_entry_safe(slice, temp, &bo->slices, slice)
897 		kref_put(&slice->ref_count, free_slice);
898 }
899 
qaic_attach_slicing_bo(struct qaic_device * qdev,struct qaic_bo * bo,struct qaic_attach_slice_hdr * hdr,struct qaic_attach_slice_entry * slice_ent)900 static int qaic_attach_slicing_bo(struct qaic_device *qdev, struct qaic_bo *bo,
901 				  struct qaic_attach_slice_hdr *hdr,
902 				  struct qaic_attach_slice_entry *slice_ent)
903 {
904 	int ret, i;
905 
906 	for (i = 0; i < hdr->count; i++) {
907 		ret = qaic_map_one_slice(qdev, bo, &slice_ent[i]);
908 		if (ret) {
909 			qaic_free_slices_bo(bo);
910 			return ret;
911 		}
912 	}
913 
914 	if (bo->total_slice_nents > qdev->dbc[hdr->dbc_id].nelem) {
915 		qaic_free_slices_bo(bo);
916 		return -ENOSPC;
917 	}
918 
919 	bo->sliced = true;
920 	bo->nr_slice = hdr->count;
921 	list_add_tail(&bo->bo_list, &qdev->dbc[hdr->dbc_id].bo_lists);
922 
923 	return 0;
924 }
925 
qaic_attach_slice_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)926 int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
927 {
928 	struct qaic_attach_slice_entry *slice_ent;
929 	struct qaic_attach_slice *args = data;
930 	int rcu_id, usr_rcu_id, qdev_rcu_id;
931 	struct dma_bridge_chan	*dbc;
932 	struct drm_gem_object *obj;
933 	struct qaic_device *qdev;
934 	unsigned long arg_size;
935 	struct qaic_user *usr;
936 	u8 __user *user_data;
937 	struct qaic_bo *bo;
938 	int ret;
939 
940 	if (args->hdr.count == 0)
941 		return -EINVAL;
942 
943 	arg_size = args->hdr.count * sizeof(*slice_ent);
944 	if (arg_size / args->hdr.count != sizeof(*slice_ent))
945 		return -EINVAL;
946 
947 	if (args->hdr.size == 0)
948 		return -EINVAL;
949 
950 	if (!(args->hdr.dir == DMA_TO_DEVICE || args->hdr.dir == DMA_FROM_DEVICE))
951 		return -EINVAL;
952 
953 	if (args->data == 0)
954 		return -EINVAL;
955 
956 	usr = file_priv->driver_priv;
957 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
958 	if (!usr->qddev) {
959 		ret = -ENODEV;
960 		goto unlock_usr_srcu;
961 	}
962 
963 	qdev = usr->qddev->qdev;
964 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
965 	if (qdev->in_reset) {
966 		ret = -ENODEV;
967 		goto unlock_dev_srcu;
968 	}
969 
970 	if (args->hdr.dbc_id >= qdev->num_dbc) {
971 		ret = -EINVAL;
972 		goto unlock_dev_srcu;
973 	}
974 
975 	user_data = u64_to_user_ptr(args->data);
976 
977 	slice_ent = kzalloc(arg_size, GFP_KERNEL);
978 	if (!slice_ent) {
979 		ret = -EINVAL;
980 		goto unlock_dev_srcu;
981 	}
982 
983 	ret = copy_from_user(slice_ent, user_data, arg_size);
984 	if (ret) {
985 		ret = -EFAULT;
986 		goto free_slice_ent;
987 	}
988 
989 	ret = qaic_validate_req(qdev, slice_ent, args->hdr.count, args->hdr.size);
990 	if (ret)
991 		goto free_slice_ent;
992 
993 	obj = drm_gem_object_lookup(file_priv, args->hdr.handle);
994 	if (!obj) {
995 		ret = -ENOENT;
996 		goto free_slice_ent;
997 	}
998 
999 	bo = to_qaic_bo(obj);
1000 
1001 	if (bo->sliced) {
1002 		ret = -EINVAL;
1003 		goto put_bo;
1004 	}
1005 
1006 	dbc = &qdev->dbc[args->hdr.dbc_id];
1007 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1008 	if (dbc->usr != usr) {
1009 		ret = -EINVAL;
1010 		goto unlock_ch_srcu;
1011 	}
1012 
1013 	ret = qaic_prepare_bo(qdev, bo, &args->hdr);
1014 	if (ret)
1015 		goto unlock_ch_srcu;
1016 
1017 	ret = qaic_attach_slicing_bo(qdev, bo, &args->hdr, slice_ent);
1018 	if (ret)
1019 		goto unprepare_bo;
1020 
1021 	if (args->hdr.dir == DMA_TO_DEVICE)
1022 		dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, args->hdr.dir);
1023 
1024 	bo->dbc = dbc;
1025 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1026 	drm_gem_object_put(obj);
1027 	kfree(slice_ent);
1028 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1029 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1030 
1031 	return 0;
1032 
1033 unprepare_bo:
1034 	qaic_unprepare_bo(qdev, bo);
1035 unlock_ch_srcu:
1036 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1037 put_bo:
1038 	drm_gem_object_put(obj);
1039 free_slice_ent:
1040 	kfree(slice_ent);
1041 unlock_dev_srcu:
1042 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1043 unlock_usr_srcu:
1044 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1045 	return ret;
1046 }
1047 
copy_exec_reqs(struct qaic_device * qdev,struct bo_slice * slice,u32 dbc_id,u32 head,u32 * ptail)1048 static inline int copy_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice, u32 dbc_id,
1049 				 u32 head, u32 *ptail)
1050 {
1051 	struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1052 	struct dbc_req *reqs = slice->reqs;
1053 	u32 tail = *ptail;
1054 	u32 avail;
1055 
1056 	avail = head - tail;
1057 	if (head <= tail)
1058 		avail += dbc->nelem;
1059 
1060 	--avail;
1061 
1062 	if (avail < slice->nents)
1063 		return -EAGAIN;
1064 
1065 	if (tail + slice->nents > dbc->nelem) {
1066 		avail = dbc->nelem - tail;
1067 		avail = min_t(u32, avail, slice->nents);
1068 		memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1069 		       sizeof(*reqs) * avail);
1070 		reqs += avail;
1071 		avail = slice->nents - avail;
1072 		if (avail)
1073 			memcpy(dbc->req_q_base, reqs, sizeof(*reqs) * avail);
1074 	} else {
1075 		memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1076 		       sizeof(*reqs) * slice->nents);
1077 	}
1078 
1079 	*ptail = (tail + slice->nents) % dbc->nelem;
1080 
1081 	return 0;
1082 }
1083 
1084 /*
1085  * Based on the value of resize we may only need to transmit first_n
1086  * entries and the last entry, with last_bytes to send from the last entry.
1087  * Note that first_n could be 0.
1088  */
copy_partial_exec_reqs(struct qaic_device * qdev,struct bo_slice * slice,u64 resize,u32 dbc_id,u32 head,u32 * ptail)1089 static inline int copy_partial_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice,
1090 					 u64 resize, u32 dbc_id, u32 head, u32 *ptail)
1091 {
1092 	struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1093 	struct dbc_req *reqs = slice->reqs;
1094 	struct dbc_req *last_req;
1095 	u32 tail = *ptail;
1096 	u64 total_bytes;
1097 	u64 last_bytes;
1098 	u32 first_n;
1099 	u32 avail;
1100 	int ret;
1101 	int i;
1102 
1103 	avail = head - tail;
1104 	if (head <= tail)
1105 		avail += dbc->nelem;
1106 
1107 	--avail;
1108 
1109 	total_bytes = 0;
1110 	for (i = 0; i < slice->nents; i++) {
1111 		total_bytes += le32_to_cpu(reqs[i].len);
1112 		if (total_bytes >= resize)
1113 			break;
1114 	}
1115 
1116 	if (total_bytes < resize) {
1117 		/* User space should have used the full buffer path. */
1118 		ret = -EINVAL;
1119 		return ret;
1120 	}
1121 
1122 	first_n = i;
1123 	last_bytes = i ? resize + le32_to_cpu(reqs[i].len) - total_bytes : resize;
1124 
1125 	if (avail < (first_n + 1))
1126 		return -EAGAIN;
1127 
1128 	if (first_n) {
1129 		if (tail + first_n > dbc->nelem) {
1130 			avail = dbc->nelem - tail;
1131 			avail = min_t(u32, avail, first_n);
1132 			memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1133 			       sizeof(*reqs) * avail);
1134 			last_req = reqs + avail;
1135 			avail = first_n - avail;
1136 			if (avail)
1137 				memcpy(dbc->req_q_base, last_req, sizeof(*reqs) * avail);
1138 		} else {
1139 			memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1140 			       sizeof(*reqs) * first_n);
1141 		}
1142 	}
1143 
1144 	/* Copy over the last entry. Here we need to adjust len to the left over
1145 	 * size, and set src and dst to the entry it is copied to.
1146 	 */
1147 	last_req = dbc->req_q_base + (tail + first_n) % dbc->nelem * get_dbc_req_elem_size();
1148 	memcpy(last_req, reqs + slice->nents - 1, sizeof(*reqs));
1149 
1150 	/*
1151 	 * last_bytes holds size of a DMA segment, maximum DMA segment size is
1152 	 * set to UINT_MAX by qaic and hence last_bytes can never exceed u32
1153 	 * range. So, by down sizing we are not corrupting the value.
1154 	 */
1155 	last_req->len = cpu_to_le32((u32)last_bytes);
1156 	last_req->src_addr = reqs[first_n].src_addr;
1157 	last_req->dest_addr = reqs[first_n].dest_addr;
1158 
1159 	*ptail = (tail + first_n + 1) % dbc->nelem;
1160 
1161 	return 0;
1162 }
1163 
send_bo_list_to_device(struct qaic_device * qdev,struct drm_file * file_priv,struct qaic_execute_entry * exec,unsigned int count,bool is_partial,struct dma_bridge_chan * dbc,u32 head,u32 * tail)1164 static int send_bo_list_to_device(struct qaic_device *qdev, struct drm_file *file_priv,
1165 				  struct qaic_execute_entry *exec, unsigned int count,
1166 				  bool is_partial, struct dma_bridge_chan *dbc, u32 head,
1167 				  u32 *tail)
1168 {
1169 	struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec;
1170 	struct drm_gem_object *obj;
1171 	struct bo_slice *slice;
1172 	unsigned long flags;
1173 	struct qaic_bo *bo;
1174 	bool queued;
1175 	int i, j;
1176 	int ret;
1177 
1178 	for (i = 0; i < count; i++) {
1179 		/*
1180 		 * ref count will be decremented when the transfer of this
1181 		 * buffer is complete. It is inside dbc_irq_threaded_fn().
1182 		 */
1183 		obj = drm_gem_object_lookup(file_priv,
1184 					    is_partial ? pexec[i].handle : exec[i].handle);
1185 		if (!obj) {
1186 			ret = -ENOENT;
1187 			goto failed_to_send_bo;
1188 		}
1189 
1190 		bo = to_qaic_bo(obj);
1191 
1192 		if (!bo->sliced) {
1193 			ret = -EINVAL;
1194 			goto failed_to_send_bo;
1195 		}
1196 
1197 		if (is_partial && pexec[i].resize > bo->size) {
1198 			ret = -EINVAL;
1199 			goto failed_to_send_bo;
1200 		}
1201 
1202 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1203 		queued = bo->queued;
1204 		bo->queued = true;
1205 		if (queued) {
1206 			spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1207 			ret = -EINVAL;
1208 			goto failed_to_send_bo;
1209 		}
1210 
1211 		bo->req_id = dbc->next_req_id++;
1212 
1213 		list_for_each_entry(slice, &bo->slices, slice) {
1214 			/*
1215 			 * If this slice does not fall under the given
1216 			 * resize then skip this slice and continue the loop
1217 			 */
1218 			if (is_partial && pexec[i].resize && pexec[i].resize <= slice->offset)
1219 				continue;
1220 
1221 			for (j = 0; j < slice->nents; j++)
1222 				slice->reqs[j].req_id = cpu_to_le16(bo->req_id);
1223 
1224 			/*
1225 			 * If it is a partial execute ioctl call then check if
1226 			 * resize has cut this slice short then do a partial copy
1227 			 * else do complete copy
1228 			 */
1229 			if (is_partial && pexec[i].resize &&
1230 			    pexec[i].resize < slice->offset + slice->size)
1231 				ret = copy_partial_exec_reqs(qdev, slice,
1232 							     pexec[i].resize - slice->offset,
1233 							     dbc->id, head, tail);
1234 			else
1235 				ret = copy_exec_reqs(qdev, slice, dbc->id, head, tail);
1236 			if (ret) {
1237 				bo->queued = false;
1238 				spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1239 				goto failed_to_send_bo;
1240 			}
1241 		}
1242 		reinit_completion(&bo->xfer_done);
1243 		list_add_tail(&bo->xfer_list, &dbc->xfer_list);
1244 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1245 		dma_sync_sgtable_for_device(&qdev->pdev->dev, bo->sgt, bo->dir);
1246 	}
1247 
1248 	return 0;
1249 
1250 failed_to_send_bo:
1251 	if (likely(obj))
1252 		drm_gem_object_put(obj);
1253 	for (j = 0; j < i; j++) {
1254 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1255 		bo = list_last_entry(&dbc->xfer_list, struct qaic_bo, xfer_list);
1256 		obj = &bo->base;
1257 		bo->queued = false;
1258 		list_del(&bo->xfer_list);
1259 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1260 		dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1261 		drm_gem_object_put(obj);
1262 	}
1263 	return ret;
1264 }
1265 
update_profiling_data(struct drm_file * file_priv,struct qaic_execute_entry * exec,unsigned int count,bool is_partial,u64 received_ts,u64 submit_ts,u32 queue_level)1266 static void update_profiling_data(struct drm_file *file_priv,
1267 				  struct qaic_execute_entry *exec, unsigned int count,
1268 				  bool is_partial, u64 received_ts, u64 submit_ts, u32 queue_level)
1269 {
1270 	struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec;
1271 	struct drm_gem_object *obj;
1272 	struct qaic_bo *bo;
1273 	int i;
1274 
1275 	for (i = 0; i < count; i++) {
1276 		/*
1277 		 * Since we already committed the BO to hardware, the only way
1278 		 * this should fail is a pending signal. We can't cancel the
1279 		 * submit to hardware, so we have to just skip the profiling
1280 		 * data. In case the signal is not fatal to the process, we
1281 		 * return success so that the user doesn't try to resubmit.
1282 		 */
1283 		obj = drm_gem_object_lookup(file_priv,
1284 					    is_partial ? pexec[i].handle : exec[i].handle);
1285 		if (!obj)
1286 			break;
1287 		bo = to_qaic_bo(obj);
1288 		bo->perf_stats.req_received_ts = received_ts;
1289 		bo->perf_stats.req_submit_ts = submit_ts;
1290 		bo->perf_stats.queue_level_before = queue_level;
1291 		queue_level += bo->total_slice_nents;
1292 		drm_gem_object_put(obj);
1293 	}
1294 }
1295 
__qaic_execute_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv,bool is_partial)1296 static int __qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv,
1297 				   bool is_partial)
1298 {
1299 	struct qaic_execute *args = data;
1300 	struct qaic_execute_entry *exec;
1301 	struct dma_bridge_chan *dbc;
1302 	int usr_rcu_id, qdev_rcu_id;
1303 	struct qaic_device *qdev;
1304 	struct qaic_user *usr;
1305 	u8 __user *user_data;
1306 	unsigned long n;
1307 	u64 received_ts;
1308 	u32 queue_level;
1309 	u64 submit_ts;
1310 	int rcu_id;
1311 	u32 head;
1312 	u32 tail;
1313 	u64 size;
1314 	int ret;
1315 
1316 	received_ts = ktime_get_ns();
1317 
1318 	size = is_partial ? sizeof(struct qaic_partial_execute_entry) : sizeof(*exec);
1319 	n = (unsigned long)size * args->hdr.count;
1320 	if (args->hdr.count == 0 || n / args->hdr.count != size)
1321 		return -EINVAL;
1322 
1323 	user_data = u64_to_user_ptr(args->data);
1324 
1325 	exec = kcalloc(args->hdr.count, size, GFP_KERNEL);
1326 	if (!exec)
1327 		return -ENOMEM;
1328 
1329 	if (copy_from_user(exec, user_data, n)) {
1330 		ret = -EFAULT;
1331 		goto free_exec;
1332 	}
1333 
1334 	usr = file_priv->driver_priv;
1335 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1336 	if (!usr->qddev) {
1337 		ret = -ENODEV;
1338 		goto unlock_usr_srcu;
1339 	}
1340 
1341 	qdev = usr->qddev->qdev;
1342 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1343 	if (qdev->in_reset) {
1344 		ret = -ENODEV;
1345 		goto unlock_dev_srcu;
1346 	}
1347 
1348 	if (args->hdr.dbc_id >= qdev->num_dbc) {
1349 		ret = -EINVAL;
1350 		goto unlock_dev_srcu;
1351 	}
1352 
1353 	dbc = &qdev->dbc[args->hdr.dbc_id];
1354 
1355 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1356 	if (!dbc->usr || dbc->usr->handle != usr->handle) {
1357 		ret = -EPERM;
1358 		goto release_ch_rcu;
1359 	}
1360 
1361 	head = readl(dbc->dbc_base + REQHP_OFF);
1362 	tail = readl(dbc->dbc_base + REQTP_OFF);
1363 
1364 	if (head == U32_MAX || tail == U32_MAX) {
1365 		/* PCI link error */
1366 		ret = -ENODEV;
1367 		goto release_ch_rcu;
1368 	}
1369 
1370 	queue_level = head <= tail ? tail - head : dbc->nelem - (head - tail);
1371 
1372 	ret = send_bo_list_to_device(qdev, file_priv, exec, args->hdr.count, is_partial, dbc,
1373 				     head, &tail);
1374 	if (ret)
1375 		goto release_ch_rcu;
1376 
1377 	/* Finalize commit to hardware */
1378 	submit_ts = ktime_get_ns();
1379 	writel(tail, dbc->dbc_base + REQTP_OFF);
1380 
1381 	update_profiling_data(file_priv, exec, args->hdr.count, is_partial, received_ts,
1382 			      submit_ts, queue_level);
1383 
1384 	if (datapath_polling)
1385 		schedule_work(&dbc->poll_work);
1386 
1387 release_ch_rcu:
1388 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1389 unlock_dev_srcu:
1390 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1391 unlock_usr_srcu:
1392 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1393 free_exec:
1394 	kfree(exec);
1395 	return ret;
1396 }
1397 
qaic_execute_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1398 int qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1399 {
1400 	return __qaic_execute_bo_ioctl(dev, data, file_priv, false);
1401 }
1402 
qaic_partial_execute_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1403 int qaic_partial_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1404 {
1405 	return __qaic_execute_bo_ioctl(dev, data, file_priv, true);
1406 }
1407 
1408 /*
1409  * Our interrupt handling is a bit more complicated than a simple ideal, but
1410  * sadly necessary.
1411  *
1412  * Each dbc has a completion queue. Entries in the queue correspond to DMA
1413  * requests which the device has processed. The hardware already has a built
1414  * in irq mitigation. When the device puts an entry into the queue, it will
1415  * only trigger an interrupt if the queue was empty. Therefore, when adding
1416  * the Nth event to a non-empty queue, the hardware doesn't trigger an
1417  * interrupt. This means the host doesn't get additional interrupts signaling
1418  * the same thing - the queue has something to process.
1419  * This behavior can be overridden in the DMA request.
1420  * This means that when the host receives an interrupt, it is required to
1421  * drain the queue.
1422  *
1423  * This behavior is what NAPI attempts to accomplish, although we can't use
1424  * NAPI as we don't have a netdev. We use threaded irqs instead.
1425  *
1426  * However, there is a situation where the host drains the queue fast enough
1427  * that every event causes an interrupt. Typically this is not a problem as
1428  * the rate of events would be low. However, that is not the case with
1429  * lprnet for example. On an Intel Xeon D-2191 where we run 8 instances of
1430  * lprnet, the host receives roughly 80k interrupts per second from the device
1431  * (per /proc/interrupts). While NAPI documentation indicates the host should
1432  * just chug along, sadly that behavior causes instability in some hosts.
1433  *
1434  * Therefore, we implement an interrupt disable scheme similar to NAPI. The
1435  * key difference is that we will delay after draining the queue for a small
1436  * time to allow additional events to come in via polling. Using the above
1437  * lprnet workload, this reduces the number of interrupts processed from
1438  * ~80k/sec to about 64 in 5 minutes and appears to solve the system
1439  * instability.
1440  */
dbc_irq_handler(int irq,void * data)1441 irqreturn_t dbc_irq_handler(int irq, void *data)
1442 {
1443 	struct dma_bridge_chan *dbc = data;
1444 	int rcu_id;
1445 	u32 head;
1446 	u32 tail;
1447 
1448 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1449 
1450 	if (!dbc->usr) {
1451 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1452 		return IRQ_HANDLED;
1453 	}
1454 
1455 	head = readl(dbc->dbc_base + RSPHP_OFF);
1456 	if (head == U32_MAX) { /* PCI link error */
1457 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1458 		return IRQ_NONE;
1459 	}
1460 
1461 	tail = readl(dbc->dbc_base + RSPTP_OFF);
1462 	if (tail == U32_MAX) { /* PCI link error */
1463 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1464 		return IRQ_NONE;
1465 	}
1466 
1467 	if (head == tail) { /* queue empty */
1468 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1469 		return IRQ_NONE;
1470 	}
1471 
1472 	disable_irq_nosync(irq);
1473 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1474 	return IRQ_WAKE_THREAD;
1475 }
1476 
irq_polling_work(struct work_struct * work)1477 void irq_polling_work(struct work_struct *work)
1478 {
1479 	struct dma_bridge_chan *dbc = container_of(work, struct dma_bridge_chan,  poll_work);
1480 	unsigned long flags;
1481 	int rcu_id;
1482 	u32 head;
1483 	u32 tail;
1484 
1485 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1486 
1487 	while (1) {
1488 		if (dbc->qdev->in_reset) {
1489 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1490 			return;
1491 		}
1492 		if (!dbc->usr) {
1493 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1494 			return;
1495 		}
1496 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1497 		if (list_empty(&dbc->xfer_list)) {
1498 			spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1499 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1500 			return;
1501 		}
1502 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1503 
1504 		head = readl(dbc->dbc_base + RSPHP_OFF);
1505 		if (head == U32_MAX) { /* PCI link error */
1506 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1507 			return;
1508 		}
1509 
1510 		tail = readl(dbc->dbc_base + RSPTP_OFF);
1511 		if (tail == U32_MAX) { /* PCI link error */
1512 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1513 			return;
1514 		}
1515 
1516 		if (head != tail) {
1517 			irq_wake_thread(dbc->irq, dbc);
1518 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1519 			return;
1520 		}
1521 
1522 		cond_resched();
1523 		usleep_range(datapath_poll_interval_us, 2 * datapath_poll_interval_us);
1524 	}
1525 }
1526 
dbc_irq_threaded_fn(int irq,void * data)1527 irqreturn_t dbc_irq_threaded_fn(int irq, void *data)
1528 {
1529 	struct dma_bridge_chan *dbc = data;
1530 	int event_count = NUM_EVENTS;
1531 	int delay_count = NUM_DELAYS;
1532 	struct qaic_device *qdev;
1533 	struct qaic_bo *bo, *i;
1534 	struct dbc_rsp *rsp;
1535 	unsigned long flags;
1536 	int rcu_id;
1537 	u16 status;
1538 	u16 req_id;
1539 	u32 head;
1540 	u32 tail;
1541 
1542 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1543 
1544 	head = readl(dbc->dbc_base + RSPHP_OFF);
1545 	if (head == U32_MAX) /* PCI link error */
1546 		goto error_out;
1547 
1548 	qdev = dbc->qdev;
1549 read_fifo:
1550 
1551 	if (!event_count) {
1552 		event_count = NUM_EVENTS;
1553 		cond_resched();
1554 	}
1555 
1556 	/*
1557 	 * if this channel isn't assigned or gets unassigned during processing
1558 	 * we have nothing further to do
1559 	 */
1560 	if (!dbc->usr)
1561 		goto error_out;
1562 
1563 	tail = readl(dbc->dbc_base + RSPTP_OFF);
1564 	if (tail == U32_MAX) /* PCI link error */
1565 		goto error_out;
1566 
1567 	if (head == tail) { /* queue empty */
1568 		if (delay_count) {
1569 			--delay_count;
1570 			usleep_range(100, 200);
1571 			goto read_fifo; /* check for a new event */
1572 		}
1573 		goto normal_out;
1574 	}
1575 
1576 	delay_count = NUM_DELAYS;
1577 	while (head != tail) {
1578 		if (!event_count)
1579 			break;
1580 		--event_count;
1581 		rsp = dbc->rsp_q_base + head * sizeof(*rsp);
1582 		req_id = le16_to_cpu(rsp->req_id);
1583 		status = le16_to_cpu(rsp->status);
1584 		if (status)
1585 			pci_dbg(qdev->pdev, "req_id %d failed with status %d\n", req_id, status);
1586 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1587 		/*
1588 		 * A BO can receive multiple interrupts, since a BO can be
1589 		 * divided into multiple slices and a buffer receives as many
1590 		 * interrupts as slices. So until it receives interrupts for
1591 		 * all the slices we cannot mark that buffer complete.
1592 		 */
1593 		list_for_each_entry_safe(bo, i, &dbc->xfer_list, xfer_list) {
1594 			if (bo->req_id == req_id)
1595 				bo->nr_slice_xfer_done++;
1596 			else
1597 				continue;
1598 
1599 			if (bo->nr_slice_xfer_done < bo->nr_slice)
1600 				break;
1601 
1602 			/*
1603 			 * At this point we have received all the interrupts for
1604 			 * BO, which means BO execution is complete.
1605 			 */
1606 			dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1607 			bo->nr_slice_xfer_done = 0;
1608 			bo->queued = false;
1609 			list_del(&bo->xfer_list);
1610 			bo->perf_stats.req_processed_ts = ktime_get_ns();
1611 			complete_all(&bo->xfer_done);
1612 			drm_gem_object_put(&bo->base);
1613 			break;
1614 		}
1615 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1616 		head = (head + 1) % dbc->nelem;
1617 	}
1618 
1619 	/*
1620 	 * Update the head pointer of response queue and let the device know
1621 	 * that we have consumed elements from the queue.
1622 	 */
1623 	writel(head, dbc->dbc_base + RSPHP_OFF);
1624 
1625 	/* elements might have been put in the queue while we were processing */
1626 	goto read_fifo;
1627 
1628 normal_out:
1629 	if (likely(!datapath_polling))
1630 		enable_irq(irq);
1631 	else
1632 		schedule_work(&dbc->poll_work);
1633 	/* checking the fifo and enabling irqs is a race, missed event check */
1634 	tail = readl(dbc->dbc_base + RSPTP_OFF);
1635 	if (tail != U32_MAX && head != tail) {
1636 		if (likely(!datapath_polling))
1637 			disable_irq_nosync(irq);
1638 		goto read_fifo;
1639 	}
1640 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1641 	return IRQ_HANDLED;
1642 
1643 error_out:
1644 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1645 	if (likely(!datapath_polling))
1646 		enable_irq(irq);
1647 	else
1648 		schedule_work(&dbc->poll_work);
1649 
1650 	return IRQ_HANDLED;
1651 }
1652 
qaic_wait_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1653 int qaic_wait_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1654 {
1655 	struct qaic_wait *args = data;
1656 	int usr_rcu_id, qdev_rcu_id;
1657 	struct dma_bridge_chan *dbc;
1658 	struct drm_gem_object *obj;
1659 	struct qaic_device *qdev;
1660 	unsigned long timeout;
1661 	struct qaic_user *usr;
1662 	struct qaic_bo *bo;
1663 	int rcu_id;
1664 	int ret;
1665 
1666 	if (args->pad != 0)
1667 		return -EINVAL;
1668 
1669 	usr = file_priv->driver_priv;
1670 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1671 	if (!usr->qddev) {
1672 		ret = -ENODEV;
1673 		goto unlock_usr_srcu;
1674 	}
1675 
1676 	qdev = usr->qddev->qdev;
1677 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1678 	if (qdev->in_reset) {
1679 		ret = -ENODEV;
1680 		goto unlock_dev_srcu;
1681 	}
1682 
1683 	if (args->dbc_id >= qdev->num_dbc) {
1684 		ret = -EINVAL;
1685 		goto unlock_dev_srcu;
1686 	}
1687 
1688 	dbc = &qdev->dbc[args->dbc_id];
1689 
1690 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1691 	if (dbc->usr != usr) {
1692 		ret = -EPERM;
1693 		goto unlock_ch_srcu;
1694 	}
1695 
1696 	obj = drm_gem_object_lookup(file_priv, args->handle);
1697 	if (!obj) {
1698 		ret = -ENOENT;
1699 		goto unlock_ch_srcu;
1700 	}
1701 
1702 	bo = to_qaic_bo(obj);
1703 	timeout = args->timeout ? args->timeout : wait_exec_default_timeout_ms;
1704 	timeout = msecs_to_jiffies(timeout);
1705 	ret = wait_for_completion_interruptible_timeout(&bo->xfer_done, timeout);
1706 	if (!ret) {
1707 		ret = -ETIMEDOUT;
1708 		goto put_obj;
1709 	}
1710 	if (ret > 0)
1711 		ret = 0;
1712 
1713 	if (!dbc->usr)
1714 		ret = -EPERM;
1715 
1716 put_obj:
1717 	drm_gem_object_put(obj);
1718 unlock_ch_srcu:
1719 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1720 unlock_dev_srcu:
1721 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1722 unlock_usr_srcu:
1723 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1724 	return ret;
1725 }
1726 
qaic_perf_stats_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1727 int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1728 {
1729 	struct qaic_perf_stats_entry *ent = NULL;
1730 	struct qaic_perf_stats *args = data;
1731 	int usr_rcu_id, qdev_rcu_id;
1732 	struct drm_gem_object *obj;
1733 	struct qaic_device *qdev;
1734 	struct qaic_user *usr;
1735 	struct qaic_bo *bo;
1736 	int ret, i;
1737 
1738 	usr = file_priv->driver_priv;
1739 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1740 	if (!usr->qddev) {
1741 		ret = -ENODEV;
1742 		goto unlock_usr_srcu;
1743 	}
1744 
1745 	qdev = usr->qddev->qdev;
1746 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1747 	if (qdev->in_reset) {
1748 		ret = -ENODEV;
1749 		goto unlock_dev_srcu;
1750 	}
1751 
1752 	if (args->hdr.dbc_id >= qdev->num_dbc) {
1753 		ret = -EINVAL;
1754 		goto unlock_dev_srcu;
1755 	}
1756 
1757 	ent = kcalloc(args->hdr.count, sizeof(*ent), GFP_KERNEL);
1758 	if (!ent) {
1759 		ret = -EINVAL;
1760 		goto unlock_dev_srcu;
1761 	}
1762 
1763 	ret = copy_from_user(ent, u64_to_user_ptr(args->data), args->hdr.count * sizeof(*ent));
1764 	if (ret) {
1765 		ret = -EFAULT;
1766 		goto free_ent;
1767 	}
1768 
1769 	for (i = 0; i < args->hdr.count; i++) {
1770 		obj = drm_gem_object_lookup(file_priv, ent[i].handle);
1771 		if (!obj) {
1772 			ret = -ENOENT;
1773 			goto free_ent;
1774 		}
1775 		bo = to_qaic_bo(obj);
1776 		/*
1777 		 * perf stats ioctl is called before wait ioctl is complete then
1778 		 * the latency information is invalid.
1779 		 */
1780 		if (bo->perf_stats.req_processed_ts < bo->perf_stats.req_submit_ts) {
1781 			ent[i].device_latency_us = 0;
1782 		} else {
1783 			ent[i].device_latency_us = div_u64((bo->perf_stats.req_processed_ts -
1784 							    bo->perf_stats.req_submit_ts), 1000);
1785 		}
1786 		ent[i].submit_latency_us = div_u64((bo->perf_stats.req_submit_ts -
1787 						    bo->perf_stats.req_received_ts), 1000);
1788 		ent[i].queue_level_before = bo->perf_stats.queue_level_before;
1789 		ent[i].num_queue_element = bo->total_slice_nents;
1790 		drm_gem_object_put(obj);
1791 	}
1792 
1793 	if (copy_to_user(u64_to_user_ptr(args->data), ent, args->hdr.count * sizeof(*ent)))
1794 		ret = -EFAULT;
1795 
1796 free_ent:
1797 	kfree(ent);
1798 unlock_dev_srcu:
1799 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1800 unlock_usr_srcu:
1801 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1802 	return ret;
1803 }
1804 
empty_xfer_list(struct qaic_device * qdev,struct dma_bridge_chan * dbc)1805 static void empty_xfer_list(struct qaic_device *qdev, struct dma_bridge_chan *dbc)
1806 {
1807 	unsigned long flags;
1808 	struct qaic_bo *bo;
1809 
1810 	spin_lock_irqsave(&dbc->xfer_lock, flags);
1811 	while (!list_empty(&dbc->xfer_list)) {
1812 		bo = list_first_entry(&dbc->xfer_list, typeof(*bo), xfer_list);
1813 		bo->queued = false;
1814 		list_del(&bo->xfer_list);
1815 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1816 		dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1817 		complete_all(&bo->xfer_done);
1818 		drm_gem_object_put(&bo->base);
1819 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1820 	}
1821 	spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1822 }
1823 
disable_dbc(struct qaic_device * qdev,u32 dbc_id,struct qaic_user * usr)1824 int disable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr)
1825 {
1826 	if (!qdev->dbc[dbc_id].usr || qdev->dbc[dbc_id].usr->handle != usr->handle)
1827 		return -EPERM;
1828 
1829 	qdev->dbc[dbc_id].usr = NULL;
1830 	synchronize_srcu(&qdev->dbc[dbc_id].ch_lock);
1831 	return 0;
1832 }
1833 
1834 /**
1835  * enable_dbc - Enable the DBC. DBCs are disabled by removing the context of
1836  * user. Add user context back to DBC to enable it. This function trusts the
1837  * DBC ID passed and expects the DBC to be disabled.
1838  * @qdev: Qranium device handle
1839  * @dbc_id: ID of the DBC
1840  * @usr: User context
1841  */
enable_dbc(struct qaic_device * qdev,u32 dbc_id,struct qaic_user * usr)1842 void enable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr)
1843 {
1844 	qdev->dbc[dbc_id].usr = usr;
1845 }
1846 
wakeup_dbc(struct qaic_device * qdev,u32 dbc_id)1847 void wakeup_dbc(struct qaic_device *qdev, u32 dbc_id)
1848 {
1849 	struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1850 
1851 	dbc->usr = NULL;
1852 	empty_xfer_list(qdev, dbc);
1853 	synchronize_srcu(&dbc->ch_lock);
1854 	/*
1855 	 * Threads holding channel lock, may add more elements in the xfer_list.
1856 	 * Flush out these elements from xfer_list.
1857 	 */
1858 	empty_xfer_list(qdev, dbc);
1859 }
1860 
release_dbc(struct qaic_device * qdev,u32 dbc_id)1861 void release_dbc(struct qaic_device *qdev, u32 dbc_id)
1862 {
1863 	struct bo_slice *slice, *slice_temp;
1864 	struct qaic_bo *bo, *bo_temp;
1865 	struct dma_bridge_chan *dbc;
1866 
1867 	dbc = &qdev->dbc[dbc_id];
1868 	if (!dbc->in_use)
1869 		return;
1870 
1871 	wakeup_dbc(qdev, dbc_id);
1872 
1873 	dma_free_coherent(&qdev->pdev->dev, dbc->total_size, dbc->req_q_base, dbc->dma_addr);
1874 	dbc->total_size = 0;
1875 	dbc->req_q_base = NULL;
1876 	dbc->dma_addr = 0;
1877 	dbc->nelem = 0;
1878 	dbc->usr = NULL;
1879 
1880 	list_for_each_entry_safe(bo, bo_temp, &dbc->bo_lists, bo_list) {
1881 		list_for_each_entry_safe(slice, slice_temp, &bo->slices, slice)
1882 			kref_put(&slice->ref_count, free_slice);
1883 		bo->sliced = false;
1884 		INIT_LIST_HEAD(&bo->slices);
1885 		bo->total_slice_nents = 0;
1886 		bo->dir = 0;
1887 		bo->dbc = NULL;
1888 		bo->nr_slice = 0;
1889 		bo->nr_slice_xfer_done = 0;
1890 		bo->queued = false;
1891 		bo->req_id = 0;
1892 		init_completion(&bo->xfer_done);
1893 		complete_all(&bo->xfer_done);
1894 		list_del(&bo->bo_list);
1895 		bo->perf_stats.req_received_ts = 0;
1896 		bo->perf_stats.req_submit_ts = 0;
1897 		bo->perf_stats.req_processed_ts = 0;
1898 		bo->perf_stats.queue_level_before = 0;
1899 	}
1900 
1901 	dbc->in_use = false;
1902 	wake_up(&dbc->dbc_release);
1903 }
1904