19fbe302bSMichael S. Tsirkin /*
29fbe302bSMichael S. Tsirkin  * Virtio PCI driver
39fbe302bSMichael S. Tsirkin  *
49fbe302bSMichael S. Tsirkin  * This module allows virtio devices to be used over a virtual PCI device.
59fbe302bSMichael S. Tsirkin  * This can be used with QEMU based VMMs like KVM or Xen.
69fbe302bSMichael S. Tsirkin  *
79fbe302bSMichael S. Tsirkin  * Copyright IBM Corp. 2007
89fbe302bSMichael S. Tsirkin  *
99fbe302bSMichael S. Tsirkin  * Authors:
109fbe302bSMichael S. Tsirkin  *  Anthony Liguori  <aliguori@us.ibm.com>
119fbe302bSMichael S. Tsirkin  *
129fbe302bSMichael S. Tsirkin  * This header is BSD licensed so anyone can use the definitions to implement
139fbe302bSMichael S. Tsirkin  * compatible drivers/servers.
149fbe302bSMichael S. Tsirkin  *
159fbe302bSMichael S. Tsirkin  * Redistribution and use in source and binary forms, with or without
169fbe302bSMichael S. Tsirkin  * modification, are permitted provided that the following conditions
179fbe302bSMichael S. Tsirkin  * are met:
189fbe302bSMichael S. Tsirkin  * 1. Redistributions of source code must retain the above copyright
199fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer.
209fbe302bSMichael S. Tsirkin  * 2. Redistributions in binary form must reproduce the above copyright
219fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer in the
229fbe302bSMichael S. Tsirkin  *    documentation and/or other materials provided with the distribution.
239fbe302bSMichael S. Tsirkin  * 3. Neither the name of IBM nor the names of its contributors
249fbe302bSMichael S. Tsirkin  *    may be used to endorse or promote products derived from this software
259fbe302bSMichael S. Tsirkin  *    without specific prior written permission.
269fbe302bSMichael S. Tsirkin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
279fbe302bSMichael S. Tsirkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
289fbe302bSMichael S. Tsirkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
299fbe302bSMichael S. Tsirkin  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
309fbe302bSMichael S. Tsirkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
319fbe302bSMichael S. Tsirkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
329fbe302bSMichael S. Tsirkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
339fbe302bSMichael S. Tsirkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
349fbe302bSMichael S. Tsirkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
359fbe302bSMichael S. Tsirkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
369fbe302bSMichael S. Tsirkin  * SUCH DAMAGE.
379fbe302bSMichael S. Tsirkin  */
389fbe302bSMichael S. Tsirkin 
399fbe302bSMichael S. Tsirkin #ifndef _LINUX_VIRTIO_PCI_H
409fbe302bSMichael S. Tsirkin #define _LINUX_VIRTIO_PCI_H
419fbe302bSMichael S. Tsirkin 
429fbe302bSMichael S. Tsirkin #include "standard-headers/linux/types.h"
439fbe302bSMichael S. Tsirkin 
449fbe302bSMichael S. Tsirkin #ifndef VIRTIO_PCI_NO_LEGACY
459fbe302bSMichael S. Tsirkin 
469fbe302bSMichael S. Tsirkin /* A 32-bit r/o bitmask of the features supported by the host */
479fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_HOST_FEATURES	0
489fbe302bSMichael S. Tsirkin 
499fbe302bSMichael S. Tsirkin /* A 32-bit r/w bitmask of features activated by the guest */
509fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_GUEST_FEATURES	4
519fbe302bSMichael S. Tsirkin 
529fbe302bSMichael S. Tsirkin /* A 32-bit r/w PFN for the currently selected queue */
539fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_QUEUE_PFN		8
549fbe302bSMichael S. Tsirkin 
559fbe302bSMichael S. Tsirkin /* A 16-bit r/o queue size for the currently selected queue */
569fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_QUEUE_NUM		12
579fbe302bSMichael S. Tsirkin 
589fbe302bSMichael S. Tsirkin /* A 16-bit r/w queue selector */
599fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_QUEUE_SEL		14
609fbe302bSMichael S. Tsirkin 
619fbe302bSMichael S. Tsirkin /* A 16-bit r/w queue notifier */
629fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_QUEUE_NOTIFY		16
639fbe302bSMichael S. Tsirkin 
649fbe302bSMichael S. Tsirkin /* An 8-bit device status register.  */
659fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_STATUS		18
669fbe302bSMichael S. Tsirkin 
679fbe302bSMichael S. Tsirkin /* An 8-bit r/o interrupt status register.  Reading the value will return the
689fbe302bSMichael S. Tsirkin  * current contents of the ISR and will also clear it.  This is effectively
699fbe302bSMichael S. Tsirkin  * a read-and-acknowledge. */
709fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_ISR			19
719fbe302bSMichael S. Tsirkin 
729fbe302bSMichael S. Tsirkin /* MSI-X registers: only enabled if MSI-X is enabled. */
739fbe302bSMichael S. Tsirkin /* A 16-bit vector for configuration changes. */
749fbe302bSMichael S. Tsirkin #define VIRTIO_MSI_CONFIG_VECTOR        20
759fbe302bSMichael S. Tsirkin /* A 16-bit vector for selected queue notifications. */
769fbe302bSMichael S. Tsirkin #define VIRTIO_MSI_QUEUE_VECTOR         22
779fbe302bSMichael S. Tsirkin 
789fbe302bSMichael S. Tsirkin /* The remaining space is defined by each driver as the per-driver
799fbe302bSMichael S. Tsirkin  * configuration space */
809fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CONFIG_OFF(msix_enabled)	((msix_enabled) ? 24 : 20)
819fbe302bSMichael S. Tsirkin /* Deprecated: please use VIRTIO_PCI_CONFIG_OFF instead */
829fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CONFIG(dev)	VIRTIO_PCI_CONFIG_OFF((dev)->msix_enabled)
839fbe302bSMichael S. Tsirkin 
849fbe302bSMichael S. Tsirkin /* Virtio ABI version, this must match exactly */
859fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_ABI_VERSION		0
869fbe302bSMichael S. Tsirkin 
879fbe302bSMichael S. Tsirkin /* How many bits to shift physical queue address written to QUEUE_PFN.
889fbe302bSMichael S. Tsirkin  * 12 is historical, and due to x86 page size. */
899fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_QUEUE_ADDR_SHIFT	12
909fbe302bSMichael S. Tsirkin 
919fbe302bSMichael S. Tsirkin /* The alignment to use between consumer and producer parts of vring.
929fbe302bSMichael S. Tsirkin  * x86 pagesize again. */
939fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_VRING_ALIGN		4096
949fbe302bSMichael S. Tsirkin 
959fbe302bSMichael S. Tsirkin #endif /* VIRTIO_PCI_NO_LEGACY */
969fbe302bSMichael S. Tsirkin 
979fbe302bSMichael S. Tsirkin /* The bit of the ISR which indicates a device configuration change. */
989fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_ISR_CONFIG		0x2
999fbe302bSMichael S. Tsirkin /* Vector value used to disable MSI for queue */
1009fbe302bSMichael S. Tsirkin #define VIRTIO_MSI_NO_VECTOR            0xffff
1019fbe302bSMichael S. Tsirkin 
1029fbe302bSMichael S. Tsirkin #ifndef VIRTIO_PCI_NO_MODERN
1039fbe302bSMichael S. Tsirkin 
1049fbe302bSMichael S. Tsirkin /* IDs for different capabilities.  Must all exist. */
1059fbe302bSMichael S. Tsirkin 
1069fbe302bSMichael S. Tsirkin /* Common configuration */
1079fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_COMMON_CFG	1
1089fbe302bSMichael S. Tsirkin /* Notifications */
1099fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_NOTIFY_CFG	2
1109fbe302bSMichael S. Tsirkin /* ISR access */
1119fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_ISR_CFG		3
1129fbe302bSMichael S. Tsirkin /* Device specific configuration */
1139fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_DEVICE_CFG	4
1149fbe302bSMichael S. Tsirkin /* PCI configuration access */
1159fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_PCI_CFG		5
11653ba2eeeSMatthew Rosato /* Additional shared memory capability */
11753ba2eeeSMatthew Rosato #define VIRTIO_PCI_CAP_SHARED_MEMORY_CFG 8
1189fbe302bSMichael S. Tsirkin 
1199fbe302bSMichael S. Tsirkin /* This is the PCI capability header: */
1209fbe302bSMichael S. Tsirkin struct virtio_pci_cap {
1219fbe302bSMichael S. Tsirkin 	uint8_t cap_vndr;		/* Generic PCI field: PCI_CAP_ID_VNDR */
1229fbe302bSMichael S. Tsirkin 	uint8_t cap_next;		/* Generic PCI field: next ptr. */
1239fbe302bSMichael S. Tsirkin 	uint8_t cap_len;		/* Generic PCI field: capability length */
1249fbe302bSMichael S. Tsirkin 	uint8_t cfg_type;		/* Identifies the structure. */
1259fbe302bSMichael S. Tsirkin 	uint8_t bar;		/* Where to find it. */
12653ba2eeeSMatthew Rosato 	uint8_t id;		/* Multiple capabilities of the same type */
12753ba2eeeSMatthew Rosato 	uint8_t padding[2];	/* Pad to full dword. */
1289fbe302bSMichael S. Tsirkin 	uint32_t offset;		/* Offset within bar. */
1299fbe302bSMichael S. Tsirkin 	uint32_t length;		/* Length of the structure, in bytes. */
1309fbe302bSMichael S. Tsirkin };
1319fbe302bSMichael S. Tsirkin 
13253ba2eeeSMatthew Rosato struct virtio_pci_cap64 {
13353ba2eeeSMatthew Rosato 	struct virtio_pci_cap cap;
13453ba2eeeSMatthew Rosato 	uint32_t offset_hi;             /* Most sig 32 bits of offset */
13553ba2eeeSMatthew Rosato 	uint32_t length_hi;             /* Most sig 32 bits of length */
13653ba2eeeSMatthew Rosato };
13753ba2eeeSMatthew Rosato 
1389fbe302bSMichael S. Tsirkin struct virtio_pci_notify_cap {
1399fbe302bSMichael S. Tsirkin 	struct virtio_pci_cap cap;
1409fbe302bSMichael S. Tsirkin 	uint32_t notify_off_multiplier;	/* Multiplier for queue_notify_off. */
1419fbe302bSMichael S. Tsirkin };
1429fbe302bSMichael S. Tsirkin 
1439fbe302bSMichael S. Tsirkin /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
1449fbe302bSMichael S. Tsirkin struct virtio_pci_common_cfg {
1459fbe302bSMichael S. Tsirkin 	/* About the whole device. */
1469fbe302bSMichael S. Tsirkin 	uint32_t device_feature_select;	/* read-write */
1479fbe302bSMichael S. Tsirkin 	uint32_t device_feature;		/* read-only */
1489fbe302bSMichael S. Tsirkin 	uint32_t guest_feature_select;	/* read-write */
1499fbe302bSMichael S. Tsirkin 	uint32_t guest_feature;		/* read-write */
1509fbe302bSMichael S. Tsirkin 	uint16_t msix_config;		/* read-write */
1519fbe302bSMichael S. Tsirkin 	uint16_t num_queues;		/* read-only */
1529fbe302bSMichael S. Tsirkin 	uint8_t device_status;		/* read-write */
1539fbe302bSMichael S. Tsirkin 	uint8_t config_generation;		/* read-only */
1549fbe302bSMichael S. Tsirkin 
1559fbe302bSMichael S. Tsirkin 	/* About a specific virtqueue. */
1569fbe302bSMichael S. Tsirkin 	uint16_t queue_select;		/* read-write */
1579fbe302bSMichael S. Tsirkin 	uint16_t queue_size;		/* read-write, power of 2. */
1589fbe302bSMichael S. Tsirkin 	uint16_t queue_msix_vector;	/* read-write */
1599fbe302bSMichael S. Tsirkin 	uint16_t queue_enable;		/* read-write */
1609fbe302bSMichael S. Tsirkin 	uint16_t queue_notify_off;	/* read-only */
1619fbe302bSMichael S. Tsirkin 	uint32_t queue_desc_lo;		/* read-write */
1629fbe302bSMichael S. Tsirkin 	uint32_t queue_desc_hi;		/* read-write */
1639fbe302bSMichael S. Tsirkin 	uint32_t queue_avail_lo;		/* read-write */
1649fbe302bSMichael S. Tsirkin 	uint32_t queue_avail_hi;		/* read-write */
1659fbe302bSMichael S. Tsirkin 	uint32_t queue_used_lo;		/* read-write */
1669fbe302bSMichael S. Tsirkin 	uint32_t queue_used_hi;		/* read-write */
1679fbe302bSMichael S. Tsirkin };
1689fbe302bSMichael S. Tsirkin 
169*efb91426SDaniel Henrique Barboza /*
170*efb91426SDaniel Henrique Barboza  * Warning: do not use sizeof on this: use offsetofend for
171*efb91426SDaniel Henrique Barboza  * specific fields you need.
172*efb91426SDaniel Henrique Barboza  */
173*efb91426SDaniel Henrique Barboza struct virtio_pci_modern_common_cfg {
174*efb91426SDaniel Henrique Barboza 	struct virtio_pci_common_cfg cfg;
175*efb91426SDaniel Henrique Barboza 
176*efb91426SDaniel Henrique Barboza 	uint16_t queue_notify_data;	/* read-write */
177*efb91426SDaniel Henrique Barboza 	uint16_t queue_reset;		/* read-write */
178*efb91426SDaniel Henrique Barboza };
179*efb91426SDaniel Henrique Barboza 
180c36f24a2SMichael S. Tsirkin /* Fields in VIRTIO_PCI_CAP_PCI_CFG: */
181c36f24a2SMichael S. Tsirkin struct virtio_pci_cfg_cap {
182c36f24a2SMichael S. Tsirkin 	struct virtio_pci_cap cap;
183c36f24a2SMichael S. Tsirkin 	uint8_t pci_cfg_data[4]; /* Data for BAR access. */
184c36f24a2SMichael S. Tsirkin };
185c36f24a2SMichael S. Tsirkin 
1869fbe302bSMichael S. Tsirkin /* Macro versions of offsets for the Old Timers! */
1879fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_VNDR		0
1889fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_NEXT		1
1899fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_LEN		2
1909fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_CFG_TYPE		3
1919fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_BAR		4
1929fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_OFFSET		8
1939fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_CAP_LENGTH		12
1949fbe302bSMichael S. Tsirkin 
1959fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_NOTIFY_CAP_MULT	16
1969fbe302bSMichael S. Tsirkin 
1979fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_DFSELECT	0
1989fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_DF		4
1999fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_GFSELECT	8
2009fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_GF		12
2019fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_MSIX		16
2029fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_NUMQ		18
2039fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_STATUS	20
2049fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_CFGGENERATION	21
2059fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_SELECT	22
2069fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_SIZE	24
2079fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_MSIX	26
2089fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_ENABLE	28
2099fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_NOFF	30
2109fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_DESCLO	32
2119fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_DESCHI	36
2129fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_AVAILLO	40
2139fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_AVAILHI	44
2149fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_USEDLO	48
2159fbe302bSMichael S. Tsirkin #define VIRTIO_PCI_COMMON_Q_USEDHI	52
216d525f73fSChenyi Qiang #define VIRTIO_PCI_COMMON_Q_NDATA	56
217d525f73fSChenyi Qiang #define VIRTIO_PCI_COMMON_Q_RESET	58
2189fbe302bSMichael S. Tsirkin 
2199fbe302bSMichael S. Tsirkin #endif /* VIRTIO_PCI_NO_MODERN */
2209fbe302bSMichael S. Tsirkin 
2219fbe302bSMichael S. Tsirkin #endif
222