xref: /linux-6.15/include/linux/iova.h (revision a0adc8ea)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2006, Intel Corporation.
4  *
5  * Copyright (C) 2006-2008 Intel Corporation
6  * Author: Anil S Keshavamurthy <[email protected]>
7  */
8 
9 #ifndef _IOVA_H_
10 #define _IOVA_H_
11 
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/rbtree.h>
15 #include <linux/atomic.h>
16 #include <linux/dma-mapping.h>
17 
18 /* iova structure */
19 struct iova {
20 	struct rb_node	node;
21 	unsigned long	pfn_hi; /* Highest allocated pfn */
22 	unsigned long	pfn_lo; /* Lowest allocated pfn */
23 };
24 
25 struct iova_magazine;
26 struct iova_cpu_rcache;
27 
28 #define IOVA_RANGE_CACHE_MAX_SIZE 6	/* log of max cached IOVA range size (in pages) */
29 #define MAX_GLOBAL_MAGS 32	/* magazines per bin */
30 
31 struct iova_rcache {
32 	spinlock_t lock;
33 	unsigned long depot_size;
34 	struct iova_magazine *depot[MAX_GLOBAL_MAGS];
35 	struct iova_cpu_rcache __percpu *cpu_rcaches;
36 };
37 
38 struct iova_domain;
39 
40 /* Call-Back from IOVA code into IOMMU drivers */
41 typedef void (* iova_flush_cb)(struct iova_domain *domain);
42 
43 /* Destructor for per-entry data */
44 typedef void (* iova_entry_dtor)(unsigned long data);
45 
46 /* Number of entries per Flush Queue */
47 #define IOVA_FQ_SIZE	256
48 
49 /* Timeout (in ms) after which entries are flushed from the Flush-Queue */
50 #define IOVA_FQ_TIMEOUT	10
51 
52 /* Flush Queue entry for defered flushing */
53 struct iova_fq_entry {
54 	unsigned long iova_pfn;
55 	unsigned long pages;
56 	unsigned long data;
57 	u64 counter; /* Flush counter when this entrie was added */
58 };
59 
60 /* Per-CPU Flush Queue structure */
61 struct iova_fq {
62 	struct iova_fq_entry entries[IOVA_FQ_SIZE];
63 	unsigned head, tail;
64 	spinlock_t lock;
65 };
66 
67 /* holds all the iova translations for a domain */
68 struct iova_domain {
69 	spinlock_t	iova_rbtree_lock; /* Lock to protect update of rbtree */
70 	struct rb_root	rbroot;		/* iova domain rbtree root */
71 	struct rb_node	*cached_node;	/* Save last alloced node */
72 	struct rb_node	*cached32_node; /* Save last 32-bit alloced node */
73 	unsigned long	granule;	/* pfn granularity for this domain */
74 	unsigned long	start_pfn;	/* Lower limit for this domain */
75 	unsigned long	dma_32bit_pfn;
76 	unsigned long	max32_alloc_size; /* Size of last failed allocation */
77 	struct iova_fq __percpu *fq;	/* Flush Queue */
78 
79 	atomic64_t	fq_flush_start_cnt;	/* Number of TLB flushes that
80 						   have been started */
81 
82 	atomic64_t	fq_flush_finish_cnt;	/* Number of TLB flushes that
83 						   have been finished */
84 
85 	struct iova	anchor;		/* rbtree lookup anchor */
86 	struct iova_rcache rcaches[IOVA_RANGE_CACHE_MAX_SIZE];	/* IOVA range caches */
87 
88 	iova_flush_cb	flush_cb;	/* Call-Back function to flush IOMMU
89 					   TLBs */
90 
91 	iova_entry_dtor entry_dtor;	/* IOMMU driver specific destructor for
92 					   iova entry */
93 
94 	struct timer_list fq_timer;		/* Timer to regularily empty the
95 						   flush-queues */
96 	atomic_t fq_timer_on;			/* 1 when timer is active, 0
97 						   when not */
98 };
99 
100 static inline unsigned long iova_size(struct iova *iova)
101 {
102 	return iova->pfn_hi - iova->pfn_lo + 1;
103 }
104 
105 static inline unsigned long iova_shift(struct iova_domain *iovad)
106 {
107 	return __ffs(iovad->granule);
108 }
109 
110 static inline unsigned long iova_mask(struct iova_domain *iovad)
111 {
112 	return iovad->granule - 1;
113 }
114 
115 static inline size_t iova_offset(struct iova_domain *iovad, dma_addr_t iova)
116 {
117 	return iova & iova_mask(iovad);
118 }
119 
120 static inline size_t iova_align(struct iova_domain *iovad, size_t size)
121 {
122 	return ALIGN(size, iovad->granule);
123 }
124 
125 static inline dma_addr_t iova_dma_addr(struct iova_domain *iovad, struct iova *iova)
126 {
127 	return (dma_addr_t)iova->pfn_lo << iova_shift(iovad);
128 }
129 
130 static inline unsigned long iova_pfn(struct iova_domain *iovad, dma_addr_t iova)
131 {
132 	return iova >> iova_shift(iovad);
133 }
134 
135 #if IS_ENABLED(CONFIG_IOMMU_IOVA)
136 int iova_cache_get(void);
137 void iova_cache_put(void);
138 
139 void free_iova(struct iova_domain *iovad, unsigned long pfn);
140 void __free_iova(struct iova_domain *iovad, struct iova *iova);
141 struct iova *alloc_iova(struct iova_domain *iovad, unsigned long size,
142 	unsigned long limit_pfn,
143 	bool size_aligned);
144 void free_iova_fast(struct iova_domain *iovad, unsigned long pfn,
145 		    unsigned long size);
146 void queue_iova(struct iova_domain *iovad,
147 		unsigned long pfn, unsigned long pages,
148 		unsigned long data);
149 unsigned long alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
150 			      unsigned long limit_pfn, bool flush_rcache);
151 struct iova *reserve_iova(struct iova_domain *iovad, unsigned long pfn_lo,
152 	unsigned long pfn_hi);
153 void copy_reserved_iova(struct iova_domain *from, struct iova_domain *to);
154 void init_iova_domain(struct iova_domain *iovad, unsigned long granule,
155 	unsigned long start_pfn);
156 bool has_iova_flush_queue(struct iova_domain *iovad);
157 int init_iova_flush_queue(struct iova_domain *iovad,
158 			  iova_flush_cb flush_cb, iova_entry_dtor entry_dtor);
159 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn);
160 void put_iova_domain(struct iova_domain *iovad);
161 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
162 #else
163 static inline int iova_cache_get(void)
164 {
165 	return -ENOTSUPP;
166 }
167 
168 static inline void iova_cache_put(void)
169 {
170 }
171 
172 static inline void free_iova(struct iova_domain *iovad, unsigned long pfn)
173 {
174 }
175 
176 static inline void __free_iova(struct iova_domain *iovad, struct iova *iova)
177 {
178 }
179 
180 static inline struct iova *alloc_iova(struct iova_domain *iovad,
181 				      unsigned long size,
182 				      unsigned long limit_pfn,
183 				      bool size_aligned)
184 {
185 	return NULL;
186 }
187 
188 static inline void free_iova_fast(struct iova_domain *iovad,
189 				  unsigned long pfn,
190 				  unsigned long size)
191 {
192 }
193 
194 static inline void queue_iova(struct iova_domain *iovad,
195 			      unsigned long pfn, unsigned long pages,
196 			      unsigned long data)
197 {
198 }
199 
200 static inline unsigned long alloc_iova_fast(struct iova_domain *iovad,
201 					    unsigned long size,
202 					    unsigned long limit_pfn,
203 					    bool flush_rcache)
204 {
205 	return 0;
206 }
207 
208 static inline struct iova *reserve_iova(struct iova_domain *iovad,
209 					unsigned long pfn_lo,
210 					unsigned long pfn_hi)
211 {
212 	return NULL;
213 }
214 
215 static inline void copy_reserved_iova(struct iova_domain *from,
216 				      struct iova_domain *to)
217 {
218 }
219 
220 static inline void init_iova_domain(struct iova_domain *iovad,
221 				    unsigned long granule,
222 				    unsigned long start_pfn)
223 {
224 }
225 
226 static inline bool has_iova_flush_queue(struct iova_domain *iovad)
227 {
228 	return false;
229 }
230 
231 static inline int init_iova_flush_queue(struct iova_domain *iovad,
232 					iova_flush_cb flush_cb,
233 					iova_entry_dtor entry_dtor)
234 {
235 	return -ENODEV;
236 }
237 
238 static inline struct iova *find_iova(struct iova_domain *iovad,
239 				     unsigned long pfn)
240 {
241 	return NULL;
242 }
243 
244 static inline void put_iova_domain(struct iova_domain *iovad)
245 {
246 }
247 
248 static inline void free_cpu_cached_iovas(unsigned int cpu,
249 					 struct iova_domain *iovad)
250 {
251 }
252 #endif
253 
254 #endif
255