1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_SWIOTLB_H
3 #define __LINUX_SWIOTLB_H
4 
5 #include <linux/device.h>
6 #include <linux/dma-direction.h>
7 #include <linux/init.h>
8 #include <linux/types.h>
9 #include <linux/limits.h>
10 #include <linux/spinlock.h>
11 
12 struct device;
13 struct page;
14 struct scatterlist;
15 
16 enum swiotlb_force {
17 	SWIOTLB_NORMAL,		/* Default - depending on HW DMA mask etc. */
18 	SWIOTLB_FORCE,		/* swiotlb=force */
19 	SWIOTLB_NO_FORCE,	/* swiotlb=noforce */
20 };
21 
22 /*
23  * Maximum allowable number of contiguous slabs to map,
24  * must be a power of 2.  What is the appropriate value ?
25  * The complexity of {map,unmap}_single is linearly dependent on this value.
26  */
27 #define IO_TLB_SEGSIZE	128
28 
29 /*
30  * log of the size of each IO TLB slab.  The number of slabs is command line
31  * controllable.
32  */
33 #define IO_TLB_SHIFT 11
34 #define IO_TLB_SIZE (1 << IO_TLB_SHIFT)
35 
36 /* default to 64MB */
37 #define IO_TLB_DEFAULT_SIZE (64UL<<20)
38 
39 extern void swiotlb_init(int verbose);
40 int swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose);
41 unsigned long swiotlb_size_or_default(void);
42 extern int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs);
43 extern int swiotlb_late_init_with_default_size(size_t default_size);
44 extern void __init swiotlb_update_mem_attributes(void);
45 
46 phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys,
47 		size_t mapping_size, size_t alloc_size,
48 		enum dma_data_direction dir, unsigned long attrs);
49 
50 extern void swiotlb_tbl_unmap_single(struct device *hwdev,
51 				     phys_addr_t tlb_addr,
52 				     size_t mapping_size,
53 				     enum dma_data_direction dir,
54 				     unsigned long attrs);
55 
56 void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
57 		size_t size, enum dma_data_direction dir);
58 void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
59 		size_t size, enum dma_data_direction dir);
60 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
61 		size_t size, enum dma_data_direction dir, unsigned long attrs);
62 
63 #ifdef CONFIG_SWIOTLB
64 extern enum swiotlb_force swiotlb_force;
65 
66 /**
67  * struct io_tlb_mem - IO TLB Memory Pool Descriptor
68  *
69  * @start:	The start address of the swiotlb memory pool. Used to do a quick
70  *		range check to see if the memory was in fact allocated by this
71  *		API.
72  * @end:	The end address of the swiotlb memory pool. Used to do a quick
73  *		range check to see if the memory was in fact allocated by this
74  *		API.
75  * @nslabs:	The number of IO TLB blocks (in groups of 64) between @start and
76  *		@end. For default swiotlb, this is command line adjustable via
77  *		setup_io_tlb_npages.
78  * @used:	The number of used IO TLB block.
79  * @list:	The free list describing the number of free entries available
80  *		from each index.
81  * @index:	The index to start searching in the next round.
82  * @orig_addr:	The original address corresponding to a mapped entry.
83  * @alloc_size:	Size of the allocated buffer.
84  * @lock:	The lock to protect the above data structures in the map and
85  *		unmap calls.
86  * @debugfs:	The dentry to debugfs.
87  * @late_alloc:	%true if allocated using the page allocator
88  * @force_bounce: %true if swiotlb bouncing is forced
89  * @for_alloc:  %true if the pool is used for memory allocation
90  */
91 struct io_tlb_mem {
92 	phys_addr_t start;
93 	phys_addr_t end;
94 	unsigned long nslabs;
95 	unsigned long used;
96 	unsigned int index;
97 	spinlock_t lock;
98 	struct dentry *debugfs;
99 	bool late_alloc;
100 	bool force_bounce;
101 	bool for_alloc;
102 	struct io_tlb_slot {
103 		phys_addr_t orig_addr;
104 		size_t alloc_size;
105 		unsigned int list;
106 	} *slots;
107 };
108 extern struct io_tlb_mem io_tlb_default_mem;
109 
is_swiotlb_buffer(struct device * dev,phys_addr_t paddr)110 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
111 {
112 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
113 
114 	return mem && paddr >= mem->start && paddr < mem->end;
115 }
116 
is_swiotlb_force_bounce(struct device * dev)117 static inline bool is_swiotlb_force_bounce(struct device *dev)
118 {
119 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
120 
121 	return mem && mem->force_bounce;
122 }
123 
124 void __init swiotlb_exit(void);
125 unsigned int swiotlb_max_segment(void);
126 size_t swiotlb_max_mapping_size(struct device *dev);
127 bool is_swiotlb_active(struct device *dev);
128 void __init swiotlb_adjust_size(unsigned long size);
129 #else
130 #define swiotlb_force SWIOTLB_NO_FORCE
is_swiotlb_buffer(struct device * dev,phys_addr_t paddr)131 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
132 {
133 	return false;
134 }
is_swiotlb_force_bounce(struct device * dev)135 static inline bool is_swiotlb_force_bounce(struct device *dev)
136 {
137 	return false;
138 }
swiotlb_exit(void)139 static inline void swiotlb_exit(void)
140 {
141 }
swiotlb_max_segment(void)142 static inline unsigned int swiotlb_max_segment(void)
143 {
144 	return 0;
145 }
swiotlb_max_mapping_size(struct device * dev)146 static inline size_t swiotlb_max_mapping_size(struct device *dev)
147 {
148 	return SIZE_MAX;
149 }
150 
is_swiotlb_active(struct device * dev)151 static inline bool is_swiotlb_active(struct device *dev)
152 {
153 	return false;
154 }
155 
swiotlb_adjust_size(unsigned long size)156 static inline void swiotlb_adjust_size(unsigned long size)
157 {
158 }
159 #endif /* CONFIG_SWIOTLB */
160 
161 extern void swiotlb_print_info(void);
162 extern void swiotlb_set_max_segment(unsigned int);
163 
164 #ifdef CONFIG_DMA_RESTRICTED_POOL
165 struct page *swiotlb_alloc(struct device *dev, size_t size);
166 bool swiotlb_free(struct device *dev, struct page *page, size_t size);
167 
is_swiotlb_for_alloc(struct device * dev)168 static inline bool is_swiotlb_for_alloc(struct device *dev)
169 {
170 	return dev->dma_io_tlb_mem->for_alloc;
171 }
172 #else
swiotlb_alloc(struct device * dev,size_t size)173 static inline struct page *swiotlb_alloc(struct device *dev, size_t size)
174 {
175 	return NULL;
176 }
swiotlb_free(struct device * dev,struct page * page,size_t size)177 static inline bool swiotlb_free(struct device *dev, struct page *page,
178 				size_t size)
179 {
180 	return false;
181 }
is_swiotlb_for_alloc(struct device * dev)182 static inline bool is_swiotlb_for_alloc(struct device *dev)
183 {
184 	return false;
185 }
186 #endif /* CONFIG_DMA_RESTRICTED_POOL */
187 
188 #endif /* __LINUX_SWIOTLB_H */
189