2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
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.
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.
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
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>
28 static ssize_t show_iommu_group(struct device *dev,
29 struct device_attribute *attr, char *buf)
33 if (iommu_device_group(dev, &groupid))
36 return sprintf(buf, "%u", groupid);
38 static DEVICE_ATTR(iommu_group, S_IRUGO, show_iommu_group, NULL);
40 static int add_iommu_group(struct device *dev, void *data)
44 if (iommu_device_group(dev, &groupid) == 0)
45 return device_create_file(dev, &dev_attr_iommu_group);
50 static int remove_iommu_group(struct device *dev)
54 if (iommu_device_group(dev, &groupid) == 0)
55 device_remove_file(dev, &dev_attr_iommu_group);
60 static int iommu_device_notifier(struct notifier_block *nb,
61 unsigned long action, void *data)
63 struct device *dev = data;
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);
73 static struct notifier_block iommu_device_nb = {
74 .notifier_call = iommu_device_notifier,
77 static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
79 bus_register_notifier(bus, &iommu_device_nb);
80 bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
84 * bus_set_iommu - set iommu-callbacks for the bus
86 * @ops: the callbacks provided by the iommu-driver
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
96 int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
98 if (bus->iommu_ops != NULL)
101 bus->iommu_ops = ops;
103 /* Do IOMMU specific setup for this bus-type */
104 iommu_bus_init(bus, ops);
108 EXPORT_SYMBOL_GPL(bus_set_iommu);
110 bool iommu_present(struct bus_type *bus)
112 return bus->iommu_ops != NULL;
114 EXPORT_SYMBOL_GPL(iommu_present);
117 * iommu_set_fault_handler() - set a fault handler for an iommu domain
118 * @domain: iommu domain
119 * @handler: fault handler
121 * This function should be used by IOMMU users which want to be notified
122 * whenever an IOMMU fault happens.
124 * The fault handler itself should return 0 on success, and an appropriate
125 * error code otherwise.
127 void iommu_set_fault_handler(struct iommu_domain *domain,
128 iommu_fault_handler_t handler)
132 domain->handler = handler;
134 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
136 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
138 struct iommu_domain *domain;
141 if (bus == NULL || bus->iommu_ops == NULL)
144 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
148 domain->ops = bus->iommu_ops;
150 ret = domain->ops->domain_init(domain);
161 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
163 void iommu_domain_free(struct iommu_domain *domain)
165 if (likely(domain->ops->domain_destroy != NULL))
166 domain->ops->domain_destroy(domain);
170 EXPORT_SYMBOL_GPL(iommu_domain_free);
172 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
174 if (unlikely(domain->ops->attach_dev == NULL))
177 return domain->ops->attach_dev(domain, dev);
179 EXPORT_SYMBOL_GPL(iommu_attach_device);
181 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
183 if (unlikely(domain->ops->detach_dev == NULL))
186 domain->ops->detach_dev(domain, dev);
188 EXPORT_SYMBOL_GPL(iommu_detach_device);
190 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
193 if (unlikely(domain->ops->iova_to_phys == NULL))
196 return domain->ops->iova_to_phys(domain, iova);
198 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
200 int iommu_domain_has_cap(struct iommu_domain *domain,
203 if (unlikely(domain->ops->domain_has_cap == NULL))
206 return domain->ops->domain_has_cap(domain, cap);
208 EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
210 int iommu_map(struct iommu_domain *domain, unsigned long iova,
211 phys_addr_t paddr, int gfp_order, int prot)
215 if (unlikely(domain->ops->map == NULL))
218 size = PAGE_SIZE << gfp_order;
220 BUG_ON(!IS_ALIGNED(iova | paddr, size));
222 return domain->ops->map(domain, iova, paddr, gfp_order, prot);
224 EXPORT_SYMBOL_GPL(iommu_map);
226 int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
230 if (unlikely(domain->ops->unmap == NULL))
233 size = PAGE_SIZE << gfp_order;
235 BUG_ON(!IS_ALIGNED(iova, size));
237 return domain->ops->unmap(domain, iova, gfp_order);
239 EXPORT_SYMBOL_GPL(iommu_unmap);
241 int iommu_device_group(struct device *dev, unsigned int *groupid)
243 if (iommu_present(dev->bus) && dev->bus->iommu_ops->device_group)
244 return dev->bus->iommu_ops->device_group(dev, groupid);
248 EXPORT_SYMBOL_GPL(iommu_device_group);