Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 .. iommu:
0003 
0004 =====================================
0005 IOMMU Userspace API
0006 =====================================
0007 
0008 IOMMU UAPI is used for virtualization cases where communications are
0009 needed between physical and virtual IOMMU drivers. For baremetal
0010 usage, the IOMMU is a system device which does not need to communicate
0011 with userspace directly.
0012 
0013 The primary use cases are guest Shared Virtual Address (SVA) and
0014 guest IO virtual address (IOVA), wherein the vIOMMU implementation
0015 relies on the physical IOMMU and for this reason requires interactions
0016 with the host driver.
0017 
0018 .. contents:: :local:
0019 
0020 Functionalities
0021 ===============
0022 Communications of user and kernel involve both directions. The
0023 supported user-kernel APIs are as follows:
0024 
0025 1. Bind/Unbind guest PASID (e.g. Intel VT-d)
0026 2. Bind/Unbind guest PASID table (e.g. ARM SMMU)
0027 3. Invalidate IOMMU caches upon guest requests
0028 4. Report errors to the guest and serve page requests
0029 
0030 Requirements
0031 ============
0032 The IOMMU UAPIs are generic and extensible to meet the following
0033 requirements:
0034 
0035 1. Emulated and para-virtualised vIOMMUs
0036 2. Multiple vendors (Intel VT-d, ARM SMMU, etc.)
0037 3. Extensions to the UAPI shall not break existing userspace
0038 
0039 Interfaces
0040 ==========
0041 Although the data structures defined in IOMMU UAPI are self-contained,
0042 there are no user API functions introduced. Instead, IOMMU UAPI is
0043 designed to work with existing user driver frameworks such as VFIO.
0044 
0045 Extension Rules & Precautions
0046 -----------------------------
0047 When IOMMU UAPI gets extended, the data structures can *only* be
0048 modified in two ways:
0049 
0050 1. Adding new fields by re-purposing the padding[] field. No size change.
0051 2. Adding new union members at the end. May increase the structure sizes.
0052 
0053 No new fields can be added *after* the variable sized union in that it
0054 will break backward compatibility when offset moves. A new flag must
0055 be introduced whenever a change affects the structure using either
0056 method. The IOMMU driver processes the data based on flags which
0057 ensures backward compatibility.
0058 
0059 Version field is only reserved for the unlikely event of UAPI upgrade
0060 at its entirety.
0061 
0062 It's *always* the caller's responsibility to indicate the size of the
0063 structure passed by setting argsz appropriately.
0064 Though at the same time, argsz is user provided data which is not
0065 trusted. The argsz field allows the user app to indicate how much data
0066 it is providing; it's still the kernel's responsibility to validate
0067 whether it's correct and sufficient for the requested operation.
0068 
0069 Compatibility Checking
0070 ----------------------
0071 When IOMMU UAPI extension results in some structure size increase,
0072 IOMMU UAPI code shall handle the following cases:
0073 
0074 1. User and kernel has exact size match
0075 2. An older user with older kernel header (smaller UAPI size) running on a
0076    newer kernel (larger UAPI size)
0077 3. A newer user with newer kernel header (larger UAPI size) running
0078    on an older kernel.
0079 4. A malicious/misbehaving user passing illegal/invalid size but within
0080    range. The data may contain garbage.
0081 
0082 Feature Checking
0083 ----------------
0084 While launching a guest with vIOMMU, it is strongly advised to check
0085 the compatibility upfront, as some subsequent errors happening during
0086 vIOMMU operation, such as cache invalidation failures cannot be nicely
0087 escalated to the guest due to IOMMU specifications. This can lead to
0088 catastrophic failures for the users.
0089 
0090 User applications such as QEMU are expected to import kernel UAPI
0091 headers. Backward compatibility is supported per feature flags.
0092 For example, an older QEMU (with older kernel header) can run on newer
0093 kernel. Newer QEMU (with new kernel header) may refuse to initialize
0094 on an older kernel if new feature flags are not supported by older
0095 kernel. Simply recompiling existing code with newer kernel header should
0096 not be an issue in that only existing flags are used.
0097 
0098 IOMMU vendor driver should report the below features to IOMMU UAPI
0099 consumers (e.g. via VFIO).
0100 
0101 1. IOMMU_NESTING_FEAT_SYSWIDE_PASID
0102 2. IOMMU_NESTING_FEAT_BIND_PGTBL
0103 3. IOMMU_NESTING_FEAT_BIND_PASID_TABLE
0104 4. IOMMU_NESTING_FEAT_CACHE_INVLD
0105 5. IOMMU_NESTING_FEAT_PAGE_REQUEST
0106 
0107 Take VFIO as example, upon request from VFIO userspace (e.g. QEMU),
0108 VFIO kernel code shall query IOMMU vendor driver for the support of
0109 the above features. Query result can then be reported back to the
0110 userspace caller. Details can be found in
0111 Documentation/driver-api/vfio.rst.
0112 
0113 
0114 Data Passing Example with VFIO
0115 ------------------------------
0116 As the ubiquitous userspace driver framework, VFIO is already IOMMU
0117 aware and shares many key concepts such as device model, group, and
0118 protection domain. Other user driver frameworks can also be extended
0119 to support IOMMU UAPI but it is outside the scope of this document.
0120 
0121 In this tight-knit VFIO-IOMMU interface, the ultimate consumer of the
0122 IOMMU UAPI data is the host IOMMU driver. VFIO facilitates user-kernel
0123 transport, capability checking, security, and life cycle management of
0124 process address space ID (PASID).
0125 
0126 VFIO layer conveys the data structures down to the IOMMU driver. It
0127 follows the pattern below::
0128 
0129    struct {
0130         __u32 argsz;
0131         __u32 flags;
0132         __u8  data[];
0133    };
0134 
0135 Here data[] contains the IOMMU UAPI data structures. VFIO has the
0136 freedom to bundle the data as well as parse data size based on its own flags.
0137 
0138 In order to determine the size and feature set of the user data, argsz
0139 and flags (or the equivalent) are also embedded in the IOMMU UAPI data
0140 structures.
0141 
0142 A "__u32 argsz" field is *always* at the beginning of each structure.
0143 
0144 For example:
0145 ::
0146 
0147    struct iommu_cache_invalidate_info {
0148         __u32   argsz;
0149         #define IOMMU_CACHE_INVALIDATE_INFO_VERSION_1 1
0150         __u32   version;
0151         /* IOMMU paging structure cache */
0152         #define IOMMU_CACHE_INV_TYPE_IOTLB      (1 << 0) /* IOMMU IOTLB */
0153         #define IOMMU_CACHE_INV_TYPE_DEV_IOTLB  (1 << 1) /* Device IOTLB */
0154         #define IOMMU_CACHE_INV_TYPE_PASID      (1 << 2) /* PASID cache */
0155         #define IOMMU_CACHE_INV_TYPE_NR         (3)
0156         __u8    cache;
0157         __u8    granularity;
0158         __u8    padding[6];
0159         union {
0160                 struct iommu_inv_pasid_info pasid_info;
0161                 struct iommu_inv_addr_info addr_info;
0162         } granu;
0163    };
0164 
0165 VFIO is responsible for checking its own argsz and flags. It then
0166 invokes appropriate IOMMU UAPI functions. The user pointers are passed
0167 to the IOMMU layer for further processing. The responsibilities are
0168 divided as follows:
0169 
0170 - Generic IOMMU layer checks argsz range based on UAPI data in the
0171   current kernel version.
0172 
0173 - Generic IOMMU layer checks content of the UAPI data for non-zero
0174   reserved bits in flags, padding fields, and unsupported version.
0175   This is to ensure not breaking userspace in the future when these
0176   fields or flags are used.
0177 
0178 - Vendor IOMMU driver checks argsz based on vendor flags. UAPI data
0179   is consumed based on flags. Vendor driver has access to
0180   unadulterated argsz value in case of vendor specific future
0181   extensions. Currently, it does not perform the copy_from_user()
0182   itself. A __user pointer can be provided in some future scenarios
0183   where there's vendor data outside of the structure definition.
0184 
0185 IOMMU code treats UAPI data in two categories:
0186 
0187 - structure contains vendor data
0188   (Example: iommu_uapi_cache_invalidate())
0189 
0190 - structure contains only generic data
0191   (Example: iommu_uapi_sva_bind_gpasid())
0192 
0193 
0194 
0195 Sharing UAPI with in-kernel users
0196 ---------------------------------
0197 For UAPIs that are shared with in-kernel users, a wrapper function is
0198 provided to distinguish the callers. For example,
0199 
0200 Userspace caller ::
0201 
0202   int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain,
0203                                    struct device *dev,
0204                                    void __user *udata)
0205 
0206 In-kernel caller ::
0207 
0208   int iommu_sva_unbind_gpasid(struct iommu_domain *domain,
0209                               struct device *dev, ioasid_t ioasid);