Skip to contentSkip to conversation

virtio-cpufreq: Xen backend for guest CPU frequency

RFC PATCH New Baorui.Liu 1 participant 4 messages Sep 17 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Baorui.Liu

author
#0 lore raw

cover virtio-cpufreq: Xen backend for guest CPU frequency

This RFC adds a Xen-specific virtio-cpufreq device model so a guest frontend can query the host pCPU frequency of the pCPU that currently runs a given vCPU.

The guest protocol uses virtio device ID 42, which is not assigned by the virtio specification or by Linux. Patch 1 defines that ID in QEMU's imported virtio_ids.h. checkpatch warns that this file is normally updated via scripts/update-linux-headers.sh; that script cannot produce ID 42 until the ID exists upstream, so the define is added by hand for this RFC.

The backend requires each vCPU to be pinned to a unique pCPU. The PCI device uses two MSI-X vectors so virtio_notify() reaches the Android frontend we tested against.

This is a port of an AMD qemu-xen device. A companion Linux guest driver RFC was sent to linux-pm.

Based on work by Meng Zhang.

Baorui.Liu (3):
standard-headers: add experimental VIRTIO_ID_CPUFREQ hw/virtio: add virtio-cpufreq Xen backend hw/virtio: give virtio-cpufreq two MSI-X vectors

 MAINTAINERS                                 |   7 +
 hw/virtio/Kconfig                           |   5 +
 hw/virtio/meson.build                       |   2 +
 hw/virtio/virtio-cpufreq-pci.c              |  88 +++++++
 hw/virtio/virtio-cpufreq.c                  | 242 ++++++++++++++++++++
 hw/virtio/virtio-pci.c                      |   3 +
 hw/virtio/virtio-qmp.c                      |   1 +
 hw/virtio/virtio.c                          |   1 +
 include/hw/virtio/virtio-cpufreq.h          |  23 ++
 include/standard-headers/linux/virtio_ids.h |   1 +
 10 files changed, 373 insertions(+)
 create mode 100644 hw/virtio/virtio-cpufreq-pci.c
 create mode 100644 hw/virtio/virtio-cpufreq.c
 create mode 100644 include/hw/virtio/virtio-cpufreq.h
Signature · 1 line
2.34.1
author
#1 lore raw

RFC PATCH 1/3 standard-headers: add experimental VIRTIO_ID_CPUFREQ

Virtio device ID 42 is not yet assigned by the virtio specification or Linux. Define it locally so the Xen virtio-cpufreq backend can match the guest frontend.

This is not generated by scripts/update-linux-headers.sh.

---
 include/standard-headers/linux/virtio_ids.h | 1 +
 1 file changed, 1 insertion(+)
include/standard-headers/linux/virtio_ids.h +1 -0
index f9056af0c6..dde5a1914f 100644
@@ -68,6 +68,7 @@
 #define VIRTIO_ID_AUDIO_POLICY		39 /* virtio audio policy */
 #define VIRTIO_ID_BT			40 /* virtio bluetooth */
 #define VIRTIO_ID_GPIO			41 /* virtio gpio */
+#define VIRTIO_ID_CPUFREQ		42 /* virtio cpufreq (experimental) */
 #define VIRTIO_ID_SPI			45 /* virtio spi */
 #define VIRTIO_ID_MEDIA			48 /* virtio media */
 
Signature · 1 line
2.34.1
author
#2 lore raw

RFC PATCH 2/3 hw/virtio: add virtio-cpufreq Xen backend

In reply to Baorui.Liu

Report the host pCPU frequency of a pinned Xen vCPU to a guest virtio-cpufreq frontend. Virtio ID 42 is experimental.

Based on the qemu-xen device by Meng Zhang.

---
 MAINTAINERS                        |   7 +
 hw/virtio/Kconfig                  |   5 +
 hw/virtio/meson.build              |   2 +
 hw/virtio/virtio-cpufreq-pci.c     |  82 ++++++++++
 hw/virtio/virtio-cpufreq.c         | 242 +++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c             |   3 +
 hw/virtio/virtio-qmp.c             |   1 +
 hw/virtio/virtio.c                 |   1 +
 include/hw/virtio/virtio-cpufreq.h |  23 +++
 9 files changed, 366 insertions(+)
 create mode 100644 hw/virtio/virtio-cpufreq-pci.c
 create mode 100644 hw/virtio/virtio-cpufreq.c
 create mode 100644 include/hw/virtio/virtio-cpufreq.h
MAINTAINERS +7 -0
index 9012bf476d..dc9014c2be 100644
@@ -2702,6 +2702,13 @@ F: hw/virtio/virtio-crypto.c
 F: hw/virtio/virtio-crypto-pci.c
 F: include/hw/virtio/virtio-crypto.h
 
+virtio-cpufreq
+M: Baorui Liu <[email protected]>
+S: Maintained
+F: hw/virtio/virtio-cpufreq.c
+F: hw/virtio/virtio-cpufreq-pci.c
+F: include/hw/virtio/virtio-cpufreq.h
+
 virtio based memory device
 M: David Hildenbrand <[email protected]>
 S: Supported
hw/virtio/Kconfig +5 -0
index 91cf833407..b6951f5096 100644
@@ -146,3 +146,8 @@ config VHOST_USER_RTC
     bool
     default y
     depends on VIRTIO && VHOST_USER
+
+config VIRTIO_CPUFREQ
+    bool
+    default y
+    depends on VIRTIO && XEN
hw/virtio/meson.build +2 -0
index b59f455a51..03793e1a7c 100644
@@ -55,6 +55,7 @@ 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'))
+system_virtio_ss.add(when: 'CONFIG_VIRTIO_CPUFREQ', if_true: files('virtio-cpufreq.c'))
 
 system_virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
 system_virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c'))
@@ -79,6 +80,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT_HOST', if_true: files('virtio-input
 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_CPUFREQ', if_true: files('virtio-cpufreq-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-cpufreq-pci.c new file +82 -0

82 lines not on this page · raw message

hw/virtio/virtio-cpufreq.c new file +242 -0

242 lines not on this page · raw message

hw/virtio/virtio-pci.c +3 -0
index c73f1f546c..fa677c730a 100644
@@ -312,6 +312,9 @@ static const VirtIOPCIIDInfo virtio_pci_id_info[] = {
         .vdev_id = VIRTIO_ID_RNG,
         .trans_devid = PCI_DEVICE_ID_VIRTIO_RNG,
         .class_id = PCI_CLASS_OTHERS,
+    }, {
+        .vdev_id = VIRTIO_ID_CPUFREQ,
+        .class_id = PCI_CLASS_OTHERS,
     },
 };
 
hw/virtio/virtio-qmp.c +1 -0
index 5aaeebdd96..c9ef4786b1 100644
@@ -643,6 +643,7 @@ VirtioDeviceFeatures *qmp_decode_features(uint16_t device_id,
     case VIRTIO_ID_DMABUF:
     case VIRTIO_ID_PARAM_SERV:
     case VIRTIO_ID_AUDIO_POLICY:
+    case VIRTIO_ID_CPUFREQ:
         break;
     default:
         g_assert_not_reached();
hw/virtio/virtio.c +1 -0
index c9f9e5b511..5edd7bf766 100644
@@ -198,6 +198,7 @@ const char *virtio_device_names[] = {
     [VIRTIO_ID_AUDIO_POLICY] = "virtio-audio-pol",
     [VIRTIO_ID_BT] = "virtio-bluetooth",
     [VIRTIO_ID_GPIO] = "virtio-gpio",
+    [VIRTIO_ID_CPUFREQ] = "virtio-cpufreq",
     [VIRTIO_ID_SPI] = "virtio-spi",
     [VIRTIO_ID_MEDIA] = "virtio-media",
 };
include/hw/virtio/virtio-cpufreq.h new file +23 -0
new file mode 100644
index 0000000000..25d151d7e6
@@ -0,0 +1,23 @@
+/*
+ * Virtio CPU frequency backend (Xen)
+ *
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef QEMU_VIRTIO_CPUFREQ_H
+#define QEMU_VIRTIO_CPUFREQ_H
+
+#include "hw/virtio/virtio.h"
+#include "qom/object.h"
+
+#define TYPE_VIRTIO_CPUFREQ "virtio-cpufreq-device"
+OBJECT_DECLARE_SIMPLE_TYPE(VirtIOCPUFreq, VIRTIO_CPUFREQ)
+
+struct VirtIOCPUFreq {
+    VirtIODevice parent_obj;
+    VirtQueue *vq;
+};
+
+#endif /* QEMU_VIRTIO_CPUFREQ_H */
Signature · 1 line
2.34.1
author
#3 lore raw

RFC PATCH 3/3 hw/virtio: give virtio-cpufreq two MSI-X vectors

In reply to Baorui.Liu

virtio_notify() does not reach the Android frontend unless the PCI device has two interrupt vectors.

Based on a qemu-xen fix by Meng Zhang.

---
 hw/virtio/virtio-cpufreq-pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/virtio/virtio-cpufreq-pci.c +6 -0
index b7066d962c..5715826af2 100644
@@ -9,6 +9,7 @@
 #include "qemu/osdep.h"
 #include "hw/virtio/virtio-pci.h"
 #include "hw/virtio/virtio-cpufreq.h"
+#include "hw/core/qdev-properties.h"
 #include "qapi/error.h"
 #include "qemu/module.h"
 #include "qom/object.h"
@@ -28,6 +29,10 @@ struct VirtIOCPUFreqPCI {
     VirtIOCPUFreq vdev;
 };
 
+static const Property virtio_cpufreq_pci_properties[] = {
+    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
+};
+
 static void virtio_cpufreq_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
 {
     VirtIOCPUFreqPCI *vcpufreq = VIRTIO_CPUFREQ_PCI(vpci_dev);
@@ -44,6 +49,7 @@ static void virtio_cpufreq_pci_class_init(ObjectClass *klass, const void *data)
     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
+    device_class_set_props(dc, virtio_cpufreq_pci_properties);
     k->realize = virtio_cpufreq_pci_realize;
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 
Signature · 1 line
2.34.1