PCI device passthrough enables an OS in a virtual machine to directly access a PCI device in the host. It promises almost the native performance, which is required in performance-critical scenarios of ACRN. HSM provides the following ioctls: - Assign - ACRN_IOCTL_ASSIGN_PCIDEV Pass data struct acrn_pcidev from userspace to the hypervisor, and inform the hypervisor to assign a PCI device to a User VM. - De-assign - ACRN_IOCTL_DEASSIGN_PCIDEV Pass data struct acrn_pcidev from userspace to the hypervisor, and inform the hypervisor to de-assign a PCI device from a User VM. - Set a interrupt of a passthrough device - ACRN_IOCTL_SET_PTDEV_INTR Pass data struct acrn_ptdev_irq from userspace to the hypervisor, and inform the hypervisor to map a INTx interrupt of passthrough device of User VM. - Reset passthrough device interrupt - ACRN_IOCTL_RESET_PTDEV_INTR Pass data struct acrn_ptdev_irq from userspace to the hypervisor, and inform the hypervisor to unmap a INTx interrupt of passthrough device of User VM.
Unified · Split
| @@ -49,7 +49,9 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd, · 48 unmodified lines | ||
| 49 | 49 | struct acrn_vm_creation *vm_param; |
| 50 | 50 | struct acrn_vcpu_regs *cpu_regs; |
| 51 | 51 | struct acrn_ioreq_notify notify; |
| 52 | + struct acrn_ptdev_irq *irq_info; | |
| 52 | 53 | struct acrn_vm_memmap memmap; |
| 54 | + struct acrn_pcidev *pcidev; | |
| 53 | 55 | int i, ret = 0; |
| 54 | 56 | |
| 55 | 57 | if (vm->vmid == ACRN_INVALID_VMID && cmd != ACRN_IOCTL_CREATE_VM) { |
| @@ -148,6 +150,54 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd, · 92 unmodified lines | ||
| 148 | 150 | |
| 149 | 151 | ret = acrn_vm_memseg_unmap(vm, &memmap); |
| 150 | 152 | break; |
| 153 | + case ACRN_IOCTL_ASSIGN_PCIDEV: | |
| 154 | + pcidev = memdup_user((void __user *)ioctl_param, | |
| 155 | + sizeof(struct acrn_pcidev)); | |
| 156 | + if (IS_ERR(pcidev)) | |
| 157 | + return PTR_ERR(pcidev); | |
| 158 | + | |
| 159 | + ret = hcall_assign_pcidev(vm->vmid, virt_to_phys(pcidev)); | |
| 160 | + if (ret < 0) | |
| 161 | + dev_dbg(acrn_dev.this_device, | |
| 162 | + "Failed to assign pci device!\n"); | |
| 163 | + kfree(pcidev); | |
| 164 | + break; | |
| 165 | + case ACRN_IOCTL_DEASSIGN_PCIDEV: | |
| 166 | + pcidev = memdup_user((void __user *)ioctl_param, | |
| 167 | + sizeof(struct acrn_pcidev)); | |
| 168 | + if (IS_ERR(pcidev)) | |
| 169 | + return PTR_ERR(pcidev); | |
| 170 | + | |
| 171 | + ret = hcall_deassign_pcidev(vm->vmid, virt_to_phys(pcidev)); | |
| 172 | + if (ret < 0) | |
| 173 | + dev_dbg(acrn_dev.this_device, | |
| 174 | + "Failed to deassign pci device!\n"); | |
| 175 | + kfree(pcidev); | |
| 176 | + break; | |
| 177 | + case ACRN_IOCTL_SET_PTDEV_INTR: | |
| 178 | + irq_info = memdup_user((void __user *)ioctl_param, | |
| 179 | + sizeof(struct acrn_ptdev_irq)); | |
| 180 | + if (IS_ERR(irq_info)) | |
| 181 | + return PTR_ERR(irq_info); | |
| 182 | + | |
| 183 | + ret = hcall_set_ptdev_intr(vm->vmid, virt_to_phys(irq_info)); | |
| 184 | + if (ret < 0) | |
| 185 | + dev_dbg(acrn_dev.this_device, | |
| 186 | + "Failed to configure intr for ptdev!\n"); | |
| 187 | + kfree(irq_info); | |
| 188 | + break; | |
| 189 | + case ACRN_IOCTL_RESET_PTDEV_INTR: | |
| 190 | + irq_info = memdup_user((void __user *)ioctl_param, | |
| 191 | + sizeof(struct acrn_ptdev_irq)); | |
| 192 | + if (IS_ERR(irq_info)) | |
| 193 | + return PTR_ERR(irq_info); | |
| 194 | + | |
| 195 | + ret = hcall_reset_ptdev_intr(vm->vmid, virt_to_phys(irq_info)); | |
| 196 | + if (ret < 0) | |
| 197 | + dev_dbg(acrn_dev.this_device, | |
| 198 | + "Failed to reset intr for ptdev!\n"); | |
| 199 | + kfree(irq_info); | |
| 200 | + break; | |
| 151 | 201 | case ACRN_IOCTL_CREATE_IOREQ_CLIENT: |
| 152 | 202 | if (vm->default_client) |
| 153 | 203 | return -EEXIST; |
| @@ -28,6 +28,12 @@ · 27 unmodified lines | ||
| 28 | 28 | #define HC_ID_MEM_BASE 0x40UL |
| 29 | 29 | #define HC_VM_SET_MEMORY_REGIONS _HC_ID(HC_ID, HC_ID_MEM_BASE + 0x02) |
| 30 | 30 | |
| 31 | +#define HC_ID_PCI_BASE 0x50UL | |
| 32 | +#define HC_SET_PTDEV_INTR _HC_ID(HC_ID, HC_ID_PCI_BASE + 0x03) | |
| 33 | +#define HC_RESET_PTDEV_INTR _HC_ID(HC_ID, HC_ID_PCI_BASE + 0x04) | |
| 34 | +#define HC_ASSIGN_PCIDEV _HC_ID(HC_ID, HC_ID_PCI_BASE + 0x05) | |
| 35 | +#define HC_DEASSIGN_PCIDEV _HC_ID(HC_ID, HC_ID_PCI_BASE + 0x06) | |
| 36 | + | |
| 31 | 37 | /** |
| 32 | 38 | * hcall_create_vm() - Create a User VM |
| 33 | 39 | * @vminfo: Service VM GPA of info of User VM creation |
| @@ -130,4 +136,52 @@ static inline long hcall_set_memory_regions(u64 regions_pa) · 96 unmodified lines | ||
| 130 | 136 | return acrn_hypercall1(HC_VM_SET_MEMORY_REGIONS, regions_pa); |
| 131 | 137 | } |
| 132 | 138 | |
| 139 | +/** | |
| 140 | + * hcall_assign_pcidev() - Assign a PCI device to a User VM | |
| 141 | + * @vmid: User VM ID | |
| 142 | + * @addr: Service VM GPA of the &struct acrn_pcidev | |
| 143 | + * | |
| 144 | + * Return: 0 on success, <0 on failure | |
| 145 | + */ | |
| 146 | +static inline long hcall_assign_pcidev(u64 vmid, u64 addr) | |
| 147 | +{ | |
| 148 | + return acrn_hypercall2(HC_ASSIGN_PCIDEV, vmid, addr); | |
| 149 | +} | |
| 150 | + | |
| 151 | +/** | |
| 152 | + * hcall_deassign_pcidev() - De-assign a PCI device from a User VM | |
| 153 | + * @vmid: User VM ID | |
| 154 | + * @addr: Service VM GPA of the &struct acrn_pcidev | |
| 155 | + * | |
| 156 | + * Return: 0 on success, <0 on failure | |
| 157 | + */ | |
| 158 | +static inline long hcall_deassign_pcidev(u64 vmid, u64 addr) | |
| 159 | +{ | |
| 160 | + return acrn_hypercall2(HC_DEASSIGN_PCIDEV, vmid, addr); | |
| 161 | +} | |
| 162 | + | |
| 163 | +/** | |
| 164 | + * hcall_set_ptdev_intr() - Configure an interrupt for an assigned PCI device. | |
| 165 | + * @vmid: User VM ID | |
| 166 | + * @irq: Service VM GPA of the &struct acrn_ptdev_irq | |
| 167 | + * | |
| 168 | + * Return: 0 on success, <0 on failure | |
| 169 | + */ | |
| 170 | +static inline long hcall_set_ptdev_intr(u64 vmid, u64 irq) | |
| 171 | +{ | |
| 172 | + return acrn_hypercall2(HC_SET_PTDEV_INTR, vmid, irq); | |
| 173 | +} | |
| 174 | + | |
| 175 | +/** | |
| 176 | + * hcall_reset_ptdev_intr() - Reset an interrupt for an assigned PCI device. | |
| 177 | + * @vmid: User VM ID | |
| 178 | + * @irq: Service VM GPA of the &struct acrn_ptdev_irq | |
| 179 | + * | |
| 180 | + * Return: 0 on success, <0 on failure | |
| 181 | + */ | |
| 182 | +static inline long hcall_reset_ptdev_intr(u64 vmid, u64 irq) | |
| 183 | +{ | |
| 184 | + return acrn_hypercall2(HC_RESET_PTDEV_INTR, vmid, irq); | |
| 185 | +} | |
| 186 | + | |
| 133 | 187 | #endif /* __ACRN_HSM_HYPERCALL_H */ |
| @@ -365,6 +365,58 @@ struct acrn_vm_memmap { · 364 unmodified lines | ||
| 365 | 365 | __u64 len; |
| 366 | 366 | }; |
| 367 | 367 | |
| 368 | +/* Type of interrupt of a passthrough device */ | |
| 369 | +#define ACRN_PTDEV_IRQ_INTX 0 | |
| 370 | +#define ACRN_PTDEV_IRQ_MSI 1 | |
| 371 | +#define ACRN_PTDEV_IRQ_MSIX 2 | |
| 372 | +/** | |
| 373 | + * struct acrn_ptdev_irq - Interrupt data of a passthrough device. | |
| 374 | + * @type: Type (ACRN_PTDEV_IRQ_*) | |
| 375 | + * @virt_bdf: Virtual Bus/Device/Function | |
| 376 | + * @phys_bdf: Physical Bus/Device/Function | |
| 377 | + * @intx: Info of interrupt | |
| 378 | + * @intx.virt_pin: Virtual IOAPIC pin | |
| 379 | + * @intx.phys_pin: Physical IOAPIC pin | |
| 380 | + * @intx.is_pic_pin: Is PIC pin or not | |
| 381 | + * | |
| 382 | + * This structure will be passed to hypervisor directly. | |
| 383 | + */ | |
| 384 | +struct acrn_ptdev_irq { | |
| 385 | + __u32 type; | |
| 386 | + __u16 virt_bdf; | |
| 387 | + __u16 phys_bdf; | |
| 388 | + | |
| 389 | + struct { | |
| 390 | + __u32 virt_pin; | |
| 391 | + __u32 phys_pin; | |
| 392 | + __u32 is_pic_pin; | |
| 393 | + } intx; | |
| 394 | +}; | |
| 395 | + | |
| 396 | +/* Type of PCI device assignment */ | |
| 397 | +#define ACRN_PTDEV_QUIRK_ASSIGN (1U << 0) | |
| 398 | + | |
| 399 | +#define ACRN_PCI_NUM_BARS 6 | |
| 400 | +/** | |
| 401 | + * struct acrn_pcidev - Info for assigning or de-assigning a PCI device | |
| 402 | + * @type: Type of the assignment | |
| 403 | + * @virt_bdf: Virtual Bus/Device/Function | |
| 404 | + * @phys_bdf: Physical Bus/Device/Function | |
| 405 | + * @intr_line: PCI interrupt line | |
| 406 | + * @intr_pin: PCI interrupt pin | |
| 407 | + * @bar: PCI BARs. | |
| 408 | + * | |
| 409 | + * This structure will be passed to hypervisor directly. | |
| 410 | + */ | |
| 411 | +struct acrn_pcidev { | |
| 412 | + __u32 type; | |
| 413 | + __u16 virt_bdf; | |
| 414 | + __u16 phys_bdf; | |
| 415 | + __u8 intr_line; | |
| 416 | + __u8 intr_pin; | |
| 417 | + __u32 bar[ACRN_PCI_NUM_BARS]; | |
| 418 | +}; | |
| 419 | + | |
| 368 | 420 | /* The ioctl type, documented in ioctl-number.rst */ |
| 369 | 421 | #define ACRN_IOCTL_TYPE 0xA2 |
| 370 | 422 | |
| @@ -400,4 +452,13 @@ struct acrn_vm_memmap { · 29 unmodified lines | ||
| 400 | 452 | #define ACRN_IOCTL_UNSET_MEMSEG \ |
| 401 | 453 | _IOW(ACRN_IOCTL_TYPE, 0x42, struct acrn_vm_memmap) |
| 402 | 454 | |
| 455 | +#define ACRN_IOCTL_SET_PTDEV_INTR \ | |
| 456 | + _IOW(ACRN_IOCTL_TYPE, 0x53, struct acrn_ptdev_irq) | |
| 457 | +#define ACRN_IOCTL_RESET_PTDEV_INTR \ | |
| 458 | + _IOW(ACRN_IOCTL_TYPE, 0x54, struct acrn_ptdev_irq) | |
| 459 | +#define ACRN_IOCTL_ASSIGN_PCIDEV \ | |
| 460 | + _IOW(ACRN_IOCTL_TYPE, 0x55, struct acrn_pcidev) | |
| 461 | +#define ACRN_IOCTL_DEASSIGN_PCIDEV \ | |
| 462 | + _IOW(ACRN_IOCTL_TYPE, 0x56, struct acrn_pcidev) | |
| 463 | + | |
| 403 | 464 | #endif /* _UAPI_ACRN_H */ |