| 1 |
/* |
| 2 |
* Copyright 2014, 2015 Red Hat. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* on the rights to use, copy, modify, merge, publish, distribute, sub |
| 8 |
* license, and/or sell copies of the Software, and to permit persons to whom |
| 9 |
* the Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 19 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 20 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 21 |
* USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <limits.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <sys/ioctl.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
|
| 31 |
#include "util/os_mman.h" |
| 32 |
#include "util/os_file.h" |
| 33 |
#include "util/os_time.h" |
| 34 |
#include "util/simple_mtx.h" |
| 35 |
#include "util/u_memory.h" |
| 36 |
#include "util/format/u_format.h" |
| 37 |
#include "util/u_hash_table.h" |
| 38 |
#include "util/u_inlines.h" |
| 39 |
#include "util/u_pointer.h" |
| 40 |
#include "frontend/drm_driver.h" |
| 41 |
#include "virgl/virgl_screen.h" |
| 42 |
#include "virgl/virgl_public.h" |
| 43 |
#include "virtio-gpu/virgl_protocol.h" |
| 44 |
|
| 45 |
#include <xf86drm.h> |
| 46 |
#include <libsync.h> |
| 47 |
#include "drm-uapi/virtgpu_drm.h" |
| 48 |
|
| 49 |
#include "virgl_drm_winsys.h" |
| 50 |
#include "virgl_drm_public.h" |
| 51 |
|
| 52 |
#define VIRGL_DRM_VERSION(major, minor) ((major) << 16 | (minor)) |
| 53 |
#define VIRGL_DRM_VERSION_FENCE_FD VIRGL_DRM_VERSION(0, 1) |
| 54 |
|
| 55 |
/* Gets a pointer to the virgl_hw_res containing the pointed to cache entry. */ |
| 56 |
#define cache_entry_container_res(ptr) \ |
| 57 |
(struct virgl_hw_res*)((char*)ptr - offsetof(struct virgl_hw_res, cache_entry)) |
| 58 |
|
| 59 |
static inline bool can_cache_resource(uint32_t bind) |
| 60 |
{ |
| 61 |
return bind == VIRGL_BIND_CONSTANT_BUFFER || |
| 62 |
bind == VIRGL_BIND_INDEX_BUFFER || |
| 63 |
bind == VIRGL_BIND_VERTEX_BUFFER || |
| 64 |
bind == VIRGL_BIND_CUSTOM || |
| 65 |
bind == VIRGL_BIND_STAGING || |
| 66 |
bind == VIRGL_BIND_DEPTH_STENCIL || |
| 67 |
bind == VIRGL_BIND_RENDER_TARGET || |
| 68 |
bind == 0; |
| 69 |
} |
| 70 |
|
| 71 |
static void virgl_hw_res_destroy(struct virgl_drm_winsys *qdws, |
| 72 |
struct virgl_hw_res *res) |
| 73 |
{ |
| 74 |
mtx_lock(&qdws->bo_handles_mutex); |
| 75 |
|
| 76 |
/* We intentionally avoid taking the lock in |
| 77 |
* virgl_drm_resource_reference. Now that the |
| 78 |
* lock is taken, we need to check the refcount |
| 79 |
* again. */ |
| 80 |
if (pipe_is_referenced(&res->reference)) { |
| 81 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if (--res->needed_references > 0) { |
| 86 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
_mesa_hash_table_remove_key(qdws->bo_handles, |
| 91 |
(void *)(uintptr_t)res->bo_handle); |
| 92 |
if (res->flink_name) |
| 93 |
_mesa_hash_table_remove_key(qdws->bo_names, |
| 94 |
(void *)(uintptr_t)res->flink_name); |
| 95 |
if (res->ptr) |
| 96 |
os_munmap(res->ptr, res->size); |
| 97 |
|
| 98 |
drmCloseBufferHandle(qdws->fd, res->bo_handle); |
| 99 |
|
| 100 |
/* We need to unlock the access to bo_handles after closing the GEM to |
| 101 |
* avoid a race condition where another thread would not find the |
| 102 |
* bo_handle leading to a call of DRM_IOCTL_GEM_OPEN which will return |
| 103 |
* the same bo_handle as the one we are closing here. */ |
| 104 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 105 |
FREE(res); |
| 106 |
} |
| 107 |
|
| 108 |
static bool virgl_drm_resource_is_busy(struct virgl_winsys *vws, |
| 109 |
struct virgl_hw_res *res) |
| 110 |
{ |
| 111 |
struct virgl_drm_winsys *vdws = virgl_drm_winsys(vws); |
| 112 |
struct drm_virtgpu_3d_wait waitcmd; |
| 113 |
int ret; |
| 114 |
|
| 115 |
if (!p_atomic_read(&res->maybe_busy) && !p_atomic_read(&res->external)) |
| 116 |
return false; |
| 117 |
|
| 118 |
memset(&waitcmd, 0, sizeof(waitcmd)); |
| 119 |
waitcmd.handle = res->bo_handle; |
| 120 |
waitcmd.flags = VIRTGPU_WAIT_NOWAIT; |
| 121 |
|
| 122 |
ret = drmIoctl(vdws->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd); |
| 123 |
if (ret && errno == EBUSY) |
| 124 |
return true; |
| 125 |
|
| 126 |
p_atomic_set(&res->maybe_busy, false); |
| 127 |
|
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
virgl_drm_winsys_destroy(struct virgl_winsys *qws) |
| 133 |
{ |
| 134 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 135 |
|
| 136 |
virgl_resource_cache_flush(&qdws->cache); |
| 137 |
|
| 138 |
_mesa_hash_table_destroy(qdws->bo_handles, NULL); |
| 139 |
_mesa_hash_table_destroy(qdws->bo_names, NULL); |
| 140 |
mtx_destroy(&qdws->bo_handles_mutex); |
| 141 |
mtx_destroy(&qdws->mutex); |
| 142 |
|
| 143 |
FREE(qdws); |
| 144 |
} |
| 145 |
|
| 146 |
static void virgl_drm_resource_reference(struct virgl_winsys *qws, |
| 147 |
struct virgl_hw_res **dres, |
| 148 |
struct virgl_hw_res *sres) |
| 149 |
{ |
| 150 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 151 |
struct virgl_hw_res *old = *dres; |
| 152 |
|
| 153 |
if (pipe_reference(&(*dres)->reference, &sres->reference)) { |
| 154 |
|
| 155 |
if (!can_cache_resource(old->bind) || |
| 156 |
p_atomic_read(&old->external)) { |
| 157 |
virgl_hw_res_destroy(qdws, old); |
| 158 |
} else { |
| 159 |
mtx_lock(&qdws->mutex); |
| 160 |
virgl_resource_cache_add(&qdws->cache, &old->cache_entry); |
| 161 |
mtx_unlock(&qdws->mutex); |
| 162 |
} |
| 163 |
} |
| 164 |
*dres = sres; |
| 165 |
} |
| 166 |
|
| 167 |
static struct virgl_hw_res * |
| 168 |
virgl_drm_winsys_resource_create_blob(struct virgl_winsys *qws, |
| 169 |
enum pipe_texture_target target, |
| 170 |
uint32_t format, |
| 171 |
uint32_t bind, |
| 172 |
uint32_t width, |
| 173 |
uint32_t height, |
| 174 |
uint32_t depth, |
| 175 |
uint32_t array_size, |
| 176 |
uint32_t last_level, |
| 177 |
uint32_t nr_samples, |
| 178 |
uint32_t flags, |
| 179 |
uint32_t size) |
| 180 |
{ |
| 181 |
int ret; |
| 182 |
int32_t blob_id; |
| 183 |
uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 }; |
| 184 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 185 |
struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 }; |
| 186 |
struct virgl_hw_res *res; |
| 187 |
struct virgl_resource_params params = { .size = size, |
| 188 |
.bind = bind, |
| 189 |
.format = format, |
| 190 |
.flags = flags, |
| 191 |
.nr_samples = nr_samples, |
| 192 |
.width = width, |
| 193 |
.height = height, |
| 194 |
.depth = depth, |
| 195 |
.array_size = array_size, |
| 196 |
.last_level = last_level, |
| 197 |
.target = target }; |
| 198 |
|
| 199 |
res = CALLOC_STRUCT(virgl_hw_res); |
| 200 |
if (!res) |
| 201 |
return NULL; |
| 202 |
|
| 203 |
/* Make sure blob is page aligned. */ |
| 204 |
width = align(width, getpagesize()); |
| 205 |
size = align(size, getpagesize()); |
| 206 |
|
| 207 |
blob_id = p_atomic_inc_return(&qdws->blob_id); |
| 208 |
cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_CREATE, 0, VIRGL_PIPE_RES_CREATE_SIZE); |
| 209 |
cmd[VIRGL_PIPE_RES_CREATE_FORMAT] = pipe_to_virgl_format(format); |
| 210 |
cmd[VIRGL_PIPE_RES_CREATE_BIND] = bind; |
| 211 |
cmd[VIRGL_PIPE_RES_CREATE_TARGET] = target; |
| 212 |
cmd[VIRGL_PIPE_RES_CREATE_WIDTH] = width; |
| 213 |
cmd[VIRGL_PIPE_RES_CREATE_HEIGHT] = height; |
| 214 |
cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = depth; |
| 215 |
cmd[VIRGL_PIPE_RES_CREATE_ARRAY_SIZE] = array_size; |
| 216 |
cmd[VIRGL_PIPE_RES_CREATE_LAST_LEVEL] = last_level; |
| 217 |
cmd[VIRGL_PIPE_RES_CREATE_NR_SAMPLES] = nr_samples; |
| 218 |
cmd[VIRGL_PIPE_RES_CREATE_FLAGS] = flags; |
| 219 |
cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = blob_id; |
| 220 |
|
| 221 |
drm_rc_blob.cmd = (unsigned long)(void *)&cmd; |
| 222 |
drm_rc_blob.cmd_size = 4 * (VIRGL_PIPE_RES_CREATE_SIZE + 1); |
| 223 |
drm_rc_blob.size = size; |
| 224 |
drm_rc_blob.blob_mem = VIRTGPU_BLOB_MEM_HOST3D; |
| 225 |
drm_rc_blob.blob_flags = VIRTGPU_BLOB_FLAG_USE_MAPPABLE; |
| 226 |
drm_rc_blob.blob_id = (uint64_t) blob_id; |
| 227 |
|
| 228 |
ret = drmIoctl(qdws->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob); |
| 229 |
if (ret != 0) { |
| 230 |
FREE(res); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
res->bind = bind; |
| 235 |
res->res_handle = drm_rc_blob.res_handle; |
| 236 |
res->bo_handle = drm_rc_blob.bo_handle; |
| 237 |
res->size = size; |
| 238 |
res->flags = flags; |
| 239 |
res->maybe_untyped = false; |
| 240 |
pipe_reference_init(&res->reference, 1); |
| 241 |
p_atomic_set(&res->external, false); |
| 242 |
p_atomic_set(&res->num_cs_references, 0); |
| 243 |
res->needed_references = 1; |
| 244 |
virgl_resource_cache_entry_init(&res->cache_entry, params); |
| 245 |
return res; |
| 246 |
} |
| 247 |
|
| 248 |
static struct virgl_hw_res * |
| 249 |
virgl_drm_winsys_resource_create(struct virgl_winsys *qws, |
| 250 |
enum pipe_texture_target target, |
| 251 |
uint32_t format, |
| 252 |
uint32_t bind, |
| 253 |
uint32_t width, |
| 254 |
uint32_t height, |
| 255 |
uint32_t depth, |
| 256 |
uint32_t array_size, |
| 257 |
uint32_t last_level, |
| 258 |
uint32_t nr_samples, |
| 259 |
uint32_t size, |
| 260 |
bool for_fencing) |
| 261 |
{ |
| 262 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 263 |
struct drm_virtgpu_resource_create createcmd; |
| 264 |
int ret; |
| 265 |
struct virgl_hw_res *res; |
| 266 |
uint32_t stride = width * util_format_get_blocksize(format); |
| 267 |
struct virgl_resource_params params = { .size = size, |
| 268 |
.bind = bind, |
| 269 |
.format = format, |
| 270 |
.flags = 0, |
| 271 |
.nr_samples = nr_samples, |
| 272 |
.width = width, |
| 273 |
.height = height, |
| 274 |
.depth = depth, |
| 275 |
.array_size = array_size, |
| 276 |
.last_level = last_level, |
| 277 |
.target = target }; |
| 278 |
|
| 279 |
res = CALLOC_STRUCT(virgl_hw_res); |
| 280 |
if (!res) |
| 281 |
return NULL; |
| 282 |
|
| 283 |
memset(&createcmd, 0, sizeof(createcmd)); |
| 284 |
createcmd.target = target; |
| 285 |
createcmd.format = pipe_to_virgl_format(format); |
| 286 |
createcmd.bind = bind; |
| 287 |
createcmd.width = width; |
| 288 |
createcmd.height = height; |
| 289 |
createcmd.depth = depth; |
| 290 |
createcmd.array_size = array_size; |
| 291 |
createcmd.last_level = last_level; |
| 292 |
createcmd.nr_samples = nr_samples; |
| 293 |
createcmd.stride = stride; |
| 294 |
createcmd.size = size; |
| 295 |
|
| 296 |
ret = drmIoctl(qdws->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &createcmd); |
| 297 |
if (ret != 0) { |
| 298 |
FREE(res); |
| 299 |
return NULL; |
| 300 |
} |
| 301 |
|
| 302 |
res->bind = bind; |
| 303 |
|
| 304 |
res->res_handle = createcmd.res_handle; |
| 305 |
res->bo_handle = createcmd.bo_handle; |
| 306 |
res->size = size; |
| 307 |
res->target = target; |
| 308 |
res->maybe_untyped = false; |
| 309 |
pipe_reference_init(&res->reference, 1); |
| 310 |
p_atomic_set(&res->external, false); |
| 311 |
p_atomic_set(&res->num_cs_references, 0); |
| 312 |
res->needed_references = 1; |
| 313 |
|
| 314 |
/* A newly created resource is considered busy by the kernel until the |
| 315 |
* command is retired. But for our purposes, we can consider it idle |
| 316 |
* unless it is used for fencing. |
| 317 |
*/ |
| 318 |
p_atomic_set(&res->maybe_busy, for_fencing); |
| 319 |
|
| 320 |
virgl_resource_cache_entry_init(&res->cache_entry, params); |
| 321 |
|
| 322 |
return res; |
| 323 |
} |
| 324 |
|
| 325 |
/* |
| 326 |
* Previously, with DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, all host resources had |
| 327 |
* a guest memory shadow resource with size = stride * bpp. Virglrenderer |
| 328 |
* would guess the stride implicitly when performing transfer operations, if |
| 329 |
* the stride wasn't specified. Interestingly, vtest would specify the stride. |
| 330 |
* |
| 331 |
* Guessing the stride breaks down with YUV images, which may be imported into |
| 332 |
* Mesa as 3R8 images. It also doesn't work if an external allocator |
| 333 |
* (i.e, minigbm) decides to use a stride not equal to stride * bpp. With blob |
| 334 |
* resources, the size = stride * bpp restriction no longer holds, so use |
| 335 |
* explicit strides passed into Mesa. |
| 336 |
*/ |
| 337 |
static inline bool use_explicit_stride(struct virgl_hw_res *res, uint32_t level, |
| 338 |
uint32_t depth) |
| 339 |
{ |
| 340 |
return (params[param_resource_blob].value && |
| 341 |
res->blob_mem == VIRTGPU_BLOB_MEM_HOST3D_GUEST && |
| 342 |
res->target == PIPE_TEXTURE_2D && |
| 343 |
level == 0 && depth == 1); |
| 344 |
} |
| 345 |
|
| 346 |
static int |
| 347 |
virgl_bo_transfer_put(struct virgl_winsys *vws, |
| 348 |
struct virgl_hw_res *res, |
| 349 |
const struct pipe_box *box, |
| 350 |
uint32_t stride, uint32_t layer_stride, |
| 351 |
uint32_t buf_offset, uint32_t level) |
| 352 |
{ |
| 353 |
struct virgl_drm_winsys *vdws = virgl_drm_winsys(vws); |
| 354 |
struct drm_virtgpu_3d_transfer_to_host tohostcmd; |
| 355 |
|
| 356 |
p_atomic_set(&res->maybe_busy, true); |
| 357 |
|
| 358 |
memset(&tohostcmd, 0, sizeof(tohostcmd)); |
| 359 |
tohostcmd.bo_handle = res->bo_handle; |
| 360 |
tohostcmd.box.x = box->x; |
| 361 |
tohostcmd.box.y = box->y; |
| 362 |
tohostcmd.box.z = box->z; |
| 363 |
tohostcmd.box.w = box->width; |
| 364 |
tohostcmd.box.h = box->height; |
| 365 |
tohostcmd.box.d = box->depth; |
| 366 |
tohostcmd.offset = buf_offset; |
| 367 |
tohostcmd.level = level; |
| 368 |
|
| 369 |
if (use_explicit_stride(res, level, box->depth)) |
| 370 |
tohostcmd.stride = stride; |
| 371 |
|
| 372 |
return drmIoctl(vdws->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &tohostcmd); |
| 373 |
} |
| 374 |
|
| 375 |
static int |
| 376 |
virgl_bo_transfer_get(struct virgl_winsys *vws, |
| 377 |
struct virgl_hw_res *res, |
| 378 |
const struct pipe_box *box, |
| 379 |
uint32_t stride, uint32_t layer_stride, |
| 380 |
uint32_t buf_offset, uint32_t level) |
| 381 |
{ |
| 382 |
struct virgl_drm_winsys *vdws = virgl_drm_winsys(vws); |
| 383 |
struct drm_virtgpu_3d_transfer_from_host fromhostcmd; |
| 384 |
|
| 385 |
p_atomic_set(&res->maybe_busy, true); |
| 386 |
|
| 387 |
memset(&fromhostcmd, 0, sizeof(fromhostcmd)); |
| 388 |
fromhostcmd.bo_handle = res->bo_handle; |
| 389 |
fromhostcmd.level = level; |
| 390 |
fromhostcmd.offset = buf_offset; |
| 391 |
fromhostcmd.box.x = box->x; |
| 392 |
fromhostcmd.box.y = box->y; |
| 393 |
fromhostcmd.box.z = box->z; |
| 394 |
fromhostcmd.box.w = box->width; |
| 395 |
fromhostcmd.box.h = box->height; |
| 396 |
fromhostcmd.box.d = box->depth; |
| 397 |
|
| 398 |
if (use_explicit_stride(res, level, box->depth)) |
| 399 |
fromhostcmd.stride = stride; |
| 400 |
|
| 401 |
return drmIoctl(vdws->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &fromhostcmd); |
| 402 |
} |
| 403 |
|
| 404 |
static struct virgl_hw_res * |
| 405 |
virgl_drm_winsys_resource_cache_create(struct virgl_winsys *qws, |
| 406 |
enum pipe_texture_target target, |
| 407 |
const void *map_front_private, |
| 408 |
uint32_t format, |
| 409 |
uint32_t bind, |
| 410 |
uint32_t width, |
| 411 |
uint32_t height, |
| 412 |
uint32_t depth, |
| 413 |
uint32_t array_size, |
| 414 |
uint32_t last_level, |
| 415 |
uint32_t nr_samples, |
| 416 |
uint32_t flags, |
| 417 |
uint32_t size) |
| 418 |
{ |
| 419 |
bool need_sync = false; |
| 420 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 421 |
struct virgl_hw_res *res; |
| 422 |
struct virgl_resource_cache_entry *entry; |
| 423 |
struct virgl_resource_params params = { .size = size, |
| 424 |
.bind = bind, |
| 425 |
.format = format, |
| 426 |
.flags = flags, |
| 427 |
.nr_samples = nr_samples, |
| 428 |
.width = width, |
| 429 |
.height = height, |
| 430 |
.depth = depth, |
| 431 |
.array_size = array_size, |
| 432 |
.last_level = last_level, |
| 433 |
.target = target }; |
| 434 |
|
| 435 |
if (!can_cache_resource(bind)) |
| 436 |
goto alloc; |
| 437 |
|
| 438 |
mtx_lock(&qdws->mutex); |
| 439 |
|
| 440 |
entry = virgl_resource_cache_remove_compatible(&qdws->cache, params); |
| 441 |
if (entry) { |
| 442 |
res = cache_entry_container_res(entry); |
| 443 |
mtx_unlock(&qdws->mutex); |
| 444 |
pipe_reference_init(&res->reference, 1); |
| 445 |
return res; |
| 446 |
} |
| 447 |
|
| 448 |
mtx_unlock(&qdws->mutex); |
| 449 |
|
| 450 |
alloc: |
| 451 |
/* PIPE_BUFFER with VIRGL_BIND_CUSTOM flag will access data when attaching, |
| 452 |
* in order to avoid race conditions we need to treat it as busy during |
| 453 |
* creation |
| 454 |
*/ |
| 455 |
if (target == PIPE_BUFFER && (bind & VIRGL_BIND_CUSTOM)) |
| 456 |
need_sync = true; |
| 457 |
|
| 458 |
if (flags & (VIRGL_RESOURCE_FLAG_MAP_PERSISTENT | |
| 459 |
VIRGL_RESOURCE_FLAG_MAP_COHERENT)) |
| 460 |
res = virgl_drm_winsys_resource_create_blob(qws, target, format, bind, |
| 461 |
width, height, depth, |
| 462 |
array_size, last_level, |
| 463 |
nr_samples, flags, size); |
| 464 |
else |
| 465 |
res = virgl_drm_winsys_resource_create(qws, target, format, bind, width, |
| 466 |
height, depth, array_size, |
| 467 |
last_level, nr_samples, size, |
| 468 |
need_sync); |
| 469 |
return res; |
| 470 |
} |
| 471 |
|
| 472 |
static uint32_t |
| 473 |
virgl_drm_winsys_resource_get_storage_size(struct virgl_winsys *qws, |
| 474 |
struct virgl_hw_res *res) |
| 475 |
{ |
| 476 |
return res->size; |
| 477 |
} |
| 478 |
|
| 479 |
static struct virgl_hw_res * |
| 480 |
virgl_drm_winsys_resource_create_handle(struct virgl_winsys *qws, |
| 481 |
struct winsys_handle *whandle, |
| 482 |
UNUSED struct pipe_resource *templ, |
| 483 |
uint32_t *plane, |
| 484 |
uint32_t *stride, |
| 485 |
uint32_t *plane_offset, |
| 486 |
uint64_t *modifier, |
| 487 |
uint32_t *blob_mem) |
| 488 |
{ |
| 489 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 490 |
struct drm_gem_open open_arg = {}; |
| 491 |
struct drm_virtgpu_resource_info info_arg = {}; |
| 492 |
struct virgl_hw_res *res = NULL; |
| 493 |
uint32_t handle = whandle->handle; |
| 494 |
|
| 495 |
if (whandle->plane >= VIRGL_MAX_PLANE_COUNT) { |
| 496 |
return NULL; |
| 497 |
} |
| 498 |
|
| 499 |
if (whandle->offset != 0 && whandle->type == WINSYS_HANDLE_TYPE_SHARED) { |
| 500 |
_debug_printf("attempt to import unsupported winsys offset %u\n", |
| 501 |
whandle->offset); |
| 502 |
return NULL; |
| 503 |
} else if (whandle->type == WINSYS_HANDLE_TYPE_FD) { |
| 504 |
*plane = whandle->plane; |
| 505 |
*stride = whandle->stride; |
| 506 |
*plane_offset = whandle->offset; |
| 507 |
*modifier = whandle->modifier; |
| 508 |
} |
| 509 |
|
| 510 |
mtx_lock(&qdws->bo_handles_mutex); |
| 511 |
|
| 512 |
/* We must maintain a list of pairs <handle, bo>, so that we always return |
| 513 |
* the same BO for one particular handle. If we didn't do that and created |
| 514 |
* more than one BO for the same handle and then relocated them in a CS, |
| 515 |
* we would hit a deadlock in the kernel. |
| 516 |
* |
| 517 |
* The list of pairs is guarded by a mutex, of course. */ |
| 518 |
if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) { |
| 519 |
res = util_hash_table_get(qdws->bo_names, (void*)(uintptr_t)handle); |
| 520 |
} else if (whandle->type == WINSYS_HANDLE_TYPE_FD) { |
| 521 |
int r; |
| 522 |
r = drmPrimeFDToHandle(qdws->fd, whandle->handle, &handle); |
| 523 |
if (r) |
| 524 |
goto done; |
| 525 |
res = util_hash_table_get(qdws->bo_handles, (void*)(uintptr_t)handle); |
| 526 |
} else { |
| 527 |
/* Unknown handle type */ |
| 528 |
goto done; |
| 529 |
} |
| 530 |
|
| 531 |
if (res) { |
| 532 |
/* qdws->bo_{names,handles} hold weak pointers to virgl_hw_res. Because |
| 533 |
* virgl_drm_resource_reference does not take qdws->bo_handles_mutex |
| 534 |
* until it enters virgl_hw_res_destroy, there is a small window that |
| 535 |
* the refcount can drop to zero. Call p_atomic_inc directly instead of |
| 536 |
* virgl_drm_resource_reference to avoid hitting assert failures. |
| 537 |
* |
| 538 |
* If the refcount was 0, that means that the resource is currently |
| 539 |
* waiting to be freed in another thread, increase the needed_references |
| 540 |
* as a workaround to make sure that it won't be double freed for now. |
| 541 |
*/ |
| 542 |
int32_t ref = p_atomic_inc_return(&res->reference.count); |
| 543 |
if (ref == 1) |
| 544 |
res->needed_references++; |
| 545 |
|
| 546 |
goto done; |
| 547 |
} |
| 548 |
|
| 549 |
res = CALLOC_STRUCT(virgl_hw_res); |
| 550 |
if (!res) |
| 551 |
goto done; |
| 552 |
|
| 553 |
if (whandle->type == WINSYS_HANDLE_TYPE_FD) { |
| 554 |
res->bo_handle = handle; |
| 555 |
} else { |
| 556 |
memset(&open_arg, 0, sizeof(open_arg)); |
| 557 |
open_arg.name = whandle->handle; |
| 558 |
if (drmIoctl(qdws->fd, DRM_IOCTL_GEM_OPEN, &open_arg)) { |
| 559 |
FREE(res); |
| 560 |
res = NULL; |
| 561 |
goto done; |
| 562 |
} |
| 563 |
res->bo_handle = open_arg.handle; |
| 564 |
res->flink_name = whandle->handle; |
| 565 |
} |
| 566 |
|
| 567 |
memset(&info_arg, 0, sizeof(info_arg)); |
| 568 |
info_arg.bo_handle = res->bo_handle; |
| 569 |
|
| 570 |
if (drmIoctl(qdws->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO, &info_arg)) { |
| 571 |
drmCloseBufferHandle(qdws->fd, res->bo_handle); |
| 572 |
|
| 573 |
/* close */ |
| 574 |
FREE(res); |
| 575 |
res = NULL; |
| 576 |
goto done; |
| 577 |
} |
| 578 |
|
| 579 |
res->res_handle = info_arg.res_handle; |
| 580 |
res->blob_mem = info_arg.blob_mem; |
| 581 |
*blob_mem = info_arg.blob_mem; |
| 582 |
|
| 583 |
res->size = info_arg.size; |
| 584 |
res->maybe_untyped = info_arg.blob_mem ? true : false; |
| 585 |
pipe_reference_init(&res->reference, 1); |
| 586 |
p_atomic_set(&res->external, true); |
| 587 |
res->num_cs_references = 0; |
| 588 |
res->needed_references = 1; |
| 589 |
|
| 590 |
if (res->flink_name) |
| 591 |
_mesa_hash_table_insert(qdws->bo_names, (void *)(uintptr_t)res->flink_name, res); |
| 592 |
_mesa_hash_table_insert(qdws->bo_handles, (void *)(uintptr_t)res->bo_handle, res); |
| 593 |
|
| 594 |
done: |
| 595 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 596 |
return res; |
| 597 |
} |
| 598 |
|
| 599 |
static void |
| 600 |
virgl_drm_winsys_resource_set_type(struct virgl_winsys *qws, |
| 601 |
struct virgl_hw_res *res, |
| 602 |
uint32_t format, uint32_t bind, |
| 603 |
uint32_t width, uint32_t height, |
| 604 |
uint32_t usage, uint64_t modifier, |
| 605 |
uint32_t plane_count, |
| 606 |
const uint32_t *plane_strides, |
| 607 |
const uint32_t *plane_offsets) |
| 608 |
{ |
| 609 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 610 |
uint32_t cmd[VIRGL_PIPE_RES_SET_TYPE_SIZE(VIRGL_MAX_PLANE_COUNT) + 1]; |
| 611 |
struct drm_virtgpu_execbuffer eb; |
| 612 |
int ret; |
| 613 |
|
| 614 |
mtx_lock(&qdws->bo_handles_mutex); |
| 615 |
|
| 616 |
if (!res->maybe_untyped) { |
| 617 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 618 |
return; |
| 619 |
} |
| 620 |
res->maybe_untyped = false; |
| 621 |
|
| 622 |
assert(plane_count && plane_count <= VIRGL_MAX_PLANE_COUNT); |
| 623 |
|
| 624 |
cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_SET_TYPE, 0, VIRGL_PIPE_RES_SET_TYPE_SIZE(plane_count)); |
| 625 |
cmd[VIRGL_PIPE_RES_SET_TYPE_RES_HANDLE] = res->res_handle, |
| 626 |
cmd[VIRGL_PIPE_RES_SET_TYPE_FORMAT] = format; |
| 627 |
cmd[VIRGL_PIPE_RES_SET_TYPE_BIND] = bind; |
| 628 |
cmd[VIRGL_PIPE_RES_SET_TYPE_WIDTH] = width; |
| 629 |
cmd[VIRGL_PIPE_RES_SET_TYPE_HEIGHT] = height; |
| 630 |
cmd[VIRGL_PIPE_RES_SET_TYPE_USAGE] = usage; |
| 631 |
cmd[VIRGL_PIPE_RES_SET_TYPE_MODIFIER_LO] = (uint32_t)modifier; |
| 632 |
cmd[VIRGL_PIPE_RES_SET_TYPE_MODIFIER_HI] = (uint32_t)(modifier >> 32); |
| 633 |
for (uint32_t i = 0; i < plane_count; i++) { |
| 634 |
cmd[VIRGL_PIPE_RES_SET_TYPE_PLANE_STRIDE(i)] = plane_strides[i]; |
| 635 |
cmd[VIRGL_PIPE_RES_SET_TYPE_PLANE_OFFSET(i)] = plane_offsets[i]; |
| 636 |
} |
| 637 |
|
| 638 |
memset(&eb, 0, sizeof(eb)); |
| 639 |
eb.command = (uintptr_t)cmd; |
| 640 |
eb.size = (1 + VIRGL_PIPE_RES_SET_TYPE_SIZE(plane_count)) * 4; |
| 641 |
eb.num_bo_handles = 1; |
| 642 |
eb.bo_handles = (uintptr_t)&res->bo_handle; |
| 643 |
|
| 644 |
ret = drmIoctl(qdws->fd, DRM_IOCTL_VIRTGPU_EXECBUFFER, &eb); |
| 645 |
if (ret == -1) |
| 646 |
_debug_printf("failed to set resource type: %s", strerror(errno)); |
| 647 |
|
| 648 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 649 |
} |
| 650 |
|
| 651 |
static bool virgl_drm_winsys_resource_get_handle(struct virgl_winsys *qws, |
| 652 |
struct virgl_hw_res *res, |
| 653 |
uint32_t stride, |
| 654 |
struct winsys_handle *whandle) |
| 655 |
{ |
| 656 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 657 |
struct drm_gem_flink flink; |
| 658 |
|
| 659 |
if (!res) |
| 660 |
return false; |
| 661 |
|
| 662 |
if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) { |
| 663 |
if (!res->flink_name) { |
| 664 |
memset(&flink, 0, sizeof(flink)); |
| 665 |
flink.handle = res->bo_handle; |
| 666 |
|
| 667 |
if (drmIoctl(qdws->fd, DRM_IOCTL_GEM_FLINK, &flink)) { |
| 668 |
return false; |
| 669 |
} |
| 670 |
res->flink_name = flink.name; |
| 671 |
|
| 672 |
mtx_lock(&qdws->bo_handles_mutex); |
| 673 |
_mesa_hash_table_insert(qdws->bo_names, (void *)(uintptr_t)res->flink_name, res); |
| 674 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 675 |
} |
| 676 |
whandle->handle = res->flink_name; |
| 677 |
} else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) { |
| 678 |
whandle->handle = res->bo_handle; |
| 679 |
} else if (whandle->type == WINSYS_HANDLE_TYPE_FD) { |
| 680 |
const uint32_t blob_flags = VIRGL_RESOURCE_FLAG_MAP_PERSISTENT | |
| 681 |
VIRGL_RESOURCE_FLAG_MAP_COHERENT; |
| 682 |
uint32_t flags = DRM_CLOEXEC; |
| 683 |
if (!(res->flags & blob_flags)) |
| 684 |
flags |= DRM_RDWR; |
| 685 |
if (drmPrimeHandleToFD(qdws->fd, res->bo_handle, flags, (int*)&whandle->handle)) |
| 686 |
return false; |
| 687 |
mtx_lock(&qdws->bo_handles_mutex); |
| 688 |
_mesa_hash_table_insert(qdws->bo_handles, (void *)(uintptr_t)res->bo_handle, res); |
| 689 |
mtx_unlock(&qdws->bo_handles_mutex); |
| 690 |
} |
| 691 |
|
| 692 |
p_atomic_set(&res->external, true); |
| 693 |
|
| 694 |
whandle->stride = stride; |
| 695 |
return true; |
| 696 |
} |
| 697 |
|
| 698 |
static void *virgl_drm_resource_map(struct virgl_winsys *qws, |
| 699 |
struct virgl_hw_res *res) |
| 700 |
{ |
| 701 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 702 |
struct drm_virtgpu_map mmap_arg; |
| 703 |
void *ptr; |
| 704 |
|
| 705 |
if (res->ptr) |
| 706 |
return res->ptr; |
| 707 |
|
| 708 |
memset(&mmap_arg, 0, sizeof(mmap_arg)); |
| 709 |
mmap_arg.handle = res->bo_handle; |
| 710 |
if (drmIoctl(qdws->fd, DRM_IOCTL_VIRTGPU_MAP, &mmap_arg)) |
| 711 |
return NULL; |
| 712 |
|
| 713 |
ptr = os_mmap(0, res->size, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 714 |
qdws->fd, mmap_arg.offset); |
| 715 |
if (ptr == MAP_FAILED) |
| 716 |
return NULL; |
| 717 |
|
| 718 |
res->ptr = ptr; |
| 719 |
return ptr; |
| 720 |
|
| 721 |
} |
| 722 |
|
| 723 |
static void virgl_drm_resource_wait(struct virgl_winsys *qws, |
| 724 |
struct virgl_hw_res *res) |
| 725 |
{ |
| 726 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 727 |
struct drm_virtgpu_3d_wait waitcmd; |
| 728 |
int ret; |
| 729 |
|
| 730 |
if (!p_atomic_read(&res->maybe_busy) && !p_atomic_read(&res->external)) |
| 731 |
return; |
| 732 |
|
| 733 |
memset(&waitcmd, 0, sizeof(waitcmd)); |
| 734 |
waitcmd.handle = res->bo_handle; |
| 735 |
|
| 736 |
ret = drmIoctl(qdws->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd); |
| 737 |
if (ret) |
| 738 |
_debug_printf("waiting got error - %d, slow gpu or hang?\n", errno); |
| 739 |
|
| 740 |
p_atomic_set(&res->maybe_busy, false); |
| 741 |
} |
| 742 |
|
| 743 |
static bool virgl_drm_alloc_res_list(struct virgl_drm_cmd_buf *cbuf, |
| 744 |
int initial_size) |
| 745 |
{ |
| 746 |
cbuf->nres = initial_size; |
| 747 |
cbuf->cres = 0; |
| 748 |
|
| 749 |
cbuf->res_bo = CALLOC(cbuf->nres, sizeof(struct virgl_hw_buf*)); |
| 750 |
if (!cbuf->res_bo) |
| 751 |
return false; |
| 752 |
|
| 753 |
cbuf->res_hlist = MALLOC(cbuf->nres * sizeof(uint32_t)); |
| 754 |
if (!cbuf->res_hlist) { |
| 755 |
FREE(cbuf->res_bo); |
| 756 |
return false; |
| 757 |
} |
| 758 |
|
| 759 |
return true; |
| 760 |
} |
| 761 |
|
| 762 |
static void virgl_drm_free_res_list(struct virgl_drm_cmd_buf *cbuf) |
| 763 |
{ |
| 764 |
int i; |
| 765 |
|
| 766 |
for (i = 0; i < cbuf->cres; i++) { |
| 767 |
p_atomic_dec(&cbuf->res_bo[i]->num_cs_references); |
| 768 |
virgl_drm_resource_reference(cbuf->ws, &cbuf->res_bo[i], NULL); |
| 769 |
} |
| 770 |
FREE(cbuf->res_hlist); |
| 771 |
FREE(cbuf->res_bo); |
| 772 |
} |
| 773 |
|
| 774 |
static bool virgl_drm_res_is_added(struct virgl_drm_cmd_buf *cbuf, |
| 775 |
struct virgl_hw_res *res) |
| 776 |
{ |
| 777 |
for (int i = 0; i < cbuf->cres; i++) { |
| 778 |
if (cbuf->res_bo[i] == res) |
| 779 |
return true; |
| 780 |
} |
| 781 |
|
| 782 |
return false; |
| 783 |
} |
| 784 |
|
| 785 |
static void virgl_drm_add_res(struct virgl_drm_winsys *qdws, |
| 786 |
struct virgl_drm_cmd_buf *cbuf, |
| 787 |
struct virgl_hw_res *res) |
| 788 |
{ |
| 789 |
bool already_in_list = virgl_drm_res_is_added(cbuf, res); |
| 790 |
if (unlikely(already_in_list)) |
| 791 |
return; |
| 792 |
|
| 793 |
if (cbuf->cres >= cbuf->nres) { |
| 794 |
unsigned new_nres = cbuf->nres + 256; |
| 795 |
void *new_ptr = REALLOC(cbuf->res_bo, |
| 796 |
cbuf->nres * sizeof(struct virgl_hw_buf*), |
| 797 |
new_nres * sizeof(struct virgl_hw_buf*)); |
| 798 |
if (!new_ptr) { |
| 799 |
_debug_printf("failure to add relocation %d, %d\n", cbuf->cres, new_nres); |
| 800 |
return; |
| 801 |
} |
| 802 |
cbuf->res_bo = new_ptr; |
| 803 |
|
| 804 |
new_ptr = REALLOC(cbuf->res_hlist, |
| 805 |
cbuf->nres * sizeof(uint32_t), |
| 806 |
new_nres * sizeof(uint32_t)); |
| 807 |
if (!new_ptr) { |
| 808 |
_debug_printf("failure to add hlist relocation %d, %d\n", cbuf->cres, cbuf->nres); |
| 809 |
return; |
| 810 |
} |
| 811 |
cbuf->res_hlist = new_ptr; |
| 812 |
cbuf->nres = new_nres; |
| 813 |
} |
| 814 |
|
| 815 |
cbuf->res_bo[cbuf->cres] = NULL; |
| 816 |
virgl_drm_resource_reference(&qdws->base, &cbuf->res_bo[cbuf->cres], res); |
| 817 |
cbuf->res_hlist[cbuf->cres] = res->bo_handle; |
| 818 |
p_atomic_inc(&res->num_cs_references); |
| 819 |
cbuf->cres++; |
| 820 |
} |
| 821 |
|
| 822 |
/* This is called after the cbuf is submitted. */ |
| 823 |
static void virgl_drm_clear_res_list(struct virgl_drm_cmd_buf *cbuf) |
| 824 |
{ |
| 825 |
int i; |
| 826 |
|
| 827 |
for (i = 0; i < cbuf->cres; i++) { |
| 828 |
/* mark all BOs busy after submission */ |
| 829 |
p_atomic_set(&cbuf->res_bo[i]->maybe_busy, true); |
| 830 |
|
| 831 |
p_atomic_dec(&cbuf->res_bo[i]->num_cs_references); |
| 832 |
virgl_drm_resource_reference(cbuf->ws, &cbuf->res_bo[i], NULL); |
| 833 |
} |
| 834 |
|
| 835 |
cbuf->cres = 0; |
| 836 |
} |
| 837 |
|
| 838 |
static void virgl_drm_emit_res(struct virgl_winsys *qws, |
| 839 |
struct virgl_cmd_buf *_cbuf, |
| 840 |
struct virgl_hw_res *res, bool write_buf) |
| 841 |
{ |
| 842 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 843 |
struct virgl_drm_cmd_buf *cbuf = virgl_drm_cmd_buf(_cbuf); |
| 844 |
|
| 845 |
if (write_buf) |
| 846 |
cbuf->base.buf[cbuf->base.cdw++] = res->res_handle; |
| 847 |
|
| 848 |
virgl_drm_add_res(qdws, cbuf, res); |
| 849 |
} |
| 850 |
|
| 851 |
static bool virgl_drm_res_is_ref(struct virgl_winsys *qws, |
| 852 |
struct virgl_cmd_buf *_cbuf, |
| 853 |
struct virgl_hw_res *res) |
| 854 |
{ |
| 855 |
if (!p_atomic_read(&res->num_cs_references)) |
| 856 |
return false; |
| 857 |
|
| 858 |
return true; |
| 859 |
} |
| 860 |
|
| 861 |
static struct virgl_cmd_buf *virgl_drm_cmd_buf_create(struct virgl_winsys *qws, |
| 862 |
uint32_t size) |
| 863 |
{ |
| 864 |
struct virgl_drm_cmd_buf *cbuf; |
| 865 |
|
| 866 |
cbuf = CALLOC_STRUCT(virgl_drm_cmd_buf); |
| 867 |
if (!cbuf) |
| 868 |
return NULL; |
| 869 |
|
| 870 |
cbuf->ws = qws; |
| 871 |
|
| 872 |
if (!virgl_drm_alloc_res_list(cbuf, 512)) { |
| 873 |
FREE(cbuf); |
| 874 |
return NULL; |
| 875 |
} |
| 876 |
|
| 877 |
cbuf->buf = CALLOC(size, sizeof(uint32_t)); |
| 878 |
if (!cbuf->buf) { |
| 879 |
FREE(cbuf->res_hlist); |
| 880 |
FREE(cbuf->res_bo); |
| 881 |
FREE(cbuf); |
| 882 |
return NULL; |
| 883 |
} |
| 884 |
|
| 885 |
cbuf->in_fence_fd = -1; |
| 886 |
cbuf->base.buf = cbuf->buf; |
| 887 |
return &cbuf->base; |
| 888 |
} |
| 889 |
|
| 890 |
static void virgl_drm_cmd_buf_destroy(struct virgl_cmd_buf *_cbuf) |
| 891 |
{ |
| 892 |
struct virgl_drm_cmd_buf *cbuf = virgl_drm_cmd_buf(_cbuf); |
| 893 |
|
| 894 |
virgl_drm_free_res_list(cbuf); |
| 895 |
|
| 896 |
FREE(cbuf->buf); |
| 897 |
FREE(cbuf); |
| 898 |
} |
| 899 |
|
| 900 |
static struct pipe_fence_handle * |
| 901 |
virgl_drm_fence_create(struct virgl_winsys *vws, int fd, bool external) |
| 902 |
{ |
| 903 |
struct virgl_drm_fence *fence; |
| 904 |
|
| 905 |
assert(vws->supports_fences); |
| 906 |
|
| 907 |
if (external) { |
| 908 |
fd = os_dupfd_cloexec(fd); |
| 909 |
if (fd < 0) |
| 910 |
return NULL; |
| 911 |
} |
| 912 |
|
| 913 |
fence = CALLOC_STRUCT(virgl_drm_fence); |
| 914 |
if (!fence) { |
| 915 |
close(fd); |
| 916 |
return NULL; |
| 917 |
} |
| 918 |
|
| 919 |
fence->fd = fd; |
| 920 |
fence->external = external; |
| 921 |
|
| 922 |
pipe_reference_init(&fence->reference, 1); |
| 923 |
|
| 924 |
return (struct pipe_fence_handle *)fence; |
| 925 |
} |
| 926 |
|
| 927 |
static struct pipe_fence_handle * |
| 928 |
virgl_drm_fence_create_legacy(struct virgl_winsys *vws) |
| 929 |
{ |
| 930 |
struct virgl_drm_fence *fence; |
| 931 |
|
| 932 |
assert(!vws->supports_fences); |
| 933 |
|
| 934 |
fence = CALLOC_STRUCT(virgl_drm_fence); |
| 935 |
if (!fence) |
| 936 |
return NULL; |
| 937 |
fence->fd = -1; |
| 938 |
|
| 939 |
/* Resources for fences should not be from the cache, since we are basing |
| 940 |
* the fence status on the resource creation busy status. |
| 941 |
*/ |
| 942 |
fence->hw_res = virgl_drm_winsys_resource_create(vws, PIPE_BUFFER, |
| 943 |
PIPE_FORMAT_R8_UNORM, VIRGL_BIND_CUSTOM, 8, 1, 1, 0, 0, 0, 8, true); |
| 944 |
if (!fence->hw_res) { |
| 945 |
FREE(fence); |
| 946 |
return NULL; |
| 947 |
} |
| 948 |
|
| 949 |
pipe_reference_init(&fence->reference, 1); |
| 950 |
|
| 951 |
return (struct pipe_fence_handle *)fence; |
| 952 |
} |
| 953 |
|
| 954 |
static int virgl_drm_winsys_submit_cmd(struct virgl_winsys *qws, |
| 955 |
struct virgl_cmd_buf *_cbuf, |
| 956 |
struct pipe_fence_handle **fence) |
| 957 |
{ |
| 958 |
struct virgl_drm_winsys *qdws = virgl_drm_winsys(qws); |
| 959 |
struct virgl_drm_cmd_buf *cbuf = virgl_drm_cmd_buf(_cbuf); |
| 960 |
struct drm_virtgpu_execbuffer eb; |
| 961 |
int ret; |
| 962 |
|
| 963 |
if (cbuf->base.cdw == 0) |
| 964 |
return 0; |
| 965 |
|
| 966 |
memset(&eb, 0, sizeof(struct drm_virtgpu_execbuffer)); |
| 967 |
eb.command = (unsigned long)(void*)cbuf->buf; |
| 968 |
eb.size = cbuf->base.cdw * 4; |
| 969 |
eb.num_bo_handles = cbuf->cres; |
| 970 |
eb.bo_handles = (unsigned long)(void *)cbuf->res_hlist; |
| 971 |
|
| 972 |
eb.fence_fd = -1; |
| 973 |
if (qws->supports_fences) { |
| 974 |
if (cbuf->in_fence_fd >= 0) { |
| 975 |
eb.flags |= VIRTGPU_EXECBUF_FENCE_FD_IN; |
| 976 |
eb.fence_fd = cbuf->in_fence_fd; |
| 977 |
} |
| 978 |
|
| 979 |
if (fence != NULL) |
| 980 |
eb.flags |= VIRTGPU_EXECBUF_FENCE_FD_OUT; |
| 981 |
} else { |
| 982 |
assert(cbuf->in_fence_fd < 0); |
| 983 |
} |
| 984 |
|
| 985 |
ret = drmIoctl(qdws->fd, DRM_IOCTL_VIRTGPU_EXECBUFFER, &eb); |
| 986 |
if (ret == -1) |
| 987 |
_debug_printf("got error from kernel - expect bad rendering %d\n", errno); |
| 988 |
cbuf->base.cdw = 0; |
| 989 |
|
| 990 |
if (qws->supports_fences) { |
| 991 |
if (cbuf->in_fence_fd >= 0) { |
| 992 |
close(cbuf->in_fence_fd); |
| 993 |
cbuf->in_fence_fd = -1; |
| 994 |
} |
| 995 |
|
| 996 |
if (fence != NULL && ret == 0) |
| 997 |
*fence = virgl_drm_fence_create(qws, eb.fence_fd, false); |
| 998 |
} else { |
| 999 |
if (fence != NULL && ret == 0) |
| 1000 |
*fence = virgl_drm_fence_create_legacy(qws); |
| 1001 |
} |
| 1002 |
|
| 1003 |
virgl_drm_clear_res_list(cbuf); |
| 1004 |
|
| 1005 |
return ret; |
| 1006 |
} |
| 1007 |
|
| 1008 |
static int virgl_drm_get_caps(struct virgl_winsys *vws, |
| 1009 |
struct virgl_drm_caps *caps) |
| 1010 |
{ |
| 1011 |
struct virgl_drm_winsys *vdws = virgl_drm_winsys(vws); |
| 1012 |
struct drm_virtgpu_get_caps args; |
| 1013 |
int ret; |
| 1014 |
|
| 1015 |
virgl_ws_fill_new_caps_defaults(caps); |
| 1016 |
|
| 1017 |
memset(&args, 0, sizeof(args)); |
| 1018 |
if (params[param_capset_fix].value) { |
| 1019 |
/* if we have the query fix - try and get cap set id 2 first */ |
| 1020 |
args.cap_set_id = 2; |
| 1021 |
args.size = sizeof(union virgl_caps); |
| 1022 |
} else { |
| 1023 |
args.cap_set_id = 1; |
| 1024 |
args.size = sizeof(struct virgl_caps_v1); |
| 1025 |
} |
| 1026 |
args.addr = (unsigned long)&caps->caps; |
| 1027 |
|
| 1028 |
ret = drmIoctl(vdws->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &args); |
| 1029 |
if (ret == -1 && errno == EINVAL) { |
| 1030 |
/* Fallback to v1 */ |
| 1031 |
args.cap_set_id = 1; |
| 1032 |
args.size = sizeof(struct virgl_caps_v1); |
| 1033 |
ret = drmIoctl(vdws->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &args); |
| 1034 |
if (ret == -1) |
| 1035 |
return ret; |
| 1036 |
} |
| 1037 |
return ret; |
| 1038 |
} |
| 1039 |
|
| 1040 |
static struct pipe_fence_handle * |
| 1041 |
virgl_cs_create_fence(struct virgl_winsys *vws, int fd) |
| 1042 |
{ |
| 1043 |
if (!vws->supports_fences) |
| 1044 |
return NULL; |
| 1045 |
|
| 1046 |
return virgl_drm_fence_create(vws, fd, true); |
| 1047 |
} |
| 1048 |
|
| 1049 |
static bool virgl_fence_wait(struct virgl_winsys *vws, |
| 1050 |
struct pipe_fence_handle *_fence, |
| 1051 |
uint64_t timeout) |
| 1052 |
{ |
| 1053 |
struct virgl_drm_fence *fence = virgl_drm_fence(_fence); |
| 1054 |
|
| 1055 |
if (vws->supports_fences) { |
| 1056 |
uint64_t timeout_ms; |
| 1057 |
int timeout_poll; |
| 1058 |
|
| 1059 |
if (timeout == 0) |
| 1060 |
return sync_wait(fence->fd, 0) == 0; |
| 1061 |
|
| 1062 |
timeout_ms = timeout / 1000000; |
| 1063 |
/* round up */ |
| 1064 |
if (timeout_ms * 1000000 < timeout) |
| 1065 |
timeout_ms++; |
| 1066 |
|
| 1067 |
timeout_poll = timeout_ms <= INT_MAX ? (int) timeout_ms : -1; |
| 1068 |
|
| 1069 |
return sync_wait(fence->fd, timeout_poll) == 0; |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (timeout == 0) |
| 1073 |
return !virgl_drm_resource_is_busy(vws, fence->hw_res); |
| 1074 |
|
| 1075 |
if (timeout != OS_TIMEOUT_INFINITE) { |
| 1076 |
int64_t start_time = os_time_get(); |
| 1077 |
timeout /= 1000; |
| 1078 |
while (virgl_drm_resource_is_busy(vws, fence->hw_res)) { |
| 1079 |
if (os_time_get() - start_time >= timeout) |
| 1080 |
return false; |
| 1081 |
os_time_sleep(10); |
| 1082 |
} |
| 1083 |
return true; |
| 1084 |
} |
| 1085 |
virgl_drm_resource_wait(vws, fence->hw_res); |
| 1086 |
|
| 1087 |
return true; |
| 1088 |
} |
| 1089 |
|
| 1090 |
static void virgl_fence_reference(struct virgl_winsys *vws, |
| 1091 |
struct pipe_fence_handle **dst, |
| 1092 |
struct pipe_fence_handle *src) |
| 1093 |
{ |
| 1094 |
struct virgl_drm_fence *dfence = virgl_drm_fence(*dst); |
| 1095 |
struct virgl_drm_fence *sfence = virgl_drm_fence(src); |
| 1096 |
|
| 1097 |
if (pipe_reference(&dfence->reference, &sfence->reference)) { |
| 1098 |
if (vws->supports_fences) { |
| 1099 |
close(dfence->fd); |
| 1100 |
} else { |
| 1101 |
virgl_drm_resource_reference(vws, &dfence->hw_res, NULL); |
| 1102 |
} |
| 1103 |
FREE(dfence); |
| 1104 |
} |
| 1105 |
|
| 1106 |
*dst = src; |
| 1107 |
} |
| 1108 |
|
| 1109 |
static void virgl_fence_server_sync(struct virgl_winsys *vws, |
| 1110 |
struct virgl_cmd_buf *_cbuf, |
| 1111 |
struct pipe_fence_handle *_fence) |
| 1112 |
{ |
| 1113 |
struct virgl_drm_cmd_buf *cbuf = virgl_drm_cmd_buf(_cbuf); |
| 1114 |
struct virgl_drm_fence *fence = virgl_drm_fence(_fence); |
| 1115 |
|
| 1116 |
if (!vws->supports_fences) |
| 1117 |
return; |
| 1118 |
|
| 1119 |
/* if not an external fence, then nothing more to do without preemption: */ |
| 1120 |
if (!fence->external) |
| 1121 |
return; |
| 1122 |
|
| 1123 |
sync_accumulate("virgl", &cbuf->in_fence_fd, fence->fd); |
| 1124 |
} |
| 1125 |
|
| 1126 |
static int virgl_fence_get_fd(struct virgl_winsys *vws, |
| 1127 |
struct pipe_fence_handle *_fence) |
| 1128 |
{ |
| 1129 |
struct virgl_drm_fence *fence = virgl_drm_fence(_fence); |
| 1130 |
|
| 1131 |
if (!vws->supports_fences) |
| 1132 |
return -1; |
| 1133 |
|
| 1134 |
return os_dupfd_cloexec(fence->fd); |
| 1135 |
} |
| 1136 |
|
| 1137 |
static int virgl_drm_get_version(int fd) |
| 1138 |
{ |
| 1139 |
int ret; |
| 1140 |
drmVersionPtr version; |
| 1141 |
|
| 1142 |
version = drmGetVersion(fd); |
| 1143 |
|
| 1144 |
if (!version) |
| 1145 |
ret = -EFAULT; |
| 1146 |
else if (version->version_major != 0) |
| 1147 |
ret = -EINVAL; |
| 1148 |
else |
| 1149 |
ret = VIRGL_DRM_VERSION(0, version->version_minor); |
| 1150 |
|
| 1151 |
drmFreeVersion(version); |
| 1152 |
|
| 1153 |
return ret; |
| 1154 |
} |
| 1155 |
|
| 1156 |
static bool |
| 1157 |
virgl_drm_resource_cache_entry_is_busy(struct virgl_resource_cache_entry *entry, |
| 1158 |
void *user_data) |
| 1159 |
{ |
| 1160 |
struct virgl_drm_winsys *qdws = user_data; |
| 1161 |
struct virgl_hw_res *res = cache_entry_container_res(entry); |
| 1162 |
|
| 1163 |
return virgl_drm_resource_is_busy(&qdws->base, res); |
| 1164 |
} |
| 1165 |
|
| 1166 |
static void |
| 1167 |
virgl_drm_resource_cache_entry_release(struct virgl_resource_cache_entry *entry, |
| 1168 |
void *user_data) |
| 1169 |
{ |
| 1170 |
struct virgl_drm_winsys *qdws = user_data; |
| 1171 |
struct virgl_hw_res *res = cache_entry_container_res(entry); |
| 1172 |
|
| 1173 |
virgl_hw_res_destroy(qdws, res); |
| 1174 |
} |
| 1175 |
|
| 1176 |
static int virgl_init_context(int drmFD) |
| 1177 |
{ |
| 1178 |
int ret; |
| 1179 |
struct drm_virtgpu_context_init init = { 0 }; |
| 1180 |
struct drm_virtgpu_context_set_param ctx_set_param = { 0 }; |
| 1181 |
uint64_t supports_capset_virgl, supports_capset_virgl2; |
| 1182 |
supports_capset_virgl = supports_capset_virgl2 = 0; |
| 1183 |
|
| 1184 |
supports_capset_virgl = ((1 << VIRTGPU_DRM_CAPSET_VIRGL) & |
| 1185 |
params[param_supported_capset_ids].value); |
| 1186 |
|
| 1187 |
supports_capset_virgl2 = ((1 << VIRTGPU_DRM_CAPSET_VIRGL2) & |
| 1188 |
params[param_supported_capset_ids].value); |
| 1189 |
|
| 1190 |
if (!supports_capset_virgl && !supports_capset_virgl2) { |
| 1191 |
_debug_printf("No virgl contexts available on host"); |
| 1192 |
return -EINVAL; |
| 1193 |
} |
| 1194 |
|
| 1195 |
ctx_set_param.param = VIRTGPU_CONTEXT_PARAM_CAPSET_ID; |
| 1196 |
ctx_set_param.value = (supports_capset_virgl2) ? |
| 1197 |
VIRTGPU_DRM_CAPSET_VIRGL2 : |
| 1198 |
VIRTGPU_DRM_CAPSET_VIRGL; |
| 1199 |
|
| 1200 |
init.ctx_set_params = (unsigned long)(void *)&ctx_set_param; |
| 1201 |
init.num_params = 1; |
| 1202 |
|
| 1203 |
ret = drmIoctl(drmFD, DRM_IOCTL_VIRTGPU_CONTEXT_INIT, &init); |
| 1204 |
/* |
| 1205 |
* EEXIST happens when a compositor does DUMB_CREATE before initializing |
| 1206 |
* virgl. |
| 1207 |
*/ |
| 1208 |
if (ret && errno != EEXIST) { |
| 1209 |
_debug_printf("DRM_IOCTL_VIRTGPU_CONTEXT_INIT failed with %s\n", |
| 1210 |
strerror(errno)); |
| 1211 |
return -1; |
| 1212 |
} |
| 1213 |
|
| 1214 |
return 0; |
| 1215 |
} |
| 1216 |
|
| 1217 |
static int |
| 1218 |
virgl_drm_winsys_get_fd(struct virgl_winsys *vws) |
| 1219 |
{ |
| 1220 |
struct virgl_drm_winsys *vdws = virgl_drm_winsys(vws); |
| 1221 |
|
| 1222 |
return vdws->fd; |
| 1223 |
} |
| 1224 |
|
| 1225 |
static struct virgl_winsys * |
| 1226 |
virgl_drm_winsys_create(int drmFD) |
| 1227 |
{ |
| 1228 |
static const unsigned CACHE_TIMEOUT_USEC = 1000000; |
| 1229 |
struct virgl_drm_winsys *qdws; |
| 1230 |
int drm_version; |
| 1231 |
int ret; |
| 1232 |
|
| 1233 |
for (uint32_t i = 0; i < ARRAY_SIZE(params); i++) { |
| 1234 |
struct drm_virtgpu_getparam getparam = { 0 }; |
| 1235 |
uint64_t value = 0; |
| 1236 |
getparam.param = params[i].param; |
| 1237 |
getparam.value = (uint64_t)(uintptr_t)&value; |
| 1238 |
ret = drmIoctl(drmFD, DRM_IOCTL_VIRTGPU_GETPARAM, &getparam); |
| 1239 |
params[i].value = (ret == 0) ? value : 0; |
| 1240 |
} |
| 1241 |
|
| 1242 |
if (!params[param_3d_features].value) |
| 1243 |
return NULL; |
| 1244 |
|
| 1245 |
drm_version = virgl_drm_get_version(drmFD); |
| 1246 |
if (drm_version < 0) |
| 1247 |
return NULL; |
| 1248 |
|
| 1249 |
if (params[param_context_init].value) { |
| 1250 |
ret = virgl_init_context(drmFD); |
| 1251 |
if (ret) |
| 1252 |
return NULL; |
| 1253 |
} |
| 1254 |
|
| 1255 |
qdws = CALLOC_STRUCT(virgl_drm_winsys); |
| 1256 |
if (!qdws) |
| 1257 |
return NULL; |
| 1258 |
|
| 1259 |
qdws->fd = drmFD; |
| 1260 |
virgl_resource_cache_init(&qdws->cache, CACHE_TIMEOUT_USEC, |
| 1261 |
virgl_drm_resource_cache_entry_is_busy, |
| 1262 |
virgl_drm_resource_cache_entry_release, |
| 1263 |
qdws); |
| 1264 |
(void) mtx_init(&qdws->mutex, mtx_plain); |
| 1265 |
(void) mtx_init(&qdws->bo_handles_mutex, mtx_plain); |
| 1266 |
p_atomic_set(&qdws->blob_id, 0); |
| 1267 |
|
| 1268 |
qdws->bo_handles = util_hash_table_create_ptr_keys(); |
| 1269 |
qdws->bo_names = util_hash_table_create_ptr_keys(); |
| 1270 |
qdws->base.destroy = virgl_drm_winsys_destroy; |
| 1271 |
|
| 1272 |
qdws->base.transfer_put = virgl_bo_transfer_put; |
| 1273 |
qdws->base.transfer_get = virgl_bo_transfer_get; |
| 1274 |
qdws->base.resource_create = virgl_drm_winsys_resource_cache_create; |
| 1275 |
qdws->base.resource_reference = virgl_drm_resource_reference; |
| 1276 |
qdws->base.resource_create_from_handle = virgl_drm_winsys_resource_create_handle; |
| 1277 |
qdws->base.resource_set_type = virgl_drm_winsys_resource_set_type; |
| 1278 |
qdws->base.resource_get_handle = virgl_drm_winsys_resource_get_handle; |
| 1279 |
qdws->base.resource_get_storage_size = virgl_drm_winsys_resource_get_storage_size; |
| 1280 |
qdws->base.resource_map = virgl_drm_resource_map; |
| 1281 |
qdws->base.resource_wait = virgl_drm_resource_wait; |
| 1282 |
qdws->base.resource_is_busy = virgl_drm_resource_is_busy; |
| 1283 |
qdws->base.cmd_buf_create = virgl_drm_cmd_buf_create; |
| 1284 |
qdws->base.cmd_buf_destroy = virgl_drm_cmd_buf_destroy; |
| 1285 |
qdws->base.submit_cmd = virgl_drm_winsys_submit_cmd; |
| 1286 |
qdws->base.emit_res = virgl_drm_emit_res; |
| 1287 |
qdws->base.res_is_referenced = virgl_drm_res_is_ref; |
| 1288 |
|
| 1289 |
qdws->base.cs_create_fence = virgl_cs_create_fence; |
| 1290 |
qdws->base.fence_wait = virgl_fence_wait; |
| 1291 |
qdws->base.fence_reference = virgl_fence_reference; |
| 1292 |
qdws->base.fence_server_sync = virgl_fence_server_sync; |
| 1293 |
qdws->base.fence_get_fd = virgl_fence_get_fd; |
| 1294 |
qdws->base.get_caps = virgl_drm_get_caps; |
| 1295 |
qdws->base.get_fd = virgl_drm_winsys_get_fd; |
| 1296 |
qdws->base.supports_fences = drm_version >= VIRGL_DRM_VERSION_FENCE_FD; |
| 1297 |
qdws->base.supports_encoded_transfers = 1; |
| 1298 |
|
| 1299 |
qdws->base.supports_coherent = params[param_resource_blob].value && |
| 1300 |
params[param_host_visible].value; |
| 1301 |
return &qdws->base; |
| 1302 |
|
| 1303 |
} |
| 1304 |
|
| 1305 |
static struct hash_table *fd_tab = NULL; |
| 1306 |
static simple_mtx_t virgl_screen_mutex = SIMPLE_MTX_INITIALIZER; |
| 1307 |
|
| 1308 |
static void |
| 1309 |
virgl_drm_screen_destroy(struct pipe_screen *pscreen) |
| 1310 |
{ |
| 1311 |
struct virgl_screen *screen = virgl_screen(pscreen); |
| 1312 |
bool destroy; |
| 1313 |
|
| 1314 |
simple_mtx_lock(&virgl_screen_mutex); |
| 1315 |
destroy = --screen->refcnt == 0; |
| 1316 |
if (destroy) { |
| 1317 |
int fd = virgl_drm_winsys(screen->vws)->fd; |
| 1318 |
_mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(fd)); |
| 1319 |
close(fd); |
| 1320 |
} |
| 1321 |
simple_mtx_unlock(&virgl_screen_mutex); |
| 1322 |
|
| 1323 |
if (destroy) { |
| 1324 |
pscreen->destroy = screen->winsys_priv; |
| 1325 |
pscreen->destroy(pscreen); |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
static uint32_t |
| 1330 |
hash_fd(const void *key) |
| 1331 |
{ |
| 1332 |
int fd = pointer_to_intptr(key); |
| 1333 |
|
| 1334 |
return _mesa_hash_int(&fd); |
| 1335 |
} |
| 1336 |
|
| 1337 |
static bool |
| 1338 |
equal_fd(const void *key1, const void *key2) |
| 1339 |
{ |
| 1340 |
int ret; |
| 1341 |
int fd1 = pointer_to_intptr(key1); |
| 1342 |
int fd2 = pointer_to_intptr(key2); |
| 1343 |
|
| 1344 |
/* Since the scope of prime handle is limited to drm_file, |
| 1345 |
* virgl_screen is only shared at the drm_file level, |
| 1346 |
* not at the device (/dev/dri/cardX) level. |
| 1347 |
*/ |
| 1348 |
ret = os_same_file_description(fd1, fd2); |
| 1349 |
if (ret == 0) { |
| 1350 |
return true; |
| 1351 |
} else if (ret < 0) { |
| 1352 |
static bool logged; |
| 1353 |
|
| 1354 |
if (!logged) { |
| 1355 |
_debug_printf("virgl: os_same_file_description couldn't " |
| 1356 |
"determine if two DRM fds reference the same " |
| 1357 |
"file description.\n" |
| 1358 |
"If they do, bad things may happen!\n"); |
| 1359 |
logged = true; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
return false; |
| 1364 |
} |
| 1365 |
|
| 1366 |
struct pipe_screen * |
| 1367 |
virgl_drm_screen_create(int fd, const struct pipe_screen_config *config) |
| 1368 |
{ |
| 1369 |
struct pipe_screen *pscreen = NULL; |
| 1370 |
|
| 1371 |
simple_mtx_lock(&virgl_screen_mutex); |
| 1372 |
if (!fd_tab) { |
| 1373 |
fd_tab = _mesa_hash_table_create(NULL, hash_fd, equal_fd); |
| 1374 |
if (!fd_tab) |
| 1375 |
goto unlock; |
| 1376 |
} |
| 1377 |
|
| 1378 |
pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(fd)); |
| 1379 |
if (pscreen) { |
| 1380 |
virgl_screen(pscreen)->refcnt++; |
| 1381 |
} else { |
| 1382 |
struct virgl_winsys *vws; |
| 1383 |
int dup_fd = os_dupfd_cloexec(fd); |
| 1384 |
if (dup_fd < 0) |
| 1385 |
goto unlock; |
| 1386 |
|
| 1387 |
vws = virgl_drm_winsys_create(dup_fd); |
| 1388 |
if (!vws) { |
| 1389 |
close(dup_fd); |
| 1390 |
goto unlock; |
| 1391 |
} |
| 1392 |
|
| 1393 |
pscreen = virgl_create_screen(vws, config); |
| 1394 |
if (pscreen) { |
| 1395 |
_mesa_hash_table_insert(fd_tab, intptr_to_pointer(dup_fd), pscreen); |
| 1396 |
|
| 1397 |
/* Bit of a hack, to avoid circular linkage dependency, |
| 1398 |
* ie. pipe driver having to call in to winsys, we |
| 1399 |
* override the pipe drivers screen->destroy(): |
| 1400 |
*/ |
| 1401 |
virgl_screen(pscreen)->winsys_priv = pscreen->destroy; |
| 1402 |
pscreen->destroy = virgl_drm_screen_destroy; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
unlock: |
| 1407 |
simple_mtx_unlock(&virgl_screen_mutex); |
| 1408 |
return pscreen; |
| 1409 |
} |