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
|
// SPDX-License-Identifier: GPL-2.0 or MIT
//! Firmware loading and management for Mali CSF GPU.
//!
//! This module handles loading the Mali GPU firmware binary, parsing it into sections,
//! and mapping those sections into the MCU's virtual address space. Each firmware section
//! has specific properties (read/write/execute permissions, cache modes) and must be loaded
//! at specific virtual addresses expected by the MCU.
//!
//! See [`Firmware`] for the main firmware management interface and [`Section`] for
//! individual firmware sections.
//!
//! [`Firmware`]: crate::fw::Firmware
//! [`Section`]: crate::fw::Section
use kernel::{
device::{
Bound,
Device, //
},
drm::{
gem::BaseObject, //
},
io::{
poll,
Io, //
},
num::Bounded,
prelude::*,
register,
str::CString,
sync::{
Arc,
ArcBorrow, //
},
time, //
};
use crate::{
driver::{
IoMem,
TyrDrmDevice, //
},
fw::parser::{
FwParser,
ParsedSection, //
},
gem,
gem::{
KernelBo,
KernelBoVaAlloc, //
},
gpu::GpuInfo,
mmu::Mmu,
regs::{
gpu_control::{
McuControlMode,
McuStatus,
GPU_ID,
MCU_CONTROL,
MCU_STATUS, //
}, //
job_control::{
JOB_IRQ_CLEAR,
JOB_IRQ_RAWSTAT, //
}, //
},
vm::Vm, //
};
mod parser;
pub(super) const CSF_MCU_SHARED_REGION_START: u32 = 0x04000000;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub(super) enum CacheMode {
None = 0,
Cached = 1,
UncachedCoherent = 2,
CachedCoherent = 3,
}
impl From<Bounded<u32, 2>> for CacheMode {
fn from(value: Bounded<u32, 2>) -> Self {
match value.get() {
0 => Self::None,
1 => Self::Cached,
2 => Self::UncachedCoherent,
3 => Self::CachedCoherent,
_ => unreachable!(),
}
}
}
impl From<CacheMode> for Bounded<u32, 2> {
fn from(value: CacheMode) -> Self {
Bounded::try_new(value as u32).unwrap()
}
}
register! {
#[allow(non_upper_case_globals)]
pub(super) SectionFlags(u32) @ 0x0 {
0:0 read => bool;
1:1 write => bool;
2:2 exec => bool;
4:3 cache_mode => CacheMode;
5:5 prot => bool;
30:30 shared => bool;
31:31 zero => bool;
}
}
impl SectionFlags {
const VALID_MASK: u32 = Self::READ_MASK
| Self::WRITE_MASK
| Self::EXEC_MASK
| Self::CACHE_MODE_MASK
| Self::PROT_MASK
| Self::SHARED_MASK
| Self::ZERO_MASK;
fn try_from_fw(value: u32) -> Result<Self> {
if value & !Self::VALID_MASK != 0 {
Err(EINVAL)
} else {
Ok(Self::from_raw(value))
}
}
}
/// A parsed section of the firmware binary.
struct Section<'drm> {
// Raw firmware section data for reset purposes
#[expect(dead_code)]
data: KVec<u8>,
// Keep the BO backing this firmware section so that both the
// GPU mapping and CPU mapping remain valid until the Section is dropped.
#[expect(dead_code)]
mem: gem::KernelBo<'drm>,
}
/// Loaded firmware with sections mapped into MCU VM.
pub(crate) struct Firmware<'drm> {
/// Iomem need to access registers.
iomem: Arc<IoMem<'drm>>,
/// MCU VM.
vm: Arc<Vm<'drm>>,
/// List of firmware sections.
#[expect(dead_code)]
sections: KVec<Section<'drm>>,
}
impl<'drm> Drop for Firmware<'drm> {
fn drop(&mut self) {
// Stop the MCU before releasing its firmware mappings and memory.
let _ = self.stop();
// AS slots retain a VM ref, we need to kill the circular ref manually.
self.vm.kill();
}
}
impl<'drm> Firmware<'drm> {
fn init_section_mem(dev: &Device, mem: &mut KernelBo<'drm>, data: &KVec<u8>) -> Result {
if data.is_empty() {
return Ok(());
}
let vmap = mem.bo().vmap::<0>()?;
let size = mem.bo().size();
if data.len() > size {
dev_err!(dev, "fw section {} bigger than BO {}", data.len(), size);
return Err(EINVAL);
}
for (i, &byte) in data.iter().enumerate() {
vmap.try_write8(byte, i)?;
}
Ok(())
}
fn request(ddev: &TyrDrmDevice, gpu_info: &GpuInfo) -> Result<kernel::firmware::Firmware> {
let gpu_id = GPU_ID::from_raw(gpu_info.gpu_id);
let path = CString::try_from_fmt(fmt!(
"arm/mali/arch{}.{}/mali_csffw.bin",
gpu_id.arch_major().get(),
gpu_id.arch_minor().get()
))?;
kernel::firmware::Firmware::request(&path, ddev.as_ref().as_ref())
}
fn load(
dev: &Device,
ddev: &TyrDrmDevice,
gpu_info: &GpuInfo,
) -> Result<(kernel::firmware::Firmware, KVec<ParsedSection>)> {
let fw = Self::request(ddev, gpu_info)?;
let mut parser = FwParser::new(dev, fw.data());
let parsed_sections = parser.parse()?;
Ok((fw, parsed_sections))
}
/// Load firmware and map sections into MCU VM.
pub(crate) fn new(
dev: &'drm Device<Bound>,
iomem: Arc<IoMem<'drm>>,
ddev: &TyrDrmDevice,
mmu: ArcBorrow<'_, Mmu<'drm>>,
gpu_info: &GpuInfo,
) -> Result<Firmware<'drm>> {
let vm = Vm::new(dev, ddev, mmu, gpu_info)?;
vm.activate()?;
let result = (|| {
let (fw, parsed_sections) = Self::load(dev, ddev, gpu_info)?;
let mut sections = KVec::new();
for parsed in parsed_sections {
let size = u64::from(parsed.va.end.checked_sub(parsed.va.start).ok_or(EINVAL)?);
let va = u64::from(parsed.va.start);
let mut mem = KernelBo::new(
ddev,
vm.clone(),
size,
KernelBoVaAlloc::Explicit(va),
parsed.vm_map_flags,
)?;
let section_start = parsed.data_range.start as usize;
let section_end = parsed.data_range.end as usize;
let mut data = KVec::new();
// Ensure that the firmware slice is not out of bounds.
let fw_data = fw.data();
let bytes = fw_data.get(section_start..section_end).ok_or(EINVAL)?;
data.extend_from_slice(bytes, GFP_KERNEL)?;
Self::init_section_mem(dev, &mut mem, &data)?;
sections.push(Section { data, mem }, GFP_KERNEL)?;
}
Ok(Firmware {
iomem,
vm: vm.clone(),
sections,
})
})();
if result.is_err() {
vm.kill();
}
result
}
pub(crate) fn boot(&self) -> Result {
let io = &self.iomem;
// Discard any stale global interrupt.
io.write_reg(JOB_IRQ_CLEAR::zeroed().with_glb(true));
io.write_reg(MCU_CONTROL::zeroed().with_req(McuControlMode::Auto));
if let Err(e) = poll::read_poll_timeout(
|| Ok((io.read(MCU_STATUS), io.read(JOB_IRQ_RAWSTAT))),
|(mcu_status, irq_rawstat)| {
mcu_status.value() == McuStatus::Enabled && irq_rawstat.glb()
},
time::Delta::from_millis(1),
time::Delta::from_millis(100),
) {
let status = io.read(MCU_STATUS);
dev_err!(
self.vm.dev(),
"MCU failed to boot, status: {:?}",
status.value()
);
return Err(e);
}
io.write_reg(JOB_IRQ_CLEAR::zeroed().with_glb(true));
Ok(())
}
fn stop(&self) -> Result {
let io = &self.iomem;
io.write_reg(MCU_CONTROL::zeroed().with_req(McuControlMode::Disable));
if let Err(e) = poll::read_poll_timeout(
|| Ok(io.read(MCU_STATUS)),
|status| status.value() == McuStatus::Disabled,
time::Delta::from_micros(10),
time::Delta::from_millis(100),
) {
let status = io.read(MCU_STATUS);
dev_err!(
self.vm.dev(),
"MCU failed to stop, status: {:?}",
status.value()
);
return Err(e);
}
Ok(())
}
}
|