| 1 |
/* |
| 2 |
* Copyright © 2021 Collabora Ltd. |
| 3 |
* |
| 4 |
* Derived from tu_device.c which is: |
| 5 |
* Copyright © 2016 Red Hat. |
| 6 |
* Copyright © 2016 Bas Nieuwenhuizen |
| 7 |
* Copyright © 2015 Intel Corporation |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: MIT |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "util/build_id.h" |
| 13 |
#include "util/driconf.h" |
| 14 |
#include "util/mesa-blake3.h" |
| 15 |
#include "util/os_misc.h" |
| 16 |
#include "util/u_call_once.h" |
| 17 |
|
| 18 |
#include "vk_alloc.h" |
| 19 |
#include "vk_log.h" |
| 20 |
|
| 21 |
#include "panvk_entrypoints.h" |
| 22 |
#include "panvk_instance.h" |
| 23 |
#include "panvk_macros.h" |
| 24 |
#include "panvk_physical_device.h" |
| 25 |
|
| 26 |
#ifdef HAVE_VALGRIND |
| 27 |
#include <memcheck.h> |
| 28 |
#include <valgrind.h> |
| 29 |
#define VG(x) x |
| 30 |
#else |
| 31 |
#define VG(x) |
| 32 |
#endif |
| 33 |
|
| 34 |
static const struct debug_control panvk_debug_options[] = { |
| 35 |
{"startup", PANVK_DEBUG_STARTUP}, |
| 36 |
{"nir", PANVK_DEBUG_NIR}, |
| 37 |
{"trace", PANVK_DEBUG_TRACE}, |
| 38 |
{"sync", PANVK_DEBUG_SYNC}, |
| 39 |
{"noafbc", PANVK_DEBUG_NO_AFBC}, |
| 40 |
{"linear", PANVK_DEBUG_LINEAR}, |
| 41 |
{"dump", PANVK_DEBUG_DUMP}, |
| 42 |
{"cs", PANVK_DEBUG_CS}, |
| 43 |
{"copy_gfx", PANVK_DEBUG_COPY_GFX}, |
| 44 |
{"force_simultaneous", PANVK_DEBUG_FORCE_SIMULTANEOUS}, |
| 45 |
{"implicit_others_inv", PANVK_DEBUG_IMPLICIT_OTHERS_INV}, |
| 46 |
{"force_blackhole", PANVK_DEBUG_FORCE_BLACKHOLE}, |
| 47 |
{"wsi_afbc", PANVK_DEBUG_WSI_AFBC}, |
| 48 |
{"no_wb_mmap", PANVK_DEBUG_NO_WB_MMAP}, |
| 49 |
{"no_user_mmap_sync", PANVK_DEBUG_NO_USER_MMAP_SYNC}, |
| 50 |
{"coherent_before_cached", PANVK_DEBUG_COHERENT_BEFORE_CACHED}, |
| 51 |
{"no_extended_va_range", PANVK_DEBUG_NO_EXTENDED_VA_RANGE}, |
| 52 |
{"hsr_prepass", PANVK_DEBUG_HSR_PREPASS}, |
| 53 |
{NULL, 0}, |
| 54 |
}; |
| 55 |
|
| 56 |
uint64_t panvk_debug; |
| 57 |
|
| 58 |
static void |
| 59 |
panvk_debug_init_once(void) |
| 60 |
{ |
| 61 |
panvk_debug = |
| 62 |
parse_debug_string(os_get_option("PANVK_DEBUG"), panvk_debug_options); |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
panvk_debug_init(void) |
| 67 |
{ |
| 68 |
static once_flag once = ONCE_FLAG_INIT; |
| 69 |
call_once(&once, panvk_debug_init_once); |
| 70 |
|
| 71 |
/* log per VkInstance creation */ |
| 72 |
if (PANVK_DEBUG(STARTUP)) { |
| 73 |
char debug_string[256]; |
| 74 |
dump_debug_control_string(debug_string, sizeof(debug_string), |
| 75 |
panvk_debug_options, panvk_debug); |
| 76 |
mesa_logi("panvk_debug: %s", debug_string); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 81 |
panvk_EnumerateInstanceVersion(uint32_t *pApiVersion) |
| 82 |
{ |
| 83 |
uint32_t version_override = vk_get_version_override(); |
| 84 |
*pApiVersion = version_override ? version_override : |
| 85 |
VK_MAKE_API_VERSION(0, 1, 4, VK_HEADER_VERSION); |
| 86 |
|
| 87 |
return VK_SUCCESS; |
| 88 |
} |
| 89 |
|
| 90 |
static const struct vk_instance_extension_table panvk_instance_extensions = { |
| 91 |
.KHR_device_group_creation = true, |
| 92 |
.KHR_external_memory_capabilities = true, |
| 93 |
.KHR_external_semaphore_capabilities = true, |
| 94 |
.KHR_external_fence_capabilities = true, |
| 95 |
.KHR_get_physical_device_properties2 = true, |
| 96 |
#ifdef VK_USE_PLATFORM_DISPLAY_KHR |
| 97 |
.KHR_get_display_properties2 = true, |
| 98 |
#endif |
| 99 |
#ifdef PANVK_USE_WSI_PLATFORM |
| 100 |
.KHR_get_surface_capabilities2 = true, |
| 101 |
.KHR_surface = true, |
| 102 |
.KHR_surface_maintenance1 = true, |
| 103 |
.EXT_surface_maintenance1 = true, |
| 104 |
#endif |
| 105 |
#ifdef VK_USE_PLATFORM_DISPLAY_KHR |
| 106 |
.KHR_display = true, |
| 107 |
.EXT_acquire_drm_display = true, |
| 108 |
.EXT_direct_mode_display = true, |
| 109 |
.EXT_display_surface_counter = true, |
| 110 |
#endif |
| 111 |
#ifdef VK_USE_PLATFORM_WAYLAND_KHR |
| 112 |
.KHR_wayland_surface = true, |
| 113 |
#endif |
| 114 |
#ifdef VK_USE_PLATFORM_XCB_KHR |
| 115 |
.KHR_xcb_surface = true, |
| 116 |
#endif |
| 117 |
#ifdef VK_USE_PLATFORM_XLIB_KHR |
| 118 |
.KHR_xlib_surface = true, |
| 119 |
#endif |
| 120 |
#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT |
| 121 |
.EXT_acquire_xlib_display = true, |
| 122 |
#endif |
| 123 |
.EXT_debug_report = true, |
| 124 |
.EXT_debug_utils = true, |
| 125 |
#ifndef VK_USE_PLATFORM_WIN32_KHR |
| 126 |
.EXT_headless_surface = true, |
| 127 |
#endif |
| 128 |
}; |
| 129 |
|
| 130 |
static VkResult |
| 131 |
panvk_physical_device_try_create(struct vk_instance *vk_instance, |
| 132 |
struct _drmDevice *drm_device, |
| 133 |
struct vk_physical_device **out) |
| 134 |
{ |
| 135 |
struct panvk_instance *instance = |
| 136 |
container_of(vk_instance, struct panvk_instance, vk); |
| 137 |
|
| 138 |
if (!(drm_device->available_nodes & (1 << DRM_NODE_RENDER)) || |
| 139 |
drm_device->bustype != DRM_BUS_PLATFORM) |
| 140 |
return VK_ERROR_INCOMPATIBLE_DRIVER; |
| 141 |
|
| 142 |
struct panvk_physical_device *device = |
| 143 |
vk_zalloc(&instance->vk.alloc, sizeof(*device), 8, |
| 144 |
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
| 145 |
if (!device) |
| 146 |
return panvk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 147 |
|
| 148 |
VkResult result = panvk_physical_device_init(device, instance, drm_device); |
| 149 |
if (result != VK_SUCCESS) { |
| 150 |
vk_free(&instance->vk.alloc, device); |
| 151 |
return result; |
| 152 |
} |
| 153 |
|
| 154 |
*out = &device->vk; |
| 155 |
return VK_SUCCESS; |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
panvk_destroy_physical_device(struct vk_physical_device *device) |
| 160 |
{ |
| 161 |
panvk_physical_device_finish((struct panvk_physical_device *)device); |
| 162 |
vk_free(&device->instance->alloc, device); |
| 163 |
} |
| 164 |
|
| 165 |
static void * |
| 166 |
panvk_kmod_zalloc(const struct pan_kmod_allocator *allocator, size_t size, |
| 167 |
bool transient) |
| 168 |
{ |
| 169 |
const VkAllocationCallbacks *vkalloc = allocator->priv; |
| 170 |
|
| 171 |
void *obj = vk_zalloc(vkalloc, size, 8, |
| 172 |
transient ? VK_SYSTEM_ALLOCATION_SCOPE_COMMAND |
| 173 |
: VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
| 174 |
|
| 175 |
/* We force errno to -ENOMEM on host allocation failures so we can properly |
| 176 |
* report it back as VK_ERROR_OUT_OF_HOST_MEMORY. */ |
| 177 |
if (!obj) |
| 178 |
errno = -ENOMEM; |
| 179 |
|
| 180 |
return obj; |
| 181 |
} |
| 182 |
|
| 183 |
static void |
| 184 |
panvk_kmod_free(const struct pan_kmod_allocator *allocator, void *data) |
| 185 |
{ |
| 186 |
const VkAllocationCallbacks *vkalloc = allocator->priv; |
| 187 |
|
| 188 |
return vk_free(vkalloc, data); |
| 189 |
} |
| 190 |
|
| 191 |
static const driOptionDescription panvk_dri_options[] = { |
| 192 |
DRI_CONF_SECTION_PERFORMANCE |
| 193 |
DRI_CONF_ADAPTIVE_SYNC(true) |
| 194 |
DRI_CONF_VK_X11_OVERRIDE_MIN_IMAGE_COUNT(0) |
| 195 |
DRI_CONF_VK_X11_STRICT_IMAGE_COUNT(false) |
| 196 |
DRI_CONF_VK_X11_ENSURE_MIN_IMAGE_COUNT(false) |
| 197 |
DRI_CONF_VK_XWAYLAND_WAIT_READY(false) |
| 198 |
DRI_CONF_SECTION_END |
| 199 |
|
| 200 |
DRI_CONF_SECTION_DEBUG |
| 201 |
DRI_CONF_FORCE_VK_VENDOR() |
| 202 |
DRI_CONF_VK_WSI_FORCE_SWAPCHAIN_TO_CURRENT_EXTENT(false) |
| 203 |
DRI_CONF_VK_X11_IGNORE_SUBOPTIMAL(false) |
| 204 |
DRI_CONF_SECTION_END |
| 205 |
|
| 206 |
DRI_CONF_SECTION_MISCELLANEOUS |
| 207 |
DRI_CONF_PAN_COMPUTE_CORE_MASK(~0ull) |
| 208 |
DRI_CONF_PAN_FRAGMENT_CORE_MASK(~0ull) |
| 209 |
DRI_CONF_PAN_ENABLE_VERTEX_PIPELINE_STORES_ATOMICS(false) |
| 210 |
DRI_CONF_PAN_FORCE_ENABLE_SHADER_ATOMICS(false) |
| 211 |
DRI_CONF_SECTION_END |
| 212 |
}; |
| 213 |
|
| 214 |
static void |
| 215 |
panvk_init_dri_options(struct panvk_instance *instance) |
| 216 |
{ |
| 217 |
driParseOptionInfo(&instance->available_dri_options, panvk_dri_options, ARRAY_SIZE(panvk_dri_options)); |
| 218 |
driParseConfigFiles(&instance->dri_options, &instance->available_dri_options, 0, "panvk", NULL, NULL, |
| 219 |
instance->vk.app_info.app_name, instance->vk.app_info.app_version, |
| 220 |
instance->vk.app_info.engine_name, instance->vk.app_info.engine_version); |
| 221 |
|
| 222 |
instance->force_vk_vendor = |
| 223 |
driQueryOptioni(&instance->dri_options, "force_vk_vendor"); |
| 224 |
|
| 225 |
instance->enable_vertex_pipeline_stores_atomics = driQueryOptionb( |
| 226 |
&instance->dri_options, "pan_enable_vertex_pipeline_stores_atomics"); |
| 227 |
instance->force_enable_shader_atomics = driQueryOptionb( |
| 228 |
&instance->dri_options, "pan_force_enable_shader_atomics"); |
| 229 |
} |
| 230 |
|
| 231 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 232 |
panvk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, |
| 233 |
const VkAllocationCallbacks *pAllocator, |
| 234 |
VkInstance *pInstance) |
| 235 |
{ |
| 236 |
struct panvk_instance *instance; |
| 237 |
VkResult result; |
| 238 |
|
| 239 |
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO); |
| 240 |
|
| 241 |
panvk_debug_init(); |
| 242 |
|
| 243 |
const struct build_id_note *note = |
| 244 |
build_id_find_nhdr_for_addr(panvk_CreateInstance); |
| 245 |
if (!note) { |
| 246 |
return panvk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED, |
| 247 |
"Failed to find build-id"); |
| 248 |
} |
| 249 |
|
| 250 |
unsigned build_id_len = build_id_length(note); |
| 251 |
if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) { |
| 252 |
return panvk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED, |
| 253 |
"build-id too short. It needs to be a SHA"); |
| 254 |
} |
| 255 |
|
| 256 |
pAllocator = pAllocator ?: vk_default_allocator(); |
| 257 |
instance = vk_zalloc(pAllocator, sizeof(*instance), 8, |
| 258 |
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); |
| 259 |
if (!instance) |
| 260 |
return panvk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 261 |
|
| 262 |
struct vk_instance_dispatch_table dispatch_table; |
| 263 |
|
| 264 |
vk_instance_dispatch_table_from_entrypoints( |
| 265 |
&dispatch_table, &panvk_instance_entrypoints, true); |
| 266 |
vk_instance_dispatch_table_from_entrypoints( |
| 267 |
&dispatch_table, &wsi_instance_entrypoints, false); |
| 268 |
result = vk_instance_init(&instance->vk, &panvk_instance_extensions, |
| 269 |
&dispatch_table, pCreateInfo, pAllocator); |
| 270 |
if (result != VK_SUCCESS) { |
| 271 |
vk_free(pAllocator, instance); |
| 272 |
return panvk_error(NULL, result); |
| 273 |
} |
| 274 |
|
| 275 |
panvk_init_dri_options(instance); |
| 276 |
|
| 277 |
instance->kmod.allocator = (struct pan_kmod_allocator){ |
| 278 |
.zalloc = panvk_kmod_zalloc, |
| 279 |
.free = panvk_kmod_free, |
| 280 |
.priv = &instance->vk.alloc, |
| 281 |
}; |
| 282 |
|
| 283 |
instance->vk.physical_devices.try_create_for_drm = |
| 284 |
panvk_physical_device_try_create; |
| 285 |
instance->vk.physical_devices.destroy = panvk_destroy_physical_device; |
| 286 |
|
| 287 |
if (PANVK_DEBUG(STARTUP)) |
| 288 |
mesa_logi("Created an instance"); |
| 289 |
|
| 290 |
VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false)); |
| 291 |
|
| 292 |
STATIC_ASSERT(sizeof(instance->driver_build_sha) == BLAKE3_KEY_LEN); |
| 293 |
copy_build_id_to_sha1(instance->driver_build_sha, note); |
| 294 |
|
| 295 |
*pInstance = panvk_instance_to_handle(instance); |
| 296 |
|
| 297 |
return VK_SUCCESS; |
| 298 |
} |
| 299 |
|
| 300 |
VKAPI_ATTR void VKAPI_CALL |
| 301 |
panvk_DestroyInstance(VkInstance _instance, |
| 302 |
const VkAllocationCallbacks *pAllocator) |
| 303 |
{ |
| 304 |
VK_FROM_HANDLE(panvk_instance, instance, _instance); |
| 305 |
|
| 306 |
if (!instance) |
| 307 |
return; |
| 308 |
|
| 309 |
driDestroyOptionCache(&instance->dri_options); |
| 310 |
driDestroyOptionInfo(&instance->available_dri_options); |
| 311 |
|
| 312 |
vk_instance_finish(&instance->vk); |
| 313 |
vk_free(&instance->vk.alloc, instance); |
| 314 |
} |
| 315 |
|
| 316 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 317 |
panvk_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount, |
| 318 |
VkLayerProperties *pProperties) |
| 319 |
{ |
| 320 |
*pPropertyCount = 0; |
| 321 |
return VK_SUCCESS; |
| 322 |
} |
| 323 |
|
| 324 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 325 |
panvk_EnumerateInstanceExtensionProperties(const char *pLayerName, |
| 326 |
uint32_t *pPropertyCount, |
| 327 |
VkExtensionProperties *pProperties) |
| 328 |
{ |
| 329 |
if (pLayerName) |
| 330 |
return panvk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT); |
| 331 |
|
| 332 |
return vk_enumerate_instance_extension_properties( |
| 333 |
&panvk_instance_extensions, pPropertyCount, pProperties); |
| 334 |
} |
| 335 |
|
| 336 |
PFN_vkVoidFunction |
| 337 |
panvk_GetInstanceProcAddr(VkInstance _instance, const char *pName) |
| 338 |
{ |
| 339 |
VK_FROM_HANDLE(panvk_instance, instance, _instance); |
| 340 |
return vk_instance_get_proc_addr(&instance->vk, &panvk_instance_entrypoints, |
| 341 |
pName); |
| 342 |
} |
| 343 |
|
| 344 |
/* The loader wants us to expose a second GetInstanceProcAddr function |
| 345 |
* to work around certain LD_PRELOAD issues seen in apps. |
| 346 |
*/ |
| 347 |
PUBLIC |
| 348 |
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL |
| 349 |
vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName) |
| 350 |
{ |
| 351 |
return panvk_GetInstanceProcAddr(instance, pName); |
| 352 |
} |