1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_SWIOTLB_H
3 #define __LINUX_SWIOTLB_H
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 #include <linux/workqueue.h>
17 #define SWIOTLB_VERBOSE (1 << 0) /* verbose initialization */
18 #define SWIOTLB_FORCE (1 << 1) /* force bounce buffering */
19 #define SWIOTLB_ANY (1 << 2) /* allow any memory for the buffer */
22 * Maximum allowable number of contiguous slabs to map,
23 * must be a power of 2. What is the appropriate value ?
24 * The complexity of {map,unmap}_single is linearly dependent on this value.
26 #define IO_TLB_SEGSIZE 128
29 * log of the size of each IO TLB slab. The number of slabs is command line
32 #define IO_TLB_SHIFT 11
33 #define IO_TLB_SIZE (1 << IO_TLB_SHIFT)
36 #define IO_TLB_DEFAULT_SIZE (64UL<<20)
38 unsigned long swiotlb_size_or_default(void);
39 void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
40 int (*remap)(void *tlb, unsigned long nslabs));
41 int swiotlb_init_late(size_t size, gfp_t gfp_mask,
42 int (*remap)(void *tlb, unsigned long nslabs));
43 extern void __init swiotlb_update_mem_attributes(void);
45 phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys,
46 size_t mapping_size, size_t alloc_size,
47 unsigned int alloc_aligned_mask, enum dma_data_direction dir,
50 extern void swiotlb_tbl_unmap_single(struct device *hwdev,
53 enum dma_data_direction dir,
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);
66 * struct io_tlb_pool - IO TLB memory pool descriptor
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
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
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 slots between @start and @end. For the
77 * default swiotlb, this can be adjusted with a boot parameter,
78 * see setup_io_tlb_npages().
79 * @late_alloc: %true if allocated using the page allocator.
80 * @nareas: Number of areas in the pool.
81 * @area_nslabs: Number of slots in each area.
82 * @areas: Array of memory area descriptors.
83 * @slots: Array of slot descriptors.
84 * @node: Member of the IO TLB memory pool list.
85 * @rcu: RCU head for swiotlb_dyn_free().
86 * @transient: %true if transient memory pool.
95 unsigned int area_nslabs;
96 struct io_tlb_area *areas;
97 struct io_tlb_slot *slots;
98 #ifdef CONFIG_SWIOTLB_DYNAMIC
99 struct list_head node;
106 * struct io_tlb_mem - Software IO TLB allocator
107 * @defpool: Default (initial) IO TLB memory pool descriptor.
108 * @pool: IO TLB memory pool descriptor (if not dynamic).
109 * @nslabs: Total number of IO TLB slabs in all pools.
110 * @debugfs: The dentry to debugfs.
111 * @force_bounce: %true if swiotlb bouncing is forced
112 * @for_alloc: %true if the pool is used for memory allocation
113 * @can_grow: %true if more pools can be allocated dynamically.
114 * @phys_limit: Maximum allowed physical address.
115 * @lock: Lock to synchronize changes to the list.
116 * @pools: List of IO TLB memory pool descriptors (if dynamic).
117 * @dyn_alloc: Dynamic IO TLB pool allocation work.
118 * @total_used: The total number of slots in the pool that are currently used
119 * across all areas. Used only for calculating used_hiwater in
121 * @used_hiwater: The high water mark for total_used. Used only for reporting
123 * @transient_nslabs: The total number of slots in all transient pools that
124 * are currently used across all areas.
127 struct io_tlb_pool defpool;
128 unsigned long nslabs;
129 struct dentry *debugfs;
132 #ifdef CONFIG_SWIOTLB_DYNAMIC
136 struct list_head pools;
137 struct work_struct dyn_alloc;
139 #ifdef CONFIG_DEBUG_FS
140 atomic_long_t total_used;
141 atomic_long_t used_hiwater;
142 atomic_long_t transient_nslabs;
146 #ifdef CONFIG_SWIOTLB_DYNAMIC
148 struct io_tlb_pool *swiotlb_find_pool(struct device *dev, phys_addr_t paddr);
152 static inline struct io_tlb_pool *swiotlb_find_pool(struct device *dev,
155 return &dev->dma_io_tlb_mem->defpool;
161 * is_swiotlb_buffer() - check if a physical address belongs to a swiotlb
162 * @dev: Device which has mapped the buffer.
163 * @paddr: Physical address within the DMA buffer.
165 * Check if @paddr points into a bounce buffer.
168 * * %true if @paddr points into a bounce buffer
171 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
173 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
178 #ifdef CONFIG_SWIOTLB_DYNAMIC
180 * All SWIOTLB buffer addresses must have been returned by
181 * swiotlb_tbl_map_single() and passed to a device driver.
182 * If a SWIOTLB address is checked on another CPU, then it was
183 * presumably loaded by the device driver from an unspecified private
184 * data structure. Make sure that this load is ordered before reading
185 * dev->dma_uses_io_tlb here and mem->pools in swiotlb_find_pool().
187 * This barrier pairs with smp_mb() in swiotlb_find_slots().
190 return READ_ONCE(dev->dma_uses_io_tlb) &&
191 swiotlb_find_pool(dev, paddr);
193 return paddr >= mem->defpool.start && paddr < mem->defpool.end;
197 static inline bool is_swiotlb_force_bounce(struct device *dev)
199 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
201 return mem && mem->force_bounce;
204 void swiotlb_init(bool addressing_limited, unsigned int flags);
205 void __init swiotlb_exit(void);
206 void swiotlb_dev_init(struct device *dev);
207 size_t swiotlb_max_mapping_size(struct device *dev);
208 bool is_swiotlb_allocated(void);
209 bool is_swiotlb_active(struct device *dev);
210 void __init swiotlb_adjust_size(unsigned long size);
211 phys_addr_t default_swiotlb_base(void);
212 phys_addr_t default_swiotlb_limit(void);
214 static inline void swiotlb_init(bool addressing_limited, unsigned int flags)
218 static inline void swiotlb_dev_init(struct device *dev)
222 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
226 static inline bool is_swiotlb_force_bounce(struct device *dev)
230 static inline void swiotlb_exit(void)
233 static inline size_t swiotlb_max_mapping_size(struct device *dev)
238 static inline bool is_swiotlb_allocated(void)
243 static inline bool is_swiotlb_active(struct device *dev)
248 static inline void swiotlb_adjust_size(unsigned long size)
252 static inline phys_addr_t default_swiotlb_base(void)
257 static inline phys_addr_t default_swiotlb_limit(void)
261 #endif /* CONFIG_SWIOTLB */
263 extern void swiotlb_print_info(void);
265 #ifdef CONFIG_DMA_RESTRICTED_POOL
266 struct page *swiotlb_alloc(struct device *dev, size_t size);
267 bool swiotlb_free(struct device *dev, struct page *page, size_t size);
269 static inline bool is_swiotlb_for_alloc(struct device *dev)
271 return dev->dma_io_tlb_mem->for_alloc;
274 static inline struct page *swiotlb_alloc(struct device *dev, size_t size)
278 static inline bool swiotlb_free(struct device *dev, struct page *page,
283 static inline bool is_swiotlb_for_alloc(struct device *dev)
287 #endif /* CONFIG_DMA_RESTRICTED_POOL */
289 #endif /* __LINUX_SWIOTLB_H */