OSDN Git Service

iommu: Fix compile error with !IOMMU_API
[android-x86/kernel.git] / drivers / iommu / iommu.c
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 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 }
38 static DEVICE_ATTR(iommu_group, S_IRUGO, show_iommu_group, NULL);
39
40 static 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
50 static 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
60 static 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
73 static struct notifier_block iommu_device_nb = {
74         .notifier_call = iommu_device_notifier,
75 };
76
77 static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
78 {
79         bus_register_notifier(bus, &iommu_device_nb);
80         bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
81 }
82
83 /**
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  */
96 int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
97 {
98         if (bus->iommu_ops != NULL)
99                 return -EBUSY;
100
101         bus->iommu_ops = ops;
102
103         /* Do IOMMU specific setup for this bus-type */
104         iommu_bus_init(bus, ops);
105
106         return 0;
107 }
108 EXPORT_SYMBOL_GPL(bus_set_iommu);
109
110 bool iommu_present(struct bus_type *bus)
111 {
112         return bus->iommu_ops != NULL;
113 }
114 EXPORT_SYMBOL_GPL(iommu_present);
115
116 /**
117  * iommu_set_fault_handler() - set a fault handler for an iommu domain
118  * @domain: iommu domain
119  * @handler: fault handler
120  *
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.
126  */
127 void 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 }
134 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
135
136 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
137 {
138         struct iommu_domain *domain;
139         int ret;
140
141         if (bus == NULL || bus->iommu_ops == NULL)
142                 return NULL;
143
144         domain = kmalloc(sizeof(*domain), GFP_KERNEL);
145         if (!domain)
146                 return NULL;
147
148         domain->ops = bus->iommu_ops;
149
150         ret = domain->ops->domain_init(domain);
151         if (ret)
152                 goto out_free;
153
154         return domain;
155
156 out_free:
157         kfree(domain);
158
159         return NULL;
160 }
161 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
162
163 void iommu_domain_free(struct iommu_domain *domain)
164 {
165         if (likely(domain->ops->domain_destroy != NULL))
166                 domain->ops->domain_destroy(domain);
167
168         kfree(domain);
169 }
170 EXPORT_SYMBOL_GPL(iommu_domain_free);
171
172 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
173 {
174         if (unlikely(domain->ops->attach_dev == NULL))
175                 return -ENODEV;
176
177         return domain->ops->attach_dev(domain, dev);
178 }
179 EXPORT_SYMBOL_GPL(iommu_attach_device);
180
181 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
182 {
183         if (unlikely(domain->ops->detach_dev == NULL))
184                 return;
185
186         domain->ops->detach_dev(domain, dev);
187 }
188 EXPORT_SYMBOL_GPL(iommu_detach_device);
189
190 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
191                                unsigned long iova)
192 {
193         if (unlikely(domain->ops->iova_to_phys == NULL))
194                 return 0;
195
196         return domain->ops->iova_to_phys(domain, iova);
197 }
198 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
199
200 int iommu_domain_has_cap(struct iommu_domain *domain,
201                          unsigned long cap)
202 {
203         if (unlikely(domain->ops->domain_has_cap == NULL))
204                 return 0;
205
206         return domain->ops->domain_has_cap(domain, cap);
207 }
208 EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
209
210 int iommu_map(struct iommu_domain *domain, unsigned long iova,
211               phys_addr_t paddr, int gfp_order, int prot)
212 {
213         size_t size;
214
215         if (unlikely(domain->ops->map == NULL))
216                 return -ENODEV;
217
218         size         = PAGE_SIZE << gfp_order;
219
220         BUG_ON(!IS_ALIGNED(iova | paddr, size));
221
222         return domain->ops->map(domain, iova, paddr, gfp_order, prot);
223 }
224 EXPORT_SYMBOL_GPL(iommu_map);
225
226 int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
227 {
228         size_t size;
229
230         if (unlikely(domain->ops->unmap == NULL))
231                 return -ENODEV;
232
233         size         = PAGE_SIZE << gfp_order;
234
235         BUG_ON(!IS_ALIGNED(iova, size));
236
237         return domain->ops->unmap(domain, iova, gfp_order);
238 }
239 EXPORT_SYMBOL_GPL(iommu_unmap);
240
241 int 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 }
248 EXPORT_SYMBOL_GPL(iommu_device_group);