blob: 7710061865ee18b76ea3cf93f6046fd9421574a0 [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/*
37 * TODO the types we are after are defined in diffrent headers on diffrent
38 * 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
Jesse Barnesac168bf2011-04-29 08:53:53 -0700273int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
274 uint32_t pixel_format, uint32_t bo_handles[4],
275 uint32_t pitches[4], uint32_t offsets[4],
276 uint32_t *buf_id, uint32_t flags)
277{
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]));
Jesse Barnesac168bf2011-04-29 08:53:53 -0700289
290 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
291 return ret;
292
293 *buf_id = f.fb_id;
294 return 0;
295}
296
Jesse Barnes731cd552008-12-17 10:09:49 -0800297int drmModeRmFB(int fd, uint32_t bufferId)
298{
Chris Wilsonb8039182010-07-01 22:38:54 +0100299 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800300}
301
302drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
303{
304 struct drm_mode_fb_cmd info;
305 drmModeFBPtr r;
306
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100307 memclear(info);
Jesse Barnes731cd552008-12-17 10:09:49 -0800308 info.fb_id = buf;
309
310 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
311 return NULL;
312
313 if (!(r = drmMalloc(sizeof(*r))))
314 return NULL;
315
316 r->fb_id = info.fb_id;
317 r->width = info.width;
318 r->height = info.height;
319 r->pitch = info.pitch;
320 r->bpp = info.bpp;
321 r->handle = info.handle;
322 r->depth = info.depth;
323
324 return r;
325}
326
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100327int drmModeDirtyFB(int fd, uint32_t bufferId,
328 drmModeClipPtr clips, uint32_t num_clips)
329{
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100330 struct drm_mode_fb_dirty_cmd dirty;
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100331
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100332 memclear(dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100333 dirty.fb_id = bufferId;
334 dirty.clips_ptr = VOID2U64(clips);
335 dirty.num_clips = num_clips;
336
Chris Wilsonb8039182010-07-01 22:38:54 +0100337 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100338}
339
Jesse Barnes731cd552008-12-17 10:09:49 -0800340/*
341 * Crtc functions
342 */
343
344drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
345{
346 struct drm_mode_crtc crtc;
347 drmModeCrtcPtr r;
348
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100349 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800350 crtc.crtc_id = crtcId;
351
352 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
353 return 0;
354
355 /*
356 * return
357 */
358
359 if (!(r = drmMalloc(sizeof(*r))))
360 return 0;
361
362 r->crtc_id = crtc.crtc_id;
363 r->x = crtc.x;
364 r->y = crtc.y;
365 r->mode_valid = crtc.mode_valid;
Rob Clarke81acf52012-10-14 16:55:32 -0500366 if (r->mode_valid) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800367 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
Rob Clarke81acf52012-10-14 16:55:32 -0500368 r->width = crtc.mode.hdisplay;
369 r->height = crtc.mode.vdisplay;
370 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800371 r->buffer_id = crtc.fb_id;
372 r->gamma_size = crtc.gamma_size;
373 return r;
374}
375
Jesse Barnes731cd552008-12-17 10:09:49 -0800376int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
Thierry Reding5e5a3c42014-04-09 09:00:49 +0200377 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100378 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800379{
380 struct drm_mode_crtc crtc;
381
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100382 memclear(crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800383 crtc.x = x;
384 crtc.y = y;
385 crtc.crtc_id = crtcId;
386 crtc.fb_id = bufferId;
387 crtc.set_connectors_ptr = VOID2U64(connectors);
388 crtc.count_connectors = count;
389 if (mode) {
390 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
391 crtc.mode_valid = 1;
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100392 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800393
Chris Wilsonb8039182010-07-01 22:38:54 +0100394 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800395}
396
397/*
398 * Cursor manipulation
399 */
400
401int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
402{
403 struct drm_mode_cursor arg;
404
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100405 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800406 arg.flags = DRM_MODE_CURSOR_BO;
407 arg.crtc_id = crtcId;
408 arg.width = width;
409 arg.height = height;
410 arg.handle = bo_handle;
411
Chris Wilsonb8039182010-07-01 22:38:54 +0100412 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800413}
414
Dave Airlie2e0ab622013-07-02 09:21:06 +0100415int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height, int32_t hot_x, int32_t hot_y)
416{
417 struct drm_mode_cursor2 arg;
418
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100419 memclear(arg);
Dave Airlie2e0ab622013-07-02 09:21:06 +0100420 arg.flags = DRM_MODE_CURSOR_BO;
421 arg.crtc_id = crtcId;
422 arg.width = width;
423 arg.height = height;
424 arg.handle = bo_handle;
425 arg.hot_x = hot_x;
426 arg.hot_y = hot_y;
427
428 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
429}
430
Jesse Barnes731cd552008-12-17 10:09:49 -0800431int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
432{
433 struct drm_mode_cursor arg;
434
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100435 memclear(arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800436 arg.flags = DRM_MODE_CURSOR_MOVE;
437 arg.crtc_id = crtcId;
438 arg.x = x;
439 arg.y = y;
440
Chris Wilsonb8039182010-07-01 22:38:54 +0100441 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800442}
443
444/*
445 * Encoder get
446 */
447drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
448{
449 struct drm_mode_get_encoder enc;
450 drmModeEncoderPtr r = NULL;
451
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100452 memclear(enc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800453 enc.encoder_id = encoder_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800454
455 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
456 return 0;
457
458 if (!(r = drmMalloc(sizeof(*r))))
459 return 0;
460
461 r->encoder_id = enc.encoder_id;
462 r->crtc_id = enc.crtc_id;
463 r->encoder_type = enc.encoder_type;
464 r->possible_crtcs = enc.possible_crtcs;
465 r->possible_clones = enc.possible_clones;
466
467 return r;
468}
469
470/*
471 * Connector manipulation
472 */
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000473static drmModeConnectorPtr
474_drmModeGetConnector(int fd, uint32_t connector_id, int probe)
Jesse Barnes731cd552008-12-17 10:09:49 -0800475{
Peter Clifton04f90a42010-01-06 20:44:11 +0000476 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800477 drmModeConnectorPtr r = NULL;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200478 struct drm_mode_modeinfo stack_mode;
Jesse Barnes731cd552008-12-17 10:09:49 -0800479
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100480 memclear(conn);
Jesse Barnes731cd552008-12-17 10:09:49 -0800481 conn.connector_id = connector_id;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000482 if (!probe) {
483 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200484 conn.modes_ptr = VOID2U64(&stack_mode);
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000485 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800486
487 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
488 return 0;
489
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000490retry:
Peter Clifton04f90a42010-01-06 20:44:11 +0000491 counts = conn;
492
Jesse Barnes731cd552008-12-17 10:09:49 -0800493 if (conn.count_props) {
494 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000495 if (!conn.props_ptr)
496 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800497 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000498 if (!conn.prop_values_ptr)
499 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800500 }
501
Peter Clifton04f90a42010-01-06 20:44:11 +0000502 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800503 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000504 if (!conn.modes_ptr)
505 goto err_allocs;
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000506 } else {
507 conn.count_modes = 1;
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200508 conn.modes_ptr = VOID2U64(&stack_mode);
Peter Clifton04f90a42010-01-06 20:44:11 +0000509 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800510
Peter Clifton04f90a42010-01-06 20:44:11 +0000511 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800512 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000513 if (!conn.encoders_ptr)
514 goto err_allocs;
515 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800516
517 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
518 goto err_allocs;
519
Peter Clifton04f90a42010-01-06 20:44:11 +0000520 /* The number of available connectors and etc may have changed with a
521 * hotplug event in between the ioctls, in which case the field is
522 * silently ignored by the kernel.
523 */
524 if (counts.count_props < conn.count_props ||
525 counts.count_modes < conn.count_modes ||
526 counts.count_encoders < conn.count_encoders) {
527 drmFree(U642VOID(conn.props_ptr));
528 drmFree(U642VOID(conn.prop_values_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200529 if (U642VOID(conn.modes_ptr) != &stack_mode)
530 drmFree(U642VOID(conn.modes_ptr));
Peter Clifton04f90a42010-01-06 20:44:11 +0000531 drmFree(U642VOID(conn.encoders_ptr));
532
533 goto retry;
534 }
535
Jesse Barnes731cd552008-12-17 10:09:49 -0800536 if(!(r = drmMalloc(sizeof(*r)))) {
537 goto err_allocs;
538 }
539
540 r->connector_id = conn.connector_id;
541 r->encoder_id = conn.encoder_id;
542 r->connection = conn.connection;
543 r->mmWidth = conn.mm_width;
544 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000545 /* convert subpixel from kernel to userspace */
546 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800547 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800548 r->count_props = conn.count_props;
549 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
550 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
551 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
552 r->count_encoders = conn.count_encoders;
553 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
554 r->connector_type = conn.connector_type;
555 r->connector_type_id = conn.connector_type_id;
556
Peter Clifton04f90a42010-01-06 20:44:11 +0000557 if ((r->count_props && !r->props) ||
558 (r->count_props && !r->prop_values) ||
559 (r->count_modes && !r->modes) ||
560 (r->count_encoders && !r->encoders)) {
561 drmFree(r->props);
562 drmFree(r->prop_values);
563 drmFree(r->modes);
564 drmFree(r->encoders);
565 drmFree(r);
566 r = 0;
567 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800568
569err_allocs:
570 drmFree(U642VOID(conn.prop_values_ptr));
571 drmFree(U642VOID(conn.props_ptr));
Ville Syrjäläe342c0f2015-12-15 14:18:32 +0200572 if (U642VOID(conn.modes_ptr) != &stack_mode)
573 drmFree(U642VOID(conn.modes_ptr));
Jesse Barnes731cd552008-12-17 10:09:49 -0800574 drmFree(U642VOID(conn.encoders_ptr));
575
576 return r;
577}
578
Chris Wilson5ed5fa12015-03-04 10:07:19 +0000579drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
580{
581 return _drmModeGetConnector(fd, connector_id, 1);
582}
583
584drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
585{
586 return _drmModeGetConnector(fd, connector_id, 0);
587}
588
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100589int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800590{
591 struct drm_mode_mode_cmd res;
592
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100593 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800594 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
595 res.connector_id = connector_id;
596
Chris Wilsonb8039182010-07-01 22:38:54 +0100597 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800598}
599
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100600int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800601{
602 struct drm_mode_mode_cmd res;
603
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100604 memclear(res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800605 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
606 res.connector_id = connector_id;
607
Chris Wilsonb8039182010-07-01 22:38:54 +0100608 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800609}
610
Jesse Barnes731cd552008-12-17 10:09:49 -0800611drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
612{
613 struct drm_mode_get_property prop;
614 drmModePropertyPtr r;
615
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100616 memclear(prop);
Jesse Barnes731cd552008-12-17 10:09:49 -0800617 prop.prop_id = property_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800618
619 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
620 return 0;
621
622 if (prop.count_values)
623 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
624
Rob Clark7b228e92012-06-05 12:28:22 -0500625 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800626 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
627
628 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
629 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
630 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
631 }
632
633 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
634 r = NULL;
635 goto err_allocs;
636 }
637
638 if (!(r = drmMalloc(sizeof(*r))))
639 return NULL;
640
641 r->prop_id = prop.prop_id;
642 r->count_values = prop.count_values;
643
644 r->flags = prop.flags;
645 if (prop.count_values)
646 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
Rob Clark7b228e92012-06-05 12:28:22 -0500647 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800648 r->count_enums = prop.count_enum_blobs;
649 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
650 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
651 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
652 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
653 r->count_blobs = prop.count_enum_blobs;
654 }
655 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
656 r->name[DRM_PROP_NAME_LEN-1] = 0;
657
658err_allocs:
659 drmFree(U642VOID(prop.values_ptr));
660 drmFree(U642VOID(prop.enum_blob_ptr));
661
662 return r;
663}
664
665void drmModeFreeProperty(drmModePropertyPtr ptr)
666{
667 if (!ptr)
668 return;
669
670 drmFree(ptr->values);
671 drmFree(ptr->enums);
672 drmFree(ptr);
673}
674
675drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
676{
677 struct drm_mode_get_blob blob;
678 drmModePropertyBlobPtr r;
679
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100680 memclear(blob);
Jesse Barnes731cd552008-12-17 10:09:49 -0800681 blob.blob_id = blob_id;
682
683 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
684 return NULL;
685
686 if (blob.length)
687 blob.data = VOID2U64(drmMalloc(blob.length));
688
689 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
690 r = NULL;
691 goto err_allocs;
692 }
693
694 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100695 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800696
697 r->id = blob.blob_id;
698 r->length = blob.length;
699 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
700
701err_allocs:
702 drmFree(U642VOID(blob.data));
703 return r;
704}
705
706void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
707{
708 if (!ptr)
709 return;
710
711 drmFree(ptr->data);
712 drmFree(ptr);
713}
714
715int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
716 uint64_t value)
717{
718 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800719
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100720 memclear(osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800721 osp.connector_id = connector_id;
722 osp.prop_id = property_id;
723 osp.value = value;
724
Chris Wilsonb8039182010-07-01 22:38:54 +0100725 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800726}
727
728/*
729 * checks if a modesetting capable driver has attached to the pci id
730 * returns 0 if modesetting supported.
731 * -EINVAL or invalid bus id
732 * -ENOSYS if no modesetting support
733*/
734int drmCheckModesettingSupported(const char *busid)
735{
Robert Millancbb31f22014-01-23 14:46:05 +0000736#if defined (__linux__)
Jesse Barnes731cd552008-12-17 10:09:49 -0800737 char pci_dev_dir[1024];
738 int domain, bus, dev, func;
739 DIR *sysdir;
740 struct dirent *dent;
741 int found = 0, ret;
742
743 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
744 if (ret != 4)
745 return -EINVAL;
746
747 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
748 domain, bus, dev, func);
749
750 sysdir = opendir(pci_dev_dir);
751 if (sysdir) {
752 dent = readdir(sysdir);
753 while (dent) {
754 if (!strncmp(dent->d_name, "controlD", 8)) {
755 found = 1;
756 break;
757 }
758
759 dent = readdir(sysdir);
760 }
761 closedir(sysdir);
762 if (found)
763 return 0;
764 }
765
766 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
767 domain, bus, dev, func);
768
769 sysdir = opendir(pci_dev_dir);
770 if (!sysdir)
771 return -EINVAL;
772
773 dent = readdir(sysdir);
774 while (dent) {
775 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
776 found = 1;
777 break;
778 }
779
780 dent = readdir(sysdir);
781 }
782
783 closedir(sysdir);
784 if (found)
785 return 0;
Robert Millancbb31f22014-01-23 14:46:05 +0000786#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
787 char kbusid[1024], sbusid[1024];
788 char oid[128];
789 int domain, bus, dev, func;
790 int i, modesetting, ret;
791 size_t len;
792
793 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
794 &func);
795 if (ret != 4)
796 return -EINVAL;
797 snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
798 dev, func);
799
800 /* How many GPUs do we expect in the machine ? */
801 for (i = 0; i < 16; i++) {
802 snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
803 len = sizeof(sbusid);
804 ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
805 if (ret == -1) {
806 if (errno == ENOENT)
807 continue;
808 return -EINVAL;
809 }
810 if (strcmp(sbusid, kbusid) != 0)
811 continue;
812 snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
813 len = sizeof(modesetting);
814 ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
815 if (ret == -1 || len != sizeof(modesetting))
816 return -EINVAL;
817 return (modesetting ? 0 : -ENOSYS);
818 }
François Tigeotedbb4e52014-07-26 13:39:58 +0200819#elif defined(__DragonFly__)
820 return 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800821#endif
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000822#ifdef __OpenBSD__
823 int fd;
824 struct drm_mode_card_res res;
825 drmModeResPtr r = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800826
Jonathan Gray1d3b8232015-07-19 07:20:37 +1000827 if ((fd = drmOpen(NULL, busid)) < 0)
828 return -EINVAL;
829
830 memset(&res, 0, sizeof(struct drm_mode_card_res));
831
832 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
833 drmClose(fd);
834 return -errno;
835 }
836
837 drmClose(fd);
838 return 0;
839#endif
840 return -ENOSYS;
Jesse Barnes731cd552008-12-17 10:09:49 -0800841}
842
Jesse Barnes731cd552008-12-17 10:09:49 -0800843int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
844 uint16_t *red, uint16_t *green, uint16_t *blue)
845{
Jesse Barnes731cd552008-12-17 10:09:49 -0800846 struct drm_mode_crtc_lut l;
847
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100848 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800849 l.crtc_id = crtc_id;
850 l.gamma_size = size;
851 l.red = VOID2U64(red);
852 l.green = VOID2U64(green);
853 l.blue = VOID2U64(blue);
854
Chris Wilsonb8039182010-07-01 22:38:54 +0100855 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800856}
857
858int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
859 uint16_t *red, uint16_t *green, uint16_t *blue)
860{
Jesse Barnes731cd552008-12-17 10:09:49 -0800861 struct drm_mode_crtc_lut l;
862
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100863 memclear(l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800864 l.crtc_id = crtc_id;
865 l.gamma_size = size;
866 l.red = VOID2U64(red);
867 l.green = VOID2U64(green);
868 l.blue = VOID2U64(blue);
869
Chris Wilsonb8039182010-07-01 22:38:54 +0100870 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800871}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400872
873int drmHandleEvent(int fd, drmEventContextPtr evctx)
874{
875 char buffer[1024];
876 int len, i;
877 struct drm_event *e;
878 struct drm_event_vblank *vblank;
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900879
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400880 /* The DRM read semantics guarantees that we always get only
881 * complete events. */
882
883 len = read(fd, buffer, sizeof buffer);
884 if (len == 0)
885 return 0;
Jan Veselyde8532d2014-11-30 12:53:18 -0500886 if (len < (int)sizeof *e)
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400887 return -1;
888
889 i = 0;
890 while (i < len) {
891 e = (struct drm_event *) &buffer[i];
892 switch (e->type) {
893 case DRM_EVENT_VBLANK:
894 if (evctx->version < 1 ||
895 evctx->vblank_handler == NULL)
896 break;
897 vblank = (struct drm_event_vblank *) e;
898 evctx->vblank_handler(fd,
Hyungwon Hwang4bac0352015-08-19 09:58:39 +0900899 vblank->sequence,
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400900 vblank->tv_sec,
901 vblank->tv_usec,
902 U642VOID (vblank->user_data));
903 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500904 case DRM_EVENT_FLIP_COMPLETE:
Jesse Barnes14f59582009-12-03 14:20:51 -0800905 if (evctx->version < 2 ||
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500906 evctx->page_flip_handler == NULL)
907 break;
908 vblank = (struct drm_event_vblank *) e;
909 evctx->page_flip_handler(fd,
910 vblank->sequence,
911 vblank->tv_sec,
912 vblank->tv_usec,
913 U642VOID (vblank->user_data));
914 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400915 default:
916 break;
917 }
918 i += e->length;
919 }
920
921 return 0;
922}
923
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500924int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
925 uint32_t flags, void *user_data)
926{
927 struct drm_mode_crtc_page_flip flip;
928
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100929 memclear(flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500930 flip.fb_id = fb_id;
931 flip.crtc_id = crtc_id;
932 flip.user_data = VOID2U64(user_data);
933 flip.flags = flags;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500934
Chris Wilsonb8039182010-07-01 22:38:54 +0100935 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500936}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700937
938int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
939 uint32_t fb_id, uint32_t flags,
Daniel Kurtz828c3e82014-05-01 19:56:43 +0800940 int32_t crtc_x, int32_t crtc_y,
Jesse Barnesac168bf2011-04-29 08:53:53 -0700941 uint32_t crtc_w, uint32_t crtc_h,
942 uint32_t src_x, uint32_t src_y,
943 uint32_t src_w, uint32_t src_h)
Jesse Barnesac168bf2011-04-29 08:53:53 -0700944{
945 struct drm_mode_set_plane s;
946
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100947 memclear(s);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700948 s.plane_id = plane_id;
949 s.crtc_id = crtc_id;
950 s.fb_id = fb_id;
951 s.flags = flags;
952 s.crtc_x = crtc_x;
953 s.crtc_y = crtc_y;
954 s.crtc_w = crtc_w;
955 s.crtc_h = crtc_h;
956 s.src_x = src_x;
957 s.src_y = src_y;
958 s.src_w = src_w;
959 s.src_h = src_h;
960
961 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
962}
963
Jesse Barnesac168bf2011-04-29 08:53:53 -0700964drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
965{
966 struct drm_mode_get_plane ovr, counts;
967 drmModePlanePtr r = 0;
968
969retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +0100970 memclear(ovr);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700971 ovr.plane_id = plane_id;
972 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
973 return 0;
974
975 counts = ovr;
976
977 if (ovr.count_format_types) {
978 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
979 sizeof(uint32_t)));
980 if (!ovr.format_type_ptr)
981 goto err_allocs;
982 }
983
984 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
985 goto err_allocs;
986
987 if (counts.count_format_types < ovr.count_format_types) {
988 drmFree(U642VOID(ovr.format_type_ptr));
989 goto retry;
990 }
991
992 if (!(r = drmMalloc(sizeof(*r))))
993 goto err_allocs;
994
995 r->count_formats = ovr.count_format_types;
996 r->plane_id = ovr.plane_id;
997 r->crtc_id = ovr.crtc_id;
998 r->fb_id = ovr.fb_id;
999 r->possible_crtcs = ovr.possible_crtcs;
1000 r->gamma_size = ovr.gamma_size;
1001 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
1002 ovr.count_format_types, sizeof(uint32_t));
1003 if (ovr.count_format_types && !r->formats) {
1004 drmFree(r->formats);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001005 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001006 r = 0;
1007 }
1008
1009err_allocs:
1010 drmFree(U642VOID(ovr.format_type_ptr));
1011
1012 return r;
1013}
1014
1015void drmModeFreePlane(drmModePlanePtr ptr)
1016{
1017 if (!ptr)
1018 return;
1019
1020 drmFree(ptr->formats);
1021 drmFree(ptr);
1022}
1023
1024drmModePlaneResPtr drmModeGetPlaneResources(int fd)
1025{
1026 struct drm_mode_get_plane_res res, counts;
1027 drmModePlaneResPtr r = 0;
1028
1029retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001030 memclear(res);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001031 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1032 return 0;
1033
1034 counts = res;
1035
1036 if (res.count_planes) {
1037 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
1038 sizeof(uint32_t)));
1039 if (!res.plane_id_ptr)
1040 goto err_allocs;
1041 }
1042
1043 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
1044 goto err_allocs;
1045
1046 if (counts.count_planes < res.count_planes) {
1047 drmFree(U642VOID(res.plane_id_ptr));
1048 goto retry;
1049 }
1050
1051 if (!(r = drmMalloc(sizeof(*r))))
1052 goto err_allocs;
1053
1054 r->count_planes = res.count_planes;
1055 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
1056 res.count_planes, sizeof(uint32_t));
1057 if (res.count_planes && !r->planes) {
1058 drmFree(r->planes);
Ville Syrjälädf497e92012-02-02 14:53:39 -05001059 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -07001060 r = 0;
1061 }
1062
1063err_allocs:
1064 drmFree(U642VOID(res.plane_id_ptr));
1065
1066 return r;
1067}
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -05001068
1069void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
1070{
1071 if (!ptr)
1072 return;
1073
1074 drmFree(ptr->planes);
1075 drmFree(ptr);
1076}
Paulo Zanoni8c757032012-05-15 18:38:28 -03001077
1078drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
1079 uint32_t object_id,
1080 uint32_t object_type)
1081{
1082 struct drm_mode_obj_get_properties properties;
1083 drmModeObjectPropertiesPtr ret = NULL;
1084 uint32_t count;
1085
1086retry:
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001087 memclear(properties);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001088 properties.obj_id = object_id;
1089 properties.obj_type = object_type;
1090
1091 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1092 return 0;
1093
1094 count = properties.count_props;
1095
1096 if (count) {
1097 properties.props_ptr = VOID2U64(drmMalloc(count *
1098 sizeof(uint32_t)));
1099 if (!properties.props_ptr)
1100 goto err_allocs;
1101 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1102 sizeof(uint64_t)));
1103 if (!properties.prop_values_ptr)
1104 goto err_allocs;
1105 }
1106
1107 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1108 goto err_allocs;
1109
1110 if (count < properties.count_props) {
1111 drmFree(U642VOID(properties.props_ptr));
1112 drmFree(U642VOID(properties.prop_values_ptr));
1113 goto retry;
1114 }
1115 count = properties.count_props;
1116
1117 ret = drmMalloc(sizeof(*ret));
1118 if (!ret)
1119 goto err_allocs;
1120
1121 ret->count_props = count;
1122 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1123 count, sizeof(uint32_t));
1124 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1125 count, sizeof(uint64_t));
1126 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1127 drmFree(ret->props);
1128 drmFree(ret->prop_values);
1129 drmFree(ret);
1130 ret = NULL;
1131 }
1132
1133err_allocs:
1134 drmFree(U642VOID(properties.props_ptr));
1135 drmFree(U642VOID(properties.prop_values_ptr));
1136 return ret;
1137}
1138
1139void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1140{
1141 if (!ptr)
1142 return;
1143 drmFree(ptr->props);
1144 drmFree(ptr->prop_values);
1145 drmFree(ptr);
1146}
1147
1148int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1149 uint32_t property_id, uint64_t value)
1150{
1151 struct drm_mode_obj_set_property prop;
1152
Daniel Vetter7e0460c2015-02-11 12:03:12 +01001153 memclear(prop);
Paulo Zanoni8c757032012-05-15 18:38:28 -03001154 prop.value = value;
1155 prop.prop_id = property_id;
1156 prop.obj_id = object_id;
1157 prop.obj_type = object_type;
1158
1159 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1160}
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001161
1162typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
1163
1164struct _drmModeAtomicReqItem {
1165 uint32_t object_id;
1166 uint32_t property_id;
1167 uint64_t value;
1168};
1169
1170struct _drmModeAtomicReq {
1171 uint32_t cursor;
1172 uint32_t size_items;
1173 drmModeAtomicReqItemPtr items;
1174};
1175
1176drmModeAtomicReqPtr drmModeAtomicAlloc(void)
1177{
1178 drmModeAtomicReqPtr req;
1179
1180 req = drmMalloc(sizeof *req);
1181 if (!req)
1182 return NULL;
1183
1184 req->items = NULL;
1185 req->cursor = 0;
1186 req->size_items = 0;
1187
1188 return req;
1189}
1190
1191drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
1192{
1193 drmModeAtomicReqPtr new;
1194
Emil Velikoved3c6652015-09-05 17:20:53 +01001195 if (!old)
1196 return NULL;
1197
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001198 new = drmMalloc(sizeof *new);
1199 if (!new)
1200 return NULL;
1201
1202 new->cursor = old->cursor;
1203 new->size_items = old->size_items;
1204
1205 if (old->size_items) {
1206 new->items = drmMalloc(old->size_items * sizeof(*new->items));
1207 if (!new->items) {
1208 free(new);
1209 return NULL;
1210 }
1211 memcpy(new->items, old->items,
1212 old->size_items * sizeof(*new->items));
1213 } else {
1214 new->items = NULL;
1215 }
1216
1217 return new;
1218}
1219
1220int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
1221{
Emil Velikoved3c6652015-09-05 17:20:53 +01001222 if (!base)
1223 return -EINVAL;
1224
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001225 if (!augment || augment->cursor == 0)
1226 return 0;
1227
1228 if (base->cursor + augment->cursor >= base->size_items) {
1229 drmModeAtomicReqItemPtr new;
1230 int saved_size = base->size_items;
1231
1232 base->size_items = base->cursor + augment->cursor;
1233 new = realloc(base->items,
1234 base->size_items * sizeof(*base->items));
1235 if (!new) {
1236 base->size_items = saved_size;
1237 return -ENOMEM;
1238 }
1239 base->items = new;
1240 }
1241
1242 memcpy(&base->items[base->cursor], augment->items,
1243 augment->cursor * sizeof(*augment->items));
1244 base->cursor += augment->cursor;
1245
1246 return 0;
1247}
1248
1249int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
1250{
Emil Velikoved3c6652015-09-05 17:20:53 +01001251 if (!req)
1252 return -EINVAL;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001253 return req->cursor;
1254}
1255
1256void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
1257{
Emil Velikoved3c6652015-09-05 17:20:53 +01001258 if (req)
1259 req->cursor = cursor;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001260}
1261
1262int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
1263 uint32_t object_id,
1264 uint32_t property_id,
1265 uint64_t value)
1266{
Emil Velikoved3c6652015-09-05 17:20:53 +01001267 if (!req)
1268 return -EINVAL;
1269
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001270 if (req->cursor >= req->size_items) {
1271 drmModeAtomicReqItemPtr new;
1272
1273 req->size_items += 16;
1274 new = realloc(req->items, req->size_items * sizeof(*req->items));
1275 if (!new) {
1276 req->size_items -= 16;
1277 return -ENOMEM;
1278 }
1279 req->items = new;
1280 }
1281
1282 req->items[req->cursor].object_id = object_id;
1283 req->items[req->cursor].property_id = property_id;
1284 req->items[req->cursor].value = value;
1285 req->cursor++;
1286
1287 return req->cursor;
1288}
1289
1290void drmModeAtomicFree(drmModeAtomicReqPtr req)
1291{
1292 if (!req)
1293 return;
1294
1295 if (req->items)
1296 drmFree(req->items);
1297 drmFree(req);
1298}
1299
1300static int sort_req_list(const void *misc, const void *other)
1301{
1302 const drmModeAtomicReqItem *first = misc;
1303 const drmModeAtomicReqItem *second = other;
1304
1305 if (first->object_id < second->object_id)
1306 return -1;
1307 else if (first->object_id > second->object_id)
1308 return 1;
1309 else
1310 return second->property_id - first->property_id;
1311}
1312
1313int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
1314 void *user_data)
1315{
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001316 drmModeAtomicReqPtr sorted;
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001317 struct drm_mode_atomic atomic;
1318 uint32_t *objs_ptr = NULL;
1319 uint32_t *count_props_ptr = NULL;
1320 uint32_t *props_ptr = NULL;
1321 uint64_t *prop_values_ptr = NULL;
1322 uint32_t last_obj_id = 0;
1323 uint32_t i;
1324 int obj_idx = -1;
1325 int ret = -1;
1326
Emil Velikoved3c6652015-09-05 17:20:53 +01001327 if (!req)
1328 return -EINVAL;
1329
Chris Wilson1a6efaf2015-07-21 13:46:45 +01001330 if (req->cursor == 0)
1331 return 0;
1332
1333 sorted = drmModeAtomicDuplicate(req);
1334 if (sorted == NULL)
Ville Syrjäläed44e0b2015-06-22 17:26:02 +01001335 return -ENOMEM;
1336
1337 memclear(atomic);
1338
1339 /* Sort the list by object ID, then by property ID. */
1340 qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
1341 sort_req_list);
1342
1343 /* Now the list is sorted, eliminate duplicate property sets. */
1344 for (i = 0; i < sorted->cursor; i++) {
1345 if (sorted->items[i].object_id != last_obj_id) {
1346 atomic.count_objs++;
1347 last_obj_id = sorted->items[i].object_id;
1348 }
1349
1350 if (i == sorted->cursor - 1)
1351 continue;
1352
1353 if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
1354 sorted->items[i].property_id != sorted->items[i + 1].property_id)
1355 continue;
1356
1357 memmove(&sorted->items[i], &sorted->items[i + 1],
1358 (sorted->cursor - i - 1) * sizeof(*sorted->items));
1359 sorted->cursor--;
1360 }
1361
1362 objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]);
1363 if (!objs_ptr) {
1364 errno = ENOMEM;
1365 goto out;
1366 }
1367
1368 count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]);
1369 if (!count_props_ptr) {
1370 errno = ENOMEM;
1371 goto out;
1372 }
1373
1374 props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]);
1375 if (!props_ptr) {
1376 errno = ENOMEM;
1377 goto out;
1378 }
1379
1380 prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]);
1381 if (!prop_values_ptr) {
1382 errno = ENOMEM;
1383 goto out;
1384 }
1385
1386 for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
1387 if (sorted->items[i].object_id != last_obj_id) {
1388 obj_idx++;
1389 objs_ptr[obj_idx] = sorted->items[i].object_id;
1390 last_obj_id = objs_ptr[obj_idx];
1391 }
1392
1393 count_props_ptr[obj_idx]++;
1394 props_ptr[i] = sorted->items[i].property_id;
1395 prop_values_ptr[i] = sorted->items[i].value;
1396
1397 }
1398
1399 atomic.flags = flags;
1400 atomic.objs_ptr = VOID2U64(objs_ptr);
1401 atomic.count_props_ptr = VOID2U64(count_props_ptr);
1402 atomic.props_ptr = VOID2U64(props_ptr);
1403 atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
1404 atomic.user_data = VOID2U64(user_data);
1405
1406 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
1407
1408out:
1409 drmFree(objs_ptr);
1410 drmFree(count_props_ptr);
1411 drmFree(props_ptr);
1412 drmFree(prop_values_ptr);
1413 drmModeAtomicFree(sorted);
1414
1415 return ret;
1416}
Daniel Stone32471b22015-06-22 17:26:03 +01001417
1418int
1419drmModeCreatePropertyBlob(int fd, const void *data, size_t length, uint32_t *id)
1420{
1421 struct drm_mode_create_blob create;
1422 int ret;
1423
1424 if (length >= 0xffffffff)
1425 return -ERANGE;
1426
1427 memclear(create);
1428
1429 create.length = length;
1430 create.data = (uintptr_t) data;
1431 create.blob_id = 0;
1432 *id = 0;
1433
1434 ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
1435 if (ret != 0)
1436 return ret;
1437
1438 *id = create.blob_id;
1439 return 0;
1440}
1441
1442int
1443drmModeDestroyPropertyBlob(int fd, uint32_t id)
1444{
1445 struct drm_mode_destroy_blob destroy;
1446
1447 memclear(destroy);
1448 destroy.blob_id = id;
1449 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
1450}