]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/base/iommu.c
iommu-api: Add ->{un}map callbacks to iommu_ops
[net-next-2.6.git] / drivers / base / iommu.c
CommitLineData
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
19#include <linux/bug.h>
20#include <linux/types.h>
60db4027
AM
21#include <linux/module.h>
22#include <linux/slab.h>
fc2100eb
JR
23#include <linux/errno.h>
24#include <linux/iommu.h>
25
26static struct iommu_ops *iommu_ops;
27
28void register_iommu(struct iommu_ops *ops)
29{
30 if (iommu_ops)
31 BUG();
32
33 iommu_ops = ops;
34}
35
ff2c8a41 36bool iommu_found(void)
fc2100eb
JR
37{
38 return iommu_ops != NULL;
39}
40EXPORT_SYMBOL_GPL(iommu_found);
41
42struct iommu_domain *iommu_domain_alloc(void)
43{
44 struct iommu_domain *domain;
45 int ret;
46
47 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
48 if (!domain)
49 return NULL;
50
51 ret = iommu_ops->domain_init(domain);
52 if (ret)
53 goto out_free;
54
55 return domain;
56
57out_free:
58 kfree(domain);
59
60 return NULL;
61}
62EXPORT_SYMBOL_GPL(iommu_domain_alloc);
63
64void iommu_domain_free(struct iommu_domain *domain)
65{
66 iommu_ops->domain_destroy(domain);
67 kfree(domain);
68}
69EXPORT_SYMBOL_GPL(iommu_domain_free);
70
71int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
72{
73 return iommu_ops->attach_dev(domain, dev);
74}
75EXPORT_SYMBOL_GPL(iommu_attach_device);
76
77void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
78{
79 iommu_ops->detach_dev(domain, dev);
80}
81EXPORT_SYMBOL_GPL(iommu_detach_device);
82
83int iommu_map_range(struct iommu_domain *domain, unsigned long iova,
84 phys_addr_t paddr, size_t size, int prot)
85{
4abc14a7 86 return iommu_ops->map_range(domain, iova, paddr, size, prot);
fc2100eb
JR
87}
88EXPORT_SYMBOL_GPL(iommu_map_range);
89
90void iommu_unmap_range(struct iommu_domain *domain, unsigned long iova,
91 size_t size)
92{
4abc14a7 93 iommu_ops->unmap_range(domain, iova, size);
fc2100eb
JR
94}
95EXPORT_SYMBOL_GPL(iommu_unmap_range);
96
97phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
98 unsigned long iova)
99{
100 return iommu_ops->iova_to_phys(domain, iova);
101}
102EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
dbb9fd86
SY
103
104int iommu_domain_has_cap(struct iommu_domain *domain,
105 unsigned long cap)
106{
107 return iommu_ops->domain_has_cap(domain, cap);
108}
109EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
cefc53c7
JR
110
111int iommu_map(struct iommu_domain *domain, unsigned long iova,
112 phys_addr_t paddr, int gfp_order, int prot)
113{
114 unsigned long invalid_mask;
115 size_t size;
116
117 size = 0x1000UL << gfp_order;
118 invalid_mask = size - 1;
119
120 BUG_ON((iova | paddr) & invalid_mask);
121
67651786
JR
122 if (iommu_ops->map)
123 return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
124
cefc53c7
JR
125 return iommu_ops->map_range(domain, iova, paddr, size, prot);
126}
127EXPORT_SYMBOL_GPL(iommu_map);
128
129int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
130{
131 unsigned long invalid_mask;
132 size_t size;
133
134 size = 0x1000UL << gfp_order;
135 invalid_mask = size - 1;
136
137 BUG_ON(iova & invalid_mask);
138
67651786
JR
139 if (iommu_ops->unmap)
140 return iommu_ops->unmap(domain, iova, gfp_order);
141
cefc53c7
JR
142 iommu_ops->unmap_range(domain, iova, size);
143
144 return gfp_order;
145}
146EXPORT_SYMBOL_GPL(iommu_unmap);