1*5f4c9760SMichael S. Tsirkin #ifndef _DRIVERS_VIRTIO_VIRTIO_PCI_COMMON_H 2*5f4c9760SMichael S. Tsirkin #define _DRIVERS_VIRTIO_VIRTIO_PCI_COMMON_H 3*5f4c9760SMichael S. Tsirkin /* 4*5f4c9760SMichael S. Tsirkin * Virtio PCI driver - APIs for common functionality for all device versions 5*5f4c9760SMichael S. Tsirkin * 6*5f4c9760SMichael S. Tsirkin * This module allows virtio devices to be used over a virtual PCI device. 7*5f4c9760SMichael S. Tsirkin * This can be used with QEMU based VMMs like KVM or Xen. 8*5f4c9760SMichael S. Tsirkin * 9*5f4c9760SMichael S. Tsirkin * Copyright IBM Corp. 2007 10*5f4c9760SMichael S. Tsirkin * Copyright Red Hat, Inc. 2014 11*5f4c9760SMichael S. Tsirkin * 12*5f4c9760SMichael S. Tsirkin * Authors: 13*5f4c9760SMichael S. Tsirkin * Anthony Liguori <aliguori@us.ibm.com> 14*5f4c9760SMichael S. Tsirkin * Rusty Russell <rusty@rustcorp.com.au> 15*5f4c9760SMichael S. Tsirkin * Michael S. Tsirkin <mst@redhat.com> 16*5f4c9760SMichael S. Tsirkin * 17*5f4c9760SMichael S. Tsirkin * This work is licensed under the terms of the GNU GPL, version 2 or later. 18*5f4c9760SMichael S. Tsirkin * See the COPYING file in the top-level directory. 19*5f4c9760SMichael S. Tsirkin * 20*5f4c9760SMichael S. Tsirkin */ 21*5f4c9760SMichael S. Tsirkin 22*5f4c9760SMichael S. Tsirkin #include <linux/module.h> 23*5f4c9760SMichael S. Tsirkin #include <linux/list.h> 24*5f4c9760SMichael S. Tsirkin #include <linux/pci.h> 25*5f4c9760SMichael S. Tsirkin #include <linux/slab.h> 26*5f4c9760SMichael S. Tsirkin #include <linux/interrupt.h> 27*5f4c9760SMichael S. Tsirkin #include <linux/virtio.h> 28*5f4c9760SMichael S. Tsirkin #include <linux/virtio_config.h> 29*5f4c9760SMichael S. Tsirkin #include <linux/virtio_ring.h> 30*5f4c9760SMichael S. Tsirkin #define VIRTIO_PCI_NO_LEGACY 31*5f4c9760SMichael S. Tsirkin #include <linux/virtio_pci.h> 32*5f4c9760SMichael S. Tsirkin #include <linux/highmem.h> 33*5f4c9760SMichael S. Tsirkin #include <linux/spinlock.h> 34*5f4c9760SMichael S. Tsirkin 35*5f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info { 36*5f4c9760SMichael S. Tsirkin /* the actual virtqueue */ 37*5f4c9760SMichael S. Tsirkin struct virtqueue *vq; 38*5f4c9760SMichael S. Tsirkin 39*5f4c9760SMichael S. Tsirkin /* the number of entries in the queue */ 40*5f4c9760SMichael S. Tsirkin int num; 41*5f4c9760SMichael S. Tsirkin 42*5f4c9760SMichael S. Tsirkin /* the virtual address of the ring queue */ 43*5f4c9760SMichael S. Tsirkin void *queue; 44*5f4c9760SMichael S. Tsirkin 45*5f4c9760SMichael S. Tsirkin /* the list node for the virtqueues list */ 46*5f4c9760SMichael S. Tsirkin struct list_head node; 47*5f4c9760SMichael S. Tsirkin 48*5f4c9760SMichael S. Tsirkin /* MSI-X vector (or none) */ 49*5f4c9760SMichael S. Tsirkin unsigned msix_vector; 50*5f4c9760SMichael S. Tsirkin }; 51*5f4c9760SMichael S. Tsirkin 52*5f4c9760SMichael S. Tsirkin /* Our device structure */ 53*5f4c9760SMichael S. Tsirkin struct virtio_pci_device { 54*5f4c9760SMichael S. Tsirkin struct virtio_device vdev; 55*5f4c9760SMichael S. Tsirkin struct pci_dev *pci_dev; 56*5f4c9760SMichael S. Tsirkin 57*5f4c9760SMichael S. Tsirkin /* the IO mapping for the PCI config space */ 58*5f4c9760SMichael S. Tsirkin void __iomem *ioaddr; 59*5f4c9760SMichael S. Tsirkin 60*5f4c9760SMichael S. Tsirkin /* the IO mapping for ISR operation */ 61*5f4c9760SMichael S. Tsirkin void __iomem *isr; 62*5f4c9760SMichael S. Tsirkin 63*5f4c9760SMichael S. Tsirkin /* a list of queues so we can dispatch IRQs */ 64*5f4c9760SMichael S. Tsirkin spinlock_t lock; 65*5f4c9760SMichael S. Tsirkin struct list_head virtqueues; 66*5f4c9760SMichael S. Tsirkin 67*5f4c9760SMichael S. Tsirkin /* array of all queues for house-keeping */ 68*5f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info **vqs; 69*5f4c9760SMichael S. Tsirkin 70*5f4c9760SMichael S. Tsirkin /* MSI-X support */ 71*5f4c9760SMichael S. Tsirkin int msix_enabled; 72*5f4c9760SMichael S. Tsirkin int intx_enabled; 73*5f4c9760SMichael S. Tsirkin struct msix_entry *msix_entries; 74*5f4c9760SMichael S. Tsirkin cpumask_var_t *msix_affinity_masks; 75*5f4c9760SMichael S. Tsirkin /* Name strings for interrupts. This size should be enough, 76*5f4c9760SMichael S. Tsirkin * and I'm too lazy to allocate each name separately. */ 77*5f4c9760SMichael S. Tsirkin char (*msix_names)[256]; 78*5f4c9760SMichael S. Tsirkin /* Number of available vectors */ 79*5f4c9760SMichael S. Tsirkin unsigned msix_vectors; 80*5f4c9760SMichael S. Tsirkin /* Vectors allocated, excluding per-vq vectors if any */ 81*5f4c9760SMichael S. Tsirkin unsigned msix_used_vectors; 82*5f4c9760SMichael S. Tsirkin 83*5f4c9760SMichael S. Tsirkin /* Whether we have vector per vq */ 84*5f4c9760SMichael S. Tsirkin bool per_vq_vectors; 85*5f4c9760SMichael S. Tsirkin 86*5f4c9760SMichael S. Tsirkin struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev, 87*5f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info *info, 88*5f4c9760SMichael S. Tsirkin unsigned idx, 89*5f4c9760SMichael S. Tsirkin void (*callback)(struct virtqueue *vq), 90*5f4c9760SMichael S. Tsirkin const char *name, 91*5f4c9760SMichael S. Tsirkin u16 msix_vec); 92*5f4c9760SMichael S. Tsirkin void (*del_vq)(struct virtio_pci_vq_info *info); 93*5f4c9760SMichael S. Tsirkin 94*5f4c9760SMichael S. Tsirkin u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector); 95*5f4c9760SMichael S. Tsirkin }; 96*5f4c9760SMichael S. Tsirkin 97*5f4c9760SMichael S. Tsirkin /* Constants for MSI-X */ 98*5f4c9760SMichael S. Tsirkin /* Use first vector for configuration changes, second and the rest for 99*5f4c9760SMichael S. Tsirkin * virtqueues Thus, we need at least 2 vectors for MSI. */ 100*5f4c9760SMichael S. Tsirkin enum { 101*5f4c9760SMichael S. Tsirkin VP_MSIX_CONFIG_VECTOR = 0, 102*5f4c9760SMichael S. Tsirkin VP_MSIX_VQ_VECTOR = 1, 103*5f4c9760SMichael S. Tsirkin }; 104*5f4c9760SMichael S. Tsirkin 105*5f4c9760SMichael S. Tsirkin /* Convert a generic virtio device to our structure */ 106*5f4c9760SMichael S. Tsirkin static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev) 107*5f4c9760SMichael S. Tsirkin { 108*5f4c9760SMichael S. Tsirkin return container_of(vdev, struct virtio_pci_device, vdev); 109*5f4c9760SMichael S. Tsirkin } 110*5f4c9760SMichael S. Tsirkin 111*5f4c9760SMichael S. Tsirkin /* wait for pending irq handlers */ 112*5f4c9760SMichael S. Tsirkin void vp_synchronize_vectors(struct virtio_device *vdev); 113*5f4c9760SMichael S. Tsirkin /* the notify function used when creating a virt queue */ 114*5f4c9760SMichael S. Tsirkin bool vp_notify(struct virtqueue *vq); 115*5f4c9760SMichael S. Tsirkin /* the config->del_vqs() implementation */ 116*5f4c9760SMichael S. Tsirkin void vp_del_vqs(struct virtio_device *vdev); 117*5f4c9760SMichael S. Tsirkin /* the config->find_vqs() implementation */ 118*5f4c9760SMichael S. Tsirkin int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, 119*5f4c9760SMichael S. Tsirkin struct virtqueue *vqs[], 120*5f4c9760SMichael S. Tsirkin vq_callback_t *callbacks[], 121*5f4c9760SMichael S. Tsirkin const char *names[]); 122*5f4c9760SMichael S. Tsirkin const char *vp_bus_name(struct virtio_device *vdev); 123*5f4c9760SMichael S. Tsirkin 124*5f4c9760SMichael S. Tsirkin /* Setup the affinity for a virtqueue: 125*5f4c9760SMichael S. Tsirkin * - force the affinity for per vq vector 126*5f4c9760SMichael S. Tsirkin * - OR over all affinities for shared MSI 127*5f4c9760SMichael S. Tsirkin * - ignore the affinity request if we're using INTX 128*5f4c9760SMichael S. Tsirkin */ 129*5f4c9760SMichael S. Tsirkin int vp_set_vq_affinity(struct virtqueue *vq, int cpu); 130*5f4c9760SMichael S. Tsirkin void virtio_pci_release_dev(struct device *); 131*5f4c9760SMichael S. Tsirkin 132*5f4c9760SMichael S. Tsirkin #ifdef CONFIG_PM_SLEEP 133*5f4c9760SMichael S. Tsirkin extern const struct dev_pm_ops virtio_pci_pm_ops; 134*5f4c9760SMichael S. Tsirkin #endif 135*5f4c9760SMichael S. Tsirkin 136*5f4c9760SMichael S. Tsirkin #endif 137