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