SWDEV-464828 - Initial implementation of VMM IPC on PAL/Windows.

Change-Id: I3d5e148fad9105704db6724b00df06bef4fc9d2f
This commit is contained in:
kjayapra-amd
2024-05-29 19:09:32 -04:00
committed by Karthik Jayaprakash
parent 191869b252
commit e7a7feb273
7 changed files with 79 additions and 23 deletions
+2 -9
View File
@@ -156,7 +156,7 @@ hipError_t hipMemExportToShareableHandle(void* shareableHandle,
}
if (!ga->asAmdMemory().getContext().devices()[0]->ExportShareableVMMHandle(
ga->asAmdMemory().getUserData().hsa_handle, flags, shareableHandle)) {
ga->asAmdMemory(), flags, shareableHandle)) {
LogPrintfError("Exporting Handle failed with flags: %d", flags);
HIP_RETURN(hipErrorInvalidValue);
}
@@ -224,20 +224,13 @@ hipError_t hipMemImportFromShareableHandle(hipMemGenericAllocationHandle_t* hand
}
amd::Device* device = hip::getCurrentDevice()->devices()[0];
amd::Memory* phys_mem_obj = new (device->context()) amd::Buffer(device->context(),
ROCCLR_MEM_PHYMEM | ROCCLR_MEM_INTERPROCESS, 0, osHandle);
amd::Memory* phys_mem_obj = device->ImportShareableVMMHandle(osHandle);
if (phys_mem_obj == nullptr) {
LogError("failed to new a va range curr_mem_obj object!");
HIP_RETURN(hipErrorInvalidValue);
}
if (!phys_mem_obj->create(nullptr, false)) {
LogError("failed to create a va range mem object");
phys_mem_obj->release();
HIP_RETURN(hipErrorInvalidValue);
}
hipMemAllocationProp prop {};
prop.type = hipMemAllocationTypePinned;
prop.location.type = hipMemLocationTypeDevice;
+7 -6
View File
@@ -1877,11 +1877,12 @@ class Device : public RuntimeObject {
/**
* Export Shareable VMM Handle to FD
*
* @param hsa_handle hsa_handle which has the phys_mem info.
* @param amd_mem_obj amd::Memory obj which holds the hsa_handle.
* @param flags any flags to be passed
* @param shareableHandle exported handle, points to fdesc.
*/
virtual bool ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shareableHandle) {
virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags,
void* shareableHandle) {
ShouldNotCallThis();
return false;
}
@@ -1889,12 +1890,12 @@ class Device : public RuntimeObject {
/**
* Import FD from Shareable VMM Handle
*
* @param osHandle os handle/fdesc
* @param hsa_handle_ptr hsa_handle which has the phys_mem info.
* @param osHandle os handle/fdesc/void*
* @param amd_mem_obj amd_mem_obj with hsa_handle/memory_obj.
*/
virtual bool ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr) const {
virtual amd::Memory* ImportShareableVMMHandle(void* osHandle) {
ShouldNotCallThis();
return false;
return nullptr;
}
/**
+29
View File
@@ -2780,4 +2780,33 @@ void Device::DestroyExtSemaphore(void* extSemaphore) {
amd::Os::alignedFree(extSemaphore);
}
// ================================================================================================
bool Device::ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle) {
device::Memory* dev_mem = static_cast<device::Memory*>(amd_mem_obj.getDeviceMemory(*this));
return dev_mem->ExportHandle(shareableHandle);
}
// ================================================================================================
amd::Memory* Device::ImportShareableVMMHandle(void* osHandle) {
int flags = 0;
size_t mem_offset = 0;
size_t mem_size = 0;
amd::Memory* amd_mem_obj = new (context()) amd::IpcBuffer(context(), flags, mem_offset,
mem_size, osHandle);
if (amd_mem_obj == nullptr) {
LogError("failed to create a mem object!");
return nullptr;
}
if (!amd_mem_obj->create(nullptr)) {
LogError("failed to create a svm hidden buffer!");
amd_mem_obj->release();
return nullptr;
}
return amd_mem_obj;
}
} // namespace amd::pal
+13
View File
@@ -155,6 +155,15 @@ class NullDevice : public amd::Device {
return true;
}
virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags,
void* shareableHandle) {
return false;
}
virtual amd::Memory* ImportShareableVMMHandle(void* osHandle) {
return nullptr;
}
virtual bool importExtSemaphore(void** extSemaphore,const amd::Os::FileDesc& handle,
amd::ExternalSemaphoreHandleType sem_handle_type) override {
return false;
@@ -557,6 +566,10 @@ class Device : public NullDevice {
return true;
}
virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle);
virtual amd::Memory* ImportShareableVMMHandle(void* osHandle);
//! Returns SRD manger object
SrdManager& srds() const { return *srdManager_; }
+23 -5
View File
@@ -2534,17 +2534,17 @@ bool Device::GetMemAccess(void* va_addr, VmmAccess* access_flags_ptr) {
}
// ================================================================================================
bool Device::ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shareableHandle) {
bool Device::ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle) {
hsa_status_t hsa_status = HSA_STATUS_SUCCESS;
hsa_amd_vmem_alloc_handle_t hsa_vmem_handle {};
hsa_vmem_handle.handle = amd_mem_obj.getUserData().hsa_handle;
int dmabuf_fd = 0;
if (hsa_handle == 0) {
if (hsa_vmem_handle.handle == 0) {
LogError("HSA Handle is not valid");
return false;
}
int dmabuf_fd = 0;
hsa_vmem_handle.handle = hsa_handle;
if ((hsa_status = hsa_amd_vmem_export_shareable_handle(&dmabuf_fd,
hsa_vmem_handle, flags)) != HSA_STATUS_SUCCESS) {
LogPrintfError("Failed hsa_vmem_export_shareable_handle with status: %d \n", hsa_status);
@@ -2557,7 +2557,7 @@ bool Device::ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shar
}
// ================================================================================================
bool Device::ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr) const {
bool Device::ImportShareableHSAHandle(void* osHandle, uint64_t* hsa_handle_ptr) const {
hsa_status_t hsa_status = HSA_STATUS_SUCCESS;
hsa_amd_vmem_alloc_handle_t hsa_vmem_handle {};
@@ -2577,6 +2577,24 @@ bool Device::ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr)
return true;
}
// ================================================================================================
amd::Memory* Device::ImportShareableVMMHandle(void* osHandle) {
amd::Memory* amd_mem_obj = new (context()) amd::Buffer(context(),
ROCCLR_MEM_PHYMEM | ROCCLR_MEM_INTERPROCESS, 0, osHandle);
if (amd_mem_obj == nullptr) {
LogError("Cannot create memory object");
return nullptr;
}
if (!amd_mem_obj->create(nullptr, false)) {
LogError("Failed to create mem_obj from imported fd");
amd_mem_obj->release();
return nullptr;
}
return amd_mem_obj;
}
// ================================================================================================
bool Device::SetSvmAttributesInt(const void* dev_ptr, size_t count,
amd::MemoryAdvice advice, bool first_alloc, bool use_cpu) const {
+4 -2
View File
@@ -480,9 +480,11 @@ class Device : public NullDevice {
virtual bool SetMemAccess(void* va_addr, size_t va_size, VmmAccess access_flags);
virtual bool GetMemAccess(void* va_addr, VmmAccess* access_flags_ptr);
virtual bool ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shareableHandle);
virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle);
virtual bool ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr) const;
bool ImportShareableHSAHandle(void* osHandle, uint64_t* hsa_handle_ptr) const;
virtual amd::Memory* ImportShareableVMMHandle(void* osHandle);
virtual bool SetClockMode(const cl_set_device_clock_mode_input_amd setClockModeInput,
cl_set_device_clock_mode_output_amd* pSetClockModeOutput);
+1 -1
View File
@@ -772,7 +772,7 @@ bool Buffer::create(bool alloc_local) {
if (memFlags & ROCCLR_MEM_INTERPROCESS) {
int dmabuf_fd = *(reinterpret_cast<int*>(owner()->getSvmPtr()));
// if interprocess flag is set, then the memory is importable.
if (!dev().ImportShareableVMMHandle(owner()->getSvmPtr(),
if (!dev().ImportShareableHSAHandle(owner()->getSvmPtr(),
&owner()->getUserData().hsa_handle)) {
LogPrintfError("Importing Shareable Memory failed with os_handle: 0x%x \n",
owner()->getSvmPtr());