blob: abaa0ca848bc1b748fbb6f4bd36840654de6c6fa [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
Joerg Roedele8245c12017-04-26 15:34:06 +020022#include <linux/scatterlist.h>
23#include <linux/device.h>
24#include <linux/types.h>
Laura Abbott74315cc2011-06-08 17:29:11 -040025#include <linux/errno.h>
Wang YanQing9a08d372013-04-19 09:38:04 +080026#include <linux/err.h>
Will Deacond0f60a42014-08-27 16:15:59 +010027#include <linux/of.h>
Joerg Roedele8245c12017-04-26 15:34:06 +020028
Shuah Khan56fa4842013-09-24 15:21:20 -060029#include <trace/events/iommu.h>
Laura Abbott74315cc2011-06-08 17:29:11 -040030
Will Deaconca13bb32013-11-05 15:59:53 +000031#define IOMMU_READ (1 << 0)
32#define IOMMU_WRITE (1 << 1)
33#define IOMMU_CACHE (1 << 2) /* DMA cache coherency */
Antonios Motakisa720b412014-10-13 14:06:16 +010034#define IOMMU_NOEXEC (1 << 3)
Robin Murphy31e68502016-04-05 12:39:30 +010035#define IOMMU_MMIO (1 << 4) /* e.g. things like MSI doorbells */
Mitchel Humpherys579b2a62017-01-06 18:58:08 +053036/*
37 * This is to make the IOMMU API setup privileged
38 * mapppings accessible by the master only at higher
39 * privileged execution level and inaccessible at
40 * less privileged levels.
41 */
42#define IOMMU_PRIV (1 << 5)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010043
Joerg Roedel905d66c2011-09-06 16:03:26 +020044struct iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -060045struct iommu_group;
Joerg Roedelff217762011-08-26 16:48:26 +020046struct bus_type;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010047struct device;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040048struct iommu_domain;
Joerg Roedelba1eabfa2012-08-03 15:55:41 +020049struct notifier_block;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040050
51/* iommu fault flags */
52#define IOMMU_FAULT_READ 0x0
53#define IOMMU_FAULT_WRITE 0x1
54
55typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +030056 struct device *, unsigned long, int, void *);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010057
Joerg Roedel0ff64f82012-01-26 19:40:53 +010058struct iommu_domain_geometry {
59 dma_addr_t aperture_start; /* First address that can be mapped */
60 dma_addr_t aperture_end; /* Last address that can be mapped */
61 bool force_aperture; /* DMA only allowed in mappable range? */
62};
63
Joerg Roedel8539c7c2015-03-26 13:43:05 +010064/* Domain feature flags */
65#define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */
66#define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API
67 implementation */
68#define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */
69
70/*
71 * This are the possible domain-types
72 *
73 * IOMMU_DOMAIN_BLOCKED - All DMA is blocked, can be used to isolate
74 * devices
75 * IOMMU_DOMAIN_IDENTITY - DMA addresses are system physical addresses
76 * IOMMU_DOMAIN_UNMANAGED - DMA mappings managed by IOMMU-API user, used
77 * for VMs
78 * IOMMU_DOMAIN_DMA - Internally used for DMA-API implementations.
79 * This flag allows IOMMU drivers to implement
80 * certain optimizations for these domains
81 */
82#define IOMMU_DOMAIN_BLOCKED (0U)
83#define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)
84#define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING)
85#define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \
86 __IOMMU_DOMAIN_DMA_API)
87
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010088struct iommu_domain {
Joerg Roedel8539c7c2015-03-26 13:43:05 +010089 unsigned type;
Thierry Redingb22f6432014-06-27 09:03:12 +020090 const struct iommu_ops *ops;
Robin Murphyd16e0fa2016-04-07 18:42:06 +010091 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040092 iommu_fault_handler_t handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +030093 void *handler_token;
Joerg Roedel0ff64f82012-01-26 19:40:53 +010094 struct iommu_domain_geometry geometry;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010095 void *iova_cookie;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010096};
97
Joerg Roedel1aed0742014-09-03 18:34:04 +020098enum iommu_cap {
99 IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA
100 transactions */
101 IOMMU_CAP_INTR_REMAP, /* IOMMU supports interrupt isolation */
Antonios Motakisc4986642014-10-13 14:06:17 +0100102 IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */
Joerg Roedel1aed0742014-09-03 18:34:04 +0200103};
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800104
Varun Sethi7cabf492013-07-15 10:20:56 +0530105/*
106 * Following constraints are specifc to FSL_PAMUV1:
107 * -aperture must be power of 2, and naturally aligned
108 * -number of windows must be power of 2, and address space size
109 * of each window is determined by aperture size / # of windows
110 * -the actual size of the mapped region of a window must be power
111 * of 2 starting with 4KB and physical address must be naturally
112 * aligned.
113 * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
114 * The caller can invoke iommu_domain_get_attr to check if the underlying
115 * iommu implementation supports these constraints.
116 */
117
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100118enum iommu_attr {
Joerg Roedel0ff64f82012-01-26 19:40:53 +0100119 DOMAIN_ATTR_GEOMETRY,
Joerg Roedeld2e12162013-01-29 13:49:04 +0100120 DOMAIN_ATTR_PAGING,
Joerg Roedel69356712013-02-04 14:00:01 +0100121 DOMAIN_ATTR_WINDOWS,
Varun Sethi7cabf492013-07-15 10:20:56 +0530122 DOMAIN_ATTR_FSL_PAMU_STASH,
123 DOMAIN_ATTR_FSL_PAMU_ENABLE,
124 DOMAIN_ATTR_FSL_PAMUV1,
Will Deaconc02607a2014-09-29 10:05:06 -0600125 DOMAIN_ATTR_NESTING, /* two stages of translation */
Joerg Roedela8b8a88a2013-01-29 14:36:31 +0100126 DOMAIN_ATTR_MAX,
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100127};
128
Eric Augerd30ddca2017-01-19 20:57:48 +0000129/* These are the possible reserved region types */
130#define IOMMU_RESV_DIRECT (1 << 0)
131#define IOMMU_RESV_RESERVED (1 << 1)
132#define IOMMU_RESV_MSI (1 << 2)
133
Joerg Roedela1015c22015-05-28 18:41:33 +0200134/**
Eric Augere5b52342017-01-19 20:57:47 +0000135 * struct iommu_resv_region - descriptor for a reserved memory region
Joerg Roedela1015c22015-05-28 18:41:33 +0200136 * @list: Linked list pointers
137 * @start: System physical start address of the region
138 * @length: Length of the region in bytes
139 * @prot: IOMMU Protection flags (READ/WRITE/...)
Eric Augerd30ddca2017-01-19 20:57:48 +0000140 * @type: Type of the reserved region
Joerg Roedela1015c22015-05-28 18:41:33 +0200141 */
Eric Augere5b52342017-01-19 20:57:47 +0000142struct iommu_resv_region {
Joerg Roedela1015c22015-05-28 18:41:33 +0200143 struct list_head list;
144 phys_addr_t start;
145 size_t length;
146 int prot;
Eric Augerd30ddca2017-01-19 20:57:48 +0000147 int type;
Joerg Roedela1015c22015-05-28 18:41:33 +0200148};
149
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200150#ifdef CONFIG_IOMMU_API
151
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200152/**
153 * struct iommu_ops - iommu ops and capabilities
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900154 * @capable: check capability
155 * @domain_alloc: allocate iommu domain
156 * @domain_free: free iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200157 * @attach_dev: attach device to an iommu domain
158 * @detach_dev: detach device from an iommu domain
159 * @map: map a physically contiguous memory region to an iommu domain
160 * @unmap: unmap a physically contiguous memory region from an iommu domain
Olav Haugan315786e2014-10-25 09:55:16 -0700161 * @map_sg: map a scatter-gather list of physically contiguous memory chunks
162 * to an iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200163 * @iova_to_phys: translate iova to physical address
Alex Williamsond72e31c2012-05-30 14:18:53 -0600164 * @add_device: add device to iommu grouping
165 * @remove_device: remove device from iommu grouping
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900166 * @device_group: find iommu group for a particular device
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100167 * @domain_get_attr: Query domain attributes
168 * @domain_set_attr: Change domain attributes
Eric Augere5b52342017-01-19 20:57:47 +0000169 * @get_resv_regions: Request list of reserved regions for a device
170 * @put_resv_regions: Free list of reserved regions for a device
171 * @apply_resv_region: Temporary helper call-back for iova reserved ranges
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900172 * @domain_window_enable: Configure and enable a particular window for a domain
173 * @domain_window_disable: Disable a particular window for a domain
174 * @domain_set_windows: Set the number of windows for a domain
175 * @domain_get_windows: Return the number of windows for a domain
Will Deacond0f60a42014-08-27 16:15:59 +0100176 * @of_xlate: add OF master IDs to iommu grouping
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100177 * @pgsize_bitmap: bitmap of all possible supported page sizes
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200178 */
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100179struct iommu_ops {
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200180 bool (*capable)(enum iommu_cap);
Joerg Roedel938c4702015-03-26 13:43:04 +0100181
182 /* Domain allocation and freeing by the iommu driver */
Joerg Roedel8539c7c2015-03-26 13:43:05 +0100183 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
Joerg Roedel938c4702015-03-26 13:43:04 +0100184 void (*domain_free)(struct iommu_domain *);
185
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100186 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
187 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
Joerg Roedel67651782010-01-21 16:32:27 +0100188 int (*map)(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200189 phys_addr_t paddr, size_t size, int prot);
190 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
191 size_t size);
Olav Haugan315786e2014-10-25 09:55:16 -0700192 size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
193 struct scatterlist *sg, unsigned int nents, int prot);
Varun Sethibb5547a2013-03-29 01:23:58 +0530194 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600195 int (*add_device)(struct device *dev);
196 void (*remove_device)(struct device *dev);
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200197 struct iommu_group *(*device_group)(struct device *dev);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100198 int (*domain_get_attr)(struct iommu_domain *domain,
199 enum iommu_attr attr, void *data);
200 int (*domain_set_attr)(struct iommu_domain *domain,
201 enum iommu_attr attr, void *data);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100202
Eric Augere5b52342017-01-19 20:57:47 +0000203 /* Request/Free a list of reserved regions for a device */
204 void (*get_resv_regions)(struct device *dev, struct list_head *list);
205 void (*put_resv_regions)(struct device *dev, struct list_head *list);
206 void (*apply_resv_region)(struct device *dev,
207 struct iommu_domain *domain,
208 struct iommu_resv_region *region);
Joerg Roedela1015c22015-05-28 18:41:33 +0200209
Joerg Roedeld7787d52013-01-29 14:26:20 +0100210 /* Window handling functions */
211 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530212 phys_addr_t paddr, u64 size, int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100213 void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900214 /* Set the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100215 int (*domain_set_windows)(struct iommu_domain *domain, u32 w_count);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900216 /* Get the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100217 u32 (*domain_get_windows)(struct iommu_domain *domain);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100218
Will Deacond0f60a42014-08-27 16:15:59 +0100219 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
Will Deacond0f60a42014-08-27 16:15:59 +0100220
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200221 unsigned long pgsize_bitmap;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100222};
223
Joerg Roedelb0119e82017-02-01 13:23:08 +0100224/**
225 * struct iommu_device - IOMMU core representation of one IOMMU hardware
226 * instance
227 * @list: Used by the iommu-core to keep a list of registered iommus
228 * @ops: iommu-ops for talking to this iommu
Joerg Roedel39ab9552017-02-01 16:56:46 +0100229 * @dev: struct device for sysfs handling
Joerg Roedelb0119e82017-02-01 13:23:08 +0100230 */
231struct iommu_device {
232 struct list_head list;
233 const struct iommu_ops *ops;
Joerg Roedelc73e1ac2017-02-07 18:18:46 +0100234 struct fwnode_handle *fwnode;
Joerg Roedel39ab9552017-02-01 16:56:46 +0100235 struct device dev;
Joerg Roedelb0119e82017-02-01 13:23:08 +0100236};
237
238int iommu_device_register(struct iommu_device *iommu);
239void iommu_device_unregister(struct iommu_device *iommu);
Joerg Roedel39ab9552017-02-01 16:56:46 +0100240int iommu_device_sysfs_add(struct iommu_device *iommu,
241 struct device *parent,
242 const struct attribute_group **groups,
243 const char *fmt, ...) __printf(4, 5);
244void iommu_device_sysfs_remove(struct iommu_device *iommu);
Joerg Roedele3d10af2017-02-01 17:23:22 +0100245int iommu_device_link(struct iommu_device *iommu, struct device *link);
246void iommu_device_unlink(struct iommu_device *iommu, struct device *link);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100247
248static inline void iommu_device_set_ops(struct iommu_device *iommu,
249 const struct iommu_ops *ops)
250{
251 iommu->ops = ops;
252}
253
Joerg Roedelc73e1ac2017-02-07 18:18:46 +0100254static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
255 struct fwnode_handle *fwnode)
256{
257 iommu->fwnode = fwnode;
258}
259
Alex Williamsond72e31c2012-05-30 14:18:53 -0600260#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
261#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
262#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
263#define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
264#define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
265#define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
266
Thierry Redingb22f6432014-06-27 09:03:12 +0200267extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
Joerg Roedela1b60c12011-09-06 18:46:34 +0200268extern bool iommu_present(struct bus_type *bus);
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200269extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
Joerg Roedel905d66c2011-09-06 16:03:26 +0200270extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100271extern struct iommu_group *iommu_group_get_by_id(int id);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100272extern void iommu_domain_free(struct iommu_domain *domain);
273extern int iommu_attach_device(struct iommu_domain *domain,
274 struct device *dev);
275extern void iommu_detach_device(struct iommu_domain *domain,
276 struct device *dev);
Joerg Roedel2c1296d2015-05-28 18:41:32 +0200277extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100278extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200279 phys_addr_t paddr, size_t size, int prot);
280extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
281 size_t size);
Olav Haugan315786e2014-10-25 09:55:16 -0700282extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
283 struct scatterlist *sg,unsigned int nents,
284 int prot);
Varun Sethibb5547a2013-03-29 01:23:58 +0530285extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400286extern void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300287 iommu_fault_handler_t handler, void *token);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600288
Eric Augere5b52342017-01-19 20:57:47 +0000289extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);
290extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200291extern int iommu_request_dm_for_dev(struct device *dev);
Eric Auger2b20cbb2017-01-19 20:57:49 +0000292extern struct iommu_resv_region *
293iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot, int type);
Eric Auger6c65fb32017-01-19 20:57:51 +0000294extern int iommu_get_group_resv_regions(struct iommu_group *group,
295 struct list_head *head);
Joerg Roedela1015c22015-05-28 18:41:33 +0200296
Alex Williamsond72e31c2012-05-30 14:18:53 -0600297extern int iommu_attach_group(struct iommu_domain *domain,
298 struct iommu_group *group);
299extern void iommu_detach_group(struct iommu_domain *domain,
300 struct iommu_group *group);
301extern struct iommu_group *iommu_group_alloc(void);
302extern void *iommu_group_get_iommudata(struct iommu_group *group);
303extern void iommu_group_set_iommudata(struct iommu_group *group,
304 void *iommu_data,
305 void (*release)(void *iommu_data));
306extern int iommu_group_set_name(struct iommu_group *group, const char *name);
307extern int iommu_group_add_device(struct iommu_group *group,
308 struct device *dev);
309extern void iommu_group_remove_device(struct device *dev);
310extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
311 int (*fn)(struct device *, void *));
312extern struct iommu_group *iommu_group_get(struct device *dev);
Robin Murphy13f59a72016-11-11 17:59:21 +0000313extern struct iommu_group *iommu_group_ref_get(struct iommu_group *group);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600314extern void iommu_group_put(struct iommu_group *group);
315extern int iommu_group_register_notifier(struct iommu_group *group,
316 struct notifier_block *nb);
317extern int iommu_group_unregister_notifier(struct iommu_group *group,
318 struct notifier_block *nb);
319extern int iommu_group_id(struct iommu_group *group);
Alex Williamson104a1c12014-07-03 09:51:18 -0600320extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
Joerg Roedel6827ca82015-05-28 18:41:35 +0200321extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400322
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100323extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
324 void *data);
325extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
326 void *data);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400327
Joerg Roedeld7787d52013-01-29 14:26:20 +0100328/* Window handling function prototypes */
329extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530330 phys_addr_t offset, u64 size,
331 int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100332extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400333
Joerg Roedel207c6e32017-04-26 15:39:28 +0200334extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
335 unsigned long iova, int flags);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100336
Olav Haugan315786e2014-10-25 09:55:16 -0700337static inline size_t iommu_map_sg(struct iommu_domain *domain,
338 unsigned long iova, struct scatterlist *sg,
339 unsigned int nents, int prot)
340{
341 return domain->ops->map_sg(domain, iova, sg, nents, prot);
342}
343
Joerg Roedel5e622922015-10-21 23:51:37 +0200344/* PCI device grouping function */
345extern struct iommu_group *pci_device_group(struct device *dev);
Joerg Roedel6eab5562015-10-21 23:51:38 +0200346/* Generic device grouping function */
347extern struct iommu_group *generic_device_group(struct device *dev);
Joerg Roedel5e622922015-10-21 23:51:37 +0200348
Robin Murphy57f98d22016-09-13 10:54:14 +0100349/**
350 * struct iommu_fwspec - per-device IOMMU instance data
351 * @ops: ops for this device's IOMMU
352 * @iommu_fwnode: firmware handle for this device's IOMMU
353 * @iommu_priv: IOMMU driver private data for this device
354 * @num_ids: number of associated device IDs
355 * @ids: IDs which this device may present to the IOMMU
356 */
357struct iommu_fwspec {
358 const struct iommu_ops *ops;
359 struct fwnode_handle *iommu_fwnode;
360 void *iommu_priv;
361 unsigned int num_ids;
362 u32 ids[1];
363};
364
365int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
366 const struct iommu_ops *ops);
367void iommu_fwspec_free(struct device *dev);
368int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);
Joerg Roedel534766d2017-01-31 16:58:42 +0100369const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode);
Robin Murphy57f98d22016-09-13 10:54:14 +0100370
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100371#else /* CONFIG_IOMMU_API */
372
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200373struct iommu_ops {};
Alex Williamsond72e31c2012-05-30 14:18:53 -0600374struct iommu_group {};
Robin Murphy57f98d22016-09-13 10:54:14 +0100375struct iommu_fwspec {};
Joerg Roedelb0119e82017-02-01 13:23:08 +0100376struct iommu_device {};
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100377
Joerg Roedela1b60c12011-09-06 18:46:34 +0200378static inline bool iommu_present(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100379{
380 return false;
381}
382
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200383static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
384{
385 return false;
386}
387
Joerg Roedel905d66c2011-09-06 16:03:26 +0200388static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100389{
390 return NULL;
391}
392
Alexey Kardashevskiyb62dfd22013-11-21 17:41:14 +1100393static inline struct iommu_group *iommu_group_get_by_id(int id)
394{
395 return NULL;
396}
397
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100398static inline void iommu_domain_free(struct iommu_domain *domain)
399{
400}
401
402static inline int iommu_attach_device(struct iommu_domain *domain,
403 struct device *dev)
404{
405 return -ENODEV;
406}
407
408static inline void iommu_detach_device(struct iommu_domain *domain,
409 struct device *dev)
410{
411}
412
Joerg Roedel2c1296d2015-05-28 18:41:32 +0200413static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
414{
415 return NULL;
416}
417
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100418static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
419 phys_addr_t paddr, int gfp_order, int prot)
420{
421 return -ENODEV;
422}
423
424static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
425 int gfp_order)
426{
427 return -ENODEV;
428}
429
Olav Haugan315786e2014-10-25 09:55:16 -0700430static inline size_t iommu_map_sg(struct iommu_domain *domain,
431 unsigned long iova, struct scatterlist *sg,
432 unsigned int nents, int prot)
433{
434 return -ENODEV;
435}
436
Joerg Roedeld7787d52013-01-29 14:26:20 +0100437static inline int iommu_domain_window_enable(struct iommu_domain *domain,
438 u32 wnd_nr, phys_addr_t paddr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530439 u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +0100440{
441 return -ENODEV;
442}
443
444static inline void iommu_domain_window_disable(struct iommu_domain *domain,
445 u32 wnd_nr)
446{
447}
448
Varun Sethibb5547a2013-03-29 01:23:58 +0530449static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100450{
451 return 0;
452}
453
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400454static inline void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300455 iommu_fault_handler_t handler, void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400456{
457}
458
Eric Augere5b52342017-01-19 20:57:47 +0000459static inline void iommu_get_resv_regions(struct device *dev,
Joerg Roedela1015c22015-05-28 18:41:33 +0200460 struct list_head *list)
461{
462}
463
Eric Augere5b52342017-01-19 20:57:47 +0000464static inline void iommu_put_resv_regions(struct device *dev,
Joerg Roedela1015c22015-05-28 18:41:33 +0200465 struct list_head *list)
466{
467}
468
Eric Auger6c65fb32017-01-19 20:57:51 +0000469static inline int iommu_get_group_resv_regions(struct iommu_group *group,
470 struct list_head *head)
471{
472 return -ENODEV;
473}
474
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200475static inline int iommu_request_dm_for_dev(struct device *dev)
476{
477 return -ENODEV;
478}
479
Alex Williamsonbef83de2012-09-24 21:23:25 -0600480static inline int iommu_attach_group(struct iommu_domain *domain,
481 struct iommu_group *group)
Alex Williamson14604322011-10-21 15:56:05 -0400482{
483 return -ENODEV;
484}
485
Alex Williamsonbef83de2012-09-24 21:23:25 -0600486static inline void iommu_detach_group(struct iommu_domain *domain,
487 struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600488{
489}
490
Alex Williamsonbef83de2012-09-24 21:23:25 -0600491static inline struct iommu_group *iommu_group_alloc(void)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600492{
493 return ERR_PTR(-ENODEV);
494}
495
Alex Williamsonbef83de2012-09-24 21:23:25 -0600496static inline void *iommu_group_get_iommudata(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600497{
498 return NULL;
499}
500
Alex Williamsonbef83de2012-09-24 21:23:25 -0600501static inline void iommu_group_set_iommudata(struct iommu_group *group,
502 void *iommu_data,
503 void (*release)(void *iommu_data))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600504{
505}
506
Alex Williamsonbef83de2012-09-24 21:23:25 -0600507static inline int iommu_group_set_name(struct iommu_group *group,
508 const char *name)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600509{
510 return -ENODEV;
511}
512
Alex Williamsonbef83de2012-09-24 21:23:25 -0600513static inline int iommu_group_add_device(struct iommu_group *group,
514 struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600515{
516 return -ENODEV;
517}
518
Alex Williamsonbef83de2012-09-24 21:23:25 -0600519static inline void iommu_group_remove_device(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600520{
521}
522
Alex Williamsonbef83de2012-09-24 21:23:25 -0600523static inline int iommu_group_for_each_dev(struct iommu_group *group,
524 void *data,
525 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600526{
527 return -ENODEV;
528}
529
Alex Williamsonbef83de2012-09-24 21:23:25 -0600530static inline struct iommu_group *iommu_group_get(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600531{
532 return NULL;
533}
534
Alex Williamsonbef83de2012-09-24 21:23:25 -0600535static inline void iommu_group_put(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600536{
537}
538
Alex Williamsonbef83de2012-09-24 21:23:25 -0600539static inline int iommu_group_register_notifier(struct iommu_group *group,
540 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600541{
542 return -ENODEV;
543}
544
Alex Williamsonbef83de2012-09-24 21:23:25 -0600545static inline int iommu_group_unregister_notifier(struct iommu_group *group,
546 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600547{
548 return 0;
549}
550
Alex Williamsonbef83de2012-09-24 21:23:25 -0600551static inline int iommu_group_id(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600552{
553 return -ENODEV;
554}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100555
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100556static inline int iommu_domain_get_attr(struct iommu_domain *domain,
557 enum iommu_attr attr, void *data)
558{
559 return -EINVAL;
560}
561
562static inline int iommu_domain_set_attr(struct iommu_domain *domain,
563 enum iommu_attr attr, void *data)
564{
565 return -EINVAL;
566}
567
Joerg Roedelb0119e82017-02-01 13:23:08 +0100568static inline int iommu_device_register(struct iommu_device *iommu)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600569{
Joerg Roedelb0119e82017-02-01 13:23:08 +0100570 return -ENODEV;
Alex Williamsonc61959e2014-06-12 16:12:24 -0600571}
572
Joerg Roedelb0119e82017-02-01 13:23:08 +0100573static inline void iommu_device_set_ops(struct iommu_device *iommu,
574 const struct iommu_ops *ops)
575{
576}
577
Joerg Roedelc73e1ac2017-02-07 18:18:46 +0100578static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
579 struct fwnode_handle *fwnode)
580{
581}
582
Joerg Roedelb0119e82017-02-01 13:23:08 +0100583static inline void iommu_device_unregister(struct iommu_device *iommu)
584{
585}
586
Joerg Roedel39ab9552017-02-01 16:56:46 +0100587static inline int iommu_device_sysfs_add(struct iommu_device *iommu,
588 struct device *parent,
589 const struct attribute_group **groups,
590 const char *fmt, ...)
591{
592 return -ENODEV;
593}
594
595static inline void iommu_device_sysfs_remove(struct iommu_device *iommu)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600596{
597}
598
Alex Williamsone09f8ea2014-07-07 14:31:36 -0600599static inline int iommu_device_link(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600600{
601 return -EINVAL;
602}
603
Alex Williamsone09f8ea2014-07-07 14:31:36 -0600604static inline void iommu_device_unlink(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600605{
606}
607
Robin Murphy57f98d22016-09-13 10:54:14 +0100608static inline int iommu_fwspec_init(struct device *dev,
609 struct fwnode_handle *iommu_fwnode,
610 const struct iommu_ops *ops)
611{
612 return -ENODEV;
613}
614
615static inline void iommu_fwspec_free(struct device *dev)
616{
617}
618
619static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids,
620 int num_ids)
621{
622 return -ENODEV;
623}
624
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +0000625static inline
Joerg Roedel534766d2017-01-31 16:58:42 +0100626const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +0000627{
628 return NULL;
629}
630
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100631#endif /* CONFIG_IOMMU_API */
632
633#endif /* __LINUX_IOMMU_H */