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 #define SWIOTLB_VERBOSE	(1 << 0) /* verbose initialization */
17 #define SWIOTLB_FORCE	(1 << 1) /* force bounce buffering */
18 #define SWIOTLB_ANY	(1 << 2) /* allow any memory for the buffer */
19 
20 /*
21  * Maximum allowable number of contiguous slabs to map,
22  * must be a power of 2.  What is the appropriate value ?
23  * The complexity of {map,unmap}_single is linearly dependent on this value.
24  */
25 #define IO_TLB_SEGSIZE	128
26 
27 /*
28  * log of the size of each IO TLB slab.  The number of slabs is command line
29  * controllable.
30  */
31 #define IO_TLB_SHIFT 11
32 #define IO_TLB_SIZE (1 << IO_TLB_SHIFT)
33 
34 /* default to 64MB */
35 #define IO_TLB_DEFAULT_SIZE (64UL<<20)
36 
37 unsigned long swiotlb_size_or_default(void);
38 void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
39 	int (*remap)(void *tlb, unsigned long nslabs));
40 int swiotlb_init_late(size_t size, gfp_t gfp_mask,
41 	int (*remap)(void *tlb, unsigned long nslabs));
42 extern void __init swiotlb_update_mem_attributes(void);
43 
44 phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys,
45 		size_t mapping_size, size_t alloc_size,
46 		unsigned int alloc_aligned_mask, enum dma_data_direction dir,
47 		unsigned long attrs);
48 
49 extern void swiotlb_tbl_unmap_single(struct device *hwdev,
50 				     phys_addr_t tlb_addr,
51 				     size_t mapping_size,
52 				     enum dma_data_direction dir,
53 				     unsigned long attrs);
54 
55 void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
56 		size_t size, enum dma_data_direction dir);
57 void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
58 		size_t size, enum dma_data_direction dir);
59 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
60 		size_t size, enum dma_data_direction dir, unsigned long attrs);
61 
62 #ifdef CONFIG_SWIOTLB
63 
64 /**
65  * struct io_tlb_mem - IO TLB Memory Pool Descriptor
66  *
67  * @start:	The start address of the swiotlb memory pool. Used to do a quick
68  *		range check to see if the memory was in fact allocated by this
69  *		API.
70  * @end:	The end address of the swiotlb memory pool. Used to do a quick
71  *		range check to see if the memory was in fact allocated by this
72  *		API.
73  * @vaddr:	The vaddr of the swiotlb memory pool. The swiotlb memory pool
74  *		may be remapped in the memory encrypted case and store virtual
75  *		address for bounce buffer operation.
76  * @nslabs:	The number of IO TLB blocks (in groups of 64) between @start and
77  *		@end. For default swiotlb, this is command line adjustable via
78  *		setup_io_tlb_npages.
79  * @used:	The number of used IO TLB block.
80  * @list:	The free list describing the number of free entries available
81  *		from each index.
82  * @orig_addr:	The original address corresponding to a mapped entry.
83  * @alloc_size:	Size of the allocated buffer.
84  * @debugfs:	The dentry to debugfs.
85  * @late_alloc:	%true if allocated using the page allocator
86  * @force_bounce: %true if swiotlb bouncing is forced
87  * @for_alloc:  %true if the pool is used for memory allocation
88  * @nareas:  The area number in the pool.
89  * @area_nslabs: The slot number in the area.
90  */
91 struct io_tlb_mem {
92 	phys_addr_t start;
93 	phys_addr_t end;
94 	void *vaddr;
95 	unsigned long nslabs;
96 	unsigned long used;
97 	struct dentry *debugfs;
98 	bool late_alloc;
99 	bool force_bounce;
100 	bool for_alloc;
101 	unsigned int nareas;
102 	unsigned int area_nslabs;
103 	struct io_tlb_area *areas;
104 	struct io_tlb_slot *slots;
105 };
106 extern struct io_tlb_mem io_tlb_default_mem;
107 
is_swiotlb_buffer(struct device * dev,phys_addr_t paddr)108 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
109 {
110 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
111 
112 	return mem && paddr >= mem->start && paddr < mem->end;
113 }
114 
is_swiotlb_force_bounce(struct device * dev)115 static inline bool is_swiotlb_force_bounce(struct device *dev)
116 {
117 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
118 
119 	return mem && mem->force_bounce;
120 }
121 
122 void swiotlb_init(bool addressing_limited, unsigned int flags);
123 void __init swiotlb_exit(void);
124 size_t swiotlb_max_mapping_size(struct device *dev);
125 bool is_swiotlb_active(struct device *dev);
126 void __init swiotlb_adjust_size(unsigned long size);
127 #else
swiotlb_init(bool addressing_limited,unsigned int flags)128 static inline void swiotlb_init(bool addressing_limited, unsigned int flags)
129 {
130 }
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_mapping_size(struct device * dev)142 static inline size_t swiotlb_max_mapping_size(struct device *dev)
143 {
144 	return SIZE_MAX;
145 }
146 
is_swiotlb_active(struct device * dev)147 static inline bool is_swiotlb_active(struct device *dev)
148 {
149 	return false;
150 }
151 
swiotlb_adjust_size(unsigned long size)152 static inline void swiotlb_adjust_size(unsigned long size)
153 {
154 }
155 #endif /* CONFIG_SWIOTLB */
156 
157 extern void swiotlb_print_info(void);
158 
159 #ifdef CONFIG_DMA_RESTRICTED_POOL
160 struct page *swiotlb_alloc(struct device *dev, size_t size);
161 bool swiotlb_free(struct device *dev, struct page *page, size_t size);
162 
is_swiotlb_for_alloc(struct device * dev)163 static inline bool is_swiotlb_for_alloc(struct device *dev)
164 {
165 	return dev->dma_io_tlb_mem->for_alloc;
166 }
167 #else
swiotlb_alloc(struct device * dev,size_t size)168 static inline struct page *swiotlb_alloc(struct device *dev, size_t size)
169 {
170 	return NULL;
171 }
swiotlb_free(struct device * dev,struct page * page,size_t size)172 static inline bool swiotlb_free(struct device *dev, struct page *page,
173 				size_t size)
174 {
175 	return false;
176 }
is_swiotlb_for_alloc(struct device * dev)177 static inline bool is_swiotlb_for_alloc(struct device *dev)
178 {
179 	return false;
180 }
181 #endif /* CONFIG_DMA_RESTRICTED_POOL */
182 
183 extern phys_addr_t swiotlb_unencrypted_base;
184 
185 #endif /* __LINUX_SWIOTLB_H */
186