blob: 3d6bdfd6c195305b11c4a7fdc5098f956aa11538 [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
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010036#include <limits.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080037#include <stdint.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010038#include <stdlib.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080039#include <sys/ioctl.h>
Eric Engestrom074947e2018-11-07 15:00:24 +000040#if HAVE_SYS_SYSCTL_H
Julien Cristaufc8c3e22015-07-06 12:45:31 +010041#include <sys/sysctl.h>
42#endif
Jesse Barnes731cd552008-12-17 10:09:49 -080043#include <stdio.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010044#include <stdbool.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080045
Lucas De Marchi26f9ce52018-09-13 15:42:26 -070046#include "libdrm_macros.h"
Jesse Barnes731cd552008-12-17 10:09:49 -080047#include "xf86drmMode.h"
48#include "xf86drm.h"
49#include <drm.h>
50#include <string.h>
51#include <dirent.h>
Kristian Høgsbergb0b96632009-09-11 13:27:35 -040052#include <unistd.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080053#include <errno.h>
54
Daniel Vetter7e0460c2015-02-11 12:03:12 +010055#define memclear(s) memset(&s, 0, sizeof(s))
Eric Anholt734de702013-12-28 22:06:51 -080056
Jesse Barnes731cd552008-12-17 10:09:49 -080057#define U642VOID(x) ((void *)(unsigned long)(x))
58#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
59
Marcin Slusarz763b6182011-06-05 18:53:16 +020060static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
Chris Wilsonb8039182010-07-01 22:38:54 +010061{
62 int ret = drmIoctl(fd, cmd, arg);
63 return ret < 0 ? -errno : ret;
64}
65
Jesse Barnes731cd552008-12-17 10:09:49 -080066/*
67 * Util functions
68 */
69
Jan Vesely65041c42015-02-27 11:51:05 -050070static void* drmAllocCpy(char *array, int count, int entry_size)
Jesse Barnes731cd552008-12-17 10:09:49 -080071{
72 char *r;
73 int i;
74
75 if (!count || !array || !entry_size)
76 return 0;
77
78 if (!(r = drmMalloc(count*entry_size)))
79 return 0;
80
81 for (i = 0; i < count; i++)
82 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
83
84 return r;
85}
86
87/*
88 * A couple of free functions.
89 */
90
Lucas De Marchi26f9ce52018-09-13 15:42:26 -070091drm_public void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -080092{
93 if (!ptr)
94 return;
95
96 drmFree(ptr);
97}
98
Lucas De Marchi26f9ce52018-09-13 15:42:26 -070099drm_public void drmModeFreeResources(drmModeResPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800100{
101 if (!ptr)
102 return;
103
Ville Syrjälädf497e92012-02-02 14:53:39 -0500104 drmFree(ptr->fbs);
105 drmFree(ptr->crtcs);
106 drmFree(ptr->connectors);
107 drmFree(ptr->encoders);
Jesse Barnes731cd552008-12-17 10:09:49 -0800108 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800109}
110
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700111drm_public void drmModeFreeFB(drmModeFBPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800112{
113 if (!ptr)
114 return;
115
116 /* we might add more frees later. */
117 drmFree(ptr);
118}
119
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700120drm_public void drmModeFreeCrtc(drmModeCrtcPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800121{
122 if (!ptr)
123 return;
124
125 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800126}
127
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700128drm_public void drmModeFreeConnector(drmModeConnectorPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800129{
130 if (!ptr)
131 return;
132
Keith Packard0a246542009-09-17 17:28:08 -0700133 drmFree(ptr->encoders);
134 drmFree(ptr->prop_values);
135 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800136 drmFree(ptr->modes);
137 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800138}
139
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700140drm_public void drmModeFreeEncoder(drmModeEncoderPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800141{
142 drmFree(ptr);
143}
144
145/*
146 * ModeSetting functions.
147 */
148
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700149drm_public drmModeResPtr drmModeGetResources(int fd)
Jesse Barnes731cd552008-12-17 10:09:49 -0800150{
Chris Wilsond1308f42010-01-06 15:19:25 +0000151 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800152 drmModeResPtr r = 0;
153
Chris Wilsond1308f42010-01-06 15:19:25 +0000154retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100155 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800156 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
157 return 0;
158
Chris Wilsond1308f42010-01-06 15:19:25 +0000159 counts = res;
160
Chris Wilson85fb3e52010-01-06 15:39:49 +0000161 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800162 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000163 if (!res.fb_id_ptr)
164 goto err_allocs;
165 }
166 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800167 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000168 if (!res.crtc_id_ptr)
169 goto err_allocs;
170 }
171 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800172 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000173 if (!res.connector_id_ptr)
174 goto err_allocs;
175 }
176 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800177 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000178 if (!res.encoder_id_ptr)
179 goto err_allocs;
180 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800181
Chris Wilsond1308f42010-01-06 15:19:25 +0000182 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800183 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000184
185 /* The number of available connectors and etc may have changed with a
186 * hotplug event in between the ioctls, in which case the field is
187 * silently ignored by the kernel.
188 */
189 if (counts.count_fbs < res.count_fbs ||
190 counts.count_crtcs < res.count_crtcs ||
191 counts.count_connectors < res.count_connectors ||
192 counts.count_encoders < res.count_encoders)
193 {
194 drmFree(U642VOID(res.fb_id_ptr));
195 drmFree(U642VOID(res.crtc_id_ptr));
196 drmFree(U642VOID(res.connector_id_ptr));
197 drmFree(U642VOID(res.encoder_id_ptr));
198
199 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800200 }
201
202 /*
203 * return
204 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800205 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000206 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800207
208 r->min_width = res.min_width;
209 r->max_width = res.max_width;
210 r->min_height = res.min_height;
211 r->max_height = res.max_height;
212 r->count_fbs = res.count_fbs;
213 r->count_crtcs = res.count_crtcs;
214 r->count_connectors = res.count_connectors;
215 r->count_encoders = res.count_encoders;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000216
217 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
218 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
219 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
220 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000221 if ((res.count_fbs && !r->fbs) ||
222 (res.count_crtcs && !r->crtcs) ||
223 (res.count_connectors && !r->connectors) ||
224 (res.count_encoders && !r->encoders))
225 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000226 drmFree(r->fbs);
227 drmFree(r->crtcs);
228 drmFree(r->connectors);
229 drmFree(r->encoders);
230 drmFree(r);
231 r = 0;
232 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800233
234err_allocs:
235 drmFree(U642VOID(res.fb_id_ptr));
236 drmFree(U642VOID(res.crtc_id_ptr));
237 drmFree(U642VOID(res.connector_id_ptr));
238 drmFree(U642VOID(res.encoder_id_ptr));
239
240 return r;
241}
242
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700243
244drm_public int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
245 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
246 uint32_t *buf_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800247{
248 struct drm_mode_fb_cmd f;
249 int ret;
250
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100251 memclear(f);
Jesse Barnes731cd552008-12-17 10:09:49 -0800252 f.width = width;
253 f.height = height;
254 f.pitch = pitch;
255 f.bpp = bpp;
256 f.depth = depth;
257 f.handle = bo_handle;
258
Chris Wilsonb8039182010-07-01 22:38:54 +0100259 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800260 return ret;
261
262 *buf_id = f.fb_id;
263 return 0;
264}
265
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700266drm_public int drmModeAddFB2WithModifiers(int fd, uint32_t width,
267 uint32_t height, uint32_t pixel_format, const uint32_t bo_handles[4],
268 const uint32_t pitches[4], const uint32_t offsets[4],
269 const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700270{
271 struct drm_mode_fb_cmd2 f;
272 int ret;
273
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100274 memclear(f);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700275 f.width = width;
276 f.height = height;
277 f.pixel_format = pixel_format;
278 f.flags = flags;
Ville Syrjälä76b4a692012-02-02 14:53:43 -0500279 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
280 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
281 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700282 if (modifier)
283 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
Jesse Barnesac168bf2011-04-29 08:53:53 -0700284
285 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
286 return ret;
287
288 *buf_id = f.fb_id;
289 return 0;
290}
291
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700292drm_public int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
293 uint32_t pixel_format, const uint32_t bo_handles[4],
294 const uint32_t pitches[4], const uint32_t offsets[4],
295 uint32_t *buf_id, uint32_t flags)
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700296{
297 return drmModeAddFB2WithModifiers(fd, width, height,
298 pixel_format, bo_handles,
299 pitches, offsets, NULL,
300 buf_id, flags);
301}
302
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700303drm_public int drmModeRmFB(int fd, uint32_t bufferId)
Jesse Barnes731cd552008-12-17 10:09:49 -0800304{
Chris Wilsonb8039182010-07-01 22:38:54 +0100305 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800306}
307
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700308drm_public drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
Jesse Barnes731cd552008-12-17 10:09:49 -0800309{
310 struct drm_mode_fb_cmd info;
311 drmModeFBPtr r;
312
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100313 memclear(info);
Jesse Barnes731cd552008-12-17 10:09:49 -0800314 info.fb_id = buf;
315
316 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
317 return NULL;
318
319 if (!(r = drmMalloc(sizeof(*r))))
320 return NULL;
321
322 r->fb_id = info.fb_id;
323 r->width = info.width;
324 r->height = info.height;
325 r->pitch = info.pitch;
326 r->bpp = info.bpp;
327 r->handle = info.handle;
328 r->depth = info.depth;
329
330 return r;
331}
332
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700333drm_public int drmModeDirtyFB(int fd, uint32_t bufferId,
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100334 drmModeClipPtr clips, uint32_t num_clips)
335{
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100336 struct drm_mode_fb_dirty_cmd dirty;
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100337
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100338 memclear(dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100339 dirty.fb_id = bufferId;
340 dirty.clips_ptr = VOID2U64(clips);
341 dirty.num_clips = num_clips;
342
Chris Wilsonb8039182010-07-01 22:38:54 +0100343 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100344}
345
Jesse Barnes731cd552008-12-17 10:09:49 -0800346/*
347 * Crtc functions
348 */
349
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700350drm_public drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
Jesse Barnes731cd552008-12-17 10:09:49 -0800351{
352 struct drm_mode_crtc crtc;
353 drmModeCrtcPtr r;
354
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100355 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800356 crtc.crtc_id = crtcId;
357
358 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
359 return 0;
360
361 /*
362 * return
363 */
364
365 if (!(r = drmMalloc(sizeof(*r))))
366 return 0;
367
368 r->crtc_id = crtc.crtc_id;
369 r->x = crtc.x;
370 r->y = crtc.y;
371 r->mode_valid = crtc.mode_valid;
Rob Clarke81acf52012-10-14 16:55:32 -0500372 if (r->mode_valid) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800373 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
Rob Clarke81acf52012-10-14 16:55:32 -0500374 r->width = crtc.mode.hdisplay;
375 r->height = crtc.mode.vdisplay;
376 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800377 r->buffer_id = crtc.fb_id;
378 r->gamma_size = crtc.gamma_size;
379 return r;
380}
381
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700382drm_public int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200383 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100384 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800385{
386 struct drm_mode_crtc crtc;
387
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100388 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800389 crtc.x = x;
390 crtc.y = y;
391 crtc.crtc_id = crtcId;
392 crtc.fb_id = bufferId;
393 crtc.set_connectors_ptr = VOID2U64(connectors);
394 crtc.count_connectors = count;
395 if (mode) {
396 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
397 crtc.mode_valid = 1;
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100398 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800399
Chris Wilsonb8039182010-07-01 22:38:54 +0100400 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800401}
402
403/*
404 * Cursor manipulation
405 */
406
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700407drm_public int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle,
408 uint32_t width, uint32_t height)
Jesse Barnes731cd552008-12-17 10:09:49 -0800409{
410 struct drm_mode_cursor arg;
411
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100412 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800413 arg.flags = DRM_MODE_CURSOR_BO;
414 arg.crtc_id = crtcId;
415 arg.width = width;
416 arg.height = height;
417 arg.handle = bo_handle;
418
Chris Wilsonb8039182010-07-01 22:38:54 +0100419 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800420}
421
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700422drm_public int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle,
423 uint32_t width, uint32_t height, int32_t hot_x,
424 int32_t hot_y)
Dave Airlie2e0ab622013-07-02 09:21:06 +0100425{
426 struct drm_mode_cursor2 arg;
427
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100428 memclear(arg);
Dave Airlie2e0ab622013-07-02 09:21:06 +0100429 arg.flags = DRM_MODE_CURSOR_BO;
430 arg.crtc_id = crtcId;
431 arg.width = width;
432 arg.height = height;
433 arg.handle = bo_handle;
434 arg.hot_x = hot_x;
435 arg.hot_y = hot_y;
436
437 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
438}
439
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700440drm_public int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
Jesse Barnes731cd552008-12-17 10:09:49 -0800441{
442 struct drm_mode_cursor arg;
443
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100444 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800445 arg.flags = DRM_MODE_CURSOR_MOVE;
446 arg.crtc_id = crtcId;
447 arg.x = x;
448 arg.y = y;
449
Chris Wilsonb8039182010-07-01 22:38:54 +0100450 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800451}
452
453/*
454 * Encoder get
455 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700456drm_public drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800457{
458 struct drm_mode_get_encoder enc;
459 drmModeEncoderPtr r = NULL;
460
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100461 memclear(enc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800462 enc.encoder_id = encoder_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800463
464 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
465 return 0;
466
467 if (!(r = drmMalloc(sizeof(*r))))
468 return 0;
469
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;
475
476 return r;
477}
478
479/*
480 * Connector manipulation
481 */
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000482static drmModeConnectorPtr
483_drmModeGetConnector(int fd, uint32_t connector_id, int probe)
Jesse Barnes731cd552008-12-17 10:09:49 -0800484{
Peter Clifton04f90a42010-01-06 20:44:11 +0000485 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800486 drmModeConnectorPtr r = NULL;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200487 struct drm_mode_modeinfo stack_mode;
Jesse Barnes731cd552008-12-17 10:09:49 -0800488
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100489 memclear(conn);
Jesse Barnes731cd552008-12-17 10:09:49 -0800490 conn.connector_id = connector_id;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000491 if (!probe) {
492 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200493 conn.modes_ptr = VOID2U64(&stack_mode);
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000494 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800495
496 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
497 return 0;
498
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000499retry:
Peter Clifton04f90a42010-01-06 20:44:11 +0000500 counts = conn;
501
Jesse Barnes731cd552008-12-17 10:09:49 -0800502 if (conn.count_props) {
503 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000504 if (!conn.props_ptr)
505 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800506 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000507 if (!conn.prop_values_ptr)
508 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800509 }
510
Peter Clifton04f90a42010-01-06 20:44:11 +0000511 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800512 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000513 if (!conn.modes_ptr)
514 goto err_allocs;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000515 } else {
516 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200517 conn.modes_ptr = VOID2U64(&stack_mode);
Peter Clifton04f90a42010-01-06 20:44:11 +0000518 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800519
Peter Clifton04f90a42010-01-06 20:44:11 +0000520 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800521 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000522 if (!conn.encoders_ptr)
523 goto err_allocs;
524 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800525
526 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
527 goto err_allocs;
528
Peter Clifton04f90a42010-01-06 20:44:11 +0000529 /* The number of available connectors and etc may have changed with a
530 * hotplug event in between the ioctls, in which case the field is
531 * silently ignored by the kernel.
532 */
533 if (counts.count_props < conn.count_props ||
534 counts.count_modes < conn.count_modes ||
535 counts.count_encoders < conn.count_encoders) {
536 drmFree(U642VOID(conn.props_ptr));
537 drmFree(U642VOID(conn.prop_values_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200538 if (U642VOID(conn.modes_ptr) != &stack_mode)
539 drmFree(U642VOID(conn.modes_ptr));
Peter Clifton04f90a42010-01-06 20:44:11 +0000540 drmFree(U642VOID(conn.encoders_ptr));
541
542 goto retry;
543 }
544
Jesse Barnes731cd552008-12-17 10:09:49 -0800545 if(!(r = drmMalloc(sizeof(*r)))) {
546 goto err_allocs;
547 }
548
549 r->connector_id = conn.connector_id;
550 r->encoder_id = conn.encoder_id;
551 r->connection = conn.connection;
552 r->mmWidth = conn.mm_width;
553 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000554 /* convert subpixel from kernel to userspace */
555 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800556 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800557 r->count_props = conn.count_props;
558 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
559 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
560 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
561 r->count_encoders = conn.count_encoders;
562 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
563 r->connector_type = conn.connector_type;
564 r->connector_type_id = conn.connector_type_id;
565
Peter Clifton04f90a42010-01-06 20:44:11 +0000566 if ((r->count_props && !r->props) ||
567 (r->count_props && !r->prop_values) ||
568 (r->count_modes && !r->modes) ||
569 (r->count_encoders && !r->encoders)) {
570 drmFree(r->props);
571 drmFree(r->prop_values);
572 drmFree(r->modes);
573 drmFree(r->encoders);
574 drmFree(r);
575 r = 0;
576 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800577
578err_allocs:
579 drmFree(U642VOID(conn.prop_values_ptr));
580 drmFree(U642VOID(conn.props_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200581 if (U642VOID(conn.modes_ptr) != &stack_mode)
582 drmFree(U642VOID(conn.modes_ptr));
Jesse Barnes731cd552008-12-17 10:09:49 -0800583 drmFree(U642VOID(conn.encoders_ptr));
584
585 return r;
586}
587
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700588drm_public drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000589{
590 return _drmModeGetConnector(fd, connector_id, 1);
591}
592
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700593drm_public drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000594{
595 return _drmModeGetConnector(fd, connector_id, 0);
596}
597
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700598drm_public int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800599{
600 struct drm_mode_mode_cmd res;
601
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100602 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800603 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
604 res.connector_id = connector_id;
605
Chris Wilsonb8039182010-07-01 22:38:54 +0100606 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800607}
608
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700609drm_public int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800610{
611 struct drm_mode_mode_cmd res;
612
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100613 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800614 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
615 res.connector_id = connector_id;
616
Chris Wilsonb8039182010-07-01 22:38:54 +0100617 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800618}
619
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700620drm_public drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800621{
622 struct drm_mode_get_property prop;
623 drmModePropertyPtr r;
624
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100625 memclear(prop);
Jesse Barnes731cd552008-12-17 10:09:49 -0800626 prop.prop_id = property_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800627
628 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
629 return 0;
630
631 if (prop.count_values)
632 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
633
Rob Clark7b228e92012-06-05 12:28:22 -0500634 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800635 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
636
637 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
638 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
639 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
640 }
641
642 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
643 r = NULL;
644 goto err_allocs;
645 }
646
647 if (!(r = drmMalloc(sizeof(*r))))
Seung-Woo Kimb39377d2019-04-29 18:10:52 +0900648 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800649
650 r->prop_id = prop.prop_id;
651 r->count_values = prop.count_values;
652
653 r->flags = prop.flags;
654 if (prop.count_values)
655 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
Rob Clark7b228e92012-06-05 12:28:22 -0500656 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800657 r->count_enums = prop.count_enum_blobs;
658 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
659 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
660 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
661 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
662 r->count_blobs = prop.count_enum_blobs;
663 }
664 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
665 r->name[DRM_PROP_NAME_LEN-1] = 0;
666
667err_allocs:
668 drmFree(U642VOID(prop.values_ptr));
669 drmFree(U642VOID(prop.enum_blob_ptr));
670
671 return r;
672}
673
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700674drm_public void drmModeFreeProperty(drmModePropertyPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800675{
676 if (!ptr)
677 return;
678
679 drmFree(ptr->values);
680 drmFree(ptr->enums);
Boram Park7915b0a2011-10-01 09:30:09 +0900681 drmFree(ptr->blob_ids);
Jesse Barnes731cd552008-12-17 10:09:49 -0800682 drmFree(ptr);
683}
684
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700685drm_public drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd,
686 uint32_t blob_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800687{
688 struct drm_mode_get_blob blob;
689 drmModePropertyBlobPtr r;
690
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100691 memclear(blob);
Jesse Barnes731cd552008-12-17 10:09:49 -0800692 blob.blob_id = blob_id;
693
694 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
695 return NULL;
696
697 if (blob.length)
698 blob.data = VOID2U64(drmMalloc(blob.length));
699
700 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
701 r = NULL;
702 goto err_allocs;
703 }
704
705 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100706 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800707
708 r->id = blob.blob_id;
709 r->length = blob.length;
710 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
711
712err_allocs:
713 drmFree(U642VOID(blob.data));
714 return r;
715}
716
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700717drm_public void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800718{
719 if (!ptr)
720 return;
721
722 drmFree(ptr->data);
723 drmFree(ptr);
724}
725
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700726drm_public int drmModeConnectorSetProperty(int fd, uint32_t connector_id,
727 uint32_t property_id,
728 uint64_t value)
Jesse Barnes731cd552008-12-17 10:09:49 -0800729{
730 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800731
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100732 memclear(osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800733 osp.connector_id = connector_id;
734 osp.prop_id = property_id;
735 osp.value = value;
736
Chris Wilsonb8039182010-07-01 22:38:54 +0100737 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800738}
739
740/*
741 * checks if a modesetting capable driver has attached to the pci id
742 * returns 0 if modesetting supported.
743 * -EINVAL or invalid bus id
744 * -ENOSYS if no modesetting support
745*/
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700746drm_public int drmCheckModesettingSupported(const char *busid)
Jesse Barnes731cd552008-12-17 10:09:49 -0800747{
Robert Millancbb31f22014-01-23 14:46:05 +0000748#if defined (__linux__)
Jesse Barnes731cd552008-12-17 10:09:49 -0800749 char pci_dev_dir[1024];
750 int domain, bus, dev, func;
751 DIR *sysdir;
752 struct dirent *dent;
753 int found = 0, ret;
754
755 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
756 if (ret != 4)
757 return -EINVAL;
758
759 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
760 domain, bus, dev, func);
761
762 sysdir = opendir(pci_dev_dir);
763 if (sysdir) {
764 dent = readdir(sysdir);
765 while (dent) {
766 if (!strncmp(dent->d_name, "controlD", 8)) {
767 found = 1;
768 break;
769 }
770
771 dent = readdir(sysdir);
772 }
773 closedir(sysdir);
774 if (found)
775 return 0;
776 }
777
778 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
779 domain, bus, dev, func);
780
781 sysdir = opendir(pci_dev_dir);
782 if (!sysdir)
783 return -EINVAL;
784
785 dent = readdir(sysdir);
786 while (dent) {
787 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
788 found = 1;
789 break;
790 }
791
792 dent = readdir(sysdir);
793 }
794
795 closedir(sysdir);
796 if (found)
797 return 0;
Robert Millancbb31f22014-01-23 14:46:05 +0000798#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
Emmanuel Vadot24e68522020-01-21 18:41:38 +0100799 char sbusid[1024];
Robert Millancbb31f22014-01-23 14:46:05 +0000800 char oid[128];
Robert Millancbb31f22014-01-23 14:46:05 +0000801 int i, modesetting, ret;
802 size_t len;
803
Robert Millancbb31f22014-01-23 14:46:05 +0000804 /* How many GPUs do we expect in the machine ? */
Emmanuel Vadotbb584b82020-01-24 20:43:05 +0100805 for (i = 0; i < 10; i++) {
Robert Millancbb31f22014-01-23 14:46:05 +0000806 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
807 len = sizeof(sbusid);
808 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
809 if (ret == -1) {
810 if (errno == ENOENT)
811 continue;
812 return -EINVAL;
813 }
Emmanuel Vadot24e68522020-01-21 18:41:38 +0100814 if (strcmp(sbusid, busid) != 0)
Robert Millancbb31f22014-01-23 14:46:05 +0000815 continue;
816 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
817 len = sizeof(modesetting);
818 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
819 if (ret == -1 || len != sizeof(modesetting))
820 return -EINVAL;
821 return (modesetting ? 0 : -ENOSYS);
822 }
François Tigeotedbb4e52014-07-26 13:39:58 +0200823#elif defined(__DragonFly__)
824 return 0;
Eric Engestrom00aa3742018-03-22 17:08:37 +0000825#elif defined(__OpenBSD__)
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000826 int fd;
827 struct drm_mode_card_res res;
828 drmModeResPtr r = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800829
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000830 if ((fd = drmOpen(NULL, busid)) < 0)
831 return -EINVAL;
832
833 memset(&res, 0, sizeof(struct drm_mode_card_res));
834
835 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
836 drmClose(fd);
837 return -errno;
838 }
839
840 drmClose(fd);
841 return 0;
842#endif
843 return -ENOSYS;
Jesse Barnes731cd552008-12-17 10:09:49 -0800844}
845
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700846drm_public int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
847 uint16_t *red, uint16_t *green,
848 uint16_t *blue)
Jesse Barnes731cd552008-12-17 10:09:49 -0800849{
Jesse Barnes731cd552008-12-17 10:09:49 -0800850 struct drm_mode_crtc_lut l;
851
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100852 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800853 l.crtc_id = crtc_id;
854 l.gamma_size = size;
855 l.red = VOID2U64(red);
856 l.green = VOID2U64(green);
857 l.blue = VOID2U64(blue);
858
Chris Wilsonb8039182010-07-01 22:38:54 +0100859 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800860}
861
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700862drm_public int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
863 uint16_t *red, uint16_t *green,
864 uint16_t *blue)
Jesse Barnes731cd552008-12-17 10:09:49 -0800865{
Jesse Barnes731cd552008-12-17 10:09:49 -0800866 struct drm_mode_crtc_lut l;
867
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100868 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800869 l.crtc_id = crtc_id;
870 l.gamma_size = size;
871 l.red = VOID2U64(red);
872 l.green = VOID2U64(green);
873 l.blue = VOID2U64(blue);
874
Chris Wilsonb8039182010-07-01 22:38:54 +0100875 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800876}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400877
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700878drm_public int drmHandleEvent(int fd, drmEventContextPtr evctx)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400879{
880 char buffer[1024];
881 int len, i;
882 struct drm_event *e;
883 struct drm_event_vblank *vblank;
Keith Packardd4331dd2017-07-01 00:43:15 -0700884 struct drm_event_crtc_sequence *seq;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300885 void *user_data;
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900886
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400887 /* The DRM read semantics guarantees that we always get only
888 * complete events. */
889
890 len = read(fd, buffer, sizeof buffer);
891 if (len == 0)
892 return 0;
Jan Veselyde8532d2014-11-30 12:53:18 -0500893 if (len < (int)sizeof *e)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400894 return -1;
895
896 i = 0;
897 while (i < len) {
Thierry Redingecc2a092015-04-13 11:36:59 +0200898 e = (struct drm_event *)(buffer + i);
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400899 switch (e->type) {
900 case DRM_EVENT_VBLANK:
901 if (evctx->version < 1 ||
902 evctx->vblank_handler == NULL)
903 break;
904 vblank = (struct drm_event_vblank *) e;
905 evctx->vblank_handler(fd,
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900906 vblank->sequence,
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400907 vblank->tv_sec,
908 vblank->tv_usec,
909 U642VOID (vblank->user_data));
910 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500911 case DRM_EVENT_FLIP_COMPLETE:
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500912 vblank = (struct drm_event_vblank *) e;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300913 user_data = U642VOID (vblank->user_data);
914
915 if (evctx->version >= 3 && evctx->page_flip_handler2)
916 evctx->page_flip_handler2(fd,
917 vblank->sequence,
918 vblank->tv_sec,
919 vblank->tv_usec,
920 vblank->crtc_id,
921 user_data);
922 else if (evctx->version >= 2 && evctx->page_flip_handler)
923 evctx->page_flip_handler(fd,
924 vblank->sequence,
925 vblank->tv_sec,
926 vblank->tv_usec,
927 user_data);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500928 break;
Keith Packardd4331dd2017-07-01 00:43:15 -0700929 case DRM_EVENT_CRTC_SEQUENCE:
930 seq = (struct drm_event_crtc_sequence *) e;
931 if (evctx->version >= 4 && evctx->sequence_handler)
932 evctx->sequence_handler(fd,
933 seq->sequence,
934 seq->time_ns,
935 seq->user_data);
936 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400937 default:
938 break;
939 }
940 i += e->length;
941 }
942
943 return 0;
944}
945
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700946drm_public int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500947 uint32_t flags, void *user_data)
948{
949 struct drm_mode_crtc_page_flip flip;
950
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100951 memclear(flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500952 flip.fb_id = fb_id;
953 flip.crtc_id = crtc_id;
954 flip.user_data = VOID2U64(user_data);
955 flip.flags = flags;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500956
Chris Wilsonb8039182010-07-01 22:38:54 +0100957 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500958}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700959
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700960drm_public int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
Michel Dänzer7dd28472016-06-29 18:07:25 +0900961 uint32_t flags, void *user_data,
962 uint32_t target_vblank)
963{
964 struct drm_mode_crtc_page_flip_target flip_target;
965
966 memclear(flip_target);
967 flip_target.fb_id = fb_id;
968 flip_target.crtc_id = crtc_id;
969 flip_target.user_data = VOID2U64(user_data);
970 flip_target.flags = flags;
971 flip_target.sequence = target_vblank;
972
973 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
974}
975
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700976drm_public int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700977 uint32_t fb_id, uint32_t flags,
Daniel Kurtz828c3e82014-05-01 19:56:43 +0800978 int32_t crtc_x, int32_t crtc_y,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700979 uint32_t crtc_w, uint32_t crtc_h,
980 uint32_t src_x, uint32_t src_y,
981 uint32_t src_w, uint32_t src_h)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700982{
983 struct drm_mode_set_plane s;
984
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100985 memclear(s);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700986 s.plane_id = plane_id;
987 s.crtc_id = crtc_id;
988 s.fb_id = fb_id;
989 s.flags = flags;
990 s.crtc_x = crtc_x;
991 s.crtc_y = crtc_y;
992 s.crtc_w = crtc_w;
993 s.crtc_h = crtc_h;
994 s.src_x = src_x;
995 s.src_y = src_y;
996 s.src_w = src_w;
997 s.src_h = src_h;
998
999 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1000}
1001
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001002drm_public drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001003{
1004 struct drm_mode_get_plane ovr, counts;
1005 drmModePlanePtr r = 0;
1006
1007retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001008 memclear(ovr);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001009 ovr.plane_id = plane_id;
1010 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1011 return 0;
1012
1013 counts = ovr;
1014
1015 if (ovr.count_format_types) {
1016 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1017 sizeof(uint32_t)));
1018 if (!ovr.format_type_ptr)
1019 goto err_allocs;
1020 }
1021
1022 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1023 goto err_allocs;
1024
1025 if (counts.count_format_types < ovr.count_format_types) {
1026 drmFree(U642VOID(ovr.format_type_ptr));
1027 goto retry;
1028 }
1029
1030 if (!(r = drmMalloc(sizeof(*r))))
1031 goto err_allocs;
1032
1033 r->count_formats = ovr.count_format_types;
1034 r->plane_id = ovr.plane_id;
1035 r->crtc_id = ovr.crtc_id;
1036 r->fb_id = ovr.fb_id;
1037 r->possible_crtcs = ovr.possible_crtcs;
1038 r->gamma_size = ovr.gamma_size;
1039 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1040 ovr.count_format_types, sizeof(uint32_t));
1041 if (ovr.count_format_types && !r->formats) {
1042 drmFree(r->formats);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001043 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001044 r = 0;
1045 }
1046
1047err_allocs:
1048 drmFree(U642VOID(ovr.format_type_ptr));
1049
1050 return r;
1051}
1052
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001053drm_public void drmModeFreePlane(drmModePlanePtr ptr)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001054{
1055 if (!ptr)
1056 return;
1057
1058 drmFree(ptr->formats);
1059 drmFree(ptr);
1060}
1061
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001062drm_public drmModePlaneResPtr drmModeGetPlaneResources(int fd)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001063{
1064 struct drm_mode_get_plane_res res, counts;
1065 drmModePlaneResPtr r = 0;
1066
1067retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001068 memclear(res);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001069 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1070 return 0;
1071
1072 counts = res;
1073
1074 if (res.count_planes) {
1075 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1076 sizeof(uint32_t)));
1077 if (!res.plane_id_ptr)
1078 goto err_allocs;
1079 }
1080
1081 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1082 goto err_allocs;
1083
1084 if (counts.count_planes < res.count_planes) {
1085 drmFree(U642VOID(res.plane_id_ptr));
1086 goto retry;
1087 }
1088
1089 if (!(r = drmMalloc(sizeof(*r))))
1090 goto err_allocs;
1091
1092 r->count_planes = res.count_planes;
1093 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1094 res.count_planes, sizeof(uint32_t));
1095 if (res.count_planes && !r->planes) {
1096 drmFree(r->planes);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001097 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001098 r = 0;
1099 }
1100
1101err_allocs:
1102 drmFree(U642VOID(res.plane_id_ptr));
1103
1104 return r;
1105}
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001106
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001107drm_public void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001108{
1109 if (!ptr)
1110 return;
1111
1112 drmFree(ptr->planes);
1113 drmFree(ptr);
1114}
Paulo Zanoni8c757032012-05-15 18:38:28 -03001115
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001116drm_public drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
Paulo Zanoni8c757032012-05-15 18:38:28 -03001117 uint32_t object_id,
1118 uint32_t object_type)
1119{
1120 struct drm_mode_obj_get_properties properties;
1121 drmModeObjectPropertiesPtr ret = NULL;
1122 uint32_t count;
1123
1124retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001125 memclear(properties);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001126 properties.obj_id = object_id;
1127 properties.obj_type = object_type;
1128
1129 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1130 return 0;
1131
1132 count = properties.count_props;
1133
1134 if (count) {
1135 properties.props_ptr = VOID2U64(drmMalloc(count *
1136 sizeof(uint32_t)));
1137 if (!properties.props_ptr)
1138 goto err_allocs;
1139 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1140 sizeof(uint64_t)));
1141 if (!properties.prop_values_ptr)
1142 goto err_allocs;
1143 }
1144
1145 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1146 goto err_allocs;
1147
1148 if (count < properties.count_props) {
1149 drmFree(U642VOID(properties.props_ptr));
1150 drmFree(U642VOID(properties.prop_values_ptr));
1151 goto retry;
1152 }
1153 count = properties.count_props;
1154
1155 ret = drmMalloc(sizeof(*ret));
1156 if (!ret)
1157 goto err_allocs;
1158
1159 ret->count_props = count;
1160 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1161 count, sizeof(uint32_t));
1162 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1163 count, sizeof(uint64_t));
1164 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1165 drmFree(ret->props);
1166 drmFree(ret->prop_values);
1167 drmFree(ret);
1168 ret = NULL;
1169 }
1170
1171err_allocs:
1172 drmFree(U642VOID(properties.props_ptr));
1173 drmFree(U642VOID(properties.prop_values_ptr));
1174 return ret;
1175}
1176
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001177drm_public void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
Paulo Zanoni8c757032012-05-15 18:38:28 -03001178{
1179 if (!ptr)
1180 return;
1181 drmFree(ptr->props);
1182 drmFree(ptr->prop_values);
1183 drmFree(ptr);
1184}
1185
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001186drm_public int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
Paulo Zanoni8c757032012-05-15 18:38:28 -03001187 uint32_t property_id, uint64_t value)
1188{
1189 struct drm_mode_obj_set_property prop;
1190
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001191 memclear(prop);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001192 prop.value = value;
1193 prop.prop_id = property_id;
1194 prop.obj_id = object_id;
1195 prop.obj_type = object_type;
1196
1197 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1198}
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001199
1200typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1201
1202struct _drmModeAtomicReqItem {
1203 uint32_t object_id;
1204 uint32_t property_id;
1205 uint64_t value;
1206};
1207
1208struct _drmModeAtomicReq {
1209 uint32_t cursor;
1210 uint32_t size_items;
1211 drmModeAtomicReqItemPtr items;
1212};
1213
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001214drm_public drmModeAtomicReqPtr drmModeAtomicAlloc(void)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001215{
1216 drmModeAtomicReqPtr req;
1217
1218 req = drmMalloc(sizeof *req);
1219 if (!req)
1220 return NULL;
1221
1222 req->items = NULL;
1223 req->cursor = 0;
1224 req->size_items = 0;
1225
1226 return req;
1227}
1228
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001229drm_public drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001230{
1231 drmModeAtomicReqPtr new;
1232
Emil Velikoved3c6652015-09-05 17:20:53 +01001233 if (!old)
1234 return NULL;
1235
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001236 new = drmMalloc(sizeof *new);
1237 if (!new)
1238 return NULL;
1239
1240 new->cursor = old->cursor;
1241 new->size_items = old->size_items;
1242
1243 if (old->size_items) {
1244 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1245 if (!new->items) {
1246 free(new);
1247 return NULL;
1248 }
1249 memcpy(new->items, old->items,
Adrian Salido763f6462019-04-24 10:08:40 -07001250 old->cursor * sizeof(*new->items));
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001251 } else {
1252 new->items = NULL;
1253 }
1254
1255 return new;
1256}
1257
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001258drm_public int drmModeAtomicMerge(drmModeAtomicReqPtr base,
1259 drmModeAtomicReqPtr augment)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001260{
Emil Velikoved3c6652015-09-05 17:20:53 +01001261 if (!base)
1262 return -EINVAL;
1263
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001264 if (!augment || augment->cursor == 0)
1265 return 0;
1266
1267 if (base->cursor + augment->cursor >= base->size_items) {
1268 drmModeAtomicReqItemPtr new;
1269 int saved_size = base->size_items;
1270
1271 base->size_items = base->cursor + augment->cursor;
1272 new = realloc(base->items,
1273 base->size_items * sizeof(*base->items));
1274 if (!new) {
1275 base->size_items = saved_size;
1276 return -ENOMEM;
1277 }
1278 base->items = new;
1279 }
1280
1281 memcpy(&base->items[base->cursor], augment->items,
1282 augment->cursor * sizeof(*augment->items));
1283 base->cursor += augment->cursor;
1284
1285 return 0;
1286}
1287
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001288drm_public int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001289{
Emil Velikoved3c6652015-09-05 17:20:53 +01001290 if (!req)
1291 return -EINVAL;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001292 return req->cursor;
1293}
1294
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001295drm_public void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001296{
Emil Velikoved3c6652015-09-05 17:20:53 +01001297 if (req)
1298 req->cursor = cursor;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001299}
1300
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001301drm_public int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1302 uint32_t object_id,
1303 uint32_t property_id,
1304 uint64_t value)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001305{
Emil Velikoved3c6652015-09-05 17:20:53 +01001306 if (!req)
1307 return -EINVAL;
1308
Daniel Stone45eee3f2018-03-07 12:41:12 +00001309 if (object_id == 0 || property_id == 0)
1310 return -EINVAL;
1311
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001312 if (req->cursor >= req->size_items) {
Adrian Salido763f6462019-04-24 10:08:40 -07001313 const uint32_t item_size_inc = getpagesize() / sizeof(*req->items);
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001314 drmModeAtomicReqItemPtr new;
1315
Adrian Salido763f6462019-04-24 10:08:40 -07001316 req->size_items += item_size_inc;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001317 new = realloc(req->items, req->size_items * sizeof(*req->items));
1318 if (!new) {
Adrian Salido763f6462019-04-24 10:08:40 -07001319 req->size_items -= item_size_inc;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001320 return -ENOMEM;
1321 }
1322 req->items = new;
1323 }
1324
1325 req->items[req->cursor].object_id = object_id;
1326 req->items[req->cursor].property_id = property_id;
1327 req->items[req->cursor].value = value;
1328 req->cursor++;
1329
1330 return req->cursor;
1331}
1332
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001333drm_public void drmModeAtomicFree(drmModeAtomicReqPtr req)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001334{
1335 if (!req)
1336 return;
1337
1338 if (req->items)
1339 drmFree(req->items);
1340 drmFree(req);
1341}
1342
1343static int sort_req_list(const void *misc, const void *other)
1344{
1345 const drmModeAtomicReqItem *first = misc;
1346 const drmModeAtomicReqItem *second = other;
1347
1348 if (first->object_id < second->object_id)
1349 return -1;
1350 else if (first->object_id > second->object_id)
1351 return 1;
1352 else
1353 return second->property_id - first->property_id;
1354}
1355
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001356drm_public int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req,
1357 uint32_t flags, void *user_data)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001358{
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001359 drmModeAtomicReqPtr sorted;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001360 struct drm_mode_atomic atomic;
1361 uint32_t *objs_ptr = NULL;
1362 uint32_t *count_props_ptr = NULL;
1363 uint32_t *props_ptr = NULL;
1364 uint64_t *prop_values_ptr = NULL;
1365 uint32_t last_obj_id = 0;
1366 uint32_t i;
1367 int obj_idx = -1;
1368 int ret = -1;
1369
Emil Velikoved3c6652015-09-05 17:20:53 +01001370 if (!req)
1371 return -EINVAL;
1372
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001373 if (req->cursor == 0)
1374 return 0;
1375
1376 sorted = drmModeAtomicDuplicate(req);
1377 if (sorted == NULL)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001378 return -ENOMEM;
1379
1380 memclear(atomic);
1381
1382 /* Sort the list by object ID, then by property ID. */
1383 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1384 sort_req_list);
1385
1386 /* Now the list is sorted, eliminate duplicate property sets. */
1387 for (i = 0; i < sorted->cursor; i++) {
1388 if (sorted->items[i].object_id != last_obj_id) {
1389 atomic.count_objs++;
1390 last_obj_id = sorted->items[i].object_id;
1391 }
1392
1393 if (i == sorted->cursor - 1)
1394 continue;
1395
1396 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1397 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1398 continue;
1399
1400 memmove(&sorted->items[i], &sorted->items[i + 1],
1401 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1402 sorted->cursor--;
1403 }
1404
1405 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1406 if (!objs_ptr) {
1407 errno = ENOMEM;
1408 goto out;
1409 }
1410
1411 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1412 if (!count_props_ptr) {
1413 errno = ENOMEM;
1414 goto out;
1415 }
1416
1417 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1418 if (!props_ptr) {
1419 errno = ENOMEM;
1420 goto out;
1421 }
1422
1423 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1424 if (!prop_values_ptr) {
1425 errno = ENOMEM;
1426 goto out;
1427 }
1428
1429 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1430 if (sorted->items[i].object_id != last_obj_id) {
1431 obj_idx++;
1432 objs_ptr[obj_idx] = sorted->items[i].object_id;
1433 last_obj_id = objs_ptr[obj_idx];
1434 }
1435
1436 count_props_ptr[obj_idx]++;
1437 props_ptr[i] = sorted->items[i].property_id;
1438 prop_values_ptr[i] = sorted->items[i].value;
1439
1440 }
1441
1442 atomic.flags = flags;
1443 atomic.objs_ptr = VOID2U64(objs_ptr);
1444 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1445 atomic.props_ptr = VOID2U64(props_ptr);
1446 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1447 atomic.user_data = VOID2U64(user_data);
1448
1449 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1450
1451out:
1452 drmFree(objs_ptr);
1453 drmFree(count_props_ptr);
1454 drmFree(props_ptr);
1455 drmFree(prop_values_ptr);
1456 drmModeAtomicFree(sorted);
1457
1458 return ret;
1459}
Daniel Stone32471b22015-06-22 17:26:03 +01001460
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001461drm_public int
1462drmModeCreatePropertyBlob(int fd, const void *data, size_t length,
1463 uint32_t *id)
Daniel Stone32471b22015-06-22 17:26:03 +01001464{
1465 struct drm_mode_create_blob create;
1466 int ret;
1467
1468 if (length >= 0xffffffff)
1469 return -ERANGE;
1470
1471 memclear(create);
1472
1473 create.length = length;
1474 create.data = (uintptr_t) data;
1475 create.blob_id = 0;
1476 *id = 0;
1477
1478 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1479 if (ret != 0)
1480 return ret;
1481
1482 *id = create.blob_id;
1483 return 0;
1484}
1485
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001486drm_public int
Daniel Stone32471b22015-06-22 17:26:03 +01001487drmModeDestroyPropertyBlob(int fd, uint32_t id)
1488{
1489 struct drm_mode_destroy_blob destroy;
1490
1491 memclear(destroy);
1492 destroy.blob_id = id;
1493 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1494}
Keith Packardc4171532017-03-16 18:11:05 -07001495
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001496drm_public int
1497drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags,
1498 uint32_t *lessee_id)
Keith Packardc4171532017-03-16 18:11:05 -07001499{
1500 struct drm_mode_create_lease create;
1501 int ret;
1502
1503 memclear(create);
1504 create.object_ids = (uintptr_t) objects;
1505 create.object_count = num_objects;
1506 create.flags = flags;
1507
1508 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1509 if (ret == 0) {
1510 *lessee_id = create.lessee_id;
1511 return create.fd;
1512 }
1513 return -errno;
1514}
1515
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001516drm_public drmModeLesseeListPtr
Keith Packardc4171532017-03-16 18:11:05 -07001517drmModeListLessees(int fd)
1518{
1519 struct drm_mode_list_lessees list;
1520 uint32_t count;
1521 drmModeLesseeListPtr ret;
1522
1523 memclear(list);
1524
1525 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1526 return NULL;
1527
1528 count = list.count_lessees;
1529 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1530 if (!ret)
1531 return NULL;
1532
1533 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1534 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1535 drmFree(ret);
1536 return NULL;
1537 }
1538
1539 ret->count = count;
1540 return ret;
1541}
1542
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001543drm_public drmModeObjectListPtr
Keith Packardc4171532017-03-16 18:11:05 -07001544drmModeGetLease(int fd)
1545{
1546 struct drm_mode_get_lease get;
1547 uint32_t count;
1548 drmModeObjectListPtr ret;
1549
1550 memclear(get);
1551
1552 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1553 return NULL;
1554
1555 count = get.count_objects;
1556 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1557 if (!ret)
1558 return NULL;
1559
1560 get.objects_ptr = VOID2U64(&ret->objects[0]);
1561 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1562 drmFree(ret);
1563 return NULL;
1564 }
1565
1566 ret->count = count;
1567 return ret;
1568}
1569
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001570drm_public int
Keith Packardc4171532017-03-16 18:11:05 -07001571drmModeRevokeLease(int fd, uint32_t lessee_id)
1572{
1573 struct drm_mode_revoke_lease revoke;
1574 int ret;
1575
1576 memclear(revoke);
1577
1578 revoke.lessee_id = lessee_id;
1579
1580 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
1581 if (ret == 0)
1582 return 0;
1583 return -errno;
1584}
Daniel Stoned8731e92020-02-11 12:19:16 -08001585
1586drm_public drmModeFB2Ptr
1587drmModeGetFB2(int fd, uint32_t fb_id)
1588{
1589 struct drm_mode_fb_cmd2 get = {
1590 .fb_id = fb_id,
1591 };
1592 drmModeFB2Ptr ret;
1593 int err;
1594
1595 err = DRM_IOCTL(fd, DRM_IOCTL_MODE_GETFB2, &get);
1596 if (err != 0)
1597 return NULL;
1598
1599 ret = drmMalloc(sizeof(drmModeFB2));
1600 if (!ret)
1601 return NULL;
1602
1603 ret->fb_id = fb_id;
1604 ret->width = get.width;
1605 ret->height = get.height;
1606 ret->pixel_format = get.pixel_format;
1607 ret->flags = get.flags;
1608 ret->modifier = get.modifier[0];
1609 memcpy(ret->handles, get.handles, sizeof(uint32_t) * 4);
1610 memcpy(ret->pitches, get.pitches, sizeof(uint32_t) * 4);
1611 memcpy(ret->offsets, get.offsets, sizeof(uint32_t) * 4);
1612
1613 return ret;
1614}
1615
1616drm_public void drmModeFreeFB2(drmModeFB2Ptr ptr)
1617{
1618 drmFree(ptr);
1619}