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