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