Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef __ACRN_HSM_DRV_H
0004 #define __ACRN_HSM_DRV_H
0005 
0006 #include <linux/acrn.h>
0007 #include <linux/dev_printk.h>
0008 #include <linux/miscdevice.h>
0009 #include <linux/types.h>
0010 
0011 #include "hypercall.h"
0012 
0013 extern struct miscdevice acrn_dev;
0014 
0015 #define ACRN_NAME_LEN       16
0016 #define ACRN_MEM_MAPPING_MAX    256
0017 
0018 #define ACRN_MEM_REGION_ADD 0
0019 #define ACRN_MEM_REGION_DEL 2
0020 
0021 struct acrn_vm;
0022 struct acrn_ioreq_client;
0023 
0024 /**
0025  * struct vm_memory_region_op - Hypervisor memory operation
0026  * @type:       Operation type (ACRN_MEM_REGION_*)
0027  * @attr:       Memory attribute (ACRN_MEM_TYPE_* | ACRN_MEM_ACCESS_*)
0028  * @user_vm_pa:     Physical address of User VM to be mapped.
0029  * @service_vm_pa:  Physical address of Service VM to be mapped.
0030  * @size:       Size of this region.
0031  *
0032  * Structure containing needed information that is provided to ACRN Hypervisor
0033  * to manage the EPT mappings of a single memory region of the User VM. Several
0034  * &struct vm_memory_region_op can be batched to ACRN Hypervisor, see &struct
0035  * vm_memory_region_batch.
0036  */
0037 struct vm_memory_region_op {
0038     u32 type;
0039     u32 attr;
0040     u64 user_vm_pa;
0041     u64 service_vm_pa;
0042     u64 size;
0043 };
0044 
0045 /**
0046  * struct vm_memory_region_batch - A batch of vm_memory_region_op.
0047  * @vmid:       A User VM ID.
0048  * @reserved:       Reserved.
0049  * @regions_num:    The number of vm_memory_region_op.
0050  * @regions_gpa:    Physical address of a vm_memory_region_op array.
0051  * @regions_op:     Flexible array of vm_memory_region_op.
0052  *
0053  * HC_VM_SET_MEMORY_REGIONS uses this structure to manage EPT mappings of
0054  * multiple memory regions of a User VM. A &struct vm_memory_region_batch
0055  * contains multiple &struct vm_memory_region_op for batch processing in the
0056  * ACRN Hypervisor.
0057  */
0058 struct vm_memory_region_batch {
0059     u16            vmid;
0060     u16            reserved[3];
0061     u32            regions_num;
0062     u64            regions_gpa;
0063     struct vm_memory_region_op regions_op[];
0064 };
0065 
0066 /**
0067  * struct vm_memory_mapping - Memory map between a User VM and the Service VM
0068  * @pages:      Pages in Service VM kernel.
0069  * @npages:     Number of pages.
0070  * @service_vm_va:  Virtual address in Service VM kernel.
0071  * @user_vm_pa:     Physical address in User VM.
0072  * @size:       Size of this memory region.
0073  *
0074  * HSM maintains memory mappings between a User VM GPA and the Service VM
0075  * kernel VA for accelerating the User VM GPA translation.
0076  */
0077 struct vm_memory_mapping {
0078     struct page **pages;
0079     int     npages;
0080     void        *service_vm_va;
0081     u64     user_vm_pa;
0082     size_t      size;
0083 };
0084 
0085 /**
0086  * struct acrn_ioreq_buffer - Data for setting the ioreq buffer of User VM
0087  * @ioreq_buf:  The GPA of the IO request shared buffer of a VM
0088  *
0089  * The parameter for the HC_SET_IOREQ_BUFFER hypercall used to set up
0090  * the shared I/O request buffer between Service VM and ACRN hypervisor.
0091  */
0092 struct acrn_ioreq_buffer {
0093     u64 ioreq_buf;
0094 };
0095 
0096 struct acrn_ioreq_range {
0097     struct list_head    list;
0098     u32         type;
0099     u64         start;
0100     u64         end;
0101 };
0102 
0103 #define ACRN_IOREQ_CLIENT_DESTROYING    0U
0104 typedef int (*ioreq_handler_t)(struct acrn_ioreq_client *client,
0105                    struct acrn_io_request *req);
0106 /**
0107  * struct acrn_ioreq_client - Structure of I/O client.
0108  * @name:   Client name
0109  * @vm:     The VM that the client belongs to
0110  * @list:   List node for this acrn_ioreq_client
0111  * @is_default: If this client is the default one
0112  * @flags:  Flags (ACRN_IOREQ_CLIENT_*)
0113  * @range_list: I/O ranges
0114  * @range_lock: Lock to protect range_list
0115  * @ioreqs_map: The pending I/O requests bitmap.
0116  * @handler:    I/O requests handler of this client
0117  * @thread: The thread which executes the handler
0118  * @wq:     The wait queue for the handler thread parking
0119  * @priv:   Data for the thread
0120  */
0121 struct acrn_ioreq_client {
0122     char            name[ACRN_NAME_LEN];
0123     struct acrn_vm      *vm;
0124     struct list_head    list;
0125     bool            is_default;
0126     unsigned long       flags;
0127     struct list_head    range_list;
0128     rwlock_t        range_lock;
0129     DECLARE_BITMAP(ioreqs_map, ACRN_IO_REQUEST_MAX);
0130     ioreq_handler_t     handler;
0131     struct task_struct  *thread;
0132     wait_queue_head_t   wq;
0133     void            *priv;
0134 };
0135 
0136 #define ACRN_INVALID_VMID (0xffffU)
0137 
0138 #define ACRN_VM_FLAG_DESTROYED      0U
0139 #define ACRN_VM_FLAG_CLEARING_IOREQ 1U
0140 extern struct list_head acrn_vm_list;
0141 extern rwlock_t acrn_vm_list_lock;
0142 /**
0143  * struct acrn_vm - Properties of ACRN User VM.
0144  * @list:           Entry within global list of all VMs.
0145  * @vmid:           User VM ID.
0146  * @vcpu_num:           Number of virtual CPUs in the VM.
0147  * @flags:          Flags (ACRN_VM_FLAG_*) of the VM. This is VM
0148  *              flag management in HSM which is different
0149  *              from the &acrn_vm_creation.vm_flag.
0150  * @regions_mapping_lock:   Lock to protect &acrn_vm.regions_mapping and
0151  *              &acrn_vm.regions_mapping_count.
0152  * @regions_mapping:        Memory mappings of this VM.
0153  * @regions_mapping_count:  Number of memory mapping of this VM.
0154  * @ioreq_clients_lock:     Lock to protect ioreq_clients and default_client
0155  * @ioreq_clients:      The I/O request clients list of this VM
0156  * @default_client:     The default I/O request client
0157  * @ioreq_buf:          I/O request shared buffer
0158  * @ioreq_page:         The page of the I/O request shared buffer
0159  * @pci_conf_addr:      Address of a PCI configuration access emulation
0160  * @monitor_page:       Page of interrupt statistics of User VM
0161  * @ioeventfds_lock:        Lock to protect ioeventfds list
0162  * @ioeventfds:         List to link all hsm_ioeventfd
0163  * @ioeventfd_client:       I/O client for ioeventfds of the VM
0164  * @irqfds_lock:        Lock to protect irqfds list
0165  * @irqfds:         List to link all hsm_irqfd
0166  * @irqfd_wq:           Workqueue for irqfd async shutdown
0167  */
0168 struct acrn_vm {
0169     struct list_head        list;
0170     u16             vmid;
0171     int             vcpu_num;
0172     unsigned long           flags;
0173     struct mutex            regions_mapping_lock;
0174     struct vm_memory_mapping    regions_mapping[ACRN_MEM_MAPPING_MAX];
0175     int             regions_mapping_count;
0176     spinlock_t          ioreq_clients_lock;
0177     struct list_head        ioreq_clients;
0178     struct acrn_ioreq_client    *default_client;
0179     struct acrn_io_request_buffer   *ioreq_buf;
0180     struct page         *ioreq_page;
0181     u32             pci_conf_addr;
0182     struct page         *monitor_page;
0183     struct mutex            ioeventfds_lock;
0184     struct list_head        ioeventfds;
0185     struct acrn_ioreq_client    *ioeventfd_client;
0186     struct mutex            irqfds_lock;
0187     struct list_head        irqfds;
0188     struct workqueue_struct     *irqfd_wq;
0189 };
0190 
0191 struct acrn_vm *acrn_vm_create(struct acrn_vm *vm,
0192                    struct acrn_vm_creation *vm_param);
0193 int acrn_vm_destroy(struct acrn_vm *vm);
0194 int acrn_mm_region_add(struct acrn_vm *vm, u64 user_gpa, u64 service_gpa,
0195                u64 size, u32 mem_type, u32 mem_access_right);
0196 int acrn_mm_region_del(struct acrn_vm *vm, u64 user_gpa, u64 size);
0197 int acrn_vm_memseg_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap);
0198 int acrn_vm_memseg_unmap(struct acrn_vm *vm, struct acrn_vm_memmap *memmap);
0199 int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap);
0200 void acrn_vm_all_ram_unmap(struct acrn_vm *vm);
0201 
0202 int acrn_ioreq_init(struct acrn_vm *vm, u64 buf_vma);
0203 void acrn_ioreq_deinit(struct acrn_vm *vm);
0204 int acrn_ioreq_intr_setup(void);
0205 void acrn_ioreq_intr_remove(void);
0206 void acrn_ioreq_request_clear(struct acrn_vm *vm);
0207 int acrn_ioreq_client_wait(struct acrn_ioreq_client *client);
0208 int acrn_ioreq_request_default_complete(struct acrn_vm *vm, u16 vcpu);
0209 struct acrn_ioreq_client *acrn_ioreq_client_create(struct acrn_vm *vm,
0210                            ioreq_handler_t handler,
0211                            void *data, bool is_default,
0212                            const char *name);
0213 void acrn_ioreq_client_destroy(struct acrn_ioreq_client *client);
0214 int acrn_ioreq_range_add(struct acrn_ioreq_client *client,
0215              u32 type, u64 start, u64 end);
0216 void acrn_ioreq_range_del(struct acrn_ioreq_client *client,
0217               u32 type, u64 start, u64 end);
0218 
0219 int acrn_msi_inject(struct acrn_vm *vm, u64 msi_addr, u64 msi_data);
0220 
0221 int acrn_ioeventfd_init(struct acrn_vm *vm);
0222 int acrn_ioeventfd_config(struct acrn_vm *vm, struct acrn_ioeventfd *args);
0223 void acrn_ioeventfd_deinit(struct acrn_vm *vm);
0224 
0225 int acrn_irqfd_init(struct acrn_vm *vm);
0226 int acrn_irqfd_config(struct acrn_vm *vm, struct acrn_irqfd *args);
0227 void acrn_irqfd_deinit(struct acrn_vm *vm);
0228 
0229 #endif /* __ACRN_HSM_DRV_H */