136f5dc91SSasha Levin #ifndef KVM__VIRTIO_PCI_H 236f5dc91SSasha Levin #define KVM__VIRTIO_PCI_H 336f5dc91SSasha Levin 421ff329dSWill Deacon #include "kvm/devices.h" 536f5dc91SSasha Levin #include "kvm/pci.h" 636f5dc91SSasha Levin 7*930876d5SJean-Philippe Brucker #include <stdbool.h> 8*930876d5SJean-Philippe Brucker #include <linux/byteorder.h> 936f5dc91SSasha Levin #include <linux/types.h> 1036f5dc91SSasha Levin 1145d3b59eSSasha Levin #define VIRTIO_PCI_MAX_VQ 32 1214bba8a0SAsias He #define VIRTIO_PCI_MAX_CONFIG 1 1336f5dc91SSasha Levin 1436f5dc91SSasha Levin struct kvm; 15*930876d5SJean-Philippe Brucker struct kvm_cpu; 1636f5dc91SSasha Levin 171599d724SSasha Levin struct virtio_pci_ioevent_param { 1802eca50cSAsias He struct virtio_device *vdev; 191599d724SSasha Levin u32 vq; 201599d724SSasha Levin }; 211599d724SSasha Levin 2243c81c74SSasha Levin #define VIRTIO_PCI_F_SIGNAL_MSI (1 << 0) 2343c81c74SSasha Levin 24*930876d5SJean-Philippe Brucker #define ALIGN_UP(x, s) ALIGN((x) + (s) - 1, (s)) 25*930876d5SJean-Philippe Brucker #define VIRTIO_NR_MSIX (VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG) 26*930876d5SJean-Philippe Brucker #define VIRTIO_MSIX_TABLE_SIZE (VIRTIO_NR_MSIX * 16) 27*930876d5SJean-Philippe Brucker #define VIRTIO_MSIX_PBA_SIZE (ALIGN_UP(VIRTIO_MSIX_TABLE_SIZE, 64) / 8) 28*930876d5SJean-Philippe Brucker #define VIRTIO_MSIX_BAR_SIZE (1UL << fls_long(VIRTIO_MSIX_TABLE_SIZE + \ 29*930876d5SJean-Philippe Brucker VIRTIO_MSIX_PBA_SIZE)) 30*930876d5SJean-Philippe Brucker 3136f5dc91SSasha Levin struct virtio_pci { 3236f5dc91SSasha Levin struct pci_device_header pci_hdr; 3321ff329dSWill Deacon struct device_header dev_hdr; 3436f5dc91SSasha Levin void *dev; 35a463650cSWill Deacon struct kvm *kvm; 3636f5dc91SSasha Levin 3721c9bc74SJean-Philippe Brucker u32 doorbell_offset; 3836f5dc91SSasha Levin u8 status; 3936f5dc91SSasha Levin u8 isr; 4043c81c74SSasha Levin u32 features; 4136f5dc91SSasha Levin 42e9922aafSAndre Przywara /* 43e9922aafSAndre Przywara * We cannot rely on the INTERRUPT_LINE byte in the config space once 44e9922aafSAndre Przywara * we have run guest code, as the OS is allowed to use that field 45e9922aafSAndre Przywara * as a scratch pad to communicate between driver and PCI layer. 46e9922aafSAndre Przywara * So store our legacy interrupt line number in here for internal use. 47e9922aafSAndre Przywara */ 48e9922aafSAndre Przywara u8 legacy_irq_line; 49e9922aafSAndre Przywara 5036f5dc91SSasha Levin /* MSI-X */ 5136f5dc91SSasha Levin u16 config_vector; 5236f5dc91SSasha Levin u32 config_gsi; 5336f5dc91SSasha Levin u32 vq_vector[VIRTIO_PCI_MAX_VQ]; 5436f5dc91SSasha Levin u32 gsis[VIRTIO_PCI_MAX_VQ]; 5506f48103SSasha Levin u64 msix_pba; 5614bba8a0SAsias He struct msix_table msix_table[VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG]; 5736f5dc91SSasha Levin 5836f5dc91SSasha Levin /* virtio queue */ 5936f5dc91SSasha Levin u16 queue_selector; 601599d724SSasha Levin struct virtio_pci_ioevent_param ioeventfds[VIRTIO_PCI_MAX_VQ]; 6136f5dc91SSasha Levin }; 6236f5dc91SSasha Levin 6302eca50cSAsias He int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq); 6402eca50cSAsias He int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev); 6502eca50cSAsias He int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev); 66eb34a8c2SJean-Philippe Brucker int virtio_pci__reset(struct kvm *kvm, struct virtio_device *vdev); 6702eca50cSAsias He int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev, 68507e02d8SAsias He int device_id, int subsys_id, int class); 6936f5dc91SSasha Levin 70*930876d5SJean-Philippe Brucker static inline bool virtio_pci__msix_enabled(struct virtio_pci *vpci) 71*930876d5SJean-Philippe Brucker { 72*930876d5SJean-Philippe Brucker return vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_ENABLE); 73*930876d5SJean-Philippe Brucker } 74*930876d5SJean-Philippe Brucker 75*930876d5SJean-Philippe Brucker static inline u16 virtio_pci__port_addr(struct virtio_pci *vpci) 76*930876d5SJean-Philippe Brucker { 77*930876d5SJean-Philippe Brucker return pci__bar_address(&vpci->pci_hdr, 0); 78*930876d5SJean-Philippe Brucker } 79*930876d5SJean-Philippe Brucker 80*930876d5SJean-Philippe Brucker static inline u32 virtio_pci__mmio_addr(struct virtio_pci *vpci) 81*930876d5SJean-Philippe Brucker { 82*930876d5SJean-Philippe Brucker return pci__bar_address(&vpci->pci_hdr, 1); 83*930876d5SJean-Philippe Brucker } 84*930876d5SJean-Philippe Brucker 85*930876d5SJean-Philippe Brucker static inline u32 virtio_pci__msix_io_addr(struct virtio_pci *vpci) 86*930876d5SJean-Philippe Brucker { 87*930876d5SJean-Philippe Brucker return pci__bar_address(&vpci->pci_hdr, 2); 88*930876d5SJean-Philippe Brucker } 89*930876d5SJean-Philippe Brucker 90*930876d5SJean-Philippe Brucker int virtio_pci__add_msix_route(struct virtio_pci *vpci, u32 vec); 91*930876d5SJean-Philippe Brucker int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_device *vdev, 92*930876d5SJean-Philippe Brucker u32 vq); 93*930876d5SJean-Philippe Brucker int virtio_pci_init_vq(struct kvm *kvm, struct virtio_device *vdev, int vq); 94*930876d5SJean-Philippe Brucker void virtio_pci_exit_vq(struct kvm *kvm, struct virtio_device *vdev, int vq); 95*930876d5SJean-Philippe Brucker 96*930876d5SJean-Philippe Brucker void virtio_pci_legacy__io_mmio_callback(struct kvm_cpu *vcpu, u64 addr, u8 *data, 97*930876d5SJean-Philippe Brucker u32 len, u8 is_write, void *ptr); 98*930876d5SJean-Philippe Brucker 9936f5dc91SSasha Levin #endif 100