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) |
| 29 | { |
| 30 | } |
| 31 | |
| 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) |
| 46 | { |
| 47 | if (bus->iommu_ops != NULL) |
| 48 | return -EBUSY; |
| 49 | |
| 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; |
| 56 | } |
| 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 | |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 65 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 66 | { |
| 67 | struct iommu_domain *domain; |
| 68 | int ret; |
| 69 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame^] | 70 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 71 | return NULL; |
| 72 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 73 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); |
| 74 | if (!domain) |
| 75 | return NULL; |
| 76 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame^] | 77 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 78 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame^] | 79 | ret = domain->ops->domain_init(domain); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 80 | if (ret) |
| 81 | goto out_free; |
| 82 | |
| 83 | return domain; |
| 84 | |
| 85 | out_free: |
| 86 | kfree(domain); |
| 87 | |
| 88 | return NULL; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 91 | |
| 92 | void iommu_domain_free(struct iommu_domain *domain) |
| 93 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 94 | if (likely(domain->ops->domain_destroy != NULL)) |
| 95 | domain->ops->domain_destroy(domain); |
| 96 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 97 | kfree(domain); |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 100 | |
| 101 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 102 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 103 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 104 | return -ENODEV; |
| 105 | |
| 106 | return domain->ops->attach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 107 | } |
| 108 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 109 | |
| 110 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 111 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 112 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 113 | return; |
| 114 | |
| 115 | domain->ops->detach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 116 | } |
| 117 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 118 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 119 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 120 | unsigned long iova) |
| 121 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 122 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 123 | return 0; |
| 124 | |
| 125 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 128 | |
| 129 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 130 | unsigned long cap) |
| 131 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 132 | if (unlikely(domain->ops->domain_has_cap == NULL)) |
| 133 | return 0; |
| 134 | |
| 135 | return domain->ops->domain_has_cap(domain, cap); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 138 | |
| 139 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 140 | phys_addr_t paddr, int gfp_order, int prot) |
| 141 | { |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 142 | size_t size; |
| 143 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 144 | if (unlikely(domain->ops->map == NULL)) |
| 145 | return -ENODEV; |
| 146 | |
Joerg Roedel | 85410340e | 2011-09-06 14:36:17 +0200 | [diff] [blame] | 147 | size = PAGE_SIZE << gfp_order; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 148 | |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 149 | BUG_ON(!IS_ALIGNED(iova | paddr, size)); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 150 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 151 | return domain->ops->map(domain, iova, paddr, gfp_order, prot); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL_GPL(iommu_map); |
| 154 | |
| 155 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) |
| 156 | { |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 157 | size_t size; |
| 158 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 159 | if (unlikely(domain->ops->unmap == NULL)) |
| 160 | return -ENODEV; |
| 161 | |
Joerg Roedel | 85410340e | 2011-09-06 14:36:17 +0200 | [diff] [blame] | 162 | size = PAGE_SIZE << gfp_order; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 163 | |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 164 | BUG_ON(!IS_ALIGNED(iova, size)); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 165 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 166 | return domain->ops->unmap(domain, iova, gfp_order); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 167 | } |
| 168 | EXPORT_SYMBOL_GPL(iommu_unmap); |