Skip to main content

strat9_bus_drivers/
stm32_rifsc.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use crate::{
24    BusChild, BusDriver, BusError, PowerState,
25    mmio::MmioRegion,
26    stm32_firewall::{FirewallController, FirewallType},
27};
28use alloc::{string::String, vec::Vec};
29
30const RIFSC_RISC_SECCFGR0: usize = 0x10;
31const RIFSC_RISC_PRIVCFGR0: usize = 0x30;
32const RIFSC_RISC_PER0_CIDCFGR: usize = 0x100;
33const RIFSC_RISC_PER0_SEMCR: usize = 0x104;
34const RIFSC_RISC_HWCFGR2: usize = 0xFEC;
35
36const CIDCFGR_STRIDE: usize = 0x08;
37const CIDCFGR_CFEN: u32 = 1 << 0;
38const CIDCFGR_SEMEN: u32 = 1 << 1;
39const CIDCFGR_SEMWL_MASK: u32 = 0xFF << 16;
40const CIDCFGR_SCID_MASK: u32 = 0x7 << 4;
41const CIDCFGR_SCID_SHIFT: u32 = 4;
42
43const SEMCR_MUTEX: u32 = 1 << 0;
44
45const HWCFGR2_CONF1_MASK: u32 = 0xFFFF;
46const HWCFGR2_CONF2_MASK: u32 = 0xFF << 16;
47const HWCFGR2_CONF2_SHIFT: u32 = 16;
48const HWCFGR2_CONF3_MASK: u32 = 0xFF << 24;
49const HWCFGR2_CONF3_SHIFT: u32 = 24;
50
51const EXPECTED_CID: u32 = 1;
52
53const COMPATIBLE: &[&str] = &["st,stm32mp25-rifsc", "st,stm32mp21-rifsc"];
54
55pub struct Stm32Rifsc {
56    regs: MmioRegion,
57    nb_risup: u32,
58    nb_rimu: u32,
59    nb_risal: u32,
60    power_state: PowerState,
61    children: Vec<BusChild>,
62}
63
64impl Stm32Rifsc {
65    /// Creates a new instance.
66    pub fn new() -> Self {
67        Self {
68            regs: MmioRegion::new(),
69            nb_risup: 0,
70            nb_rimu: 0,
71            nb_risal: 0,
72            power_state: PowerState::Off,
73            children: Vec::new(),
74        }
75    }
76
77    /// Reads hwcfg.
78    fn read_hwcfg(&mut self) {
79        let hwcfg = self.regs.read32(RIFSC_RISC_HWCFGR2);
80        self.nb_risup = hwcfg & HWCFGR2_CONF1_MASK;
81        self.nb_rimu = (hwcfg & HWCFGR2_CONF2_MASK) >> HWCFGR2_CONF2_SHIFT;
82        self.nb_risal = (hwcfg & HWCFGR2_CONF3_MASK) >> HWCFGR2_CONF3_SHIFT;
83    }
84
85    /// Returns whether secure.
86    fn is_secure(&self, id: u32) -> bool {
87        let reg_idx = id / 32;
88        let bit = id % 32;
89        let val = self
90            .regs
91            .read32(RIFSC_RISC_SECCFGR0 + (reg_idx as usize) * 4);
92        (val & (1 << bit)) != 0
93    }
94
95    /// Returns whether privileged.
96    fn is_privileged(&self, id: u32) -> bool {
97        let reg_idx = id / 32;
98        let bit = id % 32;
99        let val = self
100            .regs
101            .read32(RIFSC_RISC_PRIVCFGR0 + (reg_idx as usize) * 4);
102        (val & (1 << bit)) != 0
103    }
104
105    /// Performs the cidcfgr offset operation.
106    fn cidcfgr_offset(id: u32) -> usize {
107        RIFSC_RISC_PER0_CIDCFGR + (id as usize) * CIDCFGR_STRIDE
108    }
109
110    /// Performs the semcr offset operation.
111    fn semcr_offset(id: u32) -> usize {
112        RIFSC_RISC_PER0_SEMCR + (id as usize) * CIDCFGR_STRIDE
113    }
114
115    /// Performs the acquire semaphore operation.
116    fn acquire_semaphore(&self, id: u32) -> Result<(), BusError> {
117        let offset = Self::semcr_offset(id);
118        self.regs.write32(offset, SEMCR_MUTEX);
119        let val = self.regs.read32(offset);
120        if val & SEMCR_MUTEX != 0 {
121            Ok(())
122        } else {
123            Err(BusError::PermissionDenied)
124        }
125    }
126
127    /// Performs the release semaphore operation.
128    fn release_semaphore(&self, id: u32) {
129        let offset = Self::semcr_offset(id);
130        self.regs.write32(offset, 0);
131    }
132
133    /// Performs the add child operation.
134    pub fn add_child(&mut self, child: BusChild) {
135        self.children.push(child);
136    }
137}
138
139impl FirewallController for Stm32Rifsc {
140    /// Performs the name operation.
141    fn name(&self) -> &str {
142        "stm32-rifsc"
143    }
144
145    /// Performs the firewall type operation.
146    fn firewall_type(&self) -> FirewallType {
147        FirewallType::Peripheral
148    }
149
150    /// Performs the max entries operation.
151    fn max_entries(&self) -> u32 {
152        self.nb_risup
153    }
154
155    /// Performs the grant access operation.
156    fn grant_access(&self, firewall_id: u32) -> Result<(), BusError> {
157        if firewall_id >= self.nb_risup {
158            return Err(BusError::InvalidArgument);
159        }
160
161        if self.is_secure(firewall_id) {
162            return Err(BusError::PermissionDenied);
163        }
164
165        let cidcfgr = self.regs.read32(Self::cidcfgr_offset(firewall_id));
166
167        if cidcfgr & CIDCFGR_CFEN != 0 {
168            let scid = (cidcfgr & CIDCFGR_SCID_MASK) >> CIDCFGR_SCID_SHIFT;
169
170            if cidcfgr & CIDCFGR_SEMEN != 0 {
171                let semwl = (cidcfgr & CIDCFGR_SEMWL_MASK) >> 16;
172                if semwl & (1 << EXPECTED_CID) == 0 {
173                    return Err(BusError::PermissionDenied);
174                }
175                return self.acquire_semaphore(firewall_id);
176            }
177
178            if scid != EXPECTED_CID {
179                return Err(BusError::PermissionDenied);
180            }
181        }
182
183        Ok(())
184    }
185
186    /// Performs the release access operation.
187    fn release_access(&self, firewall_id: u32) -> Result<(), BusError> {
188        if firewall_id >= self.nb_risup {
189            return Err(BusError::InvalidArgument);
190        }
191
192        let cidcfgr = self.regs.read32(Self::cidcfgr_offset(firewall_id));
193        if cidcfgr & CIDCFGR_CFEN != 0 && cidcfgr & CIDCFGR_SEMEN != 0 {
194            self.release_semaphore(firewall_id);
195        }
196
197        Ok(())
198    }
199}
200
201impl BusDriver for Stm32Rifsc {
202    /// Performs the name operation.
203    fn name(&self) -> &str {
204        "stm32-rifsc"
205    }
206
207    /// Exposes the firewall capability to the scheme server.
208    fn as_firewall(&self) -> Option<&dyn FirewallController> {
209        Some(self)
210    }
211
212    /// Performs the compatible operation.
213    fn compatible(&self) -> &[&str] {
214        COMPATIBLE
215    }
216
217    /// Performs the init operation.
218    fn init(&mut self, base: usize) -> Result<(), BusError> {
219        self.regs.init(base, 0x1000);
220        self.read_hwcfg();
221        self.power_state = PowerState::On;
222        Ok(())
223    }
224
225    /// Performs the shutdown operation.
226    fn shutdown(&mut self) -> Result<(), BusError> {
227        self.power_state = PowerState::Off;
228        Ok(())
229    }
230
231    /// Reads reg.
232    fn read_reg(&self, offset: usize) -> Result<u32, BusError> {
233        if !self.regs.is_valid() {
234            return Err(BusError::InitFailed);
235        }
236        self.regs.check_user_offset(offset)?;
237        Ok(self.regs.read32(offset))
238    }
239
240    /// Writes reg.
241    fn write_reg(&mut self, offset: usize, value: u32) -> Result<(), BusError> {
242        if !self.regs.is_valid() {
243            return Err(BusError::InitFailed);
244        }
245        self.regs.check_user_offset(offset)?;
246        self.regs.write32(offset, value);
247        Ok(())
248    }
249
250    /// Performs the children operation.
251    fn children(&self) -> Vec<BusChild> {
252        self.children.clone()
253    }
254}