xref: /aosp_15_r20/external/minigbm/minigbm_helpers.c (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
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 "gbm.h"
21 #include "minigbm_helpers.h"
22 #include "util.h"
23 
24 /* These are set in stone. from drm_pciids.h */
25 static unsigned int radeon_igp_ids[] = {
26 	0x1304, 0x1305, 0x1306, 0x1307, 0x1309, 0x130A, 0x130B, 0x130C, 0x130D, 0x130E, 0x130F,
27 	0x1310, 0x1311, 0x1312, 0x1313, 0x1315, 0x1316, 0x1317, 0x1318, 0x131B, 0x131C, 0x131D,
28 	0x4136, 0x4137, 0x4237, 0x4336, 0x4337, 0x4437, 0x5834, 0x5835, 0x5954, 0x5955, 0x5974,
29 	0x5975, 0x5a41, 0x5a42, 0x5a61, 0x5a62, 0x7834, 0x7835, 0x791e, 0x791f, 0x793f, 0x7941,
30 	0x7942, 0x796c, 0x796d, 0x796e, 0x796f, 0x9610, 0x9611, 0x9612, 0x9613, 0x9614, 0x9615,
31 	0x9616, 0x9640, 0x9641, 0x9642, 0x9643, 0x9644, 0x9645, 0x9647, 0x9648, 0x9649, 0x964a,
32 	0x964b, 0x964c, 0x964e, 0x964f, 0x9710, 0x9711, 0x9712, 0x9713, 0x9714, 0x9715, 0x9802,
33 	0x9803, 0x9804, 0x9805, 0x9806, 0x9807, 0x9808, 0x9809, 0x980A, 0x9830, 0x9831, 0x9832,
34 	0x9833, 0x9834, 0x9835, 0x9836, 0x9837, 0x9838, 0x9839, 0x983a, 0x983b, 0x983c, 0x983d,
35 	0x983e, 0x983f, 0x9850, 0x9851, 0x9852, 0x9853, 0x9854, 0x9855, 0x9856, 0x9857, 0x9858,
36 	0x9859, 0x985A, 0x985B, 0x985C, 0x985D, 0x985E, 0x985F, 0x9900, 0x9901, 0x9903, 0x9904,
37 	0x9905, 0x9906, 0x9907, 0x9908, 0x9909, 0x990A, 0x990B, 0x990C, 0x990D, 0x990E, 0x990F,
38 	0x9910, 0x9913, 0x9917, 0x9918, 0x9919, 0x9990, 0x9991, 0x9992, 0x9993, 0x9994, 0x9995,
39 	0x9996, 0x9997, 0x9998, 0x9999, 0x999A, 0x999B, 0x999C, 0x999D, 0x99A0, 0x99A2, 0x99A4
40 };
41 
dri_node_num(const char * dri_node)42 static int dri_node_num(const char *dri_node)
43 {
44 	long num;
45 	ssize_t l = strlen(dri_node);
46 
47 	while (l > 0 && isdigit(dri_node[l - 1]))
48 		l--;
49 	num = strtol(dri_node + l, NULL, 10);
50 	/* Normalize renderDX nodes with cardX nodes. */
51 	if (num >= 128)
52 		num -= 128;
53 	return num;
54 }
55 
fd_node_num(int fd)56 static int fd_node_num(int fd)
57 {
58 	char fd_path[64];
59 	char dri_node[256];
60 	ssize_t dri_node_size;
61 
62 	snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", fd);
63 
64 	dri_node_size = readlink(fd_path, dri_node, sizeof(dri_node));
65 	if (dri_node_size < 0)
66 		return -errno;
67 	dri_node[dri_node_size] = '\0';
68 	return dri_node_num(dri_node);
69 }
70 
71 #if 0
72 static int
73 nouveau_getparam(int fd, uint64_t param, uint64_t *value)
74 {
75    struct drm_nouveau_getparam getparam = { .param = param, .value = 0 };
76    int ret = drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &getparam, sizeof(getparam));
77    *value = getparam.value;
78    return ret;
79 }
80 #endif
81 
82 /*
83  * One would wish we could read .driver_features from DRM driver.
84  */
detect_device_info(unsigned int detect_flags,int fd,struct gbm_device_info * info)85 static int detect_device_info(unsigned int detect_flags, int fd, struct gbm_device_info *info)
86 {
87 	drmVersionPtr version;
88 	drmModeResPtr resources;
89 
90 	info->dev_type_flags = 0;
91 
92 	version = drmGetVersion(fd);
93 
94 	if (!version)
95 		return -EINVAL;
96 
97 	resources = drmModeGetResources(fd);
98 	if (resources) {
99 		info->connectors = (unsigned int)(resources->count_connectors);
100 		if (resources->count_connectors)
101 			info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY;
102 		if (detect_flags & GBM_DETECT_FLAG_CONNECTED) {
103 			int c;
104 			for (c = 0; c < resources->count_connectors; c++) {
105 				drmModeConnectorPtr conn =
106 				    drmModeGetConnector(fd, resources->connectors[c]);
107 				if (!conn)
108 					continue;
109 				if (conn->connection == DRM_MODE_CONNECTED)
110 					info->connected++;
111 				if (conn->connector_type == DRM_MODE_CONNECTOR_eDP ||
112 				    conn->connector_type == DRM_MODE_CONNECTOR_LVDS ||
113 				    conn->connector_type == DRM_MODE_CONNECTOR_DSI ||
114 				    conn->connector_type == DRM_MODE_CONNECTOR_DPI)
115 					info->dev_type_flags |= GBM_DEV_TYPE_FLAG_INTERNAL_LCD;
116 				drmModeFreeConnector(conn);
117 			}
118 		}
119 		drmModeFreeResources(resources);
120 	}
121 
122 	if (strncmp("i915", version->name, version->name_len) == 0) {
123 		/*
124 		 * Detect Intel dGPU here when special getparam ioctl is added.
125 		 */
126 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D;
127 	} else if (strncmp("amdgpu", version->name, version->name_len) == 0) {
128 		struct drm_amdgpu_info request = { 0 };
129 		struct drm_amdgpu_info_device dev_info = { 0 };
130 		int ret;
131 
132 		info->dev_type_flags = GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D;
133 		request.return_pointer = (uintptr_t)&dev_info;
134 		request.return_size = sizeof(dev_info);
135 		request.query = AMDGPU_INFO_DEV_INFO;
136 
137 		ret =
138 		    drmCommandWrite(fd, DRM_AMDGPU_INFO, &request, sizeof(struct drm_amdgpu_info));
139 
140 		if (ret != 0)
141 			goto done;
142 		if (!(dev_info.ids_flags & AMDGPU_IDS_FLAGS_FUSION))
143 			info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISCRETE;
144 
145 	} else if (strncmp("radeon", version->name, version->name_len) == 0) {
146 		struct drm_radeon_info radinfo = { 0, 0, 0 };
147 		int ret;
148 		uint32_t value = 0;
149 		size_t d;
150 
151 		info->dev_type_flags |=
152 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_DISCRETE;
153 		radinfo.request = RADEON_INFO_DEVICE_ID;
154 		radinfo.value = (uintptr_t)&value;
155 		ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &radinfo,
156 					  sizeof(struct drm_radeon_info));
157 		if (ret != 0)
158 			goto done;
159 
160 		for (d = 0; d < (sizeof(radeon_igp_ids) / sizeof(radeon_igp_ids[0])); d++) {
161 			if (value == radeon_igp_ids[d]) {
162 				info->dev_type_flags &= ~GBM_DEV_TYPE_FLAG_DISCRETE;
163 				break;
164 			}
165 		}
166 
167 	} else if (strncmp("nvidia", version->name, version->name_len) == 0) {
168 		info->dev_type_flags |=
169 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_DISCRETE;
170 	} else if (strncmp("nouveau", version->name, version->name_len) == 0) {
171 		info->dev_type_flags |=
172 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_DISCRETE;
173 	} else if (strncmp("msm", version->name, version->name_len) == 0) {
174 		info->dev_type_flags |=
175 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
176 	} else if (strncmp("armada", version->name, version->name_len) == 0) {
177 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
178 	} else if (strncmp("exynos", version->name, version->name_len) == 0) {
179 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
180 	} else if (strncmp("mediatek", version->name, version->name_len) == 0) {
181 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
182 	} else if (strncmp("rockchip", version->name, version->name_len) == 0) {
183 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
184 	} else if (strncmp("omapdrm", version->name, version->name_len) == 0) {
185 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_ARMSOC;
186 	} else if (strncmp("vc4", version->name, version->name_len) == 0) {
187 		info->dev_type_flags |=
188 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
189 	} else if (strncmp("etnaviv", version->name, version->name_len) == 0) {
190 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
191 	} else if (strncmp("lima", version->name, version->name_len) == 0) {
192 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
193 	} else if (strncmp("panfrost", version->name, version->name_len) == 0) {
194 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
195 	} else if (strncmp("pvr", version->name, version->name_len) == 0) {
196 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
197 	} else if (strncmp("v3d", version->name, version->name_len) == 0) {
198 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_3D | GBM_DEV_TYPE_FLAG_ARMSOC;
199 	} else if (strncmp("vgem", version->name, version->name_len) == 0) {
200 		info->dev_type_flags |= GBM_DEV_TYPE_FLAG_BLOCKED;
201 	} else if (strncmp("evdi", version->name, version->name_len) == 0) {
202 		info->dev_type_flags |=
203 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_USB | GBM_DEV_TYPE_FLAG_BLOCKED;
204 	} else if (strncmp("udl", version->name, version->name_len) == 0) {
205 		info->dev_type_flags |=
206 		    GBM_DEV_TYPE_FLAG_DISPLAY | GBM_DEV_TYPE_FLAG_USB | GBM_DEV_TYPE_FLAG_BLOCKED;
207 	}
208 
209 done:
210 	drmFreeVersion(version);
211 	return 0;
212 }
213 
gbm_get_default_device_fd(void)214 static int gbm_get_default_device_fd(void)
215 {
216 	DIR *dir;
217 	int ret, fd, dfd = -1;
218 	char *rendernode_name;
219 	struct dirent *dir_ent;
220 	struct gbm_device_info info;
221 
222 	dir = opendir("/dev/dri");
223 	if (!dir)
224 		return -errno;
225 
226 	fd = -1;
227 	while ((dir_ent = readdir(dir))) {
228 		if (dir_ent->d_type != DT_CHR)
229 			continue;
230 
231 		if (strncmp(dir_ent->d_name, "renderD", 7))
232 			continue;
233 
234 		ret = asprintf(&rendernode_name, "/dev/dri/%s", dir_ent->d_name);
235 		if (ret < 0)
236 			continue;
237 
238 		fd = open(rendernode_name, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
239 		free(rendernode_name);
240 
241 		if (fd < 0)
242 			continue;
243 
244 		memset(&info, 0, sizeof(info));
245 		if (detect_device_info(0, fd, &info) < 0) {
246 			close(fd);
247 			fd = -1;
248 			continue;
249 		}
250 
251 		if (info.dev_type_flags & GBM_DEV_TYPE_FLAG_BLOCKED) {
252 			close(fd);
253 			fd = -1;
254 			continue;
255 		}
256 
257 		if (!(info.dev_type_flags & GBM_DEV_TYPE_FLAG_DISPLAY)) {
258 			close(fd);
259 			fd = -1;
260 			continue;
261 		}
262 
263 		if (info.dev_type_flags & GBM_DEV_TYPE_FLAG_DISCRETE) {
264 			if (dfd < 0)
265 				dfd = fd;
266 			else
267 				close(fd);
268 			fd = -1;
269 		} else {
270 			if (dfd >= 0) {
271 				close(dfd);
272 				dfd = -1;
273 			}
274 			break;
275 		}
276 	}
277 
278 	closedir(dir);
279 
280 	if (dfd >= 0)
281 		return dfd;
282 
283 	return fd;
284 }
285 
gbm_detect_device_info(unsigned int detect_flags,int fd,struct gbm_device_info * info)286 PUBLIC int gbm_detect_device_info(unsigned int detect_flags, int fd, struct gbm_device_info *info)
287 {
288 	if (!info)
289 		return -EINVAL;
290 	memset(info, 0, sizeof(*info));
291 	info->dri_node_num = fd_node_num(fd);
292 	return detect_device_info(detect_flags, fd, info);
293 }
294 
gbm_detect_device_info_path(unsigned int detect_flags,const char * dev_node,struct gbm_device_info * info)295 PUBLIC int gbm_detect_device_info_path(unsigned int detect_flags, const char *dev_node,
296 				       struct gbm_device_info *info)
297 {
298 	char rendernode_name[64];
299 	int fd;
300 	int ret;
301 
302 	if (!info)
303 		return -EINVAL;
304 	memset(info, 0, sizeof(*info));
305 	info->dri_node_num = dri_node_num(dev_node);
306 
307 	snprintf(rendernode_name, sizeof(rendernode_name), "/dev/dri/renderD%d",
308 		 info->dri_node_num + 128);
309 	fd = open(rendernode_name, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
310 	if (fd < 0)
311 		return -errno;
312 	ret = detect_device_info(detect_flags, fd, info);
313 	close(fd);
314 	return ret;
315 }
316 
try_drm_devices(drmDevicePtr * devs,int dev_count,int type,int * out_fd)317 static struct gbm_device *try_drm_devices(drmDevicePtr *devs, int dev_count, int type, int *out_fd)
318 {
319 	int i;
320 
321 	for (i = 0; i < dev_count; i++) {
322 		drmDevicePtr dev = devs[i];
323 		int fd;
324 
325 		if (!(dev->available_nodes & (1 << type)))
326 			continue;
327 
328 		fd = open(dev->nodes[type], O_RDWR | O_CLOEXEC);
329 		if (fd >= 0) {
330 			struct gbm_device *gbm = gbm_create_device(fd);
331 			if (gbm) {
332 				// DRM master might be taken by accident on a primary node even
333 				// if master is not needed for GBM. Drop it so that programs
334 				// that actually need DRM master (e.g. Chrome) won't be blocked.
335 				if (type == DRM_NODE_PRIMARY && drmIsMaster(fd))
336 					drmDropMaster(fd);
337 				*out_fd = fd;
338 				return gbm;
339 			}
340 			close(fd);
341 		}
342 	}
343 
344 	return NULL;
345 }
346 
minigbm_create_default_device(int * out_fd)347 PUBLIC struct gbm_device *minigbm_create_default_device(int *out_fd)
348 {
349 	struct gbm_device *gbm;
350 	drmDevicePtr devs[64];
351 	int dev_count;
352 	int fd;
353 
354 	/* try gbm_get_default_device_fd first */
355 	fd = gbm_get_default_device_fd();
356 	if (fd >= 0) {
357 		gbm = gbm_create_device(fd);
358 		if (gbm) {
359 			*out_fd = fd;
360 			return gbm;
361 		}
362 		close(fd);
363 	}
364 
365 	dev_count = drmGetDevices2(0, devs, sizeof(devs) / sizeof(devs[0]));
366 
367 	/* try render nodes and then primary nodes */
368 	gbm = try_drm_devices(devs, dev_count, DRM_NODE_RENDER, out_fd);
369 	if (!gbm)
370 		gbm = try_drm_devices(devs, dev_count, DRM_NODE_PRIMARY, out_fd);
371 
372 	drmFreeDevices(devs, dev_count);
373 
374 	return gbm;
375 }
376