1 #ifndef KVM__VIRTIO_PCI_H 2 #define KVM__VIRTIO_PCI_H 3 4 #include "kvm/devices.h" 5 #include "kvm/pci.h" 6 7 #include <stdbool.h> 8 #include <linux/byteorder.h> 9 #include <linux/types.h> 10 11 #define VIRTIO_PCI_MAX_VQ 32 12 #define VIRTIO_PCI_MAX_CONFIG 1 13 14 struct kvm; 15 struct kvm_cpu; 16 17 struct virtio_pci_ioevent_param { 18 struct virtio_device *vdev; 19 u32 vq; 20 }; 21 22 #define VIRTIO_PCI_F_SIGNAL_MSI (1 << 0) 23 24 #define ALIGN_UP(x, s) ALIGN((x) + (s) - 1, (s)) 25 #define VIRTIO_NR_MSIX (VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG) 26 #define VIRTIO_MSIX_TABLE_SIZE (VIRTIO_NR_MSIX * 16) 27 #define VIRTIO_MSIX_PBA_SIZE (ALIGN_UP(VIRTIO_MSIX_TABLE_SIZE, 64) / 8) 28 #define VIRTIO_MSIX_BAR_SIZE (1UL << fls_long(VIRTIO_MSIX_TABLE_SIZE + \ 29 VIRTIO_MSIX_PBA_SIZE)) 30 31 struct virtio_pci { 32 struct pci_device_header pci_hdr; 33 struct device_header dev_hdr; 34 void *dev; 35 struct kvm *kvm; 36 37 u32 doorbell_offset; 38 u8 status; 39 u8 isr; 40 u32 features; 41 42 /* 43 * We cannot rely on the INTERRUPT_LINE byte in the config space once 44 * we have run guest code, as the OS is allowed to use that field 45 * as a scratch pad to communicate between driver and PCI layer. 46 * So store our legacy interrupt line number in here for internal use. 47 */ 48 u8 legacy_irq_line; 49 50 /* MSI-X */ 51 u16 config_vector; 52 u32 config_gsi; 53 u32 vq_vector[VIRTIO_PCI_MAX_VQ]; 54 u32 gsis[VIRTIO_PCI_MAX_VQ]; 55 u64 msix_pba; 56 struct msix_table msix_table[VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG]; 57 58 /* virtio queue */ 59 u16 queue_selector; 60 struct virtio_pci_ioevent_param ioeventfds[VIRTIO_PCI_MAX_VQ]; 61 }; 62 63 int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq); 64 int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev); 65 int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev); 66 int virtio_pci__reset(struct kvm *kvm, struct virtio_device *vdev); 67 int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev, 68 int device_id, int subsys_id, int class); 69 70 static inline bool virtio_pci__msix_enabled(struct virtio_pci *vpci) 71 { 72 return vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_ENABLE); 73 } 74 75 static inline u16 virtio_pci__port_addr(struct virtio_pci *vpci) 76 { 77 return pci__bar_address(&vpci->pci_hdr, 0); 78 } 79 80 static inline u32 virtio_pci__mmio_addr(struct virtio_pci *vpci) 81 { 82 return pci__bar_address(&vpci->pci_hdr, 1); 83 } 84 85 static inline u32 virtio_pci__msix_io_addr(struct virtio_pci *vpci) 86 { 87 return pci__bar_address(&vpci->pci_hdr, 2); 88 } 89 90 int virtio_pci__add_msix_route(struct virtio_pci *vpci, u32 vec); 91 int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_device *vdev, 92 u32 vq); 93 int virtio_pci_init_vq(struct kvm *kvm, struct virtio_device *vdev, int vq); 94 void virtio_pci_exit_vq(struct kvm *kvm, struct virtio_device *vdev, int vq); 95 96 void virtio_pci_legacy__io_mmio_callback(struct kvm_cpu *vcpu, u64 addr, u8 *data, 97 u32 len, u8 is_write, void *ptr); 98 99 #endif 100