Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 1 | /* |
| 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 Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 25 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
| 26 | * Chia-I Wu <olv@lunarg.com> |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 27 | */ |
| 28 | |
Chia-I Wu | 023ef68 | 2014-09-15 11:06:50 +0800 | [diff] [blame] | 29 | #include "icd-enumerate-drm.h" |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 30 | #include "gpu.h" |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 31 | #include "instance.h" |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 32 | |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 33 | static int intel_devid_override; |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 34 | int intel_debug = -1; |
| 35 | |
| 36 | static 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 Goeltzenleuchter | d9fc984 | 2014-10-13 12:58:25 -0600 | [diff] [blame] | 56 | 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 Wu | 3fb47ce | 2014-10-28 11:19:36 +0800 | [diff] [blame] | 61 | } else if (strncmp(env, "nocache", len) == 0) { |
| 62 | intel_debug |= INTEL_DEBUG_NOCACHE; |
Chia-I Wu | c45db53 | 2015-02-19 11:20:38 -0700 | [diff] [blame] | 63 | } else if (strncmp(env, "nohiz", len) == 0) { |
| 64 | intel_debug |= INTEL_DEBUG_NOHIZ; |
Chia-I Wu | 465fe21 | 2015-02-11 11:27:06 -0700 | [diff] [blame] | 65 | } else if (strncmp(env, "hang", len) == 0) { |
| 66 | intel_debug |= INTEL_DEBUG_HANG; |
Courtney Goeltzenleuchter | d9fc984 | 2014-10-13 12:58:25 -0600 | [diff] [blame] | 67 | } else if (strncmp(env, "0x", 2) == 0) { |
| 68 | intel_debug |= INTEL_DEBUG_NOHW; |
| 69 | intel_devid_override = strtol(env, NULL, 16); |
| 70 | } |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 71 | } |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 72 | |
| 73 | if (!p) |
| 74 | break; |
| 75 | |
| 76 | env = p + 1; |
| 77 | } |
| 78 | } |
Jon Ashburn | 815bddd | 2014-10-16 15:48:50 -0600 | [diff] [blame] | 79 | |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 80 | static 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 | |
| 87 | static 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 Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 101 | static void intel_instance_destroy(struct intel_instance *instance) |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 102 | { |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 103 | struct icd_instance *icd = instance->icd; |
| 104 | |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 105 | intel_instance_remove_gpus(instance); |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 106 | icd_instance_free(icd, instance); |
| 107 | |
| 108 | icd_instance_destroy(icd); |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 111 | static struct intel_instance *intel_instance_create(const XGL_APPLICATION_INFO *app_info, |
| 112 | const XGL_ALLOC_CALLBACKS *alloc_cb) |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 113 | { |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 114 | struct intel_instance *instance; |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 115 | struct icd_instance *icd; |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 116 | |
| 117 | intel_debug_init(); |
| 118 | |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 119 | icd = icd_instance_create(app_info, alloc_cb); |
| 120 | if (!icd) |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 121 | return NULL; |
| 122 | |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 123 | instance = icd_instance_alloc(icd, sizeof(*instance), 0, |
| 124 | XGL_SYSTEM_ALLOC_API_OBJECT); |
| 125 | if (!instance) { |
| 126 | icd_instance_destroy(icd); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 130 | memset(instance, 0, sizeof(*instance)); |
Chia-I Wu | 032a2e3 | 2015-01-19 11:14:00 +0800 | [diff] [blame^] | 131 | intel_handle_init(&instance->handle, XGL_DBG_OBJECT_INSTANCE, icd); |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 132 | |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 133 | instance->icd = icd; |
| 134 | |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 135 | icd_allocator_init(alloc_cb); |
| 136 | |
| 137 | return instance; |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 140 | ICD_EXPORT XGL_RESULT XGLAPI xglCreateInstance( |
| 141 | const XGL_APPLICATION_INFO* pAppInfo, |
| 142 | const XGL_ALLOC_CALLBACKS* pAllocCb, |
| 143 | XGL_INSTANCE* pInstance) |
| 144 | { |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 145 | struct intel_instance *instance; |
| 146 | |
| 147 | instance = intel_instance_create(pAppInfo, pAllocCb); |
| 148 | if (!instance) |
| 149 | return XGL_ERROR_OUT_OF_MEMORY; |
| 150 | |
| 151 | *pInstance = (XGL_INSTANCE) instance; |
| 152 | |
| 153 | return XGL_SUCCESS; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | ICD_EXPORT XGL_RESULT XGLAPI xglDestroyInstance( |
| 157 | XGL_INSTANCE pInstance) |
| 158 | { |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 159 | struct intel_instance *instance = intel_instance(pInstance); |
| 160 | |
| 161 | intel_instance_destroy(instance); |
| 162 | |
| 163 | return XGL_SUCCESS; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | ICD_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus( |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 167 | XGL_INSTANCE instance_, |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 168 | uint32_t maxGpus, |
| 169 | uint32_t* pGpuCount, |
| 170 | XGL_PHYSICAL_GPU* pGpus) |
| 171 | { |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 172 | struct intel_instance *instance = intel_instance(instance_); |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 173 | struct icd_drm_device *devices, *dev; |
| 174 | XGL_RESULT ret; |
Jon Ashburn | 92e8013 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 175 | uint32_t count; |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 176 | |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 177 | intel_instance_remove_gpus(instance); |
Chia-I Wu | 80792d9 | 2015-02-20 13:10:25 -0700 | [diff] [blame] | 178 | |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 179 | if (!maxGpus) { |
| 180 | *pGpuCount = 0; |
| 181 | return XGL_SUCCESS; |
| 182 | } |
| 183 | |
Chia-I Wu | 15517d8 | 2015-02-20 13:41:57 -0700 | [diff] [blame] | 184 | devices = icd_drm_enumerate(instance->icd, 0x8086); |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 185 | |
| 186 | count = 0; |
| 187 | dev = devices; |
| 188 | while (dev) { |
| 189 | const char *primary_node, *render_node; |
| 190 | int devid; |
| 191 | struct intel_gpu *gpu; |
| 192 | |
| 193 | primary_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_LEGACY); |
| 194 | if (!primary_node) |
| 195 | continue; |
| 196 | |
| 197 | render_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_RENDER); |
| 198 | |
| 199 | devid = (intel_devid_override) ? intel_devid_override : dev->devid; |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 200 | ret = intel_gpu_create(instance, devid, |
| 201 | primary_node, render_node, &gpu); |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 202 | if (ret == XGL_SUCCESS) { |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 203 | intel_instance_add_gpu(instance, gpu); |
| 204 | |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 205 | pGpus[count++] = (XGL_PHYSICAL_GPU) gpu; |
| 206 | if (count >= maxGpus) |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | dev = dev->next; |
| 211 | } |
| 212 | |
Chia-I Wu | 15517d8 | 2015-02-20 13:41:57 -0700 | [diff] [blame] | 213 | icd_drm_release(instance->icd, devices); |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 214 | |
| 215 | *pGpuCount = count; |
| 216 | |
| 217 | return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Chia-I Wu | 9617727 | 2015-01-03 15:27:41 +0800 | [diff] [blame] | 220 | ICD_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback( |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 221 | XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 222 | void* pUserData) |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 223 | { |
| 224 | return icd_logger_add_callback(pfnMsgCallback, pUserData); |
| 225 | } |
| 226 | |
Chia-I Wu | 9617727 | 2015-01-03 15:27:41 +0800 | [diff] [blame] | 227 | ICD_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback( |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 228 | XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback) |
| 229 | { |
| 230 | return icd_logger_remove_callback(pfnMsgCallback); |
| 231 | } |
| 232 | |
Chia-I Wu | 9617727 | 2015-01-03 15:27:41 +0800 | [diff] [blame] | 233 | ICD_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption( |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 234 | XGL_DBG_GLOBAL_OPTION dbgOption, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 235 | size_t dataSize, |
| 236 | const void* pData) |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 237 | { |
| 238 | XGL_RESULT res = XGL_SUCCESS; |
| 239 | |
| 240 | if (dataSize == 0) |
| 241 | return XGL_ERROR_INVALID_VALUE; |
| 242 | |
| 243 | switch (dbgOption) { |
| 244 | case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE: |
| 245 | case XGL_DBG_OPTION_BREAK_ON_ERROR: |
| 246 | case XGL_DBG_OPTION_BREAK_ON_WARNING: |
| 247 | res = icd_logger_set_bool(dbgOption, *((const bool *) pData)); |
| 248 | break; |
| 249 | default: |
| 250 | res = XGL_ERROR_INVALID_VALUE; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | return res; |
| 255 | } |