blob: d4fa3aa37048ff55edd6b8c5849034269e3da3bb [file] [log] [blame]
Joerg Roedel4a77a6c2008-11-26 17:02:33 +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#ifndef __LINUX_IOMMU_H
20#define __LINUX_IOMMU_H
21
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/types.h>
Laura Abbott74315cc2011-06-08 17:29:11 -040023#include <linux/errno.h>
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -070024#include <linux/scatterlist.h>
Alex Williamson43a0ba62012-05-30 14:18:53 -060025#include <linux/err.h>
Laura Abbott74315cc2011-06-08 17:29:11 -040026
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010027#define IOMMU_READ (1)
28#define IOMMU_WRITE (2)
Sheng Yang9cf06692009-03-18 15:33:07 +080029#define IOMMU_CACHE (4) /* DMA cache coherency */
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010030
Joerg Roedel905d66c2011-09-06 16:03:26 +020031struct iommu_ops;
Alex Williamson43a0ba62012-05-30 14:18:53 -060032struct iommu_group;
Joerg Roedelff217762011-08-26 16:48:26 +020033struct bus_type;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010034struct device;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040035struct iommu_domain;
36
37/* iommu fault flags */
38#define IOMMU_FAULT_READ 0x0
39#define IOMMU_FAULT_WRITE 0x1
40
41typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
Ohad Ben-Cohend5069532012-05-21 20:20:05 +030042 struct device *, unsigned long, int, void *);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010043
44struct iommu_domain {
Joerg Roedel905d66c2011-09-06 16:03:26 +020045 struct iommu_ops *ops;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010046 void *priv;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040047 iommu_fault_handler_t handler;
Ohad Ben-Cohend5069532012-05-21 20:20:05 +030048 void *handler_token;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010049};
50
Sheng Yangdbb9fd82009-03-18 15:33:06 +080051#define IOMMU_CAP_CACHE_COHERENCY 0x1
Tom Lyon323f99c2010-07-02 16:56:14 -040052#define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */
Sheng Yangdbb9fd82009-03-18 15:33:06 +080053
Joerg Roedel39d4ebb2011-09-06 16:48:40 +020054#ifdef CONFIG_IOMMU_API
55
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +020056/**
57 * struct iommu_ops - iommu ops and capabilities
58 * @domain_init: init iommu domain
59 * @domain_destroy: destroy iommu domain
60 * @attach_dev: attach device to an iommu domain
61 * @detach_dev: detach device from an iommu domain
62 * @map: map a physically contiguous memory region to an iommu domain
63 * @unmap: unmap a physically contiguous memory region from an iommu domain
64 * @iova_to_phys: translate iova to physical address
65 * @domain_has_cap: domain capabilities query
66 * @commit: commit iommu domain
Alex Williamson43a0ba62012-05-30 14:18:53 -060067 * @add_device: add device to iommu grouping
68 * @remove_device: remove device from iommu grouping
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +020069 * @pgsize_bitmap: bitmap of supported page sizes
70 */
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010071struct iommu_ops {
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070072 int (*domain_init)(struct iommu_domain *domain, int flags);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010073 void (*domain_destroy)(struct iommu_domain *domain);
74 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
75 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
Joerg Roedel67651782010-01-21 16:32:27 +010076 int (*map)(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +020077 phys_addr_t paddr, size_t size, int prot);
78 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
79 size_t size);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -070080 int (*map_range)(struct iommu_domain *domain, unsigned int iova,
81 struct scatterlist *sg, unsigned int len, int prot);
82 int (*unmap_range)(struct iommu_domain *domain, unsigned int iova,
83 unsigned int len);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010084 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,
85 unsigned long iova);
Sheng Yangdbb9fd82009-03-18 15:33:06 +080086 int (*domain_has_cap)(struct iommu_domain *domain,
87 unsigned long cap);
Shubhraprakash Das4c436f22011-12-02 18:01:57 -070088 phys_addr_t (*get_pt_base_addr)(struct iommu_domain *domain);
Alex Williamson43a0ba62012-05-30 14:18:53 -060089 int (*add_device)(struct device *dev);
90 void (*remove_device)(struct device *dev);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +020091 unsigned long pgsize_bitmap;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010092};
93
Alex Williamson43a0ba62012-05-30 14:18:53 -060094#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
95#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
96#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
97#define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
98#define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
99#define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
100
Joerg Roedelff217762011-08-26 16:48:26 +0200101extern int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops);
Joerg Roedela1b60c12011-09-06 18:46:34 +0200102extern bool iommu_present(struct bus_type *bus);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700103extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100104extern void iommu_domain_free(struct iommu_domain *domain);
105extern int iommu_attach_device(struct iommu_domain *domain,
106 struct device *dev);
107extern void iommu_detach_device(struct iommu_domain *domain,
108 struct device *dev);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100109extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200110 phys_addr_t paddr, size_t size, int prot);
111extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
112 size_t size);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700113extern int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
114 struct scatterlist *sg, unsigned int len, int prot);
115extern int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
116 unsigned int len);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100117extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
118 unsigned long iova);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800119extern int iommu_domain_has_cap(struct iommu_domain *domain,
120 unsigned long cap);
Shubhraprakash Das4c436f22011-12-02 18:01:57 -0700121extern phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400122extern void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohend5069532012-05-21 20:20:05 +0300123 iommu_fault_handler_t handler, void *token);
Alex Williamson43a0ba62012-05-30 14:18:53 -0600124
125extern int iommu_attach_group(struct iommu_domain *domain,
126 struct iommu_group *group);
127extern void iommu_detach_group(struct iommu_domain *domain,
128 struct iommu_group *group);
129extern struct iommu_group *iommu_group_alloc(void);
130extern void *iommu_group_get_iommudata(struct iommu_group *group);
131extern void iommu_group_set_iommudata(struct iommu_group *group,
132 void *iommu_data,
133 void (*release)(void *iommu_data));
134extern int iommu_group_set_name(struct iommu_group *group, const char *name);
135extern int iommu_group_add_device(struct iommu_group *group,
136 struct device *dev);
137extern void iommu_group_remove_device(struct device *dev);
138extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
139 int (*fn)(struct device *, void *));
140extern struct iommu_group *iommu_group_get(struct device *dev);
141extern void iommu_group_put(struct iommu_group *group);
142extern int iommu_group_register_notifier(struct iommu_group *group,
143 struct notifier_block *nb);
144extern int iommu_group_unregister_notifier(struct iommu_group *group,
145 struct notifier_block *nb);
146extern int iommu_group_id(struct iommu_group *group);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400147
148/**
149 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
150 * @domain: the iommu domain where the fault has happened
151 * @dev: the device where the fault has happened
152 * @iova: the faulting address
153 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
154 *
155 * This function should be called by the low-level IOMMU implementations
156 * whenever IOMMU faults happen, to allow high-level users, that are
157 * interested in such events, to know about them.
158 *
159 * This event may be useful for several possible use cases:
160 * - mere logging of the event
161 * - dynamic TLB/PTE loading
162 * - if restarting of the faulting device is required
163 *
164 * Returns 0 on success and an appropriate error code otherwise (if dynamic
165 * PTE/TLB loading will one day be supported, implementations will be able
166 * to tell whether it succeeded or not according to this return value).
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400167 *
168 * Specifically, -ENOSYS is returned if a fault handler isn't installed
169 * (though fault handlers can also return -ENOSYS, in case they want to
170 * elicit the default behavior of the IOMMU drivers).
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400171 */
172static inline int report_iommu_fault(struct iommu_domain *domain,
173 struct device *dev, unsigned long iova, int flags)
174{
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400175 int ret = -ENOSYS;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400176
177 /*
178 * if upper layers showed interest and installed a fault handler,
179 * invoke it.
180 */
181 if (domain->handler)
Ohad Ben-Cohend5069532012-05-21 20:20:05 +0300182 ret = domain->handler(domain, dev, iova, flags,
183 domain->handler_token);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400184
185 return ret;
186}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100187
188#else /* CONFIG_IOMMU_API */
189
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200190struct iommu_ops {};
Alex Williamson43a0ba62012-05-30 14:18:53 -0600191struct iommu_group {};
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100192
Joerg Roedela1b60c12011-09-06 18:46:34 +0200193static inline bool iommu_present(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100194{
195 return false;
196}
197
Steve Mucklef132c6c2012-06-06 18:30:57 -0700198static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100199{
200 return NULL;
201}
202
203static inline void iommu_domain_free(struct iommu_domain *domain)
204{
205}
206
207static inline int iommu_attach_device(struct iommu_domain *domain,
208 struct device *dev)
209{
210 return -ENODEV;
211}
212
213static inline void iommu_detach_device(struct iommu_domain *domain,
214 struct device *dev)
215{
216}
217
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100218static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
219 phys_addr_t paddr, int gfp_order, int prot)
220{
221 return -ENODEV;
222}
223
224static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
225 int gfp_order)
226{
227 return -ENODEV;
228}
229
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700230static inline int iommu_map_range(struct iommu_domain *domain,
231 unsigned int iova, struct scatterlist *sg,
232 unsigned int len, int prot)
233{
234 return -ENODEV;
235}
236
237static inline int iommu_unmap_range(struct iommu_domain *domain,
238 unsigned int iova, unsigned int len)
239{
240 return -ENODEV;
241}
242
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100243static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
244 unsigned long iova)
245{
246 return 0;
247}
248
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800249static inline int domain_has_cap(struct iommu_domain *domain,
250 unsigned long cap)
251{
252 return 0;
253}
254
Shubhraprakash Das4c436f22011-12-02 18:01:57 -0700255static inline phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain)
256{
257 return 0;
258}
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -0400259
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400260static inline void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohend5069532012-05-21 20:20:05 +0300261 iommu_fault_handler_t handler, void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400262{
263}
264
Alex Williamson43a0ba62012-05-30 14:18:53 -0600265static inline int iommu_attach_group(struct iommu_domain *domain,
266 struct iommu_group *group)
Alex Williamson14604322011-10-21 15:56:05 -0400267{
268 return -ENODEV;
269}
270
Alex Williamson43a0ba62012-05-30 14:18:53 -0600271static inline void iommu_detach_group(struct iommu_domain *domain,
272 struct iommu_group *group)
273{
274}
275
276static inline struct iommu_group *iommu_group_alloc(void)
277{
278 return ERR_PTR(-ENODEV);
279}
280
281static inline void *iommu_group_get_iommudata(struct iommu_group *group)
282{
283 return NULL;
284}
285
286static inline void iommu_group_set_iommudata(struct iommu_group *group,
287 void *iommu_data,
288 void (*release)(void *iommu_data))
289{
290}
291
292static inline int iommu_group_set_name(struct iommu_group *group,
293 const char *name)
294{
295 return -ENODEV;
296}
297
298static inline int iommu_group_add_device(struct iommu_group *group,
299 struct device *dev)
300{
301 return -ENODEV;
302}
303
304static inline void iommu_group_remove_device(struct device *dev)
305{
306}
307
308static inline int iommu_group_for_each_dev(struct iommu_group *group,
309 void *data,
310 int (*fn)(struct device *, void *))
311{
312 return -ENODEV;
313}
314
315static inline struct iommu_group *iommu_group_get(struct device *dev)
316{
317 return NULL;
318}
319
320static inline void iommu_group_put(struct iommu_group *group)
321{
322}
323
324static inline int iommu_group_register_notifier(struct iommu_group *group,
325 struct notifier_block *nb)
326{
327 return -ENODEV;
328}
329
330static inline int iommu_group_unregister_notifier(struct iommu_group *group,
331 struct notifier_block *nb)
332{
333 return 0;
334}
335
336static inline int iommu_group_id(struct iommu_group *group)
337{
338 return -ENODEV;
339}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100340#endif /* CONFIG_IOMMU_API */
341
342#endif /* __LINUX_IOMMU_H */