blob: acbc605d509bc203b0bec1fed7725e603d578e5a [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,
Prakash Guptac2e909a2018-03-29 11:23:06 +0530151 DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE,
Joerg Roedela8b8a88a2013-01-29 14:36:31 +0100152 DOMAIN_ATTR_MAX,
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100153};
154
Joerg Roedela1015c22015-05-28 18:41:33 +0200155/**
156 * struct iommu_dm_region - descriptor for a direct mapped memory region
157 * @list: Linked list pointers
158 * @start: System physical start address of the region
159 * @length: Length of the region in bytes
160 * @prot: IOMMU Protection flags (READ/WRITE/...)
161 */
162struct iommu_dm_region {
163 struct list_head list;
164 phys_addr_t start;
165 size_t length;
166 int prot;
167};
168
Mitchel Humpherysc75ae492015-07-15 18:27:36 -0700169extern struct dentry *iommu_debugfs_top;
170
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200171#ifdef CONFIG_IOMMU_API
172
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200173/**
174 * struct iommu_ops - iommu ops and capabilities
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900175 * @capable: check capability
176 * @domain_alloc: allocate iommu domain
177 * @domain_free: free iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200178 * @attach_dev: attach device to an iommu domain
179 * @detach_dev: detach device from an iommu domain
180 * @map: map a physically contiguous memory region to an iommu domain
181 * @unmap: unmap a physically contiguous memory region from an iommu domain
Olav Haugan315786e2014-10-25 09:55:16 -0700182 * @map_sg: map a scatter-gather list of physically contiguous memory chunks
183 * to an iommu domain
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200184 * @iova_to_phys: translate iova to physical address
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700185 * @iova_to_phys_hard: translate iova to physical address using IOMMU hardware
Alex Williamsond72e31c2012-05-30 14:18:53 -0600186 * @add_device: add device to iommu grouping
187 * @remove_device: remove device from iommu grouping
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900188 * @device_group: find iommu group for a particular device
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100189 * @domain_get_attr: Query domain attributes
190 * @domain_set_attr: Change domain attributes
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900191 * @get_dm_regions: Request list of direct mapping requirements for a device
192 * @put_dm_regions: Free list of direct mapping requirements for a device
Joerg Roedel33b21a62016-07-05 13:07:53 +0200193 * @apply_dm_region: Temporary helper call-back for iova reserved ranges
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900194 * @domain_window_enable: Configure and enable a particular window for a domain
195 * @domain_window_disable: Disable a particular window for a domain
196 * @domain_set_windows: Set the number of windows for a domain
197 * @domain_get_windows: Return the number of windows for a domain
Will Deacond0f60a42014-08-27 16:15:59 +0100198 * @of_xlate: add OF master IDs to iommu grouping
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100199 * @pgsize_bitmap: bitmap of all possible supported page sizes
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700200 * @trigger_fault: trigger a fault on the device attached to an iommu domain
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700201 * @reg_read: read an IOMMU register
202 * @reg_write: write an IOMMU register
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800203 * @tlbi_domain: Invalidate all TLBs covering an iommu domain
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800204 * @enable_config_clocks: Enable all config clocks for this domain's IOMMU
205 * @disable_config_clocks: Disable all config clocks for this domain's IOMMU
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200206 */
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100207struct iommu_ops {
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200208 bool (*capable)(enum iommu_cap);
Joerg Roedel938c4702015-03-26 13:43:04 +0100209
210 /* Domain allocation and freeing by the iommu driver */
Joerg Roedel8539c7c2015-03-26 13:43:05 +0100211 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
Joerg Roedel938c4702015-03-26 13:43:04 +0100212 void (*domain_free)(struct iommu_domain *);
213
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100214 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
215 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
Joerg Roedel67651782010-01-21 16:32:27 +0100216 int (*map)(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200217 phys_addr_t paddr, size_t size, int prot);
218 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
219 size_t size);
Olav Haugan315786e2014-10-25 09:55:16 -0700220 size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
221 struct scatterlist *sg, unsigned int nents, int prot);
Varun Sethibb5547ac2013-03-29 01:23:58 +0530222 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700223 phys_addr_t (*iova_to_phys_hard)(struct iommu_domain *domain,
224 dma_addr_t iova);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600225 int (*add_device)(struct device *dev);
226 void (*remove_device)(struct device *dev);
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200227 struct iommu_group *(*device_group)(struct device *dev);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100228 int (*domain_get_attr)(struct iommu_domain *domain,
229 enum iommu_attr attr, void *data);
230 int (*domain_set_attr)(struct iommu_domain *domain,
231 enum iommu_attr attr, void *data);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100232
Joerg Roedela1015c22015-05-28 18:41:33 +0200233 /* Request/Free a list of direct mapping requirements for a device */
234 void (*get_dm_regions)(struct device *dev, struct list_head *list);
235 void (*put_dm_regions)(struct device *dev, struct list_head *list);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200236 void (*apply_dm_region)(struct device *dev, struct iommu_domain *domain,
237 struct iommu_dm_region *region);
Joerg Roedela1015c22015-05-28 18:41:33 +0200238
Joerg Roedeld7787d52013-01-29 14:26:20 +0100239 /* Window handling functions */
240 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530241 phys_addr_t paddr, u64 size, int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100242 void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900243 /* Set the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100244 int (*domain_set_windows)(struct iommu_domain *domain, u32 w_count);
Magnus Damm0d9bacb2016-01-19 14:28:48 +0900245 /* Get the number of windows per domain */
Joerg Roedel69356712013-02-04 14:00:01 +0100246 u32 (*domain_get_windows)(struct iommu_domain *domain);
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700247 void (*trigger_fault)(struct iommu_domain *domain, unsigned long flags);
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800248 void (*tlbi_domain)(struct iommu_domain *domain);
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800249 int (*enable_config_clocks)(struct iommu_domain *domain);
250 void (*disable_config_clocks)(struct iommu_domain *domain);
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -0700251 uint64_t (*iova_to_pte)(struct iommu_domain *domain,
252 dma_addr_t iova);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100253
Will Deacond0f60a42014-08-27 16:15:59 +0100254 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
Will Deacond0f60a42014-08-27 16:15:59 +0100255
Liam Mark5bb64222016-12-09 14:36:07 -0800256 bool (*is_iova_coherent)(struct iommu_domain *domain, dma_addr_t iova);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200257 unsigned long pgsize_bitmap;
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100258};
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);
Mitchel Humpherysfad25922015-05-01 17:13:21 -0700278extern size_t iommu_pgsize(unsigned long pgsize_bitmap,
279 unsigned long addr_merge, size_t size);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100280extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200281 phys_addr_t paddr, size_t size, int prot);
282extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
283 size_t size);
Liam Mark599835a2015-06-05 17:10:00 -0700284extern size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
285 struct scatterlist *sg, unsigned int nents,
286 int prot);
Olav Haugan315786e2014-10-25 09:55:16 -0700287extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
288 struct scatterlist *sg,unsigned int nents,
289 int prot);
Varun Sethibb5547ac2013-03-29 01:23:58 +0530290extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700291extern phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
292 dma_addr_t iova);
Liam Mark5bb64222016-12-09 14:36:07 -0800293extern bool iommu_is_iova_coherent(struct iommu_domain *domain,
294 dma_addr_t iova);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400295extern void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300296 iommu_fault_handler_t handler, void *token);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600297
Joerg Roedela1015c22015-05-28 18:41:33 +0200298extern void iommu_get_dm_regions(struct device *dev, struct list_head *list);
299extern void iommu_put_dm_regions(struct device *dev, struct list_head *list);
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200300extern int iommu_request_dm_for_dev(struct device *dev);
Joerg Roedela1015c22015-05-28 18:41:33 +0200301
Alex Williamsond72e31c2012-05-30 14:18:53 -0600302extern int iommu_attach_group(struct iommu_domain *domain,
303 struct iommu_group *group);
304extern void iommu_detach_group(struct iommu_domain *domain,
305 struct iommu_group *group);
306extern struct iommu_group *iommu_group_alloc(void);
307extern void *iommu_group_get_iommudata(struct iommu_group *group);
308extern void iommu_group_set_iommudata(struct iommu_group *group,
309 void *iommu_data,
310 void (*release)(void *iommu_data));
311extern int iommu_group_set_name(struct iommu_group *group, const char *name);
312extern int iommu_group_add_device(struct iommu_group *group,
313 struct device *dev);
314extern void iommu_group_remove_device(struct device *dev);
315extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
316 int (*fn)(struct device *, void *));
317extern struct iommu_group *iommu_group_get(struct device *dev);
318extern void iommu_group_put(struct iommu_group *group);
319extern int iommu_group_register_notifier(struct iommu_group *group,
320 struct notifier_block *nb);
321extern int iommu_group_unregister_notifier(struct iommu_group *group,
322 struct notifier_block *nb);
323extern int iommu_group_id(struct iommu_group *group);
Alex Williamson104a1c12014-07-03 09:51:18 -0600324extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
Joerg Roedel6827ca82015-05-28 18:41:35 +0200325extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400326
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100327extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
328 void *data);
329extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
330 void *data);
Alex Williamsonc61959e2014-06-12 16:12:24 -0600331struct device *iommu_device_create(struct device *parent, void *drvdata,
332 const struct attribute_group **groups,
Nicolas Iooss8db14862015-07-17 16:23:42 -0700333 const char *fmt, ...) __printf(4, 5);
Alex Williamsonc61959e2014-06-12 16:12:24 -0600334void iommu_device_destroy(struct device *dev);
335int iommu_device_link(struct device *dev, struct device *link);
336void iommu_device_unlink(struct device *dev, struct device *link);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400337
Joerg Roedeld7787d52013-01-29 14:26:20 +0100338/* Window handling function prototypes */
339extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530340 phys_addr_t offset, u64 size,
341 int prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +0100342extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -0700343
344extern uint64_t iommu_iova_to_pte(struct iommu_domain *domain,
345 dma_addr_t iova);
Sushmita Susheelendra5e36b912015-06-02 15:46:24 -0600346
Joerg Roedel2f4ae01d2017-04-26 15:39:28 +0200347extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
348 unsigned long iova, int flags);
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100349
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700350extern void iommu_trigger_fault(struct iommu_domain *domain,
351 unsigned long flags);
352
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700353extern unsigned long iommu_reg_read(struct iommu_domain *domain,
354 unsigned long offset);
355extern void iommu_reg_write(struct iommu_domain *domain, unsigned long offset,
356 unsigned long val);
Joerg Roedel5e622922015-10-21 23:51:37 +0200357/* PCI device grouping function */
358extern struct iommu_group *pci_device_group(struct device *dev);
Joerg Roedel6eab5562015-10-21 23:51:38 +0200359/* Generic device grouping function */
360extern struct iommu_group *generic_device_group(struct device *dev);
Joerg Roedel5e622922015-10-21 23:51:37 +0200361
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800362static inline void iommu_tlbiall(struct iommu_domain *domain)
363{
364 if (domain->ops->tlbi_domain)
365 domain->ops->tlbi_domain(domain);
366}
367
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800368static inline int iommu_enable_config_clocks(struct iommu_domain *domain)
369{
370 if (domain->ops->enable_config_clocks)
371 return domain->ops->enable_config_clocks(domain);
372 return 0;
373}
374
375static inline void iommu_disable_config_clocks(struct iommu_domain *domain)
376{
377 if (domain->ops->disable_config_clocks)
378 domain->ops->disable_config_clocks(domain);
379}
380
Robin Murphy57f98d22016-09-13 10:54:14 +0100381/**
382 * struct iommu_fwspec - per-device IOMMU instance data
383 * @ops: ops for this device's IOMMU
384 * @iommu_fwnode: firmware handle for this device's IOMMU
385 * @iommu_priv: IOMMU driver private data for this device
386 * @num_ids: number of associated device IDs
387 * @ids: IDs which this device may present to the IOMMU
388 */
389struct iommu_fwspec {
390 const struct iommu_ops *ops;
391 struct fwnode_handle *iommu_fwnode;
392 void *iommu_priv;
393 unsigned int num_ids;
394 u32 ids[1];
395};
396
397int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
398 const struct iommu_ops *ops);
399void iommu_fwspec_free(struct device *dev);
400int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);
Patrick Daly1e3c16c2017-04-12 17:59:07 -0700401int iommu_fwspec_get_id(struct device *dev, u32 *id);
Patrick Daly958973c2017-04-28 19:45:18 -0700402int iommu_is_available(struct device *dev);
Robin Murphy57f98d22016-09-13 10:54:14 +0100403
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100404#else /* CONFIG_IOMMU_API */
405
Joerg Roedel39d4ebb2011-09-06 16:48:40 +0200406struct iommu_ops {};
Alex Williamsond72e31c2012-05-30 14:18:53 -0600407struct iommu_group {};
Robin Murphy57f98d22016-09-13 10:54:14 +0100408struct iommu_fwspec {};
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100409
Joerg Roedela1b60c12011-09-06 18:46:34 +0200410static inline bool iommu_present(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100411{
412 return false;
413}
414
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200415static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
416{
417 return false;
418}
419
Joerg Roedel905d66c2011-09-06 16:03:26 +0200420static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100421{
422 return NULL;
423}
424
Alexey Kardashevskiyb62dfd22013-11-21 17:41:14 +1100425static inline struct iommu_group *iommu_group_get_by_id(int id)
426{
427 return NULL;
428}
429
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100430static inline void iommu_domain_free(struct iommu_domain *domain)
431{
432}
433
434static inline int iommu_attach_device(struct iommu_domain *domain,
435 struct device *dev)
436{
437 return -ENODEV;
438}
439
440static inline void iommu_detach_device(struct iommu_domain *domain,
441 struct device *dev)
442{
443}
444
Joerg Roedel2c1296d2015-05-28 18:41:32 +0200445static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
446{
447 return NULL;
448}
449
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100450static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
451 phys_addr_t paddr, int gfp_order, int prot)
452{
453 return -ENODEV;
454}
455
456static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
457 int gfp_order)
458{
459 return -ENODEV;
460}
461
Olav Haugan315786e2014-10-25 09:55:16 -0700462static inline size_t iommu_map_sg(struct iommu_domain *domain,
463 unsigned long iova, struct scatterlist *sg,
464 unsigned int nents, int prot)
465{
466 return -ENODEV;
467}
468
Joerg Roedeld7787d52013-01-29 14:26:20 +0100469static inline int iommu_domain_window_enable(struct iommu_domain *domain,
470 u32 wnd_nr, phys_addr_t paddr,
Varun Sethi80f97f02013-03-29 01:24:00 +0530471 u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +0100472{
473 return -ENODEV;
474}
475
476static inline void iommu_domain_window_disable(struct iommu_domain *domain,
477 u32 wnd_nr)
478{
479}
480
Varun Sethibb5547ac2013-03-29 01:23:58 +0530481static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100482{
483 return 0;
484}
485
Mitchel Humpherys36b8c322015-07-06 15:24:22 -0700486static inline phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
487 dma_addr_t iova)
488{
489 return 0;
490}
491
Liam Mark5bb64222016-12-09 14:36:07 -0800492static inline bool iommu_is_iova_coherent(struct iommu_domain *domain,
493 dma_addr_t iova)
494{
495 return 0;
496}
497
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400498static inline void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300499 iommu_fault_handler_t handler, void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400500{
501}
502
Joerg Roedela1015c22015-05-28 18:41:33 +0200503static inline void iommu_get_dm_regions(struct device *dev,
504 struct list_head *list)
505{
506}
507
508static inline void iommu_put_dm_regions(struct device *dev,
509 struct list_head *list)
510{
511}
512
Joerg Roedeld290f1e2015-05-28 18:41:36 +0200513static inline int iommu_request_dm_for_dev(struct device *dev)
514{
515 return -ENODEV;
516}
517
Alex Williamsonbef83de2012-09-24 21:23:25 -0600518static inline int iommu_attach_group(struct iommu_domain *domain,
519 struct iommu_group *group)
Alex Williamson14604322011-10-21 15:56:05 -0400520{
521 return -ENODEV;
522}
523
Alex Williamsonbef83de2012-09-24 21:23:25 -0600524static inline void iommu_detach_group(struct iommu_domain *domain,
525 struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600526{
527}
528
Alex Williamsonbef83de2012-09-24 21:23:25 -0600529static inline struct iommu_group *iommu_group_alloc(void)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600530{
531 return ERR_PTR(-ENODEV);
532}
533
Alex Williamsonbef83de2012-09-24 21:23:25 -0600534static inline void *iommu_group_get_iommudata(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600535{
536 return NULL;
537}
538
Alex Williamsonbef83de2012-09-24 21:23:25 -0600539static inline void iommu_group_set_iommudata(struct iommu_group *group,
540 void *iommu_data,
541 void (*release)(void *iommu_data))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600542{
543}
544
Alex Williamsonbef83de2012-09-24 21:23:25 -0600545static inline int iommu_group_set_name(struct iommu_group *group,
546 const char *name)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600547{
548 return -ENODEV;
549}
550
Alex Williamsonbef83de2012-09-24 21:23:25 -0600551static inline int iommu_group_add_device(struct iommu_group *group,
552 struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600553{
554 return -ENODEV;
555}
556
Alex Williamsonbef83de2012-09-24 21:23:25 -0600557static inline void iommu_group_remove_device(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600558{
559}
560
Alex Williamsonbef83de2012-09-24 21:23:25 -0600561static inline int iommu_group_for_each_dev(struct iommu_group *group,
562 void *data,
563 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600564{
565 return -ENODEV;
566}
567
Alex Williamsonbef83de2012-09-24 21:23:25 -0600568static inline struct iommu_group *iommu_group_get(struct device *dev)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600569{
570 return NULL;
571}
572
Alex Williamsonbef83de2012-09-24 21:23:25 -0600573static inline void iommu_group_put(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600574{
575}
576
Alex Williamsonbef83de2012-09-24 21:23:25 -0600577static inline int iommu_group_register_notifier(struct iommu_group *group,
578 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600579{
580 return -ENODEV;
581}
582
Alex Williamsonbef83de2012-09-24 21:23:25 -0600583static inline int iommu_group_unregister_notifier(struct iommu_group *group,
584 struct notifier_block *nb)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600585{
586 return 0;
587}
588
Alex Williamsonbef83de2012-09-24 21:23:25 -0600589static inline int iommu_group_id(struct iommu_group *group)
Alex Williamsond72e31c2012-05-30 14:18:53 -0600590{
591 return -ENODEV;
592}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100593
Joerg Roedel0cd76dd2012-01-26 19:40:52 +0100594static inline int iommu_domain_get_attr(struct iommu_domain *domain,
595 enum iommu_attr attr, void *data)
596{
597 return -EINVAL;
598}
599
600static inline int iommu_domain_set_attr(struct iommu_domain *domain,
601 enum iommu_attr attr, void *data)
602{
603 return -EINVAL;
604}
605
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600606static inline struct device *iommu_device_create(struct device *parent,
607 void *drvdata,
608 const struct attribute_group **groups,
609 const char *fmt, ...)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600610{
611 return ERR_PTR(-ENODEV);
612}
613
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600614static inline void iommu_device_destroy(struct device *dev)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600615{
616}
617
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600618static inline int iommu_device_link(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600619{
620 return -EINVAL;
621}
622
Alex Williamsone09f8ea52014-07-07 14:31:36 -0600623static inline void iommu_device_unlink(struct device *dev, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600624{
625}
626
Mitchel Humpherys8488df22015-07-09 16:59:02 -0700627static inline void iommu_trigger_fault(struct iommu_domain *domain,
628 unsigned long flags)
629{
630}
631
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -0700632static inline unsigned long iommu_reg_read(struct iommu_domain *domain,
633 unsigned long offset)
634{
635 return 0;
636}
637
638static inline void iommu_reg_write(struct iommu_domain *domain,
639 unsigned long val, unsigned long offset)
640{
641}
642
Mitchel Humpherys39bad0c2015-12-03 11:17:23 -0800643static inline void iommu_tlbiall(struct iommu_domain *domain)
644{
645}
646
Mitchel Humpherys4264f192015-12-14 16:04:46 -0800647static inline int iommu_enable_config_clocks(struct iommu_domain *domain)
648{
649 return 0;
650}
651
652static inline void iommu_disable_config_clocks(struct iommu_domain *domain)
653{
654}
655
Robin Murphy57f98d22016-09-13 10:54:14 +0100656static inline int iommu_fwspec_init(struct device *dev,
657 struct fwnode_handle *iommu_fwnode,
658 const struct iommu_ops *ops)
659{
660 return -ENODEV;
661}
662
663static inline void iommu_fwspec_free(struct device *dev)
664{
665}
666
667static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids,
668 int num_ids)
669{
670 return -ENODEV;
671}
672
Runmin Wang8cfebef2017-04-28 13:59:58 -0700673static inline int iommu_fwspec_get_id(struct device *dev, u32 *id)
Patrick Daly1e3c16c2017-04-12 17:59:07 -0700674{
675 return -ENODEV;
676}
677
Patrick Daly958973c2017-04-28 19:45:18 -0700678static inline int iommu_is_available(struct device *dev)
679{
680 return -ENODEV;
681}
Joerg Roedel4a77a6c2008-11-26 17:02:33 +0100682#endif /* CONFIG_IOMMU_API */
683
684#endif /* __LINUX_IOMMU_H */