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