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>
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 */
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.
25 #define IO_TLB_SEGSIZE 128
28 * log of the size of each IO TLB slab. The number of slabs is command line
31 #define IO_TLB_SHIFT 11
32 #define IO_TLB_SIZE (1 << IO_TLB_SHIFT)
35 #define IO_TLB_DEFAULT_SIZE (64UL<<20)
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);
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,
49 extern void swiotlb_tbl_unmap_single(struct device *hwdev,
52 enum dma_data_direction dir,
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);
63 extern enum swiotlb_force swiotlb_force;
66 * struct io_tlb_mem - IO TLB Memory Pool Descriptor
68 * @start: The start address of the swiotlb memory pool. Used to do a quick
69 * range check to see if the memory was in fact allocated by this
71 * @end: The end address of the swiotlb memory pool. Used to do a quick
72 * range check to see if the memory was in fact allocated by this
74 * @vaddr: The vaddr of the swiotlb memory pool. The swiotlb memory pool
75 * may be remapped in the memory encrypted case and store virtual
76 * address for bounce buffer operation.
77 * @nslabs: The number of IO TLB blocks (in groups of 64) between @start and
78 * @end. For default swiotlb, this is command line adjustable via
79 * setup_io_tlb_npages.
80 * @used: The number of used IO TLB block.
81 * @list: The free list describing the number of free entries available
83 * @index: The index to start searching in the next round.
84 * @orig_addr: The original address corresponding to a mapped entry.
85 * @alloc_size: Size of the allocated buffer.
86 * @lock: The lock to protect the above data structures in the map and
88 * @debugfs: The dentry to debugfs.
89 * @late_alloc: %true if allocated using the page allocator
90 * @force_bounce: %true if swiotlb bouncing is forced
91 * @for_alloc: %true if the pool is used for memory allocation
101 struct dentry *debugfs;
106 phys_addr_t orig_addr;
111 extern struct io_tlb_mem io_tlb_default_mem;
113 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
115 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
117 return mem && paddr >= mem->start && paddr < mem->end;
120 static inline bool is_swiotlb_force_bounce(struct device *dev)
122 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
124 return mem && mem->force_bounce;
127 void swiotlb_init(bool addressing_limited, unsigned int flags);
128 void __init swiotlb_exit(void);
129 unsigned int swiotlb_max_segment(void);
130 size_t swiotlb_max_mapping_size(struct device *dev);
131 bool is_swiotlb_active(struct device *dev);
132 void __init swiotlb_adjust_size(unsigned long size);
134 static inline void swiotlb_init(bool addressing_limited, unsigned int flags)
137 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
141 static inline bool is_swiotlb_force_bounce(struct device *dev)
145 static inline void swiotlb_exit(void)
148 static inline unsigned int swiotlb_max_segment(void)
152 static inline size_t swiotlb_max_mapping_size(struct device *dev)
157 static inline bool is_swiotlb_active(struct device *dev)
162 static inline void swiotlb_adjust_size(unsigned long size)
165 #endif /* CONFIG_SWIOTLB */
167 extern void swiotlb_print_info(void);
169 #ifdef CONFIG_DMA_RESTRICTED_POOL
170 struct page *swiotlb_alloc(struct device *dev, size_t size);
171 bool swiotlb_free(struct device *dev, struct page *page, size_t size);
173 static inline bool is_swiotlb_for_alloc(struct device *dev)
175 return dev->dma_io_tlb_mem->for_alloc;
178 static inline struct page *swiotlb_alloc(struct device *dev, size_t size)
182 static inline bool swiotlb_free(struct device *dev, struct page *page,
187 static inline bool is_swiotlb_for_alloc(struct device *dev)
191 #endif /* CONFIG_DMA_RESTRICTED_POOL */
193 extern phys_addr_t swiotlb_unencrypted_base;
195 #endif /* __LINUX_SWIOTLB_H */