| 1 |
/* |
| 2 |
* Copyright 2023 Google |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "VirtioGpuAddressSpaceStream.h" |
| 7 |
|
| 8 |
#include <errno.h> |
| 9 |
|
| 10 |
#include "util/log.h" |
| 11 |
#include "util/os_misc.h" |
| 12 |
|
| 13 |
static bool GetRingParamsFromCapset(enum VirtGpuCapset capset, const VirtGpuCaps& caps, |
| 14 |
uint32_t& ringSize, uint32_t& bufferSize, |
| 15 |
uint32_t& blobAlignment) { |
| 16 |
uint64_t guest_page_size = 4096; |
| 17 |
os_get_page_size(&guest_page_size); |
| 18 |
switch (capset) { |
| 19 |
case kCapsetGfxStreamVulkan: |
| 20 |
ringSize = caps.vulkanCapset.ringSize; |
| 21 |
bufferSize = caps.vulkanCapset.bufferSize; |
| 22 |
blobAlignment = caps.vulkanCapset.blobAlignment; |
| 23 |
break; |
| 24 |
case kCapsetGfxStreamGles: |
| 25 |
ringSize = caps.glesCapset.ringSize; |
| 26 |
bufferSize = caps.glesCapset.bufferSize; |
| 27 |
blobAlignment = caps.glesCapset.blobAlignment; |
| 28 |
break; |
| 29 |
case kCapsetGfxStreamComposer: |
| 30 |
ringSize = caps.composerCapset.ringSize; |
| 31 |
bufferSize = caps.composerCapset.bufferSize; |
| 32 |
blobAlignment = caps.composerCapset.blobAlignment; |
| 33 |
break; |
| 34 |
default: |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
blobAlignment = std::max(blobAlignment, (uint32_t)guest_page_size); |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
static address_space_handle_t virtgpu_address_space_open() { |
| 43 |
return (address_space_handle_t)(-EINVAL); |
| 44 |
} |
| 45 |
|
| 46 |
static void virtgpu_address_space_close(address_space_handle_t) { |
| 47 |
// Handle opened by VirtioGpuDevice wrapper |
| 48 |
} |
| 49 |
|
| 50 |
static bool virtgpu_address_space_ping(address_space_handle_t, struct address_space_ping* info) { |
| 51 |
int ret; |
| 52 |
struct VirtGpuExecBuffer exec = {}; |
| 53 |
VirtGpuDevice* instance = VirtGpuDevice::getInstance(); |
| 54 |
struct gfxstreamContextPing ping = {}; |
| 55 |
|
| 56 |
ping.hdr.opCode = GFXSTREAM_CONTEXT_PING; |
| 57 |
ping.resourceId = info->resourceId; |
| 58 |
|
| 59 |
exec.command = static_cast<void*>(&ping); |
| 60 |
exec.command_size = sizeof(ping); |
| 61 |
|
| 62 |
ret = instance->execBuffer(exec, nullptr); |
| 63 |
if (ret) |
| 64 |
return false; |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
AddressSpaceStream* createVirtioGpuAddressSpaceStream(enum VirtGpuCapset capset) { |
| 70 |
VirtGpuResourcePtr pipe, blob; |
| 71 |
VirtGpuResourceMappingPtr pipeMapping, blobMapping; |
| 72 |
struct VirtGpuExecBuffer exec = {}; |
| 73 |
struct VirtGpuCreateBlob blobCreate = {}; |
| 74 |
struct gfxstreamContextCreate contextCreate = {}; |
| 75 |
|
| 76 |
uint32_t ringSize = 0; |
| 77 |
uint32_t bufferSize = 0; |
| 78 |
uint32_t blobAlignment = 0; |
| 79 |
|
| 80 |
char* blobAddr, *bufferPtr; |
| 81 |
int ret; |
| 82 |
|
| 83 |
VirtGpuDevice* instance = VirtGpuDevice::getInstance(); |
| 84 |
auto caps = instance->getCaps(); |
| 85 |
|
| 86 |
if (!caps.params[kParamResourceBlob]) { |
| 87 |
mesa_loge("VirtGpuDevice does not support blob-resources!"); |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
if (!caps.params[kParamHostVisible] && !caps.params[kParamCreateGuestHandle]) { |
| 91 |
mesa_loge("VirtGpuDevice must support at least one of: 1) host-visible memory (kParamHostVisible) or 2) host handles created from guest memory (kParamCreateGuestHandle)."); |
| 92 |
return nullptr; |
| 93 |
} |
| 94 |
|
| 95 |
if (!GetRingParamsFromCapset(capset, caps, ringSize, bufferSize, blobAlignment)) { |
| 96 |
mesa_loge("Failed to get ring parameters"); |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
blobCreate.blobId = 0; |
| 101 |
blobCreate.blobMem = kBlobMemHost3d; |
| 102 |
blobCreate.flags = kBlobFlagMappable; |
| 103 |
blobCreate.size = ALIGN_POT(ringSize + bufferSize, blobAlignment); |
| 104 |
blob = instance->createBlob(blobCreate); |
| 105 |
if (!blob) |
| 106 |
return nullptr; |
| 107 |
|
| 108 |
// Context creation command |
| 109 |
contextCreate.hdr.opCode = GFXSTREAM_CONTEXT_CREATE; |
| 110 |
contextCreate.resourceId = blob->getResourceHandle(); |
| 111 |
|
| 112 |
exec.command = static_cast<void*>(&contextCreate); |
| 113 |
exec.command_size = sizeof(contextCreate); |
| 114 |
|
| 115 |
ret = instance->execBuffer(exec, blob.get()); |
| 116 |
if (ret) |
| 117 |
return nullptr; |
| 118 |
|
| 119 |
// Wait occurs on global timeline -- should we use context specific one? |
| 120 |
ret = blob->wait(); |
| 121 |
if (ret) |
| 122 |
return nullptr; |
| 123 |
|
| 124 |
blobMapping = blob->createMapping(); |
| 125 |
if (!blobMapping) |
| 126 |
return nullptr; |
| 127 |
|
| 128 |
blobAddr = reinterpret_cast<char*>(blobMapping->asRawPtr()); |
| 129 |
|
| 130 |
bufferPtr = blobAddr + sizeof(struct asg_ring_storage); |
| 131 |
struct asg_context context = asg_context_create(blobAddr, bufferPtr, bufferSize); |
| 132 |
|
| 133 |
context.ring_config->transfer_mode = 1; |
| 134 |
context.ring_config->host_consumed_pos = 0; |
| 135 |
context.ring_config->guest_write_pos = 0; |
| 136 |
|
| 137 |
struct address_space_ops ops = { |
| 138 |
.open = virtgpu_address_space_open, |
| 139 |
.close = virtgpu_address_space_close, |
| 140 |
.ping = virtgpu_address_space_ping, |
| 141 |
}; |
| 142 |
|
| 143 |
AddressSpaceStream* res = |
| 144 |
new AddressSpaceStream((address_space_handle_t)(-1), 1, context, 0, 0, ops); |
| 145 |
|
| 146 |
res->setMapping(blobMapping); |
| 147 |
res->setResourceId(contextCreate.resourceId); |
| 148 |
return res; |
| 149 |
} |