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
44 #include <sys/ioctl.h>
45 #ifdef HAVE_SYS_SYSCTL_H
46 #include <sys/sysctl.h>
51 #include "xf86drmMode.h"
59 #define memclear(s) memset(&s, 0, sizeof(s))
61 #define U642VOID(x) ((void *)(unsigned long)(x))
62 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
64 static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
66 int ret = drmIoctl(fd, cmd, arg);
67 return ret < 0 ? -errno : ret;
74 static void* drmAllocCpy(char *array, int count, int entry_size)
79 if (!count || !array || !entry_size)
82 if (!(r = drmMalloc(count*entry_size)))
85 for (i = 0; i < count; i++)
86 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
92 * A couple of free functions.
95 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
103 void drmModeFreeResources(drmModeResPtr ptr)
110 drmFree(ptr->connectors);
111 drmFree(ptr->encoders);
115 void drmModeFreeFB(drmModeFBPtr ptr)
120 /* we might add more frees later. */
124 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
132 void drmModeFreeConnector(drmModeConnectorPtr ptr)
137 drmFree(ptr->encoders);
138 drmFree(ptr->prop_values);
144 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
150 * ModeSetting functions.
153 drmModeResPtr drmModeGetResources(int fd)
155 struct drm_mode_card_res res, counts;
160 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
166 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
170 if (res.count_crtcs) {
171 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
172 if (!res.crtc_id_ptr)
175 if (res.count_connectors) {
176 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
177 if (!res.connector_id_ptr)
180 if (res.count_encoders) {
181 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
182 if (!res.encoder_id_ptr)
186 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
189 /* The number of available connectors and etc may have changed with a
190 * hotplug event in between the ioctls, in which case the field is
191 * silently ignored by the kernel.
193 if (counts.count_fbs < res.count_fbs ||
194 counts.count_crtcs < res.count_crtcs ||
195 counts.count_connectors < res.count_connectors ||
196 counts.count_encoders < res.count_encoders)
198 drmFree(U642VOID(res.fb_id_ptr));
199 drmFree(U642VOID(res.crtc_id_ptr));
200 drmFree(U642VOID(res.connector_id_ptr));
201 drmFree(U642VOID(res.encoder_id_ptr));
209 if (!(r = drmMalloc(sizeof(*r))))
212 r->min_width = res.min_width;
213 r->max_width = res.max_width;
214 r->min_height = res.min_height;
215 r->max_height = res.max_height;
216 r->count_fbs = res.count_fbs;
217 r->count_crtcs = res.count_crtcs;
218 r->count_connectors = res.count_connectors;
219 r->count_encoders = res.count_encoders;
221 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
222 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
223 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
224 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
225 if ((res.count_fbs && !r->fbs) ||
226 (res.count_crtcs && !r->crtcs) ||
227 (res.count_connectors && !r->connectors) ||
228 (res.count_encoders && !r->encoders))
232 drmFree(r->connectors);
233 drmFree(r->encoders);
239 drmFree(U642VOID(res.fb_id_ptr));
240 drmFree(U642VOID(res.crtc_id_ptr));
241 drmFree(U642VOID(res.connector_id_ptr));
242 drmFree(U642VOID(res.encoder_id_ptr));
247 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
248 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
251 struct drm_mode_fb_cmd f;
260 f.handle = bo_handle;
262 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
269 int drmModeAddFB2WithModifiers(int fd, uint32_t width, uint32_t height,
270 uint32_t pixel_format, const uint32_t bo_handles[4],
271 const uint32_t pitches[4], const uint32_t offsets[4],
272 const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
274 struct drm_mode_fb_cmd2 f;
280 f.pixel_format = pixel_format;
282 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
283 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
284 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
286 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
288 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
295 int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
296 uint32_t pixel_format, const uint32_t bo_handles[4],
297 const uint32_t pitches[4], const uint32_t offsets[4],
298 uint32_t *buf_id, uint32_t flags)
300 return drmModeAddFB2WithModifiers(fd, width, height,
301 pixel_format, bo_handles,
302 pitches, offsets, NULL,
306 int drmModeRmFB(int fd, uint32_t bufferId)
308 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
311 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
313 struct drm_mode_fb_cmd info;
319 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
322 if (!(r = drmMalloc(sizeof(*r))))
325 r->fb_id = info.fb_id;
326 r->width = info.width;
327 r->height = info.height;
328 r->pitch = info.pitch;
330 r->handle = info.handle;
331 r->depth = info.depth;
336 int drmModeDirtyFB(int fd, uint32_t bufferId,
337 drmModeClipPtr clips, uint32_t num_clips)
339 struct drm_mode_fb_dirty_cmd dirty;
342 dirty.fb_id = bufferId;
343 dirty.clips_ptr = VOID2U64(clips);
344 dirty.num_clips = num_clips;
346 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
353 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
355 struct drm_mode_crtc crtc;
359 crtc.crtc_id = crtcId;
361 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
368 if (!(r = drmMalloc(sizeof(*r))))
371 r->crtc_id = crtc.crtc_id;
374 r->mode_valid = crtc.mode_valid;
376 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
377 r->width = crtc.mode.hdisplay;
378 r->height = crtc.mode.vdisplay;
380 r->buffer_id = crtc.fb_id;
381 r->gamma_size = crtc.gamma_size;
385 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
386 uint32_t x, uint32_t y, uint32_t *connectors, int count,
387 drmModeModeInfoPtr mode)
389 struct drm_mode_crtc crtc;
394 crtc.crtc_id = crtcId;
395 crtc.fb_id = bufferId;
396 crtc.set_connectors_ptr = VOID2U64(connectors);
397 crtc.count_connectors = count;
399 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
403 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
407 * Cursor manipulation
410 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
412 struct drm_mode_cursor arg;
415 arg.flags = DRM_MODE_CURSOR_BO;
416 arg.crtc_id = crtcId;
419 arg.handle = bo_handle;
421 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
424 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)
426 struct drm_mode_cursor2 arg;
429 arg.flags = DRM_MODE_CURSOR_BO;
430 arg.crtc_id = crtcId;
433 arg.handle = bo_handle;
437 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
440 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
442 struct drm_mode_cursor arg;
445 arg.flags = DRM_MODE_CURSOR_MOVE;
446 arg.crtc_id = crtcId;
450 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
456 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
458 struct drm_mode_get_encoder enc;
459 drmModeEncoderPtr r = NULL;
462 enc.encoder_id = encoder_id;
464 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
467 if (!(r = drmMalloc(sizeof(*r))))
470 r->encoder_id = enc.encoder_id;
471 r->crtc_id = enc.crtc_id;
472 r->encoder_type = enc.encoder_type;
473 r->possible_crtcs = enc.possible_crtcs;
474 r->possible_clones = enc.possible_clones;
480 * Connector manipulation
482 static drmModeConnectorPtr
483 _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
485 struct drm_mode_get_connector conn, counts;
486 drmModeConnectorPtr r = NULL;
487 struct drm_mode_modeinfo stack_mode;
490 conn.connector_id = connector_id;
492 conn.count_modes = 1;
493 conn.modes_ptr = VOID2U64(&stack_mode);
496 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
502 if (conn.count_props) {
503 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
506 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
507 if (!conn.prop_values_ptr)
511 if (conn.count_modes) {
512 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
516 conn.count_modes = 1;
517 conn.modes_ptr = VOID2U64(&stack_mode);
520 if (conn.count_encoders) {
521 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
522 if (!conn.encoders_ptr)
526 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
529 /* The number of available connectors and etc may have changed with a
530 * hotplug event in between the ioctls, in which case the field is
531 * silently ignored by the kernel.
533 if (counts.count_props < conn.count_props ||
534 counts.count_modes < conn.count_modes ||
535 counts.count_encoders < conn.count_encoders) {
536 drmFree(U642VOID(conn.props_ptr));
537 drmFree(U642VOID(conn.prop_values_ptr));
538 if (U642VOID(conn.modes_ptr) != &stack_mode)
539 drmFree(U642VOID(conn.modes_ptr));
540 drmFree(U642VOID(conn.encoders_ptr));
545 if(!(r = drmMalloc(sizeof(*r)))) {
549 r->connector_id = conn.connector_id;
550 r->encoder_id = conn.encoder_id;
551 r->connection = conn.connection;
552 r->mmWidth = conn.mm_width;
553 r->mmHeight = conn.mm_height;
554 /* convert subpixel from kernel to userspace */
555 r->subpixel = conn.subpixel + 1;
556 r->count_modes = conn.count_modes;
557 r->count_props = conn.count_props;
558 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
559 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
560 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
561 r->count_encoders = conn.count_encoders;
562 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
563 r->connector_type = conn.connector_type;
564 r->connector_type_id = conn.connector_type_id;
566 if ((r->count_props && !r->props) ||
567 (r->count_props && !r->prop_values) ||
568 (r->count_modes && !r->modes) ||
569 (r->count_encoders && !r->encoders)) {
571 drmFree(r->prop_values);
573 drmFree(r->encoders);
579 drmFree(U642VOID(conn.prop_values_ptr));
580 drmFree(U642VOID(conn.props_ptr));
581 if (U642VOID(conn.modes_ptr) != &stack_mode)
582 drmFree(U642VOID(conn.modes_ptr));
583 drmFree(U642VOID(conn.encoders_ptr));
588 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
590 return _drmModeGetConnector(fd, connector_id, 1);
593 drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
595 return _drmModeGetConnector(fd, connector_id, 0);
598 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
600 struct drm_mode_mode_cmd res;
603 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
604 res.connector_id = connector_id;
606 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
609 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
611 struct drm_mode_mode_cmd res;
614 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
615 res.connector_id = connector_id;
617 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
620 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
622 struct drm_mode_get_property prop;
623 drmModePropertyPtr r;
626 prop.prop_id = property_id;
628 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
631 if (prop.count_values)
632 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
634 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
635 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
637 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
638 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
639 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
642 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
647 if (!(r = drmMalloc(sizeof(*r))))
650 r->prop_id = prop.prop_id;
651 r->count_values = prop.count_values;
653 r->flags = prop.flags;
654 if (prop.count_values)
655 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
656 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
657 r->count_enums = prop.count_enum_blobs;
658 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
659 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
660 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
661 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
662 r->count_blobs = prop.count_enum_blobs;
664 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
665 r->name[DRM_PROP_NAME_LEN-1] = 0;
668 drmFree(U642VOID(prop.values_ptr));
669 drmFree(U642VOID(prop.enum_blob_ptr));
674 void drmModeFreeProperty(drmModePropertyPtr ptr)
679 drmFree(ptr->values);
684 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
686 struct drm_mode_get_blob blob;
687 drmModePropertyBlobPtr r;
690 blob.blob_id = blob_id;
692 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
696 blob.data = VOID2U64(drmMalloc(blob.length));
698 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
703 if (!(r = drmMalloc(sizeof(*r))))
706 r->id = blob.blob_id;
707 r->length = blob.length;
708 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
711 drmFree(U642VOID(blob.data));
715 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
724 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
727 struct drm_mode_connector_set_property osp;
730 osp.connector_id = connector_id;
731 osp.prop_id = property_id;
734 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
738 * checks if a modesetting capable driver has attached to the pci id
739 * returns 0 if modesetting supported.
740 * -EINVAL or invalid bus id
741 * -ENOSYS if no modesetting support
743 int drmCheckModesettingSupported(const char *busid)
745 #if defined (__linux__)
746 char pci_dev_dir[1024];
747 int domain, bus, dev, func;
752 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
756 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
757 domain, bus, dev, func);
759 sysdir = opendir(pci_dev_dir);
761 dent = readdir(sysdir);
763 if (!strncmp(dent->d_name, "controlD", 8)) {
768 dent = readdir(sysdir);
775 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
776 domain, bus, dev, func);
778 sysdir = opendir(pci_dev_dir);
782 dent = readdir(sysdir);
784 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
789 dent = readdir(sysdir);
795 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
796 char kbusid[1024], sbusid[1024];
798 int domain, bus, dev, func;
799 int i, modesetting, ret;
802 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
806 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
809 /* How many GPUs do we expect in the machine ? */
810 for (i = 0; i < 16; i++) {
811 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
812 len = sizeof(sbusid);
813 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
819 if (strcmp(sbusid, kbusid) != 0)
821 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
822 len = sizeof(modesetting);
823 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
824 if (ret == -1 || len != sizeof(modesetting))
826 return (modesetting ? 0 : -ENOSYS);
828 #elif defined(__DragonFly__)
830 #elif defined(__OpenBSD__)
832 struct drm_mode_card_res res;
835 if ((fd = drmOpen(NULL, busid)) < 0)
838 memset(&res, 0, sizeof(struct drm_mode_card_res));
840 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
851 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
852 uint16_t *red, uint16_t *green, uint16_t *blue)
854 struct drm_mode_crtc_lut l;
859 l.red = VOID2U64(red);
860 l.green = VOID2U64(green);
861 l.blue = VOID2U64(blue);
863 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
866 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
867 uint16_t *red, uint16_t *green, uint16_t *blue)
869 struct drm_mode_crtc_lut l;
874 l.red = VOID2U64(red);
875 l.green = VOID2U64(green);
876 l.blue = VOID2U64(blue);
878 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
881 int drmHandleEvent(int fd, drmEventContextPtr evctx)
886 struct drm_event_vblank *vblank;
887 struct drm_event_crtc_sequence *seq;
890 /* The DRM read semantics guarantees that we always get only
891 * complete events. */
893 len = read(fd, buffer, sizeof buffer);
896 if (len < (int)sizeof *e)
901 e = (struct drm_event *)(buffer + i);
903 case DRM_EVENT_VBLANK:
904 if (evctx->version < 1 ||
905 evctx->vblank_handler == NULL)
907 vblank = (struct drm_event_vblank *) e;
908 evctx->vblank_handler(fd,
912 U642VOID (vblank->user_data));
914 case DRM_EVENT_FLIP_COMPLETE:
915 vblank = (struct drm_event_vblank *) e;
916 user_data = U642VOID (vblank->user_data);
918 if (evctx->version >= 3 && evctx->page_flip_handler2)
919 evctx->page_flip_handler2(fd,
925 else if (evctx->version >= 2 && evctx->page_flip_handler)
926 evctx->page_flip_handler(fd,
932 case DRM_EVENT_CRTC_SEQUENCE:
933 seq = (struct drm_event_crtc_sequence *) e;
934 if (evctx->version >= 4 && evctx->sequence_handler)
935 evctx->sequence_handler(fd,
949 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
950 uint32_t flags, void *user_data)
952 struct drm_mode_crtc_page_flip flip;
956 flip.crtc_id = crtc_id;
957 flip.user_data = VOID2U64(user_data);
960 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
963 int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
964 uint32_t flags, void *user_data,
965 uint32_t target_vblank)
967 struct drm_mode_crtc_page_flip_target flip_target;
969 memclear(flip_target);
970 flip_target.fb_id = fb_id;
971 flip_target.crtc_id = crtc_id;
972 flip_target.user_data = VOID2U64(user_data);
973 flip_target.flags = flags;
974 flip_target.sequence = target_vblank;
976 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
979 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
980 uint32_t fb_id, uint32_t flags,
981 int32_t crtc_x, int32_t crtc_y,
982 uint32_t crtc_w, uint32_t crtc_h,
983 uint32_t src_x, uint32_t src_y,
984 uint32_t src_w, uint32_t src_h)
986 struct drm_mode_set_plane s;
989 s.plane_id = plane_id;
1002 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1005 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
1007 struct drm_mode_get_plane ovr, counts;
1008 drmModePlanePtr r = 0;
1012 ovr.plane_id = plane_id;
1013 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1018 if (ovr.count_format_types) {
1019 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1021 if (!ovr.format_type_ptr)
1025 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1028 if (counts.count_format_types < ovr.count_format_types) {
1029 drmFree(U642VOID(ovr.format_type_ptr));
1033 if (!(r = drmMalloc(sizeof(*r))))
1036 r->count_formats = ovr.count_format_types;
1037 r->plane_id = ovr.plane_id;
1038 r->crtc_id = ovr.crtc_id;
1039 r->fb_id = ovr.fb_id;
1040 r->possible_crtcs = ovr.possible_crtcs;
1041 r->gamma_size = ovr.gamma_size;
1042 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1043 ovr.count_format_types, sizeof(uint32_t));
1044 if (ovr.count_format_types && !r->formats) {
1045 drmFree(r->formats);
1051 drmFree(U642VOID(ovr.format_type_ptr));
1056 void drmModeFreePlane(drmModePlanePtr ptr)
1061 drmFree(ptr->formats);
1065 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1067 struct drm_mode_get_plane_res res, counts;
1068 drmModePlaneResPtr r = 0;
1072 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1077 if (res.count_planes) {
1078 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1080 if (!res.plane_id_ptr)
1084 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1087 if (counts.count_planes < res.count_planes) {
1088 drmFree(U642VOID(res.plane_id_ptr));
1092 if (!(r = drmMalloc(sizeof(*r))))
1095 r->count_planes = res.count_planes;
1096 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1097 res.count_planes, sizeof(uint32_t));
1098 if (res.count_planes && !r->planes) {
1105 drmFree(U642VOID(res.plane_id_ptr));
1110 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1115 drmFree(ptr->planes);
1119 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1121 uint32_t object_type)
1123 struct drm_mode_obj_get_properties properties;
1124 drmModeObjectPropertiesPtr ret = NULL;
1128 memclear(properties);
1129 properties.obj_id = object_id;
1130 properties.obj_type = object_type;
1132 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1135 count = properties.count_props;
1138 properties.props_ptr = VOID2U64(drmMalloc(count *
1140 if (!properties.props_ptr)
1142 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1144 if (!properties.prop_values_ptr)
1148 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1151 if (count < properties.count_props) {
1152 drmFree(U642VOID(properties.props_ptr));
1153 drmFree(U642VOID(properties.prop_values_ptr));
1156 count = properties.count_props;
1158 ret = drmMalloc(sizeof(*ret));
1162 ret->count_props = count;
1163 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1164 count, sizeof(uint32_t));
1165 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1166 count, sizeof(uint64_t));
1167 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1168 drmFree(ret->props);
1169 drmFree(ret->prop_values);
1175 drmFree(U642VOID(properties.props_ptr));
1176 drmFree(U642VOID(properties.prop_values_ptr));
1180 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1184 drmFree(ptr->props);
1185 drmFree(ptr->prop_values);
1189 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1190 uint32_t property_id, uint64_t value)
1192 struct drm_mode_obj_set_property prop;
1196 prop.prop_id = property_id;
1197 prop.obj_id = object_id;
1198 prop.obj_type = object_type;
1200 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1203 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1205 struct _drmModeAtomicReqItem {
1207 uint32_t property_id;
1211 struct _drmModeAtomicReq {
1213 uint32_t size_items;
1214 drmModeAtomicReqItemPtr items;
1217 drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1219 drmModeAtomicReqPtr req;
1221 req = drmMalloc(sizeof *req);
1227 req->size_items = 0;
1232 drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1234 drmModeAtomicReqPtr new;
1239 new = drmMalloc(sizeof *new);
1243 new->cursor = old->cursor;
1244 new->size_items = old->size_items;
1246 if (old->size_items) {
1247 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1252 memcpy(new->items, old->items,
1253 old->size_items * sizeof(*new->items));
1261 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1266 if (!augment || augment->cursor == 0)
1269 if (base->cursor + augment->cursor >= base->size_items) {
1270 drmModeAtomicReqItemPtr new;
1271 int saved_size = base->size_items;
1273 base->size_items = base->cursor + augment->cursor;
1274 new = realloc(base->items,
1275 base->size_items * sizeof(*base->items));
1277 base->size_items = saved_size;
1283 memcpy(&base->items[base->cursor], augment->items,
1284 augment->cursor * sizeof(*augment->items));
1285 base->cursor += augment->cursor;
1290 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1297 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1300 req->cursor = cursor;
1303 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1305 uint32_t property_id,
1311 if (object_id == 0 || property_id == 0)
1314 if (req->cursor >= req->size_items) {
1315 drmModeAtomicReqItemPtr new;
1317 req->size_items += 16;
1318 new = realloc(req->items, req->size_items * sizeof(*req->items));
1320 req->size_items -= 16;
1326 req->items[req->cursor].object_id = object_id;
1327 req->items[req->cursor].property_id = property_id;
1328 req->items[req->cursor].value = value;
1334 void drmModeAtomicFree(drmModeAtomicReqPtr req)
1340 drmFree(req->items);
1344 static int sort_req_list(const void *misc, const void *other)
1346 const drmModeAtomicReqItem *first = misc;
1347 const drmModeAtomicReqItem *second = other;
1349 if (first->object_id < second->object_id)
1351 else if (first->object_id > second->object_id)
1354 return second->property_id - first->property_id;
1357 int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1360 drmModeAtomicReqPtr sorted;
1361 struct drm_mode_atomic atomic;
1362 uint32_t *objs_ptr = NULL;
1363 uint32_t *count_props_ptr = NULL;
1364 uint32_t *props_ptr = NULL;
1365 uint64_t *prop_values_ptr = NULL;
1366 uint32_t last_obj_id = 0;
1374 if (req->cursor == 0)
1377 sorted = drmModeAtomicDuplicate(req);
1383 /* Sort the list by object ID, then by property ID. */
1384 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1387 /* Now the list is sorted, eliminate duplicate property sets. */
1388 for (i = 0; i < sorted->cursor; i++) {
1389 if (sorted->items[i].object_id != last_obj_id) {
1390 atomic.count_objs++;
1391 last_obj_id = sorted->items[i].object_id;
1394 if (i == sorted->cursor - 1)
1397 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1398 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1401 memmove(&sorted->items[i], &sorted->items[i + 1],
1402 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1406 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1412 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1413 if (!count_props_ptr) {
1418 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1424 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1425 if (!prop_values_ptr) {
1430 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1431 if (sorted->items[i].object_id != last_obj_id) {
1433 objs_ptr[obj_idx] = sorted->items[i].object_id;
1434 last_obj_id = objs_ptr[obj_idx];
1437 count_props_ptr[obj_idx]++;
1438 props_ptr[i] = sorted->items[i].property_id;
1439 prop_values_ptr[i] = sorted->items[i].value;
1443 atomic.flags = flags;
1444 atomic.objs_ptr = VOID2U64(objs_ptr);
1445 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1446 atomic.props_ptr = VOID2U64(props_ptr);
1447 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1448 atomic.user_data = VOID2U64(user_data);
1450 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1454 drmFree(count_props_ptr);
1456 drmFree(prop_values_ptr);
1457 drmModeAtomicFree(sorted);
1463 drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1465 struct drm_mode_create_blob create;
1468 if (length >= 0xffffffff)
1473 create.length = length;
1474 create.data = (uintptr_t) data;
1478 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1482 *id = create.blob_id;
1487 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1489 struct drm_mode_destroy_blob destroy;
1492 destroy.blob_id = id;
1493 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1497 drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags, uint32_t *lessee_id)
1499 struct drm_mode_create_lease create;
1503 create.object_ids = (uintptr_t) objects;
1504 create.object_count = num_objects;
1505 create.flags = flags;
1507 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1509 *lessee_id = create.lessee_id;
1515 drmModeLesseeListPtr
1516 drmModeListLessees(int fd)
1518 struct drm_mode_list_lessees list;
1520 drmModeLesseeListPtr ret;
1524 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1527 count = list.count_lessees;
1528 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1532 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1533 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1542 drmModeObjectListPtr
1543 drmModeGetLease(int fd)
1545 struct drm_mode_get_lease get;
1547 drmModeObjectListPtr ret;
1551 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1554 count = get.count_objects;
1555 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1559 get.objects_ptr = VOID2U64(&ret->objects[0]);
1560 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1570 drmModeRevokeLease(int fd, uint32_t lessee_id)
1572 struct drm_mode_revoke_lease revoke;
1577 revoke.lessee_id = lessee_id;
1579 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);