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