Skip to content

Commit 5febf22

Browse files
committed
virtgpu: add the virtgpu_submit to kick a command on the host
1 parent a25b672 commit 5febf22

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

ggml/src/ggml-remotingfrontend/virtgpu-utils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <cstdint>
44
#include <cassert>
55
#include <cstddef>
6+
#include <ctime>
7+
#include <cerrno>
8+
#include <atomic>
69

710
#define unlikely(x) __builtin_expect(!!(x), 0)
811
#define likely(x) __builtin_expect(!!(x), 1)
@@ -48,3 +51,12 @@ struct util_sparse_array {
4851
void *util_sparse_array_get(struct util_sparse_array *arr, uint64_t idx);
4952
void util_sparse_array_init(struct util_sparse_array *arr,
5053
size_t elem_size, size_t node_size);
54+
55+
inline void
56+
os_time_sleep(int64_t usecs)
57+
{
58+
struct timespec time;
59+
time.tv_sec = usecs / 1000000;
60+
time.tv_nsec = (usecs % 1000000) * 1000;
61+
while (clock_nanosleep(CLOCK_MONOTONIC, 0, &time, &time) == EINTR);
62+
}

ggml/src/ggml-remotingfrontend/virtgpu.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ create_virtgpu() {
7272
INFO("Created shm at %p", shmem);
7373
}
7474

75+
virtgpu_submit(gpu, shmem);
76+
7577
breakpoint();
7678
}
7779

@@ -347,3 +349,87 @@ virtgpu_ioctl_getparam(struct virtgpu *gpu, uint64_t param)
347349
const int ret = virtgpu_ioctl(gpu, DRM_IOCTL_VIRTGPU_GETPARAM, &args);
348350
return ret ? 0 : val;
349351
}
352+
353+
354+
355+
#define PK_COMMAND_TYPE_pkCreateThread 255
356+
357+
static int virtgpu_submit(struct virtgpu *gpu, struct vn_renderer_shmem *shmem)
358+
{
359+
360+
/*
361+
* Data passed to the host
362+
*/
363+
int32_t command[3];
364+
// command identifier
365+
command[0] = PK_COMMAND_TYPE_pkCreateThread;
366+
command[1] = 0; // ?
367+
// arguments
368+
command[2] = shmem->res_id;
369+
370+
/*
371+
* Reply notification pointer
372+
*/
373+
374+
volatile std::atomic_uint *atomic_reply_notif = (volatile std::atomic_uint *) shmem->mmap_ptr;
375+
*atomic_reply_notif = 0;
376+
377+
/*
378+
* Trigger the execbuf ioctl
379+
*/
380+
381+
struct drm_virtgpu_execbuffer args = {
382+
.flags = VIRTGPU_EXECBUF_RING_IDX,
383+
.size = sizeof(command),
384+
.command = (uintptr_t) &command,
385+
386+
.bo_handles = 0,
387+
.num_bo_handles = 0,
388+
389+
.fence_fd = 0,
390+
.ring_idx = 0,
391+
.syncobj_stride = 0,
392+
.num_in_syncobjs = 0,
393+
.num_out_syncobjs = 0,
394+
.in_syncobjs = 0,
395+
.out_syncobjs = 0,
396+
};
397+
398+
int ret = drmIoctl(gpu->fd, DRM_IOCTL_VIRTGPU_EXECBUFFER, &args);
399+
400+
/*
401+
* Wait for the response notification
402+
*/
403+
404+
int resp = std::atomic_load_explicit(atomic_reply_notif, std::memory_order_acquire);
405+
printf("waiting for the response ... | %d | %p\n", resp, (void*) atomic_reply_notif);
406+
407+
while (std::atomic_load_explicit(atomic_reply_notif, std::memory_order_acquire) == 0) {
408+
int64_t base_sleep_us = 160;
409+
410+
os_time_sleep(base_sleep_us);
411+
}
412+
printf("got the response!\n");
413+
/*
414+
* Read the reply
415+
*/
416+
417+
printf("virtgpu_submit() --> 0x%x\n", ((uint32_t *)shmem->mmap_ptr)[1]);
418+
printf("virtgpu_submit() --> 0x%x\n", ((uint32_t *)shmem->mmap_ptr)[2]);
419+
printf("virtgpu_submit() --> 0x%x\n", ((uint32_t *)shmem->mmap_ptr)[3]);
420+
421+
#if 0
422+
VkCommandTypeEXT command_type;
423+
vn_decode_VkCommandTypeEXT(dec, &command_type);
424+
assert(command_type == VK_COMMAND_TYPE_vkEnumerateInstanceVersion_EXT);
425+
VkResult ret;
426+
vn_decode_VkResult(dec, &ret);
427+
if (vn_decode_simple_pointer(dec)) {
428+
vn_decode_uint32_t(dec, pApiVersion);
429+
} else {
430+
pApiVersion = NULL;
431+
}
432+
#endif
433+
434+
return ret;
435+
}

ggml/src/ggml-remotingfrontend/virtgpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,4 @@ virtgpu_ioctl_get_caps(struct virtgpu *gpu,
175175
size_t capset_size);
176176
static uint64_t virtgpu_ioctl_getparam(struct virtgpu *gpu, uint64_t param);
177177
static void virtgpu_init_renderer_info(struct virtgpu *gpu);
178+
static int virtgpu_submit(struct virtgpu *gpu, struct vn_renderer_shmem *shmem);

0 commit comments

Comments
 (0)