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 * @vaddr: The vaddr of the swiotlb memory pool. The swiotlb memory pool 77 * may be remapped in the memory encrypted case and store virtual 78 * address for bounce buffer operation. 79 * @nslabs: The number of IO TLB blocks (in groups of 64) between @start and 80 * @end. For default swiotlb, this is command line adjustable via 81 * setup_io_tlb_npages. 82 * @used: The number of used IO TLB block. 83 * @list: The free list describing the number of free entries available 84 * from each index. 85 * @index: The index to start searching in the next round. 86 * @orig_addr: The original address corresponding to a mapped entry. 87 * @alloc_size: Size of the allocated buffer. 88 * @lock: The lock to protect the above data structures in the map and 89 * unmap calls. 90 * @debugfs: The dentry to debugfs. 91 * @late_alloc: %true if allocated using the page allocator 92 * @force_bounce: %true if swiotlb bouncing is forced 93 * @for_alloc: %true if the pool is used for memory allocation 94 */ 95 struct io_tlb_mem { 96 phys_addr_t start; 97 phys_addr_t end; 98 void *vaddr; 99 unsigned long nslabs; 100 unsigned long used; 101 unsigned int index; 102 spinlock_t lock; 103 struct dentry *debugfs; 104 bool late_alloc; 105 bool force_bounce; 106 bool for_alloc; 107 struct io_tlb_slot { 108 phys_addr_t orig_addr; 109 size_t alloc_size; 110 unsigned int list; 111 } *slots; 112 }; 113 extern struct io_tlb_mem io_tlb_default_mem; 114 115 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) 116 { 117 struct io_tlb_mem *mem = dev->dma_io_tlb_mem; 118 119 return mem && paddr >= mem->start && paddr < mem->end; 120 } 121 122 static inline bool is_swiotlb_force_bounce(struct device *dev) 123 { 124 struct io_tlb_mem *mem = dev->dma_io_tlb_mem; 125 126 return mem && mem->force_bounce; 127 } 128 129 void __init swiotlb_exit(void); 130 unsigned int swiotlb_max_segment(void); 131 size_t swiotlb_max_mapping_size(struct device *dev); 132 bool is_swiotlb_active(struct device *dev); 133 void __init swiotlb_adjust_size(unsigned long size); 134 #else 135 #define swiotlb_force SWIOTLB_NO_FORCE 136 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) 137 { 138 return false; 139 } 140 static inline bool is_swiotlb_force_bounce(struct device *dev) 141 { 142 return false; 143 } 144 static inline void swiotlb_exit(void) 145 { 146 } 147 static inline unsigned int swiotlb_max_segment(void) 148 { 149 return 0; 150 } 151 static inline size_t swiotlb_max_mapping_size(struct device *dev) 152 { 153 return SIZE_MAX; 154 } 155 156 static inline bool is_swiotlb_active(struct device *dev) 157 { 158 return false; 159 } 160 161 static inline void swiotlb_adjust_size(unsigned long size) 162 { 163 } 164 #endif /* CONFIG_SWIOTLB */ 165 166 extern void swiotlb_print_info(void); 167 extern void swiotlb_set_max_segment(unsigned int); 168 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); 172 173 static inline bool is_swiotlb_for_alloc(struct device *dev) 174 { 175 return dev->dma_io_tlb_mem->for_alloc; 176 } 177 #else 178 static inline struct page *swiotlb_alloc(struct device *dev, size_t size) 179 { 180 return NULL; 181 } 182 static inline bool swiotlb_free(struct device *dev, struct page *page, 183 size_t size) 184 { 185 return false; 186 } 187 static inline bool is_swiotlb_for_alloc(struct device *dev) 188 { 189 return false; 190 } 191 #endif /* CONFIG_DMA_RESTRICTED_POOL */ 192 193 extern phys_addr_t swiotlb_unencrypted_base; 194 195 #endif /* __LINUX_SWIOTLB_H */ 196