blob: b82c2189b5df3cf00ca175c4e12157d7785353cf [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{
Scott Anderson74663092021-02-01 15:46:33 -0300436 unsigned i, j;
437 drmModePropertyBlobRes *blob = NULL;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100438 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;
Scott Anderson74663092021-02-01 15:46:33 -0300442 struct weston_drm_format *fmt;
443 int ret = 0;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100444
Stefan Agner465ab2c2020-06-17 23:36:44 +0200445 if (!use_modifiers)
446 goto fallback;
447
Daniel Stone4c2fc702019-06-18 11:12:07 +0100448 blob_id = drm_property_get_value(&plane->props[WDRM_PLANE_IN_FORMATS],
449 props,
450 0);
451 if (blob_id == 0)
452 goto fallback;
453
454 blob = drmModeGetPropertyBlob(plane->backend->drm.fd, blob_id);
455 if (!blob)
456 goto fallback;
457
458 fmt_mod_blob = blob->data;
459 blob_formats = formats_ptr(fmt_mod_blob);
460 blob_modifiers = modifiers_ptr(fmt_mod_blob);
461
Scott Anderson74663092021-02-01 15:46:33 -0300462 assert(kplane->count_formats == fmt_mod_blob->count_formats);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100463
464 for (i = 0; i < fmt_mod_blob->count_formats; i++) {
Scott Anderson74663092021-02-01 15:46:33 -0300465 fmt = weston_drm_format_array_add_format(&plane->formats,
466 blob_formats[i]);
467 if (!fmt) {
468 ret = -1;
469 goto out;
470 }
Daniel Stone4c2fc702019-06-18 11:12:07 +0100471
472 for (j = 0; j < fmt_mod_blob->count_modifiers; j++) {
473 struct drm_format_modifier *mod = &blob_modifiers[j];
474
475 if ((i < mod->offset) || (i > mod->offset + 63))
476 continue;
477 if (!(mod->formats & (1 << (i - mod->offset))))
478 continue;
479
Scott Anderson74663092021-02-01 15:46:33 -0300480 ret = weston_drm_format_add_modifier(fmt, mod->modifier);
481 if (ret < 0)
482 goto out;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100483 }
484
Scott Anderson74663092021-02-01 15:46:33 -0300485 if (fmt->modifiers.size == 0) {
Leandro Ribeiro567cc922021-04-21 11:44:53 -0300486 ret = weston_drm_format_add_modifier(fmt, DRM_FORMAT_MOD_INVALID);
Scott Anderson74663092021-02-01 15:46:33 -0300487 if (ret < 0)
488 goto out;
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900489 }
Daniel Stone4c2fc702019-06-18 11:12:07 +0100490 }
491
Scott Anderson74663092021-02-01 15:46:33 -0300492out:
Daniel Stone4c2fc702019-06-18 11:12:07 +0100493 drmModeFreePropertyBlob(blob);
Scott Anderson74663092021-02-01 15:46:33 -0300494 return ret;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100495
496fallback:
Daniel Stone4c2fc702019-06-18 11:12:07 +0100497 /* No IN_FORMATS blob available, so just use the old. */
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900498 for (i = 0; i < kplane->count_formats; i++) {
Scott Anderson74663092021-02-01 15:46:33 -0300499 fmt = weston_drm_format_array_add_format(&plane->formats,
500 kplane->formats[i]);
501 if (!fmt)
502 return -1;
Leandro Ribeiro567cc922021-04-21 11:44:53 -0300503 ret = weston_drm_format_add_modifier(fmt, DRM_FORMAT_MOD_INVALID);
Scott Anderson74663092021-02-01 15:46:33 -0300504 if (ret < 0)
505 return -1;
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900506 }
Daniel Stone4c2fc702019-06-18 11:12:07 +0100507 return 0;
508}
509
510void
511drm_output_set_gamma(struct weston_output *output_base,
512 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
513{
514 int rc;
515 struct drm_output *output = to_drm_output(output_base);
516 struct drm_backend *backend =
517 to_drm_backend(output->base.compositor);
518
519 /* check */
520 if (output_base->gamma_size != size)
521 return;
522
523 rc = drmModeCrtcSetGamma(backend->drm.fd,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300524 output->crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100525 size, r, g, b);
526 if (rc)
527 weston_log("set gamma failed: %s\n", strerror(errno));
528}
529
530/**
531 * Mark an output state as current on the output, i.e. it has been
532 * submitted to the kernel. The mode argument determines whether this
533 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
534 * or asynchronously (in which case we wait for events to complete).
535 */
536static void
537drm_output_assign_state(struct drm_output_state *state,
538 enum drm_state_apply_mode mode)
539{
540 struct drm_output *output = state->output;
541 struct drm_backend *b = to_drm_backend(output->base.compositor);
542 struct drm_plane_state *plane_state;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530543 struct drm_head *head;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100544
545 assert(!output->state_last);
546
547 if (mode == DRM_STATE_APPLY_ASYNC)
548 output->state_last = output->state_cur;
549 else
550 drm_output_state_free(output->state_cur);
551
552 wl_list_remove(&state->link);
553 wl_list_init(&state->link);
554 state->pending_state = NULL;
555
556 output->state_cur = state;
557
558 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300559 drm_debug(b, "\t[CRTC:%u] setting pending flip\n",
560 output->crtc->crtc_id);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100561 output->atomic_complete_pending = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100562 }
563
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530564 if (b->atomic_modeset &&
565 state->protection == WESTON_HDCP_DISABLE)
566 wl_list_for_each(head, &output->base.head_list, base.output_link)
567 weston_head_set_content_protection_status(&head->base,
568 WESTON_HDCP_DISABLE);
569
Daniel Stone4c2fc702019-06-18 11:12:07 +0100570 /* Replace state_cur on each affected plane with the new state, being
571 * careful to dispose of orphaned (but only orphaned) previous state.
572 * If the previous state is not orphaned (still has an output_state
573 * attached), it will be disposed of by freeing the output_state. */
574 wl_list_for_each(plane_state, &state->plane_list, link) {
575 struct drm_plane *plane = plane_state->plane;
576
577 if (plane->state_cur && !plane->state_cur->output_state)
578 drm_plane_state_free(plane->state_cur, true);
579 plane->state_cur = plane_state;
580
581 if (mode != DRM_STATE_APPLY_ASYNC) {
582 plane_state->complete = true;
583 continue;
584 }
585
586 if (b->atomic_modeset)
587 continue;
588
589 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY);
590 if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100591 output->page_flip_pending = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100592 }
593}
594
595static void
596drm_output_set_cursor(struct drm_output_state *output_state)
597{
598 struct drm_output *output = output_state->output;
599 struct drm_backend *b = to_drm_backend(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300600 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100601 struct drm_plane *plane = output->cursor_plane;
602 struct drm_plane_state *state;
Stefan Agner974390a2019-07-08 00:42:05 +0200603 uint32_t handle;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100604
605 if (!plane)
606 return;
607
608 state = drm_output_state_get_existing_plane(output_state, plane);
609 if (!state)
610 return;
611
612 if (!state->fb) {
613 pixman_region32_fini(&plane->base.damage);
614 pixman_region32_init(&plane->base.damage);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300615 drmModeSetCursor(b->drm.fd, crtc->crtc_id, 0, 0, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100616 return;
617 }
618
619 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
620 assert(!plane->state_cur->output || plane->state_cur->output == output);
621
Stefan Agner974390a2019-07-08 00:42:05 +0200622 handle = output->gbm_cursor_handle[output->current_cursor];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100623 if (plane->state_cur->fb != state->fb) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300624 if (drmModeSetCursor(b->drm.fd, crtc->crtc_id, handle,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100625 b->cursor_width, b->cursor_height)) {
626 weston_log("failed to set cursor: %s\n",
627 strerror(errno));
628 goto err;
629 }
630 }
631
632 pixman_region32_fini(&plane->base.damage);
633 pixman_region32_init(&plane->base.damage);
634
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300635 if (drmModeMoveCursor(b->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100636 state->dest_x, state->dest_y)) {
637 weston_log("failed to move cursor: %s\n", strerror(errno));
638 goto err;
639 }
640
641 return;
642
643err:
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100644 b->cursors_are_broken = true;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300645 drmModeSetCursor(b->drm.fd, crtc->crtc_id, 0, 0, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100646}
647
648static int
649drm_output_apply_state_legacy(struct drm_output_state *state)
650{
651 struct drm_output *output = state->output;
652 struct drm_backend *backend = to_drm_backend(output->base.compositor);
653 struct drm_plane *scanout_plane = output->scanout_plane;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300654 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100655 struct drm_property_info *dpms_prop;
656 struct drm_plane_state *scanout_state;
657 struct drm_mode *mode;
658 struct drm_head *head;
659 const struct pixel_format_info *pinfo = NULL;
660 uint32_t connectors[MAX_CLONED_CONNECTORS];
661 int n_conn = 0;
662 struct timespec now;
663 int ret = 0;
664
665 wl_list_for_each(head, &output->base.head_list, base.output_link) {
666 assert(n_conn < MAX_CLONED_CONNECTORS);
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300667 connectors[n_conn++] = head->connector.connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100668 }
669
670 /* If disable_planes is set then assign_planes() wasn't
671 * called for this render, so we could still have a stale
672 * cursor plane set up.
673 */
674 if (output->base.disable_planes) {
675 output->cursor_view = NULL;
676 if (output->cursor_plane) {
677 output->cursor_plane->base.x = INT32_MIN;
678 output->cursor_plane->base.y = INT32_MIN;
679 }
680 }
681
682 if (state->dpms != WESTON_DPMS_ON) {
683 if (output->cursor_plane) {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300684 ret = drmModeSetCursor(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100685 0, 0, 0);
686 if (ret)
687 weston_log("drmModeSetCursor failed disable: %s\n",
688 strerror(errno));
689 }
690
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300691 ret = drmModeSetCrtc(backend->drm.fd, crtc->crtc_id, 0, 0, 0,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100692 NULL, 0, NULL);
693 if (ret)
694 weston_log("drmModeSetCrtc failed disabling: %s\n",
695 strerror(errno));
696
697 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
698 weston_compositor_read_presentation_clock(output->base.compositor, &now);
699 drm_output_update_complete(output,
700 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
701 now.tv_sec, now.tv_nsec / 1000);
702
703 return 0;
704 }
705
706 scanout_state =
707 drm_output_state_get_existing_plane(state, scanout_plane);
708
709 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
710 * legacy PageFlip API doesn't allow us to do clipping either. */
711 assert(scanout_state->src_x == 0);
712 assert(scanout_state->src_y == 0);
713 assert(scanout_state->src_w ==
714 (unsigned) (output->base.current_mode->width << 16));
715 assert(scanout_state->src_h ==
716 (unsigned) (output->base.current_mode->height << 16));
717 assert(scanout_state->dest_x == 0);
718 assert(scanout_state->dest_y == 0);
719 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
720 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
721 /* The legacy SetCrtc API doesn't support fences */
722 assert(scanout_state->in_fence_fd == -1);
723
724 mode = to_drm_mode(output->base.current_mode);
725 if (backend->state_invalid ||
726 !scanout_plane->state_cur->fb ||
727 scanout_plane->state_cur->fb->strides[0] !=
728 scanout_state->fb->strides[0]) {
729
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300730 ret = drmModeSetCrtc(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100731 scanout_state->fb->fb_id,
732 0, 0,
733 connectors, n_conn,
734 &mode->mode_info);
735 if (ret) {
736 weston_log("set mode failed: %s\n", strerror(errno));
737 goto err;
738 }
739 }
740
741 pinfo = scanout_state->fb->format;
742 drm_debug(backend, "\t[CRTC:%u, PLANE:%u] FORMAT: %s\n",
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300743 crtc->crtc_id, scanout_state->plane->plane_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100744 pinfo ? pinfo->drm_format_name : "UNKNOWN");
745
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300746 if (drmModePageFlip(backend->drm.fd, crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100747 scanout_state->fb->fb_id,
748 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
749 weston_log("queueing pageflip failed: %s\n", strerror(errno));
750 goto err;
751 }
752
753 assert(!output->page_flip_pending);
754
755 if (output->pageflip_timer)
756 wl_event_source_timer_update(output->pageflip_timer,
757 backend->pageflip_timeout);
758
759 drm_output_set_cursor(state);
760
761 if (state->dpms != output->state_cur->dpms) {
762 wl_list_for_each(head, &output->base.head_list, base.output_link) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300763 dpms_prop = &head->connector.props[WDRM_CONNECTOR_DPMS];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100764 if (dpms_prop->prop_id == 0)
765 continue;
766
767 ret = drmModeConnectorSetProperty(backend->drm.fd,
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300768 head->connector.connector_id,
769 dpms_prop->prop_id,
770 state->dpms);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100771 if (ret) {
772 weston_log("DRM: DPMS: failed property set for %s\n",
773 head->base.name);
774 }
775 }
776 }
777
778 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
779
780 return 0;
781
782err:
783 output->cursor_view = NULL;
784 drm_output_state_free(state);
785 return -1;
786}
787
Daniel Stone4c2fc702019-06-18 11:12:07 +0100788static int
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300789crtc_add_prop(drmModeAtomicReq *req, struct drm_crtc *crtc,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100790 enum wdrm_crtc_property prop, uint64_t val)
791{
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300792 struct drm_property_info *info = &crtc->props_crtc[prop];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100793 int ret;
794
795 if (info->prop_id == 0)
796 return -1;
797
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300798 ret = drmModeAtomicAddProperty(req, crtc->crtc_id, info->prop_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100799 val);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300800 drm_debug(crtc->backend, "\t\t\t[CRTC:%lu] %lu (%s) -> %llu (0x%llx)\n",
801 (unsigned long) crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100802 (unsigned long) info->prop_id, info->name,
803 (unsigned long long) val, (unsigned long long) val);
804 return (ret <= 0) ? -1 : 0;
805}
806
807static int
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300808connector_add_prop(drmModeAtomicReq *req, struct drm_connector *connector,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100809 enum wdrm_connector_property prop, uint64_t val)
810{
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300811 struct drm_property_info *info = &connector->props[prop];
812 uint32_t connector_id = connector->connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100813 int ret;
814
815 if (info->prop_id == 0)
816 return -1;
817
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300818 ret = drmModeAtomicAddProperty(req, connector_id, info->prop_id, val);
819 drm_debug(connector->backend, "\t\t\t[CONN:%lu] %lu (%s) -> %llu (0x%llx)\n",
820 (unsigned long) connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100821 (unsigned long) info->prop_id, info->name,
822 (unsigned long long) val, (unsigned long long) val);
823 return (ret <= 0) ? -1 : 0;
824}
825
826static int
827plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
828 enum wdrm_plane_property prop, uint64_t val)
829{
830 struct drm_property_info *info = &plane->props[prop];
831 int ret;
832
833 if (info->prop_id == 0)
834 return -1;
835
836 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
837 val);
838 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] %lu (%s) -> %llu (0x%llx)\n",
839 (unsigned long) plane->plane_id,
840 (unsigned long) info->prop_id, info->name,
841 (unsigned long long) val, (unsigned long long) val);
842 return (ret <= 0) ? -1 : 0;
843}
844
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530845static bool
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300846drm_connector_has_prop(struct drm_connector *connector,
847 enum wdrm_connector_property prop)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530848{
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300849 if (connector->props[prop].prop_id != 0)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530850 return true;
851
852 return false;
853}
854
855/*
856 * This function converts the protection requests from weston_hdcp_protection
857 * corresponding drm values. These values can be set in "Content Protection"
858 * & "HDCP Content Type" connector properties.
859 */
860static void
861get_drm_protection_from_weston(enum weston_hdcp_protection weston_protection,
862 enum wdrm_content_protection_state *drm_protection,
863 enum wdrm_hdcp_content_type *drm_cp_type)
864{
865
866 switch (weston_protection) {
867 case WESTON_HDCP_DISABLE:
868 *drm_protection = WDRM_CONTENT_PROTECTION_UNDESIRED;
869 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
870 break;
871 case WESTON_HDCP_ENABLE_TYPE_0:
872 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
873 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
874 break;
875 case WESTON_HDCP_ENABLE_TYPE_1:
876 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
877 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE1;
878 break;
879 default:
880 assert(0 && "bad weston_hdcp_protection");
881 }
882}
883
884static void
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300885drm_connector_set_hdcp_property(struct drm_connector *connector,
886 enum weston_hdcp_protection protection,
887 drmModeAtomicReq *req)
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530888{
889 int ret;
890 enum wdrm_content_protection_state drm_protection;
891 enum wdrm_hdcp_content_type drm_cp_type;
892 struct drm_property_enum_info *enum_info;
893 uint64_t prop_val;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300894 struct drm_property_info *props = connector->props;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530895
896 get_drm_protection_from_weston(protection, &drm_protection,
897 &drm_cp_type);
898
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300899 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_CONTENT_PROTECTION))
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530900 return;
901
902 /*
903 * Content-type property is not exposed for platforms not supporting
904 * HDCP2.2, therefore, type-1 cannot be supported. The type-0 content
905 * still can be supported if the content-protection property is exposed.
906 */
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300907 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_HDCP_CONTENT_TYPE) &&
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530908 drm_cp_type != WDRM_HDCP_CONTENT_TYPE0)
909 return;
910
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300911 enum_info = props[WDRM_CONNECTOR_CONTENT_PROTECTION].enum_values;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530912 prop_val = enum_info[drm_protection].value;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300913 ret = connector_add_prop(req, connector,
914 WDRM_CONNECTOR_CONTENT_PROTECTION, prop_val);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530915 assert(ret == 0);
916
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300917 if (!drm_connector_has_prop(connector, WDRM_CONNECTOR_HDCP_CONTENT_TYPE))
Ankit Nautiyalfc2c1802019-08-30 19:40:46 +0530918 return;
919
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300920 enum_info = props[WDRM_CONNECTOR_HDCP_CONTENT_TYPE].enum_values;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530921 prop_val = enum_info[drm_cp_type].value;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300922 ret = connector_add_prop(req, connector,
923 WDRM_CONNECTOR_HDCP_CONTENT_TYPE, prop_val);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530924 assert(ret == 0);
925}
926
Daniel Stone4c2fc702019-06-18 11:12:07 +0100927static int
928drm_output_apply_state_atomic(struct drm_output_state *state,
929 drmModeAtomicReq *req,
930 uint32_t *flags)
931{
932 struct drm_output *output = state->output;
933 struct drm_backend *b = to_drm_backend(output->base.compositor);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300934 struct drm_crtc *crtc = output->crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100935 struct drm_plane_state *plane_state;
936 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
937 struct drm_head *head;
938 int ret = 0;
939
940 drm_debug(b, "\t\t[atomic] %s output %lu (%s) state\n",
941 (*flags & DRM_MODE_ATOMIC_TEST_ONLY) ? "testing" : "applying",
942 (unsigned long) output->base.id, output->base.name);
943
944 if (state->dpms != output->state_cur->dpms) {
945 drm_debug(b, "\t\t\t[atomic] DPMS state differs, modeset OK\n");
946 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
947 }
948
949 if (state->dpms == WESTON_DPMS_ON) {
950 ret = drm_mode_ensure_blob(b, current_mode);
951 if (ret != 0)
952 return ret;
953
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300954 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID,
Daniel Stone4c2fc702019-06-18 11:12:07 +0100955 current_mode->blob_id);
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300956 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 1);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100957
958 /* No need for the DPMS property, since it is implicit in
959 * routing and CRTC activity. */
960 wl_list_for_each(head, &output->base.head_list, base.output_link) {
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300961 ret |= connector_add_prop(req, &head->connector,
962 WDRM_CONNECTOR_CRTC_ID,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300963 crtc->crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100964 }
965 } else {
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300966 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID, 0);
967 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100968
969 /* No need for the DPMS property, since it is implicit in
970 * routing and CRTC activity. */
971 wl_list_for_each(head, &output->base.head_list, base.output_link)
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300972 ret |= connector_add_prop(req, &head->connector,
973 WDRM_CONNECTOR_CRTC_ID, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100974 }
975
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530976 wl_list_for_each(head, &output->base.head_list, base.output_link)
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300977 drm_connector_set_hdcp_property(&head->connector,
978 state->protection, req);
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530979
Daniel Stone4c2fc702019-06-18 11:12:07 +0100980 if (ret != 0) {
981 weston_log("couldn't set atomic CRTC/connector state\n");
982 return ret;
983 }
984
985 wl_list_for_each(plane_state, &state->plane_list, link) {
986 struct drm_plane *plane = plane_state->plane;
987 const struct pixel_format_info *pinfo = NULL;
988
989 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
990 plane_state->fb ? plane_state->fb->fb_id : 0);
991 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300992 plane_state->fb ? crtc->crtc_id : 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100993 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
994 plane_state->src_x);
995 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
996 plane_state->src_y);
997 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
998 plane_state->src_w);
999 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
1000 plane_state->src_h);
1001 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
1002 plane_state->dest_x);
1003 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
1004 plane_state->dest_y);
1005 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
1006 plane_state->dest_w);
1007 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
1008 plane_state->dest_h);
Scott Anderson15c603c2020-06-02 17:39:43 +12001009 if (plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS].prop_id != 0)
1010 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_DAMAGE_CLIPS,
1011 plane_state->damage_blob_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001012
1013 if (plane_state->fb && plane_state->fb->format)
1014 pinfo = plane_state->fb->format;
1015
1016 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] FORMAT: %s\n",
1017 (unsigned long) plane->plane_id,
1018 pinfo ? pinfo->drm_format_name : "UNKNOWN");
1019
1020 if (plane_state->in_fence_fd >= 0) {
1021 ret |= plane_add_prop(req, plane,
1022 WDRM_PLANE_IN_FENCE_FD,
1023 plane_state->in_fence_fd);
1024 }
1025
Marius Vladcdd6fa22019-08-29 20:42:00 +03001026 /* do note, that 'invented' zpos values are set as immutable */
1027 if (plane_state->zpos != DRM_PLANE_ZPOS_INVALID_PLANE &&
1028 plane_state->plane->zpos_min != plane_state->plane->zpos_max)
1029 ret |= plane_add_prop(req, plane,
1030 WDRM_PLANE_ZPOS,
1031 plane_state->zpos);
1032
Daniel Stone4c2fc702019-06-18 11:12:07 +01001033 if (ret != 0) {
1034 weston_log("couldn't set plane state\n");
1035 return ret;
1036 }
1037 }
1038
1039 return 0;
1040}
1041
1042/**
1043 * Helper function used only by drm_pending_state_apply, with the same
1044 * guarantees and constraints as that function.
1045 */
1046static int
1047drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
1048 enum drm_state_apply_mode mode)
1049{
1050 struct drm_backend *b = pending_state->backend;
1051 struct drm_output_state *output_state, *tmp;
1052 struct drm_plane *plane;
1053 drmModeAtomicReq *req = drmModeAtomicAlloc();
1054 uint32_t flags;
1055 int ret = 0;
1056
1057 if (!req)
1058 return -1;
1059
1060 switch (mode) {
1061 case DRM_STATE_APPLY_SYNC:
1062 flags = 0;
1063 break;
1064 case DRM_STATE_APPLY_ASYNC:
1065 flags = DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
1066 break;
1067 case DRM_STATE_TEST_ONLY:
1068 flags = DRM_MODE_ATOMIC_TEST_ONLY;
1069 break;
1070 }
1071
1072 if (b->state_invalid) {
1073 struct weston_head *head_base;
1074 struct drm_head *head;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001075 struct drm_crtc *crtc;
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001076 uint32_t connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001077 int err;
1078
1079 drm_debug(b, "\t\t[atomic] previous state invalid; "
1080 "starting with fresh state\n");
1081
1082 /* If we need to reset all our state (e.g. because we've
1083 * just started, or just been VT-switched in), explicitly
1084 * disable all the CRTCs and connectors we aren't using. */
1085 wl_list_for_each(head_base,
1086 &b->compositor->head_list, compositor_link) {
1087 struct drm_property_info *info;
1088
1089 if (weston_head_is_enabled(head_base))
1090 continue;
1091
1092 head = to_drm_head(head_base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001093 connector_id = head->connector.connector_id;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001094
1095 drm_debug(b, "\t\t[atomic] disabling inactive head %s\n",
1096 head_base->name);
1097
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001098 info = &head->connector.props[WDRM_CONNECTOR_CRTC_ID];
1099 err = drmModeAtomicAddProperty(req, connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001100 info->prop_id, 0);
1101 drm_debug(b, "\t\t\t[CONN:%lu] %lu (%s) -> 0\n",
Leandro Ribeiroe6369902020-06-17 11:09:47 -03001102 (unsigned long) connector_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001103 (unsigned long) info->prop_id,
1104 info->name);
1105 if (err <= 0)
1106 ret = -1;
1107 }
1108
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001109 wl_list_for_each(crtc, &b->crtc_list, link) {
Daniel Stone4c2fc702019-06-18 11:12:07 +01001110 struct drm_property_info *info;
1111 drmModeObjectProperties *props;
1112 uint64_t active;
1113
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001114 /* Ignore CRTCs that are in use */
1115 if (crtc->output)
1116 continue;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001117
1118 /* We can't emit a disable on a CRTC that's already
1119 * off, as the kernel will refuse to generate an event
1120 * for an off->off state and fail the commit.
1121 */
1122 props = drmModeObjectGetProperties(b->drm.fd,
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001123 crtc->crtc_id,
Daniel Stone4c2fc702019-06-18 11:12:07 +01001124 DRM_MODE_OBJECT_CRTC);
1125 if (!props) {
1126 ret = -1;
1127 continue;
1128 }
1129
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001130 info = &crtc->props_crtc[WDRM_CRTC_ACTIVE];
Daniel Stone4c2fc702019-06-18 11:12:07 +01001131 active = drm_property_get_value(info, props, 0);
1132 drmModeFreeObjectProperties(props);
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001133 if (active == 0)
Daniel Stone4c2fc702019-06-18 11:12:07 +01001134 continue;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001135
1136 drm_debug(b, "\t\t[atomic] disabling unused CRTC %lu\n",
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001137 (unsigned long) crtc->crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001138
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001139 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_ACTIVE, 0);
1140 ret |= crtc_add_prop(req, crtc, WDRM_CRTC_MODE_ID, 0);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001141 }
1142
1143 /* Disable all the planes; planes which are being used will
1144 * override this state in the output-state application. */
1145 wl_list_for_each(plane, &b->plane_list, link) {
1146 drm_debug(b, "\t\t[atomic] starting with plane %lu disabled\n",
1147 (unsigned long) plane->plane_id);
1148 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
1149 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
1150 }
1151
1152 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
1153 }
1154
1155 wl_list_for_each(output_state, &pending_state->output_list, link) {
1156 if (output_state->output->virtual)
1157 continue;
1158 if (mode == DRM_STATE_APPLY_SYNC)
1159 assert(output_state->dpms == WESTON_DPMS_OFF);
1160 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
1161 }
1162
1163 if (ret != 0) {
1164 weston_log("atomic: couldn't compile atomic state\n");
1165 goto out;
1166 }
1167
1168 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
1169 drm_debug(b, "[atomic] drmModeAtomicCommit\n");
1170
1171 /* Test commits do not take ownership of the state; return
1172 * without freeing here. */
1173 if (mode == DRM_STATE_TEST_ONLY) {
1174 drmModeAtomicFree(req);
1175 return ret;
1176 }
1177
1178 if (ret != 0) {
1179 weston_log("atomic: couldn't commit new state: %s\n",
1180 strerror(errno));
1181 goto out;
1182 }
1183
1184 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1185 link)
1186 drm_output_assign_state(output_state, mode);
1187
1188 b->state_invalid = false;
1189
1190 assert(wl_list_empty(&pending_state->output_list));
1191
1192out:
1193 drmModeAtomicFree(req);
1194 drm_pending_state_free(pending_state);
1195 return ret;
1196}
Daniel Stone4c2fc702019-06-18 11:12:07 +01001197
1198/**
1199 * Tests a pending state, to see if the kernel will accept the update as
1200 * constructed.
1201 *
1202 * Using atomic modesetting, the kernel performs the same checks as it would
1203 * on a real commit, returning success or failure without actually modifying
1204 * the running state. It does not return -EBUSY if there are pending updates
1205 * in flight, so states may be tested at any point, however this means a
1206 * state which passed testing may fail on a real commit if the timing is not
1207 * respected (e.g. committing before the previous commit has completed).
1208 *
1209 * Without atomic modesetting, we have no way to check, so we optimistically
1210 * claim it will work.
1211 *
1212 * Unlike drm_pending_state_apply() and drm_pending_state_apply_sync(), this
1213 * function does _not_ take ownership of pending_state, nor does it clear
1214 * state_invalid.
1215 */
1216int
1217drm_pending_state_test(struct drm_pending_state *pending_state)
1218{
Daniel Stone4c2fc702019-06-18 11:12:07 +01001219 struct drm_backend *b = pending_state->backend;
1220
1221 if (b->atomic_modeset)
1222 return drm_pending_state_apply_atomic(pending_state,
1223 DRM_STATE_TEST_ONLY);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001224
1225 /* We have no way to test state before application on the legacy
1226 * modesetting API, so just claim it succeeded. */
1227 return 0;
1228}
1229
1230/**
1231 * Applies all of a pending_state asynchronously: the primary entry point for
1232 * applying KMS state to a device. Updates the state for all outputs in the
1233 * pending_state, as well as disabling any unclaimed outputs.
1234 *
1235 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1236 */
1237int
1238drm_pending_state_apply(struct drm_pending_state *pending_state)
1239{
1240 struct drm_backend *b = pending_state->backend;
1241 struct drm_output_state *output_state, *tmp;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001242 struct drm_crtc *crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001243
Daniel Stone4c2fc702019-06-18 11:12:07 +01001244 if (b->atomic_modeset)
1245 return drm_pending_state_apply_atomic(pending_state,
1246 DRM_STATE_APPLY_ASYNC);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001247
1248 if (b->state_invalid) {
1249 /* If we need to reset all our state (e.g. because we've
1250 * just started, or just been VT-switched in), explicitly
1251 * disable all the CRTCs we aren't using. This also disables
1252 * all connectors on these CRTCs, so we don't need to do that
1253 * separately with the pre-atomic API. */
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001254 wl_list_for_each(crtc, &b->crtc_list, link) {
1255 if (crtc->output)
1256 continue;
1257 drmModeSetCrtc(b->drm.fd, crtc->crtc_id, 0, 0, 0,
1258 NULL, 0, NULL);
1259 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001260 }
1261
1262 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1263 link) {
1264 struct drm_output *output = output_state->output;
1265 int ret;
1266
1267 if (output->virtual) {
1268 drm_output_assign_state(output_state,
1269 DRM_STATE_APPLY_ASYNC);
1270 continue;
1271 }
1272
1273 ret = drm_output_apply_state_legacy(output_state);
1274 if (ret != 0) {
1275 weston_log("Couldn't apply state for output %s\n",
1276 output->base.name);
1277 }
1278 }
1279
1280 b->state_invalid = false;
1281
1282 assert(wl_list_empty(&pending_state->output_list));
1283
1284 drm_pending_state_free(pending_state);
1285
1286 return 0;
1287}
1288
1289/**
1290 * The synchronous version of drm_pending_state_apply. May only be used to
1291 * disable outputs. Does so synchronously: the request is guaranteed to have
1292 * completed on return, and the output will not be touched afterwards.
1293 *
1294 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1295 */
1296int
1297drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
1298{
1299 struct drm_backend *b = pending_state->backend;
1300 struct drm_output_state *output_state, *tmp;
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001301 struct drm_crtc *crtc;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001302
Daniel Stone4c2fc702019-06-18 11:12:07 +01001303 if (b->atomic_modeset)
1304 return drm_pending_state_apply_atomic(pending_state,
1305 DRM_STATE_APPLY_SYNC);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001306
1307 if (b->state_invalid) {
1308 /* If we need to reset all our state (e.g. because we've
1309 * just started, or just been VT-switched in), explicitly
1310 * disable all the CRTCs we aren't using. This also disables
1311 * all connectors on these CRTCs, so we don't need to do that
1312 * separately with the pre-atomic API. */
Leandro Ribeiro744c0cb2020-08-13 16:15:58 -03001313 wl_list_for_each(crtc, &b->crtc_list, link) {
1314 if (crtc->output)
1315 continue;
1316 drmModeSetCrtc(b->drm.fd, crtc->crtc_id, 0, 0, 0,
1317 NULL, 0, NULL);
1318 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001319 }
1320
1321 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1322 link) {
1323 int ret;
1324
1325 assert(output_state->dpms == WESTON_DPMS_OFF);
1326 ret = drm_output_apply_state_legacy(output_state);
1327 if (ret != 0) {
1328 weston_log("Couldn't apply state for output %s\n",
1329 output_state->output->base.name);
1330 }
1331 }
1332
1333 b->state_invalid = false;
1334
1335 assert(wl_list_empty(&pending_state->output_list));
1336
1337 drm_pending_state_free(pending_state);
1338
1339 return 0;
1340}
1341
1342void
1343drm_output_update_msc(struct drm_output *output, unsigned int seq)
1344{
1345 uint64_t msc_hi = output->base.msc >> 32;
1346
1347 if (seq < (output->base.msc & 0xffffffff))
1348 msc_hi++;
1349
1350 output->base.msc = (msc_hi << 32) + seq;
1351}
1352
1353static void
1354page_flip_handler(int fd, unsigned int frame,
1355 unsigned int sec, unsigned int usec, void *data)
1356{
1357 struct drm_output *output = data;
1358 struct drm_backend *b = to_drm_backend(output->base.compositor);
1359 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1360 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1361 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1362
1363 drm_output_update_msc(output, frame);
1364
1365 assert(!b->atomic_modeset);
1366 assert(output->page_flip_pending);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001367 output->page_flip_pending = false;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001368
1369 drm_output_update_complete(output, flags, sec, usec);
1370}
1371
Daniel Stone4c2fc702019-06-18 11:12:07 +01001372static void
1373atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
1374 unsigned int usec, unsigned int crtc_id, void *data)
1375{
1376 struct drm_backend *b = data;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001377 struct drm_crtc *crtc;
1378 struct drm_output *output;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001379 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1380 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1381 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1382
Leandro Ribeirob00d1a22020-08-13 14:12:28 -03001383 crtc = drm_crtc_find(b, crtc_id);
1384 assert(crtc);
1385
1386 output = crtc->output;
1387
Daniel Stone4c2fc702019-06-18 11:12:07 +01001388 /* During the initial modeset, we can disable CRTCs which we don't
1389 * actually handle during normal operation; this will give us events
1390 * for unknown outputs. Ignore them. */
1391 if (!output || !output->base.enabled)
1392 return;
1393
1394 drm_output_update_msc(output, frame);
1395
1396 drm_debug(b, "[atomic][CRTC:%u] flip processing started\n", crtc_id);
1397 assert(b->atomic_modeset);
1398 assert(output->atomic_complete_pending);
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001399 output->atomic_complete_pending = false;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001400
1401 drm_output_update_complete(output, flags, sec, usec);
1402 drm_debug(b, "[atomic][CRTC:%u] flip processing completed\n", crtc_id);
1403}
Daniel Stone4c2fc702019-06-18 11:12:07 +01001404
1405int
1406on_drm_input(int fd, uint32_t mask, void *data)
1407{
Daniel Stone4c2fc702019-06-18 11:12:07 +01001408 struct drm_backend *b = data;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001409 drmEventContext evctx;
1410
1411 memset(&evctx, 0, sizeof evctx);
Daniel Stone4c2fc702019-06-18 11:12:07 +01001412 evctx.version = 3;
1413 if (b->atomic_modeset)
1414 evctx.page_flip_handler2 = atomic_flip_handler;
1415 else
Daniel Stone4c2fc702019-06-18 11:12:07 +01001416 evctx.page_flip_handler = page_flip_handler;
1417 drmHandleEvent(fd, &evctx);
1418
1419 return 1;
1420}
1421
1422int
1423init_kms_caps(struct drm_backend *b)
1424{
1425 uint64_t cap;
1426 int ret;
1427 clockid_t clk_id;
1428
1429 weston_log("using %s\n", b->drm.filename);
1430
1431 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1432 if (ret == 0 && cap == 1)
1433 clk_id = CLOCK_MONOTONIC;
1434 else
1435 clk_id = CLOCK_REALTIME;
1436
1437 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
1438 weston_log("Error: failed to set presentation clock %d.\n",
1439 clk_id);
1440 return -1;
1441 }
1442
1443 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
1444 if (ret == 0)
1445 b->cursor_width = cap;
1446 else
1447 b->cursor_width = 64;
1448
1449 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1450 if (ret == 0)
1451 b->cursor_height = cap;
1452 else
1453 b->cursor_height = 64;
1454
1455 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
1456 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
1457 b->universal_planes = (ret == 0);
1458 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001459
Daniel Stone4c2fc702019-06-18 11:12:07 +01001460 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
1461 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
1462 if (ret != 0)
1463 cap = 0;
1464 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
1465 b->atomic_modeset = ((ret == 0) && (cap == 1));
1466 }
Daniel Stone4c2fc702019-06-18 11:12:07 +01001467 weston_log("DRM: %s atomic modesetting\n",
1468 b->atomic_modeset ? "supports" : "does not support");
1469
Stefan Agner465ab2c2020-06-17 23:36:44 +02001470 if (!getenv("WESTON_DISABLE_GBM_MODIFIERS")) {
1471 ret = drmGetCap(b->drm.fd, DRM_CAP_ADDFB2_MODIFIERS, &cap);
1472 if (ret == 0)
1473 b->fb_modifiers = cap;
1474 }
1475 weston_log("DRM: %s GBM modifiers\n",
1476 b->fb_modifiers ? "supports" : "does not support");
Daniel Stone4c2fc702019-06-18 11:12:07 +01001477
Leandro Ribeiro96bef052020-09-09 15:23:49 -03001478 drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
1479
Daniel Stone4c2fc702019-06-18 11:12:07 +01001480 /*
1481 * KMS support for hardware planes cannot properly synchronize
1482 * without nuclear page flip. Without nuclear/atomic, hw plane
1483 * and cursor plane updates would either tear or cause extra
1484 * waits for vblanks which means dropping the compositor framerate
1485 * to a fraction. For cursors, it's not so bad, so they are
1486 * enabled.
1487 */
1488 if (!b->atomic_modeset || getenv("WESTON_FORCE_RENDERER"))
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +01001489 b->sprites_are_broken = true;
Daniel Stone4c2fc702019-06-18 11:12:07 +01001490
1491 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1);
1492 b->aspect_ratio_supported = (ret == 0);
1493 weston_log("DRM: %s picture aspect ratio\n",
1494 b->aspect_ratio_supported ? "supports" : "does not support");
1495
1496 return 0;
1497}