radv_device.c 173 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright © 2016 Red Hat.
 * Copyright © 2016 Bas Nieuwenhuizen
 *
 * based in part on anv driver which is:
 * Copyright © 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
32
#include "radv_debug.h"
33
#include "radv_private.h"
34
#include "radv_shader.h"
35
#include "radv_cs.h"
36
#include "util/disk_cache.h"
37
#include "util/strtod.h"
38
#include "vk_util.h"
39
#include <xf86drm.h>
40 41 42 43 44 45
#include <amdgpu.h>
#include <amdgpu_drm.h>
#include "winsys/amdgpu/radv_amdgpu_winsys_public.h"
#include "ac_llvm_util.h"
#include "vk_format.h"
#include "sid.h"
46
#include "git_sha1.h"
47
#include "gfx9d.h"
48
#include "util/build_id.h"
49
#include "util/debug.h"
50 51
#include "util/mesa-sha1.h"

52
static int
53
radv_device_get_cache_uuid(enum radeon_family family, void *uuid)
54
{
55 56 57
	struct mesa_sha1 ctx;
	unsigned char sha1[20];
	unsigned ptr_size = sizeof(void*);
58

59
	memset(uuid, 0, VK_UUID_SIZE);
60
	_mesa_sha1_init(&ctx);
61

62 63
	if (!disk_cache_get_function_identifier(radv_device_get_cache_uuid, &ctx) ||
	    !disk_cache_get_function_identifier(LLVMInitializeAMDGPUTargetInfo, &ctx))
64 65
		return -1;

66 67 68 69 70
	_mesa_sha1_update(&ctx, &family, sizeof(family));
	_mesa_sha1_update(&ctx, &ptr_size, sizeof(ptr_size));
	_mesa_sha1_final(&ctx, sha1);

	memcpy(uuid, sha1, VK_UUID_SIZE);
71
	return 0;
72 73
}

74 75 76 77 78 79
static void
radv_get_driver_uuid(void *uuid)
{
	ac_compute_driver_uuid(uuid, VK_UUID_SIZE);
}

80
static void
81 82 83
radv_get_device_uuid(struct radeon_info *info, void *uuid)
{
	ac_compute_device_uuid(info, uuid, VK_UUID_SIZE);
84 85
}

86 87
static void
radv_get_device_name(enum radeon_family family, char *name, size_t name_len)
88
{
89 90 91
	const char *chip_string;
	char llvm_string[32] = {};

92
	switch (family) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	case CHIP_TAHITI: chip_string = "AMD RADV TAHITI"; break;
	case CHIP_PITCAIRN: chip_string = "AMD RADV PITCAIRN"; break;
	case CHIP_VERDE: chip_string = "AMD RADV CAPE VERDE"; break;
	case CHIP_OLAND: chip_string = "AMD RADV OLAND"; break;
	case CHIP_HAINAN: chip_string = "AMD RADV HAINAN"; break;
	case CHIP_BONAIRE: chip_string = "AMD RADV BONAIRE"; break;
	case CHIP_KAVERI: chip_string = "AMD RADV KAVERI"; break;
	case CHIP_KABINI: chip_string = "AMD RADV KABINI"; break;
	case CHIP_HAWAII: chip_string = "AMD RADV HAWAII"; break;
	case CHIP_MULLINS: chip_string = "AMD RADV MULLINS"; break;
	case CHIP_TONGA: chip_string = "AMD RADV TONGA"; break;
	case CHIP_ICELAND: chip_string = "AMD RADV ICELAND"; break;
	case CHIP_CARRIZO: chip_string = "AMD RADV CARRIZO"; break;
	case CHIP_FIJI: chip_string = "AMD RADV FIJI"; break;
	case CHIP_POLARIS10: chip_string = "AMD RADV POLARIS10"; break;
	case CHIP_POLARIS11: chip_string = "AMD RADV POLARIS11"; break;
	case CHIP_POLARIS12: chip_string = "AMD RADV POLARIS12"; break;
	case CHIP_STONEY: chip_string = "AMD RADV STONEY"; break;
111
	case CHIP_VEGAM: chip_string = "AMD RADV VEGA M"; break;
112
	case CHIP_VEGA10: chip_string = "AMD RADV VEGA10"; break;
113
	case CHIP_VEGA12: chip_string = "AMD RADV VEGA12"; break;
114
	case CHIP_RAVEN: chip_string = "AMD RADV RAVEN"; break;
115
	case CHIP_RAVEN2: chip_string = "AMD RADV RAVEN2"; break;
116
	default: chip_string = "AMD RADV unknown"; break;
117
	}
118

119 120 121
	snprintf(llvm_string, sizeof(llvm_string),
		 " (LLVM %i.%i.%i)", (HAVE_LLVM >> 8) & 0xff,
		 HAVE_LLVM & 0xff, MESA_LLVM_VERSION_PATCH);
122
	snprintf(name, name_len, "%s%s", chip_string, llvm_string);
123 124
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static void
radv_physical_device_init_mem_types(struct radv_physical_device *device)
{
	STATIC_ASSERT(RADV_MEM_HEAP_COUNT <= VK_MAX_MEMORY_HEAPS);
	uint64_t visible_vram_size = MIN2(device->rad_info.vram_size,
	                                  device->rad_info.vram_vis_size);

	int vram_index = -1, visible_vram_index = -1, gart_index = -1;
	device->memory_properties.memoryHeapCount = 0;
	if (device->rad_info.vram_size - visible_vram_size > 0) {
		vram_index = device->memory_properties.memoryHeapCount++;
		device->memory_properties.memoryHeaps[vram_index] = (VkMemoryHeap) {
			.size = device->rad_info.vram_size - visible_vram_size,
			.flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
		};
	}
	if (visible_vram_size) {
		visible_vram_index = device->memory_properties.memoryHeapCount++;
		device->memory_properties.memoryHeaps[visible_vram_index] = (VkMemoryHeap) {
			.size = visible_vram_size,
			.flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
		};
	}
	if (device->rad_info.gart_size > 0) {
		gart_index = device->memory_properties.memoryHeapCount++;
		device->memory_properties.memoryHeaps[gart_index] = (VkMemoryHeap) {
			.size = device->rad_info.gart_size,
152
			.flags = device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		};
	}

	STATIC_ASSERT(RADV_MEM_TYPE_COUNT <= VK_MAX_MEMORY_TYPES);
	unsigned type_count = 0;
	if (vram_index >= 0) {
		device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM;
		device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
			.propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
			.heapIndex = vram_index,
		};
	}
	if (gart_index >= 0) {
		device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_WRITE_COMBINE;
		device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
			.propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
169 170
			VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
			(device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
			.heapIndex = gart_index,
		};
	}
	if (visible_vram_index >= 0) {
		device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM_CPU_ACCESS;
		device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
			.propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
			VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
			VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
			.heapIndex = visible_vram_index,
		};
	}
	if (gart_index >= 0) {
		device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_CACHED;
		device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
			.propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
			VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
188 189
			VK_MEMORY_PROPERTY_HOST_CACHED_BIT |
			(device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
190 191 192 193 194 195
			.heapIndex = gart_index,
		};
	}
	device->memory_properties.memoryTypeCount = type_count;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static void
radv_handle_env_var_force_family(struct radv_physical_device *device)
{
	const char *family = getenv("RADV_FORCE_FAMILY");
	unsigned i;

	if (!family)
		return;

	for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
		if (!strcmp(family, ac_get_llvm_processor_name(i))) {
			/* Override family and chip_class. */
			device->rad_info.family = i;

			if (i >= CHIP_VEGA10)
				device->rad_info.chip_class = GFX9;
			else if (i >= CHIP_TONGA)
				device->rad_info.chip_class = VI;
			else if (i >= CHIP_BONAIRE)
				device->rad_info.chip_class = CIK;
			else
				device->rad_info.chip_class = SI;

			return;
		}
	}

	fprintf(stderr, "radv: Unknown family: %s\n", family);
	exit(1);
}

227 228 229
static VkResult
radv_physical_device_init(struct radv_physical_device *device,
			  struct radv_instance *instance,
230
			  drmDevicePtr drm_device)
231
{
232
	const char *path = drm_device->nodes[DRM_NODE_RENDER];
233
	VkResult result;
234
	drmVersionPtr version;
235
	int fd;
236
	int master_fd = -1;
237 238

	fd = open(path, O_RDWR | O_CLOEXEC);
239 240 241 242
	if (fd < 0) {
		if (instance->debug_flags & RADV_DEBUG_STARTUP)
			radv_logi("Could not open device '%s'", path);

243
		return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
244
	}
245

246 247 248
	version = drmGetVersion(fd);
	if (!version) {
		close(fd);
249 250 251 252

		if (instance->debug_flags & RADV_DEBUG_STARTUP)
			radv_logi("Could not get the kernel driver version for device '%s'", path);

253
		return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
254 255 256 257 258 259
				 "failed to get version %s: %m", path);
	}

	if (strcmp(version->name, "amdgpu")) {
		drmFreeVersion(version);
		close(fd);
260 261 262 263

		if (instance->debug_flags & RADV_DEBUG_STARTUP)
			radv_logi("Device '%s' is not using the amdgpu kernel driver.", path);

264
		return VK_ERROR_INCOMPATIBLE_DRIVER;
265 266 267
	}
	drmFreeVersion(version);

268 269 270
	if (instance->debug_flags & RADV_DEBUG_STARTUP)
			radv_logi("Found compatible device '%s'.", path);

271 272 273 274 275
	device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
	device->instance = instance;
	assert(strlen(path) < ARRAY_SIZE(device->path));
	strncpy(device->path, path, ARRAY_SIZE(device->path));

276 277
	device->ws = radv_amdgpu_winsys_create(fd, instance->debug_flags,
					       instance->perftest_flags);
278
	if (!device->ws) {
279
		result = vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
280 281
		goto fail;
	}
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	if (instance->enabled_extensions.KHR_display) {
		master_fd = open(drm_device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
		if (master_fd >= 0) {
			uint32_t accel_working = 0;
			struct drm_amdgpu_info request = {
				.return_pointer = (uintptr_t)&accel_working,
				.return_size = sizeof(accel_working),
				.query = AMDGPU_INFO_ACCEL_WORKING
			};

			if (drmCommandWrite(master_fd, DRM_AMDGPU_INFO, &request, sizeof (struct drm_amdgpu_info)) < 0 || !accel_working) {
				close(master_fd);
				master_fd = -1;
			}
		}
	}

300
	device->master_fd = master_fd;
301
	device->local_fd = fd;
302 303
	device->ws->query_info(device->ws, &device->rad_info);

304 305
	radv_handle_env_var_force_family(device);

306
	radv_get_device_name(device->rad_info.family, device->name, sizeof(device->name));
307

308
	if (radv_device_get_cache_uuid(device->rad_info.family, device->cache_uuid)) {
309
		device->ws->destroy(device->ws);
310
		result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
311
				   "cannot generate UUID");
312 313
		goto fail;
	}
314

315 316 317 318 319
	/* These flags affect shader compilation. */
	uint64_t shader_env_flags =
		(device->instance->perftest_flags & RADV_PERFTEST_SISCHED ? 0x1 : 0) |
		(device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH ? 0x2 : 0);

320
	/* The gpu id is already embedded in the uuid so we just pass "radv"
321 322
	 * when creating the cache.
	 */
323 324
	char buf[VK_UUID_SIZE * 2 + 1];
	disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
325
	device->disk_cache = disk_cache_create(device->name, buf, shader_env_flags);
326

327 328 329
	if (device->rad_info.chip_class < VI ||
	    device->rad_info.chip_class > GFX9)
		fprintf(stderr, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
330

331
	radv_get_driver_uuid(&device->device_uuid);
332
	radv_get_device_uuid(&device->rad_info, &device->device_uuid);
333

334 335 336
	if (device->rad_info.family == CHIP_STONEY ||
	    device->rad_info.chip_class >= GFX9) {
		device->has_rbplus = true;
337
		device->rbplus_allowed = device->rad_info.family == CHIP_STONEY ||
338
					 device->rad_info.family == CHIP_VEGA12 ||
339 340
		                         device->rad_info.family == CHIP_RAVEN ||
		                         device->rad_info.family == CHIP_RAVEN2;
341 342
	}

343
	/* The mere presence of CLEAR_STATE in the IB causes random GPU hangs
344 345 346 347
	 * on SI.
	 */
	device->has_clear_state = device->rad_info.chip_class >= CIK;

348 349
	device->cpdma_prefetch_writes_memory = device->rad_info.chip_class <= VI;

350 351 352 353
	/* Vega10/Raven need a special workaround for a hardware bug. */
	device->has_scissor_bug = device->rad_info.family == CHIP_VEGA10 ||
				  device->rad_info.family == CHIP_RAVEN;

354 355 356 357
	/* Out-of-order primitive rasterization. */
	device->has_out_of_order_rast = device->rad_info.chip_class >= VI &&
					device->rad_info.max_se >= 2;
	device->out_of_order_rast_allowed = device->has_out_of_order_rast &&
358
					    !(device->instance->debug_flags & RADV_DEBUG_NO_OUT_OF_ORDER);
359

360 361
	device->dcc_msaa_allowed =
		(device->instance->perftest_flags & RADV_PERFTEST_DCC_MSAA);
362

363
	radv_physical_device_init_mem_types(device);
364
	radv_fill_device_extension_table(device, &device->supported_extensions);
365

366 367 368 369 370 371 372 373 374
	device->bus_info = *drm_device->businfo.pci;

	if ((device->instance->debug_flags & RADV_DEBUG_INFO))
		ac_print_gpu_info(&device->rad_info);

	/* The WSI is structured as a layer on top of the driver, so this has
	 * to be the last part of initialization (at least until we get other
	 * semi-layers).
	 */
375 376 377
	result = radv_init_wsi(device);
	if (result != VK_SUCCESS) {
		device->ws->destroy(device->ws);
378
		vk_error(instance, result);
379 380 381
		goto fail;
	}

382 383 384 385
	return VK_SUCCESS;

fail:
	close(fd);
386 387
	if (master_fd != -1)
		close(master_fd);
388 389 390 391 392 393 394 395
	return result;
}

static void
radv_physical_device_finish(struct radv_physical_device *device)
{
	radv_finish_wsi(device);
	device->ws->destroy(device->ws);
396
	disk_cache_destroy(device->disk_cache);
397
	close(device->local_fd);
398 399
	if (device->master_fd != -1)
		close(device->master_fd);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
}

static void *
default_alloc_func(void *pUserData, size_t size, size_t align,
                   VkSystemAllocationScope allocationScope)
{
	return malloc(size);
}

static void *
default_realloc_func(void *pUserData, void *pOriginal, size_t size,
                     size_t align, VkSystemAllocationScope allocationScope)
{
	return realloc(pOriginal, size);
}

static void
default_free_func(void *pUserData, void *pMemory)
{
	free(pMemory);
}

static const VkAllocationCallbacks default_alloc = {
	.pUserData = NULL,
	.pfnAllocation = default_alloc_func,
	.pfnReallocation = default_realloc_func,
	.pfnFree = default_free_func,
};

429
static const struct debug_control radv_debug_options[] = {
430
	{"nofastclears", RADV_DEBUG_NO_FAST_CLEARS},
431 432 433 434 435 436 437
	{"nodcc", RADV_DEBUG_NO_DCC},
	{"shaders", RADV_DEBUG_DUMP_SHADERS},
	{"nocache", RADV_DEBUG_NO_CACHE},
	{"shaderstats", RADV_DEBUG_DUMP_SHADER_STATS},
	{"nohiz", RADV_DEBUG_NO_HIZ},
	{"nocompute", RADV_DEBUG_NO_COMPUTE_QUEUE},
	{"unsafemath", RADV_DEBUG_UNSAFE_MATH},
438 439
	{"allbos", RADV_DEBUG_ALL_BOS},
	{"noibs", RADV_DEBUG_NO_IBS},
440
	{"spirv", RADV_DEBUG_DUMP_SPIRV},
441
	{"vmfaults", RADV_DEBUG_VM_FAULTS},
442
	{"zerovram", RADV_DEBUG_ZERO_VRAM},
443
	{"syncshaders", RADV_DEBUG_SYNC_SHADERS},
444
	{"nosisched", RADV_DEBUG_NO_SISCHED},
445
	{"preoptir", RADV_DEBUG_PREOPTIR},
446
	{"nodynamicbounds", RADV_DEBUG_NO_DYNAMIC_BOUNDS},
447
	{"nooutoforder", RADV_DEBUG_NO_OUT_OF_ORDER},
448
	{"info", RADV_DEBUG_INFO},
449
	{"errors", RADV_DEBUG_ERRORS},
450
	{"startup", RADV_DEBUG_STARTUP},
451
	{"checkir", RADV_DEBUG_CHECKIR},
452
	{"nothreadllvm", RADV_DEBUG_NOTHREADLLVM},
453
	{"nobinning", RADV_DEBUG_NOBINNING},
454 455 456
	{NULL, 0}
};

457 458 459 460 461 462 463
const char *
radv_get_debug_option_name(int id)
{
	assert(id < ARRAY_SIZE(radv_debug_options) - 1);
	return radv_debug_options[id].string;
}

464
static const struct debug_control radv_perftest_options[] = {
465
	{"nobatchchain", RADV_PERFTEST_NO_BATCHCHAIN},
466
	{"sisched", RADV_PERFTEST_SISCHED},
467
	{"localbos", RADV_PERFTEST_LOCAL_BOS},
468
	{"dccmsaa", RADV_PERFTEST_DCC_MSAA},
469 470 471
	{NULL, 0}
};

472 473 474
const char *
radv_get_perftest_option_name(int id)
{
475
	assert(id < ARRAY_SIZE(radv_perftest_options) - 1);
476 477 478
	return radv_perftest_options[id].string;
}

479 480 481 482 483 484 485 486 487 488 489
static void
radv_handle_per_app_options(struct radv_instance *instance,
			    const VkApplicationInfo *info)
{
	const char *name = info ? info->pApplicationName : NULL;

	if (!name)
		return;

	if (!strcmp(name, "Talos - Linux - 32bit") ||
	    !strcmp(name, "Talos - Linux - 64bit")) {
490 491 492 493 494 495
		if (!(instance->debug_flags & RADV_DEBUG_NO_SISCHED)) {
			/* Force enable LLVM sisched for Talos because it looks
			 * safe and it gives few more FPS.
			 */
			instance->perftest_flags |= RADV_PERFTEST_SISCHED;
		}
496 497 498
	} else if (!strcmp(name, "DOOM_VFR")) {
		/* Work around a Doom VFR game bug */
		instance->debug_flags |= RADV_DEBUG_NO_DYNAMIC_BOUNDS;
499 500 501
	}
}

502 503 504 505 506 507 508 509 510 511
static int radv_get_instance_extension_index(const char *name)
{
	for (unsigned i = 0; i < RADV_INSTANCE_EXTENSION_COUNT; ++i) {
		if (strcmp(name, radv_instance_extensions[i].extensionName) == 0)
			return i;
	}
	return -1;
}


512 513 514 515 516 517
VkResult radv_CreateInstance(
	const VkInstanceCreateInfo*                 pCreateInfo,
	const VkAllocationCallbacks*                pAllocator,
	VkInstance*                                 pInstance)
{
	struct radv_instance *instance;
518
	VkResult result;
519 520 521 522 523 524 525 526

	assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);

	uint32_t client_version;
	if (pCreateInfo->pApplicationInfo &&
	    pCreateInfo->pApplicationInfo->apiVersion != 0) {
		client_version = pCreateInfo->pApplicationInfo->apiVersion;
	} else {
527
		radv_EnumerateInstanceVersion(&client_version);
528 529
	}

530 531
	instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
			      VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
532
	if (!instance)
533
		return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
534 535 536 537 538 539 540 541 542 543 544

	instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;

	if (pAllocator)
		instance->alloc = *pAllocator;
	else
		instance->alloc = default_alloc;

	instance->apiVersion = client_version;
	instance->physicalDeviceCount = -1;

545 546 547 548 549 550
	instance->debug_flags = parse_debug_string(getenv("RADV_DEBUG"),
						   radv_debug_options);

	instance->perftest_flags = parse_debug_string(getenv("RADV_PERFTEST"),
						   radv_perftest_options);

551 552 553 554

	if (instance->debug_flags & RADV_DEBUG_STARTUP)
		radv_logi("Created an instance");

555 556 557 558 559 560
	for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
		const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
		int index = radv_get_instance_extension_index(ext_name);

		if (index < 0 || !radv_supported_instance_extensions.extensions[index]) {
			vk_free2(&default_alloc, pAllocator, instance);
561
			return vk_error(instance, VK_ERROR_EXTENSION_NOT_PRESENT);
562 563 564 565 566
		}

		instance->enabled_extensions.extensions[index] = true;
	}

567 568 569
	result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
	if (result != VK_SUCCESS) {
		vk_free2(&default_alloc, pAllocator, instance);
570
		return vk_error(instance, result);
571 572
	}

573 574 575 576
	_mesa_locale_init();

	VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));

577 578
	radv_handle_per_app_options(instance, pCreateInfo->pApplicationInfo);

579 580 581 582 583 584 585 586 587 588 589
	*pInstance = radv_instance_to_handle(instance);

	return VK_SUCCESS;
}

void radv_DestroyInstance(
	VkInstance                                  _instance,
	const VkAllocationCallbacks*                pAllocator)
{
	RADV_FROM_HANDLE(radv_instance, instance, _instance);

590 591 592
	if (!instance)
		return;

593 594
	for (int i = 0; i < instance->physicalDeviceCount; ++i) {
		radv_physical_device_finish(instance->physicalDevices + i);
595 596 597 598 599 600
	}

	VG(VALGRIND_DESTROY_MEMPOOL(instance));

	_mesa_locale_fini();

601 602
	vk_debug_report_instance_destroy(&instance->debug_report_callbacks);

603
	vk_free(&instance->alloc, instance);
604 605
}

606 607
static VkResult
radv_enumerate_devices(struct radv_instance *instance)
608
{
609 610 611 612 613 614 615
	/* TODO: Check for more devices ? */
	drmDevicePtr devices[8];
	VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
	int max_devices;

	instance->physicalDeviceCount = 0;

616
	max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
617 618 619 620

	if (instance->debug_flags & RADV_DEBUG_STARTUP)
		radv_logi("Found %d drm nodes", max_devices);

621
	if (max_devices < 1)
622
		return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
623 624 625 626

	for (unsigned i = 0; i < (unsigned)max_devices; i++) {
		if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
		    devices[i]->bustype == DRM_BUS_PCI &&
627
		    devices[i]->deviceinfo.pci->vendor_id == ATI_VENDOR_ID) {
628

629 630
			result = radv_physical_device_init(instance->physicalDevices +
			                                   instance->physicalDeviceCount,
631
			                                   instance,
632
			                                   devices[i]);
633 634 635
			if (result == VK_SUCCESS)
				++instance->physicalDeviceCount;
			else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
636
				break;
637 638
		}
	}
639 640
	drmFreeDevices(devices, max_devices);

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	return result;
}

VkResult radv_EnumeratePhysicalDevices(
	VkInstance                                  _instance,
	uint32_t*                                   pPhysicalDeviceCount,
	VkPhysicalDevice*                           pPhysicalDevices)
{
	RADV_FROM_HANDLE(radv_instance, instance, _instance);
	VkResult result;

	if (instance->physicalDeviceCount < 0) {
		result = radv_enumerate_devices(instance);
		if (result != VK_SUCCESS &&
		    result != VK_ERROR_INCOMPATIBLE_DRIVER)
			return result;
	}
658 659 660 661

	if (!pPhysicalDevices) {
		*pPhysicalDeviceCount = instance->physicalDeviceCount;
	} else {
662 663 664
		*pPhysicalDeviceCount = MIN2(*pPhysicalDeviceCount, instance->physicalDeviceCount);
		for (unsigned i = 0; i < *pPhysicalDeviceCount; ++i)
			pPhysicalDevices[i] = radv_physical_device_to_handle(instance->physicalDevices + i);
665 666
	}

667 668
	return *pPhysicalDeviceCount < instance->physicalDeviceCount ? VK_INCOMPLETE
	                                                             : VK_SUCCESS;
669 670
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
VkResult radv_EnumeratePhysicalDeviceGroups(
    VkInstance                                  _instance,
    uint32_t*                                   pPhysicalDeviceGroupCount,
    VkPhysicalDeviceGroupProperties*            pPhysicalDeviceGroupProperties)
{
	RADV_FROM_HANDLE(radv_instance, instance, _instance);
	VkResult result;

	if (instance->physicalDeviceCount < 0) {
		result = radv_enumerate_devices(instance);
		if (result != VK_SUCCESS &&
		    result != VK_ERROR_INCOMPATIBLE_DRIVER)
			return result;
	}

	if (!pPhysicalDeviceGroupProperties) {
		*pPhysicalDeviceGroupCount = instance->physicalDeviceCount;
	} else {
		*pPhysicalDeviceGroupCount = MIN2(*pPhysicalDeviceGroupCount, instance->physicalDeviceCount);
		for (unsigned i = 0; i < *pPhysicalDeviceGroupCount; ++i) {
			pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
			pPhysicalDeviceGroupProperties[i].physicalDevices[0] = radv_physical_device_to_handle(instance->physicalDevices + i);
			pPhysicalDeviceGroupProperties[i].subsetAllocation = false;
		}
	}
	return *pPhysicalDeviceGroupCount < instance->physicalDeviceCount ? VK_INCOMPLETE
	                                                                  : VK_SUCCESS;
}

700 701 702 703
void radv_GetPhysicalDeviceFeatures(
	VkPhysicalDevice                            physicalDevice,
	VkPhysicalDeviceFeatures*                   pFeatures)
{
704
	RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
705 706 707 708 709 710 711
	memset(pFeatures, 0, sizeof(*pFeatures));

	*pFeatures = (VkPhysicalDeviceFeatures) {
		.robustBufferAccess                       = true,
		.fullDrawIndexUint32                      = true,
		.imageCubeArray                           = true,
		.independentBlend                         = true,
712
		.geometryShader                           = true,
713
		.tessellationShader                       = true,
714
		.sampleRateShading                        = true,
715 716 717 718 719 720 721 722 723 724 725
		.dualSrcBlend                             = true,
		.logicOp                                  = true,
		.multiDrawIndirect                        = true,
		.drawIndirectFirstInstance                = true,
		.depthClamp                               = true,
		.depthBiasClamp                           = true,
		.fillModeNonSolid                         = true,
		.depthBounds                              = true,
		.wideLines                                = true,
		.largePoints                              = true,
		.alphaToOne                               = true,
726
		.multiViewport                            = true,
727
		.samplerAnisotropy                        = true,
728 729
		.textureCompressionETC2                   = pdevice->rad_info.chip_class >= GFX9 ||
		                                            pdevice->rad_info.family == CHIP_STONEY,
730 731 732
		.textureCompressionASTC_LDR               = false,
		.textureCompressionBC                     = true,
		.occlusionQueryPrecise                    = true,
733
		.pipelineStatisticsQuery                  = true,
734 735 736
		.vertexPipelineStoresAndAtomics           = true,
		.fragmentStoresAndAtomics                 = true,
		.shaderTessellationAndGeometryPointSize   = true,
737
		.shaderImageGatherExtended                = true,
738
		.shaderStorageImageExtendedFormats        = true,
739 740 741 742 743
		.shaderStorageImageMultisample            = false,
		.shaderUniformBufferArrayDynamicIndexing  = true,
		.shaderSampledImageArrayDynamicIndexing   = true,
		.shaderStorageBufferArrayDynamicIndexing  = true,
		.shaderStorageImageArrayDynamicIndexing   = true,
744
		.shaderStorageImageReadWithoutFormat      = true,
745
		.shaderStorageImageWriteWithoutFormat     = true,
746 747
		.shaderClipDistance                       = true,
		.shaderCullDistance                       = true,
748
		.shaderFloat64                            = true,
749
		.shaderInt64                              = true,
750
		.shaderInt16                              = pdevice->rad_info.chip_class >= GFX9,
751
		.sparseBinding                            = true,
752
		.variableMultisampleRate                  = true,
753
		.inheritedQueries                         = true,
754 755 756
	};
}

757
void radv_GetPhysicalDeviceFeatures2(
758 759 760
	VkPhysicalDevice                            physicalDevice,
	VkPhysicalDeviceFeatures2KHR               *pFeatures)
{
761
	RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
762 763 764 765 766 767 768 769
	vk_foreach_struct(ext, pFeatures->pNext) {
		switch (ext->sType) {
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR: {
			VkPhysicalDeviceVariablePointerFeaturesKHR *features = (void *)ext;
			features->variablePointersStorageBuffer = true;
			features->variablePointers = false;
			break;
		}
770 771
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES_KHR: {
			VkPhysicalDeviceMultiviewFeaturesKHR *features = (VkPhysicalDeviceMultiviewFeaturesKHR*)ext;
772 773 774 775 776
			features->multiview = true;
			features->multiviewGeometryShader = true;
			features->multiviewTessellationShader = true;
			break;
		}
777 778 779 780 781 782
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES: {
			VkPhysicalDeviceShaderDrawParameterFeatures *features =
			    (VkPhysicalDeviceShaderDrawParameterFeatures*)ext;
			features->shaderDrawParameters = true;
			break;
		}
783 784 785 786 787 788
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
			VkPhysicalDeviceProtectedMemoryFeatures *features =
			    (VkPhysicalDeviceProtectedMemoryFeatures*)ext;
			features->protectedMemory = false;
			break;
		}
789 790 791
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
			VkPhysicalDevice16BitStorageFeatures *features =
			    (VkPhysicalDevice16BitStorageFeatures*)ext;
792
			bool enabled = pdevice->rad_info.chip_class >= VI;
793 794 795 796
			features->storageBuffer16BitAccess = enabled;
			features->uniformAndStorageBuffer16BitAccess = enabled;
			features->storagePushConstant16 = enabled;
			features->storageInputOutput16 = enabled;
797 798
			break;
		}
799 800 801 802 803 804
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
			VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
			    (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)ext;
			features->samplerYcbcrConversion = false;
			break;
		}
805 806
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
			VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
807
				(VkPhysicalDeviceDescriptorIndexingFeaturesEXT*)ext;
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
			features->shaderInputAttachmentArrayDynamicIndexing = true;
			features->shaderUniformTexelBufferArrayDynamicIndexing = true;
			features->shaderStorageTexelBufferArrayDynamicIndexing = true;
			features->shaderUniformBufferArrayNonUniformIndexing = false;
			features->shaderSampledImageArrayNonUniformIndexing = false;
			features->shaderStorageBufferArrayNonUniformIndexing = false;
			features->shaderStorageImageArrayNonUniformIndexing = false;
			features->shaderInputAttachmentArrayNonUniformIndexing = false;
			features->shaderUniformTexelBufferArrayNonUniformIndexing = false;
			features->shaderStorageTexelBufferArrayNonUniformIndexing = false;
			features->descriptorBindingUniformBufferUpdateAfterBind = true;
			features->descriptorBindingSampledImageUpdateAfterBind = true;
			features->descriptorBindingStorageImageUpdateAfterBind = true;
			features->descriptorBindingStorageBufferUpdateAfterBind = true;
			features->descriptorBindingUniformTexelBufferUpdateAfterBind = true;
			features->descriptorBindingStorageTexelBufferUpdateAfterBind = true;
			features->descriptorBindingUpdateUnusedWhilePending = true;
			features->descriptorBindingPartiallyBound = true;
			features->descriptorBindingVariableDescriptorCount = true;
			features->runtimeDescriptorArray = true;
			break;
		}
830 831 832 833 834 835 836
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
			VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
				(VkPhysicalDeviceConditionalRenderingFeaturesEXT*)ext;
			features->conditionalRendering = true;
			features->inheritedConditionalRendering = false;
			break;
		}
837 838 839 840 841 842 843
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
			VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
				(VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
			features->vertexAttributeInstanceRateDivisor = VK_TRUE;
			features->vertexAttributeInstanceRateZeroDivisor = VK_TRUE;
			break;
		}
844 845 846 847 848 849 850
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
			VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
				(VkPhysicalDeviceTransformFeedbackFeaturesEXT*)ext;
			features->transformFeedback = true;
			features->geometryStreams = true;
			break;
		}
851 852 853 854 855 856
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT: {
			VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *features =
				(VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *)ext;
			features->scalarBlockLayout = pdevice->rad_info.chip_class >= CIK;
			break;
		}
857 858 859 860
		default:
			break;
		}
	}
861 862 863
	return radv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
}

864 865 866 867 868 869
void radv_GetPhysicalDeviceProperties(
	VkPhysicalDevice                            physicalDevice,
	VkPhysicalDeviceProperties*                 pProperties)
{
	RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
	VkSampleCountFlags sample_counts = 0xf;
870 871 872 873 874 875 876 877

	/* make sure that the entire descriptor set is addressable with a signed
	 * 32-bit int. So the sum of all limits scaled by descriptor size has to
	 * be at most 2 GiB. the combined image & samples object count as one of
	 * both. This limit is for the pipeline layout, not for the set layout, but
	 * there is no set limit, so we just set a pipeline limit. I don't think
	 * any app is going to hit this soon. */
	size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS) /
878 879
	          (32 /* uniform buffer, 32 due to potential space wasted on alignment */ +
	           32 /* storage buffer, 32 due to potential space wasted on alignment */ +
880 881 882 883
	           32 /* sampler, largest when combined with image */ +
	           64 /* sampled image */ +
	           64 /* storage image */);

884 885 886 887 888 889 890 891 892 893 894 895 896
	VkPhysicalDeviceLimits limits = {
		.maxImageDimension1D                      = (1 << 14),
		.maxImageDimension2D                      = (1 << 14),
		.maxImageDimension3D                      = (1 << 11),
		.maxImageDimensionCube                    = (1 << 14),
		.maxImageArrayLayers                      = (1 << 11),
		.maxTexelBufferElements                   = 128 * 1024 * 1024,
		.maxUniformBufferRange                    = UINT32_MAX,
		.maxStorageBufferRange                    = UINT32_MAX,
		.maxPushConstantsSize                     = MAX_PUSH_CONSTANTS_SIZE,
		.maxMemoryAllocationCount                 = UINT32_MAX,
		.maxSamplerAllocationCount                = 64 * 1024,
		.bufferImageGranularity                   = 64, /* A cache line */
897
		.sparseAddressSpaceSize                   = 0xffffffffu, /* buffer max size */
898
		.maxBoundDescriptorSets                   = MAX_SETS,
899 900 901 902 903 904 905 906 907
		.maxPerStageDescriptorSamplers            = max_descriptor_set_size,
		.maxPerStageDescriptorUniformBuffers      = max_descriptor_set_size,
		.maxPerStageDescriptorStorageBuffers      = max_descriptor_set_size,
		.maxPerStageDescriptorSampledImages       = max_descriptor_set_size,
		.maxPerStageDescriptorStorageImages       = max_descriptor_set_size,
		.maxPerStageDescriptorInputAttachments    = max_descriptor_set_size,
		.maxPerStageResources                     = max_descriptor_set_size,
		.maxDescriptorSetSamplers                 = max_descriptor_set_size,
		.maxDescriptorSetUniformBuffers           = max_descriptor_set_size,
908
		.maxDescriptorSetUniformBuffersDynamic    = MAX_DYNAMIC_UNIFORM_BUFFERS,
909
		.maxDescriptorSetStorageBuffers           = max_descriptor_set_size,
910
		.maxDescriptorSetStorageBuffersDynamic    = MAX_DYNAMIC_STORAGE_BUFFERS,
911 912 913
		.maxDescriptorSetSampledImages            = max_descriptor_set_size,
		.maxDescriptorSetStorageImages            = max_descriptor_set_size,
		.maxDescriptorSetInputAttachments         = max_descriptor_set_size,
914 915 916 917 918
		.maxVertexInputAttributes                 = 32,
		.maxVertexInputBindings                   = 32,
		.maxVertexInputAttributeOffset            = 2047,
		.maxVertexInputBindingStride              = 2048,
		.maxVertexOutputComponents                = 128,
919 920 921 922 923 924 925 926
		.maxTessellationGenerationLevel           = 64,
		.maxTessellationPatchSize                 = 32,
		.maxTessellationControlPerVertexInputComponents = 128,
		.maxTessellationControlPerVertexOutputComponents = 128,
		.maxTessellationControlPerPatchOutputComponents = 120,
		.maxTessellationControlTotalOutputComponents = 4096,
		.maxTessellationEvaluationInputComponents = 128,
		.maxTessellationEvaluationOutputComponents = 128,
927
		.maxGeometryShaderInvocations             = 127,
928 929 930 931 932 933
		.maxGeometryInputComponents               = 64,
		.maxGeometryOutputComponents              = 128,
		.maxGeometryOutputVertices                = 256,
		.maxGeometryTotalOutputComponents         = 1024,
		.maxFragmentInputComponents               = 128,
		.maxFragmentOutputAttachments             = 8,
934
		.maxFragmentDualSrcAttachments            = 1,
935 936 937
		.maxFragmentCombinedOutputResources       = 8,
		.maxComputeSharedMemorySize               = 32768,
		.maxComputeWorkGroupCount                 = { 65535, 65535, 65535 },
938
		.maxComputeWorkGroupInvocations           = 2048,
939
		.maxComputeWorkGroupSize = {
940 941 942
			2048,
			2048,
			2048
943 944 945 946 947 948 949 950 951 952 953
		},
		.subPixelPrecisionBits                    = 4 /* FIXME */,
		.subTexelPrecisionBits                    = 4 /* FIXME */,
		.mipmapPrecisionBits                      = 4 /* FIXME */,
		.maxDrawIndexedIndexValue                 = UINT32_MAX,
		.maxDrawIndirectCount                     = UINT32_MAX,
		.maxSamplerLodBias                        = 16,
		.maxSamplerAnisotropy                     = 16,
		.maxViewports                             = MAX_VIEWPORTS,
		.maxViewportDimensions                    = { (1 << 14), (1 << 14) },
		.viewportBoundsRange                      = { INT16_MIN, INT16_MAX },
954
		.viewportSubPixelBits                     = 8,
955 956 957 958
		.minMemoryMapAlignment                    = 4096, /* A page */
		.minTexelBufferOffsetAlignment            = 1,
		.minUniformBufferOffsetAlignment          = 4,
		.minStorageBufferOffsetAlignment          = 4,
959 960 961 962
		.minTexelOffset                           = -32,
		.maxTexelOffset                           = 31,
		.minTexelGatherOffset                     = -32,
		.maxTexelGatherOffset                     = 31,
963 964 965
		.minInterpolationOffset                   = -2,
		.maxInterpolationOffset                   = 2,
		.subPixelInterpolationOffsetBits          = 8,
966 967 968 969 970 971 972 973 974 975 976 977 978 979
		.maxFramebufferWidth                      = (1 << 14),
		.maxFramebufferHeight                     = (1 << 14),
		.maxFramebufferLayers                     = (1 << 10),
		.framebufferColorSampleCounts             = sample_counts,
		.framebufferDepthSampleCounts             = sample_counts,
		.framebufferStencilSampleCounts           = sample_counts,
		.framebufferNoAttachmentsSampleCounts     = sample_counts,
		.maxColorAttachments                      = MAX_RTS,
		.sampledImageColorSampleCounts            = sample_counts,
		.sampledImageIntegerSampleCounts          = VK_SAMPLE_COUNT_1_BIT,
		.sampledImageDepthSampleCounts            = sample_counts,
		.sampledImageStencilSampleCounts          = sample_counts,
		.storageImageSampleCounts                 = VK_SAMPLE_COUNT_1_BIT,
		.maxSampleMaskWords                       = 1,
980
		.timestampComputeAndGraphics              = true,
981
		.timestampPeriod                          = 1000000.0 / pdevice->rad_info.clock_crystal_freq,
982 983 984
		.maxClipDistances                         = 8,
		.maxCullDistances                         = 8,
		.maxCombinedClipAndCullDistances          = 8,
985
		.discreteQueuePriorities                  = 2,
986 987 988 989 990 991 992 993 994 995 996 997
		.pointSizeRange                           = { 0.125, 255.875 },
		.lineWidthRange                           = { 0.0, 7.9921875 },
		.pointSizeGranularity                     = (1.0 / 8.0),
		.lineWidthGranularity                     = (1.0 / 128.0),
		.strictLines                              = false, /* FINISHME */
		.standardSampleLocations                  = true,
		.optimalBufferCopyOffsetAlignment         = 128,
		.optimalBufferCopyRowPitchAlignment       = 128,
		.nonCoherentAtomSize                      = 64,
	};

	*pProperties = (VkPhysicalDeviceProperties) {
998
		.apiVersion = radv_physical_device_api_version(pdevice),
999
		.driverVersion = vk_get_driver_version(),
1000
		.vendorID = ATI_VENDOR_ID,
1001
		.deviceID = pdevice->rad_info.pci_id,
1002
		.deviceType = pdevice->rad_info.has_dedicated_vram ? VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU : VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
1003
		.limits = limits,
1004
		.sparseProperties = {0},
1005 1006 1007
	};

	strcpy(pProperties->deviceName, pdevice->name);
1008
	memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
1009 1010
}

1011
void radv_GetPhysicalDeviceProperties2(
1012 1013 1014
	VkPhysicalDevice                            physicalDevice,
	VkPhysicalDeviceProperties2KHR             *pProperties)
{
1015
	RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
	radv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);

	vk_foreach_struct(ext, pProperties->pNext) {
		switch (ext->sType) {
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
			VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
				(VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
			properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
			break;
		}
1026 1027
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES_KHR: {
			VkPhysicalDeviceIDPropertiesKHR *properties = (VkPhysicalDeviceIDPropertiesKHR*)ext;
1028
			memcpy(properties->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
1029 1030 1031 1032
			memcpy(properties->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
			properties->deviceLUIDValid = false;
			break;
		}
1033 1034
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES_KHR: {
			VkPhysicalDeviceMultiviewPropertiesKHR *properties = (VkPhysicalDeviceMultiviewPropertiesKHR*)ext;
1035 1036 1037 1038
			properties->maxMultiviewViewCount = MAX_VIEWS;
			properties->maxMultiviewInstanceIndex = INT_MAX;
			break;
		}
1039 1040 1041 1042 1043 1044
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES_KHR: {
			VkPhysicalDevicePointClippingPropertiesKHR *properties =
			    (VkPhysicalDevicePointClippingPropertiesKHR*)ext;
			properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES_KHR;
			break;
		}
1045 1046 1047 1048 1049 1050
		case  VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DISCARD_RECTANGLE_PROPERTIES_EXT: {
			VkPhysicalDeviceDiscardRectanglePropertiesEXT *properties =
			    (VkPhysicalDeviceDiscardRectanglePropertiesEXT*)ext;
			properties->maxDiscardRectangles = MAX_DISCARD_RECTANGLES;
			break;
		}
1051 1052 1053 1054 1055 1056
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT: {
			VkPhysicalDeviceExternalMemoryHostPropertiesEXT *properties =
			    (VkPhysicalDeviceExternalMemoryHostPropertiesEXT *) ext;
			properties->minImportedHostPointerAlignment = 4096;
			break;
		}
1057 1058 1059 1060 1061
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
			VkPhysicalDeviceSubgroupProperties *properties =
			    (VkPhysicalDeviceSubgroupProperties*)ext;
			properties->subgroupSize = 64;
			properties->supportedStages = VK_SHADER_STAGE_ALL;
1062 1063 1064
			properties->supportedOperations =
							VK_SUBGROUP_FEATURE_BASIC_BIT |
							VK_SUBGROUP_FEATURE_BALLOT_BIT |
1065 1066
							VK_SUBGROUP_FEATURE_QUAD_BIT |
							VK_SUBGROUP_FEATURE_VOTE_BIT;
1067 1068
			if (pdevice->rad_info.chip_class >= VI) {
				properties->supportedOperations |=
1069
							VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
1070 1071 1072
							VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
							VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT;
			}
1073
			properties->quadOperationsInAllStages = true;
1074 1075
			break;
		}
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
			VkPhysicalDeviceMaintenance3Properties *properties =
			    (VkPhysicalDeviceMaintenance3Properties*)ext;
			/* Make sure everything is addressable by a signed 32-bit int, and
			 * our largest descriptors are 96 bytes. */
			properties->maxPerSetDescriptors = (1ull << 31) / 96;
			/* Our buffer size fields allow only this much */
			properties->maxMemoryAllocationSize = 0xFFFFFFFFull;
			break;
		}
1086 1087 1088 1089 1090 1091 1092 1093
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT: {
			VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *properties =
				(VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *)ext;
			/* GFX6-8 only support single channel min/max filter. */
			properties->filterMinmaxImageComponentMapping = pdevice->rad_info.chip_class >= GFX9;
			properties->filterMinmaxSingleComponentFormats = true;
			break;
		}
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CORE_PROPERTIES_AMD: {
			VkPhysicalDeviceShaderCorePropertiesAMD *properties =
				(VkPhysicalDeviceShaderCorePropertiesAMD *)ext;

			/* Shader engines. */
			properties->shaderEngineCount =
				pdevice->rad_info.max_se;
			properties->shaderArraysPerEngineCount =
				pdevice->rad_info.max_sh_per_se;
			properties->computeUnitsPerShaderArray =
1104
				pdevice->rad_info.num_good_cu_per_sh;
1105 1106 1107 1108 1109 1110
			properties->simdPerComputeUnit = 4;
			properties->wavefrontsPerSimd =
				pdevice->rad_info.family == CHIP_TONGA ||
				pdevice->rad_info.family == CHIP_ICELAND ||
				pdevice->rad_info.family == CHIP_POLARIS10 ||
				pdevice->rad_info.family == CHIP_POLARIS11 ||
1111 1112
				pdevice->rad_info.family == CHIP_POLARIS12 ||
				pdevice->rad_info.family == CHIP_VEGAM ? 8 : 10;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
			properties->wavefrontSize = 64;

			/* SGPR. */
			properties->sgprsPerSimd =
				radv_get_num_physical_sgprs(pdevice);
			properties->minSgprAllocation =
				pdevice->rad_info.chip_class >= VI ? 16 : 8;
			properties->maxSgprAllocation =
				pdevice->rad_info.family == CHIP_TONGA ||
				pdevice->rad_info.family == CHIP_ICELAND ? 96 : 104;
			properties->sgprAllocationGranularity =
				pdevice->rad_info.chip_class >= VI ? 16 : 8;

			/* VGPR. */
			properties->vgprsPerSimd = RADV_NUM_PHYSICAL_VGPRS;
			properties->minVgprAllocation = 4;
			properties->maxVgprAllocation = 256;
			properties->vgprAllocationGranularity = 4;
			break;
		}
1133 1134 1135 1136 1137 1138
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
			VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *properties =
				(VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
			properties->maxVertexAttribDivisor = UINT32_MAX;
			break;
		}
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES_EXT: {
			VkPhysicalDeviceDescriptorIndexingPropertiesEXT *properties =
				(VkPhysicalDeviceDescriptorIndexingPropertiesEXT*)ext;
			properties->maxUpdateAfterBindDescriptorsInAllPools = UINT32_MAX / 64;
			properties->shaderUniformBufferArrayNonUniformIndexingNative = false;
			properties->shaderSampledImageArrayNonUniformIndexingNative = false;
			properties->shaderStorageBufferArrayNonUniformIndexingNative = false;
			properties->shaderStorageImageArrayNonUniformIndexingNative = false;
			properties->shaderInputAttachmentArrayNonUniformIndexingNative = false;
			properties->robustBufferAccessUpdateAfterBind = false;
			properties->quadDivergentImplicitLod = false;

			size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS) /
			          (32 /* uniform buffer, 32 due to potential space wasted on alignment */ +
			           32 /* storage buffer, 32 due to potential space wasted on alignment */ +
			           32 /* sampler, largest when combined with image */ +
			           64 /* sampled image */ +
			           64 /* storage image */);
			properties->maxPerStageDescriptorUpdateAfterBindSamplers = max_descriptor_set_size;
			properties->maxPerStageDescriptorUpdateAfterBindUniformBuffers = max_descriptor_set_size;
			properties->maxPerStageDescriptorUpdateAfterBindStorageBuffers = max_descriptor_set_size;
			properties->maxPerStageDescriptorUpdateAfterBindSampledImages = max_descriptor_set_size;
			properties->maxPerStageDescriptorUpdateAfterBindStorageImages = max_descriptor_set_size;
			properties->maxPerStageDescriptorUpdateAfterBindInputAttachments = max_descriptor_set_size;
			properties->maxPerStageUpdateAfterBindResources = max_descriptor_set_size;
			properties->maxDescriptorSetUpdateAfterBindSamplers = max_descriptor_set_size;
			properties->maxDescriptorSetUpdateAfterBindUniformBuffers = max_descriptor_set_size;
			properties->maxDescriptorSetUpdateAfterBindUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS;
			properties->maxDescriptorSetUpdateAfterBindStorageBuffers = max_descriptor_set_size;
			properties->maxDescriptorSetUpdateAfterBindStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS;
			properties->maxDescriptorSetUpdateAfterBindSampledImages = max_descriptor_set_size;
			properties->maxDescriptorSetUpdateAfterBindStorageImages = max_descriptor_set_size;
			properties->maxDescriptorSetUpdateAfterBindInputAttachments = max_descriptor_set_size;
			break;
		}
1174 1175 1176 1177 1178 1179
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES: {
			VkPhysicalDeviceProtectedMemoryProperties *properties =
				(VkPhysicalDeviceProtectedMemoryProperties *)ext;
			properties->protectedNoFault = false;
			break;
		}
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONSERVATIVE_RASTERIZATION_PROPERTIES_EXT: {
			VkPhysicalDeviceConservativeRasterizationPropertiesEXT *properties =
				(VkPhysicalDeviceConservativeRasterizationPropertiesEXT *)ext;
			properties->primitiveOverestimationSize = 0;
			properties->maxExtraPrimitiveOverestimationSize = 0;
			properties->extraPrimitiveOverestimationSizeGranularity = 0;
			properties->primitiveUnderestimation = VK_FALSE;
			properties->conservativePointAndLineRasterization = VK_FALSE;
			properties->degenerateTrianglesRasterized = VK_FALSE;
			properties->degenerateLinesRasterized = VK_FALSE;
			properties->fullyCoveredFragmentShaderInputVariable = VK_FALSE;
			properties->conservativeRasterizationPostDepthCoverage = VK_FALSE;
			break;
		}
1194 1195 1196 1197 1198 1199 1200 1201 1202
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT: {
			VkPhysicalDevicePCIBusInfoPropertiesEXT *properties =
				(VkPhysicalDevicePCIBusInfoPropertiesEXT *)ext;
			properties->pciDomain = pdevice->bus_info.domain;
			properties->pciBus = pdevice->bus_info.bus;
			properties->pciDevice = pdevice->bus_info.dev;
			properties->pciFunction = pdevice->bus_info.func;
			break;
		}
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR: {
			VkPhysicalDeviceDriverPropertiesKHR *driver_props =
				(VkPhysicalDeviceDriverPropertiesKHR *) ext;

			driver_props->driverID = VK_DRIVER_ID_MESA_RADV_KHR;
			memset(driver_props->driverName, 0, VK_MAX_DRIVER_NAME_SIZE_KHR);
			strcpy(driver_props->driverName, "radv");

			memset(driver_props->driverInfo, 0, VK_MAX_DRIVER_INFO_SIZE_KHR);
			snprintf(driver_props->driverInfo, VK_MAX_DRIVER_INFO_SIZE_KHR,
1213
				"Mesa " PACKAGE_VERSION MESA_GIT_SHA1
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
				" (LLVM %d.%d.%d)",
				 (HAVE_LLVM >> 8) & 0xff, HAVE_LLVM & 0xff,
				 MESA_LLVM_VERSION_PATCH);

			driver_props->conformanceVersion = (VkConformanceVersionKHR) {
				.major = 1,
				.minor = 1,
				.subminor = 2,
				.patch = 0,
			};
			break;
		}
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
			VkPhysicalDeviceTransformFeedbackPropertiesEXT *properties =
				(VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
			properties->maxTransformFeedbackStreams = MAX_SO_STREAMS;
			properties->maxTransformFeedbackBuffers = MAX_SO_BUFFERS;
			properties->maxTransformFeedbackBufferSize = UINT32_MAX;
			properties->maxTransformFeedbackStreamDataSize = 512;
			properties->maxTransformFeedbackBufferDataSize = UINT32_MAX;
			properties->maxTransformFeedbackBufferDataStride = 512;
			properties->transformFeedbackQueries = true;
			properties->transformFeedbackStreamsLinesTriangles = false;
			properties->transformFeedbackRasterizationStreamSelect = false;
			properties->transformFeedbackDraw = true;
			break;
		}
1241 1242 1243 1244
		default:
			break;
		}
	}
1245 1246
}

1247 1248
static void radv_get_physical_device_queue_family_properties(
	struct radv_physical_device*                pdevice,
1249
	uint32_t*                                   pCount,
1250
	VkQueueFamilyProperties**                    pQueueFamilyProperties)
1251
{
1252 1253
	int num_queue_families = 1;
	int idx;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
1254
	if (pdevice->rad_info.num_compute_rings > 0 &&
1255 1256
	    !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE))
		num_queue_families++;
1257

1258
	if (pQueueFamilyProperties == NULL) {
1259
		*pCount = num_queue_families;
1260 1261
		return;
	}
1262 1263 1264 1265 1266 1267

	if (!*pCount)
		return;

	idx = 0;
	if (*pCount >= 1) {
1268
		*pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
1269
			.queueFlags = VK_QUEUE_GRAPHICS_BIT |
1270 1271 1272
			              VK_QUEUE_COMPUTE_BIT |
			              VK_QUEUE_TRANSFER_BIT |
			              VK_QUEUE_SPARSE_BINDING_BIT,
1273 1274 1275 1276 1277 1278 1279
			.queueCount = 1,
			.timestampValidBits = 64,
			.minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
		};
		idx++;
	}

Nicolai Hähnle's avatar
Nicolai Hähnle committed
1280
	if (pdevice->rad_info.num_compute_rings > 0 &&
1281
	    !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE)) {
1282
		if (*pCount > idx) {
1283
			*pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
1284 1285 1286
				.queueFlags = VK_QUEUE_COMPUTE_BIT |
				              VK_QUEUE_TRANSFER_BIT |
				              VK_QUEUE_SPARSE_BINDING_BIT,
Nicolai Hähnle's avatar
Nicolai Hähnle committed
1287
				.queueCount = pdevice->rad_info.num_compute_rings,
1288 1289 1290 1291 1292 1293
				.timestampValidBits = 64,
				.minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
			};
			idx++;
		}
	}
1294
	*pCount = idx;
1295 1296
}

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
void radv_GetPhysicalDeviceQueueFamilyProperties(
	VkPhysicalDevice                            physicalDevice,
	uint32_t*                                   pCount,
	VkQueueFamilyProperties*                    pQueueFamilyProperties)
{
	RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
	if (!pQueueFamilyProperties) {
		return radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
		return;
	}
	VkQueueFamilyProperties *properties[] = {
		pQueueFamilyProperties + 0,
		pQueueFamilyProperties + 1,
		pQueueFamilyProperties + 2,
	};
	radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
	assert(*pCount <= 3);
}

1316
void radv_GetPhysicalDeviceQueueFamilyProperties2(
1317 1318 1319 1320
	VkPhysicalDevice                            physicalDevice,
	uint32_t*                                   pCount,
	VkQueueFamilyProperties2KHR                *pQueueFamilyProperties)
{
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
	if (!pQueueFamilyProperties) {
		return radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
		return;
	}
	VkQueueFamilyProperties *properties[] = {
		&pQueueFamilyProperties[0].queueFamilyProperties,
		&pQueueFamilyProperties[1].queueFamilyProperties,
		&pQueueFamilyProperties[2].queueFamilyProperties,
	};
	radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
	assert(*pCount <= 3);
1333 1334
}

1335 1336
void radv_GetPhysicalDeviceMemoryProperties(
	VkPhysicalDevice                            physicalDevice,
1337
	VkPhysicalDeviceMemoryProperties           *pMemoryProperties)
1338 1339 1340
{
	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);

1341
	*pMemoryProperties = physical_device->memory_properties;
1342 1343
}

1344
void radv_GetPhysicalDeviceMemoryProperties2(
1345 1346 1347 1348 1349 1350 1351
	VkPhysicalDevice                            physicalDevice,
	VkPhysicalDeviceMemoryProperties2KHR       *pMemoryProperties)
{
	return radv_GetPhysicalDeviceMemoryProperties(physicalDevice,
						      &pMemoryProperties->memoryProperties);
}

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
VkResult radv_GetMemoryHostPointerPropertiesEXT(
	VkDevice                                    _device,
	VkExternalMemoryHandleTypeFlagBitsKHR       handleType,
	const void                                 *pHostPointer,
	VkMemoryHostPointerPropertiesEXT           *pMemoryHostPointerProperties)
{
	RADV_FROM_HANDLE(radv_device, device, _device);

	switch (handleType)
	{
	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT: {
		const struct radv_physical_device *physical_device = device->physical_device;
		uint32_t memoryTypeBits = 0;
		for (int i = 0; i < physical_device->memory_properties.memoryTypeCount; i++) {
			if (physical_device->mem_type_indices[i] == RADV_MEM_TYPE_GTT_CACHED) {
				memoryTypeBits = (1 << i);
				break;
			}
		}
		pMemoryHostPointerProperties->memoryTypeBits = memoryTypeBits;
		return VK_SUCCESS;
	}
	default:
		return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
	}
}

1379 1380 1381 1382 1383 1384 1385 1386
static enum radeon_ctx_priority
radv_get_queue_global_priority(const VkDeviceQueueGlobalPriorityCreateInfoEXT *pObj)
{
	/* Default to MEDIUM when a specific global priority isn't requested */
	if (!pObj)
		return RADEON_CTX_PRIORITY_MEDIUM;

	switch(pObj->globalPriority) {
1387
	case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
1388
		return RADEON_CTX_PRIORITY_REALTIME;
1389
	case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
1390
		return RADEON_CTX_PRIORITY_HIGH;
1391
	case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
1392
		return RADEON_CTX_PRIORITY_MEDIUM;
1393
	case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
1394 1395 1396 1397 1398 1399 1400
		return RADEON_CTX_PRIORITY_LOW;
	default:
		unreachable("Illegal global priority value");
		return RADEON_CTX_PRIORITY_INVALID;
	}
}

1401
static int
1402
radv_queue_init(struct radv_device *device, struct radv_queue *queue,
1403
		uint32_t queue_family_index, int idx,
1404
		VkDeviceQueueCreateFlags flags,
1405
		const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority)
1406 1407 1408
{
	queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
	queue->device = device;
1409 1410
	queue->queue_family_index = queue_family_index;
	queue->queue_idx = idx;
1411
	queue->priority = radv_get_queue_global_priority(global_priority);
1412
	queue->flags = flags;
1413

1414
	queue->hw_ctx = device->ws->ctx_create(device->ws, queue->priority);
1415
	if (!queue->hw_ctx)
1416
		return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1417 1418

	return VK_SUCCESS;
1419 1420 1421 1422 1423
}

static void
radv_queue_finish(struct radv_queue *queue)
{
1424 1425
	if (queue->hw_ctx)
		queue->device->ws->ctx_destroy(queue->hw_ctx);
1426

1427 1428
	if (queue->initial_full_flush_preamble_cs)
		queue->device->ws->cs_destroy(queue->initial_full_flush_preamble_cs);
1429 1430 1431 1432
	if (queue->initial_preamble_cs)
		queue->device->ws->cs_destroy(queue->initial_preamble_cs);
	if (queue->continue_preamble_cs)
		queue->device->ws->cs_destroy(queue->continue_preamble_cs);
1433 1434 1435 1436
	if (queue->descriptor_bo)
		queue->device->ws->buffer_destroy(queue->descriptor_bo);
	if (queue->scratch_bo)
		queue->device->ws->buffer_destroy(queue->scratch_bo);
1437 1438 1439 1440
	if (queue->esgs_ring_bo)
		queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
	if (queue->gsvs_ring_bo)
		queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
1441 1442
	if (queue->tess_rings_bo)
		queue->device->ws->buffer_destroy(queue->tess_rings_bo);
1443 1444
	if (queue->compute_scratch_bo)
		queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
1445 1446
}

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
static void
radv_bo_list_init(struct radv_bo_list *bo_list)
{
	pthread_mutex_init(&bo_list->mutex, NULL);
	bo_list->list.count = bo_list->capacity = 0;
	bo_list->list.bos = NULL;
}

static void
radv_bo_list_finish(struct radv_bo_list *bo_list)
{
	free(bo_list->list.bos);
	pthread_mutex_destroy(&bo_list->mutex);
}

1462 1463
static VkResult radv_bo_list_add(struct radv_device *device,
				 struct radeon_winsys_bo *bo)
1464
{
1465 1466 1467 1468 1469
	struct radv_bo_list *bo_list = &device->bo_list;

	if (unlikely(!device->use_global_bo_list))
		return VK_SUCCESS;

1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
	pthread_mutex_lock(&bo_list->mutex);
	if (bo_list->list.count == bo_list->capacity) {
		unsigned capacity = MAX2(4, bo_list->capacity * 2);
		void *data = realloc(bo_list->list.bos, capacity * sizeof(struct radeon_winsys_bo*));

		if (!data) {
			pthread_mutex_unlock(&bo_list->mutex);
			return VK_ERROR_OUT_OF_HOST_MEMORY;
		}

		bo_list->list.bos = (struct radeon_winsys_bo**)data;
		bo_list->capacity = capacity;
	}

	bo_list->list.bos[bo_list->list.count++] = bo;
	pthread_mutex_unlock(&bo_list->mutex);
	return VK_SUCCESS;
}

1489 1490
static void radv_bo_list_remove(struct radv_device *device,
				struct radeon_winsys_bo *bo)
1491
{
1492 1493 1494 1495 1496
	struct radv_bo_list *bo_list = &device->bo_list;

	if (unlikely(!device->use_global_bo_list))
		return;

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
	pthread_mutex_lock(&bo_list->mutex);
	for(unsigned i = 0; i < bo_list->list.count; ++i) {
		if (bo_list->list.bos[i] == bo) {
			bo_list->list.bos[i] = bo_list->list.bos[bo_list->list.count - 1];
			--bo_list->list.count;
			break;
		}
	}
	pthread_mutex_unlock(&bo_list->mutex);
}

1508 1509 1510
static void
radv_device_init_gs_info(struct radv_device *device)
{
1511 1512
	device->gs_table_depth = ac_get_gs_table_depth(device->physical_device->rad_info.chip_class,
						       device->physical_device->rad_info.family);
1513 1514
}

1515 1516 1517 1518 1519 1520 1521 1522 1523
static int radv_get_device_extension_index(const char *name)
{
	for (unsigned i = 0; i < RADV_DEVICE_EXTENSION_COUNT; ++i) {
		if (strcmp(name, radv_device_extensions[i].extensionName) == 0)
			return i;
	}
	return -1;
}

1524 1525 1526 1527 1528 1529 1530