| 1 |
/* |
| 2 |
* Copyright (c) 2009-2024 Broadcom. All Rights Reserved. |
| 3 |
* The term “Broadcom” refers to Broadcom Inc. |
| 4 |
* and/or its subsidiaries. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
#include "vmw_screen.h" |
| 10 |
#include "vmw_fence.h" |
| 11 |
#include "vmw_context.h" |
| 12 |
#include "vmwgfx_drm.h" |
| 13 |
#include "xf86drm.h" |
| 14 |
|
| 15 |
#include "util/os_file.h" |
| 16 |
#include "util/u_memory.h" |
| 17 |
#include "util/compiler.h" |
| 18 |
#include "util/u_hash_table.h" |
| 19 |
#ifdef MAJOR_IN_MKDEV |
| 20 |
#include <sys/mkdev.h> |
| 21 |
#endif |
| 22 |
#ifdef MAJOR_IN_SYSMACROS |
| 23 |
#include <sys/sysmacros.h> |
| 24 |
#endif |
| 25 |
#include <sys/stat.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <sys/ioctl.h> |
| 29 |
#include <sys/mman.h> |
| 30 |
|
| 31 |
static struct hash_table *dev_hash = NULL; |
| 32 |
|
| 33 |
static bool vmw_dev_compare(const void *key1, const void *key2) |
| 34 |
{ |
| 35 |
return (major(*(dev_t *)key1) == major(*(dev_t *)key2) && |
| 36 |
minor(*(dev_t *)key1) == minor(*(dev_t *)key2)); |
| 37 |
} |
| 38 |
|
| 39 |
static uint32_t vmw_dev_hash(const void *key) |
| 40 |
{ |
| 41 |
return (major(*(dev_t *) key) << 16) | minor(*(dev_t *) key); |
| 42 |
} |
| 43 |
|
| 44 |
#ifdef VMX86_STATS |
| 45 |
/** |
| 46 |
* Initializes mksstat TLS store. |
| 47 |
*/ |
| 48 |
static void |
| 49 |
vmw_winsys_screen_init_mksstat(struct vmw_winsys_screen *vws) |
| 50 |
{ |
| 51 |
size_t i; |
| 52 |
|
| 53 |
for (i = 0; i < ARRAY_SIZE(vws->mksstat_tls); ++i) { |
| 54 |
vws->mksstat_tls[i].stat_pages = NULL; |
| 55 |
vws->mksstat_tls[i].stat_id = -1UL; |
| 56 |
vws->mksstat_tls[i].pid = 0; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Deinits mksstat TLS store. |
| 62 |
*/ |
| 63 |
static void |
| 64 |
vmw_winsys_screen_deinit_mksstat(struct vmw_winsys_screen *vws) |
| 65 |
{ |
| 66 |
size_t i; |
| 67 |
|
| 68 |
for (i = 0; i < ARRAY_SIZE(vws->mksstat_tls); ++i) { |
| 69 |
uint32_t expected = __atomic_load_n(&vws->mksstat_tls[i].pid, __ATOMIC_ACQUIRE); |
| 70 |
|
| 71 |
if (expected == -1U) { |
| 72 |
fprintf(stderr, "%s encountered locked mksstat TLS entry at index %lu.\n", __func__, i); |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
if (expected == 0) |
| 77 |
continue; |
| 78 |
|
| 79 |
if (__atomic_compare_exchange_n(&vws->mksstat_tls[i].pid, &expected, 0, false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { |
| 80 |
struct drm_vmw_mksstat_remove_arg arg = { |
| 81 |
.id = vws->mksstat_tls[i].stat_id |
| 82 |
}; |
| 83 |
|
| 84 |
assert(vws->mksstat_tls[i].stat_pages); |
| 85 |
assert(vws->mksstat_tls[i].stat_id != -1UL); |
| 86 |
|
| 87 |
if (drmCommandWrite(vws->ioctl.drm_fd, DRM_VMW_MKSSTAT_REMOVE, &arg, sizeof(arg))) { |
| 88 |
fprintf(stderr, "%s could not ioctl: %s\n", __func__, strerror(errno)); |
| 89 |
} else if (munmap(vws->mksstat_tls[i].stat_pages, vmw_svga_winsys_stats_len())) { |
| 90 |
fprintf(stderr, "%s could not munmap: %s\n", __func__, strerror(errno)); |
| 91 |
} |
| 92 |
} else { |
| 93 |
fprintf(stderr, "%s encountered volatile mksstat TLS entry at index %lu.\n", __func__, i); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
#endif |
| 99 |
/* Called from vmw_drm_create_screen(), creates and initializes the |
| 100 |
* vmw_winsys_screen structure, which is the main entity in this |
| 101 |
* module. |
| 102 |
* First, check whether a vmw_winsys_screen object already exists for |
| 103 |
* this device, and in that case return that one, making sure that we |
| 104 |
* have our own file descriptor open to DRM. |
| 105 |
*/ |
| 106 |
|
| 107 |
struct vmw_winsys_screen * |
| 108 |
vmw_winsys_create( int fd ) |
| 109 |
{ |
| 110 |
struct vmw_winsys_screen *vws; |
| 111 |
struct stat stat_buf; |
| 112 |
const char *getenv_val; |
| 113 |
|
| 114 |
if (dev_hash == NULL) { |
| 115 |
dev_hash = _mesa_hash_table_create(NULL, vmw_dev_hash, vmw_dev_compare); |
| 116 |
if (dev_hash == NULL) |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
if (fstat(fd, &stat_buf)) |
| 121 |
return NULL; |
| 122 |
|
| 123 |
vws = util_hash_table_get(dev_hash, &stat_buf.st_rdev); |
| 124 |
if (vws) { |
| 125 |
vws->open_count++; |
| 126 |
return vws; |
| 127 |
} |
| 128 |
|
| 129 |
vws = CALLOC_STRUCT(vmw_winsys_screen); |
| 130 |
if (!vws) |
| 131 |
goto out_no_vws; |
| 132 |
|
| 133 |
vws->device = stat_buf.st_rdev; |
| 134 |
vws->open_count = 1; |
| 135 |
vws->ioctl.drm_fd = os_dupfd_cloexec(fd); |
| 136 |
vws->force_coherent = false; |
| 137 |
if (!vmw_ioctl_init(vws)) |
| 138 |
goto out_no_ioctl; |
| 139 |
|
| 140 |
vws->base.have_gb_dma = !vws->force_coherent; |
| 141 |
vws->base.max_mob_memory_mib = vws->ioctl.max_mob_memory >> 20; |
| 142 |
vws->base.need_to_rebind_resources = false; |
| 143 |
vws->base.have_transfer_from_buffer_cmd = vws->base.have_vgpu10; |
| 144 |
vws->base.have_constant_buffer_offset_cmd = |
| 145 |
vws->ioctl.have_drm_2_20 && vws->base.have_sm5; |
| 146 |
vws->base.have_index_vertex_buffer_offset_cmd = false; |
| 147 |
vws->base.have_rasterizer_state_v2_cmd = |
| 148 |
vws->ioctl.have_drm_2_20 && vws->base.have_sm5; |
| 149 |
|
| 150 |
getenv_val = os_get_option("SVGA_FORCE_KERNEL_UNMAPS"); |
| 151 |
vws->cache_maps = !getenv_val || strcmp(getenv_val, "0") == 0; |
| 152 |
vws->fence_ops = vmw_fence_ops_create(vws); |
| 153 |
if (!vws->fence_ops) |
| 154 |
goto out_no_fence_ops; |
| 155 |
|
| 156 |
if (!vmw_pools_init(vws)) |
| 157 |
goto out_no_pools; |
| 158 |
|
| 159 |
if (!vmw_winsys_screen_init_svga(vws)) |
| 160 |
goto out_no_svga; |
| 161 |
|
| 162 |
#ifdef VMX86_STATS |
| 163 |
vmw_winsys_screen_init_mksstat(vws); |
| 164 |
#endif |
| 165 |
_mesa_hash_table_insert(dev_hash, &vws->device, vws); |
| 166 |
|
| 167 |
cnd_init(&vws->cs_cond); |
| 168 |
mtx_init(&vws->cs_mutex, mtx_plain); |
| 169 |
|
| 170 |
return vws; |
| 171 |
out_no_svga: |
| 172 |
vmw_pools_cleanup(vws); |
| 173 |
out_no_pools: |
| 174 |
vws->fence_ops->destroy(vws->fence_ops); |
| 175 |
out_no_fence_ops: |
| 176 |
vmw_ioctl_cleanup(vws); |
| 177 |
out_no_ioctl: |
| 178 |
close(vws->ioctl.drm_fd); |
| 179 |
FREE(vws); |
| 180 |
out_no_vws: |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
void |
| 185 |
vmw_winsys_destroy(struct vmw_winsys_screen *vws) |
| 186 |
{ |
| 187 |
if (--vws->open_count == 0) { |
| 188 |
if (vws->swc) |
| 189 |
vmw_swc_unref(vws->swc); |
| 190 |
_mesa_hash_table_remove_key(dev_hash, &vws->device); |
| 191 |
vmw_pools_cleanup(vws); |
| 192 |
vws->fence_ops->destroy(vws->fence_ops); |
| 193 |
vmw_ioctl_cleanup(vws); |
| 194 |
#ifdef VMX86_STATS |
| 195 |
vmw_winsys_screen_deinit_mksstat(vws); |
| 196 |
#endif |
| 197 |
close(vws->ioctl.drm_fd); |
| 198 |
mtx_destroy(&vws->cs_mutex); |
| 199 |
cnd_destroy(&vws->cs_cond); |
| 200 |
FREE(vws); |
| 201 |
} |
| 202 |
} |