consortium-cfg 0.4.0

Configuration subsystem support for the Consortium project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright 2026 Ethan Wu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Userspace I/O (`uio_pdrv_genirq`) device-tree node generation.
//!
//! Consortium exposes shared-memory regions and doorbell registers to the Linux
//! application through UIO devices. A [`UioDevice`] renders to a `generic-uio`
//! DTS node and an optional interrupt becomes the UIO IRQ.
//!
//! How the device's windows are described depends on its [`UioBacking`]:
//!
//! - [`UioBacking::Registers`] (doorbells): MMIO register blocks listed inline
//!   with `reg`.
//! - [`UioBacking::ReservedMemory`] (shared RAM): each window is carved out of
//!   system RAM by a `reserved-memory` child node marked `no-map`, and the UIO
//!   node references those carveouts via `memory-region` rather than `reg`. RAM
//!   exposed to a coprocessor must be a `no-map` reservation so Linux never maps
//!   it cacheably or hands it to the page allocator.

use consortium_dts::utils::irq_method::IrqMethod;
use consortium_dts::{Cell, Node, Property, Reference};

use crate::error::CfgError;

/// `compatible` string matched by the Linux `uio_pdrv_genirq` driver.
pub const GENERIC_UIO_COMPATIBLE: &str = "generic-uio";

/// Conversion from a configuration or database resource into a [`UioDevice`].
pub trait ToUio {
    /// Builds the UIO device that exposes this resource to the Linux side.
    ///
    /// Returns `Ok(None)` when the resource exposes nothing, and an error when
    /// the resource cannot be represented as a UIO device.
    fn to_uio(&self) -> Result<Option<UioDevice>, CfgError>;
}

/// How a [`UioDevice`]'s windows are described in the device tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UioBacking {
    /// MMIO peripheral registers, described inline with `reg` (e.g. a doorbell
    /// register block). These are device registers, not RAM, so they are never
    /// a `reserved-memory` carveout.
    #[default]
    Registers,
    /// A RAM carveout: each window becomes a `reserved-memory` child node marked
    /// `no-map`, and the UIO node references them through `memory-region`.
    ReservedMemory,
}

/// Width of the `reg` address/size cells, set by the parent bus's
/// `#address-cells` / `#size-cells`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RegLayout {
    /// 64-bit addressing (`#address-cells = <2>`, `#size-cells = <2>`): each
    /// base and length is a high/low cell pair. This is the AArch64 default.
    #[default]
    Wide,
    /// 32-bit addressing (`#address-cells = <1>`, `#size-cells = <1>`): one
    /// cell per base and length. Used by ARMv7 SoCs (rare).
    Narrow,
}

/// Rendering options for [`UioDevice::to_node_with_options`].
#[derive(Debug, Clone, Default)]
pub struct UioOptions {
    /// `reg` cell width. Defaults to [`RegLayout::Wide`] (AArch64).
    pub layout: RegLayout,
}

/// A device-tree clock reference (`<&provider id>`) attached to a UIO node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UioClock {
    /// Device-tree label of the clock provider (e.g. `scmi_clk`).
    pub provider: String,
    /// Clock cell, emitted verbatim (a binding macro such as `CK_SCMI_IPCC1`).
    pub id: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UioDevice {
    /// address, length
    pub mmap: Vec<(u32, u32)>,
    /// irq spi number, starting from 0, not +32
    pub irq: Option<(u32, IrqMethod)>,
    /// How the windows are described: inline `reg` registers, or a
    /// `reserved-memory`/`memory-region` RAM carveout.
    pub backing: UioBacking,
    /// Whether each `mmap` entry's `reserved-memory` carveout gets
    /// `dma-coherent`. Missing entries are treated as `false`. This stays
    /// per-window because one `ipc-shm` device can aggregate cached and
    /// uncached controller windows. The property does not change the AP's
    /// uncached UIO mapping.
    pub dma_coherent: Vec<bool>,
    /// Device-tree label of the interrupt controller for [`irq`](Self::irq),
    /// emitted as `interrupt-parent = <&…>`. Required whenever the node sits
    /// somewhere the interrupt parent is not inherited (e.g. the STM32MP2 root);
    /// `None` leaves the parent to be inherited from the surrounding tree.
    pub interrupt_parent: Option<String>,
    /// Optional clock kept enabled for this window's register block, emitted as
    /// `clocks = <&provider id>`.
    pub clock: Option<UioClock>,
}

impl UioDevice {
    /// Renders this device as a `generic-uio` DTS node named `name`, using the
    /// default [`UioOptions`] (AArch64 `reg` layout, inherited interrupt parent).
    pub fn to_node(&self, name: impl Into<String>) -> Node {
        self.to_node_with_options(name, &UioOptions::default())
    }

    /// Renders this device as a `generic-uio` DTS node named `name`.
    ///
    /// The node is `<name>@<base>` using the first mapping's base address
    /// (no unit address when there are no mappings) and carries the
    /// `generic-uio` `compatible` string, `linux,uio-name = "<name>"` (the sysfs
    /// name the application opens it by), an optional GIC SPI `interrupts` entry
    /// (with an optional `interrupt-parent`), and `status = "okay";`.
    ///
    /// The windows are described per [`backing`](UioDevice::backing):
    /// [`UioBacking::Registers`] lists each window inline with `reg`;
    /// [`UioBacking::ReservedMemory`] instead references the `reserved-memory`
    /// carveouts (see [`reserved_memory_nodes`](Self::reserved_memory_nodes))
    /// through `memory-region`.
    pub fn to_node_with_options(&self, name: impl Into<String>, options: &UioOptions) -> Node {
        let name = name.into();

        let mut node = match self.mmap.first() {
            Some((base, _)) => Node::with_unit_address(name.clone(), hex(*base)),
            None => Node::new(name.clone()),
        };

        node = node.property(Property::string("compatible", GENERIC_UIO_COMPATIBLE));
        // The name `uio_pdrv_genirq` reports in sysfs, which the generated
        // application looks the device up by (`UioDevice::open_named`).
        // Without it the kernel reports `<name>@<unit-address>`.
        node = node.property(Property::string("linux,uio-name", name.clone()));

        if !self.mmap.is_empty() {
            match self.backing {
                UioBacking::Registers => {
                    let cells = self
                        .mmap
                        .iter()
                        .flat_map(|(base, length)| reg_cells(*base, *length, options.layout));
                    node = node.property(Property::cells("reg", cells));
                }
                UioBacking::ReservedMemory => {
                    // Reference one carveout phandle per window, in mapping order.
                    let refs = (0..self.mmap.len()).map(|index| {
                        Cell::reference(Reference::label(reserved_label(
                            &name,
                            index,
                            self.mmap.len(),
                        )))
                    });
                    node = node.property(Property::cells("memory-region", refs));
                }
            }
        }

        if let Some((spi, method)) = self.irq {
            if let Some(parent) = &self.interrupt_parent {
                // `interrupt-parent` is a phandle *cell* (`<&intc>`), not a bare
                // path reference (`&intc`): the latter encodes as a path string
                // and makes the kernel's interrupt parsing (and dtc) choke.
                node = node.property(Property::cells(
                    "interrupt-parent",
                    [Cell::reference(Reference::label(parent.as_str()))],
                ));
            }
            // GIC SPI form: <GIC_SPI <spi> <trigger>>. The `GIC_SPI` macro
            // applies the +32 SPI base, so `spi` is the raw SPI number.
            node = node.property(Property::cells(
                "interrupts",
                [
                    Cell::raw("GIC_SPI"),
                    Cell::number(u64::from(spi)),
                    Cell::raw(irq_trigger_macro(method)),
                ],
            ));
        }

        // Keep the register block clocked from the Linux side. The id is a
        // binding macro name emitted verbatim; the vendor base tree's `#include`d
        // clock header resolves it.
        if let Some(clock) = &self.clock {
            node = node.property(Property::cells(
                "clocks",
                [
                    Cell::reference(Reference::label(clock.provider.as_str())),
                    Cell::raw(clock.id.as_str()),
                ],
            ));
        }

        node.status_okay()
    }

    /// The `reserved-memory` carveout child nodes backing this device, using the
    /// default [`UioOptions`].
    ///
    /// Empty for a [`UioBacking::Registers`] device. Place the returned nodes
    /// under a `/reserved-memory` parent (see [`reserved_memory_container`]); the
    /// node from [`to_node`](Self::to_node) references them by label.
    pub fn reserved_memory_nodes(&self, name: impl Into<String>) -> Vec<Node> {
        self.reserved_memory_nodes_with_options(name, &UioOptions::default())
    }

    /// The `reserved-memory` carveout child nodes backing this device.
    ///
    /// One labeled, `no-map` node per window, named `<name>@<base>`, with a `reg`
    /// in the requested [`layout`](UioOptions::layout). The label matches the
    /// phandle the UIO node's `memory-region` references. Empty for a
    /// [`UioBacking::Registers`] device.
    pub fn reserved_memory_nodes_with_options(
        &self,
        name: impl Into<String>,
        options: &UioOptions,
    ) -> Vec<Node> {
        if self.backing != UioBacking::ReservedMemory {
            return Vec::new();
        }
        let name = name.into();
        let count = self.mmap.len();
        self.mmap
            .iter()
            .enumerate()
            .map(|(index, (base, length))| {
                let mut node = Node::with_unit_address(name.clone(), hex(*base))
                    .label(reserved_label(&name, index, count))
                    .property(Property::cells(
                        "reg",
                        reg_cells(*base, *length, options.layout),
                    ));
                if self.dma_coherent.get(index).copied().unwrap_or(false) {
                    node = node.property(Property::boolean("dma-coherent"));
                }
                node
                    // `no-map`: keep Linux from mapping the carveout cacheably or
                    // handing it to the allocator; the coprocessor owns it.
                    .property(Property::boolean("no-map"))
            })
            .collect()
    }
}

/// Builds the `/reserved-memory` container holding `children`, with cell widths
/// matching `layout` and an empty `ranges`, or `None` when there are no
/// children.
///
/// Aggregate the [`reserved_memory_nodes`](UioDevice::reserved_memory_nodes) of
/// every [`UioBacking::ReservedMemory`] device into one container at the device
/// tree root.
pub fn reserved_memory_container(
    children: impl IntoIterator<Item = Node>,
    layout: RegLayout,
) -> Option<Node> {
    let children: Vec<Node> = children.into_iter().collect();
    if children.is_empty() {
        return None;
    }
    let cells = match layout {
        RegLayout::Wide => 2u64,
        RegLayout::Narrow => 1u64,
    };
    let mut node = Node::new("reserved-memory")
        .property(Property::cells("#address-cells", [Cell::number(cells)]))
        .property(Property::cells("#size-cells", [Cell::number(cells)]))
        .property(Property::boolean("ranges"));
    for child in children {
        node = node.child(child);
    }
    Some(node)
}

/// The device-tree label for window `index` of a `reserved-memory` device named
/// `name` (`count` windows total).
///
/// Labels cannot contain `-`, so hyphens become underscores; with more than one
/// window the index is appended (`ipc_shm_0`, `ipc_shm_1`).
fn reserved_label(name: &str, index: usize, count: usize) -> String {
    let base = name.replace('-', "_");
    if count > 1 {
        format!("{base}_{index}")
    } else {
        base
    }
}

/// The `reg` cells for one window under the given address/size cell width.
///
/// Bases and lengths are `u32`, so the [`RegLayout::Wide`] high cells are
/// always `0x0`.
fn reg_cells(base: u32, length: u32, layout: RegLayout) -> Vec<Cell> {
    match layout {
        RegLayout::Wide => vec![
            Cell::raw("0x0"),
            Cell::raw(hex(base)),
            Cell::raw("0x0"),
            Cell::raw(hex(length)),
        ],
        RegLayout::Narrow => vec![Cell::raw(hex(base)), Cell::raw(hex(length))],
    }
}

/// The `dt-bindings/interrupt-controller/irq.h` macro name for a trigger type.
fn irq_trigger_macro(method: IrqMethod) -> &'static str {
    match method {
        IrqMethod::None => "IRQ_TYPE_NONE",
        IrqMethod::EdgeRising => "IRQ_TYPE_EDGE_RISING",
        IrqMethod::EdgeFalling => "IRQ_TYPE_EDGE_FALLING",
        IrqMethod::EdgeBoth => "IRQ_TYPE_EDGE_BOTH",
        IrqMethod::LevelHigh => "IRQ_TYPE_LEVEL_HIGH",
        IrqMethod::LevelLow => "IRQ_TYPE_LEVEL_LOW",
    }
}

/// Formats an address/size as a `0x`-prefixed lowercase hex literal, matching
/// device-tree `reg` conventions.
fn hex(value: u32) -> String {
    format!("0x{value:x}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use consortium_dts::ToDts;

    /// A register-backed device (the doorbell case).
    fn registers(mmap: Vec<(u32, u32)>, irq: Option<(u32, IrqMethod)>) -> UioDevice {
        UioDevice {
            mmap,
            irq,
            backing: UioBacking::Registers,
            dma_coherent: Vec::new(),
            interrupt_parent: None,
            clock: None,
        }
    }

    /// A reserved-memory-backed device (the shared-memory case).
    fn reserved(mmap: Vec<(u32, u32)>) -> UioDevice {
        UioDevice {
            mmap,
            irq: None,
            backing: UioBacking::ReservedMemory,
            dma_coherent: Vec::new(),
            interrupt_parent: None,
            clock: None,
        }
    }

    #[test]
    fn renders_single_window_generic_uio() {
        let device = registers(vec![(0x0049_0000, 0x400)], None);

        // Default AArch64 layout: 2 address cells + 2 size cells per window.
        let dts = device.to_node("ipcc1").to_dts();
        assert!(dts.contains("ipcc1@490000 {"));
        assert!(dts.contains("compatible = \"generic-uio\";"));
        assert!(dts.contains("reg = <0x0 0x490000 0x0 0x400>;"));
        assert!(dts.contains("status = \"okay\";"));
        assert!(!dts.contains("interrupts"));
    }

    #[test]
    fn aggregates_multiple_register_mappings() {
        let device = registers(vec![(0x0040_0000, 0x8000), (0x0040_8000, 0x2000)], None);

        let dts = device.to_node("regs").to_dts();
        assert!(dts.contains("reg = <0x0 0x400000 0x0 0x8000 0x0 0x408000 0x0 0x2000>;"));
    }

    #[test]
    fn dma_coherent_is_only_on_reserved_memory_node() {
        let mut device = reserved(vec![(0x0040_0000, 0x8000), (0x0040_8000, 0x2000)]);
        device.dma_coherent = vec![false, true];

        let uio = device.to_node("ipc-shm").to_dts();
        let reserved = device.reserved_memory_nodes("ipc-shm");
        assert!(!uio.contains("dma-coherent"));
        assert!(!reserved[0].to_dts().contains("dma-coherent"));
        assert!(reserved[1].to_dts().contains("dma-coherent;"));
        assert!(
            reserved
                .iter()
                .all(|node| node.to_dts().contains("no-map;"))
        );
    }

    #[test]
    fn narrow_layout_uses_single_cells() {
        let device = registers(vec![(0x0040_0000, 0x8000)], None);

        let options = UioOptions {
            layout: RegLayout::Narrow,
        };
        let dts = device.to_node_with_options("regs", &options).to_dts();
        assert!(dts.contains("reg = <0x400000 0x8000>;"));
    }

    #[test]
    fn renders_gic_spi_interrupt() {
        let mut device = registers(
            vec![(0x0049_0000, 0x400)],
            Some((171, IrqMethod::LevelHigh)),
        );
        device.interrupt_parent = Some("intc".to_string());

        let dts = device.to_node("ipcc1").to_dts();
        assert!(dts.contains("interrupt-parent = <&intc>;"));
        assert!(dts.contains("interrupts = <GIC_SPI 171 IRQ_TYPE_LEVEL_HIGH>;"));
    }

    #[test]
    fn renders_doorbell_clock() {
        let mut device = registers(
            vec![(0x0049_0000, 0x400)],
            Some((171, IrqMethod::LevelHigh)),
        );
        device.interrupt_parent = Some("intc".to_string());
        device.clock = Some(UioClock {
            provider: "scmi_clk".to_string(),
            id: "CK_SCMI_IPCC1".to_string(),
        });

        let dts = device.to_node("ipcc1").to_dts();
        assert!(dts.contains("clocks = <&scmi_clk CK_SCMI_IPCC1>;"));
    }

    #[test]
    fn omits_unit_address_without_mappings() {
        let device = registers(vec![], None);
        let node = device.to_node("uio");
        assert_eq!(node.unit_address, None);
    }

    #[test]
    fn reserved_memory_device_references_carveout_not_reg() {
        let device = reserved(vec![(0x2040_0000, 0x8000)]);

        // The UIO node references the carveout phandle; no inline `reg`.
        let dts = device.to_node("ipc-shm").to_dts();
        assert!(dts.contains("ipc-shm@20400000 {"));
        assert!(dts.contains("memory-region = <&ipc_shm>;"));
        assert!(!dts.contains("reg ="));

        // The carveout node is labeled, sized via `reg`, and `no-map`.
        let nodes = device.reserved_memory_nodes("ipc-shm");
        assert_eq!(nodes.len(), 1);
        let carveout = nodes[0].to_dts();
        assert!(carveout.contains("ipc_shm: ipc-shm@20400000 {"));
        assert!(carveout.contains("reg = <0x0 0x20400000 0x0 0x8000>;"));
        assert!(carveout.contains("no-map;"));
    }

    #[test]
    fn reserved_memory_multiple_windows_index_labels() {
        let device = reserved(vec![(0x2040_0000, 0x8000), (0x2040_8000, 0x2000)]);

        let dts = device.to_node("ipc-shm").to_dts();
        assert!(dts.contains("memory-region = <&ipc_shm_0 &ipc_shm_1>;"));

        let nodes = device.reserved_memory_nodes("ipc-shm");
        assert_eq!(nodes.len(), 2);
        assert!(nodes[0].to_dts().contains("ipc_shm_0: ipc-shm@20400000 {"));
        assert!(nodes[1].to_dts().contains("ipc_shm_1: ipc-shm@20408000 {"));
    }

    #[test]
    fn register_device_has_no_reserved_memory_nodes() {
        let device = registers(vec![(0x0049_0000, 0x400)], None);
        assert!(device.reserved_memory_nodes("ipcc1").is_empty());
    }

    #[test]
    fn reserved_memory_container_wraps_children() {
        let device = reserved(vec![(0x2040_0000, 0x8000)]);
        let children = device.reserved_memory_nodes("ipc-shm");

        let container = reserved_memory_container(children, RegLayout::Wide)
            .expect("children present yields a container");
        let dts = container.to_dts();
        assert!(dts.contains("reserved-memory {"));
        assert!(dts.contains("#address-cells = <2>;"));
        assert!(dts.contains("#size-cells = <2>;"));
        assert!(dts.contains("ranges;"));
        assert!(dts.contains("ipc_shm: ipc-shm@20400000 {"));

        // No children -> no container.
        assert!(reserved_memory_container(Vec::new(), RegLayout::Wide).is_none());
    }
}