# drm/komeda Arm display driver

_Peios Kernel / Hardware and drivers / Graphics_

> The drm/komeda driver supports the Arm display processor D71 and later products, this document gives a brief overview of driver design: how it works and why design it like that.

<a id="rst-drm-komeda-arm-display-driver"></a>

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

The drm/komeda driver supports the Arm display processor D71 and later products, this document gives a brief overview of driver design: how it works and why design it like that.

<a id="rst-overview-of-d71-like-display-ips"></a>

## Overview of D71 like display IPs

From D71, Arm display IP begins to adopt a flexible and modularized architecture. A display pipeline is made up of multiple individual and functional pipeline stages called components, and every component has some specific capabilities that can give the flowed pipeline pixel data a particular processing.

Typical D71 components:

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

### Layer

Layer is the first pipeline stage, which prepares the pixel data for the next stage. It fetches the pixel from memory, decodes it if it's AFBC, rotates the source image, unpacks or converts YUV pixels to the device internal RGB pixels, then adjusts the color_space of pixels if needed.

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

### Scaler

As its name suggests, scaler takes responsibility for scaling, and D71 also supports image enhancements by scaler. The usage of scaler is very flexible and can be connected to layer output for layer scaling, or connected to compositor and scale the whole display frame and then feed the output data into wb_layer which will then write it into memory.

<a id="rst-compositor-compiz"></a>

### Compositor (compiz)

Compositor blends multiple layers or pixel data flows into one single display frame. its output frame can be fed into post image processor for showing it on the monitor or fed into wb_layer and written to memory at the same time. user can also insert a scaler between compositor and wb_layer to down scale the display frame first and then write to memory.

<a id="rst-writeback-layer-wb-layer"></a>

### Writeback Layer (wb_layer)

Writeback layer does the opposite things of Layer, which connects to compiz and writes the composition result to memory.

<a id="rst-post-image-processor-improc"></a>

### Post image processor (improc)

Post image processor adjusts frame data like gamma and color space to fit the requirements of the monitor.

<a id="rst-timing-controller-timing-ctrlr"></a>

### Timing controller (timing_ctrlr)

Final stage of display pipeline, Timing controller is not for the pixel handling, but only for controlling the display timing.

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

### Merger

D71 scaler mostly only has the half horizontal input/output capabilities compared with Layer, like if Layer supports 4K input size, the scaler only can support 2K input/output in the same time. To achieve the ful frame scaling, D71 introduces Layer Split, which splits the whole image to two half parts and feeds them to two Layers A and B, and does the scaling independently. After scaling the result need to be fed to merger to merge two part images together, and then output merged result to compiz.

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

### Splitter

Similar to Layer Split, but Splitter is used for writeback, which splits the compiz result to two parts and then feed them to two scalers.

<a id="rst-possible-d71-pipeline-usage"></a>

## Possible D71 Pipeline usage

Benefitting from the modularized architecture, D71 pipelines can be easily adjusted to fit different usages. And D71 has two pipelines, which support two types of working mode:

- Dual display mode Two pipelines work independently and separately to drive two display outputs.

- Single display mode Two pipelines work together to drive only one display output.

  On this mode, pipeline_B doesn't work independently, but outputs its composition result into pipeline_A, and its pixel timing also derived from pipeline_A.timing_ctrlr. The pipeline_B works just like a "slave" of pipeline_A(master)

<a id="rst-single-pipeline-data-flow"></a>

### Single pipeline data flow

```mermaid
flowchart LR
  n0(["Memory"])
  n1(["Monitor"])
  subgraph g0["#160;"]
    n2["Scaler-0"]
    n3["Scaler-1"]
    n4["Scaler-0/1"]
    n5["Layer-0"]
    n6["Layer-1"]
    n7["Layer-2"]
    n8["Layer-3"]
    n9["Compiz"]
    n10["Wb_layer"]
    n11["Improc"]
    n12["Timing Controller"]
  end
  n5 --> n2
  n6 --> n2
  n7 --> n3
  n8 --> n3
  n5 --> n9
  n6 --> n9
  n7 --> n9
  n8 --> n9
  n2 --> n9
  n3 --> n9
  n9 --> n4
  n4 --> n10
  n9 --> n11
  n11 --> n12
  n10 --> n0
  n12 --> n1
  style n2 stroke-dasharray:6 4
  style n3 stroke-dasharray:6 4
  style n4 stroke-dasharray:6 4
  style g0 stroke-dasharray:6 4
```

Single pipeline data flow

<a id="rst-dual-pipeline-with-slave-enabled"></a>

### Dual pipeline with Slave enabled

```mermaid
flowchart LR
  n0(["Memory"])
  n1(["Monitor"])
  subgraph g0["Slave Pipeline_B"]
    n2["Slave.Scaler-0"]
    n3["Slave.Scaler-1"]
    n4["Slave.Layer-0"]
    n5["Slave.Layer-1"]
    n6["Slave.Layer-2"]
    n7["Slave.Layer-3"]
    n8["Slave.Compiz"]
  end
  subgraph g1["Master Pipeline_A"]
    n9["Scaler-0"]
    n10["Scaler-1"]
    n11["Scaler-0/1"]
    n12["Layer-0"]
    n13["Layer-1"]
    n14["Layer-2"]
    n15["Layer-3"]
    n16["Compiz"]
    n17["Wb_layer"]
    n18["Improc"]
    n19["Timing Controller"]
  end
  n4 --> n2
  n5 --> n2
  n6 --> n3
  n7 --> n3
  n4 --> n8
  n5 --> n8
  n6 --> n8
  n7 --> n8
  n2 --> n8
  n3 --> n8
  n12 --> n9
  n13 --> n9
  n14 --> n10
  n15 --> n10
  n8 --> n16
  n12 --> n16
  n13 --> n16
  n14 --> n16
  n15 --> n16
  n9 --> n16
  n10 --> n16
  n16 --> n11
  n11 --> n17
  n16 --> n18
  n18 --> n19
  n17 --> n0
  n19 --> n1
  style n2 stroke-dasharray:6 4
  style n3 stroke-dasharray:6 4
  style n9 stroke-dasharray:6 4
  style n10 stroke-dasharray:6 4
  style n11 stroke-dasharray:6 4
  style g0 stroke-dasharray:6 4
  style g1 stroke-dasharray:6 4
```

Slave pipeline enabled data flow

<a id="rst-sub-pipelines-for-input-and-output"></a>

### Sub-pipelines for input and output

A complete display pipeline can be easily divided into three sub-pipelines according to the in/out usage.

<a id="rst-layer-input-pipeline"></a>

#### Layer(input) pipeline

```mermaid
flowchart LR
  n0["Scaler-n"]
  n1["Layer-n"]
  n2["Compiz"]
  n1 --> n0
  n0 --> n2
  style n0 stroke-dasharray:6 4
```

Layer (input) data flow

```mermaid
flowchart LR
  n0["Layer-0/1"]
  n1["Scaler-0"]
  n2["Merger"]
  n3["Layer-2/3"]
  n4["Scaler-1"]
  n5["Compiz"]
  n0 --> n1
  n1 --> n2
  n3 --> n4
  n4 --> n2
  n2 --> n5
```

Layer Split pipeline

<a id="rst-writeback-output-pipeline"></a>

#### Writeback(output) pipeline

```mermaid
flowchart LR
  n0["Scaler-n"]
  n1["Compiz"]
  n2["Wb_layer"]
  n1 --> n0
  n0 --> n2
  style n0 stroke-dasharray:6 4
```

Writeback(output) data flow

```mermaid
flowchart LR
  n0["Compiz"]
  n1["Splitter"]
  n2["Scaler-0"]
  n3["Merger"]
  n4["Scaler-1"]
  n5["Wb_layer"]
  n0 --> n1
  n1 --> n2
  n2 --> n3
  n1 --> n4
  n4 --> n3
  n3 --> n5
```

Writeback(output) Split data flow

<a id="rst-display-output-pipeline"></a>

#### Display output pipeline

```mermaid
flowchart LR
  n0["Compiz"]
  n1["Improc"]
  n2["Timing Controller"]
  n0 --> n1
  n1 --> n2
```

display output data flow

In the following section we'll see these three sub-pipelines will be handled by KMS-plane/wb_conn/crtc respectively.

<a id="rst-komeda-resource-abstraction"></a>

## Komeda Resource abstraction

<a id="rst-struct-komeda-pipeline-component"></a>

### struct komeda_pipeline/component

To fully utilize and easily access/configure the HW, the driver side also uses a similar architecture: Pipeline/Component to describe the HW features and capabilities, and a specific component includes two parts:

- Data flow controlling.

- Specific component capabilities and features.

So the driver defines a common header struct komeda_component to describe the data flow control and all specific components are a subclass of this base structure.


#### <a id="c-struct-komeda_component"></a>`struct komeda_component`

**Definition**

```c
struct komeda_component {
    struct drm_private_obj obj;
    struct komeda_pipeline *pipeline;
    char name[32];
    u32 __iomem *reg;
    u32 id;
    u32 hw_id;
    u8 max_active_inputs;
    u8 max_active_outputs;
    u32 supported_inputs;
    u32 supported_outputs;
    const struct komeda_component_funcs *funcs;
};
```

**Members**

- `obj`: treat component as private obj
- `pipeline`: the komeda pipeline this component belongs to
- `name`: component name
- `reg`: component register base,
  which is initialized by chip and used by chip only
- `id`: component id
- `hw_id`: component hw id,
  which is initialized by chip and used by chip only
- `max_active_inputs`:

  **max_active_outputs**:

  maximum number of inputs/outputs that can be active at the same time
  Note:
  the number isn't the bit number of **supported_inputs** or
  **supported_outputs**, but may be less than it, since component may not
  support enabling all **supported_inputs**/outputs at the same time.
- `max_active_outputs`: maximum number of outputs
- `supported_inputs`:

  **supported_outputs**:

  bitmask of BIT(component->id) for the supported inputs/outputs,
  describes the possibilities of how a component is linked into a
  pipeline.
- `supported_outputs`: bitmask of supported output componenet ids
- `funcs`: chip functions to access HW

**Description**

struct komeda_component describe the data flow capabilities for how to link a
component into the display pipeline.
all specified components are subclass of this structure.

#### <a id="c-struct-komeda_component_output"></a>`struct komeda_component_output`

**Definition**

```c
struct komeda_component_output {
    struct komeda_component *component;
    u8 output_port;
};
```

**Members**

- `component`: indicate which component the data comes from
- `output_port`: the output port of the [`komeda_component_output.component`](#c-struct-komeda_component_output)

**Description**

a component has multiple outputs, if want to know where the data
comes from, only know the component is not enough, we still need to know
its output port

#### <a id="c-struct-komeda_component_state"></a>`struct komeda_component_state`

**Definition**

```c
struct komeda_component_state {
    struct drm_private_state obj;
    struct komeda_component *component;
    union {
        struct drm_crtc *crtc;
        struct drm_plane *plane;
        struct drm_connector *wb_conn;
        void *binding_user;
    };
    u16 active_inputs;
    u16 changed_active_inputs;
    u16 affected_inputs;
    struct komeda_component_output inputs[KOMEDA_COMPONENT_N_INPUTS];
};
```

**Members**

- `obj`: tracking component_state by drm_atomic_state
- `component`: backpointer to the component
- `{unnamed_union}`: anonymous
- `crtc`: backpointer for user crtc
- `plane`: backpointer for user plane
- `wb_conn`: backpointer for user wb_connector
- `binding_user`: currently bound user, the user can be **crtc**, **plane** or **wb_conn**,
  which is valid decided by **component** and **inputs**

  - Layer: its user always is plane.
  - compiz/improc/timing_ctrlr: the user is crtc.
  - wb_layer: wb_conn;
  - scaler: plane when input is layer, wb_conn if input is compiz.
- `active_inputs`: active_inputs is bitmask of **inputs** index

  - active_inputs = changed_active_inputs | unchanged_active_inputs
  - affected_inputs = old->active_inputs | new->active_inputs;
  - disabling_inputs = affected_inputs ^ active_inputs;
  - changed_inputs = disabling_inputs | changed_active_inputs;

  NOTE:
  changed_inputs doesn't include all active_input but only
  **changed_active_inputs**, and this bitmask can be used in chip
  level for dirty update.
- `changed_active_inputs`: bitmask of the changed **active_inputs**
- `affected_inputs`: bitmask for affected **inputs**
- `inputs`: the specific inputs\[i\] only valid on BIT(i) has been set in
  **active_inputs**, if not the inputs\[i\] is undefined.

**Description**

component_state is the data flow configuration of the component, and it's
the superclass of all specific component_state like **komeda_layer_state**,
**komeda_scaler_state**

#### <a id="c-struct-komeda_pipeline"></a>`struct komeda_pipeline`

**Definition**

```c
struct komeda_pipeline {
    struct drm_private_obj obj;
    struct komeda_dev *mdev;
    struct clk *pxlclk;
    int id;
    u32 avail_comps;
    u32 standalone_disabled_comps;
    int n_layers;
    struct komeda_layer *layers[KOMEDA_PIPELINE_MAX_LAYERS];
    int n_scalers;
    struct komeda_scaler *scalers[KOMEDA_PIPELINE_MAX_SCALERS];
    struct komeda_compiz *compiz;
    struct komeda_splitter *splitter;
    struct komeda_merger *merger;
    struct komeda_layer  *wb_layer;
    struct komeda_improc *improc;
    struct komeda_timing_ctrlr *ctrlr;
    const struct komeda_pipeline_funcs *funcs;
    struct device_node *of_node;
    struct device_node *of_output_port;
    struct device_node *of_output_links[2];
    bool dual_link;
};
```

**Members**

- `obj`: link pipeline as private obj of drm_atomic_state
- `mdev`: the parent komeda_dev
- `pxlclk`: pixel clock
- `id`: pipeline id
- `avail_comps`: available components mask of pipeline
- `standalone_disabled_comps`: When disable the pipeline, some components can not be disabled
  together with others, but need a sparated and standalone disable.
  The standalone_disabled_comps are the components which need to be
  disabled standalone, and this concept also introduce concept of
  two phase.
  phase 1: for disabling the common components.
  phase 2: for disabling the standalong_disabled_comps.
- `n_layers`: the number of layer on **layers**
- `layers`: the pipeline layers
- `n_scalers`: the number of scaler on **scalers**
- `scalers`: the pipeline scalers
- `compiz`: compositor
- `splitter`: for split the compiz output to two half data flows
- `merger`: merger
- `wb_layer`: writeback layer
- `improc`: post image processor
- `ctrlr`: timing controller
- `funcs`: chip private pipeline functions
- `of_node`: pipeline dt node
- `of_output_port`: pipeline output port
- `of_output_links`: output connector device nodes
- `dual_link`: true if of_output_links\[0\] and \[1\] are both valid

**Description**

Represent a complete display pipeline and hold all functional components.

#### <a id="c-struct-komeda_pipeline_state"></a>`struct komeda_pipeline_state`

**Definition**

```c
struct komeda_pipeline_state {
    struct drm_private_state obj;
    struct komeda_pipeline *pipe;
    struct drm_crtc *crtc;
    u32 active_comps;
};
```

**Members**

- `obj`: tracking pipeline_state by drm_atomic_state
- `pipe`: backpointer to the pipeline
- `crtc`: currently bound crtc
- `active_comps`: bitmask - BIT(component->id) of active components

**NOTE**

Unlike the pipeline, pipeline_state doesn’t gather any component_state
into it. It because all component will be managed by drm_atomic_state.


<a id="rst-resource-discovery-and-initialization"></a>

## Resource discovery and initialization

Pipeline and component are used to describe how to handle the pixel data. We still need a @struct komeda_dev to describe the whole view of the device, and the control-abilites of device.

We have &amp;komeda_dev, &amp;komeda_pipeline, &amp;komeda_component. Now fill devices with pipelines. Since komeda is not for D71 only but also intended for later products, of course we’d better share as much as possible between different products. To achieve this, split the komeda device into two layers: CORE and CHIP.

- CORE: for common features and capabilities handling.

- CHIP: for register programming and HW specific feature (limitation) handling.

CORE can access CHIP by three chip function structures:

- struct komeda_dev_funcs

- struct komeda_pipeline_funcs

- struct komeda_component_funcs


#### <a id="c-struct-komeda_dev_funcs"></a>`struct komeda_dev_funcs`

**Definition**

```c
struct komeda_dev_funcs {
    void (*init_format_table)(struct komeda_dev *mdev);
    int (*enum_resources)(struct komeda_dev *mdev);
    void (*cleanup)(struct komeda_dev *mdev);
    int (*connect_iommu)(struct komeda_dev *mdev);
    int (*disconnect_iommu)(struct komeda_dev *mdev);
    irqreturn_t (*irq_handler)(struct komeda_dev *mdev, struct komeda_events *events);
    int (*enable_irq)(struct komeda_dev *mdev);
    int (*disable_irq)(struct komeda_dev *mdev);
    void (*on_off_vblank)(struct komeda_dev *mdev, int master_pipe, bool on);
    void (*dump_register)(struct komeda_dev *mdev, struct seq_file *seq);
    int (*change_opmode)(struct komeda_dev *mdev, int new_mode);
    void (*flush)(struct komeda_dev *mdev, int master_pipe, u32 active_pipes);
};
```

**Members**

- `init_format_table`: initialize [`komeda_dev->format_table`](#c-struct-komeda_dev), this function should be called
  before the `enum _resource`
- `enum_resources`: for CHIP to report or add pipeline and component resources to CORE
- `cleanup`: call to chip to cleanup komeda_dev->chip data
- `connect_iommu`: Optional, connect to external iommu
- `disconnect_iommu`: Optional, disconnect to external iommu
- `irq_handler`: for CORE to get the HW event from the CHIP when interrupt happened.
- `enable_irq`: enable irq
- `disable_irq`: disable irq
- `on_off_vblank`: notify HW to on/off vblank
- `dump_register`: Optional, dump registers to seq_file
- `change_opmode`: Notify HW to switch to a new display operation mode.
- `flush`: Notify the HW to flush or kickoff the update

**Description**

Supplied by chip level and returned by the chip entry function xxx_identify,

#### <a id="c-struct-komeda_dev"></a>`struct komeda_dev`

**Definition**

```c
struct komeda_dev {
        struct device *dev;
        u32 __iomem   *reg_base;
        struct komeda_chip_info chip;
        struct komeda_format_caps_table fmt_tbl;
        struct clk *aclk;
        int irq;
        struct mutex lock;
        u32 dpmode;
        int n_pipelines;
        struct komeda_pipeline *pipelines[KOMEDA_MAX_PIPELINES];
        const struct komeda_dev_funcs *funcs;
        void *chip_data;
        struct iommu_domain *iommu;
        struct dentry *debugfs_root;
        u16 err_verbosity;
    #define KOMEDA_DEV_PRINT_ERR_EVENTS BIT(0);
    #define KOMEDA_DEV_PRINT_WARN_EVENTS BIT(1);
    #define KOMEDA_DEV_PRINT_INFO_EVENTS BIT(2);
    #define KOMEDA_DEV_PRINT_DUMP_STATE_ON_EVENT BIT(8);
    #define KOMEDA_DEV_PRINT_DISABLE_RATELIMIT BIT(12);
};
```

**Members**

- `dev`: the base device structure
- `reg_base`: the base address of komeda io space
- `chip`: the basic chip information
- `fmt_tbl`: initialized by [`komeda_dev_funcs->init_format_table`](#c-struct-komeda_dev_funcs)
- `aclk`: HW main engine clk
- `irq`: irq number
- `lock`: used to protect dpmode
- `dpmode`: current display mode
- `n_pipelines`: the number of pipe in **pipelines**
- `pipelines`: the komeda pipelines
- `funcs`: chip funcs to access to HW
- `chip_data`: chip data will be added by [`komeda_dev_funcs.enum_resources()`](#c-struct-komeda_dev_funcs) and
  destroyed by [`komeda_dev_funcs.cleanup()`](#c-struct-komeda_dev_funcs)
- `iommu`: iommu domain
- `debugfs_root`: root directory of komeda debugfs
- `err_verbosity`: bitmask for how much extra info to print on error

  See KOMEDA_DEV_* macros for details. Low byte contains the debug
  level categories, the high byte contains extra debug options.

**Description**

Pipeline and component are used to describe how to handle the pixel data.
komeda_device is for describing the whole view of the device, and the
control-abilites of device.


<a id="rst-format-handling"></a>

## Format handling


#### <a id="c-struct-komeda_format_caps"></a>`struct komeda_format_caps`

**Definition**

```c
struct komeda_format_caps {
    u32 hw_id;
    u32 fourcc;
    u32 supported_layer_types;
    u32 supported_rots;
    u32 supported_afbc_layouts;
    u64 supported_afbc_features;
};
```

**Members**

- `hw_id`: hw format id, hw specific value.
- `fourcc`: drm fourcc format.
- `supported_layer_types`: indicate which layer supports this format
- `supported_rots`: allowed rotations for this format
- `supported_afbc_layouts`: supported afbc layerout
- `supported_afbc_features`: supported afbc features

**Description**

komeda_format_caps is for describing ARM display specific features and
limitations for a specific format, and format_caps will be linked into
`komeda_framebuffer` like a extension of [`drm_format_info`](/kernel/hardware/gpu/drm-kms.md#c-drm_format_info).

**NOTE**

one fourcc may has two different format_caps items for fourcc and
fourcc+modifier

#### <a id="c-struct-komeda_format_caps_table"></a>`struct komeda_format_caps_table`

format_caps mananger

**Definition**

```c
struct komeda_format_caps_table {
    u32 n_formats;
    const struct komeda_format_caps *format_caps;
    bool (*format_mod_supported)(const struct komeda_format_caps *caps, u32 layer_type, u64 modifier, u32 rot);
};
```

**Members**

- `n_formats`: the size of format_caps list.
- `format_caps`: format_caps list.
- `format_mod_supported`: Optional. Some HW may have special requirements or
  limitations which can not be described by format_caps, this func supply HW
  the ability to do the further HW specific check.



#### <a id="c-struct-komeda_fb"></a>`struct komeda_fb`

Entending drm_framebuffer with komeda attribute

**Definition**

```c
struct komeda_fb {
    struct drm_framebuffer base;
    const struct komeda_format_caps *format_caps;
    bool is_va;
    u32 aligned_w;
    u32 aligned_h;
    u32 afbc_size;
    u32 offset_payload;
};
```

**Members**

- `base`: [`drm_framebuffer`](/kernel/hardware/gpu/drm-kms.md#c-struct-drm_framebuffer)
- `format_caps`: extends drm_format_info for komeda specific information
- `is_va`: if smmu is enabled, it will be true
- `aligned_w`: aligned frame buffer width
- `aligned_h`: aligned frame buffer height
- `afbc_size`: minimum size of afbc
- `offset_payload`: start of afbc body buffer


<a id="rst-attach-komeda-dev-to-drm-kms"></a>

## Attach komeda_dev to DRM-KMS

Komeda abstracts resources by pipeline/component, but DRM-KMS uses crtc/plane/connector. One KMS-obj cannot represent only one single component, since the requirements of a single KMS object cannot simply be achieved by a single component, usually that needs multiple components to fit the requirement. Like set mode, gamma, ctm for KMS all target on CRTC-obj, but komeda needs compiz, improc and timing_ctrlr to work together to fit these requirements. And a KMS-Plane may require multiple komeda resources: layer/scaler/compiz.

So, one KMS-Obj represents a sub-pipeline of komeda resources.

- Plane: [Layer(input) pipeline](<#rst-layer-input-pipeline>)

- Wb_connector: [Writeback(output) pipeline](<#rst-writeback-output-pipeline>)

- Crtc: [Display output pipeline](<#rst-display-output-pipeline>)

So, for komeda, we treat KMS crtc/plane/connector as users of pipeline and component, and at any one time a pipeline/component only can be used by one user. And pipeline/component will be treated as private object of DRM-KMS; the state will be managed by drm_atomic_state as well.

<a id="rst-how-to-map-plane-to-layer-input-pipeline"></a>

### How to map plane to Layer(input) pipeline

Komeda has multiple Layer input pipelines, see: -   [Single pipeline data flow](<#rst-single-pipeline-data-flow>) -   [Dual pipeline with Slave enabled](<#rst-dual-pipeline-with-slave-enabled>)

The easiest way is binding a plane to a fixed Layer pipeline, but consider the komeda capabilities:

- Layer Split, See [Layer(input) pipeline](<#rst-layer-input-pipeline>)

  Layer_Split is quite complicated feature, which splits a big image into two parts and handles it by two layers and two scalers individually. But it imports an edge problem or effect in the middle of the image after the split. To avoid such a problem, it needs a complicated Split calculation and some special configurations to the layer and scaler. We'd better hide such HW related complexity to user mode.

- Slave pipeline, See [Dual pipeline with Slave enabled](<#rst-dual-pipeline-with-slave-enabled>)

  Since the compiz component doesn't output alpha value, the slave pipeline only can be used for bottom layers composition. The komeda driver wants to hide this limitation to the user. The way to do this is to pick a suitable Layer according to plane_state-&gt;zpos.

So for komeda, the KMS-plane doesn't represent a fixed komeda layer pipeline, but multiple Layers with same capabilities. Komeda will select one or more Layers to fit the requirement of one KMS-plane.

<a id="rst-make-component-pipeline-to-be-drm-private-obj"></a>

### Make component/pipeline to be drm_private_obj

Add `drm_private_obj` to `komeda_component`, `komeda_pipeline`



```c
struct komeda_component {
    struct drm_private_obj obj;
    ...
}

struct komeda_pipeline {
    struct drm_private_obj obj;
    ...
}
```

<a id="rst-tracking-component-state-pipeline-state-by-drm-atomic-state"></a>

### Tracking component_state/pipeline_state by drm_atomic_state

Add `drm_private_state` and user to `komeda_component_state`, `komeda_pipeline_state`



```c
struct komeda_component_state {
    struct drm_private_state obj;
    void *binding_user;
    ...
}

struct komeda_pipeline_state {
    struct drm_private_state obj;
    struct drm_crtc *crtc;
    ...
}
```

<a id="rst-komeda-component-validation"></a>

### komeda component validation

Komeda has multiple types of components, but the process of validation are similar, usually including the following steps:



```c
int komeda_xxxx_validate(struct komeda_component_xxx xxx_comp,
            struct komeda_component_output *input_dflow,
            struct drm_plane/crtc/connector *user,
            struct drm_plane/crtc/connector_state, *user_state)
{
     setup 1: check if component is needed, like the scaler is optional depending
              on the user_state; if unneeded, just return, and the caller will
              put the data flow into next stage.
     Setup 2: check user_state with component features and capabilities to see
              if requirements can be met; if not, return fail.
     Setup 3: get component_state from drm_atomic_state, and try set to set
              user to component; fail if component has been assigned to another
              user already.
     Setup 3: configure the component_state, like set its input component,
              convert user_state to component specific state.
     Setup 4: adjust the input_dflow and prepare it for the next stage.
}
```

<a id="rst-komeda-kms-abstraction"></a>

### komeda_kms Abstraction


#### <a id="c-struct-komeda_plane"></a>`struct komeda_plane`

komeda instance of drm_plane

**Definition**

```c
struct komeda_plane {
    struct drm_plane base;
    struct komeda_layer *layer;
};
```

**Members**

- `base`: [`drm_plane`](/kernel/hardware/gpu/drm-kms.md#c-struct-drm_plane)
- `layer`: represents available layer input pipelines for this plane.

  NOTE:
  the layer is not for a specific Layer, but indicate a group of
  Layers with same capabilities.

#### <a id="c-struct-komeda_plane_state"></a>`struct komeda_plane_state`

**Definition**

```c
struct komeda_plane_state {
    struct drm_plane_state base;
    struct list_head zlist_node;
    u8 layer_split : 1;
};
```

**Members**

- `base`: [`drm_plane_state`](/kernel/hardware/gpu/drm-kms.md#c-struct-drm_plane_state)
- `zlist_node`: zorder list node
- `layer_split`: on/off layer_split

**Description**

The plane_state can be split into two data flow (left/right) and handled
by two layers [`komeda_plane.layer`](#c-struct-komeda_plane) and [`komeda_plane.layer`](#c-struct-komeda_plane).right

#### <a id="c-struct-komeda_wb_connector"></a>`struct komeda_wb_connector`

**Definition**

```c
struct komeda_wb_connector {
    struct drm_writeback_connector base;
    struct komeda_layer *wb_layer;
};
```

**Members**

- `base`: [`drm_writeback_connector`](/kernel/hardware/gpu/drm-kms.md#c-struct-drm_writeback_connector)
- `wb_layer`: represents associated writeback pipeline of komeda

#### <a id="c-struct-komeda_crtc"></a>`struct komeda_crtc`

**Definition**

```c
struct komeda_crtc {
    struct drm_crtc base;
    struct komeda_pipeline *master;
    struct komeda_pipeline *slave;
    u32 slave_planes;
    struct komeda_wb_connector *wb_conn;
    struct completion *disable_done;
    struct drm_encoder encoder;
};
```

**Members**

- `base`: [`drm_crtc`](/kernel/hardware/gpu/drm-kms.md#c-struct-drm_crtc)
- `master`: only master has display output
- `slave`: optional

  Doesn't have its own display output, the handled data flow will
  merge into the master.
- `slave_planes`: komeda slave planes mask
- `wb_conn`: komeda write back connector
- `disable_done`: this flip_done is for tracing the disable
- `encoder`: encoder at the end of the pipeline

#### <a id="c-struct-komeda_crtc_state"></a>`struct komeda_crtc_state`

**Definition**

```c
struct komeda_crtc_state {
    struct drm_crtc_state base;
    u32 affected_pipes;
    u32 active_pipes;
    u64 clock_ratio;
    u32 max_slave_zorder;
};
```

**Members**

- `base`: [`drm_crtc_state`](/kernel/hardware/gpu/drm-kms.md#c-struct-drm_crtc_state)
- `affected_pipes`: the affected pipelines in once display instance
- `active_pipes`: the active pipelines in once display instance
- `clock_ratio`: ratio of (aclk \<\< 32)/pxlclk
- `max_slave_zorder`: the maximum of slave zorder


<a id="rst-komde-kms-functions"></a>

### komde_kms Functions


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

```c
int komeda_crtc_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
```

build display output data flow

**Parameters**

- `struct drm_crtc *crtc`: DRM crtc
- `struct drm_atomic_state *state`: the crtc state object

**Description**

crtc_atomic_check is the final check stage, so beside build a display data
pipeline according to the crtc_state, but still needs to release or disable
the unclaimed pipeline resources.

**Return**

Zero for success or -errno



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

```c
int komeda_plane_atomic_check(struct drm_plane *plane, struct drm_atomic_state *state)
```

build input data flow

**Parameters**

- `struct drm_plane *plane`: DRM plane
- `struct drm_atomic_state *state`: the plane state object

**Return**

Zero for success or -errno


<a id="rst-build-komeda-to-be-a-linux-module-driver"></a>

## Build komeda to be a Linux module driver

Now we have two level devices:

- komeda_dev: describes the real display hardware.

- komeda_kms_dev: attaches or connects komeda_dev to DRM-KMS.

All komeda operations are supplied or operated by komeda_dev or komeda_kms_dev, the module driver is only a simple wrapper to pass the Linux command (probe/remove/pm) into komeda_dev or komeda_kms_dev.
