blob: 9e2c56be022a04b7d107b7a6725e0dd50133e486 [file] [log] [blame]
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -06001/*
2 * XGL 3-D graphics library
3 *
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 Wu94ae71a2015-02-20 12:26:08 -070029#include "xglIcd.h"
Chia-I Wu023ef682014-09-15 11:06:50 +080030#include "icd-enumerate-drm.h"
Chia-I Wua6e33492014-08-05 13:35:08 +080031#include "gpu.h"
Chia-I Wu94ae71a2015-02-20 12:26:08 -070032#include "instance.h"
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060033
Chia-I Wu73019ad2014-08-29 12:01:13 +080034static int intel_devid_override;
Chia-I Wu1c527012014-08-23 14:57:35 +080035int intel_debug = -1;
36
37static void intel_debug_init(void)
38{
39 const char *env;
40
41 if (intel_debug >= 0)
42 return;
43
44 intel_debug = 0;
45
46 /* parse comma-separated debug options */
47 env = getenv("INTEL_DEBUG");
48 while (env) {
49 const char *p = strchr(env, ',');
50 size_t len;
51
52 if (p)
53 len = p - env;
54 else
55 len = strlen(env);
56
Courtney Goeltzenleuchterd9fc9842014-10-13 12:58:25 -060057 if (len > 0) {
58 if (strncmp(env, "batch", len) == 0) {
59 intel_debug |= INTEL_DEBUG_BATCH;
60 } else if (strncmp(env, "nohw", len) == 0) {
61 intel_debug |= INTEL_DEBUG_NOHW;
Chia-I Wu3fb47ce2014-10-28 11:19:36 +080062 } else if (strncmp(env, "nocache", len) == 0) {
63 intel_debug |= INTEL_DEBUG_NOCACHE;
Chia-I Wuc45db532015-02-19 11:20:38 -070064 } else if (strncmp(env, "nohiz", len) == 0) {
65 intel_debug |= INTEL_DEBUG_NOHIZ;
Chia-I Wu465fe212015-02-11 11:27:06 -070066 } else if (strncmp(env, "hang", len) == 0) {
67 intel_debug |= INTEL_DEBUG_HANG;
Courtney Goeltzenleuchterd9fc9842014-10-13 12:58:25 -060068 } else if (strncmp(env, "0x", 2) == 0) {
69 intel_debug |= INTEL_DEBUG_NOHW;
70 intel_devid_override = strtol(env, NULL, 16);
71 }
Chia-I Wu73019ad2014-08-29 12:01:13 +080072 }
Chia-I Wu1c527012014-08-23 14:57:35 +080073
74 if (!p)
75 break;
76
77 env = p + 1;
78 }
79}
Jon Ashburn815bddd2014-10-16 15:48:50 -060080
Chia-I Wud71ff552015-02-20 12:50:12 -070081static void intel_instance_add_gpu(struct intel_instance *instance,
82 struct intel_gpu *gpu)
83{
84 gpu->next = instance->gpus;
85 instance->gpus = gpu;
86}
87
88static void intel_instance_remove_gpus(struct intel_instance *instance)
89{
90 struct intel_gpu *gpu = instance->gpus;
91
92 while (gpu) {
93 struct intel_gpu *next = gpu->next;
94
95 intel_gpu_destroy(gpu);
96 gpu = next;
97 }
98
99 instance->gpus = NULL;
100}
101
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700102static void intel_instance_destroy(struct intel_instance *instance)
Jon Ashburnfa836e02015-01-28 18:26:16 -0700103{
Chia-I Wud71ff552015-02-20 12:50:12 -0700104 intel_instance_remove_gpus(instance);
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700105 icd_free(instance);
Jon Ashburnfa836e02015-01-28 18:26:16 -0700106}
107
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700108static struct intel_instance *intel_instance_create(const XGL_APPLICATION_INFO *app_info,
109 const XGL_ALLOC_CALLBACKS *alloc_cb)
Jon Ashburnfa836e02015-01-28 18:26:16 -0700110{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700111 struct intel_instance *instance;
Jon Ashburnfa836e02015-01-28 18:26:16 -0700112
113 intel_debug_init();
114
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700115 instance = icd_alloc(sizeof(*instance), 0, XGL_SYSTEM_ALLOC_API_OBJECT);
116 if (!instance)
117 return NULL;
118
119 memset(instance, 0, sizeof(*instance));
120 set_loader_magic_value(instance);
121
122 icd_allocator_init(alloc_cb);
123
124 return instance;
Jon Ashburnfa836e02015-01-28 18:26:16 -0700125}
126
Jon Ashburn349508d2015-01-26 14:51:40 -0700127ICD_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
128 const XGL_APPLICATION_INFO* pAppInfo,
129 const XGL_ALLOC_CALLBACKS* pAllocCb,
130 XGL_INSTANCE* pInstance)
131{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700132 struct intel_instance *instance;
133
134 instance = intel_instance_create(pAppInfo, pAllocCb);
135 if (!instance)
136 return XGL_ERROR_OUT_OF_MEMORY;
137
138 *pInstance = (XGL_INSTANCE) instance;
139
140 return XGL_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -0700141}
142
143ICD_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
144 XGL_INSTANCE pInstance)
145{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700146 struct intel_instance *instance = intel_instance(pInstance);
147
148 intel_instance_destroy(instance);
149
150 return XGL_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -0700151}
152
153ICD_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
Chia-I Wud71ff552015-02-20 12:50:12 -0700154 XGL_INSTANCE instance_,
Jon Ashburn349508d2015-01-26 14:51:40 -0700155 uint32_t maxGpus,
156 uint32_t* pGpuCount,
157 XGL_PHYSICAL_GPU* pGpus)
158{
Chia-I Wud71ff552015-02-20 12:50:12 -0700159 struct intel_instance *instance = intel_instance(instance_);
Jon Ashburn5f078e92015-01-28 19:15:45 -0700160 struct icd_drm_device *devices, *dev;
161 XGL_RESULT ret;
Jon Ashburn92e80132015-01-29 15:47:01 -0700162 uint32_t count;
Jon Ashburn5f078e92015-01-28 19:15:45 -0700163
Chia-I Wud71ff552015-02-20 12:50:12 -0700164 intel_instance_remove_gpus(instance);
Chia-I Wu80792d92015-02-20 13:10:25 -0700165
Jon Ashburn5f078e92015-01-28 19:15:45 -0700166 if (!maxGpus) {
167 *pGpuCount = 0;
168 return XGL_SUCCESS;
169 }
170
171 devices = icd_drm_enumerate(0x8086);
172
173 count = 0;
174 dev = devices;
175 while (dev) {
176 const char *primary_node, *render_node;
177 int devid;
178 struct intel_gpu *gpu;
179
180 primary_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_LEGACY);
181 if (!primary_node)
182 continue;
183
184 render_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_RENDER);
185
186 devid = (intel_devid_override) ? intel_devid_override : dev->devid;
Chia-I Wud71ff552015-02-20 12:50:12 -0700187 ret = intel_gpu_create(instance, devid,
188 primary_node, render_node, &gpu);
Jon Ashburn5f078e92015-01-28 19:15:45 -0700189 if (ret == XGL_SUCCESS) {
Chia-I Wud71ff552015-02-20 12:50:12 -0700190 intel_instance_add_gpu(instance, gpu);
191
Jon Ashburn5f078e92015-01-28 19:15:45 -0700192 pGpus[count++] = (XGL_PHYSICAL_GPU) gpu;
193 if (count >= maxGpus)
194 break;
195 }
196
197 dev = dev->next;
198 }
199
200 icd_drm_release(devices);
201
202 *pGpuCount = count;
203
204 return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE;
Jon Ashburn349508d2015-01-26 14:51:40 -0700205}
206
Chia-I Wu96177272015-01-03 15:27:41 +0800207ICD_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(
Chia-I Wub02558c2015-01-03 15:21:51 +0800208 XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600209 void* pUserData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800210{
211 return icd_logger_add_callback(pfnMsgCallback, pUserData);
212}
213
Chia-I Wu96177272015-01-03 15:27:41 +0800214ICD_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(
Chia-I Wub02558c2015-01-03 15:21:51 +0800215 XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
216{
217 return icd_logger_remove_callback(pfnMsgCallback);
218}
219
Chia-I Wu96177272015-01-03 15:27:41 +0800220ICD_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(
Chia-I Wub02558c2015-01-03 15:21:51 +0800221 XGL_DBG_GLOBAL_OPTION dbgOption,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600222 size_t dataSize,
223 const void* pData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800224{
225 XGL_RESULT res = XGL_SUCCESS;
226
227 if (dataSize == 0)
228 return XGL_ERROR_INVALID_VALUE;
229
230 switch (dbgOption) {
231 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
232 case XGL_DBG_OPTION_BREAK_ON_ERROR:
233 case XGL_DBG_OPTION_BREAK_ON_WARNING:
234 res = icd_logger_set_bool(dbgOption, *((const bool *) pData));
235 break;
236 default:
237 res = XGL_ERROR_INVALID_VALUE;
238 break;
239 }
240
241 return res;
242}