blob: c3920b913fa351fba53f878035e382b17451b40b [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
Simon Ser523b3652021-02-23 18:19:33 +0100149drm_public int drmIsKMS(int fd)
150{
151 struct drm_mode_card_res res = {0};
152
153 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res) != 0)
154 return 0;
155
156 return res.count_crtcs > 0 && res.count_connectors > 0 && res.count_encoders > 0;
157}
158
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700159drm_public drmModeResPtr drmModeGetResources(int fd)
Jesse Barnes731cd552008-12-17 10:09:49 -0800160{
Chris Wilsond1308f42010-01-06 15:19:25 +0000161 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800162 drmModeResPtr r = 0;
163
Chris Wilsond1308f42010-01-06 15:19:25 +0000164retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100165 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800166 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
167 return 0;
168
Chris Wilsond1308f42010-01-06 15:19:25 +0000169 counts = res;
170
Chris Wilson85fb3e52010-01-06 15:39:49 +0000171 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800172 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000173 if (!res.fb_id_ptr)
174 goto err_allocs;
175 }
176 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800177 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000178 if (!res.crtc_id_ptr)
179 goto err_allocs;
180 }
181 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800182 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000183 if (!res.connector_id_ptr)
184 goto err_allocs;
185 }
186 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800187 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000188 if (!res.encoder_id_ptr)
189 goto err_allocs;
190 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800191
Chris Wilsond1308f42010-01-06 15:19:25 +0000192 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800193 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000194
195 /* The number of available connectors and etc may have changed with a
196 * hotplug event in between the ioctls, in which case the field is
197 * silently ignored by the kernel.
198 */
199 if (counts.count_fbs < res.count_fbs ||
200 counts.count_crtcs < res.count_crtcs ||
201 counts.count_connectors < res.count_connectors ||
202 counts.count_encoders < res.count_encoders)
203 {
204 drmFree(U642VOID(res.fb_id_ptr));
205 drmFree(U642VOID(res.crtc_id_ptr));
206 drmFree(U642VOID(res.connector_id_ptr));
207 drmFree(U642VOID(res.encoder_id_ptr));
208
209 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800210 }
211
212 /*
213 * return
214 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800215 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000216 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800217
218 r->min_width = res.min_width;
219 r->max_width = res.max_width;
220 r->min_height = res.min_height;
221 r->max_height = res.max_height;
222 r->count_fbs = res.count_fbs;
223 r->count_crtcs = res.count_crtcs;
224 r->count_connectors = res.count_connectors;
225 r->count_encoders = res.count_encoders;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000226
227 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
228 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
229 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
230 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000231 if ((res.count_fbs && !r->fbs) ||
232 (res.count_crtcs && !r->crtcs) ||
233 (res.count_connectors && !r->connectors) ||
234 (res.count_encoders && !r->encoders))
235 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000236 drmFree(r->fbs);
237 drmFree(r->crtcs);
238 drmFree(r->connectors);
239 drmFree(r->encoders);
240 drmFree(r);
241 r = 0;
242 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800243
244err_allocs:
245 drmFree(U642VOID(res.fb_id_ptr));
246 drmFree(U642VOID(res.crtc_id_ptr));
247 drmFree(U642VOID(res.connector_id_ptr));
248 drmFree(U642VOID(res.encoder_id_ptr));
249
250 return r;
251}
252
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700253
254drm_public int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
255 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
256 uint32_t *buf_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800257{
258 struct drm_mode_fb_cmd f;
259 int ret;
260
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100261 memclear(f);
Jesse Barnes731cd552008-12-17 10:09:49 -0800262 f.width = width;
263 f.height = height;
264 f.pitch = pitch;
265 f.bpp = bpp;
266 f.depth = depth;
267 f.handle = bo_handle;
268
Chris Wilsonb8039182010-07-01 22:38:54 +0100269 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800270 return ret;
271
272 *buf_id = f.fb_id;
273 return 0;
274}
275
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700276drm_public int drmModeAddFB2WithModifiers(int fd, uint32_t width,
277 uint32_t height, uint32_t pixel_format, const uint32_t bo_handles[4],
278 const uint32_t pitches[4], const uint32_t offsets[4],
279 const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700280{
281 struct drm_mode_fb_cmd2 f;
282 int ret;
283
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100284 memclear(f);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700285 f.width = width;
286 f.height = height;
287 f.pixel_format = pixel_format;
288 f.flags = flags;
Ville Syrjälä76b4a692012-02-02 14:53:43 -0500289 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
290 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
291 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700292 if (modifier)
293 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
Jesse Barnesac168bf2011-04-29 08:53:53 -0700294
295 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
296 return ret;
297
298 *buf_id = f.fb_id;
299 return 0;
300}
301
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700302drm_public int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
303 uint32_t pixel_format, const uint32_t bo_handles[4],
304 const uint32_t pitches[4], const uint32_t offsets[4],
305 uint32_t *buf_id, uint32_t flags)
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700306{
307 return drmModeAddFB2WithModifiers(fd, width, height,
308 pixel_format, bo_handles,
309 pitches, offsets, NULL,
310 buf_id, flags);
311}
312
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700313drm_public int drmModeRmFB(int fd, uint32_t bufferId)
Jesse Barnes731cd552008-12-17 10:09:49 -0800314{
Chris Wilsonb8039182010-07-01 22:38:54 +0100315 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800316}
317
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700318drm_public drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
Jesse Barnes731cd552008-12-17 10:09:49 -0800319{
320 struct drm_mode_fb_cmd info;
321 drmModeFBPtr r;
322
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100323 memclear(info);
Jesse Barnes731cd552008-12-17 10:09:49 -0800324 info.fb_id = buf;
325
326 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
327 return NULL;
328
329 if (!(r = drmMalloc(sizeof(*r))))
330 return NULL;
331
332 r->fb_id = info.fb_id;
333 r->width = info.width;
334 r->height = info.height;
335 r->pitch = info.pitch;
336 r->bpp = info.bpp;
337 r->handle = info.handle;
338 r->depth = info.depth;
339
340 return r;
341}
342
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700343drm_public int drmModeDirtyFB(int fd, uint32_t bufferId,
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100344 drmModeClipPtr clips, uint32_t num_clips)
345{
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100346 struct drm_mode_fb_dirty_cmd dirty;
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100347
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100348 memclear(dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100349 dirty.fb_id = bufferId;
350 dirty.clips_ptr = VOID2U64(clips);
351 dirty.num_clips = num_clips;
352
Chris Wilsonb8039182010-07-01 22:38:54 +0100353 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100354}
355
Jesse Barnes731cd552008-12-17 10:09:49 -0800356/*
357 * Crtc functions
358 */
359
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700360drm_public drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
Jesse Barnes731cd552008-12-17 10:09:49 -0800361{
362 struct drm_mode_crtc crtc;
363 drmModeCrtcPtr r;
364
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100365 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800366 crtc.crtc_id = crtcId;
367
368 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
369 return 0;
370
371 /*
372 * return
373 */
374
375 if (!(r = drmMalloc(sizeof(*r))))
376 return 0;
377
378 r->crtc_id = crtc.crtc_id;
379 r->x = crtc.x;
380 r->y = crtc.y;
381 r->mode_valid = crtc.mode_valid;
Rob Clarke81acf52012-10-14 16:55:32 -0500382 if (r->mode_valid) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800383 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
Rob Clarke81acf52012-10-14 16:55:32 -0500384 r->width = crtc.mode.hdisplay;
385 r->height = crtc.mode.vdisplay;
386 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800387 r->buffer_id = crtc.fb_id;
388 r->gamma_size = crtc.gamma_size;
389 return r;
390}
391
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700392drm_public int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200393 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100394 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800395{
396 struct drm_mode_crtc crtc;
397
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100398 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800399 crtc.x = x;
400 crtc.y = y;
401 crtc.crtc_id = crtcId;
402 crtc.fb_id = bufferId;
403 crtc.set_connectors_ptr = VOID2U64(connectors);
404 crtc.count_connectors = count;
405 if (mode) {
406 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
407 crtc.mode_valid = 1;
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100408 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800409
Chris Wilsonb8039182010-07-01 22:38:54 +0100410 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800411}
412
413/*
414 * Cursor manipulation
415 */
416
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700417drm_public int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle,
418 uint32_t width, uint32_t height)
Jesse Barnes731cd552008-12-17 10:09:49 -0800419{
420 struct drm_mode_cursor arg;
421
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100422 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800423 arg.flags = DRM_MODE_CURSOR_BO;
424 arg.crtc_id = crtcId;
425 arg.width = width;
426 arg.height = height;
427 arg.handle = bo_handle;
428
Chris Wilsonb8039182010-07-01 22:38:54 +0100429 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800430}
431
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700432drm_public int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle,
433 uint32_t width, uint32_t height, int32_t hot_x,
434 int32_t hot_y)
Dave Airlie2e0ab622013-07-02 09:21:06 +0100435{
436 struct drm_mode_cursor2 arg;
437
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100438 memclear(arg);
Dave Airlie2e0ab622013-07-02 09:21:06 +0100439 arg.flags = DRM_MODE_CURSOR_BO;
440 arg.crtc_id = crtcId;
441 arg.width = width;
442 arg.height = height;
443 arg.handle = bo_handle;
444 arg.hot_x = hot_x;
445 arg.hot_y = hot_y;
446
447 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
448}
449
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700450drm_public int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
Jesse Barnes731cd552008-12-17 10:09:49 -0800451{
452 struct drm_mode_cursor arg;
453
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100454 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800455 arg.flags = DRM_MODE_CURSOR_MOVE;
456 arg.crtc_id = crtcId;
457 arg.x = x;
458 arg.y = y;
459
Chris Wilsonb8039182010-07-01 22:38:54 +0100460 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800461}
462
463/*
464 * Encoder get
465 */
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700466drm_public drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800467{
468 struct drm_mode_get_encoder enc;
469 drmModeEncoderPtr r = NULL;
470
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100471 memclear(enc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800472 enc.encoder_id = encoder_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800473
474 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
475 return 0;
476
477 if (!(r = drmMalloc(sizeof(*r))))
478 return 0;
479
480 r->encoder_id = enc.encoder_id;
481 r->crtc_id = enc.crtc_id;
482 r->encoder_type = enc.encoder_type;
483 r->possible_crtcs = enc.possible_crtcs;
484 r->possible_clones = enc.possible_clones;
485
486 return r;
487}
488
489/*
490 * Connector manipulation
491 */
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000492static drmModeConnectorPtr
493_drmModeGetConnector(int fd, uint32_t connector_id, int probe)
Jesse Barnes731cd552008-12-17 10:09:49 -0800494{
Peter Clifton04f90a42010-01-06 20:44:11 +0000495 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800496 drmModeConnectorPtr r = NULL;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200497 struct drm_mode_modeinfo stack_mode;
Jesse Barnes731cd552008-12-17 10:09:49 -0800498
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100499 memclear(conn);
Jesse Barnes731cd552008-12-17 10:09:49 -0800500 conn.connector_id = connector_id;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000501 if (!probe) {
502 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200503 conn.modes_ptr = VOID2U64(&stack_mode);
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000504 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800505
506 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
507 return 0;
508
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000509retry:
Peter Clifton04f90a42010-01-06 20:44:11 +0000510 counts = conn;
511
Jesse Barnes731cd552008-12-17 10:09:49 -0800512 if (conn.count_props) {
513 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000514 if (!conn.props_ptr)
515 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800516 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000517 if (!conn.prop_values_ptr)
518 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800519 }
520
Peter Clifton04f90a42010-01-06 20:44:11 +0000521 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800522 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000523 if (!conn.modes_ptr)
524 goto err_allocs;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000525 } else {
526 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200527 conn.modes_ptr = VOID2U64(&stack_mode);
Peter Clifton04f90a42010-01-06 20:44:11 +0000528 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800529
Peter Clifton04f90a42010-01-06 20:44:11 +0000530 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800531 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000532 if (!conn.encoders_ptr)
533 goto err_allocs;
534 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800535
536 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
537 goto err_allocs;
538
Peter Clifton04f90a42010-01-06 20:44:11 +0000539 /* The number of available connectors and etc may have changed with a
540 * hotplug event in between the ioctls, in which case the field is
541 * silently ignored by the kernel.
542 */
543 if (counts.count_props < conn.count_props ||
544 counts.count_modes < conn.count_modes ||
545 counts.count_encoders < conn.count_encoders) {
546 drmFree(U642VOID(conn.props_ptr));
547 drmFree(U642VOID(conn.prop_values_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200548 if (U642VOID(conn.modes_ptr) != &stack_mode)
549 drmFree(U642VOID(conn.modes_ptr));
Peter Clifton04f90a42010-01-06 20:44:11 +0000550 drmFree(U642VOID(conn.encoders_ptr));
551
552 goto retry;
553 }
554
Jesse Barnes731cd552008-12-17 10:09:49 -0800555 if(!(r = drmMalloc(sizeof(*r)))) {
556 goto err_allocs;
557 }
558
559 r->connector_id = conn.connector_id;
560 r->encoder_id = conn.encoder_id;
561 r->connection = conn.connection;
562 r->mmWidth = conn.mm_width;
563 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000564 /* convert subpixel from kernel to userspace */
565 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800566 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800567 r->count_props = conn.count_props;
568 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
569 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
570 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
571 r->count_encoders = conn.count_encoders;
572 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
573 r->connector_type = conn.connector_type;
574 r->connector_type_id = conn.connector_type_id;
575
Peter Clifton04f90a42010-01-06 20:44:11 +0000576 if ((r->count_props && !r->props) ||
577 (r->count_props && !r->prop_values) ||
578 (r->count_modes && !r->modes) ||
579 (r->count_encoders && !r->encoders)) {
580 drmFree(r->props);
581 drmFree(r->prop_values);
582 drmFree(r->modes);
583 drmFree(r->encoders);
584 drmFree(r);
585 r = 0;
586 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800587
588err_allocs:
589 drmFree(U642VOID(conn.prop_values_ptr));
590 drmFree(U642VOID(conn.props_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200591 if (U642VOID(conn.modes_ptr) != &stack_mode)
592 drmFree(U642VOID(conn.modes_ptr));
Jesse Barnes731cd552008-12-17 10:09:49 -0800593 drmFree(U642VOID(conn.encoders_ptr));
594
595 return r;
596}
597
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700598drm_public drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000599{
600 return _drmModeGetConnector(fd, connector_id, 1);
601}
602
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700603drm_public drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000604{
605 return _drmModeGetConnector(fd, connector_id, 0);
606}
607
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700608drm_public int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800609{
610 struct drm_mode_mode_cmd res;
611
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100612 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800613 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
614 res.connector_id = connector_id;
615
Chris Wilsonb8039182010-07-01 22:38:54 +0100616 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800617}
618
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700619drm_public int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800620{
621 struct drm_mode_mode_cmd res;
622
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100623 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800624 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
625 res.connector_id = connector_id;
626
Chris Wilsonb8039182010-07-01 22:38:54 +0100627 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800628}
629
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700630drm_public drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800631{
632 struct drm_mode_get_property prop;
633 drmModePropertyPtr r;
634
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100635 memclear(prop);
Jesse Barnes731cd552008-12-17 10:09:49 -0800636 prop.prop_id = property_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800637
638 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
639 return 0;
640
641 if (prop.count_values)
642 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
643
Rob Clark7b228e92012-06-05 12:28:22 -0500644 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800645 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
646
647 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
648 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
649 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
650 }
651
652 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
653 r = NULL;
654 goto err_allocs;
655 }
656
657 if (!(r = drmMalloc(sizeof(*r))))
Seung-Woo Kimb39377d2019-04-29 18:10:52 +0900658 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800659
660 r->prop_id = prop.prop_id;
661 r->count_values = prop.count_values;
662
663 r->flags = prop.flags;
664 if (prop.count_values)
665 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
Rob Clark7b228e92012-06-05 12:28:22 -0500666 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800667 r->count_enums = prop.count_enum_blobs;
668 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
669 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
670 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
671 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
672 r->count_blobs = prop.count_enum_blobs;
673 }
674 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
675 r->name[DRM_PROP_NAME_LEN-1] = 0;
676
677err_allocs:
678 drmFree(U642VOID(prop.values_ptr));
679 drmFree(U642VOID(prop.enum_blob_ptr));
680
681 return r;
682}
683
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700684drm_public void drmModeFreeProperty(drmModePropertyPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800685{
686 if (!ptr)
687 return;
688
689 drmFree(ptr->values);
690 drmFree(ptr->enums);
Boram Park7915b0a2011-10-01 09:30:09 +0900691 drmFree(ptr->blob_ids);
Jesse Barnes731cd552008-12-17 10:09:49 -0800692 drmFree(ptr);
693}
694
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700695drm_public drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd,
696 uint32_t blob_id)
Jesse Barnes731cd552008-12-17 10:09:49 -0800697{
698 struct drm_mode_get_blob blob;
699 drmModePropertyBlobPtr r;
700
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100701 memclear(blob);
Jesse Barnes731cd552008-12-17 10:09:49 -0800702 blob.blob_id = blob_id;
703
704 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
705 return NULL;
706
707 if (blob.length)
708 blob.data = VOID2U64(drmMalloc(blob.length));
709
710 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
711 r = NULL;
712 goto err_allocs;
713 }
714
715 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100716 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800717
718 r->id = blob.blob_id;
719 r->length = blob.length;
720 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
721
722err_allocs:
723 drmFree(U642VOID(blob.data));
724 return r;
725}
726
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700727drm_public void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800728{
729 if (!ptr)
730 return;
731
732 drmFree(ptr->data);
733 drmFree(ptr);
734}
735
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700736drm_public int drmModeConnectorSetProperty(int fd, uint32_t connector_id,
737 uint32_t property_id,
738 uint64_t value)
Jesse Barnes731cd552008-12-17 10:09:49 -0800739{
740 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800741
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100742 memclear(osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800743 osp.connector_id = connector_id;
744 osp.prop_id = property_id;
745 osp.value = value;
746
Chris Wilsonb8039182010-07-01 22:38:54 +0100747 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800748}
749
750/*
751 * checks if a modesetting capable driver has attached to the pci id
752 * returns 0 if modesetting supported.
753 * -EINVAL or invalid bus id
754 * -ENOSYS if no modesetting support
755*/
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700756drm_public int drmCheckModesettingSupported(const char *busid)
Jesse Barnes731cd552008-12-17 10:09:49 -0800757{
Robert Millancbb31f22014-01-23 14:46:05 +0000758#if defined (__linux__)
Jesse Barnes731cd552008-12-17 10:09:49 -0800759 char pci_dev_dir[1024];
760 int domain, bus, dev, func;
761 DIR *sysdir;
762 struct dirent *dent;
763 int found = 0, ret;
764
765 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
766 if (ret != 4)
767 return -EINVAL;
768
769 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
770 domain, bus, dev, func);
771
772 sysdir = opendir(pci_dev_dir);
773 if (sysdir) {
774 dent = readdir(sysdir);
775 while (dent) {
776 if (!strncmp(dent->d_name, "controlD", 8)) {
777 found = 1;
778 break;
779 }
780
781 dent = readdir(sysdir);
782 }
783 closedir(sysdir);
784 if (found)
785 return 0;
786 }
787
788 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
789 domain, bus, dev, func);
790
791 sysdir = opendir(pci_dev_dir);
792 if (!sysdir)
793 return -EINVAL;
794
795 dent = readdir(sysdir);
796 while (dent) {
797 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
798 found = 1;
799 break;
800 }
801
802 dent = readdir(sysdir);
803 }
804
805 closedir(sysdir);
806 if (found)
807 return 0;
Robert Millancbb31f22014-01-23 14:46:05 +0000808#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
Emmanuel Vadot24e68522020-01-21 18:41:38 +0100809 char sbusid[1024];
Robert Millancbb31f22014-01-23 14:46:05 +0000810 char oid[128];
Robert Millancbb31f22014-01-23 14:46:05 +0000811 int i, modesetting, ret;
812 size_t len;
813
Robert Millancbb31f22014-01-23 14:46:05 +0000814 /* How many GPUs do we expect in the machine ? */
Emmanuel Vadotbb584b82020-01-24 20:43:05 +0100815 for (i = 0; i < 10; i++) {
Robert Millancbb31f22014-01-23 14:46:05 +0000816 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
817 len = sizeof(sbusid);
818 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
819 if (ret == -1) {
820 if (errno == ENOENT)
821 continue;
822 return -EINVAL;
823 }
Emmanuel Vadot24e68522020-01-21 18:41:38 +0100824 if (strcmp(sbusid, busid) != 0)
Robert Millancbb31f22014-01-23 14:46:05 +0000825 continue;
826 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
827 len = sizeof(modesetting);
828 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
829 if (ret == -1 || len != sizeof(modesetting))
830 return -EINVAL;
831 return (modesetting ? 0 : -ENOSYS);
832 }
François Tigeotedbb4e52014-07-26 13:39:58 +0200833#elif defined(__DragonFly__)
834 return 0;
Eric Engestrom00aa3742018-03-22 17:08:37 +0000835#elif defined(__OpenBSD__)
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000836 int fd;
837 struct drm_mode_card_res res;
838 drmModeResPtr r = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800839
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000840 if ((fd = drmOpen(NULL, busid)) < 0)
841 return -EINVAL;
842
843 memset(&res, 0, sizeof(struct drm_mode_card_res));
844
845 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
846 drmClose(fd);
847 return -errno;
848 }
849
850 drmClose(fd);
851 return 0;
852#endif
853 return -ENOSYS;
Jesse Barnes731cd552008-12-17 10:09:49 -0800854}
855
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700856drm_public int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
857 uint16_t *red, uint16_t *green,
858 uint16_t *blue)
Jesse Barnes731cd552008-12-17 10:09:49 -0800859{
Jesse Barnes731cd552008-12-17 10:09:49 -0800860 struct drm_mode_crtc_lut l;
861
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100862 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800863 l.crtc_id = crtc_id;
864 l.gamma_size = size;
865 l.red = VOID2U64(red);
866 l.green = VOID2U64(green);
867 l.blue = VOID2U64(blue);
868
Chris Wilsonb8039182010-07-01 22:38:54 +0100869 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800870}
871
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700872drm_public int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
873 uint16_t *red, uint16_t *green,
874 uint16_t *blue)
Jesse Barnes731cd552008-12-17 10:09:49 -0800875{
Jesse Barnes731cd552008-12-17 10:09:49 -0800876 struct drm_mode_crtc_lut l;
877
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100878 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800879 l.crtc_id = crtc_id;
880 l.gamma_size = size;
881 l.red = VOID2U64(red);
882 l.green = VOID2U64(green);
883 l.blue = VOID2U64(blue);
884
Chris Wilsonb8039182010-07-01 22:38:54 +0100885 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800886}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400887
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700888drm_public int drmHandleEvent(int fd, drmEventContextPtr evctx)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400889{
890 char buffer[1024];
891 int len, i;
892 struct drm_event *e;
893 struct drm_event_vblank *vblank;
Keith Packardd4331dd2017-07-01 00:43:15 -0700894 struct drm_event_crtc_sequence *seq;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300895 void *user_data;
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900896
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400897 /* The DRM read semantics guarantees that we always get only
898 * complete events. */
899
900 len = read(fd, buffer, sizeof buffer);
901 if (len == 0)
902 return 0;
Jan Veselyde8532d2014-11-30 12:53:18 -0500903 if (len < (int)sizeof *e)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400904 return -1;
905
906 i = 0;
907 while (i < len) {
Thierry Redingecc2a092015-04-13 11:36:59 +0200908 e = (struct drm_event *)(buffer + i);
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400909 switch (e->type) {
910 case DRM_EVENT_VBLANK:
911 if (evctx->version < 1 ||
912 evctx->vblank_handler == NULL)
913 break;
914 vblank = (struct drm_event_vblank *) e;
915 evctx->vblank_handler(fd,
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900916 vblank->sequence,
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400917 vblank->tv_sec,
918 vblank->tv_usec,
919 U642VOID (vblank->user_data));
920 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500921 case DRM_EVENT_FLIP_COMPLETE:
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500922 vblank = (struct drm_event_vblank *) e;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300923 user_data = U642VOID (vblank->user_data);
924
925 if (evctx->version >= 3 && evctx->page_flip_handler2)
926 evctx->page_flip_handler2(fd,
927 vblank->sequence,
928 vblank->tv_sec,
929 vblank->tv_usec,
930 vblank->crtc_id,
931 user_data);
932 else if (evctx->version >= 2 && evctx->page_flip_handler)
933 evctx->page_flip_handler(fd,
934 vblank->sequence,
935 vblank->tv_sec,
936 vblank->tv_usec,
937 user_data);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500938 break;
Keith Packardd4331dd2017-07-01 00:43:15 -0700939 case DRM_EVENT_CRTC_SEQUENCE:
940 seq = (struct drm_event_crtc_sequence *) e;
941 if (evctx->version >= 4 && evctx->sequence_handler)
942 evctx->sequence_handler(fd,
943 seq->sequence,
944 seq->time_ns,
945 seq->user_data);
946 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400947 default:
948 break;
949 }
950 i += e->length;
951 }
952
953 return 0;
954}
955
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700956drm_public int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500957 uint32_t flags, void *user_data)
958{
959 struct drm_mode_crtc_page_flip flip;
960
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100961 memclear(flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500962 flip.fb_id = fb_id;
963 flip.crtc_id = crtc_id;
964 flip.user_data = VOID2U64(user_data);
965 flip.flags = flags;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500966
Chris Wilsonb8039182010-07-01 22:38:54 +0100967 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500968}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700969
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700970drm_public int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
Michel Dänzer7dd28472016-06-29 18:07:25 +0900971 uint32_t flags, void *user_data,
972 uint32_t target_vblank)
973{
974 struct drm_mode_crtc_page_flip_target flip_target;
975
976 memclear(flip_target);
977 flip_target.fb_id = fb_id;
978 flip_target.crtc_id = crtc_id;
979 flip_target.user_data = VOID2U64(user_data);
980 flip_target.flags = flags;
981 flip_target.sequence = target_vblank;
982
983 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
984}
985
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700986drm_public int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700987 uint32_t fb_id, uint32_t flags,
Daniel Kurtz828c3e82014-05-01 19:56:43 +0800988 int32_t crtc_x, int32_t crtc_y,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700989 uint32_t crtc_w, uint32_t crtc_h,
990 uint32_t src_x, uint32_t src_y,
991 uint32_t src_w, uint32_t src_h)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700992{
993 struct drm_mode_set_plane s;
994
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100995 memclear(s);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700996 s.plane_id = plane_id;
997 s.crtc_id = crtc_id;
998 s.fb_id = fb_id;
999 s.flags = flags;
1000 s.crtc_x = crtc_x;
1001 s.crtc_y = crtc_y;
1002 s.crtc_w = crtc_w;
1003 s.crtc_h = crtc_h;
1004 s.src_x = src_x;
1005 s.src_y = src_y;
1006 s.src_w = src_w;
1007 s.src_h = src_h;
1008
1009 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1010}
1011
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001012drm_public drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001013{
1014 struct drm_mode_get_plane ovr, counts;
1015 drmModePlanePtr r = 0;
1016
1017retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001018 memclear(ovr);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001019 ovr.plane_id = plane_id;
1020 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1021 return 0;
1022
1023 counts = ovr;
1024
1025 if (ovr.count_format_types) {
1026 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1027 sizeof(uint32_t)));
1028 if (!ovr.format_type_ptr)
1029 goto err_allocs;
1030 }
1031
1032 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1033 goto err_allocs;
1034
1035 if (counts.count_format_types < ovr.count_format_types) {
1036 drmFree(U642VOID(ovr.format_type_ptr));
1037 goto retry;
1038 }
1039
1040 if (!(r = drmMalloc(sizeof(*r))))
1041 goto err_allocs;
1042
1043 r->count_formats = ovr.count_format_types;
1044 r->plane_id = ovr.plane_id;
1045 r->crtc_id = ovr.crtc_id;
1046 r->fb_id = ovr.fb_id;
1047 r->possible_crtcs = ovr.possible_crtcs;
1048 r->gamma_size = ovr.gamma_size;
1049 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1050 ovr.count_format_types, sizeof(uint32_t));
1051 if (ovr.count_format_types && !r->formats) {
1052 drmFree(r->formats);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001053 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001054 r = 0;
1055 }
1056
1057err_allocs:
1058 drmFree(U642VOID(ovr.format_type_ptr));
1059
1060 return r;
1061}
1062
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001063drm_public void drmModeFreePlane(drmModePlanePtr ptr)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001064{
1065 if (!ptr)
1066 return;
1067
1068 drmFree(ptr->formats);
1069 drmFree(ptr);
1070}
1071
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001072drm_public drmModePlaneResPtr drmModeGetPlaneResources(int fd)
Jesse Barnesac168bf2011-04-29 08:53:53 -07001073{
1074 struct drm_mode_get_plane_res res, counts;
1075 drmModePlaneResPtr r = 0;
1076
1077retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001078 memclear(res);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001079 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1080 return 0;
1081
1082 counts = res;
1083
1084 if (res.count_planes) {
1085 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1086 sizeof(uint32_t)));
1087 if (!res.plane_id_ptr)
1088 goto err_allocs;
1089 }
1090
1091 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1092 goto err_allocs;
1093
1094 if (counts.count_planes < res.count_planes) {
1095 drmFree(U642VOID(res.plane_id_ptr));
1096 goto retry;
1097 }
1098
1099 if (!(r = drmMalloc(sizeof(*r))))
1100 goto err_allocs;
1101
1102 r->count_planes = res.count_planes;
1103 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1104 res.count_planes, sizeof(uint32_t));
1105 if (res.count_planes && !r->planes) {
1106 drmFree(r->planes);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001107 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001108 r = 0;
1109 }
1110
1111err_allocs:
1112 drmFree(U642VOID(res.plane_id_ptr));
1113
1114 return r;
1115}
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001116
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001117drm_public void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001118{
1119 if (!ptr)
1120 return;
1121
1122 drmFree(ptr->planes);
1123 drmFree(ptr);
1124}
Paulo Zanoni8c757032012-05-15 18:38:28 -03001125
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001126drm_public drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
Paulo Zanoni8c757032012-05-15 18:38:28 -03001127 uint32_t object_id,
1128 uint32_t object_type)
1129{
1130 struct drm_mode_obj_get_properties properties;
1131 drmModeObjectPropertiesPtr ret = NULL;
1132 uint32_t count;
1133
1134retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001135 memclear(properties);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001136 properties.obj_id = object_id;
1137 properties.obj_type = object_type;
1138
1139 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1140 return 0;
1141
1142 count = properties.count_props;
1143
1144 if (count) {
1145 properties.props_ptr = VOID2U64(drmMalloc(count *
1146 sizeof(uint32_t)));
1147 if (!properties.props_ptr)
1148 goto err_allocs;
1149 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1150 sizeof(uint64_t)));
1151 if (!properties.prop_values_ptr)
1152 goto err_allocs;
1153 }
1154
1155 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1156 goto err_allocs;
1157
1158 if (count < properties.count_props) {
1159 drmFree(U642VOID(properties.props_ptr));
1160 drmFree(U642VOID(properties.prop_values_ptr));
1161 goto retry;
1162 }
1163 count = properties.count_props;
1164
1165 ret = drmMalloc(sizeof(*ret));
1166 if (!ret)
1167 goto err_allocs;
1168
1169 ret->count_props = count;
1170 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1171 count, sizeof(uint32_t));
1172 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1173 count, sizeof(uint64_t));
1174 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1175 drmFree(ret->props);
1176 drmFree(ret->prop_values);
1177 drmFree(ret);
1178 ret = NULL;
1179 }
1180
1181err_allocs:
1182 drmFree(U642VOID(properties.props_ptr));
1183 drmFree(U642VOID(properties.prop_values_ptr));
1184 return ret;
1185}
1186
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001187drm_public void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
Paulo Zanoni8c757032012-05-15 18:38:28 -03001188{
1189 if (!ptr)
1190 return;
1191 drmFree(ptr->props);
1192 drmFree(ptr->prop_values);
1193 drmFree(ptr);
1194}
1195
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001196drm_public int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
Paulo Zanoni8c757032012-05-15 18:38:28 -03001197 uint32_t property_id, uint64_t value)
1198{
1199 struct drm_mode_obj_set_property prop;
1200
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001201 memclear(prop);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001202 prop.value = value;
1203 prop.prop_id = property_id;
1204 prop.obj_id = object_id;
1205 prop.obj_type = object_type;
1206
1207 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1208}
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001209
1210typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1211
1212struct _drmModeAtomicReqItem {
1213 uint32_t object_id;
1214 uint32_t property_id;
1215 uint64_t value;
1216};
1217
1218struct _drmModeAtomicReq {
1219 uint32_t cursor;
1220 uint32_t size_items;
1221 drmModeAtomicReqItemPtr items;
1222};
1223
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001224drm_public drmModeAtomicReqPtr drmModeAtomicAlloc(void)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001225{
1226 drmModeAtomicReqPtr req;
1227
1228 req = drmMalloc(sizeof *req);
1229 if (!req)
1230 return NULL;
1231
1232 req->items = NULL;
1233 req->cursor = 0;
1234 req->size_items = 0;
1235
1236 return req;
1237}
1238
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001239drm_public drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001240{
1241 drmModeAtomicReqPtr new;
1242
Emil Velikoved3c6652015-09-05 17:20:53 +01001243 if (!old)
1244 return NULL;
1245
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001246 new = drmMalloc(sizeof *new);
1247 if (!new)
1248 return NULL;
1249
1250 new->cursor = old->cursor;
1251 new->size_items = old->size_items;
1252
1253 if (old->size_items) {
1254 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1255 if (!new->items) {
1256 free(new);
1257 return NULL;
1258 }
1259 memcpy(new->items, old->items,
Adrian Salido763f6462019-04-24 10:08:40 -07001260 old->cursor * sizeof(*new->items));
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001261 } else {
1262 new->items = NULL;
1263 }
1264
1265 return new;
1266}
1267
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001268drm_public int drmModeAtomicMerge(drmModeAtomicReqPtr base,
1269 drmModeAtomicReqPtr augment)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001270{
Emil Velikoved3c6652015-09-05 17:20:53 +01001271 if (!base)
1272 return -EINVAL;
1273
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001274 if (!augment || augment->cursor == 0)
1275 return 0;
1276
1277 if (base->cursor + augment->cursor >= base->size_items) {
1278 drmModeAtomicReqItemPtr new;
1279 int saved_size = base->size_items;
1280
1281 base->size_items = base->cursor + augment->cursor;
1282 new = realloc(base->items,
1283 base->size_items * sizeof(*base->items));
1284 if (!new) {
1285 base->size_items = saved_size;
1286 return -ENOMEM;
1287 }
1288 base->items = new;
1289 }
1290
1291 memcpy(&base->items[base->cursor], augment->items,
1292 augment->cursor * sizeof(*augment->items));
1293 base->cursor += augment->cursor;
1294
1295 return 0;
1296}
1297
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001298drm_public int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001299{
Emil Velikoved3c6652015-09-05 17:20:53 +01001300 if (!req)
1301 return -EINVAL;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001302 return req->cursor;
1303}
1304
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001305drm_public void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001306{
Emil Velikoved3c6652015-09-05 17:20:53 +01001307 if (req)
1308 req->cursor = cursor;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001309}
1310
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001311drm_public int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1312 uint32_t object_id,
1313 uint32_t property_id,
1314 uint64_t value)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001315{
Emil Velikoved3c6652015-09-05 17:20:53 +01001316 if (!req)
1317 return -EINVAL;
1318
Daniel Stone45eee3f2018-03-07 12:41:12 +00001319 if (object_id == 0 || property_id == 0)
1320 return -EINVAL;
1321
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001322 if (req->cursor >= req->size_items) {
Adrian Salido763f6462019-04-24 10:08:40 -07001323 const uint32_t item_size_inc = getpagesize() / sizeof(*req->items);
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001324 drmModeAtomicReqItemPtr new;
1325
Adrian Salido763f6462019-04-24 10:08:40 -07001326 req->size_items += item_size_inc;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001327 new = realloc(req->items, req->size_items * sizeof(*req->items));
1328 if (!new) {
Adrian Salido763f6462019-04-24 10:08:40 -07001329 req->size_items -= item_size_inc;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001330 return -ENOMEM;
1331 }
1332 req->items = new;
1333 }
1334
1335 req->items[req->cursor].object_id = object_id;
1336 req->items[req->cursor].property_id = property_id;
1337 req->items[req->cursor].value = value;
1338 req->cursor++;
1339
1340 return req->cursor;
1341}
1342
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001343drm_public void drmModeAtomicFree(drmModeAtomicReqPtr req)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001344{
1345 if (!req)
1346 return;
1347
1348 if (req->items)
1349 drmFree(req->items);
1350 drmFree(req);
1351}
1352
1353static int sort_req_list(const void *misc, const void *other)
1354{
1355 const drmModeAtomicReqItem *first = misc;
1356 const drmModeAtomicReqItem *second = other;
1357
1358 if (first->object_id < second->object_id)
1359 return -1;
1360 else if (first->object_id > second->object_id)
1361 return 1;
1362 else
1363 return second->property_id - first->property_id;
1364}
1365
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001366drm_public int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req,
1367 uint32_t flags, void *user_data)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001368{
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001369 drmModeAtomicReqPtr sorted;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001370 struct drm_mode_atomic atomic;
1371 uint32_t *objs_ptr = NULL;
1372 uint32_t *count_props_ptr = NULL;
1373 uint32_t *props_ptr = NULL;
1374 uint64_t *prop_values_ptr = NULL;
1375 uint32_t last_obj_id = 0;
1376 uint32_t i;
1377 int obj_idx = -1;
1378 int ret = -1;
1379
Emil Velikoved3c6652015-09-05 17:20:53 +01001380 if (!req)
1381 return -EINVAL;
1382
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001383 if (req->cursor == 0)
1384 return 0;
1385
1386 sorted = drmModeAtomicDuplicate(req);
1387 if (sorted == NULL)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001388 return -ENOMEM;
1389
1390 memclear(atomic);
1391
1392 /* Sort the list by object ID, then by property ID. */
1393 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1394 sort_req_list);
1395
1396 /* Now the list is sorted, eliminate duplicate property sets. */
1397 for (i = 0; i < sorted->cursor; i++) {
1398 if (sorted->items[i].object_id != last_obj_id) {
1399 atomic.count_objs++;
1400 last_obj_id = sorted->items[i].object_id;
1401 }
1402
1403 if (i == sorted->cursor - 1)
1404 continue;
1405
1406 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1407 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1408 continue;
1409
1410 memmove(&sorted->items[i], &sorted->items[i + 1],
1411 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1412 sorted->cursor--;
1413 }
1414
1415 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1416 if (!objs_ptr) {
1417 errno = ENOMEM;
1418 goto out;
1419 }
1420
1421 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1422 if (!count_props_ptr) {
1423 errno = ENOMEM;
1424 goto out;
1425 }
1426
1427 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1428 if (!props_ptr) {
1429 errno = ENOMEM;
1430 goto out;
1431 }
1432
1433 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1434 if (!prop_values_ptr) {
1435 errno = ENOMEM;
1436 goto out;
1437 }
1438
1439 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1440 if (sorted->items[i].object_id != last_obj_id) {
1441 obj_idx++;
1442 objs_ptr[obj_idx] = sorted->items[i].object_id;
1443 last_obj_id = objs_ptr[obj_idx];
1444 }
1445
1446 count_props_ptr[obj_idx]++;
1447 props_ptr[i] = sorted->items[i].property_id;
1448 prop_values_ptr[i] = sorted->items[i].value;
1449
1450 }
1451
1452 atomic.flags = flags;
1453 atomic.objs_ptr = VOID2U64(objs_ptr);
1454 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1455 atomic.props_ptr = VOID2U64(props_ptr);
1456 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1457 atomic.user_data = VOID2U64(user_data);
1458
1459 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1460
1461out:
1462 drmFree(objs_ptr);
1463 drmFree(count_props_ptr);
1464 drmFree(props_ptr);
1465 drmFree(prop_values_ptr);
1466 drmModeAtomicFree(sorted);
1467
1468 return ret;
1469}
Daniel Stone32471b22015-06-22 17:26:03 +01001470
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001471drm_public int
1472drmModeCreatePropertyBlob(int fd, const void *data, size_t length,
1473 uint32_t *id)
Daniel Stone32471b22015-06-22 17:26:03 +01001474{
1475 struct drm_mode_create_blob create;
1476 int ret;
1477
1478 if (length >= 0xffffffff)
1479 return -ERANGE;
1480
1481 memclear(create);
1482
1483 create.length = length;
1484 create.data = (uintptr_t) data;
1485 create.blob_id = 0;
1486 *id = 0;
1487
1488 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1489 if (ret != 0)
1490 return ret;
1491
1492 *id = create.blob_id;
1493 return 0;
1494}
1495
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001496drm_public int
Daniel Stone32471b22015-06-22 17:26:03 +01001497drmModeDestroyPropertyBlob(int fd, uint32_t id)
1498{
1499 struct drm_mode_destroy_blob destroy;
1500
1501 memclear(destroy);
1502 destroy.blob_id = id;
1503 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1504}
Keith Packardc4171532017-03-16 18:11:05 -07001505
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001506drm_public int
1507drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags,
1508 uint32_t *lessee_id)
Keith Packardc4171532017-03-16 18:11:05 -07001509{
1510 struct drm_mode_create_lease create;
1511 int ret;
1512
1513 memclear(create);
1514 create.object_ids = (uintptr_t) objects;
1515 create.object_count = num_objects;
1516 create.flags = flags;
1517
1518 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1519 if (ret == 0) {
1520 *lessee_id = create.lessee_id;
1521 return create.fd;
1522 }
1523 return -errno;
1524}
1525
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001526drm_public drmModeLesseeListPtr
Keith Packardc4171532017-03-16 18:11:05 -07001527drmModeListLessees(int fd)
1528{
1529 struct drm_mode_list_lessees list;
1530 uint32_t count;
1531 drmModeLesseeListPtr ret;
1532
1533 memclear(list);
1534
1535 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1536 return NULL;
1537
1538 count = list.count_lessees;
1539 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1540 if (!ret)
1541 return NULL;
1542
1543 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1544 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1545 drmFree(ret);
1546 return NULL;
1547 }
1548
1549 ret->count = count;
1550 return ret;
1551}
1552
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001553drm_public drmModeObjectListPtr
Keith Packardc4171532017-03-16 18:11:05 -07001554drmModeGetLease(int fd)
1555{
1556 struct drm_mode_get_lease get;
1557 uint32_t count;
1558 drmModeObjectListPtr ret;
1559
1560 memclear(get);
1561
1562 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1563 return NULL;
1564
1565 count = get.count_objects;
1566 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1567 if (!ret)
1568 return NULL;
1569
1570 get.objects_ptr = VOID2U64(&ret->objects[0]);
1571 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1572 drmFree(ret);
1573 return NULL;
1574 }
1575
1576 ret->count = count;
1577 return ret;
1578}
1579
Lucas De Marchi26f9ce52018-09-13 15:42:26 -07001580drm_public int
Keith Packardc4171532017-03-16 18:11:05 -07001581drmModeRevokeLease(int fd, uint32_t lessee_id)
1582{
1583 struct drm_mode_revoke_lease revoke;
1584 int ret;
1585
1586 memclear(revoke);
1587
1588 revoke.lessee_id = lessee_id;
1589
1590 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
1591 if (ret == 0)
1592 return 0;
1593 return -errno;
1594}
Daniel Stoned8731e92020-02-11 12:19:16 -08001595
1596drm_public drmModeFB2Ptr
1597drmModeGetFB2(int fd, uint32_t fb_id)
1598{
1599 struct drm_mode_fb_cmd2 get = {
1600 .fb_id = fb_id,
1601 };
1602 drmModeFB2Ptr ret;
1603 int err;
1604
1605 err = DRM_IOCTL(fd, DRM_IOCTL_MODE_GETFB2, &get);
1606 if (err != 0)
1607 return NULL;
1608
1609 ret = drmMalloc(sizeof(drmModeFB2));
1610 if (!ret)
1611 return NULL;
1612
1613 ret->fb_id = fb_id;
1614 ret->width = get.width;
1615 ret->height = get.height;
1616 ret->pixel_format = get.pixel_format;
1617 ret->flags = get.flags;
1618 ret->modifier = get.modifier[0];
1619 memcpy(ret->handles, get.handles, sizeof(uint32_t) * 4);
1620 memcpy(ret->pitches, get.pitches, sizeof(uint32_t) * 4);
1621 memcpy(ret->offsets, get.offsets, sizeof(uint32_t) * 4);
1622
1623 return ret;
1624}
1625
1626drm_public void drmModeFreeFB2(drmModeFB2Ptr ptr)
1627{
1628 drmFree(ptr);
1629}