Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 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 | |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | |
| 26 | #define IOMMU_READ (1) |
| 27 | #define IOMMU_WRITE (2) |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 28 | #define IOMMU_CACHE (4) /* DMA cache coherency */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 29 | |
| 30 | struct iommu_ops; |
| 31 | struct bus_type; |
| 32 | struct device; |
| 33 | struct iommu_domain; |
| 34 | |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 35 | /* iommu fault flags */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 36 | #define IOMMU_FAULT_READ 0x0 |
| 37 | #define IOMMU_FAULT_WRITE 0x1 |
| 38 | |
| 39 | typedef int (*iommu_fault_handler_t)(struct iommu_domain *, |
| 40 | struct device *, unsigned long, int); |
| 41 | |
| 42 | struct iommu_domain { |
| 43 | struct iommu_ops *ops; |
| 44 | void *priv; |
| 45 | iommu_fault_handler_t handler; |
| 46 | }; |
| 47 | |
| 48 | #define IOMMU_CAP_CACHE_COHERENCY 0x1 |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 49 | #define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 50 | |
| 51 | #ifdef CONFIG_IOMMU_API |
| 52 | |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 53 | /** |
| 54 | * struct iommu_ops - iommu ops and capabilities |
| 55 | * @domain_init: init iommu domain |
| 56 | * @domain_destroy: destroy iommu domain |
| 57 | * @attach_dev: attach device to an iommu domain |
| 58 | * @detach_dev: detach device from an iommu domain |
| 59 | * @map: map a physically contiguous memory region to an iommu domain |
| 60 | * @unmap: unmap a physically contiguous memory region from an iommu domain |
| 61 | * @iova_to_phys: translate iova to physical address |
| 62 | * @domain_has_cap: domain capabilities query |
| 63 | * @commit: commit iommu domain |
| 64 | * @pgsize_bitmap: bitmap of supported page sizes |
| 65 | */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 66 | struct iommu_ops { |
| 67 | int (*domain_init)(struct iommu_domain *domain, int flags); |
| 68 | void (*domain_destroy)(struct iommu_domain *domain); |
| 69 | int (*attach_dev)(struct iommu_domain *domain, struct device *dev); |
| 70 | void (*detach_dev)(struct iommu_domain *domain, struct device *dev); |
| 71 | int (*map)(struct iommu_domain *domain, unsigned long iova, |
| 72 | phys_addr_t paddr, size_t size, int prot); |
| 73 | size_t (*unmap)(struct iommu_domain *domain, unsigned long iova, |
| 74 | size_t size); |
| 75 | int (*map_range)(struct iommu_domain *domain, unsigned int iova, |
| 76 | struct scatterlist *sg, unsigned int len, int prot); |
| 77 | int (*unmap_range)(struct iommu_domain *domain, unsigned int iova, |
| 78 | unsigned int len); |
| 79 | phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, |
| 80 | unsigned long iova); |
| 81 | int (*domain_has_cap)(struct iommu_domain *domain, |
| 82 | unsigned long cap); |
| 83 | phys_addr_t (*get_pt_base_addr)(struct iommu_domain *domain); |
| 84 | int (*device_group)(struct device *dev, unsigned int *groupid); |
| 85 | unsigned long pgsize_bitmap; |
| 86 | }; |
| 87 | |
| 88 | extern int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops); |
| 89 | extern bool iommu_present(struct bus_type *bus); |
| 90 | extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags); |
| 91 | extern void iommu_domain_free(struct iommu_domain *domain); |
| 92 | extern int iommu_attach_device(struct iommu_domain *domain, |
| 93 | struct device *dev); |
| 94 | extern void iommu_detach_device(struct iommu_domain *domain, |
| 95 | struct device *dev); |
| 96 | extern int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 97 | phys_addr_t paddr, size_t size, int prot); |
| 98 | extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 99 | size_t size); |
| 100 | extern int iommu_map_range(struct iommu_domain *domain, unsigned int iova, |
| 101 | struct scatterlist *sg, unsigned int len, int prot); |
| 102 | extern int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova, |
| 103 | unsigned int len); |
| 104 | extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 105 | unsigned long iova); |
| 106 | extern int iommu_domain_has_cap(struct iommu_domain *domain, |
| 107 | unsigned long cap); |
| 108 | extern phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain); |
| 109 | extern void iommu_set_fault_handler(struct iommu_domain *domain, |
| 110 | iommu_fault_handler_t handler); |
| 111 | extern int iommu_device_group(struct device *dev, unsigned int *groupid); |
| 112 | |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 113 | /** |
| 114 | * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework |
| 115 | * @domain: the iommu domain where the fault has happened |
| 116 | * @dev: the device where the fault has happened |
| 117 | * @iova: the faulting address |
| 118 | * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) |
| 119 | * |
| 120 | * This function should be called by the low-level IOMMU implementations |
| 121 | * whenever IOMMU faults happen, to allow high-level users, that are |
| 122 | * interested in such events, to know about them. |
| 123 | * |
| 124 | * This event may be useful for several possible use cases: |
| 125 | * - mere logging of the event |
| 126 | * - dynamic TLB/PTE loading |
| 127 | * - if restarting of the faulting device is required |
| 128 | * |
| 129 | * Returns 0 on success and an appropriate error code otherwise (if dynamic |
| 130 | * PTE/TLB loading will one day be supported, implementations will be able |
| 131 | * to tell whether it succeeded or not according to this return value). |
| 132 | * |
| 133 | * Specifically, -ENOSYS is returned if a fault handler isn't installed |
| 134 | * (though fault handlers can also return -ENOSYS, in case they want to |
| 135 | * elicit the default behavior of the IOMMU drivers). |
| 136 | */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 137 | static inline int report_iommu_fault(struct iommu_domain *domain, |
| 138 | struct device *dev, unsigned long iova, int flags) |
| 139 | { |
| 140 | int ret = -ENOSYS; |
| 141 | |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 142 | /* |
| 143 | * if upper layers showed interest and installed a fault handler, |
| 144 | * invoke it. |
| 145 | */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 146 | if (domain->handler) |
| 147 | ret = domain->handler(domain, dev, iova, flags); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 152 | #else /* CONFIG_IOMMU_API */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 153 | |
| 154 | struct iommu_ops {}; |
| 155 | |
| 156 | static inline bool iommu_present(struct bus_type *bus) |
| 157 | { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags) |
| 162 | { |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | static inline void iommu_domain_free(struct iommu_domain *domain) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | static inline int iommu_attach_device(struct iommu_domain *domain, |
| 171 | struct device *dev) |
| 172 | { |
| 173 | return -ENODEV; |
| 174 | } |
| 175 | |
| 176 | static inline void iommu_detach_device(struct iommu_domain *domain, |
| 177 | struct device *dev) |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | static inline int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 182 | phys_addr_t paddr, int gfp_order, int prot) |
| 183 | { |
| 184 | return -ENODEV; |
| 185 | } |
| 186 | |
| 187 | static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 188 | int gfp_order) |
| 189 | { |
| 190 | return -ENODEV; |
| 191 | } |
| 192 | |
| 193 | static inline int iommu_map_range(struct iommu_domain *domain, |
| 194 | unsigned int iova, struct scatterlist *sg, |
| 195 | unsigned int len, int prot) |
| 196 | { |
| 197 | return -ENODEV; |
| 198 | } |
| 199 | |
| 200 | static inline int iommu_unmap_range(struct iommu_domain *domain, |
| 201 | unsigned int iova, unsigned int len) |
| 202 | { |
| 203 | return -ENODEV; |
| 204 | } |
| 205 | |
| 206 | static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 207 | unsigned long iova) |
| 208 | { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static inline int domain_has_cap(struct iommu_domain *domain, |
| 213 | unsigned long cap) |
| 214 | { |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static inline phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 219 | { |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static inline void iommu_set_fault_handler(struct iommu_domain *domain, |
| 224 | iommu_fault_handler_t handler) |
| 225 | { |
| 226 | } |
| 227 | |
| 228 | static inline int iommu_device_group(struct device *dev, unsigned int *groupid) |
| 229 | { |
| 230 | return -ENODEV; |
| 231 | } |
| 232 | |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 233 | #endif /* CONFIG_IOMMU_API */ |
Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame] | 234 | |
Flemmard | fd04ea2 | 2013-04-10 17:42:46 +0200 | [diff] [blame] | 235 | #endif /* __LINUX_IOMMU_H */ |