1 /* $NetBSD: intel_atomic.c,v 1.5 2021/12/19 12:24:36 riastradh Exp $ */ 2 3 /* 4 * Copyright © 2015 Intel Corporation 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 */ 25 26 /** 27 * DOC: atomic modeset support 28 * 29 * The functions here implement the state management and hardware programming 30 * dispatch required by the atomic modeset infrastructure. 31 * See intel_atomic_plane.c for the plane-specific atomic functionality. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: intel_atomic.c,v 1.5 2021/12/19 12:24:36 riastradh Exp $"); 36 37 #include <drm/drm_atomic.h> 38 #include <drm/drm_atomic_helper.h> 39 #include <drm/drm_fourcc.h> 40 #include <drm/drm_plane_helper.h> 41 42 #include "intel_atomic.h" 43 #include "intel_display_types.h" 44 #include "intel_hdcp.h" 45 #include "intel_psr.h" 46 #include "intel_sprite.h" 47 48 /** 49 * intel_digital_connector_atomic_get_property - hook for connector->atomic_get_property. 50 * @connector: Connector to get the property for. 51 * @state: Connector state to retrieve the property from. 52 * @property: Property to retrieve. 53 * @val: Return value for the property. 54 * 55 * Returns the atomic property value for a digital connector. 56 */ 57 int intel_digital_connector_atomic_get_property(struct drm_connector *connector, 58 const struct drm_connector_state *state, 59 struct drm_property *property, 60 u64 *val) 61 { 62 struct drm_device *dev = connector->dev; 63 struct drm_i915_private *dev_priv = to_i915(dev); 64 const struct intel_digital_connector_state *intel_conn_state = 65 const_container_of(state, struct intel_digital_connector_state, base); 66 67 if (property == dev_priv->force_audio_property) 68 *val = intel_conn_state->force_audio; 69 else if (property == dev_priv->broadcast_rgb_property) 70 *val = intel_conn_state->broadcast_rgb; 71 else { 72 DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n", 73 property->base.id, property->name); 74 return -EINVAL; 75 } 76 77 return 0; 78 } 79 80 /** 81 * intel_digital_connector_atomic_set_property - hook for connector->atomic_set_property. 82 * @connector: Connector to set the property for. 83 * @state: Connector state to set the property on. 84 * @property: Property to set. 85 * @val: New value for the property. 86 * 87 * Sets the atomic property value for a digital connector. 88 */ 89 int intel_digital_connector_atomic_set_property(struct drm_connector *connector, 90 struct drm_connector_state *state, 91 struct drm_property *property, 92 u64 val) 93 { 94 struct drm_device *dev = connector->dev; 95 struct drm_i915_private *dev_priv = to_i915(dev); 96 struct intel_digital_connector_state *intel_conn_state = 97 to_intel_digital_connector_state(state); 98 99 if (property == dev_priv->force_audio_property) { 100 intel_conn_state->force_audio = val; 101 return 0; 102 } 103 104 if (property == dev_priv->broadcast_rgb_property) { 105 intel_conn_state->broadcast_rgb = val; 106 return 0; 107 } 108 109 DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n", 110 property->base.id, property->name); 111 return -EINVAL; 112 } 113 114 static bool blob_equal(const struct drm_property_blob *a, 115 const struct drm_property_blob *b) 116 { 117 if (a && b) 118 return a->length == b->length && 119 !memcmp(a->data, b->data, a->length); 120 121 return !a == !b; 122 } 123 124 int intel_digital_connector_atomic_check(struct drm_connector *conn, 125 struct drm_atomic_state *state) 126 { 127 struct drm_connector_state *new_state = 128 drm_atomic_get_new_connector_state(state, conn); 129 struct intel_digital_connector_state *new_conn_state = 130 to_intel_digital_connector_state(new_state); 131 struct drm_connector_state *old_state = 132 drm_atomic_get_old_connector_state(state, conn); 133 struct intel_digital_connector_state *old_conn_state = 134 to_intel_digital_connector_state(old_state); 135 struct drm_crtc_state *crtc_state; 136 137 intel_hdcp_atomic_check(conn, old_state, new_state); 138 intel_psr_atomic_check(conn, old_state, new_state); 139 140 if (!new_state->crtc) 141 return 0; 142 143 crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc); 144 145 /* 146 * These properties are handled by fastset, and might not end 147 * up in a modeset. 148 */ 149 if (new_conn_state->force_audio != old_conn_state->force_audio || 150 new_conn_state->broadcast_rgb != old_conn_state->broadcast_rgb || 151 new_conn_state->base.colorspace != old_conn_state->base.colorspace || 152 new_conn_state->base.picture_aspect_ratio != old_conn_state->base.picture_aspect_ratio || 153 new_conn_state->base.content_type != old_conn_state->base.content_type || 154 new_conn_state->base.scaling_mode != old_conn_state->base.scaling_mode || 155 !blob_equal(new_conn_state->base.hdr_output_metadata, 156 old_conn_state->base.hdr_output_metadata)) 157 crtc_state->mode_changed = true; 158 159 return 0; 160 } 161 162 /** 163 * intel_digital_connector_duplicate_state - duplicate connector state 164 * @connector: digital connector 165 * 166 * Allocates and returns a copy of the connector state (both common and 167 * digital connector specific) for the specified connector. 168 * 169 * Returns: The newly allocated connector state, or NULL on failure. 170 */ 171 struct drm_connector_state * 172 intel_digital_connector_duplicate_state(struct drm_connector *connector) 173 { 174 struct intel_digital_connector_state *state; 175 176 state = kmemdup(connector->state, sizeof(*state), GFP_KERNEL); 177 if (!state) 178 return NULL; 179 180 __drm_atomic_helper_connector_duplicate_state(connector, &state->base); 181 return &state->base; 182 } 183 184 /** 185 * intel_connector_needs_modeset - check if connector needs a modeset 186 */ 187 bool 188 intel_connector_needs_modeset(struct intel_atomic_state *state, 189 struct drm_connector *connector) 190 { 191 const struct drm_connector_state *old_conn_state, *new_conn_state; 192 193 old_conn_state = drm_atomic_get_old_connector_state(&state->base, connector); 194 new_conn_state = drm_atomic_get_new_connector_state(&state->base, connector); 195 196 return old_conn_state->crtc != new_conn_state->crtc || 197 (new_conn_state->crtc && 198 drm_atomic_crtc_needs_modeset(drm_atomic_get_new_crtc_state(&state->base, 199 new_conn_state->crtc))); 200 } 201 202 struct intel_digital_connector_state * 203 intel_atomic_get_digital_connector_state(struct intel_atomic_state *state, 204 struct intel_connector *connector) 205 { 206 struct drm_connector_state *conn_state; 207 208 conn_state = drm_atomic_get_connector_state(&state->base, 209 &connector->base); 210 if (IS_ERR(conn_state)) 211 return ERR_CAST(conn_state); 212 213 return to_intel_digital_connector_state(conn_state); 214 } 215 216 /** 217 * intel_crtc_duplicate_state - duplicate crtc state 218 * @crtc: drm crtc 219 * 220 * Allocates and returns a copy of the crtc state (both common and 221 * Intel-specific) for the specified crtc. 222 * 223 * Returns: The newly allocated crtc state, or NULL on failure. 224 */ 225 struct drm_crtc_state * 226 intel_crtc_duplicate_state(struct drm_crtc *crtc) 227 { 228 const struct intel_crtc_state *old_crtc_state = to_intel_crtc_state(crtc->state); 229 struct intel_crtc_state *crtc_state; 230 231 crtc_state = kmemdup(old_crtc_state, sizeof(*crtc_state), GFP_KERNEL); 232 if (!crtc_state) 233 return NULL; 234 235 __drm_atomic_helper_crtc_duplicate_state(crtc, &crtc_state->uapi); 236 237 /* copy color blobs */ 238 if (crtc_state->hw.degamma_lut) 239 drm_property_blob_get(crtc_state->hw.degamma_lut); 240 if (crtc_state->hw.ctm) 241 drm_property_blob_get(crtc_state->hw.ctm); 242 if (crtc_state->hw.gamma_lut) 243 drm_property_blob_get(crtc_state->hw.gamma_lut); 244 245 crtc_state->update_pipe = false; 246 crtc_state->disable_lp_wm = false; 247 crtc_state->disable_cxsr = false; 248 crtc_state->update_wm_pre = false; 249 crtc_state->update_wm_post = false; 250 crtc_state->fifo_changed = false; 251 crtc_state->preload_luts = false; 252 crtc_state->wm.need_postvbl_update = false; 253 crtc_state->fb_bits = 0; 254 crtc_state->update_planes = 0; 255 256 return &crtc_state->uapi; 257 } 258 259 static void intel_crtc_put_color_blobs(struct intel_crtc_state *crtc_state) 260 { 261 drm_property_blob_put(crtc_state->hw.degamma_lut); 262 drm_property_blob_put(crtc_state->hw.gamma_lut); 263 drm_property_blob_put(crtc_state->hw.ctm); 264 } 265 266 void intel_crtc_free_hw_state(struct intel_crtc_state *crtc_state) 267 { 268 intel_crtc_put_color_blobs(crtc_state); 269 } 270 271 void intel_crtc_copy_color_blobs(struct intel_crtc_state *crtc_state) 272 { 273 drm_property_replace_blob(&crtc_state->hw.degamma_lut, 274 crtc_state->uapi.degamma_lut); 275 drm_property_replace_blob(&crtc_state->hw.gamma_lut, 276 crtc_state->uapi.gamma_lut); 277 drm_property_replace_blob(&crtc_state->hw.ctm, 278 crtc_state->uapi.ctm); 279 } 280 281 /** 282 * intel_crtc_destroy_state - destroy crtc state 283 * @crtc: drm crtc 284 * @state: the state to destroy 285 * 286 * Destroys the crtc state (both common and Intel-specific) for the 287 * specified crtc. 288 */ 289 void 290 intel_crtc_destroy_state(struct drm_crtc *crtc, 291 struct drm_crtc_state *state) 292 { 293 struct intel_crtc_state *crtc_state = to_intel_crtc_state(state); 294 295 __drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi); 296 intel_crtc_free_hw_state(crtc_state); 297 kfree(crtc_state); 298 } 299 300 static void intel_atomic_setup_scaler(struct intel_crtc_scaler_state *scaler_state, 301 int num_scalers_need, struct intel_crtc *intel_crtc, 302 const char *name, int idx, 303 struct intel_plane_state *plane_state, 304 int *scaler_id) 305 { 306 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev); 307 int j; 308 u32 mode; 309 310 if (*scaler_id < 0) { 311 /* find a free scaler */ 312 for (j = 0; j < intel_crtc->num_scalers; j++) { 313 if (scaler_state->scalers[j].in_use) 314 continue; 315 316 *scaler_id = j; 317 scaler_state->scalers[*scaler_id].in_use = 1; 318 break; 319 } 320 } 321 322 if (WARN(*scaler_id < 0, "Cannot find scaler for %s:%d\n", name, idx)) 323 return; 324 325 /* set scaler mode */ 326 if (plane_state && plane_state->hw.fb && 327 plane_state->hw.fb->format->is_yuv && 328 plane_state->hw.fb->format->num_planes > 1) { 329 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane); 330 if (IS_GEN(dev_priv, 9) && 331 !IS_GEMINILAKE(dev_priv)) { 332 mode = SKL_PS_SCALER_MODE_NV12; 333 } else if (icl_is_hdr_plane(dev_priv, plane->id)) { 334 /* 335 * On gen11+'s HDR planes we only use the scaler for 336 * scaling. They have a dedicated chroma upsampler, so 337 * we don't need the scaler to upsample the UV plane. 338 */ 339 mode = PS_SCALER_MODE_NORMAL; 340 } else { 341 struct intel_plane *linked = 342 plane_state->planar_linked_plane; 343 344 mode = PS_SCALER_MODE_PLANAR; 345 346 if (linked) 347 mode |= PS_PLANE_Y_SEL(linked->id); 348 } 349 } else if (INTEL_GEN(dev_priv) > 9 || IS_GEMINILAKE(dev_priv)) { 350 mode = PS_SCALER_MODE_NORMAL; 351 } else if (num_scalers_need == 1 && intel_crtc->num_scalers > 1) { 352 /* 353 * when only 1 scaler is in use on a pipe with 2 scalers 354 * scaler 0 operates in high quality (HQ) mode. 355 * In this case use scaler 0 to take advantage of HQ mode 356 */ 357 scaler_state->scalers[*scaler_id].in_use = 0; 358 *scaler_id = 0; 359 scaler_state->scalers[0].in_use = 1; 360 mode = SKL_PS_SCALER_MODE_HQ; 361 } else { 362 mode = SKL_PS_SCALER_MODE_DYN; 363 } 364 365 DRM_DEBUG_KMS("Attached scaler id %u.%u to %s:%d\n", 366 intel_crtc->pipe, *scaler_id, name, idx); 367 scaler_state->scalers[*scaler_id].mode = mode; 368 } 369 370 /** 371 * intel_atomic_setup_scalers() - setup scalers for crtc per staged requests 372 * @dev_priv: i915 device 373 * @intel_crtc: intel crtc 374 * @crtc_state: incoming crtc_state to validate and setup scalers 375 * 376 * This function sets up scalers based on staged scaling requests for 377 * a @crtc and its planes. It is called from crtc level check path. If request 378 * is a supportable request, it attaches scalers to requested planes and crtc. 379 * 380 * This function takes into account the current scaler(s) in use by any planes 381 * not being part of this atomic state 382 * 383 * Returns: 384 * 0 - scalers were setup succesfully 385 * error code - otherwise 386 */ 387 int intel_atomic_setup_scalers(struct drm_i915_private *dev_priv, 388 struct intel_crtc *intel_crtc, 389 struct intel_crtc_state *crtc_state) 390 { 391 struct drm_plane *plane = NULL; 392 struct intel_plane *intel_plane; 393 struct intel_plane_state *plane_state = NULL; 394 struct intel_crtc_scaler_state *scaler_state = 395 &crtc_state->scaler_state; 396 struct drm_atomic_state *drm_state = crtc_state->uapi.state; 397 struct intel_atomic_state *intel_state = to_intel_atomic_state(drm_state); 398 int num_scalers_need; 399 int i; 400 401 num_scalers_need = hweight32(scaler_state->scaler_users); 402 403 /* 404 * High level flow: 405 * - staged scaler requests are already in scaler_state->scaler_users 406 * - check whether staged scaling requests can be supported 407 * - add planes using scalers that aren't in current transaction 408 * - assign scalers to requested users 409 * - as part of plane commit, scalers will be committed 410 * (i.e., either attached or detached) to respective planes in hw 411 * - as part of crtc_commit, scaler will be either attached or detached 412 * to crtc in hw 413 */ 414 415 /* fail if required scalers > available scalers */ 416 if (num_scalers_need > intel_crtc->num_scalers){ 417 DRM_DEBUG_KMS("Too many scaling requests %d > %d\n", 418 num_scalers_need, intel_crtc->num_scalers); 419 return -EINVAL; 420 } 421 422 /* walkthrough scaler_users bits and start assigning scalers */ 423 for (i = 0; i < sizeof(scaler_state->scaler_users) * 8; i++) { 424 int *scaler_id; 425 const char *name; 426 int idx; 427 428 /* skip if scaler not required */ 429 if (!(scaler_state->scaler_users & (1 << i))) 430 continue; 431 432 if (i == SKL_CRTC_INDEX) { 433 name = "CRTC"; 434 idx = intel_crtc->base.base.id; 435 436 /* panel fitter case: assign as a crtc scaler */ 437 scaler_id = &scaler_state->scaler_id; 438 } else { 439 name = "PLANE"; 440 441 /* plane scaler case: assign as a plane scaler */ 442 /* find the plane that set the bit as scaler_user */ 443 plane = drm_state->planes[i].ptr; 444 445 /* 446 * to enable/disable hq mode, add planes that are using scaler 447 * into this transaction 448 */ 449 if (!plane) { 450 struct drm_plane_state *state; 451 452 /* 453 * GLK+ scalers don't have a HQ mode so it 454 * isn't necessary to change between HQ and dyn mode 455 * on those platforms. 456 */ 457 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) 458 continue; 459 460 plane = drm_plane_from_index(&dev_priv->drm, i); 461 state = drm_atomic_get_plane_state(drm_state, plane); 462 if (IS_ERR(state)) { 463 DRM_DEBUG_KMS("Failed to add [PLANE:%d] to drm_state\n", 464 plane->base.id); 465 return PTR_ERR(state); 466 } 467 } 468 469 intel_plane = to_intel_plane(plane); 470 idx = plane->base.id; 471 472 /* plane on different crtc cannot be a scaler user of this crtc */ 473 if (WARN_ON(intel_plane->pipe != intel_crtc->pipe)) 474 continue; 475 476 plane_state = intel_atomic_get_new_plane_state(intel_state, 477 intel_plane); 478 scaler_id = &plane_state->scaler_id; 479 } 480 481 intel_atomic_setup_scaler(scaler_state, num_scalers_need, 482 intel_crtc, name, idx, 483 plane_state, scaler_id); 484 } 485 486 return 0; 487 } 488 489 struct drm_atomic_state * 490 intel_atomic_state_alloc(struct drm_device *dev) 491 { 492 struct intel_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL); 493 494 if (!state || drm_atomic_state_init(dev, &state->base) < 0) { 495 kfree(state); 496 return NULL; 497 } 498 499 i915_sw_fence_init(&state->commit_ready, intel_atomic_commit_ready); 500 501 return &state->base; 502 } 503 504 void intel_atomic_state_clear(struct drm_atomic_state *s) 505 { 506 struct intel_atomic_state *state = to_intel_atomic_state(s); 507 drm_atomic_state_default_clear(&state->base); 508 state->dpll_set = state->modeset = false; 509 state->global_state_changed = false; 510 state->active_pipes = 0; 511 memset(&state->min_cdclk, 0, sizeof(state->min_cdclk)); 512 memset(&state->min_voltage_level, 0, sizeof(state->min_voltage_level)); 513 memset(&state->cdclk.logical, 0, sizeof(state->cdclk.logical)); 514 memset(&state->cdclk.actual, 0, sizeof(state->cdclk.actual)); 515 state->cdclk.pipe = INVALID_PIPE; 516 } 517 518 struct intel_crtc_state * 519 intel_atomic_get_crtc_state(struct drm_atomic_state *state, 520 struct intel_crtc *crtc) 521 { 522 struct drm_crtc_state *crtc_state; 523 crtc_state = drm_atomic_get_crtc_state(state, &crtc->base); 524 if (IS_ERR(crtc_state)) 525 return ERR_CAST(crtc_state); 526 527 return to_intel_crtc_state(crtc_state); 528 } 529 530 int intel_atomic_lock_global_state(struct intel_atomic_state *state) 531 { 532 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 533 struct intel_crtc *crtc; 534 535 state->global_state_changed = true; 536 537 for_each_intel_crtc(&dev_priv->drm, crtc) { 538 int ret; 539 540 ret = drm_modeset_lock(&crtc->base.mutex, 541 state->base.acquire_ctx); 542 if (ret) 543 return ret; 544 } 545 546 return 0; 547 } 548 549 int intel_atomic_serialize_global_state(struct intel_atomic_state *state) 550 { 551 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 552 struct intel_crtc *crtc; 553 554 state->global_state_changed = true; 555 556 for_each_intel_crtc(&dev_priv->drm, crtc) { 557 struct intel_crtc_state *crtc_state; 558 559 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc); 560 if (IS_ERR(crtc_state)) 561 return PTR_ERR(crtc_state); 562 } 563 564 return 0; 565 } 566