384a457742f81ba9e90a1185a49302b34b36445e
[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 /* The property flags below are valid only for llc platforms. */
557 pMemoryProperties->memoryTypeCount = 1;
558 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
559 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
560 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
561 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
562 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
563 .heapIndex = 1,
564 };
565
566 pMemoryProperties->memoryHeapCount = 1;
567 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
568 .size = heap_size,
569 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
570 };
571 }
572
573 PFN_vkVoidFunction anv_GetInstanceProcAddr(
574 VkInstance instance,
575 const char* pName)
576 {
577 return anv_lookup_entrypoint(pName);
578 }
579
580 PFN_vkVoidFunction anv_GetDeviceProcAddr(
581 VkDevice device,
582 const char* pName)
583 {
584 return anv_lookup_entrypoint(pName);
585 }
586
587 static VkResult
588 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
589 {
590 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
591 queue->device = device;
592 queue->pool = &device->surface_state_pool;
593
594 return VK_SUCCESS;
595 }
596
597 static void
598 anv_queue_finish(struct anv_queue *queue)
599 {
600 }
601
602 static struct anv_state
603 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
604 {
605 struct anv_state state;
606
607 state = anv_state_pool_alloc(pool, size, align);
608 memcpy(state.map, p, size);
609
610 if (!pool->block_pool->device->info.has_llc)
611 anv_state_clflush(state);
612
613 return state;
614 }
615
616 static void
617 anv_device_init_border_colors(struct anv_device *device)
618 {
619 static const VkClearColorValue border_colors[] = {
620 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
621 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
622 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
623 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
624 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
625 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
626 };
627
628 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
629 sizeof(border_colors), 32, border_colors);
630 }
631
632 VkResult anv_CreateDevice(
633 VkPhysicalDevice physicalDevice,
634 const VkDeviceCreateInfo* pCreateInfo,
635 const VkAllocationCallbacks* pAllocator,
636 VkDevice* pDevice)
637 {
638 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
639 struct anv_device *device;
640
641 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
642
643 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
644 bool found = false;
645 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
646 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
647 device_extensions[j].extensionName) == 0) {
648 found = true;
649 break;
650 }
651 }
652 if (!found)
653 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
654 }
655
656 anv_set_dispatch_devinfo(physical_device->info);
657
658 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
659 sizeof(*device), 8,
660 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
661 if (!device)
662 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
663
664 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
665 device->instance = physical_device->instance;
666
667 if (pAllocator)
668 device->alloc = *pAllocator;
669 else
670 device->alloc = physical_device->instance->alloc;
671
672 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
673 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
674 if (device->fd == -1)
675 goto fail_device;
676
677 device->context_id = anv_gem_create_context(device);
678 if (device->context_id == -1)
679 goto fail_fd;
680
681 pthread_mutex_init(&device->mutex, NULL);
682
683 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
684
685 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
686
687 anv_state_pool_init(&device->dynamic_state_pool,
688 &device->dynamic_state_block_pool);
689
690 anv_block_pool_init(&device->instruction_block_pool, device, 4096);
691 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
692
693 anv_state_pool_init(&device->surface_state_pool,
694 &device->surface_state_block_pool);
695
696 anv_bo_init_new(&device->workaround_bo, device, 1024);
697
698 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
699
700 device->info = *physical_device->info;
701 device->isl_dev = physical_device->isl_dev;
702
703 anv_queue_init(device, &device->queue);
704
705 anv_device_init_meta(device);
706
707 anv_device_init_border_colors(device);
708
709 *pDevice = anv_device_to_handle(device);
710
711 return VK_SUCCESS;
712
713 fail_fd:
714 close(device->fd);
715 fail_device:
716 anv_free(&device->alloc, device);
717
718 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
719 }
720
721 void anv_DestroyDevice(
722 VkDevice _device,
723 const VkAllocationCallbacks* pAllocator)
724 {
725 ANV_FROM_HANDLE(anv_device, device, _device);
726
727 anv_queue_finish(&device->queue);
728
729 anv_device_finish_meta(device);
730
731 #ifdef HAVE_VALGRIND
732 /* We only need to free these to prevent valgrind errors. The backing
733 * BO will go away in a couple of lines so we don't actually leak.
734 */
735 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
736 #endif
737
738 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
739 anv_gem_close(device, device->workaround_bo.gem_handle);
740
741 anv_bo_pool_finish(&device->batch_bo_pool);
742 anv_state_pool_finish(&device->dynamic_state_pool);
743 anv_block_pool_finish(&device->dynamic_state_block_pool);
744 anv_block_pool_finish(&device->instruction_block_pool);
745 anv_state_pool_finish(&device->surface_state_pool);
746 anv_block_pool_finish(&device->surface_state_block_pool);
747 anv_block_pool_finish(&device->scratch_block_pool);
748
749 close(device->fd);
750
751 anv_free(&device->alloc, device);
752 }
753
754 VkResult anv_EnumerateInstanceExtensionProperties(
755 const char* pLayerName,
756 uint32_t* pPropertyCount,
757 VkExtensionProperties* pProperties)
758 {
759 if (pProperties == NULL) {
760 *pPropertyCount = ARRAY_SIZE(global_extensions);
761 return VK_SUCCESS;
762 }
763
764 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
765
766 *pPropertyCount = ARRAY_SIZE(global_extensions);
767 memcpy(pProperties, global_extensions, sizeof(global_extensions));
768
769 return VK_SUCCESS;
770 }
771
772 VkResult anv_EnumerateDeviceExtensionProperties(
773 VkPhysicalDevice physicalDevice,
774 const char* pLayerName,
775 uint32_t* pPropertyCount,
776 VkExtensionProperties* pProperties)
777 {
778 if (pProperties == NULL) {
779 *pPropertyCount = ARRAY_SIZE(device_extensions);
780 return VK_SUCCESS;
781 }
782
783 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
784
785 *pPropertyCount = ARRAY_SIZE(device_extensions);
786 memcpy(pProperties, device_extensions, sizeof(device_extensions));
787
788 return VK_SUCCESS;
789 }
790
791 VkResult anv_EnumerateInstanceLayerProperties(
792 uint32_t* pPropertyCount,
793 VkLayerProperties* pProperties)
794 {
795 if (pProperties == NULL) {
796 *pPropertyCount = 0;
797 return VK_SUCCESS;
798 }
799
800 /* None supported at this time */
801 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
802 }
803
804 VkResult anv_EnumerateDeviceLayerProperties(
805 VkPhysicalDevice physicalDevice,
806 uint32_t* pPropertyCount,
807 VkLayerProperties* pProperties)
808 {
809 if (pProperties == NULL) {
810 *pPropertyCount = 0;
811 return VK_SUCCESS;
812 }
813
814 /* None supported at this time */
815 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
816 }
817
818 void anv_GetDeviceQueue(
819 VkDevice _device,
820 uint32_t queueNodeIndex,
821 uint32_t queueIndex,
822 VkQueue* pQueue)
823 {
824 ANV_FROM_HANDLE(anv_device, device, _device);
825
826 assert(queueIndex == 0);
827
828 *pQueue = anv_queue_to_handle(&device->queue);
829 }
830
831 VkResult anv_QueueSubmit(
832 VkQueue _queue,
833 uint32_t submitCount,
834 const VkSubmitInfo* pSubmits,
835 VkFence _fence)
836 {
837 ANV_FROM_HANDLE(anv_queue, queue, _queue);
838 ANV_FROM_HANDLE(anv_fence, fence, _fence);
839 struct anv_device *device = queue->device;
840 int ret;
841
842 for (uint32_t i = 0; i < submitCount; i++) {
843 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
844 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
845 pSubmits[i].pCommandBuffers[j]);
846 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
847
848 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
849 if (ret != 0) {
850 /* We don't know the real error. */
851 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
852 "execbuf2 failed: %m");
853 }
854
855 if (fence) {
856 ret = anv_gem_execbuffer(device, &fence->execbuf);
857 if (ret != 0) {
858 /* We don't know the real error. */
859 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
860 "execbuf2 failed: %m");
861 }
862 }
863
864 for (uint32_t k = 0; k < cmd_buffer->execbuf2.bo_count; k++)
865 cmd_buffer->execbuf2.bos[k]->offset = cmd_buffer->execbuf2.objects[k].offset;
866 }
867 }
868
869 return VK_SUCCESS;
870 }
871
872 VkResult anv_QueueWaitIdle(
873 VkQueue _queue)
874 {
875 ANV_FROM_HANDLE(anv_queue, queue, _queue);
876
877 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
878 }
879
880 VkResult anv_DeviceWaitIdle(
881 VkDevice _device)
882 {
883 ANV_FROM_HANDLE(anv_device, device, _device);
884 struct anv_state state;
885 struct anv_batch batch;
886 struct drm_i915_gem_execbuffer2 execbuf;
887 struct drm_i915_gem_exec_object2 exec2_objects[1];
888 struct anv_bo *bo = NULL;
889 VkResult result;
890 int64_t timeout;
891 int ret;
892
893 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
894 bo = &device->dynamic_state_pool.block_pool->bo;
895 batch.start = batch.next = state.map;
896 batch.end = state.map + 32;
897 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
898 anv_batch_emit(&batch, GEN7_MI_NOOP);
899
900 if (!device->info.has_llc)
901 anv_state_clflush(state);
902
903 exec2_objects[0].handle = bo->gem_handle;
904 exec2_objects[0].relocation_count = 0;
905 exec2_objects[0].relocs_ptr = 0;
906 exec2_objects[0].alignment = 0;
907 exec2_objects[0].offset = bo->offset;
908 exec2_objects[0].flags = 0;
909 exec2_objects[0].rsvd1 = 0;
910 exec2_objects[0].rsvd2 = 0;
911
912 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
913 execbuf.buffer_count = 1;
914 execbuf.batch_start_offset = state.offset;
915 execbuf.batch_len = batch.next - state.map;
916 execbuf.cliprects_ptr = 0;
917 execbuf.num_cliprects = 0;
918 execbuf.DR1 = 0;
919 execbuf.DR4 = 0;
920
921 execbuf.flags =
922 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
923 execbuf.rsvd1 = device->context_id;
924 execbuf.rsvd2 = 0;
925
926 ret = anv_gem_execbuffer(device, &execbuf);
927 if (ret != 0) {
928 /* We don't know the real error. */
929 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
930 goto fail;
931 }
932
933 timeout = INT64_MAX;
934 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
935 if (ret != 0) {
936 /* We don't know the real error. */
937 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
938 goto fail;
939 }
940
941 anv_state_pool_free(&device->dynamic_state_pool, state);
942
943 return VK_SUCCESS;
944
945 fail:
946 anv_state_pool_free(&device->dynamic_state_pool, state);
947
948 return result;
949 }
950
951 VkResult
952 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
953 {
954 bo->gem_handle = anv_gem_create(device, size);
955 if (!bo->gem_handle)
956 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
957
958 bo->map = NULL;
959 bo->index = 0;
960 bo->offset = 0;
961 bo->size = size;
962
963 return VK_SUCCESS;
964 }
965
966 VkResult anv_AllocateMemory(
967 VkDevice _device,
968 const VkMemoryAllocateInfo* pAllocateInfo,
969 const VkAllocationCallbacks* pAllocator,
970 VkDeviceMemory* pMem)
971 {
972 ANV_FROM_HANDLE(anv_device, device, _device);
973 struct anv_device_memory *mem;
974 VkResult result;
975
976 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
977
978 /* We support exactly one memory heap. */
979 assert(pAllocateInfo->memoryTypeIndex == 0);
980
981 /* FINISHME: Fail if allocation request exceeds heap size. */
982
983 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
984 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
985 if (mem == NULL)
986 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
987
988 result = anv_bo_init_new(&mem->bo, device, pAllocateInfo->allocationSize);
989 if (result != VK_SUCCESS)
990 goto fail;
991
992 *pMem = anv_device_memory_to_handle(mem);
993
994 return VK_SUCCESS;
995
996 fail:
997 anv_free2(&device->alloc, pAllocator, mem);
998
999 return result;
1000 }
1001
1002 void anv_FreeMemory(
1003 VkDevice _device,
1004 VkDeviceMemory _mem,
1005 const VkAllocationCallbacks* pAllocator)
1006 {
1007 ANV_FROM_HANDLE(anv_device, device, _device);
1008 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1009
1010 if (mem->bo.map)
1011 anv_gem_munmap(mem->bo.map, mem->bo.size);
1012
1013 if (mem->bo.gem_handle != 0)
1014 anv_gem_close(device, mem->bo.gem_handle);
1015
1016 anv_free2(&device->alloc, pAllocator, mem);
1017 }
1018
1019 VkResult anv_MapMemory(
1020 VkDevice _device,
1021 VkDeviceMemory _memory,
1022 VkDeviceSize offset,
1023 VkDeviceSize size,
1024 VkMemoryMapFlags flags,
1025 void** ppData)
1026 {
1027 ANV_FROM_HANDLE(anv_device, device, _device);
1028 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1029
1030 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1031 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1032 * at a time is valid. We could just mmap up front and return an offset
1033 * pointer here, but that may exhaust virtual memory on 32 bit
1034 * userspace. */
1035
1036 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
1037 mem->map_size = size;
1038
1039 *ppData = mem->map;
1040
1041 return VK_SUCCESS;
1042 }
1043
1044 void anv_UnmapMemory(
1045 VkDevice _device,
1046 VkDeviceMemory _memory)
1047 {
1048 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1049
1050 anv_gem_munmap(mem->map, mem->map_size);
1051 }
1052
1053 static void
1054 clflush_mapped_ranges(struct anv_device *device,
1055 uint32_t count,
1056 const VkMappedMemoryRange *ranges)
1057 {
1058 for (uint32_t i = 0; i < count; i++) {
1059 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1060 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
1061 void *end = mem->map + ranges[i].offset + ranges[i].size;
1062
1063 while (p < end) {
1064 __builtin_ia32_clflush(p);
1065 p += CACHELINE_SIZE;
1066 }
1067 }
1068 }
1069
1070 VkResult anv_FlushMappedMemoryRanges(
1071 VkDevice _device,
1072 uint32_t memoryRangeCount,
1073 const VkMappedMemoryRange* pMemoryRanges)
1074 {
1075 ANV_FROM_HANDLE(anv_device, device, _device);
1076
1077 if (device->info.has_llc)
1078 return VK_SUCCESS;
1079
1080 /* Make sure the writes we're flushing have landed. */
1081 __builtin_ia32_sfence();
1082
1083 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1084
1085 return VK_SUCCESS;
1086 }
1087
1088 VkResult anv_InvalidateMappedMemoryRanges(
1089 VkDevice _device,
1090 uint32_t memoryRangeCount,
1091 const VkMappedMemoryRange* pMemoryRanges)
1092 {
1093 ANV_FROM_HANDLE(anv_device, device, _device);
1094
1095 if (device->info.has_llc)
1096 return VK_SUCCESS;
1097
1098 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1099
1100 /* Make sure no reads get moved up above the invalidate. */
1101 __builtin_ia32_lfence();
1102
1103 return VK_SUCCESS;
1104 }
1105
1106 void anv_GetBufferMemoryRequirements(
1107 VkDevice device,
1108 VkBuffer _buffer,
1109 VkMemoryRequirements* pMemoryRequirements)
1110 {
1111 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1112
1113 /* The Vulkan spec (git aaed022) says:
1114 *
1115 * memoryTypeBits is a bitfield and contains one bit set for every
1116 * supported memory type for the resource. The bit `1<<i` is set if and
1117 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1118 * structure for the physical device is supported.
1119 *
1120 * We support exactly one memory type.
1121 */
1122 pMemoryRequirements->memoryTypeBits = 1;
1123
1124 pMemoryRequirements->size = buffer->size;
1125 pMemoryRequirements->alignment = 16;
1126 }
1127
1128 void anv_GetImageMemoryRequirements(
1129 VkDevice device,
1130 VkImage _image,
1131 VkMemoryRequirements* pMemoryRequirements)
1132 {
1133 ANV_FROM_HANDLE(anv_image, image, _image);
1134
1135 /* The Vulkan spec (git aaed022) says:
1136 *
1137 * memoryTypeBits is a bitfield and contains one bit set for every
1138 * supported memory type for the resource. The bit `1<<i` is set if and
1139 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1140 * structure for the physical device is supported.
1141 *
1142 * We support exactly one memory type.
1143 */
1144 pMemoryRequirements->memoryTypeBits = 1;
1145
1146 pMemoryRequirements->size = image->size;
1147 pMemoryRequirements->alignment = image->alignment;
1148 }
1149
1150 void anv_GetImageSparseMemoryRequirements(
1151 VkDevice device,
1152 VkImage image,
1153 uint32_t* pSparseMemoryRequirementCount,
1154 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1155 {
1156 stub();
1157 }
1158
1159 void anv_GetDeviceMemoryCommitment(
1160 VkDevice device,
1161 VkDeviceMemory memory,
1162 VkDeviceSize* pCommittedMemoryInBytes)
1163 {
1164 *pCommittedMemoryInBytes = 0;
1165 }
1166
1167 VkResult anv_BindBufferMemory(
1168 VkDevice device,
1169 VkBuffer _buffer,
1170 VkDeviceMemory _memory,
1171 VkDeviceSize memoryOffset)
1172 {
1173 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1174 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1175
1176 buffer->bo = &mem->bo;
1177 buffer->offset = memoryOffset;
1178
1179 return VK_SUCCESS;
1180 }
1181
1182 VkResult anv_BindImageMemory(
1183 VkDevice device,
1184 VkImage _image,
1185 VkDeviceMemory _memory,
1186 VkDeviceSize memoryOffset)
1187 {
1188 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1189 ANV_FROM_HANDLE(anv_image, image, _image);
1190
1191 image->bo = &mem->bo;
1192 image->offset = memoryOffset;
1193
1194 return VK_SUCCESS;
1195 }
1196
1197 VkResult anv_QueueBindSparse(
1198 VkQueue queue,
1199 uint32_t bindInfoCount,
1200 const VkBindSparseInfo* pBindInfo,
1201 VkFence fence)
1202 {
1203 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1204 }
1205
1206 VkResult anv_CreateFence(
1207 VkDevice _device,
1208 const VkFenceCreateInfo* pCreateInfo,
1209 const VkAllocationCallbacks* pAllocator,
1210 VkFence* pFence)
1211 {
1212 ANV_FROM_HANDLE(anv_device, device, _device);
1213 struct anv_fence *fence;
1214 struct anv_batch batch;
1215 VkResult result;
1216
1217 const uint32_t fence_size = 128;
1218
1219 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1220
1221 fence = anv_alloc2(&device->alloc, pAllocator, sizeof(*fence), 8,
1222 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1223 if (fence == NULL)
1224 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1225
1226 result = anv_bo_init_new(&fence->bo, device, fence_size);
1227 if (result != VK_SUCCESS)
1228 goto fail;
1229
1230 fence->bo.map =
1231 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1232 batch.next = batch.start = fence->bo.map;
1233 batch.end = fence->bo.map + fence->bo.size;
1234 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1235 anv_batch_emit(&batch, GEN7_MI_NOOP);
1236
1237 if (!device->info.has_llc) {
1238 assert(((uintptr_t) fence->bo.map & CACHELINE_MASK) == 0);
1239 assert(batch.next - fence->bo.map <= CACHELINE_SIZE);
1240 __builtin_ia32_sfence();
1241 __builtin_ia32_clflush(fence->bo.map);
1242 }
1243
1244 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1245 fence->exec2_objects[0].relocation_count = 0;
1246 fence->exec2_objects[0].relocs_ptr = 0;
1247 fence->exec2_objects[0].alignment = 0;
1248 fence->exec2_objects[0].offset = fence->bo.offset;
1249 fence->exec2_objects[0].flags = 0;
1250 fence->exec2_objects[0].rsvd1 = 0;
1251 fence->exec2_objects[0].rsvd2 = 0;
1252
1253 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1254 fence->execbuf.buffer_count = 1;
1255 fence->execbuf.batch_start_offset = 0;
1256 fence->execbuf.batch_len = batch.next - fence->bo.map;
1257 fence->execbuf.cliprects_ptr = 0;
1258 fence->execbuf.num_cliprects = 0;
1259 fence->execbuf.DR1 = 0;
1260 fence->execbuf.DR4 = 0;
1261
1262 fence->execbuf.flags =
1263 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1264 fence->execbuf.rsvd1 = device->context_id;
1265 fence->execbuf.rsvd2 = 0;
1266
1267 *pFence = anv_fence_to_handle(fence);
1268
1269 return VK_SUCCESS;
1270
1271 fail:
1272 anv_free2(&device->alloc, pAllocator, fence);
1273
1274 return result;
1275 }
1276
1277 void anv_DestroyFence(
1278 VkDevice _device,
1279 VkFence _fence,
1280 const VkAllocationCallbacks* pAllocator)
1281 {
1282 ANV_FROM_HANDLE(anv_device, device, _device);
1283 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1284
1285 anv_gem_munmap(fence->bo.map, fence->bo.size);
1286 anv_gem_close(device, fence->bo.gem_handle);
1287 anv_free2(&device->alloc, pAllocator, fence);
1288 }
1289
1290 VkResult anv_ResetFences(
1291 VkDevice _device,
1292 uint32_t fenceCount,
1293 const VkFence* pFences)
1294 {
1295 for (uint32_t i = 0; i < fenceCount; i++) {
1296 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1297 fence->ready = false;
1298 }
1299
1300 return VK_SUCCESS;
1301 }
1302
1303 VkResult anv_GetFenceStatus(
1304 VkDevice _device,
1305 VkFence _fence)
1306 {
1307 ANV_FROM_HANDLE(anv_device, device, _device);
1308 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1309 int64_t t = 0;
1310 int ret;
1311
1312 if (fence->ready)
1313 return VK_SUCCESS;
1314
1315 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1316 if (ret == 0) {
1317 fence->ready = true;
1318 return VK_SUCCESS;
1319 }
1320
1321 return VK_NOT_READY;
1322 }
1323
1324 VkResult anv_WaitForFences(
1325 VkDevice _device,
1326 uint32_t fenceCount,
1327 const VkFence* pFences,
1328 VkBool32 waitAll,
1329 uint64_t timeout)
1330 {
1331 ANV_FROM_HANDLE(anv_device, device, _device);
1332
1333 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1334 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1335 * for a couple of kernel releases. Since there's no way to know
1336 * whether or not the kernel we're using is one of the broken ones, the
1337 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1338 * maximum timeout from 584 years to 292 years - likely not a big deal.
1339 */
1340 if (timeout > INT64_MAX)
1341 timeout = INT64_MAX;
1342
1343 int64_t t = timeout;
1344
1345 /* FIXME: handle !waitAll */
1346
1347 for (uint32_t i = 0; i < fenceCount; i++) {
1348 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1349 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1350 if (ret == -1 && errno == ETIME) {
1351 return VK_TIMEOUT;
1352 } else if (ret == -1) {
1353 /* We don't know the real error. */
1354 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1355 "gem wait failed: %m");
1356 }
1357 }
1358
1359 return VK_SUCCESS;
1360 }
1361
1362 // Queue semaphore functions
1363
1364 VkResult anv_CreateSemaphore(
1365 VkDevice device,
1366 const VkSemaphoreCreateInfo* pCreateInfo,
1367 const VkAllocationCallbacks* pAllocator,
1368 VkSemaphore* pSemaphore)
1369 {
1370 *pSemaphore = (VkSemaphore)1;
1371 stub_return(VK_SUCCESS);
1372 }
1373
1374 void anv_DestroySemaphore(
1375 VkDevice device,
1376 VkSemaphore semaphore,
1377 const VkAllocationCallbacks* pAllocator)
1378 {
1379 stub();
1380 }
1381
1382 // Event functions
1383
1384 VkResult anv_CreateEvent(
1385 VkDevice device,
1386 const VkEventCreateInfo* pCreateInfo,
1387 const VkAllocationCallbacks* pAllocator,
1388 VkEvent* pEvent)
1389 {
1390 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1391 }
1392
1393 void anv_DestroyEvent(
1394 VkDevice device,
1395 VkEvent event,
1396 const VkAllocationCallbacks* pAllocator)
1397 {
1398 stub();
1399 }
1400
1401 VkResult anv_GetEventStatus(
1402 VkDevice device,
1403 VkEvent event)
1404 {
1405 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1406 }
1407
1408 VkResult anv_SetEvent(
1409 VkDevice device,
1410 VkEvent event)
1411 {
1412 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1413 }
1414
1415 VkResult anv_ResetEvent(
1416 VkDevice device,
1417 VkEvent event)
1418 {
1419 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1420 }
1421
1422 // Buffer functions
1423
1424 VkResult anv_CreateBuffer(
1425 VkDevice _device,
1426 const VkBufferCreateInfo* pCreateInfo,
1427 const VkAllocationCallbacks* pAllocator,
1428 VkBuffer* pBuffer)
1429 {
1430 ANV_FROM_HANDLE(anv_device, device, _device);
1431 struct anv_buffer *buffer;
1432
1433 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1434
1435 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1436 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1437 if (buffer == NULL)
1438 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1439
1440 buffer->size = pCreateInfo->size;
1441 buffer->bo = NULL;
1442 buffer->offset = 0;
1443
1444 *pBuffer = anv_buffer_to_handle(buffer);
1445
1446 return VK_SUCCESS;
1447 }
1448
1449 void anv_DestroyBuffer(
1450 VkDevice _device,
1451 VkBuffer _buffer,
1452 const VkAllocationCallbacks* pAllocator)
1453 {
1454 ANV_FROM_HANDLE(anv_device, device, _device);
1455 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1456
1457 anv_free2(&device->alloc, pAllocator, buffer);
1458 }
1459
1460 void
1461 anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1462 const struct anv_format *format,
1463 uint32_t offset, uint32_t range, uint32_t stride)
1464 {
1465 switch (device->info.gen) {
1466 case 7:
1467 if (device->info.is_haswell)
1468 gen75_fill_buffer_surface_state(state, format, offset, range, stride);
1469 else
1470 gen7_fill_buffer_surface_state(state, format, offset, range, stride);
1471 break;
1472 case 8:
1473 gen8_fill_buffer_surface_state(state, format, offset, range, stride);
1474 break;
1475 case 9:
1476 gen9_fill_buffer_surface_state(state, format, offset, range, stride);
1477 break;
1478 default:
1479 unreachable("unsupported gen\n");
1480 }
1481 }
1482
1483 VkResult anv_CreateBufferView(
1484 VkDevice _device,
1485 const VkBufferViewCreateInfo* pCreateInfo,
1486 const VkAllocationCallbacks* pAllocator,
1487 VkBufferView* pView)
1488 {
1489 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1490 }
1491
1492 void anv_DestroyBufferView(
1493 VkDevice _device,
1494 VkBufferView _bview,
1495 const VkAllocationCallbacks* pAllocator)
1496 {
1497 stub();
1498 }
1499
1500 void anv_DestroySampler(
1501 VkDevice _device,
1502 VkSampler _sampler,
1503 const VkAllocationCallbacks* pAllocator)
1504 {
1505 ANV_FROM_HANDLE(anv_device, device, _device);
1506 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1507
1508 anv_free2(&device->alloc, pAllocator, sampler);
1509 }
1510
1511 VkResult anv_CreateFramebuffer(
1512 VkDevice _device,
1513 const VkFramebufferCreateInfo* pCreateInfo,
1514 const VkAllocationCallbacks* pAllocator,
1515 VkFramebuffer* pFramebuffer)
1516 {
1517 ANV_FROM_HANDLE(anv_device, device, _device);
1518 struct anv_framebuffer *framebuffer;
1519
1520 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1521
1522 size_t size = sizeof(*framebuffer) +
1523 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1524 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1525 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1526 if (framebuffer == NULL)
1527 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1528
1529 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1530 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1531 VkImageView _iview = pCreateInfo->pAttachments[i];
1532 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1533 }
1534
1535 framebuffer->width = pCreateInfo->width;
1536 framebuffer->height = pCreateInfo->height;
1537 framebuffer->layers = pCreateInfo->layers;
1538
1539 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1540
1541 return VK_SUCCESS;
1542 }
1543
1544 void anv_DestroyFramebuffer(
1545 VkDevice _device,
1546 VkFramebuffer _fb,
1547 const VkAllocationCallbacks* pAllocator)
1548 {
1549 ANV_FROM_HANDLE(anv_device, device, _device);
1550 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1551
1552 anv_free2(&device->alloc, pAllocator, fb);
1553 }
1554
1555 void vkCmdDbgMarkerBegin(
1556 VkCommandBuffer commandBuffer,
1557 const char* pMarker)
1558 __attribute__ ((visibility ("default")));
1559
1560 void vkCmdDbgMarkerEnd(
1561 VkCommandBuffer commandBuffer)
1562 __attribute__ ((visibility ("default")));
1563
1564 void vkCmdDbgMarkerBegin(
1565 VkCommandBuffer commandBuffer,
1566 const char* pMarker)
1567 {
1568 }
1569
1570 void vkCmdDbgMarkerEnd(
1571 VkCommandBuffer commandBuffer)
1572 {
1573 }