blob: 6507d8b6beb4a1ae77139d833f6800d3e96ef9ad [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
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070048 if (!iommu_found())
49 return NULL;
50
Joerg Roedelfc2100e2008-11-26 17:21:24 +010051 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
52 if (!domain)
53 return NULL;
54
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070055 ret = iommu_ops->domain_init(domain, flags);
Joerg Roedelfc2100e2008-11-26 17:21:24 +010056 if (ret)
57 goto out_free;
58
59 return domain;
60
61out_free:
62 kfree(domain);
63
64 return NULL;
65}
66EXPORT_SYMBOL_GPL(iommu_domain_alloc);
67
68void iommu_domain_free(struct iommu_domain *domain)
69{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070070 if (!iommu_found())
71 return;
72
Joerg Roedelfc2100e2008-11-26 17:21:24 +010073 iommu_ops->domain_destroy(domain);
74 kfree(domain);
75}
76EXPORT_SYMBOL_GPL(iommu_domain_free);
77
78int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
79{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070080 if (!iommu_found())
81 return -ENODEV;
82
Joerg Roedelfc2100e2008-11-26 17:21:24 +010083 return iommu_ops->attach_dev(domain, dev);
84}
85EXPORT_SYMBOL_GPL(iommu_attach_device);
86
87void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
88{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070089 if (!iommu_found())
90 return;
91
Joerg Roedelfc2100e2008-11-26 17:21:24 +010092 iommu_ops->detach_dev(domain, dev);
93}
94EXPORT_SYMBOL_GPL(iommu_detach_device);
95
Joerg Roedelfc2100e2008-11-26 17:21:24 +010096phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
97 unsigned long iova)
98{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070099 if (!iommu_found())
100 return 0;
101
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100102 return iommu_ops->iova_to_phys(domain, iova);
103}
104EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800105
106int iommu_domain_has_cap(struct iommu_domain *domain,
107 unsigned long cap)
108{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700109 if (!iommu_found())
110 return -ENODEV;
111
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800112 return iommu_ops->domain_has_cap(domain, cap);
113}
114EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100115
116int iommu_map(struct iommu_domain *domain, unsigned long iova,
117 phys_addr_t paddr, int gfp_order, int prot)
118{
119 unsigned long invalid_mask;
120 size_t size;
121
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700122 if (!iommu_found())
123 return -ENODEV;
124
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100125 size = 0x1000UL << gfp_order;
126 invalid_mask = size - 1;
127
128 BUG_ON((iova | paddr) & invalid_mask);
129
Joerg Roedel12c73892010-01-21 11:50:28 +0100130 return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100131}
132EXPORT_SYMBOL_GPL(iommu_map);
133
134int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
135{
136 unsigned long invalid_mask;
137 size_t size;
138
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700139 if (!iommu_found())
140 return -ENODEV;
141
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100142 size = 0x1000UL << gfp_order;
143 invalid_mask = size - 1;
144
145 BUG_ON(iova & invalid_mask);
146
Joerg Roedel12c73892010-01-21 11:50:28 +0100147 return iommu_ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100148}
149EXPORT_SYMBOL_GPL(iommu_unmap);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700150
151int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
152 struct scatterlist *sg, unsigned int len, int prot)
153{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700154 if (!iommu_found())
155 return -ENODEV;
156
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700157 BUG_ON(iova & (~PAGE_MASK));
158
159 return iommu_ops->map_range(domain, iova, sg, len, prot);
160}
161EXPORT_SYMBOL_GPL(iommu_map_range);
162
163int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
164 unsigned int len)
165{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700166 if (!iommu_found())
167 return -ENODEV;
168
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700169 BUG_ON(iova & (~PAGE_MASK));
170
171 return iommu_ops->unmap_range(domain, iova, len);
172}
173EXPORT_SYMBOL_GPL(iommu_unmap_range);