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 | ||
40998188 | 19 | #include <linux/kernel.h> |
fc2100eb JR |
20 | #include <linux/bug.h> |
21 | #include <linux/types.h> | |
60db4027 AM |
22 | #include <linux/module.h> |
23 | #include <linux/slab.h> | |
fc2100eb JR |
24 | #include <linux/errno.h> |
25 | #include <linux/iommu.h> | |
26 | ||
27 | static struct iommu_ops *iommu_ops; | |
28 | ||
29 | void register_iommu(struct iommu_ops *ops) | |
30 | { | |
31 | if (iommu_ops) | |
32 | BUG(); | |
33 | ||
34 | iommu_ops = ops; | |
35 | } | |
36 | ||
ff2c8a41 | 37 | bool iommu_found(void) |
fc2100eb JR |
38 | { |
39 | return iommu_ops != NULL; | |
40 | } | |
41 | EXPORT_SYMBOL_GPL(iommu_found); | |
42 | ||
43 | struct iommu_domain *iommu_domain_alloc(void) | |
44 | { | |
45 | struct iommu_domain *domain; | |
46 | int ret; | |
47 | ||
48 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); | |
49 | if (!domain) | |
50 | return NULL; | |
51 | ||
52 | ret = iommu_ops->domain_init(domain); | |
53 | if (ret) | |
54 | goto out_free; | |
55 | ||
56 | return domain; | |
57 | ||
58 | out_free: | |
59 | kfree(domain); | |
60 | ||
61 | return NULL; | |
62 | } | |
63 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); | |
64 | ||
65 | void iommu_domain_free(struct iommu_domain *domain) | |
66 | { | |
67 | iommu_ops->domain_destroy(domain); | |
68 | kfree(domain); | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(iommu_domain_free); | |
71 | ||
72 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) | |
73 | { | |
74 | return iommu_ops->attach_dev(domain, dev); | |
75 | } | |
76 | EXPORT_SYMBOL_GPL(iommu_attach_device); | |
77 | ||
78 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) | |
79 | { | |
80 | iommu_ops->detach_dev(domain, dev); | |
81 | } | |
82 | EXPORT_SYMBOL_GPL(iommu_detach_device); | |
83 | ||
fc2100eb JR |
84 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
85 | unsigned long iova) | |
86 | { | |
87 | return iommu_ops->iova_to_phys(domain, iova); | |
88 | } | |
89 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); | |
dbb9fd86 SY |
90 | |
91 | int iommu_domain_has_cap(struct iommu_domain *domain, | |
92 | unsigned long cap) | |
93 | { | |
94 | return iommu_ops->domain_has_cap(domain, cap); | |
95 | } | |
96 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); | |
cefc53c7 JR |
97 | |
98 | int iommu_map(struct iommu_domain *domain, unsigned long iova, | |
99 | phys_addr_t paddr, int gfp_order, int prot) | |
100 | { | |
cefc53c7 JR |
101 | size_t size; |
102 | ||
85410340 | 103 | size = PAGE_SIZE << gfp_order; |
cefc53c7 | 104 | |
40998188 | 105 | BUG_ON(!IS_ALIGNED(iova | paddr, size)); |
cefc53c7 | 106 | |
12c7389a | 107 | return iommu_ops->map(domain, iova, paddr, gfp_order, prot); |
cefc53c7 JR |
108 | } |
109 | EXPORT_SYMBOL_GPL(iommu_map); | |
110 | ||
111 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) | |
112 | { | |
cefc53c7 JR |
113 | size_t size; |
114 | ||
85410340 | 115 | size = PAGE_SIZE << gfp_order; |
cefc53c7 | 116 | |
40998188 | 117 | BUG_ON(!IS_ALIGNED(iova, size)); |
cefc53c7 | 118 | |
12c7389a | 119 | return iommu_ops->unmap(domain, iova, gfp_order); |
cefc53c7 JR |
120 | } |
121 | EXPORT_SYMBOL_GPL(iommu_unmap); |