1 // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 4 #include "pvr_device.h" 5 #include "pvr_fw.h" 6 #include "pvr_fw_startstop.h" 7 #include "pvr_power.h" 8 #include "pvr_queue.h" 9 #include "pvr_rogue_fwif.h" 10 11 #include <drm/drm_drv.h> 12 #include <drm/drm_managed.h> 13 #include <drm/drm_print.h> 14 #include <linux/cleanup.h> 15 #include <linux/clk.h> 16 #include <linux/interrupt.h> 17 #include <linux/mutex.h> 18 #include <linux/of.h> 19 #include <linux/platform_device.h> 20 #include <linux/pm_domain.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/pwrseq/consumer.h> 23 #include <linux/reset.h> 24 #include <linux/timer.h> 25 #include <linux/types.h> 26 #include <linux/workqueue.h> 27 28 #define POWER_SYNC_TIMEOUT_US (1000000) /* 1s */ 29 30 #define WATCHDOG_TIME_MS (500) 31 32 /** 33 * pvr_device_lost() - Mark GPU device as lost 34 * @pvr_dev: Target PowerVR device. 35 * 36 * This will cause the DRM device to be unplugged. 37 */ 38 void 39 pvr_device_lost(struct pvr_device *pvr_dev) 40 { 41 if (!pvr_dev->lost) { 42 pvr_dev->lost = true; 43 drm_dev_unplug(from_pvr_device(pvr_dev)); 44 } 45 } 46 47 static int 48 pvr_power_send_command(struct pvr_device *pvr_dev, struct rogue_fwif_kccb_cmd *pow_cmd) 49 { 50 struct pvr_fw_device *fw_dev = &pvr_dev->fw_dev; 51 u32 slot_nr; 52 u32 value; 53 int err; 54 55 WRITE_ONCE(*fw_dev->power_sync, 0); 56 57 err = pvr_kccb_send_cmd_powered(pvr_dev, pow_cmd, &slot_nr); 58 if (err) 59 return err; 60 61 /* Wait for FW to acknowledge. */ 62 return readx_poll_timeout(READ_ONCE, 63 *pvr_dev->fw_dev.power_sync, value, value != 0, 100, 64 POWER_SYNC_TIMEOUT_US); 65 } 66 67 static int 68 pvr_power_request_idle(struct pvr_device *pvr_dev) 69 { 70 struct rogue_fwif_kccb_cmd pow_cmd; 71 72 /* Send FORCED_IDLE request to FW. */ 73 pow_cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_POW; 74 pow_cmd.cmd_data.pow_data.pow_type = ROGUE_FWIF_POW_FORCED_IDLE_REQ; 75 pow_cmd.cmd_data.pow_data.power_req_data.pow_request_type = ROGUE_FWIF_POWER_FORCE_IDLE; 76 77 return pvr_power_send_command(pvr_dev, &pow_cmd); 78 } 79 80 static int 81 pvr_power_request_pwr_off(struct pvr_device *pvr_dev) 82 { 83 struct rogue_fwif_kccb_cmd pow_cmd; 84 85 /* Send POW_OFF request to firmware. */ 86 pow_cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_POW; 87 pow_cmd.cmd_data.pow_data.pow_type = ROGUE_FWIF_POW_OFF_REQ; 88 pow_cmd.cmd_data.pow_data.power_req_data.forced = true; 89 90 return pvr_power_send_command(pvr_dev, &pow_cmd); 91 } 92 93 static int 94 pvr_power_fw_disable(struct pvr_device *pvr_dev, bool hard_reset, bool rpm_suspend) 95 { 96 int err; 97 98 if (!hard_reset) { 99 cancel_delayed_work_sync(&pvr_dev->watchdog.work); 100 101 err = pvr_power_request_idle(pvr_dev); 102 if (err) 103 return err; 104 105 err = pvr_power_request_pwr_off(pvr_dev); 106 if (err) 107 return err; 108 } 109 110 if (rpm_suspend) { 111 /* This also waits for late processing of GPU or firmware IRQs in other cores */ 112 disable_irq(pvr_dev->irq); 113 } 114 115 err = pvr_fw_stop(pvr_dev); 116 if (err && rpm_suspend) 117 enable_irq(pvr_dev->irq); 118 119 return err; 120 } 121 122 static int 123 pvr_power_fw_enable(struct pvr_device *pvr_dev, bool rpm_resume) 124 { 125 int err; 126 127 if (rpm_resume) 128 enable_irq(pvr_dev->irq); 129 130 err = pvr_fw_start(pvr_dev); 131 if (err) 132 goto out; 133 134 err = pvr_wait_for_fw_boot(pvr_dev); 135 if (err) { 136 drm_err(from_pvr_device(pvr_dev), "Firmware failed to boot\n"); 137 pvr_fw_stop(pvr_dev); 138 goto out; 139 } 140 141 queue_delayed_work(pvr_dev->sched_wq, &pvr_dev->watchdog.work, 142 msecs_to_jiffies(WATCHDOG_TIME_MS)); 143 144 return 0; 145 146 out: 147 if (rpm_resume) 148 disable_irq(pvr_dev->irq); 149 150 return err; 151 } 152 153 bool 154 pvr_power_is_idle(struct pvr_device *pvr_dev) 155 { 156 /* 157 * FW power state can be out of date if a KCCB command has been submitted but the FW hasn't 158 * started processing it yet. So also check the KCCB status. 159 */ 160 enum rogue_fwif_pow_state pow_state = READ_ONCE(pvr_dev->fw_dev.fwif_sysdata->pow_state); 161 bool kccb_idle = pvr_kccb_is_idle(pvr_dev); 162 163 return (pow_state == ROGUE_FWIF_POW_IDLE) && kccb_idle; 164 } 165 166 static bool 167 pvr_watchdog_kccb_stalled(struct pvr_device *pvr_dev) 168 { 169 /* Check KCCB commands are progressing. */ 170 u32 kccb_cmds_executed = pvr_dev->fw_dev.fwif_osdata->kccb_cmds_executed; 171 bool kccb_is_idle = pvr_kccb_is_idle(pvr_dev); 172 173 if (pvr_dev->watchdog.old_kccb_cmds_executed == kccb_cmds_executed && !kccb_is_idle) { 174 pvr_dev->watchdog.kccb_stall_count++; 175 176 /* 177 * If we have commands pending with no progress for 2 consecutive polls then 178 * consider KCCB command processing stalled. 179 */ 180 if (pvr_dev->watchdog.kccb_stall_count == 2) { 181 pvr_dev->watchdog.kccb_stall_count = 0; 182 return true; 183 } 184 } else if (pvr_dev->watchdog.old_kccb_cmds_executed == kccb_cmds_executed) { 185 bool has_active_contexts; 186 187 mutex_lock(&pvr_dev->queues.lock); 188 has_active_contexts = list_empty(&pvr_dev->queues.active); 189 mutex_unlock(&pvr_dev->queues.lock); 190 191 if (has_active_contexts) { 192 /* Send a HEALTH_CHECK command so we can verify FW is still alive. */ 193 struct rogue_fwif_kccb_cmd health_check_cmd; 194 195 health_check_cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_HEALTH_CHECK; 196 197 pvr_kccb_send_cmd_powered(pvr_dev, &health_check_cmd, NULL); 198 } 199 } else { 200 pvr_dev->watchdog.old_kccb_cmds_executed = kccb_cmds_executed; 201 pvr_dev->watchdog.kccb_stall_count = 0; 202 } 203 204 return false; 205 } 206 207 static void 208 pvr_watchdog_worker(struct work_struct *work) 209 { 210 struct pvr_device *pvr_dev = container_of(work, struct pvr_device, 211 watchdog.work.work); 212 bool stalled; 213 214 if (pvr_dev->lost) 215 return; 216 217 if (pm_runtime_get_if_in_use(from_pvr_device(pvr_dev)->dev) <= 0) 218 goto out_requeue; 219 220 if (!READ_ONCE(pvr_dev->fw_dev.initialised)) 221 goto out_pm_runtime_put; 222 223 stalled = pvr_watchdog_kccb_stalled(pvr_dev); 224 225 if (stalled) { 226 drm_err(from_pvr_device(pvr_dev), "FW stalled, trying hard reset"); 227 228 pvr_power_reset(pvr_dev, true); 229 /* Device may be lost at this point. */ 230 } 231 232 out_pm_runtime_put: 233 pm_runtime_put(from_pvr_device(pvr_dev)->dev); 234 235 out_requeue: 236 if (!pvr_dev->lost) { 237 queue_delayed_work(pvr_dev->sched_wq, &pvr_dev->watchdog.work, 238 msecs_to_jiffies(WATCHDOG_TIME_MS)); 239 } 240 } 241 242 /** 243 * pvr_watchdog_init() - Initialise watchdog for device 244 * @pvr_dev: Target PowerVR device. 245 * 246 * Returns: 247 * * 0 on success, or 248 * * -%ENOMEM on out of memory. 249 */ 250 int 251 pvr_watchdog_init(struct pvr_device *pvr_dev) 252 { 253 INIT_DELAYED_WORK(&pvr_dev->watchdog.work, pvr_watchdog_worker); 254 255 return 0; 256 } 257 258 static int pvr_power_init_manual(struct pvr_device *pvr_dev) 259 { 260 struct drm_device *drm_dev = from_pvr_device(pvr_dev); 261 struct reset_control *reset; 262 263 reset = devm_reset_control_get_optional_exclusive(drm_dev->dev, NULL); 264 if (IS_ERR(reset)) 265 return dev_err_probe(drm_dev->dev, PTR_ERR(reset), 266 "failed to get gpu reset line\n"); 267 268 pvr_dev->reset = reset; 269 270 return 0; 271 } 272 273 static int pvr_power_on_sequence_manual(struct pvr_device *pvr_dev) 274 { 275 int err; 276 277 err = clk_prepare_enable(pvr_dev->core_clk); 278 if (err) 279 return err; 280 281 err = clk_prepare_enable(pvr_dev->sys_clk); 282 if (err) 283 goto err_core_clk_disable; 284 285 err = clk_prepare_enable(pvr_dev->mem_clk); 286 if (err) 287 goto err_sys_clk_disable; 288 289 /* 290 * According to the hardware manual, a delay of at least 32 clock 291 * cycles is required between de-asserting the clkgen reset and 292 * de-asserting the GPU reset. Assuming a worst-case scenario with 293 * a very high GPU clock frequency, a delay of 1 microsecond is 294 * sufficient to ensure this requirement is met across all 295 * feasible GPU clock speeds. 296 */ 297 udelay(1); 298 299 err = reset_control_deassert(pvr_dev->reset); 300 if (err) 301 goto err_mem_clk_disable; 302 303 return 0; 304 305 err_mem_clk_disable: 306 clk_disable_unprepare(pvr_dev->mem_clk); 307 308 err_sys_clk_disable: 309 clk_disable_unprepare(pvr_dev->sys_clk); 310 311 err_core_clk_disable: 312 clk_disable_unprepare(pvr_dev->core_clk); 313 314 return err; 315 } 316 317 static int pvr_power_off_sequence_manual(struct pvr_device *pvr_dev) 318 { 319 int err; 320 321 err = reset_control_assert(pvr_dev->reset); 322 323 clk_disable_unprepare(pvr_dev->mem_clk); 324 clk_disable_unprepare(pvr_dev->sys_clk); 325 clk_disable_unprepare(pvr_dev->core_clk); 326 327 return err; 328 } 329 330 const struct pvr_power_sequence_ops pvr_power_sequence_ops_manual = { 331 .init = pvr_power_init_manual, 332 .power_on = pvr_power_on_sequence_manual, 333 .power_off = pvr_power_off_sequence_manual, 334 }; 335 336 static int pvr_power_init_pwrseq(struct pvr_device *pvr_dev) 337 { 338 struct device *dev = from_pvr_device(pvr_dev)->dev; 339 340 pvr_dev->pwrseq = devm_pwrseq_get(dev, "gpu-power"); 341 if (IS_ERR(pvr_dev->pwrseq)) { 342 /* 343 * This platform requires a sequencer. If we can't get it, we 344 * must return the error (including -EPROBE_DEFER to wait for 345 * the provider to appear) 346 */ 347 return dev_err_probe(dev, PTR_ERR(pvr_dev->pwrseq), 348 "Failed to get required power sequencer\n"); 349 } 350 351 return 0; 352 } 353 354 static int pvr_power_on_sequence_pwrseq(struct pvr_device *pvr_dev) 355 { 356 return pwrseq_enable(pvr_dev->pwrseq); 357 } 358 359 static int pvr_power_off_sequence_pwrseq(struct pvr_device *pvr_dev) 360 { 361 return pwrseq_disable(pvr_dev->pwrseq); 362 } 363 364 const struct pvr_power_sequence_ops pvr_power_sequence_ops_pwrseq = { 365 .init = pvr_power_init_pwrseq, 366 .power_on = pvr_power_on_sequence_pwrseq, 367 .power_off = pvr_power_off_sequence_pwrseq, 368 }; 369 370 int 371 pvr_power_device_suspend(struct device *dev) 372 { 373 struct platform_device *plat_dev = to_platform_device(dev); 374 struct drm_device *drm_dev = platform_get_drvdata(plat_dev); 375 struct pvr_device *pvr_dev = to_pvr_device(drm_dev); 376 int err = 0; 377 int idx; 378 379 if (!drm_dev_enter(drm_dev, &idx)) 380 return -EIO; 381 382 if (READ_ONCE(pvr_dev->fw_dev.initialised)) { 383 err = pvr_power_fw_disable(pvr_dev, false, true); 384 if (err) 385 goto err_drm_dev_exit; 386 } 387 388 err = pvr_dev->device_data->pwr_ops->power_off(pvr_dev); 389 390 err_drm_dev_exit: 391 drm_dev_exit(idx); 392 393 return err; 394 } 395 396 int 397 pvr_power_device_resume(struct device *dev) 398 { 399 struct platform_device *plat_dev = to_platform_device(dev); 400 struct drm_device *drm_dev = platform_get_drvdata(plat_dev); 401 struct pvr_device *pvr_dev = to_pvr_device(drm_dev); 402 int idx; 403 int err; 404 405 if (!drm_dev_enter(drm_dev, &idx)) 406 return -EIO; 407 408 err = pvr_dev->device_data->pwr_ops->power_on(pvr_dev); 409 if (err) 410 goto err_drm_dev_exit; 411 412 if (READ_ONCE(pvr_dev->fw_dev.initialised)) { 413 err = pvr_power_fw_enable(pvr_dev, true); 414 if (err) 415 goto err_power_off; 416 } 417 418 drm_dev_exit(idx); 419 420 return 0; 421 422 err_power_off: 423 pvr_dev->device_data->pwr_ops->power_off(pvr_dev); 424 425 err_drm_dev_exit: 426 drm_dev_exit(idx); 427 428 return err; 429 } 430 431 int 432 pvr_power_device_idle(struct device *dev) 433 { 434 struct platform_device *plat_dev = to_platform_device(dev); 435 struct drm_device *drm_dev = platform_get_drvdata(plat_dev); 436 struct pvr_device *pvr_dev = to_pvr_device(drm_dev); 437 438 return pvr_power_is_idle(pvr_dev) ? 0 : -EBUSY; 439 } 440 441 static int 442 pvr_power_clear_error(struct pvr_device *pvr_dev) 443 { 444 struct device *dev = from_pvr_device(pvr_dev)->dev; 445 int err; 446 447 /* Ensure the device state is known and nothing is happening past this point */ 448 pm_runtime_disable(dev); 449 450 /* Attempt to clear the runtime PM error by setting the current state again */ 451 if (pm_runtime_status_suspended(dev)) 452 err = pm_runtime_set_suspended(dev); 453 else 454 err = pm_runtime_set_active(dev); 455 456 if (err) { 457 drm_err(from_pvr_device(pvr_dev), 458 "%s: Failed to clear runtime PM error (new error %d)\n", 459 __func__, err); 460 } 461 462 pm_runtime_enable(dev); 463 464 return err; 465 } 466 467 /** 468 * pvr_power_get_clear() - Acquire a power reference, correcting any errors 469 * @pvr_dev: Device pointer 470 * 471 * Attempt to acquire a power reference on the device. If the runtime PM 472 * is in error state, attempt to clear the error and retry. 473 * 474 * Returns: 475 * * 0 on success, or 476 * * Any error code returned by pvr_power_get() or the runtime PM API. 477 */ 478 static int 479 pvr_power_get_clear(struct pvr_device *pvr_dev) 480 { 481 int err; 482 483 err = pvr_power_get(pvr_dev); 484 if (err == 0) 485 return err; 486 487 drm_warn(from_pvr_device(pvr_dev), 488 "%s: pvr_power_get returned error %d, attempting recovery\n", 489 __func__, err); 490 491 err = pvr_power_clear_error(pvr_dev); 492 if (err) 493 return err; 494 495 return pvr_power_get(pvr_dev); 496 } 497 498 /** 499 * pvr_power_reset() - Reset the GPU 500 * @pvr_dev: Device pointer 501 * @hard_reset: %true for hard reset, %false for soft reset 502 * 503 * If @hard_reset is %false and the FW processor fails to respond during the reset process, this 504 * function will attempt a hard reset. 505 * 506 * If a hard reset fails then the GPU device is reported as lost. 507 * 508 * Returns: 509 * * 0 on success, or 510 * * Any error code returned by pvr_power_get, pvr_power_fw_disable or pvr_power_fw_enable(). 511 */ 512 int 513 pvr_power_reset(struct pvr_device *pvr_dev, bool hard_reset) 514 { 515 bool queues_disabled = false; 516 int err; 517 518 /* 519 * Take a power reference during the reset. This should prevent any interference with the 520 * power state during reset. 521 */ 522 WARN_ON(pvr_power_get_clear(pvr_dev)); 523 524 down_write(&pvr_dev->reset_sem); 525 526 if (pvr_dev->lost) { 527 err = -EIO; 528 goto err_up_write; 529 } 530 531 /* Disable IRQs for the duration of the reset. */ 532 if (hard_reset) { 533 disable_irq(pvr_dev->irq); 534 } else { 535 /* 536 * Soft reset is triggered as a response to a FW command to the Host and is 537 * processed from the threaded IRQ handler. This code cannot (nor needs to) 538 * wait for any IRQ processing to complete. 539 */ 540 disable_irq_nosync(pvr_dev->irq); 541 } 542 543 do { 544 if (hard_reset) { 545 pvr_queue_device_pre_reset(pvr_dev); 546 queues_disabled = true; 547 } 548 549 err = pvr_power_fw_disable(pvr_dev, hard_reset, false); 550 if (!err) { 551 if (hard_reset) { 552 WRITE_ONCE(pvr_dev->fw_dev.initialised, false); 553 WARN_ON(pvr_power_device_suspend(from_pvr_device(pvr_dev)->dev)); 554 555 err = pvr_fw_hard_reset(pvr_dev); 556 if (err) 557 goto err_device_lost; 558 559 err = pvr_power_device_resume(from_pvr_device(pvr_dev)->dev); 560 WRITE_ONCE(pvr_dev->fw_dev.initialised, true); 561 if (err) 562 goto err_device_lost; 563 } else { 564 /* Clear the FW faulted flags. */ 565 pvr_dev->fw_dev.fwif_sysdata->hwr_state_flags &= 566 ~(ROGUE_FWIF_HWR_FW_FAULT | 567 ROGUE_FWIF_HWR_RESTART_REQUESTED); 568 } 569 570 pvr_fw_irq_clear(pvr_dev); 571 572 err = pvr_power_fw_enable(pvr_dev, false); 573 } 574 575 if (err && hard_reset) 576 goto err_device_lost; 577 578 if (err && !hard_reset) { 579 drm_err(from_pvr_device(pvr_dev), "FW stalled, trying hard reset"); 580 hard_reset = true; 581 } 582 } while (err); 583 584 if (queues_disabled) 585 pvr_queue_device_post_reset(pvr_dev); 586 587 enable_irq(pvr_dev->irq); 588 589 up_write(&pvr_dev->reset_sem); 590 591 pvr_power_put(pvr_dev); 592 593 return 0; 594 595 err_device_lost: 596 drm_err(from_pvr_device(pvr_dev), "GPU device lost"); 597 pvr_device_lost(pvr_dev); 598 599 /* Leave IRQs disabled if the device is lost. */ 600 601 if (queues_disabled) 602 pvr_queue_device_post_reset(pvr_dev); 603 604 err_up_write: 605 up_write(&pvr_dev->reset_sem); 606 607 pvr_power_put(pvr_dev); 608 609 return err; 610 } 611 612 /** 613 * pvr_watchdog_fini() - Shutdown watchdog for device 614 * @pvr_dev: Target PowerVR device. 615 */ 616 void 617 pvr_watchdog_fini(struct pvr_device *pvr_dev) 618 { 619 cancel_delayed_work_sync(&pvr_dev->watchdog.work); 620 } 621 622 int pvr_power_domains_init(struct pvr_device *pvr_dev) 623 { 624 static const char *const ROGUE_PD_NAMES[] = { "a", "b", "c", "d", "e" }; 625 626 struct drm_device *drm_dev = from_pvr_device(pvr_dev); 627 struct device *dev = drm_dev->dev; 628 629 struct dev_pm_domain_list *domains = NULL; 630 struct device_link **domain_links = NULL; 631 int domain_count; 632 int link_count; 633 634 int err; 635 int i; 636 637 domain_count = of_count_phandle_with_args(dev->of_node, "power-domains", 638 "#power-domain-cells"); 639 if (domain_count < 0) { 640 err = domain_count; 641 goto out; 642 } 643 644 if (domain_count <= 1) { 645 err = 0; 646 goto out; 647 } 648 649 if (domain_count > ARRAY_SIZE(ROGUE_PD_NAMES)) { 650 drm_err(drm_dev, "%s() only supports %zu domains on Rogue", 651 __func__, ARRAY_SIZE(ROGUE_PD_NAMES)); 652 err = -EOPNOTSUPP; 653 goto out; 654 } 655 656 link_count = domain_count - 1; 657 658 domain_links = kzalloc_objs(*domain_links, link_count); 659 if (!domain_links) { 660 err = -ENOMEM; 661 goto out; 662 } 663 664 const struct dev_pm_domain_attach_data pd_attach_data = { 665 .pd_names = ROGUE_PD_NAMES, 666 .num_pd_names = domain_count, 667 .pd_flags = 0, 668 }; 669 670 err = dev_pm_domain_attach_list(dev, &pd_attach_data, &domains); 671 if (err < 0) 672 goto err_free_links; 673 674 for (i = 0; i < link_count; i++) { 675 struct device_link *link; 676 677 link = device_link_add(domains->pd_devs[i + 1], 678 domains->pd_devs[i], 679 DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME); 680 if (!link) { 681 err = -ENODEV; 682 goto err_unlink; 683 } 684 685 domain_links[i] = link; 686 } 687 688 err = 0; 689 goto out; 690 691 err_unlink: 692 while (--i >= 0) 693 device_link_del(domain_links[i]); 694 695 dev_pm_domain_detach_list(domains); 696 domains = NULL; 697 698 err_free_links: 699 kfree(domain_links); 700 domain_links = NULL; 701 702 out: 703 pvr_dev->power = (struct pvr_device_power){ 704 .domains = domains, 705 .domain_links = domain_links, 706 }; 707 708 return err; 709 } 710 711 void pvr_power_domains_fini(struct pvr_device *pvr_dev) 712 { 713 struct pvr_device_power *pvr_power = &pvr_dev->power; 714 715 if (!pvr_power->domains) 716 goto out; 717 718 for (int i = (int)pvr_power->domains->num_pds - 2; i >= 0; --i) 719 device_link_del(pvr_power->domain_links[i]); 720 721 dev_pm_domain_detach_list(pvr_power->domains); 722 723 kfree(pvr_power->domain_links); 724 725 out: 726 *pvr_power = (struct pvr_device_power){ 0 }; 727 } 728