Debian Codegraph @

src/gfxstream/guest/platform/drm/DrmVirtGpuBlob.cpp

raw
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 #include <xf86drm.h>
10
11 #include <cerrno>
12 #include <cstring>
13 #include <mutex>
14 #include <unordered_map>
15
16 #include "DrmVirtGpu.h"
17 #include "drm-uapi/virtgpu_drm.h"
18 #include "util/log.h"
19
20 // As per the warning in xf86drm.h, callers of drmPrimeFDToHandle are expected to perform
21 // reference counting on the underlying GEM handle that is returned. With Vulkan, for example,
22 // it is entirely possible that an FD, which points to the same underlying GEM handle, is both
23 // exported then imported across Vulkan objects. As the VirtGpuResource is stored as a
24 // shared_ptr with it's own ref-countng, the ref-counting for the underyling GEM has to be
25 // internal to this implementation. Otherwise, a GEM handle which is active in another Vulkan object
26 // in the same process, may be erroneously closed in the desctructor of one of the shared ptrs.
27 static std::mutex sDrmObjectRefMutex;
28 static std::unordered_map<uint32_t, int> sDrmObjectRefMap;
29
30 DrmVirtGpuResource::DrmVirtGpuResource(int64_t deviceHandle, uint32_t blobHandle,
31 uint32_t resourceHandle, uint64_t size)
32 : mDeviceHandle(deviceHandle),
33 mBlobHandle(blobHandle),
34 mResourceHandle(resourceHandle),
35 mSize(size) {
36 const std::lock_guard<std::mutex> lock(sDrmObjectRefMutex);
37 auto refMapIt = sDrmObjectRefMap.find(blobHandle);
38 if (refMapIt == sDrmObjectRefMap.end()) {
39 sDrmObjectRefMap[blobHandle] = 1;
40 } else {
41 refMapIt->second++;
42 }
43 }
44
45 DrmVirtGpuResource::~DrmVirtGpuResource() {
46 if (mBlobHandle == static_cast<uint32_t>(INVALID_DESCRIPTOR)) {
47 return;
48 }
49
50 const std::lock_guard<std::mutex> lock(sDrmObjectRefMutex);
51 auto refMapIt = sDrmObjectRefMap.find(mBlobHandle);
52 if (refMapIt == sDrmObjectRefMap.end()) {
53 mesa_logw(
54 "DrmVirtGpuResource::~DrmVirtGpuResource() could not find the blobHandle: %d in "
55 "internal map",
56 mBlobHandle);
57 return;
58 }
59
60 refMapIt->second--;
61 if (refMapIt->second <= 0) {
62 sDrmObjectRefMap.erase(refMapIt);
63
64 struct drm_gem_close gem_close {
65 .handle = mBlobHandle, .pad = 0,
66 };
67
68 int ret = drmIoctl(mDeviceHandle, DRM_IOCTL_GEM_CLOSE, &gem_close);
69 if (ret) {
70 mesa_loge("DRM_IOCTL_GEM_CLOSE failed with : [%s, blobHandle %u, resourceHandle: %u]",
71 strerror(errno), mBlobHandle, mResourceHandle);
72 }
73 }
74 }
75
76 void DrmVirtGpuResource::intoRaw() {
77 mBlobHandle = INVALID_DESCRIPTOR;
78 mResourceHandle = INVALID_DESCRIPTOR;
79 }
80
81 uint32_t DrmVirtGpuResource::getBlobHandle() const { return mBlobHandle; }
82
83 uint32_t DrmVirtGpuResource::getResourceHandle() const { return mResourceHandle; }
84
85 uint64_t DrmVirtGpuResource::getSize() const { return mSize; }
86
87 VirtGpuResourceMappingPtr DrmVirtGpuResource::createMapping() {
88 int ret;
89 struct drm_virtgpu_map map {
90 .handle = mBlobHandle, .pad = 0,
91 };
92
93 ret = drmIoctl(mDeviceHandle, DRM_IOCTL_VIRTGPU_MAP, &map);
94 if (ret) {
95 mesa_loge("DRM_IOCTL_VIRTGPU_MAP failed with %s", strerror(errno));
96 return nullptr;
97 }
98
99 uint8_t* ptr = static_cast<uint8_t*>(
100 mmap(nullptr, mSize, PROT_WRITE | PROT_READ, MAP_SHARED, mDeviceHandle, map.offset));
101
102 if (ptr == MAP_FAILED) {
103 mesa_loge("mmap64 failed with (%s)", strerror(errno));
104 return nullptr;
105 }
106
107 return std::make_shared<DrmVirtGpuResourceMapping>(shared_from_this(), ptr, mSize);
108 }
109
110 int DrmVirtGpuResource::exportBlob(struct VirtGpuExternalHandle& handle) {
111 int ret, fd;
112
113 uint32_t flags = DRM_CLOEXEC;
114 ret = drmPrimeHandleToFD(mDeviceHandle, mBlobHandle, flags, &fd);
115 if (ret) {
116 mesa_loge("drmPrimeHandleToFD failed with %s", strerror(errno));
117 return ret;
118 }
119
120 handle.osHandle = static_cast<int64_t>(fd);
121 handle.type = kMemHandleDmabuf;
122 return 0;
123 }
124
125 int DrmVirtGpuResource::wait() {
126 int ret;
127 struct drm_virtgpu_3d_wait wait_3d = {0};
128
129 int retry = 0;
130 do {
131 if (retry > 0 && (retry % 10 == 0)) {
132 mesa_loge("DRM_IOCTL_VIRTGPU_WAIT failed with EBUSY for %d times.", retry);
133 }
134 wait_3d.handle = mBlobHandle;
135 ret = drmIoctl(mDeviceHandle, DRM_IOCTL_VIRTGPU_WAIT, &wait_3d);
136 ++retry;
137 } while (ret < 0 && errno == EBUSY);
138
139 if (ret < 0) {
140 mesa_loge("DRM_IOCTL_VIRTGPU_WAIT failed with %s", strerror(errno));
141 return ret;
142 }
143
144 return 0;
145 }
146
147 int DrmVirtGpuResource::transferToHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
148 int ret;
149 struct drm_virtgpu_3d_transfer_to_host xfer = {0};
150
151 xfer.box.x = x;
152 xfer.box.y = y;
153 xfer.box.w = w;
154 xfer.box.h = h;
155 xfer.box.d = 1;
156 xfer.bo_handle = mBlobHandle;
157
158 ret = drmIoctl(mDeviceHandle, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
159 if (ret < 0) {
160 mesa_loge("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s", strerror(errno));
161 return ret;
162 }
163
164 return 0;
165 }
166
167 int DrmVirtGpuResource::transferFromHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
168 int ret;
169 struct drm_virtgpu_3d_transfer_from_host xfer = {0};
170
171 xfer.box.x = x;
172 xfer.box.y = y;
173 xfer.box.w = w;
174 xfer.box.h = h;
175 xfer.box.d = 1;
176 xfer.bo_handle = mBlobHandle;
177
178 ret = drmIoctl(mDeviceHandle, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
179 if (ret < 0) {
180 mesa_loge("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s", strerror(errno));
181 return ret;
182 }
183
184 return 0;
185 }