# Runtime Power Management

_Peios Kernel / Hardware and drivers / Graphics / xe_

> Reference for Runtime Power Management.

<a id="rst-runtime-power-management"></a>

<!-- SPDX-License-Identifier: (GPL-2.0+ OR MIT) -->


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).


<a id="rst-internal-api"></a>

## Internal API


#### <a id="c-xe_pm_might_block_on_suspend"></a>`xe_pm_might_block_on_suspend()`

```c
void xe_pm_might_block_on_suspend(void)
```

Annotate that the code might block on suspend

**Parameters**

- `void`: no arguments

**Description**

Annotation to use where the code might block or seize to make
progress pending resume completion.

#### <a id="c-xe_pm_block_on_suspend"></a>`xe_pm_block_on_suspend()`

```c
int xe_pm_block_on_suspend(struct xe_device *xe)
```

Block pending suspend.

**Parameters**

- `struct xe_device *xe`: The xe device about to be suspended.

**Description**

Block if the pm notifier has start evicting bos, to avoid
racing and validating those bos back. The function is
annotated to ensure no locks are held that are also grabbed
in the pm notifier or the device suspend / resume.
This is intended to be used by freezable tasks only.
(Not freezable workqueues), with the intention that the function
returns `-ERESTARTSYS` when tasks are frozen during suspend,
and allows the task to freeze. The caller must be able to
handle the `-ERESTARTSYS`.

**Return**

`0` on success, `-ERESTARTSYS` on signal pending or
if freezing requested.

#### <a id="c-xe_rpm_reclaim_safe"></a>`xe_rpm_reclaim_safe()`

```c
bool xe_rpm_reclaim_safe(const struct xe_device *xe)
```

Whether runtime resume can be done from reclaim context

**Parameters**

- `const struct xe_device *xe`: The xe device.

**Return**

true if it is safe to runtime resume from reclaim context.
false otherwise.

#### <a id="c-xe_pm_suspend"></a>`xe_pm_suspend()`

```c
int xe_pm_suspend(struct xe_device *xe)
```

Helper for System suspend, i.e. S0->S3 / S0->S2idle

**Parameters**

- `struct xe_device *xe`: xe device instance

**Return**

0 on success

#### <a id="c-xe_pm_resume"></a>`xe_pm_resume()`

```c
int xe_pm_resume(struct xe_device *xe)
```

Helper for System resume S3->S0 / S2idle->S0

**Parameters**

- `struct xe_device *xe`: xe device instance

**Return**

0 on success

#### <a id="c-xe_pm_init"></a>`xe_pm_init()`

```c
int xe_pm_init(struct xe_device *xe)
```

Initialize Xe Power Management

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

This component is responsible for System and Device sleep states.

Returns 0 for success, negative error code otherwise.

#### <a id="c-xe_pm_fini"></a>`xe_pm_fini()`

```c
void xe_pm_fini(struct xe_device *xe)
```

Finalize PM

**Parameters**

- `struct xe_device *xe`: xe device instance

#### <a id="c-xe_pm_runtime_suspended"></a>`xe_pm_runtime_suspended()`

```c
bool xe_pm_runtime_suspended(struct xe_device *xe)
```

Check if runtime_pm state is suspended

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

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.

#### <a id="c-xe_pm_runtime_suspend"></a>`xe_pm_runtime_suspend()`

```c
int xe_pm_runtime_suspend(struct xe_device *xe)
```

Prepare our device for D3hot/D3Cold

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

Returns 0 for success, negative error code otherwise.

#### <a id="c-xe_pm_runtime_resume"></a>`xe_pm_runtime_resume()`

```c
int xe_pm_runtime_resume(struct xe_device *xe)
```

Waking up from D3hot/D3Cold

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

Returns 0 for success, negative error code otherwise.

#### <a id="c-xe_pm_runtime_get"></a>`xe_pm_runtime_get()`

```c
void xe_pm_runtime_get(struct xe_device *xe)
```

Get a runtime_pm reference and resume synchronously

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

When possible, scope-based runtime PM (through guard(xe_pm_runtime)) is
be preferred over direct usage of this function.  Manual get/put handling
should only be used when the function contains goto-based logic which
can break scope-based handling, or when the lifetime of the runtime PM
reference does not match a specific scope (e.g., runtime PM obtained in one
function and released in a different one).

#### <a id="c-xe_pm_runtime_put"></a>`xe_pm_runtime_put()`

```c
void xe_pm_runtime_put(struct xe_device *xe)
```

Put the runtime_pm reference back and mark as idle

**Parameters**

- `struct xe_device *xe`: xe device instance

#### <a id="c-xe_pm_runtime_get_ioctl"></a>`xe_pm_runtime_get_ioctl()`

```c
int xe_pm_runtime_get_ioctl(struct xe_device *xe)
```

Get a runtime_pm reference before ioctl

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

When possible, scope-based runtime PM (through
ACQUIRE(xe_pm_runtime_ioctl, ...)) is be preferred over direct usage of this
function.  Manual get/put handling should only be used when the function
contains goto-based logic which can break scope-based handling, or when the
lifetime of the runtime PM reference does not match a specific scope (e.g.,
runtime PM obtained in one function and released in a different one).

**Return**

Any number greater than or equal to 0 for success, negative error
code otherwise.

#### <a id="c-xe_pm_runtime_get_if_active"></a>`xe_pm_runtime_get_if_active()`

```c
bool xe_pm_runtime_get_if_active(struct xe_device *xe)
```

Get a runtime_pm reference if device active

**Parameters**

- `struct xe_device *xe`: xe device instance

**Return**

True if device is awake (regardless the previous number of references)
and a new reference was taken, false otherwise.

#### <a id="c-xe_pm_runtime_get_if_in_use"></a>`xe_pm_runtime_get_if_in_use()`

```c
bool xe_pm_runtime_get_if_in_use(struct xe_device *xe)
```

Get a new reference if device is active with previous ref taken

**Parameters**

- `struct xe_device *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.

#### <a id="c-xe_pm_runtime_get_noresume"></a>`xe_pm_runtime_get_noresume()`

```c
void xe_pm_runtime_get_noresume(struct xe_device *xe)
```

Bump runtime PM usage counter without resuming

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

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.

When possible, scope-based runtime PM (through guard(xe_pm_runtime_noresume))
is be preferred over direct usage of this function.  Manual get/put handling
should only be used when the function contains goto-based logic which can
break scope-based handling, or when the lifetime of the runtime PM reference
does not match a specific scope (e.g., runtime PM obtained in one function
and released in a different one).

#### <a id="c-xe_pm_runtime_resume_and_get"></a>`xe_pm_runtime_resume_and_get()`

```c
bool xe_pm_runtime_resume_and_get(struct xe_device *xe)
```

Resume, then get a runtime_pm ref if awake.

**Parameters**

- `struct xe_device *xe`: xe device instance

**Return**

True if device is awake and the reference was taken, false otherwise.

#### <a id="c-xe_pm_assert_unbounded_bridge"></a>`xe_pm_assert_unbounded_bridge()`

```c
void xe_pm_assert_unbounded_bridge(struct xe_device *xe)
```

Disable PM on unbounded pcie parent bridge

**Parameters**

- `struct xe_device *xe`: xe device instance

#### <a id="c-xe_pm_set_vram_threshold"></a>`xe_pm_set_vram_threshold()`

```c
int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold)
```

Set a VRAM threshold for allowing/blocking D3Cold

**Parameters**

- `struct xe_device *xe`: xe device instance
- `u32 threshold`: VRAM size in MiB for the D3cold threshold

**Return**

* 0          - success
* -EINVAL    - invalid argument

#### <a id="c-xe_pm_d3cold_allowed_toggle"></a>`xe_pm_d3cold_allowed_toggle()`

```c
void xe_pm_d3cold_allowed_toggle(struct xe_device *xe)
```

Check conditions to toggle d3cold.allowed

**Parameters**

- `struct xe_device *xe`: xe device instance

**Description**

To be called during runtime_pm idle callback.
Check for all the D3Cold conditions ahead of runtime suspend.

#### <a id="c-xe_pm_module_init"></a>`xe_pm_module_init()`

```c
int xe_pm_module_init(void)
```

Perform xe_pm specific module initialization.

**Parameters**

- `void`: no arguments

**Return**

0 on success. Currently doesn't fail.
