Loading...
Searching...
No Matches
virtio.h
/* SPDX-License-Identifier: MIT */
/*
* Copyright (C) 2013-2022, 2024 Kernkonzept GmbH.
* Author(s): Alexander Warg <alexander.warg@kernkonzept.com>
* Matthias Lange <matthias.lange@kernkonzept.com>
*
*/
#pragma once
#include <l4/sys/compiler.h>
#include <l4/sys/utcb.h>
#include <l4/sys/ipc.h>
#include <l4/sys/types.h>
enum L4_virtio_protocol
{
L4VIRTIO_PROTOCOL = 0,
};
enum L4virtio_magic
{
L4VIRTIO_MAGIC = 0x74726976
};
enum L4virtio_vendor
{
L4VIRTIO_VENDOR_KK = 0x44
};
enum L4_virtio_opcodes
{
L4VIRTIO_OP_SET_STATUS = 0,
L4VIRTIO_OP_CONFIG_QUEUE = 1,
L4VIRTIO_OP_REGISTER_DS = 3,
L4VIRTIO_OP_DEVICE_CONFIG = 4,
L4VIRTIO_OP_GET_DEVICE_IRQ = 5,
};
enum L4virtio_device_ids
{
L4VIRTIO_ID_NET = 1,
L4VIRTIO_ID_BLOCK = 2,
L4VIRTIO_ID_CONSOLE = 3,
L4VIRTIO_ID_RNG = 4,
L4VIRTIO_ID_BALLOON = 5,
L4VIRTIO_ID_RPMSG = 7,
L4VIRTIO_ID_SCSI = 8,
L4VIRTIO_ID_9P = 9,
L4VIRTIO_ID_RPROC_SERIAL = 11,
L4VIRTIO_ID_CAIF = 12,
L4VIRTIO_ID_GPU = 16,
L4VIRTIO_ID_INPUT = 18,
L4VIRTIO_ID_VSOCK = 19,
L4VIRTIO_ID_CRYPTO = 20,
L4VIRTIO_ID_FS = 26,
L4VIRTIO_ID_SCMI = 32,
L4VIRTIO_ID_I2C = 34,
L4VIRTIO_ID_GPIO = 41,
L4VIRTIO_ID_SOCK = 0x9999,
};
enum L4virtio_device_status
{
L4VIRTIO_STATUS_ACKNOWLEDGE = 1,
L4VIRTIO_STATUS_DRIVER = 2,
L4VIRTIO_STATUS_DRIVER_OK = 4,
L4VIRTIO_STATUS_FEATURES_OK = 8,
L4VIRTIO_STATUS_DEVICE_NEEDS_RESET = 0x40,
L4VIRTIO_STATUS_FAILED = 0x80
};
enum L4virtio_feature_bits
{
L4VIRTIO_FEATURE_VERSION_1 = 32,
L4VIRTIO_FEATURE_CMD_CONFIG = 160
};
enum L4_virtio_irq_status
{
L4VIRTIO_IRQ_STATUS_VRING = 1,
L4VIRTIO_IRQ_STATUS_CONFIG = 2,
};
enum L4_virtio_cmd
{
L4VIRTIO_CMD_NONE = 0x00000000,
L4VIRTIO_CMD_SET_STATUS = 0x01000000,
L4VIRTIO_CMD_CFG_QUEUE = 0x02000000,
L4VIRTIO_CMD_CFG_CHANGED = 0x04000000,
L4VIRTIO_CMD_NOTIFY_QUEUE = 0x08000000,
L4VIRTIO_CMD_MASK = 0xff000000,
};
typedef struct l4virtio_config_hdr_t
{
/* Virtio(0x00): device config */
l4_uint32_t magic;
l4_uint32_t version;
l4_uint32_t device;
l4_uint32_t vendor;
/* Virtio(0x10): device features */
l4_uint32_t dev_features;
l4_uint32_t dev_features_sel;
l4_uint32_t _res1[2];
/* Virtio(0x20): driver features */
l4_uint32_t driver_features;
l4_uint32_t driver_features_sel;
/* L4Virtio(0x28): L4 queue */
l4_uint32_t num_queues;
l4_uint32_t queues_offset;
/* Virtio(0x30): queue status */
l4_uint32_t queue_sel;
l4_uint32_t queue_num_max;
l4_uint32_t queue_num;
l4_uint32_t _res3[2];
l4_uint32_t queue_ready;
l4_uint32_t _res4[2];
/* Virtio(0x50): queue notify */
l4_uint32_t queue_notify;
l4_uint32_t _res5[3];
/* Virtio(0x60): interrupt handling */
l4_uint32_t irq_status;
l4_uint32_t irq_ack;
l4_uint32_t _res6[2];
/* Virtio(0x70): Device status register (read-only). The register must be
* written using l4virtio_set_status(). */
l4_uint32_t status;
/* L4Virtio(0x74): W: Event index to be used for config notifications (device to driver) */
l4_uint32_t cfg_driver_notify_index;
/* L4Virtio(0x78): R: Event index to be used for config notifications (driver to device) */
l4_uint32_t cfg_device_notify_index;
/* L4Virtio(0x7c) L4 specific command register polled by the driver iff supported */
l4_uint32_t cmd;
/* Virtio(0x80): queue descriptors */
l4_uint64_t queue_desc;
l4_uint32_t _res8[2];
l4_uint64_t queue_avail;
l4_uint32_t _res9[2];
l4_uint64_t queue_used;
l4_uint32_t _res10[1];
/* Virtio(0xac): shared memory region */
l4_uint32_t shm_sel;
l4_uint64_t shm_len;
l4_uint64_t shm_base;
/* L4Virtio(0xc0): use the unused space here for device and driver feature bitmaps */
l4_uint32_t dev_features_map[6];
l4_uint32_t _res11[2];
l4_uint32_t driver_features_map[6];
l4_uint32_t _res12[1];
/* Virtio(0xfc): config generation */
l4_uint32_t generation;
} l4virtio_config_hdr_t;
typedef struct l4virtio_config_queue_t
{
l4_uint16_t num_max;
l4_uint16_t num;
l4_uint16_t ready;
l4_uint16_t driver_notify_index;
l4_uint64_t desc_addr;
l4_uint64_t avail_addr;
l4_uint64_t used_addr;
l4_uint16_t device_notify_index;
} l4virtio_config_queue_t;
L4_BEGIN_DECLS
L4_INLINE l4virtio_config_queue_t *
l4virtio_config_queues(l4virtio_config_hdr_t const *cfg)
{
return (l4virtio_config_queue_t *)(((l4_addr_t)cfg) + cfg->queues_offset);
}
L4_INLINE void *
l4virtio_device_config(l4virtio_config_hdr_t const *cfg)
{
return (void *)(((l4_addr_t)cfg) + 0x100);
}
L4_INLINE void
l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
{
unsigned idx = feat / 32;
if (idx < 8)
feature_map[idx] |= 1UL << (feat % 32);
}
L4_INLINE void
l4virtio_clear_feature(l4_uint32_t *feature_map, unsigned feat)
{
unsigned idx = feat / 32;
if (idx < 8)
feature_map[idx] &= ~(1UL << (feat % 32));
}
L4_INLINE unsigned
l4virtio_get_feature(l4_uint32_t *feature_map, unsigned feat)
{
unsigned idx = feat / 32;
if (idx >= 8)
return 0;
return feature_map[idx] & (1UL << (feat % 32));
}
L4_CV int
l4virtio_set_status(l4_cap_idx_t cap, unsigned status) L4_NOTHROW;
L4_CV int
l4virtio_config_queue(l4_cap_idx_t cap, unsigned queue) L4_NOTHROW;
L4_CV 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;
L4_CV int
l4virtio_device_config_ds(l4_cap_idx_t cap, l4_cap_idx_t config_ds,
l4_addr_t *ds_offset) L4_NOTHROW;
L4_CV int
l4virtio_device_notification_irq(l4_cap_idx_t cap, unsigned index,
l4_cap_idx_t irq) L4_NOTHROW;
L4_END_DECLS
compiler.h
L4 compiler related defines.
l4_umword_t
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
l4_addr_t
unsigned long l4_addr_t
Address type.
Definition l4int.h:34
l4_uint32_t
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:29
l4_uint16_t
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition l4int.h:27
l4_uint64_t
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:31
l4_cap_idx_t
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:336
L4_CV
#define L4_CV
Define calling convention.
Definition linkage.h:33
L4_NOTHROW
#define L4_NOTHROW
Mark a function declaration and definition as never throwing an exception.
Definition compiler.h:161
L4_INLINE
#define L4_INLINE
L4 Inline function attribute.
Definition compiler.h:51
L4_BEGIN_DECLS
#define L4_BEGIN_DECLS
Start section with C types and functions.
Definition compiler.h:165
L4_END_DECLS
#define L4_END_DECLS
End section with C types and functions.
Definition compiler.h:166
L4virtio_feature_bits
L4virtio_feature_bits
L4virtio-specific feature bits.
Definition virtio.h:99
L4virtio_device_status
L4virtio_device_status
Virtio device status bits.
Definition virtio.h:88
l4virtio_set_status
int l4virtio_set_status(l4_cap_idx_t cap, unsigned status) L4_NOTHROW
l4virtio_device_notification_irq
int l4virtio_device_notification_irq(l4_cap_idx_t cap, unsigned index, l4_cap_idx_t irq) L4_NOTHROW
L4_virtio_opcodes
L4_virtio_opcodes
Opcodes to setup and configure a device.
Definition virtio.h:53
L4_virtio_cmd
L4_virtio_cmd
Virtio commands for device configuration.
Definition virtio.h:120
L4_virtio_protocol
L4_virtio_protocol
L4-VIRTIO protocol number.
Definition virtio.h:35
l4virtio_config_queue
int l4virtio_config_queue(l4_cap_idx_t cap, unsigned queue) L4_NOTHROW
l4virtio_device_config_ds
int l4virtio_device_config_ds(l4_cap_idx_t cap, l4_cap_idx_t config_ds, l4_addr_t *ds_offset) L4_NOTHROW
L4virtio_device_ids
L4virtio_device_ids
Virtio device IDs as reported in the driver's config space.
Definition virtio.h:63
l4virtio_device_config
void * l4virtio_device_config(l4virtio_config_hdr_t const *cfg)
Get the pointer to the device configuration.
Definition virtio.h:263
l4virtio_config_queues
L4_BEGIN_DECLS l4virtio_config_queue_t * l4virtio_config_queues(l4virtio_config_hdr_t const *cfg)
Get the pointer to the first queue config.
Definition virtio.h:252
l4virtio_clear_feature
void l4virtio_clear_feature(l4_uint32_t *feature_map, unsigned feat)
Clear the given feature bit in a feature map.
Definition virtio.h:284
l4virtio_set_feature
void l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
Set the given feature bit in a feature map.
Definition virtio.h:272
l4virtio_register_ds
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
l4virtio_get_feature
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:296
L4_virtio_irq_status
L4_virtio_irq_status
VIRTIO IRQ status codes (l4virtio_config_hdr_t::irq_status).
Definition virtio.h:111
L4VIRTIO_FEATURE_CMD_CONFIG
@ L4VIRTIO_FEATURE_CMD_CONFIG
Status and queue config are set via cmd field instead of via IPC.
Definition virtio.h:103
L4VIRTIO_FEATURE_VERSION_1
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
Definition virtio.h:101
L4VIRTIO_STATUS_DRIVER
@ L4VIRTIO_STATUS_DRIVER
Guest OS knows how to drive device.
Definition virtio.h:90
L4VIRTIO_STATUS_FAILED
@ L4VIRTIO_STATUS_FAILED
Driver detected fatal error.
Definition virtio.h:94
L4VIRTIO_STATUS_ACKNOWLEDGE
@ L4VIRTIO_STATUS_ACKNOWLEDGE
Guest OS has found device.
Definition virtio.h:89
L4VIRTIO_STATUS_FEATURES_OK
@ L4VIRTIO_STATUS_FEATURES_OK
Driver has acknowledged feature set.
Definition virtio.h:92
L4VIRTIO_STATUS_DRIVER_OK
@ L4VIRTIO_STATUS_DRIVER_OK
Driver is set up.
Definition virtio.h:91
L4VIRTIO_STATUS_DEVICE_NEEDS_RESET
@ L4VIRTIO_STATUS_DEVICE_NEEDS_RESET
Device detected fatal error.
Definition virtio.h:93
L4VIRTIO_OP_SET_STATUS
@ L4VIRTIO_OP_SET_STATUS
Write device status register.
Definition virtio.h:54
L4VIRTIO_OP_GET_DEVICE_IRQ
@ L4VIRTIO_OP_GET_DEVICE_IRQ
Retrieve device notification IRQ.
Definition virtio.h:58
L4VIRTIO_OP_DEVICE_CONFIG
@ L4VIRTIO_OP_DEVICE_CONFIG
Get device config page.
Definition virtio.h:57
L4VIRTIO_OP_CONFIG_QUEUE
@ L4VIRTIO_OP_CONFIG_QUEUE
Configure queue.
Definition virtio.h:55
L4VIRTIO_OP_REGISTER_DS
@ L4VIRTIO_OP_REGISTER_DS
Register shared memory with device.
Definition virtio.h:56
L4VIRTIO_CMD_NOTIFY_QUEUE
@ L4VIRTIO_CMD_NOTIFY_QUEUE
Configure a queue.
Definition virtio.h:125
L4VIRTIO_CMD_NONE
@ L4VIRTIO_CMD_NONE
No command pending.
Definition virtio.h:121
L4VIRTIO_CMD_SET_STATUS
@ L4VIRTIO_CMD_SET_STATUS
Set the status register.
Definition virtio.h:122
L4VIRTIO_CMD_CFG_QUEUE
@ L4VIRTIO_CMD_CFG_QUEUE
Configure a queue.
Definition virtio.h:123
L4VIRTIO_CMD_MASK
@ L4VIRTIO_CMD_MASK
Mask to get command bits.
Definition virtio.h:126
L4VIRTIO_CMD_CFG_CHANGED
@ L4VIRTIO_CMD_CFG_CHANGED
Device config changed.
Definition virtio.h:124
L4VIRTIO_ID_SCSI
@ L4VIRTIO_ID_SCSI
SCSI host device.
Definition virtio.h:70
L4VIRTIO_ID_BLOCK
@ L4VIRTIO_ID_BLOCK
General block device.
Definition virtio.h:65
L4VIRTIO_ID_BALLOON
@ L4VIRTIO_ID_BALLOON
Memory ballooning device.
Definition virtio.h:68
L4VIRTIO_ID_RPMSG
@ L4VIRTIO_ID_RPMSG
Device using rpmsg protocol.
Definition virtio.h:69
L4VIRTIO_ID_INPUT
@ L4VIRTIO_ID_INPUT
Input.
Definition virtio.h:75
L4VIRTIO_ID_RPROC_SERIAL
@ L4VIRTIO_ID_RPROC_SERIAL
Rproc serial device.
Definition virtio.h:72
L4VIRTIO_ID_CONSOLE
@ L4VIRTIO_ID_CONSOLE
Simple device for data IO via ports.
Definition virtio.h:66
L4VIRTIO_ID_FS
@ L4VIRTIO_ID_FS
FS.
Definition virtio.h:78
L4VIRTIO_ID_RNG
@ L4VIRTIO_ID_RNG
Entropy source.
Definition virtio.h:67
L4VIRTIO_ID_NET
@ L4VIRTIO_ID_NET
Virtual ethernet card.
Definition virtio.h:64
L4VIRTIO_ID_SOCK
@ L4VIRTIO_ID_SOCK
Unofficial socket device.
Definition virtio.h:83
L4VIRTIO_ID_CRYPTO
@ L4VIRTIO_ID_CRYPTO
Crypto.
Definition virtio.h:77
L4VIRTIO_ID_GPIO
@ L4VIRTIO_ID_GPIO
Gpio device.
Definition virtio.h:81
L4VIRTIO_ID_VSOCK
@ L4VIRTIO_ID_VSOCK
Vsock transport.
Definition virtio.h:76
L4VIRTIO_ID_GPU
@ L4VIRTIO_ID_GPU
GPU.
Definition virtio.h:74
L4VIRTIO_ID_I2C
@ L4VIRTIO_ID_I2C
I2C device.
Definition virtio.h:80
L4VIRTIO_ID_9P
@ L4VIRTIO_ID_9P
Device using 9P transport protocol.
Definition virtio.h:71
L4VIRTIO_ID_CAIF
@ L4VIRTIO_ID_CAIF
Device using CAIF network protocol.
Definition virtio.h:73
L4VIRTIO_ID_SCMI
@ L4VIRTIO_ID_SCMI
Scmi device.
Definition virtio.h:79
L4VIRTIO_IRQ_STATUS_VRING
@ L4VIRTIO_IRQ_STATUS_VRING
VRING IRQ pending flag.
Definition virtio.h:112
L4VIRTIO_IRQ_STATUS_CONFIG
@ L4VIRTIO_IRQ_STATUS_CONFIG
CONFIG IRQ pending flag.
Definition virtio.h:113
ipc.h
Common IPC interface.
types.h
Common L4 ABI Data Types.
utcb.h
UTCB definitions.
l4virtio_config_hdr_t
L4-VIRTIO config header, provided in shared data space.
Definition virtio.h:133
l4virtio_config_hdr_t::vendor
l4_uint32_t vendor
vendor ID
Definition virtio.h:138
l4virtio_config_hdr_t::magic
l4_uint32_t magic
magic value (must be 'virt').
Definition virtio.h:135
l4virtio_config_hdr_t::dev_features
l4_uint32_t dev_features
device features windows selected by device_feature_sel
Definition virtio.h:141
l4virtio_config_hdr_t::queues_offset
l4_uint32_t queues_offset
offset of virtqueue config array
Definition virtio.h:151
l4virtio_config_hdr_t::device
l4_uint32_t device
device ID
Definition virtio.h:137
l4virtio_config_hdr_t::num_queues
l4_uint32_t num_queues
number of virtqueues
Definition virtio.h:150
l4virtio_config_hdr_t::version
l4_uint32_t version
VIRTIO version.
Definition virtio.h:136
l4virtio_config_queue_t
Queue configuration entry.
Definition virtio.h:224
l4virtio_config_queue_t::device_notify_index
l4_uint16_t device_notify_index
R: Event index to be used by the driver (driver to device).
Definition virtio.h:241
l4virtio_config_queue_t::driver_notify_index
l4_uint16_t driver_notify_index
W: Event index to be used for device notifications (device to driver).
Definition virtio.h:234
l4virtio_config_queue_t::avail_addr
l4_uint64_t avail_addr
W: address of available ring.
Definition virtio.h:237
l4virtio_config_queue_t::desc_addr
l4_uint64_t desc_addr
W: address of descriptor table.
Definition virtio.h:236
l4virtio_config_queue_t::used_addr
l4_uint64_t used_addr
W: address of used ring.
Definition virtio.h:238
l4virtio_config_queue_t::ready
l4_uint16_t ready
RW: queue ready flag (read-write).
Definition virtio.h:231
l4virtio_config_queue_t::num_max
l4_uint16_t num_max
R: maximum number of descriptors supported by this queue.
Definition virtio.h:226
l4virtio_config_queue_t::num
l4_uint16_t num
RW: number of descriptors configured for this queue.
Definition virtio.h:228