2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
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.
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.
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
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>
28 static void iommu_bus_init(struct bus_type
*bus
, struct iommu_ops
*ops
)
33 * bus_set_iommu - set iommu-callbacks for the bus
35 * @ops: the callbacks provided by the iommu-driver
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
45 int bus_set_iommu(struct bus_type
*bus
, struct iommu_ops
*ops
)
47 if (bus
->iommu_ops
!= NULL
)
52 /* Do IOMMU specific setup for this bus-type */
53 iommu_bus_init(bus
, ops
);
57 EXPORT_SYMBOL_GPL(bus_set_iommu
);
59 bool iommu_present(struct bus_type
*bus
)
61 return bus
->iommu_ops
!= NULL
;
63 EXPORT_SYMBOL_GPL(iommu_present
);
66 * iommu_set_fault_handler() - set a fault handler for an iommu domain
67 * @domain: iommu domain
68 * @handler: fault handler
70 * This function should be used by IOMMU users which want to be notified
71 * whenever an IOMMU fault happens.
73 * The fault handler itself should return 0 on success, and an appropriate
74 * error code otherwise.
76 void iommu_set_fault_handler(struct iommu_domain
*domain
,
77 iommu_fault_handler_t handler
)
81 domain
->handler
= handler
;
83 EXPORT_SYMBOL_GPL(iommu_set_fault_handler
);
85 struct iommu_domain
*iommu_domain_alloc(struct bus_type
*bus
)
87 struct iommu_domain
*domain
;
90 if (bus
== NULL
|| bus
->iommu_ops
== NULL
)
93 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
97 domain
->ops
= bus
->iommu_ops
;
99 ret
= domain
->ops
->domain_init(domain
);
110 EXPORT_SYMBOL_GPL(iommu_domain_alloc
);
112 void iommu_domain_free(struct iommu_domain
*domain
)
114 if (likely(domain
->ops
->domain_destroy
!= NULL
))
115 domain
->ops
->domain_destroy(domain
);
119 EXPORT_SYMBOL_GPL(iommu_domain_free
);
121 int iommu_attach_device(struct iommu_domain
*domain
, struct device
*dev
)
123 if (unlikely(domain
->ops
->attach_dev
== NULL
))
126 return domain
->ops
->attach_dev(domain
, dev
);
128 EXPORT_SYMBOL_GPL(iommu_attach_device
);
130 void iommu_detach_device(struct iommu_domain
*domain
, struct device
*dev
)
132 if (unlikely(domain
->ops
->detach_dev
== NULL
))
135 domain
->ops
->detach_dev(domain
, dev
);
137 EXPORT_SYMBOL_GPL(iommu_detach_device
);
139 phys_addr_t
iommu_iova_to_phys(struct iommu_domain
*domain
,
142 if (unlikely(domain
->ops
->iova_to_phys
== NULL
))
145 return domain
->ops
->iova_to_phys(domain
, iova
);
147 EXPORT_SYMBOL_GPL(iommu_iova_to_phys
);
149 int iommu_domain_has_cap(struct iommu_domain
*domain
,
152 if (unlikely(domain
->ops
->domain_has_cap
== NULL
))
155 return domain
->ops
->domain_has_cap(domain
, cap
);
157 EXPORT_SYMBOL_GPL(iommu_domain_has_cap
);
159 int iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
160 phys_addr_t paddr
, int gfp_order
, int prot
)
164 if (unlikely(domain
->ops
->map
== NULL
))
167 size
= PAGE_SIZE
<< gfp_order
;
169 BUG_ON(!IS_ALIGNED(iova
| paddr
, size
));
171 return domain
->ops
->map(domain
, iova
, paddr
, gfp_order
, prot
);
173 EXPORT_SYMBOL_GPL(iommu_map
);
175 int iommu_unmap(struct iommu_domain
*domain
, unsigned long iova
, int gfp_order
)
179 if (unlikely(domain
->ops
->unmap
== NULL
))
182 size
= PAGE_SIZE
<< gfp_order
;
184 BUG_ON(!IS_ALIGNED(iova
, size
));
186 return domain
->ops
->unmap(domain
, iova
, gfp_order
);
188 EXPORT_SYMBOL_GPL(iommu_unmap
);