1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <stdint.h>
8 #include <zircon/compiler.h>
9 
10 #define VIRTIO_STATUS_ACKNOWLEDGE                   (1u << 0)
11 #define VIRTIO_STATUS_DRIVER                        (1u << 1)
12 #define VIRTIO_STATUS_DRIVER_OK                     (1u << 2)
13 #define VIRTIO_STATUS_FEATURES_OK                   (1u << 3)
14 #define VIRTIO_STATUS_DEVICE_NEEDS_RESET            (1u << 6)
15 #define VIRTIO_STATUS_FAILED                        (1u << 7)
16 
17 // PCI config space for non-transitional devices.
18 #define VIRTIO_PCI_COMMON_CFG_DEVICE_FEATURES_SEL   0x0
19 #define VIRTIO_PCI_COMMON_CFG_DEVICE_FEATURES       0x4
20 #define VIRTIO_PCI_COMMON_CFG_DRIVER_FEATURES_SEL   0x8
21 #define VIRTIO_PCI_COMMON_CFG_DRIVER_FEATURES       0xc
22 #define VIRTIO_PCI_COMMON_CFG_MSIX_CONFIG           0x10
23 #define VIRTIO_PCI_COMMON_CFG_NUM_QUEUES            0x12
24 #define VIRTIO_PCI_COMMON_CFG_DEVICE_STATUS         0x14
25 #define VIRTIO_PCI_COMMON_CFG_CONFIG_GEN            0x15
26 #define VIRTIO_PCI_COMMON_CFG_QUEUE_SEL             0x16
27 #define VIRTIO_PCI_COMMON_CFG_QUEUE_SIZE            0x18
28 #define VIRTIO_PCI_COMMON_CFG_QUEUE_MSIX_VECTOR     0x1a
29 #define VIRTIO_PCI_COMMON_CFG_QUEUE_ENABLE          0x1c
30 #define VIRTIO_PCI_COMMON_CFG_QUEUE_NOTIFY_OFF      0x1e
31 #define VIRTIO_PCI_COMMON_CFG_QUEUE_DESC_LOW        0x20
32 #define VIRTIO_PCI_COMMON_CFG_QUEUE_DESC_HIGH       0x24
33 #define VIRTIO_PCI_COMMON_CFG_QUEUE_AVAIL_LOW       0x28
34 #define VIRTIO_PCI_COMMON_CFG_QUEUE_AVAIL_HIGH      0x2c
35 #define VIRTIO_PCI_COMMON_CFG_QUEUE_USED_LOW        0x30
36 #define VIRTIO_PCI_COMMON_CFG_QUEUE_USED_HIGH       0x34
37 
38 // PCI IO space for transitional virtio devices
39 #define VIRTIO_PCI_DEVICE_FEATURES                  0x0     // uint32_t
40 #define VIRTIO_PCI_DRIVER_FEATURES                  0x4     // uint32_t
41 #define VIRTIO_PCI_QUEUE_PFN                        0x8     // uint32_t
42 #define VIRTIO_PCI_QUEUE_SIZE                       0xc     // uint16_t
43 #define VIRTIO_PCI_QUEUE_SELECT                     0xe     // uint16_t
44 #define VIRTIO_PCI_QUEUE_NOTIFY                     0x10    // uint16_t
45 #define VIRTIO_PCI_DEVICE_STATUS                    0x12    // uint8_t
46 #define VIRTIO_PCI_ISR_STATUS                       0x13    // uint8_t
47 #define VIRTIO_PCI_MSI_CONFIG_VECTOR                0x14    // uint16_t
48 #define VIRTIO_PCI_MSI_QUEUE_VECTOR                 0x16    // uint16_t
49 
50 #define VIRTIO_PCI_CONFIG_OFFSET_NOMSIX             0x14    // uint16_t
51 #define VIRTIO_PCI_CONFIG_OFFSET_MSIX               0x18    // uint16_t
52 
53 #define VIRTIO_PCI_CAP_COMMON_CFG                   1
54 #define VIRTIO_PCI_CAP_NOTIFY_CFG                   2
55 #define VIRTIO_PCI_CAP_ISR_CFG                      3
56 #define VIRTIO_PCI_CAP_DEVICE_CFG                   4
57 #define VIRTIO_PCI_CAP_PCI_CFG                      5
58 
59 #define VIRTIO_ISR_QUEUE_INT                        0x1
60 #define VIRTIO_ISR_DEV_CFG_INT                      0x2
61 
62 #define VIRTIO_F_RING_INDIRECT_DESC                 28
63 #define VIRTIO_F_RING_EVENT_IDX                     29
64 #define VIRTIO_F_VERSION_1                          32
65 
66 __BEGIN_CDECLS
67 
68 typedef struct virtio_pci_cap {
69     uint8_t cap_vndr;
70     uint8_t cap_next;
71     uint8_t cap_len;
72     uint8_t cfg_type;
73     uint8_t bar;
74     uint8_t padding[3];
75     uint32_t offset;
76     uint32_t length;
77 } __PACKED virtio_pci_cap_t;
78 
79 typedef struct virtio_pci_notify_cap {
80   virtio_pci_cap_t cap;
81   uint32_t notify_off_multiplier;
82 } __PACKED virtio_pci_notify_cap_t;
83 
84 typedef struct virtio_pci_common_cfg {
85     uint32_t device_feature_select;
86     uint32_t device_feature;
87     uint32_t driver_feature_select;
88     uint32_t driver_feature;
89     uint16_t msix_config;
90     uint16_t num_queues;
91     uint8_t device_status;
92     uint8_t config_generation;
93 
94     uint16_t queue_select;
95     uint16_t queue_size;
96     uint16_t queue_msix_vector;
97     uint16_t queue_enable;
98     uint16_t queue_notify_off;
99     uint64_t queue_desc;
100     uint64_t queue_avail;
101     uint64_t queue_used;
102 } __PACKED virtio_pci_common_cfg_t;
103 
104 #define VIRTIO_PCI_VENDOR_ID            0x1af4
105 
106 // Device ids
107 #define VIRTIO_DEV_ID_RESERVED          0
108 #define VIRTIO_DEV_ID_NETWORK           1
109 #define VIRTIO_DEV_ID_BLOCK             2
110 #define VIRTIO_DEV_ID_CONSOLE           3
111 #define VIRTIO_DEV_ID_ENTROPY           4
112 #define VIRTIO_DEV_ID_BALLOON_T         5
113 #define VIRTIO_DEV_ID_IOMEMORY          6
114 #define VIRTIO_DEV_ID_RPMSG             7
115 #define VIRTIO_DEV_ID_SCSI_HOST         8
116 #define VIRTIO_DEV_ID_9P_TRANS          9
117 #define VIRTIO_DEV_ID_MAC80211          10
118 #define VIRTIO_DEV_ID_RPROC             11
119 #define VIRTIO_DEV_ID_CAIF              12
120 #define VIRTIO_DEV_ID_BALLOON           13
121 // Intentional gap of 14-15
122 #define VIRTIO_DEV_ID_GPU               16
123 #define VIRTIO_DEV_ID_TIMER             17
124 #define VIRTIO_DEV_ID_INPUT             18
125 #define VIRTIO_DEV_ID_SOCKET            19
126 
127 // Device Types (transitional)
128 #define VIRTIO_DEV_TYPE_T_NETWORK       0x1000
129 #define VIRTIO_DEV_TYPE_T_BLOCK         0x1001
130 #define VIRTIO_DEV_TYPE_T_BALLOON       0x1002
131 #define VIRTIO_DEV_TYPE_T_CONSOLE       0x1003
132 #define VIRTIO_DEV_TYPE_T_SCSI_HOST     0x1004
133 #define VIRTIO_DEV_TYPE_T_ENTROPY       0x1005
134 #define VIRTIO_DEV_TYPE_T_9P            0x1009
135 
136 // Legacy Device Types
137 #define VIRTIO_LEGACY_DEV_TYPE(dev_id) (0x1040 + dev_id)
138 #define VIRTIO_DEV_TYPE_NETWORK        VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_NETWORK)
139 #define VIRTIO_DEV_TYPE_BLOCK          VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_BLOCK)
140 #define VIRTIO_DEV_TYPE_BALLOON        VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_BALLOON)
141 #define VIRTIO_DEV_TYPE_CONSOLE        VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_CONSOLE)
142 #define VIRTIO_DEV_TYPE_SCSI           VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_SCSI_HOST)
143 #define VIRTIO_DEV_TYPE_ENTROPY        VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_ENTROPY)
144 #define VIRTIO_DEV_TYPE_9P             VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_9P)
145 #define VIRTIO_DEV_TYPE_GPU            VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_GPU)
146 #define VIRTIO_DEV_TYPE_INPUT          VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_INPUT)
147 #define VIRTIO_DEV_TYPE_SOCKET         VIRTIO_LEGACY_DEV_TYPE(VIRTIO_DEV_ID_SOCKET)
148 
149 __END_CDECLS
150