L4Re – L4 Runtime Environment
virtio.h
1 /* SPDX-License-Identifier: GPL-2.0-only or License-Ref-kk-custom */
2 /*
3  * Copyright (C) 2013-2020 Kernkonzept GmbH.
4  * Author(s): Alexander Warg <alexander.warg@kernkonzept.com>
5  * Matthias Lange <matthias.lange@kernkonzept.com>
6  *
7  */
8 #pragma once
9 
28 #include <l4/sys/utcb.h>
29 #include <l4/sys/ipc.h>
30 #include <l4/sys/types.h>
31 
34 {
35  L4VIRTIO_PROTOCOL = 0,
36 };
37 
38 enum L4virtio_magic
39 {
40  L4VIRTIO_MAGIC = 0x74726976
41 };
42 
43 enum L4virtio_vendor
44 {
45  L4VIRTIO_VENDOR_KK = 0x44
46 };
47 
50 {
55 };
56 
59 {
75  L4VIRTIO_ID_SOCK = 0x9999,
76 };
77 
80 {
87 };
88 
91 {
96 };
97 
103 {
106 };
107 
112 {
113  L4VIRTIO_CMD_NONE = 0x00000000,
114  L4VIRTIO_CMD_SET_STATUS = 0x01000000,
115  L4VIRTIO_CMD_CFG_QUEUE = 0x02000000,
116  L4VIRTIO_CMD_MASK = 0xff000000,
117 };
118 
122 typedef struct l4virtio_config_hdr_t
123 {
130  l4_uint32_t dev_features_sel;
131  l4_uint32_t _res1[2];
132 
133  l4_uint32_t driver_features;
134  l4_uint32_t driver_features_sel;
135 
136  /* some L4virtio specific members ... */
140  /* must start at 0x30 (per virtio-mmio layout) */
141  l4_uint32_t queue_sel;
142  l4_uint32_t queue_num_max;
143  l4_uint32_t queue_num;
144  l4_uint32_t _res3[2];
145  l4_uint32_t queue_ready;
146  l4_uint32_t _res4[2];
147 
148  l4_uint32_t queue_notify;
149  l4_uint32_t _res5[3];
150 
151  l4_uint32_t irq_status;
152  l4_uint32_t irq_ack;
153  l4_uint32_t _res6[2];
154 
162  l4_uint32_t _res7[3];
163 
164  l4_uint64_t queue_desc;
165  l4_uint32_t _res8[2];
166  l4_uint64_t queue_avail;
167  l4_uint32_t _res9[2];
168  l4_uint64_t queue_used;
169 
170  /* use the unused space here for device and driver feature bitmaps */
171  l4_uint32_t dev_features_map[8];
172  l4_uint32_t driver_features_map[8];
173 
174  l4_uint32_t _res10[2];
175 
180 
183  l4_uint32_t generation;
185 
204 {
209 
212 
215 
223 
225 
231 L4_INLINE l4virtio_config_queue_t *
233 {
234  return (l4virtio_config_queue_t *)(((l4_addr_t)cfg) + cfg->queues_offset);
235 }
236 
242 L4_INLINE void *
244 {
245  return (void *)(((l4_addr_t)cfg) + 0x100);
246 }
247 
251 L4_INLINE void
252 l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
253 {
254  unsigned idx = feat / 32;
255 
256  if (idx < 8)
257  feature_map[idx] |= 1UL << (feat % 32);
258 }
259 
263 L4_INLINE void
264 l4virtio_clear_feature(l4_uint32_t *feature_map, unsigned feat)
265 {
266  unsigned idx = feat / 32;
267 
268  if (idx < 8)
269  feature_map[idx] &= ~(1UL << (feat % 32));
270 }
271 
275 L4_INLINE unsigned
276 l4virtio_get_feature(l4_uint32_t *feature_map, unsigned feat)
277 {
278  unsigned idx = feat / 32;
279 
280  if (idx >= 8)
281  return 0;
282 
283  return feature_map[idx] & (1UL << (feat % 32));
284 }
285 
291 L4_CV int
293 
299 L4_CV int
301 
307 L4_CV int
309  l4_uint64_t base, l4_umword_t offset,
310  l4_umword_t size) L4_NOTHROW;
311 
317 L4_CV int
319  l4_cap_idx_t host_irq, l4_cap_idx_t config_ds) L4_NOTHROW;
320 
326 L4_CV int
328  l4_addr_t *ds_offset) L4_NOTHROW;
329 
335 L4_CV int
338 
340 
#define L4_NOTHROW
Mark a function declaration and definition as never throwing an exception.
Definition: compiler.h:186
#define EXTERN_C_BEGIN
Start section with C types and functions.
Definition: compiler.h:190
#define EXTERN_C_END
End section with C types and functions.
Definition: compiler.h:191
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:51
unsigned long l4_addr_t
Address type.
Definition: l4int.h:45
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition: l4int.h:40
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition: l4int.h:38
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition: l4int.h:42
unsigned long l4_cap_idx_t
L4 Capability selector Type.
Definition: types.h:342
#define L4_CV
Define calling convention.
Definition: linkage.h:44
L4virtio_feature_bits
L4virtio-specific feature bits.
Definition: virtio.h:91
L4virtio_device_status
Virtio device status bits.
Definition: virtio.h:80
int l4virtio_set_status(l4_cap_idx_t cap, unsigned status) L4_NOTHROW
Write the VIRTIO status register.
int l4virtio_device_notification_irq(l4_cap_idx_t cap, unsigned index, l4_cap_idx_t irq) L4_NOTHROW
Get the notification interrupt corresponding to the given index.
L4_virtio_opcodes
L4-VIRTIO opcodes.
Definition: virtio.h:50
L4_virtio_cmd
Virtio commands for device configuration.
Definition: virtio.h:112
struct l4virtio_config_queue_t l4virtio_config_queue_t
Queue configuration entry.
l4virtio_config_queue_t * l4virtio_config_queues(l4virtio_config_hdr_t const *cfg)
Get the pointer to the first queue config.
Definition: virtio.h:232
L4_virtio_protocol
L4-VIRTIO protocol number.
Definition: virtio.h:34
int l4virtio_config_queue(l4_cap_idx_t cap, unsigned queue) L4_NOTHROW
Trigger queue configuration of the given queue.
int l4virtio_device_config_ds(l4_cap_idx_t cap, l4_cap_idx_t config_ds, l4_addr_t *ds_offset) L4_NOTHROW
Get the dataspace with the L4virtio configuration page.
L4virtio_device_ids
Virtio device IDs as reported in the driver's config space.
Definition: virtio.h:59
void l4virtio_clear_feature(l4_uint32_t *feature_map, unsigned feat)
Clear the given feature bit in a feature map.
Definition: virtio.h:264
struct l4virtio_config_hdr_t l4virtio_config_hdr_t
L4-VIRTIO config header, provided in shared data space.
void l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
Set the given feature bit in a feature map.
Definition: virtio.h:252
int l4virtio_register_ds(l4_cap_idx_t cap, l4_cap_idx_t ds_cap, l4_uint64_t base, l4_umword_t offset, l4_umword_t size) L4_NOTHROW
Register a shared data space with VIRTIO host.
unsigned l4virtio_get_feature(l4_uint32_t *feature_map, unsigned feat)
Check if the given bit in a feature map is set.
Definition: virtio.h:276
void * l4virtio_device_config(l4virtio_config_hdr_t const *cfg)
Get the pointer to the device configuration.
Definition: virtio.h:243
L4_virtio_irq_status
VIRTIO IRQ status codes (l4virtio_config_hdr_t::irq_status).
Definition: virtio.h:103
int l4virtio_register_iface(l4_cap_idx_t cap, l4_cap_idx_t guest_irq, l4_cap_idx_t host_irq, l4_cap_idx_t config_ds) L4_NOTHROW
Register client to the L4-VIRTIO device.
@ L4VIRTIO_FEATURE_CMD_CONFIG
Status and queue config are set via cmd field instead of via IPC.
Definition: virtio.h:95
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
Definition: virtio.h:93
@ L4VIRTIO_STATUS_DRIVER
Guest OS knows how to drive device.
Definition: virtio.h:82
@ L4VIRTIO_STATUS_FAILED
Driver detected fatal error.
Definition: virtio.h:86
@ L4VIRTIO_STATUS_ACKNOWLEDGE
Guest OS has found device.
Definition: virtio.h:81
@ L4VIRTIO_STATUS_FEATURES_OK
Driver has acknowledged feature set.
Definition: virtio.h:84
@ L4VIRTIO_STATUS_DRIVER_OK
Driver is set up.
Definition: virtio.h:83
@ L4VIRTIO_STATUS_DEVICE_NEEDS_RESET
Device detected fatal error.
Definition: virtio.h:85
@ L4VIRTIO_OP_SET_STATUS
Set status register in device config.
Definition: virtio.h:51
@ L4VIRTIO_OP_REGISTER_IFACE
Register a transport driver to the device.
Definition: virtio.h:53
@ L4VIRTIO_OP_CONFIG_QUEUE
Set queue config in device config.
Definition: virtio.h:52
@ L4VIRTIO_OP_REGISTER_DS
Register a data space as transport memory.
Definition: virtio.h:54
@ L4VIRTIO_CMD_NONE
No command pending.
Definition: virtio.h:113
@ L4VIRTIO_CMD_SET_STATUS
Set the status register.
Definition: virtio.h:114
@ L4VIRTIO_CMD_CFG_QUEUE
Configure a queue.
Definition: virtio.h:115
@ L4VIRTIO_CMD_MASK
Mask to get command bits.
Definition: virtio.h:116
@ L4VIRTIO_ID_SCSI
SCSI host device.
Definition: virtio.h:66
@ L4VIRTIO_ID_BLOCK
General block device.
Definition: virtio.h:61
@ L4VIRTIO_ID_BALLOON
Memory balooning device.
Definition: virtio.h:64
@ L4VIRTIO_ID_RPMSG
Device using rpmsg protocol.
Definition: virtio.h:65
@ L4VIRTIO_ID_INPUT
Input.
Definition: virtio.h:71
@ L4VIRTIO_ID_RPROC_SERIAL
Rproc serial device.
Definition: virtio.h:68
@ L4VIRTIO_ID_CONSOLE
Simple device for data IO via ports.
Definition: virtio.h:62
@ L4VIRTIO_ID_RNG
Entropy source.
Definition: virtio.h:63
@ L4VIRTIO_ID_NET
Virtual ethernet card.
Definition: virtio.h:60
@ L4VIRTIO_ID_SOCK
Unofficial socket device.
Definition: virtio.h:75
@ L4VIRTIO_ID_CRYPTO
Crypto.
Definition: virtio.h:73
@ L4VIRTIO_ID_VSOCK
Vsock transport.
Definition: virtio.h:72
@ L4VIRTIO_ID_GPU
GPU.
Definition: virtio.h:70
@ L4VIRTIO_ID_9P
Device using 9P transport protocol.
Definition: virtio.h:67
@ L4VIRTIO_ID_CAIF
Device using CAIF network protocol.
Definition: virtio.h:69
@ L4VIRTIO_IRQ_STATUS_VRING
VRING IRQ pending flag.
Definition: virtio.h:104
@ L4VIRTIO_IRQ_STATUS_CONFIG
CONFIG IRQ pending flag.
Definition: virtio.h:105
Common L4 ABI Data Types.
L4-VIRTIO config header, provided in shared data space.
Definition: virtio.h:123
l4_uint32_t vendor
vendor ID
Definition: virtio.h:127
l4_uint32_t magic
magic value (must be 'virt').
Definition: virtio.h:124
l4_uint32_t dev_features
device features windows selected by device_feature_sel
Definition: virtio.h:129
l4_uint32_t queues_offset
offset of virtqueue config array
Definition: virtio.h:138
l4_uint32_t device
device ID
Definition: virtio.h:126
l4_uint32_t cmd
L4 specific command register polled by the driver iff supported.
Definition: virtio.h:182
l4_uint32_t cfg_driver_notify_index
W: Event index to be used for config notifications (device to driver)
Definition: virtio.h:177
l4_uint32_t status
Device status register (read-only).
Definition: virtio.h:161
l4_uint32_t num_queues
number of virtqueues
Definition: virtio.h:137
l4_uint32_t version
VIRTIO version.
Definition: virtio.h:125
l4_uint32_t cfg_device_notify_index
R: Event index to be used for config notifications (driver to device)
Definition: virtio.h:179
Queue configuration entry.
Definition: virtio.h:204
l4_uint16_t device_notify_index
R: Event index to be used by the driver (driver to device)
Definition: virtio.h:221
l4_uint16_t driver_notify_index
W: Event index to be used for device notifications (device to driver)
Definition: virtio.h:214
l4_uint64_t avail_addr
W: address of available ring.
Definition: virtio.h:217
l4_uint64_t desc_addr
W: address of descriptor table.
Definition: virtio.h:216
l4_uint64_t used_addr
W: address of used ring.
Definition: virtio.h:218
l4_uint16_t ready
RW: queue ready flag (read-write)
Definition: virtio.h:211
l4_uint16_t num_max
R: maximum number of descriptors supported by this queue.
Definition: virtio.h:206
l4_uint16_t num
RW: number of descriptors configured for this queue.
Definition: virtio.h:208