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