blob: b75d9fb2fa91d4ed2481841e4eff3f68f4503e31 [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>
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
Hannes Ederff2c8a42009-03-05 12:12:44 +010036bool iommu_found(void)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010037{
38 return iommu_ops != NULL;
39}
40EXPORT_SYMBOL_GPL(iommu_found);
41
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040042/**
43 * iommu_set_fault_handler() - set a fault handler for an iommu domain
44 * @domain: iommu domain
45 * @handler: fault handler
46 */
47void iommu_set_fault_handler(struct iommu_domain *domain,
48 iommu_fault_handler_t handler)
49{
50 BUG_ON(!domain);
51
52 domain->handler = handler;
53}
54
Joerg Roedelfc2100e2008-11-26 17:21:24 +010055struct iommu_domain *iommu_domain_alloc(void)
56{
57 struct iommu_domain *domain;
58 int ret;
59
60 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
61 if (!domain)
62 return NULL;
63
64 ret = iommu_ops->domain_init(domain);
65 if (ret)
66 goto out_free;
67
68 return domain;
69
70out_free:
71 kfree(domain);
72
73 return NULL;
74}
75EXPORT_SYMBOL_GPL(iommu_domain_alloc);
76
77void iommu_domain_free(struct iommu_domain *domain)
78{
79 iommu_ops->domain_destroy(domain);
80 kfree(domain);
81}
82EXPORT_SYMBOL_GPL(iommu_domain_free);
83
84int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
85{
86 return iommu_ops->attach_dev(domain, dev);
87}
88EXPORT_SYMBOL_GPL(iommu_attach_device);
89
90void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
91{
92 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{
99 return iommu_ops->iova_to_phys(domain, iova);
100}
101EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800102
103int iommu_domain_has_cap(struct iommu_domain *domain,
104 unsigned long cap)
105{
106 return iommu_ops->domain_has_cap(domain, cap);
107}
108EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100109
110int iommu_map(struct iommu_domain *domain, unsigned long iova,
111 phys_addr_t paddr, int gfp_order, int prot)
112{
113 unsigned long invalid_mask;
114 size_t size;
115
116 size = 0x1000UL << gfp_order;
117 invalid_mask = size - 1;
118
119 BUG_ON((iova | paddr) & invalid_mask);
120
Joerg Roedel12c73892010-01-21 11:50:28 +0100121 return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100122}
123EXPORT_SYMBOL_GPL(iommu_map);
124
125int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
126{
127 unsigned long invalid_mask;
128 size_t size;
129
130 size = 0x1000UL << gfp_order;
131 invalid_mask = size - 1;
132
133 BUG_ON(iova & invalid_mask);
134
Joerg Roedel12c73892010-01-21 11:50:28 +0100135 return iommu_ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100136}
137EXPORT_SYMBOL_GPL(iommu_unmap);