mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	virt: acrn: Introduce interrupt injection interfaces
ACRN userspace need to inject virtual interrupts into a User VM in devices emulation. HSM needs provide interfaces to do so. Introduce following interrupt injection interfaces: ioctl ACRN_IOCTL_SET_IRQLINE: Pass data from userspace to the hypervisor, and inform the hypervisor to inject a virtual IOAPIC GSI interrupt to a User VM. ioctl ACRN_IOCTL_INJECT_MSI: Pass data struct acrn_msi_entry from userspace to the hypervisor, and inform the hypervisor to inject a virtual MSI to a User VM. ioctl ACRN_IOCTL_VM_INTR_MONITOR: Set a 4-Kbyte aligned shared page for statistics information of interrupts of a User VM. Cc: Zhi Wang <zhi.a.wang@intel.com> Cc: Zhenyu Wang <zhenyuw@linux.intel.com> Cc: Yu Wang <yu1.wang@intel.com> Cc: Reinette Chatre <reinette.chatre@intel.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Zhi Wang <zhi.a.wang@intel.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Shuo Liu <shuo.a.liu@intel.com> Link: https://lore.kernel.org/r/20210207031040.49576-13-shuo.a.liu@intel.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
		
							parent
							
								
									ce011e1363
								
							
						
					
					
						commit
						c7cf8d2724
					
				
					 5 changed files with 138 additions and 0 deletions
				
			
		| 
						 | 
					@ -155,6 +155,7 @@ extern rwlock_t acrn_vm_list_lock;
 | 
				
			||||||
 * @ioreq_buf:			I/O request shared buffer
 | 
					 * @ioreq_buf:			I/O request shared buffer
 | 
				
			||||||
 * @ioreq_page:			The page of the I/O request shared buffer
 | 
					 * @ioreq_page:			The page of the I/O request shared buffer
 | 
				
			||||||
 * @pci_conf_addr:		Address of a PCI configuration access emulation
 | 
					 * @pci_conf_addr:		Address of a PCI configuration access emulation
 | 
				
			||||||
 | 
					 * @monitor_page:		Page of interrupt statistics of User VM
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
struct acrn_vm {
 | 
					struct acrn_vm {
 | 
				
			||||||
	struct list_head		list;
 | 
						struct list_head		list;
 | 
				
			||||||
| 
						 | 
					@ -170,6 +171,7 @@ struct acrn_vm {
 | 
				
			||||||
	struct acrn_io_request_buffer	*ioreq_buf;
 | 
						struct acrn_io_request_buffer	*ioreq_buf;
 | 
				
			||||||
	struct page			*ioreq_page;
 | 
						struct page			*ioreq_page;
 | 
				
			||||||
	u32				pci_conf_addr;
 | 
						u32				pci_conf_addr;
 | 
				
			||||||
 | 
						struct page			*monitor_page;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct acrn_vm *acrn_vm_create(struct acrn_vm *vm,
 | 
					struct acrn_vm *acrn_vm_create(struct acrn_vm *vm,
 | 
				
			||||||
| 
						 | 
					@ -196,4 +198,6 @@ struct acrn_ioreq_client *acrn_ioreq_client_create(struct acrn_vm *vm,
 | 
				
			||||||
						   const char *name);
 | 
											   const char *name);
 | 
				
			||||||
void acrn_ioreq_client_destroy(struct acrn_ioreq_client *client);
 | 
					void acrn_ioreq_client_destroy(struct acrn_ioreq_client *client);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int acrn_msi_inject(struct acrn_vm *vm, u64 msi_addr, u64 msi_data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* __ACRN_HSM_DRV_H */
 | 
					#endif /* __ACRN_HSM_DRV_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -51,7 +51,9 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
 | 
				
			||||||
	struct acrn_ioreq_notify notify;
 | 
						struct acrn_ioreq_notify notify;
 | 
				
			||||||
	struct acrn_ptdev_irq *irq_info;
 | 
						struct acrn_ptdev_irq *irq_info;
 | 
				
			||||||
	struct acrn_vm_memmap memmap;
 | 
						struct acrn_vm_memmap memmap;
 | 
				
			||||||
 | 
						struct acrn_msi_entry *msi;
 | 
				
			||||||
	struct acrn_pcidev *pcidev;
 | 
						struct acrn_pcidev *pcidev;
 | 
				
			||||||
 | 
						struct page *page;
 | 
				
			||||||
	int i, ret = 0;
 | 
						int i, ret = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (vm->vmid == ACRN_INVALID_VMID && cmd != ACRN_IOCTL_CREATE_VM) {
 | 
						if (vm->vmid == ACRN_INVALID_VMID && cmd != ACRN_IOCTL_CREATE_VM) {
 | 
				
			||||||
| 
						 | 
					@ -198,6 +200,44 @@ static long acrn_dev_ioctl(struct file *filp, unsigned int cmd,
 | 
				
			||||||
				"Failed to reset intr for ptdev!\n");
 | 
									"Failed to reset intr for ptdev!\n");
 | 
				
			||||||
		kfree(irq_info);
 | 
							kfree(irq_info);
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
 | 
						case ACRN_IOCTL_SET_IRQLINE:
 | 
				
			||||||
 | 
							ret = hcall_set_irqline(vm->vmid, ioctl_param);
 | 
				
			||||||
 | 
							if (ret < 0)
 | 
				
			||||||
 | 
								dev_dbg(acrn_dev.this_device,
 | 
				
			||||||
 | 
									"Failed to set interrupt line!\n");
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
						case ACRN_IOCTL_INJECT_MSI:
 | 
				
			||||||
 | 
							msi = memdup_user((void __user *)ioctl_param,
 | 
				
			||||||
 | 
									  sizeof(struct acrn_msi_entry));
 | 
				
			||||||
 | 
							if (IS_ERR(msi))
 | 
				
			||||||
 | 
								return PTR_ERR(msi);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ret = hcall_inject_msi(vm->vmid, virt_to_phys(msi));
 | 
				
			||||||
 | 
							if (ret < 0)
 | 
				
			||||||
 | 
								dev_dbg(acrn_dev.this_device,
 | 
				
			||||||
 | 
									"Failed to inject MSI!\n");
 | 
				
			||||||
 | 
							kfree(msi);
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
						case ACRN_IOCTL_VM_INTR_MONITOR:
 | 
				
			||||||
 | 
							ret = pin_user_pages_fast(ioctl_param, 1,
 | 
				
			||||||
 | 
										  FOLL_WRITE | FOLL_LONGTERM, &page);
 | 
				
			||||||
 | 
							if (unlikely(ret != 1)) {
 | 
				
			||||||
 | 
								dev_dbg(acrn_dev.this_device,
 | 
				
			||||||
 | 
									"Failed to pin intr hdr buffer!\n");
 | 
				
			||||||
 | 
								return -EFAULT;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ret = hcall_vm_intr_monitor(vm->vmid, page_to_phys(page));
 | 
				
			||||||
 | 
							if (ret < 0) {
 | 
				
			||||||
 | 
								unpin_user_page(page);
 | 
				
			||||||
 | 
								dev_dbg(acrn_dev.this_device,
 | 
				
			||||||
 | 
									"Failed to monitor intr data!\n");
 | 
				
			||||||
 | 
								return ret;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if (vm->monitor_page)
 | 
				
			||||||
 | 
								unpin_user_page(vm->monitor_page);
 | 
				
			||||||
 | 
							vm->monitor_page = page;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
	case ACRN_IOCTL_CREATE_IOREQ_CLIENT:
 | 
						case ACRN_IOCTL_CREATE_IOREQ_CLIENT:
 | 
				
			||||||
		if (vm->default_client)
 | 
							if (vm->default_client)
 | 
				
			||||||
			return -EEXIST;
 | 
								return -EEXIST;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,6 +21,11 @@
 | 
				
			||||||
#define HC_RESET_VM			_HC_ID(HC_ID, HC_ID_VM_BASE + 0x05)
 | 
					#define HC_RESET_VM			_HC_ID(HC_ID, HC_ID_VM_BASE + 0x05)
 | 
				
			||||||
#define HC_SET_VCPU_REGS		_HC_ID(HC_ID, HC_ID_VM_BASE + 0x06)
 | 
					#define HC_SET_VCPU_REGS		_HC_ID(HC_ID, HC_ID_VM_BASE + 0x06)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define HC_ID_IRQ_BASE			0x20UL
 | 
				
			||||||
 | 
					#define HC_INJECT_MSI			_HC_ID(HC_ID, HC_ID_IRQ_BASE + 0x03)
 | 
				
			||||||
 | 
					#define HC_VM_INTR_MONITOR		_HC_ID(HC_ID, HC_ID_IRQ_BASE + 0x04)
 | 
				
			||||||
 | 
					#define HC_SET_IRQLINE			_HC_ID(HC_ID, HC_ID_IRQ_BASE + 0x05)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define HC_ID_IOREQ_BASE		0x30UL
 | 
					#define HC_ID_IOREQ_BASE		0x30UL
 | 
				
			||||||
#define HC_SET_IOREQ_BUFFER		_HC_ID(HC_ID, HC_ID_IOREQ_BASE + 0x00)
 | 
					#define HC_SET_IOREQ_BUFFER		_HC_ID(HC_ID, HC_ID_IOREQ_BASE + 0x00)
 | 
				
			||||||
#define HC_NOTIFY_REQUEST_FINISH	_HC_ID(HC_ID, HC_ID_IOREQ_BASE + 0x01)
 | 
					#define HC_NOTIFY_REQUEST_FINISH	_HC_ID(HC_ID, HC_ID_IOREQ_BASE + 0x01)
 | 
				
			||||||
| 
						 | 
					@ -101,6 +106,42 @@ static inline long hcall_set_vcpu_regs(u64 vmid, u64 regs_state)
 | 
				
			||||||
	return acrn_hypercall2(HC_SET_VCPU_REGS, vmid, regs_state);
 | 
						return acrn_hypercall2(HC_SET_VCPU_REGS, vmid, regs_state);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * hcall_inject_msi() - Deliver a MSI interrupt to a User VM
 | 
				
			||||||
 | 
					 * @vmid:	User VM ID
 | 
				
			||||||
 | 
					 * @msi:	Service VM GPA of MSI message
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Return: 0 on success, <0 on failure
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static inline long hcall_inject_msi(u64 vmid, u64 msi)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return acrn_hypercall2(HC_INJECT_MSI, vmid, msi);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * hcall_vm_intr_monitor() - Set a shared page for User VM interrupt statistics
 | 
				
			||||||
 | 
					 * @vmid:	User VM ID
 | 
				
			||||||
 | 
					 * @addr:	Service VM GPA of the shared page
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Return: 0 on success, <0 on failure
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static inline long hcall_vm_intr_monitor(u64 vmid, u64 addr)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return acrn_hypercall2(HC_VM_INTR_MONITOR, vmid, addr);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * hcall_set_irqline() - Set or clear an interrupt line
 | 
				
			||||||
 | 
					 * @vmid:	User VM ID
 | 
				
			||||||
 | 
					 * @op:		Service VM GPA of interrupt line operations
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Return: 0 on success, <0 on failure
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static inline long hcall_set_irqline(u64 vmid, u64 op)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return acrn_hypercall2(HC_SET_IRQLINE, vmid, op);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * hcall_set_ioreq_buffer() - Set up the shared buffer for I/O Requests.
 | 
					 * hcall_set_ioreq_buffer() - Set up the shared buffer for I/O Requests.
 | 
				
			||||||
 * @vmid:	User VM ID
 | 
					 * @vmid:	User VM ID
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -68,6 +68,10 @@ int acrn_vm_destroy(struct acrn_vm *vm)
 | 
				
			||||||
	write_unlock_bh(&acrn_vm_list_lock);
 | 
						write_unlock_bh(&acrn_vm_list_lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	acrn_ioreq_deinit(vm);
 | 
						acrn_ioreq_deinit(vm);
 | 
				
			||||||
 | 
						if (vm->monitor_page) {
 | 
				
			||||||
 | 
							put_page(vm->monitor_page);
 | 
				
			||||||
 | 
							vm->monitor_page = NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = hcall_destroy_vm(vm->vmid);
 | 
						ret = hcall_destroy_vm(vm->vmid);
 | 
				
			||||||
	if (ret < 0) {
 | 
						if (ret < 0) {
 | 
				
			||||||
| 
						 | 
					@ -83,3 +87,35 @@ int acrn_vm_destroy(struct acrn_vm *vm)
 | 
				
			||||||
	vm->vmid = ACRN_INVALID_VMID;
 | 
						vm->vmid = ACRN_INVALID_VMID;
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * acrn_inject_msi() - Inject a MSI interrupt into a User VM
 | 
				
			||||||
 | 
					 * @vm:		User VM
 | 
				
			||||||
 | 
					 * @msi_addr:	The MSI address
 | 
				
			||||||
 | 
					 * @msi_data:	The MSI data
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Return: 0 on success, <0 on error
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int acrn_msi_inject(struct acrn_vm *vm, u64 msi_addr, u64 msi_data)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct acrn_msi_entry *msi;
 | 
				
			||||||
 | 
						int ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* might be used in interrupt context, so use GFP_ATOMIC */
 | 
				
			||||||
 | 
						msi = kzalloc(sizeof(*msi), GFP_ATOMIC);
 | 
				
			||||||
 | 
						if (!msi)
 | 
				
			||||||
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * msi_addr: addr[19:12] with dest vcpu id
 | 
				
			||||||
 | 
						 * msi_data: data[7:0] with vector
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						msi->msi_addr = msi_addr;
 | 
				
			||||||
 | 
						msi->msi_data = msi_data;
 | 
				
			||||||
 | 
						ret = hcall_inject_msi(vm->vmid, virt_to_phys(msi));
 | 
				
			||||||
 | 
						if (ret < 0)
 | 
				
			||||||
 | 
							dev_err(acrn_dev.this_device,
 | 
				
			||||||
 | 
								"Failed to inject MSI to VM %u!\n", vm->vmid);
 | 
				
			||||||
 | 
						kfree(msi);
 | 
				
			||||||
 | 
						return ret;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -417,6 +417,16 @@ struct acrn_pcidev {
 | 
				
			||||||
	__u32	bar[ACRN_PCI_NUM_BARS];
 | 
						__u32	bar[ACRN_PCI_NUM_BARS];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * struct acrn_msi_entry - Info for injecting a MSI interrupt to a VM
 | 
				
			||||||
 | 
					 * @msi_addr:	MSI addr[19:12] with dest vCPU ID
 | 
				
			||||||
 | 
					 * @msi_data:	MSI data[7:0] with vector
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					struct acrn_msi_entry {
 | 
				
			||||||
 | 
						__u64	msi_addr;
 | 
				
			||||||
 | 
						__u64	msi_data;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* The ioctl type, documented in ioctl-number.rst */
 | 
					/* The ioctl type, documented in ioctl-number.rst */
 | 
				
			||||||
#define ACRN_IOCTL_TYPE			0xA2
 | 
					#define ACRN_IOCTL_TYPE			0xA2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -436,6 +446,13 @@ struct acrn_pcidev {
 | 
				
			||||||
#define ACRN_IOCTL_SET_VCPU_REGS	\
 | 
					#define ACRN_IOCTL_SET_VCPU_REGS	\
 | 
				
			||||||
	_IOW(ACRN_IOCTL_TYPE, 0x16, struct acrn_vcpu_regs)
 | 
						_IOW(ACRN_IOCTL_TYPE, 0x16, struct acrn_vcpu_regs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define ACRN_IOCTL_INJECT_MSI		\
 | 
				
			||||||
 | 
						_IOW(ACRN_IOCTL_TYPE, 0x23, struct acrn_msi_entry)
 | 
				
			||||||
 | 
					#define ACRN_IOCTL_VM_INTR_MONITOR	\
 | 
				
			||||||
 | 
						_IOW(ACRN_IOCTL_TYPE, 0x24, unsigned long)
 | 
				
			||||||
 | 
					#define ACRN_IOCTL_SET_IRQLINE		\
 | 
				
			||||||
 | 
						_IOW(ACRN_IOCTL_TYPE, 0x25, __u64)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define ACRN_IOCTL_NOTIFY_REQUEST_FINISH \
 | 
					#define ACRN_IOCTL_NOTIFY_REQUEST_FINISH \
 | 
				
			||||||
	_IOW(ACRN_IOCTL_TYPE, 0x31, struct acrn_ioreq_notify)
 | 
						_IOW(ACRN_IOCTL_TYPE, 0x31, struct acrn_ioreq_notify)
 | 
				
			||||||
#define ACRN_IOCTL_CREATE_IOREQ_CLIENT	\
 | 
					#define ACRN_IOCTL_CREATE_IOREQ_CLIENT	\
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue