]>
Commit | Line | Data |
---|---|---|
fc2100eb JR |
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 | ||
905d66c1 | 19 | #include <linux/device.h> |
40998188 | 20 | #include <linux/kernel.h> |
fc2100eb JR |
21 | #include <linux/bug.h> |
22 | #include <linux/types.h> | |
60db4027 AM |
23 | #include <linux/module.h> |
24 | #include <linux/slab.h> | |
fc2100eb JR |
25 | #include <linux/errno.h> |
26 | #include <linux/iommu.h> | |
27 | ||
ff21776d JR |
28 | static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops) |
29 | { | |
30 | } | |
fc2100eb | 31 | |
ff21776d JR |
32 | /** |
33 | * bus_set_iommu - set iommu-callbacks for the bus | |
34 | * @bus: bus. | |
35 | * @ops: the callbacks provided by the iommu-driver | |
36 | * | |
37 | * This function is called by an iommu driver to set the iommu methods | |
38 | * used for a particular bus. Drivers for devices on that bus can use | |
39 | * the iommu-api after these ops are registered. | |
40 | * This special function is needed because IOMMUs are usually devices on | |
41 | * the bus itself, so the iommu drivers are not initialized when the bus | |
42 | * is set up. With this function the iommu-driver can set the iommu-ops | |
43 | * afterwards. | |
44 | */ | |
45 | int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops) | |
fc2100eb | 46 | { |
ff21776d JR |
47 | if (bus->iommu_ops != NULL) |
48 | return -EBUSY; | |
fc2100eb | 49 | |
ff21776d JR |
50 | bus->iommu_ops = ops; |
51 | ||
52 | /* Do IOMMU specific setup for this bus-type */ | |
53 | iommu_bus_init(bus, ops); | |
fc2100eb | 54 | |
ff21776d | 55 | return 0; |
fc2100eb | 56 | } |
ff21776d | 57 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
fc2100eb | 58 | |
a1b60c1c | 59 | bool iommu_present(struct bus_type *bus) |
fc2100eb | 60 | { |
94441c3b | 61 | return bus->iommu_ops != NULL; |
fc2100eb | 62 | } |
a1b60c1c | 63 | EXPORT_SYMBOL_GPL(iommu_present); |
fc2100eb | 64 | |
4f3f8d9d OBC |
65 | /** |
66 | * iommu_set_fault_handler() - set a fault handler for an iommu domain | |
67 | * @domain: iommu domain | |
68 | * @handler: fault handler | |
0ed6d2d2 OBC |
69 | * |
70 | * This function should be used by IOMMU users which want to be notified | |
71 | * whenever an IOMMU fault happens. | |
72 | * | |
73 | * The fault handler itself should return 0 on success, and an appropriate | |
74 | * error code otherwise. | |
4f3f8d9d OBC |
75 | */ |
76 | void iommu_set_fault_handler(struct iommu_domain *domain, | |
77 | iommu_fault_handler_t handler) | |
78 | { | |
79 | BUG_ON(!domain); | |
80 | ||
81 | domain->handler = handler; | |
82 | } | |
30bd918c | 83 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
4f3f8d9d | 84 | |
905d66c1 | 85 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
fc2100eb JR |
86 | { |
87 | struct iommu_domain *domain; | |
88 | int ret; | |
89 | ||
94441c3b | 90 | if (bus == NULL || bus->iommu_ops == NULL) |
905d66c1 JR |
91 | return NULL; |
92 | ||
fc2100eb JR |
93 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); |
94 | if (!domain) | |
95 | return NULL; | |
96 | ||
94441c3b | 97 | domain->ops = bus->iommu_ops; |
905d66c1 | 98 | |
94441c3b | 99 | ret = domain->ops->domain_init(domain); |
fc2100eb JR |
100 | if (ret) |
101 | goto out_free; | |
102 | ||
103 | return domain; | |
104 | ||
105 | out_free: | |
106 | kfree(domain); | |
107 | ||
108 | return NULL; | |
109 | } | |
110 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); | |
111 | ||
112 | void iommu_domain_free(struct iommu_domain *domain) | |
113 | { | |
e5aa7f00 JR |
114 | if (likely(domain->ops->domain_destroy != NULL)) |
115 | domain->ops->domain_destroy(domain); | |
116 | ||
fc2100eb JR |
117 | kfree(domain); |
118 | } | |
119 | EXPORT_SYMBOL_GPL(iommu_domain_free); | |
120 | ||
121 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) | |
122 | { | |
e5aa7f00 JR |
123 | if (unlikely(domain->ops->attach_dev == NULL)) |
124 | return -ENODEV; | |
125 | ||
126 | return domain->ops->attach_dev(domain, dev); | |
fc2100eb JR |
127 | } |
128 | EXPORT_SYMBOL_GPL(iommu_attach_device); | |
129 | ||
130 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) | |
131 | { | |
e5aa7f00 JR |
132 | if (unlikely(domain->ops->detach_dev == NULL)) |
133 | return; | |
134 | ||
135 | domain->ops->detach_dev(domain, dev); | |
fc2100eb JR |
136 | } |
137 | EXPORT_SYMBOL_GPL(iommu_detach_device); | |
138 | ||
fc2100eb JR |
139 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
140 | unsigned long iova) | |
141 | { | |
e5aa7f00 JR |
142 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
143 | return 0; | |
144 | ||
145 | return domain->ops->iova_to_phys(domain, iova); | |
fc2100eb JR |
146 | } |
147 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); | |
dbb9fd86 SY |
148 | |
149 | int iommu_domain_has_cap(struct iommu_domain *domain, | |
150 | unsigned long cap) | |
151 | { | |
e5aa7f00 JR |
152 | if (unlikely(domain->ops->domain_has_cap == NULL)) |
153 | return 0; | |
154 | ||
155 | return domain->ops->domain_has_cap(domain, cap); | |
dbb9fd86 SY |
156 | } |
157 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); | |
cefc53c7 JR |
158 | |
159 | int iommu_map(struct iommu_domain *domain, unsigned long iova, | |
160 | phys_addr_t paddr, int gfp_order, int prot) | |
161 | { | |
cefc53c7 JR |
162 | size_t size; |
163 | ||
e5aa7f00 JR |
164 | if (unlikely(domain->ops->map == NULL)) |
165 | return -ENODEV; | |
cefc53c7 | 166 | |
85410340 | 167 | size = PAGE_SIZE << gfp_order; |
cefc53c7 | 168 | |
40998188 | 169 | BUG_ON(!IS_ALIGNED(iova | paddr, size)); |
cefc53c7 | 170 | |
5009065d | 171 | return domain->ops->map(domain, iova, paddr, size, prot); |
cefc53c7 JR |
172 | } |
173 | EXPORT_SYMBOL_GPL(iommu_map); | |
174 | ||
175 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) | |
176 | { | |
5009065d | 177 | size_t size, unmapped; |
cefc53c7 | 178 | |
e5aa7f00 JR |
179 | if (unlikely(domain->ops->unmap == NULL)) |
180 | return -ENODEV; | |
181 | ||
85410340 | 182 | size = PAGE_SIZE << gfp_order; |
cefc53c7 | 183 | |
40998188 | 184 | BUG_ON(!IS_ALIGNED(iova, size)); |
cefc53c7 | 185 | |
5009065d OBC |
186 | unmapped = domain->ops->unmap(domain, iova, size); |
187 | ||
188 | return get_order(unmapped); | |
cefc53c7 JR |
189 | } |
190 | EXPORT_SYMBOL_GPL(iommu_unmap); |