| 1 |
/* |
| 2 |
* Copyright 2018 Google |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#include "HostVisibleMemoryVirtualization.h" |
| 6 |
|
| 7 |
#include <set> |
| 8 |
|
| 9 |
#include "ResourceTracker.h" |
| 10 |
#include "Resources.h" |
| 11 |
#include "VkEncoder.h" |
| 12 |
#include "util/detect_os.h" |
| 13 |
#include "util/log.h" |
| 14 |
|
| 15 |
namespace gfxstream { |
| 16 |
namespace vk { |
| 17 |
|
| 18 |
CoherentMemory::CoherentMemory(VirtGpuResourceMappingPtr blobMapping, uint64_t size, |
| 19 |
VkDevice device, VkDeviceMemory memory) |
| 20 |
: mSize(size), mBlobMapping(blobMapping), mDevice(device), mMemory(memory) { |
| 21 |
mHeap = u_mmInit(0, mSize); |
| 22 |
mBaseAddr = blobMapping->asRawPtr(); |
| 23 |
} |
| 24 |
|
| 25 |
#if DETECT_OS_ANDROID |
| 26 |
CoherentMemory::CoherentMemory(GoldfishAddressSpaceBlockPtr block, uint64_t gpuAddr, uint64_t size, |
| 27 |
VkDevice device, VkDeviceMemory memory) |
| 28 |
: mSize(size), mBlock(block), mDevice(device), mMemory(memory) { |
| 29 |
mHeap = u_mmInit(0, mSize); |
| 30 |
mBaseAddr = (uint8_t*)block->mmap(gpuAddr); |
| 31 |
} |
| 32 |
#endif // DETECT_OS_ANDROID |
| 33 |
|
| 34 |
CoherentMemory::~CoherentMemory() { |
| 35 |
ResourceTracker::getThreadLocalEncoder()->vkFreeMemorySyncGOOGLE(mDevice, mMemory, nullptr, |
| 36 |
false); |
| 37 |
u_mmDestroy(mHeap); |
| 38 |
} |
| 39 |
|
| 40 |
VkDeviceMemory CoherentMemory::getDeviceMemory() const { return mMemory; } |
| 41 |
|
| 42 |
bool CoherentMemory::subAllocate(uint64_t size, uint8_t** ptr, uint64_t& offset) { |
| 43 |
// 2^12 = 4096 (page size) |
| 44 |
auto block = u_mmAllocMem(mHeap, size, 12, 0); |
| 45 |
if (!block) return false; |
| 46 |
|
| 47 |
offset = block->ofs; |
| 48 |
*ptr = mBaseAddr + block->ofs; |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
bool CoherentMemory::release(uint64_t offset) { |
| 53 |
auto block = u_mmFindBlock(mHeap, offset); |
| 54 |
if (block) { |
| 55 |
u_mmFreeMem(block); |
| 56 |
return true; |
| 57 |
} else { |
| 58 |
mesa_loge("unable to find block"); |
| 59 |
} |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
} // namespace vk |
| 65 |
} // namespace gfxstream |