blob: 207d7be91caa6e1cd7ca173c8e800dc25b971965 [file] [log] [blame]
Jesse Barnes731cd552008-12-17 10:09:49 -08001/*
2 * \file xf86drmMode.c
3 * Header for DRM modesetting interface.
4 *
5 * \author Jakob Bornecrantz <wallbraker@gmail.com>
6 *
7 * \par Acknowledgements:
8 * Feb 2007, Dave Airlie <airlied@linux.ie>
9 */
10
11/*
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>
15 *
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:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
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
32 * IN THE SOFTWARE.
33 *
34 */
35
36/*
Eric Engestromce975072016-04-03 19:48:12 +010037 * TODO the types we are after are defined in different headers on different
Jesse Barnes731cd552008-12-17 10:09:49 -080038 * platforms find which headers to include to get uint32_t
39 */
Emil Velikov5f762732015-06-29 17:32:21 +010040
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010041#include <limits.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080042#include <stdint.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010043#include <stdlib.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080044#include <sys/ioctl.h>
Julien Cristaufc8c3e22015-07-06 12:45:31 +010045#ifdef HAVE_SYS_SYSCTL_H
46#include <sys/sysctl.h>
47#endif
Jesse Barnes731cd552008-12-17 10:09:49 -080048#include <stdio.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010049#include <stdbool.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080050
Lucas De Marchi26f9ce52018-09-13 15:42:26 -070051#include "libdrm_macros.h"
Jesse Barnes731cd552008-12-17 10:09:49 -080052#include "xf86drmMode.h"
53#include "xf86drm.h"
54#include <drm.h>
55#include <string.h>
56#include <dirent.h>
Kristian Høgsbergb0b96632009-09-11 13:27:35 -040057#include <unistd.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080058#include <errno.h>
59
Daniel Vetter7e0460c2015-02-11 12:03:12 +010060#define memclear(s) memset(&s, 0, sizeof(s))
Eric Anholt734de702013-12-28 22:06:51 -080061
Jesse Barnes731cd552008-12-17 10:09:49 -080062#define U642VOID(x) ((void *)(unsigned long)(x))
63#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
64
Marcin Slusarz763b6182011-06-05 18:53:16 +020065static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
Chris Wilsonb8039182010-07-01 22:38:54 +010066{
67 int ret = drmIoctl(fd, cmd, arg);
68 return ret < 0 ? -errno : ret;
69}
70
Jesse Barnes731cd552008-12-17 10:09:49 -080071/*
72 * Util functions
73 */
74
Jan Vesely65041c42015-02-27 11:51:05 -050075static void* drmAllocCpy(char *array, int count, int entry_size)
Jesse Barnes731cd552008-12-17 10:09:49 -080076{
77 char *r;
78 int i;
79
80 if (!count || !array || !entry_size)
81 return 0;
82
83 if (!(r = drmMalloc(count*entry_size)))
84 return 0;
85
86 for (i = 0; i < count; i++)
87 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
88
89 return r;
90}
91
92/*
93 * A couple of free functions.
94 */
95
Lucas De Marchi26f9ce52018-09-13 15:42:26 -070096drm_public void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -080097{
98 if (!ptr)
99 return;
100
101 drmFree(ptr);
102}
103
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700104drm_public void drmModeFreeResources(drmModeResPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800105{
106 if (!ptr)
107 return;
108
Ville Syrjälädf497e92012-02-02 14:53:39 -0500109 drmFree(ptr->fbs);
110 drmFree(ptr->crtcs);
111 drmFree(ptr->connectors);
112 drmFree(ptr->encoders);
Jesse Barnes731cd552008-12-17 10:09:49 -0800113 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800114}
115
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700116drm_public void drmModeFreeFB(drmModeFBPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800117{
118 if (!ptr)
119 return;
120
121 /* we might add more frees later. */
122 drmFree(ptr);
123}
124
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700125drm_public void drmModeFreeCrtc(drmModeCrtcPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800126{
127 if (!ptr)
128 return;
129
130 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800131}
132
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700133drm_public void drmModeFreeConnector(drmModeConnectorPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800134{
135 if (!ptr)
136 return;
137
Keith Packard0a246542009-09-17 17:28:08 -0700138 drmFree(ptr->encoders);
139 drmFree(ptr->prop_values);
140 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800141 drmFree(ptr->modes);
142 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800143}
144
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700145drm_public void drmModeFreeEncoder(drmModeEncoderPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800146{
147 drmFree(ptr);
148}
149
150/*
151 * ModeSetting functions.
152 */
153
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700154drm_public drmModeResPtr drmModeGetResources(int fd)
Jesse Barnes731cd552008-12-17 10:09:49 -0800155{
Chris Wilsond1308f42010-01-06 15:19:25 +0000156 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800157 drmModeResPtr r = 0;
158
Chris Wilsond1308f42010-01-06 15:19:25 +0000159retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100160 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800161 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
162 return 0;
163
Chris Wilsond1308f42010-01-06 15:19:25 +0000164 counts = res;
165
Chris Wilson85fb3e52010-01-06 15:39:49 +0000166 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800167 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000168 if (!res.fb_id_ptr)
169 goto err_allocs;
170 }
171 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800172 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000173 if (!res.crtc_id_ptr)
174 goto err_allocs;
175 }
176 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800177 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000178 if (!res.connector_id_ptr)
179 goto err_allocs;
180 }
181 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800182 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000183 if (!res.encoder_id_ptr)
184 goto err_allocs;
185 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800186
Chris Wilsond1308f42010-01-06 15:19:25 +0000187 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800188 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000189
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.
193 */
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)
198 {
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));
203
204 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800205 }
206
207 /*
208 * return
209 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800210 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000211 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800212
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;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000221
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));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000226 if ((res.count_fbs && !r->fbs) ||
227 (res.count_crtcs && !r->crtcs) ||
228 (res.count_connectors && !r->connectors) ||
229 (res.count_encoders && !r->encoders))
230 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000231 drmFree(r->fbs);
232 drmFree(r->crtcs);
233 drmFree(r->connectors);
234 drmFree(r->encoders);
235 drmFree(r);
236 r = 0;
237 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800238
239err_allocs:
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));
244
245 return r;
246}
247
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700248
249drm_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,
251 uint32_t *buf_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800252{
253 struct drm_mode_fb_cmd f;
254 int ret;
255
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100256 memclear(f);
Jesse Barnes731cd552008-12-17 10:09:49 -0800257 f.width = width;
258 f.height = height;
259 f.pitch = pitch;
260 f.bpp = bpp;
261 f.depth = depth;
262 f.handle = bo_handle;
263
Chris Wilsonb8039182010-07-01 22:38:54 +0100264 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800265 return ret;
266
267 *buf_id = f.fb_id;
268 return 0;
269}
270
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700271drm_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)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700275{
276 struct drm_mode_fb_cmd2 f;
277 int ret;
278
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100279 memclear(f);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700280 f.width = width;
281 f.height = height;
282 f.pixel_format = pixel_format;
283 f.flags = flags;
Ville Syrjälä76b4a692012-02-02 14:53:43 -0500284 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]));
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700287 if (modifier)
288 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
Jesse Barnesac168bf2011-04-29 08:53:53 -0700289
290 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
291 return ret;
292
293 *buf_id = f.fb_id;
294 return 0;
295}
296
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700297drm_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)
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700301{
302 return drmModeAddFB2WithModifiers(fd, width, height,
303 pixel_format, bo_handles,
304 pitches, offsets, NULL,
305 buf_id, flags);
306}
307
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700308drm_public int drmModeRmFB(int fd, uint32_t bufferId)
Jesse Barnes731cd552008-12-17 10:09:49 -0800309{
Chris Wilsonb8039182010-07-01 22:38:54 +0100310 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800311}
312
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700313drm_public drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
Jesse Barnes731cd552008-12-17 10:09:49 -0800314{
315 struct drm_mode_fb_cmd info;
316 drmModeFBPtr r;
317
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100318 memclear(info);
Jesse Barnes731cd552008-12-17 10:09:49 -0800319 info.fb_id = buf;
320
321 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
322 return NULL;
323
324 if (!(r = drmMalloc(sizeof(*r))))
325 return NULL;
326
327 r->fb_id = info.fb_id;
328 r->width = info.width;
329 r->height = info.height;
330 r->pitch = info.pitch;
331 r->bpp = info.bpp;
332 r->handle = info.handle;
333 r->depth = info.depth;
334
335 return r;
336}
337
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700338drm_public int drmModeDirtyFB(int fd, uint32_t bufferId,
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100339 drmModeClipPtr clips, uint32_t num_clips)
340{
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100341 struct drm_mode_fb_dirty_cmd dirty;
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100342
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100343 memclear(dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100344 dirty.fb_id = bufferId;
345 dirty.clips_ptr = VOID2U64(clips);
346 dirty.num_clips = num_clips;
347
Chris Wilsonb8039182010-07-01 22:38:54 +0100348 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100349}
350
Jesse Barnes731cd552008-12-17 10:09:49 -0800351/*
352 * Crtc functions
353 */
354
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700355drm_public drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
Jesse Barnes731cd552008-12-17 10:09:49 -0800356{
357 struct drm_mode_crtc crtc;
358 drmModeCrtcPtr r;
359
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100360 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800361 crtc.crtc_id = crtcId;
362
363 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
364 return 0;
365
366 /*
367 * return
368 */
369
370 if (!(r = drmMalloc(sizeof(*r))))
371 return 0;
372
373 r->crtc_id = crtc.crtc_id;
374 r->x = crtc.x;
375 r->y = crtc.y;
376 r->mode_valid = crtc.mode_valid;
Rob Clarke81acf52012-10-14 16:55:32 -0500377 if (r->mode_valid) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800378 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
Rob Clarke81acf52012-10-14 16:55:32 -0500379 r->width = crtc.mode.hdisplay;
380 r->height = crtc.mode.vdisplay;
381 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800382 r->buffer_id = crtc.fb_id;
383 r->gamma_size = crtc.gamma_size;
384 return r;
385}
386
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700387drm_public int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200388 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100389 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800390{
391 struct drm_mode_crtc crtc;
392
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100393 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800394 crtc.x = x;
395 crtc.y = y;
396 crtc.crtc_id = crtcId;
397 crtc.fb_id = bufferId;
398 crtc.set_connectors_ptr = VOID2U64(connectors);
399 crtc.count_connectors = count;
400 if (mode) {
401 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
402 crtc.mode_valid = 1;
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100403 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800404
Chris Wilsonb8039182010-07-01 22:38:54 +0100405 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800406}
407
408/*
409 * Cursor manipulation
410 */
411
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700412drm_public int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle,
413 uint32_t width, uint32_t height)
Jesse Barnes731cd552008-12-17 10:09:49 -0800414{
415 struct drm_mode_cursor arg;
416
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100417 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800418 arg.flags = DRM_MODE_CURSOR_BO;
419 arg.crtc_id = crtcId;
420 arg.width = width;
421 arg.height = height;
422 arg.handle = bo_handle;
423
Chris Wilsonb8039182010-07-01 22:38:54 +0100424 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800425}
426
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700427drm_public int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle,
428 uint32_t width, uint32_t height, int32_t hot_x,
429 int32_t hot_y)
Dave Airlie2e0ab622013-07-02 09:21:06 +0100430{
431 struct drm_mode_cursor2 arg;
432
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100433 memclear(arg);
Dave Airlie2e0ab622013-07-02 09:21:06 +0100434 arg.flags = DRM_MODE_CURSOR_BO;
435 arg.crtc_id = crtcId;
436 arg.width = width;
437 arg.height = height;
438 arg.handle = bo_handle;
439 arg.hot_x = hot_x;
440 arg.hot_y = hot_y;
441
442 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
443}
444
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700445drm_public int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
Jesse Barnes731cd552008-12-17 10:09:49 -0800446{
447 struct drm_mode_cursor arg;
448
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100449 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800450 arg.flags = DRM_MODE_CURSOR_MOVE;
451 arg.crtc_id = crtcId;
452 arg.x = x;
453 arg.y = y;
454
Chris Wilsonb8039182010-07-01 22:38:54 +0100455 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800456}
457
458/*
459 * Encoder get
460 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700461drm_public drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800462{
463 struct drm_mode_get_encoder enc;
464 drmModeEncoderPtr r = NULL;
465
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100466 memclear(enc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800467 enc.encoder_id = encoder_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800468
469 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
470 return 0;
471
472 if (!(r = drmMalloc(sizeof(*r))))
473 return 0;
474
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;
480
481 return r;
482}
483
484/*
485 * Connector manipulation
486 */
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000487static drmModeConnectorPtr
488_drmModeGetConnector(int fd, uint32_t connector_id, int probe)
Jesse Barnes731cd552008-12-17 10:09:49 -0800489{
Peter Clifton04f90a42010-01-06 20:44:11 +0000490 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800491 drmModeConnectorPtr r = NULL;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200492 struct drm_mode_modeinfo stack_mode;
Jesse Barnes731cd552008-12-17 10:09:49 -0800493
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100494 memclear(conn);
Jesse Barnes731cd552008-12-17 10:09:49 -0800495 conn.connector_id = connector_id;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000496 if (!probe) {
497 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200498 conn.modes_ptr = VOID2U64(&stack_mode);
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000499 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800500
501 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
502 return 0;
503
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000504retry:
Peter Clifton04f90a42010-01-06 20:44:11 +0000505 counts = conn;
506
Jesse Barnes731cd552008-12-17 10:09:49 -0800507 if (conn.count_props) {
508 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000509 if (!conn.props_ptr)
510 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800511 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000512 if (!conn.prop_values_ptr)
513 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800514 }
515
Peter Clifton04f90a42010-01-06 20:44:11 +0000516 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800517 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000518 if (!conn.modes_ptr)
519 goto err_allocs;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000520 } else {
521 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200522 conn.modes_ptr = VOID2U64(&stack_mode);
Peter Clifton04f90a42010-01-06 20:44:11 +0000523 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800524
Peter Clifton04f90a42010-01-06 20:44:11 +0000525 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800526 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000527 if (!conn.encoders_ptr)
528 goto err_allocs;
529 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800530
531 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
532 goto err_allocs;
533
Peter Clifton04f90a42010-01-06 20:44:11 +0000534 /* 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.
537 */
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));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200543 if (U642VOID(conn.modes_ptr) != &stack_mode)
544 drmFree(U642VOID(conn.modes_ptr));
Peter Clifton04f90a42010-01-06 20:44:11 +0000545 drmFree(U642VOID(conn.encoders_ptr));
546
547 goto retry;
548 }
549
Jesse Barnes731cd552008-12-17 10:09:49 -0800550 if(!(r = drmMalloc(sizeof(*r)))) {
551 goto err_allocs;
552 }
553
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;
Dave Airlie412d3702009-04-22 20:25:40 +1000559 /* convert subpixel from kernel to userspace */
560 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800561 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800562 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;
570
Peter Clifton04f90a42010-01-06 20:44:11 +0000571 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)) {
575 drmFree(r->props);
576 drmFree(r->prop_values);
577 drmFree(r->modes);
578 drmFree(r->encoders);
579 drmFree(r);
580 r = 0;
581 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800582
583err_allocs:
584 drmFree(U642VOID(conn.prop_values_ptr));
585 drmFree(U642VOID(conn.props_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200586 if (U642VOID(conn.modes_ptr) != &stack_mode)
587 drmFree(U642VOID(conn.modes_ptr));
Jesse Barnes731cd552008-12-17 10:09:49 -0800588 drmFree(U642VOID(conn.encoders_ptr));
589
590 return r;
591}
592
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700593drm_public drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000594{
595 return _drmModeGetConnector(fd, connector_id, 1);
596}
597
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700598drm_public drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000599{
600 return _drmModeGetConnector(fd, connector_id, 0);
601}
602
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700603drm_public int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800604{
605 struct drm_mode_mode_cmd res;
606
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100607 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800608 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
609 res.connector_id = connector_id;
610
Chris Wilsonb8039182010-07-01 22:38:54 +0100611 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800612}
613
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700614drm_public int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800615{
616 struct drm_mode_mode_cmd res;
617
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100618 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800619 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
620 res.connector_id = connector_id;
621
Chris Wilsonb8039182010-07-01 22:38:54 +0100622 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800623}
624
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700625drm_public drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800626{
627 struct drm_mode_get_property prop;
628 drmModePropertyPtr r;
629
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100630 memclear(prop);
Jesse Barnes731cd552008-12-17 10:09:49 -0800631 prop.prop_id = property_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800632
633 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
634 return 0;
635
636 if (prop.count_values)
637 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
638
Rob Clark7b228e92012-06-05 12:28:22 -0500639 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800640 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
641
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)));
645 }
646
647 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
648 r = NULL;
649 goto err_allocs;
650 }
651
652 if (!(r = drmMalloc(sizeof(*r))))
Seung-Woo Kimb39377d2019-04-29 18:10:52 +0900653 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800654
655 r->prop_id = prop.prop_id;
656 r->count_values = prop.count_values;
657
658 r->flags = prop.flags;
659 if (prop.count_values)
660 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
Rob Clark7b228e92012-06-05 12:28:22 -0500661 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800662 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;
668 }
669 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
670 r->name[DRM_PROP_NAME_LEN-1] = 0;
671
672err_allocs:
673 drmFree(U642VOID(prop.values_ptr));
674 drmFree(U642VOID(prop.enum_blob_ptr));
675
676 return r;
677}
678
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700679drm_public void drmModeFreeProperty(drmModePropertyPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800680{
681 if (!ptr)
682 return;
683
684 drmFree(ptr->values);
685 drmFree(ptr->enums);
686 drmFree(ptr);
687}
688
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700689drm_public drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd,
690 uint32_t blob_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800691{
692 struct drm_mode_get_blob blob;
693 drmModePropertyBlobPtr r;
694
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100695 memclear(blob);
Jesse Barnes731cd552008-12-17 10:09:49 -0800696 blob.blob_id = blob_id;
697
698 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
699 return NULL;
700
701 if (blob.length)
702 blob.data = VOID2U64(drmMalloc(blob.length));
703
704 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
705 r = NULL;
706 goto err_allocs;
707 }
708
709 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100710 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800711
712 r->id = blob.blob_id;
713 r->length = blob.length;
714 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
715
716err_allocs:
717 drmFree(U642VOID(blob.data));
718 return r;
719}
720
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700721drm_public void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800722{
723 if (!ptr)
724 return;
725
726 drmFree(ptr->data);
727 drmFree(ptr);
728}
729
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700730drm_public int drmModeConnectorSetProperty(int fd, uint32_t connector_id,
731 uint32_t property_id,
732 uint64_t value)
Jesse Barnes731cd552008-12-17 10:09:49 -0800733{
734 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800735
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100736 memclear(osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800737 osp.connector_id = connector_id;
738 osp.prop_id = property_id;
739 osp.value = value;
740
Chris Wilsonb8039182010-07-01 22:38:54 +0100741 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800742}
743
744/*
745 * checks if a modesetting capable driver has attached to the pci id
746 * returns 0 if modesetting supported.
747 * -EINVAL or invalid bus id
748 * -ENOSYS if no modesetting support
749*/
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700750drm_public int drmCheckModesettingSupported(const char *busid)
Jesse Barnes731cd552008-12-17 10:09:49 -0800751{
Robert Millancbb31f22014-01-23 14:46:05 +0000752#if defined (__linux__)
Jesse Barnes731cd552008-12-17 10:09:49 -0800753 char pci_dev_dir[1024];
754 int domain, bus, dev, func;
755 DIR *sysdir;
756 struct dirent *dent;
757 int found = 0, ret;
758
759 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
760 if (ret != 4)
761 return -EINVAL;
762
763 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
764 domain, bus, dev, func);
765
766 sysdir = opendir(pci_dev_dir);
767 if (sysdir) {
768 dent = readdir(sysdir);
769 while (dent) {
770 if (!strncmp(dent->d_name, "controlD", 8)) {
771 found = 1;
772 break;
773 }
774
775 dent = readdir(sysdir);
776 }
777 closedir(sysdir);
778 if (found)
779 return 0;
780 }
781
782 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
783 domain, bus, dev, func);
784
785 sysdir = opendir(pci_dev_dir);
786 if (!sysdir)
787 return -EINVAL;
788
789 dent = readdir(sysdir);
790 while (dent) {
791 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
792 found = 1;
793 break;
794 }
795
796 dent = readdir(sysdir);
797 }
798
799 closedir(sysdir);
800 if (found)
801 return 0;
Robert Millancbb31f22014-01-23 14:46:05 +0000802#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
803 char kbusid[1024], sbusid[1024];
804 char oid[128];
805 int domain, bus, dev, func;
806 int i, modesetting, ret;
807 size_t len;
808
809 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
810 &func);
811 if (ret != 4)
812 return -EINVAL;
813 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
814 dev, func);
815
816 /* How many GPUs do we expect in the machine ? */
817 for (i = 0; i < 16; i++) {
818 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
819 len = sizeof(sbusid);
820 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
821 if (ret == -1) {
822 if (errno == ENOENT)
823 continue;
824 return -EINVAL;
825 }
826 if (strcmp(sbusid, kbusid) != 0)
827 continue;
828 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
829 len = sizeof(modesetting);
830 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
831 if (ret == -1 || len != sizeof(modesetting))
832 return -EINVAL;
833 return (modesetting ? 0 : -ENOSYS);
834 }
François Tigeotedbb4e52014-07-26 13:39:58 +0200835#elif defined(__DragonFly__)
836 return 0;
Eric Engestrom00aa3742018-03-22 17:08:37 +0000837#elif defined(__OpenBSD__)
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000838 int fd;
839 struct drm_mode_card_res res;
840 drmModeResPtr r = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800841
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000842 if ((fd = drmOpen(NULL, busid)) < 0)
843 return -EINVAL;
844
845 memset(&res, 0, sizeof(struct drm_mode_card_res));
846
847 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
848 drmClose(fd);
849 return -errno;
850 }
851
852 drmClose(fd);
853 return 0;
854#endif
855 return -ENOSYS;
Jesse Barnes731cd552008-12-17 10:09:49 -0800856}
857
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700858drm_public int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
859 uint16_t *red, uint16_t *green,
860 uint16_t *blue)
Jesse Barnes731cd552008-12-17 10:09:49 -0800861{
Jesse Barnes731cd552008-12-17 10:09:49 -0800862 struct drm_mode_crtc_lut l;
863
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100864 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800865 l.crtc_id = crtc_id;
866 l.gamma_size = size;
867 l.red = VOID2U64(red);
868 l.green = VOID2U64(green);
869 l.blue = VOID2U64(blue);
870
Chris Wilsonb8039182010-07-01 22:38:54 +0100871 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800872}
873
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700874drm_public int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
875 uint16_t *red, uint16_t *green,
876 uint16_t *blue)
Jesse Barnes731cd552008-12-17 10:09:49 -0800877{
Jesse Barnes731cd552008-12-17 10:09:49 -0800878 struct drm_mode_crtc_lut l;
879
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100880 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800881 l.crtc_id = crtc_id;
882 l.gamma_size = size;
883 l.red = VOID2U64(red);
884 l.green = VOID2U64(green);
885 l.blue = VOID2U64(blue);
886
Chris Wilsonb8039182010-07-01 22:38:54 +0100887 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800888}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400889
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700890drm_public int drmHandleEvent(int fd, drmEventContextPtr evctx)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400891{
892 char buffer[1024];
893 int len, i;
894 struct drm_event *e;
895 struct drm_event_vblank *vblank;
Keith Packardd4331dd2017-07-01 00:43:15 -0700896 struct drm_event_crtc_sequence *seq;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300897 void *user_data;
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900898
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400899 /* The DRM read semantics guarantees that we always get only
900 * complete events. */
901
902 len = read(fd, buffer, sizeof buffer);
903 if (len == 0)
904 return 0;
Jan Veselyde8532d2014-11-30 12:53:18 -0500905 if (len < (int)sizeof *e)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400906 return -1;
907
908 i = 0;
909 while (i < len) {
Thierry Redingecc2a092015-04-13 11:36:59 +0200910 e = (struct drm_event *)(buffer + i);
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400911 switch (e->type) {
912 case DRM_EVENT_VBLANK:
913 if (evctx->version < 1 ||
914 evctx->vblank_handler == NULL)
915 break;
916 vblank = (struct drm_event_vblank *) e;
917 evctx->vblank_handler(fd,
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900918 vblank->sequence,
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400919 vblank->tv_sec,
920 vblank->tv_usec,
921 U642VOID (vblank->user_data));
922 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500923 case DRM_EVENT_FLIP_COMPLETE:
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500924 vblank = (struct drm_event_vblank *) e;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300925 user_data = U642VOID (vblank->user_data);
926
927 if (evctx->version >= 3 && evctx->page_flip_handler2)
928 evctx->page_flip_handler2(fd,
929 vblank->sequence,
930 vblank->tv_sec,
931 vblank->tv_usec,
932 vblank->crtc_id,
933 user_data);
934 else if (evctx->version >= 2 && evctx->page_flip_handler)
935 evctx->page_flip_handler(fd,
936 vblank->sequence,
937 vblank->tv_sec,
938 vblank->tv_usec,
939 user_data);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500940 break;
Keith Packardd4331dd2017-07-01 00:43:15 -0700941 case DRM_EVENT_CRTC_SEQUENCE:
942 seq = (struct drm_event_crtc_sequence *) e;
943 if (evctx->version >= 4 && evctx->sequence_handler)
944 evctx->sequence_handler(fd,
945 seq->sequence,
946 seq->time_ns,
947 seq->user_data);
948 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400949 default:
950 break;
951 }
952 i += e->length;
953 }
954
955 return 0;
956}
957
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700958drm_public int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500959 uint32_t flags, void *user_data)
960{
961 struct drm_mode_crtc_page_flip flip;
962
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100963 memclear(flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500964 flip.fb_id = fb_id;
965 flip.crtc_id = crtc_id;
966 flip.user_data = VOID2U64(user_data);
967 flip.flags = flags;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500968
Chris Wilsonb8039182010-07-01 22:38:54 +0100969 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500970}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700971
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700972drm_public int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
Michel Dänzer7dd28472016-06-29 18:07:25 +0900973 uint32_t flags, void *user_data,
974 uint32_t target_vblank)
975{
976 struct drm_mode_crtc_page_flip_target flip_target;
977
978 memclear(flip_target);
979 flip_target.fb_id = fb_id;
980 flip_target.crtc_id = crtc_id;
981 flip_target.user_data = VOID2U64(user_data);
982 flip_target.flags = flags;
983 flip_target.sequence = target_vblank;
984
985 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
986}
987
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700988drm_public int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700989 uint32_t fb_id, uint32_t flags,
Daniel Kurtz828c3e82014-05-01 19:56:43 +0800990 int32_t crtc_x, int32_t crtc_y,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700991 uint32_t crtc_w, uint32_t crtc_h,
992 uint32_t src_x, uint32_t src_y,
993 uint32_t src_w, uint32_t src_h)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700994{
995 struct drm_mode_set_plane s;
996
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100997 memclear(s);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700998 s.plane_id = plane_id;
999 s.crtc_id = crtc_id;
1000 s.fb_id = fb_id;
1001 s.flags = flags;
1002 s.crtc_x = crtc_x;
1003 s.crtc_y = crtc_y;
1004 s.crtc_w = crtc_w;
1005 s.crtc_h = crtc_h;
1006 s.src_x = src_x;
1007 s.src_y = src_y;
1008 s.src_w = src_w;
1009 s.src_h = src_h;
1010
1011 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1012}
1013
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001014drm_public drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001015{
1016 struct drm_mode_get_plane ovr, counts;
1017 drmModePlanePtr r = 0;
1018
1019retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001020 memclear(ovr);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001021 ovr.plane_id = plane_id;
1022 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1023 return 0;
1024
1025 counts = ovr;
1026
1027 if (ovr.count_format_types) {
1028 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1029 sizeof(uint32_t)));
1030 if (!ovr.format_type_ptr)
1031 goto err_allocs;
1032 }
1033
1034 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1035 goto err_allocs;
1036
1037 if (counts.count_format_types < ovr.count_format_types) {
1038 drmFree(U642VOID(ovr.format_type_ptr));
1039 goto retry;
1040 }
1041
1042 if (!(r = drmMalloc(sizeof(*r))))
1043 goto err_allocs;
1044
1045 r->count_formats = ovr.count_format_types;
1046 r->plane_id = ovr.plane_id;
1047 r->crtc_id = ovr.crtc_id;
1048 r->fb_id = ovr.fb_id;
1049 r->possible_crtcs = ovr.possible_crtcs;
1050 r->gamma_size = ovr.gamma_size;
1051 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1052 ovr.count_format_types, sizeof(uint32_t));
1053 if (ovr.count_format_types && !r->formats) {
1054 drmFree(r->formats);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001055 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001056 r = 0;
1057 }
1058
1059err_allocs:
1060 drmFree(U642VOID(ovr.format_type_ptr));
1061
1062 return r;
1063}
1064
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001065drm_public void drmModeFreePlane(drmModePlanePtr ptr)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001066{
1067 if (!ptr)
1068 return;
1069
1070 drmFree(ptr->formats);
1071 drmFree(ptr);
1072}
1073
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001074drm_public drmModePlaneResPtr drmModeGetPlaneResources(int fd)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001075{
1076 struct drm_mode_get_plane_res res, counts;
1077 drmModePlaneResPtr r = 0;
1078
1079retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001080 memclear(res);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001081 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1082 return 0;
1083
1084 counts = res;
1085
1086 if (res.count_planes) {
1087 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1088 sizeof(uint32_t)));
1089 if (!res.plane_id_ptr)
1090 goto err_allocs;
1091 }
1092
1093 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1094 goto err_allocs;
1095
1096 if (counts.count_planes < res.count_planes) {
1097 drmFree(U642VOID(res.plane_id_ptr));
1098 goto retry;
1099 }
1100
1101 if (!(r = drmMalloc(sizeof(*r))))
1102 goto err_allocs;
1103
1104 r->count_planes = res.count_planes;
1105 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1106 res.count_planes, sizeof(uint32_t));
1107 if (res.count_planes && !r->planes) {
1108 drmFree(r->planes);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001109 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001110 r = 0;
1111 }
1112
1113err_allocs:
1114 drmFree(U642VOID(res.plane_id_ptr));
1115
1116 return r;
1117}
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001118
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001119drm_public void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001120{
1121 if (!ptr)
1122 return;
1123
1124 drmFree(ptr->planes);
1125 drmFree(ptr);
1126}
Paulo Zanoni8c757032012-05-15 18:38:28 -03001127
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001128drm_public drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
Paulo Zanoni8c757032012-05-15 18:38:28 -03001129 uint32_t object_id,
1130 uint32_t object_type)
1131{
1132 struct drm_mode_obj_get_properties properties;
1133 drmModeObjectPropertiesPtr ret = NULL;
1134 uint32_t count;
1135
1136retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001137 memclear(properties);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001138 properties.obj_id = object_id;
1139 properties.obj_type = object_type;
1140
1141 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1142 return 0;
1143
1144 count = properties.count_props;
1145
1146 if (count) {
1147 properties.props_ptr = VOID2U64(drmMalloc(count *
1148 sizeof(uint32_t)));
1149 if (!properties.props_ptr)
1150 goto err_allocs;
1151 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1152 sizeof(uint64_t)));
1153 if (!properties.prop_values_ptr)
1154 goto err_allocs;
1155 }
1156
1157 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1158 goto err_allocs;
1159
1160 if (count < properties.count_props) {
1161 drmFree(U642VOID(properties.props_ptr));
1162 drmFree(U642VOID(properties.prop_values_ptr));
1163 goto retry;
1164 }
1165 count = properties.count_props;
1166
1167 ret = drmMalloc(sizeof(*ret));
1168 if (!ret)
1169 goto err_allocs;
1170
1171 ret->count_props = count;
1172 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1173 count, sizeof(uint32_t));
1174 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1175 count, sizeof(uint64_t));
1176 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1177 drmFree(ret->props);
1178 drmFree(ret->prop_values);
1179 drmFree(ret);
1180 ret = NULL;
1181 }
1182
1183err_allocs:
1184 drmFree(U642VOID(properties.props_ptr));
1185 drmFree(U642VOID(properties.prop_values_ptr));
1186 return ret;
1187}
1188
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001189drm_public void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
Paulo Zanoni8c757032012-05-15 18:38:28 -03001190{
1191 if (!ptr)
1192 return;
1193 drmFree(ptr->props);
1194 drmFree(ptr->prop_values);
1195 drmFree(ptr);
1196}
1197
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001198drm_public int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
Paulo Zanoni8c757032012-05-15 18:38:28 -03001199 uint32_t property_id, uint64_t value)
1200{
1201 struct drm_mode_obj_set_property prop;
1202
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001203 memclear(prop);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001204 prop.value = value;
1205 prop.prop_id = property_id;
1206 prop.obj_id = object_id;
1207 prop.obj_type = object_type;
1208
1209 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1210}
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001211
1212typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1213
1214struct _drmModeAtomicReqItem {
1215 uint32_t object_id;
1216 uint32_t property_id;
1217 uint64_t value;
1218};
1219
1220struct _drmModeAtomicReq {
1221 uint32_t cursor;
1222 uint32_t size_items;
1223 drmModeAtomicReqItemPtr items;
1224};
1225
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001226drm_public drmModeAtomicReqPtr drmModeAtomicAlloc(void)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001227{
1228 drmModeAtomicReqPtr req;
1229
1230 req = drmMalloc(sizeof *req);
1231 if (!req)
1232 return NULL;
1233
1234 req->items = NULL;
1235 req->cursor = 0;
1236 req->size_items = 0;
1237
1238 return req;
1239}
1240
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001241drm_public drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001242{
1243 drmModeAtomicReqPtr new;
1244
Emil Velikoved3c6652015-09-05 17:20:53 +01001245 if (!old)
1246 return NULL;
1247
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001248 new = drmMalloc(sizeof *new);
1249 if (!new)
1250 return NULL;
1251
1252 new->cursor = old->cursor;
1253 new->size_items = old->size_items;
1254
1255 if (old->size_items) {
1256 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1257 if (!new->items) {
1258 free(new);
1259 return NULL;
1260 }
1261 memcpy(new->items, old->items,
Adrian Salido763f6462019-04-24 10:08:40 -07001262 old->cursor * sizeof(*new->items));
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001263 } else {
1264 new->items = NULL;
1265 }
1266
1267 return new;
1268}
1269
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001270drm_public int drmModeAtomicMerge(drmModeAtomicReqPtr base,
1271 drmModeAtomicReqPtr augment)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001272{
Emil Velikoved3c6652015-09-05 17:20:53 +01001273 if (!base)
1274 return -EINVAL;
1275
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001276 if (!augment || augment->cursor == 0)
1277 return 0;
1278
1279 if (base->cursor + augment->cursor >= base->size_items) {
1280 drmModeAtomicReqItemPtr new;
1281 int saved_size = base->size_items;
1282
1283 base->size_items = base->cursor + augment->cursor;
1284 new = realloc(base->items,
1285 base->size_items * sizeof(*base->items));
1286 if (!new) {
1287 base->size_items = saved_size;
1288 return -ENOMEM;
1289 }
1290 base->items = new;
1291 }
1292
1293 memcpy(&base->items[base->cursor], augment->items,
1294 augment->cursor * sizeof(*augment->items));
1295 base->cursor += augment->cursor;
1296
1297 return 0;
1298}
1299
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001300drm_public int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001301{
Emil Velikoved3c6652015-09-05 17:20:53 +01001302 if (!req)
1303 return -EINVAL;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001304 return req->cursor;
1305}
1306
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001307drm_public void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001308{
Emil Velikoved3c6652015-09-05 17:20:53 +01001309 if (req)
1310 req->cursor = cursor;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001311}
1312
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001313drm_public int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1314 uint32_t object_id,
1315 uint32_t property_id,
1316 uint64_t value)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001317{
Emil Velikoved3c6652015-09-05 17:20:53 +01001318 if (!req)
1319 return -EINVAL;
1320
Daniel Stone45eee3f2018-03-07 12:41:12 +00001321 if (object_id == 0 || property_id == 0)
1322 return -EINVAL;
1323
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001324 if (req->cursor >= req->size_items) {
Adrian Salido763f6462019-04-24 10:08:40 -07001325 const uint32_t item_size_inc = getpagesize() / sizeof(*req->items);
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001326 drmModeAtomicReqItemPtr new;
1327
Adrian Salido763f6462019-04-24 10:08:40 -07001328 req->size_items += item_size_inc;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001329 new = realloc(req->items, req->size_items * sizeof(*req->items));
1330 if (!new) {
Adrian Salido763f6462019-04-24 10:08:40 -07001331 req->size_items -= item_size_inc;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001332 return -ENOMEM;
1333 }
1334 req->items = new;
1335 }
1336
1337 req->items[req->cursor].object_id = object_id;
1338 req->items[req->cursor].property_id = property_id;
1339 req->items[req->cursor].value = value;
1340 req->cursor++;
1341
1342 return req->cursor;
1343}
1344
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001345drm_public void drmModeAtomicFree(drmModeAtomicReqPtr req)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001346{
1347 if (!req)
1348 return;
1349
1350 if (req->items)
1351 drmFree(req->items);
1352 drmFree(req);
1353}
1354
1355static int sort_req_list(const void *misc, const void *other)
1356{
1357 const drmModeAtomicReqItem *first = misc;
1358 const drmModeAtomicReqItem *second = other;
1359
1360 if (first->object_id < second->object_id)
1361 return -1;
1362 else if (first->object_id > second->object_id)
1363 return 1;
1364 else
1365 return second->property_id - first->property_id;
1366}
1367
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001368drm_public int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req,
1369 uint32_t flags, void *user_data)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001370{
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001371 drmModeAtomicReqPtr sorted;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001372 struct drm_mode_atomic atomic;
1373 uint32_t *objs_ptr = NULL;
1374 uint32_t *count_props_ptr = NULL;
1375 uint32_t *props_ptr = NULL;
1376 uint64_t *prop_values_ptr = NULL;
1377 uint32_t last_obj_id = 0;
1378 uint32_t i;
1379 int obj_idx = -1;
1380 int ret = -1;
1381
Emil Velikoved3c6652015-09-05 17:20:53 +01001382 if (!req)
1383 return -EINVAL;
1384
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001385 if (req->cursor == 0)
1386 return 0;
1387
1388 sorted = drmModeAtomicDuplicate(req);
1389 if (sorted == NULL)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001390 return -ENOMEM;
1391
1392 memclear(atomic);
1393
1394 /* Sort the list by object ID, then by property ID. */
1395 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1396 sort_req_list);
1397
1398 /* Now the list is sorted, eliminate duplicate property sets. */
1399 for (i = 0; i < sorted->cursor; i++) {
1400 if (sorted->items[i].object_id != last_obj_id) {
1401 atomic.count_objs++;
1402 last_obj_id = sorted->items[i].object_id;
1403 }
1404
1405 if (i == sorted->cursor - 1)
1406 continue;
1407
1408 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1409 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1410 continue;
1411
1412 memmove(&sorted->items[i], &sorted->items[i + 1],
1413 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1414 sorted->cursor--;
1415 }
1416
1417 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1418 if (!objs_ptr) {
1419 errno = ENOMEM;
1420 goto out;
1421 }
1422
1423 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1424 if (!count_props_ptr) {
1425 errno = ENOMEM;
1426 goto out;
1427 }
1428
1429 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1430 if (!props_ptr) {
1431 errno = ENOMEM;
1432 goto out;
1433 }
1434
1435 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1436 if (!prop_values_ptr) {
1437 errno = ENOMEM;
1438 goto out;
1439 }
1440
1441 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1442 if (sorted->items[i].object_id != last_obj_id) {
1443 obj_idx++;
1444 objs_ptr[obj_idx] = sorted->items[i].object_id;
1445 last_obj_id = objs_ptr[obj_idx];
1446 }
1447
1448 count_props_ptr[obj_idx]++;
1449 props_ptr[i] = sorted->items[i].property_id;
1450 prop_values_ptr[i] = sorted->items[i].value;
1451
1452 }
1453
1454 atomic.flags = flags;
1455 atomic.objs_ptr = VOID2U64(objs_ptr);
1456 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1457 atomic.props_ptr = VOID2U64(props_ptr);
1458 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1459 atomic.user_data = VOID2U64(user_data);
1460
1461 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1462
1463out:
1464 drmFree(objs_ptr);
1465 drmFree(count_props_ptr);
1466 drmFree(props_ptr);
1467 drmFree(prop_values_ptr);
1468 drmModeAtomicFree(sorted);
1469
1470 return ret;
1471}
Daniel Stone32471b22015-06-22 17:26:03 +01001472
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001473drm_public int
1474drmModeCreatePropertyBlob(int fd, const void *data, size_t length,
1475 uint32_t *id)
Daniel Stone32471b22015-06-22 17:26:03 +01001476{
1477 struct drm_mode_create_blob create;
1478 int ret;
1479
1480 if (length >= 0xffffffff)
1481 return -ERANGE;
1482
1483 memclear(create);
1484
1485 create.length = length;
1486 create.data = (uintptr_t) data;
1487 create.blob_id = 0;
1488 *id = 0;
1489
1490 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1491 if (ret != 0)
1492 return ret;
1493
1494 *id = create.blob_id;
1495 return 0;
1496}
1497
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001498drm_public int
Daniel Stone32471b22015-06-22 17:26:03 +01001499drmModeDestroyPropertyBlob(int fd, uint32_t id)
1500{
1501 struct drm_mode_destroy_blob destroy;
1502
1503 memclear(destroy);
1504 destroy.blob_id = id;
1505 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1506}
Keith Packardc4171532017-03-16 18:11:05 -07001507
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001508drm_public int
1509drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags,
1510 uint32_t *lessee_id)
Keith Packardc4171532017-03-16 18:11:05 -07001511{
1512 struct drm_mode_create_lease create;
1513 int ret;
1514
1515 memclear(create);
1516 create.object_ids = (uintptr_t) objects;
1517 create.object_count = num_objects;
1518 create.flags = flags;
1519
1520 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1521 if (ret == 0) {
1522 *lessee_id = create.lessee_id;
1523 return create.fd;
1524 }
1525 return -errno;
1526}
1527
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001528drm_public drmModeLesseeListPtr
Keith Packardc4171532017-03-16 18:11:05 -07001529drmModeListLessees(int fd)
1530{
1531 struct drm_mode_list_lessees list;
1532 uint32_t count;
1533 drmModeLesseeListPtr ret;
1534
1535 memclear(list);
1536
1537 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1538 return NULL;
1539
1540 count = list.count_lessees;
1541 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1542 if (!ret)
1543 return NULL;
1544
1545 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1546 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1547 drmFree(ret);
1548 return NULL;
1549 }
1550
1551 ret->count = count;
1552 return ret;
1553}
1554
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001555drm_public drmModeObjectListPtr
Keith Packardc4171532017-03-16 18:11:05 -07001556drmModeGetLease(int fd)
1557{
1558 struct drm_mode_get_lease get;
1559 uint32_t count;
1560 drmModeObjectListPtr ret;
1561
1562 memclear(get);
1563
1564 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1565 return NULL;
1566
1567 count = get.count_objects;
1568 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1569 if (!ret)
1570 return NULL;
1571
1572 get.objects_ptr = VOID2U64(&ret->objects[0]);
1573 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1574 drmFree(ret);
1575 return NULL;
1576 }
1577
1578 ret->count = count;
1579 return ret;
1580}
1581
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001582drm_public int
Keith Packardc4171532017-03-16 18:11:05 -07001583drmModeRevokeLease(int fd, uint32_t lessee_id)
1584{
1585 struct drm_mode_revoke_lease revoke;
1586 int ret;
1587
1588 memclear(revoke);
1589
1590 revoke.lessee_id = lessee_id;
1591
1592 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
1593 if (ret == 0)
1594 return 0;
1595 return -errno;
1596}