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 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
| 32 | |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 33 | #include <stdio.h> |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <assert.h> |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 37 | #include <fcntl.h> |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 38 | #include <unistd.h> |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 39 | #include <fnmatch.h> |
| 40 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 41 | #include <libudev.h> |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 42 | |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 43 | #include "gpu.h" |
| 44 | #include "intel.h" |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 45 | |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 46 | static int intel_devid_override; |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 47 | int intel_debug = -1; |
| 48 | |
| 49 | static void intel_debug_init(void) |
| 50 | { |
| 51 | const char *env; |
| 52 | |
| 53 | if (intel_debug >= 0) |
| 54 | return; |
| 55 | |
| 56 | intel_debug = 0; |
| 57 | |
| 58 | /* parse comma-separated debug options */ |
| 59 | env = getenv("INTEL_DEBUG"); |
| 60 | while (env) { |
| 61 | const char *p = strchr(env, ','); |
| 62 | size_t len; |
| 63 | |
| 64 | if (p) |
| 65 | len = p - env; |
| 66 | else |
| 67 | len = strlen(env); |
| 68 | |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 69 | if (strncmp(env, "batch", len) == 0) { |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 70 | intel_debug |= INTEL_DEBUG_BATCH; |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 71 | } else if (strncmp(env, "nohw", len) == 0) { |
Chia-I Wu | 93ada31 | 2014-08-23 15:02:25 +0800 | [diff] [blame] | 72 | intel_debug |= INTEL_DEBUG_NOHW; |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 73 | } else if (strncmp(env, "0x", 2) == 0) { |
| 74 | intel_debug |= INTEL_DEBUG_NOHW; |
| 75 | intel_devid_override = strtol(env, NULL, 16); |
| 76 | } |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 77 | |
| 78 | if (!p) |
| 79 | break; |
| 80 | |
| 81 | env = p + 1; |
| 82 | } |
| 83 | } |
| 84 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 85 | static int is_render_node(int fd, struct stat *st) |
| 86 | { |
| 87 | if (fstat(fd, st)) |
| 88 | return 0; |
| 89 | |
| 90 | if (!S_ISCHR(st->st_mode)) |
| 91 | return 0; |
| 92 | |
| 93 | return st->st_rdev & 0x80; |
| 94 | } |
| 95 | |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 96 | ICD_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO * |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 97 | pAppInfo, |
| 98 | const XGL_ALLOC_CALLBACKS * |
| 99 | pAllocCb, XGL_UINT maxGpus, |
| 100 | XGL_UINT * pGpuCount, |
| 101 | XGL_PHYSICAL_GPU * pGpus) |
| 102 | { |
| 103 | struct udev *udev; |
| 104 | struct udev_enumerate *e; |
| 105 | struct udev_device *device, *parent; |
| 106 | struct udev_list_entry *entry; |
| 107 | const char *pci_id, *path; |
| 108 | const char *usub, *dnode; |
| 109 | int fd; |
| 110 | struct stat st; |
| 111 | char *pci_glob = "*:*"; |
Chia-I Wu | 3065c9c | 2014-08-04 06:28:31 +0800 | [diff] [blame] | 112 | XGL_RESULT ret; |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 113 | XGL_UINT count = 0; |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 114 | |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 115 | intel_debug_init(); |
| 116 | |
Chia-I Wu | c217f80 | 2014-08-05 10:17:50 +0800 | [diff] [blame] | 117 | ret = icd_set_allocator(pAllocCb); |
Chia-I Wu | 3065c9c | 2014-08-04 06:28:31 +0800 | [diff] [blame] | 118 | if (ret != XGL_SUCCESS) |
| 119 | return ret; |
| 120 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 121 | /* |
| 122 | * xglInitAndEnumerateGpus() can be called multiple times. Calling it more than once |
| 123 | * forces driver reinitialization. |
| 124 | */ |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 125 | intel_gpu_remove_all(); |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 126 | |
Chia-I Wu | c7bff53 | 2014-08-06 12:14:54 +0800 | [diff] [blame] | 127 | if (!maxGpus) { |
| 128 | *pGpuCount = 0; |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 129 | return XGL_SUCCESS; |
Chia-I Wu | c7bff53 | 2014-08-06 12:14:54 +0800 | [diff] [blame] | 130 | } |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 131 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 132 | // TODO: Do we need any other validation for incoming pointers? |
| 133 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 134 | udev = udev_new(); |
| 135 | if (udev == NULL) { |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 136 | icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, |
| 137 | XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context"); |
| 138 | return XGL_ERROR_OUT_OF_MEMORY; |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 139 | } |
| 140 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 141 | fd = -1; |
| 142 | e = udev_enumerate_new(udev); |
| 143 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 144 | udev_enumerate_scan_devices(e); |
| 145 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 146 | unsigned int ven_id, dev_id; |
| 147 | struct intel_gpu *gpu; |
| 148 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 149 | path = udev_list_entry_get_name(entry); |
| 150 | device = udev_device_new_from_syspath(udev, path); |
| 151 | parent = udev_device_get_parent(device); |
| 152 | usub = udev_device_get_subsystem(parent); |
| 153 | /* Filter out KMS output devices. */ |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 154 | if (!usub || (strcmp(usub, "pci") != 0)) { |
| 155 | udev_device_unref(device); |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 156 | continue; |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 157 | } |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 158 | pci_id = udev_device_get_property_value(parent, "PCI_ID"); |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 159 | if (fnmatch(pci_glob, pci_id, 0) != 0) { |
| 160 | udev_device_unref(device); |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 161 | continue; |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 162 | } |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 163 | sscanf(pci_id, "%x:%x", &ven_id, &dev_id); |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 164 | if (ven_id != 0x8086) { |
| 165 | udev_device_unref(device); |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 166 | continue; |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 167 | } |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 168 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 169 | dnode = udev_device_get_devnode(device); |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 170 | /* TODO do not open the device at this point */ |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 171 | fd = open(dnode, O_RDWR); |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 172 | if (fd < 0) { |
| 173 | udev_device_unref(device); |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 174 | continue; |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 175 | } |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 176 | if (!is_render_node(fd, &st)) { |
| 177 | close(fd); |
| 178 | fd = -1; |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 179 | udev_device_unref(device); |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 180 | continue; |
| 181 | } |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 182 | close(fd); |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 183 | |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 184 | if (intel_devid_override) |
| 185 | dev_id = intel_devid_override; |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 186 | ret = intel_gpu_add(dev_id, dnode, &gpu); |
Chia-I Wu | 78462db | 2014-08-06 12:23:55 +0800 | [diff] [blame] | 187 | |
| 188 | udev_device_unref(device); |
| 189 | |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 190 | if (ret == XGL_SUCCESS) { |
| 191 | pGpus[count++] = (XGL_PHYSICAL_GPU) gpu; |
| 192 | if (count >= maxGpus) |
| 193 | break; |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 194 | } |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 195 | } |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 196 | |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 197 | udev_enumerate_unref(e); |
| 198 | udev_unref(udev); |
| 199 | |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 200 | *pGpuCount = count; |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 201 | |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 202 | return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE; |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 203 | } |