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"
67 #define memclear(s) memset(&s, 0, sizeof(s))
69 #define U642VOID(x) ((void *)(unsigned long)(x))
70 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
72 static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
74 int ret = drmIoctl(fd, cmd, arg);
75 return ret < 0 ? -errno : ret;
82 static void* drmAllocCpy(char *array, int count, int entry_size)
87 if (!count || !array || !entry_size)
90 if (!(r = drmMalloc(count*entry_size)))
93 for (i = 0; i < count; i++)
94 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
100 * A couple of free functions.
103 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
111 void drmModeFreeResources(drmModeResPtr ptr)
118 drmFree(ptr->connectors);
119 drmFree(ptr->encoders);
124 void drmModeFreeFB(drmModeFBPtr ptr)
129 /* we might add more frees later. */
133 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
142 void drmModeFreeConnector(drmModeConnectorPtr ptr)
147 drmFree(ptr->encoders);
148 drmFree(ptr->prop_values);
155 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
161 * ModeSetting functions.
164 drmModeResPtr drmModeGetResources(int fd)
166 struct drm_mode_card_res res, counts;
171 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
177 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
181 if (res.count_crtcs) {
182 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
183 if (!res.crtc_id_ptr)
186 if (res.count_connectors) {
187 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
188 if (!res.connector_id_ptr)
191 if (res.count_encoders) {
192 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
193 if (!res.encoder_id_ptr)
197 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
200 /* The number of available connectors and etc may have changed with a
201 * hotplug event in between the ioctls, in which case the field is
202 * silently ignored by the kernel.
204 if (counts.count_fbs < res.count_fbs ||
205 counts.count_crtcs < res.count_crtcs ||
206 counts.count_connectors < res.count_connectors ||
207 counts.count_encoders < res.count_encoders)
209 drmFree(U642VOID(res.fb_id_ptr));
210 drmFree(U642VOID(res.crtc_id_ptr));
211 drmFree(U642VOID(res.connector_id_ptr));
212 drmFree(U642VOID(res.encoder_id_ptr));
220 if (!(r = drmMalloc(sizeof(*r))))
223 r->min_width = res.min_width;
224 r->max_width = res.max_width;
225 r->min_height = res.min_height;
226 r->max_height = res.max_height;
227 r->count_fbs = res.count_fbs;
228 r->count_crtcs = res.count_crtcs;
229 r->count_connectors = res.count_connectors;
230 r->count_encoders = res.count_encoders;
232 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
233 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
234 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
235 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
236 if ((res.count_fbs && !r->fbs) ||
237 (res.count_crtcs && !r->crtcs) ||
238 (res.count_connectors && !r->connectors) ||
239 (res.count_encoders && !r->encoders))
243 drmFree(r->connectors);
244 drmFree(r->encoders);
250 drmFree(U642VOID(res.fb_id_ptr));
251 drmFree(U642VOID(res.crtc_id_ptr));
252 drmFree(U642VOID(res.connector_id_ptr));
253 drmFree(U642VOID(res.encoder_id_ptr));
258 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
259 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
262 struct drm_mode_fb_cmd f;
271 f.handle = bo_handle;
273 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
280 int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
281 uint32_t pixel_format, uint32_t bo_handles[4],
282 uint32_t pitches[4], uint32_t offsets[4],
283 uint32_t *buf_id, uint32_t flags)
285 struct drm_mode_fb_cmd2 f;
291 f.pixel_format = pixel_format;
293 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
294 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
295 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
297 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
304 int drmModeRmFB(int fd, uint32_t bufferId)
306 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
309 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
311 struct drm_mode_fb_cmd info;
317 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
320 if (!(r = drmMalloc(sizeof(*r))))
323 r->fb_id = info.fb_id;
324 r->width = info.width;
325 r->height = info.height;
326 r->pitch = info.pitch;
328 r->handle = info.handle;
329 r->depth = info.depth;
334 int drmModeDirtyFB(int fd, uint32_t bufferId,
335 drmModeClipPtr clips, uint32_t num_clips)
337 struct drm_mode_fb_dirty_cmd dirty;
340 dirty.fb_id = bufferId;
341 dirty.clips_ptr = VOID2U64(clips);
342 dirty.num_clips = num_clips;
344 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
352 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
354 struct drm_mode_crtc crtc;
358 crtc.crtc_id = crtcId;
360 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
367 if (!(r = drmMalloc(sizeof(*r))))
370 r->crtc_id = crtc.crtc_id;
373 r->mode_valid = crtc.mode_valid;
375 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
376 r->width = crtc.mode.hdisplay;
377 r->height = crtc.mode.vdisplay;
379 r->buffer_id = crtc.fb_id;
380 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;
489 conn.connector_id = connector_id;
491 conn.count_modes = 1;
492 conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
495 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
501 if (conn.count_props) {
502 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
505 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
506 if (!conn.prop_values_ptr)
510 if (conn.count_modes) {
511 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
515 conn.count_modes = 1;
516 conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
519 if (conn.count_encoders) {
520 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
521 if (!conn.encoders_ptr)
525 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
528 /* The number of available connectors and etc may have changed with a
529 * hotplug event in between the ioctls, in which case the field is
530 * silently ignored by the kernel.
532 if (counts.count_props < conn.count_props ||
533 counts.count_modes < conn.count_modes ||
534 counts.count_encoders < conn.count_encoders) {
535 drmFree(U642VOID(conn.props_ptr));
536 drmFree(U642VOID(conn.prop_values_ptr));
537 drmFree(U642VOID(conn.modes_ptr));
538 drmFree(U642VOID(conn.encoders_ptr));
543 if(!(r = drmMalloc(sizeof(*r)))) {
547 r->connector_id = conn.connector_id;
548 r->encoder_id = conn.encoder_id;
549 r->connection = conn.connection;
550 r->mmWidth = conn.mm_width;
551 r->mmHeight = conn.mm_height;
552 /* convert subpixel from kernel to userspace */
553 r->subpixel = conn.subpixel + 1;
554 r->count_modes = conn.count_modes;
555 r->count_props = conn.count_props;
556 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
557 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
558 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
559 r->count_encoders = conn.count_encoders;
560 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
561 r->connector_type = conn.connector_type;
562 r->connector_type_id = conn.connector_type_id;
564 if ((r->count_props && !r->props) ||
565 (r->count_props && !r->prop_values) ||
566 (r->count_modes && !r->modes) ||
567 (r->count_encoders && !r->encoders)) {
569 drmFree(r->prop_values);
571 drmFree(r->encoders);
577 drmFree(U642VOID(conn.prop_values_ptr));
578 drmFree(U642VOID(conn.props_ptr));
579 drmFree(U642VOID(conn.modes_ptr));
580 drmFree(U642VOID(conn.encoders_ptr));
585 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
587 return _drmModeGetConnector(fd, connector_id, 1);
590 drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
592 return _drmModeGetConnector(fd, connector_id, 0);
595 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
597 struct drm_mode_mode_cmd res;
600 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
601 res.connector_id = connector_id;
603 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
606 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
608 struct drm_mode_mode_cmd res;
611 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
612 res.connector_id = connector_id;
614 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
618 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
620 struct drm_mode_get_property prop;
621 drmModePropertyPtr r;
624 prop.prop_id = property_id;
626 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
629 if (prop.count_values)
630 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
632 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
633 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
635 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
636 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
637 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
640 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
645 if (!(r = drmMalloc(sizeof(*r))))
648 r->prop_id = prop.prop_id;
649 r->count_values = prop.count_values;
651 r->flags = prop.flags;
652 if (prop.count_values)
653 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
654 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
655 r->count_enums = prop.count_enum_blobs;
656 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
657 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
658 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
659 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
660 r->count_blobs = prop.count_enum_blobs;
662 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
663 r->name[DRM_PROP_NAME_LEN-1] = 0;
666 drmFree(U642VOID(prop.values_ptr));
667 drmFree(U642VOID(prop.enum_blob_ptr));
672 void drmModeFreeProperty(drmModePropertyPtr ptr)
677 drmFree(ptr->values);
682 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
684 struct drm_mode_get_blob blob;
685 drmModePropertyBlobPtr r;
688 blob.blob_id = blob_id;
690 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
694 blob.data = VOID2U64(drmMalloc(blob.length));
696 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
701 if (!(r = drmMalloc(sizeof(*r))))
704 r->id = blob.blob_id;
705 r->length = blob.length;
706 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
709 drmFree(U642VOID(blob.data));
713 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
722 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
725 struct drm_mode_connector_set_property osp;
728 osp.connector_id = connector_id;
729 osp.prop_id = property_id;
732 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
736 * checks if a modesetting capable driver has attached to the pci id
737 * returns 0 if modesetting supported.
738 * -EINVAL or invalid bus id
739 * -ENOSYS if no modesetting support
741 int drmCheckModesettingSupported(const char *busid)
743 #if defined (__linux__)
744 char pci_dev_dir[1024];
745 int domain, bus, dev, func;
750 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
754 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
755 domain, bus, dev, func);
757 sysdir = opendir(pci_dev_dir);
759 dent = readdir(sysdir);
761 if (!strncmp(dent->d_name, "controlD", 8)) {
766 dent = readdir(sysdir);
773 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
774 domain, bus, dev, func);
776 sysdir = opendir(pci_dev_dir);
780 dent = readdir(sysdir);
782 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
787 dent = readdir(sysdir);
793 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
794 char kbusid[1024], sbusid[1024];
796 int domain, bus, dev, func;
797 int i, modesetting, ret;
800 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
804 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
807 /* How many GPUs do we expect in the machine ? */
808 for (i = 0; i < 16; i++) {
809 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
810 len = sizeof(sbusid);
811 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
817 if (strcmp(sbusid, kbusid) != 0)
819 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
820 len = sizeof(modesetting);
821 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
822 if (ret == -1 || len != sizeof(modesetting))
824 return (modesetting ? 0 : -ENOSYS);
826 #elif defined(__DragonFly__)
833 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
834 uint16_t *red, uint16_t *green, uint16_t *blue)
836 struct drm_mode_crtc_lut l;
841 l.red = VOID2U64(red);
842 l.green = VOID2U64(green);
843 l.blue = VOID2U64(blue);
845 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
848 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
849 uint16_t *red, uint16_t *green, uint16_t *blue)
851 struct drm_mode_crtc_lut l;
856 l.red = VOID2U64(red);
857 l.green = VOID2U64(green);
858 l.blue = VOID2U64(blue);
860 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
863 int drmHandleEvent(int fd, drmEventContextPtr evctx)
868 struct drm_event_vblank *vblank;
870 /* The DRM read semantics guarantees that we always get only
871 * complete events. */
873 len = read(fd, buffer, sizeof buffer);
876 if (len < (int)sizeof *e)
881 e = (struct drm_event *) &buffer[i];
883 case DRM_EVENT_VBLANK:
884 if (evctx->version < 1 ||
885 evctx->vblank_handler == NULL)
887 vblank = (struct drm_event_vblank *) e;
888 evctx->vblank_handler(fd,
892 U642VOID (vblank->user_data));
894 case DRM_EVENT_FLIP_COMPLETE:
895 if (evctx->version < 2 ||
896 evctx->page_flip_handler == NULL)
898 vblank = (struct drm_event_vblank *) e;
899 evctx->page_flip_handler(fd,
903 U642VOID (vblank->user_data));
914 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
915 uint32_t flags, void *user_data)
917 struct drm_mode_crtc_page_flip flip;
921 flip.crtc_id = crtc_id;
922 flip.user_data = VOID2U64(user_data);
925 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
928 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
929 uint32_t fb_id, uint32_t flags,
930 int32_t crtc_x, int32_t crtc_y,
931 uint32_t crtc_w, uint32_t crtc_h,
932 uint32_t src_x, uint32_t src_y,
933 uint32_t src_w, uint32_t src_h)
936 struct drm_mode_set_plane s;
939 s.plane_id = plane_id;
952 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
956 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
958 struct drm_mode_get_plane ovr, counts;
959 drmModePlanePtr r = 0;
963 ovr.plane_id = plane_id;
964 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
969 if (ovr.count_format_types) {
970 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
972 if (!ovr.format_type_ptr)
976 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
979 if (counts.count_format_types < ovr.count_format_types) {
980 drmFree(U642VOID(ovr.format_type_ptr));
984 if (!(r = drmMalloc(sizeof(*r))))
987 r->count_formats = ovr.count_format_types;
988 r->plane_id = ovr.plane_id;
989 r->crtc_id = ovr.crtc_id;
990 r->fb_id = ovr.fb_id;
991 r->possible_crtcs = ovr.possible_crtcs;
992 r->gamma_size = ovr.gamma_size;
993 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
994 ovr.count_format_types, sizeof(uint32_t));
995 if (ovr.count_format_types && !r->formats) {
1002 drmFree(U642VOID(ovr.format_type_ptr));
1007 void drmModeFreePlane(drmModePlanePtr ptr)
1012 drmFree(ptr->formats);
1016 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1018 struct drm_mode_get_plane_res res, counts;
1019 drmModePlaneResPtr r = 0;
1023 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1028 if (res.count_planes) {
1029 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1031 if (!res.plane_id_ptr)
1035 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1038 if (counts.count_planes < res.count_planes) {
1039 drmFree(U642VOID(res.plane_id_ptr));
1043 if (!(r = drmMalloc(sizeof(*r))))
1046 r->count_planes = res.count_planes;
1047 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1048 res.count_planes, sizeof(uint32_t));
1049 if (res.count_planes && !r->planes) {
1056 drmFree(U642VOID(res.plane_id_ptr));
1061 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1066 drmFree(ptr->planes);
1070 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1072 uint32_t object_type)
1074 struct drm_mode_obj_get_properties properties;
1075 drmModeObjectPropertiesPtr ret = NULL;
1079 memclear(properties);
1080 properties.obj_id = object_id;
1081 properties.obj_type = object_type;
1083 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1086 count = properties.count_props;
1089 properties.props_ptr = VOID2U64(drmMalloc(count *
1091 if (!properties.props_ptr)
1093 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1095 if (!properties.prop_values_ptr)
1099 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1102 if (count < properties.count_props) {
1103 drmFree(U642VOID(properties.props_ptr));
1104 drmFree(U642VOID(properties.prop_values_ptr));
1107 count = properties.count_props;
1109 ret = drmMalloc(sizeof(*ret));
1113 ret->count_props = count;
1114 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1115 count, sizeof(uint32_t));
1116 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1117 count, sizeof(uint64_t));
1118 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1119 drmFree(ret->props);
1120 drmFree(ret->prop_values);
1126 drmFree(U642VOID(properties.props_ptr));
1127 drmFree(U642VOID(properties.prop_values_ptr));
1131 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1135 drmFree(ptr->props);
1136 drmFree(ptr->prop_values);
1140 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1141 uint32_t property_id, uint64_t value)
1143 struct drm_mode_obj_set_property prop;
1147 prop.prop_id = property_id;
1148 prop.obj_id = object_id;
1149 prop.obj_type = object_type;
1151 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1154 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1156 struct _drmModeAtomicReqItem {
1158 uint32_t property_id;
1162 struct _drmModeAtomicReq {
1164 uint32_t size_items;
1165 drmModeAtomicReqItemPtr items;
1168 drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1170 drmModeAtomicReqPtr req;
1172 req = drmMalloc(sizeof *req);
1178 req->size_items = 0;
1183 drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1185 drmModeAtomicReqPtr new;
1187 new = drmMalloc(sizeof *new);
1191 new->cursor = old->cursor;
1192 new->size_items = old->size_items;
1194 if (old->size_items) {
1195 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1200 memcpy(new->items, old->items,
1201 old->size_items * sizeof(*new->items));
1209 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1211 if (!augment || augment->cursor == 0)
1214 if (base->cursor + augment->cursor >= base->size_items) {
1215 drmModeAtomicReqItemPtr new;
1216 int saved_size = base->size_items;
1218 base->size_items = base->cursor + augment->cursor;
1219 new = realloc(base->items,
1220 base->size_items * sizeof(*base->items));
1222 base->size_items = saved_size;
1228 memcpy(&base->items[base->cursor], augment->items,
1229 augment->cursor * sizeof(*augment->items));
1230 base->cursor += augment->cursor;
1235 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1240 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1242 req->cursor = cursor;
1245 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1247 uint32_t property_id,
1250 if (req->cursor >= req->size_items) {
1251 drmModeAtomicReqItemPtr new;
1253 req->size_items += 16;
1254 new = realloc(req->items, req->size_items * sizeof(*req->items));
1256 req->size_items -= 16;
1262 req->items[req->cursor].object_id = object_id;
1263 req->items[req->cursor].property_id = property_id;
1264 req->items[req->cursor].value = value;
1270 void drmModeAtomicFree(drmModeAtomicReqPtr req)
1276 drmFree(req->items);
1280 static int sort_req_list(const void *misc, const void *other)
1282 const drmModeAtomicReqItem *first = misc;
1283 const drmModeAtomicReqItem *second = other;
1285 if (first->object_id < second->object_id)
1287 else if (first->object_id > second->object_id)
1290 return second->property_id - first->property_id;
1293 int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1296 drmModeAtomicReqPtr sorted = drmModeAtomicDuplicate(req);
1297 struct drm_mode_atomic atomic;
1298 uint32_t *objs_ptr = NULL;
1299 uint32_t *count_props_ptr = NULL;
1300 uint32_t *props_ptr = NULL;
1301 uint64_t *prop_values_ptr = NULL;
1302 uint32_t last_obj_id = 0;
1312 /* Sort the list by object ID, then by property ID. */
1313 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1316 /* Now the list is sorted, eliminate duplicate property sets. */
1317 for (i = 0; i < sorted->cursor; i++) {
1318 if (sorted->items[i].object_id != last_obj_id) {
1319 atomic.count_objs++;
1320 last_obj_id = sorted->items[i].object_id;
1323 if (i == sorted->cursor - 1)
1326 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1327 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1330 memmove(&sorted->items[i], &sorted->items[i + 1],
1331 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1335 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1341 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1342 if (!count_props_ptr) {
1347 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1353 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1354 if (!prop_values_ptr) {
1359 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1360 if (sorted->items[i].object_id != last_obj_id) {
1362 objs_ptr[obj_idx] = sorted->items[i].object_id;
1363 last_obj_id = objs_ptr[obj_idx];
1366 count_props_ptr[obj_idx]++;
1367 props_ptr[i] = sorted->items[i].property_id;
1368 prop_values_ptr[i] = sorted->items[i].value;
1372 atomic.flags = flags;
1373 atomic.objs_ptr = VOID2U64(objs_ptr);
1374 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1375 atomic.props_ptr = VOID2U64(props_ptr);
1376 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1377 atomic.user_data = VOID2U64(user_data);
1379 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1383 drmFree(count_props_ptr);
1385 drmFree(prop_values_ptr);
1386 drmModeAtomicFree(sorted);