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