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