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 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 28 | 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] | 29 | { |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 30 | } |
| 31 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 32 | /** |
| 33 | * bus_set_iommu - set iommu-callbacks for the bus |
| 34 | * @bus: bus. |
| 35 | * @ops: the callbacks provided by the iommu-driver |
| 36 | * |
| 37 | * This function is called by an iommu driver to set the iommu methods |
| 38 | * used for a particular bus. Drivers for devices on that bus can use |
| 39 | * the iommu-api after these ops are registered. |
| 40 | * This special function is needed because IOMMUs are usually devices on |
| 41 | * the bus itself, so the iommu drivers are not initialized when the bus |
| 42 | * is set up. With this function the iommu-driver can set the iommu-ops |
| 43 | * afterwards. |
| 44 | */ |
| 45 | int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 46 | { |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 47 | if (bus->iommu_ops != NULL) |
| 48 | return -EBUSY; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 49 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 50 | bus->iommu_ops = ops; |
| 51 | |
| 52 | /* Do IOMMU specific setup for this bus-type */ |
| 53 | iommu_bus_init(bus, ops); |
| 54 | |
| 55 | return 0; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 56 | } |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 57 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
| 58 | |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 59 | bool iommu_present(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 60 | { |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 61 | return bus->iommu_ops != NULL; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 62 | } |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(iommu_present); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 64 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 65 | /** |
| 66 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 67 | * @domain: iommu domain |
| 68 | * @handler: fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame] | 69 | * |
| 70 | * This function should be used by IOMMU users which want to be notified |
| 71 | * whenever an IOMMU fault happens. |
| 72 | * |
| 73 | * The fault handler itself should return 0 on success, and an appropriate |
| 74 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 75 | */ |
| 76 | void iommu_set_fault_handler(struct iommu_domain *domain, |
| 77 | iommu_fault_handler_t handler) |
| 78 | { |
| 79 | BUG_ON(!domain); |
| 80 | |
| 81 | domain->handler = handler; |
| 82 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 84 | |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 85 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 86 | { |
| 87 | struct iommu_domain *domain; |
| 88 | int ret; |
| 89 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 90 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 91 | return NULL; |
| 92 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 93 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); |
| 94 | if (!domain) |
| 95 | return NULL; |
| 96 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 97 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 98 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 99 | ret = domain->ops->domain_init(domain); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 100 | if (ret) |
| 101 | goto out_free; |
| 102 | |
| 103 | return domain; |
| 104 | |
| 105 | out_free: |
| 106 | kfree(domain); |
| 107 | |
| 108 | return NULL; |
| 109 | } |
| 110 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 111 | |
| 112 | void iommu_domain_free(struct iommu_domain *domain) |
| 113 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 114 | if (likely(domain->ops->domain_destroy != NULL)) |
| 115 | domain->ops->domain_destroy(domain); |
| 116 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 117 | kfree(domain); |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 120 | |
| 121 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 122 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 123 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 124 | return -ENODEV; |
| 125 | |
| 126 | return domain->ops->attach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 127 | } |
| 128 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 129 | |
| 130 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 131 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 132 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 133 | return; |
| 134 | |
| 135 | domain->ops->detach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 138 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 139 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 140 | unsigned long iova) |
| 141 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 142 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 143 | return 0; |
| 144 | |
| 145 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 146 | } |
| 147 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 148 | |
| 149 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 150 | unsigned long cap) |
| 151 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 152 | if (unlikely(domain->ops->domain_has_cap == NULL)) |
| 153 | return 0; |
| 154 | |
| 155 | return domain->ops->domain_has_cap(domain, cap); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 156 | } |
| 157 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 158 | |
| 159 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 160 | phys_addr_t paddr, int gfp_order, int prot) |
| 161 | { |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 162 | size_t size; |
| 163 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 164 | if (unlikely(domain->ops->map == NULL)) |
| 165 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 166 | |
Joerg Roedel | 85410340e | 2011-09-06 14:36:17 +0200 | [diff] [blame] | 167 | size = PAGE_SIZE << gfp_order; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 168 | |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 169 | BUG_ON(!IS_ALIGNED(iova | paddr, size)); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 170 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 171 | return domain->ops->map(domain, iova, paddr, gfp_order, prot); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 172 | } |
| 173 | EXPORT_SYMBOL_GPL(iommu_map); |
| 174 | |
| 175 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) |
| 176 | { |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 177 | size_t size; |
| 178 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 179 | if (unlikely(domain->ops->unmap == NULL)) |
| 180 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 181 | |
Joerg Roedel | 85410340e | 2011-09-06 14:36:17 +0200 | [diff] [blame] | 182 | size = PAGE_SIZE << gfp_order; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 183 | |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 184 | BUG_ON(!IS_ALIGNED(iova, size)); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 185 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 186 | return domain->ops->unmap(domain, iova, gfp_order); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(iommu_unmap); |