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