1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21648993fSJoerg Roedel #ifndef __LINUX_SWIOTLB_H 31648993fSJoerg Roedel #define __LINUX_SWIOTLB_H 41648993fSJoerg Roedel 57fd856aaSClaire Chang #include <linux/device.h> 638674442SThierry Reding #include <linux/dma-direction.h> 738674442SThierry Reding #include <linux/init.h> 81648993fSJoerg Roedel #include <linux/types.h> 9f51778dbSStephen Rothwell #include <linux/limits.h> 1073f62095SClaire Chang #include <linux/spinlock.h> 111648993fSJoerg Roedel 121648993fSJoerg Roedel struct device; 1338674442SThierry Reding struct page; 141648993fSJoerg Roedel struct scatterlist; 151648993fSJoerg Roedel 16c6af2aa9SChristoph Hellwig #define SWIOTLB_VERBOSE (1 << 0) /* verbose initialization */ 17c6af2aa9SChristoph Hellwig #define SWIOTLB_FORCE (1 << 1) /* force bounce buffering */ 188ba2ed1bSChristoph Hellwig #define SWIOTLB_ANY (1 << 2) /* allow any memory for the buffer */ 19ae7871beSGeert Uytterhoeven 200016fdeeSIan Campbell /* 210016fdeeSIan Campbell * Maximum allowable number of contiguous slabs to map, 220016fdeeSIan Campbell * must be a power of 2. What is the appropriate value ? 230016fdeeSIan Campbell * The complexity of {map,unmap}_single is linearly dependent on this value. 240016fdeeSIan Campbell */ 250016fdeeSIan Campbell #define IO_TLB_SEGSIZE 128 260016fdeeSIan Campbell 270016fdeeSIan Campbell /* 280016fdeeSIan Campbell * log of the size of each IO TLB slab. The number of slabs is command line 290016fdeeSIan Campbell * controllable. 300016fdeeSIan Campbell */ 310016fdeeSIan Campbell #define IO_TLB_SHIFT 11 32b5d7ccb7SChristoph Hellwig #define IO_TLB_SIZE (1 << IO_TLB_SHIFT) 330016fdeeSIan Campbell 34e998879dSAshish Kalra /* default to 64MB */ 35e998879dSAshish Kalra #define IO_TLB_DEFAULT_SIZE (64UL<<20) 36e998879dSAshish Kalra 37c729de8fSYinghai Lu unsigned long swiotlb_size_or_default(void); 387374153dSChristoph Hellwig void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, 397374153dSChristoph Hellwig int (*remap)(void *tlb, unsigned long nslabs)); 407374153dSChristoph Hellwig int swiotlb_init_late(size_t size, gfp_t gfp_mask, 417374153dSChristoph Hellwig int (*remap)(void *tlb, unsigned long nslabs)); 42c7753208STom Lendacky extern void __init swiotlb_update_mem_attributes(void); 431648993fSJoerg Roedel 44fc0021aaSChristoph Hellwig phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys, 45fc0021aaSChristoph Hellwig size_t mapping_size, size_t alloc_size, 46e81e99baSDavid Stevens unsigned int alloc_aligned_mask, enum dma_data_direction dir, 47e81e99baSDavid Stevens unsigned long attrs); 48d7ef1533SKonrad Rzeszutek Wilk 4961ca08c3SAlexander Duyck extern void swiotlb_tbl_unmap_single(struct device *hwdev, 5061ca08c3SAlexander Duyck phys_addr_t tlb_addr, 513fc1ca00SLu Baolu size_t mapping_size, 523fc1ca00SLu Baolu enum dma_data_direction dir, 530443fa00SAlexander Duyck unsigned long attrs); 54d7ef1533SKonrad Rzeszutek Wilk 5580808d27SChristoph Hellwig void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr, 5680808d27SChristoph Hellwig size_t size, enum dma_data_direction dir); 5780808d27SChristoph Hellwig void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr, 5880808d27SChristoph Hellwig size_t size, enum dma_data_direction dir); 594a47cbaeSChristoph Hellwig dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys, 604a47cbaeSChristoph Hellwig size_t size, enum dma_data_direction dir, unsigned long attrs); 614a47cbaeSChristoph Hellwig 625740afdbSFUJITA Tomonori #ifdef CONFIG_SWIOTLB 6373f62095SClaire Chang 6473f62095SClaire Chang /** 6573f62095SClaire Chang * struct io_tlb_mem - IO TLB Memory Pool Descriptor 6673f62095SClaire Chang * 6773f62095SClaire Chang * @start: The start address of the swiotlb memory pool. Used to do a quick 6873f62095SClaire Chang * range check to see if the memory was in fact allocated by this 6973f62095SClaire Chang * API. 7073f62095SClaire Chang * @end: The end address of the swiotlb memory pool. Used to do a quick 7173f62095SClaire Chang * range check to see if the memory was in fact allocated by this 7273f62095SClaire Chang * API. 731a5e91d8STianyu Lan * @vaddr: The vaddr of the swiotlb memory pool. The swiotlb memory pool 741a5e91d8STianyu Lan * may be remapped in the memory encrypted case and store virtual 751a5e91d8STianyu Lan * address for bounce buffer operation. 7673f62095SClaire Chang * @nslabs: The number of IO TLB blocks (in groups of 64) between @start and 770b84e4f8SClaire Chang * @end. For default swiotlb, this is command line adjustable via 780b84e4f8SClaire Chang * setup_io_tlb_npages. 7973f62095SClaire Chang * @used: The number of used IO TLB block. 8073f62095SClaire Chang * @list: The free list describing the number of free entries available 8173f62095SClaire Chang * from each index. 8273f62095SClaire Chang * @index: The index to start searching in the next round. 8373f62095SClaire Chang * @orig_addr: The original address corresponding to a mapped entry. 8473f62095SClaire Chang * @alloc_size: Size of the allocated buffer. 8573f62095SClaire Chang * @lock: The lock to protect the above data structures in the map and 8673f62095SClaire Chang * unmap calls. 8773f62095SClaire Chang * @debugfs: The dentry to debugfs. 8873f62095SClaire Chang * @late_alloc: %true if allocated using the page allocator 89903cd0f3SClaire Chang * @force_bounce: %true if swiotlb bouncing is forced 90f4111e39SClaire Chang * @for_alloc: %true if the pool is used for memory allocation 91*20347fcaSTianyu Lan * @nareas: The area number in the pool. 92*20347fcaSTianyu Lan * @area_nslabs: The slot number in the area. 9373f62095SClaire Chang */ 9473f62095SClaire Chang struct io_tlb_mem { 9573f62095SClaire Chang phys_addr_t start; 9673f62095SClaire Chang phys_addr_t end; 971a5e91d8STianyu Lan void *vaddr; 9873f62095SClaire Chang unsigned long nslabs; 9973f62095SClaire Chang unsigned long used; 10073f62095SClaire Chang unsigned int index; 10173f62095SClaire Chang spinlock_t lock; 10273f62095SClaire Chang struct dentry *debugfs; 10373f62095SClaire Chang bool late_alloc; 104903cd0f3SClaire Chang bool force_bounce; 105f4111e39SClaire Chang bool for_alloc; 106*20347fcaSTianyu Lan unsigned int nareas; 107*20347fcaSTianyu Lan unsigned int area_nslabs; 108*20347fcaSTianyu Lan struct io_tlb_area *areas; 1092d29960aSChristoph Hellwig struct io_tlb_slot { 1102d29960aSChristoph Hellwig phys_addr_t orig_addr; 1112d29960aSChristoph Hellwig size_t alloc_size; 1122d29960aSChristoph Hellwig unsigned int list; 113463e862aSWill Deacon } *slots; 11473f62095SClaire Chang }; 115463e862aSWill Deacon extern struct io_tlb_mem io_tlb_default_mem; 11655897af6SChristoph Hellwig 1177fd856aaSClaire Chang static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) 11855897af6SChristoph Hellwig { 1197fd856aaSClaire Chang struct io_tlb_mem *mem = dev->dma_io_tlb_mem; 12073f62095SClaire Chang 1212d29960aSChristoph Hellwig return mem && paddr >= mem->start && paddr < mem->end; 12255897af6SChristoph Hellwig } 12355897af6SChristoph Hellwig 124903cd0f3SClaire Chang static inline bool is_swiotlb_force_bounce(struct device *dev) 125903cd0f3SClaire Chang { 126903cd0f3SClaire Chang struct io_tlb_mem *mem = dev->dma_io_tlb_mem; 127903cd0f3SClaire Chang 128903cd0f3SClaire Chang return mem && mem->force_bounce; 129903cd0f3SClaire Chang } 130903cd0f3SClaire Chang 131c6af2aa9SChristoph Hellwig void swiotlb_init(bool addressing_limited, unsigned int flags); 13255897af6SChristoph Hellwig void __init swiotlb_exit(void); 1337453c549SKonrad Rzeszutek Wilk unsigned int swiotlb_max_segment(void); 134abe420bfSJoerg Roedel size_t swiotlb_max_mapping_size(struct device *dev); 1356f2beb26SClaire Chang bool is_swiotlb_active(struct device *dev); 1362d29960aSChristoph Hellwig void __init swiotlb_adjust_size(unsigned long size); 1375740afdbSFUJITA Tomonori #else 138c6af2aa9SChristoph Hellwig static inline void swiotlb_init(bool addressing_limited, unsigned int flags) 139c6af2aa9SChristoph Hellwig { 140c6af2aa9SChristoph Hellwig } 1417fd856aaSClaire Chang static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) 14255897af6SChristoph Hellwig { 14355897af6SChristoph Hellwig return false; 14455897af6SChristoph Hellwig } 145903cd0f3SClaire Chang static inline bool is_swiotlb_force_bounce(struct device *dev) 146903cd0f3SClaire Chang { 147903cd0f3SClaire Chang return false; 148903cd0f3SClaire Chang } 14955897af6SChristoph Hellwig static inline void swiotlb_exit(void) 15055897af6SChristoph Hellwig { 15155897af6SChristoph Hellwig } 15255897af6SChristoph Hellwig static inline unsigned int swiotlb_max_segment(void) 15355897af6SChristoph Hellwig { 15455897af6SChristoph Hellwig return 0; 15555897af6SChristoph Hellwig } 156abe420bfSJoerg Roedel static inline size_t swiotlb_max_mapping_size(struct device *dev) 157abe420bfSJoerg Roedel { 158abe420bfSJoerg Roedel return SIZE_MAX; 159abe420bfSJoerg Roedel } 160492366f7SJoerg Roedel 1616f2beb26SClaire Chang static inline bool is_swiotlb_active(struct device *dev) 162492366f7SJoerg Roedel { 163492366f7SJoerg Roedel return false; 164492366f7SJoerg Roedel } 165e998879dSAshish Kalra 1662d29960aSChristoph Hellwig static inline void swiotlb_adjust_size(unsigned long size) 167e998879dSAshish Kalra { 168e998879dSAshish Kalra } 16955897af6SChristoph Hellwig #endif /* CONFIG_SWIOTLB */ 1705740afdbSFUJITA Tomonori 171ad32e8cbSFUJITA Tomonori extern void swiotlb_print_info(void); 1729c5a3621SAkinobu Mita 173f4111e39SClaire Chang #ifdef CONFIG_DMA_RESTRICTED_POOL 174f4111e39SClaire Chang struct page *swiotlb_alloc(struct device *dev, size_t size); 175f4111e39SClaire Chang bool swiotlb_free(struct device *dev, struct page *page, size_t size); 176f4111e39SClaire Chang 177f4111e39SClaire Chang static inline bool is_swiotlb_for_alloc(struct device *dev) 178f4111e39SClaire Chang { 179f4111e39SClaire Chang return dev->dma_io_tlb_mem->for_alloc; 180f4111e39SClaire Chang } 181f4111e39SClaire Chang #else 182f4111e39SClaire Chang static inline struct page *swiotlb_alloc(struct device *dev, size_t size) 183f4111e39SClaire Chang { 184f4111e39SClaire Chang return NULL; 185f4111e39SClaire Chang } 186f4111e39SClaire Chang static inline bool swiotlb_free(struct device *dev, struct page *page, 187f4111e39SClaire Chang size_t size) 188f4111e39SClaire Chang { 189f4111e39SClaire Chang return false; 190f4111e39SClaire Chang } 191f4111e39SClaire Chang static inline bool is_swiotlb_for_alloc(struct device *dev) 192f4111e39SClaire Chang { 193f4111e39SClaire Chang return false; 194f4111e39SClaire Chang } 195f4111e39SClaire Chang #endif /* CONFIG_DMA_RESTRICTED_POOL */ 196f4111e39SClaire Chang 1971a5e91d8STianyu Lan extern phys_addr_t swiotlb_unencrypted_base; 1981a5e91d8STianyu Lan 1991648993fSJoerg Roedel #endif /* __LINUX_SWIOTLB_H */ 200