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 drmModeAddFB2(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 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 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
297 int drmModeRmFB(int fd, uint32_t bufferId)
299 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
302 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
304 struct drm_mode_fb_cmd info;
310 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
313 if (!(r = drmMalloc(sizeof(*r))))
316 r->fb_id = info.fb_id;
317 r->width = info.width;
318 r->height = info.height;
319 r->pitch = info.pitch;
321 r->handle = info.handle;
322 r->depth = info.depth;
327 int drmModeDirtyFB(int fd, uint32_t bufferId,
328 drmModeClipPtr clips, uint32_t num_clips)
330 struct drm_mode_fb_dirty_cmd dirty;
333 dirty.fb_id = bufferId;
334 dirty.clips_ptr = VOID2U64(clips);
335 dirty.num_clips = num_clips;
337 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
344 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
346 struct drm_mode_crtc crtc;
350 crtc.crtc_id = crtcId;
352 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
359 if (!(r = drmMalloc(sizeof(*r))))
362 r->crtc_id = crtc.crtc_id;
365 r->mode_valid = crtc.mode_valid;
367 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
368 r->width = crtc.mode.hdisplay;
369 r->height = crtc.mode.vdisplay;
371 r->buffer_id = crtc.fb_id;
372 r->gamma_size = crtc.gamma_size;
376 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
377 uint32_t x, uint32_t y, uint32_t *connectors, int count,
378 drmModeModeInfoPtr mode)
380 struct drm_mode_crtc crtc;
385 crtc.crtc_id = crtcId;
386 crtc.fb_id = bufferId;
387 crtc.set_connectors_ptr = VOID2U64(connectors);
388 crtc.count_connectors = count;
390 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
394 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
398 * Cursor manipulation
401 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
403 struct drm_mode_cursor arg;
406 arg.flags = DRM_MODE_CURSOR_BO;
407 arg.crtc_id = crtcId;
410 arg.handle = bo_handle;
412 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
415 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)
417 struct drm_mode_cursor2 arg;
420 arg.flags = DRM_MODE_CURSOR_BO;
421 arg.crtc_id = crtcId;
424 arg.handle = bo_handle;
428 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
431 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
433 struct drm_mode_cursor arg;
436 arg.flags = DRM_MODE_CURSOR_MOVE;
437 arg.crtc_id = crtcId;
441 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
447 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
449 struct drm_mode_get_encoder enc;
450 drmModeEncoderPtr r = NULL;
453 enc.encoder_id = encoder_id;
455 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
458 if (!(r = drmMalloc(sizeof(*r))))
461 r->encoder_id = enc.encoder_id;
462 r->crtc_id = enc.crtc_id;
463 r->encoder_type = enc.encoder_type;
464 r->possible_crtcs = enc.possible_crtcs;
465 r->possible_clones = enc.possible_clones;
471 * Connector manipulation
473 static drmModeConnectorPtr
474 _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
476 struct drm_mode_get_connector conn, counts;
477 drmModeConnectorPtr r = NULL;
478 struct drm_mode_modeinfo stack_mode;
481 conn.connector_id = connector_id;
483 conn.count_modes = 1;
484 conn.modes_ptr = VOID2U64(&stack_mode);
487 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
493 if (conn.count_props) {
494 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
497 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
498 if (!conn.prop_values_ptr)
502 if (conn.count_modes) {
503 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
507 conn.count_modes = 1;
508 conn.modes_ptr = VOID2U64(&stack_mode);
511 if (conn.count_encoders) {
512 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
513 if (!conn.encoders_ptr)
517 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
520 /* The number of available connectors and etc may have changed with a
521 * hotplug event in between the ioctls, in which case the field is
522 * silently ignored by the kernel.
524 if (counts.count_props < conn.count_props ||
525 counts.count_modes < conn.count_modes ||
526 counts.count_encoders < conn.count_encoders) {
527 drmFree(U642VOID(conn.props_ptr));
528 drmFree(U642VOID(conn.prop_values_ptr));
529 if (U642VOID(conn.modes_ptr) != &stack_mode)
530 drmFree(U642VOID(conn.modes_ptr));
531 drmFree(U642VOID(conn.encoders_ptr));
536 if(!(r = drmMalloc(sizeof(*r)))) {
540 r->connector_id = conn.connector_id;
541 r->encoder_id = conn.encoder_id;
542 r->connection = conn.connection;
543 r->mmWidth = conn.mm_width;
544 r->mmHeight = conn.mm_height;
545 /* convert subpixel from kernel to userspace */
546 r->subpixel = conn.subpixel + 1;
547 r->count_modes = conn.count_modes;
548 r->count_props = conn.count_props;
549 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
550 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
551 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
552 r->count_encoders = conn.count_encoders;
553 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
554 r->connector_type = conn.connector_type;
555 r->connector_type_id = conn.connector_type_id;
557 if ((r->count_props && !r->props) ||
558 (r->count_props && !r->prop_values) ||
559 (r->count_modes && !r->modes) ||
560 (r->count_encoders && !r->encoders)) {
562 drmFree(r->prop_values);
564 drmFree(r->encoders);
570 drmFree(U642VOID(conn.prop_values_ptr));
571 drmFree(U642VOID(conn.props_ptr));
572 if (U642VOID(conn.modes_ptr) != &stack_mode)
573 drmFree(U642VOID(conn.modes_ptr));
574 drmFree(U642VOID(conn.encoders_ptr));
579 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
581 return _drmModeGetConnector(fd, connector_id, 1);
584 drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
586 return _drmModeGetConnector(fd, connector_id, 0);
589 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
591 struct drm_mode_mode_cmd res;
594 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
595 res.connector_id = connector_id;
597 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
600 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
602 struct drm_mode_mode_cmd res;
605 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
606 res.connector_id = connector_id;
608 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
611 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
613 struct drm_mode_get_property prop;
614 drmModePropertyPtr r;
617 prop.prop_id = property_id;
619 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
622 if (prop.count_values)
623 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
625 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
626 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
628 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
629 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
630 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
633 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
638 if (!(r = drmMalloc(sizeof(*r))))
641 r->prop_id = prop.prop_id;
642 r->count_values = prop.count_values;
644 r->flags = prop.flags;
645 if (prop.count_values)
646 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
647 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
648 r->count_enums = prop.count_enum_blobs;
649 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
650 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
651 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
652 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
653 r->count_blobs = prop.count_enum_blobs;
655 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
656 r->name[DRM_PROP_NAME_LEN-1] = 0;
659 drmFree(U642VOID(prop.values_ptr));
660 drmFree(U642VOID(prop.enum_blob_ptr));
665 void drmModeFreeProperty(drmModePropertyPtr ptr)
670 drmFree(ptr->values);
675 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
677 struct drm_mode_get_blob blob;
678 drmModePropertyBlobPtr r;
681 blob.blob_id = blob_id;
683 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
687 blob.data = VOID2U64(drmMalloc(blob.length));
689 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
694 if (!(r = drmMalloc(sizeof(*r))))
697 r->id = blob.blob_id;
698 r->length = blob.length;
699 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
702 drmFree(U642VOID(blob.data));
706 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
715 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
718 struct drm_mode_connector_set_property osp;
721 osp.connector_id = connector_id;
722 osp.prop_id = property_id;
725 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
729 * checks if a modesetting capable driver has attached to the pci id
730 * returns 0 if modesetting supported.
731 * -EINVAL or invalid bus id
732 * -ENOSYS if no modesetting support
734 int drmCheckModesettingSupported(const char *busid)
736 #if defined (__linux__)
737 char pci_dev_dir[1024];
738 int domain, bus, dev, func;
743 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
747 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
748 domain, bus, dev, func);
750 sysdir = opendir(pci_dev_dir);
752 dent = readdir(sysdir);
754 if (!strncmp(dent->d_name, "controlD", 8)) {
759 dent = readdir(sysdir);
766 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
767 domain, bus, dev, func);
769 sysdir = opendir(pci_dev_dir);
773 dent = readdir(sysdir);
775 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
780 dent = readdir(sysdir);
786 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
787 char kbusid[1024], sbusid[1024];
789 int domain, bus, dev, func;
790 int i, modesetting, ret;
793 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
797 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
800 /* How many GPUs do we expect in the machine ? */
801 for (i = 0; i < 16; i++) {
802 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
803 len = sizeof(sbusid);
804 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
810 if (strcmp(sbusid, kbusid) != 0)
812 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
813 len = sizeof(modesetting);
814 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
815 if (ret == -1 || len != sizeof(modesetting))
817 return (modesetting ? 0 : -ENOSYS);
819 #elif defined(__DragonFly__)
824 struct drm_mode_card_res res;
827 if ((fd = drmOpen(NULL, busid)) < 0)
830 memset(&res, 0, sizeof(struct drm_mode_card_res));
832 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
843 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
844 uint16_t *red, uint16_t *green, uint16_t *blue)
846 struct drm_mode_crtc_lut l;
851 l.red = VOID2U64(red);
852 l.green = VOID2U64(green);
853 l.blue = VOID2U64(blue);
855 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
858 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
859 uint16_t *red, uint16_t *green, uint16_t *blue)
861 struct drm_mode_crtc_lut l;
866 l.red = VOID2U64(red);
867 l.green = VOID2U64(green);
868 l.blue = VOID2U64(blue);
870 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
873 int drmHandleEvent(int fd, drmEventContextPtr evctx)
878 struct drm_event_vblank *vblank;
880 /* The DRM read semantics guarantees that we always get only
881 * complete events. */
883 len = read(fd, buffer, sizeof buffer);
886 if (len < (int)sizeof *e)
891 e = (struct drm_event *) &buffer[i];
893 case DRM_EVENT_VBLANK:
894 if (evctx->version < 1 ||
895 evctx->vblank_handler == NULL)
897 vblank = (struct drm_event_vblank *) e;
898 evctx->vblank_handler(fd,
902 U642VOID (vblank->user_data));
904 case DRM_EVENT_FLIP_COMPLETE:
905 if (evctx->version < 2 ||
906 evctx->page_flip_handler == NULL)
908 vblank = (struct drm_event_vblank *) e;
909 evctx->page_flip_handler(fd,
913 U642VOID (vblank->user_data));
924 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
925 uint32_t flags, void *user_data)
927 struct drm_mode_crtc_page_flip flip;
931 flip.crtc_id = crtc_id;
932 flip.user_data = VOID2U64(user_data);
935 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
938 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
939 uint32_t fb_id, uint32_t flags,
940 int32_t crtc_x, int32_t crtc_y,
941 uint32_t crtc_w, uint32_t crtc_h,
942 uint32_t src_x, uint32_t src_y,
943 uint32_t src_w, uint32_t src_h)
945 struct drm_mode_set_plane s;
948 s.plane_id = plane_id;
961 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
964 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
966 struct drm_mode_get_plane ovr, counts;
967 drmModePlanePtr r = 0;
971 ovr.plane_id = plane_id;
972 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
977 if (ovr.count_format_types) {
978 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
980 if (!ovr.format_type_ptr)
984 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
987 if (counts.count_format_types < ovr.count_format_types) {
988 drmFree(U642VOID(ovr.format_type_ptr));
992 if (!(r = drmMalloc(sizeof(*r))))
995 r->count_formats = ovr.count_format_types;
996 r->plane_id = ovr.plane_id;
997 r->crtc_id = ovr.crtc_id;
998 r->fb_id = ovr.fb_id;
999 r->possible_crtcs = ovr.possible_crtcs;
1000 r->gamma_size = ovr.gamma_size;
1001 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1002 ovr.count_format_types, sizeof(uint32_t));
1003 if (ovr.count_format_types && !r->formats) {
1004 drmFree(r->formats);
1010 drmFree(U642VOID(ovr.format_type_ptr));
1015 void drmModeFreePlane(drmModePlanePtr ptr)
1020 drmFree(ptr->formats);
1024 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1026 struct drm_mode_get_plane_res res, counts;
1027 drmModePlaneResPtr r = 0;
1031 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1036 if (res.count_planes) {
1037 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1039 if (!res.plane_id_ptr)
1043 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1046 if (counts.count_planes < res.count_planes) {
1047 drmFree(U642VOID(res.plane_id_ptr));
1051 if (!(r = drmMalloc(sizeof(*r))))
1054 r->count_planes = res.count_planes;
1055 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1056 res.count_planes, sizeof(uint32_t));
1057 if (res.count_planes && !r->planes) {
1064 drmFree(U642VOID(res.plane_id_ptr));
1069 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1074 drmFree(ptr->planes);
1078 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1080 uint32_t object_type)
1082 struct drm_mode_obj_get_properties properties;
1083 drmModeObjectPropertiesPtr ret = NULL;
1087 memclear(properties);
1088 properties.obj_id = object_id;
1089 properties.obj_type = object_type;
1091 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1094 count = properties.count_props;
1097 properties.props_ptr = VOID2U64(drmMalloc(count *
1099 if (!properties.props_ptr)
1101 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1103 if (!properties.prop_values_ptr)
1107 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1110 if (count < properties.count_props) {
1111 drmFree(U642VOID(properties.props_ptr));
1112 drmFree(U642VOID(properties.prop_values_ptr));
1115 count = properties.count_props;
1117 ret = drmMalloc(sizeof(*ret));
1121 ret->count_props = count;
1122 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1123 count, sizeof(uint32_t));
1124 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1125 count, sizeof(uint64_t));
1126 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1127 drmFree(ret->props);
1128 drmFree(ret->prop_values);
1134 drmFree(U642VOID(properties.props_ptr));
1135 drmFree(U642VOID(properties.prop_values_ptr));
1139 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1143 drmFree(ptr->props);
1144 drmFree(ptr->prop_values);
1148 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1149 uint32_t property_id, uint64_t value)
1151 struct drm_mode_obj_set_property prop;
1155 prop.prop_id = property_id;
1156 prop.obj_id = object_id;
1157 prop.obj_type = object_type;
1159 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1162 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1164 struct _drmModeAtomicReqItem {
1166 uint32_t property_id;
1170 struct _drmModeAtomicReq {
1172 uint32_t size_items;
1173 drmModeAtomicReqItemPtr items;
1176 drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1178 drmModeAtomicReqPtr req;
1180 req = drmMalloc(sizeof *req);
1186 req->size_items = 0;
1191 drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1193 drmModeAtomicReqPtr new;
1198 new = drmMalloc(sizeof *new);
1202 new->cursor = old->cursor;
1203 new->size_items = old->size_items;
1205 if (old->size_items) {
1206 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1211 memcpy(new->items, old->items,
1212 old->size_items * sizeof(*new->items));
1220 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1225 if (!augment || augment->cursor == 0)
1228 if (base->cursor + augment->cursor >= base->size_items) {
1229 drmModeAtomicReqItemPtr new;
1230 int saved_size = base->size_items;
1232 base->size_items = base->cursor + augment->cursor;
1233 new = realloc(base->items,
1234 base->size_items * sizeof(*base->items));
1236 base->size_items = saved_size;
1242 memcpy(&base->items[base->cursor], augment->items,
1243 augment->cursor * sizeof(*augment->items));
1244 base->cursor += augment->cursor;
1249 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1256 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1259 req->cursor = cursor;
1262 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1264 uint32_t property_id,
1270 if (req->cursor >= req->size_items) {
1271 drmModeAtomicReqItemPtr new;
1273 req->size_items += 16;
1274 new = realloc(req->items, req->size_items * sizeof(*req->items));
1276 req->size_items -= 16;
1282 req->items[req->cursor].object_id = object_id;
1283 req->items[req->cursor].property_id = property_id;
1284 req->items[req->cursor].value = value;
1290 void drmModeAtomicFree(drmModeAtomicReqPtr req)
1296 drmFree(req->items);
1300 static int sort_req_list(const void *misc, const void *other)
1302 const drmModeAtomicReqItem *first = misc;
1303 const drmModeAtomicReqItem *second = other;
1305 if (first->object_id < second->object_id)
1307 else if (first->object_id > second->object_id)
1310 return second->property_id - first->property_id;
1313 int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1316 drmModeAtomicReqPtr sorted;
1317 struct drm_mode_atomic atomic;
1318 uint32_t *objs_ptr = NULL;
1319 uint32_t *count_props_ptr = NULL;
1320 uint32_t *props_ptr = NULL;
1321 uint64_t *prop_values_ptr = NULL;
1322 uint32_t last_obj_id = 0;
1330 if (req->cursor == 0)
1333 sorted = drmModeAtomicDuplicate(req);
1339 /* Sort the list by object ID, then by property ID. */
1340 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1343 /* Now the list is sorted, eliminate duplicate property sets. */
1344 for (i = 0; i < sorted->cursor; i++) {
1345 if (sorted->items[i].object_id != last_obj_id) {
1346 atomic.count_objs++;
1347 last_obj_id = sorted->items[i].object_id;
1350 if (i == sorted->cursor - 1)
1353 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1354 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1357 memmove(&sorted->items[i], &sorted->items[i + 1],
1358 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1362 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1368 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1369 if (!count_props_ptr) {
1374 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1380 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1381 if (!prop_values_ptr) {
1386 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1387 if (sorted->items[i].object_id != last_obj_id) {
1389 objs_ptr[obj_idx] = sorted->items[i].object_id;
1390 last_obj_id = objs_ptr[obj_idx];
1393 count_props_ptr[obj_idx]++;
1394 props_ptr[i] = sorted->items[i].property_id;
1395 prop_values_ptr[i] = sorted->items[i].value;
1399 atomic.flags = flags;
1400 atomic.objs_ptr = VOID2U64(objs_ptr);
1401 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1402 atomic.props_ptr = VOID2U64(props_ptr);
1403 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1404 atomic.user_data = VOID2U64(user_data);
1406 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1410 drmFree(count_props_ptr);
1412 drmFree(prop_values_ptr);
1413 drmModeAtomicFree(sorted);
1419 drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1421 struct drm_mode_create_blob create;
1424 if (length >= 0xffffffff)
1429 create.length = length;
1430 create.data = (uintptr_t) data;
1434 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1438 *id = create.blob_id;
1443 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1445 struct drm_mode_destroy_blob destroy;
1448 destroy.blob_id = id;
1449 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);