xref: /linux/drivers/virtio/virtio_pci_common.h (revision 59a5b0f7bf74f88da6670bcbf924d8cc1e75b1ee)
15f4c9760SMichael S. Tsirkin #ifndef _DRIVERS_VIRTIO_VIRTIO_PCI_COMMON_H
25f4c9760SMichael S. Tsirkin #define _DRIVERS_VIRTIO_VIRTIO_PCI_COMMON_H
35f4c9760SMichael S. Tsirkin /*
45f4c9760SMichael S. Tsirkin  * Virtio PCI driver - APIs for common functionality for all device versions
55f4c9760SMichael S. Tsirkin  *
65f4c9760SMichael S. Tsirkin  * This module allows virtio devices to be used over a virtual PCI device.
75f4c9760SMichael S. Tsirkin  * This can be used with QEMU based VMMs like KVM or Xen.
85f4c9760SMichael S. Tsirkin  *
95f4c9760SMichael S. Tsirkin  * Copyright IBM Corp. 2007
105f4c9760SMichael S. Tsirkin  * Copyright Red Hat, Inc. 2014
115f4c9760SMichael S. Tsirkin  *
125f4c9760SMichael S. Tsirkin  * Authors:
135f4c9760SMichael S. Tsirkin  *  Anthony Liguori  <aliguori@us.ibm.com>
145f4c9760SMichael S. Tsirkin  *  Rusty Russell <rusty@rustcorp.com.au>
155f4c9760SMichael S. Tsirkin  *  Michael S. Tsirkin <mst@redhat.com>
165f4c9760SMichael S. Tsirkin  *
175f4c9760SMichael S. Tsirkin  * This work is licensed under the terms of the GNU GPL, version 2 or later.
185f4c9760SMichael S. Tsirkin  * See the COPYING file in the top-level directory.
195f4c9760SMichael S. Tsirkin  *
205f4c9760SMichael S. Tsirkin  */
215f4c9760SMichael S. Tsirkin 
225f4c9760SMichael S. Tsirkin #include <linux/module.h>
235f4c9760SMichael S. Tsirkin #include <linux/list.h>
245f4c9760SMichael S. Tsirkin #include <linux/pci.h>
255f4c9760SMichael S. Tsirkin #include <linux/slab.h>
265f4c9760SMichael S. Tsirkin #include <linux/interrupt.h>
275f4c9760SMichael S. Tsirkin #include <linux/virtio.h>
285f4c9760SMichael S. Tsirkin #include <linux/virtio_config.h>
295f4c9760SMichael S. Tsirkin #include <linux/virtio_ring.h>
305f4c9760SMichael S. Tsirkin #include <linux/virtio_pci.h>
315f4c9760SMichael S. Tsirkin #include <linux/highmem.h>
325f4c9760SMichael S. Tsirkin #include <linux/spinlock.h>
335f4c9760SMichael S. Tsirkin 
345f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info {
355f4c9760SMichael S. Tsirkin 	/* the actual virtqueue */
365f4c9760SMichael S. Tsirkin 	struct virtqueue *vq;
375f4c9760SMichael S. Tsirkin 
385f4c9760SMichael S. Tsirkin 	/* the number of entries in the queue */
395f4c9760SMichael S. Tsirkin 	int num;
405f4c9760SMichael S. Tsirkin 
415f4c9760SMichael S. Tsirkin 	/* the virtual address of the ring queue */
425f4c9760SMichael S. Tsirkin 	void *queue;
435f4c9760SMichael S. Tsirkin 
445f4c9760SMichael S. Tsirkin 	/* the list node for the virtqueues list */
455f4c9760SMichael S. Tsirkin 	struct list_head node;
465f4c9760SMichael S. Tsirkin 
475f4c9760SMichael S. Tsirkin 	/* MSI-X vector (or none) */
485f4c9760SMichael S. Tsirkin 	unsigned msix_vector;
495f4c9760SMichael S. Tsirkin };
505f4c9760SMichael S. Tsirkin 
515f4c9760SMichael S. Tsirkin /* Our device structure */
525f4c9760SMichael S. Tsirkin struct virtio_pci_device {
535f4c9760SMichael S. Tsirkin 	struct virtio_device vdev;
545f4c9760SMichael S. Tsirkin 	struct pci_dev *pci_dev;
555f4c9760SMichael S. Tsirkin 
561fcf0512SMichael S. Tsirkin 	/* In legacy mode, these two point to within ->legacy. */
571fcf0512SMichael S. Tsirkin 	/* Where to read and clear interrupt */
581fcf0512SMichael S. Tsirkin 	u8 __iomem *isr;
591fcf0512SMichael S. Tsirkin 
601fcf0512SMichael S. Tsirkin 	/* Modern only fields */
611fcf0512SMichael S. Tsirkin 	/* The IO mapping for the PCI config space (non-legacy mode) */
621fcf0512SMichael S. Tsirkin 	struct virtio_pci_common_cfg __iomem *common;
631fcf0512SMichael S. Tsirkin 	/* Device-specific data (non-legacy mode)  */
641fcf0512SMichael S. Tsirkin 	void __iomem *device;
653909213cSMichael S. Tsirkin 	/* Base of vq notifications (non-legacy mode). */
663909213cSMichael S. Tsirkin 	void __iomem *notify_base;
671fcf0512SMichael S. Tsirkin 
681fcf0512SMichael S. Tsirkin 	/* So we can sanity-check accesses. */
693909213cSMichael S. Tsirkin 	size_t notify_len;
701fcf0512SMichael S. Tsirkin 	size_t device_len;
711fcf0512SMichael S. Tsirkin 
721fcf0512SMichael S. Tsirkin 	/* Capability for when we need to map notifications per-vq. */
731fcf0512SMichael S. Tsirkin 	int notify_map_cap;
741fcf0512SMichael S. Tsirkin 
751fcf0512SMichael S. Tsirkin 	/* Multiply queue_notify_off by this value. (non-legacy mode). */
761fcf0512SMichael S. Tsirkin 	u32 notify_offset_multiplier;
771fcf0512SMichael S. Tsirkin 
78*59a5b0f7SGerd Hoffmann 	int modern_bars;
79*59a5b0f7SGerd Hoffmann 
801fcf0512SMichael S. Tsirkin 	/* Legacy only field */
815f4c9760SMichael S. Tsirkin 	/* the IO mapping for the PCI config space */
825f4c9760SMichael S. Tsirkin 	void __iomem *ioaddr;
835f4c9760SMichael S. Tsirkin 
845f4c9760SMichael S. Tsirkin 	/* a list of queues so we can dispatch IRQs */
855f4c9760SMichael S. Tsirkin 	spinlock_t lock;
865f4c9760SMichael S. Tsirkin 	struct list_head virtqueues;
875f4c9760SMichael S. Tsirkin 
885f4c9760SMichael S. Tsirkin 	/* array of all queues for house-keeping */
895f4c9760SMichael S. Tsirkin 	struct virtio_pci_vq_info **vqs;
905f4c9760SMichael S. Tsirkin 
915f4c9760SMichael S. Tsirkin 	/* MSI-X support */
925f4c9760SMichael S. Tsirkin 	int msix_enabled;
935f4c9760SMichael S. Tsirkin 	int intx_enabled;
945f4c9760SMichael S. Tsirkin 	struct msix_entry *msix_entries;
955f4c9760SMichael S. Tsirkin 	cpumask_var_t *msix_affinity_masks;
965f4c9760SMichael S. Tsirkin 	/* Name strings for interrupts. This size should be enough,
975f4c9760SMichael S. Tsirkin 	 * and I'm too lazy to allocate each name separately. */
985f4c9760SMichael S. Tsirkin 	char (*msix_names)[256];
995f4c9760SMichael S. Tsirkin 	/* Number of available vectors */
1005f4c9760SMichael S. Tsirkin 	unsigned msix_vectors;
1015f4c9760SMichael S. Tsirkin 	/* Vectors allocated, excluding per-vq vectors if any */
1025f4c9760SMichael S. Tsirkin 	unsigned msix_used_vectors;
1035f4c9760SMichael S. Tsirkin 
1045f4c9760SMichael S. Tsirkin 	/* Whether we have vector per vq */
1055f4c9760SMichael S. Tsirkin 	bool per_vq_vectors;
1065f4c9760SMichael S. Tsirkin 
1075f4c9760SMichael S. Tsirkin 	struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev,
1085f4c9760SMichael S. Tsirkin 				      struct virtio_pci_vq_info *info,
1095f4c9760SMichael S. Tsirkin 				      unsigned idx,
1105f4c9760SMichael S. Tsirkin 				      void (*callback)(struct virtqueue *vq),
1115f4c9760SMichael S. Tsirkin 				      const char *name,
1125f4c9760SMichael S. Tsirkin 				      u16 msix_vec);
1135f4c9760SMichael S. Tsirkin 	void (*del_vq)(struct virtio_pci_vq_info *info);
1145f4c9760SMichael S. Tsirkin 
1155f4c9760SMichael S. Tsirkin 	u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector);
1165f4c9760SMichael S. Tsirkin };
1175f4c9760SMichael S. Tsirkin 
1185f4c9760SMichael S. Tsirkin /* Constants for MSI-X */
1195f4c9760SMichael S. Tsirkin /* Use first vector for configuration changes, second and the rest for
1205f4c9760SMichael S. Tsirkin  * virtqueues Thus, we need at least 2 vectors for MSI. */
1215f4c9760SMichael S. Tsirkin enum {
1225f4c9760SMichael S. Tsirkin 	VP_MSIX_CONFIG_VECTOR = 0,
1235f4c9760SMichael S. Tsirkin 	VP_MSIX_VQ_VECTOR = 1,
1245f4c9760SMichael S. Tsirkin };
1255f4c9760SMichael S. Tsirkin 
1265f4c9760SMichael S. Tsirkin /* Convert a generic virtio device to our structure */
1275f4c9760SMichael S. Tsirkin static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
1285f4c9760SMichael S. Tsirkin {
1295f4c9760SMichael S. Tsirkin 	return container_of(vdev, struct virtio_pci_device, vdev);
1305f4c9760SMichael S. Tsirkin }
1315f4c9760SMichael S. Tsirkin 
1325f4c9760SMichael S. Tsirkin /* wait for pending irq handlers */
1335f4c9760SMichael S. Tsirkin void vp_synchronize_vectors(struct virtio_device *vdev);
1345f4c9760SMichael S. Tsirkin /* the notify function used when creating a virt queue */
1355f4c9760SMichael S. Tsirkin bool vp_notify(struct virtqueue *vq);
1365f4c9760SMichael S. Tsirkin /* the config->del_vqs() implementation */
1375f4c9760SMichael S. Tsirkin void vp_del_vqs(struct virtio_device *vdev);
1385f4c9760SMichael S. Tsirkin /* the config->find_vqs() implementation */
1395f4c9760SMichael S. Tsirkin int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
1405f4c9760SMichael S. Tsirkin 		       struct virtqueue *vqs[],
1415f4c9760SMichael S. Tsirkin 		       vq_callback_t *callbacks[],
1425f4c9760SMichael S. Tsirkin 		       const char *names[]);
1435f4c9760SMichael S. Tsirkin const char *vp_bus_name(struct virtio_device *vdev);
1445f4c9760SMichael S. Tsirkin 
1455f4c9760SMichael S. Tsirkin /* Setup the affinity for a virtqueue:
1465f4c9760SMichael S. Tsirkin  * - force the affinity for per vq vector
1475f4c9760SMichael S. Tsirkin  * - OR over all affinities for shared MSI
1485f4c9760SMichael S. Tsirkin  * - ignore the affinity request if we're using INTX
1495f4c9760SMichael S. Tsirkin  */
1505f4c9760SMichael S. Tsirkin int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
1515f4c9760SMichael S. Tsirkin 
15246506da5SMichael S. Tsirkin #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
153ff31d2e2SMichael S. Tsirkin int virtio_pci_legacy_probe(struct virtio_pci_device *);
154ff31d2e2SMichael S. Tsirkin void virtio_pci_legacy_remove(struct virtio_pci_device *);
15546506da5SMichael S. Tsirkin #else
15646506da5SMichael S. Tsirkin static inline int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
15746506da5SMichael S. Tsirkin {
15846506da5SMichael S. Tsirkin 	return -ENODEV;
15946506da5SMichael S. Tsirkin }
16046506da5SMichael S. Tsirkin static inline void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev)
16146506da5SMichael S. Tsirkin {
16246506da5SMichael S. Tsirkin }
16346506da5SMichael S. Tsirkin #endif
1641fcf0512SMichael S. Tsirkin int virtio_pci_modern_probe(struct virtio_pci_device *);
1651fcf0512SMichael S. Tsirkin void virtio_pci_modern_remove(struct virtio_pci_device *);
1665f4c9760SMichael S. Tsirkin 
1675f4c9760SMichael S. Tsirkin #endif
168