xref: /aosp_15_r20/external/mesa3d/src/nouveau/vulkan/nvk_instance.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3  * SPDX-License-Identifier: MIT
4  */
5 #include "nvk_instance.h"
6 
7 #include "nvk_entrypoints.h"
8 #include "nvk_physical_device.h"
9 
10 #include "vulkan/wsi/wsi_common.h"
11 
12 #include "util/build_id.h"
13 #include "util/driconf.h"
14 #include "util/mesa-sha1.h"
15 #include "util/u_debug.h"
16 
17 VKAPI_ATTR VkResult VKAPI_CALL
nvk_EnumerateInstanceVersion(uint32_t * pApiVersion)18 nvk_EnumerateInstanceVersion(uint32_t *pApiVersion)
19 {
20    uint32_t version_override = vk_get_version_override();
21    *pApiVersion = version_override ? version_override :
22                   VK_MAKE_VERSION(1, 3, VK_HEADER_VERSION);
23 
24    return VK_SUCCESS;
25 }
26 
27 static const struct vk_instance_extension_table instance_extensions = {
28 #ifdef NVK_USE_WSI_PLATFORM
29    .KHR_get_surface_capabilities2 = true,
30    .KHR_surface = true,
31    .KHR_surface_protected_capabilities = true,
32    .EXT_surface_maintenance1 = true,
33    .EXT_swapchain_colorspace = true,
34 #endif
35 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
36    .KHR_wayland_surface = true,
37 #endif
38 #ifdef VK_USE_PLATFORM_XCB_KHR
39    .KHR_xcb_surface = true,
40 #endif
41 #ifdef VK_USE_PLATFORM_XLIB_KHR
42    .KHR_xlib_surface = true,
43 #endif
44 #ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
45    .EXT_acquire_xlib_display = true,
46 #endif
47 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
48    .KHR_display = true,
49    .KHR_get_display_properties2 = true,
50    .EXT_direct_mode_display = true,
51    .EXT_display_surface_counter = true,
52    .EXT_acquire_drm_display = true,
53 #endif
54 #ifndef VK_USE_PLATFORM_WIN32_KHR
55    .EXT_headless_surface = true,
56 #endif
57    .KHR_device_group_creation = true,
58    .KHR_external_fence_capabilities = true,
59    .KHR_external_memory_capabilities = true,
60    .KHR_external_semaphore_capabilities = true,
61    .KHR_get_physical_device_properties2 = true,
62    .EXT_debug_report = true,
63    .EXT_debug_utils = true,
64 };
65 
66 VKAPI_ATTR VkResult VKAPI_CALL
nvk_EnumerateInstanceExtensionProperties(const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)67 nvk_EnumerateInstanceExtensionProperties(const char *pLayerName,
68                                          uint32_t *pPropertyCount,
69                                          VkExtensionProperties *pProperties)
70 {
71    if (pLayerName)
72       return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
73 
74    return vk_enumerate_instance_extension_properties(
75       &instance_extensions, pPropertyCount, pProperties);
76 }
77 
78 static void
nvk_init_debug_flags(struct nvk_instance * instance)79 nvk_init_debug_flags(struct nvk_instance *instance)
80 {
81    const struct debug_control flags[] = {
82       { "push_dump", NVK_DEBUG_PUSH_DUMP },
83       { "push", NVK_DEBUG_PUSH_DUMP },
84       { "push_sync", NVK_DEBUG_PUSH_SYNC },
85       { "zero_memory", NVK_DEBUG_ZERO_MEMORY },
86       { "vm", NVK_DEBUG_VM },
87       { "no_cbuf", NVK_DEBUG_NO_CBUF },
88       { "edb_bview", NVK_DEBUG_FORCE_EDB_BVIEW },
89       { NULL, 0 },
90    };
91 
92    instance->debug_flags = parse_debug_string(getenv("NVK_DEBUG"), flags);
93 }
94 
95 static const driOptionDescription nvk_dri_options[] = {
96    DRI_CONF_SECTION_PERFORMANCE
97       DRI_CONF_ADAPTIVE_SYNC(true)
98       DRI_CONF_VK_X11_OVERRIDE_MIN_IMAGE_COUNT(0)
99       DRI_CONF_VK_X11_STRICT_IMAGE_COUNT(false)
100       DRI_CONF_VK_X11_ENSURE_MIN_IMAGE_COUNT(false)
101       DRI_CONF_VK_KHR_PRESENT_WAIT(false)
102       DRI_CONF_VK_XWAYLAND_WAIT_READY(false)
103    DRI_CONF_SECTION_END
104 
105    DRI_CONF_SECTION_DEBUG
106       DRI_CONF_FORCE_VK_VENDOR()
107       DRI_CONF_VK_WSI_FORCE_SWAPCHAIN_TO_CURRENT_EXTENT(false)
108       DRI_CONF_VK_X11_IGNORE_SUBOPTIMAL(false)
109    DRI_CONF_SECTION_END
110 };
111 
112 static void
nvk_init_dri_options(struct nvk_instance * instance)113 nvk_init_dri_options(struct nvk_instance *instance)
114 {
115    driParseOptionInfo(&instance->available_dri_options, nvk_dri_options, ARRAY_SIZE(nvk_dri_options));
116    driParseConfigFiles(&instance->dri_options, &instance->available_dri_options, 0, "nvk", NULL, NULL,
117                        instance->vk.app_info.app_name, instance->vk.app_info.app_version,
118                        instance->vk.app_info.engine_name, instance->vk.app_info.engine_version);
119 
120    instance->force_vk_vendor =
121       driQueryOptioni(&instance->dri_options, "force_vk_vendor");
122 }
123 
124 VKAPI_ATTR VkResult VKAPI_CALL
nvk_CreateInstance(const VkInstanceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkInstance * pInstance)125 nvk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
126                    const VkAllocationCallbacks *pAllocator,
127                    VkInstance *pInstance)
128 {
129    struct nvk_instance *instance;
130    VkResult result;
131 
132    if (pAllocator == NULL)
133       pAllocator = vk_default_allocator();
134 
135    instance = vk_alloc(pAllocator, sizeof(*instance), 8,
136                        VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
137    if (!instance)
138       return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
139 
140    struct vk_instance_dispatch_table dispatch_table;
141    vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
142                                                &nvk_instance_entrypoints,
143                                                true);
144    vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
145                                                &wsi_instance_entrypoints,
146                                                false);
147 
148    result = vk_instance_init(&instance->vk, &instance_extensions,
149                              &dispatch_table, pCreateInfo, pAllocator);
150    if (result != VK_SUCCESS)
151       goto fail_alloc;
152 
153    nvk_init_debug_flags(instance);
154    nvk_init_dri_options(instance);
155 
156    instance->vk.physical_devices.try_create_for_drm =
157       nvk_create_drm_physical_device;
158    instance->vk.physical_devices.destroy = nvk_physical_device_destroy;
159 
160    const struct build_id_note *note =
161       build_id_find_nhdr_for_addr(nvk_CreateInstance);
162    if (!note) {
163       result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED,
164                          "Failed to find build-id");
165       goto fail_init;
166    }
167 
168    unsigned build_id_len = build_id_length(note);
169    if (build_id_len < SHA1_DIGEST_LENGTH) {
170       result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED,
171                         "build-id too short.  It needs to be a SHA");
172       goto fail_init;
173    }
174 
175    STATIC_ASSERT(sizeof(instance->driver_build_sha) == SHA1_DIGEST_LENGTH);
176    memcpy(instance->driver_build_sha, build_id_data(note), SHA1_DIGEST_LENGTH);
177 
178    *pInstance = nvk_instance_to_handle(instance);
179    return VK_SUCCESS;
180 
181 fail_init:
182    vk_instance_finish(&instance->vk);
183 fail_alloc:
184    vk_free(pAllocator, instance);
185 
186    return result;
187 }
188 
189 VKAPI_ATTR void VKAPI_CALL
nvk_DestroyInstance(VkInstance _instance,const VkAllocationCallbacks * pAllocator)190 nvk_DestroyInstance(VkInstance _instance,
191                     const VkAllocationCallbacks *pAllocator)
192 {
193    VK_FROM_HANDLE(nvk_instance, instance, _instance);
194 
195    if (!instance)
196       return;
197 
198    driDestroyOptionCache(&instance->dri_options);
199    driDestroyOptionInfo(&instance->available_dri_options);
200 
201    vk_instance_finish(&instance->vk);
202    vk_free(&instance->vk.alloc, instance);
203 }
204 
205 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
nvk_GetInstanceProcAddr(VkInstance _instance,const char * pName)206 nvk_GetInstanceProcAddr(VkInstance _instance, const char *pName)
207 {
208    VK_FROM_HANDLE(nvk_instance, instance, _instance);
209    return vk_instance_get_proc_addr(&instance->vk,
210                                     &nvk_instance_entrypoints,
211                                     pName);
212 }
213 
214 PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(VkInstance instance,const char * pName)215 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
216 {
217    return nvk_GetInstanceProcAddr(instance, pName);
218 }
219