qualcomm/qbox
Loading...
Searching...
No Matches
virtio_gpu.h
1/*
2 * This file is part of libqbox
3 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef _LIBQBOX_COMPONENTS_VIRTIO_GPU_H
9#define _LIBQBOX_COMPONENTS_VIRTIO_GPU_H
10
11#include <cstdint>
12#include <string>
13
14#include <cci_configuration>
15
16#include <libgssync.h>
17
18#include <qemu_gpex/include/qemu_gpex.h>
19
20class QemuVirtioGpu : public qemu_gpex::Device
21{
22public:
23 cci::cci_param<uint8_t> p_outputs;
24 cci::cci_param<uint64_t> p_hostmem_mb;
25 cci::cci_param<bool> p_enable_host_visible;
26
27 void before_end_of_elaboration() override
28 {
29 qemu_gpex::Device::before_end_of_elaboration();
30 get_qemu_dev().set_prop_uint("max_outputs", p_outputs);
31 }
32
33 void gpex_realize(qemu::Bus& bus) override { qemu_gpex::Device::gpex_realize(bus); }
34
35 uint8_t outputs() { return p_outputs.get_value(); }
36
37protected:
38 QemuVirtioGpu(const sc_core::sc_module_name& name, QemuInstance& inst, const char* gpu_type, qemu_gpex* gpex)
39 : QemuVirtioGpu(name, inst, gpu_type, gpex, (const char*)NULL)
40 {
41 }
42
43 QemuVirtioGpu(const sc_core::sc_module_name& name, QemuInstance& inst, const char* gpu_type, qemu_gpex* gpex,
44 const char* device_id)
45 : qemu_gpex::Device(name, inst, gpu_type, device_id)
46 , p_outputs("outputs", 1, "Number of outputs for this gpu")
47 , p_hostmem_mb("hostmem_mb", 2048, "MB to allocate for the host-visible shared-memory region")
48 , p_enable_host_visible("enable_host_visible", false,
49 "Enable virtio-gpu blob resources and the host-visible shared-memory region "
50 "(virglrenderer >= 1.0 / QEMU >= 11). Defaults to false to keep the pre-1.0 "
51 "PCI layout (no SHARED_MEMORY_CFG capability / large BAR), which all guests "
52 "support. Set true for guests that support and benefit from host-visible blob "
53 "resources (e.g. modern Linux/Mesa).")
54 {
55 gpex->add_device(*this);
56 }
57
58 /*
59 * Host-visible / blob-resource setup shared by the 3D GPU variants
60 * (virtio-gpu-gl/cl/qnn). The availability of the blob API is a per-renderer
61 * compile-time decision, so callers must guard these with their own
62 * HAVE_*_RESOURCE_BLOB macro; at runtime the setup is gated by the
63 * enable_host_visible parameter.
64 */
65
66 /* Call from the derived constructor: adds the memfd host-visible backend. */
67 void setup_host_visible_backend()
68 {
69 if (!p_enable_host_visible) {
70 return;
71 }
72 m_inst.add_arg("-object");
73 std::string memory_object = "memory-backend-memfd,id=mem1,size=" + std::to_string(p_hostmem_mb.get_value()) +
74 "M";
75 m_inst.add_arg(memory_object.c_str());
76 m_inst.add_arg("-machine");
77 m_inst.add_arg("memory-backend=mem1");
78 }
79
80 /* Call from the derived before_end_of_elaboration(): sets the device props. */
81 void apply_host_visible_props()
82 {
83 if (!p_enable_host_visible) {
84 return;
85 }
86 get_qemu_dev().set_prop_bool("blob", true);
87 /*
88 * QEMU's "hostmem" is a DEFINE_PROP_SIZE property expressed in BYTES, but
89 * p_hostmem_mb is in MB. Convert (64-bit: 2048 * 1MB overflows int).
90 */
91 get_qemu_dev().set_prop_int("hostmem", (int64_t)p_hostmem_mb.get_value() * 1024 * 1024);
92 }
93};
94#endif
This class encapsulates a libqemu-cxx qemu::LibQemu instance. It handles QEMU parameters and instance...
Definition qemu-instance.h:89
Definition libqemu-cxx.h:793
Definition qemu_gpex.h:35