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