virtio-rtc: Add basic device support
cover virtio-rtc: Add basic device support
This patch series introduces initial support for the virtio-rtc device (VIRTIO_ID_CLOCK), based on the OASIS virtio-rtc specification.
This implementation provides a single read-only clock that returns the host's QEMU_CLOCK_HOST time in nanoseconds. It handles the basic control and read requests necessary for the device to be successfully probed and utilized by the upstream Linux virtio_rtc driver.
I have verified this implementation using the virtio_rtc driver from the Linux mainline. The device probes successfully, and the host time can be correctly read from the guest using the following commands:
$ cat /sys/class/rtc/rtc0/time $ cat /sys/class/rtc/rtc0/date $ hwclock -r -f /dev/rtc0
Kuan-Wei Chiu (2):
standard-headers: Add virtio_rtc.h from Linux 7.0-rc1
virtio-rtc: Add basic virtio-rtc support
MAINTAINERS | 7 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 2 + hw/virtio/virtio-rtc-pci.c | 65 ++++++ hw/virtio/virtio-rtc.c | 190 ++++++++++++++++ include/hw/virtio/virtio-rtc.h | 22 ++ include/standard-headers/linux/virtio_rtc.h | 237 ++++++++++++++++++++ 7 files changed, 528 insertions(+) create mode 100644 hw/virtio/virtio-rtc-pci.c create mode 100644 hw/virtio/virtio-rtc.c create mode 100644 include/hw/virtio/virtio-rtc.h create mode 100644 include/standard-headers/linux/virtio_rtc.h
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH 1/2 standard-headers: Add virtio_rtc.h from Linux 7.0-rc1
Add virtio_rtc.h from the upstream Linux kernel using scripts/update-linux-headers.sh.
Source:
- Linux commit 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f (v7.0-rc1)
- Author: Linus Torvalds <[email protected]>
- Date: Sun Feb 22 13:18:59 2026 -0800
--- include/standard-headers/linux/virtio_rtc.h | 237 ++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 include/standard-headers/linux/virtio_rtc.h
include/standard-headers/linux/virtio_rtc.h new file +237 -0
@@ -0,0 +1,237 @@ +/* SPDX-License-Identifier: ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* +* Copyright (C) 2022-2024 OpenSynergy GmbH +* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. +*/ + +#ifndef _LINUX_VIRTIO_RTC_H +#define _LINUX_VIRTIO_RTC_H + +#include "standard-headers/linux/types.h" + +/* alarm feature */ +#define VIRTIO_RTC_F_ALARM 0 + +/* read request message types */ + +#define VIRTIO_RTC_REQ_READ 0x0001 +#define VIRTIO_RTC_REQ_READ_CROSS 0x0002 + +/* control request message types */ + +#define VIRTIO_RTC_REQ_CFG 0x1000 +#define VIRTIO_RTC_REQ_CLOCK_CAP 0x1001 +#define VIRTIO_RTC_REQ_CROSS_CAP 0x1002 +#define VIRTIO_RTC_REQ_READ_ALARM 0x1003 +#define VIRTIO_RTC_REQ_SET_ALARM 0x1004 +#define VIRTIO_RTC_REQ_SET_ALARM_ENABLED 0x1005 + +/* alarmq message types */ + +#define VIRTIO_RTC_NOTIF_ALARM 0x2000 + +/* Message headers */ + +/** common request header */ +struct virtio_rtc_req_head { +uint16_t msg_type; +uint8_t reserved[6 ]; +}; + +/** common response header */ +struct virtio_rtc_resp_head { +#define VIRTIO_RTC_S_OK 0 +#define VIRTIO_RTC_S_EOPNOTSUPP 2 +#define VIRTIO_RTC_S_ENODEV 3 +#define VIRTIO_RTC_S_EINVAL 4 +#define VIRTIO_RTC_S_EIO 5 +uint8_t status; +uint8_t reserved[7 ]; +}; + +/** common notification header */ +struct virtio_rtc_notif_head { +uint16_t msg_type; +uint8_t reserved[6 ]; +}; + +/* read requests */ + +/* VIRTIO_RTC_REQ_READ message */ + +struct virtio_rtc_req_read { +struct virtio_rtc_req_head head; +uint16_t clock_id; +uint8_t reserved[6 ]; +}; + +struct virtio_rtc_resp_read { +struct virtio_rtc_resp_head head; +uint64_t clock_reading; +}; + +/* VIRTIO_RTC_REQ_READ_CROSS message */ + +struct virtio_rtc_req_read_cross { +struct virtio_rtc_req_head head; +uint16_t clock_id; +/* Arm Generic Timer Counter-timer Virtual Count Register (CNTVCT_EL0) */ +#define VIRTIO_RTC_COUNTER_ARM_VCT 0 +/* x86 Time-Stamp Counter */ +#define VIRTIO_RTC_COUNTER_X86_TSC 1 +/* Invalid */ +#define VIRTIO_RTC_COUNTER_INVALID 0xFF +uint8_t hw_counter; +uint8_t reserved[5 ]; +}; + +struct virtio_rtc_resp_read_cross { +struct virtio_rtc_resp_head head; +uint64_t clock_reading; +uint64_t counter_cycles; +}; + +/* control requests */ + +/* VIRTIO_RTC_REQ_CFG message */ + +struct virtio_rtc_req_cfg { +struct virtio_rtc_req_head head; +/* no request params */ +}; + +struct virtio_rtc_resp_cfg { +struct virtio_rtc_resp_head head; +/** # of clocks -> clock ids < num_clocks are valid */ +uint16_t num_clocks; +uint8_t reserved[6 ]; +}; + +/* VIRTIO_RTC_REQ_CLOCK_CAP message */ + +struct virtio_rtc_req_clock_cap { +struct virtio_rtc_req_head head; +uint16_t clock_id; +uint8_t reserved[6 ]; +}; + +struct virtio_rtc_resp_clock_cap { +struct virtio_rtc_resp_head head; +#define VIRTIO_RTC_CLOCK_UTC 0 +#define VIRTIO_RTC_CLOCK_TAI 1 +#define VIRTIO_RTC_CLOCK_MONOTONIC 2 +#define VIRTIO_RTC_CLOCK_UTC_SMEARED 3 +#define VIRTIO_RTC_CLOCK_UTC_MAYBE_SMEARED 4 +uint8_t type; +#define VIRTIO_RTC_SMEAR_UNSPECIFIED 0 +#define VIRTIO_RTC_SMEAR_NOON_LINEAR 1 +#define VIRTIO_RTC_SMEAR_UTC_SLS 2 +uint8_t leap_second_smearing; +#define VIRTIO_RTC_FLAG_ALARM_CAP (1 << 0) +uint8_t flags; +uint8_t reserved[5 ]; +}; + +/* VIRTIO_RTC_REQ_CROSS_CAP message */ + +struct virtio_rtc_req_cross_cap { +struct virtio_rtc_req_head head; +uint16_t clock_id; +uint8_t hw_counter; +uint8_t reserved[5 ]; +}; + +struct virtio_rtc_resp_cross_cap { +struct virtio_rtc_resp_head head; +#define VIRTIO_RTC_FLAG_CROSS_CAP (1 << 0) +uint8_t flags; +uint8_t reserved[7 ]; +}; + +/* VIRTIO_RTC_REQ_READ_ALARM message */ + +struct virtio_rtc_req_read_alarm { +struct virtio_rtc_req_head head; +uint16_t clock_id; +uint8_t reserved[6 ]; +}; + +struct virtio_rtc_resp_read_alarm { +struct virtio_rtc_resp_head head; +uint64_t alarm_time; +#define VIRTIO_RTC_FLAG_ALARM_ENABLED (1 << 0) +uint8_t flags; +uint8_t reserved[7 ]; +}; + +/* VIRTIO_RTC_REQ_SET_ALARM message */ + +struct virtio_rtc_req_set_alarm { +struct virtio_rtc_req_head head; +uint64_t alarm_time; +uint16_t clock_id; +/* flag VIRTIO_RTC_FLAG_ALARM_ENABLED */ +uint8_t flags; +uint8_t reserved[5 ]; +}; + +struct virtio_rtc_resp_set_alarm { +struct virtio_rtc_resp_head head; +/* no response params */ +}; + +/* VIRTIO_RTC_REQ_SET_ALARM_ENABLED message */ + +struct virtio_rtc_req_set_alarm_enabled { +struct virtio_rtc_req_head head; +uint16_t clock_id; +/* flag VIRTIO_RTC_ALARM_ENABLED */ +uint8_t flags; +uint8_t reserved[5 ]; +}; + +struct virtio_rtc_resp_set_alarm_enabled { +struct virtio_rtc_resp_head head; +/* no response params */ +}; + +/** Union of request types for requestq */ +union virtio_rtc_req_requestq { +struct virtio_rtc_req_read read; +struct virtio_rtc_req_read_cross read_cross; +struct virtio_rtc_req_cfg cfg; +struct virtio_rtc_req_clock_cap clock_cap; +struct virtio_rtc_req_cross_cap cross_cap; +struct virtio_rtc_req_read_alarm read_alarm; +struct virtio_rtc_req_set_alarm set_alarm; +struct virtio_rtc_req_set_alarm_enabled set_alarm_enabled; +}; + +/** Union of response types for requestq */ +union virtio_rtc_resp_requestq { +struct virtio_rtc_resp_read read; +struct virtio_rtc_resp_read_cross read_cross; +struct virtio_rtc_resp_cfg cfg; +struct virtio_rtc_resp_clock_cap clock_cap; +struct virtio_rtc_resp_cross_cap cross_cap; +struct virtio_rtc_resp_read_alarm read_alarm; +struct virtio_rtc_resp_set_alarm set_alarm; +struct virtio_rtc_resp_set_alarm_enabled set_alarm_enabled; +}; + +/* alarmq notifications */ + +/* VIRTIO_RTC_NOTIF_ALARM notification */ + +struct virtio_rtc_notif_alarm { +struct virtio_rtc_notif_head head; +uint16_t clock_id; +uint8_t reserved[6 ]; +}; + +/** Union of notification types for alarmq */ +union virtio_rtc_notif_alarmq { +struct virtio_rtc_notif_alarm alarm; +}; + +#endif /* _LINUX_VIRTIO_RTC_H */
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH 2/2 virtio-rtc: Add basic virtio-rtc support
In reply to Kuan-Wei ChiuIntroduce the core device and PCI bindings for the virtio-rtc device (VIRTIO_ID_CLOCK).
This implementation provides a read-only clock that returns the host's time (QEMU_CLOCK_HOST) to the guest. It handles fundamental control requests, reporting a single supported clock of type VIRTIO_RTC_CLOCK_UTC, and responds to standard read requests.
- Virtio RTC Spec:
https://github.com/oasis-tcs/virtio-spec/tree/master/device-types/rtc
- Linux Virtio RTC driver:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/virtio/virtio_rtc_driver.c?h=v7.0-rc1
--- MAINTAINERS | 7 ++ hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 2 + hw/virtio/virtio-rtc-pci.c | 65 +++++++++++ hw/virtio/virtio-rtc.c | 190 +++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-rtc.h | 22 ++++ 6 files changed, 291 insertions(+) create mode 100644 hw/virtio/virtio-rtc-pci.c create mode 100644 hw/virtio/virtio-rtc.c create mode 100644 include/hw/virtio/virtio-rtc.h
MAINTAINERS +7 -0
@@ -2522,6 +2522,13 @@ F: include/system/rng*.h F: backends/rng*.c F: tests/qtest/virtio-rng-test.c +virtio-rtc +M: Kuan-Wei Chiu <[email protected]> +S: Maintained +F: hw/virtio/virtio-rtc.c +F: hw/virtio/virtio-rtc-pci.c +F: include/hw/virtio/virtio-rtc.h + virtio-nsm M: Alexander Graf <[email protected]> M: Dorjoy Chowdhury <[email protected]>
hw/virtio/Kconfig +5 -0
@@ -76,6 +76,11 @@ config VIRTIO_MEM depends on VIRTIO_MEM_SUPPORTED select VIRTIO_MD +config VIRTIO_RTC + bool + default y + depends on VIRTIO + config VHOST_VSOCK_COMMON bool depends on VIRTIO
hw/virtio/meson.build +2 -0
@@ -55,6 +55,7 @@ else endif system_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c')) system_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) +system_virtio_ss.add(when: 'CONFIG_VIRTIO_RTC', if_true: files('virtio-rtc.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c'))
@@ -78,6 +79,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('virtio-crypto-pc virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT_HOST', if_true: files('virtio-input-host-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT', if_true: files('virtio-input-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng-pci.c')) +virtio_pci_ss.add(when: 'CONFIG_VIRTIO_RTC', if_true: files('virtio-rtc-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm-pci.c', 'cbor-helpers.c'), libcbor]) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_9P', if_true: files('virtio-9p-pci.c'))
hw/virtio/virtio-rtc-pci.c new file +65 -0
65 lines not on this page · raw message
hw/virtio/virtio-rtc.c new file +190 -0
190 lines not on this page · raw message
include/hw/virtio/virtio-rtc.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* Virtio RTC device +* +* Copyright (c) 2026 Kuan-Wei Chiu <[email protected]> +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef QEMU_VIRTIO_RTC_H +#define QEMU_VIRTIO_RTC_H + +#include "hw/virtio/virtio.h" +#include "qom/object.h" + +#define TYPE_VIRTIO_RTC "virtio-rtc-device" +OBJECT_DECLARE_SIMPLE_TYPE(VirtIORtc, VIRTIO_RTC) + +struct VirtIORtc { +VirtIODevice parent_obj; +VirtQueue *vq; +}; + +#endif
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Hi,
Quoted 9 lines · Kuan-Wei Chiu
This patch series introduces initial support for the virtio-rtc device (
VIRTIO_ID_CLOCK), based on the OASIS virtio-rtc specification.This implementation provides a single read-only clock that returns the host's
QEMU_CLOCK_HOSTtime in nanoseconds. It handles the basic control and read requests necessary for the device to be successfully probed and utilized by the upstream Linuxvirtio_rtcdriver.
Quoted 7 lines
I have verified this implementation using the
virtio_rtcdriver from the Linux mainline. The device probes successfully, and the host time can be correctly read from the guest using the following commands:$ cat /sys/class/rtc/rtc0/time $ cat /sys/class/rtc/rtc0/date $ hwclock -r -f /dev/rtc0
Are there any comments or feedback on this?
Regards,
Kuan-Wei
Quoted 21 lines
Kuan-Wei Chiu (2):
standard-headers: Addvirtio_rtc.hfrom Linux 7.0-rc1
virtio-rtc: Add basic virtio-rtc supportMAINTAINERS | 7 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 2 + hw/virtio/virtio-rtc-pci.c | 65 ++++++ hw/virtio/virtio-rtc.c | 190 ++++++++++++++++ include/hw/virtio/virtio-rtc.h | 22 ++ include/standard-headers/linux/virtio_rtc.h | 237 ++++++++++++++++++++ 7 files changed, 528 insertions(+) create mode 100644 hw/virtio/virtio-rtc-pci.c create mode 100644 hw/virtio/virtio-rtc.c create mode 100644 include/hw/virtio/virtio-rtc.h create mode 100644 include/standard-headers/linux/virtio_rtc.hSignature · 1 line
2.53.0.473.g4a7958ca14-goog