Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit 0cd4a26

Browse files
eddierichter-amdypapadop-amd
authored andcommitted
rocr/aie: Handle sideband
* Adding a sideband interface to get a handle from a virtual address. This is just temporary and should be removed when we move to the vmem API * Removing unused queue structure * Removing the queue from the arguments of hsa_amd_get_handle_from_vaddr * Free XDNA BOs --------- Co-authored-by: Yiannis Papadopoulos <[email protected]>
1 parent 9a99e7f commit 0cd4a26

14 files changed

+87
-6
lines changed

runtime/hsa-runtime/core/common/hsa_table_interface.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,11 @@ hsa_amd_queue_hw_ctx_config(const hsa_queue_t *queue,
930930
return amdExtTable->hsa_amd_queue_hw_ctx_config_fn(queue, config_type, args);
931931
}
932932

933+
// Mirrors AMD Extension APIs.
934+
hsa_status_t hsa_amd_get_handle_from_vaddr(void* ptr, uint32_t* handle) {
935+
return amdExtTable->hsa_amd_get_handle_from_vaddr_fn(ptr, handle);
936+
}
937+
933938
// Mirrors Amd Extension Apis
934939
hsa_status_t HSA_API hsa_amd_queue_cu_set_mask(const hsa_queue_t* queue,
935940
uint32_t num_cu_mask_count,

runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ KfdDriver::ConfigHwCtx(core::Queue &queue,
249249
hsa_amd_queue_hw_ctx_config_param_t config_type,
250250
void *args) {
251251
// Only AIE queues support this for now.
252-
return HSA_STATUS_ERROR_INVALID_QUEUE;
252+
return HSA_STATUS_ERROR_INVALID_AGENT;
253+
}
254+
255+
hsa_status_t KfdDriver::GetHandleFromVaddr(void* ptr, uint32_t* handle) {
256+
// Only AIE queues support this for now.
257+
return HSA_STATUS_ERROR_INVALID_AGENT;
253258
}
254259

255260
void *KfdDriver::AllocateKfdMemory(const HsaMemFlags &flags, uint32_t node_id,

runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp

+31-4
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,17 @@ hsa_status_t
134134
XdnaDriver::AllocateMemory(const core::MemoryRegion &mem_region,
135135
core::MemoryRegion::AllocateFlags alloc_flags,
136136
void **mem, size_t size, uint32_t node_id) {
137-
const MemoryRegion &m_region(static_cast<const MemoryRegion &>(mem_region));
137+
const auto &region = static_cast<const MemoryRegion &>(mem_region);
138138
amdxdna_drm_create_bo create_bo_args{.size = size};
139139
amdxdna_drm_get_bo_info get_bo_info_args{0};
140140
drm_gem_close close_bo_args{0};
141141
void *mapped_mem(nullptr);
142142

143-
if (!m_region.IsSystem()) {
143+
if (!region.IsSystem()) {
144144
return HSA_STATUS_ERROR_INVALID_REGION;
145145
}
146146

147-
if (m_region.kernarg()) {
147+
if (region.kernarg()) {
148148
create_bo_args.type = AMDXDNA_BO_CMD;
149149
} else {
150150
create_bo_args.type = AMDXDNA_BO_DEV;
@@ -187,11 +187,30 @@ XdnaDriver::AllocateMemory(const core::MemoryRegion &mem_region,
187187
}
188188

189189
vmem_handle_mappings.emplace(create_bo_args.handle, mapped_mem);
190+
vmem_handle_mappings_reverse.emplace(mapped_mem, create_bo_args.handle);
190191

191192
return HSA_STATUS_SUCCESS;
192193
}
193194

194-
hsa_status_t XdnaDriver::FreeMemory(void *mem, size_t size) {
195+
hsa_status_t XdnaDriver::FreeMemory(void* ptr, size_t size) {
196+
auto it = vmem_handle_mappings_reverse.find(ptr);
197+
if (it == vmem_handle_mappings_reverse.end())
198+
return HSA_STATUS_ERROR_INVALID_ALLOCATION;
199+
200+
// TODO:ypapadop-amd: need to unmap memory, but we don't know if it's mapped or not as we don't have
201+
// region information
202+
203+
auto handle = it->second;
204+
205+
drm_gem_close close_args = {};
206+
close_args.handle = handle;
207+
if (ioctl(fd_, DRM_IOCTL_GEM_CLOSE, &close_args) < 0) {
208+
return HSA_STATUS_ERROR;
209+
}
210+
211+
vmem_handle_mappings.erase(handle);
212+
vmem_handle_mappings_reverse.erase(it);
213+
195214
return HSA_STATUS_SUCCESS;
196215
}
197216

@@ -257,6 +276,14 @@ XdnaDriver::ConfigHwCtx(core::Queue &queue,
257276
}
258277
}
259278

279+
hsa_status_t XdnaDriver::GetHandleFromVaddr(void* ptr, uint32_t* handle) {
280+
auto it = vmem_handle_mappings_reverse.find(ptr);
281+
if (it == vmem_handle_mappings_reverse.end())
282+
return HSA_STATUS_ERROR_INVALID_ALLOCATION;
283+
*handle = it->second;
284+
return HSA_STATUS_SUCCESS;
285+
}
286+
260287
hsa_status_t XdnaDriver::QueryDriverVersion() {
261288
amdxdna_drm_query_aie_version aie_version{0, 0};
262289
amdxdna_drm_get_info args{DRM_AMDXDNA_QUERY_AIE_VERSION, sizeof(aie_version),

runtime/hsa-runtime/core/inc/amd_kfd_driver.h

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class KfdDriver : public core::Driver {
8282
hsa_status_t ConfigHwCtx(core::Queue &queue,
8383
hsa_amd_queue_hw_ctx_config_param_t config_type,
8484
void *args) override;
85+
hsa_status_t GetHandleFromVaddr(void* ptr, uint32_t* handle) override;
8586

8687
private:
8788
/// @brief Allocate agent accessible memory (system / local memory).

runtime/hsa-runtime/core/inc/amd_xdna_driver.h

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class XdnaDriver : public core::Driver {
8989
hsa_amd_queue_hw_ctx_config_param_t config_type,
9090
void *args) override;
9191

92+
hsa_status_t GetHandleFromVaddr(void* ptr, uint32_t* handle) override;
93+
9294
private:
9395
hsa_status_t QueryDriverVersion();
9496
/// @brief Allocate device accesible heap space.
@@ -110,6 +112,9 @@ class XdnaDriver : public core::Driver {
110112
/// to manage some of this for now.
111113
std::unordered_map<uint32_t, void *> vmem_handle_mappings;
112114

115+
// TODO: Remove this once we move to the vmem API
116+
std::unordered_map<void*, uint32_t> vmem_handle_mappings_reverse;
117+
113118
/// @brief Virtual address range allocated for the device heap.
114119
///
115120
/// Allocate a large enough space so we can carve out the device heap in

runtime/hsa-runtime/core/inc/driver.h

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class Driver {
138138
ConfigHwCtx(Queue &queue, hsa_amd_queue_hw_ctx_config_param_t config_type,
139139
void *args) = 0;
140140

141+
virtual hsa_status_t GetHandleFromVaddr(void* ptr, uint32_t* handle) = 0;
142+
141143
/// Unique identifier for supported kernel-mode drivers.
142144
const DriverType kernel_driver_type_;
143145

runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ hsa_status_t
113113
hsa_amd_queue_hw_ctx_config(const hsa_queue_t *queue,
114114
hsa_amd_queue_hw_ctx_config_param_t config_type,
115115
void *args);
116+
117+
// Mirrors AMD Extension APIs.
118+
hsa_status_t hsa_amd_get_handle_from_vaddr(void* ptr, uint32_t* handle);
119+
116120
// Mirrors Amd Extension Apis
117121
hsa_status_t hsa_amd_queue_cu_set_mask(const hsa_queue_t* queue,
118122
uint32_t num_cu_mask_count,

runtime/hsa-runtime/core/inc/runtime.h

+2
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ class Runtime {
403403
const core::MemoryRegion** mem_region,
404404
hsa_amd_memory_type_t* type);
405405

406+
hsa_status_t GetHandleFromVaddr(void* ptr, uint32_t* handle);
407+
406408
hsa_status_t EnableLogging(uint8_t* flags, void* file);
407409

408410
const std::vector<Agent*>& cpu_agents() { return cpu_agents_; }

runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void HsaApiTable::Init() {
8080
// they can add preprocessor macros on the new functions
8181

8282
constexpr size_t expected_core_api_table_size = 1016;
83-
constexpr size_t expected_amd_ext_table_size = 592;
83+
constexpr size_t expected_amd_ext_table_size = 600;//592;
8484
constexpr size_t expected_image_ext_table_size = 120;
8585
constexpr size_t expected_finalizer_ext_table_size = 64;
8686
constexpr size_t expected_tools_table_size = 64;
@@ -407,6 +407,7 @@ void HsaApiTable::UpdateAmdExts() {
407407
amd_ext_api.hsa_amd_async_function_fn = AMD::hsa_amd_async_function;
408408
amd_ext_api.hsa_amd_signal_wait_any_fn = AMD::hsa_amd_signal_wait_any;
409409
amd_ext_api.hsa_amd_queue_hw_ctx_config_fn = AMD::hsa_amd_queue_hw_ctx_config;
410+
amd_ext_api.hsa_amd_get_handle_from_vaddr_fn = AMD::hsa_amd_get_handle_from_vaddr;
410411
amd_ext_api.hsa_amd_queue_cu_set_mask_fn = AMD::hsa_amd_queue_cu_set_mask;
411412
amd_ext_api.hsa_amd_queue_cu_get_mask_fn = AMD::hsa_amd_queue_cu_get_mask;
412413
amd_ext_api.hsa_amd_memory_pool_get_info_fn = AMD::hsa_amd_memory_pool_get_info;

runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,19 @@ hsa_amd_queue_hw_ctx_config(const hsa_queue_t *queue,
634634
CATCH;
635635
}
636636

637+
638+
hsa_status_t hsa_amd_get_handle_from_vaddr(void* ptr, uint32_t* handle) {
639+
TRY;
640+
IS_OPEN();
641+
642+
IS_BAD_PTR(ptr);
643+
IS_BAD_PTR(handle);
644+
645+
return core::Runtime::runtime_singleton_->GetHandleFromVaddr(ptr, handle);
646+
647+
CATCH;
648+
}
649+
637650
hsa_status_t hsa_amd_queue_cu_set_mask(const hsa_queue_t* queue, uint32_t num_cu_mask_count,
638651
const uint32_t* cu_mask) {
639652
TRY;

runtime/hsa-runtime/core/runtime/runtime.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -3605,6 +3605,17 @@ hsa_status_t Runtime::VMemoryGetAllocPropertiesFromHandle(hsa_amd_vmem_alloc_han
36053605
return HSA_STATUS_SUCCESS;
36063606
}
36073607

3608+
hsa_status_t Runtime::GetHandleFromVaddr(void* ptr, uint32_t* handle) {
3609+
auto it = allocation_map_.find(ptr);
3610+
if (it == allocation_map_.end()) {
3611+
return HSA_STATUS_ERROR_INVALID_ALLOCATION;
3612+
}
3613+
3614+
auto* agent = it->second.region->owner();
3615+
auto& driver = AgentDriver(agent->driver_type);
3616+
return driver.GetHandleFromVaddr(ptr, handle);
3617+
}
3618+
36083619
hsa_status_t Runtime::EnableLogging(uint8_t* flags, void* file) {
36093620
memcpy(log_flags, flags, sizeof(log_flags));
36103621

runtime/hsa-runtime/hsacore.so.def

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ global:
179179
hsa_amd_async_function;
180180
hsa_amd_image_get_info_max_dim;
181181
hsa_amd_queue_hw_ctx_config;
182+
hsa_amd_get_handle_from_vaddr;
182183
hsa_amd_queue_cu_set_mask;
183184
hsa_amd_queue_cu_get_mask;
184185
hsa_amd_memory_fill;

runtime/hsa-runtime/inc/hsa_api_trace.h

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ struct AmdExtTable {
205205
decltype(hsa_amd_async_function)* hsa_amd_async_function_fn;
206206
decltype(hsa_amd_signal_wait_any)* hsa_amd_signal_wait_any_fn;
207207
decltype(hsa_amd_queue_hw_ctx_config) *hsa_amd_queue_hw_ctx_config_fn;
208+
decltype(hsa_amd_get_handle_from_vaddr)* hsa_amd_get_handle_from_vaddr_fn;
208209
decltype(hsa_amd_queue_cu_set_mask)* hsa_amd_queue_cu_set_mask_fn;
209210
decltype(hsa_amd_memory_pool_get_info)* hsa_amd_memory_pool_get_info_fn;
210211
decltype(hsa_amd_agent_iterate_memory_pools)* hsa_amd_agent_iterate_memory_pools_fn;

runtime/hsa-runtime/inc/hsa_ext_amd.h

+3
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,9 @@ hsa_status_t HSA_API hsa_amd_queue_hw_ctx_config(
13751375
const hsa_queue_t *queue, hsa_amd_queue_hw_ctx_config_param_t config_type,
13761376
void *args);
13771377

1378+
1379+
hsa_status_t HSA_API hsa_amd_get_handle_from_vaddr(void* ptr, uint32_t* handle);
1380+
13781381
/**
13791382
* @brief Set a queue's CU affinity mask.
13801383
*

0 commit comments

Comments
 (0)