From 17ef6f3c73fbf8551f80854c745ba7a89a7607e0 Mon Sep 17 00:00:00 2001 From: TUNA_BIN <65464657+TUNA-NOPE@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:44:19 +0000 Subject: [PATCH 1/5] qemu 0014: the VideoToolbox and M2 scaler devices (upstream 4895365e5b) The pinned QEMU (bd95509) predates upstream's apple-videotoolbox-pci and apple-m2scaler-pci, so a macbox build could not attach Reims's hardware video devices. 0014 is upstream's commit as it is; it goes away when QEMU_REF moves past it. Built and run on Tahoe since 2026-11-13 19:00Z (qemu 20261113-190049-r8dea1e59-qaed2b83-video). --- src/patches/README.md | 3 +- ...pple-videotoolbox-pci-and-apple-m2sc.patch | 1456 +++++++++++++++++ 2 files changed, 1458 insertions(+), 1 deletion(-) create mode 100644 src/patches/qemu/0014-hw-display-add-apple-videotoolbox-pci-and-apple-m2sc.patch diff --git a/src/patches/README.md b/src/patches/README.md index f22aec5..b26dd85 100644 --- a/src/patches/README.md +++ b/src/patches/README.md @@ -59,13 +59,14 @@ is in [docs/performance.md](../../docs/performance.md#gpu-tuning-switches). | 0030 `REIMS_VGPU_PAGED_BUFFER_BY_PAGES` | finds kept paged buffers by their guest pages; off | | 0031–0033 staged page-ups | `REIMS_VGPU_STAGED_PAGE_UP`, `REIMS_VGPU_STAGED_ZERO`: a new texture's first page-up copies its pages into a staging buffer (one copy per run) when no live view covers them, and pages of zeros write nothing. Before, each page run took its own mapping: 55k a second and a storm of TLB shootdowns; the simulator went from 89 to 101 fps. On with Coherent | -## QEMU (`vendor/qemu` at bd95509, github.com/steelbrain/qemu-reims-vgpu), 13 patches +## QEMU (`vendor/qemu` at bd95509, github.com/steelbrain/qemu-reims-vgpu), 14 patches | Patches | What they do | |---|---| | 0001–0003, 0006–0009 the census | memslots move to a second mapping of guest RAM that only KVM uses, and userfaultfd markers there report the guest CPU's touches of tracked pages (`UFFD_USER_MODE_ONLY`: no capability or `/dev/userfaultfd` needed). QEMU's own loads, stores and DMA reach it too. Off unless `REIMS_VGPU_CENSUS` is set. | | 0004, 0005 coherent pages | with `REIMS_VGPU_COHERENT=1` the same markers serve metal2vulkan's trap-coherent surfaces. A run that doesn't end well leaves `coherent.running` in the census directory, and every later start boots without coherent pages until it's removed (`macbox coherent reset`), so a fault there costs one crash, not a crash loop. | | 0010–0013 KVM shortcuts for the GPU device | `x-irqfd` (interrupts through a KVM irqfd), `x-halt-poll-ns`, `x-ioeventfd` (doorbells through KVM ioeventfds): device properties, all off unless set | +| 0014 the video devices | upstream's own 4895365e5b (on `steelbrain/reims-vgpu`, after bd95509), unchanged: `apple-videotoolbox-pci` (106b:1a03) and `apple-m2scaler-pci` (106b:1a0b), the transports of Reims's VideoToolbox and scaler devices. Without it the build can't attach them. Drop it when `QEMU_REF` moves past 4895365e5b. The devices stay detached unless `MACBOX_VIDEO=on` | ## Licenses diff --git a/src/patches/qemu/0014-hw-display-add-apple-videotoolbox-pci-and-apple-m2sc.patch b/src/patches/qemu/0014-hw-display-add-apple-videotoolbox-pci-and-apple-m2sc.patch new file mode 100644 index 0000000..84aa6e3 --- /dev/null +++ b/src/patches/qemu/0014-hw-display-add-apple-videotoolbox-pci-and-apple-m2sc.patch @@ -0,0 +1,1456 @@ +From aed2b8368377489ed9c0609a92afa8b9c23ef228 Mon Sep 17 00:00:00 2001 +From: Anees Iqbal +Date: Sun, 25 Oct 2026 09:52:45 +0200 +Subject: [PATCH] hw/display: add apple-videotoolbox-pci and apple-m2scaler-pci + +A macOS guest hands its VideoToolbox sessions and its IOSurfaceAccelerator's +scaling and conversion to the host through two virtio-PCI devices, 106b:1a03 +and 106b:1a0b, as Virtualization.framework gives them. These are their +transports, on reims-vgpu's video device (rvg_video_device_*) and scaler +device (rvg_scaler_device_*), beside apple-gfx. + +apple-videotoolbox-pci: +- modern virtio only, no device features and no device configuration, + MSI-X with a vector per queue and one for the configuration, and the + capabilities linked in ascending address order, as vmapple-virtio-blk-pci + links them; +- queue 0 carries the guest's messages, 1024 descriptors deep so that an + x86_64 guest's largest message, 0x200000 bytes on 4 KiB pages, fits: each + element's readable part, gathered across its descriptors, goes to the + library, and the status it answers is scattered into the writable part, + little-endian; a writable part under 4 bytes gets none; +- a readable part over RVG_VIDEO_MAX_MESSAGE_SIZE (16 MiB) is answered + RVG_VIDEO_ALLOCATION_FAILED from its descriptors' sizes, before anything + is allocated or copied, as rvg_video_device_deliver answers such a + message: a guest's 1024 descriptors, each up to 4 GiB, can add up to many + times the host's memory; +- queue 1 carries the host's: the library's messages, which come on its own + threads, wait in a ring and fill the guest's buffers as it posts them; a + message longer than the buffer ends QEMU, as Apple's host ends the VM; +- DRIVER_OK starts the library's host session, and a reset ends it. + +apple-m2scaler-pci: +- the same transport with one queue; each element's readable part, up to + 64 KiB, is copied into one buffer, which the library answers in place; + a larger element, or a refusal, sets DEVICE_NEEDS_RESET with no reply, + and an abort of Apple's host ends QEMU; +- the guest memory a request names is reached through the device's DMA + address space, and only where it is RAM, through MemoryRegionCaches, so + what is checked as RAM is what is copied; +- DRIVER_OK makes the library's accelerator, a needs-reset where it cannot, + and a reset drops it; +- it realizes on every host. Where the library's device does not scale + (rvg_scaler_device_scales), validate_features refuses the guest's + features with -ENOTSUP, traced as apple_m2scaler_features_refused, so + FEATURES_OK does not stay set: the guest's driver reads its status back, + gives its start up before DRIVER_OK, and the guest does without the + device, as in a VM without it. + +Both answer each element on a worker thread of the device's, as Apple's +host answers on the device's own queue, so the main loop never waits out a +decode or a transform; the status comes back through a bottom half, which +returns the element and only then pops the next, in the guest's order. A +reset and the unrealize wait for the worker's element and discard its +answer; the worker never takes the BQL. + +Neither device has a property of its own, so a user sets nothing on either: +the library picks what both decode, encode and transform with, from the GPU +apple-gfx-pci's device runs on, and logs its choice and why. Both find the +IOSurfaces a request names through the process's latest IOSurface device, +so neither needs a link. + +Not for upstream: written with an AI assistant, which QEMU's policy on +AI-generated content (docs/devel/code-provenance.rst) declines. +--- + hw/display/Kconfig | 10 + + hw/display/apple-m2scaler-pci.c | 659 ++++++++++++++++++++++++++++ + hw/display/apple-videotoolbox-pci.c | 648 +++++++++++++++++++++++++++ + hw/display/meson.build | 8 + + hw/display/trace-events | 16 + + 5 files changed, 1341 insertions(+) + create mode 100644 hw/display/apple-m2scaler-pci.c + create mode 100644 hw/display/apple-videotoolbox-pci.c + +diff --git a/hw/display/Kconfig b/hw/display/Kconfig +index 060274b1e6..2122c62d21 100644 +--- a/hw/display/Kconfig ++++ b/hw/display/Kconfig +@@ -158,3 +158,13 @@ config MAC_PVG_PCI + bool + depends on MAC_PVG && PCI + default y if PCI_DEVICES ++ ++config MAC_PVG_VIDEO_PCI ++ bool ++ depends on MAC_PVG && PCI && VIRTIO_PCI ++ default y if PCI_DEVICES ++ ++config MAC_PVG_SCALER_PCI ++ bool ++ depends on MAC_PVG && PCI && VIRTIO_PCI ++ default y if PCI_DEVICES +diff --git a/hw/display/apple-m2scaler-pci.c b/hw/display/apple-m2scaler-pci.c +new file mode 100644 +index 0000000000..1acd875cdf +--- /dev/null ++++ b/hw/display/apple-m2scaler-pci.c +@@ -0,0 +1,659 @@ ++/* ++ * QEMU Apple paravirtualized M2 scaler device, virtio-PCI 106b:1a0b ++ * ++ * SPDX-License-Identifier: GPL-2.0-or-later ++ * ++ * A macOS guest's AppleM2ScalerParavirtDriver kext hands its ++ * IOSurfaceAccelerator's scaling and conversion to the host through this ++ * device, as Virtualization.framework gives it. Its host side is reims-vgpu's ++ * scaler device (rvg_scaler_device_*, reims_vgpu.h); this file is its ++ * transport, a modern (1.x) virtio device with Apple's IDs, no device features ++ * and no device configuration, and one queue: ++ * ++ * - Each element is one request: its readable part, at most 64 KiB, is ++ * copied whole into one buffer, which the library checks, answers and ++ * rewrites in place. The ++ * reply is all of it, written into the writable part, with a used length ++ * of its size. A request the library refuses sets DEVICE_NEEDS_RESET, with ++ * no reply, and one it says ends the VM ends the process, as Apple's host ++ * does each. ++ * - The library answers on the device's worker thread, not the main loop: a ++ * transform takes the GPU for as long as it takes, and the VM's main loop ++ * runs meanwhile. One element is with the worker at a time; its answer ++ * goes back on the main loop, and only then is the next popped, so the ++ * elements are answered one by one in the guest's order, as Apple's host ++ * answers them on the device's own queue. ++ * - The guest's memory the requests name (the capabilities a type 1 request ++ * asks for, a transform's side buffers) is reached through the device's ++ * DMA address space, and only where it is RAM, as Apple's host reaches ++ * only the guest's RAM. ++ * ++ * The IOSurfaces a transform names are resolved by the library, through the ++ * process's latest IOSurface device (apple-gfx-iosurface-pci on q35, ++ * apple-gfx-mmio on vmapple), as apple-videotoolbox-pci's are. ++ * ++ * DRIVER_OK makes the library's accelerator; a reset drops it, once the ++ * element with the worker is answered, which the reset then discards. ++ * ++ * The device has no properties of its own: the library transforms on the ++ * host's video processing, which it picks itself and logs: on the GPU ++ * apple-videotoolbox-pci's backend runs on where it has it, sharing that ++ * backend, else on another of the host's GPUs that has it. It realizes on ++ * every host. Where the host has none (rvg_scaler_device_scales), the device ++ * refuses the guest's features, leaving FEATURES_OK unset: the guest's driver ++ * reads back the status bits it sets, so its start fails before DRIVER_OK, ++ * and the guest does without the device, as in a VM without it. ++ * ++ * Not for upstream: written with an AI assistant, which QEMU's policy on ++ * AI-generated content (docs/devel/code-provenance.rst) declines. ++ */ ++ ++#include "qemu/osdep.h" ++#include "qapi/error.h" ++#include "qemu/bswap.h" ++#include "qemu/error-report.h" ++#include "qemu/iov.h" ++#include "qemu/main-loop.h" ++#include "qemu/module.h" ++#include "qemu/rcu.h" ++#include "qemu/thread.h" ++#include "hw/pci/pci.h" ++#include "hw/core/qdev-properties.h" ++#include "hw/virtio/virtio.h" ++#include "hw/virtio/virtio-pci.h" ++#include "migration/vmstate.h" ++#include "system/memory.h" ++#include "system/memory_cached.h" ++#include "standard-headers/linux/virtio_ids.h" ++#include "trace.h" ++#include ++ ++#define TYPE_APPLE_M2SCALER "apple-m2scaler" ++OBJECT_DECLARE_SIMPLE_TYPE(AppleM2Scaler, APPLE_M2SCALER) ++ ++#define TYPE_APPLE_M2SCALER_PCI "apple-m2scaler-pci" ++OBJECT_DECLARE_SIMPLE_TYPE(AppleM2ScalerPCI, APPLE_M2SCALER_PCI) ++ ++/* The queue's size: ours, as Apple's is not read. */ ++#define APPLE_M2SCALER_QUEUE_SIZE 256 ++ ++/* ++ * The largest readable part the library takes, reims-vgpu-video-core's ++ * scaler::MAX_REQUEST_SIZE: ours. A larger one is a needs-reset, checked here ++ * before the element is copied, so that a guest cannot have QEMU allocate and ++ * copy what its descriptors add up to (Apple's host copies any size). The ++ * guest kext's largest request is 0x608 bytes. ++ */ ++#define APPLE_M2SCALER_MAX_REQUEST 0x10000 ++ ++/* ++ * One element with the worker: its readable part, copied, which the library ++ * rewrites into the reply, and the library's answer. ++ */ ++typedef struct AppleM2ScalerJob { ++ VirtQueueElement *elem; ++ uint8_t *request; ++ size_t length; ++ size_t room; ++ RvgScalerOutcome outcome; ++ char reason[128]; ++} AppleM2ScalerJob; ++ ++struct AppleM2Scaler { ++ VirtIODevice parent_obj; ++ ++ VirtQueue *requests; ++ RvgScalerDevice *device; ++ /* Sets DEVICE_NEEDS_RESET after a DRIVER_OK that made no accelerator. */ ++ QEMUBH *start_failed_bh; ++ ++ /* ++ * The worker, which calls rvg_scaler_device_process. Under lock: job, the ++ * element it has or is to take (NULL once it answered), done, its answer ++ * until answered_bh takes it, and ending. queued is the main loop's alone: ++ * an element is with the worker or answered and not yet returned, so no ++ * other is popped. ++ */ ++ QemuThread worker; ++ QemuMutex lock; ++ QemuCond wake; ++ QemuCond answered; ++ AppleM2ScalerJob *job; ++ AppleM2ScalerJob *done; ++ bool ending; ++ bool queued; ++ /* Returns the worker's answer to the guest, on the main loop. */ ++ QEMUBH *answered_bh; ++}; ++ ++struct AppleM2ScalerPCI { ++ VirtIOPCIProxy parent_obj; ++ ++ AppleM2Scaler vdev; ++}; ++ ++/* ++ * The pieces of [address, address + length) of the device's DMA address ++ * space, each mapped by a MemoryRegionCache: ours. A range takes one piece a ++ * memory region; the guest's requests name a few kilobytes, so more than ++ * eight pieces is refused, as is a range that wraps. ++ */ ++#define APPLE_M2SCALER_MAX_PIECES 8 ++ ++typedef struct AppleM2ScalerRange { ++ MemoryRegionCache pieces[APPLE_M2SCALER_MAX_PIECES]; ++ hwaddr lengths[APPLE_M2SCALER_MAX_PIECES]; ++ unsigned count; ++} AppleM2ScalerRange; ++ ++static void apple_m2scaler_unmap(AppleM2ScalerRange *r) ++{ ++ while (r->count) { ++ address_space_cache_destroy(&r->pieces[--r->count]); ++ } ++} ++ ++/* ++ * Maps all of [address, address + length) (length 0: the address alone), ++ * every piece the guest's RAM and not a RAM device, or none of it. On the ++ * worker, without the BQL: each piece holds its memory region, so what is ++ * checked here is what is copied, and no access goes to a region whose ++ * dispatch would take the BQL, which the main loop may hold waiting for the ++ * worker (apple_m2scaler_drain). The DMA address space is taken as it maps: ++ * behind an IOMMU nothing would be RAM here, and every request naming the ++ * guest's memory would be refused. The guests this device serves have none. ++ */ ++static bool apple_m2scaler_map(AppleM2Scaler *s, uint64_t address, ++ uint64_t length, bool is_write, ++ AppleM2ScalerRange *r) ++{ ++ AddressSpace *as = VIRTIO_DEVICE(s)->dma_as; ++ hwaddr at = address, left = length ? length : 1; ++ ++ r->count = 0; ++ if (at + left < at) { ++ return false; ++ } ++ while (left) { ++ MemoryRegionCache *piece; ++ MemoryRegion *mr; ++ int64_t got; ++ ++ if (r->count == APPLE_M2SCALER_MAX_PIECES) { ++ apple_m2scaler_unmap(r); ++ return false; ++ } ++ piece = &r->pieces[r->count++]; ++ got = address_space_cache_init(piece, as, at, left, is_write); ++ mr = piece->mrs.mr; ++ if (got <= 0 || !piece->ptr || !memory_region_is_ram(mr) || ++ memory_region_is_ram_device(mr)) { ++ apple_m2scaler_unmap(r); ++ return false; ++ } ++ r->lengths[r->count - 1] = got; ++ at += got; ++ left -= got; ++ } ++ return true; ++} ++ ++/* The library's is_ram, read and write: on the worker. */ ++static bool apple_m2scaler_is_ram(void *ctx, uint64_t address, ++ uint64_t length) ++{ ++ AppleM2ScalerRange r; ++ ++ if (!apple_m2scaler_map(ctx, address, length, false, &r)) { ++ return false; ++ } ++ apple_m2scaler_unmap(&r); ++ return true; ++} ++ ++static bool apple_m2scaler_read(void *ctx, uint64_t address, uint8_t *bytes, ++ size_t length) ++{ ++ AppleM2ScalerRange r; ++ size_t at = 0; ++ ++ if (!length) { ++ return apple_m2scaler_is_ram(ctx, address, 0); ++ } ++ if (!apple_m2scaler_map(ctx, address, length, false, &r)) { ++ return false; ++ } ++ for (unsigned i = 0; i < r.count; i++) { ++ address_space_read_cached(&r.pieces[i], 0, bytes + at, r.lengths[i]); ++ at += r.lengths[i]; ++ } ++ apple_m2scaler_unmap(&r); ++ return true; ++} ++ ++static bool apple_m2scaler_write(void *ctx, uint64_t address, ++ const uint8_t *bytes, size_t length) ++{ ++ AppleM2ScalerRange r; ++ size_t at = 0; ++ ++ if (!length) { ++ return apple_m2scaler_is_ram(ctx, address, 0); ++ } ++ if (!apple_m2scaler_map(ctx, address, length, true, &r)) { ++ return false; ++ } ++ for (unsigned i = 0; i < r.count; i++) { ++ address_space_write_cached(&r.pieces[i], 0, bytes + at, r.lengths[i]); ++ address_space_cache_invalidate(&r.pieces[i], 0, r.lengths[i]); ++ at += r.lengths[i]; ++ } ++ apple_m2scaler_unmap(&r); ++ return true; ++} ++ ++static void apple_m2scaler_job_free(AppleM2ScalerJob *job) ++{ ++ g_free(job->request); ++ g_free(job->elem); ++ g_free(job); ++} ++ ++/* ++ * The worker: takes each job, has the library answer it, and hands the ++ * answer to the main loop. An end of the VM ends the process here, as Apple's ++ * host ends it. ++ */ ++static void *apple_m2scaler_worker(void *opaque) ++{ ++ AppleM2Scaler *s = opaque; ++ ++ rcu_register_thread(); ++ for (;;) { ++ AppleM2ScalerJob *job; ++ ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ while (!s->job && !s->ending) { ++ qemu_cond_wait(&s->wake, &s->lock); ++ } ++ job = s->job; ++ } ++ if (!job) { ++ break; ++ } ++ trace_apple_m2scaler_work(job->length); ++ job->outcome = rvg_scaler_device_process(s->device, job->request, ++ job->length, job->room, ++ job->reason, ++ sizeof(job->reason)); ++ trace_apple_m2scaler_request( ++ job->length, job->room, job->outcome, ++ job->length >= 8 ? ldl_le_p(job->request) : 0, ++ job->length >= 8 ? ldl_le_p(job->request + 4) : 0); ++ if (job->outcome == RVG_SCALER_END_VM) { ++ error_report("%s: %s", TYPE_APPLE_M2SCALER_PCI, job->reason); ++ abort(); ++ } ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ s->job = NULL; ++ s->done = job; ++ qemu_cond_broadcast(&s->answered); ++ } ++ qemu_bh_schedule(s->answered_bh); ++ } ++ rcu_unregister_thread(); ++ return NULL; ++} ++ ++/* ++ * Pops the next element and hands it to the worker, unless one is with it ++ * or awaits its return. Under the BQL. ++ */ ++static void apple_m2scaler_next(AppleM2Scaler *s) ++{ ++ VirtIODevice *vdev = VIRTIO_DEVICE(s); ++ VirtQueue *vq = s->requests; ++ VirtQueueElement *elem; ++ AppleM2ScalerJob *job; ++ size_t length, room; ++ ++ if (s->queued || vdev->broken || ++ !(elem = virtqueue_pop(vq, sizeof(VirtQueueElement)))) { ++ return; ++ } ++ length = iov_size(elem->out_sg, elem->out_num); ++ room = iov_size(elem->in_sg, elem->in_num); ++ if (length > APPLE_M2SCALER_MAX_REQUEST) { ++ trace_apple_m2scaler_request(length, room, RVG_SCALER_NEEDS_RESET, 0, 0); ++ virtqueue_detach_element(vq, elem, 0); ++ g_free(elem); ++ virtio_error(vdev, "%s: Request too large", TYPE_APPLE_M2SCALER_PCI); ++ return; ++ } ++ job = g_new0(AppleM2ScalerJob, 1); ++ job->elem = elem; ++ job->length = length; ++ job->room = room; ++ job->request = g_malloc(MAX(length, 1)); ++ iov_to_buf(elem->out_sg, elem->out_num, 0, job->request, length); ++ s->queued = true; ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ s->job = job; ++ qemu_cond_signal(&s->wake); ++ } ++} ++ ++/* The worker's answer, returned to the guest; then the next element. */ ++static void apple_m2scaler_answered_bh(void *opaque) ++{ ++ AppleM2Scaler *s = opaque; ++ VirtIODevice *vdev = VIRTIO_DEVICE(s); ++ VirtQueue *vq = s->requests; ++ AppleM2ScalerJob *job; ++ ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ job = s->done; ++ s->done = NULL; ++ } ++ if (!job) { ++ return; ++ } ++ s->queued = false; ++ if (job->outcome == RVG_SCALER_REPLY) { ++ iov_from_buf(job->elem->in_sg, job->elem->in_num, 0, job->request, ++ job->length); ++ virtqueue_push(vq, job->elem, job->length); ++ virtio_notify(vdev, vq); ++ apple_m2scaler_job_free(job); ++ apple_m2scaler_next(s); ++ return; ++ } ++ virtqueue_detach_element(vq, job->elem, 0); ++ virtio_error(vdev, "%s: %s", TYPE_APPLE_M2SCALER_PCI, job->reason); ++ apple_m2scaler_job_free(job); ++} ++ ++/* ++ * Waits for the worker to answer the element it has, and discards the ++ * answer: for a reset, which empties the queue after this, and for the ++ * device's end. Under the BQL, which the worker never takes, so a reset ++ * during a transform waits that transform out. ++ */ ++static void apple_m2scaler_drain(AppleM2Scaler *s) ++{ ++ AppleM2ScalerJob *job; ++ ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ while (s->job) { ++ qemu_cond_wait(&s->answered, &s->lock); ++ } ++ job = s->done; ++ s->done = NULL; ++ } ++ qemu_bh_cancel(s->answered_bh); ++ trace_apple_m2scaler_drain(job != NULL); ++ if (job) { ++ virtqueue_detach_element(s->requests, job->elem, 0); ++ apple_m2scaler_job_free(job); ++ } ++ s->queued = false; ++} ++ ++/* ++ * The queue's notification: each element is one request, answered in turn ++ * on the worker. ++ */ ++static void apple_m2scaler_handle_requests(VirtIODevice *vdev, VirtQueue *vq) ++{ ++ apple_m2scaler_next(APPLE_M2SCALER(vdev)); ++} ++ ++static void apple_m2scaler_start_failed_bh(void *opaque) ++{ ++ virtio_error(VIRTIO_DEVICE(opaque), "%s: IOSurfaceAcceleratorCreate failed", ++ TYPE_APPLE_M2SCALER_PCI); ++} ++ ++/* No features of the device's own. */ ++static uint64_t apple_m2scaler_get_features(VirtIODevice *vdev, ++ uint64_t features, Error **errp) ++{ ++ return features; ++} ++ ++/* ++ * DRIVER_OK makes the accelerator. A failure is a needs-reset, set once the ++ * status is: virtio_set_status writes the status after this returns. ++ */ ++static int apple_m2scaler_set_status(VirtIODevice *vdev, uint8_t status) ++{ ++ AppleM2Scaler *s = APPLE_M2SCALER(vdev); ++ bool was = vdev->status & VIRTIO_CONFIG_S_DRIVER_OK; ++ bool now = status & VIRTIO_CONFIG_S_DRIVER_OK; ++ ++ trace_apple_m2scaler_set_status(status); ++ if (now && !was && !rvg_scaler_device_start(s->device)) { ++ qemu_bh_schedule(s->start_failed_bh); ++ } ++ return 0; ++} ++ ++/* The guest's reset, and the machine's: the accelerator goes. */ ++static void apple_m2scaler_reset(VirtIODevice *vdev) ++{ ++ AppleM2Scaler *s = APPLE_M2SCALER(vdev); ++ ++ trace_apple_m2scaler_reset(); ++ qemu_bh_cancel(s->start_failed_bh); ++ if (s->device) { ++ apple_m2scaler_drain(s); ++ rvg_scaler_device_reset(s->device); ++ } ++} ++ ++/* ++ * FEATURES_OK: refused where the library has no video processing for the ++ * device, so that the guest's driver gives the device up before DRIVER_OK ++ * (above). ++ */ ++static int apple_m2scaler_validate_features(VirtIODevice *vdev) ++{ ++ AppleM2Scaler *s = APPLE_M2SCALER(vdev); ++ ++ if (!rvg_scaler_device_scales(s->device)) { ++ trace_apple_m2scaler_features_refused(); ++ return -ENOTSUP; ++ } ++ return 0; ++} ++ ++static void apple_m2scaler_realize(DeviceState *dev, Error **errp) ++{ ++ VirtIODevice *vdev = VIRTIO_DEVICE(dev); ++ AppleM2Scaler *s = APPLE_M2SCALER(dev); ++ RvgScalerGuestMemory memory = { ++ .struct_size = sizeof(memory), ++ .ctx = s, ++ .is_ram = apple_m2scaler_is_ram, ++ .read = apple_m2scaler_read, ++ .write = apple_m2scaler_write, ++ }; ++ RvgScalerDeviceConfig config = { ++ .struct_size = sizeof(config), ++ .iosurface_device = NULL, /* the process's latest */ ++ .guest_memory = &memory, ++ }; ++ RvgStatus status; ++ ++ status = rvg_scaler_device_create(&config, &s->device); ++ if (status != RVG_OK) { ++ error_setg(errp, "%s: rvg_scaler_device_create: %s", ++ TYPE_APPLE_M2SCALER_PCI, rvg_status_message(status)); ++ return; ++ } ++ s->start_failed_bh = qemu_bh_new_guarded(apple_m2scaler_start_failed_bh, ++ s, &dev->mem_reentrancy_guard); ++ s->answered_bh = qemu_bh_new_guarded(apple_m2scaler_answered_bh, s, ++ &dev->mem_reentrancy_guard); ++ qemu_mutex_init(&s->lock); ++ qemu_cond_init(&s->wake); ++ qemu_cond_init(&s->answered); ++ qemu_thread_create(&s->worker, "apple-m2scaler", apple_m2scaler_worker, s, ++ QEMU_THREAD_JOINABLE); ++ ++ /* ++ * The virtio device ID is QEMU's alone: the guest reads Apple's PCI IDs, ++ * and nothing in QEMU keys behaviour on this one. ++ */ ++ virtio_init(vdev, VIRTIO_ID_VIDEO_ENCODER, 0); ++ s->requests = virtio_add_queue(vdev, APPLE_M2SCALER_QUEUE_SIZE, ++ apple_m2scaler_handle_requests); ++} ++ ++static void apple_m2scaler_unrealize(DeviceState *dev) ++{ ++ VirtIODevice *vdev = VIRTIO_DEVICE(dev); ++ AppleM2Scaler *s = APPLE_M2SCALER(dev); ++ ++ apple_m2scaler_drain(s); ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ s->ending = true; ++ qemu_cond_signal(&s->wake); ++ } ++ qemu_thread_join(&s->worker); ++ qemu_cond_destroy(&s->answered); ++ qemu_cond_destroy(&s->wake); ++ qemu_mutex_destroy(&s->lock); ++ qemu_bh_delete(s->answered_bh); ++ rvg_scaler_device_release(s->device); ++ s->device = NULL; ++ qemu_bh_delete(s->start_failed_bh); ++ virtio_delete_queue(s->requests); ++ virtio_cleanup(vdev); ++} ++ ++/* The accelerator lives in the library: reims-vgpu's QEMU migrates none. */ ++static const VMStateDescription vmstate_apple_m2scaler = { ++ .name = TYPE_APPLE_M2SCALER, ++ .unmigratable = 1, ++}; ++ ++static void apple_m2scaler_class_init(ObjectClass *klass, const void *data) ++{ ++ DeviceClass *dc = DEVICE_CLASS(klass); ++ VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); ++ ++ dc->vmsd = &vmstate_apple_m2scaler; ++ set_bit(DEVICE_CATEGORY_MISC, dc->categories); ++ vdc->realize = apple_m2scaler_realize; ++ vdc->unrealize = apple_m2scaler_unrealize; ++ vdc->get_features = apple_m2scaler_get_features; ++ vdc->set_status = apple_m2scaler_set_status; ++ vdc->validate_features = apple_m2scaler_validate_features; ++ vdc->reset = apple_m2scaler_reset; ++} ++ ++static const TypeInfo apple_m2scaler_info = { ++ .name = TYPE_APPLE_M2SCALER, ++ .parent = TYPE_VIRTIO_DEVICE, ++ .instance_size = sizeof(AppleM2Scaler), ++ .class_init = apple_m2scaler_class_init, ++}; ++ ++/* ------ The PCI device ------ */ ++ ++static const Property apple_m2scaler_pci_properties[] = { ++ DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, ++ VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), ++ DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, ++ DEV_NVECTORS_UNSPECIFIED), ++}; ++ ++static void apple_m2scaler_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) ++{ ++ AppleM2ScalerPCI *dev = APPLE_M2SCALER_PCI(vpci_dev); ++ DeviceState *vdev = DEVICE(&dev->vdev); ++ uint8_t *config = vpci_dev->pci_dev.config; ++ bool capabilities[PCI_CONFIG_SPACE_SIZE] = { false }; ++ unsigned offset, previous = PCI_CAPABILITY_LIST; ++ ++ /* One MSI-X vector for the queue, and one for the configuration. */ ++ if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { ++ vpci_dev->nvectors = 2; ++ } ++ virtio_pci_force_virtio_1(vpci_dev); ++ if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) { ++ return; ++ } ++ ++ /* ++ * virtio-pci sets the device ID of a modern virtio device from its ++ * virtio ID: Apple's IDs again. ++ */ ++ pci_config_set_vendor_id(config, RVG_SCALER_PCI_VENDOR_ID); ++ pci_config_set_device_id(config, RVG_SCALER_PCI_DEVICE_ID); ++ ++ /* ++ * The capabilities linked in ascending address order, as ++ * apple-videotoolbox-pci links them for macOS 27's virtio transport. ++ */ ++ for (offset = config[PCI_CAPABILITY_LIST]; offset; ++ offset = config[offset + PCI_CAP_LIST_NEXT]) { ++ capabilities[offset] = true; ++ } ++ for (offset = PCI_CONFIG_HEADER_SIZE; offset < PCI_CONFIG_SPACE_SIZE; ++ offset++) { ++ if (capabilities[offset]) { ++ config[previous] = offset; ++ previous = offset + PCI_CAP_LIST_NEXT; ++ } ++ } ++ config[previous] = 0; ++} ++ ++static void apple_m2scaler_pci_class_init(ObjectClass *klass, ++ const void *data) ++{ ++ DeviceClass *dc = DEVICE_CLASS(klass); ++ VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); ++ PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); ++ ++ dc->desc = "macOS Paravirtualized M2 Scaler"; ++ dc->hotpluggable = false; ++ set_bit(DEVICE_CATEGORY_MISC, dc->categories); ++ device_class_set_props(dc, apple_m2scaler_pci_properties); ++ k->realize = apple_m2scaler_pci_realize; ++ pcidev_k->vendor_id = RVG_SCALER_PCI_VENDOR_ID; ++ pcidev_k->device_id = RVG_SCALER_PCI_DEVICE_ID; ++ /* ++ * Apple's: the subsystem 106b:1a0b and the class of a processing ++ * accelerator, 0x12, subclass 0. ++ */ ++ pcidev_k->subsystem_vendor_id = RVG_SCALER_PCI_VENDOR_ID; ++ pcidev_k->subsystem_id = RVG_SCALER_PCI_DEVICE_ID; ++ pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; ++ pcidev_k->class_id = 0x1200; ++} ++ ++static void apple_m2scaler_pci_instance_init(Object *obj) ++{ ++ AppleM2ScalerPCI *dev = APPLE_M2SCALER_PCI(obj); ++ ++ virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), ++ TYPE_APPLE_M2SCALER); ++} ++ ++static const VirtioPCIDeviceTypeInfo apple_m2scaler_pci_info = { ++ .generic_name = TYPE_APPLE_M2SCALER_PCI, ++ .instance_size = sizeof(AppleM2ScalerPCI), ++ .instance_init = apple_m2scaler_pci_instance_init, ++ .class_init = apple_m2scaler_pci_class_init, ++}; ++ ++static void apple_m2scaler_register_types(void) ++{ ++ type_register_static(&apple_m2scaler_info); ++ virtio_pci_types_register(&apple_m2scaler_pci_info); ++} ++ ++type_init(apple_m2scaler_register_types) +diff --git a/hw/display/apple-videotoolbox-pci.c b/hw/display/apple-videotoolbox-pci.c +new file mode 100644 +index 0000000000..c11b48895c +--- /dev/null ++++ b/hw/display/apple-videotoolbox-pci.c +@@ -0,0 +1,648 @@ ++/* ++ * QEMU Apple paravirtualized VideoToolbox device, virtio-PCI 106b:1a03 ++ * ++ * SPDX-License-Identifier: GPL-2.0-or-later ++ * ++ * A macOS guest's AppleVideoToolboxParavirtualization kext hands its ++ * VideoToolbox's decoder, encoder and motion-estimation sessions to the host ++ * through this device, as Virtualization.framework gives it. Its host side is ++ * reims-vgpu's video device (rvg_video_device_*, reims_vgpu.h); this file is ++ * its transport, a modern (1.x) virtio device with Apple's IDs, no device ++ * features and no device configuration: ++ * ++ * - Queue 0, guest to host. Each element is one message: its readable part is ++ * the message, which the library's host session takes whole, and its ++ * writable part takes the session's int32 status, little-endian. A ++ * writable part shorter than 4 bytes is logged and gets no status. A ++ * readable part longer than RVG_VIDEO_MAX_MESSAGE_SIZE is answered ++ * RVG_VIDEO_ALLOCATION_FAILED here, as the library answers it, and never ++ * copied: a guest's descriptors may add up to many times the host's ++ * memory. It holds 1024 descriptors, where Apple's queues hold 256 ++ * (below). ++ * - The library answers each message on the device's worker thread, not the ++ * main loop, as Apple's host answers them on the device's own queue: a ++ * JPEG decode takes the GPU for as long as it takes, and a session's end ++ * may wait for its thread, and the VM's main loop runs meanwhile. One ++ * element is with the worker at a time; its status goes back on the main ++ * loop, and only then is the next popped, so the elements are answered ++ * one by one in the guest's order. ++ * - Queue 1, host to guest. The guest posts buffers of 0x4000 bytes. The ++ * library's messages, which arrive on its threads, wait in a ring here, and ++ * each fills the next buffer the guest posts, in the order they came. A ++ * notification on queue 1 only drains the ring. A message longer than the ++ * guest's buffer ends the process, as Apple's host ends the VM. The ring ++ * is not drained while an element is with the worker: a message reaches ++ * the guest after the status of the element being answered as it was ++ * sent, as on Apple's host, whose replies are queued behind the handler ++ * on the device's queue. ++ * ++ * The IOSurfaces a message names are resolved by the library, through the ++ * process's latest IOSurface device (apple-gfx-iosurface-pci on q35, ++ * apple-gfx-mmio on vmapple); without one, they resolve to none. ++ * ++ * DRIVER_OK starts the library's host session; a reset ends it, once the ++ * element with the worker is answered, which the reset then discards, and ++ * once the session's work has drained. The ring of host messages is kept ++ * across the reset, as Apple's host keeps its own. ++ * ++ * The device has no properties of its own: the library picks what it decodes ++ * and encodes with, and logs its choice and why. On macOS it is VideoToolbox. ++ * On Linux it runs on the GPU the library's GPU device (apple-gfx-pci) runs ++ * on, which the Vulkan loader's environment picks: NVIDIA's NVDEC and NVENC ++ * on an NVIDIA GPU, else VA-API on its render node, and a hardware GPU of the ++ * host's where that GPU is a software rasterizer or offers no video. Where no ++ * backend opens, the guest is offered no codec. ++ * ++ * Not for upstream: written with an AI assistant, which QEMU's policy on ++ * AI-generated content (docs/devel/code-provenance.rst) declines. ++ */ ++ ++#include "qemu/osdep.h" ++#include "qapi/error.h" ++#include "qemu/bswap.h" ++#include "qemu/error-report.h" ++#include "qemu/iov.h" ++#include "qemu/lockable.h" ++#include "qemu/main-loop.h" ++#include "qemu/module.h" ++#include "qemu/queue.h" ++#include "qemu/thread.h" ++#include "hw/pci/pci.h" ++#include "hw/core/qdev-properties.h" ++#include "hw/virtio/virtio.h" ++#include "hw/virtio/virtio-pci.h" ++#include "migration/vmstate.h" ++#include "standard-headers/linux/virtio_ids.h" ++#include "trace.h" ++#include ++ ++#define TYPE_APPLE_VIDEOTOOLBOX "apple-videotoolbox" ++OBJECT_DECLARE_SIMPLE_TYPE(AppleVideoToolbox, APPLE_VIDEOTOOLBOX) ++ ++#define TYPE_APPLE_VIDEOTOOLBOX_PCI "apple-videotoolbox-pci" ++OBJECT_DECLARE_SIMPLE_TYPE(AppleVideoToolboxPCI, APPLE_VIDEOTOOLBOX_PCI) ++ ++/* ++ * Each queue's size. Apple's device refuses a queue of more than 256 ++ * descriptors when the guest enables it, as its VirtioDevice base refuses ++ * every queue. The guest kext's queues take the size the device reports ++ * (AppleVirtIOQueue::initWithTransport), and its messages go out as direct ++ * descriptor chains, one descriptor per physical segment, a transaction of ++ * more segments than the queue holds refused with kIOReturnBadArgument ++ * (AppleVirtIOQueue::prepareTransaction, macOS 26.7.1's AppleVirtIO). ++ * VideoToolbox cuts a sample into messages of up to 0x200000 bytes, 128 ++ * pages of an arm64 guest's 16 KiB, but 512 of an x86_64 guest's 4 KiB, and ++ * 256 descriptors carry 1 MiB there. So a guest-to-host queue of 1024 is ++ * ours, for x86_64 guests; the host-to-guest queue, whose buffers are 0x4000 ++ * bytes, keeps Apple's 256. ++ */ ++#define APPLE_VIDEOTOOLBOX_GUEST_TO_HOST_QUEUE_SIZE 1024 ++#define APPLE_VIDEOTOOLBOX_HOST_TO_GUEST_QUEUE_SIZE 256 ++#define APPLE_VIDEOTOOLBOX_STATUS_SIZE 4 ++ ++/* One host-to-guest message waiting for a buffer of the guest's. */ ++typedef struct AppleVideoToolboxMessage { ++ QSIMPLEQ_ENTRY(AppleVideoToolboxMessage) next; ++ size_t length; ++ uint8_t bytes[]; ++} AppleVideoToolboxMessage; ++ ++/* ++ * One element with the worker: its readable part, copied, and the session's ++ * status for it. ++ */ ++typedef struct AppleVideoToolboxJob { ++ VirtQueueElement *elem; ++ uint8_t *message; ++ size_t length; ++ int32_t status; ++} AppleVideoToolboxJob; ++ ++struct AppleVideoToolbox { ++ VirtIODevice parent_obj; ++ ++ VirtQueue *guest_to_host; /* queue 0 */ ++ VirtQueue *host_to_guest; /* queue 1 */ ++ RvgVideoDevice *device; ++ ++ /* The library's messages, in the order they came; ring_lock protected. */ ++ QemuMutex ring_lock; ++ QSIMPLEQ_HEAD(, AppleVideoToolboxMessage) ring; ++ /* Drains the ring into queue 1 under the BQL. */ ++ QEMUBH *drain_bh; ++ ++ /* ++ * The worker, which calls rvg_video_device_deliver. Under lock: job, the ++ * element it has or is to take (NULL once it answered), done, its answer ++ * until answered_bh takes it, and ending. queued is the main loop's alone: ++ * an element is with the worker or answered and not yet returned, so no ++ * other is popped and the ring is not drained. ++ */ ++ QemuThread worker; ++ QemuMutex lock; ++ QemuCond wake; ++ QemuCond answered; ++ AppleVideoToolboxJob *job; ++ AppleVideoToolboxJob *done; ++ bool ending; ++ bool queued; ++ /* Returns the worker's answer to the guest, on the main loop. */ ++ QEMUBH *answered_bh; ++}; ++ ++struct AppleVideoToolboxPCI { ++ VirtIOPCIProxy parent_obj; ++ ++ AppleVideoToolbox vdev; ++}; ++ ++/* ++ * The library's send_to_guest: on one of its threads, or on the worker inside ++ * rvg_video_device_deliver. It copies the message into the ring and has the ++ * main loop drain it, so it waits for nothing the BQL holds. ++ */ ++static void apple_videotoolbox_send_to_guest(void *ctx, const uint8_t *message, ++ size_t length) ++{ ++ AppleVideoToolbox *s = ctx; ++ AppleVideoToolboxMessage *m = g_malloc(sizeof(*m) + length); ++ ++ m->length = length; ++ memcpy(m->bytes, message, length); ++ WITH_QEMU_LOCK_GUARD(&s->ring_lock) { ++ QSIMPLEQ_INSERT_TAIL(&s->ring, m, next); ++ } ++ trace_apple_videotoolbox_send_to_guest(length); ++ qemu_bh_schedule(s->drain_bh); ++} ++ ++/* ++ * Moves the ring's messages into the guest's buffers on queue 1, while it ++ * has both, and no element is with the worker: answered_bh drains once it ++ * has returned that element. Under the BQL. ++ */ ++static void apple_videotoolbox_drain(AppleVideoToolbox *s) ++{ ++ VirtIODevice *vdev = VIRTIO_DEVICE(s); ++ ++ if (s->queued) { ++ return; ++ } ++ for (;;) { ++ AppleVideoToolboxMessage *m; ++ VirtQueueElement *elem; ++ size_t room; ++ ++ WITH_QEMU_LOCK_GUARD(&s->ring_lock) { ++ m = QSIMPLEQ_FIRST(&s->ring); ++ } ++ if (!m) { ++ return; ++ } ++ elem = virtqueue_pop(s->host_to_guest, sizeof(VirtQueueElement)); ++ if (!elem) { ++ return; ++ } ++ room = iov_size(elem->in_sg, elem->in_num); ++ if (room < m->length) { ++ error_report("%s: a host message of %zu bytes, and the guest's " ++ "buffer holds %zu", TYPE_APPLE_VIDEOTOOLBOX_PCI, ++ m->length, room); ++ abort(); ++ } ++ iov_from_buf(elem->in_sg, elem->in_num, 0, m->bytes, m->length); ++ trace_apple_videotoolbox_to_guest(m->length, room); ++ virtqueue_push(s->host_to_guest, elem, m->length); ++ virtio_notify(vdev, s->host_to_guest); ++ g_free(elem); ++ WITH_QEMU_LOCK_GUARD(&s->ring_lock) { ++ QSIMPLEQ_REMOVE_HEAD(&s->ring, next); ++ } ++ g_free(m); ++ } ++} ++ ++static void apple_videotoolbox_drain_bh(void *opaque) ++{ ++ apple_videotoolbox_drain(opaque); ++} ++ ++/* Queue 1's notification: the guest posted buffers. */ ++static void apple_videotoolbox_handle_host_to_guest(VirtIODevice *vdev, ++ VirtQueue *vq) ++{ ++ apple_videotoolbox_drain(APPLE_VIDEOTOOLBOX(vdev)); ++} ++ ++static void apple_videotoolbox_job_free(AppleVideoToolboxJob *job) ++{ ++ g_free(job->message); ++ g_free(job->elem); ++ g_free(job); ++} ++ ++/* ++ * The worker: takes each job, has the library's host session answer it, and ++ * hands the status to the main loop. It never takes the BQL: the library's ++ * calls back, send_to_guest and the IOSurface device's, wait for nothing that ++ * holds it. ++ */ ++static void *apple_videotoolbox_worker(void *opaque) ++{ ++ AppleVideoToolbox *s = opaque; ++ ++ for (;;) { ++ AppleVideoToolboxJob *job; ++ ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ while (!s->job && !s->ending) { ++ qemu_cond_wait(&s->wake, &s->lock); ++ } ++ job = s->job; ++ } ++ if (!job) { ++ break; ++ } ++ trace_apple_videotoolbox_work(job->length); ++ job->status = rvg_video_device_deliver(s->device, job->message, ++ job->length); ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ s->job = NULL; ++ s->done = job; ++ qemu_cond_broadcast(&s->answered); ++ } ++ qemu_bh_schedule(s->answered_bh); ++ } ++ return NULL; ++} ++ ++/* ++ * Writes `status` into `elem`, a message of `length` bytes, and returns it to ++ * the guest. Under the BQL. ++ */ ++static void apple_videotoolbox_answer(AppleVideoToolbox *s, ++ VirtQueueElement *elem, size_t length, ++ int32_t status) ++{ ++ size_t room = iov_size(elem->in_sg, elem->in_num); ++ unsigned written = 0; ++ ++ trace_apple_videotoolbox_from_guest(length, room, status); ++ if (room < APPLE_VIDEOTOOLBOX_STATUS_SIZE) { ++ warn_report_once("%s: a guest message with no room for its status " ++ "(%zu bytes)", TYPE_APPLE_VIDEOTOOLBOX_PCI, room); ++ } else { ++ uint32_t le = cpu_to_le32((uint32_t)status); ++ ++ iov_from_buf(elem->in_sg, elem->in_num, 0, &le, sizeof(le)); ++ written = sizeof(le); ++ } ++ virtqueue_push(s->guest_to_host, elem, written); ++ virtio_notify(VIRTIO_DEVICE(s), s->guest_to_host); ++} ++ ++/* ++ * Pops the next element and hands it to the worker, unless one is with it ++ * or awaits its return; one too long for the library is answered here, unread, ++ * and the next popped. Under the BQL. ++ */ ++static void apple_videotoolbox_next(AppleVideoToolbox *s) ++{ ++ VirtIODevice *vdev = VIRTIO_DEVICE(s); ++ VirtQueueElement *elem; ++ AppleVideoToolboxJob *job; ++ size_t length; ++ ++ for (;;) { ++ if (s->queued || vdev->broken || ++ !(elem = virtqueue_pop(s->guest_to_host, ++ sizeof(VirtQueueElement)))) { ++ return; ++ } ++ length = iov_size(elem->out_sg, elem->out_num); ++ if (length <= RVG_VIDEO_MAX_MESSAGE_SIZE) { ++ break; ++ } ++ warn_report_once("%s: a guest message of %zu bytes, over the %u the " ++ "library takes: answered %d, unread", ++ TYPE_APPLE_VIDEOTOOLBOX_PCI, length, ++ (unsigned)RVG_VIDEO_MAX_MESSAGE_SIZE, ++ RVG_VIDEO_ALLOCATION_FAILED); ++ apple_videotoolbox_answer(s, elem, length, ++ RVG_VIDEO_ALLOCATION_FAILED); ++ g_free(elem); ++ } ++ job = g_new0(AppleVideoToolboxJob, 1); ++ job->elem = elem; ++ job->length = length; ++ job->message = g_malloc(MAX(job->length, 1)); ++ iov_to_buf(elem->out_sg, elem->out_num, 0, job->message, job->length); ++ s->queued = true; ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ s->job = job; ++ qemu_cond_signal(&s->wake); ++ } ++} ++ ++/* ++ * The worker's status, written into the element, which goes back to the ++ * guest; then what the session sent while answering it, and the next element. ++ */ ++static void apple_videotoolbox_answered_bh(void *opaque) ++{ ++ AppleVideoToolbox *s = opaque; ++ AppleVideoToolboxJob *job; ++ ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ job = s->done; ++ s->done = NULL; ++ } ++ if (!job) { ++ return; ++ } ++ apple_videotoolbox_answer(s, job->elem, job->length, job->status); ++ apple_videotoolbox_job_free(job); ++ s->queued = false; ++ apple_videotoolbox_drain(s); ++ apple_videotoolbox_next(s); ++} ++ ++/* ++ * Waits for the worker to answer the element it has, and discards the ++ * answer: for a reset, which empties the queue after this, and for the ++ * device's end. Under the BQL, which the worker never takes, so it cannot ++ * deadlock; a reset during a long JPEG decode waits for it, 10 s at most, the ++ * library's bound on its backend's call. ++ */ ++static void apple_videotoolbox_drain_worker(AppleVideoToolbox *s) ++{ ++ AppleVideoToolboxJob *job; ++ ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ while (s->job) { ++ qemu_cond_wait(&s->answered, &s->lock); ++ } ++ job = s->done; ++ s->done = NULL; ++ } ++ qemu_bh_cancel(s->answered_bh); ++ trace_apple_videotoolbox_drain_worker(job != NULL); ++ if (job) { ++ virtqueue_detach_element(s->guest_to_host, job->elem, 0); ++ apple_videotoolbox_job_free(job); ++ } ++ s->queued = false; ++} ++ ++/* ++ * Queue 0's notification: each element is one message for the session, ++ * answered in turn on the worker. ++ */ ++static void apple_videotoolbox_handle_guest_to_host(VirtIODevice *vdev, ++ VirtQueue *vq) ++{ ++ apple_videotoolbox_next(APPLE_VIDEOTOOLBOX(vdev)); ++} ++ ++/* No features of the device's own. */ ++static uint64_t apple_videotoolbox_get_features(VirtIODevice *vdev, ++ uint64_t features, ++ Error **errp) ++{ ++ return features; ++} ++ ++/* DRIVER_OK starts the host session. */ ++static int apple_videotoolbox_set_status(VirtIODevice *vdev, uint8_t status) ++{ ++ AppleVideoToolbox *s = APPLE_VIDEOTOOLBOX(vdev); ++ bool was = vdev->status & VIRTIO_CONFIG_S_DRIVER_OK; ++ bool now = status & VIRTIO_CONFIG_S_DRIVER_OK; ++ ++ trace_apple_videotoolbox_set_status(status); ++ if (now && !was) { ++ rvg_video_device_start(s->device); ++ } ++ return 0; ++} ++ ++/* The guest's reset, and the machine's: the host session ends. */ ++static void apple_videotoolbox_reset(VirtIODevice *vdev) ++{ ++ AppleVideoToolbox *s = APPLE_VIDEOTOOLBOX(vdev); ++ ++ trace_apple_videotoolbox_reset(); ++ if (s->device) { ++ apple_videotoolbox_drain_worker(s); ++ rvg_video_device_reset(s->device); ++ } ++} ++ ++static void apple_videotoolbox_realize(DeviceState *dev, Error **errp) ++{ ++ VirtIODevice *vdev = VIRTIO_DEVICE(dev); ++ AppleVideoToolbox *s = APPLE_VIDEOTOOLBOX(dev); ++ RvgVideoCallbacks callbacks = { ++ .struct_size = sizeof(callbacks), ++ .ctx = s, ++ .send_to_guest = apple_videotoolbox_send_to_guest, ++ }; ++ RvgVideoDeviceConfig config = { ++ .struct_size = sizeof(config), ++ .iosurface_device = NULL, /* the process's latest */ ++ .callbacks = &callbacks, ++ }; ++ RvgStatus status; ++ ++ qemu_mutex_init(&s->ring_lock); ++ QSIMPLEQ_INIT(&s->ring); ++ s->drain_bh = qemu_bh_new_guarded(apple_videotoolbox_drain_bh, s, ++ &dev->mem_reentrancy_guard); ++ ++ status = rvg_video_device_create(&config, &s->device); ++ if (status != RVG_OK) { ++ error_setg(errp, "%s: rvg_video_device_create: %s", ++ TYPE_APPLE_VIDEOTOOLBOX_PCI, rvg_status_message(status)); ++ qemu_bh_delete(s->drain_bh); ++ qemu_mutex_destroy(&s->ring_lock); ++ return; ++ } ++ s->answered_bh = qemu_bh_new_guarded(apple_videotoolbox_answered_bh, s, ++ &dev->mem_reentrancy_guard); ++ qemu_mutex_init(&s->lock); ++ qemu_cond_init(&s->wake); ++ qemu_cond_init(&s->answered); ++ qemu_thread_create(&s->worker, "apple-videotoolbox", ++ apple_videotoolbox_worker, s, QEMU_THREAD_JOINABLE); ++ ++ /* ++ * The virtio device ID is QEMU's alone: the guest reads Apple's PCI IDs, ++ * and nothing in QEMU keys behaviour on this one. ++ */ ++ virtio_init(vdev, VIRTIO_ID_VIDEO_DECODER, 0); ++ s->guest_to_host = virtio_add_queue(vdev, ++ APPLE_VIDEOTOOLBOX_GUEST_TO_HOST_QUEUE_SIZE, ++ apple_videotoolbox_handle_guest_to_host); ++ s->host_to_guest = virtio_add_queue(vdev, ++ APPLE_VIDEOTOOLBOX_HOST_TO_GUEST_QUEUE_SIZE, ++ apple_videotoolbox_handle_host_to_guest); ++} ++ ++static void apple_videotoolbox_unrealize(DeviceState *dev) ++{ ++ VirtIODevice *vdev = VIRTIO_DEVICE(dev); ++ AppleVideoToolbox *s = APPLE_VIDEOTOOLBOX(dev); ++ AppleVideoToolboxMessage *m, *next; ++ ++ /* The worker first: it may be inside the library, sending. */ ++ apple_videotoolbox_drain_worker(s); ++ WITH_QEMU_LOCK_GUARD(&s->lock) { ++ s->ending = true; ++ qemu_cond_signal(&s->wake); ++ } ++ qemu_thread_join(&s->worker); ++ qemu_cond_destroy(&s->answered); ++ qemu_cond_destroy(&s->wake); ++ qemu_mutex_destroy(&s->lock); ++ qemu_bh_delete(s->answered_bh); ++ /* Its last release resets it: no send_to_guest follows. */ ++ rvg_video_device_release(s->device); ++ s->device = NULL; ++ qemu_bh_delete(s->drain_bh); ++ QSIMPLEQ_FOREACH_SAFE(m, &s->ring, next, next) { ++ g_free(m); ++ } ++ qemu_mutex_destroy(&s->ring_lock); ++ virtio_delete_queue(s->guest_to_host); ++ virtio_delete_queue(s->host_to_guest); ++ virtio_cleanup(vdev); ++} ++ ++/* The host session lives in the library: reims-vgpu's QEMU migrates none. */ ++static const VMStateDescription vmstate_apple_videotoolbox = { ++ .name = TYPE_APPLE_VIDEOTOOLBOX, ++ .unmigratable = 1, ++}; ++ ++static void apple_videotoolbox_class_init(ObjectClass *klass, const void *data) ++{ ++ DeviceClass *dc = DEVICE_CLASS(klass); ++ VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); ++ ++ dc->vmsd = &vmstate_apple_videotoolbox; ++ set_bit(DEVICE_CATEGORY_MISC, dc->categories); ++ vdc->realize = apple_videotoolbox_realize; ++ vdc->unrealize = apple_videotoolbox_unrealize; ++ vdc->get_features = apple_videotoolbox_get_features; ++ vdc->set_status = apple_videotoolbox_set_status; ++ vdc->reset = apple_videotoolbox_reset; ++} ++ ++static const TypeInfo apple_videotoolbox_info = { ++ .name = TYPE_APPLE_VIDEOTOOLBOX, ++ .parent = TYPE_VIRTIO_DEVICE, ++ .instance_size = sizeof(AppleVideoToolbox), ++ .class_init = apple_videotoolbox_class_init, ++}; ++ ++/* ------ The PCI device ------ */ ++ ++static const Property apple_videotoolbox_pci_properties[] = { ++ DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, ++ VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), ++ DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, ++ DEV_NVECTORS_UNSPECIFIED), ++}; ++ ++static void apple_videotoolbox_pci_realize(VirtIOPCIProxy *vpci_dev, ++ Error **errp) ++{ ++ AppleVideoToolboxPCI *dev = APPLE_VIDEOTOOLBOX_PCI(vpci_dev); ++ DeviceState *vdev = DEVICE(&dev->vdev); ++ uint8_t *config = vpci_dev->pci_dev.config; ++ bool capabilities[PCI_CONFIG_SPACE_SIZE] = { false }; ++ unsigned offset, previous = PCI_CAPABILITY_LIST; ++ ++ /* One MSI-X vector per queue, and one for the configuration. */ ++ if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { ++ vpci_dev->nvectors = 3; ++ } ++ virtio_pci_force_virtio_1(vpci_dev); ++ if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) { ++ return; ++ } ++ ++ /* ++ * virtio-pci sets the device ID of a modern virtio device from its ++ * virtio ID: Apple's IDs again. ++ */ ++ pci_config_set_vendor_id(config, RVG_VIDEO_PCI_VENDOR_ID); ++ pci_config_set_device_id(config, RVG_VIDEO_PCI_DEVICE_ID); ++ ++ /* ++ * The capabilities linked in ascending address order, each at its offset ++ * with its contents: macOS 27's virtio transport never starts a device ++ * whose list runs from high offsets to low, as QEMU links it, and macOS ++ * 26's walks either (vmapple-virtio-blk-pci does the same). ++ */ ++ for (offset = config[PCI_CAPABILITY_LIST]; offset; ++ offset = config[offset + PCI_CAP_LIST_NEXT]) { ++ capabilities[offset] = true; ++ } ++ for (offset = PCI_CONFIG_HEADER_SIZE; offset < PCI_CONFIG_SPACE_SIZE; ++ offset++) { ++ if (capabilities[offset]) { ++ config[previous] = offset; ++ previous = offset + PCI_CAP_LIST_NEXT; ++ } ++ } ++ config[previous] = 0; ++} ++ ++static void apple_videotoolbox_pci_class_init(ObjectClass *klass, ++ const void *data) ++{ ++ DeviceClass *dc = DEVICE_CLASS(klass); ++ VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); ++ PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); ++ ++ dc->desc = "macOS Paravirtualized VideoToolbox"; ++ dc->hotpluggable = false; ++ set_bit(DEVICE_CATEGORY_MISC, dc->categories); ++ device_class_set_props(dc, apple_videotoolbox_pci_properties); ++ k->realize = apple_videotoolbox_pci_realize; ++ pcidev_k->vendor_id = RVG_VIDEO_PCI_VENDOR_ID; ++ pcidev_k->device_id = RVG_VIDEO_PCI_DEVICE_ID; ++ /* ++ * Ours, as Apple's are not read: the subsystem as the scaler device's, ++ * 106b and its device ID, and the class of a processing accelerator ++ * (0x12), as the scaler device's. ++ */ ++ pcidev_k->subsystem_vendor_id = RVG_VIDEO_PCI_VENDOR_ID; ++ pcidev_k->subsystem_id = RVG_VIDEO_PCI_DEVICE_ID; ++ pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; ++ pcidev_k->class_id = 0x1200; ++} ++ ++static void apple_videotoolbox_pci_instance_init(Object *obj) ++{ ++ AppleVideoToolboxPCI *dev = APPLE_VIDEOTOOLBOX_PCI(obj); ++ ++ virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), ++ TYPE_APPLE_VIDEOTOOLBOX); ++} ++ ++static const VirtioPCIDeviceTypeInfo apple_videotoolbox_pci_info = { ++ .generic_name = TYPE_APPLE_VIDEOTOOLBOX_PCI, ++ .instance_size = sizeof(AppleVideoToolboxPCI), ++ .instance_init = apple_videotoolbox_pci_instance_init, ++ .class_init = apple_videotoolbox_pci_class_init, ++}; ++ ++static void apple_videotoolbox_register_types(void) ++{ ++ type_register_static(&apple_videotoolbox_info); ++ virtio_pci_types_register(&apple_videotoolbox_pci_info); ++} ++ ++type_init(apple_videotoolbox_register_types) +diff --git a/hw/display/meson.build b/hw/display/meson.build +index c738d77d2f..6a21104f90 100644 +--- a/hw/display/meson.build ++++ b/hw/display/meson.build +@@ -74,6 +74,14 @@ endif + system_ss.add(when: [pvg, 'CONFIG_MAC_PVG_PCI'], + if_true: [files('apple-gfx.c', 'apple-gfx-pci.c'), + apple_gfx_activity]) ++# The VideoToolbox device's transport; reims-vgpu's video device is its host ++# side. ++system_ss.add(when: [pvg, 'CONFIG_MAC_PVG_VIDEO_PCI'], ++ if_true: files('apple-videotoolbox-pci.c')) ++# The M2 scaler device's transport; reims-vgpu's scaler device is its host ++# side. ++system_ss.add(when: [pvg, 'CONFIG_MAC_PVG_SCALER_PCI'], ++ if_true: files('apple-m2scaler-pci.c')) + system_ss.add(when: [pvg, 'CONFIG_MAC_PVG_MMIO'], + if_true: [files('apple-gfx.c', 'apple-gfx-mmio.c'), + apple_gfx_activity]) +diff --git a/hw/display/trace-events b/hw/display/trace-events +index c2d8c037c6..2d7f70938f 100644 +--- a/hw/display/trace-events ++++ b/hw/display/trace-events +@@ -221,3 +221,19 @@ apple_gfx_iosfc_raise_irq(uint32_t vector) "vector=0x%x" + apple_gfx_pci_iosfc_read(uint64_t offset, uint64_t res, unsigned size) "offset=0x%"PRIx64" res=0x%"PRIx64" size=%u" + apple_gfx_pci_iosfc_write(uint64_t offset, uint64_t val, unsigned size) "offset=0x%"PRIx64" val=0x%"PRIx64" size=%u" + ++# apple-videotoolbox-pci.c ++apple_videotoolbox_from_guest(size_t length, size_t room, int32_t status) "length=%zu room=%zu status=%d" ++apple_videotoolbox_send_to_guest(size_t length) "length=%zu" ++apple_videotoolbox_to_guest(size_t length, size_t room) "length=%zu room=%zu" ++apple_videotoolbox_set_status(uint8_t status) "status=0x%x" ++apple_videotoolbox_reset(void) "" ++apple_videotoolbox_work(size_t length) "length=%zu" ++apple_videotoolbox_drain_worker(bool discarded) "discarded=%d" ++ ++# apple-m2scaler-pci.c ++apple_m2scaler_request(size_t length, size_t room, uint32_t outcome, uint32_t type, uint32_t status) "length=%zu room=%zu outcome=%u type=%u status=0x%x" ++apple_m2scaler_set_status(uint8_t status) "status=0x%x" ++apple_m2scaler_reset(void) "" ++apple_m2scaler_features_refused(void) "" ++apple_m2scaler_work(size_t length) "length=%zu" ++apple_m2scaler_drain(bool discarded) "discarded=%d" +-- +2.53.0 + From 02a59db0987cfa8097298e0b34d0e633b4bb90c7 Mon Sep 17 00:00:00 2001 From: TUNA_BIN <65464657+TUNA-NOPE@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:45:16 +0000 Subject: [PATCH 2/5] MACBOX_VIDEO: Reims's hardware VideoToolbox, and macbox video-kexts MACBOX_VIDEO=on (default off) attaches apple-videotoolbox-pci and apple-m2scaler-pci at the slots reims-ctl's newer releases use. An x86-64 guest needs reims-util's ports of Apple's drivers and its VtWaitPatch in OpenCore: reims-util 8a8a852 and later put them in every image; for an image made earlier, macbox video-kexts adds the same three entries (src/guest/bin/opencore-video-kexts.sh), keeping config.plist.before-video. On Tahoe (2026-11-13) the Device panel's 1206x2622 stream went from VideoToolbox's software encoder (613-620 MB, 73-75% of a core scrolling, 82 ms a frame) to the host GPU's (9-18 MB, ~0%, 8-9 ms). --- docs/architecture.md | 5 ++- docs/performance.md | 24 ++++++++++++ docs/setup.md | 1 + macbox | 26 ++++++++++++- src/guest/bin/opencore-video-kexts.sh | 54 +++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100755 src/guest/bin/opencore-video-kexts.sh diff --git a/docs/architecture.md b/docs/architecture.md index 3e665d0..6c67114 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -56,7 +56,7 @@ and the QEMU command line; `reims-ctl command mac` (inside the container) prints container's port 22 to the guest, and Docker publishes that as `127.0.0.1:$MACBOX_SSH_PORT`. reims-ctl's own forward would listen on the container's loopback, which Docker can't publish. - `-set drive.disk.discard=unmap` (guest TRIM frees space) and `cache=$MACBOX_DISK_CACHE`. -- `--video-acceleration=false` (no VideoToolbox/M2 scaler devices), a borderless window, +- `--video-acceleration=false`, a borderless window, `REIMS_VGPU_STATS=1` (a line of frame statistics a second in `logs/qemu.log`). - Before each start: `WAYLAND_DISPLAY` (sway's, or the host's), and the GPU tuning: `M2V_LIVE=$MACBOX_HOME/run/live.conf` with `host_rt_in_place=1`, @@ -64,6 +64,9 @@ and the QEMU command line; `reims-ctl command mac` (inside the container) prints Coherent (`MACBOX_COHERENT`, on by default with an Arrow Lake GPU) the census, Coherent and the switches measured with it, in `live.conf` and Reims's environment ([performance.md](performance.md)). The disk's options in `extra_args` are written again, so a changed `MACBOX_DISK_CACHE` applies. +- With `MACBOX_VIDEO=on`, Reims's VideoToolbox (`apple-videotoolbox-pci`, slot 8) and M2 scaler + (`apple-m2scaler-pci`, slot 9) devices in `extra_args`, where reims-ctl's newer releases put them. + The guest drives them with reims-util's x86-64 ports of Apple's drivers (`macbox video-kexts`). The system disk starts on SATA, because macOS's installer may not find a disk on NVMe, and moves to NVMe with `macbox disk nvme` once macOS is installed. diff --git a/docs/performance.md b/docs/performance.md index 4f463ab..6935bf3 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -190,6 +190,30 @@ What to know before relying on it: boot with it (2026-11-14), streamed iPhone screens refused 11 arms, all counted as reads, and the write-loss gates stayed clean over 2,060 surface reports. +## Hardware video encoding (`MACBOX_VIDEO`) + +Without Reims's video devices, macOS in the VM has only VideoToolbox's software H.264 encoder. +Streaming an iPhone 17 Pro simulator at its full 1206x2622 (T3 Code's Device panel) keeps +`VTEncoderXPCService` at about 610 MB and, while the screen scrolls, 75-100% of a core. With +`MACBOX_VIDEO=on` and the video kexts, VideoToolbox picks Reims's hardware encoder, which encodes on +the host's GPU through VA-API (Intel iHD on Arrow Lake: H.264 and HEVC; no AV1 encoder is exposed, +and macOS's VideoToolbox has none either). Measured on Tahoe on 2026-11-13, one boot, the same +simulator, A/B in two rounds: + +| | Software encoder | Hardware encoder | +|---|---|---| +| `VTEncoderXPCService` memory | 613-620 MB | 9-18 MB | +| its CPU while scrolling | 73-75% of a core | ~0% | +| encode per frame | 82 ms (p50, scrolling) | 8-9 ms, of which ~7 on the host's GPU | +| host GPU video engine | | 4-10% | + +Two things decide whether the hardware path is cheap. VideoToolbox converts anything but 4:2:0 +NV12 (`420v`) with 64-byte row and plane alignment on the guest's GPU first, a Metal pass that +waits behind the simulators' own rendering (57 ms against 8 ms), so the frames given to it should +already be NV12 laid out that way. And the bitrate it reaches is close to the target; quality at +the same bitrate, measured on a recorded scroll at full size (SSIM): H.264 0.982 at 4 Mbit/s and +0.998 at 12 Mbit/s; HEVC 0.998 and 0.9996. + ## Tried, no gain | | Result | diff --git a/docs/setup.md b/docs/setup.md index e19afa7..794cd48 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -39,6 +39,7 @@ node), makes an SSH key `~/.ssh/macbox_ed25519`, and adds a `Host macbox` block | `MACBOX_BUILD_CPUSET` | empty | cores for `macbox build`, e.g. the efficiency cores | | `MACBOX_DISK_CACHE` | none | `writeback` panicked macOS's NVMe driver under heavy swap; `unsafe` is faster, but a crash of the host itself can corrupt the VM's disk. Applies at the next `macbox up` | | `MACBOX_COHERENT` | auto | `auto`: on with an Arrow Lake iGPU. Coherent, zero-copy surfaces, and the switches measured with it: a 3x simulator 50 → 120 fps there ([performance.md](performance.md#coherent-and-the-switches-measured-with-it-on-by-default-on-arrow-lake)); `on` with any GPU, `off` | +| `MACBOX_VIDEO` | off | `on`: Reims's hardware VideoToolbox and M2 scaler devices, so the guest's H.264/HEVC encoding (the Device panel's stream) and decoding run on the host GPU through VA-API instead of VideoToolbox's software encoder. Needs the video kexts in OpenCore first (`macbox video-kexts`); macOS 26.7 (25G241) only. See [performance.md](performance.md#hardware-video-encoding-macbox_video) | | `MACBOX_HOME` | ~/.local/share/macbox | everything macbox keeps | Then: diff --git a/macbox b/macbox index 48957fa..eee2fa4 100755 --- a/macbox +++ b/macbox @@ -15,7 +15,7 @@ eval "$_env" : "${MACBOX_GUEST_USER:=dev}" "${MACBOX_CPUS:=8}" "${MACBOX_MEMORY:=12G}" "${MACBOX_DISK_SIZE:=160G}" : "${MACBOX_RESOLUTION:=1920x1080}" "${MACBOX_REFRESH:=60}" "${MACBOX_CPUSET:=}" : "${MACBOX_RENDER_NODE:=/dev/dri/renderD128}" "${MACBOX_DISK_CACHE:=none}" "${MACBOX_GPU_TUNING:=auto}" -: "${MACBOX_COHERENT:=auto}" +: "${MACBOX_COHERENT:=auto}" "${MACBOX_VIDEO:=off}" : "${MACBOX_SIM_PREFIX:=macbox}" "${MACBOX_POOL:=full slim-1 slim-2 sharp small}" : "${MACBOX_T3_HOST_ID:=$MACBOX_SSH_HOST}" "${MACBOX_T3_SETTINGS:=$HOME/.t3/userdata/settings.json}" DOCKER="${MACBOX_DOCKER:-docker}" @@ -165,6 +165,9 @@ qemu_extra() { # the network (passt, SSH forwarded by Docker) and the disk's op -set "drive.disk.cache=$MACBOX_DISK_CACHE" -set drive.disk.discard=unmap; do out+=("$(printf '%q' "$a")") done + # Reims's VideoToolbox and M2 scaler devices, in the slots reims-ctl gives them (after the mapper) + [ "$MACBOX_VIDEO" = on ] && out+=(-device apple-videotoolbox-pci,id=video,bus=pcie.0,addr=0x8 \ + -device apple-m2scaler-pci,id=scaler,bus=pcie.0,addr=0x9) echo "${out[*]}" } @@ -209,6 +212,9 @@ MACBOX_GPU_TUNING=$MACBOX_GPU_TUNING # auto (on with an Arrow Lake iGPU, where it runs day to day), on, off: Coherent, zero-copy # IOSurfaces, and the switches measured with it (docs/performance.md) MACBOX_COHERENT=$MACBOX_COHERENT +# on: Reims's hardware VideoToolbox (H.264/HEVC encode and decode on the host's GPU through VA-API) +# and M2 scaler devices; needs the video kexts in OpenCore (macbox video-kexts). off: as before +MACBOX_VIDEO=$MACBOX_VIDEO # iOS Simulator pool: names are " (