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 different headers on different
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);
119 void drmModeFreeFB(drmModeFBPtr ptr)
124 /* we might add more frees later. */
128 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
136 void drmModeFreeConnector(drmModeConnectorPtr ptr)
141 drmFree(ptr->encoders);
142 drmFree(ptr->prop_values);
148 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
154 * ModeSetting functions.
157 drmModeResPtr drmModeGetResources(int fd)
159 struct drm_mode_card_res res, counts;
164 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
170 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
174 if (res.count_crtcs) {
175 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
176 if (!res.crtc_id_ptr)
179 if (res.count_connectors) {
180 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
181 if (!res.connector_id_ptr)
184 if (res.count_encoders) {
185 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
186 if (!res.encoder_id_ptr)
190 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
193 /* The number of available connectors and etc may have changed with a
194 * hotplug event in between the ioctls, in which case the field is
195 * silently ignored by the kernel.
197 if (counts.count_fbs < res.count_fbs ||
198 counts.count_crtcs < res.count_crtcs ||
199 counts.count_connectors < res.count_connectors ||
200 counts.count_encoders < res.count_encoders)
202 drmFree(U642VOID(res.fb_id_ptr));
203 drmFree(U642VOID(res.crtc_id_ptr));
204 drmFree(U642VOID(res.connector_id_ptr));
205 drmFree(U642VOID(res.encoder_id_ptr));
213 if (!(r = drmMalloc(sizeof(*r))))
216 r->min_width = res.min_width;
217 r->max_width = res.max_width;
218 r->min_height = res.min_height;
219 r->max_height = res.max_height;
220 r->count_fbs = res.count_fbs;
221 r->count_crtcs = res.count_crtcs;
222 r->count_connectors = res.count_connectors;
223 r->count_encoders = res.count_encoders;
225 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
226 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
227 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
228 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
229 if ((res.count_fbs && !r->fbs) ||
230 (res.count_crtcs && !r->crtcs) ||
231 (res.count_connectors && !r->connectors) ||
232 (res.count_encoders && !r->encoders))
236 drmFree(r->connectors);
237 drmFree(r->encoders);
243 drmFree(U642VOID(res.fb_id_ptr));
244 drmFree(U642VOID(res.crtc_id_ptr));
245 drmFree(U642VOID(res.connector_id_ptr));
246 drmFree(U642VOID(res.encoder_id_ptr));
251 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
252 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
255 struct drm_mode_fb_cmd f;
264 f.handle = bo_handle;
266 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
273 int drmModeAddFB2WithModifiers(int fd, uint32_t width, uint32_t height,
274 uint32_t pixel_format, uint32_t bo_handles[4],
275 uint32_t pitches[4], uint32_t offsets[4],
276 uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
278 struct drm_mode_fb_cmd2 f;
284 f.pixel_format = pixel_format;
286 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
287 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
288 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
290 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
292 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
299 int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
300 uint32_t pixel_format, uint32_t bo_handles[4],
301 uint32_t pitches[4], uint32_t offsets[4],
302 uint32_t *buf_id, uint32_t flags)
304 return drmModeAddFB2WithModifiers(fd, width, height,
305 pixel_format, bo_handles,
306 pitches, offsets, NULL,
310 int drmModeRmFB(int fd, uint32_t bufferId)
312 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
315 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
317 struct drm_mode_fb_cmd info;
323 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
326 if (!(r = drmMalloc(sizeof(*r))))
329 r->fb_id = info.fb_id;
330 r->width = info.width;
331 r->height = info.height;
332 r->pitch = info.pitch;
334 r->handle = info.handle;
335 r->depth = info.depth;
340 int drmModeDirtyFB(int fd, uint32_t bufferId,
341 drmModeClipPtr clips, uint32_t num_clips)
343 struct drm_mode_fb_dirty_cmd dirty;
346 dirty.fb_id = bufferId;
347 dirty.clips_ptr = VOID2U64(clips);
348 dirty.num_clips = num_clips;
350 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
357 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
359 struct drm_mode_crtc crtc;
363 crtc.crtc_id = crtcId;
365 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
372 if (!(r = drmMalloc(sizeof(*r))))
375 r->crtc_id = crtc.crtc_id;
378 r->mode_valid = crtc.mode_valid;
380 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
381 r->width = crtc.mode.hdisplay;
382 r->height = crtc.mode.vdisplay;
384 r->buffer_id = crtc.fb_id;
385 r->gamma_size = crtc.gamma_size;
389 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
390 uint32_t x, uint32_t y, uint32_t *connectors, int count,
391 drmModeModeInfoPtr mode)
393 struct drm_mode_crtc crtc;
398 crtc.crtc_id = crtcId;
399 crtc.fb_id = bufferId;
400 crtc.set_connectors_ptr = VOID2U64(connectors);
401 crtc.count_connectors = count;
403 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
407 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
411 * Cursor manipulation
414 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
416 struct drm_mode_cursor arg;
419 arg.flags = DRM_MODE_CURSOR_BO;
420 arg.crtc_id = crtcId;
423 arg.handle = bo_handle;
425 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
428 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)
430 struct drm_mode_cursor2 arg;
433 arg.flags = DRM_MODE_CURSOR_BO;
434 arg.crtc_id = crtcId;
437 arg.handle = bo_handle;
441 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
444 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
446 struct drm_mode_cursor arg;
449 arg.flags = DRM_MODE_CURSOR_MOVE;
450 arg.crtc_id = crtcId;
454 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
460 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
462 struct drm_mode_get_encoder enc;
463 drmModeEncoderPtr r = NULL;
466 enc.encoder_id = encoder_id;
468 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
471 if (!(r = drmMalloc(sizeof(*r))))
474 r->encoder_id = enc.encoder_id;
475 r->crtc_id = enc.crtc_id;
476 r->encoder_type = enc.encoder_type;
477 r->possible_crtcs = enc.possible_crtcs;
478 r->possible_clones = enc.possible_clones;
484 * Connector manipulation
486 static drmModeConnectorPtr
487 _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
489 struct drm_mode_get_connector conn, counts;
490 drmModeConnectorPtr r = NULL;
491 struct drm_mode_modeinfo stack_mode;
494 conn.connector_id = connector_id;
496 conn.count_modes = 1;
497 conn.modes_ptr = VOID2U64(&stack_mode);
500 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
506 if (conn.count_props) {
507 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
510 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
511 if (!conn.prop_values_ptr)
515 if (conn.count_modes) {
516 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
520 conn.count_modes = 1;
521 conn.modes_ptr = VOID2U64(&stack_mode);
524 if (conn.count_encoders) {
525 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
526 if (!conn.encoders_ptr)
530 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
533 /* The number of available connectors and etc may have changed with a
534 * hotplug event in between the ioctls, in which case the field is
535 * silently ignored by the kernel.
537 if (counts.count_props < conn.count_props ||
538 counts.count_modes < conn.count_modes ||
539 counts.count_encoders < conn.count_encoders) {
540 drmFree(U642VOID(conn.props_ptr));
541 drmFree(U642VOID(conn.prop_values_ptr));
542 if (U642VOID(conn.modes_ptr) != &stack_mode)
543 drmFree(U642VOID(conn.modes_ptr));
544 drmFree(U642VOID(conn.encoders_ptr));
549 if(!(r = drmMalloc(sizeof(*r)))) {
553 r->connector_id = conn.connector_id;
554 r->encoder_id = conn.encoder_id;
555 r->connection = conn.connection;
556 r->mmWidth = conn.mm_width;
557 r->mmHeight = conn.mm_height;
558 /* convert subpixel from kernel to userspace */
559 r->subpixel = conn.subpixel + 1;
560 r->count_modes = conn.count_modes;
561 r->count_props = conn.count_props;
562 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
563 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
564 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
565 r->count_encoders = conn.count_encoders;
566 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
567 r->connector_type = conn.connector_type;
568 r->connector_type_id = conn.connector_type_id;
570 if ((r->count_props && !r->props) ||
571 (r->count_props && !r->prop_values) ||
572 (r->count_modes && !r->modes) ||
573 (r->count_encoders && !r->encoders)) {
575 drmFree(r->prop_values);
577 drmFree(r->encoders);
583 drmFree(U642VOID(conn.prop_values_ptr));
584 drmFree(U642VOID(conn.props_ptr));
585 if (U642VOID(conn.modes_ptr) != &stack_mode)
586 drmFree(U642VOID(conn.modes_ptr));
587 drmFree(U642VOID(conn.encoders_ptr));
592 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
594 return _drmModeGetConnector(fd, connector_id, 1);
597 drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
599 return _drmModeGetConnector(fd, connector_id, 0);
602 int drmModeAttachMode(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_ATTACHMODE, &res);
613 int drmModeDetachMode(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_DETACHMODE, &res);
624 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
626 struct drm_mode_get_property prop;
627 drmModePropertyPtr r;
630 prop.prop_id = property_id;
632 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
635 if (prop.count_values)
636 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
638 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
639 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
641 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
642 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
643 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
646 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
651 if (!(r = drmMalloc(sizeof(*r))))
654 r->prop_id = prop.prop_id;
655 r->count_values = prop.count_values;
657 r->flags = prop.flags;
658 if (prop.count_values)
659 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
660 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
661 r->count_enums = prop.count_enum_blobs;
662 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
663 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
664 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
665 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
666 r->count_blobs = prop.count_enum_blobs;
668 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
669 r->name[DRM_PROP_NAME_LEN-1] = 0;
672 drmFree(U642VOID(prop.values_ptr));
673 drmFree(U642VOID(prop.enum_blob_ptr));
678 void drmModeFreeProperty(drmModePropertyPtr ptr)
683 drmFree(ptr->values);
688 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
690 struct drm_mode_get_blob blob;
691 drmModePropertyBlobPtr r;
694 blob.blob_id = blob_id;
696 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
700 blob.data = VOID2U64(drmMalloc(blob.length));
702 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
707 if (!(r = drmMalloc(sizeof(*r))))
710 r->id = blob.blob_id;
711 r->length = blob.length;
712 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
715 drmFree(U642VOID(blob.data));
719 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
728 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
731 struct drm_mode_connector_set_property osp;
734 osp.connector_id = connector_id;
735 osp.prop_id = property_id;
738 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
742 * checks if a modesetting capable driver has attached to the pci id
743 * returns 0 if modesetting supported.
744 * -EINVAL or invalid bus id
745 * -ENOSYS if no modesetting support
747 int drmCheckModesettingSupported(const char *busid)
749 #if defined (__linux__)
750 char pci_dev_dir[1024];
751 int domain, bus, dev, func;
756 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
760 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
761 domain, bus, dev, func);
763 sysdir = opendir(pci_dev_dir);
765 dent = readdir(sysdir);
767 if (!strncmp(dent->d_name, "controlD", 8)) {
772 dent = readdir(sysdir);
779 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
780 domain, bus, dev, func);
782 sysdir = opendir(pci_dev_dir);
786 dent = readdir(sysdir);
788 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
793 dent = readdir(sysdir);
799 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
800 char kbusid[1024], sbusid[1024];
802 int domain, bus, dev, func;
803 int i, modesetting, ret;
806 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
810 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
813 /* How many GPUs do we expect in the machine ? */
814 for (i = 0; i < 16; i++) {
815 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
816 len = sizeof(sbusid);
817 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
823 if (strcmp(sbusid, kbusid) != 0)
825 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
826 len = sizeof(modesetting);
827 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
828 if (ret == -1 || len != sizeof(modesetting))
830 return (modesetting ? 0 : -ENOSYS);
832 #elif defined(__DragonFly__)
837 struct drm_mode_card_res res;
840 if ((fd = drmOpen(NULL, busid)) < 0)
843 memset(&res, 0, sizeof(struct drm_mode_card_res));
845 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
856 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
857 uint16_t *red, uint16_t *green, uint16_t *blue)
859 struct drm_mode_crtc_lut l;
864 l.red = VOID2U64(red);
865 l.green = VOID2U64(green);
866 l.blue = VOID2U64(blue);
868 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
871 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
872 uint16_t *red, uint16_t *green, uint16_t *blue)
874 struct drm_mode_crtc_lut l;
879 l.red = VOID2U64(red);
880 l.green = VOID2U64(green);
881 l.blue = VOID2U64(blue);
883 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
886 int drmHandleEvent(int fd, drmEventContextPtr evctx)
891 struct drm_event_vblank *vblank;
894 /* The DRM read semantics guarantees that we always get only
895 * complete events. */
897 len = read(fd, buffer, sizeof buffer);
900 if (len < (int)sizeof *e)
905 e = (struct drm_event *)(buffer + i);
907 case DRM_EVENT_VBLANK:
908 if (evctx->version < 1 ||
909 evctx->vblank_handler == NULL)
911 vblank = (struct drm_event_vblank *) e;
912 evctx->vblank_handler(fd,
916 U642VOID (vblank->user_data));
918 case DRM_EVENT_FLIP_COMPLETE:
919 vblank = (struct drm_event_vblank *) e;
920 user_data = U642VOID (vblank->user_data);
922 if (evctx->version >= 3 && evctx->page_flip_handler2)
923 evctx->page_flip_handler2(fd,
929 else if (evctx->version >= 2 && evctx->page_flip_handler)
930 evctx->page_flip_handler(fd,
945 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
946 uint32_t flags, void *user_data)
948 struct drm_mode_crtc_page_flip flip;
952 flip.crtc_id = crtc_id;
953 flip.user_data = VOID2U64(user_data);
956 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
959 int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
960 uint32_t flags, void *user_data,
961 uint32_t target_vblank)
963 struct drm_mode_crtc_page_flip_target flip_target;
965 memclear(flip_target);
966 flip_target.fb_id = fb_id;
967 flip_target.crtc_id = crtc_id;
968 flip_target.user_data = VOID2U64(user_data);
969 flip_target.flags = flags;
970 flip_target.sequence = target_vblank;
972 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
975 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
976 uint32_t fb_id, uint32_t flags,
977 int32_t crtc_x, int32_t crtc_y,
978 uint32_t crtc_w, uint32_t crtc_h,
979 uint32_t src_x, uint32_t src_y,
980 uint32_t src_w, uint32_t src_h)
982 struct drm_mode_set_plane s;
985 s.plane_id = plane_id;
998 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1001 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
1003 struct drm_mode_get_plane ovr, counts;
1004 drmModePlanePtr r = 0;
1008 ovr.plane_id = plane_id;
1009 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1014 if (ovr.count_format_types) {
1015 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1017 if (!ovr.format_type_ptr)
1021 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1024 if (counts.count_format_types < ovr.count_format_types) {
1025 drmFree(U642VOID(ovr.format_type_ptr));
1029 if (!(r = drmMalloc(sizeof(*r))))
1032 r->count_formats = ovr.count_format_types;
1033 r->plane_id = ovr.plane_id;
1034 r->crtc_id = ovr.crtc_id;
1035 r->fb_id = ovr.fb_id;
1036 r->possible_crtcs = ovr.possible_crtcs;
1037 r->gamma_size = ovr.gamma_size;
1038 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1039 ovr.count_format_types, sizeof(uint32_t));
1040 if (ovr.count_format_types && !r->formats) {
1041 drmFree(r->formats);
1047 drmFree(U642VOID(ovr.format_type_ptr));
1052 void drmModeFreePlane(drmModePlanePtr ptr)
1057 drmFree(ptr->formats);
1061 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1063 struct drm_mode_get_plane_res res, counts;
1064 drmModePlaneResPtr r = 0;
1068 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1073 if (res.count_planes) {
1074 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1076 if (!res.plane_id_ptr)
1080 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1083 if (counts.count_planes < res.count_planes) {
1084 drmFree(U642VOID(res.plane_id_ptr));
1088 if (!(r = drmMalloc(sizeof(*r))))
1091 r->count_planes = res.count_planes;
1092 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1093 res.count_planes, sizeof(uint32_t));
1094 if (res.count_planes && !r->planes) {
1101 drmFree(U642VOID(res.plane_id_ptr));
1106 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1111 drmFree(ptr->planes);
1115 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1117 uint32_t object_type)
1119 struct drm_mode_obj_get_properties properties;
1120 drmModeObjectPropertiesPtr ret = NULL;
1124 memclear(properties);
1125 properties.obj_id = object_id;
1126 properties.obj_type = object_type;
1128 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1131 count = properties.count_props;
1134 properties.props_ptr = VOID2U64(drmMalloc(count *
1136 if (!properties.props_ptr)
1138 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1140 if (!properties.prop_values_ptr)
1144 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1147 if (count < properties.count_props) {
1148 drmFree(U642VOID(properties.props_ptr));
1149 drmFree(U642VOID(properties.prop_values_ptr));
1152 count = properties.count_props;
1154 ret = drmMalloc(sizeof(*ret));
1158 ret->count_props = count;
1159 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1160 count, sizeof(uint32_t));
1161 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1162 count, sizeof(uint64_t));
1163 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1164 drmFree(ret->props);
1165 drmFree(ret->prop_values);
1171 drmFree(U642VOID(properties.props_ptr));
1172 drmFree(U642VOID(properties.prop_values_ptr));
1176 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1180 drmFree(ptr->props);
1181 drmFree(ptr->prop_values);
1185 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1186 uint32_t property_id, uint64_t value)
1188 struct drm_mode_obj_set_property prop;
1192 prop.prop_id = property_id;
1193 prop.obj_id = object_id;
1194 prop.obj_type = object_type;
1196 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1199 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1201 struct _drmModeAtomicReqItem {
1203 uint32_t property_id;
1207 struct _drmModeAtomicReq {
1209 uint32_t size_items;
1210 drmModeAtomicReqItemPtr items;
1213 drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1215 drmModeAtomicReqPtr req;
1217 req = drmMalloc(sizeof *req);
1223 req->size_items = 0;
1228 drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1230 drmModeAtomicReqPtr new;
1235 new = drmMalloc(sizeof *new);
1239 new->cursor = old->cursor;
1240 new->size_items = old->size_items;
1242 if (old->size_items) {
1243 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1248 memcpy(new->items, old->items,
1249 old->size_items * sizeof(*new->items));
1257 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1262 if (!augment || augment->cursor == 0)
1265 if (base->cursor + augment->cursor >= base->size_items) {
1266 drmModeAtomicReqItemPtr new;
1267 int saved_size = base->size_items;
1269 base->size_items = base->cursor + augment->cursor;
1270 new = realloc(base->items,
1271 base->size_items * sizeof(*base->items));
1273 base->size_items = saved_size;
1279 memcpy(&base->items[base->cursor], augment->items,
1280 augment->cursor * sizeof(*augment->items));
1281 base->cursor += augment->cursor;
1286 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1293 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1296 req->cursor = cursor;
1299 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1301 uint32_t property_id,
1307 if (req->cursor >= req->size_items) {
1308 drmModeAtomicReqItemPtr new;
1310 req->size_items += 16;
1311 new = realloc(req->items, req->size_items * sizeof(*req->items));
1313 req->size_items -= 16;
1319 req->items[req->cursor].object_id = object_id;
1320 req->items[req->cursor].property_id = property_id;
1321 req->items[req->cursor].value = value;
1327 void drmModeAtomicFree(drmModeAtomicReqPtr req)
1333 drmFree(req->items);
1337 static int sort_req_list(const void *misc, const void *other)
1339 const drmModeAtomicReqItem *first = misc;
1340 const drmModeAtomicReqItem *second = other;
1342 if (first->object_id < second->object_id)
1344 else if (first->object_id > second->object_id)
1347 return second->property_id - first->property_id;
1350 int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1353 drmModeAtomicReqPtr sorted;
1354 struct drm_mode_atomic atomic;
1355 uint32_t *objs_ptr = NULL;
1356 uint32_t *count_props_ptr = NULL;
1357 uint32_t *props_ptr = NULL;
1358 uint64_t *prop_values_ptr = NULL;
1359 uint32_t last_obj_id = 0;
1367 if (req->cursor == 0)
1370 sorted = drmModeAtomicDuplicate(req);
1376 /* Sort the list by object ID, then by property ID. */
1377 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1380 /* Now the list is sorted, eliminate duplicate property sets. */
1381 for (i = 0; i < sorted->cursor; i++) {
1382 if (sorted->items[i].object_id != last_obj_id) {
1383 atomic.count_objs++;
1384 last_obj_id = sorted->items[i].object_id;
1387 if (i == sorted->cursor - 1)
1390 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1391 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1394 memmove(&sorted->items[i], &sorted->items[i + 1],
1395 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1399 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1405 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1406 if (!count_props_ptr) {
1411 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1417 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1418 if (!prop_values_ptr) {
1423 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1424 if (sorted->items[i].object_id != last_obj_id) {
1426 objs_ptr[obj_idx] = sorted->items[i].object_id;
1427 last_obj_id = objs_ptr[obj_idx];
1430 count_props_ptr[obj_idx]++;
1431 props_ptr[i] = sorted->items[i].property_id;
1432 prop_values_ptr[i] = sorted->items[i].value;
1436 atomic.flags = flags;
1437 atomic.objs_ptr = VOID2U64(objs_ptr);
1438 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1439 atomic.props_ptr = VOID2U64(props_ptr);
1440 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1441 atomic.user_data = VOID2U64(user_data);
1443 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1447 drmFree(count_props_ptr);
1449 drmFree(prop_values_ptr);
1450 drmModeAtomicFree(sorted);
1456 drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1458 struct drm_mode_create_blob create;
1461 if (length >= 0xffffffff)
1466 create.length = length;
1467 create.data = (uintptr_t) data;
1471 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1475 *id = create.blob_id;
1480 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1482 struct drm_mode_destroy_blob destroy;
1485 destroy.blob_id = id;
1486 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);