blob: 789bfc2481d11bcf922b3edf7913f84c96e745c1 [file] [log] [blame]
Jose Fonsecad2443b22003-05-27 00:37:33 +00001/**
2 * \file xf86drm.c
3 * 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>
48#include <signal.h>
Jesse Barnesf4f76a62009-01-07 10:18:08 -080049#include <time.h>
Dave Airlie79038752006-11-08 15:08:09 +110050#include <sys/types.h>
51#include <sys/stat.h>
52#define stat_t struct stat
53#include <sys/ioctl.h>
Dave Airlie79038752006-11-08 15:08:09 +110054#include <sys/time.h>
55#include <stdarg.h>
Alan Coopersmith0e1135d2015-03-07 11:44:32 -080056#ifdef HAVE_SYS_MKDEV_H
57# include <sys/mkdev.h> /* defines major(), minor(), and makedev() on Solaris */
58#endif
Emil Velikovb556ea12015-08-17 11:09:06 +080059#include <math.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000060
61/* Not all systems have MAP_FAILED defined */
62#ifndef MAP_FAILED
63#define MAP_FAILED ((void *)-1)
64#endif
65
Daryll Straussb3a57661999-12-05 01:19:48 +000066#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010067#include "libdrm_macros.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000068
Emil Velikov5f68d312015-09-07 13:54:32 +010069#include "util_math.h"
70
Jonathan Grayfc083322015-07-21 03:12:56 +100071#ifdef __OpenBSD__
72#define DRM_PRIMARY_MINOR_NAME "drm"
73#define DRM_CONTROL_MINOR_NAME "drmC"
74#define DRM_RENDER_MINOR_NAME "drmR"
75#else
76#define DRM_PRIMARY_MINOR_NAME "card"
77#define DRM_CONTROL_MINOR_NAME "controlD"
78#define DRM_RENDER_MINOR_NAME "renderD"
79#endif
80
Hasso Tepper27c37852008-04-07 15:27:43 +030081#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Eric Anholtcfa778a2003-02-21 23:23:09 +000082#define DRM_MAJOR 145
Rik Faith88dbee52001-02-28 09:27:44 +000083#endif
84
Eric Anholtcfa778a2003-02-21 23:23:09 +000085#ifdef __NetBSD__
86#define DRM_MAJOR 34
87#endif
88
Jonathan Gray66c3afb2015-07-21 03:12:10 +100089#ifdef __OpenBSD__
90#ifdef __i386__
91#define DRM_MAJOR 88
92#else
93#define DRM_MAJOR 87
94#endif
95#endif /* __OpenBSD__ */
Alan Hourihaneb0a92852003-09-24 14:39:25 +000096
Eric Anholtcfa778a2003-02-21 23:23:09 +000097#ifndef DRM_MAJOR
98#define DRM_MAJOR 226 /* Linux */
Rik Faith88dbee52001-02-28 09:27:44 +000099#endif
100
Adam Jackson22e41ef2006-02-20 23:09:00 +0000101/*
102 * This definition needs to be changed on some systems if dev_t is a structure.
103 * If there is a header file we can get it from, there would be best.
104 */
Brian Paul569da5a2000-06-08 14:38:22 +0000105#ifndef makedev
Brian Paul569da5a2000-06-08 14:38:22 +0000106#define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
107#endif
108
David Dawes56bd9c22001-07-30 19:59:39 +0000109#define DRM_MSG_VERBOSITY 3
110
Daniel Vetterfd387942015-02-11 12:41:04 +0100111#define memclear(s) memset(&s, 0, sizeof(s))
112
Dave Airlie79038752006-11-08 15:08:09 +1100113static drmServerInfoPtr drm_server_info;
114
115void drmSetServerInfo(drmServerInfoPtr info)
116{
Brianccd7b6e2007-05-29 14:54:00 -0600117 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +1100118}
119
Jose Fonsecad2443b22003-05-27 00:37:33 +0000120/**
121 * Output a message to stderr.
122 *
123 * \param format printf() like format string.
124 *
125 * \internal
126 * This function is a wrapper around vfprintf().
127 */
Dave Airlie79038752006-11-08 15:08:09 +1100128
Thierry Reding44b08c02014-01-22 12:06:51 +0100129static int DRM_PRINTFLIKE(1, 0)
130drmDebugPrint(const char *format, va_list ap)
Dave Airlie79038752006-11-08 15:08:09 +1100131{
Brianccd7b6e2007-05-29 14:54:00 -0600132 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100133}
134
Eric Anholtc4857422008-06-03 10:20:49 -0700135void
David Dawes56bd9c22001-07-30 19:59:39 +0000136drmMsg(const char *format, ...)
137{
138 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000139 const char *env;
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400140 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
141 (drm_server_info && drm_server_info->debug_print))
David Dawes56bd9c22001-07-30 19:59:39 +0000142 {
143 va_start(ap, format);
Dave Airlie79038752006-11-08 15:08:09 +1100144 if (drm_server_info) {
145 drm_server_info->debug_print(format,ap);
146 } else {
Jan Veselycfbe9c92015-03-18 14:17:26 -0400147 drmDebugPrint(format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100148 }
David Dawes56bd9c22001-07-30 19:59:39 +0000149 va_end(ap);
150 }
151}
152
Daryll Straussb3a57661999-12-05 01:19:48 +0000153static void *drmHashTable = NULL; /* Context switch callbacks */
154
Dave Airlie79038752006-11-08 15:08:09 +1100155void *drmGetHashTable(void)
156{
Brianccd7b6e2007-05-29 14:54:00 -0600157 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100158}
Daryll Straussb3a57661999-12-05 01:19:48 +0000159
160void *drmMalloc(int size)
161{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100162 return calloc(1, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000163}
164
165void drmFree(void *pt)
166{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100167 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000168}
169
Keith Packard8b9ab102008-06-13 16:03:22 -0700170/**
171 * Call ioctl, restarting if it is interupted
172 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800173int
Coleman Kane41b83a92008-08-18 17:08:21 -0400174drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700175{
176 int ret;
177
178 do {
179 ret = ioctl(fd, request, arg);
180 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
181 return ret;
182}
Daryll Straussb3a57661999-12-05 01:19:48 +0000183
Daryll Straussb3a57661999-12-05 01:19:48 +0000184static unsigned long drmGetKeyFromFd(int fd)
185{
David Dawesfcc21062001-03-30 17:16:20 +0000186 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000187
188 st.st_rdev = 0;
189 fstat(fd, &st);
190 return st.st_rdev;
191}
192
Dave Airlie79038752006-11-08 15:08:09 +1100193drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000194{
195 unsigned long key = drmGetKeyFromFd(fd);
196 void *value;
197 drmHashEntry *entry;
198
Brianccd7b6e2007-05-29 14:54:00 -0600199 if (!drmHashTable)
200 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000201
202 if (drmHashLookup(drmHashTable, key, &value)) {
203 entry = drmMalloc(sizeof(*entry));
204 entry->fd = fd;
205 entry->f = NULL;
206 entry->tagTable = drmHashCreate();
207 drmHashInsert(drmHashTable, key, entry);
208 } else {
209 entry = value;
210 }
211 return entry;
212}
213
Jose Fonsecad2443b22003-05-27 00:37:33 +0000214/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000215 * Compare two busid strings
216 *
217 * \param first
218 * \param second
219 *
220 * \return 1 if matched.
221 *
222 * \internal
223 * This function compares two bus ID strings. It understands the older
224 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
225 * domain, b is bus, d is device, f is function.
226 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000227static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000228{
229 /* First, check if the IDs are exactly the same */
230 if (strcasecmp(id1, id2) == 0)
231 return 1;
232
233 /* Try to match old/new-style PCI bus IDs. */
234 if (strncasecmp(id1, "pci", 3) == 0) {
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300235 unsigned int o1, b1, d1, f1;
236 unsigned int o2, b2, d2, f2;
Eric Anholt06cb1322003-10-23 02:23:31 +0000237 int ret;
238
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300239 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000240 if (ret != 4) {
241 o1 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300242 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000243 if (ret != 3)
244 return 0;
245 }
246
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300247 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000248 if (ret != 4) {
249 o2 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300250 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000251 if (ret != 3)
252 return 0;
253 }
254
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000255 /* If domains aren't properly supported by the kernel interface,
256 * just ignore them, which sucks less than picking a totally random
257 * card with "open by name"
258 */
259 if (!pci_domain_ok)
260 o1 = o2 = 0;
261
Eric Anholt06cb1322003-10-23 02:23:31 +0000262 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
263 return 0;
264 else
265 return 1;
266 }
267 return 0;
268}
269
270/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300271 * Handles error checking for chown call.
272 *
273 * \param path to file.
274 * \param id of the new owner.
275 * \param id of the new group.
276 *
277 * \return zero if success or -1 if failure.
278 *
279 * \internal
280 * Checks for failure. If failure was caused by signal call chown again.
281 * If any other failure happened then it will output error mesage using
282 * drmMsg() call.
283 */
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500284#if !defined(UDEV)
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300285static int chown_check_return(const char *path, uid_t owner, gid_t group)
286{
287 int rv;
288
289 do {
290 rv = chown(path, owner, group);
291 } while (rv != 0 && errno == EINTR);
292
293 if (rv == 0)
294 return 0;
295
296 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
297 path, errno, strerror(errno));
298 return -1;
299}
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500300#endif
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300301
302/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000303 * Open the DRM device, creating it if necessary.
304 *
305 * \param dev major and minor numbers of the device.
306 * \param minor minor number of the device.
307 *
308 * \return a file descriptor on success, or a negative value on error.
309 *
310 * \internal
311 * Assembles the device name from \p minor and opens it, creating the device
312 * special file node with the major and minor numbers specified by \p dev and
313 * parent directory if necessary and was called by root.
314 */
Jan Veselyde8532d2014-11-30 12:53:18 -0500315static int drmOpenDevice(dev_t dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000316{
David Dawes9c775d02001-05-14 14:49:58 +0000317 stat_t st;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000318 const char *dev_name;
Rik Faith88dbee52001-02-28 09:27:44 +0000319 char buf[64];
320 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100321 mode_t devmode = DRM_DEV_MODE, serv_mode;
Jan Vesely0706c142015-02-27 12:47:46 -0500322 gid_t serv_group;
323#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000324 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000325 uid_t user = DRM_DEV_UID;
Jan Vesely0706c142015-02-27 12:47:46 -0500326 gid_t group = DRM_DEV_GID;
327#endif
328
Frank Binns0c5aaee2015-01-14 14:07:51 +0000329 switch (type) {
330 case DRM_NODE_PRIMARY:
331 dev_name = DRM_DEV_NAME;
332 break;
333 case DRM_NODE_CONTROL:
334 dev_name = DRM_CONTROL_DEV_NAME;
335 break;
336 case DRM_NODE_RENDER:
337 dev_name = DRM_RENDER_DEV_NAME;
338 break;
339 default:
340 return -EINVAL;
341 };
342
343 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000344 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000345
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400346 if (drm_server_info && drm_server_info->get_perms) {
Brianccd7b6e2007-05-29 14:54:00 -0600347 drm_server_info->get_perms(&serv_group, &serv_mode);
348 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
349 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Dave Airlie79038752006-11-08 15:08:09 +1100350 }
Brian Paul569da5a2000-06-08 14:38:22 +0000351
Dave Airlie9101a022008-08-24 16:54:43 +1000352#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000353 if (stat(DRM_DIR_NAME, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600354 if (!isroot)
355 return DRM_ERR_NOT_ROOT;
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000356 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300357 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000358 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000359 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000360
Eric Anholt06cb1322003-10-23 02:23:31 +0000361 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000362 if (stat(buf, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600363 if (!isroot)
364 return DRM_ERR_NOT_ROOT;
Rik Faith88dbee52001-02-28 09:27:44 +0000365 remove(buf);
366 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000367 }
Dave Airlie79038752006-11-08 15:08:09 +1100368
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400369 if (drm_server_info && drm_server_info->get_perms) {
Jammy Zhou454b1492015-04-27 10:29:55 +0800370 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300371 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600372 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100373 }
Dave Airlie9101a022008-08-24 16:54:43 +1000374#else
375 /* if we modprobed then wait for udev */
376 {
377 int udev_count = 0;
378wait_for_udev:
379 if (stat(DRM_DIR_NAME, &st)) {
380 usleep(20);
381 udev_count++;
382
383 if (udev_count == 50)
384 return -1;
385 goto wait_for_udev;
386 }
387
388 if (stat(buf, &st)) {
389 usleep(20);
390 udev_count++;
391
392 if (udev_count == 50)
393 return -1;
394 goto wait_for_udev;
395 }
396 }
397#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000398
David Dawes56bd9c22001-07-30 19:59:39 +0000399 fd = open(buf, O_RDWR, 0);
400 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
401 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600402 if (fd >= 0)
403 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000404
Dave Airlie39e5e982010-12-07 14:26:09 +1000405#if !defined(UDEV)
Eric Anholt06cb1322003-10-23 02:23:31 +0000406 /* Check if the device node is not what we expect it to be, and recreate it
407 * and try again if so.
408 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000409 if (st.st_rdev != dev) {
Brianccd7b6e2007-05-29 14:54:00 -0600410 if (!isroot)
411 return DRM_ERR_NOT_ROOT;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000412 remove(buf);
413 mknod(buf, S_IFCHR | devmode, dev);
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400414 if (drm_server_info && drm_server_info->get_perms) {
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300415 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600416 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100417 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000418 }
419 fd = open(buf, O_RDWR, 0);
420 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
421 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600422 if (fd >= 0)
423 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000424
David Dawes56bd9c22001-07-30 19:59:39 +0000425 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000426 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000427#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000428 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000429}
430
Jose Fonsecad2443b22003-05-27 00:37:33 +0000431
432/**
433 * Open the DRM device
434 *
435 * \param minor device minor number.
436 * \param create allow to create the device if set.
437 *
438 * \return a file descriptor on success, or a negative value on error.
439 *
440 * \internal
441 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
442 * name from \p minor and opens it.
443 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800444static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000445{
446 int fd;
447 char buf[64];
Frank Binns0c5aaee2015-01-14 14:07:51 +0000448 const char *dev_name;
Dave Airliedb85ed22008-02-13 12:20:02 +1000449
Brianccd7b6e2007-05-29 14:54:00 -0600450 if (create)
Jesse Barnes731cd552008-12-17 10:09:49 -0800451 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
Rik Faith88dbee52001-02-28 09:27:44 +0000452
Frank Binns0c5aaee2015-01-14 14:07:51 +0000453 switch (type) {
454 case DRM_NODE_PRIMARY:
455 dev_name = DRM_DEV_NAME;
456 break;
457 case DRM_NODE_CONTROL:
458 dev_name = DRM_CONTROL_DEV_NAME;
459 break;
460 case DRM_NODE_RENDER:
461 dev_name = DRM_RENDER_DEV_NAME;
462 break;
463 default:
464 return -EINVAL;
465 };
466
467 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Brianccd7b6e2007-05-29 14:54:00 -0600468 if ((fd = open(buf, O_RDWR, 0)) >= 0)
469 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000470 return -errno;
471}
472
Brian Paul569da5a2000-06-08 14:38:22 +0000473
Jose Fonsecad2443b22003-05-27 00:37:33 +0000474/**
475 * Determine whether the DRM kernel driver has been loaded.
476 *
477 * \return 1 if the DRM driver is loaded, 0 otherwise.
478 *
479 * \internal
480 * Determine the presence of the kernel driver by attempting to open the 0
481 * minor and get version information. For backward compatibility with older
482 * Linux implementations, /proc/dri is also checked.
483 */
Brian Paul569da5a2000-06-08 14:38:22 +0000484int drmAvailable(void)
485{
Brian Paul569da5a2000-06-08 14:38:22 +0000486 drmVersionPtr version;
487 int retval = 0;
488 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000489
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000490 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000491#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000492 /* Try proc for backward Linux compatibility */
Brianccd7b6e2007-05-29 14:54:00 -0600493 if (!access("/proc/dri/0", R_OK))
494 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000495#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000496 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000497 }
Rik Faith88dbee52001-02-28 09:27:44 +0000498
499 if ((version = drmGetVersion(fd))) {
500 retval = 1;
501 drmFreeVersion(version);
502 }
503 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000504
505 return retval;
506}
507
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800508static int drmGetMinorBase(int type)
509{
510 switch (type) {
511 case DRM_NODE_PRIMARY:
512 return 0;
513 case DRM_NODE_CONTROL:
514 return 64;
515 case DRM_NODE_RENDER:
516 return 128;
517 default:
518 return -1;
519 };
520}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000521
Frank Binns1f735782015-02-13 10:51:15 +0000522static int drmGetMinorType(int minor)
523{
524 int type = minor >> 6;
525
526 if (minor < 0)
527 return -1;
528
529 switch (type) {
530 case DRM_NODE_PRIMARY:
531 case DRM_NODE_CONTROL:
532 case DRM_NODE_RENDER:
533 return type;
534 default:
535 return -1;
536 }
537}
538
Emil Velikov0ca03a42015-03-07 00:58:39 +0000539static const char *drmGetMinorName(int type)
540{
541 switch (type) {
542 case DRM_NODE_PRIMARY:
Jonathan Grayfc083322015-07-21 03:12:56 +1000543 return DRM_PRIMARY_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000544 case DRM_NODE_CONTROL:
Jonathan Grayfc083322015-07-21 03:12:56 +1000545 return DRM_CONTROL_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000546 case DRM_NODE_RENDER:
Jonathan Grayfc083322015-07-21 03:12:56 +1000547 return DRM_RENDER_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000548 default:
549 return NULL;
550 }
551}
552
Jose Fonsecad2443b22003-05-27 00:37:33 +0000553/**
554 * Open the device by bus ID.
555 *
556 * \param busid bus ID.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800557 * \param type device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000558 *
559 * \return a file descriptor on success, or a negative value on error.
560 *
561 * \internal
562 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
563 * comparing the device bus ID with the one supplied.
564 *
565 * \sa drmOpenMinor() and drmGetBusid().
566 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800567static int drmOpenByBusid(const char *busid, int type)
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000568{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000569 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000570 int fd;
571 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000572 drmSetVersion sv;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800573 int base = drmGetMinorBase(type);
574
575 if (base < 0)
576 return -1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000577
578 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800579 for (i = base; i < base + DRM_MAX_MINOR; i++) {
580 fd = drmOpenMinor(i, 1, type);
David Dawes56bd9c22001-07-30 19:59:39 +0000581 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
582 if (fd >= 0) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000583 /* We need to try for 1.4 first for proper PCI domain support
584 * and if that fails, we know the kernel is busted
585 */
Eric Anholt06cb1322003-10-23 02:23:31 +0000586 sv.drm_di_major = 1;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000587 sv.drm_di_minor = 4;
Eric Anholt06cb1322003-10-23 02:23:31 +0000588 sv.drm_dd_major = -1; /* Don't care */
Eric Anholt26462b92005-12-31 11:48:12 +0000589 sv.drm_dd_minor = -1; /* Don't care */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000590 if (drmSetInterfaceVersion(fd, &sv)) {
591#ifndef __alpha__
592 pci_domain_ok = 0;
593#endif
594 sv.drm_di_major = 1;
595 sv.drm_di_minor = 1;
596 sv.drm_dd_major = -1; /* Don't care */
597 sv.drm_dd_minor = -1; /* Don't care */
Thierry Reding303ff262014-04-08 22:33:04 +0200598 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000599 drmSetInterfaceVersion(fd, &sv);
600 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000601 buf = drmGetBusid(fd);
David Dawes56bd9c22001-07-30 19:59:39 +0000602 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000603 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
Rik Faith88dbee52001-02-28 09:27:44 +0000604 drmFreeBusid(buf);
605 return fd;
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000606 }
Brianccd7b6e2007-05-29 14:54:00 -0600607 if (buf)
608 drmFreeBusid(buf);
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000609 close(fd);
610 }
611 }
612 return -1;
613}
614
Jose Fonsecad2443b22003-05-27 00:37:33 +0000615
616/**
617 * Open the device by name.
618 *
619 * \param name driver name.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800620 * \param type the device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000621 *
622 * \return a file descriptor on success, or a negative value on error.
623 *
624 * \internal
625 * This function opens the first minor number that matches the driver name and
626 * isn't already in use. If it's in use it then it will already have a bus ID
627 * assigned.
628 *
629 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
630 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800631static int drmOpenByName(const char *name, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000632{
Rik Faith88dbee52001-02-28 09:27:44 +0000633 int i;
634 int fd;
635 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000636 char * id;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800637 int base = drmGetMinorBase(type);
638
639 if (base < 0)
640 return -1;
Dave Airliedb85ed22008-02-13 12:20:02 +1000641
David Dawes56bd9c22001-07-30 19:59:39 +0000642 /*
643 * Open the first minor number that matches the driver name and isn't
644 * already in use. If it's in use it will have a busid assigned already.
645 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800646 for (i = base; i < base + DRM_MAX_MINOR; i++) {
647 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
Rik Faith88dbee52001-02-28 09:27:44 +0000648 if ((version = drmGetVersion(fd))) {
649 if (!strcmp(version->name, name)) {
650 drmFreeVersion(version);
David Dawes56bd9c22001-07-30 19:59:39 +0000651 id = drmGetBusid(fd);
652 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
653 if (!id || !*id) {
Adam Jackson22e41ef2006-02-20 23:09:00 +0000654 if (id)
David Dawes56bd9c22001-07-30 19:59:39 +0000655 drmFreeBusid(id);
David Dawes56bd9c22001-07-30 19:59:39 +0000656 return fd;
657 } else {
658 drmFreeBusid(id);
659 }
660 } else {
661 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000662 }
Rik Faith88dbee52001-02-28 09:27:44 +0000663 }
David Dawes56bd9c22001-07-30 19:59:39 +0000664 close(fd);
Rik Faith88dbee52001-02-28 09:27:44 +0000665 }
666 }
667
668#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000669 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000670 for (i = 0; i < 8; i++) {
Rik Faith88dbee52001-02-28 09:27:44 +0000671 char proc_name[64], buf[512];
672 char *driver, *pt, *devstring;
673 int retcode;
674
Daryll Strauss0371c291999-12-18 18:34:59 +0000675 sprintf(proc_name, "/proc/dri/%d/name", i);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000676 if ((fd = open(proc_name, 0, 0)) >= 0) {
677 retcode = read(fd, buf, sizeof(buf)-1);
678 close(fd);
679 if (retcode) {
680 buf[retcode-1] = '\0';
681 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
682 ;
Adam Jackson22e41ef2006-02-20 23:09:00 +0000683 if (*pt) { /* Device is next */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000684 *pt = '\0';
685 if (!strcmp(driver, name)) { /* Match */
686 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
687 ;
688 if (*pt) { /* Found busid */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800689 return drmOpenByBusid(++pt, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000690 } else { /* No busid */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800691 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000692 }
693 }
694 }
695 }
Brian Paul569da5a2000-06-08 14:38:22 +0000696 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000697 }
Rik Faith88dbee52001-02-28 09:27:44 +0000698#endif
699
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000700 return -1;
701}
702
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000703
Jose Fonsecad2443b22003-05-27 00:37:33 +0000704/**
705 * Open the DRM device.
706 *
707 * Looks up the specified name and bus ID, and opens the device found. The
708 * entry in /dev/dri is created if necessary and if called by root.
709 *
710 * \param name driver name. Not referenced if bus ID is supplied.
711 * \param busid bus ID. Zero if not known.
712 *
713 * \return a file descriptor on success, or a negative value on error.
714 *
715 * \internal
716 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
717 * otherwise.
718 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000719int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000720{
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800721 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
722}
723
724/**
725 * Open the DRM device with specified type.
726 *
727 * Looks up the specified name and bus ID, and opens the device found. The
728 * entry in /dev/dri is created if necessary and if called by root.
729 *
730 * \param name driver name. Not referenced if bus ID is supplied.
731 * \param busid bus ID. Zero if not known.
732 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
733 *
734 * \return a file descriptor on success, or a negative value on error.
735 *
736 * \internal
737 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
738 * otherwise.
739 */
740int drmOpenWithType(const char *name, const char *busid, int type)
741{
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400742 if (!drmAvailable() && name != NULL && drm_server_info &&
743 drm_server_info->load_module) {
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800744 /* try to load the kernel module */
Dave Airlie79038752006-11-08 15:08:09 +1100745 if (!drm_server_info->load_module(name)) {
Brianccd7b6e2007-05-29 14:54:00 -0600746 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
Eric Anholt06cb1322003-10-23 02:23:31 +0000747 return -1;
748 }
749 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000750
Eric Anholt06cb1322003-10-23 02:23:31 +0000751 if (busid) {
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800752 int fd = drmOpenByBusid(busid, type);
Eric Anholt06cb1322003-10-23 02:23:31 +0000753 if (fd >= 0)
754 return fd;
755 }
Adam Jackson22e41ef2006-02-20 23:09:00 +0000756
Eric Anholt06cb1322003-10-23 02:23:31 +0000757 if (name)
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800758 return drmOpenByName(name, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000759
Eric Anholt06cb1322003-10-23 02:23:31 +0000760 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000761}
762
Jesse Barnes731cd552008-12-17 10:09:49 -0800763int drmOpenControl(int minor)
764{
765 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
766}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000767
Frank Binns0c5aaee2015-01-14 14:07:51 +0000768int drmOpenRender(int minor)
769{
770 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
771}
772
Jose Fonsecad2443b22003-05-27 00:37:33 +0000773/**
774 * Free the version information returned by drmGetVersion().
775 *
776 * \param v pointer to the version information.
777 *
778 * \internal
779 * It frees the memory pointed by \p %v as well as all the non-null strings
780 * pointers in it.
781 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000782void drmFreeVersion(drmVersionPtr v)
783{
Brianccd7b6e2007-05-29 14:54:00 -0600784 if (!v)
785 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100786 drmFree(v->name);
787 drmFree(v->date);
788 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000789 drmFree(v);
790}
791
Jose Fonsecad2443b22003-05-27 00:37:33 +0000792
793/**
794 * Free the non-public version information returned by the kernel.
795 *
796 * \param v pointer to the version information.
797 *
798 * \internal
799 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
800 * the non-null strings pointers in it.
801 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000802static void drmFreeKernelVersion(drm_version_t *v)
803{
Brianccd7b6e2007-05-29 14:54:00 -0600804 if (!v)
805 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100806 drmFree(v->name);
807 drmFree(v->date);
808 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000809 drmFree(v);
810}
811
Jose Fonsecad2443b22003-05-27 00:37:33 +0000812
813/**
814 * Copy version information.
815 *
816 * \param d destination pointer.
817 * \param s source pointer.
818 *
819 * \internal
820 * Used by drmGetVersion() to translate the information returned by the ioctl
821 * interface in a private structure into the public structure counterpart.
822 */
Brian Paul569da5a2000-06-08 14:38:22 +0000823static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000824{
825 d->version_major = s->version_major;
826 d->version_minor = s->version_minor;
827 d->version_patchlevel = s->version_patchlevel;
828 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400829 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000830 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400831 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000832 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400833 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000834}
835
Daryll Straussb3a57661999-12-05 01:19:48 +0000836
Jose Fonsecad2443b22003-05-27 00:37:33 +0000837/**
838 * Query the driver version information.
839 *
840 * \param fd file descriptor.
841 *
842 * \return pointer to a drmVersion structure which should be freed with
843 * drmFreeVersion().
844 *
845 * \note Similar information is available via /proc/dri.
846 *
847 * \internal
848 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
849 * first with zeros to get the string lengths, and then the actually strings.
850 * It also null-terminates them since they might not be already.
851 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000852drmVersionPtr drmGetVersion(int fd)
853{
854 drmVersionPtr retval;
855 drm_version_t *version = drmMalloc(sizeof(*version));
856
Daniel Vetter95f23cf2015-02-11 17:25:30 +0100857 memclear(*version);
Gareth Hughes36047532001-02-15 08:12:14 +0000858
Keith Packard8b9ab102008-06-13 16:03:22 -0700859 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000860 drmFreeKernelVersion(version);
861 return NULL;
862 }
863
Daryll Straussb3a57661999-12-05 01:19:48 +0000864 if (version->name_len)
865 version->name = drmMalloc(version->name_len + 1);
866 if (version->date_len)
867 version->date = drmMalloc(version->date_len + 1);
868 if (version->desc_len)
869 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000870
Keith Packard8b9ab102008-06-13 16:03:22 -0700871 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000872 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
Daryll Straussb3a57661999-12-05 01:19:48 +0000873 drmFreeKernelVersion(version);
874 return NULL;
875 }
876
Adam Jackson22e41ef2006-02-20 23:09:00 +0000877 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000878 if (version->name_len) version->name[version->name_len] = '\0';
879 if (version->date_len) version->date[version->date_len] = '\0';
880 if (version->desc_len) version->desc[version->desc_len] = '\0';
881
Daryll Straussb3a57661999-12-05 01:19:48 +0000882 retval = drmMalloc(sizeof(*retval));
883 drmCopyVersion(retval, version);
884 drmFreeKernelVersion(version);
885 return retval;
886}
887
Jens Owen3903e5a2002-04-09 21:54:56 +0000888
Jose Fonsecad2443b22003-05-27 00:37:33 +0000889/**
890 * Get version information for the DRM user space library.
891 *
892 * This version number is driver independent.
893 *
894 * \param fd file descriptor.
895 *
896 * \return version information.
897 *
898 * \internal
899 * This function allocates and fills a drm_version structure with a hard coded
900 * version number.
901 */
Jens Owen3903e5a2002-04-09 21:54:56 +0000902drmVersionPtr drmGetLibVersion(int fd)
903{
904 drm_version_t *version = drmMalloc(sizeof(*version));
905
906 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100907 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000908 * revision 1.0.x = original DRM interface with no drmGetLibVersion
909 * entry point and many drm<Device> extensions
910 * revision 1.1.x = added drmCommand entry points for device extensions
911 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000912 * revision 1.2.x = added drmSetInterfaceVersion
913 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100914 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000915 */
Dave Airlie79038752006-11-08 15:08:09 +1100916 version->version_major = 1;
917 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000918 version->version_patchlevel = 0;
919
920 return (drmVersionPtr)version;
921}
922
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000923int drmGetCap(int fd, uint64_t capability, uint64_t *value)
924{
Daniel Vetterfd387942015-02-11 12:41:04 +0100925 struct drm_get_cap cap;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000926 int ret;
927
Daniel Vetterfd387942015-02-11 12:41:04 +0100928 memclear(cap);
929 cap.capability = capability;
930
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000931 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
932 if (ret)
933 return ret;
934
935 *value = cap.value;
Dave Airlie3b04c732011-03-04 15:48:31 +1000936 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000937}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000938
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100939int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
940{
Daniel Vetterfd387942015-02-11 12:41:04 +0100941 struct drm_set_client_cap cap;
942
943 memclear(cap);
944 cap.capability = capability;
945 cap.value = value;
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100946
947 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
948}
949
Jose Fonsecad2443b22003-05-27 00:37:33 +0000950/**
951 * Free the bus ID information.
952 *
953 * \param busid bus ID information string as given by drmGetBusid().
954 *
955 * \internal
956 * This function is just frees the memory pointed by \p busid.
957 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000958void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000959{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000960 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000961}
962
Jose Fonsecad2443b22003-05-27 00:37:33 +0000963
964/**
965 * Get the bus ID of the device.
966 *
967 * \param fd file descriptor.
968 *
969 * \return bus ID string.
970 *
971 * \internal
972 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
973 * get the string length and data, passing the arguments in a drm_unique
974 * structure.
975 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000976char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000977{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000978 drm_unique_t u;
979
Daniel Vetterfd387942015-02-11 12:41:04 +0100980 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000981
Keith Packard8b9ab102008-06-13 16:03:22 -0700982 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600983 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000984 u.unique = drmMalloc(u.unique_len + 1);
Keith Packard8b9ab102008-06-13 16:03:22 -0700985 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600986 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000987 u.unique[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +0000988
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000989 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +0000990}
991
Jose Fonsecad2443b22003-05-27 00:37:33 +0000992
993/**
994 * Set the bus ID of the device.
995 *
996 * \param fd file descriptor.
997 * \param busid bus ID string.
998 *
999 * \return zero on success, negative on failure.
1000 *
1001 * \internal
1002 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1003 * the arguments in a drm_unique structure.
1004 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001005int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +00001006{
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001007 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +00001008
Daniel Vetterfd387942015-02-11 12:41:04 +01001009 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001010 u.unique = (char *)busid;
1011 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +00001012
Keith Packard8b9ab102008-06-13 16:03:22 -07001013 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
David Dawes56bd9c22001-07-30 19:59:39 +00001014 return -errno;
1015 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001016 return 0;
1017}
1018
Jon Smirl8696e712004-07-07 04:36:36 +00001019int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001020{
1021 drm_auth_t auth;
1022
Daniel Vetterfd387942015-02-11 12:41:04 +01001023 memclear(auth);
1024
Daryll Straussb3a57661999-12-05 01:19:48 +00001025 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001026 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -06001027 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001028 *magic = auth.magic;
1029 return 0;
1030}
1031
Jon Smirl8696e712004-07-07 04:36:36 +00001032int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001033{
1034 drm_auth_t auth;
1035
Daniel Vetterfd387942015-02-11 12:41:04 +01001036 memclear(auth);
Daryll Straussb3a57661999-12-05 01:19:48 +00001037 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -07001038 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -06001039 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001040 return 0;
1041}
1042
Jose Fonsecad2443b22003-05-27 00:37:33 +00001043/**
1044 * Specifies a range of memory that is available for mapping by a
1045 * non-root process.
1046 *
1047 * \param fd file descriptor.
1048 * \param offset usually the physical address. The actual meaning depends of
1049 * the \p type parameter. See below.
1050 * \param size of the memory in bytes.
1051 * \param type type of the memory to be mapped.
1052 * \param flags combination of several flags to modify the function actions.
1053 * \param handle will be set to a value that may be used as the offset
1054 * parameter for mmap().
1055 *
1056 * \return zero on success or a negative value on error.
1057 *
1058 * \par Mapping the frame buffer
1059 * For the frame buffer
1060 * - \p offset will be the physical address of the start of the frame buffer,
1061 * - \p size will be the size of the frame buffer in bytes, and
1062 * - \p type will be DRM_FRAME_BUFFER.
1063 *
1064 * \par
1065 * The area mapped will be uncached. If MTRR support is available in the
1066 * kernel, the frame buffer area will be set to write combining.
1067 *
1068 * \par Mapping the MMIO register area
1069 * For the MMIO register area,
1070 * - \p offset will be the physical address of the start of the register area,
1071 * - \p size will be the size of the register area bytes, and
1072 * - \p type will be DRM_REGISTERS.
1073 * \par
1074 * The area mapped will be uncached.
1075 *
1076 * \par Mapping the SAREA
1077 * For the SAREA,
1078 * - \p offset will be ignored and should be set to zero,
1079 * - \p size will be the desired size of the SAREA in bytes,
1080 * - \p type will be DRM_SHM.
1081 *
1082 * \par
1083 * A shared memory area of the requested size will be created and locked in
1084 * kernel memory. This area may be mapped into client-space by using the handle
1085 * returned.
1086 *
1087 * \note May only be called by root.
1088 *
1089 * \internal
1090 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1091 * the arguments in a drm_map structure.
1092 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001093int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1094 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001095{
1096 drm_map_t map;
1097
Daniel Vetterfd387942015-02-11 12:41:04 +01001098 memclear(map);
Daryll Straussb3a57661999-12-05 01:19:48 +00001099 map.offset = offset;
1100 map.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001101 map.type = type;
1102 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -07001103 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001104 return -errno;
1105 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001106 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001107 return 0;
1108}
1109
Jon Smirl8696e712004-07-07 04:36:36 +00001110int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00001111{
1112 drm_map_t map;
1113
Daniel Vetterfd387942015-02-11 12:41:04 +01001114 memclear(map);
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001115 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001116
Keith Packard8b9ab102008-06-13 16:03:22 -07001117 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001118 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001119 return 0;
1120}
1121
Jose Fonsecad2443b22003-05-27 00:37:33 +00001122/**
1123 * Make buffers available for DMA transfers.
1124 *
1125 * \param fd file descriptor.
1126 * \param count number of buffers.
1127 * \param size size of each buffer.
1128 * \param flags buffer allocation flags.
1129 * \param agp_offset offset in the AGP aperture
1130 *
1131 * \return number of buffers allocated, negative on error.
1132 *
1133 * \internal
1134 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1135 *
1136 * \sa drm_buf_desc.
1137 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001138int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1139 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001140{
1141 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001142
Daniel Vetterfd387942015-02-11 12:41:04 +01001143 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001144 request.count = count;
1145 request.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001146 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001147 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001148
Keith Packard8b9ab102008-06-13 16:03:22 -07001149 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001150 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001151 return request.count;
1152}
1153
1154int drmMarkBufs(int fd, double low, double high)
1155{
1156 drm_buf_info_t info;
1157 int i;
1158
Daniel Vetterfd387942015-02-11 12:41:04 +01001159 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001160
Keith Packard8b9ab102008-06-13 16:03:22 -07001161 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001162 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001163
Brianccd7b6e2007-05-29 14:54:00 -06001164 if (!info.count)
1165 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001166
Daryll Straussb3a57661999-12-05 01:19:48 +00001167 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1168 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001169
Keith Packard8b9ab102008-06-13 16:03:22 -07001170 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001171 int retval = -errno;
1172 drmFree(info.list);
1173 return retval;
1174 }
Gareth Hughes36047532001-02-15 08:12:14 +00001175
Daryll Straussb3a57661999-12-05 01:19:48 +00001176 for (i = 0; i < info.count; i++) {
1177 info.list[i].low_mark = low * info.list[i].count;
1178 info.list[i].high_mark = high * info.list[i].count;
Keith Packard8b9ab102008-06-13 16:03:22 -07001179 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001180 int retval = -errno;
1181 drmFree(info.list);
1182 return retval;
1183 }
1184 }
1185 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001186
Daryll Straussb3a57661999-12-05 01:19:48 +00001187 return 0;
1188}
1189
Jose Fonsecad2443b22003-05-27 00:37:33 +00001190/**
1191 * Free buffers.
1192 *
1193 * \param fd file descriptor.
1194 * \param count number of buffers to free.
1195 * \param list list of buffers to be freed.
1196 *
1197 * \return zero on success, or a negative value on failure.
1198 *
1199 * \note This function is primarily used for debugging.
1200 *
1201 * \internal
1202 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1203 * the arguments in a drm_buf_free structure.
1204 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001205int drmFreeBufs(int fd, int count, int *list)
1206{
1207 drm_buf_free_t request;
1208
Daniel Vetterfd387942015-02-11 12:41:04 +01001209 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001210 request.count = count;
1211 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001212 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001213 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001214 return 0;
1215}
1216
Jose Fonsecad2443b22003-05-27 00:37:33 +00001217
1218/**
1219 * Close the device.
1220 *
1221 * \param fd file descriptor.
1222 *
1223 * \internal
1224 * This function closes the file descriptor.
1225 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001226int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001227{
1228 unsigned long key = drmGetKeyFromFd(fd);
1229 drmHashEntry *entry = drmGetEntry(fd);
1230
1231 drmHashDestroy(entry->tagTable);
1232 entry->fd = 0;
1233 entry->f = NULL;
1234 entry->tagTable = NULL;
1235
1236 drmHashDelete(drmHashTable, key);
1237 drmFree(entry);
1238
1239 return close(fd);
1240}
1241
Jose Fonsecad2443b22003-05-27 00:37:33 +00001242
1243/**
1244 * Map a region of memory.
1245 *
1246 * \param fd file descriptor.
1247 * \param handle handle returned by drmAddMap().
1248 * \param size size in bytes. Must match the size used by drmAddMap().
1249 * \param address will contain the user-space virtual address where the mapping
1250 * begins.
1251 *
1252 * \return zero on success, or a negative value on failure.
1253 *
1254 * \internal
1255 * This function is a wrapper for mmap().
1256 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001257int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001258{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001259 static unsigned long pagesize_mask = 0;
1260
Brianccd7b6e2007-05-29 14:54:00 -06001261 if (fd < 0)
1262 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001263
1264 if (!pagesize_mask)
1265 pagesize_mask = getpagesize() - 1;
1266
1267 size = (size + pagesize_mask) & ~pagesize_mask;
1268
Emil Velikovfaf51d52014-09-07 20:03:05 +01001269 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001270 if (*address == MAP_FAILED)
1271 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001272 return 0;
1273}
1274
Jose Fonsecad2443b22003-05-27 00:37:33 +00001275
1276/**
1277 * Unmap mappings obtained with drmMap().
1278 *
1279 * \param address address as given by drmMap().
1280 * \param size size in bytes. Must match the size used by drmMap().
1281 *
1282 * \return zero on success, or a negative value on failure.
1283 *
1284 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001285 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001286 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001287int drmUnmap(drmAddress address, drmSize size)
1288{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001289 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001290}
1291
1292drmBufInfoPtr drmGetBufInfo(int fd)
1293{
1294 drm_buf_info_t info;
1295 drmBufInfoPtr retval;
1296 int i;
1297
Daniel Vetterfd387942015-02-11 12:41:04 +01001298 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001299
Keith Packard8b9ab102008-06-13 16:03:22 -07001300 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001301 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001302
1303 if (info.count) {
1304 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1305 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001306
Keith Packard8b9ab102008-06-13 16:03:22 -07001307 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001308 drmFree(info.list);
1309 return NULL;
1310 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001311
Daryll Straussb3a57661999-12-05 01:19:48 +00001312 retval = drmMalloc(sizeof(*retval));
1313 retval->count = info.count;
1314 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1315 for (i = 0; i < info.count; i++) {
1316 retval->list[i].count = info.list[i].count;
1317 retval->list[i].size = info.list[i].size;
1318 retval->list[i].low_mark = info.list[i].low_mark;
1319 retval->list[i].high_mark = info.list[i].high_mark;
1320 }
1321 drmFree(info.list);
1322 return retval;
1323 }
1324 return NULL;
1325}
1326
Jose Fonsecad2443b22003-05-27 00:37:33 +00001327/**
1328 * Map all DMA buffers into client-virtual space.
1329 *
1330 * \param fd file descriptor.
1331 *
1332 * \return a pointer to a ::drmBufMap structure.
1333 *
1334 * \note The client may not use these buffers until obtaining buffer indices
1335 * with drmDMA().
1336 *
1337 * \internal
1338 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1339 * information about the buffers in a drm_buf_map structure into the
1340 * client-visible data structures.
1341 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001342drmBufMapPtr drmMapBufs(int fd)
1343{
1344 drm_buf_map_t bufs;
1345 drmBufMapPtr retval;
1346 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001347
Daniel Vetterfd387942015-02-11 12:41:04 +01001348 memclear(bufs);
Keith Packard8b9ab102008-06-13 16:03:22 -07001349 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Brianccd7b6e2007-05-29 14:54:00 -06001350 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001351
Brianccd7b6e2007-05-29 14:54:00 -06001352 if (!bufs.count)
1353 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001354
Daryll Straussb3a57661999-12-05 01:19:48 +00001355 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1356 return NULL;
1357
Keith Packard8b9ab102008-06-13 16:03:22 -07001358 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001359 drmFree(bufs.list);
1360 return NULL;
1361 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001362
Daryll Straussb3a57661999-12-05 01:19:48 +00001363 retval = drmMalloc(sizeof(*retval));
1364 retval->count = bufs.count;
1365 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1366 for (i = 0; i < bufs.count; i++) {
1367 retval->list[i].idx = bufs.list[i].idx;
1368 retval->list[i].total = bufs.list[i].total;
1369 retval->list[i].used = 0;
1370 retval->list[i].address = bufs.list[i].address;
1371 }
Jon Smirl8696e712004-07-07 04:36:36 +00001372
1373 drmFree(bufs.list);
1374
Daryll Straussb3a57661999-12-05 01:19:48 +00001375 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001376}
1377
Jose Fonsecad2443b22003-05-27 00:37:33 +00001378
1379/**
1380 * Unmap buffers allocated with drmMapBufs().
1381 *
1382 * \return zero on success, or negative value on failure.
1383 *
1384 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001385 * Calls munmap() for every buffer stored in \p bufs and frees the
1386 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001387 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001388int drmUnmapBufs(drmBufMapPtr bufs)
1389{
1390 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001391
Daryll Straussb3a57661999-12-05 01:19:48 +00001392 for (i = 0; i < bufs->count; i++) {
Emil Velikovfaf51d52014-09-07 20:03:05 +01001393 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001394 }
Jon Smirl8696e712004-07-07 04:36:36 +00001395
1396 drmFree(bufs->list);
1397 drmFree(bufs);
1398
Daryll Straussb3a57661999-12-05 01:19:48 +00001399 return 0;
1400}
1401
Jose Fonsecad2443b22003-05-27 00:37:33 +00001402
Gareth Hughes36047532001-02-15 08:12:14 +00001403#define DRM_DMA_RETRY 16
1404
Jose Fonsecad2443b22003-05-27 00:37:33 +00001405/**
1406 * Reserve DMA buffers.
1407 *
1408 * \param fd file descriptor.
1409 * \param request
1410 *
1411 * \return zero on success, or a negative value on failure.
1412 *
1413 * \internal
1414 * Assemble the arguments into a drm_dma structure and keeps issuing the
1415 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1416 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001417int drmDMA(int fd, drmDMAReqPtr request)
1418{
1419 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001420 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001421
Daryll Straussb3a57661999-12-05 01:19:48 +00001422 dma.context = request->context;
1423 dma.send_count = request->send_count;
1424 dma.send_indices = request->send_list;
1425 dma.send_sizes = request->send_sizes;
1426 dma.flags = request->flags;
1427 dma.request_count = request->request_count;
1428 dma.request_size = request->request_size;
1429 dma.request_indices = request->request_list;
1430 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001431 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001432
1433 do {
1434 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1435 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1436
1437 if ( ret == 0 ) {
1438 request->granted_count = dma.granted_count;
1439 return 0;
1440 } else {
1441 return -errno;
1442 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001443}
1444
Jose Fonsecad2443b22003-05-27 00:37:33 +00001445
1446/**
1447 * Obtain heavyweight hardware lock.
1448 *
1449 * \param fd file descriptor.
1450 * \param context context.
1451 * \param flags flags that determine the sate of the hardware when the function
1452 * returns.
1453 *
1454 * \return always zero.
1455 *
1456 * \internal
1457 * This function translates the arguments into a drm_lock structure and issue
1458 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1459 */
Jon Smirl8696e712004-07-07 04:36:36 +00001460int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001461{
1462 drm_lock_t lock;
1463
Daniel Vetterfd387942015-02-11 12:41:04 +01001464 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001465 lock.context = context;
1466 lock.flags = 0;
1467 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1468 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1469 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1470 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1471 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1472 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001473
Keith Packard8b9ab102008-06-13 16:03:22 -07001474 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Daryll Straussb3a57661999-12-05 01:19:48 +00001475 ;
1476 return 0;
1477}
1478
Jose Fonsecad2443b22003-05-27 00:37:33 +00001479/**
1480 * Release the hardware lock.
1481 *
1482 * \param fd file descriptor.
1483 * \param context context.
1484 *
1485 * \return zero on success, or a negative value on failure.
1486 *
1487 * \internal
1488 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1489 * argument in a drm_lock structure.
1490 */
Jon Smirl8696e712004-07-07 04:36:36 +00001491int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001492{
1493 drm_lock_t lock;
1494
Daniel Vetterfd387942015-02-11 12:41:04 +01001495 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001496 lock.context = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001497 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001498}
1499
Adam Jackson22e41ef2006-02-20 23:09:00 +00001500drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001501{
1502 drm_ctx_res_t res;
1503 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001504 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001505 int i;
1506
Daniel Vetterfd387942015-02-11 12:41:04 +01001507 memclear(res);
Keith Packard8b9ab102008-06-13 16:03:22 -07001508 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001509 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001510
Brianccd7b6e2007-05-29 14:54:00 -06001511 if (!res.count)
1512 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001513
Brianccd7b6e2007-05-29 14:54:00 -06001514 if (!(list = drmMalloc(res.count * sizeof(*list))))
1515 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001516 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1517 drmFree(list);
1518 return NULL;
1519 }
1520
1521 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001522 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001523 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001524
Brianccd7b6e2007-05-29 14:54:00 -06001525 for (i = 0; i < res.count; i++)
1526 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001527 drmFree(list);
1528
1529 *count = res.count;
1530 return retval;
1531}
1532
Adam Jackson22e41ef2006-02-20 23:09:00 +00001533void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001534{
1535 drmFree(pt);
1536}
1537
Jose Fonsecad2443b22003-05-27 00:37:33 +00001538/**
1539 * Create context.
1540 *
1541 * Used by the X server during GLXContext initialization. This causes
1542 * per-context kernel-level resources to be allocated.
1543 *
1544 * \param fd file descriptor.
1545 * \param handle is set on success. To be used by the client when requesting DMA
1546 * dispatch with drmDMA().
1547 *
1548 * \return zero on success, or a negative value on failure.
1549 *
1550 * \note May only be called by root.
1551 *
1552 * \internal
1553 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1554 * argument in a drm_ctx structure.
1555 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001556int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001557{
1558 drm_ctx_t ctx;
1559
Daniel Vetterfd387942015-02-11 12:41:04 +01001560 memclear(ctx);
Keith Packard8b9ab102008-06-13 16:03:22 -07001561 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001562 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001563 *handle = ctx.handle;
1564 return 0;
1565}
1566
Jon Smirl8696e712004-07-07 04:36:36 +00001567int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001568{
1569 drm_ctx_t ctx;
1570
Daniel Vetterfd387942015-02-11 12:41:04 +01001571 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001572 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001573 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001574 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001575 return 0;
1576}
1577
Jon Smirl8696e712004-07-07 04:36:36 +00001578int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001579{
1580 drm_ctx_t ctx;
1581
Adam Jackson22e41ef2006-02-20 23:09:00 +00001582 /*
1583 * Context preserving means that no context switches are done between DMA
1584 * buffers from one context and the next. This is suitable for use in the
1585 * X server (which promises to maintain hardware context), or in the
1586 * client-side library when buffers are swapped on behalf of two threads.
1587 */
Daniel Vetterfd387942015-02-11 12:41:04 +01001588 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001589 ctx.handle = context;
Brianccd7b6e2007-05-29 14:54:00 -06001590 if (flags & DRM_CONTEXT_PRESERVED)
1591 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1592 if (flags & DRM_CONTEXT_2DONLY)
1593 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001594 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001595 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001596 return 0;
1597}
1598
Adam Jackson22e41ef2006-02-20 23:09:00 +00001599int drmGetContextFlags(int fd, drm_context_t context,
1600 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001601{
1602 drm_ctx_t ctx;
1603
Daniel Vetterfd387942015-02-11 12:41:04 +01001604 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001605 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001606 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001607 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001608 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001609 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1610 *flags |= DRM_CONTEXT_PRESERVED;
1611 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1612 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001613 return 0;
1614}
Gareth Hughes36047532001-02-15 08:12:14 +00001615
Jose Fonsecad2443b22003-05-27 00:37:33 +00001616/**
1617 * Destroy context.
1618 *
1619 * Free any kernel-level resources allocated with drmCreateContext() associated
1620 * with the context.
1621 *
1622 * \param fd file descriptor.
1623 * \param handle handle given by drmCreateContext().
1624 *
1625 * \return zero on success, or a negative value on failure.
1626 *
1627 * \note May only be called by root.
1628 *
1629 * \internal
1630 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1631 * argument in a drm_ctx structure.
1632 */
Jon Smirl8696e712004-07-07 04:36:36 +00001633int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001634{
1635 drm_ctx_t ctx;
Daniel Vetterfd387942015-02-11 12:41:04 +01001636
1637 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001638 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001639 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001640 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001641 return 0;
1642}
1643
Adam Jackson22e41ef2006-02-20 23:09:00 +00001644int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001645{
1646 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001647
1648 memclear(draw);
Keith Packard8b9ab102008-06-13 16:03:22 -07001649 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001650 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001651 *handle = draw.handle;
1652 return 0;
1653}
1654
Jon Smirl8696e712004-07-07 04:36:36 +00001655int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001656{
1657 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001658
1659 memclear(draw);
Daryll Straussb3a57661999-12-05 01:19:48 +00001660 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001661 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001662 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001663 return 0;
1664}
1665
Michel Dänzer9810ec22006-08-22 16:40:07 +02001666int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1667 drm_drawable_info_type_t type, unsigned int num,
1668 void *data)
1669{
1670 drm_update_draw_t update;
1671
Daniel Vetterfd387942015-02-11 12:41:04 +01001672 memclear(update);
Michel Dänzer9810ec22006-08-22 16:40:07 +02001673 update.handle = handle;
1674 update.type = type;
1675 update.num = num;
1676 update.data = (unsigned long long)(unsigned long)data;
1677
Keith Packard8b9ab102008-06-13 16:03:22 -07001678 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Brianccd7b6e2007-05-29 14:54:00 -06001679 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001680
1681 return 0;
1682}
1683
Jose Fonsecad2443b22003-05-27 00:37:33 +00001684/**
1685 * Acquire the AGP device.
1686 *
1687 * Must be called before any of the other AGP related calls.
1688 *
1689 * \param fd file descriptor.
1690 *
1691 * \return zero on success, or a negative value on failure.
1692 *
1693 * \internal
1694 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1695 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001696int drmAgpAcquire(int fd)
1697{
Keith Packard8b9ab102008-06-13 16:03:22 -07001698 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001699 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001700 return 0;
1701}
1702
Jose Fonsecad2443b22003-05-27 00:37:33 +00001703
1704/**
1705 * Release the AGP device.
1706 *
1707 * \param fd file descriptor.
1708 *
1709 * \return zero on success, or a negative value on failure.
1710 *
1711 * \internal
1712 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1713 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001714int drmAgpRelease(int fd)
1715{
Keith Packard8b9ab102008-06-13 16:03:22 -07001716 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001717 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001718 return 0;
1719}
1720
Jose Fonsecad2443b22003-05-27 00:37:33 +00001721
1722/**
1723 * Set the AGP mode.
1724 *
1725 * \param fd file descriptor.
1726 * \param mode AGP mode.
1727 *
1728 * \return zero on success, or a negative value on failure.
1729 *
1730 * \internal
1731 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1732 * argument in a drm_agp_mode structure.
1733 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001734int drmAgpEnable(int fd, unsigned long mode)
1735{
1736 drm_agp_mode_t m;
1737
Connor Behan14900552015-03-24 13:53:51 -04001738 memclear(m);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001739 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001740 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Brianccd7b6e2007-05-29 14:54:00 -06001741 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001742 return 0;
1743}
1744
Jose Fonsecad2443b22003-05-27 00:37:33 +00001745
1746/**
1747 * Allocate a chunk of AGP memory.
1748 *
1749 * \param fd file descriptor.
1750 * \param size requested memory size in bytes. Will be rounded to page boundary.
1751 * \param type type of memory to allocate.
1752 * \param address if not zero, will be set to the physical address of the
1753 * allocated memory.
1754 * \param handle on success will be set to a handle of the allocated memory.
1755 *
1756 * \return zero on success, or a negative value on failure.
1757 *
1758 * \internal
1759 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1760 * arguments in a drm_agp_buffer structure.
1761 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001762int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Dave Airlie7ede2092005-11-29 09:50:47 +00001763 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001764{
1765 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001766
Daniel Vetterfd387942015-02-11 12:41:04 +01001767 memclear(b);
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001768 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001769 b.size = size;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001770 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001771 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001772 return -errno;
1773 if (address != 0UL)
1774 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001775 *handle = b.handle;
1776 return 0;
1777}
1778
Jose Fonsecad2443b22003-05-27 00:37:33 +00001779
1780/**
1781 * Free a chunk of AGP memory.
1782 *
1783 * \param fd file descriptor.
1784 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1785 *
1786 * \return zero on success, or a negative value on failure.
1787 *
1788 * \internal
1789 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1790 * argument in a drm_agp_buffer structure.
1791 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001792int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001793{
1794 drm_agp_buffer_t b;
1795
Daniel Vetterfd387942015-02-11 12:41:04 +01001796 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001797 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001798 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001799 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001800 return 0;
1801}
1802
Jose Fonsecad2443b22003-05-27 00:37:33 +00001803
1804/**
1805 * Bind a chunk of AGP memory.
1806 *
1807 * \param fd file descriptor.
1808 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1809 * \param offset offset in bytes. It will round to page boundary.
1810 *
1811 * \return zero on success, or a negative value on failure.
1812 *
1813 * \internal
1814 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1815 * argument in a drm_agp_binding structure.
1816 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001817int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001818{
1819 drm_agp_binding_t b;
1820
Daniel Vetterfd387942015-02-11 12:41:04 +01001821 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001822 b.handle = handle;
1823 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001824 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001825 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001826 return 0;
1827}
1828
Jose Fonsecad2443b22003-05-27 00:37:33 +00001829
1830/**
1831 * Unbind a chunk of AGP memory.
1832 *
1833 * \param fd file descriptor.
1834 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1835 *
1836 * \return zero on success, or a negative value on failure.
1837 *
1838 * \internal
1839 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1840 * the argument in a drm_agp_binding structure.
1841 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001842int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001843{
1844 drm_agp_binding_t b;
1845
Daniel Vetterfd387942015-02-11 12:41:04 +01001846 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001847 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001848 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001849 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001850 return 0;
1851}
1852
Jose Fonsecad2443b22003-05-27 00:37:33 +00001853
1854/**
1855 * Get AGP driver major version number.
1856 *
1857 * \param fd file descriptor.
1858 *
1859 * \return major version number on success, or a negative value on failure..
1860 *
1861 * \internal
1862 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1863 * necessary information in a drm_agp_info structure.
1864 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001865int drmAgpVersionMajor(int fd)
1866{
1867 drm_agp_info_t i;
1868
Daniel Vetterfd387942015-02-11 12:41:04 +01001869 memclear(i);
1870
Keith Packard8b9ab102008-06-13 16:03:22 -07001871 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001872 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001873 return i.agp_version_major;
1874}
1875
Jose Fonsecad2443b22003-05-27 00:37:33 +00001876
1877/**
1878 * Get AGP driver minor version number.
1879 *
1880 * \param fd file descriptor.
1881 *
1882 * \return minor version number on success, or a negative value on failure.
1883 *
1884 * \internal
1885 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1886 * necessary information in a drm_agp_info structure.
1887 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001888int drmAgpVersionMinor(int fd)
1889{
1890 drm_agp_info_t i;
1891
Daniel Vetterfd387942015-02-11 12:41:04 +01001892 memclear(i);
1893
Keith Packard8b9ab102008-06-13 16:03:22 -07001894 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001895 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001896 return i.agp_version_minor;
1897}
1898
Jose Fonsecad2443b22003-05-27 00:37:33 +00001899
1900/**
1901 * Get AGP mode.
1902 *
1903 * \param fd file descriptor.
1904 *
1905 * \return mode on success, or zero on failure.
1906 *
1907 * \internal
1908 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1909 * necessary information in a drm_agp_info structure.
1910 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001911unsigned long drmAgpGetMode(int fd)
1912{
1913 drm_agp_info_t i;
1914
Daniel Vetterfd387942015-02-11 12:41:04 +01001915 memclear(i);
1916
Keith Packard8b9ab102008-06-13 16:03:22 -07001917 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001918 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001919 return i.mode;
1920}
1921
Jose Fonsecad2443b22003-05-27 00:37:33 +00001922
1923/**
1924 * Get AGP aperture base.
1925 *
1926 * \param fd file descriptor.
1927 *
1928 * \return aperture base on success, zero on failure.
1929 *
1930 * \internal
1931 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1932 * necessary information in a drm_agp_info structure.
1933 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001934unsigned long drmAgpBase(int fd)
1935{
1936 drm_agp_info_t i;
1937
Daniel Vetterfd387942015-02-11 12:41:04 +01001938 memclear(i);
1939
Keith Packard8b9ab102008-06-13 16:03:22 -07001940 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001941 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001942 return i.aperture_base;
1943}
1944
Jose Fonsecad2443b22003-05-27 00:37:33 +00001945
1946/**
1947 * Get AGP aperture size.
1948 *
1949 * \param fd file descriptor.
1950 *
1951 * \return aperture size on success, zero on failure.
1952 *
1953 * \internal
1954 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1955 * necessary information in a drm_agp_info structure.
1956 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001957unsigned long drmAgpSize(int fd)
1958{
1959 drm_agp_info_t i;
1960
Daniel Vetterfd387942015-02-11 12:41:04 +01001961 memclear(i);
1962
Keith Packard8b9ab102008-06-13 16:03:22 -07001963 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001964 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001965 return i.aperture_size;
1966}
1967
Jose Fonsecad2443b22003-05-27 00:37:33 +00001968
1969/**
1970 * Get used AGP memory.
1971 *
1972 * \param fd file descriptor.
1973 *
1974 * \return memory used on success, or zero on failure.
1975 *
1976 * \internal
1977 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1978 * necessary information in a drm_agp_info structure.
1979 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001980unsigned long drmAgpMemoryUsed(int fd)
1981{
1982 drm_agp_info_t i;
1983
Daniel Vetterfd387942015-02-11 12:41:04 +01001984 memclear(i);
1985
Keith Packard8b9ab102008-06-13 16:03:22 -07001986 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001987 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001988 return i.memory_used;
1989}
1990
Jose Fonsecad2443b22003-05-27 00:37:33 +00001991
1992/**
1993 * Get available AGP memory.
1994 *
1995 * \param fd file descriptor.
1996 *
1997 * \return memory available on success, or zero on failure.
1998 *
1999 * \internal
2000 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2001 * necessary information in a drm_agp_info structure.
2002 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002003unsigned long drmAgpMemoryAvail(int fd)
2004{
2005 drm_agp_info_t i;
2006
Daniel Vetterfd387942015-02-11 12:41:04 +01002007 memclear(i);
2008
Keith Packard8b9ab102008-06-13 16:03:22 -07002009 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002010 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002011 return i.memory_allowed;
2012}
2013
Jose Fonsecad2443b22003-05-27 00:37:33 +00002014
2015/**
2016 * Get hardware vendor ID.
2017 *
2018 * \param fd file descriptor.
2019 *
2020 * \return vendor ID on success, or zero on failure.
2021 *
2022 * \internal
2023 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2024 * necessary information in a drm_agp_info structure.
2025 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002026unsigned int drmAgpVendorId(int fd)
2027{
2028 drm_agp_info_t i;
2029
Daniel Vetterfd387942015-02-11 12:41:04 +01002030 memclear(i);
2031
Keith Packard8b9ab102008-06-13 16:03:22 -07002032 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002033 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002034 return i.id_vendor;
2035}
2036
Jose Fonsecad2443b22003-05-27 00:37:33 +00002037
2038/**
2039 * Get hardware device ID.
2040 *
2041 * \param fd file descriptor.
2042 *
2043 * \return zero on success, or zero on failure.
2044 *
2045 * \internal
2046 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2047 * necessary information in a drm_agp_info structure.
2048 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002049unsigned int drmAgpDeviceId(int fd)
2050{
2051 drm_agp_info_t i;
2052
Daniel Vetterfd387942015-02-11 12:41:04 +01002053 memclear(i);
2054
Keith Packard8b9ab102008-06-13 16:03:22 -07002055 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002056 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002057 return i.id_device;
2058}
2059
Dave Airlie7ede2092005-11-29 09:50:47 +00002060int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002061{
2062 drm_scatter_gather_t sg;
2063
Daniel Vetterfd387942015-02-11 12:41:04 +01002064 memclear(sg);
2065
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002066 *handle = 0;
2067 sg.size = size;
Keith Packard8b9ab102008-06-13 16:03:22 -07002068 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06002069 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002070 *handle = sg.handle;
2071 return 0;
2072}
2073
Dave Airlie7ede2092005-11-29 09:50:47 +00002074int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002075{
2076 drm_scatter_gather_t sg;
2077
Daniel Vetterfd387942015-02-11 12:41:04 +01002078 memclear(sg);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002079 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07002080 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06002081 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002082 return 0;
2083}
2084
Jose Fonsecad2443b22003-05-27 00:37:33 +00002085/**
2086 * Wait for VBLANK.
2087 *
2088 * \param fd file descriptor.
2089 * \param vbl pointer to a drmVBlank structure.
2090 *
2091 * \return zero on success, or a negative value on failure.
2092 *
2093 * \internal
2094 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2095 */
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002096int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2097{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002098 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002099 int ret;
2100
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002101 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2102 if (ret < 0) {
Daniel Kurtz1eb28602013-03-28 14:05:40 +08002103 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002104 goto out;
2105 }
2106 timeout.tv_sec++;
2107
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002108 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002109 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00002110 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08002111 if (ret && errno == EINTR) {
2112 clock_gettime(CLOCK_MONOTONIC, &cur);
2113 /* Timeout after 1s */
2114 if (cur.tv_sec > timeout.tv_sec + 1 ||
2115 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2116 timeout.tv_nsec)) {
2117 errno = EBUSY;
2118 ret = -1;
2119 break;
2120 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002121 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002122 } while (ret && errno == EINTR);
2123
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002124out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002125 return ret;
2126}
2127
Daryll Straussb3a57661999-12-05 01:19:48 +00002128int drmError(int err, const char *label)
2129{
2130 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002131 case DRM_ERR_NO_DEVICE:
2132 fprintf(stderr, "%s: no device\n", label);
2133 break;
2134 case DRM_ERR_NO_ACCESS:
2135 fprintf(stderr, "%s: no access\n", label);
2136 break;
2137 case DRM_ERR_NOT_ROOT:
2138 fprintf(stderr, "%s: not root\n", label);
2139 break;
2140 case DRM_ERR_INVALID:
2141 fprintf(stderr, "%s: invalid args\n", label);
2142 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002143 default:
Brianccd7b6e2007-05-29 14:54:00 -06002144 if (err < 0)
2145 err = -err;
Daryll Straussb3a57661999-12-05 01:19:48 +00002146 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2147 break;
2148 }
2149
2150 return 1;
2151}
2152
Jose Fonsecad2443b22003-05-27 00:37:33 +00002153/**
2154 * Install IRQ handler.
2155 *
2156 * \param fd file descriptor.
2157 * \param irq IRQ number.
2158 *
2159 * \return zero on success, or a negative value on failure.
2160 *
2161 * \internal
2162 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2163 * argument in a drm_control structure.
2164 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002165int drmCtlInstHandler(int fd, int irq)
2166{
2167 drm_control_t ctl;
2168
Daniel Vetterfd387942015-02-11 12:41:04 +01002169 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002170 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002171 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002172 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002173 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002174 return 0;
2175}
2176
Jose Fonsecad2443b22003-05-27 00:37:33 +00002177
2178/**
2179 * Uninstall IRQ handler.
2180 *
2181 * \param fd file descriptor.
2182 *
2183 * \return zero on success, or a negative value on failure.
2184 *
2185 * \internal
2186 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2187 * argument in a drm_control structure.
2188 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002189int drmCtlUninstHandler(int fd)
2190{
2191 drm_control_t ctl;
2192
Daniel Vetterfd387942015-02-11 12:41:04 +01002193 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002194 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002195 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002196 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002197 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002198 return 0;
2199}
2200
2201int drmFinish(int fd, int context, drmLockFlags flags)
2202{
2203 drm_lock_t lock;
2204
Daniel Vetterfd387942015-02-11 12:41:04 +01002205 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00002206 lock.context = context;
Daryll Straussb3a57661999-12-05 01:19:48 +00002207 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2208 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2209 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2210 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2211 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2212 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002213 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Brianccd7b6e2007-05-29 14:54:00 -06002214 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002215 return 0;
2216}
2217
Jose Fonsecad2443b22003-05-27 00:37:33 +00002218/**
2219 * Get IRQ from bus ID.
2220 *
2221 * \param fd file descriptor.
2222 * \param busnum bus number.
2223 * \param devnum device number.
2224 * \param funcnum function number.
2225 *
2226 * \return IRQ number on success, or a negative value on failure.
2227 *
2228 * \internal
2229 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2230 * arguments in a drm_irq_busid structure.
2231 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002232int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2233{
2234 drm_irq_busid_t p;
2235
Daniel Vetterfd387942015-02-11 12:41:04 +01002236 memclear(p);
Daryll Straussb3a57661999-12-05 01:19:48 +00002237 p.busnum = busnum;
2238 p.devnum = devnum;
2239 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002240 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Brianccd7b6e2007-05-29 14:54:00 -06002241 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002242 return p.irq;
2243}
2244
Jon Smirl8696e712004-07-07 04:36:36 +00002245int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002246{
2247 drmHashEntry *entry = drmGetEntry(fd);
2248
2249 if (drmHashInsert(entry->tagTable, context, tag)) {
2250 drmHashDelete(entry->tagTable, context);
2251 drmHashInsert(entry->tagTable, context, tag);
2252 }
2253 return 0;
2254}
2255
Jon Smirl8696e712004-07-07 04:36:36 +00002256int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002257{
2258 drmHashEntry *entry = drmGetEntry(fd);
2259
2260 return drmHashDelete(entry->tagTable, context);
2261}
2262
Jon Smirl8696e712004-07-07 04:36:36 +00002263void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002264{
2265 drmHashEntry *entry = drmGetEntry(fd);
2266 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002267
Brianccd7b6e2007-05-29 14:54:00 -06002268 if (drmHashLookup(entry->tagTable, context, &value))
2269 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002270
2271 return value;
2272}
2273
Adam Jackson22e41ef2006-02-20 23:09:00 +00002274int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2275 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002276{
2277 drm_ctx_priv_map_t map;
2278
Daniel Vetterfd387942015-02-11 12:41:04 +01002279 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002280 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002281 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002282
Keith Packard8b9ab102008-06-13 16:03:22 -07002283 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002284 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002285 return 0;
2286}
2287
Adam Jackson22e41ef2006-02-20 23:09:00 +00002288int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2289 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002290{
2291 drm_ctx_priv_map_t map;
2292
Daniel Vetterfd387942015-02-11 12:41:04 +01002293 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002294 map.ctx_id = ctx_id;
2295
Keith Packard8b9ab102008-06-13 16:03:22 -07002296 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002297 return -errno;
2298 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002299 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002300
2301 return 0;
2302}
2303
Jon Smirl8696e712004-07-07 04:36:36 +00002304int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2305 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
Rik Faith88dbee52001-02-28 09:27:44 +00002306 int *mtrr)
2307{
2308 drm_map_t map;
2309
Daniel Vetterfd387942015-02-11 12:41:04 +01002310 memclear(map);
Rik Faith88dbee52001-02-28 09:27:44 +00002311 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002312 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002313 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002314 *offset = map.offset;
2315 *size = map.size;
2316 *type = map.type;
2317 *flags = map.flags;
2318 *handle = (unsigned long)map.handle;
2319 *mtrr = map.mtrr;
2320 return 0;
2321}
2322
2323int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2324 unsigned long *magic, unsigned long *iocs)
2325{
2326 drm_client_t client;
2327
Daniel Vetterfd387942015-02-11 12:41:04 +01002328 memclear(client);
Rik Faith88dbee52001-02-28 09:27:44 +00002329 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002330 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Brianccd7b6e2007-05-29 14:54:00 -06002331 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002332 *auth = client.auth;
2333 *pid = client.pid;
2334 *uid = client.uid;
2335 *magic = client.magic;
2336 *iocs = client.iocs;
2337 return 0;
2338}
2339
2340int drmGetStats(int fd, drmStatsT *stats)
2341{
2342 drm_stats_t s;
Jan Veselyde8532d2014-11-30 12:53:18 -05002343 unsigned i;
Rik Faith88dbee52001-02-28 09:27:44 +00002344
Daniel Vetterfd387942015-02-11 12:41:04 +01002345 memclear(s);
Keith Packard8b9ab102008-06-13 16:03:22 -07002346 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Brianccd7b6e2007-05-29 14:54:00 -06002347 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002348
2349 stats->count = 0;
2350 memset(stats, 0, sizeof(*stats));
2351 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2352 return -1;
2353
2354#define SET_VALUE \
2355 stats->data[i].long_format = "%-20.20s"; \
2356 stats->data[i].rate_format = "%8.8s"; \
2357 stats->data[i].isvalue = 1; \
2358 stats->data[i].verbose = 0
2359
2360#define SET_COUNT \
2361 stats->data[i].long_format = "%-20.20s"; \
2362 stats->data[i].rate_format = "%5.5s"; \
2363 stats->data[i].isvalue = 0; \
2364 stats->data[i].mult_names = "kgm"; \
2365 stats->data[i].mult = 1000; \
2366 stats->data[i].verbose = 0
2367
2368#define SET_BYTE \
2369 stats->data[i].long_format = "%-20.20s"; \
2370 stats->data[i].rate_format = "%5.5s"; \
2371 stats->data[i].isvalue = 0; \
2372 stats->data[i].mult_names = "KGM"; \
2373 stats->data[i].mult = 1024; \
2374 stats->data[i].verbose = 0
2375
2376
2377 stats->count = s.count;
2378 for (i = 0; i < s.count; i++) {
2379 stats->data[i].value = s.data[i].value;
2380 switch (s.data[i].type) {
2381 case _DRM_STAT_LOCK:
2382 stats->data[i].long_name = "Lock";
2383 stats->data[i].rate_name = "Lock";
2384 SET_VALUE;
2385 break;
2386 case _DRM_STAT_OPENS:
2387 stats->data[i].long_name = "Opens";
2388 stats->data[i].rate_name = "O";
2389 SET_COUNT;
2390 stats->data[i].verbose = 1;
2391 break;
2392 case _DRM_STAT_CLOSES:
2393 stats->data[i].long_name = "Closes";
2394 stats->data[i].rate_name = "Lock";
2395 SET_COUNT;
2396 stats->data[i].verbose = 1;
2397 break;
2398 case _DRM_STAT_IOCTLS:
2399 stats->data[i].long_name = "Ioctls";
2400 stats->data[i].rate_name = "Ioc/s";
2401 SET_COUNT;
2402 break;
2403 case _DRM_STAT_LOCKS:
2404 stats->data[i].long_name = "Locks";
2405 stats->data[i].rate_name = "Lck/s";
2406 SET_COUNT;
2407 break;
2408 case _DRM_STAT_UNLOCKS:
2409 stats->data[i].long_name = "Unlocks";
2410 stats->data[i].rate_name = "Unl/s";
2411 SET_COUNT;
2412 break;
2413 case _DRM_STAT_IRQ:
2414 stats->data[i].long_name = "IRQs";
2415 stats->data[i].rate_name = "IRQ/s";
2416 SET_COUNT;
2417 break;
2418 case _DRM_STAT_PRIMARY:
2419 stats->data[i].long_name = "Primary Bytes";
2420 stats->data[i].rate_name = "PB/s";
2421 SET_BYTE;
2422 break;
2423 case _DRM_STAT_SECONDARY:
2424 stats->data[i].long_name = "Secondary Bytes";
2425 stats->data[i].rate_name = "SB/s";
2426 SET_BYTE;
2427 break;
2428 case _DRM_STAT_DMA:
2429 stats->data[i].long_name = "DMA";
2430 stats->data[i].rate_name = "DMA/s";
2431 SET_COUNT;
2432 break;
2433 case _DRM_STAT_SPECIAL:
2434 stats->data[i].long_name = "Special DMA";
2435 stats->data[i].rate_name = "dma/s";
2436 SET_COUNT;
2437 break;
2438 case _DRM_STAT_MISSED:
2439 stats->data[i].long_name = "Miss";
2440 stats->data[i].rate_name = "Ms/s";
2441 SET_COUNT;
2442 break;
2443 case _DRM_STAT_VALUE:
2444 stats->data[i].long_name = "Value";
2445 stats->data[i].rate_name = "Value";
2446 SET_VALUE;
2447 break;
2448 case _DRM_STAT_BYTE:
2449 stats->data[i].long_name = "Bytes";
2450 stats->data[i].rate_name = "B/s";
2451 SET_BYTE;
2452 break;
2453 case _DRM_STAT_COUNT:
2454 default:
2455 stats->data[i].long_name = "Count";
2456 stats->data[i].rate_name = "Cnt/s";
2457 SET_COUNT;
2458 break;
2459 }
2460 }
2461 return 0;
2462}
2463
Jose Fonsecad2443b22003-05-27 00:37:33 +00002464/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002465 * Issue a set-version ioctl.
2466 *
2467 * \param fd file descriptor.
2468 * \param drmCommandIndex command index
2469 * \param data source pointer of the data to be read and written.
2470 * \param size size of the data to be read and written.
2471 *
2472 * \return zero on success, or a negative value on failure.
2473 *
2474 * \internal
2475 * It issues a read-write ioctl given by
2476 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2477 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002478int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002479{
2480 int retcode = 0;
2481 drm_set_version_t sv;
2482
Daniel Vetterfd387942015-02-11 12:41:04 +01002483 memclear(sv);
Eric Anholt06cb1322003-10-23 02:23:31 +00002484 sv.drm_di_major = version->drm_di_major;
2485 sv.drm_di_minor = version->drm_di_minor;
2486 sv.drm_dd_major = version->drm_dd_major;
2487 sv.drm_dd_minor = version->drm_dd_minor;
2488
Keith Packard8b9ab102008-06-13 16:03:22 -07002489 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Eric Anholt06cb1322003-10-23 02:23:31 +00002490 retcode = -errno;
2491 }
2492
2493 version->drm_di_major = sv.drm_di_major;
2494 version->drm_di_minor = sv.drm_di_minor;
2495 version->drm_dd_major = sv.drm_dd_major;
2496 version->drm_dd_minor = sv.drm_dd_minor;
2497
2498 return retcode;
2499}
2500
2501/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002502 * Send a device-specific command.
2503 *
2504 * \param fd file descriptor.
2505 * \param drmCommandIndex command index
2506 *
2507 * \return zero on success, or a negative value on failure.
2508 *
2509 * \internal
2510 * It issues a ioctl given by
2511 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2512 */
Jens Owen3903e5a2002-04-09 21:54:56 +00002513int drmCommandNone(int fd, unsigned long drmCommandIndex)
2514{
Jens Owen3903e5a2002-04-09 21:54:56 +00002515 unsigned long request;
2516
2517 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2518
Daniel Vetterfd387942015-02-11 12:41:04 +01002519 if (drmIoctl(fd, request, NULL)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002520 return -errno;
2521 }
2522 return 0;
2523}
2524
Jose Fonsecad2443b22003-05-27 00:37:33 +00002525
2526/**
2527 * Send a device-specific read command.
2528 *
2529 * \param fd file descriptor.
2530 * \param drmCommandIndex command index
2531 * \param data destination pointer of the data to be read.
2532 * \param size size of the data to be read.
2533 *
2534 * \return zero on success, or a negative value on failure.
2535 *
2536 * \internal
2537 * It issues a read ioctl given by
2538 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2539 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002540int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2541 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002542{
2543 unsigned long request;
2544
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002545 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2546 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002547
Keith Packard8b9ab102008-06-13 16:03:22 -07002548 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002549 return -errno;
2550 }
2551 return 0;
2552}
2553
Jose Fonsecad2443b22003-05-27 00:37:33 +00002554
2555/**
2556 * Send a device-specific write command.
2557 *
2558 * \param fd file descriptor.
2559 * \param drmCommandIndex command index
2560 * \param data source pointer of the data to be written.
2561 * \param size size of the data to be written.
2562 *
2563 * \return zero on success, or a negative value on failure.
2564 *
2565 * \internal
2566 * It issues a write ioctl given by
2567 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2568 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002569int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2570 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002571{
2572 unsigned long request;
2573
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002574 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2575 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002576
Keith Packard8b9ab102008-06-13 16:03:22 -07002577 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002578 return -errno;
2579 }
2580 return 0;
2581}
2582
Jose Fonsecad2443b22003-05-27 00:37:33 +00002583
2584/**
2585 * Send a device-specific read-write command.
2586 *
2587 * \param fd file descriptor.
2588 * \param drmCommandIndex command index
2589 * \param data source pointer of the data to be read and written.
2590 * \param size size of the data to be read and written.
2591 *
2592 * \return zero on success, or a negative value on failure.
2593 *
2594 * \internal
2595 * It issues a read-write ioctl given by
2596 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2597 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002598int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2599 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002600{
2601 unsigned long request;
2602
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002603 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2604 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002605
Keith Packard8b9ab102008-06-13 16:03:22 -07002606 if (drmIoctl(fd, request, data))
Jens Owen3903e5a2002-04-09 21:54:56 +00002607 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002608 return 0;
2609}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002610
Dave Airlied51e1bb2006-11-09 08:55:58 +11002611#define DRM_MAX_FDS 16
2612static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002613 char *BusID;
2614 int fd;
2615 int refcount;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002616 int type;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002617} connection[DRM_MAX_FDS];
2618
2619static int nr_fds = 0;
2620
2621int drmOpenOnce(void *unused,
2622 const char *BusID,
2623 int *newlyopened)
2624{
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002625 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2626}
2627
2628int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2629{
Brianccd7b6e2007-05-29 14:54:00 -06002630 int i;
2631 int fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002632
Brianccd7b6e2007-05-29 14:54:00 -06002633 for (i = 0; i < nr_fds; i++)
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002634 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2635 (connection[i].type == type)) {
Brianccd7b6e2007-05-29 14:54:00 -06002636 connection[i].refcount++;
2637 *newlyopened = 0;
2638 return connection[i].fd;
2639 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002640
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002641 fd = drmOpenWithType(NULL, BusID, type);
Emil Velikovc1cd3d92015-07-14 15:05:18 +01002642 if (fd < 0 || nr_fds == DRM_MAX_FDS)
Brianccd7b6e2007-05-29 14:54:00 -06002643 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002644
Brianccd7b6e2007-05-29 14:54:00 -06002645 connection[nr_fds].BusID = strdup(BusID);
2646 connection[nr_fds].fd = fd;
2647 connection[nr_fds].refcount = 1;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002648 connection[nr_fds].type = type;
Brianccd7b6e2007-05-29 14:54:00 -06002649 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002650
Brianccd7b6e2007-05-29 14:54:00 -06002651 if (0)
2652 fprintf(stderr, "saved connection %d for %s %d\n",
2653 nr_fds, connection[nr_fds].BusID,
2654 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002655
Brianccd7b6e2007-05-29 14:54:00 -06002656 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002657
Brianccd7b6e2007-05-29 14:54:00 -06002658 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002659}
2660
2661void drmCloseOnce(int fd)
2662{
Brianccd7b6e2007-05-29 14:54:00 -06002663 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002664
Brianccd7b6e2007-05-29 14:54:00 -06002665 for (i = 0; i < nr_fds; i++) {
2666 if (fd == connection[i].fd) {
2667 if (--connection[i].refcount == 0) {
2668 drmClose(connection[i].fd);
2669 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002670
Brianccd7b6e2007-05-29 14:54:00 -06002671 if (i < --nr_fds)
2672 connection[i] = connection[nr_fds];
Dave Airlied51e1bb2006-11-09 08:55:58 +11002673
Brianccd7b6e2007-05-29 14:54:00 -06002674 return;
2675 }
2676 }
2677 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002678}
Jesse Barnes731cd552008-12-17 10:09:49 -08002679
2680int drmSetMaster(int fd)
2681{
Daniel Vetterfd387942015-02-11 12:41:04 +01002682 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002683}
2684
2685int drmDropMaster(int fd)
2686{
Daniel Vetterfd387942015-02-11 12:41:04 +01002687 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002688}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002689
2690char *drmGetDeviceNameFromFd(int fd)
2691{
2692 char name[128];
2693 struct stat sbuf;
2694 dev_t d;
2695 int i;
2696
2697 /* The whole drmOpen thing is a fiasco and we need to find a way
2698 * back to just using open(2). For now, however, lets just make
2699 * things worse with even more ad hoc directory walking code to
2700 * discover the device file name. */
2701
2702 fstat(fd, &sbuf);
2703 d = sbuf.st_rdev;
2704
2705 for (i = 0; i < DRM_MAX_MINOR; i++) {
2706 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2707 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2708 break;
2709 }
2710 if (i == DRM_MAX_MINOR)
2711 return NULL;
2712
Adam Jackson0a1ff352010-10-27 18:44:53 -04002713 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002714}
Dave Airliecc0a1452012-07-14 09:52:17 +00002715
Frank Binns1f735782015-02-13 10:51:15 +00002716int drmGetNodeTypeFromFd(int fd)
2717{
2718 struct stat sbuf;
2719 int maj, min, type;
2720
2721 if (fstat(fd, &sbuf))
2722 return -1;
2723
2724 maj = major(sbuf.st_rdev);
2725 min = minor(sbuf.st_rdev);
2726
2727 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2728 errno = EINVAL;
2729 return -1;
2730 }
2731
2732 type = drmGetMinorType(min);
2733 if (type == -1)
2734 errno = ENODEV;
2735 return type;
2736}
2737
Dave Airliecc0a1452012-07-14 09:52:17 +00002738int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2739{
2740 struct drm_prime_handle args;
2741 int ret;
2742
Guillaume Desmottes4bca42f2015-04-29 10:16:22 +02002743 memclear(args);
2744 args.fd = -1;
Dave Airliecc0a1452012-07-14 09:52:17 +00002745 args.handle = handle;
2746 args.flags = flags;
2747 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2748 if (ret)
2749 return ret;
2750
2751 *prime_fd = args.fd;
2752 return 0;
2753}
2754
2755int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2756{
2757 struct drm_prime_handle args;
2758 int ret;
2759
Guillaume Desmottes4bca42f2015-04-29 10:16:22 +02002760 memclear(args);
Dave Airliecc0a1452012-07-14 09:52:17 +00002761 args.fd = prime_fd;
Dave Airliecc0a1452012-07-14 09:52:17 +00002762 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2763 if (ret)
2764 return ret;
2765
2766 *handle = args.handle;
2767 return 0;
2768}
2769
Emil Velikov0ca03a42015-03-07 00:58:39 +00002770static char *drmGetMinorNameForFD(int fd, int type)
2771{
2772#ifdef __linux__
2773 DIR *sysdir;
2774 struct dirent *pent, *ent;
2775 struct stat sbuf;
2776 const char *name = drmGetMinorName(type);
2777 int len;
2778 char dev_name[64], buf[64];
2779 long name_max;
2780 int maj, min;
2781
2782 if (!name)
2783 return NULL;
2784
2785 len = strlen(name);
2786
2787 if (fstat(fd, &sbuf))
2788 return NULL;
2789
2790 maj = major(sbuf.st_rdev);
2791 min = minor(sbuf.st_rdev);
2792
2793 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2794 return NULL;
2795
2796 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2797
2798 sysdir = opendir(buf);
2799 if (!sysdir)
2800 return NULL;
2801
2802 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2803 if (name_max == -1)
2804 goto out_close_dir;
2805
2806 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2807 if (pent == NULL)
2808 goto out_close_dir;
2809
2810 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2811 if (strncmp(ent->d_name, name, len) == 0) {
Mathias Tillman5c42b5e2015-08-24 11:56:13 +08002812 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2813 ent->d_name);
2814
Emil Velikov0ca03a42015-03-07 00:58:39 +00002815 free(pent);
2816 closedir(sysdir);
2817
Emil Velikov0ca03a42015-03-07 00:58:39 +00002818 return strdup(dev_name);
2819 }
2820 }
2821
2822 free(pent);
2823
2824out_close_dir:
2825 closedir(sysdir);
Emil Velikov8415a002015-09-07 18:29:05 +01002826#else
2827#warning "Missing implementation of drmGetMinorNameForFD"
Emil Velikov0ca03a42015-03-07 00:58:39 +00002828#endif
2829 return NULL;
2830}
2831
2832char *drmGetPrimaryDeviceNameFromFd(int fd)
2833{
2834 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2835}
2836
2837char *drmGetRenderDeviceNameFromFd(int fd)
2838{
2839 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2840}
Emil Velikovb556ea12015-08-17 11:09:06 +08002841
Emil Velikova250fce2015-09-07 12:54:27 +01002842static int drmParseSubsystemType(int maj, int min)
Emil Velikovb556ea12015-08-17 11:09:06 +08002843{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002844#ifdef __linux__
Emil Velikova250fce2015-09-07 12:54:27 +01002845 char path[PATH_MAX + 1];
Emil Velikovb556ea12015-08-17 11:09:06 +08002846 char link[PATH_MAX + 1] = "";
2847 char *name;
2848
Emil Velikova250fce2015-09-07 12:54:27 +01002849 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
2850 maj, min);
2851
2852 if (readlink(path, link, PATH_MAX) < 0)
2853 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08002854
2855 name = strrchr(link, '/');
2856 if (!name)
2857 return -EINVAL;
2858
Emil Velikova250fce2015-09-07 12:54:27 +01002859 if (strncmp(name, "/pci", 4) == 0)
Emil Velikovb556ea12015-08-17 11:09:06 +08002860 return DRM_BUS_PCI;
2861
2862 return -EINVAL;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002863#else
2864#warning "Missing implementation of drmParseSubsystemType"
2865 return -EINVAL;
2866#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002867}
2868
Emil Velikov536e0de2015-09-07 12:37:57 +01002869static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
Emil Velikovb556ea12015-08-17 11:09:06 +08002870{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002871#ifdef __linux__
Emil Velikov536e0de2015-09-07 12:37:57 +01002872 char path[PATH_MAX + 1];
2873 char data[128];
2874 char *str;
Emil Velikovb556ea12015-08-17 11:09:06 +08002875 int domain, bus, dev, func;
Emil Velikov536e0de2015-09-07 12:37:57 +01002876 int fd, ret;
Emil Velikovb556ea12015-08-17 11:09:06 +08002877
Emil Velikov536e0de2015-09-07 12:37:57 +01002878 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
2879 fd = open(path, O_RDONLY);
2880 if (fd < 0)
2881 return -errno;
2882
2883 ret = read(fd, data, sizeof(data));
2884 close(fd);
2885 if (ret < 0)
2886 return -errno;
2887
2888#define TAG "PCI_SLOT_NAME="
2889 str = strstr(data, TAG);
Emil Velikovb556ea12015-08-17 11:09:06 +08002890 if (str == NULL)
2891 return -EINVAL;
2892
Emil Velikov536e0de2015-09-07 12:37:57 +01002893 if (sscanf(str, TAG "%04x:%02x:%02x.%1u",
Emil Velikovb556ea12015-08-17 11:09:06 +08002894 &domain, &bus, &dev, &func) != 4)
2895 return -EINVAL;
Emil Velikov536e0de2015-09-07 12:37:57 +01002896#undef TAG
Emil Velikovb556ea12015-08-17 11:09:06 +08002897
2898 info->domain = domain;
2899 info->bus = bus;
2900 info->dev = dev;
2901 info->func = func;
2902
2903 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002904#else
2905#warning "Missing implementation of drmParsePciBusInfo"
2906 return -EINVAL;
2907#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002908}
2909
Emil Velikovbc2aca92015-09-07 13:51:54 +01002910static int drmCompareBusInfo(drmDevicePtr a, drmDevicePtr b)
Emil Velikovb556ea12015-08-17 11:09:06 +08002911{
Emil Velikovbc2aca92015-09-07 13:51:54 +01002912 if (a == NULL || b == NULL)
2913 return -1;
2914
Emil Velikovb556ea12015-08-17 11:09:06 +08002915 if (a->bustype != b->bustype)
Emil Velikovbc2aca92015-09-07 13:51:54 +01002916 return -1;
Emil Velikovb556ea12015-08-17 11:09:06 +08002917
2918 switch (a->bustype) {
2919 case DRM_BUS_PCI:
Emil Velikovbc2aca92015-09-07 13:51:54 +01002920 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo));
Emil Velikovb556ea12015-08-17 11:09:06 +08002921 default:
2922 break;
2923 }
2924
Emil Velikovbc2aca92015-09-07 13:51:54 +01002925 return -1;
Emil Velikovb556ea12015-08-17 11:09:06 +08002926}
2927
2928static int drmGetNodeType(const char *name)
2929{
2930 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
2931 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
2932 return DRM_NODE_PRIMARY;
2933
2934 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
2935 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
2936 return DRM_NODE_CONTROL;
2937
2938 if (strncmp(name, DRM_RENDER_MINOR_NAME,
2939 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
2940 return DRM_NODE_RENDER;
2941
2942 return -EINVAL;
2943}
2944
Emil Velikov5f68d312015-09-07 13:54:32 +01002945static int drmGetMaxNodeName(void)
2946{
2947 return sizeof(DRM_DIR_NAME) +
2948 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
2949 sizeof(DRM_CONTROL_MINOR_NAME),
2950 sizeof(DRM_RENDER_MINOR_NAME)) +
2951 3 /* lenght of the node number */;
2952}
2953
Emil Velikovef5192e2015-09-07 12:47:47 +01002954static int drmParsePciDeviceInfo(const char *d_name,
Emil Velikovb556ea12015-08-17 11:09:06 +08002955 drmPciDeviceInfoPtr device)
2956{
Emil Velikov291b2bb2015-09-07 18:26:34 +01002957#ifdef __linux__
Emil Velikovef5192e2015-09-07 12:47:47 +01002958 char path[PATH_MAX + 1];
2959 unsigned char config[64];
2960 int fd, ret;
2961
2962 snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config", d_name);
2963 fd = open(path, O_RDONLY);
2964 if (fd < 0)
2965 return -errno;
2966
2967 ret = read(fd, config, sizeof(config));
2968 close(fd);
2969 if (ret < 0)
2970 return -errno;
Emil Velikovb556ea12015-08-17 11:09:06 +08002971
2972 device->vendor_id = config[0] | (config[1] << 8);
2973 device->device_id = config[2] | (config[3] << 8);
2974 device->revision_id = config[8];
2975 device->subvendor_id = config[44] | (config[45] << 8);
2976 device->subdevice_id = config[46] | (config[47] << 8);
2977
2978 return 0;
Emil Velikov291b2bb2015-09-07 18:26:34 +01002979#else
2980#warning "Missing implementation of drmParsePciDeviceInfo"
2981 return -EINVAL;
2982#endif
Emil Velikovb556ea12015-08-17 11:09:06 +08002983}
2984
Emil Velikovccedf662015-09-09 16:02:18 +01002985void drmFreeDevice(drmDevicePtr *device)
Emil Velikovb556ea12015-08-17 11:09:06 +08002986{
Emil Velikovb556ea12015-08-17 11:09:06 +08002987 if (device == NULL)
2988 return;
2989
Emil Velikov5f68d312015-09-07 13:54:32 +01002990 free(*device);
2991 *device = NULL;
Emil Velikovb556ea12015-08-17 11:09:06 +08002992}
2993
2994void drmFreeDevices(drmDevicePtr devices[], int count)
2995{
2996 int i;
2997
2998 if (devices == NULL)
2999 return;
3000
Emil Velikov5f68d312015-09-07 13:54:32 +01003001 for (i = 0; i < count && devices[i] != NULL; i++)
3002 drmFreeDevice(&devices[i]);
Emil Velikovb556ea12015-08-17 11:09:06 +08003003}
3004
Emil Velikovfae59d72015-09-09 17:54:34 +01003005static int drmProcessPciDevice(drmDevicePtr *device, const char *d_name,
3006 const char *node, int node_type,
3007 int maj, int min, bool fetch_deviceinfo)
3008{
3009 const int max_node_str = drmGetMaxNodeName();
3010 int ret, i;
3011 void *addr;
3012
3013 addr = *device = calloc(1, sizeof(drmDevice) +
3014 (DRM_NODE_MAX *
3015 (sizeof(void *) + max_node_str)) +
3016 sizeof(drmPciBusInfo) +
3017 sizeof(drmPciDeviceInfo));
3018 if (!*device)
3019 return -ENOMEM;
3020
3021 (*device)->bustype = DRM_BUS_PCI;
3022 (*device)->available_nodes = 1 << node_type;
3023
3024 addr += sizeof(drmDevice);
3025 (*device)->nodes = addr;
3026
3027 addr += DRM_NODE_MAX * sizeof(void *);
3028 for (i = 0; i < DRM_NODE_MAX; i++) {
3029 (*device)->nodes[i] = addr;
3030 addr += max_node_str;
3031 }
3032 memcpy((*device)->nodes[node_type], node, max_node_str);
3033
3034 (*device)->businfo.pci = addr;
3035
3036 ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
3037 if (ret)
3038 goto free_device;
3039
3040 // Fetch the device info if the user has requested it
3041 if (fetch_deviceinfo) {
3042 addr += sizeof(drmPciBusInfo);
3043 (*device)->deviceinfo.pci = addr;
3044
3045 ret = drmParsePciDeviceInfo(d_name, (*device)->deviceinfo.pci);
3046 if (ret)
3047 goto free_device;
3048 }
3049 return 0;
3050
3051free_device:
3052 free(*device);
3053 *device = NULL;
3054 return ret;
3055}
3056
3057static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3058{
3059 int node_type, i, j;
3060
3061 for (i = 0; i < count; i++) {
3062 for (j = i + 1; j < count; j++) {
3063 if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) {
3064 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3065 node_type = log2(local_devices[j]->available_nodes);
3066 memcpy(local_devices[i]->nodes[node_type],
3067 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3068 drmFreeDevice(&local_devices[j]);
3069 }
3070 }
3071 }
3072}
3073
Emil Velikovb556ea12015-08-17 11:09:06 +08003074/**
Emil Velikovccedf662015-09-09 16:02:18 +01003075 * Get information about the opened drm device
3076 *
3077 * \param fd file descriptor of the drm device
3078 * \param device the address of a drmDevicePtr where the information
3079 * will be allocated in stored
3080 *
3081 * \return zero on success, negative error code otherwise.
3082 */
3083int drmGetDevice(int fd, drmDevicePtr *device)
3084{
3085 drmDevicePtr *local_devices;
3086 drmDevicePtr d;
3087 DIR *sysdir;
3088 struct dirent *dent;
3089 struct stat sbuf;
3090 char node[PATH_MAX + 1];
3091 int node_type, subsystem_type;
3092 int maj, min;
3093 int ret, i, node_count;
3094 int max_count = 16;
3095
3096 if (fd == -1 || device == NULL)
3097 return -EINVAL;
3098
3099 if (fstat(fd, &sbuf))
3100 return -errno;
3101
3102 maj = major(sbuf.st_rdev);
3103 min = minor(sbuf.st_rdev);
3104
3105 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3106 return -EINVAL;
3107
3108 subsystem_type = drmParseSubsystemType(maj, min);
3109
3110 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3111 if (local_devices == NULL)
3112 return -ENOMEM;
3113
3114 sysdir = opendir(DRM_DIR_NAME);
3115 if (!sysdir) {
3116 ret = -errno;
3117 goto close_sysdir;
3118 }
3119
3120 i = 0;
3121 while ((dent = readdir(sysdir))) {
3122 node_type = drmGetNodeType(dent->d_name);
3123 if (node_type < 0)
3124 continue;
3125
3126 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3127 if (stat(node, &sbuf))
3128 continue;
3129
3130 maj = major(sbuf.st_rdev);
3131 min = minor(sbuf.st_rdev);
3132
3133 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3134 continue;
3135
3136 if (drmParseSubsystemType(maj, min) != subsystem_type)
3137 continue;
3138
3139 switch (subsystem_type) {
3140 case DRM_BUS_PCI:
3141 ret = drmProcessPciDevice(&d, dent->d_name, node, node_type,
3142 maj, min, true);
3143 if (ret)
3144 goto free_devices;
3145
3146 break;
3147 default:
3148 fprintf(stderr, "The subsystem type is not supported yet\n");
3149 break;
3150 }
3151
3152 if (i >= max_count) {
3153 drmDevicePtr *temp;
3154
3155 max_count += 16;
3156 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3157 if (!temp)
3158 goto free_devices;
3159 local_devices = temp;
3160 }
3161
3162 local_devices[i] = d;
3163 i++;
3164 }
3165 node_count = i;
3166
3167 /* Fold nodes into a single device if they share the same bus info */
3168 drmFoldDuplicatedDevices(local_devices, node_count);
3169
3170 *device = local_devices[0];
3171 for (i = 1; i < node_count && local_devices[i]; i++)
3172 drmFreeDevice(&local_devices[i]);
3173
3174 free(local_devices);
3175 closedir(sysdir);
3176 return 0;
3177
3178free_devices:
3179 drmFreeDevices(local_devices, i);
3180 free(local_devices);
3181
3182close_sysdir:
3183 closedir(sysdir);
3184 return ret;
3185}
3186
3187/**
Emil Velikovb556ea12015-08-17 11:09:06 +08003188 * Get drm devices on the system
3189 *
3190 * \param devices the array of devices with drmDevicePtr elements
3191 * can be NULL to get the device number first
3192 * \param max_devices the maximum number of devices for the array
3193 *
3194 * \return on error - negative error code,
3195 * if devices is NULL - total number of devices available on the system,
3196 * alternatively the number of devices stored in devices[], which is
3197 * capped by the max_devices.
3198 */
3199int drmGetDevices(drmDevicePtr devices[], int max_devices)
3200{
Emil Velikov5f68d312015-09-07 13:54:32 +01003201 drmDevicePtr *local_devices;
3202 drmDevicePtr device;
3203 DIR *sysdir;
3204 struct dirent *dent;
3205 struct stat sbuf;
3206 char node[PATH_MAX + 1];
Emil Velikovb556ea12015-08-17 11:09:06 +08003207 int node_type, subsystem_type;
3208 int maj, min;
Emil Velikovfae59d72015-09-09 17:54:34 +01003209 int ret, i, node_count, device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08003210 int max_count = 16;
Emil Velikovb556ea12015-08-17 11:09:06 +08003211
Emil Velikov5f68d312015-09-07 13:54:32 +01003212 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3213 if (local_devices == NULL)
Emil Velikovb556ea12015-08-17 11:09:06 +08003214 return -ENOMEM;
3215
3216 sysdir = opendir(DRM_DIR_NAME);
3217 if (!sysdir) {
3218 ret = -errno;
Emil Velikov5f68d312015-09-07 13:54:32 +01003219 goto close_sysdir;
Emil Velikovb556ea12015-08-17 11:09:06 +08003220 }
3221
Emil Velikov5f68d312015-09-07 13:54:32 +01003222 i = 0;
Emil Velikovb556ea12015-08-17 11:09:06 +08003223 while ((dent = readdir(sysdir))) {
3224 node_type = drmGetNodeType(dent->d_name);
3225 if (node_type < 0)
3226 continue;
3227
3228 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3229 if (stat(node, &sbuf))
3230 continue;
3231
3232 maj = major(sbuf.st_rdev);
3233 min = minor(sbuf.st_rdev);
3234
3235 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3236 continue;
3237
Emil Velikova250fce2015-09-07 12:54:27 +01003238 subsystem_type = drmParseSubsystemType(maj, min);
Emil Velikovb556ea12015-08-17 11:09:06 +08003239
3240 if (subsystem_type < 0)
3241 continue;
3242
3243 switch (subsystem_type) {
3244 case DRM_BUS_PCI:
Emil Velikovfae59d72015-09-09 17:54:34 +01003245 ret = drmProcessPciDevice(&device, dent->d_name, node, node_type,
3246 maj, min, devices != NULL);
Emil Velikovb556ea12015-08-17 11:09:06 +08003247 if (ret)
Emil Velikov5f68d312015-09-07 13:54:32 +01003248 goto free_devices;
Emil Velikovb556ea12015-08-17 11:09:06 +08003249
Emil Velikovb556ea12015-08-17 11:09:06 +08003250 break;
3251 default:
3252 fprintf(stderr, "The subsystem type is not supported yet\n");
3253 break;
3254 }
Emil Velikov5f68d312015-09-07 13:54:32 +01003255
3256 if (i >= max_count) {
3257 drmDevicePtr *temp;
3258
3259 max_count += 16;
3260 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3261 if (!temp)
3262 goto free_devices;
3263 local_devices = temp;
3264 }
3265
3266 local_devices[i] = device;
Emil Velikovb556ea12015-08-17 11:09:06 +08003267 i++;
3268 }
Emil Velikovb556ea12015-08-17 11:09:06 +08003269 node_count = i;
3270
Emil Velikov5f68d312015-09-07 13:54:32 +01003271 /* Fold nodes into a single device if they share the same bus info */
Emil Velikovfae59d72015-09-09 17:54:34 +01003272 drmFoldDuplicatedDevices(local_devices, node_count);
Emil Velikovb556ea12015-08-17 11:09:06 +08003273
Emil Velikov5f68d312015-09-07 13:54:32 +01003274 device_count = 0;
3275 for (i = 0; i < node_count && local_devices[i]; i++) {
3276 if ((devices != NULL) && (device_count < max_devices))
3277 devices[device_count] = local_devices[i];
3278 else
3279 drmFreeDevice(&local_devices[i]);
3280
3281 device_count++;
Emil Velikovb556ea12015-08-17 11:09:06 +08003282 }
3283
Emil Velikov5f68d312015-09-07 13:54:32 +01003284 free(local_devices);
Emil Velikovb556ea12015-08-17 11:09:06 +08003285 closedir(sysdir);
Emil Velikov5f68d312015-09-07 13:54:32 +01003286 return device_count;
Emil Velikovb556ea12015-08-17 11:09:06 +08003287
Emil Velikov5f68d312015-09-07 13:54:32 +01003288free_devices:
3289 drmFreeDevices(local_devices, i);
3290 free(local_devices);
3291
3292close_sysdir:
Emil Velikovb556ea12015-08-17 11:09:06 +08003293 closedir(sysdir);
3294 return ret;
3295}