blob: f989973aa2f99839cd8ba5af6c3aa9de15e4db9a [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
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060042#include "gen7_functions.h"
Chia-I Wua6e33492014-08-05 13:35:08 +080043#include "gpu.h"
44#include "intel.h"
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060045
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060046static int is_render_node(int fd, struct stat *st)
47{
48 if (fstat(fd, st))
49 return 0;
50
51 if (!S_ISCHR(st->st_mode))
52 return 0;
53
54 return st->st_rdev & 0x80;
55}
56
Chia-I Wu468e3c32014-08-04 08:03:57 +080057ICD_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO *
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060058 pAppInfo,
59 const XGL_ALLOC_CALLBACKS *
60 pAllocCb, XGL_UINT maxGpus,
61 XGL_UINT * pGpuCount,
62 XGL_PHYSICAL_GPU * pGpus)
63{
64 struct udev *udev;
65 struct udev_enumerate *e;
66 struct udev_device *device, *parent;
67 struct udev_list_entry *entry;
68 const char *pci_id, *path;
69 const char *usub, *dnode;
70 int fd;
71 struct stat st;
72 char *pci_glob = "*:*";
Chia-I Wu3065c9c2014-08-04 06:28:31 +080073 XGL_RESULT ret;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060074 XGL_UINT count = 0;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060075
Chia-I Wuc217f802014-08-05 10:17:50 +080076 ret = icd_set_allocator(pAllocCb);
Chia-I Wu3065c9c2014-08-04 06:28:31 +080077 if (ret != XGL_SUCCESS)
78 return ret;
79
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060080 /*
81 * xglInitAndEnumerateGpus() can be called multiple times. Calling it more than once
82 * forces driver reinitialization.
83 */
Chia-I Wua6e33492014-08-05 13:35:08 +080084 intel_gpu_remove_all();
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060085
Chia-I Wuc7bff532014-08-06 12:14:54 +080086 if (!maxGpus) {
87 *pGpuCount = 0;
Chia-I Wua6e33492014-08-05 13:35:08 +080088 return XGL_SUCCESS;
Chia-I Wuc7bff532014-08-06 12:14:54 +080089 }
Chia-I Wua6e33492014-08-05 13:35:08 +080090
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060091 // TODO: Do we need any other validation for incoming pointers?
92
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060093 udev = udev_new();
94 if (udev == NULL) {
Chia-I Wua6e33492014-08-05 13:35:08 +080095 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
96 XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
97 return XGL_ERROR_OUT_OF_MEMORY;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -060098 }
99
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600100 fd = -1;
101 e = udev_enumerate_new(udev);
102 udev_enumerate_add_match_subsystem(e, "drm");
103 udev_enumerate_scan_devices(e);
104 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Chia-I Wua6e33492014-08-05 13:35:08 +0800105 unsigned int ven_id, dev_id;
106 struct intel_gpu *gpu;
107
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600108 path = udev_list_entry_get_name(entry);
109 device = udev_device_new_from_syspath(udev, path);
110 parent = udev_device_get_parent(device);
111 usub = udev_device_get_subsystem(parent);
112 /* Filter out KMS output devices. */
Chia-I Wu78462db2014-08-06 12:23:55 +0800113 if (!usub || (strcmp(usub, "pci") != 0)) {
114 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600115 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800116 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600117 pci_id = udev_device_get_property_value(parent, "PCI_ID");
Chia-I Wu78462db2014-08-06 12:23:55 +0800118 if (fnmatch(pci_glob, pci_id, 0) != 0) {
119 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600120 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800121 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800122 sscanf(pci_id, "%x:%x", &ven_id, &dev_id);
Chia-I Wu78462db2014-08-06 12:23:55 +0800123 if (ven_id != 0x8086) {
124 udev_device_unref(device);
Chia-I Wua6e33492014-08-05 13:35:08 +0800125 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800126 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800127
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600128 dnode = udev_device_get_devnode(device);
Chia-I Wua6e33492014-08-05 13:35:08 +0800129 /* TODO do not open the device at this point */
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600130 fd = open(dnode, O_RDWR);
Chia-I Wu78462db2014-08-06 12:23:55 +0800131 if (fd < 0) {
132 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600133 continue;
Chia-I Wu78462db2014-08-06 12:23:55 +0800134 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600135 if (!is_render_node(fd, &st)) {
136 close(fd);
137 fd = -1;
Chia-I Wu78462db2014-08-06 12:23:55 +0800138 udev_device_unref(device);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600139 continue;
140 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800141 close(fd);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600142
Chia-I Wua6e33492014-08-05 13:35:08 +0800143 ret = intel_gpu_add(dev_id, dnode, &gpu);
Chia-I Wu78462db2014-08-06 12:23:55 +0800144
145 udev_device_unref(device);
146
Chia-I Wua6e33492014-08-05 13:35:08 +0800147 if (ret == XGL_SUCCESS) {
148 pGpus[count++] = (XGL_PHYSICAL_GPU) gpu;
149 if (count >= maxGpus)
150 break;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600151 }
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600152 }
Chia-I Wua6e33492014-08-05 13:35:08 +0800153
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600154 udev_enumerate_unref(e);
155 udev_unref(udev);
156
Chia-I Wua6e33492014-08-05 13:35:08 +0800157 *pGpuCount = count;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600158
Chia-I Wua6e33492014-08-05 13:35:08 +0800159 return (count > 0) ? XGL_SUCCESS : XGL_ERROR_UNAVAILABLE;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600160}
161
Chia-I Wu6051cf82014-08-04 10:41:45 +0800162XGL_RESULT XGLAPI intelGetExtensionSupport(
Chia-I Wua6e33492014-08-05 13:35:08 +0800163 XGL_PHYSICAL_GPU gpu_,
Chia-I Wu6051cf82014-08-04 10:41:45 +0800164 const XGL_CHAR* pExtName)
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600165{
Chia-I Wua6e33492014-08-05 13:35:08 +0800166 struct intel_gpu *gpu = intel_gpu(gpu_);
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600167
Chia-I Wua6e33492014-08-05 13:35:08 +0800168 return (intel_gpu_has_extension(gpu, (const char *) pExtName)) ?
169 XGL_SUCCESS : XGL_ERROR_INVALID_EXTENSION;
Courtney Goeltzenleuchtere06e72d2014-08-01 12:44:23 -0600170}