# cxl: linux

---

# CXL Access Coordinates Computation

_Peios Kernel / Hardware and drivers / Driver development / cxl: linux_

> A memory region performance coordinates (latency and bandwidth) are typically provided via ACPI tables SRAT <../platform/acpi/srat> and HMAT <../platform/acpi/hmat>. However, the platform firmware (BIOS) is not able to annotate those for…

<a id="rst-cxl-access-coordinates-computation"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<!-- This data file has been placed in the public domain. -->

<!-- Derived from the Unicode character mappings available from
<http://www.w3.org/2003/entities/xml/>.
Processed by unicode2rstsubs.py, part of Docutils:
<https://docutils.sourceforge.io>. -->

<a id="rst-latency-and-bandwidth-calculation"></a>

## Latency and Bandwidth Calculation

A memory region performance coordinates (latency and bandwidth) are typically provided via ACPI tables [SRAT](</kernel/hardware/driver-api/cxl-platform-acpi/srat.md>) and [HMAT](</kernel/hardware/driver-api/cxl-platform-acpi/hmat.md>). However, the platform firmware (BIOS) is not able to annotate those for CXL devices that are hot-plugged since they do not exist during platform firmware initialization. The CXL driver can compute the performance coordinates by retrieving data from several components.

The [SRAT](</kernel/hardware/driver-api/cxl-platform-acpi/srat.md>) provides a Generic Port Affinity subtable that ties a proximity domain to a device handle, which in this case would be the CXL hostbridge. Using this association, the performance coordinates for the Generic Port can be retrieved from the [HMAT](</kernel/hardware/driver-api/cxl-platform-acpi/hmat.md>) subtable. This piece represents the performance coordinates between a CPU and a Generic Port (CXL hostbridge).

The [CDAT](</kernel/hardware/driver-api/cxl-platform/cdat.md>) provides the performance coordinates for the CXL device itself. That is the bandwidth and latency to access that device's memory region. The DSMAS subtable provides a DSMADHandle that is tied to a Device Physical Address (DPA) range. The DSLBIS subtable provides the performance coordinates that's tied to a DSMADhandle and this ties the two table entries together to provide the performance coordinates for each DPA region. For example, if a device exports a DRAM region and a PMEM region, then there would be different performance characteristsics for each of those regions.

If there's a CXL switch in the topology, then the performance coordinates for the switch is provided by SSLBIS subtable. This provides the bandwidth and latency for traversing the switch between the switch upstream port and the switch downstream port that points to the endpoint device.

Simple topology example:



```text
GP0/HB0/ACPI0016-0
       RP0
        |
        | L0
        |
    SW 0 / USP0
    SW 0 / DSP0
        |
        | L1
        |
       EP0
```

In this example, there is a CXL switch between an endpoint and a root port. Latency in this example is calculated as such: L(EP0) - Latency from EP0 CDAT DSMAS+DSLBIS L(L1) - Link latency between EP0 and SW0DSP0 L(SW0) - Latency for the switch from SW0 CDAT SSLBIS. L(L0) - Link latency between SW0 and RP0 L(RP0) - Latency from root port to CPU via SRAT and HMAT (Generic Port). Total read and write latencies are the sum of all these parts.

Bandwidth in this example is calculated as such: B(EP0) - Bandwidth from EP0 CDAT DSMAS+DSLBIS B(L1) - Link bandwidth between EP0 and SW0DSP0 B(SW0) - Bandwidth for the switch from SW0 CDAT SSLBIS. B(L0) - Link bandwidth between SW0 and RP0 B(RP0) - Bandwidth from root port to CPU via SRAT and HMAT (Generic Port). The total read and write bandwidth is the min() of all these parts.

To calculate the link bandwidth: LinkOperatingFrequency (GT/s) is the current negotiated link speed. DataRatePerLink (MB/s) = LinkOperatingFrequency / 8 Bandwidth (MB/s) = PCIeCurrentLinkWidth \* DataRatePerLink Where PCIeCurrentLinkWidth is the number of lanes in the link.

To calculate the link latency: LinkLatency (picoseconds) = FlitSize / LinkBandwidth (MB/s)

See [CXL Memory Device SW Guide r1.0](<https://www.intel.com/content/www/us/en/content-details/643805/cxl-memory-device-software-guide.html>)<a id="rst-cxl-memory-device-sw-guide-r1-0"></a>, section 2.11.3 and 2.11.4 for details.

In the end, the access coordinates for a constructed memory region is calculated from one or more memory partitions from each of the CXL device(s).

<a id="rst-shared-upstream-link-calculation"></a>

## Shared Upstream Link Calculation

For certain CXL region construction with endpoints behind CXL switches (SW) or Root Ports (RP), there is the possibility of the total bandwidth for all the endpoints behind a switch being more than the switch upstream link. A similar situation can occur within the host, upstream of the root ports. The CXL driver performs an additional pass after all the targets have arrived for a region in order to recalculate the bandwidths with possible upstream link being a limiting factor in mind.

The algorithm assumes the configuration is a symmetric topology as that maximizes performance. When asymmetric topology is detected, the calculation is aborted. An asymmetric topology is detected during topology walk where the number of RPs detected as a grandparent is not equal to the number of devices iterated in the same iteration loop. The assumption is made that subtle asymmetry in properties does not happen and all paths to EPs are equal.

There can be multiple switches under an RP. There can be multiple RPs under a CXL Host Bridge (HB). There can be multiple HBs under a CXL Fixed Memory Window Structure (CFMWS) in the [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>).

An example hierarchy:



```text
               CFMWS 0
                 |
        _________|_________
       |                   |
   ACPI0017-0          ACPI0017-1
GP0/HB0/ACPI0016-0   GP1/HB1/ACPI0016-1
   |          |        |           |
  RP0        RP1      RP2         RP3
   |          |        |           |
 SW 0       SW 1     SW 2        SW 3
 |   |      |   |    |   |       |   |
EP0 EP1    EP2 EP3  EP4  EP5    EP6 EP7
```

Computation for the example hierarchy:

- **Min (GP0 to CPU BW,**

  - **Min(SW 0 Upstream Link to RP0 BW,**

    Min(SW0SSLBIS for SW0DSP0 (EP0), EP0 DSLBIS, EP0 Upstream Link) + Min(SW0SSLBIS for SW0DSP1 (EP1), EP1 DSLBIS, EP1 Upstream link)) +

  - **Min(SW 1 Upstream Link to RP1 BW,**

    Min(SW1SSLBIS for SW1DSP0 (EP2), EP2 DSLBIS, EP2 Upstream Link) + Min(SW1SSLBIS for SW1DSP1 (EP3), EP3 DSLBIS, EP3 Upstream link))) +

- **Min (GP1 to CPU BW,**

  - **Min(SW 2 Upstream Link to RP2 BW,**

    Min(SW2SSLBIS for SW2DSP0 (EP4), EP4 DSLBIS, EP4 Upstream Link) + Min(SW2SSLBIS for SW2DSP1 (EP5), EP5 DSLBIS, EP5 Upstream link)) +

  - **Min(SW 3 Upstream Link to RP3 BW,**

    Min(SW3SSLBIS for SW3DSP0 (EP6), EP6 DSLBIS, EP6 Upstream Link) + Min(SW3SSLBIS for SW3DSP1 (EP7), EP7 DSLBIS, EP7 Upstream link))))

The calculation starts at cxl_region_shared_upstream_perf_update(). A xarray is created to collect all the endpoint bandwidths via the cxl_endpoint_gather_bandwidth() function. The min() of bandwidth from the endpoint CDAT and the upstream link bandwidth is calculated. If the endpoint has a CXL switch as a parent, then min() of calculated bandwidth and the bandwidth from the SSLBIS for the switch downstream port that is associated with the endpoint is calculated. The final bandwidth is stored in a 'struct cxl_perf_ctx' in the xarray indexed by a device pointer. If the endpoint is direct attached to a root port (RP), the device pointer would be an RP device. If the endpoint is behind a switch, the device pointer would be the upstream device of the parent switch.

At the next stage, the code walks through one or more switches if they exist in the topology. For endpoints directly attached to RPs, this step is skipped. If there is another switch upstream, the code takes the min() of the current gathered bandwidth and the upstream link bandwidth. If there's a switch upstream, then the SSLBIS of the upstream switch.

Once the topology walk reaches the RP, whether it's direct attached endpoints or walking through the switch(es), cxl_rp_gather_bandwidth() is called. At this point all the bandwidths are aggregated per each host bridge, which is also the index for the resulting xarray.

The next step is to take the min() of the per host bridge bandwidth and the bandwidth from the Generic Port (GP). The bandwidths for the GP are retrieved via ACPI tables ([SRAT](</kernel/hardware/driver-api/cxl-platform-acpi/srat.md>) and [HMAT](</kernel/hardware/driver-api/cxl-platform-acpi/hmat.md>)). The minimum bandwidth are aggregated under the same ACPI0017 device to form a new xarray.

Finally, the cxl_region_update_bandwidth() is called and the aggregated bandwidth from all the members of the last xarray is updated for the access coordinates residing in the cxl region (cxlr) context.

<a id="rst-qtg-id"></a>

## QTG ID

Each [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>) has a QTG ID field. This field provides the ID that associates with a QoS Throttling Group (QTG) for the CFMWS window. Once the access coordinates are calculated, an ACPI Device Specific Method can be issued to the ACPI0016 device to retrieve the QTG ID depends on the access coordinates provided. The QTG ID for the device can be used as guidance to match to the CFMWS to setup the best Linux root decoder for the device performance.

---

# CXL Driver Operation

_Peios Kernel / Hardware and drivers / Driver development / cxl: linux_

> The devices described in this section are present in

<a id="rst-cxl-driver-operation"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

The devices described in this section are present in



```text
/sys/bus/cxl/devices/
/dev/cxl/
```

The `cxl-cli` library, maintained as part of the NDTCL project, may be used to script interactions with these devices.

<a id="rst-drivers"></a>

## Drivers

The CXL driver is split into a number of drivers.

- cxl_core  - fundamental init interface and core object creation

- cxl_port  - initializes root and provides port enumeration interface.

- cxl_acpi  - initializes root decoders and interacts with ACPI data.

- cxl_p/mem - initializes memory devices

- cxl_pci   - uses cxl_port to enumerate the actual fabric hierarchy.

<a id="rst-driver-devices"></a>

## Driver Devices

Here is an example from a single-socket system with 4 host bridges. Two host bridges have a single memory device attached, and the devices are interleaved into a single memory region. The memory region has been converted to dax.



```text
# ls /sys/bus/cxl/devices/
  dax_region0  decoder3.0  decoder6.0  mem0   port3
  decoder0.0   decoder4.0  decoder6.1  mem1   port4
  decoder1.0   decoder5.0  endpoint5   port1  region0
  decoder2.0   decoder5.1  endpoint6   port2  root0
```

```mermaid
flowchart TB
  n0(["root0"])
  n1(["port1"])
  n2(["port3"])
  n3(["decoder0.0"])
  n4(["endpoint5"])
  n5(["endpoint6"])
  n6(["decoder1.0"])
  n7(["decoder3.0"])
  n8(["decoder5.0"])
  n9(["decoder6.0"])
  n10(["region0"])
  n11(["dax_region0"])
  n12(["dax0.0"])
  n0 --> n1
  n0 --> n2
  n0 --> n3
  n1 --> n4
  n2 --> n5
  n1 --> n6
  n2 --> n7
  n4 --> n8
  n5 --> n9
  n3 --> n10
  n3 --> n6
  n3 --> n7
  n6 --> n8
  n7 --> n9
  n8 --> n10
  n9 --> n10
  n10 --> n11
  n11 --> n12
```

Diagraph of CXL fabric with a host-bridge interleave memory region

For this section we'll explore the devices present in this configuration, but we'll explore more configurations in-depth in example configurations below.

<a id="rst-base-devices"></a>

### Base Devices

Most devices in a CXL fabric are a `port` of some kind (because each device mostly routes request from one device to the next, rather than provide a direct service).

<a id="rst-root"></a>

#### Root

The `CXL Root` is logical object created by the `cxl_acpi` driver during `cxl_acpi_probe` - if the `ACPI0017` `Compute Express Link Root Object` Device Class is found.

The Root contains links to:

- `Host Bridge Ports` defined by CHBS in the [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>)

- `Downstream Ports` typically connected to `Host Bridge Ports`.

- `Root Decoders` defined by CFMWS the [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>)



```text
# ls /sys/bus/cxl/devices/root0
  decoder0.0          dport0  dport5    port2  subsystem
  decoders_committed  dport1  modalias  port3  uevent
  devtype             dport4  port1     port4  uport

# cat /sys/bus/cxl/devices/root0/devtype
  cxl_port

# cat port1/devtype
  cxl_port

# cat decoder0.0/devtype
  cxl_decoder_root
```

The root is first `logical port` in the CXL fabric, as presented by the Linux CXL driver.  The `CXL root` is a special type of `switch port`, in that it only has downstream port connections.

<a id="rst-port"></a>

#### Port

A `port` object is better described as a `switch port`.  It may represent a host bridge to the root or an actual switch port on a switch. A `switch port` contains one or more decoders used to route memory requests downstream ports, which may be connected to another `switch port` or an `endpoint port`.



```text
# ls /sys/bus/cxl/devices/port1
  decoder1.0          dport0    driver     parent_dport  uport
  decoders_committed  dport113  endpoint5  subsystem
  devtype             dport2    modalias   uevent

# cat devtype
  cxl_port

# cat decoder1.0/devtype
  cxl_decoder_switch

# cat endpoint5/devtype
  cxl_port
```

CXL `Host Bridges` in the fabric are probed during `cxl_acpi_probe` at the time the `CXL Root` is probed.  The allows for the immediate logical connection to between the root and host bridge.

- The root has a downstream port connection to a host bridge

- The host bridge has an upstream port connection to the root.

- The host bridge has one or more downstream port connections to switch or endpoint ports.

A `Host Bridge` is a special type of CXL `switch port`. It is explicitly defined in the ACPI specification via `ACPI0016` ID.  `Host Bridge` ports will be probed at `acpi_probe` time, while similar ports on an actual switch will be probed later.  Otherwise, switch and host bridge ports look very similar - the both contain switch decoders which route accesses between upstream and downstream ports.

<a id="rst-endpoint"></a>

#### Endpoint

An `endpoint` is a terminal port in the fabric.  This is a `logical device`, and may be one of many `logical devices` presented by a memory device. It is still considered a type of `port` in the fabric.

An `endpoint` contains `endpoint decoders` and the device's Coherent Device Attribute Table (which describes the device's capabilities).



```text
# ls /sys/bus/cxl/devices/endpoint5
  CDAT        decoders_committed  modalias      uevent
  decoder5.0  devtype             parent_dport  uport
  decoder5.1  driver              subsystem

# cat /sys/bus/cxl/devices/endpoint5/devtype
  cxl_port

# cat /sys/bus/cxl/devices/endpoint5/decoder5.0/devtype
  cxl_decoder_endpoint
```

<a id="rst-memory-device-memdev"></a>

#### Memory Device (memdev)

A `memdev` is probed and added by the `cxl_pci` driver in `cxl_pci_probe` and is managed by the `cxl_mem` driver. It primarily provides the `IOCTL` interface to a memory device, via `/dev/cxl/memN`, and exposes various device configuration data.



```text
# ls /sys/bus/cxl/devices/mem0
  dev       firmware_version    payload_max  security   uevent
  driver    label_storage_size  pmem         serial
  firmware  numa_node           ram          subsystem
```

A Memory Device is a discrete base object that is not a port.  While the physical device it belongs to may also host an `endpoint`, the relationship between an `endpoint` and a `memdev` is not captured in sysfs.

<a id="rst-port-relationships"></a>

#### Port Relationships

In our example described above, there are four host bridges attached to the root, and two of the host bridges have one endpoint attached.

```mermaid
flowchart TB
  n0(["root0"])
  n1(["port1"])
  n2(["port2"])
  n3(["port3"])
  n4(["port4"])
  n5(["endpoint5"])
  n6(["endpoint6"])
  n0 --> n1
  n0 --> n2
  n0 --> n3
  n0 --> n4
  n1 --> n5
  n3 --> n6
```

Diagraph of CXL fabric with a host-bridge interleave memory region

<a id="rst-decoders"></a>

### Decoders

A `Decoder` is short for a CXL Host-Managed Device Memory (HDM) Decoder. It is a device that routes accesses through the CXL fabric to an endpoint, and at the endpoint translates a `Host Physical` to `Device Physical` Addressing.

The CXL 3.1 specification heavily implies that only endpoint decoders should engage in translation of `Host Physical Address` to `Device Physical Address`.



```text
8.2.4.20 CXL HDM Decoder Capability Structure

IMPLEMENTATION NOTE
CXL Host Bridge and Upstream Switch Port Decode Flow

IMPLEMENTATION NOTE
Device Decode Logic
```

These notes imply that there are two logical groups of decoders.

- Routing Decoder - a decoder which routes accesses but does not translate addresses from HPA to DPA.

- Translating Decoder - a decoder which translates accesses from HPA to DPA for an endpoint to service.

The CXL drivers distinguish 3 decoder types: root, switch, and endpoint. Only endpoint decoders are Translating Decoders, all others are Routing Decoders.

> [!NOTE]
>
> PLATFORM VENDORS BE AWARE
>
> Linux makes a strong assumption that endpoint decoders are the only decoder in the fabric that actively translates HPA to DPA.  Linux assumes routing decoders pass the HPA unchanged to the next decoder in the fabric.
>
> It is therefore assumed that any given decoder in the fabric will have an address range that is a subset of its upstream port decoder. Any deviation from this scheme undefined per the specification.  Linux prioritizes spec-defined / architectural behavior.

Decoders may have one or more `Downstream Targets` if configured to interleave memory accesses.  This will be presented in sysfs via the `target_list` parameter.

<a id="rst-root-decoder"></a>

#### Root Decoder

A `Root Decoder` is logical construct of the physical address and interleave configurations present in the CFMWS field of the [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>). Linux presents this information as a decoder present in the `CXL Root`.  We consider this a `Root Decoder`, though technically it exists on the boundary of the CXL specification and platform-specific CXL root implementations.

Linux considers these logical decoders a type of `Routing Decoder`, and is the first decoder in the CXL fabric to receive a memory access from the platform's memory controllers.

`Root Decoders` are created during `cxl_acpi_probe`.  One root decoder is created per CFMWS entry in the [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>).

The `target_list` parameter is filled by the CFMWS target fields. Targets of a root decoder are `Host Bridges`, which means interleave done at the root decoder level is an `Inter-Host-Bridge Interleave`.

Only root decoders are capable of `Inter-Host-Bridge Interleave`.

Such interleaves must be configured by the platform and described in the ACPI CEDT CFMWS, as the target CXL host bridge UIDs in the CFMWS must match the CXL host bridge UIDs in the CHBS field of the [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>) and the UID field of CXL Host Bridges defined in the [DSDT](</kernel/hardware/driver-api/cxl-platform-acpi/dsdt.md>).

Interleave settings in a root decoder describe how to interleave accesses among the *immediate downstream targets*, not the entire interleave set.

The memory range described in the root decoder is used to

1. Create a memory region (`region0` in this example), and

2. Associate the region with an IO Memory Resource (`kernel/resource.c`)



```text
# ls /sys/bus/cxl/devices/decoder0.0/
  cap_pmem           devtype                 region0
  cap_ram            interleave_granularity  size
  cap_type2          interleave_ways         start
  cap_type3          locked                  subsystem
  create_ram_region  modalias                target_list
  delete_region      qos_class               uevent

# cat /sys/bus/cxl/devices/decoder0.0/region0/resource
  0xc050000000
```

The IO Memory Resource is created during early boot when the CFMWS region is identified in the EFI Memory Map or E820 table (on x86).

Root decoders are defined as a separate devtype, but are also a type of `Switch Decoder` due to having downstream targets.



```text
# cat /sys/bus/cxl/devices/decoder0.0/devtype
  cxl_decoder_root
```

<a id="rst-switch-decoder"></a>

#### Switch Decoder

Any non-root, translating decoder is considered a `Switch Decoder`, and will present with the type `cxl_decoder_switch`. Both `Host Bridge` and `CXL Switch` (device) decoders are of type `cxl_decoder_switch`.



```text
# ls /sys/bus/cxl/devices/decoder1.0/
  devtype                 locked    size       target_list
  interleave_granularity  modalias  start      target_type
  interleave_ways         region    subsystem  uevent

# cat /sys/bus/cxl/devices/decoder1.0/devtype
  cxl_decoder_switch

# cat /sys/bus/cxl/devices/decoder1.0/region
  region0
```

A `Switch Decoder` has associations between a region defined by a root decoder and downstream target ports.  Interleaving done within a switch decoder is a multi-downstream-port interleave (or `Intra-Host-Bridge Interleave` for host bridges).

Interleave settings in a switch decoder describe how to interleave accesses among the *immediate downstream targets*, not the entire interleave set.

Switch decoders are created during `cxl_switch_port_probe` in the `cxl_port` driver, and is created based on a PCI device's DVSEC registers.

Switch decoder programming is validated during probe if the platform programs them during boot (See `Auto Decoders` below), or on commit if programmed at runtime (See `Runtime Programming` below).

<a id="rst-endpoint-decoder"></a>

#### Endpoint Decoder

Any decoder attached to a *terminal* point in the CXL fabric (`An Endpoint`) is considered an `Endpoint Decoder`. Endpoint decoders are of type `cxl_decoder_endpoint`.



```text
# ls /sys/bus/cxl/devices/decoder5.0
  devtype                 locked    start
  dpa_resource            modalias  subsystem
  dpa_size                mode      target_type
  interleave_granularity  region    uevent
  interleave_ways         size

# cat /sys/bus/cxl/devices/decoder5.0/devtype
  cxl_decoder_endpoint

# cat /sys/bus/cxl/devices/decoder5.0/region
  region0
```

An `Endpoint Decoder` has an association with a region defined by a root decoder and describes the device-local resource associated with this region.

Unlike root and switch decoders, endpoint decoders translate `Host Physical` to `Device Physical` address ranges.  The interleave settings on an endpoint therefore describe the entire *interleave set*.

`Device Physical Address` regions must be committed in-order. For example, the DPA region starting at 0x80000000 cannot be committed before the DPA region starting at 0x0.

As of Linux v6.15, Linux does not support *imbalanced* interleave setups, all endpoints in an interleave set are expected to have the same interleave settings (granularity and ways must be the same).

Endpoint decoders are created during `cxl_endpoint_port_probe` in the `cxl_port` driver, and is created based on a PCI device's DVSEC registers.

<a id="rst-decoder-relationships"></a>

#### Decoder Relationships

In our example described above, there is one root decoder which routes memory accesses over two host bridges.  Each host bridge has a decoder which routes access to their singular endpoint targets.  Each endpoint has a decoder which translates HPA to DPA and services the memory request.

The driver validates relationships between ports by decoder programming, so we can think of decoders being related in a similarly hierarchical fashion to ports.

```mermaid
flowchart TB
  n0(["root0"])
  n1(["decoder0.0"])
  n2(["decoder1.0"])
  n3(["decoder3.0"])
  n4(["decoder5.0"])
  n5(["decoder6.0"])
  n0 --> n1
  n1 --> n2
  n1 --> n3
  n2 --> n4
  n3 --> n5
```

Diagraph of CXL root, switch, and endpoint decoders.

<a id="rst-regions"></a>

### Regions

<a id="rst-memory-region"></a>

#### Memory Region

A `Memory Region` is a logical construct that connects a set of CXL ports in the fabric to an IO Memory Resource.  It is ultimately used to expose the memory on these devices to the DAX subsystem via a `DAX Region`.

An example RAM region:



```text
# ls /sys/bus/cxl/devices/region0/
  access0      devtype                 modalias  subsystem  uuid
  access1      driver                  mode      target0
  commit       interleave_granularity  resource  target1
  dax_region0  interleave_ways         size      uevent
```

A memory region can be constructed during endpoint probe, if decoders were programmed by BIOS/EFI (see `Auto Decoders`), or by creating a region manually via a `Root Decoder`'s `create_ram_region` or `create_pmem_region` interfaces.

The interleave settings in a `Memory Region` describe the configuration of the `Interleave Set` - and are what can be expected to be seen in the endpoint interleave settings.

```mermaid
flowchart TB
  n0(["root0"])
  n1(["decoder0.0"])
  n2(["region0"])
  n3(["decoder5.0"])
  n4(["decoder6.0"])
  n0 --> n1
  n1 --> n2
  n2 --> n3
  n2 --> n4
```

Regions are created based on root decoder configurations. Endpoint decoders
must be programmed with the same interleave settings as the region.

<a id="rst-dax-region"></a>

#### DAX Region

A `DAX Region` is used to convert a CXL `Memory Region` to a DAX device. A DAX device may then be accessed directly via a file descriptor interface, or converted to System RAM via the DAX kmem driver.  See the DAX driver section for more details.



```text
# ls /sys/bus/cxl/devices/dax_region0/
  dax0.0      devtype  modalias   uevent
  dax_region  driver   subsystem
```

<a id="rst-mailbox-interfaces"></a>

### Mailbox Interfaces

A mailbox command interface for each device is exposed in



```text
/dev/cxl/mem0
/dev/cxl/mem1
```

These mailboxes may receive any specification-defined command. Raw commands (custom commands) can only be sent to these interfaces if the build config `CXL_MEM_RAW_COMMANDS` is set.  This is considered a debug and/or development interface, not an officially supported mechanism for creation of vendor-specific commands (see the `fwctl` subsystem for that).

<a id="rst-decoder-programming"></a>

## Decoder Programming

<a id="rst-runtime-programming"></a>

### Runtime Programming

During probe, the only decoders *required* to be programmed are `Root Decoders`. In reality, `Root Decoders` are a logical construct to describe the memory region and interleave configuration at the host bridge level - as described in the ACPI CEDT CFMWS.

All other `Switch` and `Endpoint` decoders may be programmed by the user at runtime - if the platform supports such configurations.

This interaction is what creates a `Software Defined Memory` environment.

See the `cxl-cli` documentation for more information about how to configure CXL decoders at runtime.

<a id="rst-auto-decoders"></a>

### Auto Decoders

Auto Decoders are decoders programmed by BIOS/EFI at boot time, and are almost always locked (cannot be changed).  This is done by a platform which may have a static configuration - or certain quirks which may prevent dynamic runtime changes to the decoders (such as requiring additional controller programming within the CPU complex outside the scope of CXL).

Auto Decoders are probed automatically as long as the devices and memory regions they are associated with probe without issue.  When probing Auto Decoders, the driver's primary responsibility is to ensure the fabric is sane - as-if validating runtime programmed regions and decoders.

If Linux cannot validate auto-decoder configuration, the memory will not be surfaced as a DAX device - and therefore not be exposed to the page allocator - effectively stranding it.

<a id="rst-interleave"></a>

### Interleave

The Linux CXL driver supports `Cross-Link First` interleave. This dictates how interleave is programmed at each decoder step, as the driver validates the relationships between a decoder and it's parent.

For example, in a `Cross-Link First` interleave setup with 16 endpoints attached to 4 host bridges, linux expects the following ways/granularity across the root, host bridge, and endpoints respectively.


<table>
<caption>4x4 cross-link first interleave settings</caption>
<tbody>
<tr>
<td>


decoder

</td>
<td>


ways

</td>
<td>


granularity

</td>
</tr>
<tr>
<td>


root

</td>
<td>


4

</td>
<td>


256

</td>
</tr>
<tr>
<td>


host bridge

</td>
<td>


4

</td>
<td>


1024

</td>
</tr>
<tr>
<td>


endpoint

</td>
<td>


16

</td>
<td>


256

</td>
</tr>
</tbody>
</table>

At the root, every a given access will be routed to the `((HPA / 256) % 4)th` target host bridge. Within a host bridge, every `((HPA / 1024) % 4)th` target endpoint.  Each endpoint translates based on the entire 16 device interleave set.

Unbalanced interleave sets are not supported - decoders at a similar point in the hierarchy (e.g. all host bridge decoders) must have the same ways and granularity configuration.

<a id="rst-at-root"></a>

#### At Root

Root decoder interleave is defined by CFMWS field of the [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>).  The CEDT may actually define multiple CFMWS configurations to describe the same physical capacity, with the intent to allow users to decide at runtime whether to online memory as interleaved or non-interleaved.



```text
           Subtable Type : 01 [CXL Fixed Memory Window Structure]
     Window base address : 0000000100000000
             Window size : 0000000100000000
Interleave Members (2^n) : 00
   Interleave Arithmetic : 00
            First Target : 00000007

           Subtable Type : 01 [CXL Fixed Memory Window Structure]
     Window base address : 0000000200000000
             Window size : 0000000100000000
Interleave Members (2^n) : 00
   Interleave Arithmetic : 00
            First Target : 00000006

           Subtable Type : 01 [CXL Fixed Memory Window Structure]
     Window base address : 0000000300000000
             Window size : 0000000200000000
Interleave Members (2^n) : 01
   Interleave Arithmetic : 00
            First Target : 00000007
             Next Target : 00000006
```

In this example, the CFMWS defines two discrete non-interleaved 4GB regions for each host bridge, and one interleaved 8GB region that targets both. This would result in 3 root decoders presenting in the root.



```text
# ls /sys/bus/cxl/devices/root0/decoder*
  decoder0.0  decoder0.1  decoder0.2

# cat /sys/bus/cxl/devices/decoder0.0/target_list start size
  7
  0x100000000
  0x100000000

# cat /sys/bus/cxl/devices/decoder0.1/target_list start size
  6
  0x200000000
  0x100000000

# cat /sys/bus/cxl/devices/decoder0.2/target_list start size
  7,6
  0x300000000
  0x200000000
```

These decoders are not runtime programmable.  They are used to generate a `Memory Region` to bring this memory online with runtime programmed settings at the `Switch` and `Endpoint` decoders.

<a id="rst-at-host-bridge-or-switch"></a>

#### At Host Bridge or Switch

`Host Bridge` and `Switch` decoders are programmable via the following fields:

- `start` - the HPA region associated with the memory region

- `size` - the size of the region

- `target_list` - the list of downstream ports

- `interleave_ways` - the number downstream ports to interleave across

- `interleave_granularity` - the granularity to interleave at.

Linux expects the `interleave_granularity` of switch decoders to be derived from their upstream port connections. In `Cross-Link First` interleave configurations, the `interleave_granularity` of a decoder is equal to `parent_interleave_granularity * parent_interleave_ways`.

<a id="rst-at-endpoint"></a>

#### At Endpoint

`Endpoint Decoders` are programmed similar to Host Bridge and Switch decoders, with the exception that the ways and granularity are defined by the interleave set (e.g. the interleave settings defined by the associated `Memory Region`).

- `start` - the HPA region associated with the memory region

- `size` - the size of the region

- `interleave_ways` - the number endpoints in the interleave set

- `interleave_granularity` - the granularity to interleave at.

These settings are used by endpoint decoders to *Translate* memory requests from HPA to DPA.  This is why they must be aware of the entire interleave set.

Linux does not support unbalanced interleave configurations.  As a result, all endpoints in an interleave set must have the same ways and granularity.

<a id="rst-example-configurations"></a>

## Example Configurations

- [Single Device](</kernel/hardware/driver-api/cxl-linux-example-configurations/single-device.md>)
- [Inter-Host-Bridge Interleave](</kernel/hardware/driver-api/cxl-linux-example-configurations/hb-interleave.md>)
- [Intra-Host-Bridge Interleave](</kernel/hardware/driver-api/cxl-linux-example-configurations/intra-hb-interleave.md>)
- [Multi-Level Interleave](</kernel/hardware/driver-api/cxl-linux-example-configurations/multi-interleave.md>)

---

# DAX Driver Operation

_Peios Kernel / Hardware and drivers / Driver development / cxl: linux_

> The Direct Access Device driver was originally designed to provide a memory-like access mechanism to memory-like block-devices. It was extended to support CXL Memory Devices, which provide user-configured memory devices.

<a id="rst-dax-driver-operation"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

The `Direct Access Device` driver was originally designed to provide a memory-like access mechanism to memory-like block-devices.  It was extended to support CXL Memory Devices, which provide user-configured memory devices.

The CXL subsystem depends on the DAX subsystem to either:

- Generate a file-like interface to userland via `/dev/daxN.Y`, or

- Engage the memory-hotplug interface to add CXL memory to page allocator.

The DAX subsystem exposes this ability through the `cxl_dax_region` driver. A `dax_region` provides the translation between a CXL `memory_region` and a `DAX Device`.

<a id="rst-dax-device"></a>

## DAX Device

A `DAX Device` is a file-like interface exposed in `/dev/daxN.Y`. A memory region exposed via dax device can be accessed via userland software via the `mmap()` system-call.  The result is direct mappings to the CXL capacity in the task's page tables.

Users wishing to manually handle allocation of CXL memory should use this interface.

<a id="rst-kmem-conversion"></a>

## kmem conversion

The `dax_kmem` driver converts a `DAX Device` into a series of `hotplug memory blocks` managed by `kernel/memory-hotplug.c`.  This capacity will be exposed to the kernel page allocator in the user-selected memory zone.

The `memmap_on_memory` setting (both global and DAX device local) dictates where the kernell will allocate the `struct folio` descriptors for this memory will come from.  If `memmap_on_memory` is set, memory hotplug will set aside a portion of the memory block capacity to allocate folios. If unset, the memory is allocated via a normal `GFP_KERNEL` allocation - and as a result will most likely land on the local NUM node of the CPU executing the hotplug operation.

---

# Linux Init (Early Boot)

_Peios Kernel / Hardware and drivers / Driver development / cxl: linux_

> Linux configuration is split into two major steps: Early-Boot and everything else.

<a id="rst-linux-init-early-boot"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

Linux configuration is split into two major steps: Early-Boot and everything else.

During early boot, Linux sets up immutable resources (such as numa nodes), while later operations include things like driver probe and memory hotplug.  Linux may read EFI and ACPI information throughout this process to configure logical representations of the devices.

During Linux Early Boot stage (functions in the kernel that have the \__init decorator), the system takes the resources created by EFI/BIOS ([ACPI tables](</kernel/hardware/driver-api/cxl-platform/acpi.md>)) and turns them into resources that the kernel can consume.

<a id="rst-bios-build-and-boot-options"></a>

## BIOS, Build and Boot Options

There are 4 pre-boot options that need to be considered during kernel build which dictate how memory will be managed by Linux during early boot.

- EFI_MEMORY_SP

  - BIOS/EFI Option that dictates whether memory is SystemRAM or Specific Purpose.  Specific Purpose memory will be deferred to drivers to manage - and not immediately exposed as system RAM.

- CONFIG_EFI_SOFT_RESERVE

  - Linux Build config option that dictates whether the kernel supports Specific Purpose memory.

- CONFIG_MHP_DEFAULT_ONLINE_TYPE

  - Linux Build config that dictates whether and how Specific Purpose memory converted to a dax device should be managed (left as DAX or onlined as SystemRAM in ZONE_NORMAL or ZONE_MOVABLE).

- nosoftreserve

  - Linux kernel boot option that dictates whether Soft Reserve should be supported.  Similar to CONFIG_EFI_SOFT_RESERVE.

<a id="rst-memory-map-creation"></a>

## Memory Map Creation

While the kernel parses the EFI memory map, if `Specific Purpose` memory is supported and detected, it will set this region aside as `SOFT_RESERVED`.

If `EFI_MEMORY_SP=0`, `CONFIG_EFI_SOFT_RESERVE=n`, or `nosoftreserve=y` - Linux will default a CXL device memory region to SystemRAM.  This will expose the memory to the kernel page allocator in `ZONE_NORMAL`, making it available for use for most allocations (including `struct page` and page tables).

If `Specific Purpose` is set and supported, `CONFIG_MHP_DEFAULT_ONLINE_TYPE_*` dictates whether the memory is onlined by default (`_OFFLINE` or `_ONLINE_*`), and if online which zone to online this memory to by default (`_NORMAL` or `_MOVABLE`).

If placed in `ZONE_MOVABLE`, the memory will not be available for most kernel allocations (such as `struct page` or page tables).  This may significant impact performance depending on the memory capacity of the system.

<a id="rst-numa-node-reservation"></a>

## NUMA Node Reservation

Linux refers to the proximity domains (`PXM`) defined in the [SRAT](</kernel/hardware/driver-api/cxl-platform-acpi/srat.md>) to create NUMA nodes in `acpi_numa_init`. Typically, there is a 1:1 relation between `PXM` and NUMA node IDs.

The SRAT is the only ACPI defined way of defining Proximity Domains. Linux chooses to, at most, map those 1:1 with NUMA nodes. [CEDT](</kernel/hardware/driver-api/cxl-platform-acpi/cedt.md>) adds a description of SPA ranges which Linux may map to one or more NUMA nodes.

If there are CXL ranges in the CFMWS but not in SRAT, then a fake `PXM` is created (as of v6.15). In the future, Linux may reject CFMWS not described by SRAT due to the ambiguity of proximity domain association.

It is important to note that NUMA node creation cannot be done at runtime. All possible NUMA nodes are identified at `__init` time, more specifically during `mm_init`. The CEDT and SRAT must contain sufficient `PXM` data for Linux to identify NUMA nodes their associated memory regions.

The relevant code exists in: `linux/drivers/acpi/numa/srat.c`.

See [Example Platform Configurations](</kernel/hardware/driver-api/cxl-platform/example-configs.md>) for more info.

<a id="rst-memory-tiers-creation"></a>

## Memory Tiers Creation

Memory tiers are a collection of NUMA nodes grouped by performance characteristics. During `__init`, Linux initializes the system with a default memory tier that contains all nodes marked `N_MEMORY`.

`memory_tier_init` is called at boot for all nodes with memory online by default. `memory_tier_late_init` is called during late-init for nodes setup during driver configuration.

Nodes are only marked `N_MEMORY` if they have *online* memory.

Tier membership can be inspected in



```text
/sys/devices/virtual/memory_tiering/memory_tierN/nodelist
0-1
```

If nodes are grouped which have clear difference in performance, check the [HMAT](</kernel/hardware/driver-api/cxl-platform-acpi/hmat.md>) and CDAT information for the CXL nodes. All nodes default to the DRAM tier, unless HMAT/CDAT information is reported to the memory_tier component via `access_coordinates`.

For more, see [CXL access coordinates documentation](</kernel/hardware/driver-api/cxl-linux/access-coordinates.md>).

<a id="rst-contiguous-memory-allocation"></a>

## Contiguous Memory Allocation

The contiguous memory allocator (CMA) enables reservation of contiguous memory regions on NUMA nodes during early boot.  However, CMA cannot reserve memory on NUMA nodes that are not online during early boot.



```text
void __init hugetlb_cma_reserve(void) {
  if (!node_online(nid))
    /* do not allow reservations */
}
```

This means if users intend to defer management of CXL memory to the driver, CMA cannot be used to guarantee huge page allocations.  If enabling CXL memory as SystemRAM in `ZONE_NORMAL` during early boot, CMA reservations per-node can be made with the `cma_pernuma` or `numa_cma` kernel command line parameters.

---

# Memory Hotplug

_Peios Kernel / Hardware and drivers / Driver development / cxl: linux_

> The final phase of surfacing CXL memory to the kernel page allocator is for the DAX driver to surface a Driver Managed memory region via the memory-hotplug component.

<a id="rst-memory-hotplug"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

The final phase of surfacing CXL memory to the kernel page allocator is for the `DAX` driver to surface a `Driver Managed` memory region via the memory-hotplug component.

There are four major configurations to consider:

1. Default Online Behavior (on/off and zone)

2. Hotplug Memory Block size

3. Memory Map Resource location

4. Driver-Managed Memory Designation

<a id="rst-default-online-behavior"></a>

## Default Online Behavior

The default-online behavior of hotplug memory is dictated by the following, in order of precedence:

- `CONFIG_MHP_DEFAULT_ONLINE_TYPE` Build Configuration

- `memhp_default_state` Boot parameter

- `/sys/devices/system/memory/auto_online_blocks` value

These dictate whether hotplugged memory blocks arrive in one of three states:

1. Offline

2. Online in `ZONE_NORMAL`

3. Online in `ZONE_MOVABLE`

`ZONE_NORMAL` implies this capacity may be used for almost any allocation, while `ZONE_MOVABLE` implies this capacity should only be used for migratable allocations.

`ZONE_MOVABLE` attempts to retain the hotplug-ability of a memory block so that it the entire region may be hot-unplugged at a later time.  Any capacity onlined into `ZONE_NORMAL` should be considered permanently attached to the page allocator.

<a id="rst-hotplug-memory-block-size"></a>

## Hotplug Memory Block Size

By default, on most architectures, the Hotplug Memory Block Size is either 128MB or 256MB.  On x86, the block size increases up to 2GB as total memory capacity exceeds 64GB.  As of v6.15, Linux does not take into account the size and alignment of the ACPI CEDT CFMWS regions (see Early Boot docs) when deciding the Hotplug Memory Block Size.

<a id="rst-memory-map"></a>

## Memory Map

The location of `struct folio` allocations to represent the hotplugged memory capacity are dictated by the following system settings:

- `/sys_module/memory_hotplug/parameters/memmap_on_memory`

- `/sys/bus/dax/devices/daxN.Y/memmap_on_memory`

If both of these parameters are set to true, `struct folio` for this capacity will be carved out of the memory block being onlined.  This has performance implications if the memory is particularly high-latency and its `struct folio` becomes hotly contended.

If either parameter is set to false, `struct folio` for this capacity will be allocated from the local node of the processor running the hotplug procedure.  This capacity will be allocated from `ZONE_NORMAL` on that node, as it is a `GFP_KERNEL` allocation.

Systems with extremely large amounts of `ZONE_MOVABLE` memory (e.g. CXL memory pools) must ensure that there is sufficient local `ZONE_NORMAL` capacity to host the memory map for the hotplugged capacity.

<a id="rst-driver-managed-memory"></a>

## Driver Managed Memory

The DAX driver surfaces this memory to memory-hotplug as "Driver Managed". This is not a configurable setting, but it's important to note that driver managed memory is explicitly excluded from use during kexec.  This is required to ensure any reset or out-of-band operations that the CXL device may be subject to during a functional system-reboot (such as a reset-on-probe) will not cause portions of the kexec kernel to be overwritten.

---

# Overview

_Peios Kernel / Hardware and drivers / Driver development / cxl: linux_

> This section presents the configuration process of a CXL Type-3 memory device, and how it is ultimately exposed to users as either a DAX device or normal memory pages via the kernel's page allocator.

<a id="rst-overview"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

This section presents the configuration process of a CXL Type-3 memory device, and how it is ultimately exposed to users as either a `DAX` device or normal memory pages via the kernel's page allocator.

Portions marked with a bullet are points at which certain kernel objects are generated.

1. Early Boot

> - **a)** BIOS, Build, and Boot Parameters
>
> > - **i)** EFI_MEMORY_SP
> >
> > - **ii)** CONFIG_EFI_SOFT_RESERVE
> >
> > - **iii)** CONFIG_MHP_DEFAULT_ONLINE_TYPE
> >
> > - **iv)** nosoftreserve
>
> - **b)** Memory Map Creation
>
> > - **i)** EFI Memory Map / E820 Consulted for Soft-Reserved
> >
> > > - CXL Memory is set aside to be handled by the CXL driver
> > >
> > > - Soft-Reserved IO Resource created for CFMWS entry
>
> - **c)** NUMA Node Creation
>
> > - Nodes created from ACPI CEDT CFMWS and SRAT Proximity domains (PXM)
>
> - **d)** Memory Tier Creation
>
> > - A default memory_tier is created with all nodes.
>
> - **e)** Contiguous Memory Allocation
>
> > - Any requested CMA is allocated from Online nodes
>
> - **f)** Init Finishes, Drivers start probing

2. ACPI and PCI Drivers

> - **a)** Detects PCI device is CXL, marking it for probe by CXL driver

3. CXL Driver Operation

> - **a)** Base device creation
>
> > - root, port, and memdev devices created
> >
> > - CEDT CFMWS IO Resource creation
>
> - **b)** Decoder creation
>
> > - root, switch, and endpoint decoders created
>
> - **c)** Logical device creation
>
> > - memory_region and endpoint devices created
>
> - **d)** Devices are associated with each other
>
> > - If auto-decoder (BIOS-programmed decoders), driver validates configurations, builds associations, and locks configs at probe time.
> >
> > - If user-configured, validation and associations are built at decoder-commit time.
>
> - **e)** Regions surfaced as DAX region
>
> > - dax_region created
> >
> > - DAX device created via DAX driver

4. DAX Driver Operation

> - **a)** DAX driver surfaces DAX region as one of two dax device modes
>
> > - kmem - dax device is converted to hotplug memory blocks
> >
> >   - DAX kmem IO Resource creation
> >
> > - hmem - dax device is left as daxdev to be accessed as a file.
> >
> >   - If hmem, journey ends here.
>
> - **b)** DAX kmem surfaces memory region to Memory Hotplug to add to page allocator as "driver managed memory"

5. Memory Hotplug

> - **a)** mhp component surfaces a dax device memory region as multiple memory blocks to the page allocator
>
> > - blocks appear in `/sys/bus/memory/devices` and linked to a NUMA node
>
> - **b)** blocks are onlined into the requested zone (NORMAL or MOVABLE)
>
> > - Memory is marked "Driver Managed" to avoid kexec from using it as region for kernel updates
