blob: 1b335772156fb20735d1819004bead387a9a3a22 [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
Laura Abbott74315cc2011-06-08 17:29:11 -040022#include <linux/errno.h>
Wang YanQing9a08d372013-04-19 09:38:04 +080023#include <linux/err.h>
Will Deacond0f60a42014-08-27 16:15:59 +010024#include <linux/of.h>
Thierry Reding76582d02012-07-25 16:24:49 +020025#include <linux/types.h>
Olav Haugan315786e2014-10-25 09:55:16 -070026#include <linux/scatterlist.h>
Shuah Khan56fa4842013-09-24 15:21:20 -060027#include <trace/events/iommu.h>
Laura Abbott74315cc2011-06-08 17:29:11 -040028
Will Deaconca13bb32013-11-05 15:59:53 +000029#define IOMMU_READ (1 << 0)
30#define IOMMU_WRITE (1 << 1)
31#define IOMMU_CACHE (1 << 2) /* DMA cache coherency */
Antonios Motakisa720b412014-10-13 14:06:16 +010032#define IOMMU_NOEXEC (1 << 3)
Robin Murphy31e68502016-04-05 12:39:30 +010033#define IOMMU_MMIO (1 << 4) /* e.g. things like MSI doorbells */
Mitchel Humpheryse038dfd2015-04-01 19:03:31 -070034#define IOMMU_PRIV (1 << 5)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010035
Joerg Roedel905d66c2011-09-06 16:03:26 +020036struct iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -060037struct iommu_group;
Joerg Roedelff217762011-08-26 16:48:26 +020038struct bus_type;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010039struct device;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040040struct iommu_domain;
Joerg Roedelba1eabfa2012-08-03 15:55:41 +020041struct notifier_block;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040042
43/* iommu fault flags */
Sushmita Susheelendra5e36b912015-06-02 15:46:24 -060044#define IOMMU_FAULT_READ (1 << 0)
45#define IOMMU_FAULT_WRITE (1 << 1)
46#define IOMMU_FAULT_TRANSLATION (1 << 2)
47#define IOMMU_FAULT_PERMISSION (1 << 3)
Mitchel Humpherys0cc54a22015-08-19 10:57:55 -070048#define IOMMU_FAULT_EXTERNAL (1 << 4)
49#define IOMMU_FAULT_TRANSACTION_STALLED (1 << 5)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040050
51typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +030052 struct device *, unsigned long, int, void *);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010053
Joerg Roedel0ff64f82012-01-26 19:40:53 +010054struct iommu_domain_geometry {
55 dma_addr_t aperture_start; /* First address that can be mapped */
56 dma_addr_t aperture_end; /* Last address that can be mapped */
57 bool force_aperture; /* DMA only allowed in mappable range? */
58};
59
Joerg Roedel8539c7c2015-03-26 13:43:05 +010060/* Domain feature flags */
61#define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */
62#define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API
63 implementation */
64#define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */
65
66/*
67 * This are the possible domain-types
68 *
69 * IOMMU_DOMAIN_BLOCKED - All DMA is blocked, can be used to isolate
70 * devices
71 * IOMMU_DOMAIN_IDENTITY - DMA addresses are system physical addresses
72 * IOMMU_DOMAIN_UNMANAGED - DMA mappings managed by IOMMU-API user, used
73 * for VMs
74 * IOMMU_DOMAIN_DMA - Internally used for DMA-API implementations.
75 * This flag allows IOMMU drivers to implement
76 * certain optimizations for these domains
77 */
78#define IOMMU_DOMAIN_BLOCKED (0U)
79#define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)
80#define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING)
81#define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \
82 __IOMMU_DOMAIN_DMA_API)
83
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010084struct iommu_domain {
Joerg Roedel8539c7c2015-03-26 13:43:05 +010085 unsigned type;
Thierry Redingb22f6432014-06-27 09:03:12 +020086 const struct iommu_ops *ops;
Robin Murphyd16e0fa2016-04-07 18:42:06 +010087 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040088 iommu_fault_handler_t handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +030089 void *handler_token;
Joerg Roedel0ff64f82012-01-26 19:40:53 +010090 struct iommu_domain_geometry geometry;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010091 void *iova_cookie;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010092};
93
Joerg Roedel1aed0742014-09-03 18:34:04 +020094enum iommu_cap {
95 IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA
96 transactions */
97 IOMMU_CAP_INTR_REMAP, /* IOMMU supports interrupt isolation */
Antonios Motakisc4986642014-10-13 14:06:17 +010098 IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */
Joerg Roedel1aed0742014-09-03 18:34:04 +020099};
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800100
Varun Sethi7cabf492013-07-15 10:20:56 +0530101/*
102 * Following constraints are specifc to FSL_PAMUV1:
103 * -aperture must be power of 2, and naturally aligned
104 * -number of windows must be power of 2, and address space size
105 * of each window is determined by aperture size / # of windows
106 * -the actual size of the mapped region of a window must be power
107 * of 2 starting with 4KB and physical address must be naturally
108 * aligned.
109 * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
110 * The caller can invoke iommu_domain_get_attr to check if the underlying
111 * iommu implementation supports these constraints.
112 */
113
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100114enum iommu_attr {
Joerg Roedel0ff64f82012-01-26 19:40:53 +0100115 DOMAIN_ATTR_GEOMETRY,
Joerg Roedeld2e12162013-01-29 13:49:04 +0100116 DOMAIN_ATTR_PAGING,
Joerg Roedel69356712013-02-04 14:00:01 +0100117 DOMAIN_ATTR_WINDOWS,
Varun Sethi7cabf492013-07-15 10:20:56 +0530118 DOMAIN_ATTR_FSL_PAMU_STASH,
119 DOMAIN_ATTR_FSL_PAMU_ENABLE,
120 DOMAIN_ATTR_FSL_PAMUV1,
Will Deaconc02607a2014-09-29 10:05:06 -0600121 DOMAIN_ATTR_NESTING, /* two stages of translation */
Mitchel Humpherys19f19d52014-11-24 12:31:02 -0800122 DOMAIN_ATTR_PT_BASE_ADDR,
Jeremy Gebbene352bb02015-06-16 10:54:01 -0600123 DOMAIN_ATTR_CONTEXT_BANK,
Jeremy Gebben1b34ee12015-07-10 16:43:23 -0600124 DOMAIN_ATTR_DYNAMIC,
Jeremy Gebbene70da582015-07-10 16:43:22 -0600125 DOMAIN_ATTR_TTBR0,
126 DOMAIN_ATTR_CONTEXTIDR,
127 DOMAIN_ATTR_PROCID,
Mitchel Humpheryse78b0a52015-09-25 17:26:31 -0700128 DOMAIN_ATTR_NON_FATAL_FAULTS,
Patrick Dalybb82f962016-03-10 15:25:51 -0800129 DOMAIN_ATTR_S1_BYPASS,
Joerg Roedela8b8a88a2013-01-29 14:36:31 +0100130 DOMAIN_ATTR_MAX,
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100131};
132
Joerg Roedela1015c22015-05-28 18:41:33 +0200133/**
134 * struct iommu_dm_region - descriptor for a direct mapped memory region
135 * @list: Linked list pointers
136 * @start: System physical start address of the region
137 * @length: Length of the region in bytes
138 * @prot: IOMMU Protection flags (READ/WRITE/...)
139 */
140struct iommu_dm_region {
141 struct list_head list;
142 phys_addr_t start;
143 size_t length;
144 int prot;
145};
146
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200147#ifdef CONFIG_IOMMU_API
148
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200149/**
150 * struct iommu_ops - iommu ops and capabilities
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900151 * @capable: check capability
152 * @domain_alloc: allocate iommu domain
153 * @domain_free: free iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200154 * @attach_dev: attach device to an iommu domain
155 * @detach_dev: detach device from an iommu domain
156 * @map: map a physically contiguous memory region to an iommu domain
157 * @unmap: unmap a physically contiguous memory region from an iommu domain
Olav Haugan315786e2014-10-25 09:55:16 -0700158 * @map_sg: map a scatter-gather list of physically contiguous memory chunks
159 * to an iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200160 * @iova_to_phys: translate iova to physical address
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700161 * @iova_to_phys_hard: translate iova to physical address using IOMMU hardware
Alex Williamsond72e31c2012-05-30 14:18:53 -0600162 * @add_device: add device to iommu grouping
163 * @remove_device: remove device from iommu grouping
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900164 * @device_group: find iommu group for a particular device
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100165 * @domain_get_attr: Query domain attributes
166 * @domain_set_attr: Change domain attributes
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900167 * @get_dm_regions: Request list of direct mapping requirements for a device
168 * @put_dm_regions: Free list of direct mapping requirements for a device
Joerg Roedel33b21a62016-07-05 13:07:53 +0200169 * @apply_dm_region: Temporary helper call-back for iova reserved ranges
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900170 * @domain_window_enable: Configure and enable a particular window for a domain
171 * @domain_window_disable: Disable a particular window for a domain
172 * @domain_set_windows: Set the number of windows for a domain
173 * @domain_get_windows: Return the number of windows for a domain
Will Deacond0f60a42014-08-27 16:15:59 +0100174 * @of_xlate: add OF master IDs to iommu grouping
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100175 * @pgsize_bitmap: bitmap of all possible supported page sizes
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700176 * @trigger_fault: trigger a fault on the device attached to an iommu domain
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700177 * @reg_read: read an IOMMU register
178 * @reg_write: write an IOMMU register
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200179 */
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100180struct iommu_ops {
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200181 bool (*capable)(enum iommu_cap);
Joerg Roedel938c4702015-03-26 13:43:04 +0100182
183 /* Domain allocation and freeing by the iommu driver */
Joerg Roedel8539c7c2015-03-26 13:43:05 +0100184 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
Joerg Roedel938c4702015-03-26 13:43:04 +0100185 void (*domain_free)(struct iommu_domain *);
186
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100187 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
188 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
Joerg Roedel67651782010-01-21 16:32:27 +0100189 int (*map)(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200190 phys_addr_t paddr, size_t size, int prot);
191 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
192 size_t size);
Olav Haugan315786e2014-10-25 09:55:16 -0700193 size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
194 struct scatterlist *sg, unsigned int nents, int prot);
Varun Sethibb5547ac2013-03-29 01:23:58 +0530195 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700196 phys_addr_t (*iova_to_phys_hard)(struct iommu_domain *domain,
197 dma_addr_t iova);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600198 int (*add_device)(struct device *dev);
199 void (*remove_device)(struct device *dev);
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200200 struct iommu_group *(*device_group)(struct device *dev);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100201 int (*domain_get_attr)(struct iommu_domain *domain,
202 enum iommu_attr attr, void *data);
203 int (*domain_set_attr)(struct iommu_domain *domain,
204 enum iommu_attr attr, void *data);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100205
Joerg Roedela1015c22015-05-28 18:41:33 +0200206 /* Request/Free a list of direct mapping requirements for a device */
207 void (*get_dm_regions)(struct device *dev, struct list_head *list);
208 void (*put_dm_regions)(struct device *dev, struct list_head *list);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200209 void (*apply_dm_region)(struct device *dev, struct iommu_domain *domain,
210 struct iommu_dm_region *region);
Joerg Roedela1015c22015-05-28 18:41:33 +0200211
Joerg Roedeld7787d52013-01-29 14:26:20 +0100212 /* Window handling functions */
213 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530214 phys_addr_t paddr, u64 size, int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100215 void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900216 /* Set the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100217 int (*domain_set_windows)(struct iommu_domain *domain, u32 w_count);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900218 /* Get the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100219 u32 (*domain_get_windows)(struct iommu_domain *domain);
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700220 void (*trigger_fault)(struct iommu_domain *domain, unsigned long flags);
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700221 unsigned long (*reg_read)(struct iommu_domain *domain,
222 unsigned long offset);
223 void (*reg_write)(struct iommu_domain *domain, unsigned long val,
224 unsigned long offset);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100225
Will Deacond0f60a42014-08-27 16:15:59 +0100226 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
Will Deacond0f60a42014-08-27 16:15:59 +0100227
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200228 unsigned long pgsize_bitmap;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100229};
230
Alex Williamsond72e31c2012-05-30 14:18:53 -0600231#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
232#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
233#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
234#define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
235#define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
236#define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
237
Thierry Redingb22f6432014-06-27 09:03:12 +0200238extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
Joerg Roedela1b60c12011-09-06 18:46:34 +0200239extern bool iommu_present(struct bus_type *bus);
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200240extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
Joerg Roedel905d66c2011-09-06 16:03:26 +0200241extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100242extern struct iommu_group *iommu_group_get_by_id(int id);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100243extern void iommu_domain_free(struct iommu_domain *domain);
244extern int iommu_attach_device(struct iommu_domain *domain,
245 struct device *dev);
246extern void iommu_detach_device(struct iommu_domain *domain,
247 struct device *dev);
Joerg Roedel2c1296d2015-05-28 18:41:32 +0200248extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
Mitchel Humpherysfad25922015-05-01 17:13:21 -0700249extern size_t iommu_pgsize(unsigned long pgsize_bitmap,
250 unsigned long addr_merge, size_t size);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100251extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200252 phys_addr_t paddr, size_t size, int prot);
253extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
254 size_t size);
Olav Haugan315786e2014-10-25 09:55:16 -0700255extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
256 struct scatterlist *sg,unsigned int nents,
257 int prot);
Varun Sethibb5547ac2013-03-29 01:23:58 +0530258extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700259extern phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
260 dma_addr_t iova);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400261extern void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300262 iommu_fault_handler_t handler, void *token);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600263
Joerg Roedela1015c22015-05-28 18:41:33 +0200264extern void iommu_get_dm_regions(struct device *dev, struct list_head *list);
265extern void iommu_put_dm_regions(struct device *dev, struct list_head *list);
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200266extern int iommu_request_dm_for_dev(struct device *dev);
Joerg Roedela1015c22015-05-28 18:41:33 +0200267
Alex Williamsond72e31c2012-05-30 14:18:53 -0600268extern int iommu_attach_group(struct iommu_domain *domain,
269 struct iommu_group *group);
270extern void iommu_detach_group(struct iommu_domain *domain,
271 struct iommu_group *group);
272extern struct iommu_group *iommu_group_alloc(void);
273extern void *iommu_group_get_iommudata(struct iommu_group *group);
274extern void iommu_group_set_iommudata(struct iommu_group *group,
275 void *iommu_data,
276 void (*release)(void *iommu_data));
277extern int iommu_group_set_name(struct iommu_group *group, const char *name);
278extern int iommu_group_add_device(struct iommu_group *group,
279 struct device *dev);
280extern void iommu_group_remove_device(struct device *dev);
281extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
282 int (*fn)(struct device *, void *));
283extern struct iommu_group *iommu_group_get(struct device *dev);
284extern void iommu_group_put(struct iommu_group *group);
285extern int iommu_group_register_notifier(struct iommu_group *group,
286 struct notifier_block *nb);
287extern int iommu_group_unregister_notifier(struct iommu_group *group,
288 struct notifier_block *nb);
289extern int iommu_group_id(struct iommu_group *group);
Alex Williamson104a1c12014-07-03 09:51:18 -0600290extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
Joerg Roedel6827ca82015-05-28 18:41:35 +0200291extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400292
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100293extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
294 void *data);
295extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
296 void *data);
Alex Williamsonc61959e2014-06-12 16:12:24 -0600297struct device *iommu_device_create(struct device *parent, void *drvdata,
298 const struct attribute_group **groups,
Nicolas Iooss8db14862015-07-17 16:23:42 -0700299 const char *fmt, ...) __printf(4, 5);
Alex Williamsonc61959e2014-06-12 16:12:24 -0600300void iommu_device_destroy(struct device *dev);
301int iommu_device_link(struct device *dev, struct device *link);
302void iommu_device_unlink(struct device *dev, struct device *link);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400303
Joerg Roedeld7787d52013-01-29 14:26:20 +0100304/* Window handling function prototypes */
305extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530306 phys_addr_t offset, u64 size,
307 int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100308extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400309/**
310 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
311 * @domain: the iommu domain where the fault has happened
312 * @dev: the device where the fault has happened
313 * @iova: the faulting address
314 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
315 *
316 * This function should be called by the low-level IOMMU implementations
317 * whenever IOMMU faults happen, to allow high-level users, that are
318 * interested in such events, to know about them.
319 *
320 * This event may be useful for several possible use cases:
321 * - mere logging of the event
322 * - dynamic TLB/PTE loading
323 * - if restarting of the faulting device is required
324 *
325 * Returns 0 on success and an appropriate error code otherwise (if dynamic
326 * PTE/TLB loading will one day be supported, implementations will be able
327 * to tell whether it succeeded or not according to this return value).
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400328 *
329 * Specifically, -ENOSYS is returned if a fault handler isn't installed
330 * (though fault handlers can also return -ENOSYS, in case they want to
331 * elicit the default behavior of the IOMMU drivers).
Sushmita Susheelendra5e36b912015-06-02 15:46:24 -0600332
333 * Client fault handler returns -EBUSY to signal to the IOMMU driver
334 * that the client will take responsibility for any further fault
335 * handling, including clearing fault status registers or retrying
336 * the faulting transaction.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400337 */
338static inline int report_iommu_fault(struct iommu_domain *domain,
339 struct device *dev, unsigned long iova, int flags)
340{
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400341 int ret = -ENOSYS;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400342
343 /*
344 * if upper layers showed interest and installed a fault handler,
345 * invoke it.
346 */
347 if (domain->handler)
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300348 ret = domain->handler(domain, dev, iova, flags,
349 domain->handler_token);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400350
Shuah Khan56fa4842013-09-24 15:21:20 -0600351 trace_io_page_fault(dev, iova, flags);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400352 return ret;
353}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100354
Olav Haugan315786e2014-10-25 09:55:16 -0700355static inline size_t iommu_map_sg(struct iommu_domain *domain,
356 unsigned long iova, struct scatterlist *sg,
357 unsigned int nents, int prot)
358{
359 return domain->ops->map_sg(domain, iova, sg, nents, prot);
360}
361
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700362extern void iommu_trigger_fault(struct iommu_domain *domain,
363 unsigned long flags);
364
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700365extern unsigned long iommu_reg_read(struct iommu_domain *domain,
366 unsigned long offset);
367extern void iommu_reg_write(struct iommu_domain *domain, unsigned long offset,
368 unsigned long val);
Joerg Roedel5e622922015-10-21 23:51:37 +0200369/* PCI device grouping function */
370extern struct iommu_group *pci_device_group(struct device *dev);
Joerg Roedel6eab5562015-10-21 23:51:38 +0200371/* Generic device grouping function */
372extern struct iommu_group *generic_device_group(struct device *dev);
Joerg Roedel5e622922015-10-21 23:51:37 +0200373
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100374#else /* CONFIG_IOMMU_API */
375
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200376struct iommu_ops {};
Alex Williamsond72e31c2012-05-30 14:18:53 -0600377struct iommu_group {};
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100378
Joerg Roedela1b60c12011-09-06 18:46:34 +0200379static inline bool iommu_present(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100380{
381 return false;
382}
383
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200384static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
385{
386 return false;
387}
388
Joerg Roedel905d66c2011-09-06 16:03:26 +0200389static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100390{
391 return NULL;
392}
393
Alexey Kardashevskiyb62dfd22013-11-21 17:41:14 +1100394static inline struct iommu_group *iommu_group_get_by_id(int id)
395{
396 return NULL;
397}
398
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100399static inline void iommu_domain_free(struct iommu_domain *domain)
400{
401}
402
403static inline int iommu_attach_device(struct iommu_domain *domain,
404 struct device *dev)
405{
406 return -ENODEV;
407}
408
409static inline void iommu_detach_device(struct iommu_domain *domain,
410 struct device *dev)
411{
412}
413
Joerg Roedel2c1296d2015-05-28 18:41:32 +0200414static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
415{
416 return NULL;
417}
418
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100419static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
420 phys_addr_t paddr, int gfp_order, int prot)
421{
422 return -ENODEV;
423}
424
425static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
426 int gfp_order)
427{
428 return -ENODEV;
429}
430
Olav Haugan315786e2014-10-25 09:55:16 -0700431static inline size_t iommu_map_sg(struct iommu_domain *domain,
432 unsigned long iova, struct scatterlist *sg,
433 unsigned int nents, int prot)
434{
435 return -ENODEV;
436}
437
Joerg Roedeld7787d52013-01-29 14:26:20 +0100438static inline int iommu_domain_window_enable(struct iommu_domain *domain,
439 u32 wnd_nr, phys_addr_t paddr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530440 u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +0100441{
442 return -ENODEV;
443}
444
445static inline void iommu_domain_window_disable(struct iommu_domain *domain,
446 u32 wnd_nr)
447{
448}
449
Varun Sethibb5547ac2013-03-29 01:23:58 +0530450static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100451{
452 return 0;
453}
454
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700455static inline phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
456 dma_addr_t iova)
457{
458 return 0;
459}
460
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400461static inline void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300462 iommu_fault_handler_t handler, void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400463{
464}
465
Joerg Roedela1015c22015-05-28 18:41:33 +0200466static inline void iommu_get_dm_regions(struct device *dev,
467 struct list_head *list)
468{
469}
470
471static inline void iommu_put_dm_regions(struct device *dev,
472 struct list_head *list)
473{
474}
475
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200476static inline int iommu_request_dm_for_dev(struct device *dev)
477{
478 return -ENODEV;
479}
480
Alex Williamsonbef83de2012-09-24 21:23:25 -0600481static inline int iommu_attach_group(struct iommu_domain *domain,
482 struct iommu_group *group)
Alex Williamson14604322011-10-21 15:56:05 -0400483{
484 return -ENODEV;
485}
486
Alex Williamsonbef83de2012-09-24 21:23:25 -0600487static inline void iommu_detach_group(struct iommu_domain *domain,
488 struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600489{
490}
491
Alex Williamsonbef83de2012-09-24 21:23:25 -0600492static inline struct iommu_group *iommu_group_alloc(void)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600493{
494 return ERR_PTR(-ENODEV);
495}
496
Alex Williamsonbef83de2012-09-24 21:23:25 -0600497static inline void *iommu_group_get_iommudata(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600498{
499 return NULL;
500}
501
Alex Williamsonbef83de2012-09-24 21:23:25 -0600502static inline void iommu_group_set_iommudata(struct iommu_group *group,
503 void *iommu_data,
504 void (*release)(void *iommu_data))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600505{
506}
507
Alex Williamsonbef83de2012-09-24 21:23:25 -0600508static inline int iommu_group_set_name(struct iommu_group *group,
509 const char *name)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600510{
511 return -ENODEV;
512}
513
Alex Williamsonbef83de2012-09-24 21:23:25 -0600514static inline int iommu_group_add_device(struct iommu_group *group,
515 struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600516{
517 return -ENODEV;
518}
519
Alex Williamsonbef83de2012-09-24 21:23:25 -0600520static inline void iommu_group_remove_device(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600521{
522}
523
Alex Williamsonbef83de2012-09-24 21:23:25 -0600524static inline int iommu_group_for_each_dev(struct iommu_group *group,
525 void *data,
526 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600527{
528 return -ENODEV;
529}
530
Alex Williamsonbef83de2012-09-24 21:23:25 -0600531static inline struct iommu_group *iommu_group_get(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600532{
533 return NULL;
534}
535
Alex Williamsonbef83de2012-09-24 21:23:25 -0600536static inline void iommu_group_put(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600537{
538}
539
Alex Williamsonbef83de2012-09-24 21:23:25 -0600540static inline int iommu_group_register_notifier(struct iommu_group *group,
541 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600542{
543 return -ENODEV;
544}
545
Alex Williamsonbef83de2012-09-24 21:23:25 -0600546static inline int iommu_group_unregister_notifier(struct iommu_group *group,
547 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600548{
549 return 0;
550}
551
Alex Williamsonbef83de2012-09-24 21:23:25 -0600552static inline int iommu_group_id(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600553{
554 return -ENODEV;
555}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100556
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100557static inline int iommu_domain_get_attr(struct iommu_domain *domain,
558 enum iommu_attr attr, void *data)
559{
560 return -EINVAL;
561}
562
563static inline int iommu_domain_set_attr(struct iommu_domain *domain,
564 enum iommu_attr attr, void *data)
565{
566 return -EINVAL;
567}
568
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600569static inline struct device *iommu_device_create(struct device *parent,
570 void *drvdata,
571 const struct attribute_group **groups,
572 const char *fmt, ...)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600573{
574 return ERR_PTR(-ENODEV);
575}
576
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600577static inline void iommu_device_destroy(struct device *dev)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600578{
579}
580
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600581static inline int iommu_device_link(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600582{
583 return -EINVAL;
584}
585
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600586static inline void iommu_device_unlink(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600587{
588}
589
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700590static inline void iommu_trigger_fault(struct iommu_domain *domain,
591 unsigned long flags)
592{
593}
594
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700595static inline unsigned long iommu_reg_read(struct iommu_domain *domain,
596 unsigned long offset)
597{
598 return 0;
599}
600
601static inline void iommu_reg_write(struct iommu_domain *domain,
602 unsigned long val, unsigned long offset)
603{
604}
605
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100606#endif /* CONFIG_IOMMU_API */
607
608#endif /* __LINUX_IOMMU_H */