blob: c788c9325ee44fc811da5b5de45be335eaf3d0bf [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#ifdef HAVE_CONFIG_H
35# include <config.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000036#endif
Dave Airlie79038752006-11-08 15:08:09 +110037#include <stdio.h>
38#include <stdlib.h>
Emil Velikovfae59d72015-09-09 17:54:34 +010039#include <stdbool.h>
Dave Airlie79038752006-11-08 15:08:09 +110040#include <unistd.h>
41#include <string.h>
Ian Romanick015efd12009-07-06 09:23:59 -070042#include <strings.h>
Dave Airlie79038752006-11-08 15:08:09 +110043#include <ctype.h>
Emil Velikov0ca03a42015-03-07 00:58:39 +000044#include <dirent.h>
45#include <stddef.h>
Dave Airlie79038752006-11-08 15:08:09 +110046#include <fcntl.h>
47#include <errno.h>
Felix Janda4031dc12015-09-26 08:08:43 +020048#include <limits.h>
Dave Airlie79038752006-11-08 15:08:09 +110049#include <signal.h>
Jesse Barnesf4f76a62009-01-07 10:18:08 -080050#include <time.h>
Dave Airlie79038752006-11-08 15:08:09 +110051#include <sys/types.h>
52#include <sys/stat.h>
53#define stat_t struct stat
54#include <sys/ioctl.h>
Dave Airlie79038752006-11-08 15:08:09 +110055#include <sys/time.h>
56#include <stdarg.h>
Mike Frysinger8c8d5dd2016-06-21 12:18:15 -040057#ifdef MAJOR_IN_MKDEV
58#include <sys/mkdev.h>
59#endif
60#ifdef MAJOR_IN_SYSMACROS
61#include <sys/sysmacros.h>
Alan Coopersmith0e1135d2015-03-07 11:44:32 -080062#endif
Emil Velikovb556ea12015-08-17 11:09:06 +080063#include <math.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000064
65/* Not all systems have MAP_FAILED defined */
66#ifndef MAP_FAILED
67#define MAP_FAILED ((void *)-1)
68#endif
69
Daryll Straussb3a57661999-12-05 01:19:48 +000070#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010071#include "libdrm_macros.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000072
Emil Velikov5f68d312015-09-07 13:54:32 +010073#include "util_math.h"
74
Jonathan Grayfc083322015-07-21 03:12:56 +100075#ifdef __OpenBSD__
Jan Vesely50d3c852016-06-30 14:22:52 -040076#define DRM_PRIMARY_MINOR_NAME "drm"
77#define DRM_CONTROL_MINOR_NAME "drmC"
78#define DRM_RENDER_MINOR_NAME "drmR"
Jonathan Grayfc083322015-07-21 03:12:56 +100079#else
Jan Vesely50d3c852016-06-30 14:22:52 -040080#define DRM_PRIMARY_MINOR_NAME "card"
81#define DRM_CONTROL_MINOR_NAME "controlD"
82#define DRM_RENDER_MINOR_NAME "renderD"
Jonathan Grayfc083322015-07-21 03:12:56 +100083#endif
84
Hasso Tepper27c37852008-04-07 15:27:43 +030085#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Eric Anholtcfa778a2003-02-21 23:23:09 +000086#define DRM_MAJOR 145
Rik Faith88dbee52001-02-28 09:27:44 +000087#endif
88
Eric Anholtcfa778a2003-02-21 23:23:09 +000089#ifdef __NetBSD__
90#define DRM_MAJOR 34
91#endif
92
Jonathan Gray66c3afb2015-07-21 03:12:10 +100093#ifdef __OpenBSD__
94#ifdef __i386__
95#define DRM_MAJOR 88
96#else
97#define DRM_MAJOR 87
98#endif
99#endif /* __OpenBSD__ */
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000100
Eric Anholtcfa778a2003-02-21 23:23:09 +0000101#ifndef DRM_MAJOR
Jan Vesely50d3c852016-06-30 14:22:52 -0400102#define DRM_MAJOR 226 /* Linux */
Rik Faith88dbee52001-02-28 09:27:44 +0000103#endif
104
David Dawes56bd9c22001-07-30 19:59:39 +0000105#define DRM_MSG_VERBOSITY 3
106
Daniel Vetterfd387942015-02-11 12:41:04 +0100107#define memclear(s) memset(&s, 0, sizeof(s))
108
Dave Airlie79038752006-11-08 15:08:09 +1100109static drmServerInfoPtr drm_server_info;
110
111void drmSetServerInfo(drmServerInfoPtr info)
112{
Brianccd7b6e2007-05-29 14:54:00 -0600113 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +1100114}
115
Jose Fonsecad2443b22003-05-27 00:37:33 +0000116/**
117 * Output a message to stderr.
118 *
119 * \param format printf() like format string.
120 *
121 * \internal
122 * This function is a wrapper around vfprintf().
123 */
Dave Airlie79038752006-11-08 15:08:09 +1100124
Thierry Reding44b08c02014-01-22 12:06:51 +0100125static int DRM_PRINTFLIKE(1, 0)
126drmDebugPrint(const char *format, va_list ap)
Dave Airlie79038752006-11-08 15:08:09 +1100127{
Brianccd7b6e2007-05-29 14:54:00 -0600128 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100129}
130
Eric Anholtc4857422008-06-03 10:20:49 -0700131void
David Dawes56bd9c22001-07-30 19:59:39 +0000132drmMsg(const char *format, ...)
133{
Jan Vesely50d3c852016-06-30 14:22:52 -0400134 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000135 const char *env;
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400136 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
137 (drm_server_info && drm_server_info->debug_print))
David Dawes56bd9c22001-07-30 19:59:39 +0000138 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400139 va_start(ap, format);
140 if (drm_server_info) {
141 drm_server_info->debug_print(format,ap);
142 } else {
143 drmDebugPrint(format, ap);
144 }
145 va_end(ap);
David Dawes56bd9c22001-07-30 19:59:39 +0000146 }
147}
148
Daryll Straussb3a57661999-12-05 01:19:48 +0000149static void *drmHashTable = NULL; /* Context switch callbacks */
150
Dave Airlie79038752006-11-08 15:08:09 +1100151void *drmGetHashTable(void)
152{
Brianccd7b6e2007-05-29 14:54:00 -0600153 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100154}
Daryll Straussb3a57661999-12-05 01:19:48 +0000155
156void *drmMalloc(int size)
157{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100158 return calloc(1, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000159}
160
161void drmFree(void *pt)
162{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100163 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000164}
165
Keith Packard8b9ab102008-06-13 16:03:22 -0700166/**
167 * Call ioctl, restarting if it is interupted
168 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800169int
Coleman Kane41b83a92008-08-18 17:08:21 -0400170drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700171{
Jan Vesely50d3c852016-06-30 14:22:52 -0400172 int ret;
Keith Packard8b9ab102008-06-13 16:03:22 -0700173
174 do {
Jan Vesely50d3c852016-06-30 14:22:52 -0400175 ret = ioctl(fd, request, arg);
Keith Packard8b9ab102008-06-13 16:03:22 -0700176 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
177 return ret;
178}
Daryll Straussb3a57661999-12-05 01:19:48 +0000179
Daryll Straussb3a57661999-12-05 01:19:48 +0000180static unsigned long drmGetKeyFromFd(int fd)
181{
David Dawesfcc21062001-03-30 17:16:20 +0000182 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000183
184 st.st_rdev = 0;
185 fstat(fd, &st);
186 return st.st_rdev;
187}
188
Dave Airlie79038752006-11-08 15:08:09 +1100189drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000190{
191 unsigned long key = drmGetKeyFromFd(fd);
192 void *value;
193 drmHashEntry *entry;
194
Brianccd7b6e2007-05-29 14:54:00 -0600195 if (!drmHashTable)
Jan Vesely50d3c852016-06-30 14:22:52 -0400196 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000197
198 if (drmHashLookup(drmHashTable, key, &value)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400199 entry = drmMalloc(sizeof(*entry));
200 entry->fd = fd;
201 entry->f = NULL;
202 entry->tagTable = drmHashCreate();
203 drmHashInsert(drmHashTable, key, entry);
Daryll Straussb3a57661999-12-05 01:19:48 +0000204 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -0400205 entry = value;
Daryll Straussb3a57661999-12-05 01:19:48 +0000206 }
207 return entry;
208}
209
Jose Fonsecad2443b22003-05-27 00:37:33 +0000210/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000211 * Compare two busid strings
212 *
213 * \param first
214 * \param second
215 *
216 * \return 1 if matched.
217 *
218 * \internal
219 * This function compares two bus ID strings. It understands the older
220 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
221 * domain, b is bus, d is device, f is function.
222 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000223static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000224{
225 /* First, check if the IDs are exactly the same */
226 if (strcasecmp(id1, id2) == 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400227 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000228
229 /* Try to match old/new-style PCI bus IDs. */
230 if (strncasecmp(id1, "pci", 3) == 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400231 unsigned int o1, b1, d1, f1;
232 unsigned int o2, b2, d2, f2;
233 int ret;
Eric Anholt06cb1322003-10-23 02:23:31 +0000234
Jan Vesely50d3c852016-06-30 14:22:52 -0400235 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
236 if (ret != 4) {
237 o1 = 0;
238 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
239 if (ret != 3)
240 return 0;
241 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000242
Jan Vesely50d3c852016-06-30 14:22:52 -0400243 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
244 if (ret != 4) {
245 o2 = 0;
246 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
247 if (ret != 3)
248 return 0;
249 }
Eric Anholt06cb1322003-10-23 02:23:31 +0000250
Jan Vesely50d3c852016-06-30 14:22:52 -0400251 /* If domains aren't properly supported by the kernel interface,
252 * just ignore them, which sucks less than picking a totally random
253 * card with "open by name"
254 */
255 if (!pci_domain_ok)
256 o1 = o2 = 0;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000257
Jan Vesely50d3c852016-06-30 14:22:52 -0400258 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
259 return 0;
260 else
261 return 1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000262 }
263 return 0;
264}
265
266/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300267 * Handles error checking for chown call.
268 *
269 * \param path to file.
270 * \param id of the new owner.
271 * \param id of the new group.
272 *
273 * \return zero if success or -1 if failure.
274 *
275 * \internal
276 * Checks for failure. If failure was caused by signal call chown again.
277 * If any other failure happened then it will output error mesage using
278 * drmMsg() call.
279 */
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500280#if !defined(UDEV)
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300281static int chown_check_return(const char *path, uid_t owner, gid_t group)
282{
Jan Vesely50d3c852016-06-30 14:22:52 -0400283 int rv;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300284
Jan Vesely50d3c852016-06-30 14:22:52 -0400285 do {
286 rv = chown(path, owner, group);
287 } while (rv != 0 && errno == EINTR);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300288
Jan Vesely50d3c852016-06-30 14:22:52 -0400289 if (rv == 0)
290 return 0;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300291
Jan Vesely50d3c852016-06-30 14:22:52 -0400292 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
293 path, errno, strerror(errno));
294 return -1;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300295}
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500296#endif
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300297
298/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000299 * Open the DRM device, creating it if necessary.
300 *
301 * \param dev major and minor numbers of the device.
302 * \param minor minor number of the device.
Jan Vesely50d3c852016-06-30 14:22:52 -0400303 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000304 * \return a file descriptor on success, or a negative value on error.
305 *
306 * \internal
307 * Assembles the device name from \p minor and opens it, creating the device
308 * special file node with the major and minor numbers specified by \p dev and
309 * parent directory if necessary and was called by root.
310 */
Jan Veselyde8532d2014-11-30 12:53:18 -0500311static int drmOpenDevice(dev_t dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000312{
David Dawes9c775d02001-05-14 14:49:58 +0000313 stat_t st;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000314 const char *dev_name;
Rik Faith88dbee52001-02-28 09:27:44 +0000315 char buf[64];
316 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100317 mode_t devmode = DRM_DEV_MODE, serv_mode;
Jan Vesely0706c142015-02-27 12:47:46 -0500318 gid_t serv_group;
319#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000320 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000321 uid_t user = DRM_DEV_UID;
Jan Vesely0706c142015-02-27 12:47:46 -0500322 gid_t group = DRM_DEV_GID;
323#endif
324
Frank Binns0c5aaee2015-01-14 14:07:51 +0000325 switch (type) {
326 case DRM_NODE_PRIMARY:
Jan Vesely50d3c852016-06-30 14:22:52 -0400327 dev_name = DRM_DEV_NAME;
328 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000329 case DRM_NODE_CONTROL:
Jan Vesely50d3c852016-06-30 14:22:52 -0400330 dev_name = DRM_CONTROL_DEV_NAME;
331 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000332 case DRM_NODE_RENDER:
Jan Vesely50d3c852016-06-30 14:22:52 -0400333 dev_name = DRM_RENDER_DEV_NAME;
334 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000335 default:
Jan Vesely50d3c852016-06-30 14:22:52 -0400336 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000337 };
338
339 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000340 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000341
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400342 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400343 drm_server_info->get_perms(&serv_group, &serv_mode);
344 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
345 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Dave Airlie79038752006-11-08 15:08:09 +1100346 }
Brian Paul569da5a2000-06-08 14:38:22 +0000347
Dave Airlie9101a022008-08-24 16:54:43 +1000348#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000349 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400350 if (!isroot)
351 return DRM_ERR_NOT_ROOT;
352 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
353 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
354 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000355 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000356
Eric Anholt06cb1322003-10-23 02:23:31 +0000357 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000358 if (stat(buf, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400359 if (!isroot)
360 return DRM_ERR_NOT_ROOT;
361 remove(buf);
362 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000363 }
Dave Airlie79038752006-11-08 15:08:09 +1100364
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400365 if (drm_server_info && drm_server_info->get_perms) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400366 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
367 chown_check_return(buf, user, group);
368 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100369 }
Dave Airlie9101a022008-08-24 16:54:43 +1000370#else
371 /* if we modprobed then wait for udev */
372 {
Jan Vesely50d3c852016-06-30 14:22:52 -0400373 int udev_count = 0;
Dave Airlie9101a022008-08-24 16:54:43 +1000374wait_for_udev:
375 if (stat(DRM_DIR_NAME, &st)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400376 usleep(20);
377 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000378
Jan Vesely50d3c852016-06-30 14:22:52 -0400379 if (udev_count == 50)
380 return -1;
381 goto wait_for_udev;
382 }
Dave Airlie9101a022008-08-24 16:54:43 +1000383
Jan Vesely50d3c852016-06-30 14:22:52 -0400384 if (stat(buf, &st)) {
385 usleep(20);
386 udev_count++;
Dave Airlie9101a022008-08-24 16:54:43 +1000387
Jan Vesely50d3c852016-06-30 14:22:52 -0400388 if (udev_count == 50)
389 return -1;
390 goto wait_for_udev;
391 }
Dave Airlie9101a022008-08-24 16:54:43 +1000392 }
393#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000394
David Dawes56bd9c22001-07-30 19:59:39 +0000395 fd = open(buf, O_RDWR, 0);
396 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400397 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600398 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400399 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000400
Dave Airlie39e5e982010-12-07 14:26:09 +1000401#if !defined(UDEV)
Eric Anholt06cb1322003-10-23 02:23:31 +0000402 /* Check if the device node is not what we expect it to be, and recreate it
403 * and try again if so.
404 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000405 if (st.st_rdev != dev) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400406 if (!isroot)
407 return DRM_ERR_NOT_ROOT;
408 remove(buf);
409 mknod(buf, S_IFCHR | devmode, dev);
410 if (drm_server_info && drm_server_info->get_perms) {
411 chown_check_return(buf, user, group);
412 chmod(buf, devmode);
413 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000414 }
415 fd = open(buf, O_RDWR, 0);
416 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Jan Vesely50d3c852016-06-30 14:22:52 -0400417 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600418 if (fd >= 0)
Jan Vesely50d3c852016-06-30 14:22:52 -0400419 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000420
David Dawes56bd9c22001-07-30 19:59:39 +0000421 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000422 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000423#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000424 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000425}
426
Jose Fonsecad2443b22003-05-27 00:37:33 +0000427
428/**
429 * Open the DRM device
430 *
431 * \param minor device minor number.
432 * \param create allow to create the device if set.
433 *
434 * \return a file descriptor on success, or a negative value on error.
Jan Vesely50d3c852016-06-30 14:22:52 -0400435 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000436 * \internal
437 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
438 * name from \p minor and opens it.
439 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800440static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000441{
442 int fd;
443 char buf[64];
Frank Binns0c5aaee2015-01-14 14:07:51 +0000444 const char *dev_name;
Jan Vesely50d3c852016-06-30 14:22:52 -0400445
Brianccd7b6e2007-05-29 14:54:00 -0600446 if (create)
Jan Vesely50d3c852016-06-30 14:22:52 -0400447 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
448
Frank Binns0c5aaee2015-01-14 14:07:51 +0000449 switch (type) {
450 case DRM_NODE_PRIMARY:
Jan Vesely50d3c852016-06-30 14:22:52 -0400451 dev_name = DRM_DEV_NAME;
452 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000453 case DRM_NODE_CONTROL:
Jan Vesely50d3c852016-06-30 14:22:52 -0400454 dev_name = DRM_CONTROL_DEV_NAME;
455 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000456 case DRM_NODE_RENDER:
Jan Vesely50d3c852016-06-30 14:22:52 -0400457 dev_name = DRM_RENDER_DEV_NAME;
458 break;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000459 default:
Jan Vesely50d3c852016-06-30 14:22:52 -0400460 return -EINVAL;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000461 };
462
463 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Brianccd7b6e2007-05-29 14:54:00 -0600464 if ((fd = open(buf, O_RDWR, 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 */
Brian Paul569da5a2000-06-08 14:38:22 +0000480int drmAvailable(void)
481{
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 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000715int 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 */
736int drmOpenWithType(const char *name, const char *busid, int type)
737{
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400738 if (!drmAvailable() && name != NULL && drm_server_info &&
739 drm_server_info->load_module) {
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
Jesse Barnes731cd552008-12-17 10:09:49 -0800759int drmOpenControl(int minor)
760{
761 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
762}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000763
Frank Binns0c5aaee2015-01-14 14:07:51 +0000764int drmOpenRender(int minor)
765{
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 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000778void drmFreeVersion(drmVersionPtr v)
779{
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 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000848drmVersionPtr drmGetVersion(int fd)
849{
850 drmVersionPtr retval;
851 drm_version_t *version = drmMalloc(sizeof(*version));
852
Daniel Vetter95f23cf2015-02-11 17:25:30 +0100853 memclear(*version);
Gareth Hughes36047532001-02-15 08:12:14 +0000854
Keith Packard8b9ab102008-06-13 16:03:22 -0700855 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400856 drmFreeKernelVersion(version);
857 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000858 }
859
Daryll Straussb3a57661999-12-05 01:19:48 +0000860 if (version->name_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400861 version->name = drmMalloc(version->name_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000862 if (version->date_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400863 version->date = drmMalloc(version->date_len + 1);
Daryll Straussb3a57661999-12-05 01:19:48 +0000864 if (version->desc_len)
Jan Vesely50d3c852016-06-30 14:22:52 -0400865 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000866
Keith Packard8b9ab102008-06-13 16:03:22 -0700867 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Jan Vesely50d3c852016-06-30 14:22:52 -0400868 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
869 drmFreeKernelVersion(version);
870 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +0000871 }
872
Adam Jackson22e41ef2006-02-20 23:09:00 +0000873 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000874 if (version->name_len) version->name[version->name_len] = '\0';
875 if (version->date_len) version->date[version->date_len] = '\0';
876 if (version->desc_len) version->desc[version->desc_len] = '\0';
877
Daryll Straussb3a57661999-12-05 01:19:48 +0000878 retval = drmMalloc(sizeof(*retval));
879 drmCopyVersion(retval, version);
880 drmFreeKernelVersion(version);
881 return retval;
882}
883
Jens Owen3903e5a2002-04-09 21:54:56 +0000884
Jose Fonsecad2443b22003-05-27 00:37:33 +0000885/**
886 * Get version information for the DRM user space library.
Jan Vesely50d3c852016-06-30 14:22:52 -0400887 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000888 * This version number is driver independent.
Jan Vesely50d3c852016-06-30 14:22:52 -0400889 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000890 * \param fd file descriptor.
891 *
892 * \return version information.
Jan Vesely50d3c852016-06-30 14:22:52 -0400893 *
Jose Fonsecad2443b22003-05-27 00:37:33 +0000894 * \internal
895 * This function allocates and fills a drm_version structure with a hard coded
896 * version number.
897 */
Jens Owen3903e5a2002-04-09 21:54:56 +0000898drmVersionPtr drmGetLibVersion(int fd)
899{
900 drm_version_t *version = drmMalloc(sizeof(*version));
901
902 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100903 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000904 * revision 1.0.x = original DRM interface with no drmGetLibVersion
905 * entry point and many drm<Device> extensions
906 * revision 1.1.x = added drmCommand entry points for device extensions
907 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000908 * revision 1.2.x = added drmSetInterfaceVersion
909 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100910 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000911 */
Dave Airlie79038752006-11-08 15:08:09 +1100912 version->version_major = 1;
913 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000914 version->version_patchlevel = 0;
915
916 return (drmVersionPtr)version;
917}
918
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000919int drmGetCap(int fd, uint64_t capability, uint64_t *value)
920{
Jan Vesely50d3c852016-06-30 14:22:52 -0400921 struct drm_get_cap cap;
922 int ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000923
Jan Vesely50d3c852016-06-30 14:22:52 -0400924 memclear(cap);
925 cap.capability = capability;
Daniel Vetterfd387942015-02-11 12:41:04 +0100926
Jan Vesely50d3c852016-06-30 14:22:52 -0400927 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
928 if (ret)
929 return ret;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000930
Jan Vesely50d3c852016-06-30 14:22:52 -0400931 *value = cap.value;
932 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000933}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000934
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100935int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
936{
Jan Vesely50d3c852016-06-30 14:22:52 -0400937 struct drm_set_client_cap cap;
Daniel Vetterfd387942015-02-11 12:41:04 +0100938
Jan Vesely50d3c852016-06-30 14:22:52 -0400939 memclear(cap);
940 cap.capability = capability;
941 cap.value = value;
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100942
Jan Vesely50d3c852016-06-30 14:22:52 -0400943 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100944}
945
Jose Fonsecad2443b22003-05-27 00:37:33 +0000946/**
947 * Free the bus ID information.
948 *
949 * \param busid bus ID information string as given by drmGetBusid().
950 *
951 * \internal
952 * This function is just frees the memory pointed by \p busid.
953 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000954void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000955{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000956 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000957}
958
Jose Fonsecad2443b22003-05-27 00:37:33 +0000959
960/**
961 * Get the bus ID of the device.
962 *
963 * \param fd file descriptor.
964 *
965 * \return bus ID string.
966 *
967 * \internal
968 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
969 * get the string length and data, passing the arguments in a drm_unique
970 * structure.
971 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000972char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000973{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000974 drm_unique_t u;
975
Daniel Vetterfd387942015-02-11 12:41:04 +0100976 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000977
Keith Packard8b9ab102008-06-13 16:03:22 -0700978 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Jan Vesely50d3c852016-06-30 14:22:52 -0400979 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000980 u.unique = drmMalloc(u.unique_len + 1);
Keith Packard8b9ab102008-06-13 16:03:22 -0700981 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Jan Vesely50d3c852016-06-30 14:22:52 -0400982 return NULL;
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 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001001int 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
Jon Smirl8696e712004-07-07 04:36:36 +00001015int 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
Jon Smirl8696e712004-07-07 04:36:36 +00001028int 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 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001089int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
Jan Vesely50d3c852016-06-30 14:22:52 -04001090 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
Jon Smirl8696e712004-07-07 04:36:36 +00001106int 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001134int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
Jan Vesely50d3c852016-06-30 14:22:52 -04001135 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
1150int drmMarkBufs(int fd, double low, double high)
1151{
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 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001201int drmFreeBufs(int fd, int count, int *list)
1202{
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 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001222int 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 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001253int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001254{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001255 static unsigned long pagesize_mask = 0;
1256
Brianccd7b6e2007-05-29 14:54:00 -06001257 if (fd < 0)
Jan Vesely50d3c852016-06-30 14:22:52 -04001258 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001259
1260 if (!pagesize_mask)
Jan Vesely50d3c852016-06-30 14:22:52 -04001261 pagesize_mask = getpagesize() - 1;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001262
1263 size = (size + pagesize_mask) & ~pagesize_mask;
1264
Emil Velikovfaf51d52014-09-07 20:03:05 +01001265 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001266 if (*address == MAP_FAILED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001267 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001268 return 0;
1269}
1270
Jose Fonsecad2443b22003-05-27 00:37:33 +00001271
1272/**
1273 * Unmap mappings obtained with drmMap().
1274 *
1275 * \param address address as given by drmMap().
1276 * \param size size in bytes. Must match the size used by drmMap().
Jan Vesely50d3c852016-06-30 14:22:52 -04001277 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001278 * \return zero on success, or a negative value on failure.
1279 *
1280 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001281 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001282 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001283int drmUnmap(drmAddress address, drmSize size)
1284{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001285 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001286}
1287
1288drmBufInfoPtr drmGetBufInfo(int fd)
1289{
1290 drm_buf_info_t info;
1291 drmBufInfoPtr retval;
1292 int i;
1293
Daniel Vetterfd387942015-02-11 12:41:04 +01001294 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001295
Keith Packard8b9ab102008-06-13 16:03:22 -07001296 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Jan Vesely50d3c852016-06-30 14:22:52 -04001297 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001298
1299 if (info.count) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001300 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1301 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001302
Jan Vesely50d3c852016-06-30 14:22:52 -04001303 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1304 drmFree(info.list);
1305 return NULL;
1306 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001307
Jan Vesely50d3c852016-06-30 14:22:52 -04001308 retval = drmMalloc(sizeof(*retval));
1309 retval->count = info.count;
1310 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1311 for (i = 0; i < info.count; i++) {
1312 retval->list[i].count = info.list[i].count;
1313 retval->list[i].size = info.list[i].size;
1314 retval->list[i].low_mark = info.list[i].low_mark;
1315 retval->list[i].high_mark = info.list[i].high_mark;
1316 }
1317 drmFree(info.list);
1318 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001319 }
1320 return NULL;
1321}
1322
Jose Fonsecad2443b22003-05-27 00:37:33 +00001323/**
1324 * Map all DMA buffers into client-virtual space.
1325 *
1326 * \param fd file descriptor.
1327 *
1328 * \return a pointer to a ::drmBufMap structure.
1329 *
1330 * \note The client may not use these buffers until obtaining buffer indices
1331 * with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001332 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001333 * \internal
1334 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1335 * information about the buffers in a drm_buf_map structure into the
1336 * client-visible data structures.
Jan Vesely50d3c852016-06-30 14:22:52 -04001337 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001338drmBufMapPtr drmMapBufs(int fd)
1339{
1340 drm_buf_map_t bufs;
1341 drmBufMapPtr retval;
1342 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001343
Daniel Vetterfd387942015-02-11 12:41:04 +01001344 memclear(bufs);
Keith Packard8b9ab102008-06-13 16:03:22 -07001345 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Jan Vesely50d3c852016-06-30 14:22:52 -04001346 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001347
Brianccd7b6e2007-05-29 14:54:00 -06001348 if (!bufs.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001349 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001350
Jan Vesely50d3c852016-06-30 14:22:52 -04001351 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1352 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001353
Jan Vesely50d3c852016-06-30 14:22:52 -04001354 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1355 drmFree(bufs.list);
1356 return NULL;
1357 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001358
Jan Vesely50d3c852016-06-30 14:22:52 -04001359 retval = drmMalloc(sizeof(*retval));
1360 retval->count = bufs.count;
1361 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1362 for (i = 0; i < bufs.count; i++) {
1363 retval->list[i].idx = bufs.list[i].idx;
1364 retval->list[i].total = bufs.list[i].total;
1365 retval->list[i].used = 0;
1366 retval->list[i].address = bufs.list[i].address;
1367 }
Jon Smirl8696e712004-07-07 04:36:36 +00001368
Jan Vesely50d3c852016-06-30 14:22:52 -04001369 drmFree(bufs.list);
1370 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001371}
1372
Jose Fonsecad2443b22003-05-27 00:37:33 +00001373
1374/**
1375 * Unmap buffers allocated with drmMapBufs().
1376 *
1377 * \return zero on success, or negative value on failure.
1378 *
1379 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001380 * Calls munmap() for every buffer stored in \p bufs and frees the
1381 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001382 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001383int drmUnmapBufs(drmBufMapPtr bufs)
1384{
1385 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001386
Daryll Straussb3a57661999-12-05 01:19:48 +00001387 for (i = 0; i < bufs->count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001388 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001389 }
Jon Smirl8696e712004-07-07 04:36:36 +00001390
1391 drmFree(bufs->list);
1392 drmFree(bufs);
Daryll Straussb3a57661999-12-05 01:19:48 +00001393 return 0;
1394}
1395
Jose Fonsecad2443b22003-05-27 00:37:33 +00001396
Jan Vesely50d3c852016-06-30 14:22:52 -04001397#define DRM_DMA_RETRY 16
Gareth Hughes36047532001-02-15 08:12:14 +00001398
Jose Fonsecad2443b22003-05-27 00:37:33 +00001399/**
1400 * Reserve DMA buffers.
1401 *
1402 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001403 * \param request
1404 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001405 * \return zero on success, or a negative value on failure.
1406 *
1407 * \internal
1408 * Assemble the arguments into a drm_dma structure and keeps issuing the
1409 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1410 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001411int drmDMA(int fd, drmDMAReqPtr request)
1412{
1413 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001414 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001415
Daryll Straussb3a57661999-12-05 01:19:48 +00001416 dma.context = request->context;
1417 dma.send_count = request->send_count;
1418 dma.send_indices = request->send_list;
1419 dma.send_sizes = request->send_sizes;
1420 dma.flags = request->flags;
1421 dma.request_count = request->request_count;
1422 dma.request_size = request->request_size;
1423 dma.request_indices = request->request_list;
1424 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001425 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001426
1427 do {
Jan Vesely50d3c852016-06-30 14:22:52 -04001428 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
Gareth Hughes36047532001-02-15 08:12:14 +00001429 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1430
1431 if ( ret == 0 ) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001432 request->granted_count = dma.granted_count;
1433 return 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001434 } else {
Jan Vesely50d3c852016-06-30 14:22:52 -04001435 return -errno;
Gareth Hughes36047532001-02-15 08:12:14 +00001436 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001437}
1438
Jose Fonsecad2443b22003-05-27 00:37:33 +00001439
1440/**
1441 * Obtain heavyweight hardware lock.
1442 *
1443 * \param fd file descriptor.
1444 * \param context context.
1445 * \param flags flags that determine the sate of the hardware when the function
1446 * returns.
Jan Vesely50d3c852016-06-30 14:22:52 -04001447 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001448 * \return always zero.
Jan Vesely50d3c852016-06-30 14:22:52 -04001449 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001450 * \internal
1451 * This function translates the arguments into a drm_lock structure and issue
1452 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1453 */
Jon Smirl8696e712004-07-07 04:36:36 +00001454int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001455{
1456 drm_lock_t lock;
1457
Daniel Vetterfd387942015-02-11 12:41:04 +01001458 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001459 lock.context = context;
1460 lock.flags = 0;
1461 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1462 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1463 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1464 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1465 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1466 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001467
Keith Packard8b9ab102008-06-13 16:03:22 -07001468 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04001469 ;
Daryll Straussb3a57661999-12-05 01:19:48 +00001470 return 0;
1471}
1472
Jose Fonsecad2443b22003-05-27 00:37:33 +00001473/**
1474 * Release the hardware lock.
1475 *
1476 * \param fd file descriptor.
1477 * \param context context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001478 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001479 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001480 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001481 * \internal
1482 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1483 * argument in a drm_lock structure.
1484 */
Jon Smirl8696e712004-07-07 04:36:36 +00001485int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001486{
1487 drm_lock_t lock;
1488
Daniel Vetterfd387942015-02-11 12:41:04 +01001489 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001490 lock.context = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001491 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001492}
1493
Adam Jackson22e41ef2006-02-20 23:09:00 +00001494drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001495{
1496 drm_ctx_res_t res;
1497 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001498 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001499 int i;
1500
Daniel Vetterfd387942015-02-11 12:41:04 +01001501 memclear(res);
Keith Packard8b9ab102008-06-13 16:03:22 -07001502 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Jan Vesely50d3c852016-06-30 14:22:52 -04001503 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001504
Brianccd7b6e2007-05-29 14:54:00 -06001505 if (!res.count)
Jan Vesely50d3c852016-06-30 14:22:52 -04001506 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001507
Brianccd7b6e2007-05-29 14:54:00 -06001508 if (!(list = drmMalloc(res.count * sizeof(*list))))
Jan Vesely50d3c852016-06-30 14:22:52 -04001509 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001510 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
Jan Vesely50d3c852016-06-30 14:22:52 -04001511 drmFree(list);
1512 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001513 }
1514
1515 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001516 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Jan Vesely50d3c852016-06-30 14:22:52 -04001517 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001518
Brianccd7b6e2007-05-29 14:54:00 -06001519 for (i = 0; i < res.count; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04001520 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001521 drmFree(list);
1522
1523 *count = res.count;
1524 return retval;
1525}
1526
Adam Jackson22e41ef2006-02-20 23:09:00 +00001527void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001528{
1529 drmFree(pt);
1530}
1531
Jose Fonsecad2443b22003-05-27 00:37:33 +00001532/**
1533 * Create context.
1534 *
1535 * Used by the X server during GLXContext initialization. This causes
1536 * per-context kernel-level resources to be allocated.
1537 *
1538 * \param fd file descriptor.
1539 * \param handle is set on success. To be used by the client when requesting DMA
1540 * dispatch with drmDMA().
Jan Vesely50d3c852016-06-30 14:22:52 -04001541 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001542 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001543 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001544 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001545 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001546 * \internal
1547 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1548 * argument in a drm_ctx structure.
1549 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001550int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001551{
1552 drm_ctx_t ctx;
1553
Daniel Vetterfd387942015-02-11 12:41:04 +01001554 memclear(ctx);
Keith Packard8b9ab102008-06-13 16:03:22 -07001555 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001556 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001557 *handle = ctx.handle;
1558 return 0;
1559}
1560
Jon Smirl8696e712004-07-07 04:36:36 +00001561int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001562{
1563 drm_ctx_t ctx;
1564
Daniel Vetterfd387942015-02-11 12:41:04 +01001565 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001566 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001567 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001568 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001569 return 0;
1570}
1571
Jon Smirl8696e712004-07-07 04:36:36 +00001572int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001573{
1574 drm_ctx_t ctx;
1575
Adam Jackson22e41ef2006-02-20 23:09:00 +00001576 /*
1577 * Context preserving means that no context switches are done between DMA
1578 * buffers from one context and the next. This is suitable for use in the
1579 * X server (which promises to maintain hardware context), or in the
1580 * client-side library when buffers are swapped on behalf of two threads.
1581 */
Daniel Vetterfd387942015-02-11 12:41:04 +01001582 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001583 ctx.handle = context;
Brianccd7b6e2007-05-29 14:54:00 -06001584 if (flags & DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001585 ctx.flags |= _DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001586 if (flags & DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001587 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001588 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001589 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001590 return 0;
1591}
1592
Adam Jackson22e41ef2006-02-20 23:09:00 +00001593int drmGetContextFlags(int fd, drm_context_t context,
1594 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001595{
1596 drm_ctx_t ctx;
1597
Daniel Vetterfd387942015-02-11 12:41:04 +01001598 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001599 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001600 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001601 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001602 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001603 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
Jan Vesely50d3c852016-06-30 14:22:52 -04001604 *flags |= DRM_CONTEXT_PRESERVED;
Brianccd7b6e2007-05-29 14:54:00 -06001605 if (ctx.flags & _DRM_CONTEXT_2DONLY)
Jan Vesely50d3c852016-06-30 14:22:52 -04001606 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001607 return 0;
1608}
Gareth Hughes36047532001-02-15 08:12:14 +00001609
Jose Fonsecad2443b22003-05-27 00:37:33 +00001610/**
1611 * Destroy context.
1612 *
1613 * Free any kernel-level resources allocated with drmCreateContext() associated
1614 * with the context.
Jan Vesely50d3c852016-06-30 14:22:52 -04001615 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001616 * \param fd file descriptor.
1617 * \param handle handle given by drmCreateContext().
Jan Vesely50d3c852016-06-30 14:22:52 -04001618 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001619 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001620 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001621 * \note May only be called by root.
Jan Vesely50d3c852016-06-30 14:22:52 -04001622 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001623 * \internal
1624 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1625 * argument in a drm_ctx structure.
1626 */
Jon Smirl8696e712004-07-07 04:36:36 +00001627int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001628{
1629 drm_ctx_t ctx;
Daniel Vetterfd387942015-02-11 12:41:04 +01001630
1631 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001632 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001633 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Jan Vesely50d3c852016-06-30 14:22:52 -04001634 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001635 return 0;
1636}
1637
Adam Jackson22e41ef2006-02-20 23:09:00 +00001638int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001639{
1640 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001641
1642 memclear(draw);
Keith Packard8b9ab102008-06-13 16:03:22 -07001643 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001644 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001645 *handle = draw.handle;
1646 return 0;
1647}
1648
Jon Smirl8696e712004-07-07 04:36:36 +00001649int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001650{
1651 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001652
1653 memclear(draw);
Daryll Straussb3a57661999-12-05 01:19:48 +00001654 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001655 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Jan Vesely50d3c852016-06-30 14:22:52 -04001656 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001657 return 0;
1658}
1659
Michel Dänzer9810ec22006-08-22 16:40:07 +02001660int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
Jan Vesely50d3c852016-06-30 14:22:52 -04001661 drm_drawable_info_type_t type, unsigned int num,
1662 void *data)
Michel Dänzer9810ec22006-08-22 16:40:07 +02001663{
1664 drm_update_draw_t update;
1665
Daniel Vetterfd387942015-02-11 12:41:04 +01001666 memclear(update);
Michel Dänzer9810ec22006-08-22 16:40:07 +02001667 update.handle = handle;
1668 update.type = type;
1669 update.num = num;
1670 update.data = (unsigned long long)(unsigned long)data;
1671
Keith Packard8b9ab102008-06-13 16:03:22 -07001672 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Jan Vesely50d3c852016-06-30 14:22:52 -04001673 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001674
1675 return 0;
1676}
1677
Jose Fonsecad2443b22003-05-27 00:37:33 +00001678/**
1679 * Acquire the AGP device.
1680 *
1681 * Must be called before any of the other AGP related calls.
1682 *
1683 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001684 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001685 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001686 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001687 * \internal
1688 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1689 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001690int drmAgpAcquire(int fd)
1691{
Keith Packard8b9ab102008-06-13 16:03:22 -07001692 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001693 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001694 return 0;
1695}
1696
Jose Fonsecad2443b22003-05-27 00:37:33 +00001697
1698/**
1699 * Release the AGP device.
1700 *
1701 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001702 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001703 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001704 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001705 * \internal
1706 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1707 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001708int drmAgpRelease(int fd)
1709{
Keith Packard8b9ab102008-06-13 16:03:22 -07001710 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Jan Vesely50d3c852016-06-30 14:22:52 -04001711 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001712 return 0;
1713}
1714
Jose Fonsecad2443b22003-05-27 00:37:33 +00001715
1716/**
1717 * Set the AGP mode.
1718 *
1719 * \param fd file descriptor.
1720 * \param mode AGP mode.
Jan Vesely50d3c852016-06-30 14:22:52 -04001721 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001722 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001723 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001724 * \internal
1725 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1726 * argument in a drm_agp_mode structure.
1727 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001728int drmAgpEnable(int fd, unsigned long mode)
1729{
1730 drm_agp_mode_t m;
1731
Connor Behan14900552015-03-24 13:53:51 -04001732 memclear(m);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001733 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001734 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Jan Vesely50d3c852016-06-30 14:22:52 -04001735 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001736 return 0;
1737}
1738
Jose Fonsecad2443b22003-05-27 00:37:33 +00001739
1740/**
1741 * Allocate a chunk of AGP memory.
1742 *
1743 * \param fd file descriptor.
1744 * \param size requested memory size in bytes. Will be rounded to page boundary.
1745 * \param type type of memory to allocate.
1746 * \param address if not zero, will be set to the physical address of the
1747 * allocated memory.
1748 * \param handle on success will be set to a handle of the allocated memory.
Jan Vesely50d3c852016-06-30 14:22:52 -04001749 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001750 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001751 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001752 * \internal
1753 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1754 * arguments in a drm_agp_buffer structure.
1755 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001756int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Jan Vesely50d3c852016-06-30 14:22:52 -04001757 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001758{
1759 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001760
Daniel Vetterfd387942015-02-11 12:41:04 +01001761 memclear(b);
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001762 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001763 b.size = size;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001764 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001765 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001766 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06001767 if (address != 0UL)
Jan Vesely50d3c852016-06-30 14:22:52 -04001768 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001769 *handle = b.handle;
1770 return 0;
1771}
1772
Jose Fonsecad2443b22003-05-27 00:37:33 +00001773
1774/**
1775 * Free a chunk of AGP memory.
1776 *
1777 * \param fd file descriptor.
1778 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001779 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001780 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001781 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001782 * \internal
1783 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1784 * argument in a drm_agp_buffer structure.
1785 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001786int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001787{
1788 drm_agp_buffer_t b;
1789
Daniel Vetterfd387942015-02-11 12:41:04 +01001790 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001791 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001792 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001793 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001794 return 0;
1795}
1796
Jose Fonsecad2443b22003-05-27 00:37:33 +00001797
1798/**
1799 * Bind a chunk of AGP memory.
1800 *
1801 * \param fd file descriptor.
1802 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1803 * \param offset offset in bytes. It will round to page boundary.
Jan Vesely50d3c852016-06-30 14:22:52 -04001804 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001805 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001806 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001807 * \internal
1808 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1809 * argument in a drm_agp_binding structure.
1810 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001811int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001812{
1813 drm_agp_binding_t b;
1814
Daniel Vetterfd387942015-02-11 12:41:04 +01001815 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001816 b.handle = handle;
1817 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001818 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001819 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001820 return 0;
1821}
1822
Jose Fonsecad2443b22003-05-27 00:37:33 +00001823
1824/**
1825 * Unbind a chunk of AGP memory.
1826 *
1827 * \param fd file descriptor.
1828 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Jan Vesely50d3c852016-06-30 14:22:52 -04001829 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001830 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04001831 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001832 * \internal
1833 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1834 * the argument in a drm_agp_binding structure.
1835 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001836int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001837{
1838 drm_agp_binding_t b;
1839
Daniel Vetterfd387942015-02-11 12:41:04 +01001840 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001841 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001842 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Jan Vesely50d3c852016-06-30 14:22:52 -04001843 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001844 return 0;
1845}
1846
Jose Fonsecad2443b22003-05-27 00:37:33 +00001847
1848/**
1849 * Get AGP driver major version number.
1850 *
1851 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001852 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001853 * \return major version number on success, or a negative value on failure..
Jan Vesely50d3c852016-06-30 14:22:52 -04001854 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001855 * \internal
1856 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1857 * necessary information in a drm_agp_info structure.
1858 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001859int drmAgpVersionMajor(int fd)
1860{
1861 drm_agp_info_t i;
1862
Daniel Vetterfd387942015-02-11 12:41:04 +01001863 memclear(i);
1864
Keith Packard8b9ab102008-06-13 16:03:22 -07001865 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001866 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001867 return i.agp_version_major;
1868}
1869
Jose Fonsecad2443b22003-05-27 00:37:33 +00001870
1871/**
1872 * Get AGP driver minor version number.
1873 *
1874 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001875 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001876 * \return minor version number 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_INFO ioctl, getting the
1880 * necessary information in a drm_agp_info structure.
1881 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001882int drmAgpVersionMinor(int fd)
1883{
1884 drm_agp_info_t i;
1885
Daniel Vetterfd387942015-02-11 12:41:04 +01001886 memclear(i);
1887
Keith Packard8b9ab102008-06-13 16:03:22 -07001888 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Jan Vesely50d3c852016-06-30 14:22:52 -04001889 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001890 return i.agp_version_minor;
1891}
1892
Jose Fonsecad2443b22003-05-27 00:37:33 +00001893
1894/**
1895 * Get AGP mode.
1896 *
1897 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001898 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001899 * \return mode on success, or zero 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001905unsigned long drmAgpGetMode(int fd)
1906{
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 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001913 return i.mode;
1914}
1915
Jose Fonsecad2443b22003-05-27 00:37:33 +00001916
1917/**
1918 * Get AGP aperture base.
1919 *
1920 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001921 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001922 * \return aperture base on success, zero 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001928unsigned long drmAgpBase(int fd)
1929{
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 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001936 return i.aperture_base;
1937}
1938
Jose Fonsecad2443b22003-05-27 00:37:33 +00001939
1940/**
1941 * Get AGP aperture size.
1942 *
1943 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001944 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001945 * \return aperture size on success, 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001951unsigned long drmAgpSize(int fd)
1952{
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.aperture_size;
1960}
1961
Jose Fonsecad2443b22003-05-27 00:37:33 +00001962
1963/**
1964 * Get used AGP memory.
1965 *
1966 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001967 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001968 * \return memory used on success, or 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001974unsigned long drmAgpMemoryUsed(int fd)
1975{
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.memory_used;
1983}
1984
Jose Fonsecad2443b22003-05-27 00:37:33 +00001985
1986/**
1987 * Get available AGP memory.
1988 *
1989 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04001990 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00001991 * \return memory available on success, or 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001997unsigned long drmAgpMemoryAvail(int fd)
1998{
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.memory_allowed;
2006}
2007
Jose Fonsecad2443b22003-05-27 00:37:33 +00002008
2009/**
2010 * Get hardware vendor ID.
2011 *
2012 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002013 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002014 * \return vendor ID 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002020unsigned int drmAgpVendorId(int fd)
2021{
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.id_vendor;
2029}
2030
Jose Fonsecad2443b22003-05-27 00:37:33 +00002031
2032/**
2033 * Get hardware device ID.
2034 *
2035 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002036 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002037 * \return zero 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 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002043unsigned int drmAgpDeviceId(int fd)
2044{
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.id_device;
2052}
2053
Dave Airlie7ede2092005-11-29 09:50:47 +00002054int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002055{
2056 drm_scatter_gather_t sg;
2057
Daniel Vetterfd387942015-02-11 12:41:04 +01002058 memclear(sg);
2059
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002060 *handle = 0;
2061 sg.size = size;
Keith Packard8b9ab102008-06-13 16:03:22 -07002062 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002063 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002064 *handle = sg.handle;
2065 return 0;
2066}
2067
Dave Airlie7ede2092005-11-29 09:50:47 +00002068int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002069{
2070 drm_scatter_gather_t sg;
2071
Daniel Vetterfd387942015-02-11 12:41:04 +01002072 memclear(sg);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002073 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07002074 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Jan Vesely50d3c852016-06-30 14:22:52 -04002075 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002076 return 0;
2077}
2078
Jose Fonsecad2443b22003-05-27 00:37:33 +00002079/**
2080 * Wait for VBLANK.
2081 *
2082 * \param fd file descriptor.
2083 * \param vbl pointer to a drmVBlank structure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002084 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002085 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002086 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002087 * \internal
2088 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2089 */
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002090int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2091{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002092 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002093 int ret;
2094
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002095 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2096 if (ret < 0) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002097 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2098 goto out;
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002099 }
2100 timeout.tv_sec++;
2101
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002102 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002103 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00002104 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08002105 if (ret && errno == EINTR) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002106 clock_gettime(CLOCK_MONOTONIC, &cur);
2107 /* Timeout after 1s */
2108 if (cur.tv_sec > timeout.tv_sec + 1 ||
2109 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2110 timeout.tv_nsec)) {
2111 errno = EBUSY;
2112 ret = -1;
2113 break;
2114 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002115 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002116 } while (ret && errno == EINTR);
2117
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002118out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002119 return ret;
2120}
2121
Daryll Straussb3a57661999-12-05 01:19:48 +00002122int drmError(int err, const char *label)
2123{
2124 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002125 case DRM_ERR_NO_DEVICE:
Jan Vesely50d3c852016-06-30 14:22:52 -04002126 fprintf(stderr, "%s: no device\n", label);
2127 break;
Brianccd7b6e2007-05-29 14:54:00 -06002128 case DRM_ERR_NO_ACCESS:
Jan Vesely50d3c852016-06-30 14:22:52 -04002129 fprintf(stderr, "%s: no access\n", label);
2130 break;
Brianccd7b6e2007-05-29 14:54:00 -06002131 case DRM_ERR_NOT_ROOT:
Jan Vesely50d3c852016-06-30 14:22:52 -04002132 fprintf(stderr, "%s: not root\n", label);
2133 break;
Brianccd7b6e2007-05-29 14:54:00 -06002134 case DRM_ERR_INVALID:
Jan Vesely50d3c852016-06-30 14:22:52 -04002135 fprintf(stderr, "%s: invalid args\n", label);
2136 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002137 default:
Jan Vesely50d3c852016-06-30 14:22:52 -04002138 if (err < 0)
2139 err = -err;
2140 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2141 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002142 }
2143
2144 return 1;
2145}
2146
Jose Fonsecad2443b22003-05-27 00:37:33 +00002147/**
2148 * Install IRQ handler.
2149 *
2150 * \param fd file descriptor.
2151 * \param irq IRQ number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002152 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002153 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002154 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002155 * \internal
2156 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2157 * argument in a drm_control structure.
2158 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002159int drmCtlInstHandler(int fd, int irq)
2160{
2161 drm_control_t ctl;
2162
Daniel Vetterfd387942015-02-11 12:41:04 +01002163 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002164 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002165 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002166 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002167 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002168 return 0;
2169}
2170
Jose Fonsecad2443b22003-05-27 00:37:33 +00002171
2172/**
2173 * Uninstall IRQ handler.
2174 *
2175 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002176 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002177 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002178 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002179 * \internal
2180 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2181 * argument in a drm_control structure.
2182 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002183int drmCtlUninstHandler(int fd)
2184{
2185 drm_control_t ctl;
2186
Daniel Vetterfd387942015-02-11 12:41:04 +01002187 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002188 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002189 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002190 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Jan Vesely50d3c852016-06-30 14:22:52 -04002191 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002192 return 0;
2193}
2194
2195int drmFinish(int fd, int context, drmLockFlags flags)
2196{
2197 drm_lock_t lock;
2198
Daniel Vetterfd387942015-02-11 12:41:04 +01002199 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00002200 lock.context = context;
Daryll Straussb3a57661999-12-05 01:19:48 +00002201 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2202 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2203 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2204 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2205 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2206 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002207 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Jan Vesely50d3c852016-06-30 14:22:52 -04002208 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002209 return 0;
2210}
2211
Jose Fonsecad2443b22003-05-27 00:37:33 +00002212/**
2213 * Get IRQ from bus ID.
2214 *
2215 * \param fd file descriptor.
2216 * \param busnum bus number.
2217 * \param devnum device number.
2218 * \param funcnum function number.
Jan Vesely50d3c852016-06-30 14:22:52 -04002219 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002220 * \return IRQ number on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002221 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002222 * \internal
2223 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2224 * arguments in a drm_irq_busid structure.
2225 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002226int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2227{
2228 drm_irq_busid_t p;
2229
Daniel Vetterfd387942015-02-11 12:41:04 +01002230 memclear(p);
Daryll Straussb3a57661999-12-05 01:19:48 +00002231 p.busnum = busnum;
2232 p.devnum = devnum;
2233 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002234 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Jan Vesely50d3c852016-06-30 14:22:52 -04002235 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002236 return p.irq;
2237}
2238
Jon Smirl8696e712004-07-07 04:36:36 +00002239int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002240{
2241 drmHashEntry *entry = drmGetEntry(fd);
2242
2243 if (drmHashInsert(entry->tagTable, context, tag)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002244 drmHashDelete(entry->tagTable, context);
2245 drmHashInsert(entry->tagTable, context, tag);
Daryll Straussb3a57661999-12-05 01:19:48 +00002246 }
2247 return 0;
2248}
2249
Jon Smirl8696e712004-07-07 04:36:36 +00002250int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002251{
2252 drmHashEntry *entry = drmGetEntry(fd);
2253
2254 return drmHashDelete(entry->tagTable, context);
2255}
2256
Jon Smirl8696e712004-07-07 04:36:36 +00002257void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002258{
2259 drmHashEntry *entry = drmGetEntry(fd);
2260 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002261
Brianccd7b6e2007-05-29 14:54:00 -06002262 if (drmHashLookup(entry->tagTable, context, &value))
Jan Vesely50d3c852016-06-30 14:22:52 -04002263 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002264
2265 return value;
2266}
2267
Adam Jackson22e41ef2006-02-20 23:09:00 +00002268int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2269 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002270{
2271 drm_ctx_priv_map_t map;
2272
Daniel Vetterfd387942015-02-11 12:41:04 +01002273 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002274 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002275 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002276
Keith Packard8b9ab102008-06-13 16:03:22 -07002277 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002278 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002279 return 0;
2280}
2281
Adam Jackson22e41ef2006-02-20 23:09:00 +00002282int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2283 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002284{
2285 drm_ctx_priv_map_t map;
2286
Daniel Vetterfd387942015-02-11 12:41:04 +01002287 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002288 map.ctx_id = ctx_id;
2289
Keith Packard8b9ab102008-06-13 16:03:22 -07002290 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002291 return -errno;
Brianccd7b6e2007-05-29 14:54:00 -06002292 if (handle)
Jan Vesely50d3c852016-06-30 14:22:52 -04002293 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002294
2295 return 0;
2296}
2297
Jon Smirl8696e712004-07-07 04:36:36 +00002298int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
Jan Vesely50d3c852016-06-30 14:22:52 -04002299 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2300 int *mtrr)
Rik Faith88dbee52001-02-28 09:27:44 +00002301{
2302 drm_map_t map;
2303
Daniel Vetterfd387942015-02-11 12:41:04 +01002304 memclear(map);
Rik Faith88dbee52001-02-28 09:27:44 +00002305 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002306 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Jan Vesely50d3c852016-06-30 14:22:52 -04002307 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002308 *offset = map.offset;
2309 *size = map.size;
2310 *type = map.type;
2311 *flags = map.flags;
2312 *handle = (unsigned long)map.handle;
2313 *mtrr = map.mtrr;
2314 return 0;
2315}
2316
2317int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
Jan Vesely50d3c852016-06-30 14:22:52 -04002318 unsigned long *magic, unsigned long *iocs)
Rik Faith88dbee52001-02-28 09:27:44 +00002319{
2320 drm_client_t client;
2321
Daniel Vetterfd387942015-02-11 12:41:04 +01002322 memclear(client);
Rik Faith88dbee52001-02-28 09:27:44 +00002323 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002324 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Jan Vesely50d3c852016-06-30 14:22:52 -04002325 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002326 *auth = client.auth;
2327 *pid = client.pid;
2328 *uid = client.uid;
2329 *magic = client.magic;
2330 *iocs = client.iocs;
2331 return 0;
2332}
2333
2334int drmGetStats(int fd, drmStatsT *stats)
2335{
2336 drm_stats_t s;
Jan Veselyde8532d2014-11-30 12:53:18 -05002337 unsigned i;
Rik Faith88dbee52001-02-28 09:27:44 +00002338
Daniel Vetterfd387942015-02-11 12:41:04 +01002339 memclear(s);
Keith Packard8b9ab102008-06-13 16:03:22 -07002340 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Jan Vesely50d3c852016-06-30 14:22:52 -04002341 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002342
2343 stats->count = 0;
2344 memset(stats, 0, sizeof(*stats));
2345 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
Jan Vesely50d3c852016-06-30 14:22:52 -04002346 return -1;
Rik Faith88dbee52001-02-28 09:27:44 +00002347
2348#define SET_VALUE \
2349 stats->data[i].long_format = "%-20.20s"; \
2350 stats->data[i].rate_format = "%8.8s"; \
2351 stats->data[i].isvalue = 1; \
2352 stats->data[i].verbose = 0
2353
2354#define SET_COUNT \
2355 stats->data[i].long_format = "%-20.20s"; \
2356 stats->data[i].rate_format = "%5.5s"; \
2357 stats->data[i].isvalue = 0; \
2358 stats->data[i].mult_names = "kgm"; \
2359 stats->data[i].mult = 1000; \
2360 stats->data[i].verbose = 0
2361
2362#define SET_BYTE \
2363 stats->data[i].long_format = "%-20.20s"; \
2364 stats->data[i].rate_format = "%5.5s"; \
2365 stats->data[i].isvalue = 0; \
2366 stats->data[i].mult_names = "KGM"; \
2367 stats->data[i].mult = 1024; \
2368 stats->data[i].verbose = 0
2369
2370
2371 stats->count = s.count;
2372 for (i = 0; i < s.count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002373 stats->data[i].value = s.data[i].value;
2374 switch (s.data[i].type) {
2375 case _DRM_STAT_LOCK:
2376 stats->data[i].long_name = "Lock";
2377 stats->data[i].rate_name = "Lock";
2378 SET_VALUE;
2379 break;
2380 case _DRM_STAT_OPENS:
2381 stats->data[i].long_name = "Opens";
2382 stats->data[i].rate_name = "O";
2383 SET_COUNT;
2384 stats->data[i].verbose = 1;
2385 break;
2386 case _DRM_STAT_CLOSES:
2387 stats->data[i].long_name = "Closes";
2388 stats->data[i].rate_name = "Lock";
2389 SET_COUNT;
2390 stats->data[i].verbose = 1;
2391 break;
2392 case _DRM_STAT_IOCTLS:
2393 stats->data[i].long_name = "Ioctls";
2394 stats->data[i].rate_name = "Ioc/s";
2395 SET_COUNT;
2396 break;
2397 case _DRM_STAT_LOCKS:
2398 stats->data[i].long_name = "Locks";
2399 stats->data[i].rate_name = "Lck/s";
2400 SET_COUNT;
2401 break;
2402 case _DRM_STAT_UNLOCKS:
2403 stats->data[i].long_name = "Unlocks";
2404 stats->data[i].rate_name = "Unl/s";
2405 SET_COUNT;
2406 break;
2407 case _DRM_STAT_IRQ:
2408 stats->data[i].long_name = "IRQs";
2409 stats->data[i].rate_name = "IRQ/s";
2410 SET_COUNT;
2411 break;
2412 case _DRM_STAT_PRIMARY:
2413 stats->data[i].long_name = "Primary Bytes";
2414 stats->data[i].rate_name = "PB/s";
2415 SET_BYTE;
2416 break;
2417 case _DRM_STAT_SECONDARY:
2418 stats->data[i].long_name = "Secondary Bytes";
2419 stats->data[i].rate_name = "SB/s";
2420 SET_BYTE;
2421 break;
2422 case _DRM_STAT_DMA:
2423 stats->data[i].long_name = "DMA";
2424 stats->data[i].rate_name = "DMA/s";
2425 SET_COUNT;
2426 break;
2427 case _DRM_STAT_SPECIAL:
2428 stats->data[i].long_name = "Special DMA";
2429 stats->data[i].rate_name = "dma/s";
2430 SET_COUNT;
2431 break;
2432 case _DRM_STAT_MISSED:
2433 stats->data[i].long_name = "Miss";
2434 stats->data[i].rate_name = "Ms/s";
2435 SET_COUNT;
2436 break;
2437 case _DRM_STAT_VALUE:
2438 stats->data[i].long_name = "Value";
2439 stats->data[i].rate_name = "Value";
2440 SET_VALUE;
2441 break;
2442 case _DRM_STAT_BYTE:
2443 stats->data[i].long_name = "Bytes";
2444 stats->data[i].rate_name = "B/s";
2445 SET_BYTE;
2446 break;
2447 case _DRM_STAT_COUNT:
2448 default:
2449 stats->data[i].long_name = "Count";
2450 stats->data[i].rate_name = "Cnt/s";
2451 SET_COUNT;
2452 break;
2453 }
Rik Faith88dbee52001-02-28 09:27:44 +00002454 }
2455 return 0;
2456}
2457
Jose Fonsecad2443b22003-05-27 00:37:33 +00002458/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002459 * Issue a set-version ioctl.
2460 *
2461 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002462 * \param drmCommandIndex command index
Eric Anholt06cb1322003-10-23 02:23:31 +00002463 * \param data source pointer of the data to be read and written.
2464 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002465 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002466 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002467 *
Eric Anholt06cb1322003-10-23 02:23:31 +00002468 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002469 * It issues a read-write ioctl given by
Eric Anholt06cb1322003-10-23 02:23:31 +00002470 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2471 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002472int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002473{
2474 int retcode = 0;
2475 drm_set_version_t sv;
2476
Daniel Vetterfd387942015-02-11 12:41:04 +01002477 memclear(sv);
Eric Anholt06cb1322003-10-23 02:23:31 +00002478 sv.drm_di_major = version->drm_di_major;
2479 sv.drm_di_minor = version->drm_di_minor;
2480 sv.drm_dd_major = version->drm_dd_major;
2481 sv.drm_dd_minor = version->drm_dd_minor;
2482
Keith Packard8b9ab102008-06-13 16:03:22 -07002483 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002484 retcode = -errno;
Eric Anholt06cb1322003-10-23 02:23:31 +00002485 }
2486
2487 version->drm_di_major = sv.drm_di_major;
2488 version->drm_di_minor = sv.drm_di_minor;
2489 version->drm_dd_major = sv.drm_dd_major;
2490 version->drm_dd_minor = sv.drm_dd_minor;
2491
2492 return retcode;
2493}
2494
2495/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002496 * Send a device-specific command.
2497 *
2498 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002499 * \param drmCommandIndex command index
2500 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002501 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002502 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002503 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002504 * It issues a ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002505 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2506 */
Jens Owen3903e5a2002-04-09 21:54:56 +00002507int drmCommandNone(int fd, unsigned long drmCommandIndex)
2508{
Jens Owen3903e5a2002-04-09 21:54:56 +00002509 unsigned long request;
2510
2511 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2512
Daniel Vetterfd387942015-02-11 12:41:04 +01002513 if (drmIoctl(fd, request, NULL)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002514 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002515 }
2516 return 0;
2517}
2518
Jose Fonsecad2443b22003-05-27 00:37:33 +00002519
2520/**
2521 * Send a device-specific read command.
2522 *
2523 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002524 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002525 * \param data destination pointer of the data to be read.
2526 * \param size size of the data to be read.
Jan Vesely50d3c852016-06-30 14:22:52 -04002527 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002528 * \return zero on success, or a negative value on failure.
2529 *
2530 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002531 * It issues a read ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002532 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2533 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002534int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2535 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002536{
2537 unsigned long request;
2538
Jan Vesely50d3c852016-06-30 14:22:52 -04002539 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2540 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002541
Keith Packard8b9ab102008-06-13 16:03:22 -07002542 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002543 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002544 }
2545 return 0;
2546}
2547
Jose Fonsecad2443b22003-05-27 00:37:33 +00002548
2549/**
2550 * Send a device-specific write command.
2551 *
2552 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002553 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002554 * \param data source pointer of the data to be written.
2555 * \param size size of the data to be written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002556 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002557 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002558 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002559 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002560 * It issues a write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002561 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2562 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002563int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2564 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002565{
2566 unsigned long request;
2567
Jan Vesely50d3c852016-06-30 14:22:52 -04002568 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2569 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002570
Keith Packard8b9ab102008-06-13 16:03:22 -07002571 if (drmIoctl(fd, request, data)) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002572 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002573 }
2574 return 0;
2575}
2576
Jose Fonsecad2443b22003-05-27 00:37:33 +00002577
2578/**
2579 * Send a device-specific read-write command.
2580 *
2581 * \param fd file descriptor.
Jan Vesely50d3c852016-06-30 14:22:52 -04002582 * \param drmCommandIndex command index
Jose Fonsecad2443b22003-05-27 00:37:33 +00002583 * \param data source pointer of the data to be read and written.
2584 * \param size size of the data to be read and written.
Jan Vesely50d3c852016-06-30 14:22:52 -04002585 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002586 * \return zero on success, or a negative value on failure.
Jan Vesely50d3c852016-06-30 14:22:52 -04002587 *
Jose Fonsecad2443b22003-05-27 00:37:33 +00002588 * \internal
Jan Vesely50d3c852016-06-30 14:22:52 -04002589 * It issues a read-write ioctl given by
Jose Fonsecad2443b22003-05-27 00:37:33 +00002590 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2591 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002592int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2593 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002594{
2595 unsigned long request;
2596
Jan Vesely50d3c852016-06-30 14:22:52 -04002597 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2598 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002599
Keith Packard8b9ab102008-06-13 16:03:22 -07002600 if (drmIoctl(fd, request, data))
Jan Vesely50d3c852016-06-30 14:22:52 -04002601 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002602 return 0;
2603}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002604
Dave Airlied51e1bb2006-11-09 08:55:58 +11002605#define DRM_MAX_FDS 16
2606static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002607 char *BusID;
2608 int fd;
2609 int refcount;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002610 int type;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002611} connection[DRM_MAX_FDS];
2612
2613static int nr_fds = 0;
2614
Jan Vesely50d3c852016-06-30 14:22:52 -04002615int drmOpenOnce(void *unused,
2616 const char *BusID,
2617 int *newlyopened)
Dave Airlied51e1bb2006-11-09 08:55:58 +11002618{
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002619 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2620}
2621
2622int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2623{
Brianccd7b6e2007-05-29 14:54:00 -06002624 int i;
2625 int fd;
Jan Vesely50d3c852016-06-30 14:22:52 -04002626
Brianccd7b6e2007-05-29 14:54:00 -06002627 for (i = 0; i < nr_fds; i++)
Jan Vesely50d3c852016-06-30 14:22:52 -04002628 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2629 (connection[i].type == type)) {
2630 connection[i].refcount++;
2631 *newlyopened = 0;
2632 return connection[i].fd;
2633 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002634
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002635 fd = drmOpenWithType(NULL, BusID, type);
Emil Velikovc1cd3d92015-07-14 15:05:18 +01002636 if (fd < 0 || nr_fds == DRM_MAX_FDS)
Jan Vesely50d3c852016-06-30 14:22:52 -04002637 return fd;
2638
Brianccd7b6e2007-05-29 14:54:00 -06002639 connection[nr_fds].BusID = strdup(BusID);
2640 connection[nr_fds].fd = fd;
2641 connection[nr_fds].refcount = 1;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002642 connection[nr_fds].type = type;
Brianccd7b6e2007-05-29 14:54:00 -06002643 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002644
Brianccd7b6e2007-05-29 14:54:00 -06002645 if (0)
Jan Vesely50d3c852016-06-30 14:22:52 -04002646 fprintf(stderr, "saved connection %d for %s %d\n",
2647 nr_fds, connection[nr_fds].BusID,
2648 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002649
Brianccd7b6e2007-05-29 14:54:00 -06002650 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002651
Brianccd7b6e2007-05-29 14:54:00 -06002652 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002653}
2654
2655void drmCloseOnce(int fd)
2656{
Brianccd7b6e2007-05-29 14:54:00 -06002657 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002658
Brianccd7b6e2007-05-29 14:54:00 -06002659 for (i = 0; i < nr_fds; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04002660 if (fd == connection[i].fd) {
2661 if (--connection[i].refcount == 0) {
2662 drmClose(connection[i].fd);
2663 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002664
Jan Vesely50d3c852016-06-30 14:22:52 -04002665 if (i < --nr_fds)
2666 connection[i] = connection[nr_fds];
2667
2668 return;
2669 }
2670 }
Brianccd7b6e2007-05-29 14:54:00 -06002671 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002672}
Jesse Barnes731cd552008-12-17 10:09:49 -08002673
2674int drmSetMaster(int fd)
2675{
Jan Vesely50d3c852016-06-30 14:22:52 -04002676 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002677}
2678
2679int drmDropMaster(int fd)
2680{
Jan Vesely50d3c852016-06-30 14:22:52 -04002681 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002682}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002683
2684char *drmGetDeviceNameFromFd(int fd)
2685{
Jan Vesely50d3c852016-06-30 14:22:52 -04002686 char name[128];
2687 struct stat sbuf;
2688 dev_t d;
2689 int i;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002690
Jan Vesely50d3c852016-06-30 14:22:52 -04002691 /* The whole drmOpen thing is a fiasco and we need to find a way
2692 * back to just using open(2). For now, however, lets just make
2693 * things worse with even more ad hoc directory walking code to
2694 * discover the device file name. */
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002695
Jan Vesely50d3c852016-06-30 14:22:52 -04002696 fstat(fd, &sbuf);
2697 d = sbuf.st_rdev;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002698
Jan Vesely50d3c852016-06-30 14:22:52 -04002699 for (i = 0; i < DRM_MAX_MINOR; i++) {
2700 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2701 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2702 break;
2703 }
2704 if (i == DRM_MAX_MINOR)
2705 return NULL;
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002706
Jan Vesely50d3c852016-06-30 14:22:52 -04002707 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002708}
Dave Airliecc0a1452012-07-14 09:52:17 +00002709
Frank Binns1f735782015-02-13 10:51:15 +00002710int drmGetNodeTypeFromFd(int fd)
2711{
Jan Vesely50d3c852016-06-30 14:22:52 -04002712 struct stat sbuf;
2713 int maj, min, type;
Frank Binns1f735782015-02-13 10:51:15 +00002714
Jan Vesely50d3c852016-06-30 14:22:52 -04002715 if (fstat(fd, &sbuf))
2716 return -1;
Frank Binns1f735782015-02-13 10:51:15 +00002717
Jan Vesely50d3c852016-06-30 14:22:52 -04002718 maj = major(sbuf.st_rdev);
2719 min = minor(sbuf.st_rdev);
Frank Binns1f735782015-02-13 10:51:15 +00002720
Jan Vesely50d3c852016-06-30 14:22:52 -04002721 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2722 errno = EINVAL;
2723 return -1;
2724 }
Frank Binns1f735782015-02-13 10:51:15 +00002725
Jan Vesely50d3c852016-06-30 14:22:52 -04002726 type = drmGetMinorType(min);
2727 if (type == -1)
2728 errno = ENODEV;
2729 return type;
Frank Binns1f735782015-02-13 10:51:15 +00002730}
2731
Dave Airliecc0a1452012-07-14 09:52:17 +00002732int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2733{
Jan Vesely50d3c852016-06-30 14:22:52 -04002734 struct drm_prime_handle args;
2735 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002736
Jan Vesely50d3c852016-06-30 14:22:52 -04002737 memclear(args);
2738 args.fd = -1;
2739 args.handle = handle;
2740 args.flags = flags;
2741 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2742 if (ret)
2743 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002744
Jan Vesely50d3c852016-06-30 14:22:52 -04002745 *prime_fd = args.fd;
2746 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002747}
2748
2749int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2750{
Jan Vesely50d3c852016-06-30 14:22:52 -04002751 struct drm_prime_handle args;
2752 int ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002753
Jan Vesely50d3c852016-06-30 14:22:52 -04002754 memclear(args);
2755 args.fd = prime_fd;
2756 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2757 if (ret)
2758 return ret;
Dave Airliecc0a1452012-07-14 09:52:17 +00002759
Jan Vesely50d3c852016-06-30 14:22:52 -04002760 *handle = args.handle;
2761 return 0;
Dave Airliecc0a1452012-07-14 09:52:17 +00002762}
2763
Emil Velikov0ca03a42015-03-07 00:58:39 +00002764static char *drmGetMinorNameForFD(int fd, int type)
2765{
2766#ifdef __linux__
Jan Vesely50d3c852016-06-30 14:22:52 -04002767 DIR *sysdir;
2768 struct dirent *pent, *ent;
2769 struct stat sbuf;
2770 const char *name = drmGetMinorName(type);
2771 int len;
2772 char dev_name[64], buf[64];
2773 long name_max;
2774 int maj, min;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002775
Jan Vesely50d3c852016-06-30 14:22:52 -04002776 if (!name)
2777 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002778
Jan Vesely50d3c852016-06-30 14:22:52 -04002779 len = strlen(name);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002780
Jan Vesely50d3c852016-06-30 14:22:52 -04002781 if (fstat(fd, &sbuf))
2782 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002783
Jan Vesely50d3c852016-06-30 14:22:52 -04002784 maj = major(sbuf.st_rdev);
2785 min = minor(sbuf.st_rdev);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002786
Jan Vesely50d3c852016-06-30 14:22:52 -04002787 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2788 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002789
Jan Vesely50d3c852016-06-30 14:22:52 -04002790 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002791
Jan Vesely50d3c852016-06-30 14:22:52 -04002792 sysdir = opendir(buf);
2793 if (!sysdir)
2794 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002795
Jan Vesely50d3c852016-06-30 14:22:52 -04002796 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2797 if (name_max == -1)
2798 goto out_close_dir;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002799
Jan Vesely50d3c852016-06-30 14:22:52 -04002800 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2801 if (pent == NULL)
2802 goto out_close_dir;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002803
Jan Vesely50d3c852016-06-30 14:22:52 -04002804 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2805 if (strncmp(ent->d_name, name, len) == 0) {
2806 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2807 ent->d_name);
Mathias Tillman5c42b5e2015-08-24 11:56:13 +08002808
Jan Vesely50d3c852016-06-30 14:22:52 -04002809 free(pent);
2810 closedir(sysdir);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002811
Jan Vesely50d3c852016-06-30 14:22:52 -04002812 return strdup(dev_name);
2813 }
2814 }
Emil Velikov0ca03a42015-03-07 00:58:39 +00002815
Jan Vesely50d3c852016-06-30 14:22:52 -04002816 free(pent);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002817
2818out_close_dir:
Jan Vesely50d3c852016-06-30 14:22:52 -04002819 closedir(sysdir);
Emil Velikov8415a002015-09-07 18:29:05 +01002820#else
2821#warning "Missing implementation of drmGetMinorNameForFD"
Emil Velikov0ca03a42015-03-07 00:58:39 +00002822#endif
Jan Vesely50d3c852016-06-30 14:22:52 -04002823 return NULL;
Emil Velikov0ca03a42015-03-07 00:58:39 +00002824}
2825
2826char *drmGetPrimaryDeviceNameFromFd(int fd)
2827{
Jan Vesely50d3c852016-06-30 14:22:52 -04002828 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002829}
2830
2831char *drmGetRenderDeviceNameFromFd(int fd)
2832{
Jan Vesely50d3c852016-06-30 14:22:52 -04002833 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
Emil Velikov0ca03a42015-03-07 00:58:39 +00002834}
Emil Velikovb556ea12015-08-17 11:09:06 +08002835
Emil Velikova250fce2015-09-07 12:54:27 +01002836static int drmParseSubsystemType(int maj, int min)
Emil Velikovb556ea12015-08-17 11:09:06 +08002837{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002838#ifdef __linux__
Emil Velikova250fce2015-09-07 12:54:27 +01002839 char path[PATH_MAX + 1];
Emil Velikovb556ea12015-08-17 11:09:06 +08002840 char link[PATH_MAX + 1] = "";
2841 char *name;
2842
Emil Velikova250fce2015-09-07 12:54:27 +01002843 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
2844 maj, min);
2845
2846 if (readlink(path, link, PATH_MAX) < 0)
2847 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08002848
2849 name = strrchr(link, '/');
2850 if (!name)
2851 return -EINVAL;
2852
Emil Velikova250fce2015-09-07 12:54:27 +01002853 if (strncmp(name, "/pci", 4) == 0)
Emil Velikovb556ea12015-08-17 11:09:06 +08002854 return DRM_BUS_PCI;
2855
2856 return -EINVAL;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002857#else
2858#warning "Missing implementation of drmParseSubsystemType"
2859 return -EINVAL;
2860#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002861}
2862
Emil Velikov536e0de2015-09-07 12:37:57 +01002863static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
Emil Velikovb556ea12015-08-17 11:09:06 +08002864{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002865#ifdef __linux__
Emil Velikov536e0de2015-09-07 12:37:57 +01002866 char path[PATH_MAX + 1];
Emil Velikovf53d3542016-11-11 19:04:11 +00002867 char data[512 + 1];
Emil Velikov536e0de2015-09-07 12:37:57 +01002868 char *str;
Emil Velikovb556ea12015-08-17 11:09:06 +08002869 int domain, bus, dev, func;
Emil Velikov536e0de2015-09-07 12:37:57 +01002870 int fd, ret;
Emil Velikovb556ea12015-08-17 11:09:06 +08002871
Emil Velikov536e0de2015-09-07 12:37:57 +01002872 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
2873 fd = open(path, O_RDONLY);
2874 if (fd < 0)
2875 return -errno;
2876
2877 ret = read(fd, data, sizeof(data));
Emil Velikovf53d3542016-11-11 19:04:11 +00002878 data[sizeof(data)-1] = '\0';
Emil Velikov536e0de2015-09-07 12:37:57 +01002879 close(fd);
2880 if (ret < 0)
2881 return -errno;
2882
2883#define TAG "PCI_SLOT_NAME="
2884 str = strstr(data, TAG);
Emil Velikovb556ea12015-08-17 11:09:06 +08002885 if (str == NULL)
2886 return -EINVAL;
2887
Emil Velikov536e0de2015-09-07 12:37:57 +01002888 if (sscanf(str, TAG "%04x:%02x:%02x.%1u",
Emil Velikovb556ea12015-08-17 11:09:06 +08002889 &domain, &bus, &dev, &func) != 4)
2890 return -EINVAL;
Emil Velikov536e0de2015-09-07 12:37:57 +01002891#undef TAG
Emil Velikovb556ea12015-08-17 11:09:06 +08002892
2893 info->domain = domain;
2894 info->bus = bus;
2895 info->dev = dev;
2896 info->func = func;
2897
2898 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002899#else
2900#warning "Missing implementation of drmParsePciBusInfo"
2901 return -EINVAL;
2902#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002903}
2904
Emil Velikovbc2aca92015-09-07 13:51:54 +01002905static int drmCompareBusInfo(drmDevicePtr a, drmDevicePtr b)
Emil Velikovb556ea12015-08-17 11:09:06 +08002906{
Emil Velikovbc2aca92015-09-07 13:51:54 +01002907 if (a == NULL || b == NULL)
2908 return -1;
2909
Emil Velikovb556ea12015-08-17 11:09:06 +08002910 if (a->bustype != b->bustype)
Emil Velikovbc2aca92015-09-07 13:51:54 +01002911 return -1;
Emil Velikovb556ea12015-08-17 11:09:06 +08002912
2913 switch (a->bustype) {
2914 case DRM_BUS_PCI:
Emil Velikovbc2aca92015-09-07 13:51:54 +01002915 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo));
Emil Velikovb556ea12015-08-17 11:09:06 +08002916 default:
2917 break;
2918 }
2919
Emil Velikovbc2aca92015-09-07 13:51:54 +01002920 return -1;
Emil Velikovb556ea12015-08-17 11:09:06 +08002921}
2922
2923static int drmGetNodeType(const char *name)
2924{
2925 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
2926 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
2927 return DRM_NODE_PRIMARY;
2928
2929 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
2930 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
2931 return DRM_NODE_CONTROL;
2932
2933 if (strncmp(name, DRM_RENDER_MINOR_NAME,
2934 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
2935 return DRM_NODE_RENDER;
2936
2937 return -EINVAL;
2938}
2939
Emil Velikov5f68d312015-09-07 13:54:32 +01002940static int drmGetMaxNodeName(void)
2941{
2942 return sizeof(DRM_DIR_NAME) +
2943 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
2944 sizeof(DRM_CONTROL_MINOR_NAME),
2945 sizeof(DRM_RENDER_MINOR_NAME)) +
Eric Engestromce975072016-04-03 19:48:12 +01002946 3 /* length of the node number */;
Emil Velikov5f68d312015-09-07 13:54:32 +01002947}
2948
Emil Velikovb40a65d2016-11-30 16:41:27 +00002949static int drmParsePciDeviceInfo(int maj, int min,
Emil Velikovb556ea12015-08-17 11:09:06 +08002950 drmPciDeviceInfoPtr device)
2951{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002952#ifdef __linux__
Emil Velikovef5192e2015-09-07 12:47:47 +01002953 char path[PATH_MAX + 1];
2954 unsigned char config[64];
2955 int fd, ret;
2956
Emil Velikovb40a65d2016-11-30 16:41:27 +00002957 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/config", maj, min);
Emil Velikovef5192e2015-09-07 12:47:47 +01002958 fd = open(path, O_RDONLY);
2959 if (fd < 0)
2960 return -errno;
2961
2962 ret = read(fd, config, sizeof(config));
2963 close(fd);
2964 if (ret < 0)
2965 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08002966
2967 device->vendor_id = config[0] | (config[1] << 8);
2968 device->device_id = config[2] | (config[3] << 8);
2969 device->revision_id = config[8];
2970 device->subvendor_id = config[44] | (config[45] << 8);
2971 device->subdevice_id = config[46] | (config[47] << 8);
2972
2973 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002974#else
2975#warning "Missing implementation of drmParsePciDeviceInfo"
2976 return -EINVAL;
2977#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002978}
2979
Emil Velikovccedf662015-09-09 16:02:18 +01002980void drmFreeDevice(drmDevicePtr *device)
Emil Velikovb556ea12015-08-17 11:09:06 +08002981{
Emil Velikovb556ea12015-08-17 11:09:06 +08002982 if (device == NULL)
2983 return;
2984
Emil Velikov5f68d312015-09-07 13:54:32 +01002985 free(*device);
2986 *device = NULL;
Emil Velikovb556ea12015-08-17 11:09:06 +08002987}
2988
2989void drmFreeDevices(drmDevicePtr devices[], int count)
2990{
2991 int i;
2992
2993 if (devices == NULL)
2994 return;
2995
Qiang Yu6c056ee2016-07-14 17:10:56 +08002996 for (i = 0; i < count; i++)
2997 if (devices[i])
2998 drmFreeDevice(&devices[i]);
Emil Velikovb556ea12015-08-17 11:09:06 +08002999}
3000
Emil Velikovb40a65d2016-11-30 16:41:27 +00003001static int drmProcessPciDevice(drmDevicePtr *device,
Emil Velikovfae59d72015-09-09 17:54:34 +01003002 const char *node, int node_type,
3003 int maj, int min, bool fetch_deviceinfo)
3004{
Nicolai Hähnle9bdec972016-05-13 01:11:13 -05003005 const int max_node_str = ALIGN(drmGetMaxNodeName(), sizeof(void *));
Emil Velikovfae59d72015-09-09 17:54:34 +01003006 int ret, i;
Michel Dänzer30455232015-10-14 12:48:52 +09003007 char *addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003008
Michel Dänzer30455232015-10-14 12:48:52 +09003009 *device = calloc(1, sizeof(drmDevice) +
Jan Vesely50d3c852016-06-30 14:22:52 -04003010 (DRM_NODE_MAX * (sizeof(void *) + max_node_str)) +
3011 sizeof(drmPciBusInfo) +
3012 sizeof(drmPciDeviceInfo));
Emil Velikovfae59d72015-09-09 17:54:34 +01003013 if (!*device)
3014 return -ENOMEM;
3015
Michel Dänzer30455232015-10-14 12:48:52 +09003016 addr = (char*)*device;
Jan Vesely50d3c852016-06-30 14:22:52 -04003017
Emil Velikovfae59d72015-09-09 17:54:34 +01003018 (*device)->bustype = DRM_BUS_PCI;
3019 (*device)->available_nodes = 1 << node_type;
3020
3021 addr += sizeof(drmDevice);
Michel Dänzer30455232015-10-14 12:48:52 +09003022 (*device)->nodes = (char**)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003023
3024 addr += DRM_NODE_MAX * sizeof(void *);
3025 for (i = 0; i < DRM_NODE_MAX; i++) {
3026 (*device)->nodes[i] = addr;
3027 addr += max_node_str;
3028 }
3029 memcpy((*device)->nodes[node_type], node, max_node_str);
3030
Michel Dänzer30455232015-10-14 12:48:52 +09003031 (*device)->businfo.pci = (drmPciBusInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003032
3033 ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
3034 if (ret)
3035 goto free_device;
3036
3037 // Fetch the device info if the user has requested it
3038 if (fetch_deviceinfo) {
3039 addr += sizeof(drmPciBusInfo);
Michel Dänzer30455232015-10-14 12:48:52 +09003040 (*device)->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
Emil Velikovfae59d72015-09-09 17:54:34 +01003041
Emil Velikovb40a65d2016-11-30 16:41:27 +00003042 ret = drmParsePciDeviceInfo(maj, min, (*device)->deviceinfo.pci);
Emil Velikovfae59d72015-09-09 17:54:34 +01003043 if (ret)
3044 goto free_device;
3045 }
3046 return 0;
3047
3048free_device:
3049 free(*device);
3050 *device = NULL;
3051 return ret;
3052}
3053
Qiang Yu3c208932016-07-14 17:10:55 +08003054/* Consider devices located on the same bus as duplicate and fold the respective
3055 * entries into a single one.
3056 *
3057 * Note: this leaves "gaps" in the array, while preserving the length.
3058 */
Emil Velikovfae59d72015-09-09 17:54:34 +01003059static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3060{
3061 int node_type, i, j;
3062
3063 for (i = 0; i < count; i++) {
3064 for (j = i + 1; j < count; j++) {
3065 if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) {
3066 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3067 node_type = log2(local_devices[j]->available_nodes);
3068 memcpy(local_devices[i]->nodes[node_type],
3069 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3070 drmFreeDevice(&local_devices[j]);
3071 }
3072 }
3073 }
3074}
3075
Emil Velikovb556ea12015-08-17 11:09:06 +08003076/**
Emil Velikovccedf662015-09-09 16:02:18 +01003077 * Get information about the opened drm device
3078 *
3079 * \param fd file descriptor of the drm device
3080 * \param device the address of a drmDevicePtr where the information
3081 * will be allocated in stored
3082 *
3083 * \return zero on success, negative error code otherwise.
3084 */
3085int drmGetDevice(int fd, drmDevicePtr *device)
3086{
3087 drmDevicePtr *local_devices;
3088 drmDevicePtr d;
3089 DIR *sysdir;
3090 struct dirent *dent;
3091 struct stat sbuf;
3092 char node[PATH_MAX + 1];
3093 int node_type, subsystem_type;
3094 int maj, min;
3095 int ret, i, node_count;
3096 int max_count = 16;
Qiang Yu3c208932016-07-14 17:10:55 +08003097 dev_t find_rdev;
Emil Velikovccedf662015-09-09 16:02:18 +01003098
3099 if (fd == -1 || device == NULL)
3100 return -EINVAL;
3101
3102 if (fstat(fd, &sbuf))
3103 return -errno;
3104
Qiang Yu3c208932016-07-14 17:10:55 +08003105 find_rdev = sbuf.st_rdev;
Emil Velikovccedf662015-09-09 16:02:18 +01003106 maj = major(sbuf.st_rdev);
3107 min = minor(sbuf.st_rdev);
3108
3109 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3110 return -EINVAL;
3111
3112 subsystem_type = drmParseSubsystemType(maj, min);
3113
3114 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3115 if (local_devices == NULL)
3116 return -ENOMEM;
3117
3118 sysdir = opendir(DRM_DIR_NAME);
3119 if (!sysdir) {
3120 ret = -errno;
Matt Ropercf0f0362015-10-16 15:11:23 -07003121 goto free_locals;
Emil Velikovccedf662015-09-09 16:02:18 +01003122 }
3123
3124 i = 0;
3125 while ((dent = readdir(sysdir))) {
3126 node_type = drmGetNodeType(dent->d_name);
3127 if (node_type < 0)
3128 continue;
3129
3130 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3131 if (stat(node, &sbuf))
3132 continue;
3133
3134 maj = major(sbuf.st_rdev);
3135 min = minor(sbuf.st_rdev);
3136
3137 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3138 continue;
3139
3140 if (drmParseSubsystemType(maj, min) != subsystem_type)
3141 continue;
3142
3143 switch (subsystem_type) {
3144 case DRM_BUS_PCI:
Emil Velikovb40a65d2016-11-30 16:41:27 +00003145 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true);
Emil Velikovccedf662015-09-09 16:02:18 +01003146 if (ret)
3147 goto free_devices;
3148
3149 break;
3150 default:
Matt Roperba5a6ec2015-10-16 15:11:24 -07003151 continue;
Emil Velikovccedf662015-09-09 16:02:18 +01003152 }
3153
3154 if (i >= max_count) {
3155 drmDevicePtr *temp;
3156
3157 max_count += 16;
3158 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3159 if (!temp)
3160 goto free_devices;
3161 local_devices = temp;
3162 }
3163
Qiang Yu3c208932016-07-14 17:10:55 +08003164 /* store target at local_devices[0] for ease to use below */
3165 if (find_rdev == sbuf.st_rdev && i) {
3166 local_devices[i] = local_devices[0];
3167 local_devices[0] = d;
3168 }
3169 else
3170 local_devices[i] = d;
Emil Velikovccedf662015-09-09 16:02:18 +01003171 i++;
3172 }
3173 node_count = i;
3174
Emil Velikovccedf662015-09-09 16:02:18 +01003175 drmFoldDuplicatedDevices(local_devices, node_count);
3176
3177 *device = local_devices[0];
Qiang Yu3c208932016-07-14 17:10:55 +08003178 drmFreeDevices(&local_devices[1], node_count - 1);
Emil Velikovccedf662015-09-09 16:02:18 +01003179
Emil Velikovccedf662015-09-09 16:02:18 +01003180 closedir(sysdir);
Matt Ropercf0f0362015-10-16 15:11:23 -07003181 free(local_devices);
Rob Herring677cd972016-10-21 10:07:59 -07003182 if (*device == NULL)
3183 return -ENODEV;
Emil Velikovccedf662015-09-09 16:02:18 +01003184 return 0;
3185
3186free_devices:
3187 drmFreeDevices(local_devices, i);
Emil Velikovccedf662015-09-09 16:02:18 +01003188 closedir(sysdir);
Matt Ropercf0f0362015-10-16 15:11:23 -07003189
3190free_locals:
3191 free(local_devices);
Emil Velikovccedf662015-09-09 16:02:18 +01003192 return ret;
3193}
3194
3195/**
Emil Velikovb556ea12015-08-17 11:09:06 +08003196 * Get drm devices on the system
3197 *
3198 * \param devices the array of devices with drmDevicePtr elements
3199 * can be NULL to get the device number first
3200 * \param max_devices the maximum number of devices for the array
3201 *
3202 * \return on error - negative error code,
3203 * if devices is NULL - total number of devices available on the system,
3204 * alternatively the number of devices stored in devices[], which is
3205 * capped by the max_devices.
3206 */
3207int drmGetDevices(drmDevicePtr devices[], int max_devices)
3208{
Emil Velikov5f68d312015-09-07 13:54:32 +01003209 drmDevicePtr *local_devices;
3210 drmDevicePtr device;
3211 DIR *sysdir;
3212 struct dirent *dent;
3213 struct stat sbuf;
3214 char node[PATH_MAX + 1];
Emil Velikovb556ea12015-08-17 11:09:06 +08003215 int node_type, subsystem_type;
3216 int maj, min;
Emil Velikovfae59d72015-09-09 17:54:34 +01003217 int ret, i, node_count, device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08003218 int max_count = 16;
Emil Velikovb556ea12015-08-17 11:09:06 +08003219
Emil Velikov5f68d312015-09-07 13:54:32 +01003220 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3221 if (local_devices == NULL)
Emil Velikovb556ea12015-08-17 11:09:06 +08003222 return -ENOMEM;
3223
3224 sysdir = opendir(DRM_DIR_NAME);
3225 if (!sysdir) {
3226 ret = -errno;
Matt Roper8c4a1cb2015-09-30 09:30:51 -07003227 goto free_locals;
Emil Velikovb556ea12015-08-17 11:09:06 +08003228 }
3229
Emil Velikov5f68d312015-09-07 13:54:32 +01003230 i = 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003231 while ((dent = readdir(sysdir))) {
3232 node_type = drmGetNodeType(dent->d_name);
3233 if (node_type < 0)
3234 continue;
3235
3236 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3237 if (stat(node, &sbuf))
3238 continue;
3239
3240 maj = major(sbuf.st_rdev);
3241 min = minor(sbuf.st_rdev);
3242
3243 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3244 continue;
3245
Emil Velikova250fce2015-09-07 12:54:27 +01003246 subsystem_type = drmParseSubsystemType(maj, min);
Emil Velikovb556ea12015-08-17 11:09:06 +08003247
3248 if (subsystem_type < 0)
3249 continue;
3250
3251 switch (subsystem_type) {
3252 case DRM_BUS_PCI:
Emil Velikovb40a65d2016-11-30 16:41:27 +00003253 ret = drmProcessPciDevice(&device, node, node_type,
Emil Velikovfae59d72015-09-09 17:54:34 +01003254 maj, min, devices != NULL);
Emil Velikovb556ea12015-08-17 11:09:06 +08003255 if (ret)
Emil Velikov5f68d312015-09-07 13:54:32 +01003256 goto free_devices;
Emil Velikovb556ea12015-08-17 11:09:06 +08003257
Emil Velikovb556ea12015-08-17 11:09:06 +08003258 break;
3259 default:
Matt Roperba5a6ec2015-10-16 15:11:24 -07003260 continue;
Emil Velikovb556ea12015-08-17 11:09:06 +08003261 }
Emil Velikov5f68d312015-09-07 13:54:32 +01003262
3263 if (i >= max_count) {
3264 drmDevicePtr *temp;
3265
3266 max_count += 16;
3267 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3268 if (!temp)
3269 goto free_devices;
3270 local_devices = temp;
3271 }
3272
3273 local_devices[i] = device;
Emil Velikovb556ea12015-08-17 11:09:06 +08003274 i++;
3275 }
Emil Velikovb556ea12015-08-17 11:09:06 +08003276 node_count = i;
3277
Emil Velikovfae59d72015-09-09 17:54:34 +01003278 drmFoldDuplicatedDevices(local_devices, node_count);
Emil Velikovb556ea12015-08-17 11:09:06 +08003279
Emil Velikov5f68d312015-09-07 13:54:32 +01003280 device_count = 0;
Qiang Yu70b64072016-06-06 12:29:16 -04003281 for (i = 0; i < node_count; i++) {
Jan Vesely50d3c852016-06-30 14:22:52 -04003282 if (!local_devices[i])
3283 continue;
Qiang Yu70b64072016-06-06 12:29:16 -04003284
Emil Velikov5f68d312015-09-07 13:54:32 +01003285 if ((devices != NULL) && (device_count < max_devices))
3286 devices[device_count] = local_devices[i];
3287 else
3288 drmFreeDevice(&local_devices[i]);
3289
3290 device_count++;
Emil Velikovb556ea12015-08-17 11:09:06 +08003291 }
3292
Emil Velikovb556ea12015-08-17 11:09:06 +08003293 closedir(sysdir);
Matt Roper8c4a1cb2015-09-30 09:30:51 -07003294 free(local_devices);
Emil Velikov5f68d312015-09-07 13:54:32 +01003295 return device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08003296
Emil Velikov5f68d312015-09-07 13:54:32 +01003297free_devices:
3298 drmFreeDevices(local_devices, i);
Emil Velikovb556ea12015-08-17 11:09:06 +08003299 closedir(sysdir);
Matt Roper8c4a1cb2015-09-30 09:30:51 -07003300
3301free_locals:
3302 free(local_devices);
Emil Velikovb556ea12015-08-17 11:09:06 +08003303 return ret;
3304}
Emil Velikov37d790f2016-11-10 17:26:50 +00003305
3306char *drmGetDeviceNameFromFd2(int fd)
3307{
3308#ifdef __linux__
3309 struct stat sbuf;
3310 char *device_name = NULL;
3311 unsigned int maj, min;
3312 FILE *f;
3313 char buf[512];
3314 static const char match[9] = "\nDEVNAME=";
3315 int expected = 1;
3316
3317
3318 if (fstat(fd, &sbuf))
3319 return NULL;
3320
3321 maj = major(sbuf.st_rdev);
3322 min = minor(sbuf.st_rdev);
3323
3324 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3325 return NULL;
3326
3327 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
3328 if (!(f = fopen(buf, "r")))
3329 return NULL;
3330
3331 while (expected < sizeof(match)) {
3332 int c = getc(f);
3333
3334 if (c == EOF) {
3335 fclose(f);
3336 return NULL;
3337 } else if (c == match[expected] )
3338 expected++;
3339 else
3340 expected = 0;
3341 }
3342
3343 strcpy(buf, "/dev/");
3344 if (fgets(buf + 5, sizeof(buf) - 5, f)) {
3345 buf[strcspn(buf, "\n")] = '\0';
3346 device_name = strdup(buf);
3347 }
3348
3349 fclose(f);
3350 return device_name;
3351#else
3352#warning "Missing implementation of drmGetDeviceNameFromFd2"
3353 return NULL;
3354#endif
3355}