blob: 74b813e9e4022d98bc476c2d4aac31e992bb7a40 [file] [log] [blame]
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001/*
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
Chad Versace2c2233e2015-07-17 15:04:27 -070030#include "anv_private.h"
Jason Ekstrand977a4692015-07-09 15:53:03 -070031#include "mesa/main/git_sha1.h"
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -070032#include "util/strtod.h"
Kristian Høgsberg769785c2015-05-08 22:32:37 -070033
Jason Ekstrandde54b4b2015-11-16 12:29:07 -080034#include "gen7_pack.h"
35
Jason Ekstranda95f51c2015-09-24 14:20:35 -070036struct anv_dispatch_table dtable;
37
Jason Ekstranda71e6142015-10-19 22:06:59 -070038static void
39compiler_debug_log(void *data, const char *fmt, ...)
40{ }
41
42static void
43compiler_perf_log(void *data, const char *fmt, ...)
44{
45 va_list args;
46 va_start(args, fmt);
47
48 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
49 vfprintf(stderr, fmt, args);
50
51 va_end(args);
52}
53
Kristian Høgsberg769785c2015-05-08 22:32:37 -070054static VkResult
Chad Versace4422bd42015-07-09 16:22:18 -070055anv_physical_device_init(struct anv_physical_device *device,
56 struct anv_instance *instance,
57 const char *path)
Kristian Høgsberg769785c2015-05-08 22:32:37 -070058{
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070059 VkResult result;
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -070060 int fd;
61
62 fd = open(path, O_RDWR | O_CLOEXEC);
63 if (fd < 0)
Chad Versacef9c948e2015-10-07 11:36:51 -070064 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
65 "failed to open %s: %m", path);
Kristian Høgsberg769785c2015-05-08 22:32:37 -070066
Jason Ekstrand39cd3782015-09-24 13:51:40 -070067 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Kristian Høgsberg769785c2015-05-08 22:32:37 -070068 device->instance = instance;
69 device->path = path;
Chad Versacef9c948e2015-10-07 11:36:51 -070070
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -070071 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070072 if (!device->chipset_id) {
Chad Versacef9c948e2015-10-07 11:36:51 -070073 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
74 "failed to get chipset id: %m");
Kristian Høgsberg769785c2015-05-08 22:32:37 -070075 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070076 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -070077
78 device->name = brw_get_device_name(device->chipset_id);
Jason Ekstrandb00e3f22015-11-03 15:45:04 -080079 device->info = brw_get_device_info(device->chipset_id);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070080 if (!device->info) {
Chad Versacef9c948e2015-10-07 11:36:51 -070081 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
82 "failed to get device info");
Kristian Høgsberg769785c2015-05-08 22:32:37 -070083 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070084 }
Jason Ekstrand584f9d42015-11-02 12:14:37 -080085
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080086 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) {
Jason Ekstrand862da6a2015-11-09 12:18:12 -080089 fprintf(stderr, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
Kristian Høgsbergdac57752015-12-01 15:39:30 -080090 } 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 == 9 && !device->info->is_broxton) {
Kristian Høgsberg Kristensencd4721c2015-11-25 22:27:01 -080093 fprintf(stderr, "WARNING: Skylake Vulkan support is incomplete\n");
Kristian Høgsbergdac57752015-12-01 15:39:30 -080094 } else if (device->info->gen == 9 && device->info->is_broxton) {
95 fprintf(stderr, "WARNING: Broxton Vulkan support is incomplete\n");
96 } else if (device->info->gen == 8) {
97 /* Broadwell/Cherryview is as fully supported as anything */
Jason Ekstrand584f9d42015-11-02 12:14:37 -080098 } else {
Jason Ekstrandfed35862015-12-02 16:14:58 -080099 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800100 "Vulkan not yet supported on %s", device->name);
101 goto fail;
102 }
103
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700104 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700105 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
106 "failed to get aperture size: %m");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700107 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700108 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700109
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700110 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700111 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
112 "kernel missing gem wait");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700113 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700114 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700115
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700116 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700117 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
118 "kernel missing execbuf2");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700119 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700120 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700121
Kristian Høgsberg Kristensen220ac932015-12-19 22:25:57 -0800122 if (!device->info->has_llc &&
123 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -0800124 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
125 "kernel missing wc mmap");
126 goto fail;
127 }
128
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700129 close(fd);
130
Jason Ekstranda71e6142015-10-19 22:06:59 -0700131 brw_process_intel_debug_variable();
132
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700133 device->compiler = brw_compiler_create(NULL, device->info);
134 if (device->compiler == NULL) {
135 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
136 goto fail;
137 }
Jason Ekstranda71e6142015-10-19 22:06:59 -0700138 device->compiler->shader_debug_log = compiler_debug_log;
139 device->compiler->shader_perf_log = compiler_perf_log;
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700140
Chad Versace738eaa82015-11-13 11:12:46 -0800141 isl_device_init(&device->isl_dev, device->info);
Chad Versaceaf392912015-11-13 10:12:51 -0800142
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700143 return VK_SUCCESS;
Chad Versace477383e2015-11-13 10:12:18 -0800144
Chad Versace8cda3e92015-07-09 16:31:39 -0700145fail:
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700146 close(fd);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700147 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700148}
149
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700150static void
151anv_physical_device_finish(struct anv_physical_device *device)
152{
153 ralloc_free(device->compiler);
154}
155
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700156static const VkExtensionProperties global_extensions[] = {
157 {
Jason Ekstrandd6664872015-12-02 16:28:36 -0800158 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
159 .specVersion = 24,
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700160 },
Jason Ekstrandd6664872015-12-02 16:28:36 -0800161 {
162 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
163 .specVersion = 5,
164 },
165#ifdef HAVE_WAYLAND_PLATFORM
166 {
167 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
168 .specVersion = 4,
169 },
170#endif
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700171};
172
173static const VkExtensionProperties device_extensions[] = {
174 {
Jason Ekstrandd6664872015-12-02 16:28:36 -0800175 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
176 .specVersion = 67,
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700177 },
178};
179
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800180static void *
181default_alloc_func(void *pUserData, size_t size, size_t align,
182 VkSystemAllocationScope allocationScope)
183{
184 return malloc(size);
185}
186
187static void *
188default_realloc_func(void *pUserData, void *pOriginal, size_t size,
189 size_t align, VkSystemAllocationScope allocationScope)
190{
191 return realloc(pOriginal, size);
192}
193
194static void
195default_free_func(void *pUserData, void *pMemory)
196{
197 free(pMemory);
198}
199
200static const VkAllocationCallbacks default_alloc = {
201 .pUserData = NULL,
202 .pfnAllocation = default_alloc_func,
203 .pfnReallocation = default_realloc_func,
204 .pfnFree = default_free_func,
205};
206
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700207VkResult anv_CreateInstance(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700208 const VkInstanceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800209 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700210 VkInstance* pInstance)
211{
212 struct anv_instance *instance;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700213
214 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
215
Jason Ekstrandd6664872015-12-02 16:28:36 -0800216 if (pCreateInfo->pApplicationInfo->apiVersion != VK_MAKE_VERSION(0, 210, 1))
Jason Ekstrande21ecb82015-10-12 18:25:19 -0700217 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
218
Jason Ekstrand93496252015-12-01 13:58:25 -0800219 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700220 bool found = false;
221 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
222 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800223 global_extensions[j].extensionName) == 0) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700224 found = true;
225 break;
226 }
227 }
228 if (!found)
Chad Versacef9c948e2015-10-07 11:36:51 -0700229 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700230 }
231
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800232 instance = anv_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
233 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700234 if (!instance)
235 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
236
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700237 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800238
239 if (pAllocator)
240 instance->alloc = *pAllocator;
241 else
242 instance->alloc = default_alloc;
243
Jason Ekstrand93496252015-12-01 13:58:25 -0800244 instance->apiVersion = pCreateInfo->pApplicationInfo->apiVersion;
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800245 instance->physicalDeviceCount = -1;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700246
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -0700247 _mesa_locale_init();
248
Jason Ekstrand930598a2015-07-31 10:18:00 -0700249 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
250
Jason Ekstrand348cb292015-09-04 11:14:45 -0700251 anv_init_wsi(instance);
252
Jason Ekstrand098209e2015-07-09 18:41:27 -0700253 *pInstance = anv_instance_to_handle(instance);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700254
255 return VK_SUCCESS;
256}
257
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700258void anv_DestroyInstance(
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800259 VkInstance _instance,
260 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700261{
Jason Ekstrand73f91872015-07-09 18:41:27 -0700262 ANV_FROM_HANDLE(anv_instance, instance, _instance);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700263
Chad Versace0ab926d2015-10-21 11:36:39 -0700264 if (instance->physicalDeviceCount > 0) {
265 /* We support at most one physical device. */
266 assert(instance->physicalDeviceCount == 1);
267 anv_physical_device_finish(&instance->physicalDevice);
268 }
269
Jason Ekstrand348cb292015-09-04 11:14:45 -0700270 anv_finish_wsi(instance);
271
Jason Ekstrand930598a2015-07-31 10:18:00 -0700272 VG(VALGRIND_DESTROY_MEMPOOL(instance));
273
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -0700274 _mesa_locale_fini();
275
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800276 anv_free(&instance->alloc, instance);
Jason Ekstrande40bdce2015-07-31 10:13:24 -0700277}
278
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700279VkResult anv_EnumeratePhysicalDevices(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700280 VkInstance _instance,
281 uint32_t* pPhysicalDeviceCount,
282 VkPhysicalDevice* pPhysicalDevices)
283{
Jason Ekstrand73f91872015-07-09 18:41:27 -0700284 ANV_FROM_HANDLE(anv_instance, instance, _instance);
Chad Versacefa915b62015-07-09 15:38:58 -0700285 VkResult result;
286
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800287 if (instance->physicalDeviceCount < 0) {
Chad Versace4422bd42015-07-09 16:22:18 -0700288 result = anv_physical_device_init(&instance->physicalDevice,
289 instance, "/dev/dri/renderD128");
Jason Ekstrandfed35862015-12-02 16:14:58 -0800290 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800291 instance->physicalDeviceCount = 0;
292 } else if (result == VK_SUCCESS) {
293 instance->physicalDeviceCount = 1;
294 } else {
Chad Versacefa915b62015-07-09 15:38:58 -0700295 return result;
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800296 }
Chad Versacefa915b62015-07-09 15:38:58 -0700297 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700298
Chad Versace5b75dff2015-07-09 15:51:06 -0700299 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
300 * otherwise it's an inout parameter.
301 *
302 * The Vulkan spec (git aaed022) says:
303 *
304 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
305 * that is initialized with the number of devices the application is
306 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
307 * an array of at least this many VkPhysicalDevice handles [...].
308 *
309 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
310 * overwrites the contents of the variable pointed to by
311 * pPhysicalDeviceCount with the number of physical devices in in the
312 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
313 * pPhysicalDeviceCount with the number of physical handles written to
314 * pPhysicalDevices.
315 */
316 if (!pPhysicalDevices) {
317 *pPhysicalDeviceCount = instance->physicalDeviceCount;
318 } else if (*pPhysicalDeviceCount >= 1) {
Jason Ekstrand098209e2015-07-09 18:41:27 -0700319 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
Chad Versace5b75dff2015-07-09 15:51:06 -0700320 *pPhysicalDeviceCount = 1;
321 } else {
322 *pPhysicalDeviceCount = 0;
323 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700324
325 return VK_SUCCESS;
326}
327
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800328void anv_GetPhysicalDeviceFeatures(
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700329 VkPhysicalDevice physicalDevice,
330 VkPhysicalDeviceFeatures* pFeatures)
331{
332 anv_finishme("Get correct values for PhysicalDeviceFeatures");
333
334 *pFeatures = (VkPhysicalDeviceFeatures) {
335 .robustBufferAccess = false,
336 .fullDrawIndexUint32 = false,
337 .imageCubeArray = false,
338 .independentBlend = false,
339 .geometryShader = true,
340 .tessellationShader = false,
341 .sampleRateShading = false,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800342 .dualSrcBlend = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700343 .logicOp = true,
Chad Versace545f5cc2015-10-07 10:05:02 -0700344 .multiDrawIndirect = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800345 .depthClamp = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700346 .depthBiasClamp = false,
347 .fillModeNonSolid = true,
348 .depthBounds = false,
349 .wideLines = true,
350 .largePoints = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800351 .alphaToOne = true,
352 .multiViewport = true,
353 .samplerAnisotropy = false, /* FINISHME */
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700354 .textureCompressionETC2 = true,
355 .textureCompressionASTC_LDR = true,
356 .textureCompressionBC = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800357 .occlusionQueryPrecise = false, /* FINISHME */
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700358 .pipelineStatisticsQuery = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800359 .vertexPipelineStoresAndAtomics = false,
360 .fragmentStoresAndAtomics = true,
361 .shaderTessellationAndGeometryPointSize = true,
Chad Versace545f5cc2015-10-07 10:05:02 -0700362 .shaderImageGatherExtended = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700363 .shaderStorageImageExtendedFormats = false,
364 .shaderStorageImageMultisample = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700365 .shaderUniformBufferArrayDynamicIndexing = true,
366 .shaderSampledImageArrayDynamicIndexing = false,
367 .shaderStorageBufferArrayDynamicIndexing = false,
368 .shaderStorageImageArrayDynamicIndexing = false,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800369 .shaderStorageImageReadWithoutFormat = false,
370 .shaderStorageImageWriteWithoutFormat = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700371 .shaderClipDistance = false,
372 .shaderCullDistance = false,
373 .shaderFloat64 = false,
374 .shaderInt64 = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700375 .shaderInt16 = false,
Chad Versace545f5cc2015-10-07 10:05:02 -0700376 .alphaToOne = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800377 .variableMultisampleRate = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700378 };
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700379}
380
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800381void anv_GetPhysicalDeviceProperties(
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700382 VkPhysicalDevice physicalDevice,
Chad Versaced48e71c2015-10-07 10:36:46 -0700383 VkPhysicalDeviceProperties* pProperties)
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700384{
Chad Versaced48e71c2015-10-07 10:36:46 -0700385 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
386 const struct brw_device_info *devinfo = pdevice->info;
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700387
Chad Versaced48e71c2015-10-07 10:36:46 -0700388 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700389
Jason Ekstrandd6897452015-12-02 16:58:54 -0800390 VkSampleCountFlags sample_counts =
391 VK_SAMPLE_COUNT_1_BIT |
392 VK_SAMPLE_COUNT_2_BIT |
393 VK_SAMPLE_COUNT_4_BIT |
394 VK_SAMPLE_COUNT_8_BIT;
395
Chad Versaced48e71c2015-10-07 10:36:46 -0700396 VkPhysicalDeviceLimits limits = {
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700397 .maxImageDimension1D = (1 << 14),
398 .maxImageDimension2D = (1 << 14),
399 .maxImageDimension3D = (1 << 10),
400 .maxImageDimensionCube = (1 << 14),
401 .maxImageArrayLayers = (1 << 10),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800402 .maxTexelBufferElements = (1 << 14),
403 .maxUniformBufferRange = UINT32_MAX,
404 .maxStorageBufferRange = UINT32_MAX,
Jason Ekstrand5446bf32015-08-26 15:01:38 -0700405 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700406 .maxMemoryAllocationCount = UINT32_MAX,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800407 .maxSamplerAllocationCount = UINT32_MAX,
Jason Ekstrande5db2092015-07-14 17:10:37 -0700408 .bufferImageGranularity = 64, /* A cache line */
Chad Versace033a37f2015-10-07 09:57:51 -0700409 .sparseAddressSpaceSize = 0,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700410 .maxBoundDescriptorSets = MAX_SETS,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700411 .maxPerStageDescriptorSamplers = 64,
412 .maxPerStageDescriptorUniformBuffers = 64,
413 .maxPerStageDescriptorStorageBuffers = 64,
414 .maxPerStageDescriptorSampledImages = 64,
415 .maxPerStageDescriptorStorageImages = 64,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800416 .maxPerStageDescriptorInputAttachments = 64,
417 .maxPerStageResources = 128,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700418 .maxDescriptorSetSamplers = 256,
419 .maxDescriptorSetUniformBuffers = 256,
Chad Versace033a37f2015-10-07 09:57:51 -0700420 .maxDescriptorSetUniformBuffersDynamic = 256,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700421 .maxDescriptorSetStorageBuffers = 256,
Chad Versace033a37f2015-10-07 09:57:51 -0700422 .maxDescriptorSetStorageBuffersDynamic = 256,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700423 .maxDescriptorSetSampledImages = 256,
424 .maxDescriptorSetStorageImages = 256,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800425 .maxDescriptorSetInputAttachments = 256,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700426 .maxVertexInputAttributes = 32,
Chad Versace033a37f2015-10-07 09:57:51 -0700427 .maxVertexInputBindings = 32,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700428 .maxVertexInputAttributeOffset = 256,
429 .maxVertexInputBindingStride = 256,
430 .maxVertexOutputComponents = 32,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800431 .maxTessellationGenerationLevel = 0,
432 .maxTessellationPatchSize = 0,
433 .maxTessellationControlPerVertexInputComponents = 0,
434 .maxTessellationControlPerVertexOutputComponents = 0,
435 .maxTessellationControlPerPatchOutputComponents = 0,
436 .maxTessellationControlTotalOutputComponents = 0,
437 .maxTessellationEvaluationInputComponents = 0,
438 .maxTessellationEvaluationOutputComponents = 0,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700439 .maxGeometryShaderInvocations = 6,
440 .maxGeometryInputComponents = 16,
441 .maxGeometryOutputComponents = 16,
442 .maxGeometryOutputVertices = 16,
443 .maxGeometryTotalOutputComponents = 16,
444 .maxFragmentInputComponents = 16,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800445 .maxFragmentOutputAttachments = 8,
446 .maxFragmentDualSrcAttachments = 2,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700447 .maxFragmentCombinedOutputResources = 8,
448 .maxComputeSharedMemorySize = 1024,
449 .maxComputeWorkGroupCount = {
450 16 * devinfo->max_cs_threads,
451 16 * devinfo->max_cs_threads,
452 16 * devinfo->max_cs_threads,
453 },
454 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
455 .maxComputeWorkGroupSize = {
456 16 * devinfo->max_cs_threads,
457 16 * devinfo->max_cs_threads,
458 16 * devinfo->max_cs_threads,
459 },
460 .subPixelPrecisionBits = 4 /* FIXME */,
461 .subTexelPrecisionBits = 4 /* FIXME */,
462 .mipmapPrecisionBits = 4 /* FIXME */,
463 .maxDrawIndexedIndexValue = UINT32_MAX,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800464 .maxDrawIndirectCount = UINT32_MAX,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700465 .maxSamplerLodBias = 16,
466 .maxSamplerAnisotropy = 16,
Jason Ekstranddaf68a92015-10-06 17:21:44 -0700467 .maxViewports = MAX_VIEWPORTS,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700468 .maxViewportDimensions = { (1 << 14), (1 << 14) },
469 .viewportBoundsRange = { -1.0, 1.0 }, /* FIXME */
470 .viewportSubPixelBits = 13, /* We take a float? */
Jason Ekstrandf076d532016-01-01 09:26:06 -0800471 .minMemoryMapAlignment = 4096, /* A page */
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700472 .minTexelBufferOffsetAlignment = 1,
473 .minUniformBufferOffsetAlignment = 1,
474 .minStorageBufferOffsetAlignment = 1,
475 .minTexelOffset = 0, /* FIXME */
476 .maxTexelOffset = 0, /* FIXME */
477 .minTexelGatherOffset = 0, /* FIXME */
478 .maxTexelGatherOffset = 0, /* FIXME */
479 .minInterpolationOffset = 0, /* FIXME */
480 .maxInterpolationOffset = 0, /* FIXME */
481 .subPixelInterpolationOffsetBits = 0, /* FIXME */
482 .maxFramebufferWidth = (1 << 14),
483 .maxFramebufferHeight = (1 << 14),
484 .maxFramebufferLayers = (1 << 10),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800485 .framebufferColorSampleCounts = sample_counts,
486 .framebufferDepthSampleCounts = sample_counts,
487 .framebufferStencilSampleCounts = sample_counts,
488 .framebufferNoAttachmentsSampleCounts = sample_counts,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700489 .maxColorAttachments = MAX_RTS,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800490 .sampledImageColorSampleCounts = sample_counts,
491 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
492 .sampledImageDepthSampleCounts = sample_counts,
493 .sampledImageStencilSampleCounts = sample_counts,
494 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700495 .maxSampleMaskWords = 1,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800496 .timestampPeriod = 80.0 / (1000 * 1000 * 1000),
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700497 .maxClipDistances = 0 /* FIXME */,
498 .maxCullDistances = 0 /* FIXME */,
499 .maxCombinedClipAndCullDistances = 0 /* FIXME */,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800500 .discreteQueuePriorities = 1,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700501 .pointSizeRange = { 0.125, 255.875 },
502 .lineWidthRange = { 0.0, 7.9921875 },
503 .pointSizeGranularity = (1.0 / 8.0),
504 .lineWidthGranularity = (1.0 / 128.0),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800505 .strictLines = false, /* FINISHME */
506 .standardSampleLocations = true, /* FINISHME */
507 .optimalBufferCopyOffsetAlignment = 128,
508 .optimalBufferCopyRowPitchAlignment = 128,
509 .nonCoherentAtomSize = 64,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700510 };
511
Jason Ekstrand977a4692015-07-09 15:53:03 -0700512 *pProperties = (VkPhysicalDeviceProperties) {
Jason Ekstrandbfeaf672015-12-03 15:23:33 -0800513 .apiVersion = VK_MAKE_VERSION(0, 210, 1),
Jason Ekstrand977a4692015-07-09 15:53:03 -0700514 .driverVersion = 1,
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800515 .vendorID = 0x8086,
516 .deviceID = pdevice->chipset_id,
Jason Ekstrand977a4692015-07-09 15:53:03 -0700517 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
Chad Versaced48e71c2015-10-07 10:36:46 -0700518 .limits = limits,
519 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
Jason Ekstrand977a4692015-07-09 15:53:03 -0700520 };
521
522 strcpy(pProperties->deviceName, pdevice->name);
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800523 snprintf((char *)pProperties->pipelineCacheUUID, VK_UUID_SIZE,
Jason Ekstrand977a4692015-07-09 15:53:03 -0700524 "anv-%s", MESA_GIT_SHA1 + 4);
Jason Ekstrand977a4692015-07-09 15:53:03 -0700525}
526
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800527void anv_GetPhysicalDeviceQueueFamilyProperties(
Jason Ekstrand1f907012015-07-09 16:11:24 -0700528 VkPhysicalDevice physicalDevice,
Jason Ekstranda6eba402015-10-05 21:17:12 -0700529 uint32_t* pCount,
530 VkQueueFamilyProperties* pQueueFamilyProperties)
Jason Ekstrand1f907012015-07-09 16:11:24 -0700531{
Jason Ekstranda6eba402015-10-05 21:17:12 -0700532 if (pQueueFamilyProperties == NULL) {
533 *pCount = 1;
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800534 return;
Jason Ekstranda6eba402015-10-05 21:17:12 -0700535 }
Jason Ekstrand1f907012015-07-09 16:11:24 -0700536
Jason Ekstranda6eba402015-10-05 21:17:12 -0700537 assert(*pCount >= 1);
Jason Ekstrand1f907012015-07-09 16:11:24 -0700538
Jason Ekstranda6eba402015-10-05 21:17:12 -0700539 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
Jason Ekstrand1f907012015-07-09 16:11:24 -0700540 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
541 VK_QUEUE_COMPUTE_BIT |
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800542 VK_QUEUE_TRANSFER_BIT,
Jason Ekstrand1f907012015-07-09 16:11:24 -0700543 .queueCount = 1,
Jason Ekstrand74c4c4a2015-12-02 16:20:40 -0800544 .timestampValidBits = 0, /* XXX: Real value here */
545 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
Jason Ekstrand1f907012015-07-09 16:11:24 -0700546 };
Jason Ekstrand1f907012015-07-09 16:11:24 -0700547}
548
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800549void anv_GetPhysicalDeviceMemoryProperties(
Chad Versacedf2a0132015-07-09 19:49:19 -0700550 VkPhysicalDevice physicalDevice,
551 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
552{
553 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700554 VkDeviceSize heap_size;
Chad Versacedf2a0132015-07-09 19:49:19 -0700555
556 /* Reserve some wiggle room for the driver by exposing only 75% of the
557 * aperture to the heap.
558 */
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700559 heap_size = 3 * physical_device->aperture_size / 4;
Chad Versacedf2a0132015-07-09 19:49:19 -0700560
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800561 if (physical_device->info->has_llc) {
562 /* Big core GPUs share LLC with the CPU and thus one memory type can be
563 * both cached and coherent at the same time.
564 */
565 pMemoryProperties->memoryTypeCount = 1;
566 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
567 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
568 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
569 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
570 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800571 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800572 };
573 } else {
574 /* The spec requires that we expose a host-visible, coherent memory
575 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
576 * to give the application a choice between cached, but not coherent and
577 * coherent but uncached (WC though).
578 */
579 pMemoryProperties->memoryTypeCount = 2;
580 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
581 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
582 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
583 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800584 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800585 };
586 pMemoryProperties->memoryTypes[1] = (VkMemoryType) {
587 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
588 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
589 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800590 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800591 };
592 }
Chad Versacedf2a0132015-07-09 19:49:19 -0700593
594 pMemoryProperties->memoryHeapCount = 1;
595 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
596 .size = heap_size,
Jason Ekstrande6ab06a2015-12-02 10:39:15 -0800597 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
Chad Versacedf2a0132015-07-09 19:49:19 -0700598 };
Chad Versacedf2a0132015-07-09 19:49:19 -0700599}
600
Jason Ekstrande7acdda2015-07-07 18:51:53 -0700601PFN_vkVoidFunction anv_GetInstanceProcAddr(
602 VkInstance instance,
603 const char* pName)
604{
605 return anv_lookup_entrypoint(pName);
606}
607
608PFN_vkVoidFunction anv_GetDeviceProcAddr(
609 VkDevice device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700610 const char* pName)
611{
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700612 return anv_lookup_entrypoint(pName);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700613}
614
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700615static VkResult
616anv_queue_init(struct anv_device *device, struct anv_queue *queue)
617{
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700618 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700619 queue->device = device;
620 queue->pool = &device->surface_state_pool;
621
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700622 return VK_SUCCESS;
623}
624
625static void
626anv_queue_finish(struct anv_queue *queue)
627{
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700628}
629
Kristian Høgsberg77359202015-12-01 15:37:12 -0800630static struct anv_state
631anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
632{
633 struct anv_state state;
634
635 state = anv_state_pool_alloc(pool, size, align);
636 memcpy(state.map, p, size);
637
638 if (!pool->block_pool->device->info.has_llc)
639 anv_state_clflush(state);
640
641 return state;
642}
643
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700644static void
645anv_device_init_border_colors(struct anv_device *device)
646{
Jason Ekstrand522ab832015-07-08 11:44:52 -0700647 static const VkClearColorValue border_colors[] = {
Jason Ekstrandbd4cde72015-10-06 10:07:47 -0700648 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
649 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
650 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
651 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
652 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
653 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700654 };
655
Kristian Høgsberg77359202015-12-01 15:37:12 -0800656 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
657 sizeof(border_colors), 32, border_colors);
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700658}
659
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700660VkResult anv_CreateDevice(
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700661 VkPhysicalDevice physicalDevice,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700662 const VkDeviceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800663 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700664 VkDevice* pDevice)
665{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700666 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800667 VkResult result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700668 struct anv_device *device;
669
670 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
671
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800672 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700673 bool found = false;
674 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
675 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800676 device_extensions[j].extensionName) == 0) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700677 found = true;
678 break;
679 }
680 }
681 if (!found)
Chad Versacef9c948e2015-10-07 11:36:51 -0700682 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700683 }
684
Jason Ekstrandaa3002b2015-11-17 06:30:02 -0800685 anv_set_dispatch_devinfo(physical_device->info);
Kristian Høgsberg Kristensenee978892015-07-30 14:44:01 -0700686
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800687 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
688 sizeof(*device), 8,
689 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700690 if (!device)
691 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
692
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700693 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700694 device->instance = physical_device->instance;
Chad Versace8cda3e92015-07-09 16:31:39 -0700695
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800696 if (pAllocator)
697 device->alloc = *pAllocator;
698 else
699 device->alloc = physical_device->instance->alloc;
700
Chad Versace8cda3e92015-07-09 16:31:39 -0700701 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700702 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800703 if (device->fd == -1) {
704 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700705 goto fail_device;
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800706 }
Chad Versace477383e2015-11-13 10:12:18 -0800707
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700708 device->context_id = anv_gem_create_context(device);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800709 if (device->context_id == -1) {
710 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700711 goto fail_fd;
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800712 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700713
Jason Ekstrand9c84b6c2015-12-28 13:26:49 -0800714 device->info = *physical_device->info;
715 device->isl_dev = physical_device->isl_dev;
716
Jason Ekstranda788e7c2015-09-17 18:23:21 -0700717 pthread_mutex_init(&device->mutex, NULL);
718
Jason Ekstrand0c4a2da2015-07-30 11:34:09 -0700719 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700720
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700721 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700722
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700723 anv_state_pool_init(&device->dynamic_state_pool,
724 &device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700725
Kristian Høgsberg Kristensen30521fb2016-01-05 12:00:54 -0800726 anv_block_pool_init(&device->instruction_block_pool, device, 64 * 1024);
727 anv_pipeline_cache_init(&device->default_pipeline_cache, device);
728
Jason Ekstrand0e944462015-09-22 16:36:00 -0700729 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700730
731 anv_state_pool_init(&device->surface_state_pool,
732 &device->surface_state_block_pool);
733
Jason Ekstrand3a3d79b2015-11-10 16:42:34 -0800734 anv_bo_init_new(&device->workaround_bo, device, 1024);
735
Kristian Høgsberg Kristensen9b9f9732015-06-19 15:41:30 -0700736 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
737
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700738 anv_queue_init(device, &device->queue);
739
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800740 result = anv_device_init_meta(device);
741 if (result != VK_SUCCESS)
742 goto fail_fd;
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -0700743
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700744 anv_device_init_border_colors(device);
745
Jason Ekstrand098209e2015-07-09 18:41:27 -0700746 *pDevice = anv_device_to_handle(device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700747
748 return VK_SUCCESS;
749
750 fail_fd:
751 close(device->fd);
752 fail_device:
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800753 anv_free(&device->alloc, device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700754
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800755 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700756}
757
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700758void anv_DestroyDevice(
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800759 VkDevice _device,
760 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700761{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700762 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700763
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700764 anv_queue_finish(&device->queue);
765
Jason Ekstrand3a38b0d2015-06-09 11:08:51 -0700766 anv_device_finish_meta(device);
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700767
Jason Ekstrand38f5eef2015-06-09 11:41:31 -0700768#ifdef HAVE_VALGRIND
769 /* We only need to free these to prevent valgrind errors. The backing
770 * BO will go away in a couple of lines so we don't actually leak.
771 */
Jason Ekstrand522ab832015-07-08 11:44:52 -0700772 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
Jason Ekstrand38f5eef2015-06-09 11:41:31 -0700773#endif
774
Jason Ekstrand3a3d79b2015-11-10 16:42:34 -0800775 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
776 anv_gem_close(device, device->workaround_bo.gem_handle);
777
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700778 anv_bo_pool_finish(&device->batch_bo_pool);
Jason Ekstrand1920ef92015-07-31 10:30:57 -0700779 anv_state_pool_finish(&device->dynamic_state_pool);
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700780 anv_block_pool_finish(&device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700781 anv_block_pool_finish(&device->instruction_block_pool);
Jason Ekstrand1920ef92015-07-31 10:30:57 -0700782 anv_state_pool_finish(&device->surface_state_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700783 anv_block_pool_finish(&device->surface_state_block_pool);
Jason Ekstrand3460e6c2015-07-22 17:51:14 -0700784 anv_block_pool_finish(&device->scratch_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700785
786 close(device->fd);
787
Kristian Høgsberg Kristensenf5510472016-01-05 11:43:25 -0800788 pthread_mutex_destroy(&device->mutex);
789
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800790 anv_free(&device->alloc, device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700791}
792
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700793VkResult anv_EnumerateInstanceExtensionProperties(
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700794 const char* pLayerName,
Jason Ekstrandfe644722015-11-30 16:28:36 -0800795 uint32_t* pPropertyCount,
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700796 VkExtensionProperties* pProperties)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700797{
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700798 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800799 *pPropertyCount = ARRAY_SIZE(global_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700800 return VK_SUCCESS;
801 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700802
Jason Ekstrandfe644722015-11-30 16:28:36 -0800803 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700804
Jason Ekstrandfe644722015-11-30 16:28:36 -0800805 *pPropertyCount = ARRAY_SIZE(global_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700806 memcpy(pProperties, global_extensions, sizeof(global_extensions));
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700807
808 return VK_SUCCESS;
809}
810
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700811VkResult anv_EnumerateDeviceExtensionProperties(
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700812 VkPhysicalDevice physicalDevice,
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700813 const char* pLayerName,
Jason Ekstrandfe644722015-11-30 16:28:36 -0800814 uint32_t* pPropertyCount,
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700815 VkExtensionProperties* pProperties)
816{
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700817 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800818 *pPropertyCount = ARRAY_SIZE(device_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700819 return VK_SUCCESS;
820 }
821
Jason Ekstrandfe644722015-11-30 16:28:36 -0800822 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
Jason Ekstrand9a7600c2015-09-01 16:44:42 -0700823
Jason Ekstrandfe644722015-11-30 16:28:36 -0800824 *pPropertyCount = ARRAY_SIZE(device_extensions);
Jason Ekstrand9a7600c2015-09-01 16:44:42 -0700825 memcpy(pProperties, device_extensions, sizeof(device_extensions));
826
827 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700828}
829
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700830VkResult anv_EnumerateInstanceLayerProperties(
Jason Ekstrandfe644722015-11-30 16:28:36 -0800831 uint32_t* pPropertyCount,
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700832 VkLayerProperties* pProperties)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700833{
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700834 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800835 *pPropertyCount = 0;
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700836 return VK_SUCCESS;
837 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700838
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700839 /* None supported at this time */
Chad Versacef9c948e2015-10-07 11:36:51 -0700840 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700841}
842
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700843VkResult anv_EnumerateDeviceLayerProperties(
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700844 VkPhysicalDevice physicalDevice,
Jason Ekstrandfe644722015-11-30 16:28:36 -0800845 uint32_t* pPropertyCount,
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700846 VkLayerProperties* pProperties)
847{
848 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800849 *pPropertyCount = 0;
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700850 return VK_SUCCESS;
851 }
852
853 /* None supported at this time */
Chad Versacef9c948e2015-10-07 11:36:51 -0700854 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700855}
856
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800857void anv_GetDeviceQueue(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700858 VkDevice _device,
859 uint32_t queueNodeIndex,
860 uint32_t queueIndex,
861 VkQueue* pQueue)
862{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700863 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700864
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700865 assert(queueIndex == 0);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700866
Jason Ekstrand098209e2015-07-09 18:41:27 -0700867 *pQueue = anv_queue_to_handle(&device->queue);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700868}
869
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700870VkResult anv_QueueSubmit(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700871 VkQueue _queue,
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800872 uint32_t submitCount,
873 const VkSubmitInfo* pSubmits,
Kristian Høgsberg6afb2642015-05-18 08:49:15 -0700874 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700875{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700876 ANV_FROM_HANDLE(anv_queue, queue, _queue);
877 ANV_FROM_HANDLE(anv_fence, fence, _fence);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700878 struct anv_device *device = queue->device;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700879 int ret;
880
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800881 for (uint32_t i = 0; i < submitCount; i++) {
882 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
883 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
884 pSubmits[i].pCommandBuffers[j]);
885 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700886
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800887 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
Chad Versacef9c948e2015-10-07 11:36:51 -0700888 if (ret != 0) {
889 /* We don't know the real error. */
890 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
891 "execbuf2 failed: %m");
892 }
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700893
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800894 if (fence) {
895 ret = anv_gem_execbuffer(device, &fence->execbuf);
896 if (ret != 0) {
897 /* We don't know the real error. */
898 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
899 "execbuf2 failed: %m");
900 }
901 }
902
903 for (uint32_t k = 0; k < cmd_buffer->execbuf2.bo_count; k++)
904 cmd_buffer->execbuf2.bos[k]->offset = cmd_buffer->execbuf2.objects[k].offset;
905 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700906 }
907
908 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700909}
910
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700911VkResult anv_QueueWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700912 VkQueue _queue)
913{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700914 ANV_FROM_HANDLE(anv_queue, queue, _queue);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700915
Jason Ekstranda95f51c2015-09-24 14:20:35 -0700916 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700917}
918
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700919VkResult anv_DeviceWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700920 VkDevice _device)
921{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700922 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700923 struct anv_state state;
924 struct anv_batch batch;
925 struct drm_i915_gem_execbuffer2 execbuf;
926 struct drm_i915_gem_exec_object2 exec2_objects[1];
927 struct anv_bo *bo = NULL;
928 VkResult result;
929 int64_t timeout;
930 int ret;
931
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700932 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
933 bo = &device->dynamic_state_pool.block_pool->bo;
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700934 batch.start = batch.next = state.map;
935 batch.end = state.map + 32;
Kristian Høgsberg Kristensen74556b02015-08-13 21:05:47 -0700936 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
937 anv_batch_emit(&batch, GEN7_MI_NOOP);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700938
Kristian Høgsberg77359202015-12-01 15:37:12 -0800939 if (!device->info.has_llc)
940 anv_state_clflush(state);
941
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700942 exec2_objects[0].handle = bo->gem_handle;
943 exec2_objects[0].relocation_count = 0;
944 exec2_objects[0].relocs_ptr = 0;
945 exec2_objects[0].alignment = 0;
946 exec2_objects[0].offset = bo->offset;
947 exec2_objects[0].flags = 0;
948 exec2_objects[0].rsvd1 = 0;
949 exec2_objects[0].rsvd2 = 0;
950
951 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
952 execbuf.buffer_count = 1;
953 execbuf.batch_start_offset = state.offset;
954 execbuf.batch_len = batch.next - state.map;
955 execbuf.cliprects_ptr = 0;
956 execbuf.num_cliprects = 0;
957 execbuf.DR1 = 0;
958 execbuf.DR4 = 0;
959
960 execbuf.flags =
961 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
962 execbuf.rsvd1 = device->context_id;
963 execbuf.rsvd2 = 0;
964
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700965 ret = anv_gem_execbuffer(device, &execbuf);
966 if (ret != 0) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700967 /* We don't know the real error. */
968 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700969 goto fail;
970 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700971
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700972 timeout = INT64_MAX;
973 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
974 if (ret != 0) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700975 /* We don't know the real error. */
976 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700977 goto fail;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700978 }
979
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700980 anv_state_pool_free(&device->dynamic_state_pool, state);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700981
982 return VK_SUCCESS;
983
984 fail:
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700985 anv_state_pool_free(&device->dynamic_state_pool, state);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700986
987 return result;
988}
989
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700990VkResult
991anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
992{
993 bo->gem_handle = anv_gem_create(device, size);
994 if (!bo->gem_handle)
995 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
996
997 bo->map = NULL;
998 bo->index = 0;
999 bo->offset = 0;
1000 bo->size = size;
1001
1002 return VK_SUCCESS;
1003}
1004
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001005VkResult anv_AllocateMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001006 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001007 const VkMemoryAllocateInfo* pAllocateInfo,
1008 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001009 VkDeviceMemory* pMem)
1010{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001011 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001012 struct anv_device_memory *mem;
1013 VkResult result;
1014
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001015 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001016
Jason Ekstrandb1325402015-12-17 11:00:38 -08001017 if (pAllocateInfo->allocationSize == 0) {
1018 /* Apparently, this is allowed */
1019 *pMem = VK_NULL_HANDLE;
1020 return VK_SUCCESS;
1021 }
1022
Chad Versacef9c948e2015-10-07 11:36:51 -07001023 /* We support exactly one memory heap. */
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -08001024 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1025 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
Chad Versacef43a3042015-07-09 19:59:44 -07001026
1027 /* FINISHME: Fail if allocation request exceeds heap size. */
1028
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001029 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1030 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001031 if (mem == NULL)
1032 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1033
Jason Ekstrand6b0b5722016-01-02 07:52:22 -08001034 /* The kernel is going to give us whole pages anyway */
1035 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1036
1037 result = anv_bo_init_new(&mem->bo, device, alloc_size);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001038 if (result != VK_SUCCESS)
1039 goto fail;
1040
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -08001041 mem->type_index = pAllocateInfo->memoryTypeIndex;
1042
Jason Ekstrand098209e2015-07-09 18:41:27 -07001043 *pMem = anv_device_memory_to_handle(mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001044
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001045 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001046
1047 fail:
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001048 anv_free2(&device->alloc, pAllocator, mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001049
1050 return result;
1051}
1052
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001053void anv_FreeMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001054 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001055 VkDeviceMemory _mem,
1056 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001057{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001058 ANV_FROM_HANDLE(anv_device, device, _device);
1059 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001060
Jason Ekstrandb1325402015-12-17 11:00:38 -08001061 if (mem == NULL)
1062 return;
1063
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001064 if (mem->bo.map)
1065 anv_gem_munmap(mem->bo.map, mem->bo.size);
1066
1067 if (mem->bo.gem_handle != 0)
1068 anv_gem_close(device, mem->bo.gem_handle);
1069
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001070 anv_free2(&device->alloc, pAllocator, mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001071}
1072
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001073VkResult anv_MapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001074 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001075 VkDeviceMemory _memory,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001076 VkDeviceSize offset,
1077 VkDeviceSize size,
1078 VkMemoryMapFlags flags,
1079 void** ppData)
1080{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001081 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001082 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001083
Jason Ekstrandb1325402015-12-17 11:00:38 -08001084 if (mem == NULL) {
1085 *ppData = NULL;
1086 return VK_SUCCESS;
1087 }
1088
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001089 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1090 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1091 * at a time is valid. We could just mmap up front and return an offset
1092 * pointer here, but that may exhaust virtual memory on 32 bit
1093 * userspace. */
1094
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -08001095 uint32_t gem_flags = 0;
1096 if (!device->info.has_llc && mem->type_index == 0)
1097 gem_flags |= I915_MMAP_WC;
1098
Jason Ekstrandf076d532016-01-01 09:26:06 -08001099 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1100 uint64_t map_offset = offset & ~4095ull;
1101 assert(offset >= map_offset);
1102 uint64_t map_size = (offset + size) - map_offset;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001103
Jason Ekstrandf076d532016-01-01 09:26:06 -08001104 /* Let's map whole pages */
Jason Ekstrand6b0b5722016-01-02 07:52:22 -08001105 map_size = align_u64(map_size, 4096);
Jason Ekstrandf076d532016-01-01 09:26:06 -08001106
1107 mem->map = anv_gem_mmap(device, mem->bo.gem_handle,
1108 map_offset, map_size, gem_flags);
1109 mem->map_size = map_size;
1110
1111 *ppData = mem->map + (offset - map_offset);
Chad Versace477383e2015-11-13 10:12:18 -08001112
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001113 return VK_SUCCESS;
1114}
1115
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001116void anv_UnmapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001117 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001118 VkDeviceMemory _memory)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001119{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001120 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001121
Jason Ekstrandb1325402015-12-17 11:00:38 -08001122 if (mem == NULL)
1123 return;
1124
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001125 anv_gem_munmap(mem->map, mem->map_size);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001126}
1127
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001128static void
1129clflush_mapped_ranges(struct anv_device *device,
1130 uint32_t count,
1131 const VkMappedMemoryRange *ranges)
1132{
1133 for (uint32_t i = 0; i < count; i++) {
1134 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1135 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
1136 void *end = mem->map + ranges[i].offset + ranges[i].size;
1137
1138 while (p < end) {
1139 __builtin_ia32_clflush(p);
1140 p += CACHELINE_SIZE;
1141 }
1142 }
1143}
1144
Jason Ekstrandd9c2cae2015-07-07 17:22:29 -07001145VkResult anv_FlushMappedMemoryRanges(
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001146 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001147 uint32_t memoryRangeCount,
1148 const VkMappedMemoryRange* pMemoryRanges)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001149{
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001150 ANV_FROM_HANDLE(anv_device, device, _device);
1151
1152 if (device->info.has_llc)
1153 return VK_SUCCESS;
1154
1155 /* Make sure the writes we're flushing have landed. */
1156 __builtin_ia32_sfence();
1157
1158 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001159
1160 return VK_SUCCESS;
1161}
1162
Jason Ekstrandd9c2cae2015-07-07 17:22:29 -07001163VkResult anv_InvalidateMappedMemoryRanges(
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001164 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001165 uint32_t memoryRangeCount,
1166 const VkMappedMemoryRange* pMemoryRanges)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001167{
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001168 ANV_FROM_HANDLE(anv_device, device, _device);
1169
1170 if (device->info.has_llc)
1171 return VK_SUCCESS;
1172
1173 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1174
1175 /* Make sure no reads get moved up above the invalidate. */
1176 __builtin_ia32_lfence();
1177
1178 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001179}
1180
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001181void anv_GetBufferMemoryRequirements(
Jason Ekstrandef8980e2015-07-07 18:16:42 -07001182 VkDevice device,
Jason Ekstrand55723e92015-07-14 14:59:39 -07001183 VkBuffer _buffer,
Jason Ekstrandef8980e2015-07-07 18:16:42 -07001184 VkMemoryRequirements* pMemoryRequirements)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001185{
Jason Ekstrand55723e92015-07-14 14:59:39 -07001186 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001187
Chad Versacef43a3042015-07-09 19:59:44 -07001188 /* The Vulkan spec (git aaed022) says:
1189 *
1190 * memoryTypeBits is a bitfield and contains one bit set for every
1191 * supported memory type for the resource. The bit `1<<i` is set if and
1192 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1193 * structure for the physical device is supported.
1194 *
1195 * We support exactly one memory type.
1196 */
1197 pMemoryRequirements->memoryTypeBits = 1;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001198
Jason Ekstrand55723e92015-07-14 14:59:39 -07001199 pMemoryRequirements->size = buffer->size;
1200 pMemoryRequirements->alignment = 16;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001201}
1202
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001203void anv_GetImageMemoryRequirements(
Jason Ekstrandbb6567f2015-07-08 09:04:16 -07001204 VkDevice device,
Jason Ekstrand55723e92015-07-14 14:59:39 -07001205 VkImage _image,
1206 VkMemoryRequirements* pMemoryRequirements)
1207{
1208 ANV_FROM_HANDLE(anv_image, image, _image);
1209
1210 /* The Vulkan spec (git aaed022) says:
1211 *
1212 * memoryTypeBits is a bitfield and contains one bit set for every
1213 * supported memory type for the resource. The bit `1<<i` is set if and
1214 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1215 * structure for the physical device is supported.
1216 *
1217 * We support exactly one memory type.
1218 */
1219 pMemoryRequirements->memoryTypeBits = 1;
1220
1221 pMemoryRequirements->size = image->size;
1222 pMemoryRequirements->alignment = image->alignment;
Jason Ekstrand55723e92015-07-14 14:59:39 -07001223}
1224
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001225void anv_GetImageSparseMemoryRequirements(
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001226 VkDevice device,
1227 VkImage image,
Jason Ekstrand5a024412015-12-02 03:34:43 -08001228 uint32_t* pSparseMemoryRequirementCount,
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001229 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1230{
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001231 stub();
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001232}
1233
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001234void anv_GetDeviceMemoryCommitment(
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001235 VkDevice device,
1236 VkDeviceMemory memory,
1237 VkDeviceSize* pCommittedMemoryInBytes)
1238{
1239 *pCommittedMemoryInBytes = 0;
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001240}
1241
Jason Ekstrand55723e92015-07-14 14:59:39 -07001242VkResult anv_BindBufferMemory(
1243 VkDevice device,
1244 VkBuffer _buffer,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001245 VkDeviceMemory _memory,
1246 VkDeviceSize memoryOffset)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001247{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001248 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Jason Ekstrand55723e92015-07-14 14:59:39 -07001249 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001250
Jason Ekstrandb1325402015-12-17 11:00:38 -08001251 if (mem) {
1252 buffer->bo = &mem->bo;
1253 buffer->offset = memoryOffset;
1254 } else {
1255 buffer->bo = NULL;
1256 buffer->offset = 0;
1257 }
Jason Ekstrand55723e92015-07-14 14:59:39 -07001258
1259 return VK_SUCCESS;
1260}
1261
1262VkResult anv_BindImageMemory(
1263 VkDevice device,
1264 VkImage _image,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001265 VkDeviceMemory _memory,
1266 VkDeviceSize memoryOffset)
Jason Ekstrand55723e92015-07-14 14:59:39 -07001267{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001268 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Jason Ekstrand55723e92015-07-14 14:59:39 -07001269 ANV_FROM_HANDLE(anv_image, image, _image);
1270
Jason Ekstrandb1325402015-12-17 11:00:38 -08001271 if (mem) {
1272 image->bo = &mem->bo;
1273 image->offset = memoryOffset;
1274 } else {
1275 image->bo = NULL;
1276 image->offset = 0;
1277 }
Jason Ekstrandbb6567f2015-07-08 09:04:16 -07001278
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001279 return VK_SUCCESS;
1280}
1281
Jason Ekstrandfd536032015-11-30 16:42:12 -08001282VkResult anv_QueueBindSparse(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001283 VkQueue queue,
Jason Ekstrandfd536032015-11-30 16:42:12 -08001284 uint32_t bindInfoCount,
1285 const VkBindSparseInfo* pBindInfo,
1286 VkFence fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001287{
Jason Ekstrandfed35862015-12-02 16:14:58 -08001288 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001289}
1290
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001291VkResult anv_CreateFence(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001292 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001293 const VkFenceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001294 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001295 VkFence* pFence)
1296{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001297 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001298 struct anv_fence *fence;
1299 struct anv_batch batch;
1300 VkResult result;
1301
1302 const uint32_t fence_size = 128;
1303
1304 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1305
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001306 fence = anv_alloc2(&device->alloc, pAllocator, sizeof(*fence), 8,
1307 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001308 if (fence == NULL)
1309 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1310
1311 result = anv_bo_init_new(&fence->bo, device, fence_size);
1312 if (result != VK_SUCCESS)
1313 goto fail;
1314
1315 fence->bo.map =
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -08001316 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size, 0);
Jason Ekstrandda8f1482015-05-27 11:42:55 -07001317 batch.next = batch.start = fence->bo.map;
1318 batch.end = fence->bo.map + fence->bo.size;
Kristian Høgsberg Kristensencff717c2015-08-18 11:04:19 -07001319 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1320 anv_batch_emit(&batch, GEN7_MI_NOOP);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001321
Kristian Høgsberg77359202015-12-01 15:37:12 -08001322 if (!device->info.has_llc) {
1323 assert(((uintptr_t) fence->bo.map & CACHELINE_MASK) == 0);
1324 assert(batch.next - fence->bo.map <= CACHELINE_SIZE);
1325 __builtin_ia32_sfence();
1326 __builtin_ia32_clflush(fence->bo.map);
1327 }
1328
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001329 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1330 fence->exec2_objects[0].relocation_count = 0;
1331 fence->exec2_objects[0].relocs_ptr = 0;
1332 fence->exec2_objects[0].alignment = 0;
1333 fence->exec2_objects[0].offset = fence->bo.offset;
1334 fence->exec2_objects[0].flags = 0;
1335 fence->exec2_objects[0].rsvd1 = 0;
1336 fence->exec2_objects[0].rsvd2 = 0;
1337
1338 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1339 fence->execbuf.buffer_count = 1;
1340 fence->execbuf.batch_start_offset = 0;
1341 fence->execbuf.batch_len = batch.next - fence->bo.map;
1342 fence->execbuf.cliprects_ptr = 0;
1343 fence->execbuf.num_cliprects = 0;
1344 fence->execbuf.DR1 = 0;
1345 fence->execbuf.DR4 = 0;
1346
1347 fence->execbuf.flags =
1348 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1349 fence->execbuf.rsvd1 = device->context_id;
1350 fence->execbuf.rsvd2 = 0;
1351
Jason Ekstrand098209e2015-07-09 18:41:27 -07001352 *pFence = anv_fence_to_handle(fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001353
1354 return VK_SUCCESS;
1355
1356 fail:
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001357 anv_free2(&device->alloc, pAllocator, fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001358
1359 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001360}
1361
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001362void anv_DestroyFence(
Chad Versaceebb191f2015-07-14 09:29:35 -07001363 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001364 VkFence _fence,
1365 const VkAllocationCallbacks* pAllocator)
Chad Versaceebb191f2015-07-14 09:29:35 -07001366{
1367 ANV_FROM_HANDLE(anv_device, device, _device);
1368 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1369
1370 anv_gem_munmap(fence->bo.map, fence->bo.size);
1371 anv_gem_close(device, fence->bo.gem_handle);
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001372 anv_free2(&device->alloc, pAllocator, fence);
Chad Versaceebb191f2015-07-14 09:29:35 -07001373}
1374
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001375VkResult anv_ResetFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001376 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001377 uint32_t fenceCount,
Jason Ekstrandd5349b12015-07-07 17:18:00 -07001378 const VkFence* pFences)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001379{
Chad Versace169251b2015-07-17 13:59:48 -07001380 for (uint32_t i = 0; i < fenceCount; i++) {
1381 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1382 fence->ready = false;
1383 }
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001384
1385 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001386}
1387
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001388VkResult anv_GetFenceStatus(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001389 VkDevice _device,
1390 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001391{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001392 ANV_FROM_HANDLE(anv_device, device, _device);
1393 ANV_FROM_HANDLE(anv_fence, fence, _fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001394 int64_t t = 0;
1395 int ret;
1396
1397 if (fence->ready)
1398 return VK_SUCCESS;
1399
1400 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1401 if (ret == 0) {
1402 fence->ready = true;
1403 return VK_SUCCESS;
1404 }
Jason Ekstrand5c497302015-07-09 18:20:28 -07001405
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001406 return VK_NOT_READY;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001407}
1408
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001409VkResult anv_WaitForFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001410 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001411 uint32_t fenceCount,
1412 const VkFence* pFences,
Chad Versace8f3b2182015-07-13 12:59:42 -07001413 VkBool32 waitAll,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001414 uint64_t timeout)
1415{
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001416 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrandaafc8742015-11-10 11:24:08 -08001417
1418 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1419 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1420 * for a couple of kernel releases. Since there's no way to know
1421 * whether or not the kernel we're using is one of the broken ones, the
1422 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1423 * maximum timeout from 584 years to 292 years - likely not a big deal.
1424 */
1425 if (timeout > INT64_MAX)
1426 timeout = INT64_MAX;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001427
Jason Ekstrand427978d2015-11-10 15:02:52 -08001428 int64_t t = timeout;
1429
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001430 /* FIXME: handle !waitAll */
1431
1432 for (uint32_t i = 0; i < fenceCount; i++) {
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001433 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
Jason Ekstrand427978d2015-11-10 15:02:52 -08001434 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
Chad Versacef9c948e2015-10-07 11:36:51 -07001435 if (ret == -1 && errno == ETIME) {
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001436 return VK_TIMEOUT;
Chad Versacef9c948e2015-10-07 11:36:51 -07001437 } else if (ret == -1) {
1438 /* We don't know the real error. */
1439 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1440 "gem wait failed: %m");
1441 }
Jason Ekstrand5c497302015-07-09 18:20:28 -07001442 }
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001443
1444 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001445}
1446
1447// Queue semaphore functions
1448
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001449VkResult anv_CreateSemaphore(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001450 VkDevice device,
1451 const VkSemaphoreCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001452 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001453 VkSemaphore* pSemaphore)
1454{
Kristian Høgsberg Kristensena00524a2015-12-20 22:58:38 -08001455 /* The DRM execbuffer ioctl always execute in-oder, even between different
1456 * rings. As such, there's nothing to do for the user space semaphore.
1457 */
1458
Jason Ekstrand3db43e82015-11-30 10:31:44 -08001459 *pSemaphore = (VkSemaphore)1;
Kristian Høgsberg Kristensena00524a2015-12-20 22:58:38 -08001460
1461 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001462}
1463
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001464void anv_DestroySemaphore(
Chad Versace549070b2015-07-14 09:31:34 -07001465 VkDevice device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001466 VkSemaphore semaphore,
1467 const VkAllocationCallbacks* pAllocator)
Chad Versace549070b2015-07-14 09:31:34 -07001468{
Chad Versace549070b2015-07-14 09:31:34 -07001469}
1470
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001471// Event functions
1472
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001473VkResult anv_CreateEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001474 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001475 const VkEventCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001476 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001477 VkEvent* pEvent)
1478{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001479 ANV_FROM_HANDLE(anv_device, device, _device);
1480 struct anv_state state;
1481 struct anv_event *event;
1482
1483 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
1484
1485 state = anv_state_pool_alloc(&device->dynamic_state_pool,
1486 sizeof(*event), 4);
1487 event = state.map;
1488 event->state = state;
1489 event->semaphore = VK_EVENT_RESET;
1490
1491 if (!device->info.has_llc) {
1492 /* Make sure the writes we're flushing have landed. */
1493 __builtin_ia32_sfence();
1494 __builtin_ia32_clflush(event);
1495 }
1496
1497 *pEvent = anv_event_to_handle(event);
1498
1499 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001500}
1501
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001502void anv_DestroyEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001503 VkDevice _device,
1504 VkEvent _event,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001505 const VkAllocationCallbacks* pAllocator)
Chad Versace68c7ef52015-07-14 09:33:47 -07001506{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001507 ANV_FROM_HANDLE(anv_device, device, _device);
1508 ANV_FROM_HANDLE(anv_event, event, _event);
1509
1510 anv_state_pool_free(&device->dynamic_state_pool, event->state);
Chad Versace68c7ef52015-07-14 09:33:47 -07001511}
1512
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001513VkResult anv_GetEventStatus(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001514 VkDevice _device,
1515 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001516{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001517 ANV_FROM_HANDLE(anv_device, device, _device);
1518 ANV_FROM_HANDLE(anv_event, event, _event);
1519
1520 if (!device->info.has_llc) {
1521 /* Make sure the writes we're flushing have landed. */
1522 __builtin_ia32_clflush(event);
1523 __builtin_ia32_lfence();
1524 }
1525
1526 return event->semaphore;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001527}
1528
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001529VkResult anv_SetEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001530 VkDevice _device,
1531 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001532{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001533 ANV_FROM_HANDLE(anv_device, device, _device);
1534 ANV_FROM_HANDLE(anv_event, event, _event);
1535
1536 event->semaphore = VK_EVENT_SET;
1537
1538 if (!device->info.has_llc) {
1539 /* Make sure the writes we're flushing have landed. */
1540 __builtin_ia32_sfence();
1541 __builtin_ia32_clflush(event);
1542 }
1543
1544 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001545}
1546
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001547VkResult anv_ResetEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001548 VkDevice _device,
1549 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001550{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001551 ANV_FROM_HANDLE(anv_device, device, _device);
1552 ANV_FROM_HANDLE(anv_event, event, _event);
1553
1554 event->semaphore = VK_EVENT_RESET;
1555
1556 if (!device->info.has_llc) {
1557 /* Make sure the writes we're flushing have landed. */
1558 __builtin_ia32_sfence();
1559 __builtin_ia32_clflush(event);
1560 }
1561
1562 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001563}
1564
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001565// Buffer functions
1566
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001567VkResult anv_CreateBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001568 VkDevice _device,
1569 const VkBufferCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001570 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001571 VkBuffer* pBuffer)
1572{
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001573 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001574 struct anv_buffer *buffer;
1575
1576 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1577
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001578 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1579 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001580 if (buffer == NULL)
1581 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1582
1583 buffer->size = pCreateInfo->size;
Jason Ekstrand783a2112015-12-14 16:51:12 -08001584 buffer->usage = pCreateInfo->usage;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07001585 buffer->bo = NULL;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001586 buffer->offset = 0;
1587
Jason Ekstrand098209e2015-07-09 18:41:27 -07001588 *pBuffer = anv_buffer_to_handle(buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001589
1590 return VK_SUCCESS;
1591}
1592
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001593void anv_DestroyBuffer(
Chad Versacee93b6d82015-07-14 09:47:45 -07001594 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001595 VkBuffer _buffer,
1596 const VkAllocationCallbacks* pAllocator)
Chad Versacee93b6d82015-07-14 09:47:45 -07001597{
1598 ANV_FROM_HANDLE(anv_device, device, _device);
1599 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1600
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001601 anv_free2(&device->alloc, pAllocator, buffer);
Chad Versacee93b6d82015-07-14 09:47:45 -07001602}
1603
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001604void
1605anv_fill_buffer_surface_state(struct anv_device *device, void *state,
Jason Ekstrand1f98bf82015-12-14 16:14:20 -08001606 enum isl_format format,
Jason Ekstrand399d5312015-11-06 15:14:10 -08001607 uint32_t offset, uint32_t range, uint32_t stride)
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001608{
1609 switch (device->info.gen) {
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -07001610 case 7:
Jason Ekstrandf0390bc2015-11-17 07:07:02 -08001611 if (device->info.is_haswell)
1612 gen75_fill_buffer_surface_state(state, format, offset, range, stride);
1613 else
1614 gen7_fill_buffer_surface_state(state, format, offset, range, stride);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -07001615 break;
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001616 case 8:
Jason Ekstrand399d5312015-11-06 15:14:10 -08001617 gen8_fill_buffer_surface_state(state, format, offset, range, stride);
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001618 break;
Kristian Høgsberg Kristensencd4721c2015-11-25 22:27:01 -08001619 case 9:
1620 gen9_fill_buffer_surface_state(state, format, offset, range, stride);
1621 break;
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001622 default:
1623 unreachable("unsupported gen\n");
1624 }
1625}
1626
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001627void anv_DestroySampler(
Chad Versaceec5e2f42015-07-14 10:34:00 -07001628 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001629 VkSampler _sampler,
1630 const VkAllocationCallbacks* pAllocator)
Chad Versaceec5e2f42015-07-14 10:34:00 -07001631{
1632 ANV_FROM_HANDLE(anv_device, device, _device);
1633 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1634
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001635 anv_free2(&device->alloc, pAllocator, sampler);
Chad Versaceec5e2f42015-07-14 10:34:00 -07001636}
1637
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001638VkResult anv_CreateFramebuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001639 VkDevice _device,
1640 const VkFramebufferCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001641 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001642 VkFramebuffer* pFramebuffer)
1643{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001644 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001645 struct anv_framebuffer *framebuffer;
1646
1647 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1648
Jason Ekstrand84783502015-07-10 20:18:52 -07001649 size_t size = sizeof(*framebuffer) +
Chad Versaced4446a72015-10-06 11:42:43 -07001650 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001651 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1652 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001653 if (framebuffer == NULL)
1654 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1655
Jason Ekstrand84783502015-07-10 20:18:52 -07001656 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1657 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
Chad Versace6dea1a92015-10-07 07:30:52 -07001658 VkImageView _iview = pCreateInfo->pAttachments[i];
Chad Versaced4446a72015-10-06 11:42:43 -07001659 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001660 }
1661
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001662 framebuffer->width = pCreateInfo->width;
1663 framebuffer->height = pCreateInfo->height;
1664 framebuffer->layers = pCreateInfo->layers;
1665
Jason Ekstrand098209e2015-07-09 18:41:27 -07001666 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001667
1668 return VK_SUCCESS;
1669}
1670
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001671void anv_DestroyFramebuffer(
Chad Versace08f77312015-07-14 10:59:30 -07001672 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001673 VkFramebuffer _fb,
1674 const VkAllocationCallbacks* pAllocator)
Chad Versace08f77312015-07-14 10:59:30 -07001675{
1676 ANV_FROM_HANDLE(anv_device, device, _device);
1677 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1678
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001679 anv_free2(&device->alloc, pAllocator, fb);
Chad Versace08f77312015-07-14 10:59:30 -07001680}
1681
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001682void vkCmdDbgMarkerBegin(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001683 VkCommandBuffer commandBuffer,
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001684 const char* pMarker)
1685 __attribute__ ((visibility ("default")));
1686
1687void vkCmdDbgMarkerEnd(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001688 VkCommandBuffer commandBuffer)
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001689 __attribute__ ((visibility ("default")));
1690
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001691void vkCmdDbgMarkerBegin(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001692 VkCommandBuffer commandBuffer,
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001693 const char* pMarker)
1694{
1695}
1696
1697void vkCmdDbgMarkerEnd(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001698 VkCommandBuffer commandBuffer)
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001699{
1700}