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
37 * TODO the types we are after are defined in diffrent headers on diffrent
38 * platforms find which headers to include to get uint32_t
48 #include <sys/ioctl.h>
49 #ifdef HAVE_SYS_SYSCTL_H
50 #include <sys/sysctl.h>
55 #include "xf86drmMode.h"
63 #define memclear(s) memset(&s, 0, sizeof(s))
65 #define U642VOID(x) ((void *)(unsigned long)(x))
66 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
68 static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
70 int ret = drmIoctl(fd, cmd, arg);
71 return ret < 0 ? -errno : ret;
78 static void* drmAllocCpy(char *array, int count, int entry_size)
83 if (!count || !array || !entry_size)
86 if (!(r = drmMalloc(count*entry_size)))
89 for (i = 0; i < count; i++)
90 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
96 * A couple of free functions.
99 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
107 void drmModeFreeResources(drmModeResPtr ptr)
114 drmFree(ptr->connectors);
115 drmFree(ptr->encoders);
120 void drmModeFreeFB(drmModeFBPtr ptr)
125 /* we might add more frees later. */
129 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
138 void drmModeFreeConnector(drmModeConnectorPtr ptr)
143 drmFree(ptr->encoders);
144 drmFree(ptr->prop_values);
151 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
157 * ModeSetting functions.
160 drmModeResPtr drmModeGetResources(int fd)
162 struct drm_mode_card_res res, counts;
167 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
173 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
177 if (res.count_crtcs) {
178 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
179 if (!res.crtc_id_ptr)
182 if (res.count_connectors) {
183 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
184 if (!res.connector_id_ptr)
187 if (res.count_encoders) {
188 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
189 if (!res.encoder_id_ptr)
193 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
196 /* The number of available connectors and etc may have changed with a
197 * hotplug event in between the ioctls, in which case the field is
198 * silently ignored by the kernel.
200 if (counts.count_fbs < res.count_fbs ||
201 counts.count_crtcs < res.count_crtcs ||
202 counts.count_connectors < res.count_connectors ||
203 counts.count_encoders < res.count_encoders)
205 drmFree(U642VOID(res.fb_id_ptr));
206 drmFree(U642VOID(res.crtc_id_ptr));
207 drmFree(U642VOID(res.connector_id_ptr));
208 drmFree(U642VOID(res.encoder_id_ptr));
216 if (!(r = drmMalloc(sizeof(*r))))
219 r->min_width = res.min_width;
220 r->max_width = res.max_width;
221 r->min_height = res.min_height;
222 r->max_height = res.max_height;
223 r->count_fbs = res.count_fbs;
224 r->count_crtcs = res.count_crtcs;
225 r->count_connectors = res.count_connectors;
226 r->count_encoders = res.count_encoders;
228 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
229 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
230 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
231 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
232 if ((res.count_fbs && !r->fbs) ||
233 (res.count_crtcs && !r->crtcs) ||
234 (res.count_connectors && !r->connectors) ||
235 (res.count_encoders && !r->encoders))
239 drmFree(r->connectors);
240 drmFree(r->encoders);
246 drmFree(U642VOID(res.fb_id_ptr));
247 drmFree(U642VOID(res.crtc_id_ptr));
248 drmFree(U642VOID(res.connector_id_ptr));
249 drmFree(U642VOID(res.encoder_id_ptr));
254 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
255 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
258 struct drm_mode_fb_cmd f;
267 f.handle = bo_handle;
269 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
276 int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
277 uint32_t pixel_format, uint32_t bo_handles[4],
278 uint32_t pitches[4], uint32_t offsets[4],
279 uint32_t *buf_id, uint32_t flags)
281 struct drm_mode_fb_cmd2 f;
287 f.pixel_format = pixel_format;
289 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
290 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
291 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
293 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
300 int drmModeRmFB(int fd, uint32_t bufferId)
302 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
305 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
307 struct drm_mode_fb_cmd info;
313 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
316 if (!(r = drmMalloc(sizeof(*r))))
319 r->fb_id = info.fb_id;
320 r->width = info.width;
321 r->height = info.height;
322 r->pitch = info.pitch;
324 r->handle = info.handle;
325 r->depth = info.depth;
330 int drmModeDirtyFB(int fd, uint32_t bufferId,
331 drmModeClipPtr clips, uint32_t num_clips)
333 struct drm_mode_fb_dirty_cmd dirty;
336 dirty.fb_id = bufferId;
337 dirty.clips_ptr = VOID2U64(clips);
338 dirty.num_clips = num_clips;
340 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
348 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
350 struct drm_mode_crtc crtc;
354 crtc.crtc_id = crtcId;
356 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
363 if (!(r = drmMalloc(sizeof(*r))))
366 r->crtc_id = crtc.crtc_id;
369 r->mode_valid = crtc.mode_valid;
371 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
372 r->width = crtc.mode.hdisplay;
373 r->height = crtc.mode.vdisplay;
375 r->buffer_id = crtc.fb_id;
376 r->gamma_size = crtc.gamma_size;
381 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
382 uint32_t x, uint32_t y, uint32_t *connectors, int count,
383 drmModeModeInfoPtr mode)
385 struct drm_mode_crtc crtc;
390 crtc.crtc_id = crtcId;
391 crtc.fb_id = bufferId;
392 crtc.set_connectors_ptr = VOID2U64(connectors);
393 crtc.count_connectors = count;
395 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
399 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
403 * Cursor manipulation
406 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
408 struct drm_mode_cursor arg;
411 arg.flags = DRM_MODE_CURSOR_BO;
412 arg.crtc_id = crtcId;
415 arg.handle = bo_handle;
417 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
420 int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height, int32_t hot_x, int32_t hot_y)
422 struct drm_mode_cursor2 arg;
425 arg.flags = DRM_MODE_CURSOR_BO;
426 arg.crtc_id = crtcId;
429 arg.handle = bo_handle;
433 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
436 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
438 struct drm_mode_cursor arg;
441 arg.flags = DRM_MODE_CURSOR_MOVE;
442 arg.crtc_id = crtcId;
446 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
452 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
454 struct drm_mode_get_encoder enc;
455 drmModeEncoderPtr r = NULL;
458 enc.encoder_id = encoder_id;
460 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
463 if (!(r = drmMalloc(sizeof(*r))))
466 r->encoder_id = enc.encoder_id;
467 r->crtc_id = enc.crtc_id;
468 r->encoder_type = enc.encoder_type;
469 r->possible_crtcs = enc.possible_crtcs;
470 r->possible_clones = enc.possible_clones;
476 * Connector manipulation
478 static drmModeConnectorPtr
479 _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
481 struct drm_mode_get_connector conn, counts;
482 drmModeConnectorPtr r = NULL;
485 conn.connector_id = connector_id;
487 conn.count_modes = 1;
488 conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
491 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
497 if (conn.count_props) {
498 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
501 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
502 if (!conn.prop_values_ptr)
506 if (conn.count_modes) {
507 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
511 conn.count_modes = 1;
512 conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
515 if (conn.count_encoders) {
516 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
517 if (!conn.encoders_ptr)
521 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
524 /* The number of available connectors and etc may have changed with a
525 * hotplug event in between the ioctls, in which case the field is
526 * silently ignored by the kernel.
528 if (counts.count_props < conn.count_props ||
529 counts.count_modes < conn.count_modes ||
530 counts.count_encoders < conn.count_encoders) {
531 drmFree(U642VOID(conn.props_ptr));
532 drmFree(U642VOID(conn.prop_values_ptr));
533 drmFree(U642VOID(conn.modes_ptr));
534 drmFree(U642VOID(conn.encoders_ptr));
539 if(!(r = drmMalloc(sizeof(*r)))) {
543 r->connector_id = conn.connector_id;
544 r->encoder_id = conn.encoder_id;
545 r->connection = conn.connection;
546 r->mmWidth = conn.mm_width;
547 r->mmHeight = conn.mm_height;
548 /* convert subpixel from kernel to userspace */
549 r->subpixel = conn.subpixel + 1;
550 r->count_modes = conn.count_modes;
551 r->count_props = conn.count_props;
552 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
553 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
554 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
555 r->count_encoders = conn.count_encoders;
556 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
557 r->connector_type = conn.connector_type;
558 r->connector_type_id = conn.connector_type_id;
560 if ((r->count_props && !r->props) ||
561 (r->count_props && !r->prop_values) ||
562 (r->count_modes && !r->modes) ||
563 (r->count_encoders && !r->encoders)) {
565 drmFree(r->prop_values);
567 drmFree(r->encoders);
573 drmFree(U642VOID(conn.prop_values_ptr));
574 drmFree(U642VOID(conn.props_ptr));
575 drmFree(U642VOID(conn.modes_ptr));
576 drmFree(U642VOID(conn.encoders_ptr));
581 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
583 return _drmModeGetConnector(fd, connector_id, 1);
586 drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
588 return _drmModeGetConnector(fd, connector_id, 0);
591 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
593 struct drm_mode_mode_cmd res;
596 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
597 res.connector_id = connector_id;
599 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
602 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
604 struct drm_mode_mode_cmd res;
607 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
608 res.connector_id = connector_id;
610 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
614 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
616 struct drm_mode_get_property prop;
617 drmModePropertyPtr r;
620 prop.prop_id = property_id;
622 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
625 if (prop.count_values)
626 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
628 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
629 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
631 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
632 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
633 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
636 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
641 if (!(r = drmMalloc(sizeof(*r))))
644 r->prop_id = prop.prop_id;
645 r->count_values = prop.count_values;
647 r->flags = prop.flags;
648 if (prop.count_values)
649 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
650 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
651 r->count_enums = prop.count_enum_blobs;
652 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
653 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
654 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
655 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
656 r->count_blobs = prop.count_enum_blobs;
658 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
659 r->name[DRM_PROP_NAME_LEN-1] = 0;
662 drmFree(U642VOID(prop.values_ptr));
663 drmFree(U642VOID(prop.enum_blob_ptr));
668 void drmModeFreeProperty(drmModePropertyPtr ptr)
673 drmFree(ptr->values);
678 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
680 struct drm_mode_get_blob blob;
681 drmModePropertyBlobPtr r;
684 blob.blob_id = blob_id;
686 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
690 blob.data = VOID2U64(drmMalloc(blob.length));
692 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
697 if (!(r = drmMalloc(sizeof(*r))))
700 r->id = blob.blob_id;
701 r->length = blob.length;
702 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
705 drmFree(U642VOID(blob.data));
709 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
718 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
721 struct drm_mode_connector_set_property osp;
724 osp.connector_id = connector_id;
725 osp.prop_id = property_id;
728 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
732 * checks if a modesetting capable driver has attached to the pci id
733 * returns 0 if modesetting supported.
734 * -EINVAL or invalid bus id
735 * -ENOSYS if no modesetting support
737 int drmCheckModesettingSupported(const char *busid)
739 #if defined (__linux__)
740 char pci_dev_dir[1024];
741 int domain, bus, dev, func;
746 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
750 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
751 domain, bus, dev, func);
753 sysdir = opendir(pci_dev_dir);
755 dent = readdir(sysdir);
757 if (!strncmp(dent->d_name, "controlD", 8)) {
762 dent = readdir(sysdir);
769 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
770 domain, bus, dev, func);
772 sysdir = opendir(pci_dev_dir);
776 dent = readdir(sysdir);
778 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
783 dent = readdir(sysdir);
789 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
790 char kbusid[1024], sbusid[1024];
792 int domain, bus, dev, func;
793 int i, modesetting, ret;
796 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
800 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
803 /* How many GPUs do we expect in the machine ? */
804 for (i = 0; i < 16; i++) {
805 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
806 len = sizeof(sbusid);
807 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
813 if (strcmp(sbusid, kbusid) != 0)
815 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
816 len = sizeof(modesetting);
817 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
818 if (ret == -1 || len != sizeof(modesetting))
820 return (modesetting ? 0 : -ENOSYS);
822 #elif defined(__DragonFly__)
827 struct drm_mode_card_res res;
830 if ((fd = drmOpen(NULL, busid)) < 0)
833 memset(&res, 0, sizeof(struct drm_mode_card_res));
835 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
846 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
847 uint16_t *red, uint16_t *green, uint16_t *blue)
849 struct drm_mode_crtc_lut l;
854 l.red = VOID2U64(red);
855 l.green = VOID2U64(green);
856 l.blue = VOID2U64(blue);
858 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
861 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
862 uint16_t *red, uint16_t *green, uint16_t *blue)
864 struct drm_mode_crtc_lut l;
869 l.red = VOID2U64(red);
870 l.green = VOID2U64(green);
871 l.blue = VOID2U64(blue);
873 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
876 int drmHandleEvent(int fd, drmEventContextPtr evctx)
881 struct drm_event_vblank *vblank;
883 /* The DRM read semantics guarantees that we always get only
884 * complete events. */
886 len = read(fd, buffer, sizeof buffer);
889 if (len < (int)sizeof *e)
894 e = (struct drm_event *) &buffer[i];
896 case DRM_EVENT_VBLANK:
897 if (evctx->version < 1 ||
898 evctx->vblank_handler == NULL)
900 vblank = (struct drm_event_vblank *) e;
901 evctx->vblank_handler(fd,
905 U642VOID (vblank->user_data));
907 case DRM_EVENT_FLIP_COMPLETE:
908 if (evctx->version < 2 ||
909 evctx->page_flip_handler == NULL)
911 vblank = (struct drm_event_vblank *) e;
912 evctx->page_flip_handler(fd,
916 U642VOID (vblank->user_data));
927 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
928 uint32_t flags, void *user_data)
930 struct drm_mode_crtc_page_flip flip;
934 flip.crtc_id = crtc_id;
935 flip.user_data = VOID2U64(user_data);
938 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
941 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
942 uint32_t fb_id, uint32_t flags,
943 int32_t crtc_x, int32_t crtc_y,
944 uint32_t crtc_w, uint32_t crtc_h,
945 uint32_t src_x, uint32_t src_y,
946 uint32_t src_w, uint32_t src_h)
949 struct drm_mode_set_plane s;
952 s.plane_id = plane_id;
965 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
969 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
971 struct drm_mode_get_plane ovr, counts;
972 drmModePlanePtr r = 0;
976 ovr.plane_id = plane_id;
977 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
982 if (ovr.count_format_types) {
983 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
985 if (!ovr.format_type_ptr)
989 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
992 if (counts.count_format_types < ovr.count_format_types) {
993 drmFree(U642VOID(ovr.format_type_ptr));
997 if (!(r = drmMalloc(sizeof(*r))))
1000 r->count_formats = ovr.count_format_types;
1001 r->plane_id = ovr.plane_id;
1002 r->crtc_id = ovr.crtc_id;
1003 r->fb_id = ovr.fb_id;
1004 r->possible_crtcs = ovr.possible_crtcs;
1005 r->gamma_size = ovr.gamma_size;
1006 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1007 ovr.count_format_types, sizeof(uint32_t));
1008 if (ovr.count_format_types && !r->formats) {
1009 drmFree(r->formats);
1015 drmFree(U642VOID(ovr.format_type_ptr));
1020 void drmModeFreePlane(drmModePlanePtr ptr)
1025 drmFree(ptr->formats);
1029 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1031 struct drm_mode_get_plane_res res, counts;
1032 drmModePlaneResPtr r = 0;
1036 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1041 if (res.count_planes) {
1042 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1044 if (!res.plane_id_ptr)
1048 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1051 if (counts.count_planes < res.count_planes) {
1052 drmFree(U642VOID(res.plane_id_ptr));
1056 if (!(r = drmMalloc(sizeof(*r))))
1059 r->count_planes = res.count_planes;
1060 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1061 res.count_planes, sizeof(uint32_t));
1062 if (res.count_planes && !r->planes) {
1069 drmFree(U642VOID(res.plane_id_ptr));
1074 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1079 drmFree(ptr->planes);
1083 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1085 uint32_t object_type)
1087 struct drm_mode_obj_get_properties properties;
1088 drmModeObjectPropertiesPtr ret = NULL;
1092 memclear(properties);
1093 properties.obj_id = object_id;
1094 properties.obj_type = object_type;
1096 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1099 count = properties.count_props;
1102 properties.props_ptr = VOID2U64(drmMalloc(count *
1104 if (!properties.props_ptr)
1106 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1108 if (!properties.prop_values_ptr)
1112 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1115 if (count < properties.count_props) {
1116 drmFree(U642VOID(properties.props_ptr));
1117 drmFree(U642VOID(properties.prop_values_ptr));
1120 count = properties.count_props;
1122 ret = drmMalloc(sizeof(*ret));
1126 ret->count_props = count;
1127 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1128 count, sizeof(uint32_t));
1129 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1130 count, sizeof(uint64_t));
1131 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1132 drmFree(ret->props);
1133 drmFree(ret->prop_values);
1139 drmFree(U642VOID(properties.props_ptr));
1140 drmFree(U642VOID(properties.prop_values_ptr));
1144 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1148 drmFree(ptr->props);
1149 drmFree(ptr->prop_values);
1153 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1154 uint32_t property_id, uint64_t value)
1156 struct drm_mode_obj_set_property prop;
1160 prop.prop_id = property_id;
1161 prop.obj_id = object_id;
1162 prop.obj_type = object_type;
1164 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1167 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1169 struct _drmModeAtomicReqItem {
1171 uint32_t property_id;
1175 struct _drmModeAtomicReq {
1177 uint32_t size_items;
1178 drmModeAtomicReqItemPtr items;
1181 drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1183 drmModeAtomicReqPtr req;
1185 req = drmMalloc(sizeof *req);
1191 req->size_items = 0;
1196 drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1198 drmModeAtomicReqPtr new;
1200 new = drmMalloc(sizeof *new);
1204 new->cursor = old->cursor;
1205 new->size_items = old->size_items;
1207 if (old->size_items) {
1208 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1213 memcpy(new->items, old->items,
1214 old->size_items * sizeof(*new->items));
1222 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1224 if (!augment || augment->cursor == 0)
1227 if (base->cursor + augment->cursor >= base->size_items) {
1228 drmModeAtomicReqItemPtr new;
1229 int saved_size = base->size_items;
1231 base->size_items = base->cursor + augment->cursor;
1232 new = realloc(base->items,
1233 base->size_items * sizeof(*base->items));
1235 base->size_items = saved_size;
1241 memcpy(&base->items[base->cursor], augment->items,
1242 augment->cursor * sizeof(*augment->items));
1243 base->cursor += augment->cursor;
1248 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1253 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1255 req->cursor = cursor;
1258 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1260 uint32_t property_id,
1263 if (req->cursor >= req->size_items) {
1264 drmModeAtomicReqItemPtr new;
1266 req->size_items += 16;
1267 new = realloc(req->items, req->size_items * sizeof(*req->items));
1269 req->size_items -= 16;
1275 req->items[req->cursor].object_id = object_id;
1276 req->items[req->cursor].property_id = property_id;
1277 req->items[req->cursor].value = value;
1283 void drmModeAtomicFree(drmModeAtomicReqPtr req)
1289 drmFree(req->items);
1293 static int sort_req_list(const void *misc, const void *other)
1295 const drmModeAtomicReqItem *first = misc;
1296 const drmModeAtomicReqItem *second = other;
1298 if (first->object_id < second->object_id)
1300 else if (first->object_id > second->object_id)
1303 return second->property_id - first->property_id;
1306 int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1309 drmModeAtomicReqPtr sorted;
1310 struct drm_mode_atomic atomic;
1311 uint32_t *objs_ptr = NULL;
1312 uint32_t *count_props_ptr = NULL;
1313 uint32_t *props_ptr = NULL;
1314 uint64_t *prop_values_ptr = NULL;
1315 uint32_t last_obj_id = 0;
1320 if (req->cursor == 0)
1323 sorted = drmModeAtomicDuplicate(req);
1329 /* Sort the list by object ID, then by property ID. */
1330 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1333 /* Now the list is sorted, eliminate duplicate property sets. */
1334 for (i = 0; i < sorted->cursor; i++) {
1335 if (sorted->items[i].object_id != last_obj_id) {
1336 atomic.count_objs++;
1337 last_obj_id = sorted->items[i].object_id;
1340 if (i == sorted->cursor - 1)
1343 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1344 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1347 memmove(&sorted->items[i], &sorted->items[i + 1],
1348 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1352 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1358 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1359 if (!count_props_ptr) {
1364 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1370 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1371 if (!prop_values_ptr) {
1376 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1377 if (sorted->items[i].object_id != last_obj_id) {
1379 objs_ptr[obj_idx] = sorted->items[i].object_id;
1380 last_obj_id = objs_ptr[obj_idx];
1383 count_props_ptr[obj_idx]++;
1384 props_ptr[i] = sorted->items[i].property_id;
1385 prop_values_ptr[i] = sorted->items[i].value;
1389 atomic.flags = flags;
1390 atomic.objs_ptr = VOID2U64(objs_ptr);
1391 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1392 atomic.props_ptr = VOID2U64(props_ptr);
1393 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1394 atomic.user_data = VOID2U64(user_data);
1396 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1400 drmFree(count_props_ptr);
1402 drmFree(prop_values_ptr);
1403 drmModeAtomicFree(sorted);
1409 drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1411 struct drm_mode_create_blob create;
1414 if (length >= 0xffffffff)
1419 create.length = length;
1420 create.data = (uintptr_t) data;
1424 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1428 *id = create.blob_id;
1433 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1435 struct drm_mode_destroy_blob destroy;
1438 destroy.blob_id = id;
1439 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);