blob: 9c35be4b333f1904f6f43a51da2df7dfaa17cb84 [file] [log] [blame]
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001/*
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 Roedel905d66c2011-09-06 16:03:26 +020019#include <linux/device.h>
Ohad Ben-Cohen40998182011-09-02 13:32:32 -040020#include <linux/kernel.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010021#include <linux/bug.h>
22#include <linux/types.h>
Andrew Morton60db4022009-05-06 16:03:07 -070023#include <linux/module.h>
24#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010025#include <linux/errno.h>
26#include <linux/iommu.h>
27
Alex Williamson14604322011-10-21 15:56:05 -040028static ssize_t show_iommu_group(struct device *dev,
29 struct device_attribute *attr, char *buf)
30{
31 unsigned int groupid;
32
33 if (iommu_device_group(dev, &groupid))
34 return 0;
35
36 return sprintf(buf, "%u", groupid);
37}
38static DEVICE_ATTR(iommu_group, S_IRUGO, show_iommu_group, NULL);
39
40static int add_iommu_group(struct device *dev, void *data)
41{
42 unsigned int groupid;
43
44 if (iommu_device_group(dev, &groupid) == 0)
45 return device_create_file(dev, &dev_attr_iommu_group);
46
47 return 0;
48}
49
50static int remove_iommu_group(struct device *dev)
51{
52 unsigned int groupid;
53
54 if (iommu_device_group(dev, &groupid) == 0)
55 device_remove_file(dev, &dev_attr_iommu_group);
56
57 return 0;
58}
59
60static int iommu_device_notifier(struct notifier_block *nb,
61 unsigned long action, void *data)
62{
63 struct device *dev = data;
64
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);
69
70 return 0;
71}
72
73static struct notifier_block iommu_device_nb = {
74 .notifier_call = iommu_device_notifier,
75};
76
Joerg Roedelff217762011-08-26 16:48:26 +020077static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010078{
Alex Williamson14604322011-10-21 15:56:05 -040079 bus_register_notifier(bus, &iommu_device_nb);
80 bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
Joerg Roedelfc2100e2008-11-26 17:21:24 +010081}
82
Joerg Roedelff217762011-08-26 16:48:26 +020083/**
84 * bus_set_iommu - set iommu-callbacks for the bus
85 * @bus: bus.
86 * @ops: the callbacks provided by the iommu-driver
87 *
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
94 * afterwards.
95 */
96int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010097{
Joerg Roedelff217762011-08-26 16:48:26 +020098 if (bus->iommu_ops != NULL)
99 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100100
Joerg Roedelff217762011-08-26 16:48:26 +0200101 bus->iommu_ops = ops;
102
103 /* Do IOMMU specific setup for this bus-type */
104 iommu_bus_init(bus, ops);
105
106 return 0;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100107}
Joerg Roedelff217762011-08-26 16:48:26 +0200108EXPORT_SYMBOL_GPL(bus_set_iommu);
109
Joerg Roedela1b60c12011-09-06 18:46:34 +0200110bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100111{
Joerg Roedel94441c32011-09-06 18:58:54 +0200112 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100113}
Joerg Roedela1b60c12011-09-06 18:46:34 +0200114EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100115
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400116/**
117 * iommu_set_fault_handler() - set a fault handler for an iommu domain
118 * @domain: iommu domain
119 * @handler: fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400120 *
121 * This function should be used by IOMMU users which want to be notified
122 * whenever an IOMMU fault happens.
123 *
124 * The fault handler itself should return 0 on success, and an appropriate
125 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400126 */
127void iommu_set_fault_handler(struct iommu_domain *domain,
128 iommu_fault_handler_t handler)
129{
130 BUG_ON(!domain);
131
132 domain->handler = handler;
133}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -0400134EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400135
Joerg Roedel905d66c2011-09-06 16:03:26 +0200136struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100137{
138 struct iommu_domain *domain;
139 int ret;
140
Joerg Roedel94441c32011-09-06 18:58:54 +0200141 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +0200142 return NULL;
143
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100144 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
145 if (!domain)
146 return NULL;
147
Joerg Roedel94441c32011-09-06 18:58:54 +0200148 domain->ops = bus->iommu_ops;
Joerg Roedel905d66c2011-09-06 16:03:26 +0200149
Joerg Roedel94441c32011-09-06 18:58:54 +0200150 ret = domain->ops->domain_init(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100151 if (ret)
152 goto out_free;
153
154 return domain;
155
156out_free:
157 kfree(domain);
158
159 return NULL;
160}
161EXPORT_SYMBOL_GPL(iommu_domain_alloc);
162
163void iommu_domain_free(struct iommu_domain *domain)
164{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200165 if (likely(domain->ops->domain_destroy != NULL))
166 domain->ops->domain_destroy(domain);
167
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100168 kfree(domain);
169}
170EXPORT_SYMBOL_GPL(iommu_domain_free);
171
172int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
173{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200174 if (unlikely(domain->ops->attach_dev == NULL))
175 return -ENODEV;
176
177 return domain->ops->attach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100178}
179EXPORT_SYMBOL_GPL(iommu_attach_device);
180
181void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
182{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200183 if (unlikely(domain->ops->detach_dev == NULL))
184 return;
185
186 domain->ops->detach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100187}
188EXPORT_SYMBOL_GPL(iommu_detach_device);
189
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100190phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
191 unsigned long iova)
192{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200193 if (unlikely(domain->ops->iova_to_phys == NULL))
194 return 0;
195
196 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100197}
198EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800199
200int iommu_domain_has_cap(struct iommu_domain *domain,
201 unsigned long cap)
202{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200203 if (unlikely(domain->ops->domain_has_cap == NULL))
204 return 0;
205
206 return domain->ops->domain_has_cap(domain, cap);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800207}
208EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100209
210int iommu_map(struct iommu_domain *domain, unsigned long iova,
211 phys_addr_t paddr, int gfp_order, int prot)
212{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100213 size_t size;
214
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200215 if (unlikely(domain->ops->map == NULL))
216 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100217
Joerg Roedel85410340e2011-09-06 14:36:17 +0200218 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100219
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400220 BUG_ON(!IS_ALIGNED(iova | paddr, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100221
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200222 return domain->ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100223}
224EXPORT_SYMBOL_GPL(iommu_map);
225
226int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
227{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100228 size_t size;
229
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200230 if (unlikely(domain->ops->unmap == NULL))
231 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100232
Joerg Roedel85410340e2011-09-06 14:36:17 +0200233 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100234
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400235 BUG_ON(!IS_ALIGNED(iova, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100236
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200237 return domain->ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100238}
239EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -0400240
241int iommu_device_group(struct device *dev, unsigned int *groupid)
242{
243 if (iommu_present(dev->bus) && dev->bus->iommu_ops->device_group)
244 return dev->bus->iommu_ops->device_group(dev, groupid);
245
246 return -ENODEV;
247}
248EXPORT_SYMBOL_GPL(iommu_device_group);