anv: Try the first 8 render nodes instead of just renderD128
[mesa.git] / src / intel / vulkan / anv_device.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31 #include "mesa/main/git_sha1.h"
32 #include "util/strtod.h"
33 #include "util/debug.h"
34
35 #include "genxml/gen7_pack.h"
36
37 struct anv_dispatch_table dtable;
38
39 static void
40 compiler_debug_log(void *data, const char *fmt, ...)
41 { }
42
43 static void
44 compiler_perf_log(void *data, const char *fmt, ...)
45 {
46 va_list args;
47 va_start(args, fmt);
48
49 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
50 vfprintf(stderr, fmt, args);
51
52 va_end(args);
53 }
54
55 static VkResult
56 anv_physical_device_init(struct anv_physical_device *device,
57 struct anv_instance *instance,
58 const char *path)
59 {
60 VkResult result;
61 int fd;
62
63 fd = open(path, O_RDWR | O_CLOEXEC);
64 if (fd < 0)
65 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
66 "failed to open %s: %m", path);
67
68 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
69 device->instance = instance;
70
71 assert(strlen(path) < ARRAY_SIZE(device->path));
72 strncpy(device->path, path, ARRAY_SIZE(device->path));
73
74 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
75 if (!device->chipset_id) {
76 result = VK_ERROR_INITIALIZATION_FAILED;
77 goto fail;
78 }
79
80 device->name = brw_get_device_name(device->chipset_id);
81 device->info = brw_get_device_info(device->chipset_id);
82 if (!device->info) {
83 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
84 "failed to get device info");
85 goto fail;
86 }
87
88 if (device->info->is_haswell) {
89 fprintf(stderr, "WARNING: Haswell Vulkan support is incomplete\n");
90 } else if (device->info->gen == 7 && !device->info->is_baytrail) {
91 fprintf(stderr, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
92 } else if (device->info->gen == 7 && device->info->is_baytrail) {
93 fprintf(stderr, "WARNING: Bay Trail Vulkan support is incomplete\n");
94 } else if (device->info->gen >= 8) {
95 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
96 * supported as anything */
97 } else {
98 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
99 "Vulkan not yet supported on %s", device->name);
100 goto fail;
101 }
102
103 device->cmd_parser_version = -1;
104 if (device->info->gen == 7) {
105 device->cmd_parser_version =
106 anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
107 if (device->cmd_parser_version == -1) {
108 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
109 "failed to get command parser version");
110 goto fail;
111 }
112 }
113
114 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
115 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
116 "failed to get aperture size: %m");
117 goto fail;
118 }
119
120 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
121 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
122 "kernel missing gem wait");
123 goto fail;
124 }
125
126 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
127 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
128 "kernel missing execbuf2");
129 goto fail;
130 }
131
132 if (!device->info->has_llc &&
133 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
134 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
135 "kernel missing wc mmap");
136 goto fail;
137 }
138
139 bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
140
141 close(fd);
142
143 brw_process_intel_debug_variable();
144
145 device->compiler = brw_compiler_create(NULL, device->info);
146 if (device->compiler == NULL) {
147 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
148 goto fail;
149 }
150 device->compiler->shader_debug_log = compiler_debug_log;
151 device->compiler->shader_perf_log = compiler_perf_log;
152
153 anv_init_wsi(device);
154
155 /* XXX: Actually detect bit6 swizzling */
156 isl_device_init(&device->isl_dev, device->info, swizzled);
157
158 return VK_SUCCESS;
159
160 fail:
161 close(fd);
162 return result;
163 }
164
165 static void
166 anv_physical_device_finish(struct anv_physical_device *device)
167 {
168 anv_finish_wsi(device);
169 ralloc_free(device->compiler);
170 }
171
172 static const VkExtensionProperties global_extensions[] = {
173 {
174 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
175 .specVersion = 25,
176 },
177 #ifdef VK_USE_PLATFORM_XCB_KHR
178 {
179 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
180 .specVersion = 5,
181 },
182 #endif
183 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
184 {
185 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
186 .specVersion = 4,
187 },
188 #endif
189 };
190
191 static const VkExtensionProperties device_extensions[] = {
192 {
193 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
194 .specVersion = 67,
195 },
196 };
197
198 static void *
199 default_alloc_func(void *pUserData, size_t size, size_t align,
200 VkSystemAllocationScope allocationScope)
201 {
202 return malloc(size);
203 }
204
205 static void *
206 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
207 size_t align, VkSystemAllocationScope allocationScope)
208 {
209 return realloc(pOriginal, size);
210 }
211
212 static void
213 default_free_func(void *pUserData, void *pMemory)
214 {
215 free(pMemory);
216 }
217
218 static const VkAllocationCallbacks default_alloc = {
219 .pUserData = NULL,
220 .pfnAllocation = default_alloc_func,
221 .pfnReallocation = default_realloc_func,
222 .pfnFree = default_free_func,
223 };
224
225 VkResult anv_CreateInstance(
226 const VkInstanceCreateInfo* pCreateInfo,
227 const VkAllocationCallbacks* pAllocator,
228 VkInstance* pInstance)
229 {
230 struct anv_instance *instance;
231
232 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
233
234 uint32_t client_version;
235 if (pCreateInfo->pApplicationInfo &&
236 pCreateInfo->pApplicationInfo->apiVersion != 0) {
237 client_version = pCreateInfo->pApplicationInfo->apiVersion;
238 } else {
239 client_version = VK_MAKE_VERSION(1, 0, 0);
240 }
241
242 if (VK_MAKE_VERSION(1, 0, 0) > client_version ||
243 client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {
244 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
245 "Client requested version %d.%d.%d",
246 VK_VERSION_MAJOR(client_version),
247 VK_VERSION_MINOR(client_version),
248 VK_VERSION_PATCH(client_version));
249 }
250
251 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
252 bool found = false;
253 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
254 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
255 global_extensions[j].extensionName) == 0) {
256 found = true;
257 break;
258 }
259 }
260 if (!found)
261 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
262 }
263
264 instance = anv_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
265 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
266 if (!instance)
267 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
268
269 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
270
271 if (pAllocator)
272 instance->alloc = *pAllocator;
273 else
274 instance->alloc = default_alloc;
275
276 instance->apiVersion = client_version;
277 instance->physicalDeviceCount = -1;
278
279 _mesa_locale_init();
280
281 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
282
283 *pInstance = anv_instance_to_handle(instance);
284
285 return VK_SUCCESS;
286 }
287
288 void anv_DestroyInstance(
289 VkInstance _instance,
290 const VkAllocationCallbacks* pAllocator)
291 {
292 ANV_FROM_HANDLE(anv_instance, instance, _instance);
293
294 if (instance->physicalDeviceCount > 0) {
295 /* We support at most one physical device. */
296 assert(instance->physicalDeviceCount == 1);
297 anv_physical_device_finish(&instance->physicalDevice);
298 }
299
300 VG(VALGRIND_DESTROY_MEMPOOL(instance));
301
302 _mesa_locale_fini();
303
304 anv_free(&instance->alloc, instance);
305 }
306
307 VkResult anv_EnumeratePhysicalDevices(
308 VkInstance _instance,
309 uint32_t* pPhysicalDeviceCount,
310 VkPhysicalDevice* pPhysicalDevices)
311 {
312 ANV_FROM_HANDLE(anv_instance, instance, _instance);
313 VkResult result;
314
315 if (instance->physicalDeviceCount < 0) {
316 char path[20];
317 for (unsigned i = 0; i < 8; i++) {
318 snprintf(path, sizeof(path), "/dev/dri/renderD%d", 128 + i);
319 result = anv_physical_device_init(&instance->physicalDevice,
320 instance, path);
321 if (result == VK_SUCCESS)
322 break;
323 }
324
325 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
326 instance->physicalDeviceCount = 0;
327 } else if (result == VK_SUCCESS) {
328 instance->physicalDeviceCount = 1;
329 } else {
330 return result;
331 }
332 }
333
334 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
335 * otherwise it's an inout parameter.
336 *
337 * The Vulkan spec (git aaed022) says:
338 *
339 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
340 * that is initialized with the number of devices the application is
341 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
342 * an array of at least this many VkPhysicalDevice handles [...].
343 *
344 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
345 * overwrites the contents of the variable pointed to by
346 * pPhysicalDeviceCount with the number of physical devices in in the
347 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
348 * pPhysicalDeviceCount with the number of physical handles written to
349 * pPhysicalDevices.
350 */
351 if (!pPhysicalDevices) {
352 *pPhysicalDeviceCount = instance->physicalDeviceCount;
353 } else if (*pPhysicalDeviceCount >= 1) {
354 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
355 *pPhysicalDeviceCount = 1;
356 } else {
357 *pPhysicalDeviceCount = 0;
358 }
359
360 return VK_SUCCESS;
361 }
362
363 void anv_GetPhysicalDeviceFeatures(
364 VkPhysicalDevice physicalDevice,
365 VkPhysicalDeviceFeatures* pFeatures)
366 {
367 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
368
369 *pFeatures = (VkPhysicalDeviceFeatures) {
370 .robustBufferAccess = true,
371 .fullDrawIndexUint32 = true,
372 .imageCubeArray = false,
373 .independentBlend = pdevice->info->gen >= 8,
374 .geometryShader = true,
375 .tessellationShader = false,
376 .sampleRateShading = false,
377 .dualSrcBlend = true,
378 .logicOp = true,
379 .multiDrawIndirect = false,
380 .drawIndirectFirstInstance = false,
381 .depthClamp = false,
382 .depthBiasClamp = false,
383 .fillModeNonSolid = true,
384 .depthBounds = false,
385 .wideLines = true,
386 .largePoints = true,
387 .alphaToOne = true,
388 .multiViewport = true,
389 .samplerAnisotropy = false, /* FINISHME */
390 .textureCompressionETC2 = pdevice->info->gen >= 8 ||
391 pdevice->info->is_baytrail,
392 .textureCompressionASTC_LDR = pdevice->info->gen >= 9, /* FINISHME CHV */
393 .textureCompressionBC = true,
394 .occlusionQueryPrecise = true,
395 .pipelineStatisticsQuery = false,
396 .fragmentStoresAndAtomics = true,
397 .shaderTessellationAndGeometryPointSize = true,
398 .shaderImageGatherExtended = false,
399 .shaderStorageImageExtendedFormats = false,
400 .shaderStorageImageMultisample = false,
401 .shaderUniformBufferArrayDynamicIndexing = true,
402 .shaderSampledImageArrayDynamicIndexing = true,
403 .shaderStorageBufferArrayDynamicIndexing = true,
404 .shaderStorageImageArrayDynamicIndexing = true,
405 .shaderStorageImageReadWithoutFormat = false,
406 .shaderStorageImageWriteWithoutFormat = true,
407 .shaderClipDistance = false,
408 .shaderCullDistance = false,
409 .shaderFloat64 = false,
410 .shaderInt64 = false,
411 .shaderInt16 = false,
412 .alphaToOne = true,
413 .variableMultisampleRate = false,
414 .inheritedQueries = false,
415 };
416
417 /* We can't do image stores in vec4 shaders */
418 pFeatures->vertexPipelineStoresAndAtomics =
419 pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
420 pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
421 }
422
423 void
424 anv_device_get_cache_uuid(void *uuid)
425 {
426 memset(uuid, 0, VK_UUID_SIZE);
427 snprintf(uuid, VK_UUID_SIZE, "anv-%s", MESA_GIT_SHA1 + 4);
428 }
429
430 void anv_GetPhysicalDeviceProperties(
431 VkPhysicalDevice physicalDevice,
432 VkPhysicalDeviceProperties* pProperties)
433 {
434 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
435 const struct brw_device_info *devinfo = pdevice->info;
436
437 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
438
439 const float time_stamp_base = devinfo->gen >= 9 ? 83.333 : 80.0;
440
441 VkSampleCountFlags sample_counts =
442 isl_device_get_sample_counts(&pdevice->isl_dev);
443
444 VkPhysicalDeviceLimits limits = {
445 .maxImageDimension1D = (1 << 14),
446 .maxImageDimension2D = (1 << 14),
447 .maxImageDimension3D = (1 << 11),
448 .maxImageDimensionCube = (1 << 14),
449 .maxImageArrayLayers = (1 << 11),
450 .maxTexelBufferElements = 128 * 1024 * 1024,
451 .maxUniformBufferRange = UINT32_MAX,
452 .maxStorageBufferRange = UINT32_MAX,
453 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
454 .maxMemoryAllocationCount = UINT32_MAX,
455 .maxSamplerAllocationCount = 64 * 1024,
456 .bufferImageGranularity = 64, /* A cache line */
457 .sparseAddressSpaceSize = 0,
458 .maxBoundDescriptorSets = MAX_SETS,
459 .maxPerStageDescriptorSamplers = 64,
460 .maxPerStageDescriptorUniformBuffers = 64,
461 .maxPerStageDescriptorStorageBuffers = 64,
462 .maxPerStageDescriptorSampledImages = 64,
463 .maxPerStageDescriptorStorageImages = 64,
464 .maxPerStageDescriptorInputAttachments = 64,
465 .maxPerStageResources = 128,
466 .maxDescriptorSetSamplers = 256,
467 .maxDescriptorSetUniformBuffers = 256,
468 .maxDescriptorSetUniformBuffersDynamic = 256,
469 .maxDescriptorSetStorageBuffers = 256,
470 .maxDescriptorSetStorageBuffersDynamic = 256,
471 .maxDescriptorSetSampledImages = 256,
472 .maxDescriptorSetStorageImages = 256,
473 .maxDescriptorSetInputAttachments = 256,
474 .maxVertexInputAttributes = 32,
475 .maxVertexInputBindings = 32,
476 .maxVertexInputAttributeOffset = 2047,
477 .maxVertexInputBindingStride = 2048,
478 .maxVertexOutputComponents = 128,
479 .maxTessellationGenerationLevel = 0,
480 .maxTessellationPatchSize = 0,
481 .maxTessellationControlPerVertexInputComponents = 0,
482 .maxTessellationControlPerVertexOutputComponents = 0,
483 .maxTessellationControlPerPatchOutputComponents = 0,
484 .maxTessellationControlTotalOutputComponents = 0,
485 .maxTessellationEvaluationInputComponents = 0,
486 .maxTessellationEvaluationOutputComponents = 0,
487 .maxGeometryShaderInvocations = 32,
488 .maxGeometryInputComponents = 64,
489 .maxGeometryOutputComponents = 128,
490 .maxGeometryOutputVertices = 256,
491 .maxGeometryTotalOutputComponents = 1024,
492 .maxFragmentInputComponents = 128,
493 .maxFragmentOutputAttachments = 8,
494 .maxFragmentDualSrcAttachments = 2,
495 .maxFragmentCombinedOutputResources = 8,
496 .maxComputeSharedMemorySize = 32768,
497 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
498 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
499 .maxComputeWorkGroupSize = {
500 16 * devinfo->max_cs_threads,
501 16 * devinfo->max_cs_threads,
502 16 * devinfo->max_cs_threads,
503 },
504 .subPixelPrecisionBits = 4 /* FIXME */,
505 .subTexelPrecisionBits = 4 /* FIXME */,
506 .mipmapPrecisionBits = 4 /* FIXME */,
507 .maxDrawIndexedIndexValue = UINT32_MAX,
508 .maxDrawIndirectCount = UINT32_MAX,
509 .maxSamplerLodBias = 16,
510 .maxSamplerAnisotropy = 16,
511 .maxViewports = MAX_VIEWPORTS,
512 .maxViewportDimensions = { (1 << 14), (1 << 14) },
513 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
514 .viewportSubPixelBits = 13, /* We take a float? */
515 .minMemoryMapAlignment = 4096, /* A page */
516 .minTexelBufferOffsetAlignment = 1,
517 .minUniformBufferOffsetAlignment = 1,
518 .minStorageBufferOffsetAlignment = 1,
519 .minTexelOffset = -8,
520 .maxTexelOffset = 7,
521 .minTexelGatherOffset = -8,
522 .maxTexelGatherOffset = 7,
523 .minInterpolationOffset = 0, /* FIXME */
524 .maxInterpolationOffset = 0, /* FIXME */
525 .subPixelInterpolationOffsetBits = 0, /* FIXME */
526 .maxFramebufferWidth = (1 << 14),
527 .maxFramebufferHeight = (1 << 14),
528 .maxFramebufferLayers = (1 << 10),
529 .framebufferColorSampleCounts = sample_counts,
530 .framebufferDepthSampleCounts = sample_counts,
531 .framebufferStencilSampleCounts = sample_counts,
532 .framebufferNoAttachmentsSampleCounts = sample_counts,
533 .maxColorAttachments = MAX_RTS,
534 .sampledImageColorSampleCounts = sample_counts,
535 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
536 .sampledImageDepthSampleCounts = sample_counts,
537 .sampledImageStencilSampleCounts = sample_counts,
538 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
539 .maxSampleMaskWords = 1,
540 .timestampComputeAndGraphics = false,
541 .timestampPeriod = time_stamp_base / (1000 * 1000 * 1000),
542 .maxClipDistances = 0 /* FIXME */,
543 .maxCullDistances = 0 /* FIXME */,
544 .maxCombinedClipAndCullDistances = 0 /* FIXME */,
545 .discreteQueuePriorities = 1,
546 .pointSizeRange = { 0.125, 255.875 },
547 .lineWidthRange = { 0.0, 7.9921875 },
548 .pointSizeGranularity = (1.0 / 8.0),
549 .lineWidthGranularity = (1.0 / 128.0),
550 .strictLines = false, /* FINISHME */
551 .standardSampleLocations = true,
552 .optimalBufferCopyOffsetAlignment = 128,
553 .optimalBufferCopyRowPitchAlignment = 128,
554 .nonCoherentAtomSize = 64,
555 };
556
557 *pProperties = (VkPhysicalDeviceProperties) {
558 .apiVersion = VK_MAKE_VERSION(1, 0, 5),
559 .driverVersion = 1,
560 .vendorID = 0x8086,
561 .deviceID = pdevice->chipset_id,
562 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
563 .limits = limits,
564 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
565 };
566
567 strcpy(pProperties->deviceName, pdevice->name);
568 anv_device_get_cache_uuid(pProperties->pipelineCacheUUID);
569 }
570
571 void anv_GetPhysicalDeviceQueueFamilyProperties(
572 VkPhysicalDevice physicalDevice,
573 uint32_t* pCount,
574 VkQueueFamilyProperties* pQueueFamilyProperties)
575 {
576 if (pQueueFamilyProperties == NULL) {
577 *pCount = 1;
578 return;
579 }
580
581 assert(*pCount >= 1);
582
583 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
584 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
585 VK_QUEUE_COMPUTE_BIT |
586 VK_QUEUE_TRANSFER_BIT,
587 .queueCount = 1,
588 .timestampValidBits = 36, /* XXX: Real value here */
589 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
590 };
591 }
592
593 void anv_GetPhysicalDeviceMemoryProperties(
594 VkPhysicalDevice physicalDevice,
595 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
596 {
597 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
598 VkDeviceSize heap_size;
599
600 /* Reserve some wiggle room for the driver by exposing only 75% of the
601 * aperture to the heap.
602 */
603 heap_size = 3 * physical_device->aperture_size / 4;
604
605 if (physical_device->info->has_llc) {
606 /* Big core GPUs share LLC with the CPU and thus one memory type can be
607 * both cached and coherent at the same time.
608 */
609 pMemoryProperties->memoryTypeCount = 1;
610 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
611 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
612 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
613 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
614 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
615 .heapIndex = 0,
616 };
617 } else {
618 /* The spec requires that we expose a host-visible, coherent memory
619 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
620 * to give the application a choice between cached, but not coherent and
621 * coherent but uncached (WC though).
622 */
623 pMemoryProperties->memoryTypeCount = 2;
624 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
625 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
626 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
627 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
628 .heapIndex = 0,
629 };
630 pMemoryProperties->memoryTypes[1] = (VkMemoryType) {
631 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
632 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
633 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
634 .heapIndex = 0,
635 };
636 }
637
638 pMemoryProperties->memoryHeapCount = 1;
639 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
640 .size = heap_size,
641 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
642 };
643 }
644
645 PFN_vkVoidFunction anv_GetInstanceProcAddr(
646 VkInstance instance,
647 const char* pName)
648 {
649 return anv_lookup_entrypoint(pName);
650 }
651
652 /* The loader wants us to expose a second GetInstanceProcAddr function
653 * to work around certain LD_PRELOAD issues seen in apps.
654 */
655 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
656 VkInstance instance,
657 const char* pName);
658
659 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
660 VkInstance instance,
661 const char* pName)
662 {
663 return anv_GetInstanceProcAddr(instance, pName);
664 }
665
666 PFN_vkVoidFunction anv_GetDeviceProcAddr(
667 VkDevice device,
668 const char* pName)
669 {
670 return anv_lookup_entrypoint(pName);
671 }
672
673 static VkResult
674 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
675 {
676 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
677 queue->device = device;
678 queue->pool = &device->surface_state_pool;
679
680 return VK_SUCCESS;
681 }
682
683 static void
684 anv_queue_finish(struct anv_queue *queue)
685 {
686 }
687
688 static struct anv_state
689 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
690 {
691 struct anv_state state;
692
693 state = anv_state_pool_alloc(pool, size, align);
694 memcpy(state.map, p, size);
695
696 if (!pool->block_pool->device->info.has_llc)
697 anv_state_clflush(state);
698
699 return state;
700 }
701
702 struct gen8_border_color {
703 union {
704 float float32[4];
705 uint32_t uint32[4];
706 };
707 /* Pad out to 64 bytes */
708 uint32_t _pad[12];
709 };
710
711 static void
712 anv_device_init_border_colors(struct anv_device *device)
713 {
714 static const struct gen8_border_color border_colors[] = {
715 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
716 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
717 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
718 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
719 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
720 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
721 };
722
723 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
724 sizeof(border_colors), 64,
725 border_colors);
726 }
727
728 VkResult
729 anv_device_submit_simple_batch(struct anv_device *device,
730 struct anv_batch *batch)
731 {
732 struct drm_i915_gem_execbuffer2 execbuf;
733 struct drm_i915_gem_exec_object2 exec2_objects[1];
734 struct anv_bo bo;
735 VkResult result = VK_SUCCESS;
736 uint32_t size;
737 int64_t timeout;
738 int ret;
739
740 /* Kernel driver requires 8 byte aligned batch length */
741 size = align_u32(batch->next - batch->start, 8);
742 result = anv_bo_pool_alloc(&device->batch_bo_pool, &bo, size);
743 if (result != VK_SUCCESS)
744 return result;
745
746 memcpy(bo.map, batch->start, size);
747 if (!device->info.has_llc)
748 anv_clflush_range(bo.map, size);
749
750 exec2_objects[0].handle = bo.gem_handle;
751 exec2_objects[0].relocation_count = 0;
752 exec2_objects[0].relocs_ptr = 0;
753 exec2_objects[0].alignment = 0;
754 exec2_objects[0].offset = bo.offset;
755 exec2_objects[0].flags = 0;
756 exec2_objects[0].rsvd1 = 0;
757 exec2_objects[0].rsvd2 = 0;
758
759 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
760 execbuf.buffer_count = 1;
761 execbuf.batch_start_offset = 0;
762 execbuf.batch_len = size;
763 execbuf.cliprects_ptr = 0;
764 execbuf.num_cliprects = 0;
765 execbuf.DR1 = 0;
766 execbuf.DR4 = 0;
767
768 execbuf.flags =
769 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
770 execbuf.rsvd1 = device->context_id;
771 execbuf.rsvd2 = 0;
772
773 ret = anv_gem_execbuffer(device, &execbuf);
774 if (ret != 0) {
775 /* We don't know the real error. */
776 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
777 goto fail;
778 }
779
780 timeout = INT64_MAX;
781 ret = anv_gem_wait(device, bo.gem_handle, &timeout);
782 if (ret != 0) {
783 /* We don't know the real error. */
784 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
785 goto fail;
786 }
787
788 fail:
789 anv_bo_pool_free(&device->batch_bo_pool, &bo);
790
791 return result;
792 }
793
794 VkResult anv_CreateDevice(
795 VkPhysicalDevice physicalDevice,
796 const VkDeviceCreateInfo* pCreateInfo,
797 const VkAllocationCallbacks* pAllocator,
798 VkDevice* pDevice)
799 {
800 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
801 VkResult result;
802 struct anv_device *device;
803
804 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
805
806 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
807 bool found = false;
808 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
809 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
810 device_extensions[j].extensionName) == 0) {
811 found = true;
812 break;
813 }
814 }
815 if (!found)
816 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
817 }
818
819 anv_set_dispatch_devinfo(physical_device->info);
820
821 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
822 sizeof(*device), 8,
823 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
824 if (!device)
825 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
826
827 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
828 device->instance = physical_device->instance;
829 device->chipset_id = physical_device->chipset_id;
830
831 if (pAllocator)
832 device->alloc = *pAllocator;
833 else
834 device->alloc = physical_device->instance->alloc;
835
836 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
837 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
838 if (device->fd == -1) {
839 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
840 goto fail_device;
841 }
842
843 device->context_id = anv_gem_create_context(device);
844 if (device->context_id == -1) {
845 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
846 goto fail_fd;
847 }
848
849 device->info = *physical_device->info;
850 device->isl_dev = physical_device->isl_dev;
851
852 /* On Broadwell and later, we can use batch chaining to more efficiently
853 * implement growing command buffers. Prior to Haswell, the kernel
854 * command parser gets in the way and we have to fall back to growing
855 * the batch.
856 */
857 device->can_chain_batches = device->info.gen >= 8;
858
859 device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
860 pCreateInfo->pEnabledFeatures->robustBufferAccess;
861
862 pthread_mutex_init(&device->mutex, NULL);
863
864 anv_bo_pool_init(&device->batch_bo_pool, device);
865
866 anv_block_pool_init(&device->dynamic_state_block_pool, device, 16384);
867
868 anv_state_pool_init(&device->dynamic_state_pool,
869 &device->dynamic_state_block_pool);
870
871 anv_block_pool_init(&device->instruction_block_pool, device, 128 * 1024);
872 anv_pipeline_cache_init(&device->default_pipeline_cache, device);
873
874 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
875
876 anv_state_pool_init(&device->surface_state_pool,
877 &device->surface_state_block_pool);
878
879 anv_bo_init_new(&device->workaround_bo, device, 1024);
880
881 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
882
883 anv_queue_init(device, &device->queue);
884
885 switch (device->info.gen) {
886 case 7:
887 if (!device->info.is_haswell)
888 result = gen7_init_device_state(device);
889 else
890 result = gen75_init_device_state(device);
891 break;
892 case 8:
893 result = gen8_init_device_state(device);
894 break;
895 case 9:
896 result = gen9_init_device_state(device);
897 break;
898 default:
899 /* Shouldn't get here as we don't create physical devices for any other
900 * gens. */
901 unreachable("unhandled gen");
902 }
903 if (result != VK_SUCCESS)
904 goto fail_fd;
905
906 result = anv_device_init_meta(device);
907 if (result != VK_SUCCESS)
908 goto fail_fd;
909
910 anv_device_init_border_colors(device);
911
912 *pDevice = anv_device_to_handle(device);
913
914 return VK_SUCCESS;
915
916 fail_fd:
917 close(device->fd);
918 fail_device:
919 anv_free(&device->alloc, device);
920
921 return result;
922 }
923
924 void anv_DestroyDevice(
925 VkDevice _device,
926 const VkAllocationCallbacks* pAllocator)
927 {
928 ANV_FROM_HANDLE(anv_device, device, _device);
929
930 anv_queue_finish(&device->queue);
931
932 anv_device_finish_meta(device);
933
934 #ifdef HAVE_VALGRIND
935 /* We only need to free these to prevent valgrind errors. The backing
936 * BO will go away in a couple of lines so we don't actually leak.
937 */
938 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
939 #endif
940
941 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
942 anv_gem_close(device, device->workaround_bo.gem_handle);
943
944 anv_bo_pool_finish(&device->batch_bo_pool);
945 anv_state_pool_finish(&device->dynamic_state_pool);
946 anv_block_pool_finish(&device->dynamic_state_block_pool);
947 anv_block_pool_finish(&device->instruction_block_pool);
948 anv_state_pool_finish(&device->surface_state_pool);
949 anv_block_pool_finish(&device->surface_state_block_pool);
950 anv_block_pool_finish(&device->scratch_block_pool);
951
952 close(device->fd);
953
954 pthread_mutex_destroy(&device->mutex);
955
956 anv_free(&device->alloc, device);
957 }
958
959 VkResult anv_EnumerateInstanceExtensionProperties(
960 const char* pLayerName,
961 uint32_t* pPropertyCount,
962 VkExtensionProperties* pProperties)
963 {
964 if (pProperties == NULL) {
965 *pPropertyCount = ARRAY_SIZE(global_extensions);
966 return VK_SUCCESS;
967 }
968
969 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
970
971 *pPropertyCount = ARRAY_SIZE(global_extensions);
972 memcpy(pProperties, global_extensions, sizeof(global_extensions));
973
974 return VK_SUCCESS;
975 }
976
977 VkResult anv_EnumerateDeviceExtensionProperties(
978 VkPhysicalDevice physicalDevice,
979 const char* pLayerName,
980 uint32_t* pPropertyCount,
981 VkExtensionProperties* pProperties)
982 {
983 if (pProperties == NULL) {
984 *pPropertyCount = ARRAY_SIZE(device_extensions);
985 return VK_SUCCESS;
986 }
987
988 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
989
990 *pPropertyCount = ARRAY_SIZE(device_extensions);
991 memcpy(pProperties, device_extensions, sizeof(device_extensions));
992
993 return VK_SUCCESS;
994 }
995
996 VkResult anv_EnumerateInstanceLayerProperties(
997 uint32_t* pPropertyCount,
998 VkLayerProperties* pProperties)
999 {
1000 if (pProperties == NULL) {
1001 *pPropertyCount = 0;
1002 return VK_SUCCESS;
1003 }
1004
1005 /* None supported at this time */
1006 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1007 }
1008
1009 VkResult anv_EnumerateDeviceLayerProperties(
1010 VkPhysicalDevice physicalDevice,
1011 uint32_t* pPropertyCount,
1012 VkLayerProperties* pProperties)
1013 {
1014 if (pProperties == NULL) {
1015 *pPropertyCount = 0;
1016 return VK_SUCCESS;
1017 }
1018
1019 /* None supported at this time */
1020 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1021 }
1022
1023 void anv_GetDeviceQueue(
1024 VkDevice _device,
1025 uint32_t queueNodeIndex,
1026 uint32_t queueIndex,
1027 VkQueue* pQueue)
1028 {
1029 ANV_FROM_HANDLE(anv_device, device, _device);
1030
1031 assert(queueIndex == 0);
1032
1033 *pQueue = anv_queue_to_handle(&device->queue);
1034 }
1035
1036 VkResult anv_QueueSubmit(
1037 VkQueue _queue,
1038 uint32_t submitCount,
1039 const VkSubmitInfo* pSubmits,
1040 VkFence _fence)
1041 {
1042 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1043 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1044 struct anv_device *device = queue->device;
1045 int ret;
1046
1047 for (uint32_t i = 0; i < submitCount; i++) {
1048 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1049 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
1050 pSubmits[i].pCommandBuffers[j]);
1051 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1052
1053 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
1054 if (ret != 0) {
1055 /* We don't know the real error. */
1056 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1057 "execbuf2 failed: %m");
1058 }
1059
1060 for (uint32_t k = 0; k < cmd_buffer->execbuf2.bo_count; k++)
1061 cmd_buffer->execbuf2.bos[k]->offset = cmd_buffer->execbuf2.objects[k].offset;
1062 }
1063 }
1064
1065 if (fence) {
1066 ret = anv_gem_execbuffer(device, &fence->execbuf);
1067 if (ret != 0) {
1068 /* We don't know the real error. */
1069 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1070 "execbuf2 failed: %m");
1071 }
1072 }
1073
1074 return VK_SUCCESS;
1075 }
1076
1077 VkResult anv_QueueWaitIdle(
1078 VkQueue _queue)
1079 {
1080 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1081
1082 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
1083 }
1084
1085 VkResult anv_DeviceWaitIdle(
1086 VkDevice _device)
1087 {
1088 ANV_FROM_HANDLE(anv_device, device, _device);
1089 struct anv_batch batch;
1090
1091 uint32_t cmds[8];
1092 batch.start = batch.next = cmds;
1093 batch.end = (void *) cmds + sizeof(cmds);
1094
1095 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1096 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1097
1098 return anv_device_submit_simple_batch(device, &batch);
1099 }
1100
1101 VkResult
1102 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
1103 {
1104 bo->gem_handle = anv_gem_create(device, size);
1105 if (!bo->gem_handle)
1106 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1107
1108 bo->map = NULL;
1109 bo->index = 0;
1110 bo->offset = 0;
1111 bo->size = size;
1112 bo->is_winsys_bo = false;
1113
1114 return VK_SUCCESS;
1115 }
1116
1117 VkResult anv_AllocateMemory(
1118 VkDevice _device,
1119 const VkMemoryAllocateInfo* pAllocateInfo,
1120 const VkAllocationCallbacks* pAllocator,
1121 VkDeviceMemory* pMem)
1122 {
1123 ANV_FROM_HANDLE(anv_device, device, _device);
1124 struct anv_device_memory *mem;
1125 VkResult result;
1126
1127 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1128
1129 if (pAllocateInfo->allocationSize == 0) {
1130 /* Apparently, this is allowed */
1131 *pMem = VK_NULL_HANDLE;
1132 return VK_SUCCESS;
1133 }
1134
1135 /* We support exactly one memory heap. */
1136 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1137 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
1138
1139 /* FINISHME: Fail if allocation request exceeds heap size. */
1140
1141 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1142 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1143 if (mem == NULL)
1144 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1145
1146 /* The kernel is going to give us whole pages anyway */
1147 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1148
1149 result = anv_bo_init_new(&mem->bo, device, alloc_size);
1150 if (result != VK_SUCCESS)
1151 goto fail;
1152
1153 mem->type_index = pAllocateInfo->memoryTypeIndex;
1154
1155 *pMem = anv_device_memory_to_handle(mem);
1156
1157 return VK_SUCCESS;
1158
1159 fail:
1160 anv_free2(&device->alloc, pAllocator, mem);
1161
1162 return result;
1163 }
1164
1165 void anv_FreeMemory(
1166 VkDevice _device,
1167 VkDeviceMemory _mem,
1168 const VkAllocationCallbacks* pAllocator)
1169 {
1170 ANV_FROM_HANDLE(anv_device, device, _device);
1171 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1172
1173 if (mem == NULL)
1174 return;
1175
1176 if (mem->bo.map)
1177 anv_gem_munmap(mem->bo.map, mem->bo.size);
1178
1179 if (mem->bo.gem_handle != 0)
1180 anv_gem_close(device, mem->bo.gem_handle);
1181
1182 anv_free2(&device->alloc, pAllocator, mem);
1183 }
1184
1185 VkResult anv_MapMemory(
1186 VkDevice _device,
1187 VkDeviceMemory _memory,
1188 VkDeviceSize offset,
1189 VkDeviceSize size,
1190 VkMemoryMapFlags flags,
1191 void** ppData)
1192 {
1193 ANV_FROM_HANDLE(anv_device, device, _device);
1194 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1195
1196 if (mem == NULL) {
1197 *ppData = NULL;
1198 return VK_SUCCESS;
1199 }
1200
1201 if (size == VK_WHOLE_SIZE)
1202 size = mem->bo.size - offset;
1203
1204 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1205 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1206 * at a time is valid. We could just mmap up front and return an offset
1207 * pointer here, but that may exhaust virtual memory on 32 bit
1208 * userspace. */
1209
1210 uint32_t gem_flags = 0;
1211 if (!device->info.has_llc && mem->type_index == 0)
1212 gem_flags |= I915_MMAP_WC;
1213
1214 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1215 uint64_t map_offset = offset & ~4095ull;
1216 assert(offset >= map_offset);
1217 uint64_t map_size = (offset + size) - map_offset;
1218
1219 /* Let's map whole pages */
1220 map_size = align_u64(map_size, 4096);
1221
1222 mem->map = anv_gem_mmap(device, mem->bo.gem_handle,
1223 map_offset, map_size, gem_flags);
1224 mem->map_size = map_size;
1225
1226 *ppData = mem->map + (offset - map_offset);
1227
1228 return VK_SUCCESS;
1229 }
1230
1231 void anv_UnmapMemory(
1232 VkDevice _device,
1233 VkDeviceMemory _memory)
1234 {
1235 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1236
1237 if (mem == NULL)
1238 return;
1239
1240 anv_gem_munmap(mem->map, mem->map_size);
1241 }
1242
1243 static void
1244 clflush_mapped_ranges(struct anv_device *device,
1245 uint32_t count,
1246 const VkMappedMemoryRange *ranges)
1247 {
1248 for (uint32_t i = 0; i < count; i++) {
1249 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1250 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
1251 void *end;
1252
1253 if (ranges[i].offset + ranges[i].size > mem->map_size)
1254 end = mem->map + mem->map_size;
1255 else
1256 end = mem->map + ranges[i].offset + ranges[i].size;
1257
1258 while (p < end) {
1259 __builtin_ia32_clflush(p);
1260 p += CACHELINE_SIZE;
1261 }
1262 }
1263 }
1264
1265 VkResult anv_FlushMappedMemoryRanges(
1266 VkDevice _device,
1267 uint32_t memoryRangeCount,
1268 const VkMappedMemoryRange* pMemoryRanges)
1269 {
1270 ANV_FROM_HANDLE(anv_device, device, _device);
1271
1272 if (device->info.has_llc)
1273 return VK_SUCCESS;
1274
1275 /* Make sure the writes we're flushing have landed. */
1276 __builtin_ia32_mfence();
1277
1278 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1279
1280 return VK_SUCCESS;
1281 }
1282
1283 VkResult anv_InvalidateMappedMemoryRanges(
1284 VkDevice _device,
1285 uint32_t memoryRangeCount,
1286 const VkMappedMemoryRange* pMemoryRanges)
1287 {
1288 ANV_FROM_HANDLE(anv_device, device, _device);
1289
1290 if (device->info.has_llc)
1291 return VK_SUCCESS;
1292
1293 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1294
1295 /* Make sure no reads get moved up above the invalidate. */
1296 __builtin_ia32_mfence();
1297
1298 return VK_SUCCESS;
1299 }
1300
1301 void anv_GetBufferMemoryRequirements(
1302 VkDevice device,
1303 VkBuffer _buffer,
1304 VkMemoryRequirements* pMemoryRequirements)
1305 {
1306 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1307
1308 /* The Vulkan spec (git aaed022) says:
1309 *
1310 * memoryTypeBits is a bitfield and contains one bit set for every
1311 * supported memory type for the resource. The bit `1<<i` is set if and
1312 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1313 * structure for the physical device is supported.
1314 *
1315 * We support exactly one memory type.
1316 */
1317 pMemoryRequirements->memoryTypeBits = 1;
1318
1319 pMemoryRequirements->size = buffer->size;
1320 pMemoryRequirements->alignment = 16;
1321 }
1322
1323 void anv_GetImageMemoryRequirements(
1324 VkDevice device,
1325 VkImage _image,
1326 VkMemoryRequirements* pMemoryRequirements)
1327 {
1328 ANV_FROM_HANDLE(anv_image, image, _image);
1329
1330 /* The Vulkan spec (git aaed022) says:
1331 *
1332 * memoryTypeBits is a bitfield and contains one bit set for every
1333 * supported memory type for the resource. The bit `1<<i` is set if and
1334 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1335 * structure for the physical device is supported.
1336 *
1337 * We support exactly one memory type.
1338 */
1339 pMemoryRequirements->memoryTypeBits = 1;
1340
1341 pMemoryRequirements->size = image->size;
1342 pMemoryRequirements->alignment = image->alignment;
1343 }
1344
1345 void anv_GetImageSparseMemoryRequirements(
1346 VkDevice device,
1347 VkImage image,
1348 uint32_t* pSparseMemoryRequirementCount,
1349 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1350 {
1351 stub();
1352 }
1353
1354 void anv_GetDeviceMemoryCommitment(
1355 VkDevice device,
1356 VkDeviceMemory memory,
1357 VkDeviceSize* pCommittedMemoryInBytes)
1358 {
1359 *pCommittedMemoryInBytes = 0;
1360 }
1361
1362 VkResult anv_BindBufferMemory(
1363 VkDevice device,
1364 VkBuffer _buffer,
1365 VkDeviceMemory _memory,
1366 VkDeviceSize memoryOffset)
1367 {
1368 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1369 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1370
1371 if (mem) {
1372 buffer->bo = &mem->bo;
1373 buffer->offset = memoryOffset;
1374 } else {
1375 buffer->bo = NULL;
1376 buffer->offset = 0;
1377 }
1378
1379 return VK_SUCCESS;
1380 }
1381
1382 VkResult anv_BindImageMemory(
1383 VkDevice device,
1384 VkImage _image,
1385 VkDeviceMemory _memory,
1386 VkDeviceSize memoryOffset)
1387 {
1388 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1389 ANV_FROM_HANDLE(anv_image, image, _image);
1390
1391 if (mem) {
1392 image->bo = &mem->bo;
1393 image->offset = memoryOffset;
1394 } else {
1395 image->bo = NULL;
1396 image->offset = 0;
1397 }
1398
1399 return VK_SUCCESS;
1400 }
1401
1402 VkResult anv_QueueBindSparse(
1403 VkQueue queue,
1404 uint32_t bindInfoCount,
1405 const VkBindSparseInfo* pBindInfo,
1406 VkFence fence)
1407 {
1408 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1409 }
1410
1411 VkResult anv_CreateFence(
1412 VkDevice _device,
1413 const VkFenceCreateInfo* pCreateInfo,
1414 const VkAllocationCallbacks* pAllocator,
1415 VkFence* pFence)
1416 {
1417 ANV_FROM_HANDLE(anv_device, device, _device);
1418 struct anv_bo fence_bo;
1419 struct anv_fence *fence;
1420 struct anv_batch batch;
1421 VkResult result;
1422
1423 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1424
1425 result = anv_bo_pool_alloc(&device->batch_bo_pool, &fence_bo, 4096);
1426 if (result != VK_SUCCESS)
1427 return result;
1428
1429 /* Fences are small. Just store the CPU data structure in the BO. */
1430 fence = fence_bo.map;
1431 fence->bo = fence_bo;
1432
1433 /* Place the batch after the CPU data but on its own cache line. */
1434 const uint32_t batch_offset = align_u32(sizeof(*fence), CACHELINE_SIZE);
1435 batch.next = batch.start = fence->bo.map + batch_offset;
1436 batch.end = fence->bo.map + fence->bo.size;
1437 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1438 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1439
1440 if (!device->info.has_llc) {
1441 assert(((uintptr_t) batch.start & CACHELINE_MASK) == 0);
1442 assert(batch.next - batch.start <= CACHELINE_SIZE);
1443 __builtin_ia32_mfence();
1444 __builtin_ia32_clflush(batch.start);
1445 }
1446
1447 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1448 fence->exec2_objects[0].relocation_count = 0;
1449 fence->exec2_objects[0].relocs_ptr = 0;
1450 fence->exec2_objects[0].alignment = 0;
1451 fence->exec2_objects[0].offset = fence->bo.offset;
1452 fence->exec2_objects[0].flags = 0;
1453 fence->exec2_objects[0].rsvd1 = 0;
1454 fence->exec2_objects[0].rsvd2 = 0;
1455
1456 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1457 fence->execbuf.buffer_count = 1;
1458 fence->execbuf.batch_start_offset = batch.start - fence->bo.map;
1459 fence->execbuf.batch_len = batch.next - batch.start;
1460 fence->execbuf.cliprects_ptr = 0;
1461 fence->execbuf.num_cliprects = 0;
1462 fence->execbuf.DR1 = 0;
1463 fence->execbuf.DR4 = 0;
1464
1465 fence->execbuf.flags =
1466 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1467 fence->execbuf.rsvd1 = device->context_id;
1468 fence->execbuf.rsvd2 = 0;
1469
1470 fence->ready = false;
1471
1472 *pFence = anv_fence_to_handle(fence);
1473
1474 return VK_SUCCESS;
1475 }
1476
1477 void anv_DestroyFence(
1478 VkDevice _device,
1479 VkFence _fence,
1480 const VkAllocationCallbacks* pAllocator)
1481 {
1482 ANV_FROM_HANDLE(anv_device, device, _device);
1483 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1484
1485 assert(fence->bo.map == fence);
1486 anv_bo_pool_free(&device->batch_bo_pool, &fence->bo);
1487 }
1488
1489 VkResult anv_ResetFences(
1490 VkDevice _device,
1491 uint32_t fenceCount,
1492 const VkFence* pFences)
1493 {
1494 for (uint32_t i = 0; i < fenceCount; i++) {
1495 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1496 fence->ready = false;
1497 }
1498
1499 return VK_SUCCESS;
1500 }
1501
1502 VkResult anv_GetFenceStatus(
1503 VkDevice _device,
1504 VkFence _fence)
1505 {
1506 ANV_FROM_HANDLE(anv_device, device, _device);
1507 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1508 int64_t t = 0;
1509 int ret;
1510
1511 if (fence->ready)
1512 return VK_SUCCESS;
1513
1514 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1515 if (ret == 0) {
1516 fence->ready = true;
1517 return VK_SUCCESS;
1518 }
1519
1520 return VK_NOT_READY;
1521 }
1522
1523 VkResult anv_WaitForFences(
1524 VkDevice _device,
1525 uint32_t fenceCount,
1526 const VkFence* pFences,
1527 VkBool32 waitAll,
1528 uint64_t timeout)
1529 {
1530 ANV_FROM_HANDLE(anv_device, device, _device);
1531
1532 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1533 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1534 * for a couple of kernel releases. Since there's no way to know
1535 * whether or not the kernel we're using is one of the broken ones, the
1536 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1537 * maximum timeout from 584 years to 292 years - likely not a big deal.
1538 */
1539 if (timeout > INT64_MAX)
1540 timeout = INT64_MAX;
1541
1542 int64_t t = timeout;
1543
1544 /* FIXME: handle !waitAll */
1545
1546 for (uint32_t i = 0; i < fenceCount; i++) {
1547 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1548 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1549 if (ret == -1 && errno == ETIME) {
1550 return VK_TIMEOUT;
1551 } else if (ret == -1) {
1552 /* We don't know the real error. */
1553 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1554 "gem wait failed: %m");
1555 }
1556 }
1557
1558 return VK_SUCCESS;
1559 }
1560
1561 // Queue semaphore functions
1562
1563 VkResult anv_CreateSemaphore(
1564 VkDevice device,
1565 const VkSemaphoreCreateInfo* pCreateInfo,
1566 const VkAllocationCallbacks* pAllocator,
1567 VkSemaphore* pSemaphore)
1568 {
1569 /* The DRM execbuffer ioctl always execute in-oder, even between different
1570 * rings. As such, there's nothing to do for the user space semaphore.
1571 */
1572
1573 *pSemaphore = (VkSemaphore)1;
1574
1575 return VK_SUCCESS;
1576 }
1577
1578 void anv_DestroySemaphore(
1579 VkDevice device,
1580 VkSemaphore semaphore,
1581 const VkAllocationCallbacks* pAllocator)
1582 {
1583 }
1584
1585 // Event functions
1586
1587 VkResult anv_CreateEvent(
1588 VkDevice _device,
1589 const VkEventCreateInfo* pCreateInfo,
1590 const VkAllocationCallbacks* pAllocator,
1591 VkEvent* pEvent)
1592 {
1593 ANV_FROM_HANDLE(anv_device, device, _device);
1594 struct anv_state state;
1595 struct anv_event *event;
1596
1597 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
1598
1599 state = anv_state_pool_alloc(&device->dynamic_state_pool,
1600 sizeof(*event), 8);
1601 event = state.map;
1602 event->state = state;
1603 event->semaphore = VK_EVENT_RESET;
1604
1605 if (!device->info.has_llc) {
1606 /* Make sure the writes we're flushing have landed. */
1607 __builtin_ia32_mfence();
1608 __builtin_ia32_clflush(event);
1609 }
1610
1611 *pEvent = anv_event_to_handle(event);
1612
1613 return VK_SUCCESS;
1614 }
1615
1616 void anv_DestroyEvent(
1617 VkDevice _device,
1618 VkEvent _event,
1619 const VkAllocationCallbacks* pAllocator)
1620 {
1621 ANV_FROM_HANDLE(anv_device, device, _device);
1622 ANV_FROM_HANDLE(anv_event, event, _event);
1623
1624 anv_state_pool_free(&device->dynamic_state_pool, event->state);
1625 }
1626
1627 VkResult anv_GetEventStatus(
1628 VkDevice _device,
1629 VkEvent _event)
1630 {
1631 ANV_FROM_HANDLE(anv_device, device, _device);
1632 ANV_FROM_HANDLE(anv_event, event, _event);
1633
1634 if (!device->info.has_llc) {
1635 /* Invalidate read cache before reading event written by GPU. */
1636 __builtin_ia32_clflush(event);
1637 __builtin_ia32_mfence();
1638
1639 }
1640
1641 return event->semaphore;
1642 }
1643
1644 VkResult anv_SetEvent(
1645 VkDevice _device,
1646 VkEvent _event)
1647 {
1648 ANV_FROM_HANDLE(anv_device, device, _device);
1649 ANV_FROM_HANDLE(anv_event, event, _event);
1650
1651 event->semaphore = VK_EVENT_SET;
1652
1653 if (!device->info.has_llc) {
1654 /* Make sure the writes we're flushing have landed. */
1655 __builtin_ia32_mfence();
1656 __builtin_ia32_clflush(event);
1657 }
1658
1659 return VK_SUCCESS;
1660 }
1661
1662 VkResult anv_ResetEvent(
1663 VkDevice _device,
1664 VkEvent _event)
1665 {
1666 ANV_FROM_HANDLE(anv_device, device, _device);
1667 ANV_FROM_HANDLE(anv_event, event, _event);
1668
1669 event->semaphore = VK_EVENT_RESET;
1670
1671 if (!device->info.has_llc) {
1672 /* Make sure the writes we're flushing have landed. */
1673 __builtin_ia32_mfence();
1674 __builtin_ia32_clflush(event);
1675 }
1676
1677 return VK_SUCCESS;
1678 }
1679
1680 // Buffer functions
1681
1682 VkResult anv_CreateBuffer(
1683 VkDevice _device,
1684 const VkBufferCreateInfo* pCreateInfo,
1685 const VkAllocationCallbacks* pAllocator,
1686 VkBuffer* pBuffer)
1687 {
1688 ANV_FROM_HANDLE(anv_device, device, _device);
1689 struct anv_buffer *buffer;
1690
1691 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1692
1693 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1694 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1695 if (buffer == NULL)
1696 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1697
1698 buffer->size = pCreateInfo->size;
1699 buffer->usage = pCreateInfo->usage;
1700 buffer->bo = NULL;
1701 buffer->offset = 0;
1702
1703 *pBuffer = anv_buffer_to_handle(buffer);
1704
1705 return VK_SUCCESS;
1706 }
1707
1708 void anv_DestroyBuffer(
1709 VkDevice _device,
1710 VkBuffer _buffer,
1711 const VkAllocationCallbacks* pAllocator)
1712 {
1713 ANV_FROM_HANDLE(anv_device, device, _device);
1714 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1715
1716 anv_free2(&device->alloc, pAllocator, buffer);
1717 }
1718
1719 void
1720 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
1721 enum isl_format format,
1722 uint32_t offset, uint32_t range, uint32_t stride)
1723 {
1724 isl_buffer_fill_state(&device->isl_dev, state.map,
1725 .address = offset,
1726 .mocs = device->default_mocs,
1727 .size = range,
1728 .format = format,
1729 .stride = stride);
1730
1731 if (!device->info.has_llc)
1732 anv_state_clflush(state);
1733 }
1734
1735 void anv_DestroySampler(
1736 VkDevice _device,
1737 VkSampler _sampler,
1738 const VkAllocationCallbacks* pAllocator)
1739 {
1740 ANV_FROM_HANDLE(anv_device, device, _device);
1741 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1742
1743 anv_free2(&device->alloc, pAllocator, sampler);
1744 }
1745
1746 VkResult anv_CreateFramebuffer(
1747 VkDevice _device,
1748 const VkFramebufferCreateInfo* pCreateInfo,
1749 const VkAllocationCallbacks* pAllocator,
1750 VkFramebuffer* pFramebuffer)
1751 {
1752 ANV_FROM_HANDLE(anv_device, device, _device);
1753 struct anv_framebuffer *framebuffer;
1754
1755 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1756
1757 size_t size = sizeof(*framebuffer) +
1758 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1759 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1760 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1761 if (framebuffer == NULL)
1762 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1763
1764 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1765 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1766 VkImageView _iview = pCreateInfo->pAttachments[i];
1767 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1768 }
1769
1770 framebuffer->width = pCreateInfo->width;
1771 framebuffer->height = pCreateInfo->height;
1772 framebuffer->layers = pCreateInfo->layers;
1773
1774 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1775
1776 return VK_SUCCESS;
1777 }
1778
1779 void anv_DestroyFramebuffer(
1780 VkDevice _device,
1781 VkFramebuffer _fb,
1782 const VkAllocationCallbacks* pAllocator)
1783 {
1784 ANV_FROM_HANDLE(anv_device, device, _device);
1785 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1786
1787 anv_free2(&device->alloc, pAllocator, fb);
1788 }
1789
1790 void vkCmdDbgMarkerBegin(
1791 VkCommandBuffer commandBuffer,
1792 const char* pMarker)
1793 __attribute__ ((visibility ("default")));
1794
1795 void vkCmdDbgMarkerEnd(
1796 VkCommandBuffer commandBuffer)
1797 __attribute__ ((visibility ("default")));
1798
1799 void vkCmdDbgMarkerBegin(
1800 VkCommandBuffer commandBuffer,
1801 const char* pMarker)
1802 {
1803 }
1804
1805 void vkCmdDbgMarkerEnd(
1806 VkCommandBuffer commandBuffer)
1807 {
1808 }