]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/linux/iommu.h
iommu: Introduce domain_alloc and domain_free iommu_ops
[mirror_ubuntu-focal-kernel.git] / include / linux / iommu.h
CommitLineData
4a77a6cf
JR
1/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#ifndef __LINUX_IOMMU_H
20#define __LINUX_IOMMU_H
21
74315ccc 22#include <linux/errno.h>
9a08d376 23#include <linux/err.h>
d0f60a44 24#include <linux/of.h>
76582d0a 25#include <linux/types.h>
315786eb 26#include <linux/scatterlist.h>
56fa4849 27#include <trace/events/iommu.h>
74315ccc 28
ca13bb3d
WD
29#define IOMMU_READ (1 << 0)
30#define IOMMU_WRITE (1 << 1)
31#define IOMMU_CACHE (1 << 2) /* DMA cache coherency */
a720b41c 32#define IOMMU_NOEXEC (1 << 3)
4a77a6cf 33
905d66c1 34struct iommu_ops;
d72e31c9 35struct iommu_group;
ff21776d 36struct bus_type;
4a77a6cf 37struct device;
4f3f8d9d 38struct iommu_domain;
ba1eabfa 39struct notifier_block;
4f3f8d9d
OBC
40
41/* iommu fault flags */
42#define IOMMU_FAULT_READ 0x0
43#define IOMMU_FAULT_WRITE 0x1
44
45typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
77ca2332 46 struct device *, unsigned long, int, void *);
4a77a6cf 47
0ff64f80
JR
48struct iommu_domain_geometry {
49 dma_addr_t aperture_start; /* First address that can be mapped */
50 dma_addr_t aperture_end; /* Last address that can be mapped */
51 bool force_aperture; /* DMA only allowed in mappable range? */
52};
53
4a77a6cf 54struct iommu_domain {
b22f6434 55 const struct iommu_ops *ops;
4a77a6cf 56 void *priv;
4f3f8d9d 57 iommu_fault_handler_t handler;
77ca2332 58 void *handler_token;
0ff64f80 59 struct iommu_domain_geometry geometry;
4a77a6cf
JR
60};
61
1aed0748
JR
62enum iommu_cap {
63 IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA
64 transactions */
65 IOMMU_CAP_INTR_REMAP, /* IOMMU supports interrupt isolation */
c4986649 66 IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */
1aed0748 67};
dbb9fd86 68
7cabf491
VS
69/*
70 * Following constraints are specifc to FSL_PAMUV1:
71 * -aperture must be power of 2, and naturally aligned
72 * -number of windows must be power of 2, and address space size
73 * of each window is determined by aperture size / # of windows
74 * -the actual size of the mapped region of a window must be power
75 * of 2 starting with 4KB and physical address must be naturally
76 * aligned.
77 * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
78 * The caller can invoke iommu_domain_get_attr to check if the underlying
79 * iommu implementation supports these constraints.
80 */
81
0cd76dd1 82enum iommu_attr {
0ff64f80 83 DOMAIN_ATTR_GEOMETRY,
d2e12160 84 DOMAIN_ATTR_PAGING,
69356712 85 DOMAIN_ATTR_WINDOWS,
7cabf491
VS
86 DOMAIN_ATTR_FSL_PAMU_STASH,
87 DOMAIN_ATTR_FSL_PAMU_ENABLE,
88 DOMAIN_ATTR_FSL_PAMUV1,
c02607aa 89 DOMAIN_ATTR_NESTING, /* two stages of translation */
a8b8a88a 90 DOMAIN_ATTR_MAX,
0cd76dd1
JR
91};
92
39d4ebb9
JR
93#ifdef CONFIG_IOMMU_API
94
7d3002cc
OBC
95/**
96 * struct iommu_ops - iommu ops and capabilities
97 * @domain_init: init iommu domain
98 * @domain_destroy: destroy iommu domain
99 * @attach_dev: attach device to an iommu domain
100 * @detach_dev: detach device from an iommu domain
101 * @map: map a physically contiguous memory region to an iommu domain
102 * @unmap: unmap a physically contiguous memory region from an iommu domain
315786eb
OH
103 * @map_sg: map a scatter-gather list of physically contiguous memory chunks
104 * to an iommu domain
7d3002cc 105 * @iova_to_phys: translate iova to physical address
d72e31c9
AW
106 * @add_device: add device to iommu grouping
107 * @remove_device: remove device from iommu grouping
0cd76dd1
JR
108 * @domain_get_attr: Query domain attributes
109 * @domain_set_attr: Change domain attributes
d0f60a44 110 * @of_xlate: add OF master IDs to iommu grouping
7d3002cc 111 * @pgsize_bitmap: bitmap of supported page sizes
1cd076bf 112 * @priv: per-instance data private to the iommu driver
7d3002cc 113 */
4a77a6cf 114struct iommu_ops {
3c0e0ca0 115 bool (*capable)(enum iommu_cap);
4a77a6cf
JR
116 int (*domain_init)(struct iommu_domain *domain);
117 void (*domain_destroy)(struct iommu_domain *domain);
938c4709
JR
118
119 /* Domain allocation and freeing by the iommu driver */
120 struct iommu_domain *(*domain_alloc)(void);
121 void (*domain_free)(struct iommu_domain *);
122
4a77a6cf
JR
123 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
124 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
67651786 125 int (*map)(struct iommu_domain *domain, unsigned long iova,
5009065d
OBC
126 phys_addr_t paddr, size_t size, int prot);
127 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
128 size_t size);
315786eb
OH
129 size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
130 struct scatterlist *sg, unsigned int nents, int prot);
bb5547ac 131 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
d72e31c9
AW
132 int (*add_device)(struct device *dev);
133 void (*remove_device)(struct device *dev);
1460432c 134 int (*device_group)(struct device *dev, unsigned int *groupid);
0cd76dd1
JR
135 int (*domain_get_attr)(struct iommu_domain *domain,
136 enum iommu_attr attr, void *data);
137 int (*domain_set_attr)(struct iommu_domain *domain,
138 enum iommu_attr attr, void *data);
d7787d57
JR
139
140 /* Window handling functions */
141 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
80f97f0f 142 phys_addr_t paddr, u64 size, int prot);
d7787d57 143 void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
69356712
JR
144 /* Set the numer of window per domain */
145 int (*domain_set_windows)(struct iommu_domain *domain, u32 w_count);
146 /* Get the numer of window per domain */
147 u32 (*domain_get_windows)(struct iommu_domain *domain);
d7787d57 148
d0f60a44
WD
149#ifdef CONFIG_OF_IOMMU
150 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
151#endif
152
7d3002cc 153 unsigned long pgsize_bitmap;
1cd076bf 154 void *priv;
4a77a6cf
JR
155};
156
d72e31c9
AW
157#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
158#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
159#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
160#define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
161#define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
162#define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
163
b22f6434 164extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
a1b60c1c 165extern bool iommu_present(struct bus_type *bus);
3c0e0ca0 166extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
905d66c1 167extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
aa16bea9 168extern struct iommu_group *iommu_group_get_by_id(int id);
4a77a6cf
JR
169extern void iommu_domain_free(struct iommu_domain *domain);
170extern int iommu_attach_device(struct iommu_domain *domain,
171 struct device *dev);
172extern void iommu_detach_device(struct iommu_domain *domain,
173 struct device *dev);
cefc53c7 174extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
7d3002cc
OBC
175 phys_addr_t paddr, size_t size, int prot);
176extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
177 size_t size);
315786eb
OH
178extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
179 struct scatterlist *sg,unsigned int nents,
180 int prot);
bb5547ac 181extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
4f3f8d9d 182extern void iommu_set_fault_handler(struct iommu_domain *domain,
77ca2332 183 iommu_fault_handler_t handler, void *token);
d72e31c9
AW
184
185extern int iommu_attach_group(struct iommu_domain *domain,
186 struct iommu_group *group);
187extern void iommu_detach_group(struct iommu_domain *domain,
188 struct iommu_group *group);
189extern struct iommu_group *iommu_group_alloc(void);
190extern void *iommu_group_get_iommudata(struct iommu_group *group);
191extern void iommu_group_set_iommudata(struct iommu_group *group,
192 void *iommu_data,
193 void (*release)(void *iommu_data));
194extern int iommu_group_set_name(struct iommu_group *group, const char *name);
195extern int iommu_group_add_device(struct iommu_group *group,
196 struct device *dev);
197extern void iommu_group_remove_device(struct device *dev);
198extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
199 int (*fn)(struct device *, void *));
200extern struct iommu_group *iommu_group_get(struct device *dev);
201extern void iommu_group_put(struct iommu_group *group);
202extern int iommu_group_register_notifier(struct iommu_group *group,
203 struct notifier_block *nb);
204extern int iommu_group_unregister_notifier(struct iommu_group *group,
205 struct notifier_block *nb);
206extern int iommu_group_id(struct iommu_group *group);
104a1c13 207extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
4f3f8d9d 208
0cd76dd1
JR
209extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
210 void *data);
211extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
212 void *data);
c61959ec
AW
213struct device *iommu_device_create(struct device *parent, void *drvdata,
214 const struct attribute_group **groups,
215 const char *fmt, ...);
216void iommu_device_destroy(struct device *dev);
217int iommu_device_link(struct device *dev, struct device *link);
218void iommu_device_unlink(struct device *dev, struct device *link);
4f3f8d9d 219
d7787d57
JR
220/* Window handling function prototypes */
221extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
80f97f0f
VS
222 phys_addr_t offset, u64 size,
223 int prot);
d7787d57 224extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
4f3f8d9d
OBC
225/**
226 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
227 * @domain: the iommu domain where the fault has happened
228 * @dev: the device where the fault has happened
229 * @iova: the faulting address
230 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
231 *
232 * This function should be called by the low-level IOMMU implementations
233 * whenever IOMMU faults happen, to allow high-level users, that are
234 * interested in such events, to know about them.
235 *
236 * This event may be useful for several possible use cases:
237 * - mere logging of the event
238 * - dynamic TLB/PTE loading
239 * - if restarting of the faulting device is required
240 *
241 * Returns 0 on success and an appropriate error code otherwise (if dynamic
242 * PTE/TLB loading will one day be supported, implementations will be able
243 * to tell whether it succeeded or not according to this return value).
0ed6d2d2
OBC
244 *
245 * Specifically, -ENOSYS is returned if a fault handler isn't installed
246 * (though fault handlers can also return -ENOSYS, in case they want to
247 * elicit the default behavior of the IOMMU drivers).
4f3f8d9d
OBC
248 */
249static inline int report_iommu_fault(struct iommu_domain *domain,
250 struct device *dev, unsigned long iova, int flags)
251{
0ed6d2d2 252 int ret = -ENOSYS;
4a77a6cf 253
4f3f8d9d
OBC
254 /*
255 * if upper layers showed interest and installed a fault handler,
256 * invoke it.
257 */
258 if (domain->handler)
77ca2332
OBC
259 ret = domain->handler(domain, dev, iova, flags,
260 domain->handler_token);
4a77a6cf 261
56fa4849 262 trace_io_page_fault(dev, iova, flags);
4f3f8d9d 263 return ret;
4a77a6cf
JR
264}
265
315786eb
OH
266static inline size_t iommu_map_sg(struct iommu_domain *domain,
267 unsigned long iova, struct scatterlist *sg,
268 unsigned int nents, int prot)
269{
270 return domain->ops->map_sg(domain, iova, sg, nents, prot);
271}
272
4a77a6cf
JR
273#else /* CONFIG_IOMMU_API */
274
39d4ebb9 275struct iommu_ops {};
d72e31c9 276struct iommu_group {};
4a77a6cf 277
a1b60c1c 278static inline bool iommu_present(struct bus_type *bus)
4a77a6cf
JR
279{
280 return false;
281}
282
3c0e0ca0
JR
283static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
284{
285 return false;
286}
287
905d66c1 288static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
4a77a6cf
JR
289{
290 return NULL;
291}
292
b62dfd29
AK
293static inline struct iommu_group *iommu_group_get_by_id(int id)
294{
295 return NULL;
296}
297
4a77a6cf
JR
298static inline void iommu_domain_free(struct iommu_domain *domain)
299{
300}
301
302static inline int iommu_attach_device(struct iommu_domain *domain,
303 struct device *dev)
304{
305 return -ENODEV;
306}
307
308static inline void iommu_detach_device(struct iommu_domain *domain,
309 struct device *dev)
310{
311}
312
cefc53c7
JR
313static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
314 phys_addr_t paddr, int gfp_order, int prot)
315{
316 return -ENODEV;
317}
318
319static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
320 int gfp_order)
321{
322 return -ENODEV;
323}
324
315786eb
OH
325static inline size_t iommu_map_sg(struct iommu_domain *domain,
326 unsigned long iova, struct scatterlist *sg,
327 unsigned int nents, int prot)
328{
329 return -ENODEV;
330}
331
d7787d57
JR
332static inline int iommu_domain_window_enable(struct iommu_domain *domain,
333 u32 wnd_nr, phys_addr_t paddr,
80f97f0f 334 u64 size, int prot)
d7787d57
JR
335{
336 return -ENODEV;
337}
338
339static inline void iommu_domain_window_disable(struct iommu_domain *domain,
340 u32 wnd_nr)
341{
342}
343
bb5547ac 344static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
4a77a6cf
JR
345{
346 return 0;
347}
348
4f3f8d9d 349static inline void iommu_set_fault_handler(struct iommu_domain *domain,
77ca2332 350 iommu_fault_handler_t handler, void *token)
4f3f8d9d
OBC
351{
352}
353
bef83de5
AW
354static inline int iommu_attach_group(struct iommu_domain *domain,
355 struct iommu_group *group)
d72e31c9
AW
356{
357 return -ENODEV;
358}
359
bef83de5
AW
360static inline void iommu_detach_group(struct iommu_domain *domain,
361 struct iommu_group *group)
d72e31c9
AW
362{
363}
364
bef83de5 365static inline struct iommu_group *iommu_group_alloc(void)
d72e31c9
AW
366{
367 return ERR_PTR(-ENODEV);
368}
369
bef83de5 370static inline void *iommu_group_get_iommudata(struct iommu_group *group)
d72e31c9
AW
371{
372 return NULL;
373}
374
bef83de5
AW
375static inline void iommu_group_set_iommudata(struct iommu_group *group,
376 void *iommu_data,
377 void (*release)(void *iommu_data))
d72e31c9
AW
378{
379}
380
bef83de5
AW
381static inline int iommu_group_set_name(struct iommu_group *group,
382 const char *name)
d72e31c9
AW
383{
384 return -ENODEV;
385}
386
bef83de5
AW
387static inline int iommu_group_add_device(struct iommu_group *group,
388 struct device *dev)
d72e31c9
AW
389{
390 return -ENODEV;
391}
392
bef83de5 393static inline void iommu_group_remove_device(struct device *dev)
d72e31c9
AW
394{
395}
396
bef83de5
AW
397static inline int iommu_group_for_each_dev(struct iommu_group *group,
398 void *data,
399 int (*fn)(struct device *, void *))
d72e31c9
AW
400{
401 return -ENODEV;
402}
403
bef83de5 404static inline struct iommu_group *iommu_group_get(struct device *dev)
d72e31c9
AW
405{
406 return NULL;
407}
408
bef83de5 409static inline void iommu_group_put(struct iommu_group *group)
d72e31c9
AW
410{
411}
412
bef83de5
AW
413static inline int iommu_group_register_notifier(struct iommu_group *group,
414 struct notifier_block *nb)
1460432c
AW
415{
416 return -ENODEV;
417}
418
bef83de5
AW
419static inline int iommu_group_unregister_notifier(struct iommu_group *group,
420 struct notifier_block *nb)
d72e31c9
AW
421{
422 return 0;
423}
424
bef83de5 425static inline int iommu_group_id(struct iommu_group *group)
d72e31c9
AW
426{
427 return -ENODEV;
428}
1460432c 429
0cd76dd1
JR
430static inline int iommu_domain_get_attr(struct iommu_domain *domain,
431 enum iommu_attr attr, void *data)
432{
433 return -EINVAL;
434}
435
436static inline int iommu_domain_set_attr(struct iommu_domain *domain,
437 enum iommu_attr attr, void *data)
438{
439 return -EINVAL;
440}
441
e09f8ea5
AW
442static inline struct device *iommu_device_create(struct device *parent,
443 void *drvdata,
444 const struct attribute_group **groups,
445 const char *fmt, ...)
c61959ec
AW
446{
447 return ERR_PTR(-ENODEV);
448}
449
e09f8ea5 450static inline void iommu_device_destroy(struct device *dev)
c61959ec
AW
451{
452}
453
e09f8ea5 454static inline int iommu_device_link(struct device *dev, struct device *link)
c61959ec
AW
455{
456 return -EINVAL;
457}
458
e09f8ea5 459static inline void iommu_device_unlink(struct device *dev, struct device *link)
c61959ec
AW
460{
461}
462
4a77a6cf
JR
463#endif /* CONFIG_IOMMU_API */
464
465#endif /* __LINUX_IOMMU_H */