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