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