/* Copyright (c) 2016-present Advanced Micro Devices, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #ifndef WITHOUT_HSA_BACKEND #include "top.hpp" #include "platform/memory.hpp" #include "utils/debug.hpp" #include "device/rocm/rocdevice.hpp" #include "device/rocm/rocglinterop.hpp" namespace roc { class Memory : public device::Memory { public: enum MEMORY_KIND { MEMORY_KIND_NORMAL = 0, MEMORY_KIND_LOCK, MEMORY_KIND_GART, MEMORY_KIND_INTEROP }; Memory(const roc::Device& dev, amd::Memory& owner); Memory(const roc::Device& dev, size_t size); virtual ~Memory(); // Getter for deviceMemory_ address getDeviceMemory() const { return reinterpret_cast
(deviceMemory_); } // Gets a pointer to a region of host-visible memory for use as the target // of an indirect map for a given memory object void* allocMapTarget(const amd::Coord3D& origin, const amd::Coord3D& region, uint mapFlags, size_t* rowPitch, size_t* slicePitch) override; // Create device memory according to OpenCL memory flag. virtual bool create() = 0; // Pins system memory associated with this memory object. bool pinSystemMemory(void* hostPtr, // System memory address size_t size // Size of allocated system memory ) override; //! Updates device memory from the owner's host allocation void syncCacheFromHost(VirtualGPU& gpu, //!< Virtual GPU device object //! Synchronization flags device::Memory::SyncFlags syncFlags = device::Memory::SyncFlags()); // Immediate blocking write from device cache to owners's backing store. // Marks owner as "current" by resetting the last writer to nullptr. void syncHostFromCache(SyncFlags syncFlags = SyncFlags()) override; //! Allocates host memory for synchronization with MGPU context void mgpuCacheWriteBack(); // Releases indirect map surface void releaseIndirectMap() override { decIndMapCount(); } //! Map the device memory to CPU visible void* cpuMap(device::VirtualDevice& vDev, //!< Virtual device for map operaiton uint flags = 0, //!< flags for the map operation // Optimization for multilayer map/unmap uint startLayer = 0, //!< Start layer for multilayer map uint numLayers = 0, //!< End layer for multilayer map size_t* rowPitch = nullptr, //!< Row pitch for the device memory size_t* slicePitch = nullptr //!< Slice pitch for the device memory ) override; //! Unmap the device memory void cpuUnmap(device::VirtualDevice& vDev //!< Virtual device for unmap operaiton ) override; // Mesa has already decomressed if needed and also does acquire at the start of every command // batch. bool processGLResource(GLResourceOP operation) override { return true; } virtual uint64_t virtualAddress() const override { return reinterpret_cast