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