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