Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +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 | |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 19 | #include <linux/device.h> |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 21 | #include <linux/bug.h> |
| 22 | #include <linux/types.h> |
Andrew Morton | 60db402 | 2009-05-06 16:03:07 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/slab.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 25 | #include <linux/errno.h> |
| 26 | #include <linux/iommu.h> |
| 27 | |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame^] | 28 | static ssize_t show_iommu_group(struct device *dev, |
| 29 | struct device_attribute *attr, char *buf) |
| 30 | { |
| 31 | unsigned int groupid; |
| 32 | |
| 33 | if (iommu_device_group(dev, &groupid)) |
| 34 | return 0; |
| 35 | |
| 36 | return sprintf(buf, "%u", groupid); |
| 37 | } |
| 38 | static DEVICE_ATTR(iommu_group, S_IRUGO, show_iommu_group, NULL); |
| 39 | |
| 40 | static int add_iommu_group(struct device *dev, void *data) |
| 41 | { |
| 42 | unsigned int groupid; |
| 43 | |
| 44 | if (iommu_device_group(dev, &groupid) == 0) |
| 45 | return device_create_file(dev, &dev_attr_iommu_group); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int remove_iommu_group(struct device *dev) |
| 51 | { |
| 52 | unsigned int groupid; |
| 53 | |
| 54 | if (iommu_device_group(dev, &groupid) == 0) |
| 55 | device_remove_file(dev, &dev_attr_iommu_group); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int iommu_device_notifier(struct notifier_block *nb, |
| 61 | unsigned long action, void *data) |
| 62 | { |
| 63 | struct device *dev = data; |
| 64 | |
| 65 | if (action == BUS_NOTIFY_ADD_DEVICE) |
| 66 | return add_iommu_group(dev, NULL); |
| 67 | else if (action == BUS_NOTIFY_DEL_DEVICE) |
| 68 | return remove_iommu_group(dev); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static struct notifier_block iommu_device_nb = { |
| 74 | .notifier_call = iommu_device_notifier, |
| 75 | }; |
| 76 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 77 | static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 78 | { |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame^] | 79 | bus_register_notifier(bus, &iommu_device_nb); |
| 80 | bus_for_each_dev(bus, NULL, NULL, add_iommu_group); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 83 | /** |
| 84 | * bus_set_iommu - set iommu-callbacks for the bus |
| 85 | * @bus: bus. |
| 86 | * @ops: the callbacks provided by the iommu-driver |
| 87 | * |
| 88 | * This function is called by an iommu driver to set the iommu methods |
| 89 | * used for a particular bus. Drivers for devices on that bus can use |
| 90 | * the iommu-api after these ops are registered. |
| 91 | * This special function is needed because IOMMUs are usually devices on |
| 92 | * the bus itself, so the iommu drivers are not initialized when the bus |
| 93 | * is set up. With this function the iommu-driver can set the iommu-ops |
| 94 | * afterwards. |
| 95 | */ |
| 96 | int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 97 | { |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 98 | if (bus->iommu_ops != NULL) |
| 99 | return -EBUSY; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 100 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 101 | bus->iommu_ops = ops; |
| 102 | |
| 103 | /* Do IOMMU specific setup for this bus-type */ |
| 104 | iommu_bus_init(bus, ops); |
| 105 | |
| 106 | return 0; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 107 | } |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
| 109 | |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 110 | bool iommu_present(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 111 | { |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 112 | return bus->iommu_ops != NULL; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 113 | } |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 114 | EXPORT_SYMBOL_GPL(iommu_present); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 115 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 116 | /** |
| 117 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 118 | * @domain: iommu domain |
| 119 | * @handler: fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame] | 120 | * |
| 121 | * This function should be used by IOMMU users which want to be notified |
| 122 | * whenever an IOMMU fault happens. |
| 123 | * |
| 124 | * The fault handler itself should return 0 on success, and an appropriate |
| 125 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 126 | */ |
| 127 | void iommu_set_fault_handler(struct iommu_domain *domain, |
| 128 | iommu_fault_handler_t handler) |
| 129 | { |
| 130 | BUG_ON(!domain); |
| 131 | |
| 132 | domain->handler = handler; |
| 133 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 134 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 135 | |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 136 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 137 | { |
| 138 | struct iommu_domain *domain; |
| 139 | int ret; |
| 140 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 141 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 142 | return NULL; |
| 143 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 144 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); |
| 145 | if (!domain) |
| 146 | return NULL; |
| 147 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 148 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 149 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 150 | ret = domain->ops->domain_init(domain); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 151 | if (ret) |
| 152 | goto out_free; |
| 153 | |
| 154 | return domain; |
| 155 | |
| 156 | out_free: |
| 157 | kfree(domain); |
| 158 | |
| 159 | return NULL; |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 162 | |
| 163 | void iommu_domain_free(struct iommu_domain *domain) |
| 164 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 165 | if (likely(domain->ops->domain_destroy != NULL)) |
| 166 | domain->ops->domain_destroy(domain); |
| 167 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 168 | kfree(domain); |
| 169 | } |
| 170 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 171 | |
| 172 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 173 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 174 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 175 | return -ENODEV; |
| 176 | |
| 177 | return domain->ops->attach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 178 | } |
| 179 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 180 | |
| 181 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 182 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 183 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 184 | return; |
| 185 | |
| 186 | domain->ops->detach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 189 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 190 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 191 | unsigned long iova) |
| 192 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 193 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 194 | return 0; |
| 195 | |
| 196 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 197 | } |
| 198 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 199 | |
| 200 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 201 | unsigned long cap) |
| 202 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 203 | if (unlikely(domain->ops->domain_has_cap == NULL)) |
| 204 | return 0; |
| 205 | |
| 206 | return domain->ops->domain_has_cap(domain, cap); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 207 | } |
| 208 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 209 | |
| 210 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 211 | phys_addr_t paddr, int gfp_order, int prot) |
| 212 | { |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 213 | size_t size; |
| 214 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 215 | if (unlikely(domain->ops->map == NULL)) |
| 216 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 217 | |
Joerg Roedel | 85410340e | 2011-09-06 14:36:17 +0200 | [diff] [blame] | 218 | size = PAGE_SIZE << gfp_order; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 219 | |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 220 | BUG_ON(!IS_ALIGNED(iova | paddr, size)); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 221 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 222 | return domain->ops->map(domain, iova, paddr, gfp_order, prot); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 223 | } |
| 224 | EXPORT_SYMBOL_GPL(iommu_map); |
| 225 | |
| 226 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) |
| 227 | { |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 228 | size_t size; |
| 229 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 230 | if (unlikely(domain->ops->unmap == NULL)) |
| 231 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 232 | |
Joerg Roedel | 85410340e | 2011-09-06 14:36:17 +0200 | [diff] [blame] | 233 | size = PAGE_SIZE << gfp_order; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 234 | |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 235 | BUG_ON(!IS_ALIGNED(iova, size)); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 236 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 237 | return domain->ops->unmap(domain, iova, gfp_order); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame^] | 240 | |
| 241 | int iommu_device_group(struct device *dev, unsigned int *groupid) |
| 242 | { |
| 243 | if (iommu_present(dev->bus) && dev->bus->iommu_ops->device_group) |
| 244 | return dev->bus->iommu_ops->device_group(dev, groupid); |
| 245 | |
| 246 | return -ENODEV; |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(iommu_device_group); |