Skip to content

Commit fa30f5f

Browse files
committed
aarch64: fix bus address for mailbox property
1 parent d56a889 commit fa30f5f

File tree

8 files changed

+47
-39
lines changed

8 files changed

+47
-39
lines changed

bootloader/Cargo.lock

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bootloader/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fixedvec = "0.2.3"
1010

1111
[target.'cfg(target_arch = "aarch64")'.dependencies]
1212
aarch64 = { git = "https://github.com/rcore-os/aarch64", version = "2.7.0" }
13-
bcm2837 = { git = "https://github.com/rcore-os/bcm2837", version = "1.0.0" }
13+
bcm2837 = { git = "https://github.com/rcore-os/bcm2837", version = "2.0.0" }
1414

1515
[build-dependencies]
1616
cc = "1.0"

bootloader/src/arch/aarch64/mod.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@ use aarch64::addr::{PhysAddr, VirtAddr};
22
use aarch64::paging::{memory_attribute::*, Page, PageTable, PageTableAttribute, PageTableFlags as EF, PhysFrame};
33
use aarch64::paging::{Size1GiB, Size2MiB, Size4KiB};
44
use aarch64::{asm::*, barrier, regs::*};
5-
use bcm2837::consts::RAW_IO_BASE;
5+
use bcm2837::addr::{phys_to_virt, virt_to_phys, PHYSICAL_IO_BASE};
66
use core::ptr;
77
use fixedvec::FixedVec;
88
use xmas_elf::program::{ProgramHeader64, Type};
99

1010
const PAGE_SIZE: usize = 4096;
1111
const ALIGN_2MB: u64 = 0x200000;
1212

13-
const PHYSICAL_MEMORY_OFFSET: u64 = 0xFFFF_0000_0000_0000;
14-
1513
global_asm!(include_str!("boot.S"));
1614

17-
/// Convert physical address to virtual address
18-
const fn phys_to_virt(paddr: u64) -> u64 {
19-
PHYSICAL_MEMORY_OFFSET + paddr
20-
}
21-
22-
/// Convert virtual address to physical address
23-
const fn virt_to_phys(vaddr: u64) -> u64 {
24-
vaddr - PHYSICAL_MEMORY_OFFSET
25-
}
26-
2715
// TODO: set segments permission
2816
fn create_page_table(start_paddr: usize, end_paddr: usize) {
2917
#[repr(align(4096))]
@@ -46,14 +34,14 @@ fn create_page_table(start_paddr: usize, end_paddr: usize) {
4634
// normal memory
4735
for frame in PhysFrame::<Size2MiB>::range_of(start_paddr as u64, end_paddr as u64) {
4836
let paddr = frame.start_address();
49-
let vaddr = VirtAddr::new(phys_to_virt(paddr.as_u64()));
37+
let vaddr = VirtAddr::new(phys_to_virt(paddr.as_u64() as usize) as u64);
5038
let page = Page::<Size2MiB>::containing_address(vaddr);
5139
p2[page.p2_index()].set_block::<Size2MiB>(paddr, block_flags, MairNormal::attr_value());
5240
}
5341
// device memory
54-
for frame in PhysFrame::<Size2MiB>::range_of(RAW_IO_BASE as u64, 0x4000_0000) {
42+
for frame in PhysFrame::<Size2MiB>::range_of(PHYSICAL_IO_BASE as u64, 0x4000_0000) {
5543
let paddr = frame.start_address();
56-
let vaddr = VirtAddr::new(phys_to_virt(paddr.as_u64()));
44+
let vaddr = VirtAddr::new(phys_to_virt(paddr.as_u64() as usize) as u64);
5745
let page = Page::<Size2MiB>::containing_address(vaddr);
5846
p2[page.p2_index()].set_block::<Size2MiB>(paddr, block_flags | EF::PXN, MairDevice::attr_value());
5947
}
@@ -133,7 +121,7 @@ pub fn map_kernel(kernel_start: usize, segments: &FixedVec<ProgramHeader64>) {
133121

134122
unsafe {
135123
let src = (kernel_start as u64 + offset) as *const u8;
136-
let dst = virt_to_phys(virt_addr) as *mut u8;
124+
let dst = virt_to_phys(virt_addr as usize) as *mut u8;
137125
ptr::copy(src, dst, file_size as usize);
138126
ptr::write_bytes(dst.offset(file_size as isize), 0, (mem_size - file_size) as usize);
139127
}
@@ -146,6 +134,6 @@ pub fn map_kernel(kernel_start: usize, segments: &FixedVec<ProgramHeader64>) {
146134
}
147135
}
148136

149-
create_page_table(0, RAW_IO_BASE);
137+
create_page_table(0, PHYSICAL_IO_BASE);
150138
enable_mmu();
151139
}

kernel/Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kernel/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
8787

8888
[target.'cfg(target_arch = "aarch64")'.dependencies]
8989
aarch64 = { git = "https://github.com/rcore-os/aarch64", version = "2.7.0" }
90-
bcm2837 = { git = "https://github.com/rcore-os/bcm2837", version = "1.0.0", optional = true }
90+
bcm2837 = { git = "https://github.com/rcore-os/bcm2837", version = "2.0.0", optional = true }
9191

9292
[target.'cfg(target_arch = "mips")'.dependencies]
9393
mips = "^0.2.0"

kernel/src/arch/aarch64/board/raspi3/mailbox.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
//! (ref: https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface)
44
55
use super::fb::FramebufferInfo;
6+
use crate::memory::virt_to_phys;
67
use aarch64::asm;
78
use alloc::string::String;
9+
use bcm2837::addr::phys_to_bus;
810
use bcm2837::mailbox::{Mailbox, MailboxChannel};
911
use core::mem;
1012
use lazy_static::lazy_static;
@@ -185,15 +187,18 @@ macro_rules! send_request {
185187
end_tag: RPI_FIRMWARE_PROPERTY_END,
186188
});
187189

188-
let start = &req as *const _ as u32;
189-
let end = start + req.0.buf_size;
190+
let start = &req as *const _ as usize;
191+
let end = start + req.0.buf_size as usize;
190192
{
191193
// flush data cache around mailbox accesses
192194
let mut mbox = MAILBOX.lock();
193-
asm::flush_dcache_range(start as usize, end as usize);
194-
mbox.write(MailboxChannel::Property, start);
195+
asm::flush_dcache_range(start, end);
196+
mbox.write(
197+
MailboxChannel::Property,
198+
phys_to_bus(virt_to_phys(start) as u32),
199+
);
195200
mbox.read(MailboxChannel::Property);
196-
asm::flush_dcache_range(start as usize, end as usize);
201+
asm::flush_dcache_range(start, end);
197202
}
198203

199204
match req.0.req_resp_code {

kernel/src/arch/aarch64/board/raspi3/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Raspberry PI 3 Model B/B+
22
3-
use bcm2837::atags::Atags;
3+
use bcm2837::{addr::bus_to_phys, atags::Atags};
44

55
pub mod emmc;
66
#[path = "../../../../drivers/gpu/fb.rs"]
@@ -12,9 +12,6 @@ pub mod timer;
1212

1313
use fb::{ColorConfig, FramebufferResult};
1414

15-
pub const IO_REMAP_BASE: usize = bcm2837::consts::IO_BASE;
16-
pub const IO_REMAP_END: usize = bcm2837::consts::KERNEL_OFFSET + 0x4000_1000;
17-
1815
/// Initialize serial port before other initializations.
1916
pub fn init_serial_early() {
2017
serial::init();
@@ -68,7 +65,7 @@ pub fn probe_fb_info(width: u32, height: u32, depth: u32) -> FramebufferResult {
6865
))?;
6966
}
7067

71-
let paddr = info.bus_addr & !0xC0000000;
68+
let paddr = bus_to_phys(info.bus_addr);
7269
let vaddr = crate::memory::phys_to_virt(paddr as usize);
7370
if vaddr == 0 {
7471
Err(format!(

kernel/src/arch/aarch64/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::consts::MEMORY_OFFSET;
44
use crate::memory::{init_heap, virt_to_phys, FRAME_ALLOCATOR};
5-
use aarch64::{paging::frame::PhysFrame, asm::ttbr_el1_write};
5+
use aarch64::{asm::ttbr_el1_write, paging::frame::PhysFrame};
66
use log::*;
77
use rcore_memory::PAGE_SIZE;
88

0 commit comments

Comments
 (0)