blob: 6f5a98804fe394a14e9947473f8890c970d357c8 [file] [log] [blame]
Daniel Stonedd1bc502019-06-17 12:13:46 +01001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
6 * Copyright (c) 2018 DisplayLink (UK) Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30#include "config.h"
31
32#include <errno.h>
33#include <stdint.h>
34#include <stdlib.h>
35#include <ctype.h>
36#include <string.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <linux/input.h>
40#include <linux/vt.h>
41#include <assert.h>
42#include <sys/mman.h>
43#include <time.h>
44
45
46#include <xf86drm.h>
47#include <xf86drmMode.h>
48#include <drm_fourcc.h>
49
50#include <gbm.h>
51#include <libudev.h>
52
53#include <libweston/libweston.h>
54#include <libweston/backend-drm.h>
Marius Vladc901e892019-06-21 22:49:18 +030055#include <libweston/weston-log.h>
Daniel Stonedd1bc502019-06-17 12:13:46 +010056#include "shared/helpers.h"
57#include "libinput-seat.h"
Marius Vlade41c1bf2019-07-16 23:11:25 +030058#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030059#include "libweston-internal.h"
Daniel Stonedd1bc502019-06-17 12:13:46 +010060
61#ifndef DRM_CLIENT_CAP_ASPECT_RATIO
62#define DRM_CLIENT_CAP_ASPECT_RATIO 4
63#endif
64
65#ifndef GBM_BO_USE_CURSOR
66#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
67#endif
68
69#ifndef GBM_BO_USE_LINEAR
70#define GBM_BO_USE_LINEAR (1 << 4)
71#endif
72
73/**
74 * A small wrapper to print information into the 'drm-backend' debug scope.
75 *
76 * The following conventions are used to print variables:
77 *
78 * - fixed uint32_t values, including Weston object IDs such as weston_output
79 * IDs, DRM object IDs such as CRTCs or properties, and GBM/DRM formats:
80 * "%lu (0x%lx)" (unsigned long) value, (unsigned long) value
81 *
82 * - fixed uint64_t values, such as DRM property values (including object IDs
83 * when used as a value):
84 * "%llu (0x%llx)" (unsigned long long) value, (unsigned long long) value
85 *
86 * - non-fixed-width signed int:
87 * "%d" value
88 *
89 * - non-fixed-width unsigned int:
90 * "%u (0x%x)" value, value
91 *
92 * - non-fixed-width unsigned long:
93 * "%lu (0x%lx)" value, value
94 *
95 * Either the integer or hexadecimal forms may be omitted if it is known that
96 * one representation is not useful (e.g. width/height in hex are rarely what
97 * you want).
98 *
99 * This is to avoid implicit widening or narrowing when we use fixed-size
100 * types: uint32_t can be resolved by either unsigned int or unsigned long
101 * on a 32-bit system but only unsigned int on a 64-bit system, with uint64_t
102 * being unsigned long long on a 32-bit system and unsigned long on a 64-bit
103 * system. To avoid confusing side effects, we explicitly cast to the widest
104 * possible type and use a matching format specifier.
105 */
106#define drm_debug(b, ...) \
107 weston_log_scope_printf((b)->debug, __VA_ARGS__)
108
109#define MAX_CLONED_CONNECTORS 4
110
111/**
112 * aspect ratio info taken from the drmModeModeInfo flag bits 19-22,
113 * which should be used to fill the aspect ratio field in weston_mode.
114 */
115#define DRM_MODE_FLAG_PIC_AR_BITS_POS 19
116#ifndef DRM_MODE_FLAG_PIC_AR_MASK
117#define DRM_MODE_FLAG_PIC_AR_MASK (0xF << DRM_MODE_FLAG_PIC_AR_BITS_POS)
118#endif
119
120/**
121 * Represents the values of an enum-type KMS property
122 */
123struct drm_property_enum_info {
124 const char *name; /**< name as string (static, not freed) */
125 bool valid; /**< true if value is supported; ignore if false */
126 uint64_t value; /**< raw value */
127};
128
129/**
130 * Holds information on a DRM property, including its ID and the enum
131 * values it holds.
132 *
133 * DRM properties are allocated dynamically, and maintained as DRM objects
134 * within the normal object ID space; they thus do not have a stable ID
135 * to refer to. This includes enum values, which must be referred to by
136 * integer values, but these are not stable.
137 *
138 * drm_property_info allows a cache to be maintained where Weston can use
139 * enum values internally to refer to properties, with the mapping to DRM
140 * ID values being maintained internally.
141 */
142struct drm_property_info {
143 const char *name; /**< name as string (static, not freed) */
144 uint32_t prop_id; /**< KMS property object ID */
145 unsigned int num_enum_values; /**< number of enum values */
146 struct drm_property_enum_info *enum_values; /**< array of enum values */
147};
148
149/**
150 * List of properties attached to DRM planes
151 */
152enum wdrm_plane_property {
153 WDRM_PLANE_TYPE = 0,
154 WDRM_PLANE_SRC_X,
155 WDRM_PLANE_SRC_Y,
156 WDRM_PLANE_SRC_W,
157 WDRM_PLANE_SRC_H,
158 WDRM_PLANE_CRTC_X,
159 WDRM_PLANE_CRTC_Y,
160 WDRM_PLANE_CRTC_W,
161 WDRM_PLANE_CRTC_H,
162 WDRM_PLANE_FB_ID,
163 WDRM_PLANE_CRTC_ID,
164 WDRM_PLANE_IN_FORMATS,
165 WDRM_PLANE_IN_FENCE_FD,
166 WDRM_PLANE_FB_DAMAGE_CLIPS,
167 WDRM_PLANE__COUNT
168};
169
170/**
171 * Possible values for the WDRM_PLANE_TYPE property.
172 */
173enum wdrm_plane_type {
174 WDRM_PLANE_TYPE_PRIMARY = 0,
175 WDRM_PLANE_TYPE_CURSOR,
176 WDRM_PLANE_TYPE_OVERLAY,
177 WDRM_PLANE_TYPE__COUNT
178};
179
180/**
181 * List of properties attached to a DRM connector
182 */
183enum wdrm_connector_property {
184 WDRM_CONNECTOR_EDID = 0,
185 WDRM_CONNECTOR_DPMS,
186 WDRM_CONNECTOR_CRTC_ID,
187 WDRM_CONNECTOR_NON_DESKTOP,
188 WDRM_CONNECTOR__COUNT
189};
190
191enum wdrm_dpms_state {
192 WDRM_DPMS_STATE_OFF = 0,
193 WDRM_DPMS_STATE_ON,
194 WDRM_DPMS_STATE_STANDBY, /* unused */
195 WDRM_DPMS_STATE_SUSPEND, /* unused */
196 WDRM_DPMS_STATE__COUNT
197};
198
199/**
200 * List of properties attached to DRM CRTCs
201 */
202enum wdrm_crtc_property {
203 WDRM_CRTC_MODE_ID = 0,
204 WDRM_CRTC_ACTIVE,
205 WDRM_CRTC__COUNT
206};
207
208struct drm_backend {
209 struct weston_backend base;
210 struct weston_compositor *compositor;
211
212 struct udev *udev;
213 struct wl_event_source *drm_source;
214
215 struct udev_monitor *udev_monitor;
216 struct wl_event_source *udev_drm_source;
217
218 struct {
219 int id;
220 int fd;
221 char *filename;
222 dev_t devnum;
223 } drm;
224 struct gbm_device *gbm;
225 struct wl_listener session_listener;
226 uint32_t gbm_format;
227
228 /* we need these parameters in order to not fail drmModeAddFB2()
229 * due to out of bounds dimensions, and then mistakenly set
230 * sprites_are_broken:
231 */
232 int min_width, max_width;
233 int min_height, max_height;
234
235 struct wl_list plane_list;
236 int sprites_are_broken;
237 int sprites_hidden;
238
239 void *repaint_data;
240
241 bool state_invalid;
242
243 /* CRTC IDs not used by any enabled output. */
244 struct wl_array unused_crtcs;
245
246 int cursors_are_broken;
247
248 bool universal_planes;
249 bool atomic_modeset;
250
251 bool use_pixman;
252 bool use_pixman_shadow;
253
254 struct udev_input input;
255
256 int32_t cursor_width;
257 int32_t cursor_height;
258
259 uint32_t pageflip_timeout;
260
261 bool shutting_down;
262
263 bool aspect_ratio_supported;
264
265 bool fb_modifiers;
266
267 struct weston_log_scope *debug;
268};
269
270struct drm_mode {
271 struct weston_mode base;
272 drmModeModeInfo mode_info;
273 uint32_t blob_id;
274};
275
276enum drm_fb_type {
277 BUFFER_INVALID = 0, /**< never used */
278 BUFFER_CLIENT, /**< directly sourced from client */
279 BUFFER_DMABUF, /**< imported from linux_dmabuf client */
280 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
281 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
282 BUFFER_CURSOR, /**< internal cursor buffer */
283};
284
285struct drm_fb {
286 enum drm_fb_type type;
287
288 int refcnt;
289
290 uint32_t fb_id, size;
291 uint32_t handles[4];
292 uint32_t strides[4];
293 uint32_t offsets[4];
294 int num_planes;
295 const struct pixel_format_info *format;
296 uint64_t modifier;
297 int width, height;
298 int fd;
299 struct weston_buffer_reference buffer_ref;
300 struct weston_buffer_release_reference buffer_release_ref;
301
302 /* Used by gbm fbs */
303 struct gbm_bo *bo;
304 struct gbm_surface *gbm_surface;
305
306 /* Used by dumb fbs */
307 void *map;
308};
309
310struct drm_edid {
311 char eisa_id[13];
312 char monitor_name[13];
313 char pnp_id[5];
314 char serial_number[13];
315};
316
317/**
318 * Pending state holds one or more drm_output_state structures, collected from
319 * performing repaint. This pending state is transient, and only lives between
320 * beginning a repaint group and flushing the results: after flush, each
321 * output state will complete and be retired separately.
322 */
323struct drm_pending_state {
324 struct drm_backend *backend;
325 struct wl_list output_list;
326};
327
328/*
329 * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC,
330 * plus >= 1 each of encoder/connector/plane. Since everything but the planes
331 * is currently statically assigned per-output, we mainly use this to track
332 * plane state.
333 *
334 * pending_state is set when the output state is owned by a pending_state,
335 * i.e. when it is being constructed and has not yet been applied. When the
336 * output state has been applied, the owning pending_state is freed.
337 */
338struct drm_output_state {
339 struct drm_pending_state *pending_state;
340 struct drm_output *output;
341 struct wl_list link;
342 enum dpms_enum dpms;
343 struct wl_list plane_list;
344};
345
346/**
347 * Plane state holds the dynamic state for a plane: where it is positioned,
348 * and which buffer it is currently displaying.
349 *
350 * The plane state is owned by an output state, except when setting an initial
351 * state. See drm_output_state for notes on state object lifetime.
352 */
353struct drm_plane_state {
354 struct drm_plane *plane;
355 struct drm_output *output;
356 struct drm_output_state *output_state;
357
358 struct drm_fb *fb;
359
360 struct weston_view *ev; /**< maintained for drm_assign_planes only */
361
362 int32_t src_x, src_y;
363 uint32_t src_w, src_h;
364 int32_t dest_x, dest_y;
365 uint32_t dest_w, dest_h;
366
367 bool complete;
368
369 /* We don't own the fd, so we shouldn't close it */
370 int in_fence_fd;
371
372 pixman_region32_t damage; /* damage to kernel */
373
374 struct wl_list link; /* drm_output_state::plane_list */
375};
376
377/**
378 * A plane represents one buffer, positioned within a CRTC, and stacked
379 * relative to other planes on the same CRTC.
380 *
381 * Each CRTC has a 'primary plane', which use used to display the classic
382 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
383 * call (which combines setting the CRTC's actual physical mode, and the
384 * properties of the primary plane).
385 *
386 * The cursor plane also has its own alternate legacy API.
387 *
388 * Other planes are used opportunistically to display content we do not
389 * wish to blit into the primary plane. These non-primary/cursor planes
390 * are referred to as 'sprites'.
391 */
392struct drm_plane {
393 struct weston_plane base;
394
395 struct drm_backend *backend;
396
397 enum wdrm_plane_type type;
398
399 uint32_t possible_crtcs;
400 uint32_t plane_id;
401 uint32_t count_formats;
402
403 struct drm_property_info props[WDRM_PLANE__COUNT];
404
405 /* The last state submitted to the kernel for this plane. */
406 struct drm_plane_state *state_cur;
407
408 struct wl_list link;
409
410 struct {
411 uint32_t format;
412 uint32_t count_modifiers;
413 uint64_t *modifiers;
414 } formats[];
415};
416
417struct drm_head {
418 struct weston_head base;
419 struct drm_backend *backend;
420
421 drmModeConnector *connector;
422 uint32_t connector_id;
423 struct drm_edid edid;
424
425 /* Holds the properties for the connector */
426 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
427
428 struct backlight *backlight;
429
430 drmModeModeInfo inherited_mode; /**< Original mode on the connector */
431 uint32_t inherited_crtc_id; /**< Original CRTC assignment */
432};
433
434struct drm_output {
435 struct weston_output base;
436 struct drm_backend *backend;
437
438 uint32_t crtc_id; /* object ID to pass to DRM functions */
439 int pipe; /* index of CRTC in resource array / bitmasks */
440
441 /* Holds the properties for the CRTC */
442 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
443
444 int page_flip_pending;
445 int atomic_complete_pending;
446 int destroy_pending;
447 int disable_pending;
448 int dpms_off_pending;
449
Stefan Agner974390a2019-07-08 00:42:05 +0200450 uint32_t gbm_cursor_handle[2];
Daniel Stonedd1bc502019-06-17 12:13:46 +0100451 struct drm_fb *gbm_cursor_fb[2];
452 struct drm_plane *cursor_plane;
453 struct weston_view *cursor_view;
454 int current_cursor;
455
456 struct gbm_surface *gbm_surface;
457 uint32_t gbm_format;
458 uint32_t gbm_bo_flags;
459
460 /* Plane being displayed directly on the CRTC */
461 struct drm_plane *scanout_plane;
462
463 /* The last state submitted to the kernel for this CRTC. */
464 struct drm_output_state *state_cur;
465 /* The previously-submitted state, where the hardware has not
466 * yet acknowledged completion of state_cur. */
467 struct drm_output_state *state_last;
468
469 struct drm_fb *dumb[2];
470 pixman_image_t *image[2];
471 int current_image;
472 pixman_region32_t previous_damage;
473
474 struct vaapi_recorder *recorder;
475 struct wl_listener recorder_frame_listener;
476
477 struct wl_event_source *pageflip_timer;
478
479 bool virtual;
480
481 submit_frame_cb virtual_submit_frame;
482};
483
484static inline struct drm_head *
485to_drm_head(struct weston_head *base)
486{
487 return container_of(base, struct drm_head, base);
488}
489
490static inline struct drm_output *
491to_drm_output(struct weston_output *base)
492{
493 return container_of(base, struct drm_output, base);
494}
495
496static inline struct drm_backend *
497to_drm_backend(struct weston_compositor *base)
498{
499 return container_of(base->backend, struct drm_backend, base);
500}
501
502static inline struct drm_mode *
503to_drm_mode(struct weston_mode *base)
504{
505 return container_of(base, struct drm_mode, base);
506}
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100507
Daniel Stone4c2fc702019-06-18 11:12:07 +0100508struct drm_output *
509drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id);
510
511struct drm_head *
512drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id);
513
Daniel Stone7580b3c2019-06-18 11:16:53 +0100514static inline bool
515drm_view_transform_supported(struct weston_view *ev, struct weston_output *output)
516{
517 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
518
519 /* This will incorrectly disallow cases where the combination of
520 * buffer and view transformations match the output transform.
521 * Fixing this requires a full analysis of the transformation
522 * chain. */
523 if (ev->transform.enabled &&
524 ev->transform.matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE)
525 return false;
526
527 if (viewport->buffer.transform != output->transform)
528 return false;
529
530 return true;
531}
532
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100533int
534drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode);
535
536struct drm_mode *
537drm_output_choose_mode(struct drm_output *output,
538 struct weston_mode *target_mode);
539void
540update_head_from_connector(struct drm_head *head,
541 drmModeObjectProperties *props);
542
543void
544drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list);
545
546void
547drm_output_print_modes(struct drm_output *output);
548
549int
550drm_output_set_mode(struct weston_output *base,
551 enum weston_drm_backend_output_mode mode,
552 const char *modeline);
553
Daniel Stone4c2fc702019-06-18 11:12:07 +0100554void
555drm_property_info_populate(struct drm_backend *b,
556 const struct drm_property_info *src,
557 struct drm_property_info *info,
558 unsigned int num_infos,
559 drmModeObjectProperties *props);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100560uint64_t
561drm_property_get_value(struct drm_property_info *info,
562 const drmModeObjectProperties *props,
563 uint64_t def);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100564int
565drm_plane_populate_formats(struct drm_plane *plane, const drmModePlane *kplane,
566 const drmModeObjectProperties *props);
567void
568drm_property_info_free(struct drm_property_info *info, int num_props);
569
570extern struct drm_property_enum_info plane_type_enums[];
571extern const struct drm_property_info plane_props[];
572extern struct drm_property_enum_info dpms_state_enums[];
573extern const struct drm_property_info connector_props[];
574extern const struct drm_property_info crtc_props[];
575
576int
577init_kms_caps(struct drm_backend *b);
578
579int
580drm_pending_state_test(struct drm_pending_state *pending_state);
581int
582drm_pending_state_apply(struct drm_pending_state *pending_state);
583int
584drm_pending_state_apply_sync(struct drm_pending_state *pending_state);
585
586void
587drm_output_set_gamma(struct weston_output *output_base,
588 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b);
589
590void
591drm_output_update_msc(struct drm_output *output, unsigned int seq);
592void
593drm_output_update_complete(struct drm_output *output, uint32_t flags,
594 unsigned int sec, unsigned int usec);
595int
596on_drm_input(int fd, uint32_t mask, void *data);
597
598struct drm_plane_state *
599drm_output_state_get_existing_plane(struct drm_output_state *state_output,
600 struct drm_plane *plane);
601void
602drm_plane_state_free(struct drm_plane_state *state, bool force);
603void
604drm_output_state_free(struct drm_output_state *state);
605void
606drm_pending_state_free(struct drm_pending_state *pending_state);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100607
608struct drm_fb *
609drm_fb_ref(struct drm_fb *fb);
610void
611drm_fb_unref(struct drm_fb *fb);
612
613struct drm_fb *
614drm_fb_create_dumb(struct drm_backend *b, int width, int height,
615 uint32_t format);
616struct drm_fb *
617drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
618 bool is_opaque, enum drm_fb_type type);
619struct drm_fb *
620drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev);
Daniel Stone6b466f22019-06-18 11:30:54 +0100621
622
623struct drm_pending_state *
624drm_pending_state_alloc(struct drm_backend *backend);
625void
626drm_pending_state_free(struct drm_pending_state *pending_state);
627struct drm_output_state *
628drm_pending_state_get_output(struct drm_pending_state *pending_state,
629 struct drm_output *output);
630
631
632/**
633 * Mode for drm_output_state_duplicate.
634 */
635enum drm_output_state_duplicate_mode {
636 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
637 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
638};
639
640struct drm_output_state *
641drm_output_state_alloc(struct drm_output *output,
642 struct drm_pending_state *pending_state);
643struct drm_output_state *
644drm_output_state_duplicate(struct drm_output_state *src,
645 struct drm_pending_state *pending_state,
646 enum drm_output_state_duplicate_mode plane_mode);
647void
648drm_output_state_free(struct drm_output_state *state);
649struct drm_plane_state *
650drm_output_state_get_plane(struct drm_output_state *state_output,
651 struct drm_plane *plane);
652struct drm_plane_state *
653drm_output_state_get_existing_plane(struct drm_output_state *state_output,
654 struct drm_plane *plane);
655
656
657
658struct drm_plane_state *
659drm_plane_state_alloc(struct drm_output_state *state_output,
660 struct drm_plane *plane);
661struct drm_plane_state *
662drm_plane_state_duplicate(struct drm_output_state *state_output,
663 struct drm_plane_state *src);
664void
665drm_plane_state_free(struct drm_plane_state *state, bool force);
666void
667drm_plane_state_put_back(struct drm_plane_state *state);
668bool
669drm_plane_state_coords_for_view(struct drm_plane_state *state,
670 struct weston_view *ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100671
672void
673drm_assign_planes(struct weston_output *output_base, void *repaint_data);
674
675bool
676drm_plane_is_available(struct drm_plane *plane, struct drm_output *output);