2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
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.
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.
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
19 #ifndef __LINUX_IOMMU_H
20 #define __LINUX_IOMMU_H
22 #include <linux/errno.h>
23 #include <linux/err.h>
25 #include <linux/types.h>
26 #include <trace/events/iommu.h>
28 #define IOMMU_READ (1 << 0)
29 #define IOMMU_WRITE (1 << 1)
30 #define IOMMU_CACHE (1 << 2) /* DMA cache coherency */
31 #define IOMMU_NOEXEC (1 << 3)
32 #define IOMMU_PRIV (1 << 4)
33 #define IOMMU_DEVICE (1 << 5) /* Indicates access to device memory */
40 struct notifier_block;
42 /* iommu fault flags */
43 #define IOMMU_FAULT_READ (1 << 0)
44 #define IOMMU_FAULT_WRITE (1 << 1)
45 #define IOMMU_FAULT_TRANSLATION (1 << 2)
46 #define IOMMU_FAULT_PERMISSION (1 << 3)
47 #define IOMMU_FAULT_EXTERNAL (1 << 4)
48 #define IOMMU_FAULT_TRANSACTION_STALLED (1 << 5)
50 typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
51 struct device *, unsigned long, int, void *);
53 struct iommu_domain_geometry {
54 dma_addr_t aperture_start; /* First address that can be mapped */
55 dma_addr_t aperture_end; /* Last address that can be mapped */
56 bool force_aperture; /* DMA only allowed in mappable range? */
59 struct iommu_pgtbl_info {
63 /* Domain feature flags */
64 #define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */
65 #define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API
67 #define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */
70 * This are the possible domain-types
72 * IOMMU_DOMAIN_BLOCKED - All DMA is blocked, can be used to isolate
74 * IOMMU_DOMAIN_IDENTITY - DMA addresses are system physical addresses
75 * IOMMU_DOMAIN_UNMANAGED - DMA mappings managed by IOMMU-API user, used
77 * IOMMU_DOMAIN_DMA - Internally used for DMA-API implementations.
78 * This flag allows IOMMU drivers to implement
79 * certain optimizations for these domains
81 #define IOMMU_DOMAIN_BLOCKED (0U)
82 #define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)
83 #define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING)
84 #define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \
85 __IOMMU_DOMAIN_DMA_API)
89 const struct iommu_ops *ops;
90 iommu_fault_handler_t handler;
92 struct iommu_domain_geometry geometry;
97 IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA
99 IOMMU_CAP_INTR_REMAP, /* IOMMU supports interrupt isolation */
100 IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */
104 * Following constraints are specifc to FSL_PAMUV1:
105 * -aperture must be power of 2, and naturally aligned
106 * -number of windows must be power of 2, and address space size
107 * of each window is determined by aperture size / # of windows
108 * -the actual size of the mapped region of a window must be power
109 * of 2 starting with 4KB and physical address must be naturally
111 * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
112 * The caller can invoke iommu_domain_get_attr to check if the underlying
113 * iommu implementation supports these constraints.
117 DOMAIN_ATTR_GEOMETRY,
120 DOMAIN_ATTR_FSL_PAMU_STASH,
121 DOMAIN_ATTR_FSL_PAMU_ENABLE,
122 DOMAIN_ATTR_FSL_PAMUV1,
123 DOMAIN_ATTR_NESTING, /* two stages of translation */
124 DOMAIN_ATTR_PT_BASE_ADDR,
125 DOMAIN_ATTR_SECURE_VMID,
127 DOMAIN_ATTR_CONTEXT_BANK,
129 DOMAIN_ATTR_CONTEXTIDR,
132 DOMAIN_ATTR_NON_FATAL_FAULTS,
133 DOMAIN_ATTR_S1_BYPASS,
135 DOMAIN_ATTR_PGTBL_INFO,
136 DOMAIN_ATTR_EARLY_MAP,
137 DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT,
138 DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT,
139 DOMAIN_ATTR_ENABLE_TTBR1,
140 DOMAIN_ATTR_CB_STALL_DISABLE,
144 extern struct dentry *iommu_debugfs_top;
147 * struct iommu_dm_region - descriptor for a direct mapped memory region
148 * @list: Linked list pointers
149 * @start: System physical start address of the region
150 * @length: Length of the region in bytes
151 * @prot: IOMMU Protection flags (READ/WRITE/...)
153 struct iommu_dm_region {
154 struct list_head list;
160 #ifdef CONFIG_IOMMU_API
163 * struct iommu_ops - iommu ops and capabilities
164 * @domain_init: init iommu domain
165 * @domain_destroy: destroy iommu domain
166 * @attach_dev: attach device to an iommu domain
167 * @detach_dev: detach device from an iommu domain
168 * @map: map a physically contiguous memory region to an iommu domain
169 * @unmap: unmap a physically contiguous memory region from an iommu domain
170 * @map_sg: map a scatter-gather list of physically contiguous memory chunks
172 * @iova_to_phys: translate iova to physical address
173 * @iova_to_phys_hard: translate iova to physical address using IOMMU hardware
174 * @add_device: add device to iommu grouping
175 * @remove_device: remove device from iommu grouping
176 * @domain_get_attr: Query domain attributes
177 * @domain_set_attr: Change domain attributes
178 * @of_xlate: add OF master IDs to iommu grouping
179 * @pgsize_bitmap: bitmap of supported page sizes
180 * @get_pgsize_bitmap: gets a bitmap of supported page sizes for a domain
181 * This takes precedence over @pgsize_bitmap.
182 * @trigger_fault: trigger a fault on the device attached to an iommu domain
183 * @reg_read: read an IOMMU register
184 * @reg_write: write an IOMMU register
185 * @tlbi_domain: Invalidate all TLBs covering an iommu domain
186 * @enable_config_clocks: Enable all config clocks for this domain's IOMMU
187 * @disable_config_clocks: Disable all config clocks for this domain's IOMMU
188 * @priv: per-instance data private to the iommu driver
191 bool (*capable)(enum iommu_cap);
193 /* Domain allocation and freeing by the iommu driver */
194 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
195 void (*domain_free)(struct iommu_domain *);
197 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
198 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
199 int (*map)(struct iommu_domain *domain, unsigned long iova,
200 phys_addr_t paddr, size_t size, int prot);
201 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
203 size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
204 struct scatterlist *sg, unsigned int nents, int prot);
205 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
206 phys_addr_t (*iova_to_phys_hard)(struct iommu_domain *domain,
208 int (*add_device)(struct device *dev);
209 void (*remove_device)(struct device *dev);
210 struct iommu_group *(*device_group)(struct device *dev);
211 int (*domain_get_attr)(struct iommu_domain *domain,
212 enum iommu_attr attr, void *data);
213 int (*domain_set_attr)(struct iommu_domain *domain,
214 enum iommu_attr attr, void *data);
216 /* Request/Free a list of direct mapping requirements for a device */
217 void (*get_dm_regions)(struct device *dev, struct list_head *list);
218 void (*put_dm_regions)(struct device *dev, struct list_head *list);
220 /* Window handling functions */
221 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
222 phys_addr_t paddr, u64 size, int prot);
223 void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
224 /* Set the numer of window per domain */
225 int (*domain_set_windows)(struct iommu_domain *domain, u32 w_count);
226 /* Get the numer of window per domain */
227 u32 (*domain_get_windows)(struct iommu_domain *domain);
228 int (*dma_supported)(struct iommu_domain *domain, struct device *dev,
230 void (*trigger_fault)(struct iommu_domain *domain, unsigned long flags);
231 unsigned long (*reg_read)(struct iommu_domain *domain,
232 unsigned long offset);
233 void (*reg_write)(struct iommu_domain *domain, unsigned long val,
234 unsigned long offset);
235 void (*tlbi_domain)(struct iommu_domain *domain);
236 int (*enable_config_clocks)(struct iommu_domain *domain);
237 void (*disable_config_clocks)(struct iommu_domain *domain);
238 uint64_t (*iova_to_pte)(struct iommu_domain *domain,
241 #ifdef CONFIG_OF_IOMMU
242 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
245 unsigned long (*get_pgsize_bitmap)(struct iommu_domain *domain);
246 bool (*is_iova_coherent)(struct iommu_domain *domain, dma_addr_t iova);
247 unsigned long pgsize_bitmap;
251 #define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
252 #define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
253 #define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
254 #define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
255 #define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
256 #define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
258 extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
259 extern bool iommu_present(struct bus_type *bus);
260 extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
261 extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
262 extern struct iommu_group *iommu_group_get_by_id(int id);
263 extern void iommu_domain_free(struct iommu_domain *domain);
264 extern int iommu_attach_device(struct iommu_domain *domain,
266 extern void iommu_detach_device(struct iommu_domain *domain,
268 extern size_t iommu_pgsize(unsigned long pgsize_bitmap,
269 unsigned long addr_merge, size_t size);
270 extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
271 extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
272 phys_addr_t paddr, size_t size, int prot);
273 extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
275 extern int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
277 extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
278 struct scatterlist *sg,unsigned int nents,
280 extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
281 extern phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
283 extern bool iommu_is_iova_coherent(struct iommu_domain *domain,
285 extern void iommu_set_fault_handler(struct iommu_domain *domain,
286 iommu_fault_handler_t handler, void *token);
287 extern void iommu_trigger_fault(struct iommu_domain *domain,
288 unsigned long flags);
289 extern unsigned long iommu_reg_read(struct iommu_domain *domain,
290 unsigned long offset);
291 extern void iommu_reg_write(struct iommu_domain *domain, unsigned long offset,
294 extern void iommu_get_dm_regions(struct device *dev, struct list_head *list);
295 extern void iommu_put_dm_regions(struct device *dev, struct list_head *list);
296 extern int iommu_request_dm_for_dev(struct device *dev);
298 extern int iommu_attach_group(struct iommu_domain *domain,
299 struct iommu_group *group);
300 extern void iommu_detach_group(struct iommu_domain *domain,
301 struct iommu_group *group);
302 extern struct iommu_group *iommu_group_alloc(void);
303 extern void *iommu_group_get_iommudata(struct iommu_group *group);
304 extern void iommu_group_set_iommudata(struct iommu_group *group,
306 void (*release)(void *iommu_data));
307 extern int iommu_group_set_name(struct iommu_group *group, const char *name);
308 extern int iommu_group_add_device(struct iommu_group *group,
310 extern void iommu_group_remove_device(struct device *dev);
311 extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
312 int (*fn)(struct device *, void *));
313 extern struct iommu_group *iommu_group_get(struct device *dev);
314 extern void iommu_group_put(struct iommu_group *group);
315 extern int iommu_group_register_notifier(struct iommu_group *group,
316 struct notifier_block *nb);
317 extern int iommu_group_unregister_notifier(struct iommu_group *group,
318 struct notifier_block *nb);
319 extern int iommu_group_id(struct iommu_group *group);
320 extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
321 extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
323 extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
325 extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
327 struct device *iommu_device_create(struct device *parent, void *drvdata,
328 const struct attribute_group **groups,
329 const char *fmt, ...) __printf(4, 5);
330 void iommu_device_destroy(struct device *dev);
331 int iommu_device_link(struct device *dev, struct device *link);
332 void iommu_device_unlink(struct device *dev, struct device *link);
334 /* Window handling function prototypes */
335 extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
336 phys_addr_t offset, u64 size,
338 extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
340 extern uint64_t iommu_iova_to_pte(struct iommu_domain *domain,
343 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
344 * @domain: the iommu domain where the fault has happened
345 * @dev: the device where the fault has happened
346 * @iova: the faulting address
347 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
349 * This function should be called by the low-level IOMMU implementations
350 * whenever IOMMU faults happen, to allow high-level users, that are
351 * interested in such events, to know about them.
353 * This event may be useful for several possible use cases:
354 * - mere logging of the event
355 * - dynamic TLB/PTE loading
356 * - if restarting of the faulting device is required
358 * Returns 0 on success and an appropriate error code otherwise (if dynamic
359 * PTE/TLB loading will one day be supported, implementations will be able
360 * to tell whether it succeeded or not according to this return value).
362 * Specifically, -ENOSYS is returned if a fault handler isn't installed
363 * (though fault handlers can also return -ENOSYS, in case they want to
364 * elicit the default behavior of the IOMMU drivers).
366 * Client fault handler returns -EBUSY to signal to the IOMMU driver
367 * that the client will take responsibility for any further fault
368 * handling, including clearing fault status registers or retrying
369 * the faulting transaction.
371 static inline int report_iommu_fault(struct iommu_domain *domain,
372 struct device *dev, unsigned long iova, int flags)
377 * if upper layers showed interest and installed a fault handler,
381 ret = domain->handler(domain, dev, iova, flags,
382 domain->handler_token);
384 trace_io_page_fault(dev, iova, flags);
388 static inline size_t iommu_map_sg(struct iommu_domain *domain,
389 unsigned long iova, struct scatterlist *sg,
390 unsigned int nents, int prot)
394 trace_map_sg_start(iova, nents);
395 ret = domain->ops->map_sg(domain, iova, sg, nents, prot);
396 trace_map_sg_end(iova, nents);
400 extern int iommu_dma_supported(struct iommu_domain *domain, struct device *dev,
403 /* PCI device grouping function */
404 extern struct iommu_group *pci_device_group(struct device *dev);
405 /* Generic device grouping function */
406 extern struct iommu_group *generic_device_group(struct device *dev);
408 static inline void iommu_tlbiall(struct iommu_domain *domain)
410 if (domain->ops->tlbi_domain)
411 domain->ops->tlbi_domain(domain);
414 static inline int iommu_enable_config_clocks(struct iommu_domain *domain)
416 if (domain->ops->enable_config_clocks)
417 return domain->ops->enable_config_clocks(domain);
421 static inline void iommu_disable_config_clocks(struct iommu_domain *domain)
423 if (domain->ops->disable_config_clocks)
424 domain->ops->disable_config_clocks(domain);
427 #else /* CONFIG_IOMMU_API */
430 struct iommu_group {};
432 static inline bool iommu_present(struct bus_type *bus)
437 static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
442 static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
447 static inline struct iommu_group *iommu_group_get_by_id(int id)
452 static inline void iommu_domain_free(struct iommu_domain *domain)
456 static inline int iommu_attach_device(struct iommu_domain *domain,
462 static inline void iommu_detach_device(struct iommu_domain *domain,
467 static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
472 static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
473 phys_addr_t paddr, int gfp_order, int prot)
478 static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
484 static inline int iommu_unmap_range(struct iommu_domain *domain,
485 unsigned int iova, unsigned int len)
490 static inline size_t iommu_map_sg(struct iommu_domain *domain,
491 unsigned long iova, struct scatterlist *sg,
492 unsigned int nents, int prot)
497 static inline int iommu_domain_window_enable(struct iommu_domain *domain,
498 u32 wnd_nr, phys_addr_t paddr,
504 static inline void iommu_domain_window_disable(struct iommu_domain *domain,
509 static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
514 static inline phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
520 static inline bool iommu_is_iova_coherent(struct iommu_domain *domain,
526 static inline void iommu_set_fault_handler(struct iommu_domain *domain,
527 iommu_fault_handler_t handler, void *token)
531 static inline void iommu_trigger_fault(struct iommu_domain *domain,
536 static inline unsigned long iommu_reg_read(struct iommu_domain *domain,
537 unsigned long offset)
542 static inline void iommu_reg_write(struct iommu_domain *domain,
543 unsigned long val, unsigned long offset)
547 static inline void iommu_get_dm_regions(struct device *dev,
548 struct list_head *list)
552 static inline void iommu_put_dm_regions(struct device *dev,
553 struct list_head *list)
557 static inline int iommu_request_dm_for_dev(struct device *dev)
562 static inline int iommu_attach_group(struct iommu_domain *domain,
563 struct iommu_group *group)
568 static inline void iommu_detach_group(struct iommu_domain *domain,
569 struct iommu_group *group)
573 static inline struct iommu_group *iommu_group_alloc(void)
575 return ERR_PTR(-ENODEV);
578 static inline void *iommu_group_get_iommudata(struct iommu_group *group)
583 static inline void iommu_group_set_iommudata(struct iommu_group *group,
585 void (*release)(void *iommu_data))
589 static inline int iommu_group_set_name(struct iommu_group *group,
595 static inline int iommu_group_add_device(struct iommu_group *group,
601 static inline void iommu_group_remove_device(struct device *dev)
605 static inline int iommu_group_for_each_dev(struct iommu_group *group,
607 int (*fn)(struct device *, void *))
612 static inline struct iommu_group *iommu_group_get(struct device *dev)
617 static inline void iommu_group_put(struct iommu_group *group)
621 static inline int iommu_group_register_notifier(struct iommu_group *group,
622 struct notifier_block *nb)
627 static inline int iommu_group_unregister_notifier(struct iommu_group *group,
628 struct notifier_block *nb)
633 static inline int iommu_group_id(struct iommu_group *group)
638 static inline int iommu_domain_get_attr(struct iommu_domain *domain,
639 enum iommu_attr attr, void *data)
644 static inline int iommu_domain_set_attr(struct iommu_domain *domain,
645 enum iommu_attr attr, void *data)
650 static inline struct device *iommu_device_create(struct device *parent,
652 const struct attribute_group **groups,
653 const char *fmt, ...)
655 return ERR_PTR(-ENODEV);
658 static inline void iommu_device_destroy(struct device *dev)
662 static inline int iommu_device_link(struct device *dev, struct device *link)
667 static inline void iommu_device_unlink(struct device *dev, struct device *link)
671 static inline int iommu_dma_supported(struct iommu_domain *domain,
672 struct device *dev, u64 mask)
677 static inline void iommu_tlbiall(struct iommu_domain *domain)
681 static inline int iommu_enable_config_clocks(struct iommu_domain *domain)
686 static inline void iommu_disable_config_clocks(struct iommu_domain *domain)
690 #endif /* CONFIG_IOMMU_API */
692 #endif /* __LINUX_IOMMU_H */