3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
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:
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
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.
48 #include <sys/types.h>
50 #define stat_t struct stat
51 #include <sys/ioctl.h>
55 #include <sys/mkdev.h>
57 #ifdef MAJOR_IN_SYSMACROS
58 #include <sys/sysmacros.h>
61 #include <sys/sysctl.h>
66 #if defined(__FreeBSD__)
67 #include <sys/param.h>
68 #include <sys/pciio.h>
71 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73 /* Not all systems have MAP_FAILED defined */
75 #define MAP_FAILED ((void *)-1)
79 #include "libdrm_macros.h"
80 #include "drm_fourcc.h"
82 #include "util_math.h"
98 #endif /* __OpenBSD__ */
101 #define DRM_MAJOR 226 /* Linux */
104 #if defined(__OpenBSD__) || defined(__DragonFly__)
112 uint16_t subvendor_id;
113 uint16_t subdevice_id;
117 #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
120 #define DRM_MSG_VERBOSITY 3
122 #define memclear(s) memset(&s, 0, sizeof(s))
124 static drmServerInfoPtr drm_server_info;
126 static bool drmNodeIsDRM(int maj, int min);
127 static char *drmGetMinorNameForFD(int fd, int type);
129 #define DRM_MODIFIER(v, f, f_name) \
130 .modifier = DRM_FORMAT_MOD_##v ## _ ##f, \
131 .modifier_name = #f_name
133 #define DRM_MODIFIER_INVALID(v, f_name) \
134 .modifier = DRM_FORMAT_MOD_INVALID, .modifier_name = #f_name
136 #define DRM_MODIFIER_LINEAR(v, f_name) \
137 .modifier = DRM_FORMAT_MOD_LINEAR, .modifier_name = #f_name
139 /* Intel is abit special as the format doesn't follow other vendors naming
141 #define DRM_MODIFIER_INTEL(f, f_name) \
142 .modifier = I915_FORMAT_MOD_##f, .modifier_name = #f_name
144 struct drmFormatModifierInfo {
146 const char *modifier_name;
149 struct drmFormatModifierVendorInfo {
151 const char *vendor_name;
154 #include "generated_static_table_fourcc.h"
156 struct drmVendorInfo {
158 char *(*vendor_cb)(uint64_t modifier);
161 struct drmFormatVendorModifierInfo {
163 const char *modifier_name;
167 drmGetFormatModifierNameFromArm(uint64_t modifier);
170 drmGetFormatModifierNameFromNvidia(uint64_t modifier);
173 drmGetFormatModifierNameFromAmd(uint64_t modifier);
176 drmGetFormatModifierNameFromAmlogic(uint64_t modifier);
178 static const struct drmVendorInfo modifier_format_vendor_table[] = {
179 { DRM_FORMAT_MOD_VENDOR_ARM, drmGetFormatModifierNameFromArm },
180 { DRM_FORMAT_MOD_VENDOR_NVIDIA, drmGetFormatModifierNameFromNvidia },
181 { DRM_FORMAT_MOD_VENDOR_AMD, drmGetFormatModifierNameFromAmd },
182 { DRM_FORMAT_MOD_VENDOR_AMLOGIC, drmGetFormatModifierNameFromAmlogic },
185 #ifndef AFBC_FORMAT_MOD_MODE_VALUE_MASK
186 #define AFBC_FORMAT_MOD_MODE_VALUE_MASK 0x000fffffffffffffULL
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" },
201 static bool is_x_t_amd_gfx9_tile(uint64_t 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:
214 drmGetAfbcFormatModifierNameFromArm(uint64_t modifier, FILE *fp)
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;
219 const char *block = NULL;
220 const char *mode = NULL;
221 bool did_print_mode = false;
223 /* add block, can only have a (single) block */
224 switch (block_size) {
225 case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
228 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
231 case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
234 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
243 fprintf(fp, "BLOCK_SIZE=%s,", block);
246 for (unsigned int i = 0; i < ARRAY_SIZE(arm_mode_value_table); i++) {
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;
253 fprintf(fp, "|%s", mode);
262 drmGetAfrcFormatModifierNameFromArm(uint64_t modifier, FILE *fp)
265 for (unsigned int i = 0; i < 2; ++i) {
266 uint64_t coding_unit_block =
267 (modifier >> (i * 4)) & AFRC_FORMAT_MOD_CU_SIZE_MASK;
268 const char *coding_unit_size = NULL;
270 switch (coding_unit_block) {
271 case AFRC_FORMAT_MOD_CU_SIZE_16:
272 coding_unit_size = "CU_16";
274 case AFRC_FORMAT_MOD_CU_SIZE_24:
275 coding_unit_size = "CU_24";
277 case AFRC_FORMAT_MOD_CU_SIZE_32:
278 coding_unit_size = "CU_32";
282 if (!coding_unit_size) {
290 fprintf(fp, "P0=%s,", coding_unit_size);
292 fprintf(fp, "P12=%s,", coding_unit_size);
297 (modifier & AFRC_FORMAT_MOD_LAYOUT_SCAN) == AFRC_FORMAT_MOD_LAYOUT_SCAN;
307 drmGetFormatModifierNameFromArm(uint64_t modifier)
309 uint64_t type = (modifier >> 52) & 0xf;
313 char *modifier_name = NULL;
316 fp = open_memstream(&modifier_name, &size);
321 case DRM_FORMAT_MOD_ARM_TYPE_AFBC:
322 result = drmGetAfbcFormatModifierNameFromArm(modifier, fp);
324 case DRM_FORMAT_MOD_ARM_TYPE_AFRC:
325 result = drmGetAfrcFormatModifierNameFromArm(modifier, fp);
327 /* misc type is already handled by the static table */
328 case DRM_FORMAT_MOD_ARM_TYPE_MISC:
340 return modifier_name;
344 drmGetFormatModifierNameFromNvidia(uint64_t modifier)
346 uint64_t height, kind, gen, sector, compression;
348 height = modifier & 0xf;
349 kind = (modifier >> 12) & 0xff;
351 gen = (modifier >> 20) & 0x3;
352 sector = (modifier >> 22) & 0x1;
353 compression = (modifier >> 23) & 0x7;
355 /* just in case there could other simpler modifiers, not yet added, avoid
356 * testing against TEGRA_TILE */
357 if ((modifier & 0x10) == 0x10) {
359 if (asprintf(&mod_nvidia, "BLOCK_LINEAR_2D,HEIGHT=%"PRIu64",KIND=%"PRIu64","
360 "GEN=%"PRIu64",SECTOR=%"PRIu64",COMPRESSION=%"PRIu64"", height,
361 kind, gen, sector, compression) < 0)
370 drmGetFormatModifierNameFromAmdDcc(uint64_t modifier, FILE *fp)
372 uint64_t dcc_max_compressed_block =
373 AMD_FMT_MOD_GET(DCC_MAX_COMPRESSED_BLOCK, modifier);
374 uint64_t dcc_retile = AMD_FMT_MOD_GET(DCC_RETILE, modifier);
376 const char *dcc_max_compressed_block_str = NULL;
381 fprintf(fp, ",DCC_RETILE");
383 if (!dcc_retile && AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier))
384 fprintf(fp, ",DCC_PIPE_ALIGN");
386 if (AMD_FMT_MOD_GET(DCC_INDEPENDENT_64B, modifier))
387 fprintf(fp, ",DCC_INDEPENDENT_64B");
389 if (AMD_FMT_MOD_GET(DCC_INDEPENDENT_128B, modifier))
390 fprintf(fp, ",DCC_INDEPENDENT_128B");
392 switch (dcc_max_compressed_block) {
393 case AMD_FMT_MOD_DCC_BLOCK_64B:
394 dcc_max_compressed_block_str = "64B";
396 case AMD_FMT_MOD_DCC_BLOCK_128B:
397 dcc_max_compressed_block_str = "128B";
399 case AMD_FMT_MOD_DCC_BLOCK_256B:
400 dcc_max_compressed_block_str = "256B";
404 if (dcc_max_compressed_block_str)
405 fprintf(fp, ",DCC_MAX_COMPRESSED_BLOCK=%s",
406 dcc_max_compressed_block_str);
408 if (AMD_FMT_MOD_GET(DCC_CONSTANT_ENCODE, modifier))
409 fprintf(fp, ",DCC_CONSTANT_ENCODE");
413 drmGetFormatModifierNameFromAmdTile(uint64_t modifier, FILE *fp)
415 uint64_t pipe_xor_bits, bank_xor_bits, packers, rb;
416 uint64_t pipe, pipe_align, dcc, dcc_retile, tile_version;
418 pipe_align = AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier);
419 pipe_xor_bits = AMD_FMT_MOD_GET(PIPE_XOR_BITS, modifier);
420 dcc = AMD_FMT_MOD_GET(DCC, modifier);
421 dcc_retile = AMD_FMT_MOD_GET(DCC_RETILE, modifier);
422 tile_version = AMD_FMT_MOD_GET(TILE_VERSION, modifier);
424 fprintf(fp, ",PIPE_XOR_BITS=%"PRIu64, pipe_xor_bits);
426 if (tile_version == AMD_FMT_MOD_TILE_VER_GFX9) {
427 bank_xor_bits = AMD_FMT_MOD_GET(BANK_XOR_BITS, modifier);
428 fprintf(fp, ",BANK_XOR_BITS=%"PRIu64, bank_xor_bits);
431 if (tile_version == AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) {
432 packers = AMD_FMT_MOD_GET(PACKERS, modifier);
433 fprintf(fp, ",PACKERS=%"PRIu64, packers);
436 if (dcc && tile_version == AMD_FMT_MOD_TILE_VER_GFX9) {
437 rb = AMD_FMT_MOD_GET(RB, modifier);
438 fprintf(fp, ",RB=%"PRIu64, rb);
441 if (dcc && tile_version == AMD_FMT_MOD_TILE_VER_GFX9 &&
442 (dcc_retile || pipe_align)) {
443 pipe = AMD_FMT_MOD_GET(PIPE, modifier);
444 fprintf(fp, ",PIPE_%"PRIu64, pipe);
449 drmGetFormatModifierNameFromAmd(uint64_t modifier)
451 uint64_t tile, tile_version, dcc;
453 char *mod_amd = NULL;
456 const char *str_tile = NULL;
457 const char *str_tile_version = NULL;
459 tile = AMD_FMT_MOD_GET(TILE, modifier);
460 tile_version = AMD_FMT_MOD_GET(TILE_VERSION, modifier);
461 dcc = AMD_FMT_MOD_GET(DCC, modifier);
463 fp = open_memstream(&mod_amd, &size);
468 switch (tile_version) {
469 case AMD_FMT_MOD_TILE_VER_GFX9:
470 str_tile_version = "GFX9";
472 case AMD_FMT_MOD_TILE_VER_GFX10:
473 str_tile_version = "GFX10";
475 case AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS:
476 str_tile_version = "GFX10_RBPLUS";
480 if (str_tile_version) {
481 fprintf(fp, "%s", str_tile_version);
490 case AMD_FMT_MOD_TILE_GFX9_64K_S:
491 str_tile = "GFX9_64K_S";
493 case AMD_FMT_MOD_TILE_GFX9_64K_D:
494 str_tile = "GFX9_64K_D";
496 case AMD_FMT_MOD_TILE_GFX9_64K_S_X:
497 str_tile = "GFX9_64K_S_X";
499 case AMD_FMT_MOD_TILE_GFX9_64K_D_X:
500 str_tile = "GFX9_64K_D_X";
502 case AMD_FMT_MOD_TILE_GFX9_64K_R_X:
503 str_tile = "GFX9_64K_R_X";
508 fprintf(fp, ",%s", str_tile);
511 drmGetFormatModifierNameFromAmdDcc(modifier, fp);
513 if (tile_version >= AMD_FMT_MOD_TILE_VER_GFX9 && is_x_t_amd_gfx9_tile(tile))
514 drmGetFormatModifierNameFromAmdTile(modifier, fp);
521 drmGetFormatModifierNameFromAmlogic(uint64_t modifier)
523 uint64_t layout = modifier & 0xff;
524 uint64_t options = (modifier >> 8) & 0xff;
525 char *mod_amlogic = NULL;
527 const char *layout_str;
528 const char *opts_str;
531 case AMLOGIC_FBC_LAYOUT_BASIC:
532 layout_str = "BASIC";
534 case AMLOGIC_FBC_LAYOUT_SCATTER:
535 layout_str = "SCATTER";
538 layout_str = "INVALID_LAYOUT";
542 if (options & AMLOGIC_FBC_OPTION_MEM_SAVING)
543 opts_str = "MEM_SAVING";
547 if (asprintf(&mod_amlogic, "FBC,LAYOUT=%s,OPTIONS=%s", layout_str, opts_str)
553 static unsigned log2_int(unsigned x)
561 if ((unsigned)(1 << l) > x) {
569 drm_public void drmSetServerInfo(drmServerInfoPtr info)
571 drm_server_info = info;
575 * Output a message to stderr.
577 * \param format printf() like format string.
580 * This function is a wrapper around vfprintf().
583 static int DRM_PRINTFLIKE(1, 0)
584 drmDebugPrint(const char *format, va_list ap)
586 return vfprintf(stderr, format, ap);
590 drmMsg(const char *format, ...)
594 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
595 (drm_server_info && drm_server_info->debug_print))
597 va_start(ap, format);
598 if (drm_server_info) {
599 drm_server_info->debug_print(format,ap);
601 drmDebugPrint(format, ap);
607 static void *drmHashTable = NULL; /* Context switch callbacks */
609 drm_public void *drmGetHashTable(void)
614 drm_public void *drmMalloc(int size)
616 return calloc(1, size);
619 drm_public void drmFree(void *pt)
625 * Call ioctl, restarting if it is interrupted
628 drmIoctl(int fd, unsigned long request, void *arg)
633 ret = ioctl(fd, request, arg);
634 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
638 static unsigned long drmGetKeyFromFd(int fd)
647 drm_public drmHashEntry *drmGetEntry(int fd)
649 unsigned long key = drmGetKeyFromFd(fd);
654 drmHashTable = drmHashCreate();
656 if (drmHashLookup(drmHashTable, key, &value)) {
657 entry = drmMalloc(sizeof(*entry));
660 entry->tagTable = drmHashCreate();
661 drmHashInsert(drmHashTable, key, entry);
669 * Compare two busid strings
674 * \return 1 if matched.
677 * This function compares two bus ID strings. It understands the older
678 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
679 * domain, b is bus, d is device, f is function.
681 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
683 /* First, check if the IDs are exactly the same */
684 if (strcasecmp(id1, id2) == 0)
687 /* Try to match old/new-style PCI bus IDs. */
688 if (strncasecmp(id1, "pci", 3) == 0) {
689 unsigned int o1, b1, d1, f1;
690 unsigned int o2, b2, d2, f2;
693 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
696 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
701 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
704 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
709 /* If domains aren't properly supported by the kernel interface,
710 * just ignore them, which sucks less than picking a totally random
711 * card with "open by name"
716 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
725 * Handles error checking for chown call.
727 * \param path to file.
728 * \param id of the new owner.
729 * \param id of the new group.
731 * \return zero if success or -1 if failure.
734 * Checks for failure. If failure was caused by signal call chown again.
735 * If any other failure happened then it will output error message using
739 static int chown_check_return(const char *path, uid_t owner, gid_t group)
744 rv = chown(path, owner, group);
745 } while (rv != 0 && errno == EINTR);
750 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
751 path, errno, strerror(errno));
756 static const char *drmGetDeviceName(int type)
759 case DRM_NODE_PRIMARY:
761 case DRM_NODE_CONTROL:
762 return DRM_CONTROL_DEV_NAME;
763 case DRM_NODE_RENDER:
764 return DRM_RENDER_DEV_NAME;
770 * Open the DRM device, creating it if necessary.
772 * \param dev major and minor numbers of the device.
773 * \param minor minor number of the device.
775 * \return a file descriptor on success, or a negative value on error.
778 * Assembles the device name from \p minor and opens it, creating the device
779 * special file node with the major and minor numbers specified by \p dev and
780 * parent directory if necessary and was called by root.
782 static int drmOpenDevice(dev_t dev, int minor, int type)
785 const char *dev_name = drmGetDeviceName(type);
786 char buf[DRM_NODE_NAME_MAX];
788 mode_t devmode = DRM_DEV_MODE, serv_mode;
791 int isroot = !geteuid();
792 uid_t user = DRM_DEV_UID;
793 gid_t group = DRM_DEV_GID;
799 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
800 drmMsg("drmOpenDevice: node name is %s\n", buf);
802 if (drm_server_info && drm_server_info->get_perms) {
803 drm_server_info->get_perms(&serv_group, &serv_mode);
804 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
805 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
809 if (stat(DRM_DIR_NAME, &st)) {
811 return DRM_ERR_NOT_ROOT;
812 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
813 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
814 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
817 /* Check if the device node exists and create it if necessary. */
818 if (stat(buf, &st)) {
820 return DRM_ERR_NOT_ROOT;
822 mknod(buf, S_IFCHR | devmode, dev);
825 if (drm_server_info && drm_server_info->get_perms) {
826 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
827 chown_check_return(buf, user, group);
831 /* if we modprobed then wait for udev */
835 if (stat(DRM_DIR_NAME, &st)) {
839 if (udev_count == 50)
844 if (stat(buf, &st)) {
848 if (udev_count == 50)
855 fd = open(buf, O_RDWR | O_CLOEXEC);
856 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
857 fd, fd < 0 ? strerror(errno) : "OK");
862 /* Check if the device node is not what we expect it to be, and recreate it
863 * and try again if so.
865 if (st.st_rdev != dev) {
867 return DRM_ERR_NOT_ROOT;
869 mknod(buf, S_IFCHR | devmode, dev);
870 if (drm_server_info && drm_server_info->get_perms) {
871 chown_check_return(buf, user, group);
875 fd = open(buf, O_RDWR | O_CLOEXEC);
876 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
877 fd, fd < 0 ? strerror(errno) : "OK");
881 drmMsg("drmOpenDevice: Open failed\n");
889 * Open the DRM device
891 * \param minor device minor number.
892 * \param create allow to create the device if set.
894 * \return a file descriptor on success, or a negative value on error.
897 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
898 * name from \p minor and opens it.
900 static int drmOpenMinor(int minor, int create, int type)
903 char buf[DRM_NODE_NAME_MAX];
904 const char *dev_name = drmGetDeviceName(type);
907 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
912 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
913 if ((fd = open(buf, O_RDWR | O_CLOEXEC)) >= 0)
920 * Determine whether the DRM kernel driver has been loaded.
922 * \return 1 if the DRM driver is loaded, 0 otherwise.
925 * Determine the presence of the kernel driver by attempting to open the 0
926 * minor and get version information. For backward compatibility with older
927 * Linux implementations, /proc/dri is also checked.
929 drm_public int drmAvailable(void)
931 drmVersionPtr version;
935 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
937 /* Try proc for backward Linux compatibility */
938 if (!access("/proc/dri/0", R_OK))
944 if ((version = drmGetVersion(fd))) {
946 drmFreeVersion(version);
953 static int drmGetMinorBase(int type)
956 case DRM_NODE_PRIMARY:
958 case DRM_NODE_CONTROL:
960 case DRM_NODE_RENDER:
967 static int drmGetMinorType(int major, int minor)
970 char name[SPECNAMELEN];
973 if (!devname_r(makedev(major, minor), S_IFCHR, name, sizeof(name)))
976 if (sscanf(name, "drm/%d", &id) != 1) {
977 // If not in /dev/drm/ we have the type in the name
978 if (sscanf(name, "dri/card%d\n", &id) >= 1)
979 return DRM_NODE_PRIMARY;
980 else if (sscanf(name, "dri/control%d\n", &id) >= 1)
981 return DRM_NODE_CONTROL;
982 else if (sscanf(name, "dri/renderD%d\n", &id) >= 1)
983 return DRM_NODE_RENDER;
989 int type = minor >> 6;
995 case DRM_NODE_PRIMARY:
996 case DRM_NODE_CONTROL:
997 case DRM_NODE_RENDER:
1004 static const char *drmGetMinorName(int type)
1007 case DRM_NODE_PRIMARY:
1008 return DRM_PRIMARY_MINOR_NAME;
1009 case DRM_NODE_CONTROL:
1010 return DRM_CONTROL_MINOR_NAME;
1011 case DRM_NODE_RENDER:
1012 return DRM_RENDER_MINOR_NAME;
1019 * Open the device by bus ID.
1021 * \param busid bus ID.
1022 * \param type device node type.
1024 * \return a file descriptor on success, or a negative value on error.
1027 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
1028 * comparing the device bus ID with the one supplied.
1030 * \sa drmOpenMinor() and drmGetBusid().
1032 static int drmOpenByBusid(const char *busid, int type)
1034 int i, pci_domain_ok = 1;
1038 int base = drmGetMinorBase(type);
1043 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
1044 for (i = base; i < base + DRM_MAX_MINOR; i++) {
1045 fd = drmOpenMinor(i, 1, type);
1046 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
1048 /* We need to try for 1.4 first for proper PCI domain support
1049 * and if that fails, we know the kernel is busted
1051 sv.drm_di_major = 1;
1052 sv.drm_di_minor = 4;
1053 sv.drm_dd_major = -1; /* Don't care */
1054 sv.drm_dd_minor = -1; /* Don't care */
1055 if (drmSetInterfaceVersion(fd, &sv)) {
1059 sv.drm_di_major = 1;
1060 sv.drm_di_minor = 1;
1061 sv.drm_dd_major = -1; /* Don't care */
1062 sv.drm_dd_minor = -1; /* Don't care */
1063 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
1064 drmSetInterfaceVersion(fd, &sv);
1066 buf = drmGetBusid(fd);
1067 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
1068 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
1082 * Open the device by name.
1084 * \param name driver name.
1085 * \param type the device node type.
1087 * \return a file descriptor on success, or a negative value on error.
1090 * This function opens the first minor number that matches the driver name and
1091 * isn't already in use. If it's in use it then it will already have a bus ID
1094 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
1096 static int drmOpenByName(const char *name, int type)
1100 drmVersionPtr version;
1102 int base = drmGetMinorBase(type);
1108 * Open the first minor number that matches the driver name and isn't
1109 * already in use. If it's in use it will have a busid assigned already.
1111 for (i = base; i < base + DRM_MAX_MINOR; i++) {
1112 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
1113 if ((version = drmGetVersion(fd))) {
1114 if (!strcmp(version->name, name)) {
1115 drmFreeVersion(version);
1116 id = drmGetBusid(fd);
1117 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
1126 drmFreeVersion(version);
1134 /* Backward-compatibility /proc support */
1135 for (i = 0; i < 8; i++) {
1136 char proc_name[64], buf[512];
1137 char *driver, *pt, *devstring;
1140 sprintf(proc_name, "/proc/dri/%d/name", i);
1141 if ((fd = open(proc_name, O_RDONLY)) >= 0) {
1142 retcode = read(fd, buf, sizeof(buf)-1);
1145 buf[retcode-1] = '\0';
1146 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
1148 if (*pt) { /* Device is next */
1150 if (!strcmp(driver, name)) { /* Match */
1151 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
1153 if (*pt) { /* Found busid */
1154 return drmOpenByBusid(++pt, type);
1155 } else { /* No busid */
1156 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
1170 * Open the DRM device.
1172 * Looks up the specified name and bus ID, and opens the device found. The
1173 * entry in /dev/dri is created if necessary and if called by root.
1175 * \param name driver name. Not referenced if bus ID is supplied.
1176 * \param busid bus ID. Zero if not known.
1178 * \return a file descriptor on success, or a negative value on error.
1181 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
1184 drm_public int drmOpen(const char *name, const char *busid)
1186 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
1190 * Open the DRM device with specified type.
1192 * Looks up the specified name and bus ID, and opens the device found. The
1193 * entry in /dev/dri is created if necessary and if called by root.
1195 * \param name driver name. Not referenced if bus ID is supplied.
1196 * \param busid bus ID. Zero if not known.
1197 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
1199 * \return a file descriptor on success, or a negative value on error.
1202 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
1205 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
1207 if (name != NULL && drm_server_info &&
1208 drm_server_info->load_module && !drmAvailable()) {
1209 /* try to load the kernel module */
1210 if (!drm_server_info->load_module(name)) {
1211 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
1217 int fd = drmOpenByBusid(busid, type);
1223 return drmOpenByName(name, type);
1228 drm_public int drmOpenControl(int minor)
1230 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
1233 drm_public int drmOpenRender(int minor)
1235 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
1239 * Free the version information returned by drmGetVersion().
1241 * \param v pointer to the version information.
1244 * It frees the memory pointed by \p %v as well as all the non-null strings
1247 drm_public void drmFreeVersion(drmVersionPtr v)
1259 * Free the non-public version information returned by the kernel.
1261 * \param v pointer to the version information.
1264 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
1265 * the non-null strings pointers in it.
1267 static void drmFreeKernelVersion(drm_version_t *v)
1279 * Copy version information.
1281 * \param d destination pointer.
1282 * \param s source pointer.
1285 * Used by drmGetVersion() to translate the information returned by the ioctl
1286 * interface in a private structure into the public structure counterpart.
1288 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
1290 d->version_major = s->version_major;
1291 d->version_minor = s->version_minor;
1292 d->version_patchlevel = s->version_patchlevel;
1293 d->name_len = s->name_len;
1294 d->name = strdup(s->name);
1295 d->date_len = s->date_len;
1296 d->date = strdup(s->date);
1297 d->desc_len = s->desc_len;
1298 d->desc = strdup(s->desc);
1303 * Query the driver version information.
1305 * \param fd file descriptor.
1307 * \return pointer to a drmVersion structure which should be freed with
1310 * \note Similar information is available via /proc/dri.
1313 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
1314 * first with zeros to get the string lengths, and then the actually strings.
1315 * It also null-terminates them since they might not be already.
1317 drm_public drmVersionPtr drmGetVersion(int fd)
1319 drmVersionPtr retval;
1320 drm_version_t *version = drmMalloc(sizeof(*version));
1322 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
1323 drmFreeKernelVersion(version);
1327 if (version->name_len)
1328 version->name = drmMalloc(version->name_len + 1);
1329 if (version->date_len)
1330 version->date = drmMalloc(version->date_len + 1);
1331 if (version->desc_len)
1332 version->desc = drmMalloc(version->desc_len + 1);
1334 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
1335 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
1336 drmFreeKernelVersion(version);
1340 /* The results might not be null-terminated strings, so terminate them. */
1341 if (version->name_len) version->name[version->name_len] = '\0';
1342 if (version->date_len) version->date[version->date_len] = '\0';
1343 if (version->desc_len) version->desc[version->desc_len] = '\0';
1345 retval = drmMalloc(sizeof(*retval));
1346 drmCopyVersion(retval, version);
1347 drmFreeKernelVersion(version);
1353 * Get version information for the DRM user space library.
1355 * This version number is driver independent.
1357 * \param fd file descriptor.
1359 * \return version information.
1362 * This function allocates and fills a drm_version structure with a hard coded
1365 drm_public drmVersionPtr drmGetLibVersion(int fd)
1367 drm_version_t *version = drmMalloc(sizeof(*version));
1370 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
1371 * revision 1.0.x = original DRM interface with no drmGetLibVersion
1372 * entry point and many drm<Device> extensions
1373 * revision 1.1.x = added drmCommand entry points for device extensions
1374 * added drmGetLibVersion to identify libdrm.a version
1375 * revision 1.2.x = added drmSetInterfaceVersion
1376 * modified drmOpen to handle both busid and name
1377 * revision 1.3.x = added server + memory manager
1379 version->version_major = 1;
1380 version->version_minor = 3;
1381 version->version_patchlevel = 0;
1383 return (drmVersionPtr)version;
1386 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
1388 struct drm_get_cap cap;
1392 cap.capability = capability;
1394 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
1402 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
1404 struct drm_set_client_cap cap;
1407 cap.capability = capability;
1410 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
1414 * Free the bus ID information.
1416 * \param busid bus ID information string as given by drmGetBusid().
1419 * This function is just frees the memory pointed by \p busid.
1421 drm_public void drmFreeBusid(const char *busid)
1423 drmFree((void *)busid);
1428 * Get the bus ID of the device.
1430 * \param fd file descriptor.
1432 * \return bus ID string.
1435 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
1436 * get the string length and data, passing the arguments in a drm_unique
1439 drm_public char *drmGetBusid(int fd)
1445 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
1447 u.unique = drmMalloc(u.unique_len + 1);
1448 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
1452 u.unique[u.unique_len] = '\0';
1459 * Set the bus ID of the device.
1461 * \param fd file descriptor.
1462 * \param busid bus ID string.
1464 * \return zero on success, negative on failure.
1467 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1468 * the arguments in a drm_unique structure.
1470 drm_public int drmSetBusid(int fd, const char *busid)
1475 u.unique = (char *)busid;
1476 u.unique_len = strlen(busid);
1478 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1484 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1491 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1493 *magic = auth.magic;
1497 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1503 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1509 * Specifies a range of memory that is available for mapping by a
1512 * \param fd file descriptor.
1513 * \param offset usually the physical address. The actual meaning depends of
1514 * the \p type parameter. See below.
1515 * \param size of the memory in bytes.
1516 * \param type type of the memory to be mapped.
1517 * \param flags combination of several flags to modify the function actions.
1518 * \param handle will be set to a value that may be used as the offset
1519 * parameter for mmap().
1521 * \return zero on success or a negative value on error.
1523 * \par Mapping the frame buffer
1524 * For the frame buffer
1525 * - \p offset will be the physical address of the start of the frame buffer,
1526 * - \p size will be the size of the frame buffer in bytes, and
1527 * - \p type will be DRM_FRAME_BUFFER.
1530 * The area mapped will be uncached. If MTRR support is available in the
1531 * kernel, the frame buffer area will be set to write combining.
1533 * \par Mapping the MMIO register area
1534 * For the MMIO register area,
1535 * - \p offset will be the physical address of the start of the register area,
1536 * - \p size will be the size of the register area bytes, and
1537 * - \p type will be DRM_REGISTERS.
1539 * The area mapped will be uncached.
1541 * \par Mapping the SAREA
1543 * - \p offset will be ignored and should be set to zero,
1544 * - \p size will be the desired size of the SAREA in bytes,
1545 * - \p type will be DRM_SHM.
1548 * A shared memory area of the requested size will be created and locked in
1549 * kernel memory. This area may be mapped into client-space by using the handle
1552 * \note May only be called by root.
1555 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1556 * the arguments in a drm_map structure.
1558 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1559 drmMapFlags flags, drm_handle_t *handle)
1564 map.offset = offset;
1566 map.type = (enum drm_map_type)type;
1567 map.flags = (enum drm_map_flags)flags;
1568 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1571 *handle = (drm_handle_t)(uintptr_t)map.handle;
1575 drm_public int drmRmMap(int fd, drm_handle_t handle)
1580 map.handle = (void *)(uintptr_t)handle;
1582 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1588 * Make buffers available for DMA transfers.
1590 * \param fd file descriptor.
1591 * \param count number of buffers.
1592 * \param size size of each buffer.
1593 * \param flags buffer allocation flags.
1594 * \param agp_offset offset in the AGP aperture
1596 * \return number of buffers allocated, negative on error.
1599 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1603 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1606 drm_buf_desc_t request;
1609 request.count = count;
1610 request.size = size;
1611 request.flags = (int)flags;
1612 request.agp_start = agp_offset;
1614 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1616 return request.count;
1619 drm_public int drmMarkBufs(int fd, double low, double high)
1621 drm_buf_info_t info;
1626 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1632 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1635 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1636 int retval = -errno;
1641 for (i = 0; i < info.count; i++) {
1642 info.list[i].low_mark = low * info.list[i].count;
1643 info.list[i].high_mark = high * info.list[i].count;
1644 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1645 int retval = -errno;
1658 * \param fd file descriptor.
1659 * \param count number of buffers to free.
1660 * \param list list of buffers to be freed.
1662 * \return zero on success, or a negative value on failure.
1664 * \note This function is primarily used for debugging.
1667 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1668 * the arguments in a drm_buf_free structure.
1670 drm_public int drmFreeBufs(int fd, int count, int *list)
1672 drm_buf_free_t request;
1675 request.count = count;
1676 request.list = list;
1677 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1686 * \param fd file descriptor.
1689 * This function closes the file descriptor.
1691 drm_public int drmClose(int fd)
1693 unsigned long key = drmGetKeyFromFd(fd);
1694 drmHashEntry *entry = drmGetEntry(fd);
1696 drmHashDestroy(entry->tagTable);
1699 entry->tagTable = NULL;
1701 drmHashDelete(drmHashTable, key);
1709 * Map a region of memory.
1711 * \param fd file descriptor.
1712 * \param handle handle returned by drmAddMap().
1713 * \param size size in bytes. Must match the size used by drmAddMap().
1714 * \param address will contain the user-space virtual address where the mapping
1717 * \return zero on success, or a negative value on failure.
1720 * This function is a wrapper for mmap().
1722 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1723 drmAddressPtr address)
1725 static unsigned long pagesize_mask = 0;
1731 pagesize_mask = getpagesize() - 1;
1733 size = (size + pagesize_mask) & ~pagesize_mask;
1735 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1736 if (*address == MAP_FAILED)
1743 * Unmap mappings obtained with drmMap().
1745 * \param address address as given by drmMap().
1746 * \param size size in bytes. Must match the size used by drmMap().
1748 * \return zero on success, or a negative value on failure.
1751 * This function is a wrapper for munmap().
1753 drm_public int drmUnmap(drmAddress address, drmSize size)
1755 return drm_munmap(address, size);
1758 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1760 drm_buf_info_t info;
1761 drmBufInfoPtr retval;
1766 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1770 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1773 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1778 retval = drmMalloc(sizeof(*retval));
1779 retval->count = info.count;
1780 if (!(retval->list = drmMalloc(info.count * sizeof(*retval->list)))) {
1786 for (i = 0; i < info.count; i++) {
1787 retval->list[i].count = info.list[i].count;
1788 retval->list[i].size = info.list[i].size;
1789 retval->list[i].low_mark = info.list[i].low_mark;
1790 retval->list[i].high_mark = info.list[i].high_mark;
1799 * Map all DMA buffers into client-virtual space.
1801 * \param fd file descriptor.
1803 * \return a pointer to a ::drmBufMap structure.
1805 * \note The client may not use these buffers until obtaining buffer indices
1809 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1810 * information about the buffers in a drm_buf_map structure into the
1811 * client-visible data structures.
1813 drm_public drmBufMapPtr drmMapBufs(int fd)
1816 drmBufMapPtr retval;
1820 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1826 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1829 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1834 retval = drmMalloc(sizeof(*retval));
1835 retval->count = bufs.count;
1836 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1837 for (i = 0; i < bufs.count; i++) {
1838 retval->list[i].idx = bufs.list[i].idx;
1839 retval->list[i].total = bufs.list[i].total;
1840 retval->list[i].used = 0;
1841 retval->list[i].address = bufs.list[i].address;
1850 * Unmap buffers allocated with drmMapBufs().
1852 * \return zero on success, or negative value on failure.
1855 * Calls munmap() for every buffer stored in \p bufs and frees the
1856 * memory allocated by drmMapBufs().
1858 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1862 for (i = 0; i < bufs->count; i++) {
1863 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1866 drmFree(bufs->list);
1872 #define DRM_DMA_RETRY 16
1875 * Reserve DMA buffers.
1877 * \param fd file descriptor.
1880 * \return zero on success, or a negative value on failure.
1883 * Assemble the arguments into a drm_dma structure and keeps issuing the
1884 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1886 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1891 dma.context = request->context;
1892 dma.send_count = request->send_count;
1893 dma.send_indices = request->send_list;
1894 dma.send_sizes = request->send_sizes;
1895 dma.flags = (enum drm_dma_flags)request->flags;
1896 dma.request_count = request->request_count;
1897 dma.request_size = request->request_size;
1898 dma.request_indices = request->request_list;
1899 dma.request_sizes = request->request_sizes;
1900 dma.granted_count = 0;
1903 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1904 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1907 request->granted_count = dma.granted_count;
1916 * Obtain heavyweight hardware lock.
1918 * \param fd file descriptor.
1919 * \param context context.
1920 * \param flags flags that determine the state of the hardware when the function
1923 * \return always zero.
1926 * This function translates the arguments into a drm_lock structure and issue
1927 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1929 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1934 lock.context = context;
1936 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1937 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1938 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1939 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1940 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1941 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1943 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1949 * Release the hardware lock.
1951 * \param fd file descriptor.
1952 * \param context context.
1954 * \return zero on success, or a negative value on failure.
1957 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1958 * argument in a drm_lock structure.
1960 drm_public int drmUnlock(int fd, drm_context_t context)
1965 lock.context = context;
1966 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1969 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1973 drm_context_t * retval;
1977 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1983 if (!(list = drmMalloc(res.count * sizeof(*list))))
1985 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1988 res.contexts = list;
1989 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1990 goto err_free_context;
1992 for (i = 0; i < res.count; i++)
1993 retval[i] = list[i].handle;
2006 drm_public void drmFreeReservedContextList(drm_context_t *pt)
2014 * Used by the X server during GLXContext initialization. This causes
2015 * per-context kernel-level resources to be allocated.
2017 * \param fd file descriptor.
2018 * \param handle is set on success. To be used by the client when requesting DMA
2019 * dispatch with drmDMA().
2021 * \return zero on success, or a negative value on failure.
2023 * \note May only be called by root.
2026 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
2027 * argument in a drm_ctx structure.
2029 drm_public int drmCreateContext(int fd, drm_context_t *handle)
2034 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
2036 *handle = ctx.handle;
2040 drm_public int drmSwitchToContext(int fd, drm_context_t context)
2045 ctx.handle = context;
2046 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
2051 drm_public int drmSetContextFlags(int fd, drm_context_t context,
2052 drm_context_tFlags flags)
2057 * Context preserving means that no context switches are done between DMA
2058 * buffers from one context and the next. This is suitable for use in the
2059 * X server (which promises to maintain hardware context), or in the
2060 * client-side library when buffers are swapped on behalf of two threads.
2063 ctx.handle = context;
2064 if (flags & DRM_CONTEXT_PRESERVED)
2065 ctx.flags |= _DRM_CONTEXT_PRESERVED;
2066 if (flags & DRM_CONTEXT_2DONLY)
2067 ctx.flags |= _DRM_CONTEXT_2DONLY;
2068 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
2073 drm_public int drmGetContextFlags(int fd, drm_context_t context,
2074 drm_context_tFlagsPtr flags)
2079 ctx.handle = context;
2080 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
2083 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
2084 *flags |= DRM_CONTEXT_PRESERVED;
2085 if (ctx.flags & _DRM_CONTEXT_2DONLY)
2086 *flags |= DRM_CONTEXT_2DONLY;
2093 * Free any kernel-level resources allocated with drmCreateContext() associated
2096 * \param fd file descriptor.
2097 * \param handle handle given by drmCreateContext().
2099 * \return zero on success, or a negative value on failure.
2101 * \note May only be called by root.
2104 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
2105 * argument in a drm_ctx structure.
2107 drm_public int drmDestroyContext(int fd, drm_context_t handle)
2112 ctx.handle = handle;
2113 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
2118 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
2123 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
2125 *handle = draw.handle;
2129 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
2134 draw.handle = handle;
2135 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
2140 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
2141 drm_drawable_info_type_t type,
2142 unsigned int num, void *data)
2144 drm_update_draw_t update;
2147 update.handle = handle;
2150 update.data = (unsigned long long)(unsigned long)data;
2152 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
2158 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
2161 struct drm_crtc_get_sequence get_seq;
2165 get_seq.crtc_id = crtcId;
2166 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
2171 *sequence = get_seq.sequence;
2173 *ns = get_seq.sequence_ns;
2177 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
2179 uint64_t *sequence_queued,
2182 struct drm_crtc_queue_sequence queue_seq;
2185 memclear(queue_seq);
2186 queue_seq.crtc_id = crtcId;
2187 queue_seq.flags = flags;
2188 queue_seq.sequence = sequence;
2189 queue_seq.user_data = user_data;
2191 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
2192 if (ret == 0 && sequence_queued)
2193 *sequence_queued = queue_seq.sequence;
2199 * Acquire the AGP device.
2201 * Must be called before any of the other AGP related calls.
2203 * \param fd file descriptor.
2205 * \return zero on success, or a negative value on failure.
2208 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
2210 drm_public int drmAgpAcquire(int fd)
2212 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
2219 * Release the AGP device.
2221 * \param fd file descriptor.
2223 * \return zero on success, or a negative value on failure.
2226 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
2228 drm_public int drmAgpRelease(int fd)
2230 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
2239 * \param fd file descriptor.
2240 * \param mode AGP mode.
2242 * \return zero on success, or a negative value on failure.
2245 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
2246 * argument in a drm_agp_mode structure.
2248 drm_public int drmAgpEnable(int fd, unsigned long mode)
2254 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
2261 * Allocate a chunk of AGP memory.
2263 * \param fd file descriptor.
2264 * \param size requested memory size in bytes. Will be rounded to page boundary.
2265 * \param type type of memory to allocate.
2266 * \param address if not zero, will be set to the physical address of the
2268 * \param handle on success will be set to a handle of the allocated memory.
2270 * \return zero on success, or a negative value on failure.
2273 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
2274 * arguments in a drm_agp_buffer structure.
2276 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
2277 unsigned long *address, drm_handle_t *handle)
2282 *handle = DRM_AGP_NO_HANDLE;
2285 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
2288 *address = b.physical;
2295 * Free a chunk of AGP memory.
2297 * \param fd file descriptor.
2298 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
2300 * \return zero on success, or a negative value on failure.
2303 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
2304 * argument in a drm_agp_buffer structure.
2306 drm_public int drmAgpFree(int fd, drm_handle_t handle)
2312 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
2319 * Bind a chunk of AGP memory.
2321 * \param fd file descriptor.
2322 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
2323 * \param offset offset in bytes. It will round to page boundary.
2325 * \return zero on success, or a negative value on failure.
2328 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
2329 * argument in a drm_agp_binding structure.
2331 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
2333 drm_agp_binding_t b;
2338 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
2345 * Unbind a chunk of AGP memory.
2347 * \param fd file descriptor.
2348 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
2350 * \return zero on success, or a negative value on failure.
2353 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
2354 * the argument in a drm_agp_binding structure.
2356 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
2358 drm_agp_binding_t b;
2362 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
2369 * Get AGP driver major version number.
2371 * \param fd file descriptor.
2373 * \return major version number on success, or a negative value on failure..
2376 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2377 * necessary information in a drm_agp_info structure.
2379 drm_public int drmAgpVersionMajor(int fd)
2385 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2387 return i.agp_version_major;
2392 * Get AGP driver minor version number.
2394 * \param fd file descriptor.
2396 * \return minor version number on success, or a negative value on failure.
2399 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2400 * necessary information in a drm_agp_info structure.
2402 drm_public int drmAgpVersionMinor(int fd)
2408 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2410 return i.agp_version_minor;
2417 * \param fd file descriptor.
2419 * \return mode on success, or zero on failure.
2422 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2423 * necessary information in a drm_agp_info structure.
2425 drm_public unsigned long drmAgpGetMode(int fd)
2431 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2438 * Get AGP aperture base.
2440 * \param fd file descriptor.
2442 * \return aperture base on success, zero on failure.
2445 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2446 * necessary information in a drm_agp_info structure.
2448 drm_public unsigned long drmAgpBase(int fd)
2454 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2456 return i.aperture_base;
2461 * Get AGP aperture size.
2463 * \param fd file descriptor.
2465 * \return aperture size on success, zero on failure.
2468 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2469 * necessary information in a drm_agp_info structure.
2471 drm_public unsigned long drmAgpSize(int fd)
2477 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2479 return i.aperture_size;
2484 * Get used AGP memory.
2486 * \param fd file descriptor.
2488 * \return memory used on success, or zero on failure.
2491 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2492 * necessary information in a drm_agp_info structure.
2494 drm_public unsigned long drmAgpMemoryUsed(int fd)
2500 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2502 return i.memory_used;
2507 * Get available AGP memory.
2509 * \param fd file descriptor.
2511 * \return memory available on success, or zero on failure.
2514 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2515 * necessary information in a drm_agp_info structure.
2517 drm_public unsigned long drmAgpMemoryAvail(int fd)
2523 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2525 return i.memory_allowed;
2530 * Get hardware vendor ID.
2532 * \param fd file descriptor.
2534 * \return vendor ID on success, or zero on failure.
2537 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2538 * necessary information in a drm_agp_info structure.
2540 drm_public unsigned int drmAgpVendorId(int fd)
2546 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2553 * Get hardware device ID.
2555 * \param fd file descriptor.
2557 * \return zero on success, or zero on failure.
2560 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2561 * necessary information in a drm_agp_info structure.
2563 drm_public unsigned int drmAgpDeviceId(int fd)
2569 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2574 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2575 drm_handle_t *handle)
2577 drm_scatter_gather_t sg;
2583 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2585 *handle = sg.handle;
2589 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2591 drm_scatter_gather_t sg;
2595 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2603 * \param fd file descriptor.
2604 * \param vbl pointer to a drmVBlank structure.
2606 * \return zero on success, or a negative value on failure.
2609 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2611 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2613 struct timespec timeout, cur;
2616 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2618 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2624 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2625 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2626 if (ret && errno == EINTR) {
2627 clock_gettime(CLOCK_MONOTONIC, &cur);
2628 /* Timeout after 1s */
2629 if (cur.tv_sec > timeout.tv_sec + 1 ||
2630 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2637 } while (ret && errno == EINTR);
2643 drm_public int drmError(int err, const char *label)
2646 case DRM_ERR_NO_DEVICE:
2647 fprintf(stderr, "%s: no device\n", label);
2649 case DRM_ERR_NO_ACCESS:
2650 fprintf(stderr, "%s: no access\n", label);
2652 case DRM_ERR_NOT_ROOT:
2653 fprintf(stderr, "%s: not root\n", label);
2655 case DRM_ERR_INVALID:
2656 fprintf(stderr, "%s: invalid args\n", label);
2661 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2669 * Install IRQ handler.
2671 * \param fd file descriptor.
2672 * \param irq IRQ number.
2674 * \return zero on success, or a negative value on failure.
2677 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2678 * argument in a drm_control structure.
2680 drm_public int drmCtlInstHandler(int fd, int irq)
2685 ctl.func = DRM_INST_HANDLER;
2687 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2694 * Uninstall IRQ handler.
2696 * \param fd file descriptor.
2698 * \return zero on success, or a negative value on failure.
2701 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2702 * argument in a drm_control structure.
2704 drm_public int drmCtlUninstHandler(int fd)
2709 ctl.func = DRM_UNINST_HANDLER;
2711 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2716 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2721 lock.context = context;
2722 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2723 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2724 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2725 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2726 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2727 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2728 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2734 * Get IRQ from bus ID.
2736 * \param fd file descriptor.
2737 * \param busnum bus number.
2738 * \param devnum device number.
2739 * \param funcnum function number.
2741 * \return IRQ number on success, or a negative value on failure.
2744 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2745 * arguments in a drm_irq_busid structure.
2747 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2755 p.funcnum = funcnum;
2756 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2761 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2763 drmHashEntry *entry = drmGetEntry(fd);
2765 if (drmHashInsert(entry->tagTable, context, tag)) {
2766 drmHashDelete(entry->tagTable, context);
2767 drmHashInsert(entry->tagTable, context, tag);
2772 drm_public int drmDelContextTag(int fd, drm_context_t context)
2774 drmHashEntry *entry = drmGetEntry(fd);
2776 return drmHashDelete(entry->tagTable, context);
2779 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2781 drmHashEntry *entry = drmGetEntry(fd);
2784 if (drmHashLookup(entry->tagTable, context, &value))
2790 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2791 drm_handle_t handle)
2793 drm_ctx_priv_map_t map;
2796 map.ctx_id = ctx_id;
2797 map.handle = (void *)(uintptr_t)handle;
2799 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2804 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2805 drm_handle_t *handle)
2807 drm_ctx_priv_map_t map;
2810 map.ctx_id = ctx_id;
2812 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2815 *handle = (drm_handle_t)(uintptr_t)map.handle;
2820 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2821 drmMapType *type, drmMapFlags *flags,
2822 drm_handle_t *handle, int *mtrr)
2828 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2830 *offset = map.offset;
2832 *type = (drmMapType)map.type;
2833 *flags = (drmMapFlags)map.flags;
2834 *handle = (unsigned long)map.handle;
2839 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2840 unsigned long *magic, unsigned long *iocs)
2842 drm_client_t client;
2846 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2848 *auth = client.auth;
2851 *magic = client.magic;
2852 *iocs = client.iocs;
2856 drm_public int drmGetStats(int fd, drmStatsT *stats)
2862 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2866 memset(stats, 0, sizeof(*stats));
2867 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2871 stats->data[i].long_format = "%-20.20s"; \
2872 stats->data[i].rate_format = "%8.8s"; \
2873 stats->data[i].isvalue = 1; \
2874 stats->data[i].verbose = 0
2877 stats->data[i].long_format = "%-20.20s"; \
2878 stats->data[i].rate_format = "%5.5s"; \
2879 stats->data[i].isvalue = 0; \
2880 stats->data[i].mult_names = "kgm"; \
2881 stats->data[i].mult = 1000; \
2882 stats->data[i].verbose = 0
2885 stats->data[i].long_format = "%-20.20s"; \
2886 stats->data[i].rate_format = "%5.5s"; \
2887 stats->data[i].isvalue = 0; \
2888 stats->data[i].mult_names = "KGM"; \
2889 stats->data[i].mult = 1024; \
2890 stats->data[i].verbose = 0
2893 stats->count = s.count;
2894 for (i = 0; i < s.count; i++) {
2895 stats->data[i].value = s.data[i].value;
2896 switch (s.data[i].type) {
2897 case _DRM_STAT_LOCK:
2898 stats->data[i].long_name = "Lock";
2899 stats->data[i].rate_name = "Lock";
2902 case _DRM_STAT_OPENS:
2903 stats->data[i].long_name = "Opens";
2904 stats->data[i].rate_name = "O";
2906 stats->data[i].verbose = 1;
2908 case _DRM_STAT_CLOSES:
2909 stats->data[i].long_name = "Closes";
2910 stats->data[i].rate_name = "Lock";
2912 stats->data[i].verbose = 1;
2914 case _DRM_STAT_IOCTLS:
2915 stats->data[i].long_name = "Ioctls";
2916 stats->data[i].rate_name = "Ioc/s";
2919 case _DRM_STAT_LOCKS:
2920 stats->data[i].long_name = "Locks";
2921 stats->data[i].rate_name = "Lck/s";
2924 case _DRM_STAT_UNLOCKS:
2925 stats->data[i].long_name = "Unlocks";
2926 stats->data[i].rate_name = "Unl/s";
2930 stats->data[i].long_name = "IRQs";
2931 stats->data[i].rate_name = "IRQ/s";
2934 case _DRM_STAT_PRIMARY:
2935 stats->data[i].long_name = "Primary Bytes";
2936 stats->data[i].rate_name = "PB/s";
2939 case _DRM_STAT_SECONDARY:
2940 stats->data[i].long_name = "Secondary Bytes";
2941 stats->data[i].rate_name = "SB/s";
2945 stats->data[i].long_name = "DMA";
2946 stats->data[i].rate_name = "DMA/s";
2949 case _DRM_STAT_SPECIAL:
2950 stats->data[i].long_name = "Special DMA";
2951 stats->data[i].rate_name = "dma/s";
2954 case _DRM_STAT_MISSED:
2955 stats->data[i].long_name = "Miss";
2956 stats->data[i].rate_name = "Ms/s";
2959 case _DRM_STAT_VALUE:
2960 stats->data[i].long_name = "Value";
2961 stats->data[i].rate_name = "Value";
2964 case _DRM_STAT_BYTE:
2965 stats->data[i].long_name = "Bytes";
2966 stats->data[i].rate_name = "B/s";
2969 case _DRM_STAT_COUNT:
2971 stats->data[i].long_name = "Count";
2972 stats->data[i].rate_name = "Cnt/s";
2981 * Issue a set-version ioctl.
2983 * \param fd file descriptor.
2984 * \param drmCommandIndex command index
2985 * \param data source pointer of the data to be read and written.
2986 * \param size size of the data to be read and written.
2988 * \return zero on success, or a negative value on failure.
2991 * It issues a read-write ioctl given by
2992 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2994 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2997 drm_set_version_t sv;
3000 sv.drm_di_major = version->drm_di_major;
3001 sv.drm_di_minor = version->drm_di_minor;
3002 sv.drm_dd_major = version->drm_dd_major;
3003 sv.drm_dd_minor = version->drm_dd_minor;
3005 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
3009 version->drm_di_major = sv.drm_di_major;
3010 version->drm_di_minor = sv.drm_di_minor;
3011 version->drm_dd_major = sv.drm_dd_major;
3012 version->drm_dd_minor = sv.drm_dd_minor;
3018 * Send a device-specific command.
3020 * \param fd file descriptor.
3021 * \param drmCommandIndex command index
3023 * \return zero on success, or a negative value on failure.
3026 * It issues a ioctl given by
3027 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
3029 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
3031 unsigned long request;
3033 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
3035 if (drmIoctl(fd, request, NULL)) {
3043 * Send a device-specific read command.
3045 * \param fd file descriptor.
3046 * \param drmCommandIndex command index
3047 * \param data destination pointer of the data to be read.
3048 * \param size size of the data to be read.
3050 * \return zero on success, or a negative value on failure.
3053 * It issues a read ioctl given by
3054 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
3056 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
3057 void *data, unsigned long size)
3059 unsigned long request;
3061 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
3062 DRM_COMMAND_BASE + drmCommandIndex, size);
3064 if (drmIoctl(fd, request, data)) {
3072 * Send a device-specific write command.
3074 * \param fd file descriptor.
3075 * \param drmCommandIndex command index
3076 * \param data source pointer of the data to be written.
3077 * \param size size of the data to be written.
3079 * \return zero on success, or a negative value on failure.
3082 * It issues a write ioctl given by
3083 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
3085 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
3086 void *data, unsigned long size)
3088 unsigned long request;
3090 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
3091 DRM_COMMAND_BASE + drmCommandIndex, size);
3093 if (drmIoctl(fd, request, data)) {
3101 * Send a device-specific read-write command.
3103 * \param fd file descriptor.
3104 * \param drmCommandIndex command index
3105 * \param data source pointer of the data to be read and written.
3106 * \param size size of the data to be read and written.
3108 * \return zero on success, or a negative value on failure.
3111 * It issues a read-write ioctl given by
3112 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
3114 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
3115 void *data, unsigned long size)
3117 unsigned long request;
3119 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
3120 DRM_COMMAND_BASE + drmCommandIndex, size);
3122 if (drmIoctl(fd, request, data))
3127 #define DRM_MAX_FDS 16
3133 } connection[DRM_MAX_FDS];
3135 static int nr_fds = 0;
3137 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
3139 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
3142 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
3148 for (i = 0; i < nr_fds; i++)
3149 if ((strcmp(BusID, connection[i].BusID) == 0) &&
3150 (connection[i].type == type)) {
3151 connection[i].refcount++;
3153 return connection[i].fd;
3156 fd = drmOpenWithType(NULL, BusID, type);
3157 if (fd < 0 || nr_fds == DRM_MAX_FDS)
3160 connection[nr_fds].BusID = strdup(BusID);
3161 connection[nr_fds].fd = fd;
3162 connection[nr_fds].refcount = 1;
3163 connection[nr_fds].type = type;
3167 fprintf(stderr, "saved connection %d for %s %d\n",
3168 nr_fds, connection[nr_fds].BusID,
3169 strcmp(BusID, connection[nr_fds].BusID));
3176 drm_public void drmCloseOnce(int fd)
3180 for (i = 0; i < nr_fds; i++) {
3181 if (fd == connection[i].fd) {
3182 if (--connection[i].refcount == 0) {
3183 drmClose(connection[i].fd);
3184 free(connection[i].BusID);
3187 connection[i] = connection[nr_fds];
3195 drm_public int drmSetMaster(int fd)
3197 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
3200 drm_public int drmDropMaster(int fd)
3202 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
3205 drm_public int drmIsMaster(int fd)
3207 /* Detect master by attempting something that requires master.
3209 * Authenticating magic tokens requires master and 0 is an
3210 * internal kernel detail which we could use. Attempting this on
3211 * a master fd would fail therefore fail with EINVAL because 0
3214 * A non-master fd will fail with EACCES, as the kernel checks
3215 * for master before attempting to do anything else.
3217 * Since we don't want to leak implementation details, use
3220 return drmAuthMagic(fd, 0) != -EACCES;
3223 drm_public char *drmGetDeviceNameFromFd(int fd)
3230 if (fstat(fd, &sbuf))
3233 maj = major(sbuf.st_rdev);
3234 min = minor(sbuf.st_rdev);
3235 nodetype = drmGetMinorType(maj, min);
3236 return drmGetMinorNameForFD(fd, nodetype);
3243 /* The whole drmOpen thing is a fiasco and we need to find a way
3244 * back to just using open(2). For now, however, lets just make
3245 * things worse with even more ad hoc directory walking code to
3246 * discover the device file name. */
3251 for (i = 0; i < DRM_MAX_MINOR; i++) {
3252 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
3253 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
3256 if (i == DRM_MAX_MINOR)
3259 return strdup(name);
3263 static bool drmNodeIsDRM(int maj, int min)
3269 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
3271 return stat(path, &sbuf) == 0;
3272 #elif defined(__FreeBSD__)
3273 char name[SPECNAMELEN];
3275 if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
3277 /* Handle drm/ and dri/ as both are present in different FreeBSD version
3278 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
3279 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
3280 * only device nodes in /dev/dri/ */
3281 return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
3283 return maj == DRM_MAJOR;
3287 drm_public int drmGetNodeTypeFromFd(int fd)
3292 if (fstat(fd, &sbuf))
3295 maj = major(sbuf.st_rdev);
3296 min = minor(sbuf.st_rdev);
3298 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
3303 type = drmGetMinorType(maj, min);
3309 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
3312 struct drm_prime_handle args;
3317 args.handle = handle;
3319 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
3323 *prime_fd = args.fd;
3327 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
3329 struct drm_prime_handle args;
3334 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
3338 *handle = args.handle;
3342 drm_public int drmCloseBufferHandle(int fd, uint32_t handle)
3344 struct drm_gem_close args;
3347 args.handle = handle;
3348 return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &args);
3351 static char *drmGetMinorNameForFD(int fd, int type)
3357 const char *name = drmGetMinorName(type);
3359 char dev_name[64], buf[64];
3367 if (fstat(fd, &sbuf))
3370 maj = major(sbuf.st_rdev);
3371 min = minor(sbuf.st_rdev);
3373 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3376 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
3378 sysdir = opendir(buf);
3382 while ((ent = readdir(sysdir))) {
3383 if (strncmp(ent->d_name, name, len) == 0) {
3384 if (snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
3389 return strdup(dev_name);
3395 #elif defined(__FreeBSD__)
3397 char dname[SPECNAMELEN];
3399 char name[SPECNAMELEN];
3400 int id, maj, min, nodetype, i;
3402 if (fstat(fd, &sbuf))
3405 maj = major(sbuf.st_rdev);
3406 min = minor(sbuf.st_rdev);
3408 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3411 if (!devname_r(sbuf.st_rdev, S_IFCHR, dname, sizeof(dname)))
3414 /* Handle both /dev/drm and /dev/dri
3415 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
3416 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
3417 * only device nodes in /dev/dri/ */
3419 /* Get the node type represented by fd so we can deduce the target name */
3420 nodetype = drmGetMinorType(maj, min);
3423 mname = drmGetMinorName(type);
3425 for (i = 0; i < SPECNAMELEN; i++) {
3426 if (isalpha(dname[i]) == 0 && dname[i] != '/')
3429 if (dname[i] == '\0')
3432 id = (int)strtol(&dname[i], NULL, 10);
3433 id -= drmGetMinorBase(nodetype);
3434 snprintf(name, sizeof(name), DRM_DIR_NAME "/%s%d", mname,
3435 id + drmGetMinorBase(type));
3437 return strdup(name);
3440 char buf[PATH_MAX + 1];
3441 const char *dev_name = drmGetDeviceName(type);
3442 unsigned int maj, min;
3445 if (fstat(fd, &sbuf))
3448 maj = major(sbuf.st_rdev);
3449 min = minor(sbuf.st_rdev);
3451 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3457 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
3458 if (n == -1 || n >= sizeof(buf))
3465 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
3467 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
3470 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
3472 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
3476 static char * DRM_PRINTFLIKE(2, 3)
3477 sysfs_uevent_get(const char *path, const char *fmt, ...)
3479 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
3480 size_t size = 0, len;
3486 num = vasprintf(&key, fmt, ap);
3490 snprintf(filename, sizeof(filename), "%s/uevent", path);
3492 fp = fopen(filename, "r");
3498 while ((num = getline(&line, &size, fp)) >= 0) {
3499 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
3500 char *start = line + len + 1, *end = line + num - 1;
3505 value = strndup(start, end - start);
3519 /* Little white lie to avoid major rework of the existing code */
3520 #define DRM_BUS_VIRTIO 0x10
3523 static int get_subsystem_type(const char *device_path)
3525 char path[PATH_MAX + 1] = "";
3526 char link[PATH_MAX + 1] = "";
3532 { "/pci", DRM_BUS_PCI },
3533 { "/usb", DRM_BUS_USB },
3534 { "/platform", DRM_BUS_PLATFORM },
3535 { "/spi", DRM_BUS_PLATFORM },
3536 { "/host1x", DRM_BUS_HOST1X },
3537 { "/virtio", DRM_BUS_VIRTIO },
3540 strncpy(path, device_path, PATH_MAX);
3541 strncat(path, "/subsystem", PATH_MAX);
3543 if (readlink(path, link, PATH_MAX) < 0)
3546 name = strrchr(link, '/');
3550 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3551 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3552 return bus_types[i].bus_type;
3559 static int drmParseSubsystemType(int maj, int min)
3562 char path[PATH_MAX + 1] = "";
3563 char real_path[PATH_MAX + 1] = "";
3566 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3568 subsystem_type = get_subsystem_type(path);
3569 /* Try to get the parent (underlying) device type */
3570 if (subsystem_type == DRM_BUS_VIRTIO) {
3571 /* Assume virtio-pci on error */
3572 if (!realpath(path, real_path))
3573 return DRM_BUS_VIRTIO;
3574 strncat(path, "/..", PATH_MAX);
3575 subsystem_type = get_subsystem_type(path);
3576 if (subsystem_type < 0)
3577 return DRM_BUS_VIRTIO;
3579 return subsystem_type;
3580 #elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
3583 #warning "Missing implementation of drmParseSubsystemType"
3590 get_pci_path(int maj, int min, char *pci_path)
3592 char path[PATH_MAX + 1], *term;
3594 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3595 if (!realpath(path, pci_path)) {
3596 strcpy(pci_path, path);
3600 term = strrchr(pci_path, '/');
3601 if (term && strncmp(term, "/virtio", 7) == 0)
3607 static int get_sysctl_pci_bus_info(int maj, int min, drmPciBusInfoPtr info)
3609 char dname[SPECNAMELEN];
3610 char sysctl_name[16];
3611 char sysctl_val[256];
3613 int id, type, nelem;
3614 unsigned int rdev, majmin, domain, bus, dev, func;
3616 rdev = makedev(maj, min);
3617 if (!devname_r(rdev, S_IFCHR, dname, sizeof(dname)))
3620 if (sscanf(dname, "drm/%d\n", &id) != 1)
3622 type = drmGetMinorType(maj, min);
3626 /* BUG: This above section is iffy, since it mandates that a driver will
3627 * create both card and render node.
3628 * If it does not, the next DRM device will create card#X and
3629 * renderD#(128+X)-1.
3630 * This is a possibility in FreeBSD but for now there is no good way for
3631 * obtaining the info.
3634 case DRM_NODE_PRIMARY:
3636 case DRM_NODE_CONTROL:
3639 case DRM_NODE_RENDER:
3646 if (snprintf(sysctl_name, sizeof(sysctl_name), "hw.dri.%d.busid", id) <= 0)
3648 sysctl_len = sizeof(sysctl_val);
3649 if (sysctlbyname(sysctl_name, sysctl_val, &sysctl_len, NULL, 0))
3652 #define bus_fmt "pci:%04x:%02x:%02x.%u"
3654 nelem = sscanf(sysctl_val, bus_fmt, &domain, &bus, &dev, &func);
3657 info->domain = domain;
3666 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3669 unsigned int domain, bus, dev, func;
3670 char pci_path[PATH_MAX + 1], *value;
3673 get_pci_path(maj, min, pci_path);
3675 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3679 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3685 info->domain = domain;
3691 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3692 struct drm_pciinfo pinfo;
3695 type = drmGetMinorType(maj, min);
3699 fd = drmOpenMinor(min, 0, type);
3703 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3709 info->domain = pinfo.domain;
3710 info->bus = pinfo.bus;
3711 info->dev = pinfo.dev;
3712 info->func = pinfo.func;
3715 #elif defined(__FreeBSD__)
3716 return get_sysctl_pci_bus_info(maj, min, info);
3718 #warning "Missing implementation of drmParsePciBusInfo"
3723 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3725 if (a == NULL || b == NULL)
3728 if (a->bustype != b->bustype)
3731 switch (a->bustype) {
3733 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3736 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3738 case DRM_BUS_PLATFORM:
3739 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3741 case DRM_BUS_HOST1X:
3742 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3751 static int drmGetNodeType(const char *name)
3753 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3754 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3755 return DRM_NODE_CONTROL;
3757 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3758 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3759 return DRM_NODE_RENDER;
3761 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3762 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3763 return DRM_NODE_PRIMARY;
3768 static int drmGetMaxNodeName(void)
3770 return sizeof(DRM_DIR_NAME) +
3771 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3772 sizeof(DRM_CONTROL_MINOR_NAME),
3773 sizeof(DRM_RENDER_MINOR_NAME)) +
3774 3 /* length of the node number */;
3778 static int parse_separate_sysfs_files(int maj, int min,
3779 drmPciDeviceInfoPtr device,
3780 bool ignore_revision)
3782 static const char *attrs[] = {
3783 "revision", /* Older kernels are missing the file, so check for it first */
3789 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3790 unsigned int data[ARRAY_SIZE(attrs)];
3794 get_pci_path(maj, min, pci_path);
3796 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3797 if (snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]) < 0)
3800 fp = fopen(path, "r");
3804 ret = fscanf(fp, "%x", &data[i]);
3811 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3812 device->vendor_id = data[1] & 0xffff;
3813 device->device_id = data[2] & 0xffff;
3814 device->subvendor_id = data[3] & 0xffff;
3815 device->subdevice_id = data[4] & 0xffff;
3820 static int parse_config_sysfs_file(int maj, int min,
3821 drmPciDeviceInfoPtr device)
3823 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3824 unsigned char config[64];
3827 get_pci_path(maj, min, pci_path);
3829 if (snprintf(path, PATH_MAX, "%s/config", pci_path) < 0)
3832 fd = open(path, O_RDONLY);
3836 ret = read(fd, config, sizeof(config));
3841 device->vendor_id = config[0] | (config[1] << 8);
3842 device->device_id = config[2] | (config[3] << 8);
3843 device->revision_id = config[8];
3844 device->subvendor_id = config[44] | (config[45] << 8);
3845 device->subdevice_id = config[46] | (config[47] << 8);
3851 static int drmParsePciDeviceInfo(int maj, int min,
3852 drmPciDeviceInfoPtr device,
3856 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3857 return parse_separate_sysfs_files(maj, min, device, true);
3859 if (parse_separate_sysfs_files(maj, min, device, false))
3860 return parse_config_sysfs_file(maj, min, device);
3863 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3864 struct drm_pciinfo pinfo;
3867 type = drmGetMinorType(maj, min);
3871 fd = drmOpenMinor(min, 0, type);
3875 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3881 device->vendor_id = pinfo.vendor_id;
3882 device->device_id = pinfo.device_id;
3883 device->revision_id = pinfo.revision_id;
3884 device->subvendor_id = pinfo.subvendor_id;
3885 device->subdevice_id = pinfo.subdevice_id;
3888 #elif defined(__FreeBSD__)
3890 struct pci_conf_io pc;
3891 struct pci_match_conf patterns[1];
3892 struct pci_conf results[1];
3895 if (get_sysctl_pci_bus_info(maj, min, &info) != 0)
3898 fd = open("/dev/pci", O_RDONLY);
3902 bzero(&patterns, sizeof(patterns));
3903 patterns[0].pc_sel.pc_domain = info.domain;
3904 patterns[0].pc_sel.pc_bus = info.bus;
3905 patterns[0].pc_sel.pc_dev = info.dev;
3906 patterns[0].pc_sel.pc_func = info.func;
3907 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS
3908 | PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC;
3909 bzero(&pc, sizeof(struct pci_conf_io));
3910 pc.num_patterns = 1;
3911 pc.pat_buf_len = sizeof(patterns);
3912 pc.patterns = patterns;
3913 pc.match_buf_len = sizeof(results);
3914 pc.matches = results;
3916 if (ioctl(fd, PCIOCGETCONF, &pc) || pc.status == PCI_GETCONF_ERROR) {
3923 device->vendor_id = results[0].pc_vendor;
3924 device->device_id = results[0].pc_device;
3925 device->subvendor_id = results[0].pc_subvendor;
3926 device->subdevice_id = results[0].pc_subdevice;
3927 device->revision_id = results[0].pc_revid;
3931 #warning "Missing implementation of drmParsePciDeviceInfo"
3936 static void drmFreePlatformDevice(drmDevicePtr device)
3938 if (device->deviceinfo.platform) {
3939 if (device->deviceinfo.platform->compatible) {
3940 char **compatible = device->deviceinfo.platform->compatible;
3942 while (*compatible) {
3947 free(device->deviceinfo.platform->compatible);
3952 static void drmFreeHost1xDevice(drmDevicePtr device)
3954 if (device->deviceinfo.host1x) {
3955 if (device->deviceinfo.host1x->compatible) {
3956 char **compatible = device->deviceinfo.host1x->compatible;
3958 while (*compatible) {
3963 free(device->deviceinfo.host1x->compatible);
3968 drm_public void drmFreeDevice(drmDevicePtr *device)
3974 switch ((*device)->bustype) {
3975 case DRM_BUS_PLATFORM:
3976 drmFreePlatformDevice(*device);
3979 case DRM_BUS_HOST1X:
3980 drmFreeHost1xDevice(*device);
3989 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3993 if (devices == NULL)
3996 for (i = 0; i < count; i++)
3998 drmFreeDevice(&devices[i]);
4001 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
4002 size_t bus_size, size_t device_size,
4005 size_t max_node_length, extra, size;
4006 drmDevicePtr device;
4010 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
4011 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
4013 size = sizeof(*device) + extra + bus_size + device_size;
4015 device = calloc(1, size);
4019 device->available_nodes = 1 << type;
4021 ptr = (char *)device + sizeof(*device);
4022 device->nodes = (char **)ptr;
4024 ptr += DRM_NODE_MAX * sizeof(void *);
4026 for (i = 0; i < DRM_NODE_MAX; i++) {
4027 device->nodes[i] = ptr;
4028 ptr += max_node_length;
4031 memcpy(device->nodes[type], node, max_node_length);
4038 static int drmProcessPciDevice(drmDevicePtr *device,
4039 const char *node, int node_type,
4040 int maj, int min, bool fetch_deviceinfo,
4047 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
4048 sizeof(drmPciDeviceInfo), &addr);
4052 dev->bustype = DRM_BUS_PCI;
4054 dev->businfo.pci = (drmPciBusInfoPtr)addr;
4056 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
4060 // Fetch the device info if the user has requested it
4061 if (fetch_deviceinfo) {
4062 addr += sizeof(drmPciBusInfo);
4063 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
4065 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
4080 static int drm_usb_dev_path(int maj, int min, char *path, size_t len)
4082 char *value, *tmp_path, *slash;
4083 bool usb_device, usb_interface;
4085 snprintf(path, len, "/sys/dev/char/%d:%d/device", maj, min);
4087 value = sysfs_uevent_get(path, "DEVTYPE");
4091 usb_device = strcmp(value, "usb_device") == 0;
4092 usb_interface = strcmp(value, "usb_interface") == 0;
4100 /* The parent of a usb_interface is a usb_device */
4102 tmp_path = realpath(path, NULL);
4106 slash = strrchr(tmp_path, '/');
4114 if (snprintf(path, len, "%s", tmp_path) >= (int)len) {
4124 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
4127 char path[PATH_MAX + 1], *value;
4128 unsigned int bus, dev;
4131 ret = drm_usb_dev_path(maj, min, path, sizeof(path));
4135 value = sysfs_uevent_get(path, "BUSNUM");
4139 ret = sscanf(value, "%03u", &bus);
4145 value = sysfs_uevent_get(path, "DEVNUM");
4149 ret = sscanf(value, "%03u", &dev);
4160 #warning "Missing implementation of drmParseUsbBusInfo"
4165 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
4168 char path[PATH_MAX + 1], *value;
4169 unsigned int vendor, product;
4172 ret = drm_usb_dev_path(maj, min, path, sizeof(path));
4176 value = sysfs_uevent_get(path, "PRODUCT");
4180 ret = sscanf(value, "%x/%x", &vendor, &product);
4186 info->vendor = vendor;
4187 info->product = product;
4191 #warning "Missing implementation of drmParseUsbDeviceInfo"
4196 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
4197 int node_type, int maj, int min,
4198 bool fetch_deviceinfo, uint32_t flags)
4204 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
4205 sizeof(drmUsbDeviceInfo), &ptr);
4209 dev->bustype = DRM_BUS_USB;
4211 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
4213 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
4217 if (fetch_deviceinfo) {
4218 ptr += sizeof(drmUsbBusInfo);
4219 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
4221 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
4235 static int drmParseOFBusInfo(int maj, int min, char *fullname)
4238 char path[PATH_MAX + 1], *name, *tmp_name;
4240 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
4242 name = sysfs_uevent_get(path, "OF_FULLNAME");
4245 /* If the device lacks OF data, pick the MODALIAS info */
4246 name = sysfs_uevent_get(path, "MODALIAS");
4250 /* .. and strip the MODALIAS=[platform,usb...]: part. */
4251 tmp_name = strrchr(name, ':');
4259 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
4260 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
4265 #warning "Missing implementation of drmParseOFBusInfo"
4270 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
4273 char path[PATH_MAX + 1], *value, *tmp_name;
4274 unsigned int count, i;
4277 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
4279 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
4281 sscanf(value, "%u", &count);
4284 /* Assume one entry if the device lack OF data */
4288 *compatible = calloc(count + 1, sizeof(char *));
4292 for (i = 0; i < count; i++) {
4293 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
4296 /* If the device lacks OF data, pick the MODALIAS info */
4297 value = sysfs_uevent_get(path, "MODALIAS");
4303 /* .. and strip the MODALIAS=[platform,usb...]: part. */
4304 tmp_name = strrchr(value, ':');
4309 tmp_name = strdup(tmp_name + 1);
4313 (*compatible)[i] = tmp_name;
4320 free((*compatible)[i]);
4325 #warning "Missing implementation of drmParseOFDeviceInfo"
4330 static int drmProcessPlatformDevice(drmDevicePtr *device,
4331 const char *node, int node_type,
4332 int maj, int min, bool fetch_deviceinfo,
4339 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
4340 sizeof(drmPlatformDeviceInfo), &ptr);
4344 dev->bustype = DRM_BUS_PLATFORM;
4346 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
4348 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
4352 if (fetch_deviceinfo) {
4353 ptr += sizeof(drmPlatformBusInfo);
4354 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
4356 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
4370 static int drmProcessHost1xDevice(drmDevicePtr *device,
4371 const char *node, int node_type,
4372 int maj, int min, bool fetch_deviceinfo,
4379 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
4380 sizeof(drmHost1xDeviceInfo), &ptr);
4384 dev->bustype = DRM_BUS_HOST1X;
4386 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
4388 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
4392 if (fetch_deviceinfo) {
4393 ptr += sizeof(drmHost1xBusInfo);
4394 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
4396 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
4411 process_device(drmDevicePtr *device, const char *d_name,
4412 int req_subsystem_type,
4413 bool fetch_deviceinfo, uint32_t flags)
4416 char node[PATH_MAX + 1];
4417 int node_type, subsystem_type;
4418 unsigned int maj, min;
4420 node_type = drmGetNodeType(d_name);
4424 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
4425 if (stat(node, &sbuf))
4428 maj = major(sbuf.st_rdev);
4429 min = minor(sbuf.st_rdev);
4431 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4434 subsystem_type = drmParseSubsystemType(maj, min);
4435 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
4438 switch (subsystem_type) {
4440 case DRM_BUS_VIRTIO:
4441 return drmProcessPciDevice(device, node, node_type, maj, min,
4442 fetch_deviceinfo, flags);
4444 return drmProcessUsbDevice(device, node, node_type, maj, min,
4445 fetch_deviceinfo, flags);
4446 case DRM_BUS_PLATFORM:
4447 return drmProcessPlatformDevice(device, node, node_type, maj, min,
4448 fetch_deviceinfo, flags);
4449 case DRM_BUS_HOST1X:
4450 return drmProcessHost1xDevice(device, node, node_type, maj, min,
4451 fetch_deviceinfo, flags);
4457 /* Consider devices located on the same bus as duplicate and fold the respective
4458 * entries into a single one.
4460 * Note: this leaves "gaps" in the array, while preserving the length.
4462 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
4464 int node_type, i, j;
4466 for (i = 0; i < count; i++) {
4467 for (j = i + 1; j < count; j++) {
4468 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
4469 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
4470 node_type = log2_int(local_devices[j]->available_nodes);
4471 memcpy(local_devices[i]->nodes[node_type],
4472 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
4473 drmFreeDevice(&local_devices[j]);
4479 /* Check that the given flags are valid returning 0 on success */
4481 drm_device_validate_flags(uint32_t flags)
4483 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
4487 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
4491 for (int i = 0; i < DRM_NODE_MAX; i++) {
4492 if (device->available_nodes & 1 << i) {
4493 if (stat(device->nodes[i], &sbuf) == 0 &&
4494 sbuf.st_rdev == find_rdev)
4502 * The kernel drm core has a number of places that assume maximum of
4503 * 3x64 devices nodes. That's 64 for each of primary, control and
4504 * render nodes. Rounded it up to 256 for simplicity.
4506 #define MAX_DRM_NODES 256
4509 * Get information about a device from its dev_t identifier
4511 * \param find_rdev dev_t identifier of the device
4512 * \param flags feature/behaviour bitmask
4513 * \param device the address of a drmDevicePtr where the information
4514 * will be allocated in stored
4516 * \return zero on success, negative error code otherwise.
4518 drm_public int drmGetDeviceFromDevId(dev_t find_rdev, uint32_t flags, drmDevicePtr *device)
4522 * DRI device nodes on OpenBSD are not in their own directory, they reside
4523 * in /dev along with a large number of statically generated /dev nodes.
4524 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
4527 char node[PATH_MAX + 1];
4528 const char *dev_name;
4529 int node_type, subsystem_type;
4530 int maj, min, n, ret;
4535 maj = major(find_rdev);
4536 min = minor(find_rdev);
4538 if (!drmNodeIsDRM(maj, min))
4541 node_type = drmGetMinorType(maj, min);
4542 if (node_type == -1)
4545 dev_name = drmGetDeviceName(node_type);
4549 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
4550 if (n == -1 || n >= PATH_MAX)
4552 if (stat(node, &sbuf))
4555 subsystem_type = drmParseSubsystemType(maj, min);
4556 if (subsystem_type != DRM_BUS_PCI)
4559 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
4567 drmDevicePtr local_devices[MAX_DRM_NODES];
4570 struct dirent *dent;
4573 int ret, i, node_count;
4575 if (drm_device_validate_flags(flags))
4581 maj = major(find_rdev);
4582 min = minor(find_rdev);
4584 if (!drmNodeIsDRM(maj, min))
4587 subsystem_type = drmParseSubsystemType(maj, min);
4588 if (subsystem_type < 0)
4589 return subsystem_type;
4591 sysdir = opendir(DRM_DIR_NAME);
4596 while ((dent = readdir(sysdir))) {
4597 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
4601 if (i >= MAX_DRM_NODES) {
4602 fprintf(stderr, "More than %d drm nodes detected. "
4603 "Please report a bug - that should not happen.\n"
4604 "Skipping extra nodes\n", MAX_DRM_NODES);
4607 local_devices[i] = d;
4612 drmFoldDuplicatedDevices(local_devices, node_count);
4616 for (i = 0; i < node_count; i++) {
4617 if (!local_devices[i])
4620 if (drm_device_has_rdev(local_devices[i], find_rdev))
4621 *device = local_devices[i];
4623 drmFreeDevice(&local_devices[i]);
4627 if (*device == NULL)
4634 * Get information about the opened drm device
4636 * \param fd file descriptor of the drm device
4637 * \param flags feature/behaviour bitmask
4638 * \param device the address of a drmDevicePtr where the information
4639 * will be allocated in stored
4641 * \return zero on success, negative error code otherwise.
4643 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
4644 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
4646 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
4653 if (fstat(fd, &sbuf))
4656 if (!S_ISCHR(sbuf.st_mode))
4659 return drmGetDeviceFromDevId(sbuf.st_rdev, flags, device);
4663 * Get information about the opened drm device
4665 * \param fd file descriptor of the drm device
4666 * \param device the address of a drmDevicePtr where the information
4667 * will be allocated in stored
4669 * \return zero on success, negative error code otherwise.
4671 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
4673 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
4677 * Get drm devices on the system
4679 * \param flags feature/behaviour bitmask
4680 * \param devices the array of devices with drmDevicePtr elements
4681 * can be NULL to get the device number first
4682 * \param max_devices the maximum number of devices for the array
4684 * \return on error - negative error code,
4685 * if devices is NULL - total number of devices available on the system,
4686 * alternatively the number of devices stored in devices[], which is
4687 * capped by the max_devices.
4689 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
4690 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
4692 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
4695 drmDevicePtr local_devices[MAX_DRM_NODES];
4696 drmDevicePtr device;
4698 struct dirent *dent;
4699 int ret, i, node_count, device_count;
4701 if (drm_device_validate_flags(flags))
4704 sysdir = opendir(DRM_DIR_NAME);
4709 while ((dent = readdir(sysdir))) {
4710 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
4714 if (i >= MAX_DRM_NODES) {
4715 fprintf(stderr, "More than %d drm nodes detected. "
4716 "Please report a bug - that should not happen.\n"
4717 "Skipping extra nodes\n", MAX_DRM_NODES);
4720 local_devices[i] = device;
4725 drmFoldDuplicatedDevices(local_devices, node_count);
4728 for (i = 0; i < node_count; i++) {
4729 if (!local_devices[i])
4732 if ((devices != NULL) && (device_count < max_devices))
4733 devices[device_count] = local_devices[i];
4735 drmFreeDevice(&local_devices[i]);
4742 if (devices != NULL)
4743 return MIN2(device_count, max_devices);
4745 return device_count;
4749 * Get drm devices on the system
4751 * \param devices the array of devices with drmDevicePtr elements
4752 * can be NULL to get the device number first
4753 * \param max_devices the maximum number of devices for the array
4755 * \return on error - negative error code,
4756 * if devices is NULL - total number of devices available on the system,
4757 * alternatively the number of devices stored in devices[], which is
4758 * capped by the max_devices.
4760 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4762 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4765 drm_public char *drmGetDeviceNameFromFd2(int fd)
4769 char path[PATH_MAX + 1], *value;
4770 unsigned int maj, min;
4772 if (fstat(fd, &sbuf))
4775 maj = major(sbuf.st_rdev);
4776 min = minor(sbuf.st_rdev);
4778 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4781 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4783 value = sysfs_uevent_get(path, "DEVNAME");
4787 snprintf(path, sizeof(path), "/dev/%s", value);
4790 return strdup(path);
4791 #elif defined(__FreeBSD__)
4792 return drmGetDeviceNameFromFd(fd);
4795 char node[PATH_MAX + 1];
4796 const char *dev_name;
4800 if (fstat(fd, &sbuf))
4803 maj = major(sbuf.st_rdev);
4804 min = minor(sbuf.st_rdev);
4806 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4809 node_type = drmGetMinorType(maj, min);
4810 if (node_type == -1)
4813 dev_name = drmGetDeviceName(node_type);
4817 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
4818 if (n == -1 || n >= PATH_MAX)
4821 return strdup(node);
4825 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4827 struct drm_syncobj_create args;
4833 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4836 *handle = args.handle;
4840 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4842 struct drm_syncobj_destroy args;
4845 args.handle = handle;
4846 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4849 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4851 struct drm_syncobj_handle args;
4856 args.handle = handle;
4857 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4864 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4866 struct drm_syncobj_handle args;
4872 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4875 *handle = args.handle;
4879 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4882 struct drm_syncobj_handle args;
4885 args.fd = sync_file_fd;
4886 args.handle = handle;
4887 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4888 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4891 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4894 struct drm_syncobj_handle args;
4899 args.handle = handle;
4900 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4901 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4904 *sync_file_fd = args.fd;
4908 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4909 int64_t timeout_nsec, unsigned flags,
4910 uint32_t *first_signaled)
4912 struct drm_syncobj_wait args;
4916 args.handles = (uintptr_t)handles;
4917 args.timeout_nsec = timeout_nsec;
4918 args.count_handles = num_handles;
4921 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4926 *first_signaled = args.first_signaled;
4930 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4931 uint32_t handle_count)
4933 struct drm_syncobj_array args;
4937 args.handles = (uintptr_t)handles;
4938 args.count_handles = handle_count;
4940 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4944 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4945 uint32_t handle_count)
4947 struct drm_syncobj_array args;
4951 args.handles = (uintptr_t)handles;
4952 args.count_handles = handle_count;
4954 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4958 drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4959 uint64_t *points, uint32_t handle_count)
4961 struct drm_syncobj_timeline_array args;
4965 args.handles = (uintptr_t)handles;
4966 args.points = (uintptr_t)points;
4967 args.count_handles = handle_count;
4969 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4973 drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4974 unsigned num_handles,
4975 int64_t timeout_nsec, unsigned flags,
4976 uint32_t *first_signaled)
4978 struct drm_syncobj_timeline_wait args;
4982 args.handles = (uintptr_t)handles;
4983 args.points = (uintptr_t)points;
4984 args.timeout_nsec = timeout_nsec;
4985 args.count_handles = num_handles;
4988 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4993 *first_signaled = args.first_signaled;
4998 drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4999 uint32_t handle_count)
5001 struct drm_syncobj_timeline_array args;
5005 args.handles = (uintptr_t)handles;
5006 args.points = (uintptr_t)points;
5007 args.count_handles = handle_count;
5009 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
5015 drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
5016 uint32_t handle_count, uint32_t flags)
5018 struct drm_syncobj_timeline_array args;
5021 args.handles = (uintptr_t)handles;
5022 args.points = (uintptr_t)points;
5023 args.count_handles = handle_count;
5026 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
5030 drm_public int drmSyncobjTransfer(int fd,
5031 uint32_t dst_handle, uint64_t dst_point,
5032 uint32_t src_handle, uint64_t src_point,
5035 struct drm_syncobj_transfer args;
5039 args.src_handle = src_handle;
5040 args.dst_handle = dst_handle;
5041 args.src_point = src_point;
5042 args.dst_point = dst_point;
5045 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);
5051 drmGetFormatModifierFromSimpleTokens(uint64_t modifier)
5055 for (i = 0; i < ARRAY_SIZE(drm_format_modifier_table); i++) {
5056 if (drm_format_modifier_table[i].modifier == modifier)
5057 return strdup(drm_format_modifier_table[i].modifier_name);
5063 /** Retrieves a human-readable representation of a vendor (as a string) from
5064 * the format token modifier
5066 * \param modifier the format modifier token
5067 * \return a char pointer to the human-readable form of the vendor. Caller is
5068 * responsible for freeing it.
5071 drmGetFormatModifierVendor(uint64_t modifier)
5074 uint8_t vendor = fourcc_mod_get_vendor(modifier);
5076 for (i = 0; i < ARRAY_SIZE(drm_format_modifier_vendor_table); i++) {
5077 if (drm_format_modifier_vendor_table[i].vendor == vendor)
5078 return strdup(drm_format_modifier_vendor_table[i].vendor_name);
5084 /** Retrieves a human-readable representation string from a format token
5087 * If the dedicated function was not able to extract a valid name or searching
5088 * the format modifier was not in the table, this function would return NULL.
5090 * \param modifier the token format
5091 * \return a malloc'ed string representation of the modifier. Caller is
5092 * responsible for freeing the string returned.
5096 drmGetFormatModifierName(uint64_t modifier)
5098 uint8_t vendorid = fourcc_mod_get_vendor(modifier);
5099 char *modifier_found = NULL;
5102 for (i = 0; i < ARRAY_SIZE(modifier_format_vendor_table); i++) {
5103 if (modifier_format_vendor_table[i].vendor == vendorid)
5104 modifier_found = modifier_format_vendor_table[i].vendor_cb(modifier);
5107 if (!modifier_found)
5108 return drmGetFormatModifierFromSimpleTokens(modifier);
5110 return modifier_found;
5114 * Get a human-readable name for a DRM FourCC format.
5116 * \param format The format.
5117 * \return A malloc'ed string containing the format name. Caller is responsible
5121 drmGetFormatName(uint32_t format)
5127 be = (format & DRM_FORMAT_BIG_ENDIAN) ? "_BE" : "";
5128 format &= ~DRM_FORMAT_BIG_ENDIAN;
5130 if (format == DRM_FORMAT_INVALID)
5131 return strdup("INVALID");
5133 code[0] = (char) ((format >> 0) & 0xFF);
5134 code[1] = (char) ((format >> 8) & 0xFF);
5135 code[2] = (char) ((format >> 16) & 0xFF);
5136 code[3] = (char) ((format >> 24) & 0xFF);
5139 /* Trim spaces at the end */
5140 for (i = 3; i > 0 && code[i] == ' '; i--)
5143 str_size = strlen(code) + strlen(be) + 1;
5144 str = malloc(str_size);
5148 snprintf(str, str_size, "%s%s", code, be);