Skip to content

Commit

Permalink
Merge pull request #153 from rcore-os/fdt
Browse files Browse the repository at this point in the history
Use `flat_device_tree` crate for examples
  • Loading branch information
qwandor authored Aug 1, 2024
2 parents a9487f2 + 5aa8449 commit 25c9e5c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 32 deletions.
6 changes: 3 additions & 3 deletions examples/aarch64/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ authors = ["Andrew Walbran <[email protected]>"]
edition = "2021"

[dependencies]
buddy_system_allocator = "0.9.0"
fdt = "0.1.5"
log = "0.4.17"
buddy_system_allocator = "0.10.0"
flat_device_tree = "3.1.1"
log = "0.4.22"
smccc = "0.1.1"
spin = "0.9.8"
virtio-drivers = { path = "../.." }
Expand Down
2 changes: 1 addition & 1 deletion examples/aarch64/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ crosvm: $(kernel_crosvm_bin) $(img)
adb shell 'mkdir -p /data/local/tmp/virt_raw'
adb push $(kernel_crosvm_bin) /data/local/tmp/virt_raw/aarch64_example
adb push $(img) /data/local/tmp/virt_raw/disk_img
adb shell "/data/local/tmp/crosvm --log-level=trace --extended-status run --disable-sandbox --serial=stdout,hardware=serial,num=1 --rwdisk=/data/local/tmp/virt_raw/disk_img --bios=/data/local/tmp/virt_raw/aarch64_example"
adb shell "/apex/com.android.virt/bin/crosvm --log-level=info --extended-status run --disable-sandbox --serial=stdout,hardware=serial,num=1 --rwdisk=/data/local/tmp/virt_raw/disk_img --bios=/data/local/tmp/virt_raw/aarch64_example"

$(img):
dd if=/dev/zero of=$@ bs=512 count=32
Expand Down
42 changes: 17 additions & 25 deletions examples/aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::{
panic::PanicInfo,
ptr::{self, NonNull},
};
use fdt::{node::FdtNode, standard_nodes::Compatible, Fdt};
use flat_device_tree::{node::FdtNode, standard_nodes::Compatible, Fdt};
use hal::HalImpl;
use log::{debug, error, info, trace, warn, LevelFilter};
use smccc::{psci::system_off, Hvc};
Expand Down Expand Up @@ -86,20 +86,16 @@ extern "C" fn main(x0: u64, x1: u64, x2: u64, x3: u64) {
node.name,
node.compatible().map(Compatible::first),
);
if let Some(reg) = node.reg() {
for range in reg {
trace!(
" {:#018x?}, length {:?}",
range.starting_address,
range.size
);
}
for range in node.reg() {
trace!(
" {:#018x?}, length {:?}",
range.starting_address,
range.size
);
}

// Check whether it is a VirtIO MMIO device.
if let (Some(compatible), Some(region)) =
(node.compatible(), node.reg().and_then(|mut reg| reg.next()))
{
if let (Some(compatible), Some(region)) = (node.compatible(), node.reg().next()) {
if compatible.all().any(|s| s == "virtio,mmio")
&& region.size.unwrap_or(0) > size_of::<VirtIOHeader>()
{
Expand Down Expand Up @@ -271,8 +267,8 @@ enum PciRangeType {
Memory64,
}

impl From<u8> for PciRangeType {
fn from(value: u8) -> Self {
impl From<u32> for PciRangeType {
fn from(value: u32) -> Self {
match value {
0 => Self::ConfigurationSpace,
1 => Self::IoSpace,
Expand All @@ -284,7 +280,7 @@ impl From<u8> for PciRangeType {
}

fn enumerate_pci(pci_node: FdtNode, cam: Cam) {
let reg = pci_node.reg().expect("PCI node missing reg property.");
let reg = pci_node.reg();
let mut allocator = PciMemory32Allocator::for_pci_ranges(&pci_node);

for region in reg {
Expand Down Expand Up @@ -328,18 +324,14 @@ struct PciMemory32Allocator {
impl PciMemory32Allocator {
/// Creates a new allocator based on the ranges property of the given PCI node.
pub fn for_pci_ranges(pci_node: &FdtNode) -> Self {
let ranges = pci_node
.property("ranges")
.expect("PCI node missing ranges property.");
let mut memory_32_address = 0;
let mut memory_32_size = 0;
for i in 0..ranges.value.len() / 28 {
let range = &ranges.value[i * 28..(i + 1) * 28];
let prefetchable = range[0] & 0x80 != 0;
let range_type = PciRangeType::from(range[0] & 0x3);
let bus_address = u64::from_be_bytes(range[4..12].try_into().unwrap());
let cpu_physical = u64::from_be_bytes(range[12..20].try_into().unwrap());
let size = u64::from_be_bytes(range[20..28].try_into().unwrap());
for range in pci_node.ranges() {
let prefetchable = range.child_bus_address_hi & 0x4000_0000 != 0;
let range_type = PciRangeType::from((range.child_bus_address_hi & 0x0300_0000) >> 24);
let bus_address = range.child_bus_address as u64;
let cpu_physical = range.parent_bus_address as u64;
let size = range.size as u64;
info!(
"range: {:?} {}prefetchable bus address: {:#018x} host physical address: {:#018x} size: {:#018x}",
range_type,
Expand Down
2 changes: 1 addition & 1 deletion examples/riscv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default = ["tcp"]
log = "0.4"
riscv = "0.10"
opensbi-rt = { git = "https://github.com/rcore-os/opensbi-rt.git", rev = "abdfeb72" }
fdt = "0.1.4"
flat_device_tree = "3.1.1"
virtio-drivers = { path = "../.." }
lazy_static = { version = "1.4", features = ["spin_no_std"] }

Expand Down
4 changes: 2 additions & 2 deletions examples/riscv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate opensbi_rt;

use alloc::vec;
use core::ptr::NonNull;
use fdt::{node::FdtNode, standard_nodes::Compatible, Fdt};
use flat_device_tree::{node::FdtNode, standard_nodes::Compatible, Fdt};
use log::LevelFilter;
use virtio_drivers::{
device::{
Expand Down Expand Up @@ -58,7 +58,7 @@ fn walk_dt(fdt: Fdt) {
}

fn virtio_probe(node: FdtNode) {
if let Some(reg) = node.reg().and_then(|mut reg| reg.next()) {
if let Some(reg) = node.reg().next() {
let paddr = reg.starting_address as usize;
let size = reg.size.unwrap();
let vaddr = paddr;
Expand Down

0 comments on commit 25c9e5c

Please sign in to comment.