hw/display: add a reims-vgpu backend for apple-gfx

The apple_gfx_backend meson option (default pg) builds apple-gfx against
ReimsVGPU.framework's C API (reims_vgpu.h) instead of
ParavirtualizedGraphics. Each framework call keeps its Objective-C form
under the pg backend, so the two read side by side; blocks become
static functions with the same bodies, called on the same queues.
This commit is contained in:
Anees Iqbal committed 2026-09-26 07:15:55 +03:00
1 parent 3a04f9e0b5
commit 7e2d0fa93e
7 files changed
+588 -21

No files matched your search

+140
View File
@@ -21,10 +21,13 @@
#include "trace.h"
#include "system/address-spaces.h"
#ifndef APPLE_GFX_BACKEND_REIMS
#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
#endif
OBJECT_DECLARE_SIMPLE_TYPE(AppleGFXMMIOState, APPLE_GFX_MMIO)
#ifndef APPLE_GFX_BACKEND_REIMS
/*
* ParavirtualizedGraphics.Framework only ships header files for the PCI
* variant which does not include IOSFC descriptors and host devices. We add
@@ -58,6 +61,7 @@ typedef bool(^IOSFCMapMemory)(uint64_t phys, uint64_t len, bool ro, void **va,
-(uint32_t)mmioReadAtOffset:(size_t)offset;
-(void)mmioWriteAtOffset:(size_t)offset value:(uint32_t)value;
@end
#endif
struct AppleGFXMapSurfaceMemoryJob;
struct AppleGFXMMIOState {
@@ -68,7 +72,11 @@ struct AppleGFXMMIOState {
qemu_irq irq_gfx;
qemu_irq irq_iosfc;
MemoryRegion iomem_iosfc;
#ifdef APPLE_GFX_BACKEND_REIMS
RvgIOSurfaceHostDevice *pgiosfc;
#else
PGIOSurfaceHostDevice *pgiosfc;
#endif
};
typedef struct AppleGFXMMIOJob {
@@ -81,7 +89,16 @@ typedef struct AppleGFXMMIOJob {
static void iosfc_do_read(void *opaque)
{
AppleGFXMMIOJob *job = opaque;
#ifdef APPLE_GFX_BACKEND_REIMS
/*
* QEMU declared -mmioReadAtOffset: as returning uint32_t, so the value
* is truncated to 32 bits, then zero-extended into job->value.
*/
job->value = (uint32_t)rvg_iosurface_host_device_mmio_read(
job->state->pgiosfc, job->offset);
#else
job->value = [job->state->pgiosfc mmioReadAtOffset:job->offset];
#endif
qatomic_set(&job->completed, true);
aio_wait_kick();
}
@@ -106,7 +123,13 @@ static uint64_t iosfc_read(void *opaque, hwaddr offset, unsigned size)
static void iosfc_do_write(void *opaque)
{
AppleGFXMMIOJob *job = opaque;
#ifdef APPLE_GFX_BACKEND_REIMS
/* QEMU declared value:(uint32_t): the value is truncated to 32 bits. */
rvg_iosurface_host_device_mmio_write(job->state->pgiosfc, job->offset,
(uint32_t)job->value);
#else
[job->state->pgiosfc mmioWriteAtOffset:job->offset value:job->value];
#endif
qatomic_set(&job->completed, true);
aio_wait_kick();
}
@@ -184,6 +207,118 @@ static bool apple_gfx_mmio_unmap_surface_memory(void *ptr)
return true;
}
#ifdef APPLE_GFX_BACKEND_REIMS
/*
* The Objective-C version's blocks, as {fn, ctx, destroy} triples: each body
* is the block's, ctx is what the block captured (nothing for the map and
* unmap blocks), and destroy is NULL because releasing the blocks released
* nothing.
*/
static bool apple_gfx_mmio_iosfc_map_memory_cb(void *ctx, uint64_t phys,
uint64_t len, bool ro,
void **va, void *e, void *f)
{
*va = apple_gfx_mmio_map_surface_memory(phys, len, ro);
trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va);
return *va != NULL;
}
static bool apple_gfx_mmio_iosfc_unmap_memory_cb(void *ctx, void *va, void *b,
void *c, void *d, void *e,
void *f)
{
return apple_gfx_mmio_unmap_surface_memory(va);
}
/* The block returned true (IOSFCRaiseInterrupt); RvgRaiseInterruptFn is void. */
static void apple_gfx_mmio_iosfc_raise_interrupt_cb(void *ctx, uint32_t vector)
{
AppleGFXMMIOState *s = ctx;
trace_apple_gfx_iosfc_raise_irq(vector);
aio_bh_schedule_oneshot(qemu_get_aio_context(),
raise_irq_bh, &s->irq_iosfc);
}
static RvgIOSurfaceHostDevice *apple_gfx_prepare_iosurface_host_device(
AppleGFXMMIOState *s)
{
RvgIOSurfaceHostDeviceDescriptor *iosfc_desc =
rvg_iosurface_host_device_descriptor_create();
RvgIOSurfaceHostDevice *iosfc_host_dev;
/*
* The legacy memory management API is no longer present
* for the IOSurface mapper as of macOS 15.4.
*/
if (@available(macOS 15.4, *)) {
RvgMemoryMapDescriptor *memory_map_descriptor =
rvg_memory_map_descriptor_create();
FlatView* fv = address_space_to_flatview(&address_space_memory);
flatview_for_each_range(fv, apple_gfx_register_memory_cb, memory_map_descriptor);
/* the device model defines this as a single-page MMIO region, hence 16KB */
rvg_iosurface_host_device_descriptor_set_mmio_length(iosfc_desc,
0x10000);
/* QEMU's own reference to the map is never released, as before. */
rvg_iosurface_host_device_descriptor_set_memory_map(
iosfc_desc, memory_map_descriptor);
} else {
rvg_iosurface_host_device_descriptor_set_map_memory(
iosfc_desc, apple_gfx_mmio_iosfc_map_memory_cb, NULL, NULL);
rvg_iosurface_host_device_descriptor_set_unmap_memory(
iosfc_desc, apple_gfx_mmio_iosfc_unmap_memory_cb, NULL, NULL);
}
rvg_iosurface_host_device_descriptor_set_raise_interrupt(
iosfc_desc, apple_gfx_mmio_iosfc_raise_interrupt_cb, s, NULL);
iosfc_host_dev = rvg_iosurface_host_device_create(iosfc_desc);
rvg_iosurface_host_device_descriptor_release(iosfc_desc);
return iosfc_host_dev;
}
static void apple_gfx_mmio_raise_interrupt_cb(void *ctx, uint32_t vector)
{
AppleGFXMMIOState *s = ctx;
trace_apple_gfx_raise_irq(vector);
aio_bh_schedule_oneshot(qemu_get_aio_context(),
raise_irq_bh, &s->irq_gfx);
}
static void apple_gfx_mmio_realize(DeviceState *dev, Error **errp)
{
@autoreleasepool {
AppleGFXMMIOState *s = APPLE_GFX_MMIO(dev);
RvgDeviceDescriptor *desc = rvg_device_descriptor_create();
rvg_device_descriptor_set_raise_interrupt(
desc, apple_gfx_mmio_raise_interrupt_cb, s, NULL);
rvg_device_descriptor_set_using_iosurface_mapper(desc, true);
rvg_device_descriptor_set_enable_argument_buffers(desc, true);
/*
* Process isolation needs PGMemoryMapDescriptor instead of
* the legacy memory management interface present in releases
* older than macOS 15.4.
*/
if (@available(macOS 15.4, *)) {
rvg_device_descriptor_set_enable_process_isolation(desc, true);
}
if (!apple_gfx_common_realize(&s->common, dev, desc, errp)) {
rvg_iosurface_host_device_release(s->pgiosfc);
s->pgiosfc = NULL;
}
s->pgiosfc = apple_gfx_prepare_iosurface_host_device(s);
rvg_device_descriptor_release(desc);
desc = NULL;
}
}
#else
static PGIOSurfaceHostDevice *apple_gfx_prepare_iosurface_host_device(
AppleGFXMMIOState *s)
{
@@ -265,6 +400,7 @@ static void apple_gfx_mmio_realize(DeviceState *dev, Error **errp)
desc = nil;
}
}
#endif
static void apple_gfx_mmio_init(Object *obj)
{
@@ -283,7 +419,11 @@ static void apple_gfx_mmio_init(Object *obj)
static void apple_gfx_mmio_reset(Object *obj, ResetType type)
{
AppleGFXMMIOState *s = APPLE_GFX_MMIO(obj);
#ifdef APPLE_GFX_BACKEND_REIMS
rvg_device_reset(s->common.pgdev);
#else
[s->common.pgdev reset];
#endif
}
static const Property apple_gfx_mmio_properties[] = {
+40
View File
@@ -18,7 +18,15 @@
#include "apple-gfx.h"
#include "trace.h"
#ifdef APPLE_GFX_BACKEND_REIMS
/* PGDeviceSpec.h's PCI facts, which reims_vgpu.h does not carry. */
#define PG_PCI_BAR_MMIO 0
#define PG_PCI_MAX_MSI_VECTORS 1
#define PG_PCI_VENDOR_ID 0x106b
#define PG_PCI_DEVICE_ID 0xeeee
#else
#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
#endif
OBJECT_DECLARE_SIMPLE_TYPE(AppleGFXPCIState, APPLE_GFX_PCI)
@@ -30,6 +38,14 @@ struct AppleGFXPCIState {
static const char *apple_gfx_pci_option_rom_path = NULL;
#ifdef APPLE_GFX_BACKEND_REIMS
static void apple_gfx_init_option_rom_path(void)
{
/* The library keeps the string: nothing to release (NULL: no ROM). */
const char *option_rom_path = rvg_option_rom_path();
apple_gfx_pci_option_rom_path = g_strdup(option_rom_path);
}
#else
static void apple_gfx_init_option_rom_path(void)
{
NSURL *option_rom_url = PGCopyOptionROMURL();
@@ -37,6 +53,7 @@ static void apple_gfx_init_option_rom_path(void)
apple_gfx_pci_option_rom_path = g_strdup(option_rom_path);
[option_rom_url release];
}
#endif
static void apple_gfx_pci_init(Object *obj)
{
@@ -82,6 +99,14 @@ static void apple_gfx_pci_interrupt(PCIDevice *dev, uint32_t vector)
apple_gfx_pci_raise_interrupt, job);
}
#ifdef APPLE_GFX_BACKEND_REIMS
static void apple_gfx_pci_raise_interrupt_cb(void *ctx, uint32_t vector)
{
PCIDevice *dev = ctx;
apple_gfx_pci_interrupt(dev, vector);
}
#endif
static void apple_gfx_pci_realize(PCIDevice *dev, Error **errp)
{
AppleGFXPCIState *s = APPLE_GFX_PCI(dev);
@@ -98,6 +123,16 @@ static void apple_gfx_pci_realize(PCIDevice *dev, Error **errp)
}
@autoreleasepool {
#ifdef APPLE_GFX_BACKEND_REIMS
/* The block, as a triple: ctx is the PCIDevice it captured. */
RvgDeviceDescriptor *desc = rvg_device_descriptor_create();
rvg_device_descriptor_set_raise_interrupt(
desc, apple_gfx_pci_raise_interrupt_cb, dev, NULL);
apple_gfx_common_realize(&s->common, DEVICE(dev), desc, errp);
rvg_device_descriptor_release(desc);
desc = NULL;
#else
PGDeviceDescriptor *desc = [PGDeviceDescriptor new];
desc.raiseInterrupt = ^(uint32_t vector) {
apple_gfx_pci_interrupt(dev, vector);
@@ -106,13 +141,18 @@ static void apple_gfx_pci_realize(PCIDevice *dev, Error **errp)
apple_gfx_common_realize(&s->common, DEVICE(dev), desc, errp);
[desc release];
desc = nil;
#endif
}
}
static void apple_gfx_pci_reset(Object *obj, ResetType type)
{
AppleGFXPCIState *s = APPLE_GFX_PCI(obj);
#ifdef APPLE_GFX_BACKEND_REIMS
rvg_device_reset(s->common.pgdev);
#else
[s->common.pgdev reset];
#endif
}
static const Property apple_gfx_pci_properties[] = {
+25
View File
@@ -17,9 +17,22 @@
#define TYPE_APPLE_GFX_MMIO "apple-gfx-mmio"
#define TYPE_APPLE_GFX_PCI "apple-gfx-pci"
#ifdef APPLE_GFX_BACKEND_REIMS
/*
* apple_gfx_backend=reims: the device drives reims-vgpu's C API
* (ReimsVGPU.framework, reims_vgpu.h) in place of the Objective-C API of
* ParavirtualizedGraphics.framework. Each call maps one to one; the
* framework's handles become Rvg* handles with rvg_*_retain/release.
*/
#import <ReimsVGPU/reims_vgpu.h>
/* PGDevice.h's opaque task type, which QEMU implements (apple-gfx.m). */
typedef struct PGTask_s PGTask_t;
#else
@class PGDeviceDescriptor;
@protocol PGDevice;
@protocol PGDisplay;
#endif
@protocol MTLDevice;
@protocol MTLTexture;
@protocol MTLCommandQueue;
@@ -31,9 +44,11 @@ typedef struct PGGuestPhysicalRange_s
void *hostAddress;
} PGGuestPhysicalRange_t;
#ifndef APPLE_GFX_BACKEND_REIMS
@interface PGMemoryMapDescriptor : NSObject
-(void)addRange:(PGGuestPhysicalRange_t) range;
@end
#endif
typedef QTAILQ_HEAD(, PGTask_s) PGTaskList;
@@ -46,8 +61,13 @@ typedef struct AppleGFXDisplayMode {
typedef struct AppleGFXState {
/* Initialised on init/realize() */
MemoryRegion iomem_gfx;
#ifdef APPLE_GFX_BACKEND_REIMS
RvgDevice *pgdev;
RvgDisplay *pgdisp;
#else
id<PGDevice> pgdev;
id<PGDisplay> pgdisp;
#endif
QemuConsole *con;
id<MTLDevice> mtl;
id<MTLCommandQueue> mtl_queue;
@@ -74,8 +94,13 @@ typedef struct AppleGFXState {
} AppleGFXState;
void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name);
#ifdef APPLE_GFX_BACKEND_REIMS
bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
RvgDeviceDescriptor *desc, Error **errp);
#else
bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
PGDeviceDescriptor *desc, Error **errp);
#endif
void *apple_gfx_host_ptr_for_gpa_range(uint64_t guest_physical,
uint64_t length, bool read_only,
MemoryRegion **mapping_in_region);
+322
View File
@@ -30,7 +30,27 @@
#include <mach/mach_vm.h>
#include <dispatch/dispatch.h>
#ifdef APPLE_GFX_BACKEND_REIMS
/* reims_vgpu.h comes through apple-gfx.h; Metal is QEMU's own. */
#import <Metal/Metal.h>
/*
* PGDevice.h's PGPhysicalMemoryRange_t, with QEMU's field names: the
* mapMemory callback's ranges are RvgPhysicalMemoryRange, the same layout.
*/
typedef struct PGPhysicalMemoryRange_s {
uint64_t physicalAddress;
uint64_t physicalLength;
} PGPhysicalMemoryRange_t;
QEMU_BUILD_BUG_ON(sizeof(PGPhysicalMemoryRange_t) !=
sizeof(RvgPhysicalMemoryRange));
QEMU_BUILD_BUG_ON(offsetof(PGPhysicalMemoryRange_t, physicalAddress) !=
offsetof(RvgPhysicalMemoryRange, physical_address));
QEMU_BUILD_BUG_ON(offsetof(PGPhysicalMemoryRange_t, physicalLength) !=
offsetof(RvgPhysicalMemoryRange, physical_length));
#else
#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
#endif
static const AppleGFXDisplayMode apple_gfx_default_modes[] = {
{ 1920, 1080, 60 },
@@ -251,9 +271,20 @@ static void apple_gfx_render_new_frame(AppleGFXState *s)
* This is not safe to call from the BQL/BH due to PVG-internal locks
* causing deadlocks.
*/
#ifdef APPLE_GFX_BACKEND_REIMS
RvgMTLRegion rvg_region = {
.origin = { region.origin.x, region.origin.y, region.origin.z },
.size = { region.size.width, region.size.height, region.size.depth },
};
bool r = rvg_display_encode_current_frame(s->pgdisp,
(__bridge void *)command_buffer,
(__bridge void *)texture,
rvg_region);
#else
bool r = [s->pgdisp encodeCurrentFrameToCommandBuffer:command_buffer
texture:texture
region:region];
#endif
if (!r) {
[texture release];
[command_buffer release];
@@ -375,7 +406,12 @@ static void set_mode(AppleGFXState *s, uint32_t width, uint32_t height)
width:width
height:height
mipmapped:NO];
#ifdef APPLE_GFX_BACKEND_REIMS
textureDescriptor.usage =
rvg_display_get_minimum_texture_usage(s->pgdisp);
#else
textureDescriptor.usage = s->pgdisp.minimumTextureUsage;
#endif
s->texture = [s->mtl newTextureWithDescriptor:textureDescriptor];
s->using_managed_texture_storage =
(s->texture.storageMode == MTLStorageModeManaged);
@@ -387,8 +423,14 @@ static void set_mode(AppleGFXState *s, uint32_t width, uint32_t height)
static void update_cursor(AppleGFXState *s)
{
assert(bql_locked());
#ifdef APPLE_GFX_BACKEND_REIMS
qemu_console_set_mouse(s->con, rvg_display_get_cursor_position(s->pgdisp).x,
rvg_display_get_cursor_position(s->pgdisp).y,
qatomic_read(&s->cursor_show));
#else
qemu_console_set_mouse(s->con, s->pgdisp.cursorPosition.x,
s->pgdisp.cursorPosition.y, qatomic_read(&s->cursor_show));
#endif
}
static void update_cursor_bh(void *opaque)
@@ -399,20 +441,43 @@ static void update_cursor_bh(void *opaque)
typedef struct AppleGFXSetCursorGlyphJob {
AppleGFXState *s;
#ifdef APPLE_GFX_BACKEND_REIMS
/*
* The RvgCursorGlyph is borrowed for the handler's call only: its
* pixels are copied where the block retained the NSBitmapImageRep.
*/
uint8_t *glyph_rgba;
uint32_t glyph_width;
uint32_t glyph_height;
RvgDisplayCoord hotspot;
#else
NSBitmapImageRep *glyph;
PGDisplayCoord_t hotspot;
#endif
} AppleGFXSetCursorGlyphJob;
static void set_cursor_glyph(void *opaque)
{
AppleGFXSetCursorGlyphJob *job = opaque;
AppleGFXState *s = job->s;
#ifdef APPLE_GFX_BACKEND_REIMS
/*
* The image the framework made from an RvgCursorGlyph: 8 bits a sample,
* 4 samples (RGBA), 32 bits a pixel, width * 4 bytes a row.
*/
uint32_t bpp = 32;
size_t width = job->glyph_width;
size_t height = job->glyph_height;
size_t padding_bytes_per_row = 0;
const uint8_t* px_data = job->glyph_rgba;
#else
NSBitmapImageRep *glyph = job->glyph;
uint32_t bpp = glyph.bitsPerPixel;
size_t width = glyph.pixelsWide;
size_t height = glyph.pixelsHigh;
size_t padding_bytes_per_row = glyph.bytesPerRow - width * 4;
const uint8_t* px_data = glyph.bitmapData;
#endif
trace_apple_gfx_cursor_set(bpp, width, height);
@@ -447,7 +512,11 @@ static void set_cursor_glyph(void *opaque)
qemu_console_set_cursor(s->con, s->cursor);
update_cursor(s);
}
#ifdef APPLE_GFX_BACKEND_REIMS
g_free(job->glyph_rgba);
#else
[glyph release];
#endif
g_free(job);
}
@@ -504,7 +573,11 @@ typedef struct AppleGFXIOJob {
static void apple_gfx_do_read(void *opaque)
{
AppleGFXIOJob *job = opaque;
#ifdef APPLE_GFX_BACKEND_REIMS
job->value = rvg_device_mmio_read(job->state->pgdev, job->offset);
#else
job->value = [job->state->pgdev mmioReadAtOffset:job->offset];
#endif
qatomic_set(&job->completed, true);
aio_wait_kick();
}
@@ -528,7 +601,11 @@ static uint64_t apple_gfx_read(void *opaque, hwaddr offset, unsigned size)
static void apple_gfx_do_write(void *opaque)
{
AppleGFXIOJob *job = opaque;
#ifdef APPLE_GFX_BACKEND_REIMS
rvg_device_mmio_write(job->state->pgdev, job->offset, job->value);
#else
[job->state->pgdev mmioWriteAtOffset:job->offset value:job->value];
#endif
qatomic_set(&job->completed, true);
aio_wait_kick();
}
@@ -577,9 +654,15 @@ static size_t apple_gfx_get_default_mmio_range_size(void)
{
size_t mmio_range_size;
@autoreleasepool {
#ifdef APPLE_GFX_BACKEND_REIMS
RvgDeviceDescriptor *desc = rvg_device_descriptor_create();
mmio_range_size = rvg_device_descriptor_get_mmio_length(desc);
rvg_device_descriptor_release(desc);
#else
PGDeviceDescriptor *desc = [PGDeviceDescriptor new];
mmio_range_size = desc.mmioLength;
[desc release];
#endif
}
return mmio_range_size;
}
@@ -597,24 +680,54 @@ void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name)
/* TODO: PVG framework supports serialising device state: integrate it! */
}
#ifndef APPLE_GFX_BACKEND_REIMS
@interface PGDeviceDescriptor (IOSurfaceMapper)
@property (readwrite, nonatomic, copy, nullable) PGMemoryMapDescriptor* memoryMapDescriptor;
@end
#endif
bool apple_gfx_register_memory_cb(Int128 start, Int128 len,
const MemoryRegion *mr,
hwaddr offset_in_region, void *opaque) {
PGGuestPhysicalRange_t range;
#ifdef APPLE_GFX_BACKEND_REIMS
RvgMemoryMapDescriptor *memory_map_descriptor = opaque;
#else
PGMemoryMapDescriptor *memory_map_descriptor = opaque;
#endif
if (memory_access_is_direct(mr, true, MEMTXATTRS_UNSPECIFIED)) {
range.physicalAddress = start;
range.physicalLength = len;
range.hostAddress = memory_region_get_ram_ptr(mr);
#ifdef APPLE_GFX_BACKEND_REIMS
rvg_memory_map_descriptor_add_range(memory_map_descriptor,
range.physicalAddress,
range.physicalLength,
range.hostAddress);
#else
[memory_map_descriptor addRange:range];
#endif
}
return false;
}
#ifdef APPLE_GFX_BACKEND_REIMS
static void apple_gfx_register_memory(AppleGFXState *s,
RvgDeviceDescriptor *desc)
{
RvgMemoryMapDescriptor *memoryMapDescriptor =
rvg_memory_map_descriptor_create();
FlatView* fv = address_space_to_flatview(&address_space_memory);
flatview_for_each_range(fv, apple_gfx_register_memory_cb, memoryMapDescriptor);
/*
* The descriptor retains the map. QEMU's own reference (+new's) is never
* released, as in the Objective-C version.
*/
rvg_device_descriptor_set_memory_map(desc, memoryMapDescriptor);
}
#else
static void apple_gfx_register_memory(AppleGFXState *s,
PGDeviceDescriptor *desc)
{
@@ -625,7 +738,74 @@ static void apple_gfx_register_memory(AppleGFXState *s,
desc.memoryMapDescriptor = memoryMapDescriptor;
}
#endif
#ifdef APPLE_GFX_BACKEND_REIMS
/*
* The Objective-C version's blocks, as {fn, ctx, destroy} triples: each body
* is the block's, ctx is the AppleGFXState the block captured, and destroy is
* NULL because releasing those blocks released nothing.
*/
static void *apple_gfx_create_task_cb(void *ctx, uint64_t vmSize,
void **baseAddress)
{
AppleGFXState *s = ctx;
PGTask_t *task = apple_gfx_new_task(s, vmSize);
*baseAddress = (void *)task->address;
trace_apple_gfx_create_task(vmSize, *baseAddress);
return task;
}
static void apple_gfx_destroy_task_cb(void *ctx, void *task_ptr)
{
AppleGFXState *s = ctx;
PGTask_t *task = task_ptr;
trace_apple_gfx_destroy_task(task, task->mapped_regions->len);
apple_gfx_destroy_task(s, task);
}
static bool apple_gfx_map_memory_cb(void *ctx, void *task, uint32_t range_count,
uint64_t virtual_offset, bool read_only,
const RvgPhysicalMemoryRange *ranges)
{
AppleGFXState *s = ctx;
return apple_gfx_task_map_memory(s, task, virtual_offset,
(PGPhysicalMemoryRange_t *)ranges,
range_count, read_only);
}
static bool apple_gfx_unmap_memory_cb(void *ctx, void *task,
uint64_t virtual_offset, uint64_t length)
{
AppleGFXState *s = ctx;
apple_gfx_task_unmap_memory(s, task, virtual_offset, length);
return true;
}
static bool apple_gfx_read_memory_cb(void *ctx, uint64_t physical_address,
uint64_t length, void *dst)
{
AppleGFXState *s = ctx;
return apple_gfx_read_memory(s, physical_address, length, dst);
}
static void apple_gfx_register_task_mapping_handlers(AppleGFXState *s,
RvgDeviceDescriptor *desc)
{
rvg_device_descriptor_set_create_task(desc, apple_gfx_create_task_cb,
s, NULL);
rvg_device_descriptor_set_destroy_task(desc, apple_gfx_destroy_task_cb,
s, NULL);
rvg_device_descriptor_set_map_memory(desc, apple_gfx_map_memory_cb,
s, NULL);
rvg_device_descriptor_set_unmap_memory(desc, apple_gfx_unmap_memory_cb,
s, NULL);
rvg_device_descriptor_set_read_memory(desc, apple_gfx_read_memory_cb,
s, NULL);
}
#else
static void apple_gfx_register_task_mapping_handlers(AppleGFXState *s,
PGDeviceDescriptor *desc)
{
@@ -660,6 +840,7 @@ static void apple_gfx_register_task_mapping_handlers(AppleGFXState *s,
return apple_gfx_read_memory(s, physical_address, length, dst);
};
}
#endif
static void new_frame_handler_bh(void *opaque)
{
@@ -679,6 +860,109 @@ static void new_frame_handler_bh(void *opaque)
}
}
#ifdef APPLE_GFX_BACKEND_REIMS
/* The display descriptor's blocks; ctx is the AppleGFXState (see above). */
static void apple_gfx_new_frame_event_cb(void *ctx)
{
AppleGFXState *s = ctx;
trace_apple_gfx_new_frame();
aio_bh_schedule_oneshot(qemu_get_aio_context(), new_frame_handler_bh, s);
}
static void apple_gfx_mode_change_cb(void *ctx, RvgDisplayCoord sizeInPixels,
uint32_t pixelFormat)
{
AppleGFXState *s = ctx;
trace_apple_gfx_mode_change(sizeInPixels.x, sizeInPixels.y);
BQL_LOCK_GUARD();
set_mode(s, sizeInPixels.x, sizeInPixels.y);
}
static void apple_gfx_cursor_glyph_cb(void *ctx, const RvgCursorGlyph *glyph,
RvgDisplayCoord hotspot)
{
AppleGFXState *s = ctx;
AppleGFXSetCursorGlyphJob *job = g_malloc0(sizeof(*job));
/* The image's buffer: height rows of width * 4 bytes. */
size_t length = (size_t)glyph->width * 4 * glyph->height;
job->s = s;
job->glyph_width = glyph->width;
job->glyph_height = glyph->height;
job->hotspot = hotspot;
job->glyph_rgba = g_malloc0(length);
memcpy(job->glyph_rgba, glyph->rgba, MIN(glyph->rgba_length, length));
aio_bh_schedule_oneshot(qemu_get_aio_context(),
set_cursor_glyph, job);
}
/*
* The block took a BOOL, which is C bool on arm64, and the framework calls it
* with the guest's byte unnormalised. RvgCursorShowFn hands over that byte as
* uint8_t; the function keeps the block's bool parameter, and is registered
* through a cast, so that it reads the byte exactly as the block's invoke
* function did.
*/
static void apple_gfx_cursor_show_cb(void *ctx, bool show)
{
AppleGFXState *s = ctx;
trace_apple_gfx_cursor_show(show);
qatomic_set(&s->cursor_show, show);
aio_bh_schedule_oneshot(qemu_get_aio_context(),
update_cursor_bh, s);
}
static void apple_gfx_cursor_move_cb(void *ctx)
{
AppleGFXState *s = ctx;
trace_apple_gfx_cursor_move();
aio_bh_schedule_oneshot(qemu_get_aio_context(),
update_cursor_bh, s);
}
static RvgDisplayDescriptor *apple_gfx_prepare_display_descriptor(AppleGFXState *s)
{
static const char name[] = "QEMU display";
RvgDisplayDescriptor *disp_desc = rvg_display_descriptor_create();
rvg_display_descriptor_set_name(disp_desc, name, sizeof(name) - 1);
rvg_display_descriptor_set_size_in_millimeters(
disp_desc, (RvgSize){ 400., 300. }); /* A 20" display */
rvg_display_descriptor_set_queue(disp_desc,
(__bridge void *)dispatch_get_main_queue());
rvg_display_descriptor_set_new_frame_event_handler(
disp_desc, apple_gfx_new_frame_event_cb, s, NULL);
rvg_display_descriptor_set_mode_change_handler(
disp_desc, apple_gfx_mode_change_cb, s, NULL);
rvg_display_descriptor_set_cursor_glyph_handler(
disp_desc, apple_gfx_cursor_glyph_cb, s, NULL);
rvg_display_descriptor_set_cursor_show_handler(
disp_desc,
(RvgCursorShowFn)(void (*)(void))apple_gfx_cursor_show_cb, s, NULL);
rvg_display_descriptor_set_cursor_move_handler(
disp_desc, apple_gfx_cursor_move_cb, s, NULL);
return disp_desc;
}
static RvgDisplayMode **apple_gfx_create_display_mode_array(
const AppleGFXDisplayMode display_modes[], uint32_t display_mode_count)
{
RvgDisplayMode **mode_array = g_new0(RvgDisplayMode *, display_mode_count);
for (unsigned i = 0; i < display_mode_count; i++) {
const AppleGFXDisplayMode *mode = &display_modes[i];
trace_apple_gfx_display_mode(i, mode->width_px, mode->height_px);
RvgDisplayCoord mode_size = { mode->width_px, mode->height_px };
mode_array[i] =
rvg_display_mode_create(mode_size, mode->refresh_rate_hz);
}
return mode_array;
}
#else
static PGDisplayDescriptor *apple_gfx_prepare_display_descriptor(AppleGFXState *s)
{
PGDisplayDescriptor *disp_desc = [PGDisplayDescriptor new];
@@ -743,6 +1027,7 @@ static NSArray<PGDisplayMode *> *apple_gfx_create_display_mode_array(
return mode_array;
}
#endif
static id<MTLDevice> copy_suitable_metal_device(void)
{
@@ -770,6 +1055,15 @@ static id<MTLDevice> copy_suitable_metal_device(void)
return dev;
}
#ifdef APPLE_GFX_BACKEND_REIMS
bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
RvgDeviceDescriptor *desc, Error **errp)
{
RvgDisplayDescriptor *disp_desc;
const AppleGFXDisplayMode *display_modes = apple_gfx_default_modes;
uint32_t num_display_modes = ARRAY_SIZE(apple_gfx_default_modes);
RvgDisplayMode **mode_array;
#else
bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
PGDeviceDescriptor *desc, Error **errp)
{
@@ -777,6 +1071,7 @@ bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
const AppleGFXDisplayMode *display_modes = apple_gfx_default_modes;
uint32_t num_display_modes = ARRAY_SIZE(apple_gfx_default_modes);
NSArray<PGDisplayMode *> *mode_array;
#endif
if (apple_gfx_mig_blocker == NULL) {
error_setg(&apple_gfx_mig_blocker,
@@ -791,7 +1086,11 @@ bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
s->mtl = copy_suitable_metal_device();
s->mtl_queue = [s->mtl newCommandQueue];
#ifdef APPLE_GFX_BACKEND_REIMS
rvg_device_descriptor_set_metal_device(desc, (__bridge void *)s->mtl);
#else
desc.device = s->mtl;
#endif
/*
* The legacy memory management interface doesn't allow for
@@ -806,7 +1105,11 @@ bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
s->cursor_show = true;
#ifdef APPLE_GFX_BACKEND_REIMS
s->pgdev = rvg_new_device(desc);
#else
s->pgdev = PGNewDeviceWithDescriptor(desc);
#endif
disp_desc = apple_gfx_prepare_display_descriptor(s);
/*
@@ -817,19 +1120,38 @@ bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
* guest will ignore these displays if they share the same serial number,
* so ensure each instance gets a unique one.
*/
#ifdef APPLE_GFX_BACKEND_REIMS
s->pgdisp = rvg_device_new_display(s->pgdev, disp_desc, 0,
next_pgdisplay_serial_num++);
rvg_display_descriptor_release(disp_desc);
#else
s->pgdisp = [s->pgdev newDisplayWithDescriptor:disp_desc
port:0
serialNum:next_pgdisplay_serial_num++];
[disp_desc release];
#endif
if (s->display_modes != NULL && s->num_display_modes > 0) {
trace_apple_gfx_common_realize_modes_property(s->num_display_modes);
display_modes = s->display_modes;
num_display_modes = s->num_display_modes;
}
#ifdef APPLE_GFX_BACKEND_REIMS
mode_array =
apple_gfx_create_display_mode_array(display_modes, num_display_modes);
rvg_display_set_mode_list(s->pgdisp,
(const RvgDisplayMode *const *)mode_array,
num_display_modes);
/* The array's release: the display keeps its own copies. */
for (uint32_t i = 0; i < num_display_modes; i++) {
rvg_display_mode_release(mode_array[i]);
}
g_free(mode_array);
#else
s->pgdisp.modeList = mode_array =
apple_gfx_create_display_mode_array(display_modes, num_display_modes);
[mode_array release];
#endif
s->con = qemu_graphic_console_create(dev, 0, &apple_gfx_fb_ops, s);
return true;
+48 -17
View File
@@ -851,26 +851,50 @@ elif host_os == 'darwin'
# install name of each dylib it links and dyld loads that path, so linking
# one that kept the system's (/System/Library/Frameworks/...) would load
# the system's copy at run time.
# With apple_gfx_backend=reims, apple-gfx drives reims-vgpu's C API
# (reims_vgpu.h) instead, and pvg_framework holds a ReimsVGPU.framework
# (paravirt-decompile's `cargo xtask bundle`); the same checks apply to it.
apple_gfx_backend = get_option('apple_gfx_backend')
if apple_gfx_backend == 'reims'
pvg_name = 'ReimsVGPU'
pvg_header = 'reims_vgpu.h'
pvg_builder = '`cargo xtask bundle` in rust/'
pvg_builder_into = 'into rust/target/Frameworks with ' + pvg_builder
else
pvg_name = 'ParavirtualizedGraphics'
pvg_header = 'ParavirtualizedGraphics.h'
pvg_builder = '`make qemu-framework`'
pvg_builder_into = 'into build/qemu-framework with ' + pvg_builder
endif
pvg_framework = get_option('pvg_framework')
# reims is never the default, so choosing it asks for apple-gfx: with pvg
# auto it counts as pvg enabled, and with pvg disabled it has no effect,
# which is said rather than passed over.
pvg_required = (get_option('pvg').enabled() or
(apple_gfx_backend == 'reims' and get_option('pvg').allowed()))
if apple_gfx_backend == 'reims' and get_option('pvg').disabled()
warning('apple_gfx_backend=reims has no effect: pvg is disabled, so ' +
'QEMU builds no apple-gfx')
endif
if get_option('pvg').allowed() and pvg_framework == ''
if get_option('pvg').enabled()
error('pvg needs pvg_framework (configure --pvg-framework=DIR), the ' +
'directory holding a ParavirtualizedGraphics.framework built ' +
'from source: paravirt-decompile builds one into ' +
'build/qemu-framework with `make qemu-framework`')
if pvg_required
error((get_option('pvg').enabled() ? 'pvg' : 'apple_gfx_backend=reims') +
' needs pvg_framework (configure --pvg-framework=DIR), the ' +
'directory holding a ' + pvg_name + '.framework built ' +
'from source: paravirt-decompile builds one ' + pvg_builder_into)
endif
elif get_option('pvg').allowed()
if not fs.is_absolute(pvg_framework)
error('pvg_framework must be an absolute path, since it is also the ' +
'rpath: ' + pvg_framework)
endif
pvg_bundle = pvg_framework / 'ParavirtualizedGraphics.framework'
pvg_binary = pvg_bundle / 'ParavirtualizedGraphics'
pvg_bundle = pvg_framework / pvg_name + '.framework'
pvg_binary = pvg_bundle / pvg_name
if not (fs.is_file(pvg_binary) and
fs.is_file(pvg_bundle / 'Headers' / 'ParavirtualizedGraphics.h'))
fs.is_file(pvg_bundle / 'Headers' / pvg_header))
error('pvg_framework: ' + pvg_framework + ' holds no ' +
'ParavirtualizedGraphics.framework with a binary and Headers; ' +
'paravirt-decompile builds one with `make qemu-framework`')
pvg_name + '.framework with a binary and Headers/' + pvg_header +
'; paravirt-decompile builds one with ' + pvg_builder)
endif
# otool -D prints "<path>:" or "<path> (architecture <arch>):", then the
# install name, per slice, or a complaint such as "<path>: is not an
@@ -893,16 +917,22 @@ elif host_os == 'darwin'
error('pvg_framework: ' + pvg_binary + ' has the install name ' +
id + ', which QEMU would record and dyld load in its place. ' +
'Build the @rpath variant: paravirt-decompile\'s ' +
'`make qemu-framework`')
pvg_builder)
endif
endforeach
metal = dependency('appleframeworks', modules: 'Metal',
required: get_option('pvg'))
required: pvg_required)
if metal.found()
pvg_compile_args = ['-F' + pvg_framework]
pvg_link_args = ['-F' + pvg_framework, '-framework', pvg_name]
if apple_gfx_backend == 'reims'
# apple-gfx*.m then import Metal themselves: reims_vgpu.h, unlike
# ParavirtualizedGraphics.h, does not.
pvg_compile_args += ['-DAPPLE_GFX_BACKEND_REIMS']
endif
pvg = declare_dependency(
compile_args: ['-F' + pvg_framework],
link_args: ['-F' + pvg_framework, '-framework', 'ParavirtualizedGraphics',
'-Wl,-rpath,' + pvg_framework],
compile_args: pvg_compile_args,
link_args: pvg_link_args + ['-Wl,-rpath,' + pvg_framework],
dependencies: metal)
endif
endif
@@ -5053,9 +5083,10 @@ if host_os == 'freebsd'
summary_info += {'libinotify-kqueue': inotify}
endif
if host_os == 'darwin'
summary_info += {'ParavirtualizedGraphics support': pvg}
summary_info += {'apple-gfx backend': apple_gfx_backend}
summary_info += {pvg_name + ' support': pvg}
if pvg.found()
summary_info += {'ParavirtualizedGraphics framework': pvg_framework}
summary_info += {pvg_name + ' framework': pvg_framework}
endif
endif
summary_info += {'valgrind': valgrind}
+3 -1
View File
@@ -207,7 +207,9 @@ option('lzo', type : 'feature', value : 'auto',
option('pvg', type: 'feature', value: 'auto',
description: 'macOS paravirtualized graphics support')
option('pvg_framework', type: 'string', value: '',
description: 'directory of a ParavirtualizedGraphics.framework built from source, with an @rpath install name, for apple-gfx to link')
description: 'directory of the framework apple-gfx links, built from source with an @rpath install name: a ParavirtualizedGraphics.framework, or a ReimsVGPU.framework with apple_gfx_backend=reims')
option('apple_gfx_backend', type: 'combo', choices: ['pg', 'reims'], value: 'pg',
description: 'library apple-gfx drives: pg (a ParavirtualizedGraphics.framework) or reims (a ReimsVGPU.framework, its C API); pvg_framework is the directory holding it')
option('rbd', type : 'feature', value : 'auto',
description: 'Ceph block device driver')
option('opengl', type : 'feature', value : 'auto',
+10 -3
View File
@@ -1,5 +1,10 @@
# This file is generated by meson-buildoptions.py, do not edit!
meson_options_help() {
printf "%s\n" ' --apple-gfx-backend=CHOICE'
printf "%s\n" ' library apple-gfx drives: pg (a'
printf "%s\n" ' ParavirtualizedGraphics.framework) or reims (a'
printf "%s\n" ' ReimsVGPU.framework, its C API); pvg_framework is'
printf "%s\n" ' the directory holding it [pg] (choices: pg/reims)'
printf "%s\n" ' --audio-drv-list=CHOICES Set audio driver list [default] (choices: alsa/co'
printf "%s\n" ' reaudio/default/dsound/jack/oss/pa/pipewire/sdl/s'
printf "%s\n" ' ndio)'
@@ -68,9 +73,10 @@ meson_options_help() {
printf "%s\n" ' --localstatedir=VALUE Localstate data directory [/var/local]'
printf "%s\n" ' --mandir=VALUE Manual page directory [share/man]'
printf "%s\n" ' --prefix=VALUE Installation prefix [/usr/local]'
printf "%s\n" ' --pvg-framework=VALUE directory of a ParavirtualizedGraphics.framework'
printf "%s\n" ' built from source, with an @rpath install name,'
printf "%s\n" ' for apple-gfx to link'
printf "%s\n" ' --pvg-framework=VALUE directory of the framework apple-gfx links, built'
printf "%s\n" ' from source with an @rpath install name: a'
printf "%s\n" ' ParavirtualizedGraphics.framework, or a'
printf "%s\n" ' ReimsVGPU.framework with apple_gfx_backend=reims'
printf "%s\n" ' --qemu-ga-distro=VALUE second path element in qemu-ga registry entries'
printf "%s\n" ' [Linux]'
printf "%s\n" ' --qemu-ga-manufacturer=VALUE'
@@ -241,6 +247,7 @@ _meson_option_parse() {
--disable-af-xdp) printf "%s" -Daf_xdp=disabled ;;
--enable-alsa) printf "%s" -Dalsa=enabled ;;
--disable-alsa) printf "%s" -Dalsa=disabled ;;
--apple-gfx-backend=*) quote_sh "-Dapple_gfx_backend=$2" ;;
--enable-asan) printf "%s" -Dasan=true ;;
--disable-asan) printf "%s" -Dasan=false ;;
--enable-attr) printf "%s" -Dattr=enabled ;;