blob: cf8e1eacb58a57a374cf5ec6477df564484a9668 [file] [log] [blame]
Jesse Barnes731cd552008-12-17 10:09:49 -08001/*
2 * \file xf86drmMode.c
3 * Header for DRM modesetting interface.
4 *
5 * \author Jakob Bornecrantz <wallbraker@gmail.com>
6 *
7 * \par Acknowledgements:
8 * Feb 2007, Dave Airlie <airlied@linux.ie>
9 */
10
11/*
12 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
13 * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
14 * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 *
34 */
35
36/*
Eric Engestromce975072016-04-03 19:48:12 +010037 * TODO the types we are after are defined in different headers on different
Jesse Barnes731cd552008-12-17 10:09:49 -080038 * platforms find which headers to include to get uint32_t
39 */
Emil Velikov5f762732015-06-29 17:32:21 +010040
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010041#include <limits.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080042#include <stdint.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010043#include <stdlib.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080044#include <sys/ioctl.h>
Julien Cristaufc8c3e22015-07-06 12:45:31 +010045#ifdef HAVE_SYS_SYSCTL_H
46#include <sys/sysctl.h>
47#endif
Jesse Barnes731cd552008-12-17 10:09:49 -080048#include <stdio.h>
Ville Syrjäläed44e0b2015-06-22 17:26:02 +010049#include <stdbool.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080050
51#include "xf86drmMode.h"
52#include "xf86drm.h"
53#include <drm.h>
54#include <string.h>
55#include <dirent.h>
Kristian Høgsbergb0b96632009-09-11 13:27:35 -040056#include <unistd.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080057#include <errno.h>
58
Daniel Vetter7e0460c2015-02-11 12:03:12 +010059#define memclear(s) memset(&s, 0, sizeof(s))
Eric Anholt734de702013-12-28 22:06:51 -080060
Jesse Barnes731cd552008-12-17 10:09:49 -080061#define U642VOID(x) ((void *)(unsigned long)(x))
62#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
63
Marcin Slusarz763b6182011-06-05 18:53:16 +020064static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
Chris Wilsonb8039182010-07-01 22:38:54 +010065{
66 int ret = drmIoctl(fd, cmd, arg);
67 return ret < 0 ? -errno : ret;
68}
69
Jesse Barnes731cd552008-12-17 10:09:49 -080070/*
71 * Util functions
72 */
73
Jan Vesely65041c42015-02-27 11:51:05 -050074static void* drmAllocCpy(char *array, int count, int entry_size)
Jesse Barnes731cd552008-12-17 10:09:49 -080075{
76 char *r;
77 int i;
78
79 if (!count || !array || !entry_size)
80 return 0;
81
82 if (!(r = drmMalloc(count*entry_size)))
83 return 0;
84
85 for (i = 0; i < count; i++)
86 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
87
88 return r;
89}
90
91/*
92 * A couple of free functions.
93 */
94
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +010095void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -080096{
97 if (!ptr)
98 return;
99
100 drmFree(ptr);
101}
102
103void drmModeFreeResources(drmModeResPtr ptr)
104{
105 if (!ptr)
106 return;
107
Ville Syrjälädf497e92012-02-02 14:53:39 -0500108 drmFree(ptr->fbs);
109 drmFree(ptr->crtcs);
110 drmFree(ptr->connectors);
111 drmFree(ptr->encoders);
Jesse Barnes731cd552008-12-17 10:09:49 -0800112 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800113}
114
115void drmModeFreeFB(drmModeFBPtr ptr)
116{
117 if (!ptr)
118 return;
119
120 /* we might add more frees later. */
121 drmFree(ptr);
122}
123
124void drmModeFreeCrtc(drmModeCrtcPtr ptr)
125{
126 if (!ptr)
127 return;
128
129 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800130}
131
132void drmModeFreeConnector(drmModeConnectorPtr ptr)
133{
134 if (!ptr)
135 return;
136
Keith Packard0a246542009-09-17 17:28:08 -0700137 drmFree(ptr->encoders);
138 drmFree(ptr->prop_values);
139 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800140 drmFree(ptr->modes);
141 drmFree(ptr);
Jesse Barnes731cd552008-12-17 10:09:49 -0800142}
143
144void drmModeFreeEncoder(drmModeEncoderPtr ptr)
145{
146 drmFree(ptr);
147}
148
149/*
150 * ModeSetting functions.
151 */
152
153drmModeResPtr drmModeGetResources(int fd)
154{
Chris Wilsond1308f42010-01-06 15:19:25 +0000155 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800156 drmModeResPtr r = 0;
157
Chris Wilsond1308f42010-01-06 15:19:25 +0000158retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100159 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800160 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
161 return 0;
162
Chris Wilsond1308f42010-01-06 15:19:25 +0000163 counts = res;
164
Chris Wilson85fb3e52010-01-06 15:39:49 +0000165 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800166 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000167 if (!res.fb_id_ptr)
168 goto err_allocs;
169 }
170 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800171 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000172 if (!res.crtc_id_ptr)
173 goto err_allocs;
174 }
175 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800176 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000177 if (!res.connector_id_ptr)
178 goto err_allocs;
179 }
180 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800181 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000182 if (!res.encoder_id_ptr)
183 goto err_allocs;
184 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800185
Chris Wilsond1308f42010-01-06 15:19:25 +0000186 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800187 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000188
189 /* The number of available connectors and etc may have changed with a
190 * hotplug event in between the ioctls, in which case the field is
191 * silently ignored by the kernel.
192 */
193 if (counts.count_fbs < res.count_fbs ||
194 counts.count_crtcs < res.count_crtcs ||
195 counts.count_connectors < res.count_connectors ||
196 counts.count_encoders < res.count_encoders)
197 {
198 drmFree(U642VOID(res.fb_id_ptr));
199 drmFree(U642VOID(res.crtc_id_ptr));
200 drmFree(U642VOID(res.connector_id_ptr));
201 drmFree(U642VOID(res.encoder_id_ptr));
202
203 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800204 }
205
206 /*
207 * return
208 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800209 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000210 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800211
212 r->min_width = res.min_width;
213 r->max_width = res.max_width;
214 r->min_height = res.min_height;
215 r->max_height = res.max_height;
216 r->count_fbs = res.count_fbs;
217 r->count_crtcs = res.count_crtcs;
218 r->count_connectors = res.count_connectors;
219 r->count_encoders = res.count_encoders;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000220
221 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
222 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
223 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
224 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000225 if ((res.count_fbs && !r->fbs) ||
226 (res.count_crtcs && !r->crtcs) ||
227 (res.count_connectors && !r->connectors) ||
228 (res.count_encoders && !r->encoders))
229 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000230 drmFree(r->fbs);
231 drmFree(r->crtcs);
232 drmFree(r->connectors);
233 drmFree(r->encoders);
234 drmFree(r);
235 r = 0;
236 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800237
238err_allocs:
239 drmFree(U642VOID(res.fb_id_ptr));
240 drmFree(U642VOID(res.crtc_id_ptr));
241 drmFree(U642VOID(res.connector_id_ptr));
242 drmFree(U642VOID(res.encoder_id_ptr));
243
244 return r;
245}
246
247int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200248 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
Jesse Barnes731cd552008-12-17 10:09:49 -0800249 uint32_t *buf_id)
250{
251 struct drm_mode_fb_cmd f;
252 int ret;
253
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100254 memclear(f);
Jesse Barnes731cd552008-12-17 10:09:49 -0800255 f.width = width;
256 f.height = height;
257 f.pitch = pitch;
258 f.bpp = bpp;
259 f.depth = depth;
260 f.handle = bo_handle;
261
Chris Wilsonb8039182010-07-01 22:38:54 +0100262 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800263 return ret;
264
265 *buf_id = f.fb_id;
266 return 0;
267}
268
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700269int drmModeAddFB2WithModifiers(int fd, uint32_t width, uint32_t height,
Tobias Jakobi09be5412017-10-10 12:12:52 +0200270 uint32_t pixel_format, const uint32_t bo_handles[4],
271 const uint32_t pitches[4], const uint32_t offsets[4],
272 const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700273{
274 struct drm_mode_fb_cmd2 f;
275 int ret;
276
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100277 memclear(f);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700278 f.width = width;
279 f.height = height;
280 f.pixel_format = pixel_format;
281 f.flags = flags;
Ville Syrjälä76b4a692012-02-02 14:53:43 -0500282 memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
283 memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
284 memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700285 if (modifier)
286 memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
Jesse Barnesac168bf2011-04-29 08:53:53 -0700287
288 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
289 return ret;
290
291 *buf_id = f.fb_id;
292 return 0;
293}
294
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700295int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
Tobias Jakobi09be5412017-10-10 12:12:52 +0200296 uint32_t pixel_format, const uint32_t bo_handles[4],
297 const uint32_t pitches[4], const uint32_t offsets[4],
Kristian H. Kristensenabfa6802016-09-08 13:08:59 -0700298 uint32_t *buf_id, uint32_t flags)
299{
300 return drmModeAddFB2WithModifiers(fd, width, height,
301 pixel_format, bo_handles,
302 pitches, offsets, NULL,
303 buf_id, flags);
304}
305
Jesse Barnes731cd552008-12-17 10:09:49 -0800306int drmModeRmFB(int fd, uint32_t bufferId)
307{
Chris Wilsonb8039182010-07-01 22:38:54 +0100308 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800309}
310
311drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
312{
313 struct drm_mode_fb_cmd info;
314 drmModeFBPtr r;
315
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100316 memclear(info);
Jesse Barnes731cd552008-12-17 10:09:49 -0800317 info.fb_id = buf;
318
319 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
320 return NULL;
321
322 if (!(r = drmMalloc(sizeof(*r))))
323 return NULL;
324
325 r->fb_id = info.fb_id;
326 r->width = info.width;
327 r->height = info.height;
328 r->pitch = info.pitch;
329 r->bpp = info.bpp;
330 r->handle = info.handle;
331 r->depth = info.depth;
332
333 return r;
334}
335
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100336int drmModeDirtyFB(int fd, uint32_t bufferId,
337 drmModeClipPtr clips, uint32_t num_clips)
338{
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100339 struct drm_mode_fb_dirty_cmd dirty;
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100340
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100341 memclear(dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100342 dirty.fb_id = bufferId;
343 dirty.clips_ptr = VOID2U64(clips);
344 dirty.num_clips = num_clips;
345
Chris Wilsonb8039182010-07-01 22:38:54 +0100346 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100347}
348
Jesse Barnes731cd552008-12-17 10:09:49 -0800349/*
350 * Crtc functions
351 */
352
353drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
354{
355 struct drm_mode_crtc crtc;
356 drmModeCrtcPtr r;
357
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100358 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800359 crtc.crtc_id = crtcId;
360
361 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
362 return 0;
363
364 /*
365 * return
366 */
367
368 if (!(r = drmMalloc(sizeof(*r))))
369 return 0;
370
371 r->crtc_id = crtc.crtc_id;
372 r->x = crtc.x;
373 r->y = crtc.y;
374 r->mode_valid = crtc.mode_valid;
Rob Clarke81acf52012-10-14 16:55:32 -0500375 if (r->mode_valid) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800376 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
Rob Clarke81acf52012-10-14 16:55:32 -0500377 r->width = crtc.mode.hdisplay;
378 r->height = crtc.mode.vdisplay;
379 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800380 r->buffer_id = crtc.fb_id;
381 r->gamma_size = crtc.gamma_size;
382 return r;
383}
384
Jesse Barnes731cd552008-12-17 10:09:49 -0800385int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200386 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100387 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800388{
389 struct drm_mode_crtc crtc;
390
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100391 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800392 crtc.x = x;
393 crtc.y = y;
394 crtc.crtc_id = crtcId;
395 crtc.fb_id = bufferId;
396 crtc.set_connectors_ptr = VOID2U64(connectors);
397 crtc.count_connectors = count;
398 if (mode) {
399 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
400 crtc.mode_valid = 1;
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100401 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800402
Chris Wilsonb8039182010-07-01 22:38:54 +0100403 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800404}
405
406/*
407 * Cursor manipulation
408 */
409
410int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
411{
412 struct drm_mode_cursor arg;
413
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100414 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800415 arg.flags = DRM_MODE_CURSOR_BO;
416 arg.crtc_id = crtcId;
417 arg.width = width;
418 arg.height = height;
419 arg.handle = bo_handle;
420
Chris Wilsonb8039182010-07-01 22:38:54 +0100421 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800422}
423
Dave Airlie2e0ab622013-07-02 09:21:06 +0100424int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height, int32_t hot_x, int32_t hot_y)
425{
426 struct drm_mode_cursor2 arg;
427
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100428 memclear(arg);
Dave Airlie2e0ab622013-07-02 09:21:06 +0100429 arg.flags = DRM_MODE_CURSOR_BO;
430 arg.crtc_id = crtcId;
431 arg.width = width;
432 arg.height = height;
433 arg.handle = bo_handle;
434 arg.hot_x = hot_x;
435 arg.hot_y = hot_y;
436
437 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
438}
439
Jesse Barnes731cd552008-12-17 10:09:49 -0800440int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
441{
442 struct drm_mode_cursor arg;
443
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100444 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800445 arg.flags = DRM_MODE_CURSOR_MOVE;
446 arg.crtc_id = crtcId;
447 arg.x = x;
448 arg.y = y;
449
Chris Wilsonb8039182010-07-01 22:38:54 +0100450 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800451}
452
453/*
454 * Encoder get
455 */
456drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
457{
458 struct drm_mode_get_encoder enc;
459 drmModeEncoderPtr r = NULL;
460
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100461 memclear(enc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800462 enc.encoder_id = encoder_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800463
464 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
465 return 0;
466
467 if (!(r = drmMalloc(sizeof(*r))))
468 return 0;
469
470 r->encoder_id = enc.encoder_id;
471 r->crtc_id = enc.crtc_id;
472 r->encoder_type = enc.encoder_type;
473 r->possible_crtcs = enc.possible_crtcs;
474 r->possible_clones = enc.possible_clones;
475
476 return r;
477}
478
479/*
480 * Connector manipulation
481 */
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000482static drmModeConnectorPtr
483_drmModeGetConnector(int fd, uint32_t connector_id, int probe)
Jesse Barnes731cd552008-12-17 10:09:49 -0800484{
Peter Clifton04f90a42010-01-06 20:44:11 +0000485 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800486 drmModeConnectorPtr r = NULL;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200487 struct drm_mode_modeinfo stack_mode;
Jesse Barnes731cd552008-12-17 10:09:49 -0800488
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100489 memclear(conn);
Jesse Barnes731cd552008-12-17 10:09:49 -0800490 conn.connector_id = connector_id;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000491 if (!probe) {
492 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200493 conn.modes_ptr = VOID2U64(&stack_mode);
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000494 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800495
496 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
497 return 0;
498
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000499retry:
Peter Clifton04f90a42010-01-06 20:44:11 +0000500 counts = conn;
501
Jesse Barnes731cd552008-12-17 10:09:49 -0800502 if (conn.count_props) {
503 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000504 if (!conn.props_ptr)
505 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800506 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000507 if (!conn.prop_values_ptr)
508 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800509 }
510
Peter Clifton04f90a42010-01-06 20:44:11 +0000511 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800512 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000513 if (!conn.modes_ptr)
514 goto err_allocs;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000515 } else {
516 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200517 conn.modes_ptr = VOID2U64(&stack_mode);
Peter Clifton04f90a42010-01-06 20:44:11 +0000518 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800519
Peter Clifton04f90a42010-01-06 20:44:11 +0000520 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800521 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000522 if (!conn.encoders_ptr)
523 goto err_allocs;
524 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800525
526 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
527 goto err_allocs;
528
Peter Clifton04f90a42010-01-06 20:44:11 +0000529 /* The number of available connectors and etc may have changed with a
530 * hotplug event in between the ioctls, in which case the field is
531 * silently ignored by the kernel.
532 */
533 if (counts.count_props < conn.count_props ||
534 counts.count_modes < conn.count_modes ||
535 counts.count_encoders < conn.count_encoders) {
536 drmFree(U642VOID(conn.props_ptr));
537 drmFree(U642VOID(conn.prop_values_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200538 if (U642VOID(conn.modes_ptr) != &stack_mode)
539 drmFree(U642VOID(conn.modes_ptr));
Peter Clifton04f90a42010-01-06 20:44:11 +0000540 drmFree(U642VOID(conn.encoders_ptr));
541
542 goto retry;
543 }
544
Jesse Barnes731cd552008-12-17 10:09:49 -0800545 if(!(r = drmMalloc(sizeof(*r)))) {
546 goto err_allocs;
547 }
548
549 r->connector_id = conn.connector_id;
550 r->encoder_id = conn.encoder_id;
551 r->connection = conn.connection;
552 r->mmWidth = conn.mm_width;
553 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000554 /* convert subpixel from kernel to userspace */
555 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800556 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800557 r->count_props = conn.count_props;
558 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
559 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
560 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
561 r->count_encoders = conn.count_encoders;
562 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
563 r->connector_type = conn.connector_type;
564 r->connector_type_id = conn.connector_type_id;
565
Peter Clifton04f90a42010-01-06 20:44:11 +0000566 if ((r->count_props && !r->props) ||
567 (r->count_props && !r->prop_values) ||
568 (r->count_modes && !r->modes) ||
569 (r->count_encoders && !r->encoders)) {
570 drmFree(r->props);
571 drmFree(r->prop_values);
572 drmFree(r->modes);
573 drmFree(r->encoders);
574 drmFree(r);
575 r = 0;
576 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800577
578err_allocs:
579 drmFree(U642VOID(conn.prop_values_ptr));
580 drmFree(U642VOID(conn.props_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200581 if (U642VOID(conn.modes_ptr) != &stack_mode)
582 drmFree(U642VOID(conn.modes_ptr));
Jesse Barnes731cd552008-12-17 10:09:49 -0800583 drmFree(U642VOID(conn.encoders_ptr));
584
585 return r;
586}
587
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000588drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
589{
590 return _drmModeGetConnector(fd, connector_id, 1);
591}
592
593drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
594{
595 return _drmModeGetConnector(fd, connector_id, 0);
596}
597
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100598int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800599{
600 struct drm_mode_mode_cmd res;
601
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100602 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800603 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
604 res.connector_id = connector_id;
605
Chris Wilsonb8039182010-07-01 22:38:54 +0100606 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800607}
608
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100609int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800610{
611 struct drm_mode_mode_cmd res;
612
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100613 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800614 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
615 res.connector_id = connector_id;
616
Chris Wilsonb8039182010-07-01 22:38:54 +0100617 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800618}
619
Jesse Barnes731cd552008-12-17 10:09:49 -0800620drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
621{
622 struct drm_mode_get_property prop;
623 drmModePropertyPtr r;
624
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100625 memclear(prop);
Jesse Barnes731cd552008-12-17 10:09:49 -0800626 prop.prop_id = property_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800627
628 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
629 return 0;
630
631 if (prop.count_values)
632 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
633
Rob Clark7b228e92012-06-05 12:28:22 -0500634 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800635 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
636
637 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
638 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
639 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
640 }
641
642 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
643 r = NULL;
644 goto err_allocs;
645 }
646
647 if (!(r = drmMalloc(sizeof(*r))))
648 return NULL;
649
650 r->prop_id = prop.prop_id;
651 r->count_values = prop.count_values;
652
653 r->flags = prop.flags;
654 if (prop.count_values)
655 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
Rob Clark7b228e92012-06-05 12:28:22 -0500656 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800657 r->count_enums = prop.count_enum_blobs;
658 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
659 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
660 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
661 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
662 r->count_blobs = prop.count_enum_blobs;
663 }
664 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
665 r->name[DRM_PROP_NAME_LEN-1] = 0;
666
667err_allocs:
668 drmFree(U642VOID(prop.values_ptr));
669 drmFree(U642VOID(prop.enum_blob_ptr));
670
671 return r;
672}
673
674void drmModeFreeProperty(drmModePropertyPtr ptr)
675{
676 if (!ptr)
677 return;
678
679 drmFree(ptr->values);
680 drmFree(ptr->enums);
681 drmFree(ptr);
682}
683
684drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
685{
686 struct drm_mode_get_blob blob;
687 drmModePropertyBlobPtr r;
688
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100689 memclear(blob);
Jesse Barnes731cd552008-12-17 10:09:49 -0800690 blob.blob_id = blob_id;
691
692 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
693 return NULL;
694
695 if (blob.length)
696 blob.data = VOID2U64(drmMalloc(blob.length));
697
698 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
699 r = NULL;
700 goto err_allocs;
701 }
702
703 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100704 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800705
706 r->id = blob.blob_id;
707 r->length = blob.length;
708 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
709
710err_allocs:
711 drmFree(U642VOID(blob.data));
712 return r;
713}
714
715void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
716{
717 if (!ptr)
718 return;
719
720 drmFree(ptr->data);
721 drmFree(ptr);
722}
723
724int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
725 uint64_t value)
726{
727 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800728
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100729 memclear(osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800730 osp.connector_id = connector_id;
731 osp.prop_id = property_id;
732 osp.value = value;
733
Chris Wilsonb8039182010-07-01 22:38:54 +0100734 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800735}
736
737/*
738 * checks if a modesetting capable driver has attached to the pci id
739 * returns 0 if modesetting supported.
740 * -EINVAL or invalid bus id
741 * -ENOSYS if no modesetting support
742*/
743int drmCheckModesettingSupported(const char *busid)
744{
Robert Millancbb31f22014-01-23 14:46:05 +0000745#if defined (__linux__)
Jesse Barnes731cd552008-12-17 10:09:49 -0800746 char pci_dev_dir[1024];
747 int domain, bus, dev, func;
748 DIR *sysdir;
749 struct dirent *dent;
750 int found = 0, ret;
751
752 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
753 if (ret != 4)
754 return -EINVAL;
755
756 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
757 domain, bus, dev, func);
758
759 sysdir = opendir(pci_dev_dir);
760 if (sysdir) {
761 dent = readdir(sysdir);
762 while (dent) {
763 if (!strncmp(dent->d_name, "controlD", 8)) {
764 found = 1;
765 break;
766 }
767
768 dent = readdir(sysdir);
769 }
770 closedir(sysdir);
771 if (found)
772 return 0;
773 }
774
775 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
776 domain, bus, dev, func);
777
778 sysdir = opendir(pci_dev_dir);
779 if (!sysdir)
780 return -EINVAL;
781
782 dent = readdir(sysdir);
783 while (dent) {
784 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
785 found = 1;
786 break;
787 }
788
789 dent = readdir(sysdir);
790 }
791
792 closedir(sysdir);
793 if (found)
794 return 0;
Robert Millancbb31f22014-01-23 14:46:05 +0000795#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
796 char kbusid[1024], sbusid[1024];
797 char oid[128];
798 int domain, bus, dev, func;
799 int i, modesetting, ret;
800 size_t len;
801
802 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
803 &func);
804 if (ret != 4)
805 return -EINVAL;
806 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
807 dev, func);
808
809 /* How many GPUs do we expect in the machine ? */
810 for (i = 0; i < 16; i++) {
811 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
812 len = sizeof(sbusid);
813 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
814 if (ret == -1) {
815 if (errno == ENOENT)
816 continue;
817 return -EINVAL;
818 }
819 if (strcmp(sbusid, kbusid) != 0)
820 continue;
821 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
822 len = sizeof(modesetting);
823 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
824 if (ret == -1 || len != sizeof(modesetting))
825 return -EINVAL;
826 return (modesetting ? 0 : -ENOSYS);
827 }
François Tigeotedbb4e52014-07-26 13:39:58 +0200828#elif defined(__DragonFly__)
829 return 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800830#endif
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000831#ifdef __OpenBSD__
832 int fd;
833 struct drm_mode_card_res res;
834 drmModeResPtr r = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800835
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000836 if ((fd = drmOpen(NULL, busid)) < 0)
837 return -EINVAL;
838
839 memset(&res, 0, sizeof(struct drm_mode_card_res));
840
841 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
842 drmClose(fd);
843 return -errno;
844 }
845
846 drmClose(fd);
847 return 0;
848#endif
849 return -ENOSYS;
Jesse Barnes731cd552008-12-17 10:09:49 -0800850}
851
Jesse Barnes731cd552008-12-17 10:09:49 -0800852int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
853 uint16_t *red, uint16_t *green, uint16_t *blue)
854{
Jesse Barnes731cd552008-12-17 10:09:49 -0800855 struct drm_mode_crtc_lut l;
856
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100857 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800858 l.crtc_id = crtc_id;
859 l.gamma_size = size;
860 l.red = VOID2U64(red);
861 l.green = VOID2U64(green);
862 l.blue = VOID2U64(blue);
863
Chris Wilsonb8039182010-07-01 22:38:54 +0100864 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800865}
866
867int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
868 uint16_t *red, uint16_t *green, uint16_t *blue)
869{
Jesse Barnes731cd552008-12-17 10:09:49 -0800870 struct drm_mode_crtc_lut l;
871
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100872 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800873 l.crtc_id = crtc_id;
874 l.gamma_size = size;
875 l.red = VOID2U64(red);
876 l.green = VOID2U64(green);
877 l.blue = VOID2U64(blue);
878
Chris Wilsonb8039182010-07-01 22:38:54 +0100879 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800880}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400881
882int drmHandleEvent(int fd, drmEventContextPtr evctx)
883{
884 char buffer[1024];
885 int len, i;
886 struct drm_event *e;
887 struct drm_event_vblank *vblank;
Keith Packardd4331dd2017-07-01 00:43:15 -0700888 struct drm_event_crtc_sequence *seq;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300889 void *user_data;
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900890
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400891 /* The DRM read semantics guarantees that we always get only
892 * complete events. */
893
894 len = read(fd, buffer, sizeof buffer);
895 if (len == 0)
896 return 0;
Jan Veselyde8532d2014-11-30 12:53:18 -0500897 if (len < (int)sizeof *e)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400898 return -1;
899
900 i = 0;
901 while (i < len) {
Thierry Redingecc2a092015-04-13 11:36:59 +0200902 e = (struct drm_event *)(buffer + i);
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400903 switch (e->type) {
904 case DRM_EVENT_VBLANK:
905 if (evctx->version < 1 ||
906 evctx->vblank_handler == NULL)
907 break;
908 vblank = (struct drm_event_vblank *) e;
909 evctx->vblank_handler(fd,
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900910 vblank->sequence,
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400911 vblank->tv_sec,
912 vblank->tv_usec,
913 U642VOID (vblank->user_data));
914 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500915 case DRM_EVENT_FLIP_COMPLETE:
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500916 vblank = (struct drm_event_vblank *) e;
Ander Conselvan de Oliveira890d43a2015-08-17 16:21:24 +0300917 user_data = U642VOID (vblank->user_data);
918
919 if (evctx->version >= 3 && evctx->page_flip_handler2)
920 evctx->page_flip_handler2(fd,
921 vblank->sequence,
922 vblank->tv_sec,
923 vblank->tv_usec,
924 vblank->crtc_id,
925 user_data);
926 else if (evctx->version >= 2 && evctx->page_flip_handler)
927 evctx->page_flip_handler(fd,
928 vblank->sequence,
929 vblank->tv_sec,
930 vblank->tv_usec,
931 user_data);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500932 break;
Keith Packardd4331dd2017-07-01 00:43:15 -0700933 case DRM_EVENT_CRTC_SEQUENCE:
934 seq = (struct drm_event_crtc_sequence *) e;
935 if (evctx->version >= 4 && evctx->sequence_handler)
936 evctx->sequence_handler(fd,
937 seq->sequence,
938 seq->time_ns,
939 seq->user_data);
940 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400941 default:
942 break;
943 }
944 i += e->length;
945 }
946
947 return 0;
948}
949
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500950int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
951 uint32_t flags, void *user_data)
952{
953 struct drm_mode_crtc_page_flip flip;
954
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100955 memclear(flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500956 flip.fb_id = fb_id;
957 flip.crtc_id = crtc_id;
958 flip.user_data = VOID2U64(user_data);
959 flip.flags = flags;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500960
Chris Wilsonb8039182010-07-01 22:38:54 +0100961 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500962}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700963
Michel Dänzer7dd28472016-06-29 18:07:25 +0900964int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
965 uint32_t flags, void *user_data,
966 uint32_t target_vblank)
967{
968 struct drm_mode_crtc_page_flip_target flip_target;
969
970 memclear(flip_target);
971 flip_target.fb_id = fb_id;
972 flip_target.crtc_id = crtc_id;
973 flip_target.user_data = VOID2U64(user_data);
974 flip_target.flags = flags;
975 flip_target.sequence = target_vblank;
976
977 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
978}
979
Jesse Barnesac168bf2011-04-29 08:53:53 -0700980int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
981 uint32_t fb_id, uint32_t flags,
Daniel Kurtz828c3e82014-05-01 19:56:43 +0800982 int32_t crtc_x, int32_t crtc_y,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700983 uint32_t crtc_w, uint32_t crtc_h,
984 uint32_t src_x, uint32_t src_y,
985 uint32_t src_w, uint32_t src_h)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700986{
987 struct drm_mode_set_plane s;
988
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100989 memclear(s);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700990 s.plane_id = plane_id;
991 s.crtc_id = crtc_id;
992 s.fb_id = fb_id;
993 s.flags = flags;
994 s.crtc_x = crtc_x;
995 s.crtc_y = crtc_y;
996 s.crtc_w = crtc_w;
997 s.crtc_h = crtc_h;
998 s.src_x = src_x;
999 s.src_y = src_y;
1000 s.src_w = src_w;
1001 s.src_h = src_h;
1002
1003 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
1004}
1005
Jesse Barnesac168bf2011-04-29 08:53:53 -07001006drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
1007{
1008 struct drm_mode_get_plane ovr, counts;
1009 drmModePlanePtr r = 0;
1010
1011retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001012 memclear(ovr);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001013 ovr.plane_id = plane_id;
1014 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1015 return 0;
1016
1017 counts = ovr;
1018
1019 if (ovr.count_format_types) {
1020 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
1021 sizeof(uint32_t)));
1022 if (!ovr.format_type_ptr)
1023 goto err_allocs;
1024 }
1025
1026 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
1027 goto err_allocs;
1028
1029 if (counts.count_format_types < ovr.count_format_types) {
1030 drmFree(U642VOID(ovr.format_type_ptr));
1031 goto retry;
1032 }
1033
1034 if (!(r = drmMalloc(sizeof(*r))))
1035 goto err_allocs;
1036
1037 r->count_formats = ovr.count_format_types;
1038 r->plane_id = ovr.plane_id;
1039 r->crtc_id = ovr.crtc_id;
1040 r->fb_id = ovr.fb_id;
1041 r->possible_crtcs = ovr.possible_crtcs;
1042 r->gamma_size = ovr.gamma_size;
1043 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1044 ovr.count_format_types, sizeof(uint32_t));
1045 if (ovr.count_format_types && !r->formats) {
1046 drmFree(r->formats);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001047 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001048 r = 0;
1049 }
1050
1051err_allocs:
1052 drmFree(U642VOID(ovr.format_type_ptr));
1053
1054 return r;
1055}
1056
1057void drmModeFreePlane(drmModePlanePtr ptr)
1058{
1059 if (!ptr)
1060 return;
1061
1062 drmFree(ptr->formats);
1063 drmFree(ptr);
1064}
1065
1066drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1067{
1068 struct drm_mode_get_plane_res res, counts;
1069 drmModePlaneResPtr r = 0;
1070
1071retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001072 memclear(res);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001073 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1074 return 0;
1075
1076 counts = res;
1077
1078 if (res.count_planes) {
1079 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1080 sizeof(uint32_t)));
1081 if (!res.plane_id_ptr)
1082 goto err_allocs;
1083 }
1084
1085 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1086 goto err_allocs;
1087
1088 if (counts.count_planes < res.count_planes) {
1089 drmFree(U642VOID(res.plane_id_ptr));
1090 goto retry;
1091 }
1092
1093 if (!(r = drmMalloc(sizeof(*r))))
1094 goto err_allocs;
1095
1096 r->count_planes = res.count_planes;
1097 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1098 res.count_planes, sizeof(uint32_t));
1099 if (res.count_planes && !r->planes) {
1100 drmFree(r->planes);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001101 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001102 r = 0;
1103 }
1104
1105err_allocs:
1106 drmFree(U642VOID(res.plane_id_ptr));
1107
1108 return r;
1109}
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001110
1111void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1112{
1113 if (!ptr)
1114 return;
1115
1116 drmFree(ptr->planes);
1117 drmFree(ptr);
1118}
Paulo Zanoni8c757032012-05-15 18:38:28 -03001119
1120drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1121 uint32_t object_id,
1122 uint32_t object_type)
1123{
1124 struct drm_mode_obj_get_properties properties;
1125 drmModeObjectPropertiesPtr ret = NULL;
1126 uint32_t count;
1127
1128retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001129 memclear(properties);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001130 properties.obj_id = object_id;
1131 properties.obj_type = object_type;
1132
1133 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1134 return 0;
1135
1136 count = properties.count_props;
1137
1138 if (count) {
1139 properties.props_ptr = VOID2U64(drmMalloc(count *
1140 sizeof(uint32_t)));
1141 if (!properties.props_ptr)
1142 goto err_allocs;
1143 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1144 sizeof(uint64_t)));
1145 if (!properties.prop_values_ptr)
1146 goto err_allocs;
1147 }
1148
1149 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1150 goto err_allocs;
1151
1152 if (count < properties.count_props) {
1153 drmFree(U642VOID(properties.props_ptr));
1154 drmFree(U642VOID(properties.prop_values_ptr));
1155 goto retry;
1156 }
1157 count = properties.count_props;
1158
1159 ret = drmMalloc(sizeof(*ret));
1160 if (!ret)
1161 goto err_allocs;
1162
1163 ret->count_props = count;
1164 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1165 count, sizeof(uint32_t));
1166 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1167 count, sizeof(uint64_t));
1168 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1169 drmFree(ret->props);
1170 drmFree(ret->prop_values);
1171 drmFree(ret);
1172 ret = NULL;
1173 }
1174
1175err_allocs:
1176 drmFree(U642VOID(properties.props_ptr));
1177 drmFree(U642VOID(properties.prop_values_ptr));
1178 return ret;
1179}
1180
1181void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1182{
1183 if (!ptr)
1184 return;
1185 drmFree(ptr->props);
1186 drmFree(ptr->prop_values);
1187 drmFree(ptr);
1188}
1189
1190int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1191 uint32_t property_id, uint64_t value)
1192{
1193 struct drm_mode_obj_set_property prop;
1194
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001195 memclear(prop);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001196 prop.value = value;
1197 prop.prop_id = property_id;
1198 prop.obj_id = object_id;
1199 prop.obj_type = object_type;
1200
1201 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1202}
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001203
1204typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1205
1206struct _drmModeAtomicReqItem {
1207 uint32_t object_id;
1208 uint32_t property_id;
1209 uint64_t value;
1210};
1211
1212struct _drmModeAtomicReq {
1213 uint32_t cursor;
1214 uint32_t size_items;
1215 drmModeAtomicReqItemPtr items;
1216};
1217
1218drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1219{
1220 drmModeAtomicReqPtr req;
1221
1222 req = drmMalloc(sizeof *req);
1223 if (!req)
1224 return NULL;
1225
1226 req->items = NULL;
1227 req->cursor = 0;
1228 req->size_items = 0;
1229
1230 return req;
1231}
1232
1233drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1234{
1235 drmModeAtomicReqPtr new;
1236
Emil Velikoved3c6652015-09-05 17:20:53 +01001237 if (!old)
1238 return NULL;
1239
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001240 new = drmMalloc(sizeof *new);
1241 if (!new)
1242 return NULL;
1243
1244 new->cursor = old->cursor;
1245 new->size_items = old->size_items;
1246
1247 if (old->size_items) {
1248 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1249 if (!new->items) {
1250 free(new);
1251 return NULL;
1252 }
1253 memcpy(new->items, old->items,
1254 old->size_items * sizeof(*new->items));
1255 } else {
1256 new->items = NULL;
1257 }
1258
1259 return new;
1260}
1261
1262int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1263{
Emil Velikoved3c6652015-09-05 17:20:53 +01001264 if (!base)
1265 return -EINVAL;
1266
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001267 if (!augment || augment->cursor == 0)
1268 return 0;
1269
1270 if (base->cursor + augment->cursor >= base->size_items) {
1271 drmModeAtomicReqItemPtr new;
1272 int saved_size = base->size_items;
1273
1274 base->size_items = base->cursor + augment->cursor;
1275 new = realloc(base->items,
1276 base->size_items * sizeof(*base->items));
1277 if (!new) {
1278 base->size_items = saved_size;
1279 return -ENOMEM;
1280 }
1281 base->items = new;
1282 }
1283
1284 memcpy(&base->items[base->cursor], augment->items,
1285 augment->cursor * sizeof(*augment->items));
1286 base->cursor += augment->cursor;
1287
1288 return 0;
1289}
1290
1291int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1292{
Emil Velikoved3c6652015-09-05 17:20:53 +01001293 if (!req)
1294 return -EINVAL;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001295 return req->cursor;
1296}
1297
1298void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1299{
Emil Velikoved3c6652015-09-05 17:20:53 +01001300 if (req)
1301 req->cursor = cursor;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001302}
1303
1304int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1305 uint32_t object_id,
1306 uint32_t property_id,
1307 uint64_t value)
1308{
Emil Velikoved3c6652015-09-05 17:20:53 +01001309 if (!req)
1310 return -EINVAL;
1311
Daniel Stone45eee3f2018-03-07 12:41:12 +00001312 if (object_id == 0 || property_id == 0)
1313 return -EINVAL;
1314
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001315 if (req->cursor >= req->size_items) {
1316 drmModeAtomicReqItemPtr new;
1317
1318 req->size_items += 16;
1319 new = realloc(req->items, req->size_items * sizeof(*req->items));
1320 if (!new) {
1321 req->size_items -= 16;
1322 return -ENOMEM;
1323 }
1324 req->items = new;
1325 }
1326
1327 req->items[req->cursor].object_id = object_id;
1328 req->items[req->cursor].property_id = property_id;
1329 req->items[req->cursor].value = value;
1330 req->cursor++;
1331
1332 return req->cursor;
1333}
1334
1335void drmModeAtomicFree(drmModeAtomicReqPtr req)
1336{
1337 if (!req)
1338 return;
1339
1340 if (req->items)
1341 drmFree(req->items);
1342 drmFree(req);
1343}
1344
1345static int sort_req_list(const void *misc, const void *other)
1346{
1347 const drmModeAtomicReqItem *first = misc;
1348 const drmModeAtomicReqItem *second = other;
1349
1350 if (first->object_id < second->object_id)
1351 return -1;
1352 else if (first->object_id > second->object_id)
1353 return 1;
1354 else
1355 return second->property_id - first->property_id;
1356}
1357
1358int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1359 void *user_data)
1360{
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001361 drmModeAtomicReqPtr sorted;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001362 struct drm_mode_atomic atomic;
1363 uint32_t *objs_ptr = NULL;
1364 uint32_t *count_props_ptr = NULL;
1365 uint32_t *props_ptr = NULL;
1366 uint64_t *prop_values_ptr = NULL;
1367 uint32_t last_obj_id = 0;
1368 uint32_t i;
1369 int obj_idx = -1;
1370 int ret = -1;
1371
Emil Velikoved3c6652015-09-05 17:20:53 +01001372 if (!req)
1373 return -EINVAL;
1374
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001375 if (req->cursor == 0)
1376 return 0;
1377
1378 sorted = drmModeAtomicDuplicate(req);
1379 if (sorted == NULL)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001380 return -ENOMEM;
1381
1382 memclear(atomic);
1383
1384 /* Sort the list by object ID, then by property ID. */
1385 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1386 sort_req_list);
1387
1388 /* Now the list is sorted, eliminate duplicate property sets. */
1389 for (i = 0; i < sorted->cursor; i++) {
1390 if (sorted->items[i].object_id != last_obj_id) {
1391 atomic.count_objs++;
1392 last_obj_id = sorted->items[i].object_id;
1393 }
1394
1395 if (i == sorted->cursor - 1)
1396 continue;
1397
1398 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1399 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1400 continue;
1401
1402 memmove(&sorted->items[i], &sorted->items[i + 1],
1403 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1404 sorted->cursor--;
1405 }
1406
1407 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1408 if (!objs_ptr) {
1409 errno = ENOMEM;
1410 goto out;
1411 }
1412
1413 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1414 if (!count_props_ptr) {
1415 errno = ENOMEM;
1416 goto out;
1417 }
1418
1419 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1420 if (!props_ptr) {
1421 errno = ENOMEM;
1422 goto out;
1423 }
1424
1425 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1426 if (!prop_values_ptr) {
1427 errno = ENOMEM;
1428 goto out;
1429 }
1430
1431 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1432 if (sorted->items[i].object_id != last_obj_id) {
1433 obj_idx++;
1434 objs_ptr[obj_idx] = sorted->items[i].object_id;
1435 last_obj_id = objs_ptr[obj_idx];
1436 }
1437
1438 count_props_ptr[obj_idx]++;
1439 props_ptr[i] = sorted->items[i].property_id;
1440 prop_values_ptr[i] = sorted->items[i].value;
1441
1442 }
1443
1444 atomic.flags = flags;
1445 atomic.objs_ptr = VOID2U64(objs_ptr);
1446 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1447 atomic.props_ptr = VOID2U64(props_ptr);
1448 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1449 atomic.user_data = VOID2U64(user_data);
1450
1451 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1452
1453out:
1454 drmFree(objs_ptr);
1455 drmFree(count_props_ptr);
1456 drmFree(props_ptr);
1457 drmFree(prop_values_ptr);
1458 drmModeAtomicFree(sorted);
1459
1460 return ret;
1461}
Daniel Stone32471b22015-06-22 17:26:03 +01001462
1463int
1464drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1465{
1466 struct drm_mode_create_blob create;
1467 int ret;
1468
1469 if (length >= 0xffffffff)
1470 return -ERANGE;
1471
1472 memclear(create);
1473
1474 create.length = length;
1475 create.data = (uintptr_t) data;
1476 create.blob_id = 0;
1477 *id = 0;
1478
1479 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1480 if (ret != 0)
1481 return ret;
1482
1483 *id = create.blob_id;
1484 return 0;
1485}
1486
1487int
1488drmModeDestroyPropertyBlob(int fd, uint32_t id)
1489{
1490 struct drm_mode_destroy_blob destroy;
1491
1492 memclear(destroy);
1493 destroy.blob_id = id;
1494 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1495}
Keith Packardc4171532017-03-16 18:11:05 -07001496
1497int
1498drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags, uint32_t *lessee_id)
1499{
1500 struct drm_mode_create_lease create;
1501 int ret;
1502
1503 memclear(create);
1504 create.object_ids = (uintptr_t) objects;
1505 create.object_count = num_objects;
1506 create.flags = flags;
1507
1508 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
1509 if (ret == 0) {
1510 *lessee_id = create.lessee_id;
1511 return create.fd;
1512 }
1513 return -errno;
1514}
1515
1516drmModeLesseeListPtr
1517drmModeListLessees(int fd)
1518{
1519 struct drm_mode_list_lessees list;
1520 uint32_t count;
1521 drmModeLesseeListPtr ret;
1522
1523 memclear(list);
1524
1525 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
1526 return NULL;
1527
1528 count = list.count_lessees;
1529 ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
1530 if (!ret)
1531 return NULL;
1532
1533 list.lessees_ptr = VOID2U64(&ret->lessees[0]);
1534 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
1535 drmFree(ret);
1536 return NULL;
1537 }
1538
1539 ret->count = count;
1540 return ret;
1541}
1542
1543drmModeObjectListPtr
1544drmModeGetLease(int fd)
1545{
1546 struct drm_mode_get_lease get;
1547 uint32_t count;
1548 drmModeObjectListPtr ret;
1549
1550 memclear(get);
1551
1552 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
1553 return NULL;
1554
1555 count = get.count_objects;
1556 ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
1557 if (!ret)
1558 return NULL;
1559
1560 get.objects_ptr = VOID2U64(&ret->objects[0]);
1561 if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
1562 drmFree(ret);
1563 return NULL;
1564 }
1565
1566 ret->count = count;
1567 return ret;
1568}
1569
1570int
1571drmModeRevokeLease(int fd, uint32_t lessee_id)
1572{
1573 struct drm_mode_revoke_lease revoke;
1574 int ret;
1575
1576 memclear(revoke);
1577
1578 revoke.lessee_id = lessee_id;
1579
1580 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
1581 if (ret == 0)
1582 return 0;
1583 return -errno;
1584}