1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2607ca46eSDavid Howells /* 3607ca46eSDavid Howells * VFIO API definition 4607ca46eSDavid Howells * 5607ca46eSDavid Howells * Copyright (C) 2012 Red Hat, Inc. All rights reserved. 6607ca46eSDavid Howells * Author: Alex Williamson <alex.williamson@redhat.com> 7607ca46eSDavid Howells * 8607ca46eSDavid Howells * This program is free software; you can redistribute it and/or modify 9607ca46eSDavid Howells * it under the terms of the GNU General Public License version 2 as 10607ca46eSDavid Howells * published by the Free Software Foundation. 11607ca46eSDavid Howells */ 12607ca46eSDavid Howells #ifndef _UAPIVFIO_H 13607ca46eSDavid Howells #define _UAPIVFIO_H 14607ca46eSDavid Howells 15607ca46eSDavid Howells #include <linux/types.h> 16607ca46eSDavid Howells #include <linux/ioctl.h> 17607ca46eSDavid Howells 18607ca46eSDavid Howells #define VFIO_API_VERSION 0 19607ca46eSDavid Howells 20607ca46eSDavid Howells 21607ca46eSDavid Howells /* Kernel & User level defines for VFIO IOCTLs. */ 22607ca46eSDavid Howells 23607ca46eSDavid Howells /* Extensions */ 24607ca46eSDavid Howells 25607ca46eSDavid Howells #define VFIO_TYPE1_IOMMU 1 265ffd229cSAlexey Kardashevskiy #define VFIO_SPAPR_TCE_IOMMU 2 271ef3e2bcSAlex Williamson #define VFIO_TYPE1v2_IOMMU 3 28aa429318SAlex Williamson /* 29aa429318SAlex Williamson * IOMMU enforces DMA cache coherence (ex. PCIe NoSnoop stripping). This 30aa429318SAlex Williamson * capability is subject to change as groups are added or removed. 31aa429318SAlex Williamson */ 32aa429318SAlex Williamson #define VFIO_DMA_CC_IOMMU 4 33607ca46eSDavid Howells 341b69be5eSGavin Shan /* Check if EEH is supported */ 351b69be5eSGavin Shan #define VFIO_EEH 5 361b69be5eSGavin Shan 37f5c9ecebSWill Deacon /* Two-stage IOMMU */ 38f5c9ecebSWill Deacon #define VFIO_TYPE1_NESTING_IOMMU 6 /* Implies v2 */ 39f5c9ecebSWill Deacon 402157e7b8SAlexey Kardashevskiy #define VFIO_SPAPR_TCE_v2_IOMMU 7 412157e7b8SAlexey Kardashevskiy 42607ca46eSDavid Howells /* 4303a76b60SAlex Williamson * The No-IOMMU IOMMU offers no translation or isolation for devices and 4403a76b60SAlex Williamson * supports no ioctls outside of VFIO_CHECK_EXTENSION. Use of VFIO's No-IOMMU 4503a76b60SAlex Williamson * code will taint the host kernel and should be used with extreme caution. 4603a76b60SAlex Williamson */ 4703a76b60SAlex Williamson #define VFIO_NOIOMMU_IOMMU 8 4803a76b60SAlex Williamson 49c98fe7c2SSteve Sistare /* Supports VFIO_DMA_UNMAP_FLAG_ALL */ 50c98fe7c2SSteve Sistare #define VFIO_UNMAP_ALL 9 51c98fe7c2SSteve Sistare 52ef3a3f6aSSteve Sistare /* 53ef3a3f6aSSteve Sistare * Supports the vaddr flag for DMA map and unmap. Not supported for mediated 54ef3a3f6aSSteve Sistare * devices, so this capability is subject to change as groups are added or 55ef3a3f6aSSteve Sistare * removed. 56ef3a3f6aSSteve Sistare */ 57441e8106SSteve Sistare #define VFIO_UPDATE_VADDR 10 58441e8106SSteve Sistare 5903a76b60SAlex Williamson /* 60607ca46eSDavid Howells * The IOCTL interface is designed for extensibility by embedding the 61607ca46eSDavid Howells * structure length (argsz) and flags into structures passed between 62607ca46eSDavid Howells * kernel and userspace. We therefore use the _IO() macro for these 63607ca46eSDavid Howells * defines to avoid implicitly embedding a size into the ioctl request. 64607ca46eSDavid Howells * As structure fields are added, argsz will increase to match and flag 65607ca46eSDavid Howells * bits will be defined to indicate additional fields with valid data. 66607ca46eSDavid Howells * It's *always* the caller's responsibility to indicate the size of 67607ca46eSDavid Howells * the structure passed by setting argsz appropriately. 68607ca46eSDavid Howells */ 69607ca46eSDavid Howells 70607ca46eSDavid Howells #define VFIO_TYPE (';') 71607ca46eSDavid Howells #define VFIO_BASE 100 72607ca46eSDavid Howells 73c84982adSAlex Williamson /* 74c84982adSAlex Williamson * For extension of INFO ioctls, VFIO makes use of a capability chain 75c84982adSAlex Williamson * designed after PCI/e capabilities. A flag bit indicates whether 76c84982adSAlex Williamson * this capability chain is supported and a field defined in the fixed 77c84982adSAlex Williamson * structure defines the offset of the first capability in the chain. 78c84982adSAlex Williamson * This field is only valid when the corresponding bit in the flags 79c84982adSAlex Williamson * bitmap is set. This offset field is relative to the start of the 80c84982adSAlex Williamson * INFO buffer, as is the next field within each capability header. 81c84982adSAlex Williamson * The id within the header is a shared address space per INFO ioctl, 82c84982adSAlex Williamson * while the version field is specific to the capability id. The 83c84982adSAlex Williamson * contents following the header are specific to the capability id. 84c84982adSAlex Williamson */ 85c84982adSAlex Williamson struct vfio_info_cap_header { 86c84982adSAlex Williamson __u16 id; /* Identifies capability */ 87c84982adSAlex Williamson __u16 version; /* Version specific to the capability ID */ 88c84982adSAlex Williamson __u32 next; /* Offset of next capability */ 89c84982adSAlex Williamson }; 90c84982adSAlex Williamson 91c84982adSAlex Williamson /* 92c84982adSAlex Williamson * Callers of INFO ioctls passing insufficiently sized buffers will see 93c84982adSAlex Williamson * the capability chain flag bit set, a zero value for the first capability 94c84982adSAlex Williamson * offset (if available within the provided argsz), and argsz will be 95c84982adSAlex Williamson * updated to report the necessary buffer size. For compatibility, the 96c84982adSAlex Williamson * INFO ioctl will not report error in this case, but the capability chain 97c84982adSAlex Williamson * will not be available. 98c84982adSAlex Williamson */ 99c84982adSAlex Williamson 100607ca46eSDavid Howells /* -------- IOCTLs for VFIO file descriptor (/dev/vfio/vfio) -------- */ 101607ca46eSDavid Howells 102607ca46eSDavid Howells /** 103607ca46eSDavid Howells * VFIO_GET_API_VERSION - _IO(VFIO_TYPE, VFIO_BASE + 0) 104607ca46eSDavid Howells * 105607ca46eSDavid Howells * Report the version of the VFIO API. This allows us to bump the entire 106607ca46eSDavid Howells * API version should we later need to add or change features in incompatible 107607ca46eSDavid Howells * ways. 108607ca46eSDavid Howells * Return: VFIO_API_VERSION 109607ca46eSDavid Howells * Availability: Always 110607ca46eSDavid Howells */ 111607ca46eSDavid Howells #define VFIO_GET_API_VERSION _IO(VFIO_TYPE, VFIO_BASE + 0) 112607ca46eSDavid Howells 113607ca46eSDavid Howells /** 114607ca46eSDavid Howells * VFIO_CHECK_EXTENSION - _IOW(VFIO_TYPE, VFIO_BASE + 1, __u32) 115607ca46eSDavid Howells * 116607ca46eSDavid Howells * Check whether an extension is supported. 117607ca46eSDavid Howells * Return: 0 if not supported, 1 (or some other positive integer) if supported. 118607ca46eSDavid Howells * Availability: Always 119607ca46eSDavid Howells */ 120607ca46eSDavid Howells #define VFIO_CHECK_EXTENSION _IO(VFIO_TYPE, VFIO_BASE + 1) 121607ca46eSDavid Howells 122607ca46eSDavid Howells /** 123607ca46eSDavid Howells * VFIO_SET_IOMMU - _IOW(VFIO_TYPE, VFIO_BASE + 2, __s32) 124607ca46eSDavid Howells * 125607ca46eSDavid Howells * Set the iommu to the given type. The type must be supported by an 126607ca46eSDavid Howells * iommu driver as verified by calling CHECK_EXTENSION using the same 127607ca46eSDavid Howells * type. A group must be set to this file descriptor before this 128607ca46eSDavid Howells * ioctl is available. The IOMMU interfaces enabled by this call are 129607ca46eSDavid Howells * specific to the value set. 130607ca46eSDavid Howells * Return: 0 on success, -errno on failure 131607ca46eSDavid Howells * Availability: When VFIO group attached 132607ca46eSDavid Howells */ 133607ca46eSDavid Howells #define VFIO_SET_IOMMU _IO(VFIO_TYPE, VFIO_BASE + 2) 134607ca46eSDavid Howells 135607ca46eSDavid Howells /* -------- IOCTLs for GROUP file descriptors (/dev/vfio/$GROUP) -------- */ 136607ca46eSDavid Howells 137607ca46eSDavid Howells /** 138607ca46eSDavid Howells * VFIO_GROUP_GET_STATUS - _IOR(VFIO_TYPE, VFIO_BASE + 3, 139607ca46eSDavid Howells * struct vfio_group_status) 140607ca46eSDavid Howells * 141607ca46eSDavid Howells * Retrieve information about the group. Fills in provided 142607ca46eSDavid Howells * struct vfio_group_info. Caller sets argsz. 143607ca46eSDavid Howells * Return: 0 on succes, -errno on failure. 144607ca46eSDavid Howells * Availability: Always 145607ca46eSDavid Howells */ 146607ca46eSDavid Howells struct vfio_group_status { 147607ca46eSDavid Howells __u32 argsz; 148607ca46eSDavid Howells __u32 flags; 149607ca46eSDavid Howells #define VFIO_GROUP_FLAGS_VIABLE (1 << 0) 150607ca46eSDavid Howells #define VFIO_GROUP_FLAGS_CONTAINER_SET (1 << 1) 151607ca46eSDavid Howells }; 152607ca46eSDavid Howells #define VFIO_GROUP_GET_STATUS _IO(VFIO_TYPE, VFIO_BASE + 3) 153607ca46eSDavid Howells 154607ca46eSDavid Howells /** 155607ca46eSDavid Howells * VFIO_GROUP_SET_CONTAINER - _IOW(VFIO_TYPE, VFIO_BASE + 4, __s32) 156607ca46eSDavid Howells * 157607ca46eSDavid Howells * Set the container for the VFIO group to the open VFIO file 158607ca46eSDavid Howells * descriptor provided. Groups may only belong to a single 159607ca46eSDavid Howells * container. Containers may, at their discretion, support multiple 160607ca46eSDavid Howells * groups. Only when a container is set are all of the interfaces 161607ca46eSDavid Howells * of the VFIO file descriptor and the VFIO group file descriptor 162607ca46eSDavid Howells * available to the user. 163607ca46eSDavid Howells * Return: 0 on success, -errno on failure. 164607ca46eSDavid Howells * Availability: Always 165607ca46eSDavid Howells */ 166607ca46eSDavid Howells #define VFIO_GROUP_SET_CONTAINER _IO(VFIO_TYPE, VFIO_BASE + 4) 167607ca46eSDavid Howells 168607ca46eSDavid Howells /** 169607ca46eSDavid Howells * VFIO_GROUP_UNSET_CONTAINER - _IO(VFIO_TYPE, VFIO_BASE + 5) 170607ca46eSDavid Howells * 171607ca46eSDavid Howells * Remove the group from the attached container. This is the 172607ca46eSDavid Howells * opposite of the SET_CONTAINER call and returns the group to 173607ca46eSDavid Howells * an initial state. All device file descriptors must be released 174607ca46eSDavid Howells * prior to calling this interface. When removing the last group 175607ca46eSDavid Howells * from a container, the IOMMU will be disabled and all state lost, 176607ca46eSDavid Howells * effectively also returning the VFIO file descriptor to an initial 177607ca46eSDavid Howells * state. 178607ca46eSDavid Howells * Return: 0 on success, -errno on failure. 179607ca46eSDavid Howells * Availability: When attached to container 180607ca46eSDavid Howells */ 181607ca46eSDavid Howells #define VFIO_GROUP_UNSET_CONTAINER _IO(VFIO_TYPE, VFIO_BASE + 5) 182607ca46eSDavid Howells 183607ca46eSDavid Howells /** 184607ca46eSDavid Howells * VFIO_GROUP_GET_DEVICE_FD - _IOW(VFIO_TYPE, VFIO_BASE + 6, char) 185607ca46eSDavid Howells * 186607ca46eSDavid Howells * Return a new file descriptor for the device object described by 187607ca46eSDavid Howells * the provided string. The string should match a device listed in 188607ca46eSDavid Howells * the devices subdirectory of the IOMMU group sysfs entry. The 189607ca46eSDavid Howells * group containing the device must already be added to this context. 190607ca46eSDavid Howells * Return: new file descriptor on success, -errno on failure. 191607ca46eSDavid Howells * Availability: When attached to container 192607ca46eSDavid Howells */ 193607ca46eSDavid Howells #define VFIO_GROUP_GET_DEVICE_FD _IO(VFIO_TYPE, VFIO_BASE + 6) 194607ca46eSDavid Howells 195607ca46eSDavid Howells /* --------------- IOCTLs for DEVICE file descriptors --------------- */ 196607ca46eSDavid Howells 197607ca46eSDavid Howells /** 198607ca46eSDavid Howells * VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7, 199607ca46eSDavid Howells * struct vfio_device_info) 200607ca46eSDavid Howells * 201607ca46eSDavid Howells * Retrieve information about the device. Fills in provided 202607ca46eSDavid Howells * struct vfio_device_info. Caller sets argsz. 203607ca46eSDavid Howells * Return: 0 on success, -errno on failure. 204607ca46eSDavid Howells */ 205607ca46eSDavid Howells struct vfio_device_info { 206607ca46eSDavid Howells __u32 argsz; 207607ca46eSDavid Howells __u32 flags; 208607ca46eSDavid Howells #define VFIO_DEVICE_FLAGS_RESET (1 << 0) /* Device supports reset */ 209607ca46eSDavid Howells #define VFIO_DEVICE_FLAGS_PCI (1 << 1) /* vfio-pci device */ 2109df85aaaSAntonios Motakis #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2) /* vfio-platform device */ 21136fe431fSAntonios Motakis #define VFIO_DEVICE_FLAGS_AMBA (1 << 3) /* vfio-amba device */ 212e01bcdd6SDong Jia Shi #define VFIO_DEVICE_FLAGS_CCW (1 << 4) /* vfio-ccw device */ 213e06670c5STony Krowiak #define VFIO_DEVICE_FLAGS_AP (1 << 5) /* vfio-ap device */ 214fb1ff4c1SBharat Bhushan #define VFIO_DEVICE_FLAGS_FSL_MC (1 << 6) /* vfio-fsl-mc device */ 2150c633f0bSMatthew Rosato #define VFIO_DEVICE_FLAGS_CAPS (1 << 7) /* Info supports caps */ 216234489acSNipun Gupta #define VFIO_DEVICE_FLAGS_CDX (1 << 8) /* vfio-cdx device */ 217607ca46eSDavid Howells __u32 num_regions; /* Max region index + 1 */ 218607ca46eSDavid Howells __u32 num_irqs; /* Max IRQ index + 1 */ 2190c633f0bSMatthew Rosato __u32 cap_offset; /* Offset within info struct of first cap */ 220a881b496SStefan Hajnoczi __u32 pad; 221607ca46eSDavid Howells }; 222607ca46eSDavid Howells #define VFIO_DEVICE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 7) 223607ca46eSDavid Howells 2242818c6e9SKirti Wankhede /* 2252818c6e9SKirti Wankhede * Vendor driver using Mediated device framework should provide device_api 2262818c6e9SKirti Wankhede * attribute in supported type attribute groups. Device API string should be one 2272818c6e9SKirti Wankhede * of the following corresponding to device flags in vfio_device_info structure. 2282818c6e9SKirti Wankhede */ 2292818c6e9SKirti Wankhede 2302818c6e9SKirti Wankhede #define VFIO_DEVICE_API_PCI_STRING "vfio-pci" 2312818c6e9SKirti Wankhede #define VFIO_DEVICE_API_PLATFORM_STRING "vfio-platform" 2322818c6e9SKirti Wankhede #define VFIO_DEVICE_API_AMBA_STRING "vfio-amba" 233aec390b9SDong Jia Shi #define VFIO_DEVICE_API_CCW_STRING "vfio-ccw" 23465f06713STony Krowiak #define VFIO_DEVICE_API_AP_STRING "vfio-ap" 2352818c6e9SKirti Wankhede 2360c633f0bSMatthew Rosato /* 2370c633f0bSMatthew Rosato * The following capabilities are unique to s390 zPCI devices. Their contents 2380c633f0bSMatthew Rosato * are further-defined in vfio_zdev.h 2390c633f0bSMatthew Rosato */ 2400c633f0bSMatthew Rosato #define VFIO_DEVICE_INFO_CAP_ZPCI_BASE 1 2410c633f0bSMatthew Rosato #define VFIO_DEVICE_INFO_CAP_ZPCI_GROUP 2 2420c633f0bSMatthew Rosato #define VFIO_DEVICE_INFO_CAP_ZPCI_UTIL 3 2430c633f0bSMatthew Rosato #define VFIO_DEVICE_INFO_CAP_ZPCI_PFIP 4 2440c633f0bSMatthew Rosato 245a5bfe22dSAlex Williamson /* 246a5bfe22dSAlex Williamson * The following VFIO_DEVICE_INFO capability reports support for PCIe AtomicOp 247a5bfe22dSAlex Williamson * completion to the root bus with supported widths provided via flags. 248a5bfe22dSAlex Williamson */ 249a5bfe22dSAlex Williamson #define VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP 5 250a5bfe22dSAlex Williamson struct vfio_device_info_cap_pci_atomic_comp { 251a5bfe22dSAlex Williamson struct vfio_info_cap_header header; 252a5bfe22dSAlex Williamson __u32 flags; 253a5bfe22dSAlex Williamson #define VFIO_PCI_ATOMIC_COMP32 (1 << 0) 254a5bfe22dSAlex Williamson #define VFIO_PCI_ATOMIC_COMP64 (1 << 1) 255a5bfe22dSAlex Williamson #define VFIO_PCI_ATOMIC_COMP128 (1 << 2) 256a5bfe22dSAlex Williamson __u32 reserved; 257a5bfe22dSAlex Williamson }; 258a5bfe22dSAlex Williamson 259607ca46eSDavid Howells /** 260607ca46eSDavid Howells * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8, 261607ca46eSDavid Howells * struct vfio_region_info) 262607ca46eSDavid Howells * 263607ca46eSDavid Howells * Retrieve information about a device region. Caller provides 264607ca46eSDavid Howells * struct vfio_region_info with index value set. Caller sets argsz. 265607ca46eSDavid Howells * Implementation of region mapping is bus driver specific. This is 266607ca46eSDavid Howells * intended to describe MMIO, I/O port, as well as bus specific 267607ca46eSDavid Howells * regions (ex. PCI config space). Zero sized regions may be used 268607ca46eSDavid Howells * to describe unimplemented regions (ex. unimplemented PCI BARs). 269607ca46eSDavid Howells * Return: 0 on success, -errno on failure. 270607ca46eSDavid Howells */ 271607ca46eSDavid Howells struct vfio_region_info { 272607ca46eSDavid Howells __u32 argsz; 273607ca46eSDavid Howells __u32 flags; 274607ca46eSDavid Howells #define VFIO_REGION_INFO_FLAG_READ (1 << 0) /* Region supports read */ 275607ca46eSDavid Howells #define VFIO_REGION_INFO_FLAG_WRITE (1 << 1) /* Region supports write */ 276607ca46eSDavid Howells #define VFIO_REGION_INFO_FLAG_MMAP (1 << 2) /* Region supports mmap */ 277ff63eb63SAlex Williamson #define VFIO_REGION_INFO_FLAG_CAPS (1 << 3) /* Info supports caps */ 278607ca46eSDavid Howells __u32 index; /* Region index */ 279ff63eb63SAlex Williamson __u32 cap_offset; /* Offset within info struct of first cap */ 2802f8d25faSStefan Hajnoczi __aligned_u64 size; /* Region size (bytes) */ 2812f8d25faSStefan Hajnoczi __aligned_u64 offset; /* Region offset from start of device fd */ 282607ca46eSDavid Howells }; 283607ca46eSDavid Howells #define VFIO_DEVICE_GET_REGION_INFO _IO(VFIO_TYPE, VFIO_BASE + 8) 284607ca46eSDavid Howells 285ff63eb63SAlex Williamson /* 286ff63eb63SAlex Williamson * The sparse mmap capability allows finer granularity of specifying areas 287ff63eb63SAlex Williamson * within a region with mmap support. When specified, the user should only 288ff63eb63SAlex Williamson * mmap the offset ranges specified by the areas array. mmaps outside of the 289ff63eb63SAlex Williamson * areas specified may fail (such as the range covering a PCI MSI-X table) or 290ff63eb63SAlex Williamson * may result in improper device behavior. 291ff63eb63SAlex Williamson * 292ff63eb63SAlex Williamson * The structures below define version 1 of this capability. 293ff63eb63SAlex Williamson */ 294ff63eb63SAlex Williamson #define VFIO_REGION_INFO_CAP_SPARSE_MMAP 1 295ff63eb63SAlex Williamson 296ff63eb63SAlex Williamson struct vfio_region_sparse_mmap_area { 2972f8d25faSStefan Hajnoczi __aligned_u64 offset; /* Offset of mmap'able area within region */ 2982f8d25faSStefan Hajnoczi __aligned_u64 size; /* Size of mmap'able area */ 299ff63eb63SAlex Williamson }; 300ff63eb63SAlex Williamson 301ff63eb63SAlex Williamson struct vfio_region_info_cap_sparse_mmap { 302ff63eb63SAlex Williamson struct vfio_info_cap_header header; 303ff63eb63SAlex Williamson __u32 nr_areas; 304ff63eb63SAlex Williamson __u32 reserved; 305ff63eb63SAlex Williamson struct vfio_region_sparse_mmap_area areas[]; 306ff63eb63SAlex Williamson }; 307ff63eb63SAlex Williamson 308c7bb4cb4SAlex Williamson /* 309c7bb4cb4SAlex Williamson * The device specific type capability allows regions unique to a specific 310c7bb4cb4SAlex Williamson * device or class of devices to be exposed. This helps solve the problem for 311c7bb4cb4SAlex Williamson * vfio bus drivers of defining which region indexes correspond to which region 312c7bb4cb4SAlex Williamson * on the device, without needing to resort to static indexes, as done by 313c7bb4cb4SAlex Williamson * vfio-pci. For instance, if we were to go back in time, we might remove 314c7bb4cb4SAlex Williamson * VFIO_PCI_VGA_REGION_INDEX and let vfio-pci simply define that all indexes 315c7bb4cb4SAlex Williamson * greater than or equal to VFIO_PCI_NUM_REGIONS are device specific and we'd 316c7bb4cb4SAlex Williamson * make a "VGA" device specific type to describe the VGA access space. This 317c7bb4cb4SAlex Williamson * means that non-VGA devices wouldn't need to waste this index, and thus the 318c7bb4cb4SAlex Williamson * address space associated with it due to implementation of device file 319c7bb4cb4SAlex Williamson * descriptor offsets in vfio-pci. 320c7bb4cb4SAlex Williamson * 321c7bb4cb4SAlex Williamson * The current implementation is now part of the user ABI, so we can't use this 322c7bb4cb4SAlex Williamson * for VGA, but there are other upcoming use cases, such as opregions for Intel 323c7bb4cb4SAlex Williamson * IGD devices and framebuffers for vGPU devices. We missed VGA, but we'll 324c7bb4cb4SAlex Williamson * use this for future additions. 325c7bb4cb4SAlex Williamson * 326c7bb4cb4SAlex Williamson * The structure below defines version 1 of this capability. 327c7bb4cb4SAlex Williamson */ 328c7bb4cb4SAlex Williamson #define VFIO_REGION_INFO_CAP_TYPE 2 329c7bb4cb4SAlex Williamson 330c7bb4cb4SAlex Williamson struct vfio_region_info_cap_type { 331c7bb4cb4SAlex Williamson struct vfio_info_cap_header header; 332c7bb4cb4SAlex Williamson __u32 type; /* global per bus driver */ 333c7bb4cb4SAlex Williamson __u32 subtype; /* type specific */ 334c7bb4cb4SAlex Williamson }; 335c7bb4cb4SAlex Williamson 336db2cb969SCornelia Huck /* 337db2cb969SCornelia Huck * List of region types, global per bus driver. 338db2cb969SCornelia Huck * If you introduce a new type, please add it here. 339db2cb969SCornelia Huck */ 340db2cb969SCornelia Huck 341db2cb969SCornelia Huck /* PCI region type containing a PCI vendor part */ 3425846ff54SAlex Williamson #define VFIO_REGION_TYPE_PCI_VENDOR_TYPE (1 << 31) 3435846ff54SAlex Williamson #define VFIO_REGION_TYPE_PCI_VENDOR_MASK (0xffff) 344db2cb969SCornelia Huck #define VFIO_REGION_TYPE_GFX (1) 345db2cb969SCornelia Huck #define VFIO_REGION_TYPE_CCW (2) 3460f3f9cd7SJason Gunthorpe #define VFIO_REGION_TYPE_MIGRATION_DEPRECATED (3) 3475846ff54SAlex Williamson 348db2cb969SCornelia Huck /* sub-types for VFIO_REGION_TYPE_PCI_* */ 349db2cb969SCornelia Huck 350db2cb969SCornelia Huck /* 8086 vendor PCI sub-types */ 3515846ff54SAlex Williamson #define VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION (1) 352f572a960SAlex Williamson #define VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG (2) 353f572a960SAlex Williamson #define VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG (3) 3545846ff54SAlex Williamson 355db2cb969SCornelia Huck /* 10de vendor PCI sub-types */ 35677b8aeb9SAlex Williamson /* 35777b8aeb9SAlex Williamson * NVIDIA GPU NVlink2 RAM is coherent RAM mapped onto the host address space. 35877b8aeb9SAlex Williamson * 35977b8aeb9SAlex Williamson * Deprecated, region no longer provided 36077b8aeb9SAlex Williamson */ 36177b8aeb9SAlex Williamson #define VFIO_REGION_SUBTYPE_NVIDIA_NVLINK2_RAM (1) 362db2cb969SCornelia Huck 363db2cb969SCornelia Huck /* 1014 vendor PCI sub-types */ 36477b8aeb9SAlex Williamson /* 36577b8aeb9SAlex Williamson * IBM NPU NVlink2 ATSD (Address Translation Shootdown) register of NPU 36677b8aeb9SAlex Williamson * to do TLB invalidation on a GPU. 36777b8aeb9SAlex Williamson * 36877b8aeb9SAlex Williamson * Deprecated, region no longer provided 36977b8aeb9SAlex Williamson */ 37077b8aeb9SAlex Williamson #define VFIO_REGION_SUBTYPE_IBM_NVLINK2_ATSD (1) 371db2cb969SCornelia Huck 372db2cb969SCornelia Huck /* sub-types for VFIO_REGION_TYPE_GFX */ 3733cdf7525SGerd Hoffmann #define VFIO_REGION_SUBTYPE_GFX_EDID (1) 3743cdf7525SGerd Hoffmann 3753cdf7525SGerd Hoffmann /** 3763cdf7525SGerd Hoffmann * struct vfio_region_gfx_edid - EDID region layout. 3773cdf7525SGerd Hoffmann * 3783cdf7525SGerd Hoffmann * Set display link state and EDID blob. 3793cdf7525SGerd Hoffmann * 3803cdf7525SGerd Hoffmann * The EDID blob has monitor information such as brand, name, serial 3813cdf7525SGerd Hoffmann * number, physical size, supported video modes and more. 3823cdf7525SGerd Hoffmann * 3833cdf7525SGerd Hoffmann * This special region allows userspace (typically qemu) set a virtual 3843cdf7525SGerd Hoffmann * EDID for the virtual monitor, which allows a flexible display 3853cdf7525SGerd Hoffmann * configuration. 3863cdf7525SGerd Hoffmann * 3873cdf7525SGerd Hoffmann * For the edid blob spec look here: 3883cdf7525SGerd Hoffmann * https://en.wikipedia.org/wiki/Extended_Display_Identification_Data 3893cdf7525SGerd Hoffmann * 3903cdf7525SGerd Hoffmann * On linux systems you can find the EDID blob in sysfs: 3913cdf7525SGerd Hoffmann * /sys/class/drm/${card}/${connector}/edid 3923cdf7525SGerd Hoffmann * 3933cdf7525SGerd Hoffmann * You can use the edid-decode ulility (comes with xorg-x11-utils) to 3943cdf7525SGerd Hoffmann * decode the EDID blob. 3953cdf7525SGerd Hoffmann * 3963cdf7525SGerd Hoffmann * @edid_offset: location of the edid blob, relative to the 3973cdf7525SGerd Hoffmann * start of the region (readonly). 3983cdf7525SGerd Hoffmann * @edid_max_size: max size of the edid blob (readonly). 3993cdf7525SGerd Hoffmann * @edid_size: actual edid size (read/write). 4003cdf7525SGerd Hoffmann * @link_state: display link state (read/write). 4013cdf7525SGerd Hoffmann * VFIO_DEVICE_GFX_LINK_STATE_UP: Monitor is turned on. 4023cdf7525SGerd Hoffmann * VFIO_DEVICE_GFX_LINK_STATE_DOWN: Monitor is turned off. 4033cdf7525SGerd Hoffmann * @max_xres: max display width (0 == no limitation, readonly). 4043cdf7525SGerd Hoffmann * @max_yres: max display height (0 == no limitation, readonly). 4053cdf7525SGerd Hoffmann * 4063cdf7525SGerd Hoffmann * EDID update protocol: 4073cdf7525SGerd Hoffmann * (1) set link-state to down. 4083cdf7525SGerd Hoffmann * (2) update edid blob and size. 4093cdf7525SGerd Hoffmann * (3) set link-state to up. 4103cdf7525SGerd Hoffmann */ 4113cdf7525SGerd Hoffmann struct vfio_region_gfx_edid { 4123cdf7525SGerd Hoffmann __u32 edid_offset; 4133cdf7525SGerd Hoffmann __u32 edid_max_size; 4143cdf7525SGerd Hoffmann __u32 edid_size; 4153cdf7525SGerd Hoffmann __u32 max_xres; 4163cdf7525SGerd Hoffmann __u32 max_yres; 4173cdf7525SGerd Hoffmann __u32 link_state; 4183cdf7525SGerd Hoffmann #define VFIO_DEVICE_GFX_LINK_STATE_UP 1 4193cdf7525SGerd Hoffmann #define VFIO_DEVICE_GFX_LINK_STATE_DOWN 2 4203cdf7525SGerd Hoffmann }; 4213cdf7525SGerd Hoffmann 422db2cb969SCornelia Huck /* sub-types for VFIO_REGION_TYPE_CCW */ 423d5afd5d1SCornelia Huck #define VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD (1) 42424c98674SFarhan Ali #define VFIO_REGION_SUBTYPE_CCW_SCHIB (2) 425d8cac29bSFarhan Ali #define VFIO_REGION_SUBTYPE_CCW_CRW (3) 426db8e5d17SCornelia Huck 427a8a24f3fSKirti Wankhede /* sub-types for VFIO_REGION_TYPE_MIGRATION */ 4280f3f9cd7SJason Gunthorpe #define VFIO_REGION_SUBTYPE_MIGRATION_DEPRECATED (1) 429a8a24f3fSKirti Wankhede 430a8a24f3fSKirti Wankhede struct vfio_device_migration_info { 431a8a24f3fSKirti Wankhede __u32 device_state; /* VFIO device state */ 432115dcec6SJason Gunthorpe #define VFIO_DEVICE_STATE_V1_STOP (0) 433115dcec6SJason Gunthorpe #define VFIO_DEVICE_STATE_V1_RUNNING (1 << 0) 434115dcec6SJason Gunthorpe #define VFIO_DEVICE_STATE_V1_SAVING (1 << 1) 435115dcec6SJason Gunthorpe #define VFIO_DEVICE_STATE_V1_RESUMING (1 << 2) 436115dcec6SJason Gunthorpe #define VFIO_DEVICE_STATE_MASK (VFIO_DEVICE_STATE_V1_RUNNING | \ 437115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_V1_SAVING | \ 438115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_V1_RESUMING) 439a8a24f3fSKirti Wankhede 440a8a24f3fSKirti Wankhede #define VFIO_DEVICE_STATE_VALID(state) \ 441115dcec6SJason Gunthorpe (state & VFIO_DEVICE_STATE_V1_RESUMING ? \ 442115dcec6SJason Gunthorpe (state & VFIO_DEVICE_STATE_MASK) == VFIO_DEVICE_STATE_V1_RESUMING : 1) 443a8a24f3fSKirti Wankhede 444a8a24f3fSKirti Wankhede #define VFIO_DEVICE_STATE_IS_ERROR(state) \ 445115dcec6SJason Gunthorpe ((state & VFIO_DEVICE_STATE_MASK) == (VFIO_DEVICE_STATE_V1_SAVING | \ 446115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_V1_RESUMING)) 447a8a24f3fSKirti Wankhede 448a8a24f3fSKirti Wankhede #define VFIO_DEVICE_STATE_SET_ERROR(state) \ 449115dcec6SJason Gunthorpe ((state & ~VFIO_DEVICE_STATE_MASK) | VFIO_DEVICE_STATE_V1_SAVING | \ 450115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_V1_RESUMING) 451a8a24f3fSKirti Wankhede 452a8a24f3fSKirti Wankhede __u32 reserved; 4532f8d25faSStefan Hajnoczi __aligned_u64 pending_bytes; 4542f8d25faSStefan Hajnoczi __aligned_u64 data_offset; 4552f8d25faSStefan Hajnoczi __aligned_u64 data_size; 456a8a24f3fSKirti Wankhede }; 457a8a24f3fSKirti Wankhede 458a32295c6SAlexey Kardashevskiy /* 459a32295c6SAlexey Kardashevskiy * The MSIX mappable capability informs that MSIX data of a BAR can be mmapped 460a32295c6SAlexey Kardashevskiy * which allows direct access to non-MSIX registers which happened to be within 461a32295c6SAlexey Kardashevskiy * the same system page. 462a32295c6SAlexey Kardashevskiy * 463a32295c6SAlexey Kardashevskiy * Even though the userspace gets direct access to the MSIX data, the existing 464a32295c6SAlexey Kardashevskiy * VFIO_DEVICE_SET_IRQS interface must still be used for MSIX configuration. 465a32295c6SAlexey Kardashevskiy */ 466a32295c6SAlexey Kardashevskiy #define VFIO_REGION_INFO_CAP_MSIX_MAPPABLE 3 467a32295c6SAlexey Kardashevskiy 46877b8aeb9SAlex Williamson /* 46977b8aeb9SAlex Williamson * Capability with compressed real address (aka SSA - small system address) 47077b8aeb9SAlex Williamson * where GPU RAM is mapped on a system bus. Used by a GPU for DMA routing 47177b8aeb9SAlex Williamson * and by the userspace to associate a NVLink bridge with a GPU. 47277b8aeb9SAlex Williamson * 47377b8aeb9SAlex Williamson * Deprecated, capability no longer provided 47477b8aeb9SAlex Williamson */ 47577b8aeb9SAlex Williamson #define VFIO_REGION_INFO_CAP_NVLINK2_SSATGT 4 4767f928917SAlexey Kardashevskiy 47777b8aeb9SAlex Williamson struct vfio_region_info_cap_nvlink2_ssatgt { 47877b8aeb9SAlex Williamson struct vfio_info_cap_header header; 4792f8d25faSStefan Hajnoczi __aligned_u64 tgt; 48077b8aeb9SAlex Williamson }; 48177b8aeb9SAlex Williamson 48277b8aeb9SAlex Williamson /* 48377b8aeb9SAlex Williamson * Capability with an NVLink link speed. The value is read by 48477b8aeb9SAlex Williamson * the NVlink2 bridge driver from the bridge's "ibm,nvlink-speed" 48577b8aeb9SAlex Williamson * property in the device tree. The value is fixed in the hardware 48677b8aeb9SAlex Williamson * and failing to provide the correct value results in the link 48777b8aeb9SAlex Williamson * not working with no indication from the driver why. 48877b8aeb9SAlex Williamson * 48977b8aeb9SAlex Williamson * Deprecated, capability no longer provided 49077b8aeb9SAlex Williamson */ 49177b8aeb9SAlex Williamson #define VFIO_REGION_INFO_CAP_NVLINK2_LNKSPD 5 49277b8aeb9SAlex Williamson 49377b8aeb9SAlex Williamson struct vfio_region_info_cap_nvlink2_lnkspd { 49477b8aeb9SAlex Williamson struct vfio_info_cap_header header; 49577b8aeb9SAlex Williamson __u32 link_speed; 49677b8aeb9SAlex Williamson __u32 __pad; 49777b8aeb9SAlex Williamson }; 4987f928917SAlexey Kardashevskiy 499607ca46eSDavid Howells /** 500607ca46eSDavid Howells * VFIO_DEVICE_GET_IRQ_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 9, 501607ca46eSDavid Howells * struct vfio_irq_info) 502607ca46eSDavid Howells * 503607ca46eSDavid Howells * Retrieve information about a device IRQ. Caller provides 504607ca46eSDavid Howells * struct vfio_irq_info with index value set. Caller sets argsz. 505607ca46eSDavid Howells * Implementation of IRQ mapping is bus driver specific. Indexes 506607ca46eSDavid Howells * using multiple IRQs are primarily intended to support MSI-like 507607ca46eSDavid Howells * interrupt blocks. Zero count irq blocks may be used to describe 508607ca46eSDavid Howells * unimplemented interrupt types. 509607ca46eSDavid Howells * 510607ca46eSDavid Howells * The EVENTFD flag indicates the interrupt index supports eventfd based 511607ca46eSDavid Howells * signaling. 512607ca46eSDavid Howells * 513607ca46eSDavid Howells * The MASKABLE flags indicates the index supports MASK and UNMASK 514607ca46eSDavid Howells * actions described below. 515607ca46eSDavid Howells * 516607ca46eSDavid Howells * AUTOMASKED indicates that after signaling, the interrupt line is 517607ca46eSDavid Howells * automatically masked by VFIO and the user needs to unmask the line 518607ca46eSDavid Howells * to receive new interrupts. This is primarily intended to distinguish 519607ca46eSDavid Howells * level triggered interrupts. 520607ca46eSDavid Howells * 521607ca46eSDavid Howells * The NORESIZE flag indicates that the interrupt lines within the index 522607ca46eSDavid Howells * are setup as a set and new subindexes cannot be enabled without first 523607ca46eSDavid Howells * disabling the entire index. This is used for interrupts like PCI MSI 524607ca46eSDavid Howells * and MSI-X where the driver may only use a subset of the available 525607ca46eSDavid Howells * indexes, but VFIO needs to enable a specific number of vectors 526607ca46eSDavid Howells * upfront. In the case of MSI-X, where the user can enable MSI-X and 527607ca46eSDavid Howells * then add and unmask vectors, it's up to userspace to make the decision 528607ca46eSDavid Howells * whether to allocate the maximum supported number of vectors or tear 529607ca46eSDavid Howells * down setup and incrementally increase the vectors as each is enabled. 5306c8017c6SReinette Chatre * Absence of the NORESIZE flag indicates that vectors can be enabled 5316c8017c6SReinette Chatre * and disabled dynamically without impacting other vectors within the 5326c8017c6SReinette Chatre * index. 533607ca46eSDavid Howells */ 534607ca46eSDavid Howells struct vfio_irq_info { 535607ca46eSDavid Howells __u32 argsz; 536607ca46eSDavid Howells __u32 flags; 537607ca46eSDavid Howells #define VFIO_IRQ_INFO_EVENTFD (1 << 0) 538607ca46eSDavid Howells #define VFIO_IRQ_INFO_MASKABLE (1 << 1) 539607ca46eSDavid Howells #define VFIO_IRQ_INFO_AUTOMASKED (1 << 2) 540607ca46eSDavid Howells #define VFIO_IRQ_INFO_NORESIZE (1 << 3) 541607ca46eSDavid Howells __u32 index; /* IRQ index */ 542607ca46eSDavid Howells __u32 count; /* Number of IRQs within this index */ 543607ca46eSDavid Howells }; 544607ca46eSDavid Howells #define VFIO_DEVICE_GET_IRQ_INFO _IO(VFIO_TYPE, VFIO_BASE + 9) 545607ca46eSDavid Howells 546607ca46eSDavid Howells /** 547607ca46eSDavid Howells * VFIO_DEVICE_SET_IRQS - _IOW(VFIO_TYPE, VFIO_BASE + 10, struct vfio_irq_set) 548607ca46eSDavid Howells * 549607ca46eSDavid Howells * Set signaling, masking, and unmasking of interrupts. Caller provides 550607ca46eSDavid Howells * struct vfio_irq_set with all fields set. 'start' and 'count' indicate 551607ca46eSDavid Howells * the range of subindexes being specified. 552607ca46eSDavid Howells * 553607ca46eSDavid Howells * The DATA flags specify the type of data provided. If DATA_NONE, the 554607ca46eSDavid Howells * operation performs the specified action immediately on the specified 555607ca46eSDavid Howells * interrupt(s). For example, to unmask AUTOMASKED interrupt [0,0]: 556607ca46eSDavid Howells * flags = (DATA_NONE|ACTION_UNMASK), index = 0, start = 0, count = 1. 557607ca46eSDavid Howells * 558607ca46eSDavid Howells * DATA_BOOL allows sparse support for the same on arrays of interrupts. 559607ca46eSDavid Howells * For example, to mask interrupts [0,1] and [0,3] (but not [0,2]): 560607ca46eSDavid Howells * flags = (DATA_BOOL|ACTION_MASK), index = 0, start = 1, count = 3, 561607ca46eSDavid Howells * data = {1,0,1} 562607ca46eSDavid Howells * 563607ca46eSDavid Howells * DATA_EVENTFD binds the specified ACTION to the provided __s32 eventfd. 564607ca46eSDavid Howells * A value of -1 can be used to either de-assign interrupts if already 565607ca46eSDavid Howells * assigned or skip un-assigned interrupts. For example, to set an eventfd 566607ca46eSDavid Howells * to be trigger for interrupts [0,0] and [0,2]: 567607ca46eSDavid Howells * flags = (DATA_EVENTFD|ACTION_TRIGGER), index = 0, start = 0, count = 3, 568607ca46eSDavid Howells * data = {fd1, -1, fd2} 569607ca46eSDavid Howells * If index [0,1] is previously set, two count = 1 ioctls calls would be 570607ca46eSDavid Howells * required to set [0,0] and [0,2] without changing [0,1]. 571607ca46eSDavid Howells * 572607ca46eSDavid Howells * Once a signaling mechanism is set, DATA_BOOL or DATA_NONE can be used 573607ca46eSDavid Howells * with ACTION_TRIGGER to perform kernel level interrupt loopback testing 574607ca46eSDavid Howells * from userspace (ie. simulate hardware triggering). 575607ca46eSDavid Howells * 576607ca46eSDavid Howells * Setting of an event triggering mechanism to userspace for ACTION_TRIGGER 577607ca46eSDavid Howells * enables the interrupt index for the device. Individual subindex interrupts 578607ca46eSDavid Howells * can be disabled using the -1 value for DATA_EVENTFD or the index can be 579607ca46eSDavid Howells * disabled as a whole with: flags = (DATA_NONE|ACTION_TRIGGER), count = 0. 580607ca46eSDavid Howells * 581607ca46eSDavid Howells * Note that ACTION_[UN]MASK specify user->kernel signaling (irqfds) while 582607ca46eSDavid Howells * ACTION_TRIGGER specifies kernel->user signaling. 583607ca46eSDavid Howells */ 584607ca46eSDavid Howells struct vfio_irq_set { 585607ca46eSDavid Howells __u32 argsz; 586607ca46eSDavid Howells __u32 flags; 587607ca46eSDavid Howells #define VFIO_IRQ_SET_DATA_NONE (1 << 0) /* Data not present */ 588607ca46eSDavid Howells #define VFIO_IRQ_SET_DATA_BOOL (1 << 1) /* Data is bool (u8) */ 589607ca46eSDavid Howells #define VFIO_IRQ_SET_DATA_EVENTFD (1 << 2) /* Data is eventfd (s32) */ 590607ca46eSDavid Howells #define VFIO_IRQ_SET_ACTION_MASK (1 << 3) /* Mask interrupt */ 591607ca46eSDavid Howells #define VFIO_IRQ_SET_ACTION_UNMASK (1 << 4) /* Unmask interrupt */ 592607ca46eSDavid Howells #define VFIO_IRQ_SET_ACTION_TRIGGER (1 << 5) /* Trigger interrupt */ 593607ca46eSDavid Howells __u32 index; 594607ca46eSDavid Howells __u32 start; 595607ca46eSDavid Howells __u32 count; 596607ca46eSDavid Howells __u8 data[]; 597607ca46eSDavid Howells }; 598607ca46eSDavid Howells #define VFIO_DEVICE_SET_IRQS _IO(VFIO_TYPE, VFIO_BASE + 10) 599607ca46eSDavid Howells 600607ca46eSDavid Howells #define VFIO_IRQ_SET_DATA_TYPE_MASK (VFIO_IRQ_SET_DATA_NONE | \ 601607ca46eSDavid Howells VFIO_IRQ_SET_DATA_BOOL | \ 602607ca46eSDavid Howells VFIO_IRQ_SET_DATA_EVENTFD) 603607ca46eSDavid Howells #define VFIO_IRQ_SET_ACTION_TYPE_MASK (VFIO_IRQ_SET_ACTION_MASK | \ 604607ca46eSDavid Howells VFIO_IRQ_SET_ACTION_UNMASK | \ 605607ca46eSDavid Howells VFIO_IRQ_SET_ACTION_TRIGGER) 606607ca46eSDavid Howells /** 607607ca46eSDavid Howells * VFIO_DEVICE_RESET - _IO(VFIO_TYPE, VFIO_BASE + 11) 608607ca46eSDavid Howells * 609607ca46eSDavid Howells * Reset a device. 610607ca46eSDavid Howells */ 611607ca46eSDavid Howells #define VFIO_DEVICE_RESET _IO(VFIO_TYPE, VFIO_BASE + 11) 612607ca46eSDavid Howells 613607ca46eSDavid Howells /* 614607ca46eSDavid Howells * The VFIO-PCI bus driver makes use of the following fixed region and 615607ca46eSDavid Howells * IRQ index mapping. Unimplemented regions return a size of zero. 616607ca46eSDavid Howells * Unimplemented IRQ types return a count of zero. 617607ca46eSDavid Howells */ 618607ca46eSDavid Howells 619607ca46eSDavid Howells enum { 620607ca46eSDavid Howells VFIO_PCI_BAR0_REGION_INDEX, 621607ca46eSDavid Howells VFIO_PCI_BAR1_REGION_INDEX, 622607ca46eSDavid Howells VFIO_PCI_BAR2_REGION_INDEX, 623607ca46eSDavid Howells VFIO_PCI_BAR3_REGION_INDEX, 624607ca46eSDavid Howells VFIO_PCI_BAR4_REGION_INDEX, 625607ca46eSDavid Howells VFIO_PCI_BAR5_REGION_INDEX, 626607ca46eSDavid Howells VFIO_PCI_ROM_REGION_INDEX, 627607ca46eSDavid Howells VFIO_PCI_CONFIG_REGION_INDEX, 62884237a82SAlex Williamson /* 62984237a82SAlex Williamson * Expose VGA regions defined for PCI base class 03, subclass 00. 63084237a82SAlex Williamson * This includes I/O port ranges 0x3b0 to 0x3bb and 0x3c0 to 0x3df 63184237a82SAlex Williamson * as well as the MMIO range 0xa0000 to 0xbffff. Each implemented 63284237a82SAlex Williamson * range is found at it's identity mapped offset from the region 63384237a82SAlex Williamson * offset, for example 0x3b0 is region_info.offset + 0x3b0. Areas 63484237a82SAlex Williamson * between described ranges are unimplemented. 63584237a82SAlex Williamson */ 63684237a82SAlex Williamson VFIO_PCI_VGA_REGION_INDEX, 637c7bb4cb4SAlex Williamson VFIO_PCI_NUM_REGIONS = 9 /* Fixed user ABI, region indexes >=9 use */ 638c7bb4cb4SAlex Williamson /* device specific cap to define content. */ 639607ca46eSDavid Howells }; 640607ca46eSDavid Howells 641607ca46eSDavid Howells enum { 642607ca46eSDavid Howells VFIO_PCI_INTX_IRQ_INDEX, 643607ca46eSDavid Howells VFIO_PCI_MSI_IRQ_INDEX, 644607ca46eSDavid Howells VFIO_PCI_MSIX_IRQ_INDEX, 645dad9f897SVijay Mohan Pandarathil VFIO_PCI_ERR_IRQ_INDEX, 6466140a8f5SAlex Williamson VFIO_PCI_REQ_IRQ_INDEX, 647607ca46eSDavid Howells VFIO_PCI_NUM_IRQS 648607ca46eSDavid Howells }; 649607ca46eSDavid Howells 650e01bcdd6SDong Jia Shi /* 651120e214eSDong Jia Shi * The vfio-ccw bus driver makes use of the following fixed region and 652120e214eSDong Jia Shi * IRQ index mapping. Unimplemented regions return a size of zero. 653120e214eSDong Jia Shi * Unimplemented IRQ types return a count of zero. 654e01bcdd6SDong Jia Shi */ 655e01bcdd6SDong Jia Shi 656e01bcdd6SDong Jia Shi enum { 657e01bcdd6SDong Jia Shi VFIO_CCW_CONFIG_REGION_INDEX, 658e01bcdd6SDong Jia Shi VFIO_CCW_NUM_REGIONS 659e01bcdd6SDong Jia Shi }; 660e01bcdd6SDong Jia Shi 661120e214eSDong Jia Shi enum { 662120e214eSDong Jia Shi VFIO_CCW_IO_IRQ_INDEX, 663d8cac29bSFarhan Ali VFIO_CCW_CRW_IRQ_INDEX, 664bccce80bSEric Farman VFIO_CCW_REQ_IRQ_INDEX, 665120e214eSDong Jia Shi VFIO_CCW_NUM_IRQS 666120e214eSDong Jia Shi }; 667120e214eSDong Jia Shi 6686afc7700STony Krowiak /* 6696afc7700STony Krowiak * The vfio-ap bus driver makes use of the following IRQ index mapping. 6706afc7700STony Krowiak * Unimplemented IRQ types return a count of zero. 6716afc7700STony Krowiak */ 6726afc7700STony Krowiak enum { 6736afc7700STony Krowiak VFIO_AP_REQ_IRQ_INDEX, 6746afc7700STony Krowiak VFIO_AP_NUM_IRQS 6756afc7700STony Krowiak }; 6766afc7700STony Krowiak 6778b27ee60SAlex Williamson /** 6781c05bb94SThomas Huth * VFIO_DEVICE_GET_PCI_HOT_RESET_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 12, 6798b27ee60SAlex Williamson * struct vfio_pci_hot_reset_info) 6808b27ee60SAlex Williamson * 6819062ff40SYi Liu * This command is used to query the affected devices in the hot reset for 6829062ff40SYi Liu * a given device. 6839062ff40SYi Liu * 6849062ff40SYi Liu * This command always reports the segment, bus, and devfn information for 6859062ff40SYi Liu * each affected device, and selectively reports the group_id or devid per 6869062ff40SYi Liu * the way how the calling device is opened. 6879062ff40SYi Liu * 6889062ff40SYi Liu * - If the calling device is opened via the traditional group/container 6899062ff40SYi Liu * API, group_id is reported. User should check if it has owned all 6909062ff40SYi Liu * the affected devices and provides a set of group fds to prove the 6919062ff40SYi Liu * ownership in VFIO_DEVICE_PCI_HOT_RESET ioctl. 6929062ff40SYi Liu * 6939062ff40SYi Liu * - If the calling device is opened as a cdev, devid is reported. 6949062ff40SYi Liu * Flag VFIO_PCI_HOT_RESET_FLAG_DEV_ID is set to indicate this 6959062ff40SYi Liu * data type. All the affected devices should be represented in 6969062ff40SYi Liu * the dev_set, ex. bound to a vfio driver, and also be owned by 6979062ff40SYi Liu * this interface which is determined by the following conditions: 6989062ff40SYi Liu * 1) Has a valid devid within the iommufd_ctx of the calling device. 6999062ff40SYi Liu * Ownership cannot be determined across separate iommufd_ctx and 7009062ff40SYi Liu * the cdev calling conventions do not support a proof-of-ownership 7019062ff40SYi Liu * model as provided in the legacy group interface. In this case 7029062ff40SYi Liu * valid devid with value greater than zero is provided in the return 7039062ff40SYi Liu * structure. 7049062ff40SYi Liu * 2) Does not have a valid devid within the iommufd_ctx of the calling 7059062ff40SYi Liu * device, but belongs to the same IOMMU group as the calling device 7069062ff40SYi Liu * or another opened device that has a valid devid within the 7079062ff40SYi Liu * iommufd_ctx of the calling device. This provides implicit ownership 7089062ff40SYi Liu * for devices within the same DMA isolation context. In this case 7099062ff40SYi Liu * the devid value of VFIO_PCI_DEVID_OWNED is provided in the return 7109062ff40SYi Liu * structure. 7119062ff40SYi Liu * 7129062ff40SYi Liu * A devid value of VFIO_PCI_DEVID_NOT_OWNED is provided in the return 7139062ff40SYi Liu * structure for affected devices where device is NOT represented in the 7149062ff40SYi Liu * dev_set or ownership is not available. Such devices prevent the use 7159062ff40SYi Liu * of VFIO_DEVICE_PCI_HOT_RESET ioctl outside of the proof-of-ownership 7169062ff40SYi Liu * calling conventions (ie. via legacy group accessed devices). Flag 7179062ff40SYi Liu * VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED would be set when all the 7189062ff40SYi Liu * affected devices are represented in the dev_set and also owned by 7199062ff40SYi Liu * the user. This flag is available only when 7209062ff40SYi Liu * flag VFIO_PCI_HOT_RESET_FLAG_DEV_ID is set, otherwise reserved. 72171791b92SYi Liu * When set, user could invoke VFIO_DEVICE_PCI_HOT_RESET with a zero 72271791b92SYi Liu * length fd array on the calling device as the ownership is validated 72371791b92SYi Liu * by iommufd_ctx. 7249062ff40SYi Liu * 7258b27ee60SAlex Williamson * Return: 0 on success, -errno on failure: 7268b27ee60SAlex Williamson * -enospc = insufficient buffer, -enodev = unsupported for device. 7278b27ee60SAlex Williamson */ 7288b27ee60SAlex Williamson struct vfio_pci_dependent_device { 7299062ff40SYi Liu union { 7308b27ee60SAlex Williamson __u32 group_id; 7319062ff40SYi Liu __u32 devid; 7329062ff40SYi Liu #define VFIO_PCI_DEVID_OWNED 0 7339062ff40SYi Liu #define VFIO_PCI_DEVID_NOT_OWNED -1 7349062ff40SYi Liu }; 7358b27ee60SAlex Williamson __u16 segment; 7368b27ee60SAlex Williamson __u8 bus; 7378b27ee60SAlex Williamson __u8 devfn; /* Use PCI_SLOT/PCI_FUNC */ 7388b27ee60SAlex Williamson }; 7398b27ee60SAlex Williamson 7408b27ee60SAlex Williamson struct vfio_pci_hot_reset_info { 7418b27ee60SAlex Williamson __u32 argsz; 7428b27ee60SAlex Williamson __u32 flags; 7439062ff40SYi Liu #define VFIO_PCI_HOT_RESET_FLAG_DEV_ID (1 << 0) 7449062ff40SYi Liu #define VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED (1 << 1) 7458b27ee60SAlex Williamson __u32 count; 7468b27ee60SAlex Williamson struct vfio_pci_dependent_device devices[]; 7478b27ee60SAlex Williamson }; 7488b27ee60SAlex Williamson 7498b27ee60SAlex Williamson #define VFIO_DEVICE_GET_PCI_HOT_RESET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12) 7508b27ee60SAlex Williamson 7518b27ee60SAlex Williamson /** 7528b27ee60SAlex Williamson * VFIO_DEVICE_PCI_HOT_RESET - _IOW(VFIO_TYPE, VFIO_BASE + 13, 7538b27ee60SAlex Williamson * struct vfio_pci_hot_reset) 7548b27ee60SAlex Williamson * 75571791b92SYi Liu * A PCI hot reset results in either a bus or slot reset which may affect 75671791b92SYi Liu * other devices sharing the bus/slot. The calling user must have 75771791b92SYi Liu * ownership of the full set of affected devices as determined by the 75871791b92SYi Liu * VFIO_DEVICE_GET_PCI_HOT_RESET_INFO ioctl. 75971791b92SYi Liu * 76071791b92SYi Liu * When called on a device file descriptor acquired through the vfio 76171791b92SYi Liu * group interface, the user is required to provide proof of ownership 76271791b92SYi Liu * of those affected devices via the group_fds array in struct 76371791b92SYi Liu * vfio_pci_hot_reset. 76471791b92SYi Liu * 76571791b92SYi Liu * When called on a direct cdev opened vfio device, the flags field of 76671791b92SYi Liu * struct vfio_pci_hot_reset_info reports the ownership status of the 76771791b92SYi Liu * affected devices and this ioctl must be called with an empty group_fds 76871791b92SYi Liu * array. See above INFO ioctl definition for ownership requirements. 76971791b92SYi Liu * 77071791b92SYi Liu * Mixed usage of legacy groups and cdevs across the set of affected 77171791b92SYi Liu * devices is not supported. 77271791b92SYi Liu * 7738b27ee60SAlex Williamson * Return: 0 on success, -errno on failure. 7748b27ee60SAlex Williamson */ 7758b27ee60SAlex Williamson struct vfio_pci_hot_reset { 7768b27ee60SAlex Williamson __u32 argsz; 7778b27ee60SAlex Williamson __u32 flags; 7788b27ee60SAlex Williamson __u32 count; 7798b27ee60SAlex Williamson __s32 group_fds[]; 7808b27ee60SAlex Williamson }; 7818b27ee60SAlex Williamson 7828b27ee60SAlex Williamson #define VFIO_DEVICE_PCI_HOT_RESET _IO(VFIO_TYPE, VFIO_BASE + 13) 7838b27ee60SAlex Williamson 784e20eaa23STina Zhang /** 785e20eaa23STina Zhang * VFIO_DEVICE_QUERY_GFX_PLANE - _IOW(VFIO_TYPE, VFIO_BASE + 14, 786e20eaa23STina Zhang * struct vfio_device_query_gfx_plane) 787e20eaa23STina Zhang * 788e20eaa23STina Zhang * Set the drm_plane_type and flags, then retrieve the gfx plane info. 789e20eaa23STina Zhang * 790e20eaa23STina Zhang * flags supported: 791e20eaa23STina Zhang * - VFIO_GFX_PLANE_TYPE_PROBE and VFIO_GFX_PLANE_TYPE_DMABUF are set 792e20eaa23STina Zhang * to ask if the mdev supports dma-buf. 0 on support, -EINVAL on no 793e20eaa23STina Zhang * support for dma-buf. 794e20eaa23STina Zhang * - VFIO_GFX_PLANE_TYPE_PROBE and VFIO_GFX_PLANE_TYPE_REGION are set 795e20eaa23STina Zhang * to ask if the mdev supports region. 0 on support, -EINVAL on no 796e20eaa23STina Zhang * support for region. 797e20eaa23STina Zhang * - VFIO_GFX_PLANE_TYPE_DMABUF or VFIO_GFX_PLANE_TYPE_REGION is set 798e20eaa23STina Zhang * with each call to query the plane info. 799e20eaa23STina Zhang * - Others are invalid and return -EINVAL. 800e20eaa23STina Zhang * 801e20eaa23STina Zhang * Note: 802e20eaa23STina Zhang * 1. Plane could be disabled by guest. In that case, success will be 803e20eaa23STina Zhang * returned with zero-initialized drm_format, size, width and height 804e20eaa23STina Zhang * fields. 805e20eaa23STina Zhang * 2. x_hot/y_hot is set to 0xFFFFFFFF if no hotspot information available 806e20eaa23STina Zhang * 807e20eaa23STina Zhang * Return: 0 on success, -errno on other failure. 808e20eaa23STina Zhang */ 809e20eaa23STina Zhang struct vfio_device_gfx_plane_info { 810e20eaa23STina Zhang __u32 argsz; 811e20eaa23STina Zhang __u32 flags; 812e20eaa23STina Zhang #define VFIO_GFX_PLANE_TYPE_PROBE (1 << 0) 813e20eaa23STina Zhang #define VFIO_GFX_PLANE_TYPE_DMABUF (1 << 1) 814e20eaa23STina Zhang #define VFIO_GFX_PLANE_TYPE_REGION (1 << 2) 815e20eaa23STina Zhang /* in */ 816e20eaa23STina Zhang __u32 drm_plane_type; /* type of plane: DRM_PLANE_TYPE_* */ 817e20eaa23STina Zhang /* out */ 818e20eaa23STina Zhang __u32 drm_format; /* drm format of plane */ 819a7bea9f4SStefan Hajnoczi __aligned_u64 drm_format_mod; /* tiled mode */ 820e20eaa23STina Zhang __u32 width; /* width of plane */ 821e20eaa23STina Zhang __u32 height; /* height of plane */ 822e20eaa23STina Zhang __u32 stride; /* stride of plane */ 823e20eaa23STina Zhang __u32 size; /* size of plane in bytes, align on page*/ 824e20eaa23STina Zhang __u32 x_pos; /* horizontal position of cursor plane */ 825e20eaa23STina Zhang __u32 y_pos; /* vertical position of cursor plane*/ 826e20eaa23STina Zhang __u32 x_hot; /* horizontal position of cursor hotspot */ 827e20eaa23STina Zhang __u32 y_hot; /* vertical position of cursor hotspot */ 828e20eaa23STina Zhang union { 829e20eaa23STina Zhang __u32 region_index; /* region index */ 830e20eaa23STina Zhang __u32 dmabuf_id; /* dma-buf id */ 831e20eaa23STina Zhang }; 832a7bea9f4SStefan Hajnoczi __u32 reserved; 833e20eaa23STina Zhang }; 834e20eaa23STina Zhang 835e20eaa23STina Zhang #define VFIO_DEVICE_QUERY_GFX_PLANE _IO(VFIO_TYPE, VFIO_BASE + 14) 836e20eaa23STina Zhang 837e20eaa23STina Zhang /** 838e20eaa23STina Zhang * VFIO_DEVICE_GET_GFX_DMABUF - _IOW(VFIO_TYPE, VFIO_BASE + 15, __u32) 839e20eaa23STina Zhang * 840e20eaa23STina Zhang * Return a new dma-buf file descriptor for an exposed guest framebuffer 841e20eaa23STina Zhang * described by the provided dmabuf_id. The dmabuf_id is returned from VFIO_ 842e20eaa23STina Zhang * DEVICE_QUERY_GFX_PLANE as a token of the exposed guest framebuffer. 843e20eaa23STina Zhang */ 844e20eaa23STina Zhang 845e20eaa23STina Zhang #define VFIO_DEVICE_GET_GFX_DMABUF _IO(VFIO_TYPE, VFIO_BASE + 15) 846e20eaa23STina Zhang 84730656177SAlex Williamson /** 84830656177SAlex Williamson * VFIO_DEVICE_IOEVENTFD - _IOW(VFIO_TYPE, VFIO_BASE + 16, 84930656177SAlex Williamson * struct vfio_device_ioeventfd) 85030656177SAlex Williamson * 85130656177SAlex Williamson * Perform a write to the device at the specified device fd offset, with 85230656177SAlex Williamson * the specified data and width when the provided eventfd is triggered. 85330656177SAlex Williamson * vfio bus drivers may not support this for all regions, for all widths, 85430656177SAlex Williamson * or at all. vfio-pci currently only enables support for BAR regions, 85530656177SAlex Williamson * excluding the MSI-X vector table. 85630656177SAlex Williamson * 85730656177SAlex Williamson * Return: 0 on success, -errno on failure. 85830656177SAlex Williamson */ 85930656177SAlex Williamson struct vfio_device_ioeventfd { 86030656177SAlex Williamson __u32 argsz; 86130656177SAlex Williamson __u32 flags; 86230656177SAlex Williamson #define VFIO_DEVICE_IOEVENTFD_8 (1 << 0) /* 1-byte write */ 86330656177SAlex Williamson #define VFIO_DEVICE_IOEVENTFD_16 (1 << 1) /* 2-byte write */ 86430656177SAlex Williamson #define VFIO_DEVICE_IOEVENTFD_32 (1 << 2) /* 4-byte write */ 86530656177SAlex Williamson #define VFIO_DEVICE_IOEVENTFD_64 (1 << 3) /* 8-byte write */ 86630656177SAlex Williamson #define VFIO_DEVICE_IOEVENTFD_SIZE_MASK (0xf) 86761050c73SStefan Hajnoczi __aligned_u64 offset; /* device fd offset of write */ 86861050c73SStefan Hajnoczi __aligned_u64 data; /* data to be written */ 86930656177SAlex Williamson __s32 fd; /* -1 for de-assignment */ 87061050c73SStefan Hajnoczi __u32 reserved; 87130656177SAlex Williamson }; 87230656177SAlex Williamson 87330656177SAlex Williamson #define VFIO_DEVICE_IOEVENTFD _IO(VFIO_TYPE, VFIO_BASE + 16) 87430656177SAlex Williamson 87543eeeeccSAlex Williamson /** 8761c05bb94SThomas Huth * VFIO_DEVICE_FEATURE - _IOWR(VFIO_TYPE, VFIO_BASE + 17, 87743eeeeccSAlex Williamson * struct vfio_device_feature) 87843eeeeccSAlex Williamson * 87943eeeeccSAlex Williamson * Get, set, or probe feature data of the device. The feature is selected 88043eeeeccSAlex Williamson * using the FEATURE_MASK portion of the flags field. Support for a feature 88143eeeeccSAlex Williamson * can be probed by setting both the FEATURE_MASK and PROBE bits. A probe 88243eeeeccSAlex Williamson * may optionally include the GET and/or SET bits to determine read vs write 88343eeeeccSAlex Williamson * access of the feature respectively. Probing a feature will return success 88443eeeeccSAlex Williamson * if the feature is supported and all of the optionally indicated GET/SET 88543eeeeccSAlex Williamson * methods are supported. The format of the data portion of the structure is 88643eeeeccSAlex Williamson * specific to the given feature. The data portion is not required for 88743eeeeccSAlex Williamson * probing. GET and SET are mutually exclusive, except for use with PROBE. 88843eeeeccSAlex Williamson * 88943eeeeccSAlex Williamson * Return 0 on success, -errno on failure. 89043eeeeccSAlex Williamson */ 89143eeeeccSAlex Williamson struct vfio_device_feature { 89243eeeeccSAlex Williamson __u32 argsz; 89343eeeeccSAlex Williamson __u32 flags; 89443eeeeccSAlex Williamson #define VFIO_DEVICE_FEATURE_MASK (0xffff) /* 16-bit feature index */ 89543eeeeccSAlex Williamson #define VFIO_DEVICE_FEATURE_GET (1 << 16) /* Get feature into data[] */ 89643eeeeccSAlex Williamson #define VFIO_DEVICE_FEATURE_SET (1 << 17) /* Set feature from data[] */ 89743eeeeccSAlex Williamson #define VFIO_DEVICE_FEATURE_PROBE (1 << 18) /* Probe feature support */ 89843eeeeccSAlex Williamson __u8 data[]; 89943eeeeccSAlex Williamson }; 90043eeeeccSAlex Williamson 90143eeeeccSAlex Williamson #define VFIO_DEVICE_FEATURE _IO(VFIO_TYPE, VFIO_BASE + 17) 90243eeeeccSAlex Williamson 90343eeeeccSAlex Williamson /* 9045fcc2696SYi Liu * VFIO_DEVICE_BIND_IOMMUFD - _IOR(VFIO_TYPE, VFIO_BASE + 18, 9055fcc2696SYi Liu * struct vfio_device_bind_iommufd) 9065fcc2696SYi Liu * @argsz: User filled size of this data. 9075fcc2696SYi Liu * @flags: Must be 0. 9085fcc2696SYi Liu * @iommufd: iommufd to bind. 9095fcc2696SYi Liu * @out_devid: The device id generated by this bind. devid is a handle for 9105fcc2696SYi Liu * this device/iommufd bond and can be used in IOMMUFD commands. 9115fcc2696SYi Liu * 9125fcc2696SYi Liu * Bind a vfio_device to the specified iommufd. 9135fcc2696SYi Liu * 9145fcc2696SYi Liu * User is restricted from accessing the device before the binding operation 9155fcc2696SYi Liu * is completed. Only allowed on cdev fds. 9165fcc2696SYi Liu * 9175fcc2696SYi Liu * Unbind is automatically conducted when device fd is closed. 9185fcc2696SYi Liu * 9195fcc2696SYi Liu * Return: 0 on success, -errno on failure. 9205fcc2696SYi Liu */ 9215fcc2696SYi Liu struct vfio_device_bind_iommufd { 9225fcc2696SYi Liu __u32 argsz; 9235fcc2696SYi Liu __u32 flags; 9245fcc2696SYi Liu __s32 iommufd; 9255fcc2696SYi Liu __u32 out_devid; 9265fcc2696SYi Liu }; 9275fcc2696SYi Liu 9285fcc2696SYi Liu #define VFIO_DEVICE_BIND_IOMMUFD _IO(VFIO_TYPE, VFIO_BASE + 18) 9295fcc2696SYi Liu 9305fcc2696SYi Liu /* 931b290a05fSYi Liu * VFIO_DEVICE_ATTACH_IOMMUFD_PT - _IOW(VFIO_TYPE, VFIO_BASE + 19, 932b290a05fSYi Liu * struct vfio_device_attach_iommufd_pt) 933b290a05fSYi Liu * @argsz: User filled size of this data. 934b290a05fSYi Liu * @flags: Must be 0. 935b290a05fSYi Liu * @pt_id: Input the target id which can represent an ioas or a hwpt 936b290a05fSYi Liu * allocated via iommufd subsystem. 937b290a05fSYi Liu * Output the input ioas id or the attached hwpt id which could 938b290a05fSYi Liu * be the specified hwpt itself or a hwpt automatically created 939b290a05fSYi Liu * for the specified ioas by kernel during the attachment. 940b290a05fSYi Liu * 941b290a05fSYi Liu * Associate the device with an address space within the bound iommufd. 942b290a05fSYi Liu * Undo by VFIO_DEVICE_DETACH_IOMMUFD_PT or device fd close. This is only 943b290a05fSYi Liu * allowed on cdev fds. 944b290a05fSYi Liu * 945c157fd88SNicolin Chen * If a vfio device is currently attached to a valid hw_pagetable, without doing 946c157fd88SNicolin Chen * a VFIO_DEVICE_DETACH_IOMMUFD_PT, a second VFIO_DEVICE_ATTACH_IOMMUFD_PT ioctl 947c157fd88SNicolin Chen * passing in another hw_pagetable (hwpt) id is allowed. This action, also known 948c157fd88SNicolin Chen * as a hw_pagetable replacement, will replace the device's currently attached 949c157fd88SNicolin Chen * hw_pagetable with a new hw_pagetable corresponding to the given pt_id. 950c157fd88SNicolin Chen * 951b290a05fSYi Liu * Return: 0 on success, -errno on failure. 952b290a05fSYi Liu */ 953b290a05fSYi Liu struct vfio_device_attach_iommufd_pt { 954b290a05fSYi Liu __u32 argsz; 955b290a05fSYi Liu __u32 flags; 956b290a05fSYi Liu __u32 pt_id; 957b290a05fSYi Liu }; 958b290a05fSYi Liu 959b290a05fSYi Liu #define VFIO_DEVICE_ATTACH_IOMMUFD_PT _IO(VFIO_TYPE, VFIO_BASE + 19) 960b290a05fSYi Liu 961b290a05fSYi Liu /* 962b290a05fSYi Liu * VFIO_DEVICE_DETACH_IOMMUFD_PT - _IOW(VFIO_TYPE, VFIO_BASE + 20, 963b290a05fSYi Liu * struct vfio_device_detach_iommufd_pt) 964b290a05fSYi Liu * @argsz: User filled size of this data. 965b290a05fSYi Liu * @flags: Must be 0. 966b290a05fSYi Liu * 967b290a05fSYi Liu * Remove the association of the device and its current associated address 968b290a05fSYi Liu * space. After it, the device should be in a blocking DMA state. This is only 969b290a05fSYi Liu * allowed on cdev fds. 970b290a05fSYi Liu * 971b290a05fSYi Liu * Return: 0 on success, -errno on failure. 972b290a05fSYi Liu */ 973b290a05fSYi Liu struct vfio_device_detach_iommufd_pt { 974b290a05fSYi Liu __u32 argsz; 975b290a05fSYi Liu __u32 flags; 976b290a05fSYi Liu }; 977b290a05fSYi Liu 978b290a05fSYi Liu #define VFIO_DEVICE_DETACH_IOMMUFD_PT _IO(VFIO_TYPE, VFIO_BASE + 20) 979b290a05fSYi Liu 980b290a05fSYi Liu /* 98143eeeeccSAlex Williamson * Provide support for setting a PCI VF Token, which is used as a shared 98243eeeeccSAlex Williamson * secret between PF and VF drivers. This feature may only be set on a 98343eeeeccSAlex Williamson * PCI SR-IOV PF when SR-IOV is enabled on the PF and there are no existing 98443eeeeccSAlex Williamson * open VFs. Data provided when setting this feature is a 16-byte array 98543eeeeccSAlex Williamson * (__u8 b[16]), representing a UUID. 98643eeeeccSAlex Williamson */ 98743eeeeccSAlex Williamson #define VFIO_DEVICE_FEATURE_PCI_VF_TOKEN (0) 98843eeeeccSAlex Williamson 989115dcec6SJason Gunthorpe /* 990115dcec6SJason Gunthorpe * Indicates the device can support the migration API through 991115dcec6SJason Gunthorpe * VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE. If this GET succeeds, the RUNNING and 992115dcec6SJason Gunthorpe * ERROR states are always supported. Support for additional states is 993115dcec6SJason Gunthorpe * indicated via the flags field; at least VFIO_MIGRATION_STOP_COPY must be 994115dcec6SJason Gunthorpe * set. 995115dcec6SJason Gunthorpe * 996115dcec6SJason Gunthorpe * VFIO_MIGRATION_STOP_COPY means that STOP, STOP_COPY and 997115dcec6SJason Gunthorpe * RESUMING are supported. 9988cb3d83bSJason Gunthorpe * 9998cb3d83bSJason Gunthorpe * VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P means that RUNNING_P2P 10008cb3d83bSJason Gunthorpe * is supported in addition to the STOP_COPY states. 10018cb3d83bSJason Gunthorpe * 10024db52602SJason Gunthorpe * VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_PRE_COPY means that 10034db52602SJason Gunthorpe * PRE_COPY is supported in addition to the STOP_COPY states. 10044db52602SJason Gunthorpe * 10054db52602SJason Gunthorpe * VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P | VFIO_MIGRATION_PRE_COPY 10064db52602SJason Gunthorpe * means that RUNNING_P2P, PRE_COPY and PRE_COPY_P2P are supported 10074db52602SJason Gunthorpe * in addition to the STOP_COPY states. 10084db52602SJason Gunthorpe * 10098cb3d83bSJason Gunthorpe * Other combinations of flags have behavior to be defined in the future. 1010115dcec6SJason Gunthorpe */ 1011115dcec6SJason Gunthorpe struct vfio_device_feature_migration { 1012115dcec6SJason Gunthorpe __aligned_u64 flags; 1013115dcec6SJason Gunthorpe #define VFIO_MIGRATION_STOP_COPY (1 << 0) 10148cb3d83bSJason Gunthorpe #define VFIO_MIGRATION_P2P (1 << 1) 10154db52602SJason Gunthorpe #define VFIO_MIGRATION_PRE_COPY (1 << 2) 1016115dcec6SJason Gunthorpe }; 1017115dcec6SJason Gunthorpe #define VFIO_DEVICE_FEATURE_MIGRATION 1 1018115dcec6SJason Gunthorpe 1019115dcec6SJason Gunthorpe /* 1020115dcec6SJason Gunthorpe * Upon VFIO_DEVICE_FEATURE_SET, execute a migration state change on the VFIO 1021115dcec6SJason Gunthorpe * device. The new state is supplied in device_state, see enum 1022115dcec6SJason Gunthorpe * vfio_device_mig_state for details 1023115dcec6SJason Gunthorpe * 1024115dcec6SJason Gunthorpe * The kernel migration driver must fully transition the device to the new state 1025115dcec6SJason Gunthorpe * value before the operation returns to the user. 1026115dcec6SJason Gunthorpe * 1027115dcec6SJason Gunthorpe * The kernel migration driver must not generate asynchronous device state 1028115dcec6SJason Gunthorpe * transitions outside of manipulation by the user or the VFIO_DEVICE_RESET 1029115dcec6SJason Gunthorpe * ioctl as described above. 1030115dcec6SJason Gunthorpe * 1031115dcec6SJason Gunthorpe * If this function fails then current device_state may be the original 1032115dcec6SJason Gunthorpe * operating state or some other state along the combination transition path. 1033115dcec6SJason Gunthorpe * The user can then decide if it should execute a VFIO_DEVICE_RESET, attempt 1034115dcec6SJason Gunthorpe * to return to the original state, or attempt to return to some other state 1035115dcec6SJason Gunthorpe * such as RUNNING or STOP. 1036115dcec6SJason Gunthorpe * 1037115dcec6SJason Gunthorpe * If the new_state starts a new data transfer session then the FD associated 1038115dcec6SJason Gunthorpe * with that session is returned in data_fd. The user is responsible to close 1039115dcec6SJason Gunthorpe * this FD when it is finished. The user must consider the migration data stream 1040115dcec6SJason Gunthorpe * carried over the FD to be opaque and must preserve the byte order of the 1041115dcec6SJason Gunthorpe * stream. The user is not required to preserve buffer segmentation when writing 1042115dcec6SJason Gunthorpe * the data stream during the RESUMING operation. 1043115dcec6SJason Gunthorpe * 1044115dcec6SJason Gunthorpe * Upon VFIO_DEVICE_FEATURE_GET, get the current migration state of the VFIO 1045115dcec6SJason Gunthorpe * device, data_fd will be -1. 1046115dcec6SJason Gunthorpe */ 1047115dcec6SJason Gunthorpe struct vfio_device_feature_mig_state { 1048115dcec6SJason Gunthorpe __u32 device_state; /* From enum vfio_device_mig_state */ 1049115dcec6SJason Gunthorpe __s32 data_fd; 1050115dcec6SJason Gunthorpe }; 1051115dcec6SJason Gunthorpe #define VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE 2 1052115dcec6SJason Gunthorpe 1053115dcec6SJason Gunthorpe /* 1054115dcec6SJason Gunthorpe * The device migration Finite State Machine is described by the enum 1055115dcec6SJason Gunthorpe * vfio_device_mig_state. Some of the FSM arcs will create a migration data 1056115dcec6SJason Gunthorpe * transfer session by returning a FD, in this case the migration data will 1057115dcec6SJason Gunthorpe * flow over the FD using read() and write() as discussed below. 1058115dcec6SJason Gunthorpe * 1059115dcec6SJason Gunthorpe * There are 5 states to support VFIO_MIGRATION_STOP_COPY: 1060115dcec6SJason Gunthorpe * RUNNING - The device is running normally 1061115dcec6SJason Gunthorpe * STOP - The device does not change the internal or external state 1062115dcec6SJason Gunthorpe * STOP_COPY - The device internal state can be read out 1063115dcec6SJason Gunthorpe * RESUMING - The device is stopped and is loading a new internal state 1064115dcec6SJason Gunthorpe * ERROR - The device has failed and must be reset 1065115dcec6SJason Gunthorpe * 10664db52602SJason Gunthorpe * And optional states to support VFIO_MIGRATION_P2P: 10678cb3d83bSJason Gunthorpe * RUNNING_P2P - RUNNING, except the device cannot do peer to peer DMA 10684db52602SJason Gunthorpe * And VFIO_MIGRATION_PRE_COPY: 10694db52602SJason Gunthorpe * PRE_COPY - The device is running normally but tracking internal state 10704db52602SJason Gunthorpe * changes 10714db52602SJason Gunthorpe * And VFIO_MIGRATION_P2P | VFIO_MIGRATION_PRE_COPY: 10724db52602SJason Gunthorpe * PRE_COPY_P2P - PRE_COPY, except the device cannot do peer to peer DMA 10738cb3d83bSJason Gunthorpe * 1074115dcec6SJason Gunthorpe * The FSM takes actions on the arcs between FSM states. The driver implements 1075115dcec6SJason Gunthorpe * the following behavior for the FSM arcs: 1076115dcec6SJason Gunthorpe * 10778cb3d83bSJason Gunthorpe * RUNNING_P2P -> STOP 1078115dcec6SJason Gunthorpe * STOP_COPY -> STOP 1079115dcec6SJason Gunthorpe * While in STOP the device must stop the operation of the device. The device 1080115dcec6SJason Gunthorpe * must not generate interrupts, DMA, or any other change to external state. 1081115dcec6SJason Gunthorpe * It must not change its internal state. When stopped the device and kernel 1082115dcec6SJason Gunthorpe * migration driver must accept and respond to interaction to support external 1083115dcec6SJason Gunthorpe * subsystems in the STOP state, for example PCI MSI-X and PCI config space. 1084115dcec6SJason Gunthorpe * Failure by the user to restrict device access while in STOP must not result 1085115dcec6SJason Gunthorpe * in error conditions outside the user context (ex. host system faults). 1086115dcec6SJason Gunthorpe * 1087115dcec6SJason Gunthorpe * The STOP_COPY arc will terminate a data transfer session. 1088115dcec6SJason Gunthorpe * 1089115dcec6SJason Gunthorpe * RESUMING -> STOP 1090115dcec6SJason Gunthorpe * Leaving RESUMING terminates a data transfer session and indicates the 1091115dcec6SJason Gunthorpe * device should complete processing of the data delivered by write(). The 1092115dcec6SJason Gunthorpe * kernel migration driver should complete the incorporation of data written 1093115dcec6SJason Gunthorpe * to the data transfer FD into the device internal state and perform 1094115dcec6SJason Gunthorpe * final validity and consistency checking of the new device state. If the 1095115dcec6SJason Gunthorpe * user provided data is found to be incomplete, inconsistent, or otherwise 1096115dcec6SJason Gunthorpe * invalid, the migration driver must fail the SET_STATE ioctl and 1097115dcec6SJason Gunthorpe * optionally go to the ERROR state as described below. 1098115dcec6SJason Gunthorpe * 1099115dcec6SJason Gunthorpe * While in STOP the device has the same behavior as other STOP states 1100115dcec6SJason Gunthorpe * described above. 1101115dcec6SJason Gunthorpe * 1102115dcec6SJason Gunthorpe * To abort a RESUMING session the device must be reset. 1103115dcec6SJason Gunthorpe * 11044db52602SJason Gunthorpe * PRE_COPY -> RUNNING 11058cb3d83bSJason Gunthorpe * RUNNING_P2P -> RUNNING 1106115dcec6SJason Gunthorpe * While in RUNNING the device is fully operational, the device may generate 1107115dcec6SJason Gunthorpe * interrupts, DMA, respond to MMIO, all vfio device regions are functional, 1108115dcec6SJason Gunthorpe * and the device may advance its internal state. 1109115dcec6SJason Gunthorpe * 11104db52602SJason Gunthorpe * The PRE_COPY arc will terminate a data transfer session. 11114db52602SJason Gunthorpe * 11124db52602SJason Gunthorpe * PRE_COPY_P2P -> RUNNING_P2P 11138cb3d83bSJason Gunthorpe * RUNNING -> RUNNING_P2P 11148cb3d83bSJason Gunthorpe * STOP -> RUNNING_P2P 11158cb3d83bSJason Gunthorpe * While in RUNNING_P2P the device is partially running in the P2P quiescent 11168cb3d83bSJason Gunthorpe * state defined below. 11178cb3d83bSJason Gunthorpe * 11184db52602SJason Gunthorpe * The PRE_COPY_P2P arc will terminate a data transfer session. 1119115dcec6SJason Gunthorpe * 11204db52602SJason Gunthorpe * RUNNING -> PRE_COPY 11214db52602SJason Gunthorpe * RUNNING_P2P -> PRE_COPY_P2P 11224db52602SJason Gunthorpe * STOP -> STOP_COPY 11234db52602SJason Gunthorpe * PRE_COPY, PRE_COPY_P2P and STOP_COPY form the "saving group" of states 11244db52602SJason Gunthorpe * which share a data transfer session. Moving between these states alters 11254db52602SJason Gunthorpe * what is streamed in session, but does not terminate or otherwise affect 11264db52602SJason Gunthorpe * the associated fd. 11274db52602SJason Gunthorpe * 11284db52602SJason Gunthorpe * These arcs begin the process of saving the device state and will return a 11294db52602SJason Gunthorpe * new data_fd. The migration driver may perform actions such as enabling 11304db52602SJason Gunthorpe * dirty logging of device state when entering PRE_COPY or PER_COPY_P2P. 11314db52602SJason Gunthorpe * 11324db52602SJason Gunthorpe * Each arc does not change the device operation, the device remains 11334db52602SJason Gunthorpe * RUNNING, P2P quiesced or in STOP. The STOP_COPY state is described below 11344db52602SJason Gunthorpe * in PRE_COPY_P2P -> STOP_COPY. 11354db52602SJason Gunthorpe * 11364db52602SJason Gunthorpe * PRE_COPY -> PRE_COPY_P2P 11374db52602SJason Gunthorpe * Entering PRE_COPY_P2P continues all the behaviors of PRE_COPY above. 11384db52602SJason Gunthorpe * However, while in the PRE_COPY_P2P state, the device is partially running 11394db52602SJason Gunthorpe * in the P2P quiescent state defined below, like RUNNING_P2P. 11404db52602SJason Gunthorpe * 11414db52602SJason Gunthorpe * PRE_COPY_P2P -> PRE_COPY 11424db52602SJason Gunthorpe * This arc allows returning the device to a full RUNNING behavior while 11434db52602SJason Gunthorpe * continuing all the behaviors of PRE_COPY. 11444db52602SJason Gunthorpe * 11454db52602SJason Gunthorpe * PRE_COPY_P2P -> STOP_COPY 1146115dcec6SJason Gunthorpe * While in the STOP_COPY state the device has the same behavior as STOP 1147115dcec6SJason Gunthorpe * with the addition that the data transfers session continues to stream the 1148115dcec6SJason Gunthorpe * migration state. End of stream on the FD indicates the entire device 1149115dcec6SJason Gunthorpe * state has been transferred. 1150115dcec6SJason Gunthorpe * 1151115dcec6SJason Gunthorpe * The user should take steps to restrict access to vfio device regions while 1152115dcec6SJason Gunthorpe * the device is in STOP_COPY or risk corruption of the device migration data 1153115dcec6SJason Gunthorpe * stream. 1154115dcec6SJason Gunthorpe * 1155115dcec6SJason Gunthorpe * STOP -> RESUMING 1156115dcec6SJason Gunthorpe * Entering the RESUMING state starts a process of restoring the device state 1157115dcec6SJason Gunthorpe * and will return a new data_fd. The data stream fed into the data_fd should 1158115dcec6SJason Gunthorpe * be taken from the data transfer output of a single FD during saving from 1159115dcec6SJason Gunthorpe * a compatible device. The migration driver may alter/reset the internal 1160115dcec6SJason Gunthorpe * device state for this arc if required to prepare the device to receive the 1161115dcec6SJason Gunthorpe * migration data. 1162115dcec6SJason Gunthorpe * 11634db52602SJason Gunthorpe * STOP_COPY -> PRE_COPY 11644db52602SJason Gunthorpe * STOP_COPY -> PRE_COPY_P2P 11654db52602SJason Gunthorpe * These arcs are not permitted and return error if requested. Future 11664db52602SJason Gunthorpe * revisions of this API may define behaviors for these arcs, in this case 11674db52602SJason Gunthorpe * support will be discoverable by a new flag in 11684db52602SJason Gunthorpe * VFIO_DEVICE_FEATURE_MIGRATION. 11694db52602SJason Gunthorpe * 1170115dcec6SJason Gunthorpe * any -> ERROR 1171115dcec6SJason Gunthorpe * ERROR cannot be specified as a device state, however any transition request 1172115dcec6SJason Gunthorpe * can be failed with an errno return and may then move the device_state into 1173115dcec6SJason Gunthorpe * ERROR. In this case the device was unable to execute the requested arc and 1174115dcec6SJason Gunthorpe * was also unable to restore the device to any valid device_state. 1175115dcec6SJason Gunthorpe * To recover from ERROR VFIO_DEVICE_RESET must be used to return the 1176115dcec6SJason Gunthorpe * device_state back to RUNNING. 1177115dcec6SJason Gunthorpe * 11788cb3d83bSJason Gunthorpe * The optional peer to peer (P2P) quiescent state is intended to be a quiescent 11798cb3d83bSJason Gunthorpe * state for the device for the purposes of managing multiple devices within a 11808cb3d83bSJason Gunthorpe * user context where peer-to-peer DMA between devices may be active. The 11814db52602SJason Gunthorpe * RUNNING_P2P and PRE_COPY_P2P states must prevent the device from initiating 11828cb3d83bSJason Gunthorpe * any new P2P DMA transactions. If the device can identify P2P transactions 11838cb3d83bSJason Gunthorpe * then it can stop only P2P DMA, otherwise it must stop all DMA. The migration 11848cb3d83bSJason Gunthorpe * driver must complete any such outstanding operations prior to completing the 11858cb3d83bSJason Gunthorpe * FSM arc into a P2P state. For the purpose of specification the states 11868cb3d83bSJason Gunthorpe * behave as though the device was fully running if not supported. Like while in 11878cb3d83bSJason Gunthorpe * STOP or STOP_COPY the user must not touch the device, otherwise the state 11888cb3d83bSJason Gunthorpe * can be exited. 11898cb3d83bSJason Gunthorpe * 1190115dcec6SJason Gunthorpe * The remaining possible transitions are interpreted as combinations of the 1191115dcec6SJason Gunthorpe * above FSM arcs. As there are multiple paths through the FSM arcs the path 1192115dcec6SJason Gunthorpe * should be selected based on the following rules: 1193115dcec6SJason Gunthorpe * - Select the shortest path. 11944db52602SJason Gunthorpe * - The path cannot have saving group states as interior arcs, only 11954db52602SJason Gunthorpe * starting/end states. 1196115dcec6SJason Gunthorpe * Refer to vfio_mig_get_next_state() for the result of the algorithm. 1197115dcec6SJason Gunthorpe * 1198115dcec6SJason Gunthorpe * The automatic transit through the FSM arcs that make up the combination 1199115dcec6SJason Gunthorpe * transition is invisible to the user. When working with combination arcs the 1200115dcec6SJason Gunthorpe * user may see any step along the path in the device_state if SET_STATE 1201115dcec6SJason Gunthorpe * fails. When handling these types of errors users should anticipate future 1202115dcec6SJason Gunthorpe * revisions of this protocol using new states and those states becoming 1203115dcec6SJason Gunthorpe * visible in this case. 12048cb3d83bSJason Gunthorpe * 12058cb3d83bSJason Gunthorpe * The optional states cannot be used with SET_STATE if the device does not 12068cb3d83bSJason Gunthorpe * support them. The user can discover if these states are supported by using 12078cb3d83bSJason Gunthorpe * VFIO_DEVICE_FEATURE_MIGRATION. By using combination transitions the user can 12088cb3d83bSJason Gunthorpe * avoid knowing about these optional states if the kernel driver supports them. 12094db52602SJason Gunthorpe * 12104db52602SJason Gunthorpe * Arcs touching PRE_COPY and PRE_COPY_P2P are removed if support for PRE_COPY 12114db52602SJason Gunthorpe * is not present. 1212115dcec6SJason Gunthorpe */ 1213115dcec6SJason Gunthorpe enum vfio_device_mig_state { 1214115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_ERROR = 0, 1215115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_STOP = 1, 1216115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_RUNNING = 2, 1217115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_STOP_COPY = 3, 1218115dcec6SJason Gunthorpe VFIO_DEVICE_STATE_RESUMING = 4, 12198cb3d83bSJason Gunthorpe VFIO_DEVICE_STATE_RUNNING_P2P = 5, 12204db52602SJason Gunthorpe VFIO_DEVICE_STATE_PRE_COPY = 6, 12214db52602SJason Gunthorpe VFIO_DEVICE_STATE_PRE_COPY_P2P = 7, 1222*2202844eSLongfang Liu VFIO_DEVICE_STATE_NR, 1223115dcec6SJason Gunthorpe }; 1224115dcec6SJason Gunthorpe 12254db52602SJason Gunthorpe /** 12264db52602SJason Gunthorpe * VFIO_MIG_GET_PRECOPY_INFO - _IO(VFIO_TYPE, VFIO_BASE + 21) 12274db52602SJason Gunthorpe * 12284db52602SJason Gunthorpe * This ioctl is used on the migration data FD in the precopy phase of the 12294db52602SJason Gunthorpe * migration data transfer. It returns an estimate of the current data sizes 12304db52602SJason Gunthorpe * remaining to be transferred. It allows the user to judge when it is 12314db52602SJason Gunthorpe * appropriate to leave PRE_COPY for STOP_COPY. 12324db52602SJason Gunthorpe * 12334db52602SJason Gunthorpe * This ioctl is valid only in PRE_COPY states and kernel driver should 12344db52602SJason Gunthorpe * return -EINVAL from any other migration state. 12354db52602SJason Gunthorpe * 12364db52602SJason Gunthorpe * The vfio_precopy_info data structure returned by this ioctl provides 12374db52602SJason Gunthorpe * estimates of data available from the device during the PRE_COPY states. 12384db52602SJason Gunthorpe * This estimate is split into two categories, initial_bytes and 12394db52602SJason Gunthorpe * dirty_bytes. 12404db52602SJason Gunthorpe * 12414db52602SJason Gunthorpe * The initial_bytes field indicates the amount of initial precopy 12424db52602SJason Gunthorpe * data available from the device. This field should have a non-zero initial 12434db52602SJason Gunthorpe * value and decrease as migration data is read from the device. 12444db52602SJason Gunthorpe * It is recommended to leave PRE_COPY for STOP_COPY only after this field 12454db52602SJason Gunthorpe * reaches zero. Leaving PRE_COPY earlier might make things slower. 12464db52602SJason Gunthorpe * 12474db52602SJason Gunthorpe * The dirty_bytes field tracks device state changes relative to data 12484db52602SJason Gunthorpe * previously retrieved. This field starts at zero and may increase as 12494db52602SJason Gunthorpe * the internal device state is modified or decrease as that modified 12504db52602SJason Gunthorpe * state is read from the device. 12514db52602SJason Gunthorpe * 12524db52602SJason Gunthorpe * Userspace may use the combination of these fields to estimate the 12534db52602SJason Gunthorpe * potential data size available during the PRE_COPY phases, as well as 12544db52602SJason Gunthorpe * trends relative to the rate the device is dirtying its internal 12554db52602SJason Gunthorpe * state, but these fields are not required to have any bearing relative 12564db52602SJason Gunthorpe * to the data size available during the STOP_COPY phase. 12574db52602SJason Gunthorpe * 12584db52602SJason Gunthorpe * Drivers have a lot of flexibility in when and what they transfer during the 12594db52602SJason Gunthorpe * PRE_COPY phase, and how they report this from VFIO_MIG_GET_PRECOPY_INFO. 12604db52602SJason Gunthorpe * 12614db52602SJason Gunthorpe * During pre-copy the migration data FD has a temporary "end of stream" that is 12624db52602SJason Gunthorpe * reached when both initial_bytes and dirty_byte are zero. For instance, this 12634db52602SJason Gunthorpe * may indicate that the device is idle and not currently dirtying any internal 12644db52602SJason Gunthorpe * state. When read() is done on this temporary end of stream the kernel driver 12654db52602SJason Gunthorpe * should return ENOMSG from read(). Userspace can wait for more data (which may 12664db52602SJason Gunthorpe * never come) by using poll. 12674db52602SJason Gunthorpe * 12684db52602SJason Gunthorpe * Once in STOP_COPY the migration data FD has a permanent end of stream 12694db52602SJason Gunthorpe * signaled in the usual way by read() always returning 0 and poll always 12704db52602SJason Gunthorpe * returning readable. ENOMSG may not be returned in STOP_COPY. 12714db52602SJason Gunthorpe * Support for this ioctl is mandatory if a driver claims to support 12724db52602SJason Gunthorpe * VFIO_MIGRATION_PRE_COPY. 12734db52602SJason Gunthorpe * 12744db52602SJason Gunthorpe * Return: 0 on success, -1 and errno set on failure. 12754db52602SJason Gunthorpe */ 12764db52602SJason Gunthorpe struct vfio_precopy_info { 12774db52602SJason Gunthorpe __u32 argsz; 12784db52602SJason Gunthorpe __u32 flags; 12794db52602SJason Gunthorpe __aligned_u64 initial_bytes; 12804db52602SJason Gunthorpe __aligned_u64 dirty_bytes; 12814db52602SJason Gunthorpe }; 12824db52602SJason Gunthorpe 12834db52602SJason Gunthorpe #define VFIO_MIG_GET_PRECOPY_INFO _IO(VFIO_TYPE, VFIO_BASE + 21) 12844db52602SJason Gunthorpe 1285385ecfdfSAbhishek Sahu /* 1286385ecfdfSAbhishek Sahu * Upon VFIO_DEVICE_FEATURE_SET, allow the device to be moved into a low power 1287385ecfdfSAbhishek Sahu * state with the platform-based power management. Device use of lower power 1288385ecfdfSAbhishek Sahu * states depends on factors managed by the runtime power management core, 1289385ecfdfSAbhishek Sahu * including system level support and coordinating support among dependent 1290385ecfdfSAbhishek Sahu * devices. Enabling device low power entry does not guarantee lower power 1291385ecfdfSAbhishek Sahu * usage by the device, nor is a mechanism provided through this feature to 1292385ecfdfSAbhishek Sahu * know the current power state of the device. If any device access happens 1293385ecfdfSAbhishek Sahu * (either from the host or through the vfio uAPI) when the device is in the 1294385ecfdfSAbhishek Sahu * low power state, then the host will move the device out of the low power 1295385ecfdfSAbhishek Sahu * state as necessary prior to the access. Once the access is completed, the 1296385ecfdfSAbhishek Sahu * device may re-enter the low power state. For single shot low power support 1297385ecfdfSAbhishek Sahu * with wake-up notification, see 1298385ecfdfSAbhishek Sahu * VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP below. Access to mmap'd 1299385ecfdfSAbhishek Sahu * device regions is disabled on LOW_POWER_ENTRY and may only be resumed after 1300385ecfdfSAbhishek Sahu * calling LOW_POWER_EXIT. 1301385ecfdfSAbhishek Sahu */ 1302385ecfdfSAbhishek Sahu #define VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY 3 1303385ecfdfSAbhishek Sahu 1304385ecfdfSAbhishek Sahu /* 1305385ecfdfSAbhishek Sahu * This device feature has the same behavior as 1306385ecfdfSAbhishek Sahu * VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY with the exception that the user 1307385ecfdfSAbhishek Sahu * provides an eventfd for wake-up notification. When the device moves out of 1308385ecfdfSAbhishek Sahu * the low power state for the wake-up, the host will not allow the device to 1309385ecfdfSAbhishek Sahu * re-enter a low power state without a subsequent user call to one of the low 1310385ecfdfSAbhishek Sahu * power entry device feature IOCTLs. Access to mmap'd device regions is 1311385ecfdfSAbhishek Sahu * disabled on LOW_POWER_ENTRY_WITH_WAKEUP and may only be resumed after the 1312385ecfdfSAbhishek Sahu * low power exit. The low power exit can happen either through LOW_POWER_EXIT 1313385ecfdfSAbhishek Sahu * or through any other access (where the wake-up notification has been 1314385ecfdfSAbhishek Sahu * generated). The access to mmap'd device regions will not trigger low power 1315385ecfdfSAbhishek Sahu * exit. 1316385ecfdfSAbhishek Sahu * 1317385ecfdfSAbhishek Sahu * The notification through the provided eventfd will be generated only when 1318385ecfdfSAbhishek Sahu * the device has entered and is resumed from a low power state after 1319385ecfdfSAbhishek Sahu * calling this device feature IOCTL. A device that has not entered low power 1320385ecfdfSAbhishek Sahu * state, as managed through the runtime power management core, will not 1321385ecfdfSAbhishek Sahu * generate a notification through the provided eventfd on access. Calling the 1322385ecfdfSAbhishek Sahu * LOW_POWER_EXIT feature is optional in the case where notification has been 1323385ecfdfSAbhishek Sahu * signaled on the provided eventfd that a resume from low power has occurred. 1324385ecfdfSAbhishek Sahu */ 1325385ecfdfSAbhishek Sahu struct vfio_device_low_power_entry_with_wakeup { 1326385ecfdfSAbhishek Sahu __s32 wakeup_eventfd; 1327385ecfdfSAbhishek Sahu __u32 reserved; 1328385ecfdfSAbhishek Sahu }; 1329385ecfdfSAbhishek Sahu 1330385ecfdfSAbhishek Sahu #define VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP 4 1331385ecfdfSAbhishek Sahu 1332385ecfdfSAbhishek Sahu /* 1333385ecfdfSAbhishek Sahu * Upon VFIO_DEVICE_FEATURE_SET, disallow use of device low power states as 1334385ecfdfSAbhishek Sahu * previously enabled via VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY or 1335385ecfdfSAbhishek Sahu * VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP device features. 1336385ecfdfSAbhishek Sahu * This device feature IOCTL may itself generate a wakeup eventfd notification 1337385ecfdfSAbhishek Sahu * in the latter case if the device had previously entered a low power state. 1338385ecfdfSAbhishek Sahu */ 1339385ecfdfSAbhishek Sahu #define VFIO_DEVICE_FEATURE_LOW_POWER_EXIT 5 1340385ecfdfSAbhishek Sahu 134142ee53f9SYishai Hadas /* 134242ee53f9SYishai Hadas * Upon VFIO_DEVICE_FEATURE_SET start/stop device DMA logging. 134342ee53f9SYishai Hadas * VFIO_DEVICE_FEATURE_PROBE can be used to detect if the device supports 134442ee53f9SYishai Hadas * DMA logging. 134542ee53f9SYishai Hadas * 134642ee53f9SYishai Hadas * DMA logging allows a device to internally record what DMAs the device is 134742ee53f9SYishai Hadas * initiating and report them back to userspace. It is part of the VFIO 134842ee53f9SYishai Hadas * migration infrastructure that allows implementing dirty page tracking 134942ee53f9SYishai Hadas * during the pre copy phase of live migration. Only DMA WRITEs are logged, 135042ee53f9SYishai Hadas * and this API is not connected to VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE. 135142ee53f9SYishai Hadas * 135242ee53f9SYishai Hadas * When DMA logging is started a range of IOVAs to monitor is provided and the 135342ee53f9SYishai Hadas * device can optimize its logging to cover only the IOVA range given. Each 135442ee53f9SYishai Hadas * DMA that the device initiates inside the range will be logged by the device 135542ee53f9SYishai Hadas * for later retrieval. 135642ee53f9SYishai Hadas * 135742ee53f9SYishai Hadas * page_size is an input that hints what tracking granularity the device 135842ee53f9SYishai Hadas * should try to achieve. If the device cannot do the hinted page size then 135942ee53f9SYishai Hadas * it's the driver choice which page size to pick based on its support. 136042ee53f9SYishai Hadas * On output the device will return the page size it selected. 136142ee53f9SYishai Hadas * 136242ee53f9SYishai Hadas * ranges is a pointer to an array of 136342ee53f9SYishai Hadas * struct vfio_device_feature_dma_logging_range. 136442ee53f9SYishai Hadas * 136542ee53f9SYishai Hadas * The core kernel code guarantees to support by minimum num_ranges that fit 136642ee53f9SYishai Hadas * into a single kernel page. User space can try higher values but should give 136742ee53f9SYishai Hadas * up if the above can't be achieved as of some driver limitations. 136842ee53f9SYishai Hadas * 136942ee53f9SYishai Hadas * A single call to start device DMA logging can be issued and a matching stop 137042ee53f9SYishai Hadas * should follow at the end. Another start is not allowed in the meantime. 137142ee53f9SYishai Hadas */ 137242ee53f9SYishai Hadas struct vfio_device_feature_dma_logging_control { 137342ee53f9SYishai Hadas __aligned_u64 page_size; 137442ee53f9SYishai Hadas __u32 num_ranges; 137542ee53f9SYishai Hadas __u32 __reserved; 137642ee53f9SYishai Hadas __aligned_u64 ranges; 137742ee53f9SYishai Hadas }; 137842ee53f9SYishai Hadas 137942ee53f9SYishai Hadas struct vfio_device_feature_dma_logging_range { 138042ee53f9SYishai Hadas __aligned_u64 iova; 138142ee53f9SYishai Hadas __aligned_u64 length; 138242ee53f9SYishai Hadas }; 138342ee53f9SYishai Hadas 138442ee53f9SYishai Hadas #define VFIO_DEVICE_FEATURE_DMA_LOGGING_START 6 138542ee53f9SYishai Hadas 138642ee53f9SYishai Hadas /* 138742ee53f9SYishai Hadas * Upon VFIO_DEVICE_FEATURE_SET stop device DMA logging that was started 138842ee53f9SYishai Hadas * by VFIO_DEVICE_FEATURE_DMA_LOGGING_START 138942ee53f9SYishai Hadas */ 139042ee53f9SYishai Hadas #define VFIO_DEVICE_FEATURE_DMA_LOGGING_STOP 7 139142ee53f9SYishai Hadas 139242ee53f9SYishai Hadas /* 139342ee53f9SYishai Hadas * Upon VFIO_DEVICE_FEATURE_GET read back and clear the device DMA log 139442ee53f9SYishai Hadas * 139542ee53f9SYishai Hadas * Query the device's DMA log for written pages within the given IOVA range. 139642ee53f9SYishai Hadas * During querying the log is cleared for the IOVA range. 139742ee53f9SYishai Hadas * 139842ee53f9SYishai Hadas * bitmap is a pointer to an array of u64s that will hold the output bitmap 139942ee53f9SYishai Hadas * with 1 bit reporting a page_size unit of IOVA. The mapping of IOVA to bits 140042ee53f9SYishai Hadas * is given by: 140142ee53f9SYishai Hadas * bitmap[(addr - iova)/page_size] & (1ULL << (addr % 64)) 140242ee53f9SYishai Hadas * 140342ee53f9SYishai Hadas * The input page_size can be any power of two value and does not have to 140442ee53f9SYishai Hadas * match the value given to VFIO_DEVICE_FEATURE_DMA_LOGGING_START. The driver 140542ee53f9SYishai Hadas * will format its internal logging to match the reporting page size, possibly 140642ee53f9SYishai Hadas * by replicating bits if the internal page size is lower than requested. 140742ee53f9SYishai Hadas * 140842ee53f9SYishai Hadas * The LOGGING_REPORT will only set bits in the bitmap and never clear or 140942ee53f9SYishai Hadas * perform any initialization of the user provided bitmap. 141042ee53f9SYishai Hadas * 141142ee53f9SYishai Hadas * If any error is returned userspace should assume that the dirty log is 141242ee53f9SYishai Hadas * corrupted. Error recovery is to consider all memory dirty and try to 141342ee53f9SYishai Hadas * restart the dirty tracking, or to abort/restart the whole migration. 141442ee53f9SYishai Hadas * 141542ee53f9SYishai Hadas * If DMA logging is not enabled, an error will be returned. 141642ee53f9SYishai Hadas * 141742ee53f9SYishai Hadas */ 141842ee53f9SYishai Hadas struct vfio_device_feature_dma_logging_report { 141942ee53f9SYishai Hadas __aligned_u64 iova; 142042ee53f9SYishai Hadas __aligned_u64 length; 142142ee53f9SYishai Hadas __aligned_u64 page_size; 142242ee53f9SYishai Hadas __aligned_u64 bitmap; 142342ee53f9SYishai Hadas }; 142442ee53f9SYishai Hadas 142542ee53f9SYishai Hadas #define VFIO_DEVICE_FEATURE_DMA_LOGGING_REPORT 8 142642ee53f9SYishai Hadas 14274e016f96SYishai Hadas /* 14284e016f96SYishai Hadas * Upon VFIO_DEVICE_FEATURE_GET read back the estimated data length that will 14294e016f96SYishai Hadas * be required to complete stop copy. 14304e016f96SYishai Hadas * 14314e016f96SYishai Hadas * Note: Can be called on each device state. 14324e016f96SYishai Hadas */ 14334e016f96SYishai Hadas 14344e016f96SYishai Hadas struct vfio_device_feature_mig_data_size { 14354e016f96SYishai Hadas __aligned_u64 stop_copy_length; 14364e016f96SYishai Hadas }; 14374e016f96SYishai Hadas 14384e016f96SYishai Hadas #define VFIO_DEVICE_FEATURE_MIG_DATA_SIZE 9 14394e016f96SYishai Hadas 1440f59a7b6aSNipun Gupta /** 1441f59a7b6aSNipun Gupta * Upon VFIO_DEVICE_FEATURE_SET, set or clear the BUS mastering for the device 1442f59a7b6aSNipun Gupta * based on the operation specified in op flag. 1443f59a7b6aSNipun Gupta * 1444f59a7b6aSNipun Gupta * The functionality is incorporated for devices that needs bus master control, 1445f59a7b6aSNipun Gupta * but the in-band device interface lacks the support. Consequently, it is not 1446f59a7b6aSNipun Gupta * applicable to PCI devices, as bus master control for PCI devices is managed 1447f59a7b6aSNipun Gupta * in-band through the configuration space. At present, this feature is supported 1448f59a7b6aSNipun Gupta * only for CDX devices. 1449f59a7b6aSNipun Gupta * When the device's BUS MASTER setting is configured as CLEAR, it will result in 1450f59a7b6aSNipun Gupta * blocking all incoming DMA requests from the device. On the other hand, configuring 1451f59a7b6aSNipun Gupta * the device's BUS MASTER setting as SET (enable) will grant the device the 1452f59a7b6aSNipun Gupta * capability to perform DMA to the host memory. 1453f59a7b6aSNipun Gupta */ 1454f59a7b6aSNipun Gupta struct vfio_device_feature_bus_master { 1455f59a7b6aSNipun Gupta __u32 op; 1456f59a7b6aSNipun Gupta #define VFIO_DEVICE_FEATURE_CLEAR_MASTER 0 /* Clear Bus Master */ 1457f59a7b6aSNipun Gupta #define VFIO_DEVICE_FEATURE_SET_MASTER 1 /* Set Bus Master */ 1458f59a7b6aSNipun Gupta }; 1459f59a7b6aSNipun Gupta #define VFIO_DEVICE_FEATURE_BUS_MASTER 10 1460f59a7b6aSNipun Gupta 1461607ca46eSDavid Howells /* -------- API for Type1 VFIO IOMMU -------- */ 1462607ca46eSDavid Howells 1463607ca46eSDavid Howells /** 1464607ca46eSDavid Howells * VFIO_IOMMU_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 12, struct vfio_iommu_info) 1465607ca46eSDavid Howells * 1466607ca46eSDavid Howells * Retrieve information about the IOMMU object. Fills in provided 1467607ca46eSDavid Howells * struct vfio_iommu_info. Caller sets argsz. 1468607ca46eSDavid Howells * 1469607ca46eSDavid Howells * XXX Should we do these by CHECK_EXTENSION too? 1470607ca46eSDavid Howells */ 1471607ca46eSDavid Howells struct vfio_iommu_type1_info { 1472607ca46eSDavid Howells __u32 argsz; 1473607ca46eSDavid Howells __u32 flags; 1474607ca46eSDavid Howells #define VFIO_IOMMU_INFO_PGSIZES (1 << 0) /* supported page sizes info */ 1475a7170720SShameer Kolothum #define VFIO_IOMMU_INFO_CAPS (1 << 1) /* Info supports caps */ 14762f8d25faSStefan Hajnoczi __aligned_u64 iova_pgsizes; /* Bitmap of supported page sizes */ 1477a7170720SShameer Kolothum __u32 cap_offset; /* Offset within info struct of first cap */ 1478a881b496SStefan Hajnoczi __u32 pad; 1479a7170720SShameer Kolothum }; 1480a7170720SShameer Kolothum 1481a7170720SShameer Kolothum /* 1482a7170720SShameer Kolothum * The IOVA capability allows to report the valid IOVA range(s) 1483a7170720SShameer Kolothum * excluding any non-relaxable reserved regions exposed by 1484a7170720SShameer Kolothum * devices attached to the container. Any DMA map attempt 1485a7170720SShameer Kolothum * outside the valid iova range will return error. 1486a7170720SShameer Kolothum * 1487a7170720SShameer Kolothum * The structures below define version 1 of this capability. 1488a7170720SShameer Kolothum */ 1489a7170720SShameer Kolothum #define VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE 1 1490a7170720SShameer Kolothum 1491a7170720SShameer Kolothum struct vfio_iova_range { 1492a7170720SShameer Kolothum __u64 start; 1493a7170720SShameer Kolothum __u64 end; 1494a7170720SShameer Kolothum }; 1495a7170720SShameer Kolothum 1496a7170720SShameer Kolothum struct vfio_iommu_type1_info_cap_iova_range { 1497a7170720SShameer Kolothum struct vfio_info_cap_header header; 1498a7170720SShameer Kolothum __u32 nr_iovas; 1499a7170720SShameer Kolothum __u32 reserved; 1500a7170720SShameer Kolothum struct vfio_iova_range iova_ranges[]; 1501607ca46eSDavid Howells }; 1502607ca46eSDavid Howells 1503ad721705SKirti Wankhede /* 1504ad721705SKirti Wankhede * The migration capability allows to report supported features for migration. 1505ad721705SKirti Wankhede * 1506ad721705SKirti Wankhede * The structures below define version 1 of this capability. 1507ad721705SKirti Wankhede * 1508ad721705SKirti Wankhede * The existence of this capability indicates that IOMMU kernel driver supports 1509ad721705SKirti Wankhede * dirty page logging. 1510ad721705SKirti Wankhede * 1511ad721705SKirti Wankhede * pgsize_bitmap: Kernel driver returns bitmap of supported page sizes for dirty 1512ad721705SKirti Wankhede * page logging. 1513ad721705SKirti Wankhede * max_dirty_bitmap_size: Kernel driver returns maximum supported dirty bitmap 1514ad721705SKirti Wankhede * size in bytes that can be used by user applications when getting the dirty 1515ad721705SKirti Wankhede * bitmap. 1516ad721705SKirti Wankhede */ 1517f751820bSAlex Williamson #define VFIO_IOMMU_TYPE1_INFO_CAP_MIGRATION 2 1518ad721705SKirti Wankhede 1519ad721705SKirti Wankhede struct vfio_iommu_type1_info_cap_migration { 1520ad721705SKirti Wankhede struct vfio_info_cap_header header; 1521ad721705SKirti Wankhede __u32 flags; 1522ad721705SKirti Wankhede __u64 pgsize_bitmap; 1523ad721705SKirti Wankhede __u64 max_dirty_bitmap_size; /* in bytes */ 1524ad721705SKirti Wankhede }; 1525ad721705SKirti Wankhede 15267d6e1329SMatthew Rosato /* 15277d6e1329SMatthew Rosato * The DMA available capability allows to report the current number of 15287d6e1329SMatthew Rosato * simultaneously outstanding DMA mappings that are allowed. 15297d6e1329SMatthew Rosato * 15307d6e1329SMatthew Rosato * The structure below defines version 1 of this capability. 15317d6e1329SMatthew Rosato * 15327d6e1329SMatthew Rosato * avail: specifies the current number of outstanding DMA mappings allowed. 15337d6e1329SMatthew Rosato */ 15347d6e1329SMatthew Rosato #define VFIO_IOMMU_TYPE1_INFO_DMA_AVAIL 3 15357d6e1329SMatthew Rosato 15367d6e1329SMatthew Rosato struct vfio_iommu_type1_info_dma_avail { 15377d6e1329SMatthew Rosato struct vfio_info_cap_header header; 15387d6e1329SMatthew Rosato __u32 avail; 15397d6e1329SMatthew Rosato }; 15407d6e1329SMatthew Rosato 1541607ca46eSDavid Howells #define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12) 1542607ca46eSDavid Howells 1543607ca46eSDavid Howells /** 1544607ca46eSDavid Howells * VFIO_IOMMU_MAP_DMA - _IOW(VFIO_TYPE, VFIO_BASE + 13, struct vfio_dma_map) 1545607ca46eSDavid Howells * 1546607ca46eSDavid Howells * Map process virtual addresses to IO virtual addresses using the 1547607ca46eSDavid Howells * provided struct vfio_dma_map. Caller sets argsz. READ &/ WRITE required. 1548441e8106SSteve Sistare * 1549ef3a3f6aSSteve Sistare * If flags & VFIO_DMA_MAP_FLAG_VADDR, update the base vaddr for iova. The vaddr 1550441e8106SSteve Sistare * must have previously been invalidated with VFIO_DMA_UNMAP_FLAG_VADDR. To 1551441e8106SSteve Sistare * maintain memory consistency within the user application, the updated vaddr 1552441e8106SSteve Sistare * must address the same memory object as originally mapped. Failure to do so 1553441e8106SSteve Sistare * will result in user memory corruption and/or device misbehavior. iova and 1554441e8106SSteve Sistare * size must match those in the original MAP_DMA call. Protection is not 1555441e8106SSteve Sistare * changed, and the READ & WRITE flags must be 0. 1556607ca46eSDavid Howells */ 1557607ca46eSDavid Howells struct vfio_iommu_type1_dma_map { 1558607ca46eSDavid Howells __u32 argsz; 1559607ca46eSDavid Howells __u32 flags; 1560607ca46eSDavid Howells #define VFIO_DMA_MAP_FLAG_READ (1 << 0) /* readable from device */ 1561607ca46eSDavid Howells #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1) /* writable from device */ 1562441e8106SSteve Sistare #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2) 1563607ca46eSDavid Howells __u64 vaddr; /* Process virtual address */ 1564607ca46eSDavid Howells __u64 iova; /* IO virtual address */ 1565607ca46eSDavid Howells __u64 size; /* Size of mapping (bytes) */ 1566607ca46eSDavid Howells }; 1567607ca46eSDavid Howells 1568607ca46eSDavid Howells #define VFIO_IOMMU_MAP_DMA _IO(VFIO_TYPE, VFIO_BASE + 13) 1569607ca46eSDavid Howells 1570b704fd14SKirti Wankhede struct vfio_bitmap { 1571b704fd14SKirti Wankhede __u64 pgsize; /* page size for bitmap in bytes */ 1572b704fd14SKirti Wankhede __u64 size; /* in bytes */ 1573b704fd14SKirti Wankhede __u64 __user *data; /* one bit per page */ 1574b704fd14SKirti Wankhede }; 1575b704fd14SKirti Wankhede 1576607ca46eSDavid Howells /** 1577166fd7d9SAlex Williamson * VFIO_IOMMU_UNMAP_DMA - _IOWR(VFIO_TYPE, VFIO_BASE + 14, 1578166fd7d9SAlex Williamson * struct vfio_dma_unmap) 1579607ca46eSDavid Howells * 1580607ca46eSDavid Howells * Unmap IO virtual addresses using the provided struct vfio_dma_unmap. 1581166fd7d9SAlex Williamson * Caller sets argsz. The actual unmapped size is returned in the size 1582166fd7d9SAlex Williamson * field. No guarantee is made to the user that arbitrary unmaps of iova 1583166fd7d9SAlex Williamson * or size different from those used in the original mapping call will 1584166fd7d9SAlex Williamson * succeed. 1585c98fe7c2SSteve Sistare * 1586331e33d2SKirti Wankhede * VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP should be set to get the dirty bitmap 1587331e33d2SKirti Wankhede * before unmapping IO virtual addresses. When this flag is set, the user must 1588331e33d2SKirti Wankhede * provide a struct vfio_bitmap in data[]. User must provide zero-allocated 1589331e33d2SKirti Wankhede * memory via vfio_bitmap.data and its size in the vfio_bitmap.size field. 1590331e33d2SKirti Wankhede * A bit in the bitmap represents one page, of user provided page size in 1591331e33d2SKirti Wankhede * vfio_bitmap.pgsize field, consecutively starting from iova offset. Bit set 1592331e33d2SKirti Wankhede * indicates that the page at that offset from iova is dirty. A Bitmap of the 1593331e33d2SKirti Wankhede * pages in the range of unmapped size is returned in the user-provided 1594331e33d2SKirti Wankhede * vfio_bitmap.data. 1595c98fe7c2SSteve Sistare * 1596c98fe7c2SSteve Sistare * If flags & VFIO_DMA_UNMAP_FLAG_ALL, unmap all addresses. iova and size 1597c98fe7c2SSteve Sistare * must be 0. This cannot be combined with the get-dirty-bitmap flag. 1598441e8106SSteve Sistare * 1599441e8106SSteve Sistare * If flags & VFIO_DMA_UNMAP_FLAG_VADDR, do not unmap, but invalidate host 1600ef3a3f6aSSteve Sistare * virtual addresses in the iova range. DMA to already-mapped pages continues. 1601ef3a3f6aSSteve Sistare * Groups may not be added to the container while any addresses are invalid. 1602ef3a3f6aSSteve Sistare * This cannot be combined with the get-dirty-bitmap flag. 1603607ca46eSDavid Howells */ 1604607ca46eSDavid Howells struct vfio_iommu_type1_dma_unmap { 1605607ca46eSDavid Howells __u32 argsz; 1606607ca46eSDavid Howells __u32 flags; 1607331e33d2SKirti Wankhede #define VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP (1 << 0) 1608c98fe7c2SSteve Sistare #define VFIO_DMA_UNMAP_FLAG_ALL (1 << 1) 1609441e8106SSteve Sistare #define VFIO_DMA_UNMAP_FLAG_VADDR (1 << 2) 1610607ca46eSDavid Howells __u64 iova; /* IO virtual address */ 1611607ca46eSDavid Howells __u64 size; /* Size of mapping (bytes) */ 1612331e33d2SKirti Wankhede __u8 data[]; 1613607ca46eSDavid Howells }; 1614607ca46eSDavid Howells 1615607ca46eSDavid Howells #define VFIO_IOMMU_UNMAP_DMA _IO(VFIO_TYPE, VFIO_BASE + 14) 1616607ca46eSDavid Howells 16175ffd229cSAlexey Kardashevskiy /* 16185ffd229cSAlexey Kardashevskiy * IOCTLs to enable/disable IOMMU container usage. 16195ffd229cSAlexey Kardashevskiy * No parameters are supported. 16205ffd229cSAlexey Kardashevskiy */ 16215ffd229cSAlexey Kardashevskiy #define VFIO_IOMMU_ENABLE _IO(VFIO_TYPE, VFIO_BASE + 15) 16225ffd229cSAlexey Kardashevskiy #define VFIO_IOMMU_DISABLE _IO(VFIO_TYPE, VFIO_BASE + 16) 16235ffd229cSAlexey Kardashevskiy 1624b704fd14SKirti Wankhede /** 1625b704fd14SKirti Wankhede * VFIO_IOMMU_DIRTY_PAGES - _IOWR(VFIO_TYPE, VFIO_BASE + 17, 1626b704fd14SKirti Wankhede * struct vfio_iommu_type1_dirty_bitmap) 1627b704fd14SKirti Wankhede * IOCTL is used for dirty pages logging. 1628b704fd14SKirti Wankhede * Caller should set flag depending on which operation to perform, details as 1629b704fd14SKirti Wankhede * below: 1630b704fd14SKirti Wankhede * 1631b704fd14SKirti Wankhede * Calling the IOCTL with VFIO_IOMMU_DIRTY_PAGES_FLAG_START flag set, instructs 1632b704fd14SKirti Wankhede * the IOMMU driver to log pages that are dirtied or potentially dirtied by 1633b704fd14SKirti Wankhede * the device; designed to be used when a migration is in progress. Dirty pages 1634b704fd14SKirti Wankhede * are logged until logging is disabled by user application by calling the IOCTL 1635b704fd14SKirti Wankhede * with VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP flag. 1636b704fd14SKirti Wankhede * 1637b704fd14SKirti Wankhede * Calling the IOCTL with VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP flag set, instructs 1638b704fd14SKirti Wankhede * the IOMMU driver to stop logging dirtied pages. 1639b704fd14SKirti Wankhede * 1640b704fd14SKirti Wankhede * Calling the IOCTL with VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP flag set 1641b704fd14SKirti Wankhede * returns the dirty pages bitmap for IOMMU container for a given IOVA range. 1642b704fd14SKirti Wankhede * The user must specify the IOVA range and the pgsize through the structure 1643b704fd14SKirti Wankhede * vfio_iommu_type1_dirty_bitmap_get in the data[] portion. This interface 1644b704fd14SKirti Wankhede * supports getting a bitmap of the smallest supported pgsize only and can be 1645b704fd14SKirti Wankhede * modified in future to get a bitmap of any specified supported pgsize. The 1646b704fd14SKirti Wankhede * user must provide a zeroed memory area for the bitmap memory and specify its 1647b704fd14SKirti Wankhede * size in bitmap.size. One bit is used to represent one page consecutively 1648b704fd14SKirti Wankhede * starting from iova offset. The user should provide page size in bitmap.pgsize 1649b704fd14SKirti Wankhede * field. A bit set in the bitmap indicates that the page at that offset from 1650b704fd14SKirti Wankhede * iova is dirty. The caller must set argsz to a value including the size of 1651b704fd14SKirti Wankhede * structure vfio_iommu_type1_dirty_bitmap_get, but excluding the size of the 1652b704fd14SKirti Wankhede * actual bitmap. If dirty pages logging is not enabled, an error will be 1653b704fd14SKirti Wankhede * returned. 1654b704fd14SKirti Wankhede * 1655b704fd14SKirti Wankhede * Only one of the flags _START, _STOP and _GET may be specified at a time. 1656b704fd14SKirti Wankhede * 1657b704fd14SKirti Wankhede */ 1658b704fd14SKirti Wankhede struct vfio_iommu_type1_dirty_bitmap { 1659b704fd14SKirti Wankhede __u32 argsz; 1660b704fd14SKirti Wankhede __u32 flags; 1661b704fd14SKirti Wankhede #define VFIO_IOMMU_DIRTY_PAGES_FLAG_START (1 << 0) 1662b704fd14SKirti Wankhede #define VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP (1 << 1) 1663b704fd14SKirti Wankhede #define VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP (1 << 2) 1664b704fd14SKirti Wankhede __u8 data[]; 1665b704fd14SKirti Wankhede }; 1666b704fd14SKirti Wankhede 1667b704fd14SKirti Wankhede struct vfio_iommu_type1_dirty_bitmap_get { 1668b704fd14SKirti Wankhede __u64 iova; /* IO virtual address */ 1669b704fd14SKirti Wankhede __u64 size; /* Size of iova range */ 1670b704fd14SKirti Wankhede struct vfio_bitmap bitmap; 1671b704fd14SKirti Wankhede }; 1672b704fd14SKirti Wankhede 1673b704fd14SKirti Wankhede #define VFIO_IOMMU_DIRTY_PAGES _IO(VFIO_TYPE, VFIO_BASE + 17) 1674b704fd14SKirti Wankhede 16755ffd229cSAlexey Kardashevskiy /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ 16765ffd229cSAlexey Kardashevskiy 16775ffd229cSAlexey Kardashevskiy /* 1678e633bc86SAlexey Kardashevskiy * The SPAPR TCE DDW info struct provides the information about 1679e633bc86SAlexey Kardashevskiy * the details of Dynamic DMA window capability. 1680e633bc86SAlexey Kardashevskiy * 1681e633bc86SAlexey Kardashevskiy * @pgsizes contains a page size bitmask, 4K/64K/16M are supported. 1682e633bc86SAlexey Kardashevskiy * @max_dynamic_windows_supported tells the maximum number of windows 1683e633bc86SAlexey Kardashevskiy * which the platform can create. 1684e633bc86SAlexey Kardashevskiy * @levels tells the maximum number of levels in multi-level IOMMU tables; 1685e633bc86SAlexey Kardashevskiy * this allows splitting a table into smaller chunks which reduces 1686e633bc86SAlexey Kardashevskiy * the amount of physically contiguous memory required for the table. 1687e633bc86SAlexey Kardashevskiy */ 1688e633bc86SAlexey Kardashevskiy struct vfio_iommu_spapr_tce_ddw_info { 1689e633bc86SAlexey Kardashevskiy __u64 pgsizes; /* Bitmap of supported page sizes */ 1690e633bc86SAlexey Kardashevskiy __u32 max_dynamic_windows_supported; 1691e633bc86SAlexey Kardashevskiy __u32 levels; 1692e633bc86SAlexey Kardashevskiy }; 1693e633bc86SAlexey Kardashevskiy 1694e633bc86SAlexey Kardashevskiy /* 16955ffd229cSAlexey Kardashevskiy * The SPAPR TCE info struct provides the information about the PCI bus 16965ffd229cSAlexey Kardashevskiy * address ranges available for DMA, these values are programmed into 16975ffd229cSAlexey Kardashevskiy * the hardware so the guest has to know that information. 16985ffd229cSAlexey Kardashevskiy * 16995ffd229cSAlexey Kardashevskiy * The DMA 32 bit window start is an absolute PCI bus address. 17005ffd229cSAlexey Kardashevskiy * The IOVA address passed via map/unmap ioctls are absolute PCI bus 17015ffd229cSAlexey Kardashevskiy * addresses too so the window works as a filter rather than an offset 17025ffd229cSAlexey Kardashevskiy * for IOVA addresses. 17035ffd229cSAlexey Kardashevskiy * 1704e633bc86SAlexey Kardashevskiy * Flags supported: 1705e633bc86SAlexey Kardashevskiy * - VFIO_IOMMU_SPAPR_INFO_DDW: informs the userspace that dynamic DMA windows 1706e633bc86SAlexey Kardashevskiy * (DDW) support is present. @ddw is only supported when DDW is present. 17075ffd229cSAlexey Kardashevskiy */ 17085ffd229cSAlexey Kardashevskiy struct vfio_iommu_spapr_tce_info { 17095ffd229cSAlexey Kardashevskiy __u32 argsz; 1710e633bc86SAlexey Kardashevskiy __u32 flags; 1711e633bc86SAlexey Kardashevskiy #define VFIO_IOMMU_SPAPR_INFO_DDW (1 << 0) /* DDW supported */ 17125ffd229cSAlexey Kardashevskiy __u32 dma32_window_start; /* 32 bit window start (bytes) */ 17135ffd229cSAlexey Kardashevskiy __u32 dma32_window_size; /* 32 bit window size (bytes) */ 1714e633bc86SAlexey Kardashevskiy struct vfio_iommu_spapr_tce_ddw_info ddw; 17155ffd229cSAlexey Kardashevskiy }; 17165ffd229cSAlexey Kardashevskiy 17175ffd229cSAlexey Kardashevskiy #define VFIO_IOMMU_SPAPR_TCE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12) 17185ffd229cSAlexey Kardashevskiy 17191b69be5eSGavin Shan /* 17201b69be5eSGavin Shan * EEH PE operation struct provides ways to: 17211b69be5eSGavin Shan * - enable/disable EEH functionality; 17221b69be5eSGavin Shan * - unfreeze IO/DMA for frozen PE; 17231b69be5eSGavin Shan * - read PE state; 17241b69be5eSGavin Shan * - reset PE; 172568cbbc3aSGavin Shan * - configure PE; 172668cbbc3aSGavin Shan * - inject EEH error. 17271b69be5eSGavin Shan */ 172868cbbc3aSGavin Shan struct vfio_eeh_pe_err { 172968cbbc3aSGavin Shan __u32 type; 173068cbbc3aSGavin Shan __u32 func; 173168cbbc3aSGavin Shan __u64 addr; 173268cbbc3aSGavin Shan __u64 mask; 173368cbbc3aSGavin Shan }; 173468cbbc3aSGavin Shan 17351b69be5eSGavin Shan struct vfio_eeh_pe_op { 17361b69be5eSGavin Shan __u32 argsz; 17371b69be5eSGavin Shan __u32 flags; 17381b69be5eSGavin Shan __u32 op; 173968cbbc3aSGavin Shan union { 174068cbbc3aSGavin Shan struct vfio_eeh_pe_err err; 174168cbbc3aSGavin Shan }; 17421b69be5eSGavin Shan }; 17431b69be5eSGavin Shan 17441b69be5eSGavin Shan #define VFIO_EEH_PE_DISABLE 0 /* Disable EEH functionality */ 17451b69be5eSGavin Shan #define VFIO_EEH_PE_ENABLE 1 /* Enable EEH functionality */ 17461b69be5eSGavin Shan #define VFIO_EEH_PE_UNFREEZE_IO 2 /* Enable IO for frozen PE */ 17471b69be5eSGavin Shan #define VFIO_EEH_PE_UNFREEZE_DMA 3 /* Enable DMA for frozen PE */ 17481b69be5eSGavin Shan #define VFIO_EEH_PE_GET_STATE 4 /* PE state retrieval */ 17491b69be5eSGavin Shan #define VFIO_EEH_PE_STATE_NORMAL 0 /* PE in functional state */ 17501b69be5eSGavin Shan #define VFIO_EEH_PE_STATE_RESET 1 /* PE reset in progress */ 17511b69be5eSGavin Shan #define VFIO_EEH_PE_STATE_STOPPED 2 /* Stopped DMA and IO */ 17521b69be5eSGavin Shan #define VFIO_EEH_PE_STATE_STOPPED_DMA 4 /* Stopped DMA only */ 17531b69be5eSGavin Shan #define VFIO_EEH_PE_STATE_UNAVAIL 5 /* State unavailable */ 17541b69be5eSGavin Shan #define VFIO_EEH_PE_RESET_DEACTIVATE 5 /* Deassert PE reset */ 17551b69be5eSGavin Shan #define VFIO_EEH_PE_RESET_HOT 6 /* Assert hot reset */ 17561b69be5eSGavin Shan #define VFIO_EEH_PE_RESET_FUNDAMENTAL 7 /* Assert fundamental reset */ 17571b69be5eSGavin Shan #define VFIO_EEH_PE_CONFIGURE 8 /* PE configuration */ 175868cbbc3aSGavin Shan #define VFIO_EEH_PE_INJECT_ERR 9 /* Inject EEH error */ 17591b69be5eSGavin Shan 17601b69be5eSGavin Shan #define VFIO_EEH_PE_OP _IO(VFIO_TYPE, VFIO_BASE + 21) 17611b69be5eSGavin Shan 17622157e7b8SAlexey Kardashevskiy /** 17632157e7b8SAlexey Kardashevskiy * VFIO_IOMMU_SPAPR_REGISTER_MEMORY - _IOW(VFIO_TYPE, VFIO_BASE + 17, struct vfio_iommu_spapr_register_memory) 17642157e7b8SAlexey Kardashevskiy * 17652157e7b8SAlexey Kardashevskiy * Registers user space memory where DMA is allowed. It pins 17662157e7b8SAlexey Kardashevskiy * user pages and does the locked memory accounting so 17672157e7b8SAlexey Kardashevskiy * subsequent VFIO_IOMMU_MAP_DMA/VFIO_IOMMU_UNMAP_DMA calls 17682157e7b8SAlexey Kardashevskiy * get faster. 17692157e7b8SAlexey Kardashevskiy */ 17702157e7b8SAlexey Kardashevskiy struct vfio_iommu_spapr_register_memory { 17712157e7b8SAlexey Kardashevskiy __u32 argsz; 17722157e7b8SAlexey Kardashevskiy __u32 flags; 17732157e7b8SAlexey Kardashevskiy __u64 vaddr; /* Process virtual address */ 17742157e7b8SAlexey Kardashevskiy __u64 size; /* Size of mapping (bytes) */ 17752157e7b8SAlexey Kardashevskiy }; 17762157e7b8SAlexey Kardashevskiy #define VFIO_IOMMU_SPAPR_REGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 17) 17772157e7b8SAlexey Kardashevskiy 17782157e7b8SAlexey Kardashevskiy /** 17792157e7b8SAlexey Kardashevskiy * VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY - _IOW(VFIO_TYPE, VFIO_BASE + 18, struct vfio_iommu_spapr_register_memory) 17802157e7b8SAlexey Kardashevskiy * 17812157e7b8SAlexey Kardashevskiy * Unregisters user space memory registered with 17822157e7b8SAlexey Kardashevskiy * VFIO_IOMMU_SPAPR_REGISTER_MEMORY. 17832157e7b8SAlexey Kardashevskiy * Uses vfio_iommu_spapr_register_memory for parameters. 17842157e7b8SAlexey Kardashevskiy */ 17852157e7b8SAlexey Kardashevskiy #define VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 18) 17862157e7b8SAlexey Kardashevskiy 1787e633bc86SAlexey Kardashevskiy /** 1788e633bc86SAlexey Kardashevskiy * VFIO_IOMMU_SPAPR_TCE_CREATE - _IOWR(VFIO_TYPE, VFIO_BASE + 19, struct vfio_iommu_spapr_tce_create) 1789e633bc86SAlexey Kardashevskiy * 1790e633bc86SAlexey Kardashevskiy * Creates an additional TCE table and programs it (sets a new DMA window) 1791e633bc86SAlexey Kardashevskiy * to every IOMMU group in the container. It receives page shift, window 1792e633bc86SAlexey Kardashevskiy * size and number of levels in the TCE table being created. 1793e633bc86SAlexey Kardashevskiy * 1794e633bc86SAlexey Kardashevskiy * It allocates and returns an offset on a PCI bus of the new DMA window. 1795e633bc86SAlexey Kardashevskiy */ 1796e633bc86SAlexey Kardashevskiy struct vfio_iommu_spapr_tce_create { 1797e633bc86SAlexey Kardashevskiy __u32 argsz; 1798e633bc86SAlexey Kardashevskiy __u32 flags; 1799e633bc86SAlexey Kardashevskiy /* in */ 1800e633bc86SAlexey Kardashevskiy __u32 page_shift; 180177d6bd47SAlexey Kardashevskiy __u32 __resv1; 1802e633bc86SAlexey Kardashevskiy __u64 window_size; 1803e633bc86SAlexey Kardashevskiy __u32 levels; 180477d6bd47SAlexey Kardashevskiy __u32 __resv2; 1805e633bc86SAlexey Kardashevskiy /* out */ 1806e633bc86SAlexey Kardashevskiy __u64 start_addr; 1807e633bc86SAlexey Kardashevskiy }; 1808e633bc86SAlexey Kardashevskiy #define VFIO_IOMMU_SPAPR_TCE_CREATE _IO(VFIO_TYPE, VFIO_BASE + 19) 1809e633bc86SAlexey Kardashevskiy 1810e633bc86SAlexey Kardashevskiy /** 1811e633bc86SAlexey Kardashevskiy * VFIO_IOMMU_SPAPR_TCE_REMOVE - _IOW(VFIO_TYPE, VFIO_BASE + 20, struct vfio_iommu_spapr_tce_remove) 1812e633bc86SAlexey Kardashevskiy * 1813e633bc86SAlexey Kardashevskiy * Unprograms a TCE table from all groups in the container and destroys it. 1814e633bc86SAlexey Kardashevskiy * It receives a PCI bus offset as a window id. 1815e633bc86SAlexey Kardashevskiy */ 1816e633bc86SAlexey Kardashevskiy struct vfio_iommu_spapr_tce_remove { 1817e633bc86SAlexey Kardashevskiy __u32 argsz; 1818e633bc86SAlexey Kardashevskiy __u32 flags; 1819e633bc86SAlexey Kardashevskiy /* in */ 1820e633bc86SAlexey Kardashevskiy __u64 start_addr; 1821e633bc86SAlexey Kardashevskiy }; 1822e633bc86SAlexey Kardashevskiy #define VFIO_IOMMU_SPAPR_TCE_REMOVE _IO(VFIO_TYPE, VFIO_BASE + 20) 1823e633bc86SAlexey Kardashevskiy 18245ffd229cSAlexey Kardashevskiy /* ***************************************************************** */ 18255ffd229cSAlexey Kardashevskiy 1826607ca46eSDavid Howells #endif /* _UAPIVFIO_H */ 1827