Merge remote-tracking branch 'origin/master' into vulkan
[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 assert(size < device->batch_bo_pool.bo_size);
720 result = anv_bo_pool_alloc(&device->batch_bo_pool, &bo);
721 if (result != VK_SUCCESS)
722 return result;
723
724 memcpy(bo.map, batch->start, size);
725 if (!device->info.has_llc)
726 anv_clflush_range(bo.map, size);
727
728 exec2_objects[0].handle = bo.gem_handle;
729 exec2_objects[0].relocation_count = 0;
730 exec2_objects[0].relocs_ptr = 0;
731 exec2_objects[0].alignment = 0;
732 exec2_objects[0].offset = bo.offset;
733 exec2_objects[0].flags = 0;
734 exec2_objects[0].rsvd1 = 0;
735 exec2_objects[0].rsvd2 = 0;
736
737 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
738 execbuf.buffer_count = 1;
739 execbuf.batch_start_offset = 0;
740 execbuf.batch_len = size;
741 execbuf.cliprects_ptr = 0;
742 execbuf.num_cliprects = 0;
743 execbuf.DR1 = 0;
744 execbuf.DR4 = 0;
745
746 execbuf.flags =
747 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
748 execbuf.rsvd1 = device->context_id;
749 execbuf.rsvd2 = 0;
750
751 ret = anv_gem_execbuffer(device, &execbuf);
752 if (ret != 0) {
753 /* We don't know the real error. */
754 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
755 goto fail;
756 }
757
758 timeout = INT64_MAX;
759 ret = anv_gem_wait(device, bo.gem_handle, &timeout);
760 if (ret != 0) {
761 /* We don't know the real error. */
762 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
763 goto fail;
764 }
765
766 fail:
767 anv_bo_pool_free(&device->batch_bo_pool, &bo);
768
769 return result;
770 }
771
772 VkResult anv_CreateDevice(
773 VkPhysicalDevice physicalDevice,
774 const VkDeviceCreateInfo* pCreateInfo,
775 const VkAllocationCallbacks* pAllocator,
776 VkDevice* pDevice)
777 {
778 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
779 VkResult result;
780 struct anv_device *device;
781
782 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
783
784 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
785 bool found = false;
786 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
787 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
788 device_extensions[j].extensionName) == 0) {
789 found = true;
790 break;
791 }
792 }
793 if (!found)
794 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
795 }
796
797 anv_set_dispatch_devinfo(physical_device->info);
798
799 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
800 sizeof(*device), 8,
801 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
802 if (!device)
803 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
804
805 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
806 device->instance = physical_device->instance;
807 device->chipset_id = physical_device->chipset_id;
808
809 if (pAllocator)
810 device->alloc = *pAllocator;
811 else
812 device->alloc = physical_device->instance->alloc;
813
814 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
815 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
816 if (device->fd == -1) {
817 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
818 goto fail_device;
819 }
820
821 device->context_id = anv_gem_create_context(device);
822 if (device->context_id == -1) {
823 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
824 goto fail_fd;
825 }
826
827 device->info = *physical_device->info;
828 device->isl_dev = physical_device->isl_dev;
829
830 pthread_mutex_init(&device->mutex, NULL);
831
832 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
833
834 anv_block_pool_init(&device->dynamic_state_block_pool, device, 16384);
835
836 anv_state_pool_init(&device->dynamic_state_pool,
837 &device->dynamic_state_block_pool);
838
839 anv_block_pool_init(&device->instruction_block_pool, device, 128 * 1024);
840 anv_pipeline_cache_init(&device->default_pipeline_cache, device);
841
842 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
843
844 anv_state_pool_init(&device->surface_state_pool,
845 &device->surface_state_block_pool);
846
847 anv_bo_init_new(&device->workaround_bo, device, 1024);
848
849 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
850
851 anv_queue_init(device, &device->queue);
852
853 switch (device->info.gen) {
854 case 7:
855 if (!device->info.is_haswell)
856 result = gen7_init_device_state(device);
857 else
858 result = gen75_init_device_state(device);
859 break;
860 case 8:
861 result = gen8_init_device_state(device);
862 break;
863 case 9:
864 result = gen9_init_device_state(device);
865 break;
866 default:
867 /* Shouldn't get here as we don't create physical devices for any other
868 * gens. */
869 unreachable("unhandled gen");
870 }
871 if (result != VK_SUCCESS)
872 goto fail_fd;
873
874 result = anv_device_init_meta(device);
875 if (result != VK_SUCCESS)
876 goto fail_fd;
877
878 anv_device_init_border_colors(device);
879
880 *pDevice = anv_device_to_handle(device);
881
882 return VK_SUCCESS;
883
884 fail_fd:
885 close(device->fd);
886 fail_device:
887 anv_free(&device->alloc, device);
888
889 return result;
890 }
891
892 void anv_DestroyDevice(
893 VkDevice _device,
894 const VkAllocationCallbacks* pAllocator)
895 {
896 ANV_FROM_HANDLE(anv_device, device, _device);
897
898 anv_queue_finish(&device->queue);
899
900 anv_device_finish_meta(device);
901
902 #ifdef HAVE_VALGRIND
903 /* We only need to free these to prevent valgrind errors. The backing
904 * BO will go away in a couple of lines so we don't actually leak.
905 */
906 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
907 #endif
908
909 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
910 anv_gem_close(device, device->workaround_bo.gem_handle);
911
912 anv_bo_pool_finish(&device->batch_bo_pool);
913 anv_state_pool_finish(&device->dynamic_state_pool);
914 anv_block_pool_finish(&device->dynamic_state_block_pool);
915 anv_block_pool_finish(&device->instruction_block_pool);
916 anv_state_pool_finish(&device->surface_state_pool);
917 anv_block_pool_finish(&device->surface_state_block_pool);
918 anv_block_pool_finish(&device->scratch_block_pool);
919
920 close(device->fd);
921
922 pthread_mutex_destroy(&device->mutex);
923
924 anv_free(&device->alloc, device);
925 }
926
927 VkResult anv_EnumerateInstanceExtensionProperties(
928 const char* pLayerName,
929 uint32_t* pPropertyCount,
930 VkExtensionProperties* pProperties)
931 {
932 if (pProperties == NULL) {
933 *pPropertyCount = ARRAY_SIZE(global_extensions);
934 return VK_SUCCESS;
935 }
936
937 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
938
939 *pPropertyCount = ARRAY_SIZE(global_extensions);
940 memcpy(pProperties, global_extensions, sizeof(global_extensions));
941
942 return VK_SUCCESS;
943 }
944
945 VkResult anv_EnumerateDeviceExtensionProperties(
946 VkPhysicalDevice physicalDevice,
947 const char* pLayerName,
948 uint32_t* pPropertyCount,
949 VkExtensionProperties* pProperties)
950 {
951 if (pProperties == NULL) {
952 *pPropertyCount = ARRAY_SIZE(device_extensions);
953 return VK_SUCCESS;
954 }
955
956 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
957
958 *pPropertyCount = ARRAY_SIZE(device_extensions);
959 memcpy(pProperties, device_extensions, sizeof(device_extensions));
960
961 return VK_SUCCESS;
962 }
963
964 VkResult anv_EnumerateInstanceLayerProperties(
965 uint32_t* pPropertyCount,
966 VkLayerProperties* pProperties)
967 {
968 if (pProperties == NULL) {
969 *pPropertyCount = 0;
970 return VK_SUCCESS;
971 }
972
973 /* None supported at this time */
974 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
975 }
976
977 VkResult anv_EnumerateDeviceLayerProperties(
978 VkPhysicalDevice physicalDevice,
979 uint32_t* pPropertyCount,
980 VkLayerProperties* pProperties)
981 {
982 if (pProperties == NULL) {
983 *pPropertyCount = 0;
984 return VK_SUCCESS;
985 }
986
987 /* None supported at this time */
988 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
989 }
990
991 void anv_GetDeviceQueue(
992 VkDevice _device,
993 uint32_t queueNodeIndex,
994 uint32_t queueIndex,
995 VkQueue* pQueue)
996 {
997 ANV_FROM_HANDLE(anv_device, device, _device);
998
999 assert(queueIndex == 0);
1000
1001 *pQueue = anv_queue_to_handle(&device->queue);
1002 }
1003
1004 VkResult anv_QueueSubmit(
1005 VkQueue _queue,
1006 uint32_t submitCount,
1007 const VkSubmitInfo* pSubmits,
1008 VkFence _fence)
1009 {
1010 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1011 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1012 struct anv_device *device = queue->device;
1013 int ret;
1014
1015 for (uint32_t i = 0; i < submitCount; i++) {
1016 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1017 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
1018 pSubmits[i].pCommandBuffers[j]);
1019 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1020
1021 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
1022 if (ret != 0) {
1023 /* We don't know the real error. */
1024 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1025 "execbuf2 failed: %m");
1026 }
1027
1028 for (uint32_t k = 0; k < cmd_buffer->execbuf2.bo_count; k++)
1029 cmd_buffer->execbuf2.bos[k]->offset = cmd_buffer->execbuf2.objects[k].offset;
1030 }
1031 }
1032
1033 if (fence) {
1034 ret = anv_gem_execbuffer(device, &fence->execbuf);
1035 if (ret != 0) {
1036 /* We don't know the real error. */
1037 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1038 "execbuf2 failed: %m");
1039 }
1040 }
1041
1042 return VK_SUCCESS;
1043 }
1044
1045 VkResult anv_QueueWaitIdle(
1046 VkQueue _queue)
1047 {
1048 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1049
1050 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
1051 }
1052
1053 VkResult anv_DeviceWaitIdle(
1054 VkDevice _device)
1055 {
1056 ANV_FROM_HANDLE(anv_device, device, _device);
1057 struct anv_batch batch;
1058
1059 uint32_t cmds[8];
1060 batch.start = batch.next = cmds;
1061 batch.end = (void *) cmds + sizeof(cmds);
1062
1063 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1064 anv_batch_emit(&batch, GEN7_MI_NOOP);
1065
1066 return anv_device_submit_simple_batch(device, &batch);
1067 }
1068
1069 VkResult
1070 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
1071 {
1072 bo->gem_handle = anv_gem_create(device, size);
1073 if (!bo->gem_handle)
1074 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1075
1076 bo->map = NULL;
1077 bo->index = 0;
1078 bo->offset = 0;
1079 bo->size = size;
1080 bo->is_winsys_bo = false;
1081
1082 return VK_SUCCESS;
1083 }
1084
1085 VkResult anv_AllocateMemory(
1086 VkDevice _device,
1087 const VkMemoryAllocateInfo* pAllocateInfo,
1088 const VkAllocationCallbacks* pAllocator,
1089 VkDeviceMemory* pMem)
1090 {
1091 ANV_FROM_HANDLE(anv_device, device, _device);
1092 struct anv_device_memory *mem;
1093 VkResult result;
1094
1095 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1096
1097 if (pAllocateInfo->allocationSize == 0) {
1098 /* Apparently, this is allowed */
1099 *pMem = VK_NULL_HANDLE;
1100 return VK_SUCCESS;
1101 }
1102
1103 /* We support exactly one memory heap. */
1104 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1105 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
1106
1107 /* FINISHME: Fail if allocation request exceeds heap size. */
1108
1109 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1110 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1111 if (mem == NULL)
1112 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1113
1114 /* The kernel is going to give us whole pages anyway */
1115 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1116
1117 result = anv_bo_init_new(&mem->bo, device, alloc_size);
1118 if (result != VK_SUCCESS)
1119 goto fail;
1120
1121 mem->type_index = pAllocateInfo->memoryTypeIndex;
1122
1123 *pMem = anv_device_memory_to_handle(mem);
1124
1125 return VK_SUCCESS;
1126
1127 fail:
1128 anv_free2(&device->alloc, pAllocator, mem);
1129
1130 return result;
1131 }
1132
1133 void anv_FreeMemory(
1134 VkDevice _device,
1135 VkDeviceMemory _mem,
1136 const VkAllocationCallbacks* pAllocator)
1137 {
1138 ANV_FROM_HANDLE(anv_device, device, _device);
1139 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1140
1141 if (mem == NULL)
1142 return;
1143
1144 if (mem->bo.map)
1145 anv_gem_munmap(mem->bo.map, mem->bo.size);
1146
1147 if (mem->bo.gem_handle != 0)
1148 anv_gem_close(device, mem->bo.gem_handle);
1149
1150 anv_free2(&device->alloc, pAllocator, mem);
1151 }
1152
1153 VkResult anv_MapMemory(
1154 VkDevice _device,
1155 VkDeviceMemory _memory,
1156 VkDeviceSize offset,
1157 VkDeviceSize size,
1158 VkMemoryMapFlags flags,
1159 void** ppData)
1160 {
1161 ANV_FROM_HANDLE(anv_device, device, _device);
1162 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1163
1164 if (mem == NULL) {
1165 *ppData = NULL;
1166 return VK_SUCCESS;
1167 }
1168
1169 if (size == VK_WHOLE_SIZE)
1170 size = mem->bo.size - offset;
1171
1172 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1173 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1174 * at a time is valid. We could just mmap up front and return an offset
1175 * pointer here, but that may exhaust virtual memory on 32 bit
1176 * userspace. */
1177
1178 uint32_t gem_flags = 0;
1179 if (!device->info.has_llc && mem->type_index == 0)
1180 gem_flags |= I915_MMAP_WC;
1181
1182 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1183 uint64_t map_offset = offset & ~4095ull;
1184 assert(offset >= map_offset);
1185 uint64_t map_size = (offset + size) - map_offset;
1186
1187 /* Let's map whole pages */
1188 map_size = align_u64(map_size, 4096);
1189
1190 mem->map = anv_gem_mmap(device, mem->bo.gem_handle,
1191 map_offset, map_size, gem_flags);
1192 mem->map_size = map_size;
1193
1194 *ppData = mem->map + (offset - map_offset);
1195
1196 return VK_SUCCESS;
1197 }
1198
1199 void anv_UnmapMemory(
1200 VkDevice _device,
1201 VkDeviceMemory _memory)
1202 {
1203 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1204
1205 if (mem == NULL)
1206 return;
1207
1208 anv_gem_munmap(mem->map, mem->map_size);
1209 }
1210
1211 static void
1212 clflush_mapped_ranges(struct anv_device *device,
1213 uint32_t count,
1214 const VkMappedMemoryRange *ranges)
1215 {
1216 for (uint32_t i = 0; i < count; i++) {
1217 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1218 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
1219 void *end;
1220
1221 if (ranges[i].offset + ranges[i].size > mem->map_size)
1222 end = mem->map + mem->map_size;
1223 else
1224 end = mem->map + ranges[i].offset + ranges[i].size;
1225
1226 while (p < end) {
1227 __builtin_ia32_clflush(p);
1228 p += CACHELINE_SIZE;
1229 }
1230 }
1231 }
1232
1233 VkResult anv_FlushMappedMemoryRanges(
1234 VkDevice _device,
1235 uint32_t memoryRangeCount,
1236 const VkMappedMemoryRange* pMemoryRanges)
1237 {
1238 ANV_FROM_HANDLE(anv_device, device, _device);
1239
1240 if (device->info.has_llc)
1241 return VK_SUCCESS;
1242
1243 /* Make sure the writes we're flushing have landed. */
1244 __builtin_ia32_mfence();
1245
1246 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1247
1248 return VK_SUCCESS;
1249 }
1250
1251 VkResult anv_InvalidateMappedMemoryRanges(
1252 VkDevice _device,
1253 uint32_t memoryRangeCount,
1254 const VkMappedMemoryRange* pMemoryRanges)
1255 {
1256 ANV_FROM_HANDLE(anv_device, device, _device);
1257
1258 if (device->info.has_llc)
1259 return VK_SUCCESS;
1260
1261 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1262
1263 /* Make sure no reads get moved up above the invalidate. */
1264 __builtin_ia32_mfence();
1265
1266 return VK_SUCCESS;
1267 }
1268
1269 void anv_GetBufferMemoryRequirements(
1270 VkDevice device,
1271 VkBuffer _buffer,
1272 VkMemoryRequirements* pMemoryRequirements)
1273 {
1274 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1275
1276 /* The Vulkan spec (git aaed022) says:
1277 *
1278 * memoryTypeBits is a bitfield and contains one bit set for every
1279 * supported memory type for the resource. The bit `1<<i` is set if and
1280 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1281 * structure for the physical device is supported.
1282 *
1283 * We support exactly one memory type.
1284 */
1285 pMemoryRequirements->memoryTypeBits = 1;
1286
1287 pMemoryRequirements->size = buffer->size;
1288 pMemoryRequirements->alignment = 16;
1289 }
1290
1291 void anv_GetImageMemoryRequirements(
1292 VkDevice device,
1293 VkImage _image,
1294 VkMemoryRequirements* pMemoryRequirements)
1295 {
1296 ANV_FROM_HANDLE(anv_image, image, _image);
1297
1298 /* The Vulkan spec (git aaed022) says:
1299 *
1300 * memoryTypeBits is a bitfield and contains one bit set for every
1301 * supported memory type for the resource. The bit `1<<i` is set if and
1302 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1303 * structure for the physical device is supported.
1304 *
1305 * We support exactly one memory type.
1306 */
1307 pMemoryRequirements->memoryTypeBits = 1;
1308
1309 pMemoryRequirements->size = image->size;
1310 pMemoryRequirements->alignment = image->alignment;
1311 }
1312
1313 void anv_GetImageSparseMemoryRequirements(
1314 VkDevice device,
1315 VkImage image,
1316 uint32_t* pSparseMemoryRequirementCount,
1317 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1318 {
1319 stub();
1320 }
1321
1322 void anv_GetDeviceMemoryCommitment(
1323 VkDevice device,
1324 VkDeviceMemory memory,
1325 VkDeviceSize* pCommittedMemoryInBytes)
1326 {
1327 *pCommittedMemoryInBytes = 0;
1328 }
1329
1330 VkResult anv_BindBufferMemory(
1331 VkDevice device,
1332 VkBuffer _buffer,
1333 VkDeviceMemory _memory,
1334 VkDeviceSize memoryOffset)
1335 {
1336 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1337 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1338
1339 if (mem) {
1340 buffer->bo = &mem->bo;
1341 buffer->offset = memoryOffset;
1342 } else {
1343 buffer->bo = NULL;
1344 buffer->offset = 0;
1345 }
1346
1347 return VK_SUCCESS;
1348 }
1349
1350 VkResult anv_BindImageMemory(
1351 VkDevice device,
1352 VkImage _image,
1353 VkDeviceMemory _memory,
1354 VkDeviceSize memoryOffset)
1355 {
1356 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1357 ANV_FROM_HANDLE(anv_image, image, _image);
1358
1359 if (mem) {
1360 image->bo = &mem->bo;
1361 image->offset = memoryOffset;
1362 } else {
1363 image->bo = NULL;
1364 image->offset = 0;
1365 }
1366
1367 return VK_SUCCESS;
1368 }
1369
1370 VkResult anv_QueueBindSparse(
1371 VkQueue queue,
1372 uint32_t bindInfoCount,
1373 const VkBindSparseInfo* pBindInfo,
1374 VkFence fence)
1375 {
1376 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1377 }
1378
1379 VkResult anv_CreateFence(
1380 VkDevice _device,
1381 const VkFenceCreateInfo* pCreateInfo,
1382 const VkAllocationCallbacks* pAllocator,
1383 VkFence* pFence)
1384 {
1385 ANV_FROM_HANDLE(anv_device, device, _device);
1386 struct anv_bo fence_bo;
1387 struct anv_fence *fence;
1388 struct anv_batch batch;
1389 VkResult result;
1390
1391 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1392
1393 result = anv_bo_pool_alloc(&device->batch_bo_pool, &fence_bo);
1394 if (result != VK_SUCCESS)
1395 return result;
1396
1397 /* Fences are small. Just store the CPU data structure in the BO. */
1398 fence = fence_bo.map;
1399 fence->bo = fence_bo;
1400
1401 /* Place the batch after the CPU data but on its own cache line. */
1402 const uint32_t batch_offset = align_u32(sizeof(*fence), CACHELINE_SIZE);
1403 batch.next = batch.start = fence->bo.map + batch_offset;
1404 batch.end = fence->bo.map + fence->bo.size;
1405 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1406 anv_batch_emit(&batch, GEN7_MI_NOOP);
1407
1408 if (!device->info.has_llc) {
1409 assert(((uintptr_t) batch.start & CACHELINE_MASK) == 0);
1410 assert(batch.next - batch.start <= CACHELINE_SIZE);
1411 __builtin_ia32_mfence();
1412 __builtin_ia32_clflush(batch.start);
1413 }
1414
1415 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1416 fence->exec2_objects[0].relocation_count = 0;
1417 fence->exec2_objects[0].relocs_ptr = 0;
1418 fence->exec2_objects[0].alignment = 0;
1419 fence->exec2_objects[0].offset = fence->bo.offset;
1420 fence->exec2_objects[0].flags = 0;
1421 fence->exec2_objects[0].rsvd1 = 0;
1422 fence->exec2_objects[0].rsvd2 = 0;
1423
1424 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1425 fence->execbuf.buffer_count = 1;
1426 fence->execbuf.batch_start_offset = batch.start - fence->bo.map;
1427 fence->execbuf.batch_len = batch.next - batch.start;
1428 fence->execbuf.cliprects_ptr = 0;
1429 fence->execbuf.num_cliprects = 0;
1430 fence->execbuf.DR1 = 0;
1431 fence->execbuf.DR4 = 0;
1432
1433 fence->execbuf.flags =
1434 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1435 fence->execbuf.rsvd1 = device->context_id;
1436 fence->execbuf.rsvd2 = 0;
1437
1438 fence->ready = false;
1439
1440 *pFence = anv_fence_to_handle(fence);
1441
1442 return VK_SUCCESS;
1443 }
1444
1445 void anv_DestroyFence(
1446 VkDevice _device,
1447 VkFence _fence,
1448 const VkAllocationCallbacks* pAllocator)
1449 {
1450 ANV_FROM_HANDLE(anv_device, device, _device);
1451 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1452
1453 assert(fence->bo.map == fence);
1454 anv_bo_pool_free(&device->batch_bo_pool, &fence->bo);
1455 }
1456
1457 VkResult anv_ResetFences(
1458 VkDevice _device,
1459 uint32_t fenceCount,
1460 const VkFence* pFences)
1461 {
1462 for (uint32_t i = 0; i < fenceCount; i++) {
1463 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1464 fence->ready = false;
1465 }
1466
1467 return VK_SUCCESS;
1468 }
1469
1470 VkResult anv_GetFenceStatus(
1471 VkDevice _device,
1472 VkFence _fence)
1473 {
1474 ANV_FROM_HANDLE(anv_device, device, _device);
1475 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1476 int64_t t = 0;
1477 int ret;
1478
1479 if (fence->ready)
1480 return VK_SUCCESS;
1481
1482 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1483 if (ret == 0) {
1484 fence->ready = true;
1485 return VK_SUCCESS;
1486 }
1487
1488 return VK_NOT_READY;
1489 }
1490
1491 VkResult anv_WaitForFences(
1492 VkDevice _device,
1493 uint32_t fenceCount,
1494 const VkFence* pFences,
1495 VkBool32 waitAll,
1496 uint64_t timeout)
1497 {
1498 ANV_FROM_HANDLE(anv_device, device, _device);
1499
1500 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1501 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1502 * for a couple of kernel releases. Since there's no way to know
1503 * whether or not the kernel we're using is one of the broken ones, the
1504 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1505 * maximum timeout from 584 years to 292 years - likely not a big deal.
1506 */
1507 if (timeout > INT64_MAX)
1508 timeout = INT64_MAX;
1509
1510 int64_t t = timeout;
1511
1512 /* FIXME: handle !waitAll */
1513
1514 for (uint32_t i = 0; i < fenceCount; i++) {
1515 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1516 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1517 if (ret == -1 && errno == ETIME) {
1518 return VK_TIMEOUT;
1519 } else if (ret == -1) {
1520 /* We don't know the real error. */
1521 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1522 "gem wait failed: %m");
1523 }
1524 }
1525
1526 return VK_SUCCESS;
1527 }
1528
1529 // Queue semaphore functions
1530
1531 VkResult anv_CreateSemaphore(
1532 VkDevice device,
1533 const VkSemaphoreCreateInfo* pCreateInfo,
1534 const VkAllocationCallbacks* pAllocator,
1535 VkSemaphore* pSemaphore)
1536 {
1537 /* The DRM execbuffer ioctl always execute in-oder, even between different
1538 * rings. As such, there's nothing to do for the user space semaphore.
1539 */
1540
1541 *pSemaphore = (VkSemaphore)1;
1542
1543 return VK_SUCCESS;
1544 }
1545
1546 void anv_DestroySemaphore(
1547 VkDevice device,
1548 VkSemaphore semaphore,
1549 const VkAllocationCallbacks* pAllocator)
1550 {
1551 }
1552
1553 // Event functions
1554
1555 VkResult anv_CreateEvent(
1556 VkDevice _device,
1557 const VkEventCreateInfo* pCreateInfo,
1558 const VkAllocationCallbacks* pAllocator,
1559 VkEvent* pEvent)
1560 {
1561 ANV_FROM_HANDLE(anv_device, device, _device);
1562 struct anv_state state;
1563 struct anv_event *event;
1564
1565 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
1566
1567 state = anv_state_pool_alloc(&device->dynamic_state_pool,
1568 sizeof(*event), 8);
1569 event = state.map;
1570 event->state = state;
1571 event->semaphore = VK_EVENT_RESET;
1572
1573 if (!device->info.has_llc) {
1574 /* Make sure the writes we're flushing have landed. */
1575 __builtin_ia32_mfence();
1576 __builtin_ia32_clflush(event);
1577 }
1578
1579 *pEvent = anv_event_to_handle(event);
1580
1581 return VK_SUCCESS;
1582 }
1583
1584 void anv_DestroyEvent(
1585 VkDevice _device,
1586 VkEvent _event,
1587 const VkAllocationCallbacks* pAllocator)
1588 {
1589 ANV_FROM_HANDLE(anv_device, device, _device);
1590 ANV_FROM_HANDLE(anv_event, event, _event);
1591
1592 anv_state_pool_free(&device->dynamic_state_pool, event->state);
1593 }
1594
1595 VkResult anv_GetEventStatus(
1596 VkDevice _device,
1597 VkEvent _event)
1598 {
1599 ANV_FROM_HANDLE(anv_device, device, _device);
1600 ANV_FROM_HANDLE(anv_event, event, _event);
1601
1602 if (!device->info.has_llc) {
1603 /* Invalidate read cache before reading event written by GPU. */
1604 __builtin_ia32_clflush(event);
1605 __builtin_ia32_mfence();
1606
1607 }
1608
1609 return event->semaphore;
1610 }
1611
1612 VkResult anv_SetEvent(
1613 VkDevice _device,
1614 VkEvent _event)
1615 {
1616 ANV_FROM_HANDLE(anv_device, device, _device);
1617 ANV_FROM_HANDLE(anv_event, event, _event);
1618
1619 event->semaphore = VK_EVENT_SET;
1620
1621 if (!device->info.has_llc) {
1622 /* Make sure the writes we're flushing have landed. */
1623 __builtin_ia32_mfence();
1624 __builtin_ia32_clflush(event);
1625 }
1626
1627 return VK_SUCCESS;
1628 }
1629
1630 VkResult anv_ResetEvent(
1631 VkDevice _device,
1632 VkEvent _event)
1633 {
1634 ANV_FROM_HANDLE(anv_device, device, _device);
1635 ANV_FROM_HANDLE(anv_event, event, _event);
1636
1637 event->semaphore = VK_EVENT_RESET;
1638
1639 if (!device->info.has_llc) {
1640 /* Make sure the writes we're flushing have landed. */
1641 __builtin_ia32_mfence();
1642 __builtin_ia32_clflush(event);
1643 }
1644
1645 return VK_SUCCESS;
1646 }
1647
1648 // Buffer functions
1649
1650 VkResult anv_CreateBuffer(
1651 VkDevice _device,
1652 const VkBufferCreateInfo* pCreateInfo,
1653 const VkAllocationCallbacks* pAllocator,
1654 VkBuffer* pBuffer)
1655 {
1656 ANV_FROM_HANDLE(anv_device, device, _device);
1657 struct anv_buffer *buffer;
1658
1659 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1660
1661 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1662 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1663 if (buffer == NULL)
1664 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1665
1666 buffer->size = pCreateInfo->size;
1667 buffer->usage = pCreateInfo->usage;
1668 buffer->bo = NULL;
1669 buffer->offset = 0;
1670
1671 *pBuffer = anv_buffer_to_handle(buffer);
1672
1673 return VK_SUCCESS;
1674 }
1675
1676 void anv_DestroyBuffer(
1677 VkDevice _device,
1678 VkBuffer _buffer,
1679 const VkAllocationCallbacks* pAllocator)
1680 {
1681 ANV_FROM_HANDLE(anv_device, device, _device);
1682 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1683
1684 anv_free2(&device->alloc, pAllocator, buffer);
1685 }
1686
1687 void
1688 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
1689 enum isl_format format,
1690 uint32_t offset, uint32_t range, uint32_t stride)
1691 {
1692 isl_buffer_fill_state(&device->isl_dev, state.map,
1693 .address = offset,
1694 .mocs = device->default_mocs,
1695 .size = range,
1696 .format = format,
1697 .stride = stride);
1698
1699 if (!device->info.has_llc)
1700 anv_state_clflush(state);
1701 }
1702
1703 void anv_DestroySampler(
1704 VkDevice _device,
1705 VkSampler _sampler,
1706 const VkAllocationCallbacks* pAllocator)
1707 {
1708 ANV_FROM_HANDLE(anv_device, device, _device);
1709 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1710
1711 anv_free2(&device->alloc, pAllocator, sampler);
1712 }
1713
1714 VkResult anv_CreateFramebuffer(
1715 VkDevice _device,
1716 const VkFramebufferCreateInfo* pCreateInfo,
1717 const VkAllocationCallbacks* pAllocator,
1718 VkFramebuffer* pFramebuffer)
1719 {
1720 ANV_FROM_HANDLE(anv_device, device, _device);
1721 struct anv_framebuffer *framebuffer;
1722
1723 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1724
1725 size_t size = sizeof(*framebuffer) +
1726 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1727 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1728 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1729 if (framebuffer == NULL)
1730 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1731
1732 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1733 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1734 VkImageView _iview = pCreateInfo->pAttachments[i];
1735 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1736 }
1737
1738 framebuffer->width = pCreateInfo->width;
1739 framebuffer->height = pCreateInfo->height;
1740 framebuffer->layers = pCreateInfo->layers;
1741
1742 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1743
1744 return VK_SUCCESS;
1745 }
1746
1747 void anv_DestroyFramebuffer(
1748 VkDevice _device,
1749 VkFramebuffer _fb,
1750 const VkAllocationCallbacks* pAllocator)
1751 {
1752 ANV_FROM_HANDLE(anv_device, device, _device);
1753 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1754
1755 anv_free2(&device->alloc, pAllocator, fb);
1756 }
1757
1758 void vkCmdDbgMarkerBegin(
1759 VkCommandBuffer commandBuffer,
1760 const char* pMarker)
1761 __attribute__ ((visibility ("default")));
1762
1763 void vkCmdDbgMarkerEnd(
1764 VkCommandBuffer commandBuffer)
1765 __attribute__ ((visibility ("default")));
1766
1767 void vkCmdDbgMarkerBegin(
1768 VkCommandBuffer commandBuffer,
1769 const char* pMarker)
1770 {
1771 }
1772
1773 void vkCmdDbgMarkerEnd(
1774 VkCommandBuffer commandBuffer)
1775 {
1776 }