blob: 3343264f5105a33729e2ac0bd85e5786ba925e81 [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
Ohad Ben-Cohen40998182011-09-02 13:32:32 -040019#include <linux/kernel.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010020#include <linux/bug.h>
21#include <linux/types.h>
Andrew Morton60db4022009-05-06 16:03:07 -070022#include <linux/module.h>
23#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010024#include <linux/errno.h>
25#include <linux/iommu.h>
26
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
Joerg Roedelff217762011-08-26 16:48:26 +020037static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
38{
39}
40
41/**
42 * bus_set_iommu - set iommu-callbacks for the bus
43 * @bus: bus.
44 * @ops: the callbacks provided by the iommu-driver
45 *
46 * This function is called by an iommu driver to set the iommu methods
47 * used for a particular bus. Drivers for devices on that bus can use
48 * the iommu-api after these ops are registered.
49 * This special function is needed because IOMMUs are usually devices on
50 * the bus itself, so the iommu drivers are not initialized when the bus
51 * is set up. With this function the iommu-driver can set the iommu-ops
52 * afterwards.
53 */
54int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
55{
56 if (bus->iommu_ops != NULL)
57 return -EBUSY;
58
59 bus->iommu_ops = ops;
60
61 /* Do IOMMU specific setup for this bus-type */
62 iommu_bus_init(bus, ops);
63
64 return 0;
65}
66EXPORT_SYMBOL_GPL(bus_set_iommu);
67
Hannes Ederff2c8a42009-03-05 12:12:44 +010068bool iommu_found(void)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010069{
70 return iommu_ops != NULL;
71}
72EXPORT_SYMBOL_GPL(iommu_found);
73
74struct iommu_domain *iommu_domain_alloc(void)
75{
76 struct iommu_domain *domain;
77 int ret;
78
79 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
80 if (!domain)
81 return NULL;
82
83 ret = iommu_ops->domain_init(domain);
84 if (ret)
85 goto out_free;
86
87 return domain;
88
89out_free:
90 kfree(domain);
91
92 return NULL;
93}
94EXPORT_SYMBOL_GPL(iommu_domain_alloc);
95
96void iommu_domain_free(struct iommu_domain *domain)
97{
98 iommu_ops->domain_destroy(domain);
99 kfree(domain);
100}
101EXPORT_SYMBOL_GPL(iommu_domain_free);
102
103int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
104{
105 return iommu_ops->attach_dev(domain, dev);
106}
107EXPORT_SYMBOL_GPL(iommu_attach_device);
108
109void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
110{
111 iommu_ops->detach_dev(domain, dev);
112}
113EXPORT_SYMBOL_GPL(iommu_detach_device);
114
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100115phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
116 unsigned long iova)
117{
118 return iommu_ops->iova_to_phys(domain, iova);
119}
120EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800121
122int iommu_domain_has_cap(struct iommu_domain *domain,
123 unsigned long cap)
124{
125 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{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100132 size_t size;
133
Joerg Roedel85410340e2011-09-06 14:36:17 +0200134 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100135
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400136 BUG_ON(!IS_ALIGNED(iova | paddr, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100137
Joerg Roedel12c73892010-01-21 11:50:28 +0100138 return iommu_ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100139}
140EXPORT_SYMBOL_GPL(iommu_map);
141
142int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
143{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100144 size_t size;
145
Joerg Roedel85410340e2011-09-06 14:36:17 +0200146 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100147
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400148 BUG_ON(!IS_ALIGNED(iova, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100149
Joerg Roedel12c73892010-01-21 11:50:28 +0100150 return iommu_ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100151}
152EXPORT_SYMBOL_GPL(iommu_unmap);