blob: bd59ef258889f94db1b875e81621876529b95984 [file] [log] [blame]
Jesse Barnes731cd552008-12-17 10:09:49 -08001/*
2 * \file xf86drmMode.c
3 * Header for DRM modesetting interface.
4 *
5 * \author Jakob Bornecrantz <wallbraker@gmail.com>
6 *
7 * \par Acknowledgements:
8 * Feb 2007, Dave Airlie <airlied@linux.ie>
9 */
10
11/*
12 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
13 * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
14 * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 *
34 */
35
36/*
Eric Engestromce975072016-04-03 19:48:12 +010037 * TODO the types we are after are defined in different headers on different
Jesse Barnes731cd552008-12-17 10:09:49 -080038 * platforms find which headers to include to get uint32_t
39 */
Emil Velikov5f762732015-06-29 17:32:21 +010040
41#ifdef HAVE_CONFIG_H
42#include "config.h"
43#endif
44
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010045#include <limits.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080046#include <stdint.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010047#include <stdlib.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080048#include <sys/ioctl.h>
Julien Cristaufc8c3e22015-07-06 12:45:31 +010049#ifdef HAVE_SYS_SYSCTL_H
50#include <sys/sysctl.h>
51#endif
Jesse Barnes731cd552008-12-17 10:09:49 -080052#include <stdio.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010053#include <stdbool.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080054
55#include "xf86drmMode.h"
56#include "xf86drm.h"
57#include <drm.h>
58#include <string.h>
59#include <dirent.h>
Kristian Høgsbergb0b96632009-09-11 13:27:35 -040060#include <unistd.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080061#include <errno.h>
62
Daniel Vetter7e0460c2015-02-11 12:03:12 +010063#define memclear(s) memset(&s, 0, sizeof(s))
Eric Anholt734de702013-12-28 22:06:51 -080064
Jesse Barnes731cd552008-12-17 10:09:49 -080065#define U642VOID(x) ((void *)(unsigned long)(x))
66#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
67
Marcin Slusarz763b6182011-06-05 18:53:16 +020068static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
Chris Wilsonb8039182010-07-01 22:38:54 +010069{
70 int ret = drmIoctl(fd, cmd, arg);
71 return ret < 0 ? -errno : ret;
72}
73
Jesse Barnes731cd552008-12-17 10:09:49 -080074/*
75 * Util functions
76 */
77
Jan Vesely65041c42015-02-27 11:51:05 -050078static void* drmAllocCpy(char *array, int count, int entry_size)
Jesse Barnes731cd552008-12-17 10:09:49 -080079{
80 char *r;
81 int i;
82
83 if (!count || !array || !entry_size)
84 return 0;
85
86 if (!(r = drmMalloc(count*entry_size)))
87 return 0;
88
89 for (i = 0; i < count; i++)
90 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
91
92 return r;
93}
94
95/*
96 * A couple of free functions.
97 */
98
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +010099void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -0800100{
101 if (!ptr)
102 return;
103
104 drmFree(ptr);
105}
106
107void drmModeFreeResources(drmModeResPtr ptr)
108{
109 if (!ptr)
110 return;
111
Ville Syrjälädf497e92012-02-02 14:53:39 -0500112 drmFree(ptr->fbs);
113 drmFree(ptr->crtcs);
114 drmFree(ptr->connectors);
115 drmFree(ptr->encoders);
Jesse Barnes731cd552008-12-17 10:09:49 -0800116 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800117}
118
119void drmModeFreeFB(drmModeFBPtr ptr)
120{
121 if (!ptr)
122 return;
123
124 /* we might add more frees later. */
125 drmFree(ptr);
126}
127
128void drmModeFreeCrtc(drmModeCrtcPtr ptr)
129{
130 if (!ptr)
131 return;
132
133 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800134}
135
136void drmModeFreeConnector(drmModeConnectorPtr ptr)
137{
138 if (!ptr)
139 return;
140
Keith Packard0a246542009-09-17 17:28:08 -0700141 drmFree(ptr->encoders);
142 drmFree(ptr->prop_values);
143 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800144 drmFree(ptr->modes);
145 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800146}
147
148void drmModeFreeEncoder(drmModeEncoderPtr ptr)
149{
150 drmFree(ptr);
151}
152
153/*
154 * ModeSetting functions.
155 */
156
157drmModeResPtr drmModeGetResources(int fd)
158{
Chris Wilsond1308f42010-01-06 15:19:25 +0000159 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800160 drmModeResPtr r = 0;
161
Chris Wilsond1308f42010-01-06 15:19:25 +0000162retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100163 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800164 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
165 return 0;
166
Chris Wilsond1308f42010-01-06 15:19:25 +0000167 counts = res;
168
Chris Wilson85fb3e52010-01-06 15:39:49 +0000169 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800170 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000171 if (!res.fb_id_ptr)
172 goto err_allocs;
173 }
174 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800175 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000176 if (!res.crtc_id_ptr)
177 goto err_allocs;
178 }
179 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800180 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000181 if (!res.connector_id_ptr)
182 goto err_allocs;
183 }
184 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800185 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000186 if (!res.encoder_id_ptr)
187 goto err_allocs;
188 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800189
Chris Wilsond1308f42010-01-06 15:19:25 +0000190 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800191 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000192
193 /* The number of available connectors and etc may have changed with a
194 * hotplug event in between the ioctls, in which case the field is
195 * silently ignored by the kernel.
196 */
197 if (counts.count_fbs < res.count_fbs ||
198 counts.count_crtcs < res.count_crtcs ||
199 counts.count_connectors < res.count_connectors ||
200 counts.count_encoders < res.count_encoders)
201 {
202 drmFree(U642VOID(res.fb_id_ptr));
203 drmFree(U642VOID(res.crtc_id_ptr));
204 drmFree(U642VOID(res.connector_id_ptr));
205 drmFree(U642VOID(res.encoder_id_ptr));
206
207 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800208 }
209
210 /*
211 * return
212 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800213 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000214 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800215
216 r->min_width = res.min_width;
217 r->max_width = res.max_width;
218 r->min_height = res.min_height;
219 r->max_height = res.max_height;
220 r->count_fbs = res.count_fbs;
221 r->count_crtcs = res.count_crtcs;
222 r->count_connectors = res.count_connectors;
223 r->count_encoders = res.count_encoders;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000224
225 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
226 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
227 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
228 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000229 if ((res.count_fbs && !r->fbs) ||
230 (res.count_crtcs && !r->crtcs) ||
231 (res.count_connectors && !r->connectors) ||
232 (res.count_encoders && !r->encoders))
233 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000234 drmFree(r->fbs);
235 drmFree(r->crtcs);
236 drmFree(r->connectors);
237 drmFree(r->encoders);
238 drmFree(r);
239 r = 0;
240 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800241
242err_allocs:
243 drmFree(U642VOID(res.fb_id_ptr));
244 drmFree(U642VOID(res.crtc_id_ptr));
245 drmFree(U642VOID(res.connector_id_ptr));
246 drmFree(U642VOID(res.encoder_id_ptr));
247
248 return r;
249}
250
251int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200252 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
Jesse Barnes731cd552008-12-17 10:09:49 -0800253 uint32_t *buf_id)
254{
255 struct drm_mode_fb_cmd f;
256 int ret;
257
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100258 memclear(f);
Jesse Barnes731cd552008-12-17 10:09:49 -0800259 f.width = width;
260 f.height = height;
261 f.pitch = pitch;
262 f.bpp = bpp;
263 f.depth = depth;
264 f.handle = bo_handle;
265
Chris Wilsonb8039182010-07-01 22:38:54 +0100266 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800267 return ret;
268
269 *buf_id = f.fb_id;
270 return 0;
271}
272
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700273int drmModeAddFB2WithModifiers(int fd, uint32_t width, uint32_t height,
Tobias Jakobi09be5412017-10-10 12:12:52 +0200274 uint32_t pixel_format, const uint32_t bo_handles[4],
275 const uint32_t pitches[4], const uint32_t offsets[4],
276 const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700277{
278 struct drm_mode_fb_cmd2 f;
279 int ret;
280
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100281 memclear(f);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700282 f.width = width;
283 f.height = height;
284 f.pixel_format = pixel_format;
285 f.flags = flags;
Ville Syrjälä76b4a692012-02-02 14:53:43 -0500286 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
287 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
288 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700289 if (modifier)
290 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
Jesse Barnesac168bf2011-04-29 08:53:53 -0700291
292 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
293 return ret;
294
295 *buf_id = f.fb_id;
296 return 0;
297}
298
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700299int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
Tobias Jakobi09be5412017-10-10 12:12:52 +0200300 uint32_t pixel_format, const uint32_t bo_handles[4],
301 const uint32_t pitches[4], const uint32_t offsets[4],
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700302 uint32_t *buf_id, uint32_t flags)
303{
304 return drmModeAddFB2WithModifiers(fd, width, height,
305 pixel_format, bo_handles,
306 pitches, offsets, NULL,
307 buf_id, flags);
308}
309
Jesse Barnes731cd552008-12-17 10:09:49 -0800310int drmModeRmFB(int fd, uint32_t bufferId)
311{
Chris Wilsonb8039182010-07-01 22:38:54 +0100312 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800313}
314
315drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
316{
317 struct drm_mode_fb_cmd info;
318 drmModeFBPtr r;
319
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100320 memclear(info);
Jesse Barnes731cd552008-12-17 10:09:49 -0800321 info.fb_id = buf;
322
323 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
324 return NULL;
325
326 if (!(r = drmMalloc(sizeof(*r))))
327 return NULL;
328
329 r->fb_id = info.fb_id;
330 r->width = info.width;
331 r->height = info.height;
332 r->pitch = info.pitch;
333 r->bpp = info.bpp;
334 r->handle = info.handle;
335 r->depth = info.depth;
336
337 return r;
338}
339
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100340int drmModeDirtyFB(int fd, uint32_t bufferId,
341 drmModeClipPtr clips, uint32_t num_clips)
342{
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100343 struct drm_mode_fb_dirty_cmd dirty;
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100344
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100345 memclear(dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100346 dirty.fb_id = bufferId;
347 dirty.clips_ptr = VOID2U64(clips);
348 dirty.num_clips = num_clips;
349
Chris Wilsonb8039182010-07-01 22:38:54 +0100350 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100351}
352
Jesse Barnes731cd552008-12-17 10:09:49 -0800353/*
354 * Crtc functions
355 */
356
357drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
358{
359 struct drm_mode_crtc crtc;
360 drmModeCrtcPtr r;
361
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100362 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800363 crtc.crtc_id = crtcId;
364
365 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
366 return 0;
367
368 /*
369 * return
370 */
371
372 if (!(r = drmMalloc(sizeof(*r))))
373 return 0;
374
375 r->crtc_id = crtc.crtc_id;
376 r->x = crtc.x;
377 r->y = crtc.y;
378 r->mode_valid = crtc.mode_valid;
Rob Clarke81acf52012-10-14 16:55:32 -0500379 if (r->mode_valid) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800380 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
Rob Clarke81acf52012-10-14 16:55:32 -0500381 r->width = crtc.mode.hdisplay;
382 r->height = crtc.mode.vdisplay;
383 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800384 r->buffer_id = crtc.fb_id;
385 r->gamma_size = crtc.gamma_size;
386 return r;
387}
388
Jesse Barnes731cd552008-12-17 10:09:49 -0800389int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200390 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100391 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800392{
393 struct drm_mode_crtc crtc;
394
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100395 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800396 crtc.x = x;
397 crtc.y = y;
398 crtc.crtc_id = crtcId;
399 crtc.fb_id = bufferId;
400 crtc.set_connectors_ptr = VOID2U64(connectors);
401 crtc.count_connectors = count;
402 if (mode) {
403 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
404 crtc.mode_valid = 1;
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100405 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800406
Chris Wilsonb8039182010-07-01 22:38:54 +0100407 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800408}
409
410/*
411 * Cursor manipulation
412 */
413
414int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
415{
416 struct drm_mode_cursor arg;
417
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100418 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800419 arg.flags = DRM_MODE_CURSOR_BO;
420 arg.crtc_id = crtcId;
421 arg.width = width;
422 arg.height = height;
423 arg.handle = bo_handle;
424
Chris Wilsonb8039182010-07-01 22:38:54 +0100425 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800426}
427
Dave Airlie2e0ab622013-07-02 09:21:06 +0100428int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height, int32_t hot_x, int32_t hot_y)
429{
430 struct drm_mode_cursor2 arg;
431
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100432 memclear(arg);
Dave Airlie2e0ab622013-07-02 09:21:06 +0100433 arg.flags = DRM_MODE_CURSOR_BO;
434 arg.crtc_id = crtcId;
435 arg.width = width;
436 arg.height = height;
437 arg.handle = bo_handle;
438 arg.hot_x = hot_x;
439 arg.hot_y = hot_y;
440
441 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
442}
443
Jesse Barnes731cd552008-12-17 10:09:49 -0800444int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
445{
446 struct drm_mode_cursor arg;
447
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100448 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800449 arg.flags = DRM_MODE_CURSOR_MOVE;
450 arg.crtc_id = crtcId;
451 arg.x = x;
452 arg.y = y;
453
Chris Wilsonb8039182010-07-01 22:38:54 +0100454 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800455}
456
457/*
458 * Encoder get
459 */
460drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
461{
462 struct drm_mode_get_encoder enc;
463 drmModeEncoderPtr r = NULL;
464
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100465 memclear(enc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800466 enc.encoder_id = encoder_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800467
468 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
469 return 0;
470
471 if (!(r = drmMalloc(sizeof(*r))))
472 return 0;
473
474 r->encoder_id = enc.encoder_id;
475 r->crtc_id = enc.crtc_id;
476 r->encoder_type = enc.encoder_type;
477 r->possible_crtcs = enc.possible_crtcs;
478 r->possible_clones = enc.possible_clones;
479
480 return r;
481}
482
483/*
484 * Connector manipulation
485 */
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000486static drmModeConnectorPtr
487_drmModeGetConnector(int fd, uint32_t connector_id, int probe)
Jesse Barnes731cd552008-12-17 10:09:49 -0800488{
Peter Clifton04f90a42010-01-06 20:44:11 +0000489 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800490 drmModeConnectorPtr r = NULL;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200491 struct drm_mode_modeinfo stack_mode;
Jesse Barnes731cd552008-12-17 10:09:49 -0800492
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100493 memclear(conn);
Jesse Barnes731cd552008-12-17 10:09:49 -0800494 conn.connector_id = connector_id;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000495 if (!probe) {
496 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200497 conn.modes_ptr = VOID2U64(&stack_mode);
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000498 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800499
500 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
501 return 0;
502
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000503retry:
Peter Clifton04f90a42010-01-06 20:44:11 +0000504 counts = conn;
505
Jesse Barnes731cd552008-12-17 10:09:49 -0800506 if (conn.count_props) {
507 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000508 if (!conn.props_ptr)
509 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800510 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000511 if (!conn.prop_values_ptr)
512 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800513 }
514
Peter Clifton04f90a42010-01-06 20:44:11 +0000515 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800516 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000517 if (!conn.modes_ptr)
518 goto err_allocs;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000519 } else {
520 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200521 conn.modes_ptr = VOID2U64(&stack_mode);
Peter Clifton04f90a42010-01-06 20:44:11 +0000522 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800523
Peter Clifton04f90a42010-01-06 20:44:11 +0000524 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800525 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000526 if (!conn.encoders_ptr)
527 goto err_allocs;
528 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800529
530 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
531 goto err_allocs;
532
Peter Clifton04f90a42010-01-06 20:44:11 +0000533 /* The number of available connectors and etc may have changed with a
534 * hotplug event in between the ioctls, in which case the field is
535 * silently ignored by the kernel.
536 */
537 if (counts.count_props < conn.count_props ||
538 counts.count_modes < conn.count_modes ||
539 counts.count_encoders < conn.count_encoders) {
540 drmFree(U642VOID(conn.props_ptr));
541 drmFree(U642VOID(conn.prop_values_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200542 if (U642VOID(conn.modes_ptr) != &stack_mode)
543 drmFree(U642VOID(conn.modes_ptr));
Peter Clifton04f90a42010-01-06 20:44:11 +0000544 drmFree(U642VOID(conn.encoders_ptr));
545
546 goto retry;
547 }
548
Jesse Barnes731cd552008-12-17 10:09:49 -0800549 if(!(r = drmMalloc(sizeof(*r)))) {
550 goto err_allocs;
551 }
552
553 r->connector_id = conn.connector_id;
554 r->encoder_id = conn.encoder_id;
555 r->connection = conn.connection;
556 r->mmWidth = conn.mm_width;
557 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000558 /* convert subpixel from kernel to userspace */
559 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800560 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800561 r->count_props = conn.count_props;
562 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
563 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
564 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
565 r->count_encoders = conn.count_encoders;
566 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
567 r->connector_type = conn.connector_type;
568 r->connector_type_id = conn.connector_type_id;
569
Peter Clifton04f90a42010-01-06 20:44:11 +0000570 if ((r->count_props && !r->props) ||
571 (r->count_props && !r->prop_values) ||
572 (r->count_modes && !r->modes) ||
573 (r->count_encoders && !r->encoders)) {
574 drmFree(r->props);
575 drmFree(r->prop_values);
576 drmFree(r->modes);
577 drmFree(r->encoders);
578 drmFree(r);
579 r = 0;
580 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800581
582err_allocs:
583 drmFree(U642VOID(conn.prop_values_ptr));
584 drmFree(U642VOID(conn.props_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200585 if (U642VOID(conn.modes_ptr) != &stack_mode)
586 drmFree(U642VOID(conn.modes_ptr));
Jesse Barnes731cd552008-12-17 10:09:49 -0800587 drmFree(U642VOID(conn.encoders_ptr));
588
589 return r;
590}
591
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000592drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
593{
594 return _drmModeGetConnector(fd, connector_id, 1);
595}
596
597drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
598{
599 return _drmModeGetConnector(fd, connector_id, 0);
600}
601
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100602int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800603{
604 struct drm_mode_mode_cmd res;
605
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100606 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800607 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
608 res.connector_id = connector_id;
609
Chris Wilsonb8039182010-07-01 22:38:54 +0100610 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800611}
612
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100613int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800614{
615 struct drm_mode_mode_cmd res;
616
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100617 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800618 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
619 res.connector_id = connector_id;
620
Chris Wilsonb8039182010-07-01 22:38:54 +0100621 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800622}
623
Jesse Barnes731cd552008-12-17 10:09:49 -0800624drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
625{
626 struct drm_mode_get_property prop;
627 drmModePropertyPtr r;
628
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100629 memclear(prop);
Jesse Barnes731cd552008-12-17 10:09:49 -0800630 prop.prop_id = property_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800631
632 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
633 return 0;
634
635 if (prop.count_values)
636 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
637
Rob Clark7b228e92012-06-05 12:28:22 -0500638 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800639 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
640
641 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
642 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
643 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
644 }
645
646 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
647 r = NULL;
648 goto err_allocs;
649 }
650
651 if (!(r = drmMalloc(sizeof(*r))))
652 return NULL;
653
654 r->prop_id = prop.prop_id;
655 r->count_values = prop.count_values;
656
657 r->flags = prop.flags;
658 if (prop.count_values)
659 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
Rob Clark7b228e92012-06-05 12:28:22 -0500660 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800661 r->count_enums = prop.count_enum_blobs;
662 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
663 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
664 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
665 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
666 r->count_blobs = prop.count_enum_blobs;
667 }
668 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
669 r->name[DRM_PROP_NAME_LEN-1] = 0;
670
671err_allocs:
672 drmFree(U642VOID(prop.values_ptr));
673 drmFree(U642VOID(prop.enum_blob_ptr));
674
675 return r;
676}
677
678void drmModeFreeProperty(drmModePropertyPtr ptr)
679{
680 if (!ptr)
681 return;
682
683 drmFree(ptr->values);
684 drmFree(ptr->enums);
685 drmFree(ptr);
686}
687
688drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
689{
690 struct drm_mode_get_blob blob;
691 drmModePropertyBlobPtr r;
692
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100693 memclear(blob);
Jesse Barnes731cd552008-12-17 10:09:49 -0800694 blob.blob_id = blob_id;
695
696 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
697 return NULL;
698
699 if (blob.length)
700 blob.data = VOID2U64(drmMalloc(blob.length));
701
702 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
703 r = NULL;
704 goto err_allocs;
705 }
706
707 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100708 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800709
710 r->id = blob.blob_id;
711 r->length = blob.length;
712 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
713
714err_allocs:
715 drmFree(U642VOID(blob.data));
716 return r;
717}
718
719void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
720{
721 if (!ptr)
722 return;
723
724 drmFree(ptr->data);
725 drmFree(ptr);
726}
727
728int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
729 uint64_t value)
730{
731 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800732
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100733 memclear(osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800734 osp.connector_id = connector_id;
735 osp.prop_id = property_id;
736 osp.value = value;
737
Chris Wilsonb8039182010-07-01 22:38:54 +0100738 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800739}
740
741/*
742 * checks if a modesetting capable driver has attached to the pci id
743 * returns 0 if modesetting supported.
744 * -EINVAL or invalid bus id
745 * -ENOSYS if no modesetting support
746*/
747int drmCheckModesettingSupported(const char *busid)
748{
Robert Millancbb31f22014-01-23 14:46:05 +0000749#if defined (__linux__)
Jesse Barnes731cd552008-12-17 10:09:49 -0800750 char pci_dev_dir[1024];
751 int domain, bus, dev, func;
752 DIR *sysdir;
753 struct dirent *dent;
754 int found = 0, ret;
755
756 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
757 if (ret != 4)
758 return -EINVAL;
759
760 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
761 domain, bus, dev, func);
762
763 sysdir = opendir(pci_dev_dir);
764 if (sysdir) {
765 dent = readdir(sysdir);
766 while (dent) {
767 if (!strncmp(dent->d_name, "controlD", 8)) {
768 found = 1;
769 break;
770 }
771
772 dent = readdir(sysdir);
773 }
774 closedir(sysdir);
775 if (found)
776 return 0;
777 }
778
779 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
780 domain, bus, dev, func);
781
782 sysdir = opendir(pci_dev_dir);
783 if (!sysdir)
784 return -EINVAL;
785
786 dent = readdir(sysdir);
787 while (dent) {
788 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
789 found = 1;
790 break;
791 }
792
793 dent = readdir(sysdir);
794 }
795
796 closedir(sysdir);
797 if (found)
798 return 0;
Robert Millancbb31f22014-01-23 14:46:05 +0000799#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
800 char kbusid[1024], sbusid[1024];
801 char oid[128];
802 int domain, bus, dev, func;
803 int i, modesetting, ret;
804 size_t len;
805
806 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
807 &func);
808 if (ret != 4)
809 return -EINVAL;
810 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
811 dev, func);
812
813 /* How many GPUs do we expect in the machine ? */
814 for (i = 0; i < 16; i++) {
815 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
816 len = sizeof(sbusid);
817 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
818 if (ret == -1) {
819 if (errno == ENOENT)
820 continue;
821 return -EINVAL;
822 }
823 if (strcmp(sbusid, kbusid) != 0)
824 continue;
825 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
826 len = sizeof(modesetting);
827 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
828 if (ret == -1 || len != sizeof(modesetting))
829 return -EINVAL;
830 return (modesetting ? 0 : -ENOSYS);
831 }
François Tigeotedbb4e52014-07-26 13:39:58 +0200832#elif defined(__DragonFly__)
833 return 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800834#endif
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000835#ifdef __OpenBSD__
836 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
Jesse Barnes731cd552008-12-17 10:09:49 -0800856int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
857 uint16_t *red, uint16_t *green, uint16_t *blue)
858{
Jesse Barnes731cd552008-12-17 10:09:49 -0800859 struct drm_mode_crtc_lut l;
860
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100861 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800862 l.crtc_id = crtc_id;
863 l.gamma_size = size;
864 l.red = VOID2U64(red);
865 l.green = VOID2U64(green);
866 l.blue = VOID2U64(blue);
867
Chris Wilsonb8039182010-07-01 22:38:54 +0100868 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800869}
870
871int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
872 uint16_t *red, uint16_t *green, uint16_t *blue)
873{
Jesse Barnes731cd552008-12-17 10:09:49 -0800874 struct drm_mode_crtc_lut l;
875
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100876 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800877 l.crtc_id = crtc_id;
878 l.gamma_size = size;
879 l.red = VOID2U64(red);
880 l.green = VOID2U64(green);
881 l.blue = VOID2U64(blue);
882
Chris Wilsonb8039182010-07-01 22:38:54 +0100883 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800884}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400885
886int drmHandleEvent(int fd, drmEventContextPtr evctx)
887{
888 char buffer[1024];
889 int len, i;
890 struct drm_event *e;
891 struct drm_event_vblank *vblank;
Keith Packardd4331dd2017-07-01 00:43:15 -0700892 struct drm_event_crtc_sequence *seq;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300893 void *user_data;
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900894
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400895 /* The DRM read semantics guarantees that we always get only
896 * complete events. */
897
898 len = read(fd, buffer, sizeof buffer);
899 if (len == 0)
900 return 0;
Jan Veselyde8532d2014-11-30 12:53:18 -0500901 if (len < (int)sizeof *e)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400902 return -1;
903
904 i = 0;
905 while (i < len) {
Thierry Redingecc2a092015-04-13 11:36:59 +0200906 e = (struct drm_event *)(buffer + i);
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400907 switch (e->type) {
908 case DRM_EVENT_VBLANK:
909 if (evctx->version < 1 ||
910 evctx->vblank_handler == NULL)
911 break;
912 vblank = (struct drm_event_vblank *) e;
913 evctx->vblank_handler(fd,
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900914 vblank->sequence,
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400915 vblank->tv_sec,
916 vblank->tv_usec,
917 U642VOID (vblank->user_data));
918 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500919 case DRM_EVENT_FLIP_COMPLETE:
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500920 vblank = (struct drm_event_vblank *) e;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300921 user_data = U642VOID (vblank->user_data);
922
923 if (evctx->version >= 3 && evctx->page_flip_handler2)
924 evctx->page_flip_handler2(fd,
925 vblank->sequence,
926 vblank->tv_sec,
927 vblank->tv_usec,
928 vblank->crtc_id,
929 user_data);
930 else if (evctx->version >= 2 && evctx->page_flip_handler)
931 evctx->page_flip_handler(fd,
932 vblank->sequence,
933 vblank->tv_sec,
934 vblank->tv_usec,
935 user_data);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500936 break;
Keith Packardd4331dd2017-07-01 00:43:15 -0700937 case DRM_EVENT_CRTC_SEQUENCE:
938 seq = (struct drm_event_crtc_sequence *) e;
939 if (evctx->version >= 4 && evctx->sequence_handler)
940 evctx->sequence_handler(fd,
941 seq->sequence,
942 seq->time_ns,
943 seq->user_data);
944 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400945 default:
946 break;
947 }
948 i += e->length;
949 }
950
951 return 0;
952}
953
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500954int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
955 uint32_t flags, void *user_data)
956{
957 struct drm_mode_crtc_page_flip flip;
958
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100959 memclear(flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500960 flip.fb_id = fb_id;
961 flip.crtc_id = crtc_id;
962 flip.user_data = VOID2U64(user_data);
963 flip.flags = flags;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500964
Chris Wilsonb8039182010-07-01 22:38:54 +0100965 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500966}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700967
Michel Dänzer7dd28472016-06-29 18:07:25 +0900968int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
969 uint32_t flags, void *user_data,
970 uint32_t target_vblank)
971{
972 struct drm_mode_crtc_page_flip_target flip_target;
973
974 memclear(flip_target);
975 flip_target.fb_id = fb_id;
976 flip_target.crtc_id = crtc_id;
977 flip_target.user_data = VOID2U64(user_data);
978 flip_target.flags = flags;
979 flip_target.sequence = target_vblank;
980
981 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
982}
983
Jesse Barnesac168bf2011-04-29 08:53:53 -0700984int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
985 uint32_t fb_id, uint32_t flags,
Daniel Kurtz828c3e82014-05-01 19:56:43 +0800986 int32_t crtc_x, int32_t crtc_y,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700987 uint32_t crtc_w, uint32_t crtc_h,
988 uint32_t src_x, uint32_t src_y,
989 uint32_t src_w, uint32_t src_h)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700990{
991 struct drm_mode_set_plane s;
992
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100993 memclear(s);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700994 s.plane_id = plane_id;
995 s.crtc_id = crtc_id;
996 s.fb_id = fb_id;
997 s.flags = flags;
998 s.crtc_x = crtc_x;
999 s.crtc_y = crtc_y;
1000 s.crtc_w = crtc_w;
1001 s.crtc_h = crtc_h;
1002 s.src_x = src_x;
1003 s.src_y = src_y;
1004 s.src_w = src_w;
1005 s.src_h = src_h;
1006
1007 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1008}
1009
Jesse Barnesac168bf2011-04-29 08:53:53 -07001010drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
1011{
1012 struct drm_mode_get_plane ovr, counts;
1013 drmModePlanePtr r = 0;
1014
1015retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001016 memclear(ovr);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001017 ovr.plane_id = plane_id;
1018 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1019 return 0;
1020
1021 counts = ovr;
1022
1023 if (ovr.count_format_types) {
1024 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1025 sizeof(uint32_t)));
1026 if (!ovr.format_type_ptr)
1027 goto err_allocs;
1028 }
1029
1030 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1031 goto err_allocs;
1032
1033 if (counts.count_format_types < ovr.count_format_types) {
1034 drmFree(U642VOID(ovr.format_type_ptr));
1035 goto retry;
1036 }
1037
1038 if (!(r = drmMalloc(sizeof(*r))))
1039 goto err_allocs;
1040
1041 r->count_formats = ovr.count_format_types;
1042 r->plane_id = ovr.plane_id;
1043 r->crtc_id = ovr.crtc_id;
1044 r->fb_id = ovr.fb_id;
1045 r->possible_crtcs = ovr.possible_crtcs;
1046 r->gamma_size = ovr.gamma_size;
1047 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1048 ovr.count_format_types, sizeof(uint32_t));
1049 if (ovr.count_format_types && !r->formats) {
1050 drmFree(r->formats);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001051 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001052 r = 0;
1053 }
1054
1055err_allocs:
1056 drmFree(U642VOID(ovr.format_type_ptr));
1057
1058 return r;
1059}
1060
1061void drmModeFreePlane(drmModePlanePtr ptr)
1062{
1063 if (!ptr)
1064 return;
1065
1066 drmFree(ptr->formats);
1067 drmFree(ptr);
1068}
1069
1070drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1071{
1072 struct drm_mode_get_plane_res res, counts;
1073 drmModePlaneResPtr r = 0;
1074
1075retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001076 memclear(res);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001077 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1078 return 0;
1079
1080 counts = res;
1081
1082 if (res.count_planes) {
1083 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1084 sizeof(uint32_t)));
1085 if (!res.plane_id_ptr)
1086 goto err_allocs;
1087 }
1088
1089 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1090 goto err_allocs;
1091
1092 if (counts.count_planes < res.count_planes) {
1093 drmFree(U642VOID(res.plane_id_ptr));
1094 goto retry;
1095 }
1096
1097 if (!(r = drmMalloc(sizeof(*r))))
1098 goto err_allocs;
1099
1100 r->count_planes = res.count_planes;
1101 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1102 res.count_planes, sizeof(uint32_t));
1103 if (res.count_planes && !r->planes) {
1104 drmFree(r->planes);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001105 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001106 r = 0;
1107 }
1108
1109err_allocs:
1110 drmFree(U642VOID(res.plane_id_ptr));
1111
1112 return r;
1113}
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001114
1115void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1116{
1117 if (!ptr)
1118 return;
1119
1120 drmFree(ptr->planes);
1121 drmFree(ptr);
1122}
Paulo Zanoni8c757032012-05-15 18:38:28 -03001123
1124drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1125 uint32_t object_id,
1126 uint32_t object_type)
1127{
1128 struct drm_mode_obj_get_properties properties;
1129 drmModeObjectPropertiesPtr ret = NULL;
1130 uint32_t count;
1131
1132retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001133 memclear(properties);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001134 properties.obj_id = object_id;
1135 properties.obj_type = object_type;
1136
1137 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1138 return 0;
1139
1140 count = properties.count_props;
1141
1142 if (count) {
1143 properties.props_ptr = VOID2U64(drmMalloc(count *
1144 sizeof(uint32_t)));
1145 if (!properties.props_ptr)
1146 goto err_allocs;
1147 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1148 sizeof(uint64_t)));
1149 if (!properties.prop_values_ptr)
1150 goto err_allocs;
1151 }
1152
1153 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1154 goto err_allocs;
1155
1156 if (count < properties.count_props) {
1157 drmFree(U642VOID(properties.props_ptr));
1158 drmFree(U642VOID(properties.prop_values_ptr));
1159 goto retry;
1160 }
1161 count = properties.count_props;
1162
1163 ret = drmMalloc(sizeof(*ret));
1164 if (!ret)
1165 goto err_allocs;
1166
1167 ret->count_props = count;
1168 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1169 count, sizeof(uint32_t));
1170 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1171 count, sizeof(uint64_t));
1172 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1173 drmFree(ret->props);
1174 drmFree(ret->prop_values);
1175 drmFree(ret);
1176 ret = NULL;
1177 }
1178
1179err_allocs:
1180 drmFree(U642VOID(properties.props_ptr));
1181 drmFree(U642VOID(properties.prop_values_ptr));
1182 return ret;
1183}
1184
1185void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1186{
1187 if (!ptr)
1188 return;
1189 drmFree(ptr->props);
1190 drmFree(ptr->prop_values);
1191 drmFree(ptr);
1192}
1193
1194int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1195 uint32_t property_id, uint64_t value)
1196{
1197 struct drm_mode_obj_set_property prop;
1198
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001199 memclear(prop);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001200 prop.value = value;
1201 prop.prop_id = property_id;
1202 prop.obj_id = object_id;
1203 prop.obj_type = object_type;
1204
1205 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1206}
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001207
1208typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1209
1210struct _drmModeAtomicReqItem {
1211 uint32_t object_id;
1212 uint32_t property_id;
1213 uint64_t value;
1214};
1215
1216struct _drmModeAtomicReq {
1217 uint32_t cursor;
1218 uint32_t size_items;
1219 drmModeAtomicReqItemPtr items;
1220};
1221
1222drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1223{
1224 drmModeAtomicReqPtr req;
1225
1226 req = drmMalloc(sizeof *req);
1227 if (!req)
1228 return NULL;
1229
1230 req->items = NULL;
1231 req->cursor = 0;
1232 req->size_items = 0;
1233
1234 return req;
1235}
1236
1237drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1238{
1239 drmModeAtomicReqPtr new;
1240
Emil Velikoved3c6652015-09-05 17:20:53 +01001241 if (!old)
1242 return NULL;
1243
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001244 new = drmMalloc(sizeof *new);
1245 if (!new)
1246 return NULL;
1247
1248 new->cursor = old->cursor;
1249 new->size_items = old->size_items;
1250
1251 if (old->size_items) {
1252 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1253 if (!new->items) {
1254 free(new);
1255 return NULL;
1256 }
1257 memcpy(new->items, old->items,
1258 old->size_items * sizeof(*new->items));
1259 } else {
1260 new->items = NULL;
1261 }
1262
1263 return new;
1264}
1265
1266int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1267{
Emil Velikoved3c6652015-09-05 17:20:53 +01001268 if (!base)
1269 return -EINVAL;
1270
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001271 if (!augment || augment->cursor == 0)
1272 return 0;
1273
1274 if (base->cursor + augment->cursor >= base->size_items) {
1275 drmModeAtomicReqItemPtr new;
1276 int saved_size = base->size_items;
1277
1278 base->size_items = base->cursor + augment->cursor;
1279 new = realloc(base->items,
1280 base->size_items * sizeof(*base->items));
1281 if (!new) {
1282 base->size_items = saved_size;
1283 return -ENOMEM;
1284 }
1285 base->items = new;
1286 }
1287
1288 memcpy(&base->items[base->cursor], augment->items,
1289 augment->cursor * sizeof(*augment->items));
1290 base->cursor += augment->cursor;
1291
1292 return 0;
1293}
1294
1295int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1296{
Emil Velikoved3c6652015-09-05 17:20:53 +01001297 if (!req)
1298 return -EINVAL;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001299 return req->cursor;
1300}
1301
1302void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1303{
Emil Velikoved3c6652015-09-05 17:20:53 +01001304 if (req)
1305 req->cursor = cursor;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001306}
1307
1308int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1309 uint32_t object_id,
1310 uint32_t property_id,
1311 uint64_t value)
1312{
Emil Velikoved3c6652015-09-05 17:20:53 +01001313 if (!req)
1314 return -EINVAL;
1315
Daniel Stone45eee3f2018-03-07 12:41:12 +00001316 if (object_id == 0 || property_id == 0)
1317 return -EINVAL;
1318
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001319 if (req->cursor >= req->size_items) {
1320 drmModeAtomicReqItemPtr new;
1321
1322 req->size_items += 16;
1323 new = realloc(req->items, req->size_items * sizeof(*req->items));
1324 if (!new) {
1325 req->size_items -= 16;
1326 return -ENOMEM;
1327 }
1328 req->items = new;
1329 }
1330
1331 req->items[req->cursor].object_id = object_id;
1332 req->items[req->cursor].property_id = property_id;
1333 req->items[req->cursor].value = value;
1334 req->cursor++;
1335
1336 return req->cursor;
1337}
1338
1339void drmModeAtomicFree(drmModeAtomicReqPtr req)
1340{
1341 if (!req)
1342 return;
1343
1344 if (req->items)
1345 drmFree(req->items);
1346 drmFree(req);
1347}
1348
1349static int sort_req_list(const void *misc, const void *other)
1350{
1351 const drmModeAtomicReqItem *first = misc;
1352 const drmModeAtomicReqItem *second = other;
1353
1354 if (first->object_id < second->object_id)
1355 return -1;
1356 else if (first->object_id > second->object_id)
1357 return 1;
1358 else
1359 return second->property_id - first->property_id;
1360}
1361
1362int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1363 void *user_data)
1364{
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001365 drmModeAtomicReqPtr sorted;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001366 struct drm_mode_atomic atomic;
1367 uint32_t *objs_ptr = NULL;
1368 uint32_t *count_props_ptr = NULL;
1369 uint32_t *props_ptr = NULL;
1370 uint64_t *prop_values_ptr = NULL;
1371 uint32_t last_obj_id = 0;
1372 uint32_t i;
1373 int obj_idx = -1;
1374 int ret = -1;
1375
Emil Velikoved3c6652015-09-05 17:20:53 +01001376 if (!req)
1377 return -EINVAL;
1378
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001379 if (req->cursor == 0)
1380 return 0;
1381
1382 sorted = drmModeAtomicDuplicate(req);
1383 if (sorted == NULL)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001384 return -ENOMEM;
1385
1386 memclear(atomic);
1387
1388 /* Sort the list by object ID, then by property ID. */
1389 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1390 sort_req_list);
1391
1392 /* Now the list is sorted, eliminate duplicate property sets. */
1393 for (i = 0; i < sorted->cursor; i++) {
1394 if (sorted->items[i].object_id != last_obj_id) {
1395 atomic.count_objs++;
1396 last_obj_id = sorted->items[i].object_id;
1397 }
1398
1399 if (i == sorted->cursor - 1)
1400 continue;
1401
1402 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1403 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1404 continue;
1405
1406 memmove(&sorted->items[i], &sorted->items[i + 1],
1407 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1408 sorted->cursor--;
1409 }
1410
1411 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1412 if (!objs_ptr) {
1413 errno = ENOMEM;
1414 goto out;
1415 }
1416
1417 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1418 if (!count_props_ptr) {
1419 errno = ENOMEM;
1420 goto out;
1421 }
1422
1423 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1424 if (!props_ptr) {
1425 errno = ENOMEM;
1426 goto out;
1427 }
1428
1429 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1430 if (!prop_values_ptr) {
1431 errno = ENOMEM;
1432 goto out;
1433 }
1434
1435 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1436 if (sorted->items[i].object_id != last_obj_id) {
1437 obj_idx++;
1438 objs_ptr[obj_idx] = sorted->items[i].object_id;
1439 last_obj_id = objs_ptr[obj_idx];
1440 }
1441
1442 count_props_ptr[obj_idx]++;
1443 props_ptr[i] = sorted->items[i].property_id;
1444 prop_values_ptr[i] = sorted->items[i].value;
1445
1446 }
1447
1448 atomic.flags = flags;
1449 atomic.objs_ptr = VOID2U64(objs_ptr);
1450 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1451 atomic.props_ptr = VOID2U64(props_ptr);
1452 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1453 atomic.user_data = VOID2U64(user_data);
1454
1455 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1456
1457out:
1458 drmFree(objs_ptr);
1459 drmFree(count_props_ptr);
1460 drmFree(props_ptr);
1461 drmFree(prop_values_ptr);
1462 drmModeAtomicFree(sorted);
1463
1464 return ret;
1465}
Daniel Stone32471b22015-06-22 17:26:03 +01001466
1467int
1468drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1469{
1470 struct drm_mode_create_blob create;
1471 int ret;
1472
1473 if (length >= 0xffffffff)
1474 return -ERANGE;
1475
1476 memclear(create);
1477
1478 create.length = length;
1479 create.data = (uintptr_t) data;
1480 create.blob_id = 0;
1481 *id = 0;
1482
1483 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1484 if (ret != 0)
1485 return ret;
1486
1487 *id = create.blob_id;
1488 return 0;
1489}
1490
1491int
1492drmModeDestroyPropertyBlob(int fd, uint32_t id)
1493{
1494 struct drm_mode_destroy_blob destroy;
1495
1496 memclear(destroy);
1497 destroy.blob_id = id;
1498 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1499}
Keith Packardc4171532017-03-16 18:11:05 -07001500
1501int
1502drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags, uint32_t *lessee_id)
1503{
1504 struct drm_mode_create_lease create;
1505 int ret;
1506
1507 memclear(create);
1508 create.object_ids = (uintptr_t) objects;
1509 create.object_count = num_objects;
1510 create.flags = flags;
1511
1512 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1513 if (ret == 0) {
1514 *lessee_id = create.lessee_id;
1515 return create.fd;
1516 }
1517 return -errno;
1518}
1519
1520drmModeLesseeListPtr
1521drmModeListLessees(int fd)
1522{
1523 struct drm_mode_list_lessees list;
1524 uint32_t count;
1525 drmModeLesseeListPtr ret;
1526
1527 memclear(list);
1528
1529 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1530 return NULL;
1531
1532 count = list.count_lessees;
1533 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1534 if (!ret)
1535 return NULL;
1536
1537 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1538 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1539 drmFree(ret);
1540 return NULL;
1541 }
1542
1543 ret->count = count;
1544 return ret;
1545}
1546
1547drmModeObjectListPtr
1548drmModeGetLease(int fd)
1549{
1550 struct drm_mode_get_lease get;
1551 uint32_t count;
1552 drmModeObjectListPtr ret;
1553
1554 memclear(get);
1555
1556 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1557 return NULL;
1558
1559 count = get.count_objects;
1560 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1561 if (!ret)
1562 return NULL;
1563
1564 get.objects_ptr = VOID2U64(&ret->objects[0]);
1565 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1566 drmFree(ret);
1567 return NULL;
1568 }
1569
1570 ret->count = count;
1571 return ret;
1572}
1573
1574int
1575drmModeRevokeLease(int fd, uint32_t lessee_id)
1576{
1577 struct drm_mode_revoke_lease revoke;
1578 int ret;
1579
1580 memclear(revoke);
1581
1582 revoke.lessee_id = lessee_id;
1583
1584 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
1585 if (ret == 0)
1586 return 0;
1587 return -errno;
1588}