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