blob: fd8c5961a70e546307af2eeda6f0424d524ac6bb [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
Emil Velikovb556ea12015-08-17 11:09:06 +080060#include <math.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000061
Emmanuel Vadot1c8d2b72020-01-21 17:42:44 +010062#if defined(__FreeBSD__)
63#include <sys/param.h>
64#endif
65
Eric Anholt9b28c5a2018-11-15 17:48:53 -080066#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
67
Daryll Straussb3a57661999-12-05 01:19:48 +000068/* Not all systems have MAP_FAILED defined */
69#ifndef MAP_FAILED
70#define MAP_FAILED ((void *)-1)
71#endif
72
Daryll Straussb3a57661999-12-05 01:19:48 +000073#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010074#include "libdrm_macros.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000075
Emil Velikov5f68d312015-09-07 13:54:32 +010076#include "util_math.h"
77
Emmanuel Vadot4fbcc9a2020-01-21 17:39:10 +010078#ifdef __DragonFly__
Eric Anholtcfa778a2003-02-21 23:23:09 +000079#define DRM_MAJOR 145
Rik Faith88dbee52001-02-28 09:27:44 +000080#endif
81
Eric Anholtcfa778a2003-02-21 23:23:09 +000082#ifdef __NetBSD__
83#define DRM_MAJOR 34
84#endif
85
Jonathan Gray66c3afb2015-07-21 03:12:10 +100086#ifdef __OpenBSD__
87#ifdef __i386__
88#define DRM_MAJOR 88
89#else
90#define DRM_MAJOR 87
91#endif
92#endif /* __OpenBSD__ */
Alan Hourihaneb0a92852003-09-24 14:39:25 +000093
Eric Anholtcfa778a2003-02-21 23:23:09 +000094#ifndef DRM_MAJOR
Jan Vesely50d3c852016-06-30 14:22:52 -040095#define DRM_MAJOR 226 /* Linux */
Rik Faith88dbee52001-02-28 09:27:44 +000096#endif
97
François Tigeot8f2e0922018-12-12 20:48:36 +010098#if defined(__OpenBSD__) || defined(__DragonFly__)
Jonathan Grayc0ef1d02016-12-01 15:18:41 +110099struct drm_pciinfo {
100 uint16_t domain;
101 uint8_t bus;
102 uint8_t dev;
103 uint8_t func;
104 uint16_t vendor_id;
105 uint16_t device_id;
106 uint16_t subvendor_id;
107 uint16_t subdevice_id;
108 uint8_t revision_id;
109};
110
111#define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
112#endif
113
David Dawes56bd9c22001-07-30 19:59:39 +0000114#define DRM_MSG_VERBOSITY 3
115
Daniel Vetterfd387942015-02-11 12:41:04 +0100116#define memclear(s) memset(&s, 0, sizeof(s))
117
Dave Airlie79038752006-11-08 15:08:09 +1100118static drmServerInfoPtr drm_server_info;
119
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700120drm_public void drmSetServerInfo(drmServerInfoPtr info)
Dave Airlie79038752006-11-08 15:08:09 +1100121{
Brianccd7b6e2007-05-29 14:54:00 -0600122 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +1100123}
124
Jose Fonsecad2443b22003-05-27 00:37:33 +0000125/**
126 * Output a message to stderr.
127 *
128 * \param format printf() like format string.
129 *
130 * \internal
131 * This function is a wrapper around vfprintf().
132 */
Dave Airlie79038752006-11-08 15:08:09 +1100133
Thierry Reding44b08c02014-01-22 12:06:51 +0100134static int DRM_PRINTFLIKE(1, 0)
135drmDebugPrint(const char *format, va_list ap)
Dave Airlie79038752006-11-08 15:08:09 +1100136{
Brianccd7b6e2007-05-29 14:54:00 -0600137 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100138}
139
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700140drm_public void
David Dawes56bd9c22001-07-30 19:59:39 +0000141drmMsg(const char *format, ...)
142{
Jan Vesely50d3c852016-06-30 14:22:52 -0400143 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000144 const char *env;
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400145 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
146 (drm_server_info && drm_server_info->debug_print))
David Dawes56bd9c22001-07-30 19:59:39 +0000147 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400148 va_start(ap, format);
149 if (drm_server_info) {
150 drm_server_info->debug_print(format,ap);
151 } else {
152 drmDebugPrint(format, ap);
153 }
154 va_end(ap);
David Dawes56bd9c22001-07-30 19:59:39 +0000155 }
156}
157
Daryll Straussb3a57661999-12-05 01:19:48 +0000158static void *drmHashTable = NULL; /* Context switch callbacks */
159
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700160drm_public void *drmGetHashTable(void)
Dave Airlie79038752006-11-08 15:08:09 +1100161{
Brianccd7b6e2007-05-29 14:54:00 -0600162 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100163}
Daryll Straussb3a57661999-12-05 01:19:48 +0000164
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700165drm_public void *drmMalloc(int size)
Daryll Straussb3a57661999-12-05 01:19:48 +0000166{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100167 return calloc(1, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000168}
169
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700170drm_public void drmFree(void *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +0000171{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100172 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000173}
174
Keith Packard8b9ab102008-06-13 16:03:22 -0700175/**
Eric Engestrom360292c2018-12-19 14:55:45 +0000176 * Call ioctl, restarting if it is interrupted
Keith Packard8b9ab102008-06-13 16:03:22 -0700177 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700178drm_public int
Coleman Kane41b83a92008-08-18 17:08:21 -0400179drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700180{
Jan Vesely50d3c852016-06-30 14:22:52 -0400181 int ret;
Keith Packard8b9ab102008-06-13 16:03:22 -0700182
183 do {
Jan Vesely50d3c852016-06-30 14:22:52 -0400184 ret = ioctl(fd, request, arg);
Keith Packard8b9ab102008-06-13 16:03:22 -0700185 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
186 return ret;
187}
Daryll Straussb3a57661999-12-05 01:19:48 +0000188
Daryll Straussb3a57661999-12-05 01:19:48 +0000189static unsigned long drmGetKeyFromFd(int fd)
190{
David Dawesfcc21062001-03-30 17:16:20 +0000191 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000192
193 st.st_rdev = 0;
194 fstat(fd, &st);
195 return st.st_rdev;
196}
197
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700198drm_public drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000199{
200 unsigned long key = drmGetKeyFromFd(fd);
201 void *value;
202 drmHashEntry *entry;
203
Brianccd7b6e2007-05-29 14:54:00 -0600204 if (!drmHashTable)
Jan Vesely50d3c852016-06-30 14:22:52 -0400205 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000206
207 if (drmHashLookup(drmHashTable, key, &value)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400208 entry = drmMalloc(sizeof(*entry));
209 entry->fd = fd;
210 entry->f = NULL;
211 entry->tagTable = drmHashCreate();
212 drmHashInsert(drmHashTable, key, entry);
Daryll Straussb3a57661999-12-05 01:19:48 +0000213 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -0400214 entry = value;
Daryll Straussb3a57661999-12-05 01:19:48 +0000215 }
216 return entry;
217}
218
Jose Fonsecad2443b22003-05-27 00:37:33 +0000219/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000220 * Compare two busid strings
221 *
222 * \param first
223 * \param second
224 *
225 * \return 1 if matched.
226 *
227 * \internal
228 * This function compares two bus ID strings. It understands the older
229 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
230 * domain, b is bus, d is device, f is function.
231 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000232static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000233{
234 /* First, check if the IDs are exactly the same */
235 if (strcasecmp(id1, id2) == 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400236 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000237
238 /* Try to match old/new-style PCI bus IDs. */
239 if (strncasecmp(id1, "pci", 3) == 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400240 unsigned int o1, b1, d1, f1;
241 unsigned int o2, b2, d2, f2;
242 int ret;
Eric Anholt06cb1322003-10-23 02:23:31 +0000243
Jan Vesely50d3c852016-06-30 14:22:52 -0400244 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
245 if (ret != 4) {
246 o1 = 0;
247 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
248 if (ret != 3)
249 return 0;
250 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000251
Jan Vesely50d3c852016-06-30 14:22:52 -0400252 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
253 if (ret != 4) {
254 o2 = 0;
255 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
256 if (ret != 3)
257 return 0;
258 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000259
Jan Vesely50d3c852016-06-30 14:22:52 -0400260 /* If domains aren't properly supported by the kernel interface,
261 * just ignore them, which sucks less than picking a totally random
262 * card with "open by name"
263 */
264 if (!pci_domain_ok)
265 o1 = o2 = 0;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000266
Jan Vesely50d3c852016-06-30 14:22:52 -0400267 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
268 return 0;
269 else
270 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000271 }
272 return 0;
273}
274
275/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300276 * Handles error checking for chown call.
277 *
278 * \param path to file.
279 * \param id of the new owner.
280 * \param id of the new group.
281 *
282 * \return zero if success or -1 if failure.
283 *
284 * \internal
285 * Checks for failure. If failure was caused by signal call chown again.
Eric Engestrom360292c2018-12-19 14:55:45 +0000286 * If any other failure happened then it will output error message using
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300287 * drmMsg() call.
288 */
Eric Engestrom07585202018-03-16 17:04:50 +0000289#if !UDEV
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300290static int chown_check_return(const char *path, uid_t owner, gid_t group)
291{
Jan Vesely50d3c852016-06-30 14:22:52 -0400292 int rv;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300293
Jan Vesely50d3c852016-06-30 14:22:52 -0400294 do {
295 rv = chown(path, owner, group);
296 } while (rv != 0 && errno == EINTR);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300297
Jan Vesely50d3c852016-06-30 14:22:52 -0400298 if (rv == 0)
299 return 0;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300300
Jan Vesely50d3c852016-06-30 14:22:52 -0400301 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
302 path, errno, strerror(errno));
303 return -1;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300304}
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500305#endif
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300306
Eric Engestrom331e51e2018-12-19 13:53:41 +0000307static const char *drmGetDeviceName(int type)
308{
309 switch (type) {
310 case DRM_NODE_PRIMARY:
311 return DRM_DEV_NAME;
312 case DRM_NODE_CONTROL:
313 return DRM_CONTROL_DEV_NAME;
314 case DRM_NODE_RENDER:
315 return DRM_RENDER_DEV_NAME;
316 }
317 return NULL;
318}
319
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300320/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000321 * Open the DRM device, creating it if necessary.
322 *
323 * \param dev major and minor numbers of the device.
324 * \param minor minor number of the device.
Jan Vesely50d3c852016-06-30 14:22:52 -0400325 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000326 * \return a file descriptor on success, or a negative value on error.
327 *
328 * \internal
329 * Assembles the device name from \p minor and opens it, creating the device
330 * special file node with the major and minor numbers specified by \p dev and
331 * parent directory if necessary and was called by root.
332 */
Jan Veselyde8532d2014-11-30 12:53:18 -0500333static int drmOpenDevice(dev_t dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000334{
David Dawes9c775d02001-05-14 14:49:58 +0000335 stat_t st;
Eric Engestrom331e51e2018-12-19 13:53:41 +0000336 const char *dev_name = drmGetDeviceName(type);
Eric Engestrom6869e4c2018-12-19 13:42:08 +0000337 char buf[DRM_NODE_NAME_MAX];
Rik Faith88dbee52001-02-28 09:27:44 +0000338 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100339 mode_t devmode = DRM_DEV_MODE, serv_mode;
Jan Vesely0706c142015-02-27 12:47:46 -0500340 gid_t serv_group;
Eric Engestrom07585202018-03-16 17:04:50 +0000341#if !UDEV
Rik Faith88dbee52001-02-28 09:27:44 +0000342 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000343 uid_t user = DRM_DEV_UID;
Jan Vesely0706c142015-02-27 12:47:46 -0500344 gid_t group = DRM_DEV_GID;
345#endif
346
Eric Engestrom331e51e2018-12-19 13:53:41 +0000347 if (!dev_name)
Jan Vesely50d3c852016-06-30 14:22:52 -0400348 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000349
350 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000351 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000352
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400353 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400354 drm_server_info->get_perms(&serv_group, &serv_mode);
355 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
356 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Dave Airlie79038752006-11-08 15:08:09 +1100357 }
Brian Paul569da5a2000-06-08 14:38:22 +0000358
Eric Engestrom07585202018-03-16 17:04:50 +0000359#if !UDEV
Rik Faith88dbee52001-02-28 09:27:44 +0000360 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400361 if (!isroot)
362 return DRM_ERR_NOT_ROOT;
363 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
364 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
365 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000366 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000367
Eric Anholt06cb1322003-10-23 02:23:31 +0000368 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000369 if (stat(buf, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400370 if (!isroot)
371 return DRM_ERR_NOT_ROOT;
372 remove(buf);
373 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000374 }
Dave Airlie79038752006-11-08 15:08:09 +1100375
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400376 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400377 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
378 chown_check_return(buf, user, group);
379 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100380 }
Dave Airlie9101a022008-08-24 16:54:43 +1000381#else
382 /* if we modprobed then wait for udev */
383 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400384 int udev_count = 0;
Dave Airlie9101a022008-08-24 16:54:43 +1000385wait_for_udev:
386 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400387 usleep(20);
388 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000389
Jan Vesely50d3c852016-06-30 14:22:52 -0400390 if (udev_count == 50)
391 return -1;
392 goto wait_for_udev;
393 }
Dave Airlie9101a022008-08-24 16:54:43 +1000394
Jan Vesely50d3c852016-06-30 14:22:52 -0400395 if (stat(buf, &st)) {
396 usleep(20);
397 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000398
Jan Vesely50d3c852016-06-30 14:22:52 -0400399 if (udev_count == 50)
400 return -1;
401 goto wait_for_udev;
402 }
Dave Airlie9101a022008-08-24 16:54:43 +1000403 }
404#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000405
Michel Dänzer35615692018-05-18 11:16:51 +0200406 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
David Dawes56bd9c22001-07-30 19:59:39 +0000407 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400408 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600409 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400410 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000411
Eric Engestrom07585202018-03-16 17:04:50 +0000412#if !UDEV
Eric Anholt06cb1322003-10-23 02:23:31 +0000413 /* Check if the device node is not what we expect it to be, and recreate it
414 * and try again if so.
415 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000416 if (st.st_rdev != dev) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400417 if (!isroot)
418 return DRM_ERR_NOT_ROOT;
419 remove(buf);
420 mknod(buf, S_IFCHR | devmode, dev);
421 if (drm_server_info && drm_server_info->get_perms) {
422 chown_check_return(buf, user, group);
423 chmod(buf, devmode);
424 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000425 }
Michel Dänzer35615692018-05-18 11:16:51 +0200426 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
Eric Anholtd2f2b422002-08-08 21:23:46 +0000427 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400428 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600429 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400430 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000431
David Dawes56bd9c22001-07-30 19:59:39 +0000432 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000433 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000434#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000435 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000436}
437
Jose Fonsecad2443b22003-05-27 00:37:33 +0000438
439/**
440 * Open the DRM device
441 *
442 * \param minor device minor number.
443 * \param create allow to create the device if set.
444 *
445 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400446 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000447 * \internal
448 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
449 * name from \p minor and opens it.
450 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800451static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000452{
453 int fd;
Eric Engestrom6869e4c2018-12-19 13:42:08 +0000454 char buf[DRM_NODE_NAME_MAX];
Eric Engestrom331e51e2018-12-19 13:53:41 +0000455 const char *dev_name = drmGetDeviceName(type);
Jan Vesely50d3c852016-06-30 14:22:52 -0400456
Brianccd7b6e2007-05-29 14:54:00 -0600457 if (create)
Jan Vesely50d3c852016-06-30 14:22:52 -0400458 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
459
Eric Engestrom331e51e2018-12-19 13:53:41 +0000460 if (!dev_name)
Jan Vesely50d3c852016-06-30 14:22:52 -0400461 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000462
463 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Michel Dänzer35615692018-05-18 11:16:51 +0200464 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400465 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000466 return -errno;
467}
468
Brian Paul569da5a2000-06-08 14:38:22 +0000469
Jose Fonsecad2443b22003-05-27 00:37:33 +0000470/**
471 * Determine whether the DRM kernel driver has been loaded.
Jan Vesely50d3c852016-06-30 14:22:52 -0400472 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000473 * \return 1 if the DRM driver is loaded, 0 otherwise.
474 *
Jan Vesely50d3c852016-06-30 14:22:52 -0400475 * \internal
Jose Fonsecad2443b22003-05-27 00:37:33 +0000476 * Determine the presence of the kernel driver by attempting to open the 0
477 * minor and get version information. For backward compatibility with older
478 * Linux implementations, /proc/dri is also checked.
479 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700480drm_public int drmAvailable(void)
Brian Paul569da5a2000-06-08 14:38:22 +0000481{
Brian Paul569da5a2000-06-08 14:38:22 +0000482 drmVersionPtr version;
483 int retval = 0;
484 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000485
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000486 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000487#ifdef __linux__
Jan Vesely50d3c852016-06-30 14:22:52 -0400488 /* Try proc for backward Linux compatibility */
489 if (!access("/proc/dri/0", R_OK))
490 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000491#endif
Jan Vesely50d3c852016-06-30 14:22:52 -0400492 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000493 }
Jan Vesely50d3c852016-06-30 14:22:52 -0400494
Rik Faith88dbee52001-02-28 09:27:44 +0000495 if ((version = drmGetVersion(fd))) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400496 retval = 1;
497 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000498 }
499 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000500
501 return retval;
502}
503
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800504static int drmGetMinorBase(int type)
505{
506 switch (type) {
507 case DRM_NODE_PRIMARY:
508 return 0;
509 case DRM_NODE_CONTROL:
510 return 64;
511 case DRM_NODE_RENDER:
512 return 128;
513 default:
514 return -1;
515 };
516}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000517
Frank Binns1f735782015-02-13 10:51:15 +0000518static int drmGetMinorType(int minor)
519{
520 int type = minor >> 6;
521
522 if (minor < 0)
523 return -1;
524
525 switch (type) {
526 case DRM_NODE_PRIMARY:
527 case DRM_NODE_CONTROL:
528 case DRM_NODE_RENDER:
529 return type;
530 default:
531 return -1;
532 }
533}
534
Emil Velikov0ca03a42015-03-07 00:58:39 +0000535static const char *drmGetMinorName(int type)
536{
537 switch (type) {
538 case DRM_NODE_PRIMARY:
Jonathan Grayfc083322015-07-21 03:12:56 +1000539 return DRM_PRIMARY_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000540 case DRM_NODE_CONTROL:
Jonathan Grayfc083322015-07-21 03:12:56 +1000541 return DRM_CONTROL_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000542 case DRM_NODE_RENDER:
Jonathan Grayfc083322015-07-21 03:12:56 +1000543 return DRM_RENDER_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000544 default:
545 return NULL;
546 }
547}
548
Jose Fonsecad2443b22003-05-27 00:37:33 +0000549/**
550 * Open the device by bus ID.
551 *
552 * \param busid bus ID.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800553 * \param type device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000554 *
555 * \return a file descriptor on success, or a negative value on error.
556 *
557 * \internal
558 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
559 * comparing the device bus ID with the one supplied.
560 *
561 * \sa drmOpenMinor() and drmGetBusid().
562 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800563static int drmOpenByBusid(const char *busid, int type)
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000564{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000565 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000566 int fd;
567 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000568 drmSetVersion sv;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800569 int base = drmGetMinorBase(type);
570
571 if (base < 0)
572 return -1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000573
574 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800575 for (i = base; i < base + DRM_MAX_MINOR; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400576 fd = drmOpenMinor(i, 1, type);
577 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
578 if (fd >= 0) {
579 /* We need to try for 1.4 first for proper PCI domain support
580 * and if that fails, we know the kernel is busted
581 */
582 sv.drm_di_major = 1;
583 sv.drm_di_minor = 4;
584 sv.drm_dd_major = -1; /* Don't care */
585 sv.drm_dd_minor = -1; /* Don't care */
586 if (drmSetInterfaceVersion(fd, &sv)) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000587#ifndef __alpha__
Jan Vesely50d3c852016-06-30 14:22:52 -0400588 pci_domain_ok = 0;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000589#endif
Jan Vesely50d3c852016-06-30 14:22:52 -0400590 sv.drm_di_major = 1;
591 sv.drm_di_minor = 1;
592 sv.drm_dd_major = -1; /* Don't care */
593 sv.drm_dd_minor = -1; /* Don't care */
594 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
595 drmSetInterfaceVersion(fd, &sv);
596 }
597 buf = drmGetBusid(fd);
598 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
599 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
600 drmFreeBusid(buf);
601 return fd;
602 }
603 if (buf)
604 drmFreeBusid(buf);
605 close(fd);
606 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000607 }
608 return -1;
609}
610
Jose Fonsecad2443b22003-05-27 00:37:33 +0000611
612/**
613 * Open the device by name.
614 *
615 * \param name driver name.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800616 * \param type the device node type.
Jan Vesely50d3c852016-06-30 14:22:52 -0400617 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000618 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400619 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000620 * \internal
621 * This function opens the first minor number that matches the driver name and
622 * isn't already in use. If it's in use it then it will already have a bus ID
623 * assigned.
Jan Vesely50d3c852016-06-30 14:22:52 -0400624 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000625 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
626 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800627static int drmOpenByName(const char *name, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000628{
Rik Faith88dbee52001-02-28 09:27:44 +0000629 int i;
630 int fd;
631 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000632 char * id;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800633 int base = drmGetMinorBase(type);
634
635 if (base < 0)
636 return -1;
Dave Airliedb85ed22008-02-13 12:20:02 +1000637
David Dawes56bd9c22001-07-30 19:59:39 +0000638 /*
639 * Open the first minor number that matches the driver name and isn't
640 * already in use. If it's in use it will have a busid assigned already.
641 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800642 for (i = base; i < base + DRM_MAX_MINOR; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400643 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
644 if ((version = drmGetVersion(fd))) {
645 if (!strcmp(version->name, name)) {
646 drmFreeVersion(version);
647 id = drmGetBusid(fd);
648 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
649 if (!id || !*id) {
650 if (id)
651 drmFreeBusid(id);
652 return fd;
653 } else {
654 drmFreeBusid(id);
655 }
656 } else {
657 drmFreeVersion(version);
658 }
659 }
660 close(fd);
661 }
Rik Faith88dbee52001-02-28 09:27:44 +0000662 }
663
664#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000665 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000666 for (i = 0; i < 8; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400667 char proc_name[64], buf[512];
668 char *driver, *pt, *devstring;
669 int retcode;
670
671 sprintf(proc_name, "/proc/dri/%d/name", i);
672 if ((fd = open(proc_name, 0, 0)) >= 0) {
673 retcode = read(fd, buf, sizeof(buf)-1);
674 close(fd);
675 if (retcode) {
676 buf[retcode-1] = '\0';
677 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
678 ;
679 if (*pt) { /* Device is next */
680 *pt = '\0';
681 if (!strcmp(driver, name)) { /* Match */
682 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
683 ;
684 if (*pt) { /* Found busid */
685 return drmOpenByBusid(++pt, type);
686 } else { /* No busid */
687 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
688 }
689 }
690 }
691 }
692 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000693 }
Rik Faith88dbee52001-02-28 09:27:44 +0000694#endif
695
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000696 return -1;
697}
698
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000699
Jose Fonsecad2443b22003-05-27 00:37:33 +0000700/**
701 * Open the DRM device.
702 *
703 * Looks up the specified name and bus ID, and opens the device found. The
704 * entry in /dev/dri is created if necessary and if called by root.
705 *
706 * \param name driver name. Not referenced if bus ID is supplied.
707 * \param busid bus ID. Zero if not known.
Jan Vesely50d3c852016-06-30 14:22:52 -0400708 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000709 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400710 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000711 * \internal
712 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
713 * otherwise.
714 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700715drm_public int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000716{
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800717 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
718}
719
720/**
721 * Open the DRM device with specified type.
722 *
723 * Looks up the specified name and bus ID, and opens the device found. The
724 * entry in /dev/dri is created if necessary and if called by root.
725 *
726 * \param name driver name. Not referenced if bus ID is supplied.
727 * \param busid bus ID. Zero if not known.
728 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
729 *
730 * \return a file descriptor on success, or a negative value on error.
731 *
732 * \internal
733 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
734 * otherwise.
735 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700736drm_public int drmOpenWithType(const char *name, const char *busid, int type)
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800737{
Prabhanjan Kandula225d73f2019-04-24 10:08:39 -0700738 if (name != NULL && drm_server_info &&
739 drm_server_info->load_module && !drmAvailable()) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400740 /* try to load the kernel module */
741 if (!drm_server_info->load_module(name)) {
742 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
743 return -1;
744 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000745 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000746
Eric Anholt06cb1322003-10-23 02:23:31 +0000747 if (busid) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400748 int fd = drmOpenByBusid(busid, type);
749 if (fd >= 0)
750 return fd;
Eric Anholt06cb1322003-10-23 02:23:31 +0000751 }
Jan Vesely50d3c852016-06-30 14:22:52 -0400752
Eric Anholt06cb1322003-10-23 02:23:31 +0000753 if (name)
Jan Vesely50d3c852016-06-30 14:22:52 -0400754 return drmOpenByName(name, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000755
Eric Anholt06cb1322003-10-23 02:23:31 +0000756 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000757}
758
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700759drm_public int drmOpenControl(int minor)
Jesse Barnes731cd552008-12-17 10:09:49 -0800760{
761 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
762}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000763
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700764drm_public int drmOpenRender(int minor)
Frank Binns0c5aaee2015-01-14 14:07:51 +0000765{
766 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
767}
768
Jose Fonsecad2443b22003-05-27 00:37:33 +0000769/**
770 * Free the version information returned by drmGetVersion().
771 *
772 * \param v pointer to the version information.
773 *
774 * \internal
775 * It frees the memory pointed by \p %v as well as all the non-null strings
776 * pointers in it.
777 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700778drm_public void drmFreeVersion(drmVersionPtr v)
Daryll Straussb3a57661999-12-05 01:19:48 +0000779{
Brianccd7b6e2007-05-29 14:54:00 -0600780 if (!v)
Jan Vesely50d3c852016-06-30 14:22:52 -0400781 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100782 drmFree(v->name);
783 drmFree(v->date);
784 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000785 drmFree(v);
786}
787
Jose Fonsecad2443b22003-05-27 00:37:33 +0000788
789/**
790 * Free the non-public version information returned by the kernel.
791 *
792 * \param v pointer to the version information.
793 *
794 * \internal
795 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
796 * the non-null strings pointers in it.
797 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000798static void drmFreeKernelVersion(drm_version_t *v)
799{
Brianccd7b6e2007-05-29 14:54:00 -0600800 if (!v)
Jan Vesely50d3c852016-06-30 14:22:52 -0400801 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100802 drmFree(v->name);
803 drmFree(v->date);
804 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000805 drmFree(v);
806}
807
Jose Fonsecad2443b22003-05-27 00:37:33 +0000808
809/**
810 * Copy version information.
Jan Vesely50d3c852016-06-30 14:22:52 -0400811 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000812 * \param d destination pointer.
813 * \param s source pointer.
Jan Vesely50d3c852016-06-30 14:22:52 -0400814 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000815 * \internal
816 * Used by drmGetVersion() to translate the information returned by the ioctl
817 * interface in a private structure into the public structure counterpart.
818 */
Brian Paul569da5a2000-06-08 14:38:22 +0000819static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000820{
821 d->version_major = s->version_major;
822 d->version_minor = s->version_minor;
823 d->version_patchlevel = s->version_patchlevel;
824 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400825 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000826 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400827 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000828 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400829 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000830}
831
Daryll Straussb3a57661999-12-05 01:19:48 +0000832
Jose Fonsecad2443b22003-05-27 00:37:33 +0000833/**
834 * Query the driver version information.
835 *
836 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -0400837 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000838 * \return pointer to a drmVersion structure which should be freed with
839 * drmFreeVersion().
Jan Vesely50d3c852016-06-30 14:22:52 -0400840 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000841 * \note Similar information is available via /proc/dri.
Jan Vesely50d3c852016-06-30 14:22:52 -0400842 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000843 * \internal
844 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
845 * first with zeros to get the string lengths, and then the actually strings.
846 * It also null-terminates them since they might not be already.
847 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700848drm_public drmVersionPtr drmGetVersion(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000849{
850 drmVersionPtr retval;
851 drm_version_t *version = drmMalloc(sizeof(*version));
852
Keith Packard8b9ab102008-06-13 16:03:22 -0700853 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400854 drmFreeKernelVersion(version);
855 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000856 }
857
Daryll Straussb3a57661999-12-05 01:19:48 +0000858 if (version->name_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400859 version->name = drmMalloc(version->name_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000860 if (version->date_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400861 version->date = drmMalloc(version->date_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000862 if (version->desc_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400863 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000864
Keith Packard8b9ab102008-06-13 16:03:22 -0700865 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400866 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
867 drmFreeKernelVersion(version);
868 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000869 }
870
Adam Jackson22e41ef2006-02-20 23:09:00 +0000871 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000872 if (version->name_len) version->name[version->name_len] = '\0';
873 if (version->date_len) version->date[version->date_len] = '\0';
874 if (version->desc_len) version->desc[version->desc_len] = '\0';
875
Daryll Straussb3a57661999-12-05 01:19:48 +0000876 retval = drmMalloc(sizeof(*retval));
877 drmCopyVersion(retval, version);
878 drmFreeKernelVersion(version);
879 return retval;
880}
881
Jens Owen3903e5a2002-04-09 21:54:56 +0000882
Jose Fonsecad2443b22003-05-27 00:37:33 +0000883/**
884 * Get version information for the DRM user space library.
Jan Vesely50d3c852016-06-30 14:22:52 -0400885 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000886 * This version number is driver independent.
Jan Vesely50d3c852016-06-30 14:22:52 -0400887 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000888 * \param fd file descriptor.
889 *
890 * \return version information.
Jan Vesely50d3c852016-06-30 14:22:52 -0400891 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000892 * \internal
893 * This function allocates and fills a drm_version structure with a hard coded
894 * version number.
895 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700896drm_public drmVersionPtr drmGetLibVersion(int fd)
Jens Owen3903e5a2002-04-09 21:54:56 +0000897{
898 drm_version_t *version = drmMalloc(sizeof(*version));
899
900 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100901 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000902 * revision 1.0.x = original DRM interface with no drmGetLibVersion
903 * entry point and many drm<Device> extensions
904 * revision 1.1.x = added drmCommand entry points for device extensions
905 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000906 * revision 1.2.x = added drmSetInterfaceVersion
907 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100908 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000909 */
Dave Airlie79038752006-11-08 15:08:09 +1100910 version->version_major = 1;
911 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000912 version->version_patchlevel = 0;
913
914 return (drmVersionPtr)version;
915}
916
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700917drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000918{
Jan Vesely50d3c852016-06-30 14:22:52 -0400919 struct drm_get_cap cap;
920 int ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000921
Jan Vesely50d3c852016-06-30 14:22:52 -0400922 memclear(cap);
923 cap.capability = capability;
Daniel Vetterfd387942015-02-11 12:41:04 +0100924
Jan Vesely50d3c852016-06-30 14:22:52 -0400925 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
926 if (ret)
927 return ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000928
Jan Vesely50d3c852016-06-30 14:22:52 -0400929 *value = cap.value;
930 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000931}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000932
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700933drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100934{
Jan Vesely50d3c852016-06-30 14:22:52 -0400935 struct drm_set_client_cap cap;
Daniel Vetterfd387942015-02-11 12:41:04 +0100936
Jan Vesely50d3c852016-06-30 14:22:52 -0400937 memclear(cap);
938 cap.capability = capability;
939 cap.value = value;
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100940
Jan Vesely50d3c852016-06-30 14:22:52 -0400941 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100942}
943
Jose Fonsecad2443b22003-05-27 00:37:33 +0000944/**
945 * Free the bus ID information.
946 *
947 * \param busid bus ID information string as given by drmGetBusid().
948 *
949 * \internal
950 * This function is just frees the memory pointed by \p busid.
951 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700952drm_public void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000953{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000954 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000955}
956
Jose Fonsecad2443b22003-05-27 00:37:33 +0000957
958/**
959 * Get the bus ID of the device.
960 *
961 * \param fd file descriptor.
962 *
963 * \return bus ID string.
964 *
965 * \internal
966 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
967 * get the string length and data, passing the arguments in a drm_unique
968 * structure.
969 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700970drm_public char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000971{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000972 drm_unique_t u;
973
Daniel Vetterfd387942015-02-11 12:41:04 +0100974 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000975
Keith Packard8b9ab102008-06-13 16:03:22 -0700976 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Jan Vesely50d3c852016-06-30 14:22:52 -0400977 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000978 u.unique = drmMalloc(u.unique_len + 1);
Seung-Woo Kim7b806e82017-03-27 11:09:29 +0900979 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
980 drmFree(u.unique);
Jan Vesely50d3c852016-06-30 14:22:52 -0400981 return NULL;
Seung-Woo Kim7b806e82017-03-27 11:09:29 +0900982 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000983 u.unique[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +0000984
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000985 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +0000986}
987
Jose Fonsecad2443b22003-05-27 00:37:33 +0000988
989/**
990 * Set the bus ID of the device.
991 *
992 * \param fd file descriptor.
993 * \param busid bus ID string.
994 *
995 * \return zero on success, negative on failure.
996 *
997 * \internal
998 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
999 * the arguments in a drm_unique structure.
1000 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001001drm_public int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +00001002{
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001003 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +00001004
Daniel Vetterfd387942015-02-11 12:41:04 +01001005 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001006 u.unique = (char *)busid;
1007 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +00001008
Keith Packard8b9ab102008-06-13 16:03:22 -07001009 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001010 return -errno;
David Dawes56bd9c22001-07-30 19:59:39 +00001011 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001012 return 0;
1013}
1014
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001015drm_public int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001016{
1017 drm_auth_t auth;
1018
Daniel Vetterfd387942015-02-11 12:41:04 +01001019 memclear(auth);
1020
Daryll Straussb3a57661999-12-05 01:19:48 +00001021 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001022 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Jan Vesely50d3c852016-06-30 14:22:52 -04001023 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001024 *magic = auth.magic;
1025 return 0;
1026}
1027
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001028drm_public int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001029{
1030 drm_auth_t auth;
1031
Daniel Vetterfd387942015-02-11 12:41:04 +01001032 memclear(auth);
Daryll Straussb3a57661999-12-05 01:19:48 +00001033 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -07001034 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Jan Vesely50d3c852016-06-30 14:22:52 -04001035 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001036 return 0;
1037}
1038
Jose Fonsecad2443b22003-05-27 00:37:33 +00001039/**
1040 * Specifies a range of memory that is available for mapping by a
1041 * non-root process.
1042 *
1043 * \param fd file descriptor.
1044 * \param offset usually the physical address. The actual meaning depends of
1045 * the \p type parameter. See below.
1046 * \param size of the memory in bytes.
1047 * \param type type of the memory to be mapped.
1048 * \param flags combination of several flags to modify the function actions.
1049 * \param handle will be set to a value that may be used as the offset
1050 * parameter for mmap().
Jan Vesely50d3c852016-06-30 14:22:52 -04001051 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001052 * \return zero on success or a negative value on error.
1053 *
1054 * \par Mapping the frame buffer
1055 * For the frame buffer
1056 * - \p offset will be the physical address of the start of the frame buffer,
1057 * - \p size will be the size of the frame buffer in bytes, and
1058 * - \p type will be DRM_FRAME_BUFFER.
1059 *
1060 * \par
1061 * The area mapped will be uncached. If MTRR support is available in the
Jan Vesely50d3c852016-06-30 14:22:52 -04001062 * kernel, the frame buffer area will be set to write combining.
Jose Fonsecad2443b22003-05-27 00:37:33 +00001063 *
1064 * \par Mapping the MMIO register area
1065 * For the MMIO register area,
1066 * - \p offset will be the physical address of the start of the register area,
1067 * - \p size will be the size of the register area bytes, and
1068 * - \p type will be DRM_REGISTERS.
1069 * \par
Jan Vesely50d3c852016-06-30 14:22:52 -04001070 * The area mapped will be uncached.
1071 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001072 * \par Mapping the SAREA
1073 * For the SAREA,
1074 * - \p offset will be ignored and should be set to zero,
1075 * - \p size will be the desired size of the SAREA in bytes,
1076 * - \p type will be DRM_SHM.
Jan Vesely50d3c852016-06-30 14:22:52 -04001077 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001078 * \par
1079 * A shared memory area of the requested size will be created and locked in
1080 * kernel memory. This area may be mapped into client-space by using the handle
Jan Vesely50d3c852016-06-30 14:22:52 -04001081 * returned.
1082 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001083 * \note May only be called by root.
1084 *
1085 * \internal
1086 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1087 * the arguments in a drm_map structure.
1088 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001089drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1090 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001091{
1092 drm_map_t map;
1093
Daniel Vetterfd387942015-02-11 12:41:04 +01001094 memclear(map);
Daryll Straussb3a57661999-12-05 01:19:48 +00001095 map.offset = offset;
1096 map.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001097 map.type = type;
1098 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -07001099 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04001100 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06001101 if (handle)
Jan Vesely50d3c852016-06-30 14:22:52 -04001102 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001103 return 0;
1104}
1105
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001106drm_public int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00001107{
1108 drm_map_t map;
1109
Daniel Vetterfd387942015-02-11 12:41:04 +01001110 memclear(map);
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001111 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001112
Keith Packard8b9ab102008-06-13 16:03:22 -07001113 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04001114 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001115 return 0;
1116}
1117
Jose Fonsecad2443b22003-05-27 00:37:33 +00001118/**
1119 * Make buffers available for DMA transfers.
Jan Vesely50d3c852016-06-30 14:22:52 -04001120 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001121 * \param fd file descriptor.
1122 * \param count number of buffers.
1123 * \param size size of each buffer.
1124 * \param flags buffer allocation flags.
Jan Vesely50d3c852016-06-30 14:22:52 -04001125 * \param agp_offset offset in the AGP aperture
Jose Fonsecad2443b22003-05-27 00:37:33 +00001126 *
1127 * \return number of buffers allocated, negative on error.
1128 *
1129 * \internal
1130 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1131 *
1132 * \sa drm_buf_desc.
1133 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001134drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1135 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001136{
1137 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001138
Daniel Vetterfd387942015-02-11 12:41:04 +01001139 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001140 request.count = count;
1141 request.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001142 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001143 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001144
Keith Packard8b9ab102008-06-13 16:03:22 -07001145 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Jan Vesely50d3c852016-06-30 14:22:52 -04001146 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001147 return request.count;
1148}
1149
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001150drm_public int drmMarkBufs(int fd, double low, double high)
Daryll Straussb3a57661999-12-05 01:19:48 +00001151{
1152 drm_buf_info_t info;
1153 int i;
1154
Daniel Vetterfd387942015-02-11 12:41:04 +01001155 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001156
Keith Packard8b9ab102008-06-13 16:03:22 -07001157 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Jan Vesely50d3c852016-06-30 14:22:52 -04001158 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001159
Brianccd7b6e2007-05-29 14:54:00 -06001160 if (!info.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001161 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001162
Daryll Straussb3a57661999-12-05 01:19:48 +00001163 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
Jan Vesely50d3c852016-06-30 14:22:52 -04001164 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001165
Keith Packard8b9ab102008-06-13 16:03:22 -07001166 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001167 int retval = -errno;
1168 drmFree(info.list);
1169 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001170 }
Gareth Hughes36047532001-02-15 08:12:14 +00001171
Daryll Straussb3a57661999-12-05 01:19:48 +00001172 for (i = 0; i < info.count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001173 info.list[i].low_mark = low * info.list[i].count;
1174 info.list[i].high_mark = high * info.list[i].count;
1175 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1176 int retval = -errno;
1177 drmFree(info.list);
1178 return retval;
1179 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001180 }
1181 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001182
Daryll Straussb3a57661999-12-05 01:19:48 +00001183 return 0;
1184}
1185
Jose Fonsecad2443b22003-05-27 00:37:33 +00001186/**
1187 * Free buffers.
1188 *
1189 * \param fd file descriptor.
1190 * \param count number of buffers to free.
1191 * \param list list of buffers to be freed.
1192 *
1193 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001194 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001195 * \note This function is primarily used for debugging.
Jan Vesely50d3c852016-06-30 14:22:52 -04001196 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001197 * \internal
1198 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1199 * the arguments in a drm_buf_free structure.
1200 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001201drm_public int drmFreeBufs(int fd, int count, int *list)
Daryll Straussb3a57661999-12-05 01:19:48 +00001202{
1203 drm_buf_free_t request;
1204
Daniel Vetterfd387942015-02-11 12:41:04 +01001205 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001206 request.count = count;
1207 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001208 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Jan Vesely50d3c852016-06-30 14:22:52 -04001209 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001210 return 0;
1211}
1212
Jose Fonsecad2443b22003-05-27 00:37:33 +00001213
1214/**
1215 * Close the device.
1216 *
1217 * \param fd file descriptor.
1218 *
1219 * \internal
1220 * This function closes the file descriptor.
1221 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001222drm_public int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001223{
1224 unsigned long key = drmGetKeyFromFd(fd);
1225 drmHashEntry *entry = drmGetEntry(fd);
1226
1227 drmHashDestroy(entry->tagTable);
1228 entry->fd = 0;
1229 entry->f = NULL;
1230 entry->tagTable = NULL;
1231
1232 drmHashDelete(drmHashTable, key);
1233 drmFree(entry);
1234
1235 return close(fd);
1236}
1237
Jose Fonsecad2443b22003-05-27 00:37:33 +00001238
1239/**
1240 * Map a region of memory.
1241 *
1242 * \param fd file descriptor.
1243 * \param handle handle returned by drmAddMap().
1244 * \param size size in bytes. Must match the size used by drmAddMap().
1245 * \param address will contain the user-space virtual address where the mapping
1246 * begins.
1247 *
1248 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001249 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001250 * \internal
1251 * This function is a wrapper for mmap().
1252 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001253drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1254 drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001255{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001256 static unsigned long pagesize_mask = 0;
1257
Brianccd7b6e2007-05-29 14:54:00 -06001258 if (fd < 0)
Jan Vesely50d3c852016-06-30 14:22:52 -04001259 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001260
1261 if (!pagesize_mask)
Jan Vesely50d3c852016-06-30 14:22:52 -04001262 pagesize_mask = getpagesize() - 1;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001263
1264 size = (size + pagesize_mask) & ~pagesize_mask;
1265
Emil Velikovfaf51d52014-09-07 20:03:05 +01001266 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001267 if (*address == MAP_FAILED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001268 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001269 return 0;
1270}
1271
Jose Fonsecad2443b22003-05-27 00:37:33 +00001272
1273/**
1274 * Unmap mappings obtained with drmMap().
1275 *
1276 * \param address address as given by drmMap().
1277 * \param size size in bytes. Must match the size used by drmMap().
Jan Vesely50d3c852016-06-30 14:22:52 -04001278 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001279 * \return zero on success, or a negative value on failure.
1280 *
1281 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001282 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001283 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001284drm_public int drmUnmap(drmAddress address, drmSize size)
Daryll Straussb3a57661999-12-05 01:19:48 +00001285{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001286 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001287}
1288
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001289drm_public drmBufInfoPtr drmGetBufInfo(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001290{
1291 drm_buf_info_t info;
1292 drmBufInfoPtr retval;
1293 int i;
1294
Daniel Vetterfd387942015-02-11 12:41:04 +01001295 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001296
Keith Packard8b9ab102008-06-13 16:03:22 -07001297 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Jan Vesely50d3c852016-06-30 14:22:52 -04001298 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001299
1300 if (info.count) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001301 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1302 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001303
Jan Vesely50d3c852016-06-30 14:22:52 -04001304 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1305 drmFree(info.list);
1306 return NULL;
1307 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001308
Jan Vesely50d3c852016-06-30 14:22:52 -04001309 retval = drmMalloc(sizeof(*retval));
1310 retval->count = info.count;
1311 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1312 for (i = 0; i < info.count; i++) {
1313 retval->list[i].count = info.list[i].count;
1314 retval->list[i].size = info.list[i].size;
1315 retval->list[i].low_mark = info.list[i].low_mark;
1316 retval->list[i].high_mark = info.list[i].high_mark;
1317 }
1318 drmFree(info.list);
1319 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001320 }
1321 return NULL;
1322}
1323
Jose Fonsecad2443b22003-05-27 00:37:33 +00001324/**
1325 * Map all DMA buffers into client-virtual space.
1326 *
1327 * \param fd file descriptor.
1328 *
1329 * \return a pointer to a ::drmBufMap structure.
1330 *
1331 * \note The client may not use these buffers until obtaining buffer indices
1332 * with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001333 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001334 * \internal
1335 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1336 * information about the buffers in a drm_buf_map structure into the
1337 * client-visible data structures.
Jan Vesely50d3c852016-06-30 14:22:52 -04001338 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001339drm_public drmBufMapPtr drmMapBufs(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001340{
1341 drm_buf_map_t bufs;
1342 drmBufMapPtr retval;
1343 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001344
Daniel Vetterfd387942015-02-11 12:41:04 +01001345 memclear(bufs);
Keith Packard8b9ab102008-06-13 16:03:22 -07001346 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Jan Vesely50d3c852016-06-30 14:22:52 -04001347 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001348
Brianccd7b6e2007-05-29 14:54:00 -06001349 if (!bufs.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001350 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001351
Jan Vesely50d3c852016-06-30 14:22:52 -04001352 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1353 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001354
Jan Vesely50d3c852016-06-30 14:22:52 -04001355 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1356 drmFree(bufs.list);
1357 return NULL;
1358 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001359
Jan Vesely50d3c852016-06-30 14:22:52 -04001360 retval = drmMalloc(sizeof(*retval));
1361 retval->count = bufs.count;
1362 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1363 for (i = 0; i < bufs.count; i++) {
1364 retval->list[i].idx = bufs.list[i].idx;
1365 retval->list[i].total = bufs.list[i].total;
1366 retval->list[i].used = 0;
1367 retval->list[i].address = bufs.list[i].address;
1368 }
Jon Smirl8696e712004-07-07 04:36:36 +00001369
Jan Vesely50d3c852016-06-30 14:22:52 -04001370 drmFree(bufs.list);
1371 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001372}
1373
Jose Fonsecad2443b22003-05-27 00:37:33 +00001374
1375/**
1376 * Unmap buffers allocated with drmMapBufs().
1377 *
1378 * \return zero on success, or negative value on failure.
1379 *
1380 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001381 * Calls munmap() for every buffer stored in \p bufs and frees the
1382 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001383 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001384drm_public int drmUnmapBufs(drmBufMapPtr bufs)
Daryll Straussb3a57661999-12-05 01:19:48 +00001385{
1386 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001387
Daryll Straussb3a57661999-12-05 01:19:48 +00001388 for (i = 0; i < bufs->count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001389 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001390 }
Jon Smirl8696e712004-07-07 04:36:36 +00001391
1392 drmFree(bufs->list);
1393 drmFree(bufs);
Daryll Straussb3a57661999-12-05 01:19:48 +00001394 return 0;
1395}
1396
Jose Fonsecad2443b22003-05-27 00:37:33 +00001397
Jan Vesely50d3c852016-06-30 14:22:52 -04001398#define DRM_DMA_RETRY 16
Gareth Hughes36047532001-02-15 08:12:14 +00001399
Jose Fonsecad2443b22003-05-27 00:37:33 +00001400/**
1401 * Reserve DMA buffers.
1402 *
1403 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001404 * \param request
1405 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001406 * \return zero on success, or a negative value on failure.
1407 *
1408 * \internal
1409 * Assemble the arguments into a drm_dma structure and keeps issuing the
1410 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1411 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001412drm_public int drmDMA(int fd, drmDMAReqPtr request)
Daryll Straussb3a57661999-12-05 01:19:48 +00001413{
1414 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001415 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001416
Daryll Straussb3a57661999-12-05 01:19:48 +00001417 dma.context = request->context;
1418 dma.send_count = request->send_count;
1419 dma.send_indices = request->send_list;
1420 dma.send_sizes = request->send_sizes;
1421 dma.flags = request->flags;
1422 dma.request_count = request->request_count;
1423 dma.request_size = request->request_size;
1424 dma.request_indices = request->request_list;
1425 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001426 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001427
1428 do {
Jan Vesely50d3c852016-06-30 14:22:52 -04001429 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
Gareth Hughes36047532001-02-15 08:12:14 +00001430 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1431
1432 if ( ret == 0 ) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001433 request->granted_count = dma.granted_count;
1434 return 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001435 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -04001436 return -errno;
Gareth Hughes36047532001-02-15 08:12:14 +00001437 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001438}
1439
Jose Fonsecad2443b22003-05-27 00:37:33 +00001440
1441/**
1442 * Obtain heavyweight hardware lock.
1443 *
1444 * \param fd file descriptor.
1445 * \param context context.
Eric Engestrom360292c2018-12-19 14:55:45 +00001446 * \param flags flags that determine the state of the hardware when the function
Jose Fonsecad2443b22003-05-27 00:37:33 +00001447 * returns.
Jan Vesely50d3c852016-06-30 14:22:52 -04001448 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001449 * \return always zero.
Jan Vesely50d3c852016-06-30 14:22:52 -04001450 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001451 * \internal
1452 * This function translates the arguments into a drm_lock structure and issue
1453 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1454 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001455drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001456{
1457 drm_lock_t lock;
1458
Daniel Vetterfd387942015-02-11 12:41:04 +01001459 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001460 lock.context = context;
1461 lock.flags = 0;
1462 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1463 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1464 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1465 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1466 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1467 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001468
Keith Packard8b9ab102008-06-13 16:03:22 -07001469 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04001470 ;
Daryll Straussb3a57661999-12-05 01:19:48 +00001471 return 0;
1472}
1473
Jose Fonsecad2443b22003-05-27 00:37:33 +00001474/**
1475 * Release the hardware lock.
1476 *
1477 * \param fd file descriptor.
1478 * \param context context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001479 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001480 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001481 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001482 * \internal
1483 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1484 * argument in a drm_lock structure.
1485 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001486drm_public int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001487{
1488 drm_lock_t lock;
1489
Daniel Vetterfd387942015-02-11 12:41:04 +01001490 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001491 lock.context = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001492 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001493}
1494
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001495drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001496{
1497 drm_ctx_res_t res;
1498 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001499 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001500 int i;
1501
Daniel Vetterfd387942015-02-11 12:41:04 +01001502 memclear(res);
Keith Packard8b9ab102008-06-13 16:03:22 -07001503 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Jan Vesely50d3c852016-06-30 14:22:52 -04001504 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001505
Brianccd7b6e2007-05-29 14:54:00 -06001506 if (!res.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001507 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001508
Brianccd7b6e2007-05-29 14:54:00 -06001509 if (!(list = drmMalloc(res.count * sizeof(*list))))
Jan Vesely50d3c852016-06-30 14:22:52 -04001510 return NULL;
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001511 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1512 goto err_free_list;
Daryll Straussb3a57661999-12-05 01:19:48 +00001513
1514 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001515 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001516 goto err_free_context;
Daryll Straussb3a57661999-12-05 01:19:48 +00001517
Brianccd7b6e2007-05-29 14:54:00 -06001518 for (i = 0; i < res.count; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04001519 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001520 drmFree(list);
1521
1522 *count = res.count;
1523 return retval;
Seung-Woo Kim7b806e82017-03-27 11:09:29 +09001524
1525err_free_list:
1526 drmFree(list);
1527err_free_context:
1528 drmFree(retval);
1529 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001530}
1531
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001532drm_public void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001533{
1534 drmFree(pt);
1535}
1536
Jose Fonsecad2443b22003-05-27 00:37:33 +00001537/**
1538 * Create context.
1539 *
1540 * Used by the X server during GLXContext initialization. This causes
1541 * per-context kernel-level resources to be allocated.
1542 *
1543 * \param fd file descriptor.
1544 * \param handle is set on success. To be used by the client when requesting DMA
1545 * dispatch with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001546 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001547 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001548 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001549 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001550 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001551 * \internal
1552 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1553 * argument in a drm_ctx structure.
1554 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001555drm_public int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001556{
1557 drm_ctx_t ctx;
1558
Daniel Vetterfd387942015-02-11 12:41:04 +01001559 memclear(ctx);
Keith Packard8b9ab102008-06-13 16:03:22 -07001560 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001561 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001562 *handle = ctx.handle;
1563 return 0;
1564}
1565
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001566drm_public int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001567{
1568 drm_ctx_t ctx;
1569
Daniel Vetterfd387942015-02-11 12:41:04 +01001570 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001571 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001572 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001573 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001574 return 0;
1575}
1576
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001577drm_public int drmSetContextFlags(int fd, drm_context_t context,
1578 drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001579{
1580 drm_ctx_t ctx;
1581
Adam Jackson22e41ef2006-02-20 23:09:00 +00001582 /*
1583 * Context preserving means that no context switches are done between DMA
1584 * buffers from one context and the next. This is suitable for use in the
1585 * X server (which promises to maintain hardware context), or in the
1586 * client-side library when buffers are swapped on behalf of two threads.
1587 */
Daniel Vetterfd387942015-02-11 12:41:04 +01001588 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001589 ctx.handle = context;
Brianccd7b6e2007-05-29 14:54:00 -06001590 if (flags & DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001591 ctx.flags |= _DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001592 if (flags & DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001593 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001594 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001595 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001596 return 0;
1597}
1598
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001599drm_public int drmGetContextFlags(int fd, drm_context_t context,
1600 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001601{
1602 drm_ctx_t ctx;
1603
Daniel Vetterfd387942015-02-11 12:41:04 +01001604 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001605 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001606 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001607 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001608 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001609 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001610 *flags |= DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001611 if (ctx.flags & _DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001612 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001613 return 0;
1614}
Gareth Hughes36047532001-02-15 08:12:14 +00001615
Jose Fonsecad2443b22003-05-27 00:37:33 +00001616/**
1617 * Destroy context.
1618 *
1619 * Free any kernel-level resources allocated with drmCreateContext() associated
1620 * with the context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001621 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001622 * \param fd file descriptor.
1623 * \param handle handle given by drmCreateContext().
Jan Vesely50d3c852016-06-30 14:22:52 -04001624 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001625 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001626 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001627 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001628 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001629 * \internal
1630 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1631 * argument in a drm_ctx structure.
1632 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001633drm_public int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001634{
1635 drm_ctx_t ctx;
Daniel Vetterfd387942015-02-11 12:41:04 +01001636
1637 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001638 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001639 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001640 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001641 return 0;
1642}
1643
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001644drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001645{
1646 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001647
1648 memclear(draw);
Keith Packard8b9ab102008-06-13 16:03:22 -07001649 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001650 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001651 *handle = draw.handle;
1652 return 0;
1653}
1654
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001655drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001656{
1657 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001658
1659 memclear(draw);
Daryll Straussb3a57661999-12-05 01:19:48 +00001660 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001661 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001662 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001663 return 0;
1664}
1665
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001666drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1667 drm_drawable_info_type_t type,
1668 unsigned int num, void *data)
Michel Dänzer9810ec22006-08-22 16:40:07 +02001669{
1670 drm_update_draw_t update;
1671
Daniel Vetterfd387942015-02-11 12:41:04 +01001672 memclear(update);
Michel Dänzer9810ec22006-08-22 16:40:07 +02001673 update.handle = handle;
1674 update.type = type;
1675 update.num = num;
1676 update.data = (unsigned long long)(unsigned long)data;
1677
Keith Packard8b9ab102008-06-13 16:03:22 -07001678 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Jan Vesely50d3c852016-06-30 14:22:52 -04001679 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001680
1681 return 0;
1682}
1683
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001684drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1685 uint64_t *ns)
Keith Packardd4331dd2017-07-01 00:43:15 -07001686{
1687 struct drm_crtc_get_sequence get_seq;
1688 int ret;
1689
1690 memclear(get_seq);
1691 get_seq.crtc_id = crtcId;
1692 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1693 if (ret)
1694 return ret;
1695
1696 if (sequence)
1697 *sequence = get_seq.sequence;
1698 if (ns)
1699 *ns = get_seq.sequence_ns;
1700 return 0;
1701}
1702
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001703drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1704 uint64_t sequence,
1705 uint64_t *sequence_queued,
1706 uint64_t user_data)
Keith Packardd4331dd2017-07-01 00:43:15 -07001707{
1708 struct drm_crtc_queue_sequence queue_seq;
1709 int ret;
1710
1711 memclear(queue_seq);
1712 queue_seq.crtc_id = crtcId;
1713 queue_seq.flags = flags;
1714 queue_seq.sequence = sequence;
1715 queue_seq.user_data = user_data;
1716
1717 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1718 if (ret == 0 && sequence_queued)
1719 *sequence_queued = queue_seq.sequence;
1720
1721 return ret;
1722}
1723
Jose Fonsecad2443b22003-05-27 00:37:33 +00001724/**
1725 * Acquire the AGP device.
1726 *
1727 * Must be called before any of the other AGP related calls.
1728 *
1729 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001730 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001731 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001732 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001733 * \internal
1734 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1735 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001736drm_public int drmAgpAcquire(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001737{
Keith Packard8b9ab102008-06-13 16:03:22 -07001738 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001739 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001740 return 0;
1741}
1742
Jose Fonsecad2443b22003-05-27 00:37:33 +00001743
1744/**
1745 * Release the AGP device.
1746 *
1747 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001748 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001749 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001750 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001751 * \internal
1752 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1753 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001754drm_public int drmAgpRelease(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001755{
Keith Packard8b9ab102008-06-13 16:03:22 -07001756 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001757 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001758 return 0;
1759}
1760
Jose Fonsecad2443b22003-05-27 00:37:33 +00001761
1762/**
1763 * Set the AGP mode.
1764 *
1765 * \param fd file descriptor.
1766 * \param mode AGP mode.
Jan Vesely50d3c852016-06-30 14:22:52 -04001767 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001768 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001769 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001770 * \internal
1771 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1772 * argument in a drm_agp_mode structure.
1773 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001774drm_public int drmAgpEnable(int fd, unsigned long mode)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001775{
1776 drm_agp_mode_t m;
1777
Connor Behan14900552015-03-24 13:53:51 -04001778 memclear(m);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001779 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001780 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Jan Vesely50d3c852016-06-30 14:22:52 -04001781 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001782 return 0;
1783}
1784
Jose Fonsecad2443b22003-05-27 00:37:33 +00001785
1786/**
1787 * Allocate a chunk of AGP memory.
1788 *
1789 * \param fd file descriptor.
1790 * \param size requested memory size in bytes. Will be rounded to page boundary.
1791 * \param type type of memory to allocate.
1792 * \param address if not zero, will be set to the physical address of the
1793 * allocated memory.
1794 * \param handle on success will be set to a handle of the allocated memory.
Jan Vesely50d3c852016-06-30 14:22:52 -04001795 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001796 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001797 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001798 * \internal
1799 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1800 * arguments in a drm_agp_buffer structure.
1801 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001802drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1803 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001804{
1805 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001806
Daniel Vetterfd387942015-02-11 12:41:04 +01001807 memclear(b);
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001808 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001809 b.size = size;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001810 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001811 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001812 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06001813 if (address != 0UL)
Jan Vesely50d3c852016-06-30 14:22:52 -04001814 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001815 *handle = b.handle;
1816 return 0;
1817}
1818
Jose Fonsecad2443b22003-05-27 00:37:33 +00001819
1820/**
1821 * Free a chunk of AGP memory.
1822 *
1823 * \param fd file descriptor.
1824 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001825 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001826 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001827 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001828 * \internal
1829 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1830 * argument in a drm_agp_buffer structure.
1831 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001832drm_public int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001833{
1834 drm_agp_buffer_t b;
1835
Daniel Vetterfd387942015-02-11 12:41:04 +01001836 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001837 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001838 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001839 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001840 return 0;
1841}
1842
Jose Fonsecad2443b22003-05-27 00:37:33 +00001843
1844/**
1845 * Bind a chunk of AGP memory.
1846 *
1847 * \param fd file descriptor.
1848 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1849 * \param offset offset in bytes. It will round to page boundary.
Jan Vesely50d3c852016-06-30 14:22:52 -04001850 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001851 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001852 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001853 * \internal
1854 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1855 * argument in a drm_agp_binding structure.
1856 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001857drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001858{
1859 drm_agp_binding_t b;
1860
Daniel Vetterfd387942015-02-11 12:41:04 +01001861 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001862 b.handle = handle;
1863 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001864 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001865 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001866 return 0;
1867}
1868
Jose Fonsecad2443b22003-05-27 00:37:33 +00001869
1870/**
1871 * Unbind a chunk of AGP memory.
1872 *
1873 * \param fd file descriptor.
1874 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001875 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001876 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001877 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001878 * \internal
1879 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1880 * the argument in a drm_agp_binding structure.
1881 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001882drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001883{
1884 drm_agp_binding_t b;
1885
Daniel Vetterfd387942015-02-11 12:41:04 +01001886 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001887 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001888 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001889 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001890 return 0;
1891}
1892
Jose Fonsecad2443b22003-05-27 00:37:33 +00001893
1894/**
1895 * Get AGP driver major version number.
1896 *
1897 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001898 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001899 * \return major version number on success, or a negative value on failure..
Jan Vesely50d3c852016-06-30 14:22:52 -04001900 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001901 * \internal
1902 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1903 * necessary information in a drm_agp_info structure.
1904 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001905drm_public int drmAgpVersionMajor(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001906{
1907 drm_agp_info_t i;
1908
Daniel Vetterfd387942015-02-11 12:41:04 +01001909 memclear(i);
1910
Keith Packard8b9ab102008-06-13 16:03:22 -07001911 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001912 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001913 return i.agp_version_major;
1914}
1915
Jose Fonsecad2443b22003-05-27 00:37:33 +00001916
1917/**
1918 * Get AGP driver minor version number.
1919 *
1920 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001921 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001922 * \return minor version number on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001923 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001924 * \internal
1925 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1926 * necessary information in a drm_agp_info structure.
1927 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001928drm_public int drmAgpVersionMinor(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001929{
1930 drm_agp_info_t i;
1931
Daniel Vetterfd387942015-02-11 12:41:04 +01001932 memclear(i);
1933
Keith Packard8b9ab102008-06-13 16:03:22 -07001934 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001935 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001936 return i.agp_version_minor;
1937}
1938
Jose Fonsecad2443b22003-05-27 00:37:33 +00001939
1940/**
1941 * Get AGP mode.
1942 *
1943 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001944 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001945 * \return mode on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001946 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001947 * \internal
1948 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1949 * necessary information in a drm_agp_info structure.
1950 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001951drm_public unsigned long drmAgpGetMode(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001952{
1953 drm_agp_info_t i;
1954
Daniel Vetterfd387942015-02-11 12:41:04 +01001955 memclear(i);
1956
Keith Packard8b9ab102008-06-13 16:03:22 -07001957 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001958 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001959 return i.mode;
1960}
1961
Jose Fonsecad2443b22003-05-27 00:37:33 +00001962
1963/**
1964 * Get AGP aperture base.
1965 *
1966 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001967 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001968 * \return aperture base on success, zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001969 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001970 * \internal
1971 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1972 * necessary information in a drm_agp_info structure.
1973 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001974drm_public unsigned long drmAgpBase(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001975{
1976 drm_agp_info_t i;
1977
Daniel Vetterfd387942015-02-11 12:41:04 +01001978 memclear(i);
1979
Keith Packard8b9ab102008-06-13 16:03:22 -07001980 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001981 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001982 return i.aperture_base;
1983}
1984
Jose Fonsecad2443b22003-05-27 00:37:33 +00001985
1986/**
1987 * Get AGP aperture size.
1988 *
1989 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001990 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001991 * \return aperture size on success, zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001992 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001993 * \internal
1994 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1995 * necessary information in a drm_agp_info structure.
1996 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001997drm_public unsigned long drmAgpSize(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001998{
1999 drm_agp_info_t i;
2000
Daniel Vetterfd387942015-02-11 12:41:04 +01002001 memclear(i);
2002
Keith Packard8b9ab102008-06-13 16:03:22 -07002003 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002004 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002005 return i.aperture_size;
2006}
2007
Jose Fonsecad2443b22003-05-27 00:37:33 +00002008
2009/**
2010 * Get used AGP memory.
2011 *
2012 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002013 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002014 * \return memory used on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002015 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002016 * \internal
2017 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2018 * necessary information in a drm_agp_info structure.
2019 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002020drm_public unsigned long drmAgpMemoryUsed(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002021{
2022 drm_agp_info_t i;
2023
Daniel Vetterfd387942015-02-11 12:41:04 +01002024 memclear(i);
2025
Keith Packard8b9ab102008-06-13 16:03:22 -07002026 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002027 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002028 return i.memory_used;
2029}
2030
Jose Fonsecad2443b22003-05-27 00:37:33 +00002031
2032/**
2033 * Get available AGP memory.
2034 *
2035 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002036 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002037 * \return memory available on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002038 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002039 * \internal
2040 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2041 * necessary information in a drm_agp_info structure.
2042 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002043drm_public unsigned long drmAgpMemoryAvail(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002044{
2045 drm_agp_info_t i;
2046
Daniel Vetterfd387942015-02-11 12:41:04 +01002047 memclear(i);
2048
Keith Packard8b9ab102008-06-13 16:03:22 -07002049 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002050 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002051 return i.memory_allowed;
2052}
2053
Jose Fonsecad2443b22003-05-27 00:37:33 +00002054
2055/**
2056 * Get hardware vendor ID.
2057 *
2058 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002059 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002060 * \return vendor ID on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002061 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002062 * \internal
2063 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2064 * necessary information in a drm_agp_info structure.
2065 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002066drm_public unsigned int drmAgpVendorId(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002067{
2068 drm_agp_info_t i;
2069
Daniel Vetterfd387942015-02-11 12:41:04 +01002070 memclear(i);
2071
Keith Packard8b9ab102008-06-13 16:03:22 -07002072 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002073 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002074 return i.id_vendor;
2075}
2076
Jose Fonsecad2443b22003-05-27 00:37:33 +00002077
2078/**
2079 * Get hardware device ID.
2080 *
2081 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002082 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002083 * \return zero on success, or zero on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002084 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002085 * \internal
2086 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2087 * necessary information in a drm_agp_info structure.
2088 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002089drm_public unsigned int drmAgpDeviceId(int fd)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002090{
2091 drm_agp_info_t i;
2092
Daniel Vetterfd387942015-02-11 12:41:04 +01002093 memclear(i);
2094
Keith Packard8b9ab102008-06-13 16:03:22 -07002095 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04002096 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002097 return i.id_device;
2098}
2099
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002100drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2101 drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002102{
2103 drm_scatter_gather_t sg;
2104
Daniel Vetterfd387942015-02-11 12:41:04 +01002105 memclear(sg);
2106
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002107 *handle = 0;
2108 sg.size = size;
Keith Packard8b9ab102008-06-13 16:03:22 -07002109 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002110 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002111 *handle = sg.handle;
2112 return 0;
2113}
2114
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002115drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002116{
2117 drm_scatter_gather_t sg;
2118
Daniel Vetterfd387942015-02-11 12:41:04 +01002119 memclear(sg);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002120 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07002121 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002122 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002123 return 0;
2124}
2125
Jose Fonsecad2443b22003-05-27 00:37:33 +00002126/**
2127 * Wait for VBLANK.
2128 *
2129 * \param fd file descriptor.
2130 * \param vbl pointer to a drmVBlank structure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002131 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002132 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002133 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002134 * \internal
2135 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2136 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002137drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002138{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002139 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002140 int ret;
2141
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002142 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2143 if (ret < 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002144 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2145 goto out;
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002146 }
2147 timeout.tv_sec++;
2148
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002149 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002150 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00002151 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08002152 if (ret && errno == EINTR) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002153 clock_gettime(CLOCK_MONOTONIC, &cur);
2154 /* Timeout after 1s */
2155 if (cur.tv_sec > timeout.tv_sec + 1 ||
2156 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2157 timeout.tv_nsec)) {
2158 errno = EBUSY;
2159 ret = -1;
2160 break;
2161 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002162 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002163 } while (ret && errno == EINTR);
2164
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002165out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002166 return ret;
2167}
2168
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002169drm_public int drmError(int err, const char *label)
Daryll Straussb3a57661999-12-05 01:19:48 +00002170{
2171 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002172 case DRM_ERR_NO_DEVICE:
Jan Vesely50d3c852016-06-30 14:22:52 -04002173 fprintf(stderr, "%s: no device\n", label);
2174 break;
Brianccd7b6e2007-05-29 14:54:00 -06002175 case DRM_ERR_NO_ACCESS:
Jan Vesely50d3c852016-06-30 14:22:52 -04002176 fprintf(stderr, "%s: no access\n", label);
2177 break;
Brianccd7b6e2007-05-29 14:54:00 -06002178 case DRM_ERR_NOT_ROOT:
Jan Vesely50d3c852016-06-30 14:22:52 -04002179 fprintf(stderr, "%s: not root\n", label);
2180 break;
Brianccd7b6e2007-05-29 14:54:00 -06002181 case DRM_ERR_INVALID:
Jan Vesely50d3c852016-06-30 14:22:52 -04002182 fprintf(stderr, "%s: invalid args\n", label);
2183 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002184 default:
Jan Vesely50d3c852016-06-30 14:22:52 -04002185 if (err < 0)
2186 err = -err;
2187 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2188 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002189 }
2190
2191 return 1;
2192}
2193
Jose Fonsecad2443b22003-05-27 00:37:33 +00002194/**
2195 * Install IRQ handler.
2196 *
2197 * \param fd file descriptor.
2198 * \param irq IRQ number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002199 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002200 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002201 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002202 * \internal
2203 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2204 * argument in a drm_control structure.
2205 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002206drm_public int drmCtlInstHandler(int fd, int irq)
Daryll Straussb3a57661999-12-05 01:19:48 +00002207{
2208 drm_control_t ctl;
2209
Daniel Vetterfd387942015-02-11 12:41:04 +01002210 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002211 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002212 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002213 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002214 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002215 return 0;
2216}
2217
Jose Fonsecad2443b22003-05-27 00:37:33 +00002218
2219/**
2220 * Uninstall IRQ handler.
2221 *
2222 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002223 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002224 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002225 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002226 * \internal
2227 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2228 * argument in a drm_control structure.
2229 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002230drm_public int drmCtlUninstHandler(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00002231{
2232 drm_control_t ctl;
2233
Daniel Vetterfd387942015-02-11 12:41:04 +01002234 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002235 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002236 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002237 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002238 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002239 return 0;
2240}
2241
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002242drm_public int drmFinish(int fd, int context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00002243{
2244 drm_lock_t lock;
2245
Daniel Vetterfd387942015-02-11 12:41:04 +01002246 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00002247 lock.context = context;
Daryll Straussb3a57661999-12-05 01:19:48 +00002248 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2249 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2250 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2251 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2252 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2253 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002254 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04002255 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002256 return 0;
2257}
2258
Jose Fonsecad2443b22003-05-27 00:37:33 +00002259/**
2260 * Get IRQ from bus ID.
2261 *
2262 * \param fd file descriptor.
2263 * \param busnum bus number.
2264 * \param devnum device number.
2265 * \param funcnum function number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002266 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002267 * \return IRQ number on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002268 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002269 * \internal
2270 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2271 * arguments in a drm_irq_busid structure.
2272 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002273drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2274 int funcnum)
Daryll Straussb3a57661999-12-05 01:19:48 +00002275{
2276 drm_irq_busid_t p;
2277
Daniel Vetterfd387942015-02-11 12:41:04 +01002278 memclear(p);
Daryll Straussb3a57661999-12-05 01:19:48 +00002279 p.busnum = busnum;
2280 p.devnum = devnum;
2281 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002282 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Jan Vesely50d3c852016-06-30 14:22:52 -04002283 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002284 return p.irq;
2285}
2286
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002287drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002288{
2289 drmHashEntry *entry = drmGetEntry(fd);
2290
2291 if (drmHashInsert(entry->tagTable, context, tag)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002292 drmHashDelete(entry->tagTable, context);
2293 drmHashInsert(entry->tagTable, context, tag);
Daryll Straussb3a57661999-12-05 01:19:48 +00002294 }
2295 return 0;
2296}
2297
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002298drm_public int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002299{
2300 drmHashEntry *entry = drmGetEntry(fd);
2301
2302 return drmHashDelete(entry->tagTable, context);
2303}
2304
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002305drm_public void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002306{
2307 drmHashEntry *entry = drmGetEntry(fd);
2308 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002309
Brianccd7b6e2007-05-29 14:54:00 -06002310 if (drmHashLookup(entry->tagTable, context, &value))
Jan Vesely50d3c852016-06-30 14:22:52 -04002311 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002312
2313 return value;
2314}
2315
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002316drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2317 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002318{
2319 drm_ctx_priv_map_t map;
2320
Daniel Vetterfd387942015-02-11 12:41:04 +01002321 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002322 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002323 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002324
Keith Packard8b9ab102008-06-13 16:03:22 -07002325 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002326 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002327 return 0;
2328}
2329
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002330drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2331 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002332{
2333 drm_ctx_priv_map_t map;
2334
Daniel Vetterfd387942015-02-11 12:41:04 +01002335 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002336 map.ctx_id = ctx_id;
2337
Keith Packard8b9ab102008-06-13 16:03:22 -07002338 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002339 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06002340 if (handle)
Jan Vesely50d3c852016-06-30 14:22:52 -04002341 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002342
2343 return 0;
2344}
2345
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002346drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2347 drmMapType *type, drmMapFlags *flags,
2348 drm_handle_t *handle, int *mtrr)
Rik Faith88dbee52001-02-28 09:27:44 +00002349{
2350 drm_map_t map;
2351
Daniel Vetterfd387942015-02-11 12:41:04 +01002352 memclear(map);
Rik Faith88dbee52001-02-28 09:27:44 +00002353 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002354 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002355 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002356 *offset = map.offset;
2357 *size = map.size;
2358 *type = map.type;
2359 *flags = map.flags;
2360 *handle = (unsigned long)map.handle;
2361 *mtrr = map.mtrr;
2362 return 0;
2363}
2364
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002365drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2366 unsigned long *magic, unsigned long *iocs)
Rik Faith88dbee52001-02-28 09:27:44 +00002367{
2368 drm_client_t client;
2369
Daniel Vetterfd387942015-02-11 12:41:04 +01002370 memclear(client);
Rik Faith88dbee52001-02-28 09:27:44 +00002371 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002372 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Jan Vesely50d3c852016-06-30 14:22:52 -04002373 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002374 *auth = client.auth;
2375 *pid = client.pid;
2376 *uid = client.uid;
2377 *magic = client.magic;
2378 *iocs = client.iocs;
2379 return 0;
2380}
2381
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002382drm_public int drmGetStats(int fd, drmStatsT *stats)
Rik Faith88dbee52001-02-28 09:27:44 +00002383{
2384 drm_stats_t s;
Jan Veselyde8532d2014-11-30 12:53:18 -05002385 unsigned i;
Rik Faith88dbee52001-02-28 09:27:44 +00002386
Daniel Vetterfd387942015-02-11 12:41:04 +01002387 memclear(s);
Keith Packard8b9ab102008-06-13 16:03:22 -07002388 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Jan Vesely50d3c852016-06-30 14:22:52 -04002389 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002390
2391 stats->count = 0;
2392 memset(stats, 0, sizeof(*stats));
2393 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
Jan Vesely50d3c852016-06-30 14:22:52 -04002394 return -1;
Rik Faith88dbee52001-02-28 09:27:44 +00002395
2396#define SET_VALUE \
2397 stats->data[i].long_format = "%-20.20s"; \
2398 stats->data[i].rate_format = "%8.8s"; \
2399 stats->data[i].isvalue = 1; \
2400 stats->data[i].verbose = 0
2401
2402#define SET_COUNT \
2403 stats->data[i].long_format = "%-20.20s"; \
2404 stats->data[i].rate_format = "%5.5s"; \
2405 stats->data[i].isvalue = 0; \
2406 stats->data[i].mult_names = "kgm"; \
2407 stats->data[i].mult = 1000; \
2408 stats->data[i].verbose = 0
2409
2410#define SET_BYTE \
2411 stats->data[i].long_format = "%-20.20s"; \
2412 stats->data[i].rate_format = "%5.5s"; \
2413 stats->data[i].isvalue = 0; \
2414 stats->data[i].mult_names = "KGM"; \
2415 stats->data[i].mult = 1024; \
2416 stats->data[i].verbose = 0
2417
2418
2419 stats->count = s.count;
2420 for (i = 0; i < s.count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002421 stats->data[i].value = s.data[i].value;
2422 switch (s.data[i].type) {
2423 case _DRM_STAT_LOCK:
2424 stats->data[i].long_name = "Lock";
2425 stats->data[i].rate_name = "Lock";
2426 SET_VALUE;
2427 break;
2428 case _DRM_STAT_OPENS:
2429 stats->data[i].long_name = "Opens";
2430 stats->data[i].rate_name = "O";
2431 SET_COUNT;
2432 stats->data[i].verbose = 1;
2433 break;
2434 case _DRM_STAT_CLOSES:
2435 stats->data[i].long_name = "Closes";
2436 stats->data[i].rate_name = "Lock";
2437 SET_COUNT;
2438 stats->data[i].verbose = 1;
2439 break;
2440 case _DRM_STAT_IOCTLS:
2441 stats->data[i].long_name = "Ioctls";
2442 stats->data[i].rate_name = "Ioc/s";
2443 SET_COUNT;
2444 break;
2445 case _DRM_STAT_LOCKS:
2446 stats->data[i].long_name = "Locks";
2447 stats->data[i].rate_name = "Lck/s";
2448 SET_COUNT;
2449 break;
2450 case _DRM_STAT_UNLOCKS:
2451 stats->data[i].long_name = "Unlocks";
2452 stats->data[i].rate_name = "Unl/s";
2453 SET_COUNT;
2454 break;
2455 case _DRM_STAT_IRQ:
2456 stats->data[i].long_name = "IRQs";
2457 stats->data[i].rate_name = "IRQ/s";
2458 SET_COUNT;
2459 break;
2460 case _DRM_STAT_PRIMARY:
2461 stats->data[i].long_name = "Primary Bytes";
2462 stats->data[i].rate_name = "PB/s";
2463 SET_BYTE;
2464 break;
2465 case _DRM_STAT_SECONDARY:
2466 stats->data[i].long_name = "Secondary Bytes";
2467 stats->data[i].rate_name = "SB/s";
2468 SET_BYTE;
2469 break;
2470 case _DRM_STAT_DMA:
2471 stats->data[i].long_name = "DMA";
2472 stats->data[i].rate_name = "DMA/s";
2473 SET_COUNT;
2474 break;
2475 case _DRM_STAT_SPECIAL:
2476 stats->data[i].long_name = "Special DMA";
2477 stats->data[i].rate_name = "dma/s";
2478 SET_COUNT;
2479 break;
2480 case _DRM_STAT_MISSED:
2481 stats->data[i].long_name = "Miss";
2482 stats->data[i].rate_name = "Ms/s";
2483 SET_COUNT;
2484 break;
2485 case _DRM_STAT_VALUE:
2486 stats->data[i].long_name = "Value";
2487 stats->data[i].rate_name = "Value";
2488 SET_VALUE;
2489 break;
2490 case _DRM_STAT_BYTE:
2491 stats->data[i].long_name = "Bytes";
2492 stats->data[i].rate_name = "B/s";
2493 SET_BYTE;
2494 break;
2495 case _DRM_STAT_COUNT:
2496 default:
2497 stats->data[i].long_name = "Count";
2498 stats->data[i].rate_name = "Cnt/s";
2499 SET_COUNT;
2500 break;
2501 }
Rik Faith88dbee52001-02-28 09:27:44 +00002502 }
2503 return 0;
2504}
2505
Jose Fonsecad2443b22003-05-27 00:37:33 +00002506/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002507 * Issue a set-version ioctl.
2508 *
2509 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002510 * \param drmCommandIndex command index
Eric Anholt06cb1322003-10-23 02:23:31 +00002511 * \param data source pointer of the data to be read and written.
2512 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002513 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002514 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002515 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002516 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002517 * It issues a read-write ioctl given by
Eric Anholt06cb1322003-10-23 02:23:31 +00002518 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2519 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002520drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002521{
2522 int retcode = 0;
2523 drm_set_version_t sv;
2524
Daniel Vetterfd387942015-02-11 12:41:04 +01002525 memclear(sv);
Eric Anholt06cb1322003-10-23 02:23:31 +00002526 sv.drm_di_major = version->drm_di_major;
2527 sv.drm_di_minor = version->drm_di_minor;
2528 sv.drm_dd_major = version->drm_dd_major;
2529 sv.drm_dd_minor = version->drm_dd_minor;
2530
Keith Packard8b9ab102008-06-13 16:03:22 -07002531 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002532 retcode = -errno;
Eric Anholt06cb1322003-10-23 02:23:31 +00002533 }
2534
2535 version->drm_di_major = sv.drm_di_major;
2536 version->drm_di_minor = sv.drm_di_minor;
2537 version->drm_dd_major = sv.drm_dd_major;
2538 version->drm_dd_minor = sv.drm_dd_minor;
2539
2540 return retcode;
2541}
2542
2543/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002544 * Send a device-specific command.
2545 *
2546 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002547 * \param drmCommandIndex command index
2548 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002549 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002550 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002551 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002552 * It issues a ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002553 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2554 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002555drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
Jens Owen3903e5a2002-04-09 21:54:56 +00002556{
Jens Owen3903e5a2002-04-09 21:54:56 +00002557 unsigned long request;
2558
2559 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2560
Daniel Vetterfd387942015-02-11 12:41:04 +01002561 if (drmIoctl(fd, request, NULL)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002562 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002563 }
2564 return 0;
2565}
2566
Jose Fonsecad2443b22003-05-27 00:37:33 +00002567
2568/**
2569 * Send a device-specific read command.
2570 *
2571 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002572 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002573 * \param data destination pointer of the data to be read.
2574 * \param size size of the data to be read.
Jan Vesely50d3c852016-06-30 14:22:52 -04002575 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002576 * \return zero on success, or a negative value on failure.
2577 *
2578 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002579 * It issues a read 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 drmCommandRead(int fd, unsigned long drmCommandIndex,
2583 void *data, unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002584{
2585 unsigned long request;
2586
Jan Vesely50d3c852016-06-30 14:22:52 -04002587 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2588 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002589
Keith Packard8b9ab102008-06-13 16:03:22 -07002590 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002591 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002592 }
2593 return 0;
2594}
2595
Jose Fonsecad2443b22003-05-27 00:37:33 +00002596
2597/**
2598 * Send a device-specific write command.
2599 *
2600 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002601 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002602 * \param data source pointer of the data to be written.
2603 * \param size size of the data to be written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002604 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002605 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002606 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002607 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002608 * It issues a write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002609 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2610 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002611drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2612 void *data, unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002613{
2614 unsigned long request;
2615
Jan Vesely50d3c852016-06-30 14:22:52 -04002616 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2617 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002618
Keith Packard8b9ab102008-06-13 16:03:22 -07002619 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002620 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002621 }
2622 return 0;
2623}
2624
Jose Fonsecad2443b22003-05-27 00:37:33 +00002625
2626/**
2627 * Send a device-specific read-write command.
2628 *
2629 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002630 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002631 * \param data source pointer of the data to be read and written.
2632 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002633 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002634 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002635 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002636 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002637 * It issues a read-write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002638 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2639 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002640drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2641 void *data, unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002642{
2643 unsigned long request;
2644
Jan Vesely50d3c852016-06-30 14:22:52 -04002645 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2646 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002647
Keith Packard8b9ab102008-06-13 16:03:22 -07002648 if (drmIoctl(fd, request, data))
Jan Vesely50d3c852016-06-30 14:22:52 -04002649 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002650 return 0;
2651}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002652
Dave Airlied51e1bb2006-11-09 08:55:58 +11002653#define DRM_MAX_FDS 16
2654static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002655 char *BusID;
2656 int fd;
2657 int refcount;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002658 int type;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002659} connection[DRM_MAX_FDS];
2660
2661static int nr_fds = 0;
2662
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002663drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
Dave Airlied51e1bb2006-11-09 08:55:58 +11002664{
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002665 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2666}
2667
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002668drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2669 int type)
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002670{
Brianccd7b6e2007-05-29 14:54:00 -06002671 int i;
2672 int fd;
Jan Vesely50d3c852016-06-30 14:22:52 -04002673
Brianccd7b6e2007-05-29 14:54:00 -06002674 for (i = 0; i < nr_fds; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04002675 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2676 (connection[i].type == type)) {
2677 connection[i].refcount++;
2678 *newlyopened = 0;
2679 return connection[i].fd;
2680 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002681
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002682 fd = drmOpenWithType(NULL, BusID, type);
Emil Velikovc1cd3d92015-07-14 15:05:18 +01002683 if (fd < 0 || nr_fds == DRM_MAX_FDS)
Jan Vesely50d3c852016-06-30 14:22:52 -04002684 return fd;
2685
Brianccd7b6e2007-05-29 14:54:00 -06002686 connection[nr_fds].BusID = strdup(BusID);
2687 connection[nr_fds].fd = fd;
2688 connection[nr_fds].refcount = 1;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002689 connection[nr_fds].type = type;
Brianccd7b6e2007-05-29 14:54:00 -06002690 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002691
Brianccd7b6e2007-05-29 14:54:00 -06002692 if (0)
Jan Vesely50d3c852016-06-30 14:22:52 -04002693 fprintf(stderr, "saved connection %d for %s %d\n",
2694 nr_fds, connection[nr_fds].BusID,
2695 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002696
Brianccd7b6e2007-05-29 14:54:00 -06002697 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002698
Brianccd7b6e2007-05-29 14:54:00 -06002699 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002700}
2701
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002702drm_public void drmCloseOnce(int fd)
Dave Airlied51e1bb2006-11-09 08:55:58 +11002703{
Brianccd7b6e2007-05-29 14:54:00 -06002704 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002705
Brianccd7b6e2007-05-29 14:54:00 -06002706 for (i = 0; i < nr_fds; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002707 if (fd == connection[i].fd) {
2708 if (--connection[i].refcount == 0) {
2709 drmClose(connection[i].fd);
2710 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002711
Jan Vesely50d3c852016-06-30 14:22:52 -04002712 if (i < --nr_fds)
2713 connection[i] = connection[nr_fds];
2714
2715 return;
2716 }
2717 }
Brianccd7b6e2007-05-29 14:54:00 -06002718 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002719}
Jesse Barnes731cd552008-12-17 10:09:49 -08002720
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002721drm_public int drmSetMaster(int fd)
Jesse Barnes731cd552008-12-17 10:09:49 -08002722{
Jan Vesely50d3c852016-06-30 14:22:52 -04002723 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002724}
2725
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002726drm_public int drmDropMaster(int fd)
Jesse Barnes731cd552008-12-17 10:09:49 -08002727{
Jan Vesely50d3c852016-06-30 14:22:52 -04002728 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002729}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002730
Eric Engestromeba66092019-02-08 14:46:07 +00002731drm_public int drmIsMaster(int fd)
Christopher James Halse Rogers17dfe3a2019-01-23 15:38:45 +11002732{
2733 /* Detect master by attempting something that requires master.
2734 *
2735 * Authenticating magic tokens requires master and 0 is an
2736 * internal kernel detail which we could use. Attempting this on
2737 * a master fd would fail therefore fail with EINVAL because 0
2738 * is invalid.
2739 *
2740 * A non-master fd will fail with EACCES, as the kernel checks
2741 * for master before attempting to do anything else.
2742 *
2743 * Since we don't want to leak implementation details, use
2744 * EACCES.
2745 */
2746 return drmAuthMagic(fd, 0) != -EACCES;
2747}
2748
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002749drm_public char *drmGetDeviceNameFromFd(int fd)
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002750{
Jan Vesely50d3c852016-06-30 14:22:52 -04002751 char name[128];
2752 struct stat sbuf;
2753 dev_t d;
2754 int i;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002755
Jan Vesely50d3c852016-06-30 14:22:52 -04002756 /* The whole drmOpen thing is a fiasco and we need to find a way
2757 * back to just using open(2). For now, however, lets just make
2758 * things worse with even more ad hoc directory walking code to
2759 * discover the device file name. */
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002760
Jan Vesely50d3c852016-06-30 14:22:52 -04002761 fstat(fd, &sbuf);
2762 d = sbuf.st_rdev;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002763
Jan Vesely50d3c852016-06-30 14:22:52 -04002764 for (i = 0; i < DRM_MAX_MINOR; i++) {
2765 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2766 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2767 break;
2768 }
2769 if (i == DRM_MAX_MINOR)
2770 return NULL;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002771
Jan Vesely50d3c852016-06-30 14:22:52 -04002772 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002773}
Dave Airliecc0a1452012-07-14 09:52:17 +00002774
Thomas Hellstromf8392582018-08-31 13:47:05 +02002775static bool drmNodeIsDRM(int maj, int min)
2776{
2777#ifdef __linux__
2778 char path[64];
2779 struct stat sbuf;
2780
2781 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2782 maj, min);
2783 return stat(path, &sbuf) == 0;
Emmanuel Vadot1c8d2b72020-01-21 17:42:44 +01002784#elif __FreeBSD__
2785 char name[SPECNAMELEN];
2786
2787 if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
2788 return 0;
2789 /* Handle drm/ and dri/ as both are present in different FreeBSD version
2790 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
2791 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
2792 * only device nodes in /dev/dri/ */
2793 return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
Thomas Hellstromf8392582018-08-31 13:47:05 +02002794#else
2795 return maj == DRM_MAJOR;
2796#endif
2797}
2798
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002799drm_public int drmGetNodeTypeFromFd(int fd)
Frank Binns1f735782015-02-13 10:51:15 +00002800{
Jan Vesely50d3c852016-06-30 14:22:52 -04002801 struct stat sbuf;
2802 int maj, min, type;
Frank Binns1f735782015-02-13 10:51:15 +00002803
Jan Vesely50d3c852016-06-30 14:22:52 -04002804 if (fstat(fd, &sbuf))
2805 return -1;
Frank Binns1f735782015-02-13 10:51:15 +00002806
Jan Vesely50d3c852016-06-30 14:22:52 -04002807 maj = major(sbuf.st_rdev);
2808 min = minor(sbuf.st_rdev);
Frank Binns1f735782015-02-13 10:51:15 +00002809
Thomas Hellstromf8392582018-08-31 13:47:05 +02002810 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002811 errno = EINVAL;
2812 return -1;
2813 }
Frank Binns1f735782015-02-13 10:51:15 +00002814
Jan Vesely50d3c852016-06-30 14:22:52 -04002815 type = drmGetMinorType(min);
2816 if (type == -1)
2817 errno = ENODEV;
2818 return type;
Frank Binns1f735782015-02-13 10:51:15 +00002819}
2820
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002821drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2822 int *prime_fd)
Dave Airliecc0a1452012-07-14 09:52:17 +00002823{
Jan Vesely50d3c852016-06-30 14:22:52 -04002824 struct drm_prime_handle args;
2825 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002826
Jan Vesely50d3c852016-06-30 14:22:52 -04002827 memclear(args);
2828 args.fd = -1;
2829 args.handle = handle;
2830 args.flags = flags;
2831 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2832 if (ret)
2833 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002834
Jan Vesely50d3c852016-06-30 14:22:52 -04002835 *prime_fd = args.fd;
2836 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002837}
2838
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002839drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
Dave Airliecc0a1452012-07-14 09:52:17 +00002840{
Jan Vesely50d3c852016-06-30 14:22:52 -04002841 struct drm_prime_handle args;
2842 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002843
Jan Vesely50d3c852016-06-30 14:22:52 -04002844 memclear(args);
2845 args.fd = prime_fd;
2846 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2847 if (ret)
2848 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002849
Jan Vesely50d3c852016-06-30 14:22:52 -04002850 *handle = args.handle;
2851 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002852}
2853
Emil Velikov0ca03a42015-03-07 00:58:39 +00002854static char *drmGetMinorNameForFD(int fd, int type)
2855{
2856#ifdef __linux__
Jan Vesely50d3c852016-06-30 14:22:52 -04002857 DIR *sysdir;
John Stultzbb45ce42018-03-20 17:48:23 +00002858 struct dirent *ent;
Jan Vesely50d3c852016-06-30 14:22:52 -04002859 struct stat sbuf;
2860 const char *name = drmGetMinorName(type);
2861 int len;
2862 char dev_name[64], buf[64];
Jan Vesely50d3c852016-06-30 14:22:52 -04002863 int maj, min;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002864
Jan Vesely50d3c852016-06-30 14:22:52 -04002865 if (!name)
2866 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002867
Jan Vesely50d3c852016-06-30 14:22:52 -04002868 len = strlen(name);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002869
Jan Vesely50d3c852016-06-30 14:22:52 -04002870 if (fstat(fd, &sbuf))
2871 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002872
Jan Vesely50d3c852016-06-30 14:22:52 -04002873 maj = major(sbuf.st_rdev);
2874 min = minor(sbuf.st_rdev);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002875
Thomas Hellstromf8392582018-08-31 13:47:05 +02002876 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jan Vesely50d3c852016-06-30 14:22:52 -04002877 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002878
Jan Vesely50d3c852016-06-30 14:22:52 -04002879 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002880
Jan Vesely50d3c852016-06-30 14:22:52 -04002881 sysdir = opendir(buf);
2882 if (!sysdir)
2883 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002884
John Stultzbb45ce42018-03-20 17:48:23 +00002885 while ((ent = readdir(sysdir))) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002886 if (strncmp(ent->d_name, name, len) == 0) {
2887 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2888 ent->d_name);
Mathias Tillman5c42b5e2015-08-24 11:56:13 +08002889
Jan Vesely50d3c852016-06-30 14:22:52 -04002890 closedir(sysdir);
Jan Vesely50d3c852016-06-30 14:22:52 -04002891 return strdup(dev_name);
2892 }
2893 }
Kevin Strasserf34b6942018-05-18 12:48:17 -07002894
2895 closedir(sysdir);
John Stultzbb45ce42018-03-20 17:48:23 +00002896 return NULL;
Emil Velikov8415a002015-09-07 18:29:05 +01002897#else
Jonathan Grayf1890112016-12-01 15:18:39 +11002898 struct stat sbuf;
2899 char buf[PATH_MAX + 1];
Eric Engestrom331e51e2018-12-19 13:53:41 +00002900 const char *dev_name = drmGetDeviceName(type);
Jonathan Grayf1890112016-12-01 15:18:39 +11002901 unsigned int maj, min;
Jonathan Gray293b95e2019-05-13 02:52:04 +10002902 int n;
Jonathan Grayf1890112016-12-01 15:18:39 +11002903
2904 if (fstat(fd, &sbuf))
2905 return NULL;
2906
2907 maj = major(sbuf.st_rdev);
2908 min = minor(sbuf.st_rdev);
2909
Thomas Hellstromf8392582018-08-31 13:47:05 +02002910 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jonathan Grayf1890112016-12-01 15:18:39 +11002911 return NULL;
2912
Eric Engestrom331e51e2018-12-19 13:53:41 +00002913 if (!dev_name)
Jonathan Grayf1890112016-12-01 15:18:39 +11002914 return NULL;
Jonathan Grayf1890112016-12-01 15:18:39 +11002915
Jonathan Gray293b95e2019-05-13 02:52:04 +10002916 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
Jonathan Grayf1890112016-12-01 15:18:39 +11002917 if (n == -1 || n >= sizeof(buf))
2918 return NULL;
2919
2920 return strdup(buf);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002921#endif
Emil Velikov0ca03a42015-03-07 00:58:39 +00002922}
2923
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002924drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
Emil Velikov0ca03a42015-03-07 00:58:39 +00002925{
Jan Vesely50d3c852016-06-30 14:22:52 -04002926 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002927}
2928
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07002929drm_public char *drmGetRenderDeviceNameFromFd(int fd)
Emil Velikov0ca03a42015-03-07 00:58:39 +00002930{
Jan Vesely50d3c852016-06-30 14:22:52 -04002931 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002932}
Emil Velikovb556ea12015-08-17 11:09:06 +08002933
Thierry Redingf8484cc2016-12-22 00:39:36 +01002934#ifdef __linux__
2935static char * DRM_PRINTFLIKE(2, 3)
2936sysfs_uevent_get(const char *path, const char *fmt, ...)
2937{
2938 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2939 size_t size = 0, len;
2940 ssize_t num;
2941 va_list ap;
2942 FILE *fp;
2943
2944 va_start(ap, fmt);
2945 num = vasprintf(&key, fmt, ap);
2946 va_end(ap);
2947 len = num;
2948
2949 snprintf(filename, sizeof(filename), "%s/uevent", path);
2950
2951 fp = fopen(filename, "r");
2952 if (!fp) {
2953 free(key);
2954 return NULL;
2955 }
2956
2957 while ((num = getline(&line, &size, fp)) >= 0) {
2958 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
2959 char *start = line + len + 1, *end = line + num - 1;
2960
2961 if (*end != '\n')
2962 end++;
2963
2964 value = strndup(start, end - start);
2965 break;
2966 }
2967 }
2968
2969 free(line);
2970 fclose(fp);
2971
2972 free(key);
2973
2974 return value;
2975}
2976#endif
2977
Emil Velikov39885802018-05-15 17:37:49 +01002978/* Little white lie to avoid major rework of the existing code */
2979#define DRM_BUS_VIRTIO 0x10
2980
Emil Velikov291b2bb2015-09-07 18:26:34 +01002981#ifdef __linux__
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01002982static int get_subsystem_type(const char *device_path)
2983{
2984 char path[PATH_MAX + 1] = "";
Emil Velikovb556ea12015-08-17 11:09:06 +08002985 char link[PATH_MAX + 1] = "";
2986 char *name;
Eric Anholt9b28c5a2018-11-15 17:48:53 -08002987 struct {
2988 const char *name;
2989 int bus_type;
2990 } bus_types[] = {
2991 { "/pci", DRM_BUS_PCI },
2992 { "/usb", DRM_BUS_USB },
2993 { "/platform", DRM_BUS_PLATFORM },
Eric Anholt89700ab2018-11-15 17:52:19 -08002994 { "/spi", DRM_BUS_PLATFORM },
Eric Anholt9b28c5a2018-11-15 17:48:53 -08002995 { "/host1x", DRM_BUS_HOST1X },
2996 { "/virtio", DRM_BUS_VIRTIO },
2997 };
Emil Velikovb556ea12015-08-17 11:09:06 +08002998
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01002999 strncpy(path, device_path, PATH_MAX);
3000 strncat(path, "/subsystem", PATH_MAX);
Emil Velikova250fce2015-09-07 12:54:27 +01003001
3002 if (readlink(path, link, PATH_MAX) < 0)
3003 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08003004
3005 name = strrchr(link, '/');
3006 if (!name)
3007 return -EINVAL;
3008
Eric Anholt9b28c5a2018-11-15 17:48:53 -08003009 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3010 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3011 return bus_types[i].bus_type;
3012 }
Emil Velikov39885802018-05-15 17:37:49 +01003013
Emil Velikovb556ea12015-08-17 11:09:06 +08003014 return -EINVAL;
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003015}
3016#endif
3017
3018static int drmParseSubsystemType(int maj, int min)
3019{
3020#ifdef __linux__
3021 char path[PATH_MAX + 1] = "";
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003022 char real_path[PATH_MAX + 1] = "";
3023 int subsystem_type;
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003024
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003025 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3026 if (!realpath(path, real_path))
3027 return -errno;
3028 snprintf(path, sizeof(path), "%s", real_path);
Vasyl Vavrychukc4eae712020-01-29 03:54:56 +01003029
Vasyl Vavrychuk8a733722020-01-29 16:08:41 +01003030 subsystem_type = get_subsystem_type(path);
3031 if (subsystem_type == DRM_BUS_VIRTIO) {
3032 strncat(path, "/..", PATH_MAX);
3033 subsystem_type = get_subsystem_type(path);
3034 }
3035 return subsystem_type;
François Tigeot200e9e92018-12-12 20:48:35 +01003036#elif defined(__OpenBSD__) || defined(__DragonFly__)
Thierry Redinge17cad12016-12-21 18:00:52 +01003037 return DRM_BUS_PCI;
Emil Velikov291b2bb2015-09-07 18:26:34 +01003038#else
3039#warning "Missing implementation of drmParseSubsystemType"
3040 return -EINVAL;
3041#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08003042}
3043
Eric Engestrom56499532018-09-05 13:23:59 +01003044static void
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003045get_pci_path(int maj, int min, char *pci_path)
Emil Velikova0290012018-05-15 17:29:44 +01003046{
Emil Velikov39885802018-05-15 17:37:49 +01003047 char path[PATH_MAX + 1], *term;
Emil Velikova0290012018-05-15 17:29:44 +01003048
3049 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003050 if (!realpath(path, pci_path)) {
3051 strcpy(pci_path, path);
Eric Engestrom56499532018-09-05 13:23:59 +01003052 return;
3053 }
Emil Velikova0290012018-05-15 17:29:44 +01003054
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003055 term = strrchr(pci_path, '/');
Emil Velikov39885802018-05-15 17:37:49 +01003056 if (term && strncmp(term, "/virtio", 7) == 0)
3057 *term = 0;
Emil Velikovbcb9d972018-08-23 10:49:54 +01003058}
3059
Emil Velikov536e0de2015-09-07 12:37:57 +01003060static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
Emil Velikovb556ea12015-08-17 11:09:06 +08003061{
Emil Velikov291b2bb2015-09-07 18:26:34 +01003062#ifdef __linux__
Thierry Reding5403cb32017-01-18 08:29:23 +01003063 unsigned int domain, bus, dev, func;
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003064 char pci_path[PATH_MAX + 1], *value;
Thierry Reding5403cb32017-01-18 08:29:23 +01003065 int num;
Emil Velikovb556ea12015-08-17 11:09:06 +08003066
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003067 get_pci_path(maj, min, pci_path);
Emil Velikov536e0de2015-09-07 12:37:57 +01003068
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003069 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
Thierry Reding5403cb32017-01-18 08:29:23 +01003070 if (!value)
3071 return -ENOENT;
Emil Velikov536e0de2015-09-07 12:37:57 +01003072
Thierry Reding5403cb32017-01-18 08:29:23 +01003073 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3074 free(value);
3075
3076 if (num != 4)
Emil Velikovb556ea12015-08-17 11:09:06 +08003077 return -EINVAL;
3078
Emil Velikovb556ea12015-08-17 11:09:06 +08003079 info->domain = domain;
3080 info->bus = bus;
3081 info->dev = dev;
3082 info->func = func;
3083
3084 return 0;
François Tigeot8f2e0922018-12-12 20:48:36 +01003085#elif defined(__OpenBSD__) || defined(__DragonFly__)
Jonathan Grayfd190562016-12-01 15:18:42 +11003086 struct drm_pciinfo pinfo;
3087 int fd, type;
3088
3089 type = drmGetMinorType(min);
3090 if (type == -1)
3091 return -ENODEV;
3092
3093 fd = drmOpenMinor(min, 0, type);
3094 if (fd < 0)
3095 return -errno;
3096
3097 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3098 close(fd);
3099 return -errno;
3100 }
3101 close(fd);
3102
3103 info->domain = pinfo.domain;
3104 info->bus = pinfo.bus;
3105 info->dev = pinfo.dev;
3106 info->func = pinfo.func;
3107
3108 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01003109#else
3110#warning "Missing implementation of drmParsePciBusInfo"
3111 return -EINVAL;
3112#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08003113}
3114
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003115drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
Emil Velikovb556ea12015-08-17 11:09:06 +08003116{
Emil Velikovbc2aca92015-09-07 13:51:54 +01003117 if (a == NULL || b == NULL)
Adam Jackson7c27cd72017-05-04 10:48:56 -04003118 return 0;
Emil Velikovbc2aca92015-09-07 13:51:54 +01003119
Emil Velikovb556ea12015-08-17 11:09:06 +08003120 if (a->bustype != b->bustype)
Adam Jackson7c27cd72017-05-04 10:48:56 -04003121 return 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003122
3123 switch (a->bustype) {
3124 case DRM_BUS_PCI:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003125 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
Thierry Redingf8484cc2016-12-22 00:39:36 +01003126
3127 case DRM_BUS_USB:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003128 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
Thierry Redingf8484cc2016-12-22 00:39:36 +01003129
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003130 case DRM_BUS_PLATFORM:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003131 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003132
3133 case DRM_BUS_HOST1X:
Adam Jackson7c27cd72017-05-04 10:48:56 -04003134 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003135
Emil Velikovb556ea12015-08-17 11:09:06 +08003136 default:
3137 break;
3138 }
3139
Adam Jackson7c27cd72017-05-04 10:48:56 -04003140 return 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003141}
3142
3143static int drmGetNodeType(const char *name)
3144{
Emil Velikovb556ea12015-08-17 11:09:06 +08003145 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3146 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3147 return DRM_NODE_CONTROL;
3148
3149 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3150 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3151 return DRM_NODE_RENDER;
3152
Jonathan Gray13e2c352019-05-13 02:50:49 +10003153 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3154 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3155 return DRM_NODE_PRIMARY;
3156
Emil Velikovb556ea12015-08-17 11:09:06 +08003157 return -EINVAL;
3158}
3159
Emil Velikov5f68d312015-09-07 13:54:32 +01003160static int drmGetMaxNodeName(void)
3161{
3162 return sizeof(DRM_DIR_NAME) +
3163 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3164 sizeof(DRM_CONTROL_MINOR_NAME),
3165 sizeof(DRM_RENDER_MINOR_NAME)) +
Eric Engestromce975072016-04-03 19:48:12 +01003166 3 /* length of the node number */;
Emil Velikov5f68d312015-09-07 13:54:32 +01003167}
3168
Emil Velikov291b2bb2015-09-07 18:26:34 +01003169#ifdef __linux__
Emil Velikovaae3f312016-11-01 18:04:06 +00003170static int parse_separate_sysfs_files(int maj, int min,
Emil Velikov11687bf2016-11-30 17:24:21 +00003171 drmPciDeviceInfoPtr device,
3172 bool ignore_revision)
Emil Velikovaae3f312016-11-01 18:04:06 +00003173{
Emil Velikovaae3f312016-11-01 18:04:06 +00003174 static const char *attrs[] = {
3175 "revision", /* Older kernels are missing the file, so check for it first */
3176 "vendor",
3177 "device",
3178 "subsystem_vendor",
3179 "subsystem_device",
3180 };
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003181 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
Emil Velikovaae3f312016-11-01 18:04:06 +00003182 unsigned int data[ARRAY_SIZE(attrs)];
3183 FILE *fp;
3184 int ret;
3185
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003186 get_pci_path(maj, min, pci_path);
Emil Velikova0290012018-05-15 17:29:44 +01003187
Emil Velikov11687bf2016-11-30 17:24:21 +00003188 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003189 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
Emil Velikovaae3f312016-11-01 18:04:06 +00003190 fp = fopen(path, "r");
3191 if (!fp)
3192 return -errno;
3193
3194 ret = fscanf(fp, "%x", &data[i]);
3195 fclose(fp);
3196 if (ret != 1)
3197 return -errno;
3198
3199 }
3200
Emil Velikov11687bf2016-11-30 17:24:21 +00003201 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
Emil Velikovaae3f312016-11-01 18:04:06 +00003202 device->vendor_id = data[1] & 0xffff;
3203 device->device_id = data[2] & 0xffff;
3204 device->subvendor_id = data[3] & 0xffff;
3205 device->subdevice_id = data[4] & 0xffff;
3206
3207 return 0;
3208}
3209
3210static int parse_config_sysfs_file(int maj, int min,
3211 drmPciDeviceInfoPtr device)
3212{
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003213 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
Emil Velikovef5192e2015-09-07 12:47:47 +01003214 unsigned char config[64];
3215 int fd, ret;
3216
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003217 get_pci_path(maj, min, pci_path);
Emil Velikova0290012018-05-15 17:29:44 +01003218
Eric Engestrom9030a0f2018-09-05 13:29:08 +01003219 snprintf(path, PATH_MAX, "%s/config", pci_path);
Emil Velikovef5192e2015-09-07 12:47:47 +01003220 fd = open(path, O_RDONLY);
3221 if (fd < 0)
3222 return -errno;
3223
3224 ret = read(fd, config, sizeof(config));
3225 close(fd);
3226 if (ret < 0)
3227 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08003228
3229 device->vendor_id = config[0] | (config[1] << 8);
3230 device->device_id = config[2] | (config[3] << 8);
3231 device->revision_id = config[8];
3232 device->subvendor_id = config[44] | (config[45] << 8);
3233 device->subdevice_id = config[46] | (config[47] << 8);
3234
3235 return 0;
Emil Velikovaae3f312016-11-01 18:04:06 +00003236}
3237#endif
3238
3239static int drmParsePciDeviceInfo(int maj, int min,
3240 drmPciDeviceInfoPtr device,
3241 uint32_t flags)
3242{
3243#ifdef __linux__
Emil Velikov11687bf2016-11-30 17:24:21 +00003244 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3245 return parse_separate_sysfs_files(maj, min, device, true);
3246
3247 if (parse_separate_sysfs_files(maj, min, device, false))
Emil Velikovaae3f312016-11-01 18:04:06 +00003248 return parse_config_sysfs_file(maj, min, device);
3249
3250 return 0;
François Tigeot8f2e0922018-12-12 20:48:36 +01003251#elif defined(__OpenBSD__) || defined(__DragonFly__)
Jonathan Grayc0ef1d02016-12-01 15:18:41 +11003252 struct drm_pciinfo pinfo;
3253 int fd, type;
3254
3255 type = drmGetMinorType(min);
3256 if (type == -1)
3257 return -ENODEV;
3258
3259 fd = drmOpenMinor(min, 0, type);
3260 if (fd < 0)
3261 return -errno;
3262
3263 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3264 close(fd);
3265 return -errno;
3266 }
3267 close(fd);
3268
3269 device->vendor_id = pinfo.vendor_id;
3270 device->device_id = pinfo.device_id;
3271 device->revision_id = pinfo.revision_id;
3272 device->subvendor_id = pinfo.subvendor_id;
3273 device->subdevice_id = pinfo.subdevice_id;
3274
3275 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01003276#else
3277#warning "Missing implementation of drmParsePciDeviceInfo"
3278 return -EINVAL;
3279#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08003280}
3281
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003282static void drmFreePlatformDevice(drmDevicePtr device)
3283{
3284 if (device->deviceinfo.platform) {
3285 if (device->deviceinfo.platform->compatible) {
3286 char **compatible = device->deviceinfo.platform->compatible;
3287
3288 while (*compatible) {
3289 free(*compatible);
3290 compatible++;
3291 }
3292
3293 free(device->deviceinfo.platform->compatible);
3294 }
3295 }
3296}
3297
3298static void drmFreeHost1xDevice(drmDevicePtr device)
3299{
3300 if (device->deviceinfo.host1x) {
3301 if (device->deviceinfo.host1x->compatible) {
3302 char **compatible = device->deviceinfo.host1x->compatible;
3303
3304 while (*compatible) {
3305 free(*compatible);
3306 compatible++;
3307 }
3308
3309 free(device->deviceinfo.host1x->compatible);
3310 }
3311 }
3312}
3313
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003314drm_public void drmFreeDevice(drmDevicePtr *device)
Emil Velikovb556ea12015-08-17 11:09:06 +08003315{
Emil Velikovb556ea12015-08-17 11:09:06 +08003316 if (device == NULL)
3317 return;
3318
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003319 if (*device) {
3320 switch ((*device)->bustype) {
3321 case DRM_BUS_PLATFORM:
3322 drmFreePlatformDevice(*device);
3323 break;
3324
3325 case DRM_BUS_HOST1X:
3326 drmFreeHost1xDevice(*device);
3327 break;
3328 }
3329 }
3330
Emil Velikov5f68d312015-09-07 13:54:32 +01003331 free(*device);
3332 *device = NULL;
Emil Velikovb556ea12015-08-17 11:09:06 +08003333}
3334
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003335drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
Emil Velikovb556ea12015-08-17 11:09:06 +08003336{
3337 int i;
3338
3339 if (devices == NULL)
3340 return;
3341
Qiang Yu6c056ee2016-07-14 17:10:56 +08003342 for (i = 0; i < count; i++)
3343 if (devices[i])
3344 drmFreeDevice(&devices[i]);
Emil Velikovb556ea12015-08-17 11:09:06 +08003345}
3346
Thierry Reding2e57bba2016-12-21 17:54:48 +01003347static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3348 size_t bus_size, size_t device_size,
3349 char **ptrp)
3350{
3351 size_t max_node_length, extra, size;
3352 drmDevicePtr device;
3353 unsigned int i;
3354 char *ptr;
3355
3356 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3357 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3358
3359 size = sizeof(*device) + extra + bus_size + device_size;
3360
3361 device = calloc(1, size);
3362 if (!device)
3363 return NULL;
3364
3365 device->available_nodes = 1 << type;
3366
3367 ptr = (char *)device + sizeof(*device);
3368 device->nodes = (char **)ptr;
3369
3370 ptr += DRM_NODE_MAX * sizeof(void *);
3371
3372 for (i = 0; i < DRM_NODE_MAX; i++) {
3373 device->nodes[i] = ptr;
3374 ptr += max_node_length;
3375 }
3376
3377 memcpy(device->nodes[type], node, max_node_length);
3378
3379 *ptrp = ptr;
3380
3381 return device;
3382}
3383
Emil Velikovb40a65d2016-11-30 16:41:27 +00003384static int drmProcessPciDevice(drmDevicePtr *device,
Emil Velikovfae59d72015-09-09 17:54:34 +01003385 const char *node, int node_type,
Emil Velikov138d2312016-11-30 17:13:51 +00003386 int maj, int min, bool fetch_deviceinfo,
3387 uint32_t flags)
Emil Velikovfae59d72015-09-09 17:54:34 +01003388{
Thierry Reding2e57bba2016-12-21 17:54:48 +01003389 drmDevicePtr dev;
Michel Dänzer30455232015-10-14 12:48:52 +09003390 char *addr;
Thierry Reding2e57bba2016-12-21 17:54:48 +01003391 int ret;
Emil Velikovfae59d72015-09-09 17:54:34 +01003392
Thierry Reding2e57bba2016-12-21 17:54:48 +01003393 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3394 sizeof(drmPciDeviceInfo), &addr);
3395 if (!dev)
Emil Velikovfae59d72015-09-09 17:54:34 +01003396 return -ENOMEM;
3397
Thierry Reding2e57bba2016-12-21 17:54:48 +01003398 dev->bustype = DRM_BUS_PCI;
Jan Vesely50d3c852016-06-30 14:22:52 -04003399
Thierry Reding2e57bba2016-12-21 17:54:48 +01003400 dev->businfo.pci = (drmPciBusInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003401
Thierry Reding2e57bba2016-12-21 17:54:48 +01003402 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
Emil Velikovfae59d72015-09-09 17:54:34 +01003403 if (ret)
3404 goto free_device;
3405
3406 // Fetch the device info if the user has requested it
3407 if (fetch_deviceinfo) {
3408 addr += sizeof(drmPciBusInfo);
Thierry Reding2e57bba2016-12-21 17:54:48 +01003409 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003410
Thierry Reding2e57bba2016-12-21 17:54:48 +01003411 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
Emil Velikovfae59d72015-09-09 17:54:34 +01003412 if (ret)
3413 goto free_device;
3414 }
Thierry Reding2e57bba2016-12-21 17:54:48 +01003415
3416 *device = dev;
3417
Emil Velikovfae59d72015-09-09 17:54:34 +01003418 return 0;
3419
3420free_device:
Thierry Reding2e57bba2016-12-21 17:54:48 +01003421 free(dev);
Emil Velikovfae59d72015-09-09 17:54:34 +01003422 return ret;
3423}
3424
Thierry Redingf8484cc2016-12-22 00:39:36 +01003425static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3426{
3427#ifdef __linux__
3428 char path[PATH_MAX + 1], *value;
3429 unsigned int bus, dev;
3430 int ret;
3431
3432 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3433
3434 value = sysfs_uevent_get(path, "BUSNUM");
3435 if (!value)
3436 return -ENOENT;
3437
3438 ret = sscanf(value, "%03u", &bus);
3439 free(value);
3440
3441 if (ret <= 0)
3442 return -errno;
3443
3444 value = sysfs_uevent_get(path, "DEVNUM");
3445 if (!value)
3446 return -ENOENT;
3447
3448 ret = sscanf(value, "%03u", &dev);
3449 free(value);
3450
3451 if (ret <= 0)
3452 return -errno;
3453
3454 info->bus = bus;
3455 info->dev = dev;
3456
3457 return 0;
3458#else
3459#warning "Missing implementation of drmParseUsbBusInfo"
3460 return -EINVAL;
3461#endif
3462}
3463
3464static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3465{
3466#ifdef __linux__
3467 char path[PATH_MAX + 1], *value;
3468 unsigned int vendor, product;
3469 int ret;
3470
3471 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3472
3473 value = sysfs_uevent_get(path, "PRODUCT");
3474 if (!value)
3475 return -ENOENT;
3476
3477 ret = sscanf(value, "%x/%x", &vendor, &product);
3478 free(value);
3479
3480 if (ret <= 0)
3481 return -errno;
3482
3483 info->vendor = vendor;
3484 info->product = product;
3485
3486 return 0;
3487#else
3488#warning "Missing implementation of drmParseUsbDeviceInfo"
3489 return -EINVAL;
3490#endif
3491}
3492
3493static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3494 int node_type, int maj, int min,
3495 bool fetch_deviceinfo, uint32_t flags)
3496{
3497 drmDevicePtr dev;
3498 char *ptr;
3499 int ret;
3500
3501 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3502 sizeof(drmUsbDeviceInfo), &ptr);
3503 if (!dev)
3504 return -ENOMEM;
3505
3506 dev->bustype = DRM_BUS_USB;
3507
3508 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3509
3510 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3511 if (ret < 0)
3512 goto free_device;
3513
3514 if (fetch_deviceinfo) {
3515 ptr += sizeof(drmUsbBusInfo);
3516 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3517
3518 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3519 if (ret < 0)
3520 goto free_device;
3521 }
3522
3523 *device = dev;
3524
3525 return 0;
3526
3527free_device:
3528 free(dev);
3529 return ret;
3530}
3531
Emil Velikovee798b92019-01-23 10:39:12 +00003532static int drmParseOFBusInfo(int maj, int min, char *fullname)
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003533{
3534#ifdef __linux__
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003535 char path[PATH_MAX + 1], *name, *tmp_name;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003536
3537 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3538
3539 name = sysfs_uevent_get(path, "OF_FULLNAME");
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003540 tmp_name = name;
3541 if (!name) {
3542 /* If the device lacks OF data, pick the MODALIAS info */
3543 name = sysfs_uevent_get(path, "MODALIAS");
3544 if (!name)
3545 return -ENOENT;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003546
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003547 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3548 tmp_name = strrchr(name, ':');
3549 if (!tmp_name) {
3550 free(name);
3551 return -ENOENT;
3552 }
3553 tmp_name++;
3554 }
3555
Emil Velikovee798b92019-01-23 10:39:12 +00003556 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3557 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003558 free(name);
3559
3560 return 0;
3561#else
Emil Velikovee798b92019-01-23 10:39:12 +00003562#warning "Missing implementation of drmParseOFBusInfo"
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003563 return -EINVAL;
3564#endif
3565}
3566
Emil Velikovee798b92019-01-23 10:39:12 +00003567static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003568{
3569#ifdef __linux__
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003570 char path[PATH_MAX + 1], *value, *tmp_name;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003571 unsigned int count, i;
3572 int err;
3573
3574 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3575
3576 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003577 if (value) {
3578 sscanf(value, "%u", &count);
3579 free(value);
3580 } else {
3581 /* Assume one entry if the device lack OF data */
3582 count = 1;
3583 }
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003584
Emil Velikovee798b92019-01-23 10:39:12 +00003585 *compatible = calloc(count + 1, sizeof(char *));
3586 if (!*compatible)
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003587 return -ENOMEM;
3588
3589 for (i = 0; i < count; i++) {
3590 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003591 tmp_name = value;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003592 if (!value) {
Emil Velikov3df8a7f2019-01-23 09:44:07 +00003593 /* If the device lacks OF data, pick the MODALIAS info */
3594 value = sysfs_uevent_get(path, "MODALIAS");
3595 if (!value) {
3596 err = -ENOENT;
3597 goto free;
3598 }
3599
3600 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3601 tmp_name = strrchr(value, ':');
3602 if (!tmp_name) {
3603 free(value);
3604 return -ENOENT;
3605 }
3606 tmp_name = strdup(tmp_name + 1);
3607 free(value);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003608 }
3609
Andreas Baierl4735ca72019-03-11 16:04:08 +01003610 (*compatible)[i] = tmp_name;
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003611 }
3612
3613 return 0;
3614
3615free:
3616 while (i--)
Andreas Baierl4735ca72019-03-11 16:04:08 +01003617 free((*compatible)[i]);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003618
Emil Velikovee798b92019-01-23 10:39:12 +00003619 free(*compatible);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003620 return err;
3621#else
Emil Velikovee798b92019-01-23 10:39:12 +00003622#warning "Missing implementation of drmParseOFDeviceInfo"
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003623 return -EINVAL;
3624#endif
3625}
3626
3627static int drmProcessPlatformDevice(drmDevicePtr *device,
3628 const char *node, int node_type,
3629 int maj, int min, bool fetch_deviceinfo,
3630 uint32_t flags)
3631{
3632 drmDevicePtr dev;
3633 char *ptr;
3634 int ret;
3635
3636 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3637 sizeof(drmPlatformDeviceInfo), &ptr);
3638 if (!dev)
3639 return -ENOMEM;
3640
3641 dev->bustype = DRM_BUS_PLATFORM;
3642
3643 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3644
Emil Velikovee798b92019-01-23 10:39:12 +00003645 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003646 if (ret < 0)
3647 goto free_device;
3648
3649 if (fetch_deviceinfo) {
3650 ptr += sizeof(drmPlatformBusInfo);
3651 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3652
Emil Velikovee798b92019-01-23 10:39:12 +00003653 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003654 if (ret < 0)
3655 goto free_device;
3656 }
3657
3658 *device = dev;
3659
3660 return 0;
3661
3662free_device:
3663 free(dev);
3664 return ret;
3665}
3666
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003667static int drmProcessHost1xDevice(drmDevicePtr *device,
3668 const char *node, int node_type,
3669 int maj, int min, bool fetch_deviceinfo,
3670 uint32_t flags)
3671{
3672 drmDevicePtr dev;
3673 char *ptr;
3674 int ret;
3675
3676 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3677 sizeof(drmHost1xDeviceInfo), &ptr);
3678 if (!dev)
3679 return -ENOMEM;
3680
3681 dev->bustype = DRM_BUS_HOST1X;
3682
3683 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3684
Emil Velikovee798b92019-01-23 10:39:12 +00003685 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003686 if (ret < 0)
3687 goto free_device;
3688
3689 if (fetch_deviceinfo) {
3690 ptr += sizeof(drmHost1xBusInfo);
3691 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3692
Emil Velikovee798b92019-01-23 10:39:12 +00003693 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
Thierry Reding7b1f37f2016-12-21 17:59:04 +01003694 if (ret < 0)
3695 goto free_device;
3696 }
3697
3698 *device = dev;
3699
3700 return 0;
3701
3702free_device:
3703 free(dev);
3704 return ret;
3705}
3706
Emil Velikovf808fee2018-05-15 15:02:52 +01003707static int
3708process_device(drmDevicePtr *device, const char *d_name,
3709 int req_subsystem_type,
3710 bool fetch_deviceinfo, uint32_t flags)
3711{
3712 struct stat sbuf;
3713 char node[PATH_MAX + 1];
3714 int node_type, subsystem_type;
3715 unsigned int maj, min;
3716
3717 node_type = drmGetNodeType(d_name);
3718 if (node_type < 0)
3719 return -1;
3720
3721 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3722 if (stat(node, &sbuf))
3723 return -1;
3724
3725 maj = major(sbuf.st_rdev);
3726 min = minor(sbuf.st_rdev);
3727
Thomas Hellstromf8392582018-08-31 13:47:05 +02003728 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Emil Velikovf808fee2018-05-15 15:02:52 +01003729 return -1;
3730
3731 subsystem_type = drmParseSubsystemType(maj, min);
3732 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3733 return -1;
3734
3735 switch (subsystem_type) {
3736 case DRM_BUS_PCI:
3737 return drmProcessPciDevice(device, node, node_type, maj, min,
3738 fetch_deviceinfo, flags);
3739 case DRM_BUS_USB:
3740 return drmProcessUsbDevice(device, node, node_type, maj, min,
3741 fetch_deviceinfo, flags);
3742 case DRM_BUS_PLATFORM:
3743 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3744 fetch_deviceinfo, flags);
3745 case DRM_BUS_HOST1X:
3746 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3747 fetch_deviceinfo, flags);
3748 default:
3749 return -1;
3750 }
3751}
3752
Qiang Yu3c208932016-07-14 17:10:55 +08003753/* Consider devices located on the same bus as duplicate and fold the respective
3754 * entries into a single one.
3755 *
3756 * Note: this leaves "gaps" in the array, while preserving the length.
3757 */
Emil Velikovfae59d72015-09-09 17:54:34 +01003758static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3759{
3760 int node_type, i, j;
3761
3762 for (i = 0; i < count; i++) {
3763 for (j = i + 1; j < count; j++) {
Adam Jacksona2fa2e02017-05-04 15:57:14 -04003764 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
Emil Velikovfae59d72015-09-09 17:54:34 +01003765 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3766 node_type = log2(local_devices[j]->available_nodes);
3767 memcpy(local_devices[i]->nodes[node_type],
3768 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3769 drmFreeDevice(&local_devices[j]);
3770 }
3771 }
3772 }
3773}
3774
Emil Velikov11687bf2016-11-30 17:24:21 +00003775/* Check that the given flags are valid returning 0 on success */
3776static int
3777drm_device_validate_flags(uint32_t flags)
3778{
3779 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3780}
3781
Emil Velikov56e72d32018-06-21 16:06:35 +01003782static bool
3783drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3784{
3785 struct stat sbuf;
3786
3787 for (int i = 0; i < DRM_NODE_MAX; i++) {
3788 if (device->available_nodes & 1 << i) {
3789 if (stat(device->nodes[i], &sbuf) == 0 &&
3790 sbuf.st_rdev == find_rdev)
3791 return true;
3792 }
3793 }
3794 return false;
3795}
3796
Emil Velikov95b262f2018-05-15 16:32:10 +01003797/*
3798 * The kernel drm core has a number of places that assume maximum of
3799 * 3x64 devices nodes. That's 64 for each of primary, control and
3800 * render nodes. Rounded it up to 256 for simplicity.
3801 */
3802#define MAX_DRM_NODES 256
3803
Emil Velikovb556ea12015-08-17 11:09:06 +08003804/**
Emil Velikovccedf662015-09-09 16:02:18 +01003805 * Get information about the opened drm device
3806 *
3807 * \param fd file descriptor of the drm device
Emil Velikov11687bf2016-11-30 17:24:21 +00003808 * \param flags feature/behaviour bitmask
Emil Velikovccedf662015-09-09 16:02:18 +01003809 * \param device the address of a drmDevicePtr where the information
3810 * will be allocated in stored
3811 *
3812 * \return zero on success, negative error code otherwise.
Emil Velikov11687bf2016-11-30 17:24:21 +00003813 *
3814 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3815 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
Emil Velikovccedf662015-09-09 16:02:18 +01003816 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003817drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
Emil Velikovccedf662015-09-09 16:02:18 +01003818{
Jonathan Gray08257922016-12-01 15:18:43 +11003819#ifdef __OpenBSD__
3820 /*
3821 * DRI device nodes on OpenBSD are not in their own directory, they reside
3822 * in /dev along with a large number of statically generated /dev nodes.
3823 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3824 */
3825 drmDevicePtr d;
3826 struct stat sbuf;
3827 char node[PATH_MAX + 1];
3828 const char *dev_name;
3829 int node_type, subsystem_type;
Jonathan Gray293b95e2019-05-13 02:52:04 +10003830 int maj, min, n, ret;
Jonathan Gray08257922016-12-01 15:18:43 +11003831
3832 if (fd == -1 || device == NULL)
3833 return -EINVAL;
3834
3835 if (fstat(fd, &sbuf))
3836 return -errno;
3837
3838 maj = major(sbuf.st_rdev);
3839 min = minor(sbuf.st_rdev);
3840
Thomas Hellstromf8392582018-08-31 13:47:05 +02003841 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jonathan Gray08257922016-12-01 15:18:43 +11003842 return -EINVAL;
3843
3844 node_type = drmGetMinorType(min);
3845 if (node_type == -1)
3846 return -ENODEV;
3847
Eric Engestrom331e51e2018-12-19 13:53:41 +00003848 dev_name = drmGetDeviceName(node_type);
3849 if (!dev_name)
Jonathan Gray08257922016-12-01 15:18:43 +11003850 return -EINVAL;
Jonathan Gray08257922016-12-01 15:18:43 +11003851
Jonathan Gray293b95e2019-05-13 02:52:04 +10003852 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
Jonathan Gray08257922016-12-01 15:18:43 +11003853 if (n == -1 || n >= PATH_MAX)
3854 return -errno;
3855 if (stat(node, &sbuf))
3856 return -EINVAL;
3857
3858 subsystem_type = drmParseSubsystemType(maj, min);
3859 if (subsystem_type != DRM_BUS_PCI)
3860 return -ENODEV;
3861
3862 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3863 if (ret)
3864 return ret;
3865
3866 *device = d;
3867
3868 return 0;
3869#else
Emil Velikov95b262f2018-05-15 16:32:10 +01003870 drmDevicePtr local_devices[MAX_DRM_NODES];
Emil Velikovccedf662015-09-09 16:02:18 +01003871 drmDevicePtr d;
3872 DIR *sysdir;
3873 struct dirent *dent;
3874 struct stat sbuf;
Emil Velikovf808fee2018-05-15 15:02:52 +01003875 int subsystem_type;
Emil Velikovccedf662015-09-09 16:02:18 +01003876 int maj, min;
3877 int ret, i, node_count;
Qiang Yu3c208932016-07-14 17:10:55 +08003878 dev_t find_rdev;
Emil Velikov11687bf2016-11-30 17:24:21 +00003879
3880 if (drm_device_validate_flags(flags))
3881 return -EINVAL;
Emil Velikovccedf662015-09-09 16:02:18 +01003882
3883 if (fd == -1 || device == NULL)
3884 return -EINVAL;
3885
3886 if (fstat(fd, &sbuf))
3887 return -errno;
3888
Qiang Yu3c208932016-07-14 17:10:55 +08003889 find_rdev = sbuf.st_rdev;
Emil Velikovccedf662015-09-09 16:02:18 +01003890 maj = major(sbuf.st_rdev);
3891 min = minor(sbuf.st_rdev);
3892
Thomas Hellstromf8392582018-08-31 13:47:05 +02003893 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Emil Velikovccedf662015-09-09 16:02:18 +01003894 return -EINVAL;
3895
3896 subsystem_type = drmParseSubsystemType(maj, min);
Emil Velikov7f52a0e2018-05-15 16:43:58 +01003897 if (subsystem_type < 0)
3898 return subsystem_type;
Emil Velikovccedf662015-09-09 16:02:18 +01003899
Emil Velikovccedf662015-09-09 16:02:18 +01003900 sysdir = opendir(DRM_DIR_NAME);
Emil Velikov95b262f2018-05-15 16:32:10 +01003901 if (!sysdir)
3902 return -errno;
Emil Velikovccedf662015-09-09 16:02:18 +01003903
3904 i = 0;
3905 while ((dent = readdir(sysdir))) {
Emil Velikovf808fee2018-05-15 15:02:52 +01003906 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3907 if (ret)
Emil Velikovccedf662015-09-09 16:02:18 +01003908 continue;
3909
Emil Velikov95b262f2018-05-15 16:32:10 +01003910 if (i >= MAX_DRM_NODES) {
3911 fprintf(stderr, "More than %d drm nodes detected. "
3912 "Please report a bug - that should not happen.\n"
3913 "Skipping extra nodes\n", MAX_DRM_NODES);
3914 break;
Emil Velikovccedf662015-09-09 16:02:18 +01003915 }
Emil Velikov56e72d32018-06-21 16:06:35 +01003916 local_devices[i] = d;
Emil Velikovccedf662015-09-09 16:02:18 +01003917 i++;
3918 }
3919 node_count = i;
3920
Emil Velikovccedf662015-09-09 16:02:18 +01003921 drmFoldDuplicatedDevices(local_devices, node_count);
3922
Mariusz Ceier4519db22018-07-29 10:20:14 +02003923 *device = NULL;
3924
Emil Velikov56e72d32018-06-21 16:06:35 +01003925 for (i = 0; i < node_count; i++) {
3926 if (!local_devices[i])
3927 continue;
3928
3929 if (drm_device_has_rdev(local_devices[i], find_rdev))
3930 *device = local_devices[i];
3931 else
3932 drmFreeDevice(&local_devices[i]);
3933 }
Emil Velikovccedf662015-09-09 16:02:18 +01003934
Emil Velikovccedf662015-09-09 16:02:18 +01003935 closedir(sysdir);
Rob Herring677cd972016-10-21 10:07:59 -07003936 if (*device == NULL)
Thierry Redinge17cad12016-12-21 18:00:52 +01003937 return -ENODEV;
Emil Velikovccedf662015-09-09 16:02:18 +01003938 return 0;
Jonathan Gray08257922016-12-01 15:18:43 +11003939#endif
Emil Velikovccedf662015-09-09 16:02:18 +01003940}
3941
3942/**
Emil Velikov11687bf2016-11-30 17:24:21 +00003943 * Get information about the opened drm device
3944 *
3945 * \param fd file descriptor of the drm device
3946 * \param device the address of a drmDevicePtr where the information
3947 * will be allocated in stored
3948 *
3949 * \return zero on success, negative error code otherwise.
3950 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003951drm_public int drmGetDevice(int fd, drmDevicePtr *device)
Emil Velikov11687bf2016-11-30 17:24:21 +00003952{
3953 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
3954}
3955
3956/**
Emil Velikovb556ea12015-08-17 11:09:06 +08003957 * Get drm devices on the system
3958 *
Emil Velikov11687bf2016-11-30 17:24:21 +00003959 * \param flags feature/behaviour bitmask
Emil Velikovb556ea12015-08-17 11:09:06 +08003960 * \param devices the array of devices with drmDevicePtr elements
3961 * can be NULL to get the device number first
3962 * \param max_devices the maximum number of devices for the array
3963 *
3964 * \return on error - negative error code,
3965 * if devices is NULL - total number of devices available on the system,
3966 * alternatively the number of devices stored in devices[], which is
3967 * capped by the max_devices.
Emil Velikov11687bf2016-11-30 17:24:21 +00003968 *
3969 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
3970 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
Emil Velikovb556ea12015-08-17 11:09:06 +08003971 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07003972drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
3973 int max_devices)
Emil Velikovb556ea12015-08-17 11:09:06 +08003974{
Emil Velikov95b262f2018-05-15 16:32:10 +01003975 drmDevicePtr local_devices[MAX_DRM_NODES];
Emil Velikov5f68d312015-09-07 13:54:32 +01003976 drmDevicePtr device;
3977 DIR *sysdir;
3978 struct dirent *dent;
Emil Velikovfae59d72015-09-09 17:54:34 +01003979 int ret, i, node_count, device_count;
Emil Velikov11687bf2016-11-30 17:24:21 +00003980
3981 if (drm_device_validate_flags(flags))
3982 return -EINVAL;
Emil Velikovb556ea12015-08-17 11:09:06 +08003983
Emil Velikovb556ea12015-08-17 11:09:06 +08003984 sysdir = opendir(DRM_DIR_NAME);
Emil Velikov95b262f2018-05-15 16:32:10 +01003985 if (!sysdir)
3986 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08003987
Emil Velikov5f68d312015-09-07 13:54:32 +01003988 i = 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003989 while ((dent = readdir(sysdir))) {
Emil Velikovf808fee2018-05-15 15:02:52 +01003990 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
3991 if (ret)
Emil Velikovb556ea12015-08-17 11:09:06 +08003992 continue;
3993
Emil Velikov95b262f2018-05-15 16:32:10 +01003994 if (i >= MAX_DRM_NODES) {
3995 fprintf(stderr, "More than %d drm nodes detected. "
3996 "Please report a bug - that should not happen.\n"
3997 "Skipping extra nodes\n", MAX_DRM_NODES);
3998 break;
Emil Velikov5f68d312015-09-07 13:54:32 +01003999 }
Emil Velikov5f68d312015-09-07 13:54:32 +01004000 local_devices[i] = device;
Emil Velikovb556ea12015-08-17 11:09:06 +08004001 i++;
4002 }
Emil Velikovb556ea12015-08-17 11:09:06 +08004003 node_count = i;
4004
Emil Velikovfae59d72015-09-09 17:54:34 +01004005 drmFoldDuplicatedDevices(local_devices, node_count);
Emil Velikovb556ea12015-08-17 11:09:06 +08004006
Emil Velikov5f68d312015-09-07 13:54:32 +01004007 device_count = 0;
Qiang Yu70b64072016-06-06 12:29:16 -04004008 for (i = 0; i < node_count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04004009 if (!local_devices[i])
4010 continue;
Qiang Yu70b64072016-06-06 12:29:16 -04004011
Emil Velikov5f68d312015-09-07 13:54:32 +01004012 if ((devices != NULL) && (device_count < max_devices))
4013 devices[device_count] = local_devices[i];
4014 else
4015 drmFreeDevice(&local_devices[i]);
4016
4017 device_count++;
Emil Velikovb556ea12015-08-17 11:09:06 +08004018 }
4019
Emil Velikovb556ea12015-08-17 11:09:06 +08004020 closedir(sysdir);
Emil Velikov5f68d312015-09-07 13:54:32 +01004021 return device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08004022}
Emil Velikov37d790f2016-11-10 17:26:50 +00004023
Emil Velikov11687bf2016-11-30 17:24:21 +00004024/**
4025 * Get drm devices on the system
4026 *
4027 * \param devices the array of devices with drmDevicePtr elements
4028 * can be NULL to get the device number first
4029 * \param max_devices the maximum number of devices for the array
4030 *
4031 * \return on error - negative error code,
4032 * if devices is NULL - total number of devices available on the system,
4033 * alternatively the number of devices stored in devices[], which is
4034 * capped by the max_devices.
4035 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004036drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
Emil Velikov11687bf2016-11-30 17:24:21 +00004037{
4038 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4039}
4040
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004041drm_public char *drmGetDeviceNameFromFd2(int fd)
Emil Velikov37d790f2016-11-10 17:26:50 +00004042{
4043#ifdef __linux__
4044 struct stat sbuf;
Thierry Reding5403cb32017-01-18 08:29:23 +01004045 char path[PATH_MAX + 1], *value;
Emil Velikov37d790f2016-11-10 17:26:50 +00004046 unsigned int maj, min;
Emil Velikov37d790f2016-11-10 17:26:50 +00004047
4048 if (fstat(fd, &sbuf))
4049 return NULL;
4050
4051 maj = major(sbuf.st_rdev);
4052 min = minor(sbuf.st_rdev);
4053
Thomas Hellstromf8392582018-08-31 13:47:05 +02004054 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Emil Velikov37d790f2016-11-10 17:26:50 +00004055 return NULL;
4056
Thierry Reding5403cb32017-01-18 08:29:23 +01004057 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4058
4059 value = sysfs_uevent_get(path, "DEVNAME");
4060 if (!value)
Emil Velikov37d790f2016-11-10 17:26:50 +00004061 return NULL;
4062
Thierry Reding5403cb32017-01-18 08:29:23 +01004063 snprintf(path, sizeof(path), "/dev/%s", value);
4064 free(value);
Emil Velikov37d790f2016-11-10 17:26:50 +00004065
Thierry Reding5403cb32017-01-18 08:29:23 +01004066 return strdup(path);
Emil Velikov37d790f2016-11-10 17:26:50 +00004067#else
Jonathan Graye2e766d2016-12-17 16:09:52 +11004068 struct stat sbuf;
4069 char node[PATH_MAX + 1];
4070 const char *dev_name;
4071 int node_type;
Jonathan Gray293b95e2019-05-13 02:52:04 +10004072 int maj, min, n;
Jonathan Graye2e766d2016-12-17 16:09:52 +11004073
4074 if (fstat(fd, &sbuf))
4075 return NULL;
4076
4077 maj = major(sbuf.st_rdev);
4078 min = minor(sbuf.st_rdev);
4079
Thomas Hellstromf8392582018-08-31 13:47:05 +02004080 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Jonathan Graye2e766d2016-12-17 16:09:52 +11004081 return NULL;
4082
4083 node_type = drmGetMinorType(min);
4084 if (node_type == -1)
4085 return NULL;
4086
Eric Engestrom331e51e2018-12-19 13:53:41 +00004087 dev_name = drmGetDeviceName(node_type);
4088 if (!dev_name)
Jonathan Graye2e766d2016-12-17 16:09:52 +11004089 return NULL;
Jonathan Graye2e766d2016-12-17 16:09:52 +11004090
Jonathan Gray293b95e2019-05-13 02:52:04 +10004091 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
Jonathan Graye2e766d2016-12-17 16:09:52 +11004092 if (n == -1 || n >= PATH_MAX)
4093 return NULL;
4094
4095 return strdup(node);
Emil Velikov37d790f2016-11-10 17:26:50 +00004096#endif
4097}
Dave Airliefc492272017-06-17 11:01:01 +10004098
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004099drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
Dave Airliefc492272017-06-17 11:01:01 +10004100{
4101 struct drm_syncobj_create args;
4102 int ret;
4103
4104 memclear(args);
4105 args.flags = flags;
4106 args.handle = 0;
4107 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4108 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004109 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004110 *handle = args.handle;
4111 return 0;
4112}
4113
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004114drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
Dave Airliefc492272017-06-17 11:01:01 +10004115{
4116 struct drm_syncobj_destroy args;
4117
4118 memclear(args);
4119 args.handle = handle;
4120 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4121}
4122
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004123drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
Dave Airliefc492272017-06-17 11:01:01 +10004124{
4125 struct drm_syncobj_handle args;
4126 int ret;
4127
4128 memclear(args);
4129 args.fd = -1;
4130 args.handle = handle;
4131 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4132 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004133 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004134 *obj_fd = args.fd;
4135 return 0;
4136}
4137
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004138drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
Dave Airliefc492272017-06-17 11:01:01 +10004139{
4140 struct drm_syncobj_handle args;
4141 int ret;
4142
4143 memclear(args);
4144 args.fd = obj_fd;
4145 args.handle = 0;
4146 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4147 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004148 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004149 *handle = args.handle;
4150 return 0;
4151}
4152
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004153drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4154 int sync_file_fd)
Dave Airliefc492272017-06-17 11:01:01 +10004155{
4156 struct drm_syncobj_handle args;
4157
4158 memclear(args);
4159 args.fd = sync_file_fd;
4160 args.handle = handle;
4161 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4162 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4163}
4164
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004165drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4166 int *sync_file_fd)
Dave Airliefc492272017-06-17 11:01:01 +10004167{
4168 struct drm_syncobj_handle args;
4169 int ret;
4170
4171 memclear(args);
4172 args.fd = -1;
4173 args.handle = handle;
4174 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4175 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4176 if (ret)
Dave Airlie61ff9772017-10-25 07:43:56 +01004177 return ret;
Dave Airliefc492272017-06-17 11:01:01 +10004178 *sync_file_fd = args.fd;
4179 return 0;
4180}
Marek Olšák2048a9e2017-09-11 21:57:32 +02004181
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004182drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4183 int64_t timeout_nsec, unsigned flags,
4184 uint32_t *first_signaled)
Marek Olšák2048a9e2017-09-11 21:57:32 +02004185{
Dave Airlie61ff9772017-10-25 07:43:56 +01004186 struct drm_syncobj_wait args;
4187 int ret;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004188
Dave Airlie61ff9772017-10-25 07:43:56 +01004189 memclear(args);
Bas Nieuwenhuizenb1e63d92018-02-06 11:21:35 +01004190 args.handles = (uintptr_t)handles;
Dave Airlie61ff9772017-10-25 07:43:56 +01004191 args.timeout_nsec = timeout_nsec;
4192 args.count_handles = num_handles;
4193 args.flags = flags;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004194
Dave Airlie61ff9772017-10-25 07:43:56 +01004195 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4196 if (ret < 0)
Chunming Zhoubde3b9b2018-02-07 11:22:32 +08004197 return -errno;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004198
Dave Airlie61ff9772017-10-25 07:43:56 +01004199 if (first_signaled)
4200 *first_signaled = args.first_signaled;
4201 return ret;
Marek Olšák2048a9e2017-09-11 21:57:32 +02004202}
Bas Nieuwenhuizen1abcced2017-12-17 00:27:09 +01004203
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004204drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4205 uint32_t handle_count)
Bas Nieuwenhuizen1abcced2017-12-17 00:27:09 +01004206{
4207 struct drm_syncobj_array args;
4208 int ret;
4209
4210 memclear(args);
4211 args.handles = (uintptr_t)handles;
4212 args.count_handles = handle_count;
4213
4214 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4215 return ret;
4216}
4217
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07004218drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4219 uint32_t handle_count)
Bas Nieuwenhuizen1abcced2017-12-17 00:27:09 +01004220{
4221 struct drm_syncobj_array args;
4222 int ret;
4223
4224 memclear(args);
4225 args.handles = (uintptr_t)handles;
4226 args.count_handles = handle_count;
4227
4228 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4229 return ret;
4230}
Chunming Zhouec6ae512019-05-16 16:07:09 +08004231
Chunming Zhou12712eb2019-05-16 16:07:11 +08004232drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4233 uint64_t *points, uint32_t handle_count)
4234{
4235 struct drm_syncobj_timeline_array args;
4236 int ret;
4237
4238 memclear(args);
4239 args.handles = (uintptr_t)handles;
4240 args.points = (uintptr_t)points;
4241 args.count_handles = handle_count;
4242
4243 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4244 return ret;
4245}
4246
Chunming Zhouec6ae512019-05-16 16:07:09 +08004247drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4248 unsigned num_handles,
4249 int64_t timeout_nsec, unsigned flags,
4250 uint32_t *first_signaled)
4251{
4252 struct drm_syncobj_timeline_wait args;
4253 int ret;
4254
4255 memclear(args);
4256 args.handles = (uintptr_t)handles;
4257 args.points = (uintptr_t)points;
4258 args.timeout_nsec = timeout_nsec;
4259 args.count_handles = num_handles;
4260 args.flags = flags;
4261
4262 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4263 if (ret < 0)
4264 return -errno;
4265
4266 if (first_signaled)
4267 *first_signaled = args.first_signaled;
4268 return ret;
4269}
4270
4271
4272drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4273 uint32_t handle_count)
4274{
4275 struct drm_syncobj_timeline_array args;
4276 int ret;
4277
4278 memclear(args);
4279 args.handles = (uintptr_t)handles;
4280 args.points = (uintptr_t)points;
4281 args.count_handles = handle_count;
4282
4283 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4284 if (ret)
4285 return ret;
4286 return 0;
4287}
4288
Chunming Zhou0a7ad7d2019-07-24 15:55:20 +08004289drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
4290 uint32_t handle_count, uint32_t flags)
4291{
4292 struct drm_syncobj_timeline_array args;
4293
4294 memclear(args);
4295 args.handles = (uintptr_t)handles;
4296 args.points = (uintptr_t)points;
4297 args.count_handles = handle_count;
4298 args.flags = flags;
4299
4300 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4301}
4302
4303
Chunming Zhou12712eb2019-05-16 16:07:11 +08004304drm_public int drmSyncobjTransfer(int fd,
4305 uint32_t dst_handle, uint64_t dst_point,
4306 uint32_t src_handle, uint64_t src_point,
4307 uint32_t flags)
4308{
4309 struct drm_syncobj_transfer args;
4310 int ret;
Chunming Zhouec6ae512019-05-16 16:07:09 +08004311
Chunming Zhou12712eb2019-05-16 16:07:11 +08004312 memclear(args);
4313 args.src_handle = src_handle;
4314 args.dst_handle = dst_handle;
4315 args.src_point = src_point;
4316 args.dst_point = dst_point;
4317 args.flags = flags;
4318
4319 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);
4320
4321 return ret;
4322}