blob: 1377e854e05bac94bf791bdc86ae19a41d95a538 [file] [log] [blame]
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001/*
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>
Andrew Morton60db4022009-05-06 16:03:07 -070021#include <linux/module.h>
22#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010023#include <linux/errno.h>
24#include <linux/iommu.h>
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -070025#include <linux/scatterlist.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010026
27static struct iommu_ops *iommu_ops;
28
29void register_iommu(struct iommu_ops *ops)
30{
31 if (iommu_ops)
32 BUG();
33
34 iommu_ops = ops;
35}
36
Hannes Ederff2c8a42009-03-05 12:12:44 +010037bool iommu_found(void)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010038{
39 return iommu_ops != NULL;
40}
41EXPORT_SYMBOL_GPL(iommu_found);
42
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070043struct iommu_domain *iommu_domain_alloc(int flags)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010044{
45 struct iommu_domain *domain;
46 int ret;
47
48 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
49 if (!domain)
50 return NULL;
51
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070052 ret = iommu_ops->domain_init(domain, flags);
Joerg Roedelfc2100e2008-11-26 17:21:24 +010053 if (ret)
54 goto out_free;
55
56 return domain;
57
58out_free:
59 kfree(domain);
60
61 return NULL;
62}
63EXPORT_SYMBOL_GPL(iommu_domain_alloc);
64
65void iommu_domain_free(struct iommu_domain *domain)
66{
67 iommu_ops->domain_destroy(domain);
68 kfree(domain);
69}
70EXPORT_SYMBOL_GPL(iommu_domain_free);
71
72int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
73{
74 return iommu_ops->attach_dev(domain, dev);
75}
76EXPORT_SYMBOL_GPL(iommu_attach_device);
77
78void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
79{
80 iommu_ops->detach_dev(domain, dev);
81}
82EXPORT_SYMBOL_GPL(iommu_detach_device);
83
Joerg Roedelfc2100e2008-11-26 17:21:24 +010084phys_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}
89EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +080090
91int iommu_domain_has_cap(struct iommu_domain *domain,
92 unsigned long cap)
93{
94 return iommu_ops->domain_has_cap(domain, cap);
95}
96EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +010097
98int iommu_map(struct iommu_domain *domain, unsigned long iova,
99 phys_addr_t paddr, int gfp_order, int prot)
100{
101 unsigned long invalid_mask;
102 size_t size;
103
104 size = 0x1000UL << gfp_order;
105 invalid_mask = size - 1;
106
107 BUG_ON((iova | paddr) & invalid_mask);
108
Joerg Roedel12c73892010-01-21 11:50:28 +0100109 return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100110}
111EXPORT_SYMBOL_GPL(iommu_map);
112
113int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
114{
115 unsigned long invalid_mask;
116 size_t size;
117
118 size = 0x1000UL << gfp_order;
119 invalid_mask = size - 1;
120
121 BUG_ON(iova & invalid_mask);
122
Joerg Roedel12c73892010-01-21 11:50:28 +0100123 return iommu_ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100124}
125EXPORT_SYMBOL_GPL(iommu_unmap);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700126
127int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
128 struct scatterlist *sg, unsigned int len, int prot)
129{
130 BUG_ON(iova & (~PAGE_MASK));
131
132 return iommu_ops->map_range(domain, iova, sg, len, prot);
133}
134EXPORT_SYMBOL_GPL(iommu_map_range);
135
136int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
137 unsigned int len)
138{
139 BUG_ON(iova & (~PAGE_MASK));
140
141 return iommu_ops->unmap_range(domain, iova, len);
142}
143EXPORT_SYMBOL_GPL(iommu_unmap_range);