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