swiotlb.h 5.5 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4
#ifndef __LINUX_SWIOTLB_H
#define __LINUX_SWIOTLB_H

5
#include <linux/device.h>
6 7
#include <linux/dma-direction.h>
#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 15
struct scatterlist;

16 17 18
enum swiotlb_force {
	SWIOTLB_NORMAL,		/* Default - depending on HW DMA mask etc. */
	SWIOTLB_FORCE,		/* swiotlb=force */
19
	SWIOTLB_NO_FORCE,	/* swiotlb=noforce */
20 21
};

22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Maximum allowable number of contiguous slabs to map,
 * must be a power of 2.  What is the appropriate value ?
 * The complexity of {map,unmap}_single is linearly dependent on this value.
 */
#define IO_TLB_SEGSIZE	128

/*
 * log of the size of each IO TLB slab.  The number of slabs is command line
 * controllable.
 */
#define IO_TLB_SHIFT 11
34
#define IO_TLB_SIZE (1 << IO_TLB_SHIFT)
35

36 37 38
/* default to 64MB */
#define IO_TLB_DEFAULT_SIZE (64UL<<20)

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 47
phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys,
		size_t mapping_size, size_t alloc_size,
48 49
		unsigned int alloc_aligned_mask, enum dma_data_direction dir,
		unsigned long attrs);
50

51 52
extern void swiotlb_tbl_unmap_single(struct device *hwdev,
				     phys_addr_t tlb_addr,
53 54
				     size_t mapping_size,
				     enum dma_data_direction dir,
55
				     unsigned long attrs);
56

57 58 59 60
void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
		size_t size, enum dma_data_direction dir);
void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
		size_t size, enum dma_data_direction dir);
61 62 63
dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
		size_t size, enum dma_data_direction dir, unsigned long attrs);

64
#ifdef CONFIG_SWIOTLB
65
extern enum swiotlb_force swiotlb_force;
66 67 68 69 70 71 72 73 74 75

/**
 * struct io_tlb_mem - IO TLB Memory Pool Descriptor
 *
 * @start:	The start address of the swiotlb memory pool. Used to do a quick
 *		range check to see if the memory was in fact allocated by this
 *		API.
 * @end:	The end address of the swiotlb memory pool. Used to do a quick
 *		range check to see if the memory was in fact allocated by this
 *		API.
76 77 78
 * @vaddr:	The vaddr of the swiotlb memory pool. The swiotlb memory pool
 *		may be remapped in the memory encrypted case and store virtual
 *		address for bounce buffer operation.
79
 * @nslabs:	The number of IO TLB blocks (in groups of 64) between @start and
80 81
 *		@end. For default swiotlb, this is command line adjustable via
 *		setup_io_tlb_npages.
82 83 84 85 86 87 88 89 90 91
 * @used:	The number of used IO TLB block.
 * @list:	The free list describing the number of free entries available
 *		from each index.
 * @index:	The index to start searching in the next round.
 * @orig_addr:	The original address corresponding to a mapped entry.
 * @alloc_size:	Size of the allocated buffer.
 * @lock:	The lock to protect the above data structures in the map and
 *		unmap calls.
 * @debugfs:	The dentry to debugfs.
 * @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 96 97
 */
struct io_tlb_mem {
	phys_addr_t start;
	phys_addr_t end;
98
	void *vaddr;
99 100 101 102 103 104
	unsigned long nslabs;
	unsigned long used;
	unsigned int index;
	spinlock_t lock;
	struct dentry *debugfs;
	bool late_alloc;
105
	bool force_bounce;
106
	bool for_alloc;
107 108 109 110
	struct io_tlb_slot {
		phys_addr_t orig_addr;
		size_t alloc_size;
		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 123 124 125 126 127 128
static inline bool is_swiotlb_force_bounce(struct device *dev)
{
	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;

	return mem && mem->force_bounce;
}

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 139
{
	return false;
}
140 141 142 143
static inline bool is_swiotlb_force_bounce(struct device *dev)
{
	return false;
}
144 145 146 147 148 149 150
static inline void swiotlb_exit(void)
{
}
static inline unsigned int swiotlb_max_segment(void)
{
	return 0;
}
151 152 153 154
static inline size_t swiotlb_max_mapping_size(struct device *dev)
{
	return SIZE_MAX;
}
155

156
static inline bool is_swiotlb_active(struct device *dev)
157 158 159
{
	return false;
}
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);
A
Akinobu Mita 已提交
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
#ifdef CONFIG_DMA_RESTRICTED_POOL
struct page *swiotlb_alloc(struct device *dev, size_t size);
bool swiotlb_free(struct device *dev, struct page *page, size_t size);

static inline bool is_swiotlb_for_alloc(struct device *dev)
{
	return dev->dma_io_tlb_mem->for_alloc;
}
#else
static inline struct page *swiotlb_alloc(struct device *dev, size_t size)
{
	return NULL;
}
static inline bool swiotlb_free(struct device *dev, struct page *page,
				size_t size)
{
	return false;
}
static inline bool is_swiotlb_for_alloc(struct device *dev)
{
	return false;
}
#endif /* CONFIG_DMA_RESTRICTED_POOL */

192 193
extern phys_addr_t swiotlb_unencrypted_base;

194
#endif /* __LINUX_SWIOTLB_H */