blob: 1575aaa36c94d9c16b6d8d77c2e59e96e50324cf [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
Joerg Roedel905d66c2011-09-06 16:03:26 +020019#include <linux/device.h>
Ohad Ben-Cohen40998182011-09-02 13:32:32 -040020#include <linux/kernel.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010021#include <linux/bug.h>
22#include <linux/types.h>
Andrew Morton60db4022009-05-06 16:03:07 -070023#include <linux/module.h>
24#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010025#include <linux/errno.h>
26#include <linux/iommu.h>
27
28static struct iommu_ops *iommu_ops;
29
30void register_iommu(struct iommu_ops *ops)
31{
32 if (iommu_ops)
33 BUG();
34
35 iommu_ops = ops;
36}
37
Joerg Roedelff217762011-08-26 16:48:26 +020038static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
39{
40}
41
42/**
43 * bus_set_iommu - set iommu-callbacks for the bus
44 * @bus: bus.
45 * @ops: the callbacks provided by the iommu-driver
46 *
47 * This function is called by an iommu driver to set the iommu methods
48 * used for a particular bus. Drivers for devices on that bus can use
49 * the iommu-api after these ops are registered.
50 * This special function is needed because IOMMUs are usually devices on
51 * the bus itself, so the iommu drivers are not initialized when the bus
52 * is set up. With this function the iommu-driver can set the iommu-ops
53 * afterwards.
54 */
55int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
56{
57 if (bus->iommu_ops != NULL)
58 return -EBUSY;
59
60 bus->iommu_ops = ops;
61
62 /* Do IOMMU specific setup for this bus-type */
63 iommu_bus_init(bus, ops);
64
65 return 0;
66}
67EXPORT_SYMBOL_GPL(bus_set_iommu);
68
Joerg Roedela1b60c12011-09-06 18:46:34 +020069bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010070{
Joerg Roedela1b60c12011-09-06 18:46:34 +020071 if (bus->iommu_ops != NULL)
72 return true;
73 else
74 return iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +010075}
Joerg Roedela1b60c12011-09-06 18:46:34 +020076EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +010077
Joerg Roedel905d66c2011-09-06 16:03:26 +020078struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010079{
80 struct iommu_domain *domain;
Joerg Roedel905d66c2011-09-06 16:03:26 +020081 struct iommu_ops *ops;
Joerg Roedelfc2100e2008-11-26 17:21:24 +010082 int ret;
83
Joerg Roedel905d66c2011-09-06 16:03:26 +020084 if (bus->iommu_ops)
85 ops = bus->iommu_ops;
86 else
87 ops = iommu_ops;
88
89 if (ops == NULL)
90 return NULL;
91
Joerg Roedelfc2100e2008-11-26 17:21:24 +010092 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
93 if (!domain)
94 return NULL;
95
Joerg Roedel905d66c2011-09-06 16:03:26 +020096 domain->ops = ops;
97
Joerg Roedelfc2100e2008-11-26 17:21:24 +010098 ret = iommu_ops->domain_init(domain);
99 if (ret)
100 goto out_free;
101
102 return domain;
103
104out_free:
105 kfree(domain);
106
107 return NULL;
108}
109EXPORT_SYMBOL_GPL(iommu_domain_alloc);
110
111void iommu_domain_free(struct iommu_domain *domain)
112{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200113 if (likely(domain->ops->domain_destroy != NULL))
114 domain->ops->domain_destroy(domain);
115
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100116 kfree(domain);
117}
118EXPORT_SYMBOL_GPL(iommu_domain_free);
119
120int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
121{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200122 if (unlikely(domain->ops->attach_dev == NULL))
123 return -ENODEV;
124
125 return domain->ops->attach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100126}
127EXPORT_SYMBOL_GPL(iommu_attach_device);
128
129void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
130{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200131 if (unlikely(domain->ops->detach_dev == NULL))
132 return;
133
134 domain->ops->detach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100135}
136EXPORT_SYMBOL_GPL(iommu_detach_device);
137
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100138phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
139 unsigned long iova)
140{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200141 if (unlikely(domain->ops->iova_to_phys == NULL))
142 return 0;
143
144 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100145}
146EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800147
148int iommu_domain_has_cap(struct iommu_domain *domain,
149 unsigned long cap)
150{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200151 if (unlikely(domain->ops->domain_has_cap == NULL))
152 return 0;
153
154 return domain->ops->domain_has_cap(domain, cap);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800155}
156EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100157
158int iommu_map(struct iommu_domain *domain, unsigned long iova,
159 phys_addr_t paddr, int gfp_order, int prot)
160{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100161 size_t size;
162
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200163 if (unlikely(domain->ops->map == NULL))
164 return -ENODEV;
165
Joerg Roedel85410340e2011-09-06 14:36:17 +0200166 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100167
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400168 BUG_ON(!IS_ALIGNED(iova | paddr, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100169
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200170 return domain->ops->map(domain, iova, paddr, gfp_order, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100171}
172EXPORT_SYMBOL_GPL(iommu_map);
173
174int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
175{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100176 size_t size;
177
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200178 if (unlikely(domain->ops->unmap == NULL))
179 return -ENODEV;
180
Joerg Roedel85410340e2011-09-06 14:36:17 +0200181 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100182
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400183 BUG_ON(!IS_ALIGNED(iova, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100184
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200185 return domain->ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100186}
187EXPORT_SYMBOL_GPL(iommu_unmap);