blob: c5b4f630cbc7ac25745e12a2486154fbf215c555 [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 Wu023ef682014-09-15 11:06:50 +080029#include "icd-enumerate-drm.h"
Chia-I Wua6e33492014-08-05 13:35:08 +080030#include "gpu.h"
31#include "intel.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 Wu465fe212015-02-11 11:27:06 -070063 } else if (strncmp(env, "hang", len) == 0) {
64 intel_debug |= INTEL_DEBUG_HANG;
Courtney Goeltzenleuchterd9fc9842014-10-13 12:58:25 -060065 } else if (strncmp(env, "0x", 2) == 0) {
66 intel_debug |= INTEL_DEBUG_NOHW;
67 intel_devid_override = strtol(env, NULL, 16);
68 }
Chia-I Wu73019ad2014-08-29 12:01:13 +080069 }
Chia-I Wu1c527012014-08-23 14:57:35 +080070
71 if (!p)
72 break;
73
74 env = p + 1;
75 }
76}
Jon Ashburn815bddd2014-10-16 15:48:50 -060077
Jon Ashburnfa836e02015-01-28 18:26:16 -070078static XGL_RESULT intel_instance_destroy(struct intel_instance *inst)
79{
80 intel_base_destroy(&inst->obj.base);
81 intel_gpu_remove_all();
82 return XGL_SUCCESS;
83}
84
85static void inst_destroy(struct intel_obj *obj)
86{
87 struct intel_instance *inst = intel_instance_from_obj(obj);
88
89 intel_instance_destroy(inst);
90}
91
92static XGL_RESULT intel_instance_create(
93 const XGL_APPLICATION_INFO* info,
94 const XGL_ALLOC_CALLBACKS* cb,
95 struct intel_instance** inst_ret)
96{
97 struct intel_instance *inst;
98 XGL_RESULT ret;
99
100 inst = (struct intel_instance *) intel_base_create(NULL, sizeof(*inst), false,
101 XGL_DBG_OBJECT_INSTANCE, info, 0);
102 if (!inst)
103 return XGL_ERROR_OUT_OF_MEMORY;
104
105 inst->obj.destroy = inst_destroy;
106 inst->obj.base.get_info = intel_base_get_info;
107
108 *inst_ret = inst;
109
110 intel_debug_init();
111
112 ret = icd_allocator_init(cb);
113 return ret;
114}
115
Jon Ashburn349508d2015-01-26 14:51:40 -0700116ICD_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
117 const XGL_APPLICATION_INFO* pAppInfo,
118 const XGL_ALLOC_CALLBACKS* pAllocCb,
119 XGL_INSTANCE* pInstance)
120{
Jon Ashburnfa836e02015-01-28 18:26:16 -0700121 return intel_instance_create(pAppInfo, pAllocCb, (struct intel_instance **) pInstance);
Jon Ashburn349508d2015-01-26 14:51:40 -0700122}
123
124ICD_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
125 XGL_INSTANCE pInstance)
126{
Jon Ashburnfa836e02015-01-28 18:26:16 -0700127 return intel_instance_destroy((struct intel_instance *) pInstance);
Jon Ashburn349508d2015-01-26 14:51:40 -0700128}
129
130ICD_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
131 XGL_INSTANCE instance,
132 uint32_t maxGpus,
133 uint32_t* pGpuCount,
134 XGL_PHYSICAL_GPU* pGpus)
135{
Jon Ashburn5f078e92015-01-28 19:15:45 -0700136 struct icd_drm_device *devices, *dev;
137 XGL_RESULT ret;
Jon Ashburn92e80132015-01-29 15:47:01 -0700138 uint32_t count;
Jon Ashburn5f078e92015-01-28 19:15:45 -0700139
140 if (!maxGpus) {
141 *pGpuCount = 0;
142 return XGL_SUCCESS;
143 }
144
145 devices = icd_drm_enumerate(0x8086);
146
147 count = 0;
148 dev = devices;
149 while (dev) {
150 const char *primary_node, *render_node;
151 int devid;
152 struct intel_gpu *gpu;
153
154 primary_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_LEGACY);
155 if (!primary_node)
156 continue;
157
158 render_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_RENDER);
159
160 devid = (intel_devid_override) ? intel_devid_override : dev->devid;
161 ret = intel_gpu_add(devid, primary_node, render_node, &gpu);
162 if (ret == XGL_SUCCESS) {
163 pGpus[count++] = (XGL_PHYSICAL_GPU) gpu;
164 if (count >= maxGpus)
165 break;
166 }
167
168 dev = dev->next;
169 }
170
171 icd_drm_release(devices);
172
173 *pGpuCount = count;
174
175 return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE;
Jon Ashburn349508d2015-01-26 14:51:40 -0700176}
177
Chia-I Wu64b5aa82015-01-03 23:36:36 +0800178ICD_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(
179 XGL_PHYSICAL_GPU gpu,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600180 size_t maxLayerCount,
181 size_t maxStringSize,
182 size_t* pOutLayerCount,
183 char* const* pOutLayers,
184 void* pReserved)
Chia-I Wu64b5aa82015-01-03 23:36:36 +0800185{
186 if (!pOutLayerCount)
187 return XGL_ERROR_INVALID_POINTER;
188
189 *pOutLayerCount = 0;
190
191 return XGL_SUCCESS;
192}
193
Chia-I Wu96177272015-01-03 15:27:41 +0800194ICD_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(
Chia-I Wub02558c2015-01-03 15:21:51 +0800195 XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600196 void* pUserData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800197{
198 return icd_logger_add_callback(pfnMsgCallback, pUserData);
199}
200
Chia-I Wu96177272015-01-03 15:27:41 +0800201ICD_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(
Chia-I Wub02558c2015-01-03 15:21:51 +0800202 XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
203{
204 return icd_logger_remove_callback(pfnMsgCallback);
205}
206
Chia-I Wu96177272015-01-03 15:27:41 +0800207ICD_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(
Chia-I Wub02558c2015-01-03 15:21:51 +0800208 XGL_DBG_GLOBAL_OPTION dbgOption,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600209 size_t dataSize,
210 const void* pData)
Chia-I Wub02558c2015-01-03 15:21:51 +0800211{
212 XGL_RESULT res = XGL_SUCCESS;
213
214 if (dataSize == 0)
215 return XGL_ERROR_INVALID_VALUE;
216
217 switch (dbgOption) {
218 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
219 case XGL_DBG_OPTION_BREAK_ON_ERROR:
220 case XGL_DBG_OPTION_BREAK_ON_WARNING:
221 res = icd_logger_set_bool(dbgOption, *((const bool *) pData));
222 break;
223 default:
224 res = XGL_ERROR_INVALID_VALUE;
225 break;
226 }
227
228 return res;
229}