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