• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 #include <amdgpu_drm.h>
7 #include <ctype.h>
8 #include <dirent.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <radeon_drm.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <xf86drm.h>
18 #include <xf86drmMode.h>
19 
20 #include "minigbm_helpers.h"
21 #include "util.h"
22 
23 /* These are set in stone. from drm_pciids.h */
24 static unsigned int radeon_igp_ids[] = {
25 	0x1304, 0x1305, 0x1306, 0x1307, 0x1309, 0x130A, 0x130B, 0x130C, 0x130D, 0x130E, 0x130F,
26 	0x1310, 0x1311, 0x1312, 0x1313, 0x1315, 0x1316, 0x1317, 0x1318, 0x131B, 0x131C, 0x131D,
27 	0x4136, 0x4137, 0x4237, 0x4336, 0x4337, 0x4437, 0x5834, 0x5835, 0x5954, 0x5955, 0x5974,
28 	0x5975, 0x5a41, 0x5a42, 0x5a61, 0x5a62, 0x7834, 0x7835, 0x791e, 0x791f, 0x793f, 0x7941,
29 	0x7942, 0x796c, 0x796d, 0x796e, 0x796f, 0x9610, 0x9611, 0x9612, 0x9613, 0x9614, 0x9615,
30 	0x9616, 0x9640, 0x9641, 0x9642, 0x9643, 0x9644, 0x9645, 0x9647, 0x9648, 0x9649, 0x964a,
31 	0x964b, 0x964c, 0x964e, 0x964f, 0x9710, 0x9711, 0x9712, 0x9713, 0x9714, 0x9715, 0x9802,
32 	0x9803, 0x9804, 0x9805, 0x9806, 0x9807, 0x9808, 0x9809, 0x980A, 0x9830, 0x9831, 0x9832,
33 	0x9833, 0x9834, 0x9835, 0x9836, 0x9837, 0x9838, 0x9839, 0x983a, 0x983b, 0x983c, 0x983d,
34 	0x983e, 0x983f, 0x9850, 0x9851, 0x9852, 0x9853, 0x9854, 0x9855, 0x9856, 0x9857, 0x9858,
35 	0x9859, 0x985A, 0x985B, 0x985C, 0x985D, 0x985E, 0x985F, 0x9900, 0x9901, 0x9903, 0x9904,
36 	0x9905, 0x9906, 0x9907, 0x9908, 0x9909, 0x990A, 0x990B, 0x990C, 0x990D, 0x990E, 0x990F,
37 	0x9910, 0x9913, 0x9917, 0x9918, 0x9919, 0x9990, 0x9991, 0x9992, 0x9993, 0x9994, 0x9995,
38 	0x9996, 0x9997, 0x9998, 0x9999, 0x999A, 0x999B, 0x999C, 0x999D, 0x99A0, 0x99A2, 0x99A4
39 };
40 
dri_node_num(const char * dri_node)41 static int dri_node_num(const char *dri_node)
42 {
43 	long num;
44 	ssize_t l = strlen(dri_node);
45 
46 	while (l > 0 && isdigit(dri_node[l - 1]))
47 		l--;
48 	num = strtol(dri_node + l, NULL, 10);
49 	/* Normalize renderDX nodes with cardX nodes. */
50 	if (num >= 128)
51 		num -= 128;
52 	return num;
53 }
54 
fd_node_num(int fd)55 static int fd_node_num(int fd)
56 {
57 	char fd_path[64];
58 	char dri_node[256];
59 	ssize_t dri_node_size;
60 
61 	snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", fd);
62 
63 	dri_node_size = readlink(fd_path, dri_node, sizeof(dri_node));
64 	if (dri_node_size < 0)
65 		return -errno;
66 	dri_node[dri_node_size] = '\0';
67 	return dri_node_num(dri_node);
68 }
69 
70 #if 0
71 static int
72 nouveau_getparam(int fd, uint64_t param, uint64_t *value)
73 {
74    struct drm_nouveau_getparam getparam = { .param = param, .value = 0 };
75    int ret = drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &getparam, sizeof(getparam));
76    *value = getparam.value;
77    return ret;
78 }
79 #endif
80 
81 /*
82  * One would wish we could read .driver_features from DRM driver.
83  */
detect_device_info(unsigned int detect_flags,int fd,struct gbm_device_info * info)84 static int detect_device_info(unsigned int detect_flags, int fd, struct gbm_device_info *info)
85 {
86 	drmVersionPtr version;
87 	drmModeResPtr resources;
88 
89 	info->dev_type_flags = 0;
90 
91 	version = drmGetVersion(fd);
92 
93 	if (!version)
94 		return -EINVAL;
95 
96 	resources = drmModeGetResources(fd);
97 	if (resources) {
98 		info->connectors = (unsigned int)(resources->count_connectors);
99 		if (resources->count_connectors)
100 			info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY;
101 		if (detect_flags & GBM_DETECT_FLAG_CONNECTED) {
102 			int c;
103 			for (c = 0; c < resources->count_connectors; c++) {
104 				drmModeConnectorPtr conn =
105 				    drmModeGetConnector(fd, resources->connectors[c]);
106 				if (!conn)
107 					continue;
108 				if (conn->connection == DRM_MODE_CONNECTED)
109 					info->connected++;
110 				if (conn->connector_type == DRM_MODE_CONNECTOR_eDP ||
111 				    conn->connector_type == DRM_MODE_CONNECTOR_LVDS ||
112 				    conn->connector_type == DRM_MODE_CONNECTOR_DSI ||
113 				    conn->connector_type == DRM_MODE_CONNECTOR_DPI)
114 					info->dev_type_flags |= GBM_DEV_TYPE_FLAG_INTERNAL_LCD;
115 				drmModeFreeConnector(conn);
116 			}
117 		}
118 		drmModeFreeResources(resources);
119 	}
120 
121 	if (strncmp("i915", version->name, version->name_len) == 0) {
122 		/*
123 		 * Detect Intel dGPU here when special getparam ioctl is added.
124 		 */
125 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D;
126 	} else if (strncmp("amdgpu", version->name, version->name_len) == 0) {
127 		struct drm_amdgpu_info request = { 0 };
128 		struct drm_amdgpu_info_device dev_info = { 0 };
129 		int ret;
130 
131 		info->dev_type_flags = GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D;
132 		request.return_pointer = (uintptr_t)&dev_info;
133 		request.return_size = sizeof(dev_info);
134 		request.query = AMDGPU_INFO_DEV_INFO;
135 
136 		ret =
137 		    drmCommandWrite(fd, DRM_AMDGPU_INFO, &request, sizeof(struct drm_amdgpu_info));
138 
139 		if (ret != 0)
140 			goto done;
141 		if (!(dev_info.ids_flags & AMDGPU_IDS_FLAGS_FUSION))
142 			info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISCRETE;
143 
144 	} else if (strncmp("radeon", version->name, version->name_len) == 0) {
145 		struct drm_radeon_info radinfo = { 0, 0, 0 };
146 		int ret;
147 		uint32_t value = 0;
148 		size_t d;
149 
150 		info->dev_type_flags |=
151 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_DISCRETE;
152 		radinfo.request = RADEON_INFO_DEVICE_ID;
153 		radinfo.value = (uintptr_t)&value;
154 		ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &radinfo,
155 					  sizeof(struct drm_radeon_info));
156 		if (ret != 0)
157 			goto done;
158 
159 		for (d = 0; d < (sizeof(radeon_igp_ids) / sizeof(radeon_igp_ids[0])); d++) {
160 			if (value == radeon_igp_ids[d]) {
161 				info->dev_type_flags &= ~GBM_DEV_TYPE_FLAG_DISCRETE;
162 				break;
163 			}
164 		}
165 
166 	} else if (strncmp("nvidia", version->name, version->name_len) == 0) {
167 		info->dev_type_flags |=
168 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_DISCRETE;
169 	} else if (strncmp("nouveau", version->name, version->name_len) == 0) {
170 		info->dev_type_flags |=
171 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_DISCRETE;
172 	} else if (strncmp("msm", version->name, version->name_len) == 0) {
173 		info->dev_type_flags |=
174 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
175 	} else if (strncmp("armada", version->name, version->name_len) == 0) {
176 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
177 	} else if (strncmp("exynos", version->name, version->name_len) == 0) {
178 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
179 	} else if (strncmp("mediatek", version->name, version->name_len) == 0) {
180 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
181 	} else if (strncmp("rockchip", version->name, version->name_len) == 0) {
182 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
183 	} else if (strncmp("omapdrm", version->name, version->name_len) == 0) {
184 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
185 	} else if (strncmp("vc4", version->name, version->name_len) == 0) {
186 		info->dev_type_flags |=
187 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
188 	} else if (strncmp("etnaviv", version->name, version->name_len) == 0) {
189 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
190 	} else if (strncmp("lima", version->name, version->name_len) == 0) {
191 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
192 	} else if (strncmp("panfrost", version->name, version->name_len) == 0) {
193 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
194 	} else if (strncmp("pvr", version->name, version->name_len) == 0) {
195 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
196 	} else if (strncmp("v3d", version->name, version->name_len) == 0) {
197 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
198 	} else if (strncmp("vgem", version->name, version->name_len) == 0) {
199 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_BLOCKED;
200 	} else if (strncmp("evdi", version->name, version->name_len) == 0) {
201 		info->dev_type_flags |=
202 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_USB | GBM_DEV_TYPE_FLAG_BLOCKED;
203 	} else if (strncmp("udl", version->name, version->name_len) == 0) {
204 		info->dev_type_flags |=
205 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_USB | GBM_DEV_TYPE_FLAG_BLOCKED;
206 	}
207 
208 done:
209 	drmFreeVersion(version);
210 	return 0;
211 }
212 
gbm_get_default_device_fd(void)213 PUBLIC int gbm_get_default_device_fd(void)
214 {
215 	DIR *dir;
216 	int ret, fd, dfd = -1;
217 	char *rendernode_name;
218 	struct dirent *dir_ent;
219 	struct gbm_device_info info;
220 
221 	dir = opendir("/dev/dri");
222 	if (!dir)
223 		return -errno;
224 
225 	fd = -1;
226 	while ((dir_ent = readdir(dir))) {
227 		if (dir_ent->d_type != DT_CHR)
228 			continue;
229 
230 		if (strncmp(dir_ent->d_name, "renderD", 7))
231 			continue;
232 
233 		ret = asprintf(&rendernode_name, "/dev/dri/%s", dir_ent->d_name);
234 		if (ret < 0)
235 			continue;
236 
237 		fd = open(rendernode_name, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
238 		free(rendernode_name);
239 
240 		if (fd < 0)
241 			continue;
242 
243 		memset(&info, 0, sizeof(info));
244 		if (detect_device_info(0, fd, &info) < 0) {
245 			close(fd);
246 			fd = -1;
247 			continue;
248 		}
249 
250 		if (info.dev_type_flags & GBM_DEV_TYPE_FLAG_BLOCKED) {
251 			close(fd);
252 			fd = -1;
253 			continue;
254 		}
255 
256 		if (!(info.dev_type_flags & GBM_DEV_TYPE_FLAG_DISPLAY)) {
257 			close(fd);
258 			fd = -1;
259 			continue;
260 		}
261 
262 		if (info.dev_type_flags & GBM_DEV_TYPE_FLAG_DISCRETE) {
263 			if (dfd < 0)
264 				dfd = fd;
265 			else
266 				close(fd);
267 			fd = -1;
268 		} else {
269 			if (dfd >= 0) {
270 				close(dfd);
271 				dfd = -1;
272 			}
273 			break;
274 		}
275 	}
276 
277 	closedir(dir);
278 
279 	if (dfd >= 0)
280 		return dfd;
281 
282 	return fd;
283 }
284 
gbm_detect_device_info(unsigned int detect_flags,int fd,struct gbm_device_info * info)285 PUBLIC int gbm_detect_device_info(unsigned int detect_flags, int fd, struct gbm_device_info *info)
286 {
287 	if (!info)
288 		return -EINVAL;
289 	memset(info, 0, sizeof(*info));
290 	info->dri_node_num = fd_node_num(fd);
291 	return detect_device_info(detect_flags, fd, info);
292 }
293 
gbm_detect_device_info_path(unsigned int detect_flags,const char * dev_node,struct gbm_device_info * info)294 PUBLIC int gbm_detect_device_info_path(unsigned int detect_flags, const char *dev_node,
295 				       struct gbm_device_info *info)
296 {
297 	char rendernode_name[64];
298 	int fd;
299 	int ret;
300 
301 	if (!info)
302 		return -EINVAL;
303 	memset(info, 0, sizeof(*info));
304 	info->dri_node_num = dri_node_num(dev_node);
305 
306 	snprintf(rendernode_name, sizeof(rendernode_name), "/dev/dri/renderD%d",
307 		 info->dri_node_num + 128);
308 	fd = open(rendernode_name, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
309 	if (fd < 0)
310 		return -errno;
311 	ret = detect_device_info(detect_flags, fd, info);
312 	close(fd);
313 	return ret;
314 }
315