blob: 2dda00b7331dd132896bbc0c4bceffde9c8ff12b [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
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -040043/**
44 * iommu_set_fault_handler() - set a fault handler for an iommu domain
45 * @domain: iommu domain
46 * @handler: fault handler
47 */
48void iommu_set_fault_handler(struct iommu_domain *domain,
49 iommu_fault_handler_t handler)
50{
51 BUG_ON(!domain);
52
53 domain->handler = handler;
54}
55
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070056struct iommu_domain *iommu_domain_alloc(int flags)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010057{
58 struct iommu_domain *domain;
59 int ret;
60
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070061 if (!iommu_found())
62 return NULL;
63
Joerg Roedelfc2100e2008-11-26 17:21:24 +010064 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
65 if (!domain)
66 return NULL;
67
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070068 ret = iommu_ops->domain_init(domain, flags);
Joerg Roedelfc2100e2008-11-26 17:21:24 +010069 if (ret)
70 goto out_free;
71
72 return domain;
73
74out_free:
75 kfree(domain);
76
77 return NULL;
78}
79EXPORT_SYMBOL_GPL(iommu_domain_alloc);
80
81void iommu_domain_free(struct iommu_domain *domain)
82{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070083 if (!iommu_found())
84 return;
85
Joerg Roedelfc2100e2008-11-26 17:21:24 +010086 iommu_ops->domain_destroy(domain);
87 kfree(domain);
88}
89EXPORT_SYMBOL_GPL(iommu_domain_free);
90
91int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
92{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070093 if (!iommu_found())
94 return -ENODEV;
95
Joerg Roedelfc2100e2008-11-26 17:21:24 +010096 return iommu_ops->attach_dev(domain, dev);
97}
98EXPORT_SYMBOL_GPL(iommu_attach_device);
99
100void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
101{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700102 if (!iommu_found())
103 return;
104
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100105 iommu_ops->detach_dev(domain, dev);
106}
107EXPORT_SYMBOL_GPL(iommu_detach_device);
108
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100109phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
110 unsigned long iova)
111{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700112 if (!iommu_found())
113 return 0;
114
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100115 return iommu_ops->iova_to_phys(domain, iova);
116}
117EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800118
119int iommu_domain_has_cap(struct iommu_domain *domain,
120 unsigned long cap)
121{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700122 if (!iommu_found())
123 return -ENODEV;
124
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800125 return iommu_ops->domain_has_cap(domain, cap);
126}
127EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100128
129int iommu_map(struct iommu_domain *domain, unsigned long iova,
130 phys_addr_t paddr, int gfp_order, int prot)
131{
132 unsigned long invalid_mask;
133 size_t size;
134
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700135 if (!iommu_found())
136 return -ENODEV;
137
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100138 size = 0x1000UL << gfp_order;
139 invalid_mask = size - 1;
140
141 BUG_ON((iova | paddr) & invalid_mask);
142
Joerg Roedel12c73892010-01-21 11:50:28 +0100143 return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100144}
145EXPORT_SYMBOL_GPL(iommu_map);
146
147int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
148{
149 unsigned long invalid_mask;
150 size_t size;
151
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700152 if (!iommu_found())
153 return -ENODEV;
154
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100155 size = 0x1000UL << gfp_order;
156 invalid_mask = size - 1;
157
158 BUG_ON(iova & invalid_mask);
159
Joerg Roedel12c73892010-01-21 11:50:28 +0100160 return iommu_ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100161}
162EXPORT_SYMBOL_GPL(iommu_unmap);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700163
164int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
165 struct scatterlist *sg, unsigned int len, int prot)
166{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700167 if (!iommu_found())
168 return -ENODEV;
169
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700170 BUG_ON(iova & (~PAGE_MASK));
171
172 return iommu_ops->map_range(domain, iova, sg, len, prot);
173}
174EXPORT_SYMBOL_GPL(iommu_map_range);
175
176int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
177 unsigned int len)
178{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700179 if (!iommu_found())
180 return -ENODEV;
181
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700182 BUG_ON(iova & (~PAGE_MASK));
183
184 return iommu_ops->unmap_range(domain, iova, len);
185}
186EXPORT_SYMBOL_GPL(iommu_unmap_range);
Shubhraprakash Das4c436f22011-12-02 18:01:57 -0700187
188phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain)
189{
190 if (!iommu_found())
191 return 0;
192
193 return iommu_ops->get_pt_base_addr(domain);
194}
195EXPORT_SYMBOL_GPL(iommu_get_pt_base_addr);