Skip to content

Commit c6608fc

Browse files
soburicfriedt
authored andcommitted
drivers: virtio: Separate macros into a common header
Move macros derived from the VIRTIO specification to a shared include. This change allows the VIRTIO standard definitions to be referenced from outside the driver implementation. The following definitions have also been added: - VIRTIO_F_VERSION_1 - VIRTIO_F_ACCESS_PLATFORM - VIRTIO_RING_F_INDIRECT_DESC - VIRTIO_RING_F_EVENT_IDX - VIRTQ_AVAIL_F_NO_INTERRUPT - VIRTQ_USED_F_NO_NOTIFY Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent a2691c6 commit c6608fc

File tree

2 files changed

+225
-60
lines changed

2 files changed

+225
-60
lines changed

drivers/virtio/virtio_common.h

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,7 @@
77
#ifndef ZEPHYR_VIRTIO_VIRTIO_COMMON_H_
88
#define ZEPHYR_VIRTIO_VIRTIO_COMMON_H_
99

10-
#define DEVICE_STATUS_ACKNOWLEDGE 0
11-
#define DEVICE_STATUS_DRIVER 1
12-
#define DEVICE_STATUS_DRIVER_OK 2
13-
#define DEVICE_STATUS_FEATURES_OK 3
14-
#define DEVICE_STATUS_NEEDS_RESET 6
15-
#define DEVICE_STATUS_FAILED 7
16-
17-
#define VIRTIO_F_VERSION_1 32
18-
19-
/* Ranges of feature bits for specific device types (see spec 2.2)*/
20-
#define DEV_TYPE_FEAT_RANGE_0_BEGIN 0
21-
#define DEV_TYPE_FEAT_RANGE_0_END 23
22-
#define DEV_TYPE_FEAT_RANGE_1_BEGIN 50
23-
#define DEV_TYPE_FEAT_RANGE_1_END 127
24-
25-
/*
26-
* While defined separately in 4.1.4.5 for PCI and in 4.2.2 for MMIO
27-
* the same bits are responsible for the same interrupts, so defines
28-
* with them can be unified
29-
*/
30-
#define VIRTIO_QUEUE_INTERRUPT 1
31-
#define VIRTIO_DEVICE_CONFIGURATION_INTERRUPT 2
32-
33-
/*
34-
* VIRTIO-MMIO register definitions.
35-
*
36-
* Based on Virtual I/O Device (VIRTIO) Version 1.3 specification:
37-
* https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf
38-
*/
39-
40-
#define VIRTIO_MMIO_MAGIC_VALUE 0x000
41-
#define VIRTIO_MMIO_VERSION 0x004
42-
#define VIRTIO_MMIO_DEVICE_ID 0x008
43-
#define VIRTIO_MMIO_VENDOR_ID 0x00c
44-
#define VIRTIO_MMIO_DEVICE_FEATURES 0x010
45-
#define VIRTIO_MMIO_DEVICE_FEATURES_SEL 0x014
46-
#define VIRTIO_MMIO_DRIVER_FEATURES 0x020
47-
#define VIRTIO_MMIO_DRIVER_FEATURES_SEL 0x024
48-
#define VIRTIO_MMIO_QUEUE_SEL 0x030
49-
#define VIRTIO_MMIO_QUEUE_SIZE_MAX 0x034
50-
#define VIRTIO_MMIO_QUEUE_SIZE 0x038
51-
#define VIRTIO_MMIO_QUEUE_READY 0x044
52-
#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050
53-
#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060
54-
#define VIRTIO_MMIO_INTERRUPT_ACK 0x064
55-
#define VIRTIO_MMIO_STATUS 0x070
56-
#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080
57-
#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084
58-
#define VIRTIO_MMIO_QUEUE_AVAIL_LOW 0x090
59-
#define VIRTIO_MMIO_QUEUE_AVAIL_HIGH 0x094
60-
#define VIRTIO_MMIO_QUEUE_USED_LOW 0x0a0
61-
#define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4
62-
#define VIRTIO_MMIO_SHM_SEL 0x0ac
63-
#define VIRTIO_MMIO_SHM_LEN_LOW 0x0b0
64-
#define VIRTIO_MMIO_SHM_LEN_HIGH 0x0b4
65-
#define VIRTIO_MMIO_SHM_BASE_LOW 0x0b8
66-
#define VIRTIO_MMIO_SHM_BASE_HIGH 0x0bc
67-
#define VIRTIO_MMIO_QUEUE_RESET 0x0c0
68-
#define VIRTIO_MMIO_CONFIG_GENERATION 0x0fc
69-
#define VIRTIO_MMIO_CONFIG 0x100
10+
#include <zephyr/drivers/virtio/virtio_config.h>
7011

7112
/**
7213
* Common virtio isr
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* Copyright (c) 2025 Antmicro <www.antmicro.com>
3+
* Copyright (c) 2025 TOKITA Hiroshi
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
/**
9+
* @file
10+
*
11+
* VIRTIO common definitions based on the specification.
12+
*
13+
* Based on Virtual I/O Device (VIRTIO) Version 1.3 specification:
14+
* https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf
15+
*/
16+
17+
#ifndef ZEPHYR_DRIVERS_VIRTIO_VIRTIO_CONFIG_H_
18+
#define ZEPHYR_DRIVERS_VIRTIO_VIRTIO_CONFIG_H_
19+
20+
/**
21+
* @name Virtio device status bits
22+
*
23+
* Bit positions of the device status field.
24+
* These are described in
25+
* <a href="https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#section.2.1">
26+
* 2.1 Device Status Field </a>
27+
*
28+
* @{
29+
*/
30+
31+
/** Indicates the guest has found and recognized the device presence. */
32+
#define DEVICE_STATUS_ACKNOWLEDGE 0
33+
/** Indicates the guest driver is ready to drive the device. */
34+
#define DEVICE_STATUS_DRIVER 1
35+
/** Indicates the driver has successfully set up the device and is ready. */
36+
#define DEVICE_STATUS_DRIVER_OK 2
37+
/** Indicates the driver and device agreed on the negotiated feature set. */
38+
#define DEVICE_STATUS_FEATURES_OK 3
39+
/** Indicates the device requests a reset to recover from an error. */
40+
#define DEVICE_STATUS_NEEDS_RESET 6
41+
/** Indicates the device has experienced a non-recoverable error. */
42+
#define DEVICE_STATUS_FAILED 7
43+
44+
/** @} */
45+
46+
/**
47+
* @name Feature Bits
48+
*
49+
* Negotiable device-independent feature bit positions.
50+
* These are described in
51+
* <a href=
52+
* "https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#chapter.6">
53+
* 6 Reserved Feature Bits
54+
* </a>
55+
*
56+
* @{
57+
*/
58+
59+
/** Indicates descriptors can reference descriptor tables. */
60+
#define VIRTIO_RING_F_INDIRECT_DESC 28
61+
/** Indicates driver/device use event index for notifications. */
62+
#define VIRTIO_RING_F_EVENT_IDX 29
63+
/** Indicates device complies with Virtio 1.0+ semantics. */
64+
#define VIRTIO_F_VERSION_1 32
65+
/** Indicates device needs platform-specific handling. */
66+
#define VIRTIO_F_ACCESS_PLATFORM 33
67+
68+
/** @} */
69+
70+
/**
71+
* @name Ring Flag Bits
72+
*
73+
* Available and used ring flag bit positions as described in
74+
*
75+
* <a href=
76+
* "https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#subsection.2.7.6">
77+
* 2.7.6 The Virtqueue Available Ring
78+
* </a>
79+
* and
80+
* <a href=
81+
* "https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#subsection.2.7.8">
82+
* 2.7.8 The Virtqueue Used Ring
83+
* </a>
84+
*
85+
* @{
86+
*/
87+
88+
/**
89+
* Driver requests the device to skip interrupts.
90+
* This is valid if @ref VIRTIO_RING_F_EVENT_IDX negotiated.
91+
*/
92+
#define VIRTQ_AVAIL_F_NO_INTERRUPT 1
93+
94+
/**
95+
* Device requests the driver to suppress notifications.
96+
* This is valid if @ref VIRTIO_RING_F_EVENT_IDX negotiated.
97+
*/
98+
#define VIRTQ_USED_F_NO_NOTIFY 1
99+
100+
/** @} */
101+
102+
/**
103+
* @name Ranges of feature bits
104+
*
105+
* These described in
106+
* <a href=
107+
* "https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#section.2.2">
108+
* 2.2 Feature Bits
109+
* </a>
110+
*
111+
* @{
112+
*/
113+
114+
/** Start of the first device-specific feature range. */
115+
#define DEV_TYPE_FEAT_RANGE_0_BEGIN 0
116+
/** End of the first device-specific feature range. */
117+
#define DEV_TYPE_FEAT_RANGE_0_END 23
118+
/** Start of the second device-specific feature range. */
119+
#define DEV_TYPE_FEAT_RANGE_1_BEGIN 50
120+
/** End of the second device-specific feature range. */
121+
#define DEV_TYPE_FEAT_RANGE_1_END 127
122+
123+
/** @} */
124+
125+
/**
126+
* @name Transport interrupt bits
127+
*
128+
* While defined separately in
129+
*
130+
* <a href=
131+
* "https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#subsubsection.4.1.4.5">
132+
* 4.1.4.5 ISR status capabilit </a> for PCI and
133+
* <a href=
134+
* "https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#subsection.4.2.2">
135+
* 4.2.2 MMIO Device Register Layout </a> for MMIO
136+
*
137+
* the same bits are responsible for the same interrupts, so defines
138+
* with them can be unified
139+
*
140+
* @{
141+
*/
142+
143+
/** A virtqueue has pending used buffers. */
144+
#define VIRTIO_QUEUE_INTERRUPT 1
145+
/** Device configuration space has changed. */
146+
#define VIRTIO_DEVICE_CONFIGURATION_INTERRUPT 2
147+
148+
/** @} */
149+
150+
/**
151+
* @name VIRTIO-MMIO registers
152+
*
153+
* The details are described in
154+
* <a href=
155+
* "https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.pdf#subsection.4.2.2">
156+
* 4.2.2 MMIO Device Register Layout </a>
157+
*
158+
* @{
159+
*/
160+
161+
/** Magic value identifying the virtio MMIO device. */
162+
#define VIRTIO_MMIO_MAGIC_VALUE 0x000
163+
/** Virtio specification version exposed by the device. */
164+
#define VIRTIO_MMIO_VERSION 0x004
165+
/** Device type identifier register. */
166+
#define VIRTIO_MMIO_DEVICE_ID 0x008
167+
/** Vendor-specific identifier register. */
168+
#define VIRTIO_MMIO_VENDOR_ID 0x00c
169+
/** Lower 32 bits of the device feature bitmap. */
170+
#define VIRTIO_MMIO_DEVICE_FEATURES 0x010
171+
/** Selector choosing the device feature word. */
172+
#define VIRTIO_MMIO_DEVICE_FEATURES_SEL 0x014
173+
/** Lower 32 bits of the negotiated driver feature bitmap. */
174+
#define VIRTIO_MMIO_DRIVER_FEATURES 0x020
175+
/** Selector choosing the driver feature word. */
176+
#define VIRTIO_MMIO_DRIVER_FEATURES_SEL 0x024
177+
/** Virtqueue index selected for subsequent accesses. */
178+
#define VIRTIO_MMIO_QUEUE_SEL 0x030
179+
/** Maximum queue size supported by the selected virtqueue. */
180+
#define VIRTIO_MMIO_QUEUE_SIZE_MAX 0x034
181+
/** Queue size chosen by the driver for the selected virtqueue. */
182+
#define VIRTIO_MMIO_QUEUE_SIZE 0x038
183+
/** Ready flag indicating driver ownership of the queue. */
184+
#define VIRTIO_MMIO_QUEUE_READY 0x044
185+
/** Doorbell register for queue notifications. */
186+
#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050
187+
/** Pending interrupt summary bits. */
188+
#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060
189+
/** Interrupt acknowledgment register. */
190+
#define VIRTIO_MMIO_INTERRUPT_ACK 0x064
191+
/** Device status. */
192+
#define VIRTIO_MMIO_STATUS 0x070
193+
/** Lower 32 bits of the descriptor table address. */
194+
#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080
195+
/** Upper 32 bits of the descriptor table address. */
196+
#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084
197+
/** Lower 32 bits of the available ring address. */
198+
#define VIRTIO_MMIO_QUEUE_AVAIL_LOW 0x090
199+
/** Upper 32 bits of the available ring address. */
200+
#define VIRTIO_MMIO_QUEUE_AVAIL_HIGH 0x094
201+
/** Lower 32 bits of the used ring address. */
202+
#define VIRTIO_MMIO_QUEUE_USED_LOW 0x0a0
203+
/** Upper 32 bits of the used ring address. */
204+
#define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4
205+
/** Shared memory region selector. */
206+
#define VIRTIO_MMIO_SHM_SEL 0x0ac
207+
/** Lower 32 bits of the shared memory length. */
208+
#define VIRTIO_MMIO_SHM_LEN_LOW 0x0b0
209+
/** Upper 32 bits of the shared memory length. */
210+
#define VIRTIO_MMIO_SHM_LEN_HIGH 0x0b4
211+
/** Lower 32 bits of the shared memory base address. */
212+
#define VIRTIO_MMIO_SHM_BASE_LOW 0x0b8
213+
/** Upper 32 bits of the shared memory base address. */
214+
#define VIRTIO_MMIO_SHM_BASE_HIGH 0x0bc
215+
/** Queue reset control register. */
216+
#define VIRTIO_MMIO_QUEUE_RESET 0x0c0
217+
/** Generation counter for configuration space. */
218+
#define VIRTIO_MMIO_CONFIG_GENERATION 0x0fc
219+
/** Base offset of the device configuration structure. */
220+
#define VIRTIO_MMIO_CONFIG 0x100
221+
222+
/** @} */
223+
224+
#endif /* ZEPHYR_DRIVERS_VIRTIO_VIRTIO_CONFIG_H_ */

0 commit comments

Comments
 (0)