blob: 283cb573a13337e026de4dc91edeb3468e17c1ef [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: MIT
2/*
3 * Copyright 2018 Noralf Trønnes
4 * Copyright (c) 2006-2009 Red Hat Inc.
5 * Copyright (c) 2006-2008 Intel Corporation
6 * Jesse Barnes <jesse.barnes@intel.com>
7 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
8 */
9
10#include <linux/module.h>
11#include <linux/mutex.h>
12#include <linux/slab.h>
13
14#include <drm/drm_atomic.h>
15#include <drm/drm_client.h>
16#include <drm/drm_connector.h>
17#include <drm/drm_crtc.h>
18#include <drm/drm_device.h>
19#include <drm/drm_drv.h>
20#include <drm/drm_encoder.h>
21#include <drm/drm_print.h>
22
23#include "drm_crtc_internal.h"
24#include "drm_internal.h"
25
26#define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
27
28struct drm_client_offset {
29 int x, y;
30};
31
32int drm_client_modeset_create(struct drm_client_dev *client)
33{
34 struct drm_device *dev = client->dev;
35 unsigned int num_crtc = dev->mode_config.num_crtc;
36 unsigned int max_connector_count = 1;
37 struct drm_mode_set *modeset;
38 struct drm_crtc *crtc;
39 unsigned int i = 0;
40
41 /* Add terminating zero entry to enable index less iteration */
42 client->modesets = kcalloc(num_crtc + 1, sizeof(*client->modesets), GFP_KERNEL);
43 if (!client->modesets)
44 return -ENOMEM;
45
46 mutex_init(&client->modeset_mutex);
47
48 drm_for_each_crtc(crtc, dev)
49 client->modesets[i++].crtc = crtc;
50
51 /* Cloning is only supported in the single crtc case. */
52 if (num_crtc == 1)
53 max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
54
55 for (modeset = client->modesets; modeset->crtc; modeset++) {
56 modeset->connectors = kcalloc(max_connector_count,
57 sizeof(*modeset->connectors), GFP_KERNEL);
58 if (!modeset->connectors)
59 goto err_free;
60 }
61
62 return 0;
63
64err_free:
65 drm_client_modeset_free(client);
66
67 return -ENOMEM;
68}
69
70static void drm_client_modeset_release(struct drm_client_dev *client)
71{
72 struct drm_mode_set *modeset;
73 unsigned int i;
74
75 drm_client_for_each_modeset(modeset, client) {
76 drm_mode_destroy(client->dev, modeset->mode);
77 modeset->mode = NULL;
78 modeset->fb = NULL;
79
80 for (i = 0; i < modeset->num_connectors; i++) {
81 drm_connector_put(modeset->connectors[i]);
82 modeset->connectors[i] = NULL;
83 }
84 modeset->num_connectors = 0;
85 }
86}
87
88void drm_client_modeset_free(struct drm_client_dev *client)
89{
90 struct drm_mode_set *modeset;
91
92 mutex_lock(&client->modeset_mutex);
93
94 drm_client_modeset_release(client);
95
96 drm_client_for_each_modeset(modeset, client)
97 kfree(modeset->connectors);
98
99 mutex_unlock(&client->modeset_mutex);
100
101 mutex_destroy(&client->modeset_mutex);
102 kfree(client->modesets);
103}
104
105static struct drm_mode_set *
106drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
107{
108 struct drm_mode_set *modeset;
109
110 drm_client_for_each_modeset(modeset, client)
111 if (modeset->crtc == crtc)
112 return modeset;
113
114 return NULL;
115}
116
117static struct drm_display_mode *
118drm_connector_has_preferred_mode(struct drm_connector *connector, int width, int height)
119{
120 struct drm_display_mode *mode;
121
122 list_for_each_entry(mode, &connector->modes, head) {
123 if (mode->hdisplay > width ||
124 mode->vdisplay > height)
125 continue;
126 if (mode->type & DRM_MODE_TYPE_PREFERRED)
127 return mode;
128 }
129 return NULL;
130}
131
132static struct drm_display_mode *
133drm_connector_pick_cmdline_mode(struct drm_connector *connector)
134{
135 struct drm_cmdline_mode *cmdline_mode;
136 struct drm_display_mode *mode;
137 bool prefer_non_interlace;
138
139 cmdline_mode = &connector->cmdline_mode;
140 if (cmdline_mode->specified == false)
141 return NULL;
142
143 /* attempt to find a matching mode in the list of modes
144 * we have gotten so far, if not add a CVT mode that conforms
145 */
146 if (cmdline_mode->rb || cmdline_mode->margins)
147 goto create_mode;
148
149 prefer_non_interlace = !cmdline_mode->interlace;
150again:
151 list_for_each_entry(mode, &connector->modes, head) {
152 /* Check (optional) mode name first */
153 if (!strcmp(mode->name, cmdline_mode->name))
154 return mode;
155
156 /* check width/height */
157 if (mode->hdisplay != cmdline_mode->xres ||
158 mode->vdisplay != cmdline_mode->yres)
159 continue;
160
161 if (cmdline_mode->refresh_specified) {
162 if (mode->vrefresh != cmdline_mode->refresh)
163 continue;
164 }
165
166 if (cmdline_mode->interlace) {
167 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
168 continue;
169 } else if (prefer_non_interlace) {
170 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
171 continue;
172 }
173 return mode;
174 }
175
176 if (prefer_non_interlace) {
177 prefer_non_interlace = false;
178 goto again;
179 }
180
181create_mode:
182 mode = drm_mode_create_from_cmdline_mode(connector->dev, cmdline_mode);
183 if (mode)
184 list_add(&mode->head, &connector->modes);
185
186 return mode;
187}
188
189static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
190{
191 bool enable;
192
193 if (connector->display_info.non_desktop)
194 return false;
195
196 if (strict)
197 enable = connector->status == connector_status_connected;
198 else
199 enable = connector->status != connector_status_disconnected;
200
201 return enable;
202}
203
204static void drm_client_connectors_enabled(struct drm_connector **connectors,
205 unsigned int connector_count,
206 bool *enabled)
207{
208 bool any_enabled = false;
209 struct drm_connector *connector;
210 int i = 0;
211
212 for (i = 0; i < connector_count; i++) {
213 connector = connectors[i];
214 enabled[i] = drm_connector_enabled(connector, true);
215 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
216 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
217
218 any_enabled |= enabled[i];
219 }
220
221 if (any_enabled)
222 return;
223
224 for (i = 0; i < connector_count; i++)
225 enabled[i] = drm_connector_enabled(connectors[i], false);
226}
227
228static bool drm_client_target_cloned(struct drm_device *dev,
229 struct drm_connector **connectors,
230 unsigned int connector_count,
231 struct drm_display_mode **modes,
232 struct drm_client_offset *offsets,
233 bool *enabled, int width, int height)
234{
235 int count, i, j;
236 bool can_clone = false;
237 struct drm_display_mode *dmt_mode, *mode;
238
239 /* only contemplate cloning in the single crtc case */
240 if (dev->mode_config.num_crtc > 1)
241 return false;
242
243 count = 0;
244 for (i = 0; i < connector_count; i++) {
245 if (enabled[i])
246 count++;
247 }
248
249 /* only contemplate cloning if more than one connector is enabled */
250 if (count <= 1)
251 return false;
252
253 /* check the command line or if nothing common pick 1024x768 */
254 can_clone = true;
255 for (i = 0; i < connector_count; i++) {
256 if (!enabled[i])
257 continue;
258 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
259 if (!modes[i]) {
260 can_clone = false;
261 break;
262 }
263 for (j = 0; j < i; j++) {
264 if (!enabled[j])
265 continue;
266 if (!drm_mode_match(modes[j], modes[i],
267 DRM_MODE_MATCH_TIMINGS |
268 DRM_MODE_MATCH_CLOCK |
269 DRM_MODE_MATCH_FLAGS |
270 DRM_MODE_MATCH_3D_FLAGS))
271 can_clone = false;
272 }
273 }
274
275 if (can_clone) {
276 DRM_DEBUG_KMS("can clone using command line\n");
277 return true;
278 }
279
280 /* try and find a 1024x768 mode on each connector */
281 can_clone = true;
282 dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
283
284 if (!dmt_mode)
285 goto fail;
286
287 for (i = 0; i < connector_count; i++) {
288 if (!enabled[i])
289 continue;
290
291 list_for_each_entry(mode, &connectors[i]->modes, head) {
292 if (drm_mode_match(mode, dmt_mode,
293 DRM_MODE_MATCH_TIMINGS |
294 DRM_MODE_MATCH_CLOCK |
295 DRM_MODE_MATCH_FLAGS |
296 DRM_MODE_MATCH_3D_FLAGS))
297 modes[i] = mode;
298 }
299 if (!modes[i])
300 can_clone = false;
301 }
302 kfree(dmt_mode);
303
304 if (can_clone) {
305 DRM_DEBUG_KMS("can clone using 1024x768\n");
306 return true;
307 }
308fail:
309 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
310 return false;
311}
312
313static int drm_client_get_tile_offsets(struct drm_connector **connectors,
314 unsigned int connector_count,
315 struct drm_display_mode **modes,
316 struct drm_client_offset *offsets,
317 int idx,
318 int h_idx, int v_idx)
319{
320 struct drm_connector *connector;
321 int i;
322 int hoffset = 0, voffset = 0;
323
324 for (i = 0; i < connector_count; i++) {
325 connector = connectors[i];
326 if (!connector->has_tile)
327 continue;
328
329 if (!modes[i] && (h_idx || v_idx)) {
330 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
331 connector->base.id);
332 continue;
333 }
334 if (connector->tile_h_loc < h_idx)
335 hoffset += modes[i]->hdisplay;
336
337 if (connector->tile_v_loc < v_idx)
338 voffset += modes[i]->vdisplay;
339 }
340 offsets[idx].x = hoffset;
341 offsets[idx].y = voffset;
342 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
343 return 0;
344}
345
346static bool drm_client_target_preferred(struct drm_connector **connectors,
347 unsigned int connector_count,
348 struct drm_display_mode **modes,
349 struct drm_client_offset *offsets,
350 bool *enabled, int width, int height)
351{
352 const u64 mask = BIT_ULL(connector_count) - 1;
353 struct drm_connector *connector;
354 u64 conn_configured = 0;
355 int tile_pass = 0;
356 int i;
357
358retry:
359 for (i = 0; i < connector_count; i++) {
360 connector = connectors[i];
361
362 if (conn_configured & BIT_ULL(i))
363 continue;
364
365 if (enabled[i] == false) {
366 conn_configured |= BIT_ULL(i);
367 continue;
368 }
369
370 /* first pass over all the untiled connectors */
371 if (tile_pass == 0 && connector->has_tile)
372 continue;
373
374 if (tile_pass == 1) {
375 if (connector->tile_h_loc != 0 ||
376 connector->tile_v_loc != 0)
377 continue;
378
379 } else {
380 if (connector->tile_h_loc != tile_pass - 1 &&
381 connector->tile_v_loc != tile_pass - 1)
382 /* if this tile_pass doesn't cover any of the tiles - keep going */
383 continue;
384
385 /*
386 * find the tile offsets for this pass - need to find
387 * all tiles left and above
388 */
389 drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
390 connector->tile_h_loc, connector->tile_v_loc);
391 }
392 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
393 connector->base.id);
394
395 /* got for command line mode first */
396 modes[i] = drm_connector_pick_cmdline_mode(connector);
397 if (!modes[i]) {
398 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
399 connector->base.id, connector->tile_group ? connector->tile_group->id : 0);
400 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
401 }
402 /* No preferred modes, pick one off the list */
403 if (!modes[i] && !list_empty(&connector->modes)) {
404 list_for_each_entry(modes[i], &connector->modes, head)
405 break;
406 }
407 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
408 "none");
409 conn_configured |= BIT_ULL(i);
410 }
411
412 if ((conn_configured & mask) != mask) {
413 tile_pass++;
414 goto retry;
415 }
416 return true;
417}
418
419static bool connector_has_possible_crtc(struct drm_connector *connector,
420 struct drm_crtc *crtc)
421{
422 struct drm_encoder *encoder;
423 int i;
424
425 drm_connector_for_each_possible_encoder(connector, encoder, i) {
426 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
427 return true;
428 }
429
430 return false;
431}
432
433static int drm_client_pick_crtcs(struct drm_client_dev *client,
434 struct drm_connector **connectors,
435 unsigned int connector_count,
436 struct drm_crtc **best_crtcs,
437 struct drm_display_mode **modes,
438 int n, int width, int height)
439{
440 struct drm_device *dev = client->dev;
441 struct drm_connector *connector;
442 int my_score, best_score, score;
443 struct drm_crtc **crtcs, *crtc;
444 struct drm_mode_set *modeset;
445 int o;
446
447 if (n == connector_count)
448 return 0;
449
450 connector = connectors[n];
451
452 best_crtcs[n] = NULL;
453 best_score = drm_client_pick_crtcs(client, connectors, connector_count,
454 best_crtcs, modes, n + 1, width, height);
455 if (modes[n] == NULL)
456 return best_score;
457
458 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
459 if (!crtcs)
460 return best_score;
461
462 my_score = 1;
463 if (connector->status == connector_status_connected)
464 my_score++;
465 if (connector->cmdline_mode.specified)
466 my_score++;
467 if (drm_connector_has_preferred_mode(connector, width, height))
468 my_score++;
469
470 /*
471 * select a crtc for this connector and then attempt to configure
472 * remaining connectors
473 */
474 drm_client_for_each_modeset(modeset, client) {
475 crtc = modeset->crtc;
476
477 if (!connector_has_possible_crtc(connector, crtc))
478 continue;
479
480 for (o = 0; o < n; o++)
481 if (best_crtcs[o] == crtc)
482 break;
483
484 if (o < n) {
485 /* ignore cloning unless only a single crtc */
486 if (dev->mode_config.num_crtc > 1)
487 continue;
488
489 if (!drm_mode_equal(modes[o], modes[n]))
490 continue;
491 }
492
493 crtcs[n] = crtc;
494 memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
495 score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
496 crtcs, modes, n + 1, width, height);
497 if (score > best_score) {
498 best_score = score;
499 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
500 }
501 }
502
503 kfree(crtcs);
504 return best_score;
505}
506
507/* Try to read the BIOS display configuration and use it for the initial config */
508static bool drm_client_firmware_config(struct drm_client_dev *client,
509 struct drm_connector **connectors,
510 unsigned int connector_count,
511 struct drm_crtc **crtcs,
512 struct drm_display_mode **modes,
513 struct drm_client_offset *offsets,
514 bool *enabled, int width, int height)
515{
516 unsigned int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
517 unsigned long conn_configured, conn_seq, mask;
518 struct drm_device *dev = client->dev;
519 int i, j;
520 bool *save_enabled;
521 bool fallback = true, ret = true;
522 int num_connectors_enabled = 0;
523 int num_connectors_detected = 0;
524 struct drm_modeset_acquire_ctx ctx;
525
526 if (!drm_drv_uses_atomic_modeset(dev))
527 return false;
528
529 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
530 if (!save_enabled)
531 return false;
532
533 drm_modeset_acquire_init(&ctx, 0);
534
535 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
536 drm_modeset_backoff(&ctx);
537
538 memcpy(save_enabled, enabled, count);
539 mask = GENMASK(count - 1, 0);
540 conn_configured = 0;
541retry:
542 conn_seq = conn_configured;
543 for (i = 0; i < count; i++) {
544 struct drm_connector *connector;
545 struct drm_encoder *encoder;
546 struct drm_crtc *new_crtc;
547
548 connector = connectors[i];
549
550 if (conn_configured & BIT(i))
551 continue;
552
553 if (conn_seq == 0 && !connector->has_tile)
554 continue;
555
556 if (connector->status == connector_status_connected)
557 num_connectors_detected++;
558
559 if (!enabled[i]) {
560 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
561 connector->name);
562 conn_configured |= BIT(i);
563 continue;
564 }
565
566 if (connector->force == DRM_FORCE_OFF) {
567 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
568 connector->name);
569 enabled[i] = false;
570 continue;
571 }
572
573 encoder = connector->state->best_encoder;
574 if (!encoder || WARN_ON(!connector->state->crtc)) {
575 if (connector->force > DRM_FORCE_OFF)
576 goto bail;
577
578 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
579 connector->name);
580 enabled[i] = false;
581 conn_configured |= BIT(i);
582 continue;
583 }
584
585 num_connectors_enabled++;
586
587 new_crtc = connector->state->crtc;
588
589 /*
590 * Make sure we're not trying to drive multiple connectors
591 * with a single CRTC, since our cloning support may not
592 * match the BIOS.
593 */
594 for (j = 0; j < count; j++) {
595 if (crtcs[j] == new_crtc) {
596 DRM_DEBUG_KMS("fallback: cloned configuration\n");
597 goto bail;
598 }
599 }
600
601 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
602 connector->name);
603
604 /* go for command line mode first */
605 modes[i] = drm_connector_pick_cmdline_mode(connector);
606
607 /* try for preferred next */
608 if (!modes[i]) {
609 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
610 connector->name, connector->has_tile);
611 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
612 }
613
614 /* No preferred mode marked by the EDID? Are there any modes? */
615 if (!modes[i] && !list_empty(&connector->modes)) {
616 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
617 connector->name);
618 modes[i] = list_first_entry(&connector->modes,
619 struct drm_display_mode,
620 head);
621 }
622
623 /* last resort: use current mode */
624 if (!modes[i]) {
625 /*
626 * IMPORTANT: We want to use the adjusted mode (i.e.
627 * after the panel fitter upscaling) as the initial
628 * config, not the input mode, which is what crtc->mode
629 * usually contains. But since our current
630 * code puts a mode derived from the post-pfit timings
631 * into crtc->mode this works out correctly.
632 *
633 * This is crtc->mode and not crtc->state->mode for the
634 * fastboot check to work correctly.
635 */
636 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
637 connector->name);
638 modes[i] = &connector->state->crtc->mode;
639 }
640 crtcs[i] = new_crtc;
641
642 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
643 connector->name,
644 connector->state->crtc->base.id,
645 connector->state->crtc->name,
646 modes[i]->hdisplay, modes[i]->vdisplay,
647 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
648
649 fallback = false;
650 conn_configured |= BIT(i);
651 }
652
653 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
654 goto retry;
655
656 /*
657 * If the BIOS didn't enable everything it could, fall back to have the
658 * same user experiencing of lighting up as much as possible like the
659 * fbdev helper library.
660 */
661 if (num_connectors_enabled != num_connectors_detected &&
662 num_connectors_enabled < dev->mode_config.num_crtc) {
663 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
664 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
665 num_connectors_detected);
666 fallback = true;
667 }
668
669 if (fallback) {
670bail:
671 DRM_DEBUG_KMS("Not using firmware configuration\n");
672 memcpy(enabled, save_enabled, count);
673 ret = false;
674 }
675
676 drm_modeset_drop_locks(&ctx);
677 drm_modeset_acquire_fini(&ctx);
678
679 kfree(save_enabled);
680 return ret;
681}
682
683/**
684 * drm_client_modeset_probe() - Probe for displays
685 * @client: DRM client
686 * @width: Maximum display mode width (optional)
687 * @height: Maximum display mode height (optional)
688 *
689 * This function sets up display pipelines for enabled connectors and stores the
690 * config in the client's modeset array.
691 *
692 * Returns:
693 * Zero on success or negative error code on failure.
694 */
695int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
696{
697 struct drm_connector *connector, **connectors = NULL;
698 struct drm_connector_list_iter conn_iter;
699 struct drm_device *dev = client->dev;
700 unsigned int total_modes_count = 0;
701 struct drm_client_offset *offsets;
702 unsigned int connector_count = 0;
703 /* points to modes protected by mode_config.mutex */
704 struct drm_display_mode **modes;
705 struct drm_crtc **crtcs;
706 int i, ret = 0;
707 bool *enabled;
708
709 DRM_DEBUG_KMS("\n");
710
711 if (!width)
712 width = dev->mode_config.max_width;
713 if (!height)
714 height = dev->mode_config.max_height;
715
716 drm_connector_list_iter_begin(dev, &conn_iter);
717 drm_client_for_each_connector_iter(connector, &conn_iter) {
718 struct drm_connector **tmp;
719
720 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
721 if (!tmp) {
722 ret = -ENOMEM;
723 goto free_connectors;
724 }
725
726 connectors = tmp;
727 drm_connector_get(connector);
728 connectors[connector_count++] = connector;
729 }
730 drm_connector_list_iter_end(&conn_iter);
731
732 if (!connector_count)
733 return 0;
734
735 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
736 modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
737 offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
738 enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
739 if (!crtcs || !modes || !enabled || !offsets) {
740 DRM_ERROR("Memory allocation failed\n");
741 ret = -ENOMEM;
742 goto out;
743 }
744
745 mutex_lock(&client->modeset_mutex);
746
747 mutex_lock(&dev->mode_config.mutex);
748 for (i = 0; i < connector_count; i++)
749 total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
750 if (!total_modes_count)
751 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
752 drm_client_connectors_enabled(connectors, connector_count, enabled);
753
754 if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
755 modes, offsets, enabled, width, height)) {
756 memset(modes, 0, connector_count * sizeof(*modes));
757 memset(crtcs, 0, connector_count * sizeof(*crtcs));
758 memset(offsets, 0, connector_count * sizeof(*offsets));
759
760 if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
761 offsets, enabled, width, height) &&
762 !drm_client_target_preferred(connectors, connector_count, modes,
763 offsets, enabled, width, height))
764 DRM_ERROR("Unable to find initial modes\n");
765
766 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
767 width, height);
768
769 drm_client_pick_crtcs(client, connectors, connector_count,
770 crtcs, modes, 0, width, height);
771 }
772
773 drm_client_modeset_release(client);
774
775 for (i = 0; i < connector_count; i++) {
776 struct drm_display_mode *mode = modes[i];
777 struct drm_crtc *crtc = crtcs[i];
778 struct drm_client_offset *offset = &offsets[i];
779
780 if (mode && crtc) {
781 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
782 struct drm_connector *connector = connectors[i];
783
784 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
785 mode->name, crtc->base.id, offset->x, offset->y);
786
787 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
788 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
789 ret = -EINVAL;
790 break;
791 }
792
793 kfree(modeset->mode);
794 modeset->mode = drm_mode_duplicate(dev, mode);
795 if (!modeset->mode) {
796 ret = -ENOMEM;
797 break;
798 }
799
800 drm_connector_get(connector);
801 modeset->connectors[modeset->num_connectors++] = connector;
802 modeset->x = offset->x;
803 modeset->y = offset->y;
804 }
805 }
806 mutex_unlock(&dev->mode_config.mutex);
807
808 mutex_unlock(&client->modeset_mutex);
809out:
810 kfree(crtcs);
811 kfree(modes);
812 kfree(offsets);
813 kfree(enabled);
814free_connectors:
815 for (i = 0; i < connector_count; i++)
816 drm_connector_put(connectors[i]);
817 kfree(connectors);
818
819 return ret;
820}
821EXPORT_SYMBOL(drm_client_modeset_probe);
822
823/**
824 * drm_client_rotation() - Check the initial rotation value
825 * @modeset: DRM modeset
826 * @rotation: Returned rotation value
827 *
828 * This function checks if the primary plane in @modeset can hw rotate
829 * to match the rotation needed on its connector.
830 *
831 * Note: Currently only 0 and 180 degrees are supported.
832 *
833 * Return:
834 * True if the plane can do the rotation, false otherwise.
835 */
836bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
837{
838 struct drm_connector *connector = modeset->connectors[0];
839 struct drm_plane *plane = modeset->crtc->primary;
840 struct drm_cmdline_mode *cmdline;
841 u64 valid_mask = 0;
842 unsigned int i;
843
844 if (!modeset->num_connectors)
845 return false;
846
847 switch (connector->display_info.panel_orientation) {
848 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
849 *rotation = DRM_MODE_ROTATE_180;
850 break;
851 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
852 *rotation = DRM_MODE_ROTATE_90;
853 break;
854 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
855 *rotation = DRM_MODE_ROTATE_270;
856 break;
857 default:
858 *rotation = DRM_MODE_ROTATE_0;
859 }
860
861 /**
862 * The panel already defined the default rotation
863 * through its orientation. Whatever has been provided
864 * on the command line needs to be added to that.
865 *
866 * Unfortunately, the rotations are at different bit
867 * indices, so the math to add them up are not as
868 * trivial as they could.
869 *
870 * Reflections on the other hand are pretty trivial to deal with, a
871 * simple XOR between the two handle the addition nicely.
872 */
873 cmdline = &connector->cmdline_mode;
874 if (cmdline->specified && cmdline->rotation_reflection) {
875 unsigned int cmdline_rest, panel_rest;
876 unsigned int cmdline_rot, panel_rot;
877 unsigned int sum_rot, sum_rest;
878
879 panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
880 cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
881 sum_rot = (panel_rot + cmdline_rot) % 4;
882
883 panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
884 cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
885 sum_rest = panel_rest ^ cmdline_rest;
886
887 *rotation = (1 << sum_rot) | sum_rest;
888 }
889
890 /*
891 * TODO: support 90 / 270 degree hardware rotation,
892 * depending on the hardware this may require the framebuffer
893 * to be in a specific tiling format.
894 */
895 if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
896 (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
897 !plane->rotation_property)
898 return false;
899
900 for (i = 0; i < plane->rotation_property->num_values; i++)
901 valid_mask |= (1ULL << plane->rotation_property->values[i]);
902
903 if (!(*rotation & valid_mask))
904 return false;
905
906 return true;
907}
908EXPORT_SYMBOL(drm_client_rotation);
909
910static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active)
911{
912 struct drm_device *dev = client->dev;
913 struct drm_plane *plane;
914 struct drm_atomic_state *state;
915 struct drm_modeset_acquire_ctx ctx;
916 struct drm_mode_set *mode_set;
917 int ret;
918
919 drm_modeset_acquire_init(&ctx, 0);
920
921 state = drm_atomic_state_alloc(dev);
922 if (!state) {
923 ret = -ENOMEM;
924 goto out_ctx;
925 }
926
927 state->acquire_ctx = &ctx;
928retry:
929 drm_for_each_plane(plane, dev) {
930 struct drm_plane_state *plane_state;
931
932 plane_state = drm_atomic_get_plane_state(state, plane);
933 if (IS_ERR(plane_state)) {
934 ret = PTR_ERR(plane_state);
935 goto out_state;
936 }
937
938 plane_state->rotation = DRM_MODE_ROTATE_0;
939
940 /* disable non-primary: */
941 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
942 continue;
943
944 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
945 if (ret != 0)
946 goto out_state;
947 }
948
949 drm_client_for_each_modeset(mode_set, client) {
950 struct drm_plane *primary = mode_set->crtc->primary;
951 unsigned int rotation;
952
953 if (drm_client_rotation(mode_set, &rotation)) {
954 struct drm_plane_state *plane_state;
955
956 /* Cannot fail as we've already gotten the plane state above */
957 plane_state = drm_atomic_get_new_plane_state(state, primary);
958 plane_state->rotation = rotation;
959 }
960
961 ret = __drm_atomic_helper_set_config(mode_set, state);
962 if (ret != 0)
963 goto out_state;
964
965 /*
966 * __drm_atomic_helper_set_config() sets active when a
967 * mode is set, unconditionally clear it if we force DPMS off
968 */
969 if (!active) {
970 struct drm_crtc *crtc = mode_set->crtc;
971 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
972
973 crtc_state->active = false;
974 }
975 }
976
977 ret = drm_atomic_commit(state);
978
979out_state:
980 if (ret == -EDEADLK)
981 goto backoff;
982
983 drm_atomic_state_put(state);
984out_ctx:
985 drm_modeset_drop_locks(&ctx);
986 drm_modeset_acquire_fini(&ctx);
987
988 return ret;
989
990backoff:
991 drm_atomic_state_clear(state);
992 drm_modeset_backoff(&ctx);
993
994 goto retry;
995}
996
997static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
998{
999 struct drm_device *dev = client->dev;
1000 struct drm_mode_set *mode_set;
1001 struct drm_plane *plane;
1002 int ret = 0;
1003
1004 drm_modeset_lock_all(dev);
1005 drm_for_each_plane(plane, dev) {
1006 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
1007 drm_plane_force_disable(plane);
1008
1009 if (plane->rotation_property)
1010 drm_mode_plane_set_obj_prop(plane,
1011 plane->rotation_property,
1012 DRM_MODE_ROTATE_0);
1013 }
1014
1015 drm_client_for_each_modeset(mode_set, client) {
1016 struct drm_crtc *crtc = mode_set->crtc;
1017
1018 if (crtc->funcs->cursor_set2) {
1019 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1020 if (ret)
1021 goto out;
1022 } else if (crtc->funcs->cursor_set) {
1023 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1024 if (ret)
1025 goto out;
1026 }
1027
1028 ret = drm_mode_set_config_internal(mode_set);
1029 if (ret)
1030 goto out;
1031 }
1032out:
1033 drm_modeset_unlock_all(dev);
1034
1035 return ret;
1036}
1037
1038/**
1039 * drm_client_modeset_commit_force() - Force commit CRTC configuration
1040 * @client: DRM client
1041 *
1042 * Commit modeset configuration to crtcs without checking if there is a DRM master.
1043 *
1044 * Returns:
1045 * Zero on success or negative error code on failure.
1046 */
1047int drm_client_modeset_commit_force(struct drm_client_dev *client)
1048{
1049 struct drm_device *dev = client->dev;
1050 int ret;
1051
1052 mutex_lock(&client->modeset_mutex);
1053 if (drm_drv_uses_atomic_modeset(dev))
1054 ret = drm_client_modeset_commit_atomic(client, true);
1055 else
1056 ret = drm_client_modeset_commit_legacy(client);
1057 mutex_unlock(&client->modeset_mutex);
1058
1059 return ret;
1060}
1061EXPORT_SYMBOL(drm_client_modeset_commit_force);
1062
1063/**
1064 * drm_client_modeset_commit() - Commit CRTC configuration
1065 * @client: DRM client
1066 *
1067 * Commit modeset configuration to crtcs.
1068 *
1069 * Returns:
1070 * Zero on success or negative error code on failure.
1071 */
1072int drm_client_modeset_commit(struct drm_client_dev *client)
1073{
1074 struct drm_device *dev = client->dev;
1075 int ret;
1076
1077 if (!drm_master_internal_acquire(dev))
1078 return -EBUSY;
1079
1080 ret = drm_client_modeset_commit_force(client);
1081
1082 drm_master_internal_release(dev);
1083
1084 return ret;
1085}
1086EXPORT_SYMBOL(drm_client_modeset_commit);
1087
1088static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1089{
1090 struct drm_device *dev = client->dev;
1091 struct drm_connector *connector;
1092 struct drm_mode_set *modeset;
1093 int j;
1094
1095 drm_modeset_lock_all(dev);
1096 drm_client_for_each_modeset(modeset, client) {
1097 if (!modeset->crtc->enabled)
1098 continue;
1099
1100 for (j = 0; j < modeset->num_connectors; j++) {
1101 connector = modeset->connectors[j];
1102 connector->funcs->dpms(connector, dpms_mode);
1103 drm_object_property_set_value(&connector->base,
1104 dev->mode_config.dpms_property, dpms_mode);
1105 }
1106 }
1107 drm_modeset_unlock_all(dev);
1108}
1109
1110/**
1111 * drm_client_modeset_dpms() - Set DPMS mode
1112 * @client: DRM client
1113 * @mode: DPMS mode
1114 *
1115 * Note: For atomic drivers @mode is reduced to on/off.
1116 *
1117 * Returns:
1118 * Zero on success or negative error code on failure.
1119 */
1120int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1121{
1122 struct drm_device *dev = client->dev;
1123 int ret = 0;
1124
1125 if (!drm_master_internal_acquire(dev))
1126 return -EBUSY;
1127
1128 mutex_lock(&client->modeset_mutex);
1129 if (drm_drv_uses_atomic_modeset(dev))
1130 ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON);
1131 else
1132 drm_client_modeset_dpms_legacy(client, mode);
1133 mutex_unlock(&client->modeset_mutex);
1134
1135 drm_master_internal_release(dev);
1136
1137 return ret;
1138}
1139EXPORT_SYMBOL(drm_client_modeset_dpms);