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