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 (drm_gem_is_imported(obj))
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 (drm_gem_is_imported(obj)) {
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 drm_gem_object_put(obj);
735 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
736 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
737
738 return 0;
739
740 free_bo:
741 drm_gem_object_put(obj);
742 unlock_dev_srcu:
743 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
744 unlock_usr_srcu:
745 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
746 return ret;
747 }
748
qaic_mmap_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)749 int qaic_mmap_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
750 {
751 struct qaic_mmap_bo *args = data;
752 int usr_rcu_id, qdev_rcu_id;
753 struct drm_gem_object *obj;
754 struct qaic_device *qdev;
755 struct qaic_user *usr;
756 int ret = 0;
757
758 usr = file_priv->driver_priv;
759 usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
760 if (!usr->qddev) {
761 ret = -ENODEV;
762 goto unlock_usr_srcu;
763 }
764
765 qdev = usr->qddev->qdev;
766 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
767 if (qdev->dev_state != QAIC_ONLINE) {
768 ret = -ENODEV;
769 goto unlock_dev_srcu;
770 }
771
772 obj = drm_gem_object_lookup(file_priv, args->handle);
773 if (!obj) {
774 ret = -ENOENT;
775 goto unlock_dev_srcu;
776 }
777
778 args->offset = drm_vma_node_offset_addr(&obj->vma_node);
779
780 drm_gem_object_put(obj);
781
782 unlock_dev_srcu:
783 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
784 unlock_usr_srcu:
785 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
786 return ret;
787 }
788
qaic_gem_prime_import(struct drm_device * dev,struct dma_buf * dma_buf)789 struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
790 {
791 struct dma_buf_attachment *attach;
792 struct drm_gem_object *obj;
793 struct qaic_bo *bo;
794 int ret;
795
796 bo = qaic_alloc_init_bo();
797 if (IS_ERR(bo)) {
798 ret = PTR_ERR(bo);
799 goto out;
800 }
801
802 obj = &bo->base;
803 get_dma_buf(dma_buf);
804
805 attach = dma_buf_attach(dma_buf, dev->dev);
806 if (IS_ERR(attach)) {
807 ret = PTR_ERR(attach);
808 goto attach_fail;
809 }
810
811 if (!attach->dmabuf->size) {
812 ret = -EINVAL;
813 goto size_align_fail;
814 }
815
816 drm_gem_private_object_init(dev, obj, attach->dmabuf->size);
817 /*
818 * skipping dma_buf_map_attachment() as we do not know the direction
819 * just yet. Once the direction is known in the subsequent IOCTL to
820 * attach slicing, we can do it then.
821 */
822
823 obj->funcs = &qaic_gem_funcs;
824 obj->import_attach = attach;
825 obj->resv = dma_buf->resv;
826
827 return obj;
828
829 size_align_fail:
830 dma_buf_detach(dma_buf, attach);
831 attach_fail:
832 dma_buf_put(dma_buf);
833 kfree(bo);
834 out:
835 return ERR_PTR(ret);
836 }
837
qaic_prepare_import_bo(struct qaic_bo * bo,struct qaic_attach_slice_hdr * hdr)838 static int qaic_prepare_import_bo(struct qaic_bo *bo, struct qaic_attach_slice_hdr *hdr)
839 {
840 struct drm_gem_object *obj = &bo->base;
841 struct sg_table *sgt;
842 int ret;
843
844 sgt = dma_buf_map_attachment(obj->import_attach, hdr->dir);
845 if (IS_ERR(sgt)) {
846 ret = PTR_ERR(sgt);
847 return ret;
848 }
849
850 bo->sgt = sgt;
851
852 return 0;
853 }
854
qaic_prepare_export_bo(struct qaic_device * qdev,struct qaic_bo * bo,struct qaic_attach_slice_hdr * hdr)855 static int qaic_prepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo,
856 struct qaic_attach_slice_hdr *hdr)
857 {
858 int ret;
859
860 ret = dma_map_sgtable(&qdev->pdev->dev, bo->sgt, hdr->dir, 0);
861 if (ret)
862 return -EFAULT;
863
864 return 0;
865 }
866
qaic_prepare_bo(struct qaic_device * qdev,struct qaic_bo * bo,struct qaic_attach_slice_hdr * hdr)867 static int qaic_prepare_bo(struct qaic_device *qdev, struct qaic_bo *bo,
868 struct qaic_attach_slice_hdr *hdr)
869 {
870 int ret;
871
872 if (drm_gem_is_imported(&bo->base))
873 ret = qaic_prepare_import_bo(bo, hdr);
874 else
875 ret = qaic_prepare_export_bo(qdev, bo, hdr);
876 bo->dir = hdr->dir;
877 bo->dbc = &qdev->dbc[hdr->dbc_id];
878 bo->nr_slice = hdr->count;
879
880 return ret;
881 }
882
qaic_unprepare_import_bo(struct qaic_bo * bo)883 static void qaic_unprepare_import_bo(struct qaic_bo *bo)
884 {
885 dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, bo->dir);
886 bo->sgt = NULL;
887 }
888
qaic_unprepare_export_bo(struct qaic_device * qdev,struct qaic_bo * bo)889 static void qaic_unprepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo)
890 {
891 dma_unmap_sgtable(&qdev->pdev->dev, bo->sgt, bo->dir, 0);
892 }
893
qaic_unprepare_bo(struct qaic_device * qdev,struct qaic_bo * bo)894 static void qaic_unprepare_bo(struct qaic_device *qdev, struct qaic_bo *bo)
895 {
896 if (drm_gem_is_imported(&bo->base))
897 qaic_unprepare_import_bo(bo);
898 else
899 qaic_unprepare_export_bo(qdev, bo);
900
901 bo->dir = 0;
902 bo->dbc = NULL;
903 bo->nr_slice = 0;
904 }
905
qaic_free_slices_bo(struct qaic_bo * bo)906 static void qaic_free_slices_bo(struct qaic_bo *bo)
907 {
908 struct bo_slice *slice, *temp;
909
910 list_for_each_entry_safe(slice, temp, &bo->slices, slice)
911 kref_put(&slice->ref_count, free_slice);
912 if (WARN_ON_ONCE(bo->total_slice_nents != 0))
913 bo->total_slice_nents = 0;
914 bo->nr_slice = 0;
915 }
916
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)917 static int qaic_attach_slicing_bo(struct qaic_device *qdev, struct qaic_bo *bo,
918 struct qaic_attach_slice_hdr *hdr,
919 struct qaic_attach_slice_entry *slice_ent)
920 {
921 int ret, i;
922
923 for (i = 0; i < hdr->count; i++) {
924 ret = qaic_map_one_slice(qdev, bo, &slice_ent[i]);
925 if (ret) {
926 qaic_free_slices_bo(bo);
927 return ret;
928 }
929 }
930
931 if (bo->total_slice_nents > bo->dbc->nelem) {
932 qaic_free_slices_bo(bo);
933 return -ENOSPC;
934 }
935
936 return 0;
937 }
938
qaic_attach_slice_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)939 int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
940 {
941 struct qaic_attach_slice_entry *slice_ent;
942 struct qaic_attach_slice *args = data;
943 int rcu_id, usr_rcu_id, qdev_rcu_id;
944 struct dma_bridge_chan *dbc;
945 struct drm_gem_object *obj;
946 struct qaic_device *qdev;
947 unsigned long arg_size;
948 struct qaic_user *usr;
949 u8 __user *user_data;
950 struct qaic_bo *bo;
951 int ret;
952
953 if (args->hdr.count == 0)
954 return -EINVAL;
955
956 arg_size = args->hdr.count * sizeof(*slice_ent);
957 if (arg_size / args->hdr.count != sizeof(*slice_ent))
958 return -EINVAL;
959
960 if (!(args->hdr.dir == DMA_TO_DEVICE || args->hdr.dir == DMA_FROM_DEVICE))
961 return -EINVAL;
962
963 if (args->data == 0)
964 return -EINVAL;
965
966 usr = file_priv->driver_priv;
967 usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
968 if (!usr->qddev) {
969 ret = -ENODEV;
970 goto unlock_usr_srcu;
971 }
972
973 qdev = usr->qddev->qdev;
974 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
975 if (qdev->dev_state != QAIC_ONLINE) {
976 ret = -ENODEV;
977 goto unlock_dev_srcu;
978 }
979
980 if (args->hdr.dbc_id >= qdev->num_dbc) {
981 ret = -EINVAL;
982 goto unlock_dev_srcu;
983 }
984
985 user_data = u64_to_user_ptr(args->data);
986
987 slice_ent = kzalloc(arg_size, GFP_KERNEL);
988 if (!slice_ent) {
989 ret = -EINVAL;
990 goto unlock_dev_srcu;
991 }
992
993 ret = copy_from_user(slice_ent, user_data, arg_size);
994 if (ret) {
995 ret = -EFAULT;
996 goto free_slice_ent;
997 }
998
999 obj = drm_gem_object_lookup(file_priv, args->hdr.handle);
1000 if (!obj) {
1001 ret = -ENOENT;
1002 goto free_slice_ent;
1003 }
1004
1005 ret = qaic_validate_req(qdev, slice_ent, args->hdr.count, obj->size);
1006 if (ret)
1007 goto put_bo;
1008
1009 bo = to_qaic_bo(obj);
1010 ret = mutex_lock_interruptible(&bo->lock);
1011 if (ret)
1012 goto put_bo;
1013
1014 if (bo->sliced) {
1015 ret = -EINVAL;
1016 goto unlock_bo;
1017 }
1018
1019 dbc = &qdev->dbc[args->hdr.dbc_id];
1020 rcu_id = srcu_read_lock(&dbc->ch_lock);
1021 if (dbc->usr != usr) {
1022 ret = -EINVAL;
1023 goto unlock_ch_srcu;
1024 }
1025
1026 ret = qaic_prepare_bo(qdev, bo, &args->hdr);
1027 if (ret)
1028 goto unlock_ch_srcu;
1029
1030 ret = qaic_attach_slicing_bo(qdev, bo, &args->hdr, slice_ent);
1031 if (ret)
1032 goto unprepare_bo;
1033
1034 if (args->hdr.dir == DMA_TO_DEVICE)
1035 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, args->hdr.dir);
1036
1037 bo->sliced = true;
1038 list_add_tail(&bo->bo_list, &bo->dbc->bo_lists);
1039 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1040 mutex_unlock(&bo->lock);
1041 kfree(slice_ent);
1042 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1043 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1044
1045 return 0;
1046
1047 unprepare_bo:
1048 qaic_unprepare_bo(qdev, bo);
1049 unlock_ch_srcu:
1050 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1051 unlock_bo:
1052 mutex_unlock(&bo->lock);
1053 put_bo:
1054 drm_gem_object_put(obj);
1055 free_slice_ent:
1056 kfree(slice_ent);
1057 unlock_dev_srcu:
1058 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1059 unlock_usr_srcu:
1060 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1061 return ret;
1062 }
1063
fifo_space_avail(u32 head,u32 tail,u32 q_size)1064 static inline u32 fifo_space_avail(u32 head, u32 tail, u32 q_size)
1065 {
1066 u32 avail = head - tail - 1;
1067
1068 if (head <= tail)
1069 avail += q_size;
1070
1071 return avail;
1072 }
1073
copy_exec_reqs(struct qaic_device * qdev,struct bo_slice * slice,u32 dbc_id,u32 head,u32 * ptail)1074 static inline int copy_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice, u32 dbc_id,
1075 u32 head, u32 *ptail)
1076 {
1077 struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1078 struct dbc_req *reqs = slice->reqs;
1079 u32 tail = *ptail;
1080 u32 avail;
1081
1082 avail = fifo_space_avail(head, tail, dbc->nelem);
1083 if (avail < slice->nents)
1084 return -EAGAIN;
1085
1086 if (tail + slice->nents > dbc->nelem) {
1087 avail = dbc->nelem - tail;
1088 avail = min_t(u32, avail, slice->nents);
1089 memcpy(fifo_at(dbc->req_q_base, tail), reqs, sizeof(*reqs) * avail);
1090 reqs += avail;
1091 avail = slice->nents - avail;
1092 if (avail)
1093 memcpy(dbc->req_q_base, reqs, sizeof(*reqs) * avail);
1094 } else {
1095 memcpy(fifo_at(dbc->req_q_base, tail), reqs, sizeof(*reqs) * slice->nents);
1096 }
1097
1098 *ptail = (tail + slice->nents) % dbc->nelem;
1099
1100 return 0;
1101 }
1102
copy_partial_exec_reqs(struct qaic_device * qdev,struct bo_slice * slice,u64 resize,struct dma_bridge_chan * dbc,u32 head,u32 * ptail)1103 static inline int copy_partial_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice,
1104 u64 resize, struct dma_bridge_chan *dbc, u32 head,
1105 u32 *ptail)
1106 {
1107 struct dbc_req *reqs = slice->reqs;
1108 struct dbc_req *last_req;
1109 u32 tail = *ptail;
1110 u64 last_bytes;
1111 u32 first_n;
1112 u32 avail;
1113
1114 avail = fifo_space_avail(head, tail, dbc->nelem);
1115
1116 /*
1117 * After this for loop is complete, first_n represents the index
1118 * of the last DMA request of this slice that needs to be
1119 * transferred after resizing and last_bytes represents DMA size
1120 * of that request.
1121 */
1122 last_bytes = resize;
1123 for (first_n = 0; first_n < slice->nents; first_n++)
1124 if (last_bytes > le32_to_cpu(reqs[first_n].len))
1125 last_bytes -= le32_to_cpu(reqs[first_n].len);
1126 else
1127 break;
1128
1129 if (avail < (first_n + 1))
1130 return -EAGAIN;
1131
1132 if (first_n) {
1133 if (tail + first_n > dbc->nelem) {
1134 avail = dbc->nelem - tail;
1135 avail = min_t(u32, avail, first_n);
1136 memcpy(fifo_at(dbc->req_q_base, tail), reqs, sizeof(*reqs) * avail);
1137 last_req = reqs + avail;
1138 avail = first_n - avail;
1139 if (avail)
1140 memcpy(dbc->req_q_base, last_req, sizeof(*reqs) * avail);
1141 } else {
1142 memcpy(fifo_at(dbc->req_q_base, tail), reqs, sizeof(*reqs) * first_n);
1143 }
1144 }
1145
1146 /*
1147 * Copy over the last entry. Here we need to adjust len to the left over
1148 * size, and set src and dst to the entry it is copied to.
1149 */
1150 last_req = fifo_at(dbc->req_q_base, (tail + first_n) % dbc->nelem);
1151 memcpy(last_req, reqs + slice->nents - 1, sizeof(*reqs));
1152
1153 /*
1154 * last_bytes holds size of a DMA segment, maximum DMA segment size is
1155 * set to UINT_MAX by qaic and hence last_bytes can never exceed u32
1156 * range. So, by down sizing we are not corrupting the value.
1157 */
1158 last_req->len = cpu_to_le32((u32)last_bytes);
1159 last_req->src_addr = reqs[first_n].src_addr;
1160 last_req->dest_addr = reqs[first_n].dest_addr;
1161 if (!last_bytes)
1162 /* Disable DMA transfer */
1163 last_req->cmd = GENMASK(7, 2) & reqs[first_n].cmd;
1164
1165 *ptail = (tail + first_n + 1) % dbc->nelem;
1166
1167 return 0;
1168 }
1169
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)1170 static int send_bo_list_to_device(struct qaic_device *qdev, struct drm_file *file_priv,
1171 struct qaic_execute_entry *exec, unsigned int count,
1172 bool is_partial, struct dma_bridge_chan *dbc, u32 head,
1173 u32 *tail)
1174 {
1175 struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec;
1176 struct drm_gem_object *obj;
1177 struct bo_slice *slice;
1178 unsigned long flags;
1179 struct qaic_bo *bo;
1180 int i, j;
1181 int ret;
1182
1183 for (i = 0; i < count; i++) {
1184 /*
1185 * ref count will be decremented when the transfer of this
1186 * buffer is complete. It is inside dbc_irq_threaded_fn().
1187 */
1188 obj = drm_gem_object_lookup(file_priv,
1189 is_partial ? pexec[i].handle : exec[i].handle);
1190 if (!obj) {
1191 ret = -ENOENT;
1192 goto failed_to_send_bo;
1193 }
1194
1195 bo = to_qaic_bo(obj);
1196 ret = mutex_lock_interruptible(&bo->lock);
1197 if (ret)
1198 goto failed_to_send_bo;
1199
1200 if (!bo->sliced) {
1201 ret = -EINVAL;
1202 goto unlock_bo;
1203 }
1204
1205 if (is_partial && pexec[i].resize > bo->base.size) {
1206 ret = -EINVAL;
1207 goto unlock_bo;
1208 }
1209
1210 spin_lock_irqsave(&dbc->xfer_lock, flags);
1211 if (bo_queued(bo)) {
1212 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1213 ret = -EINVAL;
1214 goto unlock_bo;
1215 }
1216
1217 bo->req_id = dbc->next_req_id++;
1218
1219 list_for_each_entry(slice, &bo->slices, slice) {
1220 for (j = 0; j < slice->nents; j++)
1221 slice->reqs[j].req_id = cpu_to_le16(bo->req_id);
1222
1223 if (is_partial && (!pexec[i].resize || pexec[i].resize <= slice->offset))
1224 /* Configure the slice for no DMA transfer */
1225 ret = copy_partial_exec_reqs(qdev, slice, 0, dbc, head, tail);
1226 else if (is_partial && pexec[i].resize < slice->offset + slice->size)
1227 /* Configure the slice to be partially DMA transferred */
1228 ret = copy_partial_exec_reqs(qdev, slice,
1229 pexec[i].resize - slice->offset, dbc,
1230 head, tail);
1231 else
1232 ret = copy_exec_reqs(qdev, slice, dbc->id, head, tail);
1233 if (ret) {
1234 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1235 goto unlock_bo;
1236 }
1237 }
1238 reinit_completion(&bo->xfer_done);
1239 list_add_tail(&bo->xfer_list, &dbc->xfer_list);
1240 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1241 dma_sync_sgtable_for_device(&qdev->pdev->dev, bo->sgt, bo->dir);
1242 mutex_unlock(&bo->lock);
1243 }
1244
1245 return 0;
1246
1247 unlock_bo:
1248 mutex_unlock(&bo->lock);
1249 failed_to_send_bo:
1250 if (likely(obj))
1251 drm_gem_object_put(obj);
1252 for (j = 0; j < i; j++) {
1253 spin_lock_irqsave(&dbc->xfer_lock, flags);
1254 bo = list_last_entry(&dbc->xfer_list, struct qaic_bo, xfer_list);
1255 obj = &bo->base;
1256 list_del_init(&bo->xfer_list);
1257 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1258 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1259 drm_gem_object_put(obj);
1260 }
1261 return ret;
1262 }
1263
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)1264 static void update_profiling_data(struct drm_file *file_priv,
1265 struct qaic_execute_entry *exec, unsigned int count,
1266 bool is_partial, u64 received_ts, u64 submit_ts, u32 queue_level)
1267 {
1268 struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec;
1269 struct drm_gem_object *obj;
1270 struct qaic_bo *bo;
1271 int i;
1272
1273 for (i = 0; i < count; i++) {
1274 /*
1275 * Since we already committed the BO to hardware, the only way
1276 * this should fail is a pending signal. We can't cancel the
1277 * submit to hardware, so we have to just skip the profiling
1278 * data. In case the signal is not fatal to the process, we
1279 * return success so that the user doesn't try to resubmit.
1280 */
1281 obj = drm_gem_object_lookup(file_priv,
1282 is_partial ? pexec[i].handle : exec[i].handle);
1283 if (!obj)
1284 break;
1285 bo = to_qaic_bo(obj);
1286 bo->perf_stats.req_received_ts = received_ts;
1287 bo->perf_stats.req_submit_ts = submit_ts;
1288 bo->perf_stats.queue_level_before = queue_level;
1289 queue_level += bo->total_slice_nents;
1290 drm_gem_object_put(obj);
1291 }
1292 }
1293
__qaic_execute_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv,bool is_partial)1294 static int __qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv,
1295 bool is_partial)
1296 {
1297 struct qaic_execute *args = data;
1298 struct qaic_execute_entry *exec;
1299 struct dma_bridge_chan *dbc;
1300 int usr_rcu_id, qdev_rcu_id;
1301 struct qaic_device *qdev;
1302 struct qaic_user *usr;
1303 u8 __user *user_data;
1304 unsigned long n;
1305 u64 received_ts;
1306 u32 queue_level;
1307 u64 submit_ts;
1308 int rcu_id;
1309 u32 head;
1310 u32 tail;
1311 u64 size;
1312 int ret;
1313
1314 received_ts = ktime_get_ns();
1315
1316 size = is_partial ? sizeof(struct qaic_partial_execute_entry) : sizeof(*exec);
1317 n = (unsigned long)size * args->hdr.count;
1318 if (args->hdr.count == 0 || n / args->hdr.count != size)
1319 return -EINVAL;
1320
1321 user_data = u64_to_user_ptr(args->data);
1322
1323 exec = kcalloc(args->hdr.count, size, GFP_KERNEL);
1324 if (!exec)
1325 return -ENOMEM;
1326
1327 if (copy_from_user(exec, user_data, n)) {
1328 ret = -EFAULT;
1329 goto free_exec;
1330 }
1331
1332 usr = file_priv->driver_priv;
1333 usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1334 if (!usr->qddev) {
1335 ret = -ENODEV;
1336 goto unlock_usr_srcu;
1337 }
1338
1339 qdev = usr->qddev->qdev;
1340 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1341 if (qdev->dev_state != QAIC_ONLINE) {
1342 ret = -ENODEV;
1343 goto unlock_dev_srcu;
1344 }
1345
1346 if (args->hdr.dbc_id >= qdev->num_dbc) {
1347 ret = -EINVAL;
1348 goto unlock_dev_srcu;
1349 }
1350
1351 dbc = &qdev->dbc[args->hdr.dbc_id];
1352
1353 rcu_id = srcu_read_lock(&dbc->ch_lock);
1354 if (!dbc->usr || dbc->usr->handle != usr->handle) {
1355 ret = -EPERM;
1356 goto release_ch_rcu;
1357 }
1358
1359 ret = mutex_lock_interruptible(&dbc->req_lock);
1360 if (ret)
1361 goto release_ch_rcu;
1362
1363 head = readl(dbc->dbc_base + REQHP_OFF);
1364 tail = readl(dbc->dbc_base + REQTP_OFF);
1365
1366 if (head == U32_MAX || tail == U32_MAX) {
1367 /* PCI link error */
1368 ret = -ENODEV;
1369 goto unlock_req_lock;
1370 }
1371
1372 queue_level = head <= tail ? tail - head : dbc->nelem - (head - tail);
1373
1374 ret = send_bo_list_to_device(qdev, file_priv, exec, args->hdr.count, is_partial, dbc,
1375 head, &tail);
1376 if (ret)
1377 goto unlock_req_lock;
1378
1379 /* Finalize commit to hardware */
1380 submit_ts = ktime_get_ns();
1381 writel(tail, dbc->dbc_base + REQTP_OFF);
1382 mutex_unlock(&dbc->req_lock);
1383
1384 update_profiling_data(file_priv, exec, args->hdr.count, is_partial, received_ts,
1385 submit_ts, queue_level);
1386
1387 if (datapath_polling)
1388 schedule_work(&dbc->poll_work);
1389
1390 unlock_req_lock:
1391 if (ret)
1392 mutex_unlock(&dbc->req_lock);
1393 release_ch_rcu:
1394 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1395 unlock_dev_srcu:
1396 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1397 unlock_usr_srcu:
1398 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1399 free_exec:
1400 kfree(exec);
1401 return ret;
1402 }
1403
qaic_execute_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1404 int qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1405 {
1406 return __qaic_execute_bo_ioctl(dev, data, file_priv, false);
1407 }
1408
qaic_partial_execute_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1409 int qaic_partial_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1410 {
1411 return __qaic_execute_bo_ioctl(dev, data, file_priv, true);
1412 }
1413
1414 /*
1415 * Our interrupt handling is a bit more complicated than a simple ideal, but
1416 * sadly necessary.
1417 *
1418 * Each dbc has a completion queue. Entries in the queue correspond to DMA
1419 * requests which the device has processed. The hardware already has a built
1420 * in irq mitigation. When the device puts an entry into the queue, it will
1421 * only trigger an interrupt if the queue was empty. Therefore, when adding
1422 * the Nth event to a non-empty queue, the hardware doesn't trigger an
1423 * interrupt. This means the host doesn't get additional interrupts signaling
1424 * the same thing - the queue has something to process.
1425 * This behavior can be overridden in the DMA request.
1426 * This means that when the host receives an interrupt, it is required to
1427 * drain the queue.
1428 *
1429 * This behavior is what NAPI attempts to accomplish, although we can't use
1430 * NAPI as we don't have a netdev. We use threaded irqs instead.
1431 *
1432 * However, there is a situation where the host drains the queue fast enough
1433 * that every event causes an interrupt. Typically this is not a problem as
1434 * the rate of events would be low. However, that is not the case with
1435 * lprnet for example. On an Intel Xeon D-2191 where we run 8 instances of
1436 * lprnet, the host receives roughly 80k interrupts per second from the device
1437 * (per /proc/interrupts). While NAPI documentation indicates the host should
1438 * just chug along, sadly that behavior causes instability in some hosts.
1439 *
1440 * Therefore, we implement an interrupt disable scheme similar to NAPI. The
1441 * key difference is that we will delay after draining the queue for a small
1442 * time to allow additional events to come in via polling. Using the above
1443 * lprnet workload, this reduces the number of interrupts processed from
1444 * ~80k/sec to about 64 in 5 minutes and appears to solve the system
1445 * instability.
1446 */
dbc_irq_handler(int irq,void * data)1447 irqreturn_t dbc_irq_handler(int irq, void *data)
1448 {
1449 struct dma_bridge_chan *dbc = data;
1450 int rcu_id;
1451 u32 head;
1452 u32 tail;
1453
1454 rcu_id = srcu_read_lock(&dbc->ch_lock);
1455
1456 if (datapath_polling) {
1457 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1458 /*
1459 * Normally datapath_polling will not have irqs enabled, but
1460 * when running with only one MSI the interrupt is shared with
1461 * MHI so it cannot be disabled. Return ASAP instead.
1462 */
1463 return IRQ_HANDLED;
1464 }
1465
1466 if (!dbc->usr) {
1467 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1468 return IRQ_HANDLED;
1469 }
1470
1471 head = readl(dbc->dbc_base + RSPHP_OFF);
1472 if (head == U32_MAX) { /* PCI link error */
1473 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1474 return IRQ_NONE;
1475 }
1476
1477 tail = readl(dbc->dbc_base + RSPTP_OFF);
1478 if (tail == U32_MAX) { /* PCI link error */
1479 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1480 return IRQ_NONE;
1481 }
1482
1483 if (head == tail) { /* queue empty */
1484 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1485 return IRQ_NONE;
1486 }
1487
1488 if (!dbc->qdev->single_msi)
1489 disable_irq_nosync(irq);
1490 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1491 return IRQ_WAKE_THREAD;
1492 }
1493
irq_polling_work(struct work_struct * work)1494 void irq_polling_work(struct work_struct *work)
1495 {
1496 struct dma_bridge_chan *dbc = container_of(work, struct dma_bridge_chan, poll_work);
1497 unsigned long flags;
1498 int rcu_id;
1499 u32 head;
1500 u32 tail;
1501
1502 rcu_id = srcu_read_lock(&dbc->ch_lock);
1503
1504 while (1) {
1505 if (dbc->qdev->dev_state != QAIC_ONLINE) {
1506 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1507 return;
1508 }
1509 if (!dbc->usr) {
1510 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1511 return;
1512 }
1513 spin_lock_irqsave(&dbc->xfer_lock, flags);
1514 if (list_empty(&dbc->xfer_list)) {
1515 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1516 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1517 return;
1518 }
1519 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1520
1521 head = readl(dbc->dbc_base + RSPHP_OFF);
1522 if (head == U32_MAX) { /* PCI link error */
1523 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1524 return;
1525 }
1526
1527 tail = readl(dbc->dbc_base + RSPTP_OFF);
1528 if (tail == U32_MAX) { /* PCI link error */
1529 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1530 return;
1531 }
1532
1533 if (head != tail) {
1534 irq_wake_thread(dbc->irq, dbc);
1535 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1536 return;
1537 }
1538
1539 cond_resched();
1540 usleep_range(datapath_poll_interval_us, 2 * datapath_poll_interval_us);
1541 }
1542 }
1543
dbc_irq_threaded_fn(int irq,void * data)1544 irqreturn_t dbc_irq_threaded_fn(int irq, void *data)
1545 {
1546 struct dma_bridge_chan *dbc = data;
1547 int event_count = NUM_EVENTS;
1548 int delay_count = NUM_DELAYS;
1549 struct qaic_device *qdev;
1550 struct qaic_bo *bo, *i;
1551 struct dbc_rsp *rsp;
1552 unsigned long flags;
1553 int rcu_id;
1554 u16 status;
1555 u16 req_id;
1556 u32 head;
1557 u32 tail;
1558
1559 rcu_id = srcu_read_lock(&dbc->ch_lock);
1560 qdev = dbc->qdev;
1561
1562 head = readl(dbc->dbc_base + RSPHP_OFF);
1563 if (head == U32_MAX) /* PCI link error */
1564 goto error_out;
1565
1566 read_fifo:
1567
1568 if (!event_count) {
1569 event_count = NUM_EVENTS;
1570 cond_resched();
1571 }
1572
1573 /*
1574 * if this channel isn't assigned or gets unassigned during processing
1575 * we have nothing further to do
1576 */
1577 if (!dbc->usr)
1578 goto error_out;
1579
1580 tail = readl(dbc->dbc_base + RSPTP_OFF);
1581 if (tail == U32_MAX) /* PCI link error */
1582 goto error_out;
1583
1584 if (head == tail) { /* queue empty */
1585 if (delay_count) {
1586 --delay_count;
1587 usleep_range(100, 200);
1588 goto read_fifo; /* check for a new event */
1589 }
1590 goto normal_out;
1591 }
1592
1593 delay_count = NUM_DELAYS;
1594 while (head != tail) {
1595 if (!event_count)
1596 break;
1597 --event_count;
1598 rsp = dbc->rsp_q_base + head * sizeof(*rsp);
1599 req_id = le16_to_cpu(rsp->req_id);
1600 status = le16_to_cpu(rsp->status);
1601 if (status)
1602 pci_dbg(qdev->pdev, "req_id %d failed with status %d\n", req_id, status);
1603 spin_lock_irqsave(&dbc->xfer_lock, flags);
1604 /*
1605 * A BO can receive multiple interrupts, since a BO can be
1606 * divided into multiple slices and a buffer receives as many
1607 * interrupts as slices. So until it receives interrupts for
1608 * all the slices we cannot mark that buffer complete.
1609 */
1610 list_for_each_entry_safe(bo, i, &dbc->xfer_list, xfer_list) {
1611 if (bo->req_id == req_id)
1612 bo->nr_slice_xfer_done++;
1613 else
1614 continue;
1615
1616 if (bo->nr_slice_xfer_done < bo->nr_slice)
1617 break;
1618
1619 /*
1620 * At this point we have received all the interrupts for
1621 * BO, which means BO execution is complete.
1622 */
1623 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1624 bo->nr_slice_xfer_done = 0;
1625 list_del_init(&bo->xfer_list);
1626 bo->perf_stats.req_processed_ts = ktime_get_ns();
1627 complete_all(&bo->xfer_done);
1628 drm_gem_object_put(&bo->base);
1629 break;
1630 }
1631 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1632 head = (head + 1) % dbc->nelem;
1633 }
1634
1635 /*
1636 * Update the head pointer of response queue and let the device know
1637 * that we have consumed elements from the queue.
1638 */
1639 writel(head, dbc->dbc_base + RSPHP_OFF);
1640
1641 /* elements might have been put in the queue while we were processing */
1642 goto read_fifo;
1643
1644 normal_out:
1645 if (!qdev->single_msi && likely(!datapath_polling))
1646 enable_irq(irq);
1647 else if (unlikely(datapath_polling))
1648 schedule_work(&dbc->poll_work);
1649 /* checking the fifo and enabling irqs is a race, missed event check */
1650 tail = readl(dbc->dbc_base + RSPTP_OFF);
1651 if (tail != U32_MAX && head != tail) {
1652 if (!qdev->single_msi && likely(!datapath_polling))
1653 disable_irq_nosync(irq);
1654 goto read_fifo;
1655 }
1656 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1657 return IRQ_HANDLED;
1658
1659 error_out:
1660 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1661 if (!qdev->single_msi && likely(!datapath_polling))
1662 enable_irq(irq);
1663 else if (unlikely(datapath_polling))
1664 schedule_work(&dbc->poll_work);
1665
1666 return IRQ_HANDLED;
1667 }
1668
qaic_wait_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1669 int qaic_wait_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1670 {
1671 struct qaic_wait *args = data;
1672 int usr_rcu_id, qdev_rcu_id;
1673 struct dma_bridge_chan *dbc;
1674 struct drm_gem_object *obj;
1675 struct qaic_device *qdev;
1676 unsigned long timeout;
1677 struct qaic_user *usr;
1678 struct qaic_bo *bo;
1679 int rcu_id;
1680 int ret;
1681
1682 if (args->pad != 0)
1683 return -EINVAL;
1684
1685 usr = file_priv->driver_priv;
1686 usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1687 if (!usr->qddev) {
1688 ret = -ENODEV;
1689 goto unlock_usr_srcu;
1690 }
1691
1692 qdev = usr->qddev->qdev;
1693 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1694 if (qdev->dev_state != QAIC_ONLINE) {
1695 ret = -ENODEV;
1696 goto unlock_dev_srcu;
1697 }
1698
1699 if (args->dbc_id >= qdev->num_dbc) {
1700 ret = -EINVAL;
1701 goto unlock_dev_srcu;
1702 }
1703
1704 dbc = &qdev->dbc[args->dbc_id];
1705
1706 rcu_id = srcu_read_lock(&dbc->ch_lock);
1707 if (dbc->usr != usr) {
1708 ret = -EPERM;
1709 goto unlock_ch_srcu;
1710 }
1711
1712 obj = drm_gem_object_lookup(file_priv, args->handle);
1713 if (!obj) {
1714 ret = -ENOENT;
1715 goto unlock_ch_srcu;
1716 }
1717
1718 bo = to_qaic_bo(obj);
1719 timeout = args->timeout ? args->timeout : wait_exec_default_timeout_ms;
1720 timeout = msecs_to_jiffies(timeout);
1721 ret = wait_for_completion_interruptible_timeout(&bo->xfer_done, timeout);
1722 if (!ret) {
1723 ret = -ETIMEDOUT;
1724 goto put_obj;
1725 }
1726 if (ret > 0)
1727 ret = 0;
1728
1729 if (!dbc->usr)
1730 ret = -EPERM;
1731
1732 put_obj:
1733 drm_gem_object_put(obj);
1734 unlock_ch_srcu:
1735 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1736 unlock_dev_srcu:
1737 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1738 unlock_usr_srcu:
1739 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1740 return ret;
1741 }
1742
qaic_perf_stats_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1743 int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1744 {
1745 struct qaic_perf_stats_entry *ent = NULL;
1746 struct qaic_perf_stats *args = data;
1747 int usr_rcu_id, qdev_rcu_id;
1748 struct drm_gem_object *obj;
1749 struct qaic_device *qdev;
1750 struct qaic_user *usr;
1751 struct qaic_bo *bo;
1752 int ret, i;
1753
1754 usr = file_priv->driver_priv;
1755 usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1756 if (!usr->qddev) {
1757 ret = -ENODEV;
1758 goto unlock_usr_srcu;
1759 }
1760
1761 qdev = usr->qddev->qdev;
1762 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1763 if (qdev->dev_state != QAIC_ONLINE) {
1764 ret = -ENODEV;
1765 goto unlock_dev_srcu;
1766 }
1767
1768 if (args->hdr.dbc_id >= qdev->num_dbc) {
1769 ret = -EINVAL;
1770 goto unlock_dev_srcu;
1771 }
1772
1773 ent = kcalloc(args->hdr.count, sizeof(*ent), GFP_KERNEL);
1774 if (!ent) {
1775 ret = -EINVAL;
1776 goto unlock_dev_srcu;
1777 }
1778
1779 ret = copy_from_user(ent, u64_to_user_ptr(args->data), args->hdr.count * sizeof(*ent));
1780 if (ret) {
1781 ret = -EFAULT;
1782 goto free_ent;
1783 }
1784
1785 for (i = 0; i < args->hdr.count; i++) {
1786 obj = drm_gem_object_lookup(file_priv, ent[i].handle);
1787 if (!obj) {
1788 ret = -ENOENT;
1789 goto free_ent;
1790 }
1791 bo = to_qaic_bo(obj);
1792 /*
1793 * perf stats ioctl is called before wait ioctl is complete then
1794 * the latency information is invalid.
1795 */
1796 if (bo->perf_stats.req_processed_ts < bo->perf_stats.req_submit_ts) {
1797 ent[i].device_latency_us = 0;
1798 } else {
1799 ent[i].device_latency_us = div_u64((bo->perf_stats.req_processed_ts -
1800 bo->perf_stats.req_submit_ts), 1000);
1801 }
1802 ent[i].submit_latency_us = div_u64((bo->perf_stats.req_submit_ts -
1803 bo->perf_stats.req_received_ts), 1000);
1804 ent[i].queue_level_before = bo->perf_stats.queue_level_before;
1805 ent[i].num_queue_element = bo->total_slice_nents;
1806 drm_gem_object_put(obj);
1807 }
1808
1809 if (copy_to_user(u64_to_user_ptr(args->data), ent, args->hdr.count * sizeof(*ent)))
1810 ret = -EFAULT;
1811
1812 free_ent:
1813 kfree(ent);
1814 unlock_dev_srcu:
1815 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1816 unlock_usr_srcu:
1817 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1818 return ret;
1819 }
1820
detach_slice_bo(struct qaic_device * qdev,struct qaic_bo * bo)1821 static void detach_slice_bo(struct qaic_device *qdev, struct qaic_bo *bo)
1822 {
1823 qaic_free_slices_bo(bo);
1824 qaic_unprepare_bo(qdev, bo);
1825 qaic_init_bo(bo, true);
1826 list_del(&bo->bo_list);
1827 drm_gem_object_put(&bo->base);
1828 }
1829
qaic_detach_slice_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1830 int qaic_detach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1831 {
1832 struct qaic_detach_slice *args = data;
1833 int rcu_id, usr_rcu_id, qdev_rcu_id;
1834 struct dma_bridge_chan *dbc;
1835 struct drm_gem_object *obj;
1836 struct qaic_device *qdev;
1837 struct qaic_user *usr;
1838 unsigned long flags;
1839 struct qaic_bo *bo;
1840 int ret;
1841
1842 if (args->pad != 0)
1843 return -EINVAL;
1844
1845 usr = file_priv->driver_priv;
1846 usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1847 if (!usr->qddev) {
1848 ret = -ENODEV;
1849 goto unlock_usr_srcu;
1850 }
1851
1852 qdev = usr->qddev->qdev;
1853 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1854 if (qdev->dev_state != QAIC_ONLINE) {
1855 ret = -ENODEV;
1856 goto unlock_dev_srcu;
1857 }
1858
1859 obj = drm_gem_object_lookup(file_priv, args->handle);
1860 if (!obj) {
1861 ret = -ENOENT;
1862 goto unlock_dev_srcu;
1863 }
1864
1865 bo = to_qaic_bo(obj);
1866 ret = mutex_lock_interruptible(&bo->lock);
1867 if (ret)
1868 goto put_bo;
1869
1870 if (!bo->sliced) {
1871 ret = -EINVAL;
1872 goto unlock_bo;
1873 }
1874
1875 dbc = bo->dbc;
1876 rcu_id = srcu_read_lock(&dbc->ch_lock);
1877 if (dbc->usr != usr) {
1878 ret = -EINVAL;
1879 goto unlock_ch_srcu;
1880 }
1881
1882 /* Check if BO is committed to H/W for DMA */
1883 spin_lock_irqsave(&dbc->xfer_lock, flags);
1884 if (bo_queued(bo)) {
1885 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1886 ret = -EBUSY;
1887 goto unlock_ch_srcu;
1888 }
1889 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1890
1891 detach_slice_bo(qdev, bo);
1892
1893 unlock_ch_srcu:
1894 srcu_read_unlock(&dbc->ch_lock, rcu_id);
1895 unlock_bo:
1896 mutex_unlock(&bo->lock);
1897 put_bo:
1898 drm_gem_object_put(obj);
1899 unlock_dev_srcu:
1900 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1901 unlock_usr_srcu:
1902 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1903 return ret;
1904 }
1905
empty_xfer_list(struct qaic_device * qdev,struct dma_bridge_chan * dbc)1906 static void empty_xfer_list(struct qaic_device *qdev, struct dma_bridge_chan *dbc)
1907 {
1908 unsigned long flags;
1909 struct qaic_bo *bo;
1910
1911 spin_lock_irqsave(&dbc->xfer_lock, flags);
1912 while (!list_empty(&dbc->xfer_list)) {
1913 bo = list_first_entry(&dbc->xfer_list, typeof(*bo), xfer_list);
1914 list_del_init(&bo->xfer_list);
1915 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1916 bo->nr_slice_xfer_done = 0;
1917 bo->req_id = 0;
1918 bo->perf_stats.req_received_ts = 0;
1919 bo->perf_stats.req_submit_ts = 0;
1920 bo->perf_stats.req_processed_ts = 0;
1921 bo->perf_stats.queue_level_before = 0;
1922 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1923 complete_all(&bo->xfer_done);
1924 drm_gem_object_put(&bo->base);
1925 spin_lock_irqsave(&dbc->xfer_lock, flags);
1926 }
1927 spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1928 }
1929
disable_dbc(struct qaic_device * qdev,u32 dbc_id,struct qaic_user * usr)1930 int disable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr)
1931 {
1932 if (!qdev->dbc[dbc_id].usr || qdev->dbc[dbc_id].usr->handle != usr->handle)
1933 return -EPERM;
1934
1935 qdev->dbc[dbc_id].usr = NULL;
1936 synchronize_srcu(&qdev->dbc[dbc_id].ch_lock);
1937 return 0;
1938 }
1939
1940 /**
1941 * enable_dbc - Enable the DBC. DBCs are disabled by removing the context of
1942 * user. Add user context back to DBC to enable it. This function trusts the
1943 * DBC ID passed and expects the DBC to be disabled.
1944 * @qdev: Qranium device handle
1945 * @dbc_id: ID of the DBC
1946 * @usr: User context
1947 */
enable_dbc(struct qaic_device * qdev,u32 dbc_id,struct qaic_user * usr)1948 void enable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr)
1949 {
1950 qdev->dbc[dbc_id].usr = usr;
1951 }
1952
wakeup_dbc(struct qaic_device * qdev,u32 dbc_id)1953 void wakeup_dbc(struct qaic_device *qdev, u32 dbc_id)
1954 {
1955 struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1956
1957 dbc->usr = NULL;
1958 empty_xfer_list(qdev, dbc);
1959 synchronize_srcu(&dbc->ch_lock);
1960 /*
1961 * Threads holding channel lock, may add more elements in the xfer_list.
1962 * Flush out these elements from xfer_list.
1963 */
1964 empty_xfer_list(qdev, dbc);
1965 }
1966
release_dbc(struct qaic_device * qdev,u32 dbc_id)1967 void release_dbc(struct qaic_device *qdev, u32 dbc_id)
1968 {
1969 struct qaic_bo *bo, *bo_temp;
1970 struct dma_bridge_chan *dbc;
1971
1972 dbc = &qdev->dbc[dbc_id];
1973 if (!dbc->in_use)
1974 return;
1975
1976 wakeup_dbc(qdev, dbc_id);
1977
1978 dma_free_coherent(&qdev->pdev->dev, dbc->total_size, dbc->req_q_base, dbc->dma_addr);
1979 dbc->total_size = 0;
1980 dbc->req_q_base = NULL;
1981 dbc->dma_addr = 0;
1982 dbc->nelem = 0;
1983 dbc->usr = NULL;
1984
1985 list_for_each_entry_safe(bo, bo_temp, &dbc->bo_lists, bo_list) {
1986 drm_gem_object_get(&bo->base);
1987 mutex_lock(&bo->lock);
1988 detach_slice_bo(qdev, bo);
1989 mutex_unlock(&bo->lock);
1990 drm_gem_object_put(&bo->base);
1991 }
1992
1993 dbc->in_use = false;
1994 wake_up(&dbc->dbc_release);
1995 }
1996
qaic_data_get_fifo_info(struct dma_bridge_chan * dbc,u32 * head,u32 * tail)1997 void qaic_data_get_fifo_info(struct dma_bridge_chan *dbc, u32 *head, u32 *tail)
1998 {
1999 if (!dbc || !head || !tail)
2000 return;
2001
2002 *head = readl(dbc->dbc_base + REQHP_OFF);
2003 *tail = readl(dbc->dbc_base + REQTP_OFF);
2004 }
2005