diff options
author | Weiyi Wang <wwylele@gmail.com> | 2017-07-22 09:15:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-22 09:15:52 +0200 |
commit | 045d0b5bbdf790952ddfedcfc8816c0afc7a2300 (patch) | |
tree | fba1a440adf7d7ecd59edaf48e215e70b7c6cf9e /src/core/memory.cpp | |
parent | Merge pull request #2833 from j-selby/single-header-json (diff) | |
parent | Memory: Add function to flush a virtual range from the rasterizer cache (diff) | |
download | yuzu-045d0b5bbdf790952ddfedcfc8816c0afc7a2300.tar yuzu-045d0b5bbdf790952ddfedcfc8816c0afc7a2300.tar.gz yuzu-045d0b5bbdf790952ddfedcfc8816c0afc7a2300.tar.bz2 yuzu-045d0b5bbdf790952ddfedcfc8816c0afc7a2300.tar.lz yuzu-045d0b5bbdf790952ddfedcfc8816c0afc7a2300.tar.xz yuzu-045d0b5bbdf790952ddfedcfc8816c0afc7a2300.tar.zst yuzu-045d0b5bbdf790952ddfedcfc8816c0afc7a2300.zip |
Diffstat (limited to 'src/core/memory.cpp')
-rw-r--r-- | src/core/memory.cpp | 128 |
1 files changed, 76 insertions, 52 deletions
diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 9024f4922..65649d9d7 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -83,19 +83,13 @@ static void MapPages(u32 base, u32 size, u8* memory, PageType type) { LOG_DEBUG(HW_Memory, "Mapping %p onto %08X-%08X", memory, base * PAGE_SIZE, (base + size) * PAGE_SIZE); - u32 end = base + size; + RasterizerFlushVirtualRegion(base << PAGE_BITS, size * PAGE_SIZE, + FlushMode::FlushAndInvalidate); + u32 end = base + size; while (base != end) { ASSERT_MSG(base < PAGE_TABLE_NUM_ENTRIES, "out of range mapping at %08X", base); - // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be - // null here - if (current_page_table->attributes[base] == PageType::RasterizerCachedMemory || - current_page_table->attributes[base] == PageType::RasterizerCachedSpecial) { - RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(base << PAGE_BITS), - PAGE_SIZE); - } - current_page_table->attributes[base] = type; current_page_table->pointers[base] = memory; current_page_table->cached_res_count[base] = 0; @@ -196,7 +190,7 @@ T Read(const VAddr vaddr) { ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr); break; case PageType::RasterizerCachedMemory: { - RasterizerFlushRegion(VirtualToPhysicalAddress(vaddr), sizeof(T)); + RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::Flush); T value; std::memcpy(&value, GetPointerFromVMA(vaddr), sizeof(T)); @@ -205,8 +199,7 @@ T Read(const VAddr vaddr) { case PageType::Special: return ReadMMIO<T>(GetMMIOHandler(vaddr), vaddr); case PageType::RasterizerCachedSpecial: { - RasterizerFlushRegion(VirtualToPhysicalAddress(vaddr), sizeof(T)); - + RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::Flush); return ReadMMIO<T>(GetMMIOHandler(vaddr), vaddr); } default: @@ -236,8 +229,7 @@ void Write(const VAddr vaddr, const T data) { ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr); break; case PageType::RasterizerCachedMemory: { - RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(vaddr), sizeof(T)); - + RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::FlushAndInvalidate); std::memcpy(GetPointerFromVMA(vaddr), &data, sizeof(T)); break; } @@ -245,8 +237,7 @@ void Write(const VAddr vaddr, const T data) { WriteMMIO<T>(GetMMIOHandler(vaddr), vaddr, data); break; case PageType::RasterizerCachedSpecial: { - RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(vaddr), sizeof(T)); - + RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::FlushAndInvalidate); WriteMMIO<T>(GetMMIOHandler(vaddr), vaddr, data); break; } @@ -275,7 +266,8 @@ bool IsValidVirtualAddress(const VAddr vaddr) { } bool IsValidPhysicalAddress(const PAddr paddr) { - return IsValidVirtualAddress(PhysicalToVirtualAddress(paddr)); + boost::optional<VAddr> vaddr = PhysicalToVirtualAddress(paddr); + return vaddr && IsValidVirtualAddress(*vaddr); } u8* GetPointer(const VAddr vaddr) { @@ -308,7 +300,8 @@ std::string ReadCString(VAddr vaddr, std::size_t max_length) { u8* GetPhysicalPointer(PAddr address) { // TODO(Subv): This call should not go through the application's memory mapping. - return GetPointer(PhysicalToVirtualAddress(address)); + boost::optional<VAddr> vaddr = PhysicalToVirtualAddress(address); + return vaddr ? GetPointer(*vaddr) : nullptr; } void RasterizerMarkRegionCached(PAddr start, u32 size, int count_delta) { @@ -319,8 +312,12 @@ void RasterizerMarkRegionCached(PAddr start, u32 size, int count_delta) { u32 num_pages = ((start + size - 1) >> PAGE_BITS) - (start >> PAGE_BITS) + 1; PAddr paddr = start; - for (unsigned i = 0; i < num_pages; ++i) { - VAddr vaddr = PhysicalToVirtualAddress(paddr); + for (unsigned i = 0; i < num_pages; ++i, paddr += PAGE_SIZE) { + boost::optional<VAddr> maybe_vaddr = PhysicalToVirtualAddress(paddr); + if (!maybe_vaddr) + continue; + VAddr vaddr = *maybe_vaddr; + u8& res_count = current_page_table->cached_res_count[vaddr >> PAGE_BITS]; ASSERT_MSG(count_delta <= UINT8_MAX - res_count, "Rasterizer resource cache counter overflow!"); @@ -368,7 +365,6 @@ void RasterizerMarkRegionCached(PAddr start, u32 size, int count_delta) { UNREACHABLE(); } } - paddr += PAGE_SIZE; } } @@ -379,11 +375,48 @@ void RasterizerFlushRegion(PAddr start, u32 size) { } void RasterizerFlushAndInvalidateRegion(PAddr start, u32 size) { + // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be + // null here if (VideoCore::g_renderer != nullptr) { VideoCore::g_renderer->Rasterizer()->FlushAndInvalidateRegion(start, size); } } +void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode) { + // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be + // null here + if (VideoCore::g_renderer != nullptr) { + VAddr end = start + size; + + auto CheckRegion = [&](VAddr region_start, VAddr region_end) { + if (start >= region_end || end <= region_start) { + // No overlap with region + return; + } + + VAddr overlap_start = std::max(start, region_start); + VAddr overlap_end = std::min(end, region_end); + + PAddr physical_start = TryVirtualToPhysicalAddress(overlap_start).value(); + u32 overlap_size = overlap_end - overlap_start; + + auto* rasterizer = VideoCore::g_renderer->Rasterizer(); + switch (mode) { + case FlushMode::Flush: + rasterizer->FlushRegion(physical_start, overlap_size); + break; + case FlushMode::FlushAndInvalidate: + rasterizer->FlushAndInvalidateRegion(physical_start, overlap_size); + break; + } + }; + + CheckRegion(LINEAR_HEAP_VADDR, LINEAR_HEAP_VADDR_END); + CheckRegion(NEW_LINEAR_HEAP_VADDR, NEW_LINEAR_HEAP_VADDR_END); + CheckRegion(VRAM_VADDR, VRAM_VADDR_END); + } +} + u8 Read8(const VAddr addr) { return Read<u8>(addr); } @@ -430,16 +463,13 @@ void ReadBlock(const VAddr src_addr, void* dest_buffer, const size_t size) { break; } case PageType::RasterizerCachedMemory: { - RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount); - + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush); std::memcpy(dest_buffer, GetPointerFromVMA(current_vaddr), copy_amount); break; } case PageType::RasterizerCachedSpecial: { DEBUG_ASSERT(GetMMIOHandler(current_vaddr)); - - RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount); - + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush); GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, dest_buffer, copy_amount); break; } @@ -500,18 +530,13 @@ void WriteBlock(const VAddr dest_addr, const void* src_buffer, const size_t size break; } case PageType::RasterizerCachedMemory: { - RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), - copy_amount); - + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate); std::memcpy(GetPointerFromVMA(current_vaddr), src_buffer, copy_amount); break; } case PageType::RasterizerCachedSpecial: { DEBUG_ASSERT(GetMMIOHandler(current_vaddr)); - - RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), - copy_amount); - + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate); GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, src_buffer, copy_amount); break; } @@ -557,18 +582,13 @@ void ZeroBlock(const VAddr dest_addr, const size_t size) { break; } case PageType::RasterizerCachedMemory: { - RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), - copy_amount); - + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate); std::memset(GetPointerFromVMA(current_vaddr), 0, copy_amount); break; } case PageType::RasterizerCachedSpecial: { DEBUG_ASSERT(GetMMIOHandler(current_vaddr)); - - RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), - copy_amount); - + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate); GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, zeros.data(), copy_amount); break; } @@ -613,15 +633,13 @@ void CopyBlock(VAddr dest_addr, VAddr src_addr, const size_t size) { break; } case PageType::RasterizerCachedMemory: { - RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount); - + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush); WriteBlock(dest_addr, GetPointerFromVMA(current_vaddr), copy_amount); break; } case PageType::RasterizerCachedSpecial: { DEBUG_ASSERT(GetMMIOHandler(current_vaddr)); - - RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount); + RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush); std::vector<u8> buffer(copy_amount); GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, buffer.data(), buffer.size()); @@ -680,7 +698,7 @@ void WriteMMIO<u64>(MMIORegionPointer mmio_handler, VAddr addr, const u64 data) mmio_handler->Write64(addr, data); } -PAddr VirtualToPhysicalAddress(const VAddr addr) { +boost::optional<PAddr> TryVirtualToPhysicalAddress(const VAddr addr) { if (addr == 0) { return 0; } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) { @@ -697,12 +715,20 @@ PAddr VirtualToPhysicalAddress(const VAddr addr) { return addr - N3DS_EXTRA_RAM_VADDR + N3DS_EXTRA_RAM_PADDR; } - LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08X", addr); - // To help with debugging, set bit on address so that it's obviously invalid. - return addr | 0x80000000; + return boost::none; +} + +PAddr VirtualToPhysicalAddress(const VAddr addr) { + auto paddr = TryVirtualToPhysicalAddress(addr); + if (!paddr) { + LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08X", addr); + // To help with debugging, set bit on address so that it's obviously invalid. + return addr | 0x80000000; + } + return *paddr; } -VAddr PhysicalToVirtualAddress(const PAddr addr) { +boost::optional<VAddr> PhysicalToVirtualAddress(const PAddr addr) { if (addr == 0) { return 0; } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) { @@ -717,9 +743,7 @@ VAddr PhysicalToVirtualAddress(const PAddr addr) { return addr - N3DS_EXTRA_RAM_PADDR + N3DS_EXTRA_RAM_VADDR; } - LOG_ERROR(HW_Memory, "Unknown physical address @ 0x%08X", addr); - // To help with debugging, set bit on address so that it's obviously invalid. - return addr | 0x80000000; + return boost::none; } } // namespace |