forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/drivers/base/arm/dma_buf_test_exporter/dma-buf-test-exporter.c
....@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
22 /*
33 *
4
- * (C) COPYRIGHT 2012-2021 ARM Limited. All rights reserved.
4
+ * (C) COPYRIGHT 2012-2022 ARM Limited. All rights reserved.
55 *
66 * This program is free software and is provided to you under the terms of the
77 * GNU General Public License version 2 as published by the Free Software
....@@ -19,7 +19,7 @@
1919 *
2020 */
2121
22
-#include <linux/dma-buf-test-exporter.h>
22
+#include <uapi/base/arm/dma_buf_test_exporter/dma-buf-test-exporter.h>
2323 #include <linux/dma-buf.h>
2424 #include <linux/miscdevice.h>
2525 #include <linux/slab.h>
....@@ -30,10 +30,10 @@
3030 #include <linux/atomic.h>
3131 #include <linux/mm.h>
3232 #include <linux/highmem.h>
33
-#if (KERNEL_VERSION(4, 8, 0) > LINUX_VERSION_CODE)
34
-#include <linux/dma-attrs.h>
35
-#endif
3633 #include <linux/dma-mapping.h>
34
+
35
+#define DMA_BUF_TE_VER_MAJOR 1
36
+#define DMA_BUF_TE_VER_MINOR 0
3737
3838 /* Maximum size allowed in a single DMA_BUF_TE_ALLOC call */
3939 #define DMA_BUF_TE_ALLOC_MAX_SIZE ((8ull << 30) >> PAGE_SHIFT) /* 8 GB */
....@@ -81,6 +81,7 @@
8181 #endif
8282 {
8383 struct dma_buf_te_alloc *alloc;
84
+
8485 alloc = buf->priv;
8586
8687 if (alloc->fail_attach)
....@@ -95,6 +96,12 @@
9596 return 0;
9697 }
9798
99
+/**
100
+ * dma_buf_te_detach - The detach callback function to release &attachment
101
+ *
102
+ * @buf: buffer for the &attachment
103
+ * @attachment: attachment data to be released
104
+ */
98105 static void dma_buf_te_detach(struct dma_buf *buf, struct dma_buf_attachment *attachment)
99106 {
100107 struct dma_buf_te_alloc *alloc = buf->priv;
....@@ -199,24 +206,16 @@
199206 {
200207 size_t i;
201208 struct dma_buf_te_alloc *alloc;
209
+
202210 alloc = buf->priv;
203211 /* no need for locking */
204212
205213 if (alloc->contiguous) {
206
-#if (KERNEL_VERSION(4, 8, 0) <= LINUX_VERSION_CODE)
207214 dma_free_attrs(te_device.this_device,
208215 alloc->nr_pages * PAGE_SIZE,
209216 alloc->contig_cpu_addr,
210217 alloc->contig_dma_addr,
211218 DMA_ATTR_WRITE_COMBINE);
212
-#else
213
- DEFINE_DMA_ATTRS(attrs);
214
-
215
- dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
216
- dma_free_attrs(te_device.this_device,
217
- alloc->nr_pages * PAGE_SIZE,
218
- alloc->contig_cpu_addr, alloc->contig_dma_addr, &attrs);
219
-#endif
220219 } else {
221220 for (i = 0; i < alloc->nr_pages; i++)
222221 __free_page(alloc->pages[i]);
....@@ -240,6 +239,7 @@
240239 list_for_each_entry(attachment, &dmabuf->attachments, node) {
241240 struct dma_buf_te_attachment *pa = attachment->priv;
242241 struct sg_table *sg = pa->sg;
242
+
243243 if (!sg) {
244244 dev_dbg(te_device.this_device, "no mapping for device %s\n", dev_name(attachment->dev));
245245 continue;
....@@ -260,37 +260,23 @@
260260 return 0;
261261 }
262262
263
-#if (KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE)
264263 static int dma_buf_te_begin_cpu_access(struct dma_buf *dmabuf,
265264 enum dma_data_direction direction)
266
-#else
267
-static int dma_buf_te_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
268
- size_t len,
269
- enum dma_data_direction direction)
270
-#endif
271265 {
272266 return dma_buf_te_sync(dmabuf, direction, true);
273267 }
274268
275
-#if (KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE)
276269 static int dma_buf_te_end_cpu_access(struct dma_buf *dmabuf,
277270 enum dma_data_direction direction)
278271 {
279272 return dma_buf_te_sync(dmabuf, direction, false);
280273 }
281
-#else
282
-static void dma_buf_te_end_cpu_access(struct dma_buf *dmabuf, size_t start,
283
- size_t len,
284
- enum dma_data_direction direction)
285
-{
286
- dma_buf_te_sync(dmabuf, direction, false);
287
-}
288
-#endif
289274
290275 static void dma_buf_te_mmap_open(struct vm_area_struct *vma)
291276 {
292277 struct dma_buf *dma_buf;
293278 struct dma_buf_te_alloc *alloc;
279
+
294280 dma_buf = vma->vm_private_data;
295281 alloc = dma_buf->priv;
296282
....@@ -303,6 +289,7 @@
303289 {
304290 struct dma_buf *dma_buf;
305291 struct dma_buf_te_alloc *alloc;
292
+
306293 dma_buf = vma->vm_private_data;
307294 alloc = dma_buf->priv;
308295
....@@ -344,7 +331,7 @@
344331 return 0;
345332 }
346333
347
-struct vm_operations_struct dma_buf_te_vm_ops = {
334
+static const struct vm_operations_struct dma_buf_te_vm_ops = {
348335 .open = dma_buf_te_mmap_open,
349336 .close = dma_buf_te_mmap_close,
350337 .fault = dma_buf_te_mmap_fault
....@@ -353,6 +340,7 @@
353340 static int dma_buf_te_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
354341 {
355342 struct dma_buf_te_alloc *alloc;
343
+
356344 alloc = dmabuf->priv;
357345
358346 if (alloc->fail_mmap)
....@@ -398,7 +386,6 @@
398386 return;
399387
400388 kunmap(alloc->pages[page_num]);
401
- return;
402389 }
403390
404391 static struct dma_buf_ops dma_buf_te_ops = {
....@@ -510,21 +497,11 @@
510497 if (contiguous) {
511498 dma_addr_t dma_aux;
512499
513
-#if (KERNEL_VERSION(4, 8, 0) <= LINUX_VERSION_CODE)
514500 alloc->contig_cpu_addr = dma_alloc_attrs(te_device.this_device,
515501 alloc->nr_pages * PAGE_SIZE,
516502 &alloc->contig_dma_addr,
517503 GFP_KERNEL | __GFP_ZERO,
518504 DMA_ATTR_WRITE_COMBINE);
519
-#else
520
- DEFINE_DMA_ATTRS(attrs);
521
-
522
- dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
523
- alloc->contig_cpu_addr = dma_alloc_attrs(te_device.this_device,
524
- alloc->nr_pages * PAGE_SIZE,
525
- &alloc->contig_dma_addr,
526
- GFP_KERNEL | __GFP_ZERO, &attrs);
527
-#endif
528505 if (!alloc->contig_cpu_addr) {
529506 dev_err(te_device.this_device, "%s: couldn't alloc contiguous buffer %zu pages",
530507 __func__, alloc->nr_pages);
....@@ -580,20 +557,11 @@
580557 /* i still valid */
581558 no_page:
582559 if (contiguous) {
583
-#if (KERNEL_VERSION(4, 8, 0) <= LINUX_VERSION_CODE)
584560 dma_free_attrs(te_device.this_device,
585561 alloc->nr_pages * PAGE_SIZE,
586562 alloc->contig_cpu_addr,
587563 alloc->contig_dma_addr,
588564 DMA_ATTR_WRITE_COMBINE);
589
-#else
590
- DEFINE_DMA_ATTRS(attrs);
591
-
592
- dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
593
- dma_free_attrs(te_device.this_device,
594
- alloc->nr_pages * PAGE_SIZE,
595
- alloc->contig_cpu_addr, alloc->contig_dma_addr, &attrs);
596
-#endif
597565 } else {
598566 while (i-- > 0)
599567 __free_page(alloc->pages[i]);
....@@ -692,7 +660,6 @@
692660 struct sg_table *sgt;
693661 struct scatterlist *sg;
694662 unsigned int count;
695
- unsigned int offset = 0;
696663 int ret = 0;
697664 size_t i;
698665
....@@ -706,11 +673,7 @@
706673 goto no_import;
707674 }
708675
709
- ret = dma_buf_begin_cpu_access(dma_buf,
710
-#if KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE
711
- 0, dma_buf->size,
712
-#endif
713
- DMA_BIDIRECTIONAL);
676
+ ret = dma_buf_begin_cpu_access(dma_buf, DMA_BIDIRECTIONAL);
714677 if (ret)
715678 goto no_cpu_access;
716679
....@@ -733,15 +696,10 @@
733696 dma_buf_kunmap(dma_buf, i >> PAGE_SHIFT, addr);
734697 #endif
735698 }
736
- offset += sg_dma_len(sg);
737699 }
738700
739701 no_kmap:
740
- dma_buf_end_cpu_access(dma_buf,
741
-#if KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE
742
- 0, dma_buf->size,
743
-#endif
744
- DMA_BIDIRECTIONAL);
702
+ dma_buf_end_cpu_access(dma_buf, DMA_BIDIRECTIONAL);
745703 no_cpu_access:
746704 dma_buf_unmap_attachment(attachment, sgt, DMA_BIDIRECTIONAL);
747705 no_import:
....@@ -798,13 +756,14 @@
798756 static int __init dma_buf_te_init(void)
799757 {
800758 int res;
759
+
801760 te_device.minor = MISC_DYNAMIC_MINOR;
802761 te_device.name = "dma_buf_te";
803762 te_device.fops = &dma_buf_te_fops;
804763
805764 res = misc_register(&te_device);
806765 if (res) {
807
- printk(KERN_WARNING"Misc device registration failed of 'dma_buf_te'\n");
766
+ pr_warn("Misc device registration failed of 'dma_buf_te'\n");
808767 return res;
809768 }
810769 te_device.this_device->coherent_dma_mask = DMA_BIT_MASK(32);
....@@ -822,3 +781,4 @@
822781 module_init(dma_buf_te_init);
823782 module_exit(dma_buf_te_exit);
824783 MODULE_LICENSE("GPL");
784
+MODULE_INFO(import_ns, "DMA_BUF");