]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/iommu/iommu.c
46e1c24f2f4362c7a40513cf35ed3c73853c5a31
[mv-sheeva.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 struct iommu_ops *iommu_ops;
29
30 void register_iommu(struct iommu_ops *ops)
31 {
32         if (iommu_ops)
33                 BUG();
34
35         iommu_ops = ops;
36 }
37
38 static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
39 {
40 }
41
42 /**
43  * bus_set_iommu - set iommu-callbacks for the bus
44  * @bus: bus.
45  * @ops: the callbacks provided by the iommu-driver
46  *
47  * This function is called by an iommu driver to set the iommu methods
48  * used for a particular bus. Drivers for devices on that bus can use
49  * the iommu-api after these ops are registered.
50  * This special function is needed because IOMMUs are usually devices on
51  * the bus itself, so the iommu drivers are not initialized when the bus
52  * is set up. With this function the iommu-driver can set the iommu-ops
53  * afterwards.
54  */
55 int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
56 {
57         if (bus->iommu_ops != NULL)
58                 return -EBUSY;
59
60         bus->iommu_ops = ops;
61
62         /* Do IOMMU specific setup for this bus-type */
63         iommu_bus_init(bus, ops);
64
65         return 0;
66 }
67 EXPORT_SYMBOL_GPL(bus_set_iommu);
68
69 bool iommu_found(void)
70 {
71         return iommu_ops != NULL;
72 }
73 EXPORT_SYMBOL_GPL(iommu_found);
74
75 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
76 {
77         struct iommu_domain *domain;
78         struct iommu_ops *ops;
79         int ret;
80
81         if (bus->iommu_ops)
82                 ops = bus->iommu_ops;
83         else
84                 ops = iommu_ops;
85
86         if (ops == NULL)
87                 return NULL;
88
89         domain = kmalloc(sizeof(*domain), GFP_KERNEL);
90         if (!domain)
91                 return NULL;
92
93         domain->ops = ops;
94
95         ret = iommu_ops->domain_init(domain);
96         if (ret)
97                 goto out_free;
98
99         return domain;
100
101 out_free:
102         kfree(domain);
103
104         return NULL;
105 }
106 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
107
108 void iommu_domain_free(struct iommu_domain *domain)
109 {
110         iommu_ops->domain_destroy(domain);
111         kfree(domain);
112 }
113 EXPORT_SYMBOL_GPL(iommu_domain_free);
114
115 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
116 {
117         return iommu_ops->attach_dev(domain, dev);
118 }
119 EXPORT_SYMBOL_GPL(iommu_attach_device);
120
121 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
122 {
123         iommu_ops->detach_dev(domain, dev);
124 }
125 EXPORT_SYMBOL_GPL(iommu_detach_device);
126
127 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
128                                unsigned long iova)
129 {
130         return iommu_ops->iova_to_phys(domain, iova);
131 }
132 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
133
134 int iommu_domain_has_cap(struct iommu_domain *domain,
135                          unsigned long cap)
136 {
137         return iommu_ops->domain_has_cap(domain, cap);
138 }
139 EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
140
141 int iommu_map(struct iommu_domain *domain, unsigned long iova,
142               phys_addr_t paddr, int gfp_order, int prot)
143 {
144         size_t size;
145
146         size         = PAGE_SIZE << gfp_order;
147
148         BUG_ON(!IS_ALIGNED(iova | paddr, size));
149
150         return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
151 }
152 EXPORT_SYMBOL_GPL(iommu_map);
153
154 int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
155 {
156         size_t size;
157
158         size         = PAGE_SIZE << gfp_order;
159
160         BUG_ON(!IS_ALIGNED(iova, size));
161
162         return iommu_ops->unmap(domain, iova, gfp_order);
163 }
164 EXPORT_SYMBOL_GPL(iommu_unmap);