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