145051539SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24a77a6cfSJoerg Roedel /*
34a77a6cfSJoerg Roedel * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
44a77a6cfSJoerg Roedel * Author: Joerg Roedel <joerg.roedel@amd.com>
54a77a6cfSJoerg Roedel */
64a77a6cfSJoerg Roedel
74a77a6cfSJoerg Roedel #ifndef __LINUX_IOMMU_H
84a77a6cfSJoerg Roedel #define __LINUX_IOMMU_H
94a77a6cfSJoerg Roedel
10e8245c1bSJoerg Roedel #include <linux/scatterlist.h>
11e8245c1bSJoerg Roedel #include <linux/device.h>
12e8245c1bSJoerg Roedel #include <linux/types.h>
1374315cccSLaura Abbott #include <linux/errno.h>
149a08d376SWang YanQing #include <linux/err.h>
15d0f60a44SWill Deacon #include <linux/of.h>
164e32348bSJacob Pan #include <uapi/linux/iommu.h>
1774315cccSLaura Abbott
18ca13bb3dSWill Deacon #define IOMMU_READ (1 << 0)
19ca13bb3dSWill Deacon #define IOMMU_WRITE (1 << 1)
20ca13bb3dSWill Deacon #define IOMMU_CACHE (1 << 2) /* DMA cache coherency */
21a720b41cSAntonios Motakis #define IOMMU_NOEXEC (1 << 3)
2231e6850eSRobin Murphy #define IOMMU_MMIO (1 << 4) /* e.g. things like MSI doorbells */
23579b2a65SMitchel Humpherys /*
24adf5e516SRobin Murphy * Where the bus hardware includes a privilege level as part of its access type
25adf5e516SRobin Murphy * markings, and certain devices are capable of issuing transactions marked as
26adf5e516SRobin Murphy * either 'supervisor' or 'user', the IOMMU_PRIV flag requests that the other
27adf5e516SRobin Murphy * given permission flags only apply to accesses at the higher privilege level,
28adf5e516SRobin Murphy * and that unprivileged transactions should have as little access as possible.
29adf5e516SRobin Murphy * This would usually imply the same permissions as kernel mappings on the CPU,
30adf5e516SRobin Murphy * if the IOMMU page table format is equivalent.
31579b2a65SMitchel Humpherys */
32579b2a65SMitchel Humpherys #define IOMMU_PRIV (1 << 5)
334a77a6cfSJoerg Roedel
34905d66c1SJoerg Roedel struct iommu_ops;
35d72e31c9SAlex Williamson struct iommu_group;
36ff21776dSJoerg Roedel struct bus_type;
374a77a6cfSJoerg Roedel struct device;
384f3f8d9dSOhad Ben-Cohen struct iommu_domain;
399a630a4bSLu Baolu struct iommu_domain_ops;
40ba1eabfaSJoerg Roedel struct notifier_block;
4126b25a2bSJean-Philippe Brucker struct iommu_sva;
424e32348bSJacob Pan struct iommu_fault_event;
4346983fcdSRobin Murphy struct iommu_dma_cookie;
444f3f8d9dSOhad Ben-Cohen
454f3f8d9dSOhad Ben-Cohen /* iommu fault flags */
464f3f8d9dSOhad Ben-Cohen #define IOMMU_FAULT_READ 0x0
474f3f8d9dSOhad Ben-Cohen #define IOMMU_FAULT_WRITE 0x1
484f3f8d9dSOhad Ben-Cohen
494f3f8d9dSOhad Ben-Cohen typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
5077ca2332SOhad Ben-Cohen struct device *, unsigned long, int, void *);
514e32348bSJacob Pan typedef int (*iommu_dev_fault_handler_t)(struct iommu_fault *, void *);
524a77a6cfSJoerg Roedel
530ff64f80SJoerg Roedel struct iommu_domain_geometry {
540ff64f80SJoerg Roedel dma_addr_t aperture_start; /* First address that can be mapped */
550ff64f80SJoerg Roedel dma_addr_t aperture_end; /* Last address that can be mapped */
560ff64f80SJoerg Roedel bool force_aperture; /* DMA only allowed in mappable range? */
570ff64f80SJoerg Roedel };
580ff64f80SJoerg Roedel
598539c7c1SJoerg Roedel /* Domain feature flags */
608539c7c1SJoerg Roedel #define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */
618539c7c1SJoerg Roedel #define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API
628539c7c1SJoerg Roedel implementation */
638539c7c1SJoerg Roedel #define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */
64bf3aed46SRobin Murphy #define __IOMMU_DOMAIN_DMA_FQ (1U << 3) /* DMA-API uses flush queue */
658539c7c1SJoerg Roedel
6613646796SLu Baolu #define __IOMMU_DOMAIN_SVA (1U << 4) /* Shared process address space */
6713646796SLu Baolu
68a4fdd976SRobin Murphy #define IOMMU_DOMAIN_ALLOC_FLAGS ~__IOMMU_DOMAIN_DMA_FQ
698539c7c1SJoerg Roedel /*
708539c7c1SJoerg Roedel * This are the possible domain-types
718539c7c1SJoerg Roedel *
728539c7c1SJoerg Roedel * IOMMU_DOMAIN_BLOCKED - All DMA is blocked, can be used to isolate
738539c7c1SJoerg Roedel * devices
748539c7c1SJoerg Roedel * IOMMU_DOMAIN_IDENTITY - DMA addresses are system physical addresses
758539c7c1SJoerg Roedel * IOMMU_DOMAIN_UNMANAGED - DMA mappings managed by IOMMU-API user, used
768539c7c1SJoerg Roedel * for VMs
778539c7c1SJoerg Roedel * IOMMU_DOMAIN_DMA - Internally used for DMA-API implementations.
788539c7c1SJoerg Roedel * This flag allows IOMMU drivers to implement
798539c7c1SJoerg Roedel * certain optimizations for these domains
80bf3aed46SRobin Murphy * IOMMU_DOMAIN_DMA_FQ - As above, but definitely using batched TLB
81bf3aed46SRobin Murphy * invalidation.
8213646796SLu Baolu * IOMMU_DOMAIN_SVA - DMA addresses are shared process addresses
8313646796SLu Baolu * represented by mm_struct's.
848539c7c1SJoerg Roedel */
858539c7c1SJoerg Roedel #define IOMMU_DOMAIN_BLOCKED (0U)
868539c7c1SJoerg Roedel #define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)
878539c7c1SJoerg Roedel #define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING)
888539c7c1SJoerg Roedel #define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \
898539c7c1SJoerg Roedel __IOMMU_DOMAIN_DMA_API)
90bf3aed46SRobin Murphy #define IOMMU_DOMAIN_DMA_FQ (__IOMMU_DOMAIN_PAGING | \
91bf3aed46SRobin Murphy __IOMMU_DOMAIN_DMA_API | \
92bf3aed46SRobin Murphy __IOMMU_DOMAIN_DMA_FQ)
9313646796SLu Baolu #define IOMMU_DOMAIN_SVA (__IOMMU_DOMAIN_SVA)
948539c7c1SJoerg Roedel
954a77a6cfSJoerg Roedel struct iommu_domain {
968539c7c1SJoerg Roedel unsigned type;
979a630a4bSLu Baolu const struct iommu_domain_ops *ops;
98d16e0faaSRobin Murphy unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
990ff64f80SJoerg Roedel struct iommu_domain_geometry geometry;
10046983fcdSRobin Murphy struct iommu_dma_cookie *iova_cookie;
1018cc93159SLu Baolu enum iommu_page_response_code (*iopf_handler)(struct iommu_fault *fault,
1028cc93159SLu Baolu void *data);
1038cc93159SLu Baolu void *fault_data;
10413646796SLu Baolu union {
10513646796SLu Baolu struct {
10613646796SLu Baolu iommu_fault_handler_t handler;
10713646796SLu Baolu void *handler_token;
10813646796SLu Baolu };
10913646796SLu Baolu struct { /* IOMMU_DOMAIN_SVA */
11013646796SLu Baolu struct mm_struct *mm;
11113646796SLu Baolu int users;
11213646796SLu Baolu };
11313646796SLu Baolu };
1144a77a6cfSJoerg Roedel };
1154a77a6cfSJoerg Roedel
iommu_is_dma_domain(struct iommu_domain * domain)116bf3aed46SRobin Murphy static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
117bf3aed46SRobin Murphy {
118bf3aed46SRobin Murphy return domain->type & __IOMMU_DOMAIN_DMA_API;
119bf3aed46SRobin Murphy }
120bf3aed46SRobin Murphy
1211aed0748SJoerg Roedel enum iommu_cap {
122f78dc1daSJason Gunthorpe IOMMU_CAP_CACHE_COHERENCY, /* IOMMU_CACHE is supported */
123c4986649SAntonios Motakis IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */
124d0be55fbSRobin Murphy IOMMU_CAP_PRE_BOOT_PROTECTION, /* Firmware says it used the IOMMU for
125d0be55fbSRobin Murphy DMA protection and we should too */
1264989764dSJason Gunthorpe /*
1274989764dSJason Gunthorpe * Per-device flag indicating if enforce_cache_coherency() will work on
1284989764dSJason Gunthorpe * this device.
1294989764dSJason Gunthorpe */
1304989764dSJason Gunthorpe IOMMU_CAP_ENFORCE_CACHE_COHERENCY,
1314a20ce0fSRobin Murphy /*
1324a20ce0fSRobin Murphy * IOMMU driver does not issue TLB maintenance during .unmap, so can
1334a20ce0fSRobin Murphy * usefully support the non-strict DMA flush queue.
1344a20ce0fSRobin Murphy */
1354a20ce0fSRobin Murphy IOMMU_CAP_DEFERRED_FLUSH,
1361aed0748SJoerg Roedel };
137dbb9fd86SSheng Yang
138d30ddcaaSEric Auger /* These are the possible reserved region types */
1399d3a4de4SRobin Murphy enum iommu_resv_type {
1409d3a4de4SRobin Murphy /* Memory regions which must be mapped 1:1 at all times */
1419d3a4de4SRobin Murphy IOMMU_RESV_DIRECT,
142adfd3738SEric Auger /*
143adfd3738SEric Auger * Memory regions which are advertised to be 1:1 but are
144adfd3738SEric Auger * commonly considered relaxable in some conditions,
145adfd3738SEric Auger * for instance in device assignment use case (USB, Graphics)
146adfd3738SEric Auger */
147adfd3738SEric Auger IOMMU_RESV_DIRECT_RELAXABLE,
1489d3a4de4SRobin Murphy /* Arbitrary "never map this or give it to a device" address ranges */
1499d3a4de4SRobin Murphy IOMMU_RESV_RESERVED,
1509d3a4de4SRobin Murphy /* Hardware MSI region (untranslated) */
1519d3a4de4SRobin Murphy IOMMU_RESV_MSI,
1529d3a4de4SRobin Murphy /* Software-managed MSI translation window */
1539d3a4de4SRobin Murphy IOMMU_RESV_SW_MSI,
1549d3a4de4SRobin Murphy };
155d30ddcaaSEric Auger
156a1015c2bSJoerg Roedel /**
157e5b5234aSEric Auger * struct iommu_resv_region - descriptor for a reserved memory region
158a1015c2bSJoerg Roedel * @list: Linked list pointers
159a1015c2bSJoerg Roedel * @start: System physical start address of the region
160a1015c2bSJoerg Roedel * @length: Length of the region in bytes
161a1015c2bSJoerg Roedel * @prot: IOMMU Protection flags (READ/WRITE/...)
162d30ddcaaSEric Auger * @type: Type of the reserved region
1633b7e2482SShameer Kolothum * @free: Callback to free associated memory allocations
164a1015c2bSJoerg Roedel */
165e5b5234aSEric Auger struct iommu_resv_region {
166a1015c2bSJoerg Roedel struct list_head list;
167a1015c2bSJoerg Roedel phys_addr_t start;
168a1015c2bSJoerg Roedel size_t length;
169a1015c2bSJoerg Roedel int prot;
1709d3a4de4SRobin Murphy enum iommu_resv_type type;
1713b7e2482SShameer Kolothum void (*free)(struct device *dev, struct iommu_resv_region *region);
172a1015c2bSJoerg Roedel };
173a1015c2bSJoerg Roedel
174491cf4a6SShameer Kolothum struct iommu_iort_rmr_data {
175491cf4a6SShameer Kolothum struct iommu_resv_region rr;
176491cf4a6SShameer Kolothum
177491cf4a6SShameer Kolothum /* Stream IDs associated with IORT RMR entry */
178491cf4a6SShameer Kolothum const u32 *sids;
179491cf4a6SShameer Kolothum u32 num_sids;
180491cf4a6SShameer Kolothum };
181491cf4a6SShameer Kolothum
18234b48c70SJean-Philippe Brucker /**
18334b48c70SJean-Philippe Brucker * enum iommu_dev_features - Per device IOMMU features
18434b48c70SJean-Philippe Brucker * @IOMMU_DEV_FEAT_SVA: Shared Virtual Addresses
18534b48c70SJean-Philippe Brucker * @IOMMU_DEV_FEAT_IOPF: I/O Page Faults such as PRI or Stall. Generally
18634b48c70SJean-Philippe Brucker * enabling %IOMMU_DEV_FEAT_SVA requires
18734b48c70SJean-Philippe Brucker * %IOMMU_DEV_FEAT_IOPF, but some devices manage I/O Page
18834b48c70SJean-Philippe Brucker * Faults themselves instead of relying on the IOMMU. When
18934b48c70SJean-Philippe Brucker * supported, this feature must be enabled before and
19034b48c70SJean-Philippe Brucker * disabled after %IOMMU_DEV_FEAT_SVA.
19134b48c70SJean-Philippe Brucker *
192309c56e8SChristoph Hellwig * Device drivers enable a feature using iommu_dev_enable_feature().
19334b48c70SJean-Philippe Brucker */
194a3a19592SLu Baolu enum iommu_dev_features {
19534b48c70SJean-Philippe Brucker IOMMU_DEV_FEAT_SVA,
19634b48c70SJean-Philippe Brucker IOMMU_DEV_FEAT_IOPF,
19726b25a2bSJean-Philippe Brucker };
19826b25a2bSJean-Philippe Brucker
19942987801SJacob Pan #define IOMMU_NO_PASID (0U) /* Reserved for DMA w/o PASID */
2002dcebc7dSJacob Pan #define IOMMU_FIRST_GLOBAL_PASID (1U) /*starting range for allocation */
20126b25a2bSJean-Philippe Brucker #define IOMMU_PASID_INVALID (-1U)
202cd389115SJacob Pan typedef unsigned int ioasid_t;
20326b25a2bSJean-Philippe Brucker
20439d4ebb9SJoerg Roedel #ifdef CONFIG_IOMMU_API
20539d4ebb9SJoerg Roedel
2067d3002ccSOhad Ben-Cohen /**
207a7d20dc1SWill Deacon * struct iommu_iotlb_gather - Range information for a pending IOTLB flush
208a7d20dc1SWill Deacon *
209a7d20dc1SWill Deacon * @start: IOVA representing the start of the range to be flushed
210862c3715SYong Wu * @end: IOVA representing the end of the range to be flushed (inclusive)
211a7d20dc1SWill Deacon * @pgsize: The interval at which to perform the flush
2127a7c5badSRobin Murphy * @freelist: Removed pages to free after sync
2137a7c5badSRobin Murphy * @queued: Indicates that the flush will be queued
214a7d20dc1SWill Deacon *
215a7d20dc1SWill Deacon * This structure is intended to be updated by multiple calls to the
216a7d20dc1SWill Deacon * ->unmap() function in struct iommu_ops before eventually being passed
2177a7c5badSRobin Murphy * into ->iotlb_sync(). Drivers can add pages to @freelist to be freed after
2187a7c5badSRobin Murphy * ->iotlb_sync() or ->iotlb_flush_all() have cleared all cached references to
2197a7c5badSRobin Murphy * them. @queued is set to indicate when ->iotlb_flush_all() will be called
2207a7c5badSRobin Murphy * later instead of ->iotlb_sync(), so drivers may optimise accordingly.
221a7d20dc1SWill Deacon */
222a7d20dc1SWill Deacon struct iommu_iotlb_gather {
223a7d20dc1SWill Deacon unsigned long start;
224a7d20dc1SWill Deacon unsigned long end;
225a7d20dc1SWill Deacon size_t pgsize;
22687f60cc6SMatthew Wilcox (Oracle) struct list_head freelist;
2277a7c5badSRobin Murphy bool queued;
228a7d20dc1SWill Deacon };
229a7d20dc1SWill Deacon
230a7d20dc1SWill Deacon /**
2317d3002ccSOhad Ben-Cohen * struct iommu_ops - iommu ops and capabilities
2320d9bacb6SMagnus Damm * @capable: check capability
23360fedb26SLu Baolu * @hw_info: report iommu hardware information. The data buffer returned by this
23460fedb26SLu Baolu * op is allocated in the iommu driver and freed by the caller after
23560fedb26SLu Baolu * use. The information type is one of enum iommu_hw_info_type defined
23660fedb26SLu Baolu * in include/uapi/linux/iommufd.h.
2370d9bacb6SMagnus Damm * @domain_alloc: allocate iommu domain
238a6a4c7e2SJoerg Roedel * @probe_device: Add device to iommu driver handling
239a6a4c7e2SJoerg Roedel * @release_device: Remove device from iommu driver handling
240a6a4c7e2SJoerg Roedel * @probe_finalize: Do final setup work after the device is added to an IOMMU
241a6a4c7e2SJoerg Roedel * group and attached to the groups domain
2426caeb33fSLu Baolu * @set_platform_dma_ops: Returning control back to the platform DMA ops. This op
2436caeb33fSLu Baolu * is to support old IOMMU drivers, new drivers should use
2446caeb33fSLu Baolu * default domains, and the common IOMMU DMA ops.
2450d9bacb6SMagnus Damm * @device_group: find iommu group for a particular device
246e5b5234aSEric Auger * @get_resv_regions: Request list of reserved regions for a device
247d0f60a44SWill Deacon * @of_xlate: add OF master IDs to iommu grouping
248a7055d57SGeert Uytterhoeven * @is_attach_deferred: Check if domain attach should be deferred from iommu
249a7055d57SGeert Uytterhoeven * driver init to device driver init (default no)
25028394501SYuan Can * @dev_enable/disable_feat: per device entries to enable/disable
251a3a19592SLu Baolu * iommu specific features.
252bf3255b3SJean-Philippe Brucker * @page_response: handle page request response
2534cbf3851SSai Praneeth Prakhya * @def_domain_type: device default domain type, return value:
2544cbf3851SSai Praneeth Prakhya * - IOMMU_DOMAIN_IDENTITY: must use an identity domain
2554cbf3851SSai Praneeth Prakhya * - IOMMU_DOMAIN_DMA: must use a dma domain
2564cbf3851SSai Praneeth Prakhya * - 0: use the default setting
2579a630a4bSLu Baolu * @default_domain_ops: the default ops for domains
25816603704SLu Baolu * @remove_dev_pasid: Remove any translation configurations of a specific
25916603704SLu Baolu * pasid, so that any DMA transactions with this pasid
26016603704SLu Baolu * will be blocked by the hardware.
26125f003deSWill Deacon * @pgsize_bitmap: bitmap of all possible supported page sizes
26225f003deSWill Deacon * @owner: Driver module providing these ops
2637d3002ccSOhad Ben-Cohen */
2644a77a6cfSJoerg Roedel struct iommu_ops {
265359ad157SRobin Murphy bool (*capable)(struct device *dev, enum iommu_cap);
26660fedb26SLu Baolu void *(*hw_info)(struct device *dev, u32 *length, u32 *type);
267938c4709SJoerg Roedel
268938c4709SJoerg Roedel /* Domain allocation and freeing by the iommu driver */
2698539c7c1SJoerg Roedel struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
270938c4709SJoerg Roedel
271a6a4c7e2SJoerg Roedel struct iommu_device *(*probe_device)(struct device *dev);
272a6a4c7e2SJoerg Roedel void (*release_device)(struct device *dev);
273a6a4c7e2SJoerg Roedel void (*probe_finalize)(struct device *dev);
2746caeb33fSLu Baolu void (*set_platform_dma_ops)(struct device *dev);
27546c6b2bcSJoerg Roedel struct iommu_group *(*device_group)(struct device *dev);
276d7787d57SJoerg Roedel
277e5b5234aSEric Auger /* Request/Free a list of reserved regions for a device */
278e5b5234aSEric Auger void (*get_resv_regions)(struct device *dev, struct list_head *list);
279a1015c2bSJoerg Roedel
280d0f60a44SWill Deacon int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
28141bb23e7SLu Baolu bool (*is_attach_deferred)(struct device *dev);
282d0f60a44SWill Deacon
283a3a19592SLu Baolu /* Per device IOMMU features */
284a3a19592SLu Baolu int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);
285a3a19592SLu Baolu int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);
286a3a19592SLu Baolu
287bf3255b3SJean-Philippe Brucker int (*page_response)(struct device *dev,
288bf3255b3SJean-Philippe Brucker struct iommu_fault_event *evt,
289bf3255b3SJean-Philippe Brucker struct iommu_page_response *msg);
290bf3255b3SJean-Philippe Brucker
2914cbf3851SSai Praneeth Prakhya int (*def_domain_type)(struct device *dev);
29216603704SLu Baolu void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid);
2934cbf3851SSai Praneeth Prakhya
2949a630a4bSLu Baolu const struct iommu_domain_ops *default_domain_ops;
2957d3002ccSOhad Ben-Cohen unsigned long pgsize_bitmap;
29625f003deSWill Deacon struct module *owner;
2974a77a6cfSJoerg Roedel };
2984a77a6cfSJoerg Roedel
299b0119e87SJoerg Roedel /**
3009a630a4bSLu Baolu * struct iommu_domain_ops - domain specific operations
3019a630a4bSLu Baolu * @attach_dev: attach an iommu domain to a device
30200208852SNicolin Chen * Return:
30300208852SNicolin Chen * * 0 - success
30400208852SNicolin Chen * * EINVAL - can indicate that device and domain are incompatible due to
30500208852SNicolin Chen * some previous configuration of the domain, in which case the
30600208852SNicolin Chen * driver shouldn't log an error, since it is legitimate for a
30700208852SNicolin Chen * caller to test reuse of existing domains. Otherwise, it may
30800208852SNicolin Chen * still represent some other fundamental problem
30900208852SNicolin Chen * * ENOMEM - out of memory
31000208852SNicolin Chen * * ENOSPC - non-ENOMEM type of resource allocation failures
31100208852SNicolin Chen * * EBUSY - device is attached to a domain and cannot be changed
31200208852SNicolin Chen * * ENODEV - device specific errors, not able to be attached
31300208852SNicolin Chen * * <others> - treated as ENODEV by the caller. Use is discouraged
31416603704SLu Baolu * @set_dev_pasid: set an iommu domain to a pasid of device
3159a630a4bSLu Baolu * @map: map a physically contiguous memory region to an iommu domain
3169a630a4bSLu Baolu * @map_pages: map a physically contiguous set of pages of the same size to
3179a630a4bSLu Baolu * an iommu domain.
3189a630a4bSLu Baolu * @unmap: unmap a physically contiguous memory region from an iommu domain
3199a630a4bSLu Baolu * @unmap_pages: unmap a number of pages of the same size from an iommu domain
3209a630a4bSLu Baolu * @flush_iotlb_all: Synchronously flush all hardware TLBs for this domain
3219a630a4bSLu Baolu * @iotlb_sync_map: Sync mappings created recently using @map to the hardware
3229a630a4bSLu Baolu * @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush
3239a630a4bSLu Baolu * queue
3249a630a4bSLu Baolu * @iova_to_phys: translate iova to physical address
3256043257bSJason Gunthorpe * @enforce_cache_coherency: Prevent any kind of DMA from bypassing IOMMU_CACHE,
3266043257bSJason Gunthorpe * including no-snoop TLPs on PCIe or other platform
3276043257bSJason Gunthorpe * specific mechanisms.
3289a630a4bSLu Baolu * @enable_nesting: Enable nesting
3299a630a4bSLu Baolu * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*)
3309a630a4bSLu Baolu * @free: Release the domain after use.
3319a630a4bSLu Baolu */
3329a630a4bSLu Baolu struct iommu_domain_ops {
3339a630a4bSLu Baolu int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
33416603704SLu Baolu int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev,
33516603704SLu Baolu ioasid_t pasid);
3369a630a4bSLu Baolu
3379a630a4bSLu Baolu int (*map)(struct iommu_domain *domain, unsigned long iova,
3389a630a4bSLu Baolu phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
3399a630a4bSLu Baolu int (*map_pages)(struct iommu_domain *domain, unsigned long iova,
3409a630a4bSLu Baolu phys_addr_t paddr, size_t pgsize, size_t pgcount,
3419a630a4bSLu Baolu int prot, gfp_t gfp, size_t *mapped);
3429a630a4bSLu Baolu size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
3439a630a4bSLu Baolu size_t size, struct iommu_iotlb_gather *iotlb_gather);
3449a630a4bSLu Baolu size_t (*unmap_pages)(struct iommu_domain *domain, unsigned long iova,
3459a630a4bSLu Baolu size_t pgsize, size_t pgcount,
3469a630a4bSLu Baolu struct iommu_iotlb_gather *iotlb_gather);
3479a630a4bSLu Baolu
3489a630a4bSLu Baolu void (*flush_iotlb_all)(struct iommu_domain *domain);
3499a630a4bSLu Baolu void (*iotlb_sync_map)(struct iommu_domain *domain, unsigned long iova,
3509a630a4bSLu Baolu size_t size);
3519a630a4bSLu Baolu void (*iotlb_sync)(struct iommu_domain *domain,
3529a630a4bSLu Baolu struct iommu_iotlb_gather *iotlb_gather);
3539a630a4bSLu Baolu
3549a630a4bSLu Baolu phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,
3559a630a4bSLu Baolu dma_addr_t iova);
3569a630a4bSLu Baolu
3576043257bSJason Gunthorpe bool (*enforce_cache_coherency)(struct iommu_domain *domain);
3589a630a4bSLu Baolu int (*enable_nesting)(struct iommu_domain *domain);
3599a630a4bSLu Baolu int (*set_pgtable_quirks)(struct iommu_domain *domain,
3609a630a4bSLu Baolu unsigned long quirks);
3619a630a4bSLu Baolu
3629a630a4bSLu Baolu void (*free)(struct iommu_domain *domain);
3639a630a4bSLu Baolu };
3649a630a4bSLu Baolu
3659a630a4bSLu Baolu /**
366b0119e87SJoerg Roedel * struct iommu_device - IOMMU core representation of one IOMMU hardware
367b0119e87SJoerg Roedel * instance
368b0119e87SJoerg Roedel * @list: Used by the iommu-core to keep a list of registered iommus
369b0119e87SJoerg Roedel * @ops: iommu-ops for talking to this iommu
37039ab9555SJoerg Roedel * @dev: struct device for sysfs handling
3711adf3cc2SLu Baolu * @max_pasids: number of supported PASIDs
372b0119e87SJoerg Roedel */
373b0119e87SJoerg Roedel struct iommu_device {
374b0119e87SJoerg Roedel struct list_head list;
375b0119e87SJoerg Roedel const struct iommu_ops *ops;
376c73e1ac8SJoerg Roedel struct fwnode_handle *fwnode;
3772926a2aaSJoerg Roedel struct device *dev;
3781adf3cc2SLu Baolu u32 max_pasids;
379b0119e87SJoerg Roedel };
380b0119e87SJoerg Roedel
3814e32348bSJacob Pan /**
3824e32348bSJacob Pan * struct iommu_fault_event - Generic fault event
3834e32348bSJacob Pan *
3844e32348bSJacob Pan * Can represent recoverable faults such as a page requests or
3854e32348bSJacob Pan * unrecoverable faults such as DMA or IRQ remapping faults.
3864e32348bSJacob Pan *
3874e32348bSJacob Pan * @fault: fault descriptor
388bf3255b3SJean-Philippe Brucker * @list: pending fault event list, used for tracking responses
3894e32348bSJacob Pan */
3904e32348bSJacob Pan struct iommu_fault_event {
3914e32348bSJacob Pan struct iommu_fault fault;
392bf3255b3SJean-Philippe Brucker struct list_head list;
3934e32348bSJacob Pan };
3944e32348bSJacob Pan
3954e32348bSJacob Pan /**
3964e32348bSJacob Pan * struct iommu_fault_param - per-device IOMMU fault data
3974e32348bSJacob Pan * @handler: Callback function to handle IOMMU faults at device level
3984e32348bSJacob Pan * @data: handler private data
399bf3255b3SJean-Philippe Brucker * @faults: holds the pending faults which needs response
400bf3255b3SJean-Philippe Brucker * @lock: protect pending faults list
4014e32348bSJacob Pan */
4024e32348bSJacob Pan struct iommu_fault_param {
4034e32348bSJacob Pan iommu_dev_fault_handler_t handler;
4044e32348bSJacob Pan void *data;
405bf3255b3SJean-Philippe Brucker struct list_head faults;
406bf3255b3SJean-Philippe Brucker struct mutex lock;
4074e32348bSJacob Pan };
4084e32348bSJacob Pan
4094e32348bSJacob Pan /**
410045a7042SJoerg Roedel * struct dev_iommu - Collection of per-device IOMMU data
4114e32348bSJacob Pan *
4124e32348bSJacob Pan * @fault_param: IOMMU detected device fault reporting data
413fc36479dSJean-Philippe Brucker * @iopf_param: I/O Page Fault queue and data
41472acd9dfSJoerg Roedel * @fwspec: IOMMU fwspec data
415a6a4c7e2SJoerg Roedel * @iommu_dev: IOMMU device this device is linked to
416986d5eccSJoerg Roedel * @priv: IOMMU Driver private data
41722d2c7afSLu Baolu * @max_pasids: number of PASIDs this device can consume
418dd8a25c5SJason Gunthorpe * @attach_deferred: the dma domain attachment is deferred
419791c2b17SRobin Murphy * @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs
420a48ce36eSLu Baolu * @require_direct: device requires IOMMU_RESV_DIRECT regions
4214e32348bSJacob Pan *
4224e32348bSJacob Pan * TODO: migrate other per device data pointers under iommu_dev_data, e.g.
4234e32348bSJacob Pan * struct iommu_group *iommu_group;
4244e32348bSJacob Pan */
425045a7042SJoerg Roedel struct dev_iommu {
4260c830e6bSJacob Pan struct mutex lock;
4274e32348bSJacob Pan struct iommu_fault_param *fault_param;
428fc36479dSJean-Philippe Brucker struct iopf_device_param *iopf_param;
42972acd9dfSJoerg Roedel struct iommu_fwspec *fwspec;
430a6a4c7e2SJoerg Roedel struct iommu_device *iommu_dev;
431986d5eccSJoerg Roedel void *priv;
43222d2c7afSLu Baolu u32 max_pasids;
433dd8a25c5SJason Gunthorpe u32 attach_deferred:1;
434791c2b17SRobin Murphy u32 pci_32bit_workaround:1;
435a48ce36eSLu Baolu u32 require_direct:1;
4364e32348bSJacob Pan };
4374e32348bSJacob Pan
4382d471b20SRobin Murphy int iommu_device_register(struct iommu_device *iommu,
4392d471b20SRobin Murphy const struct iommu_ops *ops,
4402d471b20SRobin Murphy struct device *hwdev);
441b0119e87SJoerg Roedel void iommu_device_unregister(struct iommu_device *iommu);
44239ab9555SJoerg Roedel int iommu_device_sysfs_add(struct iommu_device *iommu,
44339ab9555SJoerg Roedel struct device *parent,
44439ab9555SJoerg Roedel const struct attribute_group **groups,
44539ab9555SJoerg Roedel const char *fmt, ...) __printf(4, 5);
44639ab9555SJoerg Roedel void iommu_device_sysfs_remove(struct iommu_device *iommu);
447e3d10af1SJoerg Roedel int iommu_device_link(struct iommu_device *iommu, struct device *link);
448e3d10af1SJoerg Roedel void iommu_device_unlink(struct iommu_device *iommu, struct device *link);
4493ab65729SLianbo Jiang int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain);
450b0119e87SJoerg Roedel
dev_to_iommu_device(struct device * dev)4512926a2aaSJoerg Roedel static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
4522926a2aaSJoerg Roedel {
4532926a2aaSJoerg Roedel return (struct iommu_device *)dev_get_drvdata(dev);
4542926a2aaSJoerg Roedel }
4552926a2aaSJoerg Roedel
iommu_iotlb_gather_init(struct iommu_iotlb_gather * gather)456a7d20dc1SWill Deacon static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather)
457a7d20dc1SWill Deacon {
458a7d20dc1SWill Deacon *gather = (struct iommu_iotlb_gather) {
459a7d20dc1SWill Deacon .start = ULONG_MAX,
46087f60cc6SMatthew Wilcox (Oracle) .freelist = LIST_HEAD_INIT(gather->freelist),
461a7d20dc1SWill Deacon };
462a7d20dc1SWill Deacon }
463a7d20dc1SWill Deacon
464b18d0a0fSGreg Kroah-Hartman extern int bus_iommu_probe(const struct bus_type *bus);
465b18d0a0fSGreg Kroah-Hartman extern bool iommu_present(const struct bus_type *bus);
466ed36d04eSRobin Murphy extern bool device_iommu_capable(struct device *dev, enum iommu_cap cap);
467efc30a8fSJason Gunthorpe extern bool iommu_group_has_isolated_msi(struct iommu_group *group);
468b18d0a0fSGreg Kroah-Hartman extern struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus);
4694a77a6cfSJoerg Roedel extern void iommu_domain_free(struct iommu_domain *domain);
4704a77a6cfSJoerg Roedel extern int iommu_attach_device(struct iommu_domain *domain,
4714a77a6cfSJoerg Roedel struct device *dev);
4724a77a6cfSJoerg Roedel extern void iommu_detach_device(struct iommu_domain *domain,
4734a77a6cfSJoerg Roedel struct device *dev);
474808be0aaSJacob Pan extern int iommu_sva_unbind_gpasid(struct iommu_domain *domain,
475808be0aaSJacob Pan struct device *dev, ioasid_t pasid);
4762c1296d9SJoerg Roedel extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
4776af588feSRobin Murphy extern struct iommu_domain *iommu_get_dma_domain(struct device *dev);
478cefc53c7SJoerg Roedel extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
4791369459bSJason Gunthorpe phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
4807d3002ccSOhad Ben-Cohen extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
4817d3002ccSOhad Ben-Cohen size_t size);
482add02cfdSJoerg Roedel extern size_t iommu_unmap_fast(struct iommu_domain *domain,
483a7d20dc1SWill Deacon unsigned long iova, size_t size,
484a7d20dc1SWill Deacon struct iommu_iotlb_gather *iotlb_gather);
485ad8f36e4SLogan Gunthorpe extern ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
486f2b2c051SJason Gunthorpe struct scatterlist *sg, unsigned int nents,
487f2b2c051SJason Gunthorpe int prot, gfp_t gfp);
488bb5547acSVarun Sethi extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
4894f3f8d9dSOhad Ben-Cohen extern void iommu_set_fault_handler(struct iommu_domain *domain,
49077ca2332SOhad Ben-Cohen iommu_fault_handler_t handler, void *token);
491d72e31c9SAlex Williamson
492e5b5234aSEric Auger extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);
493e5b5234aSEric Auger extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);
4948a69961cSJoerg Roedel extern void iommu_set_default_passthrough(bool cmd_line);
4958a69961cSJoerg Roedel extern void iommu_set_default_translated(bool cmd_line);
4968a69961cSJoerg Roedel extern bool iommu_default_passthrough(void);
4972b20cbbaSEric Auger extern struct iommu_resv_region *
4989d3a4de4SRobin Murphy iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot,
4990251d010SLu Baolu enum iommu_resv_type type, gfp_t gfp);
5006c65fb31SEric Auger extern int iommu_get_group_resv_regions(struct iommu_group *group,
5016c65fb31SEric Auger struct list_head *head);
502a1015c2bSJoerg Roedel
503d72e31c9SAlex Williamson extern int iommu_attach_group(struct iommu_domain *domain,
504d72e31c9SAlex Williamson struct iommu_group *group);
505d72e31c9SAlex Williamson extern void iommu_detach_group(struct iommu_domain *domain,
506d72e31c9SAlex Williamson struct iommu_group *group);
507d72e31c9SAlex Williamson extern struct iommu_group *iommu_group_alloc(void);
508d72e31c9SAlex Williamson extern void *iommu_group_get_iommudata(struct iommu_group *group);
509d72e31c9SAlex Williamson extern void iommu_group_set_iommudata(struct iommu_group *group,
510d72e31c9SAlex Williamson void *iommu_data,
511d72e31c9SAlex Williamson void (*release)(void *iommu_data));
512d72e31c9SAlex Williamson extern int iommu_group_set_name(struct iommu_group *group, const char *name);
513d72e31c9SAlex Williamson extern int iommu_group_add_device(struct iommu_group *group,
514d72e31c9SAlex Williamson struct device *dev);
515d72e31c9SAlex Williamson extern void iommu_group_remove_device(struct device *dev);
516d72e31c9SAlex Williamson extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
517d72e31c9SAlex Williamson int (*fn)(struct device *, void *));
518d72e31c9SAlex Williamson extern struct iommu_group *iommu_group_get(struct device *dev);
51913f59a78SRobin Murphy extern struct iommu_group *iommu_group_ref_get(struct iommu_group *group);
520d72e31c9SAlex Williamson extern void iommu_group_put(struct iommu_group *group);
5210c830e6bSJacob Pan extern int iommu_register_device_fault_handler(struct device *dev,
5220c830e6bSJacob Pan iommu_dev_fault_handler_t handler,
5230c830e6bSJacob Pan void *data);
5240c830e6bSJacob Pan
5250c830e6bSJacob Pan extern int iommu_unregister_device_fault_handler(struct device *dev);
5260c830e6bSJacob Pan
5270c830e6bSJacob Pan extern int iommu_report_device_fault(struct device *dev,
5280c830e6bSJacob Pan struct iommu_fault_event *evt);
529bf3255b3SJean-Philippe Brucker extern int iommu_page_response(struct device *dev,
530bf3255b3SJean-Philippe Brucker struct iommu_page_response *msg);
5310c830e6bSJacob Pan
532d72e31c9SAlex Williamson extern int iommu_group_id(struct iommu_group *group);
5336827ca83SJoerg Roedel extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
5344f3f8d9dSOhad Ben-Cohen
5357e147547SChristoph Hellwig int iommu_enable_nesting(struct iommu_domain *domain);
5364fc52b81SChristoph Hellwig int iommu_set_pgtable_quirks(struct iommu_domain *domain,
5374fc52b81SChristoph Hellwig unsigned long quirks);
5384f3f8d9dSOhad Ben-Cohen
539308723e3SJohn Garry void iommu_set_dma_strict(void);
5404f3f8d9dSOhad Ben-Cohen
541207c6e36SJoerg Roedel extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
542207c6e36SJoerg Roedel unsigned long iova, int flags);
5434a77a6cfSJoerg Roedel
iommu_flush_iotlb_all(struct iommu_domain * domain)544aae4c8e2STom Murphy static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)
545add02cfdSJoerg Roedel {
546add02cfdSJoerg Roedel if (domain->ops->flush_iotlb_all)
547add02cfdSJoerg Roedel domain->ops->flush_iotlb_all(domain);
548add02cfdSJoerg Roedel }
549add02cfdSJoerg Roedel
iommu_iotlb_sync(struct iommu_domain * domain,struct iommu_iotlb_gather * iotlb_gather)550aae4c8e2STom Murphy static inline void iommu_iotlb_sync(struct iommu_domain *domain,
551a7d20dc1SWill Deacon struct iommu_iotlb_gather *iotlb_gather)
552add02cfdSJoerg Roedel {
553add02cfdSJoerg Roedel if (domain->ops->iotlb_sync)
55456f8af5eSWill Deacon domain->ops->iotlb_sync(domain, iotlb_gather);
555a7d20dc1SWill Deacon
556a7d20dc1SWill Deacon iommu_iotlb_gather_init(iotlb_gather);
557add02cfdSJoerg Roedel }
558add02cfdSJoerg Roedel
5593136895cSRobin Murphy /**
560febb82c2SNadav Amit * iommu_iotlb_gather_is_disjoint - Checks whether a new range is disjoint
561febb82c2SNadav Amit *
562febb82c2SNadav Amit * @gather: TLB gather data
563febb82c2SNadav Amit * @iova: start of page to invalidate
564febb82c2SNadav Amit * @size: size of page to invalidate
565febb82c2SNadav Amit *
566febb82c2SNadav Amit * Helper for IOMMU drivers to check whether a new range and the gathered range
567febb82c2SNadav Amit * are disjoint. For many IOMMUs, flushing the IOMMU in this case is better
568febb82c2SNadav Amit * than merging the two, which might lead to unnecessary invalidations.
569febb82c2SNadav Amit */
570febb82c2SNadav Amit static inline
iommu_iotlb_gather_is_disjoint(struct iommu_iotlb_gather * gather,unsigned long iova,size_t size)571febb82c2SNadav Amit bool iommu_iotlb_gather_is_disjoint(struct iommu_iotlb_gather *gather,
5724fcf8544SWill Deacon unsigned long iova, size_t size)
5734fcf8544SWill Deacon {
574862c3715SYong Wu unsigned long start = iova, end = start + size - 1;
5754fcf8544SWill Deacon
576febb82c2SNadav Amit return gather->end != 0 &&
577febb82c2SNadav Amit (end + 1 < gather->start || start > gather->end + 1);
578febb82c2SNadav Amit }
579febb82c2SNadav Amit
580febb82c2SNadav Amit
581febb82c2SNadav Amit /**
5823136895cSRobin Murphy * iommu_iotlb_gather_add_range - Gather for address-based TLB invalidation
5833136895cSRobin Murphy * @gather: TLB gather data
5843136895cSRobin Murphy * @iova: start of page to invalidate
5853136895cSRobin Murphy * @size: size of page to invalidate
5863136895cSRobin Murphy *
5873136895cSRobin Murphy * Helper for IOMMU drivers to build arbitrarily-sized invalidation commands
5883136895cSRobin Murphy * where only the address range matters, and simply minimising intermediate
5893136895cSRobin Murphy * syncs is preferred.
5903136895cSRobin Murphy */
iommu_iotlb_gather_add_range(struct iommu_iotlb_gather * gather,unsigned long iova,size_t size)5913136895cSRobin Murphy static inline void iommu_iotlb_gather_add_range(struct iommu_iotlb_gather *gather,
5923136895cSRobin Murphy unsigned long iova, size_t size)
5933136895cSRobin Murphy {
5943136895cSRobin Murphy unsigned long end = iova + size - 1;
5953136895cSRobin Murphy
5963136895cSRobin Murphy if (gather->start > iova)
5973136895cSRobin Murphy gather->start = iova;
5983136895cSRobin Murphy if (gather->end < end)
5993136895cSRobin Murphy gather->end = end;
6003136895cSRobin Murphy }
6013136895cSRobin Murphy
6023136895cSRobin Murphy /**
6033136895cSRobin Murphy * iommu_iotlb_gather_add_page - Gather for page-based TLB invalidation
6043136895cSRobin Murphy * @domain: IOMMU domain to be invalidated
6053136895cSRobin Murphy * @gather: TLB gather data
6063136895cSRobin Murphy * @iova: start of page to invalidate
6073136895cSRobin Murphy * @size: size of page to invalidate
6083136895cSRobin Murphy *
6093136895cSRobin Murphy * Helper for IOMMU drivers to build invalidation commands based on individual
6103136895cSRobin Murphy * pages, or with page size/table level hints which cannot be gathered if they
6113136895cSRobin Murphy * differ.
6123136895cSRobin Murphy */
iommu_iotlb_gather_add_page(struct iommu_domain * domain,struct iommu_iotlb_gather * gather,unsigned long iova,size_t size)6134fcf8544SWill Deacon static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain,
6144fcf8544SWill Deacon struct iommu_iotlb_gather *gather,
6154fcf8544SWill Deacon unsigned long iova, size_t size)
6164fcf8544SWill Deacon {
6174fcf8544SWill Deacon /*
6184fcf8544SWill Deacon * If the new page is disjoint from the current range or is mapped at
6194fcf8544SWill Deacon * a different granularity, then sync the TLB so that the gather
6204fcf8544SWill Deacon * structure can be rewritten.
6214fcf8544SWill Deacon */
622febb82c2SNadav Amit if ((gather->pgsize && gather->pgsize != size) ||
623febb82c2SNadav Amit iommu_iotlb_gather_is_disjoint(gather, iova, size))
624aae4c8e2STom Murphy iommu_iotlb_sync(domain, gather);
6254fcf8544SWill Deacon
6264fcf8544SWill Deacon gather->pgsize = size;
6273136895cSRobin Murphy iommu_iotlb_gather_add_range(gather, iova, size);
6284fcf8544SWill Deacon }
6294fcf8544SWill Deacon
iommu_iotlb_gather_queued(struct iommu_iotlb_gather * gather)630f7403abfSRobin Murphy static inline bool iommu_iotlb_gather_queued(struct iommu_iotlb_gather *gather)
631f7403abfSRobin Murphy {
632f7403abfSRobin Murphy return gather && gather->queued;
6334fcf8544SWill Deacon }
6344fcf8544SWill Deacon
6355e62292bSJoerg Roedel /* PCI device grouping function */
6365e62292bSJoerg Roedel extern struct iommu_group *pci_device_group(struct device *dev);
6376eab556aSJoerg Roedel /* Generic device grouping function */
6386eab556aSJoerg Roedel extern struct iommu_group *generic_device_group(struct device *dev);
639eab03e2aSNipun Gupta /* FSL-MC device grouping function */
640eab03e2aSNipun Gupta struct iommu_group *fsl_mc_device_group(struct device *dev);
6415e62292bSJoerg Roedel
64257f98d2fSRobin Murphy /**
64357f98d2fSRobin Murphy * struct iommu_fwspec - per-device IOMMU instance data
64457f98d2fSRobin Murphy * @ops: ops for this device's IOMMU
64557f98d2fSRobin Murphy * @iommu_fwnode: firmware handle for this device's IOMMU
6460d35309aSJean-Philippe Brucker * @flags: IOMMU_FWSPEC_* flags
64757f98d2fSRobin Murphy * @num_ids: number of associated device IDs
64857f98d2fSRobin Murphy * @ids: IDs which this device may present to the IOMMU
649495b637fSThierry Reding *
650495b637fSThierry Reding * Note that the IDs (and any other information, really) stored in this structure should be
651495b637fSThierry Reding * considered private to the IOMMU device driver and are not to be used directly by IOMMU
652495b637fSThierry Reding * consumers.
65357f98d2fSRobin Murphy */
65457f98d2fSRobin Murphy struct iommu_fwspec {
65557f98d2fSRobin Murphy const struct iommu_ops *ops;
65657f98d2fSRobin Murphy struct fwnode_handle *iommu_fwnode;
6575702ee24SJean-Philippe Brucker u32 flags;
65857f98d2fSRobin Murphy unsigned int num_ids;
659098accf2SRobin Murphy u32 ids[];
66057f98d2fSRobin Murphy };
66157f98d2fSRobin Murphy
6625702ee24SJean-Philippe Brucker /* ATS is supported */
6635702ee24SJean-Philippe Brucker #define IOMMU_FWSPEC_PCI_RC_ATS (1 << 0)
6645702ee24SJean-Philippe Brucker
66526b25a2bSJean-Philippe Brucker /**
66626b25a2bSJean-Philippe Brucker * struct iommu_sva - handle to a device-mm bond
66726b25a2bSJean-Philippe Brucker */
66826b25a2bSJean-Philippe Brucker struct iommu_sva {
66926b25a2bSJean-Philippe Brucker struct device *dev;
670be51b1d6SLu Baolu struct iommu_domain *domain;
67126b25a2bSJean-Philippe Brucker };
67226b25a2bSJean-Philippe Brucker
67357f98d2fSRobin Murphy int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
67457f98d2fSRobin Murphy const struct iommu_ops *ops);
67557f98d2fSRobin Murphy void iommu_fwspec_free(struct device *dev);
67657f98d2fSRobin Murphy int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);
677534766dfSJoerg Roedel const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode);
67857f98d2fSRobin Murphy
dev_iommu_fwspec_get(struct device * dev)679b4ef725eSJoerg Roedel static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
680b4ef725eSJoerg Roedel {
68172acd9dfSJoerg Roedel if (dev->iommu)
68272acd9dfSJoerg Roedel return dev->iommu->fwspec;
68372acd9dfSJoerg Roedel else
68472acd9dfSJoerg Roedel return NULL;
685b4ef725eSJoerg Roedel }
686b4ef725eSJoerg Roedel
dev_iommu_fwspec_set(struct device * dev,struct iommu_fwspec * fwspec)687b4ef725eSJoerg Roedel static inline void dev_iommu_fwspec_set(struct device *dev,
688b4ef725eSJoerg Roedel struct iommu_fwspec *fwspec)
689b4ef725eSJoerg Roedel {
69072acd9dfSJoerg Roedel dev->iommu->fwspec = fwspec;
691b4ef725eSJoerg Roedel }
692b4ef725eSJoerg Roedel
dev_iommu_priv_get(struct device * dev)693f9867f41SJoerg Roedel static inline void *dev_iommu_priv_get(struct device *dev)
694f9867f41SJoerg Roedel {
6954c9fb5d9SJoerg Roedel if (dev->iommu)
696986d5eccSJoerg Roedel return dev->iommu->priv;
6974c9fb5d9SJoerg Roedel else
6984c9fb5d9SJoerg Roedel return NULL;
699f9867f41SJoerg Roedel }
700f9867f41SJoerg Roedel
dev_iommu_priv_set(struct device * dev,void * priv)701f9867f41SJoerg Roedel static inline void dev_iommu_priv_set(struct device *dev, void *priv)
702f9867f41SJoerg Roedel {
703986d5eccSJoerg Roedel dev->iommu->priv = priv;
704f9867f41SJoerg Roedel }
705f9867f41SJoerg Roedel
706*c1114090SRobin Murphy extern struct mutex iommu_probe_device_lock;
707cc5aed44SJoerg Roedel int iommu_probe_device(struct device *dev);
708cc5aed44SJoerg Roedel
709a3a19592SLu Baolu int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);
710a3a19592SLu Baolu int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);
711a3a19592SLu Baolu
7121ea2a07aSLu Baolu int iommu_device_use_default_domain(struct device *dev);
7131ea2a07aSLu Baolu void iommu_device_unuse_default_domain(struct device *dev);
7141ea2a07aSLu Baolu
7151ea2a07aSLu Baolu int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner);
7161ea2a07aSLu Baolu void iommu_group_release_dma_owner(struct iommu_group *group);
7171ea2a07aSLu Baolu bool iommu_group_dma_owner_claimed(struct iommu_group *group);
7181ea2a07aSLu Baolu
71989395cceSLu Baolu int iommu_device_claim_dma_owner(struct device *dev, void *owner);
72089395cceSLu Baolu void iommu_device_release_dma_owner(struct device *dev);
72189395cceSLu Baolu
72213646796SLu Baolu struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
72313646796SLu Baolu struct mm_struct *mm);
72416603704SLu Baolu int iommu_attach_device_pasid(struct iommu_domain *domain,
72516603704SLu Baolu struct device *dev, ioasid_t pasid);
72616603704SLu Baolu void iommu_detach_device_pasid(struct iommu_domain *domain,
72716603704SLu Baolu struct device *dev, ioasid_t pasid);
72816603704SLu Baolu struct iommu_domain *
72916603704SLu Baolu iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid,
73016603704SLu Baolu unsigned int type);
7312dcebc7dSJacob Pan ioasid_t iommu_alloc_global_pasid(struct device *dev);
7322dcebc7dSJacob Pan void iommu_free_global_pasid(ioasid_t pasid);
7334a77a6cfSJoerg Roedel #else /* CONFIG_IOMMU_API */
7344a77a6cfSJoerg Roedel
73539d4ebb9SJoerg Roedel struct iommu_ops {};
736d72e31c9SAlex Williamson struct iommu_group {};
73757f98d2fSRobin Murphy struct iommu_fwspec {};
738b0119e87SJoerg Roedel struct iommu_device {};
7394e32348bSJacob Pan struct iommu_fault_param {};
740a7d20dc1SWill Deacon struct iommu_iotlb_gather {};
7414a77a6cfSJoerg Roedel
iommu_present(const struct bus_type * bus)742b18d0a0fSGreg Kroah-Hartman static inline bool iommu_present(const struct bus_type *bus)
7434a77a6cfSJoerg Roedel {
7444a77a6cfSJoerg Roedel return false;
7454a77a6cfSJoerg Roedel }
7464a77a6cfSJoerg Roedel
device_iommu_capable(struct device * dev,enum iommu_cap cap)747ed36d04eSRobin Murphy static inline bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
748ed36d04eSRobin Murphy {
749ed36d04eSRobin Murphy return false;
750ed36d04eSRobin Murphy }
751ed36d04eSRobin Murphy
iommu_domain_alloc(const struct bus_type * bus)752b18d0a0fSGreg Kroah-Hartman static inline struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
7534a77a6cfSJoerg Roedel {
7544a77a6cfSJoerg Roedel return NULL;
7554a77a6cfSJoerg Roedel }
7564a77a6cfSJoerg Roedel
iommu_domain_free(struct iommu_domain * domain)7574a77a6cfSJoerg Roedel static inline void iommu_domain_free(struct iommu_domain *domain)
7584a77a6cfSJoerg Roedel {
7594a77a6cfSJoerg Roedel }
7604a77a6cfSJoerg Roedel
iommu_attach_device(struct iommu_domain * domain,struct device * dev)7614a77a6cfSJoerg Roedel static inline int iommu_attach_device(struct iommu_domain *domain,
7624a77a6cfSJoerg Roedel struct device *dev)
7634a77a6cfSJoerg Roedel {
7644a77a6cfSJoerg Roedel return -ENODEV;
7654a77a6cfSJoerg Roedel }
7664a77a6cfSJoerg Roedel
iommu_detach_device(struct iommu_domain * domain,struct device * dev)7674a77a6cfSJoerg Roedel static inline void iommu_detach_device(struct iommu_domain *domain,
7684a77a6cfSJoerg Roedel struct device *dev)
7694a77a6cfSJoerg Roedel {
7704a77a6cfSJoerg Roedel }
7714a77a6cfSJoerg Roedel
iommu_get_domain_for_dev(struct device * dev)7722c1296d9SJoerg Roedel static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
7732c1296d9SJoerg Roedel {
7742c1296d9SJoerg Roedel return NULL;
7752c1296d9SJoerg Roedel }
7762c1296d9SJoerg Roedel
iommu_map(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t size,int prot,gfp_t gfp)777cefc53c7SJoerg Roedel static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
7781369459bSJason Gunthorpe phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
779781ca2deSTom Murphy {
780781ca2deSTom Murphy return -ENODEV;
781781ca2deSTom Murphy }
782781ca2deSTom Murphy
iommu_unmap(struct iommu_domain * domain,unsigned long iova,size_t size)783c5611a87SSuravee Suthikulpanit static inline size_t iommu_unmap(struct iommu_domain *domain,
784c5611a87SSuravee Suthikulpanit unsigned long iova, size_t size)
785cefc53c7SJoerg Roedel {
786c5611a87SSuravee Suthikulpanit return 0;
787cefc53c7SJoerg Roedel }
788cefc53c7SJoerg Roedel
iommu_unmap_fast(struct iommu_domain * domain,unsigned long iova,int gfp_order,struct iommu_iotlb_gather * iotlb_gather)789c5611a87SSuravee Suthikulpanit static inline size_t iommu_unmap_fast(struct iommu_domain *domain,
790a7d20dc1SWill Deacon unsigned long iova, int gfp_order,
791a7d20dc1SWill Deacon struct iommu_iotlb_gather *iotlb_gather)
7924a77a6cfSJoerg Roedel {
793c5611a87SSuravee Suthikulpanit return 0;
7944a77a6cfSJoerg Roedel }
7954a77a6cfSJoerg Roedel
iommu_map_sg(struct iommu_domain * domain,unsigned long iova,struct scatterlist * sg,unsigned int nents,int prot,gfp_t gfp)796ad8f36e4SLogan Gunthorpe static inline ssize_t iommu_map_sg(struct iommu_domain *domain,
797315786ebSOlav Haugan unsigned long iova, struct scatterlist *sg,
798f2b2c051SJason Gunthorpe unsigned int nents, int prot, gfp_t gfp)
799781ca2deSTom Murphy {
800ad8f36e4SLogan Gunthorpe return -ENODEV;
801781ca2deSTom Murphy }
802781ca2deSTom Murphy
iommu_flush_iotlb_all(struct iommu_domain * domain)803aae4c8e2STom Murphy static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)
804add02cfdSJoerg Roedel {
805add02cfdSJoerg Roedel }
806add02cfdSJoerg Roedel
iommu_iotlb_sync(struct iommu_domain * domain,struct iommu_iotlb_gather * iotlb_gather)807aae4c8e2STom Murphy static inline void iommu_iotlb_sync(struct iommu_domain *domain,
808a7d20dc1SWill Deacon struct iommu_iotlb_gather *iotlb_gather)
809add02cfdSJoerg Roedel {
810add02cfdSJoerg Roedel }
811add02cfdSJoerg Roedel
iommu_iova_to_phys(struct iommu_domain * domain,dma_addr_t iova)812bb5547acSVarun Sethi static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
8134a77a6cfSJoerg Roedel {
8144a77a6cfSJoerg Roedel return 0;
8154a77a6cfSJoerg Roedel }
8164a77a6cfSJoerg Roedel
iommu_set_fault_handler(struct iommu_domain * domain,iommu_fault_handler_t handler,void * token)8174f3f8d9dSOhad Ben-Cohen static inline void iommu_set_fault_handler(struct iommu_domain *domain,
81877ca2332SOhad Ben-Cohen iommu_fault_handler_t handler, void *token)
8194f3f8d9dSOhad Ben-Cohen {
8204f3f8d9dSOhad Ben-Cohen }
8214f3f8d9dSOhad Ben-Cohen
iommu_get_resv_regions(struct device * dev,struct list_head * list)822e5b5234aSEric Auger static inline void iommu_get_resv_regions(struct device *dev,
823a1015c2bSJoerg Roedel struct list_head *list)
824a1015c2bSJoerg Roedel {
825a1015c2bSJoerg Roedel }
826a1015c2bSJoerg Roedel
iommu_put_resv_regions(struct device * dev,struct list_head * list)827e5b5234aSEric Auger static inline void iommu_put_resv_regions(struct device *dev,
828a1015c2bSJoerg Roedel struct list_head *list)
829a1015c2bSJoerg Roedel {
830a1015c2bSJoerg Roedel }
831a1015c2bSJoerg Roedel
iommu_get_group_resv_regions(struct iommu_group * group,struct list_head * head)8326c65fb31SEric Auger static inline int iommu_get_group_resv_regions(struct iommu_group *group,
8336c65fb31SEric Auger struct list_head *head)
8346c65fb31SEric Auger {
8356c65fb31SEric Auger return -ENODEV;
8366c65fb31SEric Auger }
8376c65fb31SEric Auger
iommu_set_default_passthrough(bool cmd_line)8388a69961cSJoerg Roedel static inline void iommu_set_default_passthrough(bool cmd_line)
8398a69961cSJoerg Roedel {
8408a69961cSJoerg Roedel }
8418a69961cSJoerg Roedel
iommu_set_default_translated(bool cmd_line)8428a69961cSJoerg Roedel static inline void iommu_set_default_translated(bool cmd_line)
8438a69961cSJoerg Roedel {
8448a69961cSJoerg Roedel }
8458a69961cSJoerg Roedel
iommu_default_passthrough(void)8468a69961cSJoerg Roedel static inline bool iommu_default_passthrough(void)
8478a69961cSJoerg Roedel {
8488a69961cSJoerg Roedel return true;
8498a69961cSJoerg Roedel }
8508a69961cSJoerg Roedel
iommu_attach_group(struct iommu_domain * domain,struct iommu_group * group)851bef83de5SAlex Williamson static inline int iommu_attach_group(struct iommu_domain *domain,
852bef83de5SAlex Williamson struct iommu_group *group)
8531460432cSAlex Williamson {
8541460432cSAlex Williamson return -ENODEV;
8551460432cSAlex Williamson }
8561460432cSAlex Williamson
iommu_detach_group(struct iommu_domain * domain,struct iommu_group * group)857bef83de5SAlex Williamson static inline void iommu_detach_group(struct iommu_domain *domain,
858bef83de5SAlex Williamson struct iommu_group *group)
859d72e31c9SAlex Williamson {
860d72e31c9SAlex Williamson }
861d72e31c9SAlex Williamson
iommu_group_alloc(void)862bef83de5SAlex Williamson static inline struct iommu_group *iommu_group_alloc(void)
863d72e31c9SAlex Williamson {
864d72e31c9SAlex Williamson return ERR_PTR(-ENODEV);
865d72e31c9SAlex Williamson }
866d72e31c9SAlex Williamson
iommu_group_get_iommudata(struct iommu_group * group)867bef83de5SAlex Williamson static inline void *iommu_group_get_iommudata(struct iommu_group *group)
868d72e31c9SAlex Williamson {
869d72e31c9SAlex Williamson return NULL;
870d72e31c9SAlex Williamson }
871d72e31c9SAlex Williamson
iommu_group_set_iommudata(struct iommu_group * group,void * iommu_data,void (* release)(void * iommu_data))872bef83de5SAlex Williamson static inline void iommu_group_set_iommudata(struct iommu_group *group,
873bef83de5SAlex Williamson void *iommu_data,
874d72e31c9SAlex Williamson void (*release)(void *iommu_data))
875d72e31c9SAlex Williamson {
876d72e31c9SAlex Williamson }
877d72e31c9SAlex Williamson
iommu_group_set_name(struct iommu_group * group,const char * name)878bef83de5SAlex Williamson static inline int iommu_group_set_name(struct iommu_group *group,
879bef83de5SAlex Williamson const char *name)
880d72e31c9SAlex Williamson {
881d72e31c9SAlex Williamson return -ENODEV;
882d72e31c9SAlex Williamson }
883d72e31c9SAlex Williamson
iommu_group_add_device(struct iommu_group * group,struct device * dev)884bef83de5SAlex Williamson static inline int iommu_group_add_device(struct iommu_group *group,
885bef83de5SAlex Williamson struct device *dev)
886d72e31c9SAlex Williamson {
887d72e31c9SAlex Williamson return -ENODEV;
888d72e31c9SAlex Williamson }
889d72e31c9SAlex Williamson
iommu_group_remove_device(struct device * dev)890bef83de5SAlex Williamson static inline void iommu_group_remove_device(struct device *dev)
891d72e31c9SAlex Williamson {
892d72e31c9SAlex Williamson }
893d72e31c9SAlex Williamson
iommu_group_for_each_dev(struct iommu_group * group,void * data,int (* fn)(struct device *,void *))894bef83de5SAlex Williamson static inline int iommu_group_for_each_dev(struct iommu_group *group,
895bef83de5SAlex Williamson void *data,
896d72e31c9SAlex Williamson int (*fn)(struct device *, void *))
897d72e31c9SAlex Williamson {
898d72e31c9SAlex Williamson return -ENODEV;
899d72e31c9SAlex Williamson }
900d72e31c9SAlex Williamson
iommu_group_get(struct device * dev)901bef83de5SAlex Williamson static inline struct iommu_group *iommu_group_get(struct device *dev)
902d72e31c9SAlex Williamson {
903d72e31c9SAlex Williamson return NULL;
904d72e31c9SAlex Williamson }
905d72e31c9SAlex Williamson
iommu_group_put(struct iommu_group * group)906bef83de5SAlex Williamson static inline void iommu_group_put(struct iommu_group *group)
907d72e31c9SAlex Williamson {
908d72e31c9SAlex Williamson }
909d72e31c9SAlex Williamson
9100c830e6bSJacob Pan static inline
iommu_register_device_fault_handler(struct device * dev,iommu_dev_fault_handler_t handler,void * data)9110c830e6bSJacob Pan int iommu_register_device_fault_handler(struct device *dev,
9120c830e6bSJacob Pan iommu_dev_fault_handler_t handler,
9130c830e6bSJacob Pan void *data)
9140c830e6bSJacob Pan {
9150c830e6bSJacob Pan return -ENODEV;
9160c830e6bSJacob Pan }
9170c830e6bSJacob Pan
iommu_unregister_device_fault_handler(struct device * dev)9180c830e6bSJacob Pan static inline int iommu_unregister_device_fault_handler(struct device *dev)
9190c830e6bSJacob Pan {
9200c830e6bSJacob Pan return 0;
9210c830e6bSJacob Pan }
9220c830e6bSJacob Pan
9230c830e6bSJacob Pan static inline
iommu_report_device_fault(struct device * dev,struct iommu_fault_event * evt)9240c830e6bSJacob Pan int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
9250c830e6bSJacob Pan {
9260c830e6bSJacob Pan return -ENODEV;
9270c830e6bSJacob Pan }
9280c830e6bSJacob Pan
iommu_page_response(struct device * dev,struct iommu_page_response * msg)929bf3255b3SJean-Philippe Brucker static inline int iommu_page_response(struct device *dev,
930bf3255b3SJean-Philippe Brucker struct iommu_page_response *msg)
931bf3255b3SJean-Philippe Brucker {
932bf3255b3SJean-Philippe Brucker return -ENODEV;
933bf3255b3SJean-Philippe Brucker }
934bf3255b3SJean-Philippe Brucker
iommu_group_id(struct iommu_group * group)935bef83de5SAlex Williamson static inline int iommu_group_id(struct iommu_group *group)
936d72e31c9SAlex Williamson {
937d72e31c9SAlex Williamson return -ENODEV;
938d72e31c9SAlex Williamson }
9394a77a6cfSJoerg Roedel
iommu_set_pgtable_quirks(struct iommu_domain * domain,unsigned long quirks)9404fc52b81SChristoph Hellwig static inline int iommu_set_pgtable_quirks(struct iommu_domain *domain,
9414fc52b81SChristoph Hellwig unsigned long quirks)
9420cd76dd1SJoerg Roedel {
9434fc52b81SChristoph Hellwig return 0;
9440cd76dd1SJoerg Roedel }
9450cd76dd1SJoerg Roedel
iommu_device_register(struct iommu_device * iommu,const struct iommu_ops * ops,struct device * hwdev)9462d471b20SRobin Murphy static inline int iommu_device_register(struct iommu_device *iommu,
9472d471b20SRobin Murphy const struct iommu_ops *ops,
9482d471b20SRobin Murphy struct device *hwdev)
949b0119e87SJoerg Roedel {
950b0119e87SJoerg Roedel return -ENODEV;
951b0119e87SJoerg Roedel }
952b0119e87SJoerg Roedel
dev_to_iommu_device(struct device * dev)9532926a2aaSJoerg Roedel static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
9542926a2aaSJoerg Roedel {
9552926a2aaSJoerg Roedel return NULL;
9562926a2aaSJoerg Roedel }
9572926a2aaSJoerg Roedel
iommu_iotlb_gather_init(struct iommu_iotlb_gather * gather)958a7d20dc1SWill Deacon static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather)
959a7d20dc1SWill Deacon {
960a7d20dc1SWill Deacon }
961a7d20dc1SWill Deacon
iommu_iotlb_gather_add_page(struct iommu_domain * domain,struct iommu_iotlb_gather * gather,unsigned long iova,size_t size)9624fcf8544SWill Deacon static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain,
9634fcf8544SWill Deacon struct iommu_iotlb_gather *gather,
9644fcf8544SWill Deacon unsigned long iova, size_t size)
9654fcf8544SWill Deacon {
9664fcf8544SWill Deacon }
9674fcf8544SWill Deacon
iommu_iotlb_gather_queued(struct iommu_iotlb_gather * gather)968f7403abfSRobin Murphy static inline bool iommu_iotlb_gather_queued(struct iommu_iotlb_gather *gather)
969f7403abfSRobin Murphy {
970f7403abfSRobin Murphy return false;
971f7403abfSRobin Murphy }
972f7403abfSRobin Murphy
iommu_device_unregister(struct iommu_device * iommu)973b0119e87SJoerg Roedel static inline void iommu_device_unregister(struct iommu_device *iommu)
974b0119e87SJoerg Roedel {
975b0119e87SJoerg Roedel }
976b0119e87SJoerg Roedel
iommu_device_sysfs_add(struct iommu_device * iommu,struct device * parent,const struct attribute_group ** groups,const char * fmt,...)97739ab9555SJoerg Roedel static inline int iommu_device_sysfs_add(struct iommu_device *iommu,
97839ab9555SJoerg Roedel struct device *parent,
979c61959ecSAlex Williamson const struct attribute_group **groups,
980c61959ecSAlex Williamson const char *fmt, ...)
981c61959ecSAlex Williamson {
98239ab9555SJoerg Roedel return -ENODEV;
983c61959ecSAlex Williamson }
984c61959ecSAlex Williamson
iommu_device_sysfs_remove(struct iommu_device * iommu)98539ab9555SJoerg Roedel static inline void iommu_device_sysfs_remove(struct iommu_device *iommu)
986c61959ecSAlex Williamson {
987c61959ecSAlex Williamson }
988c61959ecSAlex Williamson
iommu_device_link(struct device * dev,struct device * link)989e09f8ea5SAlex Williamson static inline int iommu_device_link(struct device *dev, struct device *link)
990c61959ecSAlex Williamson {
991c61959ecSAlex Williamson return -EINVAL;
992c61959ecSAlex Williamson }
993c61959ecSAlex Williamson
iommu_device_unlink(struct device * dev,struct device * link)994e09f8ea5SAlex Williamson static inline void iommu_device_unlink(struct device *dev, struct device *link)
995c61959ecSAlex Williamson {
996c61959ecSAlex Williamson }
997c61959ecSAlex Williamson
iommu_fwspec_init(struct device * dev,struct fwnode_handle * iommu_fwnode,const struct iommu_ops * ops)99857f98d2fSRobin Murphy static inline int iommu_fwspec_init(struct device *dev,
99957f98d2fSRobin Murphy struct fwnode_handle *iommu_fwnode,
100057f98d2fSRobin Murphy const struct iommu_ops *ops)
100157f98d2fSRobin Murphy {
100257f98d2fSRobin Murphy return -ENODEV;
100357f98d2fSRobin Murphy }
100457f98d2fSRobin Murphy
iommu_fwspec_free(struct device * dev)100557f98d2fSRobin Murphy static inline void iommu_fwspec_free(struct device *dev)
100657f98d2fSRobin Murphy {
100757f98d2fSRobin Murphy }
100857f98d2fSRobin Murphy
iommu_fwspec_add_ids(struct device * dev,u32 * ids,int num_ids)100957f98d2fSRobin Murphy static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids,
101057f98d2fSRobin Murphy int num_ids)
101157f98d2fSRobin Murphy {
101257f98d2fSRobin Murphy return -ENODEV;
101357f98d2fSRobin Murphy }
101457f98d2fSRobin Murphy
1015e4f10ffeSLorenzo Pieralisi static inline
iommu_ops_from_fwnode(struct fwnode_handle * fwnode)1016534766dfSJoerg Roedel const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
1017e4f10ffeSLorenzo Pieralisi {
1018e4f10ffeSLorenzo Pieralisi return NULL;
1019e4f10ffeSLorenzo Pieralisi }
1020e4f10ffeSLorenzo Pieralisi
1021a3a19592SLu Baolu static inline int
iommu_dev_enable_feature(struct device * dev,enum iommu_dev_features feat)1022a3a19592SLu Baolu iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
1023a3a19592SLu Baolu {
1024a3a19592SLu Baolu return -ENODEV;
1025a3a19592SLu Baolu }
1026a3a19592SLu Baolu
1027a3a19592SLu Baolu static inline int
iommu_dev_disable_feature(struct device * dev,enum iommu_dev_features feat)1028a3a19592SLu Baolu iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
1029a3a19592SLu Baolu {
1030a3a19592SLu Baolu return -ENODEV;
1031a3a19592SLu Baolu }
1032a3a19592SLu Baolu
dev_iommu_fwspec_get(struct device * dev)10330008d0c3SJoerg Roedel static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
10340008d0c3SJoerg Roedel {
10350008d0c3SJoerg Roedel return NULL;
10360008d0c3SJoerg Roedel }
10371ea2a07aSLu Baolu
iommu_device_use_default_domain(struct device * dev)10381ea2a07aSLu Baolu static inline int iommu_device_use_default_domain(struct device *dev)
10391ea2a07aSLu Baolu {
10401ea2a07aSLu Baolu return 0;
10411ea2a07aSLu Baolu }
10421ea2a07aSLu Baolu
iommu_device_unuse_default_domain(struct device * dev)10431ea2a07aSLu Baolu static inline void iommu_device_unuse_default_domain(struct device *dev)
10441ea2a07aSLu Baolu {
10451ea2a07aSLu Baolu }
10461ea2a07aSLu Baolu
10471ea2a07aSLu Baolu static inline int
iommu_group_claim_dma_owner(struct iommu_group * group,void * owner)10481ea2a07aSLu Baolu iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
10491ea2a07aSLu Baolu {
10501ea2a07aSLu Baolu return -ENODEV;
10511ea2a07aSLu Baolu }
10521ea2a07aSLu Baolu
iommu_group_release_dma_owner(struct iommu_group * group)10531ea2a07aSLu Baolu static inline void iommu_group_release_dma_owner(struct iommu_group *group)
10541ea2a07aSLu Baolu {
10551ea2a07aSLu Baolu }
10561ea2a07aSLu Baolu
iommu_group_dma_owner_claimed(struct iommu_group * group)10571ea2a07aSLu Baolu static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group)
10581ea2a07aSLu Baolu {
10591ea2a07aSLu Baolu return false;
10601ea2a07aSLu Baolu }
106116603704SLu Baolu
iommu_device_release_dma_owner(struct device * dev)106289395cceSLu Baolu static inline void iommu_device_release_dma_owner(struct device *dev)
106389395cceSLu Baolu {
106489395cceSLu Baolu }
106589395cceSLu Baolu
iommu_device_claim_dma_owner(struct device * dev,void * owner)106689395cceSLu Baolu static inline int iommu_device_claim_dma_owner(struct device *dev, void *owner)
106789395cceSLu Baolu {
106889395cceSLu Baolu return -ENODEV;
106989395cceSLu Baolu }
107089395cceSLu Baolu
107113646796SLu Baolu static inline struct iommu_domain *
iommu_sva_domain_alloc(struct device * dev,struct mm_struct * mm)107213646796SLu Baolu iommu_sva_domain_alloc(struct device *dev, struct mm_struct *mm)
107313646796SLu Baolu {
107413646796SLu Baolu return NULL;
107513646796SLu Baolu }
107613646796SLu Baolu
iommu_attach_device_pasid(struct iommu_domain * domain,struct device * dev,ioasid_t pasid)107716603704SLu Baolu static inline int iommu_attach_device_pasid(struct iommu_domain *domain,
107816603704SLu Baolu struct device *dev, ioasid_t pasid)
107916603704SLu Baolu {
108016603704SLu Baolu return -ENODEV;
108116603704SLu Baolu }
108216603704SLu Baolu
iommu_detach_device_pasid(struct iommu_domain * domain,struct device * dev,ioasid_t pasid)108316603704SLu Baolu static inline void iommu_detach_device_pasid(struct iommu_domain *domain,
108416603704SLu Baolu struct device *dev, ioasid_t pasid)
108516603704SLu Baolu {
108616603704SLu Baolu }
108716603704SLu Baolu
108816603704SLu Baolu static inline struct iommu_domain *
iommu_get_domain_for_dev_pasid(struct device * dev,ioasid_t pasid,unsigned int type)108916603704SLu Baolu iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid,
109016603704SLu Baolu unsigned int type)
109116603704SLu Baolu {
109216603704SLu Baolu return NULL;
109316603704SLu Baolu }
10942dcebc7dSJacob Pan
iommu_alloc_global_pasid(struct device * dev)10952dcebc7dSJacob Pan static inline ioasid_t iommu_alloc_global_pasid(struct device *dev)
10962dcebc7dSJacob Pan {
10972dcebc7dSJacob Pan return IOMMU_PASID_INVALID;
10982dcebc7dSJacob Pan }
10992dcebc7dSJacob Pan
iommu_free_global_pasid(ioasid_t pasid)11002dcebc7dSJacob Pan static inline void iommu_free_global_pasid(ioasid_t pasid) {}
11014a77a6cfSJoerg Roedel #endif /* CONFIG_IOMMU_API */
11024a77a6cfSJoerg Roedel
1103ca37faf3SMarek Szyprowski /**
1104ca37faf3SMarek Szyprowski * iommu_map_sgtable - Map the given buffer to the IOMMU domain
1105ca37faf3SMarek Szyprowski * @domain: The IOMMU domain to perform the mapping
1106ca37faf3SMarek Szyprowski * @iova: The start address to map the buffer
1107ca37faf3SMarek Szyprowski * @sgt: The sg_table object describing the buffer
1108ca37faf3SMarek Szyprowski * @prot: IOMMU protection bits
1109ca37faf3SMarek Szyprowski *
1110ca37faf3SMarek Szyprowski * Creates a mapping at @iova for the buffer described by a scatterlist
1111ca37faf3SMarek Szyprowski * stored in the given sg_table object in the provided IOMMU domain.
1112ca37faf3SMarek Szyprowski */
iommu_map_sgtable(struct iommu_domain * domain,unsigned long iova,struct sg_table * sgt,int prot)1113ca37faf3SMarek Szyprowski static inline size_t iommu_map_sgtable(struct iommu_domain *domain,
1114ca37faf3SMarek Szyprowski unsigned long iova, struct sg_table *sgt, int prot)
1115ca37faf3SMarek Szyprowski {
1116f2b2c051SJason Gunthorpe return iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, prot,
1117f2b2c051SJason Gunthorpe GFP_KERNEL);
1118ca37faf3SMarek Szyprowski }
1119ca37faf3SMarek Szyprowski
1120bad614b2SGary R Hook #ifdef CONFIG_IOMMU_DEBUGFS
1121bad614b2SGary R Hook extern struct dentry *iommu_debugfs_dir;
1122bad614b2SGary R Hook void iommu_debugfs_setup(void);
1123bad614b2SGary R Hook #else
iommu_debugfs_setup(void)1124bad614b2SGary R Hook static inline void iommu_debugfs_setup(void) {}
1125bad614b2SGary R Hook #endif
1126bad614b2SGary R Hook
1127fa49364cSRobin Murphy #ifdef CONFIG_IOMMU_DMA
1128fa49364cSRobin Murphy #include <linux/msi.h>
1129fa49364cSRobin Murphy
1130fa49364cSRobin Murphy /* Setup call for arch DMA mapping code */
1131fa49364cSRobin Murphy void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit);
1132fa49364cSRobin Murphy
1133fa49364cSRobin Murphy int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);
1134fa49364cSRobin Murphy
1135fa49364cSRobin Murphy int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr);
1136fa49364cSRobin Murphy void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg);
1137fa49364cSRobin Murphy
1138fa49364cSRobin Murphy #else /* CONFIG_IOMMU_DMA */
1139fa49364cSRobin Murphy
1140fa49364cSRobin Murphy struct msi_desc;
1141fa49364cSRobin Murphy struct msi_msg;
1142fa49364cSRobin Murphy
iommu_setup_dma_ops(struct device * dev,u64 dma_base,u64 dma_limit)1143fa49364cSRobin Murphy static inline void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit)
1144fa49364cSRobin Murphy {
1145fa49364cSRobin Murphy }
1146fa49364cSRobin Murphy
iommu_get_msi_cookie(struct iommu_domain * domain,dma_addr_t base)1147fa49364cSRobin Murphy static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
1148fa49364cSRobin Murphy {
1149fa49364cSRobin Murphy return -ENODEV;
1150fa49364cSRobin Murphy }
1151fa49364cSRobin Murphy
iommu_dma_prepare_msi(struct msi_desc * desc,phys_addr_t msi_addr)1152fa49364cSRobin Murphy static inline int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
1153fa49364cSRobin Murphy {
1154fa49364cSRobin Murphy return 0;
1155fa49364cSRobin Murphy }
1156fa49364cSRobin Murphy
iommu_dma_compose_msi_msg(struct msi_desc * desc,struct msi_msg * msg)1157fa49364cSRobin Murphy static inline void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
1158fa49364cSRobin Murphy {
1159fa49364cSRobin Murphy }
1160fa49364cSRobin Murphy
1161fa49364cSRobin Murphy #endif /* CONFIG_IOMMU_DMA */
1162fa49364cSRobin Murphy
1163493c9b68SThierry Reding /*
1164493c9b68SThierry Reding * Newer generations of Tegra SoCs require devices' stream IDs to be directly programmed into
1165493c9b68SThierry Reding * some registers. These are always paired with a Tegra SMMU or ARM SMMU, for which the contents
1166493c9b68SThierry Reding * of the struct iommu_fwspec are known. Use this helper to formalize access to these internals.
1167493c9b68SThierry Reding */
1168493c9b68SThierry Reding #define TEGRA_STREAM_ID_BYPASS 0x7f
1169493c9b68SThierry Reding
tegra_dev_iommu_get_stream_id(struct device * dev,u32 * stream_id)1170493c9b68SThierry Reding static inline bool tegra_dev_iommu_get_stream_id(struct device *dev, u32 *stream_id)
1171493c9b68SThierry Reding {
1172493c9b68SThierry Reding #ifdef CONFIG_IOMMU_API
1173493c9b68SThierry Reding struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1174493c9b68SThierry Reding
1175493c9b68SThierry Reding if (fwspec && fwspec->num_ids == 1) {
1176493c9b68SThierry Reding *stream_id = fwspec->ids[0] & 0xffff;
1177493c9b68SThierry Reding return true;
1178493c9b68SThierry Reding }
1179493c9b68SThierry Reding #endif
1180493c9b68SThierry Reding
1181493c9b68SThierry Reding return false;
1182493c9b68SThierry Reding }
1183493c9b68SThierry Reding
1184be51b1d6SLu Baolu #ifdef CONFIG_IOMMU_SVA
mm_pasid_init(struct mm_struct * mm)1185cd389115SJacob Pan static inline void mm_pasid_init(struct mm_struct *mm)
1186cd389115SJacob Pan {
1187fffaed1eSJacob Pan mm->pasid = IOMMU_PASID_INVALID;
1188cd389115SJacob Pan }
mm_valid_pasid(struct mm_struct * mm)118958390c8cSLinus Torvalds static inline bool mm_valid_pasid(struct mm_struct *mm)
119058390c8cSLinus Torvalds {
119158390c8cSLinus Torvalds return mm->pasid != IOMMU_PASID_INVALID;
119258390c8cSLinus Torvalds }
1193cd389115SJacob Pan void mm_pasid_drop(struct mm_struct *mm);
1194be51b1d6SLu Baolu struct iommu_sva *iommu_sva_bind_device(struct device *dev,
1195be51b1d6SLu Baolu struct mm_struct *mm);
1196be51b1d6SLu Baolu void iommu_sva_unbind_device(struct iommu_sva *handle);
1197be51b1d6SLu Baolu u32 iommu_sva_get_pasid(struct iommu_sva *handle);
1198be51b1d6SLu Baolu #else
1199be51b1d6SLu Baolu static inline struct iommu_sva *
iommu_sva_bind_device(struct device * dev,struct mm_struct * mm)1200be51b1d6SLu Baolu iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
1201be51b1d6SLu Baolu {
1202be51b1d6SLu Baolu return NULL;
1203be51b1d6SLu Baolu }
1204be51b1d6SLu Baolu
iommu_sva_unbind_device(struct iommu_sva * handle)1205be51b1d6SLu Baolu static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
1206be51b1d6SLu Baolu {
1207be51b1d6SLu Baolu }
1208be51b1d6SLu Baolu
iommu_sva_get_pasid(struct iommu_sva * handle)1209be51b1d6SLu Baolu static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)
1210be51b1d6SLu Baolu {
1211be51b1d6SLu Baolu return IOMMU_PASID_INVALID;
1212be51b1d6SLu Baolu }
mm_pasid_init(struct mm_struct * mm)1213cd389115SJacob Pan static inline void mm_pasid_init(struct mm_struct *mm) {}
mm_valid_pasid(struct mm_struct * mm)121458390c8cSLinus Torvalds static inline bool mm_valid_pasid(struct mm_struct *mm) { return false; }
mm_pasid_drop(struct mm_struct * mm)1215cd389115SJacob Pan static inline void mm_pasid_drop(struct mm_struct *mm) {}
1216be51b1d6SLu Baolu #endif /* CONFIG_IOMMU_SVA */
1217be51b1d6SLu Baolu
12184a77a6cfSJoerg Roedel #endif /* __LINUX_IOMMU_H */
1219