blob: d0b8a5af34fe50c4c00d20522cbb2043c456a3db [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
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060045static int is_render_node(int fd, struct stat *st)
46{
47 if (fstat(fd, st))
48 return 0;
49
50 if (!S_ISCHR(st->st_mode))
51 return 0;
52
53 return st->st_rdev & 0x80;
54}
55
Chia-I Wu468e3c32014-08-04 08:03:57 +080056ICD_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO *
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060057 pAppInfo,
58 const XGL_ALLOC_CALLBACKS *
59 pAllocCb, XGL_UINT maxGpus,
60 XGL_UINT * pGpuCount,
61 XGL_PHYSICAL_GPU * pGpus)
62{
63 struct udev *udev;
64 struct udev_enumerate *e;
65 struct udev_device *device, *parent;
66 struct udev_list_entry *entry;
67 const char *pci_id, *path;
68 const char *usub, *dnode;
69 int fd;
70 struct stat st;
71 char *pci_glob = "*:*";
Chia-I Wu3065c9c2014-08-04 06:28:31 +080072 XGL_RESULT ret;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060073 XGL_UINT count = 0;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060074
Chia-I Wuc217f802014-08-05 10:17:50 +080075 ret = icd_set_allocator(pAllocCb);
Chia-I Wu3065c9c2014-08-04 06:28:31 +080076 if (ret != XGL_SUCCESS)
77 return ret;
78
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060079 /*
80 * xglInitAndEnumerateGpus() can be called multiple times. Calling it more than once
81 * forces driver reinitialization.
82 */
Chia-I Wua6e33492014-08-05 13:35:08 +080083 intel_gpu_remove_all();
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060084
Chia-I Wuc7bff532014-08-06 12:14:54 +080085 if (!maxGpus) {
86 *pGpuCount = 0;
Chia-I Wua6e33492014-08-05 13:35:08 +080087 return XGL_SUCCESS;
Chia-I Wuc7bff532014-08-06 12:14:54 +080088 }
Chia-I Wua6e33492014-08-05 13:35:08 +080089
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060090 // TODO: Do we need any other validation for incoming pointers?
91
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060092 udev = udev_new();
93 if (udev == NULL) {
Chia-I Wua6e33492014-08-05 13:35:08 +080094 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
95 XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
96 return XGL_ERROR_OUT_OF_MEMORY;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060097 }
98
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060099 fd = -1;
100 e = udev_enumerate_new(udev);
101 udev_enumerate_add_match_subsystem(e, "drm");
102 udev_enumerate_scan_devices(e);
103 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Chia-I Wua6e33492014-08-05 13:35:08 +0800104 unsigned int ven_id, dev_id;
105 struct intel_gpu *gpu;
106
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600107 path = udev_list_entry_get_name(entry);
108 device = udev_device_new_from_syspath(udev, path);
109 parent = udev_device_get_parent(device);
110 usub = udev_device_get_subsystem(parent);
111 /* Filter out KMS output devices. */
Chia-I Wu78462db2014-08-06 12:23:55 +0800112 if (!usub || (strcmp(usub, "pci") != 0)) {
113 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600114 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800115 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600116 pci_id = udev_device_get_property_value(parent, "PCI_ID");
Chia-I Wu78462db2014-08-06 12:23:55 +0800117 if (fnmatch(pci_glob, pci_id, 0) != 0) {
118 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600119 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800120 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800121 sscanf(pci_id, "%x:%x", &ven_id, &dev_id);
Chia-I Wu78462db2014-08-06 12:23:55 +0800122 if (ven_id != 0x8086) {
123 udev_device_unref(device);
Chia-I Wua6e33492014-08-05 13:35:08 +0800124 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800125 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800126
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600127 dnode = udev_device_get_devnode(device);
Chia-I Wua6e33492014-08-05 13:35:08 +0800128 /* TODO do not open the device at this point */
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600129 fd = open(dnode, O_RDWR);
Chia-I Wu78462db2014-08-06 12:23:55 +0800130 if (fd < 0) {
131 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600132 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800133 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600134 if (!is_render_node(fd, &st)) {
135 close(fd);
136 fd = -1;
Chia-I Wu78462db2014-08-06 12:23:55 +0800137 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600138 continue;
139 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800140 close(fd);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600141
Chia-I Wua6e33492014-08-05 13:35:08 +0800142 ret = intel_gpu_add(dev_id, dnode, &gpu);
Chia-I Wu78462db2014-08-06 12:23:55 +0800143
144 udev_device_unref(device);
145
Chia-I Wua6e33492014-08-05 13:35:08 +0800146 if (ret == XGL_SUCCESS) {
147 pGpus[count++] = (XGL_PHYSICAL_GPU) gpu;
148 if (count >= maxGpus)
149 break;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600150 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600151 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800152
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600153 udev_enumerate_unref(e);
154 udev_unref(udev);
155
Chia-I Wua6e33492014-08-05 13:35:08 +0800156 *pGpuCount = count;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600157
Chia-I Wua6e33492014-08-05 13:35:08 +0800158 return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600159}