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