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