SWDEV-558848 - vmm api support for rocr on windows (#1761)

* SWDEV-558848 - vmm api support for rocr on windows

* Fixes to VMM handle Map/Unmap Set/Get Access

* Fix GetShareableHandle to use pointer for shareable handle

* Update os specific map/unmap memory calls

* clang format update

* Minor syntax fixes from code review

Co-authored-by: Yiannis Papadopoulos <102817138+ypapadop-amd@users.noreply.github.com>

---------

Co-authored-by: Rahul Manocha <rmanocha@amd.com>
Co-authored-by: Yiannis Papadopoulos <102817138+ypapadop-amd@users.noreply.github.com>
This commit is contained in:
Rahul Manocha
2025-12-10 08:39:51 -08:00
committed by GitHub
vanhempi 465633d707
commit 0c1f87a7f6
15 muutettua tiedostoa jossa 121 lisäystä ja 52 poistoa
@@ -2628,7 +2628,7 @@ void Runtime::CheckVirtualMemApiSupport() {
virtual_mem_api_supported_ = true;
}
#else
virtual_mem_api_supported_ = false;
virtual_mem_api_supported_ = true;
#endif
}
}
@@ -3672,11 +3672,21 @@ hsa_status_t Runtime::VMemoryHandleMap(void* va, size_t size, size_t in_offset,
if (status != HSA_STATUS_SUCCESS)
return status;
close(dmabuf_fd);
if (dmabuf_fd != -1) {
close(dmabuf_fd);
}
// Get address that memory is mapped to
ret = GetAmdgpuDeviceArgs(agent, shareable_handle, &drm_fd, &drm_cpu_addr);
if (ret) return HSA_STATUS_ERROR;
if (shareable_handle.IsValid()) {
ret = GetAmdgpuDeviceArgs(agent, shareable_handle, &drm_fd, &drm_cpu_addr);
if (ret) return HSA_STATUS_ERROR;
} else {
hsa_status_t status = agent_driver.GetShareableHandle(memoryHandleIt->first, size, &shareable_handle);
if (status != HSA_STATUS_SUCCESS) {
return status;
}
drm_cpu_addr = reinterpret_cast<uint64_t>(va);
}
mapped_handle_map_.emplace(
std::piecewise_construct, std::forward_as_tuple(va),
@@ -3795,22 +3805,17 @@ Runtime::MappedHandleAllowedAgent::~MappedHandleAllowedAgent() {
hsa_status_t Runtime::MappedHandleAllowedAgent::EnableAccess(hsa_access_permission_t perms) {
if (targetAgent->device_type() == core::Agent::DeviceType::kAmdCpuDevice) {
#if defined(__linux__)
if (!core::Runtime::runtime_singleton_->thunkLoader()->IsDXG()) {
void* mapped_ptr =
mmap(va, size, PermissionsToMmapFlags(perms), MAP_SHARED | MAP_FIXED, mappedHandle->drm_fd,
reinterpret_cast<uint64_t>(mappedHandle->drm_cpu_addr));
if (mapped_ptr != va)
if (!rocr::os::MapMemory(va, size, PermissionsToMemProt(perms), mappedHandle->drm_fd,
reinterpret_cast<uint64_t>(mappedHandle->drm_cpu_addr))) {
return HSA_STATUS_ERROR;
}
}
} else {
hsa_status_t status = targetAgent->driver().Map(
shareable_handle, va, mappedHandle->offset, size, perms);
if (status != HSA_STATUS_SUCCESS)
return status;
#else
assert(!"Unimplemented!");
#endif
}
permissions = perms;
return HSA_STATUS_SUCCESS;
@@ -3819,21 +3824,15 @@ hsa_status_t Runtime::MappedHandleAllowedAgent::EnableAccess(hsa_access_permissi
hsa_status_t Runtime::MappedHandleAllowedAgent::RemoveAccess() {
if (targetAgent->device_type() == core::Agent::DeviceType::kAmdCpuDevice) {
if (permissions != HSA_ACCESS_PERMISSION_NONE) {
#if defined(__linux__)
if (munmap(va, size) != 0) return HSA_STATUS_ERROR;
/* We need to keep the CPU mapping. So change it to PROT_NONE */
void* mapped_ptr = mmap(va, mappedHandle->size, PROT_NONE, MAP_SHARED | MAP_FIXED,
mappedHandle->drm_fd,
reinterpret_cast<uint64_t>(mappedHandle->drm_cpu_addr));
if (mapped_ptr != va)
hsa_access_permission_t perms = HSA_ACCESS_PERMISSION_NONE;
if (!rocr::os::UnmapMemory(va, size)) {
return HSA_STATUS_ERROR;
permissions = HSA_ACCESS_PERMISSION_NONE;
#else
assert(!"Unimplemented!");
#endif
}
if (!rocr::os::MapMemory(va, size, PermissionsToMemProt(perms), mappedHandle->drm_fd,
reinterpret_cast<uint64_t>(mappedHandle->drm_cpu_addr))) {
return HSA_STATUS_ERROR;
}
permissions = perms;
}
} else {
return targetAgent->driver().Unmap(
@@ -3850,6 +3849,7 @@ Runtime::MappedHandle::MappedHandle(MemoryHandle *mem_handle, AddressHandle *add
shareable_handle(shareable_handle)
{
/* Create a CPU mapping with PROT_NONE */
#if defined(__linux__)
auto cpu_agent = static_cast<AMD::GpuAgent*>(agentOwner())->GetNearestCpuAgent();
auto agentPermsIt = allowed_agents.emplace(std::piecewise_construct,
std::forward_as_tuple(cpu_agent),
@@ -3860,6 +3860,7 @@ Runtime::MappedHandle::MappedHandle(MemoryHandle *mem_handle, AddressHandle *add
auto ret = agentPermsIt->second.EnableAccess(HSA_ACCESS_PERMISSION_NONE);
if (ret != HSA_STATUS_SUCCESS)
throw AMD::hsa_exception(ret, "Failed to create default CPU mapping");
#endif
}
// Note: VMemorySetAccessPerHandle should be called with &memory_lock_ held
@@ -390,6 +390,9 @@ namespace core {
HSAKMT_PFN(hsaKmtAisReadWriteFile) = (HSAKMT_DEF(hsaKmtAisReadWriteFile)*)dlsym(thunk_handle, "hsaKmtAisReadWriteFile");
if (HSAKMT_PFN(hsaKmtAisReadWriteFile) == NULL) goto ERROR;
HSAKMT_PFN(hsaKmtGetMemoryHandle) = (HSAKMT_DEF(hsaKmtGetMemoryHandle)*)dlsym(thunk_handle, "hsaKmtGetMemoryHandle");
if (HSAKMT_PFN(hsaKmtGetMemoryHandle) == NULL) goto ERROR;
DRM_PFN(amdgpu_device_deinitialize) = (DRM_DEF(amdgpu_device_deinitialize)*)dlsym(thunk_handle, "amdgpu_device_deinitialize");
if (DRM_PFN(amdgpu_device_deinitialize) == NULL) goto ERROR;
@@ -521,6 +524,7 @@ ERROR:
#endif
HSAKMT_PFN(hsaKmtModelEnabled) = (HSAKMT_DEF(hsaKmtModelEnabled)*)(&hsaKmtModelEnabled);
HSAKMT_PFN(hsaKmtAisReadWriteFile) = (HSAKMT_DEF(hsaKmtAisReadWriteFile)*)(&hsaKmtAisReadWriteFile);
HSAKMT_PFN(hsaKmtGetMemoryHandle) = (HSAKMT_DEF(hsaKmtGetMemoryHandle)*)(&hsaKmtGetMemoryHandle);
DRM_PFN(amdgpu_device_initialize) = (DRM_DEF(amdgpu_device_initialize)*)(&amdgpu_device_initialize);
DRM_PFN(amdgpu_device_deinitialize) = (DRM_DEF(amdgpu_device_deinitialize)*)(&amdgpu_device_deinitialize);