blob: ab19566214017e81dd53211b251be23ec337488c [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 Wu94ae71a2015-02-20 12:26:08 -070081static void intel_instance_destroy(struct intel_instance *instance)
Jon Ashburnfa836e02015-01-28 18:26:16 -070082{
Jon Ashburnfa836e02015-01-28 18:26:16 -070083 intel_gpu_remove_all();
Chia-I Wu94ae71a2015-02-20 12:26:08 -070084 icd_free(instance);
Jon Ashburnfa836e02015-01-28 18:26:16 -070085}
86
Chia-I Wu94ae71a2015-02-20 12:26:08 -070087static struct intel_instance *intel_instance_create(const XGL_APPLICATION_INFO *app_info,
88 const XGL_ALLOC_CALLBACKS *alloc_cb)
Jon Ashburnfa836e02015-01-28 18:26:16 -070089{
Chia-I Wu94ae71a2015-02-20 12:26:08 -070090 struct intel_instance *instance;
Jon Ashburnfa836e02015-01-28 18:26:16 -070091
92 intel_debug_init();
93
Chia-I Wu94ae71a2015-02-20 12:26:08 -070094 instance = icd_alloc(sizeof(*instance), 0, XGL_SYSTEM_ALLOC_API_OBJECT);
95 if (!instance)
96 return NULL;
97
98 memset(instance, 0, sizeof(*instance));
99 set_loader_magic_value(instance);
100
101 icd_allocator_init(alloc_cb);
102
103 return instance;
Jon Ashburnfa836e02015-01-28 18:26:16 -0700104}
105
Jon Ashburn349508d2015-01-26 14:51:40 -0700106ICD_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
107 const XGL_APPLICATION_INFO* pAppInfo,
108 const XGL_ALLOC_CALLBACKS* pAllocCb,
109 XGL_INSTANCE* pInstance)
110{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700111 struct intel_instance *instance;
112
113 instance = intel_instance_create(pAppInfo, pAllocCb);
114 if (!instance)
115 return XGL_ERROR_OUT_OF_MEMORY;
116
117 *pInstance = (XGL_INSTANCE) instance;
118
119 return XGL_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -0700120}
121
122ICD_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
123 XGL_INSTANCE pInstance)
124{
Chia-I Wu94ae71a2015-02-20 12:26:08 -0700125 struct intel_instance *instance = intel_instance(pInstance);
126
127 intel_instance_destroy(instance);
128
129 return XGL_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -0700130}
131
132ICD_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
133 XGL_INSTANCE instance,
134 uint32_t maxGpus,
135 uint32_t* pGpuCount,
136 XGL_PHYSICAL_GPU* pGpus)
137{
Jon Ashburn5f078e92015-01-28 19:15:45 -0700138 struct icd_drm_device *devices, *dev;
139 XGL_RESULT ret;
Jon Ashburn92e80132015-01-29 15:47:01 -0700140 uint32_t count;
Jon Ashburn5f078e92015-01-28 19:15:45 -0700141
Chia-I Wu80792d92015-02-20 13:10:25 -0700142 intel_gpu_remove_all();
143
Jon Ashburn5f078e92015-01-28 19:15:45 -0700144 if (!maxGpus) {
145 *pGpuCount = 0;
146 return XGL_SUCCESS;
147 }
148
149 devices = icd_drm_enumerate(0x8086);
150
151 count = 0;
152 dev = devices;
153 while (dev) {
154 const char *primary_node, *render_node;
155 int devid;
156 struct intel_gpu *gpu;
157
158 primary_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_LEGACY);
159 if (!primary_node)
160 continue;
161
162 render_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_RENDER);
163
164 devid = (intel_devid_override) ? intel_devid_override : dev->devid;
165 ret = intel_gpu_add(devid, primary_node, render_node, &gpu);
166 if (ret == XGL_SUCCESS) {
167 pGpus[count++] = (XGL_PHYSICAL_GPU) gpu;
168 if (count >= maxGpus)
169 break;
170 }
171
172 dev = dev->next;
173 }
174
175 icd_drm_release(devices);
176
177 *pGpuCount = count;
178
179 return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE;
Jon Ashburn349508d2015-01-26 14:51:40 -0700180}
181
Chia-I Wu96177272015-01-03 15:27:41 +0800182ICD_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(
Chia-I Wub02558c2015-01-03 15:21:51 +0800183 XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600184 void* pUserData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800185{
186 return icd_logger_add_callback(pfnMsgCallback, pUserData);
187}
188
Chia-I Wu96177272015-01-03 15:27:41 +0800189ICD_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(
Chia-I Wub02558c2015-01-03 15:21:51 +0800190 XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
191{
192 return icd_logger_remove_callback(pfnMsgCallback);
193}
194
Chia-I Wu96177272015-01-03 15:27:41 +0800195ICD_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(
Chia-I Wub02558c2015-01-03 15:21:51 +0800196 XGL_DBG_GLOBAL_OPTION dbgOption,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600197 size_t dataSize,
198 const void* pData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800199{
200 XGL_RESULT res = XGL_SUCCESS;
201
202 if (dataSize == 0)
203 return XGL_ERROR_INVALID_VALUE;
204
205 switch (dbgOption) {
206 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
207 case XGL_DBG_OPTION_BREAK_ON_ERROR:
208 case XGL_DBG_OPTION_BREAK_ON_WARNING:
209 res = icd_logger_set_bool(dbgOption, *((const bool *) pData));
210 break;
211 default:
212 res = XGL_ERROR_INVALID_VALUE;
213 break;
214 }
215
216 return res;
217}