Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 1 | #ifndef _VIRTIO_PCI_H |
| 2 | #define _VIRTIO_PCI_H |
| 3 | |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 4 | #include "x86.h" // inl |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 5 | #include "biosvar.h" // GET_LOWFLAT |
Kevin O'Connor | 7d09d0e | 2010-05-10 21:51:38 -0400 | [diff] [blame] | 6 | |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 7 | /* The bit of the ISR which indicates a device configuration change. */ |
| 8 | #define VIRTIO_PCI_ISR_CONFIG 0x2 |
| 9 | |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 10 | /* Virtio ABI version, this must match exactly */ |
| 11 | #define VIRTIO_PCI_ABI_VERSION 0 |
| 12 | |
Gerd Hoffmann | d00335e | 2015-06-30 08:42:50 +0200 | [diff] [blame] | 13 | /* --- virtio 0.9.5 (legacy) struct --------------------------------- */ |
| 14 | |
| 15 | typedef struct virtio_pci_legacy { |
| 16 | u32 host_features; |
| 17 | u32 guest_features; |
| 18 | u32 queue_pfn; |
| 19 | u16 queue_num; |
| 20 | u16 queue_sel; |
| 21 | u16 queue_notify; |
| 22 | u8 status; |
| 23 | u8 isr; |
| 24 | u8 device[]; |
| 25 | } virtio_pci_legacy; |
| 26 | |
Gerd Hoffmann | 90ac3d4 | 2015-06-25 16:13:51 +0200 | [diff] [blame] | 27 | /* --- virtio 1.0 (modern) structs ---------------------------------- */ |
| 28 | |
| 29 | /* Common configuration */ |
| 30 | #define VIRTIO_PCI_CAP_COMMON_CFG 1 |
| 31 | /* Notifications */ |
| 32 | #define VIRTIO_PCI_CAP_NOTIFY_CFG 2 |
| 33 | /* ISR access */ |
| 34 | #define VIRTIO_PCI_CAP_ISR_CFG 3 |
| 35 | /* Device specific configuration */ |
| 36 | #define VIRTIO_PCI_CAP_DEVICE_CFG 4 |
| 37 | /* PCI configuration access */ |
| 38 | #define VIRTIO_PCI_CAP_PCI_CFG 5 |
| 39 | |
| 40 | /* This is the PCI capability header: */ |
| 41 | struct virtio_pci_cap { |
| 42 | u8 cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */ |
| 43 | u8 cap_next; /* Generic PCI field: next ptr. */ |
| 44 | u8 cap_len; /* Generic PCI field: capability length */ |
| 45 | u8 cfg_type; /* Identifies the structure. */ |
| 46 | u8 bar; /* Where to find it. */ |
| 47 | u8 padding[3]; /* Pad to full dword. */ |
| 48 | u32 offset; /* Offset within bar. */ |
| 49 | u32 length; /* Length of the structure, in bytes. */ |
| 50 | }; |
| 51 | |
| 52 | struct virtio_pci_notify_cap { |
| 53 | struct virtio_pci_cap cap; |
| 54 | u32 notify_off_multiplier; /* Multiplier for queue_notify_off. */ |
| 55 | }; |
| 56 | |
Gerd Hoffmann | 0e21548 | 2015-07-03 11:07:05 +0200 | [diff] [blame] | 57 | struct virtio_pci_cfg_cap { |
| 58 | struct virtio_pci_cap cap; |
| 59 | u8 pci_cfg_data[4]; /* Data for BAR access. */ |
| 60 | }; |
| 61 | |
Gerd Hoffmann | 90ac3d4 | 2015-06-25 16:13:51 +0200 | [diff] [blame] | 62 | typedef struct virtio_pci_common_cfg { |
| 63 | /* About the whole device. */ |
| 64 | u32 device_feature_select; /* read-write */ |
| 65 | u32 device_feature; /* read-only */ |
| 66 | u32 guest_feature_select; /* read-write */ |
| 67 | u32 guest_feature; /* read-write */ |
| 68 | u16 msix_config; /* read-write */ |
| 69 | u16 num_queues; /* read-only */ |
| 70 | u8 device_status; /* read-write */ |
| 71 | u8 config_generation; /* read-only */ |
| 72 | |
| 73 | /* About a specific virtqueue. */ |
| 74 | u16 queue_select; /* read-write */ |
| 75 | u16 queue_size; /* read-write, power of 2. */ |
| 76 | u16 queue_msix_vector; /* read-write */ |
| 77 | u16 queue_enable; /* read-write */ |
| 78 | u16 queue_notify_off; /* read-only */ |
| 79 | u32 queue_desc_lo; /* read-write */ |
| 80 | u32 queue_desc_hi; /* read-write */ |
| 81 | u32 queue_avail_lo; /* read-write */ |
| 82 | u32 queue_avail_hi; /* read-write */ |
| 83 | u32 queue_used_lo; /* read-write */ |
| 84 | u32 queue_used_hi; /* read-write */ |
| 85 | } virtio_pci_common_cfg; |
| 86 | |
| 87 | typedef struct virtio_pci_isr { |
| 88 | u8 isr; |
| 89 | } virtio_pci_isr; |
| 90 | |
| 91 | /* --- driver structs ----------------------------------------------- */ |
| 92 | |
Gerd Hoffmann | 0e21548 | 2015-07-03 11:07:05 +0200 | [diff] [blame] | 93 | #define VP_ACCESS_IO 1 |
| 94 | #define VP_ACCESS_MMIO 2 |
| 95 | #define VP_ACCESS_PCICFG 3 |
| 96 | |
Gerd Hoffmann | d896646 | 2015-06-25 16:14:21 +0200 | [diff] [blame] | 97 | struct vp_cap { |
Kevin O'Connor | f46739b | 2016-02-02 22:34:27 -0500 | [diff] [blame] | 98 | union { |
| 99 | void *memaddr; |
| 100 | u32 ioaddr; |
Gerd Hoffmann | 0e21548 | 2015-07-03 11:07:05 +0200 | [diff] [blame] | 101 | u32 baroff; |
Kevin O'Connor | f46739b | 2016-02-02 22:34:27 -0500 | [diff] [blame] | 102 | }; |
Gerd Hoffmann | 0e21548 | 2015-07-03 11:07:05 +0200 | [diff] [blame] | 103 | u16 bdf; |
Gerd Hoffmann | d896646 | 2015-06-25 16:14:21 +0200 | [diff] [blame] | 104 | u8 cap; |
Gerd Hoffmann | 0e21548 | 2015-07-03 11:07:05 +0200 | [diff] [blame] | 105 | u8 cfg; |
Gerd Hoffmann | d896646 | 2015-06-25 16:14:21 +0200 | [diff] [blame] | 106 | u8 bar; |
Gerd Hoffmann | 0e21548 | 2015-07-03 11:07:05 +0200 | [diff] [blame] | 107 | u8 mode; |
Gerd Hoffmann | d896646 | 2015-06-25 16:14:21 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 110 | struct vp_device { |
Gerd Hoffmann | 0de7891 | 2015-06-30 08:50:09 +0200 | [diff] [blame] | 111 | struct vp_cap common, notify, isr, device, legacy; |
Gerd Hoffmann | 2e68fb1 | 2015-06-26 08:19:28 +0200 | [diff] [blame] | 112 | u32 notify_off_multiplier; |
Gerd Hoffmann | 46d1792 | 2015-06-25 12:28:46 +0200 | [diff] [blame] | 113 | u8 use_modern; |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
Gerd Hoffmann | c579d2f | 2016-06-17 11:45:43 +0200 | [diff] [blame] | 116 | u64 _vp_read(struct vp_cap *cap, u32 offset, u8 size); |
| 117 | void _vp_write(struct vp_cap *cap, u32 offset, u8 size, u64 var); |
Gerd Hoffmann | 3bbc849 | 2015-06-25 12:24:59 +0200 | [diff] [blame] | 118 | |
| 119 | #define vp_read(_cap, _struct, _field) \ |
| 120 | _vp_read(_cap, offsetof(_struct, _field), \ |
| 121 | sizeof(((_struct *)0)->_field)) |
| 122 | |
| 123 | #define vp_write(_cap, _struct, _field, _var) \ |
| 124 | _vp_write(_cap, offsetof(_struct, _field), \ |
| 125 | sizeof(((_struct *)0)->_field), _var) |
| 126 | |
Gerd Hoffmann | 46d1792 | 2015-06-25 12:28:46 +0200 | [diff] [blame] | 127 | u64 vp_get_features(struct vp_device *vp); |
| 128 | void vp_set_features(struct vp_device *vp, u64 features); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 129 | |
Gerd Hoffmann | 6b67c62 | 2015-06-26 11:55:41 +0200 | [diff] [blame] | 130 | static inline void vp_get_legacy(struct vp_device *vp, unsigned offset, |
| 131 | void *buf, unsigned len) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 132 | { |
Gerd Hoffmann | 6b67c62 | 2015-06-26 11:55:41 +0200 | [diff] [blame] | 133 | u8 *ptr = buf; |
| 134 | unsigned i; |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 135 | |
Gerd Hoffmann | 6b67c62 | 2015-06-26 11:55:41 +0200 | [diff] [blame] | 136 | for (i = 0; i < len; i++) |
Gerd Hoffmann | fb752e1 | 2015-06-30 09:53:03 +0200 | [diff] [blame] | 137 | ptr[i] = vp_read(&vp->legacy, virtio_pci_legacy, device[i]); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 138 | } |
| 139 | |
Gerd Hoffmann | 6ee9776 | 2015-06-25 16:37:41 +0200 | [diff] [blame] | 140 | u8 vp_get_status(struct vp_device *vp); |
| 141 | void vp_set_status(struct vp_device *vp, u8 status); |
Gerd Hoffmann | adeb2e3 | 2015-06-25 16:43:17 +0200 | [diff] [blame] | 142 | u8 vp_get_isr(struct vp_device *vp); |
Gerd Hoffmann | 984db76 | 2015-06-25 16:44:06 +0200 | [diff] [blame] | 143 | void vp_reset(struct vp_device *vp); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 144 | |
Gerd Hoffmann | 74d0cdc | 2015-06-25 10:24:27 +0200 | [diff] [blame] | 145 | struct pci_device; |
Kevin O'Connor | 7d09d0e | 2010-05-10 21:51:38 -0400 | [diff] [blame] | 146 | struct vring_virtqueue; |
Gerd Hoffmann | 74d0cdc | 2015-06-25 10:24:27 +0200 | [diff] [blame] | 147 | void vp_init_simple(struct vp_device *vp, struct pci_device *pci); |
Gerd Hoffmann | 2e68fb1 | 2015-06-26 08:19:28 +0200 | [diff] [blame] | 148 | void vp_notify(struct vp_device *vp, struct vring_virtqueue *vq); |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 149 | int vp_find_vq(struct vp_device *vp, int queue_index, |
Paolo Bonzini | e1a17bb | 2011-11-16 13:02:57 +0100 | [diff] [blame] | 150 | struct vring_virtqueue **p_vq); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 151 | #endif /* _VIRTIO_PCI_H_ */ |