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