1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2019 Linaro Ltd. 4 * 5 * Author: Stanimir Varbanov <stanimir.varbanov@linaro.org> 6 */ 7 #include <linux/clk.h> 8 #include <linux/interconnect.h> 9 #include <linux/iopoll.h> 10 #include <linux/kernel.h> 11 #include <linux/pm_domain.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/types.h> 14 #include <media/v4l2-mem2mem.h> 15 16 #include "core.h" 17 #include "hfi_parser.h" 18 #include "hfi_venus_io.h" 19 #include "pm_helpers.h" 20 21 static bool legacy_binding; 22 23 static int core_clks_get(struct venus_core *core) 24 { 25 const struct venus_resources *res = core->res; 26 struct device *dev = core->dev; 27 unsigned int i; 28 29 for (i = 0; i < res->clks_num; i++) { 30 core->clks[i] = devm_clk_get(dev, res->clks[i]); 31 if (IS_ERR(core->clks[i])) 32 return PTR_ERR(core->clks[i]); 33 } 34 35 return 0; 36 } 37 38 static int core_clks_enable(struct venus_core *core) 39 { 40 const struct venus_resources *res = core->res; 41 unsigned int i; 42 int ret; 43 44 for (i = 0; i < res->clks_num; i++) { 45 ret = clk_prepare_enable(core->clks[i]); 46 if (ret) 47 goto err; 48 } 49 50 return 0; 51 err: 52 while (i--) 53 clk_disable_unprepare(core->clks[i]); 54 55 return ret; 56 } 57 58 static void core_clks_disable(struct venus_core *core) 59 { 60 const struct venus_resources *res = core->res; 61 unsigned int i = res->clks_num; 62 63 while (i--) 64 clk_disable_unprepare(core->clks[i]); 65 } 66 67 static int core_clks_set_rate(struct venus_core *core, unsigned long freq) 68 { 69 struct clk *clk = core->clks[0]; 70 int ret; 71 72 ret = clk_set_rate(clk, freq); 73 if (ret) 74 return ret; 75 76 ret = clk_set_rate(core->vcodec0_clks[0], freq); 77 if (ret) 78 return ret; 79 80 ret = clk_set_rate(core->vcodec1_clks[0], freq); 81 if (ret) 82 return ret; 83 84 return 0; 85 } 86 87 static int vcodec_clks_get(struct venus_core *core, struct device *dev, 88 struct clk **clks, const char * const *id) 89 { 90 const struct venus_resources *res = core->res; 91 unsigned int i; 92 93 for (i = 0; i < res->vcodec_clks_num; i++) { 94 if (!id[i]) 95 continue; 96 clks[i] = devm_clk_get(dev, id[i]); 97 if (IS_ERR(clks[i])) 98 return PTR_ERR(clks[i]); 99 } 100 101 return 0; 102 } 103 104 static int vcodec_clks_enable(struct venus_core *core, struct clk **clks) 105 { 106 const struct venus_resources *res = core->res; 107 unsigned int i; 108 int ret; 109 110 for (i = 0; i < res->vcodec_clks_num; i++) { 111 ret = clk_prepare_enable(clks[i]); 112 if (ret) 113 goto err; 114 } 115 116 return 0; 117 err: 118 while (i--) 119 clk_disable_unprepare(clks[i]); 120 121 return ret; 122 } 123 124 static void vcodec_clks_disable(struct venus_core *core, struct clk **clks) 125 { 126 const struct venus_resources *res = core->res; 127 unsigned int i = res->vcodec_clks_num; 128 129 while (i--) 130 clk_disable_unprepare(clks[i]); 131 } 132 133 static u32 load_per_instance(struct venus_inst *inst) 134 { 135 u32 mbs; 136 137 if (!inst || !(inst->state >= INST_INIT && inst->state < INST_STOP)) 138 return 0; 139 140 mbs = (ALIGN(inst->width, 16) / 16) * (ALIGN(inst->height, 16) / 16); 141 142 return mbs * inst->fps; 143 } 144 145 static u32 load_per_type(struct venus_core *core, u32 session_type) 146 { 147 struct venus_inst *inst = NULL; 148 u32 mbs_per_sec = 0; 149 150 mutex_lock(&core->lock); 151 list_for_each_entry(inst, &core->instances, list) { 152 if (inst->session_type != session_type) 153 continue; 154 155 mbs_per_sec += load_per_instance(inst); 156 } 157 mutex_unlock(&core->lock); 158 159 return mbs_per_sec; 160 } 161 162 static void mbs_to_bw(struct venus_inst *inst, u32 mbs, u32 *avg, u32 *peak) 163 { 164 const struct venus_resources *res = inst->core->res; 165 const struct bw_tbl *bw_tbl; 166 unsigned int num_rows, i; 167 168 *avg = 0; 169 *peak = 0; 170 171 if (mbs == 0) 172 return; 173 174 if (inst->session_type == VIDC_SESSION_TYPE_ENC) { 175 num_rows = res->bw_tbl_enc_size; 176 bw_tbl = res->bw_tbl_enc; 177 } else if (inst->session_type == VIDC_SESSION_TYPE_DEC) { 178 num_rows = res->bw_tbl_dec_size; 179 bw_tbl = res->bw_tbl_dec; 180 } else { 181 return; 182 } 183 184 if (!bw_tbl || num_rows == 0) 185 return; 186 187 for (i = 0; i < num_rows; i++) { 188 if (mbs > bw_tbl[i].mbs_per_sec) 189 break; 190 191 if (inst->dpb_fmt & HFI_COLOR_FORMAT_10_BIT_BASE) { 192 *avg = bw_tbl[i].avg_10bit; 193 *peak = bw_tbl[i].peak_10bit; 194 } else { 195 *avg = bw_tbl[i].avg; 196 *peak = bw_tbl[i].peak; 197 } 198 } 199 } 200 201 static int load_scale_bw(struct venus_core *core) 202 { 203 struct venus_inst *inst = NULL; 204 u32 mbs_per_sec, avg, peak, total_avg = 0, total_peak = 0; 205 206 mutex_lock(&core->lock); 207 list_for_each_entry(inst, &core->instances, list) { 208 mbs_per_sec = load_per_instance(inst); 209 mbs_to_bw(inst, mbs_per_sec, &avg, &peak); 210 total_avg += avg; 211 total_peak += peak; 212 } 213 mutex_unlock(&core->lock); 214 215 dev_dbg(core->dev, "total: avg_bw: %u, peak_bw: %u\n", 216 total_avg, total_peak); 217 218 return icc_set_bw(core->video_path, total_avg, total_peak); 219 } 220 221 static int load_scale_v1(struct venus_inst *inst) 222 { 223 struct venus_core *core = inst->core; 224 const struct freq_tbl *table = core->res->freq_tbl; 225 unsigned int num_rows = core->res->freq_tbl_size; 226 unsigned long freq = table[0].freq; 227 struct device *dev = core->dev; 228 u32 mbs_per_sec; 229 unsigned int i; 230 int ret; 231 232 mbs_per_sec = load_per_type(core, VIDC_SESSION_TYPE_ENC) + 233 load_per_type(core, VIDC_SESSION_TYPE_DEC); 234 235 if (mbs_per_sec > core->res->max_load) 236 dev_warn(dev, "HW is overloaded, needed: %d max: %d\n", 237 mbs_per_sec, core->res->max_load); 238 239 if (!mbs_per_sec && num_rows > 1) { 240 freq = table[num_rows - 1].freq; 241 goto set_freq; 242 } 243 244 for (i = 0; i < num_rows; i++) { 245 if (mbs_per_sec > table[i].load) 246 break; 247 freq = table[i].freq; 248 } 249 250 set_freq: 251 252 ret = core_clks_set_rate(core, freq); 253 if (ret) { 254 dev_err(dev, "failed to set clock rate %lu (%d)\n", 255 freq, ret); 256 return ret; 257 } 258 259 ret = load_scale_bw(core); 260 if (ret) { 261 dev_err(dev, "failed to set bandwidth (%d)\n", 262 ret); 263 return ret; 264 } 265 266 return 0; 267 } 268 269 static int core_get_v1(struct device *dev) 270 { 271 struct venus_core *core = dev_get_drvdata(dev); 272 273 return core_clks_get(core); 274 } 275 276 static int core_power_v1(struct device *dev, int on) 277 { 278 struct venus_core *core = dev_get_drvdata(dev); 279 int ret = 0; 280 281 if (on == POWER_ON) 282 ret = core_clks_enable(core); 283 else 284 core_clks_disable(core); 285 286 return ret; 287 } 288 289 static const struct venus_pm_ops pm_ops_v1 = { 290 .core_get = core_get_v1, 291 .core_power = core_power_v1, 292 .load_scale = load_scale_v1, 293 }; 294 295 static void 296 vcodec_control_v3(struct venus_core *core, u32 session_type, bool enable) 297 { 298 void __iomem *ctrl; 299 300 if (session_type == VIDC_SESSION_TYPE_DEC) 301 ctrl = core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL; 302 else 303 ctrl = core->base + WRAPPER_VENC_VCODEC_POWER_CONTROL; 304 305 if (enable) 306 writel(0, ctrl); 307 else 308 writel(1, ctrl); 309 } 310 311 static int vdec_get_v3(struct device *dev) 312 { 313 struct venus_core *core = dev_get_drvdata(dev); 314 315 return vcodec_clks_get(core, dev, core->vcodec0_clks, 316 core->res->vcodec0_clks); 317 } 318 319 static int vdec_power_v3(struct device *dev, int on) 320 { 321 struct venus_core *core = dev_get_drvdata(dev); 322 int ret = 0; 323 324 vcodec_control_v3(core, VIDC_SESSION_TYPE_DEC, true); 325 326 if (on == POWER_ON) 327 ret = vcodec_clks_enable(core, core->vcodec0_clks); 328 else 329 vcodec_clks_disable(core, core->vcodec0_clks); 330 331 vcodec_control_v3(core, VIDC_SESSION_TYPE_DEC, false); 332 333 return ret; 334 } 335 336 static int venc_get_v3(struct device *dev) 337 { 338 struct venus_core *core = dev_get_drvdata(dev); 339 340 return vcodec_clks_get(core, dev, core->vcodec1_clks, 341 core->res->vcodec1_clks); 342 } 343 344 static int venc_power_v3(struct device *dev, int on) 345 { 346 struct venus_core *core = dev_get_drvdata(dev); 347 int ret = 0; 348 349 vcodec_control_v3(core, VIDC_SESSION_TYPE_ENC, true); 350 351 if (on == POWER_ON) 352 ret = vcodec_clks_enable(core, core->vcodec1_clks); 353 else 354 vcodec_clks_disable(core, core->vcodec1_clks); 355 356 vcodec_control_v3(core, VIDC_SESSION_TYPE_ENC, false); 357 358 return ret; 359 } 360 361 static const struct venus_pm_ops pm_ops_v3 = { 362 .core_get = core_get_v1, 363 .core_power = core_power_v1, 364 .vdec_get = vdec_get_v3, 365 .vdec_power = vdec_power_v3, 366 .venc_get = venc_get_v3, 367 .venc_power = venc_power_v3, 368 .load_scale = load_scale_v1, 369 }; 370 371 static int vcodec_control_v4(struct venus_core *core, u32 coreid, bool enable) 372 { 373 void __iomem *ctrl, *stat; 374 u32 val; 375 int ret; 376 377 if (coreid == VIDC_CORE_ID_1) { 378 ctrl = core->base + WRAPPER_VCODEC0_MMCC_POWER_CONTROL; 379 stat = core->base + WRAPPER_VCODEC0_MMCC_POWER_STATUS; 380 } else { 381 ctrl = core->base + WRAPPER_VCODEC1_MMCC_POWER_CONTROL; 382 stat = core->base + WRAPPER_VCODEC1_MMCC_POWER_STATUS; 383 } 384 385 if (enable) { 386 writel(0, ctrl); 387 388 ret = readl_poll_timeout(stat, val, val & BIT(1), 1, 100); 389 if (ret) 390 return ret; 391 } else { 392 writel(1, ctrl); 393 394 ret = readl_poll_timeout(stat, val, !(val & BIT(1)), 1, 100); 395 if (ret) 396 return ret; 397 } 398 399 return 0; 400 } 401 402 static int poweroff_coreid(struct venus_core *core, unsigned int coreid_mask) 403 { 404 int ret; 405 406 if (coreid_mask & VIDC_CORE_ID_1) { 407 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, true); 408 if (ret) 409 return ret; 410 411 vcodec_clks_disable(core, core->vcodec0_clks); 412 413 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, false); 414 if (ret) 415 return ret; 416 417 ret = pm_runtime_put_sync(core->pmdomains[1]); 418 if (ret < 0) 419 return ret; 420 } 421 422 if (coreid_mask & VIDC_CORE_ID_2) { 423 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, true); 424 if (ret) 425 return ret; 426 427 vcodec_clks_disable(core, core->vcodec1_clks); 428 429 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, false); 430 if (ret) 431 return ret; 432 433 ret = pm_runtime_put_sync(core->pmdomains[2]); 434 if (ret < 0) 435 return ret; 436 } 437 438 return 0; 439 } 440 441 static int poweron_coreid(struct venus_core *core, unsigned int coreid_mask) 442 { 443 int ret; 444 445 if (coreid_mask & VIDC_CORE_ID_1) { 446 ret = pm_runtime_get_sync(core->pmdomains[1]); 447 if (ret < 0) 448 return ret; 449 450 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, true); 451 if (ret) 452 return ret; 453 454 ret = vcodec_clks_enable(core, core->vcodec0_clks); 455 if (ret) 456 return ret; 457 458 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, false); 459 if (ret < 0) 460 return ret; 461 } 462 463 if (coreid_mask & VIDC_CORE_ID_2) { 464 ret = pm_runtime_get_sync(core->pmdomains[2]); 465 if (ret < 0) 466 return ret; 467 468 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, true); 469 if (ret) 470 return ret; 471 472 ret = vcodec_clks_enable(core, core->vcodec1_clks); 473 if (ret) 474 return ret; 475 476 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, false); 477 if (ret < 0) 478 return ret; 479 } 480 481 return 0; 482 } 483 484 static void 485 min_loaded_core(struct venus_inst *inst, u32 *min_coreid, u32 *min_load) 486 { 487 u32 mbs_per_sec, load, core1_load = 0, core2_load = 0; 488 u32 cores_max = core_num_max(inst); 489 struct venus_core *core = inst->core; 490 struct venus_inst *inst_pos; 491 unsigned long vpp_freq; 492 u32 coreid; 493 494 mutex_lock(&core->lock); 495 496 list_for_each_entry(inst_pos, &core->instances, list) { 497 if (inst_pos == inst) 498 continue; 499 500 if (inst_pos->state != INST_START) 501 continue; 502 503 vpp_freq = inst_pos->clk_data.codec_freq_data->vpp_freq; 504 coreid = inst_pos->clk_data.core_id; 505 506 mbs_per_sec = load_per_instance(inst_pos); 507 load = mbs_per_sec * vpp_freq; 508 509 if ((coreid & VIDC_CORE_ID_3) == VIDC_CORE_ID_3) { 510 core1_load += load / 2; 511 core2_load += load / 2; 512 } else if (coreid & VIDC_CORE_ID_1) { 513 core1_load += load; 514 } else if (coreid & VIDC_CORE_ID_2) { 515 core2_load += load; 516 } 517 } 518 519 *min_coreid = core1_load <= core2_load ? 520 VIDC_CORE_ID_1 : VIDC_CORE_ID_2; 521 *min_load = min(core1_load, core2_load); 522 523 if (cores_max < VIDC_CORE_ID_2 || core->res->vcodec_num < 2) { 524 *min_coreid = VIDC_CORE_ID_1; 525 *min_load = core1_load; 526 } 527 528 mutex_unlock(&core->lock); 529 } 530 531 static int decide_core(struct venus_inst *inst) 532 { 533 const u32 ptype = HFI_PROPERTY_CONFIG_VIDEOCORES_USAGE; 534 struct venus_core *core = inst->core; 535 u32 min_coreid, min_load, inst_load; 536 struct hfi_videocores_usage_type cu; 537 unsigned long max_freq; 538 539 if (legacy_binding) { 540 if (inst->session_type == VIDC_SESSION_TYPE_DEC) 541 cu.video_core_enable_mask = VIDC_CORE_ID_1; 542 else 543 cu.video_core_enable_mask = VIDC_CORE_ID_2; 544 545 goto done; 546 } 547 548 if (inst->clk_data.core_id != VIDC_CORE_ID_DEFAULT) 549 return 0; 550 551 inst_load = load_per_instance(inst); 552 inst_load *= inst->clk_data.codec_freq_data->vpp_freq; 553 max_freq = core->res->freq_tbl[0].freq; 554 555 min_loaded_core(inst, &min_coreid, &min_load); 556 557 if ((inst_load + min_load) > max_freq) { 558 dev_warn(core->dev, "HW is overloaded, needed: %u max: %lu\n", 559 inst_load, max_freq); 560 return -EINVAL; 561 } 562 563 inst->clk_data.core_id = min_coreid; 564 cu.video_core_enable_mask = min_coreid; 565 566 done: 567 return hfi_session_set_property(inst, ptype, &cu); 568 } 569 570 static int acquire_core(struct venus_inst *inst) 571 { 572 struct venus_core *core = inst->core; 573 unsigned int coreid_mask = 0; 574 575 if (inst->core_acquired) 576 return 0; 577 578 inst->core_acquired = true; 579 580 if (inst->clk_data.core_id & VIDC_CORE_ID_1) { 581 if (core->core0_usage_count++) 582 return 0; 583 584 coreid_mask = VIDC_CORE_ID_1; 585 } 586 587 if (inst->clk_data.core_id & VIDC_CORE_ID_2) { 588 if (core->core1_usage_count++) 589 return 0; 590 591 coreid_mask |= VIDC_CORE_ID_2; 592 } 593 594 return poweron_coreid(core, coreid_mask); 595 } 596 597 static int release_core(struct venus_inst *inst) 598 { 599 struct venus_core *core = inst->core; 600 unsigned int coreid_mask = 0; 601 int ret; 602 603 if (!inst->core_acquired) 604 return 0; 605 606 if (inst->clk_data.core_id & VIDC_CORE_ID_1) { 607 if (--core->core0_usage_count) 608 goto done; 609 610 coreid_mask = VIDC_CORE_ID_1; 611 } 612 613 if (inst->clk_data.core_id & VIDC_CORE_ID_2) { 614 if (--core->core1_usage_count) 615 goto done; 616 617 coreid_mask |= VIDC_CORE_ID_2; 618 } 619 620 ret = poweroff_coreid(core, coreid_mask); 621 if (ret) 622 return ret; 623 624 done: 625 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT; 626 inst->core_acquired = false; 627 return 0; 628 } 629 630 static int coreid_power_v4(struct venus_inst *inst, int on) 631 { 632 struct venus_core *core = inst->core; 633 int ret; 634 635 if (legacy_binding) 636 return 0; 637 638 if (on == POWER_ON) { 639 ret = decide_core(inst); 640 if (ret) 641 return ret; 642 643 mutex_lock(&core->lock); 644 ret = acquire_core(inst); 645 mutex_unlock(&core->lock); 646 } else { 647 mutex_lock(&core->lock); 648 ret = release_core(inst); 649 mutex_unlock(&core->lock); 650 } 651 652 return ret; 653 } 654 655 static int vdec_get_v4(struct device *dev) 656 { 657 struct venus_core *core = dev_get_drvdata(dev); 658 659 if (!legacy_binding) 660 return 0; 661 662 return vcodec_clks_get(core, dev, core->vcodec0_clks, 663 core->res->vcodec0_clks); 664 } 665 666 static void vdec_put_v4(struct device *dev) 667 { 668 struct venus_core *core = dev_get_drvdata(dev); 669 unsigned int i; 670 671 if (!legacy_binding) 672 return; 673 674 for (i = 0; i < core->res->vcodec_clks_num; i++) 675 core->vcodec0_clks[i] = NULL; 676 } 677 678 static int vdec_power_v4(struct device *dev, int on) 679 { 680 struct venus_core *core = dev_get_drvdata(dev); 681 int ret; 682 683 if (!legacy_binding) 684 return 0; 685 686 ret = vcodec_control_v4(core, VIDC_CORE_ID_1, true); 687 if (ret) 688 return ret; 689 690 if (on == POWER_ON) 691 ret = vcodec_clks_enable(core, core->vcodec0_clks); 692 else 693 vcodec_clks_disable(core, core->vcodec0_clks); 694 695 vcodec_control_v4(core, VIDC_CORE_ID_1, false); 696 697 return ret; 698 } 699 700 static int venc_get_v4(struct device *dev) 701 { 702 struct venus_core *core = dev_get_drvdata(dev); 703 704 if (!legacy_binding) 705 return 0; 706 707 return vcodec_clks_get(core, dev, core->vcodec1_clks, 708 core->res->vcodec1_clks); 709 } 710 711 static void venc_put_v4(struct device *dev) 712 { 713 struct venus_core *core = dev_get_drvdata(dev); 714 unsigned int i; 715 716 if (!legacy_binding) 717 return; 718 719 for (i = 0; i < core->res->vcodec_clks_num; i++) 720 core->vcodec1_clks[i] = NULL; 721 } 722 723 static int venc_power_v4(struct device *dev, int on) 724 { 725 struct venus_core *core = dev_get_drvdata(dev); 726 int ret; 727 728 if (!legacy_binding) 729 return 0; 730 731 ret = vcodec_control_v4(core, VIDC_CORE_ID_2, true); 732 if (ret) 733 return ret; 734 735 if (on == POWER_ON) 736 ret = vcodec_clks_enable(core, core->vcodec1_clks); 737 else 738 vcodec_clks_disable(core, core->vcodec1_clks); 739 740 vcodec_control_v4(core, VIDC_CORE_ID_2, false); 741 742 return ret; 743 } 744 745 static int vcodec_domains_get(struct device *dev) 746 { 747 struct venus_core *core = dev_get_drvdata(dev); 748 const struct venus_resources *res = core->res; 749 struct device *pd; 750 unsigned int i; 751 752 if (!res->vcodec_pmdomains_num) 753 return -ENODEV; 754 755 for (i = 0; i < res->vcodec_pmdomains_num; i++) { 756 pd = dev_pm_domain_attach_by_name(dev, 757 res->vcodec_pmdomains[i]); 758 if (IS_ERR(pd)) 759 return PTR_ERR(pd); 760 core->pmdomains[i] = pd; 761 } 762 763 core->pd_dl_venus = device_link_add(dev, core->pmdomains[0], 764 DL_FLAG_PM_RUNTIME | 765 DL_FLAG_STATELESS | 766 DL_FLAG_RPM_ACTIVE); 767 if (!core->pd_dl_venus) 768 return -ENODEV; 769 770 return 0; 771 } 772 773 static void vcodec_domains_put(struct device *dev) 774 { 775 struct venus_core *core = dev_get_drvdata(dev); 776 const struct venus_resources *res = core->res; 777 unsigned int i; 778 779 if (!res->vcodec_pmdomains_num) 780 return; 781 782 if (core->pd_dl_venus) 783 device_link_del(core->pd_dl_venus); 784 785 for (i = 0; i < res->vcodec_pmdomains_num; i++) { 786 if (IS_ERR_OR_NULL(core->pmdomains[i])) 787 continue; 788 dev_pm_domain_detach(core->pmdomains[i], true); 789 } 790 } 791 792 static int core_get_v4(struct device *dev) 793 { 794 struct venus_core *core = dev_get_drvdata(dev); 795 const struct venus_resources *res = core->res; 796 int ret; 797 798 ret = core_clks_get(core); 799 if (ret) 800 return ret; 801 802 if (!res->vcodec_pmdomains_num) 803 legacy_binding = true; 804 805 dev_info(dev, "%s legacy binding\n", legacy_binding ? "" : "non"); 806 807 ret = vcodec_clks_get(core, dev, core->vcodec0_clks, res->vcodec0_clks); 808 if (ret) 809 return ret; 810 811 ret = vcodec_clks_get(core, dev, core->vcodec1_clks, res->vcodec1_clks); 812 if (ret) 813 return ret; 814 815 if (legacy_binding) 816 return 0; 817 818 ret = vcodec_domains_get(dev); 819 if (ret) 820 return ret; 821 822 return 0; 823 } 824 825 static void core_put_v4(struct device *dev) 826 { 827 if (legacy_binding) 828 return; 829 830 vcodec_domains_put(dev); 831 } 832 833 static int core_power_v4(struct device *dev, int on) 834 { 835 struct venus_core *core = dev_get_drvdata(dev); 836 int ret = 0; 837 838 if (on == POWER_ON) 839 ret = core_clks_enable(core); 840 else 841 core_clks_disable(core); 842 843 return ret; 844 } 845 846 static unsigned long calculate_inst_freq(struct venus_inst *inst, 847 unsigned long filled_len) 848 { 849 unsigned long vpp_freq = 0, vsp_freq = 0; 850 u32 fps = (u32)inst->fps; 851 u32 mbs_per_sec; 852 853 mbs_per_sec = load_per_instance(inst) / fps; 854 855 vpp_freq = mbs_per_sec * inst->clk_data.codec_freq_data->vpp_freq; 856 /* 21 / 20 is overhead factor */ 857 vpp_freq += vpp_freq / 20; 858 vsp_freq = mbs_per_sec * inst->clk_data.codec_freq_data->vsp_freq; 859 860 /* 10 / 7 is overhead factor */ 861 if (inst->session_type == VIDC_SESSION_TYPE_ENC) 862 vsp_freq += (inst->controls.enc.bitrate * 10) / 7; 863 else 864 vsp_freq += ((fps * filled_len * 8) * 10) / 7; 865 866 return max(vpp_freq, vsp_freq); 867 } 868 869 static int load_scale_v4(struct venus_inst *inst) 870 { 871 struct venus_core *core = inst->core; 872 const struct freq_tbl *table = core->res->freq_tbl; 873 unsigned int num_rows = core->res->freq_tbl_size; 874 struct device *dev = core->dev; 875 unsigned long freq = 0, freq_core1 = 0, freq_core2 = 0; 876 unsigned long filled_len = 0; 877 int i, ret; 878 879 for (i = 0; i < inst->num_input_bufs; i++) 880 filled_len = max(filled_len, inst->payloads[i]); 881 882 if (inst->session_type == VIDC_SESSION_TYPE_DEC && !filled_len) 883 return 0; 884 885 freq = calculate_inst_freq(inst, filled_len); 886 inst->clk_data.freq = freq; 887 888 mutex_lock(&core->lock); 889 list_for_each_entry(inst, &core->instances, list) { 890 if (inst->clk_data.core_id == VIDC_CORE_ID_1) { 891 freq_core1 += inst->clk_data.freq; 892 } else if (inst->clk_data.core_id == VIDC_CORE_ID_2) { 893 freq_core2 += inst->clk_data.freq; 894 } else if (inst->clk_data.core_id == VIDC_CORE_ID_3) { 895 freq_core1 += inst->clk_data.freq; 896 freq_core2 += inst->clk_data.freq; 897 } 898 } 899 mutex_unlock(&core->lock); 900 901 freq = max(freq_core1, freq_core2); 902 903 if (freq >= table[0].freq) { 904 freq = table[0].freq; 905 dev_warn(dev, "HW is overloaded, needed: %lu max: %lu\n", 906 freq, table[0].freq); 907 goto set_freq; 908 } 909 910 for (i = num_rows - 1 ; i >= 0; i--) { 911 if (freq <= table[i].freq) { 912 freq = table[i].freq; 913 break; 914 } 915 } 916 917 set_freq: 918 919 ret = core_clks_set_rate(core, freq); 920 if (ret) { 921 dev_err(dev, "failed to set clock rate %lu (%d)\n", 922 freq, ret); 923 return ret; 924 } 925 926 ret = load_scale_bw(core); 927 if (ret) { 928 dev_err(dev, "failed to set bandwidth (%d)\n", 929 ret); 930 return ret; 931 } 932 933 return 0; 934 } 935 936 static const struct venus_pm_ops pm_ops_v4 = { 937 .core_get = core_get_v4, 938 .core_put = core_put_v4, 939 .core_power = core_power_v4, 940 .vdec_get = vdec_get_v4, 941 .vdec_put = vdec_put_v4, 942 .vdec_power = vdec_power_v4, 943 .venc_get = venc_get_v4, 944 .venc_put = venc_put_v4, 945 .venc_power = venc_power_v4, 946 .coreid_power = coreid_power_v4, 947 .load_scale = load_scale_v4, 948 }; 949 950 const struct venus_pm_ops *venus_pm_get(enum hfi_version version) 951 { 952 switch (version) { 953 case HFI_VERSION_1XX: 954 default: 955 return &pm_ops_v1; 956 case HFI_VERSION_3XX: 957 return &pm_ops_v3; 958 case HFI_VERSION_4XX: 959 return &pm_ops_v4; 960 } 961 962 return NULL; 963 } 964