blob: d508ab5ca47e94f62162fb1aa77a41427d150415 [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>
36#include <drm_fourcc.h>
37
Daniel Stone4c2fc702019-06-18 11:12:07 +010038#include <libweston/libweston.h>
39#include <libweston/backend-drm.h>
40#include "shared/helpers.h"
41#include "drm-internal.h"
42#include "pixel-formats.h"
43#include "presentation-time-server-protocol.h"
44
Tomohito Esaki29beeaf2019-06-24 17:23:44 +090045#ifndef DRM_FORMAT_MOD_LINEAR
46#define DRM_FORMAT_MOD_LINEAR 0
47#endif
48
Daniel Stone4c2fc702019-06-18 11:12:07 +010049struct drm_property_enum_info plane_type_enums[] = {
50 [WDRM_PLANE_TYPE_PRIMARY] = {
51 .name = "Primary",
52 },
53 [WDRM_PLANE_TYPE_OVERLAY] = {
54 .name = "Overlay",
55 },
56 [WDRM_PLANE_TYPE_CURSOR] = {
57 .name = "Cursor",
58 },
59};
60
61const struct drm_property_info plane_props[] = {
62 [WDRM_PLANE_TYPE] = {
63 .name = "type",
64 .enum_values = plane_type_enums,
65 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
66 },
67 [WDRM_PLANE_SRC_X] = { .name = "SRC_X", },
68 [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", },
69 [WDRM_PLANE_SRC_W] = { .name = "SRC_W", },
70 [WDRM_PLANE_SRC_H] = { .name = "SRC_H", },
71 [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", },
72 [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", },
73 [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", },
74 [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", },
75 [WDRM_PLANE_FB_ID] = { .name = "FB_ID", },
76 [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", },
77 [WDRM_PLANE_IN_FORMATS] = { .name = "IN_FORMATS" },
78 [WDRM_PLANE_IN_FENCE_FD] = { .name = "IN_FENCE_FD" },
79 [WDRM_PLANE_FB_DAMAGE_CLIPS] = { .name = "FB_DAMAGE_CLIPS" },
Marius Vladcdd6fa22019-08-29 20:42:00 +030080 [WDRM_PLANE_ZPOS] = { .name = "zpos" },
Daniel Stone4c2fc702019-06-18 11:12:07 +010081};
82
83struct drm_property_enum_info dpms_state_enums[] = {
84 [WDRM_DPMS_STATE_OFF] = {
85 .name = "Off",
86 },
87 [WDRM_DPMS_STATE_ON] = {
88 .name = "On",
89 },
90 [WDRM_DPMS_STATE_STANDBY] = {
91 .name = "Standby",
92 },
93 [WDRM_DPMS_STATE_SUSPEND] = {
94 .name = "Suspend",
95 },
96};
97
Ankit Nautiyala344fe32019-05-14 18:36:08 +053098struct drm_property_enum_info content_protection_enums[] = {
99 [WDRM_CONTENT_PROTECTION_UNDESIRED] = {
100 .name = "Undesired",
101 },
102 [WDRM_CONTENT_PROTECTION_DESIRED] = {
103 .name = "Desired",
104 },
105 [WDRM_CONTENT_PROTECTION_ENABLED] = {
106 .name = "Enabled",
107 },
108};
109
110struct drm_property_enum_info hdcp_content_type_enums[] = {
111 [WDRM_HDCP_CONTENT_TYPE0] = {
112 .name = "HDCP Type0",
113 },
114 [WDRM_HDCP_CONTENT_TYPE1] = {
115 .name = "HDCP Type1",
116 },
117};
118
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000119struct drm_property_enum_info panel_orientation_enums[] = {
120 [WDRM_PANEL_ORIENTATION_NORMAL] = { .name = "Normal", },
121 [WDRM_PANEL_ORIENTATION_UPSIDE_DOWN] = { .name = "Upside Down", },
122 [WDRM_PANEL_ORIENTATION_LEFT_SIDE_UP] = { .name = "Left Side Up", },
123 [WDRM_PANEL_ORIENTATION_RIGHT_SIDE_UP] = { .name = "Right Side Up", },
124};
125
Daniel Stone4c2fc702019-06-18 11:12:07 +0100126const struct drm_property_info connector_props[] = {
127 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
128 [WDRM_CONNECTOR_DPMS] = {
129 .name = "DPMS",
130 .enum_values = dpms_state_enums,
131 .num_enum_values = WDRM_DPMS_STATE__COUNT,
132 },
133 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
134 [WDRM_CONNECTOR_NON_DESKTOP] = { .name = "non-desktop", },
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530135 [WDRM_CONNECTOR_CONTENT_PROTECTION] = {
136 .name = "Content Protection",
137 .enum_values = content_protection_enums,
138 .num_enum_values = WDRM_CONTENT_PROTECTION__COUNT,
139 },
140 [WDRM_CONNECTOR_HDCP_CONTENT_TYPE] = {
141 .name = "HDCP Content Type",
142 .enum_values = hdcp_content_type_enums,
143 .num_enum_values = WDRM_HDCP_CONTENT_TYPE__COUNT,
144 },
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000145 [WDRM_CONNECTOR_PANEL_ORIENTATION] = {
146 .name = "panel orientation",
147 .enum_values = panel_orientation_enums,
148 .num_enum_values = WDRM_PANEL_ORIENTATION__COUNT,
149 },
Daniel Stone4c2fc702019-06-18 11:12:07 +0100150};
151
152const struct drm_property_info crtc_props[] = {
153 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
154 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
155};
156
157
158/**
159 * Mode for drm_pending_state_apply and co.
160 */
161enum drm_state_apply_mode {
162 DRM_STATE_APPLY_SYNC, /**< state fully processed */
163 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
164 DRM_STATE_TEST_ONLY, /**< test if the state can be applied */
165};
166
167/**
168 * Get the current value of a KMS property
169 *
170 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
171 * for the target property, return the current value of that property,
172 * with an optional default. If the property is a KMS enum type, the return
173 * value will be translated into the appropriate internal enum.
174 *
175 * If the property is not present, the default value will be returned.
176 *
177 * @param info Internal structure for property to look up
178 * @param props Raw KMS properties for the target object
179 * @param def Value to return if property is not found
180 */
181uint64_t
182drm_property_get_value(struct drm_property_info *info,
183 const drmModeObjectProperties *props,
184 uint64_t def)
185{
186 unsigned int i;
187
188 if (info->prop_id == 0)
189 return def;
190
191 for (i = 0; i < props->count_props; i++) {
192 unsigned int j;
193
194 if (props->props[i] != info->prop_id)
195 continue;
196
197 /* Simple (non-enum) types can return the value directly */
198 if (info->num_enum_values == 0)
199 return props->prop_values[i];
200
201 /* Map from raw value to enum value */
202 for (j = 0; j < info->num_enum_values; j++) {
203 if (!info->enum_values[j].valid)
204 continue;
205 if (info->enum_values[j].value != props->prop_values[i])
206 continue;
207
208 return j;
209 }
210
211 /* We don't have a mapping for this enum; return default. */
212 break;
213 }
214
215 return def;
216}
217
218/**
Marius Vlad1accffe2019-11-01 12:00:09 +0200219 * Get the current range values of a KMS property
220 *
221 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
222 * for the target property, return the current range values of that property,
223 *
224 * If the property is not present, or there's no it is not a prop range then
225 * NULL will be returned.
226 *
227 * @param info Internal structure for property to look up
228 * @param props Raw KMS properties for the target object
229 */
230uint64_t *
231drm_property_get_range_values(struct drm_property_info *info,
232 const drmModeObjectProperties *props)
233{
234 unsigned int i;
235
236 if (info->prop_id == 0)
237 return NULL;
238
239 for (i = 0; i < props->count_props; i++) {
240
241 if (props->props[i] != info->prop_id)
242 continue;
243
244 if (!(info->flags & DRM_MODE_PROP_RANGE) &&
245 !(info->flags & DRM_MODE_PROP_SIGNED_RANGE))
246 continue;
247
248 return info->range_values;
249 }
250
251 return NULL;
252}
253
254/**
Daniel Stone4c2fc702019-06-18 11:12:07 +0100255 * Cache DRM property values
256 *
257 * Update a per-object array of drm_property_info structures, given the
258 * DRM properties of the object.
259 *
260 * Call this every time an object newly appears (note that only connectors
261 * can be hotplugged), the first time it is seen, or when its status changes
262 * in a way which invalidates the potential property values (currently, the
263 * only case for this is connector hotplug).
264 *
265 * This updates the property IDs and enum values within the drm_property_info
266 * array.
267 *
268 * DRM property enum values are dynamic at runtime; the user must query the
269 * property to find out the desired runtime value for a requested string
270 * name. Using the 'type' field on planes as an example, there is no single
271 * hardcoded constant for primary plane types; instead, the property must be
272 * queried at runtime to find the value associated with the string "Primary".
273 *
274 * This helper queries and caches the enum values, to allow us to use a set
275 * of compile-time-constant enums portably across various implementations.
276 * The values given in enum_names are searched for, and stored in the
277 * same-indexed field of the map array.
278 *
279 * @param b DRM backend object
280 * @param src DRM property info array to source from
281 * @param info DRM property info array to copy into
282 * @param num_infos Number of entries in the source array
283 * @param props DRM object properties for the object
284 */
285void
286drm_property_info_populate(struct drm_backend *b,
287 const struct drm_property_info *src,
288 struct drm_property_info *info,
289 unsigned int num_infos,
290 drmModeObjectProperties *props)
291{
292 drmModePropertyRes *prop;
293 unsigned i, j;
294
295 for (i = 0; i < num_infos; i++) {
296 unsigned int j;
297
298 info[i].name = src[i].name;
299 info[i].prop_id = 0;
300 info[i].num_enum_values = src[i].num_enum_values;
301
302 if (src[i].num_enum_values == 0)
303 continue;
304
305 info[i].enum_values =
306 malloc(src[i].num_enum_values *
307 sizeof(*info[i].enum_values));
308 assert(info[i].enum_values);
309 for (j = 0; j < info[i].num_enum_values; j++) {
310 info[i].enum_values[j].name = src[i].enum_values[j].name;
311 info[i].enum_values[j].valid = false;
312 }
313 }
314
315 for (i = 0; i < props->count_props; i++) {
316 unsigned int k;
317
318 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
319 if (!prop)
320 continue;
321
322 for (j = 0; j < num_infos; j++) {
323 if (!strcmp(prop->name, info[j].name))
324 break;
325 }
326
327 /* We don't know/care about this property. */
328 if (j == num_infos) {
329#ifdef DEBUG
330 weston_log("DRM debug: unrecognized property %u '%s'\n",
331 prop->prop_id, prop->name);
332#endif
333 drmModeFreeProperty(prop);
334 continue;
335 }
336
337 if (info[j].num_enum_values == 0 &&
338 (prop->flags & DRM_MODE_PROP_ENUM)) {
339 weston_log("DRM: expected property %s to not be an"
340 " enum, but it is; ignoring\n", prop->name);
341 drmModeFreeProperty(prop);
342 continue;
343 }
344
345 info[j].prop_id = props->props[i];
Marius Vlad1accffe2019-11-01 12:00:09 +0200346 info[j].flags = prop->flags;
347
348 if (prop->flags & DRM_MODE_PROP_RANGE ||
349 prop->flags & DRM_MODE_PROP_SIGNED_RANGE) {
350 info[j].num_range_values = prop->count_values;
351 for (int i = 0; i < prop->count_values; i++)
352 info[j].range_values[i] = prop->values[i];
353 }
354
Daniel Stone4c2fc702019-06-18 11:12:07 +0100355
356 if (info[j].num_enum_values == 0) {
357 drmModeFreeProperty(prop);
358 continue;
359 }
360
361 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
362 weston_log("DRM: expected property %s to be an enum,"
363 " but it is not; ignoring\n", prop->name);
364 drmModeFreeProperty(prop);
365 info[j].prop_id = 0;
366 continue;
367 }
368
369 for (k = 0; k < info[j].num_enum_values; k++) {
370 int l;
371
372 for (l = 0; l < prop->count_enums; l++) {
373 if (!strcmp(prop->enums[l].name,
374 info[j].enum_values[k].name))
375 break;
376 }
377
378 if (l == prop->count_enums)
379 continue;
380
381 info[j].enum_values[k].valid = true;
382 info[j].enum_values[k].value = prop->enums[l].value;
383 }
384
385 drmModeFreeProperty(prop);
386 }
387
388#ifdef DEBUG
389 for (i = 0; i < num_infos; i++) {
390 if (info[i].prop_id == 0)
391 weston_log("DRM warning: property '%s' missing\n",
392 info[i].name);
393 }
394#endif
395}
396
397/**
398 * Free DRM property information
399 *
400 * Frees all memory associated with a DRM property info array and zeroes
401 * it out, leaving it usable for a further drm_property_info_update() or
402 * drm_property_info_free().
403 *
404 * @param info DRM property info array
405 * @param num_props Number of entries in array to free
406 */
407void
408drm_property_info_free(struct drm_property_info *info, int num_props)
409{
410 int i;
411
412 for (i = 0; i < num_props; i++)
413 free(info[i].enum_values);
414
415 memset(info, 0, sizeof(*info) * num_props);
416}
417
Daniel Stone4c2fc702019-06-18 11:12:07 +0100418static inline uint32_t *
419formats_ptr(struct drm_format_modifier_blob *blob)
420{
421 return (uint32_t *)(((char *)blob) + blob->formats_offset);
422}
423
424static inline struct drm_format_modifier *
425modifiers_ptr(struct drm_format_modifier_blob *blob)
426{
427 return (struct drm_format_modifier *)
428 (((char *)blob) + blob->modifiers_offset);
429}
Daniel Stone4c2fc702019-06-18 11:12:07 +0100430
431/**
432 * Populates the plane's formats array, using either the IN_FORMATS blob
433 * property (if available), or the plane's format list if not.
434 */
435int
436drm_plane_populate_formats(struct drm_plane *plane, const drmModePlane *kplane,
Stefan Agner465ab2c2020-06-17 23:36:44 +0200437 const drmModeObjectProperties *props,
438 const bool use_modifiers)
Daniel Stone4c2fc702019-06-18 11:12:07 +0100439{
440 unsigned i;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100441 drmModePropertyBlobRes *blob;
442 struct drm_format_modifier_blob *fmt_mod_blob;
443 struct drm_format_modifier *blob_modifiers;
444 uint32_t *blob_formats;
445 uint32_t blob_id;
446
Stefan Agner465ab2c2020-06-17 23:36:44 +0200447 if (!use_modifiers)
448 goto fallback;
449
Daniel Stone4c2fc702019-06-18 11:12:07 +0100450 blob_id = drm_property_get_value(&plane->props[WDRM_PLANE_IN_FORMATS],
451 props,
452 0);
453 if (blob_id == 0)
454 goto fallback;
455
456 blob = drmModeGetPropertyBlob(plane->backend->drm.fd, blob_id);
457 if (!blob)
458 goto fallback;
459
460 fmt_mod_blob = blob->data;
461 blob_formats = formats_ptr(fmt_mod_blob);
462 blob_modifiers = modifiers_ptr(fmt_mod_blob);
463
464 if (plane->count_formats != fmt_mod_blob->count_formats) {
465 weston_log("DRM backend: format count differs between "
466 "plane (%d) and IN_FORMATS (%d)\n",
467 plane->count_formats,
468 fmt_mod_blob->count_formats);
469 weston_log("This represents a kernel bug; Weston is "
470 "unable to continue.\n");
471 abort();
472 }
473
474 for (i = 0; i < fmt_mod_blob->count_formats; i++) {
475 uint32_t count_modifiers = 0;
476 uint64_t *modifiers = NULL;
477 unsigned j;
478
479 for (j = 0; j < fmt_mod_blob->count_modifiers; j++) {
480 struct drm_format_modifier *mod = &blob_modifiers[j];
481
482 if ((i < mod->offset) || (i > mod->offset + 63))
483 continue;
484 if (!(mod->formats & (1 << (i - mod->offset))))
485 continue;
486
487 modifiers = realloc(modifiers,
488 (count_modifiers + 1) *
489 sizeof(modifiers[0]));
490 assert(modifiers);
491 modifiers[count_modifiers++] = mod->modifier;
492 }
493
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900494 if (count_modifiers == 0) {
495 modifiers = malloc(sizeof(*modifiers));
496 *modifiers = DRM_FORMAT_MOD_LINEAR;
497 count_modifiers = 1;
498 }
499
Daniel Stone4c2fc702019-06-18 11:12:07 +0100500 plane->formats[i].format = blob_formats[i];
501 plane->formats[i].modifiers = modifiers;
502 plane->formats[i].count_modifiers = count_modifiers;
503 }
504
505 drmModeFreePropertyBlob(blob);
506
507 return 0;
508
509fallback:
Daniel Stone4c2fc702019-06-18 11:12:07 +0100510 /* No IN_FORMATS blob available, so just use the old. */
511 assert(plane->count_formats == kplane->count_formats);
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900512 for (i = 0; i < kplane->count_formats; i++) {
Daniel Stone4c2fc702019-06-18 11:12:07 +0100513 plane->formats[i].format = kplane->formats[i];
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900514 plane->formats[i].modifiers = malloc(sizeof(uint64_t));
515 plane->formats[i].modifiers[0] = DRM_FORMAT_MOD_LINEAR;
516 plane->formats[i].count_modifiers = 1;
517 }
Daniel Stone4c2fc702019-06-18 11:12:07 +0100518
519 return 0;
520}
521
522void
523drm_output_set_gamma(struct weston_output *output_base,
524 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
525{
526 int rc;
527 struct drm_output *output = to_drm_output(output_base);
528 struct drm_backend *backend =
529 to_drm_backend(output->base.compositor);
530
531 /* check */
532 if (output_base->gamma_size != size)
533 return;
534
535 rc = drmModeCrtcSetGamma(backend->drm.fd,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300536 output->crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100537 size, r, g, b);
538 if (rc)
539 weston_log("set gamma failed: %s\n", strerror(errno));
540}
541
542/**
543 * Mark an output state as current on the output, i.e. it has been
544 * submitted to the kernel. The mode argument determines whether this
545 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
546 * or asynchronously (in which case we wait for events to complete).
547 */
548static void
549drm_output_assign_state(struct drm_output_state *state,
550 enum drm_state_apply_mode mode)
551{
552 struct drm_output *output = state->output;
553 struct drm_backend *b = to_drm_backend(output->base.compositor);
554 struct drm_plane_state *plane_state;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530555 struct drm_head *head;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100556
557 assert(!output->state_last);
558
559 if (mode == DRM_STATE_APPLY_ASYNC)
560 output->state_last = output->state_cur;
561 else
562 drm_output_state_free(output->state_cur);
563
564 wl_list_remove(&state->link);
565 wl_list_init(&state->link);
566 state->pending_state = NULL;
567
568 output->state_cur = state;
569
570 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300571 drm_debug(b, "\t[CRTC:%u] setting pending flip\n",
572 output->crtc->crtc_id);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100573 output->atomic_complete_pending = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100574 }
575
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530576 if (b->atomic_modeset &&
577 state->protection == WESTON_HDCP_DISABLE)
578 wl_list_for_each(head, &output->base.head_list, base.output_link)
579 weston_head_set_content_protection_status(&head->base,
580 WESTON_HDCP_DISABLE);
581
Daniel Stone4c2fc702019-06-18 11:12:07 +0100582 /* Replace state_cur on each affected plane with the new state, being
583 * careful to dispose of orphaned (but only orphaned) previous state.
584 * If the previous state is not orphaned (still has an output_state
585 * attached), it will be disposed of by freeing the output_state. */
586 wl_list_for_each(plane_state, &state->plane_list, link) {
587 struct drm_plane *plane = plane_state->plane;
588
589 if (plane->state_cur && !plane->state_cur->output_state)
590 drm_plane_state_free(plane->state_cur, true);
591 plane->state_cur = plane_state;
592
593 if (mode != DRM_STATE_APPLY_ASYNC) {
594 plane_state->complete = true;
595 continue;
596 }
597
598 if (b->atomic_modeset)
599 continue;
600
601 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY);
602 if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100603 output->page_flip_pending = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100604 }
605}
606
607static void
608drm_output_set_cursor(struct drm_output_state *output_state)
609{
610 struct drm_output *output = output_state->output;
611 struct drm_backend *b = to_drm_backend(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300612 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100613 struct drm_plane *plane = output->cursor_plane;
614 struct drm_plane_state *state;
Stefan Agner974390a2019-07-08 00:42:05 +0200615 uint32_t handle;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100616
617 if (!plane)
618 return;
619
620 state = drm_output_state_get_existing_plane(output_state, plane);
621 if (!state)
622 return;
623
624 if (!state->fb) {
625 pixman_region32_fini(&plane->base.damage);
626 pixman_region32_init(&plane->base.damage);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300627 drmModeSetCursor(b->drm.fd, crtc->crtc_id, 0, 0, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100628 return;
629 }
630
631 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
632 assert(!plane->state_cur->output || plane->state_cur->output == output);
633
Stefan Agner974390a2019-07-08 00:42:05 +0200634 handle = output->gbm_cursor_handle[output->current_cursor];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100635 if (plane->state_cur->fb != state->fb) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300636 if (drmModeSetCursor(b->drm.fd, crtc->crtc_id, handle,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100637 b->cursor_width, b->cursor_height)) {
638 weston_log("failed to set cursor: %s\n",
639 strerror(errno));
640 goto err;
641 }
642 }
643
644 pixman_region32_fini(&plane->base.damage);
645 pixman_region32_init(&plane->base.damage);
646
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300647 if (drmModeMoveCursor(b->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100648 state->dest_x, state->dest_y)) {
649 weston_log("failed to move cursor: %s\n", strerror(errno));
650 goto err;
651 }
652
653 return;
654
655err:
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100656 b->cursors_are_broken = true;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300657 drmModeSetCursor(b->drm.fd, crtc->crtc_id, 0, 0, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100658}
659
660static int
661drm_output_apply_state_legacy(struct drm_output_state *state)
662{
663 struct drm_output *output = state->output;
664 struct drm_backend *backend = to_drm_backend(output->base.compositor);
665 struct drm_plane *scanout_plane = output->scanout_plane;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300666 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100667 struct drm_property_info *dpms_prop;
668 struct drm_plane_state *scanout_state;
669 struct drm_mode *mode;
670 struct drm_head *head;
671 const struct pixel_format_info *pinfo = NULL;
672 uint32_t connectors[MAX_CLONED_CONNECTORS];
673 int n_conn = 0;
674 struct timespec now;
675 int ret = 0;
676
677 wl_list_for_each(head, &output->base.head_list, base.output_link) {
678 assert(n_conn < MAX_CLONED_CONNECTORS);
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300679 connectors[n_conn++] = head->connector.connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100680 }
681
682 /* If disable_planes is set then assign_planes() wasn't
683 * called for this render, so we could still have a stale
684 * cursor plane set up.
685 */
686 if (output->base.disable_planes) {
687 output->cursor_view = NULL;
688 if (output->cursor_plane) {
689 output->cursor_plane->base.x = INT32_MIN;
690 output->cursor_plane->base.y = INT32_MIN;
691 }
692 }
693
694 if (state->dpms != WESTON_DPMS_ON) {
695 if (output->cursor_plane) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300696 ret = drmModeSetCursor(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100697 0, 0, 0);
698 if (ret)
699 weston_log("drmModeSetCursor failed disable: %s\n",
700 strerror(errno));
701 }
702
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300703 ret = drmModeSetCrtc(backend->drm.fd, crtc->crtc_id, 0, 0, 0,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100704 NULL, 0, NULL);
705 if (ret)
706 weston_log("drmModeSetCrtc failed disabling: %s\n",
707 strerror(errno));
708
709 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
710 weston_compositor_read_presentation_clock(output->base.compositor, &now);
711 drm_output_update_complete(output,
712 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
713 now.tv_sec, now.tv_nsec / 1000);
714
715 return 0;
716 }
717
718 scanout_state =
719 drm_output_state_get_existing_plane(state, scanout_plane);
720
721 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
722 * legacy PageFlip API doesn't allow us to do clipping either. */
723 assert(scanout_state->src_x == 0);
724 assert(scanout_state->src_y == 0);
725 assert(scanout_state->src_w ==
726 (unsigned) (output->base.current_mode->width << 16));
727 assert(scanout_state->src_h ==
728 (unsigned) (output->base.current_mode->height << 16));
729 assert(scanout_state->dest_x == 0);
730 assert(scanout_state->dest_y == 0);
731 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
732 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
733 /* The legacy SetCrtc API doesn't support fences */
734 assert(scanout_state->in_fence_fd == -1);
735
736 mode = to_drm_mode(output->base.current_mode);
737 if (backend->state_invalid ||
738 !scanout_plane->state_cur->fb ||
739 scanout_plane->state_cur->fb->strides[0] !=
740 scanout_state->fb->strides[0]) {
741
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300742 ret = drmModeSetCrtc(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100743 scanout_state->fb->fb_id,
744 0, 0,
745 connectors, n_conn,
746 &mode->mode_info);
747 if (ret) {
748 weston_log("set mode failed: %s\n", strerror(errno));
749 goto err;
750 }
751 }
752
753 pinfo = scanout_state->fb->format;
754 drm_debug(backend, "\t[CRTC:%u, PLANE:%u] FORMAT: %s\n",
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300755 crtc->crtc_id, scanout_state->plane->plane_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100756 pinfo ? pinfo->drm_format_name : "UNKNOWN");
757
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300758 if (drmModePageFlip(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100759 scanout_state->fb->fb_id,
760 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
761 weston_log("queueing pageflip failed: %s\n", strerror(errno));
762 goto err;
763 }
764
765 assert(!output->page_flip_pending);
766
767 if (output->pageflip_timer)
768 wl_event_source_timer_update(output->pageflip_timer,
769 backend->pageflip_timeout);
770
771 drm_output_set_cursor(state);
772
773 if (state->dpms != output->state_cur->dpms) {
774 wl_list_for_each(head, &output->base.head_list, base.output_link) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300775 dpms_prop = &head->connector.props[WDRM_CONNECTOR_DPMS];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100776 if (dpms_prop->prop_id == 0)
777 continue;
778
779 ret = drmModeConnectorSetProperty(backend->drm.fd,
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300780 head->connector.connector_id,
781 dpms_prop->prop_id,
782 state->dpms);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100783 if (ret) {
784 weston_log("DRM: DPMS: failed property set for %s\n",
785 head->base.name);
786 }
787 }
788 }
789
790 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
791
792 return 0;
793
794err:
795 output->cursor_view = NULL;
796 drm_output_state_free(state);
797 return -1;
798}
799
Daniel Stone4c2fc702019-06-18 11:12:07 +0100800static int
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300801crtc_add_prop(drmModeAtomicReq *req, struct drm_crtc *crtc,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100802 enum wdrm_crtc_property prop, uint64_t val)
803{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300804 struct drm_property_info *info = &crtc->props_crtc[prop];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100805 int ret;
806
807 if (info->prop_id == 0)
808 return -1;
809
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300810 ret = drmModeAtomicAddProperty(req, crtc->crtc_id, info->prop_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100811 val);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300812 drm_debug(crtc->backend, "\t\t\t[CRTC:%lu] %lu (%s) -> %llu (0x%llx)\n",
813 (unsigned long) crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100814 (unsigned long) info->prop_id, info->name,
815 (unsigned long long) val, (unsigned long long) val);
816 return (ret <= 0) ? -1 : 0;
817}
818
819static int
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300820connector_add_prop(drmModeAtomicReq *req, struct drm_connector *connector,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100821 enum wdrm_connector_property prop, uint64_t val)
822{
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300823 struct drm_property_info *info = &connector->props[prop];
824 uint32_t connector_id = connector->connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100825 int ret;
826
827 if (info->prop_id == 0)
828 return -1;
829
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300830 ret = drmModeAtomicAddProperty(req, connector_id, info->prop_id, val);
831 drm_debug(connector->backend, "\t\t\t[CONN:%lu] %lu (%s) -> %llu (0x%llx)\n",
832 (unsigned long) connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100833 (unsigned long) info->prop_id, info->name,
834 (unsigned long long) val, (unsigned long long) val);
835 return (ret <= 0) ? -1 : 0;
836}
837
838static int
839plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
840 enum wdrm_plane_property prop, uint64_t val)
841{
842 struct drm_property_info *info = &plane->props[prop];
843 int ret;
844
845 if (info->prop_id == 0)
846 return -1;
847
848 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
849 val);
850 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] %lu (%s) -> %llu (0x%llx)\n",
851 (unsigned long) plane->plane_id,
852 (unsigned long) info->prop_id, info->name,
853 (unsigned long long) val, (unsigned long long) val);
854 return (ret <= 0) ? -1 : 0;
855}
856
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530857static bool
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300858drm_connector_has_prop(struct drm_connector *connector,
859 enum wdrm_connector_property prop)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530860{
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300861 if (connector->props[prop].prop_id != 0)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530862 return true;
863
864 return false;
865}
866
867/*
868 * This function converts the protection requests from weston_hdcp_protection
869 * corresponding drm values. These values can be set in "Content Protection"
870 * & "HDCP Content Type" connector properties.
871 */
872static void
873get_drm_protection_from_weston(enum weston_hdcp_protection weston_protection,
874 enum wdrm_content_protection_state *drm_protection,
875 enum wdrm_hdcp_content_type *drm_cp_type)
876{
877
878 switch (weston_protection) {
879 case WESTON_HDCP_DISABLE:
880 *drm_protection = WDRM_CONTENT_PROTECTION_UNDESIRED;
881 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
882 break;
883 case WESTON_HDCP_ENABLE_TYPE_0:
884 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
885 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
886 break;
887 case WESTON_HDCP_ENABLE_TYPE_1:
888 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
889 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE1;
890 break;
891 default:
892 assert(0 && "bad weston_hdcp_protection");
893 }
894}
895
896static void
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300897drm_connector_set_hdcp_property(struct drm_connector *connector,
898 enum weston_hdcp_protection protection,
899 drmModeAtomicReq *req)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530900{
901 int ret;
902 enum wdrm_content_protection_state drm_protection;
903 enum wdrm_hdcp_content_type drm_cp_type;
904 struct drm_property_enum_info *enum_info;
905 uint64_t prop_val;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300906 struct drm_property_info *props = connector->props;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530907
908 get_drm_protection_from_weston(protection, &drm_protection,
909 &drm_cp_type);
910
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300911 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_CONTENT_PROTECTION))
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530912 return;
913
914 /*
915 * Content-type property is not exposed for platforms not supporting
916 * HDCP2.2, therefore, type-1 cannot be supported. The type-0 content
917 * still can be supported if the content-protection property is exposed.
918 */
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300919 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_HDCP_CONTENT_TYPE) &&
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530920 drm_cp_type != WDRM_HDCP_CONTENT_TYPE0)
921 return;
922
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300923 enum_info = props[WDRM_CONNECTOR_CONTENT_PROTECTION].enum_values;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530924 prop_val = enum_info[drm_protection].value;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300925 ret = connector_add_prop(req, connector,
926 WDRM_CONNECTOR_CONTENT_PROTECTION, prop_val);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530927 assert(ret == 0);
928
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300929 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_HDCP_CONTENT_TYPE))
Ankit Nautiyalfc2c1802019-08-30 19:40:46 +0530930 return;
931
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300932 enum_info = props[WDRM_CONNECTOR_HDCP_CONTENT_TYPE].enum_values;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530933 prop_val = enum_info[drm_cp_type].value;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300934 ret = connector_add_prop(req, connector,
935 WDRM_CONNECTOR_HDCP_CONTENT_TYPE, prop_val);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530936 assert(ret == 0);
937}
938
Daniel Stone4c2fc702019-06-18 11:12:07 +0100939static int
940drm_output_apply_state_atomic(struct drm_output_state *state,
941 drmModeAtomicReq *req,
942 uint32_t *flags)
943{
944 struct drm_output *output = state->output;
945 struct drm_backend *b = to_drm_backend(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300946 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100947 struct drm_plane_state *plane_state;
948 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
949 struct drm_head *head;
950 int ret = 0;
951
952 drm_debug(b, "\t\t[atomic] %s output %lu (%s) state\n",
953 (*flags & DRM_MODE_ATOMIC_TEST_ONLY) ? "testing" : "applying",
954 (unsigned long) output->base.id, output->base.name);
955
956 if (state->dpms != output->state_cur->dpms) {
957 drm_debug(b, "\t\t\t[atomic] DPMS state differs, modeset OK\n");
958 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
959 }
960
961 if (state->dpms == WESTON_DPMS_ON) {
962 ret = drm_mode_ensure_blob(b, current_mode);
963 if (ret != 0)
964 return ret;
965
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300966 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100967 current_mode->blob_id);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300968 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 1);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100969
970 /* No need for the DPMS property, since it is implicit in
971 * routing and CRTC activity. */
972 wl_list_for_each(head, &output->base.head_list, base.output_link) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300973 ret |= connector_add_prop(req, &head->connector,
974 WDRM_CONNECTOR_CRTC_ID,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300975 crtc->crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100976 }
977 } else {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300978 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID, 0);
979 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100980
981 /* No need for the DPMS property, since it is implicit in
982 * routing and CRTC activity. */
983 wl_list_for_each(head, &output->base.head_list, base.output_link)
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300984 ret |= connector_add_prop(req, &head->connector,
985 WDRM_CONNECTOR_CRTC_ID, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100986 }
987
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530988 wl_list_for_each(head, &output->base.head_list, base.output_link)
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300989 drm_connector_set_hdcp_property(&head->connector,
990 state->protection, req);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530991
Daniel Stone4c2fc702019-06-18 11:12:07 +0100992 if (ret != 0) {
993 weston_log("couldn't set atomic CRTC/connector state\n");
994 return ret;
995 }
996
997 wl_list_for_each(plane_state, &state->plane_list, link) {
998 struct drm_plane *plane = plane_state->plane;
999 const struct pixel_format_info *pinfo = NULL;
1000
1001 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
1002 plane_state->fb ? plane_state->fb->fb_id : 0);
1003 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001004 plane_state->fb ? crtc->crtc_id : 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001005 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
1006 plane_state->src_x);
1007 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
1008 plane_state->src_y);
1009 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
1010 plane_state->src_w);
1011 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
1012 plane_state->src_h);
1013 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
1014 plane_state->dest_x);
1015 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
1016 plane_state->dest_y);
1017 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
1018 plane_state->dest_w);
1019 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
1020 plane_state->dest_h);
Scott Anderson15c603c2020-06-02 17:39:43 +12001021 if (plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS].prop_id != 0)
1022 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_DAMAGE_CLIPS,
1023 plane_state->damage_blob_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001024
1025 if (plane_state->fb && plane_state->fb->format)
1026 pinfo = plane_state->fb->format;
1027
1028 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] FORMAT: %s\n",
1029 (unsigned long) plane->plane_id,
1030 pinfo ? pinfo->drm_format_name : "UNKNOWN");
1031
1032 if (plane_state->in_fence_fd >= 0) {
1033 ret |= plane_add_prop(req, plane,
1034 WDRM_PLANE_IN_FENCE_FD,
1035 plane_state->in_fence_fd);
1036 }
1037
Marius Vladcdd6fa22019-08-29 20:42:00 +03001038 /* do note, that 'invented' zpos values are set as immutable */
1039 if (plane_state->zpos != DRM_PLANE_ZPOS_INVALID_PLANE &&
1040 plane_state->plane->zpos_min != plane_state->plane->zpos_max)
1041 ret |= plane_add_prop(req, plane,
1042 WDRM_PLANE_ZPOS,
1043 plane_state->zpos);
1044
Daniel Stone4c2fc702019-06-18 11:12:07 +01001045 if (ret != 0) {
1046 weston_log("couldn't set plane state\n");
1047 return ret;
1048 }
1049 }
1050
1051 return 0;
1052}
1053
1054/**
1055 * Helper function used only by drm_pending_state_apply, with the same
1056 * guarantees and constraints as that function.
1057 */
1058static int
1059drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
1060 enum drm_state_apply_mode mode)
1061{
1062 struct drm_backend *b = pending_state->backend;
1063 struct drm_output_state *output_state, *tmp;
1064 struct drm_plane *plane;
1065 drmModeAtomicReq *req = drmModeAtomicAlloc();
1066 uint32_t flags;
1067 int ret = 0;
1068
1069 if (!req)
1070 return -1;
1071
1072 switch (mode) {
1073 case DRM_STATE_APPLY_SYNC:
1074 flags = 0;
1075 break;
1076 case DRM_STATE_APPLY_ASYNC:
1077 flags = DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
1078 break;
1079 case DRM_STATE_TEST_ONLY:
1080 flags = DRM_MODE_ATOMIC_TEST_ONLY;
1081 break;
1082 }
1083
1084 if (b->state_invalid) {
1085 struct weston_head *head_base;
1086 struct drm_head *head;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001087 struct drm_crtc *crtc;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001088 uint32_t connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001089 int err;
1090
1091 drm_debug(b, "\t\t[atomic] previous state invalid; "
1092 "starting with fresh state\n");
1093
1094 /* If we need to reset all our state (e.g. because we've
1095 * just started, or just been VT-switched in), explicitly
1096 * disable all the CRTCs and connectors we aren't using. */
1097 wl_list_for_each(head_base,
1098 &b->compositor->head_list, compositor_link) {
1099 struct drm_property_info *info;
1100
1101 if (weston_head_is_enabled(head_base))
1102 continue;
1103
1104 head = to_drm_head(head_base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001105 connector_id = head->connector.connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001106
1107 drm_debug(b, "\t\t[atomic] disabling inactive head %s\n",
1108 head_base->name);
1109
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001110 info = &head->connector.props[WDRM_CONNECTOR_CRTC_ID];
1111 err = drmModeAtomicAddProperty(req, connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001112 info->prop_id, 0);
1113 drm_debug(b, "\t\t\t[CONN:%lu] %lu (%s) -> 0\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001114 (unsigned long) connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001115 (unsigned long) info->prop_id,
1116 info->name);
1117 if (err <= 0)
1118 ret = -1;
1119 }
1120
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001121 wl_list_for_each(crtc, &b->crtc_list, link) {
Daniel Stone4c2fc702019-06-18 11:12:07 +01001122 struct drm_property_info *info;
1123 drmModeObjectProperties *props;
1124 uint64_t active;
1125
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001126 /* Ignore CRTCs that are in use */
1127 if (crtc->output)
1128 continue;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001129
1130 /* We can't emit a disable on a CRTC that's already
1131 * off, as the kernel will refuse to generate an event
1132 * for an off->off state and fail the commit.
1133 */
1134 props = drmModeObjectGetProperties(b->drm.fd,
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001135 crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001136 DRM_MODE_OBJECT_CRTC);
1137 if (!props) {
1138 ret = -1;
1139 continue;
1140 }
1141
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001142 info = &crtc->props_crtc[WDRM_CRTC_ACTIVE];
Daniel Stone4c2fc702019-06-18 11:12:07 +01001143 active = drm_property_get_value(info, props, 0);
1144 drmModeFreeObjectProperties(props);
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001145 if (active == 0)
Daniel Stone4c2fc702019-06-18 11:12:07 +01001146 continue;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001147
1148 drm_debug(b, "\t\t[atomic] disabling unused CRTC %lu\n",
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001149 (unsigned long) crtc->crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001150
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001151 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 0);
1152 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001153 }
1154
1155 /* Disable all the planes; planes which are being used will
1156 * override this state in the output-state application. */
1157 wl_list_for_each(plane, &b->plane_list, link) {
1158 drm_debug(b, "\t\t[atomic] starting with plane %lu disabled\n",
1159 (unsigned long) plane->plane_id);
1160 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
1161 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
1162 }
1163
1164 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
1165 }
1166
1167 wl_list_for_each(output_state, &pending_state->output_list, link) {
1168 if (output_state->output->virtual)
1169 continue;
1170 if (mode == DRM_STATE_APPLY_SYNC)
1171 assert(output_state->dpms == WESTON_DPMS_OFF);
1172 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
1173 }
1174
1175 if (ret != 0) {
1176 weston_log("atomic: couldn't compile atomic state\n");
1177 goto out;
1178 }
1179
1180 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
1181 drm_debug(b, "[atomic] drmModeAtomicCommit\n");
1182
1183 /* Test commits do not take ownership of the state; return
1184 * without freeing here. */
1185 if (mode == DRM_STATE_TEST_ONLY) {
1186 drmModeAtomicFree(req);
1187 return ret;
1188 }
1189
1190 if (ret != 0) {
1191 weston_log("atomic: couldn't commit new state: %s\n",
1192 strerror(errno));
1193 goto out;
1194 }
1195
1196 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1197 link)
1198 drm_output_assign_state(output_state, mode);
1199
1200 b->state_invalid = false;
1201
1202 assert(wl_list_empty(&pending_state->output_list));
1203
1204out:
1205 drmModeAtomicFree(req);
1206 drm_pending_state_free(pending_state);
1207 return ret;
1208}
Daniel Stone4c2fc702019-06-18 11:12:07 +01001209
1210/**
1211 * Tests a pending state, to see if the kernel will accept the update as
1212 * constructed.
1213 *
1214 * Using atomic modesetting, the kernel performs the same checks as it would
1215 * on a real commit, returning success or failure without actually modifying
1216 * the running state. It does not return -EBUSY if there are pending updates
1217 * in flight, so states may be tested at any point, however this means a
1218 * state which passed testing may fail on a real commit if the timing is not
1219 * respected (e.g. committing before the previous commit has completed).
1220 *
1221 * Without atomic modesetting, we have no way to check, so we optimistically
1222 * claim it will work.
1223 *
1224 * Unlike drm_pending_state_apply() and drm_pending_state_apply_sync(), this
1225 * function does _not_ take ownership of pending_state, nor does it clear
1226 * state_invalid.
1227 */
1228int
1229drm_pending_state_test(struct drm_pending_state *pending_state)
1230{
Daniel Stone4c2fc702019-06-18 11:12:07 +01001231 struct drm_backend *b = pending_state->backend;
1232
1233 if (b->atomic_modeset)
1234 return drm_pending_state_apply_atomic(pending_state,
1235 DRM_STATE_TEST_ONLY);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001236
1237 /* We have no way to test state before application on the legacy
1238 * modesetting API, so just claim it succeeded. */
1239 return 0;
1240}
1241
1242/**
1243 * Applies all of a pending_state asynchronously: the primary entry point for
1244 * applying KMS state to a device. Updates the state for all outputs in the
1245 * pending_state, as well as disabling any unclaimed outputs.
1246 *
1247 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1248 */
1249int
1250drm_pending_state_apply(struct drm_pending_state *pending_state)
1251{
1252 struct drm_backend *b = pending_state->backend;
1253 struct drm_output_state *output_state, *tmp;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001254 struct drm_crtc *crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001255
Daniel Stone4c2fc702019-06-18 11:12:07 +01001256 if (b->atomic_modeset)
1257 return drm_pending_state_apply_atomic(pending_state,
1258 DRM_STATE_APPLY_ASYNC);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001259
1260 if (b->state_invalid) {
1261 /* If we need to reset all our state (e.g. because we've
1262 * just started, or just been VT-switched in), explicitly
1263 * disable all the CRTCs we aren't using. This also disables
1264 * all connectors on these CRTCs, so we don't need to do that
1265 * separately with the pre-atomic API. */
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001266 wl_list_for_each(crtc, &b->crtc_list, link) {
1267 if (crtc->output)
1268 continue;
1269 drmModeSetCrtc(b->drm.fd, crtc->crtc_id, 0, 0, 0,
1270 NULL, 0, NULL);
1271 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001272 }
1273
1274 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1275 link) {
1276 struct drm_output *output = output_state->output;
1277 int ret;
1278
1279 if (output->virtual) {
1280 drm_output_assign_state(output_state,
1281 DRM_STATE_APPLY_ASYNC);
1282 continue;
1283 }
1284
1285 ret = drm_output_apply_state_legacy(output_state);
1286 if (ret != 0) {
1287 weston_log("Couldn't apply state for output %s\n",
1288 output->base.name);
1289 }
1290 }
1291
1292 b->state_invalid = false;
1293
1294 assert(wl_list_empty(&pending_state->output_list));
1295
1296 drm_pending_state_free(pending_state);
1297
1298 return 0;
1299}
1300
1301/**
1302 * The synchronous version of drm_pending_state_apply. May only be used to
1303 * disable outputs. Does so synchronously: the request is guaranteed to have
1304 * completed on return, and the output will not be touched afterwards.
1305 *
1306 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1307 */
1308int
1309drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
1310{
1311 struct drm_backend *b = pending_state->backend;
1312 struct drm_output_state *output_state, *tmp;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001313 struct drm_crtc *crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001314
Daniel Stone4c2fc702019-06-18 11:12:07 +01001315 if (b->atomic_modeset)
1316 return drm_pending_state_apply_atomic(pending_state,
1317 DRM_STATE_APPLY_SYNC);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001318
1319 if (b->state_invalid) {
1320 /* If we need to reset all our state (e.g. because we've
1321 * just started, or just been VT-switched in), explicitly
1322 * disable all the CRTCs we aren't using. This also disables
1323 * all connectors on these CRTCs, so we don't need to do that
1324 * separately with the pre-atomic API. */
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001325 wl_list_for_each(crtc, &b->crtc_list, link) {
1326 if (crtc->output)
1327 continue;
1328 drmModeSetCrtc(b->drm.fd, crtc->crtc_id, 0, 0, 0,
1329 NULL, 0, NULL);
1330 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001331 }
1332
1333 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1334 link) {
1335 int ret;
1336
1337 assert(output_state->dpms == WESTON_DPMS_OFF);
1338 ret = drm_output_apply_state_legacy(output_state);
1339 if (ret != 0) {
1340 weston_log("Couldn't apply state for output %s\n",
1341 output_state->output->base.name);
1342 }
1343 }
1344
1345 b->state_invalid = false;
1346
1347 assert(wl_list_empty(&pending_state->output_list));
1348
1349 drm_pending_state_free(pending_state);
1350
1351 return 0;
1352}
1353
1354void
1355drm_output_update_msc(struct drm_output *output, unsigned int seq)
1356{
1357 uint64_t msc_hi = output->base.msc >> 32;
1358
1359 if (seq < (output->base.msc & 0xffffffff))
1360 msc_hi++;
1361
1362 output->base.msc = (msc_hi << 32) + seq;
1363}
1364
1365static void
1366page_flip_handler(int fd, unsigned int frame,
1367 unsigned int sec, unsigned int usec, void *data)
1368{
1369 struct drm_output *output = data;
1370 struct drm_backend *b = to_drm_backend(output->base.compositor);
1371 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1372 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1373 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1374
1375 drm_output_update_msc(output, frame);
1376
1377 assert(!b->atomic_modeset);
1378 assert(output->page_flip_pending);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001379 output->page_flip_pending = false;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001380
1381 drm_output_update_complete(output, flags, sec, usec);
1382}
1383
Daniel Stone4c2fc702019-06-18 11:12:07 +01001384static void
1385atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
1386 unsigned int usec, unsigned int crtc_id, void *data)
1387{
1388 struct drm_backend *b = data;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001389 struct drm_crtc *crtc;
1390 struct drm_output *output;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001391 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1392 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1393 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1394
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001395 crtc = drm_crtc_find(b, crtc_id);
1396 assert(crtc);
1397
1398 output = crtc->output;
1399
Daniel Stone4c2fc702019-06-18 11:12:07 +01001400 /* During the initial modeset, we can disable CRTCs which we don't
1401 * actually handle during normal operation; this will give us events
1402 * for unknown outputs. Ignore them. */
1403 if (!output || !output->base.enabled)
1404 return;
1405
1406 drm_output_update_msc(output, frame);
1407
1408 drm_debug(b, "[atomic][CRTC:%u] flip processing started\n", crtc_id);
1409 assert(b->atomic_modeset);
1410 assert(output->atomic_complete_pending);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001411 output->atomic_complete_pending = false;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001412
1413 drm_output_update_complete(output, flags, sec, usec);
1414 drm_debug(b, "[atomic][CRTC:%u] flip processing completed\n", crtc_id);
1415}
Daniel Stone4c2fc702019-06-18 11:12:07 +01001416
1417int
1418on_drm_input(int fd, uint32_t mask, void *data)
1419{
Daniel Stone4c2fc702019-06-18 11:12:07 +01001420 struct drm_backend *b = data;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001421 drmEventContext evctx;
1422
1423 memset(&evctx, 0, sizeof evctx);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001424 evctx.version = 3;
1425 if (b->atomic_modeset)
1426 evctx.page_flip_handler2 = atomic_flip_handler;
1427 else
Daniel Stone4c2fc702019-06-18 11:12:07 +01001428 evctx.page_flip_handler = page_flip_handler;
1429 drmHandleEvent(fd, &evctx);
1430
1431 return 1;
1432}
1433
1434int
1435init_kms_caps(struct drm_backend *b)
1436{
1437 uint64_t cap;
1438 int ret;
1439 clockid_t clk_id;
1440
1441 weston_log("using %s\n", b->drm.filename);
1442
1443 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1444 if (ret == 0 && cap == 1)
1445 clk_id = CLOCK_MONOTONIC;
1446 else
1447 clk_id = CLOCK_REALTIME;
1448
1449 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
1450 weston_log("Error: failed to set presentation clock %d.\n",
1451 clk_id);
1452 return -1;
1453 }
1454
1455 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
1456 if (ret == 0)
1457 b->cursor_width = cap;
1458 else
1459 b->cursor_width = 64;
1460
1461 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1462 if (ret == 0)
1463 b->cursor_height = cap;
1464 else
1465 b->cursor_height = 64;
1466
1467 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
1468 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
1469 b->universal_planes = (ret == 0);
1470 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001471
Daniel Stone4c2fc702019-06-18 11:12:07 +01001472 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
1473 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
1474 if (ret != 0)
1475 cap = 0;
1476 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
1477 b->atomic_modeset = ((ret == 0) && (cap == 1));
1478 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001479 weston_log("DRM: %s atomic modesetting\n",
1480 b->atomic_modeset ? "supports" : "does not support");
1481
Stefan Agner465ab2c2020-06-17 23:36:44 +02001482 if (!getenv("WESTON_DISABLE_GBM_MODIFIERS")) {
1483 ret = drmGetCap(b->drm.fd, DRM_CAP_ADDFB2_MODIFIERS, &cap);
1484 if (ret == 0)
1485 b->fb_modifiers = cap;
1486 }
1487 weston_log("DRM: %s GBM modifiers\n",
1488 b->fb_modifiers ? "supports" : "does not support");
Daniel Stone4c2fc702019-06-18 11:12:07 +01001489
1490 /*
1491 * KMS support for hardware planes cannot properly synchronize
1492 * without nuclear page flip. Without nuclear/atomic, hw plane
1493 * and cursor plane updates would either tear or cause extra
1494 * waits for vblanks which means dropping the compositor framerate
1495 * to a fraction. For cursors, it's not so bad, so they are
1496 * enabled.
1497 */
1498 if (!b->atomic_modeset || getenv("WESTON_FORCE_RENDERER"))
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001499 b->sprites_are_broken = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001500
1501 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1);
1502 b->aspect_ratio_supported = (ret == 0);
1503 weston_log("DRM: %s picture aspect ratio\n",
1504 b->aspect_ratio_supported ? "supports" : "does not support");
1505
1506 return 0;
1507}