3 * Header for DRM modesetting interface.
5 * \author Jakob Bornecrantz <wallbraker@gmail.com>
7 * \par Acknowledgements:
8 * Feb 2007, Dave Airlie <airlied@linux.ie>
12 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
13 * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
14 * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
40 #include <sys/ioctl.h>
43 #include <sys/types.h>
45 #include <sys/sysctl.h>
50 #include "libdrm_macros.h"
51 #include "xf86drmMode.h"
54 #include <drm_fourcc.h>
60 #define memclear(s) memset(&s, 0, sizeof(s))
62 #define U642VOID(x) ((void *)(unsigned long)(x))
63 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
65 static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
67 int ret = drmIoctl(fd, cmd, arg);
68 return ret < 0 ? -errno : ret;
75 static void* drmAllocCpy(char *array, int count, int entry_size)
80 if (!count || !array || !entry_size)
83 if (!(r = drmMalloc(count*entry_size)))
86 for (i = 0; i < count; i++)
87 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
93 * A couple of free functions.
96 drm_public void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
104 drm_public void drmModeFreeResources(drmModeResPtr ptr)
111 drmFree(ptr->connectors);
112 drmFree(ptr->encoders);
116 drm_public void drmModeFreeFB(drmModeFBPtr ptr)
121 /* we might add more frees later. */
125 drm_public void drmModeFreeCrtc(drmModeCrtcPtr ptr)
133 drm_public void drmModeFreeConnector(drmModeConnectorPtr ptr)
138 drmFree(ptr->encoders);
139 drmFree(ptr->prop_values);
145 drm_public void drmModeFreeEncoder(drmModeEncoderPtr ptr)
151 * ModeSetting functions.
154 drm_public int drmIsKMS(int fd)
156 struct drm_mode_card_res res = {0};
158 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res) != 0)
161 return res.count_crtcs > 0 && res.count_connectors > 0 && res.count_encoders > 0;
164 drm_public drmModeResPtr drmModeGetResources(int fd)
166 struct drm_mode_card_res res, counts;
171 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
177 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
181 if (res.count_crtcs) {
182 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
183 if (!res.crtc_id_ptr)
186 if (res.count_connectors) {
187 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
188 if (!res.connector_id_ptr)
191 if (res.count_encoders) {
192 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
193 if (!res.encoder_id_ptr)
197 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
200 /* The number of available connectors and etc may have changed with a
201 * hotplug event in between the ioctls, in which case the field is
202 * silently ignored by the kernel.
204 if (counts.count_fbs < res.count_fbs ||
205 counts.count_crtcs < res.count_crtcs ||
206 counts.count_connectors < res.count_connectors ||
207 counts.count_encoders < res.count_encoders)
209 drmFree(U642VOID(res.fb_id_ptr));
210 drmFree(U642VOID(res.crtc_id_ptr));
211 drmFree(U642VOID(res.connector_id_ptr));
212 drmFree(U642VOID(res.encoder_id_ptr));
220 if (!(r = drmMalloc(sizeof(*r))))
223 r->min_width = res.min_width;
224 r->max_width = res.max_width;
225 r->min_height = res.min_height;
226 r->max_height = res.max_height;
227 r->count_fbs = res.count_fbs;
228 r->count_crtcs = res.count_crtcs;
229 r->count_connectors = res.count_connectors;
230 r->count_encoders = res.count_encoders;
232 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
233 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
234 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
235 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
236 if ((res.count_fbs && !r->fbs) ||
237 (res.count_crtcs && !r->crtcs) ||
238 (res.count_connectors && !r->connectors) ||
239 (res.count_encoders && !r->encoders))
243 drmFree(r->connectors);
244 drmFree(r->encoders);
250 drmFree(U642VOID(res.fb_id_ptr));
251 drmFree(U642VOID(res.crtc_id_ptr));
252 drmFree(U642VOID(res.connector_id_ptr));
253 drmFree(U642VOID(res.encoder_id_ptr));
259 drm_public int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
260 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
263 struct drm_mode_fb_cmd f;
272 f.handle = bo_handle;
274 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
281 drm_public int drmModeAddFB2WithModifiers(int fd, uint32_t width,
282 uint32_t height, uint32_t pixel_format, const uint32_t bo_handles[4],
283 const uint32_t pitches[4], const uint32_t offsets[4],
284 const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
286 struct drm_mode_fb_cmd2 f;
292 f.pixel_format = pixel_format;
294 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
295 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
296 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
298 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
300 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
307 drm_public int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
308 uint32_t pixel_format, const uint32_t bo_handles[4],
309 const uint32_t pitches[4], const uint32_t offsets[4],
310 uint32_t *buf_id, uint32_t flags)
312 return drmModeAddFB2WithModifiers(fd, width, height,
313 pixel_format, bo_handles,
314 pitches, offsets, NULL,
318 drm_public int drmModeRmFB(int fd, uint32_t bufferId)
320 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
323 drm_public drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
325 struct drm_mode_fb_cmd info;
331 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
334 if (!(r = drmMalloc(sizeof(*r))))
337 r->fb_id = info.fb_id;
338 r->width = info.width;
339 r->height = info.height;
340 r->pitch = info.pitch;
342 r->handle = info.handle;
343 r->depth = info.depth;
348 drm_public int drmModeDirtyFB(int fd, uint32_t bufferId,
349 drmModeClipPtr clips, uint32_t num_clips)
351 struct drm_mode_fb_dirty_cmd dirty;
354 dirty.fb_id = bufferId;
355 dirty.clips_ptr = VOID2U64(clips);
356 dirty.num_clips = num_clips;
358 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
365 drm_public drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
367 struct drm_mode_crtc crtc;
371 crtc.crtc_id = crtcId;
373 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
380 if (!(r = drmMalloc(sizeof(*r))))
383 r->crtc_id = crtc.crtc_id;
386 r->mode_valid = crtc.mode_valid;
388 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
389 r->width = crtc.mode.hdisplay;
390 r->height = crtc.mode.vdisplay;
392 r->buffer_id = crtc.fb_id;
393 r->gamma_size = crtc.gamma_size;
397 drm_public int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
398 uint32_t x, uint32_t y, uint32_t *connectors, int count,
399 drmModeModeInfoPtr mode)
401 struct drm_mode_crtc crtc;
406 crtc.crtc_id = crtcId;
407 crtc.fb_id = bufferId;
408 crtc.set_connectors_ptr = VOID2U64(connectors);
409 crtc.count_connectors = count;
411 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
415 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
419 * Cursor manipulation
422 drm_public int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle,
423 uint32_t width, uint32_t height)
425 struct drm_mode_cursor arg;
428 arg.flags = DRM_MODE_CURSOR_BO;
429 arg.crtc_id = crtcId;
432 arg.handle = bo_handle;
434 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
437 drm_public int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle,
438 uint32_t width, uint32_t height, int32_t hot_x,
441 struct drm_mode_cursor2 arg;
444 arg.flags = DRM_MODE_CURSOR_BO;
445 arg.crtc_id = crtcId;
448 arg.handle = bo_handle;
452 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
455 drm_public int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
457 struct drm_mode_cursor arg;
460 arg.flags = DRM_MODE_CURSOR_MOVE;
461 arg.crtc_id = crtcId;
465 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
471 drm_public drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
473 struct drm_mode_get_encoder enc;
474 drmModeEncoderPtr r = NULL;
477 enc.encoder_id = encoder_id;
479 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
482 if (!(r = drmMalloc(sizeof(*r))))
485 r->encoder_id = enc.encoder_id;
486 r->crtc_id = enc.crtc_id;
487 r->encoder_type = enc.encoder_type;
488 r->possible_crtcs = enc.possible_crtcs;
489 r->possible_clones = enc.possible_clones;
495 * Connector manipulation
497 static drmModeConnectorPtr
498 _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
500 struct drm_mode_get_connector conn, counts;
501 drmModeConnectorPtr r = NULL;
502 struct drm_mode_modeinfo stack_mode;
505 conn.connector_id = connector_id;
507 conn.count_modes = 1;
508 conn.modes_ptr = VOID2U64(&stack_mode);
511 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
517 if (conn.count_props) {
518 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
521 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
522 if (!conn.prop_values_ptr)
526 if (conn.count_modes) {
527 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
531 conn.count_modes = 1;
532 conn.modes_ptr = VOID2U64(&stack_mode);
535 if (conn.count_encoders) {
536 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
537 if (!conn.encoders_ptr)
541 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
544 /* The number of available connectors and etc may have changed with a
545 * hotplug event in between the ioctls, in which case the field is
546 * silently ignored by the kernel.
548 if (counts.count_props < conn.count_props ||
549 counts.count_modes < conn.count_modes ||
550 counts.count_encoders < conn.count_encoders) {
551 drmFree(U642VOID(conn.props_ptr));
552 drmFree(U642VOID(conn.prop_values_ptr));
553 if (U642VOID(conn.modes_ptr) != &stack_mode)
554 drmFree(U642VOID(conn.modes_ptr));
555 drmFree(U642VOID(conn.encoders_ptr));
560 if(!(r = drmMalloc(sizeof(*r)))) {
564 r->connector_id = conn.connector_id;
565 r->encoder_id = conn.encoder_id;
566 r->connection = conn.connection;
567 r->mmWidth = conn.mm_width;
568 r->mmHeight = conn.mm_height;
569 /* convert subpixel from kernel to userspace */
570 r->subpixel = conn.subpixel + 1;
571 r->count_modes = conn.count_modes;
572 r->count_props = conn.count_props;
573 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
574 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
575 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
576 r->count_encoders = conn.count_encoders;
577 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
578 r->connector_type = conn.connector_type;
579 r->connector_type_id = conn.connector_type_id;
581 if ((r->count_props && !r->props) ||
582 (r->count_props && !r->prop_values) ||
583 (r->count_modes && !r->modes) ||
584 (r->count_encoders && !r->encoders)) {
586 drmFree(r->prop_values);
588 drmFree(r->encoders);
594 drmFree(U642VOID(conn.prop_values_ptr));
595 drmFree(U642VOID(conn.props_ptr));
596 if (U642VOID(conn.modes_ptr) != &stack_mode)
597 drmFree(U642VOID(conn.modes_ptr));
598 drmFree(U642VOID(conn.encoders_ptr));
603 drm_public drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
605 return _drmModeGetConnector(fd, connector_id, 1);
608 drm_public drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
610 return _drmModeGetConnector(fd, connector_id, 0);
613 drm_public int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
615 struct drm_mode_mode_cmd res;
618 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
619 res.connector_id = connector_id;
621 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
624 drm_public int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
626 struct drm_mode_mode_cmd res;
629 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
630 res.connector_id = connector_id;
632 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
635 drm_public drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
637 struct drm_mode_get_property prop;
638 drmModePropertyPtr r;
641 prop.prop_id = property_id;
643 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
646 if (prop.count_values)
647 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
649 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
650 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
652 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
653 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
654 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
657 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
662 if (!(r = drmMalloc(sizeof(*r))))
665 r->prop_id = prop.prop_id;
666 r->count_values = prop.count_values;
668 r->flags = prop.flags;
669 if (prop.count_values)
670 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
671 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
672 r->count_enums = prop.count_enum_blobs;
673 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
674 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
675 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
676 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
677 r->count_blobs = prop.count_enum_blobs;
679 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
680 r->name[DRM_PROP_NAME_LEN-1] = 0;
683 drmFree(U642VOID(prop.values_ptr));
684 drmFree(U642VOID(prop.enum_blob_ptr));
689 drm_public void drmModeFreeProperty(drmModePropertyPtr ptr)
694 drmFree(ptr->values);
696 drmFree(ptr->blob_ids);
700 drm_public drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd,
703 struct drm_mode_get_blob blob;
704 drmModePropertyBlobPtr r;
707 blob.blob_id = blob_id;
709 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
713 blob.data = VOID2U64(drmMalloc(blob.length));
715 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
720 if (!(r = drmMalloc(sizeof(*r))))
723 r->id = blob.blob_id;
724 r->length = blob.length;
725 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
728 drmFree(U642VOID(blob.data));
732 static inline const uint32_t *
733 get_formats_ptr(const struct drm_format_modifier_blob *blob)
735 return (const uint32_t *)(((uint8_t *)blob) + blob->formats_offset);
738 static inline const struct drm_format_modifier *
739 get_modifiers_ptr(const struct drm_format_modifier_blob *blob)
741 return (const struct drm_format_modifier *)(((uint8_t *)blob) +
742 blob->modifiers_offset);
745 static bool _drmModeFormatModifierGetNext(const drmModePropertyBlobRes *blob,
746 drmModeFormatModifierIterator *iter)
748 const struct drm_format_modifier *blob_modifiers, *mod;
749 const struct drm_format_modifier_blob *fmt_mod_blob;
750 const uint32_t *blob_formats;
752 assert(blob && iter);
754 fmt_mod_blob = blob->data;
755 blob_modifiers = get_modifiers_ptr(fmt_mod_blob);
756 blob_formats = get_formats_ptr(fmt_mod_blob);
758 /* fmt_idx and mod_idx designate the number of processed formats
761 if (iter->fmt_idx >= fmt_mod_blob->count_formats ||
762 iter->mod_idx >= fmt_mod_blob->count_modifiers)
765 iter->fmt = blob_formats[iter->fmt_idx];
766 iter->mod = DRM_FORMAT_MOD_INVALID;
768 /* From the latest valid found, get the next valid modifier */
769 while (iter->mod_idx < fmt_mod_blob->count_modifiers) {
770 mod = &blob_modifiers[iter->mod_idx++];
772 /* Check if the format that fmt_idx designates, belongs to
773 * this modifier 64-bit window selected via mod->offset.
775 if (iter->fmt_idx < mod->offset ||
776 iter->fmt_idx >= mod->offset + 64)
778 if (!(mod->formats & (1 << (iter->fmt_idx - mod->offset))))
781 iter->mod = mod->modifier;
785 if (iter->mod_idx == fmt_mod_blob->count_modifiers) {
790 /* Since mod_idx reset, in order for the caller to iterate over
791 * the last modifier of the last format, always return true here
792 * and early return from the next call.
798 * Iterate over formats first and then over modifiers. On each call, iter->fmt
799 * is retained until all associated modifiers are returned. Then, either update
800 * iter->fmt with the next format, or exit if there aren't any left.
802 * NOTE: clients should not make any assumption on mod_idx and fmt_idx values
804 * @blob: valid kernel blob holding formats and modifiers
805 * @iter: input and output iterator data. Iter data must be initialised to zero
806 * @return: false, on error or there aren't any further formats or modifiers left.
807 * true, on success and there are more formats or modifiers.
809 drm_public bool drmModeFormatModifierBlobIterNext(const drmModePropertyBlobRes *blob,
810 drmModeFormatModifierIterator *iter)
812 drmModeFormatModifierIterator tmp;
818 tmp.fmt_idx = iter->fmt_idx;
819 tmp.mod_idx = iter->mod_idx;
821 /* With the current state of things, DRM/KMS drivers are allowed to
822 * construct blobs having formats and no modifiers. Userspace can't
823 * legitimately abort in such cases.
825 * While waiting for the kernel to perhaps disallow formats with no
826 * modifiers in IN_FORMATS blobs, skip the format altogether.
829 has_fmt = _drmModeFormatModifierGetNext(blob, &tmp);
830 if (has_fmt && tmp.mod != DRM_FORMAT_MOD_INVALID)
833 } while (has_fmt && tmp.mod == DRM_FORMAT_MOD_INVALID);
838 drm_public void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
847 drm_public int drmModeConnectorSetProperty(int fd, uint32_t connector_id,
848 uint32_t property_id,
851 struct drm_mode_connector_set_property osp;
854 osp.connector_id = connector_id;
855 osp.prop_id = property_id;
858 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
862 * checks if a modesetting capable driver has attached to the pci id
863 * returns 0 if modesetting supported.
864 * -EINVAL or invalid bus id
865 * -ENOSYS if no modesetting support
867 drm_public int drmCheckModesettingSupported(const char *busid)
869 #if defined (__linux__)
870 char pci_dev_dir[1024];
871 int domain, bus, dev, func;
876 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
880 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
881 domain, bus, dev, func);
883 sysdir = opendir(pci_dev_dir);
885 dent = readdir(sysdir);
887 if (!strncmp(dent->d_name, "controlD", 8)) {
892 dent = readdir(sysdir);
899 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
900 domain, bus, dev, func);
902 sysdir = opendir(pci_dev_dir);
906 dent = readdir(sysdir);
908 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
913 dent = readdir(sysdir);
919 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
922 int i, modesetting, ret;
925 /* How many GPUs do we expect in the machine ? */
926 for (i = 0; i < 10; i++) {
927 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
928 len = sizeof(sbusid);
929 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
935 if (strcmp(sbusid, busid) != 0)
937 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
938 len = sizeof(modesetting);
939 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
940 if (ret == -1 || len != sizeof(modesetting))
942 return (modesetting ? 0 : -ENOSYS);
944 #elif defined(__DragonFly__)
946 #elif defined(__OpenBSD__)
948 struct drm_mode_card_res res;
951 if ((fd = drmOpen(NULL, busid)) < 0)
954 memset(&res, 0, sizeof(struct drm_mode_card_res));
956 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
967 drm_public int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
968 uint16_t *red, uint16_t *green,
971 struct drm_mode_crtc_lut l;
976 l.red = VOID2U64(red);
977 l.green = VOID2U64(green);
978 l.blue = VOID2U64(blue);
980 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
983 drm_public int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
984 uint16_t *red, uint16_t *green,
987 struct drm_mode_crtc_lut l;
992 l.red = VOID2U64(red);
993 l.green = VOID2U64(green);
994 l.blue = VOID2U64(blue);
996 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
999 drm_public int drmHandleEvent(int fd, drmEventContextPtr evctx)
1003 struct drm_event *e;
1004 struct drm_event_vblank *vblank;
1005 struct drm_event_crtc_sequence *seq;
1008 /* The DRM read semantics guarantees that we always get only
1009 * complete events. */
1011 len = read(fd, buffer, sizeof buffer);
1014 if (len < (int)sizeof *e)
1019 e = (struct drm_event *)(buffer + i);
1021 case DRM_EVENT_VBLANK:
1022 if (evctx->version < 1 ||
1023 evctx->vblank_handler == NULL)
1025 vblank = (struct drm_event_vblank *) e;
1026 evctx->vblank_handler(fd,
1030 U642VOID (vblank->user_data));
1032 case DRM_EVENT_FLIP_COMPLETE:
1033 vblank = (struct drm_event_vblank *) e;
1034 user_data = U642VOID (vblank->user_data);
1036 if (evctx->version >= 3 && evctx->page_flip_handler2)
1037 evctx->page_flip_handler2(fd,
1043 else if (evctx->version >= 2 && evctx->page_flip_handler)
1044 evctx->page_flip_handler(fd,
1050 case DRM_EVENT_CRTC_SEQUENCE:
1051 seq = (struct drm_event_crtc_sequence *) e;
1052 if (evctx->version >= 4 && evctx->sequence_handler)
1053 evctx->sequence_handler(fd,
1067 drm_public int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
1068 uint32_t flags, void *user_data)
1070 struct drm_mode_crtc_page_flip flip;
1074 flip.crtc_id = crtc_id;
1075 flip.user_data = VOID2U64(user_data);
1078 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
1081 drm_public int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
1082 uint32_t flags, void *user_data,
1083 uint32_t target_vblank)
1085 struct drm_mode_crtc_page_flip_target flip_target;
1087 memclear(flip_target);
1088 flip_target.fb_id = fb_id;
1089 flip_target.crtc_id = crtc_id;
1090 flip_target.user_data = VOID2U64(user_data);
1091 flip_target.flags = flags;
1092 flip_target.sequence = target_vblank;
1094 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
1097 drm_public int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
1098 uint32_t fb_id, uint32_t flags,
1099 int32_t crtc_x, int32_t crtc_y,
1100 uint32_t crtc_w, uint32_t crtc_h,
1101 uint32_t src_x, uint32_t src_y,
1102 uint32_t src_w, uint32_t src_h)
1104 struct drm_mode_set_plane s;
1107 s.plane_id = plane_id;
1108 s.crtc_id = crtc_id;
1120 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1123 drm_public drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
1125 struct drm_mode_get_plane ovr, counts;
1126 drmModePlanePtr r = 0;
1130 ovr.plane_id = plane_id;
1131 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1136 if (ovr.count_format_types) {
1137 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1139 if (!ovr.format_type_ptr)
1143 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1146 if (counts.count_format_types < ovr.count_format_types) {
1147 drmFree(U642VOID(ovr.format_type_ptr));
1151 if (!(r = drmMalloc(sizeof(*r))))
1154 r->count_formats = ovr.count_format_types;
1155 r->plane_id = ovr.plane_id;
1156 r->crtc_id = ovr.crtc_id;
1157 r->fb_id = ovr.fb_id;
1158 r->possible_crtcs = ovr.possible_crtcs;
1159 r->gamma_size = ovr.gamma_size;
1160 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1161 ovr.count_format_types, sizeof(uint32_t));
1162 if (ovr.count_format_types && !r->formats) {
1163 drmFree(r->formats);
1169 drmFree(U642VOID(ovr.format_type_ptr));
1174 drm_public void drmModeFreePlane(drmModePlanePtr ptr)
1179 drmFree(ptr->formats);
1183 drm_public drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1185 struct drm_mode_get_plane_res res, counts;
1186 drmModePlaneResPtr r = 0;
1190 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1195 if (res.count_planes) {
1196 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1198 if (!res.plane_id_ptr)
1202 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1205 if (counts.count_planes < res.count_planes) {
1206 drmFree(U642VOID(res.plane_id_ptr));
1210 if (!(r = drmMalloc(sizeof(*r))))
1213 r->count_planes = res.count_planes;
1214 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1215 res.count_planes, sizeof(uint32_t));
1216 if (res.count_planes && !r->planes) {
1223 drmFree(U642VOID(res.plane_id_ptr));
1228 drm_public void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1233 drmFree(ptr->planes);
1237 drm_public drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1239 uint32_t object_type)
1241 struct drm_mode_obj_get_properties properties;
1242 drmModeObjectPropertiesPtr ret = NULL;
1246 memclear(properties);
1247 properties.obj_id = object_id;
1248 properties.obj_type = object_type;
1250 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1253 count = properties.count_props;
1256 properties.props_ptr = VOID2U64(drmMalloc(count *
1258 if (!properties.props_ptr)
1260 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1262 if (!properties.prop_values_ptr)
1266 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1269 if (count < properties.count_props) {
1270 drmFree(U642VOID(properties.props_ptr));
1271 drmFree(U642VOID(properties.prop_values_ptr));
1274 count = properties.count_props;
1276 ret = drmMalloc(sizeof(*ret));
1280 ret->count_props = count;
1281 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1282 count, sizeof(uint32_t));
1283 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1284 count, sizeof(uint64_t));
1285 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1286 drmFree(ret->props);
1287 drmFree(ret->prop_values);
1293 drmFree(U642VOID(properties.props_ptr));
1294 drmFree(U642VOID(properties.prop_values_ptr));
1298 drm_public void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1302 drmFree(ptr->props);
1303 drmFree(ptr->prop_values);
1307 drm_public int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1308 uint32_t property_id, uint64_t value)
1310 struct drm_mode_obj_set_property prop;
1314 prop.prop_id = property_id;
1315 prop.obj_id = object_id;
1316 prop.obj_type = object_type;
1318 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1321 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1323 struct _drmModeAtomicReqItem {
1325 uint32_t property_id;
1330 struct _drmModeAtomicReq {
1332 uint32_t size_items;
1333 drmModeAtomicReqItemPtr items;
1336 drm_public drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1338 drmModeAtomicReqPtr req;
1340 req = drmMalloc(sizeof *req);
1346 req->size_items = 0;
1351 drm_public drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1353 drmModeAtomicReqPtr new;
1358 new = drmMalloc(sizeof *new);
1362 new->cursor = old->cursor;
1363 new->size_items = old->size_items;
1365 if (old->size_items) {
1366 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1371 memcpy(new->items, old->items,
1372 old->cursor * sizeof(*new->items));
1380 drm_public int drmModeAtomicMerge(drmModeAtomicReqPtr base,
1381 drmModeAtomicReqPtr augment)
1388 if (!augment || augment->cursor == 0)
1391 if (base->cursor + augment->cursor >= base->size_items) {
1392 drmModeAtomicReqItemPtr new;
1393 int saved_size = base->size_items;
1395 base->size_items = base->cursor + augment->cursor;
1396 new = realloc(base->items,
1397 base->size_items * sizeof(*base->items));
1399 base->size_items = saved_size;
1405 memcpy(&base->items[base->cursor], augment->items,
1406 augment->cursor * sizeof(*augment->items));
1407 for (i = base->cursor; i < base->cursor + augment->cursor; i++)
1408 base->items[i].cursor = i;
1409 base->cursor += augment->cursor;
1414 drm_public int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1421 drm_public void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1424 req->cursor = cursor;
1427 drm_public int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1429 uint32_t property_id,
1435 if (object_id == 0 || property_id == 0)
1438 if (req->cursor >= req->size_items) {
1439 const uint32_t item_size_inc = getpagesize() / sizeof(*req->items);
1440 drmModeAtomicReqItemPtr new;
1442 req->size_items += item_size_inc;
1443 new = realloc(req->items, req->size_items * sizeof(*req->items));
1445 req->size_items -= item_size_inc;
1451 req->items[req->cursor].object_id = object_id;
1452 req->items[req->cursor].property_id = property_id;
1453 req->items[req->cursor].value = value;
1454 req->items[req->cursor].cursor = req->cursor;
1460 drm_public void drmModeAtomicFree(drmModeAtomicReqPtr req)
1466 drmFree(req->items);
1470 static int sort_req_list(const void *misc, const void *other)
1472 const drmModeAtomicReqItem *first = misc;
1473 const drmModeAtomicReqItem *second = other;
1475 if (first->object_id != second->object_id)
1476 return first->object_id - second->object_id;
1477 else if (first->property_id != second->property_id)
1478 return first->property_id - second->property_id;
1480 return first->cursor - second->cursor;
1483 drm_public int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req,
1484 uint32_t flags, void *user_data)
1486 drmModeAtomicReqPtr sorted;
1487 struct drm_mode_atomic atomic;
1488 uint32_t *objs_ptr = NULL;
1489 uint32_t *count_props_ptr = NULL;
1490 uint32_t *props_ptr = NULL;
1491 uint64_t *prop_values_ptr = NULL;
1492 uint32_t last_obj_id = 0;
1500 if (req->cursor == 0)
1503 sorted = drmModeAtomicDuplicate(req);
1509 /* Sort the list by object ID, then by property ID. */
1510 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1513 /* Now the list is sorted, eliminate duplicate property sets. */
1514 for (i = 0; i < sorted->cursor; i++) {
1515 if (sorted->items[i].object_id != last_obj_id) {
1516 atomic.count_objs++;
1517 last_obj_id = sorted->items[i].object_id;
1520 if (i == sorted->cursor - 1)
1523 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1524 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1527 memmove(&sorted->items[i], &sorted->items[i + 1],
1528 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1532 for (i = 0; i < sorted->cursor; i++)
1533 sorted->items[i].cursor = i;
1535 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1541 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1542 if (!count_props_ptr) {
1547 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1553 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1554 if (!prop_values_ptr) {
1559 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1560 if (sorted->items[i].object_id != last_obj_id) {
1562 objs_ptr[obj_idx] = sorted->items[i].object_id;
1563 last_obj_id = objs_ptr[obj_idx];
1566 count_props_ptr[obj_idx]++;
1567 props_ptr[i] = sorted->items[i].property_id;
1568 prop_values_ptr[i] = sorted->items[i].value;
1572 atomic.flags = flags;
1573 atomic.objs_ptr = VOID2U64(objs_ptr);
1574 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1575 atomic.props_ptr = VOID2U64(props_ptr);
1576 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1577 atomic.user_data = VOID2U64(user_data);
1579 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1583 drmFree(count_props_ptr);
1585 drmFree(prop_values_ptr);
1586 drmModeAtomicFree(sorted);
1592 drmModeCreatePropertyBlob(int fd, const void *data, size_t length,
1595 struct drm_mode_create_blob create;
1598 if (length >= 0xffffffff)
1603 create.length = length;
1604 create.data = (uintptr_t) data;
1608 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1612 *id = create.blob_id;
1617 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1619 struct drm_mode_destroy_blob destroy;
1622 destroy.blob_id = id;
1623 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1627 drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags,
1628 uint32_t *lessee_id)
1630 struct drm_mode_create_lease create;
1634 create.object_ids = (uintptr_t) objects;
1635 create.object_count = num_objects;
1636 create.flags = flags;
1638 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1640 *lessee_id = create.lessee_id;
1646 drm_public drmModeLesseeListPtr
1647 drmModeListLessees(int fd)
1649 struct drm_mode_list_lessees list;
1651 drmModeLesseeListPtr ret;
1655 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1658 count = list.count_lessees;
1659 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1663 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1664 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1673 drm_public drmModeObjectListPtr
1674 drmModeGetLease(int fd)
1676 struct drm_mode_get_lease get;
1678 drmModeObjectListPtr ret;
1682 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1685 count = get.count_objects;
1686 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1690 get.objects_ptr = VOID2U64(&ret->objects[0]);
1691 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1701 drmModeRevokeLease(int fd, uint32_t lessee_id)
1703 struct drm_mode_revoke_lease revoke;
1708 revoke.lessee_id = lessee_id;
1710 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
1716 drm_public drmModeFB2Ptr
1717 drmModeGetFB2(int fd, uint32_t fb_id)
1719 struct drm_mode_fb_cmd2 get = {
1725 err = DRM_IOCTL(fd, DRM_IOCTL_MODE_GETFB2, &get);
1729 ret = drmMalloc(sizeof(drmModeFB2));
1734 ret->width = get.width;
1735 ret->height = get.height;
1736 ret->pixel_format = get.pixel_format;
1737 ret->flags = get.flags;
1738 ret->modifier = get.modifier[0];
1739 memcpy(ret->handles, get.handles, sizeof(uint32_t) * 4);
1740 memcpy(ret->pitches, get.pitches, sizeof(uint32_t) * 4);
1741 memcpy(ret->offsets, get.offsets, sizeof(uint32_t) * 4);
1746 drm_public void drmModeFreeFB2(drmModeFB2Ptr ptr)