1 /*
2 * Copyright 2022 Google
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <fcntl.h>
7 #include <sys/mman.h>
8 #include <unistd.h>
9
10 #include <cerrno>
11 #include <cstring>
12
13 #include "VirtGpuKumquat.h"
14 #include "util/log.h"
15
VirtGpuKumquatResource(struct virtgpu_kumquat * virtGpu,uint32_t blobHandle,uint32_t resourceHandle,uint64_t size)16 VirtGpuKumquatResource::VirtGpuKumquatResource(struct virtgpu_kumquat* virtGpu, uint32_t blobHandle,
17 uint32_t resourceHandle, uint64_t size)
18 : mVirtGpu(virtGpu), mBlobHandle(blobHandle), mResourceHandle(resourceHandle), mSize(size) {}
19
~VirtGpuKumquatResource()20 VirtGpuKumquatResource::~VirtGpuKumquatResource() {
21 struct drm_kumquat_resource_unref unref {
22 .bo_handle = mBlobHandle, .pad = 0,
23 };
24
25 int ret = virtgpu_kumquat_resource_unref(mVirtGpu, &unref);
26 if (ret) {
27 mesa_loge("Closed failed with : [%s, blobHandle %u, resourceHandle: %u]", strerror(errno),
28 mBlobHandle, mResourceHandle);
29 }
30 }
31
getBlobHandle() const32 uint32_t VirtGpuKumquatResource::getBlobHandle() const { return mBlobHandle; }
33
getResourceHandle() const34 uint32_t VirtGpuKumquatResource::getResourceHandle() const { return mResourceHandle; }
35
getSize() const36 uint64_t VirtGpuKumquatResource::getSize() const { return mSize; }
37
createMapping()38 VirtGpuResourceMappingPtr VirtGpuKumquatResource::createMapping() {
39 int ret;
40 struct drm_kumquat_map map {
41 .bo_handle = mBlobHandle, .ptr = NULL, .size = mSize,
42 };
43
44 ret = virtgpu_kumquat_resource_map(mVirtGpu, &map);
45 if (ret < 0) {
46 mesa_loge("Mapping failed with %s for resource %u blob %u", strerror(errno),
47 mResourceHandle, mBlobHandle);
48 return nullptr;
49 }
50
51 return std::make_shared<VirtGpuKumquatResourceMapping>(shared_from_this(), mVirtGpu,
52 (uint8_t*)map.ptr, mSize);
53 }
54
exportBlob(struct VirtGpuExternalHandle & handle)55 int VirtGpuKumquatResource::exportBlob(struct VirtGpuExternalHandle& handle) {
56 int ret;
57 struct drm_kumquat_resource_export exp = {0};
58
59 exp.bo_handle = mBlobHandle;
60
61 ret = virtgpu_kumquat_resource_export(mVirtGpu, &exp);
62 if (ret) {
63 mesa_loge("Failed to export blob with %s", strerror(errno));
64 return ret;
65 }
66
67 handle.osHandle = static_cast<int64_t>(exp.os_handle);
68 handle.type = static_cast<VirtGpuHandleType>(exp.handle_type);
69 return 0;
70 }
71
wait()72 int VirtGpuKumquatResource::wait() {
73 int ret;
74 struct drm_kumquat_wait wait = {
75 .handle = mBlobHandle,
76 .flags = 0,
77 };
78
79 ret = virtgpu_kumquat_wait(mVirtGpu, &wait);
80 if (ret < 0) {
81 mesa_loge("Wait failed with %s", strerror(errno));
82 return ret;
83 }
84
85 return 0;
86 }
87
transferToHost(uint32_t x,uint32_t y,uint32_t w,uint32_t h)88 int VirtGpuKumquatResource::transferToHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
89 int ret;
90 struct drm_kumquat_transfer_to_host xfer = {0};
91
92 xfer.box.x = x;
93 xfer.box.y = y;
94 xfer.box.w = w;
95 xfer.box.h = h;
96 xfer.box.d = 1;
97 xfer.bo_handle = mBlobHandle;
98
99 ret = virtgpu_kumquat_transfer_to_host(mVirtGpu, &xfer);
100 if (ret < 0) {
101 mesa_loge("Transfer to host failed with %s", strerror(errno));
102 return ret;
103 }
104
105 return 0;
106 }
107
transferFromHost(uint32_t x,uint32_t y,uint32_t w,uint32_t h)108 int VirtGpuKumquatResource::transferFromHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
109 int ret;
110 struct drm_kumquat_transfer_from_host xfer = {0};
111
112 xfer.box.x = x;
113 xfer.box.y = y;
114 xfer.box.w = w;
115 xfer.box.h = h;
116 xfer.box.d = 1;
117 xfer.bo_handle = mBlobHandle;
118
119 ret = virtgpu_kumquat_transfer_from_host(mVirtGpu, &xfer);
120 if (ret < 0) {
121 mesa_loge("Transfer from host failed with %s", strerror(errno));
122 return ret;
123 }
124
125 return 0;
126 }
127