blob: ad3f6dbf4ab5174a152c3346f4606465226ad17a [file] [log] [blame]
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -06001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan 3-D graphics library
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
Chia-I Wu44e42362014-09-02 08:32:09 +080025 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 * Chia-I Wu <olv@lunarg.com>
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060027 */
28
Chia-I Wu023ef682014-09-15 11:06:50 +080029#include "icd-enumerate-drm.h"
Chia-I Wua6e33492014-08-05 13:35:08 +080030#include "gpu.h"
Chia-I Wu94ae71a2015-02-20 12:26:08 -070031#include "instance.h"
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060032
Chia-I Wu73019ad2014-08-29 12:01:13 +080033static int intel_devid_override;
Chia-I Wu1c527012014-08-23 14:57:35 +080034int intel_debug = -1;
35
36static void intel_debug_init(void)
37{
38 const char *env;
39
40 if (intel_debug >= 0)
41 return;
42
43 intel_debug = 0;
44
45 /* parse comma-separated debug options */
46 env = getenv("INTEL_DEBUG");
47 while (env) {
48 const char *p = strchr(env, ',');
49 size_t len;
50
51 if (p)
52 len = p - env;
53 else
54 len = strlen(env);
55
Courtney Goeltzenleuchterd9fc9842014-10-13 12:58:25 -060056 if (len > 0) {
57 if (strncmp(env, "batch", len) == 0) {
58 intel_debug |= INTEL_DEBUG_BATCH;
59 } else if (strncmp(env, "nohw", len) == 0) {
60 intel_debug |= INTEL_DEBUG_NOHW;
Chia-I Wu3fb47ce2014-10-28 11:19:36 +080061 } else if (strncmp(env, "nocache", len) == 0) {
62 intel_debug |= INTEL_DEBUG_NOCACHE;
Chia-I Wuc45db532015-02-19 11:20:38 -070063 } else if (strncmp(env, "nohiz", len) == 0) {
64 intel_debug |= INTEL_DEBUG_NOHIZ;
Chia-I Wu465fe212015-02-11 11:27:06 -070065 } else if (strncmp(env, "hang", len) == 0) {
66 intel_debug |= INTEL_DEBUG_HANG;
Courtney Goeltzenleuchterd9fc9842014-10-13 12:58:25 -060067 } else if (strncmp(env, "0x", 2) == 0) {
68 intel_debug |= INTEL_DEBUG_NOHW;
69 intel_devid_override = strtol(env, NULL, 16);
70 }
Chia-I Wu73019ad2014-08-29 12:01:13 +080071 }
Chia-I Wu1c527012014-08-23 14:57:35 +080072
73 if (!p)
74 break;
75
76 env = p + 1;
77 }
78}
Jon Ashburn815bddd2014-10-16 15:48:50 -060079
Chia-I Wud71ff552015-02-20 12:50:12 -070080static void intel_instance_add_gpu(struct intel_instance *instance,
81 struct intel_gpu *gpu)
82{
83 gpu->next = instance->gpus;
84 instance->gpus = gpu;
85}
86
87static void intel_instance_remove_gpus(struct intel_instance *instance)
88{
89 struct intel_gpu *gpu = instance->gpus;
90
91 while (gpu) {
92 struct intel_gpu *next = gpu->next;
93
94 intel_gpu_destroy(gpu);
95 gpu = next;
96 }
97
98 instance->gpus = NULL;
99}
100
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700101static void intel_instance_destroy(struct intel_instance *instance)
Jon Ashburnfa836e02015-01-28 18:26:16 -0700102{
Chia-I Wu96a41bc2015-02-21 14:19:23 +0800103 struct icd_instance *icd = instance->icd;
104
Chia-I Wud71ff552015-02-20 12:50:12 -0700105 intel_instance_remove_gpus(instance);
Chia-I Wu96a41bc2015-02-21 14:19:23 +0800106 icd_instance_free(icd, instance);
107
108 icd_instance_destroy(icd);
Jon Ashburnfa836e02015-01-28 18:26:16 -0700109}
110
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600111static struct intel_instance *intel_instance_create(const VkInstanceCreateInfo* info)
Jon Ashburnfa836e02015-01-28 18:26:16 -0700112{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700113 struct intel_instance *instance;
Chia-I Wu96a41bc2015-02-21 14:19:23 +0800114 struct icd_instance *icd;
Jon Ashburn29669a42015-04-04 14:52:07 -0600115 uint32_t i;
Jon Ashburnfa836e02015-01-28 18:26:16 -0700116
117 intel_debug_init();
118
Jon Ashburn29669a42015-04-04 14:52:07 -0600119 icd = icd_instance_create(info->pAppInfo, info->pAllocCb);
Chia-I Wu96a41bc2015-02-21 14:19:23 +0800120 if (!icd)
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700121 return NULL;
122
Chia-I Wu96a41bc2015-02-21 14:19:23 +0800123 instance = icd_instance_alloc(icd, sizeof(*instance), 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600124 VK_SYSTEM_ALLOC_API_OBJECT);
Chia-I Wu96a41bc2015-02-21 14:19:23 +0800125 if (!instance) {
126 icd_instance_destroy(icd);
127 return NULL;
128 }
129
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700130 memset(instance, 0, sizeof(*instance));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600131 intel_handle_init(&instance->handle, VK_DBG_OBJECT_INSTANCE, icd);
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700132
Chia-I Wu96a41bc2015-02-21 14:19:23 +0800133 instance->icd = icd;
134
Jon Ashburn29669a42015-04-04 14:52:07 -0600135 for (i = 0; i < info->extensionCount; i++) {
136 const enum intel_ext_type ext = intel_gpu_lookup_extension(NULL,
137 info->ppEnabledExtensionNames[i]);
138
139 if (ext != INTEL_EXT_INVALID)
140 instance->exts[ext] = true;
141 }
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700142 return instance;
Jon Ashburnfa836e02015-01-28 18:26:16 -0700143}
144
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600145ICD_EXPORT VK_RESULT VKAPI vkCreateInstance(
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600146 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600147 VK_INSTANCE* pInstance)
Jon Ashburn349508d2015-01-26 14:51:40 -0700148{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700149 struct intel_instance *instance;
150
Jon Ashburn29669a42015-04-04 14:52:07 -0600151 instance = intel_instance_create(pCreateInfo);
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700152 if (!instance)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600153 return VK_ERROR_OUT_OF_MEMORY;
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700154
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600155 *pInstance = (VK_INSTANCE) instance;
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700156
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600157 return VK_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -0700158}
159
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600160ICD_EXPORT VK_RESULT VKAPI vkDestroyInstance(
161 VK_INSTANCE pInstance)
Jon Ashburn349508d2015-01-26 14:51:40 -0700162{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700163 struct intel_instance *instance = intel_instance(pInstance);
164
165 intel_instance_destroy(instance);
166
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600167 return VK_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -0700168}
169
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600170ICD_EXPORT VK_RESULT VKAPI vkEnumerateGpus(
171 VK_INSTANCE instance_,
Jon Ashburn349508d2015-01-26 14:51:40 -0700172 uint32_t maxGpus,
173 uint32_t* pGpuCount,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600174 VK_PHYSICAL_GPU* pGpus)
Jon Ashburn349508d2015-01-26 14:51:40 -0700175{
Chia-I Wud71ff552015-02-20 12:50:12 -0700176 struct intel_instance *instance = intel_instance(instance_);
Jon Ashburn5f078e92015-01-28 19:15:45 -0700177 struct icd_drm_device *devices, *dev;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600178 VK_RESULT ret;
Jon Ashburn92e80132015-01-29 15:47:01 -0700179 uint32_t count;
Jon Ashburn5f078e92015-01-28 19:15:45 -0700180
Chia-I Wud71ff552015-02-20 12:50:12 -0700181 intel_instance_remove_gpus(instance);
Chia-I Wu80792d92015-02-20 13:10:25 -0700182
Jon Ashburn5f078e92015-01-28 19:15:45 -0700183 if (!maxGpus) {
184 *pGpuCount = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600185 return VK_SUCCESS;
Jon Ashburn5f078e92015-01-28 19:15:45 -0700186 }
187
Chia-I Wu15517d82015-02-20 13:41:57 -0700188 devices = icd_drm_enumerate(instance->icd, 0x8086);
Jon Ashburn5f078e92015-01-28 19:15:45 -0700189
190 count = 0;
191 dev = devices;
192 while (dev) {
193 const char *primary_node, *render_node;
194 int devid;
195 struct intel_gpu *gpu;
196
197 primary_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_LEGACY);
198 if (!primary_node)
199 continue;
200
201 render_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_RENDER);
202
203 devid = (intel_devid_override) ? intel_devid_override : dev->devid;
Chia-I Wud71ff552015-02-20 12:50:12 -0700204 ret = intel_gpu_create(instance, devid,
205 primary_node, render_node, &gpu);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600206 if (ret == VK_SUCCESS) {
Chia-I Wud71ff552015-02-20 12:50:12 -0700207 intel_instance_add_gpu(instance, gpu);
208
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600209 pGpus[count++] = (VK_PHYSICAL_GPU) gpu;
Jon Ashburn5f078e92015-01-28 19:15:45 -0700210 if (count >= maxGpus)
211 break;
212 }
213
214 dev = dev->next;
215 }
216
Chia-I Wu15517d82015-02-20 13:41:57 -0700217 icd_drm_release(instance->icd, devices);
Jon Ashburn5f078e92015-01-28 19:15:45 -0700218
219 *pGpuCount = count;
220
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600221 return (count > 0) ? VK_SUCCESS : VK_ERROR_UNAVAILABLE;
Jon Ashburn349508d2015-01-26 14:51:40 -0700222}
223
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600224ICD_EXPORT VK_RESULT VKAPI vkDbgRegisterMsgCallback(
225 VK_INSTANCE instance_,
226 VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600227 void* pUserData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800228{
Courtney Goeltzenleuchter0629efa2015-04-13 14:59:19 -0600229 struct intel_instance *instance = intel_instance(instance_);
230
231 return icd_instance_add_logger(instance->icd, pfnMsgCallback, pUserData);
Chia-I Wub02558c2015-01-03 15:21:51 +0800232}
233
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600234ICD_EXPORT VK_RESULT VKAPI vkDbgUnregisterMsgCallback(
235 VK_INSTANCE instance_,
236 VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wub02558c2015-01-03 15:21:51 +0800237{
Courtney Goeltzenleuchter0629efa2015-04-13 14:59:19 -0600238 struct intel_instance *instance = intel_instance(instance_);
239
240 return icd_instance_remove_logger(instance->icd, pfnMsgCallback);
Chia-I Wub02558c2015-01-03 15:21:51 +0800241}
242
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600243ICD_EXPORT VK_RESULT VKAPI vkDbgSetGlobalOption(
244 VK_INSTANCE instance_,
245 VK_DBG_GLOBAL_OPTION dbgOption,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600246 size_t dataSize,
247 const void* pData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800248{
Courtney Goeltzenleuchter0629efa2015-04-13 14:59:19 -0600249 struct intel_instance *instance = intel_instance(instance_);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600250 VK_RESULT res = VK_SUCCESS;
Chia-I Wub02558c2015-01-03 15:21:51 +0800251
252 if (dataSize == 0)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600253 return VK_ERROR_INVALID_VALUE;
Chia-I Wub02558c2015-01-03 15:21:51 +0800254
255 switch (dbgOption) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600256 case VK_DBG_OPTION_DEBUG_ECHO_ENABLE:
257 case VK_DBG_OPTION_BREAK_ON_ERROR:
258 case VK_DBG_OPTION_BREAK_ON_WARNING:
Courtney Goeltzenleuchter0629efa2015-04-13 14:59:19 -0600259 res = icd_instance_set_bool(instance->icd, dbgOption,
260 *((const bool *) pData));
Chia-I Wub02558c2015-01-03 15:21:51 +0800261 break;
262 default:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600263 res = VK_ERROR_INVALID_VALUE;
Chia-I Wub02558c2015-01-03 15:21:51 +0800264 break;
265 }
266
267 return res;
268}