Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1 | /** |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2 | * \file xf86drm.c |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 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 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 36 | #include <stdbool.h> |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | #include <string.h> |
Ian Romanick | 015efd1 | 2009-07-06 09:23:59 -0700 | [diff] [blame] | 39 | #include <strings.h> |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 40 | #include <ctype.h> |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 41 | #include <dirent.h> |
| 42 | #include <stddef.h> |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 43 | #include <fcntl.h> |
| 44 | #include <errno.h> |
Felix Janda | 4031dc1 | 2015-09-26 08:08:43 +0200 | [diff] [blame] | 45 | #include <limits.h> |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 46 | #include <signal.h> |
Jesse Barnes | f4f76a6 | 2009-01-07 10:18:08 -0800 | [diff] [blame] | 47 | #include <time.h> |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #define stat_t struct stat |
| 51 | #include <sys/ioctl.h> |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 52 | #include <sys/time.h> |
| 53 | #include <stdarg.h> |
Mike Frysinger | 8c8d5dd | 2016-06-21 12:18:15 -0400 | [diff] [blame] | 54 | #ifdef MAJOR_IN_MKDEV |
| 55 | #include <sys/mkdev.h> |
| 56 | #endif |
| 57 | #ifdef MAJOR_IN_SYSMACROS |
| 58 | #include <sys/sysmacros.h> |
Alan Coopersmith | 0e1135d | 2015-03-07 11:44:32 -0800 | [diff] [blame] | 59 | #endif |
Emmanuel Vadot | 3791137 | 2020-01-21 18:49:11 +0100 | [diff] [blame] | 60 | #if HAVE_SYS_SYSCTL_H |
| 61 | #include <sys/sysctl.h> |
| 62 | #endif |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 63 | #include <math.h> |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 64 | |
Emmanuel Vadot | 1c8d2b7 | 2020-01-21 17:42:44 +0100 | [diff] [blame] | 65 | #if defined(__FreeBSD__) |
| 66 | #include <sys/param.h> |
Emmanuel Vadot | e321dd7 | 2020-01-21 18:49:47 +0100 | [diff] [blame] | 67 | #include <sys/pciio.h> |
Emmanuel Vadot | 1c8d2b7 | 2020-01-21 17:42:44 +0100 | [diff] [blame] | 68 | #endif |
| 69 | |
Eric Anholt | 9b28c5a | 2018-11-15 17:48:53 -0800 | [diff] [blame] | 70 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 71 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 72 | /* Not all systems have MAP_FAILED defined */ |
| 73 | #ifndef MAP_FAILED |
| 74 | #define MAP_FAILED ((void *)-1) |
| 75 | #endif |
| 76 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 77 | #include "xf86drm.h" |
Emil Velikov | 42465fe | 2015-04-05 15:51:59 +0100 | [diff] [blame] | 78 | #include "libdrm_macros.h" |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 79 | |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 80 | #include "util_math.h" |
| 81 | |
Emmanuel Vadot | 4fbcc9a | 2020-01-21 17:39:10 +0100 | [diff] [blame] | 82 | #ifdef __DragonFly__ |
Eric Anholt | cfa778a | 2003-02-21 23:23:09 +0000 | [diff] [blame] | 83 | #define DRM_MAJOR 145 |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
Eric Anholt | cfa778a | 2003-02-21 23:23:09 +0000 | [diff] [blame] | 86 | #ifdef __NetBSD__ |
| 87 | #define DRM_MAJOR 34 |
| 88 | #endif |
| 89 | |
Jonathan Gray | 66c3afb | 2015-07-21 03:12:10 +1000 | [diff] [blame] | 90 | #ifdef __OpenBSD__ |
| 91 | #ifdef __i386__ |
| 92 | #define DRM_MAJOR 88 |
| 93 | #else |
| 94 | #define DRM_MAJOR 87 |
| 95 | #endif |
| 96 | #endif /* __OpenBSD__ */ |
Alan Hourihane | b0a9285 | 2003-09-24 14:39:25 +0000 | [diff] [blame] | 97 | |
Eric Anholt | cfa778a | 2003-02-21 23:23:09 +0000 | [diff] [blame] | 98 | #ifndef DRM_MAJOR |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 99 | #define DRM_MAJOR 226 /* Linux */ |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
François Tigeot | 8f2e092 | 2018-12-12 20:48:36 +0100 | [diff] [blame] | 102 | #if defined(__OpenBSD__) || defined(__DragonFly__) |
Jonathan Gray | c0ef1d0 | 2016-12-01 15:18:41 +1100 | [diff] [blame] | 103 | struct drm_pciinfo { |
| 104 | uint16_t domain; |
| 105 | uint8_t bus; |
| 106 | uint8_t dev; |
| 107 | uint8_t func; |
| 108 | uint16_t vendor_id; |
| 109 | uint16_t device_id; |
| 110 | uint16_t subvendor_id; |
| 111 | uint16_t subdevice_id; |
| 112 | uint8_t revision_id; |
| 113 | }; |
| 114 | |
| 115 | #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo) |
| 116 | #endif |
| 117 | |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 118 | #define DRM_MSG_VERBOSITY 3 |
| 119 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 120 | #define memclear(s) memset(&s, 0, sizeof(s)) |
| 121 | |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 122 | static drmServerInfoPtr drm_server_info; |
| 123 | |
Emmanuel Vadot | 6818a50 | 2020-01-21 17:50:15 +0100 | [diff] [blame] | 124 | static bool drmNodeIsDRM(int maj, int min); |
Emmanuel Vadot | 5aa83dd | 2020-04-28 16:33:05 +0200 | [diff] [blame] | 125 | static char *drmGetMinorNameForFD(int fd, int type); |
Emmanuel Vadot | 6818a50 | 2020-01-21 17:50:15 +0100 | [diff] [blame] | 126 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 127 | drm_public void drmSetServerInfo(drmServerInfoPtr info) |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 128 | { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 129 | drm_server_info = info; |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 130 | } |
| 131 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 132 | /** |
| 133 | * Output a message to stderr. |
| 134 | * |
| 135 | * \param format printf() like format string. |
| 136 | * |
| 137 | * \internal |
| 138 | * This function is a wrapper around vfprintf(). |
| 139 | */ |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 140 | |
Thierry Reding | 44b08c0 | 2014-01-22 12:06:51 +0100 | [diff] [blame] | 141 | static int DRM_PRINTFLIKE(1, 0) |
| 142 | drmDebugPrint(const char *format, va_list ap) |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 143 | { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 144 | return vfprintf(stderr, format, ap); |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 145 | } |
| 146 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 147 | drm_public void |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 148 | drmMsg(const char *format, ...) |
| 149 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 150 | va_list ap; |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 151 | const char *env; |
Rob Clark | eb7c2d5 | 2015-09-04 08:08:02 -0400 | [diff] [blame] | 152 | if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || |
| 153 | (drm_server_info && drm_server_info->debug_print)) |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 154 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 155 | va_start(ap, format); |
| 156 | if (drm_server_info) { |
| 157 | drm_server_info->debug_print(format,ap); |
| 158 | } else { |
| 159 | drmDebugPrint(format, ap); |
| 160 | } |
| 161 | va_end(ap); |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 165 | static void *drmHashTable = NULL; /* Context switch callbacks */ |
| 166 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 167 | drm_public void *drmGetHashTable(void) |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 168 | { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 169 | return drmHashTable; |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 170 | } |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 171 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 172 | drm_public void *drmMalloc(int size) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 173 | { |
Emil Velikov | d0e592d | 2015-04-28 13:33:46 +0100 | [diff] [blame] | 174 | return calloc(1, size); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 177 | drm_public void drmFree(void *pt) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 178 | { |
Emil Velikov | d0e592d | 2015-04-28 13:33:46 +0100 | [diff] [blame] | 179 | free(pt); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 182 | /** |
Eric Engestrom | 360292c | 2018-12-19 14:55:45 +0000 | [diff] [blame] | 183 | * Call ioctl, restarting if it is interrupted |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 184 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 185 | drm_public int |
Coleman Kane | 41b83a9 | 2008-08-18 17:08:21 -0400 | [diff] [blame] | 186 | drmIoctl(int fd, unsigned long request, void *arg) |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 187 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 188 | int ret; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 189 | |
| 190 | do { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 191 | ret = ioctl(fd, request, arg); |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 192 | } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); |
| 193 | return ret; |
| 194 | } |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 195 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 196 | static unsigned long drmGetKeyFromFd(int fd) |
| 197 | { |
David Dawes | fcc2106 | 2001-03-30 17:16:20 +0000 | [diff] [blame] | 198 | stat_t st; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 199 | |
| 200 | st.st_rdev = 0; |
| 201 | fstat(fd, &st); |
| 202 | return st.st_rdev; |
| 203 | } |
| 204 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 205 | drm_public drmHashEntry *drmGetEntry(int fd) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 206 | { |
| 207 | unsigned long key = drmGetKeyFromFd(fd); |
| 208 | void *value; |
| 209 | drmHashEntry *entry; |
| 210 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 211 | if (!drmHashTable) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 212 | drmHashTable = drmHashCreate(); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 213 | |
| 214 | if (drmHashLookup(drmHashTable, key, &value)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 215 | entry = drmMalloc(sizeof(*entry)); |
| 216 | entry->fd = fd; |
| 217 | entry->f = NULL; |
| 218 | entry->tagTable = drmHashCreate(); |
| 219 | drmHashInsert(drmHashTable, key, entry); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 220 | } else { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 221 | entry = value; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 222 | } |
| 223 | return entry; |
| 224 | } |
| 225 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 226 | /** |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 227 | * Compare two busid strings |
| 228 | * |
| 229 | * \param first |
| 230 | * \param second |
| 231 | * |
| 232 | * \return 1 if matched. |
| 233 | * |
| 234 | * \internal |
| 235 | * This function compares two bus ID strings. It understands the older |
| 236 | * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is |
| 237 | * domain, b is bus, d is device, f is function. |
| 238 | */ |
Benjamin Herrenschmidt | b04515c | 2010-08-06 13:55:11 +1000 | [diff] [blame] | 239 | 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] | 240 | { |
| 241 | /* First, check if the IDs are exactly the same */ |
| 242 | if (strcasecmp(id1, id2) == 0) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 243 | return 1; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 244 | |
| 245 | /* Try to match old/new-style PCI bus IDs. */ |
| 246 | if (strncasecmp(id1, "pci", 3) == 0) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 247 | unsigned int o1, b1, d1, f1; |
| 248 | unsigned int o2, b2, d2, f2; |
| 249 | int ret; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 250 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 251 | ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1); |
| 252 | if (ret != 4) { |
| 253 | o1 = 0; |
| 254 | ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1); |
| 255 | if (ret != 3) |
| 256 | return 0; |
| 257 | } |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 258 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 259 | ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2); |
| 260 | if (ret != 4) { |
| 261 | o2 = 0; |
| 262 | ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2); |
| 263 | if (ret != 3) |
| 264 | return 0; |
| 265 | } |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 266 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 267 | /* If domains aren't properly supported by the kernel interface, |
| 268 | * just ignore them, which sucks less than picking a totally random |
| 269 | * card with "open by name" |
| 270 | */ |
| 271 | if (!pci_domain_ok) |
| 272 | o1 = o2 = 0; |
Benjamin Herrenschmidt | b04515c | 2010-08-06 13:55:11 +1000 | [diff] [blame] | 273 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 274 | if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2)) |
| 275 | return 0; |
| 276 | else |
| 277 | return 1; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /** |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 283 | * Handles error checking for chown call. |
| 284 | * |
| 285 | * \param path to file. |
| 286 | * \param id of the new owner. |
| 287 | * \param id of the new group. |
| 288 | * |
| 289 | * \return zero if success or -1 if failure. |
| 290 | * |
| 291 | * \internal |
| 292 | * Checks for failure. If failure was caused by signal call chown again. |
Eric Engestrom | 360292c | 2018-12-19 14:55:45 +0000 | [diff] [blame] | 293 | * If any other failure happened then it will output error message using |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 294 | * drmMsg() call. |
| 295 | */ |
Eric Engestrom | 0758520 | 2018-03-16 17:04:50 +0000 | [diff] [blame] | 296 | #if !UDEV |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 297 | static int chown_check_return(const char *path, uid_t owner, gid_t group) |
| 298 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 299 | int rv; |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 300 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 301 | do { |
| 302 | rv = chown(path, owner, group); |
| 303 | } while (rv != 0 && errno == EINTR); |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 304 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 305 | if (rv == 0) |
| 306 | return 0; |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 307 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 308 | drmMsg("Failed to change owner or group for file %s! %d: %s\n", |
| 309 | path, errno, strerror(errno)); |
| 310 | return -1; |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 311 | } |
Jan Vesely | 6fc0e4b | 2015-02-27 12:54:34 -0500 | [diff] [blame] | 312 | #endif |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 313 | |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 314 | static const char *drmGetDeviceName(int type) |
| 315 | { |
| 316 | switch (type) { |
| 317 | case DRM_NODE_PRIMARY: |
| 318 | return DRM_DEV_NAME; |
| 319 | case DRM_NODE_CONTROL: |
| 320 | return DRM_CONTROL_DEV_NAME; |
| 321 | case DRM_NODE_RENDER: |
| 322 | return DRM_RENDER_DEV_NAME; |
| 323 | } |
| 324 | return NULL; |
| 325 | } |
| 326 | |
Pauli Nieminen | c5a5bbb | 2009-07-06 23:37:20 +0300 | [diff] [blame] | 327 | /** |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 328 | * Open the DRM device, creating it if necessary. |
| 329 | * |
| 330 | * \param dev major and minor numbers of the device. |
| 331 | * \param minor minor number of the device. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 332 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 333 | * \return a file descriptor on success, or a negative value on error. |
| 334 | * |
| 335 | * \internal |
| 336 | * Assembles the device name from \p minor and opens it, creating the device |
| 337 | * special file node with the major and minor numbers specified by \p dev and |
| 338 | * parent directory if necessary and was called by root. |
| 339 | */ |
Jan Vesely | de8532d | 2014-11-30 12:53:18 -0500 | [diff] [blame] | 340 | static int drmOpenDevice(dev_t dev, int minor, int type) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 341 | { |
David Dawes | 9c775d0 | 2001-05-14 14:49:58 +0000 | [diff] [blame] | 342 | stat_t st; |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 343 | const char *dev_name = drmGetDeviceName(type); |
Eric Engestrom | 6869e4c | 2018-12-19 13:42:08 +0000 | [diff] [blame] | 344 | char buf[DRM_NODE_NAME_MAX]; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 345 | int fd; |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 346 | mode_t devmode = DRM_DEV_MODE, serv_mode; |
Jan Vesely | 0706c14 | 2015-02-27 12:47:46 -0500 | [diff] [blame] | 347 | gid_t serv_group; |
Eric Engestrom | 0758520 | 2018-03-16 17:04:50 +0000 | [diff] [blame] | 348 | #if !UDEV |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 349 | int isroot = !geteuid(); |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 350 | uid_t user = DRM_DEV_UID; |
Jan Vesely | 0706c14 | 2015-02-27 12:47:46 -0500 | [diff] [blame] | 351 | gid_t group = DRM_DEV_GID; |
| 352 | #endif |
| 353 | |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 354 | if (!dev_name) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 355 | return -EINVAL; |
Frank Binns | 0c5aaee | 2015-01-14 14:07:51 +0000 | [diff] [blame] | 356 | |
| 357 | sprintf(buf, dev_name, DRM_DIR_NAME, minor); |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 358 | drmMsg("drmOpenDevice: node name is %s\n", buf); |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 359 | |
Rob Clark | eb7c2d5 | 2015-09-04 08:08:02 -0400 | [diff] [blame] | 360 | if (drm_server_info && drm_server_info->get_perms) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 361 | drm_server_info->get_perms(&serv_group, &serv_mode); |
| 362 | devmode = serv_mode ? serv_mode : DRM_DEV_MODE; |
| 363 | devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH); |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 364 | } |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 365 | |
Eric Engestrom | 0758520 | 2018-03-16 17:04:50 +0000 | [diff] [blame] | 366 | #if !UDEV |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 367 | if (stat(DRM_DIR_NAME, &st)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 368 | if (!isroot) |
| 369 | return DRM_ERR_NOT_ROOT; |
| 370 | mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE); |
| 371 | chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */ |
| 372 | chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE); |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 373 | } |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 374 | |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 375 | /* Check if the device node exists and create it if necessary. */ |
Eric Anholt | d2f2b42 | 2002-08-08 21:23:46 +0000 | [diff] [blame] | 376 | if (stat(buf, &st)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 377 | if (!isroot) |
| 378 | return DRM_ERR_NOT_ROOT; |
| 379 | remove(buf); |
| 380 | mknod(buf, S_IFCHR | devmode, dev); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 381 | } |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 382 | |
Rob Clark | eb7c2d5 | 2015-09-04 08:08:02 -0400 | [diff] [blame] | 383 | if (drm_server_info && drm_server_info->get_perms) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 384 | group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID; |
| 385 | chown_check_return(buf, user, group); |
| 386 | chmod(buf, devmode); |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 387 | } |
Dave Airlie | 9101a02 | 2008-08-24 16:54:43 +1000 | [diff] [blame] | 388 | #else |
| 389 | /* if we modprobed then wait for udev */ |
| 390 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 391 | int udev_count = 0; |
Dave Airlie | 9101a02 | 2008-08-24 16:54:43 +1000 | [diff] [blame] | 392 | wait_for_udev: |
| 393 | if (stat(DRM_DIR_NAME, &st)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 394 | usleep(20); |
| 395 | udev_count++; |
Dave Airlie | 9101a02 | 2008-08-24 16:54:43 +1000 | [diff] [blame] | 396 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 397 | if (udev_count == 50) |
| 398 | return -1; |
| 399 | goto wait_for_udev; |
| 400 | } |
Dave Airlie | 9101a02 | 2008-08-24 16:54:43 +1000 | [diff] [blame] | 401 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 402 | if (stat(buf, &st)) { |
| 403 | usleep(20); |
| 404 | udev_count++; |
Dave Airlie | 9101a02 | 2008-08-24 16:54:43 +1000 | [diff] [blame] | 405 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 406 | if (udev_count == 50) |
| 407 | return -1; |
| 408 | goto wait_for_udev; |
| 409 | } |
Dave Airlie | 9101a02 | 2008-08-24 16:54:43 +1000 | [diff] [blame] | 410 | } |
| 411 | #endif |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 412 | |
Michel Dänzer | 3561569 | 2018-05-18 11:16:51 +0200 | [diff] [blame] | 413 | fd = open(buf, O_RDWR | O_CLOEXEC, 0); |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 414 | drmMsg("drmOpenDevice: open result is %d, (%s)\n", |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 415 | fd, fd < 0 ? strerror(errno) : "OK"); |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 416 | if (fd >= 0) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 417 | return fd; |
Eric Anholt | d2f2b42 | 2002-08-08 21:23:46 +0000 | [diff] [blame] | 418 | |
Eric Engestrom | 0758520 | 2018-03-16 17:04:50 +0000 | [diff] [blame] | 419 | #if !UDEV |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 420 | /* Check if the device node is not what we expect it to be, and recreate it |
| 421 | * and try again if so. |
| 422 | */ |
Eric Anholt | d2f2b42 | 2002-08-08 21:23:46 +0000 | [diff] [blame] | 423 | if (st.st_rdev != dev) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 424 | if (!isroot) |
| 425 | return DRM_ERR_NOT_ROOT; |
| 426 | remove(buf); |
| 427 | mknod(buf, S_IFCHR | devmode, dev); |
| 428 | if (drm_server_info && drm_server_info->get_perms) { |
| 429 | chown_check_return(buf, user, group); |
| 430 | chmod(buf, devmode); |
| 431 | } |
Eric Anholt | d2f2b42 | 2002-08-08 21:23:46 +0000 | [diff] [blame] | 432 | } |
Michel Dänzer | 3561569 | 2018-05-18 11:16:51 +0200 | [diff] [blame] | 433 | fd = open(buf, O_RDWR | O_CLOEXEC, 0); |
Eric Anholt | d2f2b42 | 2002-08-08 21:23:46 +0000 | [diff] [blame] | 434 | drmMsg("drmOpenDevice: open result is %d, (%s)\n", |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 435 | fd, fd < 0 ? strerror(errno) : "OK"); |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 436 | if (fd >= 0) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 437 | return fd; |
Eric Anholt | d2f2b42 | 2002-08-08 21:23:46 +0000 | [diff] [blame] | 438 | |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 439 | drmMsg("drmOpenDevice: Open failed\n"); |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 440 | remove(buf); |
Dave Airlie | 39e5e98 | 2010-12-07 14:26:09 +1000 | [diff] [blame] | 441 | #endif |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 442 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 445 | |
| 446 | /** |
| 447 | * Open the DRM device |
| 448 | * |
| 449 | * \param minor device minor number. |
| 450 | * \param create allow to create the device if set. |
| 451 | * |
| 452 | * \return a file descriptor on success, or a negative value on error. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 453 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 454 | * \internal |
| 455 | * Calls drmOpenDevice() if \p create is set, otherwise assembles the device |
| 456 | * name from \p minor and opens it. |
| 457 | */ |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 458 | static int drmOpenMinor(int minor, int create, int type) |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 459 | { |
| 460 | int fd; |
Eric Engestrom | 6869e4c | 2018-12-19 13:42:08 +0000 | [diff] [blame] | 461 | char buf[DRM_NODE_NAME_MAX]; |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 462 | const char *dev_name = drmGetDeviceName(type); |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 463 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 464 | if (create) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 465 | return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type); |
| 466 | |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 467 | if (!dev_name) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 468 | return -EINVAL; |
Frank Binns | 0c5aaee | 2015-01-14 14:07:51 +0000 | [diff] [blame] | 469 | |
| 470 | sprintf(buf, dev_name, DRM_DIR_NAME, minor); |
Michel Dänzer | 3561569 | 2018-05-18 11:16:51 +0200 | [diff] [blame] | 471 | if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 472 | return fd; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 473 | return -errno; |
| 474 | } |
| 475 | |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 476 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 477 | /** |
| 478 | * Determine whether the DRM kernel driver has been loaded. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 479 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 480 | * \return 1 if the DRM driver is loaded, 0 otherwise. |
| 481 | * |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 482 | * \internal |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 483 | * Determine the presence of the kernel driver by attempting to open the 0 |
| 484 | * minor and get version information. For backward compatibility with older |
| 485 | * Linux implementations, /proc/dri is also checked. |
| 486 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 487 | drm_public int drmAvailable(void) |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 488 | { |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 489 | drmVersionPtr version; |
| 490 | int retval = 0; |
| 491 | int fd; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 492 | |
Frank Binns | ad8bbfd | 2015-01-14 14:07:50 +0000 | [diff] [blame] | 493 | if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) { |
Alan Hourihane | b0a9285 | 2003-09-24 14:39:25 +0000 | [diff] [blame] | 494 | #ifdef __linux__ |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 495 | /* Try proc for backward Linux compatibility */ |
| 496 | if (!access("/proc/dri/0", R_OK)) |
| 497 | return 1; |
Alan Hourihane | b0a9285 | 2003-09-24 14:39:25 +0000 | [diff] [blame] | 498 | #endif |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 499 | return 0; |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 500 | } |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 501 | |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 502 | if ((version = drmGetVersion(fd))) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 503 | retval = 1; |
| 504 | drmFreeVersion(version); |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 505 | } |
| 506 | close(fd); |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 507 | |
| 508 | return retval; |
| 509 | } |
| 510 | |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 511 | static int drmGetMinorBase(int type) |
| 512 | { |
| 513 | switch (type) { |
| 514 | case DRM_NODE_PRIMARY: |
| 515 | return 0; |
| 516 | case DRM_NODE_CONTROL: |
| 517 | return 64; |
| 518 | case DRM_NODE_RENDER: |
| 519 | return 128; |
| 520 | default: |
| 521 | return -1; |
| 522 | }; |
| 523 | } |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 524 | |
Emmanuel Vadot | c55a1e5 | 2020-01-21 17:47:09 +0100 | [diff] [blame] | 525 | static int drmGetMinorType(int major, int minor) |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 526 | { |
Emmanuel Vadot | c55a1e5 | 2020-01-21 17:47:09 +0100 | [diff] [blame] | 527 | #ifdef __FreeBSD__ |
| 528 | char name[SPECNAMELEN]; |
| 529 | int id; |
| 530 | |
| 531 | if (!devname_r(makedev(major, minor), S_IFCHR, name, sizeof(name))) |
| 532 | return -1; |
| 533 | |
| 534 | if (sscanf(name, "drm/%d", &id) != 1) { |
| 535 | // If not in /dev/drm/ we have the type in the name |
| 536 | if (sscanf(name, "dri/card%d\n", &id) >= 1) |
| 537 | return DRM_NODE_PRIMARY; |
| 538 | else if (sscanf(name, "dri/control%d\n", &id) >= 1) |
| 539 | return DRM_NODE_CONTROL; |
| 540 | else if (sscanf(name, "dri/renderD%d\n", &id) >= 1) |
| 541 | return DRM_NODE_RENDER; |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | minor = id; |
| 546 | #endif |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 547 | int type = minor >> 6; |
| 548 | |
| 549 | if (minor < 0) |
| 550 | return -1; |
| 551 | |
| 552 | switch (type) { |
| 553 | case DRM_NODE_PRIMARY: |
| 554 | case DRM_NODE_CONTROL: |
| 555 | case DRM_NODE_RENDER: |
| 556 | return type; |
| 557 | default: |
| 558 | return -1; |
| 559 | } |
| 560 | } |
| 561 | |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 562 | static const char *drmGetMinorName(int type) |
| 563 | { |
| 564 | switch (type) { |
| 565 | case DRM_NODE_PRIMARY: |
Jonathan Gray | fc08332 | 2015-07-21 03:12:56 +1000 | [diff] [blame] | 566 | return DRM_PRIMARY_MINOR_NAME; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 567 | case DRM_NODE_CONTROL: |
Jonathan Gray | fc08332 | 2015-07-21 03:12:56 +1000 | [diff] [blame] | 568 | return DRM_CONTROL_MINOR_NAME; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 569 | case DRM_NODE_RENDER: |
Jonathan Gray | fc08332 | 2015-07-21 03:12:56 +1000 | [diff] [blame] | 570 | return DRM_RENDER_MINOR_NAME; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 571 | default: |
| 572 | return NULL; |
| 573 | } |
| 574 | } |
| 575 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 576 | /** |
| 577 | * Open the device by bus ID. |
| 578 | * |
| 579 | * \param busid bus ID. |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 580 | * \param type device node type. |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 581 | * |
| 582 | * \return a file descriptor on success, or a negative value on error. |
| 583 | * |
| 584 | * \internal |
| 585 | * This function attempts to open every possible minor (up to DRM_MAX_MINOR), |
| 586 | * comparing the device bus ID with the one supplied. |
| 587 | * |
| 588 | * \sa drmOpenMinor() and drmGetBusid(). |
| 589 | */ |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 590 | static int drmOpenByBusid(const char *busid, int type) |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 591 | { |
Benjamin Herrenschmidt | b04515c | 2010-08-06 13:55:11 +1000 | [diff] [blame] | 592 | int i, pci_domain_ok = 1; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 593 | int fd; |
| 594 | const char *buf; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 595 | drmSetVersion sv; |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 596 | int base = drmGetMinorBase(type); |
| 597 | |
| 598 | if (base < 0) |
| 599 | return -1; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 600 | |
| 601 | drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid); |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 602 | for (i = base; i < base + DRM_MAX_MINOR; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 603 | fd = drmOpenMinor(i, 1, type); |
| 604 | drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd); |
| 605 | if (fd >= 0) { |
| 606 | /* We need to try for 1.4 first for proper PCI domain support |
| 607 | * and if that fails, we know the kernel is busted |
| 608 | */ |
| 609 | sv.drm_di_major = 1; |
| 610 | sv.drm_di_minor = 4; |
| 611 | sv.drm_dd_major = -1; /* Don't care */ |
| 612 | sv.drm_dd_minor = -1; /* Don't care */ |
| 613 | if (drmSetInterfaceVersion(fd, &sv)) { |
Benjamin Herrenschmidt | b04515c | 2010-08-06 13:55:11 +1000 | [diff] [blame] | 614 | #ifndef __alpha__ |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 615 | pci_domain_ok = 0; |
Benjamin Herrenschmidt | b04515c | 2010-08-06 13:55:11 +1000 | [diff] [blame] | 616 | #endif |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 617 | sv.drm_di_major = 1; |
| 618 | sv.drm_di_minor = 1; |
| 619 | sv.drm_dd_major = -1; /* Don't care */ |
| 620 | sv.drm_dd_minor = -1; /* Don't care */ |
| 621 | drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n"); |
| 622 | drmSetInterfaceVersion(fd, &sv); |
| 623 | } |
| 624 | buf = drmGetBusid(fd); |
| 625 | drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf); |
| 626 | if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) { |
| 627 | drmFreeBusid(buf); |
| 628 | return fd; |
| 629 | } |
| 630 | if (buf) |
| 631 | drmFreeBusid(buf); |
| 632 | close(fd); |
| 633 | } |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 634 | } |
| 635 | return -1; |
| 636 | } |
| 637 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 638 | |
| 639 | /** |
| 640 | * Open the device by name. |
| 641 | * |
| 642 | * \param name driver name. |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 643 | * \param type the device node type. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 644 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 645 | * \return a file descriptor on success, or a negative value on error. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 646 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 647 | * \internal |
| 648 | * This function opens the first minor number that matches the driver name and |
| 649 | * isn't already in use. If it's in use it then it will already have a bus ID |
| 650 | * assigned. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 651 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 652 | * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid(). |
| 653 | */ |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 654 | static int drmOpenByName(const char *name, int type) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 655 | { |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 656 | int i; |
| 657 | int fd; |
| 658 | drmVersionPtr version; |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 659 | char * id; |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 660 | int base = drmGetMinorBase(type); |
| 661 | |
| 662 | if (base < 0) |
| 663 | return -1; |
Dave Airlie | db85ed2 | 2008-02-13 12:20:02 +1000 | [diff] [blame] | 664 | |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 665 | /* |
| 666 | * Open the first minor number that matches the driver name and isn't |
| 667 | * already in use. If it's in use it will have a busid assigned already. |
| 668 | */ |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 669 | for (i = base; i < base + DRM_MAX_MINOR; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 670 | if ((fd = drmOpenMinor(i, 1, type)) >= 0) { |
| 671 | if ((version = drmGetVersion(fd))) { |
| 672 | if (!strcmp(version->name, name)) { |
| 673 | drmFreeVersion(version); |
| 674 | id = drmGetBusid(fd); |
| 675 | drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL"); |
| 676 | if (!id || !*id) { |
| 677 | if (id) |
| 678 | drmFreeBusid(id); |
| 679 | return fd; |
| 680 | } else { |
| 681 | drmFreeBusid(id); |
| 682 | } |
| 683 | } else { |
| 684 | drmFreeVersion(version); |
| 685 | } |
| 686 | } |
| 687 | close(fd); |
| 688 | } |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | #ifdef __linux__ |
Adam Jackson | 22e41ef | 2006-02-20 23:09:00 +0000 | [diff] [blame] | 692 | /* Backward-compatibility /proc support */ |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 693 | for (i = 0; i < 8; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 694 | char proc_name[64], buf[512]; |
| 695 | char *driver, *pt, *devstring; |
| 696 | int retcode; |
| 697 | |
| 698 | sprintf(proc_name, "/proc/dri/%d/name", i); |
Eric Engestrom | 5ab6031 | 2020-05-28 17:17:26 +0200 | [diff] [blame^] | 699 | if ((fd = open(proc_name, O_RDONLY, 0)) >= 0) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 700 | retcode = read(fd, buf, sizeof(buf)-1); |
| 701 | close(fd); |
| 702 | if (retcode) { |
| 703 | buf[retcode-1] = '\0'; |
| 704 | for (driver = pt = buf; *pt && *pt != ' '; ++pt) |
| 705 | ; |
| 706 | if (*pt) { /* Device is next */ |
| 707 | *pt = '\0'; |
| 708 | if (!strcmp(driver, name)) { /* Match */ |
| 709 | for (devstring = ++pt; *pt && *pt != ' '; ++pt) |
| 710 | ; |
| 711 | if (*pt) { /* Found busid */ |
| 712 | return drmOpenByBusid(++pt, type); |
| 713 | } else { /* No busid */ |
| 714 | return drmOpenDevice(strtol(devstring, NULL, 0),i, type); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 720 | } |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 721 | #endif |
| 722 | |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 723 | return -1; |
| 724 | } |
| 725 | |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 726 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 727 | /** |
| 728 | * Open the DRM device. |
| 729 | * |
| 730 | * Looks up the specified name and bus ID, and opens the device found. The |
| 731 | * entry in /dev/dri is created if necessary and if called by root. |
| 732 | * |
| 733 | * \param name driver name. Not referenced if bus ID is supplied. |
| 734 | * \param busid bus ID. Zero if not known. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 735 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 736 | * \return a file descriptor on success, or a negative value on error. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 737 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 738 | * \internal |
| 739 | * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName() |
| 740 | * otherwise. |
| 741 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 742 | drm_public int drmOpen(const char *name, const char *busid) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 743 | { |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 744 | return drmOpenWithType(name, busid, DRM_NODE_PRIMARY); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Open the DRM device with specified type. |
| 749 | * |
| 750 | * Looks up the specified name and bus ID, and opens the device found. The |
| 751 | * entry in /dev/dri is created if necessary and if called by root. |
| 752 | * |
| 753 | * \param name driver name. Not referenced if bus ID is supplied. |
| 754 | * \param busid bus ID. Zero if not known. |
| 755 | * \param type the device node type to open, PRIMARY, CONTROL or RENDER |
| 756 | * |
| 757 | * \return a file descriptor on success, or a negative value on error. |
| 758 | * |
| 759 | * \internal |
| 760 | * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName() |
| 761 | * otherwise. |
| 762 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 763 | drm_public int drmOpenWithType(const char *name, const char *busid, int type) |
Jammy Zhou | f1adc4b | 2015-02-11 12:40:51 +0800 | [diff] [blame] | 764 | { |
Prabhanjan Kandula | 225d73f | 2019-04-24 10:08:39 -0700 | [diff] [blame] | 765 | if (name != NULL && drm_server_info && |
| 766 | drm_server_info->load_module && !drmAvailable()) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 767 | /* try to load the kernel module */ |
| 768 | if (!drm_server_info->load_module(name)) { |
| 769 | drmMsg("[drm] failed to load kernel module \"%s\"\n", name); |
| 770 | return -1; |
| 771 | } |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 772 | } |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 773 | |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 774 | if (busid) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 775 | int fd = drmOpenByBusid(busid, type); |
| 776 | if (fd >= 0) |
| 777 | return fd; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 778 | } |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 779 | |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 780 | if (name) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 781 | return drmOpenByName(name, type); |
Adam Jackson | 22e41ef | 2006-02-20 23:09:00 +0000 | [diff] [blame] | 782 | |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 783 | return -1; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 786 | drm_public int drmOpenControl(int minor) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 787 | { |
| 788 | return drmOpenMinor(minor, 0, DRM_NODE_CONTROL); |
| 789 | } |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 790 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 791 | drm_public int drmOpenRender(int minor) |
Frank Binns | 0c5aaee | 2015-01-14 14:07:51 +0000 | [diff] [blame] | 792 | { |
| 793 | return drmOpenMinor(minor, 0, DRM_NODE_RENDER); |
| 794 | } |
| 795 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 796 | /** |
| 797 | * Free the version information returned by drmGetVersion(). |
| 798 | * |
| 799 | * \param v pointer to the version information. |
| 800 | * |
| 801 | * \internal |
| 802 | * It frees the memory pointed by \p %v as well as all the non-null strings |
| 803 | * pointers in it. |
| 804 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 805 | drm_public void drmFreeVersion(drmVersionPtr v) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 806 | { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 807 | if (!v) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 808 | return; |
Jakob Bornecrantz | 9d8ba2d | 2007-02-25 10:48:26 +1100 | [diff] [blame] | 809 | drmFree(v->name); |
| 810 | drmFree(v->date); |
| 811 | drmFree(v->desc); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 812 | drmFree(v); |
| 813 | } |
| 814 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 815 | |
| 816 | /** |
| 817 | * Free the non-public version information returned by the kernel. |
| 818 | * |
| 819 | * \param v pointer to the version information. |
| 820 | * |
| 821 | * \internal |
| 822 | * Used by drmGetVersion() to free the memory pointed by \p %v as well as all |
| 823 | * the non-null strings pointers in it. |
| 824 | */ |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 825 | static void drmFreeKernelVersion(drm_version_t *v) |
| 826 | { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 827 | if (!v) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 828 | return; |
Jakob Bornecrantz | 9d8ba2d | 2007-02-25 10:48:26 +1100 | [diff] [blame] | 829 | drmFree(v->name); |
| 830 | drmFree(v->date); |
| 831 | drmFree(v->desc); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 832 | drmFree(v); |
| 833 | } |
| 834 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 835 | |
| 836 | /** |
| 837 | * Copy version information. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 838 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 839 | * \param d destination pointer. |
| 840 | * \param s source pointer. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 841 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 842 | * \internal |
| 843 | * Used by drmGetVersion() to translate the information returned by the ioctl |
| 844 | * interface in a private structure into the public structure counterpart. |
| 845 | */ |
Brian Paul | 569da5a | 2000-06-08 14:38:22 +0000 | [diff] [blame] | 846 | static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 847 | { |
| 848 | d->version_major = s->version_major; |
| 849 | d->version_minor = s->version_minor; |
| 850 | d->version_patchlevel = s->version_patchlevel; |
| 851 | d->name_len = s->name_len; |
Adam Jackson | 0a1ff35 | 2010-10-27 18:44:53 -0400 | [diff] [blame] | 852 | d->name = strdup(s->name); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 853 | d->date_len = s->date_len; |
Adam Jackson | 0a1ff35 | 2010-10-27 18:44:53 -0400 | [diff] [blame] | 854 | d->date = strdup(s->date); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 855 | d->desc_len = s->desc_len; |
Adam Jackson | 0a1ff35 | 2010-10-27 18:44:53 -0400 | [diff] [blame] | 856 | d->desc = strdup(s->desc); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 859 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 860 | /** |
| 861 | * Query the driver version information. |
| 862 | * |
| 863 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 864 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 865 | * \return pointer to a drmVersion structure which should be freed with |
| 866 | * drmFreeVersion(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 867 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 868 | * \note Similar information is available via /proc/dri. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 869 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 870 | * \internal |
| 871 | * It gets the version information via successive DRM_IOCTL_VERSION ioctls, |
| 872 | * first with zeros to get the string lengths, and then the actually strings. |
| 873 | * It also null-terminates them since they might not be already. |
| 874 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 875 | drm_public drmVersionPtr drmGetVersion(int fd) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 876 | { |
| 877 | drmVersionPtr retval; |
| 878 | drm_version_t *version = drmMalloc(sizeof(*version)); |
| 879 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 880 | if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 881 | drmFreeKernelVersion(version); |
| 882 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 885 | if (version->name_len) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 886 | version->name = drmMalloc(version->name_len + 1); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 887 | if (version->date_len) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 888 | version->date = drmMalloc(version->date_len + 1); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 889 | if (version->desc_len) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 890 | version->desc = drmMalloc(version->desc_len + 1); |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 891 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 892 | if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 893 | drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno)); |
| 894 | drmFreeKernelVersion(version); |
| 895 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Adam Jackson | 22e41ef | 2006-02-20 23:09:00 +0000 | [diff] [blame] | 898 | /* The results might not be null-terminated strings, so terminate them. */ |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 899 | if (version->name_len) version->name[version->name_len] = '\0'; |
| 900 | if (version->date_len) version->date[version->date_len] = '\0'; |
| 901 | if (version->desc_len) version->desc[version->desc_len] = '\0'; |
| 902 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 903 | retval = drmMalloc(sizeof(*retval)); |
| 904 | drmCopyVersion(retval, version); |
| 905 | drmFreeKernelVersion(version); |
| 906 | return retval; |
| 907 | } |
| 908 | |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 909 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 910 | /** |
| 911 | * Get version information for the DRM user space library. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 912 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 913 | * This version number is driver independent. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 914 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 915 | * \param fd file descriptor. |
| 916 | * |
| 917 | * \return version information. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 918 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 919 | * \internal |
| 920 | * This function allocates and fills a drm_version structure with a hard coded |
| 921 | * version number. |
| 922 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 923 | drm_public drmVersionPtr drmGetLibVersion(int fd) |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 924 | { |
| 925 | drm_version_t *version = drmMalloc(sizeof(*version)); |
| 926 | |
| 927 | /* Version history: |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 928 | * 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] | 929 | * revision 1.0.x = original DRM interface with no drmGetLibVersion |
| 930 | * entry point and many drm<Device> extensions |
| 931 | * revision 1.1.x = added drmCommand entry points for device extensions |
| 932 | * added drmGetLibVersion to identify libdrm.a version |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 933 | * revision 1.2.x = added drmSetInterfaceVersion |
| 934 | * modified drmOpen to handle both busid and name |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 935 | * revision 1.3.x = added server + memory manager |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 936 | */ |
Dave Airlie | 7903875 | 2006-11-08 15:08:09 +1100 | [diff] [blame] | 937 | version->version_major = 1; |
| 938 | version->version_minor = 3; |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 939 | version->version_patchlevel = 0; |
| 940 | |
| 941 | return (drmVersionPtr)version; |
| 942 | } |
| 943 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 944 | drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value) |
Ben Skeggs | 5c6c691 | 2011-02-21 11:27:19 +1000 | [diff] [blame] | 945 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 946 | struct drm_get_cap cap; |
| 947 | int ret; |
Ben Skeggs | 5c6c691 | 2011-02-21 11:27:19 +1000 | [diff] [blame] | 948 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 949 | memclear(cap); |
| 950 | cap.capability = capability; |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 951 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 952 | ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap); |
| 953 | if (ret) |
| 954 | return ret; |
Ben Skeggs | 5c6c691 | 2011-02-21 11:27:19 +1000 | [diff] [blame] | 955 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 956 | *value = cap.value; |
| 957 | return 0; |
Ben Skeggs | 5c6c691 | 2011-02-21 11:27:19 +1000 | [diff] [blame] | 958 | } |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 959 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 960 | drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value) |
Damien Lespiau | ddbbdb1 | 2013-09-03 15:34:41 +0100 | [diff] [blame] | 961 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 962 | struct drm_set_client_cap cap; |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 963 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 964 | memclear(cap); |
| 965 | cap.capability = capability; |
| 966 | cap.value = value; |
Damien Lespiau | ddbbdb1 | 2013-09-03 15:34:41 +0100 | [diff] [blame] | 967 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 968 | return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap); |
Damien Lespiau | ddbbdb1 | 2013-09-03 15:34:41 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 971 | /** |
| 972 | * Free the bus ID information. |
| 973 | * |
| 974 | * \param busid bus ID information string as given by drmGetBusid(). |
| 975 | * |
| 976 | * \internal |
| 977 | * This function is just frees the memory pointed by \p busid. |
| 978 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 979 | drm_public void drmFreeBusid(const char *busid) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 980 | { |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 981 | drmFree((void *)busid); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 984 | |
| 985 | /** |
| 986 | * Get the bus ID of the device. |
| 987 | * |
| 988 | * \param fd file descriptor. |
| 989 | * |
| 990 | * \return bus ID string. |
| 991 | * |
| 992 | * \internal |
| 993 | * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to |
| 994 | * get the string length and data, passing the arguments in a drm_unique |
| 995 | * structure. |
| 996 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 997 | drm_public char *drmGetBusid(int fd) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 998 | { |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 999 | drm_unique_t u; |
| 1000 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1001 | memclear(u); |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 1002 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1003 | if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1004 | return NULL; |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 1005 | u.unique = drmMalloc(u.unique_len + 1); |
Seung-Woo Kim | 7b806e8 | 2017-03-27 11:09:29 +0900 | [diff] [blame] | 1006 | if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) { |
| 1007 | drmFree(u.unique); |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1008 | return NULL; |
Seung-Woo Kim | 7b806e8 | 2017-03-27 11:09:29 +0900 | [diff] [blame] | 1009 | } |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 1010 | u.unique[u.unique_len] = '\0'; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 1011 | |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 1012 | return u.unique; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1015 | |
| 1016 | /** |
| 1017 | * Set the bus ID of the device. |
| 1018 | * |
| 1019 | * \param fd file descriptor. |
| 1020 | * \param busid bus ID string. |
| 1021 | * |
| 1022 | * \return zero on success, negative on failure. |
| 1023 | * |
| 1024 | * \internal |
| 1025 | * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing |
| 1026 | * the arguments in a drm_unique structure. |
| 1027 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1028 | drm_public int drmSetBusid(int fd, const char *busid) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1029 | { |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 1030 | drm_unique_t u; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1031 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1032 | memclear(u); |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 1033 | u.unique = (char *)busid; |
| 1034 | u.unique_len = strlen(busid); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1035 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1036 | if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1037 | return -errno; |
David Dawes | 56bd9c2 | 2001-07-30 19:59:39 +0000 | [diff] [blame] | 1038 | } |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1039 | return 0; |
| 1040 | } |
| 1041 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1042 | drm_public int drmGetMagic(int fd, drm_magic_t * magic) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1043 | { |
| 1044 | drm_auth_t auth; |
| 1045 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1046 | memclear(auth); |
| 1047 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1048 | *magic = 0; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1049 | if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1050 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1051 | *magic = auth.magic; |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1055 | drm_public int drmAuthMagic(int fd, drm_magic_t magic) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1056 | { |
| 1057 | drm_auth_t auth; |
| 1058 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1059 | memclear(auth); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1060 | auth.magic = magic; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1061 | if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1062 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1063 | return 0; |
| 1064 | } |
| 1065 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1066 | /** |
| 1067 | * Specifies a range of memory that is available for mapping by a |
| 1068 | * non-root process. |
| 1069 | * |
| 1070 | * \param fd file descriptor. |
| 1071 | * \param offset usually the physical address. The actual meaning depends of |
| 1072 | * the \p type parameter. See below. |
| 1073 | * \param size of the memory in bytes. |
| 1074 | * \param type type of the memory to be mapped. |
| 1075 | * \param flags combination of several flags to modify the function actions. |
| 1076 | * \param handle will be set to a value that may be used as the offset |
| 1077 | * parameter for mmap(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1078 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1079 | * \return zero on success or a negative value on error. |
| 1080 | * |
| 1081 | * \par Mapping the frame buffer |
| 1082 | * For the frame buffer |
| 1083 | * - \p offset will be the physical address of the start of the frame buffer, |
| 1084 | * - \p size will be the size of the frame buffer in bytes, and |
| 1085 | * - \p type will be DRM_FRAME_BUFFER. |
| 1086 | * |
| 1087 | * \par |
| 1088 | * The area mapped will be uncached. If MTRR support is available in the |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1089 | * kernel, the frame buffer area will be set to write combining. |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1090 | * |
| 1091 | * \par Mapping the MMIO register area |
| 1092 | * For the MMIO register area, |
| 1093 | * - \p offset will be the physical address of the start of the register area, |
| 1094 | * - \p size will be the size of the register area bytes, and |
| 1095 | * - \p type will be DRM_REGISTERS. |
| 1096 | * \par |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1097 | * The area mapped will be uncached. |
| 1098 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1099 | * \par Mapping the SAREA |
| 1100 | * For the SAREA, |
| 1101 | * - \p offset will be ignored and should be set to zero, |
| 1102 | * - \p size will be the desired size of the SAREA in bytes, |
| 1103 | * - \p type will be DRM_SHM. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1104 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1105 | * \par |
| 1106 | * A shared memory area of the requested size will be created and locked in |
| 1107 | * kernel memory. This area may be mapped into client-space by using the handle |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1108 | * returned. |
| 1109 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1110 | * \note May only be called by root. |
| 1111 | * |
| 1112 | * \internal |
| 1113 | * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing |
| 1114 | * the arguments in a drm_map structure. |
| 1115 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1116 | drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type, |
| 1117 | drmMapFlags flags, drm_handle_t *handle) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1118 | { |
| 1119 | drm_map_t map; |
| 1120 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1121 | memclear(map); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1122 | map.offset = offset; |
| 1123 | map.size = size; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1124 | map.type = type; |
| 1125 | map.flags = flags; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1126 | if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1127 | return -errno; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1128 | if (handle) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1129 | *handle = (drm_handle_t)(uintptr_t)map.handle; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1130 | return 0; |
| 1131 | } |
| 1132 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1133 | drm_public int drmRmMap(int fd, drm_handle_t handle) |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 1134 | { |
| 1135 | drm_map_t map; |
| 1136 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1137 | memclear(map); |
Jeremy Huddleston | 961bf9b | 2011-11-01 14:42:13 -0700 | [diff] [blame] | 1138 | map.handle = (void *)(uintptr_t)handle; |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 1139 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1140 | if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1141 | return -errno; |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 1142 | return 0; |
| 1143 | } |
| 1144 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1145 | /** |
| 1146 | * Make buffers available for DMA transfers. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1147 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1148 | * \param fd file descriptor. |
| 1149 | * \param count number of buffers. |
| 1150 | * \param size size of each buffer. |
| 1151 | * \param flags buffer allocation flags. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1152 | * \param agp_offset offset in the AGP aperture |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1153 | * |
| 1154 | * \return number of buffers allocated, negative on error. |
| 1155 | * |
| 1156 | * \internal |
| 1157 | * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl. |
| 1158 | * |
| 1159 | * \sa drm_buf_desc. |
| 1160 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1161 | drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags, |
| 1162 | int agp_offset) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1163 | { |
| 1164 | drm_buf_desc_t request; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1165 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1166 | memclear(request); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1167 | request.count = count; |
| 1168 | request.size = size; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1169 | request.flags = flags; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1170 | request.agp_start = agp_offset; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1171 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1172 | if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1173 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1174 | return request.count; |
| 1175 | } |
| 1176 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1177 | drm_public int drmMarkBufs(int fd, double low, double high) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1178 | { |
| 1179 | drm_buf_info_t info; |
| 1180 | int i; |
| 1181 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1182 | memclear(info); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1183 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1184 | if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1185 | return -EINVAL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1186 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1187 | if (!info.count) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1188 | return -EINVAL; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1189 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1190 | if (!(info.list = drmMalloc(info.count * sizeof(*info.list)))) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1191 | return -ENOMEM; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1192 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1193 | if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1194 | int retval = -errno; |
| 1195 | drmFree(info.list); |
| 1196 | return retval; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1197 | } |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1198 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1199 | for (i = 0; i < info.count; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1200 | info.list[i].low_mark = low * info.list[i].count; |
| 1201 | info.list[i].high_mark = high * info.list[i].count; |
| 1202 | if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) { |
| 1203 | int retval = -errno; |
| 1204 | drmFree(info.list); |
| 1205 | return retval; |
| 1206 | } |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1207 | } |
| 1208 | drmFree(info.list); |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1209 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1213 | /** |
| 1214 | * Free buffers. |
| 1215 | * |
| 1216 | * \param fd file descriptor. |
| 1217 | * \param count number of buffers to free. |
| 1218 | * \param list list of buffers to be freed. |
| 1219 | * |
| 1220 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1221 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1222 | * \note This function is primarily used for debugging. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1223 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1224 | * \internal |
| 1225 | * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing |
| 1226 | * the arguments in a drm_buf_free structure. |
| 1227 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1228 | drm_public int drmFreeBufs(int fd, int count, int *list) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1229 | { |
| 1230 | drm_buf_free_t request; |
| 1231 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1232 | memclear(request); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1233 | request.count = count; |
| 1234 | request.list = list; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1235 | if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1236 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1237 | return 0; |
| 1238 | } |
| 1239 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1240 | |
| 1241 | /** |
| 1242 | * Close the device. |
| 1243 | * |
| 1244 | * \param fd file descriptor. |
| 1245 | * |
| 1246 | * \internal |
| 1247 | * This function closes the file descriptor. |
| 1248 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1249 | drm_public int drmClose(int fd) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1250 | { |
| 1251 | unsigned long key = drmGetKeyFromFd(fd); |
| 1252 | drmHashEntry *entry = drmGetEntry(fd); |
| 1253 | |
| 1254 | drmHashDestroy(entry->tagTable); |
| 1255 | entry->fd = 0; |
| 1256 | entry->f = NULL; |
| 1257 | entry->tagTable = NULL; |
| 1258 | |
| 1259 | drmHashDelete(drmHashTable, key); |
| 1260 | drmFree(entry); |
| 1261 | |
| 1262 | return close(fd); |
| 1263 | } |
| 1264 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1265 | |
| 1266 | /** |
| 1267 | * Map a region of memory. |
| 1268 | * |
| 1269 | * \param fd file descriptor. |
| 1270 | * \param handle handle returned by drmAddMap(). |
| 1271 | * \param size size in bytes. Must match the size used by drmAddMap(). |
| 1272 | * \param address will contain the user-space virtual address where the mapping |
| 1273 | * begins. |
| 1274 | * |
| 1275 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1276 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1277 | * \internal |
| 1278 | * This function is a wrapper for mmap(). |
| 1279 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1280 | drm_public int drmMap(int fd, drm_handle_t handle, drmSize size, |
| 1281 | drmAddressPtr address) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1282 | { |
Alan Hourihane | c7558d8 | 2000-09-24 09:34:10 +0000 | [diff] [blame] | 1283 | static unsigned long pagesize_mask = 0; |
| 1284 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1285 | if (fd < 0) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1286 | return -EINVAL; |
Alan Hourihane | c7558d8 | 2000-09-24 09:34:10 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (!pagesize_mask) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1289 | pagesize_mask = getpagesize() - 1; |
Alan Hourihane | c7558d8 | 2000-09-24 09:34:10 +0000 | [diff] [blame] | 1290 | |
| 1291 | size = (size + pagesize_mask) & ~pagesize_mask; |
| 1292 | |
Emil Velikov | faf51d5 | 2014-09-07 20:03:05 +0100 | [diff] [blame] | 1293 | *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle); |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1294 | if (*address == MAP_FAILED) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1295 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1296 | return 0; |
| 1297 | } |
| 1298 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1299 | |
| 1300 | /** |
| 1301 | * Unmap mappings obtained with drmMap(). |
| 1302 | * |
| 1303 | * \param address address as given by drmMap(). |
| 1304 | * \param size size in bytes. Must match the size used by drmMap(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1305 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1306 | * \return zero on success, or a negative value on failure. |
| 1307 | * |
| 1308 | * \internal |
Adam Jackson | 22e41ef | 2006-02-20 23:09:00 +0000 | [diff] [blame] | 1309 | * This function is a wrapper for munmap(). |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1310 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1311 | drm_public int drmUnmap(drmAddress address, drmSize size) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1312 | { |
Emil Velikov | faf51d5 | 2014-09-07 20:03:05 +0100 | [diff] [blame] | 1313 | return drm_munmap(address, size); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1316 | drm_public drmBufInfoPtr drmGetBufInfo(int fd) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1317 | { |
| 1318 | drm_buf_info_t info; |
| 1319 | drmBufInfoPtr retval; |
| 1320 | int i; |
| 1321 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1322 | memclear(info); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1323 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1324 | if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1325 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1326 | |
| 1327 | if (info.count) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1328 | if (!(info.list = drmMalloc(info.count * sizeof(*info.list)))) |
| 1329 | return NULL; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1330 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1331 | if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) { |
| 1332 | drmFree(info.list); |
| 1333 | return NULL; |
| 1334 | } |
Adam Jackson | 22e41ef | 2006-02-20 23:09:00 +0000 | [diff] [blame] | 1335 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1336 | retval = drmMalloc(sizeof(*retval)); |
| 1337 | retval->count = info.count; |
| 1338 | retval->list = drmMalloc(info.count * sizeof(*retval->list)); |
| 1339 | for (i = 0; i < info.count; i++) { |
| 1340 | retval->list[i].count = info.list[i].count; |
| 1341 | retval->list[i].size = info.list[i].size; |
| 1342 | retval->list[i].low_mark = info.list[i].low_mark; |
| 1343 | retval->list[i].high_mark = info.list[i].high_mark; |
| 1344 | } |
| 1345 | drmFree(info.list); |
| 1346 | return retval; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1347 | } |
| 1348 | return NULL; |
| 1349 | } |
| 1350 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1351 | /** |
| 1352 | * Map all DMA buffers into client-virtual space. |
| 1353 | * |
| 1354 | * \param fd file descriptor. |
| 1355 | * |
| 1356 | * \return a pointer to a ::drmBufMap structure. |
| 1357 | * |
| 1358 | * \note The client may not use these buffers until obtaining buffer indices |
| 1359 | * with drmDMA(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1360 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1361 | * \internal |
| 1362 | * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned |
| 1363 | * information about the buffers in a drm_buf_map structure into the |
| 1364 | * client-visible data structures. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1365 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1366 | drm_public drmBufMapPtr drmMapBufs(int fd) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1367 | { |
| 1368 | drm_buf_map_t bufs; |
| 1369 | drmBufMapPtr retval; |
| 1370 | int i; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1371 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1372 | memclear(bufs); |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1373 | if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1374 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1375 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1376 | if (!bufs.count) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1377 | return NULL; |
Jon Smirl | 8696e71 | 2004-07-07 04:36:36 +0000 | [diff] [blame] | 1378 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1379 | if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list)))) |
| 1380 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1381 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1382 | if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) { |
| 1383 | drmFree(bufs.list); |
| 1384 | return NULL; |
| 1385 | } |
Adam Jackson | 22e41ef | 2006-02-20 23:09:00 +0000 | [diff] [blame] | 1386 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1387 | retval = drmMalloc(sizeof(*retval)); |
| 1388 | retval->count = bufs.count; |
| 1389 | retval->list = drmMalloc(bufs.count * sizeof(*retval->list)); |
| 1390 | for (i = 0; i < bufs.count; i++) { |
| 1391 | retval->list[i].idx = bufs.list[i].idx; |
| 1392 | retval->list[i].total = bufs.list[i].total; |
| 1393 | retval->list[i].used = 0; |
| 1394 | retval->list[i].address = bufs.list[i].address; |
| 1395 | } |
Jon Smirl | 8696e71 | 2004-07-07 04:36:36 +0000 | [diff] [blame] | 1396 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1397 | drmFree(bufs.list); |
| 1398 | return retval; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1401 | |
| 1402 | /** |
| 1403 | * Unmap buffers allocated with drmMapBufs(). |
| 1404 | * |
| 1405 | * \return zero on success, or negative value on failure. |
| 1406 | * |
| 1407 | * \internal |
Jon Smirl | 8696e71 | 2004-07-07 04:36:36 +0000 | [diff] [blame] | 1408 | * Calls munmap() for every buffer stored in \p bufs and frees the |
| 1409 | * memory allocated by drmMapBufs(). |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1410 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1411 | drm_public int drmUnmapBufs(drmBufMapPtr bufs) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1412 | { |
| 1413 | int i; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1414 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1415 | for (i = 0; i < bufs->count; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1416 | drm_munmap(bufs->list[i].address, bufs->list[i].total); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1417 | } |
Jon Smirl | 8696e71 | 2004-07-07 04:36:36 +0000 | [diff] [blame] | 1418 | |
| 1419 | drmFree(bufs->list); |
| 1420 | drmFree(bufs); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1424 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1425 | #define DRM_DMA_RETRY 16 |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1426 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1427 | /** |
| 1428 | * Reserve DMA buffers. |
| 1429 | * |
| 1430 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1431 | * \param request |
| 1432 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1433 | * \return zero on success, or a negative value on failure. |
| 1434 | * |
| 1435 | * \internal |
| 1436 | * Assemble the arguments into a drm_dma structure and keeps issuing the |
| 1437 | * DRM_IOCTL_DMA ioctl until success or until maximum number of retries. |
| 1438 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1439 | drm_public int drmDMA(int fd, drmDMAReqPtr request) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1440 | { |
| 1441 | drm_dma_t dma; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1442 | int ret, i = 0; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1443 | |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1444 | dma.context = request->context; |
| 1445 | dma.send_count = request->send_count; |
| 1446 | dma.send_indices = request->send_list; |
| 1447 | dma.send_sizes = request->send_sizes; |
| 1448 | dma.flags = request->flags; |
| 1449 | dma.request_count = request->request_count; |
| 1450 | dma.request_size = request->request_size; |
| 1451 | dma.request_indices = request->request_list; |
| 1452 | dma.request_sizes = request->request_sizes; |
Jon Smirl | 8696e71 | 2004-07-07 04:36:36 +0000 | [diff] [blame] | 1453 | dma.granted_count = 0; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1454 | |
| 1455 | do { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1456 | ret = ioctl( fd, DRM_IOCTL_DMA, &dma ); |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1457 | } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY ); |
| 1458 | |
| 1459 | if ( ret == 0 ) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1460 | request->granted_count = dma.granted_count; |
| 1461 | return 0; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1462 | } else { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1463 | return -errno; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1464 | } |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1467 | |
| 1468 | /** |
| 1469 | * Obtain heavyweight hardware lock. |
| 1470 | * |
| 1471 | * \param fd file descriptor. |
| 1472 | * \param context context. |
Eric Engestrom | 360292c | 2018-12-19 14:55:45 +0000 | [diff] [blame] | 1473 | * \param flags flags that determine the state of the hardware when the function |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1474 | * returns. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1475 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1476 | * \return always zero. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1477 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1478 | * \internal |
| 1479 | * This function translates the arguments into a drm_lock structure and issue |
| 1480 | * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired. |
| 1481 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1482 | drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1483 | { |
| 1484 | drm_lock_t lock; |
| 1485 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1486 | memclear(lock); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1487 | lock.context = context; |
| 1488 | lock.flags = 0; |
| 1489 | if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY; |
| 1490 | if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT; |
| 1491 | if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH; |
| 1492 | if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL; |
| 1493 | if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES; |
| 1494 | if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1495 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1496 | while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1497 | ; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1501 | /** |
| 1502 | * Release the hardware lock. |
| 1503 | * |
| 1504 | * \param fd file descriptor. |
| 1505 | * \param context context. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1506 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1507 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1508 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1509 | * \internal |
| 1510 | * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the |
| 1511 | * argument in a drm_lock structure. |
| 1512 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1513 | drm_public int drmUnlock(int fd, drm_context_t context) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1514 | { |
| 1515 | drm_lock_t lock; |
| 1516 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1517 | memclear(lock); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1518 | lock.context = context; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1519 | return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1522 | drm_public drm_context_t *drmGetReservedContextList(int fd, int *count) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1523 | { |
| 1524 | drm_ctx_res_t res; |
| 1525 | drm_ctx_t *list; |
Jon Smirl | 8696e71 | 2004-07-07 04:36:36 +0000 | [diff] [blame] | 1526 | drm_context_t * retval; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1527 | int i; |
| 1528 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1529 | memclear(res); |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1530 | if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1531 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1532 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1533 | if (!res.count) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1534 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1535 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1536 | if (!(list = drmMalloc(res.count * sizeof(*list)))) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1537 | return NULL; |
Seung-Woo Kim | 7b806e8 | 2017-03-27 11:09:29 +0900 | [diff] [blame] | 1538 | if (!(retval = drmMalloc(res.count * sizeof(*retval)))) |
| 1539 | goto err_free_list; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1540 | |
| 1541 | res.contexts = list; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1542 | if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res)) |
Seung-Woo Kim | 7b806e8 | 2017-03-27 11:09:29 +0900 | [diff] [blame] | 1543 | goto err_free_context; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1544 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1545 | for (i = 0; i < res.count; i++) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1546 | retval[i] = list[i].handle; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1547 | drmFree(list); |
| 1548 | |
| 1549 | *count = res.count; |
| 1550 | return retval; |
Seung-Woo Kim | 7b806e8 | 2017-03-27 11:09:29 +0900 | [diff] [blame] | 1551 | |
| 1552 | err_free_list: |
| 1553 | drmFree(list); |
| 1554 | err_free_context: |
| 1555 | drmFree(retval); |
| 1556 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1559 | drm_public void drmFreeReservedContextList(drm_context_t *pt) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1560 | { |
| 1561 | drmFree(pt); |
| 1562 | } |
| 1563 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1564 | /** |
| 1565 | * Create context. |
| 1566 | * |
| 1567 | * Used by the X server during GLXContext initialization. This causes |
| 1568 | * per-context kernel-level resources to be allocated. |
| 1569 | * |
| 1570 | * \param fd file descriptor. |
| 1571 | * \param handle is set on success. To be used by the client when requesting DMA |
| 1572 | * dispatch with drmDMA(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1573 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1574 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1575 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1576 | * \note May only be called by root. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1577 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1578 | * \internal |
| 1579 | * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the |
| 1580 | * argument in a drm_ctx structure. |
| 1581 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1582 | drm_public int drmCreateContext(int fd, drm_context_t *handle) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1583 | { |
| 1584 | drm_ctx_t ctx; |
| 1585 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1586 | memclear(ctx); |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1587 | if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1588 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1589 | *handle = ctx.handle; |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1593 | drm_public int drmSwitchToContext(int fd, drm_context_t context) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1594 | { |
| 1595 | drm_ctx_t ctx; |
| 1596 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1597 | memclear(ctx); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1598 | ctx.handle = context; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1599 | if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1600 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1601 | return 0; |
| 1602 | } |
| 1603 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1604 | drm_public int drmSetContextFlags(int fd, drm_context_t context, |
| 1605 | drm_context_tFlags flags) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1606 | { |
| 1607 | drm_ctx_t ctx; |
| 1608 | |
Adam Jackson | 22e41ef | 2006-02-20 23:09:00 +0000 | [diff] [blame] | 1609 | /* |
| 1610 | * Context preserving means that no context switches are done between DMA |
| 1611 | * buffers from one context and the next. This is suitable for use in the |
| 1612 | * X server (which promises to maintain hardware context), or in the |
| 1613 | * client-side library when buffers are swapped on behalf of two threads. |
| 1614 | */ |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1615 | memclear(ctx); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1616 | ctx.handle = context; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1617 | if (flags & DRM_CONTEXT_PRESERVED) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1618 | ctx.flags |= _DRM_CONTEXT_PRESERVED; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1619 | if (flags & DRM_CONTEXT_2DONLY) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1620 | ctx.flags |= _DRM_CONTEXT_2DONLY; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1621 | if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1622 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1623 | return 0; |
| 1624 | } |
| 1625 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1626 | drm_public int drmGetContextFlags(int fd, drm_context_t context, |
| 1627 | drm_context_tFlagsPtr flags) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1628 | { |
| 1629 | drm_ctx_t ctx; |
| 1630 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1631 | memclear(ctx); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1632 | ctx.handle = context; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1633 | if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1634 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1635 | *flags = 0; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1636 | if (ctx.flags & _DRM_CONTEXT_PRESERVED) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1637 | *flags |= DRM_CONTEXT_PRESERVED; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1638 | if (ctx.flags & _DRM_CONTEXT_2DONLY) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1639 | *flags |= DRM_CONTEXT_2DONLY; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1640 | return 0; |
| 1641 | } |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 1642 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1643 | /** |
| 1644 | * Destroy context. |
| 1645 | * |
| 1646 | * Free any kernel-level resources allocated with drmCreateContext() associated |
| 1647 | * with the context. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1648 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1649 | * \param fd file descriptor. |
| 1650 | * \param handle handle given by drmCreateContext(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1651 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1652 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1653 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1654 | * \note May only be called by root. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1655 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1656 | * \internal |
| 1657 | * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the |
| 1658 | * argument in a drm_ctx structure. |
| 1659 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1660 | drm_public int drmDestroyContext(int fd, drm_context_t handle) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1661 | { |
| 1662 | drm_ctx_t ctx; |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1663 | |
| 1664 | memclear(ctx); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1665 | ctx.handle = handle; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1666 | if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1667 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1668 | return 0; |
| 1669 | } |
| 1670 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1671 | drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1672 | { |
| 1673 | drm_draw_t draw; |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1674 | |
| 1675 | memclear(draw); |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1676 | if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1677 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1678 | *handle = draw.handle; |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1682 | drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1683 | { |
| 1684 | drm_draw_t draw; |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1685 | |
| 1686 | memclear(draw); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1687 | draw.handle = handle; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1688 | if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1689 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1693 | drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle, |
| 1694 | drm_drawable_info_type_t type, |
| 1695 | unsigned int num, void *data) |
Michel Dänzer | 9810ec2 | 2006-08-22 16:40:07 +0200 | [diff] [blame] | 1696 | { |
| 1697 | drm_update_draw_t update; |
| 1698 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1699 | memclear(update); |
Michel Dänzer | 9810ec2 | 2006-08-22 16:40:07 +0200 | [diff] [blame] | 1700 | update.handle = handle; |
| 1701 | update.type = type; |
| 1702 | update.num = num; |
| 1703 | update.data = (unsigned long long)(unsigned long)data; |
| 1704 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1705 | if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1706 | return -errno; |
Michel Dänzer | 9810ec2 | 2006-08-22 16:40:07 +0200 | [diff] [blame] | 1707 | |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1711 | drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence, |
| 1712 | uint64_t *ns) |
Keith Packard | d4331dd | 2017-07-01 00:43:15 -0700 | [diff] [blame] | 1713 | { |
| 1714 | struct drm_crtc_get_sequence get_seq; |
| 1715 | int ret; |
| 1716 | |
| 1717 | memclear(get_seq); |
| 1718 | get_seq.crtc_id = crtcId; |
| 1719 | ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq); |
| 1720 | if (ret) |
| 1721 | return ret; |
| 1722 | |
| 1723 | if (sequence) |
| 1724 | *sequence = get_seq.sequence; |
| 1725 | if (ns) |
| 1726 | *ns = get_seq.sequence_ns; |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1730 | drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags, |
| 1731 | uint64_t sequence, |
| 1732 | uint64_t *sequence_queued, |
| 1733 | uint64_t user_data) |
Keith Packard | d4331dd | 2017-07-01 00:43:15 -0700 | [diff] [blame] | 1734 | { |
| 1735 | struct drm_crtc_queue_sequence queue_seq; |
| 1736 | int ret; |
| 1737 | |
| 1738 | memclear(queue_seq); |
| 1739 | queue_seq.crtc_id = crtcId; |
| 1740 | queue_seq.flags = flags; |
| 1741 | queue_seq.sequence = sequence; |
| 1742 | queue_seq.user_data = user_data; |
| 1743 | |
| 1744 | ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq); |
| 1745 | if (ret == 0 && sequence_queued) |
| 1746 | *sequence_queued = queue_seq.sequence; |
| 1747 | |
| 1748 | return ret; |
| 1749 | } |
| 1750 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1751 | /** |
| 1752 | * Acquire the AGP device. |
| 1753 | * |
| 1754 | * Must be called before any of the other AGP related calls. |
| 1755 | * |
| 1756 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1757 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1758 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1759 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1760 | * \internal |
| 1761 | * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl. |
| 1762 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1763 | drm_public int drmAgpAcquire(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1764 | { |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1765 | if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1766 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1767 | return 0; |
| 1768 | } |
| 1769 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1770 | |
| 1771 | /** |
| 1772 | * Release the AGP device. |
| 1773 | * |
| 1774 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1775 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1776 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1777 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1778 | * \internal |
| 1779 | * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl. |
| 1780 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1781 | drm_public int drmAgpRelease(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1782 | { |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1783 | if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1784 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1785 | return 0; |
| 1786 | } |
| 1787 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1788 | |
| 1789 | /** |
| 1790 | * Set the AGP mode. |
| 1791 | * |
| 1792 | * \param fd file descriptor. |
| 1793 | * \param mode AGP mode. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1794 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1795 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1796 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1797 | * \internal |
| 1798 | * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the |
| 1799 | * argument in a drm_agp_mode structure. |
| 1800 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1801 | drm_public int drmAgpEnable(int fd, unsigned long mode) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1802 | { |
| 1803 | drm_agp_mode_t m; |
| 1804 | |
Connor Behan | 1490055 | 2015-03-24 13:53:51 -0400 | [diff] [blame] | 1805 | memclear(m); |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1806 | m.mode = mode; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1807 | if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1808 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1809 | return 0; |
| 1810 | } |
| 1811 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1812 | |
| 1813 | /** |
| 1814 | * Allocate a chunk of AGP memory. |
| 1815 | * |
| 1816 | * \param fd file descriptor. |
| 1817 | * \param size requested memory size in bytes. Will be rounded to page boundary. |
| 1818 | * \param type type of memory to allocate. |
| 1819 | * \param address if not zero, will be set to the physical address of the |
| 1820 | * allocated memory. |
| 1821 | * \param handle on success will be set to a handle of the allocated memory. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1822 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1823 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1824 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1825 | * \internal |
| 1826 | * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the |
| 1827 | * arguments in a drm_agp_buffer structure. |
| 1828 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1829 | drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type, |
| 1830 | unsigned long *address, drm_handle_t *handle) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1831 | { |
| 1832 | drm_agp_buffer_t b; |
Alan Hourihane | b0a9285 | 2003-09-24 14:39:25 +0000 | [diff] [blame] | 1833 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1834 | memclear(b); |
Alan Hourihane | b0a9285 | 2003-09-24 14:39:25 +0000 | [diff] [blame] | 1835 | *handle = DRM_AGP_NO_HANDLE; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1836 | b.size = size; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1837 | b.type = type; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1838 | if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1839 | return -errno; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 1840 | if (address != 0UL) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1841 | *address = b.physical; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1842 | *handle = b.handle; |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1846 | |
| 1847 | /** |
| 1848 | * Free a chunk of AGP memory. |
| 1849 | * |
| 1850 | * \param fd file descriptor. |
| 1851 | * \param handle handle to the allocated memory, as given by drmAgpAllocate(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1852 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1853 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1854 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1855 | * \internal |
| 1856 | * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the |
| 1857 | * argument in a drm_agp_buffer structure. |
| 1858 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1859 | drm_public int drmAgpFree(int fd, drm_handle_t handle) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1860 | { |
| 1861 | drm_agp_buffer_t b; |
| 1862 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1863 | memclear(b); |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1864 | b.handle = handle; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1865 | if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1866 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1867 | return 0; |
| 1868 | } |
| 1869 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1870 | |
| 1871 | /** |
| 1872 | * Bind a chunk of AGP memory. |
| 1873 | * |
| 1874 | * \param fd file descriptor. |
| 1875 | * \param handle handle to the allocated memory, as given by drmAgpAllocate(). |
| 1876 | * \param offset offset in bytes. It will round to page boundary. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1877 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1878 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1879 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1880 | * \internal |
| 1881 | * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the |
| 1882 | * argument in a drm_agp_binding structure. |
| 1883 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1884 | drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1885 | { |
| 1886 | drm_agp_binding_t b; |
| 1887 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1888 | memclear(b); |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1889 | b.handle = handle; |
| 1890 | b.offset = offset; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1891 | if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1892 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1893 | return 0; |
| 1894 | } |
| 1895 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1896 | |
| 1897 | /** |
| 1898 | * Unbind a chunk of AGP memory. |
| 1899 | * |
| 1900 | * \param fd file descriptor. |
| 1901 | * \param handle handle to the allocated memory, as given by drmAgpAllocate(). |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1902 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1903 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1904 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1905 | * \internal |
| 1906 | * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing |
| 1907 | * the argument in a drm_agp_binding structure. |
| 1908 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1909 | drm_public int drmAgpUnbind(int fd, drm_handle_t handle) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1910 | { |
| 1911 | drm_agp_binding_t b; |
| 1912 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1913 | memclear(b); |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1914 | b.handle = handle; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1915 | if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1916 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1917 | return 0; |
| 1918 | } |
| 1919 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1920 | |
| 1921 | /** |
| 1922 | * Get AGP driver major version number. |
| 1923 | * |
| 1924 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1925 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1926 | * \return major version number on success, or a negative value on failure.. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1927 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1928 | * \internal |
| 1929 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 1930 | * necessary information in a drm_agp_info structure. |
| 1931 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1932 | drm_public int drmAgpVersionMajor(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1933 | { |
| 1934 | drm_agp_info_t i; |
| 1935 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1936 | memclear(i); |
| 1937 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1938 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1939 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1940 | return i.agp_version_major; |
| 1941 | } |
| 1942 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1943 | |
| 1944 | /** |
| 1945 | * Get AGP driver minor version number. |
| 1946 | * |
| 1947 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1948 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1949 | * \return minor version number on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1950 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1951 | * \internal |
| 1952 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 1953 | * necessary information in a drm_agp_info structure. |
| 1954 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1955 | drm_public int drmAgpVersionMinor(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1956 | { |
| 1957 | drm_agp_info_t i; |
| 1958 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1959 | memclear(i); |
| 1960 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1961 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1962 | return -errno; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1963 | return i.agp_version_minor; |
| 1964 | } |
| 1965 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1966 | |
| 1967 | /** |
| 1968 | * Get AGP mode. |
| 1969 | * |
| 1970 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1971 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1972 | * \return mode on success, or zero on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1973 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1974 | * \internal |
| 1975 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 1976 | * necessary information in a drm_agp_info structure. |
| 1977 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1978 | drm_public unsigned long drmAgpGetMode(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1979 | { |
| 1980 | drm_agp_info_t i; |
| 1981 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 1982 | memclear(i); |
| 1983 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 1984 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1985 | return 0; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 1986 | return i.mode; |
| 1987 | } |
| 1988 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1989 | |
| 1990 | /** |
| 1991 | * Get AGP aperture base. |
| 1992 | * |
| 1993 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1994 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1995 | * \return aperture base on success, zero on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 1996 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 1997 | * \internal |
| 1998 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 1999 | * necessary information in a drm_agp_info structure. |
| 2000 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2001 | drm_public unsigned long drmAgpBase(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2002 | { |
| 2003 | drm_agp_info_t i; |
| 2004 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2005 | memclear(i); |
| 2006 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2007 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2008 | return 0; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2009 | return i.aperture_base; |
| 2010 | } |
| 2011 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2012 | |
| 2013 | /** |
| 2014 | * Get AGP aperture size. |
| 2015 | * |
| 2016 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2017 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2018 | * \return aperture size on success, zero on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2019 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2020 | * \internal |
| 2021 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 2022 | * necessary information in a drm_agp_info structure. |
| 2023 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2024 | drm_public unsigned long drmAgpSize(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2025 | { |
| 2026 | drm_agp_info_t i; |
| 2027 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2028 | memclear(i); |
| 2029 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2030 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2031 | return 0; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2032 | return i.aperture_size; |
| 2033 | } |
| 2034 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2035 | |
| 2036 | /** |
| 2037 | * Get used AGP memory. |
| 2038 | * |
| 2039 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2040 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2041 | * \return memory used on success, or zero on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2042 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2043 | * \internal |
| 2044 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 2045 | * necessary information in a drm_agp_info structure. |
| 2046 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2047 | drm_public unsigned long drmAgpMemoryUsed(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2048 | { |
| 2049 | drm_agp_info_t i; |
| 2050 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2051 | memclear(i); |
| 2052 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2053 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2054 | return 0; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2055 | return i.memory_used; |
| 2056 | } |
| 2057 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2058 | |
| 2059 | /** |
| 2060 | * Get available AGP memory. |
| 2061 | * |
| 2062 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2063 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2064 | * \return memory available on success, or zero on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2065 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2066 | * \internal |
| 2067 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 2068 | * necessary information in a drm_agp_info structure. |
| 2069 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2070 | drm_public unsigned long drmAgpMemoryAvail(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2071 | { |
| 2072 | drm_agp_info_t i; |
| 2073 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2074 | memclear(i); |
| 2075 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2076 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2077 | return 0; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2078 | return i.memory_allowed; |
| 2079 | } |
| 2080 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2081 | |
| 2082 | /** |
| 2083 | * Get hardware vendor ID. |
| 2084 | * |
| 2085 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2086 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2087 | * \return vendor ID on success, or zero on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2088 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2089 | * \internal |
| 2090 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 2091 | * necessary information in a drm_agp_info structure. |
| 2092 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2093 | drm_public unsigned int drmAgpVendorId(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2094 | { |
| 2095 | drm_agp_info_t i; |
| 2096 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2097 | memclear(i); |
| 2098 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2099 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2100 | return 0; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2101 | return i.id_vendor; |
| 2102 | } |
| 2103 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2104 | |
| 2105 | /** |
| 2106 | * Get hardware device ID. |
| 2107 | * |
| 2108 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2109 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2110 | * \return zero on success, or zero on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2111 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2112 | * \internal |
| 2113 | * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the |
| 2114 | * necessary information in a drm_agp_info structure. |
| 2115 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2116 | drm_public unsigned int drmAgpDeviceId(int fd) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2117 | { |
| 2118 | drm_agp_info_t i; |
| 2119 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2120 | memclear(i); |
| 2121 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2122 | if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2123 | return 0; |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 2124 | return i.id_device; |
| 2125 | } |
| 2126 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2127 | drm_public int drmScatterGatherAlloc(int fd, unsigned long size, |
| 2128 | drm_handle_t *handle) |
Kevin E Martin | 5d6ddbc | 2001-04-05 22:16:12 +0000 | [diff] [blame] | 2129 | { |
| 2130 | drm_scatter_gather_t sg; |
| 2131 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2132 | memclear(sg); |
| 2133 | |
Kevin E Martin | 5d6ddbc | 2001-04-05 22:16:12 +0000 | [diff] [blame] | 2134 | *handle = 0; |
| 2135 | sg.size = size; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2136 | if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2137 | return -errno; |
Kevin E Martin | 5d6ddbc | 2001-04-05 22:16:12 +0000 | [diff] [blame] | 2138 | *handle = sg.handle; |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2142 | drm_public int drmScatterGatherFree(int fd, drm_handle_t handle) |
Kevin E Martin | 5d6ddbc | 2001-04-05 22:16:12 +0000 | [diff] [blame] | 2143 | { |
| 2144 | drm_scatter_gather_t sg; |
| 2145 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2146 | memclear(sg); |
Kevin E Martin | 5d6ddbc | 2001-04-05 22:16:12 +0000 | [diff] [blame] | 2147 | sg.handle = handle; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2148 | if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2149 | return -errno; |
Kevin E Martin | 5d6ddbc | 2001-04-05 22:16:12 +0000 | [diff] [blame] | 2150 | return 0; |
| 2151 | } |
| 2152 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2153 | /** |
| 2154 | * Wait for VBLANK. |
| 2155 | * |
| 2156 | * \param fd file descriptor. |
| 2157 | * \param vbl pointer to a drmVBlank structure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2158 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2159 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2160 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2161 | * \internal |
| 2162 | * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl. |
| 2163 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2164 | drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl) |
Michel Daenzer | 55acd0d | 2002-09-25 17:18:19 +0000 | [diff] [blame] | 2165 | { |
Jesse Barnes | f4f76a6 | 2009-01-07 10:18:08 -0800 | [diff] [blame] | 2166 | struct timespec timeout, cur; |
Michel Daenzer | 55acd0d | 2002-09-25 17:18:19 +0000 | [diff] [blame] | 2167 | int ret; |
| 2168 | |
Jesse Barnes | f4f76a6 | 2009-01-07 10:18:08 -0800 | [diff] [blame] | 2169 | ret = clock_gettime(CLOCK_MONOTONIC, &timeout); |
| 2170 | if (ret < 0) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2171 | fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno)); |
| 2172 | goto out; |
Jesse Barnes | f4f76a6 | 2009-01-07 10:18:08 -0800 | [diff] [blame] | 2173 | } |
| 2174 | timeout.tv_sec++; |
| 2175 | |
Michel Daenzer | 55acd0d | 2002-09-25 17:18:19 +0000 | [diff] [blame] | 2176 | do { |
Jesse Barnes | f4f76a6 | 2009-01-07 10:18:08 -0800 | [diff] [blame] | 2177 | ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl); |
Michel Daenzer | c7d471b | 2003-02-02 03:06:47 +0000 | [diff] [blame] | 2178 | vbl->request.type &= ~DRM_VBLANK_RELATIVE; |
Jesse Barnes | ca37077 | 2009-01-07 10:48:26 -0800 | [diff] [blame] | 2179 | if (ret && errno == EINTR) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2180 | clock_gettime(CLOCK_MONOTONIC, &cur); |
| 2181 | /* Timeout after 1s */ |
| 2182 | if (cur.tv_sec > timeout.tv_sec + 1 || |
| 2183 | (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >= |
| 2184 | timeout.tv_nsec)) { |
| 2185 | errno = EBUSY; |
| 2186 | ret = -1; |
| 2187 | break; |
| 2188 | } |
Jesse Barnes | f4f76a6 | 2009-01-07 10:18:08 -0800 | [diff] [blame] | 2189 | } |
Michel Daenzer | 55acd0d | 2002-09-25 17:18:19 +0000 | [diff] [blame] | 2190 | } while (ret && errno == EINTR); |
| 2191 | |
Jesse Barnes | f4f76a6 | 2009-01-07 10:18:08 -0800 | [diff] [blame] | 2192 | out: |
Michel Daenzer | 55acd0d | 2002-09-25 17:18:19 +0000 | [diff] [blame] | 2193 | return ret; |
| 2194 | } |
| 2195 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2196 | drm_public int drmError(int err, const char *label) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2197 | { |
| 2198 | switch (err) { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2199 | case DRM_ERR_NO_DEVICE: |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2200 | fprintf(stderr, "%s: no device\n", label); |
| 2201 | break; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2202 | case DRM_ERR_NO_ACCESS: |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2203 | fprintf(stderr, "%s: no access\n", label); |
| 2204 | break; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2205 | case DRM_ERR_NOT_ROOT: |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2206 | fprintf(stderr, "%s: not root\n", label); |
| 2207 | break; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2208 | case DRM_ERR_INVALID: |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2209 | fprintf(stderr, "%s: invalid args\n", label); |
| 2210 | break; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2211 | default: |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2212 | if (err < 0) |
| 2213 | err = -err; |
| 2214 | fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) ); |
| 2215 | break; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | return 1; |
| 2219 | } |
| 2220 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2221 | /** |
| 2222 | * Install IRQ handler. |
| 2223 | * |
| 2224 | * \param fd file descriptor. |
| 2225 | * \param irq IRQ number. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2226 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2227 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2228 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2229 | * \internal |
| 2230 | * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the |
| 2231 | * argument in a drm_control structure. |
| 2232 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2233 | drm_public int drmCtlInstHandler(int fd, int irq) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2234 | { |
| 2235 | drm_control_t ctl; |
| 2236 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2237 | memclear(ctl); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2238 | ctl.func = DRM_INST_HANDLER; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2239 | ctl.irq = irq; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2240 | if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2241 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2242 | return 0; |
| 2243 | } |
| 2244 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2245 | |
| 2246 | /** |
| 2247 | * Uninstall IRQ handler. |
| 2248 | * |
| 2249 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2250 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2251 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2252 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2253 | * \internal |
| 2254 | * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the |
| 2255 | * argument in a drm_control structure. |
| 2256 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2257 | drm_public int drmCtlUninstHandler(int fd) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2258 | { |
| 2259 | drm_control_t ctl; |
| 2260 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2261 | memclear(ctl); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2262 | ctl.func = DRM_UNINST_HANDLER; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2263 | ctl.irq = 0; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2264 | if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2265 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2266 | return 0; |
| 2267 | } |
| 2268 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2269 | drm_public int drmFinish(int fd, int context, drmLockFlags flags) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2270 | { |
| 2271 | drm_lock_t lock; |
| 2272 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2273 | memclear(lock); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2274 | lock.context = context; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2275 | if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY; |
| 2276 | if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT; |
| 2277 | if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH; |
| 2278 | if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL; |
| 2279 | if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES; |
| 2280 | if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2281 | if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2282 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2283 | return 0; |
| 2284 | } |
| 2285 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2286 | /** |
| 2287 | * Get IRQ from bus ID. |
| 2288 | * |
| 2289 | * \param fd file descriptor. |
| 2290 | * \param busnum bus number. |
| 2291 | * \param devnum device number. |
| 2292 | * \param funcnum function number. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2293 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2294 | * \return IRQ number on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2295 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2296 | * \internal |
| 2297 | * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the |
| 2298 | * arguments in a drm_irq_busid structure. |
| 2299 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2300 | drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum, |
| 2301 | int funcnum) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2302 | { |
| 2303 | drm_irq_busid_t p; |
| 2304 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2305 | memclear(p); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2306 | p.busnum = busnum; |
| 2307 | p.devnum = devnum; |
| 2308 | p.funcnum = funcnum; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2309 | if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2310 | return -errno; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2311 | return p.irq; |
| 2312 | } |
| 2313 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2314 | drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2315 | { |
| 2316 | drmHashEntry *entry = drmGetEntry(fd); |
| 2317 | |
| 2318 | if (drmHashInsert(entry->tagTable, context, tag)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2319 | drmHashDelete(entry->tagTable, context); |
| 2320 | drmHashInsert(entry->tagTable, context, tag); |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2321 | } |
| 2322 | return 0; |
| 2323 | } |
| 2324 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2325 | drm_public int drmDelContextTag(int fd, drm_context_t context) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2326 | { |
| 2327 | drmHashEntry *entry = drmGetEntry(fd); |
| 2328 | |
| 2329 | return drmHashDelete(entry->tagTable, context); |
| 2330 | } |
| 2331 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2332 | drm_public void *drmGetContextTag(int fd, drm_context_t context) |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2333 | { |
| 2334 | drmHashEntry *entry = drmGetEntry(fd); |
| 2335 | void *value; |
Gareth Hughes | 3604753 | 2001-02-15 08:12:14 +0000 | [diff] [blame] | 2336 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2337 | if (drmHashLookup(entry->tagTable, context, &value)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2338 | return NULL; |
Daryll Strauss | b3a5766 | 1999-12-05 01:19:48 +0000 | [diff] [blame] | 2339 | |
| 2340 | return value; |
| 2341 | } |
| 2342 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2343 | drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id, |
| 2344 | drm_handle_t handle) |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 2345 | { |
| 2346 | drm_ctx_priv_map_t map; |
| 2347 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2348 | memclear(map); |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 2349 | map.ctx_id = ctx_id; |
Jeremy Huddleston | 961bf9b | 2011-11-01 14:42:13 -0700 | [diff] [blame] | 2350 | map.handle = (void *)(uintptr_t)handle; |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 2351 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2352 | if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2353 | return -errno; |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 2354 | return 0; |
| 2355 | } |
| 2356 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2357 | drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id, |
| 2358 | drm_handle_t *handle) |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 2359 | { |
| 2360 | drm_ctx_priv_map_t map; |
| 2361 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2362 | memclear(map); |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 2363 | map.ctx_id = ctx_id; |
| 2364 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2365 | if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2366 | return -errno; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2367 | if (handle) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2368 | *handle = (drm_handle_t)(uintptr_t)map.handle; |
Kevin E Martin | 74e19a4 | 2001-03-14 22:22:50 +0000 | [diff] [blame] | 2369 | |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2373 | drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size, |
| 2374 | drmMapType *type, drmMapFlags *flags, |
| 2375 | drm_handle_t *handle, int *mtrr) |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2376 | { |
| 2377 | drm_map_t map; |
| 2378 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2379 | memclear(map); |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2380 | map.offset = idx; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2381 | if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2382 | return -errno; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2383 | *offset = map.offset; |
| 2384 | *size = map.size; |
| 2385 | *type = map.type; |
| 2386 | *flags = map.flags; |
| 2387 | *handle = (unsigned long)map.handle; |
| 2388 | *mtrr = map.mtrr; |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2392 | drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid, |
| 2393 | unsigned long *magic, unsigned long *iocs) |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2394 | { |
| 2395 | drm_client_t client; |
| 2396 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2397 | memclear(client); |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2398 | client.idx = idx; |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2399 | if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2400 | return -errno; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2401 | *auth = client.auth; |
| 2402 | *pid = client.pid; |
| 2403 | *uid = client.uid; |
| 2404 | *magic = client.magic; |
| 2405 | *iocs = client.iocs; |
| 2406 | return 0; |
| 2407 | } |
| 2408 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2409 | drm_public int drmGetStats(int fd, drmStatsT *stats) |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2410 | { |
| 2411 | drm_stats_t s; |
Jan Vesely | de8532d | 2014-11-30 12:53:18 -0500 | [diff] [blame] | 2412 | unsigned i; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2413 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2414 | memclear(s); |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2415 | if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2416 | return -errno; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2417 | |
| 2418 | stats->count = 0; |
| 2419 | memset(stats, 0, sizeof(*stats)); |
| 2420 | if (s.count > sizeof(stats->data)/sizeof(stats->data[0])) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2421 | return -1; |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2422 | |
| 2423 | #define SET_VALUE \ |
| 2424 | stats->data[i].long_format = "%-20.20s"; \ |
| 2425 | stats->data[i].rate_format = "%8.8s"; \ |
| 2426 | stats->data[i].isvalue = 1; \ |
| 2427 | stats->data[i].verbose = 0 |
| 2428 | |
| 2429 | #define SET_COUNT \ |
| 2430 | stats->data[i].long_format = "%-20.20s"; \ |
| 2431 | stats->data[i].rate_format = "%5.5s"; \ |
| 2432 | stats->data[i].isvalue = 0; \ |
| 2433 | stats->data[i].mult_names = "kgm"; \ |
| 2434 | stats->data[i].mult = 1000; \ |
| 2435 | stats->data[i].verbose = 0 |
| 2436 | |
| 2437 | #define SET_BYTE \ |
| 2438 | stats->data[i].long_format = "%-20.20s"; \ |
| 2439 | stats->data[i].rate_format = "%5.5s"; \ |
| 2440 | stats->data[i].isvalue = 0; \ |
| 2441 | stats->data[i].mult_names = "KGM"; \ |
| 2442 | stats->data[i].mult = 1024; \ |
| 2443 | stats->data[i].verbose = 0 |
| 2444 | |
| 2445 | |
| 2446 | stats->count = s.count; |
| 2447 | for (i = 0; i < s.count; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2448 | stats->data[i].value = s.data[i].value; |
| 2449 | switch (s.data[i].type) { |
| 2450 | case _DRM_STAT_LOCK: |
| 2451 | stats->data[i].long_name = "Lock"; |
| 2452 | stats->data[i].rate_name = "Lock"; |
| 2453 | SET_VALUE; |
| 2454 | break; |
| 2455 | case _DRM_STAT_OPENS: |
| 2456 | stats->data[i].long_name = "Opens"; |
| 2457 | stats->data[i].rate_name = "O"; |
| 2458 | SET_COUNT; |
| 2459 | stats->data[i].verbose = 1; |
| 2460 | break; |
| 2461 | case _DRM_STAT_CLOSES: |
| 2462 | stats->data[i].long_name = "Closes"; |
| 2463 | stats->data[i].rate_name = "Lock"; |
| 2464 | SET_COUNT; |
| 2465 | stats->data[i].verbose = 1; |
| 2466 | break; |
| 2467 | case _DRM_STAT_IOCTLS: |
| 2468 | stats->data[i].long_name = "Ioctls"; |
| 2469 | stats->data[i].rate_name = "Ioc/s"; |
| 2470 | SET_COUNT; |
| 2471 | break; |
| 2472 | case _DRM_STAT_LOCKS: |
| 2473 | stats->data[i].long_name = "Locks"; |
| 2474 | stats->data[i].rate_name = "Lck/s"; |
| 2475 | SET_COUNT; |
| 2476 | break; |
| 2477 | case _DRM_STAT_UNLOCKS: |
| 2478 | stats->data[i].long_name = "Unlocks"; |
| 2479 | stats->data[i].rate_name = "Unl/s"; |
| 2480 | SET_COUNT; |
| 2481 | break; |
| 2482 | case _DRM_STAT_IRQ: |
| 2483 | stats->data[i].long_name = "IRQs"; |
| 2484 | stats->data[i].rate_name = "IRQ/s"; |
| 2485 | SET_COUNT; |
| 2486 | break; |
| 2487 | case _DRM_STAT_PRIMARY: |
| 2488 | stats->data[i].long_name = "Primary Bytes"; |
| 2489 | stats->data[i].rate_name = "PB/s"; |
| 2490 | SET_BYTE; |
| 2491 | break; |
| 2492 | case _DRM_STAT_SECONDARY: |
| 2493 | stats->data[i].long_name = "Secondary Bytes"; |
| 2494 | stats->data[i].rate_name = "SB/s"; |
| 2495 | SET_BYTE; |
| 2496 | break; |
| 2497 | case _DRM_STAT_DMA: |
| 2498 | stats->data[i].long_name = "DMA"; |
| 2499 | stats->data[i].rate_name = "DMA/s"; |
| 2500 | SET_COUNT; |
| 2501 | break; |
| 2502 | case _DRM_STAT_SPECIAL: |
| 2503 | stats->data[i].long_name = "Special DMA"; |
| 2504 | stats->data[i].rate_name = "dma/s"; |
| 2505 | SET_COUNT; |
| 2506 | break; |
| 2507 | case _DRM_STAT_MISSED: |
| 2508 | stats->data[i].long_name = "Miss"; |
| 2509 | stats->data[i].rate_name = "Ms/s"; |
| 2510 | SET_COUNT; |
| 2511 | break; |
| 2512 | case _DRM_STAT_VALUE: |
| 2513 | stats->data[i].long_name = "Value"; |
| 2514 | stats->data[i].rate_name = "Value"; |
| 2515 | SET_VALUE; |
| 2516 | break; |
| 2517 | case _DRM_STAT_BYTE: |
| 2518 | stats->data[i].long_name = "Bytes"; |
| 2519 | stats->data[i].rate_name = "B/s"; |
| 2520 | SET_BYTE; |
| 2521 | break; |
| 2522 | case _DRM_STAT_COUNT: |
| 2523 | default: |
| 2524 | stats->data[i].long_name = "Count"; |
| 2525 | stats->data[i].rate_name = "Cnt/s"; |
| 2526 | SET_COUNT; |
| 2527 | break; |
| 2528 | } |
Rik Faith | 88dbee5 | 2001-02-28 09:27:44 +0000 | [diff] [blame] | 2529 | } |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2533 | /** |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2534 | * Issue a set-version ioctl. |
| 2535 | * |
| 2536 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2537 | * \param drmCommandIndex command index |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2538 | * \param data source pointer of the data to be read and written. |
| 2539 | * \param size size of the data to be read and written. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2540 | * |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2541 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2542 | * |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2543 | * \internal |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2544 | * It issues a read-write ioctl given by |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2545 | * \code DRM_COMMAND_BASE + drmCommandIndex \endcode. |
| 2546 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2547 | drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version) |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2548 | { |
| 2549 | int retcode = 0; |
| 2550 | drm_set_version_t sv; |
| 2551 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2552 | memclear(sv); |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2553 | sv.drm_di_major = version->drm_di_major; |
| 2554 | sv.drm_di_minor = version->drm_di_minor; |
| 2555 | sv.drm_dd_major = version->drm_dd_major; |
| 2556 | sv.drm_dd_minor = version->drm_dd_minor; |
| 2557 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2558 | if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2559 | retcode = -errno; |
Eric Anholt | 06cb132 | 2003-10-23 02:23:31 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | version->drm_di_major = sv.drm_di_major; |
| 2563 | version->drm_di_minor = sv.drm_di_minor; |
| 2564 | version->drm_dd_major = sv.drm_dd_major; |
| 2565 | version->drm_dd_minor = sv.drm_dd_minor; |
| 2566 | |
| 2567 | return retcode; |
| 2568 | } |
| 2569 | |
| 2570 | /** |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2571 | * Send a device-specific command. |
| 2572 | * |
| 2573 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2574 | * \param drmCommandIndex command index |
| 2575 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2576 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2577 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2578 | * \internal |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2579 | * It issues a ioctl given by |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2580 | * \code DRM_COMMAND_BASE + drmCommandIndex \endcode. |
| 2581 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2582 | drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex) |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2583 | { |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2584 | unsigned long request; |
| 2585 | |
| 2586 | request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex); |
| 2587 | |
Daniel Vetter | fd38794 | 2015-02-11 12:41:04 +0100 | [diff] [blame] | 2588 | if (drmIoctl(fd, request, NULL)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2589 | return -errno; |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2590 | } |
| 2591 | return 0; |
| 2592 | } |
| 2593 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2594 | |
| 2595 | /** |
| 2596 | * Send a device-specific read command. |
| 2597 | * |
| 2598 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2599 | * \param drmCommandIndex command index |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2600 | * \param data destination pointer of the data to be read. |
| 2601 | * \param size size of the data to be read. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2602 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2603 | * \return zero on success, or a negative value on failure. |
| 2604 | * |
| 2605 | * \internal |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2606 | * It issues a read ioctl given by |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2607 | * \code DRM_COMMAND_BASE + drmCommandIndex \endcode. |
| 2608 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2609 | drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex, |
| 2610 | void *data, unsigned long size) |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2611 | { |
| 2612 | unsigned long request; |
| 2613 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2614 | request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE, |
| 2615 | DRM_COMMAND_BASE + drmCommandIndex, size); |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2616 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2617 | if (drmIoctl(fd, request, data)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2618 | return -errno; |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2619 | } |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2623 | |
| 2624 | /** |
| 2625 | * Send a device-specific write command. |
| 2626 | * |
| 2627 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2628 | * \param drmCommandIndex command index |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2629 | * \param data source pointer of the data to be written. |
| 2630 | * \param size size of the data to be written. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2631 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2632 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2633 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2634 | * \internal |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2635 | * It issues a write ioctl given by |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2636 | * \code DRM_COMMAND_BASE + drmCommandIndex \endcode. |
| 2637 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2638 | drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex, |
| 2639 | void *data, unsigned long size) |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2640 | { |
| 2641 | unsigned long request; |
| 2642 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2643 | request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE, |
| 2644 | DRM_COMMAND_BASE + drmCommandIndex, size); |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2645 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2646 | if (drmIoctl(fd, request, data)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2647 | return -errno; |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2648 | } |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2652 | |
| 2653 | /** |
| 2654 | * Send a device-specific read-write command. |
| 2655 | * |
| 2656 | * \param fd file descriptor. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2657 | * \param drmCommandIndex command index |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2658 | * \param data source pointer of the data to be read and written. |
| 2659 | * \param size size of the data to be read and written. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2660 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2661 | * \return zero on success, or a negative value on failure. |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2662 | * |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2663 | * \internal |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2664 | * It issues a read-write ioctl given by |
Jose Fonseca | d2443b2 | 2003-05-27 00:37:33 +0000 | [diff] [blame] | 2665 | * \code DRM_COMMAND_BASE + drmCommandIndex \endcode. |
| 2666 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2667 | drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, |
| 2668 | void *data, unsigned long size) |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2669 | { |
| 2670 | unsigned long request; |
| 2671 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2672 | request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE, |
| 2673 | DRM_COMMAND_BASE + drmCommandIndex, size); |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2674 | |
Keith Packard | 8b9ab10 | 2008-06-13 16:03:22 -0700 | [diff] [blame] | 2675 | if (drmIoctl(fd, request, data)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2676 | return -errno; |
Jens Owen | 3903e5a | 2002-04-09 21:54:56 +0000 | [diff] [blame] | 2677 | return 0; |
| 2678 | } |
Thomas Hellstrom | 166da93 | 2006-08-21 21:02:08 +0200 | [diff] [blame] | 2679 | |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2680 | #define DRM_MAX_FDS 16 |
| 2681 | static struct { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2682 | char *BusID; |
| 2683 | int fd; |
| 2684 | int refcount; |
Jammy Zhou | dbc8b11 | 2015-02-02 18:06:27 +0800 | [diff] [blame] | 2685 | int type; |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2686 | } connection[DRM_MAX_FDS]; |
| 2687 | |
| 2688 | static int nr_fds = 0; |
| 2689 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2690 | drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened) |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2691 | { |
Jammy Zhou | dbc8b11 | 2015-02-02 18:06:27 +0800 | [diff] [blame] | 2692 | return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY); |
| 2693 | } |
| 2694 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2695 | drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened, |
| 2696 | int type) |
Jammy Zhou | dbc8b11 | 2015-02-02 18:06:27 +0800 | [diff] [blame] | 2697 | { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2698 | int i; |
| 2699 | int fd; |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2700 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2701 | for (i = 0; i < nr_fds; i++) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2702 | if ((strcmp(BusID, connection[i].BusID) == 0) && |
| 2703 | (connection[i].type == type)) { |
| 2704 | connection[i].refcount++; |
| 2705 | *newlyopened = 0; |
| 2706 | return connection[i].fd; |
| 2707 | } |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2708 | |
Jammy Zhou | dbc8b11 | 2015-02-02 18:06:27 +0800 | [diff] [blame] | 2709 | fd = drmOpenWithType(NULL, BusID, type); |
Emil Velikov | c1cd3d9 | 2015-07-14 15:05:18 +0100 | [diff] [blame] | 2710 | if (fd < 0 || nr_fds == DRM_MAX_FDS) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2711 | return fd; |
| 2712 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2713 | connection[nr_fds].BusID = strdup(BusID); |
| 2714 | connection[nr_fds].fd = fd; |
| 2715 | connection[nr_fds].refcount = 1; |
Jammy Zhou | dbc8b11 | 2015-02-02 18:06:27 +0800 | [diff] [blame] | 2716 | connection[nr_fds].type = type; |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2717 | *newlyopened = 1; |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2718 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2719 | if (0) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2720 | fprintf(stderr, "saved connection %d for %s %d\n", |
| 2721 | nr_fds, connection[nr_fds].BusID, |
| 2722 | strcmp(BusID, connection[nr_fds].BusID)); |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2723 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2724 | nr_fds++; |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2725 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2726 | return fd; |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2727 | } |
| 2728 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2729 | drm_public void drmCloseOnce(int fd) |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2730 | { |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2731 | int i; |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2732 | |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2733 | for (i = 0; i < nr_fds; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2734 | if (fd == connection[i].fd) { |
| 2735 | if (--connection[i].refcount == 0) { |
| 2736 | drmClose(connection[i].fd); |
| 2737 | free(connection[i].BusID); |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2738 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2739 | if (i < --nr_fds) |
| 2740 | connection[i] = connection[nr_fds]; |
| 2741 | |
| 2742 | return; |
| 2743 | } |
| 2744 | } |
Brian | ccd7b6e | 2007-05-29 14:54:00 -0600 | [diff] [blame] | 2745 | } |
Dave Airlie | d51e1bb | 2006-11-09 08:55:58 +1100 | [diff] [blame] | 2746 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 2747 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2748 | drm_public int drmSetMaster(int fd) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 2749 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2750 | return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 2751 | } |
| 2752 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2753 | drm_public int drmDropMaster(int fd) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 2754 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2755 | return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 2756 | } |
Kristian Høgsberg | 22d4666 | 2009-11-23 20:51:34 -0500 | [diff] [blame] | 2757 | |
Eric Engestrom | eba6609 | 2019-02-08 14:46:07 +0000 | [diff] [blame] | 2758 | drm_public int drmIsMaster(int fd) |
Christopher James Halse Rogers | 17dfe3a | 2019-01-23 15:38:45 +1100 | [diff] [blame] | 2759 | { |
| 2760 | /* Detect master by attempting something that requires master. |
| 2761 | * |
| 2762 | * Authenticating magic tokens requires master and 0 is an |
| 2763 | * internal kernel detail which we could use. Attempting this on |
| 2764 | * a master fd would fail therefore fail with EINVAL because 0 |
| 2765 | * is invalid. |
| 2766 | * |
| 2767 | * A non-master fd will fail with EACCES, as the kernel checks |
| 2768 | * for master before attempting to do anything else. |
| 2769 | * |
| 2770 | * Since we don't want to leak implementation details, use |
| 2771 | * EACCES. |
| 2772 | */ |
| 2773 | return drmAuthMagic(fd, 0) != -EACCES; |
| 2774 | } |
| 2775 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2776 | drm_public char *drmGetDeviceNameFromFd(int fd) |
Kristian Høgsberg | 22d4666 | 2009-11-23 20:51:34 -0500 | [diff] [blame] | 2777 | { |
Emmanuel Vadot | 6818a50 | 2020-01-21 17:50:15 +0100 | [diff] [blame] | 2778 | #ifdef __FreeBSD__ |
| 2779 | struct stat sbuf; |
Emmanuel Vadot | 6818a50 | 2020-01-21 17:50:15 +0100 | [diff] [blame] | 2780 | int maj, min; |
Emmanuel Vadot | 5aa83dd | 2020-04-28 16:33:05 +0200 | [diff] [blame] | 2781 | int nodetype; |
Emmanuel Vadot | 6818a50 | 2020-01-21 17:50:15 +0100 | [diff] [blame] | 2782 | |
| 2783 | if (fstat(fd, &sbuf)) |
| 2784 | return NULL; |
| 2785 | |
| 2786 | maj = major(sbuf.st_rdev); |
| 2787 | min = minor(sbuf.st_rdev); |
Emmanuel Vadot | 5aa83dd | 2020-04-28 16:33:05 +0200 | [diff] [blame] | 2788 | nodetype = drmGetMinorType(maj, min); |
| 2789 | return drmGetMinorNameForFD(fd, nodetype); |
Emmanuel Vadot | 6818a50 | 2020-01-21 17:50:15 +0100 | [diff] [blame] | 2790 | #else |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2791 | char name[128]; |
| 2792 | struct stat sbuf; |
| 2793 | dev_t d; |
| 2794 | int i; |
Kristian Høgsberg | 22d4666 | 2009-11-23 20:51:34 -0500 | [diff] [blame] | 2795 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2796 | /* The whole drmOpen thing is a fiasco and we need to find a way |
| 2797 | * back to just using open(2). For now, however, lets just make |
| 2798 | * things worse with even more ad hoc directory walking code to |
| 2799 | * discover the device file name. */ |
Kristian Høgsberg | 22d4666 | 2009-11-23 20:51:34 -0500 | [diff] [blame] | 2800 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2801 | fstat(fd, &sbuf); |
| 2802 | d = sbuf.st_rdev; |
Kristian Høgsberg | 22d4666 | 2009-11-23 20:51:34 -0500 | [diff] [blame] | 2803 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2804 | for (i = 0; i < DRM_MAX_MINOR; i++) { |
| 2805 | snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i); |
| 2806 | if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) |
| 2807 | break; |
| 2808 | } |
| 2809 | if (i == DRM_MAX_MINOR) |
| 2810 | return NULL; |
Kristian Høgsberg | 22d4666 | 2009-11-23 20:51:34 -0500 | [diff] [blame] | 2811 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2812 | return strdup(name); |
Emmanuel Vadot | 6818a50 | 2020-01-21 17:50:15 +0100 | [diff] [blame] | 2813 | #endif |
Kristian Høgsberg | 22d4666 | 2009-11-23 20:51:34 -0500 | [diff] [blame] | 2814 | } |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2815 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 2816 | static bool drmNodeIsDRM(int maj, int min) |
| 2817 | { |
| 2818 | #ifdef __linux__ |
| 2819 | char path[64]; |
| 2820 | struct stat sbuf; |
| 2821 | |
| 2822 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm", |
| 2823 | maj, min); |
| 2824 | return stat(path, &sbuf) == 0; |
Emmanuel Vadot | 1c8d2b7 | 2020-01-21 17:42:44 +0100 | [diff] [blame] | 2825 | #elif __FreeBSD__ |
| 2826 | char name[SPECNAMELEN]; |
| 2827 | |
| 2828 | if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name))) |
| 2829 | return 0; |
| 2830 | /* Handle drm/ and dri/ as both are present in different FreeBSD version |
| 2831 | * FreeBSD on amd64/i386/powerpc external kernel modules create node in |
| 2832 | * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates |
| 2833 | * only device nodes in /dev/dri/ */ |
| 2834 | return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4)); |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 2835 | #else |
| 2836 | return maj == DRM_MAJOR; |
| 2837 | #endif |
| 2838 | } |
| 2839 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2840 | drm_public int drmGetNodeTypeFromFd(int fd) |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 2841 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2842 | struct stat sbuf; |
| 2843 | int maj, min, type; |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 2844 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2845 | if (fstat(fd, &sbuf)) |
| 2846 | return -1; |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 2847 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2848 | maj = major(sbuf.st_rdev); |
| 2849 | min = minor(sbuf.st_rdev); |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 2850 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 2851 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2852 | errno = EINVAL; |
| 2853 | return -1; |
| 2854 | } |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 2855 | |
Emmanuel Vadot | c55a1e5 | 2020-01-21 17:47:09 +0100 | [diff] [blame] | 2856 | type = drmGetMinorType(maj, min); |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2857 | if (type == -1) |
| 2858 | errno = ENODEV; |
| 2859 | return type; |
Frank Binns | 1f73578 | 2015-02-13 10:51:15 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2862 | drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, |
| 2863 | int *prime_fd) |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2864 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2865 | struct drm_prime_handle args; |
| 2866 | int ret; |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2867 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2868 | memclear(args); |
| 2869 | args.fd = -1; |
| 2870 | args.handle = handle; |
| 2871 | args.flags = flags; |
| 2872 | ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args); |
| 2873 | if (ret) |
| 2874 | return ret; |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2875 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2876 | *prime_fd = args.fd; |
| 2877 | return 0; |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 2880 | drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2881 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2882 | struct drm_prime_handle args; |
| 2883 | int ret; |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2884 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2885 | memclear(args); |
| 2886 | args.fd = prime_fd; |
| 2887 | ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args); |
| 2888 | if (ret) |
| 2889 | return ret; |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2890 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2891 | *handle = args.handle; |
| 2892 | return 0; |
Dave Airlie | cc0a145 | 2012-07-14 09:52:17 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2895 | static char *drmGetMinorNameForFD(int fd, int type) |
| 2896 | { |
| 2897 | #ifdef __linux__ |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2898 | DIR *sysdir; |
John Stultz | bb45ce4 | 2018-03-20 17:48:23 +0000 | [diff] [blame] | 2899 | struct dirent *ent; |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2900 | struct stat sbuf; |
| 2901 | const char *name = drmGetMinorName(type); |
| 2902 | int len; |
| 2903 | char dev_name[64], buf[64]; |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2904 | int maj, min; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2905 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2906 | if (!name) |
| 2907 | return NULL; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2908 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2909 | len = strlen(name); |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2910 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2911 | if (fstat(fd, &sbuf)) |
| 2912 | return NULL; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2913 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2914 | maj = major(sbuf.st_rdev); |
| 2915 | min = minor(sbuf.st_rdev); |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2916 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 2917 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2918 | return NULL; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2919 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2920 | snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min); |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2921 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2922 | sysdir = opendir(buf); |
| 2923 | if (!sysdir) |
| 2924 | return NULL; |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 2925 | |
John Stultz | bb45ce4 | 2018-03-20 17:48:23 +0000 | [diff] [blame] | 2926 | while ((ent = readdir(sysdir))) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2927 | if (strncmp(ent->d_name, name, len) == 0) { |
| 2928 | snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s", |
| 2929 | ent->d_name); |
Mathias Tillman | 5c42b5e | 2015-08-24 11:56:13 +0800 | [diff] [blame] | 2930 | |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2931 | closedir(sysdir); |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 2932 | return strdup(dev_name); |
| 2933 | } |
| 2934 | } |
Kevin Strasser | f34b694 | 2018-05-18 12:48:17 -0700 | [diff] [blame] | 2935 | |
| 2936 | closedir(sysdir); |
John Stultz | bb45ce4 | 2018-03-20 17:48:23 +0000 | [diff] [blame] | 2937 | return NULL; |
Emmanuel Vadot | 57c50cf | 2020-01-21 17:53:07 +0100 | [diff] [blame] | 2938 | #elif __FreeBSD__ |
| 2939 | struct stat sbuf; |
| 2940 | char dname[SPECNAMELEN]; |
| 2941 | const char *mname; |
| 2942 | char name[SPECNAMELEN]; |
Emmanuel Vadot | f52e2b2 | 2020-04-28 15:26:13 +0200 | [diff] [blame] | 2943 | int id, maj, min, nodetype, i; |
Emmanuel Vadot | 57c50cf | 2020-01-21 17:53:07 +0100 | [diff] [blame] | 2944 | |
| 2945 | if (fstat(fd, &sbuf)) |
| 2946 | return NULL; |
| 2947 | |
| 2948 | maj = major(sbuf.st_rdev); |
| 2949 | min = minor(sbuf.st_rdev); |
| 2950 | |
| 2951 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
| 2952 | return NULL; |
| 2953 | |
| 2954 | if (!devname_r(sbuf.st_rdev, S_IFCHR, dname, sizeof(dname))) |
| 2955 | return NULL; |
| 2956 | |
| 2957 | /* Handle both /dev/drm and /dev/dri |
| 2958 | * FreeBSD on amd64/i386/powerpc external kernel modules create node in |
| 2959 | * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates |
| 2960 | * only device nodes in /dev/dri/ */ |
Emmanuel Vadot | f52e2b2 | 2020-04-28 15:26:13 +0200 | [diff] [blame] | 2961 | |
| 2962 | /* Get the node type represented by fd so we can deduce the target name */ |
| 2963 | nodetype = drmGetMinorType(maj, min); |
| 2964 | if (nodetype == -1) |
| 2965 | return (NULL); |
Emmanuel Vadot | 57c50cf | 2020-01-21 17:53:07 +0100 | [diff] [blame] | 2966 | mname = drmGetMinorName(type); |
Emmanuel Vadot | f52e2b2 | 2020-04-28 15:26:13 +0200 | [diff] [blame] | 2967 | |
| 2968 | for (i = 0; i < SPECNAMELEN; i++) { |
| 2969 | if (isalpha(dname[i]) == 0 && dname[i] != '/') |
| 2970 | break; |
| 2971 | } |
| 2972 | if (dname[i] == '\0') |
| 2973 | return (NULL); |
| 2974 | |
| 2975 | id = (int)strtol(&dname[i], NULL, 10); |
| 2976 | id -= drmGetMinorBase(nodetype); |
| 2977 | snprintf(name, sizeof(name), DRM_DIR_NAME "/%s%d", mname, |
| 2978 | id + drmGetMinorBase(type)); |
| 2979 | |
Emmanuel Vadot | 57c50cf | 2020-01-21 17:53:07 +0100 | [diff] [blame] | 2980 | return strdup(name); |
Emil Velikov | 8415a00 | 2015-09-07 18:29:05 +0100 | [diff] [blame] | 2981 | #else |
Jonathan Gray | f189011 | 2016-12-01 15:18:39 +1100 | [diff] [blame] | 2982 | struct stat sbuf; |
| 2983 | char buf[PATH_MAX + 1]; |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 2984 | const char *dev_name = drmGetDeviceName(type); |
Jonathan Gray | f189011 | 2016-12-01 15:18:39 +1100 | [diff] [blame] | 2985 | unsigned int maj, min; |
Jonathan Gray | 293b95e | 2019-05-13 02:52:04 +1000 | [diff] [blame] | 2986 | int n; |
Jonathan Gray | f189011 | 2016-12-01 15:18:39 +1100 | [diff] [blame] | 2987 | |
| 2988 | if (fstat(fd, &sbuf)) |
| 2989 | return NULL; |
| 2990 | |
| 2991 | maj = major(sbuf.st_rdev); |
| 2992 | min = minor(sbuf.st_rdev); |
| 2993 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 2994 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
Jonathan Gray | f189011 | 2016-12-01 15:18:39 +1100 | [diff] [blame] | 2995 | return NULL; |
| 2996 | |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 2997 | if (!dev_name) |
Jonathan Gray | f189011 | 2016-12-01 15:18:39 +1100 | [diff] [blame] | 2998 | return NULL; |
Jonathan Gray | f189011 | 2016-12-01 15:18:39 +1100 | [diff] [blame] | 2999 | |
Jonathan Gray | 293b95e | 2019-05-13 02:52:04 +1000 | [diff] [blame] | 3000 | n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min); |
Jonathan Gray | f189011 | 2016-12-01 15:18:39 +1100 | [diff] [blame] | 3001 | if (n == -1 || n >= sizeof(buf)) |
| 3002 | return NULL; |
| 3003 | |
| 3004 | return strdup(buf); |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 3005 | #endif |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 3008 | drm_public char *drmGetPrimaryDeviceNameFromFd(int fd) |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 3009 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 3010 | return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY); |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 3013 | drm_public char *drmGetRenderDeviceNameFromFd(int fd) |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 3014 | { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 3015 | return drmGetMinorNameForFD(fd, DRM_NODE_RENDER); |
Emil Velikov | 0ca03a4 | 2015-03-07 00:58:39 +0000 | [diff] [blame] | 3016 | } |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3017 | |
Thierry Reding | f8484cc | 2016-12-22 00:39:36 +0100 | [diff] [blame] | 3018 | #ifdef __linux__ |
| 3019 | static char * DRM_PRINTFLIKE(2, 3) |
| 3020 | sysfs_uevent_get(const char *path, const char *fmt, ...) |
| 3021 | { |
| 3022 | char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL; |
| 3023 | size_t size = 0, len; |
| 3024 | ssize_t num; |
| 3025 | va_list ap; |
| 3026 | FILE *fp; |
| 3027 | |
| 3028 | va_start(ap, fmt); |
| 3029 | num = vasprintf(&key, fmt, ap); |
| 3030 | va_end(ap); |
| 3031 | len = num; |
| 3032 | |
| 3033 | snprintf(filename, sizeof(filename), "%s/uevent", path); |
| 3034 | |
| 3035 | fp = fopen(filename, "r"); |
| 3036 | if (!fp) { |
| 3037 | free(key); |
| 3038 | return NULL; |
| 3039 | } |
| 3040 | |
| 3041 | while ((num = getline(&line, &size, fp)) >= 0) { |
| 3042 | if ((strncmp(line, key, len) == 0) && (line[len] == '=')) { |
| 3043 | char *start = line + len + 1, *end = line + num - 1; |
| 3044 | |
| 3045 | if (*end != '\n') |
| 3046 | end++; |
| 3047 | |
| 3048 | value = strndup(start, end - start); |
| 3049 | break; |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | free(line); |
| 3054 | fclose(fp); |
| 3055 | |
| 3056 | free(key); |
| 3057 | |
| 3058 | return value; |
| 3059 | } |
| 3060 | #endif |
| 3061 | |
Emil Velikov | 3988580 | 2018-05-15 17:37:49 +0100 | [diff] [blame] | 3062 | /* Little white lie to avoid major rework of the existing code */ |
| 3063 | #define DRM_BUS_VIRTIO 0x10 |
| 3064 | |
Emil Velikov | 291b2bb | 2015-09-07 18:26:34 +0100 | [diff] [blame] | 3065 | #ifdef __linux__ |
Vasyl Vavrychuk | c4eae71 | 2020-01-29 03:54:56 +0100 | [diff] [blame] | 3066 | static int get_subsystem_type(const char *device_path) |
| 3067 | { |
| 3068 | char path[PATH_MAX + 1] = ""; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3069 | char link[PATH_MAX + 1] = ""; |
| 3070 | char *name; |
Eric Anholt | 9b28c5a | 2018-11-15 17:48:53 -0800 | [diff] [blame] | 3071 | struct { |
| 3072 | const char *name; |
| 3073 | int bus_type; |
| 3074 | } bus_types[] = { |
| 3075 | { "/pci", DRM_BUS_PCI }, |
| 3076 | { "/usb", DRM_BUS_USB }, |
| 3077 | { "/platform", DRM_BUS_PLATFORM }, |
Eric Anholt | 89700ab | 2018-11-15 17:52:19 -0800 | [diff] [blame] | 3078 | { "/spi", DRM_BUS_PLATFORM }, |
Eric Anholt | 9b28c5a | 2018-11-15 17:48:53 -0800 | [diff] [blame] | 3079 | { "/host1x", DRM_BUS_HOST1X }, |
| 3080 | { "/virtio", DRM_BUS_VIRTIO }, |
| 3081 | }; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3082 | |
Vasyl Vavrychuk | c4eae71 | 2020-01-29 03:54:56 +0100 | [diff] [blame] | 3083 | strncpy(path, device_path, PATH_MAX); |
| 3084 | strncat(path, "/subsystem", PATH_MAX); |
Emil Velikov | a250fce | 2015-09-07 12:54:27 +0100 | [diff] [blame] | 3085 | |
| 3086 | if (readlink(path, link, PATH_MAX) < 0) |
| 3087 | return -errno; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3088 | |
| 3089 | name = strrchr(link, '/'); |
| 3090 | if (!name) |
| 3091 | return -EINVAL; |
| 3092 | |
Eric Anholt | 9b28c5a | 2018-11-15 17:48:53 -0800 | [diff] [blame] | 3093 | for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) { |
| 3094 | if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0) |
| 3095 | return bus_types[i].bus_type; |
| 3096 | } |
Emil Velikov | 3988580 | 2018-05-15 17:37:49 +0100 | [diff] [blame] | 3097 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3098 | return -EINVAL; |
Vasyl Vavrychuk | c4eae71 | 2020-01-29 03:54:56 +0100 | [diff] [blame] | 3099 | } |
| 3100 | #endif |
| 3101 | |
| 3102 | static int drmParseSubsystemType(int maj, int min) |
| 3103 | { |
| 3104 | #ifdef __linux__ |
| 3105 | char path[PATH_MAX + 1] = ""; |
Vasyl Vavrychuk | 8a73372 | 2020-01-29 16:08:41 +0100 | [diff] [blame] | 3106 | char real_path[PATH_MAX + 1] = ""; |
| 3107 | int subsystem_type; |
Vasyl Vavrychuk | c4eae71 | 2020-01-29 03:54:56 +0100 | [diff] [blame] | 3108 | |
Vasyl Vavrychuk | 8a73372 | 2020-01-29 16:08:41 +0100 | [diff] [blame] | 3109 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); |
Vasyl Vavrychuk | c4eae71 | 2020-01-29 03:54:56 +0100 | [diff] [blame] | 3110 | |
Vasyl Vavrychuk | 8a73372 | 2020-01-29 16:08:41 +0100 | [diff] [blame] | 3111 | subsystem_type = get_subsystem_type(path); |
Mikhail Golubev | 57df075 | 2020-04-20 20:08:50 +0200 | [diff] [blame] | 3112 | /* Try to get the parent (underlying) device type */ |
Vasyl Vavrychuk | 8a73372 | 2020-01-29 16:08:41 +0100 | [diff] [blame] | 3113 | if (subsystem_type == DRM_BUS_VIRTIO) { |
Mikhail Golubev | 57df075 | 2020-04-20 20:08:50 +0200 | [diff] [blame] | 3114 | /* Assume virtio-pci on error */ |
| 3115 | if (!realpath(path, real_path)) |
| 3116 | return DRM_BUS_VIRTIO; |
Vasyl Vavrychuk | 8a73372 | 2020-01-29 16:08:41 +0100 | [diff] [blame] | 3117 | strncat(path, "/..", PATH_MAX); |
| 3118 | subsystem_type = get_subsystem_type(path); |
Mikhail Golubev | 57df075 | 2020-04-20 20:08:50 +0200 | [diff] [blame] | 3119 | if (subsystem_type < 0) |
| 3120 | return DRM_BUS_VIRTIO; |
| 3121 | } |
Vasyl Vavrychuk | 8a73372 | 2020-01-29 16:08:41 +0100 | [diff] [blame] | 3122 | return subsystem_type; |
Emmanuel Vadot | 41f3a7b | 2020-01-21 17:56:49 +0100 | [diff] [blame] | 3123 | #elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__) |
Thierry Reding | e17cad1 | 2016-12-21 18:00:52 +0100 | [diff] [blame] | 3124 | return DRM_BUS_PCI; |
Emil Velikov | 291b2bb | 2015-09-07 18:26:34 +0100 | [diff] [blame] | 3125 | #else |
| 3126 | #warning "Missing implementation of drmParseSubsystemType" |
| 3127 | return -EINVAL; |
| 3128 | #endif |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3129 | } |
| 3130 | |
Emmanuel Vadot | 44bcf9c | 2020-04-20 22:44:02 +0200 | [diff] [blame] | 3131 | #ifdef __linux__ |
Eric Engestrom | 5649953 | 2018-09-05 13:23:59 +0100 | [diff] [blame] | 3132 | static void |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3133 | get_pci_path(int maj, int min, char *pci_path) |
Emil Velikov | a029001 | 2018-05-15 17:29:44 +0100 | [diff] [blame] | 3134 | { |
Emil Velikov | 3988580 | 2018-05-15 17:37:49 +0100 | [diff] [blame] | 3135 | char path[PATH_MAX + 1], *term; |
Emil Velikov | a029001 | 2018-05-15 17:29:44 +0100 | [diff] [blame] | 3136 | |
| 3137 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3138 | if (!realpath(path, pci_path)) { |
| 3139 | strcpy(pci_path, path); |
Eric Engestrom | 5649953 | 2018-09-05 13:23:59 +0100 | [diff] [blame] | 3140 | return; |
| 3141 | } |
Emil Velikov | a029001 | 2018-05-15 17:29:44 +0100 | [diff] [blame] | 3142 | |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3143 | term = strrchr(pci_path, '/'); |
Emil Velikov | 3988580 | 2018-05-15 17:37:49 +0100 | [diff] [blame] | 3144 | if (term && strncmp(term, "/virtio", 7) == 0) |
| 3145 | *term = 0; |
Emil Velikov | bcb9d97 | 2018-08-23 10:49:54 +0100 | [diff] [blame] | 3146 | } |
Emmanuel Vadot | 44bcf9c | 2020-04-20 22:44:02 +0200 | [diff] [blame] | 3147 | #endif |
Emil Velikov | bcb9d97 | 2018-08-23 10:49:54 +0100 | [diff] [blame] | 3148 | |
Emmanuel Vadot | 3791137 | 2020-01-21 18:49:11 +0100 | [diff] [blame] | 3149 | #ifdef __FreeBSD__ |
| 3150 | static int get_sysctl_pci_bus_info(int maj, int min, drmPciBusInfoPtr info) |
| 3151 | { |
| 3152 | char dname[SPECNAMELEN]; |
| 3153 | char sysctl_name[16]; |
| 3154 | char sysctl_val[256]; |
| 3155 | size_t sysctl_len; |
| 3156 | int id, type, nelem; |
| 3157 | unsigned int rdev, majmin, domain, bus, dev, func; |
| 3158 | |
| 3159 | rdev = makedev(maj, min); |
| 3160 | if (!devname_r(rdev, S_IFCHR, dname, sizeof(dname))) |
| 3161 | return -EINVAL; |
| 3162 | |
| 3163 | if (sscanf(dname, "drm/%d\n", &id) != 1) |
| 3164 | return -EINVAL; |
| 3165 | type = drmGetMinorType(maj, min); |
| 3166 | if (type == -1) |
| 3167 | return -EINVAL; |
| 3168 | |
| 3169 | /* BUG: This above section is iffy, since it mandates that a driver will |
| 3170 | * create both card and render node. |
| 3171 | * If it does not, the next DRM device will create card#X and |
| 3172 | * renderD#(128+X)-1. |
| 3173 | * This is a possibility in FreeBSD but for now there is no good way for |
| 3174 | * obtaining the info. |
| 3175 | */ |
| 3176 | switch (type) { |
| 3177 | case DRM_NODE_PRIMARY: |
| 3178 | break; |
| 3179 | case DRM_NODE_CONTROL: |
| 3180 | id -= 64; |
| 3181 | break; |
| 3182 | case DRM_NODE_RENDER: |
| 3183 | id -= 128; |
| 3184 | break; |
| 3185 | } |
| 3186 | if (id < 0) |
| 3187 | return -EINVAL; |
| 3188 | |
| 3189 | if (snprintf(sysctl_name, sizeof(sysctl_name), "hw.dri.%d.busid", id) <= 0) |
| 3190 | return -EINVAL; |
| 3191 | sysctl_len = sizeof(sysctl_val); |
| 3192 | if (sysctlbyname(sysctl_name, sysctl_val, &sysctl_len, NULL, 0)) |
| 3193 | return -EINVAL; |
| 3194 | |
| 3195 | #define bus_fmt "pci:%04x:%02x:%02x.%u" |
| 3196 | |
| 3197 | nelem = sscanf(sysctl_val, bus_fmt, &domain, &bus, &dev, &func); |
| 3198 | if (nelem != 4) |
| 3199 | return -EINVAL; |
| 3200 | info->domain = domain; |
| 3201 | info->bus = bus; |
| 3202 | info->dev = dev; |
| 3203 | info->func = func; |
| 3204 | |
| 3205 | return 0; |
| 3206 | } |
| 3207 | #endif |
| 3208 | |
Emil Velikov | 536e0de | 2015-09-07 12:37:57 +0100 | [diff] [blame] | 3209 | static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3210 | { |
Emil Velikov | 291b2bb | 2015-09-07 18:26:34 +0100 | [diff] [blame] | 3211 | #ifdef __linux__ |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 3212 | unsigned int domain, bus, dev, func; |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3213 | char pci_path[PATH_MAX + 1], *value; |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 3214 | int num; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3215 | |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3216 | get_pci_path(maj, min, pci_path); |
Emil Velikov | 536e0de | 2015-09-07 12:37:57 +0100 | [diff] [blame] | 3217 | |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3218 | value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME"); |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 3219 | if (!value) |
| 3220 | return -ENOENT; |
Emil Velikov | 536e0de | 2015-09-07 12:37:57 +0100 | [diff] [blame] | 3221 | |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 3222 | num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func); |
| 3223 | free(value); |
| 3224 | |
| 3225 | if (num != 4) |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3226 | return -EINVAL; |
| 3227 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3228 | info->domain = domain; |
| 3229 | info->bus = bus; |
| 3230 | info->dev = dev; |
| 3231 | info->func = func; |
| 3232 | |
| 3233 | return 0; |
François Tigeot | 8f2e092 | 2018-12-12 20:48:36 +0100 | [diff] [blame] | 3234 | #elif defined(__OpenBSD__) || defined(__DragonFly__) |
Jonathan Gray | fd19056 | 2016-12-01 15:18:42 +1100 | [diff] [blame] | 3235 | struct drm_pciinfo pinfo; |
| 3236 | int fd, type; |
| 3237 | |
Emmanuel Vadot | c55a1e5 | 2020-01-21 17:47:09 +0100 | [diff] [blame] | 3238 | type = drmGetMinorType(maj, min); |
Jonathan Gray | fd19056 | 2016-12-01 15:18:42 +1100 | [diff] [blame] | 3239 | if (type == -1) |
| 3240 | return -ENODEV; |
| 3241 | |
| 3242 | fd = drmOpenMinor(min, 0, type); |
| 3243 | if (fd < 0) |
| 3244 | return -errno; |
| 3245 | |
| 3246 | if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) { |
| 3247 | close(fd); |
| 3248 | return -errno; |
| 3249 | } |
| 3250 | close(fd); |
| 3251 | |
| 3252 | info->domain = pinfo.domain; |
| 3253 | info->bus = pinfo.bus; |
| 3254 | info->dev = pinfo.dev; |
| 3255 | info->func = pinfo.func; |
| 3256 | |
| 3257 | return 0; |
Emmanuel Vadot | 3791137 | 2020-01-21 18:49:11 +0100 | [diff] [blame] | 3258 | #elif __FreeBSD__ |
| 3259 | return get_sysctl_pci_bus_info(maj, min, info); |
Emil Velikov | 291b2bb | 2015-09-07 18:26:34 +0100 | [diff] [blame] | 3260 | #else |
| 3261 | #warning "Missing implementation of drmParsePciBusInfo" |
| 3262 | return -EINVAL; |
| 3263 | #endif |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3264 | } |
| 3265 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 3266 | drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b) |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3267 | { |
Emil Velikov | bc2aca9 | 2015-09-07 13:51:54 +0100 | [diff] [blame] | 3268 | if (a == NULL || b == NULL) |
Adam Jackson | 7c27cd7 | 2017-05-04 10:48:56 -0400 | [diff] [blame] | 3269 | return 0; |
Emil Velikov | bc2aca9 | 2015-09-07 13:51:54 +0100 | [diff] [blame] | 3270 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3271 | if (a->bustype != b->bustype) |
Adam Jackson | 7c27cd7 | 2017-05-04 10:48:56 -0400 | [diff] [blame] | 3272 | return 0; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3273 | |
| 3274 | switch (a->bustype) { |
| 3275 | case DRM_BUS_PCI: |
Adam Jackson | 7c27cd7 | 2017-05-04 10:48:56 -0400 | [diff] [blame] | 3276 | return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0; |
Thierry Reding | f8484cc | 2016-12-22 00:39:36 +0100 | [diff] [blame] | 3277 | |
| 3278 | case DRM_BUS_USB: |
Adam Jackson | 7c27cd7 | 2017-05-04 10:48:56 -0400 | [diff] [blame] | 3279 | return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0; |
Thierry Reding | f8484cc | 2016-12-22 00:39:36 +0100 | [diff] [blame] | 3280 | |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3281 | case DRM_BUS_PLATFORM: |
Adam Jackson | 7c27cd7 | 2017-05-04 10:48:56 -0400 | [diff] [blame] | 3282 | return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3283 | |
| 3284 | case DRM_BUS_HOST1X: |
Adam Jackson | 7c27cd7 | 2017-05-04 10:48:56 -0400 | [diff] [blame] | 3285 | return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3286 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3287 | default: |
| 3288 | break; |
| 3289 | } |
| 3290 | |
Adam Jackson | 7c27cd7 | 2017-05-04 10:48:56 -0400 | [diff] [blame] | 3291 | return 0; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | static int drmGetNodeType(const char *name) |
| 3295 | { |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3296 | if (strncmp(name, DRM_CONTROL_MINOR_NAME, |
| 3297 | sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0) |
| 3298 | return DRM_NODE_CONTROL; |
| 3299 | |
| 3300 | if (strncmp(name, DRM_RENDER_MINOR_NAME, |
| 3301 | sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0) |
| 3302 | return DRM_NODE_RENDER; |
| 3303 | |
Jonathan Gray | 13e2c35 | 2019-05-13 02:50:49 +1000 | [diff] [blame] | 3304 | if (strncmp(name, DRM_PRIMARY_MINOR_NAME, |
| 3305 | sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0) |
| 3306 | return DRM_NODE_PRIMARY; |
| 3307 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3308 | return -EINVAL; |
| 3309 | } |
| 3310 | |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 3311 | static int drmGetMaxNodeName(void) |
| 3312 | { |
| 3313 | return sizeof(DRM_DIR_NAME) + |
| 3314 | MAX3(sizeof(DRM_PRIMARY_MINOR_NAME), |
| 3315 | sizeof(DRM_CONTROL_MINOR_NAME), |
| 3316 | sizeof(DRM_RENDER_MINOR_NAME)) + |
Eric Engestrom | ce97507 | 2016-04-03 19:48:12 +0100 | [diff] [blame] | 3317 | 3 /* length of the node number */; |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 3318 | } |
| 3319 | |
Emil Velikov | 291b2bb | 2015-09-07 18:26:34 +0100 | [diff] [blame] | 3320 | #ifdef __linux__ |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3321 | static int parse_separate_sysfs_files(int maj, int min, |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 3322 | drmPciDeviceInfoPtr device, |
| 3323 | bool ignore_revision) |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3324 | { |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3325 | static const char *attrs[] = { |
| 3326 | "revision", /* Older kernels are missing the file, so check for it first */ |
| 3327 | "vendor", |
| 3328 | "device", |
| 3329 | "subsystem_vendor", |
| 3330 | "subsystem_device", |
| 3331 | }; |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3332 | char path[PATH_MAX + 1], pci_path[PATH_MAX + 1]; |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3333 | unsigned int data[ARRAY_SIZE(attrs)]; |
| 3334 | FILE *fp; |
| 3335 | int ret; |
| 3336 | |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3337 | get_pci_path(maj, min, pci_path); |
Emil Velikov | a029001 | 2018-05-15 17:29:44 +0100 | [diff] [blame] | 3338 | |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 3339 | for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) { |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3340 | snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]); |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3341 | fp = fopen(path, "r"); |
| 3342 | if (!fp) |
| 3343 | return -errno; |
| 3344 | |
| 3345 | ret = fscanf(fp, "%x", &data[i]); |
| 3346 | fclose(fp); |
| 3347 | if (ret != 1) |
| 3348 | return -errno; |
| 3349 | |
| 3350 | } |
| 3351 | |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 3352 | device->revision_id = ignore_revision ? 0xff : data[0] & 0xff; |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3353 | device->vendor_id = data[1] & 0xffff; |
| 3354 | device->device_id = data[2] & 0xffff; |
| 3355 | device->subvendor_id = data[3] & 0xffff; |
| 3356 | device->subdevice_id = data[4] & 0xffff; |
| 3357 | |
| 3358 | return 0; |
| 3359 | } |
| 3360 | |
| 3361 | static int parse_config_sysfs_file(int maj, int min, |
| 3362 | drmPciDeviceInfoPtr device) |
| 3363 | { |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3364 | char path[PATH_MAX + 1], pci_path[PATH_MAX + 1]; |
Emil Velikov | ef5192e | 2015-09-07 12:47:47 +0100 | [diff] [blame] | 3365 | unsigned char config[64]; |
| 3366 | int fd, ret; |
| 3367 | |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3368 | get_pci_path(maj, min, pci_path); |
Emil Velikov | a029001 | 2018-05-15 17:29:44 +0100 | [diff] [blame] | 3369 | |
Eric Engestrom | 9030a0f | 2018-09-05 13:29:08 +0100 | [diff] [blame] | 3370 | snprintf(path, PATH_MAX, "%s/config", pci_path); |
Emil Velikov | ef5192e | 2015-09-07 12:47:47 +0100 | [diff] [blame] | 3371 | fd = open(path, O_RDONLY); |
| 3372 | if (fd < 0) |
| 3373 | return -errno; |
| 3374 | |
| 3375 | ret = read(fd, config, sizeof(config)); |
| 3376 | close(fd); |
| 3377 | if (ret < 0) |
| 3378 | return -errno; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3379 | |
| 3380 | device->vendor_id = config[0] | (config[1] << 8); |
| 3381 | device->device_id = config[2] | (config[3] << 8); |
| 3382 | device->revision_id = config[8]; |
| 3383 | device->subvendor_id = config[44] | (config[45] << 8); |
| 3384 | device->subdevice_id = config[46] | (config[47] << 8); |
| 3385 | |
| 3386 | return 0; |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3387 | } |
| 3388 | #endif |
| 3389 | |
| 3390 | static int drmParsePciDeviceInfo(int maj, int min, |
| 3391 | drmPciDeviceInfoPtr device, |
| 3392 | uint32_t flags) |
| 3393 | { |
| 3394 | #ifdef __linux__ |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 3395 | if (!(flags & DRM_DEVICE_GET_PCI_REVISION)) |
| 3396 | return parse_separate_sysfs_files(maj, min, device, true); |
| 3397 | |
| 3398 | if (parse_separate_sysfs_files(maj, min, device, false)) |
Emil Velikov | aae3f31 | 2016-11-01 18:04:06 +0000 | [diff] [blame] | 3399 | return parse_config_sysfs_file(maj, min, device); |
| 3400 | |
| 3401 | return 0; |
François Tigeot | 8f2e092 | 2018-12-12 20:48:36 +0100 | [diff] [blame] | 3402 | #elif defined(__OpenBSD__) || defined(__DragonFly__) |
Jonathan Gray | c0ef1d0 | 2016-12-01 15:18:41 +1100 | [diff] [blame] | 3403 | struct drm_pciinfo pinfo; |
| 3404 | int fd, type; |
| 3405 | |
Emmanuel Vadot | c55a1e5 | 2020-01-21 17:47:09 +0100 | [diff] [blame] | 3406 | type = drmGetMinorType(maj, min); |
Jonathan Gray | c0ef1d0 | 2016-12-01 15:18:41 +1100 | [diff] [blame] | 3407 | if (type == -1) |
| 3408 | return -ENODEV; |
| 3409 | |
| 3410 | fd = drmOpenMinor(min, 0, type); |
| 3411 | if (fd < 0) |
| 3412 | return -errno; |
| 3413 | |
| 3414 | if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) { |
| 3415 | close(fd); |
| 3416 | return -errno; |
| 3417 | } |
| 3418 | close(fd); |
| 3419 | |
| 3420 | device->vendor_id = pinfo.vendor_id; |
| 3421 | device->device_id = pinfo.device_id; |
| 3422 | device->revision_id = pinfo.revision_id; |
| 3423 | device->subvendor_id = pinfo.subvendor_id; |
| 3424 | device->subdevice_id = pinfo.subdevice_id; |
| 3425 | |
| 3426 | return 0; |
Emmanuel Vadot | e321dd7 | 2020-01-21 18:49:47 +0100 | [diff] [blame] | 3427 | #elif __FreeBSD__ |
| 3428 | drmPciBusInfo info; |
| 3429 | struct pci_conf_io pc; |
| 3430 | struct pci_match_conf patterns[1]; |
| 3431 | struct pci_conf results[1]; |
| 3432 | int fd, error; |
| 3433 | |
| 3434 | if (get_sysctl_pci_bus_info(maj, min, &info) != 0) |
| 3435 | return -EINVAL; |
| 3436 | |
| 3437 | fd = open("/dev/pci", O_RDONLY, 0); |
| 3438 | if (fd < 0) |
| 3439 | return -errno; |
| 3440 | |
| 3441 | bzero(&patterns, sizeof(patterns)); |
| 3442 | patterns[0].pc_sel.pc_domain = info.domain; |
| 3443 | patterns[0].pc_sel.pc_bus = info.bus; |
| 3444 | patterns[0].pc_sel.pc_dev = info.dev; |
| 3445 | patterns[0].pc_sel.pc_func = info.func; |
| 3446 | patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS |
| 3447 | | PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC; |
| 3448 | bzero(&pc, sizeof(struct pci_conf_io)); |
| 3449 | pc.num_patterns = 1; |
| 3450 | pc.pat_buf_len = sizeof(patterns); |
| 3451 | pc.patterns = patterns; |
| 3452 | pc.match_buf_len = sizeof(results); |
| 3453 | pc.matches = results; |
| 3454 | |
| 3455 | if (ioctl(fd, PCIOCGETCONF, &pc) || pc.status == PCI_GETCONF_ERROR) { |
| 3456 | error = errno; |
| 3457 | close(fd); |
| 3458 | return -error; |
| 3459 | } |
| 3460 | close(fd); |
| 3461 | |
| 3462 | device->vendor_id = results[0].pc_vendor; |
| 3463 | device->device_id = results[0].pc_device; |
| 3464 | device->subvendor_id = results[0].pc_subvendor; |
| 3465 | device->subdevice_id = results[0].pc_subdevice; |
| 3466 | device->revision_id = results[0].pc_revid; |
| 3467 | |
| 3468 | return 0; |
Emil Velikov | 291b2bb | 2015-09-07 18:26:34 +0100 | [diff] [blame] | 3469 | #else |
| 3470 | #warning "Missing implementation of drmParsePciDeviceInfo" |
| 3471 | return -EINVAL; |
| 3472 | #endif |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3473 | } |
| 3474 | |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3475 | static void drmFreePlatformDevice(drmDevicePtr device) |
| 3476 | { |
| 3477 | if (device->deviceinfo.platform) { |
| 3478 | if (device->deviceinfo.platform->compatible) { |
| 3479 | char **compatible = device->deviceinfo.platform->compatible; |
| 3480 | |
| 3481 | while (*compatible) { |
| 3482 | free(*compatible); |
| 3483 | compatible++; |
| 3484 | } |
| 3485 | |
| 3486 | free(device->deviceinfo.platform->compatible); |
| 3487 | } |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | static void drmFreeHost1xDevice(drmDevicePtr device) |
| 3492 | { |
| 3493 | if (device->deviceinfo.host1x) { |
| 3494 | if (device->deviceinfo.host1x->compatible) { |
| 3495 | char **compatible = device->deviceinfo.host1x->compatible; |
| 3496 | |
| 3497 | while (*compatible) { |
| 3498 | free(*compatible); |
| 3499 | compatible++; |
| 3500 | } |
| 3501 | |
| 3502 | free(device->deviceinfo.host1x->compatible); |
| 3503 | } |
| 3504 | } |
| 3505 | } |
| 3506 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 3507 | drm_public void drmFreeDevice(drmDevicePtr *device) |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3508 | { |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3509 | if (device == NULL) |
| 3510 | return; |
| 3511 | |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3512 | if (*device) { |
| 3513 | switch ((*device)->bustype) { |
| 3514 | case DRM_BUS_PLATFORM: |
| 3515 | drmFreePlatformDevice(*device); |
| 3516 | break; |
| 3517 | |
| 3518 | case DRM_BUS_HOST1X: |
| 3519 | drmFreeHost1xDevice(*device); |
| 3520 | break; |
| 3521 | } |
| 3522 | } |
| 3523 | |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 3524 | free(*device); |
| 3525 | *device = NULL; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3526 | } |
| 3527 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 3528 | drm_public void drmFreeDevices(drmDevicePtr devices[], int count) |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3529 | { |
| 3530 | int i; |
| 3531 | |
| 3532 | if (devices == NULL) |
| 3533 | return; |
| 3534 | |
Qiang Yu | 6c056ee | 2016-07-14 17:10:56 +0800 | [diff] [blame] | 3535 | for (i = 0; i < count; i++) |
| 3536 | if (devices[i]) |
| 3537 | drmFreeDevice(&devices[i]); |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 3538 | } |
| 3539 | |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3540 | static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node, |
| 3541 | size_t bus_size, size_t device_size, |
| 3542 | char **ptrp) |
| 3543 | { |
| 3544 | size_t max_node_length, extra, size; |
| 3545 | drmDevicePtr device; |
| 3546 | unsigned int i; |
| 3547 | char *ptr; |
| 3548 | |
| 3549 | max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *)); |
| 3550 | extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length); |
| 3551 | |
| 3552 | size = sizeof(*device) + extra + bus_size + device_size; |
| 3553 | |
| 3554 | device = calloc(1, size); |
| 3555 | if (!device) |
| 3556 | return NULL; |
| 3557 | |
| 3558 | device->available_nodes = 1 << type; |
| 3559 | |
| 3560 | ptr = (char *)device + sizeof(*device); |
| 3561 | device->nodes = (char **)ptr; |
| 3562 | |
| 3563 | ptr += DRM_NODE_MAX * sizeof(void *); |
| 3564 | |
| 3565 | for (i = 0; i < DRM_NODE_MAX; i++) { |
| 3566 | device->nodes[i] = ptr; |
| 3567 | ptr += max_node_length; |
| 3568 | } |
| 3569 | |
| 3570 | memcpy(device->nodes[type], node, max_node_length); |
| 3571 | |
| 3572 | *ptrp = ptr; |
| 3573 | |
| 3574 | return device; |
| 3575 | } |
| 3576 | |
Emil Velikov | b40a65d | 2016-11-30 16:41:27 +0000 | [diff] [blame] | 3577 | static int drmProcessPciDevice(drmDevicePtr *device, |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3578 | const char *node, int node_type, |
Emil Velikov | 138d231 | 2016-11-30 17:13:51 +0000 | [diff] [blame] | 3579 | int maj, int min, bool fetch_deviceinfo, |
| 3580 | uint32_t flags) |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3581 | { |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3582 | drmDevicePtr dev; |
Michel Dänzer | 3045523 | 2015-10-14 12:48:52 +0900 | [diff] [blame] | 3583 | char *addr; |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3584 | int ret; |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3585 | |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3586 | dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo), |
| 3587 | sizeof(drmPciDeviceInfo), &addr); |
| 3588 | if (!dev) |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3589 | return -ENOMEM; |
| 3590 | |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3591 | dev->bustype = DRM_BUS_PCI; |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 3592 | |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3593 | dev->businfo.pci = (drmPciBusInfoPtr)addr; |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3594 | |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3595 | ret = drmParsePciBusInfo(maj, min, dev->businfo.pci); |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3596 | if (ret) |
| 3597 | goto free_device; |
| 3598 | |
| 3599 | // Fetch the device info if the user has requested it |
| 3600 | if (fetch_deviceinfo) { |
| 3601 | addr += sizeof(drmPciBusInfo); |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3602 | dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr; |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3603 | |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3604 | ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags); |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3605 | if (ret) |
| 3606 | goto free_device; |
| 3607 | } |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3608 | |
| 3609 | *device = dev; |
| 3610 | |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3611 | return 0; |
| 3612 | |
| 3613 | free_device: |
Thierry Reding | 2e57bba | 2016-12-21 17:54:48 +0100 | [diff] [blame] | 3614 | free(dev); |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3615 | return ret; |
| 3616 | } |
| 3617 | |
Scott Anderson | bf63f8a | 2020-01-31 16:18:19 +1300 | [diff] [blame] | 3618 | #ifdef __linux__ |
| 3619 | static int drm_usb_dev_path(int maj, int min, char *path, size_t len) |
| 3620 | { |
| 3621 | char *value, *tmp_path, *slash; |
| 3622 | |
| 3623 | snprintf(path, len, "/sys/dev/char/%d:%d/device", maj, min); |
| 3624 | |
| 3625 | value = sysfs_uevent_get(path, "DEVTYPE"); |
| 3626 | if (!value) |
| 3627 | return -ENOENT; |
| 3628 | |
| 3629 | if (strcmp(value, "usb_device") == 0) |
| 3630 | return 0; |
| 3631 | if (strcmp(value, "usb_interface") != 0) |
| 3632 | return -ENOTSUP; |
| 3633 | |
| 3634 | /* The parent of a usb_interface is a usb_device */ |
| 3635 | |
| 3636 | tmp_path = realpath(path, NULL); |
| 3637 | if (!tmp_path) |
| 3638 | return -errno; |
| 3639 | |
| 3640 | slash = strrchr(tmp_path, '/'); |
| 3641 | if (!slash) { |
| 3642 | free(tmp_path); |
| 3643 | return -EINVAL; |
| 3644 | } |
| 3645 | |
| 3646 | *slash = '\0'; |
| 3647 | |
| 3648 | if (snprintf(path, len, "%s", tmp_path) >= (int)len) { |
| 3649 | free(tmp_path); |
| 3650 | return -EINVAL; |
| 3651 | } |
| 3652 | |
| 3653 | free(tmp_path); |
| 3654 | return 0; |
| 3655 | } |
| 3656 | #endif |
| 3657 | |
Thierry Reding | f8484cc | 2016-12-22 00:39:36 +0100 | [diff] [blame] | 3658 | static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info) |
| 3659 | { |
| 3660 | #ifdef __linux__ |
| 3661 | char path[PATH_MAX + 1], *value; |
| 3662 | unsigned int bus, dev; |
| 3663 | int ret; |
| 3664 | |
Scott Anderson | bf63f8a | 2020-01-31 16:18:19 +1300 | [diff] [blame] | 3665 | ret = drm_usb_dev_path(maj, min, path, sizeof(path)); |
| 3666 | if (ret < 0) |
| 3667 | return ret; |
Thierry Reding | f8484cc | 2016-12-22 00:39:36 +0100 | [diff] [blame] | 3668 | |
| 3669 | value = sysfs_uevent_get(path, "BUSNUM"); |
| 3670 | if (!value) |
| 3671 | return -ENOENT; |
| 3672 | |
| 3673 | ret = sscanf(value, "%03u", &bus); |
| 3674 | free(value); |
| 3675 | |
| 3676 | if (ret <= 0) |
| 3677 | return -errno; |
| 3678 | |
| 3679 | value = sysfs_uevent_get(path, "DEVNUM"); |
| 3680 | if (!value) |
| 3681 | return -ENOENT; |
| 3682 | |
| 3683 | ret = sscanf(value, "%03u", &dev); |
| 3684 | free(value); |
| 3685 | |
| 3686 | if (ret <= 0) |
| 3687 | return -errno; |
| 3688 | |
| 3689 | info->bus = bus; |
| 3690 | info->dev = dev; |
| 3691 | |
| 3692 | return 0; |
| 3693 | #else |
| 3694 | #warning "Missing implementation of drmParseUsbBusInfo" |
| 3695 | return -EINVAL; |
| 3696 | #endif |
| 3697 | } |
| 3698 | |
| 3699 | static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info) |
| 3700 | { |
| 3701 | #ifdef __linux__ |
| 3702 | char path[PATH_MAX + 1], *value; |
| 3703 | unsigned int vendor, product; |
| 3704 | int ret; |
| 3705 | |
Scott Anderson | bf63f8a | 2020-01-31 16:18:19 +1300 | [diff] [blame] | 3706 | ret = drm_usb_dev_path(maj, min, path, sizeof(path)); |
| 3707 | if (ret < 0) |
| 3708 | return ret; |
Thierry Reding | f8484cc | 2016-12-22 00:39:36 +0100 | [diff] [blame] | 3709 | |
| 3710 | value = sysfs_uevent_get(path, "PRODUCT"); |
| 3711 | if (!value) |
| 3712 | return -ENOENT; |
| 3713 | |
| 3714 | ret = sscanf(value, "%x/%x", &vendor, &product); |
| 3715 | free(value); |
| 3716 | |
| 3717 | if (ret <= 0) |
| 3718 | return -errno; |
| 3719 | |
| 3720 | info->vendor = vendor; |
| 3721 | info->product = product; |
| 3722 | |
| 3723 | return 0; |
| 3724 | #else |
| 3725 | #warning "Missing implementation of drmParseUsbDeviceInfo" |
| 3726 | return -EINVAL; |
| 3727 | #endif |
| 3728 | } |
| 3729 | |
| 3730 | static int drmProcessUsbDevice(drmDevicePtr *device, const char *node, |
| 3731 | int node_type, int maj, int min, |
| 3732 | bool fetch_deviceinfo, uint32_t flags) |
| 3733 | { |
| 3734 | drmDevicePtr dev; |
| 3735 | char *ptr; |
| 3736 | int ret; |
| 3737 | |
| 3738 | dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo), |
| 3739 | sizeof(drmUsbDeviceInfo), &ptr); |
| 3740 | if (!dev) |
| 3741 | return -ENOMEM; |
| 3742 | |
| 3743 | dev->bustype = DRM_BUS_USB; |
| 3744 | |
| 3745 | dev->businfo.usb = (drmUsbBusInfoPtr)ptr; |
| 3746 | |
| 3747 | ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb); |
| 3748 | if (ret < 0) |
| 3749 | goto free_device; |
| 3750 | |
| 3751 | if (fetch_deviceinfo) { |
| 3752 | ptr += sizeof(drmUsbBusInfo); |
| 3753 | dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr; |
| 3754 | |
| 3755 | ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb); |
| 3756 | if (ret < 0) |
| 3757 | goto free_device; |
| 3758 | } |
| 3759 | |
| 3760 | *device = dev; |
| 3761 | |
| 3762 | return 0; |
| 3763 | |
| 3764 | free_device: |
| 3765 | free(dev); |
| 3766 | return ret; |
| 3767 | } |
| 3768 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3769 | static int drmParseOFBusInfo(int maj, int min, char *fullname) |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3770 | { |
| 3771 | #ifdef __linux__ |
Emil Velikov | 3df8a7f | 2019-01-23 09:44:07 +0000 | [diff] [blame] | 3772 | char path[PATH_MAX + 1], *name, *tmp_name; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3773 | |
| 3774 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); |
| 3775 | |
| 3776 | name = sysfs_uevent_get(path, "OF_FULLNAME"); |
Emil Velikov | 3df8a7f | 2019-01-23 09:44:07 +0000 | [diff] [blame] | 3777 | tmp_name = name; |
| 3778 | if (!name) { |
| 3779 | /* If the device lacks OF data, pick the MODALIAS info */ |
| 3780 | name = sysfs_uevent_get(path, "MODALIAS"); |
| 3781 | if (!name) |
| 3782 | return -ENOENT; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3783 | |
Emil Velikov | 3df8a7f | 2019-01-23 09:44:07 +0000 | [diff] [blame] | 3784 | /* .. and strip the MODALIAS=[platform,usb...]: part. */ |
| 3785 | tmp_name = strrchr(name, ':'); |
| 3786 | if (!tmp_name) { |
| 3787 | free(name); |
| 3788 | return -ENOENT; |
| 3789 | } |
| 3790 | tmp_name++; |
| 3791 | } |
| 3792 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3793 | strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN); |
| 3794 | fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0'; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3795 | free(name); |
| 3796 | |
| 3797 | return 0; |
| 3798 | #else |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3799 | #warning "Missing implementation of drmParseOFBusInfo" |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3800 | return -EINVAL; |
| 3801 | #endif |
| 3802 | } |
| 3803 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3804 | static int drmParseOFDeviceInfo(int maj, int min, char ***compatible) |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3805 | { |
| 3806 | #ifdef __linux__ |
Emil Velikov | 3df8a7f | 2019-01-23 09:44:07 +0000 | [diff] [blame] | 3807 | char path[PATH_MAX + 1], *value, *tmp_name; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3808 | unsigned int count, i; |
| 3809 | int err; |
| 3810 | |
| 3811 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); |
| 3812 | |
| 3813 | value = sysfs_uevent_get(path, "OF_COMPATIBLE_N"); |
Emil Velikov | 3df8a7f | 2019-01-23 09:44:07 +0000 | [diff] [blame] | 3814 | if (value) { |
| 3815 | sscanf(value, "%u", &count); |
| 3816 | free(value); |
| 3817 | } else { |
| 3818 | /* Assume one entry if the device lack OF data */ |
| 3819 | count = 1; |
| 3820 | } |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3821 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3822 | *compatible = calloc(count + 1, sizeof(char *)); |
| 3823 | if (!*compatible) |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3824 | return -ENOMEM; |
| 3825 | |
| 3826 | for (i = 0; i < count; i++) { |
| 3827 | value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i); |
Emil Velikov | 3df8a7f | 2019-01-23 09:44:07 +0000 | [diff] [blame] | 3828 | tmp_name = value; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3829 | if (!value) { |
Emil Velikov | 3df8a7f | 2019-01-23 09:44:07 +0000 | [diff] [blame] | 3830 | /* If the device lacks OF data, pick the MODALIAS info */ |
| 3831 | value = sysfs_uevent_get(path, "MODALIAS"); |
| 3832 | if (!value) { |
| 3833 | err = -ENOENT; |
| 3834 | goto free; |
| 3835 | } |
| 3836 | |
| 3837 | /* .. and strip the MODALIAS=[platform,usb...]: part. */ |
| 3838 | tmp_name = strrchr(value, ':'); |
| 3839 | if (!tmp_name) { |
| 3840 | free(value); |
| 3841 | return -ENOENT; |
| 3842 | } |
| 3843 | tmp_name = strdup(tmp_name + 1); |
| 3844 | free(value); |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3845 | } |
| 3846 | |
Andreas Baierl | 4735ca7 | 2019-03-11 16:04:08 +0100 | [diff] [blame] | 3847 | (*compatible)[i] = tmp_name; |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | return 0; |
| 3851 | |
| 3852 | free: |
| 3853 | while (i--) |
Andreas Baierl | 4735ca7 | 2019-03-11 16:04:08 +0100 | [diff] [blame] | 3854 | free((*compatible)[i]); |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3855 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3856 | free(*compatible); |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3857 | return err; |
| 3858 | #else |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3859 | #warning "Missing implementation of drmParseOFDeviceInfo" |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3860 | return -EINVAL; |
| 3861 | #endif |
| 3862 | } |
| 3863 | |
| 3864 | static int drmProcessPlatformDevice(drmDevicePtr *device, |
| 3865 | const char *node, int node_type, |
| 3866 | int maj, int min, bool fetch_deviceinfo, |
| 3867 | uint32_t flags) |
| 3868 | { |
| 3869 | drmDevicePtr dev; |
| 3870 | char *ptr; |
| 3871 | int ret; |
| 3872 | |
| 3873 | dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo), |
| 3874 | sizeof(drmPlatformDeviceInfo), &ptr); |
| 3875 | if (!dev) |
| 3876 | return -ENOMEM; |
| 3877 | |
| 3878 | dev->bustype = DRM_BUS_PLATFORM; |
| 3879 | |
| 3880 | dev->businfo.platform = (drmPlatformBusInfoPtr)ptr; |
| 3881 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3882 | ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname); |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3883 | if (ret < 0) |
| 3884 | goto free_device; |
| 3885 | |
| 3886 | if (fetch_deviceinfo) { |
| 3887 | ptr += sizeof(drmPlatformBusInfo); |
| 3888 | dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr; |
| 3889 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3890 | ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible); |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3891 | if (ret < 0) |
| 3892 | goto free_device; |
| 3893 | } |
| 3894 | |
| 3895 | *device = dev; |
| 3896 | |
| 3897 | return 0; |
| 3898 | |
| 3899 | free_device: |
| 3900 | free(dev); |
| 3901 | return ret; |
| 3902 | } |
| 3903 | |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3904 | static int drmProcessHost1xDevice(drmDevicePtr *device, |
| 3905 | const char *node, int node_type, |
| 3906 | int maj, int min, bool fetch_deviceinfo, |
| 3907 | uint32_t flags) |
| 3908 | { |
| 3909 | drmDevicePtr dev; |
| 3910 | char *ptr; |
| 3911 | int ret; |
| 3912 | |
| 3913 | dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo), |
| 3914 | sizeof(drmHost1xDeviceInfo), &ptr); |
| 3915 | if (!dev) |
| 3916 | return -ENOMEM; |
| 3917 | |
| 3918 | dev->bustype = DRM_BUS_HOST1X; |
| 3919 | |
| 3920 | dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr; |
| 3921 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3922 | ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname); |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3923 | if (ret < 0) |
| 3924 | goto free_device; |
| 3925 | |
| 3926 | if (fetch_deviceinfo) { |
| 3927 | ptr += sizeof(drmHost1xBusInfo); |
| 3928 | dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr; |
| 3929 | |
Emil Velikov | ee798b9 | 2019-01-23 10:39:12 +0000 | [diff] [blame] | 3930 | ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible); |
Thierry Reding | 7b1f37f | 2016-12-21 17:59:04 +0100 | [diff] [blame] | 3931 | if (ret < 0) |
| 3932 | goto free_device; |
| 3933 | } |
| 3934 | |
| 3935 | *device = dev; |
| 3936 | |
| 3937 | return 0; |
| 3938 | |
| 3939 | free_device: |
| 3940 | free(dev); |
| 3941 | return ret; |
| 3942 | } |
| 3943 | |
Emil Velikov | f808fee | 2018-05-15 15:02:52 +0100 | [diff] [blame] | 3944 | static int |
| 3945 | process_device(drmDevicePtr *device, const char *d_name, |
| 3946 | int req_subsystem_type, |
| 3947 | bool fetch_deviceinfo, uint32_t flags) |
| 3948 | { |
| 3949 | struct stat sbuf; |
| 3950 | char node[PATH_MAX + 1]; |
| 3951 | int node_type, subsystem_type; |
| 3952 | unsigned int maj, min; |
| 3953 | |
| 3954 | node_type = drmGetNodeType(d_name); |
| 3955 | if (node_type < 0) |
| 3956 | return -1; |
| 3957 | |
| 3958 | snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name); |
| 3959 | if (stat(node, &sbuf)) |
| 3960 | return -1; |
| 3961 | |
| 3962 | maj = major(sbuf.st_rdev); |
| 3963 | min = minor(sbuf.st_rdev); |
| 3964 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 3965 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
Emil Velikov | f808fee | 2018-05-15 15:02:52 +0100 | [diff] [blame] | 3966 | return -1; |
| 3967 | |
| 3968 | subsystem_type = drmParseSubsystemType(maj, min); |
| 3969 | if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type) |
| 3970 | return -1; |
| 3971 | |
| 3972 | switch (subsystem_type) { |
| 3973 | case DRM_BUS_PCI: |
Mikhail Golubev | 57df075 | 2020-04-20 20:08:50 +0200 | [diff] [blame] | 3974 | case DRM_BUS_VIRTIO: |
Emil Velikov | f808fee | 2018-05-15 15:02:52 +0100 | [diff] [blame] | 3975 | return drmProcessPciDevice(device, node, node_type, maj, min, |
| 3976 | fetch_deviceinfo, flags); |
| 3977 | case DRM_BUS_USB: |
| 3978 | return drmProcessUsbDevice(device, node, node_type, maj, min, |
| 3979 | fetch_deviceinfo, flags); |
| 3980 | case DRM_BUS_PLATFORM: |
| 3981 | return drmProcessPlatformDevice(device, node, node_type, maj, min, |
| 3982 | fetch_deviceinfo, flags); |
| 3983 | case DRM_BUS_HOST1X: |
| 3984 | return drmProcessHost1xDevice(device, node, node_type, maj, min, |
| 3985 | fetch_deviceinfo, flags); |
| 3986 | default: |
| 3987 | return -1; |
| 3988 | } |
| 3989 | } |
| 3990 | |
Qiang Yu | 3c20893 | 2016-07-14 17:10:55 +0800 | [diff] [blame] | 3991 | /* Consider devices located on the same bus as duplicate and fold the respective |
| 3992 | * entries into a single one. |
| 3993 | * |
| 3994 | * Note: this leaves "gaps" in the array, while preserving the length. |
| 3995 | */ |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 3996 | static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count) |
| 3997 | { |
| 3998 | int node_type, i, j; |
| 3999 | |
| 4000 | for (i = 0; i < count; i++) { |
| 4001 | for (j = i + 1; j < count; j++) { |
Adam Jackson | a2fa2e0 | 2017-05-04 15:57:14 -0400 | [diff] [blame] | 4002 | if (drmDevicesEqual(local_devices[i], local_devices[j])) { |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 4003 | local_devices[i]->available_nodes |= local_devices[j]->available_nodes; |
| 4004 | node_type = log2(local_devices[j]->available_nodes); |
| 4005 | memcpy(local_devices[i]->nodes[node_type], |
| 4006 | local_devices[j]->nodes[node_type], drmGetMaxNodeName()); |
| 4007 | drmFreeDevice(&local_devices[j]); |
| 4008 | } |
| 4009 | } |
| 4010 | } |
| 4011 | } |
| 4012 | |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4013 | /* Check that the given flags are valid returning 0 on success */ |
| 4014 | static int |
| 4015 | drm_device_validate_flags(uint32_t flags) |
| 4016 | { |
| 4017 | return (flags & ~DRM_DEVICE_GET_PCI_REVISION); |
| 4018 | } |
| 4019 | |
Emil Velikov | 56e72d3 | 2018-06-21 16:06:35 +0100 | [diff] [blame] | 4020 | static bool |
| 4021 | drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev) |
| 4022 | { |
| 4023 | struct stat sbuf; |
| 4024 | |
| 4025 | for (int i = 0; i < DRM_NODE_MAX; i++) { |
| 4026 | if (device->available_nodes & 1 << i) { |
| 4027 | if (stat(device->nodes[i], &sbuf) == 0 && |
| 4028 | sbuf.st_rdev == find_rdev) |
| 4029 | return true; |
| 4030 | } |
| 4031 | } |
| 4032 | return false; |
| 4033 | } |
| 4034 | |
Emil Velikov | 95b262f | 2018-05-15 16:32:10 +0100 | [diff] [blame] | 4035 | /* |
| 4036 | * The kernel drm core has a number of places that assume maximum of |
| 4037 | * 3x64 devices nodes. That's 64 for each of primary, control and |
| 4038 | * render nodes. Rounded it up to 256 for simplicity. |
| 4039 | */ |
| 4040 | #define MAX_DRM_NODES 256 |
| 4041 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4042 | /** |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4043 | * Get information about the opened drm device |
| 4044 | * |
| 4045 | * \param fd file descriptor of the drm device |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4046 | * \param flags feature/behaviour bitmask |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4047 | * \param device the address of a drmDevicePtr where the information |
| 4048 | * will be allocated in stored |
| 4049 | * |
| 4050 | * \return zero on success, negative error code otherwise. |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4051 | * |
| 4052 | * \note Unlike drmGetDevice it does not retrieve the pci device revision field |
| 4053 | * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set. |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4054 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4055 | drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4056 | { |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4057 | #ifdef __OpenBSD__ |
| 4058 | /* |
| 4059 | * DRI device nodes on OpenBSD are not in their own directory, they reside |
| 4060 | * in /dev along with a large number of statically generated /dev nodes. |
| 4061 | * Avoid stat'ing all of /dev needlessly by implementing this custom path. |
| 4062 | */ |
| 4063 | drmDevicePtr d; |
| 4064 | struct stat sbuf; |
| 4065 | char node[PATH_MAX + 1]; |
| 4066 | const char *dev_name; |
| 4067 | int node_type, subsystem_type; |
Jonathan Gray | 293b95e | 2019-05-13 02:52:04 +1000 | [diff] [blame] | 4068 | int maj, min, n, ret; |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4069 | |
| 4070 | if (fd == -1 || device == NULL) |
| 4071 | return -EINVAL; |
| 4072 | |
| 4073 | if (fstat(fd, &sbuf)) |
| 4074 | return -errno; |
| 4075 | |
| 4076 | maj = major(sbuf.st_rdev); |
| 4077 | min = minor(sbuf.st_rdev); |
| 4078 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 4079 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4080 | return -EINVAL; |
| 4081 | |
Emmanuel Vadot | c55a1e5 | 2020-01-21 17:47:09 +0100 | [diff] [blame] | 4082 | node_type = drmGetMinorType(maj, min); |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4083 | if (node_type == -1) |
| 4084 | return -ENODEV; |
| 4085 | |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 4086 | dev_name = drmGetDeviceName(node_type); |
| 4087 | if (!dev_name) |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4088 | return -EINVAL; |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4089 | |
Jonathan Gray | 293b95e | 2019-05-13 02:52:04 +1000 | [diff] [blame] | 4090 | n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min); |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4091 | if (n == -1 || n >= PATH_MAX) |
| 4092 | return -errno; |
| 4093 | if (stat(node, &sbuf)) |
| 4094 | return -EINVAL; |
| 4095 | |
| 4096 | subsystem_type = drmParseSubsystemType(maj, min); |
| 4097 | if (subsystem_type != DRM_BUS_PCI) |
| 4098 | return -ENODEV; |
| 4099 | |
| 4100 | ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags); |
| 4101 | if (ret) |
| 4102 | return ret; |
| 4103 | |
| 4104 | *device = d; |
| 4105 | |
| 4106 | return 0; |
| 4107 | #else |
Emil Velikov | 95b262f | 2018-05-15 16:32:10 +0100 | [diff] [blame] | 4108 | drmDevicePtr local_devices[MAX_DRM_NODES]; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4109 | drmDevicePtr d; |
| 4110 | DIR *sysdir; |
| 4111 | struct dirent *dent; |
| 4112 | struct stat sbuf; |
Emil Velikov | f808fee | 2018-05-15 15:02:52 +0100 | [diff] [blame] | 4113 | int subsystem_type; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4114 | int maj, min; |
| 4115 | int ret, i, node_count; |
Qiang Yu | 3c20893 | 2016-07-14 17:10:55 +0800 | [diff] [blame] | 4116 | dev_t find_rdev; |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4117 | |
| 4118 | if (drm_device_validate_flags(flags)) |
| 4119 | return -EINVAL; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4120 | |
| 4121 | if (fd == -1 || device == NULL) |
| 4122 | return -EINVAL; |
| 4123 | |
| 4124 | if (fstat(fd, &sbuf)) |
| 4125 | return -errno; |
| 4126 | |
Qiang Yu | 3c20893 | 2016-07-14 17:10:55 +0800 | [diff] [blame] | 4127 | find_rdev = sbuf.st_rdev; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4128 | maj = major(sbuf.st_rdev); |
| 4129 | min = minor(sbuf.st_rdev); |
| 4130 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 4131 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4132 | return -EINVAL; |
| 4133 | |
| 4134 | subsystem_type = drmParseSubsystemType(maj, min); |
Emil Velikov | 7f52a0e | 2018-05-15 16:43:58 +0100 | [diff] [blame] | 4135 | if (subsystem_type < 0) |
| 4136 | return subsystem_type; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4137 | |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4138 | sysdir = opendir(DRM_DIR_NAME); |
Emil Velikov | 95b262f | 2018-05-15 16:32:10 +0100 | [diff] [blame] | 4139 | if (!sysdir) |
| 4140 | return -errno; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4141 | |
| 4142 | i = 0; |
| 4143 | while ((dent = readdir(sysdir))) { |
Emil Velikov | f808fee | 2018-05-15 15:02:52 +0100 | [diff] [blame] | 4144 | ret = process_device(&d, dent->d_name, subsystem_type, true, flags); |
| 4145 | if (ret) |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4146 | continue; |
| 4147 | |
Emil Velikov | 95b262f | 2018-05-15 16:32:10 +0100 | [diff] [blame] | 4148 | if (i >= MAX_DRM_NODES) { |
| 4149 | fprintf(stderr, "More than %d drm nodes detected. " |
| 4150 | "Please report a bug - that should not happen.\n" |
| 4151 | "Skipping extra nodes\n", MAX_DRM_NODES); |
| 4152 | break; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4153 | } |
Emil Velikov | 56e72d3 | 2018-06-21 16:06:35 +0100 | [diff] [blame] | 4154 | local_devices[i] = d; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4155 | i++; |
| 4156 | } |
| 4157 | node_count = i; |
| 4158 | |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4159 | drmFoldDuplicatedDevices(local_devices, node_count); |
| 4160 | |
Mariusz Ceier | 4519db2 | 2018-07-29 10:20:14 +0200 | [diff] [blame] | 4161 | *device = NULL; |
| 4162 | |
Emil Velikov | 56e72d3 | 2018-06-21 16:06:35 +0100 | [diff] [blame] | 4163 | for (i = 0; i < node_count; i++) { |
| 4164 | if (!local_devices[i]) |
| 4165 | continue; |
| 4166 | |
| 4167 | if (drm_device_has_rdev(local_devices[i], find_rdev)) |
| 4168 | *device = local_devices[i]; |
| 4169 | else |
| 4170 | drmFreeDevice(&local_devices[i]); |
| 4171 | } |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4172 | |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4173 | closedir(sysdir); |
Rob Herring | 677cd97 | 2016-10-21 10:07:59 -0700 | [diff] [blame] | 4174 | if (*device == NULL) |
Thierry Reding | e17cad1 | 2016-12-21 18:00:52 +0100 | [diff] [blame] | 4175 | return -ENODEV; |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4176 | return 0; |
Jonathan Gray | 0825792 | 2016-12-01 15:18:43 +1100 | [diff] [blame] | 4177 | #endif |
Emil Velikov | ccedf66 | 2015-09-09 16:02:18 +0100 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | /** |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4181 | * Get information about the opened drm device |
| 4182 | * |
| 4183 | * \param fd file descriptor of the drm device |
| 4184 | * \param device the address of a drmDevicePtr where the information |
| 4185 | * will be allocated in stored |
| 4186 | * |
| 4187 | * \return zero on success, negative error code otherwise. |
| 4188 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4189 | drm_public int drmGetDevice(int fd, drmDevicePtr *device) |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4190 | { |
| 4191 | return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device); |
| 4192 | } |
| 4193 | |
| 4194 | /** |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4195 | * Get drm devices on the system |
| 4196 | * |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4197 | * \param flags feature/behaviour bitmask |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4198 | * \param devices the array of devices with drmDevicePtr elements |
| 4199 | * can be NULL to get the device number first |
| 4200 | * \param max_devices the maximum number of devices for the array |
| 4201 | * |
| 4202 | * \return on error - negative error code, |
| 4203 | * if devices is NULL - total number of devices available on the system, |
| 4204 | * alternatively the number of devices stored in devices[], which is |
| 4205 | * capped by the max_devices. |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4206 | * |
| 4207 | * \note Unlike drmGetDevices it does not retrieve the pci device revision field |
| 4208 | * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set. |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4209 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4210 | drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], |
| 4211 | int max_devices) |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4212 | { |
Emil Velikov | 95b262f | 2018-05-15 16:32:10 +0100 | [diff] [blame] | 4213 | drmDevicePtr local_devices[MAX_DRM_NODES]; |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 4214 | drmDevicePtr device; |
| 4215 | DIR *sysdir; |
| 4216 | struct dirent *dent; |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 4217 | int ret, i, node_count, device_count; |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4218 | |
| 4219 | if (drm_device_validate_flags(flags)) |
| 4220 | return -EINVAL; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4221 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4222 | sysdir = opendir(DRM_DIR_NAME); |
Emil Velikov | 95b262f | 2018-05-15 16:32:10 +0100 | [diff] [blame] | 4223 | if (!sysdir) |
| 4224 | return -errno; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4225 | |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 4226 | i = 0; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4227 | while ((dent = readdir(sysdir))) { |
Emil Velikov | f808fee | 2018-05-15 15:02:52 +0100 | [diff] [blame] | 4228 | ret = process_device(&device, dent->d_name, -1, devices != NULL, flags); |
| 4229 | if (ret) |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4230 | continue; |
| 4231 | |
Emil Velikov | 95b262f | 2018-05-15 16:32:10 +0100 | [diff] [blame] | 4232 | if (i >= MAX_DRM_NODES) { |
| 4233 | fprintf(stderr, "More than %d drm nodes detected. " |
| 4234 | "Please report a bug - that should not happen.\n" |
| 4235 | "Skipping extra nodes\n", MAX_DRM_NODES); |
| 4236 | break; |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 4237 | } |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 4238 | local_devices[i] = device; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4239 | i++; |
| 4240 | } |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4241 | node_count = i; |
| 4242 | |
Emil Velikov | fae59d7 | 2015-09-09 17:54:34 +0100 | [diff] [blame] | 4243 | drmFoldDuplicatedDevices(local_devices, node_count); |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4244 | |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 4245 | device_count = 0; |
Qiang Yu | 70b6407 | 2016-06-06 12:29:16 -0400 | [diff] [blame] | 4246 | for (i = 0; i < node_count; i++) { |
Jan Vesely | 50d3c85 | 2016-06-30 14:22:52 -0400 | [diff] [blame] | 4247 | if (!local_devices[i]) |
| 4248 | continue; |
Qiang Yu | 70b6407 | 2016-06-06 12:29:16 -0400 | [diff] [blame] | 4249 | |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 4250 | if ((devices != NULL) && (device_count < max_devices)) |
| 4251 | devices[device_count] = local_devices[i]; |
| 4252 | else |
| 4253 | drmFreeDevice(&local_devices[i]); |
| 4254 | |
| 4255 | device_count++; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4256 | } |
| 4257 | |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4258 | closedir(sysdir); |
Emil Velikov | 5f68d31 | 2015-09-07 13:54:32 +0100 | [diff] [blame] | 4259 | return device_count; |
Emil Velikov | b556ea1 | 2015-08-17 11:09:06 +0800 | [diff] [blame] | 4260 | } |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4261 | |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4262 | /** |
| 4263 | * Get drm devices on the system |
| 4264 | * |
| 4265 | * \param devices the array of devices with drmDevicePtr elements |
| 4266 | * can be NULL to get the device number first |
| 4267 | * \param max_devices the maximum number of devices for the array |
| 4268 | * |
| 4269 | * \return on error - negative error code, |
| 4270 | * if devices is NULL - total number of devices available on the system, |
| 4271 | * alternatively the number of devices stored in devices[], which is |
| 4272 | * capped by the max_devices. |
| 4273 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4274 | drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices) |
Emil Velikov | 11687bf | 2016-11-30 17:24:21 +0000 | [diff] [blame] | 4275 | { |
| 4276 | return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices); |
| 4277 | } |
| 4278 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4279 | drm_public char *drmGetDeviceNameFromFd2(int fd) |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4280 | { |
| 4281 | #ifdef __linux__ |
| 4282 | struct stat sbuf; |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 4283 | char path[PATH_MAX + 1], *value; |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4284 | unsigned int maj, min; |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4285 | |
| 4286 | if (fstat(fd, &sbuf)) |
| 4287 | return NULL; |
| 4288 | |
| 4289 | maj = major(sbuf.st_rdev); |
| 4290 | min = minor(sbuf.st_rdev); |
| 4291 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 4292 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4293 | return NULL; |
| 4294 | |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 4295 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min); |
| 4296 | |
| 4297 | value = sysfs_uevent_get(path, "DEVNAME"); |
| 4298 | if (!value) |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4299 | return NULL; |
| 4300 | |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 4301 | snprintf(path, sizeof(path), "/dev/%s", value); |
| 4302 | free(value); |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4303 | |
Thierry Reding | 5403cb3 | 2017-01-18 08:29:23 +0100 | [diff] [blame] | 4304 | return strdup(path); |
Emmanuel Vadot | 13c9de3 | 2020-01-21 17:58:28 +0100 | [diff] [blame] | 4305 | #elif __FreeBSD__ |
| 4306 | return drmGetDeviceNameFromFd(fd); |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4307 | #else |
Jonathan Gray | e2e766d | 2016-12-17 16:09:52 +1100 | [diff] [blame] | 4308 | struct stat sbuf; |
| 4309 | char node[PATH_MAX + 1]; |
| 4310 | const char *dev_name; |
| 4311 | int node_type; |
Jonathan Gray | 293b95e | 2019-05-13 02:52:04 +1000 | [diff] [blame] | 4312 | int maj, min, n; |
Jonathan Gray | e2e766d | 2016-12-17 16:09:52 +1100 | [diff] [blame] | 4313 | |
| 4314 | if (fstat(fd, &sbuf)) |
| 4315 | return NULL; |
| 4316 | |
| 4317 | maj = major(sbuf.st_rdev); |
| 4318 | min = minor(sbuf.st_rdev); |
| 4319 | |
Thomas Hellstrom | f839258 | 2018-08-31 13:47:05 +0200 | [diff] [blame] | 4320 | if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) |
Jonathan Gray | e2e766d | 2016-12-17 16:09:52 +1100 | [diff] [blame] | 4321 | return NULL; |
| 4322 | |
Emmanuel Vadot | c55a1e5 | 2020-01-21 17:47:09 +0100 | [diff] [blame] | 4323 | node_type = drmGetMinorType(maj, min); |
Jonathan Gray | e2e766d | 2016-12-17 16:09:52 +1100 | [diff] [blame] | 4324 | if (node_type == -1) |
| 4325 | return NULL; |
| 4326 | |
Eric Engestrom | 331e51e | 2018-12-19 13:53:41 +0000 | [diff] [blame] | 4327 | dev_name = drmGetDeviceName(node_type); |
| 4328 | if (!dev_name) |
Jonathan Gray | e2e766d | 2016-12-17 16:09:52 +1100 | [diff] [blame] | 4329 | return NULL; |
Jonathan Gray | e2e766d | 2016-12-17 16:09:52 +1100 | [diff] [blame] | 4330 | |
Jonathan Gray | 293b95e | 2019-05-13 02:52:04 +1000 | [diff] [blame] | 4331 | n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min); |
Jonathan Gray | e2e766d | 2016-12-17 16:09:52 +1100 | [diff] [blame] | 4332 | if (n == -1 || n >= PATH_MAX) |
| 4333 | return NULL; |
| 4334 | |
| 4335 | return strdup(node); |
Emil Velikov | 37d790f | 2016-11-10 17:26:50 +0000 | [diff] [blame] | 4336 | #endif |
| 4337 | } |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4338 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4339 | drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle) |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4340 | { |
| 4341 | struct drm_syncobj_create args; |
| 4342 | int ret; |
| 4343 | |
| 4344 | memclear(args); |
| 4345 | args.flags = flags; |
| 4346 | args.handle = 0; |
| 4347 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args); |
| 4348 | if (ret) |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4349 | return ret; |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4350 | *handle = args.handle; |
| 4351 | return 0; |
| 4352 | } |
| 4353 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4354 | drm_public int drmSyncobjDestroy(int fd, uint32_t handle) |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4355 | { |
| 4356 | struct drm_syncobj_destroy args; |
| 4357 | |
| 4358 | memclear(args); |
| 4359 | args.handle = handle; |
| 4360 | return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args); |
| 4361 | } |
| 4362 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4363 | drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd) |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4364 | { |
| 4365 | struct drm_syncobj_handle args; |
| 4366 | int ret; |
| 4367 | |
| 4368 | memclear(args); |
| 4369 | args.fd = -1; |
| 4370 | args.handle = handle; |
| 4371 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args); |
| 4372 | if (ret) |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4373 | return ret; |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4374 | *obj_fd = args.fd; |
| 4375 | return 0; |
| 4376 | } |
| 4377 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4378 | drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle) |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4379 | { |
| 4380 | struct drm_syncobj_handle args; |
| 4381 | int ret; |
| 4382 | |
| 4383 | memclear(args); |
| 4384 | args.fd = obj_fd; |
| 4385 | args.handle = 0; |
| 4386 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args); |
| 4387 | if (ret) |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4388 | return ret; |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4389 | *handle = args.handle; |
| 4390 | return 0; |
| 4391 | } |
| 4392 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4393 | drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle, |
| 4394 | int sync_file_fd) |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4395 | { |
| 4396 | struct drm_syncobj_handle args; |
| 4397 | |
| 4398 | memclear(args); |
| 4399 | args.fd = sync_file_fd; |
| 4400 | args.handle = handle; |
| 4401 | args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE; |
| 4402 | return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args); |
| 4403 | } |
| 4404 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4405 | drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle, |
| 4406 | int *sync_file_fd) |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4407 | { |
| 4408 | struct drm_syncobj_handle args; |
| 4409 | int ret; |
| 4410 | |
| 4411 | memclear(args); |
| 4412 | args.fd = -1; |
| 4413 | args.handle = handle; |
| 4414 | args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE; |
| 4415 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args); |
| 4416 | if (ret) |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4417 | return ret; |
Dave Airlie | fc49227 | 2017-06-17 11:01:01 +1000 | [diff] [blame] | 4418 | *sync_file_fd = args.fd; |
| 4419 | return 0; |
| 4420 | } |
Marek Olšák | 2048a9e | 2017-09-11 21:57:32 +0200 | [diff] [blame] | 4421 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4422 | drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles, |
| 4423 | int64_t timeout_nsec, unsigned flags, |
| 4424 | uint32_t *first_signaled) |
Marek Olšák | 2048a9e | 2017-09-11 21:57:32 +0200 | [diff] [blame] | 4425 | { |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4426 | struct drm_syncobj_wait args; |
| 4427 | int ret; |
Marek Olšák | 2048a9e | 2017-09-11 21:57:32 +0200 | [diff] [blame] | 4428 | |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4429 | memclear(args); |
Bas Nieuwenhuizen | b1e63d9 | 2018-02-06 11:21:35 +0100 | [diff] [blame] | 4430 | args.handles = (uintptr_t)handles; |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4431 | args.timeout_nsec = timeout_nsec; |
| 4432 | args.count_handles = num_handles; |
| 4433 | args.flags = flags; |
Marek Olšák | 2048a9e | 2017-09-11 21:57:32 +0200 | [diff] [blame] | 4434 | |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4435 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args); |
| 4436 | if (ret < 0) |
Chunming Zhou | bde3b9b | 2018-02-07 11:22:32 +0800 | [diff] [blame] | 4437 | return -errno; |
Marek Olšák | 2048a9e | 2017-09-11 21:57:32 +0200 | [diff] [blame] | 4438 | |
Dave Airlie | 61ff977 | 2017-10-25 07:43:56 +0100 | [diff] [blame] | 4439 | if (first_signaled) |
| 4440 | *first_signaled = args.first_signaled; |
| 4441 | return ret; |
Marek Olšák | 2048a9e | 2017-09-11 21:57:32 +0200 | [diff] [blame] | 4442 | } |
Bas Nieuwenhuizen | 1abcced | 2017-12-17 00:27:09 +0100 | [diff] [blame] | 4443 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4444 | drm_public int drmSyncobjReset(int fd, const uint32_t *handles, |
| 4445 | uint32_t handle_count) |
Bas Nieuwenhuizen | 1abcced | 2017-12-17 00:27:09 +0100 | [diff] [blame] | 4446 | { |
| 4447 | struct drm_syncobj_array args; |
| 4448 | int ret; |
| 4449 | |
| 4450 | memclear(args); |
| 4451 | args.handles = (uintptr_t)handles; |
| 4452 | args.count_handles = handle_count; |
| 4453 | |
| 4454 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args); |
| 4455 | return ret; |
| 4456 | } |
| 4457 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 4458 | drm_public int drmSyncobjSignal(int fd, const uint32_t *handles, |
| 4459 | uint32_t handle_count) |
Bas Nieuwenhuizen | 1abcced | 2017-12-17 00:27:09 +0100 | [diff] [blame] | 4460 | { |
| 4461 | struct drm_syncobj_array args; |
| 4462 | int ret; |
| 4463 | |
| 4464 | memclear(args); |
| 4465 | args.handles = (uintptr_t)handles; |
| 4466 | args.count_handles = handle_count; |
| 4467 | |
| 4468 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args); |
| 4469 | return ret; |
| 4470 | } |
Chunming Zhou | ec6ae51 | 2019-05-16 16:07:09 +0800 | [diff] [blame] | 4471 | |
Chunming Zhou | 12712eb | 2019-05-16 16:07:11 +0800 | [diff] [blame] | 4472 | drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles, |
| 4473 | uint64_t *points, uint32_t handle_count) |
| 4474 | { |
| 4475 | struct drm_syncobj_timeline_array args; |
| 4476 | int ret; |
| 4477 | |
| 4478 | memclear(args); |
| 4479 | args.handles = (uintptr_t)handles; |
| 4480 | args.points = (uintptr_t)points; |
| 4481 | args.count_handles = handle_count; |
| 4482 | |
| 4483 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args); |
| 4484 | return ret; |
| 4485 | } |
| 4486 | |
Chunming Zhou | ec6ae51 | 2019-05-16 16:07:09 +0800 | [diff] [blame] | 4487 | drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points, |
| 4488 | unsigned num_handles, |
| 4489 | int64_t timeout_nsec, unsigned flags, |
| 4490 | uint32_t *first_signaled) |
| 4491 | { |
| 4492 | struct drm_syncobj_timeline_wait args; |
| 4493 | int ret; |
| 4494 | |
| 4495 | memclear(args); |
| 4496 | args.handles = (uintptr_t)handles; |
| 4497 | args.points = (uintptr_t)points; |
| 4498 | args.timeout_nsec = timeout_nsec; |
| 4499 | args.count_handles = num_handles; |
| 4500 | args.flags = flags; |
| 4501 | |
| 4502 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args); |
| 4503 | if (ret < 0) |
| 4504 | return -errno; |
| 4505 | |
| 4506 | if (first_signaled) |
| 4507 | *first_signaled = args.first_signaled; |
| 4508 | return ret; |
| 4509 | } |
| 4510 | |
| 4511 | |
| 4512 | drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points, |
| 4513 | uint32_t handle_count) |
| 4514 | { |
| 4515 | struct drm_syncobj_timeline_array args; |
| 4516 | int ret; |
| 4517 | |
| 4518 | memclear(args); |
| 4519 | args.handles = (uintptr_t)handles; |
| 4520 | args.points = (uintptr_t)points; |
| 4521 | args.count_handles = handle_count; |
| 4522 | |
| 4523 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args); |
| 4524 | if (ret) |
| 4525 | return ret; |
| 4526 | return 0; |
| 4527 | } |
| 4528 | |
Chunming Zhou | 0a7ad7d | 2019-07-24 15:55:20 +0800 | [diff] [blame] | 4529 | drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points, |
| 4530 | uint32_t handle_count, uint32_t flags) |
| 4531 | { |
| 4532 | struct drm_syncobj_timeline_array args; |
| 4533 | |
| 4534 | memclear(args); |
| 4535 | args.handles = (uintptr_t)handles; |
| 4536 | args.points = (uintptr_t)points; |
| 4537 | args.count_handles = handle_count; |
| 4538 | args.flags = flags; |
| 4539 | |
| 4540 | return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args); |
| 4541 | } |
| 4542 | |
| 4543 | |
Chunming Zhou | 12712eb | 2019-05-16 16:07:11 +0800 | [diff] [blame] | 4544 | drm_public int drmSyncobjTransfer(int fd, |
| 4545 | uint32_t dst_handle, uint64_t dst_point, |
| 4546 | uint32_t src_handle, uint64_t src_point, |
| 4547 | uint32_t flags) |
| 4548 | { |
| 4549 | struct drm_syncobj_transfer args; |
| 4550 | int ret; |
Chunming Zhou | ec6ae51 | 2019-05-16 16:07:09 +0800 | [diff] [blame] | 4551 | |
Chunming Zhou | 12712eb | 2019-05-16 16:07:11 +0800 | [diff] [blame] | 4552 | memclear(args); |
| 4553 | args.src_handle = src_handle; |
| 4554 | args.dst_handle = dst_handle; |
| 4555 | args.src_point = src_point; |
| 4556 | args.dst_point = dst_point; |
| 4557 | args.flags = flags; |
| 4558 | |
| 4559 | ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args); |
| 4560 | |
| 4561 | return ret; |
| 4562 | } |