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