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