blob: 67323e08860fdd842b2983b07ae62d02d251ba98 [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
Emil Velikov83548e12016-11-24 20:30:39 +000024#include <dlfcn.h>
Kristian Høgsberg769785c2015-05-08 22:32:37 -070025#include <assert.h>
26#include <stdbool.h>
27#include <string.h>
Jason Ekstrand920f34a2016-11-07 17:24:24 -080028#include <sys/mman.h>
Emil Velikov83548e12016-11-24 20:30:39 +000029#include <sys/stat.h>
Kristian Høgsberg769785c2015-05-08 22:32:37 -070030#include <unistd.h>
31#include <fcntl.h>
32
Chad Versace2c2233e2015-07-17 15:04:27 -070033#include "anv_private.h"
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -070034#include "util/strtod.h"
Jason Ekstrande45748b2016-01-20 11:16:44 -080035#include "util/debug.h"
Kristian Høgsberg769785c2015-05-08 22:32:37 -070036
Jason Ekstrandf6d95872016-02-18 10:19:02 -080037#include "genxml/gen7_pack.h"
Jason Ekstrandde54b4b2015-11-16 12:29:07 -080038
Jason Ekstranda95f51c2015-09-24 14:20:35 -070039struct anv_dispatch_table dtable;
40
Jason Ekstranda71e6142015-10-19 22:06:59 -070041static void
42compiler_debug_log(void *data, const char *fmt, ...)
43{ }
44
45static void
46compiler_perf_log(void *data, const char *fmt, ...)
47{
48 va_list args;
49 va_start(args, fmt);
50
51 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
52 vfprintf(stderr, fmt, args);
53
54 va_end(args);
55}
56
Emil Velikov83548e12016-11-24 20:30:39 +000057static bool
58anv_get_function_timestamp(void *ptr, uint32_t* timestamp)
59{
60 Dl_info info;
61 struct stat st;
62 if (!dladdr(ptr, &info) || !info.dli_fname)
63 return false;
64
65 if (stat(info.dli_fname, &st))
66 return false;
67
68 *timestamp = st.st_mtim.tv_sec;
69 return true;
70}
71
72static bool
Emil Velikovde138e92016-11-24 20:30:38 +000073anv_device_get_cache_uuid(void *uuid)
74{
Emil Velikov83548e12016-11-24 20:30:39 +000075 uint32_t timestamp;
76
Emil Velikovde138e92016-11-24 20:30:38 +000077 memset(uuid, 0, VK_UUID_SIZE);
Kenneth Graunke15d3fc12016-11-28 13:37:44 -080078 if (!anv_get_function_timestamp(anv_device_get_cache_uuid, &timestamp))
79 return false;
Emil Velikov83548e12016-11-24 20:30:39 +000080
81 snprintf(uuid, VK_UUID_SIZE, "anv-%d", timestamp);
82 return true;
Emil Velikovde138e92016-11-24 20:30:38 +000083}
84
Kristian Høgsberg769785c2015-05-08 22:32:37 -070085static VkResult
Chad Versace4422bd42015-07-09 16:22:18 -070086anv_physical_device_init(struct anv_physical_device *device,
87 struct anv_instance *instance,
88 const char *path)
Kristian Høgsberg769785c2015-05-08 22:32:37 -070089{
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070090 VkResult result;
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -070091 int fd;
92
93 fd = open(path, O_RDWR | O_CLOEXEC);
94 if (fd < 0)
Jason Ekstrand34ff4fb2016-08-22 18:10:14 -070095 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
Kristian Høgsberg769785c2015-05-08 22:32:37 -070096
Jason Ekstrand39cd3782015-09-24 13:51:40 -070097 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Kristian Høgsberg769785c2015-05-08 22:32:37 -070098 device->instance = instance;
Jason Ekstrande023c102016-05-24 11:02:18 -070099
100 assert(strlen(path) < ARRAY_SIZE(device->path));
101 strncpy(device->path, path, ARRAY_SIZE(device->path));
Chad Versacef9c948e2015-10-07 11:36:51 -0700102
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700103 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700104 if (!device->chipset_id) {
Jason Ekstrand34ff4fb2016-08-22 18:10:14 -0700105 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700106 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700107 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700108
Jason Ekstrand979d0ac2016-08-25 16:22:58 -0700109 device->name = gen_get_device_name(device->chipset_id);
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300110 if (!gen_get_device_info(device->chipset_id, &device->info)) {
Jason Ekstrand34ff4fb2016-08-22 18:10:14 -0700111 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700112 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700113 }
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800114
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300115 if (device->info.is_haswell) {
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800116 fprintf(stderr, "WARNING: Haswell Vulkan support is incomplete\n");
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300117 } else if (device->info.gen == 7 && !device->info.is_baytrail) {
Jason Ekstrand862da6a2015-11-09 12:18:12 -0800118 fprintf(stderr, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300119 } else if (device->info.gen == 7 && device->info.is_baytrail) {
Kristian Høgsbergdac57752015-12-01 15:39:30 -0800120 fprintf(stderr, "WARNING: Bay Trail Vulkan support is incomplete\n");
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300121 } else if (device->info.gen >= 8) {
Kristian Høgsberg Kristensen7c5e1fd2016-01-08 22:24:58 -0800122 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
123 * supported as anything */
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800124 } else {
Jason Ekstrandfed35862015-12-02 16:14:58 -0800125 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800126 "Vulkan not yet supported on %s", device->name);
127 goto fail;
128 }
129
Jordan Justen1a3adae2016-03-28 14:45:24 -0700130 device->cmd_parser_version = -1;
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300131 if (device->info.gen == 7) {
Jordan Justen1a3adae2016-03-28 14:45:24 -0700132 device->cmd_parser_version =
133 anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
134 if (device->cmd_parser_version == -1) {
135 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
136 "failed to get command parser version");
137 goto fail;
138 }
139 }
140
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700141 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700142 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
143 "failed to get aperture size: %m");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700144 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700145 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700146
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700147 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700148 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
149 "kernel missing gem wait");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700150 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700151 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700152
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700153 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700154 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
155 "kernel missing execbuf2");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700156 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700157 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700158
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300159 if (!device->info.has_llc &&
Kristian Høgsberg Kristensen220ac932015-12-19 22:25:57 -0800160 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -0800161 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
162 "kernel missing wc mmap");
163 goto fail;
164 }
165
Emil Velikov83548e12016-11-24 20:30:39 +0000166 if (!anv_device_get_cache_uuid(device->uuid)) {
167 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
168 "cannot generate UUID");
169 goto fail;
170 }
Jason Ekstrand580b2e82016-01-05 13:53:05 -0800171 bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
172
Lionel Landwerlin09394ee2016-09-07 17:19:35 +0100173 /* GENs prior to 8 do not support EU/Subslice info */
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300174 if (device->info.gen >= 8) {
Lionel Landwerlin09394ee2016-09-07 17:19:35 +0100175 device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
176 device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
177
178 /* Without this information, we cannot get the right Braswell
179 * brandstrings, and we have to use conservative numbers for GPGPU on
180 * many platforms, but otherwise, things will just work.
181 */
182 if (device->subslice_total < 1 || device->eu_total < 1) {
183 fprintf(stderr, "WARNING: Kernel 4.1 required to properly"
184 " query GPU properties.\n");
185 }
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300186 } else if (device->info.gen == 7) {
187 device->subslice_total = 1 << (device->info.gt - 1);
Lionel Landwerlin09394ee2016-09-07 17:19:35 +0100188 }
189
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300190 if (device->info.is_cherryview &&
Lionel Landwerlin09394ee2016-09-07 17:19:35 +0100191 device->subslice_total > 0 && device->eu_total > 0) {
192 /* Logical CS threads = EUs per subslice * 7 threads per EU */
Lionel Landwerlin6b217282016-09-23 01:04:25 +0300193 uint32_t max_cs_threads = device->eu_total / device->subslice_total * 7;
Lionel Landwerlin09394ee2016-09-07 17:19:35 +0100194
195 /* Fuse configurations may give more threads than expected, never less. */
Lionel Landwerlin6b217282016-09-23 01:04:25 +0300196 if (max_cs_threads > device->info.max_cs_threads)
197 device->info.max_cs_threads = max_cs_threads;
Lionel Landwerlin09394ee2016-09-07 17:19:35 +0100198 }
199
Jason Ekstranda71e6142015-10-19 22:06:59 -0700200 brw_process_intel_debug_variable();
201
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300202 device->compiler = brw_compiler_create(NULL, &device->info);
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700203 if (device->compiler == NULL) {
204 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
205 goto fail;
206 }
Jason Ekstranda71e6142015-10-19 22:06:59 -0700207 device->compiler->shader_debug_log = compiler_debug_log;
208 device->compiler->shader_perf_log = compiler_perf_log;
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700209
Emil Velikovace54032016-05-28 20:03:34 +0100210 result = anv_init_wsi(device);
Emil Velikova1cf4942016-11-24 20:30:43 +0000211 if (result != VK_SUCCESS) {
212 ralloc_free(device->compiler);
213 goto fail;
214 }
Jason Ekstrandeb6baa32016-05-15 22:21:24 -0700215
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300216 isl_device_init(&device->isl_dev, &device->info, swizzled);
Chad Versaceaf392912015-11-13 10:12:51 -0800217
Emil Velikov3af81712016-11-24 20:30:42 +0000218 close(fd);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700219 return VK_SUCCESS;
Chad Versace477383e2015-11-13 10:12:18 -0800220
Chad Versace8cda3e92015-07-09 16:31:39 -0700221fail:
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700222 close(fd);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700223 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700224}
225
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700226static void
227anv_physical_device_finish(struct anv_physical_device *device)
228{
Jason Ekstrandeb6baa32016-05-15 22:21:24 -0700229 anv_finish_wsi(device);
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700230 ralloc_free(device->compiler);
231}
232
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700233static const VkExtensionProperties global_extensions[] = {
234 {
Jason Ekstrandd6664872015-12-02 16:28:36 -0800235 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
Jason Ekstrandc688e4d2016-01-28 15:34:22 -0800236 .specVersion = 25,
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700237 },
Emil Velikov6dc169e2016-04-20 19:15:18 +0100238#ifdef VK_USE_PLATFORM_XCB_KHR
Jason Ekstrandd6664872015-12-02 16:28:36 -0800239 {
240 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
Emil Velikovf373a912016-11-09 18:10:46 +0000241 .specVersion = 6,
Jason Ekstrandd6664872015-12-02 16:28:36 -0800242 },
Emil Velikov6dc169e2016-04-20 19:15:18 +0100243#endif
Kevin Strasser71258e92016-08-12 14:17:20 -0700244#ifdef VK_USE_PLATFORM_XLIB_KHR
245 {
246 .extensionName = VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
Emil Velikovf373a912016-11-09 18:10:46 +0000247 .specVersion = 6,
Kevin Strasser71258e92016-08-12 14:17:20 -0700248 },
249#endif
Emil Velikovcbc48372016-04-20 19:01:00 +0100250#ifdef VK_USE_PLATFORM_WAYLAND_KHR
Jason Ekstrandd6664872015-12-02 16:28:36 -0800251 {
252 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
Emil Velikovf373a912016-11-09 18:10:46 +0000253 .specVersion = 5,
Jason Ekstrandd6664872015-12-02 16:28:36 -0800254 },
255#endif
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700256};
257
258static const VkExtensionProperties device_extensions[] = {
259 {
Jason Ekstrandd6664872015-12-02 16:28:36 -0800260 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
Emil Velikovf373a912016-11-09 18:10:46 +0000261 .specVersion = 68,
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700262 },
Ilia Mirkinfda1d012016-11-30 00:56:48 -0500263 {
264 .extensionName = VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
265 .specVersion = 1,
266 }
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700267};
268
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800269static void *
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300270default_alloc_func(void *pUserData, size_t size, size_t align,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800271 VkSystemAllocationScope allocationScope)
272{
273 return malloc(size);
274}
275
276static void *
277default_realloc_func(void *pUserData, void *pOriginal, size_t size,
278 size_t align, VkSystemAllocationScope allocationScope)
279{
280 return realloc(pOriginal, size);
281}
282
283static void
284default_free_func(void *pUserData, void *pMemory)
285{
286 free(pMemory);
287}
288
289static const VkAllocationCallbacks default_alloc = {
290 .pUserData = NULL,
291 .pfnAllocation = default_alloc_func,
292 .pfnReallocation = default_realloc_func,
293 .pfnFree = default_free_func,
294};
295
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700296VkResult anv_CreateInstance(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700297 const VkInstanceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800298 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700299 VkInstance* pInstance)
300{
301 struct anv_instance *instance;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700302
303 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
304
Jason Ekstrandc32273d2016-02-25 08:52:35 -0800305 uint32_t client_version;
306 if (pCreateInfo->pApplicationInfo &&
307 pCreateInfo->pApplicationInfo->apiVersion != 0) {
308 client_version = pCreateInfo->pApplicationInfo->apiVersion;
309 } else {
310 client_version = VK_MAKE_VERSION(1, 0, 0);
311 }
312
Jason Ekstranda19ceee2016-01-28 15:43:44 -0800313 if (VK_MAKE_VERSION(1, 0, 0) > client_version ||
Jason Ekstrand204d9372016-03-22 16:17:09 -0700314 client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {
Jason Ekstrandfd99f3d2016-02-02 13:15:18 -0800315 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
316 "Client requested version %d.%d.%d",
317 VK_VERSION_MAJOR(client_version),
318 VK_VERSION_MINOR(client_version),
319 VK_VERSION_PATCH(client_version));
Jason Ekstrand608b4112016-01-28 08:18:50 -0800320 }
Jason Ekstrande21ecb82015-10-12 18:25:19 -0700321
Jason Ekstrandaab95172016-01-14 07:41:45 -0800322 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700323 bool found = false;
324 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
325 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800326 global_extensions[j].extensionName) == 0) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700327 found = true;
328 break;
329 }
330 }
331 if (!found)
Chad Versacef9c948e2015-10-07 11:36:51 -0700332 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700333 }
334
Dave Airlie1ae6ece2016-10-14 13:31:35 +1000335 instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
Jason Ekstrand45d17fc2016-01-18 14:04:13 -0800336 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700337 if (!instance)
338 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
339
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700340 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800341
342 if (pAllocator)
343 instance->alloc = *pAllocator;
344 else
345 instance->alloc = default_alloc;
346
Philipp Zabelecd1d942016-02-16 22:55:33 +0100347 instance->apiVersion = client_version;
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800348 instance->physicalDeviceCount = -1;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700349
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -0700350 _mesa_locale_init();
351
Jason Ekstrand930598a2015-07-31 10:18:00 -0700352 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
353
Jason Ekstrand098209e2015-07-09 18:41:27 -0700354 *pInstance = anv_instance_to_handle(instance);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700355
356 return VK_SUCCESS;
357}
358
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700359void anv_DestroyInstance(
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800360 VkInstance _instance,
361 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700362{
Jason Ekstrand73f91872015-07-09 18:41:27 -0700363 ANV_FROM_HANDLE(anv_instance, instance, _instance);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700364
Chad Versace0ab926d2015-10-21 11:36:39 -0700365 if (instance->physicalDeviceCount > 0) {
366 /* We support at most one physical device. */
367 assert(instance->physicalDeviceCount == 1);
368 anv_physical_device_finish(&instance->physicalDevice);
369 }
370
Jason Ekstrand930598a2015-07-31 10:18:00 -0700371 VG(VALGRIND_DESTROY_MEMPOOL(instance));
372
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -0700373 _mesa_locale_fini();
374
Dave Airlie1ae6ece2016-10-14 13:31:35 +1000375 vk_free(&instance->alloc, instance);
Jason Ekstrande40bdce2015-07-31 10:13:24 -0700376}
377
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700378VkResult anv_EnumeratePhysicalDevices(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700379 VkInstance _instance,
380 uint32_t* pPhysicalDeviceCount,
381 VkPhysicalDevice* pPhysicalDevices)
382{
Jason Ekstrand73f91872015-07-09 18:41:27 -0700383 ANV_FROM_HANDLE(anv_instance, instance, _instance);
Chad Versacefa915b62015-07-09 15:38:58 -0700384 VkResult result;
385
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800386 if (instance->physicalDeviceCount < 0) {
Jason Ekstrandb93b5932016-05-24 12:06:35 -0700387 char path[20];
388 for (unsigned i = 0; i < 8; i++) {
389 snprintf(path, sizeof(path), "/dev/dri/renderD%d", 128 + i);
390 result = anv_physical_device_init(&instance->physicalDevice,
391 instance, path);
Jason Ekstranda5f8ff62016-11-01 17:51:56 -0700392 if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
Jason Ekstrandb93b5932016-05-24 12:06:35 -0700393 break;
394 }
395
Jason Ekstrandfed35862015-12-02 16:14:58 -0800396 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800397 instance->physicalDeviceCount = 0;
398 } else if (result == VK_SUCCESS) {
399 instance->physicalDeviceCount = 1;
400 } else {
Chad Versacefa915b62015-07-09 15:38:58 -0700401 return result;
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800402 }
Chad Versacefa915b62015-07-09 15:38:58 -0700403 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700404
Chad Versace5b75dff2015-07-09 15:51:06 -0700405 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
406 * otherwise it's an inout parameter.
407 *
408 * The Vulkan spec (git aaed022) says:
409 *
410 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
411 * that is initialized with the number of devices the application is
412 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
413 * an array of at least this many VkPhysicalDevice handles [...].
414 *
415 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
416 * overwrites the contents of the variable pointed to by
417 * pPhysicalDeviceCount with the number of physical devices in in the
418 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
419 * pPhysicalDeviceCount with the number of physical handles written to
420 * pPhysicalDevices.
421 */
422 if (!pPhysicalDevices) {
423 *pPhysicalDeviceCount = instance->physicalDeviceCount;
424 } else if (*pPhysicalDeviceCount >= 1) {
Jason Ekstrand098209e2015-07-09 18:41:27 -0700425 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
Chad Versace5b75dff2015-07-09 15:51:06 -0700426 *pPhysicalDeviceCount = 1;
Nicolas Kochfd27d5f2016-10-06 21:21:32 +0200427 } else if (*pPhysicalDeviceCount < instance->physicalDeviceCount) {
428 return VK_INCOMPLETE;
Chad Versace5b75dff2015-07-09 15:51:06 -0700429 } else {
430 *pPhysicalDeviceCount = 0;
431 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700432
433 return VK_SUCCESS;
434}
435
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800436void anv_GetPhysicalDeviceFeatures(
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700437 VkPhysicalDevice physicalDevice,
438 VkPhysicalDeviceFeatures* pFeatures)
439{
Kristian Høgsberg Kristensen4a2d17f2016-02-15 21:24:40 -0800440 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700441
442 *pFeatures = (VkPhysicalDeviceFeatures) {
Kristian Høgsberg Kristensendc5fdcd2016-02-01 11:54:40 -0800443 .robustBufferAccess = true,
Kristian Høgsberg Kristensen4a2d17f2016-02-15 21:24:40 -0800444 .fullDrawIndexUint32 = true,
Ilia Mirkina34f89c2016-11-27 14:41:42 -0500445 .imageCubeArray = true,
Jason Ekstrandf124f4a2016-07-14 18:01:29 -0700446 .independentBlend = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700447 .geometryShader = true,
Kenneth Graunke23a36c22016-09-25 15:33:03 -0700448 .tessellationShader = true,
Anuj Phogatc4cd0e82016-08-08 16:10:00 -0700449 .sampleRateShading = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800450 .dualSrcBlend = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700451 .logicOp = true,
Jason Ekstrand802f0022016-01-14 06:58:11 -0800452 .multiDrawIndirect = false,
Ilia Mirkine6847f22016-11-22 23:20:11 -0500453 .drawIndirectFirstInstance = true,
Jason Ekstrandeb6764c2016-06-14 08:40:49 -0700454 .depthClamp = true,
Ilia Mirkind2280a02016-11-22 23:03:12 -0500455 .depthBiasClamp = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700456 .fillModeNonSolid = true,
457 .depthBounds = false,
458 .wideLines = true,
459 .largePoints = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800460 .alphaToOne = true,
461 .multiViewport = true,
Lionel Landwerlin014bd4a2016-10-07 13:53:04 +0100462 .samplerAnisotropy = true,
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300463 .textureCompressionETC2 = pdevice->info.gen >= 8 ||
464 pdevice->info.is_baytrail,
465 .textureCompressionASTC_LDR = pdevice->info.gen >= 9, /* FINISHME CHV */
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700466 .textureCompressionBC = true,
Kristian Høgsberg Kristensen4a2d17f2016-02-15 21:24:40 -0800467 .occlusionQueryPrecise = true,
Kristian Høgsberg Kristensen9d8bae62016-02-29 10:55:39 -0800468 .pipelineStatisticsQuery = false,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800469 .fragmentStoresAndAtomics = true,
470 .shaderTessellationAndGeometryPointSize = true,
Ilia Mirkinc633f222016-11-27 15:45:54 -0500471 .shaderImageGatherExtended = true,
Ilia Mirkin76b97d52016-11-27 16:37:17 -0500472 .shaderStorageImageExtendedFormats = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700473 .shaderStorageImageMultisample = false,
Ilia Mirkinbe92b3f2016-11-27 17:39:52 -0500474 .shaderStorageImageReadWithoutFormat = false,
Ilia Mirkin1f13cb82016-12-30 00:39:30 -0500475 .shaderStorageImageWriteWithoutFormat = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700476 .shaderUniformBufferArrayDynamicIndexing = true,
Kristian Høgsberg Kristensen4a2d17f2016-02-15 21:24:40 -0800477 .shaderSampledImageArrayDynamicIndexing = true,
478 .shaderStorageBufferArrayDynamicIndexing = true,
479 .shaderStorageImageArrayDynamicIndexing = true,
Kenneth Graunkea4d7a5b2016-10-03 20:44:38 -0700480 .shaderClipDistance = true,
481 .shaderCullDistance = true,
Samuel Iglesias Gonsálvez0449c932016-11-10 10:06:48 +0100482 .shaderFloat64 = pdevice->info.gen >= 8,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700483 .shaderInt64 = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700484 .shaderInt16 = false,
Ilia Mirkinbe92b3f2016-11-27 17:39:52 -0500485 .shaderResourceMinLod = false,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800486 .variableMultisampleRate = false,
Jason Ekstrand802f0022016-01-14 06:58:11 -0800487 .inheritedQueries = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700488 };
Jason Ekstrand5ec4ecc2016-04-15 14:53:16 -0700489
490 /* We can't do image stores in vec4 shaders */
491 pFeatures->vertexPipelineStoresAndAtomics =
Jason Ekstrand93db8282016-04-15 16:39:17 -0700492 pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
493 pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700494}
495
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800496void anv_GetPhysicalDeviceProperties(
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700497 VkPhysicalDevice physicalDevice,
Chad Versaced48e71c2015-10-07 10:36:46 -0700498 VkPhysicalDeviceProperties* pProperties)
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700499{
Chad Versaced48e71c2015-10-07 10:36:46 -0700500 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300501 const struct gen_device_info *devinfo = &pdevice->info;
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700502
Kristian Høgsberg Kristensendae800d2016-01-09 00:50:04 -0800503 const float time_stamp_base = devinfo->gen >= 9 ? 83.333 : 80.0;
504
Nanley Cherya5748cb2016-07-06 11:13:48 -0700505 /* See assertions made when programming the buffer surface state. */
506 const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
507 (1ul << 30) : (1ul << 27);
508
Jason Ekstrandd6897452015-12-02 16:58:54 -0800509 VkSampleCountFlags sample_counts =
Chad Versace1c5d7b32016-01-20 16:04:28 -0800510 isl_device_get_sample_counts(&pdevice->isl_dev);
Jason Ekstrandd6897452015-12-02 16:58:54 -0800511
Chad Versaced48e71c2015-10-07 10:36:46 -0700512 VkPhysicalDeviceLimits limits = {
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700513 .maxImageDimension1D = (1 << 14),
514 .maxImageDimension2D = (1 << 14),
Nanley Chery181b1422016-03-05 15:17:00 -0800515 .maxImageDimension3D = (1 << 11),
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700516 .maxImageDimensionCube = (1 << 14),
Nanley Chery181b1422016-03-05 15:17:00 -0800517 .maxImageArrayLayers = (1 << 11),
Kenneth Graunke38a3a532016-01-26 23:09:45 -0800518 .maxTexelBufferElements = 128 * 1024 * 1024,
Nanley Cherya5748cb2016-07-06 11:13:48 -0700519 .maxUniformBufferRange = (1ul << 27),
520 .maxStorageBufferRange = max_raw_buffer_sz,
Jason Ekstrand5446bf32015-08-26 15:01:38 -0700521 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700522 .maxMemoryAllocationCount = UINT32_MAX,
Kristian Høgsberg Kristensen7b7a7c22016-01-20 14:36:52 -0800523 .maxSamplerAllocationCount = 64 * 1024,
Jason Ekstrande5db2092015-07-14 17:10:37 -0700524 .bufferImageGranularity = 64, /* A cache line */
Chad Versace033a37f2015-10-07 09:57:51 -0700525 .sparseAddressSpaceSize = 0,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700526 .maxBoundDescriptorSets = MAX_SETS,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700527 .maxPerStageDescriptorSamplers = 64,
528 .maxPerStageDescriptorUniformBuffers = 64,
529 .maxPerStageDescriptorStorageBuffers = 64,
530 .maxPerStageDescriptorSampledImages = 64,
531 .maxPerStageDescriptorStorageImages = 64,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800532 .maxPerStageDescriptorInputAttachments = 64,
533 .maxPerStageResources = 128,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700534 .maxDescriptorSetSamplers = 256,
535 .maxDescriptorSetUniformBuffers = 256,
Jason Ekstrand2349a9e2017-03-04 10:52:43 -0800536 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700537 .maxDescriptorSetStorageBuffers = 256,
Jason Ekstrand2349a9e2017-03-04 10:52:43 -0800538 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700539 .maxDescriptorSetSampledImages = 256,
540 .maxDescriptorSetStorageImages = 256,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800541 .maxDescriptorSetInputAttachments = 256,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700542 .maxVertexInputAttributes = 32,
Chad Versace033a37f2015-10-07 09:57:51 -0700543 .maxVertexInputBindings = 32,
Kenneth Graunke38a3a532016-01-26 23:09:45 -0800544 .maxVertexInputAttributeOffset = 2047,
545 .maxVertexInputBindingStride = 2048,
546 .maxVertexOutputComponents = 128,
Kenneth Graunkeebd88b52016-09-29 18:11:21 -0700547 .maxTessellationGenerationLevel = 64,
548 .maxTessellationPatchSize = 32,
549 .maxTessellationControlPerVertexInputComponents = 128,
550 .maxTessellationControlPerVertexOutputComponents = 128,
551 .maxTessellationControlPerPatchOutputComponents = 128,
552 .maxTessellationControlTotalOutputComponents = 2048,
553 .maxTessellationEvaluationInputComponents = 128,
554 .maxTessellationEvaluationOutputComponents = 128,
Kenneth Graunke38a3a532016-01-26 23:09:45 -0800555 .maxGeometryShaderInvocations = 32,
556 .maxGeometryInputComponents = 64,
557 .maxGeometryOutputComponents = 128,
558 .maxGeometryOutputVertices = 256,
559 .maxGeometryTotalOutputComponents = 1024,
560 .maxFragmentInputComponents = 128,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800561 .maxFragmentOutputAttachments = 8,
Dave Airlieeaf07682016-11-29 11:16:56 +1000562 .maxFragmentDualSrcAttachments = 1,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700563 .maxFragmentCombinedOutputResources = 8,
Kenneth Graunke38a3a532016-01-26 23:09:45 -0800564 .maxComputeSharedMemorySize = 32768,
565 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
Lionel Landwerlin6b217282016-09-23 01:04:25 +0300566 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700567 .maxComputeWorkGroupSize = {
Lionel Landwerlin6b217282016-09-23 01:04:25 +0300568 16 * devinfo->max_cs_threads,
569 16 * devinfo->max_cs_threads,
570 16 * devinfo->max_cs_threads,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700571 },
572 .subPixelPrecisionBits = 4 /* FIXME */,
573 .subTexelPrecisionBits = 4 /* FIXME */,
574 .mipmapPrecisionBits = 4 /* FIXME */,
575 .maxDrawIndexedIndexValue = UINT32_MAX,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800576 .maxDrawIndirectCount = UINT32_MAX,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700577 .maxSamplerLodBias = 16,
578 .maxSamplerAnisotropy = 16,
Jason Ekstranddaf68a92015-10-06 17:21:44 -0700579 .maxViewports = MAX_VIEWPORTS,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700580 .maxViewportDimensions = { (1 << 14), (1 << 14) },
Nanley Chery7ac08ad2016-05-17 15:28:01 -0700581 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700582 .viewportSubPixelBits = 13, /* We take a float? */
Jason Ekstrandf076d532016-01-01 09:26:06 -0800583 .minMemoryMapAlignment = 4096, /* A page */
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700584 .minTexelBufferOffsetAlignment = 1,
Francisco Jerez79d08ed2016-12-15 13:34:02 -0800585 .minUniformBufferOffsetAlignment = 16,
586 .minStorageBufferOffsetAlignment = 4,
Kenneth Graunke38a3a532016-01-26 23:09:45 -0800587 .minTexelOffset = -8,
588 .maxTexelOffset = 7,
Ilia Mirkin7a8def82016-11-27 21:05:36 -0500589 .minTexelGatherOffset = -32,
590 .maxTexelGatherOffset = 31,
Anuj Phogat0bf531a2016-07-28 17:37:20 -0700591 .minInterpolationOffset = -0.5,
592 .maxInterpolationOffset = 0.4375,
593 .subPixelInterpolationOffsetBits = 4,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700594 .maxFramebufferWidth = (1 << 14),
595 .maxFramebufferHeight = (1 << 14),
Ilia Mirkine2c669a2016-11-28 19:49:51 -0500596 .maxFramebufferLayers = (1 << 11),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800597 .framebufferColorSampleCounts = sample_counts,
598 .framebufferDepthSampleCounts = sample_counts,
599 .framebufferStencilSampleCounts = sample_counts,
600 .framebufferNoAttachmentsSampleCounts = sample_counts,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700601 .maxColorAttachments = MAX_RTS,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800602 .sampledImageColorSampleCounts = sample_counts,
603 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
604 .sampledImageDepthSampleCounts = sample_counts,
605 .sampledImageStencilSampleCounts = sample_counts,
606 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700607 .maxSampleMaskWords = 1,
Jason Ekstrand802f0022016-01-14 06:58:11 -0800608 .timestampComputeAndGraphics = false,
Philipp Zabel0408d502016-10-06 01:48:04 +0200609 .timestampPeriod = time_stamp_base,
Kenneth Graunkea4d7a5b2016-10-03 20:44:38 -0700610 .maxClipDistances = 8,
611 .maxCullDistances = 8,
612 .maxCombinedClipAndCullDistances = 8,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800613 .discreteQueuePriorities = 1,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700614 .pointSizeRange = { 0.125, 255.875 },
615 .lineWidthRange = { 0.0, 7.9921875 },
616 .pointSizeGranularity = (1.0 / 8.0),
617 .lineWidthGranularity = (1.0 / 128.0),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800618 .strictLines = false, /* FINISHME */
Chad Versace8cc6f052016-01-26 10:56:06 -0800619 .standardSampleLocations = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800620 .optimalBufferCopyOffsetAlignment = 128,
621 .optimalBufferCopyRowPitchAlignment = 128,
622 .nonCoherentAtomSize = 64,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700623 };
624
Jason Ekstrand977a4692015-07-09 15:53:03 -0700625 *pProperties = (VkPhysicalDeviceProperties) {
Jason Ekstrand20417b22016-03-22 16:21:21 -0700626 .apiVersion = VK_MAKE_VERSION(1, 0, 5),
Jason Ekstrand977a4692015-07-09 15:53:03 -0700627 .driverVersion = 1,
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800628 .vendorID = 0x8086,
629 .deviceID = pdevice->chipset_id,
Jason Ekstrand977a4692015-07-09 15:53:03 -0700630 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
Chad Versaced48e71c2015-10-07 10:36:46 -0700631 .limits = limits,
632 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
Jason Ekstrand977a4692015-07-09 15:53:03 -0700633 };
634
635 strcpy(pProperties->deviceName, pdevice->name);
Emil Velikovde138e92016-11-24 20:30:38 +0000636 memcpy(pProperties->pipelineCacheUUID, pdevice->uuid, VK_UUID_SIZE);
Jason Ekstrand977a4692015-07-09 15:53:03 -0700637}
638
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800639void anv_GetPhysicalDeviceQueueFamilyProperties(
Jason Ekstrand1f907012015-07-09 16:11:24 -0700640 VkPhysicalDevice physicalDevice,
Jason Ekstranda6eba402015-10-05 21:17:12 -0700641 uint32_t* pCount,
642 VkQueueFamilyProperties* pQueueFamilyProperties)
Jason Ekstrand1f907012015-07-09 16:11:24 -0700643{
Jason Ekstranda6eba402015-10-05 21:17:12 -0700644 if (pQueueFamilyProperties == NULL) {
645 *pCount = 1;
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800646 return;
Jason Ekstranda6eba402015-10-05 21:17:12 -0700647 }
Jason Ekstrand1f907012015-07-09 16:11:24 -0700648
Chad Versaced6545f22016-12-27 10:25:58 -0800649 /* The spec implicitly allows the incoming count to be 0. From the Vulkan
650 * 1.0.38 spec, Section 4.1 Physical Devices:
651 *
652 * If the value referenced by pQueueFamilyPropertyCount is not 0 [then
653 * do stuff].
654 */
655 if (*pCount == 0)
656 return;
Jason Ekstrand1f907012015-07-09 16:11:24 -0700657
Jason Ekstranda6eba402015-10-05 21:17:12 -0700658 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
Jason Ekstrand1f907012015-07-09 16:11:24 -0700659 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
660 VK_QUEUE_COMPUTE_BIT |
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800661 VK_QUEUE_TRANSFER_BIT,
Jason Ekstrand1f907012015-07-09 16:11:24 -0700662 .queueCount = 1,
Kristian Høgsberg Kristensen925ad842016-01-09 00:51:14 -0800663 .timestampValidBits = 36, /* XXX: Real value here */
Jason Ekstrand74c4c4a2015-12-02 16:20:40 -0800664 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
Jason Ekstrand1f907012015-07-09 16:11:24 -0700665 };
Damien Grassart75252822016-12-25 01:00:58 +0100666
667 *pCount = 1;
Jason Ekstrand1f907012015-07-09 16:11:24 -0700668}
669
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800670void anv_GetPhysicalDeviceMemoryProperties(
Chad Versacedf2a0132015-07-09 19:49:19 -0700671 VkPhysicalDevice physicalDevice,
672 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
673{
674 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700675 VkDeviceSize heap_size;
Chad Versacedf2a0132015-07-09 19:49:19 -0700676
677 /* Reserve some wiggle room for the driver by exposing only 75% of the
678 * aperture to the heap.
679 */
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700680 heap_size = 3 * physical_device->aperture_size / 4;
Chad Versacedf2a0132015-07-09 19:49:19 -0700681
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300682 if (physical_device->info.has_llc) {
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800683 /* Big core GPUs share LLC with the CPU and thus one memory type can be
684 * both cached and coherent at the same time.
685 */
686 pMemoryProperties->memoryTypeCount = 1;
687 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
688 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
689 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
690 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
691 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800692 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800693 };
694 } else {
695 /* The spec requires that we expose a host-visible, coherent memory
696 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
697 * to give the application a choice between cached, but not coherent and
698 * coherent but uncached (WC though).
699 */
700 pMemoryProperties->memoryTypeCount = 2;
701 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
702 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
703 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
704 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800705 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800706 };
707 pMemoryProperties->memoryTypes[1] = (VkMemoryType) {
708 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
709 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
710 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800711 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800712 };
713 }
Chad Versacedf2a0132015-07-09 19:49:19 -0700714
715 pMemoryProperties->memoryHeapCount = 1;
716 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
717 .size = heap_size,
Jason Ekstrande6ab06a2015-12-02 10:39:15 -0800718 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
Chad Versacedf2a0132015-07-09 19:49:19 -0700719 };
Chad Versacedf2a0132015-07-09 19:49:19 -0700720}
721
Jason Ekstrande7acdda2015-07-07 18:51:53 -0700722PFN_vkVoidFunction anv_GetInstanceProcAddr(
723 VkInstance instance,
724 const char* pName)
725{
Jason Ekstrand6d557ae2016-10-07 15:47:45 -0700726 return anv_lookup_entrypoint(NULL, pName);
Jason Ekstrande7acdda2015-07-07 18:51:53 -0700727}
728
Emil Velikov40e4fff2016-07-28 14:40:08 +0100729/* With version 1+ of the loader interface the ICD should expose
730 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
Jason Ekstrand3a2b23a2016-02-11 21:18:02 -0800731 */
Emil Velikov40e4fff2016-07-28 14:40:08 +0100732PUBLIC
Jason Ekstrand3a2b23a2016-02-11 21:18:02 -0800733VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
734 VkInstance instance,
735 const char* pName);
736
Emil Velikov40e4fff2016-07-28 14:40:08 +0100737PUBLIC
Jason Ekstrand3a2b23a2016-02-11 21:18:02 -0800738VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
739 VkInstance instance,
740 const char* pName)
741{
742 return anv_GetInstanceProcAddr(instance, pName);
743}
744
Jason Ekstrande7acdda2015-07-07 18:51:53 -0700745PFN_vkVoidFunction anv_GetDeviceProcAddr(
Jason Ekstrand6d557ae2016-10-07 15:47:45 -0700746 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700747 const char* pName)
748{
Jason Ekstrand6d557ae2016-10-07 15:47:45 -0700749 ANV_FROM_HANDLE(anv_device, device, _device);
750 return anv_lookup_entrypoint(&device->info, pName);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700751}
752
Gwan-gyeong Munca470692016-11-25 23:34:43 +0900753static void
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700754anv_queue_init(struct anv_device *device, struct anv_queue *queue)
755{
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700756 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700757 queue->device = device;
758 queue->pool = &device->surface_state_pool;
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700759}
760
761static void
762anv_queue_finish(struct anv_queue *queue)
763{
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700764}
765
Kristian Høgsberg77359202015-12-01 15:37:12 -0800766static struct anv_state
767anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
768{
769 struct anv_state state;
770
771 state = anv_state_pool_alloc(pool, size, align);
772 memcpy(state.map, p, size);
773
774 if (!pool->block_pool->device->info.has_llc)
775 anv_state_clflush(state);
776
777 return state;
778}
779
Jason Ekstrandd49298c2016-01-18 12:16:31 -0800780struct gen8_border_color {
781 union {
782 float float32[4];
783 uint32_t uint32[4];
784 };
785 /* Pad out to 64 bytes */
786 uint32_t _pad[12];
787};
788
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700789static void
790anv_device_init_border_colors(struct anv_device *device)
791{
Jason Ekstrandd49298c2016-01-18 12:16:31 -0800792 static const struct gen8_border_color border_colors[] = {
Jason Ekstrandbd4cde72015-10-06 10:07:47 -0700793 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
794 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
795 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
796 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
797 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
798 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700799 };
800
Kristian Høgsberg77359202015-12-01 15:37:12 -0800801 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
Jason Ekstrandd49298c2016-01-18 12:16:31 -0800802 sizeof(border_colors), 64,
803 border_colors);
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700804}
805
Kristian Høgsberg Kristensen6cdada02016-02-05 16:11:12 -0800806VkResult
807anv_device_submit_simple_batch(struct anv_device *device,
808 struct anv_batch *batch)
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800809{
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800810 struct drm_i915_gem_execbuffer2 execbuf;
811 struct drm_i915_gem_exec_object2 exec2_objects[1];
Jason Ekstrand07798c92016-10-31 20:36:26 -0700812 struct anv_bo bo, *exec_bos[1];
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800813 VkResult result = VK_SUCCESS;
814 uint32_t size;
815 int64_t timeout;
816 int ret;
817
Kristian Høgsberg Kristensen6cdada02016-02-05 16:11:12 -0800818 /* Kernel driver requires 8 byte aligned batch length */
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800819 size = align_u32(batch->next - batch->start, 8);
Jason Ekstrandecfb0742016-03-18 13:06:08 -0700820 result = anv_bo_pool_alloc(&device->batch_bo_pool, &bo, size);
Jason Ekstrandea930412016-02-11 18:41:04 -0800821 if (result != VK_SUCCESS)
822 return result;
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800823
Jason Ekstrandea930412016-02-11 18:41:04 -0800824 memcpy(bo.map, batch->start, size);
Jason Ekstrand699f2122016-02-12 11:00:42 -0800825 if (!device->info.has_llc)
826 anv_clflush_range(bo.map, size);
Jason Ekstrandea930412016-02-11 18:41:04 -0800827
Jason Ekstrand07798c92016-10-31 20:36:26 -0700828 exec_bos[0] = &bo;
Jason Ekstrandea930412016-02-11 18:41:04 -0800829 exec2_objects[0].handle = bo.gem_handle;
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800830 exec2_objects[0].relocation_count = 0;
831 exec2_objects[0].relocs_ptr = 0;
832 exec2_objects[0].alignment = 0;
Jason Ekstrandea930412016-02-11 18:41:04 -0800833 exec2_objects[0].offset = bo.offset;
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800834 exec2_objects[0].flags = 0;
835 exec2_objects[0].rsvd1 = 0;
836 exec2_objects[0].rsvd2 = 0;
837
838 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
839 execbuf.buffer_count = 1;
Jason Ekstrandea930412016-02-11 18:41:04 -0800840 execbuf.batch_start_offset = 0;
Kristian Høgsberg Kristensen6cdada02016-02-05 16:11:12 -0800841 execbuf.batch_len = size;
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800842 execbuf.cliprects_ptr = 0;
843 execbuf.num_cliprects = 0;
844 execbuf.DR1 = 0;
845 execbuf.DR4 = 0;
846
847 execbuf.flags =
848 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
849 execbuf.rsvd1 = device->context_id;
850 execbuf.rsvd2 = 0;
851
Jason Ekstrand07798c92016-10-31 20:36:26 -0700852 result = anv_device_execbuf(device, &execbuf, exec_bos);
853 if (result != VK_SUCCESS)
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800854 goto fail;
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800855
856 timeout = INT64_MAX;
Jason Ekstrandea930412016-02-11 18:41:04 -0800857 ret = anv_gem_wait(device, bo.gem_handle, &timeout);
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800858 if (ret != 0) {
859 /* We don't know the real error. */
Jason Ekstrandc41ec162016-10-31 16:33:43 -0700860 result = vk_errorf(VK_ERROR_DEVICE_LOST, "execbuf2 failed: %m");
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800861 goto fail;
862 }
863
864 fail:
Jason Ekstrandea930412016-02-11 18:41:04 -0800865 anv_bo_pool_free(&device->batch_bo_pool, &bo);
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -0800866
867 return result;
868}
869
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700870VkResult anv_CreateDevice(
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700871 VkPhysicalDevice physicalDevice,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700872 const VkDeviceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800873 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700874 VkDevice* pDevice)
875{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700876 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800877 VkResult result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700878 struct anv_device *device;
879
880 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
881
Jason Ekstrandaab95172016-01-14 07:41:45 -0800882 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700883 bool found = false;
884 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
885 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800886 device_extensions[j].extensionName) == 0) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700887 found = true;
888 break;
889 }
890 }
891 if (!found)
Chad Versacef9c948e2015-10-07 11:36:51 -0700892 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700893 }
894
Dave Airlie1ae6ece2016-10-14 13:31:35 +1000895 device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800896 sizeof(*device), 8,
Jason Ekstrand45d17fc2016-01-18 14:04:13 -0800897 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700898 if (!device)
899 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
900
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700901 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700902 device->instance = physical_device->instance;
Kristian Høgsberg Kristensen39a120a2016-02-10 09:43:03 -0800903 device->chipset_id = physical_device->chipset_id;
Chad Versace8cda3e92015-07-09 16:31:39 -0700904
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800905 if (pAllocator)
906 device->alloc = *pAllocator;
907 else
908 device->alloc = physical_device->instance->alloc;
909
Chad Versace8cda3e92015-07-09 16:31:39 -0700910 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700911 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800912 if (device->fd == -1) {
913 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700914 goto fail_device;
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800915 }
Chad Versace477383e2015-11-13 10:12:18 -0800916
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700917 device->context_id = anv_gem_create_context(device);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800918 if (device->context_id == -1) {
919 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700920 goto fail_fd;
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800921 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700922
Lionel Landwerlinbc245902016-09-22 14:58:11 +0300923 device->info = physical_device->info;
Jason Ekstrand9c84b6c2015-12-28 13:26:49 -0800924 device->isl_dev = physical_device->isl_dev;
925
Jason Ekstrand869e3932016-03-18 16:32:46 -0700926 /* On Broadwell and later, we can use batch chaining to more efficiently
927 * implement growing command buffers. Prior to Haswell, the kernel
928 * command parser gets in the way and we have to fall back to growing
929 * the batch.
930 */
931 device->can_chain_batches = device->info.gen >= 8;
932
Jason Ekstrandc29ffea2016-05-14 14:52:36 -0700933 device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
934 pCreateInfo->pEnabledFeatures->robustBufferAccess;
935
Jason Ekstranda788e7c2015-09-17 18:23:21 -0700936 pthread_mutex_init(&device->mutex, NULL);
937
Jason Ekstrand843775b2016-11-02 09:11:11 -0700938 pthread_condattr_t condattr;
939 pthread_condattr_init(&condattr);
940 pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
941 pthread_cond_init(&device->queue_submit, NULL);
942 pthread_condattr_destroy(&condattr);
943
Jason Ekstrandecfb0742016-03-18 13:06:08 -0700944 anv_bo_pool_init(&device->batch_bo_pool, device);
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700945
Jordan Justenc7f6e422016-01-08 12:15:29 -0800946 anv_block_pool_init(&device->dynamic_state_block_pool, device, 16384);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700947
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700948 anv_state_pool_init(&device->dynamic_state_pool,
949 &device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700950
Samuel Iglesias Gonsálvezff0dd672017-01-10 12:44:32 +0100951 anv_block_pool_init(&device->instruction_block_pool, device, 1024 * 1024);
Jason Ekstrand68997182016-08-24 23:48:32 -0700952 anv_state_pool_init(&device->instruction_state_pool,
953 &device->instruction_block_pool);
Kristian Høgsberg Kristensen30521fb2016-01-05 12:00:54 -0800954
Jason Ekstrand0e944462015-09-22 16:36:00 -0700955 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700956
957 anv_state_pool_init(&device->surface_state_pool,
958 &device->surface_state_block_pool);
959
Jason Ekstrand3a3d79b2015-11-10 16:42:34 -0800960 anv_bo_init_new(&device->workaround_bo, device, 1024);
961
Jason Ekstrandc2f2c8e2016-06-16 15:26:54 -0700962 anv_scratch_pool_init(device, &device->scratch_pool);
Kristian Høgsberg Kristensen9b9f9732015-06-19 15:41:30 -0700963
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700964 anv_queue_init(device, &device->queue);
965
Kristian Høgsberg Kristensen6cdada02016-02-05 16:11:12 -0800966 switch (device->info.gen) {
967 case 7:
968 if (!device->info.is_haswell)
969 result = gen7_init_device_state(device);
970 else
971 result = gen75_init_device_state(device);
972 break;
973 case 8:
974 result = gen8_init_device_state(device);
975 break;
976 case 9:
977 result = gen9_init_device_state(device);
978 break;
Kristian Høgsberg Kristensen5d72d7b2016-02-14 13:20:06 -0800979 default:
980 /* Shouldn't get here as we don't create physical devices for any other
981 * gens. */
982 unreachable("unhandled gen");
Kristian Høgsberg Kristensen6cdada02016-02-05 16:11:12 -0800983 }
984 if (result != VK_SUCCESS)
985 goto fail_fd;
986
Jason Ekstrand8f780af2016-08-22 21:37:28 -0700987 anv_device_init_blorp(device);
988
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700989 anv_device_init_border_colors(device);
990
Jason Ekstrand098209e2015-07-09 18:41:27 -0700991 *pDevice = anv_device_to_handle(device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700992
993 return VK_SUCCESS;
994
995 fail_fd:
996 close(device->fd);
997 fail_device:
Dave Airlie1ae6ece2016-10-14 13:31:35 +1000998 vk_free(&device->alloc, device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700999
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -08001000 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001001}
1002
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001003void anv_DestroyDevice(
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001004 VkDevice _device,
1005 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001006{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001007 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001008
Jason Ekstrand8f780af2016-08-22 21:37:28 -07001009 anv_device_finish_blorp(device);
1010
Gwan-gyeong Munb1786522016-11-25 23:34:46 +09001011 anv_queue_finish(&device->queue);
1012
Jason Ekstrand38f5eef2015-06-09 11:41:31 -07001013#ifdef HAVE_VALGRIND
1014 /* We only need to free these to prevent valgrind errors. The backing
1015 * BO will go away in a couple of lines so we don't actually leak.
1016 */
Jason Ekstrand522ab832015-07-08 11:44:52 -07001017 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
Jason Ekstrand38f5eef2015-06-09 11:41:31 -07001018#endif
1019
Gwan-gyeong Munb1786522016-11-25 23:34:46 +09001020 anv_scratch_pool_finish(device, &device->scratch_pool);
1021
Jason Ekstrand3a3d79b2015-11-10 16:42:34 -08001022 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
1023 anv_gem_close(device, device->workaround_bo.gem_handle);
1024
Jason Ekstrand1920ef92015-07-31 10:30:57 -07001025 anv_state_pool_finish(&device->surface_state_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001026 anv_block_pool_finish(&device->surface_state_block_pool);
Gwan-gyeong Munb1786522016-11-25 23:34:46 +09001027 anv_state_pool_finish(&device->instruction_state_pool);
1028 anv_block_pool_finish(&device->instruction_block_pool);
1029 anv_state_pool_finish(&device->dynamic_state_pool);
1030 anv_block_pool_finish(&device->dynamic_state_block_pool);
1031
1032 anv_bo_pool_finish(&device->batch_bo_pool);
1033
1034 pthread_cond_destroy(&device->queue_submit);
1035 pthread_mutex_destroy(&device->mutex);
1036
1037 anv_gem_destroy_context(device, device->context_id);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001038
1039 close(device->fd);
1040
Dave Airlie1ae6ece2016-10-14 13:31:35 +10001041 vk_free(&device->alloc, device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001042}
1043
Jason Ekstrand8ba684c2015-10-06 09:25:03 -07001044VkResult anv_EnumerateInstanceExtensionProperties(
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001045 const char* pLayerName,
Jason Ekstrandfe644722015-11-30 16:28:36 -08001046 uint32_t* pPropertyCount,
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -07001047 VkExtensionProperties* pProperties)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001048{
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001049 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -08001050 *pPropertyCount = ARRAY_SIZE(global_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001051 return VK_SUCCESS;
1052 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001053
Emil Velikov5cc07d82016-10-06 14:12:27 +01001054 *pPropertyCount = MIN2(*pPropertyCount, ARRAY_SIZE(global_extensions));
1055 typed_memcpy(pProperties, global_extensions, *pPropertyCount);
Kristian Høgsberg783e6212015-05-17 19:22:52 -07001056
Emil Velikov5cc07d82016-10-06 14:12:27 +01001057 if (*pPropertyCount < ARRAY_SIZE(global_extensions))
1058 return VK_INCOMPLETE;
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -07001059
1060 return VK_SUCCESS;
1061}
1062
Jason Ekstrand8ba684c2015-10-06 09:25:03 -07001063VkResult anv_EnumerateDeviceExtensionProperties(
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -07001064 VkPhysicalDevice physicalDevice,
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001065 const char* pLayerName,
Jason Ekstrandfe644722015-11-30 16:28:36 -08001066 uint32_t* pPropertyCount,
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -07001067 VkExtensionProperties* pProperties)
1068{
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001069 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -08001070 *pPropertyCount = ARRAY_SIZE(device_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001071 return VK_SUCCESS;
1072 }
1073
Emil Velikov5cc07d82016-10-06 14:12:27 +01001074 *pPropertyCount = MIN2(*pPropertyCount, ARRAY_SIZE(device_extensions));
1075 typed_memcpy(pProperties, device_extensions, *pPropertyCount);
Jason Ekstrand9a7600c2015-09-01 16:44:42 -07001076
Emil Velikov5cc07d82016-10-06 14:12:27 +01001077 if (*pPropertyCount < ARRAY_SIZE(device_extensions))
1078 return VK_INCOMPLETE;
Jason Ekstrand9a7600c2015-09-01 16:44:42 -07001079
1080 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001081}
1082
Jason Ekstrand8ba684c2015-10-06 09:25:03 -07001083VkResult anv_EnumerateInstanceLayerProperties(
Jason Ekstrandfe644722015-11-30 16:28:36 -08001084 uint32_t* pPropertyCount,
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001085 VkLayerProperties* pProperties)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001086{
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001087 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -08001088 *pPropertyCount = 0;
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001089 return VK_SUCCESS;
1090 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001091
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001092 /* None supported at this time */
Chad Versacef9c948e2015-10-07 11:36:51 -07001093 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001094}
1095
Jason Ekstrand8ba684c2015-10-06 09:25:03 -07001096VkResult anv_EnumerateDeviceLayerProperties(
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001097 VkPhysicalDevice physicalDevice,
Jason Ekstrandfe644722015-11-30 16:28:36 -08001098 uint32_t* pPropertyCount,
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001099 VkLayerProperties* pProperties)
1100{
1101 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -08001102 *pPropertyCount = 0;
Jason Ekstrand02db21a2015-07-14 16:11:21 -07001103 return VK_SUCCESS;
1104 }
1105
1106 /* None supported at this time */
Chad Versacef9c948e2015-10-07 11:36:51 -07001107 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001108}
1109
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001110void anv_GetDeviceQueue(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001111 VkDevice _device,
1112 uint32_t queueNodeIndex,
1113 uint32_t queueIndex,
1114 VkQueue* pQueue)
1115{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001116 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001117
Jason Ekstrand66b00d52015-06-09 12:28:58 -07001118 assert(queueIndex == 0);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001119
Jason Ekstrand098209e2015-07-09 18:41:27 -07001120 *pQueue = anv_queue_to_handle(&device->queue);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001121}
1122
Jason Ekstrand07798c92016-10-31 20:36:26 -07001123VkResult
1124anv_device_execbuf(struct anv_device *device,
1125 struct drm_i915_gem_execbuffer2 *execbuf,
1126 struct anv_bo **execbuf_bos)
1127{
1128 int ret = anv_gem_execbuffer(device, execbuf);
1129 if (ret != 0) {
1130 /* We don't know the real error. */
1131 return vk_errorf(VK_ERROR_DEVICE_LOST, "execbuf2 failed: %m");
1132 }
1133
Jason Ekstrand18266242016-11-09 18:45:21 -08001134 struct drm_i915_gem_exec_object2 *objects =
1135 (void *)(uintptr_t)execbuf->buffers_ptr;
Jason Ekstrand07798c92016-10-31 20:36:26 -07001136 for (uint32_t k = 0; k < execbuf->buffer_count; k++)
1137 execbuf_bos[k]->offset = objects[k].offset;
1138
1139 return VK_SUCCESS;
1140}
1141
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001142VkResult anv_QueueSubmit(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001143 VkQueue _queue,
Jason Ekstrand4e904a02015-12-02 17:18:41 -08001144 uint32_t submitCount,
1145 const VkSubmitInfo* pSubmits,
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001146 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001147{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001148 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1149 ANV_FROM_HANDLE(anv_fence, fence, _fence);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001150 struct anv_device *device = queue->device;
Jason Ekstrand07798c92016-10-31 20:36:26 -07001151 VkResult result = VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001152
Kristian Høgsberg Kristensenb3a29f22016-03-08 15:31:47 -08001153 /* We lock around QueueSubmit for three main reasons:
Jason Ekstrand8b61c572016-11-05 19:47:33 -07001154 *
1155 * 1) When a block pool is resized, we create a new gem handle with a
1156 * different size and, in the case of surface states, possibly a
1157 * different center offset but we re-use the same anv_bo struct when
1158 * we do so. If this happens in the middle of setting up an execbuf,
1159 * we could end up with our list of BOs out of sync with our list of
1160 * gem handles.
1161 *
1162 * 2) The algorithm we use for building the list of unique buffers isn't
1163 * thread-safe. While the client is supposed to syncronize around
1164 * QueueSubmit, this would be extremely difficult to debug if it ever
1165 * came up in the wild due to a broken app. It's better to play it
1166 * safe and just lock around QueueSubmit.
1167 *
Kristian Høgsberg Kristensenb3a29f22016-03-08 15:31:47 -08001168 * 3) The anv_cmd_buffer_execbuf function may perform relocations in
1169 * userspace. Due to the fact that the surface state buffer is shared
1170 * between batches, we can't afford to have that happen from multiple
1171 * threads at the same time. Even though the user is supposed to
1172 * ensure this doesn't happen, we play it safe as in (2) above.
1173 *
Jason Ekstrand8b61c572016-11-05 19:47:33 -07001174 * Since the only other things that ever take the device lock such as block
1175 * pool resize only rarely happen, this will almost never be contended so
1176 * taking a lock isn't really an expensive operation in this case.
1177 */
1178 pthread_mutex_lock(&device->mutex);
1179
Jason Ekstrand4e904a02015-12-02 17:18:41 -08001180 for (uint32_t i = 0; i < submitCount; i++) {
1181 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1182 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
1183 pSubmits[i].pCommandBuffers[j]);
1184 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001185
Jason Ekstranddb9f4b22016-11-02 10:33:54 -07001186 result = anv_cmd_buffer_execbuf(device, cmd_buffer);
Jason Ekstrand07798c92016-10-31 20:36:26 -07001187 if (result != VK_SUCCESS)
Jason Ekstrand8b61c572016-11-05 19:47:33 -07001188 goto out;
Jason Ekstrand4e904a02015-12-02 17:18:41 -08001189 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001190 }
1191
Kristian Høgsberg Kristensenb0c30b72016-02-12 15:08:09 -08001192 if (fence) {
Jason Ekstrand07798c92016-10-31 20:36:26 -07001193 struct anv_bo *fence_bo = &fence->bo;
1194 result = anv_device_execbuf(device, &fence->execbuf, &fence_bo);
1195 if (result != VK_SUCCESS)
Jason Ekstrand8b61c572016-11-05 19:47:33 -07001196 goto out;
Jason Ekstrand843775b2016-11-02 09:11:11 -07001197
1198 /* Update the fence and wake up any waiters */
1199 assert(fence->state == ANV_FENCE_STATE_RESET);
1200 fence->state = ANV_FENCE_STATE_SUBMITTED;
1201 pthread_cond_broadcast(&device->queue_submit);
Kristian Høgsberg Kristensenb0c30b72016-02-12 15:08:09 -08001202 }
1203
Jason Ekstrand8b61c572016-11-05 19:47:33 -07001204out:
1205 pthread_mutex_unlock(&device->mutex);
1206
1207 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001208}
1209
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001210VkResult anv_QueueWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001211 VkQueue _queue)
1212{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001213 ANV_FROM_HANDLE(anv_queue, queue, _queue);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001214
Jason Ekstrand4c9dec82016-10-07 15:41:17 -07001215 return anv_DeviceWaitIdle(anv_device_to_handle(queue->device));
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001216}
1217
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001218VkResult anv_DeviceWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001219 VkDevice _device)
1220{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001221 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001222 struct anv_batch batch;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001223
Kristian Høgsberg Kristensenc9c33442016-02-05 15:23:38 -08001224 uint32_t cmds[8];
1225 batch.start = batch.next = cmds;
1226 batch.end = (void *) cmds + sizeof(cmds);
1227
Jason Ekstrand50018522016-04-18 17:03:00 -07001228 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1229 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001230
Kristian Høgsberg Kristensen6cdada02016-02-05 16:11:12 -08001231 return anv_device_submit_simple_batch(device, &batch);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001232}
1233
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001234VkResult
1235anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
1236{
Jason Ekstrand6283b6d2016-11-01 13:09:36 -07001237 uint32_t gem_handle = anv_gem_create(device, size);
1238 if (!gem_handle)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001239 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1240
Jason Ekstrand6283b6d2016-11-01 13:09:36 -07001241 anv_bo_init(bo, gem_handle, size);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001242
1243 return VK_SUCCESS;
1244}
1245
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001246VkResult anv_AllocateMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001247 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001248 const VkMemoryAllocateInfo* pAllocateInfo,
1249 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001250 VkDeviceMemory* pMem)
1251{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001252 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001253 struct anv_device_memory *mem;
1254 VkResult result;
1255
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001256 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001257
Chad Versace72ffe832016-11-17 12:45:26 -08001258 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
1259 assert(pAllocateInfo->allocationSize > 0);
Jason Ekstrandb1325402015-12-17 11:00:38 -08001260
Chad Versacef9c948e2015-10-07 11:36:51 -07001261 /* We support exactly one memory heap. */
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -08001262 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1263 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
Chad Versacef43a3042015-07-09 19:59:44 -07001264
1265 /* FINISHME: Fail if allocation request exceeds heap size. */
1266
Dave Airlie1ae6ece2016-10-14 13:31:35 +10001267 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001268 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001269 if (mem == NULL)
1270 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1271
Jason Ekstrand6b0b5722016-01-02 07:52:22 -08001272 /* The kernel is going to give us whole pages anyway */
1273 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1274
1275 result = anv_bo_init_new(&mem->bo, device, alloc_size);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001276 if (result != VK_SUCCESS)
1277 goto fail;
1278
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -08001279 mem->type_index = pAllocateInfo->memoryTypeIndex;
1280
Jason Ekstrandb1217ea2016-11-07 17:25:07 -08001281 mem->map = NULL;
1282 mem->map_size = 0;
1283
Jason Ekstrand098209e2015-07-09 18:41:27 -07001284 *pMem = anv_device_memory_to_handle(mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001285
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001286 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001287
1288 fail:
Dave Airlie1ae6ece2016-10-14 13:31:35 +10001289 vk_free2(&device->alloc, pAllocator, mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001290
1291 return result;
1292}
1293
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001294void anv_FreeMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001295 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001296 VkDeviceMemory _mem,
1297 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001298{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001299 ANV_FROM_HANDLE(anv_device, device, _device);
1300 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001301
Jason Ekstrandb1325402015-12-17 11:00:38 -08001302 if (mem == NULL)
1303 return;
1304
Jason Ekstrandb1217ea2016-11-07 17:25:07 -08001305 if (mem->map)
1306 anv_UnmapMemory(_device, _mem);
1307
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001308 if (mem->bo.map)
1309 anv_gem_munmap(mem->bo.map, mem->bo.size);
1310
1311 if (mem->bo.gem_handle != 0)
1312 anv_gem_close(device, mem->bo.gem_handle);
1313
Dave Airlie1ae6ece2016-10-14 13:31:35 +10001314 vk_free2(&device->alloc, pAllocator, mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001315}
1316
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001317VkResult anv_MapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001318 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001319 VkDeviceMemory _memory,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001320 VkDeviceSize offset,
1321 VkDeviceSize size,
1322 VkMemoryMapFlags flags,
1323 void** ppData)
1324{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001325 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001326 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001327
Jason Ekstrandb1325402015-12-17 11:00:38 -08001328 if (mem == NULL) {
1329 *ppData = NULL;
1330 return VK_SUCCESS;
1331 }
1332
Jason Ekstrand56dbf132016-01-19 15:01:10 -08001333 if (size == VK_WHOLE_SIZE)
1334 size = mem->bo.size - offset;
1335
Jason Ekstrand73ef9c82016-11-07 17:23:44 -08001336 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
1337 *
1338 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
1339 * assert(size != 0);
1340 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
1341 * equal to the size of the memory minus offset
1342 */
1343 assert(size > 0);
1344 assert(offset + size <= mem->bo.size);
1345
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001346 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1347 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1348 * at a time is valid. We could just mmap up front and return an offset
1349 * pointer here, but that may exhaust virtual memory on 32 bit
1350 * userspace. */
1351
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -08001352 uint32_t gem_flags = 0;
1353 if (!device->info.has_llc && mem->type_index == 0)
1354 gem_flags |= I915_MMAP_WC;
1355
Jason Ekstrandf076d532016-01-01 09:26:06 -08001356 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1357 uint64_t map_offset = offset & ~4095ull;
1358 assert(offset >= map_offset);
1359 uint64_t map_size = (offset + size) - map_offset;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001360
Jason Ekstrandf076d532016-01-01 09:26:06 -08001361 /* Let's map whole pages */
Jason Ekstrand6b0b5722016-01-02 07:52:22 -08001362 map_size = align_u64(map_size, 4096);
Jason Ekstrandf076d532016-01-01 09:26:06 -08001363
Jason Ekstrand920f34a2016-11-07 17:24:24 -08001364 void *map = anv_gem_mmap(device, mem->bo.gem_handle,
1365 map_offset, map_size, gem_flags);
1366 if (map == MAP_FAILED)
1367 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
1368
1369 mem->map = map;
Jason Ekstrandf076d532016-01-01 09:26:06 -08001370 mem->map_size = map_size;
1371
1372 *ppData = mem->map + (offset - map_offset);
Chad Versace477383e2015-11-13 10:12:18 -08001373
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001374 return VK_SUCCESS;
1375}
1376
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001377void anv_UnmapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001378 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001379 VkDeviceMemory _memory)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001380{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001381 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001382
Jason Ekstrandb1325402015-12-17 11:00:38 -08001383 if (mem == NULL)
1384 return;
1385
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001386 anv_gem_munmap(mem->map, mem->map_size);
Jason Ekstrandb1217ea2016-11-07 17:25:07 -08001387
1388 mem->map = NULL;
1389 mem->map_size = 0;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001390}
1391
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001392static void
1393clflush_mapped_ranges(struct anv_device *device,
1394 uint32_t count,
1395 const VkMappedMemoryRange *ranges)
1396{
1397 for (uint32_t i = 0; i < count; i++) {
1398 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1399 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
Kristian Høgsberg Kristensen31d34862016-01-29 12:07:34 -08001400 void *end;
1401
1402 if (ranges[i].offset + ranges[i].size > mem->map_size)
1403 end = mem->map + mem->map_size;
1404 else
1405 end = mem->map + ranges[i].offset + ranges[i].size;
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001406
1407 while (p < end) {
1408 __builtin_ia32_clflush(p);
1409 p += CACHELINE_SIZE;
1410 }
1411 }
1412}
1413
Jason Ekstrandd9c2cae2015-07-07 17:22:29 -07001414VkResult anv_FlushMappedMemoryRanges(
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001415 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001416 uint32_t memoryRangeCount,
1417 const VkMappedMemoryRange* pMemoryRanges)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001418{
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001419 ANV_FROM_HANDLE(anv_device, device, _device);
1420
1421 if (device->info.has_llc)
1422 return VK_SUCCESS;
1423
1424 /* Make sure the writes we're flushing have landed. */
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001425 __builtin_ia32_mfence();
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001426
1427 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001428
1429 return VK_SUCCESS;
1430}
1431
Jason Ekstrandd9c2cae2015-07-07 17:22:29 -07001432VkResult anv_InvalidateMappedMemoryRanges(
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001433 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001434 uint32_t memoryRangeCount,
1435 const VkMappedMemoryRange* pMemoryRanges)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001436{
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001437 ANV_FROM_HANDLE(anv_device, device, _device);
1438
1439 if (device->info.has_llc)
1440 return VK_SUCCESS;
1441
1442 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1443
1444 /* Make sure no reads get moved up above the invalidate. */
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001445 __builtin_ia32_mfence();
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001446
1447 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001448}
1449
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001450void anv_GetBufferMemoryRequirements(
Connor Abbott3b524132017-02-14 12:23:59 -05001451 VkDevice _device,
Jason Ekstrand55723e92015-07-14 14:59:39 -07001452 VkBuffer _buffer,
Jason Ekstrandef8980e2015-07-07 18:16:42 -07001453 VkMemoryRequirements* pMemoryRequirements)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001454{
Jason Ekstrand55723e92015-07-14 14:59:39 -07001455 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
Connor Abbott3b524132017-02-14 12:23:59 -05001456 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001457
Chad Versacef43a3042015-07-09 19:59:44 -07001458 /* The Vulkan spec (git aaed022) says:
1459 *
1460 * memoryTypeBits is a bitfield and contains one bit set for every
1461 * supported memory type for the resource. The bit `1<<i` is set if and
1462 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1463 * structure for the physical device is supported.
1464 *
Connor Abbott3b524132017-02-14 12:23:59 -05001465 * We support exactly one memory type on LLC, two on non-LLC.
Chad Versacef43a3042015-07-09 19:59:44 -07001466 */
Connor Abbott3b524132017-02-14 12:23:59 -05001467 pMemoryRequirements->memoryTypeBits = device->info.has_llc ? 1 : 3;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001468
Jason Ekstrand55723e92015-07-14 14:59:39 -07001469 pMemoryRequirements->size = buffer->size;
1470 pMemoryRequirements->alignment = 16;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001471}
1472
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001473void anv_GetImageMemoryRequirements(
Connor Abbott3b524132017-02-14 12:23:59 -05001474 VkDevice _device,
Jason Ekstrand55723e92015-07-14 14:59:39 -07001475 VkImage _image,
1476 VkMemoryRequirements* pMemoryRequirements)
1477{
1478 ANV_FROM_HANDLE(anv_image, image, _image);
Connor Abbott3b524132017-02-14 12:23:59 -05001479 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand55723e92015-07-14 14:59:39 -07001480
1481 /* The Vulkan spec (git aaed022) says:
1482 *
1483 * memoryTypeBits is a bitfield and contains one bit set for every
1484 * supported memory type for the resource. The bit `1<<i` is set if and
1485 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1486 * structure for the physical device is supported.
1487 *
Connor Abbott3b524132017-02-14 12:23:59 -05001488 * We support exactly one memory type on LLC, two on non-LLC.
Jason Ekstrand55723e92015-07-14 14:59:39 -07001489 */
Connor Abbott3b524132017-02-14 12:23:59 -05001490 pMemoryRequirements->memoryTypeBits = device->info.has_llc ? 1 : 3;
Jason Ekstrand55723e92015-07-14 14:59:39 -07001491
1492 pMemoryRequirements->size = image->size;
1493 pMemoryRequirements->alignment = image->alignment;
Jason Ekstrand55723e92015-07-14 14:59:39 -07001494}
1495
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001496void anv_GetImageSparseMemoryRequirements(
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001497 VkDevice device,
1498 VkImage image,
Jason Ekstrand5a024412015-12-02 03:34:43 -08001499 uint32_t* pSparseMemoryRequirementCount,
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001500 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1501{
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001502 stub();
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001503}
1504
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001505void anv_GetDeviceMemoryCommitment(
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001506 VkDevice device,
1507 VkDeviceMemory memory,
1508 VkDeviceSize* pCommittedMemoryInBytes)
1509{
1510 *pCommittedMemoryInBytes = 0;
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001511}
1512
Jason Ekstrand55723e92015-07-14 14:59:39 -07001513VkResult anv_BindBufferMemory(
1514 VkDevice device,
1515 VkBuffer _buffer,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001516 VkDeviceMemory _memory,
1517 VkDeviceSize memoryOffset)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001518{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001519 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Jason Ekstrand55723e92015-07-14 14:59:39 -07001520 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001521
Jason Ekstrandb1325402015-12-17 11:00:38 -08001522 if (mem) {
1523 buffer->bo = &mem->bo;
1524 buffer->offset = memoryOffset;
1525 } else {
1526 buffer->bo = NULL;
1527 buffer->offset = 0;
1528 }
Jason Ekstrand55723e92015-07-14 14:59:39 -07001529
1530 return VK_SUCCESS;
1531}
1532
Jason Ekstrandfd536032015-11-30 16:42:12 -08001533VkResult anv_QueueBindSparse(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001534 VkQueue queue,
Jason Ekstrandfd536032015-11-30 16:42:12 -08001535 uint32_t bindInfoCount,
1536 const VkBindSparseInfo* pBindInfo,
1537 VkFence fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001538{
Jason Ekstrandfed35862015-12-02 16:14:58 -08001539 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001540}
1541
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001542VkResult anv_CreateFence(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001543 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001544 const VkFenceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001545 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001546 VkFence* pFence)
1547{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001548 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand23088912016-03-07 13:45:25 -08001549 struct anv_bo fence_bo;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001550 struct anv_fence *fence;
1551 struct anv_batch batch;
1552 VkResult result;
1553
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001554 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1555
Jason Ekstrandb1c5d452016-03-18 11:50:53 -07001556 result = anv_bo_pool_alloc(&device->batch_bo_pool, &fence_bo, 4096);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001557 if (result != VK_SUCCESS)
Jason Ekstrand23088912016-03-07 13:45:25 -08001558 return result;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001559
Jason Ekstrand23088912016-03-07 13:45:25 -08001560 /* Fences are small. Just store the CPU data structure in the BO. */
1561 fence = fence_bo.map;
1562 fence->bo = fence_bo;
1563
1564 /* Place the batch after the CPU data but on its own cache line. */
1565 const uint32_t batch_offset = align_u32(sizeof(*fence), CACHELINE_SIZE);
1566 batch.next = batch.start = fence->bo.map + batch_offset;
Jason Ekstrandda8f1482015-05-27 11:42:55 -07001567 batch.end = fence->bo.map + fence->bo.size;
Jason Ekstrand50018522016-04-18 17:03:00 -07001568 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1569 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001570
Kristian Høgsberg77359202015-12-01 15:37:12 -08001571 if (!device->info.has_llc) {
Jason Ekstrand23088912016-03-07 13:45:25 -08001572 assert(((uintptr_t) batch.start & CACHELINE_MASK) == 0);
1573 assert(batch.next - batch.start <= CACHELINE_SIZE);
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001574 __builtin_ia32_mfence();
Jason Ekstrandabaa3be2016-03-15 15:24:24 -07001575 __builtin_ia32_clflush(batch.start);
Kristian Høgsberg77359202015-12-01 15:37:12 -08001576 }
1577
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001578 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1579 fence->exec2_objects[0].relocation_count = 0;
1580 fence->exec2_objects[0].relocs_ptr = 0;
1581 fence->exec2_objects[0].alignment = 0;
1582 fence->exec2_objects[0].offset = fence->bo.offset;
1583 fence->exec2_objects[0].flags = 0;
1584 fence->exec2_objects[0].rsvd1 = 0;
1585 fence->exec2_objects[0].rsvd2 = 0;
1586
1587 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1588 fence->execbuf.buffer_count = 1;
Jason Ekstrand23088912016-03-07 13:45:25 -08001589 fence->execbuf.batch_start_offset = batch.start - fence->bo.map;
1590 fence->execbuf.batch_len = batch.next - batch.start;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001591 fence->execbuf.cliprects_ptr = 0;
1592 fence->execbuf.num_cliprects = 0;
1593 fence->execbuf.DR1 = 0;
1594 fence->execbuf.DR4 = 0;
1595
1596 fence->execbuf.flags =
1597 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1598 fence->execbuf.rsvd1 = device->context_id;
1599 fence->execbuf.rsvd2 = 0;
1600
Jason Ekstrand1c974322016-11-10 21:46:13 -08001601 if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
1602 fence->state = ANV_FENCE_STATE_SIGNALED;
1603 } else {
1604 fence->state = ANV_FENCE_STATE_RESET;
1605 }
Jason Ekstrand0d2145b2016-02-02 12:22:00 -08001606
Jason Ekstrand098209e2015-07-09 18:41:27 -07001607 *pFence = anv_fence_to_handle(fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001608
1609 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001610}
1611
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001612void anv_DestroyFence(
Chad Versaceebb191f2015-07-14 09:29:35 -07001613 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001614 VkFence _fence,
1615 const VkAllocationCallbacks* pAllocator)
Chad Versaceebb191f2015-07-14 09:29:35 -07001616{
1617 ANV_FROM_HANDLE(anv_device, device, _device);
1618 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1619
Jason Ekstrand49f08ad2016-11-10 21:32:32 -08001620 if (!fence)
1621 return;
1622
Jason Ekstrand23088912016-03-07 13:45:25 -08001623 assert(fence->bo.map == fence);
Jason Ekstrand23de7872016-03-06 14:16:51 -08001624 anv_bo_pool_free(&device->batch_bo_pool, &fence->bo);
Chad Versaceebb191f2015-07-14 09:29:35 -07001625}
1626
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001627VkResult anv_ResetFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001628 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001629 uint32_t fenceCount,
Jason Ekstrandd5349b12015-07-07 17:18:00 -07001630 const VkFence* pFences)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001631{
Chad Versace169251b2015-07-17 13:59:48 -07001632 for (uint32_t i = 0; i < fenceCount; i++) {
1633 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
Jason Ekstrand843775b2016-11-02 09:11:11 -07001634 fence->state = ANV_FENCE_STATE_RESET;
Chad Versace169251b2015-07-17 13:59:48 -07001635 }
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001636
1637 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001638}
1639
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001640VkResult anv_GetFenceStatus(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001641 VkDevice _device,
1642 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001643{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001644 ANV_FROM_HANDLE(anv_device, device, _device);
1645 ANV_FROM_HANDLE(anv_fence, fence, _fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001646 int64_t t = 0;
1647 int ret;
1648
Jason Ekstrand843775b2016-11-02 09:11:11 -07001649 switch (fence->state) {
1650 case ANV_FENCE_STATE_RESET:
1651 /* If it hasn't even been sent off to the GPU yet, it's not ready */
1652 return VK_NOT_READY;
1653
1654 case ANV_FENCE_STATE_SIGNALED:
1655 /* It's been signaled, return success */
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001656 return VK_SUCCESS;
1657
Jason Ekstrand843775b2016-11-02 09:11:11 -07001658 case ANV_FENCE_STATE_SUBMITTED:
1659 /* It's been submitted to the GPU but we don't know if it's done yet. */
1660 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1661 if (ret == 0) {
1662 fence->state = ANV_FENCE_STATE_SIGNALED;
1663 return VK_SUCCESS;
1664 } else {
1665 return VK_NOT_READY;
1666 }
1667 default:
1668 unreachable("Invalid fence status");
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001669 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001670}
1671
Jason Ekstrand843775b2016-11-02 09:11:11 -07001672#define NSEC_PER_SEC 1000000000
1673#define INT_TYPE_MAX(type) ((1ull << (sizeof(type) * 8 - 1)) - 1)
1674
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001675VkResult anv_WaitForFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001676 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001677 uint32_t fenceCount,
1678 const VkFence* pFences,
Chad Versace8f3b2182015-07-13 12:59:42 -07001679 VkBool32 waitAll,
Jason Ekstrand843775b2016-11-02 09:11:11 -07001680 uint64_t _timeout)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001681{
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001682 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand843775b2016-11-02 09:11:11 -07001683 int ret;
Jason Ekstrandaafc8742015-11-10 11:24:08 -08001684
1685 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1686 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1687 * for a couple of kernel releases. Since there's no way to know
1688 * whether or not the kernel we're using is one of the broken ones, the
1689 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1690 * maximum timeout from 584 years to 292 years - likely not a big deal.
1691 */
Jason Ekstrand843775b2016-11-02 09:11:11 -07001692 int64_t timeout = MIN2(_timeout, INT64_MAX);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001693
Jason Ekstrand843775b2016-11-02 09:11:11 -07001694 uint32_t pending_fences = fenceCount;
1695 while (pending_fences) {
1696 pending_fences = 0;
1697 bool signaled_fences = false;
1698 for (uint32_t i = 0; i < fenceCount; i++) {
1699 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1700 switch (fence->state) {
1701 case ANV_FENCE_STATE_RESET:
1702 /* This fence hasn't been submitted yet, we'll catch it the next
1703 * time around. Yes, this may mean we dead-loop but, short of
1704 * lots of locking and a condition variable, there's not much that
1705 * we can do about that.
1706 */
1707 pending_fences++;
1708 continue;
Jason Ekstrand427978d2015-11-10 15:02:52 -08001709
Jason Ekstrand843775b2016-11-02 09:11:11 -07001710 case ANV_FENCE_STATE_SIGNALED:
1711 /* This fence is not pending. If waitAll isn't set, we can return
1712 * early. Otherwise, we have to keep going.
1713 */
1714 if (!waitAll)
1715 return VK_SUCCESS;
1716 continue;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001717
Jason Ekstrand843775b2016-11-02 09:11:11 -07001718 case ANV_FENCE_STATE_SUBMITTED:
1719 /* These are the fences we really care about. Go ahead and wait
1720 * on it until we hit a timeout.
1721 */
1722 ret = anv_gem_wait(device, fence->bo.gem_handle, &timeout);
1723 if (ret == -1 && errno == ETIME) {
1724 return VK_TIMEOUT;
1725 } else if (ret == -1) {
1726 /* We don't know the real error. */
1727 return vk_errorf(VK_ERROR_DEVICE_LOST, "gem wait failed: %m");
1728 } else {
1729 fence->state = ANV_FENCE_STATE_SIGNALED;
1730 signaled_fences = true;
1731 if (!waitAll)
1732 return VK_SUCCESS;
1733 continue;
1734 }
1735 }
1736 }
1737
1738 if (pending_fences && !signaled_fences) {
1739 /* If we've hit this then someone decided to vkWaitForFences before
1740 * they've actually submitted any of them to a queue. This is a
1741 * fairly pessimal case, so it's ok to lock here and use a standard
1742 * pthreads condition variable.
1743 */
1744 pthread_mutex_lock(&device->mutex);
1745
1746 /* It's possible that some of the fences have changed state since the
1747 * last time we checked. Now that we have the lock, check for
1748 * pending fences again and don't wait if it's changed.
1749 */
1750 uint32_t now_pending_fences = 0;
1751 for (uint32_t i = 0; i < fenceCount; i++) {
1752 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1753 if (fence->state == ANV_FENCE_STATE_RESET)
1754 now_pending_fences++;
1755 }
1756 assert(now_pending_fences <= pending_fences);
1757
1758 if (now_pending_fences == pending_fences) {
1759 struct timespec before;
1760 clock_gettime(CLOCK_MONOTONIC, &before);
1761
1762 uint32_t abs_nsec = before.tv_nsec + timeout % NSEC_PER_SEC;
1763 uint64_t abs_sec = before.tv_sec + (abs_nsec / NSEC_PER_SEC) +
1764 (timeout / NSEC_PER_SEC);
1765 abs_nsec %= NSEC_PER_SEC;
1766
1767 /* Avoid roll-over in tv_sec on 32-bit systems if the user
1768 * provided timeout is UINT64_MAX
1769 */
1770 struct timespec abstime;
1771 abstime.tv_nsec = abs_nsec;
1772 abstime.tv_sec = MIN2(abs_sec, INT_TYPE_MAX(abstime.tv_sec));
1773
1774 ret = pthread_cond_timedwait(&device->queue_submit,
1775 &device->mutex, &abstime);
1776 assert(ret != EINVAL);
1777
1778 struct timespec after;
1779 clock_gettime(CLOCK_MONOTONIC, &after);
1780 uint64_t time_elapsed =
1781 ((uint64_t)after.tv_sec * NSEC_PER_SEC + after.tv_nsec) -
1782 ((uint64_t)before.tv_sec * NSEC_PER_SEC + before.tv_nsec);
1783
1784 if (time_elapsed >= timeout) {
1785 pthread_mutex_unlock(&device->mutex);
1786 return VK_TIMEOUT;
1787 }
1788
1789 timeout -= time_elapsed;
1790 }
1791
1792 pthread_mutex_unlock(&device->mutex);
Chad Versacef9c948e2015-10-07 11:36:51 -07001793 }
Jason Ekstrand5c497302015-07-09 18:20:28 -07001794 }
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001795
1796 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001797}
1798
1799// Queue semaphore functions
1800
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001801VkResult anv_CreateSemaphore(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001802 VkDevice device,
1803 const VkSemaphoreCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001804 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001805 VkSemaphore* pSemaphore)
1806{
Kristian Høgsberg Kristensena00524a2015-12-20 22:58:38 -08001807 /* The DRM execbuffer ioctl always execute in-oder, even between different
1808 * rings. As such, there's nothing to do for the user space semaphore.
1809 */
1810
Jason Ekstrand3db43e82015-11-30 10:31:44 -08001811 *pSemaphore = (VkSemaphore)1;
Kristian Høgsberg Kristensena00524a2015-12-20 22:58:38 -08001812
1813 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001814}
1815
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001816void anv_DestroySemaphore(
Chad Versace549070b2015-07-14 09:31:34 -07001817 VkDevice device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001818 VkSemaphore semaphore,
1819 const VkAllocationCallbacks* pAllocator)
Chad Versace549070b2015-07-14 09:31:34 -07001820{
Chad Versace549070b2015-07-14 09:31:34 -07001821}
1822
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001823// Event functions
1824
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001825VkResult anv_CreateEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001826 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001827 const VkEventCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001828 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001829 VkEvent* pEvent)
1830{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001831 ANV_FROM_HANDLE(anv_device, device, _device);
1832 struct anv_state state;
1833 struct anv_event *event;
1834
1835 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
1836
1837 state = anv_state_pool_alloc(&device->dynamic_state_pool,
Jason Ekstrand25b09d12016-02-11 18:57:37 -08001838 sizeof(*event), 8);
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001839 event = state.map;
1840 event->state = state;
1841 event->semaphore = VK_EVENT_RESET;
1842
1843 if (!device->info.has_llc) {
1844 /* Make sure the writes we're flushing have landed. */
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001845 __builtin_ia32_mfence();
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001846 __builtin_ia32_clflush(event);
1847 }
1848
1849 *pEvent = anv_event_to_handle(event);
1850
1851 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001852}
1853
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001854void anv_DestroyEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001855 VkDevice _device,
1856 VkEvent _event,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001857 const VkAllocationCallbacks* pAllocator)
Chad Versace68c7ef52015-07-14 09:33:47 -07001858{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001859 ANV_FROM_HANDLE(anv_device, device, _device);
1860 ANV_FROM_HANDLE(anv_event, event, _event);
1861
Jason Ekstrand49f08ad2016-11-10 21:32:32 -08001862 if (!event)
1863 return;
1864
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001865 anv_state_pool_free(&device->dynamic_state_pool, event->state);
Chad Versace68c7ef52015-07-14 09:33:47 -07001866}
1867
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001868VkResult anv_GetEventStatus(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001869 VkDevice _device,
1870 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001871{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001872 ANV_FROM_HANDLE(anv_device, device, _device);
1873 ANV_FROM_HANDLE(anv_event, event, _event);
1874
1875 if (!device->info.has_llc) {
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001876 /* Invalidate read cache before reading event written by GPU. */
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001877 __builtin_ia32_clflush(event);
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001878 __builtin_ia32_mfence();
1879
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001880 }
1881
1882 return event->semaphore;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001883}
1884
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001885VkResult anv_SetEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001886 VkDevice _device,
1887 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001888{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001889 ANV_FROM_HANDLE(anv_device, device, _device);
1890 ANV_FROM_HANDLE(anv_event, event, _event);
1891
1892 event->semaphore = VK_EVENT_SET;
1893
1894 if (!device->info.has_llc) {
1895 /* Make sure the writes we're flushing have landed. */
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001896 __builtin_ia32_mfence();
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001897 __builtin_ia32_clflush(event);
1898 }
1899
1900 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001901}
1902
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001903VkResult anv_ResetEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001904 VkDevice _device,
1905 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001906{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001907 ANV_FROM_HANDLE(anv_device, device, _device);
1908 ANV_FROM_HANDLE(anv_event, event, _event);
1909
1910 event->semaphore = VK_EVENT_RESET;
1911
1912 if (!device->info.has_llc) {
1913 /* Make sure the writes we're flushing have landed. */
Kristian Høgsberg Kristensen0c4ef362016-01-29 12:10:12 -08001914 __builtin_ia32_mfence();
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001915 __builtin_ia32_clflush(event);
1916 }
1917
1918 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001919}
1920
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001921// Buffer functions
1922
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001923VkResult anv_CreateBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001924 VkDevice _device,
1925 const VkBufferCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001926 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001927 VkBuffer* pBuffer)
1928{
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001929 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001930 struct anv_buffer *buffer;
1931
1932 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1933
Dave Airlie1ae6ece2016-10-14 13:31:35 +10001934 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001935 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001936 if (buffer == NULL)
1937 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1938
1939 buffer->size = pCreateInfo->size;
Jason Ekstrand783a2112015-12-14 16:51:12 -08001940 buffer->usage = pCreateInfo->usage;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07001941 buffer->bo = NULL;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001942 buffer->offset = 0;
1943
Jason Ekstrand098209e2015-07-09 18:41:27 -07001944 *pBuffer = anv_buffer_to_handle(buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001945
1946 return VK_SUCCESS;
1947}
1948
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001949void anv_DestroyBuffer(
Chad Versacee93b6d82015-07-14 09:47:45 -07001950 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001951 VkBuffer _buffer,
1952 const VkAllocationCallbacks* pAllocator)
Chad Versacee93b6d82015-07-14 09:47:45 -07001953{
1954 ANV_FROM_HANDLE(anv_device, device, _device);
1955 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1956
Jason Ekstrand49f08ad2016-11-10 21:32:32 -08001957 if (!buffer)
1958 return;
1959
Dave Airlie1ae6ece2016-10-14 13:31:35 +10001960 vk_free2(&device->alloc, pAllocator, buffer);
Chad Versacee93b6d82015-07-14 09:47:45 -07001961}
1962
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001963void
Francisco Jerez6840cc12016-01-26 14:50:52 -08001964anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
Jason Ekstrand1f98bf82015-12-14 16:14:20 -08001965 enum isl_format format,
Jason Ekstrand399d5312015-11-06 15:14:10 -08001966 uint32_t offset, uint32_t range, uint32_t stride)
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001967{
Jason Ekstrandeb19d642016-02-22 16:54:25 -08001968 isl_buffer_fill_state(&device->isl_dev, state.map,
1969 .address = offset,
1970 .mocs = device->default_mocs,
1971 .size = range,
1972 .format = format,
1973 .stride = stride);
Francisco Jerez6840cc12016-01-26 14:50:52 -08001974
1975 if (!device->info.has_llc)
1976 anv_state_clflush(state);
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001977}
1978
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001979void anv_DestroySampler(
Chad Versaceec5e2f42015-07-14 10:34:00 -07001980 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001981 VkSampler _sampler,
1982 const VkAllocationCallbacks* pAllocator)
Chad Versaceec5e2f42015-07-14 10:34:00 -07001983{
1984 ANV_FROM_HANDLE(anv_device, device, _device);
1985 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1986
Jason Ekstrand49f08ad2016-11-10 21:32:32 -08001987 if (!sampler)
1988 return;
1989
Dave Airlie1ae6ece2016-10-14 13:31:35 +10001990 vk_free2(&device->alloc, pAllocator, sampler);
Chad Versaceec5e2f42015-07-14 10:34:00 -07001991}
1992
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001993VkResult anv_CreateFramebuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001994 VkDevice _device,
1995 const VkFramebufferCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001996 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001997 VkFramebuffer* pFramebuffer)
1998{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001999 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002000 struct anv_framebuffer *framebuffer;
2001
2002 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2003
Jason Ekstrand84783502015-07-10 20:18:52 -07002004 size_t size = sizeof(*framebuffer) +
Chad Versaced4446a72015-10-06 11:42:43 -07002005 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
Dave Airlie1ae6ece2016-10-14 13:31:35 +10002006 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08002007 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002008 if (framebuffer == NULL)
2009 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2010
Jason Ekstrand84783502015-07-10 20:18:52 -07002011 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2012 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
Chad Versace6dea1a92015-10-07 07:30:52 -07002013 VkImageView _iview = pCreateInfo->pAttachments[i];
Chad Versaced4446a72015-10-06 11:42:43 -07002014 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002015 }
2016
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002017 framebuffer->width = pCreateInfo->width;
2018 framebuffer->height = pCreateInfo->height;
2019 framebuffer->layers = pCreateInfo->layers;
2020
Jason Ekstrand098209e2015-07-09 18:41:27 -07002021 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002022
2023 return VK_SUCCESS;
2024}
2025
Jason Ekstrand05a26a62015-10-05 20:50:51 -07002026void anv_DestroyFramebuffer(
Chad Versace08f77312015-07-14 10:59:30 -07002027 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08002028 VkFramebuffer _fb,
2029 const VkAllocationCallbacks* pAllocator)
Chad Versace08f77312015-07-14 10:59:30 -07002030{
2031 ANV_FROM_HANDLE(anv_device, device, _device);
2032 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
2033
Jason Ekstrand49f08ad2016-11-10 21:32:32 -08002034 if (!fb)
2035 return;
2036
Dave Airlie1ae6ece2016-10-14 13:31:35 +10002037 vk_free2(&device->alloc, pAllocator, fb);
Chad Versace08f77312015-07-14 10:59:30 -07002038}
Chad Versace1e41d7f2017-01-10 17:29:08 -08002039
2040/* vk_icd.h does not declare this function, so we declare it here to
2041 * suppress Wmissing-prototypes.
2042 */
2043PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2044vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
2045
2046PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2047vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion)
2048{
2049 /* For the full details on loader interface versioning, see
2050 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2051 * What follows is a condensed summary, to help you navigate the large and
2052 * confusing official doc.
2053 *
2054 * - Loader interface v0 is incompatible with later versions. We don't
2055 * support it.
2056 *
2057 * - In loader interface v1:
2058 * - The first ICD entrypoint called by the loader is
2059 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2060 * entrypoint.
2061 * - The ICD must statically expose no other Vulkan symbol unless it is
2062 * linked with -Bsymbolic.
2063 * - Each dispatchable Vulkan handle created by the ICD must be
2064 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2065 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2066 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2067 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2068 * such loader-managed surfaces.
2069 *
2070 * - Loader interface v2 differs from v1 in:
2071 * - The first ICD entrypoint called by the loader is
2072 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2073 * statically expose this entrypoint.
2074 *
2075 * - Loader interface v3 differs from v2 in:
2076 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2077 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2078 * because the loader no longer does so.
2079 */
2080 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2081 return VK_SUCCESS;
2082}