blob: 5468afb36a095f12e5e9f7325b6f805bc5941af1 [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
Daniel Stone4c2fc702019-06-18 11:12:07 +0100119const struct drm_property_info connector_props[] = {
120 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
121 [WDRM_CONNECTOR_DPMS] = {
122 .name = "DPMS",
123 .enum_values = dpms_state_enums,
124 .num_enum_values = WDRM_DPMS_STATE__COUNT,
125 },
126 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
127 [WDRM_CONNECTOR_NON_DESKTOP] = { .name = "non-desktop", },
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530128 [WDRM_CONNECTOR_CONTENT_PROTECTION] = {
129 .name = "Content Protection",
130 .enum_values = content_protection_enums,
131 .num_enum_values = WDRM_CONTENT_PROTECTION__COUNT,
132 },
133 [WDRM_CONNECTOR_HDCP_CONTENT_TYPE] = {
134 .name = "HDCP Content Type",
135 .enum_values = hdcp_content_type_enums,
136 .num_enum_values = WDRM_HDCP_CONTENT_TYPE__COUNT,
137 },
Daniel Stone4c2fc702019-06-18 11:12:07 +0100138};
139
140const struct drm_property_info crtc_props[] = {
141 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
142 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
143};
144
145
146/**
147 * Mode for drm_pending_state_apply and co.
148 */
149enum drm_state_apply_mode {
150 DRM_STATE_APPLY_SYNC, /**< state fully processed */
151 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
152 DRM_STATE_TEST_ONLY, /**< test if the state can be applied */
153};
154
155/**
156 * Get the current value of a KMS property
157 *
158 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
159 * for the target property, return the current value of that property,
160 * with an optional default. If the property is a KMS enum type, the return
161 * value will be translated into the appropriate internal enum.
162 *
163 * If the property is not present, the default value will be returned.
164 *
165 * @param info Internal structure for property to look up
166 * @param props Raw KMS properties for the target object
167 * @param def Value to return if property is not found
168 */
169uint64_t
170drm_property_get_value(struct drm_property_info *info,
171 const drmModeObjectProperties *props,
172 uint64_t def)
173{
174 unsigned int i;
175
176 if (info->prop_id == 0)
177 return def;
178
179 for (i = 0; i < props->count_props; i++) {
180 unsigned int j;
181
182 if (props->props[i] != info->prop_id)
183 continue;
184
185 /* Simple (non-enum) types can return the value directly */
186 if (info->num_enum_values == 0)
187 return props->prop_values[i];
188
189 /* Map from raw value to enum value */
190 for (j = 0; j < info->num_enum_values; j++) {
191 if (!info->enum_values[j].valid)
192 continue;
193 if (info->enum_values[j].value != props->prop_values[i])
194 continue;
195
196 return j;
197 }
198
199 /* We don't have a mapping for this enum; return default. */
200 break;
201 }
202
203 return def;
204}
205
206/**
Marius Vlad1accffe2019-11-01 12:00:09 +0200207 * Get the current range values of a KMS property
208 *
209 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
210 * for the target property, return the current range values of that property,
211 *
212 * If the property is not present, or there's no it is not a prop range then
213 * NULL will be returned.
214 *
215 * @param info Internal structure for property to look up
216 * @param props Raw KMS properties for the target object
217 */
218uint64_t *
219drm_property_get_range_values(struct drm_property_info *info,
220 const drmModeObjectProperties *props)
221{
222 unsigned int i;
223
224 if (info->prop_id == 0)
225 return NULL;
226
227 for (i = 0; i < props->count_props; i++) {
228
229 if (props->props[i] != info->prop_id)
230 continue;
231
232 if (!(info->flags & DRM_MODE_PROP_RANGE) &&
233 !(info->flags & DRM_MODE_PROP_SIGNED_RANGE))
234 continue;
235
236 return info->range_values;
237 }
238
239 return NULL;
240}
241
242/**
Daniel Stone4c2fc702019-06-18 11:12:07 +0100243 * Cache DRM property values
244 *
245 * Update a per-object array of drm_property_info structures, given the
246 * DRM properties of the object.
247 *
248 * Call this every time an object newly appears (note that only connectors
249 * can be hotplugged), the first time it is seen, or when its status changes
250 * in a way which invalidates the potential property values (currently, the
251 * only case for this is connector hotplug).
252 *
253 * This updates the property IDs and enum values within the drm_property_info
254 * array.
255 *
256 * DRM property enum values are dynamic at runtime; the user must query the
257 * property to find out the desired runtime value for a requested string
258 * name. Using the 'type' field on planes as an example, there is no single
259 * hardcoded constant for primary plane types; instead, the property must be
260 * queried at runtime to find the value associated with the string "Primary".
261 *
262 * This helper queries and caches the enum values, to allow us to use a set
263 * of compile-time-constant enums portably across various implementations.
264 * The values given in enum_names are searched for, and stored in the
265 * same-indexed field of the map array.
266 *
267 * @param b DRM backend object
268 * @param src DRM property info array to source from
269 * @param info DRM property info array to copy into
270 * @param num_infos Number of entries in the source array
271 * @param props DRM object properties for the object
272 */
273void
274drm_property_info_populate(struct drm_backend *b,
275 const struct drm_property_info *src,
276 struct drm_property_info *info,
277 unsigned int num_infos,
278 drmModeObjectProperties *props)
279{
280 drmModePropertyRes *prop;
281 unsigned i, j;
282
283 for (i = 0; i < num_infos; i++) {
284 unsigned int j;
285
286 info[i].name = src[i].name;
287 info[i].prop_id = 0;
288 info[i].num_enum_values = src[i].num_enum_values;
289
290 if (src[i].num_enum_values == 0)
291 continue;
292
293 info[i].enum_values =
294 malloc(src[i].num_enum_values *
295 sizeof(*info[i].enum_values));
296 assert(info[i].enum_values);
297 for (j = 0; j < info[i].num_enum_values; j++) {
298 info[i].enum_values[j].name = src[i].enum_values[j].name;
299 info[i].enum_values[j].valid = false;
300 }
301 }
302
303 for (i = 0; i < props->count_props; i++) {
304 unsigned int k;
305
306 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
307 if (!prop)
308 continue;
309
310 for (j = 0; j < num_infos; j++) {
311 if (!strcmp(prop->name, info[j].name))
312 break;
313 }
314
315 /* We don't know/care about this property. */
316 if (j == num_infos) {
317#ifdef DEBUG
318 weston_log("DRM debug: unrecognized property %u '%s'\n",
319 prop->prop_id, prop->name);
320#endif
321 drmModeFreeProperty(prop);
322 continue;
323 }
324
325 if (info[j].num_enum_values == 0 &&
326 (prop->flags & DRM_MODE_PROP_ENUM)) {
327 weston_log("DRM: expected property %s to not be an"
328 " enum, but it is; ignoring\n", prop->name);
329 drmModeFreeProperty(prop);
330 continue;
331 }
332
333 info[j].prop_id = props->props[i];
Marius Vlad1accffe2019-11-01 12:00:09 +0200334 info[j].flags = prop->flags;
335
336 if (prop->flags & DRM_MODE_PROP_RANGE ||
337 prop->flags & DRM_MODE_PROP_SIGNED_RANGE) {
338 info[j].num_range_values = prop->count_values;
339 for (int i = 0; i < prop->count_values; i++)
340 info[j].range_values[i] = prop->values[i];
341 }
342
Daniel Stone4c2fc702019-06-18 11:12:07 +0100343
344 if (info[j].num_enum_values == 0) {
345 drmModeFreeProperty(prop);
346 continue;
347 }
348
349 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
350 weston_log("DRM: expected property %s to be an enum,"
351 " but it is not; ignoring\n", prop->name);
352 drmModeFreeProperty(prop);
353 info[j].prop_id = 0;
354 continue;
355 }
356
357 for (k = 0; k < info[j].num_enum_values; k++) {
358 int l;
359
360 for (l = 0; l < prop->count_enums; l++) {
361 if (!strcmp(prop->enums[l].name,
362 info[j].enum_values[k].name))
363 break;
364 }
365
366 if (l == prop->count_enums)
367 continue;
368
369 info[j].enum_values[k].valid = true;
370 info[j].enum_values[k].value = prop->enums[l].value;
371 }
372
373 drmModeFreeProperty(prop);
374 }
375
376#ifdef DEBUG
377 for (i = 0; i < num_infos; i++) {
378 if (info[i].prop_id == 0)
379 weston_log("DRM warning: property '%s' missing\n",
380 info[i].name);
381 }
382#endif
383}
384
385/**
386 * Free DRM property information
387 *
388 * Frees all memory associated with a DRM property info array and zeroes
389 * it out, leaving it usable for a further drm_property_info_update() or
390 * drm_property_info_free().
391 *
392 * @param info DRM property info array
393 * @param num_props Number of entries in array to free
394 */
395void
396drm_property_info_free(struct drm_property_info *info, int num_props)
397{
398 int i;
399
400 for (i = 0; i < num_props; i++)
401 free(info[i].enum_values);
402
403 memset(info, 0, sizeof(*info) * num_props);
404}
405
406#ifdef HAVE_DRM_FORMATS_BLOB
407static inline uint32_t *
408formats_ptr(struct drm_format_modifier_blob *blob)
409{
410 return (uint32_t *)(((char *)blob) + blob->formats_offset);
411}
412
413static inline struct drm_format_modifier *
414modifiers_ptr(struct drm_format_modifier_blob *blob)
415{
416 return (struct drm_format_modifier *)
417 (((char *)blob) + blob->modifiers_offset);
418}
419#endif
420
421/**
422 * Populates the plane's formats array, using either the IN_FORMATS blob
423 * property (if available), or the plane's format list if not.
424 */
425int
426drm_plane_populate_formats(struct drm_plane *plane, const drmModePlane *kplane,
427 const drmModeObjectProperties *props)
428{
429 unsigned i;
430#ifdef HAVE_DRM_FORMATS_BLOB
431 drmModePropertyBlobRes *blob;
432 struct drm_format_modifier_blob *fmt_mod_blob;
433 struct drm_format_modifier *blob_modifiers;
434 uint32_t *blob_formats;
435 uint32_t blob_id;
436
437 blob_id = drm_property_get_value(&plane->props[WDRM_PLANE_IN_FORMATS],
438 props,
439 0);
440 if (blob_id == 0)
441 goto fallback;
442
443 blob = drmModeGetPropertyBlob(plane->backend->drm.fd, blob_id);
444 if (!blob)
445 goto fallback;
446
447 fmt_mod_blob = blob->data;
448 blob_formats = formats_ptr(fmt_mod_blob);
449 blob_modifiers = modifiers_ptr(fmt_mod_blob);
450
451 if (plane->count_formats != fmt_mod_blob->count_formats) {
452 weston_log("DRM backend: format count differs between "
453 "plane (%d) and IN_FORMATS (%d)\n",
454 plane->count_formats,
455 fmt_mod_blob->count_formats);
456 weston_log("This represents a kernel bug; Weston is "
457 "unable to continue.\n");
458 abort();
459 }
460
461 for (i = 0; i < fmt_mod_blob->count_formats; i++) {
462 uint32_t count_modifiers = 0;
463 uint64_t *modifiers = NULL;
464 unsigned j;
465
466 for (j = 0; j < fmt_mod_blob->count_modifiers; j++) {
467 struct drm_format_modifier *mod = &blob_modifiers[j];
468
469 if ((i < mod->offset) || (i > mod->offset + 63))
470 continue;
471 if (!(mod->formats & (1 << (i - mod->offset))))
472 continue;
473
474 modifiers = realloc(modifiers,
475 (count_modifiers + 1) *
476 sizeof(modifiers[0]));
477 assert(modifiers);
478 modifiers[count_modifiers++] = mod->modifier;
479 }
480
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900481 if (count_modifiers == 0) {
482 modifiers = malloc(sizeof(*modifiers));
483 *modifiers = DRM_FORMAT_MOD_LINEAR;
484 count_modifiers = 1;
485 }
486
Daniel Stone4c2fc702019-06-18 11:12:07 +0100487 plane->formats[i].format = blob_formats[i];
488 plane->formats[i].modifiers = modifiers;
489 plane->formats[i].count_modifiers = count_modifiers;
490 }
491
492 drmModeFreePropertyBlob(blob);
493
494 return 0;
495
496fallback:
497#endif
498 /* No IN_FORMATS blob available, so just use the old. */
499 assert(plane->count_formats == kplane->count_formats);
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900500 for (i = 0; i < kplane->count_formats; i++) {
Daniel Stone4c2fc702019-06-18 11:12:07 +0100501 plane->formats[i].format = kplane->formats[i];
Tomohito Esaki29beeaf2019-06-24 17:23:44 +0900502 plane->formats[i].modifiers = malloc(sizeof(uint64_t));
503 plane->formats[i].modifiers[0] = DRM_FORMAT_MOD_LINEAR;
504 plane->formats[i].count_modifiers = 1;
505 }
Daniel Stone4c2fc702019-06-18 11:12:07 +0100506
507 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,
524 output->crtc_id,
525 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) {
559 drm_debug(b, "\t[CRTC:%u] setting pending flip\n", output->crtc_id);
560 output->atomic_complete_pending = 1;
561 }
562
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530563 if (b->atomic_modeset &&
564 state->protection == WESTON_HDCP_DISABLE)
565 wl_list_for_each(head, &output->base.head_list, base.output_link)
566 weston_head_set_content_protection_status(&head->base,
567 WESTON_HDCP_DISABLE);
568
Daniel Stone4c2fc702019-06-18 11:12:07 +0100569 /* Replace state_cur on each affected plane with the new state, being
570 * careful to dispose of orphaned (but only orphaned) previous state.
571 * If the previous state is not orphaned (still has an output_state
572 * attached), it will be disposed of by freeing the output_state. */
573 wl_list_for_each(plane_state, &state->plane_list, link) {
574 struct drm_plane *plane = plane_state->plane;
575
576 if (plane->state_cur && !plane->state_cur->output_state)
577 drm_plane_state_free(plane->state_cur, true);
578 plane->state_cur = plane_state;
579
580 if (mode != DRM_STATE_APPLY_ASYNC) {
581 plane_state->complete = true;
582 continue;
583 }
584
585 if (b->atomic_modeset)
586 continue;
587
588 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY);
589 if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
590 output->page_flip_pending = 1;
591 }
592}
593
594static void
595drm_output_set_cursor(struct drm_output_state *output_state)
596{
597 struct drm_output *output = output_state->output;
598 struct drm_backend *b = to_drm_backend(output->base.compositor);
599 struct drm_plane *plane = output->cursor_plane;
600 struct drm_plane_state *state;
Stefan Agner974390a2019-07-08 00:42:05 +0200601 uint32_t handle;
Daniel Stone4c2fc702019-06-18 11:12:07 +0100602
603 if (!plane)
604 return;
605
606 state = drm_output_state_get_existing_plane(output_state, plane);
607 if (!state)
608 return;
609
610 if (!state->fb) {
611 pixman_region32_fini(&plane->base.damage);
612 pixman_region32_init(&plane->base.damage);
613 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
614 return;
615 }
616
617 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
618 assert(!plane->state_cur->output || plane->state_cur->output == output);
619
Stefan Agner974390a2019-07-08 00:42:05 +0200620 handle = output->gbm_cursor_handle[output->current_cursor];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100621 if (plane->state_cur->fb != state->fb) {
Daniel Stone4c2fc702019-06-18 11:12:07 +0100622 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
623 b->cursor_width, b->cursor_height)) {
624 weston_log("failed to set cursor: %s\n",
625 strerror(errno));
626 goto err;
627 }
628 }
629
630 pixman_region32_fini(&plane->base.damage);
631 pixman_region32_init(&plane->base.damage);
632
633 if (drmModeMoveCursor(b->drm.fd, output->crtc_id,
634 state->dest_x, state->dest_y)) {
635 weston_log("failed to move cursor: %s\n", strerror(errno));
636 goto err;
637 }
638
639 return;
640
641err:
642 b->cursors_are_broken = 1;
643 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
644}
645
646static int
647drm_output_apply_state_legacy(struct drm_output_state *state)
648{
649 struct drm_output *output = state->output;
650 struct drm_backend *backend = to_drm_backend(output->base.compositor);
651 struct drm_plane *scanout_plane = output->scanout_plane;
652 struct drm_property_info *dpms_prop;
653 struct drm_plane_state *scanout_state;
654 struct drm_mode *mode;
655 struct drm_head *head;
656 const struct pixel_format_info *pinfo = NULL;
657 uint32_t connectors[MAX_CLONED_CONNECTORS];
658 int n_conn = 0;
659 struct timespec now;
660 int ret = 0;
661
662 wl_list_for_each(head, &output->base.head_list, base.output_link) {
663 assert(n_conn < MAX_CLONED_CONNECTORS);
664 connectors[n_conn++] = head->connector_id;
665 }
666
667 /* If disable_planes is set then assign_planes() wasn't
668 * called for this render, so we could still have a stale
669 * cursor plane set up.
670 */
671 if (output->base.disable_planes) {
672 output->cursor_view = NULL;
673 if (output->cursor_plane) {
674 output->cursor_plane->base.x = INT32_MIN;
675 output->cursor_plane->base.y = INT32_MIN;
676 }
677 }
678
679 if (state->dpms != WESTON_DPMS_ON) {
680 if (output->cursor_plane) {
681 ret = drmModeSetCursor(backend->drm.fd, output->crtc_id,
682 0, 0, 0);
683 if (ret)
684 weston_log("drmModeSetCursor failed disable: %s\n",
685 strerror(errno));
686 }
687
688 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0,
689 NULL, 0, NULL);
690 if (ret)
691 weston_log("drmModeSetCrtc failed disabling: %s\n",
692 strerror(errno));
693
694 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
695 weston_compositor_read_presentation_clock(output->base.compositor, &now);
696 drm_output_update_complete(output,
697 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
698 now.tv_sec, now.tv_nsec / 1000);
699
700 return 0;
701 }
702
703 scanout_state =
704 drm_output_state_get_existing_plane(state, scanout_plane);
705
706 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
707 * legacy PageFlip API doesn't allow us to do clipping either. */
708 assert(scanout_state->src_x == 0);
709 assert(scanout_state->src_y == 0);
710 assert(scanout_state->src_w ==
711 (unsigned) (output->base.current_mode->width << 16));
712 assert(scanout_state->src_h ==
713 (unsigned) (output->base.current_mode->height << 16));
714 assert(scanout_state->dest_x == 0);
715 assert(scanout_state->dest_y == 0);
716 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
717 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
718 /* The legacy SetCrtc API doesn't support fences */
719 assert(scanout_state->in_fence_fd == -1);
720
721 mode = to_drm_mode(output->base.current_mode);
722 if (backend->state_invalid ||
723 !scanout_plane->state_cur->fb ||
724 scanout_plane->state_cur->fb->strides[0] !=
725 scanout_state->fb->strides[0]) {
726
727 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
728 scanout_state->fb->fb_id,
729 0, 0,
730 connectors, n_conn,
731 &mode->mode_info);
732 if (ret) {
733 weston_log("set mode failed: %s\n", strerror(errno));
734 goto err;
735 }
736 }
737
738 pinfo = scanout_state->fb->format;
739 drm_debug(backend, "\t[CRTC:%u, PLANE:%u] FORMAT: %s\n",
740 output->crtc_id, scanout_state->plane->plane_id,
741 pinfo ? pinfo->drm_format_name : "UNKNOWN");
742
743 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
744 scanout_state->fb->fb_id,
745 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
746 weston_log("queueing pageflip failed: %s\n", strerror(errno));
747 goto err;
748 }
749
750 assert(!output->page_flip_pending);
751
752 if (output->pageflip_timer)
753 wl_event_source_timer_update(output->pageflip_timer,
754 backend->pageflip_timeout);
755
756 drm_output_set_cursor(state);
757
758 if (state->dpms != output->state_cur->dpms) {
759 wl_list_for_each(head, &output->base.head_list, base.output_link) {
760 dpms_prop = &head->props_conn[WDRM_CONNECTOR_DPMS];
761 if (dpms_prop->prop_id == 0)
762 continue;
763
764 ret = drmModeConnectorSetProperty(backend->drm.fd,
765 head->connector_id,
766 dpms_prop->prop_id,
767 state->dpms);
768 if (ret) {
769 weston_log("DRM: DPMS: failed property set for %s\n",
770 head->base.name);
771 }
772 }
773 }
774
775 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
776
777 return 0;
778
779err:
780 output->cursor_view = NULL;
781 drm_output_state_free(state);
782 return -1;
783}
784
785#ifdef HAVE_DRM_ATOMIC
786static int
787crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output,
788 enum wdrm_crtc_property prop, uint64_t val)
789{
790 struct drm_property_info *info = &output->props_crtc[prop];
791 int ret;
792
793 if (info->prop_id == 0)
794 return -1;
795
796 ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id,
797 val);
798 drm_debug(output->backend, "\t\t\t[CRTC:%lu] %lu (%s) -> %llu (0x%llx)\n",
799 (unsigned long) output->crtc_id,
800 (unsigned long) info->prop_id, info->name,
801 (unsigned long long) val, (unsigned long long) val);
802 return (ret <= 0) ? -1 : 0;
803}
804
805static int
806connector_add_prop(drmModeAtomicReq *req, struct drm_head *head,
807 enum wdrm_connector_property prop, uint64_t val)
808{
809 struct drm_property_info *info = &head->props_conn[prop];
810 int ret;
811
812 if (info->prop_id == 0)
813 return -1;
814
815 ret = drmModeAtomicAddProperty(req, head->connector_id,
816 info->prop_id, val);
817 drm_debug(head->backend, "\t\t\t[CONN:%lu] %lu (%s) -> %llu (0x%llx)\n",
818 (unsigned long) head->connector_id,
819 (unsigned long) info->prop_id, info->name,
820 (unsigned long long) val, (unsigned long long) val);
821 return (ret <= 0) ? -1 : 0;
822}
823
824static int
825plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
826 enum wdrm_plane_property prop, uint64_t val)
827{
828 struct drm_property_info *info = &plane->props[prop];
829 int ret;
830
831 if (info->prop_id == 0)
832 return -1;
833
834 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
835 val);
836 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] %lu (%s) -> %llu (0x%llx)\n",
837 (unsigned long) plane->plane_id,
838 (unsigned long) info->prop_id, info->name,
839 (unsigned long long) val, (unsigned long long) val);
840 return (ret <= 0) ? -1 : 0;
841}
842
843
844static int
845plane_add_damage(drmModeAtomicReq *req, struct drm_backend *backend,
846 struct drm_plane_state *plane_state)
847{
848 struct drm_plane *plane = plane_state->plane;
849 struct drm_property_info *info =
850 &plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS];
851 pixman_box32_t *rects;
852 uint32_t blob_id;
853 int n_rects;
854 int ret;
855
856 if (!pixman_region32_not_empty(&plane_state->damage))
857 return 0;
858
859 /*
860 * If a plane doesn't support fb damage blob property, kernel will
861 * perform full plane update.
862 */
863 if (info->prop_id == 0)
864 return 0;
865
866 rects = pixman_region32_rectangles(&plane_state->damage, &n_rects);
867
868 ret = drmModeCreatePropertyBlob(backend->drm.fd, rects,
869 sizeof(*rects) * n_rects, &blob_id);
870 if (ret != 0)
871 return ret;
872
873 ret = plane_add_prop(req, plane, WDRM_PLANE_FB_DAMAGE_CLIPS, blob_id);
874 if (ret != 0)
875 return ret;
876
877 return 0;
878}
879
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530880static bool
881drm_head_has_prop(struct drm_head *head,
882 enum wdrm_connector_property prop)
883{
884 if (head && head->props_conn[prop].prop_id != 0)
885 return true;
886
887 return false;
888}
889
890/*
891 * This function converts the protection requests from weston_hdcp_protection
892 * corresponding drm values. These values can be set in "Content Protection"
893 * & "HDCP Content Type" connector properties.
894 */
895static void
896get_drm_protection_from_weston(enum weston_hdcp_protection weston_protection,
897 enum wdrm_content_protection_state *drm_protection,
898 enum wdrm_hdcp_content_type *drm_cp_type)
899{
900
901 switch (weston_protection) {
902 case WESTON_HDCP_DISABLE:
903 *drm_protection = WDRM_CONTENT_PROTECTION_UNDESIRED;
904 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
905 break;
906 case WESTON_HDCP_ENABLE_TYPE_0:
907 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
908 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE0;
909 break;
910 case WESTON_HDCP_ENABLE_TYPE_1:
911 *drm_protection = WDRM_CONTENT_PROTECTION_DESIRED;
912 *drm_cp_type = WDRM_HDCP_CONTENT_TYPE1;
913 break;
914 default:
915 assert(0 && "bad weston_hdcp_protection");
916 }
917}
918
919static void
920drm_head_set_hdcp_property(struct drm_head *head,
921 enum weston_hdcp_protection protection,
922 drmModeAtomicReq *req)
923{
924 int ret;
925 enum wdrm_content_protection_state drm_protection;
926 enum wdrm_hdcp_content_type drm_cp_type;
927 struct drm_property_enum_info *enum_info;
928 uint64_t prop_val;
929
930 get_drm_protection_from_weston(protection, &drm_protection,
931 &drm_cp_type);
932
933 if (!drm_head_has_prop(head, WDRM_CONNECTOR_CONTENT_PROTECTION))
934 return;
935
936 /*
937 * Content-type property is not exposed for platforms not supporting
938 * HDCP2.2, therefore, type-1 cannot be supported. The type-0 content
939 * still can be supported if the content-protection property is exposed.
940 */
941 if (!drm_head_has_prop(head, WDRM_CONNECTOR_HDCP_CONTENT_TYPE) &&
942 drm_cp_type != WDRM_HDCP_CONTENT_TYPE0)
943 return;
944
945 enum_info = head->props_conn[WDRM_CONNECTOR_CONTENT_PROTECTION].enum_values;
946 prop_val = enum_info[drm_protection].value;
947 ret = connector_add_prop(req, head, WDRM_CONNECTOR_CONTENT_PROTECTION,
948 prop_val);
949 assert(ret == 0);
950
Ankit Nautiyalfc2c1802019-08-30 19:40:46 +0530951 if (!drm_head_has_prop(head, WDRM_CONNECTOR_HDCP_CONTENT_TYPE))
952 return;
953
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530954 enum_info = head->props_conn[WDRM_CONNECTOR_HDCP_CONTENT_TYPE].enum_values;
955 prop_val = enum_info[drm_cp_type].value;
956 ret = connector_add_prop(req, head, WDRM_CONNECTOR_HDCP_CONTENT_TYPE,
957 prop_val);
958 assert(ret == 0);
959}
960
Daniel Stone4c2fc702019-06-18 11:12:07 +0100961static int
962drm_output_apply_state_atomic(struct drm_output_state *state,
963 drmModeAtomicReq *req,
964 uint32_t *flags)
965{
966 struct drm_output *output = state->output;
967 struct drm_backend *b = to_drm_backend(output->base.compositor);
968 struct drm_plane_state *plane_state;
969 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
970 struct drm_head *head;
971 int ret = 0;
972
973 drm_debug(b, "\t\t[atomic] %s output %lu (%s) state\n",
974 (*flags & DRM_MODE_ATOMIC_TEST_ONLY) ? "testing" : "applying",
975 (unsigned long) output->base.id, output->base.name);
976
977 if (state->dpms != output->state_cur->dpms) {
978 drm_debug(b, "\t\t\t[atomic] DPMS state differs, modeset OK\n");
979 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
980 }
981
982 if (state->dpms == WESTON_DPMS_ON) {
983 ret = drm_mode_ensure_blob(b, current_mode);
984 if (ret != 0)
985 return ret;
986
987 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID,
988 current_mode->blob_id);
989 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1);
990
991 /* No need for the DPMS property, since it is implicit in
992 * routing and CRTC activity. */
993 wl_list_for_each(head, &output->base.head_list, base.output_link) {
994 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID,
995 output->crtc_id);
996 }
997 } else {
998 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0);
999 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0);
1000
1001 /* No need for the DPMS property, since it is implicit in
1002 * routing and CRTC activity. */
1003 wl_list_for_each(head, &output->base.head_list, base.output_link)
1004 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, 0);
1005 }
1006
Ankit Nautiyala344fe32019-05-14 18:36:08 +05301007 wl_list_for_each(head, &output->base.head_list, base.output_link)
1008 drm_head_set_hdcp_property(head, state->protection, req);
1009
Daniel Stone4c2fc702019-06-18 11:12:07 +01001010 if (ret != 0) {
1011 weston_log("couldn't set atomic CRTC/connector state\n");
1012 return ret;
1013 }
1014
1015 wl_list_for_each(plane_state, &state->plane_list, link) {
1016 struct drm_plane *plane = plane_state->plane;
1017 const struct pixel_format_info *pinfo = NULL;
1018
1019 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
1020 plane_state->fb ? plane_state->fb->fb_id : 0);
1021 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
1022 plane_state->fb ? output->crtc_id : 0);
1023 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
1024 plane_state->src_x);
1025 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
1026 plane_state->src_y);
1027 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
1028 plane_state->src_w);
1029 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
1030 plane_state->src_h);
1031 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
1032 plane_state->dest_x);
1033 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
1034 plane_state->dest_y);
1035 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
1036 plane_state->dest_w);
1037 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
1038 plane_state->dest_h);
1039 ret |= plane_add_damage(req, b, plane_state);
1040
1041 if (plane_state->fb && plane_state->fb->format)
1042 pinfo = plane_state->fb->format;
1043
1044 drm_debug(plane->backend, "\t\t\t[PLANE:%lu] FORMAT: %s\n",
1045 (unsigned long) plane->plane_id,
1046 pinfo ? pinfo->drm_format_name : "UNKNOWN");
1047
1048 if (plane_state->in_fence_fd >= 0) {
1049 ret |= plane_add_prop(req, plane,
1050 WDRM_PLANE_IN_FENCE_FD,
1051 plane_state->in_fence_fd);
1052 }
1053
Marius Vladcdd6fa22019-08-29 20:42:00 +03001054 /* do note, that 'invented' zpos values are set as immutable */
1055 if (plane_state->zpos != DRM_PLANE_ZPOS_INVALID_PLANE &&
1056 plane_state->plane->zpos_min != plane_state->plane->zpos_max)
1057 ret |= plane_add_prop(req, plane,
1058 WDRM_PLANE_ZPOS,
1059 plane_state->zpos);
1060
Daniel Stone4c2fc702019-06-18 11:12:07 +01001061 if (ret != 0) {
1062 weston_log("couldn't set plane state\n");
1063 return ret;
1064 }
1065 }
1066
1067 return 0;
1068}
1069
1070/**
1071 * Helper function used only by drm_pending_state_apply, with the same
1072 * guarantees and constraints as that function.
1073 */
1074static int
1075drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
1076 enum drm_state_apply_mode mode)
1077{
1078 struct drm_backend *b = pending_state->backend;
1079 struct drm_output_state *output_state, *tmp;
1080 struct drm_plane *plane;
1081 drmModeAtomicReq *req = drmModeAtomicAlloc();
1082 uint32_t flags;
1083 int ret = 0;
1084
1085 if (!req)
1086 return -1;
1087
1088 switch (mode) {
1089 case DRM_STATE_APPLY_SYNC:
1090 flags = 0;
1091 break;
1092 case DRM_STATE_APPLY_ASYNC:
1093 flags = DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
1094 break;
1095 case DRM_STATE_TEST_ONLY:
1096 flags = DRM_MODE_ATOMIC_TEST_ONLY;
1097 break;
1098 }
1099
1100 if (b->state_invalid) {
1101 struct weston_head *head_base;
1102 struct drm_head *head;
1103 uint32_t *unused;
1104 int err;
1105
1106 drm_debug(b, "\t\t[atomic] previous state invalid; "
1107 "starting with fresh state\n");
1108
1109 /* If we need to reset all our state (e.g. because we've
1110 * just started, or just been VT-switched in), explicitly
1111 * disable all the CRTCs and connectors we aren't using. */
1112 wl_list_for_each(head_base,
1113 &b->compositor->head_list, compositor_link) {
1114 struct drm_property_info *info;
1115
1116 if (weston_head_is_enabled(head_base))
1117 continue;
1118
1119 head = to_drm_head(head_base);
1120
1121 drm_debug(b, "\t\t[atomic] disabling inactive head %s\n",
1122 head_base->name);
1123
1124 info = &head->props_conn[WDRM_CONNECTOR_CRTC_ID];
1125 err = drmModeAtomicAddProperty(req, head->connector_id,
1126 info->prop_id, 0);
1127 drm_debug(b, "\t\t\t[CONN:%lu] %lu (%s) -> 0\n",
1128 (unsigned long) head->connector_id,
1129 (unsigned long) info->prop_id,
1130 info->name);
1131 if (err <= 0)
1132 ret = -1;
1133 }
1134
1135 wl_array_for_each(unused, &b->unused_crtcs) {
1136 struct drm_property_info infos[WDRM_CRTC__COUNT];
1137 struct drm_property_info *info;
1138 drmModeObjectProperties *props;
1139 uint64_t active;
1140
1141 memset(infos, 0, sizeof(infos));
1142
1143 /* We can't emit a disable on a CRTC that's already
1144 * off, as the kernel will refuse to generate an event
1145 * for an off->off state and fail the commit.
1146 */
1147 props = drmModeObjectGetProperties(b->drm.fd,
1148 *unused,
1149 DRM_MODE_OBJECT_CRTC);
1150 if (!props) {
1151 ret = -1;
1152 continue;
1153 }
1154
1155 drm_property_info_populate(b, crtc_props, infos,
1156 WDRM_CRTC__COUNT,
1157 props);
1158
1159 info = &infos[WDRM_CRTC_ACTIVE];
1160 active = drm_property_get_value(info, props, 0);
1161 drmModeFreeObjectProperties(props);
1162 if (active == 0) {
1163 drm_property_info_free(infos, WDRM_CRTC__COUNT);
1164 continue;
1165 }
1166
1167 drm_debug(b, "\t\t[atomic] disabling unused CRTC %lu\n",
1168 (unsigned long) *unused);
1169
1170 drm_debug(b, "\t\t\t[CRTC:%lu] %lu (%s) -> 0\n",
1171 (unsigned long) *unused,
1172 (unsigned long) info->prop_id, info->name);
1173 err = drmModeAtomicAddProperty(req, *unused,
1174 info->prop_id, 0);
1175 if (err <= 0)
1176 ret = -1;
1177
1178 info = &infos[WDRM_CRTC_MODE_ID];
1179 drm_debug(b, "\t\t\t[CRTC:%lu] %lu (%s) -> 0\n",
1180 (unsigned long) *unused,
1181 (unsigned long) info->prop_id, info->name);
1182 err = drmModeAtomicAddProperty(req, *unused,
1183 info->prop_id, 0);
1184 if (err <= 0)
1185 ret = -1;
1186
1187 drm_property_info_free(infos, WDRM_CRTC__COUNT);
1188 }
1189
1190 /* Disable all the planes; planes which are being used will
1191 * override this state in the output-state application. */
1192 wl_list_for_each(plane, &b->plane_list, link) {
1193 drm_debug(b, "\t\t[atomic] starting with plane %lu disabled\n",
1194 (unsigned long) plane->plane_id);
1195 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
1196 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
1197 }
1198
1199 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
1200 }
1201
1202 wl_list_for_each(output_state, &pending_state->output_list, link) {
1203 if (output_state->output->virtual)
1204 continue;
1205 if (mode == DRM_STATE_APPLY_SYNC)
1206 assert(output_state->dpms == WESTON_DPMS_OFF);
1207 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
1208 }
1209
1210 if (ret != 0) {
1211 weston_log("atomic: couldn't compile atomic state\n");
1212 goto out;
1213 }
1214
1215 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
1216 drm_debug(b, "[atomic] drmModeAtomicCommit\n");
1217
1218 /* Test commits do not take ownership of the state; return
1219 * without freeing here. */
1220 if (mode == DRM_STATE_TEST_ONLY) {
1221 drmModeAtomicFree(req);
1222 return ret;
1223 }
1224
1225 if (ret != 0) {
1226 weston_log("atomic: couldn't commit new state: %s\n",
1227 strerror(errno));
1228 goto out;
1229 }
1230
1231 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1232 link)
1233 drm_output_assign_state(output_state, mode);
1234
1235 b->state_invalid = false;
1236
1237 assert(wl_list_empty(&pending_state->output_list));
1238
1239out:
1240 drmModeAtomicFree(req);
1241 drm_pending_state_free(pending_state);
1242 return ret;
1243}
1244#endif
1245
1246/**
1247 * Tests a pending state, to see if the kernel will accept the update as
1248 * constructed.
1249 *
1250 * Using atomic modesetting, the kernel performs the same checks as it would
1251 * on a real commit, returning success or failure without actually modifying
1252 * the running state. It does not return -EBUSY if there are pending updates
1253 * in flight, so states may be tested at any point, however this means a
1254 * state which passed testing may fail on a real commit if the timing is not
1255 * respected (e.g. committing before the previous commit has completed).
1256 *
1257 * Without atomic modesetting, we have no way to check, so we optimistically
1258 * claim it will work.
1259 *
1260 * Unlike drm_pending_state_apply() and drm_pending_state_apply_sync(), this
1261 * function does _not_ take ownership of pending_state, nor does it clear
1262 * state_invalid.
1263 */
1264int
1265drm_pending_state_test(struct drm_pending_state *pending_state)
1266{
1267#ifdef HAVE_DRM_ATOMIC
1268 struct drm_backend *b = pending_state->backend;
1269
1270 if (b->atomic_modeset)
1271 return drm_pending_state_apply_atomic(pending_state,
1272 DRM_STATE_TEST_ONLY);
1273#endif
1274
1275 /* We have no way to test state before application on the legacy
1276 * modesetting API, so just claim it succeeded. */
1277 return 0;
1278}
1279
1280/**
1281 * Applies all of a pending_state asynchronously: the primary entry point for
1282 * applying KMS state to a device. Updates the state for all outputs in the
1283 * pending_state, as well as disabling any unclaimed outputs.
1284 *
1285 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1286 */
1287int
1288drm_pending_state_apply(struct drm_pending_state *pending_state)
1289{
1290 struct drm_backend *b = pending_state->backend;
1291 struct drm_output_state *output_state, *tmp;
1292 uint32_t *unused;
1293
1294#ifdef HAVE_DRM_ATOMIC
1295 if (b->atomic_modeset)
1296 return drm_pending_state_apply_atomic(pending_state,
1297 DRM_STATE_APPLY_ASYNC);
1298#endif
1299
1300 if (b->state_invalid) {
1301 /* If we need to reset all our state (e.g. because we've
1302 * just started, or just been VT-switched in), explicitly
1303 * disable all the CRTCs we aren't using. This also disables
1304 * all connectors on these CRTCs, so we don't need to do that
1305 * separately with the pre-atomic API. */
1306 wl_array_for_each(unused, &b->unused_crtcs)
1307 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
1308 NULL);
1309 }
1310
1311 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1312 link) {
1313 struct drm_output *output = output_state->output;
1314 int ret;
1315
1316 if (output->virtual) {
1317 drm_output_assign_state(output_state,
1318 DRM_STATE_APPLY_ASYNC);
1319 continue;
1320 }
1321
1322 ret = drm_output_apply_state_legacy(output_state);
1323 if (ret != 0) {
1324 weston_log("Couldn't apply state for output %s\n",
1325 output->base.name);
1326 }
1327 }
1328
1329 b->state_invalid = false;
1330
1331 assert(wl_list_empty(&pending_state->output_list));
1332
1333 drm_pending_state_free(pending_state);
1334
1335 return 0;
1336}
1337
1338/**
1339 * The synchronous version of drm_pending_state_apply. May only be used to
1340 * disable outputs. Does so synchronously: the request is guaranteed to have
1341 * completed on return, and the output will not be touched afterwards.
1342 *
1343 * Unconditionally takes ownership of pending_state, and clears state_invalid.
1344 */
1345int
1346drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
1347{
1348 struct drm_backend *b = pending_state->backend;
1349 struct drm_output_state *output_state, *tmp;
1350 uint32_t *unused;
1351
1352#ifdef HAVE_DRM_ATOMIC
1353 if (b->atomic_modeset)
1354 return drm_pending_state_apply_atomic(pending_state,
1355 DRM_STATE_APPLY_SYNC);
1356#endif
1357
1358 if (b->state_invalid) {
1359 /* If we need to reset all our state (e.g. because we've
1360 * just started, or just been VT-switched in), explicitly
1361 * disable all the CRTCs we aren't using. This also disables
1362 * all connectors on these CRTCs, so we don't need to do that
1363 * separately with the pre-atomic API. */
1364 wl_array_for_each(unused, &b->unused_crtcs)
1365 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
1366 NULL);
1367 }
1368
1369 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1370 link) {
1371 int ret;
1372
1373 assert(output_state->dpms == WESTON_DPMS_OFF);
1374 ret = drm_output_apply_state_legacy(output_state);
1375 if (ret != 0) {
1376 weston_log("Couldn't apply state for output %s\n",
1377 output_state->output->base.name);
1378 }
1379 }
1380
1381 b->state_invalid = false;
1382
1383 assert(wl_list_empty(&pending_state->output_list));
1384
1385 drm_pending_state_free(pending_state);
1386
1387 return 0;
1388}
1389
1390void
1391drm_output_update_msc(struct drm_output *output, unsigned int seq)
1392{
1393 uint64_t msc_hi = output->base.msc >> 32;
1394
1395 if (seq < (output->base.msc & 0xffffffff))
1396 msc_hi++;
1397
1398 output->base.msc = (msc_hi << 32) + seq;
1399}
1400
1401static void
1402page_flip_handler(int fd, unsigned int frame,
1403 unsigned int sec, unsigned int usec, void *data)
1404{
1405 struct drm_output *output = data;
1406 struct drm_backend *b = to_drm_backend(output->base.compositor);
1407 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1408 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1409 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1410
1411 drm_output_update_msc(output, frame);
1412
1413 assert(!b->atomic_modeset);
1414 assert(output->page_flip_pending);
1415 output->page_flip_pending = 0;
1416
1417 drm_output_update_complete(output, flags, sec, usec);
1418}
1419
1420#ifdef HAVE_DRM_ATOMIC
1421static void
1422atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
1423 unsigned int usec, unsigned int crtc_id, void *data)
1424{
1425 struct drm_backend *b = data;
1426 struct drm_output *output = drm_output_find_by_crtc(b, crtc_id);
1427 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1428 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1429 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
1430
1431 /* During the initial modeset, we can disable CRTCs which we don't
1432 * actually handle during normal operation; this will give us events
1433 * for unknown outputs. Ignore them. */
1434 if (!output || !output->base.enabled)
1435 return;
1436
1437 drm_output_update_msc(output, frame);
1438
1439 drm_debug(b, "[atomic][CRTC:%u] flip processing started\n", crtc_id);
1440 assert(b->atomic_modeset);
1441 assert(output->atomic_complete_pending);
1442 output->atomic_complete_pending = 0;
1443
1444 drm_output_update_complete(output, flags, sec, usec);
1445 drm_debug(b, "[atomic][CRTC:%u] flip processing completed\n", crtc_id);
1446}
1447#endif
1448
1449int
1450on_drm_input(int fd, uint32_t mask, void *data)
1451{
1452#ifdef HAVE_DRM_ATOMIC
1453 struct drm_backend *b = data;
1454#endif
1455 drmEventContext evctx;
1456
1457 memset(&evctx, 0, sizeof evctx);
1458#ifndef HAVE_DRM_ATOMIC
1459 evctx.version = 2;
1460#else
1461 evctx.version = 3;
1462 if (b->atomic_modeset)
1463 evctx.page_flip_handler2 = atomic_flip_handler;
1464 else
1465#endif
1466 evctx.page_flip_handler = page_flip_handler;
1467 drmHandleEvent(fd, &evctx);
1468
1469 return 1;
1470}
1471
1472int
1473init_kms_caps(struct drm_backend *b)
1474{
1475 uint64_t cap;
1476 int ret;
1477 clockid_t clk_id;
1478
1479 weston_log("using %s\n", b->drm.filename);
1480
1481 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1482 if (ret == 0 && cap == 1)
1483 clk_id = CLOCK_MONOTONIC;
1484 else
1485 clk_id = CLOCK_REALTIME;
1486
1487 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
1488 weston_log("Error: failed to set presentation clock %d.\n",
1489 clk_id);
1490 return -1;
1491 }
1492
1493 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
1494 if (ret == 0)
1495 b->cursor_width = cap;
1496 else
1497 b->cursor_width = 64;
1498
1499 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1500 if (ret == 0)
1501 b->cursor_height = cap;
1502 else
1503 b->cursor_height = 64;
1504
1505 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
1506 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
1507 b->universal_planes = (ret == 0);
1508 }
1509 weston_log("DRM: %s universal planes\n",
1510 b->universal_planes ? "supports" : "does not support");
1511
1512#ifdef HAVE_DRM_ATOMIC
1513 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
1514 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
1515 if (ret != 0)
1516 cap = 0;
1517 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
1518 b->atomic_modeset = ((ret == 0) && (cap == 1));
1519 }
1520#endif
1521 weston_log("DRM: %s atomic modesetting\n",
1522 b->atomic_modeset ? "supports" : "does not support");
1523
1524#ifdef HAVE_DRM_ADDFB2_MODIFIERS
1525 ret = drmGetCap(b->drm.fd, DRM_CAP_ADDFB2_MODIFIERS, &cap);
1526 if (ret == 0)
1527 b->fb_modifiers = cap;
1528 else
1529#endif
1530 b->fb_modifiers = 0;
1531
1532 /*
1533 * KMS support for hardware planes cannot properly synchronize
1534 * without nuclear page flip. Without nuclear/atomic, hw plane
1535 * and cursor plane updates would either tear or cause extra
1536 * waits for vblanks which means dropping the compositor framerate
1537 * to a fraction. For cursors, it's not so bad, so they are
1538 * enabled.
1539 */
1540 if (!b->atomic_modeset || getenv("WESTON_FORCE_RENDERER"))
1541 b->sprites_are_broken = 1;
1542
1543 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1);
1544 b->aspect_ratio_supported = (ret == 0);
1545 weston_log("DRM: %s picture aspect ratio\n",
1546 b->aspect_ratio_supported ? "supports" : "does not support");
1547
1548 return 0;
1549}