1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2015 Broadcom 4 */ 5 6 /** 7 * DOC: VC4 KMS 8 * 9 * This is the general code for implementing KMS mode setting that 10 * doesn't clearly associate with any of the other objects (plane, 11 * crtc, HDMI encoder). 12 */ 13 14 #include <linux/clk.h> 15 #include <linux/sort.h> 16 17 #include <drm/drm_atomic.h> 18 #include <drm/drm_atomic_helper.h> 19 #include <drm/drm_crtc.h> 20 #include <drm/drm_fourcc.h> 21 #include <drm/drm_gem_framebuffer_helper.h> 22 #include <drm/drm_print.h> 23 #include <drm/drm_probe_helper.h> 24 #include <drm/drm_vblank.h> 25 26 #include "vc4_drv.h" 27 #include "vc4_regs.h" 28 29 struct vc4_ctm_state { 30 struct drm_private_state base; 31 struct drm_color_ctm *ctm; 32 int fifo; 33 }; 34 35 #define to_vc4_ctm_state(_state) \ 36 container_of_const(_state, struct vc4_ctm_state, base) 37 38 struct vc4_load_tracker_state { 39 struct drm_private_state base; 40 u64 hvs_load; 41 u64 membus_load; 42 }; 43 44 #define to_vc4_load_tracker_state(_state) \ 45 container_of_const(_state, struct vc4_load_tracker_state, base) 46 47 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state, 48 struct drm_private_obj *manager) 49 { 50 struct drm_device *dev = state->dev; 51 struct vc4_dev *vc4 = to_vc4_dev(dev); 52 struct drm_private_state *priv_state; 53 int ret; 54 55 ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx); 56 if (ret) 57 return ERR_PTR(ret); 58 59 priv_state = drm_atomic_get_private_obj_state(state, manager); 60 if (IS_ERR(priv_state)) 61 return ERR_CAST(priv_state); 62 63 return to_vc4_ctm_state(priv_state); 64 } 65 66 static struct drm_private_state * 67 vc4_ctm_duplicate_state(struct drm_private_obj *obj) 68 { 69 struct vc4_ctm_state *state; 70 71 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 72 if (!state) 73 return NULL; 74 75 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 76 77 return &state->base; 78 } 79 80 static void vc4_ctm_destroy_state(struct drm_private_obj *obj, 81 struct drm_private_state *state) 82 { 83 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state); 84 85 kfree(ctm_state); 86 } 87 88 static struct drm_private_state * 89 vc4_ctm_create_state(struct drm_private_obj *obj) 90 { 91 struct vc4_ctm_state *ctm_state; 92 93 ctm_state = kzalloc_obj(*ctm_state); 94 if (!ctm_state) 95 return ERR_PTR(-ENOMEM); 96 97 __drm_atomic_helper_private_obj_create_state(obj, &ctm_state->base); 98 99 return &ctm_state->base; 100 } 101 102 static const struct drm_private_state_funcs vc4_ctm_state_funcs = { 103 .atomic_create_state = vc4_ctm_create_state, 104 .atomic_duplicate_state = vc4_ctm_duplicate_state, 105 .atomic_destroy_state = vc4_ctm_destroy_state, 106 }; 107 108 static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused) 109 { 110 struct vc4_dev *vc4 = to_vc4_dev(dev); 111 112 drm_atomic_private_obj_fini(&vc4->ctm_manager); 113 } 114 115 static int vc4_ctm_obj_init(struct vc4_dev *vc4) 116 { 117 drm_modeset_lock_init(&vc4->ctm_state_lock); 118 119 drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, 120 &vc4_ctm_state_funcs); 121 122 return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL); 123 } 124 125 /* Converts a DRM S31.32 value to the HW S0.9 format. */ 126 static u16 vc4_ctm_s31_32_to_s0_9(u64 in) 127 { 128 u16 r; 129 130 /* Sign bit. */ 131 r = in & BIT_ULL(63) ? BIT(9) : 0; 132 133 if ((in & GENMASK_ULL(62, 32)) > 0) { 134 /* We have zero integer bits so we can only saturate here. */ 135 r |= GENMASK(8, 0); 136 } else { 137 /* Otherwise take the 9 most important fractional bits. */ 138 r |= (in >> 23) & GENMASK(8, 0); 139 } 140 141 return r; 142 } 143 144 static void 145 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state) 146 { 147 struct vc4_hvs *hvs = vc4->hvs; 148 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state); 149 struct drm_color_ctm *ctm = ctm_state->ctm; 150 151 WARN_ON_ONCE(vc4->gen > VC4_GEN_5); 152 153 if (ctm_state->fifo) { 154 HVS_WRITE(SCALER_OLEDCOEF2, 155 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]), 156 SCALER_OLEDCOEF2_R_TO_R) | 157 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]), 158 SCALER_OLEDCOEF2_R_TO_G) | 159 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]), 160 SCALER_OLEDCOEF2_R_TO_B)); 161 HVS_WRITE(SCALER_OLEDCOEF1, 162 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]), 163 SCALER_OLEDCOEF1_G_TO_R) | 164 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]), 165 SCALER_OLEDCOEF1_G_TO_G) | 166 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]), 167 SCALER_OLEDCOEF1_G_TO_B)); 168 HVS_WRITE(SCALER_OLEDCOEF0, 169 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]), 170 SCALER_OLEDCOEF0_B_TO_R) | 171 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]), 172 SCALER_OLEDCOEF0_B_TO_G) | 173 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]), 174 SCALER_OLEDCOEF0_B_TO_B)); 175 } 176 177 HVS_WRITE(SCALER_OLEDOFFS, 178 VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO)); 179 } 180 181 struct vc4_hvs_state * 182 vc4_hvs_get_new_global_state(const struct drm_atomic_state *state) 183 { 184 struct vc4_dev *vc4 = to_vc4_dev(state->dev); 185 struct drm_private_state *priv_state; 186 187 priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels); 188 if (!priv_state) 189 return ERR_PTR(-EINVAL); 190 191 return to_vc4_hvs_state(priv_state); 192 } 193 194 struct vc4_hvs_state * 195 vc4_hvs_get_old_global_state(const struct drm_atomic_state *state) 196 { 197 struct vc4_dev *vc4 = to_vc4_dev(state->dev); 198 struct drm_private_state *priv_state; 199 200 priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels); 201 if (!priv_state) 202 return ERR_PTR(-EINVAL); 203 204 return to_vc4_hvs_state(priv_state); 205 } 206 207 struct vc4_hvs_state * 208 vc4_hvs_get_global_state(struct drm_atomic_state *state) 209 { 210 struct vc4_dev *vc4 = to_vc4_dev(state->dev); 211 struct drm_private_state *priv_state; 212 213 priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels); 214 if (IS_ERR(priv_state)) 215 return ERR_CAST(priv_state); 216 217 return to_vc4_hvs_state(priv_state); 218 } 219 220 static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4, 221 struct drm_atomic_state *state) 222 { 223 struct vc4_hvs *hvs = vc4->hvs; 224 struct drm_crtc_state *crtc_state; 225 struct drm_crtc *crtc; 226 unsigned int i; 227 228 WARN_ON_ONCE(vc4->gen != VC4_GEN_4); 229 230 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 231 struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 232 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 233 u32 dispctrl; 234 u32 dsp3_mux; 235 236 if (!crtc_state->active) 237 continue; 238 239 if (vc4_state->assigned_channel != 2) 240 continue; 241 242 /* 243 * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to 244 * FIFO X'. 245 * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'. 246 * 247 * DSP3 is connected to FIFO2 unless the transposer is 248 * enabled. In this case, FIFO 2 is directly accessed by the 249 * TXP IP, and we need to disable the FIFO2 -> pixelvalve1 250 * route. 251 */ 252 if (vc4_crtc->feeds_txp) 253 dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX); 254 else 255 dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX); 256 257 dispctrl = HVS_READ(SCALER_DISPCTRL) & 258 ~SCALER_DISPCTRL_DSP3_MUX_MASK; 259 HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux); 260 } 261 } 262 263 static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4, 264 struct drm_atomic_state *state) 265 { 266 struct vc4_hvs *hvs = vc4->hvs; 267 struct drm_crtc_state *crtc_state; 268 struct drm_crtc *crtc; 269 unsigned char mux; 270 unsigned int i; 271 u32 reg; 272 273 WARN_ON_ONCE(vc4->gen != VC4_GEN_5); 274 275 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 276 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 277 struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 278 unsigned int channel = vc4_state->assigned_channel; 279 280 if (!vc4_state->update_muxing) 281 continue; 282 283 switch (vc4_crtc->data->hvs_output) { 284 case 2: 285 drm_WARN_ON(&vc4->base, 286 VC4_GET_FIELD(HVS_READ(SCALER_DISPCTRL), 287 SCALER_DISPCTRL_DSP3_MUX) == channel); 288 289 mux = (channel == 2) ? 0 : 1; 290 reg = HVS_READ(SCALER_DISPECTRL); 291 HVS_WRITE(SCALER_DISPECTRL, 292 (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) | 293 VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX)); 294 break; 295 296 case 3: 297 if (channel == VC4_HVS_CHANNEL_DISABLED) 298 mux = 3; 299 else 300 mux = channel; 301 302 reg = HVS_READ(SCALER_DISPCTRL); 303 HVS_WRITE(SCALER_DISPCTRL, 304 (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) | 305 VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX)); 306 break; 307 308 case 4: 309 if (channel == VC4_HVS_CHANNEL_DISABLED) 310 mux = 3; 311 else 312 mux = channel; 313 314 reg = HVS_READ(SCALER_DISPEOLN); 315 HVS_WRITE(SCALER_DISPEOLN, 316 (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) | 317 VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX)); 318 319 break; 320 321 case 5: 322 if (channel == VC4_HVS_CHANNEL_DISABLED) 323 mux = 3; 324 else 325 mux = channel; 326 327 reg = HVS_READ(SCALER_DISPDITHER); 328 HVS_WRITE(SCALER_DISPDITHER, 329 (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) | 330 VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX)); 331 break; 332 333 default: 334 break; 335 } 336 } 337 } 338 339 static void vc6_hvs_pv_muxing_commit(struct vc4_dev *vc4, 340 struct drm_atomic_state *state) 341 { 342 struct vc4_hvs *hvs = vc4->hvs; 343 struct drm_crtc_state *crtc_state; 344 struct drm_crtc *crtc; 345 unsigned int i; 346 347 WARN_ON_ONCE(vc4->gen != VC4_GEN_6_C && vc4->gen != VC4_GEN_6_D); 348 349 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 350 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 351 struct vc4_encoder *vc4_encoder; 352 struct drm_encoder *encoder; 353 unsigned char mux; 354 u32 reg; 355 356 if (!vc4_state->update_muxing) 357 continue; 358 359 if (vc4_state->assigned_channel != 1) 360 continue; 361 362 encoder = vc4_get_crtc_encoder(crtc, crtc_state); 363 vc4_encoder = to_vc4_encoder(encoder); 364 switch (vc4_encoder->type) { 365 case VC4_ENCODER_TYPE_HDMI1: 366 mux = 0; 367 break; 368 369 case VC4_ENCODER_TYPE_TXP1: 370 mux = 2; 371 break; 372 373 default: 374 drm_err(&vc4->base, "Unhandled encoder type for PV muxing %d", 375 vc4_encoder->type); 376 mux = 0; 377 break; 378 } 379 380 reg = HVS_READ(SCALER6_CONTROL); 381 HVS_WRITE(SCALER6_CONTROL, 382 (reg & ~SCALER6_CONTROL_DSP1_TARGET_MASK) | 383 VC4_SET_FIELD(mux, SCALER6_CONTROL_DSP1_TARGET)); 384 } 385 } 386 387 static void vc4_atomic_commit_tail(struct drm_atomic_state *state) 388 { 389 struct drm_device *dev = state->dev; 390 struct vc4_dev *vc4 = to_vc4_dev(dev); 391 struct vc4_hvs *hvs = vc4->hvs; 392 struct vc4_hvs_state *new_hvs_state; 393 struct vc4_hvs_state *old_hvs_state; 394 unsigned int channel; 395 396 old_hvs_state = vc4_hvs_get_old_global_state(state); 397 if (WARN_ON(IS_ERR(old_hvs_state))) 398 return; 399 400 new_hvs_state = vc4_hvs_get_new_global_state(state); 401 if (WARN_ON(IS_ERR(new_hvs_state))) 402 return; 403 404 if (vc4->gen < VC4_GEN_6_C) { 405 struct drm_crtc_state *new_crtc_state; 406 struct drm_crtc *crtc; 407 int i; 408 409 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 410 struct vc4_crtc_state *vc4_crtc_state; 411 412 if (!new_crtc_state->commit) 413 continue; 414 415 vc4_crtc_state = to_vc4_crtc_state(new_crtc_state); 416 vc4_hvs_mask_underrun(hvs, vc4_crtc_state->assigned_channel); 417 } 418 } 419 420 for (channel = 0; channel < HVS_NUM_CHANNELS; channel++) { 421 struct drm_crtc_commit *commit; 422 int ret; 423 424 if (!old_hvs_state->fifo_state[channel].in_use) 425 continue; 426 427 commit = old_hvs_state->fifo_state[channel].pending_commit; 428 if (!commit) 429 continue; 430 431 ret = drm_crtc_commit_wait(commit); 432 if (ret) 433 drm_err(dev, "Timed out waiting for commit\n"); 434 435 drm_crtc_commit_put(commit); 436 old_hvs_state->fifo_state[channel].pending_commit = NULL; 437 } 438 439 if (vc4->gen == VC4_GEN_5) { 440 unsigned long state_rate = max(old_hvs_state->core_clock_rate, 441 new_hvs_state->core_clock_rate); 442 unsigned long core_rate = clamp_t(unsigned long, state_rate, 443 500000000, hvs->max_core_rate); 444 445 drm_dbg(dev, "Raising the core clock at %lu Hz\n", core_rate); 446 447 /* 448 * Do a temporary request on the core clock during the 449 * modeset. 450 */ 451 WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate)); 452 WARN_ON(clk_set_min_rate(hvs->disp_clk, core_rate)); 453 } 454 455 drm_atomic_helper_commit_modeset_disables(dev, state); 456 457 if (vc4->gen <= VC4_GEN_5) 458 vc4_ctm_commit(vc4, state); 459 460 switch (vc4->gen) { 461 case VC4_GEN_4: 462 vc4_hvs_pv_muxing_commit(vc4, state); 463 break; 464 465 case VC4_GEN_5: 466 vc5_hvs_pv_muxing_commit(vc4, state); 467 break; 468 469 case VC4_GEN_6_C: 470 case VC4_GEN_6_D: 471 vc6_hvs_pv_muxing_commit(vc4, state); 472 break; 473 474 default: 475 drm_err(dev, "Unknown VC4 generation: %d", vc4->gen); 476 break; 477 } 478 479 drm_atomic_helper_commit_planes(dev, state, 480 DRM_PLANE_COMMIT_ACTIVE_ONLY); 481 482 drm_atomic_helper_commit_modeset_enables(dev, state); 483 484 drm_atomic_helper_fake_vblank(state); 485 486 drm_atomic_helper_commit_hw_done(state); 487 488 drm_atomic_helper_wait_for_flip_done(dev, state); 489 490 drm_atomic_helper_cleanup_planes(dev, state); 491 492 if (vc4->gen == VC4_GEN_5) { 493 unsigned long core_rate = min_t(unsigned long, 494 hvs->max_core_rate, 495 new_hvs_state->core_clock_rate); 496 497 drm_dbg(dev, "Running the core clock at %lu Hz\n", core_rate); 498 499 /* 500 * Request a clock rate based on the current HVS 501 * requirements. 502 */ 503 WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate)); 504 WARN_ON(clk_set_min_rate(hvs->disp_clk, core_rate)); 505 506 drm_dbg(dev, "Core clock actual rate: %lu Hz\n", 507 clk_get_rate(hvs->core_clk)); 508 } 509 } 510 511 static int vc4_atomic_commit_setup(struct drm_atomic_state *state) 512 { 513 struct drm_crtc_state *crtc_state; 514 struct vc4_hvs_state *hvs_state; 515 struct drm_crtc *crtc; 516 unsigned int i; 517 518 hvs_state = vc4_hvs_get_new_global_state(state); 519 if (WARN_ON(IS_ERR(hvs_state))) 520 return PTR_ERR(hvs_state); 521 522 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 523 struct vc4_crtc_state *vc4_crtc_state = 524 to_vc4_crtc_state(crtc_state); 525 unsigned int channel = 526 vc4_crtc_state->assigned_channel; 527 528 if (channel == VC4_HVS_CHANNEL_DISABLED) 529 continue; 530 531 if (!hvs_state->fifo_state[channel].in_use) 532 continue; 533 534 hvs_state->fifo_state[channel].pending_commit = 535 drm_crtc_commit_get(crtc_state->commit); 536 } 537 538 return 0; 539 } 540 541 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev, 542 struct drm_file *file_priv, 543 const struct drm_format_info *info, 544 const struct drm_mode_fb_cmd2 *mode_cmd) 545 { 546 struct vc4_dev *vc4 = to_vc4_dev(dev); 547 struct drm_mode_fb_cmd2 mode_cmd_local; 548 549 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 550 return ERR_PTR(-ENODEV); 551 552 /* If the user didn't specify a modifier, use the 553 * vc4_set_tiling_ioctl() state for the BO. 554 */ 555 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) { 556 struct drm_gem_object *gem_obj; 557 struct vc4_bo *bo; 558 559 gem_obj = drm_gem_object_lookup(file_priv, 560 mode_cmd->handles[0]); 561 if (!gem_obj) { 562 DRM_DEBUG("Failed to look up GEM BO %d\n", 563 mode_cmd->handles[0]); 564 return ERR_PTR(-ENOENT); 565 } 566 bo = to_vc4_bo(gem_obj); 567 568 mode_cmd_local = *mode_cmd; 569 570 if (bo->t_format) { 571 mode_cmd_local.modifier[0] = 572 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED; 573 } else { 574 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE; 575 } 576 577 drm_gem_object_put(gem_obj); 578 579 mode_cmd = &mode_cmd_local; 580 } 581 582 return drm_gem_fb_create(dev, file_priv, info, mode_cmd); 583 } 584 585 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC 586 * at a time and the HW only supports S0.9 scalars. To account for the latter, 587 * we don't allow userland to set a CTM that we have no hope of approximating. 588 */ 589 static int 590 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 591 { 592 struct vc4_dev *vc4 = to_vc4_dev(dev); 593 struct vc4_ctm_state *ctm_state = NULL; 594 struct drm_crtc *crtc; 595 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 596 struct drm_color_ctm *ctm; 597 int i; 598 599 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 600 /* CTM is being disabled. */ 601 if (!new_crtc_state->ctm && old_crtc_state->ctm) { 602 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager); 603 if (IS_ERR(ctm_state)) 604 return PTR_ERR(ctm_state); 605 ctm_state->fifo = 0; 606 } 607 } 608 609 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 610 if (new_crtc_state->ctm == old_crtc_state->ctm) 611 continue; 612 613 if (!ctm_state) { 614 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager); 615 if (IS_ERR(ctm_state)) 616 return PTR_ERR(ctm_state); 617 } 618 619 /* CTM is being enabled or the matrix changed. */ 620 if (new_crtc_state->ctm) { 621 struct vc4_crtc_state *vc4_crtc_state = 622 to_vc4_crtc_state(new_crtc_state); 623 624 /* fifo is 1-based since 0 disables CTM. */ 625 int fifo = vc4_crtc_state->assigned_channel + 1; 626 627 /* Check userland isn't trying to turn on CTM for more 628 * than one CRTC at a time. 629 */ 630 if (ctm_state->fifo && ctm_state->fifo != fifo) { 631 DRM_DEBUG_DRIVER("Too many CTM configured\n"); 632 return -EINVAL; 633 } 634 635 /* Check we can approximate the specified CTM. 636 * We disallow scalars |c| > 1.0 since the HW has 637 * no integer bits. 638 */ 639 ctm = new_crtc_state->ctm->data; 640 for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) { 641 u64 val = ctm->matrix[i]; 642 643 val &= ~BIT_ULL(63); 644 if (val > BIT_ULL(32)) 645 return -EINVAL; 646 } 647 648 ctm_state->fifo = fifo; 649 ctm_state->ctm = ctm; 650 } 651 } 652 653 return 0; 654 } 655 656 static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state) 657 { 658 struct drm_plane_state *old_plane_state, *new_plane_state; 659 struct vc4_dev *vc4 = to_vc4_dev(state->dev); 660 struct vc4_load_tracker_state *load_state; 661 struct drm_private_state *priv_state; 662 struct drm_plane *plane; 663 int i; 664 665 priv_state = drm_atomic_get_private_obj_state(state, 666 &vc4->load_tracker); 667 if (IS_ERR(priv_state)) 668 return PTR_ERR(priv_state); 669 670 load_state = to_vc4_load_tracker_state(priv_state); 671 for_each_oldnew_plane_in_state(state, plane, old_plane_state, 672 new_plane_state, i) { 673 struct vc4_plane_state *vc4_plane_state; 674 675 if (old_plane_state->fb && old_plane_state->crtc) { 676 vc4_plane_state = to_vc4_plane_state(old_plane_state); 677 load_state->membus_load -= vc4_plane_state->membus_load; 678 load_state->hvs_load -= vc4_plane_state->hvs_load; 679 } 680 681 if (new_plane_state->fb && new_plane_state->crtc) { 682 vc4_plane_state = to_vc4_plane_state(new_plane_state); 683 load_state->membus_load += vc4_plane_state->membus_load; 684 load_state->hvs_load += vc4_plane_state->hvs_load; 685 } 686 } 687 688 /* Don't check the load when the tracker is disabled. */ 689 if (!vc4->load_tracker_enabled) 690 return 0; 691 692 /* The absolute limit is 2Gbyte/sec, but let's take a margin to let 693 * the system work when other blocks are accessing the memory. 694 */ 695 if (load_state->membus_load > SZ_1G + SZ_512M) 696 return -ENOSPC; 697 698 /* HVS clock is supposed to run @ 250Mhz, let's take a margin and 699 * consider the maximum number of cycles is 240M. 700 */ 701 if (load_state->hvs_load > 240000000ULL) 702 return -ENOSPC; 703 704 return 0; 705 } 706 707 static struct drm_private_state * 708 vc4_load_tracker_duplicate_state(struct drm_private_obj *obj) 709 { 710 struct vc4_load_tracker_state *state; 711 712 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 713 if (!state) 714 return NULL; 715 716 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 717 718 return &state->base; 719 } 720 721 static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj, 722 struct drm_private_state *state) 723 { 724 struct vc4_load_tracker_state *load_state; 725 726 load_state = to_vc4_load_tracker_state(state); 727 kfree(load_state); 728 } 729 730 static struct drm_private_state * 731 vc4_load_tracker_create_state(struct drm_private_obj *obj) 732 { 733 struct vc4_load_tracker_state *load_state; 734 735 load_state = kzalloc_obj(*load_state); 736 if (!load_state) 737 return ERR_PTR(-ENOMEM); 738 739 __drm_atomic_helper_private_obj_create_state(obj, &load_state->base); 740 741 return &load_state->base; 742 } 743 744 static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = { 745 .atomic_create_state = vc4_load_tracker_create_state, 746 .atomic_duplicate_state = vc4_load_tracker_duplicate_state, 747 .atomic_destroy_state = vc4_load_tracker_destroy_state, 748 }; 749 750 static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused) 751 { 752 struct vc4_dev *vc4 = to_vc4_dev(dev); 753 754 drm_atomic_private_obj_fini(&vc4->load_tracker); 755 } 756 757 static int vc4_load_tracker_obj_init(struct vc4_dev *vc4) 758 { 759 drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker, 760 &vc4_load_tracker_state_funcs); 761 762 return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL); 763 } 764 765 static struct drm_private_state * 766 vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj) 767 { 768 struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state); 769 struct vc4_hvs_state *state; 770 unsigned int i; 771 772 state = kzalloc_obj(*state); 773 if (!state) 774 return NULL; 775 776 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 777 778 for (i = 0; i < HVS_NUM_CHANNELS; i++) { 779 state->fifo_state[i].in_use = old_state->fifo_state[i].in_use; 780 state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load; 781 } 782 783 state->core_clock_rate = old_state->core_clock_rate; 784 785 return &state->base; 786 } 787 788 static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj, 789 struct drm_private_state *state) 790 { 791 struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state); 792 unsigned int i; 793 794 for (i = 0; i < HVS_NUM_CHANNELS; i++) { 795 if (!hvs_state->fifo_state[i].pending_commit) 796 continue; 797 798 drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit); 799 } 800 801 kfree(hvs_state); 802 } 803 804 static void vc4_hvs_channels_print_state(struct drm_printer *p, 805 const struct drm_private_state *state) 806 { 807 const struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state); 808 unsigned int i; 809 810 drm_printf(p, "HVS State\n"); 811 drm_printf(p, "\tCore Clock Rate: %lu\n", hvs_state->core_clock_rate); 812 813 for (i = 0; i < HVS_NUM_CHANNELS; i++) { 814 drm_printf(p, "\tChannel %d\n", i); 815 drm_printf(p, "\t\tin use=%d\n", hvs_state->fifo_state[i].in_use); 816 drm_printf(p, "\t\tload=%lu\n", hvs_state->fifo_state[i].fifo_load); 817 } 818 } 819 820 static struct drm_private_state * 821 vc4_hvs_channels_create_state(struct drm_private_obj *obj) 822 { 823 struct vc4_hvs_state *hvs_state; 824 825 hvs_state = kzalloc_obj(*hvs_state); 826 if (!hvs_state) 827 return ERR_PTR(-ENOMEM); 828 829 __drm_atomic_helper_private_obj_create_state(obj, &hvs_state->base); 830 831 return &hvs_state->base; 832 } 833 834 static const struct drm_private_state_funcs vc4_hvs_state_funcs = { 835 .atomic_create_state = vc4_hvs_channels_create_state, 836 .atomic_duplicate_state = vc4_hvs_channels_duplicate_state, 837 .atomic_destroy_state = vc4_hvs_channels_destroy_state, 838 .atomic_print_state = vc4_hvs_channels_print_state, 839 }; 840 841 static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused) 842 { 843 struct vc4_dev *vc4 = to_vc4_dev(dev); 844 845 drm_atomic_private_obj_fini(&vc4->hvs_channels); 846 } 847 848 static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4) 849 { 850 drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels, 851 &vc4_hvs_state_funcs); 852 853 return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL); 854 } 855 856 static int cmp_vc4_crtc_hvs_output(const void *a, const void *b) 857 { 858 const struct vc4_crtc *crtc_a = 859 to_vc4_crtc(*(const struct drm_crtc **)a); 860 const struct vc4_crtc_data *data_a = 861 vc4_crtc_to_vc4_crtc_data(crtc_a); 862 const struct vc4_crtc *crtc_b = 863 to_vc4_crtc(*(const struct drm_crtc **)b); 864 const struct vc4_crtc_data *data_b = 865 vc4_crtc_to_vc4_crtc_data(crtc_b); 866 867 return data_a->hvs_output - data_b->hvs_output; 868 } 869 870 /* 871 * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and 872 * the TXP (and therefore all the CRTCs found on that platform). 873 * 874 * The naive (and our initial) implementation would just iterate over 875 * all the active CRTCs, try to find a suitable FIFO, and then remove it 876 * from the pool of available FIFOs. However, there are a few corner 877 * cases that need to be considered: 878 * 879 * - When running in a dual-display setup (so with two CRTCs involved), 880 * we can update the state of a single CRTC (for example by changing 881 * its mode using xrandr under X11) without affecting the other. In 882 * this case, the other CRTC wouldn't be in the state at all, so we 883 * need to consider all the running CRTCs in the DRM device to assign 884 * a FIFO, not just the one in the state. 885 * 886 * - To fix the above, we can't use drm_atomic_get_crtc_state on all 887 * enabled CRTCs to pull their CRTC state into the global state, since 888 * a page flip would start considering their vblank to complete. Since 889 * we don't have a guarantee that they are actually active, that 890 * vblank might never happen, and shouldn't even be considered if we 891 * want to do a page flip on a single CRTC. That can be tested by 892 * doing a modetest -v first on HDMI1 and then on HDMI0. 893 * 894 * - Since we need the pixelvalve to be disabled and enabled back when 895 * the FIFO is changed, we should keep the FIFO assigned for as long 896 * as the CRTC is enabled, only considering it free again once that 897 * CRTC has been disabled. This can be tested by booting X11 on a 898 * single display, and changing the resolution down and then back up. 899 */ 900 static int vc4_pv_muxing_atomic_check(struct drm_device *dev, 901 struct drm_atomic_state *state) 902 { 903 struct vc4_hvs_state *hvs_new_state; 904 struct drm_crtc **sorted_crtcs; 905 struct drm_crtc *crtc; 906 unsigned int unassigned_channels = 0; 907 unsigned int i; 908 int ret; 909 910 hvs_new_state = vc4_hvs_get_global_state(state); 911 if (IS_ERR(hvs_new_state)) 912 return PTR_ERR(hvs_new_state); 913 914 for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++) 915 if (!hvs_new_state->fifo_state[i].in_use) 916 unassigned_channels |= BIT(i); 917 918 /* 919 * The problem we have to solve here is that we have up to 7 920 * encoders, connected to up to 6 CRTCs. 921 * 922 * Those CRTCs, depending on the instance, can be routed to 1, 2 923 * or 3 HVS FIFOs, and we need to set the muxing between FIFOs and 924 * outputs in the HVS accordingly. 925 * 926 * It would be pretty hard to come up with an algorithm that 927 * would generically solve this. However, the current routing 928 * trees we support allow us to simplify a bit the problem. 929 * 930 * Indeed, with the current supported layouts, if we try to 931 * assign in the ascending crtc index order the FIFOs, we can't 932 * fall into the situation where an earlier CRTC that had 933 * multiple routes is assigned one that was the only option for 934 * a later CRTC. 935 * 936 * If the layout changes and doesn't give us that in the future, 937 * we will need to have something smarter, but it works so far. 938 */ 939 sorted_crtcs = kmalloc_objs(*sorted_crtcs, dev->num_crtcs); 940 if (!sorted_crtcs) 941 return -ENOMEM; 942 943 i = 0; 944 drm_for_each_crtc(crtc, dev) 945 sorted_crtcs[i++] = crtc; 946 947 sort(sorted_crtcs, i, sizeof(*sorted_crtcs), cmp_vc4_crtc_hvs_output, NULL); 948 949 for (i = 0; i < dev->num_crtcs; i++) { 950 struct vc4_crtc_state *old_vc4_crtc_state, *new_vc4_crtc_state; 951 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 952 struct vc4_crtc *vc4_crtc; 953 unsigned int matching_channels; 954 unsigned int channel; 955 956 crtc = sorted_crtcs[i]; 957 if (!crtc) 958 continue; 959 vc4_crtc = to_vc4_crtc(crtc); 960 961 old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc); 962 if (!old_crtc_state) 963 continue; 964 old_vc4_crtc_state = to_vc4_crtc_state(old_crtc_state); 965 966 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 967 if (!new_crtc_state) 968 continue; 969 new_vc4_crtc_state = to_vc4_crtc_state(new_crtc_state); 970 971 drm_dbg(dev, "%s: Trying to find a channel.\n", crtc->name); 972 973 /* Nothing to do here, let's skip it */ 974 if (old_crtc_state->enable == new_crtc_state->enable) { 975 if (new_crtc_state->enable) 976 drm_dbg(dev, "%s: Already enabled, reusing channel %d.\n", 977 crtc->name, new_vc4_crtc_state->assigned_channel); 978 else 979 drm_dbg(dev, "%s: Disabled, ignoring.\n", crtc->name); 980 981 continue; 982 } 983 984 /* Muxing will need to be modified, mark it as such */ 985 new_vc4_crtc_state->update_muxing = true; 986 987 /* If we're disabling our CRTC, we put back our channel */ 988 if (!new_crtc_state->enable) { 989 channel = old_vc4_crtc_state->assigned_channel; 990 991 drm_dbg(dev, "%s: Disabling, Freeing channel %d\n", 992 crtc->name, channel); 993 994 hvs_new_state->fifo_state[channel].in_use = false; 995 new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED; 996 continue; 997 } 998 999 matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels; 1000 if (!matching_channels) { 1001 ret = -EINVAL; 1002 goto err_free_crtc_array; 1003 } 1004 1005 channel = ffs(matching_channels) - 1; 1006 1007 drm_dbg(dev, "Assigned HVS channel %d to CRTC %s\n", channel, crtc->name); 1008 new_vc4_crtc_state->assigned_channel = channel; 1009 unassigned_channels &= ~BIT(channel); 1010 hvs_new_state->fifo_state[channel].in_use = true; 1011 } 1012 1013 kfree(sorted_crtcs); 1014 return 0; 1015 1016 err_free_crtc_array: 1017 kfree(sorted_crtcs); 1018 return ret; 1019 } 1020 1021 static int 1022 vc4_core_clock_atomic_check(struct drm_atomic_state *state) 1023 { 1024 struct vc4_dev *vc4 = to_vc4_dev(state->dev); 1025 struct drm_private_state *priv_state; 1026 struct vc4_hvs_state *hvs_new_state; 1027 struct vc4_load_tracker_state *load_state; 1028 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 1029 struct drm_crtc *crtc; 1030 unsigned int num_outputs; 1031 unsigned long pixel_rate; 1032 unsigned long cob_rate; 1033 unsigned int i; 1034 1035 priv_state = drm_atomic_get_private_obj_state(state, 1036 &vc4->load_tracker); 1037 if (IS_ERR(priv_state)) 1038 return PTR_ERR(priv_state); 1039 1040 load_state = to_vc4_load_tracker_state(priv_state); 1041 1042 hvs_new_state = vc4_hvs_get_global_state(state); 1043 if (IS_ERR(hvs_new_state)) 1044 return PTR_ERR(hvs_new_state); 1045 1046 for_each_oldnew_crtc_in_state(state, crtc, 1047 old_crtc_state, 1048 new_crtc_state, 1049 i) { 1050 if (old_crtc_state->active) { 1051 struct vc4_crtc_state *old_vc4_state = 1052 to_vc4_crtc_state(old_crtc_state); 1053 unsigned int channel = old_vc4_state->assigned_channel; 1054 1055 hvs_new_state->fifo_state[channel].fifo_load = 0; 1056 } 1057 1058 if (new_crtc_state->active) { 1059 struct vc4_crtc_state *new_vc4_state = 1060 to_vc4_crtc_state(new_crtc_state); 1061 unsigned int channel = new_vc4_state->assigned_channel; 1062 1063 hvs_new_state->fifo_state[channel].fifo_load = 1064 new_vc4_state->hvs_load; 1065 } 1066 } 1067 1068 cob_rate = 0; 1069 num_outputs = 0; 1070 for (i = 0; i < HVS_NUM_CHANNELS; i++) { 1071 if (!hvs_new_state->fifo_state[i].in_use) 1072 continue; 1073 1074 num_outputs++; 1075 cob_rate = max_t(unsigned long, 1076 hvs_new_state->fifo_state[i].fifo_load, 1077 cob_rate); 1078 } 1079 1080 pixel_rate = load_state->hvs_load; 1081 if (num_outputs > 1) { 1082 pixel_rate = (pixel_rate * 40) / 100; 1083 } else { 1084 pixel_rate = (pixel_rate * 60) / 100; 1085 } 1086 1087 hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate); 1088 1089 return 0; 1090 } 1091 1092 1093 static int 1094 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 1095 { 1096 int ret; 1097 1098 ret = vc4_pv_muxing_atomic_check(dev, state); 1099 if (ret) 1100 return ret; 1101 1102 ret = vc4_ctm_atomic_check(dev, state); 1103 if (ret < 0) 1104 return ret; 1105 1106 ret = drm_atomic_helper_check(dev, state); 1107 if (ret) 1108 return ret; 1109 1110 ret = vc4_load_tracker_atomic_check(state); 1111 if (ret) 1112 return ret; 1113 1114 return vc4_core_clock_atomic_check(state); 1115 } 1116 1117 static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = { 1118 .atomic_commit_setup = vc4_atomic_commit_setup, 1119 .atomic_commit_tail = vc4_atomic_commit_tail, 1120 }; 1121 1122 static const struct drm_mode_config_funcs vc4_mode_funcs = { 1123 .atomic_check = vc4_atomic_check, 1124 .atomic_commit = drm_atomic_helper_commit, 1125 .fb_create = vc4_fb_create, 1126 }; 1127 1128 static const struct drm_mode_config_funcs vc5_mode_funcs = { 1129 .atomic_check = vc4_atomic_check, 1130 .atomic_commit = drm_atomic_helper_commit, 1131 .fb_create = drm_gem_fb_create, 1132 }; 1133 1134 int vc4_kms_load(struct drm_device *dev) 1135 { 1136 struct vc4_dev *vc4 = to_vc4_dev(dev); 1137 int ret; 1138 1139 /* 1140 * The limits enforced by the load tracker aren't relevant for 1141 * the BCM2711, but the load tracker computations are used for 1142 * the core clock rate calculation. 1143 */ 1144 if (vc4->gen == VC4_GEN_4) { 1145 /* Start with the load tracker enabled. Can be 1146 * disabled through the debugfs load_tracker file. 1147 */ 1148 vc4->load_tracker_enabled = true; 1149 } 1150 1151 /* Set support for vblank irq fast disable, before drm_vblank_init() */ 1152 dev->vblank_disable_immediate = true; 1153 1154 ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 1155 if (ret < 0) { 1156 dev_err(dev->dev, "failed to initialize vblank\n"); 1157 return ret; 1158 } 1159 1160 if (vc4->gen >= VC4_GEN_6_C) { 1161 dev->mode_config.max_width = 8192; 1162 dev->mode_config.max_height = 8192; 1163 } else if (vc4->gen >= VC4_GEN_5) { 1164 dev->mode_config.max_width = 7680; 1165 dev->mode_config.max_height = 7680; 1166 } else { 1167 dev->mode_config.max_width = 2048; 1168 dev->mode_config.max_height = 2048; 1169 } 1170 1171 dev->mode_config.funcs = (vc4->gen > VC4_GEN_4) ? &vc5_mode_funcs : &vc4_mode_funcs; 1172 dev->mode_config.helper_private = &vc4_mode_config_helpers; 1173 dev->mode_config.preferred_depth = 24; 1174 dev->mode_config.async_page_flip = true; 1175 dev->mode_config.normalize_zpos = true; 1176 1177 ret = vc4_ctm_obj_init(vc4); 1178 if (ret) 1179 return ret; 1180 1181 ret = vc4_load_tracker_obj_init(vc4); 1182 if (ret) 1183 return ret; 1184 1185 ret = vc4_hvs_channels_obj_init(vc4); 1186 if (ret) 1187 return ret; 1188 1189 drm_mode_config_reset(dev); 1190 1191 drm_kms_helper_poll_init(dev); 1192 1193 return 0; 1194 } 1195