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>
46 #include <sys/sysctl.h>
51 #include "libdrm_macros.h"
52 #include "xf86drmMode.h"
60 #define memclear(s) memset(&s, 0, sizeof(s))
62 #define U642VOID(x) ((void *)(unsigned long)(x))
63 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
65 static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
67 int ret = drmIoctl(fd, cmd, arg);
68 return ret < 0 ? -errno : ret;
75 static void* drmAllocCpy(char *array, int count, int entry_size)
80 if (!count || !array || !entry_size)
83 if (!(r = drmMalloc(count*entry_size)))
86 for (i = 0; i < count; i++)
87 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
93 * A couple of free functions.
96 drm_public void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
104 drm_public void drmModeFreeResources(drmModeResPtr ptr)
111 drmFree(ptr->connectors);
112 drmFree(ptr->encoders);
116 drm_public void drmModeFreeFB(drmModeFBPtr ptr)
121 /* we might add more frees later. */
125 drm_public void drmModeFreeCrtc(drmModeCrtcPtr ptr)
133 drm_public void drmModeFreeConnector(drmModeConnectorPtr ptr)
138 drmFree(ptr->encoders);
139 drmFree(ptr->prop_values);
145 drm_public void drmModeFreeEncoder(drmModeEncoderPtr ptr)
151 * ModeSetting functions.
154 drm_public drmModeResPtr drmModeGetResources(int fd)
156 struct drm_mode_card_res res, counts;
161 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
167 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
171 if (res.count_crtcs) {
172 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
173 if (!res.crtc_id_ptr)
176 if (res.count_connectors) {
177 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
178 if (!res.connector_id_ptr)
181 if (res.count_encoders) {
182 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
183 if (!res.encoder_id_ptr)
187 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
190 /* The number of available connectors and etc may have changed with a
191 * hotplug event in between the ioctls, in which case the field is
192 * silently ignored by the kernel.
194 if (counts.count_fbs < res.count_fbs ||
195 counts.count_crtcs < res.count_crtcs ||
196 counts.count_connectors < res.count_connectors ||
197 counts.count_encoders < res.count_encoders)
199 drmFree(U642VOID(res.fb_id_ptr));
200 drmFree(U642VOID(res.crtc_id_ptr));
201 drmFree(U642VOID(res.connector_id_ptr));
202 drmFree(U642VOID(res.encoder_id_ptr));
210 if (!(r = drmMalloc(sizeof(*r))))
213 r->min_width = res.min_width;
214 r->max_width = res.max_width;
215 r->min_height = res.min_height;
216 r->max_height = res.max_height;
217 r->count_fbs = res.count_fbs;
218 r->count_crtcs = res.count_crtcs;
219 r->count_connectors = res.count_connectors;
220 r->count_encoders = res.count_encoders;
222 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
223 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
224 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
225 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
226 if ((res.count_fbs && !r->fbs) ||
227 (res.count_crtcs && !r->crtcs) ||
228 (res.count_connectors && !r->connectors) ||
229 (res.count_encoders && !r->encoders))
233 drmFree(r->connectors);
234 drmFree(r->encoders);
240 drmFree(U642VOID(res.fb_id_ptr));
241 drmFree(U642VOID(res.crtc_id_ptr));
242 drmFree(U642VOID(res.connector_id_ptr));
243 drmFree(U642VOID(res.encoder_id_ptr));
249 drm_public int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
250 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
253 struct drm_mode_fb_cmd f;
262 f.handle = bo_handle;
264 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
271 drm_public int drmModeAddFB2WithModifiers(int fd, uint32_t width,
272 uint32_t height, uint32_t pixel_format, const uint32_t bo_handles[4],
273 const uint32_t pitches[4], const uint32_t offsets[4],
274 const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
276 struct drm_mode_fb_cmd2 f;
282 f.pixel_format = pixel_format;
284 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
285 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
286 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
288 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
290 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
297 drm_public int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
298 uint32_t pixel_format, const uint32_t bo_handles[4],
299 const uint32_t pitches[4], const uint32_t offsets[4],
300 uint32_t *buf_id, uint32_t flags)
302 return drmModeAddFB2WithModifiers(fd, width, height,
303 pixel_format, bo_handles,
304 pitches, offsets, NULL,
308 drm_public int drmModeRmFB(int fd, uint32_t bufferId)
310 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
313 drm_public drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
315 struct drm_mode_fb_cmd info;
321 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
324 if (!(r = drmMalloc(sizeof(*r))))
327 r->fb_id = info.fb_id;
328 r->width = info.width;
329 r->height = info.height;
330 r->pitch = info.pitch;
332 r->handle = info.handle;
333 r->depth = info.depth;
338 drm_public int drmModeDirtyFB(int fd, uint32_t bufferId,
339 drmModeClipPtr clips, uint32_t num_clips)
341 struct drm_mode_fb_dirty_cmd dirty;
344 dirty.fb_id = bufferId;
345 dirty.clips_ptr = VOID2U64(clips);
346 dirty.num_clips = num_clips;
348 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
355 drm_public drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
357 struct drm_mode_crtc crtc;
361 crtc.crtc_id = crtcId;
363 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
370 if (!(r = drmMalloc(sizeof(*r))))
373 r->crtc_id = crtc.crtc_id;
376 r->mode_valid = crtc.mode_valid;
378 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
379 r->width = crtc.mode.hdisplay;
380 r->height = crtc.mode.vdisplay;
382 r->buffer_id = crtc.fb_id;
383 r->gamma_size = crtc.gamma_size;
387 drm_public int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
388 uint32_t x, uint32_t y, uint32_t *connectors, int count,
389 drmModeModeInfoPtr mode)
391 struct drm_mode_crtc crtc;
396 crtc.crtc_id = crtcId;
397 crtc.fb_id = bufferId;
398 crtc.set_connectors_ptr = VOID2U64(connectors);
399 crtc.count_connectors = count;
401 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
405 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
409 * Cursor manipulation
412 drm_public int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle,
413 uint32_t width, uint32_t height)
415 struct drm_mode_cursor arg;
418 arg.flags = DRM_MODE_CURSOR_BO;
419 arg.crtc_id = crtcId;
422 arg.handle = bo_handle;
424 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
427 drm_public int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle,
428 uint32_t width, uint32_t height, int32_t hot_x,
431 struct drm_mode_cursor2 arg;
434 arg.flags = DRM_MODE_CURSOR_BO;
435 arg.crtc_id = crtcId;
438 arg.handle = bo_handle;
442 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
445 drm_public int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
447 struct drm_mode_cursor arg;
450 arg.flags = DRM_MODE_CURSOR_MOVE;
451 arg.crtc_id = crtcId;
455 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
461 drm_public drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
463 struct drm_mode_get_encoder enc;
464 drmModeEncoderPtr r = NULL;
467 enc.encoder_id = encoder_id;
469 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
472 if (!(r = drmMalloc(sizeof(*r))))
475 r->encoder_id = enc.encoder_id;
476 r->crtc_id = enc.crtc_id;
477 r->encoder_type = enc.encoder_type;
478 r->possible_crtcs = enc.possible_crtcs;
479 r->possible_clones = enc.possible_clones;
485 * Connector manipulation
487 static drmModeConnectorPtr
488 _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
490 struct drm_mode_get_connector conn, counts;
491 drmModeConnectorPtr r = NULL;
492 struct drm_mode_modeinfo stack_mode;
495 conn.connector_id = connector_id;
497 conn.count_modes = 1;
498 conn.modes_ptr = VOID2U64(&stack_mode);
501 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
507 if (conn.count_props) {
508 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
511 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
512 if (!conn.prop_values_ptr)
516 if (conn.count_modes) {
517 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
521 conn.count_modes = 1;
522 conn.modes_ptr = VOID2U64(&stack_mode);
525 if (conn.count_encoders) {
526 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
527 if (!conn.encoders_ptr)
531 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
534 /* The number of available connectors and etc may have changed with a
535 * hotplug event in between the ioctls, in which case the field is
536 * silently ignored by the kernel.
538 if (counts.count_props < conn.count_props ||
539 counts.count_modes < conn.count_modes ||
540 counts.count_encoders < conn.count_encoders) {
541 drmFree(U642VOID(conn.props_ptr));
542 drmFree(U642VOID(conn.prop_values_ptr));
543 if (U642VOID(conn.modes_ptr) != &stack_mode)
544 drmFree(U642VOID(conn.modes_ptr));
545 drmFree(U642VOID(conn.encoders_ptr));
550 if(!(r = drmMalloc(sizeof(*r)))) {
554 r->connector_id = conn.connector_id;
555 r->encoder_id = conn.encoder_id;
556 r->connection = conn.connection;
557 r->mmWidth = conn.mm_width;
558 r->mmHeight = conn.mm_height;
559 /* convert subpixel from kernel to userspace */
560 r->subpixel = conn.subpixel + 1;
561 r->count_modes = conn.count_modes;
562 r->count_props = conn.count_props;
563 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
564 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
565 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
566 r->count_encoders = conn.count_encoders;
567 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
568 r->connector_type = conn.connector_type;
569 r->connector_type_id = conn.connector_type_id;
571 if ((r->count_props && !r->props) ||
572 (r->count_props && !r->prop_values) ||
573 (r->count_modes && !r->modes) ||
574 (r->count_encoders && !r->encoders)) {
576 drmFree(r->prop_values);
578 drmFree(r->encoders);
584 drmFree(U642VOID(conn.prop_values_ptr));
585 drmFree(U642VOID(conn.props_ptr));
586 if (U642VOID(conn.modes_ptr) != &stack_mode)
587 drmFree(U642VOID(conn.modes_ptr));
588 drmFree(U642VOID(conn.encoders_ptr));
593 drm_public drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
595 return _drmModeGetConnector(fd, connector_id, 1);
598 drm_public drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
600 return _drmModeGetConnector(fd, connector_id, 0);
603 drm_public int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
605 struct drm_mode_mode_cmd res;
608 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
609 res.connector_id = connector_id;
611 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
614 drm_public int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
616 struct drm_mode_mode_cmd res;
619 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
620 res.connector_id = connector_id;
622 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
625 drm_public drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
627 struct drm_mode_get_property prop;
628 drmModePropertyPtr r;
631 prop.prop_id = property_id;
633 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
636 if (prop.count_values)
637 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
639 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
640 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
642 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
643 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
644 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
647 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
652 if (!(r = drmMalloc(sizeof(*r))))
655 r->prop_id = prop.prop_id;
656 r->count_values = prop.count_values;
658 r->flags = prop.flags;
659 if (prop.count_values)
660 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
661 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
662 r->count_enums = prop.count_enum_blobs;
663 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
664 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
665 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
666 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
667 r->count_blobs = prop.count_enum_blobs;
669 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
670 r->name[DRM_PROP_NAME_LEN-1] = 0;
673 drmFree(U642VOID(prop.values_ptr));
674 drmFree(U642VOID(prop.enum_blob_ptr));
679 drm_public void drmModeFreeProperty(drmModePropertyPtr ptr)
684 drmFree(ptr->values);
686 drmFree(ptr->blob_ids);
690 drm_public drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd,
693 struct drm_mode_get_blob blob;
694 drmModePropertyBlobPtr r;
697 blob.blob_id = blob_id;
699 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
703 blob.data = VOID2U64(drmMalloc(blob.length));
705 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
710 if (!(r = drmMalloc(sizeof(*r))))
713 r->id = blob.blob_id;
714 r->length = blob.length;
715 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
718 drmFree(U642VOID(blob.data));
722 drm_public void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
731 drm_public int drmModeConnectorSetProperty(int fd, uint32_t connector_id,
732 uint32_t property_id,
735 struct drm_mode_connector_set_property osp;
738 osp.connector_id = connector_id;
739 osp.prop_id = property_id;
742 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
746 * checks if a modesetting capable driver has attached to the pci id
747 * returns 0 if modesetting supported.
748 * -EINVAL or invalid bus id
749 * -ENOSYS if no modesetting support
751 drm_public int drmCheckModesettingSupported(const char *busid)
753 #if defined (__linux__)
754 char pci_dev_dir[1024];
755 int domain, bus, dev, func;
760 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
764 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
765 domain, bus, dev, func);
767 sysdir = opendir(pci_dev_dir);
769 dent = readdir(sysdir);
771 if (!strncmp(dent->d_name, "controlD", 8)) {
776 dent = readdir(sysdir);
783 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
784 domain, bus, dev, func);
786 sysdir = opendir(pci_dev_dir);
790 dent = readdir(sysdir);
792 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
797 dent = readdir(sysdir);
803 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
806 int i, modesetting, ret;
809 /* How many GPUs do we expect in the machine ? */
810 for (i = 0; i < 10; 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, busid) != 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 drm_public int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
852 uint16_t *red, uint16_t *green,
855 struct drm_mode_crtc_lut l;
860 l.red = VOID2U64(red);
861 l.green = VOID2U64(green);
862 l.blue = VOID2U64(blue);
864 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
867 drm_public int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
868 uint16_t *red, uint16_t *green,
871 struct drm_mode_crtc_lut l;
876 l.red = VOID2U64(red);
877 l.green = VOID2U64(green);
878 l.blue = VOID2U64(blue);
880 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
883 drm_public int drmHandleEvent(int fd, drmEventContextPtr evctx)
888 struct drm_event_vblank *vblank;
889 struct drm_event_crtc_sequence *seq;
892 /* The DRM read semantics guarantees that we always get only
893 * complete events. */
895 len = read(fd, buffer, sizeof buffer);
898 if (len < (int)sizeof *e)
903 e = (struct drm_event *)(buffer + i);
905 case DRM_EVENT_VBLANK:
906 if (evctx->version < 1 ||
907 evctx->vblank_handler == NULL)
909 vblank = (struct drm_event_vblank *) e;
910 evctx->vblank_handler(fd,
914 U642VOID (vblank->user_data));
916 case DRM_EVENT_FLIP_COMPLETE:
917 vblank = (struct drm_event_vblank *) e;
918 user_data = U642VOID (vblank->user_data);
920 if (evctx->version >= 3 && evctx->page_flip_handler2)
921 evctx->page_flip_handler2(fd,
927 else if (evctx->version >= 2 && evctx->page_flip_handler)
928 evctx->page_flip_handler(fd,
934 case DRM_EVENT_CRTC_SEQUENCE:
935 seq = (struct drm_event_crtc_sequence *) e;
936 if (evctx->version >= 4 && evctx->sequence_handler)
937 evctx->sequence_handler(fd,
951 drm_public int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
952 uint32_t flags, void *user_data)
954 struct drm_mode_crtc_page_flip flip;
958 flip.crtc_id = crtc_id;
959 flip.user_data = VOID2U64(user_data);
962 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
965 drm_public int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
966 uint32_t flags, void *user_data,
967 uint32_t target_vblank)
969 struct drm_mode_crtc_page_flip_target flip_target;
971 memclear(flip_target);
972 flip_target.fb_id = fb_id;
973 flip_target.crtc_id = crtc_id;
974 flip_target.user_data = VOID2U64(user_data);
975 flip_target.flags = flags;
976 flip_target.sequence = target_vblank;
978 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
981 drm_public int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
982 uint32_t fb_id, uint32_t flags,
983 int32_t crtc_x, int32_t crtc_y,
984 uint32_t crtc_w, uint32_t crtc_h,
985 uint32_t src_x, uint32_t src_y,
986 uint32_t src_w, uint32_t src_h)
988 struct drm_mode_set_plane s;
991 s.plane_id = plane_id;
1004 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1007 drm_public drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
1009 struct drm_mode_get_plane ovr, counts;
1010 drmModePlanePtr r = 0;
1014 ovr.plane_id = plane_id;
1015 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1020 if (ovr.count_format_types) {
1021 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1023 if (!ovr.format_type_ptr)
1027 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1030 if (counts.count_format_types < ovr.count_format_types) {
1031 drmFree(U642VOID(ovr.format_type_ptr));
1035 if (!(r = drmMalloc(sizeof(*r))))
1038 r->count_formats = ovr.count_format_types;
1039 r->plane_id = ovr.plane_id;
1040 r->crtc_id = ovr.crtc_id;
1041 r->fb_id = ovr.fb_id;
1042 r->possible_crtcs = ovr.possible_crtcs;
1043 r->gamma_size = ovr.gamma_size;
1044 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1045 ovr.count_format_types, sizeof(uint32_t));
1046 if (ovr.count_format_types && !r->formats) {
1047 drmFree(r->formats);
1053 drmFree(U642VOID(ovr.format_type_ptr));
1058 drm_public void drmModeFreePlane(drmModePlanePtr ptr)
1063 drmFree(ptr->formats);
1067 drm_public drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1069 struct drm_mode_get_plane_res res, counts;
1070 drmModePlaneResPtr r = 0;
1074 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1079 if (res.count_planes) {
1080 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1082 if (!res.plane_id_ptr)
1086 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1089 if (counts.count_planes < res.count_planes) {
1090 drmFree(U642VOID(res.plane_id_ptr));
1094 if (!(r = drmMalloc(sizeof(*r))))
1097 r->count_planes = res.count_planes;
1098 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1099 res.count_planes, sizeof(uint32_t));
1100 if (res.count_planes && !r->planes) {
1107 drmFree(U642VOID(res.plane_id_ptr));
1112 drm_public void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1117 drmFree(ptr->planes);
1121 drm_public drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1123 uint32_t object_type)
1125 struct drm_mode_obj_get_properties properties;
1126 drmModeObjectPropertiesPtr ret = NULL;
1130 memclear(properties);
1131 properties.obj_id = object_id;
1132 properties.obj_type = object_type;
1134 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1137 count = properties.count_props;
1140 properties.props_ptr = VOID2U64(drmMalloc(count *
1142 if (!properties.props_ptr)
1144 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1146 if (!properties.prop_values_ptr)
1150 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1153 if (count < properties.count_props) {
1154 drmFree(U642VOID(properties.props_ptr));
1155 drmFree(U642VOID(properties.prop_values_ptr));
1158 count = properties.count_props;
1160 ret = drmMalloc(sizeof(*ret));
1164 ret->count_props = count;
1165 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1166 count, sizeof(uint32_t));
1167 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1168 count, sizeof(uint64_t));
1169 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1170 drmFree(ret->props);
1171 drmFree(ret->prop_values);
1177 drmFree(U642VOID(properties.props_ptr));
1178 drmFree(U642VOID(properties.prop_values_ptr));
1182 drm_public void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1186 drmFree(ptr->props);
1187 drmFree(ptr->prop_values);
1191 drm_public int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1192 uint32_t property_id, uint64_t value)
1194 struct drm_mode_obj_set_property prop;
1198 prop.prop_id = property_id;
1199 prop.obj_id = object_id;
1200 prop.obj_type = object_type;
1202 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1205 typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1207 struct _drmModeAtomicReqItem {
1209 uint32_t property_id;
1213 struct _drmModeAtomicReq {
1215 uint32_t size_items;
1216 drmModeAtomicReqItemPtr items;
1219 drm_public drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1221 drmModeAtomicReqPtr req;
1223 req = drmMalloc(sizeof *req);
1229 req->size_items = 0;
1234 drm_public drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1236 drmModeAtomicReqPtr new;
1241 new = drmMalloc(sizeof *new);
1245 new->cursor = old->cursor;
1246 new->size_items = old->size_items;
1248 if (old->size_items) {
1249 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1254 memcpy(new->items, old->items,
1255 old->cursor * sizeof(*new->items));
1263 drm_public int drmModeAtomicMerge(drmModeAtomicReqPtr base,
1264 drmModeAtomicReqPtr augment)
1269 if (!augment || augment->cursor == 0)
1272 if (base->cursor + augment->cursor >= base->size_items) {
1273 drmModeAtomicReqItemPtr new;
1274 int saved_size = base->size_items;
1276 base->size_items = base->cursor + augment->cursor;
1277 new = realloc(base->items,
1278 base->size_items * sizeof(*base->items));
1280 base->size_items = saved_size;
1286 memcpy(&base->items[base->cursor], augment->items,
1287 augment->cursor * sizeof(*augment->items));
1288 base->cursor += augment->cursor;
1293 drm_public int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1300 drm_public void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1303 req->cursor = cursor;
1306 drm_public int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1308 uint32_t property_id,
1314 if (object_id == 0 || property_id == 0)
1317 if (req->cursor >= req->size_items) {
1318 const uint32_t item_size_inc = getpagesize() / sizeof(*req->items);
1319 drmModeAtomicReqItemPtr new;
1321 req->size_items += item_size_inc;
1322 new = realloc(req->items, req->size_items * sizeof(*req->items));
1324 req->size_items -= item_size_inc;
1330 req->items[req->cursor].object_id = object_id;
1331 req->items[req->cursor].property_id = property_id;
1332 req->items[req->cursor].value = value;
1338 drm_public void drmModeAtomicFree(drmModeAtomicReqPtr req)
1344 drmFree(req->items);
1348 static int sort_req_list(const void *misc, const void *other)
1350 const drmModeAtomicReqItem *first = misc;
1351 const drmModeAtomicReqItem *second = other;
1353 if (first->object_id < second->object_id)
1355 else if (first->object_id > second->object_id)
1358 return second->property_id - first->property_id;
1361 drm_public int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req,
1362 uint32_t flags, void *user_data)
1364 drmModeAtomicReqPtr sorted;
1365 struct drm_mode_atomic atomic;
1366 uint32_t *objs_ptr = NULL;
1367 uint32_t *count_props_ptr = NULL;
1368 uint32_t *props_ptr = NULL;
1369 uint64_t *prop_values_ptr = NULL;
1370 uint32_t last_obj_id = 0;
1378 if (req->cursor == 0)
1381 sorted = drmModeAtomicDuplicate(req);
1387 /* Sort the list by object ID, then by property ID. */
1388 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1391 /* Now the list is sorted, eliminate duplicate property sets. */
1392 for (i = 0; i < sorted->cursor; i++) {
1393 if (sorted->items[i].object_id != last_obj_id) {
1394 atomic.count_objs++;
1395 last_obj_id = sorted->items[i].object_id;
1398 if (i == sorted->cursor - 1)
1401 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1402 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1405 memmove(&sorted->items[i], &sorted->items[i + 1],
1406 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1410 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1416 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1417 if (!count_props_ptr) {
1422 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1428 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1429 if (!prop_values_ptr) {
1434 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1435 if (sorted->items[i].object_id != last_obj_id) {
1437 objs_ptr[obj_idx] = sorted->items[i].object_id;
1438 last_obj_id = objs_ptr[obj_idx];
1441 count_props_ptr[obj_idx]++;
1442 props_ptr[i] = sorted->items[i].property_id;
1443 prop_values_ptr[i] = sorted->items[i].value;
1447 atomic.flags = flags;
1448 atomic.objs_ptr = VOID2U64(objs_ptr);
1449 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1450 atomic.props_ptr = VOID2U64(props_ptr);
1451 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1452 atomic.user_data = VOID2U64(user_data);
1454 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1458 drmFree(count_props_ptr);
1460 drmFree(prop_values_ptr);
1461 drmModeAtomicFree(sorted);
1467 drmModeCreatePropertyBlob(int fd, const void *data, size_t length,
1470 struct drm_mode_create_blob create;
1473 if (length >= 0xffffffff)
1478 create.length = length;
1479 create.data = (uintptr_t) data;
1483 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1487 *id = create.blob_id;
1492 drmModeDestroyPropertyBlob(int fd, uint32_t id)
1494 struct drm_mode_destroy_blob destroy;
1497 destroy.blob_id = id;
1498 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1502 drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags,
1503 uint32_t *lessee_id)
1505 struct drm_mode_create_lease create;
1509 create.object_ids = (uintptr_t) objects;
1510 create.object_count = num_objects;
1511 create.flags = flags;
1513 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1515 *lessee_id = create.lessee_id;
1521 drm_public drmModeLesseeListPtr
1522 drmModeListLessees(int fd)
1524 struct drm_mode_list_lessees list;
1526 drmModeLesseeListPtr ret;
1530 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1533 count = list.count_lessees;
1534 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1538 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1539 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1548 drm_public drmModeObjectListPtr
1549 drmModeGetLease(int fd)
1551 struct drm_mode_get_lease get;
1553 drmModeObjectListPtr ret;
1557 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1560 count = get.count_objects;
1561 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1565 get.objects_ptr = VOID2U64(&ret->objects[0]);
1566 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1576 drmModeRevokeLease(int fd, uint32_t lessee_id)
1578 struct drm_mode_revoke_lease revoke;
1583 revoke.lessee_id = lessee_id;
1585 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
1591 drm_public drmModeFB2Ptr
1592 drmModeGetFB2(int fd, uint32_t fb_id)
1594 struct drm_mode_fb_cmd2 get = {
1600 err = DRM_IOCTL(fd, DRM_IOCTL_MODE_GETFB2, &get);
1604 ret = drmMalloc(sizeof(drmModeFB2));
1609 ret->width = get.width;
1610 ret->height = get.height;
1611 ret->pixel_format = get.pixel_format;
1612 ret->flags = get.flags;
1613 ret->modifier = get.modifier[0];
1614 memcpy(ret->handles, get.handles, sizeof(uint32_t) * 4);
1615 memcpy(ret->pitches, get.pitches, sizeof(uint32_t) * 4);
1616 memcpy(ret->offsets, get.offsets, sizeof(uint32_t) * 4);
1621 drm_public void drmModeFreeFB2(drmModeFB2Ptr ptr)