blob: 94e0fdb4ce33a290619f801f9cd73bbef4bcb572 [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>
23#include <linux/errno.h>
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -070024#include <linux/scatterlist.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010026#define IOMMU_READ (1)
27#define IOMMU_WRITE (2)
Sheng Yang9cf06692009-03-18 15:33:07 +080028#define IOMMU_CACHE (4) /* DMA cache coherency */
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010029
30struct device;
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -040031struct iommu_domain;
32
33/* iommu fault flags */
34#define IOMMU_FAULT_READ 0x0
35#define IOMMU_FAULT_WRITE 0x1
36
37typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
38 struct device *, unsigned long, int);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010039
40struct iommu_domain {
41 void *priv;
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -040042 iommu_fault_handler_t handler;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010043};
44
Sheng Yangdbb9fd82009-03-18 15:33:06 +080045#define IOMMU_CAP_CACHE_COHERENCY 0x1
Tom Lyon323f99c2010-07-02 16:56:14 -040046#define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */
Sheng Yangdbb9fd82009-03-18 15:33:06 +080047
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010048struct iommu_ops {
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070049 int (*domain_init)(struct iommu_domain *domain, int flags);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010050 void (*domain_destroy)(struct iommu_domain *domain);
51 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
52 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
Joerg Roedel67651782010-01-21 16:32:27 +010053 int (*map)(struct iommu_domain *domain, unsigned long iova,
54 phys_addr_t paddr, int gfp_order, int prot);
55 int (*unmap)(struct iommu_domain *domain, unsigned long iova,
56 int gfp_order);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -070057 int (*map_range)(struct iommu_domain *domain, unsigned int iova,
58 struct scatterlist *sg, unsigned int len, int prot);
59 int (*unmap_range)(struct iommu_domain *domain, unsigned int iova,
60 unsigned int len);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010061 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,
62 unsigned long iova);
Sheng Yangdbb9fd82009-03-18 15:33:06 +080063 int (*domain_has_cap)(struct iommu_domain *domain,
64 unsigned long cap);
Shubhraprakash Das4c436f22011-12-02 18:01:57 -070065 phys_addr_t (*get_pt_base_addr)(struct iommu_domain *domain);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010066};
67
68#ifdef CONFIG_IOMMU_API
69
70extern void register_iommu(struct iommu_ops *ops);
71extern bool iommu_found(void);
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -070072extern struct iommu_domain *iommu_domain_alloc(int flags);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010073extern void iommu_domain_free(struct iommu_domain *domain);
74extern int iommu_attach_device(struct iommu_domain *domain,
75 struct device *dev);
76extern void iommu_detach_device(struct iommu_domain *domain,
77 struct device *dev);
Joerg Roedelcefc53c2010-01-08 13:35:09 +010078extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
79 phys_addr_t paddr, int gfp_order, int prot);
80extern int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
81 int gfp_order);
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -070082extern int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
83 struct scatterlist *sg, unsigned int len, int prot);
84extern int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
85 unsigned int len);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010086extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
87 unsigned long iova);
Sheng Yangdbb9fd82009-03-18 15:33:06 +080088extern int iommu_domain_has_cap(struct iommu_domain *domain,
89 unsigned long cap);
Shubhraprakash Das4c436f22011-12-02 18:01:57 -070090extern phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain);
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -040091extern void iommu_set_fault_handler(struct iommu_domain *domain,
92 iommu_fault_handler_t handler);
93
94/**
95 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
96 * @domain: the iommu domain where the fault has happened
97 * @dev: the device where the fault has happened
98 * @iova: the faulting address
99 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
100 *
101 * This function should be called by the low-level IOMMU implementations
102 * whenever IOMMU faults happen, to allow high-level users, that are
103 * interested in such events, to know about them.
104 *
105 * This event may be useful for several possible use cases:
106 * - mere logging of the event
107 * - dynamic TLB/PTE loading
108 * - if restarting of the faulting device is required
109 *
110 * Returns 0 on success and an appropriate error code otherwise (if dynamic
111 * PTE/TLB loading will one day be supported, implementations will be able
112 * to tell whether it succeeded or not according to this return value).
113 */
114static inline int report_iommu_fault(struct iommu_domain *domain,
115 struct device *dev, unsigned long iova, int flags)
116{
117 int ret = 0;
118
119 /*
120 * if upper layers showed interest and installed a fault handler,
121 * invoke it.
122 */
123 if (domain->handler)
124 ret = domain->handler(domain, dev, iova, flags);
125
126 return ret;
127}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100128
129#else /* CONFIG_IOMMU_API */
130
131static inline void register_iommu(struct iommu_ops *ops)
132{
133}
134
135static inline bool iommu_found(void)
136{
137 return false;
138}
139
Stepan Moskovchenkoff2d3662011-08-31 17:13:32 -0700140static inline struct iommu_domain *iommu_domain_alloc(int flags)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100141{
142 return NULL;
143}
144
145static inline void iommu_domain_free(struct iommu_domain *domain)
146{
147}
148
149static inline int iommu_attach_device(struct iommu_domain *domain,
150 struct device *dev)
151{
152 return -ENODEV;
153}
154
155static inline void iommu_detach_device(struct iommu_domain *domain,
156 struct device *dev)
157{
158}
159
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100160static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
161 phys_addr_t paddr, int gfp_order, int prot)
162{
163 return -ENODEV;
164}
165
166static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
167 int gfp_order)
168{
169 return -ENODEV;
170}
171
Stepan Moskovchenko0b1345e2011-08-11 19:32:27 -0700172static inline int iommu_map_range(struct iommu_domain *domain,
173 unsigned int iova, struct scatterlist *sg,
174 unsigned int len, int prot)
175{
176 return -ENODEV;
177}
178
179static inline int iommu_unmap_range(struct iommu_domain *domain,
180 unsigned int iova, unsigned int len)
181{
182 return -ENODEV;
183}
184
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100185static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
186 unsigned long iova)
187{
188 return 0;
189}
190
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800191static inline int domain_has_cap(struct iommu_domain *domain,
192 unsigned long cap)
193{
194 return 0;
195}
196
Shubhraprakash Das4c436f22011-12-02 18:01:57 -0700197static inline phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain)
198{
199 return 0;
200}
Ohad Ben-Cohena1c6df32011-09-13 15:25:23 -0400201
202static inline void iommu_set_fault_handler(struct iommu_domain *domain,
203 iommu_fault_handler_t handler)
204{
205}
206
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100207#endif /* CONFIG_IOMMU_API */
208
209#endif /* __LINUX_IOMMU_H */