blob: 46e1c24f2f4362c7a40513cf35ed3c73853c5a31 [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
Hannes Ederff2c8a42009-03-05 12:12:44 +010069bool iommu_found(void)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010070{
71 return iommu_ops != NULL;
72}
73EXPORT_SYMBOL_GPL(iommu_found);
74
Joerg Roedel905d66c2011-09-06 16:03:26 +020075struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010076{
77 struct iommu_domain *domain;
Joerg Roedel905d66c2011-09-06 16:03:26 +020078 struct iommu_ops *ops;
Joerg Roedelfc2100e2008-11-26 17:21:24 +010079 int ret;
80
Joerg Roedel905d66c2011-09-06 16:03:26 +020081 if (bus->iommu_ops)
82 ops = bus->iommu_ops;
83 else
84 ops = iommu_ops;
85
86 if (ops == NULL)
87 return NULL;
88
Joerg Roedelfc2100e2008-11-26 17:21:24 +010089 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
90 if (!domain)
91 return NULL;
92
Joerg Roedel905d66c2011-09-06 16:03:26 +020093 domain->ops = ops;
94
Joerg Roedelfc2100e2008-11-26 17:21:24 +010095 ret = iommu_ops->domain_init(domain);
96 if (ret)
97 goto out_free;
98
99 return domain;
100
101out_free:
102 kfree(domain);
103
104 return NULL;
105}
106EXPORT_SYMBOL_GPL(iommu_domain_alloc);
107
108void iommu_domain_free(struct iommu_domain *domain)
109{
110 iommu_ops->domain_destroy(domain);
111 kfree(domain);
112}
113EXPORT_SYMBOL_GPL(iommu_domain_free);
114
115int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
116{
117 return iommu_ops->attach_dev(domain, dev);
118}
119EXPORT_SYMBOL_GPL(iommu_attach_device);
120
121void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
122{
123 iommu_ops->detach_dev(domain, dev);
124}
125EXPORT_SYMBOL_GPL(iommu_detach_device);
126
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100127phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
128 unsigned long iova)
129{
130 return iommu_ops->iova_to_phys(domain, iova);
131}
132EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800133
134int iommu_domain_has_cap(struct iommu_domain *domain,
135 unsigned long cap)
136{
137 return iommu_ops->domain_has_cap(domain, cap);
138}
139EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100140
141int iommu_map(struct iommu_domain *domain, unsigned long iova,
142 phys_addr_t paddr, int gfp_order, int prot)
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 | paddr, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100149
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{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100156 size_t size;
157
Joerg Roedel85410340e2011-09-06 14:36:17 +0200158 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100159
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400160 BUG_ON(!IS_ALIGNED(iova, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100161
Joerg Roedel12c73892010-01-21 11:50:28 +0100162 return iommu_ops->unmap(domain, iova, gfp_order);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100163}
164EXPORT_SYMBOL_GPL(iommu_unmap);