xref: /DragonOS/kernel/src/arch/riscv64/driver/of.rs (revision dd8e74ef0d7f91a141bd217736bef4fe7dc6df3d)
1 use system_error::SystemError;
2 
3 use crate::{
4     driver::open_firmware::fdt::OpenFirmwareFdtDriver,
5     init::boot_params,
6     kdebug,
7     libs::align::page_align_up,
8     mm::{mmio_buddy::mmio_pool, MemoryManagementArch, PhysAddr},
9 };
10 
11 impl OpenFirmwareFdtDriver {
12     #[allow(dead_code)]
13     pub unsafe fn map_fdt(&self) -> Result<(), SystemError> {
14         let bp_guard = boot_params().read();
15         let fdt_size = bp_guard.arch.fdt_size;
16         let fdt_paddr = bp_guard.arch.fdt_paddr;
17 
18         let offset = fdt_paddr.data() & crate::arch::MMArch::PAGE_OFFSET_MASK;
19         let map_size = page_align_up(fdt_size + offset);
20         let map_paddr = PhysAddr::new(fdt_paddr.data() & crate::arch::MMArch::PAGE_MASK);
21         // kdebug!(
22         //     "map_fdt paddr: {:?}, map_pa: {:?},fdt_size: {},  size: {:?}",
23         //     fdt_paddr,
24         //     map_paddr,
25         //     fdt_size,
26         //     map_size
27         // );
28         let mmio_guard = mmio_pool().create_mmio(map_size)?;
29 
30         // drop the boot params guard in order to avoid deadlock
31         drop(bp_guard);
32         // kdebug!("map_fdt: map fdt to {:?}, size: {}", map_paddr, map_size);
33         mmio_guard.map_phys(map_paddr, map_size)?;
34         let mut bp_guard = boot_params().write();
35         let vaddr = mmio_guard.vaddr() + offset;
36 
37         self.set_fdt_map_guard(Some(mmio_guard));
38         bp_guard.arch.fdt_vaddr.replace(vaddr);
39 
40         return Ok(());
41     }
42 }
43