blob: 4b6661252e7e95fd06fe4df51fa79414a0edbe48 [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;
Dave Airlie79038752006-11-08 15:08:09 +1100137 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
David Dawes56bd9c22001-07-30 19:59:39 +0000138 {
139 va_start(ap, format);
Dave Airlie79038752006-11-08 15:08:09 +1100140 if (drm_server_info) {
141 drm_server_info->debug_print(format,ap);
142 } else {
Jan Veselycfbe9c92015-03-18 14:17:26 -0400143 drmDebugPrint(format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100144 }
David Dawes56bd9c22001-07-30 19:59:39 +0000145 va_end(ap);
146 }
147}
148
Daryll Straussb3a57661999-12-05 01:19:48 +0000149static void *drmHashTable = NULL; /* Context switch callbacks */
150
Dave Airlie79038752006-11-08 15:08:09 +1100151void *drmGetHashTable(void)
152{
Brianccd7b6e2007-05-29 14:54:00 -0600153 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100154}
Daryll Straussb3a57661999-12-05 01:19:48 +0000155
156void *drmMalloc(int size)
157{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100158 return calloc(1, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000159}
160
161void drmFree(void *pt)
162{
Emil Velikovd0e592d2015-04-28 13:33:46 +0100163 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000164}
165
Keith Packard8b9ab102008-06-13 16:03:22 -0700166/**
167 * Call ioctl, restarting if it is interupted
168 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800169int
Coleman Kane41b83a92008-08-18 17:08:21 -0400170drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700171{
172 int ret;
173
174 do {
175 ret = ioctl(fd, request, arg);
176 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
177 return ret;
178}
Daryll Straussb3a57661999-12-05 01:19:48 +0000179
Daryll Straussb3a57661999-12-05 01:19:48 +0000180static unsigned long drmGetKeyFromFd(int fd)
181{
David Dawesfcc21062001-03-30 17:16:20 +0000182 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000183
184 st.st_rdev = 0;
185 fstat(fd, &st);
186 return st.st_rdev;
187}
188
Dave Airlie79038752006-11-08 15:08:09 +1100189drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000190{
191 unsigned long key = drmGetKeyFromFd(fd);
192 void *value;
193 drmHashEntry *entry;
194
Brianccd7b6e2007-05-29 14:54:00 -0600195 if (!drmHashTable)
196 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000197
198 if (drmHashLookup(drmHashTable, key, &value)) {
199 entry = drmMalloc(sizeof(*entry));
200 entry->fd = fd;
201 entry->f = NULL;
202 entry->tagTable = drmHashCreate();
203 drmHashInsert(drmHashTable, key, entry);
204 } else {
205 entry = value;
206 }
207 return entry;
208}
209
Jose Fonsecad2443b22003-05-27 00:37:33 +0000210/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000211 * Compare two busid strings
212 *
213 * \param first
214 * \param second
215 *
216 * \return 1 if matched.
217 *
218 * \internal
219 * This function compares two bus ID strings. It understands the older
220 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
221 * domain, b is bus, d is device, f is function.
222 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000223static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000224{
225 /* First, check if the IDs are exactly the same */
226 if (strcasecmp(id1, id2) == 0)
227 return 1;
228
229 /* Try to match old/new-style PCI bus IDs. */
230 if (strncasecmp(id1, "pci", 3) == 0) {
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300231 unsigned int o1, b1, d1, f1;
232 unsigned int o2, b2, d2, f2;
Eric Anholt06cb1322003-10-23 02:23:31 +0000233 int ret;
234
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300235 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000236 if (ret != 4) {
237 o1 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300238 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000239 if (ret != 3)
240 return 0;
241 }
242
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300243 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000244 if (ret != 4) {
245 o2 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300246 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000247 if (ret != 3)
248 return 0;
249 }
250
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000251 /* If domains aren't properly supported by the kernel interface,
252 * just ignore them, which sucks less than picking a totally random
253 * card with "open by name"
254 */
255 if (!pci_domain_ok)
256 o1 = o2 = 0;
257
Eric Anholt06cb1322003-10-23 02:23:31 +0000258 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
259 return 0;
260 else
261 return 1;
262 }
263 return 0;
264}
265
266/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300267 * Handles error checking for chown call.
268 *
269 * \param path to file.
270 * \param id of the new owner.
271 * \param id of the new group.
272 *
273 * \return zero if success or -1 if failure.
274 *
275 * \internal
276 * Checks for failure. If failure was caused by signal call chown again.
277 * If any other failure happened then it will output error mesage using
278 * drmMsg() call.
279 */
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500280#if !defined(UDEV)
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300281static int chown_check_return(const char *path, uid_t owner, gid_t group)
282{
283 int rv;
284
285 do {
286 rv = chown(path, owner, group);
287 } while (rv != 0 && errno == EINTR);
288
289 if (rv == 0)
290 return 0;
291
292 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
293 path, errno, strerror(errno));
294 return -1;
295}
Jan Vesely6fc0e4b2015-02-27 12:54:34 -0500296#endif
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300297
298/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000299 * Open the DRM device, creating it if necessary.
300 *
301 * \param dev major and minor numbers of the device.
302 * \param minor minor number of the device.
303 *
304 * \return a file descriptor on success, or a negative value on error.
305 *
306 * \internal
307 * Assembles the device name from \p minor and opens it, creating the device
308 * special file node with the major and minor numbers specified by \p dev and
309 * parent directory if necessary and was called by root.
310 */
Jan Veselyde8532d2014-11-30 12:53:18 -0500311static int drmOpenDevice(dev_t dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000312{
David Dawes9c775d02001-05-14 14:49:58 +0000313 stat_t st;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000314 const char *dev_name;
Rik Faith88dbee52001-02-28 09:27:44 +0000315 char buf[64];
316 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100317 mode_t devmode = DRM_DEV_MODE, serv_mode;
Jan Vesely0706c142015-02-27 12:47:46 -0500318 gid_t serv_group;
319#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000320 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000321 uid_t user = DRM_DEV_UID;
Jan Vesely0706c142015-02-27 12:47:46 -0500322 gid_t group = DRM_DEV_GID;
323#endif
324
Frank Binns0c5aaee2015-01-14 14:07:51 +0000325 switch (type) {
326 case DRM_NODE_PRIMARY:
327 dev_name = DRM_DEV_NAME;
328 break;
329 case DRM_NODE_CONTROL:
330 dev_name = DRM_CONTROL_DEV_NAME;
331 break;
332 case DRM_NODE_RENDER:
333 dev_name = DRM_RENDER_DEV_NAME;
334 break;
335 default:
336 return -EINVAL;
337 };
338
339 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000340 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000341
Dave Airlie79038752006-11-08 15:08:09 +1100342 if (drm_server_info) {
Brianccd7b6e2007-05-29 14:54:00 -0600343 drm_server_info->get_perms(&serv_group, &serv_mode);
344 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
345 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Dave Airlie79038752006-11-08 15:08:09 +1100346 }
Brian Paul569da5a2000-06-08 14:38:22 +0000347
Dave Airlie9101a022008-08-24 16:54:43 +1000348#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000349 if (stat(DRM_DIR_NAME, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600350 if (!isroot)
351 return DRM_ERR_NOT_ROOT;
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000352 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300353 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000354 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000355 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000356
Eric Anholt06cb1322003-10-23 02:23:31 +0000357 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000358 if (stat(buf, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600359 if (!isroot)
360 return DRM_ERR_NOT_ROOT;
Rik Faith88dbee52001-02-28 09:27:44 +0000361 remove(buf);
362 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000363 }
Dave Airlie79038752006-11-08 15:08:09 +1100364
365 if (drm_server_info) {
Jammy Zhou454b1492015-04-27 10:29:55 +0800366 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300367 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600368 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100369 }
Dave Airlie9101a022008-08-24 16:54:43 +1000370#else
371 /* if we modprobed then wait for udev */
372 {
373 int udev_count = 0;
374wait_for_udev:
375 if (stat(DRM_DIR_NAME, &st)) {
376 usleep(20);
377 udev_count++;
378
379 if (udev_count == 50)
380 return -1;
381 goto wait_for_udev;
382 }
383
384 if (stat(buf, &st)) {
385 usleep(20);
386 udev_count++;
387
388 if (udev_count == 50)
389 return -1;
390 goto wait_for_udev;
391 }
392 }
393#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000394
David Dawes56bd9c22001-07-30 19:59:39 +0000395 fd = open(buf, O_RDWR, 0);
396 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
397 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600398 if (fd >= 0)
399 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000400
Dave Airlie39e5e982010-12-07 14:26:09 +1000401#if !defined(UDEV)
Eric Anholt06cb1322003-10-23 02:23:31 +0000402 /* Check if the device node is not what we expect it to be, and recreate it
403 * and try again if so.
404 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000405 if (st.st_rdev != dev) {
Brianccd7b6e2007-05-29 14:54:00 -0600406 if (!isroot)
407 return DRM_ERR_NOT_ROOT;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000408 remove(buf);
409 mknod(buf, S_IFCHR | devmode, dev);
Dave Airlie79038752006-11-08 15:08:09 +1100410 if (drm_server_info) {
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300411 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600412 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100413 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000414 }
415 fd = open(buf, O_RDWR, 0);
416 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
417 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600418 if (fd >= 0)
419 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000420
David Dawes56bd9c22001-07-30 19:59:39 +0000421 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000422 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000423#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000424 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000425}
426
Jose Fonsecad2443b22003-05-27 00:37:33 +0000427
428/**
429 * Open the DRM device
430 *
431 * \param minor device minor number.
432 * \param create allow to create the device if set.
433 *
434 * \return a file descriptor on success, or a negative value on error.
435 *
436 * \internal
437 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
438 * name from \p minor and opens it.
439 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800440static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000441{
442 int fd;
443 char buf[64];
Frank Binns0c5aaee2015-01-14 14:07:51 +0000444 const char *dev_name;
Dave Airliedb85ed22008-02-13 12:20:02 +1000445
Brianccd7b6e2007-05-29 14:54:00 -0600446 if (create)
Jesse Barnes731cd552008-12-17 10:09:49 -0800447 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
Rik Faith88dbee52001-02-28 09:27:44 +0000448
Frank Binns0c5aaee2015-01-14 14:07:51 +0000449 switch (type) {
450 case DRM_NODE_PRIMARY:
451 dev_name = DRM_DEV_NAME;
452 break;
453 case DRM_NODE_CONTROL:
454 dev_name = DRM_CONTROL_DEV_NAME;
455 break;
456 case DRM_NODE_RENDER:
457 dev_name = DRM_RENDER_DEV_NAME;
458 break;
459 default:
460 return -EINVAL;
461 };
462
463 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Brianccd7b6e2007-05-29 14:54:00 -0600464 if ((fd = open(buf, O_RDWR, 0)) >= 0)
465 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000466 return -errno;
467}
468
Brian Paul569da5a2000-06-08 14:38:22 +0000469
Jose Fonsecad2443b22003-05-27 00:37:33 +0000470/**
471 * Determine whether the DRM kernel driver has been loaded.
472 *
473 * \return 1 if the DRM driver is loaded, 0 otherwise.
474 *
475 * \internal
476 * Determine the presence of the kernel driver by attempting to open the 0
477 * minor and get version information. For backward compatibility with older
478 * Linux implementations, /proc/dri is also checked.
479 */
Brian Paul569da5a2000-06-08 14:38:22 +0000480int drmAvailable(void)
481{
Brian Paul569da5a2000-06-08 14:38:22 +0000482 drmVersionPtr version;
483 int retval = 0;
484 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000485
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000486 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000487#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000488 /* Try proc for backward Linux compatibility */
Brianccd7b6e2007-05-29 14:54:00 -0600489 if (!access("/proc/dri/0", R_OK))
490 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000491#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000492 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000493 }
Rik Faith88dbee52001-02-28 09:27:44 +0000494
495 if ((version = drmGetVersion(fd))) {
496 retval = 1;
497 drmFreeVersion(version);
498 }
499 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000500
501 return retval;
502}
503
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800504static int drmGetMinorBase(int type)
505{
506 switch (type) {
507 case DRM_NODE_PRIMARY:
508 return 0;
509 case DRM_NODE_CONTROL:
510 return 64;
511 case DRM_NODE_RENDER:
512 return 128;
513 default:
514 return -1;
515 };
516}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000517
Frank Binns1f735782015-02-13 10:51:15 +0000518static int drmGetMinorType(int minor)
519{
520 int type = minor >> 6;
521
522 if (minor < 0)
523 return -1;
524
525 switch (type) {
526 case DRM_NODE_PRIMARY:
527 case DRM_NODE_CONTROL:
528 case DRM_NODE_RENDER:
529 return type;
530 default:
531 return -1;
532 }
533}
534
Emil Velikov0ca03a42015-03-07 00:58:39 +0000535static const char *drmGetMinorName(int type)
536{
537 switch (type) {
538 case DRM_NODE_PRIMARY:
Jonathan Grayfc083322015-07-21 03:12:56 +1000539 return DRM_PRIMARY_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000540 case DRM_NODE_CONTROL:
Jonathan Grayfc083322015-07-21 03:12:56 +1000541 return DRM_CONTROL_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000542 case DRM_NODE_RENDER:
Jonathan Grayfc083322015-07-21 03:12:56 +1000543 return DRM_RENDER_MINOR_NAME;
Emil Velikov0ca03a42015-03-07 00:58:39 +0000544 default:
545 return NULL;
546 }
547}
548
Jose Fonsecad2443b22003-05-27 00:37:33 +0000549/**
550 * Open the device by bus ID.
551 *
552 * \param busid bus ID.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800553 * \param type device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000554 *
555 * \return a file descriptor on success, or a negative value on error.
556 *
557 * \internal
558 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
559 * comparing the device bus ID with the one supplied.
560 *
561 * \sa drmOpenMinor() and drmGetBusid().
562 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800563static int drmOpenByBusid(const char *busid, int type)
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000564{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000565 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000566 int fd;
567 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000568 drmSetVersion sv;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800569 int base = drmGetMinorBase(type);
570
571 if (base < 0)
572 return -1;
Eric Anholt06cb1322003-10-23 02:23:31 +0000573
574 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800575 for (i = base; i < base + DRM_MAX_MINOR; i++) {
576 fd = drmOpenMinor(i, 1, type);
David Dawes56bd9c22001-07-30 19:59:39 +0000577 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
578 if (fd >= 0) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000579 /* We need to try for 1.4 first for proper PCI domain support
580 * and if that fails, we know the kernel is busted
581 */
Eric Anholt06cb1322003-10-23 02:23:31 +0000582 sv.drm_di_major = 1;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000583 sv.drm_di_minor = 4;
Eric Anholt06cb1322003-10-23 02:23:31 +0000584 sv.drm_dd_major = -1; /* Don't care */
Eric Anholt26462b92005-12-31 11:48:12 +0000585 sv.drm_dd_minor = -1; /* Don't care */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000586 if (drmSetInterfaceVersion(fd, &sv)) {
587#ifndef __alpha__
588 pci_domain_ok = 0;
589#endif
590 sv.drm_di_major = 1;
591 sv.drm_di_minor = 1;
592 sv.drm_dd_major = -1; /* Don't care */
593 sv.drm_dd_minor = -1; /* Don't care */
Thierry Reding303ff262014-04-08 22:33:04 +0200594 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000595 drmSetInterfaceVersion(fd, &sv);
596 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000597 buf = drmGetBusid(fd);
David Dawes56bd9c22001-07-30 19:59:39 +0000598 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000599 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
Rik Faith88dbee52001-02-28 09:27:44 +0000600 drmFreeBusid(buf);
601 return fd;
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000602 }
Brianccd7b6e2007-05-29 14:54:00 -0600603 if (buf)
604 drmFreeBusid(buf);
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000605 close(fd);
606 }
607 }
608 return -1;
609}
610
Jose Fonsecad2443b22003-05-27 00:37:33 +0000611
612/**
613 * Open the device by name.
614 *
615 * \param name driver name.
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800616 * \param type the device node type.
Jose Fonsecad2443b22003-05-27 00:37:33 +0000617 *
618 * \return a file descriptor on success, or a negative value on error.
619 *
620 * \internal
621 * This function opens the first minor number that matches the driver name and
622 * isn't already in use. If it's in use it then it will already have a bus ID
623 * assigned.
624 *
625 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
626 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800627static int drmOpenByName(const char *name, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000628{
Rik Faith88dbee52001-02-28 09:27:44 +0000629 int i;
630 int fd;
631 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000632 char * id;
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800633 int base = drmGetMinorBase(type);
634
635 if (base < 0)
636 return -1;
Dave Airliedb85ed22008-02-13 12:20:02 +1000637
David Dawes56bd9c22001-07-30 19:59:39 +0000638 /*
639 * Open the first minor number that matches the driver name and isn't
640 * already in use. If it's in use it will have a busid assigned already.
641 */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800642 for (i = base; i < base + DRM_MAX_MINOR; i++) {
643 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
Rik Faith88dbee52001-02-28 09:27:44 +0000644 if ((version = drmGetVersion(fd))) {
645 if (!strcmp(version->name, name)) {
646 drmFreeVersion(version);
David Dawes56bd9c22001-07-30 19:59:39 +0000647 id = drmGetBusid(fd);
648 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
649 if (!id || !*id) {
Adam Jackson22e41ef2006-02-20 23:09:00 +0000650 if (id)
David Dawes56bd9c22001-07-30 19:59:39 +0000651 drmFreeBusid(id);
David Dawes56bd9c22001-07-30 19:59:39 +0000652 return fd;
653 } else {
654 drmFreeBusid(id);
655 }
656 } else {
657 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000658 }
Rik Faith88dbee52001-02-28 09:27:44 +0000659 }
David Dawes56bd9c22001-07-30 19:59:39 +0000660 close(fd);
Rik Faith88dbee52001-02-28 09:27:44 +0000661 }
662 }
663
664#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000665 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000666 for (i = 0; i < 8; i++) {
Rik Faith88dbee52001-02-28 09:27:44 +0000667 char proc_name[64], buf[512];
668 char *driver, *pt, *devstring;
669 int retcode;
670
Daryll Strauss0371c291999-12-18 18:34:59 +0000671 sprintf(proc_name, "/proc/dri/%d/name", i);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000672 if ((fd = open(proc_name, 0, 0)) >= 0) {
673 retcode = read(fd, buf, sizeof(buf)-1);
674 close(fd);
675 if (retcode) {
676 buf[retcode-1] = '\0';
677 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
678 ;
Adam Jackson22e41ef2006-02-20 23:09:00 +0000679 if (*pt) { /* Device is next */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000680 *pt = '\0';
681 if (!strcmp(driver, name)) { /* Match */
682 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
683 ;
684 if (*pt) { /* Found busid */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800685 return drmOpenByBusid(++pt, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000686 } else { /* No busid */
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800687 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000688 }
689 }
690 }
691 }
Brian Paul569da5a2000-06-08 14:38:22 +0000692 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000693 }
Rik Faith88dbee52001-02-28 09:27:44 +0000694#endif
695
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000696 return -1;
697}
698
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000699
Jose Fonsecad2443b22003-05-27 00:37:33 +0000700/**
701 * Open the DRM device.
702 *
703 * Looks up the specified name and bus ID, and opens the device found. The
704 * entry in /dev/dri is created if necessary and if called by root.
705 *
706 * \param name driver name. Not referenced if bus ID is supplied.
707 * \param busid bus ID. Zero if not known.
708 *
709 * \return a file descriptor on success, or a negative value on error.
710 *
711 * \internal
712 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
713 * otherwise.
714 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000715int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000716{
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800717 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
718}
719
720/**
721 * Open the DRM device with specified type.
722 *
723 * Looks up the specified name and bus ID, and opens the device found. The
724 * entry in /dev/dri is created if necessary and if called by root.
725 *
726 * \param name driver name. Not referenced if bus ID is supplied.
727 * \param busid bus ID. Zero if not known.
728 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
729 *
730 * \return a file descriptor on success, or a negative value on error.
731 *
732 * \internal
733 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
734 * otherwise.
735 */
736int drmOpenWithType(const char *name, const char *busid, int type)
737{
Dave Airlie79038752006-11-08 15:08:09 +1100738 if (!drmAvailable() && name != NULL && drm_server_info) {
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800739 /* try to load the kernel module */
Dave Airlie79038752006-11-08 15:08:09 +1100740 if (!drm_server_info->load_module(name)) {
Brianccd7b6e2007-05-29 14:54:00 -0600741 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
Eric Anholt06cb1322003-10-23 02:23:31 +0000742 return -1;
743 }
744 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000745
Eric Anholt06cb1322003-10-23 02:23:31 +0000746 if (busid) {
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800747 int fd = drmOpenByBusid(busid, type);
Eric Anholt06cb1322003-10-23 02:23:31 +0000748 if (fd >= 0)
749 return fd;
750 }
Adam Jackson22e41ef2006-02-20 23:09:00 +0000751
Eric Anholt06cb1322003-10-23 02:23:31 +0000752 if (name)
Jammy Zhouf1adc4b2015-02-11 12:40:51 +0800753 return drmOpenByName(name, type);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000754
Eric Anholt06cb1322003-10-23 02:23:31 +0000755 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000756}
757
Jesse Barnes731cd552008-12-17 10:09:49 -0800758int drmOpenControl(int minor)
759{
760 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
761}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000762
Frank Binns0c5aaee2015-01-14 14:07:51 +0000763int drmOpenRender(int minor)
764{
765 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
766}
767
Jose Fonsecad2443b22003-05-27 00:37:33 +0000768/**
769 * Free the version information returned by drmGetVersion().
770 *
771 * \param v pointer to the version information.
772 *
773 * \internal
774 * It frees the memory pointed by \p %v as well as all the non-null strings
775 * pointers in it.
776 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000777void drmFreeVersion(drmVersionPtr v)
778{
Brianccd7b6e2007-05-29 14:54:00 -0600779 if (!v)
780 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100781 drmFree(v->name);
782 drmFree(v->date);
783 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000784 drmFree(v);
785}
786
Jose Fonsecad2443b22003-05-27 00:37:33 +0000787
788/**
789 * Free the non-public version information returned by the kernel.
790 *
791 * \param v pointer to the version information.
792 *
793 * \internal
794 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
795 * the non-null strings pointers in it.
796 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000797static void drmFreeKernelVersion(drm_version_t *v)
798{
Brianccd7b6e2007-05-29 14:54:00 -0600799 if (!v)
800 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100801 drmFree(v->name);
802 drmFree(v->date);
803 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000804 drmFree(v);
805}
806
Jose Fonsecad2443b22003-05-27 00:37:33 +0000807
808/**
809 * Copy version information.
810 *
811 * \param d destination pointer.
812 * \param s source pointer.
813 *
814 * \internal
815 * Used by drmGetVersion() to translate the information returned by the ioctl
816 * interface in a private structure into the public structure counterpart.
817 */
Brian Paul569da5a2000-06-08 14:38:22 +0000818static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000819{
820 d->version_major = s->version_major;
821 d->version_minor = s->version_minor;
822 d->version_patchlevel = s->version_patchlevel;
823 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400824 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000825 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400826 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000827 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400828 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000829}
830
Daryll Straussb3a57661999-12-05 01:19:48 +0000831
Jose Fonsecad2443b22003-05-27 00:37:33 +0000832/**
833 * Query the driver version information.
834 *
835 * \param fd file descriptor.
836 *
837 * \return pointer to a drmVersion structure which should be freed with
838 * drmFreeVersion().
839 *
840 * \note Similar information is available via /proc/dri.
841 *
842 * \internal
843 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
844 * first with zeros to get the string lengths, and then the actually strings.
845 * It also null-terminates them since they might not be already.
846 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000847drmVersionPtr drmGetVersion(int fd)
848{
849 drmVersionPtr retval;
850 drm_version_t *version = drmMalloc(sizeof(*version));
851
Daniel Vetter95f23cf2015-02-11 17:25:30 +0100852 memclear(*version);
Gareth Hughes36047532001-02-15 08:12:14 +0000853
Keith Packard8b9ab102008-06-13 16:03:22 -0700854 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000855 drmFreeKernelVersion(version);
856 return NULL;
857 }
858
Daryll Straussb3a57661999-12-05 01:19:48 +0000859 if (version->name_len)
860 version->name = drmMalloc(version->name_len + 1);
861 if (version->date_len)
862 version->date = drmMalloc(version->date_len + 1);
863 if (version->desc_len)
864 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000865
Keith Packard8b9ab102008-06-13 16:03:22 -0700866 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000867 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
Daryll Straussb3a57661999-12-05 01:19:48 +0000868 drmFreeKernelVersion(version);
869 return NULL;
870 }
871
Adam Jackson22e41ef2006-02-20 23:09:00 +0000872 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000873 if (version->name_len) version->name[version->name_len] = '\0';
874 if (version->date_len) version->date[version->date_len] = '\0';
875 if (version->desc_len) version->desc[version->desc_len] = '\0';
876
Daryll Straussb3a57661999-12-05 01:19:48 +0000877 retval = drmMalloc(sizeof(*retval));
878 drmCopyVersion(retval, version);
879 drmFreeKernelVersion(version);
880 return retval;
881}
882
Jens Owen3903e5a2002-04-09 21:54:56 +0000883
Jose Fonsecad2443b22003-05-27 00:37:33 +0000884/**
885 * Get version information for the DRM user space library.
886 *
887 * This version number is driver independent.
888 *
889 * \param fd file descriptor.
890 *
891 * \return version information.
892 *
893 * \internal
894 * This function allocates and fills a drm_version structure with a hard coded
895 * version number.
896 */
Jens Owen3903e5a2002-04-09 21:54:56 +0000897drmVersionPtr drmGetLibVersion(int fd)
898{
899 drm_version_t *version = drmMalloc(sizeof(*version));
900
901 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100902 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000903 * revision 1.0.x = original DRM interface with no drmGetLibVersion
904 * entry point and many drm<Device> extensions
905 * revision 1.1.x = added drmCommand entry points for device extensions
906 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000907 * revision 1.2.x = added drmSetInterfaceVersion
908 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100909 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000910 */
Dave Airlie79038752006-11-08 15:08:09 +1100911 version->version_major = 1;
912 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000913 version->version_patchlevel = 0;
914
915 return (drmVersionPtr)version;
916}
917
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000918int drmGetCap(int fd, uint64_t capability, uint64_t *value)
919{
Daniel Vetterfd387942015-02-11 12:41:04 +0100920 struct drm_get_cap cap;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000921 int ret;
922
Daniel Vetterfd387942015-02-11 12:41:04 +0100923 memclear(cap);
924 cap.capability = capability;
925
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000926 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
927 if (ret)
928 return ret;
929
930 *value = cap.value;
Dave Airlie3b04c732011-03-04 15:48:31 +1000931 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000932}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000933
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100934int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
935{
Daniel Vetterfd387942015-02-11 12:41:04 +0100936 struct drm_set_client_cap cap;
937
938 memclear(cap);
939 cap.capability = capability;
940 cap.value = value;
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100941
942 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
943}
944
Jose Fonsecad2443b22003-05-27 00:37:33 +0000945/**
946 * Free the bus ID information.
947 *
948 * \param busid bus ID information string as given by drmGetBusid().
949 *
950 * \internal
951 * This function is just frees the memory pointed by \p busid.
952 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000953void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000954{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000955 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000956}
957
Jose Fonsecad2443b22003-05-27 00:37:33 +0000958
959/**
960 * Get the bus ID of the device.
961 *
962 * \param fd file descriptor.
963 *
964 * \return bus ID string.
965 *
966 * \internal
967 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
968 * get the string length and data, passing the arguments in a drm_unique
969 * structure.
970 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000971char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000972{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000973 drm_unique_t u;
974
Daniel Vetterfd387942015-02-11 12:41:04 +0100975 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000976
Keith Packard8b9ab102008-06-13 16:03:22 -0700977 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600978 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000979 u.unique = drmMalloc(u.unique_len + 1);
Keith Packard8b9ab102008-06-13 16:03:22 -0700980 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600981 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000982 u.unique[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +0000983
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000984 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +0000985}
986
Jose Fonsecad2443b22003-05-27 00:37:33 +0000987
988/**
989 * Set the bus ID of the device.
990 *
991 * \param fd file descriptor.
992 * \param busid bus ID string.
993 *
994 * \return zero on success, negative on failure.
995 *
996 * \internal
997 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
998 * the arguments in a drm_unique structure.
999 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001000int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +00001001{
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001002 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +00001003
Daniel Vetterfd387942015-02-11 12:41:04 +01001004 memclear(u);
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001005 u.unique = (char *)busid;
1006 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +00001007
Keith Packard8b9ab102008-06-13 16:03:22 -07001008 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
David Dawes56bd9c22001-07-30 19:59:39 +00001009 return -errno;
1010 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001011 return 0;
1012}
1013
Jon Smirl8696e712004-07-07 04:36:36 +00001014int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001015{
1016 drm_auth_t auth;
1017
Daniel Vetterfd387942015-02-11 12:41:04 +01001018 memclear(auth);
1019
Daryll Straussb3a57661999-12-05 01:19:48 +00001020 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001021 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -06001022 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001023 *magic = auth.magic;
1024 return 0;
1025}
1026
Jon Smirl8696e712004-07-07 04:36:36 +00001027int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +00001028{
1029 drm_auth_t auth;
1030
Daniel Vetterfd387942015-02-11 12:41:04 +01001031 memclear(auth);
Daryll Straussb3a57661999-12-05 01:19:48 +00001032 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -07001033 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -06001034 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001035 return 0;
1036}
1037
Jose Fonsecad2443b22003-05-27 00:37:33 +00001038/**
1039 * Specifies a range of memory that is available for mapping by a
1040 * non-root process.
1041 *
1042 * \param fd file descriptor.
1043 * \param offset usually the physical address. The actual meaning depends of
1044 * the \p type parameter. See below.
1045 * \param size of the memory in bytes.
1046 * \param type type of the memory to be mapped.
1047 * \param flags combination of several flags to modify the function actions.
1048 * \param handle will be set to a value that may be used as the offset
1049 * parameter for mmap().
1050 *
1051 * \return zero on success or a negative value on error.
1052 *
1053 * \par Mapping the frame buffer
1054 * For the frame buffer
1055 * - \p offset will be the physical address of the start of the frame buffer,
1056 * - \p size will be the size of the frame buffer in bytes, and
1057 * - \p type will be DRM_FRAME_BUFFER.
1058 *
1059 * \par
1060 * The area mapped will be uncached. If MTRR support is available in the
1061 * kernel, the frame buffer area will be set to write combining.
1062 *
1063 * \par Mapping the MMIO register area
1064 * For the MMIO register area,
1065 * - \p offset will be the physical address of the start of the register area,
1066 * - \p size will be the size of the register area bytes, and
1067 * - \p type will be DRM_REGISTERS.
1068 * \par
1069 * The area mapped will be uncached.
1070 *
1071 * \par Mapping the SAREA
1072 * For the SAREA,
1073 * - \p offset will be ignored and should be set to zero,
1074 * - \p size will be the desired size of the SAREA in bytes,
1075 * - \p type will be DRM_SHM.
1076 *
1077 * \par
1078 * A shared memory area of the requested size will be created and locked in
1079 * kernel memory. This area may be mapped into client-space by using the handle
1080 * returned.
1081 *
1082 * \note May only be called by root.
1083 *
1084 * \internal
1085 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1086 * the arguments in a drm_map structure.
1087 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001088int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1089 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001090{
1091 drm_map_t map;
1092
Daniel Vetterfd387942015-02-11 12:41:04 +01001093 memclear(map);
Daryll Straussb3a57661999-12-05 01:19:48 +00001094 map.offset = offset;
1095 map.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001096 map.type = type;
1097 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -07001098 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001099 return -errno;
1100 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001101 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001102 return 0;
1103}
1104
Jon Smirl8696e712004-07-07 04:36:36 +00001105int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00001106{
1107 drm_map_t map;
1108
Daniel Vetterfd387942015-02-11 12:41:04 +01001109 memclear(map);
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001110 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001111
Keith Packard8b9ab102008-06-13 16:03:22 -07001112 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001113 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001114 return 0;
1115}
1116
Jose Fonsecad2443b22003-05-27 00:37:33 +00001117/**
1118 * Make buffers available for DMA transfers.
1119 *
1120 * \param fd file descriptor.
1121 * \param count number of buffers.
1122 * \param size size of each buffer.
1123 * \param flags buffer allocation flags.
1124 * \param agp_offset offset in the AGP aperture
1125 *
1126 * \return number of buffers allocated, negative on error.
1127 *
1128 * \internal
1129 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1130 *
1131 * \sa drm_buf_desc.
1132 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001133int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1134 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001135{
1136 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001137
Daniel Vetterfd387942015-02-11 12:41:04 +01001138 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001139 request.count = count;
1140 request.size = size;
Daryll Straussb3a57661999-12-05 01:19:48 +00001141 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001142 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001143
Keith Packard8b9ab102008-06-13 16:03:22 -07001144 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001145 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001146 return request.count;
1147}
1148
1149int drmMarkBufs(int fd, double low, double high)
1150{
1151 drm_buf_info_t info;
1152 int i;
1153
Daniel Vetterfd387942015-02-11 12:41:04 +01001154 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001155
Keith Packard8b9ab102008-06-13 16:03:22 -07001156 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001157 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001158
Brianccd7b6e2007-05-29 14:54:00 -06001159 if (!info.count)
1160 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001161
Daryll Straussb3a57661999-12-05 01:19:48 +00001162 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1163 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001164
Keith Packard8b9ab102008-06-13 16:03:22 -07001165 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001166 int retval = -errno;
1167 drmFree(info.list);
1168 return retval;
1169 }
Gareth Hughes36047532001-02-15 08:12:14 +00001170
Daryll Straussb3a57661999-12-05 01:19:48 +00001171 for (i = 0; i < info.count; i++) {
1172 info.list[i].low_mark = low * info.list[i].count;
1173 info.list[i].high_mark = high * info.list[i].count;
Keith Packard8b9ab102008-06-13 16:03:22 -07001174 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001175 int retval = -errno;
1176 drmFree(info.list);
1177 return retval;
1178 }
1179 }
1180 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001181
Daryll Straussb3a57661999-12-05 01:19:48 +00001182 return 0;
1183}
1184
Jose Fonsecad2443b22003-05-27 00:37:33 +00001185/**
1186 * Free buffers.
1187 *
1188 * \param fd file descriptor.
1189 * \param count number of buffers to free.
1190 * \param list list of buffers to be freed.
1191 *
1192 * \return zero on success, or a negative value on failure.
1193 *
1194 * \note This function is primarily used for debugging.
1195 *
1196 * \internal
1197 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1198 * the arguments in a drm_buf_free structure.
1199 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001200int drmFreeBufs(int fd, int count, int *list)
1201{
1202 drm_buf_free_t request;
1203
Daniel Vetterfd387942015-02-11 12:41:04 +01001204 memclear(request);
Daryll Straussb3a57661999-12-05 01:19:48 +00001205 request.count = count;
1206 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001207 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001208 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001209 return 0;
1210}
1211
Jose Fonsecad2443b22003-05-27 00:37:33 +00001212
1213/**
1214 * Close the device.
1215 *
1216 * \param fd file descriptor.
1217 *
1218 * \internal
1219 * This function closes the file descriptor.
1220 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001221int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001222{
1223 unsigned long key = drmGetKeyFromFd(fd);
1224 drmHashEntry *entry = drmGetEntry(fd);
1225
1226 drmHashDestroy(entry->tagTable);
1227 entry->fd = 0;
1228 entry->f = NULL;
1229 entry->tagTable = NULL;
1230
1231 drmHashDelete(drmHashTable, key);
1232 drmFree(entry);
1233
1234 return close(fd);
1235}
1236
Jose Fonsecad2443b22003-05-27 00:37:33 +00001237
1238/**
1239 * Map a region of memory.
1240 *
1241 * \param fd file descriptor.
1242 * \param handle handle returned by drmAddMap().
1243 * \param size size in bytes. Must match the size used by drmAddMap().
1244 * \param address will contain the user-space virtual address where the mapping
1245 * begins.
1246 *
1247 * \return zero on success, or a negative value on failure.
1248 *
1249 * \internal
1250 * This function is a wrapper for mmap().
1251 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001252int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001253{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001254 static unsigned long pagesize_mask = 0;
1255
Brianccd7b6e2007-05-29 14:54:00 -06001256 if (fd < 0)
1257 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001258
1259 if (!pagesize_mask)
1260 pagesize_mask = getpagesize() - 1;
1261
1262 size = (size + pagesize_mask) & ~pagesize_mask;
1263
Emil Velikovfaf51d52014-09-07 20:03:05 +01001264 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001265 if (*address == MAP_FAILED)
1266 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001267 return 0;
1268}
1269
Jose Fonsecad2443b22003-05-27 00:37:33 +00001270
1271/**
1272 * Unmap mappings obtained with drmMap().
1273 *
1274 * \param address address as given by drmMap().
1275 * \param size size in bytes. Must match the size used by drmMap().
1276 *
1277 * \return zero on success, or a negative value on failure.
1278 *
1279 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001280 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001281 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001282int drmUnmap(drmAddress address, drmSize size)
1283{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001284 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001285}
1286
1287drmBufInfoPtr drmGetBufInfo(int fd)
1288{
1289 drm_buf_info_t info;
1290 drmBufInfoPtr retval;
1291 int i;
1292
Daniel Vetterfd387942015-02-11 12:41:04 +01001293 memclear(info);
Daryll Straussb3a57661999-12-05 01:19:48 +00001294
Keith Packard8b9ab102008-06-13 16:03:22 -07001295 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001296 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001297
1298 if (info.count) {
1299 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1300 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001301
Keith Packard8b9ab102008-06-13 16:03:22 -07001302 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001303 drmFree(info.list);
1304 return NULL;
1305 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001306
Daryll Straussb3a57661999-12-05 01:19:48 +00001307 retval = drmMalloc(sizeof(*retval));
1308 retval->count = info.count;
1309 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1310 for (i = 0; i < info.count; i++) {
1311 retval->list[i].count = info.list[i].count;
1312 retval->list[i].size = info.list[i].size;
1313 retval->list[i].low_mark = info.list[i].low_mark;
1314 retval->list[i].high_mark = info.list[i].high_mark;
1315 }
1316 drmFree(info.list);
1317 return retval;
1318 }
1319 return NULL;
1320}
1321
Jose Fonsecad2443b22003-05-27 00:37:33 +00001322/**
1323 * Map all DMA buffers into client-virtual space.
1324 *
1325 * \param fd file descriptor.
1326 *
1327 * \return a pointer to a ::drmBufMap structure.
1328 *
1329 * \note The client may not use these buffers until obtaining buffer indices
1330 * with drmDMA().
1331 *
1332 * \internal
1333 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1334 * information about the buffers in a drm_buf_map structure into the
1335 * client-visible data structures.
1336 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001337drmBufMapPtr drmMapBufs(int fd)
1338{
1339 drm_buf_map_t bufs;
1340 drmBufMapPtr retval;
1341 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001342
Daniel Vetterfd387942015-02-11 12:41:04 +01001343 memclear(bufs);
Keith Packard8b9ab102008-06-13 16:03:22 -07001344 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Brianccd7b6e2007-05-29 14:54:00 -06001345 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001346
Brianccd7b6e2007-05-29 14:54:00 -06001347 if (!bufs.count)
1348 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001349
Daryll Straussb3a57661999-12-05 01:19:48 +00001350 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1351 return NULL;
1352
Keith Packard8b9ab102008-06-13 16:03:22 -07001353 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001354 drmFree(bufs.list);
1355 return NULL;
1356 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001357
Daryll Straussb3a57661999-12-05 01:19:48 +00001358 retval = drmMalloc(sizeof(*retval));
1359 retval->count = bufs.count;
1360 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1361 for (i = 0; i < bufs.count; i++) {
1362 retval->list[i].idx = bufs.list[i].idx;
1363 retval->list[i].total = bufs.list[i].total;
1364 retval->list[i].used = 0;
1365 retval->list[i].address = bufs.list[i].address;
1366 }
Jon Smirl8696e712004-07-07 04:36:36 +00001367
1368 drmFree(bufs.list);
1369
Daryll Straussb3a57661999-12-05 01:19:48 +00001370 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001371}
1372
Jose Fonsecad2443b22003-05-27 00:37:33 +00001373
1374/**
1375 * Unmap buffers allocated with drmMapBufs().
1376 *
1377 * \return zero on success, or negative value on failure.
1378 *
1379 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001380 * Calls munmap() for every buffer stored in \p bufs and frees the
1381 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001382 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001383int drmUnmapBufs(drmBufMapPtr bufs)
1384{
1385 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001386
Daryll Straussb3a57661999-12-05 01:19:48 +00001387 for (i = 0; i < bufs->count; i++) {
Emil Velikovfaf51d52014-09-07 20:03:05 +01001388 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001389 }
Jon Smirl8696e712004-07-07 04:36:36 +00001390
1391 drmFree(bufs->list);
1392 drmFree(bufs);
1393
Daryll Straussb3a57661999-12-05 01:19:48 +00001394 return 0;
1395}
1396
Jose Fonsecad2443b22003-05-27 00:37:33 +00001397
Gareth Hughes36047532001-02-15 08:12:14 +00001398#define DRM_DMA_RETRY 16
1399
Jose Fonsecad2443b22003-05-27 00:37:33 +00001400/**
1401 * Reserve DMA buffers.
1402 *
1403 * \param fd file descriptor.
1404 * \param request
1405 *
1406 * \return zero on success, or a negative value on failure.
1407 *
1408 * \internal
1409 * Assemble the arguments into a drm_dma structure and keeps issuing the
1410 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1411 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001412int drmDMA(int fd, drmDMAReqPtr request)
1413{
1414 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001415 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001416
Daryll Straussb3a57661999-12-05 01:19:48 +00001417 dma.context = request->context;
1418 dma.send_count = request->send_count;
1419 dma.send_indices = request->send_list;
1420 dma.send_sizes = request->send_sizes;
1421 dma.flags = request->flags;
1422 dma.request_count = request->request_count;
1423 dma.request_size = request->request_size;
1424 dma.request_indices = request->request_list;
1425 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001426 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001427
1428 do {
1429 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1430 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1431
1432 if ( ret == 0 ) {
1433 request->granted_count = dma.granted_count;
1434 return 0;
1435 } else {
1436 return -errno;
1437 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001438}
1439
Jose Fonsecad2443b22003-05-27 00:37:33 +00001440
1441/**
1442 * Obtain heavyweight hardware lock.
1443 *
1444 * \param fd file descriptor.
1445 * \param context context.
1446 * \param flags flags that determine the sate of the hardware when the function
1447 * returns.
1448 *
1449 * \return always zero.
1450 *
1451 * \internal
1452 * This function translates the arguments into a drm_lock structure and issue
1453 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1454 */
Jon Smirl8696e712004-07-07 04:36:36 +00001455int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001456{
1457 drm_lock_t lock;
1458
Daniel Vetterfd387942015-02-11 12:41:04 +01001459 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001460 lock.context = context;
1461 lock.flags = 0;
1462 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1463 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1464 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1465 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1466 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1467 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001468
Keith Packard8b9ab102008-06-13 16:03:22 -07001469 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Daryll Straussb3a57661999-12-05 01:19:48 +00001470 ;
1471 return 0;
1472}
1473
Jose Fonsecad2443b22003-05-27 00:37:33 +00001474/**
1475 * Release the hardware lock.
1476 *
1477 * \param fd file descriptor.
1478 * \param context context.
1479 *
1480 * \return zero on success, or a negative value on failure.
1481 *
1482 * \internal
1483 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1484 * argument in a drm_lock structure.
1485 */
Jon Smirl8696e712004-07-07 04:36:36 +00001486int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001487{
1488 drm_lock_t lock;
1489
Daniel Vetterfd387942015-02-11 12:41:04 +01001490 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001491 lock.context = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001492 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001493}
1494
Adam Jackson22e41ef2006-02-20 23:09:00 +00001495drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001496{
1497 drm_ctx_res_t res;
1498 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001499 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001500 int i;
1501
Daniel Vetterfd387942015-02-11 12:41:04 +01001502 memclear(res);
Keith Packard8b9ab102008-06-13 16:03:22 -07001503 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001504 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001505
Brianccd7b6e2007-05-29 14:54:00 -06001506 if (!res.count)
1507 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001508
Brianccd7b6e2007-05-29 14:54:00 -06001509 if (!(list = drmMalloc(res.count * sizeof(*list))))
1510 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001511 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1512 drmFree(list);
1513 return NULL;
1514 }
1515
1516 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001517 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001518 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001519
Brianccd7b6e2007-05-29 14:54:00 -06001520 for (i = 0; i < res.count; i++)
1521 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001522 drmFree(list);
1523
1524 *count = res.count;
1525 return retval;
1526}
1527
Adam Jackson22e41ef2006-02-20 23:09:00 +00001528void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001529{
1530 drmFree(pt);
1531}
1532
Jose Fonsecad2443b22003-05-27 00:37:33 +00001533/**
1534 * Create context.
1535 *
1536 * Used by the X server during GLXContext initialization. This causes
1537 * per-context kernel-level resources to be allocated.
1538 *
1539 * \param fd file descriptor.
1540 * \param handle is set on success. To be used by the client when requesting DMA
1541 * dispatch with drmDMA().
1542 *
1543 * \return zero on success, or a negative value on failure.
1544 *
1545 * \note May only be called by root.
1546 *
1547 * \internal
1548 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1549 * argument in a drm_ctx structure.
1550 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001551int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001552{
1553 drm_ctx_t ctx;
1554
Daniel Vetterfd387942015-02-11 12:41:04 +01001555 memclear(ctx);
Keith Packard8b9ab102008-06-13 16:03:22 -07001556 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001557 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001558 *handle = ctx.handle;
1559 return 0;
1560}
1561
Jon Smirl8696e712004-07-07 04:36:36 +00001562int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001563{
1564 drm_ctx_t ctx;
1565
Daniel Vetterfd387942015-02-11 12:41:04 +01001566 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001567 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001568 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001569 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001570 return 0;
1571}
1572
Jon Smirl8696e712004-07-07 04:36:36 +00001573int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001574{
1575 drm_ctx_t ctx;
1576
Adam Jackson22e41ef2006-02-20 23:09:00 +00001577 /*
1578 * Context preserving means that no context switches are done between DMA
1579 * buffers from one context and the next. This is suitable for use in the
1580 * X server (which promises to maintain hardware context), or in the
1581 * client-side library when buffers are swapped on behalf of two threads.
1582 */
Daniel Vetterfd387942015-02-11 12:41:04 +01001583 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001584 ctx.handle = context;
Brianccd7b6e2007-05-29 14:54:00 -06001585 if (flags & DRM_CONTEXT_PRESERVED)
1586 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1587 if (flags & DRM_CONTEXT_2DONLY)
1588 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001589 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001590 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001591 return 0;
1592}
1593
Adam Jackson22e41ef2006-02-20 23:09:00 +00001594int drmGetContextFlags(int fd, drm_context_t context,
1595 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001596{
1597 drm_ctx_t ctx;
1598
Daniel Vetterfd387942015-02-11 12:41:04 +01001599 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001600 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001601 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001602 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001603 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001604 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1605 *flags |= DRM_CONTEXT_PRESERVED;
1606 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1607 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001608 return 0;
1609}
Gareth Hughes36047532001-02-15 08:12:14 +00001610
Jose Fonsecad2443b22003-05-27 00:37:33 +00001611/**
1612 * Destroy context.
1613 *
1614 * Free any kernel-level resources allocated with drmCreateContext() associated
1615 * with the context.
1616 *
1617 * \param fd file descriptor.
1618 * \param handle handle given by drmCreateContext().
1619 *
1620 * \return zero on success, or a negative value on failure.
1621 *
1622 * \note May only be called by root.
1623 *
1624 * \internal
1625 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1626 * argument in a drm_ctx structure.
1627 */
Jon Smirl8696e712004-07-07 04:36:36 +00001628int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001629{
1630 drm_ctx_t ctx;
Daniel Vetterfd387942015-02-11 12:41:04 +01001631
1632 memclear(ctx);
Daryll Straussb3a57661999-12-05 01:19:48 +00001633 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001634 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001635 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001636 return 0;
1637}
1638
Adam Jackson22e41ef2006-02-20 23:09:00 +00001639int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001640{
1641 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001642
1643 memclear(draw);
Keith Packard8b9ab102008-06-13 16:03:22 -07001644 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001645 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001646 *handle = draw.handle;
1647 return 0;
1648}
1649
Jon Smirl8696e712004-07-07 04:36:36 +00001650int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001651{
1652 drm_draw_t draw;
Daniel Vetterfd387942015-02-11 12:41:04 +01001653
1654 memclear(draw);
Daryll Straussb3a57661999-12-05 01:19:48 +00001655 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001656 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001657 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001658 return 0;
1659}
1660
Michel Dänzer9810ec22006-08-22 16:40:07 +02001661int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1662 drm_drawable_info_type_t type, unsigned int num,
1663 void *data)
1664{
1665 drm_update_draw_t update;
1666
Daniel Vetterfd387942015-02-11 12:41:04 +01001667 memclear(update);
Michel Dänzer9810ec22006-08-22 16:40:07 +02001668 update.handle = handle;
1669 update.type = type;
1670 update.num = num;
1671 update.data = (unsigned long long)(unsigned long)data;
1672
Keith Packard8b9ab102008-06-13 16:03:22 -07001673 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Brianccd7b6e2007-05-29 14:54:00 -06001674 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001675
1676 return 0;
1677}
1678
Jose Fonsecad2443b22003-05-27 00:37:33 +00001679/**
1680 * Acquire the AGP device.
1681 *
1682 * Must be called before any of the other AGP related calls.
1683 *
1684 * \param fd file descriptor.
1685 *
1686 * \return zero on success, or a negative value on failure.
1687 *
1688 * \internal
1689 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1690 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001691int drmAgpAcquire(int fd)
1692{
Keith Packard8b9ab102008-06-13 16:03:22 -07001693 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001694 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001695 return 0;
1696}
1697
Jose Fonsecad2443b22003-05-27 00:37:33 +00001698
1699/**
1700 * Release the AGP device.
1701 *
1702 * \param fd file descriptor.
1703 *
1704 * \return zero on success, or a negative value on failure.
1705 *
1706 * \internal
1707 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1708 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001709int drmAgpRelease(int fd)
1710{
Keith Packard8b9ab102008-06-13 16:03:22 -07001711 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001712 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001713 return 0;
1714}
1715
Jose Fonsecad2443b22003-05-27 00:37:33 +00001716
1717/**
1718 * Set the AGP mode.
1719 *
1720 * \param fd file descriptor.
1721 * \param mode AGP mode.
1722 *
1723 * \return zero on success, or a negative value on failure.
1724 *
1725 * \internal
1726 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1727 * argument in a drm_agp_mode structure.
1728 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001729int drmAgpEnable(int fd, unsigned long mode)
1730{
1731 drm_agp_mode_t m;
1732
Connor Behan14900552015-03-24 13:53:51 -04001733 memclear(m);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001734 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001735 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Brianccd7b6e2007-05-29 14:54:00 -06001736 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001737 return 0;
1738}
1739
Jose Fonsecad2443b22003-05-27 00:37:33 +00001740
1741/**
1742 * Allocate a chunk of AGP memory.
1743 *
1744 * \param fd file descriptor.
1745 * \param size requested memory size in bytes. Will be rounded to page boundary.
1746 * \param type type of memory to allocate.
1747 * \param address if not zero, will be set to the physical address of the
1748 * allocated memory.
1749 * \param handle on success will be set to a handle of the allocated memory.
1750 *
1751 * \return zero on success, or a negative value on failure.
1752 *
1753 * \internal
1754 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1755 * arguments in a drm_agp_buffer structure.
1756 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001757int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Dave Airlie7ede2092005-11-29 09:50:47 +00001758 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001759{
1760 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001761
Daniel Vetterfd387942015-02-11 12:41:04 +01001762 memclear(b);
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001763 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001764 b.size = size;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001765 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001766 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001767 return -errno;
1768 if (address != 0UL)
1769 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001770 *handle = b.handle;
1771 return 0;
1772}
1773
Jose Fonsecad2443b22003-05-27 00:37:33 +00001774
1775/**
1776 * Free a chunk of AGP memory.
1777 *
1778 * \param fd file descriptor.
1779 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1780 *
1781 * \return zero on success, or a negative value on failure.
1782 *
1783 * \internal
1784 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1785 * argument in a drm_agp_buffer structure.
1786 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001787int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001788{
1789 drm_agp_buffer_t b;
1790
Daniel Vetterfd387942015-02-11 12:41:04 +01001791 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001792 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001793 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001794 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001795 return 0;
1796}
1797
Jose Fonsecad2443b22003-05-27 00:37:33 +00001798
1799/**
1800 * Bind a chunk of AGP memory.
1801 *
1802 * \param fd file descriptor.
1803 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1804 * \param offset offset in bytes. It will round to page boundary.
1805 *
1806 * \return zero on success, or a negative value on failure.
1807 *
1808 * \internal
1809 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1810 * argument in a drm_agp_binding structure.
1811 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001812int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001813{
1814 drm_agp_binding_t b;
1815
Daniel Vetterfd387942015-02-11 12:41:04 +01001816 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001817 b.handle = handle;
1818 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001819 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001820 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001821 return 0;
1822}
1823
Jose Fonsecad2443b22003-05-27 00:37:33 +00001824
1825/**
1826 * Unbind a chunk of AGP memory.
1827 *
1828 * \param fd file descriptor.
1829 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1830 *
1831 * \return zero on success, or a negative value on failure.
1832 *
1833 * \internal
1834 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1835 * the argument in a drm_agp_binding structure.
1836 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001837int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001838{
1839 drm_agp_binding_t b;
1840
Daniel Vetterfd387942015-02-11 12:41:04 +01001841 memclear(b);
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001842 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001843 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001844 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001845 return 0;
1846}
1847
Jose Fonsecad2443b22003-05-27 00:37:33 +00001848
1849/**
1850 * Get AGP driver major version number.
1851 *
1852 * \param fd file descriptor.
1853 *
1854 * \return major version number on success, or a negative value on failure..
1855 *
1856 * \internal
1857 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1858 * necessary information in a drm_agp_info structure.
1859 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001860int drmAgpVersionMajor(int fd)
1861{
1862 drm_agp_info_t i;
1863
Daniel Vetterfd387942015-02-11 12:41:04 +01001864 memclear(i);
1865
Keith Packard8b9ab102008-06-13 16:03:22 -07001866 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001867 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001868 return i.agp_version_major;
1869}
1870
Jose Fonsecad2443b22003-05-27 00:37:33 +00001871
1872/**
1873 * Get AGP driver minor version number.
1874 *
1875 * \param fd file descriptor.
1876 *
1877 * \return minor version number on success, or a negative value on failure.
1878 *
1879 * \internal
1880 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1881 * necessary information in a drm_agp_info structure.
1882 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001883int drmAgpVersionMinor(int fd)
1884{
1885 drm_agp_info_t i;
1886
Daniel Vetterfd387942015-02-11 12:41:04 +01001887 memclear(i);
1888
Keith Packard8b9ab102008-06-13 16:03:22 -07001889 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001890 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001891 return i.agp_version_minor;
1892}
1893
Jose Fonsecad2443b22003-05-27 00:37:33 +00001894
1895/**
1896 * Get AGP mode.
1897 *
1898 * \param fd file descriptor.
1899 *
1900 * \return mode on success, or zero on failure.
1901 *
1902 * \internal
1903 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1904 * necessary information in a drm_agp_info structure.
1905 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001906unsigned long drmAgpGetMode(int fd)
1907{
1908 drm_agp_info_t i;
1909
Daniel Vetterfd387942015-02-11 12:41:04 +01001910 memclear(i);
1911
Keith Packard8b9ab102008-06-13 16:03:22 -07001912 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001913 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001914 return i.mode;
1915}
1916
Jose Fonsecad2443b22003-05-27 00:37:33 +00001917
1918/**
1919 * Get AGP aperture base.
1920 *
1921 * \param fd file descriptor.
1922 *
1923 * \return aperture base on success, zero on failure.
1924 *
1925 * \internal
1926 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1927 * necessary information in a drm_agp_info structure.
1928 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001929unsigned long drmAgpBase(int fd)
1930{
1931 drm_agp_info_t i;
1932
Daniel Vetterfd387942015-02-11 12:41:04 +01001933 memclear(i);
1934
Keith Packard8b9ab102008-06-13 16:03:22 -07001935 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001936 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001937 return i.aperture_base;
1938}
1939
Jose Fonsecad2443b22003-05-27 00:37:33 +00001940
1941/**
1942 * Get AGP aperture size.
1943 *
1944 * \param fd file descriptor.
1945 *
1946 * \return aperture size on success, zero on failure.
1947 *
1948 * \internal
1949 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1950 * necessary information in a drm_agp_info structure.
1951 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001952unsigned long drmAgpSize(int fd)
1953{
1954 drm_agp_info_t i;
1955
Daniel Vetterfd387942015-02-11 12:41:04 +01001956 memclear(i);
1957
Keith Packard8b9ab102008-06-13 16:03:22 -07001958 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001959 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001960 return i.aperture_size;
1961}
1962
Jose Fonsecad2443b22003-05-27 00:37:33 +00001963
1964/**
1965 * Get used AGP memory.
1966 *
1967 * \param fd file descriptor.
1968 *
1969 * \return memory used on success, or zero on failure.
1970 *
1971 * \internal
1972 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1973 * necessary information in a drm_agp_info structure.
1974 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001975unsigned long drmAgpMemoryUsed(int fd)
1976{
1977 drm_agp_info_t i;
1978
Daniel Vetterfd387942015-02-11 12:41:04 +01001979 memclear(i);
1980
Keith Packard8b9ab102008-06-13 16:03:22 -07001981 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001982 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001983 return i.memory_used;
1984}
1985
Jose Fonsecad2443b22003-05-27 00:37:33 +00001986
1987/**
1988 * Get available AGP memory.
1989 *
1990 * \param fd file descriptor.
1991 *
1992 * \return memory available on success, or zero on failure.
1993 *
1994 * \internal
1995 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1996 * necessary information in a drm_agp_info structure.
1997 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001998unsigned long drmAgpMemoryAvail(int fd)
1999{
2000 drm_agp_info_t i;
2001
Daniel Vetterfd387942015-02-11 12:41:04 +01002002 memclear(i);
2003
Keith Packard8b9ab102008-06-13 16:03:22 -07002004 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002005 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002006 return i.memory_allowed;
2007}
2008
Jose Fonsecad2443b22003-05-27 00:37:33 +00002009
2010/**
2011 * Get hardware vendor ID.
2012 *
2013 * \param fd file descriptor.
2014 *
2015 * \return vendor ID on success, or zero on failure.
2016 *
2017 * \internal
2018 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2019 * necessary information in a drm_agp_info structure.
2020 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002021unsigned int drmAgpVendorId(int fd)
2022{
2023 drm_agp_info_t i;
2024
Daniel Vetterfd387942015-02-11 12:41:04 +01002025 memclear(i);
2026
Keith Packard8b9ab102008-06-13 16:03:22 -07002027 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002028 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002029 return i.id_vendor;
2030}
2031
Jose Fonsecad2443b22003-05-27 00:37:33 +00002032
2033/**
2034 * Get hardware device ID.
2035 *
2036 * \param fd file descriptor.
2037 *
2038 * \return zero on success, or zero on failure.
2039 *
2040 * \internal
2041 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2042 * necessary information in a drm_agp_info structure.
2043 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002044unsigned int drmAgpDeviceId(int fd)
2045{
2046 drm_agp_info_t i;
2047
Daniel Vetterfd387942015-02-11 12:41:04 +01002048 memclear(i);
2049
Keith Packard8b9ab102008-06-13 16:03:22 -07002050 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06002051 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00002052 return i.id_device;
2053}
2054
Dave Airlie7ede2092005-11-29 09:50:47 +00002055int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002056{
2057 drm_scatter_gather_t sg;
2058
Daniel Vetterfd387942015-02-11 12:41:04 +01002059 memclear(sg);
2060
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002061 *handle = 0;
2062 sg.size = size;
Keith Packard8b9ab102008-06-13 16:03:22 -07002063 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06002064 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002065 *handle = sg.handle;
2066 return 0;
2067}
2068
Dave Airlie7ede2092005-11-29 09:50:47 +00002069int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002070{
2071 drm_scatter_gather_t sg;
2072
Daniel Vetterfd387942015-02-11 12:41:04 +01002073 memclear(sg);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002074 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07002075 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06002076 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00002077 return 0;
2078}
2079
Jose Fonsecad2443b22003-05-27 00:37:33 +00002080/**
2081 * Wait for VBLANK.
2082 *
2083 * \param fd file descriptor.
2084 * \param vbl pointer to a drmVBlank structure.
2085 *
2086 * \return zero on success, or a negative value on failure.
2087 *
2088 * \internal
2089 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2090 */
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002091int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2092{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002093 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002094 int ret;
2095
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002096 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2097 if (ret < 0) {
Daniel Kurtz1eb28602013-03-28 14:05:40 +08002098 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002099 goto out;
2100 }
2101 timeout.tv_sec++;
2102
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002103 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002104 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00002105 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08002106 if (ret && errno == EINTR) {
2107 clock_gettime(CLOCK_MONOTONIC, &cur);
2108 /* Timeout after 1s */
2109 if (cur.tv_sec > timeout.tv_sec + 1 ||
2110 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2111 timeout.tv_nsec)) {
2112 errno = EBUSY;
2113 ret = -1;
2114 break;
2115 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002116 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002117 } while (ret && errno == EINTR);
2118
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002119out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002120 return ret;
2121}
2122
Daryll Straussb3a57661999-12-05 01:19:48 +00002123int drmError(int err, const char *label)
2124{
2125 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002126 case DRM_ERR_NO_DEVICE:
2127 fprintf(stderr, "%s: no device\n", label);
2128 break;
2129 case DRM_ERR_NO_ACCESS:
2130 fprintf(stderr, "%s: no access\n", label);
2131 break;
2132 case DRM_ERR_NOT_ROOT:
2133 fprintf(stderr, "%s: not root\n", label);
2134 break;
2135 case DRM_ERR_INVALID:
2136 fprintf(stderr, "%s: invalid args\n", label);
2137 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002138 default:
Brianccd7b6e2007-05-29 14:54:00 -06002139 if (err < 0)
2140 err = -err;
Daryll Straussb3a57661999-12-05 01:19:48 +00002141 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2142 break;
2143 }
2144
2145 return 1;
2146}
2147
Jose Fonsecad2443b22003-05-27 00:37:33 +00002148/**
2149 * Install IRQ handler.
2150 *
2151 * \param fd file descriptor.
2152 * \param irq IRQ number.
2153 *
2154 * \return zero on success, or a negative value on failure.
2155 *
2156 * \internal
2157 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2158 * argument in a drm_control structure.
2159 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002160int drmCtlInstHandler(int fd, int irq)
2161{
2162 drm_control_t ctl;
2163
Daniel Vetterfd387942015-02-11 12:41:04 +01002164 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002165 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002166 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002167 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002168 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002169 return 0;
2170}
2171
Jose Fonsecad2443b22003-05-27 00:37:33 +00002172
2173/**
2174 * Uninstall IRQ handler.
2175 *
2176 * \param fd file descriptor.
2177 *
2178 * \return zero on success, or a negative value on failure.
2179 *
2180 * \internal
2181 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2182 * argument in a drm_control structure.
2183 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002184int drmCtlUninstHandler(int fd)
2185{
2186 drm_control_t ctl;
2187
Daniel Vetterfd387942015-02-11 12:41:04 +01002188 memclear(ctl);
Daryll Straussb3a57661999-12-05 01:19:48 +00002189 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002190 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002191 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002192 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002193 return 0;
2194}
2195
2196int drmFinish(int fd, int context, drmLockFlags flags)
2197{
2198 drm_lock_t lock;
2199
Daniel Vetterfd387942015-02-11 12:41:04 +01002200 memclear(lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00002201 lock.context = context;
Daryll Straussb3a57661999-12-05 01:19:48 +00002202 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2203 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2204 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2205 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2206 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2207 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002208 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Brianccd7b6e2007-05-29 14:54:00 -06002209 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002210 return 0;
2211}
2212
Jose Fonsecad2443b22003-05-27 00:37:33 +00002213/**
2214 * Get IRQ from bus ID.
2215 *
2216 * \param fd file descriptor.
2217 * \param busnum bus number.
2218 * \param devnum device number.
2219 * \param funcnum function number.
2220 *
2221 * \return IRQ number on success, or a negative value on failure.
2222 *
2223 * \internal
2224 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2225 * arguments in a drm_irq_busid structure.
2226 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002227int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2228{
2229 drm_irq_busid_t p;
2230
Daniel Vetterfd387942015-02-11 12:41:04 +01002231 memclear(p);
Daryll Straussb3a57661999-12-05 01:19:48 +00002232 p.busnum = busnum;
2233 p.devnum = devnum;
2234 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002235 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Brianccd7b6e2007-05-29 14:54:00 -06002236 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002237 return p.irq;
2238}
2239
Jon Smirl8696e712004-07-07 04:36:36 +00002240int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002241{
2242 drmHashEntry *entry = drmGetEntry(fd);
2243
2244 if (drmHashInsert(entry->tagTable, context, tag)) {
2245 drmHashDelete(entry->tagTable, context);
2246 drmHashInsert(entry->tagTable, context, tag);
2247 }
2248 return 0;
2249}
2250
Jon Smirl8696e712004-07-07 04:36:36 +00002251int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002252{
2253 drmHashEntry *entry = drmGetEntry(fd);
2254
2255 return drmHashDelete(entry->tagTable, context);
2256}
2257
Jon Smirl8696e712004-07-07 04:36:36 +00002258void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002259{
2260 drmHashEntry *entry = drmGetEntry(fd);
2261 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002262
Brianccd7b6e2007-05-29 14:54:00 -06002263 if (drmHashLookup(entry->tagTable, context, &value))
2264 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002265
2266 return value;
2267}
2268
Adam Jackson22e41ef2006-02-20 23:09:00 +00002269int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2270 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002271{
2272 drm_ctx_priv_map_t map;
2273
Daniel Vetterfd387942015-02-11 12:41:04 +01002274 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002275 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002276 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002277
Keith Packard8b9ab102008-06-13 16:03:22 -07002278 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002279 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002280 return 0;
2281}
2282
Adam Jackson22e41ef2006-02-20 23:09:00 +00002283int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2284 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002285{
2286 drm_ctx_priv_map_t map;
2287
Daniel Vetterfd387942015-02-11 12:41:04 +01002288 memclear(map);
Kevin E Martin74e19a42001-03-14 22:22:50 +00002289 map.ctx_id = ctx_id;
2290
Keith Packard8b9ab102008-06-13 16:03:22 -07002291 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002292 return -errno;
2293 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002294 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002295
2296 return 0;
2297}
2298
Jon Smirl8696e712004-07-07 04:36:36 +00002299int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2300 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
Rik Faith88dbee52001-02-28 09:27:44 +00002301 int *mtrr)
2302{
2303 drm_map_t map;
2304
Daniel Vetterfd387942015-02-11 12:41:04 +01002305 memclear(map);
Rik Faith88dbee52001-02-28 09:27:44 +00002306 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002307 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002308 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002309 *offset = map.offset;
2310 *size = map.size;
2311 *type = map.type;
2312 *flags = map.flags;
2313 *handle = (unsigned long)map.handle;
2314 *mtrr = map.mtrr;
2315 return 0;
2316}
2317
2318int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2319 unsigned long *magic, unsigned long *iocs)
2320{
2321 drm_client_t client;
2322
Daniel Vetterfd387942015-02-11 12:41:04 +01002323 memclear(client);
Rik Faith88dbee52001-02-28 09:27:44 +00002324 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002325 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Brianccd7b6e2007-05-29 14:54:00 -06002326 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002327 *auth = client.auth;
2328 *pid = client.pid;
2329 *uid = client.uid;
2330 *magic = client.magic;
2331 *iocs = client.iocs;
2332 return 0;
2333}
2334
2335int drmGetStats(int fd, drmStatsT *stats)
2336{
2337 drm_stats_t s;
Jan Veselyde8532d2014-11-30 12:53:18 -05002338 unsigned i;
Rik Faith88dbee52001-02-28 09:27:44 +00002339
Daniel Vetterfd387942015-02-11 12:41:04 +01002340 memclear(s);
Keith Packard8b9ab102008-06-13 16:03:22 -07002341 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Brianccd7b6e2007-05-29 14:54:00 -06002342 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002343
2344 stats->count = 0;
2345 memset(stats, 0, sizeof(*stats));
2346 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2347 return -1;
2348
2349#define SET_VALUE \
2350 stats->data[i].long_format = "%-20.20s"; \
2351 stats->data[i].rate_format = "%8.8s"; \
2352 stats->data[i].isvalue = 1; \
2353 stats->data[i].verbose = 0
2354
2355#define SET_COUNT \
2356 stats->data[i].long_format = "%-20.20s"; \
2357 stats->data[i].rate_format = "%5.5s"; \
2358 stats->data[i].isvalue = 0; \
2359 stats->data[i].mult_names = "kgm"; \
2360 stats->data[i].mult = 1000; \
2361 stats->data[i].verbose = 0
2362
2363#define SET_BYTE \
2364 stats->data[i].long_format = "%-20.20s"; \
2365 stats->data[i].rate_format = "%5.5s"; \
2366 stats->data[i].isvalue = 0; \
2367 stats->data[i].mult_names = "KGM"; \
2368 stats->data[i].mult = 1024; \
2369 stats->data[i].verbose = 0
2370
2371
2372 stats->count = s.count;
2373 for (i = 0; i < s.count; i++) {
2374 stats->data[i].value = s.data[i].value;
2375 switch (s.data[i].type) {
2376 case _DRM_STAT_LOCK:
2377 stats->data[i].long_name = "Lock";
2378 stats->data[i].rate_name = "Lock";
2379 SET_VALUE;
2380 break;
2381 case _DRM_STAT_OPENS:
2382 stats->data[i].long_name = "Opens";
2383 stats->data[i].rate_name = "O";
2384 SET_COUNT;
2385 stats->data[i].verbose = 1;
2386 break;
2387 case _DRM_STAT_CLOSES:
2388 stats->data[i].long_name = "Closes";
2389 stats->data[i].rate_name = "Lock";
2390 SET_COUNT;
2391 stats->data[i].verbose = 1;
2392 break;
2393 case _DRM_STAT_IOCTLS:
2394 stats->data[i].long_name = "Ioctls";
2395 stats->data[i].rate_name = "Ioc/s";
2396 SET_COUNT;
2397 break;
2398 case _DRM_STAT_LOCKS:
2399 stats->data[i].long_name = "Locks";
2400 stats->data[i].rate_name = "Lck/s";
2401 SET_COUNT;
2402 break;
2403 case _DRM_STAT_UNLOCKS:
2404 stats->data[i].long_name = "Unlocks";
2405 stats->data[i].rate_name = "Unl/s";
2406 SET_COUNT;
2407 break;
2408 case _DRM_STAT_IRQ:
2409 stats->data[i].long_name = "IRQs";
2410 stats->data[i].rate_name = "IRQ/s";
2411 SET_COUNT;
2412 break;
2413 case _DRM_STAT_PRIMARY:
2414 stats->data[i].long_name = "Primary Bytes";
2415 stats->data[i].rate_name = "PB/s";
2416 SET_BYTE;
2417 break;
2418 case _DRM_STAT_SECONDARY:
2419 stats->data[i].long_name = "Secondary Bytes";
2420 stats->data[i].rate_name = "SB/s";
2421 SET_BYTE;
2422 break;
2423 case _DRM_STAT_DMA:
2424 stats->data[i].long_name = "DMA";
2425 stats->data[i].rate_name = "DMA/s";
2426 SET_COUNT;
2427 break;
2428 case _DRM_STAT_SPECIAL:
2429 stats->data[i].long_name = "Special DMA";
2430 stats->data[i].rate_name = "dma/s";
2431 SET_COUNT;
2432 break;
2433 case _DRM_STAT_MISSED:
2434 stats->data[i].long_name = "Miss";
2435 stats->data[i].rate_name = "Ms/s";
2436 SET_COUNT;
2437 break;
2438 case _DRM_STAT_VALUE:
2439 stats->data[i].long_name = "Value";
2440 stats->data[i].rate_name = "Value";
2441 SET_VALUE;
2442 break;
2443 case _DRM_STAT_BYTE:
2444 stats->data[i].long_name = "Bytes";
2445 stats->data[i].rate_name = "B/s";
2446 SET_BYTE;
2447 break;
2448 case _DRM_STAT_COUNT:
2449 default:
2450 stats->data[i].long_name = "Count";
2451 stats->data[i].rate_name = "Cnt/s";
2452 SET_COUNT;
2453 break;
2454 }
2455 }
2456 return 0;
2457}
2458
Jose Fonsecad2443b22003-05-27 00:37:33 +00002459/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002460 * Issue a set-version ioctl.
2461 *
2462 * \param fd file descriptor.
2463 * \param drmCommandIndex command index
2464 * \param data source pointer of the data to be read and written.
2465 * \param size size of the data to be read and written.
2466 *
2467 * \return zero on success, or a negative value on failure.
2468 *
2469 * \internal
2470 * It issues a read-write ioctl given by
2471 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2472 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002473int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002474{
2475 int retcode = 0;
2476 drm_set_version_t sv;
2477
Daniel Vetterfd387942015-02-11 12:41:04 +01002478 memclear(sv);
Eric Anholt06cb1322003-10-23 02:23:31 +00002479 sv.drm_di_major = version->drm_di_major;
2480 sv.drm_di_minor = version->drm_di_minor;
2481 sv.drm_dd_major = version->drm_dd_major;
2482 sv.drm_dd_minor = version->drm_dd_minor;
2483
Keith Packard8b9ab102008-06-13 16:03:22 -07002484 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Eric Anholt06cb1322003-10-23 02:23:31 +00002485 retcode = -errno;
2486 }
2487
2488 version->drm_di_major = sv.drm_di_major;
2489 version->drm_di_minor = sv.drm_di_minor;
2490 version->drm_dd_major = sv.drm_dd_major;
2491 version->drm_dd_minor = sv.drm_dd_minor;
2492
2493 return retcode;
2494}
2495
2496/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002497 * Send a device-specific command.
2498 *
2499 * \param fd file descriptor.
2500 * \param drmCommandIndex command index
2501 *
2502 * \return zero on success, or a negative value on failure.
2503 *
2504 * \internal
2505 * It issues a ioctl given by
2506 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2507 */
Jens Owen3903e5a2002-04-09 21:54:56 +00002508int drmCommandNone(int fd, unsigned long drmCommandIndex)
2509{
Jens Owen3903e5a2002-04-09 21:54:56 +00002510 unsigned long request;
2511
2512 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2513
Daniel Vetterfd387942015-02-11 12:41:04 +01002514 if (drmIoctl(fd, request, NULL)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002515 return -errno;
2516 }
2517 return 0;
2518}
2519
Jose Fonsecad2443b22003-05-27 00:37:33 +00002520
2521/**
2522 * Send a device-specific read command.
2523 *
2524 * \param fd file descriptor.
2525 * \param drmCommandIndex command index
2526 * \param data destination pointer of the data to be read.
2527 * \param size size of the data to be read.
2528 *
2529 * \return zero on success, or a negative value on failure.
2530 *
2531 * \internal
2532 * It issues a read ioctl given by
2533 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2534 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002535int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2536 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002537{
2538 unsigned long request;
2539
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002540 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2541 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002542
Keith Packard8b9ab102008-06-13 16:03:22 -07002543 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002544 return -errno;
2545 }
2546 return 0;
2547}
2548
Jose Fonsecad2443b22003-05-27 00:37:33 +00002549
2550/**
2551 * Send a device-specific write command.
2552 *
2553 * \param fd file descriptor.
2554 * \param drmCommandIndex command index
2555 * \param data source pointer of the data to be written.
2556 * \param size size of the data to be written.
2557 *
2558 * \return zero on success, or a negative value on failure.
2559 *
2560 * \internal
2561 * It issues a write ioctl given by
2562 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2563 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002564int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2565 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002566{
2567 unsigned long request;
2568
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002569 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2570 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002571
Keith Packard8b9ab102008-06-13 16:03:22 -07002572 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002573 return -errno;
2574 }
2575 return 0;
2576}
2577
Jose Fonsecad2443b22003-05-27 00:37:33 +00002578
2579/**
2580 * Send a device-specific read-write command.
2581 *
2582 * \param fd file descriptor.
2583 * \param drmCommandIndex command index
2584 * \param data source pointer of the data to be read and written.
2585 * \param size size of the data to be read and written.
2586 *
2587 * \return zero on success, or a negative value on failure.
2588 *
2589 * \internal
2590 * It issues a read-write ioctl given by
2591 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2592 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002593int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2594 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002595{
2596 unsigned long request;
2597
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002598 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2599 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002600
Keith Packard8b9ab102008-06-13 16:03:22 -07002601 if (drmIoctl(fd, request, data))
Jens Owen3903e5a2002-04-09 21:54:56 +00002602 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002603 return 0;
2604}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002605
Dave Airlied51e1bb2006-11-09 08:55:58 +11002606#define DRM_MAX_FDS 16
2607static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002608 char *BusID;
2609 int fd;
2610 int refcount;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002611 int type;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002612} connection[DRM_MAX_FDS];
2613
2614static int nr_fds = 0;
2615
2616int drmOpenOnce(void *unused,
2617 const char *BusID,
2618 int *newlyopened)
2619{
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002620 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2621}
2622
2623int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2624{
Brianccd7b6e2007-05-29 14:54:00 -06002625 int i;
2626 int fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002627
Brianccd7b6e2007-05-29 14:54:00 -06002628 for (i = 0; i < nr_fds; i++)
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002629 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2630 (connection[i].type == type)) {
Brianccd7b6e2007-05-29 14:54:00 -06002631 connection[i].refcount++;
2632 *newlyopened = 0;
2633 return connection[i].fd;
2634 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002635
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002636 fd = drmOpenWithType(NULL, BusID, type);
Emil Velikovc1cd3d92015-07-14 15:05:18 +01002637 if (fd < 0 || nr_fds == DRM_MAX_FDS)
Brianccd7b6e2007-05-29 14:54:00 -06002638 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002639
Brianccd7b6e2007-05-29 14:54:00 -06002640 connection[nr_fds].BusID = strdup(BusID);
2641 connection[nr_fds].fd = fd;
2642 connection[nr_fds].refcount = 1;
Jammy Zhoudbc8b112015-02-02 18:06:27 +08002643 connection[nr_fds].type = type;
Brianccd7b6e2007-05-29 14:54:00 -06002644 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002645
Brianccd7b6e2007-05-29 14:54:00 -06002646 if (0)
2647 fprintf(stderr, "saved connection %d for %s %d\n",
2648 nr_fds, connection[nr_fds].BusID,
2649 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002650
Brianccd7b6e2007-05-29 14:54:00 -06002651 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002652
Brianccd7b6e2007-05-29 14:54:00 -06002653 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002654}
2655
2656void drmCloseOnce(int fd)
2657{
Brianccd7b6e2007-05-29 14:54:00 -06002658 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002659
Brianccd7b6e2007-05-29 14:54:00 -06002660 for (i = 0; i < nr_fds; i++) {
2661 if (fd == connection[i].fd) {
2662 if (--connection[i].refcount == 0) {
2663 drmClose(connection[i].fd);
2664 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002665
Brianccd7b6e2007-05-29 14:54:00 -06002666 if (i < --nr_fds)
2667 connection[i] = connection[nr_fds];
Dave Airlied51e1bb2006-11-09 08:55:58 +11002668
Brianccd7b6e2007-05-29 14:54:00 -06002669 return;
2670 }
2671 }
2672 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002673}
Jesse Barnes731cd552008-12-17 10:09:49 -08002674
2675int drmSetMaster(int fd)
2676{
Daniel Vetterfd387942015-02-11 12:41:04 +01002677 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002678}
2679
2680int drmDropMaster(int fd)
2681{
Daniel Vetterfd387942015-02-11 12:41:04 +01002682 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Jesse Barnes731cd552008-12-17 10:09:49 -08002683}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002684
2685char *drmGetDeviceNameFromFd(int fd)
2686{
2687 char name[128];
2688 struct stat sbuf;
2689 dev_t d;
2690 int i;
2691
2692 /* The whole drmOpen thing is a fiasco and we need to find a way
2693 * back to just using open(2). For now, however, lets just make
2694 * things worse with even more ad hoc directory walking code to
2695 * discover the device file name. */
2696
2697 fstat(fd, &sbuf);
2698 d = sbuf.st_rdev;
2699
2700 for (i = 0; i < DRM_MAX_MINOR; i++) {
2701 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2702 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2703 break;
2704 }
2705 if (i == DRM_MAX_MINOR)
2706 return NULL;
2707
Adam Jackson0a1ff352010-10-27 18:44:53 -04002708 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002709}
Dave Airliecc0a1452012-07-14 09:52:17 +00002710
Frank Binns1f735782015-02-13 10:51:15 +00002711int drmGetNodeTypeFromFd(int fd)
2712{
2713 struct stat sbuf;
2714 int maj, min, type;
2715
2716 if (fstat(fd, &sbuf))
2717 return -1;
2718
2719 maj = major(sbuf.st_rdev);
2720 min = minor(sbuf.st_rdev);
2721
2722 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2723 errno = EINVAL;
2724 return -1;
2725 }
2726
2727 type = drmGetMinorType(min);
2728 if (type == -1)
2729 errno = ENODEV;
2730 return type;
2731}
2732
Dave Airliecc0a1452012-07-14 09:52:17 +00002733int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2734{
2735 struct drm_prime_handle args;
2736 int ret;
2737
Guillaume Desmottes4bca42f2015-04-29 10:16:22 +02002738 memclear(args);
2739 args.fd = -1;
Dave Airliecc0a1452012-07-14 09:52:17 +00002740 args.handle = handle;
2741 args.flags = flags;
2742 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2743 if (ret)
2744 return ret;
2745
2746 *prime_fd = args.fd;
2747 return 0;
2748}
2749
2750int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2751{
2752 struct drm_prime_handle args;
2753 int ret;
2754
Guillaume Desmottes4bca42f2015-04-29 10:16:22 +02002755 memclear(args);
Dave Airliecc0a1452012-07-14 09:52:17 +00002756 args.fd = prime_fd;
Dave Airliecc0a1452012-07-14 09:52:17 +00002757 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2758 if (ret)
2759 return ret;
2760
2761 *handle = args.handle;
2762 return 0;
2763}
2764
Emil Velikov0ca03a42015-03-07 00:58:39 +00002765static char *drmGetMinorNameForFD(int fd, int type)
2766{
2767#ifdef __linux__
2768 DIR *sysdir;
2769 struct dirent *pent, *ent;
2770 struct stat sbuf;
2771 const char *name = drmGetMinorName(type);
2772 int len;
2773 char dev_name[64], buf[64];
2774 long name_max;
2775 int maj, min;
2776
2777 if (!name)
2778 return NULL;
2779
2780 len = strlen(name);
2781
2782 if (fstat(fd, &sbuf))
2783 return NULL;
2784
2785 maj = major(sbuf.st_rdev);
2786 min = minor(sbuf.st_rdev);
2787
2788 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2789 return NULL;
2790
2791 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2792
2793 sysdir = opendir(buf);
2794 if (!sysdir)
2795 return NULL;
2796
2797 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2798 if (name_max == -1)
2799 goto out_close_dir;
2800
2801 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2802 if (pent == NULL)
2803 goto out_close_dir;
2804
2805 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2806 if (strncmp(ent->d_name, name, len) == 0) {
Mathias Tillman5c42b5e2015-08-24 11:56:13 +08002807 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2808 ent->d_name);
2809
Emil Velikov0ca03a42015-03-07 00:58:39 +00002810 free(pent);
2811 closedir(sysdir);
2812
Emil Velikov0ca03a42015-03-07 00:58:39 +00002813 return strdup(dev_name);
2814 }
2815 }
2816
2817 free(pent);
2818
2819out_close_dir:
2820 closedir(sysdir);
2821#endif
2822 return NULL;
2823}
2824
2825char *drmGetPrimaryDeviceNameFromFd(int fd)
2826{
2827 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2828}
2829
2830char *drmGetRenderDeviceNameFromFd(int fd)
2831{
2832 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2833}
Emil Velikovb556ea12015-08-17 11:09:06 +08002834
2835#ifdef __linux__
2836static int drmParseSubsystemType(const char *str)
2837{
2838 char link[PATH_MAX + 1] = "";
2839 char *name;
2840
2841 if (readlink(str, link, PATH_MAX) < 0)
2842 return -EINVAL;
2843
2844 name = strrchr(link, '/');
2845 if (!name)
2846 return -EINVAL;
2847
2848 name++;
2849
2850 if (strncmp(name, "pci", 3) == 0)
2851 return DRM_BUS_PCI;
2852
2853 return -EINVAL;
2854}
2855
2856static int drmParsePciBusInfo(const char *str, drmPciBusInfoPtr info)
2857{
2858 int domain, bus, dev, func;
2859 char *value;
2860
2861 if (str == NULL)
2862 return -EINVAL;
2863
2864 value = strstr(str, "PCI_SLOT_NAME=");
2865 if (value == NULL)
2866 return -EINVAL;
2867
2868 value += strlen("PCI_SLOT_NAME=");
2869
2870 if (sscanf(value, "%04x:%02x:%02x.%1u",
2871 &domain, &bus, &dev, &func) != 4)
2872 return -EINVAL;
2873
2874 info->domain = domain;
2875 info->bus = bus;
2876 info->dev = dev;
2877 info->func = func;
2878
2879 return 0;
2880}
2881
2882static int drmSameDevice(drmDevicePtr a, drmDevicePtr b)
2883{
2884 if (a->bustype != b->bustype)
2885 return 0;
2886
2887 switch (a->bustype) {
2888 case DRM_BUS_PCI:
2889 if (memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0)
2890 return 1;
2891 default:
2892 break;
2893 }
2894
2895 return 0;
2896}
2897
2898static int drmGetNodeType(const char *name)
2899{
2900 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
2901 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
2902 return DRM_NODE_PRIMARY;
2903
2904 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
2905 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
2906 return DRM_NODE_CONTROL;
2907
2908 if (strncmp(name, DRM_RENDER_MINOR_NAME,
2909 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
2910 return DRM_NODE_RENDER;
2911
2912 return -EINVAL;
2913}
2914
2915static int drmParsePciDeviceInfo(const unsigned char *config,
2916 drmPciDeviceInfoPtr device)
2917{
2918 if (config == NULL)
2919 return -EINVAL;
2920
2921 device->vendor_id = config[0] | (config[1] << 8);
2922 device->device_id = config[2] | (config[3] << 8);
2923 device->revision_id = config[8];
2924 device->subvendor_id = config[44] | (config[45] << 8);
2925 device->subdevice_id = config[46] | (config[47] << 8);
2926
2927 return 0;
2928}
2929
2930static void drmFreeDevice(drmDevicePtr device)
2931{
2932 int i;
2933
2934 if (device == NULL)
2935 return;
2936
2937 if (device->nodes != NULL)
2938 for (i = 0; i < DRM_NODE_MAX; i++)
2939 free(device->nodes[i]);
2940
2941 free(device->nodes);
2942 free(device->businfo.pci);
2943 free(device->deviceinfo.pci);
2944}
2945
2946void drmFreeDevices(drmDevicePtr devices[], int count)
2947{
2948 int i;
2949
2950 if (devices == NULL)
2951 return;
2952
2953 for (i = 0; i < count; i++) {
2954 drmFreeDevice(devices[i]);
2955 free(devices[i]);
2956 devices[i] = NULL;
2957 }
2958}
2959
2960/**
2961 * Get drm devices on the system
2962 *
2963 * \param devices the array of devices with drmDevicePtr elements
2964 * can be NULL to get the device number first
2965 * \param max_devices the maximum number of devices for the array
2966 *
2967 * \return on error - negative error code,
2968 * if devices is NULL - total number of devices available on the system,
2969 * alternatively the number of devices stored in devices[], which is
2970 * capped by the max_devices.
2971 */
2972int drmGetDevices(drmDevicePtr devices[], int max_devices)
2973{
2974 drmDevicePtr devs = NULL;
2975 drmPciBusInfoPtr pcibus = NULL;
2976 drmPciDeviceInfoPtr pcidevice = NULL;
2977 DIR *sysdir = NULL;
2978 struct dirent *dent = NULL;
2979 struct stat sbuf = {0};
2980 char node[PATH_MAX + 1] = "";
2981 char path[PATH_MAX + 1] = "";
2982 char data[128] = "";
2983 unsigned char config[64] = "";
2984 int node_type, subsystem_type;
2985 int maj, min;
2986 int fd;
2987 int ret, i = 0, j, node_count, device_count = 0;
2988 int max_count = 16;
2989 int *duplicated = NULL;
2990
2991 devs = calloc(max_count, sizeof(*devs));
2992 if (devs == NULL)
2993 return -ENOMEM;
2994
2995 sysdir = opendir(DRM_DIR_NAME);
2996 if (!sysdir) {
2997 ret = -errno;
2998 goto free_locals;
2999 }
3000
3001 while ((dent = readdir(sysdir))) {
3002 node_type = drmGetNodeType(dent->d_name);
3003 if (node_type < 0)
3004 continue;
3005
3006 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3007 if (stat(node, &sbuf))
3008 continue;
3009
3010 maj = major(sbuf.st_rdev);
3011 min = minor(sbuf.st_rdev);
3012
3013 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3014 continue;
3015
3016 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
3017 maj, min);
3018 subsystem_type = drmParseSubsystemType(path);
3019
3020 if (subsystem_type < 0)
3021 continue;
3022
3023 switch (subsystem_type) {
3024 case DRM_BUS_PCI:
3025 pcibus = calloc(1, sizeof(*pcibus));
3026 if (pcibus == NULL) {
3027 ret = -ENOMEM;
3028 goto free_locals;
3029 }
3030
3031 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent",
3032 maj, min);
3033 fd = open(path, O_RDONLY);
3034 if (fd < 0) {
3035 ret = -errno;
3036 goto free_locals;
3037 }
3038 ret = read(fd, data, sizeof(data));
3039 if (ret < 0) {
3040 ret = -errno;
3041 close(fd);
3042 goto free_locals;
3043 }
3044
3045 ret = drmParsePciBusInfo(data, pcibus);
3046 close(fd);
3047 if (ret)
3048 goto free_locals;
3049
3050 if (i >= max_count) {
3051 max_count += 16;
3052 devs = realloc(devs, max_count * sizeof(*devs));
3053 }
3054
3055 devs[i].businfo.pci = pcibus;
3056 devs[i].bustype = subsystem_type;
3057 devs[i].nodes = calloc(DRM_NODE_MAX, sizeof(char *));
3058 if (devs[i].nodes == NULL) {
3059 ret = -ENOMEM;
3060 goto free_locals;
3061 }
3062 devs[i].nodes[node_type] = strdup(node);
3063 if (devs[i].nodes[node_type] == NULL) {
3064 ret = -ENOMEM;
3065 goto free_locals;
3066 }
3067 devs[i].available_nodes = 1 << node_type;
3068
3069 if (devices != NULL) {
3070 snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config",
3071 dent->d_name);
3072 fd = open(path, O_RDONLY);
3073 if (fd < 0) {
3074 ret = -errno;
3075 goto free_locals;
3076 }
3077 ret = read(fd, config, 64);
3078 if (ret < 0) {
3079 ret = -errno;
3080 close(fd);
3081 goto free_locals;
3082 }
3083
3084 pcidevice = calloc(1, sizeof(*pcidevice));
3085 if (pcidevice == NULL) {
3086 ret = -ENOMEM;
3087 goto free_locals;
3088 }
3089
3090 ret = drmParsePciDeviceInfo(config, pcidevice);
3091 if (ret)
3092 goto free_locals;
3093
3094 devs[i].deviceinfo.pci = pcidevice;
3095 close(fd);
3096 }
3097 break;
3098 default:
3099 fprintf(stderr, "The subsystem type is not supported yet\n");
3100 break;
3101 }
3102 i++;
3103 }
3104
3105 node_count = i;
3106
3107 /* merge duplicated devices with same domain/bus/device/func IDs */
3108 duplicated = calloc(node_count, sizeof(*duplicated));
3109 if (duplicated == NULL) {
3110 ret = -ENOMEM;
3111 goto free_locals;
3112 }
3113
3114 for (i = 0; i < node_count; i++) {
3115 for (j = i+1; j < node_count; j++) {
3116 if (duplicated[i] || duplicated[j])
3117 continue;
3118 if (drmSameDevice(&devs[i], &devs[j])) {
3119 duplicated[j] = 1;
3120 devs[i].available_nodes |= devs[j].available_nodes;
3121 node_type = log2(devs[j].available_nodes);
3122 devs[i].nodes[node_type] = devs[j].nodes[node_type];
3123 free(devs[j].nodes);
3124 free(devs[j].businfo.pci);
3125 free(devs[j].deviceinfo.pci);
3126 }
3127 }
3128 }
3129
3130 for (i = 0; i < node_count; i++) {
3131 if(duplicated[i] == 0) {
3132 if ((devices != NULL) && (device_count < max_devices)) {
3133 devices[device_count] = calloc(1, sizeof(drmDevice));
3134 if (devices[device_count] == NULL) {
3135 ret = -ENOMEM;
3136 break;
3137 }
3138 memcpy(devices[device_count], &devs[i], sizeof(drmDevice));
3139 } else
3140 drmFreeDevice(&devs[i]);
3141 device_count++;
3142 }
3143 }
3144
3145 if (i < node_count) {
3146 drmFreeDevices(devices, device_count);
3147 for ( ; i < node_count; i++)
3148 if(duplicated[i] == 0)
3149 drmFreeDevice(&devs[i]);
3150 } else
3151 ret = device_count;
3152
3153 free(duplicated);
3154 free(devs);
3155 closedir(sysdir);
3156 return ret;
3157
3158free_locals:
3159 for (j = 0; j < i; j++)
3160 drmFreeDevice(&devs[j]);
3161 free(pcidevice);
3162 free(pcibus);
3163 free(devs);
3164 closedir(sysdir);
3165 return ret;
3166}
3167#else
3168void drmFreeDevices(drmDevicePtr devices[], int count)
3169{
3170 (void)devices;
3171 (void)count;
3172}
3173
3174int drmGetDevices(drmDevicePtr devices[], int max_devices)
3175{
3176 (void)devices;
3177 (void)max_devices;
3178 return -EINVAL;
3179}
3180
3181#warning "Missing implementation of drmGetDevices/drmFreeDevices"
3182
3183#endif