Skip to main content

strat9_bus_drivers/
omap_l3_noc.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::{BusChild, BusDriver, BusError, PowerState, mmio::MmioRegion};
24use alloc::{string::String, vec::Vec};
25use core::sync::atomic::{AtomicU64, Ordering};
26
27const L3_TARG_STDERRLOG_MAIN: usize = 0x48;
28const L3_TARG_STDERRLOG_HDR: usize = 0x4C;
29const L3_TARG_STDERRLOG_MSTADDR: usize = 0x50;
30const L3_TARG_STDERRLOG_INFO: usize = 0x58;
31const L3_TARG_STDERRLOG_SLVOFSLSB: usize = 0x5C;
32const L3_TARG_STDERRLOG_CINFO_INFO: usize = 0x64;
33const L3_TARG_STDERRLOG_CINFO_MSTADDR: usize = 0x68;
34const L3_TARG_STDERRLOG_CINFO_OPCODE: usize = 0x6C;
35
36const L3_FLAGMUX_REGERR0: usize = 0x0C;
37const L3_FLAGMUX_MASK0: usize = 0x08;
38
39const CLEAR_STDERR_LOG: u32 = 1 << 31;
40const CUSTOM_ERROR: u32 = 0x2;
41const STANDARD_ERROR: u32 = 0x0;
42
43const MAX_L3_MODULES: usize = 3;
44
45const COMPATIBLE: &[&str] = &[
46    "ti,omap4-l3-noc",
47    "ti,omap5-l3-noc",
48    "ti,dra7-l3-noc",
49    "ti,am4372-l3-noc",
50];
51
52pub struct L3TargetData {
53    pub offset: u32,
54    pub name: &'static str,
55}
56
57pub struct L3FlagmuxData {
58    pub offset: u32,
59    pub targets: &'static [L3TargetData],
60    pub mask_app_bits: u32,
61    pub mask_dbg_bits: u32,
62}
63
64pub struct L3MasterData {
65    pub id: u32,
66    pub name: &'static str,
67}
68
69pub struct OmapL3Noc {
70    modules: [MmioRegion; MAX_L3_MODULES],
71    num_modules: usize,
72    error_count: AtomicU64,
73    power_state: PowerState,
74    saved_mask_app: [u32; MAX_L3_MODULES],
75    saved_mask_dbg: [u32; MAX_L3_MODULES],
76}
77
78impl OmapL3Noc {
79    /// Creates a new instance.
80    pub fn new() -> Self {
81        Self {
82            modules: [MmioRegion::new(), MmioRegion::new(), MmioRegion::new()],
83            num_modules: 0,
84            error_count: AtomicU64::new(0),
85            power_state: PowerState::Off,
86            saved_mask_app: [0; MAX_L3_MODULES],
87            saved_mask_dbg: [0; MAX_L3_MODULES],
88        }
89    }
90
91    /// Initializes module.
92    pub fn init_module(&mut self, index: usize, base: usize, size: usize) {
93        if index < MAX_L3_MODULES {
94            self.modules[index].init(base, size);
95            if index >= self.num_modules {
96                self.num_modules = index + 1;
97            }
98        }
99    }
100
101    /// Handles target error.
102    pub fn handle_target_error(
103        &self,
104        module: usize,
105        target_offset: usize,
106    ) -> Result<ErrorInfo, BusError> {
107        if module >= self.num_modules || !self.modules[module].is_valid() {
108            return Err(BusError::InvalidArgument);
109        }
110
111        let base_offset = target_offset;
112        let main = self.modules[module].read32(base_offset + L3_TARG_STDERRLOG_MAIN);
113        let hdr = self.modules[module].read32(base_offset + L3_TARG_STDERRLOG_HDR);
114        let mstaddr = self.modules[module].read32(base_offset + L3_TARG_STDERRLOG_MSTADDR);
115
116        let err_type = if (hdr & 0xFF) == CUSTOM_ERROR {
117            ErrorType::Custom
118        } else {
119            ErrorType::Standard
120        };
121
122        let info = match err_type {
123            ErrorType::Standard => {
124                let slave_addr =
125                    self.modules[module].read32(base_offset + L3_TARG_STDERRLOG_SLVOFSLSB);
126                ErrorInfo {
127                    err_type,
128                    master_addr: mstaddr,
129                    slave_addr,
130                    header: hdr,
131                    main_reg: main,
132                }
133            }
134            ErrorType::Custom => {
135                let cinfo = self.modules[module].read32(base_offset + L3_TARG_STDERRLOG_CINFO_INFO);
136                ErrorInfo {
137                    err_type,
138                    master_addr: mstaddr,
139                    slave_addr: cinfo,
140                    header: hdr,
141                    main_reg: main,
142                }
143            }
144        };
145
146        self.modules[module].write32(base_offset + L3_TARG_STDERRLOG_MAIN, CLEAR_STDERR_LOG);
147
148        Ok(info)
149    }
150
151    /// Reads flagmux.
152    pub fn read_flagmux(&self, module: usize, inttype: usize) -> u32 {
153        if module >= self.num_modules || !self.modules[module].is_valid() {
154            return 0;
155        }
156        self.modules[module].read32(L3_FLAGMUX_REGERR0 + (inttype << 3))
157    }
158
159    /// Performs the mask target operation.
160    pub fn mask_target(&self, module: usize, target_bit: u32, inttype: usize) {
161        if module >= self.num_modules || !self.modules[module].is_valid() {
162            return;
163        }
164        let mask_offset = L3_FLAGMUX_MASK0 + (inttype << 3);
165        self.modules[module].set_bits32(mask_offset, 1 << target_bit);
166    }
167}
168
169#[derive(Debug, Clone, Copy)]
170pub enum ErrorType {
171    Standard,
172    Custom,
173}
174
175#[derive(Debug, Clone)]
176pub struct ErrorInfo {
177    pub err_type: ErrorType,
178    pub master_addr: u32,
179    pub slave_addr: u32,
180    pub header: u32,
181    pub main_reg: u32,
182}
183
184impl BusDriver for OmapL3Noc {
185    /// Performs the name operation.
186    fn name(&self) -> &str {
187        "omap-l3-noc"
188    }
189
190    /// Performs the compatible operation.
191    fn compatible(&self) -> &[&str] {
192        COMPATIBLE
193    }
194
195    /// Performs the init operation.
196    fn init(&mut self, base: usize) -> Result<(), BusError> {
197        self.init_module(0, base, 0x1000);
198        self.power_state = PowerState::On;
199        Ok(())
200    }
201
202    /// Performs the shutdown operation.
203    fn shutdown(&mut self) -> Result<(), BusError> {
204        self.power_state = PowerState::Off;
205        Ok(())
206    }
207
208    /// Performs the suspend operation.
209    fn suspend(&mut self) -> Result<(), BusError> {
210        for i in 0..self.num_modules {
211            if self.modules[i].is_valid() {
212                self.saved_mask_app[i] = self.modules[i].read32(L3_FLAGMUX_MASK0);
213                self.saved_mask_dbg[i] = self.modules[i].read32(L3_FLAGMUX_MASK0 + 8);
214            }
215        }
216        self.power_state = PowerState::Suspended;
217        Ok(())
218    }
219
220    /// Performs the resume operation.
221    fn resume(&mut self) -> Result<(), BusError> {
222        for i in 0..self.num_modules {
223            if self.modules[i].is_valid() {
224                self.modules[i].write32(L3_FLAGMUX_MASK0, self.saved_mask_app[i]);
225                self.modules[i].write32(L3_FLAGMUX_MASK0 + 8, self.saved_mask_dbg[i]);
226            }
227        }
228        self.power_state = PowerState::On;
229        Ok(())
230    }
231
232    /// Reads reg.
233    fn read_reg(&self, offset: usize) -> Result<u32, BusError> {
234        if self.num_modules == 0 || !self.modules[0].is_valid() {
235            return Err(BusError::InitFailed);
236        }
237        self.modules[0].check_user_offset(offset)?;
238        Ok(self.modules[0].read32(offset))
239    }
240
241    /// Writes reg.
242    fn write_reg(&mut self, offset: usize, value: u32) -> Result<(), BusError> {
243        if self.num_modules == 0 || !self.modules[0].is_valid() {
244            return Err(BusError::InitFailed);
245        }
246        self.modules[0].check_user_offset(offset)?;
247        self.modules[0].write32(offset, value);
248        Ok(())
249    }
250
251    /// Performs the error count operation.
252    fn error_count(&self) -> u64 {
253        self.error_count.load(Ordering::Relaxed)
254    }
255
256    /// Handles irq.
257    fn handle_irq(&mut self) -> bool {
258        self.error_count.fetch_add(1, Ordering::Relaxed);
259        true
260    }
261}