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