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 #include <linux/device.h> 20 #include <linux/kernel.h> 21 #include <linux/bug.h> 22 #include <linux/types.h> 23 #include <linux/module.h> 24 #include <linux/slab.h> 25 #include <linux/errno.h> 26 #include <linux/iommu.h> 27 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 59 bool iommu_present(struct bus_type *bus) 60 { 61 return bus->iommu_ops != NULL; 62 } 63 EXPORT_SYMBOL_GPL(iommu_present); 64 65 /** 66 * iommu_set_fault_handler() - set a fault handler for an iommu domain 67 * @domain: iommu domain 68 * @handler: fault handler 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. 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 } 83 EXPORT_SYMBOL_GPL(iommu_set_fault_handler); 84 85 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) 86 { 87 struct iommu_domain *domain; 88 int ret; 89 90 if (bus == NULL || bus->iommu_ops == NULL) 91 return NULL; 92 93 domain = kmalloc(sizeof(*domain), GFP_KERNEL); 94 if (!domain) 95 return NULL; 96 97 domain->ops = bus->iommu_ops; 98 99 ret = domain->ops->domain_init(domain); 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 { 114 if (likely(domain->ops->domain_destroy != NULL)) 115 domain->ops->domain_destroy(domain); 116 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 { 123 if (unlikely(domain->ops->attach_dev == NULL)) 124 return -ENODEV; 125 126 return domain->ops->attach_dev(domain, dev); 127 } 128 EXPORT_SYMBOL_GPL(iommu_attach_device); 129 130 void iommu_detach_device(struct iommu_domain *domain, struct device *dev) 131 { 132 if (unlikely(domain->ops->detach_dev == NULL)) 133 return; 134 135 domain->ops->detach_dev(domain, dev); 136 } 137 EXPORT_SYMBOL_GPL(iommu_detach_device); 138 139 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, 140 unsigned long iova) 141 { 142 if (unlikely(domain->ops->iova_to_phys == NULL)) 143 return 0; 144 145 return domain->ops->iova_to_phys(domain, iova); 146 } 147 EXPORT_SYMBOL_GPL(iommu_iova_to_phys); 148 149 int iommu_domain_has_cap(struct iommu_domain *domain, 150 unsigned long cap) 151 { 152 if (unlikely(domain->ops->domain_has_cap == NULL)) 153 return 0; 154 155 return domain->ops->domain_has_cap(domain, cap); 156 } 157 EXPORT_SYMBOL_GPL(iommu_domain_has_cap); 158 159 int iommu_map(struct iommu_domain *domain, unsigned long iova, 160 phys_addr_t paddr, int gfp_order, int prot) 161 { 162 size_t size; 163 164 if (unlikely(domain->ops->map == NULL)) 165 return -ENODEV; 166 167 size = PAGE_SIZE << gfp_order; 168 169 BUG_ON(!IS_ALIGNED(iova | paddr, size)); 170 171 return domain->ops->map(domain, iova, paddr, gfp_order, prot); 172 } 173 EXPORT_SYMBOL_GPL(iommu_map); 174 175 int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) 176 { 177 size_t size; 178 179 if (unlikely(domain->ops->unmap == NULL)) 180 return -ENODEV; 181 182 size = PAGE_SIZE << gfp_order; 183 184 BUG_ON(!IS_ALIGNED(iova, size)); 185 186 return domain->ops->unmap(domain, iova, gfp_order); 187 } 188 EXPORT_SYMBOL_GPL(iommu_unmap); 189