blob: 720952ff2cbd33fd16645af07abace9effe654ee [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>
51#include <sys/mman.h>
52#include <sys/time.h>
53#include <stdarg.h>
Daryll Straussb3a57661999-12-05 01:19:48 +000054
55/* Not all systems have MAP_FAILED defined */
56#ifndef MAP_FAILED
57#define MAP_FAILED ((void *)-1)
58#endif
59
Daryll Straussb3a57661999-12-05 01:19:48 +000060#include "xf86drm.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
89#define DRM_NODE_RENDER 1
90
Dave Airlie79038752006-11-08 15:08:09 +110091static drmServerInfoPtr drm_server_info;
92
93void drmSetServerInfo(drmServerInfoPtr info)
94{
Brianccd7b6e2007-05-29 14:54:00 -060095 drm_server_info = info;
Dave Airlie79038752006-11-08 15:08:09 +110096}
97
Jose Fonsecad2443b22003-05-27 00:37:33 +000098/**
99 * Output a message to stderr.
100 *
101 * \param format printf() like format string.
102 *
103 * \internal
104 * This function is a wrapper around vfprintf().
105 */
Dave Airlie79038752006-11-08 15:08:09 +1100106
107static int drmDebugPrint(const char *format, va_list ap)
108{
Brianccd7b6e2007-05-29 14:54:00 -0600109 return vfprintf(stderr, format, ap);
Dave Airlie79038752006-11-08 15:08:09 +1100110}
111
112static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
113
Eric Anholtc4857422008-06-03 10:20:49 -0700114void
David Dawes56bd9c22001-07-30 19:59:39 +0000115drmMsg(const char *format, ...)
116{
117 va_list ap;
David Dawes56bd9c22001-07-30 19:59:39 +0000118 const char *env;
Dave Airlie79038752006-11-08 15:08:09 +1100119 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
David Dawes56bd9c22001-07-30 19:59:39 +0000120 {
121 va_start(ap, format);
Dave Airlie79038752006-11-08 15:08:09 +1100122 if (drm_server_info) {
123 drm_server_info->debug_print(format,ap);
124 } else {
125 drm_debug_print(format, ap);
126 }
David Dawes56bd9c22001-07-30 19:59:39 +0000127 va_end(ap);
128 }
129}
130
Dave Airlie79038752006-11-08 15:08:09 +1100131void
132drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
133{
Brianccd7b6e2007-05-29 14:54:00 -0600134 drm_debug_print = debug_msg_ptr;
Dave Airlie79038752006-11-08 15:08:09 +1100135}
136
Daryll Straussb3a57661999-12-05 01:19:48 +0000137static void *drmHashTable = NULL; /* Context switch callbacks */
138
Dave Airlie79038752006-11-08 15:08:09 +1100139void *drmGetHashTable(void)
140{
Brianccd7b6e2007-05-29 14:54:00 -0600141 return drmHashTable;
Dave Airlie79038752006-11-08 15:08:09 +1100142}
Daryll Straussb3a57661999-12-05 01:19:48 +0000143
144void *drmMalloc(int size)
145{
146 void *pt;
Brianccd7b6e2007-05-29 14:54:00 -0600147 if ((pt = malloc(size)))
148 memset(pt, 0, size);
Daryll Straussb3a57661999-12-05 01:19:48 +0000149 return pt;
150}
151
152void drmFree(void *pt)
153{
Brianccd7b6e2007-05-29 14:54:00 -0600154 if (pt)
155 free(pt);
Daryll Straussb3a57661999-12-05 01:19:48 +0000156}
157
Keith Packard8b9ab102008-06-13 16:03:22 -0700158/**
159 * Call ioctl, restarting if it is interupted
160 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800161int
Coleman Kane41b83a92008-08-18 17:08:21 -0400162drmIoctl(int fd, unsigned long request, void *arg)
Keith Packard8b9ab102008-06-13 16:03:22 -0700163{
164 int ret;
165
166 do {
167 ret = ioctl(fd, request, arg);
168 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
169 return ret;
170}
Daryll Straussb3a57661999-12-05 01:19:48 +0000171
Daryll Straussb3a57661999-12-05 01:19:48 +0000172static unsigned long drmGetKeyFromFd(int fd)
173{
David Dawesfcc21062001-03-30 17:16:20 +0000174 stat_t st;
Daryll Straussb3a57661999-12-05 01:19:48 +0000175
176 st.st_rdev = 0;
177 fstat(fd, &st);
178 return st.st_rdev;
179}
180
Dave Airlie79038752006-11-08 15:08:09 +1100181drmHashEntry *drmGetEntry(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000182{
183 unsigned long key = drmGetKeyFromFd(fd);
184 void *value;
185 drmHashEntry *entry;
186
Brianccd7b6e2007-05-29 14:54:00 -0600187 if (!drmHashTable)
188 drmHashTable = drmHashCreate();
Daryll Straussb3a57661999-12-05 01:19:48 +0000189
190 if (drmHashLookup(drmHashTable, key, &value)) {
191 entry = drmMalloc(sizeof(*entry));
192 entry->fd = fd;
193 entry->f = NULL;
194 entry->tagTable = drmHashCreate();
195 drmHashInsert(drmHashTable, key, entry);
196 } else {
197 entry = value;
198 }
199 return entry;
200}
201
Jose Fonsecad2443b22003-05-27 00:37:33 +0000202/**
Eric Anholt06cb1322003-10-23 02:23:31 +0000203 * Compare two busid strings
204 *
205 * \param first
206 * \param second
207 *
208 * \return 1 if matched.
209 *
210 * \internal
211 * This function compares two bus ID strings. It understands the older
212 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
213 * domain, b is bus, d is device, f is function.
214 */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000215static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Eric Anholt06cb1322003-10-23 02:23:31 +0000216{
217 /* First, check if the IDs are exactly the same */
218 if (strcasecmp(id1, id2) == 0)
219 return 1;
220
221 /* Try to match old/new-style PCI bus IDs. */
222 if (strncasecmp(id1, "pci", 3) == 0) {
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300223 unsigned int o1, b1, d1, f1;
224 unsigned int o2, b2, d2, f2;
Eric Anholt06cb1322003-10-23 02:23:31 +0000225 int ret;
226
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300227 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000228 if (ret != 4) {
229 o1 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300230 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
Eric Anholt06cb1322003-10-23 02:23:31 +0000231 if (ret != 3)
232 return 0;
233 }
234
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300235 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000236 if (ret != 4) {
237 o2 = 0;
Pauli Nieminen90ae0f22009-07-04 02:18:51 +0300238 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
Eric Anholt06cb1322003-10-23 02:23:31 +0000239 if (ret != 3)
240 return 0;
241 }
242
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000243 /* If domains aren't properly supported by the kernel interface,
244 * just ignore them, which sucks less than picking a totally random
245 * card with "open by name"
246 */
247 if (!pci_domain_ok)
248 o1 = o2 = 0;
249
Eric Anholt06cb1322003-10-23 02:23:31 +0000250 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
251 return 0;
252 else
253 return 1;
254 }
255 return 0;
256}
257
258/**
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300259 * Handles error checking for chown call.
260 *
261 * \param path to file.
262 * \param id of the new owner.
263 * \param id of the new group.
264 *
265 * \return zero if success or -1 if failure.
266 *
267 * \internal
268 * Checks for failure. If failure was caused by signal call chown again.
269 * If any other failure happened then it will output error mesage using
270 * drmMsg() call.
271 */
272static int chown_check_return(const char *path, uid_t owner, gid_t group)
273{
274 int rv;
275
276 do {
277 rv = chown(path, owner, group);
278 } while (rv != 0 && errno == EINTR);
279
280 if (rv == 0)
281 return 0;
282
283 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
284 path, errno, strerror(errno));
285 return -1;
286}
287
288/**
Jose Fonsecad2443b22003-05-27 00:37:33 +0000289 * Open the DRM device, creating it if necessary.
290 *
291 * \param dev major and minor numbers of the device.
292 * \param minor minor number of the device.
293 *
294 * \return a file descriptor on success, or a negative value on error.
295 *
296 * \internal
297 * Assembles the device name from \p minor and opens it, creating the device
298 * special file node with the major and minor numbers specified by \p dev and
299 * parent directory if necessary and was called by root.
300 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800301static int drmOpenDevice(long dev, int minor, int type)
Daryll Straussb3a57661999-12-05 01:19:48 +0000302{
David Dawes9c775d02001-05-14 14:49:58 +0000303 stat_t st;
Rik Faith88dbee52001-02-28 09:27:44 +0000304 char buf[64];
305 int fd;
Dave Airlie79038752006-11-08 15:08:09 +1100306 mode_t devmode = DRM_DEV_MODE, serv_mode;
Rik Faith88dbee52001-02-28 09:27:44 +0000307 int isroot = !geteuid();
Rik Faith88dbee52001-02-28 09:27:44 +0000308 uid_t user = DRM_DEV_UID;
Dave Airlie79038752006-11-08 15:08:09 +1100309 gid_t group = DRM_DEV_GID, serv_group;
310
Jesse Barnes731cd552008-12-17 10:09:49 -0800311 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
Eric Anholt06cb1322003-10-23 02:23:31 +0000312 drmMsg("drmOpenDevice: node name is %s\n", buf);
David Dawes56bd9c22001-07-30 19:59:39 +0000313
Dave Airlie79038752006-11-08 15:08:09 +1100314 if (drm_server_info) {
Brianccd7b6e2007-05-29 14:54:00 -0600315 drm_server_info->get_perms(&serv_group, &serv_mode);
316 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
317 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
318 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
Dave Airlie79038752006-11-08 15:08:09 +1100319 }
Brian Paul569da5a2000-06-08 14:38:22 +0000320
Dave Airlie9101a022008-08-24 16:54:43 +1000321#if !defined(UDEV)
Rik Faith88dbee52001-02-28 09:27:44 +0000322 if (stat(DRM_DIR_NAME, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600323 if (!isroot)
324 return DRM_ERR_NOT_ROOT;
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000325 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300326 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
Alan Hourihaneb3a20ce2002-10-22 23:38:53 +0000327 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Brian Paul569da5a2000-06-08 14:38:22 +0000328 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000329
Eric Anholt06cb1322003-10-23 02:23:31 +0000330 /* Check if the device node exists and create it if necessary. */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000331 if (stat(buf, &st)) {
Brianccd7b6e2007-05-29 14:54:00 -0600332 if (!isroot)
333 return DRM_ERR_NOT_ROOT;
Rik Faith88dbee52001-02-28 09:27:44 +0000334 remove(buf);
335 mknod(buf, S_IFCHR | devmode, dev);
Daryll Straussb3a57661999-12-05 01:19:48 +0000336 }
Dave Airlie79038752006-11-08 15:08:09 +1100337
338 if (drm_server_info) {
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300339 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600340 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100341 }
Dave Airlie9101a022008-08-24 16:54:43 +1000342#else
343 /* if we modprobed then wait for udev */
344 {
345 int udev_count = 0;
346wait_for_udev:
347 if (stat(DRM_DIR_NAME, &st)) {
348 usleep(20);
349 udev_count++;
350
351 if (udev_count == 50)
352 return -1;
353 goto wait_for_udev;
354 }
355
356 if (stat(buf, &st)) {
357 usleep(20);
358 udev_count++;
359
360 if (udev_count == 50)
361 return -1;
362 goto wait_for_udev;
363 }
364 }
365#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000366
David Dawes56bd9c22001-07-30 19:59:39 +0000367 fd = open(buf, O_RDWR, 0);
368 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
369 fd, fd < 0 ? strerror(errno) : "OK");
Brianccd7b6e2007-05-29 14:54:00 -0600370 if (fd >= 0)
371 return fd;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000372
Dave Airlie39e5e982010-12-07 14:26:09 +1000373#if !defined(UDEV)
Eric Anholt06cb1322003-10-23 02:23:31 +0000374 /* Check if the device node is not what we expect it to be, and recreate it
375 * and try again if so.
376 */
Eric Anholtd2f2b422002-08-08 21:23:46 +0000377 if (st.st_rdev != dev) {
Brianccd7b6e2007-05-29 14:54:00 -0600378 if (!isroot)
379 return DRM_ERR_NOT_ROOT;
Eric Anholtd2f2b422002-08-08 21:23:46 +0000380 remove(buf);
381 mknod(buf, S_IFCHR | devmode, dev);
Dave Airlie79038752006-11-08 15:08:09 +1100382 if (drm_server_info) {
Pauli Nieminenc5a5bbb2009-07-06 23:37:20 +0300383 chown_check_return(buf, user, group);
Brianccd7b6e2007-05-29 14:54:00 -0600384 chmod(buf, devmode);
Dave Airlie79038752006-11-08 15:08:09 +1100385 }
Eric Anholtd2f2b422002-08-08 21:23:46 +0000386 }
387 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
David Dawes56bd9c22001-07-30 19:59:39 +0000393 drmMsg("drmOpenDevice: Open failed\n");
Rik Faith88dbee52001-02-28 09:27:44 +0000394 remove(buf);
Dave Airlie39e5e982010-12-07 14:26:09 +1000395#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000396 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000397}
398
Jose Fonsecad2443b22003-05-27 00:37:33 +0000399
400/**
401 * Open the DRM device
402 *
403 * \param minor device minor number.
404 * \param create allow to create the device if set.
405 *
406 * \return a file descriptor on success, or a negative value on error.
407 *
408 * \internal
409 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
410 * name from \p minor and opens it.
411 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800412static int drmOpenMinor(int minor, int create, int type)
Rik Faith88dbee52001-02-28 09:27:44 +0000413{
414 int fd;
415 char buf[64];
Dave Airliedb85ed22008-02-13 12:20:02 +1000416
Brianccd7b6e2007-05-29 14:54:00 -0600417 if (create)
Jesse Barnes731cd552008-12-17 10:09:49 -0800418 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
Rik Faith88dbee52001-02-28 09:27:44 +0000419
Jesse Barnes731cd552008-12-17 10:09:49 -0800420 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
Brianccd7b6e2007-05-29 14:54:00 -0600421 if ((fd = open(buf, O_RDWR, 0)) >= 0)
422 return fd;
Rik Faith88dbee52001-02-28 09:27:44 +0000423 return -errno;
424}
425
Brian Paul569da5a2000-06-08 14:38:22 +0000426
Jose Fonsecad2443b22003-05-27 00:37:33 +0000427/**
428 * Determine whether the DRM kernel driver has been loaded.
429 *
430 * \return 1 if the DRM driver is loaded, 0 otherwise.
431 *
432 * \internal
433 * Determine the presence of the kernel driver by attempting to open the 0
434 * minor and get version information. For backward compatibility with older
435 * Linux implementations, /proc/dri is also checked.
436 */
Brian Paul569da5a2000-06-08 14:38:22 +0000437int drmAvailable(void)
438{
Brian Paul569da5a2000-06-08 14:38:22 +0000439 drmVersionPtr version;
440 int retval = 0;
441 int fd;
Gareth Hughes36047532001-02-15 08:12:14 +0000442
Jesse Barnes731cd552008-12-17 10:09:49 -0800443 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000444#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000445 /* Try proc for backward Linux compatibility */
Brianccd7b6e2007-05-29 14:54:00 -0600446 if (!access("/proc/dri/0", R_OK))
447 return 1;
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000448#endif
Rik Faith88dbee52001-02-28 09:27:44 +0000449 return 0;
Brian Paul569da5a2000-06-08 14:38:22 +0000450 }
Rik Faith88dbee52001-02-28 09:27:44 +0000451
452 if ((version = drmGetVersion(fd))) {
453 retval = 1;
454 drmFreeVersion(version);
455 }
456 close(fd);
Brian Paul569da5a2000-06-08 14:38:22 +0000457
458 return retval;
459}
460
Jose Fonsecad2443b22003-05-27 00:37:33 +0000461
462/**
463 * Open the device by bus ID.
464 *
465 * \param busid bus ID.
466 *
467 * \return a file descriptor on success, or a negative value on error.
468 *
469 * \internal
470 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
471 * comparing the device bus ID with the one supplied.
472 *
473 * \sa drmOpenMinor() and drmGetBusid().
474 */
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000475static int drmOpenByBusid(const char *busid)
476{
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000477 int i, pci_domain_ok = 1;
Rik Faith88dbee52001-02-28 09:27:44 +0000478 int fd;
479 const char *buf;
Eric Anholt06cb1322003-10-23 02:23:31 +0000480 drmSetVersion sv;
481
482 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Dave Airliedb85ed22008-02-13 12:20:02 +1000483 for (i = 0; i < DRM_MAX_MINOR; i++) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800484 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
David Dawes56bd9c22001-07-30 19:59:39 +0000485 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
486 if (fd >= 0) {
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000487 /* We need to try for 1.4 first for proper PCI domain support
488 * and if that fails, we know the kernel is busted
489 */
Eric Anholt06cb1322003-10-23 02:23:31 +0000490 sv.drm_di_major = 1;
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000491 sv.drm_di_minor = 4;
Eric Anholt06cb1322003-10-23 02:23:31 +0000492 sv.drm_dd_major = -1; /* Don't care */
Eric Anholt26462b92005-12-31 11:48:12 +0000493 sv.drm_dd_minor = -1; /* Don't care */
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000494 if (drmSetInterfaceVersion(fd, &sv)) {
495#ifndef __alpha__
496 pci_domain_ok = 0;
497#endif
498 sv.drm_di_major = 1;
499 sv.drm_di_minor = 1;
500 sv.drm_dd_major = -1; /* Don't care */
501 sv.drm_dd_minor = -1; /* Don't care */
502 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n",fd);
503 drmSetInterfaceVersion(fd, &sv);
504 }
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000505 buf = drmGetBusid(fd);
David Dawes56bd9c22001-07-30 19:59:39 +0000506 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
Benjamin Herrenschmidtb04515c2010-08-06 13:55:11 +1000507 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
Rik Faith88dbee52001-02-28 09:27:44 +0000508 drmFreeBusid(buf);
509 return fd;
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000510 }
Brianccd7b6e2007-05-29 14:54:00 -0600511 if (buf)
512 drmFreeBusid(buf);
Daryll Strausse1dba5c1999-12-07 03:37:16 +0000513 close(fd);
514 }
515 }
516 return -1;
517}
518
Jose Fonsecad2443b22003-05-27 00:37:33 +0000519
520/**
521 * Open the device by name.
522 *
523 * \param name driver name.
524 *
525 * \return a file descriptor on success, or a negative value on error.
526 *
527 * \internal
528 * This function opens the first minor number that matches the driver name and
529 * isn't already in use. If it's in use it then it will already have a bus ID
530 * assigned.
531 *
532 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
533 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000534static int drmOpenByName(const char *name)
Daryll Straussb3a57661999-12-05 01:19:48 +0000535{
Rik Faith88dbee52001-02-28 09:27:44 +0000536 int i;
537 int fd;
538 drmVersionPtr version;
David Dawes56bd9c22001-07-30 19:59:39 +0000539 char * id;
Rik Faith88dbee52001-02-28 09:27:44 +0000540
Dave Airliedb85ed22008-02-13 12:20:02 +1000541 if (!drmAvailable()) {
542 if (!drm_server_info) {
543 return -1;
544 }
545 else {
546 /* try to load the kernel module now */
547 if (!drm_server_info->load_module(name)) {
548 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
549 return -1;
550 }
551 }
552 }
553
David Dawes56bd9c22001-07-30 19:59:39 +0000554 /*
555 * Open the first minor number that matches the driver name and isn't
556 * already in use. If it's in use it will have a busid assigned already.
557 */
Dave Airliedb85ed22008-02-13 12:20:02 +1000558 for (i = 0; i < DRM_MAX_MINOR; i++) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800559 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
Rik Faith88dbee52001-02-28 09:27:44 +0000560 if ((version = drmGetVersion(fd))) {
561 if (!strcmp(version->name, name)) {
562 drmFreeVersion(version);
David Dawes56bd9c22001-07-30 19:59:39 +0000563 id = drmGetBusid(fd);
564 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
565 if (!id || !*id) {
Adam Jackson22e41ef2006-02-20 23:09:00 +0000566 if (id)
David Dawes56bd9c22001-07-30 19:59:39 +0000567 drmFreeBusid(id);
David Dawes56bd9c22001-07-30 19:59:39 +0000568 return fd;
569 } else {
570 drmFreeBusid(id);
571 }
572 } else {
573 drmFreeVersion(version);
Rik Faith88dbee52001-02-28 09:27:44 +0000574 }
Rik Faith88dbee52001-02-28 09:27:44 +0000575 }
David Dawes56bd9c22001-07-30 19:59:39 +0000576 close(fd);
Rik Faith88dbee52001-02-28 09:27:44 +0000577 }
578 }
579
580#ifdef __linux__
Adam Jackson22e41ef2006-02-20 23:09:00 +0000581 /* Backward-compatibility /proc support */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000582 for (i = 0; i < 8; i++) {
Rik Faith88dbee52001-02-28 09:27:44 +0000583 char proc_name[64], buf[512];
584 char *driver, *pt, *devstring;
585 int retcode;
586
Daryll Strauss0371c291999-12-18 18:34:59 +0000587 sprintf(proc_name, "/proc/dri/%d/name", i);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000588 if ((fd = open(proc_name, 0, 0)) >= 0) {
589 retcode = read(fd, buf, sizeof(buf)-1);
590 close(fd);
591 if (retcode) {
592 buf[retcode-1] = '\0';
593 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
594 ;
Adam Jackson22e41ef2006-02-20 23:09:00 +0000595 if (*pt) { /* Device is next */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000596 *pt = '\0';
597 if (!strcmp(driver, name)) { /* Match */
598 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
599 ;
600 if (*pt) { /* Found busid */
Rik Faith88dbee52001-02-28 09:27:44 +0000601 return drmOpenByBusid(++pt);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000602 } else { /* No busid */
Jesse Barnes731cd552008-12-17 10:09:49 -0800603 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000604 }
605 }
606 }
607 }
Brian Paul569da5a2000-06-08 14:38:22 +0000608 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000609 }
Rik Faith88dbee52001-02-28 09:27:44 +0000610#endif
611
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000612 return -1;
613}
614
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000615
Jose Fonsecad2443b22003-05-27 00:37:33 +0000616/**
617 * Open the DRM device.
618 *
619 * Looks up the specified name and bus ID, and opens the device found. The
620 * entry in /dev/dri is created if necessary and if called by root.
621 *
622 * \param name driver name. Not referenced if bus ID is supplied.
623 * \param busid bus ID. Zero if not known.
624 *
625 * \return a file descriptor on success, or a negative value on error.
626 *
627 * \internal
628 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
629 * otherwise.
630 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000631int drmOpen(const char *name, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000632{
Dave Airlie79038752006-11-08 15:08:09 +1100633 if (!drmAvailable() && name != NULL && drm_server_info) {
Eric Anholt06cb1322003-10-23 02:23:31 +0000634 /* try to load the kernel */
Dave Airlie79038752006-11-08 15:08:09 +1100635 if (!drm_server_info->load_module(name)) {
Brianccd7b6e2007-05-29 14:54:00 -0600636 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
Eric Anholt06cb1322003-10-23 02:23:31 +0000637 return -1;
638 }
639 }
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000640
Eric Anholt06cb1322003-10-23 02:23:31 +0000641 if (busid) {
Brianccd7b6e2007-05-29 14:54:00 -0600642 int fd = drmOpenByBusid(busid);
Eric Anholt06cb1322003-10-23 02:23:31 +0000643 if (fd >= 0)
644 return fd;
645 }
Adam Jackson22e41ef2006-02-20 23:09:00 +0000646
Eric Anholt06cb1322003-10-23 02:23:31 +0000647 if (name)
648 return drmOpenByName(name);
Adam Jackson22e41ef2006-02-20 23:09:00 +0000649
Eric Anholt06cb1322003-10-23 02:23:31 +0000650 return -1;
Daryll Straussb3a57661999-12-05 01:19:48 +0000651}
652
Jesse Barnes731cd552008-12-17 10:09:49 -0800653int drmOpenControl(int minor)
654{
655 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
656}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000657
658/**
659 * Free the version information returned by drmGetVersion().
660 *
661 * \param v pointer to the version information.
662 *
663 * \internal
664 * It frees the memory pointed by \p %v as well as all the non-null strings
665 * pointers in it.
666 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000667void drmFreeVersion(drmVersionPtr v)
668{
Brianccd7b6e2007-05-29 14:54:00 -0600669 if (!v)
670 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100671 drmFree(v->name);
672 drmFree(v->date);
673 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000674 drmFree(v);
675}
676
Jose Fonsecad2443b22003-05-27 00:37:33 +0000677
678/**
679 * Free the non-public version information returned by the kernel.
680 *
681 * \param v pointer to the version information.
682 *
683 * \internal
684 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
685 * the non-null strings pointers in it.
686 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000687static void drmFreeKernelVersion(drm_version_t *v)
688{
Brianccd7b6e2007-05-29 14:54:00 -0600689 if (!v)
690 return;
Jakob Bornecrantz9d8ba2d2007-02-25 10:48:26 +1100691 drmFree(v->name);
692 drmFree(v->date);
693 drmFree(v->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000694 drmFree(v);
695}
696
Jose Fonsecad2443b22003-05-27 00:37:33 +0000697
698/**
699 * Copy version information.
700 *
701 * \param d destination pointer.
702 * \param s source pointer.
703 *
704 * \internal
705 * Used by drmGetVersion() to translate the information returned by the ioctl
706 * interface in a private structure into the public structure counterpart.
707 */
Brian Paul569da5a2000-06-08 14:38:22 +0000708static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Daryll Straussb3a57661999-12-05 01:19:48 +0000709{
710 d->version_major = s->version_major;
711 d->version_minor = s->version_minor;
712 d->version_patchlevel = s->version_patchlevel;
713 d->name_len = s->name_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400714 d->name = strdup(s->name);
Daryll Straussb3a57661999-12-05 01:19:48 +0000715 d->date_len = s->date_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400716 d->date = strdup(s->date);
Daryll Straussb3a57661999-12-05 01:19:48 +0000717 d->desc_len = s->desc_len;
Adam Jackson0a1ff352010-10-27 18:44:53 -0400718 d->desc = strdup(s->desc);
Daryll Straussb3a57661999-12-05 01:19:48 +0000719}
720
Daryll Straussb3a57661999-12-05 01:19:48 +0000721
Jose Fonsecad2443b22003-05-27 00:37:33 +0000722/**
723 * Query the driver version information.
724 *
725 * \param fd file descriptor.
726 *
727 * \return pointer to a drmVersion structure which should be freed with
728 * drmFreeVersion().
729 *
730 * \note Similar information is available via /proc/dri.
731 *
732 * \internal
733 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
734 * first with zeros to get the string lengths, and then the actually strings.
735 * It also null-terminates them since they might not be already.
736 */
Daryll Straussb3a57661999-12-05 01:19:48 +0000737drmVersionPtr drmGetVersion(int fd)
738{
739 drmVersionPtr retval;
740 drm_version_t *version = drmMalloc(sizeof(*version));
741
Daryll Straussb3a57661999-12-05 01:19:48 +0000742 version->name_len = 0;
743 version->name = NULL;
744 version->date_len = 0;
745 version->date = NULL;
746 version->desc_len = 0;
747 version->desc = NULL;
Gareth Hughes36047532001-02-15 08:12:14 +0000748
Keith Packard8b9ab102008-06-13 16:03:22 -0700749 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000750 drmFreeKernelVersion(version);
751 return NULL;
752 }
753
Daryll Straussb3a57661999-12-05 01:19:48 +0000754 if (version->name_len)
755 version->name = drmMalloc(version->name_len + 1);
756 if (version->date_len)
757 version->date = drmMalloc(version->date_len + 1);
758 if (version->desc_len)
759 version->desc = drmMalloc(version->desc_len + 1);
Gareth Hughes36047532001-02-15 08:12:14 +0000760
Keith Packard8b9ab102008-06-13 16:03:22 -0700761 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Alan Hourihaneb0a92852003-09-24 14:39:25 +0000762 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
Daryll Straussb3a57661999-12-05 01:19:48 +0000763 drmFreeKernelVersion(version);
764 return NULL;
765 }
766
Adam Jackson22e41ef2006-02-20 23:09:00 +0000767 /* The results might not be null-terminated strings, so terminate them. */
Daryll Straussb3a57661999-12-05 01:19:48 +0000768 if (version->name_len) version->name[version->name_len] = '\0';
769 if (version->date_len) version->date[version->date_len] = '\0';
770 if (version->desc_len) version->desc[version->desc_len] = '\0';
771
Daryll Straussb3a57661999-12-05 01:19:48 +0000772 retval = drmMalloc(sizeof(*retval));
773 drmCopyVersion(retval, version);
774 drmFreeKernelVersion(version);
775 return retval;
776}
777
Jens Owen3903e5a2002-04-09 21:54:56 +0000778
Jose Fonsecad2443b22003-05-27 00:37:33 +0000779/**
780 * Get version information for the DRM user space library.
781 *
782 * This version number is driver independent.
783 *
784 * \param fd file descriptor.
785 *
786 * \return version information.
787 *
788 * \internal
789 * This function allocates and fills a drm_version structure with a hard coded
790 * version number.
791 */
Jens Owen3903e5a2002-04-09 21:54:56 +0000792drmVersionPtr drmGetLibVersion(int fd)
793{
794 drm_version_t *version = drmMalloc(sizeof(*version));
795
796 /* Version history:
Dave Airlie79038752006-11-08 15:08:09 +1100797 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Jens Owen3903e5a2002-04-09 21:54:56 +0000798 * revision 1.0.x = original DRM interface with no drmGetLibVersion
799 * entry point and many drm<Device> extensions
800 * revision 1.1.x = added drmCommand entry points for device extensions
801 * added drmGetLibVersion to identify libdrm.a version
Eric Anholt06cb1322003-10-23 02:23:31 +0000802 * revision 1.2.x = added drmSetInterfaceVersion
803 * modified drmOpen to handle both busid and name
Dave Airlie79038752006-11-08 15:08:09 +1100804 * revision 1.3.x = added server + memory manager
Jens Owen3903e5a2002-04-09 21:54:56 +0000805 */
Dave Airlie79038752006-11-08 15:08:09 +1100806 version->version_major = 1;
807 version->version_minor = 3;
Jens Owen3903e5a2002-04-09 21:54:56 +0000808 version->version_patchlevel = 0;
809
810 return (drmVersionPtr)version;
811}
812
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000813int drmGetCap(int fd, uint64_t capability, uint64_t *value)
814{
815 struct drm_get_cap cap = { capability, 0 };
816 int ret;
817
818 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
819 if (ret)
820 return ret;
821
822 *value = cap.value;
Dave Airlie3b04c732011-03-04 15:48:31 +1000823 return 0;
Ben Skeggs5c6c6912011-02-21 11:27:19 +1000824}
Jose Fonsecad2443b22003-05-27 00:37:33 +0000825
Damien Lespiauddbbdb12013-09-03 15:34:41 +0100826int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
827{
828 struct drm_set_client_cap cap = { capability, value };
829
830 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
831}
832
Jose Fonsecad2443b22003-05-27 00:37:33 +0000833/**
834 * Free the bus ID information.
835 *
836 * \param busid bus ID information string as given by drmGetBusid().
837 *
838 * \internal
839 * This function is just frees the memory pointed by \p busid.
840 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000841void drmFreeBusid(const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000842{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000843 drmFree((void *)busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000844}
845
Jose Fonsecad2443b22003-05-27 00:37:33 +0000846
847/**
848 * Get the bus ID of the device.
849 *
850 * \param fd file descriptor.
851 *
852 * \return bus ID string.
853 *
854 * \internal
855 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
856 * get the string length and data, passing the arguments in a drm_unique
857 * structure.
858 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000859char *drmGetBusid(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +0000860{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000861 drm_unique_t u;
862
863 u.unique_len = 0;
864 u.unique = NULL;
865
Keith Packard8b9ab102008-06-13 16:03:22 -0700866 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600867 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000868 u.unique = drmMalloc(u.unique_len + 1);
Keith Packard8b9ab102008-06-13 16:03:22 -0700869 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Brianccd7b6e2007-05-29 14:54:00 -0600870 return NULL;
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000871 u.unique[u.unique_len] = '\0';
Eric Anholt06cb1322003-10-23 02:23:31 +0000872
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000873 return u.unique;
Daryll Straussb3a57661999-12-05 01:19:48 +0000874}
875
Jose Fonsecad2443b22003-05-27 00:37:33 +0000876
877/**
878 * Set the bus ID of the device.
879 *
880 * \param fd file descriptor.
881 * \param busid bus ID string.
882 *
883 * \return zero on success, negative on failure.
884 *
885 * \internal
886 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
887 * the arguments in a drm_unique structure.
888 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000889int drmSetBusid(int fd, const char *busid)
Daryll Straussb3a57661999-12-05 01:19:48 +0000890{
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000891 drm_unique_t u;
Daryll Straussb3a57661999-12-05 01:19:48 +0000892
Daryll Straussb6a28bf1999-12-05 23:10:37 +0000893 u.unique = (char *)busid;
894 u.unique_len = strlen(busid);
Daryll Straussb3a57661999-12-05 01:19:48 +0000895
Keith Packard8b9ab102008-06-13 16:03:22 -0700896 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
David Dawes56bd9c22001-07-30 19:59:39 +0000897 return -errno;
898 }
Daryll Straussb3a57661999-12-05 01:19:48 +0000899 return 0;
900}
901
Jon Smirl8696e712004-07-07 04:36:36 +0000902int drmGetMagic(int fd, drm_magic_t * magic)
Daryll Straussb3a57661999-12-05 01:19:48 +0000903{
904 drm_auth_t auth;
905
906 *magic = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -0700907 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -0600908 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000909 *magic = auth.magic;
910 return 0;
911}
912
Jon Smirl8696e712004-07-07 04:36:36 +0000913int drmAuthMagic(int fd, drm_magic_t magic)
Daryll Straussb3a57661999-12-05 01:19:48 +0000914{
915 drm_auth_t auth;
916
917 auth.magic = magic;
Keith Packard8b9ab102008-06-13 16:03:22 -0700918 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Brianccd7b6e2007-05-29 14:54:00 -0600919 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +0000920 return 0;
921}
922
Jose Fonsecad2443b22003-05-27 00:37:33 +0000923/**
924 * Specifies a range of memory that is available for mapping by a
925 * non-root process.
926 *
927 * \param fd file descriptor.
928 * \param offset usually the physical address. The actual meaning depends of
929 * the \p type parameter. See below.
930 * \param size of the memory in bytes.
931 * \param type type of the memory to be mapped.
932 * \param flags combination of several flags to modify the function actions.
933 * \param handle will be set to a value that may be used as the offset
934 * parameter for mmap().
935 *
936 * \return zero on success or a negative value on error.
937 *
938 * \par Mapping the frame buffer
939 * For the frame buffer
940 * - \p offset will be the physical address of the start of the frame buffer,
941 * - \p size will be the size of the frame buffer in bytes, and
942 * - \p type will be DRM_FRAME_BUFFER.
943 *
944 * \par
945 * The area mapped will be uncached. If MTRR support is available in the
946 * kernel, the frame buffer area will be set to write combining.
947 *
948 * \par Mapping the MMIO register area
949 * For the MMIO register area,
950 * - \p offset will be the physical address of the start of the register area,
951 * - \p size will be the size of the register area bytes, and
952 * - \p type will be DRM_REGISTERS.
953 * \par
954 * The area mapped will be uncached.
955 *
956 * \par Mapping the SAREA
957 * For the SAREA,
958 * - \p offset will be ignored and should be set to zero,
959 * - \p size will be the desired size of the SAREA in bytes,
960 * - \p type will be DRM_SHM.
961 *
962 * \par
963 * A shared memory area of the requested size will be created and locked in
964 * kernel memory. This area may be mapped into client-space by using the handle
965 * returned.
966 *
967 * \note May only be called by root.
968 *
969 * \internal
970 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
971 * the arguments in a drm_map structure.
972 */
Adam Jackson22e41ef2006-02-20 23:09:00 +0000973int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
974 drmMapFlags flags, drm_handle_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +0000975{
976 drm_map_t map;
977
978 map.offset = offset;
979 map.size = size;
980 map.handle = 0;
981 map.type = type;
982 map.flags = flags;
Keith Packard8b9ab102008-06-13 16:03:22 -0700983 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -0600984 return -errno;
985 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -0700986 *handle = (drm_handle_t)(uintptr_t)map.handle;
Daryll Straussb3a57661999-12-05 01:19:48 +0000987 return 0;
988}
989
Jon Smirl8696e712004-07-07 04:36:36 +0000990int drmRmMap(int fd, drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +0000991{
992 drm_map_t map;
993
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -0700994 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +0000995
Keith Packard8b9ab102008-06-13 16:03:22 -0700996 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -0600997 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +0000998 return 0;
999}
1000
Jose Fonsecad2443b22003-05-27 00:37:33 +00001001/**
1002 * Make buffers available for DMA transfers.
1003 *
1004 * \param fd file descriptor.
1005 * \param count number of buffers.
1006 * \param size size of each buffer.
1007 * \param flags buffer allocation flags.
1008 * \param agp_offset offset in the AGP aperture
1009 *
1010 * \return number of buffers allocated, negative on error.
1011 *
1012 * \internal
1013 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1014 *
1015 * \sa drm_buf_desc.
1016 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001017int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1018 int agp_offset)
Daryll Straussb3a57661999-12-05 01:19:48 +00001019{
1020 drm_buf_desc_t request;
Gareth Hughes36047532001-02-15 08:12:14 +00001021
Daryll Straussb3a57661999-12-05 01:19:48 +00001022 request.count = count;
1023 request.size = size;
1024 request.low_mark = 0;
1025 request.high_mark = 0;
1026 request.flags = flags;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001027 request.agp_start = agp_offset;
Gareth Hughes36047532001-02-15 08:12:14 +00001028
Keith Packard8b9ab102008-06-13 16:03:22 -07001029 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001030 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001031 return request.count;
1032}
1033
1034int drmMarkBufs(int fd, double low, double high)
1035{
1036 drm_buf_info_t info;
1037 int i;
1038
1039 info.count = 0;
1040 info.list = NULL;
1041
Keith Packard8b9ab102008-06-13 16:03:22 -07001042 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001043 return -EINVAL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001044
Brianccd7b6e2007-05-29 14:54:00 -06001045 if (!info.count)
1046 return -EINVAL;
Gareth Hughes36047532001-02-15 08:12:14 +00001047
Daryll Straussb3a57661999-12-05 01:19:48 +00001048 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1049 return -ENOMEM;
Gareth Hughes36047532001-02-15 08:12:14 +00001050
Keith Packard8b9ab102008-06-13 16:03:22 -07001051 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001052 int retval = -errno;
1053 drmFree(info.list);
1054 return retval;
1055 }
Gareth Hughes36047532001-02-15 08:12:14 +00001056
Daryll Straussb3a57661999-12-05 01:19:48 +00001057 for (i = 0; i < info.count; i++) {
1058 info.list[i].low_mark = low * info.list[i].count;
1059 info.list[i].high_mark = high * info.list[i].count;
Keith Packard8b9ab102008-06-13 16:03:22 -07001060 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001061 int retval = -errno;
1062 drmFree(info.list);
1063 return retval;
1064 }
1065 }
1066 drmFree(info.list);
Gareth Hughes36047532001-02-15 08:12:14 +00001067
Daryll Straussb3a57661999-12-05 01:19:48 +00001068 return 0;
1069}
1070
Jose Fonsecad2443b22003-05-27 00:37:33 +00001071/**
1072 * Free buffers.
1073 *
1074 * \param fd file descriptor.
1075 * \param count number of buffers to free.
1076 * \param list list of buffers to be freed.
1077 *
1078 * \return zero on success, or a negative value on failure.
1079 *
1080 * \note This function is primarily used for debugging.
1081 *
1082 * \internal
1083 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1084 * the arguments in a drm_buf_free structure.
1085 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001086int drmFreeBufs(int fd, int count, int *list)
1087{
1088 drm_buf_free_t request;
1089
1090 request.count = count;
1091 request.list = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001092 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Brianccd7b6e2007-05-29 14:54:00 -06001093 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001094 return 0;
1095}
1096
Jose Fonsecad2443b22003-05-27 00:37:33 +00001097
1098/**
1099 * Close the device.
1100 *
1101 * \param fd file descriptor.
1102 *
1103 * \internal
1104 * This function closes the file descriptor.
1105 */
Daryll Straussb6a28bf1999-12-05 23:10:37 +00001106int drmClose(int fd)
Daryll Straussb3a57661999-12-05 01:19:48 +00001107{
1108 unsigned long key = drmGetKeyFromFd(fd);
1109 drmHashEntry *entry = drmGetEntry(fd);
1110
1111 drmHashDestroy(entry->tagTable);
1112 entry->fd = 0;
1113 entry->f = NULL;
1114 entry->tagTable = NULL;
1115
1116 drmHashDelete(drmHashTable, key);
1117 drmFree(entry);
1118
1119 return close(fd);
1120}
1121
Jose Fonsecad2443b22003-05-27 00:37:33 +00001122
1123/**
1124 * Map a region of memory.
1125 *
1126 * \param fd file descriptor.
1127 * \param handle handle returned by drmAddMap().
1128 * \param size size in bytes. Must match the size used by drmAddMap().
1129 * \param address will contain the user-space virtual address where the mapping
1130 * begins.
1131 *
1132 * \return zero on success, or a negative value on failure.
1133 *
1134 * \internal
1135 * This function is a wrapper for mmap().
1136 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001137int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
Daryll Straussb3a57661999-12-05 01:19:48 +00001138{
Alan Hourihanec7558d82000-09-24 09:34:10 +00001139 static unsigned long pagesize_mask = 0;
1140
Brianccd7b6e2007-05-29 14:54:00 -06001141 if (fd < 0)
1142 return -EINVAL;
Alan Hourihanec7558d82000-09-24 09:34:10 +00001143
1144 if (!pagesize_mask)
1145 pagesize_mask = getpagesize() - 1;
1146
1147 size = (size + pagesize_mask) & ~pagesize_mask;
1148
Daryll Straussb3a57661999-12-05 01:19:48 +00001149 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Brianccd7b6e2007-05-29 14:54:00 -06001150 if (*address == MAP_FAILED)
1151 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001152 return 0;
1153}
1154
Jose Fonsecad2443b22003-05-27 00:37:33 +00001155
1156/**
1157 * Unmap mappings obtained with drmMap().
1158 *
1159 * \param address address as given by drmMap().
1160 * \param size size in bytes. Must match the size used by drmMap().
1161 *
1162 * \return zero on success, or a negative value on failure.
1163 *
1164 * \internal
Adam Jackson22e41ef2006-02-20 23:09:00 +00001165 * This function is a wrapper for munmap().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001166 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001167int drmUnmap(drmAddress address, drmSize size)
1168{
1169 return munmap(address, size);
1170}
1171
1172drmBufInfoPtr drmGetBufInfo(int fd)
1173{
1174 drm_buf_info_t info;
1175 drmBufInfoPtr retval;
1176 int i;
1177
1178 info.count = 0;
1179 info.list = NULL;
1180
Keith Packard8b9ab102008-06-13 16:03:22 -07001181 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Brianccd7b6e2007-05-29 14:54:00 -06001182 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001183
1184 if (info.count) {
1185 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1186 return NULL;
Gareth Hughes36047532001-02-15 08:12:14 +00001187
Keith Packard8b9ab102008-06-13 16:03:22 -07001188 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001189 drmFree(info.list);
1190 return NULL;
1191 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001192
Daryll Straussb3a57661999-12-05 01:19:48 +00001193 retval = drmMalloc(sizeof(*retval));
1194 retval->count = info.count;
1195 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1196 for (i = 0; i < info.count; i++) {
1197 retval->list[i].count = info.list[i].count;
1198 retval->list[i].size = info.list[i].size;
1199 retval->list[i].low_mark = info.list[i].low_mark;
1200 retval->list[i].high_mark = info.list[i].high_mark;
1201 }
1202 drmFree(info.list);
1203 return retval;
1204 }
1205 return NULL;
1206}
1207
Jose Fonsecad2443b22003-05-27 00:37:33 +00001208/**
1209 * Map all DMA buffers into client-virtual space.
1210 *
1211 * \param fd file descriptor.
1212 *
1213 * \return a pointer to a ::drmBufMap structure.
1214 *
1215 * \note The client may not use these buffers until obtaining buffer indices
1216 * with drmDMA().
1217 *
1218 * \internal
1219 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1220 * information about the buffers in a drm_buf_map structure into the
1221 * client-visible data structures.
1222 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001223drmBufMapPtr drmMapBufs(int fd)
1224{
1225 drm_buf_map_t bufs;
1226 drmBufMapPtr retval;
1227 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001228
Daryll Straussb3a57661999-12-05 01:19:48 +00001229 bufs.count = 0;
1230 bufs.list = NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001231 bufs.virtual = NULL;
Keith Packard8b9ab102008-06-13 16:03:22 -07001232 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Brianccd7b6e2007-05-29 14:54:00 -06001233 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001234
Brianccd7b6e2007-05-29 14:54:00 -06001235 if (!bufs.count)
1236 return NULL;
Jon Smirl8696e712004-07-07 04:36:36 +00001237
Daryll Straussb3a57661999-12-05 01:19:48 +00001238 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1239 return NULL;
1240
Keith Packard8b9ab102008-06-13 16:03:22 -07001241 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
Daryll Straussb3a57661999-12-05 01:19:48 +00001242 drmFree(bufs.list);
1243 return NULL;
1244 }
Adam Jackson22e41ef2006-02-20 23:09:00 +00001245
Daryll Straussb3a57661999-12-05 01:19:48 +00001246 retval = drmMalloc(sizeof(*retval));
1247 retval->count = bufs.count;
1248 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1249 for (i = 0; i < bufs.count; i++) {
1250 retval->list[i].idx = bufs.list[i].idx;
1251 retval->list[i].total = bufs.list[i].total;
1252 retval->list[i].used = 0;
1253 retval->list[i].address = bufs.list[i].address;
1254 }
Jon Smirl8696e712004-07-07 04:36:36 +00001255
1256 drmFree(bufs.list);
1257
Daryll Straussb3a57661999-12-05 01:19:48 +00001258 return retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001259}
1260
Jose Fonsecad2443b22003-05-27 00:37:33 +00001261
1262/**
1263 * Unmap buffers allocated with drmMapBufs().
1264 *
1265 * \return zero on success, or negative value on failure.
1266 *
1267 * \internal
Jon Smirl8696e712004-07-07 04:36:36 +00001268 * Calls munmap() for every buffer stored in \p bufs and frees the
1269 * memory allocated by drmMapBufs().
Jose Fonsecad2443b22003-05-27 00:37:33 +00001270 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001271int drmUnmapBufs(drmBufMapPtr bufs)
1272{
1273 int i;
Gareth Hughes36047532001-02-15 08:12:14 +00001274
Daryll Straussb3a57661999-12-05 01:19:48 +00001275 for (i = 0; i < bufs->count; i++) {
1276 munmap(bufs->list[i].address, bufs->list[i].total);
1277 }
Jon Smirl8696e712004-07-07 04:36:36 +00001278
1279 drmFree(bufs->list);
1280 drmFree(bufs);
1281
Daryll Straussb3a57661999-12-05 01:19:48 +00001282 return 0;
1283}
1284
Jose Fonsecad2443b22003-05-27 00:37:33 +00001285
Gareth Hughes36047532001-02-15 08:12:14 +00001286#define DRM_DMA_RETRY 16
1287
Jose Fonsecad2443b22003-05-27 00:37:33 +00001288/**
1289 * Reserve DMA buffers.
1290 *
1291 * \param fd file descriptor.
1292 * \param request
1293 *
1294 * \return zero on success, or a negative value on failure.
1295 *
1296 * \internal
1297 * Assemble the arguments into a drm_dma structure and keeps issuing the
1298 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1299 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001300int drmDMA(int fd, drmDMAReqPtr request)
1301{
1302 drm_dma_t dma;
Gareth Hughes36047532001-02-15 08:12:14 +00001303 int ret, i = 0;
Daryll Straussb3a57661999-12-05 01:19:48 +00001304
Daryll Straussb3a57661999-12-05 01:19:48 +00001305 dma.context = request->context;
1306 dma.send_count = request->send_count;
1307 dma.send_indices = request->send_list;
1308 dma.send_sizes = request->send_sizes;
1309 dma.flags = request->flags;
1310 dma.request_count = request->request_count;
1311 dma.request_size = request->request_size;
1312 dma.request_indices = request->request_list;
1313 dma.request_sizes = request->request_sizes;
Jon Smirl8696e712004-07-07 04:36:36 +00001314 dma.granted_count = 0;
Gareth Hughes36047532001-02-15 08:12:14 +00001315
1316 do {
1317 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1318 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1319
1320 if ( ret == 0 ) {
1321 request->granted_count = dma.granted_count;
1322 return 0;
1323 } else {
1324 return -errno;
1325 }
Daryll Straussb3a57661999-12-05 01:19:48 +00001326}
1327
Jose Fonsecad2443b22003-05-27 00:37:33 +00001328
1329/**
1330 * Obtain heavyweight hardware lock.
1331 *
1332 * \param fd file descriptor.
1333 * \param context context.
1334 * \param flags flags that determine the sate of the hardware when the function
1335 * returns.
1336 *
1337 * \return always zero.
1338 *
1339 * \internal
1340 * This function translates the arguments into a drm_lock structure and issue
1341 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1342 */
Jon Smirl8696e712004-07-07 04:36:36 +00001343int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001344{
1345 drm_lock_t lock;
1346
1347 lock.context = context;
1348 lock.flags = 0;
1349 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1350 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1351 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1352 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1353 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1354 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Gareth Hughes36047532001-02-15 08:12:14 +00001355
Keith Packard8b9ab102008-06-13 16:03:22 -07001356 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Daryll Straussb3a57661999-12-05 01:19:48 +00001357 ;
1358 return 0;
1359}
1360
Jose Fonsecad2443b22003-05-27 00:37:33 +00001361/**
1362 * Release the hardware lock.
1363 *
1364 * \param fd file descriptor.
1365 * \param context context.
1366 *
1367 * \return zero on success, or a negative value on failure.
1368 *
1369 * \internal
1370 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1371 * argument in a drm_lock structure.
1372 */
Jon Smirl8696e712004-07-07 04:36:36 +00001373int drmUnlock(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001374{
1375 drm_lock_t lock;
1376
1377 lock.context = context;
1378 lock.flags = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001379 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Daryll Straussb3a57661999-12-05 01:19:48 +00001380}
1381
Adam Jackson22e41ef2006-02-20 23:09:00 +00001382drm_context_t *drmGetReservedContextList(int fd, int *count)
Daryll Straussb3a57661999-12-05 01:19:48 +00001383{
1384 drm_ctx_res_t res;
1385 drm_ctx_t *list;
Jon Smirl8696e712004-07-07 04:36:36 +00001386 drm_context_t * retval;
Daryll Straussb3a57661999-12-05 01:19:48 +00001387 int i;
1388
1389 res.count = 0;
1390 res.contexts = NULL;
Keith Packard8b9ab102008-06-13 16:03:22 -07001391 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001392 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001393
Brianccd7b6e2007-05-29 14:54:00 -06001394 if (!res.count)
1395 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001396
Brianccd7b6e2007-05-29 14:54:00 -06001397 if (!(list = drmMalloc(res.count * sizeof(*list))))
1398 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001399 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1400 drmFree(list);
1401 return NULL;
1402 }
1403
1404 res.contexts = list;
Keith Packard8b9ab102008-06-13 16:03:22 -07001405 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Brianccd7b6e2007-05-29 14:54:00 -06001406 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00001407
Brianccd7b6e2007-05-29 14:54:00 -06001408 for (i = 0; i < res.count; i++)
1409 retval[i] = list[i].handle;
Daryll Straussb3a57661999-12-05 01:19:48 +00001410 drmFree(list);
1411
1412 *count = res.count;
1413 return retval;
1414}
1415
Adam Jackson22e41ef2006-02-20 23:09:00 +00001416void drmFreeReservedContextList(drm_context_t *pt)
Daryll Straussb3a57661999-12-05 01:19:48 +00001417{
1418 drmFree(pt);
1419}
1420
Jose Fonsecad2443b22003-05-27 00:37:33 +00001421/**
1422 * Create context.
1423 *
1424 * Used by the X server during GLXContext initialization. This causes
1425 * per-context kernel-level resources to be allocated.
1426 *
1427 * \param fd file descriptor.
1428 * \param handle is set on success. To be used by the client when requesting DMA
1429 * dispatch with drmDMA().
1430 *
1431 * \return zero on success, or a negative value on failure.
1432 *
1433 * \note May only be called by root.
1434 *
1435 * \internal
1436 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1437 * argument in a drm_ctx structure.
1438 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00001439int drmCreateContext(int fd, drm_context_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001440{
1441 drm_ctx_t ctx;
1442
1443 ctx.flags = 0; /* Modified with functions below */
Keith Packard8b9ab102008-06-13 16:03:22 -07001444 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001445 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001446 *handle = ctx.handle;
1447 return 0;
1448}
1449
Jon Smirl8696e712004-07-07 04:36:36 +00001450int drmSwitchToContext(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00001451{
1452 drm_ctx_t ctx;
1453
1454 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001455 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001456 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001457 return 0;
1458}
1459
Jon Smirl8696e712004-07-07 04:36:36 +00001460int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001461{
1462 drm_ctx_t ctx;
1463
Adam Jackson22e41ef2006-02-20 23:09:00 +00001464 /*
1465 * Context preserving means that no context switches are done between DMA
1466 * buffers from one context and the next. This is suitable for use in the
1467 * X server (which promises to maintain hardware context), or in the
1468 * client-side library when buffers are swapped on behalf of two threads.
1469 */
Daryll Straussb3a57661999-12-05 01:19:48 +00001470 ctx.handle = context;
1471 ctx.flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001472 if (flags & DRM_CONTEXT_PRESERVED)
1473 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1474 if (flags & DRM_CONTEXT_2DONLY)
1475 ctx.flags |= _DRM_CONTEXT_2DONLY;
Keith Packard8b9ab102008-06-13 16:03:22 -07001476 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001477 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001478 return 0;
1479}
1480
Adam Jackson22e41ef2006-02-20 23:09:00 +00001481int drmGetContextFlags(int fd, drm_context_t context,
1482 drm_context_tFlagsPtr flags)
Daryll Straussb3a57661999-12-05 01:19:48 +00001483{
1484 drm_ctx_t ctx;
1485
1486 ctx.handle = context;
Keith Packard8b9ab102008-06-13 16:03:22 -07001487 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001488 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001489 *flags = 0;
Brianccd7b6e2007-05-29 14:54:00 -06001490 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1491 *flags |= DRM_CONTEXT_PRESERVED;
1492 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1493 *flags |= DRM_CONTEXT_2DONLY;
Daryll Straussb3a57661999-12-05 01:19:48 +00001494 return 0;
1495}
Gareth Hughes36047532001-02-15 08:12:14 +00001496
Jose Fonsecad2443b22003-05-27 00:37:33 +00001497/**
1498 * Destroy context.
1499 *
1500 * Free any kernel-level resources allocated with drmCreateContext() associated
1501 * with the context.
1502 *
1503 * \param fd file descriptor.
1504 * \param handle handle given by drmCreateContext().
1505 *
1506 * \return zero on success, or a negative value on failure.
1507 *
1508 * \note May only be called by root.
1509 *
1510 * \internal
1511 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1512 * argument in a drm_ctx structure.
1513 */
Jon Smirl8696e712004-07-07 04:36:36 +00001514int drmDestroyContext(int fd, drm_context_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001515{
1516 drm_ctx_t ctx;
1517 ctx.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001518 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Brianccd7b6e2007-05-29 14:54:00 -06001519 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001520 return 0;
1521}
1522
Adam Jackson22e41ef2006-02-20 23:09:00 +00001523int drmCreateDrawable(int fd, drm_drawable_t *handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001524{
1525 drm_draw_t draw;
Keith Packard8b9ab102008-06-13 16:03:22 -07001526 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001527 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001528 *handle = draw.handle;
1529 return 0;
1530}
1531
Jon Smirl8696e712004-07-07 04:36:36 +00001532int drmDestroyDrawable(int fd, drm_drawable_t handle)
Daryll Straussb3a57661999-12-05 01:19:48 +00001533{
1534 drm_draw_t draw;
1535 draw.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001536 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Brianccd7b6e2007-05-29 14:54:00 -06001537 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00001538 return 0;
1539}
1540
Michel Dänzer9810ec22006-08-22 16:40:07 +02001541int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1542 drm_drawable_info_type_t type, unsigned int num,
1543 void *data)
1544{
1545 drm_update_draw_t update;
1546
1547 update.handle = handle;
1548 update.type = type;
1549 update.num = num;
1550 update.data = (unsigned long long)(unsigned long)data;
1551
Keith Packard8b9ab102008-06-13 16:03:22 -07001552 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Brianccd7b6e2007-05-29 14:54:00 -06001553 return -errno;
Michel Dänzer9810ec22006-08-22 16:40:07 +02001554
1555 return 0;
1556}
1557
Jose Fonsecad2443b22003-05-27 00:37:33 +00001558/**
1559 * Acquire the AGP device.
1560 *
1561 * Must be called before any of the other AGP related calls.
1562 *
1563 * \param fd file descriptor.
1564 *
1565 * \return zero on success, or a negative value on failure.
1566 *
1567 * \internal
1568 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1569 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001570int drmAgpAcquire(int fd)
1571{
Keith Packard8b9ab102008-06-13 16:03:22 -07001572 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001573 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001574 return 0;
1575}
1576
Jose Fonsecad2443b22003-05-27 00:37:33 +00001577
1578/**
1579 * Release the AGP device.
1580 *
1581 * \param fd file descriptor.
1582 *
1583 * \return zero on success, or a negative value on failure.
1584 *
1585 * \internal
1586 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1587 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001588int drmAgpRelease(int fd)
1589{
Keith Packard8b9ab102008-06-13 16:03:22 -07001590 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Brianccd7b6e2007-05-29 14:54:00 -06001591 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001592 return 0;
1593}
1594
Jose Fonsecad2443b22003-05-27 00:37:33 +00001595
1596/**
1597 * Set the AGP mode.
1598 *
1599 * \param fd file descriptor.
1600 * \param mode AGP mode.
1601 *
1602 * \return zero on success, or a negative value on failure.
1603 *
1604 * \internal
1605 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1606 * argument in a drm_agp_mode structure.
1607 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001608int drmAgpEnable(int fd, unsigned long mode)
1609{
1610 drm_agp_mode_t m;
1611
1612 m.mode = mode;
Keith Packard8b9ab102008-06-13 16:03:22 -07001613 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Brianccd7b6e2007-05-29 14:54:00 -06001614 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001615 return 0;
1616}
1617
Jose Fonsecad2443b22003-05-27 00:37:33 +00001618
1619/**
1620 * Allocate a chunk of AGP memory.
1621 *
1622 * \param fd file descriptor.
1623 * \param size requested memory size in bytes. Will be rounded to page boundary.
1624 * \param type type of memory to allocate.
1625 * \param address if not zero, will be set to the physical address of the
1626 * allocated memory.
1627 * \param handle on success will be set to a handle of the allocated memory.
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_ALLOC ioctl, passing the
1633 * arguments in a drm_agp_buffer structure.
1634 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001635int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Dave Airlie7ede2092005-11-29 09:50:47 +00001636 unsigned long *address, drm_handle_t *handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001637{
1638 drm_agp_buffer_t b;
Alan Hourihaneb0a92852003-09-24 14:39:25 +00001639
1640 *handle = DRM_AGP_NO_HANDLE;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001641 b.size = size;
1642 b.handle = 0;
1643 b.type = type;
Keith Packard8b9ab102008-06-13 16:03:22 -07001644 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001645 return -errno;
1646 if (address != 0UL)
1647 *address = b.physical;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001648 *handle = b.handle;
1649 return 0;
1650}
1651
Jose Fonsecad2443b22003-05-27 00:37:33 +00001652
1653/**
1654 * Free a chunk of AGP memory.
1655 *
1656 * \param fd file descriptor.
1657 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1658 *
1659 * \return zero on success, or a negative value on failure.
1660 *
1661 * \internal
1662 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1663 * argument in a drm_agp_buffer structure.
1664 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001665int drmAgpFree(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001666{
1667 drm_agp_buffer_t b;
1668
1669 b.size = 0;
1670 b.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001671 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001672 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001673 return 0;
1674}
1675
Jose Fonsecad2443b22003-05-27 00:37:33 +00001676
1677/**
1678 * Bind a chunk of AGP memory.
1679 *
1680 * \param fd file descriptor.
1681 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1682 * \param offset offset in bytes. It will round to page boundary.
1683 *
1684 * \return zero on success, or a negative value on failure.
1685 *
1686 * \internal
1687 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1688 * argument in a drm_agp_binding structure.
1689 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001690int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001691{
1692 drm_agp_binding_t b;
1693
1694 b.handle = handle;
1695 b.offset = offset;
Keith Packard8b9ab102008-06-13 16:03:22 -07001696 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001697 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001698 return 0;
1699}
1700
Jose Fonsecad2443b22003-05-27 00:37:33 +00001701
1702/**
1703 * Unbind a chunk of AGP memory.
1704 *
1705 * \param fd file descriptor.
1706 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1707 *
1708 * \return zero on success, or a negative value on failure.
1709 *
1710 * \internal
1711 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1712 * the argument in a drm_agp_binding structure.
1713 */
Dave Airlie7ede2092005-11-29 09:50:47 +00001714int drmAgpUnbind(int fd, drm_handle_t handle)
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001715{
1716 drm_agp_binding_t b;
1717
1718 b.handle = handle;
1719 b.offset = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001720 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Brianccd7b6e2007-05-29 14:54:00 -06001721 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001722 return 0;
1723}
1724
Jose Fonsecad2443b22003-05-27 00:37:33 +00001725
1726/**
1727 * Get AGP driver major version number.
1728 *
1729 * \param fd file descriptor.
1730 *
1731 * \return major version number on success, or a negative value on failure..
1732 *
1733 * \internal
1734 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1735 * necessary information in a drm_agp_info structure.
1736 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001737int drmAgpVersionMajor(int fd)
1738{
1739 drm_agp_info_t i;
1740
Keith Packard8b9ab102008-06-13 16:03:22 -07001741 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001742 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001743 return i.agp_version_major;
1744}
1745
Jose Fonsecad2443b22003-05-27 00:37:33 +00001746
1747/**
1748 * Get AGP driver minor version number.
1749 *
1750 * \param fd file descriptor.
1751 *
1752 * \return minor version number on success, or a negative value on failure.
1753 *
1754 * \internal
1755 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1756 * necessary information in a drm_agp_info structure.
1757 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001758int drmAgpVersionMinor(int fd)
1759{
1760 drm_agp_info_t i;
1761
Keith Packard8b9ab102008-06-13 16:03:22 -07001762 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001763 return -errno;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001764 return i.agp_version_minor;
1765}
1766
Jose Fonsecad2443b22003-05-27 00:37:33 +00001767
1768/**
1769 * Get AGP mode.
1770 *
1771 * \param fd file descriptor.
1772 *
1773 * \return mode on success, or zero on failure.
1774 *
1775 * \internal
1776 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1777 * necessary information in a drm_agp_info structure.
1778 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001779unsigned long drmAgpGetMode(int fd)
1780{
1781 drm_agp_info_t i;
1782
Keith Packard8b9ab102008-06-13 16:03:22 -07001783 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001784 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001785 return i.mode;
1786}
1787
Jose Fonsecad2443b22003-05-27 00:37:33 +00001788
1789/**
1790 * Get AGP aperture base.
1791 *
1792 * \param fd file descriptor.
1793 *
1794 * \return aperture base on success, zero on failure.
1795 *
1796 * \internal
1797 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1798 * necessary information in a drm_agp_info structure.
1799 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001800unsigned long drmAgpBase(int fd)
1801{
1802 drm_agp_info_t i;
1803
Keith Packard8b9ab102008-06-13 16:03:22 -07001804 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001805 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001806 return i.aperture_base;
1807}
1808
Jose Fonsecad2443b22003-05-27 00:37:33 +00001809
1810/**
1811 * Get AGP aperture size.
1812 *
1813 * \param fd file descriptor.
1814 *
1815 * \return aperture size on success, zero on failure.
1816 *
1817 * \internal
1818 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1819 * necessary information in a drm_agp_info structure.
1820 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001821unsigned long drmAgpSize(int fd)
1822{
1823 drm_agp_info_t i;
1824
Keith Packard8b9ab102008-06-13 16:03:22 -07001825 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001826 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001827 return i.aperture_size;
1828}
1829
Jose Fonsecad2443b22003-05-27 00:37:33 +00001830
1831/**
1832 * Get used AGP memory.
1833 *
1834 * \param fd file descriptor.
1835 *
1836 * \return memory used on success, or zero on failure.
1837 *
1838 * \internal
1839 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1840 * necessary information in a drm_agp_info structure.
1841 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001842unsigned long drmAgpMemoryUsed(int fd)
1843{
1844 drm_agp_info_t i;
1845
Keith Packard8b9ab102008-06-13 16:03:22 -07001846 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001847 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001848 return i.memory_used;
1849}
1850
Jose Fonsecad2443b22003-05-27 00:37:33 +00001851
1852/**
1853 * Get available AGP memory.
1854 *
1855 * \param fd file descriptor.
1856 *
1857 * \return memory available on success, or zero on failure.
1858 *
1859 * \internal
1860 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1861 * necessary information in a drm_agp_info structure.
1862 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001863unsigned long drmAgpMemoryAvail(int fd)
1864{
1865 drm_agp_info_t i;
1866
Keith Packard8b9ab102008-06-13 16:03:22 -07001867 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001868 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001869 return i.memory_allowed;
1870}
1871
Jose Fonsecad2443b22003-05-27 00:37:33 +00001872
1873/**
1874 * Get hardware vendor ID.
1875 *
1876 * \param fd file descriptor.
1877 *
1878 * \return vendor ID on success, or zero on failure.
1879 *
1880 * \internal
1881 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1882 * necessary information in a drm_agp_info structure.
1883 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001884unsigned int drmAgpVendorId(int fd)
1885{
1886 drm_agp_info_t i;
1887
Keith Packard8b9ab102008-06-13 16:03:22 -07001888 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001889 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001890 return i.id_vendor;
1891}
1892
Jose Fonsecad2443b22003-05-27 00:37:33 +00001893
1894/**
1895 * Get hardware device ID.
1896 *
1897 * \param fd file descriptor.
1898 *
1899 * \return zero on success, or zero on failure.
1900 *
1901 * \internal
1902 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1903 * necessary information in a drm_agp_info structure.
1904 */
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001905unsigned int drmAgpDeviceId(int fd)
1906{
1907 drm_agp_info_t i;
1908
Keith Packard8b9ab102008-06-13 16:03:22 -07001909 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Brianccd7b6e2007-05-29 14:54:00 -06001910 return 0;
Jeff Hartmannba1b1ae2000-04-04 22:08:14 +00001911 return i.id_device;
1912}
1913
Dave Airlie7ede2092005-11-29 09:50:47 +00001914int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001915{
1916 drm_scatter_gather_t sg;
1917
1918 *handle = 0;
1919 sg.size = size;
1920 sg.handle = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07001921 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06001922 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001923 *handle = sg.handle;
1924 return 0;
1925}
1926
Dave Airlie7ede2092005-11-29 09:50:47 +00001927int drmScatterGatherFree(int fd, drm_handle_t handle)
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001928{
1929 drm_scatter_gather_t sg;
1930
1931 sg.size = 0;
1932 sg.handle = handle;
Keith Packard8b9ab102008-06-13 16:03:22 -07001933 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Brianccd7b6e2007-05-29 14:54:00 -06001934 return -errno;
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +00001935 return 0;
1936}
1937
Jose Fonsecad2443b22003-05-27 00:37:33 +00001938/**
1939 * Wait for VBLANK.
1940 *
1941 * \param fd file descriptor.
1942 * \param vbl pointer to a drmVBlank structure.
1943 *
1944 * \return zero on success, or a negative value on failure.
1945 *
1946 * \internal
1947 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1948 */
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001949int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1950{
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001951 struct timespec timeout, cur;
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001952 int ret;
1953
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001954 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1955 if (ret < 0) {
Daniel Kurtz1eb28602013-03-28 14:05:40 +08001956 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001957 goto out;
1958 }
1959 timeout.tv_sec++;
1960
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001961 do {
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001962 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Michel Daenzerc7d471b2003-02-02 03:06:47 +00001963 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Jesse Barnesca370772009-01-07 10:48:26 -08001964 if (ret && errno == EINTR) {
1965 clock_gettime(CLOCK_MONOTONIC, &cur);
1966 /* Timeout after 1s */
1967 if (cur.tv_sec > timeout.tv_sec + 1 ||
1968 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1969 timeout.tv_nsec)) {
1970 errno = EBUSY;
1971 ret = -1;
1972 break;
1973 }
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001974 }
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001975 } while (ret && errno == EINTR);
1976
Jesse Barnesf4f76a62009-01-07 10:18:08 -08001977out:
Michel Daenzer55acd0d2002-09-25 17:18:19 +00001978 return ret;
1979}
1980
Daryll Straussb3a57661999-12-05 01:19:48 +00001981int drmError(int err, const char *label)
1982{
1983 switch (err) {
Brianccd7b6e2007-05-29 14:54:00 -06001984 case DRM_ERR_NO_DEVICE:
1985 fprintf(stderr, "%s: no device\n", label);
1986 break;
1987 case DRM_ERR_NO_ACCESS:
1988 fprintf(stderr, "%s: no access\n", label);
1989 break;
1990 case DRM_ERR_NOT_ROOT:
1991 fprintf(stderr, "%s: not root\n", label);
1992 break;
1993 case DRM_ERR_INVALID:
1994 fprintf(stderr, "%s: invalid args\n", label);
1995 break;
Daryll Straussb3a57661999-12-05 01:19:48 +00001996 default:
Brianccd7b6e2007-05-29 14:54:00 -06001997 if (err < 0)
1998 err = -err;
Daryll Straussb3a57661999-12-05 01:19:48 +00001999 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2000 break;
2001 }
2002
2003 return 1;
2004}
2005
Jose Fonsecad2443b22003-05-27 00:37:33 +00002006/**
2007 * Install IRQ handler.
2008 *
2009 * \param fd file descriptor.
2010 * \param irq IRQ number.
2011 *
2012 * \return zero on success, or a negative value on failure.
2013 *
2014 * \internal
2015 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2016 * argument in a drm_control structure.
2017 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002018int drmCtlInstHandler(int fd, int irq)
2019{
2020 drm_control_t ctl;
2021
2022 ctl.func = DRM_INST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002023 ctl.irq = irq;
Keith Packard8b9ab102008-06-13 16:03:22 -07002024 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002025 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002026 return 0;
2027}
2028
Jose Fonsecad2443b22003-05-27 00:37:33 +00002029
2030/**
2031 * Uninstall IRQ handler.
2032 *
2033 * \param fd file descriptor.
2034 *
2035 * \return zero on success, or a negative value on failure.
2036 *
2037 * \internal
2038 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2039 * argument in a drm_control structure.
2040 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002041int drmCtlUninstHandler(int fd)
2042{
2043 drm_control_t ctl;
2044
2045 ctl.func = DRM_UNINST_HANDLER;
Daryll Straussb3a57661999-12-05 01:19:48 +00002046 ctl.irq = 0;
Keith Packard8b9ab102008-06-13 16:03:22 -07002047 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Brianccd7b6e2007-05-29 14:54:00 -06002048 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002049 return 0;
2050}
2051
2052int drmFinish(int fd, int context, drmLockFlags flags)
2053{
2054 drm_lock_t lock;
2055
2056 lock.context = context;
2057 lock.flags = 0;
2058 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2059 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2060 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2061 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2062 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2063 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Keith Packard8b9ab102008-06-13 16:03:22 -07002064 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Brianccd7b6e2007-05-29 14:54:00 -06002065 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002066 return 0;
2067}
2068
Jose Fonsecad2443b22003-05-27 00:37:33 +00002069/**
2070 * Get IRQ from bus ID.
2071 *
2072 * \param fd file descriptor.
2073 * \param busnum bus number.
2074 * \param devnum device number.
2075 * \param funcnum function number.
2076 *
2077 * \return IRQ number on success, or a negative value on failure.
2078 *
2079 * \internal
2080 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2081 * arguments in a drm_irq_busid structure.
2082 */
Daryll Straussb3a57661999-12-05 01:19:48 +00002083int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2084{
2085 drm_irq_busid_t p;
2086
2087 p.busnum = busnum;
2088 p.devnum = devnum;
2089 p.funcnum = funcnum;
Keith Packard8b9ab102008-06-13 16:03:22 -07002090 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Brianccd7b6e2007-05-29 14:54:00 -06002091 return -errno;
Daryll Straussb3a57661999-12-05 01:19:48 +00002092 return p.irq;
2093}
2094
Jon Smirl8696e712004-07-07 04:36:36 +00002095int drmAddContextTag(int fd, drm_context_t context, void *tag)
Daryll Straussb3a57661999-12-05 01:19:48 +00002096{
2097 drmHashEntry *entry = drmGetEntry(fd);
2098
2099 if (drmHashInsert(entry->tagTable, context, tag)) {
2100 drmHashDelete(entry->tagTable, context);
2101 drmHashInsert(entry->tagTable, context, tag);
2102 }
2103 return 0;
2104}
2105
Jon Smirl8696e712004-07-07 04:36:36 +00002106int drmDelContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002107{
2108 drmHashEntry *entry = drmGetEntry(fd);
2109
2110 return drmHashDelete(entry->tagTable, context);
2111}
2112
Jon Smirl8696e712004-07-07 04:36:36 +00002113void *drmGetContextTag(int fd, drm_context_t context)
Daryll Straussb3a57661999-12-05 01:19:48 +00002114{
2115 drmHashEntry *entry = drmGetEntry(fd);
2116 void *value;
Gareth Hughes36047532001-02-15 08:12:14 +00002117
Brianccd7b6e2007-05-29 14:54:00 -06002118 if (drmHashLookup(entry->tagTable, context, &value))
2119 return NULL;
Daryll Straussb3a57661999-12-05 01:19:48 +00002120
2121 return value;
2122}
2123
Adam Jackson22e41ef2006-02-20 23:09:00 +00002124int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2125 drm_handle_t handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002126{
2127 drm_ctx_priv_map_t map;
2128
2129 map.ctx_id = ctx_id;
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002130 map.handle = (void *)(uintptr_t)handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002131
Keith Packard8b9ab102008-06-13 16:03:22 -07002132 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002133 return -errno;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002134 return 0;
2135}
2136
Adam Jackson22e41ef2006-02-20 23:09:00 +00002137int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2138 drm_handle_t *handle)
Kevin E Martin74e19a42001-03-14 22:22:50 +00002139{
2140 drm_ctx_priv_map_t map;
2141
2142 map.ctx_id = ctx_id;
2143
Keith Packard8b9ab102008-06-13 16:03:22 -07002144 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002145 return -errno;
2146 if (handle)
Jeremy Huddleston961bf9b2011-11-01 14:42:13 -07002147 *handle = (drm_handle_t)(uintptr_t)map.handle;
Kevin E Martin74e19a42001-03-14 22:22:50 +00002148
2149 return 0;
2150}
2151
Jon Smirl8696e712004-07-07 04:36:36 +00002152int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2153 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
Rik Faith88dbee52001-02-28 09:27:44 +00002154 int *mtrr)
2155{
2156 drm_map_t map;
2157
2158 map.offset = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002159 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Brianccd7b6e2007-05-29 14:54:00 -06002160 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002161 *offset = map.offset;
2162 *size = map.size;
2163 *type = map.type;
2164 *flags = map.flags;
2165 *handle = (unsigned long)map.handle;
2166 *mtrr = map.mtrr;
2167 return 0;
2168}
2169
2170int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2171 unsigned long *magic, unsigned long *iocs)
2172{
2173 drm_client_t client;
2174
2175 client.idx = idx;
Keith Packard8b9ab102008-06-13 16:03:22 -07002176 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Brianccd7b6e2007-05-29 14:54:00 -06002177 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002178 *auth = client.auth;
2179 *pid = client.pid;
2180 *uid = client.uid;
2181 *magic = client.magic;
2182 *iocs = client.iocs;
2183 return 0;
2184}
2185
2186int drmGetStats(int fd, drmStatsT *stats)
2187{
2188 drm_stats_t s;
2189 int i;
2190
Keith Packard8b9ab102008-06-13 16:03:22 -07002191 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Brianccd7b6e2007-05-29 14:54:00 -06002192 return -errno;
Rik Faith88dbee52001-02-28 09:27:44 +00002193
2194 stats->count = 0;
2195 memset(stats, 0, sizeof(*stats));
2196 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2197 return -1;
2198
2199#define SET_VALUE \
2200 stats->data[i].long_format = "%-20.20s"; \
2201 stats->data[i].rate_format = "%8.8s"; \
2202 stats->data[i].isvalue = 1; \
2203 stats->data[i].verbose = 0
2204
2205#define SET_COUNT \
2206 stats->data[i].long_format = "%-20.20s"; \
2207 stats->data[i].rate_format = "%5.5s"; \
2208 stats->data[i].isvalue = 0; \
2209 stats->data[i].mult_names = "kgm"; \
2210 stats->data[i].mult = 1000; \
2211 stats->data[i].verbose = 0
2212
2213#define SET_BYTE \
2214 stats->data[i].long_format = "%-20.20s"; \
2215 stats->data[i].rate_format = "%5.5s"; \
2216 stats->data[i].isvalue = 0; \
2217 stats->data[i].mult_names = "KGM"; \
2218 stats->data[i].mult = 1024; \
2219 stats->data[i].verbose = 0
2220
2221
2222 stats->count = s.count;
2223 for (i = 0; i < s.count; i++) {
2224 stats->data[i].value = s.data[i].value;
2225 switch (s.data[i].type) {
2226 case _DRM_STAT_LOCK:
2227 stats->data[i].long_name = "Lock";
2228 stats->data[i].rate_name = "Lock";
2229 SET_VALUE;
2230 break;
2231 case _DRM_STAT_OPENS:
2232 stats->data[i].long_name = "Opens";
2233 stats->data[i].rate_name = "O";
2234 SET_COUNT;
2235 stats->data[i].verbose = 1;
2236 break;
2237 case _DRM_STAT_CLOSES:
2238 stats->data[i].long_name = "Closes";
2239 stats->data[i].rate_name = "Lock";
2240 SET_COUNT;
2241 stats->data[i].verbose = 1;
2242 break;
2243 case _DRM_STAT_IOCTLS:
2244 stats->data[i].long_name = "Ioctls";
2245 stats->data[i].rate_name = "Ioc/s";
2246 SET_COUNT;
2247 break;
2248 case _DRM_STAT_LOCKS:
2249 stats->data[i].long_name = "Locks";
2250 stats->data[i].rate_name = "Lck/s";
2251 SET_COUNT;
2252 break;
2253 case _DRM_STAT_UNLOCKS:
2254 stats->data[i].long_name = "Unlocks";
2255 stats->data[i].rate_name = "Unl/s";
2256 SET_COUNT;
2257 break;
2258 case _DRM_STAT_IRQ:
2259 stats->data[i].long_name = "IRQs";
2260 stats->data[i].rate_name = "IRQ/s";
2261 SET_COUNT;
2262 break;
2263 case _DRM_STAT_PRIMARY:
2264 stats->data[i].long_name = "Primary Bytes";
2265 stats->data[i].rate_name = "PB/s";
2266 SET_BYTE;
2267 break;
2268 case _DRM_STAT_SECONDARY:
2269 stats->data[i].long_name = "Secondary Bytes";
2270 stats->data[i].rate_name = "SB/s";
2271 SET_BYTE;
2272 break;
2273 case _DRM_STAT_DMA:
2274 stats->data[i].long_name = "DMA";
2275 stats->data[i].rate_name = "DMA/s";
2276 SET_COUNT;
2277 break;
2278 case _DRM_STAT_SPECIAL:
2279 stats->data[i].long_name = "Special DMA";
2280 stats->data[i].rate_name = "dma/s";
2281 SET_COUNT;
2282 break;
2283 case _DRM_STAT_MISSED:
2284 stats->data[i].long_name = "Miss";
2285 stats->data[i].rate_name = "Ms/s";
2286 SET_COUNT;
2287 break;
2288 case _DRM_STAT_VALUE:
2289 stats->data[i].long_name = "Value";
2290 stats->data[i].rate_name = "Value";
2291 SET_VALUE;
2292 break;
2293 case _DRM_STAT_BYTE:
2294 stats->data[i].long_name = "Bytes";
2295 stats->data[i].rate_name = "B/s";
2296 SET_BYTE;
2297 break;
2298 case _DRM_STAT_COUNT:
2299 default:
2300 stats->data[i].long_name = "Count";
2301 stats->data[i].rate_name = "Cnt/s";
2302 SET_COUNT;
2303 break;
2304 }
2305 }
2306 return 0;
2307}
2308
Jose Fonsecad2443b22003-05-27 00:37:33 +00002309/**
Eric Anholt06cb1322003-10-23 02:23:31 +00002310 * Issue a set-version ioctl.
2311 *
2312 * \param fd file descriptor.
2313 * \param drmCommandIndex command index
2314 * \param data source pointer of the data to be read and written.
2315 * \param size size of the data to be read and written.
2316 *
2317 * \return zero on success, or a negative value on failure.
2318 *
2319 * \internal
2320 * It issues a read-write ioctl given by
2321 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2322 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002323int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Eric Anholt06cb1322003-10-23 02:23:31 +00002324{
2325 int retcode = 0;
2326 drm_set_version_t sv;
2327
2328 sv.drm_di_major = version->drm_di_major;
2329 sv.drm_di_minor = version->drm_di_minor;
2330 sv.drm_dd_major = version->drm_dd_major;
2331 sv.drm_dd_minor = version->drm_dd_minor;
2332
Keith Packard8b9ab102008-06-13 16:03:22 -07002333 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Eric Anholt06cb1322003-10-23 02:23:31 +00002334 retcode = -errno;
2335 }
2336
2337 version->drm_di_major = sv.drm_di_major;
2338 version->drm_di_minor = sv.drm_di_minor;
2339 version->drm_dd_major = sv.drm_dd_major;
2340 version->drm_dd_minor = sv.drm_dd_minor;
2341
2342 return retcode;
2343}
2344
2345/**
Jose Fonsecad2443b22003-05-27 00:37:33 +00002346 * Send a device-specific command.
2347 *
2348 * \param fd file descriptor.
2349 * \param drmCommandIndex command index
2350 *
2351 * \return zero on success, or a negative value on failure.
2352 *
2353 * \internal
2354 * It issues a ioctl given by
2355 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2356 */
Jens Owen3903e5a2002-04-09 21:54:56 +00002357int drmCommandNone(int fd, unsigned long drmCommandIndex)
2358{
2359 void *data = NULL; /* dummy */
2360 unsigned long request;
2361
2362 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2363
Keith Packard8b9ab102008-06-13 16:03:22 -07002364 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002365 return -errno;
2366 }
2367 return 0;
2368}
2369
Jose Fonsecad2443b22003-05-27 00:37:33 +00002370
2371/**
2372 * Send a device-specific read command.
2373 *
2374 * \param fd file descriptor.
2375 * \param drmCommandIndex command index
2376 * \param data destination pointer of the data to be read.
2377 * \param size size of the data to be read.
2378 *
2379 * \return zero on success, or a negative value on failure.
2380 *
2381 * \internal
2382 * It issues a read ioctl given by
2383 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2384 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002385int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2386 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002387{
2388 unsigned long request;
2389
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002390 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2391 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002392
Keith Packard8b9ab102008-06-13 16:03:22 -07002393 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002394 return -errno;
2395 }
2396 return 0;
2397}
2398
Jose Fonsecad2443b22003-05-27 00:37:33 +00002399
2400/**
2401 * Send a device-specific write command.
2402 *
2403 * \param fd file descriptor.
2404 * \param drmCommandIndex command index
2405 * \param data source pointer of the data to be written.
2406 * \param size size of the data to be written.
2407 *
2408 * \return zero on success, or a negative value on failure.
2409 *
2410 * \internal
2411 * It issues a write ioctl given by
2412 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2413 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002414int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2415 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002416{
2417 unsigned long request;
2418
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002419 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2420 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002421
Keith Packard8b9ab102008-06-13 16:03:22 -07002422 if (drmIoctl(fd, request, data)) {
Jens Owen3903e5a2002-04-09 21:54:56 +00002423 return -errno;
2424 }
2425 return 0;
2426}
2427
Jose Fonsecad2443b22003-05-27 00:37:33 +00002428
2429/**
2430 * Send a device-specific read-write command.
2431 *
2432 * \param fd file descriptor.
2433 * \param drmCommandIndex command index
2434 * \param data source pointer of the data to be read and written.
2435 * \param size size of the data to be read and written.
2436 *
2437 * \return zero on success, or a negative value on failure.
2438 *
2439 * \internal
2440 * It issues a read-write ioctl given by
2441 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2442 */
Adam Jackson22e41ef2006-02-20 23:09:00 +00002443int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2444 unsigned long size)
Jens Owen3903e5a2002-04-09 21:54:56 +00002445{
2446 unsigned long request;
2447
Alan Hourihane74ef13f2002-07-05 08:31:11 +00002448 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2449 DRM_COMMAND_BASE + drmCommandIndex, size);
Jens Owen3903e5a2002-04-09 21:54:56 +00002450
Keith Packard8b9ab102008-06-13 16:03:22 -07002451 if (drmIoctl(fd, request, data))
Jens Owen3903e5a2002-04-09 21:54:56 +00002452 return -errno;
Jens Owen3903e5a2002-04-09 21:54:56 +00002453 return 0;
2454}
Thomas Hellstrom166da932006-08-21 21:02:08 +02002455
Dave Airlied51e1bb2006-11-09 08:55:58 +11002456#define DRM_MAX_FDS 16
2457static struct {
Brianccd7b6e2007-05-29 14:54:00 -06002458 char *BusID;
2459 int fd;
2460 int refcount;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002461} connection[DRM_MAX_FDS];
2462
2463static int nr_fds = 0;
2464
2465int drmOpenOnce(void *unused,
2466 const char *BusID,
2467 int *newlyopened)
2468{
Brianccd7b6e2007-05-29 14:54:00 -06002469 int i;
2470 int fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002471
Brianccd7b6e2007-05-29 14:54:00 -06002472 for (i = 0; i < nr_fds; i++)
2473 if (strcmp(BusID, connection[i].BusID) == 0) {
2474 connection[i].refcount++;
2475 *newlyopened = 0;
2476 return connection[i].fd;
2477 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002478
Brianccd7b6e2007-05-29 14:54:00 -06002479 fd = drmOpen(unused, BusID);
2480 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2481 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002482
Brianccd7b6e2007-05-29 14:54:00 -06002483 connection[nr_fds].BusID = strdup(BusID);
2484 connection[nr_fds].fd = fd;
2485 connection[nr_fds].refcount = 1;
2486 *newlyopened = 1;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002487
Brianccd7b6e2007-05-29 14:54:00 -06002488 if (0)
2489 fprintf(stderr, "saved connection %d for %s %d\n",
2490 nr_fds, connection[nr_fds].BusID,
2491 strcmp(BusID, connection[nr_fds].BusID));
Dave Airlied51e1bb2006-11-09 08:55:58 +11002492
Brianccd7b6e2007-05-29 14:54:00 -06002493 nr_fds++;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002494
Brianccd7b6e2007-05-29 14:54:00 -06002495 return fd;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002496}
2497
2498void drmCloseOnce(int fd)
2499{
Brianccd7b6e2007-05-29 14:54:00 -06002500 int i;
Dave Airlied51e1bb2006-11-09 08:55:58 +11002501
Brianccd7b6e2007-05-29 14:54:00 -06002502 for (i = 0; i < nr_fds; i++) {
2503 if (fd == connection[i].fd) {
2504 if (--connection[i].refcount == 0) {
2505 drmClose(connection[i].fd);
2506 free(connection[i].BusID);
Dave Airlied51e1bb2006-11-09 08:55:58 +11002507
Brianccd7b6e2007-05-29 14:54:00 -06002508 if (i < --nr_fds)
2509 connection[i] = connection[nr_fds];
Dave Airlied51e1bb2006-11-09 08:55:58 +11002510
Brianccd7b6e2007-05-29 14:54:00 -06002511 return;
2512 }
2513 }
2514 }
Dave Airlied51e1bb2006-11-09 08:55:58 +11002515}
Jesse Barnes731cd552008-12-17 10:09:49 -08002516
2517int drmSetMaster(int fd)
2518{
Kristian Høgsbergfbc8b2d2009-12-09 10:37:40 -05002519 return ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
Jesse Barnes731cd552008-12-17 10:09:49 -08002520}
2521
2522int drmDropMaster(int fd)
2523{
Kristian Høgsbergfbc8b2d2009-12-09 10:37:40 -05002524 return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
Jesse Barnes731cd552008-12-17 10:09:49 -08002525}
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002526
2527char *drmGetDeviceNameFromFd(int fd)
2528{
2529 char name[128];
2530 struct stat sbuf;
2531 dev_t d;
2532 int i;
2533
2534 /* The whole drmOpen thing is a fiasco and we need to find a way
2535 * back to just using open(2). For now, however, lets just make
2536 * things worse with even more ad hoc directory walking code to
2537 * discover the device file name. */
2538
2539 fstat(fd, &sbuf);
2540 d = sbuf.st_rdev;
2541
2542 for (i = 0; i < DRM_MAX_MINOR; i++) {
2543 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2544 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2545 break;
2546 }
2547 if (i == DRM_MAX_MINOR)
2548 return NULL;
2549
Adam Jackson0a1ff352010-10-27 18:44:53 -04002550 return strdup(name);
Kristian Høgsberg22d46662009-11-23 20:51:34 -05002551}
Dave Airliecc0a1452012-07-14 09:52:17 +00002552
2553int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2554{
2555 struct drm_prime_handle args;
2556 int ret;
2557
2558 args.handle = handle;
2559 args.flags = flags;
2560 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2561 if (ret)
2562 return ret;
2563
2564 *prime_fd = args.fd;
2565 return 0;
2566}
2567
2568int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2569{
2570 struct drm_prime_handle args;
2571 int ret;
2572
2573 args.fd = prime_fd;
2574 args.flags = 0;
2575 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2576 if (ret)
2577 return ret;
2578
2579 *handle = args.handle;
2580 return 0;
2581}
2582