blob: f25acfc932486b7d9e4b4397261dd580f8eca3dd [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 Roedelb578e4b2017-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>
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)
Patrick Daly23301482017-10-12 16:18:25 -070035#define IOMMU_GUARD (1 << 28) /* Guard Page */
Patrick Dalya0c45d42016-11-03 13:28:24 -070036/* Use upstream device's bus attribute */
37#define IOMMU_USE_UPSTREAM_HINT (1 << 6)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010038
Joerg Roedel905d66c2011-09-06 16:03:26 +020039struct iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -060040struct iommu_group;
Joerg Roedelff217762011-08-26 16:48:26 +020041struct bus_type;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010042struct device;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040043struct iommu_domain;
Joerg Roedelba1eabfa2012-08-03 15:55:41 +020044struct notifier_block;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040045
46/* iommu fault flags */
Sushmita Susheelendra5e36b912015-06-02 15:46:24 -060047#define IOMMU_FAULT_READ (1 << 0)
48#define IOMMU_FAULT_WRITE (1 << 1)
49#define IOMMU_FAULT_TRANSLATION (1 << 2)
50#define IOMMU_FAULT_PERMISSION (1 << 3)
Mitchel Humpherys0cc54a22015-08-19 10:57:55 -070051#define IOMMU_FAULT_EXTERNAL (1 << 4)
52#define IOMMU_FAULT_TRANSACTION_STALLED (1 << 5)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040053
54typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +030055 struct device *, unsigned long, int, void *);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010056
Joerg Roedel0ff64f82012-01-26 19:40:53 +010057struct iommu_domain_geometry {
58 dma_addr_t aperture_start; /* First address that can be mapped */
59 dma_addr_t aperture_end; /* Last address that can be mapped */
60 bool force_aperture; /* DMA only allowed in mappable range? */
61};
62
Mitchel Humpherys3679a502016-02-12 14:10:31 -080063struct iommu_pgtbl_info {
64 void *pmds;
65};
66
Joerg Roedel8539c7c2015-03-26 13:43:05 +010067/* Domain feature flags */
68#define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */
69#define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API
70 implementation */
71#define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */
72
73/*
74 * This are the possible domain-types
75 *
76 * IOMMU_DOMAIN_BLOCKED - All DMA is blocked, can be used to isolate
77 * devices
78 * IOMMU_DOMAIN_IDENTITY - DMA addresses are system physical addresses
79 * IOMMU_DOMAIN_UNMANAGED - DMA mappings managed by IOMMU-API user, used
80 * for VMs
81 * IOMMU_DOMAIN_DMA - Internally used for DMA-API implementations.
82 * This flag allows IOMMU drivers to implement
83 * certain optimizations for these domains
84 */
85#define IOMMU_DOMAIN_BLOCKED (0U)
86#define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)
87#define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING)
88#define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \
89 __IOMMU_DOMAIN_DMA_API)
90
Liam Mark599835a2015-06-05 17:10:00 -070091
92#define IOMMU_DOMAIN_NAME_LEN 32
Joerg Roedel4a77a6c2008-11-26 17:02:33 +010093struct iommu_domain {
Joerg Roedel8539c7c2015-03-26 13:43:05 +010094 unsigned type;
Thierry Redingb22f6432014-06-27 09:03:12 +020095 const struct iommu_ops *ops;
Robin Murphyd16e0fa2016-04-07 18:42:06 +010096 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -040097 iommu_fault_handler_t handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +030098 void *handler_token;
Joerg Roedel0ff64f82012-01-26 19:40:53 +010099 struct iommu_domain_geometry geometry;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100100 void *iova_cookie;
Liam Mark599835a2015-06-05 17:10:00 -0700101 char name[IOMMU_DOMAIN_NAME_LEN];
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100102};
103
Joerg Roedel1aed0742014-09-03 18:34:04 +0200104enum iommu_cap {
105 IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA
106 transactions */
107 IOMMU_CAP_INTR_REMAP, /* IOMMU supports interrupt isolation */
Antonios Motakisc4986642014-10-13 14:06:17 +0100108 IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */
Joerg Roedel1aed0742014-09-03 18:34:04 +0200109};
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800110
Varun Sethi7cabf492013-07-15 10:20:56 +0530111/*
112 * Following constraints are specifc to FSL_PAMUV1:
113 * -aperture must be power of 2, and naturally aligned
114 * -number of windows must be power of 2, and address space size
115 * of each window is determined by aperture size / # of windows
116 * -the actual size of the mapped region of a window must be power
117 * of 2 starting with 4KB and physical address must be naturally
118 * aligned.
119 * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
120 * The caller can invoke iommu_domain_get_attr to check if the underlying
121 * iommu implementation supports these constraints.
122 */
123
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100124enum iommu_attr {
Joerg Roedel0ff64f82012-01-26 19:40:53 +0100125 DOMAIN_ATTR_GEOMETRY,
Joerg Roedeld2e12162013-01-29 13:49:04 +0100126 DOMAIN_ATTR_PAGING,
Joerg Roedel69356712013-02-04 14:00:01 +0100127 DOMAIN_ATTR_WINDOWS,
Varun Sethi7cabf492013-07-15 10:20:56 +0530128 DOMAIN_ATTR_FSL_PAMU_STASH,
129 DOMAIN_ATTR_FSL_PAMU_ENABLE,
130 DOMAIN_ATTR_FSL_PAMUV1,
Will Deaconc02607a2014-09-29 10:05:06 -0600131 DOMAIN_ATTR_NESTING, /* two stages of translation */
Mitchel Humpherys19f19d52014-11-24 12:31:02 -0800132 DOMAIN_ATTR_PT_BASE_ADDR,
Jeremy Gebbene352bb02015-06-16 10:54:01 -0600133 DOMAIN_ATTR_CONTEXT_BANK,
Jeremy Gebben1b34ee12015-07-10 16:43:23 -0600134 DOMAIN_ATTR_DYNAMIC,
Jeremy Gebbene70da582015-07-10 16:43:22 -0600135 DOMAIN_ATTR_TTBR0,
136 DOMAIN_ATTR_CONTEXTIDR,
137 DOMAIN_ATTR_PROCID,
Mitchel Humpheryse78b0a52015-09-25 17:26:31 -0700138 DOMAIN_ATTR_NON_FATAL_FAULTS,
Patrick Dalybb82f962016-03-10 15:25:51 -0800139 DOMAIN_ATTR_S1_BYPASS,
Patrick Daly023ffedb2016-07-06 17:39:30 -0700140 DOMAIN_ATTR_ATOMIC,
Patrick Daly67be3692016-07-08 15:09:41 -0700141 DOMAIN_ATTR_SECURE_VMID,
Mitchel Humpherys60581e12016-02-12 13:53:20 -0800142 DOMAIN_ATTR_FAST,
Mitchel Humpherys3679a502016-02-12 14:10:31 -0800143 DOMAIN_ATTR_PGTBL_INFO,
Patrick Daly0b37c5a2016-11-09 13:29:11 -0800144 DOMAIN_ATTR_USE_UPSTREAM_HINT,
Patrick Dalyef6c1dc2016-11-16 14:35:23 -0800145 DOMAIN_ATTR_EARLY_MAP,
Mitchel Humpherys67ec6272016-06-07 14:55:50 -0700146 DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT,
Liam Mark07edfbf2016-12-20 11:35:44 -0800147 DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT,
Charan Teja Reddyc682e472017-04-20 19:11:20 +0530148 DOMAIN_ATTR_CB_STALL_DISABLE,
Patrick Daly1e279922017-09-06 15:57:45 -0700149 DOMAIN_ATTR_UPSTREAM_IOVA_ALLOCATOR,
Patrick Daly83174c12017-10-26 12:31:15 -0700150 DOMAIN_ATTR_MMU500_ERRATA_MIN_ALIGN,
Joerg Roedela8b8a88a2013-01-29 14:36:31 +0100151 DOMAIN_ATTR_MAX,
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100152};
153
Joerg Roedela1015c22015-05-28 18:41:33 +0200154/**
155 * struct iommu_dm_region - descriptor for a direct mapped memory region
156 * @list: Linked list pointers
157 * @start: System physical start address of the region
158 * @length: Length of the region in bytes
159 * @prot: IOMMU Protection flags (READ/WRITE/...)
160 */
161struct iommu_dm_region {
162 struct list_head list;
163 phys_addr_t start;
164 size_t length;
165 int prot;
166};
167
Mitchel Humpherysc75ae492015-07-15 18:27:36 -0700168extern struct dentry *iommu_debugfs_top;
169
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200170#ifdef CONFIG_IOMMU_API
171
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200172/**
173 * struct iommu_ops - iommu ops and capabilities
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900174 * @capable: check capability
175 * @domain_alloc: allocate iommu domain
176 * @domain_free: free iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200177 * @attach_dev: attach device to an iommu domain
178 * @detach_dev: detach device from an iommu domain
179 * @map: map a physically contiguous memory region to an iommu domain
180 * @unmap: unmap a physically contiguous memory region from an iommu domain
Olav Haugan315786e2014-10-25 09:55:16 -0700181 * @map_sg: map a scatter-gather list of physically contiguous memory chunks
182 * to an iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200183 * @iova_to_phys: translate iova to physical address
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700184 * @iova_to_phys_hard: translate iova to physical address using IOMMU hardware
Alex Williamsond72e31c2012-05-30 14:18:53 -0600185 * @add_device: add device to iommu grouping
186 * @remove_device: remove device from iommu grouping
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900187 * @device_group: find iommu group for a particular device
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100188 * @domain_get_attr: Query domain attributes
189 * @domain_set_attr: Change domain attributes
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900190 * @get_dm_regions: Request list of direct mapping requirements for a device
191 * @put_dm_regions: Free list of direct mapping requirements for a device
Joerg Roedel33b21a62016-07-05 13:07:53 +0200192 * @apply_dm_region: Temporary helper call-back for iova reserved ranges
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900193 * @domain_window_enable: Configure and enable a particular window for a domain
194 * @domain_window_disable: Disable a particular window for a domain
195 * @domain_set_windows: Set the number of windows for a domain
196 * @domain_get_windows: Return the number of windows for a domain
Will Deacond0f60a42014-08-27 16:15:59 +0100197 * @of_xlate: add OF master IDs to iommu grouping
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100198 * @pgsize_bitmap: bitmap of all possible supported page sizes
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700199 * @trigger_fault: trigger a fault on the device attached to an iommu domain
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700200 * @reg_read: read an IOMMU register
201 * @reg_write: write an IOMMU register
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800202 * @tlbi_domain: Invalidate all TLBs covering an iommu domain
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800203 * @enable_config_clocks: Enable all config clocks for this domain's IOMMU
204 * @disable_config_clocks: Disable all config clocks for this domain's IOMMU
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200205 */
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100206struct iommu_ops {
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200207 bool (*capable)(enum iommu_cap);
Joerg Roedel938c4702015-03-26 13:43:04 +0100208
209 /* Domain allocation and freeing by the iommu driver */
Joerg Roedel8539c7c2015-03-26 13:43:05 +0100210 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
Joerg Roedel938c4702015-03-26 13:43:04 +0100211 void (*domain_free)(struct iommu_domain *);
212
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100213 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
214 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
Joerg Roedel67651782010-01-21 16:32:27 +0100215 int (*map)(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200216 phys_addr_t paddr, size_t size, int prot);
217 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
218 size_t size);
Olav Haugan315786e2014-10-25 09:55:16 -0700219 size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
220 struct scatterlist *sg, unsigned int nents, int prot);
Varun Sethibb5547ac2013-03-29 01:23:58 +0530221 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700222 phys_addr_t (*iova_to_phys_hard)(struct iommu_domain *domain,
223 dma_addr_t iova);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600224 int (*add_device)(struct device *dev);
225 void (*remove_device)(struct device *dev);
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200226 struct iommu_group *(*device_group)(struct device *dev);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100227 int (*domain_get_attr)(struct iommu_domain *domain,
228 enum iommu_attr attr, void *data);
229 int (*domain_set_attr)(struct iommu_domain *domain,
230 enum iommu_attr attr, void *data);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100231
Joerg Roedela1015c22015-05-28 18:41:33 +0200232 /* Request/Free a list of direct mapping requirements for a device */
233 void (*get_dm_regions)(struct device *dev, struct list_head *list);
234 void (*put_dm_regions)(struct device *dev, struct list_head *list);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200235 void (*apply_dm_region)(struct device *dev, struct iommu_domain *domain,
236 struct iommu_dm_region *region);
Joerg Roedela1015c22015-05-28 18:41:33 +0200237
Joerg Roedeld7787d52013-01-29 14:26:20 +0100238 /* Window handling functions */
239 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530240 phys_addr_t paddr, u64 size, int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100241 void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900242 /* Set the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100243 int (*domain_set_windows)(struct iommu_domain *domain, u32 w_count);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900244 /* Get the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100245 u32 (*domain_get_windows)(struct iommu_domain *domain);
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700246 void (*trigger_fault)(struct iommu_domain *domain, unsigned long flags);
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800247 void (*tlbi_domain)(struct iommu_domain *domain);
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800248 int (*enable_config_clocks)(struct iommu_domain *domain);
249 void (*disable_config_clocks)(struct iommu_domain *domain);
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -0700250 uint64_t (*iova_to_pte)(struct iommu_domain *domain,
251 dma_addr_t iova);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100252
Will Deacond0f60a42014-08-27 16:15:59 +0100253 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
Will Deacond0f60a42014-08-27 16:15:59 +0100254
Liam Mark5bb64222016-12-09 14:36:07 -0800255 bool (*is_iova_coherent)(struct iommu_domain *domain, dma_addr_t iova);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200256 unsigned long pgsize_bitmap;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100257};
258
Alex Williamsond72e31c2012-05-30 14:18:53 -0600259#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
260#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
261#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
262#define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
263#define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
264#define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
265
Thierry Redingb22f6432014-06-27 09:03:12 +0200266extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
Joerg Roedela1b60c12011-09-06 18:46:34 +0200267extern bool iommu_present(struct bus_type *bus);
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200268extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
Joerg Roedel905d66c2011-09-06 16:03:26 +0200269extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100270extern struct iommu_group *iommu_group_get_by_id(int id);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100271extern void iommu_domain_free(struct iommu_domain *domain);
272extern int iommu_attach_device(struct iommu_domain *domain,
273 struct device *dev);
274extern void iommu_detach_device(struct iommu_domain *domain,
275 struct device *dev);
Joerg Roedel2c1296d2015-05-28 18:41:32 +0200276extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
Mitchel Humpherysfad25922015-05-01 17:13:21 -0700277extern size_t iommu_pgsize(unsigned long pgsize_bitmap,
278 unsigned long addr_merge, size_t size);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100279extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200280 phys_addr_t paddr, size_t size, int prot);
281extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
282 size_t size);
Liam Mark599835a2015-06-05 17:10:00 -0700283extern size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
284 struct scatterlist *sg, unsigned int nents,
285 int prot);
Olav Haugan315786e2014-10-25 09:55:16 -0700286extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
287 struct scatterlist *sg,unsigned int nents,
288 int prot);
Varun Sethibb5547ac2013-03-29 01:23:58 +0530289extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700290extern phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
291 dma_addr_t iova);
Liam Mark5bb64222016-12-09 14:36:07 -0800292extern bool iommu_is_iova_coherent(struct iommu_domain *domain,
293 dma_addr_t iova);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400294extern void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300295 iommu_fault_handler_t handler, void *token);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600296
Joerg Roedela1015c22015-05-28 18:41:33 +0200297extern void iommu_get_dm_regions(struct device *dev, struct list_head *list);
298extern void iommu_put_dm_regions(struct device *dev, struct list_head *list);
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200299extern int iommu_request_dm_for_dev(struct device *dev);
Joerg Roedela1015c22015-05-28 18:41:33 +0200300
Alex Williamsond72e31c2012-05-30 14:18:53 -0600301extern int iommu_attach_group(struct iommu_domain *domain,
302 struct iommu_group *group);
303extern void iommu_detach_group(struct iommu_domain *domain,
304 struct iommu_group *group);
305extern struct iommu_group *iommu_group_alloc(void);
306extern void *iommu_group_get_iommudata(struct iommu_group *group);
307extern void iommu_group_set_iommudata(struct iommu_group *group,
308 void *iommu_data,
309 void (*release)(void *iommu_data));
310extern int iommu_group_set_name(struct iommu_group *group, const char *name);
311extern int iommu_group_add_device(struct iommu_group *group,
312 struct device *dev);
313extern void iommu_group_remove_device(struct device *dev);
314extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
315 int (*fn)(struct device *, void *));
316extern struct iommu_group *iommu_group_get(struct device *dev);
317extern void iommu_group_put(struct iommu_group *group);
318extern int iommu_group_register_notifier(struct iommu_group *group,
319 struct notifier_block *nb);
320extern int iommu_group_unregister_notifier(struct iommu_group *group,
321 struct notifier_block *nb);
322extern int iommu_group_id(struct iommu_group *group);
Alex Williamson104a1c12014-07-03 09:51:18 -0600323extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
Joerg Roedel6827ca82015-05-28 18:41:35 +0200324extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400325
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100326extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
327 void *data);
328extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
329 void *data);
Alex Williamsonc61959e2014-06-12 16:12:24 -0600330struct device *iommu_device_create(struct device *parent, void *drvdata,
331 const struct attribute_group **groups,
Nicolas Iooss8db14862015-07-17 16:23:42 -0700332 const char *fmt, ...) __printf(4, 5);
Alex Williamsonc61959e2014-06-12 16:12:24 -0600333void iommu_device_destroy(struct device *dev);
334int iommu_device_link(struct device *dev, struct device *link);
335void iommu_device_unlink(struct device *dev, struct device *link);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400336
Joerg Roedeld7787d52013-01-29 14:26:20 +0100337/* Window handling function prototypes */
338extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530339 phys_addr_t offset, u64 size,
340 int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100341extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -0700342
343extern uint64_t iommu_iova_to_pte(struct iommu_domain *domain,
344 dma_addr_t iova);
Sushmita Susheelendra5e36b912015-06-02 15:46:24 -0600345
Joerg Roedel2f4ae01d2017-04-26 15:39:28 +0200346extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
347 unsigned long iova, int flags);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100348
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700349extern void iommu_trigger_fault(struct iommu_domain *domain,
350 unsigned long flags);
351
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700352extern unsigned long iommu_reg_read(struct iommu_domain *domain,
353 unsigned long offset);
354extern void iommu_reg_write(struct iommu_domain *domain, unsigned long offset,
355 unsigned long val);
Joerg Roedel5e622922015-10-21 23:51:37 +0200356/* PCI device grouping function */
357extern struct iommu_group *pci_device_group(struct device *dev);
Joerg Roedel6eab5562015-10-21 23:51:38 +0200358/* Generic device grouping function */
359extern struct iommu_group *generic_device_group(struct device *dev);
Joerg Roedel5e622922015-10-21 23:51:37 +0200360
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800361static inline void iommu_tlbiall(struct iommu_domain *domain)
362{
363 if (domain->ops->tlbi_domain)
364 domain->ops->tlbi_domain(domain);
365}
366
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800367static inline int iommu_enable_config_clocks(struct iommu_domain *domain)
368{
369 if (domain->ops->enable_config_clocks)
370 return domain->ops->enable_config_clocks(domain);
371 return 0;
372}
373
374static inline void iommu_disable_config_clocks(struct iommu_domain *domain)
375{
376 if (domain->ops->disable_config_clocks)
377 domain->ops->disable_config_clocks(domain);
378}
379
Robin Murphy57f98d22016-09-13 10:54:14 +0100380/**
381 * struct iommu_fwspec - per-device IOMMU instance data
382 * @ops: ops for this device's IOMMU
383 * @iommu_fwnode: firmware handle for this device's IOMMU
384 * @iommu_priv: IOMMU driver private data for this device
385 * @num_ids: number of associated device IDs
386 * @ids: IDs which this device may present to the IOMMU
387 */
388struct iommu_fwspec {
389 const struct iommu_ops *ops;
390 struct fwnode_handle *iommu_fwnode;
391 void *iommu_priv;
392 unsigned int num_ids;
393 u32 ids[1];
394};
395
396int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
397 const struct iommu_ops *ops);
398void iommu_fwspec_free(struct device *dev);
399int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);
Patrick Daly1e3c16c2017-04-12 17:59:07 -0700400int iommu_fwspec_get_id(struct device *dev, u32 *id);
Patrick Daly958973c2017-04-28 19:45:18 -0700401int iommu_is_available(struct device *dev);
Robin Murphy57f98d22016-09-13 10:54:14 +0100402
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100403#else /* CONFIG_IOMMU_API */
404
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200405struct iommu_ops {};
Alex Williamsond72e31c2012-05-30 14:18:53 -0600406struct iommu_group {};
Robin Murphy57f98d22016-09-13 10:54:14 +0100407struct iommu_fwspec {};
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100408
Joerg Roedela1b60c12011-09-06 18:46:34 +0200409static inline bool iommu_present(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100410{
411 return false;
412}
413
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200414static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
415{
416 return false;
417}
418
Joerg Roedel905d66c2011-09-06 16:03:26 +0200419static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100420{
421 return NULL;
422}
423
Alexey Kardashevskiyb62dfd22013-11-21 17:41:14 +1100424static inline struct iommu_group *iommu_group_get_by_id(int id)
425{
426 return NULL;
427}
428
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100429static inline void iommu_domain_free(struct iommu_domain *domain)
430{
431}
432
433static inline int iommu_attach_device(struct iommu_domain *domain,
434 struct device *dev)
435{
436 return -ENODEV;
437}
438
439static inline void iommu_detach_device(struct iommu_domain *domain,
440 struct device *dev)
441{
442}
443
Joerg Roedel2c1296d2015-05-28 18:41:32 +0200444static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
445{
446 return NULL;
447}
448
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100449static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
450 phys_addr_t paddr, int gfp_order, int prot)
451{
452 return -ENODEV;
453}
454
455static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
456 int gfp_order)
457{
458 return -ENODEV;
459}
460
Olav Haugan315786e2014-10-25 09:55:16 -0700461static inline size_t iommu_map_sg(struct iommu_domain *domain,
462 unsigned long iova, struct scatterlist *sg,
463 unsigned int nents, int prot)
464{
465 return -ENODEV;
466}
467
Joerg Roedeld7787d52013-01-29 14:26:20 +0100468static inline int iommu_domain_window_enable(struct iommu_domain *domain,
469 u32 wnd_nr, phys_addr_t paddr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530470 u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +0100471{
472 return -ENODEV;
473}
474
475static inline void iommu_domain_window_disable(struct iommu_domain *domain,
476 u32 wnd_nr)
477{
478}
479
Varun Sethibb5547ac2013-03-29 01:23:58 +0530480static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100481{
482 return 0;
483}
484
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700485static inline phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
486 dma_addr_t iova)
487{
488 return 0;
489}
490
Liam Mark5bb64222016-12-09 14:36:07 -0800491static inline bool iommu_is_iova_coherent(struct iommu_domain *domain,
492 dma_addr_t iova)
493{
494 return 0;
495}
496
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400497static inline void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300498 iommu_fault_handler_t handler, void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400499{
500}
501
Joerg Roedela1015c22015-05-28 18:41:33 +0200502static inline void iommu_get_dm_regions(struct device *dev,
503 struct list_head *list)
504{
505}
506
507static inline void iommu_put_dm_regions(struct device *dev,
508 struct list_head *list)
509{
510}
511
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200512static inline int iommu_request_dm_for_dev(struct device *dev)
513{
514 return -ENODEV;
515}
516
Alex Williamsonbef83de2012-09-24 21:23:25 -0600517static inline int iommu_attach_group(struct iommu_domain *domain,
518 struct iommu_group *group)
Alex Williamson14604322011-10-21 15:56:05 -0400519{
520 return -ENODEV;
521}
522
Alex Williamsonbef83de2012-09-24 21:23:25 -0600523static inline void iommu_detach_group(struct iommu_domain *domain,
524 struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600525{
526}
527
Alex Williamsonbef83de2012-09-24 21:23:25 -0600528static inline struct iommu_group *iommu_group_alloc(void)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600529{
530 return ERR_PTR(-ENODEV);
531}
532
Alex Williamsonbef83de2012-09-24 21:23:25 -0600533static inline void *iommu_group_get_iommudata(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600534{
535 return NULL;
536}
537
Alex Williamsonbef83de2012-09-24 21:23:25 -0600538static inline void iommu_group_set_iommudata(struct iommu_group *group,
539 void *iommu_data,
540 void (*release)(void *iommu_data))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600541{
542}
543
Alex Williamsonbef83de2012-09-24 21:23:25 -0600544static inline int iommu_group_set_name(struct iommu_group *group,
545 const char *name)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600546{
547 return -ENODEV;
548}
549
Alex Williamsonbef83de2012-09-24 21:23:25 -0600550static inline int iommu_group_add_device(struct iommu_group *group,
551 struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600552{
553 return -ENODEV;
554}
555
Alex Williamsonbef83de2012-09-24 21:23:25 -0600556static inline void iommu_group_remove_device(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600557{
558}
559
Alex Williamsonbef83de2012-09-24 21:23:25 -0600560static inline int iommu_group_for_each_dev(struct iommu_group *group,
561 void *data,
562 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600563{
564 return -ENODEV;
565}
566
Alex Williamsonbef83de2012-09-24 21:23:25 -0600567static inline struct iommu_group *iommu_group_get(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600568{
569 return NULL;
570}
571
Alex Williamsonbef83de2012-09-24 21:23:25 -0600572static inline void iommu_group_put(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600573{
574}
575
Alex Williamsonbef83de2012-09-24 21:23:25 -0600576static inline int iommu_group_register_notifier(struct iommu_group *group,
577 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600578{
579 return -ENODEV;
580}
581
Alex Williamsonbef83de2012-09-24 21:23:25 -0600582static inline int iommu_group_unregister_notifier(struct iommu_group *group,
583 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600584{
585 return 0;
586}
587
Alex Williamsonbef83de2012-09-24 21:23:25 -0600588static inline int iommu_group_id(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600589{
590 return -ENODEV;
591}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100592
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100593static inline int iommu_domain_get_attr(struct iommu_domain *domain,
594 enum iommu_attr attr, void *data)
595{
596 return -EINVAL;
597}
598
599static inline int iommu_domain_set_attr(struct iommu_domain *domain,
600 enum iommu_attr attr, void *data)
601{
602 return -EINVAL;
603}
604
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600605static inline struct device *iommu_device_create(struct device *parent,
606 void *drvdata,
607 const struct attribute_group **groups,
608 const char *fmt, ...)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600609{
610 return ERR_PTR(-ENODEV);
611}
612
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600613static inline void iommu_device_destroy(struct device *dev)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600614{
615}
616
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600617static inline int iommu_device_link(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600618{
619 return -EINVAL;
620}
621
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600622static inline void iommu_device_unlink(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600623{
624}
625
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700626static inline void iommu_trigger_fault(struct iommu_domain *domain,
627 unsigned long flags)
628{
629}
630
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700631static inline unsigned long iommu_reg_read(struct iommu_domain *domain,
632 unsigned long offset)
633{
634 return 0;
635}
636
637static inline void iommu_reg_write(struct iommu_domain *domain,
638 unsigned long val, unsigned long offset)
639{
640}
641
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800642static inline void iommu_tlbiall(struct iommu_domain *domain)
643{
644}
645
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800646static inline int iommu_enable_config_clocks(struct iommu_domain *domain)
647{
648 return 0;
649}
650
651static inline void iommu_disable_config_clocks(struct iommu_domain *domain)
652{
653}
654
Robin Murphy57f98d22016-09-13 10:54:14 +0100655static inline int iommu_fwspec_init(struct device *dev,
656 struct fwnode_handle *iommu_fwnode,
657 const struct iommu_ops *ops)
658{
659 return -ENODEV;
660}
661
662static inline void iommu_fwspec_free(struct device *dev)
663{
664}
665
666static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids,
667 int num_ids)
668{
669 return -ENODEV;
670}
671
Runmin Wang8cfebef2017-04-28 13:59:58 -0700672static inline int iommu_fwspec_get_id(struct device *dev, u32 *id)
Patrick Daly1e3c16c2017-04-12 17:59:07 -0700673{
674 return -ENODEV;
675}
676
Patrick Daly958973c2017-04-28 19:45:18 -0700677static inline int iommu_is_available(struct device *dev)
678{
679 return -ENODEV;
680}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100681#endif /* CONFIG_IOMMU_API */
682
683#endif /* __LINUX_IOMMU_H */