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
43 #include <sys/ioctl.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);
116 void drmModeFreeFB(drmModeFBPtr ptr)
121 /* we might add more frees later. */
125 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
134 void drmModeFreeConnector(drmModeConnectorPtr ptr)
139 drmFree(ptr->encoders);
140 drmFree(ptr->prop_values);
147 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
153 * ModeSetting functions.
156 drmModeResPtr drmModeGetResources(int fd)
158 struct drm_mode_card_res res, counts;
163 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
169 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
173 if (res.count_crtcs) {
174 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
175 if (!res.crtc_id_ptr)
178 if (res.count_connectors) {
179 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
180 if (!res.connector_id_ptr)
183 if (res.count_encoders) {
184 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
185 if (!res.encoder_id_ptr)
189 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
192 /* The number of available connectors and etc may have changed with a
193 * hotplug event in between the ioctls, in which case the field is
194 * silently ignored by the kernel.
196 if (counts.count_fbs < res.count_fbs ||
197 counts.count_crtcs < res.count_crtcs ||
198 counts.count_connectors < res.count_connectors ||
199 counts.count_encoders < res.count_encoders)
201 drmFree(U642VOID(res.fb_id_ptr));
202 drmFree(U642VOID(res.crtc_id_ptr));
203 drmFree(U642VOID(res.connector_id_ptr));
204 drmFree(U642VOID(res.encoder_id_ptr));
212 if (!(r = drmMalloc(sizeof(*r))))
215 r->min_width = res.min_width;
216 r->max_width = res.max_width;
217 r->min_height = res.min_height;
218 r->max_height = res.max_height;
219 r->count_fbs = res.count_fbs;
220 r->count_crtcs = res.count_crtcs;
221 r->count_connectors = res.count_connectors;
222 r->count_encoders = res.count_encoders;
224 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
225 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
226 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
227 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
228 if ((res.count_fbs && !r->fbs) ||
229 (res.count_crtcs && !r->crtcs) ||
230 (res.count_connectors && !r->connectors) ||
231 (res.count_encoders && !r->encoders))
235 drmFree(r->connectors);
236 drmFree(r->encoders);
242 drmFree(U642VOID(res.fb_id_ptr));
243 drmFree(U642VOID(res.crtc_id_ptr));
244 drmFree(U642VOID(res.connector_id_ptr));
245 drmFree(U642VOID(res.encoder_id_ptr));
250 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
251 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
254 struct drm_mode_fb_cmd f;
263 f.handle = bo_handle;
265 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
272 int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
273 uint32_t pixel_format, uint32_t bo_handles[4],
274 uint32_t pitches[4], uint32_t offsets[4],
275 uint32_t *buf_id, uint32_t flags)
277 struct drm_mode_fb_cmd2 f;
283 f.pixel_format = pixel_format;
285 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
286 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
287 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
289 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
296 int drmModeRmFB(int fd, uint32_t bufferId)
298 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
301 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
303 struct drm_mode_fb_cmd info;
309 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
312 if (!(r = drmMalloc(sizeof(*r))))
315 r->fb_id = info.fb_id;
316 r->width = info.width;
317 r->height = info.height;
318 r->pitch = info.pitch;
320 r->handle = info.handle;
321 r->depth = info.depth;
326 int drmModeDirtyFB(int fd, uint32_t bufferId,
327 drmModeClipPtr clips, uint32_t num_clips)
329 struct drm_mode_fb_dirty_cmd dirty;
332 dirty.fb_id = bufferId;
333 dirty.clips_ptr = VOID2U64(clips);
334 dirty.num_clips = num_clips;
336 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;
377 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
378 uint32_t x, uint32_t y, uint32_t *connectors, int count,
379 drmModeModeInfoPtr mode)
381 struct drm_mode_crtc crtc;
386 crtc.crtc_id = crtcId;
387 crtc.fb_id = bufferId;
388 crtc.set_connectors_ptr = VOID2U64(connectors);
389 crtc.count_connectors = count;
391 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
395 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
399 * Cursor manipulation
402 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
404 struct drm_mode_cursor arg;
407 arg.flags = DRM_MODE_CURSOR_BO;
408 arg.crtc_id = crtcId;
411 arg.handle = bo_handle;
413 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
416 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)
418 struct drm_mode_cursor2 arg;
421 arg.flags = DRM_MODE_CURSOR_BO;
422 arg.crtc_id = crtcId;
425 arg.handle = bo_handle;
429 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
432 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
434 struct drm_mode_cursor arg;
437 arg.flags = DRM_MODE_CURSOR_MOVE;
438 arg.crtc_id = crtcId;
442 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
448 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
450 struct drm_mode_get_encoder enc;
451 drmModeEncoderPtr r = NULL;
454 enc.encoder_id = encoder_id;
456 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
459 if (!(r = drmMalloc(sizeof(*r))))
462 r->encoder_id = enc.encoder_id;
463 r->crtc_id = enc.crtc_id;
464 r->encoder_type = enc.encoder_type;
465 r->possible_crtcs = enc.possible_crtcs;
466 r->possible_clones = enc.possible_clones;
472 * Connector manipulation
474 static drmModeConnectorPtr
475 _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
477 struct drm_mode_get_connector conn, counts;
478 drmModeConnectorPtr r = NULL;
481 conn.connector_id = connector_id;
483 conn.count_modes = 1;
484 conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
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(drmMalloc(sizeof(struct drm_mode_modeinfo)));
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 drmFree(U642VOID(conn.modes_ptr));
530 drmFree(U642VOID(conn.encoders_ptr));
535 if(!(r = drmMalloc(sizeof(*r)))) {
539 r->connector_id = conn.connector_id;
540 r->encoder_id = conn.encoder_id;
541 r->connection = conn.connection;
542 r->mmWidth = conn.mm_width;
543 r->mmHeight = conn.mm_height;
544 /* convert subpixel from kernel to userspace */
545 r->subpixel = conn.subpixel + 1;
546 r->count_modes = conn.count_modes;
547 r->count_props = conn.count_props;
548 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
549 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
550 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
551 r->count_encoders = conn.count_encoders;
552 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
553 r->connector_type = conn.connector_type;
554 r->connector_type_id = conn.connector_type_id;
556 if ((r->count_props && !r->props) ||
557 (r->count_props && !r->prop_values) ||
558 (r->count_modes && !r->modes) ||
559 (r->count_encoders && !r->encoders)) {
561 drmFree(r->prop_values);
563 drmFree(r->encoders);
569 drmFree(U642VOID(conn.prop_values_ptr));
570 drmFree(U642VOID(conn.props_ptr));
571 drmFree(U642VOID(conn.modes_ptr));
572 drmFree(U642VOID(conn.encoders_ptr));
577 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
579 return _drmModeGetConnector(fd, connector_id, 1);
582 drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
584 return _drmModeGetConnector(fd, connector_id, 0);
587 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
589 struct drm_mode_mode_cmd res;
592 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
593 res.connector_id = connector_id;
595 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
598 int drmModeDetachMode(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_DETACHMODE, &res);
610 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
612 struct drm_mode_get_property prop;
613 drmModePropertyPtr r;
616 prop.prop_id = property_id;
618 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
621 if (prop.count_values)
622 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
624 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
625 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
627 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
628 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
629 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
632 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
637 if (!(r = drmMalloc(sizeof(*r))))
640 r->prop_id = prop.prop_id;
641 r->count_values = prop.count_values;
643 r->flags = prop.flags;
644 if (prop.count_values)
645 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
646 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
647 r->count_enums = prop.count_enum_blobs;
648 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
649 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
650 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
651 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
652 r->count_blobs = prop.count_enum_blobs;
654 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
655 r->name[DRM_PROP_NAME_LEN-1] = 0;
658 drmFree(U642VOID(prop.values_ptr));
659 drmFree(U642VOID(prop.enum_blob_ptr));
664 void drmModeFreeProperty(drmModePropertyPtr ptr)
669 drmFree(ptr->values);
674 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
676 struct drm_mode_get_blob blob;
677 drmModePropertyBlobPtr r;
680 blob.blob_id = blob_id;
682 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
686 blob.data = VOID2U64(drmMalloc(blob.length));
688 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
693 if (!(r = drmMalloc(sizeof(*r))))
696 r->id = blob.blob_id;
697 r->length = blob.length;
698 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
701 drmFree(U642VOID(blob.data));
705 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
714 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
717 struct drm_mode_connector_set_property osp;
720 osp.connector_id = connector_id;
721 osp.prop_id = property_id;
724 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
728 * checks if a modesetting capable driver has attached to the pci id
729 * returns 0 if modesetting supported.
730 * -EINVAL or invalid bus id
731 * -ENOSYS if no modesetting support
733 int drmCheckModesettingSupported(const char *busid)
735 #if defined (__linux__)
736 char pci_dev_dir[1024];
737 int domain, bus, dev, func;
742 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
746 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
747 domain, bus, dev, func);
749 sysdir = opendir(pci_dev_dir);
751 dent = readdir(sysdir);
753 if (!strncmp(dent->d_name, "controlD", 8)) {
758 dent = readdir(sysdir);
765 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
766 domain, bus, dev, func);
768 sysdir = opendir(pci_dev_dir);
772 dent = readdir(sysdir);
774 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
779 dent = readdir(sysdir);
785 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
786 char kbusid[1024], sbusid[1024];
788 int domain, bus, dev, func;
789 int i, modesetting, ret;
792 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
796 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
799 /* How many GPUs do we expect in the machine ? */
800 for (i = 0; i < 16; i++) {
801 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
802 len = sizeof(sbusid);
803 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
809 if (strcmp(sbusid, kbusid) != 0)
811 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
812 len = sizeof(modesetting);
813 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
814 if (ret == -1 || len != sizeof(modesetting))
816 return (modesetting ? 0 : -ENOSYS);
818 #elif defined(__DragonFly__)
825 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
826 uint16_t *red, uint16_t *green, uint16_t *blue)
828 struct drm_mode_crtc_lut l;
833 l.red = VOID2U64(red);
834 l.green = VOID2U64(green);
835 l.blue = VOID2U64(blue);
837 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
840 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
841 uint16_t *red, uint16_t *green, uint16_t *blue)
843 struct drm_mode_crtc_lut l;
848 l.red = VOID2U64(red);
849 l.green = VOID2U64(green);
850 l.blue = VOID2U64(blue);
852 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
855 int drmHandleEvent(int fd, drmEventContextPtr evctx)
860 struct drm_event_vblank *vblank;
862 /* The DRM read semantics guarantees that we always get only
863 * complete events. */
865 len = read(fd, buffer, sizeof buffer);
868 if (len < (int)sizeof *e)
873 e = (struct drm_event *) &buffer[i];
875 case DRM_EVENT_VBLANK:
876 if (evctx->version < 1 ||
877 evctx->vblank_handler == NULL)
879 vblank = (struct drm_event_vblank *) e;
880 evctx->vblank_handler(fd,
884 U642VOID (vblank->user_data));
886 case DRM_EVENT_FLIP_COMPLETE:
887 if (evctx->version < 2 ||
888 evctx->page_flip_handler == NULL)
890 vblank = (struct drm_event_vblank *) e;
891 evctx->page_flip_handler(fd,
895 U642VOID (vblank->user_data));
906 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
907 uint32_t flags, void *user_data)
909 struct drm_mode_crtc_page_flip flip;
913 flip.crtc_id = crtc_id;
914 flip.user_data = VOID2U64(user_data);
917 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
920 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
921 uint32_t fb_id, uint32_t flags,
922 int32_t crtc_x, int32_t crtc_y,
923 uint32_t crtc_w, uint32_t crtc_h,
924 uint32_t src_x, uint32_t src_y,
925 uint32_t src_w, uint32_t src_h)
928 struct drm_mode_set_plane s;
931 s.plane_id = plane_id;
944 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
948 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
950 struct drm_mode_get_plane ovr, counts;
951 drmModePlanePtr r = 0;
955 ovr.plane_id = plane_id;
956 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
961 if (ovr.count_format_types) {
962 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
964 if (!ovr.format_type_ptr)
968 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
971 if (counts.count_format_types < ovr.count_format_types) {
972 drmFree(U642VOID(ovr.format_type_ptr));
976 if (!(r = drmMalloc(sizeof(*r))))
979 r->count_formats = ovr.count_format_types;
980 r->plane_id = ovr.plane_id;
981 r->crtc_id = ovr.crtc_id;
982 r->fb_id = ovr.fb_id;
983 r->possible_crtcs = ovr.possible_crtcs;
984 r->gamma_size = ovr.gamma_size;
985 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
986 ovr.count_format_types, sizeof(uint32_t));
987 if (ovr.count_format_types && !r->formats) {
994 drmFree(U642VOID(ovr.format_type_ptr));
999 void drmModeFreePlane(drmModePlanePtr ptr)
1004 drmFree(ptr->formats);
1008 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1010 struct drm_mode_get_plane_res res, counts;
1011 drmModePlaneResPtr r = 0;
1015 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1020 if (res.count_planes) {
1021 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1023 if (!res.plane_id_ptr)
1027 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1030 if (counts.count_planes < res.count_planes) {
1031 drmFree(U642VOID(res.plane_id_ptr));
1035 if (!(r = drmMalloc(sizeof(*r))))
1038 r->count_planes = res.count_planes;
1039 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1040 res.count_planes, sizeof(uint32_t));
1041 if (res.count_planes && !r->planes) {
1048 drmFree(U642VOID(res.plane_id_ptr));
1053 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1058 drmFree(ptr->planes);
1062 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1064 uint32_t object_type)
1066 struct drm_mode_obj_get_properties properties;
1067 drmModeObjectPropertiesPtr ret = NULL;
1071 memclear(properties);
1072 properties.obj_id = object_id;
1073 properties.obj_type = object_type;
1075 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1078 count = properties.count_props;
1081 properties.props_ptr = VOID2U64(drmMalloc(count *
1083 if (!properties.props_ptr)
1085 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1087 if (!properties.prop_values_ptr)
1091 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1094 if (count < properties.count_props) {
1095 drmFree(U642VOID(properties.props_ptr));
1096 drmFree(U642VOID(properties.prop_values_ptr));
1099 count = properties.count_props;
1101 ret = drmMalloc(sizeof(*ret));
1105 ret->count_props = count;
1106 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1107 count, sizeof(uint32_t));
1108 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1109 count, sizeof(uint64_t));
1110 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1111 drmFree(ret->props);
1112 drmFree(ret->prop_values);
1118 drmFree(U642VOID(properties.props_ptr));
1119 drmFree(U642VOID(properties.prop_values_ptr));
1123 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1127 drmFree(ptr->props);
1128 drmFree(ptr->prop_values);
1132 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1133 uint32_t property_id, uint64_t value)
1135 struct drm_mode_obj_set_property prop;
1139 prop.prop_id = property_id;
1140 prop.obj_id = object_id;
1141 prop.obj_type = object_type;
1143 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1146 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1148 struct _drmModeAtomicReqItem {
1150 uint32_t property_id;
1154 struct _drmModeAtomicReq {
1156 uint32_t size_items;
1157 drmModeAtomicReqItemPtr items;
1160 drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1162 drmModeAtomicReqPtr req;
1164 req = drmMalloc(sizeof *req);
1170 req->size_items = 0;
1175 drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1177 drmModeAtomicReqPtr new;
1179 new = drmMalloc(sizeof *new);
1183 new->cursor = old->cursor;
1184 new->size_items = old->size_items;
1186 if (old->size_items) {
1187 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1192 memcpy(new->items, old->items,
1193 old->size_items * sizeof(*new->items));
1201 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1203 if (!augment || augment->cursor == 0)
1206 if (base->cursor + augment->cursor >= base->size_items) {
1207 drmModeAtomicReqItemPtr new;
1208 int saved_size = base->size_items;
1210 base->size_items = base->cursor + augment->cursor;
1211 new = realloc(base->items,
1212 base->size_items * sizeof(*base->items));
1214 base->size_items = saved_size;
1220 memcpy(&base->items[base->cursor], augment->items,
1221 augment->cursor * sizeof(*augment->items));
1222 base->cursor += augment->cursor;
1227 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1232 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1234 req->cursor = cursor;
1237 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1239 uint32_t property_id,
1242 if (req->cursor >= req->size_items) {
1243 drmModeAtomicReqItemPtr new;
1245 req->size_items += 16;
1246 new = realloc(req->items, req->size_items * sizeof(*req->items));
1248 req->size_items -= 16;
1254 req->items[req->cursor].object_id = object_id;
1255 req->items[req->cursor].property_id = property_id;
1256 req->items[req->cursor].value = value;
1262 void drmModeAtomicFree(drmModeAtomicReqPtr req)
1268 drmFree(req->items);
1272 static int sort_req_list(const void *misc, const void *other)
1274 const drmModeAtomicReqItem *first = misc;
1275 const drmModeAtomicReqItem *second = other;
1277 if (first->object_id < second->object_id)
1279 else if (first->object_id > second->object_id)
1282 return second->property_id - first->property_id;
1285 int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1288 drmModeAtomicReqPtr sorted = drmModeAtomicDuplicate(req);
1289 struct drm_mode_atomic atomic;
1290 uint32_t *objs_ptr = NULL;
1291 uint32_t *count_props_ptr = NULL;
1292 uint32_t *props_ptr = NULL;
1293 uint64_t *prop_values_ptr = NULL;
1294 uint32_t last_obj_id = 0;
1304 /* Sort the list by object ID, then by property ID. */
1305 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1308 /* Now the list is sorted, eliminate duplicate property sets. */
1309 for (i = 0; i < sorted->cursor; i++) {
1310 if (sorted->items[i].object_id != last_obj_id) {
1311 atomic.count_objs++;
1312 last_obj_id = sorted->items[i].object_id;
1315 if (i == sorted->cursor - 1)
1318 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1319 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1322 memmove(&sorted->items[i], &sorted->items[i + 1],
1323 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1327 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1333 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1334 if (!count_props_ptr) {
1339 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1345 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1346 if (!prop_values_ptr) {
1351 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1352 if (sorted->items[i].object_id != last_obj_id) {
1354 objs_ptr[obj_idx] = sorted->items[i].object_id;
1355 last_obj_id = objs_ptr[obj_idx];
1358 count_props_ptr[obj_idx]++;
1359 props_ptr[i] = sorted->items[i].property_id;
1360 prop_values_ptr[i] = sorted->items[i].value;
1364 atomic.flags = flags;
1365 atomic.objs_ptr = VOID2U64(objs_ptr);
1366 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1367 atomic.props_ptr = VOID2U64(props_ptr);
1368 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1369 atomic.user_data = VOID2U64(user_data);
1371 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1375 drmFree(count_props_ptr);
1377 drmFree(prop_values_ptr);
1378 drmModeAtomicFree(sorted);
1384 drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1386 struct drm_mode_create_blob create;
1389 if (length >= 0xffffffff)
1394 create.length = length;
1395 create.data = (uintptr_t) data;
1399 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1403 *id = create.blob_id;
1408 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1410 struct drm_mode_destroy_blob destroy;
1413 destroy.blob_id = id;
1414 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);