forked from ikwzm/udmabuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu-dma-buf.c
2640 lines (2417 loc) · 88.2 KB
/
u-dma-buf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************************
*
* Copyright (C) 2015-2023 Ichiro Kawazome
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************************/
#include <linux/cdev.h>
#include <linux/clk.h>
#include <linux/dma-mapping.h>
#include <linux/fs.h>
#include <linux/idr.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/sysctl.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/scatterlist.h>
#include <linux/pagemap.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/version.h>
#include <asm/page.h>
#include <asm/byteorder.h>
/**
* DOC: Udmabuf Constants.
*/
MODULE_DESCRIPTION("User space mappable DMA buffer device driver");
MODULE_AUTHOR("ikwzm");
MODULE_LICENSE("Dual BSD/GPL");
#define DRIVER_VERSION "4.5.3"
#define DRIVER_NAME "u-dma-buf"
#define DEVICE_NAME_FORMAT "udmabuf%d"
#define DEVICE_MAX_NUM 256
#define UDMABUF_DEBUG 1
#define USE_QUIRK_MMAP 1
#define IN_KERNEL_FUNCTIONS 1
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
#include <linux/dma-map-ops.h>
#define IS_DMA_COHERENT(dev) dev_is_dma_coherent(dev)
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0))
#include <linux/dma-noncoherent.h>
#define IS_DMA_COHERENT(dev) dev_is_dma_coherent(dev)
#elif ((LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) && (defined(CONFIG_ARM) || defined(CONFIG_ARM64)))
#define IS_DMA_COHERENT(dev) is_device_dma_coherent(dev)
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
#define USE_DEV_GROUPS 1
#else
#define USE_DEV_GROUPS 0
#endif
#if ((LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) && defined(CONFIG_OF))
#define USE_OF_RESERVED_MEM 1
#else
#define USE_OF_RESERVED_MEM 0
#endif
#if ((LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) && defined(CONFIG_OF))
#define USE_OF_DMA_CONFIG 1
#else
#define USE_OF_DMA_CONFIG 0
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0))
#define USE_DEV_PROPERTY 1
#else
#define USE_DEV_PROPERTY 0
#endif
#if (USE_OF_RESERVED_MEM == 1)
#include <linux/of_reserved_mem.h>
#endif
#ifndef U64_MAX
#define U64_MAX ((u64)~0ULL)
#endif
/**
* DOC: Udmabuf Static Variables.
*
* * udmabuf_sys_class - udmabuf system class
* * init_enable - udmabuf install/uninstall infomation enable
* * dma_mask_bit - udmabuf dma mask bit
* * bind - udmabuf bind device name
* * quirk_mmap_mode - udmabuf default quirk mmap mode
*/
/**
* udmabuf_sys_class - udmabuf system class
*/
static struct class* udmabuf_sys_class = NULL;
/**
* info_enable module parameter
*/
static int info_enable = 1;
module_param( info_enable , int, S_IRUGO);
MODULE_PARM_DESC( info_enable , "udmabuf install/uninstall infomation enable");
#define DMA_INFO_ENABLE (info_enable & 0x02)
#define CONFIG_INFO_ENABLE (info_enable & 0x04)
/**
* dma_mask_bit module parameter
*/
static int dma_mask_bit = 32;
module_param( dma_mask_bit, int, S_IRUGO);
MODULE_PARM_DESC( dma_mask_bit, "udmabuf dma mask bit(default=32)");
/**
* bind module parameter
*/
static char* bind = NULL;
module_param( bind, charp, S_IRUGO);
MODULE_PARM_DESC( bind, "bind device name. exp pci/0000:00:20:0");
#if (USE_QUIRK_MMAP == 1)
/**
* quirk_mmap_mode - udmabuf default quirk mmap mode
*/
#define QUIRK_MMAP_MODE_UNDEFINED 0
#define QUIRK_MMAP_MODE_ALWAYS_OFF 1
#define QUIRK_MMAP_MODE_ALWAYS_ON 2
#define QUIRK_MMAP_MODE_AUTO 3
#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
static int quirk_mmap_mode = QUIRK_MMAP_MODE_ALWAYS_ON;
#else
static int quirk_mmap_mode = QUIRK_MMAP_MODE_AUTO;
#endif
module_param( quirk_mmap_mode, int, S_IRUGO);
MODULE_PARM_DESC( quirk_mmap_mode, "udmabuf default quirk mmap mode(1:off,2:on,3:auto)(default=3)");
#endif /* #if (USE_QUIRK_MMAP == 1) */
/**
* DOC: Udmabuf Object Data Structure.
*
* This section defines the structure of udmabuf device.
*
*/
/**
* struct udmabuf_object - udmabuf object structure.
*/
struct udmabuf_object {
struct device* sys_dev;
struct device* dma_dev;
struct cdev cdev;
dev_t device_number;
struct mutex sem;
bool is_open;
size_t size;
size_t alloc_size;
void* virt_addr;
dma_addr_t phys_addr;
int sync_mode;
u64 sync_offset;
size_t sync_size;
int sync_direction;
bool sync_owner;
u64 sync_for_cpu;
u64 sync_for_device;
#if (USE_QUIRK_MMAP == 1)
int quirk_mmap_mode;
#endif
#if (USE_OF_RESERVED_MEM == 1)
bool of_reserved_mem;
#endif
#if ((UDMABUF_DEBUG == 1) && (USE_QUIRK_MMAP == 1))
bool debug_vma;
#endif
};
#if ((UDMABUF_DEBUG == 1) && (USE_QUIRK_MMAP == 1))
#define UDMABUF_VMA_DEBUG(this) (this->debug_vma)
#else
#define UDMABUF_VMA_DEBUG(this) (0)
#endif
/**
* sync_mode(synchronous mode) value
*/
#define SYNC_MODE_INVALID (0x00)
#define SYNC_MODE_NONCACHED (0x01)
#define SYNC_MODE_WRITECOMBINE (0x02)
#define SYNC_MODE_DMACOHERENT (0x03)
#define SYNC_MODE_MASK (0x03)
#define SYNC_MODE_MIN (0x01)
#define SYNC_MODE_MAX (0x03)
#define SYNC_ALWAYS (0x04)
/**
* DOC: Udmabuf System Class Device File Description.
*
* This section define the device file created in system class when udmabuf is
* loaded into the kernel.
*
* The device file created in system class is as follows.
*
* * /sys/class/u-dma-buf/<device-name>/driver_version
* * /sys/class/u-dma-buf/<device-name>/phys_addr
* * /sys/class/u-dma-buf/<device-name>/size
* * /sys/class/u-dma-buf/<device-name>/sync_mode
* * /sys/class/u-dma-buf/<device-name>/sync_offset
* * /sys/class/u-dma-buf/<device-name>/sync_size
* * /sys/class/u-dma-buf/<device-name>/sync_direction
* * /sys/class/u-dma-buf/<device-name>/sync_owner
* * /sys/class/u-dma-buf/<device-name>/sync_for_cpu
* * /sys/class/u-dma-buf/<device-name>/sync_for_device
* * /sys/class/u-dma-buf/<device-name>/dma_coherent
* * /sys/class/u-dma-buf/<device-name>/quirk_mmap_mode
* *
*/
#define SYNC_COMMAND_DIR_MASK (0x000000000000000C)
#define SYNC_COMMAND_DIR_SHIFT (2)
#define SYNC_COMMAND_SIZE_MASK (0x00000000FFFFFFF0)
#define SYNC_COMMAND_SIZE_SHIFT (0)
#define SYNC_COMMAND_OFFSET_MASK (0xFFFFFFFF00000000)
#define SYNC_COMMAND_OFFSET_SHIFT (32)
#define SYNC_COMMAND_ARGMENT_MASK (0xFFFFFFFFFFFFFFFE)
/**
* udmabuf_sync_command_argments() - get argment for dma_sync_single_for_cpu() or dma_sync_single_for_device()
*
* @this: Pointer to the udmabuf object.
* @command sync command (this->sync_for_cpu or this->sync_for_device)
* @phys_addr Pointer to the phys_addr for dma_sync_single_for_...()
* @size Pointer to the size for dma_sync_single_for_...()
* @direction Pointer to the direction for dma_sync_single_for_...()
* Return: Success(=0) or error status(<0).
*/
static int udmabuf_sync_command_argments(
struct udmabuf_object *this ,
u64 command ,
dma_addr_t *phys_addr,
size_t *size ,
enum dma_data_direction *direction
) {
u64 sync_offset ;
size_t sync_size ;
int sync_direction;
if ((command & SYNC_COMMAND_ARGMENT_MASK) != 0) {
sync_offset = (u64 )((command & SYNC_COMMAND_OFFSET_MASK) >> SYNC_COMMAND_OFFSET_SHIFT);
sync_size = (size_t)((command & SYNC_COMMAND_SIZE_MASK ) >> SYNC_COMMAND_SIZE_SHIFT );
sync_direction = (int )((command & SYNC_COMMAND_DIR_MASK ) >> SYNC_COMMAND_DIR_SHIFT );
} else {
sync_offset = this->sync_offset;
sync_size = this->sync_size;
sync_direction = this->sync_direction;
}
if (sync_offset + sync_size > this->size)
return -EINVAL;
switch(sync_direction) {
case 1 : *direction = DMA_TO_DEVICE ; break;
case 2 : *direction = DMA_FROM_DEVICE ; break;
default: *direction = DMA_BIDIRECTIONAL; break;
}
*phys_addr = this->phys_addr + sync_offset;
*size = sync_size;
return 0;
}
/**
* udmabuf_sync_for_cpu() - call dma_sync_single_for_cpu() when (sync_for_cpu != 0)
* @this: Pointer to the udmabuf object.
* Return: Success(=0) or error status(<0).
*/
static int udmabuf_sync_for_cpu(struct udmabuf_object* this)
{
int status = 0;
if (this->sync_for_cpu) {
dma_addr_t phys_addr;
size_t size;
enum dma_data_direction direction;
status = udmabuf_sync_command_argments(this, this->sync_for_cpu, &phys_addr, &size, &direction);
if (status == 0) {
dma_sync_single_for_cpu(this->dma_dev, phys_addr, size, direction);
this->sync_for_cpu = 0;
this->sync_owner = 0;
}
}
return status;
}
/**
* udmabuf_sync_for_device() - call dma_sync_single_for_device() when (sync_for_device != 0)
* @this: Pointer to the udmabuf object.
* Return: Success(=0) or error status(<0).
*/
static int udmabuf_sync_for_device(struct udmabuf_object* this)
{
int status = 0;
if (this->sync_for_device) {
dma_addr_t phys_addr;
size_t size;
enum dma_data_direction direction;
status = udmabuf_sync_command_argments(this, this->sync_for_device, &phys_addr, &size, &direction);
if (status == 0) {
dma_sync_single_for_device(this->dma_dev, phys_addr, size, direction);
this->sync_for_device = 0;
this->sync_owner = 1;
}
}
return status;
}
#define DEF_ATTR_SHOW(__attr_name, __format, __value) \
static ssize_t udmabuf_show_ ## __attr_name(struct device *dev, struct device_attribute *attr, char *buf) \
{ \
ssize_t status; \
struct udmabuf_object* this = dev_get_drvdata(dev); \
if (mutex_lock_interruptible(&this->sem) != 0) \
return -ERESTARTSYS; \
status = sprintf(buf, __format, (__value)); \
mutex_unlock(&this->sem); \
return status; \
}
static inline int NO_ACTION(struct udmabuf_object* this){return 0;}
#define DEF_ATTR_SET(__attr_name, __min, __max, __pre_action, __post_action) \
static ssize_t udmabuf_set_ ## __attr_name(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) \
{ \
ssize_t status; \
u64 value; \
struct udmabuf_object* this = dev_get_drvdata(dev); \
if (0 != mutex_lock_interruptible(&this->sem)){return -ERESTARTSYS;} \
if (0 != (status = kstrtoull(buf, 0, &value))){ goto failed;} \
if ((value < __min) || (__max < value)) {status = -EINVAL; goto failed;} \
if (0 != (status = __pre_action(this))) { goto failed;} \
this->__attr_name = value; \
if (0 != (status = __post_action(this))) { goto failed;} \
status = size; \
failed: \
mutex_unlock(&this->sem); \
return status; \
}
DEF_ATTR_SHOW(driver_version , "%s\n" , DRIVER_VERSION );
DEF_ATTR_SHOW(size , "%zu\n" , this->size );
DEF_ATTR_SHOW(phys_addr , "%pad\n" , &this->phys_addr );
DEF_ATTR_SHOW(sync_mode , "%d\n" , this->sync_mode );
DEF_ATTR_SET( sync_mode , 0, 7, NO_ACTION, NO_ACTION );
DEF_ATTR_SHOW(sync_offset , "0x%llx\n", this->sync_offset );
DEF_ATTR_SET( sync_offset , 0, U64_MAX, NO_ACTION, NO_ACTION );
DEF_ATTR_SHOW(sync_size , "%zu\n" , this->sync_size );
DEF_ATTR_SET( sync_size , 0, SIZE_MAX, NO_ACTION, NO_ACTION );
DEF_ATTR_SHOW(sync_direction , "%d\n" , this->sync_direction );
DEF_ATTR_SET( sync_direction , 0, 2, NO_ACTION, NO_ACTION );
DEF_ATTR_SHOW(sync_owner , "%d\n" , this->sync_owner );
DEF_ATTR_SHOW(sync_for_cpu , "%llu\n" , this->sync_for_cpu );
DEF_ATTR_SET( sync_for_cpu , 0, U64_MAX, NO_ACTION, udmabuf_sync_for_cpu );
DEF_ATTR_SHOW(sync_for_device, "%llu\n" , this->sync_for_device );
DEF_ATTR_SET( sync_for_device , 0, U64_MAX, NO_ACTION, udmabuf_sync_for_device);
#if (USE_QUIRK_MMAP == 1)
DEF_ATTR_SHOW(quirk_mmap_mode, "%d\n" , this->quirk_mmap_mode );
#endif
#if defined(IS_DMA_COHERENT)
DEF_ATTR_SHOW(dma_coherent , "%d\n" , IS_DMA_COHERENT(this->dma_dev) );
#endif
#if ((UDMABUF_DEBUG == 1) && (USE_QUIRK_MMAP == 1))
DEF_ATTR_SHOW(debug_vma , "%d\n" , this->debug_vma );
DEF_ATTR_SET( debug_vma , 0, 1, NO_ACTION, NO_ACTION );
#endif
static struct device_attribute udmabuf_device_attrs[] = {
__ATTR(driver_version , 0444, udmabuf_show_driver_version , NULL ),
__ATTR(size , 0444, udmabuf_show_size , NULL ),
__ATTR(phys_addr , 0444, udmabuf_show_phys_addr , NULL ),
__ATTR(sync_mode , 0664, udmabuf_show_sync_mode , udmabuf_set_sync_mode ),
__ATTR(sync_offset , 0664, udmabuf_show_sync_offset , udmabuf_set_sync_offset ),
__ATTR(sync_size , 0664, udmabuf_show_sync_size , udmabuf_set_sync_size ),
__ATTR(sync_direction , 0664, udmabuf_show_sync_direction , udmabuf_set_sync_direction ),
__ATTR(sync_owner , 0444, udmabuf_show_sync_owner , NULL ),
__ATTR(sync_for_cpu , 0664, udmabuf_show_sync_for_cpu , udmabuf_set_sync_for_cpu ),
__ATTR(sync_for_device, 0664, udmabuf_show_sync_for_device , udmabuf_set_sync_for_device),
#if (USE_QUIRK_MMAP == 1)
__ATTR(quirk_mmap_mode, 0444, udmabuf_show_quirk_mmap_mode , NULL ),
#endif
#if defined(IS_DMA_COHERENT)
__ATTR(dma_coherent , 0444, udmabuf_show_dma_coherent , NULL ),
#endif
#if ((UDMABUF_DEBUG == 1) && (USE_QUIRK_MMAP == 1))
__ATTR(debug_vma , 0664, udmabuf_show_debug_vma , udmabuf_set_debug_vma ),
#endif
__ATTR_NULL,
};
#if (USE_DEV_GROUPS == 1)
#define udmabuf_device_attrs_size (sizeof(udmabuf_device_attrs)/sizeof(udmabuf_device_attrs[0]))
static struct attribute* udmabuf_attrs[udmabuf_device_attrs_size] = {
NULL
};
static struct attribute_group udmabuf_attr_group = {
.attrs = udmabuf_attrs
};
static const struct attribute_group* udmabuf_attr_groups[] = {
&udmabuf_attr_group,
NULL
};
static inline void udmabuf_sys_class_set_attributes(void)
{
int i;
for (i = 0 ; i < udmabuf_device_attrs_size-1 ; i++) {
udmabuf_attrs[i] = &(udmabuf_device_attrs[i].attr);
}
udmabuf_attrs[i] = NULL;
udmabuf_sys_class->dev_groups = udmabuf_attr_groups;
}
#else
static inline void udmabuf_sys_class_set_attributes(void)
{
udmabuf_sys_class->dev_attrs = udmabuf_device_attrs;
}
#endif
#if (USE_QUIRK_MMAP == 1)
/**
* DOC: Udmabuf Object VM Area Operations for quirk-mmap.
*
* This section defines the operation of vm when mmap-ed the udmabuf object.
*
* * udmabuf_mmap_vma_open() - udmabuf object quirk-mmap vm area open operation.
* * udmabuf_mmap_vma_close() - udmabuf object quirk-mmap vm area close operation.
* * udmabuf_mmap_vma_fault() - udmabuf object quirk-mmap vm area fault operation.
* * udmabuf_mmap_vm_ops - udmabuf object quirk-mmap vm operation table.
* * udmabuf_set_quirk_mmap_mode() - set quirk-mmap in udmabuf object.
* * udmabuf_quirk_mmap_enable() - check if udmabuf object can use quirk-mmap.
*/
/**
* udmabuf_mmap_vma_open() - udmabuf device file mmap vm area open operation.
* @vma: Pointer to the vm area structure.
* Return: None
*/
static void udmabuf_mmap_vma_open(struct vm_area_struct* vma)
{
struct udmabuf_object* this = vma->vm_private_data;
if (UDMABUF_VMA_DEBUG(this))
dev_info(this->dma_dev, "vma_open(virt_addr=0x%lx, offset=0x%lx)\n", vma->vm_start, vma->vm_pgoff<<PAGE_SHIFT);
}
/**
* udmabuf_mmap_vma_close() - udmabuf device file mmap vm area close operation.
* @vma: Pointer to the vm area structure.
* Return: None
*/
static void udmabuf_mmap_vma_close(struct vm_area_struct* vma)
{
struct udmabuf_object* this = vma->vm_private_data;
if (UDMABUF_VMA_DEBUG(this))
dev_info(this->dma_dev, "vma_close()\n");
}
/**
* VM_FAULT_RETURN_TYPE - Type of udmabuf_mmap_vma_fault() return value.
*/
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 17, 0))
typedef vm_fault_t VM_FAULT_RETURN_TYPE;
#else
typedef int VM_FAULT_RETURN_TYPE;
#endif
/**
* _udmabuf_mmap_vma_fault() - udmabuf device file mmap vm area fault operation.
* @vma: Pointer to the vm area structure.
* @vfm: Pointer to the vm fault structure.
* Return: VM_FAULT_RETURN_TYPE (Success(=0) or error status(!=0)).
*/
static inline VM_FAULT_RETURN_TYPE _udmabuf_mmap_vma_fault(struct vm_area_struct* vma, struct vm_fault* vmf)
{
struct udmabuf_object* this = vma->vm_private_data;
unsigned long offset = vmf->pgoff << PAGE_SHIFT;
unsigned long phys_addr = this->phys_addr + offset;
unsigned long page_frame_num = phys_addr >> PAGE_SHIFT;
unsigned long request_size = 1 << PAGE_SHIFT;
unsigned long available_size = this->alloc_size -offset;
unsigned long virt_addr;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0))
virt_addr = vmf->address;
#else
virt_addr = (unsigned long)vmf->virtual_address;
#endif
if (UDMABUF_VMA_DEBUG(this))
dev_info(this->dma_dev,
"vma_fault(virt_addr=%pad, phys_addr=%pad)\n", &virt_addr, &phys_addr
);
if (request_size > available_size)
return VM_FAULT_SIGBUS;
if (!pfn_valid(page_frame_num))
return VM_FAULT_SIGBUS;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0))
return vmf_insert_pfn(vma, virt_addr, page_frame_num);
#else
{
int err = vm_insert_pfn(vma, virt_addr, page_frame_num);
if (err == -ENOMEM)
return VM_FAULT_OOM;
if (err < 0 && err != -EBUSY)
return VM_FAULT_SIGBUS;
return VM_FAULT_NOPAGE;
}
#endif
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
/**
* udmabuf_mmap_vma_fault() - udmabuf device file mmap vm area fault operation.
* @vfm: Pointer to the vm fault structure.
* Return: VM_FAULT_RETURN_TYPE (Success(=0) or error status(!=0)).
*/
static VM_FAULT_RETURN_TYPE udmabuf_mmap_vma_fault(struct vm_fault* vmf)
{
return _udmabuf_mmap_vma_fault(vmf->vma, vmf);
}
#else
/**
* udmabuf_mmap_vma_fault() - udmabuf device file mmap vm area fault operation.
* @vma: Pointer to the vm area structure.
* @vfm: Pointer to the vm fault structure.
* Return: VM_FAULT_RETURN_TYPE (Success(=0) or error status(!=0)).
*/
static VM_FAULT_RETURN_TYPE udmabuf_mmap_vma_fault(struct vm_area_struct* vma, struct vm_fault* vmf)
{
return _udmabuf_mmap_vma_fault(vma, vmf);
}
#endif
/**
* udmabuf device file mmap vm operation table.
*/
static const struct vm_operations_struct udmabuf_mmap_vm_ops = {
.open = udmabuf_mmap_vma_open ,
.close = udmabuf_mmap_vma_close,
.fault = udmabuf_mmap_vma_fault,
};
/**
* udmabuf_set_quirk_mmap_mode() - set quirk-mmap in udmabuf object.
* @this: Pointer to the udmabuf object.
* @value: quirk-mmap mode.
* Return: Success(=0) or error status(<0).
*/
static inline int udmabuf_set_quirk_mmap_mode(struct udmabuf_object* this, int value)
{
if (!this)
return -ENODEV;
if ((value == QUIRK_MMAP_MODE_ALWAYS_OFF) ||
(value == QUIRK_MMAP_MODE_ALWAYS_ON ) ||
(value == QUIRK_MMAP_MODE_AUTO )) {
this->quirk_mmap_mode = value;
return 0;
} else {
return -EINVAL;
}
}
/**
* udmabuf_quirk_mmap_enable() - check if udmabuf object can use quirk-mmap.
* @this: Pointer to the udmabuf object.
* Return: Enable(=True) or Disable(=False).
*/
static bool udmabuf_quirk_mmap_enable(struct udmabuf_object* this)
{
if (!this)
return true;
if (this->quirk_mmap_mode == QUIRK_MMAP_MODE_ALWAYS_OFF)
return false;
if (this->quirk_mmap_mode == QUIRK_MMAP_MODE_ALWAYS_ON )
return true;
#if defined(IS_DMA_COHERENT)
if (this->quirk_mmap_mode == QUIRK_MMAP_MODE_AUTO )
return !IS_DMA_COHERENT(this->dma_dev);
#endif
return true;
}
#endif /* #if (USE_QUIRK_MMAP == 1) */
/**
* DOC: Udmabuf Object Memory Map Operation.
*/
/**
* _PGPROT_NONCACHED - vm_page_prot value when sync_mode is SYNC_MODE_NONCACHED
* _PGPROT_WRITECOMBINE - vm_page_prot value when sync_mode is SYNC_MODE_WRITECOMBINE
* _PGPROT_DMACOHERENT - vm_page_prot value when sync_mode is SYNC_MODE_DMACOHERENT
*/
#if defined(CONFIG_ARM)
#define _PGPROT_NONCACHED(vm_page_prot) pgprot_noncached(vm_page_prot)
#define _PGPROT_WRITECOMBINE(vm_page_prot) pgprot_writecombine(vm_page_prot)
#define _PGPROT_DMACOHERENT(vm_page_prot) pgprot_dmacoherent(vm_page_prot)
#elif defined(CONFIG_ARM64)
#define _PGPROT_NONCACHED(vm_page_prot) pgprot_noncached(vm_page_prot)
#define _PGPROT_WRITECOMBINE(vm_page_prot) pgprot_writecombine(vm_page_prot)
#define _PGPROT_DMACOHERENT(vm_page_prot) pgprot_writecombine(vm_page_prot)
#else
#define _PGPROT_NONCACHED(vm_page_prot) pgprot_noncached(vm_page_prot)
#define _PGPROT_WRITECOMBINE(vm_page_prot) pgprot_writecombine(vm_page_prot)
#define _PGPROT_DMACOHERENT(vm_page_prot) pgprot_writecombine(vm_page_prot)
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0))
static inline void vm_flags_set(struct vm_area_struct* vma, vm_flags_t flags)
{
vma->vm_flags |= flags;
}
#endif
/**
* udmabuf_object_mmap() - udmabuf object memory map operation.
* @this: Pointer to the udmabuf object.
* @vma: Pointer to the vm area structure.
* @force_sync Force sync flag.
* Return: Success(=0) or error status(<0).
*/
static int udmabuf_object_mmap(struct udmabuf_object* this, struct vm_area_struct* vma, bool force_sync)
{
if (vma->vm_pgoff + vma_pages(vma) > (this->alloc_size >> PAGE_SHIFT))
return -EINVAL;
if ((force_sync == true) | (this->sync_mode & SYNC_ALWAYS)) {
switch (this->sync_mode & SYNC_MODE_MASK) {
case SYNC_MODE_NONCACHED :
vm_flags_set(vma, VM_IO);
vma->vm_page_prot = _PGPROT_NONCACHED(vma->vm_page_prot);
break;
case SYNC_MODE_WRITECOMBINE :
vm_flags_set(vma, VM_IO);
vma->vm_page_prot = _PGPROT_WRITECOMBINE(vma->vm_page_prot);
break;
case SYNC_MODE_DMACOHERENT :
vm_flags_set(vma, VM_IO);
vma->vm_page_prot = _PGPROT_DMACOHERENT(vma->vm_page_prot);
break;
default :
break;
}
}
#if (USE_QUIRK_MMAP == 1)
if (udmabuf_quirk_mmap_enable(this))
{
unsigned long page_frame_num = (this->phys_addr >> PAGE_SHIFT) + vma->vm_pgoff;
if (pfn_valid(page_frame_num)) {
vm_flags_set(vma, VM_PFNMAP);
vma->vm_ops = &udmabuf_mmap_vm_ops;
vma->vm_private_data = this;
udmabuf_mmap_vma_open(vma);
return 0;
}
}
#endif
return dma_mmap_coherent(this->dma_dev, vma, this->virt_addr, this->phys_addr, this->alloc_size);
}
/**
* DOC: Udmabuf Device File Operations.
*
* This section defines the operation of the udmabuf device file.
*
* * udmabuf_device_file_open() - udmabuf device file open operation.
* * udmabuf_device_file_release() - udmabuf device file release operation.
* * udmabuf_device_file_mmap() - udmabuf device file memory map operation.
* * udmabuf_device_file_read() - udmabuf device file read operation.
* * udmabuf_device_file_write() - udmabuf device file write operation.
* * udmabuf_device_file_llseek() - udmabuf device file llseek operation.
* * udmabuf_device_file_ops - udmabuf device file operation table.
*/
/**
* udmabuf_device_file_open() - udmabuf device file open operation.
* @inode: Pointer to the inode structure of this device.
* @file: to the file structure.
* Return: Success(=0) or error status(<0).
*/
static int udmabuf_device_file_open(struct inode *inode, struct file *file)
{
struct udmabuf_object* this;
int status = 0;
this = container_of(inode->i_cdev, struct udmabuf_object, cdev);
file->private_data = this;
this->is_open = 1;
return status;
}
/**
* udmabuf_device_file_release() - udmabuf device file release operation.
* @inode: Pointer to the inode structure of this device.
* @file: Pointer to the file structure.
* Return: Success(=0) or error status(<0).
*/
static int udmabuf_device_file_release(struct inode *inode, struct file *file)
{
struct udmabuf_object* this = file->private_data;
this->is_open = 0;
return 0;
}
/**
* udmabuf_device_file_mmap() - udmabuf device file memory map operation.
* @file: Pointer to the file structure.
* @vma: Pointer to the vm area structure.
* Return: Success(=0) or error status(<0).
*/
static int udmabuf_device_file_mmap(struct file *file, struct vm_area_struct* vma)
{
struct udmabuf_object* this = file->private_data;
bool force_sync = ((file->f_flags & O_SYNC) != 0);
return udmabuf_object_mmap(this, vma, force_sync);
}
/**
* udmabuf_device_file_read() - udmabuf device file read operation.
* @file: Pointer to the file structure.
* @buff: Pointer to the user buffer.
* @count: The number of bytes to be read.
* @ppos: Pointer to the offset value.
* Return: Transferd size.
*/
static ssize_t udmabuf_device_file_read(struct file* file, char __user* buff, size_t count, loff_t* ppos)
{
struct udmabuf_object* this = file->private_data;
int result = 0;
size_t xfer_size;
size_t remain_size;
dma_addr_t phys_addr;
void* virt_addr;
if (mutex_lock_interruptible(&this->sem))
return -ERESTARTSYS;
if (*ppos >= this->size) {
result = 0;
goto return_unlock;
}
phys_addr = this->phys_addr + *ppos;
virt_addr = this->virt_addr + *ppos;
xfer_size = (*ppos + count >= this->size) ? this->size - *ppos : count;
if ((file->f_flags & O_SYNC) | (this->sync_mode & SYNC_ALWAYS))
dma_sync_single_for_cpu(this->dma_dev, phys_addr, xfer_size, DMA_FROM_DEVICE);
if ((remain_size = copy_to_user(buff, virt_addr, xfer_size)) != 0) {
result = 0;
goto return_unlock;
}
if ((file->f_flags & O_SYNC) | (this->sync_mode & SYNC_ALWAYS))
dma_sync_single_for_device(this->dma_dev, phys_addr, xfer_size, DMA_FROM_DEVICE);
*ppos += xfer_size;
result = xfer_size;
return_unlock:
mutex_unlock(&this->sem);
return result;
}
/**
* udmabuf_device_file_write() - udmabuf device file write operation.
* @file: Pointer to the file structure.
* @buff: Pointer to the user buffer.
* @count: The number of bytes to be written.
* @ppos: Pointer to the offset value
* Return: Transferd size.
*/
static ssize_t udmabuf_device_file_write(struct file* file, const char __user* buff, size_t count, loff_t* ppos)
{
struct udmabuf_object* this = file->private_data;
int result = 0;
size_t xfer_size;
size_t remain_size;
dma_addr_t phys_addr;
void* virt_addr;
if (mutex_lock_interruptible(&this->sem))
return -ERESTARTSYS;
if (*ppos >= this->size) {
result = 0;
goto return_unlock;
}
phys_addr = this->phys_addr + *ppos;
virt_addr = this->virt_addr + *ppos;
xfer_size = (*ppos + count >= this->size) ? this->size - *ppos : count;
if ((file->f_flags & O_SYNC) | (this->sync_mode & SYNC_ALWAYS))
dma_sync_single_for_cpu(this->dma_dev, phys_addr, xfer_size, DMA_TO_DEVICE);
if ((remain_size = copy_from_user(virt_addr, buff, xfer_size)) != 0) {
result = 0;
goto return_unlock;
}
if ((file->f_flags & O_SYNC) | (this->sync_mode & SYNC_ALWAYS))
dma_sync_single_for_device(this->dma_dev, phys_addr, xfer_size, DMA_TO_DEVICE);
*ppos += xfer_size;
result = xfer_size;
return_unlock:
mutex_unlock(&this->sem);
return result;
}
/**
* udmabuf_device_file_llseek() - udmabuf device file llseek operation.
* @file: Pointer to the file structure.
* @offset: File offset to seek.
* @whence: Type of seek.
* Return: The new position.
*/
static loff_t udmabuf_device_file_llseek(struct file* file, loff_t offset, int whence)
{
struct udmabuf_object* this = file->private_data;
loff_t new_pos;
switch (whence) {
case 0 : /* SEEK_SET */
new_pos = offset;
break;
case 1 : /* SEEK_CUR */
new_pos = file->f_pos + offset;
break;
case 2 : /* SEEK_END */
new_pos = this->size + offset;
break;
default:
return -EINVAL;
}
if (new_pos < 0 ){return -EINVAL;}
if (new_pos > this->size){return -EINVAL;}
file->f_pos = new_pos;
return new_pos;
}
/**
* udmabuf device file operation table.
*/
static const struct file_operations udmabuf_device_file_ops = {
.owner = THIS_MODULE,
.open = udmabuf_device_file_open,
.release = udmabuf_device_file_release,
.mmap = udmabuf_device_file_mmap,
.read = udmabuf_device_file_read,
.write = udmabuf_device_file_write,
.llseek = udmabuf_device_file_llseek,
};
/**
* DOC: Udmabuf Object Operations.
*
* This section defines the operation of udmabuf object.
*
* * udmabuf_device_ida - Udmabuf Object Device Minor Number allocator variable.
* * udmabuf_device_number - Udmabuf Object Device Major Number.
* * udmabuf_object_create() - Create udmabuf object.
* * udmabuf_object_setup() - Setup the udmabuf object.
* * udmabuf_object_info() - Print infomation the udmabuf object.
* * udmabuf_object_destroy() - Destroy the udmabuf object.
*/
static DEFINE_IDA(udmabuf_device_ida);
static dev_t udmabuf_device_number = 0;
/**
* udmabuf_object_create() - Create udmabuf object.
* @name: device name or NULL.
* @parent: parent device or NULL.
* @minor: minor_number or -1 or -2.
* Return: Pointer to the udmabuf object or NULL.
*/
static struct udmabuf_object* udmabuf_object_create(const char* name, struct device* parent, int minor)
{
struct udmabuf_object* this = NULL;
unsigned int done = 0;
const unsigned int DONE_ALLOC_MINOR = (1 << 0);
const unsigned int DONE_CHRDEV_ADD = (1 << 1);
const unsigned int DONE_DEVICE_CREATE = (1 << 3);
const unsigned int DONE_SET_DMA_DEV = (1 << 4);
/*
* allocate device minor number
*/
{
if ((0 <= minor) && (minor < DEVICE_MAX_NUM)) {
if (ida_simple_get(&udmabuf_device_ida, minor, minor+1, GFP_KERNEL) < 0) {
pr_err(DRIVER_NAME ": couldn't allocate minor number(=%d).\n", minor);
goto failed;
}
} else if(minor < 0) {
if ((minor = ida_simple_get(&udmabuf_device_ida, 0, DEVICE_MAX_NUM, GFP_KERNEL)) < 0) {
pr_err(DRIVER_NAME ": couldn't allocate new minor number. return=%d.\n", minor);
goto failed;
}
} else {
pr_err(DRIVER_NAME ": invalid minor number(=%d), valid range is 0 to %d\n", minor, DEVICE_MAX_NUM-1);
goto failed;
}
done |= DONE_ALLOC_MINOR;
}
/*
* create (udmabuf_object*) this.
*/
{
this = kzalloc(sizeof(*this), GFP_KERNEL);
if (IS_ERR_OR_NULL(this)) {
int retval = PTR_ERR(this);
this = NULL;
pr_err(DRIVER_NAME ": kzalloc() failed. return=%d\n", retval);
goto failed;
}
}
/*
* set device_number
*/
{
this->device_number = MKDEV(MAJOR(udmabuf_device_number), minor);
}
/*
* register /sys/class/u-dma-buf/<name>
*/
{
if (name == NULL) {
this->sys_dev = device_create(udmabuf_sys_class,
parent,
this->device_number,
(void *)this,
DEVICE_NAME_FORMAT, MINOR(this->device_number));
} else {
this->sys_dev = device_create(udmabuf_sys_class,
parent,
this->device_number,
(void *)this,
"%s", name);
}
if (IS_ERR_OR_NULL(this->sys_dev)) {
int retval = PTR_ERR(this->sys_dev);
this->sys_dev = NULL;
pr_err(DRIVER_NAME ": device_create() failed. return=%d\n", retval);
goto failed;
}
done |= DONE_DEVICE_CREATE;
}