diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp index dc2ea2691..3add10cd8 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp @@ -124,6 +124,12 @@ struct StoreWrite { std::span Bytes() const { return ownedBytes.empty() ? viewBytes : std::span(ownedBytes); } }; std::optional ResolveStore(std::span packet, const QueueState& queue, std::size_t limit); +struct MemoryCopy { + std::uint64_t source; + std::uint64_t destination; + std::size_t bytes; +}; +std::optional DecodeMemoryCopy(std::span packet); // A DISPATCH_INDIRECT's arguments: the guest address of its three group-count dwords (no memory // access, so the GPU can read them in place: VulkanDevice::DispatchIndirect), the DISPATCH_DIRECT // packet made by reading them there (through the checked guest memory path, which waits for diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp index e5301b851..1b7c0b9c0 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp @@ -7,6 +7,7 @@ #include "prx/libSceAgcDriver/Eq/include/Event.hpp" #include #include +#include namespace AgcDriver::DriverDetail { @@ -130,6 +131,21 @@ bool Driver::preparePacketMemory(const Submission& submission, QueueState& queue Graphics::Recorder::CloseLabelGroup(GuestMemory::TrackerGeneration()); } } + if (drained && opcode == 0x50) { + const auto copy = Pm4::DecodeMemoryCopy(packet); + if (copy.has_value() && copy->bytes > gpuStoreLimit && GuestMemory::Accessible(reinterpret_cast(copy->source), copy->bytes) && GuestMemory::Accessible(reinterpret_cast(copy->destination), copy->bytes, true)) { + GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Copy); + std::lock_guard gpuLock(GuestMemory::GpuMutex()); + if (const auto localDevice = device.Load()) { + recordDeferredLabels(localDevice.get(), submission.queue); + const auto outcome = localDevice->CopyBuffer(copy->destination, copy->source, copy->bytes, 0, std::numeric_limits::max(), 0, 0, submission.queue, [](std::span, std::uint64_t) {}); + if (outcome.path == 1 || outcome.path == 3) { + wroteOnGpu = true; + drained = false; + } + } + } + } if (drained) ++storesDrained; } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp index 26207b5c0..a4336edc5 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp @@ -519,6 +519,16 @@ std::optional ResolveStore(std::span packet, co } } +std::optional DecodeMemoryCopy(std::span packet) { + if (packet.size() != 7 || ((packet[0] >> 8u) & 0xffu) != 0x50) return std::nullopt; + if (!memorySelector(dmaSource(packet)) || !memorySelector(dmaDestination(packet))) return std::nullopt; + const std::size_t bytes = packet[6] & 0x3ffffffu; + const auto source = address(packet[2], packet[3]); + const auto destination = address(packet[4], packet[5]); + if (bytes == 0 || (source < destination + bytes && destination < source + bytes)) return std::nullopt; + return MemoryCopy{source, destination, bytes}; +} + bool UsesGpuCacheBarrier(std::span packet) { require(!packet.empty() && ((packet[0] >> 8u) & 0xffu) == 0x58, "cache barrier requires ACQUIRE_MEM"); Validate(packet, 0); diff --git a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp index d8a2e8285..8a70a94c3 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp @@ -338,6 +338,29 @@ void testCopies() { #endif } +void testMemoryCopyDecode() { + using AgcDriver::Pm4::DecodeMemoryCopy; + constexpr std::uint64_t source = 0x1120000000ull, destination = 0x403d7b400ull; + const auto packet = [&](std::uint32_t control, std::uint64_t from, std::uint64_t to, std::uint32_t command) { + return makePacket(0x50, {control, static_cast(from), static_cast(from >> 32u), static_cast(to), static_cast(to >> 32u), command}); + }; + const auto copy = DecodeMemoryCopy(packet(0x60000000, source, destination, 0xbdd800)); + check(copy.has_value() && copy->source == source && copy->destination == destination && copy->bytes == 0xbdd800, "a memory-to-memory DMA_DATA did not decode as a copy"); + check(DecodeMemoryCopy(packet(0x00000000, source, destination, 64)).has_value(), "a DMA_DATA with memory selectors 0 did not decode as a copy"); + check(!DecodeMemoryCopy(packet(0x40000000, 0x44332211, destination, 64)).has_value(), "an immediate fill decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60100000, source, 0x100, 64)).has_value(), "a DMA_DATA to the GDS decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x20000000, 0x100, destination, 64)).has_value(), "a DMA_DATA from the GDS decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 26u))).has_value(), "a register source decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 27u))).has_value(), "a register destination decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 28u))).has_value(), "a non-incrementing source decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 29u))).has_value(), "a non-incrementing destination decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 0)).has_value(), "an empty DMA_DATA decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, source + 32, 64)).has_value(), "overlapping ranges decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source + 32, source, 64)).has_value(), "overlapping ranges below the source decoded as a copy"); + check(DecodeMemoryCopy(packet(0x60000000, source, source + 64, 64)).has_value(), "adjacent ranges did not decode as a copy"); + check(!DecodeMemoryCopy(makePacket(0x40, {0x10101, 0, 0, 0, 0})).has_value(), "a COPY_DATA decoded as a DMA_DATA copy"); +} + void testMemorySynchronization() { struct MemoryState { std::uint32_t source = 0; @@ -544,6 +567,7 @@ int main(int argc, char** argv) { testIndirectDraw(); testMemory(); testCopies(); + testMemoryCopyDecode(); testMemorySynchronization(); testEventWrite(); testAcquireMem();