blob: 9d9e4ab40a4e7d93ff4f73997f18dc4504cb4e3b [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
Ohad Ben-Cohen48c02c92011-09-27 07:36:40 -040047 *
48 * This function should be used by IOMMU users which want to be notified
49 * whenever an IOMMU fault happens.
50 *
51 * The fault handler itself should return 0 on success, and an appropriate
52 * error code otherwise.
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -040053 */
54void iommu_set_fault_handler(struct iommu_domain *domain,
55 iommu_fault_handler_t handler)
56{
57 BUG_ON(!domain);
58
59 domain->handler = handler;
60}
Ohad Ben-Cohena4d334f2011-09-26 09:11:46 -040061EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -040062
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070063struct iommu_domain *iommu_domain_alloc(int flags)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010064{
65 struct iommu_domain *domain;
66 int ret;
67
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070068 if (!iommu_found())
69 return NULL;
70
Joerg Roedelfc2100e2008-11-26 17:21:24 +010071 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
72 if (!domain)
73 return NULL;
74
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070075 ret = iommu_ops->domain_init(domain, flags);
Joerg Roedelfc2100e2008-11-26 17:21:24 +010076 if (ret)
77 goto out_free;
78
79 return domain;
80
81out_free:
82 kfree(domain);
83
84 return NULL;
85}
86EXPORT_SYMBOL_GPL(iommu_domain_alloc);
87
88void iommu_domain_free(struct iommu_domain *domain)
89{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -070090 if (!iommu_found())
91 return;
92
Joerg Roedelfc2100e2008-11-26 17:21:24 +010093 iommu_ops->domain_destroy(domain);
94 kfree(domain);
95}
96EXPORT_SYMBOL_GPL(iommu_domain_free);
97
98int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
99{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700100 if (!iommu_found())
101 return -ENODEV;
102
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100103 return iommu_ops->attach_dev(domain, dev);
104}
105EXPORT_SYMBOL_GPL(iommu_attach_device);
106
107void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
108{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700109 if (!iommu_found())
110 return;
111
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100112 iommu_ops->detach_dev(domain, dev);
113}
114EXPORT_SYMBOL_GPL(iommu_detach_device);
115
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100116phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
117 unsigned long iova)
118{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700119 if (!iommu_found())
120 return 0;
121
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100122 return iommu_ops->iova_to_phys(domain, iova);
123}
124EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800125
126int iommu_domain_has_cap(struct iommu_domain *domain,
127 unsigned long cap)
128{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700129 if (!iommu_found())
130 return -ENODEV;
131
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800132 return iommu_ops->domain_has_cap(domain, cap);
133}
134EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100135
136int iommu_map(struct iommu_domain *domain, unsigned long iova,
137 phys_addr_t paddr, int gfp_order, int prot)
138{
139 unsigned long invalid_mask;
140 size_t size;
141
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700142 if (!iommu_found())
143 return -ENODEV;
144
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100145 size = 0x1000UL << gfp_order;
146 invalid_mask = size - 1;
147
148 BUG_ON((iova | paddr) & invalid_mask);
149
Joerg Roedel12c73892010-01-21 11:50:28 +0100150 return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100151}
152EXPORT_SYMBOL_GPL(iommu_map);
153
154int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
155{
156 unsigned long invalid_mask;
157 size_t size;
158
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700159 if (!iommu_found())
160 return -ENODEV;
161
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100162 size = 0x1000UL << gfp_order;
163 invalid_mask = size - 1;
164
165 BUG_ON(iova & invalid_mask);
166
Joerg Roedel12c73892010-01-21 11:50:28 +0100167 return iommu_ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100168}
169EXPORT_SYMBOL_GPL(iommu_unmap);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700170
171int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
172 struct scatterlist *sg, unsigned int len, int prot)
173{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700174 if (!iommu_found())
175 return -ENODEV;
176
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700177 BUG_ON(iova & (~PAGE_MASK));
178
179 return iommu_ops->map_range(domain, iova, sg, len, prot);
180}
181EXPORT_SYMBOL_GPL(iommu_map_range);
182
183int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
184 unsigned int len)
185{
Stepan Moskovchenko452926c2011-10-31 16:00:47 -0700186 if (!iommu_found())
187 return -ENODEV;
188
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700189 BUG_ON(iova & (~PAGE_MASK));
190
191 return iommu_ops->unmap_range(domain, iova, len);
192}
193EXPORT_SYMBOL_GPL(iommu_unmap_range);
Shubhraprakash Das4c436f22011-12-02 18:01:57 -0700194
195phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain)
196{
197 if (!iommu_found())
198 return 0;
199
200 return iommu_ops->get_pt_base_addr(domain);
201}
202EXPORT_SYMBOL_GPL(iommu_get_pt_base_addr);