]> Pileus Git - ~andy/linux/blob - drivers/iommu/iommu.c
Merge branches 'iommu/fixes', 'arm/omap' and 'x86/amd' into next
[~andy/linux] / 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 #define pr_fmt(fmt)    "%s: " fmt, __func__
20
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/bug.h>
24 #include <linux/types.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/iommu.h>
29
30 static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
31 {
32 }
33
34 /**
35  * bus_set_iommu - set iommu-callbacks for the bus
36  * @bus: bus.
37  * @ops: the callbacks provided by the iommu-driver
38  *
39  * This function is called by an iommu driver to set the iommu methods
40  * used for a particular bus. Drivers for devices on that bus can use
41  * the iommu-api after these ops are registered.
42  * This special function is needed because IOMMUs are usually devices on
43  * the bus itself, so the iommu drivers are not initialized when the bus
44  * is set up. With this function the iommu-driver can set the iommu-ops
45  * afterwards.
46  */
47 int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
48 {
49         if (bus->iommu_ops != NULL)
50                 return -EBUSY;
51
52         bus->iommu_ops = ops;
53
54         /* Do IOMMU specific setup for this bus-type */
55         iommu_bus_init(bus, ops);
56
57         return 0;
58 }
59 EXPORT_SYMBOL_GPL(bus_set_iommu);
60
61 bool iommu_present(struct bus_type *bus)
62 {
63         return bus->iommu_ops != NULL;
64 }
65 EXPORT_SYMBOL_GPL(iommu_present);
66
67 /**
68  * iommu_set_fault_handler() - set a fault handler for an iommu domain
69  * @domain: iommu domain
70  * @handler: fault handler
71  *
72  * This function should be used by IOMMU users which want to be notified
73  * whenever an IOMMU fault happens.
74  *
75  * The fault handler itself should return 0 on success, and an appropriate
76  * error code otherwise.
77  */
78 void iommu_set_fault_handler(struct iommu_domain *domain,
79                                         iommu_fault_handler_t handler)
80 {
81         BUG_ON(!domain);
82
83         domain->handler = handler;
84 }
85 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
86
87 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
88 {
89         struct iommu_domain *domain;
90         int ret;
91
92         if (bus == NULL || bus->iommu_ops == NULL)
93                 return NULL;
94
95         domain = kzalloc(sizeof(*domain), GFP_KERNEL);
96         if (!domain)
97                 return NULL;
98
99         domain->ops = bus->iommu_ops;
100
101         ret = domain->ops->domain_init(domain);
102         if (ret)
103                 goto out_free;
104
105         return domain;
106
107 out_free:
108         kfree(domain);
109
110         return NULL;
111 }
112 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
113
114 void iommu_domain_free(struct iommu_domain *domain)
115 {
116         if (likely(domain->ops->domain_destroy != NULL))
117                 domain->ops->domain_destroy(domain);
118
119         kfree(domain);
120 }
121 EXPORT_SYMBOL_GPL(iommu_domain_free);
122
123 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
124 {
125         if (unlikely(domain->ops->attach_dev == NULL))
126                 return -ENODEV;
127
128         return domain->ops->attach_dev(domain, dev);
129 }
130 EXPORT_SYMBOL_GPL(iommu_attach_device);
131
132 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
133 {
134         if (unlikely(domain->ops->detach_dev == NULL))
135                 return;
136
137         domain->ops->detach_dev(domain, dev);
138 }
139 EXPORT_SYMBOL_GPL(iommu_detach_device);
140
141 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
142                                unsigned long iova)
143 {
144         if (unlikely(domain->ops->iova_to_phys == NULL))
145                 return 0;
146
147         return domain->ops->iova_to_phys(domain, iova);
148 }
149 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
150
151 int iommu_domain_has_cap(struct iommu_domain *domain,
152                          unsigned long cap)
153 {
154         if (unlikely(domain->ops->domain_has_cap == NULL))
155                 return 0;
156
157         return domain->ops->domain_has_cap(domain, cap);
158 }
159 EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
160
161 int iommu_map(struct iommu_domain *domain, unsigned long iova,
162               phys_addr_t paddr, size_t size, int prot)
163 {
164         unsigned long orig_iova = iova;
165         unsigned int min_pagesz;
166         size_t orig_size = size;
167         int ret = 0;
168
169         if (unlikely(domain->ops->map == NULL))
170                 return -ENODEV;
171
172         /* find out the minimum page size supported */
173         min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
174
175         /*
176          * both the virtual address and the physical one, as well as
177          * the size of the mapping, must be aligned (at least) to the
178          * size of the smallest page supported by the hardware
179          */
180         if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
181                 pr_err("unaligned: iova 0x%lx pa 0x%lx size 0x%lx min_pagesz "
182                         "0x%x\n", iova, (unsigned long)paddr,
183                         (unsigned long)size, min_pagesz);
184                 return -EINVAL;
185         }
186
187         pr_debug("map: iova 0x%lx pa 0x%lx size 0x%lx\n", iova,
188                                 (unsigned long)paddr, (unsigned long)size);
189
190         while (size) {
191                 unsigned long pgsize, addr_merge = iova | paddr;
192                 unsigned int pgsize_idx;
193
194                 /* Max page size that still fits into 'size' */
195                 pgsize_idx = __fls(size);
196
197                 /* need to consider alignment requirements ? */
198                 if (likely(addr_merge)) {
199                         /* Max page size allowed by both iova and paddr */
200                         unsigned int align_pgsize_idx = __ffs(addr_merge);
201
202                         pgsize_idx = min(pgsize_idx, align_pgsize_idx);
203                 }
204
205                 /* build a mask of acceptable page sizes */
206                 pgsize = (1UL << (pgsize_idx + 1)) - 1;
207
208                 /* throw away page sizes not supported by the hardware */
209                 pgsize &= domain->ops->pgsize_bitmap;
210
211                 /* make sure we're still sane */
212                 BUG_ON(!pgsize);
213
214                 /* pick the biggest page */
215                 pgsize_idx = __fls(pgsize);
216                 pgsize = 1UL << pgsize_idx;
217
218                 pr_debug("mapping: iova 0x%lx pa 0x%lx pgsize %lu\n", iova,
219                                         (unsigned long)paddr, pgsize);
220
221                 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
222                 if (ret)
223                         break;
224
225                 iova += pgsize;
226                 paddr += pgsize;
227                 size -= pgsize;
228         }
229
230         /* unroll mapping in case something went wrong */
231         if (ret)
232                 iommu_unmap(domain, orig_iova, orig_size - size);
233
234         return ret;
235 }
236 EXPORT_SYMBOL_GPL(iommu_map);
237
238 size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
239 {
240         size_t unmapped_page, unmapped = 0;
241         unsigned int min_pagesz;
242
243         if (unlikely(domain->ops->unmap == NULL))
244                 return -ENODEV;
245
246         /* find out the minimum page size supported */
247         min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
248
249         /*
250          * The virtual address, as well as the size of the mapping, must be
251          * aligned (at least) to the size of the smallest page supported
252          * by the hardware
253          */
254         if (!IS_ALIGNED(iova | size, min_pagesz)) {
255                 pr_err("unaligned: iova 0x%lx size 0x%lx min_pagesz 0x%x\n",
256                                         iova, (unsigned long)size, min_pagesz);
257                 return -EINVAL;
258         }
259
260         pr_debug("unmap this: iova 0x%lx size 0x%lx\n", iova,
261                                                         (unsigned long)size);
262
263         /*
264          * Keep iterating until we either unmap 'size' bytes (or more)
265          * or we hit an area that isn't mapped.
266          */
267         while (unmapped < size) {
268                 size_t left = size - unmapped;
269
270                 unmapped_page = domain->ops->unmap(domain, iova, left);
271                 if (!unmapped_page)
272                         break;
273
274                 pr_debug("unmapped: iova 0x%lx size %lx\n", iova,
275                                         (unsigned long)unmapped_page);
276
277                 iova += unmapped_page;
278                 unmapped += unmapped_page;
279         }
280
281         return unmapped;
282 }
283 EXPORT_SYMBOL_GPL(iommu_unmap);