1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2019 Intel Corporation 4 */ 5 6 #include <drm/drm_atomic_state_helper.h> 7 8 #include "intel_atomic.h" 9 #include "intel_bw.h" 10 #include "intel_cdclk.h" 11 #include "intel_display_types.h" 12 #include "intel_pm.h" 13 #include "intel_sideband.h" 14 15 /* Parameters for Qclk Geyserville (QGV) */ 16 struct intel_qgv_point { 17 u16 dclk, t_rp, t_rdpre, t_rc, t_ras, t_rcd; 18 }; 19 20 struct intel_qgv_info { 21 struct intel_qgv_point points[I915_NUM_QGV_POINTS]; 22 u8 num_points; 23 u8 t_bl; 24 }; 25 26 static int icl_pcode_read_qgv_point_info(struct drm_i915_private *dev_priv, 27 struct intel_qgv_point *sp, 28 int point) 29 { 30 u32 val = 0, val2 = 0; 31 int ret; 32 33 ret = sandybridge_pcode_read(dev_priv, 34 ICL_PCODE_MEM_SUBSYSYSTEM_INFO | 35 ICL_PCODE_MEM_SS_READ_QGV_POINT_INFO(point), 36 &val, &val2); 37 if (ret) 38 return ret; 39 40 sp->dclk = val & 0xffff; 41 sp->t_rp = (val & 0xff0000) >> 16; 42 sp->t_rcd = (val & 0xff000000) >> 24; 43 44 sp->t_rdpre = val2 & 0xff; 45 sp->t_ras = (val2 & 0xff00) >> 8; 46 47 sp->t_rc = sp->t_rp + sp->t_ras; 48 49 return 0; 50 } 51 52 int icl_pcode_restrict_qgv_points(struct drm_i915_private *dev_priv, 53 u32 points_mask) 54 { 55 int ret; 56 57 /* bspec says to keep retrying for at least 1 ms */ 58 ret = skl_pcode_request(dev_priv, ICL_PCODE_SAGV_DE_MEM_SS_CONFIG, 59 points_mask, 60 ICL_PCODE_POINTS_RESTRICTED_MASK, 61 ICL_PCODE_POINTS_RESTRICTED, 62 1); 63 64 if (ret < 0) { 65 drm_err(&dev_priv->drm, "Failed to disable qgv points (%d)\n", ret); 66 return ret; 67 } 68 69 return 0; 70 } 71 72 static int icl_get_qgv_points(struct drm_i915_private *dev_priv, 73 struct intel_qgv_info *qi) 74 { 75 const struct dram_info *dram_info = &dev_priv->dram_info; 76 int i, ret; 77 78 qi->num_points = dram_info->num_qgv_points; 79 80 if (IS_DISPLAY_VER(dev_priv, 12)) 81 switch (dram_info->type) { 82 case INTEL_DRAM_DDR4: 83 qi->t_bl = 4; 84 break; 85 case INTEL_DRAM_DDR5: 86 qi->t_bl = 8; 87 break; 88 default: 89 qi->t_bl = 16; 90 break; 91 } 92 else if (IS_DISPLAY_VER(dev_priv, 11)) 93 qi->t_bl = dev_priv->dram_info.type == INTEL_DRAM_DDR4 ? 4 : 8; 94 95 if (drm_WARN_ON(&dev_priv->drm, 96 qi->num_points > ARRAY_SIZE(qi->points))) 97 qi->num_points = ARRAY_SIZE(qi->points); 98 99 for (i = 0; i < qi->num_points; i++) { 100 struct intel_qgv_point *sp = &qi->points[i]; 101 102 ret = icl_pcode_read_qgv_point_info(dev_priv, sp, i); 103 if (ret) 104 return ret; 105 106 drm_dbg_kms(&dev_priv->drm, 107 "QGV %d: DCLK=%d tRP=%d tRDPRE=%d tRAS=%d tRCD=%d tRC=%d\n", 108 i, sp->dclk, sp->t_rp, sp->t_rdpre, sp->t_ras, 109 sp->t_rcd, sp->t_rc); 110 } 111 112 return 0; 113 } 114 115 static int icl_calc_bw(int dclk, int num, int den) 116 { 117 /* multiples of 16.666MHz (100/6) */ 118 return DIV_ROUND_CLOSEST(num * dclk * 100, den * 6); 119 } 120 121 static int icl_sagv_max_dclk(const struct intel_qgv_info *qi) 122 { 123 u16 dclk = 0; 124 int i; 125 126 for (i = 0; i < qi->num_points; i++) 127 dclk = max(dclk, qi->points[i].dclk); 128 129 return dclk; 130 } 131 132 struct intel_sa_info { 133 u16 displayrtids; 134 u8 deburst, deprogbwlimit; 135 }; 136 137 static const struct intel_sa_info icl_sa_info = { 138 .deburst = 8, 139 .deprogbwlimit = 25, /* GB/s */ 140 .displayrtids = 128, 141 }; 142 143 static const struct intel_sa_info tgl_sa_info = { 144 .deburst = 16, 145 .deprogbwlimit = 34, /* GB/s */ 146 .displayrtids = 256, 147 }; 148 149 static const struct intel_sa_info rkl_sa_info = { 150 .deburst = 16, 151 .deprogbwlimit = 20, /* GB/s */ 152 .displayrtids = 128, 153 }; 154 155 static const struct intel_sa_info adls_sa_info = { 156 .deburst = 16, 157 .deprogbwlimit = 38, /* GB/s */ 158 .displayrtids = 256, 159 }; 160 161 static int icl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa) 162 { 163 struct intel_qgv_info qi = {}; 164 bool is_y_tile = true; /* assume y tile may be used */ 165 int num_channels = dev_priv->dram_info.num_channels; 166 int deinterleave; 167 int ipqdepth, ipqdepthpch; 168 int dclk_max; 169 int maxdebw; 170 int i, ret; 171 172 ret = icl_get_qgv_points(dev_priv, &qi); 173 if (ret) { 174 drm_dbg_kms(&dev_priv->drm, 175 "Failed to get memory subsystem information, ignoring bandwidth limits"); 176 return ret; 177 } 178 179 deinterleave = DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2); 180 dclk_max = icl_sagv_max_dclk(&qi); 181 182 ipqdepthpch = 16; 183 184 maxdebw = min(sa->deprogbwlimit * 1000, 185 icl_calc_bw(dclk_max, 16, 1) * 6 / 10); /* 60% */ 186 ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels); 187 188 for (i = 0; i < ARRAY_SIZE(dev_priv->max_bw); i++) { 189 struct intel_bw_info *bi = &dev_priv->max_bw[i]; 190 int clpchgroup; 191 int j; 192 193 clpchgroup = (sa->deburst * deinterleave / num_channels) << i; 194 bi->num_planes = (ipqdepth - clpchgroup) / clpchgroup + 1; 195 196 bi->num_qgv_points = qi.num_points; 197 198 for (j = 0; j < qi.num_points; j++) { 199 const struct intel_qgv_point *sp = &qi.points[j]; 200 int ct, bw; 201 202 /* 203 * Max row cycle time 204 * 205 * FIXME what is the logic behind the 206 * assumed burst length? 207 */ 208 ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd + 209 (clpchgroup - 1) * qi.t_bl + sp->t_rdpre); 210 bw = icl_calc_bw(sp->dclk, clpchgroup * 32 * num_channels, ct); 211 212 bi->deratedbw[j] = min(maxdebw, 213 bw * 9 / 10); /* 90% */ 214 215 drm_dbg_kms(&dev_priv->drm, 216 "BW%d / QGV %d: num_planes=%d deratedbw=%u\n", 217 i, j, bi->num_planes, bi->deratedbw[j]); 218 } 219 220 if (bi->num_planes == 1) 221 break; 222 } 223 224 /* 225 * In case if SAGV is disabled in BIOS, we always get 1 226 * SAGV point, but we can't send PCode commands to restrict it 227 * as it will fail and pointless anyway. 228 */ 229 if (qi.num_points == 1) 230 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED; 231 else 232 dev_priv->sagv_status = I915_SAGV_ENABLED; 233 234 return 0; 235 } 236 237 static unsigned int icl_max_bw(struct drm_i915_private *dev_priv, 238 int num_planes, int qgv_point) 239 { 240 int i; 241 242 /* 243 * Let's return max bw for 0 planes 244 */ 245 num_planes = max(1, num_planes); 246 247 for (i = 0; i < ARRAY_SIZE(dev_priv->max_bw); i++) { 248 const struct intel_bw_info *bi = 249 &dev_priv->max_bw[i]; 250 251 /* 252 * Pcode will not expose all QGV points when 253 * SAGV is forced to off/min/med/max. 254 */ 255 if (qgv_point >= bi->num_qgv_points) 256 return UINT_MAX; 257 258 if (num_planes >= bi->num_planes) 259 return bi->deratedbw[qgv_point]; 260 } 261 262 return 0; 263 } 264 265 void intel_bw_init_hw(struct drm_i915_private *dev_priv) 266 { 267 if (!HAS_DISPLAY(dev_priv)) 268 return; 269 270 if (IS_ALDERLAKE_S(dev_priv)) 271 icl_get_bw_info(dev_priv, &adls_sa_info); 272 else if (IS_ROCKETLAKE(dev_priv)) 273 icl_get_bw_info(dev_priv, &rkl_sa_info); 274 else if (IS_DISPLAY_VER(dev_priv, 12)) 275 icl_get_bw_info(dev_priv, &tgl_sa_info); 276 else if (IS_DISPLAY_VER(dev_priv, 11)) 277 icl_get_bw_info(dev_priv, &icl_sa_info); 278 } 279 280 static unsigned int intel_bw_crtc_num_active_planes(const struct intel_crtc_state *crtc_state) 281 { 282 /* 283 * We assume cursors are small enough 284 * to not not cause bandwidth problems. 285 */ 286 return hweight8(crtc_state->active_planes & ~BIT(PLANE_CURSOR)); 287 } 288 289 static unsigned int intel_bw_crtc_data_rate(const struct intel_crtc_state *crtc_state) 290 { 291 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 292 unsigned int data_rate = 0; 293 enum plane_id plane_id; 294 295 for_each_plane_id_on_crtc(crtc, plane_id) { 296 /* 297 * We assume cursors are small enough 298 * to not not cause bandwidth problems. 299 */ 300 if (plane_id == PLANE_CURSOR) 301 continue; 302 303 data_rate += crtc_state->data_rate[plane_id]; 304 } 305 306 return data_rate; 307 } 308 309 void intel_bw_crtc_update(struct intel_bw_state *bw_state, 310 const struct intel_crtc_state *crtc_state) 311 { 312 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 313 struct drm_i915_private *i915 = to_i915(crtc->base.dev); 314 315 bw_state->data_rate[crtc->pipe] = 316 intel_bw_crtc_data_rate(crtc_state); 317 bw_state->num_active_planes[crtc->pipe] = 318 intel_bw_crtc_num_active_planes(crtc_state); 319 320 drm_dbg_kms(&i915->drm, "pipe %c data rate %u num active planes %u\n", 321 pipe_name(crtc->pipe), 322 bw_state->data_rate[crtc->pipe], 323 bw_state->num_active_planes[crtc->pipe]); 324 } 325 326 static unsigned int intel_bw_num_active_planes(struct drm_i915_private *dev_priv, 327 const struct intel_bw_state *bw_state) 328 { 329 unsigned int num_active_planes = 0; 330 enum pipe pipe; 331 332 for_each_pipe(dev_priv, pipe) 333 num_active_planes += bw_state->num_active_planes[pipe]; 334 335 return num_active_planes; 336 } 337 338 static unsigned int intel_bw_data_rate(struct drm_i915_private *dev_priv, 339 const struct intel_bw_state *bw_state) 340 { 341 unsigned int data_rate = 0; 342 enum pipe pipe; 343 344 for_each_pipe(dev_priv, pipe) 345 data_rate += bw_state->data_rate[pipe]; 346 347 return data_rate; 348 } 349 350 struct intel_bw_state * 351 intel_atomic_get_old_bw_state(struct intel_atomic_state *state) 352 { 353 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 354 struct intel_global_state *bw_state; 355 356 bw_state = intel_atomic_get_old_global_obj_state(state, &dev_priv->bw_obj); 357 358 return to_intel_bw_state(bw_state); 359 } 360 361 struct intel_bw_state * 362 intel_atomic_get_new_bw_state(struct intel_atomic_state *state) 363 { 364 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 365 struct intel_global_state *bw_state; 366 367 bw_state = intel_atomic_get_new_global_obj_state(state, &dev_priv->bw_obj); 368 369 return to_intel_bw_state(bw_state); 370 } 371 372 struct intel_bw_state * 373 intel_atomic_get_bw_state(struct intel_atomic_state *state) 374 { 375 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 376 struct intel_global_state *bw_state; 377 378 bw_state = intel_atomic_get_global_obj_state(state, &dev_priv->bw_obj); 379 if (IS_ERR(bw_state)) 380 return ERR_CAST(bw_state); 381 382 return to_intel_bw_state(bw_state); 383 } 384 385 int skl_bw_calc_min_cdclk(struct intel_atomic_state *state) 386 { 387 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 388 struct intel_bw_state *new_bw_state = NULL; 389 struct intel_bw_state *old_bw_state = NULL; 390 const struct intel_crtc_state *crtc_state; 391 struct intel_crtc *crtc; 392 int max_bw = 0; 393 int slice_id; 394 enum pipe pipe; 395 int i; 396 397 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) { 398 enum plane_id plane_id; 399 struct intel_dbuf_bw *crtc_bw; 400 401 new_bw_state = intel_atomic_get_bw_state(state); 402 if (IS_ERR(new_bw_state)) 403 return PTR_ERR(new_bw_state); 404 405 old_bw_state = intel_atomic_get_old_bw_state(state); 406 407 crtc_bw = &new_bw_state->dbuf_bw[crtc->pipe]; 408 409 memset(&crtc_bw->used_bw, 0, sizeof(crtc_bw->used_bw)); 410 411 if (!crtc_state->hw.active) 412 continue; 413 414 for_each_plane_id_on_crtc(crtc, plane_id) { 415 const struct skl_ddb_entry *plane_alloc = 416 &crtc_state->wm.skl.plane_ddb_y[plane_id]; 417 const struct skl_ddb_entry *uv_plane_alloc = 418 &crtc_state->wm.skl.plane_ddb_uv[plane_id]; 419 unsigned int data_rate = crtc_state->data_rate[plane_id]; 420 unsigned int dbuf_mask = 0; 421 422 dbuf_mask |= skl_ddb_dbuf_slice_mask(dev_priv, plane_alloc); 423 dbuf_mask |= skl_ddb_dbuf_slice_mask(dev_priv, uv_plane_alloc); 424 425 /* 426 * FIXME: To calculate that more properly we probably 427 * need to to split per plane data_rate into data_rate_y 428 * and data_rate_uv for multiplanar formats in order not 429 * to get accounted those twice if they happen to reside 430 * on different slices. 431 * However for pre-icl this would work anyway because 432 * we have only single slice and for icl+ uv plane has 433 * non-zero data rate. 434 * So in worst case those calculation are a bit 435 * pessimistic, which shouldn't pose any significant 436 * problem anyway. 437 */ 438 for_each_dbuf_slice_in_mask(slice_id, dbuf_mask) 439 crtc_bw->used_bw[slice_id] += data_rate; 440 } 441 } 442 443 if (!old_bw_state) 444 return 0; 445 446 for_each_pipe(dev_priv, pipe) { 447 struct intel_dbuf_bw *crtc_bw; 448 449 crtc_bw = &new_bw_state->dbuf_bw[pipe]; 450 451 for_each_dbuf_slice(slice_id) { 452 /* 453 * Current experimental observations show that contrary 454 * to BSpec we get underruns once we exceed 64 * CDCLK 455 * for slices in total. 456 * As a temporary measure in order not to keep CDCLK 457 * bumped up all the time we calculate CDCLK according 458 * to this formula for overall bw consumed by slices. 459 */ 460 max_bw += crtc_bw->used_bw[slice_id]; 461 } 462 } 463 464 new_bw_state->min_cdclk = max_bw / 64; 465 466 if (new_bw_state->min_cdclk != old_bw_state->min_cdclk) { 467 int ret = intel_atomic_lock_global_state(&new_bw_state->base); 468 469 if (ret) 470 return ret; 471 } 472 473 return 0; 474 } 475 476 int intel_bw_calc_min_cdclk(struct intel_atomic_state *state) 477 { 478 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 479 struct intel_bw_state *new_bw_state = NULL; 480 struct intel_bw_state *old_bw_state = NULL; 481 const struct intel_crtc_state *crtc_state; 482 struct intel_crtc *crtc; 483 int min_cdclk = 0; 484 enum pipe pipe; 485 int i; 486 487 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) { 488 new_bw_state = intel_atomic_get_bw_state(state); 489 if (IS_ERR(new_bw_state)) 490 return PTR_ERR(new_bw_state); 491 492 old_bw_state = intel_atomic_get_old_bw_state(state); 493 } 494 495 if (!old_bw_state) 496 return 0; 497 498 for_each_pipe(dev_priv, pipe) { 499 struct intel_cdclk_state *cdclk_state; 500 501 cdclk_state = intel_atomic_get_new_cdclk_state(state); 502 if (!cdclk_state) 503 return 0; 504 505 min_cdclk = max(cdclk_state->min_cdclk[pipe], min_cdclk); 506 } 507 508 new_bw_state->min_cdclk = min_cdclk; 509 510 if (new_bw_state->min_cdclk != old_bw_state->min_cdclk) { 511 int ret = intel_atomic_lock_global_state(&new_bw_state->base); 512 513 if (ret) 514 return ret; 515 } 516 517 return 0; 518 } 519 520 int intel_bw_atomic_check(struct intel_atomic_state *state) 521 { 522 struct drm_i915_private *dev_priv = to_i915(state->base.dev); 523 struct intel_crtc_state *new_crtc_state, *old_crtc_state; 524 struct intel_bw_state *new_bw_state = NULL; 525 const struct intel_bw_state *old_bw_state = NULL; 526 unsigned int data_rate; 527 unsigned int num_active_planes; 528 struct intel_crtc *crtc; 529 int i, ret; 530 u32 allowed_points = 0; 531 unsigned int max_bw_point = 0, max_bw = 0; 532 unsigned int num_qgv_points = dev_priv->max_bw[0].num_qgv_points; 533 u32 mask = (1 << num_qgv_points) - 1; 534 535 /* FIXME earlier gens need some checks too */ 536 if (DISPLAY_VER(dev_priv) < 11) 537 return 0; 538 539 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, 540 new_crtc_state, i) { 541 unsigned int old_data_rate = 542 intel_bw_crtc_data_rate(old_crtc_state); 543 unsigned int new_data_rate = 544 intel_bw_crtc_data_rate(new_crtc_state); 545 unsigned int old_active_planes = 546 intel_bw_crtc_num_active_planes(old_crtc_state); 547 unsigned int new_active_planes = 548 intel_bw_crtc_num_active_planes(new_crtc_state); 549 550 /* 551 * Avoid locking the bw state when 552 * nothing significant has changed. 553 */ 554 if (old_data_rate == new_data_rate && 555 old_active_planes == new_active_planes) 556 continue; 557 558 new_bw_state = intel_atomic_get_bw_state(state); 559 if (IS_ERR(new_bw_state)) 560 return PTR_ERR(new_bw_state); 561 562 new_bw_state->data_rate[crtc->pipe] = new_data_rate; 563 new_bw_state->num_active_planes[crtc->pipe] = new_active_planes; 564 565 drm_dbg_kms(&dev_priv->drm, 566 "pipe %c data rate %u num active planes %u\n", 567 pipe_name(crtc->pipe), 568 new_bw_state->data_rate[crtc->pipe], 569 new_bw_state->num_active_planes[crtc->pipe]); 570 } 571 572 if (!new_bw_state) 573 return 0; 574 575 ret = intel_atomic_lock_global_state(&new_bw_state->base); 576 if (ret) 577 return ret; 578 579 data_rate = intel_bw_data_rate(dev_priv, new_bw_state); 580 data_rate = DIV_ROUND_UP(data_rate, 1000); 581 582 num_active_planes = intel_bw_num_active_planes(dev_priv, new_bw_state); 583 584 for (i = 0; i < num_qgv_points; i++) { 585 unsigned int max_data_rate; 586 587 max_data_rate = icl_max_bw(dev_priv, num_active_planes, i); 588 /* 589 * We need to know which qgv point gives us 590 * maximum bandwidth in order to disable SAGV 591 * if we find that we exceed SAGV block time 592 * with watermarks. By that moment we already 593 * have those, as it is calculated earlier in 594 * intel_atomic_check, 595 */ 596 if (max_data_rate > max_bw) { 597 max_bw_point = i; 598 max_bw = max_data_rate; 599 } 600 if (max_data_rate >= data_rate) 601 allowed_points |= BIT(i); 602 drm_dbg_kms(&dev_priv->drm, "QGV point %d: max bw %d required %d\n", 603 i, max_data_rate, data_rate); 604 } 605 606 /* 607 * BSpec states that we always should have at least one allowed point 608 * left, so if we couldn't - simply reject the configuration for obvious 609 * reasons. 610 */ 611 if (allowed_points == 0) { 612 drm_dbg_kms(&dev_priv->drm, "No QGV points provide sufficient memory" 613 " bandwidth %d for display configuration(%d active planes).\n", 614 data_rate, num_active_planes); 615 return -EINVAL; 616 } 617 618 /* 619 * Leave only single point with highest bandwidth, if 620 * we can't enable SAGV due to the increased memory latency it may 621 * cause. 622 */ 623 if (!intel_can_enable_sagv(dev_priv, new_bw_state)) { 624 allowed_points = BIT(max_bw_point); 625 drm_dbg_kms(&dev_priv->drm, "No SAGV, using single QGV point %d\n", 626 max_bw_point); 627 } 628 /* 629 * We store the ones which need to be masked as that is what PCode 630 * actually accepts as a parameter. 631 */ 632 new_bw_state->qgv_points_mask = ~allowed_points & mask; 633 634 old_bw_state = intel_atomic_get_old_bw_state(state); 635 /* 636 * If the actual mask had changed we need to make sure that 637 * the commits are serialized(in case this is a nomodeset, nonblocking) 638 */ 639 if (new_bw_state->qgv_points_mask != old_bw_state->qgv_points_mask) { 640 ret = intel_atomic_serialize_global_state(&new_bw_state->base); 641 if (ret) 642 return ret; 643 } 644 645 return 0; 646 } 647 648 static struct intel_global_state * 649 intel_bw_duplicate_state(struct intel_global_obj *obj) 650 { 651 struct intel_bw_state *state; 652 653 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 654 if (!state) 655 return NULL; 656 657 return &state->base; 658 } 659 660 static void intel_bw_destroy_state(struct intel_global_obj *obj, 661 struct intel_global_state *state) 662 { 663 kfree(state); 664 } 665 666 static const struct intel_global_state_funcs intel_bw_funcs = { 667 .atomic_duplicate_state = intel_bw_duplicate_state, 668 .atomic_destroy_state = intel_bw_destroy_state, 669 }; 670 671 int intel_bw_init(struct drm_i915_private *dev_priv) 672 { 673 struct intel_bw_state *state; 674 675 state = kzalloc(sizeof(*state), GFP_KERNEL); 676 if (!state) 677 return -ENOMEM; 678 679 intel_atomic_global_obj_init(dev_priv, &dev_priv->bw_obj, 680 &state->base, &intel_bw_funcs); 681 682 return 0; 683 } 684