Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +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 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <linux/types.h> |
| 23 | #include <linux/errno.h> |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 24 | #include <linux/scatterlist.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 26 | #define IOMMU_READ (1) |
| 27 | #define IOMMU_WRITE (2) |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 28 | #define IOMMU_CACHE (4) /* DMA cache coherency */ |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 29 | |
| 30 | struct device; |
Ohad Ben-Cohen | a1c6df3 | 2011-09-13 15:25:23 -0400 | [diff] [blame^] | 31 | struct iommu_domain; |
| 32 | |
| 33 | /* iommu fault flags */ |
| 34 | #define IOMMU_FAULT_READ 0x0 |
| 35 | #define IOMMU_FAULT_WRITE 0x1 |
| 36 | |
| 37 | typedef int (*iommu_fault_handler_t)(struct iommu_domain *, |
| 38 | struct device *, unsigned long, int); |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 39 | |
| 40 | struct iommu_domain { |
| 41 | void *priv; |
Ohad Ben-Cohen | a1c6df3 | 2011-09-13 15:25:23 -0400 | [diff] [blame^] | 42 | iommu_fault_handler_t handler; |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 45 | #define IOMMU_CAP_CACHE_COHERENCY 0x1 |
Tom Lyon | 323f99c | 2010-07-02 16:56:14 -0400 | [diff] [blame] | 46 | #define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */ |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 47 | |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 48 | struct iommu_ops { |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 49 | int (*domain_init)(struct iommu_domain *domain, int flags); |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 50 | void (*domain_destroy)(struct iommu_domain *domain); |
| 51 | int (*attach_dev)(struct iommu_domain *domain, struct device *dev); |
| 52 | void (*detach_dev)(struct iommu_domain *domain, struct device *dev); |
Joerg Roedel | 6765178 | 2010-01-21 16:32:27 +0100 | [diff] [blame] | 53 | int (*map)(struct iommu_domain *domain, unsigned long iova, |
| 54 | phys_addr_t paddr, int gfp_order, int prot); |
| 55 | int (*unmap)(struct iommu_domain *domain, unsigned long iova, |
| 56 | int gfp_order); |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 57 | int (*map_range)(struct iommu_domain *domain, unsigned int iova, |
| 58 | struct scatterlist *sg, unsigned int len, int prot); |
| 59 | int (*unmap_range)(struct iommu_domain *domain, unsigned int iova, |
| 60 | unsigned int len); |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 61 | phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, |
| 62 | unsigned long iova); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 63 | int (*domain_has_cap)(struct iommu_domain *domain, |
| 64 | unsigned long cap); |
Shubhraprakash Das | 4c436f2 | 2011-12-02 18:01:57 -0700 | [diff] [blame] | 65 | phys_addr_t (*get_pt_base_addr)(struct iommu_domain *domain); |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | #ifdef CONFIG_IOMMU_API |
| 69 | |
| 70 | extern void register_iommu(struct iommu_ops *ops); |
| 71 | extern bool iommu_found(void); |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 72 | extern struct iommu_domain *iommu_domain_alloc(int flags); |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 73 | extern void iommu_domain_free(struct iommu_domain *domain); |
| 74 | extern int iommu_attach_device(struct iommu_domain *domain, |
| 75 | struct device *dev); |
| 76 | extern void iommu_detach_device(struct iommu_domain *domain, |
| 77 | struct device *dev); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 78 | extern int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 79 | phys_addr_t paddr, int gfp_order, int prot); |
| 80 | extern int iommu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 81 | int gfp_order); |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 82 | extern int iommu_map_range(struct iommu_domain *domain, unsigned int iova, |
| 83 | struct scatterlist *sg, unsigned int len, int prot); |
| 84 | extern int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova, |
| 85 | unsigned int len); |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 86 | extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 87 | unsigned long iova); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 88 | extern int iommu_domain_has_cap(struct iommu_domain *domain, |
| 89 | unsigned long cap); |
Shubhraprakash Das | 4c436f2 | 2011-12-02 18:01:57 -0700 | [diff] [blame] | 90 | extern phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain); |
Ohad Ben-Cohen | a1c6df3 | 2011-09-13 15:25:23 -0400 | [diff] [blame^] | 91 | extern void iommu_set_fault_handler(struct iommu_domain *domain, |
| 92 | iommu_fault_handler_t handler); |
| 93 | |
| 94 | /** |
| 95 | * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework |
| 96 | * @domain: the iommu domain where the fault has happened |
| 97 | * @dev: the device where the fault has happened |
| 98 | * @iova: the faulting address |
| 99 | * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) |
| 100 | * |
| 101 | * This function should be called by the low-level IOMMU implementations |
| 102 | * whenever IOMMU faults happen, to allow high-level users, that are |
| 103 | * interested in such events, to know about them. |
| 104 | * |
| 105 | * This event may be useful for several possible use cases: |
| 106 | * - mere logging of the event |
| 107 | * - dynamic TLB/PTE loading |
| 108 | * - if restarting of the faulting device is required |
| 109 | * |
| 110 | * Returns 0 on success and an appropriate error code otherwise (if dynamic |
| 111 | * PTE/TLB loading will one day be supported, implementations will be able |
| 112 | * to tell whether it succeeded or not according to this return value). |
| 113 | */ |
| 114 | static inline int report_iommu_fault(struct iommu_domain *domain, |
| 115 | struct device *dev, unsigned long iova, int flags) |
| 116 | { |
| 117 | int ret = 0; |
| 118 | |
| 119 | /* |
| 120 | * if upper layers showed interest and installed a fault handler, |
| 121 | * invoke it. |
| 122 | */ |
| 123 | if (domain->handler) |
| 124 | ret = domain->handler(domain, dev, iova, flags); |
| 125 | |
| 126 | return ret; |
| 127 | } |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 128 | |
| 129 | #else /* CONFIG_IOMMU_API */ |
| 130 | |
| 131 | static inline void register_iommu(struct iommu_ops *ops) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | static inline bool iommu_found(void) |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 140 | static inline struct iommu_domain *iommu_domain_alloc(int flags) |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 141 | { |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | static inline void iommu_domain_free(struct iommu_domain *domain) |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | static inline int iommu_attach_device(struct iommu_domain *domain, |
| 150 | struct device *dev) |
| 151 | { |
| 152 | return -ENODEV; |
| 153 | } |
| 154 | |
| 155 | static inline void iommu_detach_device(struct iommu_domain *domain, |
| 156 | struct device *dev) |
| 157 | { |
| 158 | } |
| 159 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 160 | static inline int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 161 | phys_addr_t paddr, int gfp_order, int prot) |
| 162 | { |
| 163 | return -ENODEV; |
| 164 | } |
| 165 | |
| 166 | static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 167 | int gfp_order) |
| 168 | { |
| 169 | return -ENODEV; |
| 170 | } |
| 171 | |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 172 | static inline int iommu_map_range(struct iommu_domain *domain, |
| 173 | unsigned int iova, struct scatterlist *sg, |
| 174 | unsigned int len, int prot) |
| 175 | { |
| 176 | return -ENODEV; |
| 177 | } |
| 178 | |
| 179 | static inline int iommu_unmap_range(struct iommu_domain *domain, |
| 180 | unsigned int iova, unsigned int len) |
| 181 | { |
| 182 | return -ENODEV; |
| 183 | } |
| 184 | |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 185 | static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 186 | unsigned long iova) |
| 187 | { |
| 188 | return 0; |
| 189 | } |
| 190 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 191 | static inline int domain_has_cap(struct iommu_domain *domain, |
| 192 | unsigned long cap) |
| 193 | { |
| 194 | return 0; |
| 195 | } |
| 196 | |
Shubhraprakash Das | 4c436f2 | 2011-12-02 18:01:57 -0700 | [diff] [blame] | 197 | static inline phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 198 | { |
| 199 | return 0; |
| 200 | } |
Ohad Ben-Cohen | a1c6df3 | 2011-09-13 15:25:23 -0400 | [diff] [blame^] | 201 | |
| 202 | static inline void iommu_set_fault_handler(struct iommu_domain *domain, |
| 203 | iommu_fault_handler_t handler) |
| 204 | { |
| 205 | } |
| 206 | |
Joerg Roedel | 4a77a6c | 2008-11-26 17:02:33 +0100 | [diff] [blame] | 207 | #endif /* CONFIG_IOMMU_API */ |
| 208 | |
| 209 | #endif /* __LINUX_IOMMU_H */ |