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__)
829 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
830 uint16_t *red, uint16_t *green, uint16_t *blue)
832 struct drm_mode_crtc_lut l;
837 l.red = VOID2U64(red);
838 l.green = VOID2U64(green);
839 l.blue = VOID2U64(blue);
841 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
844 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
845 uint16_t *red, uint16_t *green, uint16_t *blue)
847 struct drm_mode_crtc_lut l;
852 l.red = VOID2U64(red);
853 l.green = VOID2U64(green);
854 l.blue = VOID2U64(blue);
856 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
859 int drmHandleEvent(int fd, drmEventContextPtr evctx)
864 struct drm_event_vblank *vblank;
866 /* The DRM read semantics guarantees that we always get only
867 * complete events. */
869 len = read(fd, buffer, sizeof buffer);
872 if (len < (int)sizeof *e)
877 e = (struct drm_event *) &buffer[i];
879 case DRM_EVENT_VBLANK:
880 if (evctx->version < 1 ||
881 evctx->vblank_handler == NULL)
883 vblank = (struct drm_event_vblank *) e;
884 evctx->vblank_handler(fd,
888 U642VOID (vblank->user_data));
890 case DRM_EVENT_FLIP_COMPLETE:
891 if (evctx->version < 2 ||
892 evctx->page_flip_handler == NULL)
894 vblank = (struct drm_event_vblank *) e;
895 evctx->page_flip_handler(fd,
899 U642VOID (vblank->user_data));
910 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
911 uint32_t flags, void *user_data)
913 struct drm_mode_crtc_page_flip flip;
917 flip.crtc_id = crtc_id;
918 flip.user_data = VOID2U64(user_data);
921 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
924 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
925 uint32_t fb_id, uint32_t flags,
926 int32_t crtc_x, int32_t crtc_y,
927 uint32_t crtc_w, uint32_t crtc_h,
928 uint32_t src_x, uint32_t src_y,
929 uint32_t src_w, uint32_t src_h)
932 struct drm_mode_set_plane s;
935 s.plane_id = plane_id;
948 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
952 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
954 struct drm_mode_get_plane ovr, counts;
955 drmModePlanePtr r = 0;
959 ovr.plane_id = plane_id;
960 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
965 if (ovr.count_format_types) {
966 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
968 if (!ovr.format_type_ptr)
972 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
975 if (counts.count_format_types < ovr.count_format_types) {
976 drmFree(U642VOID(ovr.format_type_ptr));
980 if (!(r = drmMalloc(sizeof(*r))))
983 r->count_formats = ovr.count_format_types;
984 r->plane_id = ovr.plane_id;
985 r->crtc_id = ovr.crtc_id;
986 r->fb_id = ovr.fb_id;
987 r->possible_crtcs = ovr.possible_crtcs;
988 r->gamma_size = ovr.gamma_size;
989 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
990 ovr.count_format_types, sizeof(uint32_t));
991 if (ovr.count_format_types && !r->formats) {
998 drmFree(U642VOID(ovr.format_type_ptr));
1003 void drmModeFreePlane(drmModePlanePtr ptr)
1008 drmFree(ptr->formats);
1012 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1014 struct drm_mode_get_plane_res res, counts;
1015 drmModePlaneResPtr r = 0;
1019 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1024 if (res.count_planes) {
1025 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1027 if (!res.plane_id_ptr)
1031 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1034 if (counts.count_planes < res.count_planes) {
1035 drmFree(U642VOID(res.plane_id_ptr));
1039 if (!(r = drmMalloc(sizeof(*r))))
1042 r->count_planes = res.count_planes;
1043 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1044 res.count_planes, sizeof(uint32_t));
1045 if (res.count_planes && !r->planes) {
1052 drmFree(U642VOID(res.plane_id_ptr));
1057 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1062 drmFree(ptr->planes);
1066 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1068 uint32_t object_type)
1070 struct drm_mode_obj_get_properties properties;
1071 drmModeObjectPropertiesPtr ret = NULL;
1075 memclear(properties);
1076 properties.obj_id = object_id;
1077 properties.obj_type = object_type;
1079 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1082 count = properties.count_props;
1085 properties.props_ptr = VOID2U64(drmMalloc(count *
1087 if (!properties.props_ptr)
1089 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1091 if (!properties.prop_values_ptr)
1095 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1098 if (count < properties.count_props) {
1099 drmFree(U642VOID(properties.props_ptr));
1100 drmFree(U642VOID(properties.prop_values_ptr));
1103 count = properties.count_props;
1105 ret = drmMalloc(sizeof(*ret));
1109 ret->count_props = count;
1110 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1111 count, sizeof(uint32_t));
1112 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1113 count, sizeof(uint64_t));
1114 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1115 drmFree(ret->props);
1116 drmFree(ret->prop_values);
1122 drmFree(U642VOID(properties.props_ptr));
1123 drmFree(U642VOID(properties.prop_values_ptr));
1127 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1131 drmFree(ptr->props);
1132 drmFree(ptr->prop_values);
1136 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1137 uint32_t property_id, uint64_t value)
1139 struct drm_mode_obj_set_property prop;
1143 prop.prop_id = property_id;
1144 prop.obj_id = object_id;
1145 prop.obj_type = object_type;
1147 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1150 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1152 struct _drmModeAtomicReqItem {
1154 uint32_t property_id;
1158 struct _drmModeAtomicReq {
1160 uint32_t size_items;
1161 drmModeAtomicReqItemPtr items;
1164 drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1166 drmModeAtomicReqPtr req;
1168 req = drmMalloc(sizeof *req);
1174 req->size_items = 0;
1179 drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1181 drmModeAtomicReqPtr new;
1183 new = drmMalloc(sizeof *new);
1187 new->cursor = old->cursor;
1188 new->size_items = old->size_items;
1190 if (old->size_items) {
1191 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1196 memcpy(new->items, old->items,
1197 old->size_items * sizeof(*new->items));
1205 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1207 if (!augment || augment->cursor == 0)
1210 if (base->cursor + augment->cursor >= base->size_items) {
1211 drmModeAtomicReqItemPtr new;
1212 int saved_size = base->size_items;
1214 base->size_items = base->cursor + augment->cursor;
1215 new = realloc(base->items,
1216 base->size_items * sizeof(*base->items));
1218 base->size_items = saved_size;
1224 memcpy(&base->items[base->cursor], augment->items,
1225 augment->cursor * sizeof(*augment->items));
1226 base->cursor += augment->cursor;
1231 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1236 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1238 req->cursor = cursor;
1241 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1243 uint32_t property_id,
1246 if (req->cursor >= req->size_items) {
1247 drmModeAtomicReqItemPtr new;
1249 req->size_items += 16;
1250 new = realloc(req->items, req->size_items * sizeof(*req->items));
1252 req->size_items -= 16;
1258 req->items[req->cursor].object_id = object_id;
1259 req->items[req->cursor].property_id = property_id;
1260 req->items[req->cursor].value = value;
1266 void drmModeAtomicFree(drmModeAtomicReqPtr req)
1272 drmFree(req->items);
1276 static int sort_req_list(const void *misc, const void *other)
1278 const drmModeAtomicReqItem *first = misc;
1279 const drmModeAtomicReqItem *second = other;
1281 if (first->object_id < second->object_id)
1283 else if (first->object_id > second->object_id)
1286 return second->property_id - first->property_id;
1289 int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1292 drmModeAtomicReqPtr sorted = drmModeAtomicDuplicate(req);
1293 struct drm_mode_atomic atomic;
1294 uint32_t *objs_ptr = NULL;
1295 uint32_t *count_props_ptr = NULL;
1296 uint32_t *props_ptr = NULL;
1297 uint64_t *prop_values_ptr = NULL;
1298 uint32_t last_obj_id = 0;
1308 /* Sort the list by object ID, then by property ID. */
1309 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1312 /* Now the list is sorted, eliminate duplicate property sets. */
1313 for (i = 0; i < sorted->cursor; i++) {
1314 if (sorted->items[i].object_id != last_obj_id) {
1315 atomic.count_objs++;
1316 last_obj_id = sorted->items[i].object_id;
1319 if (i == sorted->cursor - 1)
1322 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1323 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1326 memmove(&sorted->items[i], &sorted->items[i + 1],
1327 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1331 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1337 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1338 if (!count_props_ptr) {
1343 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1349 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1350 if (!prop_values_ptr) {
1355 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1356 if (sorted->items[i].object_id != last_obj_id) {
1358 objs_ptr[obj_idx] = sorted->items[i].object_id;
1359 last_obj_id = objs_ptr[obj_idx];
1362 count_props_ptr[obj_idx]++;
1363 props_ptr[i] = sorted->items[i].property_id;
1364 prop_values_ptr[i] = sorted->items[i].value;
1368 atomic.flags = flags;
1369 atomic.objs_ptr = VOID2U64(objs_ptr);
1370 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1371 atomic.props_ptr = VOID2U64(props_ptr);
1372 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1373 atomic.user_data = VOID2U64(user_data);
1375 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1379 drmFree(count_props_ptr);
1381 drmFree(prop_values_ptr);
1382 drmModeAtomicFree(sorted);
1388 drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1390 struct drm_mode_create_blob create;
1393 if (length >= 0xffffffff)
1398 create.length = length;
1399 create.data = (uintptr_t) data;
1403 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1407 *id = create.blob_id;
1412 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1414 struct drm_mode_destroy_blob destroy;
1417 destroy.blob_id = id;
1418 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);