blob: 30ab0b2e739a4d09e4c6f18f87635aa0a2fe90cf [file] [log] [blame]
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001/*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <assert.h>
25#include <stdbool.h>
26#include <string.h>
27#include <unistd.h>
28#include <fcntl.h>
29
Chad Versace2c2233e2015-07-17 15:04:27 -070030#include "anv_private.h"
Jason Ekstrand977a4692015-07-09 15:53:03 -070031#include "mesa/main/git_sha1.h"
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -070032#include "util/strtod.h"
Kristian Høgsberg769785c2015-05-08 22:32:37 -070033
Jason Ekstrandde54b4b2015-11-16 12:29:07 -080034#include "gen7_pack.h"
35
Jason Ekstranda95f51c2015-09-24 14:20:35 -070036struct anv_dispatch_table dtable;
37
Jason Ekstranda71e6142015-10-19 22:06:59 -070038static void
39compiler_debug_log(void *data, const char *fmt, ...)
40{ }
41
42static void
43compiler_perf_log(void *data, const char *fmt, ...)
44{
45 va_list args;
46 va_start(args, fmt);
47
48 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
49 vfprintf(stderr, fmt, args);
50
51 va_end(args);
52}
53
Kristian Høgsberg769785c2015-05-08 22:32:37 -070054static VkResult
Chad Versace4422bd42015-07-09 16:22:18 -070055anv_physical_device_init(struct anv_physical_device *device,
56 struct anv_instance *instance,
57 const char *path)
Kristian Høgsberg769785c2015-05-08 22:32:37 -070058{
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070059 VkResult result;
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -070060 int fd;
61
62 fd = open(path, O_RDWR | O_CLOEXEC);
63 if (fd < 0)
Chad Versacef9c948e2015-10-07 11:36:51 -070064 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
65 "failed to open %s: %m", path);
Kristian Høgsberg769785c2015-05-08 22:32:37 -070066
Jason Ekstrand39cd3782015-09-24 13:51:40 -070067 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Kristian Høgsberg769785c2015-05-08 22:32:37 -070068 device->instance = instance;
69 device->path = path;
Chad Versacef9c948e2015-10-07 11:36:51 -070070
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -070071 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070072 if (!device->chipset_id) {
Chad Versacef9c948e2015-10-07 11:36:51 -070073 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
74 "failed to get chipset id: %m");
Kristian Høgsberg769785c2015-05-08 22:32:37 -070075 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070076 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -070077
78 device->name = brw_get_device_name(device->chipset_id);
Jason Ekstrandb00e3f22015-11-03 15:45:04 -080079 device->info = brw_get_device_info(device->chipset_id);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070080 if (!device->info) {
Chad Versacef9c948e2015-10-07 11:36:51 -070081 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
82 "failed to get device info");
Kristian Høgsberg769785c2015-05-08 22:32:37 -070083 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -070084 }
Jason Ekstrand584f9d42015-11-02 12:14:37 -080085
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080086 if (device->info->is_haswell) {
87 fprintf(stderr, "WARNING: Haswell Vulkan support is incomplete\n");
88 } else if (device->info->gen == 7 && !device->info->is_baytrail) {
Jason Ekstrand862da6a2015-11-09 12:18:12 -080089 fprintf(stderr, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
Kristian Høgsbergdac57752015-12-01 15:39:30 -080090 } else if (device->info->gen == 7 && device->info->is_baytrail) {
91 fprintf(stderr, "WARNING: Bay Trail Vulkan support is incomplete\n");
92 } else if (device->info->gen == 9 && !device->info->is_broxton) {
Kristian Høgsberg Kristensencd4721c2015-11-25 22:27:01 -080093 fprintf(stderr, "WARNING: Skylake Vulkan support is incomplete\n");
Kristian Høgsbergdac57752015-12-01 15:39:30 -080094 } else if (device->info->gen == 9 && device->info->is_broxton) {
95 fprintf(stderr, "WARNING: Broxton Vulkan support is incomplete\n");
96 } else if (device->info->gen == 8) {
97 /* Broadwell/Cherryview is as fully supported as anything */
Jason Ekstrand584f9d42015-11-02 12:14:37 -080098 } else {
Jason Ekstrandfed35862015-12-02 16:14:58 -080099 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800100 "Vulkan not yet supported on %s", device->name);
101 goto fail;
102 }
103
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700104 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700105 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
106 "failed to get aperture size: %m");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700107 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700108 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700109
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700110 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700111 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
112 "kernel missing gem wait");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700113 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700114 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700115
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700116 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700117 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
118 "kernel missing execbuf2");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700119 goto fail;
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700120 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700121
Kristian Høgsberg Kristensen220ac932015-12-19 22:25:57 -0800122 if (!device->info->has_llc &&
123 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -0800124 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
125 "kernel missing wc mmap");
126 goto fail;
127 }
128
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700129 close(fd);
130
Jason Ekstranda71e6142015-10-19 22:06:59 -0700131 brw_process_intel_debug_variable();
132
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700133 device->compiler = brw_compiler_create(NULL, device->info);
134 if (device->compiler == NULL) {
135 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
136 goto fail;
137 }
Jason Ekstranda71e6142015-10-19 22:06:59 -0700138 device->compiler->shader_debug_log = compiler_debug_log;
139 device->compiler->shader_perf_log = compiler_perf_log;
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700140
Chad Versace738eaa82015-11-13 11:12:46 -0800141 isl_device_init(&device->isl_dev, device->info);
Chad Versaceaf392912015-11-13 10:12:51 -0800142
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700143 return VK_SUCCESS;
Chad Versace477383e2015-11-13 10:12:18 -0800144
Chad Versace8cda3e92015-07-09 16:31:39 -0700145fail:
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700146 close(fd);
Kristian Høgsberg Kristensenc4b30e72015-08-26 04:03:38 -0700147 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700148}
149
Jason Ekstrand6fb44692015-10-19 20:21:45 -0700150static void
151anv_physical_device_finish(struct anv_physical_device *device)
152{
153 ralloc_free(device->compiler);
154}
155
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700156static const VkExtensionProperties global_extensions[] = {
157 {
Jason Ekstrandd6664872015-12-02 16:28:36 -0800158 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
159 .specVersion = 24,
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700160 },
Jason Ekstrandd6664872015-12-02 16:28:36 -0800161 {
162 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
163 .specVersion = 5,
164 },
165#ifdef HAVE_WAYLAND_PLATFORM
166 {
167 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
168 .specVersion = 4,
169 },
170#endif
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700171};
172
173static const VkExtensionProperties device_extensions[] = {
174 {
Jason Ekstrandd6664872015-12-02 16:28:36 -0800175 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
176 .specVersion = 67,
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700177 },
178};
179
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800180static void *
181default_alloc_func(void *pUserData, size_t size, size_t align,
182 VkSystemAllocationScope allocationScope)
183{
184 return malloc(size);
185}
186
187static void *
188default_realloc_func(void *pUserData, void *pOriginal, size_t size,
189 size_t align, VkSystemAllocationScope allocationScope)
190{
191 return realloc(pOriginal, size);
192}
193
194static void
195default_free_func(void *pUserData, void *pMemory)
196{
197 free(pMemory);
198}
199
200static const VkAllocationCallbacks default_alloc = {
201 .pUserData = NULL,
202 .pfnAllocation = default_alloc_func,
203 .pfnReallocation = default_realloc_func,
204 .pfnFree = default_free_func,
205};
206
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700207VkResult anv_CreateInstance(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700208 const VkInstanceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800209 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700210 VkInstance* pInstance)
211{
212 struct anv_instance *instance;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700213
214 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
215
Jason Ekstrandd6664872015-12-02 16:28:36 -0800216 if (pCreateInfo->pApplicationInfo->apiVersion != VK_MAKE_VERSION(0, 210, 1))
Jason Ekstrande21ecb82015-10-12 18:25:19 -0700217 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
218
Jason Ekstrand93496252015-12-01 13:58:25 -0800219 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700220 bool found = false;
221 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
222 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800223 global_extensions[j].extensionName) == 0) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700224 found = true;
225 break;
226 }
227 }
228 if (!found)
Chad Versacef9c948e2015-10-07 11:36:51 -0700229 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700230 }
231
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800232 instance = anv_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
233 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700234 if (!instance)
235 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
236
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700237 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800238
239 if (pAllocator)
240 instance->alloc = *pAllocator;
241 else
242 instance->alloc = default_alloc;
243
Jason Ekstrand93496252015-12-01 13:58:25 -0800244 instance->apiVersion = pCreateInfo->pApplicationInfo->apiVersion;
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800245 instance->physicalDeviceCount = -1;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700246
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -0700247 _mesa_locale_init();
248
Jason Ekstrand930598a2015-07-31 10:18:00 -0700249 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
250
Jason Ekstrand348cb292015-09-04 11:14:45 -0700251 anv_init_wsi(instance);
252
Jason Ekstrand098209e2015-07-09 18:41:27 -0700253 *pInstance = anv_instance_to_handle(instance);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700254
255 return VK_SUCCESS;
256}
257
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700258void anv_DestroyInstance(
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800259 VkInstance _instance,
260 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700261{
Jason Ekstrand73f91872015-07-09 18:41:27 -0700262 ANV_FROM_HANDLE(anv_instance, instance, _instance);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700263
Chad Versace0ab926d2015-10-21 11:36:39 -0700264 if (instance->physicalDeviceCount > 0) {
265 /* We support at most one physical device. */
266 assert(instance->physicalDeviceCount == 1);
267 anv_physical_device_finish(&instance->physicalDevice);
268 }
269
Jason Ekstrand348cb292015-09-04 11:14:45 -0700270 anv_finish_wsi(instance);
271
Jason Ekstrand930598a2015-07-31 10:18:00 -0700272 VG(VALGRIND_DESTROY_MEMPOOL(instance));
273
Jason Ekstrand6a7ca4e2015-08-14 17:25:04 -0700274 _mesa_locale_fini();
275
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800276 anv_free(&instance->alloc, instance);
Jason Ekstrande40bdce2015-07-31 10:13:24 -0700277}
278
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700279VkResult anv_EnumeratePhysicalDevices(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700280 VkInstance _instance,
281 uint32_t* pPhysicalDeviceCount,
282 VkPhysicalDevice* pPhysicalDevices)
283{
Jason Ekstrand73f91872015-07-09 18:41:27 -0700284 ANV_FROM_HANDLE(anv_instance, instance, _instance);
Chad Versacefa915b62015-07-09 15:38:58 -0700285 VkResult result;
286
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800287 if (instance->physicalDeviceCount < 0) {
Chad Versace4422bd42015-07-09 16:22:18 -0700288 result = anv_physical_device_init(&instance->physicalDevice,
289 instance, "/dev/dri/renderD128");
Jason Ekstrandfed35862015-12-02 16:14:58 -0800290 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800291 instance->physicalDeviceCount = 0;
292 } else if (result == VK_SUCCESS) {
293 instance->physicalDeviceCount = 1;
294 } else {
Chad Versacefa915b62015-07-09 15:38:58 -0700295 return result;
Jason Ekstrand584f9d42015-11-02 12:14:37 -0800296 }
Chad Versacefa915b62015-07-09 15:38:58 -0700297 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700298
Chad Versace5b75dff2015-07-09 15:51:06 -0700299 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
300 * otherwise it's an inout parameter.
301 *
302 * The Vulkan spec (git aaed022) says:
303 *
304 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
305 * that is initialized with the number of devices the application is
306 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
307 * an array of at least this many VkPhysicalDevice handles [...].
308 *
309 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
310 * overwrites the contents of the variable pointed to by
311 * pPhysicalDeviceCount with the number of physical devices in in the
312 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
313 * pPhysicalDeviceCount with the number of physical handles written to
314 * pPhysicalDevices.
315 */
316 if (!pPhysicalDevices) {
317 *pPhysicalDeviceCount = instance->physicalDeviceCount;
318 } else if (*pPhysicalDeviceCount >= 1) {
Jason Ekstrand098209e2015-07-09 18:41:27 -0700319 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
Chad Versace5b75dff2015-07-09 15:51:06 -0700320 *pPhysicalDeviceCount = 1;
321 } else {
322 *pPhysicalDeviceCount = 0;
323 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700324
325 return VK_SUCCESS;
326}
327
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800328void anv_GetPhysicalDeviceFeatures(
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700329 VkPhysicalDevice physicalDevice,
330 VkPhysicalDeviceFeatures* pFeatures)
331{
332 anv_finishme("Get correct values for PhysicalDeviceFeatures");
333
334 *pFeatures = (VkPhysicalDeviceFeatures) {
335 .robustBufferAccess = false,
336 .fullDrawIndexUint32 = false,
337 .imageCubeArray = false,
338 .independentBlend = false,
339 .geometryShader = true,
340 .tessellationShader = false,
341 .sampleRateShading = false,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800342 .dualSrcBlend = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700343 .logicOp = true,
Chad Versace545f5cc2015-10-07 10:05:02 -0700344 .multiDrawIndirect = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800345 .depthClamp = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700346 .depthBiasClamp = false,
347 .fillModeNonSolid = true,
348 .depthBounds = false,
349 .wideLines = true,
350 .largePoints = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800351 .alphaToOne = true,
352 .multiViewport = true,
353 .samplerAnisotropy = false, /* FINISHME */
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700354 .textureCompressionETC2 = true,
355 .textureCompressionASTC_LDR = true,
356 .textureCompressionBC = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800357 .occlusionQueryPrecise = false, /* FINISHME */
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700358 .pipelineStatisticsQuery = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800359 .vertexPipelineStoresAndAtomics = false,
360 .fragmentStoresAndAtomics = true,
361 .shaderTessellationAndGeometryPointSize = true,
Chad Versace545f5cc2015-10-07 10:05:02 -0700362 .shaderImageGatherExtended = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700363 .shaderStorageImageExtendedFormats = false,
364 .shaderStorageImageMultisample = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700365 .shaderUniformBufferArrayDynamicIndexing = true,
366 .shaderSampledImageArrayDynamicIndexing = false,
367 .shaderStorageBufferArrayDynamicIndexing = false,
368 .shaderStorageImageArrayDynamicIndexing = false,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800369 .shaderStorageImageReadWithoutFormat = false,
370 .shaderStorageImageWriteWithoutFormat = true,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700371 .shaderClipDistance = false,
372 .shaderCullDistance = false,
373 .shaderFloat64 = false,
374 .shaderInt64 = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700375 .shaderInt16 = false,
Chad Versace545f5cc2015-10-07 10:05:02 -0700376 .alphaToOne = true,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800377 .variableMultisampleRate = false,
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700378 };
Jason Ekstrandf6d51f32015-07-09 13:54:08 -0700379}
380
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800381void anv_GetPhysicalDeviceProperties(
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700382 VkPhysicalDevice physicalDevice,
Chad Versaced48e71c2015-10-07 10:36:46 -0700383 VkPhysicalDeviceProperties* pProperties)
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700384{
Chad Versaced48e71c2015-10-07 10:36:46 -0700385 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
386 const struct brw_device_info *devinfo = pdevice->info;
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700387
Chad Versaced48e71c2015-10-07 10:36:46 -0700388 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700389
Jason Ekstrandd6897452015-12-02 16:58:54 -0800390 VkSampleCountFlags sample_counts =
391 VK_SAMPLE_COUNT_1_BIT |
392 VK_SAMPLE_COUNT_2_BIT |
393 VK_SAMPLE_COUNT_4_BIT |
394 VK_SAMPLE_COUNT_8_BIT;
395
Chad Versaced48e71c2015-10-07 10:36:46 -0700396 VkPhysicalDeviceLimits limits = {
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700397 .maxImageDimension1D = (1 << 14),
398 .maxImageDimension2D = (1 << 14),
399 .maxImageDimension3D = (1 << 10),
400 .maxImageDimensionCube = (1 << 14),
401 .maxImageArrayLayers = (1 << 10),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800402 .maxTexelBufferElements = (1 << 14),
403 .maxUniformBufferRange = UINT32_MAX,
404 .maxStorageBufferRange = UINT32_MAX,
Jason Ekstrand5446bf32015-08-26 15:01:38 -0700405 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700406 .maxMemoryAllocationCount = UINT32_MAX,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800407 .maxSamplerAllocationCount = UINT32_MAX,
Jason Ekstrande5db2092015-07-14 17:10:37 -0700408 .bufferImageGranularity = 64, /* A cache line */
Chad Versace033a37f2015-10-07 09:57:51 -0700409 .sparseAddressSpaceSize = 0,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700410 .maxBoundDescriptorSets = MAX_SETS,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700411 .maxPerStageDescriptorSamplers = 64,
412 .maxPerStageDescriptorUniformBuffers = 64,
413 .maxPerStageDescriptorStorageBuffers = 64,
414 .maxPerStageDescriptorSampledImages = 64,
415 .maxPerStageDescriptorStorageImages = 64,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800416 .maxPerStageDescriptorInputAttachments = 64,
417 .maxPerStageResources = 128,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700418 .maxDescriptorSetSamplers = 256,
419 .maxDescriptorSetUniformBuffers = 256,
Chad Versace033a37f2015-10-07 09:57:51 -0700420 .maxDescriptorSetUniformBuffersDynamic = 256,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700421 .maxDescriptorSetStorageBuffers = 256,
Chad Versace033a37f2015-10-07 09:57:51 -0700422 .maxDescriptorSetStorageBuffersDynamic = 256,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700423 .maxDescriptorSetSampledImages = 256,
424 .maxDescriptorSetStorageImages = 256,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800425 .maxDescriptorSetInputAttachments = 256,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700426 .maxVertexInputAttributes = 32,
Chad Versace033a37f2015-10-07 09:57:51 -0700427 .maxVertexInputBindings = 32,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700428 .maxVertexInputAttributeOffset = 256,
429 .maxVertexInputBindingStride = 256,
430 .maxVertexOutputComponents = 32,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800431 .maxTessellationGenerationLevel = 0,
432 .maxTessellationPatchSize = 0,
433 .maxTessellationControlPerVertexInputComponents = 0,
434 .maxTessellationControlPerVertexOutputComponents = 0,
435 .maxTessellationControlPerPatchOutputComponents = 0,
436 .maxTessellationControlTotalOutputComponents = 0,
437 .maxTessellationEvaluationInputComponents = 0,
438 .maxTessellationEvaluationOutputComponents = 0,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700439 .maxGeometryShaderInvocations = 6,
440 .maxGeometryInputComponents = 16,
441 .maxGeometryOutputComponents = 16,
442 .maxGeometryOutputVertices = 16,
443 .maxGeometryTotalOutputComponents = 16,
444 .maxFragmentInputComponents = 16,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800445 .maxFragmentOutputAttachments = 8,
446 .maxFragmentDualSrcAttachments = 2,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700447 .maxFragmentCombinedOutputResources = 8,
448 .maxComputeSharedMemorySize = 1024,
449 .maxComputeWorkGroupCount = {
450 16 * devinfo->max_cs_threads,
451 16 * devinfo->max_cs_threads,
452 16 * devinfo->max_cs_threads,
453 },
454 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
455 .maxComputeWorkGroupSize = {
456 16 * devinfo->max_cs_threads,
457 16 * devinfo->max_cs_threads,
458 16 * devinfo->max_cs_threads,
459 },
460 .subPixelPrecisionBits = 4 /* FIXME */,
461 .subTexelPrecisionBits = 4 /* FIXME */,
462 .mipmapPrecisionBits = 4 /* FIXME */,
463 .maxDrawIndexedIndexValue = UINT32_MAX,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800464 .maxDrawIndirectCount = UINT32_MAX,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700465 .maxSamplerLodBias = 16,
466 .maxSamplerAnisotropy = 16,
Jason Ekstranddaf68a92015-10-06 17:21:44 -0700467 .maxViewports = MAX_VIEWPORTS,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700468 .maxViewportDimensions = { (1 << 14), (1 << 14) },
469 .viewportBoundsRange = { -1.0, 1.0 }, /* FIXME */
470 .viewportSubPixelBits = 13, /* We take a float? */
Jason Ekstrandf076d532016-01-01 09:26:06 -0800471 .minMemoryMapAlignment = 4096, /* A page */
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700472 .minTexelBufferOffsetAlignment = 1,
473 .minUniformBufferOffsetAlignment = 1,
474 .minStorageBufferOffsetAlignment = 1,
475 .minTexelOffset = 0, /* FIXME */
476 .maxTexelOffset = 0, /* FIXME */
477 .minTexelGatherOffset = 0, /* FIXME */
478 .maxTexelGatherOffset = 0, /* FIXME */
479 .minInterpolationOffset = 0, /* FIXME */
480 .maxInterpolationOffset = 0, /* FIXME */
481 .subPixelInterpolationOffsetBits = 0, /* FIXME */
482 .maxFramebufferWidth = (1 << 14),
483 .maxFramebufferHeight = (1 << 14),
484 .maxFramebufferLayers = (1 << 10),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800485 .framebufferColorSampleCounts = sample_counts,
486 .framebufferDepthSampleCounts = sample_counts,
487 .framebufferStencilSampleCounts = sample_counts,
488 .framebufferNoAttachmentsSampleCounts = sample_counts,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700489 .maxColorAttachments = MAX_RTS,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800490 .sampledImageColorSampleCounts = sample_counts,
491 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
492 .sampledImageDepthSampleCounts = sample_counts,
493 .sampledImageStencilSampleCounts = sample_counts,
494 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700495 .maxSampleMaskWords = 1,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800496 .timestampPeriod = 80.0 / (1000 * 1000 * 1000),
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700497 .maxClipDistances = 0 /* FIXME */,
498 .maxCullDistances = 0 /* FIXME */,
499 .maxCombinedClipAndCullDistances = 0 /* FIXME */,
Jason Ekstrandd6897452015-12-02 16:58:54 -0800500 .discreteQueuePriorities = 1,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700501 .pointSizeRange = { 0.125, 255.875 },
502 .lineWidthRange = { 0.0, 7.9921875 },
503 .pointSizeGranularity = (1.0 / 8.0),
504 .lineWidthGranularity = (1.0 / 128.0),
Jason Ekstrandd6897452015-12-02 16:58:54 -0800505 .strictLines = false, /* FINISHME */
506 .standardSampleLocations = true, /* FINISHME */
507 .optimalBufferCopyOffsetAlignment = 128,
508 .optimalBufferCopyRowPitchAlignment = 128,
509 .nonCoherentAtomSize = 64,
Jason Ekstrand65e0b302015-07-09 15:38:30 -0700510 };
511
Jason Ekstrand977a4692015-07-09 15:53:03 -0700512 *pProperties = (VkPhysicalDeviceProperties) {
Jason Ekstrandbfeaf672015-12-03 15:23:33 -0800513 .apiVersion = VK_MAKE_VERSION(0, 210, 1),
Jason Ekstrand977a4692015-07-09 15:53:03 -0700514 .driverVersion = 1,
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800515 .vendorID = 0x8086,
516 .deviceID = pdevice->chipset_id,
Jason Ekstrand977a4692015-07-09 15:53:03 -0700517 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
Chad Versaced48e71c2015-10-07 10:36:46 -0700518 .limits = limits,
519 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
Jason Ekstrand977a4692015-07-09 15:53:03 -0700520 };
521
522 strcpy(pProperties->deviceName, pdevice->name);
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800523 snprintf((char *)pProperties->pipelineCacheUUID, VK_UUID_SIZE,
Jason Ekstrand977a4692015-07-09 15:53:03 -0700524 "anv-%s", MESA_GIT_SHA1 + 4);
Jason Ekstrand977a4692015-07-09 15:53:03 -0700525}
526
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800527void anv_GetPhysicalDeviceQueueFamilyProperties(
Jason Ekstrand1f907012015-07-09 16:11:24 -0700528 VkPhysicalDevice physicalDevice,
Jason Ekstranda6eba402015-10-05 21:17:12 -0700529 uint32_t* pCount,
530 VkQueueFamilyProperties* pQueueFamilyProperties)
Jason Ekstrand1f907012015-07-09 16:11:24 -0700531{
Jason Ekstranda6eba402015-10-05 21:17:12 -0700532 if (pQueueFamilyProperties == NULL) {
533 *pCount = 1;
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800534 return;
Jason Ekstranda6eba402015-10-05 21:17:12 -0700535 }
Jason Ekstrand1f907012015-07-09 16:11:24 -0700536
Jason Ekstranda6eba402015-10-05 21:17:12 -0700537 assert(*pCount >= 1);
Jason Ekstrand1f907012015-07-09 16:11:24 -0700538
Jason Ekstranda6eba402015-10-05 21:17:12 -0700539 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
Jason Ekstrand1f907012015-07-09 16:11:24 -0700540 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
541 VK_QUEUE_COMPUTE_BIT |
Jason Ekstrand6a8a5422015-11-30 11:12:44 -0800542 VK_QUEUE_TRANSFER_BIT,
Jason Ekstrand1f907012015-07-09 16:11:24 -0700543 .queueCount = 1,
Jason Ekstrand74c4c4a2015-12-02 16:20:40 -0800544 .timestampValidBits = 0, /* XXX: Real value here */
545 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
Jason Ekstrand1f907012015-07-09 16:11:24 -0700546 };
Jason Ekstrand1f907012015-07-09 16:11:24 -0700547}
548
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800549void anv_GetPhysicalDeviceMemoryProperties(
Chad Versacedf2a0132015-07-09 19:49:19 -0700550 VkPhysicalDevice physicalDevice,
551 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
552{
553 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700554 VkDeviceSize heap_size;
Chad Versacedf2a0132015-07-09 19:49:19 -0700555
556 /* Reserve some wiggle room for the driver by exposing only 75% of the
557 * aperture to the heap.
558 */
Kristian Høgsberg Kristensen9564dd32015-07-21 13:09:25 -0700559 heap_size = 3 * physical_device->aperture_size / 4;
Chad Versacedf2a0132015-07-09 19:49:19 -0700560
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800561 if (physical_device->info->has_llc) {
562 /* Big core GPUs share LLC with the CPU and thus one memory type can be
563 * both cached and coherent at the same time.
564 */
565 pMemoryProperties->memoryTypeCount = 1;
566 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
567 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
568 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
569 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
570 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800571 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800572 };
573 } else {
574 /* The spec requires that we expose a host-visible, coherent memory
575 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
576 * to give the application a choice between cached, but not coherent and
577 * coherent but uncached (WC though).
578 */
579 pMemoryProperties->memoryTypeCount = 2;
580 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
581 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
582 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
583 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800584 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800585 };
586 pMemoryProperties->memoryTypes[1] = (VkMemoryType) {
587 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
588 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
589 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
Jason Ekstrand3421ba12015-12-30 19:32:41 -0800590 .heapIndex = 0,
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -0800591 };
592 }
Chad Versacedf2a0132015-07-09 19:49:19 -0700593
594 pMemoryProperties->memoryHeapCount = 1;
595 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
596 .size = heap_size,
Jason Ekstrande6ab06a2015-12-02 10:39:15 -0800597 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
Chad Versacedf2a0132015-07-09 19:49:19 -0700598 };
Chad Versacedf2a0132015-07-09 19:49:19 -0700599}
600
Jason Ekstrande7acdda2015-07-07 18:51:53 -0700601PFN_vkVoidFunction anv_GetInstanceProcAddr(
602 VkInstance instance,
603 const char* pName)
604{
605 return anv_lookup_entrypoint(pName);
606}
607
608PFN_vkVoidFunction anv_GetDeviceProcAddr(
609 VkDevice device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700610 const char* pName)
611{
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700612 return anv_lookup_entrypoint(pName);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700613}
614
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700615static VkResult
616anv_queue_init(struct anv_device *device, struct anv_queue *queue)
617{
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700618 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700619 queue->device = device;
620 queue->pool = &device->surface_state_pool;
621
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700622 return VK_SUCCESS;
623}
624
625static void
626anv_queue_finish(struct anv_queue *queue)
627{
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700628}
629
Kristian Høgsberg77359202015-12-01 15:37:12 -0800630static struct anv_state
631anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
632{
633 struct anv_state state;
634
635 state = anv_state_pool_alloc(pool, size, align);
636 memcpy(state.map, p, size);
637
638 if (!pool->block_pool->device->info.has_llc)
639 anv_state_clflush(state);
640
641 return state;
642}
643
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700644static void
645anv_device_init_border_colors(struct anv_device *device)
646{
Jason Ekstrand522ab832015-07-08 11:44:52 -0700647 static const VkClearColorValue border_colors[] = {
Jason Ekstrandbd4cde72015-10-06 10:07:47 -0700648 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
649 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
650 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
651 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
652 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
653 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700654 };
655
Kristian Høgsberg77359202015-12-01 15:37:12 -0800656 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
657 sizeof(border_colors), 32, border_colors);
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700658}
659
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700660VkResult anv_CreateDevice(
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700661 VkPhysicalDevice physicalDevice,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700662 const VkDeviceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800663 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700664 VkDevice* pDevice)
665{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700666 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800667 VkResult result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700668 struct anv_device *device;
669
670 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
671
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800672 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700673 bool found = false;
674 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
675 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
Jason Ekstrandaadb7dc2015-11-30 21:10:14 -0800676 device_extensions[j].extensionName) == 0) {
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700677 found = true;
678 break;
679 }
680 }
681 if (!found)
Chad Versacef9c948e2015-10-07 11:36:51 -0700682 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
Jason Ekstrandb5f68892015-09-17 11:19:16 -0700683 }
684
Jason Ekstrandaa3002b2015-11-17 06:30:02 -0800685 anv_set_dispatch_devinfo(physical_device->info);
Kristian Høgsberg Kristensenee978892015-07-30 14:44:01 -0700686
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800687 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
688 sizeof(*device), 8,
689 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700690 if (!device)
691 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
692
Jason Ekstrand39cd3782015-09-24 13:51:40 -0700693 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700694 device->instance = physical_device->instance;
Chad Versace8cda3e92015-07-09 16:31:39 -0700695
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800696 if (pAllocator)
697 device->alloc = *pAllocator;
698 else
699 device->alloc = physical_device->instance->alloc;
700
Chad Versace8cda3e92015-07-09 16:31:39 -0700701 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700702 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800703 if (device->fd == -1) {
704 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700705 goto fail_device;
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800706 }
Chad Versace477383e2015-11-13 10:12:18 -0800707
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700708 device->context_id = anv_gem_create_context(device);
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800709 if (device->context_id == -1) {
710 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700711 goto fail_fd;
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800712 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700713
Jason Ekstrand9c84b6c2015-12-28 13:26:49 -0800714 device->info = *physical_device->info;
715 device->isl_dev = physical_device->isl_dev;
716
Jason Ekstranda788e7c2015-09-17 18:23:21 -0700717 pthread_mutex_init(&device->mutex, NULL);
718
Jason Ekstrand0c4a2da2015-07-30 11:34:09 -0700719 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700720
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700721 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700722
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700723 anv_state_pool_init(&device->dynamic_state_pool,
724 &device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700725
Jason Ekstrand28c4ef92015-12-15 11:49:26 -0800726 anv_block_pool_init(&device->instruction_block_pool, device, 8192);
Jason Ekstrand0e944462015-09-22 16:36:00 -0700727 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700728
729 anv_state_pool_init(&device->surface_state_pool,
730 &device->surface_state_block_pool);
731
Jason Ekstrand3a3d79b2015-11-10 16:42:34 -0800732 anv_bo_init_new(&device->workaround_bo, device, 1024);
733
Kristian Høgsberg Kristensen9b9f9732015-06-19 15:41:30 -0700734 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
735
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700736 anv_queue_init(device, &device->queue);
737
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800738 result = anv_device_init_meta(device);
739 if (result != VK_SUCCESS)
740 goto fail_fd;
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -0700741
Kristian Høgsberg Kristensendc56e4f2015-05-29 16:06:06 -0700742 anv_device_init_border_colors(device);
743
Jason Ekstrand098209e2015-07-09 18:41:27 -0700744 *pDevice = anv_device_to_handle(device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700745
746 return VK_SUCCESS;
747
748 fail_fd:
749 close(device->fd);
750 fail_device:
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800751 anv_free(&device->alloc, device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700752
Kristian Høgsberg Kristensen5526c172016-01-03 22:43:47 -0800753 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700754}
755
Jason Ekstrand05a26a62015-10-05 20:50:51 -0700756void anv_DestroyDevice(
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800757 VkDevice _device,
758 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700759{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700760 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700761
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700762 anv_queue_finish(&device->queue);
763
Jason Ekstrand3a38b0d2015-06-09 11:08:51 -0700764 anv_device_finish_meta(device);
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700765
Jason Ekstrand38f5eef2015-06-09 11:41:31 -0700766#ifdef HAVE_VALGRIND
767 /* We only need to free these to prevent valgrind errors. The backing
768 * BO will go away in a couple of lines so we don't actually leak.
769 */
Jason Ekstrand522ab832015-07-08 11:44:52 -0700770 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
Jason Ekstrand38f5eef2015-06-09 11:41:31 -0700771#endif
772
Jason Ekstrand3a3d79b2015-11-10 16:42:34 -0800773 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
774 anv_gem_close(device, device->workaround_bo.gem_handle);
775
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700776 anv_bo_pool_finish(&device->batch_bo_pool);
Jason Ekstrand1920ef92015-07-31 10:30:57 -0700777 anv_state_pool_finish(&device->dynamic_state_pool);
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700778 anv_block_pool_finish(&device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700779 anv_block_pool_finish(&device->instruction_block_pool);
Jason Ekstrand1920ef92015-07-31 10:30:57 -0700780 anv_state_pool_finish(&device->surface_state_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700781 anv_block_pool_finish(&device->surface_state_block_pool);
Jason Ekstrand3460e6c2015-07-22 17:51:14 -0700782 anv_block_pool_finish(&device->scratch_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700783
784 close(device->fd);
785
Kristian Høgsberg Kristensenf5510472016-01-05 11:43:25 -0800786 pthread_mutex_destroy(&device->mutex);
787
Jason Ekstrandfcfb4042015-12-02 03:28:27 -0800788 anv_free(&device->alloc, device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700789}
790
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700791VkResult anv_EnumerateInstanceExtensionProperties(
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700792 const char* pLayerName,
Jason Ekstrandfe644722015-11-30 16:28:36 -0800793 uint32_t* pPropertyCount,
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700794 VkExtensionProperties* pProperties)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700795{
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700796 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800797 *pPropertyCount = ARRAY_SIZE(global_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700798 return VK_SUCCESS;
799 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700800
Jason Ekstrandfe644722015-11-30 16:28:36 -0800801 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700802
Jason Ekstrandfe644722015-11-30 16:28:36 -0800803 *pPropertyCount = ARRAY_SIZE(global_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700804 memcpy(pProperties, global_extensions, sizeof(global_extensions));
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700805
806 return VK_SUCCESS;
807}
808
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700809VkResult anv_EnumerateDeviceExtensionProperties(
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700810 VkPhysicalDevice physicalDevice,
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700811 const char* pLayerName,
Jason Ekstrandfe644722015-11-30 16:28:36 -0800812 uint32_t* pPropertyCount,
Jason Ekstrand8e05bbe2015-07-08 10:38:07 -0700813 VkExtensionProperties* pProperties)
814{
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700815 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800816 *pPropertyCount = ARRAY_SIZE(device_extensions);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700817 return VK_SUCCESS;
818 }
819
Jason Ekstrandfe644722015-11-30 16:28:36 -0800820 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
Jason Ekstrand9a7600c2015-09-01 16:44:42 -0700821
Jason Ekstrandfe644722015-11-30 16:28:36 -0800822 *pPropertyCount = ARRAY_SIZE(device_extensions);
Jason Ekstrand9a7600c2015-09-01 16:44:42 -0700823 memcpy(pProperties, device_extensions, sizeof(device_extensions));
824
825 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700826}
827
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700828VkResult anv_EnumerateInstanceLayerProperties(
Jason Ekstrandfe644722015-11-30 16:28:36 -0800829 uint32_t* pPropertyCount,
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700830 VkLayerProperties* pProperties)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700831{
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700832 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800833 *pPropertyCount = 0;
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700834 return VK_SUCCESS;
835 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700836
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700837 /* None supported at this time */
Chad Versacef9c948e2015-10-07 11:36:51 -0700838 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700839}
840
Jason Ekstrand8ba684c2015-10-06 09:25:03 -0700841VkResult anv_EnumerateDeviceLayerProperties(
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700842 VkPhysicalDevice physicalDevice,
Jason Ekstrandfe644722015-11-30 16:28:36 -0800843 uint32_t* pPropertyCount,
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700844 VkLayerProperties* pProperties)
845{
846 if (pProperties == NULL) {
Jason Ekstrandfe644722015-11-30 16:28:36 -0800847 *pPropertyCount = 0;
Jason Ekstrand02db21a2015-07-14 16:11:21 -0700848 return VK_SUCCESS;
849 }
850
851 /* None supported at this time */
Chad Versacef9c948e2015-10-07 11:36:51 -0700852 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700853}
854
Jason Ekstrandf1a7c782015-11-30 12:21:19 -0800855void anv_GetDeviceQueue(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700856 VkDevice _device,
857 uint32_t queueNodeIndex,
858 uint32_t queueIndex,
859 VkQueue* pQueue)
860{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700861 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700862
Jason Ekstrand66b00d52015-06-09 12:28:58 -0700863 assert(queueIndex == 0);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700864
Jason Ekstrand098209e2015-07-09 18:41:27 -0700865 *pQueue = anv_queue_to_handle(&device->queue);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700866}
867
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700868VkResult anv_QueueSubmit(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700869 VkQueue _queue,
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800870 uint32_t submitCount,
871 const VkSubmitInfo* pSubmits,
Kristian Høgsberg6afb2642015-05-18 08:49:15 -0700872 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700873{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700874 ANV_FROM_HANDLE(anv_queue, queue, _queue);
875 ANV_FROM_HANDLE(anv_fence, fence, _fence);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700876 struct anv_device *device = queue->device;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700877 int ret;
878
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800879 for (uint32_t i = 0; i < submitCount; i++) {
880 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
881 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
882 pSubmits[i].pCommandBuffers[j]);
883 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700884
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800885 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
Chad Versacef9c948e2015-10-07 11:36:51 -0700886 if (ret != 0) {
887 /* We don't know the real error. */
888 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
889 "execbuf2 failed: %m");
890 }
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700891
Jason Ekstrand4e904a02015-12-02 17:18:41 -0800892 if (fence) {
893 ret = anv_gem_execbuffer(device, &fence->execbuf);
894 if (ret != 0) {
895 /* We don't know the real error. */
896 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
897 "execbuf2 failed: %m");
898 }
899 }
900
901 for (uint32_t k = 0; k < cmd_buffer->execbuf2.bo_count; k++)
902 cmd_buffer->execbuf2.bos[k]->offset = cmd_buffer->execbuf2.objects[k].offset;
903 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700904 }
905
906 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700907}
908
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700909VkResult anv_QueueWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700910 VkQueue _queue)
911{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700912 ANV_FROM_HANDLE(anv_queue, queue, _queue);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700913
Jason Ekstranda95f51c2015-09-24 14:20:35 -0700914 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700915}
916
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700917VkResult anv_DeviceWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700918 VkDevice _device)
919{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -0700920 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700921 struct anv_state state;
922 struct anv_batch batch;
923 struct drm_i915_gem_execbuffer2 execbuf;
924 struct drm_i915_gem_exec_object2 exec2_objects[1];
925 struct anv_bo *bo = NULL;
926 VkResult result;
927 int64_t timeout;
928 int ret;
929
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700930 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
931 bo = &device->dynamic_state_pool.block_pool->bo;
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700932 batch.start = batch.next = state.map;
933 batch.end = state.map + 32;
Kristian Høgsberg Kristensen74556b02015-08-13 21:05:47 -0700934 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
935 anv_batch_emit(&batch, GEN7_MI_NOOP);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700936
Kristian Høgsberg77359202015-12-01 15:37:12 -0800937 if (!device->info.has_llc)
938 anv_state_clflush(state);
939
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700940 exec2_objects[0].handle = bo->gem_handle;
941 exec2_objects[0].relocation_count = 0;
942 exec2_objects[0].relocs_ptr = 0;
943 exec2_objects[0].alignment = 0;
944 exec2_objects[0].offset = bo->offset;
945 exec2_objects[0].flags = 0;
946 exec2_objects[0].rsvd1 = 0;
947 exec2_objects[0].rsvd2 = 0;
948
949 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
950 execbuf.buffer_count = 1;
951 execbuf.batch_start_offset = state.offset;
952 execbuf.batch_len = batch.next - state.map;
953 execbuf.cliprects_ptr = 0;
954 execbuf.num_cliprects = 0;
955 execbuf.DR1 = 0;
956 execbuf.DR4 = 0;
957
958 execbuf.flags =
959 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
960 execbuf.rsvd1 = device->context_id;
961 execbuf.rsvd2 = 0;
962
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700963 ret = anv_gem_execbuffer(device, &execbuf);
964 if (ret != 0) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700965 /* We don't know the real error. */
966 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700967 goto fail;
968 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700969
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700970 timeout = INT64_MAX;
971 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
972 if (ret != 0) {
Chad Versacef9c948e2015-10-07 11:36:51 -0700973 /* We don't know the real error. */
974 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
Kristian Høgsberg Kristensenaac6f7c2015-08-14 09:39:01 -0700975 goto fail;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700976 }
977
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700978 anv_state_pool_free(&device->dynamic_state_pool, state);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700979
980 return VK_SUCCESS;
981
982 fail:
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700983 anv_state_pool_free(&device->dynamic_state_pool, state);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700984
985 return result;
986}
987
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700988VkResult
989anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
990{
991 bo->gem_handle = anv_gem_create(device, size);
992 if (!bo->gem_handle)
993 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
994
995 bo->map = NULL;
996 bo->index = 0;
997 bo->offset = 0;
998 bo->size = size;
999
1000 return VK_SUCCESS;
1001}
1002
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001003VkResult anv_AllocateMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001004 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001005 const VkMemoryAllocateInfo* pAllocateInfo,
1006 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001007 VkDeviceMemory* pMem)
1008{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001009 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001010 struct anv_device_memory *mem;
1011 VkResult result;
1012
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001013 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001014
Jason Ekstrandb1325402015-12-17 11:00:38 -08001015 if (pAllocateInfo->allocationSize == 0) {
1016 /* Apparently, this is allowed */
1017 *pMem = VK_NULL_HANDLE;
1018 return VK_SUCCESS;
1019 }
1020
Chad Versacef9c948e2015-10-07 11:36:51 -07001021 /* We support exactly one memory heap. */
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -08001022 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1023 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
Chad Versacef43a3042015-07-09 19:59:44 -07001024
1025 /* FINISHME: Fail if allocation request exceeds heap size. */
1026
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001027 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1028 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001029 if (mem == NULL)
1030 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1031
Jason Ekstrand6b0b5722016-01-02 07:52:22 -08001032 /* The kernel is going to give us whole pages anyway */
1033 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1034
1035 result = anv_bo_init_new(&mem->bo, device, alloc_size);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001036 if (result != VK_SUCCESS)
1037 goto fail;
1038
Kristian Høgsberg Kristensenc3c61d22015-12-03 23:09:09 -08001039 mem->type_index = pAllocateInfo->memoryTypeIndex;
1040
Jason Ekstrand098209e2015-07-09 18:41:27 -07001041 *pMem = anv_device_memory_to_handle(mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001042
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001043 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001044
1045 fail:
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001046 anv_free2(&device->alloc, pAllocator, mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001047
1048 return result;
1049}
1050
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001051void anv_FreeMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001052 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001053 VkDeviceMemory _mem,
1054 const VkAllocationCallbacks* pAllocator)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001055{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001056 ANV_FROM_HANDLE(anv_device, device, _device);
1057 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001058
Jason Ekstrandb1325402015-12-17 11:00:38 -08001059 if (mem == NULL)
1060 return;
1061
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001062 if (mem->bo.map)
1063 anv_gem_munmap(mem->bo.map, mem->bo.size);
1064
1065 if (mem->bo.gem_handle != 0)
1066 anv_gem_close(device, mem->bo.gem_handle);
1067
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001068 anv_free2(&device->alloc, pAllocator, mem);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001069}
1070
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001071VkResult anv_MapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001072 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001073 VkDeviceMemory _memory,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001074 VkDeviceSize offset,
1075 VkDeviceSize size,
1076 VkMemoryMapFlags flags,
1077 void** ppData)
1078{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001079 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001080 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001081
Jason Ekstrandb1325402015-12-17 11:00:38 -08001082 if (mem == NULL) {
1083 *ppData = NULL;
1084 return VK_SUCCESS;
1085 }
1086
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001087 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1088 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1089 * at a time is valid. We could just mmap up front and return an offset
1090 * pointer here, but that may exhaust virtual memory on 32 bit
1091 * userspace. */
1092
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -08001093 uint32_t gem_flags = 0;
1094 if (!device->info.has_llc && mem->type_index == 0)
1095 gem_flags |= I915_MMAP_WC;
1096
Jason Ekstrandf076d532016-01-01 09:26:06 -08001097 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1098 uint64_t map_offset = offset & ~4095ull;
1099 assert(offset >= map_offset);
1100 uint64_t map_size = (offset + size) - map_offset;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001101
Jason Ekstrandf076d532016-01-01 09:26:06 -08001102 /* Let's map whole pages */
Jason Ekstrand6b0b5722016-01-02 07:52:22 -08001103 map_size = align_u64(map_size, 4096);
Jason Ekstrandf076d532016-01-01 09:26:06 -08001104
1105 mem->map = anv_gem_mmap(device, mem->bo.gem_handle,
1106 map_offset, map_size, gem_flags);
1107 mem->map_size = map_size;
1108
1109 *ppData = mem->map + (offset - map_offset);
Chad Versace477383e2015-11-13 10:12:18 -08001110
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001111 return VK_SUCCESS;
1112}
1113
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001114void anv_UnmapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001115 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001116 VkDeviceMemory _memory)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001117{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001118 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001119
Jason Ekstrandb1325402015-12-17 11:00:38 -08001120 if (mem == NULL)
1121 return;
1122
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001123 anv_gem_munmap(mem->map, mem->map_size);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001124}
1125
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001126static void
1127clflush_mapped_ranges(struct anv_device *device,
1128 uint32_t count,
1129 const VkMappedMemoryRange *ranges)
1130{
1131 for (uint32_t i = 0; i < count; i++) {
1132 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1133 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
1134 void *end = mem->map + ranges[i].offset + ranges[i].size;
1135
1136 while (p < end) {
1137 __builtin_ia32_clflush(p);
1138 p += CACHELINE_SIZE;
1139 }
1140 }
1141}
1142
Jason Ekstrandd9c2cae2015-07-07 17:22:29 -07001143VkResult anv_FlushMappedMemoryRanges(
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001144 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001145 uint32_t memoryRangeCount,
1146 const VkMappedMemoryRange* pMemoryRanges)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001147{
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001148 ANV_FROM_HANDLE(anv_device, device, _device);
1149
1150 if (device->info.has_llc)
1151 return VK_SUCCESS;
1152
1153 /* Make sure the writes we're flushing have landed. */
1154 __builtin_ia32_sfence();
1155
1156 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001157
1158 return VK_SUCCESS;
1159}
1160
Jason Ekstrandd9c2cae2015-07-07 17:22:29 -07001161VkResult anv_InvalidateMappedMemoryRanges(
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001162 VkDevice _device,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001163 uint32_t memoryRangeCount,
1164 const VkMappedMemoryRange* pMemoryRanges)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001165{
Kristian Høgsberge0b5f032015-12-01 15:25:07 -08001166 ANV_FROM_HANDLE(anv_device, device, _device);
1167
1168 if (device->info.has_llc)
1169 return VK_SUCCESS;
1170
1171 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1172
1173 /* Make sure no reads get moved up above the invalidate. */
1174 __builtin_ia32_lfence();
1175
1176 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001177}
1178
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001179void anv_GetBufferMemoryRequirements(
Jason Ekstrandef8980e2015-07-07 18:16:42 -07001180 VkDevice device,
Jason Ekstrand55723e92015-07-14 14:59:39 -07001181 VkBuffer _buffer,
Jason Ekstrandef8980e2015-07-07 18:16:42 -07001182 VkMemoryRequirements* pMemoryRequirements)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001183{
Jason Ekstrand55723e92015-07-14 14:59:39 -07001184 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001185
Chad Versacef43a3042015-07-09 19:59:44 -07001186 /* The Vulkan spec (git aaed022) says:
1187 *
1188 * memoryTypeBits is a bitfield and contains one bit set for every
1189 * supported memory type for the resource. The bit `1<<i` is set if and
1190 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1191 * structure for the physical device is supported.
1192 *
1193 * We support exactly one memory type.
1194 */
1195 pMemoryRequirements->memoryTypeBits = 1;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001196
Jason Ekstrand55723e92015-07-14 14:59:39 -07001197 pMemoryRequirements->size = buffer->size;
1198 pMemoryRequirements->alignment = 16;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001199}
1200
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001201void anv_GetImageMemoryRequirements(
Jason Ekstrandbb6567f2015-07-08 09:04:16 -07001202 VkDevice device,
Jason Ekstrand55723e92015-07-14 14:59:39 -07001203 VkImage _image,
1204 VkMemoryRequirements* pMemoryRequirements)
1205{
1206 ANV_FROM_HANDLE(anv_image, image, _image);
1207
1208 /* The Vulkan spec (git aaed022) says:
1209 *
1210 * memoryTypeBits is a bitfield and contains one bit set for every
1211 * supported memory type for the resource. The bit `1<<i` is set if and
1212 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1213 * structure for the physical device is supported.
1214 *
1215 * We support exactly one memory type.
1216 */
1217 pMemoryRequirements->memoryTypeBits = 1;
1218
1219 pMemoryRequirements->size = image->size;
1220 pMemoryRequirements->alignment = image->alignment;
Jason Ekstrand55723e92015-07-14 14:59:39 -07001221}
1222
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001223void anv_GetImageSparseMemoryRequirements(
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001224 VkDevice device,
1225 VkImage image,
Jason Ekstrand5a024412015-12-02 03:34:43 -08001226 uint32_t* pSparseMemoryRequirementCount,
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001227 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1228{
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001229 stub();
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001230}
1231
Jason Ekstrandf1a7c782015-11-30 12:21:19 -08001232void anv_GetDeviceMemoryCommitment(
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001233 VkDevice device,
1234 VkDeviceMemory memory,
1235 VkDeviceSize* pCommittedMemoryInBytes)
1236{
1237 *pCommittedMemoryInBytes = 0;
Jason Ekstrandc7fcfeb2015-07-14 17:06:11 -07001238}
1239
Jason Ekstrand55723e92015-07-14 14:59:39 -07001240VkResult anv_BindBufferMemory(
1241 VkDevice device,
1242 VkBuffer _buffer,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001243 VkDeviceMemory _memory,
1244 VkDeviceSize memoryOffset)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001245{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001246 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Jason Ekstrand55723e92015-07-14 14:59:39 -07001247 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001248
Jason Ekstrandb1325402015-12-17 11:00:38 -08001249 if (mem) {
1250 buffer->bo = &mem->bo;
1251 buffer->offset = memoryOffset;
1252 } else {
1253 buffer->bo = NULL;
1254 buffer->offset = 0;
1255 }
Jason Ekstrand55723e92015-07-14 14:59:39 -07001256
1257 return VK_SUCCESS;
1258}
1259
1260VkResult anv_BindImageMemory(
1261 VkDevice device,
1262 VkImage _image,
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001263 VkDeviceMemory _memory,
1264 VkDeviceSize memoryOffset)
Jason Ekstrand55723e92015-07-14 14:59:39 -07001265{
Jason Ekstrand6a6da542015-11-30 21:18:12 -08001266 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
Jason Ekstrand55723e92015-07-14 14:59:39 -07001267 ANV_FROM_HANDLE(anv_image, image, _image);
1268
Jason Ekstrandb1325402015-12-17 11:00:38 -08001269 if (mem) {
1270 image->bo = &mem->bo;
1271 image->offset = memoryOffset;
1272 } else {
1273 image->bo = NULL;
1274 image->offset = 0;
1275 }
Jason Ekstrandbb6567f2015-07-08 09:04:16 -07001276
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001277 return VK_SUCCESS;
1278}
1279
Jason Ekstrandfd536032015-11-30 16:42:12 -08001280VkResult anv_QueueBindSparse(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001281 VkQueue queue,
Jason Ekstrandfd536032015-11-30 16:42:12 -08001282 uint32_t bindInfoCount,
1283 const VkBindSparseInfo* pBindInfo,
1284 VkFence fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001285{
Jason Ekstrandfed35862015-12-02 16:14:58 -08001286 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001287}
1288
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001289VkResult anv_CreateFence(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001290 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001291 const VkFenceCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001292 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001293 VkFence* pFence)
1294{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001295 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001296 struct anv_fence *fence;
1297 struct anv_batch batch;
1298 VkResult result;
1299
1300 const uint32_t fence_size = 128;
1301
1302 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1303
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001304 fence = anv_alloc2(&device->alloc, pAllocator, sizeof(*fence), 8,
1305 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001306 if (fence == NULL)
1307 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1308
1309 result = anv_bo_init_new(&fence->bo, device, fence_size);
1310 if (result != VK_SUCCESS)
1311 goto fail;
1312
1313 fence->bo.map =
Kristian Høgsberg Kristensenbbb68752015-12-03 23:58:05 -08001314 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size, 0);
Jason Ekstrandda8f1482015-05-27 11:42:55 -07001315 batch.next = batch.start = fence->bo.map;
1316 batch.end = fence->bo.map + fence->bo.size;
Kristian Høgsberg Kristensencff717c2015-08-18 11:04:19 -07001317 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1318 anv_batch_emit(&batch, GEN7_MI_NOOP);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001319
Kristian Høgsberg77359202015-12-01 15:37:12 -08001320 if (!device->info.has_llc) {
1321 assert(((uintptr_t) fence->bo.map & CACHELINE_MASK) == 0);
1322 assert(batch.next - fence->bo.map <= CACHELINE_SIZE);
1323 __builtin_ia32_sfence();
1324 __builtin_ia32_clflush(fence->bo.map);
1325 }
1326
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001327 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1328 fence->exec2_objects[0].relocation_count = 0;
1329 fence->exec2_objects[0].relocs_ptr = 0;
1330 fence->exec2_objects[0].alignment = 0;
1331 fence->exec2_objects[0].offset = fence->bo.offset;
1332 fence->exec2_objects[0].flags = 0;
1333 fence->exec2_objects[0].rsvd1 = 0;
1334 fence->exec2_objects[0].rsvd2 = 0;
1335
1336 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1337 fence->execbuf.buffer_count = 1;
1338 fence->execbuf.batch_start_offset = 0;
1339 fence->execbuf.batch_len = batch.next - fence->bo.map;
1340 fence->execbuf.cliprects_ptr = 0;
1341 fence->execbuf.num_cliprects = 0;
1342 fence->execbuf.DR1 = 0;
1343 fence->execbuf.DR4 = 0;
1344
1345 fence->execbuf.flags =
1346 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1347 fence->execbuf.rsvd1 = device->context_id;
1348 fence->execbuf.rsvd2 = 0;
1349
Jason Ekstrand098209e2015-07-09 18:41:27 -07001350 *pFence = anv_fence_to_handle(fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001351
1352 return VK_SUCCESS;
1353
1354 fail:
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001355 anv_free2(&device->alloc, pAllocator, fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001356
1357 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001358}
1359
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001360void anv_DestroyFence(
Chad Versaceebb191f2015-07-14 09:29:35 -07001361 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001362 VkFence _fence,
1363 const VkAllocationCallbacks* pAllocator)
Chad Versaceebb191f2015-07-14 09:29:35 -07001364{
1365 ANV_FROM_HANDLE(anv_device, device, _device);
1366 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1367
1368 anv_gem_munmap(fence->bo.map, fence->bo.size);
1369 anv_gem_close(device, fence->bo.gem_handle);
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001370 anv_free2(&device->alloc, pAllocator, fence);
Chad Versaceebb191f2015-07-14 09:29:35 -07001371}
1372
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001373VkResult anv_ResetFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001374 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001375 uint32_t fenceCount,
Jason Ekstrandd5349b12015-07-07 17:18:00 -07001376 const VkFence* pFences)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001377{
Chad Versace169251b2015-07-17 13:59:48 -07001378 for (uint32_t i = 0; i < fenceCount; i++) {
1379 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1380 fence->ready = false;
1381 }
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001382
1383 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001384}
1385
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001386VkResult anv_GetFenceStatus(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001387 VkDevice _device,
1388 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001389{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001390 ANV_FROM_HANDLE(anv_device, device, _device);
1391 ANV_FROM_HANDLE(anv_fence, fence, _fence);
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001392 int64_t t = 0;
1393 int ret;
1394
1395 if (fence->ready)
1396 return VK_SUCCESS;
1397
1398 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1399 if (ret == 0) {
1400 fence->ready = true;
1401 return VK_SUCCESS;
1402 }
Jason Ekstrand5c497302015-07-09 18:20:28 -07001403
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001404 return VK_NOT_READY;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001405}
1406
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001407VkResult anv_WaitForFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001408 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001409 uint32_t fenceCount,
1410 const VkFence* pFences,
Chad Versace8f3b2182015-07-13 12:59:42 -07001411 VkBool32 waitAll,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001412 uint64_t timeout)
1413{
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001414 ANV_FROM_HANDLE(anv_device, device, _device);
Jason Ekstrandaafc8742015-11-10 11:24:08 -08001415
1416 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1417 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1418 * for a couple of kernel releases. Since there's no way to know
1419 * whether or not the kernel we're using is one of the broken ones, the
1420 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1421 * maximum timeout from 584 years to 292 years - likely not a big deal.
1422 */
1423 if (timeout > INT64_MAX)
1424 timeout = INT64_MAX;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001425
Jason Ekstrand427978d2015-11-10 15:02:52 -08001426 int64_t t = timeout;
1427
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001428 /* FIXME: handle !waitAll */
1429
1430 for (uint32_t i = 0; i < fenceCount; i++) {
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001431 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
Jason Ekstrand427978d2015-11-10 15:02:52 -08001432 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
Chad Versacef9c948e2015-10-07 11:36:51 -07001433 if (ret == -1 && errno == ETIME) {
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001434 return VK_TIMEOUT;
Chad Versacef9c948e2015-10-07 11:36:51 -07001435 } else if (ret == -1) {
1436 /* We don't know the real error. */
1437 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1438 "gem wait failed: %m");
1439 }
Jason Ekstrand5c497302015-07-09 18:20:28 -07001440 }
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001441
1442 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001443}
1444
1445// Queue semaphore functions
1446
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001447VkResult anv_CreateSemaphore(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001448 VkDevice device,
1449 const VkSemaphoreCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001450 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001451 VkSemaphore* pSemaphore)
1452{
Kristian Høgsberg Kristensena00524a2015-12-20 22:58:38 -08001453 /* The DRM execbuffer ioctl always execute in-oder, even between different
1454 * rings. As such, there's nothing to do for the user space semaphore.
1455 */
1456
Jason Ekstrand3db43e82015-11-30 10:31:44 -08001457 *pSemaphore = (VkSemaphore)1;
Kristian Høgsberg Kristensena00524a2015-12-20 22:58:38 -08001458
1459 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001460}
1461
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001462void anv_DestroySemaphore(
Chad Versace549070b2015-07-14 09:31:34 -07001463 VkDevice device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001464 VkSemaphore semaphore,
1465 const VkAllocationCallbacks* pAllocator)
Chad Versace549070b2015-07-14 09:31:34 -07001466{
Chad Versace549070b2015-07-14 09:31:34 -07001467}
1468
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001469// Event functions
1470
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001471VkResult anv_CreateEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001472 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001473 const VkEventCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001474 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001475 VkEvent* pEvent)
1476{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001477 ANV_FROM_HANDLE(anv_device, device, _device);
1478 struct anv_state state;
1479 struct anv_event *event;
1480
1481 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
1482
1483 state = anv_state_pool_alloc(&device->dynamic_state_pool,
1484 sizeof(*event), 4);
1485 event = state.map;
1486 event->state = state;
1487 event->semaphore = VK_EVENT_RESET;
1488
1489 if (!device->info.has_llc) {
1490 /* Make sure the writes we're flushing have landed. */
1491 __builtin_ia32_sfence();
1492 __builtin_ia32_clflush(event);
1493 }
1494
1495 *pEvent = anv_event_to_handle(event);
1496
1497 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001498}
1499
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001500void anv_DestroyEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001501 VkDevice _device,
1502 VkEvent _event,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001503 const VkAllocationCallbacks* pAllocator)
Chad Versace68c7ef52015-07-14 09:33:47 -07001504{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001505 ANV_FROM_HANDLE(anv_device, device, _device);
1506 ANV_FROM_HANDLE(anv_event, event, _event);
1507
1508 anv_state_pool_free(&device->dynamic_state_pool, event->state);
Chad Versace68c7ef52015-07-14 09:33:47 -07001509}
1510
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001511VkResult anv_GetEventStatus(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001512 VkDevice _device,
1513 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001514{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001515 ANV_FROM_HANDLE(anv_device, device, _device);
1516 ANV_FROM_HANDLE(anv_event, event, _event);
1517
1518 if (!device->info.has_llc) {
1519 /* Make sure the writes we're flushing have landed. */
1520 __builtin_ia32_clflush(event);
1521 __builtin_ia32_lfence();
1522 }
1523
1524 return event->semaphore;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001525}
1526
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001527VkResult anv_SetEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001528 VkDevice _device,
1529 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001530{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001531 ANV_FROM_HANDLE(anv_device, device, _device);
1532 ANV_FROM_HANDLE(anv_event, event, _event);
1533
1534 event->semaphore = VK_EVENT_SET;
1535
1536 if (!device->info.has_llc) {
1537 /* Make sure the writes we're flushing have landed. */
1538 __builtin_ia32_sfence();
1539 __builtin_ia32_clflush(event);
1540 }
1541
1542 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001543}
1544
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001545VkResult anv_ResetEvent(
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001546 VkDevice _device,
1547 VkEvent _event)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001548{
Kristian Høgsberg Kristensenc4802bc2015-12-19 22:17:19 -08001549 ANV_FROM_HANDLE(anv_device, device, _device);
1550 ANV_FROM_HANDLE(anv_event, event, _event);
1551
1552 event->semaphore = VK_EVENT_RESET;
1553
1554 if (!device->info.has_llc) {
1555 /* Make sure the writes we're flushing have landed. */
1556 __builtin_ia32_sfence();
1557 __builtin_ia32_clflush(event);
1558 }
1559
1560 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001561}
1562
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001563// Buffer functions
1564
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001565VkResult anv_CreateBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001566 VkDevice _device,
1567 const VkBufferCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001568 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001569 VkBuffer* pBuffer)
1570{
Jason Ekstrandc8577b52015-07-08 14:24:12 -07001571 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001572 struct anv_buffer *buffer;
1573
1574 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1575
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001576 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1577 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001578 if (buffer == NULL)
1579 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1580
1581 buffer->size = pCreateInfo->size;
Jason Ekstrand783a2112015-12-14 16:51:12 -08001582 buffer->usage = pCreateInfo->usage;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07001583 buffer->bo = NULL;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001584 buffer->offset = 0;
1585
Jason Ekstrand098209e2015-07-09 18:41:27 -07001586 *pBuffer = anv_buffer_to_handle(buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001587
1588 return VK_SUCCESS;
1589}
1590
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001591void anv_DestroyBuffer(
Chad Versacee93b6d82015-07-14 09:47:45 -07001592 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001593 VkBuffer _buffer,
1594 const VkAllocationCallbacks* pAllocator)
Chad Versacee93b6d82015-07-14 09:47:45 -07001595{
1596 ANV_FROM_HANDLE(anv_device, device, _device);
1597 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1598
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001599 anv_free2(&device->alloc, pAllocator, buffer);
Chad Versacee93b6d82015-07-14 09:47:45 -07001600}
1601
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001602void
1603anv_fill_buffer_surface_state(struct anv_device *device, void *state,
Jason Ekstrand1f98bf82015-12-14 16:14:20 -08001604 enum isl_format format,
Jason Ekstrand399d5312015-11-06 15:14:10 -08001605 uint32_t offset, uint32_t range, uint32_t stride)
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001606{
1607 switch (device->info.gen) {
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -07001608 case 7:
Jason Ekstrandf0390bc2015-11-17 07:07:02 -08001609 if (device->info.is_haswell)
1610 gen75_fill_buffer_surface_state(state, format, offset, range, stride);
1611 else
1612 gen7_fill_buffer_surface_state(state, format, offset, range, stride);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -07001613 break;
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001614 case 8:
Jason Ekstrand399d5312015-11-06 15:14:10 -08001615 gen8_fill_buffer_surface_state(state, format, offset, range, stride);
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001616 break;
Kristian Høgsberg Kristensencd4721c2015-11-25 22:27:01 -08001617 case 9:
1618 gen9_fill_buffer_surface_state(state, format, offset, range, stride);
1619 break;
Kristian Høgsberg Kristensen8fe74ec2015-08-19 16:01:33 -07001620 default:
1621 unreachable("unsupported gen\n");
1622 }
1623}
1624
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001625void anv_DestroySampler(
Chad Versaceec5e2f42015-07-14 10:34:00 -07001626 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001627 VkSampler _sampler,
1628 const VkAllocationCallbacks* pAllocator)
Chad Versaceec5e2f42015-07-14 10:34:00 -07001629{
1630 ANV_FROM_HANDLE(anv_device, device, _device);
1631 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1632
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001633 anv_free2(&device->alloc, pAllocator, sampler);
Chad Versaceec5e2f42015-07-14 10:34:00 -07001634}
1635
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001636VkResult anv_CreateFramebuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001637 VkDevice _device,
1638 const VkFramebufferCreateInfo* pCreateInfo,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001639 const VkAllocationCallbacks* pAllocator,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001640 VkFramebuffer* pFramebuffer)
1641{
Jason Ekstrandc95f9b62015-07-09 18:20:10 -07001642 ANV_FROM_HANDLE(anv_device, device, _device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001643 struct anv_framebuffer *framebuffer;
1644
1645 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1646
Jason Ekstrand84783502015-07-10 20:18:52 -07001647 size_t size = sizeof(*framebuffer) +
Chad Versaced4446a72015-10-06 11:42:43 -07001648 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001649 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1650 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001651 if (framebuffer == NULL)
1652 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1653
Jason Ekstrand84783502015-07-10 20:18:52 -07001654 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1655 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
Chad Versace6dea1a92015-10-07 07:30:52 -07001656 VkImageView _iview = pCreateInfo->pAttachments[i];
Chad Versaced4446a72015-10-06 11:42:43 -07001657 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001658 }
1659
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001660 framebuffer->width = pCreateInfo->width;
1661 framebuffer->height = pCreateInfo->height;
1662 framebuffer->layers = pCreateInfo->layers;
1663
Jason Ekstrand098209e2015-07-09 18:41:27 -07001664 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001665
1666 return VK_SUCCESS;
1667}
1668
Jason Ekstrand05a26a62015-10-05 20:50:51 -07001669void anv_DestroyFramebuffer(
Chad Versace08f77312015-07-14 10:59:30 -07001670 VkDevice _device,
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001671 VkFramebuffer _fb,
1672 const VkAllocationCallbacks* pAllocator)
Chad Versace08f77312015-07-14 10:59:30 -07001673{
1674 ANV_FROM_HANDLE(anv_device, device, _device);
1675 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1676
Jason Ekstrandfcfb4042015-12-02 03:28:27 -08001677 anv_free2(&device->alloc, pAllocator, fb);
Chad Versace08f77312015-07-14 10:59:30 -07001678}
1679
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001680void vkCmdDbgMarkerBegin(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001681 VkCommandBuffer commandBuffer,
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001682 const char* pMarker)
1683 __attribute__ ((visibility ("default")));
1684
1685void vkCmdDbgMarkerEnd(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001686 VkCommandBuffer commandBuffer)
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001687 __attribute__ ((visibility ("default")));
1688
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001689void vkCmdDbgMarkerBegin(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001690 VkCommandBuffer commandBuffer,
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001691 const char* pMarker)
1692{
1693}
1694
1695void vkCmdDbgMarkerEnd(
Jason Ekstranda89a4852015-11-30 11:48:08 -08001696 VkCommandBuffer commandBuffer)
Kristian Høgsbergf8866472015-05-15 22:04:15 -07001697{
1698}