| 1 |
/* |
| 2 |
* Copyright 2022 Google |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <pthread.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include <cerrno> |
| 11 |
#include <cstring> |
| 12 |
#include <fstream> |
| 13 |
#include <string> |
| 14 |
|
| 15 |
#include "GfxStreamVulkanMapper.h" |
| 16 |
#include "VirtGpuKumquat.h" |
| 17 |
#include "util/log.h" |
| 18 |
|
| 19 |
#define PARAM(x) \ |
| 20 |
(struct VirtGpuParam) { x, #x, 0 } |
| 21 |
|
| 22 |
static inline uint32_t align_up(uint32_t n, uint32_t a) { return ((n + a - 1) / a) * a; } |
| 23 |
|
| 24 |
VirtGpuKumquatDevice::VirtGpuKumquatDevice(enum VirtGpuCapset capset, int32_t descriptor) |
| 25 |
: VirtGpuDevice(capset) { |
| 26 |
struct VirtGpuParam params[] = { |
| 27 |
PARAM(VIRTGPU_KUMQUAT_PARAM_3D_FEATURES), |
| 28 |
PARAM(VIRTGPU_KUMQUAT_PARAM_CAPSET_QUERY_FIX), |
| 29 |
PARAM(VIRTGPU_KUMQUAT_PARAM_RESOURCE_BLOB), |
| 30 |
PARAM(VIRTGPU_KUMQUAT_PARAM_HOST_VISIBLE), |
| 31 |
PARAM(VIRTGPU_KUMQUAT_PARAM_CROSS_DEVICE), |
| 32 |
PARAM(VIRTGPU_KUMQUAT_PARAM_CONTEXT_INIT), |
| 33 |
PARAM(VIRTGPU_KUMQUAT_PARAM_SUPPORTED_CAPSET_IDs), |
| 34 |
PARAM(VIRTGPU_KUMQUAT_PARAM_EXPLICIT_DEBUG_NAME), |
| 35 |
PARAM(VIRTGPU_KUMQUAT_PARAM_FENCE_PASSING), |
| 36 |
PARAM(VIRTGPU_KUMQUAT_PARAM_CREATE_GUEST_HANDLE), |
| 37 |
}; |
| 38 |
|
| 39 |
int ret; |
| 40 |
struct drm_kumquat_get_caps get_caps = {0}; |
| 41 |
struct drm_kumquat_context_init init = {0}; |
| 42 |
struct drm_kumquat_context_set_param ctx_set_params[3] = {{0}}; |
| 43 |
const char* processName = nullptr; |
| 44 |
std::string gpu_socket_path = "/tmp/kumquat-gpu-"; |
| 45 |
|
| 46 |
memset(&mCaps, 0, sizeof(struct VirtGpuCaps)); |
| 47 |
|
| 48 |
#if DETECT_OS_ANDROID |
| 49 |
processName = getprogname(); |
| 50 |
#endif |
| 51 |
|
| 52 |
if (descriptor >= 0) { |
| 53 |
gpu_socket_path.append(std::to_string(descriptor)); |
| 54 |
mDescriptor = descriptor; |
| 55 |
} else { |
| 56 |
gpu_socket_path.append("0"); |
| 57 |
} |
| 58 |
|
| 59 |
ret = virtgpu_kumquat_init(&mVirtGpu, gpu_socket_path.c_str()); |
| 60 |
if (ret) { |
| 61 |
mesa_logi("Failed to init virtgpu kumquat"); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
for (uint32_t i = 0; i < kParamMax; i++) { |
| 66 |
struct drm_kumquat_getparam get_param = {0}; |
| 67 |
get_param.param = params[i].param; |
| 68 |
|
| 69 |
ret = virtgpu_kumquat_get_param(mVirtGpu, &get_param); |
| 70 |
if (ret) { |
| 71 |
mesa_logi("virtgpu backend not enabling %s", params[i].name); |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
mCaps.params[i] = get_param.value; |
| 76 |
} |
| 77 |
|
| 78 |
get_caps.cap_set_id = static_cast<uint32_t>(capset); |
| 79 |
switch (capset) { |
| 80 |
case kCapsetGfxStreamVulkan: |
| 81 |
get_caps.size = sizeof(struct vulkanCapset); |
| 82 |
get_caps.addr = (unsigned long long)&mCaps.vulkanCapset; |
| 83 |
break; |
| 84 |
case kCapsetGfxStreamGles: |
| 85 |
get_caps.size = sizeof(struct glesCapset); |
| 86 |
get_caps.addr = (unsigned long long)&mCaps.glesCapset; |
| 87 |
break; |
| 88 |
case kCapsetGfxStreamComposer: |
| 89 |
get_caps.size = sizeof(struct composerCapset); |
| 90 |
get_caps.addr = (unsigned long long)&mCaps.composerCapset; |
| 91 |
break; |
| 92 |
default: |
| 93 |
get_caps.size = 0; |
| 94 |
} |
| 95 |
|
| 96 |
ret = virtgpu_kumquat_get_caps(mVirtGpu, &get_caps); |
| 97 |
if (ret) { |
| 98 |
// Don't fail get capabilities just yet, AEMU doesn't use this API |
| 99 |
// yet (b/272121235); |
| 100 |
mesa_loge("DRM_IOCTL_VIRTGPU_KUMQUAT_GET_CAPS failed with %s", strerror(errno)); |
| 101 |
} |
| 102 |
|
| 103 |
// We always need an ASG blob in some cases, so always define blobAlignment |
| 104 |
if (!mCaps.vulkanCapset.blobAlignment) { |
| 105 |
mCaps.vulkanCapset.blobAlignment = 4096; |
| 106 |
} |
| 107 |
|
| 108 |
ctx_set_params[0].param = VIRTGPU_KUMQUAT_CONTEXT_PARAM_NUM_RINGS; |
| 109 |
ctx_set_params[0].value = 2; |
| 110 |
init.num_params = 1; |
| 111 |
|
| 112 |
if (capset != kCapsetNone) { |
| 113 |
ctx_set_params[init.num_params].param = VIRTGPU_KUMQUAT_CONTEXT_PARAM_CAPSET_ID; |
| 114 |
ctx_set_params[init.num_params].value = static_cast<uint32_t>(capset); |
| 115 |
init.num_params++; |
| 116 |
} |
| 117 |
|
| 118 |
if (mCaps.params[kParamExplicitDebugName] && processName) { |
| 119 |
ctx_set_params[init.num_params].param = VIRTGPU_KUMQUAT_CONTEXT_PARAM_DEBUG_NAME; |
| 120 |
ctx_set_params[init.num_params].value = reinterpret_cast<uint64_t>(processName); |
| 121 |
init.num_params++; |
| 122 |
} |
| 123 |
|
| 124 |
init.ctx_set_params = (unsigned long long)&ctx_set_params[0]; |
| 125 |
ret = virtgpu_kumquat_context_init(mVirtGpu, &init); |
| 126 |
if (ret) { |
| 127 |
mesa_loge( |
| 128 |
"DRM_IOCTL_VIRTGPU_KUMQUAT_CONTEXT_INIT failed with %s, continuing without context...", |
| 129 |
strerror(errno)); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
VirtGpuKumquatDevice::~VirtGpuKumquatDevice() { virtgpu_kumquat_finish(&mVirtGpu); } |
| 134 |
|
| 135 |
struct VirtGpuCaps VirtGpuKumquatDevice::getCaps(void) { return mCaps; } |
| 136 |
|
| 137 |
int64_t VirtGpuKumquatDevice::getDeviceHandle(void) { return mDescriptor; } |
| 138 |
|
| 139 |
VirtGpuResourcePtr VirtGpuKumquatDevice::createResource(uint32_t width, uint32_t height, |
| 140 |
uint32_t stride, uint32_t size, |
| 141 |
uint32_t virglFormat, uint32_t target, |
| 142 |
uint32_t bind) { |
| 143 |
struct drm_kumquat_resource_create_3d create = { |
| 144 |
.target = target, |
| 145 |
.format = virglFormat, |
| 146 |
.bind = bind, |
| 147 |
.width = width, |
| 148 |
.height = height, |
| 149 |
.depth = 1U, |
| 150 |
.array_size = 1U, |
| 151 |
.last_level = 0, |
| 152 |
.nr_samples = 0, |
| 153 |
.size = size, |
| 154 |
.stride = stride, |
| 155 |
}; |
| 156 |
|
| 157 |
int ret = virtgpu_kumquat_resource_create_3d(mVirtGpu, &create); |
| 158 |
if (ret) { |
| 159 |
mesa_loge("DRM_IOCTL_VIRTGPU_KUMQUAT_RESOURCE_CREATE failed with %s", strerror(errno)); |
| 160 |
return nullptr; |
| 161 |
} |
| 162 |
|
| 163 |
return std::make_shared<VirtGpuKumquatResource>(mVirtGpu, create.bo_handle, create.res_handle, |
| 164 |
static_cast<uint64_t>(create.size)); |
| 165 |
} |
| 166 |
|
| 167 |
VirtGpuResourcePtr VirtGpuKumquatDevice::createBlob(const struct VirtGpuCreateBlob& blobCreate) { |
| 168 |
int ret; |
| 169 |
struct drm_kumquat_resource_create_blob create = {0}; |
| 170 |
|
| 171 |
create.size = blobCreate.size; |
| 172 |
create.blob_mem = blobCreate.blobMem; |
| 173 |
create.blob_flags = blobCreate.flags; |
| 174 |
create.blob_id = blobCreate.blobId; |
| 175 |
create.cmd = (uint64_t)(uintptr_t)blobCreate.blobCmd; |
| 176 |
create.cmd_size = blobCreate.blobCmdSize; |
| 177 |
|
| 178 |
ret = virtgpu_kumquat_resource_create_blob(mVirtGpu, &create); |
| 179 |
if (ret < 0) { |
| 180 |
mesa_loge("DRM_VIRTGPU_KUMQUAT_RESOURCE_CREATE_BLOB failed with %s", strerror(errno)); |
| 181 |
return nullptr; |
| 182 |
} |
| 183 |
|
| 184 |
return std::make_shared<VirtGpuKumquatResource>(mVirtGpu, create.bo_handle, create.res_handle, |
| 185 |
blobCreate.size); |
| 186 |
} |
| 187 |
|
| 188 |
VirtGpuResourcePtr VirtGpuKumquatDevice::importBlob(const struct VirtGpuExternalHandle& handle) { |
| 189 |
int ret; |
| 190 |
struct drm_kumquat_resource_import resource_import = {0}; |
| 191 |
|
| 192 |
resource_import.os_handle = static_cast<uint64_t>(handle.osHandle); |
| 193 |
resource_import.handle_type = static_cast<uint32_t>(handle.type); |
| 194 |
|
| 195 |
ret = virtgpu_kumquat_resource_import(mVirtGpu, &resource_import); |
| 196 |
if (ret < 0) { |
| 197 |
mesa_loge("DRM_VIRTGPU_KUMQUAT_RESOURCE_IMPORT failed with %s", strerror(errno)); |
| 198 |
return nullptr; |
| 199 |
} |
| 200 |
|
| 201 |
return std::make_shared<VirtGpuKumquatResource>( |
| 202 |
mVirtGpu, resource_import.bo_handle, resource_import.res_handle, resource_import.size); |
| 203 |
} |
| 204 |
|
| 205 |
int VirtGpuKumquatDevice::execBuffer(struct VirtGpuExecBuffer& execbuffer, |
| 206 |
const VirtGpuResource* blob) { |
| 207 |
int ret; |
| 208 |
struct drm_kumquat_execbuffer exec = {0}; |
| 209 |
uint32_t blobHandle; |
| 210 |
|
| 211 |
exec.flags = execbuffer.flags; |
| 212 |
exec.size = execbuffer.command_size; |
| 213 |
exec.ring_idx = execbuffer.ring_idx; |
| 214 |
exec.command = (uint64_t)(uintptr_t)(execbuffer.command); |
| 215 |
exec.fence_handle = -1; |
| 216 |
|
| 217 |
if (blob) { |
| 218 |
blobHandle = blob->getBlobHandle(); |
| 219 |
exec.bo_handles = (uint64_t)(uintptr_t)(&blobHandle); |
| 220 |
exec.num_bo_handles = 1; |
| 221 |
} |
| 222 |
|
| 223 |
ret = virtgpu_kumquat_execbuffer(mVirtGpu, &exec); |
| 224 |
if (ret) { |
| 225 |
mesa_loge("DRM_IOCTL_VIRTGPU_KUMQUAT_EXECBUFFER failed: %s", strerror(errno)); |
| 226 |
return ret; |
| 227 |
} |
| 228 |
|
| 229 |
if (execbuffer.flags & kFenceOut) { |
| 230 |
execbuffer.handle.osHandle = exec.fence_handle; |
| 231 |
execbuffer.handle.type = kFenceHandleSyncFd; |
| 232 |
} |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
VirtGpuDevice* kumquatCreateVirtGpuDevice(enum VirtGpuCapset capset, int32_t descriptor) { |
| 238 |
return new VirtGpuKumquatDevice(capset, descriptor); |
| 239 |
} |