Skip to content

aarch64: Implement RTC Pl031 #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 60 additions & 17 deletions arch/src/aarch64/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use memory_model::{GuestAddress, GuestMemory, GuestMemoryError};

// This is a value for uniquely identifying the FDT node declaring the interrupt controller.
const GIC_PHANDLE: u32 = 1;
// This is a value for uniquely identifying the FDT node containing the clock definition.
const CLOCK_PHANDLE: u32 = 2;
// Read the documentation specified when appending the root node to the FDT.
const ADDRESS_CELLS: u32 = 0x2;
const SIZE_CELLS: u32 = 0x2;
Expand Down Expand Up @@ -73,14 +75,6 @@ pub enum Error {

pub type Result<T> = result::Result<T, Error>;

/// Types of devices that get added to the FDT.
#[derive(Clone, Debug)]
pub enum FDTDeviceType {
Virtio,
#[cfg(target_arch = "aarch64")]
Serial,
}

// Creates the flattened device tree for this VM.
pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug>(
guest_mem: &GuestMemory,
Expand Down Expand Up @@ -112,6 +106,7 @@ pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug>(
create_chosen_node(&mut fdt, cmdline)?;
create_gic_node(&mut fdt, u64::from(num_cpus))?;
create_timer_node(&mut fdt)?;
create_clock_node(&mut fdt)?;
create_psci_node(&mut fdt)?;
device_info.map_or(Ok(()), |v| create_devices_node(&mut fdt, v))?;

Expand Down Expand Up @@ -220,7 +215,6 @@ fn append_property_string(fdt: &mut Vec<u8>, name: &str, value: &str) -> Result<
fn append_property_cstring(fdt: &mut Vec<u8>, name: &str, cstr_value: &CStr) -> Result<()> {
let value_bytes = cstr_value.to_bytes_with_nul();
let cstr_name = CString::new(name).map_err(CstringFDTTransform)?;

// Safe because we allocated fdt, converted name and value to CStrings
let fdt_ret = unsafe {
fdt_property(
Expand Down Expand Up @@ -382,6 +376,22 @@ fn create_gic_node(fdt: &mut Vec<u8>, vcpu_count: u64) -> Result<()> {
Ok(())
}

fn create_clock_node(fdt: &mut Vec<u8>) -> Result<()> {
// The Advanced Peripheral Bus (APB) is part of the Advanced Microcontroller Bus Architecture
// (AMBA) protocol family. It defines a low-cost interface that is optimized for minimal power
// consumption and reduced interface complexity.
// PCLK is the clock source and this node defines exactly the clock for the APB.
append_begin_node(fdt, "apb-pclk")?;
append_property_string(fdt, "compatible", "fixed-clock")?;
append_property_u32(fdt, "#clock-cells", 0x0)?;
append_property_u32(fdt, "clock-frequency", 24000000)?;
append_property_string(fdt, "clock-output-names", "clk24mhz")?;
append_property_u32(fdt, "phandle", CLOCK_PHANDLE)?;
append_end_node(fdt)?;

Ok(())
}

fn create_timer_node(fdt: &mut Vec<u8>) -> Result<()> {
// See
// https://github.com/torvalds/linux/blob/master/Documentation/devicetree/bindings/interrupt-controller/arch_timer.txt
Expand Down Expand Up @@ -453,14 +463,33 @@ fn create_serial_node<T: DeviceInfoForFDT + Clone + Debug>(
Ok(())
}

fn create_rtc_node<T: DeviceInfoForFDT + Clone + Debug>(
fdt: &mut Vec<u8>,
dev_info: T,
) -> Result<()> {
let compatible = b"arm,pl031\0arm,primecell\0";
let rtc_reg_prop = generate_prop64(&[dev_info.addr(), dev_info.length()]);
let irq = generate_prop32(&[GIC_FDT_IRQ_TYPE_SPI, dev_info.irq(), IRQ_TYPE_LEVEL_HI]);
append_begin_node(fdt, &format!("rtc@{:x}", dev_info.addr()))?;
append_property(fdt, "compatible", compatible)?;
append_property(fdt, "reg", &rtc_reg_prop)?;
append_property(fdt, "interrupts", &irq)?;
append_property_u32(fdt, "clocks", CLOCK_PHANDLE)?;
append_property_string(fdt, "clock-names", "apb_pclk")?;
append_end_node(fdt)?;

Ok(())
}

fn create_devices_node<T: DeviceInfoForFDT + Clone + Debug>(
fdt: &mut Vec<u8>,
dev_info: &HashMap<String, T>,
) -> Result<()> {
for (_, info) in &*dev_info {
match info.type_() {
DeviceType::Virtio => create_virtio_node(fdt, info.clone())?,
DeviceType::RTC => create_rtc_node(fdt, info.clone())?,
DeviceType::Serial => create_serial_node(fdt, info.clone())?,
DeviceType::Virtio => create_virtio_node(fdt, info.clone())?,
};
}

Expand All @@ -478,7 +507,7 @@ mod tests {
pub struct MMIODeviceInfo {
addr: u64,
irq: u32,
type_: u32,
type_: DeviceType,
}

impl DeviceInfoForFDT for MMIODeviceInfo {
Expand All @@ -491,8 +520,8 @@ mod tests {
fn length(&self) -> u64 {
LEN
}
fn type_(&self) -> u32 {
self.type_
fn type_(&self) -> &DeviceType {
&self.type_
}
}
// The `load` function from the `device_tree` will mistakenly check the actual size
Expand All @@ -514,23 +543,36 @@ mod tests {
MMIODeviceInfo {
addr: 0x00,
irq: 1,
type_: 0,
type_: DeviceType::Serial,
},
),
(
"virtio".to_string(),
MMIODeviceInfo {
addr: 0x00 + LEN,
irq: 2,
type_: 1,
type_: DeviceType::Virtio,
},
),
(
"rtc".to_string(),
MMIODeviceInfo {
addr: 0x00 + 2 * LEN,
irq: 3,
type_: DeviceType::RTC,
},
),
]
.iter()
.cloned()
.collect();
let mut dtb =
create_fdt(&mem, 1, &CString::new("console=tty0").unwrap(), &dev_info).unwrap();
let mut dtb = create_fdt(
&mem,
1,
&CString::new("console=tty0").unwrap(),
Some(&dev_info),
)
.unwrap();

/* Use this code when wanting to generate a new DTB sample.
{
Expand All @@ -546,6 +588,7 @@ mod tests {
output.write_all(&dtb).unwrap();
}
*/

let bytes = include_bytes!("output.dtb");
let pos = 4;
let val = layout::FDT_MAX_SIZE;
Expand Down
2 changes: 1 addition & 1 deletion arch/src/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl From<Error> for super::Error {
}
}

pub use self::fdt::{DeviceInfoForFDT, FDTDeviceType};
pub use self::fdt::DeviceInfoForFDT;

/// Returns a Vec of the valid memory addresses for aarch64.
/// See [`layout`](layout) module for a drawing of the specific memory model for this platform.
Expand Down
2 changes: 2 additions & 0 deletions arch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ pub enum DeviceType {
Virtio,
#[cfg(target_arch = "aarch64")]
Serial,
#[cfg(target_arch = "aarch64")]
RTC,
}
1 change: 1 addition & 0 deletions devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["The Chromium OS Authors"]
byteorder = ">=1.2.1"
epoll = "=4.0.1"
libc = ">=0.2.39"
time = ">=0.1.39"

dumbo = { path = "../dumbo" }
logger = { path = "../logger" }
Expand Down
4 changes: 4 additions & 0 deletions devices/src/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
// found in the THIRD-PARTY file.

mod i8042;
#[cfg(target_arch = "aarch64")]
mod rtc_pl031;
mod serial;

pub use self::i8042::Error as I8042DeviceError;
pub use self::i8042::I8042Device;
#[cfg(target_arch = "aarch64")]
pub use self::rtc_pl031::RTC;
pub use self::serial::Serial;
Loading