blob: b4c5aa05562370cd9c99e69bb5cff4a854f3cc70 [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>
39#include <unistd.h>
40#include <string.h>
Ian Romanick015efd12009-07-06 09:23:59 -070041#include <strings.h>
Dave Airlie79038752006-11-08 15:08:09 +110042#include <ctype.h>
Emil Velikov0ca03a42015-03-07 00:58:39 +000043#include <dirent.h>
44#include <stddef.h>
Dave Airlie79038752006-11-08 15:08:09 +110045#include <fcntl.h>
46#include <errno.h>
47#include <signal.h>
Jesse Barnesf4f76a62009-01-07 10:18:08 -080048#include <time.h>
Dave Airlie79038752006-11-08 15:08:09 +110049#include <sys/types.h>
50#include <sys/stat.h>
51#define stat_t struct stat
52#include <sys/ioctl.h>
Dave Airlie79038752006-11-08 15:08:09 +110053#include <sys/time.h>
54#include <stdarg.h>
Alan Coopersmith0e1135d2015-03-07 11:44:32 -080055#ifdef HAVE_SYS_MKDEV_H
56# include <sys/mkdev.h> /* defines major(), minor(), and makedev() on Solaris */
57#endif
Emil Velikovb556ea12015-08-17 11:09:06 +080058#include <math.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000059
60/* Not all systems have MAP_FAILED defined */
61#ifndef MAP_FAILED
62#define MAP_FAILED ((void *)-1)
63#endif
64
Daryll Straussb3a57661999-12-05 01:19:48 +000065#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010066#include "libdrm_macros.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000067
Jonathan Grayfc083322015-07-21 03:12:56 +100068#ifdef __OpenBSD__
69#define DRM_PRIMARY_MINOR_NAME "drm"
70#define DRM_CONTROL_MINOR_NAME "drmC"
71#define DRM_RENDER_MINOR_NAME "drmR"
72#else
73#define DRM_PRIMARY_MINOR_NAME "card"
74#define DRM_CONTROL_MINOR_NAME "controlD"
75#define DRM_RENDER_MINOR_NAME "renderD"
76#endif
77
Hasso Tepper27c37852008-04-07 15:27:43 +030078#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Eric Anholtcfa778a2003-02-21 23:23:09 +000079#define DRM_MAJOR 145
Rik Faith88dbee52001-02-28 09:27:44 +000080#endif
81
Eric Anholtcfa778a2003-02-21 23:23:09 +000082#ifdef __NetBSD__
83#define DRM_MAJOR 34
84#endif
85
Jonathan Gray66c3afb2015-07-21 03:12:10 +100086#ifdef __OpenBSD__
87#ifdef __i386__
88#define DRM_MAJOR 88
89#else
90#define DRM_MAJOR 87
91#endif
92#endif /* __OpenBSD__ */
Alan Hourihaneb0a92852003-09-24 14:39:25 +000093
Eric Anholtcfa778a2003-02-21 23:23:09 +000094#ifndef DRM_MAJOR
95#define DRM_MAJOR 226 /* Linux */
Rik Faith88dbee52001-02-28 09:27:44 +000096#endif
97
Adam Jackson22e41ef2006-02-20 23:09:00 +000098/*
99 * This definition needs to be changed on some systems if dev_t is a structure.
100 * If there is a header file we can get it from, there would be best.
101 */
Brian Paul569da5a2000-06-08 14:38:22 +0000102#ifndef makedev
Brian Paul569da5a2000-06-08 14:38:22 +0000103#define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
104#endif
105
David Dawes56bd9c22001-07-30 19:59:39 +0000106#define DRM_MSG_VERBOSITY 3
107
Daniel Vetterfd387942015-02-11 12:41:04 +0100108#define memclear(s) memset(&s, 0, sizeof(s))
109
Dave Airlie79038752006-11-08 15:08:09 +1100110static drmServerInfoPtr drm_server_info;
111
112void drmSetServerInfo(drmServerInfoPtr info)
113{
Brianccd7b6e2007-05-29 14:54:00 -0600114 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +1100115}
116
Jose Fonsecad2443b22003-05-27 00:37:33 +0000117/**
118 * Output a message to stderr.
119 *
120 * \param format printf() like format string.
121 *
122 * \internal
123 * This function is a wrapper around vfprintf().
124 */
Dave Airlie79038752006-11-08 15:08:09 +1100125
Thierry Reding44b08c02014-01-22 12:06:51 +0100126static int DRM_PRINTFLIKE(1, 0)
127drmDebugPrint(const char *format, va_list ap)
Dave Airlie79038752006-11-08 15:08:09 +1100128{
Brianccd7b6e2007-05-29 14:54:00 -0600129 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100130}
131
Eric Anholtc4857422008-06-03 10:20:49 -0700132void
David Dawes56bd9c22001-07-30 19:59:39 +0000133drmMsg(const char *format, ...)
134{
135 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000136 const char *env;
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400137 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
138 (drm_server_info && drm_server_info->debug_print))
David Dawes56bd9c22001-07-30 19:59:39 +0000139 {
140 va_start(ap, format);
Dave Airlie79038752006-11-08 15:08:09 +1100141 if (drm_server_info) {
142 drm_server_info->debug_print(format,ap);
143 } else {
Jan Veselycfbe9c92015-03-18 14:17:26 -0400144 drmDebugPrint(format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100145 }
David Dawes56bd9c22001-07-30 19:59:39 +0000146 va_end(ap);
147 }
148}
149
Daryll Straussb3a57661999-12-05 01:19:48 +0000150static void *drmHashTable = NULL; /* Context switch callbacks */
151
Dave Airlie79038752006-11-08 15:08:09 +1100152void *drmGetHashTable(void)
153{
Brianccd7b6e2007-05-29 14:54:00 -0600154 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100155}
Daryll Straussb3a57661999-12-05 01:19:48 +0000156
157void *drmMalloc(int size)
158{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100159 return calloc(1, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000160}
161
162void drmFree(void *pt)
163{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100164 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000165}
166
Keith Packard8b9ab102008-06-13 16:03:22 -0700167/**
168 * Call ioctl, restarting if it is interupted
169 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800170int
Coleman Kane41b83a92008-08-18 17:08:21 -0400171drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700172{
173 int ret;
174
175 do {
176 ret = ioctl(fd, request, arg);
177 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
178 return ret;
179}
Daryll Straussb3a57661999-12-05 01:19:48 +0000180
Daryll Straussb3a57661999-12-05 01:19:48 +0000181static unsigned long drmGetKeyFromFd(int fd)
182{
David Dawesfcc21062001-03-30 17:16:20 +0000183 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000184
185 st.st_rdev = 0;
186 fstat(fd, &st);
187 return st.st_rdev;
188}
189
Dave Airlie79038752006-11-08 15:08:09 +1100190drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000191{
192 unsigned long key = drmGetKeyFromFd(fd);
193 void *value;
194 drmHashEntry *entry;
195
Brianccd7b6e2007-05-29 14:54:00 -0600196 if (!drmHashTable)
197 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000198
199 if (drmHashLookup(drmHashTable, key, &value)) {
200 entry = drmMalloc(sizeof(*entry));
201 entry->fd = fd;
202 entry->f = NULL;
203 entry->tagTable = drmHashCreate();
204 drmHashInsert(drmHashTable, key, entry);
205 } else {
206 entry = value;
207 }
208 return entry;
209}
210
Jose Fonsecad2443b22003-05-27 00:37:33 +0000211/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000212 * Compare two busid strings
213 *
214 * \param first
215 * \param second
216 *
217 * \return 1 if matched.
218 *
219 * \internal
220 * This function compares two bus ID strings. It understands the older
221 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
222 * domain, b is bus, d is device, f is function.
223 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000224static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000225{
226 /* First, check if the IDs are exactly the same */
227 if (strcasecmp(id1, id2) == 0)
228 return 1;
229
230 /* Try to match old/new-style PCI bus IDs. */
231 if (strncasecmp(id1, "pci", 3) == 0) {
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300232 unsigned int o1, b1, d1, f1;
233 unsigned int o2, b2, d2, f2;
Eric Anholt06cb1322003-10-23 02:23:31 +0000234 int ret;
235
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300236 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000237 if (ret != 4) {
238 o1 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300239 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000240 if (ret != 3)
241 return 0;
242 }
243
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300244 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000245 if (ret != 4) {
246 o2 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300247 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000248 if (ret != 3)
249 return 0;
250 }
251
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000252 /* If domains aren't properly supported by the kernel interface,
253 * just ignore them, which sucks less than picking a totally random
254 * card with "open by name"
255 */
256 if (!pci_domain_ok)
257 o1 = o2 = 0;
258
Eric Anholt06cb1322003-10-23 02:23:31 +0000259 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
260 return 0;
261 else
262 return 1;
263 }
264 return 0;
265}
266
267/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300268 * Handles error checking for chown call.
269 *
270 * \param path to file.
271 * \param id of the new owner.
272 * \param id of the new group.
273 *
274 * \return zero if success or -1 if failure.
275 *
276 * \internal
277 * Checks for failure. If failure was caused by signal call chown again.
278 * If any other failure happened then it will output error mesage using
279 * drmMsg() call.
280 */
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500281#if !defined(UDEV)
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300282static int chown_check_return(const char *path, uid_t owner, gid_t group)
283{
284 int rv;
285
286 do {
287 rv = chown(path, owner, group);
288 } while (rv != 0 && errno == EINTR);
289
290 if (rv == 0)
291 return 0;
292
293 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
294 path, errno, strerror(errno));
295 return -1;
296}
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500297#endif
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300298
299/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000300 * Open the DRM device, creating it if necessary.
301 *
302 * \param dev major and minor numbers of the device.
303 * \param minor minor number of the device.
304 *
305 * \return a file descriptor on success, or a negative value on error.
306 *
307 * \internal
308 * Assembles the device name from \p minor and opens it, creating the device
309 * special file node with the major and minor numbers specified by \p dev and
310 * parent directory if necessary and was called by root.
311 */
Jan Veselyde8532d2014-11-30 12:53:18 -0500312static int drmOpenDevice(dev_t dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000313{
David Dawes9c775d02001-05-14 14:49:58 +0000314 stat_t st;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000315 const char *dev_name;
Rik Faith88dbee52001-02-28 09:27:44 +0000316 char buf[64];
317 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100318 mode_t devmode = DRM_DEV_MODE, serv_mode;
Jan Vesely0706c142015-02-27 12:47:46 -0500319 gid_t serv_group;
320#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000321 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000322 uid_t user = DRM_DEV_UID;
Jan Vesely0706c142015-02-27 12:47:46 -0500323 gid_t group = DRM_DEV_GID;
324#endif
325
Frank Binns0c5aaee2015-01-14 14:07:51 +0000326 switch (type) {
327 case DRM_NODE_PRIMARY:
328 dev_name = DRM_DEV_NAME;
329 break;
330 case DRM_NODE_CONTROL:
331 dev_name = DRM_CONTROL_DEV_NAME;
332 break;
333 case DRM_NODE_RENDER:
334 dev_name = DRM_RENDER_DEV_NAME;
335 break;
336 default:
337 return -EINVAL;
338 };
339
340 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000341 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000342
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400343 if (drm_server_info && drm_server_info->get_perms) {
Brianccd7b6e2007-05-29 14:54:00 -0600344 drm_server_info->get_perms(&serv_group, &serv_mode);
345 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
346 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Dave Airlie79038752006-11-08 15:08:09 +1100347 }
Brian Paul569da5a2000-06-08 14:38:22 +0000348
Dave Airlie9101a022008-08-24 16:54:43 +1000349#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000350 if (stat(DRM_DIR_NAME, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600351 if (!isroot)
352 return DRM_ERR_NOT_ROOT;
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000353 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300354 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000355 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000356 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000357
Eric Anholt06cb1322003-10-23 02:23:31 +0000358 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000359 if (stat(buf, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600360 if (!isroot)
361 return DRM_ERR_NOT_ROOT;
Rik Faith88dbee52001-02-28 09:27:44 +0000362 remove(buf);
363 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000364 }
Dave Airlie79038752006-11-08 15:08:09 +1100365
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400366 if (drm_server_info && drm_server_info->get_perms) {
Jammy Zhou454b1492015-04-27 10:29:55 +0800367 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300368 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600369 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100370 }
Dave Airlie9101a022008-08-24 16:54:43 +1000371#else
372 /* if we modprobed then wait for udev */
373 {
374 int udev_count = 0;
375wait_for_udev:
376 if (stat(DRM_DIR_NAME, &st)) {
377 usleep(20);
378 udev_count++;
379
380 if (udev_count == 50)
381 return -1;
382 goto wait_for_udev;
383 }
384
385 if (stat(buf, &st)) {
386 usleep(20);
387 udev_count++;
388
389 if (udev_count == 50)
390 return -1;
391 goto wait_for_udev;
392 }
393 }
394#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000395
David Dawes56bd9c22001-07-30 19:59:39 +0000396 fd = open(buf, O_RDWR, 0);
397 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
398 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600399 if (fd >= 0)
400 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000401
Dave Airlie39e5e982010-12-07 14:26:09 +1000402#if !defined(UDEV)
Eric Anholt06cb1322003-10-23 02:23:31 +0000403 /* Check if the device node is not what we expect it to be, and recreate it
404 * and try again if so.
405 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000406 if (st.st_rdev != dev) {
Brianccd7b6e2007-05-29 14:54:00 -0600407 if (!isroot)
408 return DRM_ERR_NOT_ROOT;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000409 remove(buf);
410 mknod(buf, S_IFCHR | devmode, dev);
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400411 if (drm_server_info && drm_server_info->get_perms) {
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300412 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600413 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100414 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000415 }
416 fd = open(buf, O_RDWR, 0);
417 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
418 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600419 if (fd >= 0)
420 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000421
David Dawes56bd9c22001-07-30 19:59:39 +0000422 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000423 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000424#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000425 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000426}
427
Jose Fonsecad2443b22003-05-27 00:37:33 +0000428
429/**
430 * Open the DRM device
431 *
432 * \param minor device minor number.
433 * \param create allow to create the device if set.
434 *
435 * \return a file descriptor on success, or a negative value on error.
436 *
437 * \internal
438 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
439 * name from \p minor and opens it.
440 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800441static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000442{
443 int fd;
444 char buf[64];
Frank Binns0c5aaee2015-01-14 14:07:51 +0000445 const char *dev_name;
Dave Airliedb85ed22008-02-13 12:20:02 +1000446
Brianccd7b6e2007-05-29 14:54:00 -0600447 if (create)
Jesse Barnes731cd552008-12-17 10:09:49 -0800448 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
Rik Faith88dbee52001-02-28 09:27:44 +0000449
Frank Binns0c5aaee2015-01-14 14:07:51 +0000450 switch (type) {
451 case DRM_NODE_PRIMARY:
452 dev_name = DRM_DEV_NAME;
453 break;
454 case DRM_NODE_CONTROL:
455 dev_name = DRM_CONTROL_DEV_NAME;
456 break;
457 case DRM_NODE_RENDER:
458 dev_name = DRM_RENDER_DEV_NAME;
459 break;
460 default:
461 return -EINVAL;
462 };
463
464 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Brianccd7b6e2007-05-29 14:54:00 -0600465 if ((fd = open(buf, O_RDWR, 0)) >= 0)
466 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000467 return -errno;
468}
469
Brian Paul569da5a2000-06-08 14:38:22 +0000470
Jose Fonsecad2443b22003-05-27 00:37:33 +0000471/**
472 * Determine whether the DRM kernel driver has been loaded.
473 *
474 * \return 1 if the DRM driver is loaded, 0 otherwise.
475 *
476 * \internal
477 * Determine the presence of the kernel driver by attempting to open the 0
478 * minor and get version information. For backward compatibility with older
479 * Linux implementations, /proc/dri is also checked.
480 */
Brian Paul569da5a2000-06-08 14:38:22 +0000481int drmAvailable(void)
482{
Brian Paul569da5a2000-06-08 14:38:22 +0000483 drmVersionPtr version;
484 int retval = 0;
485 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000486
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000487 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000488#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000489 /* Try proc for backward Linux compatibility */
Brianccd7b6e2007-05-29 14:54:00 -0600490 if (!access("/proc/dri/0", R_OK))
491 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000492#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000493 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000494 }
Rik Faith88dbee52001-02-28 09:27:44 +0000495
496 if ((version = drmGetVersion(fd))) {
497 retval = 1;
498 drmFreeVersion(version);
499 }
500 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000501
502 return retval;
503}
504
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800505static int drmGetMinorBase(int type)
506{
507 switch (type) {
508 case DRM_NODE_PRIMARY:
509 return 0;
510 case DRM_NODE_CONTROL:
511 return 64;
512 case DRM_NODE_RENDER:
513 return 128;
514 default:
515 return -1;
516 };
517}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000518
Frank Binns1f735782015-02-13 10:51:15 +0000519static int drmGetMinorType(int minor)
520{
521 int type = minor >> 6;
522
523 if (minor < 0)
524 return -1;
525
526 switch (type) {
527 case DRM_NODE_PRIMARY:
528 case DRM_NODE_CONTROL:
529 case DRM_NODE_RENDER:
530 return type;
531 default:
532 return -1;
533 }
534}
535
Emil Velikov0ca03a42015-03-07 00:58:39 +0000536static const char *drmGetMinorName(int type)
537{
538 switch (type) {
539 case DRM_NODE_PRIMARY:
Jonathan Grayfc083322015-07-21 03:12:56 +1000540 return DRM_PRIMARY_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000541 case DRM_NODE_CONTROL:
Jonathan Grayfc083322015-07-21 03:12:56 +1000542 return DRM_CONTROL_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000543 case DRM_NODE_RENDER:
Jonathan Grayfc083322015-07-21 03:12:56 +1000544 return DRM_RENDER_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000545 default:
546 return NULL;
547 }
548}
549
Jose Fonsecad2443b22003-05-27 00:37:33 +0000550/**
551 * Open the device by bus ID.
552 *
553 * \param busid bus ID.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800554 * \param type device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000555 *
556 * \return a file descriptor on success, or a negative value on error.
557 *
558 * \internal
559 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
560 * comparing the device bus ID with the one supplied.
561 *
562 * \sa drmOpenMinor() and drmGetBusid().
563 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800564static int drmOpenByBusid(const char *busid, int type)
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000565{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000566 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000567 int fd;
568 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000569 drmSetVersion sv;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800570 int base = drmGetMinorBase(type);
571
572 if (base < 0)
573 return -1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000574
575 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800576 for (i = base; i < base + DRM_MAX_MINOR; i++) {
577 fd = drmOpenMinor(i, 1, type);
David Dawes56bd9c22001-07-30 19:59:39 +0000578 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
579 if (fd >= 0) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000580 /* We need to try for 1.4 first for proper PCI domain support
581 * and if that fails, we know the kernel is busted
582 */
Eric Anholt06cb1322003-10-23 02:23:31 +0000583 sv.drm_di_major = 1;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000584 sv.drm_di_minor = 4;
Eric Anholt06cb1322003-10-23 02:23:31 +0000585 sv.drm_dd_major = -1; /* Don't care */
Eric Anholt26462b92005-12-31 11:48:12 +0000586 sv.drm_dd_minor = -1; /* Don't care */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000587 if (drmSetInterfaceVersion(fd, &sv)) {
588#ifndef __alpha__
589 pci_domain_ok = 0;
590#endif
591 sv.drm_di_major = 1;
592 sv.drm_di_minor = 1;
593 sv.drm_dd_major = -1; /* Don't care */
594 sv.drm_dd_minor = -1; /* Don't care */
Thierry Reding303ff262014-04-08 22:33:04 +0200595 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000596 drmSetInterfaceVersion(fd, &sv);
597 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000598 buf = drmGetBusid(fd);
David Dawes56bd9c22001-07-30 19:59:39 +0000599 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000600 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
Rik Faith88dbee52001-02-28 09:27:44 +0000601 drmFreeBusid(buf);
602 return fd;
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000603 }
Brianccd7b6e2007-05-29 14:54:00 -0600604 if (buf)
605 drmFreeBusid(buf);
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000606 close(fd);
607 }
608 }
609 return -1;
610}
611
Jose Fonsecad2443b22003-05-27 00:37:33 +0000612
613/**
614 * Open the device by name.
615 *
616 * \param name driver name.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800617 * \param type the device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000618 *
619 * \return a file descriptor on success, or a negative value on error.
620 *
621 * \internal
622 * This function opens the first minor number that matches the driver name and
623 * isn't already in use. If it's in use it then it will already have a bus ID
624 * assigned.
625 *
626 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
627 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800628static int drmOpenByName(const char *name, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000629{
Rik Faith88dbee52001-02-28 09:27:44 +0000630 int i;
631 int fd;
632 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000633 char * id;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800634 int base = drmGetMinorBase(type);
635
636 if (base < 0)
637 return -1;
Dave Airliedb85ed22008-02-13 12:20:02 +1000638
David Dawes56bd9c22001-07-30 19:59:39 +0000639 /*
640 * Open the first minor number that matches the driver name and isn't
641 * already in use. If it's in use it will have a busid assigned already.
642 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800643 for (i = base; i < base + DRM_MAX_MINOR; i++) {
644 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
Rik Faith88dbee52001-02-28 09:27:44 +0000645 if ((version = drmGetVersion(fd))) {
646 if (!strcmp(version->name, name)) {
647 drmFreeVersion(version);
David Dawes56bd9c22001-07-30 19:59:39 +0000648 id = drmGetBusid(fd);
649 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
650 if (!id || !*id) {
Adam Jackson22e41ef2006-02-20 23:09:00 +0000651 if (id)
David Dawes56bd9c22001-07-30 19:59:39 +0000652 drmFreeBusid(id);
David Dawes56bd9c22001-07-30 19:59:39 +0000653 return fd;
654 } else {
655 drmFreeBusid(id);
656 }
657 } else {
658 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000659 }
Rik Faith88dbee52001-02-28 09:27:44 +0000660 }
David Dawes56bd9c22001-07-30 19:59:39 +0000661 close(fd);
Rik Faith88dbee52001-02-28 09:27:44 +0000662 }
663 }
664
665#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000666 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000667 for (i = 0; i < 8; i++) {
Rik Faith88dbee52001-02-28 09:27:44 +0000668 char proc_name[64], buf[512];
669 char *driver, *pt, *devstring;
670 int retcode;
671
Daryll Strauss0371c291999-12-18 18:34:59 +0000672 sprintf(proc_name, "/proc/dri/%d/name", i);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000673 if ((fd = open(proc_name, 0, 0)) >= 0) {
674 retcode = read(fd, buf, sizeof(buf)-1);
675 close(fd);
676 if (retcode) {
677 buf[retcode-1] = '\0';
678 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
679 ;
Adam Jackson22e41ef2006-02-20 23:09:00 +0000680 if (*pt) { /* Device is next */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000681 *pt = '\0';
682 if (!strcmp(driver, name)) { /* Match */
683 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
684 ;
685 if (*pt) { /* Found busid */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800686 return drmOpenByBusid(++pt, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000687 } else { /* No busid */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800688 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000689 }
690 }
691 }
692 }
Brian Paul569da5a2000-06-08 14:38:22 +0000693 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000694 }
Rik Faith88dbee52001-02-28 09:27:44 +0000695#endif
696
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000697 return -1;
698}
699
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000700
Jose Fonsecad2443b22003-05-27 00:37:33 +0000701/**
702 * Open the DRM device.
703 *
704 * Looks up the specified name and bus ID, and opens the device found. The
705 * entry in /dev/dri is created if necessary and if called by root.
706 *
707 * \param name driver name. Not referenced if bus ID is supplied.
708 * \param busid bus ID. Zero if not known.
709 *
710 * \return a file descriptor on success, or a negative value on error.
711 *
712 * \internal
713 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
714 * otherwise.
715 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000716int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000717{
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800718 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
719}
720
721/**
722 * Open the DRM device with specified type.
723 *
724 * Looks up the specified name and bus ID, and opens the device found. The
725 * entry in /dev/dri is created if necessary and if called by root.
726 *
727 * \param name driver name. Not referenced if bus ID is supplied.
728 * \param busid bus ID. Zero if not known.
729 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
730 *
731 * \return a file descriptor on success, or a negative value on error.
732 *
733 * \internal
734 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
735 * otherwise.
736 */
737int drmOpenWithType(const char *name, const char *busid, int type)
738{
Rob Clarkeb7c2d52015-09-04 08:08:02 -0400739 if (!drmAvailable() && name != NULL && drm_server_info &&
740 drm_server_info->load_module) {
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800741 /* try to load the kernel module */
Dave Airlie79038752006-11-08 15:08:09 +1100742 if (!drm_server_info->load_module(name)) {
Brianccd7b6e2007-05-29 14:54:00 -0600743 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
Eric Anholt06cb1322003-10-23 02:23:31 +0000744 return -1;
745 }
746 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000747
Eric Anholt06cb1322003-10-23 02:23:31 +0000748 if (busid) {
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800749 int fd = drmOpenByBusid(busid, type);
Eric Anholt06cb1322003-10-23 02:23:31 +0000750 if (fd >= 0)
751 return fd;
752 }
Adam Jackson22e41ef2006-02-20 23:09:00 +0000753
Eric Anholt06cb1322003-10-23 02:23:31 +0000754 if (name)
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800755 return drmOpenByName(name, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000756
Eric Anholt06cb1322003-10-23 02:23:31 +0000757 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000758}
759
Jesse Barnes731cd552008-12-17 10:09:49 -0800760int drmOpenControl(int minor)
761{
762 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
763}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000764
Frank Binns0c5aaee2015-01-14 14:07:51 +0000765int drmOpenRender(int minor)
766{
767 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
768}
769
Jose Fonsecad2443b22003-05-27 00:37:33 +0000770/**
771 * Free the version information returned by drmGetVersion().
772 *
773 * \param v pointer to the version information.
774 *
775 * \internal
776 * It frees the memory pointed by \p %v as well as all the non-null strings
777 * pointers in it.
778 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000779void drmFreeVersion(drmVersionPtr v)
780{
Brianccd7b6e2007-05-29 14:54:00 -0600781 if (!v)
782 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100783 drmFree(v->name);
784 drmFree(v->date);
785 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000786 drmFree(v);
787}
788
Jose Fonsecad2443b22003-05-27 00:37:33 +0000789
790/**
791 * Free the non-public version information returned by the kernel.
792 *
793 * \param v pointer to the version information.
794 *
795 * \internal
796 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
797 * the non-null strings pointers in it.
798 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000799static void drmFreeKernelVersion(drm_version_t *v)
800{
Brianccd7b6e2007-05-29 14:54:00 -0600801 if (!v)
802 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100803 drmFree(v->name);
804 drmFree(v->date);
805 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000806 drmFree(v);
807}
808
Jose Fonsecad2443b22003-05-27 00:37:33 +0000809
810/**
811 * Copy version information.
812 *
813 * \param d destination pointer.
814 * \param s source pointer.
815 *
816 * \internal
817 * Used by drmGetVersion() to translate the information returned by the ioctl
818 * interface in a private structure into the public structure counterpart.
819 */
Brian Paul569da5a2000-06-08 14:38:22 +0000820static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000821{
822 d->version_major = s->version_major;
823 d->version_minor = s->version_minor;
824 d->version_patchlevel = s->version_patchlevel;
825 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400826 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000827 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400828 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000829 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400830 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000831}
832
Daryll Straussb3a57661999-12-05 01:19:48 +0000833
Jose Fonsecad2443b22003-05-27 00:37:33 +0000834/**
835 * Query the driver version information.
836 *
837 * \param fd file descriptor.
838 *
839 * \return pointer to a drmVersion structure which should be freed with
840 * drmFreeVersion().
841 *
842 * \note Similar information is available via /proc/dri.
843 *
844 * \internal
845 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
846 * first with zeros to get the string lengths, and then the actually strings.
847 * It also null-terminates them since they might not be already.
848 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000849drmVersionPtr drmGetVersion(int fd)
850{
851 drmVersionPtr retval;
852 drm_version_t *version = drmMalloc(sizeof(*version));
853
Daniel Vetter95f23cf2015-02-11 17:25:30 +0100854 memclear(*version);
Gareth Hughes36047532001-02-15 08:12:14 +0000855
Keith Packard8b9ab102008-06-13 16:03:22 -0700856 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000857 drmFreeKernelVersion(version);
858 return NULL;
859 }
860
Daryll Straussb3a57661999-12-05 01:19:48 +0000861 if (version->name_len)
862 version->name = drmMalloc(version->name_len + 1);
863 if (version->date_len)
864 version->date = drmMalloc(version->date_len + 1);
865 if (version->desc_len)
866 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000867
Keith Packard8b9ab102008-06-13 16:03:22 -0700868 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000869 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
Daryll Straussb3a57661999-12-05 01:19:48 +0000870 drmFreeKernelVersion(version);
871 return NULL;
872 }
873
Adam Jackson22e41ef2006-02-20 23:09:00 +0000874 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000875 if (version->name_len) version->name[version->name_len] = '\0';
876 if (version->date_len) version->date[version->date_len] = '\0';
877 if (version->desc_len) version->desc[version->desc_len] = '\0';
878
Daryll Straussb3a57661999-12-05 01:19:48 +0000879 retval = drmMalloc(sizeof(*retval));
880 drmCopyVersion(retval, version);
881 drmFreeKernelVersion(version);
882 return retval;
883}
884
Jens Owen3903e5a2002-04-09 21:54:56 +0000885
Jose Fonsecad2443b22003-05-27 00:37:33 +0000886/**
887 * Get version information for the DRM user space library.
888 *
889 * This version number is driver independent.
890 *
891 * \param fd file descriptor.
892 *
893 * \return version information.
894 *
895 * \internal
896 * This function allocates and fills a drm_version structure with a hard coded
897 * version number.
898 */
Jens Owen3903e5a2002-04-09 21:54:56 +0000899drmVersionPtr drmGetLibVersion(int fd)
900{
901 drm_version_t *version = drmMalloc(sizeof(*version));
902
903 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100904 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000905 * revision 1.0.x = original DRM interface with no drmGetLibVersion
906 * entry point and many drm<Device> extensions
907 * revision 1.1.x = added drmCommand entry points for device extensions
908 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000909 * revision 1.2.x = added drmSetInterfaceVersion
910 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100911 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000912 */
Dave Airlie79038752006-11-08 15:08:09 +1100913 version->version_major = 1;
914 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000915 version->version_patchlevel = 0;
916
917 return (drmVersionPtr)version;
918}
919
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000920int drmGetCap(int fd, uint64_t capability, uint64_t *value)
921{
Daniel Vetterfd387942015-02-11 12:41:04 +0100922 struct drm_get_cap cap;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000923 int ret;
924
Daniel Vetterfd387942015-02-11 12:41:04 +0100925 memclear(cap);
926 cap.capability = capability;
927
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000928 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
929 if (ret)
930 return ret;
931
932 *value = cap.value;
Dave Airlie3b04c732011-03-04 15:48:31 +1000933 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000934}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000935
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100936int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
937{
Daniel Vetterfd387942015-02-11 12:41:04 +0100938 struct drm_set_client_cap cap;
939
940 memclear(cap);
941 cap.capability = capability;
942 cap.value = value;
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100943
944 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
945}
946
Jose Fonsecad2443b22003-05-27 00:37:33 +0000947/**
948 * Free the bus ID information.
949 *
950 * \param busid bus ID information string as given by drmGetBusid().
951 *
952 * \internal
953 * This function is just frees the memory pointed by \p busid.
954 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000955void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000956{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000957 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000958}
959
Jose Fonsecad2443b22003-05-27 00:37:33 +0000960
961/**
962 * Get the bus ID of the device.
963 *
964 * \param fd file descriptor.
965 *
966 * \return bus ID string.
967 *
968 * \internal
969 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
970 * get the string length and data, passing the arguments in a drm_unique
971 * structure.
972 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000973char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000974{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000975 drm_unique_t u;
976
Daniel Vetterfd387942015-02-11 12:41:04 +0100977 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000978
Keith Packard8b9ab102008-06-13 16:03:22 -0700979 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600980 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000981 u.unique = drmMalloc(u.unique_len + 1);
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[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +0000985
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000986 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +0000987}
988
Jose Fonsecad2443b22003-05-27 00:37:33 +0000989
990/**
991 * Set the bus ID of the device.
992 *
993 * \param fd file descriptor.
994 * \param busid bus ID string.
995 *
996 * \return zero on success, negative on failure.
997 *
998 * \internal
999 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1000 * the arguments in a drm_unique structure.
1001 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001002int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +00001003{
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001004 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +00001005
Daniel Vetterfd387942015-02-11 12:41:04 +01001006 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001007 u.unique = (char *)busid;
1008 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +00001009
Keith Packard8b9ab102008-06-13 16:03:22 -07001010 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
David Dawes56bd9c22001-07-30 19:59:39 +00001011 return -errno;
1012 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001013 return 0;
1014}
1015
Jon Smirl8696e712004-07-07 04:36:36 +00001016int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001017{
1018 drm_auth_t auth;
1019
Daniel Vetterfd387942015-02-11 12:41:04 +01001020 memclear(auth);
1021
Daryll Straussb3a57661999-12-05 01:19:48 +00001022 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001023 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -06001024 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001025 *magic = auth.magic;
1026 return 0;
1027}
1028
Jon Smirl8696e712004-07-07 04:36:36 +00001029int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001030{
1031 drm_auth_t auth;
1032
Daniel Vetterfd387942015-02-11 12:41:04 +01001033 memclear(auth);
Daryll Straussb3a57661999-12-05 01:19:48 +00001034 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -07001035 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -06001036 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001037 return 0;
1038}
1039
Jose Fonsecad2443b22003-05-27 00:37:33 +00001040/**
1041 * Specifies a range of memory that is available for mapping by a
1042 * non-root process.
1043 *
1044 * \param fd file descriptor.
1045 * \param offset usually the physical address. The actual meaning depends of
1046 * the \p type parameter. See below.
1047 * \param size of the memory in bytes.
1048 * \param type type of the memory to be mapped.
1049 * \param flags combination of several flags to modify the function actions.
1050 * \param handle will be set to a value that may be used as the offset
1051 * parameter for mmap().
1052 *
1053 * \return zero on success or a negative value on error.
1054 *
1055 * \par Mapping the frame buffer
1056 * For the frame buffer
1057 * - \p offset will be the physical address of the start of the frame buffer,
1058 * - \p size will be the size of the frame buffer in bytes, and
1059 * - \p type will be DRM_FRAME_BUFFER.
1060 *
1061 * \par
1062 * The area mapped will be uncached. If MTRR support is available in the
1063 * kernel, the frame buffer area will be set to write combining.
1064 *
1065 * \par Mapping the MMIO register area
1066 * For the MMIO register area,
1067 * - \p offset will be the physical address of the start of the register area,
1068 * - \p size will be the size of the register area bytes, and
1069 * - \p type will be DRM_REGISTERS.
1070 * \par
1071 * The area mapped will be uncached.
1072 *
1073 * \par Mapping the SAREA
1074 * For the SAREA,
1075 * - \p offset will be ignored and should be set to zero,
1076 * - \p size will be the desired size of the SAREA in bytes,
1077 * - \p type will be DRM_SHM.
1078 *
1079 * \par
1080 * A shared memory area of the requested size will be created and locked in
1081 * kernel memory. This area may be mapped into client-space by using the handle
1082 * returned.
1083 *
1084 * \note May only be called by root.
1085 *
1086 * \internal
1087 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1088 * the arguments in a drm_map structure.
1089 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001090int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1091 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001092{
1093 drm_map_t map;
1094
Daniel Vetterfd387942015-02-11 12:41:04 +01001095 memclear(map);
Daryll Straussb3a57661999-12-05 01:19:48 +00001096 map.offset = offset;
1097 map.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001098 map.type = type;
1099 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -07001100 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001101 return -errno;
1102 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001103 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001104 return 0;
1105}
1106
Jon Smirl8696e712004-07-07 04:36:36 +00001107int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00001108{
1109 drm_map_t map;
1110
Daniel Vetterfd387942015-02-11 12:41:04 +01001111 memclear(map);
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001112 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001113
Keith Packard8b9ab102008-06-13 16:03:22 -07001114 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001115 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001116 return 0;
1117}
1118
Jose Fonsecad2443b22003-05-27 00:37:33 +00001119/**
1120 * Make buffers available for DMA transfers.
1121 *
1122 * \param fd file descriptor.
1123 * \param count number of buffers.
1124 * \param size size of each buffer.
1125 * \param flags buffer allocation flags.
1126 * \param agp_offset offset in the AGP aperture
1127 *
1128 * \return number of buffers allocated, negative on error.
1129 *
1130 * \internal
1131 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1132 *
1133 * \sa drm_buf_desc.
1134 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001135int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1136 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001137{
1138 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001139
Daniel Vetterfd387942015-02-11 12:41:04 +01001140 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001141 request.count = count;
1142 request.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001143 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001144 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001145
Keith Packard8b9ab102008-06-13 16:03:22 -07001146 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001147 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001148 return request.count;
1149}
1150
1151int drmMarkBufs(int fd, double low, double high)
1152{
1153 drm_buf_info_t info;
1154 int i;
1155
Daniel Vetterfd387942015-02-11 12:41:04 +01001156 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001157
Keith Packard8b9ab102008-06-13 16:03:22 -07001158 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001159 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001160
Brianccd7b6e2007-05-29 14:54:00 -06001161 if (!info.count)
1162 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001163
Daryll Straussb3a57661999-12-05 01:19:48 +00001164 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1165 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001166
Keith Packard8b9ab102008-06-13 16:03:22 -07001167 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001168 int retval = -errno;
1169 drmFree(info.list);
1170 return retval;
1171 }
Gareth Hughes36047532001-02-15 08:12:14 +00001172
Daryll Straussb3a57661999-12-05 01:19:48 +00001173 for (i = 0; i < info.count; i++) {
1174 info.list[i].low_mark = low * info.list[i].count;
1175 info.list[i].high_mark = high * info.list[i].count;
Keith Packard8b9ab102008-06-13 16:03:22 -07001176 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001177 int retval = -errno;
1178 drmFree(info.list);
1179 return retval;
1180 }
1181 }
1182 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001183
Daryll Straussb3a57661999-12-05 01:19:48 +00001184 return 0;
1185}
1186
Jose Fonsecad2443b22003-05-27 00:37:33 +00001187/**
1188 * Free buffers.
1189 *
1190 * \param fd file descriptor.
1191 * \param count number of buffers to free.
1192 * \param list list of buffers to be freed.
1193 *
1194 * \return zero on success, or a negative value on failure.
1195 *
1196 * \note This function is primarily used for debugging.
1197 *
1198 * \internal
1199 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1200 * the arguments in a drm_buf_free structure.
1201 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001202int drmFreeBufs(int fd, int count, int *list)
1203{
1204 drm_buf_free_t request;
1205
Daniel Vetterfd387942015-02-11 12:41:04 +01001206 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001207 request.count = count;
1208 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001209 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001210 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001211 return 0;
1212}
1213
Jose Fonsecad2443b22003-05-27 00:37:33 +00001214
1215/**
1216 * Close the device.
1217 *
1218 * \param fd file descriptor.
1219 *
1220 * \internal
1221 * This function closes the file descriptor.
1222 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001223int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001224{
1225 unsigned long key = drmGetKeyFromFd(fd);
1226 drmHashEntry *entry = drmGetEntry(fd);
1227
1228 drmHashDestroy(entry->tagTable);
1229 entry->fd = 0;
1230 entry->f = NULL;
1231 entry->tagTable = NULL;
1232
1233 drmHashDelete(drmHashTable, key);
1234 drmFree(entry);
1235
1236 return close(fd);
1237}
1238
Jose Fonsecad2443b22003-05-27 00:37:33 +00001239
1240/**
1241 * Map a region of memory.
1242 *
1243 * \param fd file descriptor.
1244 * \param handle handle returned by drmAddMap().
1245 * \param size size in bytes. Must match the size used by drmAddMap().
1246 * \param address will contain the user-space virtual address where the mapping
1247 * begins.
1248 *
1249 * \return zero on success, or a negative value on failure.
1250 *
1251 * \internal
1252 * This function is a wrapper for mmap().
1253 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001254int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001255{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001256 static unsigned long pagesize_mask = 0;
1257
Brianccd7b6e2007-05-29 14:54:00 -06001258 if (fd < 0)
1259 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001260
1261 if (!pagesize_mask)
1262 pagesize_mask = getpagesize() - 1;
1263
1264 size = (size + pagesize_mask) & ~pagesize_mask;
1265
Emil Velikovfaf51d52014-09-07 20:03:05 +01001266 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001267 if (*address == MAP_FAILED)
1268 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001269 return 0;
1270}
1271
Jose Fonsecad2443b22003-05-27 00:37:33 +00001272
1273/**
1274 * Unmap mappings obtained with drmMap().
1275 *
1276 * \param address address as given by drmMap().
1277 * \param size size in bytes. Must match the size used by drmMap().
1278 *
1279 * \return zero on success, or a negative value on failure.
1280 *
1281 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001282 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001283 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001284int drmUnmap(drmAddress address, drmSize size)
1285{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001286 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001287}
1288
1289drmBufInfoPtr drmGetBufInfo(int fd)
1290{
1291 drm_buf_info_t info;
1292 drmBufInfoPtr retval;
1293 int i;
1294
Daniel Vetterfd387942015-02-11 12:41:04 +01001295 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001296
Keith Packard8b9ab102008-06-13 16:03:22 -07001297 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001298 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001299
1300 if (info.count) {
1301 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1302 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001303
Keith Packard8b9ab102008-06-13 16:03:22 -07001304 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001305 drmFree(info.list);
1306 return NULL;
1307 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001308
Daryll Straussb3a57661999-12-05 01:19:48 +00001309 retval = drmMalloc(sizeof(*retval));
1310 retval->count = info.count;
1311 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1312 for (i = 0; i < info.count; i++) {
1313 retval->list[i].count = info.list[i].count;
1314 retval->list[i].size = info.list[i].size;
1315 retval->list[i].low_mark = info.list[i].low_mark;
1316 retval->list[i].high_mark = info.list[i].high_mark;
1317 }
1318 drmFree(info.list);
1319 return retval;
1320 }
1321 return NULL;
1322}
1323
Jose Fonsecad2443b22003-05-27 00:37:33 +00001324/**
1325 * Map all DMA buffers into client-virtual space.
1326 *
1327 * \param fd file descriptor.
1328 *
1329 * \return a pointer to a ::drmBufMap structure.
1330 *
1331 * \note The client may not use these buffers until obtaining buffer indices
1332 * with drmDMA().
1333 *
1334 * \internal
1335 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1336 * information about the buffers in a drm_buf_map structure into the
1337 * client-visible data structures.
1338 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001339drmBufMapPtr drmMapBufs(int fd)
1340{
1341 drm_buf_map_t bufs;
1342 drmBufMapPtr retval;
1343 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001344
Daniel Vetterfd387942015-02-11 12:41:04 +01001345 memclear(bufs);
Keith Packard8b9ab102008-06-13 16:03:22 -07001346 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Brianccd7b6e2007-05-29 14:54:00 -06001347 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001348
Brianccd7b6e2007-05-29 14:54:00 -06001349 if (!bufs.count)
1350 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001351
Daryll Straussb3a57661999-12-05 01:19:48 +00001352 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1353 return NULL;
1354
Keith Packard8b9ab102008-06-13 16:03:22 -07001355 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001356 drmFree(bufs.list);
1357 return NULL;
1358 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001359
Daryll Straussb3a57661999-12-05 01:19:48 +00001360 retval = drmMalloc(sizeof(*retval));
1361 retval->count = bufs.count;
1362 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1363 for (i = 0; i < bufs.count; i++) {
1364 retval->list[i].idx = bufs.list[i].idx;
1365 retval->list[i].total = bufs.list[i].total;
1366 retval->list[i].used = 0;
1367 retval->list[i].address = bufs.list[i].address;
1368 }
Jon Smirl8696e712004-07-07 04:36:36 +00001369
1370 drmFree(bufs.list);
1371
Daryll Straussb3a57661999-12-05 01:19:48 +00001372 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001373}
1374
Jose Fonsecad2443b22003-05-27 00:37:33 +00001375
1376/**
1377 * Unmap buffers allocated with drmMapBufs().
1378 *
1379 * \return zero on success, or negative value on failure.
1380 *
1381 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001382 * Calls munmap() for every buffer stored in \p bufs and frees the
1383 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001384 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001385int drmUnmapBufs(drmBufMapPtr bufs)
1386{
1387 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001388
Daryll Straussb3a57661999-12-05 01:19:48 +00001389 for (i = 0; i < bufs->count; i++) {
Emil Velikovfaf51d52014-09-07 20:03:05 +01001390 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001391 }
Jon Smirl8696e712004-07-07 04:36:36 +00001392
1393 drmFree(bufs->list);
1394 drmFree(bufs);
1395
Daryll Straussb3a57661999-12-05 01:19:48 +00001396 return 0;
1397}
1398
Jose Fonsecad2443b22003-05-27 00:37:33 +00001399
Gareth Hughes36047532001-02-15 08:12:14 +00001400#define DRM_DMA_RETRY 16
1401
Jose Fonsecad2443b22003-05-27 00:37:33 +00001402/**
1403 * Reserve DMA buffers.
1404 *
1405 * \param fd file descriptor.
1406 * \param request
1407 *
1408 * \return zero on success, or a negative value on failure.
1409 *
1410 * \internal
1411 * Assemble the arguments into a drm_dma structure and keeps issuing the
1412 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1413 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001414int drmDMA(int fd, drmDMAReqPtr request)
1415{
1416 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001417 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001418
Daryll Straussb3a57661999-12-05 01:19:48 +00001419 dma.context = request->context;
1420 dma.send_count = request->send_count;
1421 dma.send_indices = request->send_list;
1422 dma.send_sizes = request->send_sizes;
1423 dma.flags = request->flags;
1424 dma.request_count = request->request_count;
1425 dma.request_size = request->request_size;
1426 dma.request_indices = request->request_list;
1427 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001428 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001429
1430 do {
1431 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1432 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1433
1434 if ( ret == 0 ) {
1435 request->granted_count = dma.granted_count;
1436 return 0;
1437 } else {
1438 return -errno;
1439 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001440}
1441
Jose Fonsecad2443b22003-05-27 00:37:33 +00001442
1443/**
1444 * Obtain heavyweight hardware lock.
1445 *
1446 * \param fd file descriptor.
1447 * \param context context.
1448 * \param flags flags that determine the sate of the hardware when the function
1449 * returns.
1450 *
1451 * \return always zero.
1452 *
1453 * \internal
1454 * This function translates the arguments into a drm_lock structure and issue
1455 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1456 */
Jon Smirl8696e712004-07-07 04:36:36 +00001457int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001458{
1459 drm_lock_t lock;
1460
Daniel Vetterfd387942015-02-11 12:41:04 +01001461 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001462 lock.context = context;
1463 lock.flags = 0;
1464 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1465 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1466 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1467 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1468 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1469 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001470
Keith Packard8b9ab102008-06-13 16:03:22 -07001471 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Daryll Straussb3a57661999-12-05 01:19:48 +00001472 ;
1473 return 0;
1474}
1475
Jose Fonsecad2443b22003-05-27 00:37:33 +00001476/**
1477 * Release the hardware lock.
1478 *
1479 * \param fd file descriptor.
1480 * \param context context.
1481 *
1482 * \return zero on success, or a negative value on failure.
1483 *
1484 * \internal
1485 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1486 * argument in a drm_lock structure.
1487 */
Jon Smirl8696e712004-07-07 04:36:36 +00001488int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001489{
1490 drm_lock_t lock;
1491
Daniel Vetterfd387942015-02-11 12:41:04 +01001492 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001493 lock.context = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001494 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001495}
1496
Adam Jackson22e41ef2006-02-20 23:09:00 +00001497drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001498{
1499 drm_ctx_res_t res;
1500 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001501 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001502 int i;
1503
Daniel Vetterfd387942015-02-11 12:41:04 +01001504 memclear(res);
Keith Packard8b9ab102008-06-13 16:03:22 -07001505 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001506 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001507
Brianccd7b6e2007-05-29 14:54:00 -06001508 if (!res.count)
1509 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001510
Brianccd7b6e2007-05-29 14:54:00 -06001511 if (!(list = drmMalloc(res.count * sizeof(*list))))
1512 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001513 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1514 drmFree(list);
1515 return NULL;
1516 }
1517
1518 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001519 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001520 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001521
Brianccd7b6e2007-05-29 14:54:00 -06001522 for (i = 0; i < res.count; i++)
1523 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001524 drmFree(list);
1525
1526 *count = res.count;
1527 return retval;
1528}
1529
Adam Jackson22e41ef2006-02-20 23:09:00 +00001530void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001531{
1532 drmFree(pt);
1533}
1534
Jose Fonsecad2443b22003-05-27 00:37:33 +00001535/**
1536 * Create context.
1537 *
1538 * Used by the X server during GLXContext initialization. This causes
1539 * per-context kernel-level resources to be allocated.
1540 *
1541 * \param fd file descriptor.
1542 * \param handle is set on success. To be used by the client when requesting DMA
1543 * dispatch with drmDMA().
1544 *
1545 * \return zero on success, or a negative value on failure.
1546 *
1547 * \note May only be called by root.
1548 *
1549 * \internal
1550 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1551 * argument in a drm_ctx structure.
1552 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001553int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001554{
1555 drm_ctx_t ctx;
1556
Daniel Vetterfd387942015-02-11 12:41:04 +01001557 memclear(ctx);
Keith Packard8b9ab102008-06-13 16:03:22 -07001558 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001559 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001560 *handle = ctx.handle;
1561 return 0;
1562}
1563
Jon Smirl8696e712004-07-07 04:36:36 +00001564int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001565{
1566 drm_ctx_t ctx;
1567
Daniel Vetterfd387942015-02-11 12:41:04 +01001568 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001569 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001570 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001571 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001572 return 0;
1573}
1574
Jon Smirl8696e712004-07-07 04:36:36 +00001575int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001576{
1577 drm_ctx_t ctx;
1578
Adam Jackson22e41ef2006-02-20 23:09:00 +00001579 /*
1580 * Context preserving means that no context switches are done between DMA
1581 * buffers from one context and the next. This is suitable for use in the
1582 * X server (which promises to maintain hardware context), or in the
1583 * client-side library when buffers are swapped on behalf of two threads.
1584 */
Daniel Vetterfd387942015-02-11 12:41:04 +01001585 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001586 ctx.handle = context;
Brianccd7b6e2007-05-29 14:54:00 -06001587 if (flags & DRM_CONTEXT_PRESERVED)
1588 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1589 if (flags & DRM_CONTEXT_2DONLY)
1590 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001591 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001592 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001593 return 0;
1594}
1595
Adam Jackson22e41ef2006-02-20 23:09:00 +00001596int drmGetContextFlags(int fd, drm_context_t context,
1597 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001598{
1599 drm_ctx_t ctx;
1600
Daniel Vetterfd387942015-02-11 12:41:04 +01001601 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001602 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001603 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001604 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001605 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001606 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1607 *flags |= DRM_CONTEXT_PRESERVED;
1608 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1609 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001610 return 0;
1611}
Gareth Hughes36047532001-02-15 08:12:14 +00001612
Jose Fonsecad2443b22003-05-27 00:37:33 +00001613/**
1614 * Destroy context.
1615 *
1616 * Free any kernel-level resources allocated with drmCreateContext() associated
1617 * with the context.
1618 *
1619 * \param fd file descriptor.
1620 * \param handle handle given by drmCreateContext().
1621 *
1622 * \return zero on success, or a negative value on failure.
1623 *
1624 * \note May only be called by root.
1625 *
1626 * \internal
1627 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1628 * argument in a drm_ctx structure.
1629 */
Jon Smirl8696e712004-07-07 04:36:36 +00001630int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001631{
1632 drm_ctx_t ctx;
Daniel Vetterfd387942015-02-11 12:41:04 +01001633
1634 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001635 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001636 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001637 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001638 return 0;
1639}
1640
Adam Jackson22e41ef2006-02-20 23:09:00 +00001641int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001642{
1643 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001644
1645 memclear(draw);
Keith Packard8b9ab102008-06-13 16:03:22 -07001646 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001647 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001648 *handle = draw.handle;
1649 return 0;
1650}
1651
Jon Smirl8696e712004-07-07 04:36:36 +00001652int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001653{
1654 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001655
1656 memclear(draw);
Daryll Straussb3a57661999-12-05 01:19:48 +00001657 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001658 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001659 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001660 return 0;
1661}
1662
Michel Dänzer9810ec22006-08-22 16:40:07 +02001663int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1664 drm_drawable_info_type_t type, unsigned int num,
1665 void *data)
1666{
1667 drm_update_draw_t update;
1668
Daniel Vetterfd387942015-02-11 12:41:04 +01001669 memclear(update);
Michel Dänzer9810ec22006-08-22 16:40:07 +02001670 update.handle = handle;
1671 update.type = type;
1672 update.num = num;
1673 update.data = (unsigned long long)(unsigned long)data;
1674
Keith Packard8b9ab102008-06-13 16:03:22 -07001675 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Brianccd7b6e2007-05-29 14:54:00 -06001676 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001677
1678 return 0;
1679}
1680
Jose Fonsecad2443b22003-05-27 00:37:33 +00001681/**
1682 * Acquire the AGP device.
1683 *
1684 * Must be called before any of the other AGP related calls.
1685 *
1686 * \param fd file descriptor.
1687 *
1688 * \return zero on success, or a negative value on failure.
1689 *
1690 * \internal
1691 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1692 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001693int drmAgpAcquire(int fd)
1694{
Keith Packard8b9ab102008-06-13 16:03:22 -07001695 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001696 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001697 return 0;
1698}
1699
Jose Fonsecad2443b22003-05-27 00:37:33 +00001700
1701/**
1702 * Release the AGP device.
1703 *
1704 * \param fd file descriptor.
1705 *
1706 * \return zero on success, or a negative value on failure.
1707 *
1708 * \internal
1709 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1710 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001711int drmAgpRelease(int fd)
1712{
Keith Packard8b9ab102008-06-13 16:03:22 -07001713 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001714 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001715 return 0;
1716}
1717
Jose Fonsecad2443b22003-05-27 00:37:33 +00001718
1719/**
1720 * Set the AGP mode.
1721 *
1722 * \param fd file descriptor.
1723 * \param mode AGP mode.
1724 *
1725 * \return zero on success, or a negative value on failure.
1726 *
1727 * \internal
1728 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1729 * argument in a drm_agp_mode structure.
1730 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001731int drmAgpEnable(int fd, unsigned long mode)
1732{
1733 drm_agp_mode_t m;
1734
Connor Behan14900552015-03-24 13:53:51 -04001735 memclear(m);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001736 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001737 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Brianccd7b6e2007-05-29 14:54:00 -06001738 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001739 return 0;
1740}
1741
Jose Fonsecad2443b22003-05-27 00:37:33 +00001742
1743/**
1744 * Allocate a chunk of AGP memory.
1745 *
1746 * \param fd file descriptor.
1747 * \param size requested memory size in bytes. Will be rounded to page boundary.
1748 * \param type type of memory to allocate.
1749 * \param address if not zero, will be set to the physical address of the
1750 * allocated memory.
1751 * \param handle on success will be set to a handle of the allocated memory.
1752 *
1753 * \return zero on success, or a negative value on failure.
1754 *
1755 * \internal
1756 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1757 * arguments in a drm_agp_buffer structure.
1758 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001759int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Dave Airlie7ede2092005-11-29 09:50:47 +00001760 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001761{
1762 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001763
Daniel Vetterfd387942015-02-11 12:41:04 +01001764 memclear(b);
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001765 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001766 b.size = size;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001767 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001768 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001769 return -errno;
1770 if (address != 0UL)
1771 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001772 *handle = b.handle;
1773 return 0;
1774}
1775
Jose Fonsecad2443b22003-05-27 00:37:33 +00001776
1777/**
1778 * Free a chunk of AGP memory.
1779 *
1780 * \param fd file descriptor.
1781 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1782 *
1783 * \return zero on success, or a negative value on failure.
1784 *
1785 * \internal
1786 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1787 * argument in a drm_agp_buffer structure.
1788 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001789int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001790{
1791 drm_agp_buffer_t b;
1792
Daniel Vetterfd387942015-02-11 12:41:04 +01001793 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001794 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001795 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001796 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001797 return 0;
1798}
1799
Jose Fonsecad2443b22003-05-27 00:37:33 +00001800
1801/**
1802 * Bind a chunk of AGP memory.
1803 *
1804 * \param fd file descriptor.
1805 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1806 * \param offset offset in bytes. It will round to page boundary.
1807 *
1808 * \return zero on success, or a negative value on failure.
1809 *
1810 * \internal
1811 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1812 * argument in a drm_agp_binding structure.
1813 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001814int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001815{
1816 drm_agp_binding_t b;
1817
Daniel Vetterfd387942015-02-11 12:41:04 +01001818 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001819 b.handle = handle;
1820 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001821 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001822 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001823 return 0;
1824}
1825
Jose Fonsecad2443b22003-05-27 00:37:33 +00001826
1827/**
1828 * Unbind a chunk of AGP memory.
1829 *
1830 * \param fd file descriptor.
1831 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1832 *
1833 * \return zero on success, or a negative value on failure.
1834 *
1835 * \internal
1836 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1837 * the argument in a drm_agp_binding structure.
1838 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001839int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001840{
1841 drm_agp_binding_t b;
1842
Daniel Vetterfd387942015-02-11 12:41:04 +01001843 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001844 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001845 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001846 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001847 return 0;
1848}
1849
Jose Fonsecad2443b22003-05-27 00:37:33 +00001850
1851/**
1852 * Get AGP driver major version number.
1853 *
1854 * \param fd file descriptor.
1855 *
1856 * \return major version number on success, or a negative value on failure..
1857 *
1858 * \internal
1859 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1860 * necessary information in a drm_agp_info structure.
1861 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001862int drmAgpVersionMajor(int fd)
1863{
1864 drm_agp_info_t i;
1865
Daniel Vetterfd387942015-02-11 12:41:04 +01001866 memclear(i);
1867
Keith Packard8b9ab102008-06-13 16:03:22 -07001868 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001869 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001870 return i.agp_version_major;
1871}
1872
Jose Fonsecad2443b22003-05-27 00:37:33 +00001873
1874/**
1875 * Get AGP driver minor version number.
1876 *
1877 * \param fd file descriptor.
1878 *
1879 * \return minor version number on success, or a negative value on failure.
1880 *
1881 * \internal
1882 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1883 * necessary information in a drm_agp_info structure.
1884 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001885int drmAgpVersionMinor(int fd)
1886{
1887 drm_agp_info_t i;
1888
Daniel Vetterfd387942015-02-11 12:41:04 +01001889 memclear(i);
1890
Keith Packard8b9ab102008-06-13 16:03:22 -07001891 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001892 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001893 return i.agp_version_minor;
1894}
1895
Jose Fonsecad2443b22003-05-27 00:37:33 +00001896
1897/**
1898 * Get AGP mode.
1899 *
1900 * \param fd file descriptor.
1901 *
1902 * \return mode on success, or zero on failure.
1903 *
1904 * \internal
1905 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1906 * necessary information in a drm_agp_info structure.
1907 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001908unsigned long drmAgpGetMode(int fd)
1909{
1910 drm_agp_info_t i;
1911
Daniel Vetterfd387942015-02-11 12:41:04 +01001912 memclear(i);
1913
Keith Packard8b9ab102008-06-13 16:03:22 -07001914 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001915 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001916 return i.mode;
1917}
1918
Jose Fonsecad2443b22003-05-27 00:37:33 +00001919
1920/**
1921 * Get AGP aperture base.
1922 *
1923 * \param fd file descriptor.
1924 *
1925 * \return aperture base on success, zero on failure.
1926 *
1927 * \internal
1928 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1929 * necessary information in a drm_agp_info structure.
1930 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001931unsigned long drmAgpBase(int fd)
1932{
1933 drm_agp_info_t i;
1934
Daniel Vetterfd387942015-02-11 12:41:04 +01001935 memclear(i);
1936
Keith Packard8b9ab102008-06-13 16:03:22 -07001937 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001938 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001939 return i.aperture_base;
1940}
1941
Jose Fonsecad2443b22003-05-27 00:37:33 +00001942
1943/**
1944 * Get AGP aperture size.
1945 *
1946 * \param fd file descriptor.
1947 *
1948 * \return aperture size on success, zero on failure.
1949 *
1950 * \internal
1951 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1952 * necessary information in a drm_agp_info structure.
1953 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001954unsigned long drmAgpSize(int fd)
1955{
1956 drm_agp_info_t i;
1957
Daniel Vetterfd387942015-02-11 12:41:04 +01001958 memclear(i);
1959
Keith Packard8b9ab102008-06-13 16:03:22 -07001960 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001961 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001962 return i.aperture_size;
1963}
1964
Jose Fonsecad2443b22003-05-27 00:37:33 +00001965
1966/**
1967 * Get used AGP memory.
1968 *
1969 * \param fd file descriptor.
1970 *
1971 * \return memory used on success, or zero on failure.
1972 *
1973 * \internal
1974 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1975 * necessary information in a drm_agp_info structure.
1976 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001977unsigned long drmAgpMemoryUsed(int fd)
1978{
1979 drm_agp_info_t i;
1980
Daniel Vetterfd387942015-02-11 12:41:04 +01001981 memclear(i);
1982
Keith Packard8b9ab102008-06-13 16:03:22 -07001983 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001984 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001985 return i.memory_used;
1986}
1987
Jose Fonsecad2443b22003-05-27 00:37:33 +00001988
1989/**
1990 * Get available AGP memory.
1991 *
1992 * \param fd file descriptor.
1993 *
1994 * \return memory available on success, or zero on failure.
1995 *
1996 * \internal
1997 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1998 * necessary information in a drm_agp_info structure.
1999 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002000unsigned long drmAgpMemoryAvail(int fd)
2001{
2002 drm_agp_info_t i;
2003
Daniel Vetterfd387942015-02-11 12:41:04 +01002004 memclear(i);
2005
Keith Packard8b9ab102008-06-13 16:03:22 -07002006 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002007 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002008 return i.memory_allowed;
2009}
2010
Jose Fonsecad2443b22003-05-27 00:37:33 +00002011
2012/**
2013 * Get hardware vendor ID.
2014 *
2015 * \param fd file descriptor.
2016 *
2017 * \return vendor ID on success, or zero on failure.
2018 *
2019 * \internal
2020 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2021 * necessary information in a drm_agp_info structure.
2022 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002023unsigned int drmAgpVendorId(int fd)
2024{
2025 drm_agp_info_t i;
2026
Daniel Vetterfd387942015-02-11 12:41:04 +01002027 memclear(i);
2028
Keith Packard8b9ab102008-06-13 16:03:22 -07002029 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002030 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002031 return i.id_vendor;
2032}
2033
Jose Fonsecad2443b22003-05-27 00:37:33 +00002034
2035/**
2036 * Get hardware device ID.
2037 *
2038 * \param fd file descriptor.
2039 *
2040 * \return zero on success, or zero on failure.
2041 *
2042 * \internal
2043 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2044 * necessary information in a drm_agp_info structure.
2045 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002046unsigned int drmAgpDeviceId(int fd)
2047{
2048 drm_agp_info_t i;
2049
Daniel Vetterfd387942015-02-11 12:41:04 +01002050 memclear(i);
2051
Keith Packard8b9ab102008-06-13 16:03:22 -07002052 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002053 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002054 return i.id_device;
2055}
2056
Dave Airlie7ede2092005-11-29 09:50:47 +00002057int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002058{
2059 drm_scatter_gather_t sg;
2060
Daniel Vetterfd387942015-02-11 12:41:04 +01002061 memclear(sg);
2062
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002063 *handle = 0;
2064 sg.size = size;
Keith Packard8b9ab102008-06-13 16:03:22 -07002065 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06002066 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002067 *handle = sg.handle;
2068 return 0;
2069}
2070
Dave Airlie7ede2092005-11-29 09:50:47 +00002071int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002072{
2073 drm_scatter_gather_t sg;
2074
Daniel Vetterfd387942015-02-11 12:41:04 +01002075 memclear(sg);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002076 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07002077 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06002078 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002079 return 0;
2080}
2081
Jose Fonsecad2443b22003-05-27 00:37:33 +00002082/**
2083 * Wait for VBLANK.
2084 *
2085 * \param fd file descriptor.
2086 * \param vbl pointer to a drmVBlank structure.
2087 *
2088 * \return zero on success, or a negative value on failure.
2089 *
2090 * \internal
2091 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2092 */
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002093int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2094{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002095 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002096 int ret;
2097
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002098 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2099 if (ret < 0) {
Daniel Kurtz1eb28602013-03-28 14:05:40 +08002100 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002101 goto out;
2102 }
2103 timeout.tv_sec++;
2104
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002105 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002106 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00002107 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08002108 if (ret && errno == EINTR) {
2109 clock_gettime(CLOCK_MONOTONIC, &cur);
2110 /* Timeout after 1s */
2111 if (cur.tv_sec > timeout.tv_sec + 1 ||
2112 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2113 timeout.tv_nsec)) {
2114 errno = EBUSY;
2115 ret = -1;
2116 break;
2117 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002118 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002119 } while (ret && errno == EINTR);
2120
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002121out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002122 return ret;
2123}
2124
Daryll Straussb3a57661999-12-05 01:19:48 +00002125int drmError(int err, const char *label)
2126{
2127 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002128 case DRM_ERR_NO_DEVICE:
2129 fprintf(stderr, "%s: no device\n", label);
2130 break;
2131 case DRM_ERR_NO_ACCESS:
2132 fprintf(stderr, "%s: no access\n", label);
2133 break;
2134 case DRM_ERR_NOT_ROOT:
2135 fprintf(stderr, "%s: not root\n", label);
2136 break;
2137 case DRM_ERR_INVALID:
2138 fprintf(stderr, "%s: invalid args\n", label);
2139 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002140 default:
Brianccd7b6e2007-05-29 14:54:00 -06002141 if (err < 0)
2142 err = -err;
Daryll Straussb3a57661999-12-05 01:19:48 +00002143 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2144 break;
2145 }
2146
2147 return 1;
2148}
2149
Jose Fonsecad2443b22003-05-27 00:37:33 +00002150/**
2151 * Install IRQ handler.
2152 *
2153 * \param fd file descriptor.
2154 * \param irq IRQ number.
2155 *
2156 * \return zero on success, or a negative value on failure.
2157 *
2158 * \internal
2159 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2160 * argument in a drm_control structure.
2161 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002162int drmCtlInstHandler(int fd, int irq)
2163{
2164 drm_control_t ctl;
2165
Daniel Vetterfd387942015-02-11 12:41:04 +01002166 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002167 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002168 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002169 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002170 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002171 return 0;
2172}
2173
Jose Fonsecad2443b22003-05-27 00:37:33 +00002174
2175/**
2176 * Uninstall IRQ handler.
2177 *
2178 * \param fd file descriptor.
2179 *
2180 * \return zero on success, or a negative value on failure.
2181 *
2182 * \internal
2183 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2184 * argument in a drm_control structure.
2185 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002186int drmCtlUninstHandler(int fd)
2187{
2188 drm_control_t ctl;
2189
Daniel Vetterfd387942015-02-11 12:41:04 +01002190 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002191 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002192 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002193 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002194 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002195 return 0;
2196}
2197
2198int drmFinish(int fd, int context, drmLockFlags flags)
2199{
2200 drm_lock_t lock;
2201
Daniel Vetterfd387942015-02-11 12:41:04 +01002202 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00002203 lock.context = context;
Daryll Straussb3a57661999-12-05 01:19:48 +00002204 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2205 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2206 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2207 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2208 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2209 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002210 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Brianccd7b6e2007-05-29 14:54:00 -06002211 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002212 return 0;
2213}
2214
Jose Fonsecad2443b22003-05-27 00:37:33 +00002215/**
2216 * Get IRQ from bus ID.
2217 *
2218 * \param fd file descriptor.
2219 * \param busnum bus number.
2220 * \param devnum device number.
2221 * \param funcnum function number.
2222 *
2223 * \return IRQ number on success, or a negative value on failure.
2224 *
2225 * \internal
2226 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2227 * arguments in a drm_irq_busid structure.
2228 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002229int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2230{
2231 drm_irq_busid_t p;
2232
Daniel Vetterfd387942015-02-11 12:41:04 +01002233 memclear(p);
Daryll Straussb3a57661999-12-05 01:19:48 +00002234 p.busnum = busnum;
2235 p.devnum = devnum;
2236 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002237 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Brianccd7b6e2007-05-29 14:54:00 -06002238 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002239 return p.irq;
2240}
2241
Jon Smirl8696e712004-07-07 04:36:36 +00002242int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002243{
2244 drmHashEntry *entry = drmGetEntry(fd);
2245
2246 if (drmHashInsert(entry->tagTable, context, tag)) {
2247 drmHashDelete(entry->tagTable, context);
2248 drmHashInsert(entry->tagTable, context, tag);
2249 }
2250 return 0;
2251}
2252
Jon Smirl8696e712004-07-07 04:36:36 +00002253int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002254{
2255 drmHashEntry *entry = drmGetEntry(fd);
2256
2257 return drmHashDelete(entry->tagTable, context);
2258}
2259
Jon Smirl8696e712004-07-07 04:36:36 +00002260void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002261{
2262 drmHashEntry *entry = drmGetEntry(fd);
2263 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002264
Brianccd7b6e2007-05-29 14:54:00 -06002265 if (drmHashLookup(entry->tagTable, context, &value))
2266 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002267
2268 return value;
2269}
2270
Adam Jackson22e41ef2006-02-20 23:09:00 +00002271int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2272 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002273{
2274 drm_ctx_priv_map_t map;
2275
Daniel Vetterfd387942015-02-11 12:41:04 +01002276 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002277 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002278 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002279
Keith Packard8b9ab102008-06-13 16:03:22 -07002280 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002281 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002282 return 0;
2283}
2284
Adam Jackson22e41ef2006-02-20 23:09:00 +00002285int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2286 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002287{
2288 drm_ctx_priv_map_t map;
2289
Daniel Vetterfd387942015-02-11 12:41:04 +01002290 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002291 map.ctx_id = ctx_id;
2292
Keith Packard8b9ab102008-06-13 16:03:22 -07002293 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002294 return -errno;
2295 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002296 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002297
2298 return 0;
2299}
2300
Jon Smirl8696e712004-07-07 04:36:36 +00002301int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2302 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
Rik Faith88dbee52001-02-28 09:27:44 +00002303 int *mtrr)
2304{
2305 drm_map_t map;
2306
Daniel Vetterfd387942015-02-11 12:41:04 +01002307 memclear(map);
Rik Faith88dbee52001-02-28 09:27:44 +00002308 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002309 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002310 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002311 *offset = map.offset;
2312 *size = map.size;
2313 *type = map.type;
2314 *flags = map.flags;
2315 *handle = (unsigned long)map.handle;
2316 *mtrr = map.mtrr;
2317 return 0;
2318}
2319
2320int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2321 unsigned long *magic, unsigned long *iocs)
2322{
2323 drm_client_t client;
2324
Daniel Vetterfd387942015-02-11 12:41:04 +01002325 memclear(client);
Rik Faith88dbee52001-02-28 09:27:44 +00002326 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002327 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Brianccd7b6e2007-05-29 14:54:00 -06002328 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002329 *auth = client.auth;
2330 *pid = client.pid;
2331 *uid = client.uid;
2332 *magic = client.magic;
2333 *iocs = client.iocs;
2334 return 0;
2335}
2336
2337int drmGetStats(int fd, drmStatsT *stats)
2338{
2339 drm_stats_t s;
Jan Veselyde8532d2014-11-30 12:53:18 -05002340 unsigned i;
Rik Faith88dbee52001-02-28 09:27:44 +00002341
Daniel Vetterfd387942015-02-11 12:41:04 +01002342 memclear(s);
Keith Packard8b9ab102008-06-13 16:03:22 -07002343 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Brianccd7b6e2007-05-29 14:54:00 -06002344 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002345
2346 stats->count = 0;
2347 memset(stats, 0, sizeof(*stats));
2348 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2349 return -1;
2350
2351#define SET_VALUE \
2352 stats->data[i].long_format = "%-20.20s"; \
2353 stats->data[i].rate_format = "%8.8s"; \
2354 stats->data[i].isvalue = 1; \
2355 stats->data[i].verbose = 0
2356
2357#define SET_COUNT \
2358 stats->data[i].long_format = "%-20.20s"; \
2359 stats->data[i].rate_format = "%5.5s"; \
2360 stats->data[i].isvalue = 0; \
2361 stats->data[i].mult_names = "kgm"; \
2362 stats->data[i].mult = 1000; \
2363 stats->data[i].verbose = 0
2364
2365#define SET_BYTE \
2366 stats->data[i].long_format = "%-20.20s"; \
2367 stats->data[i].rate_format = "%5.5s"; \
2368 stats->data[i].isvalue = 0; \
2369 stats->data[i].mult_names = "KGM"; \
2370 stats->data[i].mult = 1024; \
2371 stats->data[i].verbose = 0
2372
2373
2374 stats->count = s.count;
2375 for (i = 0; i < s.count; i++) {
2376 stats->data[i].value = s.data[i].value;
2377 switch (s.data[i].type) {
2378 case _DRM_STAT_LOCK:
2379 stats->data[i].long_name = "Lock";
2380 stats->data[i].rate_name = "Lock";
2381 SET_VALUE;
2382 break;
2383 case _DRM_STAT_OPENS:
2384 stats->data[i].long_name = "Opens";
2385 stats->data[i].rate_name = "O";
2386 SET_COUNT;
2387 stats->data[i].verbose = 1;
2388 break;
2389 case _DRM_STAT_CLOSES:
2390 stats->data[i].long_name = "Closes";
2391 stats->data[i].rate_name = "Lock";
2392 SET_COUNT;
2393 stats->data[i].verbose = 1;
2394 break;
2395 case _DRM_STAT_IOCTLS:
2396 stats->data[i].long_name = "Ioctls";
2397 stats->data[i].rate_name = "Ioc/s";
2398 SET_COUNT;
2399 break;
2400 case _DRM_STAT_LOCKS:
2401 stats->data[i].long_name = "Locks";
2402 stats->data[i].rate_name = "Lck/s";
2403 SET_COUNT;
2404 break;
2405 case _DRM_STAT_UNLOCKS:
2406 stats->data[i].long_name = "Unlocks";
2407 stats->data[i].rate_name = "Unl/s";
2408 SET_COUNT;
2409 break;
2410 case _DRM_STAT_IRQ:
2411 stats->data[i].long_name = "IRQs";
2412 stats->data[i].rate_name = "IRQ/s";
2413 SET_COUNT;
2414 break;
2415 case _DRM_STAT_PRIMARY:
2416 stats->data[i].long_name = "Primary Bytes";
2417 stats->data[i].rate_name = "PB/s";
2418 SET_BYTE;
2419 break;
2420 case _DRM_STAT_SECONDARY:
2421 stats->data[i].long_name = "Secondary Bytes";
2422 stats->data[i].rate_name = "SB/s";
2423 SET_BYTE;
2424 break;
2425 case _DRM_STAT_DMA:
2426 stats->data[i].long_name = "DMA";
2427 stats->data[i].rate_name = "DMA/s";
2428 SET_COUNT;
2429 break;
2430 case _DRM_STAT_SPECIAL:
2431 stats->data[i].long_name = "Special DMA";
2432 stats->data[i].rate_name = "dma/s";
2433 SET_COUNT;
2434 break;
2435 case _DRM_STAT_MISSED:
2436 stats->data[i].long_name = "Miss";
2437 stats->data[i].rate_name = "Ms/s";
2438 SET_COUNT;
2439 break;
2440 case _DRM_STAT_VALUE:
2441 stats->data[i].long_name = "Value";
2442 stats->data[i].rate_name = "Value";
2443 SET_VALUE;
2444 break;
2445 case _DRM_STAT_BYTE:
2446 stats->data[i].long_name = "Bytes";
2447 stats->data[i].rate_name = "B/s";
2448 SET_BYTE;
2449 break;
2450 case _DRM_STAT_COUNT:
2451 default:
2452 stats->data[i].long_name = "Count";
2453 stats->data[i].rate_name = "Cnt/s";
2454 SET_COUNT;
2455 break;
2456 }
2457 }
2458 return 0;
2459}
2460
Jose Fonsecad2443b22003-05-27 00:37:33 +00002461/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002462 * Issue a set-version ioctl.
2463 *
2464 * \param fd file descriptor.
2465 * \param drmCommandIndex command index
2466 * \param data source pointer of the data to be read and written.
2467 * \param size size of the data to be read and written.
2468 *
2469 * \return zero on success, or a negative value on failure.
2470 *
2471 * \internal
2472 * It issues a read-write ioctl given by
2473 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2474 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002475int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002476{
2477 int retcode = 0;
2478 drm_set_version_t sv;
2479
Daniel Vetterfd387942015-02-11 12:41:04 +01002480 memclear(sv);
Eric Anholt06cb1322003-10-23 02:23:31 +00002481 sv.drm_di_major = version->drm_di_major;
2482 sv.drm_di_minor = version->drm_di_minor;
2483 sv.drm_dd_major = version->drm_dd_major;
2484 sv.drm_dd_minor = version->drm_dd_minor;
2485
Keith Packard8b9ab102008-06-13 16:03:22 -07002486 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Eric Anholt06cb1322003-10-23 02:23:31 +00002487 retcode = -errno;
2488 }
2489
2490 version->drm_di_major = sv.drm_di_major;
2491 version->drm_di_minor = sv.drm_di_minor;
2492 version->drm_dd_major = sv.drm_dd_major;
2493 version->drm_dd_minor = sv.drm_dd_minor;
2494
2495 return retcode;
2496}
2497
2498/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002499 * Send a device-specific command.
2500 *
2501 * \param fd file descriptor.
2502 * \param drmCommandIndex command index
2503 *
2504 * \return zero on success, or a negative value on failure.
2505 *
2506 * \internal
2507 * It issues a ioctl given by
2508 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2509 */
Jens Owen3903e5a2002-04-09 21:54:56 +00002510int drmCommandNone(int fd, unsigned long drmCommandIndex)
2511{
Jens Owen3903e5a2002-04-09 21:54:56 +00002512 unsigned long request;
2513
2514 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2515
Daniel Vetterfd387942015-02-11 12:41:04 +01002516 if (drmIoctl(fd, request, NULL)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002517 return -errno;
2518 }
2519 return 0;
2520}
2521
Jose Fonsecad2443b22003-05-27 00:37:33 +00002522
2523/**
2524 * Send a device-specific read command.
2525 *
2526 * \param fd file descriptor.
2527 * \param drmCommandIndex command index
2528 * \param data destination pointer of the data to be read.
2529 * \param size size of the data to be read.
2530 *
2531 * \return zero on success, or a negative value on failure.
2532 *
2533 * \internal
2534 * It issues a read ioctl given by
2535 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2536 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002537int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2538 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002539{
2540 unsigned long request;
2541
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002542 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2543 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002544
Keith Packard8b9ab102008-06-13 16:03:22 -07002545 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002546 return -errno;
2547 }
2548 return 0;
2549}
2550
Jose Fonsecad2443b22003-05-27 00:37:33 +00002551
2552/**
2553 * Send a device-specific write command.
2554 *
2555 * \param fd file descriptor.
2556 * \param drmCommandIndex command index
2557 * \param data source pointer of the data to be written.
2558 * \param size size of the data to be written.
2559 *
2560 * \return zero on success, or a negative value on failure.
2561 *
2562 * \internal
2563 * It issues a write ioctl given by
2564 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2565 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002566int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2567 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002568{
2569 unsigned long request;
2570
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002571 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2572 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002573
Keith Packard8b9ab102008-06-13 16:03:22 -07002574 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002575 return -errno;
2576 }
2577 return 0;
2578}
2579
Jose Fonsecad2443b22003-05-27 00:37:33 +00002580
2581/**
2582 * Send a device-specific read-write command.
2583 *
2584 * \param fd file descriptor.
2585 * \param drmCommandIndex command index
2586 * \param data source pointer of the data to be read and written.
2587 * \param size size of the data to be read and written.
2588 *
2589 * \return zero on success, or a negative value on failure.
2590 *
2591 * \internal
2592 * It issues a read-write ioctl given by
2593 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2594 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002595int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2596 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002597{
2598 unsigned long request;
2599
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002600 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2601 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002602
Keith Packard8b9ab102008-06-13 16:03:22 -07002603 if (drmIoctl(fd, request, data))
Jens Owen3903e5a2002-04-09 21:54:56 +00002604 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002605 return 0;
2606}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002607
Dave Airlied51e1bb2006-11-09 08:55:58 +11002608#define DRM_MAX_FDS 16
2609static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002610 char *BusID;
2611 int fd;
2612 int refcount;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002613 int type;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002614} connection[DRM_MAX_FDS];
2615
2616static int nr_fds = 0;
2617
2618int drmOpenOnce(void *unused,
2619 const char *BusID,
2620 int *newlyopened)
2621{
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002622 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2623}
2624
2625int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2626{
Brianccd7b6e2007-05-29 14:54:00 -06002627 int i;
2628 int fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002629
Brianccd7b6e2007-05-29 14:54:00 -06002630 for (i = 0; i < nr_fds; i++)
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002631 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2632 (connection[i].type == type)) {
Brianccd7b6e2007-05-29 14:54:00 -06002633 connection[i].refcount++;
2634 *newlyopened = 0;
2635 return connection[i].fd;
2636 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002637
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002638 fd = drmOpenWithType(NULL, BusID, type);
Emil Velikovc1cd3d92015-07-14 15:05:18 +01002639 if (fd < 0 || nr_fds == DRM_MAX_FDS)
Brianccd7b6e2007-05-29 14:54:00 -06002640 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002641
Brianccd7b6e2007-05-29 14:54:00 -06002642 connection[nr_fds].BusID = strdup(BusID);
2643 connection[nr_fds].fd = fd;
2644 connection[nr_fds].refcount = 1;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002645 connection[nr_fds].type = type;
Brianccd7b6e2007-05-29 14:54:00 -06002646 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002647
Brianccd7b6e2007-05-29 14:54:00 -06002648 if (0)
2649 fprintf(stderr, "saved connection %d for %s %d\n",
2650 nr_fds, connection[nr_fds].BusID,
2651 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002652
Brianccd7b6e2007-05-29 14:54:00 -06002653 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002654
Brianccd7b6e2007-05-29 14:54:00 -06002655 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002656}
2657
2658void drmCloseOnce(int fd)
2659{
Brianccd7b6e2007-05-29 14:54:00 -06002660 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002661
Brianccd7b6e2007-05-29 14:54:00 -06002662 for (i = 0; i < nr_fds; i++) {
2663 if (fd == connection[i].fd) {
2664 if (--connection[i].refcount == 0) {
2665 drmClose(connection[i].fd);
2666 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002667
Brianccd7b6e2007-05-29 14:54:00 -06002668 if (i < --nr_fds)
2669 connection[i] = connection[nr_fds];
Dave Airlied51e1bb2006-11-09 08:55:58 +11002670
Brianccd7b6e2007-05-29 14:54:00 -06002671 return;
2672 }
2673 }
2674 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002675}
Jesse Barnes731cd552008-12-17 10:09:49 -08002676
2677int drmSetMaster(int fd)
2678{
Daniel Vetterfd387942015-02-11 12:41:04 +01002679 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002680}
2681
2682int drmDropMaster(int fd)
2683{
Daniel Vetterfd387942015-02-11 12:41:04 +01002684 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002685}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002686
2687char *drmGetDeviceNameFromFd(int fd)
2688{
2689 char name[128];
2690 struct stat sbuf;
2691 dev_t d;
2692 int i;
2693
2694 /* The whole drmOpen thing is a fiasco and we need to find a way
2695 * back to just using open(2). For now, however, lets just make
2696 * things worse with even more ad hoc directory walking code to
2697 * discover the device file name. */
2698
2699 fstat(fd, &sbuf);
2700 d = sbuf.st_rdev;
2701
2702 for (i = 0; i < DRM_MAX_MINOR; i++) {
2703 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2704 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2705 break;
2706 }
2707 if (i == DRM_MAX_MINOR)
2708 return NULL;
2709
Adam Jackson0a1ff352010-10-27 18:44:53 -04002710 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002711}
Dave Airliecc0a1452012-07-14 09:52:17 +00002712
Frank Binns1f735782015-02-13 10:51:15 +00002713int drmGetNodeTypeFromFd(int fd)
2714{
2715 struct stat sbuf;
2716 int maj, min, type;
2717
2718 if (fstat(fd, &sbuf))
2719 return -1;
2720
2721 maj = major(sbuf.st_rdev);
2722 min = minor(sbuf.st_rdev);
2723
2724 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2725 errno = EINVAL;
2726 return -1;
2727 }
2728
2729 type = drmGetMinorType(min);
2730 if (type == -1)
2731 errno = ENODEV;
2732 return type;
2733}
2734
Dave Airliecc0a1452012-07-14 09:52:17 +00002735int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2736{
2737 struct drm_prime_handle args;
2738 int ret;
2739
Guillaume Desmottes4bca42f2015-04-29 10:16:22 +02002740 memclear(args);
2741 args.fd = -1;
Dave Airliecc0a1452012-07-14 09:52:17 +00002742 args.handle = handle;
2743 args.flags = flags;
2744 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2745 if (ret)
2746 return ret;
2747
2748 *prime_fd = args.fd;
2749 return 0;
2750}
2751
2752int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2753{
2754 struct drm_prime_handle args;
2755 int ret;
2756
Guillaume Desmottes4bca42f2015-04-29 10:16:22 +02002757 memclear(args);
Dave Airliecc0a1452012-07-14 09:52:17 +00002758 args.fd = prime_fd;
Dave Airliecc0a1452012-07-14 09:52:17 +00002759 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2760 if (ret)
2761 return ret;
2762
2763 *handle = args.handle;
2764 return 0;
2765}
2766
Emil Velikov0ca03a42015-03-07 00:58:39 +00002767static char *drmGetMinorNameForFD(int fd, int type)
2768{
2769#ifdef __linux__
2770 DIR *sysdir;
2771 struct dirent *pent, *ent;
2772 struct stat sbuf;
2773 const char *name = drmGetMinorName(type);
2774 int len;
2775 char dev_name[64], buf[64];
2776 long name_max;
2777 int maj, min;
2778
2779 if (!name)
2780 return NULL;
2781
2782 len = strlen(name);
2783
2784 if (fstat(fd, &sbuf))
2785 return NULL;
2786
2787 maj = major(sbuf.st_rdev);
2788 min = minor(sbuf.st_rdev);
2789
2790 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2791 return NULL;
2792
2793 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2794
2795 sysdir = opendir(buf);
2796 if (!sysdir)
2797 return NULL;
2798
2799 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2800 if (name_max == -1)
2801 goto out_close_dir;
2802
2803 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2804 if (pent == NULL)
2805 goto out_close_dir;
2806
2807 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2808 if (strncmp(ent->d_name, name, len) == 0) {
Mathias Tillman5c42b5e2015-08-24 11:56:13 +08002809 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2810 ent->d_name);
2811
Emil Velikov0ca03a42015-03-07 00:58:39 +00002812 free(pent);
2813 closedir(sysdir);
2814
Emil Velikov0ca03a42015-03-07 00:58:39 +00002815 return strdup(dev_name);
2816 }
2817 }
2818
2819 free(pent);
2820
2821out_close_dir:
2822 closedir(sysdir);
2823#endif
2824 return NULL;
2825}
2826
2827char *drmGetPrimaryDeviceNameFromFd(int fd)
2828{
2829 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2830}
2831
2832char *drmGetRenderDeviceNameFromFd(int fd)
2833{
2834 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2835}
Emil Velikovb556ea12015-08-17 11:09:06 +08002836
2837#ifdef __linux__
2838static int drmParseSubsystemType(const char *str)
2839{
2840 char link[PATH_MAX + 1] = "";
2841 char *name;
2842
2843 if (readlink(str, link, PATH_MAX) < 0)
2844 return -EINVAL;
2845
2846 name = strrchr(link, '/');
2847 if (!name)
2848 return -EINVAL;
2849
2850 name++;
2851
2852 if (strncmp(name, "pci", 3) == 0)
2853 return DRM_BUS_PCI;
2854
2855 return -EINVAL;
2856}
2857
Emil Velikov536e0de2015-09-07 12:37:57 +01002858static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
Emil Velikovb556ea12015-08-17 11:09:06 +08002859{
Emil Velikov536e0de2015-09-07 12:37:57 +01002860 char path[PATH_MAX + 1];
2861 char data[128];
2862 char *str;
Emil Velikovb556ea12015-08-17 11:09:06 +08002863 int domain, bus, dev, func;
Emil Velikov536e0de2015-09-07 12:37:57 +01002864 int fd, ret;
Emil Velikovb556ea12015-08-17 11:09:06 +08002865
Emil Velikov536e0de2015-09-07 12:37:57 +01002866 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
2867 fd = open(path, O_RDONLY);
2868 if (fd < 0)
2869 return -errno;
2870
2871 ret = read(fd, data, sizeof(data));
2872 close(fd);
2873 if (ret < 0)
2874 return -errno;
2875
2876#define TAG "PCI_SLOT_NAME="
2877 str = strstr(data, TAG);
Emil Velikovb556ea12015-08-17 11:09:06 +08002878 if (str == NULL)
2879 return -EINVAL;
2880
Emil Velikov536e0de2015-09-07 12:37:57 +01002881 if (sscanf(str, TAG "%04x:%02x:%02x.%1u",
Emil Velikovb556ea12015-08-17 11:09:06 +08002882 &domain, &bus, &dev, &func) != 4)
2883 return -EINVAL;
Emil Velikov536e0de2015-09-07 12:37:57 +01002884#undef TAG
Emil Velikovb556ea12015-08-17 11:09:06 +08002885
2886 info->domain = domain;
2887 info->bus = bus;
2888 info->dev = dev;
2889 info->func = func;
2890
2891 return 0;
2892}
2893
2894static int drmSameDevice(drmDevicePtr a, drmDevicePtr b)
2895{
2896 if (a->bustype != b->bustype)
2897 return 0;
2898
2899 switch (a->bustype) {
2900 case DRM_BUS_PCI:
2901 if (memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0)
2902 return 1;
2903 default:
2904 break;
2905 }
2906
2907 return 0;
2908}
2909
2910static int drmGetNodeType(const char *name)
2911{
2912 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
2913 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
2914 return DRM_NODE_PRIMARY;
2915
2916 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
2917 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
2918 return DRM_NODE_CONTROL;
2919
2920 if (strncmp(name, DRM_RENDER_MINOR_NAME,
2921 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
2922 return DRM_NODE_RENDER;
2923
2924 return -EINVAL;
2925}
2926
2927static int drmParsePciDeviceInfo(const unsigned char *config,
2928 drmPciDeviceInfoPtr device)
2929{
2930 if (config == NULL)
2931 return -EINVAL;
2932
2933 device->vendor_id = config[0] | (config[1] << 8);
2934 device->device_id = config[2] | (config[3] << 8);
2935 device->revision_id = config[8];
2936 device->subvendor_id = config[44] | (config[45] << 8);
2937 device->subdevice_id = config[46] | (config[47] << 8);
2938
2939 return 0;
2940}
2941
2942static void drmFreeDevice(drmDevicePtr device)
2943{
2944 int i;
2945
2946 if (device == NULL)
2947 return;
2948
2949 if (device->nodes != NULL)
2950 for (i = 0; i < DRM_NODE_MAX; i++)
2951 free(device->nodes[i]);
2952
2953 free(device->nodes);
2954 free(device->businfo.pci);
2955 free(device->deviceinfo.pci);
2956}
2957
2958void drmFreeDevices(drmDevicePtr devices[], int count)
2959{
2960 int i;
2961
2962 if (devices == NULL)
2963 return;
2964
2965 for (i = 0; i < count; i++) {
2966 drmFreeDevice(devices[i]);
2967 free(devices[i]);
2968 devices[i] = NULL;
2969 }
2970}
2971
2972/**
2973 * Get drm devices on the system
2974 *
2975 * \param devices the array of devices with drmDevicePtr elements
2976 * can be NULL to get the device number first
2977 * \param max_devices the maximum number of devices for the array
2978 *
2979 * \return on error - negative error code,
2980 * if devices is NULL - total number of devices available on the system,
2981 * alternatively the number of devices stored in devices[], which is
2982 * capped by the max_devices.
2983 */
2984int drmGetDevices(drmDevicePtr devices[], int max_devices)
2985{
2986 drmDevicePtr devs = NULL;
2987 drmPciBusInfoPtr pcibus = NULL;
2988 drmPciDeviceInfoPtr pcidevice = NULL;
2989 DIR *sysdir = NULL;
2990 struct dirent *dent = NULL;
2991 struct stat sbuf = {0};
2992 char node[PATH_MAX + 1] = "";
2993 char path[PATH_MAX + 1] = "";
Emil Velikovb556ea12015-08-17 11:09:06 +08002994 unsigned char config[64] = "";
2995 int node_type, subsystem_type;
2996 int maj, min;
2997 int fd;
2998 int ret, i = 0, j, node_count, device_count = 0;
2999 int max_count = 16;
3000 int *duplicated = NULL;
3001
3002 devs = calloc(max_count, sizeof(*devs));
3003 if (devs == NULL)
3004 return -ENOMEM;
3005
3006 sysdir = opendir(DRM_DIR_NAME);
3007 if (!sysdir) {
3008 ret = -errno;
3009 goto free_locals;
3010 }
3011
3012 while ((dent = readdir(sysdir))) {
3013 node_type = drmGetNodeType(dent->d_name);
3014 if (node_type < 0)
3015 continue;
3016
3017 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3018 if (stat(node, &sbuf))
3019 continue;
3020
3021 maj = major(sbuf.st_rdev);
3022 min = minor(sbuf.st_rdev);
3023
3024 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3025 continue;
3026
3027 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
3028 maj, min);
3029 subsystem_type = drmParseSubsystemType(path);
3030
3031 if (subsystem_type < 0)
3032 continue;
3033
3034 switch (subsystem_type) {
3035 case DRM_BUS_PCI:
3036 pcibus = calloc(1, sizeof(*pcibus));
3037 if (pcibus == NULL) {
3038 ret = -ENOMEM;
3039 goto free_locals;
3040 }
3041
Emil Velikov536e0de2015-09-07 12:37:57 +01003042 ret = drmParsePciBusInfo(maj, min, pcibus);
Emil Velikovb556ea12015-08-17 11:09:06 +08003043 if (ret)
3044 goto free_locals;
3045
3046 if (i >= max_count) {
3047 max_count += 16;
3048 devs = realloc(devs, max_count * sizeof(*devs));
3049 }
3050
3051 devs[i].businfo.pci = pcibus;
3052 devs[i].bustype = subsystem_type;
3053 devs[i].nodes = calloc(DRM_NODE_MAX, sizeof(char *));
3054 if (devs[i].nodes == NULL) {
3055 ret = -ENOMEM;
3056 goto free_locals;
3057 }
3058 devs[i].nodes[node_type] = strdup(node);
3059 if (devs[i].nodes[node_type] == NULL) {
3060 ret = -ENOMEM;
3061 goto free_locals;
3062 }
3063 devs[i].available_nodes = 1 << node_type;
3064
3065 if (devices != NULL) {
3066 snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config",
3067 dent->d_name);
3068 fd = open(path, O_RDONLY);
3069 if (fd < 0) {
3070 ret = -errno;
3071 goto free_locals;
3072 }
3073 ret = read(fd, config, 64);
3074 if (ret < 0) {
3075 ret = -errno;
3076 close(fd);
3077 goto free_locals;
3078 }
3079
3080 pcidevice = calloc(1, sizeof(*pcidevice));
3081 if (pcidevice == NULL) {
3082 ret = -ENOMEM;
3083 goto free_locals;
3084 }
3085
3086 ret = drmParsePciDeviceInfo(config, pcidevice);
3087 if (ret)
3088 goto free_locals;
3089
3090 devs[i].deviceinfo.pci = pcidevice;
3091 close(fd);
3092 }
3093 break;
3094 default:
3095 fprintf(stderr, "The subsystem type is not supported yet\n");
3096 break;
3097 }
3098 i++;
3099 }
3100
3101 node_count = i;
3102
3103 /* merge duplicated devices with same domain/bus/device/func IDs */
3104 duplicated = calloc(node_count, sizeof(*duplicated));
3105 if (duplicated == NULL) {
3106 ret = -ENOMEM;
3107 goto free_locals;
3108 }
3109
3110 for (i = 0; i < node_count; i++) {
3111 for (j = i+1; j < node_count; j++) {
3112 if (duplicated[i] || duplicated[j])
3113 continue;
3114 if (drmSameDevice(&devs[i], &devs[j])) {
3115 duplicated[j] = 1;
3116 devs[i].available_nodes |= devs[j].available_nodes;
3117 node_type = log2(devs[j].available_nodes);
3118 devs[i].nodes[node_type] = devs[j].nodes[node_type];
3119 free(devs[j].nodes);
3120 free(devs[j].businfo.pci);
3121 free(devs[j].deviceinfo.pci);
3122 }
3123 }
3124 }
3125
3126 for (i = 0; i < node_count; i++) {
3127 if(duplicated[i] == 0) {
3128 if ((devices != NULL) && (device_count < max_devices)) {
3129 devices[device_count] = calloc(1, sizeof(drmDevice));
3130 if (devices[device_count] == NULL) {
3131 ret = -ENOMEM;
3132 break;
3133 }
3134 memcpy(devices[device_count], &devs[i], sizeof(drmDevice));
3135 } else
3136 drmFreeDevice(&devs[i]);
3137 device_count++;
3138 }
3139 }
3140
3141 if (i < node_count) {
3142 drmFreeDevices(devices, device_count);
3143 for ( ; i < node_count; i++)
3144 if(duplicated[i] == 0)
3145 drmFreeDevice(&devs[i]);
3146 } else
3147 ret = device_count;
3148
3149 free(duplicated);
3150 free(devs);
3151 closedir(sysdir);
3152 return ret;
3153
3154free_locals:
3155 for (j = 0; j < i; j++)
3156 drmFreeDevice(&devs[j]);
3157 free(pcidevice);
3158 free(pcibus);
3159 free(devs);
3160 closedir(sysdir);
3161 return ret;
3162}
3163#else
3164void drmFreeDevices(drmDevicePtr devices[], int count)
3165{
3166 (void)devices;
3167 (void)count;
3168}
3169
3170int drmGetDevices(drmDevicePtr devices[], int max_devices)
3171{
3172 (void)devices;
3173 (void)max_devices;
3174 return -EINVAL;
3175}
3176
3177#warning "Missing implementation of drmGetDevices/drmFreeDevices"
3178
3179#endif