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