hw/display/apple-gfx: Introduce ParavirtualizedGraphics.Framework support

MacOS provides a framework (library) that allows any vmm to implement a
paravirtualized 3d graphics passthrough to the host metal stack called
ParavirtualizedGraphics.Framework (PVG). The library abstracts away
almost every aspect of the paravirtualized device model and only provides
and receives callbacks on MMIO access as well as to share memory address
space between the VM and PVG.

This patch implements a QEMU device that drives PVG for the VMApple
variant of it.

Signed-off-by: Alexander Graf <graf@amazon.com>
Co-authored-by: Alexander Graf <graf@amazon.com>

Subsequent changes:

 * Cherry-pick/rebase conflict fixes, API use updates.
 * Moved from hw/vmapple/ (useful outside that machine type)
 * Overhaul of threading model, many thread safety improvements.
 * Asynchronous rendering.
 * Memory and object lifetime fixes.
 * Refactoring to split generic and (vmapple) MMIO variant specific
   code.

Implementation wise, most of the complexity lies in the differing threading
models of ParavirtualizedGraphics.framework, which uses libdispatch and
internal locks, versus QEMU, which heavily uses the BQL, especially during
memory-mapped device I/O. Great care has therefore been taken to prevent
deadlocks by never calling into PVG methods while holding the BQL, and
similarly never acquiring the BQL in a callback from PVG. Different strategies
have been used (libdispatch, blocking and non-blocking BHs, RCU, etc.)
depending on the specific requirements at each framework entry and exit point.

Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Tested-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Message-ID: <20241223221645.29911-3-phil@philjordan.eu>
[PMD: Re-ordered imported headers, style fixups]
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
This commit is contained in:
Phil Dennis-Jordan 2023-06-14 22:57:33 +00:00 committed by Philippe Mathieu-Daudé
parent f5ab12caba
commit 2352159c97
7 changed files with 1171 additions and 0 deletions

63
hw/display/apple-gfx.h Normal file
View file

@ -0,0 +1,63 @@
/*
* Data structures and functions shared between variants of the macOS
* ParavirtualizedGraphics.framework based apple-gfx display adapter.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef QEMU_APPLE_GFX_H
#define QEMU_APPLE_GFX_H
#include "qemu/queue.h"
#include "exec/memory.h"
#include "hw/qdev-properties.h"
#include "ui/surface.h"
#define TYPE_APPLE_GFX_MMIO "apple-gfx-mmio"
@class PGDeviceDescriptor;
@protocol PGDevice;
@protocol PGDisplay;
@protocol MTLDevice;
@protocol MTLTexture;
@protocol MTLCommandQueue;
typedef QTAILQ_HEAD(, PGTask_s) PGTaskList;
typedef struct AppleGFXState {
/* Initialised on init/realize() */
MemoryRegion iomem_gfx;
id<PGDevice> pgdev;
id<PGDisplay> pgdisp;
QemuConsole *con;
id<MTLDevice> mtl;
id<MTLCommandQueue> mtl_queue;
/* List `tasks` is protected by task_mutex */
QemuMutex task_mutex;
PGTaskList tasks;
/* Mutable state (BQL protected) */
QEMUCursor *cursor;
DisplaySurface *surface;
id<MTLTexture> texture;
int8_t pending_frames; /* # guest frames in the rendering pipeline */
bool gfx_update_requested; /* QEMU display system wants a new frame */
bool new_frame_ready; /* Guest has rendered a frame, ready to be used */
bool using_managed_texture_storage;
uint32_t rendering_frame_width;
uint32_t rendering_frame_height;
/* Mutable state (atomic) */
bool cursor_show;
} AppleGFXState;
void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name);
bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev,
PGDeviceDescriptor *desc, Error **errp);
void *apple_gfx_host_ptr_for_gpa_range(uint64_t guest_physical,
uint64_t length, bool read_only,
MemoryRegion **mapping_in_region);
#endif