blob: 07a18c448d3fc27d47fed32c5ac340f2e75834c9 [file] [log] [blame]
Jose Fonsecad2443b22003-05-27 00:37:33 +00001/**
Jan Vesely50d3c852016-06-30 14:22:52 -04002 * \file xf86drm.c
Jose Fonsecad2443b22003-05-27 00:37:33 +00003 * User-level interface to DRM device
Daryll Straussb3a57661999-12-05 01:19:48 +00004 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
7 */
8
9/*
Brian Paul569da5a2000-06-08 14:38:22 +000010 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
Daryll Straussb3a57661999-12-05 01:19:48 +000012 * All Rights Reserved.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
Gareth Hughes36047532001-02-15 08:12:14 +000020 *
Daryll Straussb3a57661999-12-05 01:19:48 +000021 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
Gareth Hughes36047532001-02-15 08:12:14 +000024 *
Daryll Straussb3a57661999-12-05 01:19:48 +000025 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
Daryll Straussb3a57661999-12-05 01:19:48 +000032 */
33
Dave Airlie79038752006-11-08 15:08:09 +110034#include <stdio.h>
35#include <stdlib.h>
Emil Velikovfae59d72015-09-09 17:54:34 +010036#include <stdbool.h>
Dave Airlie79038752006-11-08 15:08:09 +110037#include <unistd.h>
38#include <string.h>
Ian Romanick015efd12009-07-06 09:23:59 -070039#include <strings.h>
Dave Airlie79038752006-11-08 15:08:09 +110040#include <ctype.h>
Emil Velikov0ca03a42015-03-07 00:58:39 +000041#include <dirent.h>
42#include <stddef.h>
Dave Airlie79038752006-11-08 15:08:09 +110043#include <fcntl.h>
44#include <errno.h>
Felix Janda4031dc12015-09-26 08:08:43 +020045#include <limits.h>
Dave Airlie79038752006-11-08 15:08:09 +110046#include <signal.h>
Jesse Barnesf4f76a62009-01-07 10:18:08 -080047#include <time.h>
Dave Airlie79038752006-11-08 15:08:09 +110048#include <sys/types.h>
49#include <sys/stat.h>
50#define stat_t struct stat
51#include <sys/ioctl.h>
Dave Airlie79038752006-11-08 15:08:09 +110052#include <sys/time.h>
53#include <stdarg.h>
Mike Frysinger8c8d5dd2016-06-21 12:18:15 -040054#ifdef MAJOR_IN_MKDEV
55#include <sys/mkdev.h>
56#endif
57#ifdef MAJOR_IN_SYSMACROS
58#include <sys/sysmacros.h>
Alan Coopersmith0e1135d2015-03-07 11:44:32 -080059#endif
Emmanuel Vadot37911372020-01-21 18:49:11 +010060#if HAVE_SYS_SYSCTL_H
61#include <sys/sysctl.h>
62#endif
Emil Velikovb556ea12015-08-17 11:09:06 +080063#include <math.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000064
Emmanuel Vadot1c8d2b72020-01-21 17:42:44 +010065#if defined(__FreeBSD__)
66#include <sys/param.h>
Emmanuel Vadote321dd72020-01-21 18:49:47 +010067#include <sys/pciio.h>
Emmanuel Vadot1c8d2b72020-01-21 17:42:44 +010068#endif
69
Eric Anholt9b28c5a2018-11-15 17:48:53 -080070#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
71
Daryll Straussb3a57661999-12-05 01:19:48 +000072/* Not all systems have MAP_FAILED defined */
73#ifndef MAP_FAILED
74#define MAP_FAILED ((void *)-1)
75#endif
76
Daryll Straussb3a57661999-12-05 01:19:48 +000077#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010078#include "libdrm_macros.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000079
Emil Velikov5f68d312015-09-07 13:54:32 +010080#include "util_math.h"
81
Emmanuel Vadot4fbcc9a2020-01-21 17:39:10 +010082#ifdef __DragonFly__
Eric Anholtcfa778a2003-02-21 23:23:09 +000083#define DRM_MAJOR 145
Rik Faith88dbee52001-02-28 09:27:44 +000084#endif
85
Eric Anholtcfa778a2003-02-21 23:23:09 +000086#ifdef __NetBSD__
87#define DRM_MAJOR 34
88#endif
89
Jonathan Gray66c3afb2015-07-21 03:12:10 +100090#ifdef __OpenBSD__
91#ifdef __i386__
92#define DRM_MAJOR 88
93#else
94#define DRM_MAJOR 87
95#endif
96#endif /* __OpenBSD__ */
Alan Hourihaneb0a92852003-09-24 14:39:25 +000097
Eric Anholtcfa778a2003-02-21 23:23:09 +000098#ifndef DRM_MAJOR
Jan Vesely50d3c852016-06-30 14:22:52 -040099#define DRM_MAJOR 226 /* Linux */
Rik Faith88dbee52001-02-28 09:27:44 +0000100#endif
101
François Tigeot8f2e0922018-12-12 20:48:36 +0100102#if defined(__OpenBSD__) || defined(__DragonFly__)
Jonathan Grayc0ef1d02016-12-01 15:18:41 +1100103struct drm_pciinfo {
104 uint16_t domain;
105 uint8_t bus;
106 uint8_t dev;
107 uint8_t func;
108 uint16_t vendor_id;
109 uint16_t device_id;
110 uint16_t subvendor_id;
111 uint16_t subdevice_id;
112 uint8_t revision_id;
113};
114
115#define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
116#endif
117
David Dawes56bd9c22001-07-30 19:59:39 +0000118#define DRM_MSG_VERBOSITY 3
119
Daniel Vetterfd387942015-02-11 12:41:04 +0100120#define memclear(s) memset(&s, 0, sizeof(s))
121
Dave Airlie79038752006-11-08 15:08:09 +1100122static drmServerInfoPtr drm_server_info;
123
Emmanuel Vadot6818a502020-01-21 17:50:15 +0100124static bool drmNodeIsDRM(int maj, int min);
Emmanuel Vadot5aa83dd2020-04-28 16:33:05 +0200125static char *drmGetMinorNameForFD(int fd, int type);
Emmanuel Vadot6818a502020-01-21 17:50:15 +0100126
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700127drm_public void drmSetServerInfo(drmServerInfoPtr info)
Dave Airlie79038752006-11-08 15:08:09 +1100128{
Brianccd7b6e2007-05-29 14:54:00 -0600129 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +1100130}
131
Jose Fonsecad2443b22003-05-27 00:37:33 +0000132/**
133 * Output a message to stderr.
134 *
135 * \param format printf() like format string.
136 *
137 * \internal
138 * This function is a wrapper around vfprintf().
139 */
Dave Airlie79038752006-11-08 15:08:09 +1100140
Thierry Reding44b08c02014-01-22 12:06:51 +0100141static int DRM_PRINTFLIKE(1, 0)
142drmDebugPrint(const char *format, va_list ap)
Dave Airlie79038752006-11-08 15:08:09 +1100143{
Brianccd7b6e2007-05-29 14:54:00 -0600144 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100145}
146
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700147drm_public void
David Dawes56bd9c22001-07-30 19:59:39 +0000148drmMsg(const char *format, ...)
149{
Jan Vesely50d3c852016-06-30 14:22:52 -0400150 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000151 const char *env;
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400152 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
153 (drm_server_info && drm_server_info->debug_print))
David Dawes56bd9c22001-07-30 19:59:39 +0000154 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400155 va_start(ap, format);
156 if (drm_server_info) {
157 drm_server_info->debug_print(format,ap);
158 } else {
159 drmDebugPrint(format, ap);
160 }
161 va_end(ap);
David Dawes56bd9c22001-07-30 19:59:39 +0000162 }
163}
164
Daryll Straussb3a57661999-12-05 01:19:48 +0000165static void *drmHashTable = NULL; /* Context switch callbacks */
166
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700167drm_public void *drmGetHashTable(void)
Dave Airlie79038752006-11-08 15:08:09 +1100168{
Brianccd7b6e2007-05-29 14:54:00 -0600169 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100170}
Daryll Straussb3a57661999-12-05 01:19:48 +0000171
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700172drm_public void *drmMalloc(int size)
Daryll Straussb3a57661999-12-05 01:19:48 +0000173{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100174 return calloc(1, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000175}
176
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700177drm_public void drmFree(void *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +0000178{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100179 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000180}
181
Keith Packard8b9ab102008-06-13 16:03:22 -0700182/**
Eric Engestrom360292c2018-12-19 14:55:45 +0000183 * Call ioctl, restarting if it is interrupted
Keith Packard8b9ab102008-06-13 16:03:22 -0700184 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700185drm_public int
Coleman Kane41b83a92008-08-18 17:08:21 -0400186drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700187{
Jan Vesely50d3c852016-06-30 14:22:52 -0400188 int ret;
Keith Packard8b9ab102008-06-13 16:03:22 -0700189
190 do {
Jan Vesely50d3c852016-06-30 14:22:52 -0400191 ret = ioctl(fd, request, arg);
Keith Packard8b9ab102008-06-13 16:03:22 -0700192 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
193 return ret;
194}
Daryll Straussb3a57661999-12-05 01:19:48 +0000195
Daryll Straussb3a57661999-12-05 01:19:48 +0000196static unsigned long drmGetKeyFromFd(int fd)
197{
David Dawesfcc21062001-03-30 17:16:20 +0000198 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000199
200 st.st_rdev = 0;
201 fstat(fd, &st);
202 return st.st_rdev;
203}
204
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700205drm_public drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000206{
207 unsigned long key = drmGetKeyFromFd(fd);
208 void *value;
209 drmHashEntry *entry;
210
Brianccd7b6e2007-05-29 14:54:00 -0600211 if (!drmHashTable)
Jan Vesely50d3c852016-06-30 14:22:52 -0400212 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000213
214 if (drmHashLookup(drmHashTable, key, &value)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400215 entry = drmMalloc(sizeof(*entry));
216 entry->fd = fd;
217 entry->f = NULL;
218 entry->tagTable = drmHashCreate();
219 drmHashInsert(drmHashTable, key, entry);
Daryll Straussb3a57661999-12-05 01:19:48 +0000220 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -0400221 entry = value;
Daryll Straussb3a57661999-12-05 01:19:48 +0000222 }
223 return entry;
224}
225
Jose Fonsecad2443b22003-05-27 00:37:33 +0000226/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000227 * Compare two busid strings
228 *
229 * \param first
230 * \param second
231 *
232 * \return 1 if matched.
233 *
234 * \internal
235 * This function compares two bus ID strings. It understands the older
236 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
237 * domain, b is bus, d is device, f is function.
238 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000239static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000240{
241 /* First, check if the IDs are exactly the same */
242 if (strcasecmp(id1, id2) == 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400243 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000244
245 /* Try to match old/new-style PCI bus IDs. */
246 if (strncasecmp(id1, "pci", 3) == 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400247 unsigned int o1, b1, d1, f1;
248 unsigned int o2, b2, d2, f2;
249 int ret;
Eric Anholt06cb1322003-10-23 02:23:31 +0000250
Jan Vesely50d3c852016-06-30 14:22:52 -0400251 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
252 if (ret != 4) {
253 o1 = 0;
254 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
255 if (ret != 3)
256 return 0;
257 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000258
Jan Vesely50d3c852016-06-30 14:22:52 -0400259 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
260 if (ret != 4) {
261 o2 = 0;
262 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
263 if (ret != 3)
264 return 0;
265 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000266
Jan Vesely50d3c852016-06-30 14:22:52 -0400267 /* If domains aren't properly supported by the kernel interface,
268 * just ignore them, which sucks less than picking a totally random
269 * card with "open by name"
270 */
271 if (!pci_domain_ok)
272 o1 = o2 = 0;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000273
Jan Vesely50d3c852016-06-30 14:22:52 -0400274 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
275 return 0;
276 else
277 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000278 }
279 return 0;
280}
281
282/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300283 * Handles error checking for chown call.
284 *
285 * \param path to file.
286 * \param id of the new owner.
287 * \param id of the new group.
288 *
289 * \return zero if success or -1 if failure.
290 *
291 * \internal
292 * Checks for failure. If failure was caused by signal call chown again.
Eric Engestrom360292c2018-12-19 14:55:45 +0000293 * If any other failure happened then it will output error message using
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300294 * drmMsg() call.
295 */
Eric Engestrom07585202018-03-16 17:04:50 +0000296#if !UDEV
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300297static int chown_check_return(const char *path, uid_t owner, gid_t group)
298{
Jan Vesely50d3c852016-06-30 14:22:52 -0400299 int rv;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300300
Jan Vesely50d3c852016-06-30 14:22:52 -0400301 do {
302 rv = chown(path, owner, group);
303 } while (rv != 0 && errno == EINTR);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300304
Jan Vesely50d3c852016-06-30 14:22:52 -0400305 if (rv == 0)
306 return 0;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300307
Jan Vesely50d3c852016-06-30 14:22:52 -0400308 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
309 path, errno, strerror(errno));
310 return -1;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300311}
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500312#endif
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300313
Eric Engestrom331e51e2018-12-19 13:53:41 +0000314static const char *drmGetDeviceName(int type)
315{
316 switch (type) {
317 case DRM_NODE_PRIMARY:
318 return DRM_DEV_NAME;
319 case DRM_NODE_CONTROL:
320 return DRM_CONTROL_DEV_NAME;
321 case DRM_NODE_RENDER:
322 return DRM_RENDER_DEV_NAME;
323 }
324 return NULL;
325}
326
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300327/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000328 * Open the DRM device, creating it if necessary.
329 *
330 * \param dev major and minor numbers of the device.
331 * \param minor minor number of the device.
Jan Vesely50d3c852016-06-30 14:22:52 -0400332 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000333 * \return a file descriptor on success, or a negative value on error.
334 *
335 * \internal
336 * Assembles the device name from \p minor and opens it, creating the device
337 * special file node with the major and minor numbers specified by \p dev and
338 * parent directory if necessary and was called by root.
339 */
Jan Veselyde8532d2014-11-30 12:53:18 -0500340static int drmOpenDevice(dev_t dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000341{
David Dawes9c775d02001-05-14 14:49:58 +0000342 stat_t st;
Eric Engestrom331e51e2018-12-19 13:53:41 +0000343 const char *dev_name = drmGetDeviceName(type);
Eric Engestrom6869e4c2018-12-19 13:42:08 +0000344 char buf[DRM_NODE_NAME_MAX];
Rik Faith88dbee52001-02-28 09:27:44 +0000345 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100346 mode_t devmode = DRM_DEV_MODE, serv_mode;
Jan Vesely0706c142015-02-27 12:47:46 -0500347 gid_t serv_group;
Eric Engestrom07585202018-03-16 17:04:50 +0000348#if !UDEV
Rik Faith88dbee52001-02-28 09:27:44 +0000349 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000350 uid_t user = DRM_DEV_UID;
Jan Vesely0706c142015-02-27 12:47:46 -0500351 gid_t group = DRM_DEV_GID;
352#endif
353
Eric Engestrom331e51e2018-12-19 13:53:41 +0000354 if (!dev_name)
Jan Vesely50d3c852016-06-30 14:22:52 -0400355 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000356
357 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000358 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000359
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400360 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400361 drm_server_info->get_perms(&serv_group, &serv_mode);
362 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
363 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Dave Airlie79038752006-11-08 15:08:09 +1100364 }
Brian Paul569da5a2000-06-08 14:38:22 +0000365
Eric Engestrom07585202018-03-16 17:04:50 +0000366#if !UDEV
Rik Faith88dbee52001-02-28 09:27:44 +0000367 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400368 if (!isroot)
369 return DRM_ERR_NOT_ROOT;
370 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
371 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
372 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000373 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000374
Eric Anholt06cb1322003-10-23 02:23:31 +0000375 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000376 if (stat(buf, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400377 if (!isroot)
378 return DRM_ERR_NOT_ROOT;
379 remove(buf);
380 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000381 }
Dave Airlie79038752006-11-08 15:08:09 +1100382
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400383 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400384 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
385 chown_check_return(buf, user, group);
386 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100387 }
Dave Airlie9101a022008-08-24 16:54:43 +1000388#else
389 /* if we modprobed then wait for udev */
390 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400391 int udev_count = 0;
Dave Airlie9101a022008-08-24 16:54:43 +1000392wait_for_udev:
393 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400394 usleep(20);
395 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000396
Jan Vesely50d3c852016-06-30 14:22:52 -0400397 if (udev_count == 50)
398 return -1;
399 goto wait_for_udev;
400 }
Dave Airlie9101a022008-08-24 16:54:43 +1000401
Jan Vesely50d3c852016-06-30 14:22:52 -0400402 if (stat(buf, &st)) {
403 usleep(20);
404 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000405
Jan Vesely50d3c852016-06-30 14:22:52 -0400406 if (udev_count == 50)
407 return -1;
408 goto wait_for_udev;
409 }
Dave Airlie9101a022008-08-24 16:54:43 +1000410 }
411#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000412
Michel Dänzer35615692018-05-18 11:16:51 +0200413 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
David Dawes56bd9c22001-07-30 19:59:39 +0000414 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400415 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600416 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400417 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000418
Eric Engestrom07585202018-03-16 17:04:50 +0000419#if !UDEV
Eric Anholt06cb1322003-10-23 02:23:31 +0000420 /* Check if the device node is not what we expect it to be, and recreate it
421 * and try again if so.
422 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000423 if (st.st_rdev != dev) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400424 if (!isroot)
425 return DRM_ERR_NOT_ROOT;
426 remove(buf);
427 mknod(buf, S_IFCHR | devmode, dev);
428 if (drm_server_info && drm_server_info->get_perms) {
429 chown_check_return(buf, user, group);
430 chmod(buf, devmode);
431 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000432 }
Michel Dänzer35615692018-05-18 11:16:51 +0200433 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
Eric Anholtd2f2b422002-08-08 21:23:46 +0000434 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400435 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600436 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400437 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000438
David Dawes56bd9c22001-07-30 19:59:39 +0000439 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000440 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000441#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000442 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000443}
444
Jose Fonsecad2443b22003-05-27 00:37:33 +0000445
446/**
447 * Open the DRM device
448 *
449 * \param minor device minor number.
450 * \param create allow to create the device if set.
451 *
452 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400453 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000454 * \internal
455 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
456 * name from \p minor and opens it.
457 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800458static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000459{
460 int fd;
Eric Engestrom6869e4c2018-12-19 13:42:08 +0000461 char buf[DRM_NODE_NAME_MAX];
Eric Engestrom331e51e2018-12-19 13:53:41 +0000462 const char *dev_name = drmGetDeviceName(type);
Jan Vesely50d3c852016-06-30 14:22:52 -0400463
Brianccd7b6e2007-05-29 14:54:00 -0600464 if (create)
Jan Vesely50d3c852016-06-30 14:22:52 -0400465 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
466
Eric Engestrom331e51e2018-12-19 13:53:41 +0000467 if (!dev_name)
Jan Vesely50d3c852016-06-30 14:22:52 -0400468 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000469
470 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Michel Dänzer35615692018-05-18 11:16:51 +0200471 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400472 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000473 return -errno;
474}
475
Brian Paul569da5a2000-06-08 14:38:22 +0000476
Jose Fonsecad2443b22003-05-27 00:37:33 +0000477/**
478 * Determine whether the DRM kernel driver has been loaded.
Jan Vesely50d3c852016-06-30 14:22:52 -0400479 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000480 * \return 1 if the DRM driver is loaded, 0 otherwise.
481 *
Jan Vesely50d3c852016-06-30 14:22:52 -0400482 * \internal
Jose Fonsecad2443b22003-05-27 00:37:33 +0000483 * Determine the presence of the kernel driver by attempting to open the 0
484 * minor and get version information. For backward compatibility with older
485 * Linux implementations, /proc/dri is also checked.
486 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700487drm_public int drmAvailable(void)
Brian Paul569da5a2000-06-08 14:38:22 +0000488{
Brian Paul569da5a2000-06-08 14:38:22 +0000489 drmVersionPtr version;
490 int retval = 0;
491 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000492
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000493 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000494#ifdef __linux__
Jan Vesely50d3c852016-06-30 14:22:52 -0400495 /* Try proc for backward Linux compatibility */
496 if (!access("/proc/dri/0", R_OK))
497 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000498#endif
Jan Vesely50d3c852016-06-30 14:22:52 -0400499 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000500 }
Jan Vesely50d3c852016-06-30 14:22:52 -0400501
Rik Faith88dbee52001-02-28 09:27:44 +0000502 if ((version = drmGetVersion(fd))) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400503 retval = 1;
504 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000505 }
506 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000507
508 return retval;
509}
510
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800511static int drmGetMinorBase(int type)
512{
513 switch (type) {
514 case DRM_NODE_PRIMARY:
515 return 0;
516 case DRM_NODE_CONTROL:
517 return 64;
518 case DRM_NODE_RENDER:
519 return 128;
520 default:
521 return -1;
522 };
523}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000524
Emmanuel Vadotc55a1e52020-01-21 17:47:09 +0100525static int drmGetMinorType(int major, int minor)
Frank Binns1f735782015-02-13 10:51:15 +0000526{
Emmanuel Vadotc55a1e52020-01-21 17:47:09 +0100527#ifdef __FreeBSD__
528 char name[SPECNAMELEN];
529 int id;
530
531 if (!devname_r(makedev(major, minor), S_IFCHR, name, sizeof(name)))
532 return -1;
533
534 if (sscanf(name, "drm/%d", &id) != 1) {
535 // If not in /dev/drm/ we have the type in the name
536 if (sscanf(name, "dri/card%d\n", &id) >= 1)
537 return DRM_NODE_PRIMARY;
538 else if (sscanf(name, "dri/control%d\n", &id) >= 1)
539 return DRM_NODE_CONTROL;
540 else if (sscanf(name, "dri/renderD%d\n", &id) >= 1)
541 return DRM_NODE_RENDER;
542 return -1;
543 }
544
545 minor = id;
546#endif
Frank Binns1f735782015-02-13 10:51:15 +0000547 int type = minor >> 6;
548
549 if (minor < 0)
550 return -1;
551
552 switch (type) {
553 case DRM_NODE_PRIMARY:
554 case DRM_NODE_CONTROL:
555 case DRM_NODE_RENDER:
556 return type;
557 default:
558 return -1;
559 }
560}
561
Emil Velikov0ca03a42015-03-07 00:58:39 +0000562static const char *drmGetMinorName(int type)
563{
564 switch (type) {
565 case DRM_NODE_PRIMARY:
Jonathan Grayfc083322015-07-21 03:12:56 +1000566 return DRM_PRIMARY_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000567 case DRM_NODE_CONTROL:
Jonathan Grayfc083322015-07-21 03:12:56 +1000568 return DRM_CONTROL_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000569 case DRM_NODE_RENDER:
Jonathan Grayfc083322015-07-21 03:12:56 +1000570 return DRM_RENDER_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000571 default:
572 return NULL;
573 }
574}
575
Jose Fonsecad2443b22003-05-27 00:37:33 +0000576/**
577 * Open the device by bus ID.
578 *
579 * \param busid bus ID.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800580 * \param type device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000581 *
582 * \return a file descriptor on success, or a negative value on error.
583 *
584 * \internal
585 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
586 * comparing the device bus ID with the one supplied.
587 *
588 * \sa drmOpenMinor() and drmGetBusid().
589 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800590static int drmOpenByBusid(const char *busid, int type)
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000591{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000592 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000593 int fd;
594 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000595 drmSetVersion sv;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800596 int base = drmGetMinorBase(type);
597
598 if (base < 0)
599 return -1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000600
601 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800602 for (i = base; i < base + DRM_MAX_MINOR; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400603 fd = drmOpenMinor(i, 1, type);
604 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
605 if (fd >= 0) {
606 /* We need to try for 1.4 first for proper PCI domain support
607 * and if that fails, we know the kernel is busted
608 */
609 sv.drm_di_major = 1;
610 sv.drm_di_minor = 4;
611 sv.drm_dd_major = -1; /* Don't care */
612 sv.drm_dd_minor = -1; /* Don't care */
613 if (drmSetInterfaceVersion(fd, &sv)) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000614#ifndef __alpha__
Jan Vesely50d3c852016-06-30 14:22:52 -0400615 pci_domain_ok = 0;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000616#endif
Jan Vesely50d3c852016-06-30 14:22:52 -0400617 sv.drm_di_major = 1;
618 sv.drm_di_minor = 1;
619 sv.drm_dd_major = -1; /* Don't care */
620 sv.drm_dd_minor = -1; /* Don't care */
621 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
622 drmSetInterfaceVersion(fd, &sv);
623 }
624 buf = drmGetBusid(fd);
625 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
626 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
627 drmFreeBusid(buf);
628 return fd;
629 }
630 if (buf)
631 drmFreeBusid(buf);
632 close(fd);
633 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000634 }
635 return -1;
636}
637
Jose Fonsecad2443b22003-05-27 00:37:33 +0000638
639/**
640 * Open the device by name.
641 *
642 * \param name driver name.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800643 * \param type the device node type.
Jan Vesely50d3c852016-06-30 14:22:52 -0400644 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000645 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400646 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000647 * \internal
648 * This function opens the first minor number that matches the driver name and
649 * isn't already in use. If it's in use it then it will already have a bus ID
650 * assigned.
Jan Vesely50d3c852016-06-30 14:22:52 -0400651 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000652 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
653 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800654static int drmOpenByName(const char *name, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000655{
Rik Faith88dbee52001-02-28 09:27:44 +0000656 int i;
657 int fd;
658 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000659 char * id;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800660 int base = drmGetMinorBase(type);
661
662 if (base < 0)
663 return -1;
Dave Airliedb85ed22008-02-13 12:20:02 +1000664
David Dawes56bd9c22001-07-30 19:59:39 +0000665 /*
666 * Open the first minor number that matches the driver name and isn't
667 * already in use. If it's in use it will have a busid assigned already.
668 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800669 for (i = base; i < base + DRM_MAX_MINOR; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400670 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
671 if ((version = drmGetVersion(fd))) {
672 if (!strcmp(version->name, name)) {
673 drmFreeVersion(version);
674 id = drmGetBusid(fd);
675 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
676 if (!id || !*id) {
677 if (id)
678 drmFreeBusid(id);
679 return fd;
680 } else {
681 drmFreeBusid(id);
682 }
683 } else {
684 drmFreeVersion(version);
685 }
686 }
687 close(fd);
688 }
Rik Faith88dbee52001-02-28 09:27:44 +0000689 }
690
691#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000692 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000693 for (i = 0; i < 8; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400694 char proc_name[64], buf[512];
695 char *driver, *pt, *devstring;
696 int retcode;
697
698 sprintf(proc_name, "/proc/dri/%d/name", i);
Eric Engestrom5ab60312020-05-28 17:17:26 +0200699 if ((fd = open(proc_name, O_RDONLY, 0)) >= 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400700 retcode = read(fd, buf, sizeof(buf)-1);
701 close(fd);
702 if (retcode) {
703 buf[retcode-1] = '\0';
704 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
705 ;
706 if (*pt) { /* Device is next */
707 *pt = '\0';
708 if (!strcmp(driver, name)) { /* Match */
709 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
710 ;
711 if (*pt) { /* Found busid */
712 return drmOpenByBusid(++pt, type);
713 } else { /* No busid */
714 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
715 }
716 }
717 }
718 }
719 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000720 }
Rik Faith88dbee52001-02-28 09:27:44 +0000721#endif
722
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000723 return -1;
724}
725
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000726
Jose Fonsecad2443b22003-05-27 00:37:33 +0000727/**
728 * Open the DRM device.
729 *
730 * Looks up the specified name and bus ID, and opens the device found. The
731 * entry in /dev/dri is created if necessary and if called by root.
732 *
733 * \param name driver name. Not referenced if bus ID is supplied.
734 * \param busid bus ID. Zero if not known.
Jan Vesely50d3c852016-06-30 14:22:52 -0400735 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000736 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400737 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000738 * \internal
739 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
740 * otherwise.
741 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700742drm_public int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000743{
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800744 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
745}
746
747/**
748 * Open the DRM device with specified type.
749 *
750 * Looks up the specified name and bus ID, and opens the device found. The
751 * entry in /dev/dri is created if necessary and if called by root.
752 *
753 * \param name driver name. Not referenced if bus ID is supplied.
754 * \param busid bus ID. Zero if not known.
755 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
756 *
757 * \return a file descriptor on success, or a negative value on error.
758 *
759 * \internal
760 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
761 * otherwise.
762 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700763drm_public int drmOpenWithType(const char *name, const char *busid, int type)
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800764{
Prabhanjan Kandula225d73f2019-04-24 10:08:39 -0700765 if (name != NULL && drm_server_info &&
766 drm_server_info->load_module && !drmAvailable()) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400767 /* try to load the kernel module */
768 if (!drm_server_info->load_module(name)) {
769 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
770 return -1;
771 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000772 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000773
Eric Anholt06cb1322003-10-23 02:23:31 +0000774 if (busid) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400775 int fd = drmOpenByBusid(busid, type);
776 if (fd >= 0)
777 return fd;
Eric Anholt06cb1322003-10-23 02:23:31 +0000778 }
Jan Vesely50d3c852016-06-30 14:22:52 -0400779
Eric Anholt06cb1322003-10-23 02:23:31 +0000780 if (name)
Jan Vesely50d3c852016-06-30 14:22:52 -0400781 return drmOpenByName(name, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000782
Eric Anholt06cb1322003-10-23 02:23:31 +0000783 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000784}
785
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700786drm_public int drmOpenControl(int minor)
Jesse Barnes731cd552008-12-17 10:09:49 -0800787{
788 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
789}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000790
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700791drm_public int drmOpenRender(int minor)
Frank Binns0c5aaee2015-01-14 14:07:51 +0000792{
793 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
794}
795
Jose Fonsecad2443b22003-05-27 00:37:33 +0000796/**
797 * Free the version information returned by drmGetVersion().
798 *
799 * \param v pointer to the version information.
800 *
801 * \internal
802 * It frees the memory pointed by \p %v as well as all the non-null strings
803 * pointers in it.
804 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700805drm_public void drmFreeVersion(drmVersionPtr v)
Daryll Straussb3a57661999-12-05 01:19:48 +0000806{
Brianccd7b6e2007-05-29 14:54:00 -0600807 if (!v)
Jan Vesely50d3c852016-06-30 14:22:52 -0400808 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100809 drmFree(v->name);
810 drmFree(v->date);
811 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000812 drmFree(v);
813}
814
Jose Fonsecad2443b22003-05-27 00:37:33 +0000815
816/**
817 * Free the non-public version information returned by the kernel.
818 *
819 * \param v pointer to the version information.
820 *
821 * \internal
822 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
823 * the non-null strings pointers in it.
824 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000825static void drmFreeKernelVersion(drm_version_t *v)
826{
Brianccd7b6e2007-05-29 14:54:00 -0600827 if (!v)
Jan Vesely50d3c852016-06-30 14:22:52 -0400828 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100829 drmFree(v->name);
830 drmFree(v->date);
831 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000832 drmFree(v);
833}
834
Jose Fonsecad2443b22003-05-27 00:37:33 +0000835
836/**
837 * Copy version information.
Jan Vesely50d3c852016-06-30 14:22:52 -0400838 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000839 * \param d destination pointer.
840 * \param s source pointer.
Jan Vesely50d3c852016-06-30 14:22:52 -0400841 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000842 * \internal
843 * Used by drmGetVersion() to translate the information returned by the ioctl
844 * interface in a private structure into the public structure counterpart.
845 */
Brian Paul569da5a2000-06-08 14:38:22 +0000846static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000847{
848 d->version_major = s->version_major;
849 d->version_minor = s->version_minor;
850 d->version_patchlevel = s->version_patchlevel;
851 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400852 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000853 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400854 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000855 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400856 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000857}
858
Daryll Straussb3a57661999-12-05 01:19:48 +0000859
Jose Fonsecad2443b22003-05-27 00:37:33 +0000860/**
861 * Query the driver version information.
862 *
863 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -0400864 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000865 * \return pointer to a drmVersion structure which should be freed with
866 * drmFreeVersion().
Jan Vesely50d3c852016-06-30 14:22:52 -0400867 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000868 * \note Similar information is available via /proc/dri.
Jan Vesely50d3c852016-06-30 14:22:52 -0400869 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000870 * \internal
871 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
872 * first with zeros to get the string lengths, and then the actually strings.
873 * It also null-terminates them since they might not be already.
874 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700875drm_public drmVersionPtr drmGetVersion(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000876{
877 drmVersionPtr retval;
878 drm_version_t *version = drmMalloc(sizeof(*version));
879
Keith Packard8b9ab102008-06-13 16:03:22 -0700880 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400881 drmFreeKernelVersion(version);
882 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000883 }
884
Daryll Straussb3a57661999-12-05 01:19:48 +0000885 if (version->name_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400886 version->name = drmMalloc(version->name_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000887 if (version->date_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400888 version->date = drmMalloc(version->date_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000889 if (version->desc_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400890 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000891
Keith Packard8b9ab102008-06-13 16:03:22 -0700892 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400893 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
894 drmFreeKernelVersion(version);
895 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000896 }
897
Adam Jackson22e41ef2006-02-20 23:09:00 +0000898 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000899 if (version->name_len) version->name[version->name_len] = '\0';
900 if (version->date_len) version->date[version->date_len] = '\0';
901 if (version->desc_len) version->desc[version->desc_len] = '\0';
902
Daryll Straussb3a57661999-12-05 01:19:48 +0000903 retval = drmMalloc(sizeof(*retval));
904 drmCopyVersion(retval, version);
905 drmFreeKernelVersion(version);
906 return retval;
907}
908
Jens Owen3903e5a2002-04-09 21:54:56 +0000909
Jose Fonsecad2443b22003-05-27 00:37:33 +0000910/**
911 * Get version information for the DRM user space library.
Jan Vesely50d3c852016-06-30 14:22:52 -0400912 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000913 * This version number is driver independent.
Jan Vesely50d3c852016-06-30 14:22:52 -0400914 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000915 * \param fd file descriptor.
916 *
917 * \return version information.
Jan Vesely50d3c852016-06-30 14:22:52 -0400918 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000919 * \internal
920 * This function allocates and fills a drm_version structure with a hard coded
921 * version number.
922 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700923drm_public drmVersionPtr drmGetLibVersion(int fd)
Jens Owen3903e5a2002-04-09 21:54:56 +0000924{
925 drm_version_t *version = drmMalloc(sizeof(*version));
926
927 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100928 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000929 * revision 1.0.x = original DRM interface with no drmGetLibVersion
930 * entry point and many drm<Device> extensions
931 * revision 1.1.x = added drmCommand entry points for device extensions
932 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000933 * revision 1.2.x = added drmSetInterfaceVersion
934 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100935 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000936 */
Dave Airlie79038752006-11-08 15:08:09 +1100937 version->version_major = 1;
938 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000939 version->version_patchlevel = 0;
940
941 return (drmVersionPtr)version;
942}
943
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700944drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000945{
Jan Vesely50d3c852016-06-30 14:22:52 -0400946 struct drm_get_cap cap;
947 int ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000948
Jan Vesely50d3c852016-06-30 14:22:52 -0400949 memclear(cap);
950 cap.capability = capability;
Daniel Vetterfd387942015-02-11 12:41:04 +0100951
Jan Vesely50d3c852016-06-30 14:22:52 -0400952 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
953 if (ret)
954 return ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000955
Jan Vesely50d3c852016-06-30 14:22:52 -0400956 *value = cap.value;
957 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000958}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000959
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700960drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100961{
Jan Vesely50d3c852016-06-30 14:22:52 -0400962 struct drm_set_client_cap cap;
Daniel Vetterfd387942015-02-11 12:41:04 +0100963
Jan Vesely50d3c852016-06-30 14:22:52 -0400964 memclear(cap);
965 cap.capability = capability;
966 cap.value = value;
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100967
Jan Vesely50d3c852016-06-30 14:22:52 -0400968 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100969}
970
Jose Fonsecad2443b22003-05-27 00:37:33 +0000971/**
972 * Free the bus ID information.
973 *
974 * \param busid bus ID information string as given by drmGetBusid().
975 *
976 * \internal
977 * This function is just frees the memory pointed by \p busid.
978 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700979drm_public void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000980{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000981 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000982}
983
Jose Fonsecad2443b22003-05-27 00:37:33 +0000984
985/**
986 * Get the bus ID of the device.
987 *
988 * \param fd file descriptor.
989 *
990 * \return bus ID string.
991 *
992 * \internal
993 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
994 * get the string length and data, passing the arguments in a drm_unique
995 * structure.
996 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700997drm_public char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000998{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000999 drm_unique_t u;
1000
Daniel Vetterfd387942015-02-11 12:41:04 +01001001 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001002
Keith Packard8b9ab102008-06-13 16:03:22 -07001003 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Jan Vesely50d3c852016-06-30 14:22:52 -04001004 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001005 u.unique = drmMalloc(u.unique_len + 1);
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001006 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
1007 drmFree(u.unique);
Jan Vesely50d3c852016-06-30 14:22:52 -04001008 return NULL;
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001009 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001010 u.unique[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +00001011
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001012 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +00001013}
1014
Jose Fonsecad2443b22003-05-27 00:37:33 +00001015
1016/**
1017 * Set the bus ID of the device.
1018 *
1019 * \param fd file descriptor.
1020 * \param busid bus ID string.
1021 *
1022 * \return zero on success, negative on failure.
1023 *
1024 * \internal
1025 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1026 * the arguments in a drm_unique structure.
1027 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001028drm_public int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +00001029{
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001030 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +00001031
Daniel Vetterfd387942015-02-11 12:41:04 +01001032 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001033 u.unique = (char *)busid;
1034 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +00001035
Keith Packard8b9ab102008-06-13 16:03:22 -07001036 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001037 return -errno;
David Dawes56bd9c22001-07-30 19:59:39 +00001038 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001039 return 0;
1040}
1041
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001042drm_public int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001043{
1044 drm_auth_t auth;
1045
Daniel Vetterfd387942015-02-11 12:41:04 +01001046 memclear(auth);
1047
Daryll Straussb3a57661999-12-05 01:19:48 +00001048 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001049 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Jan Vesely50d3c852016-06-30 14:22:52 -04001050 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001051 *magic = auth.magic;
1052 return 0;
1053}
1054
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001055drm_public int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001056{
1057 drm_auth_t auth;
1058
Daniel Vetterfd387942015-02-11 12:41:04 +01001059 memclear(auth);
Daryll Straussb3a57661999-12-05 01:19:48 +00001060 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -07001061 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Jan Vesely50d3c852016-06-30 14:22:52 -04001062 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001063 return 0;
1064}
1065
Jose Fonsecad2443b22003-05-27 00:37:33 +00001066/**
1067 * Specifies a range of memory that is available for mapping by a
1068 * non-root process.
1069 *
1070 * \param fd file descriptor.
1071 * \param offset usually the physical address. The actual meaning depends of
1072 * the \p type parameter. See below.
1073 * \param size of the memory in bytes.
1074 * \param type type of the memory to be mapped.
1075 * \param flags combination of several flags to modify the function actions.
1076 * \param handle will be set to a value that may be used as the offset
1077 * parameter for mmap().
Jan Vesely50d3c852016-06-30 14:22:52 -04001078 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001079 * \return zero on success or a negative value on error.
1080 *
1081 * \par Mapping the frame buffer
1082 * For the frame buffer
1083 * - \p offset will be the physical address of the start of the frame buffer,
1084 * - \p size will be the size of the frame buffer in bytes, and
1085 * - \p type will be DRM_FRAME_BUFFER.
1086 *
1087 * \par
1088 * The area mapped will be uncached. If MTRR support is available in the
Jan Vesely50d3c852016-06-30 14:22:52 -04001089 * kernel, the frame buffer area will be set to write combining.
Jose Fonsecad2443b22003-05-27 00:37:33 +00001090 *
1091 * \par Mapping the MMIO register area
1092 * For the MMIO register area,
1093 * - \p offset will be the physical address of the start of the register area,
1094 * - \p size will be the size of the register area bytes, and
1095 * - \p type will be DRM_REGISTERS.
1096 * \par
Jan Vesely50d3c852016-06-30 14:22:52 -04001097 * The area mapped will be uncached.
1098 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001099 * \par Mapping the SAREA
1100 * For the SAREA,
1101 * - \p offset will be ignored and should be set to zero,
1102 * - \p size will be the desired size of the SAREA in bytes,
1103 * - \p type will be DRM_SHM.
Jan Vesely50d3c852016-06-30 14:22:52 -04001104 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001105 * \par
1106 * A shared memory area of the requested size will be created and locked in
1107 * kernel memory. This area may be mapped into client-space by using the handle
Jan Vesely50d3c852016-06-30 14:22:52 -04001108 * returned.
1109 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001110 * \note May only be called by root.
1111 *
1112 * \internal
1113 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1114 * the arguments in a drm_map structure.
1115 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001116drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1117 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001118{
1119 drm_map_t map;
1120
Daniel Vetterfd387942015-02-11 12:41:04 +01001121 memclear(map);
Daryll Straussb3a57661999-12-05 01:19:48 +00001122 map.offset = offset;
1123 map.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001124 map.type = type;
1125 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -07001126 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04001127 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06001128 if (handle)
Jan Vesely50d3c852016-06-30 14:22:52 -04001129 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001130 return 0;
1131}
1132
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001133drm_public int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00001134{
1135 drm_map_t map;
1136
Daniel Vetterfd387942015-02-11 12:41:04 +01001137 memclear(map);
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001138 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001139
Keith Packard8b9ab102008-06-13 16:03:22 -07001140 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04001141 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001142 return 0;
1143}
1144
Jose Fonsecad2443b22003-05-27 00:37:33 +00001145/**
1146 * Make buffers available for DMA transfers.
Jan Vesely50d3c852016-06-30 14:22:52 -04001147 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001148 * \param fd file descriptor.
1149 * \param count number of buffers.
1150 * \param size size of each buffer.
1151 * \param flags buffer allocation flags.
Jan Vesely50d3c852016-06-30 14:22:52 -04001152 * \param agp_offset offset in the AGP aperture
Jose Fonsecad2443b22003-05-27 00:37:33 +00001153 *
1154 * \return number of buffers allocated, negative on error.
1155 *
1156 * \internal
1157 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1158 *
1159 * \sa drm_buf_desc.
1160 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001161drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1162 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001163{
1164 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001165
Daniel Vetterfd387942015-02-11 12:41:04 +01001166 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001167 request.count = count;
1168 request.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001169 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001170 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001171
Keith Packard8b9ab102008-06-13 16:03:22 -07001172 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Jan Vesely50d3c852016-06-30 14:22:52 -04001173 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001174 return request.count;
1175}
1176
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001177drm_public int drmMarkBufs(int fd, double low, double high)
Daryll Straussb3a57661999-12-05 01:19:48 +00001178{
1179 drm_buf_info_t info;
1180 int i;
1181
Daniel Vetterfd387942015-02-11 12:41:04 +01001182 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001183
Keith Packard8b9ab102008-06-13 16:03:22 -07001184 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Jan Vesely50d3c852016-06-30 14:22:52 -04001185 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001186
Brianccd7b6e2007-05-29 14:54:00 -06001187 if (!info.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001188 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001189
Daryll Straussb3a57661999-12-05 01:19:48 +00001190 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
Jan Vesely50d3c852016-06-30 14:22:52 -04001191 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001192
Keith Packard8b9ab102008-06-13 16:03:22 -07001193 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001194 int retval = -errno;
1195 drmFree(info.list);
1196 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001197 }
Gareth Hughes36047532001-02-15 08:12:14 +00001198
Daryll Straussb3a57661999-12-05 01:19:48 +00001199 for (i = 0; i < info.count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001200 info.list[i].low_mark = low * info.list[i].count;
1201 info.list[i].high_mark = high * info.list[i].count;
1202 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1203 int retval = -errno;
1204 drmFree(info.list);
1205 return retval;
1206 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001207 }
1208 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001209
Daryll Straussb3a57661999-12-05 01:19:48 +00001210 return 0;
1211}
1212
Jose Fonsecad2443b22003-05-27 00:37:33 +00001213/**
1214 * Free buffers.
1215 *
1216 * \param fd file descriptor.
1217 * \param count number of buffers to free.
1218 * \param list list of buffers to be freed.
1219 *
1220 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001221 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001222 * \note This function is primarily used for debugging.
Jan Vesely50d3c852016-06-30 14:22:52 -04001223 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001224 * \internal
1225 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1226 * the arguments in a drm_buf_free structure.
1227 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001228drm_public int drmFreeBufs(int fd, int count, int *list)
Daryll Straussb3a57661999-12-05 01:19:48 +00001229{
1230 drm_buf_free_t request;
1231
Daniel Vetterfd387942015-02-11 12:41:04 +01001232 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001233 request.count = count;
1234 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001235 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Jan Vesely50d3c852016-06-30 14:22:52 -04001236 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001237 return 0;
1238}
1239
Jose Fonsecad2443b22003-05-27 00:37:33 +00001240
1241/**
1242 * Close the device.
1243 *
1244 * \param fd file descriptor.
1245 *
1246 * \internal
1247 * This function closes the file descriptor.
1248 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001249drm_public int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001250{
1251 unsigned long key = drmGetKeyFromFd(fd);
1252 drmHashEntry *entry = drmGetEntry(fd);
1253
1254 drmHashDestroy(entry->tagTable);
1255 entry->fd = 0;
1256 entry->f = NULL;
1257 entry->tagTable = NULL;
1258
1259 drmHashDelete(drmHashTable, key);
1260 drmFree(entry);
1261
1262 return close(fd);
1263}
1264
Jose Fonsecad2443b22003-05-27 00:37:33 +00001265
1266/**
1267 * Map a region of memory.
1268 *
1269 * \param fd file descriptor.
1270 * \param handle handle returned by drmAddMap().
1271 * \param size size in bytes. Must match the size used by drmAddMap().
1272 * \param address will contain the user-space virtual address where the mapping
1273 * begins.
1274 *
1275 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001276 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001277 * \internal
1278 * This function is a wrapper for mmap().
1279 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001280drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1281 drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001282{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001283 static unsigned long pagesize_mask = 0;
1284
Brianccd7b6e2007-05-29 14:54:00 -06001285 if (fd < 0)
Jan Vesely50d3c852016-06-30 14:22:52 -04001286 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001287
1288 if (!pagesize_mask)
Jan Vesely50d3c852016-06-30 14:22:52 -04001289 pagesize_mask = getpagesize() - 1;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001290
1291 size = (size + pagesize_mask) & ~pagesize_mask;
1292
Emil Velikovfaf51d52014-09-07 20:03:05 +01001293 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001294 if (*address == MAP_FAILED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001295 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001296 return 0;
1297}
1298
Jose Fonsecad2443b22003-05-27 00:37:33 +00001299
1300/**
1301 * Unmap mappings obtained with drmMap().
1302 *
1303 * \param address address as given by drmMap().
1304 * \param size size in bytes. Must match the size used by drmMap().
Jan Vesely50d3c852016-06-30 14:22:52 -04001305 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001306 * \return zero on success, or a negative value on failure.
1307 *
1308 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001309 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001310 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001311drm_public int drmUnmap(drmAddress address, drmSize size)
Daryll Straussb3a57661999-12-05 01:19:48 +00001312{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001313 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001314}
1315
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001316drm_public drmBufInfoPtr drmGetBufInfo(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001317{
1318 drm_buf_info_t info;
1319 drmBufInfoPtr retval;
1320 int i;
1321
Daniel Vetterfd387942015-02-11 12:41:04 +01001322 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001323
Keith Packard8b9ab102008-06-13 16:03:22 -07001324 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Jan Vesely50d3c852016-06-30 14:22:52 -04001325 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001326
1327 if (info.count) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001328 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1329 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001330
Jan Vesely50d3c852016-06-30 14:22:52 -04001331 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1332 drmFree(info.list);
1333 return NULL;
1334 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001335
Jan Vesely50d3c852016-06-30 14:22:52 -04001336 retval = drmMalloc(sizeof(*retval));
1337 retval->count = info.count;
1338 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1339 for (i = 0; i < info.count; i++) {
1340 retval->list[i].count = info.list[i].count;
1341 retval->list[i].size = info.list[i].size;
1342 retval->list[i].low_mark = info.list[i].low_mark;
1343 retval->list[i].high_mark = info.list[i].high_mark;
1344 }
1345 drmFree(info.list);
1346 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001347 }
1348 return NULL;
1349}
1350
Jose Fonsecad2443b22003-05-27 00:37:33 +00001351/**
1352 * Map all DMA buffers into client-virtual space.
1353 *
1354 * \param fd file descriptor.
1355 *
1356 * \return a pointer to a ::drmBufMap structure.
1357 *
1358 * \note The client may not use these buffers until obtaining buffer indices
1359 * with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001360 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001361 * \internal
1362 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1363 * information about the buffers in a drm_buf_map structure into the
1364 * client-visible data structures.
Jan Vesely50d3c852016-06-30 14:22:52 -04001365 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001366drm_public drmBufMapPtr drmMapBufs(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001367{
1368 drm_buf_map_t bufs;
1369 drmBufMapPtr retval;
1370 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001371
Daniel Vetterfd387942015-02-11 12:41:04 +01001372 memclear(bufs);
Keith Packard8b9ab102008-06-13 16:03:22 -07001373 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Jan Vesely50d3c852016-06-30 14:22:52 -04001374 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001375
Brianccd7b6e2007-05-29 14:54:00 -06001376 if (!bufs.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001377 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001378
Jan Vesely50d3c852016-06-30 14:22:52 -04001379 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1380 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001381
Jan Vesely50d3c852016-06-30 14:22:52 -04001382 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1383 drmFree(bufs.list);
1384 return NULL;
1385 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001386
Jan Vesely50d3c852016-06-30 14:22:52 -04001387 retval = drmMalloc(sizeof(*retval));
1388 retval->count = bufs.count;
1389 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1390 for (i = 0; i < bufs.count; i++) {
1391 retval->list[i].idx = bufs.list[i].idx;
1392 retval->list[i].total = bufs.list[i].total;
1393 retval->list[i].used = 0;
1394 retval->list[i].address = bufs.list[i].address;
1395 }
Jon Smirl8696e712004-07-07 04:36:36 +00001396
Jan Vesely50d3c852016-06-30 14:22:52 -04001397 drmFree(bufs.list);
1398 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001399}
1400
Jose Fonsecad2443b22003-05-27 00:37:33 +00001401
1402/**
1403 * Unmap buffers allocated with drmMapBufs().
1404 *
1405 * \return zero on success, or negative value on failure.
1406 *
1407 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001408 * Calls munmap() for every buffer stored in \p bufs and frees the
1409 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001410 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001411drm_public int drmUnmapBufs(drmBufMapPtr bufs)
Daryll Straussb3a57661999-12-05 01:19:48 +00001412{
1413 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001414
Daryll Straussb3a57661999-12-05 01:19:48 +00001415 for (i = 0; i < bufs->count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001416 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001417 }
Jon Smirl8696e712004-07-07 04:36:36 +00001418
1419 drmFree(bufs->list);
1420 drmFree(bufs);
Daryll Straussb3a57661999-12-05 01:19:48 +00001421 return 0;
1422}
1423
Jose Fonsecad2443b22003-05-27 00:37:33 +00001424
Jan Vesely50d3c852016-06-30 14:22:52 -04001425#define DRM_DMA_RETRY 16
Gareth Hughes36047532001-02-15 08:12:14 +00001426
Jose Fonsecad2443b22003-05-27 00:37:33 +00001427/**
1428 * Reserve DMA buffers.
1429 *
1430 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001431 * \param request
1432 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001433 * \return zero on success, or a negative value on failure.
1434 *
1435 * \internal
1436 * Assemble the arguments into a drm_dma structure and keeps issuing the
1437 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1438 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001439drm_public int drmDMA(int fd, drmDMAReqPtr request)
Daryll Straussb3a57661999-12-05 01:19:48 +00001440{
1441 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001442 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001443
Daryll Straussb3a57661999-12-05 01:19:48 +00001444 dma.context = request->context;
1445 dma.send_count = request->send_count;
1446 dma.send_indices = request->send_list;
1447 dma.send_sizes = request->send_sizes;
1448 dma.flags = request->flags;
1449 dma.request_count = request->request_count;
1450 dma.request_size = request->request_size;
1451 dma.request_indices = request->request_list;
1452 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001453 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001454
1455 do {
Jan Vesely50d3c852016-06-30 14:22:52 -04001456 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
Gareth Hughes36047532001-02-15 08:12:14 +00001457 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1458
1459 if ( ret == 0 ) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001460 request->granted_count = dma.granted_count;
1461 return 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001462 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -04001463 return -errno;
Gareth Hughes36047532001-02-15 08:12:14 +00001464 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001465}
1466
Jose Fonsecad2443b22003-05-27 00:37:33 +00001467
1468/**
1469 * Obtain heavyweight hardware lock.
1470 *
1471 * \param fd file descriptor.
1472 * \param context context.
Eric Engestrom360292c2018-12-19 14:55:45 +00001473 * \param flags flags that determine the state of the hardware when the function
Jose Fonsecad2443b22003-05-27 00:37:33 +00001474 * returns.
Jan Vesely50d3c852016-06-30 14:22:52 -04001475 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001476 * \return always zero.
Jan Vesely50d3c852016-06-30 14:22:52 -04001477 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001478 * \internal
1479 * This function translates the arguments into a drm_lock structure and issue
1480 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1481 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001482drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001483{
1484 drm_lock_t lock;
1485
Daniel Vetterfd387942015-02-11 12:41:04 +01001486 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001487 lock.context = context;
1488 lock.flags = 0;
1489 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1490 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1491 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1492 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1493 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1494 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001495
Keith Packard8b9ab102008-06-13 16:03:22 -07001496 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04001497 ;
Daryll Straussb3a57661999-12-05 01:19:48 +00001498 return 0;
1499}
1500
Jose Fonsecad2443b22003-05-27 00:37:33 +00001501/**
1502 * Release the hardware lock.
1503 *
1504 * \param fd file descriptor.
1505 * \param context context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001506 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001507 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001508 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001509 * \internal
1510 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1511 * argument in a drm_lock structure.
1512 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001513drm_public int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001514{
1515 drm_lock_t lock;
1516
Daniel Vetterfd387942015-02-11 12:41:04 +01001517 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001518 lock.context = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001519 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001520}
1521
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001522drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001523{
1524 drm_ctx_res_t res;
1525 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001526 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001527 int i;
1528
Daniel Vetterfd387942015-02-11 12:41:04 +01001529 memclear(res);
Keith Packard8b9ab102008-06-13 16:03:22 -07001530 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Jan Vesely50d3c852016-06-30 14:22:52 -04001531 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001532
Brianccd7b6e2007-05-29 14:54:00 -06001533 if (!res.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001534 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001535
Brianccd7b6e2007-05-29 14:54:00 -06001536 if (!(list = drmMalloc(res.count * sizeof(*list))))
Jan Vesely50d3c852016-06-30 14:22:52 -04001537 return NULL;
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001538 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1539 goto err_free_list;
Daryll Straussb3a57661999-12-05 01:19:48 +00001540
1541 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001542 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001543 goto err_free_context;
Daryll Straussb3a57661999-12-05 01:19:48 +00001544
Brianccd7b6e2007-05-29 14:54:00 -06001545 for (i = 0; i < res.count; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04001546 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001547 drmFree(list);
1548
1549 *count = res.count;
1550 return retval;
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001551
1552err_free_list:
1553 drmFree(list);
1554err_free_context:
1555 drmFree(retval);
1556 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001557}
1558
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001559drm_public void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001560{
1561 drmFree(pt);
1562}
1563
Jose Fonsecad2443b22003-05-27 00:37:33 +00001564/**
1565 * Create context.
1566 *
1567 * Used by the X server during GLXContext initialization. This causes
1568 * per-context kernel-level resources to be allocated.
1569 *
1570 * \param fd file descriptor.
1571 * \param handle is set on success. To be used by the client when requesting DMA
1572 * dispatch with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001573 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001574 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001575 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001576 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001577 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001578 * \internal
1579 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1580 * argument in a drm_ctx structure.
1581 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001582drm_public int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001583{
1584 drm_ctx_t ctx;
1585
Daniel Vetterfd387942015-02-11 12:41:04 +01001586 memclear(ctx);
Keith Packard8b9ab102008-06-13 16:03:22 -07001587 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001588 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001589 *handle = ctx.handle;
1590 return 0;
1591}
1592
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001593drm_public int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001594{
1595 drm_ctx_t ctx;
1596
Daniel Vetterfd387942015-02-11 12:41:04 +01001597 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001598 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001599 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001600 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001601 return 0;
1602}
1603
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001604drm_public int drmSetContextFlags(int fd, drm_context_t context,
1605 drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001606{
1607 drm_ctx_t ctx;
1608
Adam Jackson22e41ef2006-02-20 23:09:00 +00001609 /*
1610 * Context preserving means that no context switches are done between DMA
1611 * buffers from one context and the next. This is suitable for use in the
1612 * X server (which promises to maintain hardware context), or in the
1613 * client-side library when buffers are swapped on behalf of two threads.
1614 */
Daniel Vetterfd387942015-02-11 12:41:04 +01001615 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001616 ctx.handle = context;
Brianccd7b6e2007-05-29 14:54:00 -06001617 if (flags & DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001618 ctx.flags |= _DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001619 if (flags & DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001620 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001621 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001622 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001623 return 0;
1624}
1625
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001626drm_public int drmGetContextFlags(int fd, drm_context_t context,
1627 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001628{
1629 drm_ctx_t ctx;
1630
Daniel Vetterfd387942015-02-11 12:41:04 +01001631 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001632 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001633 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001634 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001635 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001636 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001637 *flags |= DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001638 if (ctx.flags & _DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001639 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001640 return 0;
1641}
Gareth Hughes36047532001-02-15 08:12:14 +00001642
Jose Fonsecad2443b22003-05-27 00:37:33 +00001643/**
1644 * Destroy context.
1645 *
1646 * Free any kernel-level resources allocated with drmCreateContext() associated
1647 * with the context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001648 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001649 * \param fd file descriptor.
1650 * \param handle handle given by drmCreateContext().
Jan Vesely50d3c852016-06-30 14:22:52 -04001651 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001652 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001653 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001654 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001655 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001656 * \internal
1657 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1658 * argument in a drm_ctx structure.
1659 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001660drm_public int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001661{
1662 drm_ctx_t ctx;
Daniel Vetterfd387942015-02-11 12:41:04 +01001663
1664 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001665 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001666 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001667 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001668 return 0;
1669}
1670
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001671drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001672{
1673 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001674
1675 memclear(draw);
Keith Packard8b9ab102008-06-13 16:03:22 -07001676 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001677 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001678 *handle = draw.handle;
1679 return 0;
1680}
1681
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001682drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001683{
1684 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001685
1686 memclear(draw);
Daryll Straussb3a57661999-12-05 01:19:48 +00001687 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001688 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001689 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001690 return 0;
1691}
1692
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001693drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1694 drm_drawable_info_type_t type,
1695 unsigned int num, void *data)
Michel Dänzer9810ec22006-08-22 16:40:07 +02001696{
1697 drm_update_draw_t update;
1698
Daniel Vetterfd387942015-02-11 12:41:04 +01001699 memclear(update);
Michel Dänzer9810ec22006-08-22 16:40:07 +02001700 update.handle = handle;
1701 update.type = type;
1702 update.num = num;
1703 update.data = (unsigned long long)(unsigned long)data;
1704
Keith Packard8b9ab102008-06-13 16:03:22 -07001705 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Jan Vesely50d3c852016-06-30 14:22:52 -04001706 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001707
1708 return 0;
1709}
1710
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001711drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1712 uint64_t *ns)
Keith Packardd4331dd2017-07-01 00:43:15 -07001713{
1714 struct drm_crtc_get_sequence get_seq;
1715 int ret;
1716
1717 memclear(get_seq);
1718 get_seq.crtc_id = crtcId;
1719 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1720 if (ret)
1721 return ret;
1722
1723 if (sequence)
1724 *sequence = get_seq.sequence;
1725 if (ns)
1726 *ns = get_seq.sequence_ns;
1727 return 0;
1728}
1729
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001730drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1731 uint64_t sequence,
1732 uint64_t *sequence_queued,
1733 uint64_t user_data)
Keith Packardd4331dd2017-07-01 00:43:15 -07001734{
1735 struct drm_crtc_queue_sequence queue_seq;
1736 int ret;
1737
1738 memclear(queue_seq);
1739 queue_seq.crtc_id = crtcId;
1740 queue_seq.flags = flags;
1741 queue_seq.sequence = sequence;
1742 queue_seq.user_data = user_data;
1743
1744 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1745 if (ret == 0 && sequence_queued)
1746 *sequence_queued = queue_seq.sequence;
1747
1748 return ret;
1749}
1750
Jose Fonsecad2443b22003-05-27 00:37:33 +00001751/**
1752 * Acquire the AGP device.
1753 *
1754 * Must be called before any of the other AGP related calls.
1755 *
1756 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001757 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001758 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001759 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001760 * \internal
1761 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1762 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001763drm_public int drmAgpAcquire(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001764{
Keith Packard8b9ab102008-06-13 16:03:22 -07001765 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001766 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001767 return 0;
1768}
1769
Jose Fonsecad2443b22003-05-27 00:37:33 +00001770
1771/**
1772 * Release the AGP device.
1773 *
1774 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001775 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001776 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001777 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001778 * \internal
1779 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1780 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001781drm_public int drmAgpRelease(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001782{
Keith Packard8b9ab102008-06-13 16:03:22 -07001783 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001784 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001785 return 0;
1786}
1787
Jose Fonsecad2443b22003-05-27 00:37:33 +00001788
1789/**
1790 * Set the AGP mode.
1791 *
1792 * \param fd file descriptor.
1793 * \param mode AGP mode.
Jan Vesely50d3c852016-06-30 14:22:52 -04001794 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001795 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001796 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001797 * \internal
1798 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1799 * argument in a drm_agp_mode structure.
1800 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001801drm_public int drmAgpEnable(int fd, unsigned long mode)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001802{
1803 drm_agp_mode_t m;
1804
Connor Behan14900552015-03-24 13:53:51 -04001805 memclear(m);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001806 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001807 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Jan Vesely50d3c852016-06-30 14:22:52 -04001808 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001809 return 0;
1810}
1811
Jose Fonsecad2443b22003-05-27 00:37:33 +00001812
1813/**
1814 * Allocate a chunk of AGP memory.
1815 *
1816 * \param fd file descriptor.
1817 * \param size requested memory size in bytes. Will be rounded to page boundary.
1818 * \param type type of memory to allocate.
1819 * \param address if not zero, will be set to the physical address of the
1820 * allocated memory.
1821 * \param handle on success will be set to a handle of the allocated memory.
Jan Vesely50d3c852016-06-30 14:22:52 -04001822 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001823 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001824 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001825 * \internal
1826 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1827 * arguments in a drm_agp_buffer structure.
1828 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001829drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1830 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001831{
1832 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001833
Daniel Vetterfd387942015-02-11 12:41:04 +01001834 memclear(b);
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001835 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001836 b.size = size;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001837 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001838 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001839 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06001840 if (address != 0UL)
Jan Vesely50d3c852016-06-30 14:22:52 -04001841 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001842 *handle = b.handle;
1843 return 0;
1844}
1845
Jose Fonsecad2443b22003-05-27 00:37:33 +00001846
1847/**
1848 * Free a chunk of AGP memory.
1849 *
1850 * \param fd file descriptor.
1851 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001852 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001853 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001854 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001855 * \internal
1856 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1857 * argument in a drm_agp_buffer structure.
1858 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001859drm_public int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001860{
1861 drm_agp_buffer_t b;
1862
Daniel Vetterfd387942015-02-11 12:41:04 +01001863 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001864 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001865 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001866 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001867 return 0;
1868}
1869
Jose Fonsecad2443b22003-05-27 00:37:33 +00001870
1871/**
1872 * Bind a chunk of AGP memory.
1873 *
1874 * \param fd file descriptor.
1875 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1876 * \param offset offset in bytes. It will round to page boundary.
Jan Vesely50d3c852016-06-30 14:22:52 -04001877 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001878 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001879 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001880 * \internal
1881 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1882 * argument in a drm_agp_binding structure.
1883 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001884drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001885{
1886 drm_agp_binding_t b;
1887
Daniel Vetterfd387942015-02-11 12:41:04 +01001888 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001889 b.handle = handle;
1890 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001891 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001892 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001893 return 0;
1894}
1895
Jose Fonsecad2443b22003-05-27 00:37:33 +00001896
1897/**
1898 * Unbind a chunk of AGP memory.
1899 *
1900 * \param fd file descriptor.
1901 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001902 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001903 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001904 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001905 * \internal
1906 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1907 * the argument in a drm_agp_binding structure.
1908 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001909drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001910{
1911 drm_agp_binding_t b;
1912
Daniel Vetterfd387942015-02-11 12:41:04 +01001913 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001914 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001915 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001916 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001917 return 0;
1918}
1919
Jose Fonsecad2443b22003-05-27 00:37:33 +00001920
1921/**
1922 * Get AGP driver major version number.
1923 *
1924 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001925 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001926 * \return major version number on success, or a negative value on failure..
Jan Vesely50d3c852016-06-30 14:22:52 -04001927 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001928 * \internal
1929 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1930 * necessary information in a drm_agp_info structure.
1931 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001932drm_public int drmAgpVersionMajor(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001933{
1934 drm_agp_info_t i;
1935
Daniel Vetterfd387942015-02-11 12:41:04 +01001936 memclear(i);
1937
Keith Packard8b9ab102008-06-13 16:03:22 -07001938 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001939 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001940 return i.agp_version_major;
1941}
1942
Jose Fonsecad2443b22003-05-27 00:37:33 +00001943
1944/**
1945 * Get AGP driver minor version number.
1946 *
1947 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001948 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001949 * \return minor version number on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001950 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001951 * \internal
1952 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1953 * necessary information in a drm_agp_info structure.
1954 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001955drm_public int drmAgpVersionMinor(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001956{
1957 drm_agp_info_t i;
1958
Daniel Vetterfd387942015-02-11 12:41:04 +01001959 memclear(i);
1960
Keith Packard8b9ab102008-06-13 16:03:22 -07001961 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001962 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001963 return i.agp_version_minor;
1964}
1965
Jose Fonsecad2443b22003-05-27 00:37:33 +00001966
1967/**
1968 * Get AGP mode.
1969 *
1970 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001971 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001972 * \return mode on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001973 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001974 * \internal
1975 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1976 * necessary information in a drm_agp_info structure.
1977 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001978drm_public unsigned long drmAgpGetMode(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001979{
1980 drm_agp_info_t i;
1981
Daniel Vetterfd387942015-02-11 12:41:04 +01001982 memclear(i);
1983
Keith Packard8b9ab102008-06-13 16:03:22 -07001984 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001985 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001986 return i.mode;
1987}
1988
Jose Fonsecad2443b22003-05-27 00:37:33 +00001989
1990/**
1991 * Get AGP aperture base.
1992 *
1993 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001994 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001995 * \return aperture base on success, zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001996 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001997 * \internal
1998 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1999 * necessary information in a drm_agp_info structure.
2000 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002001drm_public unsigned long drmAgpBase(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002002{
2003 drm_agp_info_t i;
2004
Daniel Vetterfd387942015-02-11 12:41:04 +01002005 memclear(i);
2006
Keith Packard8b9ab102008-06-13 16:03:22 -07002007 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002008 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002009 return i.aperture_base;
2010}
2011
Jose Fonsecad2443b22003-05-27 00:37:33 +00002012
2013/**
2014 * Get AGP aperture size.
2015 *
2016 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002017 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002018 * \return aperture size on success, zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002019 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002020 * \internal
2021 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2022 * necessary information in a drm_agp_info structure.
2023 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002024drm_public unsigned long drmAgpSize(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002025{
2026 drm_agp_info_t i;
2027
Daniel Vetterfd387942015-02-11 12:41:04 +01002028 memclear(i);
2029
Keith Packard8b9ab102008-06-13 16:03:22 -07002030 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002031 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002032 return i.aperture_size;
2033}
2034
Jose Fonsecad2443b22003-05-27 00:37:33 +00002035
2036/**
2037 * Get used AGP memory.
2038 *
2039 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002040 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002041 * \return memory used on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002042 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002043 * \internal
2044 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2045 * necessary information in a drm_agp_info structure.
2046 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002047drm_public unsigned long drmAgpMemoryUsed(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002048{
2049 drm_agp_info_t i;
2050
Daniel Vetterfd387942015-02-11 12:41:04 +01002051 memclear(i);
2052
Keith Packard8b9ab102008-06-13 16:03:22 -07002053 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002054 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002055 return i.memory_used;
2056}
2057
Jose Fonsecad2443b22003-05-27 00:37:33 +00002058
2059/**
2060 * Get available AGP memory.
2061 *
2062 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002063 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002064 * \return memory available on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002065 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002066 * \internal
2067 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2068 * necessary information in a drm_agp_info structure.
2069 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002070drm_public unsigned long drmAgpMemoryAvail(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002071{
2072 drm_agp_info_t i;
2073
Daniel Vetterfd387942015-02-11 12:41:04 +01002074 memclear(i);
2075
Keith Packard8b9ab102008-06-13 16:03:22 -07002076 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002077 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002078 return i.memory_allowed;
2079}
2080
Jose Fonsecad2443b22003-05-27 00:37:33 +00002081
2082/**
2083 * Get hardware vendor ID.
2084 *
2085 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002086 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002087 * \return vendor ID on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002088 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002089 * \internal
2090 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2091 * necessary information in a drm_agp_info structure.
2092 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002093drm_public unsigned int drmAgpVendorId(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002094{
2095 drm_agp_info_t i;
2096
Daniel Vetterfd387942015-02-11 12:41:04 +01002097 memclear(i);
2098
Keith Packard8b9ab102008-06-13 16:03:22 -07002099 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002100 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002101 return i.id_vendor;
2102}
2103
Jose Fonsecad2443b22003-05-27 00:37:33 +00002104
2105/**
2106 * Get hardware device ID.
2107 *
2108 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002109 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002110 * \return zero on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002111 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002112 * \internal
2113 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2114 * necessary information in a drm_agp_info structure.
2115 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002116drm_public unsigned int drmAgpDeviceId(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002117{
2118 drm_agp_info_t i;
2119
Daniel Vetterfd387942015-02-11 12:41:04 +01002120 memclear(i);
2121
Keith Packard8b9ab102008-06-13 16:03:22 -07002122 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002123 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002124 return i.id_device;
2125}
2126
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002127drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2128 drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002129{
2130 drm_scatter_gather_t sg;
2131
Daniel Vetterfd387942015-02-11 12:41:04 +01002132 memclear(sg);
2133
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002134 *handle = 0;
2135 sg.size = size;
Keith Packard8b9ab102008-06-13 16:03:22 -07002136 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002137 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002138 *handle = sg.handle;
2139 return 0;
2140}
2141
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002142drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002143{
2144 drm_scatter_gather_t sg;
2145
Daniel Vetterfd387942015-02-11 12:41:04 +01002146 memclear(sg);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002147 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07002148 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002149 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002150 return 0;
2151}
2152
Jose Fonsecad2443b22003-05-27 00:37:33 +00002153/**
2154 * Wait for VBLANK.
2155 *
2156 * \param fd file descriptor.
2157 * \param vbl pointer to a drmVBlank structure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002158 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002159 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002160 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002161 * \internal
2162 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2163 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002164drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002165{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002166 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002167 int ret;
2168
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002169 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2170 if (ret < 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002171 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2172 goto out;
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002173 }
2174 timeout.tv_sec++;
2175
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002176 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002177 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00002178 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08002179 if (ret && errno == EINTR) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002180 clock_gettime(CLOCK_MONOTONIC, &cur);
2181 /* Timeout after 1s */
2182 if (cur.tv_sec > timeout.tv_sec + 1 ||
2183 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2184 timeout.tv_nsec)) {
2185 errno = EBUSY;
2186 ret = -1;
2187 break;
2188 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002189 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002190 } while (ret && errno == EINTR);
2191
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002192out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002193 return ret;
2194}
2195
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002196drm_public int drmError(int err, const char *label)
Daryll Straussb3a57661999-12-05 01:19:48 +00002197{
2198 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002199 case DRM_ERR_NO_DEVICE:
Jan Vesely50d3c852016-06-30 14:22:52 -04002200 fprintf(stderr, "%s: no device\n", label);
2201 break;
Brianccd7b6e2007-05-29 14:54:00 -06002202 case DRM_ERR_NO_ACCESS:
Jan Vesely50d3c852016-06-30 14:22:52 -04002203 fprintf(stderr, "%s: no access\n", label);
2204 break;
Brianccd7b6e2007-05-29 14:54:00 -06002205 case DRM_ERR_NOT_ROOT:
Jan Vesely50d3c852016-06-30 14:22:52 -04002206 fprintf(stderr, "%s: not root\n", label);
2207 break;
Brianccd7b6e2007-05-29 14:54:00 -06002208 case DRM_ERR_INVALID:
Jan Vesely50d3c852016-06-30 14:22:52 -04002209 fprintf(stderr, "%s: invalid args\n", label);
2210 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002211 default:
Jan Vesely50d3c852016-06-30 14:22:52 -04002212 if (err < 0)
2213 err = -err;
2214 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2215 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002216 }
2217
2218 return 1;
2219}
2220
Jose Fonsecad2443b22003-05-27 00:37:33 +00002221/**
2222 * Install IRQ handler.
2223 *
2224 * \param fd file descriptor.
2225 * \param irq IRQ number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002226 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002227 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002228 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002229 * \internal
2230 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2231 * argument in a drm_control structure.
2232 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002233drm_public int drmCtlInstHandler(int fd, int irq)
Daryll Straussb3a57661999-12-05 01:19:48 +00002234{
2235 drm_control_t ctl;
2236
Daniel Vetterfd387942015-02-11 12:41:04 +01002237 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002238 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002239 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002240 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002241 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002242 return 0;
2243}
2244
Jose Fonsecad2443b22003-05-27 00:37:33 +00002245
2246/**
2247 * Uninstall IRQ handler.
2248 *
2249 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002250 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002251 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002252 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002253 * \internal
2254 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2255 * argument in a drm_control structure.
2256 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002257drm_public int drmCtlUninstHandler(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00002258{
2259 drm_control_t ctl;
2260
Daniel Vetterfd387942015-02-11 12:41:04 +01002261 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002262 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002263 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002264 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002265 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002266 return 0;
2267}
2268
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002269drm_public int drmFinish(int fd, int context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00002270{
2271 drm_lock_t lock;
2272
Daniel Vetterfd387942015-02-11 12:41:04 +01002273 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00002274 lock.context = context;
Daryll Straussb3a57661999-12-05 01:19:48 +00002275 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2276 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2277 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2278 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2279 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2280 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002281 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04002282 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002283 return 0;
2284}
2285
Jose Fonsecad2443b22003-05-27 00:37:33 +00002286/**
2287 * Get IRQ from bus ID.
2288 *
2289 * \param fd file descriptor.
2290 * \param busnum bus number.
2291 * \param devnum device number.
2292 * \param funcnum function number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002293 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002294 * \return IRQ number on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002295 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002296 * \internal
2297 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2298 * arguments in a drm_irq_busid structure.
2299 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002300drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2301 int funcnum)
Daryll Straussb3a57661999-12-05 01:19:48 +00002302{
2303 drm_irq_busid_t p;
2304
Daniel Vetterfd387942015-02-11 12:41:04 +01002305 memclear(p);
Daryll Straussb3a57661999-12-05 01:19:48 +00002306 p.busnum = busnum;
2307 p.devnum = devnum;
2308 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002309 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Jan Vesely50d3c852016-06-30 14:22:52 -04002310 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002311 return p.irq;
2312}
2313
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002314drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002315{
2316 drmHashEntry *entry = drmGetEntry(fd);
2317
2318 if (drmHashInsert(entry->tagTable, context, tag)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002319 drmHashDelete(entry->tagTable, context);
2320 drmHashInsert(entry->tagTable, context, tag);
Daryll Straussb3a57661999-12-05 01:19:48 +00002321 }
2322 return 0;
2323}
2324
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002325drm_public int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002326{
2327 drmHashEntry *entry = drmGetEntry(fd);
2328
2329 return drmHashDelete(entry->tagTable, context);
2330}
2331
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002332drm_public void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002333{
2334 drmHashEntry *entry = drmGetEntry(fd);
2335 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002336
Brianccd7b6e2007-05-29 14:54:00 -06002337 if (drmHashLookup(entry->tagTable, context, &value))
Jan Vesely50d3c852016-06-30 14:22:52 -04002338 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002339
2340 return value;
2341}
2342
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002343drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2344 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002345{
2346 drm_ctx_priv_map_t map;
2347
Daniel Vetterfd387942015-02-11 12:41:04 +01002348 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002349 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002350 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002351
Keith Packard8b9ab102008-06-13 16:03:22 -07002352 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002353 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002354 return 0;
2355}
2356
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002357drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2358 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002359{
2360 drm_ctx_priv_map_t map;
2361
Daniel Vetterfd387942015-02-11 12:41:04 +01002362 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002363 map.ctx_id = ctx_id;
2364
Keith Packard8b9ab102008-06-13 16:03:22 -07002365 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002366 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06002367 if (handle)
Jan Vesely50d3c852016-06-30 14:22:52 -04002368 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002369
2370 return 0;
2371}
2372
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002373drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2374 drmMapType *type, drmMapFlags *flags,
2375 drm_handle_t *handle, int *mtrr)
Rik Faith88dbee52001-02-28 09:27:44 +00002376{
2377 drm_map_t map;
2378
Daniel Vetterfd387942015-02-11 12:41:04 +01002379 memclear(map);
Rik Faith88dbee52001-02-28 09:27:44 +00002380 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002381 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002382 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002383 *offset = map.offset;
2384 *size = map.size;
2385 *type = map.type;
2386 *flags = map.flags;
2387 *handle = (unsigned long)map.handle;
2388 *mtrr = map.mtrr;
2389 return 0;
2390}
2391
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002392drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2393 unsigned long *magic, unsigned long *iocs)
Rik Faith88dbee52001-02-28 09:27:44 +00002394{
2395 drm_client_t client;
2396
Daniel Vetterfd387942015-02-11 12:41:04 +01002397 memclear(client);
Rik Faith88dbee52001-02-28 09:27:44 +00002398 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002399 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Jan Vesely50d3c852016-06-30 14:22:52 -04002400 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002401 *auth = client.auth;
2402 *pid = client.pid;
2403 *uid = client.uid;
2404 *magic = client.magic;
2405 *iocs = client.iocs;
2406 return 0;
2407}
2408
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002409drm_public int drmGetStats(int fd, drmStatsT *stats)
Rik Faith88dbee52001-02-28 09:27:44 +00002410{
2411 drm_stats_t s;
Jan Veselyde8532d2014-11-30 12:53:18 -05002412 unsigned i;
Rik Faith88dbee52001-02-28 09:27:44 +00002413
Daniel Vetterfd387942015-02-11 12:41:04 +01002414 memclear(s);
Keith Packard8b9ab102008-06-13 16:03:22 -07002415 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Jan Vesely50d3c852016-06-30 14:22:52 -04002416 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002417
2418 stats->count = 0;
2419 memset(stats, 0, sizeof(*stats));
2420 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
Jan Vesely50d3c852016-06-30 14:22:52 -04002421 return -1;
Rik Faith88dbee52001-02-28 09:27:44 +00002422
2423#define SET_VALUE \
2424 stats->data[i].long_format = "%-20.20s"; \
2425 stats->data[i].rate_format = "%8.8s"; \
2426 stats->data[i].isvalue = 1; \
2427 stats->data[i].verbose = 0
2428
2429#define SET_COUNT \
2430 stats->data[i].long_format = "%-20.20s"; \
2431 stats->data[i].rate_format = "%5.5s"; \
2432 stats->data[i].isvalue = 0; \
2433 stats->data[i].mult_names = "kgm"; \
2434 stats->data[i].mult = 1000; \
2435 stats->data[i].verbose = 0
2436
2437#define SET_BYTE \
2438 stats->data[i].long_format = "%-20.20s"; \
2439 stats->data[i].rate_format = "%5.5s"; \
2440 stats->data[i].isvalue = 0; \
2441 stats->data[i].mult_names = "KGM"; \
2442 stats->data[i].mult = 1024; \
2443 stats->data[i].verbose = 0
2444
2445
2446 stats->count = s.count;
2447 for (i = 0; i < s.count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002448 stats->data[i].value = s.data[i].value;
2449 switch (s.data[i].type) {
2450 case _DRM_STAT_LOCK:
2451 stats->data[i].long_name = "Lock";
2452 stats->data[i].rate_name = "Lock";
2453 SET_VALUE;
2454 break;
2455 case _DRM_STAT_OPENS:
2456 stats->data[i].long_name = "Opens";
2457 stats->data[i].rate_name = "O";
2458 SET_COUNT;
2459 stats->data[i].verbose = 1;
2460 break;
2461 case _DRM_STAT_CLOSES:
2462 stats->data[i].long_name = "Closes";
2463 stats->data[i].rate_name = "Lock";
2464 SET_COUNT;
2465 stats->data[i].verbose = 1;
2466 break;
2467 case _DRM_STAT_IOCTLS:
2468 stats->data[i].long_name = "Ioctls";
2469 stats->data[i].rate_name = "Ioc/s";
2470 SET_COUNT;
2471 break;
2472 case _DRM_STAT_LOCKS:
2473 stats->data[i].long_name = "Locks";
2474 stats->data[i].rate_name = "Lck/s";
2475 SET_COUNT;
2476 break;
2477 case _DRM_STAT_UNLOCKS:
2478 stats->data[i].long_name = "Unlocks";
2479 stats->data[i].rate_name = "Unl/s";
2480 SET_COUNT;
2481 break;
2482 case _DRM_STAT_IRQ:
2483 stats->data[i].long_name = "IRQs";
2484 stats->data[i].rate_name = "IRQ/s";
2485 SET_COUNT;
2486 break;
2487 case _DRM_STAT_PRIMARY:
2488 stats->data[i].long_name = "Primary Bytes";
2489 stats->data[i].rate_name = "PB/s";
2490 SET_BYTE;
2491 break;
2492 case _DRM_STAT_SECONDARY:
2493 stats->data[i].long_name = "Secondary Bytes";
2494 stats->data[i].rate_name = "SB/s";
2495 SET_BYTE;
2496 break;
2497 case _DRM_STAT_DMA:
2498 stats->data[i].long_name = "DMA";
2499 stats->data[i].rate_name = "DMA/s";
2500 SET_COUNT;
2501 break;
2502 case _DRM_STAT_SPECIAL:
2503 stats->data[i].long_name = "Special DMA";
2504 stats->data[i].rate_name = "dma/s";
2505 SET_COUNT;
2506 break;
2507 case _DRM_STAT_MISSED:
2508 stats->data[i].long_name = "Miss";
2509 stats->data[i].rate_name = "Ms/s";
2510 SET_COUNT;
2511 break;
2512 case _DRM_STAT_VALUE:
2513 stats->data[i].long_name = "Value";
2514 stats->data[i].rate_name = "Value";
2515 SET_VALUE;
2516 break;
2517 case _DRM_STAT_BYTE:
2518 stats->data[i].long_name = "Bytes";
2519 stats->data[i].rate_name = "B/s";
2520 SET_BYTE;
2521 break;
2522 case _DRM_STAT_COUNT:
2523 default:
2524 stats->data[i].long_name = "Count";
2525 stats->data[i].rate_name = "Cnt/s";
2526 SET_COUNT;
2527 break;
2528 }
Rik Faith88dbee52001-02-28 09:27:44 +00002529 }
2530 return 0;
2531}
2532
Jose Fonsecad2443b22003-05-27 00:37:33 +00002533/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002534 * Issue a set-version ioctl.
2535 *
2536 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002537 * \param drmCommandIndex command index
Eric Anholt06cb1322003-10-23 02:23:31 +00002538 * \param data source pointer of the data to be read and written.
2539 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002540 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002541 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002542 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002543 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002544 * It issues a read-write ioctl given by
Eric Anholt06cb1322003-10-23 02:23:31 +00002545 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2546 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002547drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002548{
2549 int retcode = 0;
2550 drm_set_version_t sv;
2551
Daniel Vetterfd387942015-02-11 12:41:04 +01002552 memclear(sv);
Eric Anholt06cb1322003-10-23 02:23:31 +00002553 sv.drm_di_major = version->drm_di_major;
2554 sv.drm_di_minor = version->drm_di_minor;
2555 sv.drm_dd_major = version->drm_dd_major;
2556 sv.drm_dd_minor = version->drm_dd_minor;
2557
Keith Packard8b9ab102008-06-13 16:03:22 -07002558 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002559 retcode = -errno;
Eric Anholt06cb1322003-10-23 02:23:31 +00002560 }
2561
2562 version->drm_di_major = sv.drm_di_major;
2563 version->drm_di_minor = sv.drm_di_minor;
2564 version->drm_dd_major = sv.drm_dd_major;
2565 version->drm_dd_minor = sv.drm_dd_minor;
2566
2567 return retcode;
2568}
2569
2570/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002571 * Send a device-specific command.
2572 *
2573 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002574 * \param drmCommandIndex command index
2575 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002576 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002577 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002578 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002579 * It issues a ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002580 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2581 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002582drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
Jens Owen3903e5a2002-04-09 21:54:56 +00002583{
Jens Owen3903e5a2002-04-09 21:54:56 +00002584 unsigned long request;
2585
2586 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2587
Daniel Vetterfd387942015-02-11 12:41:04 +01002588 if (drmIoctl(fd, request, NULL)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002589 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002590 }
2591 return 0;
2592}
2593
Jose Fonsecad2443b22003-05-27 00:37:33 +00002594
2595/**
2596 * Send a device-specific read command.
2597 *
2598 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002599 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002600 * \param data destination pointer of the data to be read.
2601 * \param size size of the data to be read.
Jan Vesely50d3c852016-06-30 14:22:52 -04002602 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002603 * \return zero on success, or a negative value on failure.
2604 *
2605 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002606 * It issues a read ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002607 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2608 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002609drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2610 void *data, unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002611{
2612 unsigned long request;
2613
Jan Vesely50d3c852016-06-30 14:22:52 -04002614 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2615 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002616
Keith Packard8b9ab102008-06-13 16:03:22 -07002617 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002618 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002619 }
2620 return 0;
2621}
2622
Jose Fonsecad2443b22003-05-27 00:37:33 +00002623
2624/**
2625 * Send a device-specific write command.
2626 *
2627 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002628 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002629 * \param data source pointer of the data to be written.
2630 * \param size size of the data to be written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002631 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002632 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002633 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002634 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002635 * It issues a write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002636 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2637 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002638drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2639 void *data, unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002640{
2641 unsigned long request;
2642
Jan Vesely50d3c852016-06-30 14:22:52 -04002643 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2644 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002645
Keith Packard8b9ab102008-06-13 16:03:22 -07002646 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002647 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002648 }
2649 return 0;
2650}
2651
Jose Fonsecad2443b22003-05-27 00:37:33 +00002652
2653/**
2654 * Send a device-specific read-write command.
2655 *
2656 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002657 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002658 * \param data source pointer of the data to be read and written.
2659 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002660 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002661 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002662 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002663 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002664 * It issues a read-write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002665 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2666 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002667drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2668 void *data, unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002669{
2670 unsigned long request;
2671
Jan Vesely50d3c852016-06-30 14:22:52 -04002672 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2673 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002674
Keith Packard8b9ab102008-06-13 16:03:22 -07002675 if (drmIoctl(fd, request, data))
Jan Vesely50d3c852016-06-30 14:22:52 -04002676 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002677 return 0;
2678}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002679
Dave Airlied51e1bb2006-11-09 08:55:58 +11002680#define DRM_MAX_FDS 16
2681static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002682 char *BusID;
2683 int fd;
2684 int refcount;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002685 int type;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002686} connection[DRM_MAX_FDS];
2687
2688static int nr_fds = 0;
2689
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002690drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
Dave Airlied51e1bb2006-11-09 08:55:58 +11002691{
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002692 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2693}
2694
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002695drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2696 int type)
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002697{
Brianccd7b6e2007-05-29 14:54:00 -06002698 int i;
2699 int fd;
Jan Vesely50d3c852016-06-30 14:22:52 -04002700
Brianccd7b6e2007-05-29 14:54:00 -06002701 for (i = 0; i < nr_fds; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04002702 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2703 (connection[i].type == type)) {
2704 connection[i].refcount++;
2705 *newlyopened = 0;
2706 return connection[i].fd;
2707 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002708
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002709 fd = drmOpenWithType(NULL, BusID, type);
Emil Velikovc1cd3d92015-07-14 15:05:18 +01002710 if (fd < 0 || nr_fds == DRM_MAX_FDS)
Jan Vesely50d3c852016-06-30 14:22:52 -04002711 return fd;
2712
Brianccd7b6e2007-05-29 14:54:00 -06002713 connection[nr_fds].BusID = strdup(BusID);
2714 connection[nr_fds].fd = fd;
2715 connection[nr_fds].refcount = 1;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002716 connection[nr_fds].type = type;
Brianccd7b6e2007-05-29 14:54:00 -06002717 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002718
Brianccd7b6e2007-05-29 14:54:00 -06002719 if (0)
Jan Vesely50d3c852016-06-30 14:22:52 -04002720 fprintf(stderr, "saved connection %d for %s %d\n",
2721 nr_fds, connection[nr_fds].BusID,
2722 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002723
Brianccd7b6e2007-05-29 14:54:00 -06002724 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002725
Brianccd7b6e2007-05-29 14:54:00 -06002726 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002727}
2728
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002729drm_public void drmCloseOnce(int fd)
Dave Airlied51e1bb2006-11-09 08:55:58 +11002730{
Brianccd7b6e2007-05-29 14:54:00 -06002731 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002732
Brianccd7b6e2007-05-29 14:54:00 -06002733 for (i = 0; i < nr_fds; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002734 if (fd == connection[i].fd) {
2735 if (--connection[i].refcount == 0) {
2736 drmClose(connection[i].fd);
2737 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002738
Jan Vesely50d3c852016-06-30 14:22:52 -04002739 if (i < --nr_fds)
2740 connection[i] = connection[nr_fds];
2741
2742 return;
2743 }
2744 }
Brianccd7b6e2007-05-29 14:54:00 -06002745 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002746}
Jesse Barnes731cd552008-12-17 10:09:49 -08002747
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002748drm_public int drmSetMaster(int fd)
Jesse Barnes731cd552008-12-17 10:09:49 -08002749{
Jan Vesely50d3c852016-06-30 14:22:52 -04002750 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002751}
2752
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002753drm_public int drmDropMaster(int fd)
Jesse Barnes731cd552008-12-17 10:09:49 -08002754{
Jan Vesely50d3c852016-06-30 14:22:52 -04002755 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002756}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002757
Eric Engestromeba66092019-02-08 14:46:07 +00002758drm_public int drmIsMaster(int fd)
Christopher James Halse Rogers17dfe3a2019-01-23 15:38:45 +11002759{
2760 /* Detect master by attempting something that requires master.
2761 *
2762 * Authenticating magic tokens requires master and 0 is an
2763 * internal kernel detail which we could use. Attempting this on
2764 * a master fd would fail therefore fail with EINVAL because 0
2765 * is invalid.
2766 *
2767 * A non-master fd will fail with EACCES, as the kernel checks
2768 * for master before attempting to do anything else.
2769 *
2770 * Since we don't want to leak implementation details, use
2771 * EACCES.
2772 */
2773 return drmAuthMagic(fd, 0) != -EACCES;
2774}
2775
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002776drm_public char *drmGetDeviceNameFromFd(int fd)
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002777{
Emmanuel Vadot6818a502020-01-21 17:50:15 +01002778#ifdef __FreeBSD__
2779 struct stat sbuf;
Emmanuel Vadot6818a502020-01-21 17:50:15 +01002780 int maj, min;
Emmanuel Vadot5aa83dd2020-04-28 16:33:05 +02002781 int nodetype;
Emmanuel Vadot6818a502020-01-21 17:50:15 +01002782
2783 if (fstat(fd, &sbuf))
2784 return NULL;
2785
2786 maj = major(sbuf.st_rdev);
2787 min = minor(sbuf.st_rdev);
Emmanuel Vadot5aa83dd2020-04-28 16:33:05 +02002788 nodetype = drmGetMinorType(maj, min);
2789 return drmGetMinorNameForFD(fd, nodetype);
Emmanuel Vadot6818a502020-01-21 17:50:15 +01002790#else
Jan Vesely50d3c852016-06-30 14:22:52 -04002791 char name[128];
2792 struct stat sbuf;
2793 dev_t d;
2794 int i;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002795
Jan Vesely50d3c852016-06-30 14:22:52 -04002796 /* The whole drmOpen thing is a fiasco and we need to find a way
2797 * back to just using open(2). For now, however, lets just make
2798 * things worse with even more ad hoc directory walking code to
2799 * discover the device file name. */
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002800
Jan Vesely50d3c852016-06-30 14:22:52 -04002801 fstat(fd, &sbuf);
2802 d = sbuf.st_rdev;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002803
Jan Vesely50d3c852016-06-30 14:22:52 -04002804 for (i = 0; i < DRM_MAX_MINOR; i++) {
2805 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2806 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2807 break;
2808 }
2809 if (i == DRM_MAX_MINOR)
2810 return NULL;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002811
Jan Vesely50d3c852016-06-30 14:22:52 -04002812 return strdup(name);
Emmanuel Vadot6818a502020-01-21 17:50:15 +01002813#endif
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002814}
Dave Airliecc0a1452012-07-14 09:52:17 +00002815
Thomas Hellstromf8392582018-08-31 13:47:05 +02002816static bool drmNodeIsDRM(int maj, int min)
2817{
2818#ifdef __linux__
2819 char path[64];
2820 struct stat sbuf;
2821
2822 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2823 maj, min);
2824 return stat(path, &sbuf) == 0;
Emmanuel Vadot1c8d2b72020-01-21 17:42:44 +01002825#elif __FreeBSD__
2826 char name[SPECNAMELEN];
2827
2828 if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
2829 return 0;
2830 /* Handle drm/ and dri/ as both are present in different FreeBSD version
2831 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
2832 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
2833 * only device nodes in /dev/dri/ */
2834 return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
Thomas Hellstromf8392582018-08-31 13:47:05 +02002835#else
2836 return maj == DRM_MAJOR;
2837#endif
2838}
2839
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002840drm_public int drmGetNodeTypeFromFd(int fd)
Frank Binns1f735782015-02-13 10:51:15 +00002841{
Jan Vesely50d3c852016-06-30 14:22:52 -04002842 struct stat sbuf;
2843 int maj, min, type;
Frank Binns1f735782015-02-13 10:51:15 +00002844
Jan Vesely50d3c852016-06-30 14:22:52 -04002845 if (fstat(fd, &sbuf))
2846 return -1;
Frank Binns1f735782015-02-13 10:51:15 +00002847
Jan Vesely50d3c852016-06-30 14:22:52 -04002848 maj = major(sbuf.st_rdev);
2849 min = minor(sbuf.st_rdev);
Frank Binns1f735782015-02-13 10:51:15 +00002850
Thomas Hellstromf8392582018-08-31 13:47:05 +02002851 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002852 errno = EINVAL;
2853 return -1;
2854 }
Frank Binns1f735782015-02-13 10:51:15 +00002855
Emmanuel Vadotc55a1e52020-01-21 17:47:09 +01002856 type = drmGetMinorType(maj, min);
Jan Vesely50d3c852016-06-30 14:22:52 -04002857 if (type == -1)
2858 errno = ENODEV;
2859 return type;
Frank Binns1f735782015-02-13 10:51:15 +00002860}
2861
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002862drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2863 int *prime_fd)
Dave Airliecc0a1452012-07-14 09:52:17 +00002864{
Jan Vesely50d3c852016-06-30 14:22:52 -04002865 struct drm_prime_handle args;
2866 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002867
Jan Vesely50d3c852016-06-30 14:22:52 -04002868 memclear(args);
2869 args.fd = -1;
2870 args.handle = handle;
2871 args.flags = flags;
2872 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2873 if (ret)
2874 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002875
Jan Vesely50d3c852016-06-30 14:22:52 -04002876 *prime_fd = args.fd;
2877 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002878}
2879
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002880drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
Dave Airliecc0a1452012-07-14 09:52:17 +00002881{
Jan Vesely50d3c852016-06-30 14:22:52 -04002882 struct drm_prime_handle args;
2883 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002884
Jan Vesely50d3c852016-06-30 14:22:52 -04002885 memclear(args);
2886 args.fd = prime_fd;
2887 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2888 if (ret)
2889 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002890
Jan Vesely50d3c852016-06-30 14:22:52 -04002891 *handle = args.handle;
2892 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002893}
2894
Emil Velikov0ca03a42015-03-07 00:58:39 +00002895static char *drmGetMinorNameForFD(int fd, int type)
2896{
2897#ifdef __linux__
Jan Vesely50d3c852016-06-30 14:22:52 -04002898 DIR *sysdir;
John Stultzbb45ce42018-03-20 17:48:23 +00002899 struct dirent *ent;
Jan Vesely50d3c852016-06-30 14:22:52 -04002900 struct stat sbuf;
2901 const char *name = drmGetMinorName(type);
2902 int len;
2903 char dev_name[64], buf[64];
Jan Vesely50d3c852016-06-30 14:22:52 -04002904 int maj, min;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002905
Jan Vesely50d3c852016-06-30 14:22:52 -04002906 if (!name)
2907 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002908
Jan Vesely50d3c852016-06-30 14:22:52 -04002909 len = strlen(name);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002910
Jan Vesely50d3c852016-06-30 14:22:52 -04002911 if (fstat(fd, &sbuf))
2912 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002913
Jan Vesely50d3c852016-06-30 14:22:52 -04002914 maj = major(sbuf.st_rdev);
2915 min = minor(sbuf.st_rdev);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002916
Thomas Hellstromf8392582018-08-31 13:47:05 +02002917 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jan Vesely50d3c852016-06-30 14:22:52 -04002918 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002919
Jan Vesely50d3c852016-06-30 14:22:52 -04002920 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002921
Jan Vesely50d3c852016-06-30 14:22:52 -04002922 sysdir = opendir(buf);
2923 if (!sysdir)
2924 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002925
John Stultzbb45ce42018-03-20 17:48:23 +00002926 while ((ent = readdir(sysdir))) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002927 if (strncmp(ent->d_name, name, len) == 0) {
2928 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2929 ent->d_name);
Mathias Tillman5c42b5e2015-08-24 11:56:13 +08002930
Jan Vesely50d3c852016-06-30 14:22:52 -04002931 closedir(sysdir);
Jan Vesely50d3c852016-06-30 14:22:52 -04002932 return strdup(dev_name);
2933 }
2934 }
Kevin Strasserf34b6942018-05-18 12:48:17 -07002935
2936 closedir(sysdir);
John Stultzbb45ce42018-03-20 17:48:23 +00002937 return NULL;
Emmanuel Vadot57c50cf2020-01-21 17:53:07 +01002938#elif __FreeBSD__
2939 struct stat sbuf;
2940 char dname[SPECNAMELEN];
2941 const char *mname;
2942 char name[SPECNAMELEN];
Emmanuel Vadotf52e2b22020-04-28 15:26:13 +02002943 int id, maj, min, nodetype, i;
Emmanuel Vadot57c50cf2020-01-21 17:53:07 +01002944
2945 if (fstat(fd, &sbuf))
2946 return NULL;
2947
2948 maj = major(sbuf.st_rdev);
2949 min = minor(sbuf.st_rdev);
2950
2951 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2952 return NULL;
2953
2954 if (!devname_r(sbuf.st_rdev, S_IFCHR, dname, sizeof(dname)))
2955 return NULL;
2956
2957 /* Handle both /dev/drm and /dev/dri
2958 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
2959 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
2960 * only device nodes in /dev/dri/ */
Emmanuel Vadotf52e2b22020-04-28 15:26:13 +02002961
2962 /* Get the node type represented by fd so we can deduce the target name */
2963 nodetype = drmGetMinorType(maj, min);
2964 if (nodetype == -1)
2965 return (NULL);
Emmanuel Vadot57c50cf2020-01-21 17:53:07 +01002966 mname = drmGetMinorName(type);
Emmanuel Vadotf52e2b22020-04-28 15:26:13 +02002967
2968 for (i = 0; i < SPECNAMELEN; i++) {
2969 if (isalpha(dname[i]) == 0 && dname[i] != '/')
2970 break;
2971 }
2972 if (dname[i] == '\0')
2973 return (NULL);
2974
2975 id = (int)strtol(&dname[i], NULL, 10);
2976 id -= drmGetMinorBase(nodetype);
2977 snprintf(name, sizeof(name), DRM_DIR_NAME "/%s%d", mname,
2978 id + drmGetMinorBase(type));
2979
Emmanuel Vadot57c50cf2020-01-21 17:53:07 +01002980 return strdup(name);
Emil Velikov8415a002015-09-07 18:29:05 +01002981#else
Jonathan Grayf1890112016-12-01 15:18:39 +11002982 struct stat sbuf;
2983 char buf[PATH_MAX + 1];
Eric Engestrom331e51e2018-12-19 13:53:41 +00002984 const char *dev_name = drmGetDeviceName(type);
Jonathan Grayf1890112016-12-01 15:18:39 +11002985 unsigned int maj, min;
Jonathan Gray293b95e2019-05-13 02:52:04 +10002986 int n;
Jonathan Grayf1890112016-12-01 15:18:39 +11002987
2988 if (fstat(fd, &sbuf))
2989 return NULL;
2990
2991 maj = major(sbuf.st_rdev);
2992 min = minor(sbuf.st_rdev);
2993
Thomas Hellstromf8392582018-08-31 13:47:05 +02002994 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jonathan Grayf1890112016-12-01 15:18:39 +11002995 return NULL;
2996
Eric Engestrom331e51e2018-12-19 13:53:41 +00002997 if (!dev_name)
Jonathan Grayf1890112016-12-01 15:18:39 +11002998 return NULL;
Jonathan Grayf1890112016-12-01 15:18:39 +11002999
Jonathan Gray293b95e2019-05-13 02:52:04 +10003000 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
Jonathan Grayf1890112016-12-01 15:18:39 +11003001 if (n == -1 || n >= sizeof(buf))
3002 return NULL;
3003
3004 return strdup(buf);
Emil Velikov0ca03a42015-03-07 00:58:39 +00003005#endif
Emil Velikov0ca03a42015-03-07 00:58:39 +00003006}
3007
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003008drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
Emil Velikov0ca03a42015-03-07 00:58:39 +00003009{
Jan Vesely50d3c852016-06-30 14:22:52 -04003010 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
Emil Velikov0ca03a42015-03-07 00:58:39 +00003011}
3012
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003013drm_public char *drmGetRenderDeviceNameFromFd(int fd)
Emil Velikov0ca03a42015-03-07 00:58:39 +00003014{
Jan Vesely50d3c852016-06-30 14:22:52 -04003015 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
Emil Velikov0ca03a42015-03-07 00:58:39 +00003016}
Emil Velikovb556ea12015-08-17 11:09:06 +08003017
Thierry Redingf8484cc2016-12-22 00:39:36 +01003018#ifdef __linux__
3019static char * DRM_PRINTFLIKE(2, 3)
3020sysfs_uevent_get(const char *path, const char *fmt, ...)
3021{
3022 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
3023 size_t size = 0, len;
3024 ssize_t num;
3025 va_list ap;
3026 FILE *fp;
3027
3028 va_start(ap, fmt);
3029 num = vasprintf(&key, fmt, ap);
3030 va_end(ap);
3031 len = num;
3032
3033 snprintf(filename, sizeof(filename), "%s/uevent", path);
3034
3035 fp = fopen(filename, "r");
3036 if (!fp) {
3037 free(key);
3038 return NULL;
3039 }
3040
3041 while ((num = getline(&line, &size, fp)) >= 0) {
3042 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
3043 char *start = line + len + 1, *end = line + num - 1;
3044
3045 if (*end != '\n')
3046 end++;
3047
3048 value = strndup(start, end - start);
3049 break;
3050 }
3051 }
3052
3053 free(line);
3054 fclose(fp);
3055
3056 free(key);
3057
3058 return value;
3059}
3060#endif
3061
Emil Velikov39885802018-05-15 17:37:49 +01003062/* Little white lie to avoid major rework of the existing code */
3063#define DRM_BUS_VIRTIO 0x10
3064
Emil Velikov291b2bb2015-09-07 18:26:34 +01003065#ifdef __linux__
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003066static int get_subsystem_type(const char *device_path)
3067{
3068 char path[PATH_MAX + 1] = "";
Emil Velikovb556ea12015-08-17 11:09:06 +08003069 char link[PATH_MAX + 1] = "";
3070 char *name;
Eric Anholt9b28c5a2018-11-15 17:48:53 -08003071 struct {
3072 const char *name;
3073 int bus_type;
3074 } bus_types[] = {
3075 { "/pci", DRM_BUS_PCI },
3076 { "/usb", DRM_BUS_USB },
3077 { "/platform", DRM_BUS_PLATFORM },
Eric Anholt89700ab2018-11-15 17:52:19 -08003078 { "/spi", DRM_BUS_PLATFORM },
Eric Anholt9b28c5a2018-11-15 17:48:53 -08003079 { "/host1x", DRM_BUS_HOST1X },
3080 { "/virtio", DRM_BUS_VIRTIO },
3081 };
Emil Velikovb556ea12015-08-17 11:09:06 +08003082
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003083 strncpy(path, device_path, PATH_MAX);
3084 strncat(path, "/subsystem", PATH_MAX);
Emil Velikova250fce2015-09-07 12:54:27 +01003085
3086 if (readlink(path, link, PATH_MAX) < 0)
3087 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08003088
3089 name = strrchr(link, '/');
3090 if (!name)
3091 return -EINVAL;
3092
Eric Anholt9b28c5a2018-11-15 17:48:53 -08003093 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3094 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3095 return bus_types[i].bus_type;
3096 }
Emil Velikov39885802018-05-15 17:37:49 +01003097
Emil Velikovb556ea12015-08-17 11:09:06 +08003098 return -EINVAL;
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003099}
3100#endif
3101
3102static int drmParseSubsystemType(int maj, int min)
3103{
3104#ifdef __linux__
3105 char path[PATH_MAX + 1] = "";
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003106 char real_path[PATH_MAX + 1] = "";
3107 int subsystem_type;
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003108
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003109 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003110
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003111 subsystem_type = get_subsystem_type(path);
Mikhail Golubev57df0752020-04-20 20:08:50 +02003112 /* Try to get the parent (underlying) device type */
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003113 if (subsystem_type == DRM_BUS_VIRTIO) {
Mikhail Golubev57df0752020-04-20 20:08:50 +02003114 /* Assume virtio-pci on error */
3115 if (!realpath(path, real_path))
3116 return DRM_BUS_VIRTIO;
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003117 strncat(path, "/..", PATH_MAX);
3118 subsystem_type = get_subsystem_type(path);
Mikhail Golubev57df0752020-04-20 20:08:50 +02003119 if (subsystem_type < 0)
3120 return DRM_BUS_VIRTIO;
3121 }
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003122 return subsystem_type;
Emmanuel Vadot41f3a7b2020-01-21 17:56:49 +01003123#elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
Thierry Redinge17cad12016-12-21 18:00:52 +01003124 return DRM_BUS_PCI;
Emil Velikov291b2bb2015-09-07 18:26:34 +01003125#else
3126#warning "Missing implementation of drmParseSubsystemType"
3127 return -EINVAL;
3128#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08003129}
3130
Emmanuel Vadot44bcf9c2020-04-20 22:44:02 +02003131#ifdef __linux__
Eric Engestrom56499532018-09-05 13:23:59 +01003132static void
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003133get_pci_path(int maj, int min, char *pci_path)
Emil Velikova0290012018-05-15 17:29:44 +01003134{
Emil Velikov39885802018-05-15 17:37:49 +01003135 char path[PATH_MAX + 1], *term;
Emil Velikova0290012018-05-15 17:29:44 +01003136
3137 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003138 if (!realpath(path, pci_path)) {
3139 strcpy(pci_path, path);
Eric Engestrom56499532018-09-05 13:23:59 +01003140 return;
3141 }
Emil Velikova0290012018-05-15 17:29:44 +01003142
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003143 term = strrchr(pci_path, '/');
Emil Velikov39885802018-05-15 17:37:49 +01003144 if (term && strncmp(term, "/virtio", 7) == 0)
3145 *term = 0;
Emil Velikovbcb9d972018-08-23 10:49:54 +01003146}
Emmanuel Vadot44bcf9c2020-04-20 22:44:02 +02003147#endif
Emil Velikovbcb9d972018-08-23 10:49:54 +01003148
Emmanuel Vadot37911372020-01-21 18:49:11 +01003149#ifdef __FreeBSD__
3150static int get_sysctl_pci_bus_info(int maj, int min, drmPciBusInfoPtr info)
3151{
3152 char dname[SPECNAMELEN];
3153 char sysctl_name[16];
3154 char sysctl_val[256];
3155 size_t sysctl_len;
3156 int id, type, nelem;
3157 unsigned int rdev, majmin, domain, bus, dev, func;
3158
3159 rdev = makedev(maj, min);
3160 if (!devname_r(rdev, S_IFCHR, dname, sizeof(dname)))
3161 return -EINVAL;
3162
3163 if (sscanf(dname, "drm/%d\n", &id) != 1)
3164 return -EINVAL;
3165 type = drmGetMinorType(maj, min);
3166 if (type == -1)
3167 return -EINVAL;
3168
3169 /* BUG: This above section is iffy, since it mandates that a driver will
3170 * create both card and render node.
3171 * If it does not, the next DRM device will create card#X and
3172 * renderD#(128+X)-1.
3173 * This is a possibility in FreeBSD but for now there is no good way for
3174 * obtaining the info.
3175 */
3176 switch (type) {
3177 case DRM_NODE_PRIMARY:
3178 break;
3179 case DRM_NODE_CONTROL:
3180 id -= 64;
3181 break;
3182 case DRM_NODE_RENDER:
3183 id -= 128;
3184 break;
3185 }
3186 if (id < 0)
3187 return -EINVAL;
3188
3189 if (snprintf(sysctl_name, sizeof(sysctl_name), "hw.dri.%d.busid", id) <= 0)
3190 return -EINVAL;
3191 sysctl_len = sizeof(sysctl_val);
3192 if (sysctlbyname(sysctl_name, sysctl_val, &sysctl_len, NULL, 0))
3193 return -EINVAL;
3194
3195 #define bus_fmt "pci:%04x:%02x:%02x.%u"
3196
3197 nelem = sscanf(sysctl_val, bus_fmt, &domain, &bus, &dev, &func);
3198 if (nelem != 4)
3199 return -EINVAL;
3200 info->domain = domain;
3201 info->bus = bus;
3202 info->dev = dev;
3203 info->func = func;
3204
3205 return 0;
3206}
3207#endif
3208
Emil Velikov536e0de2015-09-07 12:37:57 +01003209static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
Emil Velikovb556ea12015-08-17 11:09:06 +08003210{
Emil Velikov291b2bb2015-09-07 18:26:34 +01003211#ifdef __linux__
Thierry Reding5403cb32017-01-18 08:29:23 +01003212 unsigned int domain, bus, dev, func;
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003213 char pci_path[PATH_MAX + 1], *value;
Thierry Reding5403cb32017-01-18 08:29:23 +01003214 int num;
Emil Velikovb556ea12015-08-17 11:09:06 +08003215
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003216 get_pci_path(maj, min, pci_path);
Emil Velikov536e0de2015-09-07 12:37:57 +01003217
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003218 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
Thierry Reding5403cb32017-01-18 08:29:23 +01003219 if (!value)
3220 return -ENOENT;
Emil Velikov536e0de2015-09-07 12:37:57 +01003221
Thierry Reding5403cb32017-01-18 08:29:23 +01003222 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3223 free(value);
3224
3225 if (num != 4)
Emil Velikovb556ea12015-08-17 11:09:06 +08003226 return -EINVAL;
3227
Emil Velikovb556ea12015-08-17 11:09:06 +08003228 info->domain = domain;
3229 info->bus = bus;
3230 info->dev = dev;
3231 info->func = func;
3232
3233 return 0;
François Tigeot8f2e0922018-12-12 20:48:36 +01003234#elif defined(__OpenBSD__) || defined(__DragonFly__)
Jonathan Grayfd190562016-12-01 15:18:42 +11003235 struct drm_pciinfo pinfo;
3236 int fd, type;
3237
Emmanuel Vadotc55a1e52020-01-21 17:47:09 +01003238 type = drmGetMinorType(maj, min);
Jonathan Grayfd190562016-12-01 15:18:42 +11003239 if (type == -1)
3240 return -ENODEV;
3241
3242 fd = drmOpenMinor(min, 0, type);
3243 if (fd < 0)
3244 return -errno;
3245
3246 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3247 close(fd);
3248 return -errno;
3249 }
3250 close(fd);
3251
3252 info->domain = pinfo.domain;
3253 info->bus = pinfo.bus;
3254 info->dev = pinfo.dev;
3255 info->func = pinfo.func;
3256
3257 return 0;
Emmanuel Vadot37911372020-01-21 18:49:11 +01003258#elif __FreeBSD__
3259 return get_sysctl_pci_bus_info(maj, min, info);
Emil Velikov291b2bb2015-09-07 18:26:34 +01003260#else
3261#warning "Missing implementation of drmParsePciBusInfo"
3262 return -EINVAL;
3263#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08003264}
3265
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003266drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
Emil Velikovb556ea12015-08-17 11:09:06 +08003267{
Emil Velikovbc2aca92015-09-07 13:51:54 +01003268 if (a == NULL || b == NULL)
Adam Jackson7c27cd72017-05-04 10:48:56 -04003269 return 0;
Emil Velikovbc2aca92015-09-07 13:51:54 +01003270
Emil Velikovb556ea12015-08-17 11:09:06 +08003271 if (a->bustype != b->bustype)
Adam Jackson7c27cd72017-05-04 10:48:56 -04003272 return 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003273
3274 switch (a->bustype) {
3275 case DRM_BUS_PCI:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003276 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
Thierry Redingf8484cc2016-12-22 00:39:36 +01003277
3278 case DRM_BUS_USB:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003279 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
Thierry Redingf8484cc2016-12-22 00:39:36 +01003280
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003281 case DRM_BUS_PLATFORM:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003282 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003283
3284 case DRM_BUS_HOST1X:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003285 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003286
Emil Velikovb556ea12015-08-17 11:09:06 +08003287 default:
3288 break;
3289 }
3290
Adam Jackson7c27cd72017-05-04 10:48:56 -04003291 return 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003292}
3293
3294static int drmGetNodeType(const char *name)
3295{
Emil Velikovb556ea12015-08-17 11:09:06 +08003296 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3297 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3298 return DRM_NODE_CONTROL;
3299
3300 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3301 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3302 return DRM_NODE_RENDER;
3303
Jonathan Gray13e2c352019-05-13 02:50:49 +10003304 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3305 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3306 return DRM_NODE_PRIMARY;
3307
Emil Velikovb556ea12015-08-17 11:09:06 +08003308 return -EINVAL;
3309}
3310
Emil Velikov5f68d312015-09-07 13:54:32 +01003311static int drmGetMaxNodeName(void)
3312{
3313 return sizeof(DRM_DIR_NAME) +
3314 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3315 sizeof(DRM_CONTROL_MINOR_NAME),
3316 sizeof(DRM_RENDER_MINOR_NAME)) +
Eric Engestromce975072016-04-03 19:48:12 +01003317 3 /* length of the node number */;
Emil Velikov5f68d312015-09-07 13:54:32 +01003318}
3319
Emil Velikov291b2bb2015-09-07 18:26:34 +01003320#ifdef __linux__
Emil Velikovaae3f312016-11-01 18:04:06 +00003321static int parse_separate_sysfs_files(int maj, int min,
Emil Velikov11687bf2016-11-30 17:24:21 +00003322 drmPciDeviceInfoPtr device,
3323 bool ignore_revision)
Emil Velikovaae3f312016-11-01 18:04:06 +00003324{
Emil Velikovaae3f312016-11-01 18:04:06 +00003325 static const char *attrs[] = {
3326 "revision", /* Older kernels are missing the file, so check for it first */
3327 "vendor",
3328 "device",
3329 "subsystem_vendor",
3330 "subsystem_device",
3331 };
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003332 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
Emil Velikovaae3f312016-11-01 18:04:06 +00003333 unsigned int data[ARRAY_SIZE(attrs)];
3334 FILE *fp;
3335 int ret;
3336
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003337 get_pci_path(maj, min, pci_path);
Emil Velikova0290012018-05-15 17:29:44 +01003338
Emil Velikov11687bf2016-11-30 17:24:21 +00003339 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003340 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
Emil Velikovaae3f312016-11-01 18:04:06 +00003341 fp = fopen(path, "r");
3342 if (!fp)
3343 return -errno;
3344
3345 ret = fscanf(fp, "%x", &data[i]);
3346 fclose(fp);
3347 if (ret != 1)
3348 return -errno;
3349
3350 }
3351
Emil Velikov11687bf2016-11-30 17:24:21 +00003352 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
Emil Velikovaae3f312016-11-01 18:04:06 +00003353 device->vendor_id = data[1] & 0xffff;
3354 device->device_id = data[2] & 0xffff;
3355 device->subvendor_id = data[3] & 0xffff;
3356 device->subdevice_id = data[4] & 0xffff;
3357
3358 return 0;
3359}
3360
3361static int parse_config_sysfs_file(int maj, int min,
3362 drmPciDeviceInfoPtr device)
3363{
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003364 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
Emil Velikovef5192e2015-09-07 12:47:47 +01003365 unsigned char config[64];
3366 int fd, ret;
3367
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003368 get_pci_path(maj, min, pci_path);
Emil Velikova0290012018-05-15 17:29:44 +01003369
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003370 snprintf(path, PATH_MAX, "%s/config", pci_path);
Emil Velikovef5192e2015-09-07 12:47:47 +01003371 fd = open(path, O_RDONLY);
3372 if (fd < 0)
3373 return -errno;
3374
3375 ret = read(fd, config, sizeof(config));
3376 close(fd);
3377 if (ret < 0)
3378 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08003379
3380 device->vendor_id = config[0] | (config[1] << 8);
3381 device->device_id = config[2] | (config[3] << 8);
3382 device->revision_id = config[8];
3383 device->subvendor_id = config[44] | (config[45] << 8);
3384 device->subdevice_id = config[46] | (config[47] << 8);
3385
3386 return 0;
Emil Velikovaae3f312016-11-01 18:04:06 +00003387}
3388#endif
3389
3390static int drmParsePciDeviceInfo(int maj, int min,
3391 drmPciDeviceInfoPtr device,
3392 uint32_t flags)
3393{
3394#ifdef __linux__
Emil Velikov11687bf2016-11-30 17:24:21 +00003395 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3396 return parse_separate_sysfs_files(maj, min, device, true);
3397
3398 if (parse_separate_sysfs_files(maj, min, device, false))
Emil Velikovaae3f312016-11-01 18:04:06 +00003399 return parse_config_sysfs_file(maj, min, device);
3400
3401 return 0;
François Tigeot8f2e0922018-12-12 20:48:36 +01003402#elif defined(__OpenBSD__) || defined(__DragonFly__)
Jonathan Grayc0ef1d02016-12-01 15:18:41 +11003403 struct drm_pciinfo pinfo;
3404 int fd, type;
3405
Emmanuel Vadotc55a1e52020-01-21 17:47:09 +01003406 type = drmGetMinorType(maj, min);
Jonathan Grayc0ef1d02016-12-01 15:18:41 +11003407 if (type == -1)
3408 return -ENODEV;
3409
3410 fd = drmOpenMinor(min, 0, type);
3411 if (fd < 0)
3412 return -errno;
3413
3414 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3415 close(fd);
3416 return -errno;
3417 }
3418 close(fd);
3419
3420 device->vendor_id = pinfo.vendor_id;
3421 device->device_id = pinfo.device_id;
3422 device->revision_id = pinfo.revision_id;
3423 device->subvendor_id = pinfo.subvendor_id;
3424 device->subdevice_id = pinfo.subdevice_id;
3425
3426 return 0;
Emmanuel Vadote321dd72020-01-21 18:49:47 +01003427#elif __FreeBSD__
3428 drmPciBusInfo info;
3429 struct pci_conf_io pc;
3430 struct pci_match_conf patterns[1];
3431 struct pci_conf results[1];
3432 int fd, error;
3433
3434 if (get_sysctl_pci_bus_info(maj, min, &info) != 0)
3435 return -EINVAL;
3436
3437 fd = open("/dev/pci", O_RDONLY, 0);
3438 if (fd < 0)
3439 return -errno;
3440
3441 bzero(&patterns, sizeof(patterns));
3442 patterns[0].pc_sel.pc_domain = info.domain;
3443 patterns[0].pc_sel.pc_bus = info.bus;
3444 patterns[0].pc_sel.pc_dev = info.dev;
3445 patterns[0].pc_sel.pc_func = info.func;
3446 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS
3447 | PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC;
3448 bzero(&pc, sizeof(struct pci_conf_io));
3449 pc.num_patterns = 1;
3450 pc.pat_buf_len = sizeof(patterns);
3451 pc.patterns = patterns;
3452 pc.match_buf_len = sizeof(results);
3453 pc.matches = results;
3454
3455 if (ioctl(fd, PCIOCGETCONF, &pc) || pc.status == PCI_GETCONF_ERROR) {
3456 error = errno;
3457 close(fd);
3458 return -error;
3459 }
3460 close(fd);
3461
3462 device->vendor_id = results[0].pc_vendor;
3463 device->device_id = results[0].pc_device;
3464 device->subvendor_id = results[0].pc_subvendor;
3465 device->subdevice_id = results[0].pc_subdevice;
3466 device->revision_id = results[0].pc_revid;
3467
3468 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01003469#else
3470#warning "Missing implementation of drmParsePciDeviceInfo"
3471 return -EINVAL;
3472#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08003473}
3474
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003475static void drmFreePlatformDevice(drmDevicePtr device)
3476{
3477 if (device->deviceinfo.platform) {
3478 if (device->deviceinfo.platform->compatible) {
3479 char **compatible = device->deviceinfo.platform->compatible;
3480
3481 while (*compatible) {
3482 free(*compatible);
3483 compatible++;
3484 }
3485
3486 free(device->deviceinfo.platform->compatible);
3487 }
3488 }
3489}
3490
3491static void drmFreeHost1xDevice(drmDevicePtr device)
3492{
3493 if (device->deviceinfo.host1x) {
3494 if (device->deviceinfo.host1x->compatible) {
3495 char **compatible = device->deviceinfo.host1x->compatible;
3496
3497 while (*compatible) {
3498 free(*compatible);
3499 compatible++;
3500 }
3501
3502 free(device->deviceinfo.host1x->compatible);
3503 }
3504 }
3505}
3506
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003507drm_public void drmFreeDevice(drmDevicePtr *device)
Emil Velikovb556ea12015-08-17 11:09:06 +08003508{
Emil Velikovb556ea12015-08-17 11:09:06 +08003509 if (device == NULL)
3510 return;
3511
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003512 if (*device) {
3513 switch ((*device)->bustype) {
3514 case DRM_BUS_PLATFORM:
3515 drmFreePlatformDevice(*device);
3516 break;
3517
3518 case DRM_BUS_HOST1X:
3519 drmFreeHost1xDevice(*device);
3520 break;
3521 }
3522 }
3523
Emil Velikov5f68d312015-09-07 13:54:32 +01003524 free(*device);
3525 *device = NULL;
Emil Velikovb556ea12015-08-17 11:09:06 +08003526}
3527
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003528drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
Emil Velikovb556ea12015-08-17 11:09:06 +08003529{
3530 int i;
3531
3532 if (devices == NULL)
3533 return;
3534
Qiang Yu6c056ee2016-07-14 17:10:56 +08003535 for (i = 0; i < count; i++)
3536 if (devices[i])
3537 drmFreeDevice(&devices[i]);
Emil Velikovb556ea12015-08-17 11:09:06 +08003538}
3539
Thierry Reding2e57bba2016-12-21 17:54:48 +01003540static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3541 size_t bus_size, size_t device_size,
3542 char **ptrp)
3543{
3544 size_t max_node_length, extra, size;
3545 drmDevicePtr device;
3546 unsigned int i;
3547 char *ptr;
3548
3549 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3550 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3551
3552 size = sizeof(*device) + extra + bus_size + device_size;
3553
3554 device = calloc(1, size);
3555 if (!device)
3556 return NULL;
3557
3558 device->available_nodes = 1 << type;
3559
3560 ptr = (char *)device + sizeof(*device);
3561 device->nodes = (char **)ptr;
3562
3563 ptr += DRM_NODE_MAX * sizeof(void *);
3564
3565 for (i = 0; i < DRM_NODE_MAX; i++) {
3566 device->nodes[i] = ptr;
3567 ptr += max_node_length;
3568 }
3569
3570 memcpy(device->nodes[type], node, max_node_length);
3571
3572 *ptrp = ptr;
3573
3574 return device;
3575}
3576
Emil Velikovb40a65d2016-11-30 16:41:27 +00003577static int drmProcessPciDevice(drmDevicePtr *device,
Emil Velikovfae59d72015-09-09 17:54:34 +01003578 const char *node, int node_type,
Emil Velikov138d2312016-11-30 17:13:51 +00003579 int maj, int min, bool fetch_deviceinfo,
3580 uint32_t flags)
Emil Velikovfae59d72015-09-09 17:54:34 +01003581{
Thierry Reding2e57bba2016-12-21 17:54:48 +01003582 drmDevicePtr dev;
Michel Dänzer30455232015-10-14 12:48:52 +09003583 char *addr;
Thierry Reding2e57bba2016-12-21 17:54:48 +01003584 int ret;
Emil Velikovfae59d72015-09-09 17:54:34 +01003585
Thierry Reding2e57bba2016-12-21 17:54:48 +01003586 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3587 sizeof(drmPciDeviceInfo), &addr);
3588 if (!dev)
Emil Velikovfae59d72015-09-09 17:54:34 +01003589 return -ENOMEM;
3590
Thierry Reding2e57bba2016-12-21 17:54:48 +01003591 dev->bustype = DRM_BUS_PCI;
Jan Vesely50d3c852016-06-30 14:22:52 -04003592
Thierry Reding2e57bba2016-12-21 17:54:48 +01003593 dev->businfo.pci = (drmPciBusInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003594
Thierry Reding2e57bba2016-12-21 17:54:48 +01003595 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
Emil Velikovfae59d72015-09-09 17:54:34 +01003596 if (ret)
3597 goto free_device;
3598
3599 // Fetch the device info if the user has requested it
3600 if (fetch_deviceinfo) {
3601 addr += sizeof(drmPciBusInfo);
Thierry Reding2e57bba2016-12-21 17:54:48 +01003602 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003603
Thierry Reding2e57bba2016-12-21 17:54:48 +01003604 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
Emil Velikovfae59d72015-09-09 17:54:34 +01003605 if (ret)
3606 goto free_device;
3607 }
Thierry Reding2e57bba2016-12-21 17:54:48 +01003608
3609 *device = dev;
3610
Emil Velikovfae59d72015-09-09 17:54:34 +01003611 return 0;
3612
3613free_device:
Thierry Reding2e57bba2016-12-21 17:54:48 +01003614 free(dev);
Emil Velikovfae59d72015-09-09 17:54:34 +01003615 return ret;
3616}
3617
Scott Andersonbf63f8a2020-01-31 16:18:19 +13003618#ifdef __linux__
3619static int drm_usb_dev_path(int maj, int min, char *path, size_t len)
3620{
3621 char *value, *tmp_path, *slash;
3622
3623 snprintf(path, len, "/sys/dev/char/%d:%d/device", maj, min);
3624
3625 value = sysfs_uevent_get(path, "DEVTYPE");
3626 if (!value)
3627 return -ENOENT;
3628
3629 if (strcmp(value, "usb_device") == 0)
3630 return 0;
3631 if (strcmp(value, "usb_interface") != 0)
3632 return -ENOTSUP;
3633
3634 /* The parent of a usb_interface is a usb_device */
3635
3636 tmp_path = realpath(path, NULL);
3637 if (!tmp_path)
3638 return -errno;
3639
3640 slash = strrchr(tmp_path, '/');
3641 if (!slash) {
3642 free(tmp_path);
3643 return -EINVAL;
3644 }
3645
3646 *slash = '\0';
3647
3648 if (snprintf(path, len, "%s", tmp_path) >= (int)len) {
3649 free(tmp_path);
3650 return -EINVAL;
3651 }
3652
3653 free(tmp_path);
3654 return 0;
3655}
3656#endif
3657
Thierry Redingf8484cc2016-12-22 00:39:36 +01003658static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3659{
3660#ifdef __linux__
3661 char path[PATH_MAX + 1], *value;
3662 unsigned int bus, dev;
3663 int ret;
3664
Scott Andersonbf63f8a2020-01-31 16:18:19 +13003665 ret = drm_usb_dev_path(maj, min, path, sizeof(path));
3666 if (ret < 0)
3667 return ret;
Thierry Redingf8484cc2016-12-22 00:39:36 +01003668
3669 value = sysfs_uevent_get(path, "BUSNUM");
3670 if (!value)
3671 return -ENOENT;
3672
3673 ret = sscanf(value, "%03u", &bus);
3674 free(value);
3675
3676 if (ret <= 0)
3677 return -errno;
3678
3679 value = sysfs_uevent_get(path, "DEVNUM");
3680 if (!value)
3681 return -ENOENT;
3682
3683 ret = sscanf(value, "%03u", &dev);
3684 free(value);
3685
3686 if (ret <= 0)
3687 return -errno;
3688
3689 info->bus = bus;
3690 info->dev = dev;
3691
3692 return 0;
3693#else
3694#warning "Missing implementation of drmParseUsbBusInfo"
3695 return -EINVAL;
3696#endif
3697}
3698
3699static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3700{
3701#ifdef __linux__
3702 char path[PATH_MAX + 1], *value;
3703 unsigned int vendor, product;
3704 int ret;
3705
Scott Andersonbf63f8a2020-01-31 16:18:19 +13003706 ret = drm_usb_dev_path(maj, min, path, sizeof(path));
3707 if (ret < 0)
3708 return ret;
Thierry Redingf8484cc2016-12-22 00:39:36 +01003709
3710 value = sysfs_uevent_get(path, "PRODUCT");
3711 if (!value)
3712 return -ENOENT;
3713
3714 ret = sscanf(value, "%x/%x", &vendor, &product);
3715 free(value);
3716
3717 if (ret <= 0)
3718 return -errno;
3719
3720 info->vendor = vendor;
3721 info->product = product;
3722
3723 return 0;
3724#else
3725#warning "Missing implementation of drmParseUsbDeviceInfo"
3726 return -EINVAL;
3727#endif
3728}
3729
3730static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3731 int node_type, int maj, int min,
3732 bool fetch_deviceinfo, uint32_t flags)
3733{
3734 drmDevicePtr dev;
3735 char *ptr;
3736 int ret;
3737
3738 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3739 sizeof(drmUsbDeviceInfo), &ptr);
3740 if (!dev)
3741 return -ENOMEM;
3742
3743 dev->bustype = DRM_BUS_USB;
3744
3745 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3746
3747 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3748 if (ret < 0)
3749 goto free_device;
3750
3751 if (fetch_deviceinfo) {
3752 ptr += sizeof(drmUsbBusInfo);
3753 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3754
3755 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3756 if (ret < 0)
3757 goto free_device;
3758 }
3759
3760 *device = dev;
3761
3762 return 0;
3763
3764free_device:
3765 free(dev);
3766 return ret;
3767}
3768
Emil Velikovee798b92019-01-23 10:39:12 +00003769static int drmParseOFBusInfo(int maj, int min, char *fullname)
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003770{
3771#ifdef __linux__
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003772 char path[PATH_MAX + 1], *name, *tmp_name;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003773
3774 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3775
3776 name = sysfs_uevent_get(path, "OF_FULLNAME");
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003777 tmp_name = name;
3778 if (!name) {
3779 /* If the device lacks OF data, pick the MODALIAS info */
3780 name = sysfs_uevent_get(path, "MODALIAS");
3781 if (!name)
3782 return -ENOENT;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003783
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003784 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3785 tmp_name = strrchr(name, ':');
3786 if (!tmp_name) {
3787 free(name);
3788 return -ENOENT;
3789 }
3790 tmp_name++;
3791 }
3792
Emil Velikovee798b92019-01-23 10:39:12 +00003793 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3794 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003795 free(name);
3796
3797 return 0;
3798#else
Emil Velikovee798b92019-01-23 10:39:12 +00003799#warning "Missing implementation of drmParseOFBusInfo"
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003800 return -EINVAL;
3801#endif
3802}
3803
Emil Velikovee798b92019-01-23 10:39:12 +00003804static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003805{
3806#ifdef __linux__
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003807 char path[PATH_MAX + 1], *value, *tmp_name;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003808 unsigned int count, i;
3809 int err;
3810
3811 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3812
3813 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003814 if (value) {
3815 sscanf(value, "%u", &count);
3816 free(value);
3817 } else {
3818 /* Assume one entry if the device lack OF data */
3819 count = 1;
3820 }
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003821
Emil Velikovee798b92019-01-23 10:39:12 +00003822 *compatible = calloc(count + 1, sizeof(char *));
3823 if (!*compatible)
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003824 return -ENOMEM;
3825
3826 for (i = 0; i < count; i++) {
3827 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003828 tmp_name = value;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003829 if (!value) {
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003830 /* If the device lacks OF data, pick the MODALIAS info */
3831 value = sysfs_uevent_get(path, "MODALIAS");
3832 if (!value) {
3833 err = -ENOENT;
3834 goto free;
3835 }
3836
3837 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3838 tmp_name = strrchr(value, ':');
3839 if (!tmp_name) {
3840 free(value);
3841 return -ENOENT;
3842 }
3843 tmp_name = strdup(tmp_name + 1);
3844 free(value);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003845 }
3846
Andreas Baierl4735ca72019-03-11 16:04:08 +01003847 (*compatible)[i] = tmp_name;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003848 }
3849
3850 return 0;
3851
3852free:
3853 while (i--)
Andreas Baierl4735ca72019-03-11 16:04:08 +01003854 free((*compatible)[i]);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003855
Emil Velikovee798b92019-01-23 10:39:12 +00003856 free(*compatible);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003857 return err;
3858#else
Emil Velikovee798b92019-01-23 10:39:12 +00003859#warning "Missing implementation of drmParseOFDeviceInfo"
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003860 return -EINVAL;
3861#endif
3862}
3863
3864static int drmProcessPlatformDevice(drmDevicePtr *device,
3865 const char *node, int node_type,
3866 int maj, int min, bool fetch_deviceinfo,
3867 uint32_t flags)
3868{
3869 drmDevicePtr dev;
3870 char *ptr;
3871 int ret;
3872
3873 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3874 sizeof(drmPlatformDeviceInfo), &ptr);
3875 if (!dev)
3876 return -ENOMEM;
3877
3878 dev->bustype = DRM_BUS_PLATFORM;
3879
3880 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3881
Emil Velikovee798b92019-01-23 10:39:12 +00003882 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003883 if (ret < 0)
3884 goto free_device;
3885
3886 if (fetch_deviceinfo) {
3887 ptr += sizeof(drmPlatformBusInfo);
3888 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3889
Emil Velikovee798b92019-01-23 10:39:12 +00003890 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003891 if (ret < 0)
3892 goto free_device;
3893 }
3894
3895 *device = dev;
3896
3897 return 0;
3898
3899free_device:
3900 free(dev);
3901 return ret;
3902}
3903
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003904static int drmProcessHost1xDevice(drmDevicePtr *device,
3905 const char *node, int node_type,
3906 int maj, int min, bool fetch_deviceinfo,
3907 uint32_t flags)
3908{
3909 drmDevicePtr dev;
3910 char *ptr;
3911 int ret;
3912
3913 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3914 sizeof(drmHost1xDeviceInfo), &ptr);
3915 if (!dev)
3916 return -ENOMEM;
3917
3918 dev->bustype = DRM_BUS_HOST1X;
3919
3920 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3921
Emil Velikovee798b92019-01-23 10:39:12 +00003922 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003923 if (ret < 0)
3924 goto free_device;
3925
3926 if (fetch_deviceinfo) {
3927 ptr += sizeof(drmHost1xBusInfo);
3928 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3929
Emil Velikovee798b92019-01-23 10:39:12 +00003930 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003931 if (ret < 0)
3932 goto free_device;
3933 }
3934
3935 *device = dev;
3936
3937 return 0;
3938
3939free_device:
3940 free(dev);
3941 return ret;
3942}
3943
Emil Velikovf808fee2018-05-15 15:02:52 +01003944static int
3945process_device(drmDevicePtr *device, const char *d_name,
3946 int req_subsystem_type,
3947 bool fetch_deviceinfo, uint32_t flags)
3948{
3949 struct stat sbuf;
3950 char node[PATH_MAX + 1];
3951 int node_type, subsystem_type;
3952 unsigned int maj, min;
3953
3954 node_type = drmGetNodeType(d_name);
3955 if (node_type < 0)
3956 return -1;
3957
3958 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3959 if (stat(node, &sbuf))
3960 return -1;
3961
3962 maj = major(sbuf.st_rdev);
3963 min = minor(sbuf.st_rdev);
3964
Thomas Hellstromf8392582018-08-31 13:47:05 +02003965 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Emil Velikovf808fee2018-05-15 15:02:52 +01003966 return -1;
3967
3968 subsystem_type = drmParseSubsystemType(maj, min);
3969 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3970 return -1;
3971
3972 switch (subsystem_type) {
3973 case DRM_BUS_PCI:
Mikhail Golubev57df0752020-04-20 20:08:50 +02003974 case DRM_BUS_VIRTIO:
Emil Velikovf808fee2018-05-15 15:02:52 +01003975 return drmProcessPciDevice(device, node, node_type, maj, min,
3976 fetch_deviceinfo, flags);
3977 case DRM_BUS_USB:
3978 return drmProcessUsbDevice(device, node, node_type, maj, min,
3979 fetch_deviceinfo, flags);
3980 case DRM_BUS_PLATFORM:
3981 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3982 fetch_deviceinfo, flags);
3983 case DRM_BUS_HOST1X:
3984 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3985 fetch_deviceinfo, flags);
3986 default:
3987 return -1;
3988 }
3989}
3990
Qiang Yu3c208932016-07-14 17:10:55 +08003991/* Consider devices located on the same bus as duplicate and fold the respective
3992 * entries into a single one.
3993 *
3994 * Note: this leaves "gaps" in the array, while preserving the length.
3995 */
Emil Velikovfae59d72015-09-09 17:54:34 +01003996static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3997{
3998 int node_type, i, j;
3999
4000 for (i = 0; i < count; i++) {
4001 for (j = i + 1; j < count; j++) {
Adam Jacksona2fa2e02017-05-04 15:57:14 -04004002 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
Emil Velikovfae59d72015-09-09 17:54:34 +01004003 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
4004 node_type = log2(local_devices[j]->available_nodes);
4005 memcpy(local_devices[i]->nodes[node_type],
4006 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
4007 drmFreeDevice(&local_devices[j]);
4008 }
4009 }
4010 }
4011}
4012
Emil Velikov11687bf2016-11-30 17:24:21 +00004013/* Check that the given flags are valid returning 0 on success */
4014static int
4015drm_device_validate_flags(uint32_t flags)
4016{
4017 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
4018}
4019
Emil Velikov56e72d32018-06-21 16:06:35 +01004020static bool
4021drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
4022{
4023 struct stat sbuf;
4024
4025 for (int i = 0; i < DRM_NODE_MAX; i++) {
4026 if (device->available_nodes & 1 << i) {
4027 if (stat(device->nodes[i], &sbuf) == 0 &&
4028 sbuf.st_rdev == find_rdev)
4029 return true;
4030 }
4031 }
4032 return false;
4033}
4034
Emil Velikov95b262f2018-05-15 16:32:10 +01004035/*
4036 * The kernel drm core has a number of places that assume maximum of
4037 * 3x64 devices nodes. That's 64 for each of primary, control and
4038 * render nodes. Rounded it up to 256 for simplicity.
4039 */
4040#define MAX_DRM_NODES 256
4041
Emil Velikovb556ea12015-08-17 11:09:06 +08004042/**
Emil Velikovccedf662015-09-09 16:02:18 +01004043 * Get information about the opened drm device
4044 *
4045 * \param fd file descriptor of the drm device
Emil Velikov11687bf2016-11-30 17:24:21 +00004046 * \param flags feature/behaviour bitmask
Emil Velikovccedf662015-09-09 16:02:18 +01004047 * \param device the address of a drmDevicePtr where the information
4048 * will be allocated in stored
4049 *
4050 * \return zero on success, negative error code otherwise.
Emil Velikov11687bf2016-11-30 17:24:21 +00004051 *
4052 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
4053 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
Emil Velikovccedf662015-09-09 16:02:18 +01004054 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004055drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
Emil Velikovccedf662015-09-09 16:02:18 +01004056{
Jonathan Gray08257922016-12-01 15:18:43 +11004057#ifdef __OpenBSD__
4058 /*
4059 * DRI device nodes on OpenBSD are not in their own directory, they reside
4060 * in /dev along with a large number of statically generated /dev nodes.
4061 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
4062 */
4063 drmDevicePtr d;
4064 struct stat sbuf;
4065 char node[PATH_MAX + 1];
4066 const char *dev_name;
4067 int node_type, subsystem_type;
Jonathan Gray293b95e2019-05-13 02:52:04 +10004068 int maj, min, n, ret;
Jonathan Gray08257922016-12-01 15:18:43 +11004069
4070 if (fd == -1 || device == NULL)
4071 return -EINVAL;
4072
4073 if (fstat(fd, &sbuf))
4074 return -errno;
4075
4076 maj = major(sbuf.st_rdev);
4077 min = minor(sbuf.st_rdev);
4078
Thomas Hellstromf8392582018-08-31 13:47:05 +02004079 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jonathan Gray08257922016-12-01 15:18:43 +11004080 return -EINVAL;
4081
Emmanuel Vadotc55a1e52020-01-21 17:47:09 +01004082 node_type = drmGetMinorType(maj, min);
Jonathan Gray08257922016-12-01 15:18:43 +11004083 if (node_type == -1)
4084 return -ENODEV;
4085
Eric Engestrom331e51e2018-12-19 13:53:41 +00004086 dev_name = drmGetDeviceName(node_type);
4087 if (!dev_name)
Jonathan Gray08257922016-12-01 15:18:43 +11004088 return -EINVAL;
Jonathan Gray08257922016-12-01 15:18:43 +11004089
Jonathan Gray293b95e2019-05-13 02:52:04 +10004090 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
Jonathan Gray08257922016-12-01 15:18:43 +11004091 if (n == -1 || n >= PATH_MAX)
4092 return -errno;
4093 if (stat(node, &sbuf))
4094 return -EINVAL;
4095
4096 subsystem_type = drmParseSubsystemType(maj, min);
4097 if (subsystem_type != DRM_BUS_PCI)
4098 return -ENODEV;
4099
4100 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
4101 if (ret)
4102 return ret;
4103
4104 *device = d;
4105
4106 return 0;
4107#else
Emil Velikov95b262f2018-05-15 16:32:10 +01004108 drmDevicePtr local_devices[MAX_DRM_NODES];
Emil Velikovccedf662015-09-09 16:02:18 +01004109 drmDevicePtr d;
4110 DIR *sysdir;
4111 struct dirent *dent;
4112 struct stat sbuf;
Emil Velikovf808fee2018-05-15 15:02:52 +01004113 int subsystem_type;
Emil Velikovccedf662015-09-09 16:02:18 +01004114 int maj, min;
4115 int ret, i, node_count;
Qiang Yu3c208932016-07-14 17:10:55 +08004116 dev_t find_rdev;
Emil Velikov11687bf2016-11-30 17:24:21 +00004117
4118 if (drm_device_validate_flags(flags))
4119 return -EINVAL;
Emil Velikovccedf662015-09-09 16:02:18 +01004120
4121 if (fd == -1 || device == NULL)
4122 return -EINVAL;
4123
4124 if (fstat(fd, &sbuf))
4125 return -errno;
4126
Qiang Yu3c208932016-07-14 17:10:55 +08004127 find_rdev = sbuf.st_rdev;
Emil Velikovccedf662015-09-09 16:02:18 +01004128 maj = major(sbuf.st_rdev);
4129 min = minor(sbuf.st_rdev);
4130
Thomas Hellstromf8392582018-08-31 13:47:05 +02004131 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Emil Velikovccedf662015-09-09 16:02:18 +01004132 return -EINVAL;
4133
4134 subsystem_type = drmParseSubsystemType(maj, min);
Emil Velikov7f52a0e2018-05-15 16:43:58 +01004135 if (subsystem_type < 0)
4136 return subsystem_type;
Emil Velikovccedf662015-09-09 16:02:18 +01004137
Emil Velikovccedf662015-09-09 16:02:18 +01004138 sysdir = opendir(DRM_DIR_NAME);
Emil Velikov95b262f2018-05-15 16:32:10 +01004139 if (!sysdir)
4140 return -errno;
Emil Velikovccedf662015-09-09 16:02:18 +01004141
4142 i = 0;
4143 while ((dent = readdir(sysdir))) {
Emil Velikovf808fee2018-05-15 15:02:52 +01004144 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
4145 if (ret)
Emil Velikovccedf662015-09-09 16:02:18 +01004146 continue;
4147
Emil Velikov95b262f2018-05-15 16:32:10 +01004148 if (i >= MAX_DRM_NODES) {
4149 fprintf(stderr, "More than %d drm nodes detected. "
4150 "Please report a bug - that should not happen.\n"
4151 "Skipping extra nodes\n", MAX_DRM_NODES);
4152 break;
Emil Velikovccedf662015-09-09 16:02:18 +01004153 }
Emil Velikov56e72d32018-06-21 16:06:35 +01004154 local_devices[i] = d;
Emil Velikovccedf662015-09-09 16:02:18 +01004155 i++;
4156 }
4157 node_count = i;
4158
Emil Velikovccedf662015-09-09 16:02:18 +01004159 drmFoldDuplicatedDevices(local_devices, node_count);
4160
Mariusz Ceier4519db22018-07-29 10:20:14 +02004161 *device = NULL;
4162
Emil Velikov56e72d32018-06-21 16:06:35 +01004163 for (i = 0; i < node_count; i++) {
4164 if (!local_devices[i])
4165 continue;
4166
4167 if (drm_device_has_rdev(local_devices[i], find_rdev))
4168 *device = local_devices[i];
4169 else
4170 drmFreeDevice(&local_devices[i]);
4171 }
Emil Velikovccedf662015-09-09 16:02:18 +01004172
Emil Velikovccedf662015-09-09 16:02:18 +01004173 closedir(sysdir);
Rob Herring677cd972016-10-21 10:07:59 -07004174 if (*device == NULL)
Thierry Redinge17cad12016-12-21 18:00:52 +01004175 return -ENODEV;
Emil Velikovccedf662015-09-09 16:02:18 +01004176 return 0;
Jonathan Gray08257922016-12-01 15:18:43 +11004177#endif
Emil Velikovccedf662015-09-09 16:02:18 +01004178}
4179
4180/**
Emil Velikov11687bf2016-11-30 17:24:21 +00004181 * Get information about the opened drm device
4182 *
4183 * \param fd file descriptor of the drm device
4184 * \param device the address of a drmDevicePtr where the information
4185 * will be allocated in stored
4186 *
4187 * \return zero on success, negative error code otherwise.
4188 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004189drm_public int drmGetDevice(int fd, drmDevicePtr *device)
Emil Velikov11687bf2016-11-30 17:24:21 +00004190{
4191 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
4192}
4193
4194/**
Emil Velikovb556ea12015-08-17 11:09:06 +08004195 * Get drm devices on the system
4196 *
Emil Velikov11687bf2016-11-30 17:24:21 +00004197 * \param flags feature/behaviour bitmask
Emil Velikovb556ea12015-08-17 11:09:06 +08004198 * \param devices the array of devices with drmDevicePtr elements
4199 * can be NULL to get the device number first
4200 * \param max_devices the maximum number of devices for the array
4201 *
4202 * \return on error - negative error code,
4203 * if devices is NULL - total number of devices available on the system,
4204 * alternatively the number of devices stored in devices[], which is
4205 * capped by the max_devices.
Emil Velikov11687bf2016-11-30 17:24:21 +00004206 *
4207 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
4208 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
Emil Velikovb556ea12015-08-17 11:09:06 +08004209 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004210drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
4211 int max_devices)
Emil Velikovb556ea12015-08-17 11:09:06 +08004212{
Emil Velikov95b262f2018-05-15 16:32:10 +01004213 drmDevicePtr local_devices[MAX_DRM_NODES];
Emil Velikov5f68d312015-09-07 13:54:32 +01004214 drmDevicePtr device;
4215 DIR *sysdir;
4216 struct dirent *dent;
Emil Velikovfae59d72015-09-09 17:54:34 +01004217 int ret, i, node_count, device_count;
Emil Velikov11687bf2016-11-30 17:24:21 +00004218
4219 if (drm_device_validate_flags(flags))
4220 return -EINVAL;
Emil Velikovb556ea12015-08-17 11:09:06 +08004221
Emil Velikovb556ea12015-08-17 11:09:06 +08004222 sysdir = opendir(DRM_DIR_NAME);
Emil Velikov95b262f2018-05-15 16:32:10 +01004223 if (!sysdir)
4224 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08004225
Emil Velikov5f68d312015-09-07 13:54:32 +01004226 i = 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08004227 while ((dent = readdir(sysdir))) {
Emil Velikovf808fee2018-05-15 15:02:52 +01004228 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
4229 if (ret)
Emil Velikovb556ea12015-08-17 11:09:06 +08004230 continue;
4231
Emil Velikov95b262f2018-05-15 16:32:10 +01004232 if (i >= MAX_DRM_NODES) {
4233 fprintf(stderr, "More than %d drm nodes detected. "
4234 "Please report a bug - that should not happen.\n"
4235 "Skipping extra nodes\n", MAX_DRM_NODES);
4236 break;
Emil Velikov5f68d312015-09-07 13:54:32 +01004237 }
Emil Velikov5f68d312015-09-07 13:54:32 +01004238 local_devices[i] = device;
Emil Velikovb556ea12015-08-17 11:09:06 +08004239 i++;
4240 }
Emil Velikovb556ea12015-08-17 11:09:06 +08004241 node_count = i;
4242
Emil Velikovfae59d72015-09-09 17:54:34 +01004243 drmFoldDuplicatedDevices(local_devices, node_count);
Emil Velikovb556ea12015-08-17 11:09:06 +08004244
Emil Velikov5f68d312015-09-07 13:54:32 +01004245 device_count = 0;
Qiang Yu70b64072016-06-06 12:29:16 -04004246 for (i = 0; i < node_count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04004247 if (!local_devices[i])
4248 continue;
Qiang Yu70b64072016-06-06 12:29:16 -04004249
Emil Velikov5f68d312015-09-07 13:54:32 +01004250 if ((devices != NULL) && (device_count < max_devices))
4251 devices[device_count] = local_devices[i];
4252 else
4253 drmFreeDevice(&local_devices[i]);
4254
4255 device_count++;
Emil Velikovb556ea12015-08-17 11:09:06 +08004256 }
4257
Emil Velikovb556ea12015-08-17 11:09:06 +08004258 closedir(sysdir);
Emil Velikov5f68d312015-09-07 13:54:32 +01004259 return device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08004260}
Emil Velikov37d790f2016-11-10 17:26:50 +00004261
Emil Velikov11687bf2016-11-30 17:24:21 +00004262/**
4263 * Get drm devices on the system
4264 *
4265 * \param devices the array of devices with drmDevicePtr elements
4266 * can be NULL to get the device number first
4267 * \param max_devices the maximum number of devices for the array
4268 *
4269 * \return on error - negative error code,
4270 * if devices is NULL - total number of devices available on the system,
4271 * alternatively the number of devices stored in devices[], which is
4272 * capped by the max_devices.
4273 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004274drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
Emil Velikov11687bf2016-11-30 17:24:21 +00004275{
4276 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4277}
4278
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004279drm_public char *drmGetDeviceNameFromFd2(int fd)
Emil Velikov37d790f2016-11-10 17:26:50 +00004280{
4281#ifdef __linux__
4282 struct stat sbuf;
Thierry Reding5403cb32017-01-18 08:29:23 +01004283 char path[PATH_MAX + 1], *value;
Emil Velikov37d790f2016-11-10 17:26:50 +00004284 unsigned int maj, min;
Emil Velikov37d790f2016-11-10 17:26:50 +00004285
4286 if (fstat(fd, &sbuf))
4287 return NULL;
4288
4289 maj = major(sbuf.st_rdev);
4290 min = minor(sbuf.st_rdev);
4291
Thomas Hellstromf8392582018-08-31 13:47:05 +02004292 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Emil Velikov37d790f2016-11-10 17:26:50 +00004293 return NULL;
4294
Thierry Reding5403cb32017-01-18 08:29:23 +01004295 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4296
4297 value = sysfs_uevent_get(path, "DEVNAME");
4298 if (!value)
Emil Velikov37d790f2016-11-10 17:26:50 +00004299 return NULL;
4300
Thierry Reding5403cb32017-01-18 08:29:23 +01004301 snprintf(path, sizeof(path), "/dev/%s", value);
4302 free(value);
Emil Velikov37d790f2016-11-10 17:26:50 +00004303
Thierry Reding5403cb32017-01-18 08:29:23 +01004304 return strdup(path);
Emmanuel Vadot13c9de32020-01-21 17:58:28 +01004305#elif __FreeBSD__
4306 return drmGetDeviceNameFromFd(fd);
Emil Velikov37d790f2016-11-10 17:26:50 +00004307#else
Jonathan Graye2e766d2016-12-17 16:09:52 +11004308 struct stat sbuf;
4309 char node[PATH_MAX + 1];
4310 const char *dev_name;
4311 int node_type;
Jonathan Gray293b95e2019-05-13 02:52:04 +10004312 int maj, min, n;
Jonathan Graye2e766d2016-12-17 16:09:52 +11004313
4314 if (fstat(fd, &sbuf))
4315 return NULL;
4316
4317 maj = major(sbuf.st_rdev);
4318 min = minor(sbuf.st_rdev);
4319
Thomas Hellstromf8392582018-08-31 13:47:05 +02004320 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jonathan Graye2e766d2016-12-17 16:09:52 +11004321 return NULL;
4322
Emmanuel Vadotc55a1e52020-01-21 17:47:09 +01004323 node_type = drmGetMinorType(maj, min);
Jonathan Graye2e766d2016-12-17 16:09:52 +11004324 if (node_type == -1)
4325 return NULL;
4326
Eric Engestrom331e51e2018-12-19 13:53:41 +00004327 dev_name = drmGetDeviceName(node_type);
4328 if (!dev_name)
Jonathan Graye2e766d2016-12-17 16:09:52 +11004329 return NULL;
Jonathan Graye2e766d2016-12-17 16:09:52 +11004330
Jonathan Gray293b95e2019-05-13 02:52:04 +10004331 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
Jonathan Graye2e766d2016-12-17 16:09:52 +11004332 if (n == -1 || n >= PATH_MAX)
4333 return NULL;
4334
4335 return strdup(node);
Emil Velikov37d790f2016-11-10 17:26:50 +00004336#endif
4337}
Dave Airliefc492272017-06-17 11:01:01 +10004338
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004339drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
Dave Airliefc492272017-06-17 11:01:01 +10004340{
4341 struct drm_syncobj_create args;
4342 int ret;
4343
4344 memclear(args);
4345 args.flags = flags;
4346 args.handle = 0;
4347 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4348 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004349 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004350 *handle = args.handle;
4351 return 0;
4352}
4353
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004354drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
Dave Airliefc492272017-06-17 11:01:01 +10004355{
4356 struct drm_syncobj_destroy args;
4357
4358 memclear(args);
4359 args.handle = handle;
4360 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4361}
4362
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004363drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
Dave Airliefc492272017-06-17 11:01:01 +10004364{
4365 struct drm_syncobj_handle args;
4366 int ret;
4367
4368 memclear(args);
4369 args.fd = -1;
4370 args.handle = handle;
4371 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4372 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004373 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004374 *obj_fd = args.fd;
4375 return 0;
4376}
4377
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004378drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
Dave Airliefc492272017-06-17 11:01:01 +10004379{
4380 struct drm_syncobj_handle args;
4381 int ret;
4382
4383 memclear(args);
4384 args.fd = obj_fd;
4385 args.handle = 0;
4386 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4387 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004388 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004389 *handle = args.handle;
4390 return 0;
4391}
4392
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004393drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4394 int sync_file_fd)
Dave Airliefc492272017-06-17 11:01:01 +10004395{
4396 struct drm_syncobj_handle args;
4397
4398 memclear(args);
4399 args.fd = sync_file_fd;
4400 args.handle = handle;
4401 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4402 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4403}
4404
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004405drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4406 int *sync_file_fd)
Dave Airliefc492272017-06-17 11:01:01 +10004407{
4408 struct drm_syncobj_handle args;
4409 int ret;
4410
4411 memclear(args);
4412 args.fd = -1;
4413 args.handle = handle;
4414 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4415 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4416 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004417 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004418 *sync_file_fd = args.fd;
4419 return 0;
4420}
Marek Olšák2048a9e2017-09-11 21:57:32 +02004421
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004422drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4423 int64_t timeout_nsec, unsigned flags,
4424 uint32_t *first_signaled)
Marek Olšák2048a9e2017-09-11 21:57:32 +02004425{
Dave Airlie61ff9772017-10-25 07:43:56 +01004426 struct drm_syncobj_wait args;
4427 int ret;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004428
Dave Airlie61ff9772017-10-25 07:43:56 +01004429 memclear(args);
Bas Nieuwenhuizenb1e63d92018-02-06 11:21:35 +01004430 args.handles = (uintptr_t)handles;
Dave Airlie61ff9772017-10-25 07:43:56 +01004431 args.timeout_nsec = timeout_nsec;
4432 args.count_handles = num_handles;
4433 args.flags = flags;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004434
Dave Airlie61ff9772017-10-25 07:43:56 +01004435 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4436 if (ret < 0)
Chunming Zhoubde3b9b2018-02-07 11:22:32 +08004437 return -errno;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004438
Dave Airlie61ff9772017-10-25 07:43:56 +01004439 if (first_signaled)
4440 *first_signaled = args.first_signaled;
4441 return ret;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004442}
Bas Nieuwenhuizen1abcced2017-12-17 00:27:09 +01004443
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004444drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4445 uint32_t handle_count)
Bas Nieuwenhuizen1abcced2017-12-17 00:27:09 +01004446{
4447 struct drm_syncobj_array args;
4448 int ret;
4449
4450 memclear(args);
4451 args.handles = (uintptr_t)handles;
4452 args.count_handles = handle_count;
4453
4454 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4455 return ret;
4456}
4457
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004458drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4459 uint32_t handle_count)
Bas Nieuwenhuizen1abcced2017-12-17 00:27:09 +01004460{
4461 struct drm_syncobj_array args;
4462 int ret;
4463
4464 memclear(args);
4465 args.handles = (uintptr_t)handles;
4466 args.count_handles = handle_count;
4467
4468 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4469 return ret;
4470}
Chunming Zhouec6ae512019-05-16 16:07:09 +08004471
Chunming Zhou12712eb2019-05-16 16:07:11 +08004472drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4473 uint64_t *points, uint32_t handle_count)
4474{
4475 struct drm_syncobj_timeline_array args;
4476 int ret;
4477
4478 memclear(args);
4479 args.handles = (uintptr_t)handles;
4480 args.points = (uintptr_t)points;
4481 args.count_handles = handle_count;
4482
4483 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4484 return ret;
4485}
4486
Chunming Zhouec6ae512019-05-16 16:07:09 +08004487drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4488 unsigned num_handles,
4489 int64_t timeout_nsec, unsigned flags,
4490 uint32_t *first_signaled)
4491{
4492 struct drm_syncobj_timeline_wait args;
4493 int ret;
4494
4495 memclear(args);
4496 args.handles = (uintptr_t)handles;
4497 args.points = (uintptr_t)points;
4498 args.timeout_nsec = timeout_nsec;
4499 args.count_handles = num_handles;
4500 args.flags = flags;
4501
4502 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4503 if (ret < 0)
4504 return -errno;
4505
4506 if (first_signaled)
4507 *first_signaled = args.first_signaled;
4508 return ret;
4509}
4510
4511
4512drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4513 uint32_t handle_count)
4514{
4515 struct drm_syncobj_timeline_array args;
4516 int ret;
4517
4518 memclear(args);
4519 args.handles = (uintptr_t)handles;
4520 args.points = (uintptr_t)points;
4521 args.count_handles = handle_count;
4522
4523 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4524 if (ret)
4525 return ret;
4526 return 0;
4527}
4528
Chunming Zhou0a7ad7d2019-07-24 15:55:20 +08004529drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
4530 uint32_t handle_count, uint32_t flags)
4531{
4532 struct drm_syncobj_timeline_array args;
4533
4534 memclear(args);
4535 args.handles = (uintptr_t)handles;
4536 args.points = (uintptr_t)points;
4537 args.count_handles = handle_count;
4538 args.flags = flags;
4539
4540 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4541}
4542
4543
Chunming Zhou12712eb2019-05-16 16:07:11 +08004544drm_public int drmSyncobjTransfer(int fd,
4545 uint32_t dst_handle, uint64_t dst_point,
4546 uint32_t src_handle, uint64_t src_point,
4547 uint32_t flags)
4548{
4549 struct drm_syncobj_transfer args;
4550 int ret;
Chunming Zhouec6ae512019-05-16 16:07:09 +08004551
Chunming Zhou12712eb2019-05-16 16:07:11 +08004552 memclear(args);
4553 args.src_handle = src_handle;
4554 args.dst_handle = dst_handle;
4555 args.src_point = src_point;
4556 args.dst_point = dst_point;
4557 args.flags = flags;
4558
4559 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);
4560
4561 return ret;
4562}