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 unsigned int alloc_aligned_mask, enum dma_data_direction dir,
49 unsigned long attrs);
50
51 extern void swiotlb_tbl_unmap_single(struct device *hwdev,
52 phys_addr_t tlb_addr,
53 size_t mapping_size,
54 enum dma_data_direction dir,
55 unsigned long attrs);
56
57 void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
58 size_t size, enum dma_data_direction dir);
59 void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
60 size_t size, enum dma_data_direction dir);
61 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
62 size_t size, enum dma_data_direction dir, unsigned long attrs);
63
64 #ifdef CONFIG_SWIOTLB
65 extern enum swiotlb_force swiotlb_force;
66
67 /**
68 * struct io_tlb_mem - IO TLB Memory Pool Descriptor
69 *
70 * @start: The start 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 * @end: The end address of the swiotlb memory pool. Used to do a quick
74 * range check to see if the memory was in fact allocated by this
75 * API.
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 * @index: The index to start searching in the next round.
83 * @orig_addr: The original address corresponding to a mapped entry.
84 * @alloc_size: Size of the allocated buffer.
85 * @lock: The lock to protect the above data structures in the map and
86 * unmap calls.
87 * @debugfs: The dentry to debugfs.
88 * @late_alloc: %true if allocated using the page allocator
89 * @force_bounce: %true if swiotlb bouncing is forced
90 * @for_alloc: %true if the pool is used for memory allocation
91 */
92 struct io_tlb_mem {
93 phys_addr_t start;
94 phys_addr_t end;
95 unsigned long nslabs;
96 unsigned long used;
97 unsigned int index;
98 spinlock_t lock;
99 struct dentry *debugfs;
100 bool late_alloc;
101 bool force_bounce;
102 bool for_alloc;
103 struct io_tlb_slot {
104 phys_addr_t orig_addr;
105 size_t alloc_size;
106 unsigned int list;
107 } *slots;
108 };
109 extern struct io_tlb_mem io_tlb_default_mem;
110
is_swiotlb_buffer(struct device * dev,phys_addr_t paddr)111 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
112 {
113 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
114
115 return mem && paddr >= mem->start && paddr < mem->end;
116 }
117
is_swiotlb_force_bounce(struct device * dev)118 static inline bool is_swiotlb_force_bounce(struct device *dev)
119 {
120 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
121
122 return mem && mem->force_bounce;
123 }
124
125 void __init swiotlb_exit(void);
126 unsigned int swiotlb_max_segment(void);
127 size_t swiotlb_max_mapping_size(struct device *dev);
128 bool is_swiotlb_active(struct device *dev);
129 void __init swiotlb_adjust_size(unsigned long size);
130 #else
131 #define swiotlb_force SWIOTLB_NO_FORCE
is_swiotlb_buffer(struct device * dev,phys_addr_t paddr)132 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
133 {
134 return false;
135 }
is_swiotlb_force_bounce(struct device * dev)136 static inline bool is_swiotlb_force_bounce(struct device *dev)
137 {
138 return false;
139 }
swiotlb_exit(void)140 static inline void swiotlb_exit(void)
141 {
142 }
swiotlb_max_segment(void)143 static inline unsigned int swiotlb_max_segment(void)
144 {
145 return 0;
146 }
swiotlb_max_mapping_size(struct device * dev)147 static inline size_t swiotlb_max_mapping_size(struct device *dev)
148 {
149 return SIZE_MAX;
150 }
151
is_swiotlb_active(struct device * dev)152 static inline bool is_swiotlb_active(struct device *dev)
153 {
154 return false;
155 }
156
swiotlb_adjust_size(unsigned long size)157 static inline void swiotlb_adjust_size(unsigned long size)
158 {
159 }
160 #endif /* CONFIG_SWIOTLB */
161
162 extern void swiotlb_print_info(void);
163 extern void swiotlb_set_max_segment(unsigned int);
164
165 #ifdef CONFIG_DMA_RESTRICTED_POOL
166 struct page *swiotlb_alloc(struct device *dev, size_t size);
167 bool swiotlb_free(struct device *dev, struct page *page, size_t size);
168
is_swiotlb_for_alloc(struct device * dev)169 static inline bool is_swiotlb_for_alloc(struct device *dev)
170 {
171 return dev->dma_io_tlb_mem->for_alloc;
172 }
173 #else
swiotlb_alloc(struct device * dev,size_t size)174 static inline struct page *swiotlb_alloc(struct device *dev, size_t size)
175 {
176 return NULL;
177 }
swiotlb_free(struct device * dev,struct page * page,size_t size)178 static inline bool swiotlb_free(struct device *dev, struct page *page,
179 size_t size)
180 {
181 return false;
182 }
is_swiotlb_for_alloc(struct device * dev)183 static inline bool is_swiotlb_for_alloc(struct device *dev)
184 {
185 return false;
186 }
187 #endif /* CONFIG_DMA_RESTRICTED_POOL */
188
189 #endif /* __LINUX_SWIOTLB_H */
190