blob: 21f872051b738a4a568c95c727b6736ca58c3766 [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:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
Chia-I Wua6e33492014-08-05 13:35:08 +080032#include <stdio.h>
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060033#include <sys/types.h>
34#include <sys/stat.h>
35#include <assert.h>
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060036#include <fcntl.h>
Chia-I Wua6e33492014-08-05 13:35:08 +080037#include <unistd.h>
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060038#include <fnmatch.h>
39
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060040#include <libudev.h>
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060041
Chia-I Wua6e33492014-08-05 13:35:08 +080042#include "gpu.h"
43#include "intel.h"
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060044
Chia-I Wu1c527012014-08-23 14:57:35 +080045int intel_debug = -1;
46
47static 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;
Chia-I Wu93ada312014-08-23 15:02:25 +080069 else if (strncmp(env, "nohw", len) == 0)
70 intel_debug |= INTEL_DEBUG_NOHW;
Chia-I Wu1c527012014-08-23 14:57:35 +080071
72 if (!p)
73 break;
74
75 env = p + 1;
76 }
77}
78
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060079static int is_render_node(int fd, struct stat *st)
80{
81 if (fstat(fd, st))
82 return 0;
83
84 if (!S_ISCHR(st->st_mode))
85 return 0;
86
87 return st->st_rdev & 0x80;
88}
89
Chia-I Wu468e3c32014-08-04 08:03:57 +080090ICD_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO *
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060091 pAppInfo,
92 const XGL_ALLOC_CALLBACKS *
93 pAllocCb, XGL_UINT maxGpus,
94 XGL_UINT * pGpuCount,
95 XGL_PHYSICAL_GPU * pGpus)
96{
97 struct udev *udev;
98 struct udev_enumerate *e;
99 struct udev_device *device, *parent;
100 struct udev_list_entry *entry;
101 const char *pci_id, *path;
102 const char *usub, *dnode;
103 int fd;
104 struct stat st;
105 char *pci_glob = "*:*";
Chia-I Wu3065c9c2014-08-04 06:28:31 +0800106 XGL_RESULT ret;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600107 XGL_UINT count = 0;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600108
Chia-I Wu1c527012014-08-23 14:57:35 +0800109 intel_debug_init();
110
Chia-I Wuc217f802014-08-05 10:17:50 +0800111 ret = icd_set_allocator(pAllocCb);
Chia-I Wu3065c9c2014-08-04 06:28:31 +0800112 if (ret != XGL_SUCCESS)
113 return ret;
114
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600115 /*
116 * xglInitAndEnumerateGpus() can be called multiple times. Calling it more than once
117 * forces driver reinitialization.
118 */
Chia-I Wua6e33492014-08-05 13:35:08 +0800119 intel_gpu_remove_all();
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600120
Chia-I Wuc7bff532014-08-06 12:14:54 +0800121 if (!maxGpus) {
122 *pGpuCount = 0;
Chia-I Wua6e33492014-08-05 13:35:08 +0800123 return XGL_SUCCESS;
Chia-I Wuc7bff532014-08-06 12:14:54 +0800124 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800125
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600126 // TODO: Do we need any other validation for incoming pointers?
127
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600128 udev = udev_new();
129 if (udev == NULL) {
Chia-I Wua6e33492014-08-05 13:35:08 +0800130 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
131 XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
132 return XGL_ERROR_OUT_OF_MEMORY;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600133 }
134
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600135 fd = -1;
136 e = udev_enumerate_new(udev);
137 udev_enumerate_add_match_subsystem(e, "drm");
138 udev_enumerate_scan_devices(e);
139 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Chia-I Wua6e33492014-08-05 13:35:08 +0800140 unsigned int ven_id, dev_id;
141 struct intel_gpu *gpu;
142
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600143 path = udev_list_entry_get_name(entry);
144 device = udev_device_new_from_syspath(udev, path);
145 parent = udev_device_get_parent(device);
146 usub = udev_device_get_subsystem(parent);
147 /* Filter out KMS output devices. */
Chia-I Wu78462db2014-08-06 12:23:55 +0800148 if (!usub || (strcmp(usub, "pci") != 0)) {
149 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600150 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800151 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600152 pci_id = udev_device_get_property_value(parent, "PCI_ID");
Chia-I Wu78462db2014-08-06 12:23:55 +0800153 if (fnmatch(pci_glob, pci_id, 0) != 0) {
154 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600155 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800156 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800157 sscanf(pci_id, "%x:%x", &ven_id, &dev_id);
Chia-I Wu78462db2014-08-06 12:23:55 +0800158 if (ven_id != 0x8086) {
159 udev_device_unref(device);
Chia-I Wua6e33492014-08-05 13:35:08 +0800160 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800161 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800162
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600163 dnode = udev_device_get_devnode(device);
Chia-I Wua6e33492014-08-05 13:35:08 +0800164 /* TODO do not open the device at this point */
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600165 fd = open(dnode, O_RDWR);
Chia-I Wu78462db2014-08-06 12:23:55 +0800166 if (fd < 0) {
167 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600168 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800169 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600170 if (!is_render_node(fd, &st)) {
171 close(fd);
172 fd = -1;
Chia-I Wu78462db2014-08-06 12:23:55 +0800173 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600174 continue;
175 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800176 close(fd);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600177
Chia-I Wua6e33492014-08-05 13:35:08 +0800178 ret = intel_gpu_add(dev_id, dnode, &gpu);
Chia-I Wu78462db2014-08-06 12:23:55 +0800179
180 udev_device_unref(device);
181
Chia-I Wua6e33492014-08-05 13:35:08 +0800182 if (ret == XGL_SUCCESS) {
183 pGpus[count++] = (XGL_PHYSICAL_GPU) gpu;
184 if (count >= maxGpus)
185 break;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600186 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600187 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800188
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600189 udev_enumerate_unref(e);
190 udev_unref(udev);
191
Chia-I Wua6e33492014-08-05 13:35:08 +0800192 *pGpuCount = count;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600193
Chia-I Wua6e33492014-08-05 13:35:08 +0800194 return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600195}