blob: 7a2953d8f12e14675c669bd8109543001d244b7b [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
Joerg Roedelff217762011-08-26 16:48:26 +020028static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010029{
Joerg Roedelfc2100e2008-11-26 17:21:24 +010030}
31
Joerg Roedelff217762011-08-26 16:48:26 +020032/**
33 * bus_set_iommu - set iommu-callbacks for the bus
34 * @bus: bus.
35 * @ops: the callbacks provided by the iommu-driver
36 *
37 * This function is called by an iommu driver to set the iommu methods
38 * used for a particular bus. Drivers for devices on that bus can use
39 * the iommu-api after these ops are registered.
40 * This special function is needed because IOMMUs are usually devices on
41 * the bus itself, so the iommu drivers are not initialized when the bus
42 * is set up. With this function the iommu-driver can set the iommu-ops
43 * afterwards.
44 */
45int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010046{
Joerg Roedelff217762011-08-26 16:48:26 +020047 if (bus->iommu_ops != NULL)
48 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +010049
Joerg Roedelff217762011-08-26 16:48:26 +020050 bus->iommu_ops = ops;
51
52 /* Do IOMMU specific setup for this bus-type */
53 iommu_bus_init(bus, ops);
54
55 return 0;
Joerg Roedelfc2100e2008-11-26 17:21:24 +010056}
Joerg Roedelff217762011-08-26 16:48:26 +020057EXPORT_SYMBOL_GPL(bus_set_iommu);
58
Joerg Roedela1b60c12011-09-06 18:46:34 +020059bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010060{
Joerg Roedel94441c32011-09-06 18:58:54 +020061 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +010062}
Joerg Roedela1b60c12011-09-06 18:46:34 +020063EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +010064
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040065/**
66 * iommu_set_fault_handler() - set a fault handler for an iommu domain
67 * @domain: iommu domain
68 * @handler: fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -040069 *
70 * This function should be used by IOMMU users which want to be notified
71 * whenever an IOMMU fault happens.
72 *
73 * The fault handler itself should return 0 on success, and an appropriate
74 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040075 */
76void iommu_set_fault_handler(struct iommu_domain *domain,
77 iommu_fault_handler_t handler)
78{
79 BUG_ON(!domain);
80
81 domain->handler = handler;
82}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -040083EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040084
Joerg Roedel905d66c2011-09-06 16:03:26 +020085struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +010086{
87 struct iommu_domain *domain;
88 int ret;
89
Joerg Roedel94441c32011-09-06 18:58:54 +020090 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +020091 return NULL;
92
Joerg Roedelfc2100e2008-11-26 17:21:24 +010093 domain = kmalloc(sizeof(*domain), GFP_KERNEL);
94 if (!domain)
95 return NULL;
96
Joerg Roedel94441c32011-09-06 18:58:54 +020097 domain->ops = bus->iommu_ops;
Joerg Roedel905d66c2011-09-06 16:03:26 +020098
Joerg Roedel94441c32011-09-06 18:58:54 +020099 ret = domain->ops->domain_init(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100100 if (ret)
101 goto out_free;
102
103 return domain;
104
105out_free:
106 kfree(domain);
107
108 return NULL;
109}
110EXPORT_SYMBOL_GPL(iommu_domain_alloc);
111
112void iommu_domain_free(struct iommu_domain *domain)
113{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200114 if (likely(domain->ops->domain_destroy != NULL))
115 domain->ops->domain_destroy(domain);
116
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100117 kfree(domain);
118}
119EXPORT_SYMBOL_GPL(iommu_domain_free);
120
121int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
122{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200123 if (unlikely(domain->ops->attach_dev == NULL))
124 return -ENODEV;
125
126 return domain->ops->attach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100127}
128EXPORT_SYMBOL_GPL(iommu_attach_device);
129
130void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
131{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200132 if (unlikely(domain->ops->detach_dev == NULL))
133 return;
134
135 domain->ops->detach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100136}
137EXPORT_SYMBOL_GPL(iommu_detach_device);
138
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100139phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
140 unsigned long iova)
141{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200142 if (unlikely(domain->ops->iova_to_phys == NULL))
143 return 0;
144
145 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100146}
147EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800148
149int iommu_domain_has_cap(struct iommu_domain *domain,
150 unsigned long cap)
151{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200152 if (unlikely(domain->ops->domain_has_cap == NULL))
153 return 0;
154
155 return domain->ops->domain_has_cap(domain, cap);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800156}
157EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100158
159int iommu_map(struct iommu_domain *domain, unsigned long iova,
160 phys_addr_t paddr, int gfp_order, int prot)
161{
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100162 size_t size;
163
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200164 if (unlikely(domain->ops->map == NULL))
165 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100166
Joerg Roedel85410340e2011-09-06 14:36:17 +0200167 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100168
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400169 BUG_ON(!IS_ALIGNED(iova | paddr, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100170
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200171 return domain->ops->map(domain, iova, paddr, size, prot);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100172}
173EXPORT_SYMBOL_GPL(iommu_map);
174
175int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order)
176{
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200177 size_t size, unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100178
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200179 if (unlikely(domain->ops->unmap == NULL))
180 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100181
Joerg Roedel85410340e2011-09-06 14:36:17 +0200182 size = PAGE_SIZE << gfp_order;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100183
Ohad Ben-Cohen40998182011-09-02 13:32:32 -0400184 BUG_ON(!IS_ALIGNED(iova, size));
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100185
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200186 unmapped = domain->ops->unmap(domain, iova, size);
187
188 return get_order(unmapped);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100189}
190EXPORT_SYMBOL_GPL(iommu_unmap);