// SPDX-License-Identifier: MIT /* * Copyright © 2022 Intel Corporation */ #include "xe_pm.h" #include <linux/pm_runtime.h> #include <drm/drm_managed.h> #include <drm/ttm/ttm_placement.h> #include "display/xe_display.h" #include "xe_bo.h" #include "xe_bo_evict.h" #include "xe_device.h" #include "xe_device_sysfs.h" #include "xe_ggtt.h" #include "xe_gt.h" #include "xe_guc.h" #include "xe_irq.h" #include "xe_pcode.h" #include "xe_wa.h" /** * DOC: Xe Power Management * * Xe PM implements the main routines for both system level suspend states and * for the opportunistic runtime suspend states. * * System Level Suspend (S-States) - In general this is OS initiated suspend * driven by ACPI for achieving S0ix (a.k.a. S2idle, freeze), S3 (suspend to ram), * S4 (disk). The main functions here are `xe_pm_suspend` and `xe_pm_resume`. They * are the main point for the suspend to and resume from these states. * * PCI Device Suspend (D-States) - This is the opportunistic PCIe device low power * state D3, controlled by the PCI subsystem and ACPI with the help from the * runtime_pm infrastructure. * PCI D3 is special and can mean D3hot, where Vcc power is on for keeping memory * alive and quicker low latency resume or D3Cold where Vcc power is off for * better power savings. * The Vcc control of PCI hierarchy can only be controlled at the PCI root port * level, while the device driver can be behind multiple bridges/switches and * paired with other devices. For this reason, the PCI subsystem cannot perform * the transition towards D3Cold. The lowest runtime PM possible from the PCI * subsystem is D3hot. Then, if all these paired devices in the same root port * are in D3hot, ACPI will assist here and run its own methods (_PR3 and _OFF) * to perform the transition from D3hot to D3cold. Xe may disallow this * transition by calling pci_d3cold_disable(root_pdev) before going to runtime * suspend. It will be based on runtime conditions such as VRAM usage for a * quick and low latency resume for instance. * * Runtime PM - This infrastructure provided by the Linux kernel allows the * device drivers to indicate when the can be runtime suspended, so the device * could be put at D3 (if supported), or allow deeper package sleep states * (PC-states), and/or other low level power states. Xe PM component provides * `xe_pm_runtime_suspend` and `xe_pm_runtime_resume` functions that PCI * subsystem will call before transition to/from runtime suspend. * * Also, Xe PM provides get and put functions that Xe driver will use to * indicate activity. In order to avoid locking complications with the memory * management, whenever possible, these get and put functions needs to be called * from the higher/outer levels. * The main cases that need to be protected from the outer levels are: IOCTL, * sysfs, debugfs, dma-buf sharing, GPU execution. * * This component is not responsible for GT idleness (RC6) nor GT frequency * management (RPS). */ #ifdef CONFIG_LOCKDEP static struct lockdep_map xe_pm_runtime_lockdep_map = …; #endif /** * xe_pm_suspend - Helper for System suspend, i.e. S0->S3 / S0->S2idle * @xe: xe device instance * * Return: 0 on success */ int xe_pm_suspend(struct xe_device *xe) { … } /** * xe_pm_resume - Helper for System resume S3->S0 / S2idle->S0 * @xe: xe device instance * * Return: 0 on success */ int xe_pm_resume(struct xe_device *xe) { … } static bool xe_pm_pci_d3cold_capable(struct xe_device *xe) { … } static void xe_pm_runtime_init(struct xe_device *xe) { … } int xe_pm_init_early(struct xe_device *xe) { … } /** * xe_pm_init - Initialize Xe Power Management * @xe: xe device instance * * This component is responsible for System and Device sleep states. * * Returns 0 for success, negative error code otherwise. */ int xe_pm_init(struct xe_device *xe) { … } /** * xe_pm_runtime_fini - Finalize Runtime PM * @xe: xe device instance */ void xe_pm_runtime_fini(struct xe_device *xe) { … } static void xe_pm_write_callback_task(struct xe_device *xe, struct task_struct *task) { … } struct task_struct *xe_pm_read_callback_task(struct xe_device *xe) { … } /** * xe_pm_runtime_suspended - Check if runtime_pm state is suspended * @xe: xe device instance * * This does not provide any guarantee that the device is going to remain * suspended as it might be racing with the runtime state transitions. * It can be used only as a non-reliable assertion, to ensure that we are not in * the sleep state while trying to access some memory for instance. * * Returns true if PCI device is suspended, false otherwise. */ bool xe_pm_runtime_suspended(struct xe_device *xe) { … } /** * xe_pm_runtime_suspend - Prepare our device for D3hot/D3Cold * @xe: xe device instance * * Returns 0 for success, negative error code otherwise. */ int xe_pm_runtime_suspend(struct xe_device *xe) { … } /** * xe_pm_runtime_resume - Waking up from D3hot/D3Cold * @xe: xe device instance * * Returns 0 for success, negative error code otherwise. */ int xe_pm_runtime_resume(struct xe_device *xe) { … } /* * For places where resume is synchronous it can be quite easy to deadlock * if we are not careful. Also in practice it might be quite timing * sensitive to ever see the 0 -> 1 transition with the callers locks * held, so deadlocks might exist but are hard for lockdep to ever see. * With this in mind, help lockdep learn about the potentially scary * stuff that can happen inside the runtime_resume callback by acquiring * a dummy lock (it doesn't protect anything and gets compiled out on * non-debug builds). Lockdep then only needs to see the * xe_pm_runtime_lockdep_map -> runtime_resume callback once, and then can * hopefully validate all the (callers_locks) -> xe_pm_runtime_lockdep_map. * For example if the (callers_locks) are ever grabbed in the * runtime_resume callback, lockdep should give us a nice splat. */ static void pm_runtime_lockdep_prime(void) { … } /** * xe_pm_runtime_get - Get a runtime_pm reference and resume synchronously * @xe: xe device instance */ void xe_pm_runtime_get(struct xe_device *xe) { … } /** * xe_pm_runtime_put - Put the runtime_pm reference back and mark as idle * @xe: xe device instance */ void xe_pm_runtime_put(struct xe_device *xe) { … } /** * xe_pm_runtime_get_ioctl - Get a runtime_pm reference before ioctl * @xe: xe device instance * * Returns: Any number greater than or equal to 0 for success, negative error * code otherwise. */ int xe_pm_runtime_get_ioctl(struct xe_device *xe) { … } /** * xe_pm_runtime_get_if_active - Get a runtime_pm reference if device active * @xe: xe device instance * * Return: True if device is awake (regardless the previous number of references) * and a new reference was taken, false otherwise. */ bool xe_pm_runtime_get_if_active(struct xe_device *xe) { … } /** * xe_pm_runtime_get_if_in_use - Get a new reference if device is active with previous ref taken * @xe: xe device instance * * Return: True if device is awake, a previous reference had been already taken, * and a new reference was now taken, false otherwise. */ bool xe_pm_runtime_get_if_in_use(struct xe_device *xe) { … } /** * xe_pm_runtime_get_noresume - Bump runtime PM usage counter without resuming * @xe: xe device instance * * This function should be used in inner places where it is surely already * protected by outer-bound callers of `xe_pm_runtime_get`. * It will warn if not protected. * The reference should be put back after this function regardless, since it * will always bump the usage counter, regardless. */ void xe_pm_runtime_get_noresume(struct xe_device *xe) { … } /** * xe_pm_runtime_resume_and_get - Resume, then get a runtime_pm ref if awake. * @xe: xe device instance * * Returns: True if device is awake and the reference was taken, false otherwise. */ bool xe_pm_runtime_resume_and_get(struct xe_device *xe) { … } /** * xe_pm_assert_unbounded_bridge - Disable PM on unbounded pcie parent bridge * @xe: xe device instance */ void xe_pm_assert_unbounded_bridge(struct xe_device *xe) { … } /** * xe_pm_set_vram_threshold - Set a vram threshold for allowing/blocking D3Cold * @xe: xe device instance * @threshold: VRAM size in bites for the D3cold threshold * * Returns 0 for success, negative error code otherwise. */ int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold) { … } /** * xe_pm_d3cold_allowed_toggle - Check conditions to toggle d3cold.allowed * @xe: xe device instance * * To be called during runtime_pm idle callback. * Check for all the D3Cold conditions ahead of runtime suspend. */ void xe_pm_d3cold_allowed_toggle(struct xe_device *xe) { … }