OSDN Git Service

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