blob: f0f85266b5ad9c12ab1d2321a8437196af4701bb [file] [log] [blame]
Daniel Stone4c2fc702019-06-18 11:12:07 +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 <stdint.h>
33
34#include <xf86drm.h>
35#include <xf86drmMode.h>
Daniel Stone4c2fc702019-06-18 11:12:07 +010036
Daniel Stone4c2fc702019-06-18 11:12:07 +010037#include <libweston/libweston.h>
38#include <libweston/backend-drm.h>
39#include "shared/helpers.h"
Pekka Paalanen4b301fe2021-02-04 17:39:45 +020040#include "shared/weston-drm-fourcc.h"
Daniel Stone4c2fc702019-06-18 11:12:07 +010041#include "drm-internal.h"
42#include "pixel-formats.h"
43#include "presentation-time-server-protocol.h"
44
45struct drm_property_enum_info plane_type_enums[] = {
46 [WDRM_PLANE_TYPE_PRIMARY] = {
47 .name = "Primary",
48 },
49 [WDRM_PLANE_TYPE_OVERLAY] = {
50 .name = "Overlay",
51 },
52 [WDRM_PLANE_TYPE_CURSOR] = {
53 .name = "Cursor",
54 },
55};
56
57const struct drm_property_info plane_props[] = {
58 [WDRM_PLANE_TYPE] = {
59 .name = "type",
60 .enum_values = plane_type_enums,
61 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
62 },
63 [WDRM_PLANE_SRC_X] = { .name = "SRC_X", },
64 [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", },
65 [WDRM_PLANE_SRC_W] = { .name = "SRC_W", },
66 [WDRM_PLANE_SRC_H] = { .name = "SRC_H", },
67 [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", },
68 [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", },
69 [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", },
70 [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", },
71 [WDRM_PLANE_FB_ID] = { .name = "FB_ID", },
72 [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", },
73 [WDRM_PLANE_IN_FORMATS] = { .name = "IN_FORMATS" },
74 [WDRM_PLANE_IN_FENCE_FD] = { .name = "IN_FENCE_FD" },
75 [WDRM_PLANE_FB_DAMAGE_CLIPS] = { .name = "FB_DAMAGE_CLIPS" },
Marius Vladcdd6fa22019-08-29 20:42:00 +030076 [WDRM_PLANE_ZPOS] = { .name = "zpos" },
Daniel Stone4c2fc702019-06-18 11:12:07 +010077};
78
79struct drm_property_enum_info dpms_state_enums[] = {
80 [WDRM_DPMS_STATE_OFF] = {
81 .name = "Off",
82 },
83 [WDRM_DPMS_STATE_ON] = {
84 .name = "On",
85 },
86 [WDRM_DPMS_STATE_STANDBY] = {
87 .name = "Standby",
88 },
89 [WDRM_DPMS_STATE_SUSPEND] = {
90 .name = "Suspend",
91 },
92};
93
Ankit Nautiyala344fe32019-05-14 18:36:08 +053094struct drm_property_enum_info content_protection_enums[] = {
95 [WDRM_CONTENT_PROTECTION_UNDESIRED] = {
96 .name = "Undesired",
97 },
98 [WDRM_CONTENT_PROTECTION_DESIRED] = {
99 .name = "Desired",
100 },
101 [WDRM_CONTENT_PROTECTION_ENABLED] = {
102 .name = "Enabled",
103 },
104};
105
106struct drm_property_enum_info hdcp_content_type_enums[] = {
107 [WDRM_HDCP_CONTENT_TYPE0] = {
108 .name = "HDCP Type0",
109 },
110 [WDRM_HDCP_CONTENT_TYPE1] = {
111 .name = "HDCP Type1",
112 },
113};
114
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000115struct drm_property_enum_info panel_orientation_enums[] = {
116 [WDRM_PANEL_ORIENTATION_NORMAL] = { .name = "Normal", },
117 [WDRM_PANEL_ORIENTATION_UPSIDE_DOWN] = { .name = "Upside Down", },
118 [WDRM_PANEL_ORIENTATION_LEFT_SIDE_UP] = { .name = "Left Side Up", },
119 [WDRM_PANEL_ORIENTATION_RIGHT_SIDE_UP] = { .name = "Right Side Up", },
120};
121
Daniel Stone4c2fc702019-06-18 11:12:07 +0100122const struct drm_property_info connector_props[] = {
123 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
124 [WDRM_CONNECTOR_DPMS] = {
125 .name = "DPMS",
126 .enum_values = dpms_state_enums,
127 .num_enum_values = WDRM_DPMS_STATE__COUNT,
128 },
129 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
130 [WDRM_CONNECTOR_NON_DESKTOP] = { .name = "non-desktop", },
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530131 [WDRM_CONNECTOR_CONTENT_PROTECTION] = {
132 .name = "Content Protection",
133 .enum_values = content_protection_enums,
134 .num_enum_values = WDRM_CONTENT_PROTECTION__COUNT,
135 },
136 [WDRM_CONNECTOR_HDCP_CONTENT_TYPE] = {
137 .name = "HDCP Content Type",
138 .enum_values = hdcp_content_type_enums,
139 .num_enum_values = WDRM_HDCP_CONTENT_TYPE__COUNT,
140 },
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000141 [WDRM_CONNECTOR_PANEL_ORIENTATION] = {
142 .name = "panel orientation",
143 .enum_values = panel_orientation_enums,
144 .num_enum_values = WDRM_PANEL_ORIENTATION__COUNT,
145 },
Daniel Stone4c2fc702019-06-18 11:12:07 +0100146};
147
148const struct drm_property_info crtc_props[] = {
149 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
150 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
151};
152
153
154/**
155 * Mode for drm_pending_state_apply and co.
156 */
157enum drm_state_apply_mode {
158 DRM_STATE_APPLY_SYNC, /**< state fully processed */
159 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
160 DRM_STATE_TEST_ONLY, /**< test if the state can be applied */
161};
162
163/**
164 * Get the current value of a KMS property
165 *
166 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
167 * for the target property, return the current value of that property,
168 * with an optional default. If the property is a KMS enum type, the return
169 * value will be translated into the appropriate internal enum.
170 *
171 * If the property is not present, the default value will be returned.
172 *
173 * @param info Internal structure for property to look up
174 * @param props Raw KMS properties for the target object
175 * @param def Value to return if property is not found
176 */
177uint64_t
178drm_property_get_value(struct drm_property_info *info,
179 const drmModeObjectProperties *props,
180 uint64_t def)
181{
182 unsigned int i;
183
184 if (info->prop_id == 0)
185 return def;
186
187 for (i = 0; i < props->count_props; i++) {
188 unsigned int j;
189
190 if (props->props[i] != info->prop_id)
191 continue;
192
193 /* Simple (non-enum) types can return the value directly */
194 if (info->num_enum_values == 0)
195 return props->prop_values[i];
196
197 /* Map from raw value to enum value */
198 for (j = 0; j < info->num_enum_values; j++) {
199 if (!info->enum_values[j].valid)
200 continue;
201 if (info->enum_values[j].value != props->prop_values[i])
202 continue;
203
204 return j;
205 }
206
207 /* We don't have a mapping for this enum; return default. */
208 break;
209 }
210
211 return def;
212}
213
214/**
Marius Vlad1accffe2019-11-01 12:00:09 +0200215 * Get the current range values of a KMS property
216 *
217 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
218 * for the target property, return the current range values of that property,
219 *
220 * If the property is not present, or there's no it is not a prop range then
221 * NULL will be returned.
222 *
223 * @param info Internal structure for property to look up
224 * @param props Raw KMS properties for the target object
225 */
226uint64_t *
227drm_property_get_range_values(struct drm_property_info *info,
228 const drmModeObjectProperties *props)
229{
230 unsigned int i;
231
232 if (info->prop_id == 0)
233 return NULL;
234
235 for (i = 0; i < props->count_props; i++) {
236
237 if (props->props[i] != info->prop_id)
238 continue;
239
240 if (!(info->flags & DRM_MODE_PROP_RANGE) &&
241 !(info->flags & DRM_MODE_PROP_SIGNED_RANGE))
242 continue;
243
244 return info->range_values;
245 }
246
247 return NULL;
248}
249
250/**
Daniel Stone4c2fc702019-06-18 11:12:07 +0100251 * Cache DRM property values
252 *
253 * Update a per-object array of drm_property_info structures, given the
254 * DRM properties of the object.
255 *
256 * Call this every time an object newly appears (note that only connectors
257 * can be hotplugged), the first time it is seen, or when its status changes
258 * in a way which invalidates the potential property values (currently, the
259 * only case for this is connector hotplug).
260 *
261 * This updates the property IDs and enum values within the drm_property_info
262 * array.
263 *
264 * DRM property enum values are dynamic at runtime; the user must query the
265 * property to find out the desired runtime value for a requested string
266 * name. Using the 'type' field on planes as an example, there is no single
267 * hardcoded constant for primary plane types; instead, the property must be
268 * queried at runtime to find the value associated with the string "Primary".
269 *
270 * This helper queries and caches the enum values, to allow us to use a set
271 * of compile-time-constant enums portably across various implementations.
272 * The values given in enum_names are searched for, and stored in the
273 * same-indexed field of the map array.
274 *
275 * @param b DRM backend object
276 * @param src DRM property info array to source from
277 * @param info DRM property info array to copy into
278 * @param num_infos Number of entries in the source array
279 * @param props DRM object properties for the object
280 */
281void
282drm_property_info_populate(struct drm_backend *b,
283 const struct drm_property_info *src,
284 struct drm_property_info *info,
285 unsigned int num_infos,
286 drmModeObjectProperties *props)
287{
288 drmModePropertyRes *prop;
289 unsigned i, j;
290
291 for (i = 0; i < num_infos; i++) {
292 unsigned int j;
293
294 info[i].name = src[i].name;
295 info[i].prop_id = 0;
296 info[i].num_enum_values = src[i].num_enum_values;
297
298 if (src[i].num_enum_values == 0)
299 continue;
300
301 info[i].enum_values =
302 malloc(src[i].num_enum_values *
303 sizeof(*info[i].enum_values));
304 assert(info[i].enum_values);
305 for (j = 0; j < info[i].num_enum_values; j++) {
306 info[i].enum_values[j].name = src[i].enum_values[j].name;
307 info[i].enum_values[j].valid = false;
308 }
309 }
310
311 for (i = 0; i < props->count_props; i++) {
312 unsigned int k;
313
314 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
315 if (!prop)
316 continue;
317
318 for (j = 0; j < num_infos; j++) {
319 if (!strcmp(prop->name, info[j].name))
320 break;
321 }
322
323 /* We don't know/care about this property. */
324 if (j == num_infos) {
325#ifdef DEBUG
326 weston_log("DRM debug: unrecognized property %u '%s'\n",
327 prop->prop_id, prop->name);
328#endif
329 drmModeFreeProperty(prop);
330 continue;
331 }
332
333 if (info[j].num_enum_values == 0 &&
334 (prop->flags & DRM_MODE_PROP_ENUM)) {
335 weston_log("DRM: expected property %s to not be an"
336 " enum, but it is; ignoring\n", prop->name);
337 drmModeFreeProperty(prop);
338 continue;
339 }
340
341 info[j].prop_id = props->props[i];
Marius Vlad1accffe2019-11-01 12:00:09 +0200342 info[j].flags = prop->flags;
343
344 if (prop->flags & DRM_MODE_PROP_RANGE ||
345 prop->flags & DRM_MODE_PROP_SIGNED_RANGE) {
346 info[j].num_range_values = prop->count_values;
347 for (int i = 0; i < prop->count_values; i++)
348 info[j].range_values[i] = prop->values[i];
349 }
350
Daniel Stone4c2fc702019-06-18 11:12:07 +0100351
352 if (info[j].num_enum_values == 0) {
353 drmModeFreeProperty(prop);
354 continue;
355 }
356
357 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
358 weston_log("DRM: expected property %s to be an enum,"
359 " but it is not; ignoring\n", prop->name);
360 drmModeFreeProperty(prop);
361 info[j].prop_id = 0;
362 continue;
363 }
364
365 for (k = 0; k < info[j].num_enum_values; k++) {
366 int l;
367
368 for (l = 0; l < prop->count_enums; l++) {
369 if (!strcmp(prop->enums[l].name,
370 info[j].enum_values[k].name))
371 break;
372 }
373
374 if (l == prop->count_enums)
375 continue;
376
377 info[j].enum_values[k].valid = true;
378 info[j].enum_values[k].value = prop->enums[l].value;
379 }
380
381 drmModeFreeProperty(prop);
382 }
383
384#ifdef DEBUG
385 for (i = 0; i < num_infos; i++) {
386 if (info[i].prop_id == 0)
387 weston_log("DRM warning: property '%s' missing\n",
388 info[i].name);
389 }
390#endif
391}
392
393/**
394 * Free DRM property information
395 *
396 * Frees all memory associated with a DRM property info array and zeroes
397 * it out, leaving it usable for a further drm_property_info_update() or
398 * drm_property_info_free().
399 *
400 * @param info DRM property info array
401 * @param num_props Number of entries in array to free
402 */
403void
404drm_property_info_free(struct drm_property_info *info, int num_props)
405{
406 int i;
407
408 for (i = 0; i < num_props; i++)
409 free(info[i].enum_values);
410
411 memset(info, 0, sizeof(*info) * num_props);
412}
413
Daniel Stone4c2fc702019-06-18 11:12:07 +0100414static inline uint32_t *
415formats_ptr(struct drm_format_modifier_blob *blob)
416{
417 return (uint32_t *)(((char *)blob) + blob->formats_offset);
418}
419
420static inline struct drm_format_modifier *
421modifiers_ptr(struct drm_format_modifier_blob *blob)
422{
423 return (struct drm_format_modifier *)
424 (((char *)blob) + blob->modifiers_offset);
425}
Daniel Stone4c2fc702019-06-18 11:12:07 +0100426
427/**
428 * Populates the plane's formats array, using either the IN_FORMATS blob
429 * property (if available), or the plane's format list if not.
430 */
431int
432drm_plane_populate_formats(struct drm_plane *plane, const drmModePlane *kplane,
Stefan Agner465ab2c2020-06-17 23:36:44 +0200433 const drmModeObjectProperties *props,
434 const bool use_modifiers)
Daniel Stone4c2fc702019-06-18 11:12:07 +0100435{
436 unsigned i;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100437 drmModePropertyBlobRes *blob;
438 struct drm_format_modifier_blob *fmt_mod_blob;
439 struct drm_format_modifier *blob_modifiers;
440 uint32_t *blob_formats;
441 uint32_t blob_id;
442
Stefan Agner465ab2c2020-06-17 23:36:44 +0200443 if (!use_modifiers)
444 goto fallback;
445
Daniel Stone4c2fc702019-06-18 11:12:07 +0100446 blob_id = drm_property_get_value(&plane->props[WDRM_PLANE_IN_FORMATS],
447 props,
448 0);
449 if (blob_id == 0)
450 goto fallback;
451
452 blob = drmModeGetPropertyBlob(plane->backend->drm.fd, blob_id);
453 if (!blob)
454 goto fallback;
455
456 fmt_mod_blob = blob->data;
457 blob_formats = formats_ptr(fmt_mod_blob);
458 blob_modifiers = modifiers_ptr(fmt_mod_blob);
459
460 if (plane->count_formats != fmt_mod_blob->count_formats) {
461 weston_log("DRM backend: format count differs between "
462 "plane (%d) and IN_FORMATS (%d)\n",
463 plane->count_formats,
464 fmt_mod_blob->count_formats);
465 weston_log("This represents a kernel bug; Weston is "
466 "unable to continue.\n");
467 abort();
468 }
469
470 for (i = 0; i < fmt_mod_blob->count_formats; i++) {
471 uint32_t count_modifiers = 0;
472 uint64_t *modifiers = NULL;
473 unsigned j;
474
475 for (j = 0; j < fmt_mod_blob->count_modifiers; j++) {
476 struct drm_format_modifier *mod = &blob_modifiers[j];
477
478 if ((i < mod->offset) || (i > mod->offset + 63))
479 continue;
480 if (!(mod->formats & (1 << (i - mod->offset))))
481 continue;
482
483 modifiers = realloc(modifiers,
484 (count_modifiers + 1) *
485 sizeof(modifiers[0]));
486 assert(modifiers);
487 modifiers[count_modifiers++] = mod->modifier;
488 }
489
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900490 if (count_modifiers == 0) {
491 modifiers = malloc(sizeof(*modifiers));
492 *modifiers = DRM_FORMAT_MOD_LINEAR;
493 count_modifiers = 1;
494 }
495
Daniel Stone4c2fc702019-06-18 11:12:07 +0100496 plane->formats[i].format = blob_formats[i];
497 plane->formats[i].modifiers = modifiers;
498 plane->formats[i].count_modifiers = count_modifiers;
499 }
500
501 drmModeFreePropertyBlob(blob);
502
503 return 0;
504
505fallback:
Daniel Stone4c2fc702019-06-18 11:12:07 +0100506 /* No IN_FORMATS blob available, so just use the old. */
507 assert(plane->count_formats == kplane->count_formats);
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900508 for (i = 0; i < kplane->count_formats; i++) {
Daniel Stone4c2fc702019-06-18 11:12:07 +0100509 plane->formats[i].format = kplane->formats[i];
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900510 plane->formats[i].modifiers = malloc(sizeof(uint64_t));
511 plane->formats[i].modifiers[0] = DRM_FORMAT_MOD_LINEAR;
512 plane->formats[i].count_modifiers = 1;
513 }
Daniel Stone4c2fc702019-06-18 11:12:07 +0100514
515 return 0;
516}
517
518void
519drm_output_set_gamma(struct weston_output *output_base,
520 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
521{
522 int rc;
523 struct drm_output *output = to_drm_output(output_base);
524 struct drm_backend *backend =
525 to_drm_backend(output->base.compositor);
526
527 /* check */
528 if (output_base->gamma_size != size)
529 return;
530
531 rc = drmModeCrtcSetGamma(backend->drm.fd,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300532 output->crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100533 size, r, g, b);
534 if (rc)
535 weston_log("set gamma failed: %s\n", strerror(errno));
536}
537
538/**
539 * Mark an output state as current on the output, i.e. it has been
540 * submitted to the kernel. The mode argument determines whether this
541 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
542 * or asynchronously (in which case we wait for events to complete).
543 */
544static void
545drm_output_assign_state(struct drm_output_state *state,
546 enum drm_state_apply_mode mode)
547{
548 struct drm_output *output = state->output;
549 struct drm_backend *b = to_drm_backend(output->base.compositor);
550 struct drm_plane_state *plane_state;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530551 struct drm_head *head;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100552
553 assert(!output->state_last);
554
555 if (mode == DRM_STATE_APPLY_ASYNC)
556 output->state_last = output->state_cur;
557 else
558 drm_output_state_free(output->state_cur);
559
560 wl_list_remove(&state->link);
561 wl_list_init(&state->link);
562 state->pending_state = NULL;
563
564 output->state_cur = state;
565
566 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300567 drm_debug(b, "\t[CRTC:%u] setting pending flip\n",
568 output->crtc->crtc_id);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100569 output->atomic_complete_pending = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100570 }
571
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530572 if (b->atomic_modeset &&
573 state->protection == WESTON_HDCP_DISABLE)
574 wl_list_for_each(head, &output->base.head_list, base.output_link)
575 weston_head_set_content_protection_status(&head->base,
576 WESTON_HDCP_DISABLE);
577
Daniel Stone4c2fc702019-06-18 11:12:07 +0100578 /* Replace state_cur on each affected plane with the new state, being
579 * careful to dispose of orphaned (but only orphaned) previous state.
580 * If the previous state is not orphaned (still has an output_state
581 * attached), it will be disposed of by freeing the output_state. */
582 wl_list_for_each(plane_state, &state->plane_list, link) {
583 struct drm_plane *plane = plane_state->plane;
584
585 if (plane->state_cur && !plane->state_cur->output_state)
586 drm_plane_state_free(plane->state_cur, true);
587 plane->state_cur = plane_state;
588
589 if (mode != DRM_STATE_APPLY_ASYNC) {
590 plane_state->complete = true;
591 continue;
592 }
593
594 if (b->atomic_modeset)
595 continue;
596
597 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY);
598 if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100599 output->page_flip_pending = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100600 }
601}
602
603static void
604drm_output_set_cursor(struct drm_output_state *output_state)
605{
606 struct drm_output *output = output_state->output;
607 struct drm_backend *b = to_drm_backend(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300608 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100609 struct drm_plane *plane = output->cursor_plane;
610 struct drm_plane_state *state;
Stefan Agner974390a2019-07-08 00:42:05 +0200611 uint32_t handle;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100612
613 if (!plane)
614 return;
615
616 state = drm_output_state_get_existing_plane(output_state, plane);
617 if (!state)
618 return;
619
620 if (!state->fb) {
621 pixman_region32_fini(&plane->base.damage);
622 pixman_region32_init(&plane->base.damage);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300623 drmModeSetCursor(b->drm.fd, crtc->crtc_id, 0, 0, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100624 return;
625 }
626
627 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
628 assert(!plane->state_cur->output || plane->state_cur->output == output);
629
Stefan Agner974390a2019-07-08 00:42:05 +0200630 handle = output->gbm_cursor_handle[output->current_cursor];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100631 if (plane->state_cur->fb != state->fb) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300632 if (drmModeSetCursor(b->drm.fd, crtc->crtc_id, handle,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100633 b->cursor_width, b->cursor_height)) {
634 weston_log("failed to set cursor: %s\n",
635 strerror(errno));
636 goto err;
637 }
638 }
639
640 pixman_region32_fini(&plane->base.damage);
641 pixman_region32_init(&plane->base.damage);
642
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300643 if (drmModeMoveCursor(b->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100644 state->dest_x, state->dest_y)) {
645 weston_log("failed to move cursor: %s\n", strerror(errno));
646 goto err;
647 }
648
649 return;
650
651err:
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100652 b->cursors_are_broken = true;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300653 drmModeSetCursor(b->drm.fd, crtc->crtc_id, 0, 0, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100654}
655
656static int
657drm_output_apply_state_legacy(struct drm_output_state *state)
658{
659 struct drm_output *output = state->output;
660 struct drm_backend *backend = to_drm_backend(output->base.compositor);
661 struct drm_plane *scanout_plane = output->scanout_plane;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300662 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100663 struct drm_property_info *dpms_prop;
664 struct drm_plane_state *scanout_state;
665 struct drm_mode *mode;
666 struct drm_head *head;
667 const struct pixel_format_info *pinfo = NULL;
668 uint32_t connectors[MAX_CLONED_CONNECTORS];
669 int n_conn = 0;
670 struct timespec now;
671 int ret = 0;
672
673 wl_list_for_each(head, &output->base.head_list, base.output_link) {
674 assert(n_conn < MAX_CLONED_CONNECTORS);
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300675 connectors[n_conn++] = head->connector.connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100676 }
677
678 /* If disable_planes is set then assign_planes() wasn't
679 * called for this render, so we could still have a stale
680 * cursor plane set up.
681 */
682 if (output->base.disable_planes) {
683 output->cursor_view = NULL;
684 if (output->cursor_plane) {
685 output->cursor_plane->base.x = INT32_MIN;
686 output->cursor_plane->base.y = INT32_MIN;
687 }
688 }
689
690 if (state->dpms != WESTON_DPMS_ON) {
691 if (output->cursor_plane) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300692 ret = drmModeSetCursor(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100693 0, 0, 0);
694 if (ret)
695 weston_log("drmModeSetCursor failed disable: %s\n",
696 strerror(errno));
697 }
698
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300699 ret = drmModeSetCrtc(backend->drm.fd, crtc->crtc_id, 0, 0, 0,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100700 NULL, 0, NULL);
701 if (ret)
702 weston_log("drmModeSetCrtc failed disabling: %s\n",
703 strerror(errno));
704
705 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
706 weston_compositor_read_presentation_clock(output->base.compositor, &now);
707 drm_output_update_complete(output,
708 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
709 now.tv_sec, now.tv_nsec / 1000);
710
711 return 0;
712 }
713
714 scanout_state =
715 drm_output_state_get_existing_plane(state, scanout_plane);
716
717 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
718 * legacy PageFlip API doesn't allow us to do clipping either. */
719 assert(scanout_state->src_x == 0);
720 assert(scanout_state->src_y == 0);
721 assert(scanout_state->src_w ==
722 (unsigned) (output->base.current_mode->width << 16));
723 assert(scanout_state->src_h ==
724 (unsigned) (output->base.current_mode->height << 16));
725 assert(scanout_state->dest_x == 0);
726 assert(scanout_state->dest_y == 0);
727 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
728 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
729 /* The legacy SetCrtc API doesn't support fences */
730 assert(scanout_state->in_fence_fd == -1);
731
732 mode = to_drm_mode(output->base.current_mode);
733 if (backend->state_invalid ||
734 !scanout_plane->state_cur->fb ||
735 scanout_plane->state_cur->fb->strides[0] !=
736 scanout_state->fb->strides[0]) {
737
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300738 ret = drmModeSetCrtc(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100739 scanout_state->fb->fb_id,
740 0, 0,
741 connectors, n_conn,
742 &mode->mode_info);
743 if (ret) {
744 weston_log("set mode failed: %s\n", strerror(errno));
745 goto err;
746 }
747 }
748
749 pinfo = scanout_state->fb->format;
750 drm_debug(backend, "\t[CRTC:%u, PLANE:%u] FORMAT: %s\n",
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300751 crtc->crtc_id, scanout_state->plane->plane_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100752 pinfo ? pinfo->drm_format_name : "UNKNOWN");
753
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300754 if (drmModePageFlip(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100755 scanout_state->fb->fb_id,
756 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
757 weston_log("queueing pageflip failed: %s\n", strerror(errno));
758 goto err;
759 }
760
761 assert(!output->page_flip_pending);
762
763 if (output->pageflip_timer)
764 wl_event_source_timer_update(output->pageflip_timer,
765 backend->pageflip_timeout);
766
767 drm_output_set_cursor(state);
768
769 if (state->dpms != output->state_cur->dpms) {
770 wl_list_for_each(head, &output->base.head_list, base.output_link) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300771 dpms_prop = &head->connector.props[WDRM_CONNECTOR_DPMS];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100772 if (dpms_prop->prop_id == 0)
773 continue;
774
775 ret = drmModeConnectorSetProperty(backend->drm.fd,
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300776 head->connector.connector_id,
777 dpms_prop->prop_id,
778 state->dpms);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100779 if (ret) {
780 weston_log("DRM: DPMS: failed property set for %s\n",
781 head->base.name);
782 }
783 }
784 }
785
786 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
787
788 return 0;
789
790err:
791 output->cursor_view = NULL;
792 drm_output_state_free(state);
793 return -1;
794}
795
Daniel Stone4c2fc702019-06-18 11:12:07 +0100796static int
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300797crtc_add_prop(drmModeAtomicReq *req, struct drm_crtc *crtc,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100798 enum wdrm_crtc_property prop, uint64_t val)
799{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300800 struct drm_property_info *info = &crtc->props_crtc[prop];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100801 int ret;
802
803 if (info->prop_id == 0)
804 return -1;
805
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300806 ret = drmModeAtomicAddProperty(req, crtc->crtc_id, info->prop_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100807 val);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300808 drm_debug(crtc->backend, "\t\t\t[CRTC:%lu] %lu (%s) -> %llu (0x%llx)\n",
809 (unsigned long) crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100810 (unsigned long) info->prop_id, info->name,
811 (unsigned long long) val, (unsigned long long) val);
812 return (ret <= 0) ? -1 : 0;
813}
814
815static int
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300816connector_add_prop(drmModeAtomicReq *req, struct drm_connector *connector,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100817 enum wdrm_connector_property prop, uint64_t val)
818{
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300819 struct drm_property_info *info = &connector->props[prop];
820 uint32_t connector_id = connector->connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100821 int ret;
822
823 if (info->prop_id == 0)
824 return -1;
825
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300826 ret = drmModeAtomicAddProperty(req, connector_id, info->prop_id, val);
827 drm_debug(connector->backend, "\t\t\t[CONN:%lu] %lu (%s) -> %llu (0x%llx)\n",
828 (unsigned long) connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100829 (unsigned long) info->prop_id, info->name,
830 (unsigned long long) val, (unsigned long long) val);
831 return (ret <= 0) ? -1 : 0;
832}
833
834static int
835plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
836 enum wdrm_plane_property prop, uint64_t val)
837{
838 struct drm_property_info *info = &plane->props[prop];
839 int ret;
840
841 if (info->prop_id == 0)
842 return -1;
843
844 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
845 val);
846 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] %lu (%s) -> %llu (0x%llx)\n",
847 (unsigned long) plane->plane_id,
848 (unsigned long) info->prop_id, info->name,
849 (unsigned long long) val, (unsigned long long) val);
850 return (ret <= 0) ? -1 : 0;
851}
852
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530853static bool
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300854drm_connector_has_prop(struct drm_connector *connector,
855 enum wdrm_connector_property prop)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530856{
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300857 if (connector->props[prop].prop_id != 0)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530858 return true;
859
860 return false;
861}
862
863/*
864 * This function converts the protection requests from weston_hdcp_protection
865 * corresponding drm values. These values can be set in "Content Protection"
866 * & "HDCP Content Type" connector properties.
867 */
868static void
869get_drm_protection_from_weston(enum weston_hdcp_protection weston_protection,
870 enum wdrm_content_protection_state *drm_protection,
871 enum wdrm_hdcp_content_type *drm_cp_type)
872{
873
874 switch (weston_protection) {
875 case WESTON_HDCP_DISABLE:
876 *drm_protection = WDRM_CONTENT_PROTECTION_UNDESIRED;
877 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
878 break;
879 case WESTON_HDCP_ENABLE_TYPE_0:
880 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
881 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
882 break;
883 case WESTON_HDCP_ENABLE_TYPE_1:
884 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
885 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE1;
886 break;
887 default:
888 assert(0 && "bad weston_hdcp_protection");
889 }
890}
891
892static void
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300893drm_connector_set_hdcp_property(struct drm_connector *connector,
894 enum weston_hdcp_protection protection,
895 drmModeAtomicReq *req)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530896{
897 int ret;
898 enum wdrm_content_protection_state drm_protection;
899 enum wdrm_hdcp_content_type drm_cp_type;
900 struct drm_property_enum_info *enum_info;
901 uint64_t prop_val;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300902 struct drm_property_info *props = connector->props;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530903
904 get_drm_protection_from_weston(protection, &drm_protection,
905 &drm_cp_type);
906
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300907 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_CONTENT_PROTECTION))
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530908 return;
909
910 /*
911 * Content-type property is not exposed for platforms not supporting
912 * HDCP2.2, therefore, type-1 cannot be supported. The type-0 content
913 * still can be supported if the content-protection property is exposed.
914 */
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300915 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_HDCP_CONTENT_TYPE) &&
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530916 drm_cp_type != WDRM_HDCP_CONTENT_TYPE0)
917 return;
918
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300919 enum_info = props[WDRM_CONNECTOR_CONTENT_PROTECTION].enum_values;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530920 prop_val = enum_info[drm_protection].value;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300921 ret = connector_add_prop(req, connector,
922 WDRM_CONNECTOR_CONTENT_PROTECTION, prop_val);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530923 assert(ret == 0);
924
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300925 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_HDCP_CONTENT_TYPE))
Ankit Nautiyalfc2c1802019-08-30 19:40:46 +0530926 return;
927
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300928 enum_info = props[WDRM_CONNECTOR_HDCP_CONTENT_TYPE].enum_values;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530929 prop_val = enum_info[drm_cp_type].value;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300930 ret = connector_add_prop(req, connector,
931 WDRM_CONNECTOR_HDCP_CONTENT_TYPE, prop_val);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530932 assert(ret == 0);
933}
934
Daniel Stone4c2fc702019-06-18 11:12:07 +0100935static int
936drm_output_apply_state_atomic(struct drm_output_state *state,
937 drmModeAtomicReq *req,
938 uint32_t *flags)
939{
940 struct drm_output *output = state->output;
941 struct drm_backend *b = to_drm_backend(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300942 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100943 struct drm_plane_state *plane_state;
944 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
945 struct drm_head *head;
946 int ret = 0;
947
948 drm_debug(b, "\t\t[atomic] %s output %lu (%s) state\n",
949 (*flags & DRM_MODE_ATOMIC_TEST_ONLY) ? "testing" : "applying",
950 (unsigned long) output->base.id, output->base.name);
951
952 if (state->dpms != output->state_cur->dpms) {
953 drm_debug(b, "\t\t\t[atomic] DPMS state differs, modeset OK\n");
954 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
955 }
956
957 if (state->dpms == WESTON_DPMS_ON) {
958 ret = drm_mode_ensure_blob(b, current_mode);
959 if (ret != 0)
960 return ret;
961
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300962 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100963 current_mode->blob_id);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300964 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 1);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100965
966 /* No need for the DPMS property, since it is implicit in
967 * routing and CRTC activity. */
968 wl_list_for_each(head, &output->base.head_list, base.output_link) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300969 ret |= connector_add_prop(req, &head->connector,
970 WDRM_CONNECTOR_CRTC_ID,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300971 crtc->crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100972 }
973 } else {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300974 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID, 0);
975 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100976
977 /* No need for the DPMS property, since it is implicit in
978 * routing and CRTC activity. */
979 wl_list_for_each(head, &output->base.head_list, base.output_link)
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300980 ret |= connector_add_prop(req, &head->connector,
981 WDRM_CONNECTOR_CRTC_ID, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100982 }
983
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530984 wl_list_for_each(head, &output->base.head_list, base.output_link)
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300985 drm_connector_set_hdcp_property(&head->connector,
986 state->protection, req);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530987
Daniel Stone4c2fc702019-06-18 11:12:07 +0100988 if (ret != 0) {
989 weston_log("couldn't set atomic CRTC/connector state\n");
990 return ret;
991 }
992
993 wl_list_for_each(plane_state, &state->plane_list, link) {
994 struct drm_plane *plane = plane_state->plane;
995 const struct pixel_format_info *pinfo = NULL;
996
997 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
998 plane_state->fb ? plane_state->fb->fb_id : 0);
999 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001000 plane_state->fb ? crtc->crtc_id : 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001001 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
1002 plane_state->src_x);
1003 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
1004 plane_state->src_y);
1005 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
1006 plane_state->src_w);
1007 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
1008 plane_state->src_h);
1009 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
1010 plane_state->dest_x);
1011 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
1012 plane_state->dest_y);
1013 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
1014 plane_state->dest_w);
1015 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
1016 plane_state->dest_h);
Scott Anderson15c603c2020-06-02 17:39:43 +12001017 if (plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS].prop_id != 0)
1018 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_DAMAGE_CLIPS,
1019 plane_state->damage_blob_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001020
1021 if (plane_state->fb && plane_state->fb->format)
1022 pinfo = plane_state->fb->format;
1023
1024 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] FORMAT: %s\n",
1025 (unsigned long) plane->plane_id,
1026 pinfo ? pinfo->drm_format_name : "UNKNOWN");
1027
1028 if (plane_state->in_fence_fd >= 0) {
1029 ret |= plane_add_prop(req, plane,
1030 WDRM_PLANE_IN_FENCE_FD,
1031 plane_state->in_fence_fd);
1032 }
1033
Marius Vladcdd6fa22019-08-29 20:42:00 +03001034 /* do note, that 'invented' zpos values are set as immutable */
1035 if (plane_state->zpos != DRM_PLANE_ZPOS_INVALID_PLANE &&
1036 plane_state->plane->zpos_min != plane_state->plane->zpos_max)
1037 ret |= plane_add_prop(req, plane,
1038 WDRM_PLANE_ZPOS,
1039 plane_state->zpos);
1040
Daniel Stone4c2fc702019-06-18 11:12:07 +01001041 if (ret != 0) {
1042 weston_log("couldn't set plane state\n");
1043 return ret;
1044 }
1045 }
1046
1047 return 0;
1048}
1049
1050/**
1051 * Helper function used only by drm_pending_state_apply, with the same
1052 * guarantees and constraints as that function.
1053 */
1054static int
1055drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
1056 enum drm_state_apply_mode mode)
1057{
1058 struct drm_backend *b = pending_state->backend;
1059 struct drm_output_state *output_state, *tmp;
1060 struct drm_plane *plane;
1061 drmModeAtomicReq *req = drmModeAtomicAlloc();
1062 uint32_t flags;
1063 int ret = 0;
1064
1065 if (!req)
1066 return -1;
1067
1068 switch (mode) {
1069 case DRM_STATE_APPLY_SYNC:
1070 flags = 0;
1071 break;
1072 case DRM_STATE_APPLY_ASYNC:
1073 flags = DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
1074 break;
1075 case DRM_STATE_TEST_ONLY:
1076 flags = DRM_MODE_ATOMIC_TEST_ONLY;
1077 break;
1078 }
1079
1080 if (b->state_invalid) {
1081 struct weston_head *head_base;
1082 struct drm_head *head;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001083 struct drm_crtc *crtc;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001084 uint32_t connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001085 int err;
1086
1087 drm_debug(b, "\t\t[atomic] previous state invalid; "
1088 "starting with fresh state\n");
1089
1090 /* If we need to reset all our state (e.g. because we've
1091 * just started, or just been VT-switched in), explicitly
1092 * disable all the CRTCs and connectors we aren't using. */
1093 wl_list_for_each(head_base,
1094 &b->compositor->head_list, compositor_link) {
1095 struct drm_property_info *info;
1096
1097 if (weston_head_is_enabled(head_base))
1098 continue;
1099
1100 head = to_drm_head(head_base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001101 connector_id = head->connector.connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001102
1103 drm_debug(b, "\t\t[atomic] disabling inactive head %s\n",
1104 head_base->name);
1105
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001106 info = &head->connector.props[WDRM_CONNECTOR_CRTC_ID];
1107 err = drmModeAtomicAddProperty(req, connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001108 info->prop_id, 0);
1109 drm_debug(b, "\t\t\t[CONN:%lu] %lu (%s) -> 0\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001110 (unsigned long) connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001111 (unsigned long) info->prop_id,
1112 info->name);
1113 if (err <= 0)
1114 ret = -1;
1115 }
1116
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001117 wl_list_for_each(crtc, &b->crtc_list, link) {
Daniel Stone4c2fc702019-06-18 11:12:07 +01001118 struct drm_property_info *info;
1119 drmModeObjectProperties *props;
1120 uint64_t active;
1121
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001122 /* Ignore CRTCs that are in use */
1123 if (crtc->output)
1124 continue;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001125
1126 /* We can't emit a disable on a CRTC that's already
1127 * off, as the kernel will refuse to generate an event
1128 * for an off->off state and fail the commit.
1129 */
1130 props = drmModeObjectGetProperties(b->drm.fd,
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001131 crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001132 DRM_MODE_OBJECT_CRTC);
1133 if (!props) {
1134 ret = -1;
1135 continue;
1136 }
1137
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001138 info = &crtc->props_crtc[WDRM_CRTC_ACTIVE];
Daniel Stone4c2fc702019-06-18 11:12:07 +01001139 active = drm_property_get_value(info, props, 0);
1140 drmModeFreeObjectProperties(props);
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001141 if (active == 0)
Daniel Stone4c2fc702019-06-18 11:12:07 +01001142 continue;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001143
1144 drm_debug(b, "\t\t[atomic] disabling unused CRTC %lu\n",
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001145 (unsigned long) crtc->crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001146
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001147 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 0);
1148 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001149 }
1150
1151 /* Disable all the planes; planes which are being used will
1152 * override this state in the output-state application. */
1153 wl_list_for_each(plane, &b->plane_list, link) {
1154 drm_debug(b, "\t\t[atomic] starting with plane %lu disabled\n",
1155 (unsigned long) plane->plane_id);
1156 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
1157 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
1158 }
1159
1160 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
1161 }
1162
1163 wl_list_for_each(output_state, &pending_state->output_list, link) {
1164 if (output_state->output->virtual)
1165 continue;
1166 if (mode == DRM_STATE_APPLY_SYNC)
1167 assert(output_state->dpms == WESTON_DPMS_OFF);
1168 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
1169 }
1170
1171 if (ret != 0) {
1172 weston_log("atomic: couldn't compile atomic state\n");
1173 goto out;
1174 }
1175
1176 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
1177 drm_debug(b, "[atomic] drmModeAtomicCommit\n");
1178
1179 /* Test commits do not take ownership of the state; return
1180 * without freeing here. */
1181 if (mode == DRM_STATE_TEST_ONLY) {
1182 drmModeAtomicFree(req);
1183 return ret;
1184 }
1185
1186 if (ret != 0) {
1187 weston_log("atomic: couldn't commit new state: %s\n",
1188 strerror(errno));
1189 goto out;
1190 }
1191
1192 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1193 link)
1194 drm_output_assign_state(output_state, mode);
1195
1196 b->state_invalid = false;
1197
1198 assert(wl_list_empty(&pending_state->output_list));
1199
1200out:
1201 drmModeAtomicFree(req);
1202 drm_pending_state_free(pending_state);
1203 return ret;
1204}
Daniel Stone4c2fc702019-06-18 11:12:07 +01001205
1206/**
1207 * Tests a pending state, to see if the kernel will accept the update as
1208 * constructed.
1209 *
1210 * Using atomic modesetting, the kernel performs the same checks as it would
1211 * on a real commit, returning success or failure without actually modifying
1212 * the running state. It does not return -EBUSY if there are pending updates
1213 * in flight, so states may be tested at any point, however this means a
1214 * state which passed testing may fail on a real commit if the timing is not
1215 * respected (e.g. committing before the previous commit has completed).
1216 *
1217 * Without atomic modesetting, we have no way to check, so we optimistically
1218 * claim it will work.
1219 *
1220 * Unlike drm_pending_state_apply() and drm_pending_state_apply_sync(), this
1221 * function does _not_ take ownership of pending_state, nor does it clear
1222 * state_invalid.
1223 */
1224int
1225drm_pending_state_test(struct drm_pending_state *pending_state)
1226{
Daniel Stone4c2fc702019-06-18 11:12:07 +01001227 struct drm_backend *b = pending_state->backend;
1228
1229 if (b->atomic_modeset)
1230 return drm_pending_state_apply_atomic(pending_state,
1231 DRM_STATE_TEST_ONLY);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001232
1233 /* We have no way to test state before application on the legacy
1234 * modesetting API, so just claim it succeeded. */
1235 return 0;
1236}
1237
1238/**
1239 * Applies all of a pending_state asynchronously: the primary entry point for
1240 * applying KMS state to a device. Updates the state for all outputs in the
1241 * pending_state, as well as disabling any unclaimed outputs.
1242 *
1243 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1244 */
1245int
1246drm_pending_state_apply(struct drm_pending_state *pending_state)
1247{
1248 struct drm_backend *b = pending_state->backend;
1249 struct drm_output_state *output_state, *tmp;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001250 struct drm_crtc *crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001251
Daniel Stone4c2fc702019-06-18 11:12:07 +01001252 if (b->atomic_modeset)
1253 return drm_pending_state_apply_atomic(pending_state,
1254 DRM_STATE_APPLY_ASYNC);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001255
1256 if (b->state_invalid) {
1257 /* If we need to reset all our state (e.g. because we've
1258 * just started, or just been VT-switched in), explicitly
1259 * disable all the CRTCs we aren't using. This also disables
1260 * all connectors on these CRTCs, so we don't need to do that
1261 * separately with the pre-atomic API. */
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001262 wl_list_for_each(crtc, &b->crtc_list, link) {
1263 if (crtc->output)
1264 continue;
1265 drmModeSetCrtc(b->drm.fd, crtc->crtc_id, 0, 0, 0,
1266 NULL, 0, NULL);
1267 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001268 }
1269
1270 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1271 link) {
1272 struct drm_output *output = output_state->output;
1273 int ret;
1274
1275 if (output->virtual) {
1276 drm_output_assign_state(output_state,
1277 DRM_STATE_APPLY_ASYNC);
1278 continue;
1279 }
1280
1281 ret = drm_output_apply_state_legacy(output_state);
1282 if (ret != 0) {
1283 weston_log("Couldn't apply state for output %s\n",
1284 output->base.name);
1285 }
1286 }
1287
1288 b->state_invalid = false;
1289
1290 assert(wl_list_empty(&pending_state->output_list));
1291
1292 drm_pending_state_free(pending_state);
1293
1294 return 0;
1295}
1296
1297/**
1298 * The synchronous version of drm_pending_state_apply. May only be used to
1299 * disable outputs. Does so synchronously: the request is guaranteed to have
1300 * completed on return, and the output will not be touched afterwards.
1301 *
1302 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1303 */
1304int
1305drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
1306{
1307 struct drm_backend *b = pending_state->backend;
1308 struct drm_output_state *output_state, *tmp;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001309 struct drm_crtc *crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001310
Daniel Stone4c2fc702019-06-18 11:12:07 +01001311 if (b->atomic_modeset)
1312 return drm_pending_state_apply_atomic(pending_state,
1313 DRM_STATE_APPLY_SYNC);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001314
1315 if (b->state_invalid) {
1316 /* If we need to reset all our state (e.g. because we've
1317 * just started, or just been VT-switched in), explicitly
1318 * disable all the CRTCs we aren't using. This also disables
1319 * all connectors on these CRTCs, so we don't need to do that
1320 * separately with the pre-atomic API. */
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001321 wl_list_for_each(crtc, &b->crtc_list, link) {
1322 if (crtc->output)
1323 continue;
1324 drmModeSetCrtc(b->drm.fd, crtc->crtc_id, 0, 0, 0,
1325 NULL, 0, NULL);
1326 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001327 }
1328
1329 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1330 link) {
1331 int ret;
1332
1333 assert(output_state->dpms == WESTON_DPMS_OFF);
1334 ret = drm_output_apply_state_legacy(output_state);
1335 if (ret != 0) {
1336 weston_log("Couldn't apply state for output %s\n",
1337 output_state->output->base.name);
1338 }
1339 }
1340
1341 b->state_invalid = false;
1342
1343 assert(wl_list_empty(&pending_state->output_list));
1344
1345 drm_pending_state_free(pending_state);
1346
1347 return 0;
1348}
1349
1350void
1351drm_output_update_msc(struct drm_output *output, unsigned int seq)
1352{
1353 uint64_t msc_hi = output->base.msc >> 32;
1354
1355 if (seq < (output->base.msc & 0xffffffff))
1356 msc_hi++;
1357
1358 output->base.msc = (msc_hi << 32) + seq;
1359}
1360
1361static void
1362page_flip_handler(int fd, unsigned int frame,
1363 unsigned int sec, unsigned int usec, void *data)
1364{
1365 struct drm_output *output = data;
1366 struct drm_backend *b = to_drm_backend(output->base.compositor);
1367 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1368 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1369 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1370
1371 drm_output_update_msc(output, frame);
1372
1373 assert(!b->atomic_modeset);
1374 assert(output->page_flip_pending);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001375 output->page_flip_pending = false;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001376
1377 drm_output_update_complete(output, flags, sec, usec);
1378}
1379
Daniel Stone4c2fc702019-06-18 11:12:07 +01001380static void
1381atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
1382 unsigned int usec, unsigned int crtc_id, void *data)
1383{
1384 struct drm_backend *b = data;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001385 struct drm_crtc *crtc;
1386 struct drm_output *output;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001387 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1388 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1389 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1390
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001391 crtc = drm_crtc_find(b, crtc_id);
1392 assert(crtc);
1393
1394 output = crtc->output;
1395
Daniel Stone4c2fc702019-06-18 11:12:07 +01001396 /* During the initial modeset, we can disable CRTCs which we don't
1397 * actually handle during normal operation; this will give us events
1398 * for unknown outputs. Ignore them. */
1399 if (!output || !output->base.enabled)
1400 return;
1401
1402 drm_output_update_msc(output, frame);
1403
1404 drm_debug(b, "[atomic][CRTC:%u] flip processing started\n", crtc_id);
1405 assert(b->atomic_modeset);
1406 assert(output->atomic_complete_pending);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001407 output->atomic_complete_pending = false;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001408
1409 drm_output_update_complete(output, flags, sec, usec);
1410 drm_debug(b, "[atomic][CRTC:%u] flip processing completed\n", crtc_id);
1411}
Daniel Stone4c2fc702019-06-18 11:12:07 +01001412
1413int
1414on_drm_input(int fd, uint32_t mask, void *data)
1415{
Daniel Stone4c2fc702019-06-18 11:12:07 +01001416 struct drm_backend *b = data;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001417 drmEventContext evctx;
1418
1419 memset(&evctx, 0, sizeof evctx);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001420 evctx.version = 3;
1421 if (b->atomic_modeset)
1422 evctx.page_flip_handler2 = atomic_flip_handler;
1423 else
Daniel Stone4c2fc702019-06-18 11:12:07 +01001424 evctx.page_flip_handler = page_flip_handler;
1425 drmHandleEvent(fd, &evctx);
1426
1427 return 1;
1428}
1429
1430int
1431init_kms_caps(struct drm_backend *b)
1432{
1433 uint64_t cap;
1434 int ret;
1435 clockid_t clk_id;
1436
1437 weston_log("using %s\n", b->drm.filename);
1438
1439 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1440 if (ret == 0 && cap == 1)
1441 clk_id = CLOCK_MONOTONIC;
1442 else
1443 clk_id = CLOCK_REALTIME;
1444
1445 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
1446 weston_log("Error: failed to set presentation clock %d.\n",
1447 clk_id);
1448 return -1;
1449 }
1450
1451 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
1452 if (ret == 0)
1453 b->cursor_width = cap;
1454 else
1455 b->cursor_width = 64;
1456
1457 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1458 if (ret == 0)
1459 b->cursor_height = cap;
1460 else
1461 b->cursor_height = 64;
1462
1463 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
1464 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
1465 b->universal_planes = (ret == 0);
1466 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001467
Daniel Stone4c2fc702019-06-18 11:12:07 +01001468 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
1469 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
1470 if (ret != 0)
1471 cap = 0;
1472 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
1473 b->atomic_modeset = ((ret == 0) && (cap == 1));
1474 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001475 weston_log("DRM: %s atomic modesetting\n",
1476 b->atomic_modeset ? "supports" : "does not support");
1477
Stefan Agner465ab2c2020-06-17 23:36:44 +02001478 if (!getenv("WESTON_DISABLE_GBM_MODIFIERS")) {
1479 ret = drmGetCap(b->drm.fd, DRM_CAP_ADDFB2_MODIFIERS, &cap);
1480 if (ret == 0)
1481 b->fb_modifiers = cap;
1482 }
1483 weston_log("DRM: %s GBM modifiers\n",
1484 b->fb_modifiers ? "supports" : "does not support");
Daniel Stone4c2fc702019-06-18 11:12:07 +01001485
Leandro Ribeiro96bef052020-09-09 15:23:49 -03001486 drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
1487
Daniel Stone4c2fc702019-06-18 11:12:07 +01001488 /*
1489 * KMS support for hardware planes cannot properly synchronize
1490 * without nuclear page flip. Without nuclear/atomic, hw plane
1491 * and cursor plane updates would either tear or cause extra
1492 * waits for vblanks which means dropping the compositor framerate
1493 * to a fraction. For cursors, it's not so bad, so they are
1494 * enabled.
1495 */
1496 if (!b->atomic_modeset || getenv("WESTON_FORCE_RENDERER"))
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001497 b->sprites_are_broken = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001498
1499 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1);
1500 b->aspect_ratio_supported = (ret == 0);
1501 weston_log("DRM: %s picture aspect ratio\n",
1502 b->aspect_ratio_supported ? "supports" : "does not support");
1503
1504 return 0;
1505}