Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-virtgpu/virtgpu-shm.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-virtgpu / virtgpu-shm.cpp` 99 lines ยท 2741 bytes Raw Blame History 1 #include "virtgpu-shm.h" 2 3 #include "virtgpu.h" 4 #include "ggml-remoting.h" 5 6 #include 7 8 static uint32_t virtgpu_ioctl_resource_create_blob(virtgpu * gpu, 9 uint32_t blob_mem, 10 uint32_t blob_flags, 11 size_t blob_size, 12 uint64_t blob_id, 13 uint32_t * res_id) { 14 #ifdef SIMULATE_BO_SIZE_FIX 15 blob_size = align64(blob_size, 4096); 16 #endif 17 18 drm_virtgpu_resource_create_blob args = { 19 .blob_mem = blob_mem, 20 .blob_flags = blob_flags, 21 .bo_handle = 0, 22 .res_handle = 0, 23 .size = blob_size, 24 .pad = 0, 25 .cmd_size = 0, 26 .cmd = 0, 27 .blob_id = blob_id, 28 }; 29 30 if (virtgpu_ioctl(gpu, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &args)) { 31 return 0; 32 } 33 34 *res_id = args.res_handle; 35 return args.bo_handle; 36 } 37 38 static void virtgpu_ioctl_gem_close(virtgpu * gpu, uint32_t gem_handle) { 39 drm_gem_close args = { 40 .handle = gem_handle, 41 .pad = 0, 42 }; 43 44 const int ret = virtgpu_ioctl(gpu, DRM_IOCTL_GEM_CLOSE, &args); 45 assert(!ret); 46 #ifdef NDEBUG 47 UNUSED(ret); 48 #endif 49 } 50 51 static void * virtgpu_ioctl_map(virtgpu * gpu, uint32_t gem_handle, size_t size) { 52 drm_virtgpu_map args = { 53 .offset = 0, 54 .handle = gem_handle, 55 .pad = 0, 56 }; 57 58 if (virtgpu_ioctl(gpu, DRM_IOCTL_VIRTGPU_MAP, &args)) { 59 return NULL; 60 } 61 62 void * ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, gpu->fd, args.offset); 63 if (ptr == MAP_FAILED) { 64 return NULL; 65 } 66 67 return ptr; 68 } 69 70 void virtgpu_shmem_destroy(virtgpu * gpu, virtgpu_shmem * shmem) { 71 munmap(shmem->mmap_ptr, shmem->mmap_size); 72 virtgpu_ioctl_gem_close(gpu, shmem->gem_handle); 73 } 74 75 int virtgpu_shmem_create(virtgpu * gpu, size_t size, virtgpu_shmem * shmem) { 76 size = align64(size, 16384); 77 78 uint32_t res_id; 79 uint32_t gem_handle = virtgpu_ioctl_resource_create_blob(gpu, VIRTGPU_BLOB_MEM_HOST3D, 80 VIRTGPU_BLOB_FLAG_USE_MAPPABLE, size, 0, &res_id); 81 82 if (!gem_handle) { 83 return 1; 84 } 85 86 void * ptr = virtgpu_ioctl_map(gpu, gem_handle, size); 87 if (!ptr) { 88 virtgpu_ioctl_gem_close(gpu, gem_handle); 89 GGML_LOG_ERROR(GGML_VIRTGPU "%s: virtgpu_ioctl_map failed\n", __func__); 90 return 1; 91 } 92 93 shmem->res_id = res_id; 94 shmem->mmap_size = size; 95 shmem->mmap_ptr = ptr; 96 shmem->gem_handle = gem_handle; 97 98 return 0; 99 }