# Hardware workarounds

_Peios Kernel / Hardware and drivers / Graphics / xe_

> Reference for Hardware workarounds.

<a id="rst-hardware-workarounds"></a>

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


Hardware workarounds are register programming documented to be executed in
the driver that fall outside of the normal programming sequences for a
platform. There are some basic categories of workarounds, depending on
how/when they are applied:

- LRC workarounds: workarounds that touch registers that are
  saved/restored to/from the HW context image. The list is emitted (via Load
  Register Immediate commands) once when initializing the device and saved in
  the default context. That default context is then used on every context
  creation to have a "primed golden context", i.e. a context image that
  already contains the changes needed to all the registers. See
  drivers/gpu/drm/xe/xe_lrc.c for default context handling.

- Engine workarounds: the list of these WAs is applied whenever the specific
  engine is reset. It's also possible that a set of engine classes share a
  common power domain and they are reset together. This happens on some
  platforms with render and compute engines. In this case (at least) one of
  them need to keeep the workaround programming: the approach taken in the
  driver is to tie those workarounds to the first compute/render engine that
  is registered.  When executing with GuC submission, engine resets are
  outside of kernel driver control, hence the list of registers involved is
  written once, on engine initialization, and then passed to GuC, that
  saves/restores their values before/after the reset takes place. See
  drivers/gpu/drm/xe/xe_guc_ads.c for reference.

- GT workarounds: the list of these WAs is applied whenever these registers
  revert to their default values: on GPU reset, suspend/resume \[1\]_, etc.

- Register whitelist: some workarounds need to be implemented in userspace,
  but need to touch privileged registers. The whitelist in the kernel
  instructs the hardware to allow the access to happen. From the kernel side,
  this is just a special case of a MMIO workaround (as we write the list of
  these to/be-whitelisted registers to some special HW registers).

- Workaround batchbuffers: buffers that get executed automatically by the
  hardware on every HW context restore. These buffers are created and
  programmed in the default context so the hardware always go through those
  programming sequences when switching contexts. The support for workaround
  batchbuffers is enabled via these hardware mechanisms:

  1. INDIRECT_CTX (also known as **mid context restore bb**): A batchbuffer
     and an offset are provided in the default context, pointing the hardware
     to jump to that location when that offset is reached in the context
     restore.  When a context is being restored, this is executed after the
     ring context, in the middle (or beginning) of the engine context image.

  1. BB_PER_CTX_PTR (also known as **post context restore bb**): A
     batchbuffer is provided in the default context, pointing the hardware to
     a buffer to continue executing after the engine registers are restored
     in a context restore sequence.

  Below is the timeline for a context restore sequence:

```text
                  INDIRECT_CTX_OFFSET
             |----------->|
.------------.------------.-------------.------------.--------------.-----------.
|Ring        | Engine     | Mid-context | Engine     | Post-context | Ring      |
|Restore     | Restore (1)| BB Restore  | Restore (2)| BB Restore   | Execution |
`------------'------------'-------------'------------'--------------'-----------'
```

- Other/OOB:  There are WAs that, due to their nature, cannot be applied from
  a central place. Those are peppered around the rest of the code, as needed.
  There's a central place to control which workarounds are enabled:
  drivers/gpu/drm/xe/xe_wa_oob.rules for GT workarounds and
  drivers/gpu/drm/xe/xe_device_wa_oob.rules for device/SoC workarounds.
  These files only record which workarounds are enabled: during early device
  initialization those rules are evaluated and recorded by the driver. Then
  later the driver checks with `XE_GT_WA()` and `XE_DEVICE_WA()` to
  implement them.

> [!NOTE]
> Hardware workarounds in xe work the same way as in i915, with the
> difference of how they are maintained in the code. In xe it uses the
> xe_rtp infrastructure so the workarounds can be kept in tables, following
> a more declarative approach rather than procedural.


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

## Internal API


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

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

process OOB workaround table

**Parameters**

- `struct xe_device *xe`: device instance to process workarounds for

**Description**

process OOB workaround table for this device, marking in **xe** the
workarounds that are active.

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

```c
void xe_wa_process_gt_oob(struct xe_gt *gt)
```

process GT OOB workaround table

**Parameters**

- `struct xe_gt *gt`: GT instance to process workarounds for

**Description**

Process OOB workaround table for this platform, marking in **gt** the
workarounds that are active.

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

```c
void xe_wa_process_gt(struct xe_gt *gt)
```

process GT workaround table

**Parameters**

- `struct xe_gt *gt`: GT instance to process workarounds for

**Description**

Process GT workaround table for this platform, saving in **gt** all the
workarounds that need to be applied at the GT level.

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

```c
void xe_wa_process_engine(struct xe_hw_engine *hwe)
```

process engine workaround table

**Parameters**

- `struct xe_hw_engine *hwe`: engine instance to process workarounds for

**Description**

Process engine workaround table for this platform, saving in **hwe** all the
workarounds that need to be applied at the engine level that match this
engine.

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

```c
void xe_wa_process_lrc(struct xe_hw_engine *hwe)
```

process context workaround table

**Parameters**

- `struct xe_hw_engine *hwe`: engine instance to process workarounds for

**Description**

Process context workaround table for this platform, saving in **hwe** all the
workarounds that need to be applied on context restore. These are workarounds
touching registers that are part of the HW context image.

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

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

initialize device with workaround oob bookkeeping

**Parameters**

- `struct xe_device *xe`: Xe device instance to initialize

**Description**

Returns 0 for success, negative with error code otherwise

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

```c
int xe_wa_gt_init(struct xe_gt *gt)
```

initialize gt with workaround bookkeeping

**Parameters**

- `struct xe_gt *gt`: GT instance to initialize

**Description**

Returns 0 for success, negative error code otherwise.

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

```c
int xe_wa_gt_dump(struct xe_gt *gt, struct drm_printer *p)
```

Dump GT workarounds into a drm printer.

**Parameters**

- `struct xe_gt *gt`: the `xe_gt`
- `struct drm_printer *p`: the [`drm_printer`](/kernel/hardware/gpu/drm-internals.md#c-struct-drm_printer)

**Return**

always 0.
