Compare commits

...
Author SHA1 Message Date
Lingfeng Yang f818886828 virto-gpu 3d initial migration support
Bug: 162770284

This CL adds support for virtio-gpu-asg migration. Currently tested only
with quickboot so far; support for loading snapshots while emulator is
running is still not resolved.

The main gotcha-invariant that this CL relies on is that we save and
load renderer state and host-visible blob resources _before_ we save and
load virtio-gpu device state itself. This is necessary for migration to
work because we first need to reallocate new host pointers for blob
resources before the blob resources can be properly deserialized from
disk and loaded in hypervisor's RAM slots.

The other special thing is that this needs to save the GPA's
associated with guest scatterlists per virtio-gpu resource and use
cpu_physical_memory_map to restore on load.

Otherwise, it's a pretty straightforward save/load of the virtio-gpu 3d
renderer state and reuses gfxstream's existing snapshot save/load
functions.

Note that this also assumes the virtqueue is empty and there are no
outstanding commands for virtio-gpu in the various vq's.

Also, update the snapshot version.

Change-Id: I3b66cd98bc4eaa17d0b144cf7f70b57c1a17af05
2023-02-12 08:03:10 +02:00
Lingfeng Yang 9748378628 virtio-gpu: add resource create blob / map + unmap blob support
Bug: 162770284

Needed for Vulkan support and zero-copy kernel bypass command buffers
(virtio-gpu-asg)

Change-Id: I3e94689cd56317df9dca0eb68ff42ae2b5b5df32
2023-02-12 08:03:10 +02:00
osy 1fc3358fee virgl: support goldfish pipe proxy implementation
Taken from Android Emulator code.
2023-02-12 08:03:10 +02:00
osy 2b0fd418aa virgl: make libvirglrenderer calls indirect
This will allow support for dynamic switching of different virglrenderer
implementations.
2023-02-11 22:56:37 +02:00
osy d66b46f0be virtio-gpu: load/save callback to instance methods
We wish to support additional functionality for load/save in sub-classes.
2023-02-11 22:56:37 +02:00
osy 5786281b5f hvf: support user-backed mapping
Taken from Android Emulator code.
2023-02-11 22:56:37 +02:00
Lingfeng Yang f9a11d22b1 Allow address space device to work w/ TCG
bug: 156559140
bug: https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=52082

adds gpa2hva along with user backed ram map/unmap in a generic way so
that it can work with tcg

Test: Launch Fuchsia guest with TCG x86, Vulkan host rendering works
Change-Id: Ifa53dd89a8193132ed49a3f7d286f3992ccddc8f
2023-02-11 22:56:37 +02:00
Lingfeng Yang e97555efb5 [qemu] API to directly control hypervisor mappings
This is convenient for if we want to add/remove host pointers
to the guest at runtime.

TODO: Need to expose this API to our opengl driver/translator
somewhere

Change-Id: I47fc36d33ff84e501fef7e37f3716d9b1028e35a
2023-02-11 22:56:37 +02:00
Lingfeng Yang 3f949cf30c [qemu] API to separately manage host backing of MemoryRegion
- memory_region_init_ram_user_backed
- memory_region_init_is_user_backed

This tells QEMU that we are reserving memory regions where
we will manage the hypervisor backing by ourselves.

Change-Id: I11b273565f021890313fdb7f54c49668f46edff0
2023-02-11 21:00:27 +02:00
17 changed files with 1368 additions and 160 deletions

No files matched your search

+264 -70
View File
@@ -58,21 +58,33 @@
#include "sysemu/runstate.h"
#include "qemu/guest-random.h"
#include "hw/boards.h"
#include "exec/memory-remap.h"
#include "exec/ram_addr.h"
HVFState *hvf_state;
/* Memory slots */
hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size)
{
#define HVF_MAX_SLOTS 512
#ifdef DEBUG_HVF
#define DPRINTF(fmt, ...) \
do { fprintf(stdout, fmt, ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) \
do { } while (0)
#endif
#define qemu_abort(...) do { g_error(__VA_ARGS__); g_abort(); } while (0)
pthread_rwlock_t mem_lock = PTHREAD_RWLOCK_INITIALIZER;
hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t end) {
hvf_slot *slot;
int x;
for (x = 0; x < hvf_state->num_slots; ++x) {
slot = &hvf_state->slots[x];
if (slot->size && start < (slot->start + slot->size) &&
(start + size) > slot->start) {
if (slot->size && start < (slot->start + slot->size) && end > slot->start)
return slot;
}
}
return NULL;
}
@@ -82,22 +94,236 @@ struct mac_slot {
uint64_t size;
uint64_t gpa_start;
uint64_t gva;
void* hva;
};
struct mac_slot mac_slots[32];
struct mac_slot mac_slots[HVF_MAX_SLOTS];
static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags)
{
#define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
void* hvf_gpa2hva(uint64_t gpa, bool* found) {
struct mac_slot *mslot;
*found = false;
for (uint32_t i = 0; i < HVF_MAX_SLOTS; i++) {
mslot = &mac_slots[i];
if (!mslot->present) continue;
if (gpa >= mslot->gpa_start &&
gpa < mslot->gpa_start + mslot->size) {
*found = true;
return (void*)((char*)(mslot->hva) + (gpa - mslot->gpa_start));
}
}
return 0;
}
#define min(a, b) ((a) < (b) ? (a) : (b))
int hvf_hva2gpa(void* hva, uint64_t length, int array_size,
uint64_t* gpa, uint64_t* size) {
struct mac_slot *mslot;
int count = 0;
for (uint32_t i = 0; i < HVF_MAX_SLOTS; i++) {
mslot = &mac_slots[i];
if (!mslot->present) continue;
uintptr_t hva_start_num = (uintptr_t)mslot->hva;
uintptr_t hva_num = (uintptr_t)hva;
// Start of this hva region is in this slot.
if (hva_num >= hva_start_num &&
hva_num < hva_start_num + mslot->size) {
if (count < array_size) {
gpa[count] = mslot->gpa_start + (hva_num - hva_start_num);
size[count] = min(length,
mslot->size - (hva_num - hva_start_num));
}
count++;
// End of this hva region is in this slot.
// Its start is outside of this slot.
} else if (hva_num + length <= hva_start_num + mslot->size &&
hva_num + length > hva_start_num) {
if (count < array_size) {
gpa[count] = mslot->gpa_start;
size[count] = hva_num + length - hva_start_num;
}
count++;
// This slot belongs to this hva region completely.
} else if (hva_num + length > hva_start_num + mslot->size &&
hva_num < hva_start_num) {
if (count < array_size) {
gpa[count] = mslot->gpa_start;
size[count] = mslot->size;
}
count++;
}
}
return count;
}
static hvf_slot* hvf_next_free_slot() {
hvf_slot* mem = 0;
int x;
for (x = 0; x < hvf_state->num_slots; ++x) {
mem = &hvf_state->slots[x];
if (!mem->size) {
return mem;
}
}
return mem;
}
int __hvf_set_memory(hvf_slot *slot);
int __hvf_set_memory_with_flags_locked(hvf_slot *slot, hv_memory_flags_t flags);
int hvf_map_safe(void* hva, uint64_t gpa, uint64_t size, uint64_t flags) {
pthread_rwlock_wrlock(&mem_lock);
DPRINTF("%s: hva: [%p 0x%llx] gpa: [0x%llx 0x%llx]\n", __func__,
hva, (unsigned long long)(uintptr_t)(((char*)hva) + size),
(unsigned long long)gpa,
(unsigned long long)gpa + size);
hvf_slot *mem;
mem = hvf_find_overlap_slot(gpa, gpa + size);
if (mem &&
mem->mem == hva &&
mem->start == gpa &&
mem->size == size) {
pthread_rwlock_unlock(&mem_lock);
return HV_SUCCESS;
} else if (mem &&
mem->start == gpa &&
mem->size == size) {
// unmap existing mapping, but only if it coincides
mem->size = 0;
__hvf_set_memory_with_flags_locked(mem, 0);
} else if (mem) {
// TODO: Manage and support partially-overlapping user-backed RAM mappings.
// for now, consider it fatal.
pthread_rwlock_unlock(&mem_lock);
qemu_abort("%s: FATAL: tried to map [0x%llx 0x%llx) to %p "
"while it was mapped to [0x%llx 0x%llx), %p",
__func__,
(unsigned long long)gpa,
(unsigned long long)gpa + size,
hva,
(unsigned long long)mem->start,
(unsigned long long)mem->start + mem->size,
mem->mem);
}
mem = hvf_next_free_slot();
if (mem->size) {
qemu_abort("%s: no free slots\n", __func__);
}
mem->mem = (uint8_t*)hva;
mem->start = gpa;
mem->size = size;
int res = __hvf_set_memory_with_flags_locked(mem, (hv_memory_flags_t)flags);
pthread_rwlock_unlock(&mem_lock);
return res;
}
int hvf_unmap_safe(uint64_t gpa, uint64_t size) {
DPRINTF("%s: gpa: [0x%llx 0x%llx]\n", __func__,
(unsigned long long)gpa,
(unsigned long long)gpa + size);
pthread_rwlock_wrlock(&mem_lock);
hvf_slot *mem;
int res = 0;
mem = hvf_find_overlap_slot(gpa, gpa + size);
if (mem &&
(mem->start != gpa ||
mem->size != size)) {
pthread_rwlock_unlock(&mem_lock);
qemu_abort("%s: tried to unmap [0x%llx 0x%llx) but partially overlapping "
"[0x%llx 0x%llx), %p was encountered",
__func__, gpa, gpa + size,
mem->start, mem->start + mem->size, mem->mem);
} else if (mem) {
mem->size = 0;
res = __hvf_set_memory_with_flags_locked(mem, 0);
} else {
// fall through, allow res to be 0 still if slot was not found.
}
pthread_rwlock_unlock(&mem_lock);
return res;
}
int hvf_protect_safe(uint64_t gpa, uint64_t size, uint64_t flags) {
pthread_rwlock_wrlock(&mem_lock);
int res = hv_vm_protect(gpa, size, flags);
pthread_rwlock_unlock(&mem_lock);
return res;
}
int hvf_remap_safe(void* hva, uint64_t gpa, uint64_t size, uint64_t flags) {
pthread_rwlock_wrlock(&mem_lock);
int res = hv_vm_unmap(gpa, size);
assert_hvf_ok(res);
res = hv_vm_map(hva, gpa, size, flags);
assert_hvf_ok(res);
pthread_rwlock_unlock(&mem_lock);
return res;
}
// API for adding and removing mappings of guest RAM and host addrs.
// Implementation depends on the hypervisor.
static hv_memory_flags_t user_backed_flags_to_hvf_flags(int flags) {
hv_memory_flags_t hvf_flags = 0;
if (flags & USER_BACKED_RAM_FLAGS_READ) {
hvf_flags |= HV_MEMORY_READ;
}
if (flags & USER_BACKED_RAM_FLAGS_WRITE) {
hvf_flags |= HV_MEMORY_WRITE;
}
if (flags & USER_BACKED_RAM_FLAGS_EXEC) {
hvf_flags |= HV_MEMORY_EXEC;
}
return hvf_flags;
}
static void hvf_user_backed_ram_map(uint64_t gpa, void* hva, uint64_t size, int flags) {
hvf_map_safe(hva, gpa, size, user_backed_flags_to_hvf_flags(flags));
}
static void hvf_user_backed_ram_unmap(uint64_t gpa, uint64_t size) {
hvf_unmap_safe(gpa, size);
}
int __hvf_set_memory(hvf_slot *slot) {
pthread_rwlock_wrlock(&mem_lock);
int res = __hvf_set_memory_with_flags_locked(slot, HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC);
pthread_rwlock_unlock(&mem_lock);
return res;
}
int __hvf_set_memory_with_flags_locked(hvf_slot *slot, hv_memory_flags_t flags) {
struct mac_slot *macslot;
hv_return_t ret;
macslot = &mac_slots[slot->slot_id];
if (macslot->present) {
if (macslot->size != slot->size) {
macslot->present = 0;
ret = hv_vm_unmap(macslot->gpa_start, macslot->size);
assert_hvf_ok(ret);
DPRINTF("%s: hv_vm_unmap for gpa [0x%llx 0x%llx]\n", __func__,
(unsigned long long)macslot->gpa_start,
(unsigned long long)(macslot->gpa_start + macslot->size));
int unmapres = hv_vm_unmap(macslot->gpa_start, macslot->size);
assert_hvf_ok(unmapres);
}
}
@@ -108,93 +334,58 @@ static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags)
macslot->present = 1;
macslot->gpa_start = slot->start;
macslot->size = slot->size;
ret = hv_vm_map(slot->mem, slot->start, slot->size, flags);
assert_hvf_ok(ret);
macslot->hva = slot->mem;
DPRINTF("%s: hv_vm_map for hva 0x%llx gpa [0x%llx 0x%llx]\n", __func__,
(unsigned long long)(slot->mem),
(unsigned long long)macslot->gpa_start,
(unsigned long long)(macslot->gpa_start + macslot->size));
int mapres = (hv_vm_map(slot->mem, slot->start, slot->size, flags));
assert_hvf_ok(mapres);
return 0;
}
static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
{
static void hvf_set_phys_mem(MemoryRegionSection* section, bool add) {
hvf_slot *mem;
MemoryRegion *area = section->mr;
bool writable = !area->readonly && !area->rom_device;
hv_memory_flags_t flags;
uint64_t page_size = qemu_real_host_page_size();
if (!memory_region_is_ram(area)) {
if (writable) {
return;
} else if (!memory_region_is_romd(area)) {
/*
* If the memory device is not in romd_mode, then we actually want
* to remove the hvf memory slot so all accesses will trap.
*/
add = false;
}
}
if (!QEMU_IS_ALIGNED(int128_get64(section->size), page_size) ||
!QEMU_IS_ALIGNED(section->offset_within_address_space, page_size)) {
/* Not page aligned, so we can not map as RAM */
add = false;
}
if (!memory_region_is_ram(area)) return;
if (memory_region_is_user_backed(area)) return;
mem = hvf_find_overlap_slot(
section->offset_within_address_space,
int128_get64(section->size));
section->offset_within_address_space + int128_get64(section->size));
if (mem && add) {
if (mem->size == int128_get64(section->size) &&
mem->start == section->offset_within_address_space &&
mem->mem == (memory_region_get_ram_ptr(area) +
section->offset_within_region)) {
return; /* Same region was attempted to register, go away. */
}
mem->start == section->offset_within_address_space &&
mem->mem == (memory_region_get_ram_ptr(area) + section->offset_within_region))
return; // Same region was attempted to register, go away.
}
/* Region needs to be reset. set the size to 0 and remap it. */
// Region needs to be reset. set the size to 0 and remap it.
if (mem) {
mem->size = 0;
if (do_hvf_set_memory(mem, 0)) {
error_report("Failed to reset overlapping slot");
abort();
if (__hvf_set_memory(mem)) {
qemu_abort("%s: Failed to reset overlapping slot\n", __func__);
}
}
if (!add) {
return;
}
if (!add) return;
if (area->readonly ||
(!memory_region_is_ram(area) && memory_region_is_romd(area))) {
flags = HV_MEMORY_READ | HV_MEMORY_EXEC;
} else {
flags = HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC;
}
// Now make a new slot.
/* Now make a new slot. */
int x;
mem = hvf_next_free_slot();
for (x = 0; x < hvf_state->num_slots; ++x) {
mem = &hvf_state->slots[x];
if (!mem->size) {
break;
}
}
if (x == hvf_state->num_slots) {
error_report("No free slots");
abort();
if (!mem) {
qemu_abort("%s: no free slots\n", __func__);
}
mem->size = int128_get64(section->size);
mem->mem = memory_region_get_ram_ptr(area) + section->offset_within_region;
mem->start = section->offset_within_address_space;
mem->region = area;
if (do_hvf_set_memory(mem, flags)) {
error_report("Error registering new memory slot");
abort();
if (__hvf_set_memory(mem)) {
qemu_abort("%s: error regsitering new memory slot\n", __func__);
}
}
@@ -324,6 +515,9 @@ static int hvf_accel_init(MachineState *ms)
hvf_state = s;
memory_listener_register(&hvf_memory_listener, &address_space_memory);
qemu_set_user_backed_mapping_funcs(
hvf_user_backed_ram_map,
hvf_user_backed_ram_unmap);
return hvf_arch_init();
}
+2 -1
View File
@@ -165,7 +165,8 @@ virtio_gpu_base_device_realize(DeviceState *qdev,
return false;
}
if (virtio_gpu_virgl_enabled(g->conf)) {
if (virtio_gpu_virgl_enabled(g->conf) &&
!virtio_gpu_goldfish_pipe_enabled(g->conf)) {
error_setg(&g->migration_blocker, "virgl is not yet migratable");
if (migrate_add_blocker(g->migration_blocker, errp) < 0) {
error_free(g->migration_blocker);
+58 -2
View File
@@ -18,21 +18,25 @@
#include "qapi/error.h"
#include "sysemu/sysemu.h"
#include "hw/virtio/virtio.h"
#ifdef CONFIG_ANDROID
#include "hw/virtio/virtio-goldfish-pipe.h"
#endif
#include "hw/virtio/virtio-gpu.h"
#include "hw/virtio/virtio-gpu-bswap.h"
#include "hw/virtio/virtio-gpu-pixman.h"
#include "hw/qdev-properties.h"
#include <virglrenderer.h>
#include "hw/virtio/virtio-gpu-virgl.h"
static void virtio_gpu_gl_update_cursor_data(VirtIOGPU *g,
struct virtio_gpu_scanout *s,
uint32_t resource_id)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
uint32_t width, height;
uint32_t pixels, *data;
data = virgl_renderer_get_cursor_data(resource_id, &width, &height);
data = gl->virgl->virgl_renderer_get_cursor_data(resource_id, &width, &height);
if (!data) {
return;
}
@@ -104,9 +108,46 @@ static void virtio_gpu_gl_reset(VirtIODevice *vdev)
}
}
static int virtio_gpu_gl_save(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field, JSONWriter *vmdesc)
{
VirtIOGPU *g = opaque;
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
if (gl->virgl->virgl_renderer_save_snapshot) {
// First save the renderer state, then the ram slots, because when we
// load, the renderer state and host pointers backing blob resources
// need to be restored first before the ram slots corresponding to the
// blob resources themselves.
gl->virgl->virgl_renderer_save_snapshot(f);
#ifdef CONFIG_ANDROID
virtio_gpu_save_ram_slots(f, g);
#endif
}
return virtio_gpu_save(f, opaque, size, field, vmdesc);
}
static int virtio_gpu_gl_load(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field)
{
VirtIOGPU *g = opaque;
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
if (gl->virgl->virgl_renderer_load_snapshot) {
gl->virgl->virgl_renderer_load_snapshot(f);
#ifdef CONFIG_ANDROID
virtio_gpu_load_ram_slots(f, g);
#endif
}
return virtio_gpu_load(f, opaque, size, field);
}
static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
{
VirtIOGPU *g = VIRTIO_GPU(qdev);
VirtIOGPUGL *gl = VIRTIO_GPU_GL(qdev);
#if HOST_BIG_ENDIAN
error_setg(errp, "virgl is not supported on bigendian platforms");
@@ -123,6 +164,15 @@ static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
return;
}
#ifdef CONFIG_ANDROID
if (virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
gl->virgl = get_goldfish_pipe_virgl_renderer_virtio_interface();
} else
#endif
{
gl->virgl = get_default_virtio_interface();
}
g->parent_obj.conf.flags |= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED);
VIRTIO_GPU_BASE(g)->virtio_config.num_capsets =
virtio_gpu_virgl_get_num_capsets(g);
@@ -133,6 +183,10 @@ static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
static Property virtio_gpu_gl_properties[] = {
DEFINE_PROP_BIT("stats", VirtIOGPU, parent_obj.conf.flags,
VIRTIO_GPU_FLAG_STATS_ENABLED, false),
#ifdef CONFIG_ANDROID
DEFINE_PROP_BIT("goldfish-pipe", VirtIOGPU, parent_obj.conf.flags,
VIRTIO_GPU_FLAG_GOLDFISH_PIPE_ENABLED, false),
#endif
DEFINE_PROP_END_OF_LIST(),
};
@@ -147,6 +201,8 @@ static void virtio_gpu_gl_class_init(ObjectClass *klass, void *data)
vgc->handle_ctrl = virtio_gpu_gl_handle_ctrl;
vgc->process_cmd = virtio_gpu_virgl_process_cmd;
vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data;
vgc->gpu_save = virtio_gpu_gl_save;
vgc->gpu_load = virtio_gpu_gl_load;
vdc->realize = virtio_gpu_gl_device_realize;
vdc->reset = virtio_gpu_gl_reset;
+84
View File
@@ -20,6 +20,7 @@
#include "hw/virtio/virtio-bus.h"
#include "hw/virtio/virtio-gpu-pci.h"
#include "qom/object.h"
#include "standard-headers/linux/virtio_pci.h"
#define TYPE_VIRTIO_GPU_GL_PCI "virtio-gpu-gl-pci"
typedef struct VirtIOGPUGLPCI VirtIOGPUGLPCI;
@@ -31,6 +32,88 @@ struct VirtIOGPUGLPCI {
VirtIOGPUGL vdev;
};
static void virtio_gpu_pci_gl_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
{
VirtIOGPUGLPCI *vgpu = VIRTIO_GPU_GL_PCI(vpci_dev);
VirtIOGPUGL *gl = &vgpu->vdev;
VirtIOGPUBase *g = &gl->parent_obj.parent_obj;
DeviceState *vdev = DEVICE(&vgpu->vdev);
int i;
// Cool way to lay things out that should not affect stuff
// and is compat with the current android11-5.4 kernel:
// | 0 1 : modern | 2 msix | 4 5 hostshm |
//
// This is similar to the crosvm
// virtio gpu pci bar layout, same except maybe for different msix bar #.
vpci_dev->modern_mem_bar_idx = 0; // Overwritten: legacy io bar idx
vpci_dev->msix_bar_idx = 2;
// Not considered: modern io bar idx (only used in MODERN_PIO_NOTIFY,
// which doesn't seem to be used in virtio-gpu)
vpci_dev->hostshm_mem_bar_idx = 4;
// Note that in future versions of Linux kernels, the bar may be negotiated
// via some other mechanism based off the shm_cap logic below.
#define VIRTIO_GPU_PCI_HOST_COHERENT_BAR_SIZE (1ULL << 32ULL)
memory_region_init_ram_user_backed(
&gl->host_coherent_memory, OBJECT(gl),
"virtio-gpu-host-coherent",
VIRTIO_GPU_PCI_HOST_COHERENT_BAR_SIZE);
// Add shm cap to the guest
// (Doesn't seem to be used in 5.4, but is a way to be fwd compatible
// with newer kernels)
struct virtio_pci_cap64 shm_cap;
uint32_t mask32 = ~0;
shm_cap.cap.cap_len = sizeof(shm_cap);
shm_cap.cap.cfg_type = 8; // PciCapabilityType::SharedMemoryConfig
shm_cap.cap.bar = vpci_dev->hostshm_mem_bar_idx;
shm_cap.cap.length =
cpu_to_le32(mask32 & (int128_get64(gl->host_coherent_memory.size)));
shm_cap.length_hi =
cpu_to_le32(mask32 & (int128_get64(gl->host_coherent_memory.size) >> 32ULL));
shm_cap.cap.offset = mask32 & 0;
shm_cap.offset_hi = mask32 & 0;
#define VIRTIO_GPU_PCI_CAP_HOSTSHM_ID 1
shm_cap.cap.id = VIRTIO_GPU_PCI_CAP_HOSTSHM_ID;
pci_register_bar(&vpci_dev->pci_dev, vpci_dev->hostshm_mem_bar_idx,
PCI_BASE_ADDRESS_SPACE_MEMORY |
PCI_BASE_ADDRESS_MEM_TYPE_64, &gl->host_coherent_memory);
int offset = pci_add_capability(
&vpci_dev->pci_dev, PCI_CAP_ID_VNDR, 0,
shm_cap.cap.cap_len, &error_abort);
// Add the mem cap to virtio-gpu-pci proxy's configuration.
memcpy(vpci_dev->pci_dev.config + offset + PCI_CAP_FLAGS,
&shm_cap.cap.cap_len,
shm_cap.cap.cap_len - PCI_CAP_FLAGS);
virtio_pci_force_virtio_1(vpci_dev);
if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) {
return;
}
for (i = 0; i < g->conf.max_outputs; i++) {
object_property_set_link(OBJECT(g->scanout[i].con), "device",
OBJECT(vpci_dev), &error_abort);
}
}
static void virtio_gpu_pci_gl_class_init(ObjectClass *klass, void *data)
{
VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
k->realize = virtio_gpu_pci_gl_realize;
}
static void virtio_gpu_gl_initfn(Object *obj)
{
VirtIOGPUGLPCI *dev = VIRTIO_GPU_GL_PCI(obj);
@@ -45,6 +128,7 @@ static const VirtioPCIDeviceTypeInfo virtio_gpu_gl_pci_info = {
.parent = TYPE_VIRTIO_GPU_PCI_BASE,
.instance_size = sizeof(VirtIOGPUGLPCI),
.instance_init = virtio_gpu_gl_initfn,
.class_init = virtio_gpu_pci_gl_class_init,
};
module_obj(TYPE_VIRTIO_GPU_GL_PCI);
module_kconfig(VIRTIO_PCI);
+432 -59
View File
@@ -16,14 +16,17 @@
#include "trace.h"
#include "hw/virtio/virtio.h"
#include "hw/virtio/virtio-gpu.h"
#include "exec/memory-remap.h"
#include "migration/qemu-file-types.h"
#include <virglrenderer.h>
#include "hw/virtio/virtio-gpu-virgl.h"
static struct virgl_renderer_callbacks virtio_gpu_3d_cbs;
static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_create_2d c2d;
struct virgl_renderer_resource_create_args args;
@@ -42,12 +45,13 @@ static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
args.last_level = 0;
args.nr_samples = 0;
args.flags = VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP;
virgl_renderer_resource_create(&args, NULL, 0);
gl->virgl->virgl_renderer_resource_create(&args, NULL, 0);
}
static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_create_3d c3d;
struct virgl_renderer_resource_create_args args;
@@ -66,12 +70,13 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
args.last_level = c3d.last_level;
args.nr_samples = c3d.nr_samples;
args.flags = c3d.flags;
virgl_renderer_resource_create(&args, NULL, 0);
gl->virgl->virgl_renderer_resource_create(&args, NULL, 0);
}
static void virgl_cmd_resource_unref(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_unref unref;
struct iovec *res_iovs = NULL;
int num_iovs = 0;
@@ -79,37 +84,39 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
VIRTIO_GPU_FILL_CMD(unref);
trace_virtio_gpu_cmd_res_unref(unref.resource_id);
virgl_renderer_resource_detach_iov(unref.resource_id,
gl->virgl->virgl_renderer_resource_detach_iov(unref.resource_id,
&res_iovs,
&num_iovs);
if (res_iovs != NULL && num_iovs != 0) {
virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs);
}
virgl_renderer_resource_unref(unref.resource_id);
gl->virgl->virgl_renderer_resource_unref(unref.resource_id);
}
static void virgl_cmd_context_create(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_ctx_create cc;
VIRTIO_GPU_FILL_CMD(cc);
trace_virtio_gpu_cmd_ctx_create(cc.hdr.ctx_id,
cc.debug_name);
virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen,
cc.debug_name);
gl->virgl->virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen,
cc.debug_name);
}
static void virgl_cmd_context_destroy(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_ctx_destroy cd;
VIRTIO_GPU_FILL_CMD(cd);
trace_virtio_gpu_cmd_ctx_destroy(cd.hdr.ctx_id);
virgl_renderer_context_destroy(cd.hdr.ctx_id);
gl->virgl->virgl_renderer_context_destroy(cd.hdr.ctx_id);
}
static void virtio_gpu_rect_update(VirtIOGPU *g, int idx, int x, int y,
@@ -119,7 +126,9 @@ static void virtio_gpu_rect_update(VirtIOGPU *g, int idx, int x, int y,
return;
}
dpy_gl_update(g->parent_obj.scanout[idx].con, x, y, width, height);
if (!virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
dpy_gl_update(g->parent_obj.scanout[idx].con, x, y, width, height);
}
}
static void virgl_cmd_resource_flush(VirtIOGPU *g,
@@ -137,6 +146,13 @@ static void virgl_cmd_resource_flush(VirtIOGPU *g,
continue;
}
virtio_gpu_rect_update(g, i, rf.r.x, rf.r.y, rf.r.width, rf.r.height);
#ifdef CONFIG_ANDROID
if (i == 0 && virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
stream_renderer_flush_resource_and_readback(rf.resource_id, rf.r.x, rf.r.y,
rf.r.width, rf.r.height,
NULL, 0);
}
#endif
}
}
@@ -172,6 +188,7 @@ static GLuint virgl_borrow_texture_for_scanout(uint32_t id, bool *y_0_top,
static void virgl_cmd_set_scanout(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_set_scanout ss;
VIRTIO_GPU_FILL_CMD(ss);
@@ -189,12 +206,14 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
if (ss.resource_id && ss.r.width && ss.r.height) {
qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
ss.r.width, ss.r.height);
virgl_renderer_force_ctx_0();
dpy_gl_scanout_texture(
g->parent_obj.scanout[ss.scanout_id].con, ss.resource_id,
virgl_borrow_texture_for_scanout,
ss.r.x, ss.r.y, ss.r.width, ss.r.height);
} else {
gl->virgl->virgl_renderer_force_ctx_0();
if (!virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
dpy_gl_scanout_texture(
g->parent_obj.scanout[ss.scanout_id].con, ss.resource_id,
virgl_borrow_texture_for_scanout,
ss.r.x, ss.r.y, ss.r.width, ss.r.height);
}
} else if (!virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
dpy_gfx_replace_surface(
g->parent_obj.scanout[ss.scanout_id].con, NULL);
dpy_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
@@ -205,6 +224,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
static void virgl_cmd_submit_3d(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_cmd_submit cs;
void *buf;
size_t s;
@@ -227,7 +247,7 @@ static void virgl_cmd_submit_3d(VirtIOGPU *g,
g->stats.bytes_3d += cs.size;
}
virgl_renderer_submit_cmd(buf, cs.hdr.ctx_id, cs.size / 4);
gl->virgl->virgl_renderer_submit_cmd(buf, cs.hdr.ctx_id, cs.size / 4);
out:
g_free(buf);
@@ -236,6 +256,7 @@ out:
static void virgl_cmd_transfer_to_host_2d(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_transfer_to_host_2d t2d;
struct virtio_gpu_box box;
@@ -249,71 +270,75 @@ static void virgl_cmd_transfer_to_host_2d(VirtIOGPU *g,
box.h = t2d.r.height;
box.d = 1;
virgl_renderer_transfer_write_iov(t2d.resource_id,
0,
0,
0,
0,
(struct virgl_box *)&box,
t2d.offset, NULL, 0);
gl->virgl->virgl_renderer_transfer_write_iov(t2d.resource_id,
0,
0,
0,
0,
(struct virgl_box *)&box,
t2d.offset, NULL, 0);
}
static void virgl_cmd_transfer_to_host_3d(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_transfer_host_3d t3d;
VIRTIO_GPU_FILL_CMD(t3d);
trace_virtio_gpu_cmd_res_xfer_toh_3d(t3d.resource_id);
virgl_renderer_transfer_write_iov(t3d.resource_id,
t3d.hdr.ctx_id,
t3d.level,
t3d.stride,
t3d.layer_stride,
(struct virgl_box *)&t3d.box,
t3d.offset, NULL, 0);
gl->virgl->virgl_renderer_transfer_write_iov(t3d.resource_id,
t3d.hdr.ctx_id,
t3d.level,
t3d.stride,
t3d.layer_stride,
(struct virgl_box *)&t3d.box,
t3d.offset, NULL, 0);
}
static void
virgl_cmd_transfer_from_host_3d(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_transfer_host_3d tf3d;
VIRTIO_GPU_FILL_CMD(tf3d);
trace_virtio_gpu_cmd_res_xfer_fromh_3d(tf3d.resource_id);
virgl_renderer_transfer_read_iov(tf3d.resource_id,
tf3d.hdr.ctx_id,
tf3d.level,
tf3d.stride,
tf3d.layer_stride,
(struct virgl_box *)&tf3d.box,
tf3d.offset, NULL, 0);
gl->virgl->virgl_renderer_transfer_read_iov(tf3d.resource_id,
tf3d.hdr.ctx_id,
tf3d.level,
tf3d.stride,
tf3d.layer_stride,
(struct virgl_box *)&tf3d.box,
tf3d.offset, NULL, 0);
}
static void virgl_resource_attach_backing(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_attach_backing att_rb;
struct iovec *res_iovs;
uint32_t res_niov;
int ret;
uint64_t* addrs;
VIRTIO_GPU_FILL_CMD(att_rb);
trace_virtio_gpu_cmd_res_back_attach(att_rb.resource_id);
ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
cmd, NULL, &res_iovs, &res_niov);
cmd, &addrs, &res_iovs, &res_niov);
if (ret != 0) {
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
return;
}
ret = virgl_renderer_resource_attach_iov(att_rb.resource_id,
res_iovs, res_niov);
ret = gl->virgl->virgl_renderer_resource_attach_iov_with_addrs(att_rb.resource_id,
res_iovs, res_niov, addrs);
if (ret != 0)
virtio_gpu_cleanup_mapping_iov(g, res_iovs, res_niov);
@@ -322,6 +347,7 @@ static void virgl_resource_attach_backing(VirtIOGPU *g,
static void virgl_resource_detach_backing(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_detach_backing detach_rb;
struct iovec *res_iovs = NULL;
int num_iovs = 0;
@@ -329,9 +355,9 @@ static void virgl_resource_detach_backing(VirtIOGPU *g,
VIRTIO_GPU_FILL_CMD(detach_rb);
trace_virtio_gpu_cmd_res_back_detach(detach_rb.resource_id);
virgl_renderer_resource_detach_iov(detach_rb.resource_id,
&res_iovs,
&num_iovs);
gl->virgl->virgl_renderer_resource_detach_iov(detach_rb.resource_id,
&res_iovs,
&num_iovs);
if (res_iovs == NULL || num_iovs == 0) {
return;
}
@@ -342,30 +368,33 @@ static void virgl_resource_detach_backing(VirtIOGPU *g,
static void virgl_cmd_ctx_attach_resource(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_ctx_resource att_res;
VIRTIO_GPU_FILL_CMD(att_res);
trace_virtio_gpu_cmd_ctx_res_attach(att_res.hdr.ctx_id,
att_res.resource_id);
virgl_renderer_ctx_attach_resource(att_res.hdr.ctx_id, att_res.resource_id);
gl->virgl->virgl_renderer_ctx_attach_resource(att_res.hdr.ctx_id, att_res.resource_id);
}
static void virgl_cmd_ctx_detach_resource(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_ctx_resource det_res;
VIRTIO_GPU_FILL_CMD(det_res);
trace_virtio_gpu_cmd_ctx_res_detach(det_res.hdr.ctx_id,
det_res.resource_id);
virgl_renderer_ctx_detach_resource(det_res.hdr.ctx_id, det_res.resource_id);
gl->virgl->virgl_renderer_ctx_detach_resource(det_res.hdr.ctx_id, det_res.resource_id);
}
static void virgl_cmd_get_capset_info(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_get_capset_info info;
struct virtio_gpu_resp_capset_info resp;
@@ -374,14 +403,14 @@ static void virgl_cmd_get_capset_info(VirtIOGPU *g,
memset(&resp, 0, sizeof(resp));
if (info.capset_index == 0) {
resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL;
virgl_renderer_get_cap_set(resp.capset_id,
&resp.capset_max_version,
&resp.capset_max_size);
gl->virgl->virgl_renderer_get_cap_set(resp.capset_id,
&resp.capset_max_version,
&resp.capset_max_size);
} else if (info.capset_index == 1) {
resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL2;
virgl_renderer_get_cap_set(resp.capset_id,
&resp.capset_max_version,
&resp.capset_max_size);
gl->virgl->virgl_renderer_get_cap_set(resp.capset_id,
&resp.capset_max_version,
&resp.capset_max_size);
} else {
resp.capset_max_version = 0;
resp.capset_max_size = 0;
@@ -393,12 +422,13 @@ static void virgl_cmd_get_capset_info(VirtIOGPU *g,
static void virgl_cmd_get_capset(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_get_capset gc;
struct virtio_gpu_resp_capset *resp;
uint32_t max_ver, max_size;
VIRTIO_GPU_FILL_CMD(gc);
virgl_renderer_get_cap_set(gc.capset_id, &max_ver,
gl->virgl->virgl_renderer_get_cap_set(gc.capset_id, &max_ver,
&max_size);
if (!max_size) {
cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
@@ -407,19 +437,183 @@ static void virgl_cmd_get_capset(VirtIOGPU *g,
resp = g_malloc0(sizeof(*resp) + max_size);
resp->hdr.type = VIRTIO_GPU_RESP_OK_CAPSET;
virgl_renderer_fill_caps(gc.capset_id,
gc.capset_version,
(void *)resp->capset_data);
gl->virgl->virgl_renderer_fill_caps(gc.capset_id,
gc.capset_version,
(void *)resp->capset_data);
virtio_gpu_ctrl_response(g, cmd, &resp->hdr, sizeof(*resp) + max_size);
g_free(resp);
}
#ifdef CONFIG_ANDROID
static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
struct virtio_gpu_ctrl_command* cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_create_blob cb;
VIRTIO_GPU_FILL_CMD(cb);
if (!gl->virgl->virgl_renderer_resource_create_v2) {
fprintf(stderr, "%s: not supported\n", __func__);
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
return;
}
gl->virgl->virgl_renderer_resource_create_v2(
cb.resource_id,
cb.blob_id);
}
#define VIRTIO_GPU_MAX_RAM_SLOTS 2048
struct VirtioGpuRamSlotInfo {
int used;
uint64_t gpa;
uint64_t offset;
uint64_t size;
uint32_t resource_id;
};
struct VirtioGpuRamSlotTable {
struct VirtioGpuRamSlotInfo slots[VIRTIO_GPU_MAX_RAM_SLOTS];
};
static struct VirtioGpuRamSlotTable* virtio_gpu_ram_slot_table_get(void) {
static struct VirtioGpuRamSlotTable* s_table;
if (!s_table) {
struct VirtioGpuRamSlotTable* table =
(struct VirtioGpuRamSlotTable*)malloc(sizeof(*table));
memset(table, 0, sizeof(*table));
s_table = table;
}
return s_table;
}
static int virtio_gpu_ram_slot_infos_first_free_slot() {
struct VirtioGpuRamSlotTable* table = virtio_gpu_ram_slot_table_get();
for (int i = 0; i < VIRTIO_GPU_MAX_RAM_SLOTS; ++i) {
if (0 == table->slots[i].used) return i;
}
return -1;
}
static void virtio_gpu_map_slot(
MemoryRegion* parent, uint32_t resource_id,
uint64_t gpa, uint64_t offset, void *hva, uint64_t size, int flags) {
struct VirtioGpuRamSlotTable* table = virtio_gpu_ram_slot_table_get();
int slot = virtio_gpu_ram_slot_infos_first_free_slot();
if (slot < 0) {
fprintf(stderr, "%s: error: no free slots to "
"map hva %p -> gpa [0x%llx 0x%llx)\n", __func__,
hva, (unsigned long long)gpa, (unsigned long long)gpa + size);
return;
}
qemu_user_backed_ram_map(gpa, hva, size, USER_BACKED_RAM_FLAGS_READ | USER_BACKED_RAM_FLAGS_WRITE);
table->slots[slot].gpa = gpa;
table->slots[slot].offset = offset;
table->slots[slot].size = size;
table->slots[slot].used = 1;
table->slots[slot].resource_id = resource_id;
}
static void virtio_gpu_unmap_slot(
MemoryRegion* parent, uint32_t resource_id) {
struct VirtioGpuRamSlotTable* table = virtio_gpu_ram_slot_table_get();
for (int i = 0; i < VIRTIO_GPU_MAX_RAM_SLOTS; ++i) {
if (0 == table->slots[i].used) continue;
if (resource_id != table->slots[i].resource_id) continue;
qemu_user_backed_ram_unmap(table->slots[i].gpa, table->slots[i].size);
table->slots[i].used = 0;
return;
}
}
static void virgl_cmd_resource_map(VirtIOGPU *g,
struct virtio_gpu_ctrl_command* cmd) {
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_map_blob m;
struct virtio_gpu_resp_map_info resp;
VIRTIO_GPU_FILL_CMD(m);
void* hva;
uint64_t size;
uint64_t offset = m.offset;
if (!gl->virgl->virgl_renderer_resource_map) {
fprintf(stderr, "%s: not supported\n", __func__);
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
return;
}
gl->virgl->virgl_renderer_resource_map(m.resource_id, &hva, &size);
if (!hva || !size) {
fprintf(stderr, "%s: failed for resource %u\n", __func__, m.resource_id);
cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
return;
}
uint64_t host_coherent_start =
object_property_get_uint(OBJECT(&gl->host_coherent_memory), "addr", NULL);
uint64_t phys_addr =
host_coherent_start + offset;
virtio_gpu_map_slot(
&gl->host_coherent_memory,
m.resource_id,
phys_addr,
offset,
hva,
size,
0xff /* flags, unused */);
memset(&resp, 0, sizeof(resp));
resp.hdr.type = VIRTIO_GPU_RESP_OK_MAP_INFO;
resp.map_info = 0x3;
resp.padding = 0;
virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
}
static void virgl_cmd_resource_unmap(VirtIOGPU *g,
struct virtio_gpu_ctrl_command* cmd) {
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_resource_unmap_blob u;
VIRTIO_GPU_FILL_CMD(u);
if (!gl->virgl->virgl_renderer_resource_unmap) {
fprintf(stderr, "%s: not supported\n", __func__);
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
return;
}
virtio_gpu_unmap_slot(
&gl->host_coherent_memory,
u.resource_id);
gl->virgl->virgl_renderer_resource_unmap(u.resource_id);
}
#endif /* CONFIG_ANDROID */
void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
{
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
virgl_renderer_force_ctx_0();
gl->virgl->virgl_renderer_force_ctx_0();
switch (cmd->cmd_hdr.type) {
case VIRTIO_GPU_CMD_CTX_CREATE:
virgl_cmd_context_create(g, cmd);
@@ -480,6 +674,17 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
case VIRTIO_GPU_CMD_GET_EDID:
virtio_gpu_get_edid(g, cmd);
break;
#ifdef CONFIG_ANDROID
case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB:
virgl_cmd_resource_create_blob(g, cmd);
break;
case VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB:
virgl_cmd_resource_map(g, cmd);
break;
case VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB:
virgl_cmd_resource_unmap(g, cmd);
break;
#endif /* CONFIG_ANDROID */
default:
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
break;
@@ -500,7 +705,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
}
trace_virtio_gpu_fence_ctrl(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
gl->virgl->virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
}
static void virgl_write_fence(void *opaque, uint32_t fence)
@@ -538,6 +743,12 @@ virgl_create_context(void *opaque, int scanout_idx,
qparams.major_ver = params->major_ver;
qparams.minor_ver = params->minor_ver;
if (virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
fprintf(stderr, "%s: error: tried to use "
"standard 3d cbs in proxy mode\n", __func__);
abort();
}
ctx = dpy_gl_ctx_create(g->parent_obj.scanout[scanout_idx].con, &qparams);
return (virgl_renderer_gl_context)ctx;
}
@@ -547,6 +758,12 @@ static void virgl_destroy_context(void *opaque, virgl_renderer_gl_context ctx)
VirtIOGPU *g = opaque;
QEMUGLContext qctx = (QEMUGLContext)ctx;
if (virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
fprintf(stderr, "%s: error: tried to use "
"standard 3d cbs in proxy mode\n", __func__);
abort();
}
dpy_gl_ctx_destroy(g->parent_obj.scanout[0].con, qctx);
}
@@ -556,6 +773,12 @@ static int virgl_make_context_current(void *opaque, int scanout_idx,
VirtIOGPU *g = opaque;
QEMUGLContext qctx = (QEMUGLContext)ctx;
if (virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
fprintf(stderr, "%s: error: tried to use "
"standard 3d cbs in proxy mode\n", __func__);
abort();
}
return dpy_gl_ctx_make_current(g->parent_obj.scanout[scanout_idx].con,
qctx);
}
@@ -591,8 +814,9 @@ static void virtio_gpu_print_stats(void *opaque)
static void virtio_gpu_fence_poll(void *opaque)
{
VirtIOGPU *g = opaque;
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
virgl_renderer_poll();
gl->virgl->virgl_renderer_poll();
virtio_gpu_process_cmdq(g);
if (!QTAILQ_EMPTY(&g->cmdq) || !QTAILQ_EMPTY(&g->fenceq)) {
timer_mod(g->fence_poll, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 10);
@@ -608,6 +832,10 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
{
int i;
if (virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
return;
}
for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
dpy_gfx_replace_surface(g->parent_obj.scanout[i].con, NULL);
dpy_gl_scanout_disable(g->parent_obj.scanout[i].con);
@@ -619,11 +847,22 @@ void virtio_gpu_virgl_reset(VirtIOGPU *g)
virgl_renderer_reset();
}
#ifdef CONFIG_ANDROID
static struct virgl_renderer_callbacks proxy_3d_cbs;
#endif
int virtio_gpu_virgl_init(VirtIOGPU *g)
{
int ret;
ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
#ifdef CONFIG_ANDROID
if (virtio_gpu_goldfish_pipe_enabled(g->parent_obj.conf)) {
ret = virgl_renderer_init(g, 0, &proxy_3d_cbs);
} else
#endif
{
ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
}
if (ret != 0) {
error_report("virgl could not be initialized: %d", ret);
return ret;
@@ -649,3 +888,137 @@ int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g)
return capset2_max_ver ? 2 : 1;
}
#ifdef CONFIG_ANDROID
void virtio_gpu_save_ram_slots(void* qemufile, VirtIOGPU* g) {
struct VirtioGpuRamSlotTable* table = virtio_gpu_ram_slot_table_get();
QEMUFile* file = (QEMUFile*)qemufile;
qemu_put_be32(file, VIRTIO_GPU_MAX_RAM_SLOTS);
for (int i = 0 ; i < VIRTIO_GPU_MAX_RAM_SLOTS; ++i) {
qemu_put_be32(file, table->slots[i].used);
qemu_put_be64(file, table->slots[i].gpa);
qemu_put_be64(file, table->slots[i].offset);
qemu_put_be64(file, table->slots[i].size);
qemu_put_be32(file, table->slots[i].resource_id);
}
}
static void virtio_gpu_clear_slots() {
struct VirtioGpuRamSlotTable* table = virtio_gpu_ram_slot_table_get();
for (int i = 0; i < VIRTIO_GPU_MAX_RAM_SLOTS; ++i) {
if (0 == table->slots[i].used) continue;
qemu_user_backed_ram_unmap(table->slots[i].gpa, table->slots[i].size);
table->slots[i].used = 0;
}
}
void virtio_gpu_load_ram_slots(void* qemufile, VirtIOGPU* g) {
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
QEMUFile* file = (QEMUFile*)qemufile;
struct VirtioGpuRamSlotTable* table = virtio_gpu_ram_slot_table_get();
virtio_gpu_clear_slots();
uint32_t count = qemu_get_be32(file);
for (uint32_t i = 0; i < count; ++i) {
table->slots[i].used = qemu_get_be32(file);
table->slots[i].gpa = qemu_get_be64(file);
table->slots[i].offset = qemu_get_be64(file);
table->slots[i].size = qemu_get_be64(file);
table->slots[i].resource_id = qemu_get_be32(file);
if (!table->slots[i].used) continue;
void* hva;
uint64_t size;
gl->virgl->virgl_renderer_resource_map(table->slots[i].resource_id, &hva, &size);
if (size != table->slots[i].size) {
fprintf(stderr, "%s: resource %u: fatal: size in renderer (0x%llx) did not match slot size (0x%llx)\n", __func__,
table->slots[i].resource_id,
(unsigned long long)size,
(unsigned long long)table->slots[i].size);
abort();
}
qemu_user_backed_ram_map(
table->slots[i].gpa,
hva, size, USER_BACKED_RAM_FLAGS_READ | USER_BACKED_RAM_FLAGS_WRITE);
}
}
static void proxy_virgl_write_fence(void *opaque, uint32_t fence) {
VirtIOGPU *g = opaque;
struct virtio_gpu_ctrl_command *cmd, *tmp;
QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
/*
* the guest can end up emitting fences out of order
* so we should check all fenced cmds not just the first one.
*/
if (cmd->cmd_hdr.fence_id > fence) {
continue;
}
trace_virtio_gpu_fence_resp(cmd->cmd_hdr.fence_id);
virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
QTAILQ_REMOVE(&g->fenceq, cmd, next);
g_free(cmd);
g->inflight--;
if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
fprintf(stderr, "inflight: %3d (-)\r", g->inflight);
}
}
}
static virgl_renderer_gl_context
proxy_virgl_create_context(void *opaque, int scanout_idx,
struct virgl_renderer_gl_ctx_param *params)
{
return (virgl_renderer_gl_context)0;
}
static void proxy_virgl_destroy_context(
void *opaque, virgl_renderer_gl_context ctx) { }
static int proxy_virgl_make_context_current(
void *opaque, int scanout_idx, virgl_renderer_gl_context ctx) { return 0; }
static struct virgl_renderer_callbacks proxy_3d_cbs = {
.version = 1,
.write_fence = proxy_virgl_write_fence,
.create_gl_context = proxy_virgl_create_context,
.destroy_gl_context = proxy_virgl_destroy_context,
.make_current = proxy_virgl_make_context_current,
#ifdef VIRGL_RENDERER_UNSTABLE_APIS
.write_context_fence = NULL,
#endif
};
#endif
static struct virgl_renderer_virtio_interface default_virtio_interface = {
.virgl_renderer_init = virgl_renderer_init,
.virgl_renderer_poll = virgl_renderer_poll,
.virgl_renderer_get_cursor_data = virgl_renderer_get_cursor_data,
.virgl_renderer_resource_create = virgl_renderer_resource_create,
.virgl_renderer_resource_unref = virgl_renderer_resource_unref,
.virgl_renderer_context_create = virgl_renderer_context_create,
.virgl_renderer_context_destroy = virgl_renderer_context_destroy,
.virgl_renderer_submit_cmd = virgl_renderer_submit_cmd,
.virgl_renderer_transfer_read_iov = virgl_renderer_transfer_read_iov,
.virgl_renderer_transfer_write_iov = virgl_renderer_transfer_write_iov,
.virgl_renderer_get_cap_set = virgl_renderer_get_cap_set,
.virgl_renderer_fill_caps = virgl_renderer_fill_caps,
.virgl_renderer_resource_attach_iov = virgl_renderer_resource_attach_iov,
.virgl_renderer_resource_detach_iov = virgl_renderer_resource_detach_iov,
.virgl_renderer_create_fence = virgl_renderer_create_fence,
.virgl_renderer_force_ctx_0 = virgl_renderer_force_ctx_0,
.virgl_renderer_ctx_attach_resource = virgl_renderer_ctx_attach_resource,
.virgl_renderer_ctx_detach_resource = virgl_renderer_ctx_detach_resource,
};
struct virgl_renderer_virtio_interface* get_default_virtio_interface() {
return &default_virtio_interface;
}
+49 -27
View File
@@ -1174,8 +1174,17 @@ static const VMStateDescription vmstate_virtio_gpu_scanouts = {
},
};
static int virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field, JSONWriter *vmdesc)
static int _virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field, JSONWriter *vmdesc)
{
VirtIOGPU *g = opaque;
VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
return vgc->gpu_save(f, opaque, size, field, vmdesc);
}
int virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field, JSONWriter *vmdesc)
{
VirtIOGPU *g = opaque;
struct virtio_gpu_simple_resource *res;
@@ -1202,8 +1211,17 @@ static int virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
return vmstate_save_state(f, &vmstate_virtio_gpu_scanouts, g, NULL);
}
static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field)
static int _virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field)
{
VirtIOGPU *g = opaque;
VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
return vgc->gpu_load(f, opaque, size, field);
}
int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field)
{
VirtIOGPU *g = opaque;
struct virtio_gpu_simple_resource *res;
@@ -1286,29 +1304,31 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
resource_id = qemu_get_be32(f);
}
/* load & apply scanout state */
/* load scanout state */
vmstate_load_state(f, &vmstate_virtio_gpu_scanouts, g, 1);
for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
scanout = &g->parent_obj.scanout[i];
if (!scanout->resource_id) {
continue;
}
res = virtio_gpu_find_resource(g, scanout->resource_id);
if (!res) {
return -EINVAL;
}
scanout->ds = qemu_create_displaysurface_pixman(res->image);
if (!scanout->ds) {
return -EINVAL;
}
dpy_gfx_replace_surface(scanout->con, scanout->ds);
dpy_gfx_update_full(scanout->con);
if (scanout->cursor.resource_id) {
update_cursor(g, &scanout->cursor);
}
res->scanout_bitmask |= (1 << i);
}
// TODO: Figure out why this hangs the emulator display and causes ANR in the current app.
// for (i = 0; i < g->conf.max_outputs; i++) {
// scanout = &g->scanout[i];
// if (!scanout->resource_id) {
// continue;
// }
// res = virtio_gpu_find_resource(g, scanout->resource_id);
// if (!res) {
// return -EINVAL;
// }
// scanout->ds = qemu_create_displaysurface_pixman(res->image);
// if (!scanout->ds) {
// return -EINVAL;
// }
// dpy_gfx_replace_surface(scanout->con, scanout->ds);
// dpy_gfx_update(scanout->con, 0, 0, scanout->width, scanout->height);
// if (scanout->cursor.resource_id) {
// update_cursor(g, &scanout->cursor);
// }
// res->scanout_bitmask |= (1 << i);
// }
return 0;
}
@@ -1410,8 +1430,8 @@ static const VMStateDescription vmstate_virtio_gpu = {
.name = "virtio-gpu",
.info = &(const VMStateInfo) {
.name = "virtio-gpu",
.get = virtio_gpu_load,
.put = virtio_gpu_save,
.get = _virtio_gpu_load,
.put = _virtio_gpu_save,
},
.flags = VMS_SINGLE,
} /* device */,
@@ -1438,6 +1458,8 @@ static void virtio_gpu_class_init(ObjectClass *klass, void *data)
vgc->handle_ctrl = virtio_gpu_handle_ctrl;
vgc->process_cmd = virtio_gpu_simple_process_cmd;
vgc->update_cursor_data = virtio_gpu_update_cursor_data;
vgc->gpu_save = virtio_gpu_save;
vgc->gpu_load = virtio_gpu_load;
vgbc->gl_flushed = virtio_gpu_handle_gl_flushed;
vdc->realize = virtio_gpu_device_realize;
+17
View File
@@ -0,0 +1,17 @@
#ifndef EXEC_MEMORY_REMAH_H_INCLUDED
#define EXEC_MEMORY_REMAH_H_INCLUDED
#include <stdint.h>
/*
* API for adding and removing mappings of guest RAM and host addrs.
* Implementation depends on the hypervisor.
*/
#define USER_BACKED_RAM_FLAGS_NONE 0x0
#define USER_BACKED_RAM_FLAGS_READ 0x1
#define USER_BACKED_RAM_FLAGS_WRITE 0x2
#define USER_BACKED_RAM_FLAGS_EXEC 0x4
void qemu_user_backed_ram_map(uint64_t gpa, void* hva, uint64_t size, int flags);
void qemu_user_backed_ram_unmap(uint64_t gpa, uint64_t size);
#endif /* EXEC_MEMORY_REMAH_H_INCLUDED */
+28
View File
@@ -188,6 +188,8 @@ typedef struct IOMMUTLBEvent {
/* RAM is a persistent kind memory */
#define RAM_PMEM (1 << 5)
/* RAM backing is managed by user */
#define RAM_USER_BACKED (1 << 6)
/*
* UFFDIO_WRITEPROTECT is used on this RAMBlock to
@@ -737,6 +739,7 @@ struct MemoryRegion {
bool nonvolatile;
bool rom_device;
bool flush_coalesced_mmio;
bool user_backed;
uint8_t dirty_log_mask;
bool is_iommu;
RAMBlock *ram_block;
@@ -1509,6 +1512,16 @@ void memory_region_init_ram(MemoryRegion *mr,
uint64_t size,
Error **errp);
/**
* memory_region_init_ram_user_backed - Initialize RAM memory region where
* the allocation is dynamically managed by the user.
* Accesses into the region will modify memory directly.
*/
void memory_region_init_ram_user_backed(MemoryRegion *mr,
Object *owner,
const char *name,
uint64_t size);
/**
* memory_region_init_rom: Initialize a ROM memory region.
*
@@ -1596,6 +1609,21 @@ static inline bool memory_region_is_ram(MemoryRegion *mr)
return mr->ram;
}
/**
* memory_region_is_user_backed: check whether a memory region is random access
*
* Returns %true if a memory region's backing
* is user managed. This allows hypervisor to avoid
* directly adding the region on initialization,
* which can be a fatal error.
*
* @mr: the memory region being queried
*/
static inline bool memory_region_is_user_backed(MemoryRegion *mr)
{
return mr->user_backed;
}
/**
* memory_region_is_ram_device: check whether a memory region is a ram device
*
+9
View File
@@ -133,6 +133,15 @@ RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size,
uint64_t length,
void *host),
MemoryRegion *mr, Error **errp);
RAMBlock *qemu_ram_alloc_user_backed(ram_addr_t size, MemoryRegion *mr,
Error **errp);
typedef void (*QemuUserBackedRamMapFunc)(uint64_t gpa, void* hva, uint64_t size, int flags);
typedef void (*QemuUserBackedRamUnmapFunc)(uint64_t gpa, uint64_t size);
void qemu_set_user_backed_mapping_funcs(QemuUserBackedRamMapFunc mapFunc,
QemuUserBackedRamUnmapFunc unmapFunc);
void qemu_ram_free(RAMBlock *block);
int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
+134
View File
@@ -0,0 +1,134 @@
#ifndef VIRTIO_GOLDFISH_PIPE
#define VIRTIO_GOLDFISH_PIPE
/* An override of virtio-gpu-3d (virgl) that runs goldfish pipe. One could
* implement an actual virtio goldfish pipe, but this hijacking of virgl is
* done in order to avoid any guest kernel changes. */
#include <virglrenderer.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef uint32_t VirtioGpuCtxId;
typedef uint8_t VirtioGpuRingIdx;
struct virgl_renderer_virtio_interface*
get_goldfish_pipe_virgl_renderer_virtio_interface(void);
/* Needed for goldfish pipe */
void virgl_write_fence(void *opaque, uint32_t fence);
void virtio_goldfish_pipe_reset(void* hwpipe, void* hostpipe);
#define VIRTIO_GOLDFISH_EXPORT_API
#ifdef VIRTIO_GOLDFISH_EXPORT_API
#ifdef _WIN32
#define VG_EXPORT __declspec(dllexport)
#else
#define VG_EXPORT __attribute__((visibility("default")))
#endif
VG_EXPORT int pipe_virgl_renderer_init(void *cookie,
int flags,
struct virgl_renderer_callbacks *cb);
VG_EXPORT void pipe_virgl_renderer_poll(void);
VG_EXPORT void* pipe_virgl_renderer_get_cursor_data(
uint32_t resource_id, uint32_t *width, uint32_t *height);
VG_EXPORT int pipe_virgl_renderer_resource_create(
struct virgl_renderer_resource_create_args *args,
struct iovec *iov, uint32_t num_iovs);
VG_EXPORT void pipe_virgl_renderer_resource_unref(uint32_t res_handle);
VG_EXPORT int pipe_virgl_renderer_context_create(
uint32_t handle, uint32_t nlen, const char *name);
VG_EXPORT void pipe_virgl_renderer_context_destroy(uint32_t handle);
VG_EXPORT int pipe_virgl_renderer_submit_cmd(void *buffer,
int ctx_id,
int bytes);
VG_EXPORT int pipe_virgl_renderer_transfer_read_iov(
uint32_t handle, uint32_t ctx_id,
uint32_t level, uint32_t stride,
uint32_t layer_stride,
struct virgl_box *box,
uint64_t offset, struct iovec *iov,
int iovec_cnt);
VG_EXPORT int pipe_virgl_renderer_transfer_write_iov(
uint32_t handle,
uint32_t ctx_id,
int level,
uint32_t stride,
uint32_t layer_stride,
struct virgl_box *box,
uint64_t offset,
struct iovec *iovec,
unsigned int iovec_cnt);
VG_EXPORT void pipe_virgl_renderer_get_cap_set(uint32_t, uint32_t*, uint32_t*);
VG_EXPORT void pipe_virgl_renderer_fill_caps(uint32_t, uint32_t, void *caps);
VG_EXPORT int pipe_virgl_renderer_resource_attach_iov(
int res_handle, struct iovec *iov,
int num_iovs);
VG_EXPORT void pipe_virgl_renderer_resource_detach_iov(
int res_handle, struct iovec **iov, int *num_iovs);
VG_EXPORT int pipe_virgl_renderer_create_fence(
int client_fence_id, uint32_t cmd_type);
VG_EXPORT void pipe_virgl_renderer_force_ctx_0(void);
VG_EXPORT void pipe_virgl_renderer_ctx_attach_resource(
int ctx_id, int res_handle);
VG_EXPORT void pipe_virgl_renderer_ctx_detach_resource(
int ctx_id, int res_handle);
VG_EXPORT int pipe_virgl_renderer_resource_get_info(
int res_handle,
struct virgl_renderer_resource_info *info);
VG_EXPORT void stream_renderer_flush_resource_and_readback(
uint32_t res_handle, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
void* pixels, uint32_t max_bytes);
VG_EXPORT void stream_renderer_resource_create_v2(
uint32_t res_handle, uint64_t hvaId);
VG_EXPORT uint64_t stream_renderer_resource_get_hva(uint32_t res_handle);
VG_EXPORT uint64_t stream_renderer_resource_get_hva_size(uint32_t res_handle);
VG_EXPORT void stream_renderer_resource_set_hv_slot(uint32_t res_handle, uint32_t slot);
VG_EXPORT uint32_t stream_renderer_resource_get_hv_slot(uint32_t res_handle);
VG_EXPORT int stream_renderer_resource_map(uint32_t res_handle, void** hvaOut, uint64_t* sizeOut);
VG_EXPORT int stream_renderer_resource_unmap(uint32_t res_handle);
VG_EXPORT int stream_renderer_context_create_fence(
uint64_t fence_id, uint32_t ctx_id, uint8_t ring_idx);
// Platform resources and contexts support
#define STREAM_RENDERER_PLATFORM_RESOURCE_TYPE_EGL_NATIVE_PIXMAP 0x01
#define STREAM_RENDERER_PLATFORM_RESOURCE_TYPE_EGL_IMAGE 0x02
VG_EXPORT int stream_renderer_platform_import_resource(int res_handle, int res_type, void* resource);
VG_EXPORT int stream_renderer_platform_resource_info(int res_handle, int* width, int* height, int* internal_format);
VG_EXPORT void* stream_renderer_platform_create_shared_egl_context(void);
VG_EXPORT int stream_renderer_platform_destroy_shared_egl_context(void*);
#else
#define VG_EXPORT
#endif // !VIRTIO_GOLDFISH_EXPORT_API
#ifdef __cplusplus
} // extern "C"
#endif
// based on VIRGL_RENDERER_USE* and friends
enum RendererFlags {
GFXSTREAM_RENDERER_FLAGS_USE_EGL_BIT = 1 << 0,
GFXSTREAM_RENDERER_FLAGS_THREAD_SYNC = 1 << 1,
GFXSTREAM_RENDERER_FLAGS_USE_GLX_BIT = 1 << 2,
GFXSTREAM_RENDERER_FLAGS_USE_SURFACELESS_BIT = 1 << 3,
GFXSTREAM_RENDERER_FLAGS_USE_GLES_BIT = 1 << 4,
GFXSTREAM_RENDERER_FLAGS_NO_VK_BIT = 1 << 5, // for disabling vk
GFXSTREAM_RENDERER_FLAGS_ENABLE_GLES31_BIT =
1 << 9, // disables the PlayStoreImage flag
GFXSTREAM_RENDERER_FLAGS_GUEST_USES_ANGLE = 1 << 21,
GFXSTREAM_RENDERER_FLAGS_VULKAN_NATIVE_SWAPCHAIN_BIT = 1 << 22,
GFXSTREAM_RENDERER_FLAGS_ASYNC_FENCE_CB = 1 << 23,
};
#endif
+1
View File
@@ -34,6 +34,7 @@ struct VirtIOGPUPCIBase {
#define DEFINE_VIRTIO_GPU_PCI_PROPERTIES(_state) \
DEFINE_PROP_BIT("ioeventfd", _state, flags, \
VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), \
DEFINE_PROP_BIT("hostshm", _state, flags, VIRTIO_PCI_FLAG_HOSTSHM_BIT, true), \
DEFINE_PROP_UINT32("vectors", _state, nvectors, 3)
#endif /* HW_VIRTIO_GPU_PCI_H */
+114
View File
@@ -0,0 +1,114 @@
#ifndef HW_VIRTIO_GPU_VIRGL_H
#define HW_VIRTIO_GPU_VIRGL_H
#include <virglrenderer.h>
struct virgl_renderer_resource_info;
/* Struct of virglrenderer API calls in order to be able to switch between
* different virgl API impls, or different devices altogether that use
* the virglrenderer API (e.g., goldfish pipe) */
typedef int (*virgl_renderer_init_t)(
void *cookie, int flags, struct virgl_renderer_callbacks *cb);
typedef void (*virgl_renderer_poll_t)(void); /* force fences */
typedef void* (*virgl_renderer_get_cursor_data_t)(
uint32_t resource_id, uint32_t *width, uint32_t *height);
typedef int (*virgl_renderer_resource_create_t)(
struct virgl_renderer_resource_create_args *args,
struct iovec *iov, uint32_t num_iovs);
typedef void (*virgl_renderer_resource_unref_t)(uint32_t res_handle);
typedef int (*virgl_renderer_context_create_t)(
uint32_t handle, uint32_t nlen, const char *name);
typedef void (*virgl_renderer_context_destroy_t)(uint32_t handle);
typedef int (*virgl_renderer_submit_cmd_t)(void *buffer,
int ctx_id,
int ndw);
typedef int (*virgl_renderer_transfer_read_iov_t)(
uint32_t handle, uint32_t ctx_id,
uint32_t level, uint32_t stride,
uint32_t layer_stride,
struct virgl_box *box,
uint64_t offset, struct iovec *iov,
int iovec_cnt);
typedef int (*virgl_renderer_transfer_write_iov_t)(
uint32_t handle,
uint32_t ctx_id,
int level,
uint32_t stride,
uint32_t layer_stride,
struct virgl_box *box,
uint64_t offset,
struct iovec *iovec,
unsigned int iovec_cnt);
typedef void (*virgl_renderer_get_cap_set_t)(uint32_t set, uint32_t *max_ver,
uint32_t *max_size);
typedef void (*virgl_renderer_fill_caps_t)(uint32_t set, uint32_t version,
void *caps);
typedef int (*virgl_renderer_resource_attach_iov_t)(
int res_handle, struct iovec *iov,
int num_iovs);
typedef void (*virgl_renderer_resource_detach_iov_t)(
int res_handle, struct iovec **iov, int *num_iovs);
typedef int (*virgl_renderer_create_fence_t)(
int client_fence_id, uint32_t ctx_id);
typedef void (*virgl_renderer_force_ctx_0_t)(void);
typedef void (*virgl_renderer_ctx_attach_resource_t)(
int ctx_id, int res_handle);
typedef void (*virgl_renderer_ctx_detach_resource_t)(
int ctx_id, int res_handle);
typedef int (*virgl_renderer_resource_get_info_t)(
int res_handle,
struct virgl_renderer_resource_info *info);
typedef int (*virgl_renderer_resource_create_v2_t)(unsigned int res_handle, uint64_t hvaId);
typedef int (*virgl_renderer_resource_map_t)(unsigned int res_handle, void** hvaOut, uint64_t* sizeOut);
typedef int (*virgl_renderer_resource_unmap_t)(unsigned int res_handle);
typedef void (*virgl_renderer_save_snapshot_t)(void* qemufile);
typedef void (*virgl_renderer_load_snapshot_t)(void* qemufile);
typedef int (*virgl_renderer_resource_attach_iov_with_addrs_t)(
int res_handle, struct iovec *iov, int num_iovs, uint64_t* addrs);
#define LIST_VIRGLRENDERER_API(f) \
f(virgl_renderer_init) \
f(virgl_renderer_poll) \
f(virgl_renderer_get_cursor_data) \
f(virgl_renderer_resource_create) \
f(virgl_renderer_resource_unref) \
f(virgl_renderer_context_create) \
f(virgl_renderer_context_destroy) \
f(virgl_renderer_submit_cmd) \
f(virgl_renderer_transfer_read_iov) \
f(virgl_renderer_transfer_write_iov) \
f(virgl_renderer_get_cap_set) \
f(virgl_renderer_fill_caps) \
f(virgl_renderer_resource_attach_iov) \
f(virgl_renderer_resource_detach_iov) \
f(virgl_renderer_create_fence) \
f(virgl_renderer_force_ctx_0) \
f(virgl_renderer_ctx_attach_resource) \
f(virgl_renderer_ctx_detach_resource) \
f(virgl_renderer_resource_get_info) \
f(virgl_renderer_resource_create_v2) \
f(virgl_renderer_resource_map) \
f(virgl_renderer_resource_unmap) \
f(virgl_renderer_save_snapshot) \
f(virgl_renderer_load_snapshot) \
f(virgl_renderer_resource_attach_iov_with_addrs) \
#define VIRGLRENDERER_API_DEFINE_STRUCT_FIELD(api) \
api##_t api;
struct virgl_renderer_virtio_interface {
LIST_VIRGLRENDERER_API(VIRGLRENDERER_API_DEFINE_STRUCT_FIELD)
};
struct virgl_renderer_virtio_interface* get_default_virtio_interface(void);
extern struct virgl_renderer_virtio_interface *get_goldfish_pipe_virgl_renderer_virtio_interface(void);
#endif /* HW_VIRTIO_GPU_VIRGL_H */
+18
View File
@@ -90,6 +90,7 @@ enum virtio_gpu_base_conf_flags {
VIRTIO_GPU_FLAG_EDID_ENABLED,
VIRTIO_GPU_FLAG_DMABUF_ENABLED,
VIRTIO_GPU_FLAG_BLOB_ENABLED,
VIRTIO_GPU_FLAG_GOLDFISH_PIPE_ENABLED,
};
#define virtio_gpu_virgl_enabled(_cfg) \
@@ -102,6 +103,8 @@ enum virtio_gpu_base_conf_flags {
(_cfg.flags & (1 << VIRTIO_GPU_FLAG_DMABUF_ENABLED))
#define virtio_gpu_blob_enabled(_cfg) \
(_cfg.flags & (1 << VIRTIO_GPU_FLAG_BLOB_ENABLED))
#define virtio_gpu_goldfish_pipe_enabled(_cfg) \
(_cfg.flags & (1 << VIRTIO_GPU_FLAG_GOLDFISH_PIPE_ENABLED))
struct virtio_gpu_base_conf {
uint32_t max_outputs;
@@ -199,6 +202,10 @@ struct VirtIOGPUClass {
void (*update_cursor_data)(VirtIOGPU *g,
struct virtio_gpu_scanout *s,
uint32_t resource_id);
int (*gpu_save)(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field, JSONWriter *vmdesc);
int (*gpu_load)(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field);
};
struct VirtIOGPUGL {
@@ -206,6 +213,8 @@ struct VirtIOGPUGL {
bool renderer_inited;
bool renderer_reset;
struct virgl_renderer_virtio_interface* virgl;
MemoryRegion host_coherent_memory;
};
struct VhostUserGPU {
@@ -265,6 +274,10 @@ void virtio_gpu_simple_process_cmd(VirtIOGPU *g, struct virtio_gpu_ctrl_command
void virtio_gpu_update_cursor_data(VirtIOGPU *g,
struct virtio_gpu_scanout *s,
uint32_t resource_id);
int virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field, JSONWriter *vmdesc);
int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
const VMStateField *field);
/* virtio-gpu-udmabuf.c */
bool virtio_gpu_have_udmabuf(void);
@@ -285,4 +298,9 @@ void virtio_gpu_virgl_reset(VirtIOGPU *g);
int virtio_gpu_virgl_init(VirtIOGPU *g);
int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g);
#ifdef CONFIG_ANDROID
void virtio_gpu_save_ram_slots(void* qemufile, VirtIOGPU* g);
void virtio_gpu_load_ram_slots(void* qemufile, VirtIOGPU* g);
#endif
#endif
+6
View File
@@ -43,6 +43,7 @@ enum {
VIRTIO_PCI_FLAG_INIT_FLR_BIT,
VIRTIO_PCI_FLAG_AER_BIT,
VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT,
VIRTIO_PCI_FLAG_HOSTSHM_BIT,
};
/* Need to activate work-arounds for buggy guests at vmstate load. */
@@ -89,6 +90,9 @@ enum {
#define VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED \
(1 << VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT)
/* Host coherent shared memory */
#define VIRTIO_PCI_FLAG_HOSTSHM (1 << VIRTIO_PCI_FLAG_HOSTSHM_BIT)
typedef struct {
MSIMessage msg;
int virq;
@@ -142,10 +146,12 @@ struct VirtIOPCIProxy {
};
MemoryRegion modern_bar;
MemoryRegion io_bar;
MemoryRegion hostshm_bar;
uint32_t legacy_io_bar_idx;
uint32_t msix_bar_idx;
uint32_t modern_io_bar_idx;
uint32_t modern_mem_bar_idx;
uint32_t hostshm_mem_bar_idx;
int config_cap;
uint32_t flags;
bool disable_modern;
+9
View File
@@ -28,6 +28,15 @@ extern bool hvf_allowed;
#define hvf_get_supported_cpuid(func, idx, reg) 0
#endif /* !CONFIG_HVF */
void* hvf_gpa2hva(uint64_t gpa, bool* found);
int hvf_hva2gpa(void* hva, uint64_t length, int max,
uint64_t* gpa, uint64_t* size);
int hvf_map_safe(void* hva, uint64_t gpa, uint64_t size, uint64_t flags);
int hvf_unmap_safe(uint64_t gpa, uint64_t size);
int hvf_protect_safe(uint64_t gpa, uint64_t size, uint64_t flags);
int hvf_remap_safe(void* hva, uint64_t gpa, uint64_t size, uint64_t flags);
#endif /* NEED_CPU_H */
#define TYPE_HVF_ACCEL ACCEL_CLASS_NAME("hvf")
+17
View File
@@ -1744,6 +1744,23 @@ void memory_region_init_iommu(void *_iommu_mr,
iommu_mr->iommu_notify_flags = IOMMU_NOTIFIER_NONE;
}
void memory_region_init_ram_user_backed(MemoryRegion *mr,
Object *owner,
const char *name,
uint64_t size)
{
DeviceState* owner_dev;
memory_region_init(mr, owner, name, size);
mr->ram = true;
mr->user_backed = true;
mr->terminates = true;
mr->destructor = memory_region_destructor_ram;
mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
mr->ram_block = qemu_ram_alloc_user_backed(size, mr, &error_fatal);
owner_dev = DEVICE(owner);
vmstate_register_ram(mr, owner_dev);
}
static void memory_region_finalize(Object *obj)
{
MemoryRegion *mr = MEMORY_REGION(obj);
+126 -1
View File
@@ -45,6 +45,7 @@
#include "qemu/log.h"
#include "qemu/memalign.h"
#include "exec/memory.h"
#include "exec/memory-remap.h"
#include "exec/ioport.h"
#include "sysemu/dma.h"
#include "sysemu/hostmem.h"
@@ -1973,7 +1974,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
qemu_mutex_lock_ramlist();
new_block->offset = find_ram_offset(new_block->max_length);
if (!new_block->host) {
if (!new_block->host && !(new_block->flags & RAM_USER_BACKED)) {
if (xen_enabled()) {
xen_ram_alloc(new_block->offset, new_block->max_length,
new_block->mr, &err);
@@ -2198,6 +2199,130 @@ RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz,
RAM_RESIZEABLE, mr, errp);
}
RAMBlock *qemu_ram_alloc_user_backed(ram_addr_t size, MemoryRegion *mr,
Error **errp)
{
RAMBlock *new_block;
Error *local_err = NULL;
size = HOST_PAGE_ALIGN(size);
new_block = (RAMBlock*)g_malloc0(sizeof(*new_block));
new_block->mr = mr;
new_block->used_length = size;
new_block->max_length = size;
new_block->fd = -1;
new_block->page_size = getpagesize();
new_block->host = NULL;
new_block->flags |= RAM_PREALLOC;
new_block->flags |= RAM_USER_BACKED;
ram_block_add(new_block, &local_err);
if (local_err) {
g_free(new_block);
error_propagate(errp, local_err);
return NULL;
}
return new_block;
}
#define TCG_MAX_USER_RAM_SLOTS 1024
struct TcgUserRamSlotInfo {
int used;
MemoryRegion mr;
uint64_t gpa;
};
struct TcgUserRamTable {
struct TcgUserRamSlotInfo slots[TCG_MAX_USER_RAM_SLOTS];
};
static struct TcgUserRamTable* tcg_user_ram_table_get(void) {
static struct TcgUserRamTable* s_table;
if (!s_table) {
struct TcgUserRamTable* table =
(struct TcgUserRamTable*)malloc(sizeof(*table));
memset(table, 0, sizeof(*table));
s_table = table;
}
return s_table;
}
static int tcg_user_ram_slot_infos_first_free_slot() {
struct TcgUserRamTable* table = tcg_user_ram_table_get();
for (int i = 0; i < TCG_MAX_USER_RAM_SLOTS; ++i) {
if (0 == table->slots[i].used) return i;
}
return -1;
}
static void tcg_user_ram_slot_map(
uint64_t gpa, void *hva, uint64_t size, int flags) {
struct TcgUserRamTable* table = tcg_user_ram_table_get();
int slot = tcg_user_ram_slot_infos_first_free_slot();
MemoryRegion* mr = &(table->slots[slot].mr);
if (slot < 0) {
fprintf(stderr, "%s: error: no free slots to "
"map hva %p -> gpa [0x%llx 0x%llx)\n", __func__,
hva, (unsigned long long)gpa, (unsigned long long)gpa + size);
return;
}
memory_region_init_ram_ptr(
mr, 0 /* unattached */, "tcg-user-backed", size, hva);
memory_region_add_subregion(system_memory, gpa, mr);
table->slots[slot].gpa = gpa;
table->slots[slot].used = 1;
}
static void tcg_user_ram_slot_unmap(
uint64_t gpa, uint64_t size) {
struct TcgUserRamTable* table = tcg_user_ram_table_get();
for (int i = 0; i < TCG_MAX_USER_RAM_SLOTS; ++i) {
if (0 == table->slots[i].used) continue;
if (gpa != table->slots[i].gpa) continue;
memory_region_del_subregion(system_memory, &(table->slots[i].mr));
table->slots[i].used = 0;
return;
}
}
static void qemu_user_backed_ram_map_empty(uint64_t gpa, void *hva, uint64_t size, int flags)
{
tcg_user_ram_slot_map(gpa, hva, size, flags);
}
static void qemu_user_backed_ram_unmap_empty(uint64_t gpa, uint64_t size)
{
tcg_user_ram_slot_unmap(gpa, size);
}
static QemuUserBackedRamMapFunc s_user_backed_ram_map = &qemu_user_backed_ram_map_empty;
static QemuUserBackedRamUnmapFunc s_user_backed_ram_unmap = &qemu_user_backed_ram_unmap_empty;
void qemu_set_user_backed_mapping_funcs(QemuUserBackedRamMapFunc mapFunc,
QemuUserBackedRamUnmapFunc unmapFunc)
{
s_user_backed_ram_map = mapFunc;
s_user_backed_ram_unmap = unmapFunc;
}
void qemu_user_backed_ram_map(uint64_t gpa, void *hva, uint64_t size, int flags)
{
s_user_backed_ram_map(gpa, hva, size, flags);
}
void qemu_user_backed_ram_unmap(uint64_t gpa, uint64_t size)
{
s_user_backed_ram_unmap(gpa, size);
}
static void reclaim_ramblock(RAMBlock *block)
{
if (block->flags & RAM_PREALLOC) {