Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

uio: Add SVA support for PCI devices via uio_pci_generic_sva.c

This patch introduces a new UIO driver, uio_pci_generic_sva, which
extends the functionality of uio_pci_generic by adding support for
Shared Virtual Addressing (SVA) when IOMMU is enabled in the system.

The key enhancement allows PCI devices to directly use user-space virtual
addresses for DMA operations, eliminating the need for bounce buffers or
explicit IOVA mapping. This is achieved by leveraging the kernel's IOMMU-SVA
subsystem, including process address space attachment, page fault handling,
and shared context management between CPU and device.

With this driver, userspace applications can perform zero-copy DMA using
native pointers:

void *addr = malloc(N);
set_dma_addr((uint64_t)addr); // Passing user VA directly
start_dma();

The device can now access 'addr' through the IOMMU's PASID-based translation,
provided that the underlying IOMMU hardware (e.g., Intel VT-d 3.1+, AMD-Vi,
ARM SMMU, RISCV IOMMU) and platform support SVA.

Dependencies:
- CONFIG_IOMMU_SVA must be enabled.
- The platform must support PRI (Page Request Interface) and PASID.
- Device drivers/userspace must handle page faults if demand-paging is used.

The implementation reuses core logic from uio_pci_generic.c while adding
PASID setting, and integration with the IOMMU SVA APIs.

Also, add a read-only sysfs attribute 'pasid' to expose the Process Address
Space ID assigned by IOMMU driver when binding an SVA-enabled device.
For details, refer to the ABI documentation for uio_pci_sva driver sysfs attribute
(Documentation/ABI/testing/sysfs-driver-uio_pci_sva-pasid).

Signed-off-by: Yaxing Guo <guoyaxing@bosc.ac.cn>
Link: https://patch.msgid.link/20250926095828.506-1-guoyaxing@bosc.ac.cn
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Yaxing Guo and committed by
Greg Kroah-Hartman
3397c3cd 4d4e746a

+234
+29
Documentation/ABI/testing/sysfs-driver-uio_pci_sva-pasid
··· 1 + What: /sys/bus/pci/drivers/uio_pci_sva/<pci_dev>/pasid 2 + Date: September 2025 3 + Contact: Yaxing Guo <guoyaxing@bosc.ac.cn> 4 + Description: 5 + Process Address Space ID (PASID) assigned by IOMMU driver to 6 + the device for use with Shared Virtual Addressing (SVA). 7 + 8 + This read-only attribute exposes the PASID (A 20-bit identifier 9 + used in PCIe Address Translation Services and iommu table walks) 10 + allocated by the IOMMU driver during sva device binding. 11 + 12 + User-space UIO applications must read this attribute to obtain 13 + the PASID and program it into the device's configuration registers. 14 + This enables the device to perform DMA using user-space virtual 15 + address, with address translation handled by IOMMU. 16 + 17 + UIO User-space applications must: 18 + - Opening device and Mapping the device's register space via /dev/uioX 19 + (This triggers the IOMMU driver to allocate the PASID) 20 + - Reading the PASID from sysfs 21 + - Writing the PASID to a device-specific register (with example offset) 22 + The code may be like: 23 + 24 + map = mmap(..., "/dev/uio0", ...); 25 + 26 + f = fopen("/sys/.../pasid", "r"); 27 + fscanf(f, "%d", &pasid); 28 + 29 + map[REG_PASID_OFFSET] = pasid;
+12
drivers/uio/Kconfig
··· 164 164 opae-sdk/tools/libopaeuio/ 165 165 166 166 If you compile this as a module, it will be called uio_dfl. 167 + 168 + config UIO_PCI_GENERIC_SVA 169 + tristate "Generic driver for PCI Express that supports sva" 170 + depends on PCI && IOMMU_SVA 171 + help 172 + Userspace I/O driver for PCI devices that support Shared Virtual 173 + Addressing (SVA), enabling direct use of user-space virtual 174 + addresses in device DMA operations via IOMMU hardware. 175 + 176 + This driver binds to PCI devices and exposes them to userspace 177 + via the UIO framework. 178 + 167 179 endif
+1
drivers/uio/Makefile
··· 11 11 obj-$(CONFIG_UIO_FSL_ELBC_GPCM) += uio_fsl_elbc_gpcm.o 12 12 obj-$(CONFIG_UIO_HV_GENERIC) += uio_hv_generic.o 13 13 obj-$(CONFIG_UIO_DFL) += uio_dfl.o 14 + obj-$(CONFIG_UIO_PCI_GENERIC_SVA) += uio_pci_generic_sva.o
+192
drivers/uio/uio_pci_generic_sva.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * UIO PCI Express sva driver 4 + * 5 + * Copyright (c) 2025 Beijing Institute of Open Source Chip (BOSC) 6 + */ 7 + 8 + #include <linux/device.h> 9 + #include <linux/module.h> 10 + #include <linux/pci.h> 11 + #include <linux/uio_driver.h> 12 + #include <linux/iommu.h> 13 + 14 + struct uio_pci_sva_dev { 15 + struct pci_dev *pdev; 16 + struct uio_info info; 17 + struct iommu_sva *sva_handle; 18 + int pasid; 19 + }; 20 + 21 + static irqreturn_t irq_handler(int irq, struct uio_info *dev_info) 22 + { 23 + return IRQ_HANDLED; 24 + } 25 + 26 + static int uio_pci_sva_open(struct uio_info *info, struct inode *inode) 27 + { 28 + struct iommu_sva *handle; 29 + struct uio_pci_sva_dev *udev = info->priv; 30 + struct iommu_domain *domain; 31 + 32 + if (!udev && !udev->pdev) 33 + return -ENODEV; 34 + 35 + domain = iommu_get_domain_for_dev(&udev->pdev->dev); 36 + if (domain) 37 + iommu_detach_device(domain, &udev->pdev->dev); 38 + 39 + handle = iommu_sva_bind_device(&udev->pdev->dev, current->mm); 40 + if (IS_ERR(handle)) 41 + return -EINVAL; 42 + 43 + udev->pasid = iommu_sva_get_pasid(handle); 44 + 45 + udev->sva_handle = handle; 46 + 47 + return 0; 48 + } 49 + 50 + static int uio_pci_sva_release(struct uio_info *info, struct inode *inode) 51 + { 52 + struct uio_pci_sva_dev *udev = info->priv; 53 + 54 + if (!udev && !udev->pdev) 55 + return -ENODEV; 56 + 57 + iommu_sva_unbind_device(udev->sva_handle); 58 + 59 + return 0; 60 + } 61 + 62 + static int probe(struct pci_dev *pdev, const struct pci_device_id *id) 63 + { 64 + struct uio_pci_sva_dev *udev; 65 + int ret, i, irq = 0; 66 + 67 + ret = pci_enable_device(pdev); 68 + if (ret) { 69 + dev_err(&pdev->dev, "pci_enable_device failed: %d\n", ret); 70 + return ret; 71 + } 72 + 73 + ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 74 + if (ret) 75 + goto out_disable; 76 + 77 + pci_set_master(pdev); 78 + 79 + ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSIX | PCI_IRQ_MSI); 80 + if (ret > 0) { 81 + irq = pci_irq_vector(pdev, 0); 82 + if (irq < 0) { 83 + dev_err(&pdev->dev, "Failed to get MSI vector\n"); 84 + ret = irq; 85 + goto out_disable; 86 + } 87 + } else 88 + dev_warn(&pdev->dev, 89 + "No IRQ vectors available (%d), using polling\n", ret); 90 + 91 + udev = devm_kzalloc(&pdev->dev, sizeof(struct uio_pci_sva_dev), 92 + GFP_KERNEL); 93 + if (!udev) { 94 + ret = -ENOMEM; 95 + goto out_disable; 96 + } 97 + 98 + udev->pdev = pdev; 99 + udev->info.name = "uio_pci_sva"; 100 + udev->info.version = "0.0.1"; 101 + udev->info.open = uio_pci_sva_open; 102 + udev->info.release = uio_pci_sva_release; 103 + udev->info.irq = irq; 104 + udev->info.handler = irq_handler; 105 + udev->info.priv = udev; 106 + 107 + for (i = 0; i < MAX_UIO_MAPS; i++) { 108 + struct resource *r = &pdev->resource[i]; 109 + struct uio_mem *uiomem = &udev->info.mem[i]; 110 + 111 + if (r->flags != (IORESOURCE_SIZEALIGN | IORESOURCE_MEM)) 112 + continue; 113 + 114 + if (uiomem >= &udev->info.mem[MAX_UIO_MAPS]) { 115 + dev_warn(&pdev->dev, "Do not support more than %d iomem\n", 116 + MAX_UIO_MAPS); 117 + break; 118 + } 119 + 120 + uiomem->memtype = UIO_MEM_PHYS; 121 + uiomem->addr = r->start & PAGE_MASK; 122 + uiomem->offs = r->start & ~PAGE_MASK; 123 + uiomem->size = 124 + (uiomem->offs + resource_size(r) + PAGE_SIZE - 1) & 125 + PAGE_MASK; 126 + uiomem->name = r->name; 127 + } 128 + 129 + ret = devm_uio_register_device(&pdev->dev, &udev->info); 130 + if (ret) { 131 + dev_err(&pdev->dev, "Failed to register uio device\n"); 132 + goto out_free; 133 + } 134 + 135 + pci_set_drvdata(pdev, udev); 136 + 137 + return 0; 138 + 139 + out_free: 140 + kfree(udev); 141 + out_disable: 142 + pci_disable_device(pdev); 143 + 144 + return ret; 145 + } 146 + 147 + static void remove(struct pci_dev *pdev) 148 + { 149 + struct uio_pci_sva_dev *udev = pci_get_drvdata(pdev); 150 + 151 + pci_release_regions(pdev); 152 + pci_disable_device(pdev); 153 + kfree(udev); 154 + } 155 + 156 + static ssize_t pasid_show(struct device *dev, 157 + struct device_attribute *attr, char *buf) 158 + { 159 + struct pci_dev *pdev = to_pci_dev(dev); 160 + struct uio_pci_sva_dev *udev = pci_get_drvdata(pdev); 161 + 162 + return sysfs_emit(buf, "%d\n", udev->pasid); 163 + } 164 + static DEVICE_ATTR_RO(pasid); 165 + 166 + static struct attribute *uio_pci_sva_attrs[] = { 167 + &dev_attr_pasid.attr, 168 + NULL 169 + }; 170 + 171 + static const struct attribute_group uio_pci_sva_attr_group = { 172 + .attrs = uio_pci_sva_attrs, 173 + }; 174 + 175 + static const struct attribute_group *uio_pci_sva_attr_groups[] = { 176 + &uio_pci_sva_attr_group, 177 + NULL 178 + }; 179 + 180 + static struct pci_driver uio_pci_generic_sva_driver = { 181 + .name = "uio_pci_sva", 182 + .dev_groups = uio_pci_sva_attr_groups, 183 + .id_table = NULL, 184 + .probe = probe, 185 + .remove = remove, 186 + }; 187 + 188 + module_pci_driver(uio_pci_generic_sva_driver); 189 + MODULE_VERSION("0.0.01"); 190 + MODULE_LICENSE("GPL v2"); 191 + MODULE_AUTHOR("Yaxing Guo <guoyaxing@bosc.ac.cn>"); 192 + MODULE_DESCRIPTION("Generic UIO sva driver for PCI");