75
|
1 |
/**
|
|
2 |
* \file xf86drm.c
|
|
3 |
* User-level interface to DRM device
|
|
4 |
*
|
|
5 |
* \author Rickard E. (Rik) Faith <[email protected]>
|
|
6 |
* \author Kevin E. Martin <[email protected]>
|
|
7 |
*/
|
|
8 |
|
|
9 |
/*
|
|
10 |
* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
|
|
11 |
* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
|
|
12 |
* 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:
|
|
20 |
*
|
|
21 |
* 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.
|
|
24 |
*
|
|
25 |
* 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.
|
|
32 |
*/
|
|
33 |
|
|
34 |
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drm.c,v 1.36 2003/08/24 17:35:35 tsi Exp $ */
|
|
35 |
|
|
36 |
#ifdef HAVE_XORG_CONFIG_H
|
|
37 |
#include <xorg-config.h>
|
|
38 |
#endif
|
|
39 |
|
|
40 |
#ifdef XFree86Server
|
|
41 |
# include "xf86.h"
|
|
42 |
# include "xf86_OSproc.h"
|
|
43 |
# include "drm.h"
|
|
44 |
# include "xf86_ansic.h"
|
|
45 |
# define _DRM_MALLOC xalloc
|
|
46 |
# define _DRM_FREE xfree
|
|
47 |
# ifndef XFree86LOADER
|
|
48 |
# include <sys/mman.h>
|
|
49 |
# endif
|
|
50 |
#else
|
|
51 |
# include <stdio.h>
|
|
52 |
# include <stdlib.h>
|
|
53 |
# include <unistd.h>
|
|
54 |
# include <string.h>
|
|
55 |
# include <ctype.h>
|
|
56 |
# include <fcntl.h>
|
|
57 |
# include <errno.h>
|
|
58 |
# include <signal.h>
|
|
59 |
# include <sys/types.h>
|
|
60 |
# include <sys/stat.h>
|
|
61 |
# define stat_t struct stat
|
|
62 |
# include <sys/ioctl.h>
|
|
63 |
# include <sys/mman.h>
|
|
64 |
# include <sys/time.h>
|
|
65 |
# include <stdarg.h>
|
|
66 |
# ifdef DRM_USE_MALLOC
|
|
67 |
# define _DRM_MALLOC malloc
|
|
68 |
# define _DRM_FREE free
|
|
69 |
# else
|
|
70 |
# define _DRM_MALLOC malloc
|
|
71 |
# define _DRM_FREE free
|
|
72 |
# endif
|
|
73 |
# include "drm.h"
|
|
74 |
#endif
|
|
75 |
|
|
76 |
/* No longer needed with CVS kernel modules on alpha
|
|
77 |
#if defined(__alpha__) && defined(__linux__)
|
|
78 |
extern unsigned long _bus_base(void);
|
|
79 |
#define BUS_BASE _bus_base()
|
|
80 |
#endif
|
|
81 |
*/
|
|
82 |
|
|
83 |
/* Not all systems have MAP_FAILED defined */
|
|
84 |
#ifndef MAP_FAILED
|
|
85 |
#define MAP_FAILED ((void *)-1)
|
|
86 |
#endif
|
|
87 |
|
|
88 |
#include "xf86drm.h"
|
|
89 |
|
|
90 |
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
91 |
#define DRM_MAJOR 145
|
|
92 |
#endif
|
|
93 |
|
|
94 |
#ifdef __NetBSD__
|
|
95 |
#define DRM_MAJOR 34
|
|
96 |
#endif
|
|
97 |
|
|
98 |
# ifdef __OpenBSD__
|
|
99 |
# define DRM_MAJOR 81
|
|
100 |
# endif
|
|
101 |
|
|
102 |
#ifndef DRM_MAJOR
|
|
103 |
#define DRM_MAJOR 226 /* Linux */
|
|
104 |
#endif
|
|
105 |
|
|
106 |
#ifndef DRM_MAX_MINOR
|
|
107 |
#define DRM_MAX_MINOR 16
|
|
108 |
#endif
|
|
109 |
|
|
110 |
#ifndef makedev
|
|
111 |
/* This definition needs to be changed on
|
|
112 |
some systems if dev_t is a structure.
|
|
113 |
If there is a header file we can get it
|
|
114 |
from, there would be best. */
|
|
115 |
#define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
|
|
116 |
#endif
|
|
117 |
|
|
118 |
#define DRM_MSG_VERBOSITY 3
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Output a message to stderr.
|
|
122 |
*
|
|
123 |
* \param format printf() like format string.
|
|
124 |
*
|
|
125 |
* \internal
|
|
126 |
* This function is a wrapper around vfprintf().
|
|
127 |
*/
|
|
128 |
static void
|
|
129 |
drmMsg(const char *format, ...)
|
|
130 |
{
|
|
131 |
va_list ap;
|
|
132 |
|
|
133 |
#ifndef XFree86Server
|
|
134 |
const char *env;
|
|
135 |
if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose"))
|
|
136 |
#endif
|
|
137 |
{
|
|
138 |
va_start(ap, format);
|
|
139 |
#ifdef XFree86Server
|
|
140 |
xf86VDrvMsgVerb(-1, X_NONE, DRM_MSG_VERBOSITY, format, ap);
|
|
141 |
#else
|
|
142 |
vfprintf(stderr, format, ap);
|
|
143 |
#endif
|
|
144 |
va_end(ap);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
static void *drmHashTable = NULL; /* Context switch callbacks */
|
|
149 |
|
|
150 |
typedef struct drmHashEntry {
|
|
151 |
int fd;
|
|
152 |
void (*f)(int, void *, void *);
|
|
153 |
void *tagTable;
|
|
154 |
} drmHashEntry;
|
|
155 |
|
|
156 |
void *drmMalloc(int size)
|
|
157 |
{
|
|
158 |
void *pt;
|
|
159 |
if ((pt = _DRM_MALLOC(size))) memset(pt, 0, size);
|
|
160 |
return pt;
|
|
161 |
}
|
|
162 |
|
|
163 |
void drmFree(void *pt)
|
|
164 |
{
|
|
165 |
if (pt) _DRM_FREE(pt);
|
|
166 |
}
|
|
167 |
|
|
168 |
/* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
|
|
169 |
static char *drmStrdup(const char *s)
|
|
170 |
{
|
|
171 |
char *retval = NULL;
|
|
172 |
|
|
173 |
if (s) {
|
|
174 |
retval = _DRM_MALLOC(strlen(s)+1);
|
|
175 |
strcpy(retval, s);
|
|
176 |
}
|
|
177 |
return retval;
|
|
178 |
}
|
|
179 |
|
|
180 |
|
|
181 |
static unsigned long drmGetKeyFromFd(int fd)
|
|
182 |
{
|
|
183 |
stat_t st;
|
|
184 |
|
|
185 |
st.st_rdev = 0;
|
|
186 |
fstat(fd, &st);
|
|
187 |
return st.st_rdev;
|
|
188 |
}
|
|
189 |
|
|
190 |
static drmHashEntry *drmGetEntry(int fd)
|
|
191 |
{
|
|
192 |
unsigned long key = drmGetKeyFromFd(fd);
|
|
193 |
void *value;
|
|
194 |
drmHashEntry *entry;
|
|
195 |
|
|
196 |
if (!drmHashTable) drmHashTable = drmHashCreate();
|
|
197 |
|
|
198 |
if (drmHashLookup(drmHashTable, key, &value)) {
|
|
199 |
entry = drmMalloc(sizeof(*entry));
|
|
200 |
entry->fd = fd;
|
|
201 |
entry->f = NULL;
|
|
202 |
entry->tagTable = drmHashCreate();
|
|
203 |
drmHashInsert(drmHashTable, key, entry);
|
|
204 |
} else {
|
|
205 |
entry = value;
|
|
206 |
}
|
|
207 |
return entry;
|
|
208 |
}
|
|
209 |
|
|
210 |
/**
|
|
211 |
* Compare two busid strings
|
|
212 |
*
|
|
213 |
* \param first
|
|
214 |
* \param second
|
|
215 |
*
|
|
216 |
* \return 1 if matched.
|
|
217 |
*
|
|
218 |
* \internal
|
|
219 |
* This function compares two bus ID strings. It understands the older
|
|
220 |
* PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
|
|
221 |
* domain, b is bus, d is device, f is function.
|
|
222 |
*/
|
|
223 |
static int drmMatchBusID(const char *id1, const char *id2)
|
|
224 |
{
|
|
225 |
/* First, check if the IDs are exactly the same */
|
|
226 |
if (strcasecmp(id1, id2) == 0)
|
|
227 |
return 1;
|
|
228 |
|
|
229 |
/* Try to match old/new-style PCI bus IDs. */
|
|
230 |
if (strncasecmp(id1, "pci", 3) == 0) {
|
|
231 |
int o1, b1, d1, f1;
|
|
232 |
int o2, b2, d2, f2;
|
|
233 |
int ret;
|
|
234 |
|
|
235 |
ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
|
|
236 |
if (ret != 4) {
|
|
237 |
o1 = 0;
|
|
238 |
ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
|
|
239 |
if (ret != 3)
|
|
240 |
return 0;
|
|
241 |
}
|
|
242 |
|
|
243 |
ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
|
|
244 |
if (ret != 4) {
|
|
245 |
o2 = 0;
|
|
246 |
ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
|
|
247 |
if (ret != 3)
|
|
248 |
return 0;
|
|
249 |
}
|
|
250 |
|
|
251 |
if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
|
|
252 |
return 0;
|
|
253 |
else
|
|
254 |
return 1;
|
|
255 |
}
|
|
256 |
return 0;
|
|
257 |
}
|
|
258 |
|
|
259 |
/**
|
|
260 |
* Open the DRM device, creating it if necessary.
|
|
261 |
*
|
|
262 |
* \param dev major and minor numbers of the device.
|
|
263 |
* \param minor minor number of the device.
|
|
264 |
*
|
|
265 |
* \return a file descriptor on success, or a negative value on error.
|
|
266 |
*
|
|
267 |
* \internal
|
|
268 |
* Assembles the device name from \p minor and opens it, creating the device
|
|
269 |
* special file node with the major and minor numbers specified by \p dev and
|
|
270 |
* parent directory if necessary and was called by root.
|
|
271 |
*/
|
|
272 |
static int drmOpenDevice(long dev, int minor)
|
|
273 |
{
|
|
274 |
stat_t st;
|
|
275 |
char buf[64];
|
|
276 |
int fd;
|
|
277 |
mode_t devmode = DRM_DEV_MODE;
|
|
278 |
int isroot = !geteuid();
|
|
279 |
#if defined(XFree86Server)
|
|
280 |
uid_t user = DRM_DEV_UID;
|
|
281 |
gid_t group = DRM_DEV_GID;
|
|
282 |
#endif
|
|
283 |
|
|
284 |
sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
|
|
285 |
drmMsg("drmOpenDevice: node name is %s\n", buf);
|
|
286 |
|
|
287 |
#if defined(XFree86Server)
|
|
288 |
devmode = xf86ConfigDRI.mode ? xf86ConfigDRI.mode : DRM_DEV_MODE;
|
|
289 |
devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
|
|
290 |
group = (xf86ConfigDRI.group >= 0) ? xf86ConfigDRI.group : DRM_DEV_GID;
|
|
291 |
#endif
|
|
292 |
|
|
293 |
if (stat(DRM_DIR_NAME, &st)) {
|
|
294 |
if (!isroot) return DRM_ERR_NOT_ROOT;
|
|
295 |
mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
|
|
296 |
chown(DRM_DIR_NAME, 0, 0); /* root:root */
|
|
297 |
chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
|
|
298 |
}
|
|
299 |
|
|
300 |
/* Check if the device node exists and create it if necessary. */
|
|
301 |
if (stat(buf, &st)) {
|
|
302 |
if (!isroot) return DRM_ERR_NOT_ROOT;
|
|
303 |
remove(buf);
|
|
304 |
mknod(buf, S_IFCHR | devmode, dev);
|
|
305 |
}
|
|
306 |
#if defined(XFree86Server)
|
|
307 |
chown(buf, user, group);
|
|
308 |
chmod(buf, devmode);
|
|
309 |
#endif
|
|
310 |
|
|
311 |
fd = open(buf, O_RDWR, 0);
|
|
312 |
drmMsg("drmOpenDevice: open result is %d, (%s)\n",
|
|
313 |
fd, fd < 0 ? strerror(errno) : "OK");
|
|
314 |
if (fd >= 0) return fd;
|
|
315 |
|
|
316 |
/* Check if the device node is not what we expect it to be, and recreate it
|
|
317 |
* and try again if so.
|
|
318 |
*/
|
|
319 |
if (st.st_rdev != dev) {
|
|
320 |
if (!isroot) return DRM_ERR_NOT_ROOT;
|
|
321 |
remove(buf);
|
|
322 |
mknod(buf, S_IFCHR | devmode, dev);
|
|
323 |
#if defined(XFree86Server)
|
|
324 |
chown(buf, user, group);
|
|
325 |
chmod(buf, devmode);
|
|
326 |
#endif
|
|
327 |
}
|
|
328 |
fd = open(buf, O_RDWR, 0);
|
|
329 |
drmMsg("drmOpenDevice: open result is %d, (%s)\n",
|
|
330 |
fd, fd < 0 ? strerror(errno) : "OK");
|
|
331 |
if (fd >= 0) return fd;
|
|
332 |
|
|
333 |
drmMsg("drmOpenDevice: Open failed\n");
|
|
334 |
remove(buf);
|
|
335 |
return -errno;
|
|
336 |
}
|
|
337 |
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Open the DRM device
|
|
341 |
*
|
|
342 |
* \param minor device minor number.
|
|
343 |
* \param create allow to create the device if set.
|
|
344 |
*
|
|
345 |
* \return a file descriptor on success, or a negative value on error.
|
|
346 |
*
|
|
347 |
* \internal
|
|
348 |
* Calls drmOpenDevice() if \p create is set, otherwise assembles the device
|
|
349 |
* name from \p minor and opens it.
|
|
350 |
*/
|
|
351 |
static int drmOpenMinor(int minor, int create)
|
|
352 |
{
|
|
353 |
int fd;
|
|
354 |
char buf[64];
|
|
355 |
|
|
356 |
if (create) return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
|
|
357 |
|
|
358 |
sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
|
|
359 |
if ((fd = open(buf, O_RDWR, 0)) >= 0) return fd;
|
|
360 |
return -errno;
|
|
361 |
}
|
|
362 |
|
|
363 |
|
|
364 |
/**
|
|
365 |
* Determine whether the DRM kernel driver has been loaded.
|
|
366 |
*
|
|
367 |
* \return 1 if the DRM driver is loaded, 0 otherwise.
|
|
368 |
*
|
|
369 |
* \internal
|
|
370 |
* Determine the presence of the kernel driver by attempting to open the 0
|
|
371 |
* minor and get version information. For backward compatibility with older
|
|
372 |
* Linux implementations, /proc/dri is also checked.
|
|
373 |
*/
|
|
374 |
int drmAvailable(void)
|
|
375 |
{
|
|
376 |
drmVersionPtr version;
|
|
377 |
int retval = 0;
|
|
378 |
int fd;
|
|
379 |
|
|
380 |
if ((fd = drmOpenMinor(0, 1)) < 0) {
|
|
381 |
#ifdef __linux__
|
|
382 |
/* Try proc for backward Linux compatibility */
|
|
383 |
if (!access("/proc/dri/0", R_OK)) return 1;
|
|
384 |
#endif
|
|
385 |
return 0;
|
|
386 |
}
|
|
387 |
|
|
388 |
if ((version = drmGetVersion(fd))) {
|
|
389 |
retval = 1;
|
|
390 |
drmFreeVersion(version);
|
|
391 |
}
|
|
392 |
close(fd);
|
|
393 |
|
|
394 |
return retval;
|
|
395 |
}
|
|
396 |
|
|
397 |
|
|
398 |
/**
|
|
399 |
* Open the device by bus ID.
|
|
400 |
*
|
|
401 |
* \param busid bus ID.
|
|
402 |
*
|
|
403 |
* \return a file descriptor on success, or a negative value on error.
|
|
404 |
*
|
|
405 |
* \internal
|
|
406 |
* This function attempts to open every possible minor (up to DRM_MAX_MINOR),
|
|
407 |
* comparing the device bus ID with the one supplied.
|
|
408 |
*
|
|
409 |
* \sa drmOpenMinor() and drmGetBusid().
|
|
410 |
*/
|
|
411 |
static int drmOpenByBusid(const char *busid)
|
|
412 |
{
|
|
413 |
int i;
|
|
414 |
int fd;
|
|
415 |
const char *buf;
|
|
416 |
drmSetVersion sv;
|
|
417 |
|
|
418 |
drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
|
|
419 |
for (i = 0; i < DRM_MAX_MINOR; i++) {
|
|
420 |
fd = drmOpenMinor(i, 1);
|
|
421 |
drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
|
|
422 |
if (fd >= 0) {
|
|
423 |
sv.drm_di_major = 1;
|
|
424 |
sv.drm_di_minor = 1;
|
|
425 |
sv.drm_dd_major = -1; /* Don't care */
|
|
426 |
drmSetInterfaceVersion(fd, &sv);
|
|
427 |
buf = drmGetBusid(fd);
|
|
428 |
drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
|
|
429 |
if (buf && drmMatchBusID(buf, busid)) {
|
|
430 |
drmFreeBusid(buf);
|
|
431 |
return fd;
|
|
432 |
}
|
|
433 |
if (buf) drmFreeBusid(buf);
|
|
434 |
close(fd);
|
|
435 |
}
|
|
436 |
}
|
|
437 |
return -1;
|
|
438 |
}
|
|
439 |
|
|
440 |
|
|
441 |
/**
|
|
442 |
* Open the device by name.
|
|
443 |
*
|
|
444 |
* \param name driver name.
|
|
445 |
*
|
|
446 |
* \return a file descriptor on success, or a negative value on error.
|
|
447 |
*
|
|
448 |
* \internal
|
|
449 |
* This function opens the first minor number that matches the driver name and
|
|
450 |
* isn't already in use. If it's in use it then it will already have a bus ID
|
|
451 |
* assigned.
|
|
452 |
*
|
|
453 |
* \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
|
|
454 |
*/
|
|
455 |
static int drmOpenByName(const char *name)
|
|
456 |
{
|
|
457 |
int i;
|
|
458 |
int fd;
|
|
459 |
drmVersionPtr version;
|
|
460 |
char * id;
|
|
461 |
|
|
462 |
if (!drmAvailable()) {
|
|
463 |
#if !defined(XFree86Server)
|
|
464 |
return -1;
|
|
465 |
#else
|
|
466 |
/* try to load the kernel module now */
|
|
467 |
if (!xf86LoadKernelModule(name)) {
|
|
468 |
ErrorF("[drm] failed to load kernel module \"%s\"\n",
|
|
469 |
name);
|
|
470 |
return -1;
|
|
471 |
}
|
|
472 |
#endif
|
|
473 |
}
|
|
474 |
|
|
475 |
/*
|
|
476 |
* Open the first minor number that matches the driver name and isn't
|
|
477 |
* already in use. If it's in use it will have a busid assigned already.
|
|
478 |
*/
|
|
479 |
for (i = 0; i < DRM_MAX_MINOR; i++) {
|
|
480 |
if ((fd = drmOpenMinor(i, 1)) >= 0) {
|
|
481 |
if ((version = drmGetVersion(fd))) {
|
|
482 |
if (!strcmp(version->name, name)) {
|
|
483 |
drmFreeVersion(version);
|
|
484 |
id = drmGetBusid(fd);
|
|
485 |
drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
|
|
486 |
if (!id || !*id) {
|
|
487 |
if (id) {
|
|
488 |
drmFreeBusid(id);
|
|
489 |
}
|
|
490 |
return fd;
|
|
491 |
} else {
|
|
492 |
drmFreeBusid(id);
|
|
493 |
}
|
|
494 |
} else {
|
|
495 |
drmFreeVersion(version);
|
|
496 |
}
|
|
497 |
}
|
|
498 |
close(fd);
|
|
499 |
}
|
|
500 |
}
|
|
501 |
|
|
502 |
#ifdef __linux__
|
|
503 |
/* Backward-compatibility /proc support */
|
|
504 |
for (i = 0; i < 8; i++) {
|
|
505 |
char proc_name[64], buf[512];
|
|
506 |
char *driver, *pt, *devstring;
|
|
507 |
int retcode;
|
|
508 |
|
|
509 |
sprintf(proc_name, "/proc/dri/%d/name", i);
|
|
510 |
if ((fd = open(proc_name, 0, 0)) >= 0) {
|
|
511 |
retcode = read(fd, buf, sizeof(buf)-1);
|
|
512 |
close(fd);
|
|
513 |
if (retcode) {
|
|
514 |
buf[retcode-1] = '\0';
|
|
515 |
for (driver = pt = buf; *pt && *pt != ' '; ++pt)
|
|
516 |
;
|
|
517 |
if (*pt) { /* Device is next */
|
|
518 |
*pt = '\0';
|
|
519 |
if (!strcmp(driver, name)) { /* Match */
|
|
520 |
for (devstring = ++pt; *pt && *pt != ' '; ++pt)
|
|
521 |
;
|
|
522 |
if (*pt) { /* Found busid */
|
|
523 |
return drmOpenByBusid(++pt);
|
|
524 |
} else { /* No busid */
|
|
525 |
return drmOpenDevice(strtol(devstring, NULL, 0),i);
|
|
526 |
}
|
|
527 |
}
|
|
528 |
}
|
|
529 |
}
|
|
530 |
}
|
|
531 |
}
|
|
532 |
#endif
|
|
533 |
|
|
534 |
return -1;
|
|
535 |
}
|
|
536 |
|
|
537 |
|
|
538 |
/**
|
|
539 |
* Open the DRM device.
|
|
540 |
*
|
|
541 |
* Looks up the specified name and bus ID, and opens the device found. The
|
|
542 |
* entry in /dev/dri is created if necessary and if called by root.
|
|
543 |
*
|
|
544 |
* \param name driver name. Not referenced if bus ID is supplied.
|
|
545 |
* \param busid bus ID. Zero if not known.
|
|
546 |
*
|
|
547 |
* \return a file descriptor on success, or a negative value on error.
|
|
548 |
*
|
|
549 |
* \internal
|
|
550 |
* It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
|
|
551 |
* otherwise.
|
|
552 |
*/
|
|
553 |
int drmOpen(const char *name, const char *busid)
|
|
554 |
{
|
|
555 |
#ifdef XFree86Server
|
|
556 |
if (!drmAvailable() && name != NULL) {
|
|
557 |
/* try to load the kernel */
|
|
558 |
if (!xf86LoadKernelModule(name)) {
|
|
559 |
ErrorF("[drm] failed to load kernel module \"%s\"\n",
|
|
560 |
name);
|
|
561 |
return -1;
|
|
562 |
}
|
|
563 |
}
|
|
564 |
#endif
|
|
565 |
|
|
566 |
if (busid) {
|
|
567 |
int fd;
|
|
568 |
|
|
569 |
fd = drmOpenByBusid(busid);
|
|
570 |
if (fd >= 0)
|
|
571 |
return fd;
|
|
572 |
}
|
|
573 |
if (name)
|
|
574 |
return drmOpenByName(name);
|
|
575 |
return -1;
|
|
576 |
}
|
|
577 |
|
|
578 |
|
|
579 |
/**
|
|
580 |
* Free the version information returned by drmGetVersion().
|
|
581 |
*
|
|
582 |
* \param v pointer to the version information.
|
|
583 |
*
|
|
584 |
* \internal
|
|
585 |
* It frees the memory pointed by \p %v as well as all the non-null strings
|
|
586 |
* pointers in it.
|
|
587 |
*/
|
|
588 |
void drmFreeVersion(drmVersionPtr v)
|
|
589 |
{
|
|
590 |
if (!v) return;
|
|
591 |
if (v->name) drmFree(v->name);
|
|
592 |
if (v->date) drmFree(v->date);
|
|
593 |
if (v->desc) drmFree(v->desc);
|
|
594 |
drmFree(v);
|
|
595 |
}
|
|
596 |
|
|
597 |
|
|
598 |
/**
|
|
599 |
* Free the non-public version information returned by the kernel.
|
|
600 |
*
|
|
601 |
* \param v pointer to the version information.
|
|
602 |
*
|
|
603 |
* \internal
|
|
604 |
* Used by drmGetVersion() to free the memory pointed by \p %v as well as all
|
|
605 |
* the non-null strings pointers in it.
|
|
606 |
*/
|
|
607 |
static void drmFreeKernelVersion(drm_version_t *v)
|
|
608 |
{
|
|
609 |
if (!v) return;
|
|
610 |
if (v->name) drmFree(v->name);
|
|
611 |
if (v->date) drmFree(v->date);
|
|
612 |
if (v->desc) drmFree(v->desc);
|
|
613 |
drmFree(v);
|
|
614 |
}
|
|
615 |
|
|
616 |
|
|
617 |
/**
|
|
618 |
* Copy version information.
|
|
619 |
*
|
|
620 |
* \param d destination pointer.
|
|
621 |
* \param s source pointer.
|
|
622 |
*
|
|
623 |
* \internal
|
|
624 |
* Used by drmGetVersion() to translate the information returned by the ioctl
|
|
625 |
* interface in a private structure into the public structure counterpart.
|
|
626 |
*/
|
|
627 |
static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
|
|
628 |
{
|
|
629 |
d->version_major = s->version_major;
|
|
630 |
d->version_minor = s->version_minor;
|
|
631 |
d->version_patchlevel = s->version_patchlevel;
|
|
632 |
d->name_len = s->name_len;
|
|
633 |
d->name = drmStrdup(s->name);
|
|
634 |
d->date_len = s->date_len;
|
|
635 |
d->date = drmStrdup(s->date);
|
|
636 |
d->desc_len = s->desc_len;
|
|
637 |
d->desc = drmStrdup(s->desc);
|
|
638 |
}
|
|
639 |
|
|
640 |
|
|
641 |
/**
|
|
642 |
* Query the driver version information.
|
|
643 |
*
|
|
644 |
* \param fd file descriptor.
|
|
645 |
*
|
|
646 |
* \return pointer to a drmVersion structure which should be freed with
|
|
647 |
* drmFreeVersion().
|
|
648 |
*
|
|
649 |
* \note Similar information is available via /proc/dri.
|
|
650 |
*
|
|
651 |
* \internal
|
|
652 |
* It gets the version information via successive DRM_IOCTL_VERSION ioctls,
|
|
653 |
* first with zeros to get the string lengths, and then the actually strings.
|
|
654 |
* It also null-terminates them since they might not be already.
|
|
655 |
*/
|
|
656 |
drmVersionPtr drmGetVersion(int fd)
|
|
657 |
{
|
|
658 |
drmVersionPtr retval;
|
|
659 |
drm_version_t *version = drmMalloc(sizeof(*version));
|
|
660 |
|
|
661 |
/* First, get the lengths */
|
|
662 |
version->name_len = 0;
|
|
663 |
version->name = NULL;
|
|
664 |
version->date_len = 0;
|
|
665 |
version->date = NULL;
|
|
666 |
version->desc_len = 0;
|
|
667 |
version->desc = NULL;
|
|
668 |
|
|
669 |
if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
|
|
670 |
drmFreeKernelVersion(version);
|
|
671 |
return NULL;
|
|
672 |
}
|
|
673 |
|
|
674 |
/* Now, allocate space and get the data */
|
|
675 |
if (version->name_len)
|
|
676 |
version->name = drmMalloc(version->name_len + 1);
|
|
677 |
if (version->date_len)
|
|
678 |
version->date = drmMalloc(version->date_len + 1);
|
|
679 |
if (version->desc_len)
|
|
680 |
version->desc = drmMalloc(version->desc_len + 1);
|
|
681 |
|
|
682 |
if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
|
|
683 |
drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
|
|
684 |
drmFreeKernelVersion(version);
|
|
685 |
return NULL;
|
|
686 |
}
|
|
687 |
|
|
688 |
/* The results might not be null-terminated
|
|
689 |
strings, so terminate them. */
|
|
690 |
|
|
691 |
if (version->name_len) version->name[version->name_len] = '\0';
|
|
692 |
if (version->date_len) version->date[version->date_len] = '\0';
|
|
693 |
if (version->desc_len) version->desc[version->desc_len] = '\0';
|
|
694 |
|
|
695 |
/* Now, copy it all back into the
|
|
696 |
client-visible data structure... */
|
|
697 |
retval = drmMalloc(sizeof(*retval));
|
|
698 |
drmCopyVersion(retval, version);
|
|
699 |
drmFreeKernelVersion(version);
|
|
700 |
return retval;
|
|
701 |
}
|
|
702 |
|
|
703 |
|
|
704 |
/**
|
|
705 |
* Get version information for the DRM user space library.
|
|
706 |
*
|
|
707 |
* This version number is driver independent.
|
|
708 |
*
|
|
709 |
* \param fd file descriptor.
|
|
710 |
*
|
|
711 |
* \return version information.
|
|
712 |
*
|
|
713 |
* \internal
|
|
714 |
* This function allocates and fills a drm_version structure with a hard coded
|
|
715 |
* version number.
|
|
716 |
*/
|
|
717 |
drmVersionPtr drmGetLibVersion(int fd)
|
|
718 |
{
|
|
719 |
drm_version_t *version = drmMalloc(sizeof(*version));
|
|
720 |
|
|
721 |
/* Version history:
|
|
722 |
* revision 1.0.x = original DRM interface with no drmGetLibVersion
|
|
723 |
* entry point and many drm<Device> extensions
|
|
724 |
* revision 1.1.x = added drmCommand entry points for device extensions
|
|
725 |
* added drmGetLibVersion to identify libdrm.a version
|
|
726 |
* revision 1.2.x = added drmSetInterfaceVersion
|
|
727 |
* modified drmOpen to handle both busid and name
|
|
728 |
*/
|
|
729 |
version->version_major = 1;
|
|
730 |
version->version_minor = 2;
|
|
731 |
version->version_patchlevel = 0;
|
|
732 |
|
|
733 |
return (drmVersionPtr)version;
|
|
734 |
}
|
|
735 |
|
|
736 |
|
|
737 |
/**
|
|
738 |
* Free the bus ID information.
|
|
739 |
*
|
|
740 |
* \param busid bus ID information string as given by drmGetBusid().
|
|
741 |
*
|
|
742 |
* \internal
|
|
743 |
* This function is just frees the memory pointed by \p busid.
|
|
744 |
*/
|
|
745 |
void drmFreeBusid(const char *busid)
|
|
746 |
{
|
|
747 |
drmFree((void *)busid);
|
|
748 |
}
|
|
749 |
|
|
750 |
|
|
751 |
/**
|
|
752 |
* Get the bus ID of the device.
|
|
753 |
*
|
|
754 |
* \param fd file descriptor.
|
|
755 |
*
|
|
756 |
* \return bus ID string.
|
|
757 |
*
|
|
758 |
* \internal
|
|
759 |
* This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
|
|
760 |
* get the string length and data, passing the arguments in a drm_unique
|
|
761 |
* structure.
|
|
762 |
*/
|
|
763 |
char *drmGetBusid(int fd)
|
|
764 |
{
|
|
765 |
drm_unique_t u;
|
|
766 |
|
|
767 |
u.unique_len = 0;
|
|
768 |
u.unique = NULL;
|
|
769 |
|
|
770 |
if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
|
|
771 |
u.unique = drmMalloc(u.unique_len + 1);
|
|
772 |
if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
|
|
773 |
u.unique[u.unique_len] = '\0';
|
|
774 |
|
|
775 |
return u.unique;
|
|
776 |
}
|
|
777 |
|
|
778 |
|
|
779 |
/**
|
|
780 |
* Set the bus ID of the device.
|
|
781 |
*
|
|
782 |
* \param fd file descriptor.
|
|
783 |
* \param busid bus ID string.
|
|
784 |
*
|
|
785 |
* \return zero on success, negative on failure.
|
|
786 |
*
|
|
787 |
* \internal
|
|
788 |
* This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
|
|
789 |
* the arguments in a drm_unique structure.
|
|
790 |
*/
|
|
791 |
int drmSetBusid(int fd, const char *busid)
|
|
792 |
{
|
|
793 |
drm_unique_t u;
|
|
794 |
|
|
795 |
u.unique = (char *)busid;
|
|
796 |
u.unique_len = strlen(busid);
|
|
797 |
|
|
798 |
if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
|
|
799 |
return -errno;
|
|
800 |
}
|
|
801 |
return 0;
|
|
802 |
}
|
|
803 |
|
|
804 |
int drmGetMagic(int fd, drm_magic_t * magic)
|
|
805 |
{
|
|
806 |
drm_auth_t auth;
|
|
807 |
|
|
808 |
*magic = 0;
|
|
809 |
if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth)) return -errno;
|
|
810 |
*magic = auth.magic;
|
|
811 |
return 0;
|
|
812 |
}
|
|
813 |
|
|
814 |
int drmAuthMagic(int fd, drm_magic_t magic)
|
|
815 |
{
|
|
816 |
drm_auth_t auth;
|
|
817 |
|
|
818 |
auth.magic = magic;
|
|
819 |
if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth)) return -errno;
|
|
820 |
return 0;
|
|
821 |
}
|
|
822 |
|
|
823 |
/**
|
|
824 |
* Specifies a range of memory that is available for mapping by a
|
|
825 |
* non-root process.
|
|
826 |
*
|
|
827 |
* \param fd file descriptor.
|
|
828 |
* \param offset usually the physical address. The actual meaning depends of
|
|
829 |
* the \p type parameter. See below.
|
|
830 |
* \param size of the memory in bytes.
|
|
831 |
* \param type type of the memory to be mapped.
|
|
832 |
* \param flags combination of several flags to modify the function actions.
|
|
833 |
* \param handle will be set to a value that may be used as the offset
|
|
834 |
* parameter for mmap().
|
|
835 |
*
|
|
836 |
* \return zero on success or a negative value on error.
|
|
837 |
*
|
|
838 |
* \par Mapping the frame buffer
|
|
839 |
* For the frame buffer
|
|
840 |
* - \p offset will be the physical address of the start of the frame buffer,
|
|
841 |
* - \p size will be the size of the frame buffer in bytes, and
|
|
842 |
* - \p type will be DRM_FRAME_BUFFER.
|
|
843 |
*
|
|
844 |
* \par
|
|
845 |
* The area mapped will be uncached. If MTRR support is available in the
|
|
846 |
* kernel, the frame buffer area will be set to write combining.
|
|
847 |
*
|
|
848 |
* \par Mapping the MMIO register area
|
|
849 |
* For the MMIO register area,
|
|
850 |
* - \p offset will be the physical address of the start of the register area,
|
|
851 |
* - \p size will be the size of the register area bytes, and
|
|
852 |
* - \p type will be DRM_REGISTERS.
|
|
853 |
* \par
|
|
854 |
* The area mapped will be uncached.
|
|
855 |
*
|
|
856 |
* \par Mapping the SAREA
|
|
857 |
* For the SAREA,
|
|
858 |
* - \p offset will be ignored and should be set to zero,
|
|
859 |
* - \p size will be the desired size of the SAREA in bytes,
|
|
860 |
* - \p type will be DRM_SHM.
|
|
861 |
*
|
|
862 |
* \par
|
|
863 |
* A shared memory area of the requested size will be created and locked in
|
|
864 |
* kernel memory. This area may be mapped into client-space by using the handle
|
|
865 |
* returned.
|
|
866 |
*
|
|
867 |
* \note May only be called by root.
|
|
868 |
*
|
|
869 |
* \internal
|
|
870 |
* This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
|
|
871 |
* the arguments in a drm_map structure.
|
|
872 |
*/
|
|
873 |
int drmAddMap(int fd,
|
|
874 |
drm_handle_t offset,
|
|
875 |
drmSize size,
|
|
876 |
drmMapType type,
|
|
877 |
drmMapFlags flags,
|
|
878 |
drm_handle_t * handle)
|
|
879 |
{
|
|
880 |
drm_map_t map;
|
|
881 |
|
|
882 |
map.offset = offset;
|
|
883 |
/* No longer needed with CVS kernel modules on alpha
|
|
884 |
#ifdef __alpha__
|
|
885 |
if (type != DRM_SHM)
|
|
886 |
map.offset += BUS_BASE;
|
|
887 |
#endif
|
|
888 |
*/
|
|
889 |
map.size = size;
|
|
890 |
map.handle = 0;
|
|
891 |
map.type = type;
|
|
892 |
map.flags = flags;
|
|
893 |
if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map)) return -errno;
|
|
894 |
if (handle) *handle = (drm_handle_t)map.handle;
|
|
895 |
return 0;
|
|
896 |
}
|
|
897 |
|
|
898 |
int drmRmMap(int fd, drm_handle_t handle)
|
|
899 |
{
|
|
900 |
drm_map_t map;
|
|
901 |
|
|
902 |
map.handle = (drm_handle_t)handle;
|
|
903 |
|
|
904 |
if(ioctl(fd, DRM_IOCTL_RM_MAP, &map)) return -errno;
|
|
905 |
return 0;
|
|
906 |
}
|
|
907 |
|
|
908 |
/**
|
|
909 |
* Make buffers available for DMA transfers.
|
|
910 |
*
|
|
911 |
* \param fd file descriptor.
|
|
912 |
* \param count number of buffers.
|
|
913 |
* \param size size of each buffer.
|
|
914 |
* \param flags buffer allocation flags.
|
|
915 |
* \param agp_offset offset in the AGP aperture
|
|
916 |
*
|
|
917 |
* \return number of buffers allocated, negative on error.
|
|
918 |
*
|
|
919 |
* \internal
|
|
920 |
* This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
|
|
921 |
*
|
|
922 |
* \sa drm_buf_desc.
|
|
923 |
*/
|
|
924 |
int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
|
|
925 |
int agp_offset)
|
|
926 |
{
|
|
927 |
drm_buf_desc_t request;
|
|
928 |
|
|
929 |
request.count = count;
|
|
930 |
request.size = size;
|
|
931 |
request.low_mark = 0;
|
|
932 |
request.high_mark = 0;
|
|
933 |
request.flags = flags;
|
|
934 |
request.agp_start = agp_offset;
|
|
935 |
|
|
936 |
if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request)) return -errno;
|
|
937 |
return request.count;
|
|
938 |
}
|
|
939 |
|
|
940 |
int drmMarkBufs(int fd, double low, double high)
|
|
941 |
{
|
|
942 |
drm_buf_info_t info;
|
|
943 |
int i;
|
|
944 |
|
|
945 |
info.count = 0;
|
|
946 |
info.list = NULL;
|
|
947 |
|
|
948 |
if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return -EINVAL;
|
|
949 |
|
|
950 |
if (!info.count) return -EINVAL;
|
|
951 |
|
|
952 |
if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
|
|
953 |
return -ENOMEM;
|
|
954 |
|
|
955 |
if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
|
|
956 |
int retval = -errno;
|
|
957 |
drmFree(info.list);
|
|
958 |
return retval;
|
|
959 |
}
|
|
960 |
|
|
961 |
for (i = 0; i < info.count; i++) {
|
|
962 |
info.list[i].low_mark = low * info.list[i].count;
|
|
963 |
info.list[i].high_mark = high * info.list[i].count;
|
|
964 |
if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
|
|
965 |
int retval = -errno;
|
|
966 |
drmFree(info.list);
|
|
967 |
return retval;
|
|
968 |
}
|
|
969 |
}
|
|
970 |
drmFree(info.list);
|
|
971 |
|
|
972 |
return 0;
|
|
973 |
}
|
|
974 |
|
|
975 |
/**
|
|
976 |
* Free buffers.
|
|
977 |
*
|
|
978 |
* \param fd file descriptor.
|
|
979 |
* \param count number of buffers to free.
|
|
980 |
* \param list list of buffers to be freed.
|
|
981 |
*
|
|
982 |
* \return zero on success, or a negative value on failure.
|
|
983 |
*
|
|
984 |
* \note This function is primarily used for debugging.
|
|
985 |
*
|
|
986 |
* \internal
|
|
987 |
* This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
|
|
988 |
* the arguments in a drm_buf_free structure.
|
|
989 |
*/
|
|
990 |
int drmFreeBufs(int fd, int count, int *list)
|
|
991 |
{
|
|
992 |
drm_buf_free_t request;
|
|
993 |
|
|
994 |
request.count = count;
|
|
995 |
request.list = list;
|
|
996 |
if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request)) return -errno;
|
|
997 |
return 0;
|
|
998 |
}
|
|
999 |
|
|
1000 |
|
|
1001 |
/**
|
|
1002 |
* Close the device.
|
|
1003 |
*
|
|
1004 |
* \param fd file descriptor.
|
|
1005 |
*
|
|
1006 |
* \internal
|
|
1007 |
* This function closes the file descriptor.
|
|
1008 |
*/
|
|
1009 |
int drmClose(int fd)
|
|
1010 |
{
|
|
1011 |
unsigned long key = drmGetKeyFromFd(fd);
|
|
1012 |
drmHashEntry *entry = drmGetEntry(fd);
|
|
1013 |
|
|
1014 |
drmHashDestroy(entry->tagTable);
|
|
1015 |
entry->fd = 0;
|
|
1016 |
entry->f = NULL;
|
|
1017 |
entry->tagTable = NULL;
|
|
1018 |
|
|
1019 |
drmHashDelete(drmHashTable, key);
|
|
1020 |
drmFree(entry);
|
|
1021 |
|
|
1022 |
return close(fd);
|
|
1023 |
}
|
|
1024 |
|
|
1025 |
|
|
1026 |
/**
|
|
1027 |
* Map a region of memory.
|
|
1028 |
*
|
|
1029 |
* \param fd file descriptor.
|
|
1030 |
* \param handle handle returned by drmAddMap().
|
|
1031 |
* \param size size in bytes. Must match the size used by drmAddMap().
|
|
1032 |
* \param address will contain the user-space virtual address where the mapping
|
|
1033 |
* begins.
|
|
1034 |
*
|
|
1035 |
* \return zero on success, or a negative value on failure.
|
|
1036 |
*
|
|
1037 |
* \internal
|
|
1038 |
* This function is a wrapper for mmap().
|
|
1039 |
*/
|
|
1040 |
int drmMap(int fd,
|
|
1041 |
drm_handle_t handle,
|
|
1042 |
drmSize size,
|
|
1043 |
drmAddressPtr address)
|
|
1044 |
{
|
|
1045 |
static unsigned long pagesize_mask = 0;
|
|
1046 |
|
|
1047 |
if (fd < 0) return -EINVAL;
|
|
1048 |
|
|
1049 |
if (!pagesize_mask)
|
|
1050 |
pagesize_mask = getpagesize() - 1;
|
|
1051 |
|
|
1052 |
size = (size + pagesize_mask) & ~pagesize_mask;
|
|
1053 |
|
|
1054 |
*address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
|
|
1055 |
if (*address == MAP_FAILED) return -errno;
|
|
1056 |
return 0;
|
|
1057 |
}
|
|
1058 |
|
|
1059 |
|
|
1060 |
/**
|
|
1061 |
* Unmap mappings obtained with drmMap().
|
|
1062 |
*
|
|
1063 |
* \param address address as given by drmMap().
|
|
1064 |
* \param size size in bytes. Must match the size used by drmMap().
|
|
1065 |
*
|
|
1066 |
* \return zero on success, or a negative value on failure.
|
|
1067 |
*
|
|
1068 |
* \internal
|
|
1069 |
* This function is a wrapper for unmap().
|
|
1070 |
*/
|
|
1071 |
int drmUnmap(drmAddress address, drmSize size)
|
|
1072 |
{
|
|
1073 |
return munmap(address, size);
|
|
1074 |
}
|
|
1075 |
|
|
1076 |
drmBufInfoPtr drmGetBufInfo(int fd)
|
|
1077 |
{
|
|
1078 |
drm_buf_info_t info;
|
|
1079 |
drmBufInfoPtr retval;
|
|
1080 |
int i;
|
|
1081 |
|
|
1082 |
info.count = 0;
|
|
1083 |
info.list = NULL;
|
|
1084 |
|
|
1085 |
if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return NULL;
|
|
1086 |
|
|
1087 |
if (info.count) {
|
|
1088 |
if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
|
|
1089 |
return NULL;
|
|
1090 |
|
|
1091 |
if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
|
|
1092 |
drmFree(info.list);
|
|
1093 |
return NULL;
|
|
1094 |
}
|
|
1095 |
/* Now, copy it all back into the
|
|
1096 |
client-visible data structure... */
|
|
1097 |
retval = drmMalloc(sizeof(*retval));
|
|
1098 |
retval->count = info.count;
|
|
1099 |
retval->list = drmMalloc(info.count * sizeof(*retval->list));
|
|
1100 |
for (i = 0; i < info.count; i++) {
|
|
1101 |
retval->list[i].count = info.list[i].count;
|
|
1102 |
retval->list[i].size = info.list[i].size;
|
|
1103 |
retval->list[i].low_mark = info.list[i].low_mark;
|
|
1104 |
retval->list[i].high_mark = info.list[i].high_mark;
|
|
1105 |
}
|
|
1106 |
drmFree(info.list);
|
|
1107 |
return retval;
|
|
1108 |
}
|
|
1109 |
return NULL;
|
|
1110 |
}
|
|
1111 |
|
|
1112 |
/**
|
|
1113 |
* Map all DMA buffers into client-virtual space.
|
|
1114 |
*
|
|
1115 |
* \param fd file descriptor.
|
|
1116 |
*
|
|
1117 |
* \return a pointer to a ::drmBufMap structure.
|
|
1118 |
*
|
|
1119 |
* \note The client may not use these buffers until obtaining buffer indices
|
|
1120 |
* with drmDMA().
|
|
1121 |
*
|
|
1122 |
* \internal
|
|
1123 |
* This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
|
|
1124 |
* information about the buffers in a drm_buf_map structure into the
|
|
1125 |
* client-visible data structures.
|
|
1126 |
*/
|
|
1127 |
drmBufMapPtr drmMapBufs(int fd)
|
|
1128 |
{
|
|
1129 |
drm_buf_map_t bufs;
|
|
1130 |
drmBufMapPtr retval;
|
|
1131 |
int i;
|
|
1132 |
|
|
1133 |
bufs.count = 0;
|
|
1134 |
bufs.list = NULL;
|
|
1135 |
bufs.virtual = NULL;
|
|
1136 |
if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) return NULL;
|
|
1137 |
|
|
1138 |
if (!bufs.count) return NULL;
|
|
1139 |
|
|
1140 |
if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
|
|
1141 |
return NULL;
|
|
1142 |
|
|
1143 |
if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
|
|
1144 |
drmFree(bufs.list);
|
|
1145 |
return NULL;
|
|
1146 |
}
|
|
1147 |
/* Now, copy it all back into the
|
|
1148 |
client-visible data structure... */
|
|
1149 |
retval = drmMalloc(sizeof(*retval));
|
|
1150 |
retval->count = bufs.count;
|
|
1151 |
retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
|
|
1152 |
for (i = 0; i < bufs.count; i++) {
|
|
1153 |
retval->list[i].idx = bufs.list[i].idx;
|
|
1154 |
retval->list[i].total = bufs.list[i].total;
|
|
1155 |
retval->list[i].used = 0;
|
|
1156 |
retval->list[i].address = bufs.list[i].address;
|
|
1157 |
}
|
|
1158 |
|
|
1159 |
drmFree(bufs.list);
|
|
1160 |
|
|
1161 |
return retval;
|
|
1162 |
}
|
|
1163 |
|
|
1164 |
|
|
1165 |
/**
|
|
1166 |
* Unmap buffers allocated with drmMapBufs().
|
|
1167 |
*
|
|
1168 |
* \return zero on success, or negative value on failure.
|
|
1169 |
*
|
|
1170 |
* \internal
|
|
1171 |
* Calls munmap() for every buffer stored in \p bufs and frees the
|
|
1172 |
* memory allocated by drmMapBufs().
|
|
1173 |
*/
|
|
1174 |
int drmUnmapBufs(drmBufMapPtr bufs)
|
|
1175 |
{
|
|
1176 |
int i;
|
|
1177 |
|
|
1178 |
for (i = 0; i < bufs->count; i++) {
|
|
1179 |
munmap(bufs->list[i].address, bufs->list[i].total);
|
|
1180 |
}
|
|
1181 |
|
|
1182 |
drmFree(bufs->list);
|
|
1183 |
drmFree(bufs);
|
|
1184 |
|
|
1185 |
return 0;
|
|
1186 |
}
|
|
1187 |
|
|
1188 |
|
|
1189 |
#define DRM_DMA_RETRY 16
|
|
1190 |
|
|
1191 |
/**
|
|
1192 |
* Reserve DMA buffers.
|
|
1193 |
*
|
|
1194 |
* \param fd file descriptor.
|
|
1195 |
* \param request
|
|
1196 |
*
|
|
1197 |
* \return zero on success, or a negative value on failure.
|
|
1198 |
*
|
|
1199 |
* \internal
|
|
1200 |
* Assemble the arguments into a drm_dma structure and keeps issuing the
|
|
1201 |
* DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
|
|
1202 |
*/
|
|
1203 |
int drmDMA(int fd, drmDMAReqPtr request)
|
|
1204 |
{
|
|
1205 |
drm_dma_t dma;
|
|
1206 |
int ret, i = 0;
|
|
1207 |
|
|
1208 |
/* Copy to hidden structure */
|
|
1209 |
dma.context = request->context;
|
|
1210 |
dma.send_count = request->send_count;
|
|
1211 |
dma.send_indices = request->send_list;
|
|
1212 |
dma.send_sizes = request->send_sizes;
|
|
1213 |
dma.flags = request->flags;
|
|
1214 |
dma.request_count = request->request_count;
|
|
1215 |
dma.request_size = request->request_size;
|
|
1216 |
dma.request_indices = request->request_list;
|
|
1217 |
dma.request_sizes = request->request_sizes;
|
|
1218 |
dma.granted_count = 0;
|
|
1219 |
|
|
1220 |
do {
|
|
1221 |
ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
|
|
1222 |
} while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
|
|
1223 |
|
|
1224 |
if ( ret == 0 ) {
|
|
1225 |
request->granted_count = dma.granted_count;
|
|
1226 |
return 0;
|
|
1227 |
} else {
|
|
1228 |
return -errno;
|
|
1229 |
}
|
|
1230 |
}
|
|
1231 |
|
|
1232 |
|
|
1233 |
/**
|
|
1234 |
* Obtain heavyweight hardware lock.
|
|
1235 |
*
|
|
1236 |
* \param fd file descriptor.
|
|
1237 |
* \param context context.
|
|
1238 |
* \param flags flags that determine the sate of the hardware when the function
|
|
1239 |
* returns.
|
|
1240 |
*
|
|
1241 |
* \return always zero.
|
|
1242 |
*
|
|
1243 |
* \internal
|
|
1244 |
* This function translates the arguments into a drm_lock structure and issue
|
|
1245 |
* the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
|
|
1246 |
*/
|
|
1247 |
int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
|
|
1248 |
{
|
|
1249 |
drm_lock_t lock;
|
|
1250 |
|
|
1251 |
lock.context = context;
|
|
1252 |
lock.flags = 0;
|
|
1253 |
if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
|
|
1254 |
if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
|
|
1255 |
if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
|
|
1256 |
if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
|
|
1257 |
if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
|
|
1258 |
if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
|
|
1259 |
|
|
1260 |
while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
|
|
1261 |
;
|
|
1262 |
return 0;
|
|
1263 |
}
|
|
1264 |
|
|
1265 |
/**
|
|
1266 |
* Release the hardware lock.
|
|
1267 |
*
|
|
1268 |
* \param fd file descriptor.
|
|
1269 |
* \param context context.
|
|
1270 |
*
|
|
1271 |
* \return zero on success, or a negative value on failure.
|
|
1272 |
*
|
|
1273 |
* \internal
|
|
1274 |
* This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
|
|
1275 |
* argument in a drm_lock structure.
|
|
1276 |
*/
|
|
1277 |
int drmUnlock(int fd, drm_context_t context)
|
|
1278 |
{
|
|
1279 |
drm_lock_t lock;
|
|
1280 |
|
|
1281 |
lock.context = context;
|
|
1282 |
lock.flags = 0;
|
|
1283 |
return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
|
|
1284 |
}
|
|
1285 |
|
|
1286 |
drm_context_t * drmGetReservedContextList(int fd, int *count)
|
|
1287 |
{
|
|
1288 |
drm_ctx_res_t res;
|
|
1289 |
drm_ctx_t *list;
|
|
1290 |
drm_context_t * retval;
|
|
1291 |
int i;
|
|
1292 |
|
|
1293 |
res.count = 0;
|
|
1294 |
res.contexts = NULL;
|
|
1295 |
if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
|
|
1296 |
|
|
1297 |
if (!res.count) return NULL;
|
|
1298 |
|
|
1299 |
if (!(list = drmMalloc(res.count * sizeof(*list)))) return NULL;
|
|
1300 |
if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
|
|
1301 |
drmFree(list);
|
|
1302 |
return NULL;
|
|
1303 |
}
|
|
1304 |
|
|
1305 |
res.contexts = list;
|
|
1306 |
if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
|
|
1307 |
|
|
1308 |
for (i = 0; i < res.count; i++) retval[i] = list[i].handle;
|
|
1309 |
drmFree(list);
|
|
1310 |
|
|
1311 |
*count = res.count;
|
|
1312 |
return retval;
|
|
1313 |
}
|
|
1314 |
|
|
1315 |
void drmFreeReservedContextList(drm_context_t * pt)
|
|
1316 |
{
|
|
1317 |
drmFree(pt);
|
|
1318 |
}
|
|
1319 |
|
|
1320 |
/**
|
|
1321 |
* Create context.
|
|
1322 |
*
|
|
1323 |
* Used by the X server during GLXContext initialization. This causes
|
|
1324 |
* per-context kernel-level resources to be allocated.
|
|
1325 |
*
|
|
1326 |
* \param fd file descriptor.
|
|
1327 |
* \param handle is set on success. To be used by the client when requesting DMA
|
|
1328 |
* dispatch with drmDMA().
|
|
1329 |
*
|
|
1330 |
* \return zero on success, or a negative value on failure.
|
|
1331 |
*
|
|
1332 |
* \note May only be called by root.
|
|
1333 |
*
|
|
1334 |
* \internal
|
|
1335 |
* This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
|
|
1336 |
* argument in a drm_ctx structure.
|
|
1337 |
*/
|
|
1338 |
int drmCreateContext(int fd, drm_context_t * handle)
|
|
1339 |
{
|
|
1340 |
drm_ctx_t ctx;
|
|
1341 |
|
|
1342 |
ctx.flags = 0; /* Modified with functions below */
|
|
1343 |
if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx)) return -errno;
|
|
1344 |
*handle = ctx.handle;
|
|
1345 |
return 0;
|
|
1346 |
}
|
|
1347 |
|
|
1348 |
int drmSwitchToContext(int fd, drm_context_t context)
|
|
1349 |
{
|
|
1350 |
drm_ctx_t ctx;
|
|
1351 |
|
|
1352 |
ctx.handle = context;
|
|
1353 |
if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx)) return -errno;
|
|
1354 |
return 0;
|
|
1355 |
}
|
|
1356 |
|
|
1357 |
int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
|
|
1358 |
{
|
|
1359 |
drm_ctx_t ctx;
|
|
1360 |
|
|
1361 |
/* Context preserving means that no context
|
|
1362 |
switched are done between DMA buffers
|
|
1363 |
from one context and the next. This is
|
|
1364 |
suitable for use in the X server (which
|
|
1365 |
promises to maintain hardware context,
|
|
1366 |
or in the client-side library when
|
|
1367 |
buffers are swapped on behalf of two
|
|
1368 |
threads. */
|
|
1369 |
ctx.handle = context;
|
|
1370 |
ctx.flags = 0;
|
|
1371 |
if (flags & DRM_CONTEXT_PRESERVED) ctx.flags |= _DRM_CONTEXT_PRESERVED;
|
|
1372 |
if (flags & DRM_CONTEXT_2DONLY) ctx.flags |= _DRM_CONTEXT_2DONLY;
|
|
1373 |
if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx)) return -errno;
|
|
1374 |
return 0;
|
|
1375 |
}
|
|
1376 |
|
|
1377 |
int drmGetContextFlags(int fd, drm_context_t context, drm_context_tFlagsPtr flags)
|
|
1378 |
{
|
|
1379 |
drm_ctx_t ctx;
|
|
1380 |
|
|
1381 |
ctx.handle = context;
|
|
1382 |
if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx)) return -errno;
|
|
1383 |
*flags = 0;
|
|
1384 |
if (ctx.flags & _DRM_CONTEXT_PRESERVED) *flags |= DRM_CONTEXT_PRESERVED;
|
|
1385 |
if (ctx.flags & _DRM_CONTEXT_2DONLY) *flags |= DRM_CONTEXT_2DONLY;
|
|
1386 |
return 0;
|
|
1387 |
}
|
|
1388 |
|
|
1389 |
/**
|
|
1390 |
* Destroy context.
|
|
1391 |
*
|
|
1392 |
* Free any kernel-level resources allocated with drmCreateContext() associated
|
|
1393 |
* with the context.
|
|
1394 |
*
|
|
1395 |
* \param fd file descriptor.
|
|
1396 |
* \param handle handle given by drmCreateContext().
|
|
1397 |
*
|
|
1398 |
* \return zero on success, or a negative value on failure.
|
|
1399 |
*
|
|
1400 |
* \note May only be called by root.
|
|
1401 |
*
|
|
1402 |
* \internal
|
|
1403 |
* This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
|
|
1404 |
* argument in a drm_ctx structure.
|
|
1405 |
*/
|
|
1406 |
int drmDestroyContext(int fd, drm_context_t handle)
|
|
1407 |
{
|
|
1408 |
drm_ctx_t ctx;
|
|
1409 |
ctx.handle = handle;
|
|
1410 |
if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx)) return -errno;
|
|
1411 |
return 0;
|
|
1412 |
}
|
|
1413 |
|
|
1414 |
int drmCreateDrawable(int fd, drm_drawable_t * handle)
|
|
1415 |
{
|
|
1416 |
drm_draw_t draw;
|
|
1417 |
if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw)) return -errno;
|
|
1418 |
*handle = draw.handle;
|
|
1419 |
return 0;
|
|
1420 |
}
|
|
1421 |
|
|
1422 |
int drmDestroyDrawable(int fd, drm_drawable_t handle)
|
|
1423 |
{
|
|
1424 |
drm_draw_t draw;
|
|
1425 |
draw.handle = handle;
|
|
1426 |
if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw)) return -errno;
|
|
1427 |
return 0;
|
|
1428 |
}
|
|
1429 |
|
|
1430 |
/**
|
|
1431 |
* Acquire the AGP device.
|
|
1432 |
*
|
|
1433 |
* Must be called before any of the other AGP related calls.
|
|
1434 |
*
|
|
1435 |
* \param fd file descriptor.
|
|
1436 |
*
|
|
1437 |
* \return zero on success, or a negative value on failure.
|
|
1438 |
*
|
|
1439 |
* \internal
|
|
1440 |
* This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
|
|
1441 |
*/
|
|
1442 |
int drmAgpAcquire(int fd)
|
|
1443 |
{
|
|
1444 |
if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL)) return -errno;
|
|
1445 |
return 0;
|
|
1446 |
}
|
|
1447 |
|
|
1448 |
|
|
1449 |
/**
|
|
1450 |
* Release the AGP device.
|
|
1451 |
*
|
|
1452 |
* \param fd file descriptor.
|
|
1453 |
*
|
|
1454 |
* \return zero on success, or a negative value on failure.
|
|
1455 |
*
|
|
1456 |
* \internal
|
|
1457 |
* This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
|
|
1458 |
*/
|
|
1459 |
int drmAgpRelease(int fd)
|
|
1460 |
{
|
|
1461 |
if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL)) return -errno;
|
|
1462 |
return 0;
|
|
1463 |
}
|
|
1464 |
|
|
1465 |
|
|
1466 |
/**
|
|
1467 |
* Set the AGP mode.
|
|
1468 |
*
|
|
1469 |
* \param fd file descriptor.
|
|
1470 |
* \param mode AGP mode.
|
|
1471 |
*
|
|
1472 |
* \return zero on success, or a negative value on failure.
|
|
1473 |
*
|
|
1474 |
* \internal
|
|
1475 |
* This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
|
|
1476 |
* argument in a drm_agp_mode structure.
|
|
1477 |
*/
|
|
1478 |
int drmAgpEnable(int fd, unsigned long mode)
|
|
1479 |
{
|
|
1480 |
drm_agp_mode_t m;
|
|
1481 |
|
|
1482 |
m.mode = mode;
|
|
1483 |
if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m)) return -errno;
|
|
1484 |
return 0;
|
|
1485 |
}
|
|
1486 |
|
|
1487 |
|
|
1488 |
/**
|
|
1489 |
* Allocate a chunk of AGP memory.
|
|
1490 |
*
|
|
1491 |
* \param fd file descriptor.
|
|
1492 |
* \param size requested memory size in bytes. Will be rounded to page boundary.
|
|
1493 |
* \param type type of memory to allocate.
|
|
1494 |
* \param address if not zero, will be set to the physical address of the
|
|
1495 |
* allocated memory.
|
|
1496 |
* \param handle on success will be set to a handle of the allocated memory.
|
|
1497 |
*
|
|
1498 |
* \return zero on success, or a negative value on failure.
|
|
1499 |
*
|
|
1500 |
* \internal
|
|
1501 |
* This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
|
|
1502 |
* arguments in a drm_agp_buffer structure.
|
|
1503 |
*/
|
|
1504 |
int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
|
|
1505 |
unsigned long *address, drm_handle_t *handle)
|
|
1506 |
{
|
|
1507 |
drm_agp_buffer_t b;
|
|
1508 |
|
|
1509 |
*handle = DRM_AGP_NO_HANDLE;
|
|
1510 |
b.size = size;
|
|
1511 |
b.handle = 0;
|
|
1512 |
b.type = type;
|
|
1513 |
if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b)) return -errno;
|
|
1514 |
if (address != 0UL) *address = b.physical;
|
|
1515 |
*handle = b.handle;
|
|
1516 |
return 0;
|
|
1517 |
}
|
|
1518 |
|
|
1519 |
|
|
1520 |
/**
|
|
1521 |
* Free a chunk of AGP memory.
|
|
1522 |
*
|
|
1523 |
* \param fd file descriptor.
|
|
1524 |
* \param handle handle to the allocated memory, as given by drmAgpAllocate().
|
|
1525 |
*
|
|
1526 |
* \return zero on success, or a negative value on failure.
|
|
1527 |
*
|
|
1528 |
* \internal
|
|
1529 |
* This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
|
|
1530 |
* argument in a drm_agp_buffer structure.
|
|
1531 |
*/
|
|
1532 |
int drmAgpFree(int fd, drm_handle_t handle)
|
|
1533 |
{
|
|
1534 |
drm_agp_buffer_t b;
|
|
1535 |
|
|
1536 |
b.size = 0;
|
|
1537 |
b.handle = handle;
|
|
1538 |
if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b)) return -errno;
|
|
1539 |
return 0;
|
|
1540 |
}
|
|
1541 |
|
|
1542 |
|
|
1543 |
/**
|
|
1544 |
* Bind a chunk of AGP memory.
|
|
1545 |
*
|
|
1546 |
* \param fd file descriptor.
|
|
1547 |
* \param handle handle to the allocated memory, as given by drmAgpAllocate().
|
|
1548 |
* \param offset offset in bytes. It will round to page boundary.
|
|
1549 |
*
|
|
1550 |
* \return zero on success, or a negative value on failure.
|
|
1551 |
*
|
|
1552 |
* \internal
|
|
1553 |
* This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
|
|
1554 |
* argument in a drm_agp_binding structure.
|
|
1555 |
*/
|
|
1556 |
int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
|
|
1557 |
{
|
|
1558 |
drm_agp_binding_t b;
|
|
1559 |
|
|
1560 |
b.handle = handle;
|
|
1561 |
b.offset = offset;
|
|
1562 |
if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b)) return -errno;
|
|
1563 |
return 0;
|
|
1564 |
}
|
|
1565 |
|
|
1566 |
|
|
1567 |
/**
|
|
1568 |
* Unbind a chunk of AGP memory.
|
|
1569 |
*
|
|
1570 |
* \param fd file descriptor.
|
|
1571 |
* \param handle handle to the allocated memory, as given by drmAgpAllocate().
|
|
1572 |
*
|
|
1573 |
* \return zero on success, or a negative value on failure.
|
|
1574 |
*
|
|
1575 |
* \internal
|
|
1576 |
* This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
|
|
1577 |
* the argument in a drm_agp_binding structure.
|
|
1578 |
*/
|
|
1579 |
int drmAgpUnbind(int fd, drm_handle_t handle)
|
|
1580 |
{
|
|
1581 |
drm_agp_binding_t b;
|
|
1582 |
|
|
1583 |
b.handle = handle;
|
|
1584 |
b.offset = 0;
|
|
1585 |
if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b)) return -errno;
|
|
1586 |
return 0;
|
|
1587 |
}
|
|
1588 |
|
|
1589 |
|
|
1590 |
/**
|
|
1591 |
* Get AGP driver major version number.
|
|
1592 |
*
|
|
1593 |
* \param fd file descriptor.
|
|
1594 |
*
|
|
1595 |
* \return major version number on success, or a negative value on failure..
|
|
1596 |
*
|
|
1597 |
* \internal
|
|
1598 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1599 |
* necessary information in a drm_agp_info structure.
|
|
1600 |
*/
|
|
1601 |
int drmAgpVersionMajor(int fd)
|
|
1602 |
{
|
|
1603 |
drm_agp_info_t i;
|
|
1604 |
|
|
1605 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
|
|
1606 |
return i.agp_version_major;
|
|
1607 |
}
|
|
1608 |
|
|
1609 |
|
|
1610 |
/**
|
|
1611 |
* Get AGP driver minor version number.
|
|
1612 |
*
|
|
1613 |
* \param fd file descriptor.
|
|
1614 |
*
|
|
1615 |
* \return minor version number on success, or a negative value on failure.
|
|
1616 |
*
|
|
1617 |
* \internal
|
|
1618 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1619 |
* necessary information in a drm_agp_info structure.
|
|
1620 |
*/
|
|
1621 |
int drmAgpVersionMinor(int fd)
|
|
1622 |
{
|
|
1623 |
drm_agp_info_t i;
|
|
1624 |
|
|
1625 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
|
|
1626 |
return i.agp_version_minor;
|
|
1627 |
}
|
|
1628 |
|
|
1629 |
|
|
1630 |
/**
|
|
1631 |
* Get AGP mode.
|
|
1632 |
*
|
|
1633 |
* \param fd file descriptor.
|
|
1634 |
*
|
|
1635 |
* \return mode on success, or zero on failure.
|
|
1636 |
*
|
|
1637 |
* \internal
|
|
1638 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1639 |
* necessary information in a drm_agp_info structure.
|
|
1640 |
*/
|
|
1641 |
unsigned long drmAgpGetMode(int fd)
|
|
1642 |
{
|
|
1643 |
drm_agp_info_t i;
|
|
1644 |
|
|
1645 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
|
|
1646 |
return i.mode;
|
|
1647 |
}
|
|
1648 |
|
|
1649 |
|
|
1650 |
/**
|
|
1651 |
* Get AGP aperture base.
|
|
1652 |
*
|
|
1653 |
* \param fd file descriptor.
|
|
1654 |
*
|
|
1655 |
* \return aperture base on success, zero on failure.
|
|
1656 |
*
|
|
1657 |
* \internal
|
|
1658 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1659 |
* necessary information in a drm_agp_info structure.
|
|
1660 |
*/
|
|
1661 |
unsigned long drmAgpBase(int fd)
|
|
1662 |
{
|
|
1663 |
drm_agp_info_t i;
|
|
1664 |
|
|
1665 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
|
|
1666 |
return i.aperture_base;
|
|
1667 |
}
|
|
1668 |
|
|
1669 |
|
|
1670 |
/**
|
|
1671 |
* Get AGP aperture size.
|
|
1672 |
*
|
|
1673 |
* \param fd file descriptor.
|
|
1674 |
*
|
|
1675 |
* \return aperture size on success, zero on failure.
|
|
1676 |
*
|
|
1677 |
* \internal
|
|
1678 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1679 |
* necessary information in a drm_agp_info structure.
|
|
1680 |
*/
|
|
1681 |
unsigned long drmAgpSize(int fd)
|
|
1682 |
{
|
|
1683 |
drm_agp_info_t i;
|
|
1684 |
|
|
1685 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
|
|
1686 |
return i.aperture_size;
|
|
1687 |
}
|
|
1688 |
|
|
1689 |
|
|
1690 |
/**
|
|
1691 |
* Get used AGP memory.
|
|
1692 |
*
|
|
1693 |
* \param fd file descriptor.
|
|
1694 |
*
|
|
1695 |
* \return memory used on success, or zero on failure.
|
|
1696 |
*
|
|
1697 |
* \internal
|
|
1698 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1699 |
* necessary information in a drm_agp_info structure.
|
|
1700 |
*/
|
|
1701 |
unsigned long drmAgpMemoryUsed(int fd)
|
|
1702 |
{
|
|
1703 |
drm_agp_info_t i;
|
|
1704 |
|
|
1705 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
|
|
1706 |
return i.memory_used;
|
|
1707 |
}
|
|
1708 |
|
|
1709 |
|
|
1710 |
/**
|
|
1711 |
* Get available AGP memory.
|
|
1712 |
*
|
|
1713 |
* \param fd file descriptor.
|
|
1714 |
*
|
|
1715 |
* \return memory available on success, or zero on failure.
|
|
1716 |
*
|
|
1717 |
* \internal
|
|
1718 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1719 |
* necessary information in a drm_agp_info structure.
|
|
1720 |
*/
|
|
1721 |
unsigned long drmAgpMemoryAvail(int fd)
|
|
1722 |
{
|
|
1723 |
drm_agp_info_t i;
|
|
1724 |
|
|
1725 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
|
|
1726 |
return i.memory_allowed;
|
|
1727 |
}
|
|
1728 |
|
|
1729 |
|
|
1730 |
/**
|
|
1731 |
* Get hardware vendor ID.
|
|
1732 |
*
|
|
1733 |
* \param fd file descriptor.
|
|
1734 |
*
|
|
1735 |
* \return vendor ID on success, or zero on failure.
|
|
1736 |
*
|
|
1737 |
* \internal
|
|
1738 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1739 |
* necessary information in a drm_agp_info structure.
|
|
1740 |
*/
|
|
1741 |
unsigned int drmAgpVendorId(int fd)
|
|
1742 |
{
|
|
1743 |
drm_agp_info_t i;
|
|
1744 |
|
|
1745 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
|
|
1746 |
return i.id_vendor;
|
|
1747 |
}
|
|
1748 |
|
|
1749 |
|
|
1750 |
/**
|
|
1751 |
* Get hardware device ID.
|
|
1752 |
*
|
|
1753 |
* \param fd file descriptor.
|
|
1754 |
*
|
|
1755 |
* \return zero on success, or zero on failure.
|
|
1756 |
*
|
|
1757 |
* \internal
|
|
1758 |
* This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
|
|
1759 |
* necessary information in a drm_agp_info structure.
|
|
1760 |
*/
|
|
1761 |
unsigned int drmAgpDeviceId(int fd)
|
|
1762 |
{
|
|
1763 |
drm_agp_info_t i;
|
|
1764 |
|
|
1765 |
if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
|
|
1766 |
return i.id_device;
|
|
1767 |
}
|
|
1768 |
|
|
1769 |
int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
|
|
1770 |
{
|
|
1771 |
drm_scatter_gather_t sg;
|
|
1772 |
|
|
1773 |
*handle = 0;
|
|
1774 |
sg.size = size;
|
|
1775 |
sg.handle = 0;
|
|
1776 |
if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg)) return -errno;
|
|
1777 |
*handle = sg.handle;
|
|
1778 |
return 0;
|
|
1779 |
}
|
|
1780 |
|
|
1781 |
int drmScatterGatherFree(int fd, drm_handle_t handle)
|
|
1782 |
{
|
|
1783 |
drm_scatter_gather_t sg;
|
|
1784 |
|
|
1785 |
sg.size = 0;
|
|
1786 |
sg.handle = handle;
|
|
1787 |
if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg)) return -errno;
|
|
1788 |
return 0;
|
|
1789 |
}
|
|
1790 |
|
|
1791 |
/**
|
|
1792 |
* Wait for VBLANK.
|
|
1793 |
*
|
|
1794 |
* \param fd file descriptor.
|
|
1795 |
* \param vbl pointer to a drmVBlank structure.
|
|
1796 |
*
|
|
1797 |
* \return zero on success, or a negative value on failure.
|
|
1798 |
*
|
|
1799 |
* \internal
|
|
1800 |
* This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
|
|
1801 |
*/
|
|
1802 |
int drmWaitVBlank(int fd, drmVBlankPtr vbl)
|
|
1803 |
{
|
|
1804 |
int ret;
|
|
1805 |
|
|
1806 |
do {
|
|
1807 |
ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
|
|
1808 |
vbl->request.type &= ~DRM_VBLANK_RELATIVE;
|
|
1809 |
} while (ret && errno == EINTR);
|
|
1810 |
|
|
1811 |
return ret;
|
|
1812 |
}
|
|
1813 |
|
|
1814 |
int drmError(int err, const char *label)
|
|
1815 |
{
|
|
1816 |
switch (err) {
|
|
1817 |
case DRM_ERR_NO_DEVICE: fprintf(stderr, "%s: no device\n", label); break;
|
|
1818 |
case DRM_ERR_NO_ACCESS: fprintf(stderr, "%s: no access\n", label); break;
|
|
1819 |
case DRM_ERR_NOT_ROOT: fprintf(stderr, "%s: not root\n", label); break;
|
|
1820 |
case DRM_ERR_INVALID: fprintf(stderr, "%s: invalid args\n", label);break;
|
|
1821 |
default:
|
|
1822 |
if (err < 0) err = -err;
|
|
1823 |
fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
|
|
1824 |
break;
|
|
1825 |
}
|
|
1826 |
|
|
1827 |
return 1;
|
|
1828 |
}
|
|
1829 |
|
|
1830 |
/**
|
|
1831 |
* Install IRQ handler.
|
|
1832 |
*
|
|
1833 |
* \param fd file descriptor.
|
|
1834 |
* \param irq IRQ number.
|
|
1835 |
*
|
|
1836 |
* \return zero on success, or a negative value on failure.
|
|
1837 |
*
|
|
1838 |
* \internal
|
|
1839 |
* This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
|
|
1840 |
* argument in a drm_control structure.
|
|
1841 |
*/
|
|
1842 |
int drmCtlInstHandler(int fd, int irq)
|
|
1843 |
{
|
|
1844 |
drm_control_t ctl;
|
|
1845 |
|
|
1846 |
ctl.func = DRM_INST_HANDLER;
|
|
1847 |
ctl.irq = irq;
|
|
1848 |
if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
|
|
1849 |
return 0;
|
|
1850 |
}
|
|
1851 |
|
|
1852 |
|
|
1853 |
/**
|
|
1854 |
* Uninstall IRQ handler.
|
|
1855 |
*
|
|
1856 |
* \param fd file descriptor.
|
|
1857 |
*
|
|
1858 |
* \return zero on success, or a negative value on failure.
|
|
1859 |
*
|
|
1860 |
* \internal
|
|
1861 |
* This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
|
|
1862 |
* argument in a drm_control structure.
|
|
1863 |
*/
|
|
1864 |
int drmCtlUninstHandler(int fd)
|
|
1865 |
{
|
|
1866 |
drm_control_t ctl;
|
|
1867 |
|
|
1868 |
ctl.func = DRM_UNINST_HANDLER;
|
|
1869 |
ctl.irq = 0;
|
|
1870 |
if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
|
|
1871 |
return 0;
|
|
1872 |
}
|
|
1873 |
|
|
1874 |
int drmFinish(int fd, int context, drmLockFlags flags)
|
|
1875 |
{
|
|
1876 |
drm_lock_t lock;
|
|
1877 |
|
|
1878 |
lock.context = context;
|
|
1879 |
lock.flags = 0;
|
|
1880 |
if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
|
|
1881 |
if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
|
|
1882 |
if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
|
|
1883 |
if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
|
|
1884 |
if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
|
|
1885 |
if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
|
|
1886 |
if (ioctl(fd, DRM_IOCTL_FINISH, &lock)) return -errno;
|
|
1887 |
return 0;
|
|
1888 |
}
|
|
1889 |
|
|
1890 |
/**
|
|
1891 |
* Get IRQ from bus ID.
|
|
1892 |
*
|
|
1893 |
* \param fd file descriptor.
|
|
1894 |
* \param busnum bus number.
|
|
1895 |
* \param devnum device number.
|
|
1896 |
* \param funcnum function number.
|
|
1897 |
*
|
|
1898 |
* \return IRQ number on success, or a negative value on failure.
|
|
1899 |
*
|
|
1900 |
* \internal
|
|
1901 |
* This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
|
|
1902 |
* arguments in a drm_irq_busid structure.
|
|
1903 |
*/
|
|
1904 |
int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
|
|
1905 |
{
|
|
1906 |
drm_irq_busid_t p;
|
|
1907 |
|
|
1908 |
p.busnum = busnum;
|
|
1909 |
p.devnum = devnum;
|
|
1910 |
p.funcnum = funcnum;
|
|
1911 |
if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p)) return -errno;
|
|
1912 |
return p.irq;
|
|
1913 |
}
|
|
1914 |
|
|
1915 |
int drmAddContextTag(int fd, drm_context_t context, void *tag)
|
|
1916 |
{
|
|
1917 |
drmHashEntry *entry = drmGetEntry(fd);
|
|
1918 |
|
|
1919 |
if (drmHashInsert(entry->tagTable, context, tag)) {
|
|
1920 |
drmHashDelete(entry->tagTable, context);
|
|
1921 |
drmHashInsert(entry->tagTable, context, tag);
|
|
1922 |
}
|
|
1923 |
return 0;
|
|
1924 |
}
|
|
1925 |
|
|
1926 |
int drmDelContextTag(int fd, drm_context_t context)
|
|
1927 |
{
|
|
1928 |
drmHashEntry *entry = drmGetEntry(fd);
|
|
1929 |
|
|
1930 |
return drmHashDelete(entry->tagTable, context);
|
|
1931 |
}
|
|
1932 |
|
|
1933 |
void *drmGetContextTag(int fd, drm_context_t context)
|
|
1934 |
{
|
|
1935 |
drmHashEntry *entry = drmGetEntry(fd);
|
|
1936 |
void *value;
|
|
1937 |
|
|
1938 |
if (drmHashLookup(entry->tagTable, context, &value)) return NULL;
|
|
1939 |
|
|
1940 |
return value;
|
|
1941 |
}
|
|
1942 |
|
|
1943 |
int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id, drm_handle_t handle)
|
|
1944 |
{
|
|
1945 |
drm_ctx_priv_map_t map;
|
|
1946 |
|
|
1947 |
map.ctx_id = ctx_id;
|
|
1948 |
map.handle = (void *)(unsigned long)handle;
|
|
1949 |
|
|
1950 |
if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map)) return -errno;
|
|
1951 |
return 0;
|
|
1952 |
}
|
|
1953 |
|
|
1954 |
int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id, drm_handle_t * handle)
|
|
1955 |
{
|
|
1956 |
drm_ctx_priv_map_t map;
|
|
1957 |
|
|
1958 |
map.ctx_id = ctx_id;
|
|
1959 |
|
|
1960 |
if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map)) return -errno;
|
|
1961 |
if (handle) *handle = (drm_handle_t)(unsigned long)map.handle;
|
|
1962 |
|
|
1963 |
return 0;
|
|
1964 |
}
|
|
1965 |
|
|
1966 |
int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
|
|
1967 |
drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
|
|
1968 |
int *mtrr)
|
|
1969 |
{
|
|
1970 |
drm_map_t map;
|
|
1971 |
|
|
1972 |
map.offset = idx;
|
|
1973 |
if (ioctl(fd, DRM_IOCTL_GET_MAP, &map)) return -errno;
|
|
1974 |
*offset = map.offset;
|
|
1975 |
*size = map.size;
|
|
1976 |
*type = map.type;
|
|
1977 |
*flags = map.flags;
|
|
1978 |
*handle = (drm_handle_t)(unsigned long)map.handle;
|
|
1979 |
*mtrr = map.mtrr;
|
|
1980 |
return 0;
|
|
1981 |
}
|
|
1982 |
|
|
1983 |
int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
|
|
1984 |
unsigned long *magic, unsigned long *iocs)
|
|
1985 |
{
|
|
1986 |
drm_client_t client;
|
|
1987 |
|
|
1988 |
client.idx = idx;
|
|
1989 |
if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client)) return -errno;
|
|
1990 |
*auth = client.auth;
|
|
1991 |
*pid = client.pid;
|
|
1992 |
*uid = client.uid;
|
|
1993 |
*magic = client.magic;
|
|
1994 |
*iocs = client.iocs;
|
|
1995 |
return 0;
|
|
1996 |
}
|
|
1997 |
|
|
1998 |
int drmGetStats(int fd, drmStatsT *stats)
|
|
1999 |
{
|
|
2000 |
drm_stats_t s;
|
|
2001 |
unsigned int i;
|
|
2002 |
|
|
2003 |
if (ioctl(fd, DRM_IOCTL_GET_STATS, &s)) return -errno;
|
|
2004 |
|
|
2005 |
stats->count = 0;
|
|
2006 |
memset(stats, 0, sizeof(*stats));
|
|
2007 |
if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
|
|
2008 |
return -1;
|
|
2009 |
|
|
2010 |
#define SET_VALUE \
|
|
2011 |
stats->data[i].long_format = "%-20.20s"; \
|
|
2012 |
stats->data[i].rate_format = "%8.8s"; \
|
|
2013 |
stats->data[i].isvalue = 1; \
|
|
2014 |
stats->data[i].verbose = 0
|
|
2015 |
|
|
2016 |
#define SET_COUNT \
|
|
2017 |
stats->data[i].long_format = "%-20.20s"; \
|
|
2018 |
stats->data[i].rate_format = "%5.5s"; \
|
|
2019 |
stats->data[i].isvalue = 0; \
|
|
2020 |
stats->data[i].mult_names = "kgm"; \
|
|
2021 |
stats->data[i].mult = 1000; \
|
|
2022 |
stats->data[i].verbose = 0
|
|
2023 |
|
|
2024 |
#define SET_BYTE \
|
|
2025 |
stats->data[i].long_format = "%-20.20s"; \
|
|
2026 |
stats->data[i].rate_format = "%5.5s"; \
|
|
2027 |
stats->data[i].isvalue = 0; \
|
|
2028 |
stats->data[i].mult_names = "KGM"; \
|
|
2029 |
stats->data[i].mult = 1024; \
|
|
2030 |
stats->data[i].verbose = 0
|
|
2031 |
|
|
2032 |
|
|
2033 |
stats->count = s.count;
|
|
2034 |
for (i = 0; i < s.count; i++) {
|
|
2035 |
stats->data[i].value = s.data[i].value;
|
|
2036 |
switch (s.data[i].type) {
|
|
2037 |
case _DRM_STAT_LOCK:
|
|
2038 |
stats->data[i].long_name = "Lock";
|
|
2039 |
stats->data[i].rate_name = "Lock";
|
|
2040 |
SET_VALUE;
|
|
2041 |
break;
|
|
2042 |
case _DRM_STAT_OPENS:
|
|
2043 |
stats->data[i].long_name = "Opens";
|
|
2044 |
stats->data[i].rate_name = "O";
|
|
2045 |
SET_COUNT;
|
|
2046 |
stats->data[i].verbose = 1;
|
|
2047 |
break;
|
|
2048 |
case _DRM_STAT_CLOSES:
|
|
2049 |
stats->data[i].long_name = "Closes";
|
|
2050 |
stats->data[i].rate_name = "Lock";
|
|
2051 |
SET_COUNT;
|
|
2052 |
stats->data[i].verbose = 1;
|
|
2053 |
break;
|
|
2054 |
case _DRM_STAT_IOCTLS:
|
|
2055 |
stats->data[i].long_name = "Ioctls";
|
|
2056 |
stats->data[i].rate_name = "Ioc/s";
|
|
2057 |
SET_COUNT;
|
|
2058 |
break;
|
|
2059 |
case _DRM_STAT_LOCKS:
|
|
2060 |
stats->data[i].long_name = "Locks";
|
|
2061 |
stats->data[i].rate_name = "Lck/s";
|
|
2062 |
SET_COUNT;
|
|
2063 |
break;
|
|
2064 |
case _DRM_STAT_UNLOCKS:
|
|
2065 |
stats->data[i].long_name = "Unlocks";
|
|
2066 |
stats->data[i].rate_name = "Unl/s";
|
|
2067 |
SET_COUNT;
|
|
2068 |
break;
|
|
2069 |
case _DRM_STAT_IRQ:
|
|
2070 |
stats->data[i].long_name = "IRQs";
|
|
2071 |
stats->data[i].rate_name = "IRQ/s";
|
|
2072 |
SET_COUNT;
|
|
2073 |
break;
|
|
2074 |
case _DRM_STAT_PRIMARY:
|
|
2075 |
stats->data[i].long_name = "Primary Bytes";
|
|
2076 |
stats->data[i].rate_name = "PB/s";
|
|
2077 |
SET_BYTE;
|
|
2078 |
break;
|
|
2079 |
case _DRM_STAT_SECONDARY:
|
|
2080 |
stats->data[i].long_name = "Secondary Bytes";
|
|
2081 |
stats->data[i].rate_name = "SB/s";
|
|
2082 |
SET_BYTE;
|
|
2083 |
break;
|
|
2084 |
case _DRM_STAT_DMA:
|
|
2085 |
stats->data[i].long_name = "DMA";
|
|
2086 |
stats->data[i].rate_name = "DMA/s";
|
|
2087 |
SET_COUNT;
|
|
2088 |
break;
|
|
2089 |
case _DRM_STAT_SPECIAL:
|
|
2090 |
stats->data[i].long_name = "Special DMA";
|
|
2091 |
stats->data[i].rate_name = "dma/s";
|
|
2092 |
SET_COUNT;
|
|
2093 |
break;
|
|
2094 |
case _DRM_STAT_MISSED:
|
|
2095 |
stats->data[i].long_name = "Miss";
|
|
2096 |
stats->data[i].rate_name = "Ms/s";
|
|
2097 |
SET_COUNT;
|
|
2098 |
break;
|
|
2099 |
case _DRM_STAT_VALUE:
|
|
2100 |
stats->data[i].long_name = "Value";
|
|
2101 |
stats->data[i].rate_name = "Value";
|
|
2102 |
SET_VALUE;
|
|
2103 |
break;
|
|
2104 |
case _DRM_STAT_BYTE:
|
|
2105 |
stats->data[i].long_name = "Bytes";
|
|
2106 |
stats->data[i].rate_name = "B/s";
|
|
2107 |
SET_BYTE;
|
|
2108 |
break;
|
|
2109 |
case _DRM_STAT_COUNT:
|
|
2110 |
default:
|
|
2111 |
stats->data[i].long_name = "Count";
|
|
2112 |
stats->data[i].rate_name = "Cnt/s";
|
|
2113 |
SET_COUNT;
|
|
2114 |
break;
|
|
2115 |
}
|
|
2116 |
}
|
|
2117 |
return 0;
|
|
2118 |
}
|
|
2119 |
|
|
2120 |
/**
|
|
2121 |
* Issue a set-version ioctl.
|
|
2122 |
*
|
|
2123 |
* \param fd file descriptor.
|
|
2124 |
* \param drmCommandIndex command index
|
|
2125 |
* \param data source pointer of the data to be read and written.
|
|
2126 |
* \param size size of the data to be read and written.
|
|
2127 |
*
|
|
2128 |
* \return zero on success, or a negative value on failure.
|
|
2129 |
*
|
|
2130 |
* \internal
|
|
2131 |
* It issues a read-write ioctl given by
|
|
2132 |
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
|
|
2133 |
*/
|
|
2134 |
int drmSetInterfaceVersion(int fd, drmSetVersion *version )
|
|
2135 |
{
|
|
2136 |
int retcode = 0;
|
|
2137 |
drm_set_version_t sv;
|
|
2138 |
|
|
2139 |
sv.drm_di_major = version->drm_di_major;
|
|
2140 |
sv.drm_di_minor = version->drm_di_minor;
|
|
2141 |
sv.drm_dd_major = version->drm_dd_major;
|
|
2142 |
sv.drm_dd_minor = version->drm_dd_minor;
|
|
2143 |
|
|
2144 |
if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
|
|
2145 |
retcode = -errno;
|
|
2146 |
}
|
|
2147 |
|
|
2148 |
version->drm_di_major = sv.drm_di_major;
|
|
2149 |
version->drm_di_minor = sv.drm_di_minor;
|
|
2150 |
version->drm_dd_major = sv.drm_dd_major;
|
|
2151 |
version->drm_dd_minor = sv.drm_dd_minor;
|
|
2152 |
|
|
2153 |
return retcode;
|
|
2154 |
}
|
|
2155 |
|
|
2156 |
/**
|
|
2157 |
* Send a device-specific command.
|
|
2158 |
*
|
|
2159 |
* \param fd file descriptor.
|
|
2160 |
* \param drmCommandIndex command index
|
|
2161 |
*
|
|
2162 |
* \return zero on success, or a negative value on failure.
|
|
2163 |
*
|
|
2164 |
* \internal
|
|
2165 |
* It issues a ioctl given by
|
|
2166 |
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
|
|
2167 |
*/
|
|
2168 |
int drmCommandNone(int fd, unsigned long drmCommandIndex)
|
|
2169 |
{
|
|
2170 |
void *data = NULL; /* dummy */
|
|
2171 |
unsigned long request;
|
|
2172 |
|
|
2173 |
request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
|
|
2174 |
|
|
2175 |
if (ioctl(fd, request, data)) {
|
|
2176 |
return -errno;
|
|
2177 |
}
|
|
2178 |
return 0;
|
|
2179 |
}
|
|
2180 |
|
|
2181 |
|
|
2182 |
/**
|
|
2183 |
* Send a device-specific read command.
|
|
2184 |
*
|
|
2185 |
* \param fd file descriptor.
|
|
2186 |
* \param drmCommandIndex command index
|
|
2187 |
* \param data destination pointer of the data to be read.
|
|
2188 |
* \param size size of the data to be read.
|
|
2189 |
*
|
|
2190 |
* \return zero on success, or a negative value on failure.
|
|
2191 |
*
|
|
2192 |
* \internal
|
|
2193 |
* It issues a read ioctl given by
|
|
2194 |
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
|
|
2195 |
*/
|
|
2196 |
int drmCommandRead(int fd, unsigned long drmCommandIndex,
|
|
2197 |
void *data, unsigned long size )
|
|
2198 |
{
|
|
2199 |
unsigned long request;
|
|
2200 |
|
|
2201 |
request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
|
|
2202 |
DRM_COMMAND_BASE + drmCommandIndex, size);
|
|
2203 |
|
|
2204 |
if (ioctl(fd, request, data)) {
|
|
2205 |
return -errno;
|
|
2206 |
}
|
|
2207 |
return 0;
|
|
2208 |
}
|
|
2209 |
|
|
2210 |
|
|
2211 |
/**
|
|
2212 |
* Send a device-specific write command.
|
|
2213 |
*
|
|
2214 |
* \param fd file descriptor.
|
|
2215 |
* \param drmCommandIndex command index
|
|
2216 |
* \param data source pointer of the data to be written.
|
|
2217 |
* \param size size of the data to be written.
|
|
2218 |
*
|
|
2219 |
* \return zero on success, or a negative value on failure.
|
|
2220 |
*
|
|
2221 |
* \internal
|
|
2222 |
* It issues a write ioctl given by
|
|
2223 |
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
|
|
2224 |
*/
|
|
2225 |
int drmCommandWrite(int fd, unsigned long drmCommandIndex,
|
|
2226 |
void *data, unsigned long size )
|
|
2227 |
{
|
|
2228 |
unsigned long request;
|
|
2229 |
|
|
2230 |
request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
|
|
2231 |
DRM_COMMAND_BASE + drmCommandIndex, size);
|
|
2232 |
|
|
2233 |
if (ioctl(fd, request, data)) {
|
|
2234 |
return -errno;
|
|
2235 |
}
|
|
2236 |
return 0;
|
|
2237 |
}
|
|
2238 |
|
|
2239 |
|
|
2240 |
/**
|
|
2241 |
* Send a device-specific read-write command.
|
|
2242 |
*
|
|
2243 |
* \param fd file descriptor.
|
|
2244 |
* \param drmCommandIndex command index
|
|
2245 |
* \param data source pointer of the data to be read and written.
|
|
2246 |
* \param size size of the data to be read and written.
|
|
2247 |
*
|
|
2248 |
* \return zero on success, or a negative value on failure.
|
|
2249 |
*
|
|
2250 |
* \internal
|
|
2251 |
* It issues a read-write ioctl given by
|
|
2252 |
* \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
|
|
2253 |
*/
|
|
2254 |
int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
|
|
2255 |
void *data, unsigned long size )
|
|
2256 |
{
|
|
2257 |
unsigned long request;
|
|
2258 |
|
|
2259 |
request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
|
|
2260 |
DRM_COMMAND_BASE + drmCommandIndex, size);
|
|
2261 |
|
|
2262 |
if (ioctl(fd, request, data)) {
|
|
2263 |
return -errno;
|
|
2264 |
}
|
|
2265 |
return 0;
|
|
2266 |
}
|
|
2267 |
|
|
2268 |
#if defined(XFree86Server)
|
|
2269 |
static void drmSIGIOHandler(int interrupt, void *closure)
|
|
2270 |
{
|
|
2271 |
unsigned long key;
|
|
2272 |
void *value;
|
|
2273 |
ssize_t count;
|
|
2274 |
drm_ctx_t ctx;
|
|
2275 |
typedef void (*_drmCallback)(int, void *, void *);
|
|
2276 |
char buf[256];
|
|
2277 |
drm_context_t old;
|
|
2278 |
drm_context_t new;
|
|
2279 |
void *oldctx;
|
|
2280 |
void *newctx;
|
|
2281 |
char *pt;
|
|
2282 |
drmHashEntry *entry;
|
|
2283 |
|
|
2284 |
if (!drmHashTable) return;
|
|
2285 |
if (drmHashFirst(drmHashTable, &key, &value)) {
|
|
2286 |
entry = value;
|
|
2287 |
do {
|
|
2288 |
#if 0
|
|
2289 |
fprintf(stderr, "Trying %d\n", entry->fd);
|
|
2290 |
#endif
|
|
2291 |
if ((count = read(entry->fd, buf, sizeof(buf))) > 0) {
|
|
2292 |
buf[count] = '\0';
|
|
2293 |
#if 0
|
|
2294 |
fprintf(stderr, "Got %s\n", buf);
|
|
2295 |
#endif
|
|
2296 |
|
|
2297 |
for (pt = buf; *pt != ' '; ++pt); /* Find first space */
|
|
2298 |
++pt;
|
|
2299 |
old = strtol(pt, &pt, 0);
|
|
2300 |
new = strtol(pt, NULL, 0);
|
|
2301 |
oldctx = drmGetContextTag(entry->fd, old);
|
|
2302 |
newctx = drmGetContextTag(entry->fd, new);
|
|
2303 |
#if 0
|
|
2304 |
fprintf(stderr, "%d %d %p %p\n", old, new, oldctx, newctx);
|
|
2305 |
#endif
|
|
2306 |
((_drmCallback)entry->f)(entry->fd, oldctx, newctx);
|
|
2307 |
ctx.handle = new;
|
|
2308 |
ioctl(entry->fd, DRM_IOCTL_NEW_CTX, &ctx);
|
|
2309 |
}
|
|
2310 |
} while (drmHashNext(drmHashTable, &key, &value));
|
|
2311 |
}
|
|
2312 |
}
|
|
2313 |
|
|
2314 |
int drmInstallSIGIOHandler(int fd, void (*f)(int, void *, void *))
|
|
2315 |
{
|
|
2316 |
drmHashEntry *entry;
|
|
2317 |
|
|
2318 |
entry = drmGetEntry(fd);
|
|
2319 |
entry->f = f;
|
|
2320 |
|
|
2321 |
#if defined(sun)
|
|
2322 |
return 1;
|
|
2323 |
#else
|
|
2324 |
return xf86InstallSIGIOHandler(fd, drmSIGIOHandler, 0);
|
|
2325 |
#endif
|
|
2326 |
}
|
|
2327 |
|
|
2328 |
int drmRemoveSIGIOHandler(int fd)
|
|
2329 |
{
|
|
2330 |
drmHashEntry *entry = drmGetEntry(fd);
|
|
2331 |
|
|
2332 |
entry->f = NULL;
|
|
2333 |
|
|
2334 |
#if defined(sun)
|
|
2335 |
return 1;
|
|
2336 |
#else
|
|
2337 |
return xf86RemoveSIGIOHandler(fd);
|
|
2338 |
#endif
|
|
2339 |
}
|
|
2340 |
#endif
|