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