Debian Codegraph @

src/imagination/vulkan/pvr_instance.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * based in part on anv driver which is:
5 * Copyright © 2015 Intel Corporation
6 *
7 * based in part on v3dv driver which is:
8 * Copyright © 2019 Raspberry Pi
9 *
10 * SPDX-License-Identifier: MIT
11 */
12
13 #include "pvr_instance.h"
14
15 #include <fcntl.h>
16 #include <xf86drm.h>
17 #include <xf86drmMode.h>
18
19 #include "vk_alloc.h"
20 #include "vk_log.h"
21
22 #include "wsi_common.h"
23
24 #include "util/build_id.h"
25
26 #include "pvr_debug.h"
27 #include "pvr_device.h"
28 #include "pvr_entrypoints.h"
29 #include "pvr_macros.h"
30 #include "pvr_physical_device.h"
31 #include "pvr_winsys.h"
32 #include "pvr_wsi.h"
33
34 #if defined(VK_USE_PLATFORM_DISPLAY_KHR)
35 # define PVR_USE_WSI_PLATFORM_DISPLAY true
36 #else
37 # define PVR_USE_WSI_PLATFORM_DISPLAY false
38 #endif
39
40 static const struct vk_instance_extension_table pvr_instance_extensions = {
41 .KHR_device_group_creation = true,
42 .KHR_display = PVR_USE_WSI_PLATFORM_DISPLAY,
43 .KHR_external_fence_capabilities = true,
44 .KHR_external_memory_capabilities = true,
45 .KHR_external_semaphore_capabilities = true,
46 .KHR_get_display_properties2 = PVR_USE_WSI_PLATFORM_DISPLAY,
47 .KHR_get_physical_device_properties2 = true,
48 .KHR_get_surface_capabilities2 = PVR_USE_WSI_PLATFORM,
49 .KHR_surface = PVR_USE_WSI_PLATFORM,
50 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
51 .KHR_wayland_surface = true,
52 #endif
53 #ifdef VK_USE_PLATFORM_XCB_KHR
54 .KHR_xcb_surface = true,
55 #endif
56 #ifdef VK_USE_PLATFORM_XLIB_KHR
57 .KHR_xlib_surface = true,
58 #endif
59 .EXT_debug_report = true,
60 .EXT_debug_utils = true,
61 #ifndef VK_USE_PLATFORM_WIN32_KHR
62 .EXT_headless_surface = PVR_USE_WSI_PLATFORM,
63 #endif
64 };
65
66 static VkResult pvr_get_drm_devices(void *const obj,
67 drmDevicePtr *const devices,
68 const int max_devices,
69 int *const num_devices_out)
70 {
71 int ret = drmGetDevices2(0, devices, max_devices);
72 if (ret < 0) {
73 return vk_errorf(obj,
74 VK_ERROR_INITIALIZATION_FAILED,
75 "Failed to enumerate drm devices (errno %d: %s)",
76 -ret,
77 strerror(-ret));
78 }
79
80 if (num_devices_out)
81 *num_devices_out = ret;
82
83 return VK_SUCCESS;
84 }
85
86 static bool
87 pvr_drm_device_is_compatible(drmDevicePtr drm_dev)
88 {
89 drmVersionPtr version;
90 bool is_pvr;
91 int32_t fd;
92
93 fd = open(drm_dev->nodes[DRM_NODE_RENDER], O_RDWR | O_CLOEXEC);
94 if (fd < 0) {
95 mesa_logd("Failed to open render node: %s\n",
96 drm_dev->nodes[DRM_NODE_RENDER]);
97
98 return false;
99 }
100
101 version = drmGetVersion(fd);
102 if (!version) {
103 mesa_logd("Failed to get version information for render node: %s\n",
104 drm_dev->nodes[DRM_NODE_RENDER]);
105
106 close(fd);
107 return false;
108 }
109
110 is_pvr = !strcmp(version->name, PVR_DRM_DRIVER_NAME);
111
112 #if defined(PVR_SUPPORT_SERVICES_DRIVER)
113 is_pvr |= !strcmp(version->name, PVR_SRV_DRIVER_NAME);
114 #endif /* defined(PVR_SUPPORT_SERVICES_DRIVER) */
115
116 drmFreeVersion(version);
117 close(fd);
118
119 return is_pvr;
120 }
121
122 static bool pvr_drm_device_is_compatible_display(drmDevicePtr drm_dev)
123 {
124 uint64_t has_dumb_buffer = 0;
125 uint64_t prime_caps = 0;
126 bool ret = false;
127 int32_t fd;
128
129 mesa_logd("Checking DRM primary node for compatibility: %s",
130 drm_dev->nodes[DRM_NODE_PRIMARY]);
131 fd = open(drm_dev->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
132 if (fd < 0) {
133 mesa_logd("Failed to open display node: %s\n",
134 drm_dev->nodes[DRM_NODE_PRIMARY]);
135 return ret;
136 }
137
138 /* Must support KMS */
139 if (!drmIsKMS(fd)) {
140 mesa_logd("DRM device does not support KMS");
141 goto out;
142 }
143
144 /* Must support dumb buffers, as these are used by the PVR winsys to
145 * allocate device memory for PVR_WINSYS_BO_TYPE_DISPLAY buffer objects.
146 */
147 if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb_buffer) ||
148 !has_dumb_buffer) {
149 mesa_logd("DRM device does not support dumb buffers");
150 goto out;
151 }
152
153 /* Must support PRIME export (so GPU can import dumb buffers) */
154 if (drmGetCap(fd, DRM_CAP_PRIME, &prime_caps)) {
155 mesa_loge("Failed to query DRM_CAP_PRIME: %s", strerror(errno));
156 goto out;
157 }
158
159 if (!(prime_caps & DRM_PRIME_CAP_EXPORT)) {
160 mesa_logd("DRM device lacks PRIME export support (caps: 0x%" PRIx64 ")",
161 prime_caps);
162 goto out;
163 }
164
165 ret = true;
166
167 out:
168 close(fd);
169 return ret;
170 }
171
172 static VkResult
173 pvr_physical_device_enumerate(struct vk_instance *const vk_instance)
174 {
175 struct pvr_instance *const instance =
176 container_of(vk_instance, struct pvr_instance, vk);
177
178 drmDevicePtr drm_display_device = NULL;
179 drmDevicePtr drm_render_device = NULL;
180 struct pvr_physical_device *pdevice;
181 drmDevicePtr *drm_devices;
182 int num_drm_devices = 0;
183 VkResult result;
184
185 result = pvr_get_drm_devices(instance, NULL, 0, &num_drm_devices);
186 if (result != VK_SUCCESS)
187 goto out;
188
189 if (num_drm_devices == 0) {
190 result = VK_SUCCESS;
191 goto out;
192 }
193
194 drm_devices = vk_alloc(&vk_instance->alloc,
195 sizeof(*drm_devices) * num_drm_devices,
196 8,
197 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
198 if (!drm_devices) {
199 result = vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
200 goto out;
201 }
202
203 result = pvr_get_drm_devices(instance, drm_devices, num_drm_devices, NULL);
204 if (result != VK_SUCCESS)
205 goto out_free_drm_device_ptrs;
206
207 /* First search for our render node... */
208 for (int i = 0; i < num_drm_devices; i++) {
209 drmDevice *const drm_dev = drm_devices[i];
210
211 if (drm_dev->bustype != DRM_BUS_PLATFORM)
212 continue;
213
214 if (!(drm_dev->available_nodes & BITFIELD_BIT(DRM_NODE_RENDER)))
215 continue;
216
217
218 if (!pvr_drm_device_is_compatible(drm_dev))
219 continue;
220
221 drm_render_device = drm_dev;
222 break;
223 }
224
225 if (!drm_render_device) {
226 result = VK_SUCCESS;
227 goto out_free_drm_devices;
228 }
229
230 mesa_logd("Found compatible render device '%s'.",
231 drm_render_device->nodes[DRM_NODE_RENDER]);
232
233 /* ...then find a compatible display node, if available. */
234 for (int i = 0; i < num_drm_devices; i++) {
235 drmDevice *const drm_dev = drm_devices[i];
236
237 if (drm_dev->bustype != DRM_BUS_PLATFORM)
238 continue;
239
240 if (!(drm_dev->available_nodes & BITFIELD_BIT(DRM_NODE_PRIMARY)))
241 continue;
242
243 if (!pvr_drm_device_is_compatible_display(drm_devices[i]))
244 continue;
245
246 drm_display_device = drm_dev;
247 mesa_logd("Found a compatible display device: '%s'.",
248 drm_display_device->nodes[DRM_NODE_PRIMARY]);
249 break;
250 }
251
252 pdevice = vk_alloc(&vk_instance->alloc,
253 sizeof(*pdevice),
254 8,
255 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
256 if (!pdevice) {
257 result = vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
258 goto out_free_drm_devices;
259 }
260
261 result = pvr_physical_device_init(pdevice,
262 instance,
263 drm_render_device,
264 drm_display_device);
265 if (result != VK_SUCCESS) {
266 if (result == VK_ERROR_INCOMPATIBLE_DRIVER)
267 result = VK_SUCCESS;
268
269 goto err_free_pdevice;
270 }
271
272 if (PVR_IS_DEBUG_SET(INFO)) {
273 pvr_physical_device_dump_info(
274 pdevice,
275 drm_display_device
276 ? drm_display_device->deviceinfo.platform->compatible
277 : NULL,
278 drm_render_device->deviceinfo.platform->compatible);
279 }
280
281 list_add(&pdevice->vk.link, &vk_instance->physical_devices.list);
282
283 result = VK_SUCCESS;
284 goto out_free_drm_devices;
285
286 err_free_pdevice:
287 vk_free(&vk_instance->alloc, pdevice);
288
289 out_free_drm_devices:
290 drmFreeDevices(drm_devices, num_drm_devices);
291
292 out_free_drm_device_ptrs:
293 vk_free(&vk_instance->alloc, drm_devices);
294
295 out:
296 return result;
297 }
298
299 static bool
300 pvr_get_driver_build_sha(struct pvr_instance *instance)
301 {
302 const struct build_id_note *note;
303 unsigned build_id_len;
304
305 note = build_id_find_nhdr_for_addr(pvr_get_driver_build_sha);
306 if (!note) {
307 mesa_loge("Failed to find build-id.");
308 return false;
309 }
310
311 build_id_len = build_id_length(note);
312 if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) {
313 mesa_loge("Build-id too short. It needs to be a SHA.");
314 return false;
315 }
316
317 STATIC_ASSERT(sizeof(instance->driver_build_sha) == BLAKE3_KEY_LEN);
318 copy_build_id_to_sha1(instance->driver_build_sha, note);
319
320 return true;
321 }
322
323 VkResult pvr_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
324 const VkAllocationCallbacks *pAllocator,
325 VkInstance *pInstance)
326 {
327 struct vk_instance_dispatch_table dispatch_table;
328 struct pvr_instance *instance;
329 VkResult result;
330
331 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
332
333 if (!pAllocator)
334 pAllocator = vk_default_allocator();
335
336 instance = vk_alloc(pAllocator,
337 sizeof(*instance),
338 8,
339 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
340 if (!instance)
341 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
342
343 vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
344 &pvr_instance_entrypoints,
345 true);
346
347 vk_instance_dispatch_table_from_entrypoints(&dispatch_table,
348 &wsi_instance_entrypoints,
349 false);
350
351 result = vk_instance_init(&instance->vk,
352 &pvr_instance_extensions,
353 &dispatch_table,
354 pCreateInfo,
355 pAllocator);
356 if (result != VK_SUCCESS)
357 goto err_free_instance;
358
359 pvr_process_debug_variable();
360
361 instance->active_device_count = 0;
362
363 instance->vk.physical_devices.enumerate = pvr_physical_device_enumerate;
364 instance->vk.physical_devices.destroy = pvr_physical_device_destroy;
365
366 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
367
368 if (!pvr_get_driver_build_sha(instance)) {
369 result = vk_errorf(NULL,
370 VK_ERROR_INITIALIZATION_FAILED,
371 "Failed to get driver build sha.");
372 goto err_free_instance;
373 }
374
375 *pInstance = pvr_instance_to_handle(instance);
376
377 return VK_SUCCESS;
378
379 err_free_instance:
380 vk_free(pAllocator, instance);
381 return result;
382 }
383
384 void pvr_DestroyInstance(VkInstance _instance,
385 const VkAllocationCallbacks *pAllocator)
386 {
387 VK_FROM_HANDLE(pvr_instance, instance, _instance);
388
389 if (!instance)
390 return;
391
392 VG(VALGRIND_DESTROY_MEMPOOL(instance));
393
394 vk_instance_finish(&instance->vk);
395 vk_free(&instance->vk.alloc, instance);
396 }
397
398 VkResult pvr_EnumerateInstanceVersion(uint32_t *pApiVersion)
399 {
400 *pApiVersion = VK_MAKE_API_VERSION(0, 1, 4, VK_HEADER_VERSION);
401 return VK_SUCCESS;
402 }
403
404 VkResult
405 pvr_EnumerateInstanceExtensionProperties(const char *pLayerName,
406 uint32_t *pPropertyCount,
407 VkExtensionProperties *pProperties)
408 {
409 if (pLayerName)
410 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
411
412 return vk_enumerate_instance_extension_properties(&pvr_instance_extensions,
413 pPropertyCount,
414 pProperties);
415 }
416
417 PFN_vkVoidFunction pvr_GetInstanceProcAddr(VkInstance _instance,
418 const char *pName)
419 {
420 const struct vk_instance *vk_instance = NULL;
421
422 if (_instance != NULL) {
423 VK_FROM_HANDLE(pvr_instance, instance, _instance);
424 vk_instance = &instance->vk;
425 }
426
427 return vk_instance_get_proc_addr(vk_instance,
428 &pvr_instance_entrypoints,
429 pName);
430 }
431
432 /* With version 1+ of the loader interface the ICD should expose
433 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in
434 * apps.
435 */
436 PUBLIC
437 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
438 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
439 {
440 return pvr_GetInstanceProcAddr(instance, pName);
441 }
442
443 VkResult pvr_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
444 VkLayerProperties *pProperties)
445 {
446 if (!pProperties) {
447 *pPropertyCount = 0;
448 return VK_SUCCESS;
449 }
450
451 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
452 }