blob: 345325a37f28d89eafc31e52f4f3d57c04e04729 [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>
43#include <fcntl.h>
44#include <errno.h>
45#include <signal.h>
Jesse Barnesf4f76a62009-01-07 10:18:08 -080046#include <time.h>
Dave Airlie79038752006-11-08 15:08:09 +110047#include <sys/types.h>
48#include <sys/stat.h>
49#define stat_t struct stat
50#include <sys/ioctl.h>
Dave Airlie79038752006-11-08 15:08:09 +110051#include <sys/time.h>
52#include <stdarg.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000053
54/* Not all systems have MAP_FAILED defined */
55#ifndef MAP_FAILED
56#define MAP_FAILED ((void *)-1)
57#endif
58
Daryll Straussb3a57661999-12-05 01:19:48 +000059#include "xf86drm.h"
Emil Velikovfaf51d52014-09-07 20:03:05 +010060#include "libdrm.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000061
Hasso Tepper27c37852008-04-07 15:27:43 +030062#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Eric Anholtcfa778a2003-02-21 23:23:09 +000063#define DRM_MAJOR 145
Rik Faith88dbee52001-02-28 09:27:44 +000064#endif
65
Eric Anholtcfa778a2003-02-21 23:23:09 +000066#ifdef __NetBSD__
67#define DRM_MAJOR 34
68#endif
69
Alan Hourihaneb0a92852003-09-24 14:39:25 +000070# ifdef __OpenBSD__
71# define DRM_MAJOR 81
72# endif
73
Eric Anholtcfa778a2003-02-21 23:23:09 +000074#ifndef DRM_MAJOR
75#define DRM_MAJOR 226 /* Linux */
Rik Faith88dbee52001-02-28 09:27:44 +000076#endif
77
Adam Jackson22e41ef2006-02-20 23:09:00 +000078/*
79 * This definition needs to be changed on some systems if dev_t is a structure.
80 * If there is a header file we can get it from, there would be best.
81 */
Brian Paul569da5a2000-06-08 14:38:22 +000082#ifndef makedev
Brian Paul569da5a2000-06-08 14:38:22 +000083#define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
84#endif
85
David Dawes56bd9c22001-07-30 19:59:39 +000086#define DRM_MSG_VERBOSITY 3
87
Jesse Barnes731cd552008-12-17 10:09:49 -080088#define DRM_NODE_CONTROL 0
Frank Binnsad8bbfd2015-01-14 14:07:50 +000089#define DRM_NODE_PRIMARY 1
Frank Binns0c5aaee2015-01-14 14:07:51 +000090#define DRM_NODE_RENDER 2
Jesse Barnes731cd552008-12-17 10:09:49 -080091
Dave Airlie79038752006-11-08 15:08:09 +110092static drmServerInfoPtr drm_server_info;
93
94void drmSetServerInfo(drmServerInfoPtr info)
95{
Brianccd7b6e2007-05-29 14:54:00 -060096 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +110097}
98
Jose Fonsecad2443b22003-05-27 00:37:33 +000099/**
100 * Output a message to stderr.
101 *
102 * \param format printf() like format string.
103 *
104 * \internal
105 * This function is a wrapper around vfprintf().
106 */
Dave Airlie79038752006-11-08 15:08:09 +1100107
Thierry Reding44b08c02014-01-22 12:06:51 +0100108static int DRM_PRINTFLIKE(1, 0)
109drmDebugPrint(const char *format, va_list ap)
Dave Airlie79038752006-11-08 15:08:09 +1100110{
Brianccd7b6e2007-05-29 14:54:00 -0600111 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100112}
113
Thierry Reding44b08c02014-01-22 12:06:51 +0100114typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
115 va_list ap);
116
117static debug_msg_func_t drm_debug_print = drmDebugPrint;
Dave Airlie79038752006-11-08 15:08:09 +1100118
Eric Anholtc4857422008-06-03 10:20:49 -0700119void
David Dawes56bd9c22001-07-30 19:59:39 +0000120drmMsg(const char *format, ...)
121{
122 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000123 const char *env;
Dave Airlie79038752006-11-08 15:08:09 +1100124 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
David Dawes56bd9c22001-07-30 19:59:39 +0000125 {
126 va_start(ap, format);
Dave Airlie79038752006-11-08 15:08:09 +1100127 if (drm_server_info) {
128 drm_server_info->debug_print(format,ap);
129 } else {
130 drm_debug_print(format, ap);
131 }
David Dawes56bd9c22001-07-30 19:59:39 +0000132 va_end(ap);
133 }
134}
135
Dave Airlie79038752006-11-08 15:08:09 +1100136void
Thierry Reding44b08c02014-01-22 12:06:51 +0100137drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
Dave Airlie79038752006-11-08 15:08:09 +1100138{
Brianccd7b6e2007-05-29 14:54:00 -0600139 drm_debug_print = debug_msg_ptr;
Dave Airlie79038752006-11-08 15:08:09 +1100140}
141
Daryll Straussb3a57661999-12-05 01:19:48 +0000142static void *drmHashTable = NULL; /* Context switch callbacks */
143
Dave Airlie79038752006-11-08 15:08:09 +1100144void *drmGetHashTable(void)
145{
Brianccd7b6e2007-05-29 14:54:00 -0600146 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100147}
Daryll Straussb3a57661999-12-05 01:19:48 +0000148
149void *drmMalloc(int size)
150{
151 void *pt;
Brianccd7b6e2007-05-29 14:54:00 -0600152 if ((pt = malloc(size)))
153 memset(pt, 0, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000154 return pt;
155}
156
157void drmFree(void *pt)
158{
Brianccd7b6e2007-05-29 14:54:00 -0600159 if (pt)
160 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000161}
162
Keith Packard8b9ab102008-06-13 16:03:22 -0700163/**
164 * Call ioctl, restarting if it is interupted
165 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800166int
Coleman Kane41b83a92008-08-18 17:08:21 -0400167drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700168{
169 int ret;
170
171 do {
172 ret = ioctl(fd, request, arg);
173 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
174 return ret;
175}
Daryll Straussb3a57661999-12-05 01:19:48 +0000176
Daryll Straussb3a57661999-12-05 01:19:48 +0000177static unsigned long drmGetKeyFromFd(int fd)
178{
David Dawesfcc21062001-03-30 17:16:20 +0000179 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000180
181 st.st_rdev = 0;
182 fstat(fd, &st);
183 return st.st_rdev;
184}
185
Dave Airlie79038752006-11-08 15:08:09 +1100186drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000187{
188 unsigned long key = drmGetKeyFromFd(fd);
189 void *value;
190 drmHashEntry *entry;
191
Brianccd7b6e2007-05-29 14:54:00 -0600192 if (!drmHashTable)
193 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000194
195 if (drmHashLookup(drmHashTable, key, &value)) {
196 entry = drmMalloc(sizeof(*entry));
197 entry->fd = fd;
198 entry->f = NULL;
199 entry->tagTable = drmHashCreate();
200 drmHashInsert(drmHashTable, key, entry);
201 } else {
202 entry = value;
203 }
204 return entry;
205}
206
Jose Fonsecad2443b22003-05-27 00:37:33 +0000207/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000208 * Compare two busid strings
209 *
210 * \param first
211 * \param second
212 *
213 * \return 1 if matched.
214 *
215 * \internal
216 * This function compares two bus ID strings. It understands the older
217 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
218 * domain, b is bus, d is device, f is function.
219 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000220static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000221{
222 /* First, check if the IDs are exactly the same */
223 if (strcasecmp(id1, id2) == 0)
224 return 1;
225
226 /* Try to match old/new-style PCI bus IDs. */
227 if (strncasecmp(id1, "pci", 3) == 0) {
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300228 unsigned int o1, b1, d1, f1;
229 unsigned int o2, b2, d2, f2;
Eric Anholt06cb1322003-10-23 02:23:31 +0000230 int ret;
231
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300232 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000233 if (ret != 4) {
234 o1 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300235 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000236 if (ret != 3)
237 return 0;
238 }
239
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300240 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000241 if (ret != 4) {
242 o2 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300243 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000244 if (ret != 3)
245 return 0;
246 }
247
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000248 /* If domains aren't properly supported by the kernel interface,
249 * just ignore them, which sucks less than picking a totally random
250 * card with "open by name"
251 */
252 if (!pci_domain_ok)
253 o1 = o2 = 0;
254
Eric Anholt06cb1322003-10-23 02:23:31 +0000255 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
256 return 0;
257 else
258 return 1;
259 }
260 return 0;
261}
262
263/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300264 * Handles error checking for chown call.
265 *
266 * \param path to file.
267 * \param id of the new owner.
268 * \param id of the new group.
269 *
270 * \return zero if success or -1 if failure.
271 *
272 * \internal
273 * Checks for failure. If failure was caused by signal call chown again.
274 * If any other failure happened then it will output error mesage using
275 * drmMsg() call.
276 */
277static int chown_check_return(const char *path, uid_t owner, gid_t group)
278{
279 int rv;
280
281 do {
282 rv = chown(path, owner, group);
283 } while (rv != 0 && errno == EINTR);
284
285 if (rv == 0)
286 return 0;
287
288 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
289 path, errno, strerror(errno));
290 return -1;
291}
292
293/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000294 * Open the DRM device, creating it if necessary.
295 *
296 * \param dev major and minor numbers of the device.
297 * \param minor minor number of the device.
298 *
299 * \return a file descriptor on success, or a negative value on error.
300 *
301 * \internal
302 * Assembles the device name from \p minor and opens it, creating the device
303 * special file node with the major and minor numbers specified by \p dev and
304 * parent directory if necessary and was called by root.
305 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800306static int drmOpenDevice(long dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000307{
David Dawes9c775d02001-05-14 14:49:58 +0000308 stat_t st;
Frank Binns0c5aaee2015-01-14 14:07:51 +0000309 const char *dev_name;
Rik Faith88dbee52001-02-28 09:27:44 +0000310 char buf[64];
311 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100312 mode_t devmode = DRM_DEV_MODE, serv_mode;
Rik Faith88dbee52001-02-28 09:27:44 +0000313 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000314 uid_t user = DRM_DEV_UID;
Dave Airlie79038752006-11-08 15:08:09 +1100315 gid_t group = DRM_DEV_GID, serv_group;
316
Frank Binns0c5aaee2015-01-14 14:07:51 +0000317 switch (type) {
318 case DRM_NODE_PRIMARY:
319 dev_name = DRM_DEV_NAME;
320 break;
321 case DRM_NODE_CONTROL:
322 dev_name = DRM_CONTROL_DEV_NAME;
323 break;
324 case DRM_NODE_RENDER:
325 dev_name = DRM_RENDER_DEV_NAME;
326 break;
327 default:
328 return -EINVAL;
329 };
330
331 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000332 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000333
Dave Airlie79038752006-11-08 15:08:09 +1100334 if (drm_server_info) {
Brianccd7b6e2007-05-29 14:54:00 -0600335 drm_server_info->get_perms(&serv_group, &serv_mode);
336 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
337 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
338 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
Dave Airlie79038752006-11-08 15:08:09 +1100339 }
Brian Paul569da5a2000-06-08 14:38:22 +0000340
Dave Airlie9101a022008-08-24 16:54:43 +1000341#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000342 if (stat(DRM_DIR_NAME, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600343 if (!isroot)
344 return DRM_ERR_NOT_ROOT;
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000345 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300346 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000347 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000348 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000349
Eric Anholt06cb1322003-10-23 02:23:31 +0000350 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000351 if (stat(buf, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600352 if (!isroot)
353 return DRM_ERR_NOT_ROOT;
Rik Faith88dbee52001-02-28 09:27:44 +0000354 remove(buf);
355 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000356 }
Dave Airlie79038752006-11-08 15:08:09 +1100357
358 if (drm_server_info) {
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300359 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600360 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100361 }
Dave Airlie9101a022008-08-24 16:54:43 +1000362#else
363 /* if we modprobed then wait for udev */
364 {
365 int udev_count = 0;
366wait_for_udev:
367 if (stat(DRM_DIR_NAME, &st)) {
368 usleep(20);
369 udev_count++;
370
371 if (udev_count == 50)
372 return -1;
373 goto wait_for_udev;
374 }
375
376 if (stat(buf, &st)) {
377 usleep(20);
378 udev_count++;
379
380 if (udev_count == 50)
381 return -1;
382 goto wait_for_udev;
383 }
384 }
385#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000386
David Dawes56bd9c22001-07-30 19:59:39 +0000387 fd = open(buf, O_RDWR, 0);
388 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
389 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600390 if (fd >= 0)
391 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000392
Dave Airlie39e5e982010-12-07 14:26:09 +1000393#if !defined(UDEV)
Eric Anholt06cb1322003-10-23 02:23:31 +0000394 /* Check if the device node is not what we expect it to be, and recreate it
395 * and try again if so.
396 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000397 if (st.st_rdev != dev) {
Brianccd7b6e2007-05-29 14:54:00 -0600398 if (!isroot)
399 return DRM_ERR_NOT_ROOT;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000400 remove(buf);
401 mknod(buf, S_IFCHR | devmode, dev);
Dave Airlie79038752006-11-08 15:08:09 +1100402 if (drm_server_info) {
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300403 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600404 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100405 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000406 }
407 fd = open(buf, O_RDWR, 0);
408 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
409 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600410 if (fd >= 0)
411 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000412
David Dawes56bd9c22001-07-30 19:59:39 +0000413 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000414 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000415#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000416 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000417}
418
Jose Fonsecad2443b22003-05-27 00:37:33 +0000419
420/**
421 * Open the DRM device
422 *
423 * \param minor device minor number.
424 * \param create allow to create the device if set.
425 *
426 * \return a file descriptor on success, or a negative value on error.
427 *
428 * \internal
429 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
430 * name from \p minor and opens it.
431 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800432static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000433{
434 int fd;
435 char buf[64];
Frank Binns0c5aaee2015-01-14 14:07:51 +0000436 const char *dev_name;
Dave Airliedb85ed22008-02-13 12:20:02 +1000437
Brianccd7b6e2007-05-29 14:54:00 -0600438 if (create)
Jesse Barnes731cd552008-12-17 10:09:49 -0800439 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
Rik Faith88dbee52001-02-28 09:27:44 +0000440
Frank Binns0c5aaee2015-01-14 14:07:51 +0000441 switch (type) {
442 case DRM_NODE_PRIMARY:
443 dev_name = DRM_DEV_NAME;
444 break;
445 case DRM_NODE_CONTROL:
446 dev_name = DRM_CONTROL_DEV_NAME;
447 break;
448 case DRM_NODE_RENDER:
449 dev_name = DRM_RENDER_DEV_NAME;
450 break;
451 default:
452 return -EINVAL;
453 };
454
455 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Brianccd7b6e2007-05-29 14:54:00 -0600456 if ((fd = open(buf, O_RDWR, 0)) >= 0)
457 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000458 return -errno;
459}
460
Brian Paul569da5a2000-06-08 14:38:22 +0000461
Jose Fonsecad2443b22003-05-27 00:37:33 +0000462/**
463 * Determine whether the DRM kernel driver has been loaded.
464 *
465 * \return 1 if the DRM driver is loaded, 0 otherwise.
466 *
467 * \internal
468 * Determine the presence of the kernel driver by attempting to open the 0
469 * minor and get version information. For backward compatibility with older
470 * Linux implementations, /proc/dri is also checked.
471 */
Brian Paul569da5a2000-06-08 14:38:22 +0000472int drmAvailable(void)
473{
Brian Paul569da5a2000-06-08 14:38:22 +0000474 drmVersionPtr version;
475 int retval = 0;
476 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000477
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000478 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000479#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000480 /* Try proc for backward Linux compatibility */
Brianccd7b6e2007-05-29 14:54:00 -0600481 if (!access("/proc/dri/0", R_OK))
482 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000483#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000484 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000485 }
Rik Faith88dbee52001-02-28 09:27:44 +0000486
487 if ((version = drmGetVersion(fd))) {
488 retval = 1;
489 drmFreeVersion(version);
490 }
491 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000492
493 return retval;
494}
495
Jose Fonsecad2443b22003-05-27 00:37:33 +0000496
497/**
498 * Open the device by bus ID.
499 *
500 * \param busid bus ID.
501 *
502 * \return a file descriptor on success, or a negative value on error.
503 *
504 * \internal
505 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
506 * comparing the device bus ID with the one supplied.
507 *
508 * \sa drmOpenMinor() and drmGetBusid().
509 */
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000510static int drmOpenByBusid(const char *busid)
511{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000512 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000513 int fd;
514 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000515 drmSetVersion sv;
516
517 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Dave Airliedb85ed22008-02-13 12:20:02 +1000518 for (i = 0; i < DRM_MAX_MINOR; i++) {
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000519 fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY);
David Dawes56bd9c22001-07-30 19:59:39 +0000520 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
521 if (fd >= 0) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000522 /* We need to try for 1.4 first for proper PCI domain support
523 * and if that fails, we know the kernel is busted
524 */
Eric Anholt06cb1322003-10-23 02:23:31 +0000525 sv.drm_di_major = 1;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000526 sv.drm_di_minor = 4;
Eric Anholt06cb1322003-10-23 02:23:31 +0000527 sv.drm_dd_major = -1; /* Don't care */
Eric Anholt26462b92005-12-31 11:48:12 +0000528 sv.drm_dd_minor = -1; /* Don't care */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000529 if (drmSetInterfaceVersion(fd, &sv)) {
530#ifndef __alpha__
531 pci_domain_ok = 0;
532#endif
533 sv.drm_di_major = 1;
534 sv.drm_di_minor = 1;
535 sv.drm_dd_major = -1; /* Don't care */
536 sv.drm_dd_minor = -1; /* Don't care */
Thierry Reding303ff262014-04-08 22:33:04 +0200537 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000538 drmSetInterfaceVersion(fd, &sv);
539 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000540 buf = drmGetBusid(fd);
David Dawes56bd9c22001-07-30 19:59:39 +0000541 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000542 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
Rik Faith88dbee52001-02-28 09:27:44 +0000543 drmFreeBusid(buf);
544 return fd;
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000545 }
Brianccd7b6e2007-05-29 14:54:00 -0600546 if (buf)
547 drmFreeBusid(buf);
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000548 close(fd);
549 }
550 }
551 return -1;
552}
553
Jose Fonsecad2443b22003-05-27 00:37:33 +0000554
555/**
556 * Open the device by name.
557 *
558 * \param name driver name.
559 *
560 * \return a file descriptor on success, or a negative value on error.
561 *
562 * \internal
563 * This function opens the first minor number that matches the driver name and
564 * isn't already in use. If it's in use it then it will already have a bus ID
565 * assigned.
566 *
567 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
568 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000569static int drmOpenByName(const char *name)
Daryll Straussb3a57661999-12-05 01:19:48 +0000570{
Rik Faith88dbee52001-02-28 09:27:44 +0000571 int i;
572 int fd;
573 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000574 char * id;
Dave Airliedb85ed22008-02-13 12:20:02 +1000575
David Dawes56bd9c22001-07-30 19:59:39 +0000576 /*
577 * Open the first minor number that matches the driver name and isn't
578 * already in use. If it's in use it will have a busid assigned already.
579 */
Dave Airliedb85ed22008-02-13 12:20:02 +1000580 for (i = 0; i < DRM_MAX_MINOR; i++) {
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000581 if ((fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY)) >= 0) {
Rik Faith88dbee52001-02-28 09:27:44 +0000582 if ((version = drmGetVersion(fd))) {
583 if (!strcmp(version->name, name)) {
584 drmFreeVersion(version);
David Dawes56bd9c22001-07-30 19:59:39 +0000585 id = drmGetBusid(fd);
586 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
587 if (!id || !*id) {
Adam Jackson22e41ef2006-02-20 23:09:00 +0000588 if (id)
David Dawes56bd9c22001-07-30 19:59:39 +0000589 drmFreeBusid(id);
David Dawes56bd9c22001-07-30 19:59:39 +0000590 return fd;
591 } else {
592 drmFreeBusid(id);
593 }
594 } else {
595 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000596 }
Rik Faith88dbee52001-02-28 09:27:44 +0000597 }
David Dawes56bd9c22001-07-30 19:59:39 +0000598 close(fd);
Rik Faith88dbee52001-02-28 09:27:44 +0000599 }
600 }
601
602#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000603 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000604 for (i = 0; i < 8; i++) {
Rik Faith88dbee52001-02-28 09:27:44 +0000605 char proc_name[64], buf[512];
606 char *driver, *pt, *devstring;
607 int retcode;
608
Daryll Strauss0371c291999-12-18 18:34:59 +0000609 sprintf(proc_name, "/proc/dri/%d/name", i);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000610 if ((fd = open(proc_name, 0, 0)) >= 0) {
611 retcode = read(fd, buf, sizeof(buf)-1);
612 close(fd);
613 if (retcode) {
614 buf[retcode-1] = '\0';
615 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
616 ;
Adam Jackson22e41ef2006-02-20 23:09:00 +0000617 if (*pt) { /* Device is next */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000618 *pt = '\0';
619 if (!strcmp(driver, name)) { /* Match */
620 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
621 ;
622 if (*pt) { /* Found busid */
Rik Faith88dbee52001-02-28 09:27:44 +0000623 return drmOpenByBusid(++pt);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000624 } else { /* No busid */
Frank Binnsad8bbfd2015-01-14 14:07:50 +0000625 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_PRIMARY);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000626 }
627 }
628 }
629 }
Brian Paul569da5a2000-06-08 14:38:22 +0000630 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000631 }
Rik Faith88dbee52001-02-28 09:27:44 +0000632#endif
633
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000634 return -1;
635}
636
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000637
Jose Fonsecad2443b22003-05-27 00:37:33 +0000638/**
639 * Open the DRM device.
640 *
641 * Looks up the specified name and bus ID, and opens the device found. The
642 * entry in /dev/dri is created if necessary and if called by root.
643 *
644 * \param name driver name. Not referenced if bus ID is supplied.
645 * \param busid bus ID. Zero if not known.
646 *
647 * \return a file descriptor on success, or a negative value on error.
648 *
649 * \internal
650 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
651 * otherwise.
652 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000653int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000654{
Dave Airlie79038752006-11-08 15:08:09 +1100655 if (!drmAvailable() && name != NULL && drm_server_info) {
Eric Anholt06cb1322003-10-23 02:23:31 +0000656 /* try to load the kernel */
Dave Airlie79038752006-11-08 15:08:09 +1100657 if (!drm_server_info->load_module(name)) {
Brianccd7b6e2007-05-29 14:54:00 -0600658 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
Eric Anholt06cb1322003-10-23 02:23:31 +0000659 return -1;
660 }
661 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000662
Eric Anholt06cb1322003-10-23 02:23:31 +0000663 if (busid) {
Brianccd7b6e2007-05-29 14:54:00 -0600664 int fd = drmOpenByBusid(busid);
Eric Anholt06cb1322003-10-23 02:23:31 +0000665 if (fd >= 0)
666 return fd;
667 }
Adam Jackson22e41ef2006-02-20 23:09:00 +0000668
Eric Anholt06cb1322003-10-23 02:23:31 +0000669 if (name)
670 return drmOpenByName(name);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000671
Eric Anholt06cb1322003-10-23 02:23:31 +0000672 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000673}
674
Jesse Barnes731cd552008-12-17 10:09:49 -0800675int drmOpenControl(int minor)
676{
677 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
678}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000679
Frank Binns0c5aaee2015-01-14 14:07:51 +0000680int drmOpenRender(int minor)
681{
682 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
683}
684
Jose Fonsecad2443b22003-05-27 00:37:33 +0000685/**
686 * Free the version information returned by drmGetVersion().
687 *
688 * \param v pointer to the version information.
689 *
690 * \internal
691 * It frees the memory pointed by \p %v as well as all the non-null strings
692 * pointers in it.
693 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000694void drmFreeVersion(drmVersionPtr v)
695{
Brianccd7b6e2007-05-29 14:54:00 -0600696 if (!v)
697 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100698 drmFree(v->name);
699 drmFree(v->date);
700 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000701 drmFree(v);
702}
703
Jose Fonsecad2443b22003-05-27 00:37:33 +0000704
705/**
706 * Free the non-public version information returned by the kernel.
707 *
708 * \param v pointer to the version information.
709 *
710 * \internal
711 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
712 * the non-null strings pointers in it.
713 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000714static void drmFreeKernelVersion(drm_version_t *v)
715{
Brianccd7b6e2007-05-29 14:54:00 -0600716 if (!v)
717 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100718 drmFree(v->name);
719 drmFree(v->date);
720 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000721 drmFree(v);
722}
723
Jose Fonsecad2443b22003-05-27 00:37:33 +0000724
725/**
726 * Copy version information.
727 *
728 * \param d destination pointer.
729 * \param s source pointer.
730 *
731 * \internal
732 * Used by drmGetVersion() to translate the information returned by the ioctl
733 * interface in a private structure into the public structure counterpart.
734 */
Brian Paul569da5a2000-06-08 14:38:22 +0000735static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000736{
737 d->version_major = s->version_major;
738 d->version_minor = s->version_minor;
739 d->version_patchlevel = s->version_patchlevel;
740 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400741 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000742 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400743 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000744 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400745 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000746}
747
Daryll Straussb3a57661999-12-05 01:19:48 +0000748
Jose Fonsecad2443b22003-05-27 00:37:33 +0000749/**
750 * Query the driver version information.
751 *
752 * \param fd file descriptor.
753 *
754 * \return pointer to a drmVersion structure which should be freed with
755 * drmFreeVersion().
756 *
757 * \note Similar information is available via /proc/dri.
758 *
759 * \internal
760 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
761 * first with zeros to get the string lengths, and then the actually strings.
762 * It also null-terminates them since they might not be already.
763 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000764drmVersionPtr drmGetVersion(int fd)
765{
766 drmVersionPtr retval;
767 drm_version_t *version = drmMalloc(sizeof(*version));
768
Daryll Straussb3a57661999-12-05 01:19:48 +0000769 version->name_len = 0;
770 version->name = NULL;
771 version->date_len = 0;
772 version->date = NULL;
773 version->desc_len = 0;
774 version->desc = NULL;
Gareth Hughes36047532001-02-15 08:12:14 +0000775
Keith Packard8b9ab102008-06-13 16:03:22 -0700776 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000777 drmFreeKernelVersion(version);
778 return NULL;
779 }
780
Daryll Straussb3a57661999-12-05 01:19:48 +0000781 if (version->name_len)
782 version->name = drmMalloc(version->name_len + 1);
783 if (version->date_len)
784 version->date = drmMalloc(version->date_len + 1);
785 if (version->desc_len)
786 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000787
Keith Packard8b9ab102008-06-13 16:03:22 -0700788 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000789 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
Daryll Straussb3a57661999-12-05 01:19:48 +0000790 drmFreeKernelVersion(version);
791 return NULL;
792 }
793
Adam Jackson22e41ef2006-02-20 23:09:00 +0000794 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000795 if (version->name_len) version->name[version->name_len] = '\0';
796 if (version->date_len) version->date[version->date_len] = '\0';
797 if (version->desc_len) version->desc[version->desc_len] = '\0';
798
Daryll Straussb3a57661999-12-05 01:19:48 +0000799 retval = drmMalloc(sizeof(*retval));
800 drmCopyVersion(retval, version);
801 drmFreeKernelVersion(version);
802 return retval;
803}
804
Jens Owen3903e5a2002-04-09 21:54:56 +0000805
Jose Fonsecad2443b22003-05-27 00:37:33 +0000806/**
807 * Get version information for the DRM user space library.
808 *
809 * This version number is driver independent.
810 *
811 * \param fd file descriptor.
812 *
813 * \return version information.
814 *
815 * \internal
816 * This function allocates and fills a drm_version structure with a hard coded
817 * version number.
818 */
Jens Owen3903e5a2002-04-09 21:54:56 +0000819drmVersionPtr drmGetLibVersion(int fd)
820{
821 drm_version_t *version = drmMalloc(sizeof(*version));
822
823 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100824 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000825 * revision 1.0.x = original DRM interface with no drmGetLibVersion
826 * entry point and many drm<Device> extensions
827 * revision 1.1.x = added drmCommand entry points for device extensions
828 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000829 * revision 1.2.x = added drmSetInterfaceVersion
830 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100831 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000832 */
Dave Airlie79038752006-11-08 15:08:09 +1100833 version->version_major = 1;
834 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000835 version->version_patchlevel = 0;
836
837 return (drmVersionPtr)version;
838}
839
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000840int drmGetCap(int fd, uint64_t capability, uint64_t *value)
841{
842 struct drm_get_cap cap = { capability, 0 };
843 int ret;
844
845 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
846 if (ret)
847 return ret;
848
849 *value = cap.value;
Dave Airlie3b04c732011-03-04 15:48:31 +1000850 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000851}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000852
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100853int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
854{
855 struct drm_set_client_cap cap = { capability, value };
856
857 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
858}
859
Jose Fonsecad2443b22003-05-27 00:37:33 +0000860/**
861 * Free the bus ID information.
862 *
863 * \param busid bus ID information string as given by drmGetBusid().
864 *
865 * \internal
866 * This function is just frees the memory pointed by \p busid.
867 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000868void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000869{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000870 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000871}
872
Jose Fonsecad2443b22003-05-27 00:37:33 +0000873
874/**
875 * Get the bus ID of the device.
876 *
877 * \param fd file descriptor.
878 *
879 * \return bus ID string.
880 *
881 * \internal
882 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
883 * get the string length and data, passing the arguments in a drm_unique
884 * structure.
885 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000886char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000887{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000888 drm_unique_t u;
889
890 u.unique_len = 0;
891 u.unique = NULL;
892
Keith Packard8b9ab102008-06-13 16:03:22 -0700893 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600894 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000895 u.unique = drmMalloc(u.unique_len + 1);
Keith Packard8b9ab102008-06-13 16:03:22 -0700896 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600897 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000898 u.unique[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +0000899
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000900 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +0000901}
902
Jose Fonsecad2443b22003-05-27 00:37:33 +0000903
904/**
905 * Set the bus ID of the device.
906 *
907 * \param fd file descriptor.
908 * \param busid bus ID string.
909 *
910 * \return zero on success, negative on failure.
911 *
912 * \internal
913 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
914 * the arguments in a drm_unique structure.
915 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000916int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000917{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000918 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +0000919
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000920 u.unique = (char *)busid;
921 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000922
Keith Packard8b9ab102008-06-13 16:03:22 -0700923 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
David Dawes56bd9c22001-07-30 19:59:39 +0000924 return -errno;
925 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000926 return 0;
927}
928
Jon Smirl8696e712004-07-07 04:36:36 +0000929int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +0000930{
931 drm_auth_t auth;
932
933 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -0700934 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -0600935 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000936 *magic = auth.magic;
937 return 0;
938}
939
Jon Smirl8696e712004-07-07 04:36:36 +0000940int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +0000941{
942 drm_auth_t auth;
943
944 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -0700945 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -0600946 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000947 return 0;
948}
949
Jose Fonsecad2443b22003-05-27 00:37:33 +0000950/**
951 * Specifies a range of memory that is available for mapping by a
952 * non-root process.
953 *
954 * \param fd file descriptor.
955 * \param offset usually the physical address. The actual meaning depends of
956 * the \p type parameter. See below.
957 * \param size of the memory in bytes.
958 * \param type type of the memory to be mapped.
959 * \param flags combination of several flags to modify the function actions.
960 * \param handle will be set to a value that may be used as the offset
961 * parameter for mmap().
962 *
963 * \return zero on success or a negative value on error.
964 *
965 * \par Mapping the frame buffer
966 * For the frame buffer
967 * - \p offset will be the physical address of the start of the frame buffer,
968 * - \p size will be the size of the frame buffer in bytes, and
969 * - \p type will be DRM_FRAME_BUFFER.
970 *
971 * \par
972 * The area mapped will be uncached. If MTRR support is available in the
973 * kernel, the frame buffer area will be set to write combining.
974 *
975 * \par Mapping the MMIO register area
976 * For the MMIO register area,
977 * - \p offset will be the physical address of the start of the register area,
978 * - \p size will be the size of the register area bytes, and
979 * - \p type will be DRM_REGISTERS.
980 * \par
981 * The area mapped will be uncached.
982 *
983 * \par Mapping the SAREA
984 * For the SAREA,
985 * - \p offset will be ignored and should be set to zero,
986 * - \p size will be the desired size of the SAREA in bytes,
987 * - \p type will be DRM_SHM.
988 *
989 * \par
990 * A shared memory area of the requested size will be created and locked in
991 * kernel memory. This area may be mapped into client-space by using the handle
992 * returned.
993 *
994 * \note May only be called by root.
995 *
996 * \internal
997 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
998 * the arguments in a drm_map structure.
999 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001000int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1001 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001002{
1003 drm_map_t map;
1004
1005 map.offset = offset;
1006 map.size = size;
1007 map.handle = 0;
1008 map.type = type;
1009 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -07001010 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001011 return -errno;
1012 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001013 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001014 return 0;
1015}
1016
Jon Smirl8696e712004-07-07 04:36:36 +00001017int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00001018{
1019 drm_map_t map;
1020
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07001021 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001022
Keith Packard8b9ab102008-06-13 16:03:22 -07001023 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06001024 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00001025 return 0;
1026}
1027
Jose Fonsecad2443b22003-05-27 00:37:33 +00001028/**
1029 * Make buffers available for DMA transfers.
1030 *
1031 * \param fd file descriptor.
1032 * \param count number of buffers.
1033 * \param size size of each buffer.
1034 * \param flags buffer allocation flags.
1035 * \param agp_offset offset in the AGP aperture
1036 *
1037 * \return number of buffers allocated, negative on error.
1038 *
1039 * \internal
1040 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1041 *
1042 * \sa drm_buf_desc.
1043 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001044int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1045 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001046{
1047 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001048
Daryll Straussb3a57661999-12-05 01:19:48 +00001049 request.count = count;
1050 request.size = size;
1051 request.low_mark = 0;
1052 request.high_mark = 0;
1053 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001054 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001055
Keith Packard8b9ab102008-06-13 16:03:22 -07001056 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001057 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001058 return request.count;
1059}
1060
1061int drmMarkBufs(int fd, double low, double high)
1062{
1063 drm_buf_info_t info;
1064 int i;
1065
1066 info.count = 0;
1067 info.list = NULL;
1068
Keith Packard8b9ab102008-06-13 16:03:22 -07001069 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001070 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001071
Brianccd7b6e2007-05-29 14:54:00 -06001072 if (!info.count)
1073 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001074
Daryll Straussb3a57661999-12-05 01:19:48 +00001075 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1076 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001077
Keith Packard8b9ab102008-06-13 16:03:22 -07001078 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001079 int retval = -errno;
1080 drmFree(info.list);
1081 return retval;
1082 }
Gareth Hughes36047532001-02-15 08:12:14 +00001083
Daryll Straussb3a57661999-12-05 01:19:48 +00001084 for (i = 0; i < info.count; i++) {
1085 info.list[i].low_mark = low * info.list[i].count;
1086 info.list[i].high_mark = high * info.list[i].count;
Keith Packard8b9ab102008-06-13 16:03:22 -07001087 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001088 int retval = -errno;
1089 drmFree(info.list);
1090 return retval;
1091 }
1092 }
1093 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001094
Daryll Straussb3a57661999-12-05 01:19:48 +00001095 return 0;
1096}
1097
Jose Fonsecad2443b22003-05-27 00:37:33 +00001098/**
1099 * Free buffers.
1100 *
1101 * \param fd file descriptor.
1102 * \param count number of buffers to free.
1103 * \param list list of buffers to be freed.
1104 *
1105 * \return zero on success, or a negative value on failure.
1106 *
1107 * \note This function is primarily used for debugging.
1108 *
1109 * \internal
1110 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1111 * the arguments in a drm_buf_free structure.
1112 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001113int drmFreeBufs(int fd, int count, int *list)
1114{
1115 drm_buf_free_t request;
1116
1117 request.count = count;
1118 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001119 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001120 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001121 return 0;
1122}
1123
Jose Fonsecad2443b22003-05-27 00:37:33 +00001124
1125/**
1126 * Close the device.
1127 *
1128 * \param fd file descriptor.
1129 *
1130 * \internal
1131 * This function closes the file descriptor.
1132 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001133int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001134{
1135 unsigned long key = drmGetKeyFromFd(fd);
1136 drmHashEntry *entry = drmGetEntry(fd);
1137
1138 drmHashDestroy(entry->tagTable);
1139 entry->fd = 0;
1140 entry->f = NULL;
1141 entry->tagTable = NULL;
1142
1143 drmHashDelete(drmHashTable, key);
1144 drmFree(entry);
1145
1146 return close(fd);
1147}
1148
Jose Fonsecad2443b22003-05-27 00:37:33 +00001149
1150/**
1151 * Map a region of memory.
1152 *
1153 * \param fd file descriptor.
1154 * \param handle handle returned by drmAddMap().
1155 * \param size size in bytes. Must match the size used by drmAddMap().
1156 * \param address will contain the user-space virtual address where the mapping
1157 * begins.
1158 *
1159 * \return zero on success, or a negative value on failure.
1160 *
1161 * \internal
1162 * This function is a wrapper for mmap().
1163 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001164int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001165{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001166 static unsigned long pagesize_mask = 0;
1167
Brianccd7b6e2007-05-29 14:54:00 -06001168 if (fd < 0)
1169 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001170
1171 if (!pagesize_mask)
1172 pagesize_mask = getpagesize() - 1;
1173
1174 size = (size + pagesize_mask) & ~pagesize_mask;
1175
Emil Velikovfaf51d52014-09-07 20:03:05 +01001176 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001177 if (*address == MAP_FAILED)
1178 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001179 return 0;
1180}
1181
Jose Fonsecad2443b22003-05-27 00:37:33 +00001182
1183/**
1184 * Unmap mappings obtained with drmMap().
1185 *
1186 * \param address address as given by drmMap().
1187 * \param size size in bytes. Must match the size used by drmMap().
1188 *
1189 * \return zero on success, or a negative value on failure.
1190 *
1191 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001192 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001193 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001194int drmUnmap(drmAddress address, drmSize size)
1195{
Emil Velikovfaf51d52014-09-07 20:03:05 +01001196 return drm_munmap(address, size);
Daryll Straussb3a57661999-12-05 01:19:48 +00001197}
1198
1199drmBufInfoPtr drmGetBufInfo(int fd)
1200{
1201 drm_buf_info_t info;
1202 drmBufInfoPtr retval;
1203 int i;
1204
1205 info.count = 0;
1206 info.list = NULL;
1207
Keith Packard8b9ab102008-06-13 16:03:22 -07001208 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001209 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001210
1211 if (info.count) {
1212 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1213 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001214
Keith Packard8b9ab102008-06-13 16:03:22 -07001215 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001216 drmFree(info.list);
1217 return NULL;
1218 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001219
Daryll Straussb3a57661999-12-05 01:19:48 +00001220 retval = drmMalloc(sizeof(*retval));
1221 retval->count = info.count;
1222 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1223 for (i = 0; i < info.count; i++) {
1224 retval->list[i].count = info.list[i].count;
1225 retval->list[i].size = info.list[i].size;
1226 retval->list[i].low_mark = info.list[i].low_mark;
1227 retval->list[i].high_mark = info.list[i].high_mark;
1228 }
1229 drmFree(info.list);
1230 return retval;
1231 }
1232 return NULL;
1233}
1234
Jose Fonsecad2443b22003-05-27 00:37:33 +00001235/**
1236 * Map all DMA buffers into client-virtual space.
1237 *
1238 * \param fd file descriptor.
1239 *
1240 * \return a pointer to a ::drmBufMap structure.
1241 *
1242 * \note The client may not use these buffers until obtaining buffer indices
1243 * with drmDMA().
1244 *
1245 * \internal
1246 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1247 * information about the buffers in a drm_buf_map structure into the
1248 * client-visible data structures.
1249 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001250drmBufMapPtr drmMapBufs(int fd)
1251{
1252 drm_buf_map_t bufs;
1253 drmBufMapPtr retval;
1254 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001255
Daryll Straussb3a57661999-12-05 01:19:48 +00001256 bufs.count = 0;
1257 bufs.list = NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001258 bufs.virtual = NULL;
Keith Packard8b9ab102008-06-13 16:03:22 -07001259 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Brianccd7b6e2007-05-29 14:54:00 -06001260 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001261
Brianccd7b6e2007-05-29 14:54:00 -06001262 if (!bufs.count)
1263 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001264
Daryll Straussb3a57661999-12-05 01:19:48 +00001265 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1266 return NULL;
1267
Keith Packard8b9ab102008-06-13 16:03:22 -07001268 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001269 drmFree(bufs.list);
1270 return NULL;
1271 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001272
Daryll Straussb3a57661999-12-05 01:19:48 +00001273 retval = drmMalloc(sizeof(*retval));
1274 retval->count = bufs.count;
1275 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1276 for (i = 0; i < bufs.count; i++) {
1277 retval->list[i].idx = bufs.list[i].idx;
1278 retval->list[i].total = bufs.list[i].total;
1279 retval->list[i].used = 0;
1280 retval->list[i].address = bufs.list[i].address;
1281 }
Jon Smirl8696e712004-07-07 04:36:36 +00001282
1283 drmFree(bufs.list);
1284
Daryll Straussb3a57661999-12-05 01:19:48 +00001285 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001286}
1287
Jose Fonsecad2443b22003-05-27 00:37:33 +00001288
1289/**
1290 * Unmap buffers allocated with drmMapBufs().
1291 *
1292 * \return zero on success, or negative value on failure.
1293 *
1294 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001295 * Calls munmap() for every buffer stored in \p bufs and frees the
1296 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001297 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001298int drmUnmapBufs(drmBufMapPtr bufs)
1299{
1300 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001301
Daryll Straussb3a57661999-12-05 01:19:48 +00001302 for (i = 0; i < bufs->count; i++) {
Emil Velikovfaf51d52014-09-07 20:03:05 +01001303 drm_munmap(bufs->list[i].address, bufs->list[i].total);
Daryll Straussb3a57661999-12-05 01:19:48 +00001304 }
Jon Smirl8696e712004-07-07 04:36:36 +00001305
1306 drmFree(bufs->list);
1307 drmFree(bufs);
1308
Daryll Straussb3a57661999-12-05 01:19:48 +00001309 return 0;
1310}
1311
Jose Fonsecad2443b22003-05-27 00:37:33 +00001312
Gareth Hughes36047532001-02-15 08:12:14 +00001313#define DRM_DMA_RETRY 16
1314
Jose Fonsecad2443b22003-05-27 00:37:33 +00001315/**
1316 * Reserve DMA buffers.
1317 *
1318 * \param fd file descriptor.
1319 * \param request
1320 *
1321 * \return zero on success, or a negative value on failure.
1322 *
1323 * \internal
1324 * Assemble the arguments into a drm_dma structure and keeps issuing the
1325 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1326 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001327int drmDMA(int fd, drmDMAReqPtr request)
1328{
1329 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001330 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001331
Daryll Straussb3a57661999-12-05 01:19:48 +00001332 dma.context = request->context;
1333 dma.send_count = request->send_count;
1334 dma.send_indices = request->send_list;
1335 dma.send_sizes = request->send_sizes;
1336 dma.flags = request->flags;
1337 dma.request_count = request->request_count;
1338 dma.request_size = request->request_size;
1339 dma.request_indices = request->request_list;
1340 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001341 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001342
1343 do {
1344 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1345 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1346
1347 if ( ret == 0 ) {
1348 request->granted_count = dma.granted_count;
1349 return 0;
1350 } else {
1351 return -errno;
1352 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001353}
1354
Jose Fonsecad2443b22003-05-27 00:37:33 +00001355
1356/**
1357 * Obtain heavyweight hardware lock.
1358 *
1359 * \param fd file descriptor.
1360 * \param context context.
1361 * \param flags flags that determine the sate of the hardware when the function
1362 * returns.
1363 *
1364 * \return always zero.
1365 *
1366 * \internal
1367 * This function translates the arguments into a drm_lock structure and issue
1368 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1369 */
Jon Smirl8696e712004-07-07 04:36:36 +00001370int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001371{
1372 drm_lock_t lock;
1373
1374 lock.context = context;
1375 lock.flags = 0;
1376 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1377 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1378 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1379 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1380 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1381 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001382
Keith Packard8b9ab102008-06-13 16:03:22 -07001383 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Daryll Straussb3a57661999-12-05 01:19:48 +00001384 ;
1385 return 0;
1386}
1387
Jose Fonsecad2443b22003-05-27 00:37:33 +00001388/**
1389 * Release the hardware lock.
1390 *
1391 * \param fd file descriptor.
1392 * \param context context.
1393 *
1394 * \return zero on success, or a negative value on failure.
1395 *
1396 * \internal
1397 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1398 * argument in a drm_lock structure.
1399 */
Jon Smirl8696e712004-07-07 04:36:36 +00001400int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001401{
1402 drm_lock_t lock;
1403
1404 lock.context = context;
1405 lock.flags = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001406 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001407}
1408
Adam Jackson22e41ef2006-02-20 23:09:00 +00001409drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001410{
1411 drm_ctx_res_t res;
1412 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001413 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001414 int i;
1415
1416 res.count = 0;
1417 res.contexts = NULL;
Keith Packard8b9ab102008-06-13 16:03:22 -07001418 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001419 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001420
Brianccd7b6e2007-05-29 14:54:00 -06001421 if (!res.count)
1422 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001423
Brianccd7b6e2007-05-29 14:54:00 -06001424 if (!(list = drmMalloc(res.count * sizeof(*list))))
1425 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001426 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1427 drmFree(list);
1428 return NULL;
1429 }
1430
1431 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001432 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001433 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001434
Brianccd7b6e2007-05-29 14:54:00 -06001435 for (i = 0; i < res.count; i++)
1436 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001437 drmFree(list);
1438
1439 *count = res.count;
1440 return retval;
1441}
1442
Adam Jackson22e41ef2006-02-20 23:09:00 +00001443void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001444{
1445 drmFree(pt);
1446}
1447
Jose Fonsecad2443b22003-05-27 00:37:33 +00001448/**
1449 * Create context.
1450 *
1451 * Used by the X server during GLXContext initialization. This causes
1452 * per-context kernel-level resources to be allocated.
1453 *
1454 * \param fd file descriptor.
1455 * \param handle is set on success. To be used by the client when requesting DMA
1456 * dispatch with drmDMA().
1457 *
1458 * \return zero on success, or a negative value on failure.
1459 *
1460 * \note May only be called by root.
1461 *
1462 * \internal
1463 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1464 * argument in a drm_ctx structure.
1465 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001466int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001467{
1468 drm_ctx_t ctx;
1469
1470 ctx.flags = 0; /* Modified with functions below */
Keith Packard8b9ab102008-06-13 16:03:22 -07001471 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001472 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001473 *handle = ctx.handle;
1474 return 0;
1475}
1476
Jon Smirl8696e712004-07-07 04:36:36 +00001477int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001478{
1479 drm_ctx_t ctx;
1480
1481 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001482 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001483 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001484 return 0;
1485}
1486
Jon Smirl8696e712004-07-07 04:36:36 +00001487int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001488{
1489 drm_ctx_t ctx;
1490
Adam Jackson22e41ef2006-02-20 23:09:00 +00001491 /*
1492 * Context preserving means that no context switches are done between DMA
1493 * buffers from one context and the next. This is suitable for use in the
1494 * X server (which promises to maintain hardware context), or in the
1495 * client-side library when buffers are swapped on behalf of two threads.
1496 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001497 ctx.handle = context;
1498 ctx.flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001499 if (flags & DRM_CONTEXT_PRESERVED)
1500 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1501 if (flags & DRM_CONTEXT_2DONLY)
1502 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001503 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001504 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001505 return 0;
1506}
1507
Adam Jackson22e41ef2006-02-20 23:09:00 +00001508int drmGetContextFlags(int fd, drm_context_t context,
1509 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001510{
1511 drm_ctx_t ctx;
1512
1513 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001514 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001515 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001516 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001517 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1518 *flags |= DRM_CONTEXT_PRESERVED;
1519 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1520 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001521 return 0;
1522}
Gareth Hughes36047532001-02-15 08:12:14 +00001523
Jose Fonsecad2443b22003-05-27 00:37:33 +00001524/**
1525 * Destroy context.
1526 *
1527 * Free any kernel-level resources allocated with drmCreateContext() associated
1528 * with the context.
1529 *
1530 * \param fd file descriptor.
1531 * \param handle handle given by drmCreateContext().
1532 *
1533 * \return zero on success, or a negative value on failure.
1534 *
1535 * \note May only be called by root.
1536 *
1537 * \internal
1538 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1539 * argument in a drm_ctx structure.
1540 */
Jon Smirl8696e712004-07-07 04:36:36 +00001541int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001542{
1543 drm_ctx_t ctx;
1544 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001545 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001546 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001547 return 0;
1548}
1549
Adam Jackson22e41ef2006-02-20 23:09:00 +00001550int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001551{
1552 drm_draw_t draw;
Keith Packard8b9ab102008-06-13 16:03:22 -07001553 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001554 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001555 *handle = draw.handle;
1556 return 0;
1557}
1558
Jon Smirl8696e712004-07-07 04:36:36 +00001559int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001560{
1561 drm_draw_t draw;
1562 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001563 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001564 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001565 return 0;
1566}
1567
Michel Dänzer9810ec22006-08-22 16:40:07 +02001568int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1569 drm_drawable_info_type_t type, unsigned int num,
1570 void *data)
1571{
1572 drm_update_draw_t update;
1573
1574 update.handle = handle;
1575 update.type = type;
1576 update.num = num;
1577 update.data = (unsigned long long)(unsigned long)data;
1578
Keith Packard8b9ab102008-06-13 16:03:22 -07001579 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Brianccd7b6e2007-05-29 14:54:00 -06001580 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001581
1582 return 0;
1583}
1584
Jose Fonsecad2443b22003-05-27 00:37:33 +00001585/**
1586 * Acquire the AGP device.
1587 *
1588 * Must be called before any of the other AGP related calls.
1589 *
1590 * \param fd file descriptor.
1591 *
1592 * \return zero on success, or a negative value on failure.
1593 *
1594 * \internal
1595 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1596 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001597int drmAgpAcquire(int fd)
1598{
Keith Packard8b9ab102008-06-13 16:03:22 -07001599 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001600 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001601 return 0;
1602}
1603
Jose Fonsecad2443b22003-05-27 00:37:33 +00001604
1605/**
1606 * Release the AGP device.
1607 *
1608 * \param fd file descriptor.
1609 *
1610 * \return zero on success, or a negative value on failure.
1611 *
1612 * \internal
1613 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1614 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001615int drmAgpRelease(int fd)
1616{
Keith Packard8b9ab102008-06-13 16:03:22 -07001617 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001618 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001619 return 0;
1620}
1621
Jose Fonsecad2443b22003-05-27 00:37:33 +00001622
1623/**
1624 * Set the AGP mode.
1625 *
1626 * \param fd file descriptor.
1627 * \param mode AGP mode.
1628 *
1629 * \return zero on success, or a negative value on failure.
1630 *
1631 * \internal
1632 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1633 * argument in a drm_agp_mode structure.
1634 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001635int drmAgpEnable(int fd, unsigned long mode)
1636{
1637 drm_agp_mode_t m;
1638
1639 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001640 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Brianccd7b6e2007-05-29 14:54:00 -06001641 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001642 return 0;
1643}
1644
Jose Fonsecad2443b22003-05-27 00:37:33 +00001645
1646/**
1647 * Allocate a chunk of AGP memory.
1648 *
1649 * \param fd file descriptor.
1650 * \param size requested memory size in bytes. Will be rounded to page boundary.
1651 * \param type type of memory to allocate.
1652 * \param address if not zero, will be set to the physical address of the
1653 * allocated memory.
1654 * \param handle on success will be set to a handle of the allocated memory.
1655 *
1656 * \return zero on success, or a negative value on failure.
1657 *
1658 * \internal
1659 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1660 * arguments in a drm_agp_buffer structure.
1661 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001662int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Dave Airlie7ede2092005-11-29 09:50:47 +00001663 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001664{
1665 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001666
1667 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001668 b.size = size;
1669 b.handle = 0;
1670 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001671 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001672 return -errno;
1673 if (address != 0UL)
1674 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001675 *handle = b.handle;
1676 return 0;
1677}
1678
Jose Fonsecad2443b22003-05-27 00:37:33 +00001679
1680/**
1681 * Free a chunk of AGP memory.
1682 *
1683 * \param fd file descriptor.
1684 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
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_FREE ioctl, passing the
1690 * argument in a drm_agp_buffer structure.
1691 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001692int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001693{
1694 drm_agp_buffer_t b;
1695
1696 b.size = 0;
1697 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001698 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001699 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001700 return 0;
1701}
1702
Jose Fonsecad2443b22003-05-27 00:37:33 +00001703
1704/**
1705 * Bind a chunk of AGP memory.
1706 *
1707 * \param fd file descriptor.
1708 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1709 * \param offset offset in bytes. It will round to page boundary.
1710 *
1711 * \return zero on success, or a negative value on failure.
1712 *
1713 * \internal
1714 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1715 * argument in a drm_agp_binding structure.
1716 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001717int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001718{
1719 drm_agp_binding_t b;
1720
1721 b.handle = handle;
1722 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001723 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001724 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001725 return 0;
1726}
1727
Jose Fonsecad2443b22003-05-27 00:37:33 +00001728
1729/**
1730 * Unbind a chunk of AGP memory.
1731 *
1732 * \param fd file descriptor.
1733 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1734 *
1735 * \return zero on success, or a negative value on failure.
1736 *
1737 * \internal
1738 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1739 * the argument in a drm_agp_binding structure.
1740 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001741int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001742{
1743 drm_agp_binding_t b;
1744
1745 b.handle = handle;
1746 b.offset = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001747 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001748 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001749 return 0;
1750}
1751
Jose Fonsecad2443b22003-05-27 00:37:33 +00001752
1753/**
1754 * Get AGP driver major version number.
1755 *
1756 * \param fd file descriptor.
1757 *
1758 * \return major version number on success, or a negative value on failure..
1759 *
1760 * \internal
1761 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1762 * necessary information in a drm_agp_info structure.
1763 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001764int drmAgpVersionMajor(int fd)
1765{
1766 drm_agp_info_t i;
1767
Keith Packard8b9ab102008-06-13 16:03:22 -07001768 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001769 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001770 return i.agp_version_major;
1771}
1772
Jose Fonsecad2443b22003-05-27 00:37:33 +00001773
1774/**
1775 * Get AGP driver minor version number.
1776 *
1777 * \param fd file descriptor.
1778 *
1779 * \return minor version number on success, or a negative value on failure.
1780 *
1781 * \internal
1782 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1783 * necessary information in a drm_agp_info structure.
1784 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001785int drmAgpVersionMinor(int fd)
1786{
1787 drm_agp_info_t i;
1788
Keith Packard8b9ab102008-06-13 16:03:22 -07001789 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001790 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001791 return i.agp_version_minor;
1792}
1793
Jose Fonsecad2443b22003-05-27 00:37:33 +00001794
1795/**
1796 * Get AGP mode.
1797 *
1798 * \param fd file descriptor.
1799 *
1800 * \return mode on success, or zero on failure.
1801 *
1802 * \internal
1803 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1804 * necessary information in a drm_agp_info structure.
1805 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001806unsigned long drmAgpGetMode(int fd)
1807{
1808 drm_agp_info_t i;
1809
Keith Packard8b9ab102008-06-13 16:03:22 -07001810 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001811 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001812 return i.mode;
1813}
1814
Jose Fonsecad2443b22003-05-27 00:37:33 +00001815
1816/**
1817 * Get AGP aperture base.
1818 *
1819 * \param fd file descriptor.
1820 *
1821 * \return aperture base on success, zero on failure.
1822 *
1823 * \internal
1824 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1825 * necessary information in a drm_agp_info structure.
1826 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001827unsigned long drmAgpBase(int fd)
1828{
1829 drm_agp_info_t i;
1830
Keith Packard8b9ab102008-06-13 16:03:22 -07001831 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001832 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001833 return i.aperture_base;
1834}
1835
Jose Fonsecad2443b22003-05-27 00:37:33 +00001836
1837/**
1838 * Get AGP aperture size.
1839 *
1840 * \param fd file descriptor.
1841 *
1842 * \return aperture size on success, zero on failure.
1843 *
1844 * \internal
1845 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1846 * necessary information in a drm_agp_info structure.
1847 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001848unsigned long drmAgpSize(int fd)
1849{
1850 drm_agp_info_t i;
1851
Keith Packard8b9ab102008-06-13 16:03:22 -07001852 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001853 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001854 return i.aperture_size;
1855}
1856
Jose Fonsecad2443b22003-05-27 00:37:33 +00001857
1858/**
1859 * Get used AGP memory.
1860 *
1861 * \param fd file descriptor.
1862 *
1863 * \return memory used on success, or zero on failure.
1864 *
1865 * \internal
1866 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1867 * necessary information in a drm_agp_info structure.
1868 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001869unsigned long drmAgpMemoryUsed(int fd)
1870{
1871 drm_agp_info_t i;
1872
Keith Packard8b9ab102008-06-13 16:03:22 -07001873 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001874 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001875 return i.memory_used;
1876}
1877
Jose Fonsecad2443b22003-05-27 00:37:33 +00001878
1879/**
1880 * Get available AGP memory.
1881 *
1882 * \param fd file descriptor.
1883 *
1884 * \return memory available on success, or zero on failure.
1885 *
1886 * \internal
1887 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1888 * necessary information in a drm_agp_info structure.
1889 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001890unsigned long drmAgpMemoryAvail(int fd)
1891{
1892 drm_agp_info_t i;
1893
Keith Packard8b9ab102008-06-13 16:03:22 -07001894 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001895 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001896 return i.memory_allowed;
1897}
1898
Jose Fonsecad2443b22003-05-27 00:37:33 +00001899
1900/**
1901 * Get hardware vendor ID.
1902 *
1903 * \param fd file descriptor.
1904 *
1905 * \return vendor ID on success, or zero on failure.
1906 *
1907 * \internal
1908 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1909 * necessary information in a drm_agp_info structure.
1910 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001911unsigned int drmAgpVendorId(int fd)
1912{
1913 drm_agp_info_t i;
1914
Keith Packard8b9ab102008-06-13 16:03:22 -07001915 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001916 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001917 return i.id_vendor;
1918}
1919
Jose Fonsecad2443b22003-05-27 00:37:33 +00001920
1921/**
1922 * Get hardware device ID.
1923 *
1924 * \param fd file descriptor.
1925 *
1926 * \return zero on success, or zero on failure.
1927 *
1928 * \internal
1929 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1930 * necessary information in a drm_agp_info structure.
1931 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001932unsigned int drmAgpDeviceId(int fd)
1933{
1934 drm_agp_info_t i;
1935
Keith Packard8b9ab102008-06-13 16:03:22 -07001936 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001937 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001938 return i.id_device;
1939}
1940
Dave Airlie7ede2092005-11-29 09:50:47 +00001941int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001942{
1943 drm_scatter_gather_t sg;
1944
1945 *handle = 0;
1946 sg.size = size;
1947 sg.handle = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001948 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06001949 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001950 *handle = sg.handle;
1951 return 0;
1952}
1953
Dave Airlie7ede2092005-11-29 09:50:47 +00001954int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001955{
1956 drm_scatter_gather_t sg;
1957
1958 sg.size = 0;
1959 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001960 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06001961 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001962 return 0;
1963}
1964
Jose Fonsecad2443b22003-05-27 00:37:33 +00001965/**
1966 * Wait for VBLANK.
1967 *
1968 * \param fd file descriptor.
1969 * \param vbl pointer to a drmVBlank structure.
1970 *
1971 * \return zero on success, or a negative value on failure.
1972 *
1973 * \internal
1974 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1975 */
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001976int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1977{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001978 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001979 int ret;
1980
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001981 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1982 if (ret < 0) {
Daniel Kurtz1eb28602013-03-28 14:05:40 +08001983 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001984 goto out;
1985 }
1986 timeout.tv_sec++;
1987
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001988 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001989 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00001990 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08001991 if (ret && errno == EINTR) {
1992 clock_gettime(CLOCK_MONOTONIC, &cur);
1993 /* Timeout after 1s */
1994 if (cur.tv_sec > timeout.tv_sec + 1 ||
1995 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1996 timeout.tv_nsec)) {
1997 errno = EBUSY;
1998 ret = -1;
1999 break;
2000 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002001 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002002 } while (ret && errno == EINTR);
2003
Jesse Barnesf4f76a62009-01-07 10:18:08 -08002004out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00002005 return ret;
2006}
2007
Daryll Straussb3a57661999-12-05 01:19:48 +00002008int drmError(int err, const char *label)
2009{
2010 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06002011 case DRM_ERR_NO_DEVICE:
2012 fprintf(stderr, "%s: no device\n", label);
2013 break;
2014 case DRM_ERR_NO_ACCESS:
2015 fprintf(stderr, "%s: no access\n", label);
2016 break;
2017 case DRM_ERR_NOT_ROOT:
2018 fprintf(stderr, "%s: not root\n", label);
2019 break;
2020 case DRM_ERR_INVALID:
2021 fprintf(stderr, "%s: invalid args\n", label);
2022 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00002023 default:
Brianccd7b6e2007-05-29 14:54:00 -06002024 if (err < 0)
2025 err = -err;
Daryll Straussb3a57661999-12-05 01:19:48 +00002026 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2027 break;
2028 }
2029
2030 return 1;
2031}
2032
Jose Fonsecad2443b22003-05-27 00:37:33 +00002033/**
2034 * Install IRQ handler.
2035 *
2036 * \param fd file descriptor.
2037 * \param irq IRQ number.
2038 *
2039 * \return zero on success, or a negative value on failure.
2040 *
2041 * \internal
2042 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2043 * argument in a drm_control structure.
2044 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002045int drmCtlInstHandler(int fd, int irq)
2046{
2047 drm_control_t ctl;
2048
2049 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002050 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002051 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002052 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002053 return 0;
2054}
2055
Jose Fonsecad2443b22003-05-27 00:37:33 +00002056
2057/**
2058 * Uninstall IRQ handler.
2059 *
2060 * \param fd file descriptor.
2061 *
2062 * \return zero on success, or a negative value on failure.
2063 *
2064 * \internal
2065 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2066 * argument in a drm_control structure.
2067 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002068int drmCtlUninstHandler(int fd)
2069{
2070 drm_control_t ctl;
2071
2072 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002073 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002074 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002075 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002076 return 0;
2077}
2078
2079int drmFinish(int fd, int context, drmLockFlags flags)
2080{
2081 drm_lock_t lock;
2082
2083 lock.context = context;
2084 lock.flags = 0;
2085 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2086 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2087 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2088 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2089 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2090 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002091 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Brianccd7b6e2007-05-29 14:54:00 -06002092 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002093 return 0;
2094}
2095
Jose Fonsecad2443b22003-05-27 00:37:33 +00002096/**
2097 * Get IRQ from bus ID.
2098 *
2099 * \param fd file descriptor.
2100 * \param busnum bus number.
2101 * \param devnum device number.
2102 * \param funcnum function number.
2103 *
2104 * \return IRQ number on success, or a negative value on failure.
2105 *
2106 * \internal
2107 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2108 * arguments in a drm_irq_busid structure.
2109 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002110int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2111{
2112 drm_irq_busid_t p;
2113
2114 p.busnum = busnum;
2115 p.devnum = devnum;
2116 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002117 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Brianccd7b6e2007-05-29 14:54:00 -06002118 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002119 return p.irq;
2120}
2121
Jon Smirl8696e712004-07-07 04:36:36 +00002122int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002123{
2124 drmHashEntry *entry = drmGetEntry(fd);
2125
2126 if (drmHashInsert(entry->tagTable, context, tag)) {
2127 drmHashDelete(entry->tagTable, context);
2128 drmHashInsert(entry->tagTable, context, tag);
2129 }
2130 return 0;
2131}
2132
Jon Smirl8696e712004-07-07 04:36:36 +00002133int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002134{
2135 drmHashEntry *entry = drmGetEntry(fd);
2136
2137 return drmHashDelete(entry->tagTable, context);
2138}
2139
Jon Smirl8696e712004-07-07 04:36:36 +00002140void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002141{
2142 drmHashEntry *entry = drmGetEntry(fd);
2143 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002144
Brianccd7b6e2007-05-29 14:54:00 -06002145 if (drmHashLookup(entry->tagTable, context, &value))
2146 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002147
2148 return value;
2149}
2150
Adam Jackson22e41ef2006-02-20 23:09:00 +00002151int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2152 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002153{
2154 drm_ctx_priv_map_t map;
2155
2156 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002157 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002158
Keith Packard8b9ab102008-06-13 16:03:22 -07002159 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002160 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002161 return 0;
2162}
2163
Adam Jackson22e41ef2006-02-20 23:09:00 +00002164int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2165 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002166{
2167 drm_ctx_priv_map_t map;
2168
2169 map.ctx_id = ctx_id;
2170
Keith Packard8b9ab102008-06-13 16:03:22 -07002171 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002172 return -errno;
2173 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002174 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002175
2176 return 0;
2177}
2178
Jon Smirl8696e712004-07-07 04:36:36 +00002179int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2180 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
Rik Faith88dbee52001-02-28 09:27:44 +00002181 int *mtrr)
2182{
2183 drm_map_t map;
2184
2185 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002186 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002187 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002188 *offset = map.offset;
2189 *size = map.size;
2190 *type = map.type;
2191 *flags = map.flags;
2192 *handle = (unsigned long)map.handle;
2193 *mtrr = map.mtrr;
2194 return 0;
2195}
2196
2197int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2198 unsigned long *magic, unsigned long *iocs)
2199{
2200 drm_client_t client;
2201
2202 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002203 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Brianccd7b6e2007-05-29 14:54:00 -06002204 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002205 *auth = client.auth;
2206 *pid = client.pid;
2207 *uid = client.uid;
2208 *magic = client.magic;
2209 *iocs = client.iocs;
2210 return 0;
2211}
2212
2213int drmGetStats(int fd, drmStatsT *stats)
2214{
2215 drm_stats_t s;
2216 int i;
2217
Keith Packard8b9ab102008-06-13 16:03:22 -07002218 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Brianccd7b6e2007-05-29 14:54:00 -06002219 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002220
2221 stats->count = 0;
2222 memset(stats, 0, sizeof(*stats));
2223 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2224 return -1;
2225
2226#define SET_VALUE \
2227 stats->data[i].long_format = "%-20.20s"; \
2228 stats->data[i].rate_format = "%8.8s"; \
2229 stats->data[i].isvalue = 1; \
2230 stats->data[i].verbose = 0
2231
2232#define SET_COUNT \
2233 stats->data[i].long_format = "%-20.20s"; \
2234 stats->data[i].rate_format = "%5.5s"; \
2235 stats->data[i].isvalue = 0; \
2236 stats->data[i].mult_names = "kgm"; \
2237 stats->data[i].mult = 1000; \
2238 stats->data[i].verbose = 0
2239
2240#define SET_BYTE \
2241 stats->data[i].long_format = "%-20.20s"; \
2242 stats->data[i].rate_format = "%5.5s"; \
2243 stats->data[i].isvalue = 0; \
2244 stats->data[i].mult_names = "KGM"; \
2245 stats->data[i].mult = 1024; \
2246 stats->data[i].verbose = 0
2247
2248
2249 stats->count = s.count;
2250 for (i = 0; i < s.count; i++) {
2251 stats->data[i].value = s.data[i].value;
2252 switch (s.data[i].type) {
2253 case _DRM_STAT_LOCK:
2254 stats->data[i].long_name = "Lock";
2255 stats->data[i].rate_name = "Lock";
2256 SET_VALUE;
2257 break;
2258 case _DRM_STAT_OPENS:
2259 stats->data[i].long_name = "Opens";
2260 stats->data[i].rate_name = "O";
2261 SET_COUNT;
2262 stats->data[i].verbose = 1;
2263 break;
2264 case _DRM_STAT_CLOSES:
2265 stats->data[i].long_name = "Closes";
2266 stats->data[i].rate_name = "Lock";
2267 SET_COUNT;
2268 stats->data[i].verbose = 1;
2269 break;
2270 case _DRM_STAT_IOCTLS:
2271 stats->data[i].long_name = "Ioctls";
2272 stats->data[i].rate_name = "Ioc/s";
2273 SET_COUNT;
2274 break;
2275 case _DRM_STAT_LOCKS:
2276 stats->data[i].long_name = "Locks";
2277 stats->data[i].rate_name = "Lck/s";
2278 SET_COUNT;
2279 break;
2280 case _DRM_STAT_UNLOCKS:
2281 stats->data[i].long_name = "Unlocks";
2282 stats->data[i].rate_name = "Unl/s";
2283 SET_COUNT;
2284 break;
2285 case _DRM_STAT_IRQ:
2286 stats->data[i].long_name = "IRQs";
2287 stats->data[i].rate_name = "IRQ/s";
2288 SET_COUNT;
2289 break;
2290 case _DRM_STAT_PRIMARY:
2291 stats->data[i].long_name = "Primary Bytes";
2292 stats->data[i].rate_name = "PB/s";
2293 SET_BYTE;
2294 break;
2295 case _DRM_STAT_SECONDARY:
2296 stats->data[i].long_name = "Secondary Bytes";
2297 stats->data[i].rate_name = "SB/s";
2298 SET_BYTE;
2299 break;
2300 case _DRM_STAT_DMA:
2301 stats->data[i].long_name = "DMA";
2302 stats->data[i].rate_name = "DMA/s";
2303 SET_COUNT;
2304 break;
2305 case _DRM_STAT_SPECIAL:
2306 stats->data[i].long_name = "Special DMA";
2307 stats->data[i].rate_name = "dma/s";
2308 SET_COUNT;
2309 break;
2310 case _DRM_STAT_MISSED:
2311 stats->data[i].long_name = "Miss";
2312 stats->data[i].rate_name = "Ms/s";
2313 SET_COUNT;
2314 break;
2315 case _DRM_STAT_VALUE:
2316 stats->data[i].long_name = "Value";
2317 stats->data[i].rate_name = "Value";
2318 SET_VALUE;
2319 break;
2320 case _DRM_STAT_BYTE:
2321 stats->data[i].long_name = "Bytes";
2322 stats->data[i].rate_name = "B/s";
2323 SET_BYTE;
2324 break;
2325 case _DRM_STAT_COUNT:
2326 default:
2327 stats->data[i].long_name = "Count";
2328 stats->data[i].rate_name = "Cnt/s";
2329 SET_COUNT;
2330 break;
2331 }
2332 }
2333 return 0;
2334}
2335
Jose Fonsecad2443b22003-05-27 00:37:33 +00002336/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002337 * Issue a set-version ioctl.
2338 *
2339 * \param fd file descriptor.
2340 * \param drmCommandIndex command index
2341 * \param data source pointer of the data to be read and written.
2342 * \param size size of the data to be read and written.
2343 *
2344 * \return zero on success, or a negative value on failure.
2345 *
2346 * \internal
2347 * It issues a read-write ioctl given by
2348 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2349 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002350int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002351{
2352 int retcode = 0;
2353 drm_set_version_t sv;
2354
2355 sv.drm_di_major = version->drm_di_major;
2356 sv.drm_di_minor = version->drm_di_minor;
2357 sv.drm_dd_major = version->drm_dd_major;
2358 sv.drm_dd_minor = version->drm_dd_minor;
2359
Keith Packard8b9ab102008-06-13 16:03:22 -07002360 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Eric Anholt06cb1322003-10-23 02:23:31 +00002361 retcode = -errno;
2362 }
2363
2364 version->drm_di_major = sv.drm_di_major;
2365 version->drm_di_minor = sv.drm_di_minor;
2366 version->drm_dd_major = sv.drm_dd_major;
2367 version->drm_dd_minor = sv.drm_dd_minor;
2368
2369 return retcode;
2370}
2371
2372/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002373 * Send a device-specific command.
2374 *
2375 * \param fd file descriptor.
2376 * \param drmCommandIndex command index
2377 *
2378 * \return zero on success, or a negative value on failure.
2379 *
2380 * \internal
2381 * It issues a ioctl given by
2382 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2383 */
Jens Owen3903e5a2002-04-09 21:54:56 +00002384int drmCommandNone(int fd, unsigned long drmCommandIndex)
2385{
2386 void *data = NULL; /* dummy */
2387 unsigned long request;
2388
2389 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2390
Keith Packard8b9ab102008-06-13 16:03:22 -07002391 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002392 return -errno;
2393 }
2394 return 0;
2395}
2396
Jose Fonsecad2443b22003-05-27 00:37:33 +00002397
2398/**
2399 * Send a device-specific read command.
2400 *
2401 * \param fd file descriptor.
2402 * \param drmCommandIndex command index
2403 * \param data destination pointer of the data to be read.
2404 * \param size size of the data to be read.
2405 *
2406 * \return zero on success, or a negative value on failure.
2407 *
2408 * \internal
2409 * It issues a read ioctl given by
2410 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2411 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002412int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2413 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002414{
2415 unsigned long request;
2416
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002417 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2418 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002419
Keith Packard8b9ab102008-06-13 16:03:22 -07002420 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002421 return -errno;
2422 }
2423 return 0;
2424}
2425
Jose Fonsecad2443b22003-05-27 00:37:33 +00002426
2427/**
2428 * Send a device-specific write command.
2429 *
2430 * \param fd file descriptor.
2431 * \param drmCommandIndex command index
2432 * \param data source pointer of the data to be written.
2433 * \param size size of the data to be written.
2434 *
2435 * \return zero on success, or a negative value on failure.
2436 *
2437 * \internal
2438 * It issues a write ioctl given by
2439 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2440 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002441int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2442 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002443{
2444 unsigned long request;
2445
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002446 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2447 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002448
Keith Packard8b9ab102008-06-13 16:03:22 -07002449 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002450 return -errno;
2451 }
2452 return 0;
2453}
2454
Jose Fonsecad2443b22003-05-27 00:37:33 +00002455
2456/**
2457 * Send a device-specific read-write command.
2458 *
2459 * \param fd file descriptor.
2460 * \param drmCommandIndex command index
2461 * \param data source pointer of the data to be read and written.
2462 * \param size size of the data to be read and written.
2463 *
2464 * \return zero on success, or a negative value on failure.
2465 *
2466 * \internal
2467 * It issues a read-write ioctl given by
2468 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2469 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002470int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2471 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002472{
2473 unsigned long request;
2474
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002475 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2476 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002477
Keith Packard8b9ab102008-06-13 16:03:22 -07002478 if (drmIoctl(fd, request, data))
Jens Owen3903e5a2002-04-09 21:54:56 +00002479 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002480 return 0;
2481}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002482
Dave Airlied51e1bb2006-11-09 08:55:58 +11002483#define DRM_MAX_FDS 16
2484static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002485 char *BusID;
2486 int fd;
2487 int refcount;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002488} connection[DRM_MAX_FDS];
2489
2490static int nr_fds = 0;
2491
2492int drmOpenOnce(void *unused,
2493 const char *BusID,
2494 int *newlyopened)
2495{
Brianccd7b6e2007-05-29 14:54:00 -06002496 int i;
2497 int fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002498
Brianccd7b6e2007-05-29 14:54:00 -06002499 for (i = 0; i < nr_fds; i++)
2500 if (strcmp(BusID, connection[i].BusID) == 0) {
2501 connection[i].refcount++;
2502 *newlyopened = 0;
2503 return connection[i].fd;
2504 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002505
Brianccd7b6e2007-05-29 14:54:00 -06002506 fd = drmOpen(unused, BusID);
2507 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2508 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002509
Brianccd7b6e2007-05-29 14:54:00 -06002510 connection[nr_fds].BusID = strdup(BusID);
2511 connection[nr_fds].fd = fd;
2512 connection[nr_fds].refcount = 1;
2513 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002514
Brianccd7b6e2007-05-29 14:54:00 -06002515 if (0)
2516 fprintf(stderr, "saved connection %d for %s %d\n",
2517 nr_fds, connection[nr_fds].BusID,
2518 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002519
Brianccd7b6e2007-05-29 14:54:00 -06002520 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002521
Brianccd7b6e2007-05-29 14:54:00 -06002522 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002523}
2524
2525void drmCloseOnce(int fd)
2526{
Brianccd7b6e2007-05-29 14:54:00 -06002527 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002528
Brianccd7b6e2007-05-29 14:54:00 -06002529 for (i = 0; i < nr_fds; i++) {
2530 if (fd == connection[i].fd) {
2531 if (--connection[i].refcount == 0) {
2532 drmClose(connection[i].fd);
2533 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002534
Brianccd7b6e2007-05-29 14:54:00 -06002535 if (i < --nr_fds)
2536 connection[i] = connection[nr_fds];
Dave Airlied51e1bb2006-11-09 08:55:58 +11002537
Brianccd7b6e2007-05-29 14:54:00 -06002538 return;
2539 }
2540 }
2541 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002542}
Jesse Barnes731cd552008-12-17 10:09:49 -08002543
2544int drmSetMaster(int fd)
2545{
Kristian Høgsbergfbc8b2d2009-12-09 10:37:40 -05002546 return ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
Jesse Barnes731cd552008-12-17 10:09:49 -08002547}
2548
2549int drmDropMaster(int fd)
2550{
Kristian Høgsbergfbc8b2d2009-12-09 10:37:40 -05002551 return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
Jesse Barnes731cd552008-12-17 10:09:49 -08002552}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002553
2554char *drmGetDeviceNameFromFd(int fd)
2555{
2556 char name[128];
2557 struct stat sbuf;
2558 dev_t d;
2559 int i;
2560
2561 /* The whole drmOpen thing is a fiasco and we need to find a way
2562 * back to just using open(2). For now, however, lets just make
2563 * things worse with even more ad hoc directory walking code to
2564 * discover the device file name. */
2565
2566 fstat(fd, &sbuf);
2567 d = sbuf.st_rdev;
2568
2569 for (i = 0; i < DRM_MAX_MINOR; i++) {
2570 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2571 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2572 break;
2573 }
2574 if (i == DRM_MAX_MINOR)
2575 return NULL;
2576
Adam Jackson0a1ff352010-10-27 18:44:53 -04002577 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002578}
Dave Airliecc0a1452012-07-14 09:52:17 +00002579
2580int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2581{
2582 struct drm_prime_handle args;
2583 int ret;
2584
2585 args.handle = handle;
2586 args.flags = flags;
2587 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2588 if (ret)
2589 return ret;
2590
2591 *prime_fd = args.fd;
2592 return 0;
2593}
2594
2595int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2596{
2597 struct drm_prime_handle args;
2598 int ret;
2599
2600 args.fd = prime_fd;
2601 args.flags = 0;
2602 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2603 if (ret)
2604 return ret;
2605
2606 *handle = args.handle;
2607 return 0;
2608}
2609