Files
rocm-systems/projects/rocr-runtime/libhsakmt/src/virtio/hsakmt_virtio_openclose.c
T
Honglei Huang 5e68bd163a libhsakmt/virtio: add virtio support for libhsakmt
This patch adds VirtIO support to the libhsakmt library, enabling communication
 with AMD GPUs via VirtIO.

Details
- CMakeLists.txt: Added a new CMakeLists.txt file for the VirtIO component
of libhsakmt.
- hsakmt_virtio.c/h: Implemented the core VirtIO functionality, including
VirtIO GPU device initialization, command execution, and memory management.
- virtio_gpu.c/h: Contains the implementation of the VirtIO GPU device,
including ioctl handling, shared memory management, and command execution.
- hsakmt_virtio_events.c: Implements event handling for VirtIO, such as event
creation, destruction, setting, resetting, and querying event states.
- hsakmt_virtio_memory.c: Manages memory operations for VirtIO, including memory
allocation, freeing, mapping, and unmapping.
- hsakmt_virtio_queues.c: Implements queue management for VirtIO, including
queue creation, destruction, and updating.
- hsakmt_virtio_topology.c: Handles system and node properties for VirtIO.
- hsakmt_virtio_vm.c: Manages VM-related operations for VirtIO, such as
reserving and dereserving VA space.
- include/linux/virtgpu_drm.h: Contains DRM definitions for VirtIO GPU.

Key Features
- VirtIO GPU Initialization: The library can now initialize a VirtIO GPU device
and communicate with it.
- Command Execution: Supports executing commands on the VirtIO GPU device.
- Memory Management: Provides functions for allocating, freeing, mapping, and
unmapping memory for VirtIO operations.
- Event Handling: Implements a comprehensive event system for VirtIO.
- Queue Management: Allows for creating, destroying, and updating queues
on the VirtIO GPU device.
- System and Node Properties: Retrieves and manages system and node
properties for VirtIO.

Signed-off-by: Honglei Huang <Honglei1.Huang@amd.com>


[ROCm/ROCR-Runtime commit: 48d3719dba]
2025-07-24 23:20:36 +08:00

133 lines
4.4 KiB
C

/*
* Copyright 2025 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
*/
#include "hsakmt/hsakmt_virtio.h"
#include "hsakmt_virtio_device.h"
pthread_mutex_t dev_mutex = PTHREAD_MUTEX_INITIALIZER;
vhsakmt_device_handle dev_list = NULL;
vhsakmt_device_handle vhsakmt_dev(void) { return dev_list; }
static HSAKMT_STATUS vhsakmt_openKFD_cmd(vhsakmt_device_handle dev) {
void* vm_start = vhsakmt_vm_start();
if (!vm_start) return -HSAKMT_STATUS_NO_MEMORY;
struct vhsakmt_ccmd_query_info_rsp* rsp;
struct vhsakmt_ccmd_query_info_req req = {
.hdr = VHSAKMT_CCMD(QUERY_INFO, sizeof(struct vhsakmt_ccmd_query_info_req)),
.type = VHSAKMT_CCMD_QUERY_OPEN_KFD,
.open_kfd_args =
{
.cur_vm_start = VHSA_VPTR_TO_UINT64(vm_start),
},
};
if (!req.open_kfd_args.cur_vm_start) {
vhsa_err("%s: failed to get current heap start address\n", __FUNCTION__);
return -HSAKMT_STATUS_ERROR;
}
rsp = vhsakmt_alloc_rsp(dev, &req.hdr, sizeof(struct vhsakmt_ccmd_query_info_rsp));
if (!rsp) return -HSAKMT_STATUS_NO_MEMORY;
vhsakmt_execbuf_cpu(dev, &req.hdr, __FUNCTION__);
if (!rsp->open_kfd_rsp.vm_start || !rsp->open_kfd_rsp.vm_size) {
vhsa_err("%s: failed to get KFD VM area\n", __FUNCTION__);
return -HSAKMT_STATUS_ERROR;
}
vhsakmt_set_vm_area(dev, rsp->open_kfd_rsp.vm_start, rsp->open_kfd_rsp.vm_size);
if (vhsakmt_reserve_va(dev->vm_start, dev->vm_size)) {
vhsa_err("%s: failed to reserve VM area: [%lx-%lx]-0x%lx\n", __FUNCTION__, dev->vm_start,
dev->vm_start + dev->vm_size, dev->vm_size);
return -HSAKMT_STATUS_NO_MEMORY;
}
vhsa_debug("%s: kfd vm range: [%lx-%lx]-0x%lx\n", __FUNCTION__, dev->vm_start,
dev->vm_start + dev->vm_size, dev->vm_size);
return rsp->ret;
}
static vhsakmt_device_handle vhsakmt_device_init(void) {
int fd;
vhsakmt_device_handle dev = NULL;
if (vhsakmt_dev()) return vhsakmt_dev();
pthread_mutex_lock(&dev_mutex);
fd = virtio_gpu_kfd_open();
if (fd < 0) goto open_failed;
dev = calloc(1, sizeof(struct vhsakmt_device));
if (!dev) goto open_failed;
dev->vgdev = virtio_gpu_init(fd, 0);
if (!dev->vgdev) goto malloc_failed;
rbtree_init(&dev->bo_rbt);
atomic_store(&dev->next_blob_id, 1);
atomic_store(&dev->refcount, 1);
pthread_mutex_init(&dev->bo_handles_mutex, NULL);
pthread_mutex_init(&dev->vhsakmt_mutex, NULL);
dev_list = dev;
pthread_mutex_unlock(&dev_mutex);
return dev;
malloc_failed:
free(dev);
dev = NULL;
open_failed:
pthread_mutex_unlock(&dev_mutex);
return dev;
}
HSAKMT_STATUS HSAKMTAPI vhsaKmtOpenKFD(void) {
vhsakmt_device_handle dev;
char* d = getenv("VHSAKMT_DEBUG_LEVEL");
if (d) vhsakmt_debug_level = atoi(d);
dev = vhsakmt_device_init();
if (!dev) return HSAKMT_STATUS_ERROR;
return vhsakmt_openKFD_cmd(vhsakmt_dev());
}
static void vhsakmt_device_destroy(struct vhsakmt_device* dev) {
pthread_mutex_destroy(&dev->bo_handles_mutex);
vhsakmt_dereserve_va(dev->vm_start, dev->vm_size);
if (dev->sys_props) free(dev->sys_props);
if (dev->vhsakmt_nodes) free(dev->vhsakmt_nodes);
virtio_gpu_close(dev->vgdev);
}
HSAKMT_STATUS HSAKMTAPI vhsaKmtCloseKFD(void) {
vhsakmt_device_handle dev = vhsakmt_dev();
pthread_mutex_lock(&dev_mutex);
if (vhsakmt_atomic_dec_return(&dev->refcount) <= 0) vhsakmt_device_destroy(dev);
pthread_mutex_unlock(&dev_mutex);
return 0;
}