xref: /linux-6.15/include/linux/swiotlb.h (revision 463e862a)
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 
16ae7871beSGeert Uytterhoeven enum swiotlb_force {
17ae7871beSGeert Uytterhoeven 	SWIOTLB_NORMAL,		/* Default - depending on HW DMA mask etc. */
18ae7871beSGeert Uytterhoeven 	SWIOTLB_FORCE,		/* swiotlb=force */
19fff5d992SGeert Uytterhoeven 	SWIOTLB_NO_FORCE,	/* swiotlb=noforce */
20ae7871beSGeert Uytterhoeven };
21ae7871beSGeert Uytterhoeven 
220016fdeeSIan Campbell /*
230016fdeeSIan Campbell  * Maximum allowable number of contiguous slabs to map,
240016fdeeSIan Campbell  * must be a power of 2.  What is the appropriate value ?
250016fdeeSIan Campbell  * The complexity of {map,unmap}_single is linearly dependent on this value.
260016fdeeSIan Campbell  */
270016fdeeSIan Campbell #define IO_TLB_SEGSIZE	128
280016fdeeSIan Campbell 
290016fdeeSIan Campbell /*
300016fdeeSIan Campbell  * log of the size of each IO TLB slab.  The number of slabs is command line
310016fdeeSIan Campbell  * controllable.
320016fdeeSIan Campbell  */
330016fdeeSIan Campbell #define IO_TLB_SHIFT 11
34b5d7ccb7SChristoph Hellwig #define IO_TLB_SIZE (1 << IO_TLB_SHIFT)
350016fdeeSIan Campbell 
36e998879dSAshish Kalra /* default to 64MB */
37e998879dSAshish Kalra #define IO_TLB_DEFAULT_SIZE (64UL<<20)
38e998879dSAshish Kalra 
39ad32e8cbSFUJITA Tomonori extern void swiotlb_init(int verbose);
40ac2cbab2SYinghai Lu int swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose);
41c729de8fSYinghai Lu unsigned long swiotlb_size_or_default(void);
4274838b75SKonrad Rzeszutek Wilk extern int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs);
4361b82bbfSAndy Shevchenko extern int swiotlb_late_init_with_default_size(size_t default_size);
44c7753208STom Lendacky extern void __init swiotlb_update_mem_attributes(void);
451648993fSJoerg Roedel 
46fc0021aaSChristoph Hellwig phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys,
47fc0021aaSChristoph Hellwig 		size_t mapping_size, size_t alloc_size,
48fc0021aaSChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs);
49d7ef1533SKonrad Rzeszutek Wilk 
5061ca08c3SAlexander Duyck extern void swiotlb_tbl_unmap_single(struct device *hwdev,
5161ca08c3SAlexander Duyck 				     phys_addr_t tlb_addr,
523fc1ca00SLu Baolu 				     size_t mapping_size,
533fc1ca00SLu Baolu 				     enum dma_data_direction dir,
540443fa00SAlexander Duyck 				     unsigned long attrs);
55d7ef1533SKonrad Rzeszutek Wilk 
5680808d27SChristoph Hellwig void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
5780808d27SChristoph Hellwig 		size_t size, enum dma_data_direction dir);
5880808d27SChristoph Hellwig void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
5980808d27SChristoph Hellwig 		size_t size, enum dma_data_direction dir);
604a47cbaeSChristoph Hellwig dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
614a47cbaeSChristoph Hellwig 		size_t size, enum dma_data_direction dir, unsigned long attrs);
624a47cbaeSChristoph Hellwig 
635740afdbSFUJITA Tomonori #ifdef CONFIG_SWIOTLB
6455897af6SChristoph Hellwig extern enum swiotlb_force swiotlb_force;
6573f62095SClaire Chang 
6673f62095SClaire Chang /**
6773f62095SClaire Chang  * struct io_tlb_mem - IO TLB Memory Pool Descriptor
6873f62095SClaire Chang  *
6973f62095SClaire Chang  * @start:	The start address of the swiotlb memory pool. Used to do a quick
7073f62095SClaire Chang  *		range check to see if the memory was in fact allocated by this
7173f62095SClaire Chang  *		API.
7273f62095SClaire Chang  * @end:	The end address of the swiotlb memory pool. Used to do a quick
7373f62095SClaire Chang  *		range check to see if the memory was in fact allocated by this
7473f62095SClaire Chang  *		API.
7573f62095SClaire Chang  * @nslabs:	The number of IO TLB blocks (in groups of 64) between @start and
760b84e4f8SClaire Chang  *		@end. For default swiotlb, this is command line adjustable via
770b84e4f8SClaire Chang  *		setup_io_tlb_npages.
7873f62095SClaire Chang  * @used:	The number of used IO TLB block.
7973f62095SClaire Chang  * @list:	The free list describing the number of free entries available
8073f62095SClaire Chang  *		from each index.
8173f62095SClaire Chang  * @index:	The index to start searching in the next round.
8273f62095SClaire Chang  * @orig_addr:	The original address corresponding to a mapped entry.
8373f62095SClaire Chang  * @alloc_size:	Size of the allocated buffer.
8473f62095SClaire Chang  * @lock:	The lock to protect the above data structures in the map and
8573f62095SClaire Chang  *		unmap calls.
8673f62095SClaire Chang  * @debugfs:	The dentry to debugfs.
8773f62095SClaire Chang  * @late_alloc:	%true if allocated using the page allocator
88903cd0f3SClaire Chang  * @force_bounce: %true if swiotlb bouncing is forced
89f4111e39SClaire Chang  * @for_alloc:  %true if the pool is used for memory allocation
9073f62095SClaire Chang  */
9173f62095SClaire Chang struct io_tlb_mem {
9273f62095SClaire Chang 	phys_addr_t start;
9373f62095SClaire Chang 	phys_addr_t end;
9473f62095SClaire Chang 	unsigned long nslabs;
9573f62095SClaire Chang 	unsigned long used;
9673f62095SClaire Chang 	unsigned int index;
9773f62095SClaire Chang 	spinlock_t lock;
9873f62095SClaire Chang 	struct dentry *debugfs;
9973f62095SClaire Chang 	bool late_alloc;
100903cd0f3SClaire Chang 	bool force_bounce;
101f4111e39SClaire Chang 	bool for_alloc;
1022d29960aSChristoph Hellwig 	struct io_tlb_slot {
1032d29960aSChristoph Hellwig 		phys_addr_t orig_addr;
1042d29960aSChristoph Hellwig 		size_t alloc_size;
1052d29960aSChristoph Hellwig 		unsigned int list;
106*463e862aSWill Deacon 	} *slots;
10773f62095SClaire Chang };
108*463e862aSWill Deacon extern struct io_tlb_mem io_tlb_default_mem;
10955897af6SChristoph Hellwig 
1107fd856aaSClaire Chang static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
11155897af6SChristoph Hellwig {
1127fd856aaSClaire Chang 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
11373f62095SClaire Chang 
1142d29960aSChristoph Hellwig 	return mem && paddr >= mem->start && paddr < mem->end;
11555897af6SChristoph Hellwig }
11655897af6SChristoph Hellwig 
117903cd0f3SClaire Chang static inline bool is_swiotlb_force_bounce(struct device *dev)
118903cd0f3SClaire Chang {
119903cd0f3SClaire Chang 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
120903cd0f3SClaire Chang 
121903cd0f3SClaire Chang 	return mem && mem->force_bounce;
122903cd0f3SClaire Chang }
123903cd0f3SClaire Chang 
12455897af6SChristoph Hellwig void __init swiotlb_exit(void);
1257453c549SKonrad Rzeszutek Wilk unsigned int swiotlb_max_segment(void);
126abe420bfSJoerg Roedel size_t swiotlb_max_mapping_size(struct device *dev);
1276f2beb26SClaire Chang bool is_swiotlb_active(struct device *dev);
1282d29960aSChristoph Hellwig void __init swiotlb_adjust_size(unsigned long size);
1295740afdbSFUJITA Tomonori #else
13055897af6SChristoph Hellwig #define swiotlb_force SWIOTLB_NO_FORCE
1317fd856aaSClaire Chang static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
13255897af6SChristoph Hellwig {
13355897af6SChristoph Hellwig 	return false;
13455897af6SChristoph Hellwig }
135903cd0f3SClaire Chang static inline bool is_swiotlb_force_bounce(struct device *dev)
136903cd0f3SClaire Chang {
137903cd0f3SClaire Chang 	return false;
138903cd0f3SClaire Chang }
13955897af6SChristoph Hellwig static inline void swiotlb_exit(void)
14055897af6SChristoph Hellwig {
14155897af6SChristoph Hellwig }
14255897af6SChristoph Hellwig static inline unsigned int swiotlb_max_segment(void)
14355897af6SChristoph Hellwig {
14455897af6SChristoph Hellwig 	return 0;
14555897af6SChristoph Hellwig }
146abe420bfSJoerg Roedel static inline size_t swiotlb_max_mapping_size(struct device *dev)
147abe420bfSJoerg Roedel {
148abe420bfSJoerg Roedel 	return SIZE_MAX;
149abe420bfSJoerg Roedel }
150492366f7SJoerg Roedel 
1516f2beb26SClaire Chang static inline bool is_swiotlb_active(struct device *dev)
152492366f7SJoerg Roedel {
153492366f7SJoerg Roedel 	return false;
154492366f7SJoerg Roedel }
155e998879dSAshish Kalra 
1562d29960aSChristoph Hellwig static inline void swiotlb_adjust_size(unsigned long size)
157e998879dSAshish Kalra {
158e998879dSAshish Kalra }
15955897af6SChristoph Hellwig #endif /* CONFIG_SWIOTLB */
1605740afdbSFUJITA Tomonori 
161ad32e8cbSFUJITA Tomonori extern void swiotlb_print_info(void);
1627453c549SKonrad Rzeszutek Wilk extern void swiotlb_set_max_segment(unsigned int);
1639c5a3621SAkinobu Mita 
164f4111e39SClaire Chang #ifdef CONFIG_DMA_RESTRICTED_POOL
165f4111e39SClaire Chang struct page *swiotlb_alloc(struct device *dev, size_t size);
166f4111e39SClaire Chang bool swiotlb_free(struct device *dev, struct page *page, size_t size);
167f4111e39SClaire Chang 
168f4111e39SClaire Chang static inline bool is_swiotlb_for_alloc(struct device *dev)
169f4111e39SClaire Chang {
170f4111e39SClaire Chang 	return dev->dma_io_tlb_mem->for_alloc;
171f4111e39SClaire Chang }
172f4111e39SClaire Chang #else
173f4111e39SClaire Chang static inline struct page *swiotlb_alloc(struct device *dev, size_t size)
174f4111e39SClaire Chang {
175f4111e39SClaire Chang 	return NULL;
176f4111e39SClaire Chang }
177f4111e39SClaire Chang static inline bool swiotlb_free(struct device *dev, struct page *page,
178f4111e39SClaire Chang 				size_t size)
179f4111e39SClaire Chang {
180f4111e39SClaire Chang 	return false;
181f4111e39SClaire Chang }
182f4111e39SClaire Chang static inline bool is_swiotlb_for_alloc(struct device *dev)
183f4111e39SClaire Chang {
184f4111e39SClaire Chang 	return false;
185f4111e39SClaire Chang }
186f4111e39SClaire Chang #endif /* CONFIG_DMA_RESTRICTED_POOL */
187f4111e39SClaire Chang 
1881648993fSJoerg Roedel #endif /* __LINUX_SWIOTLB_H */
189