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