Skip to content

Commit

Permalink
Translate uart irq number on all arm platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Aug 22, 2023
1 parent 325a09d commit 44ca698
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
- name: Build fs/shell for aarch64-raspi4
run: make PLATFORM=aarch64-raspi4 A=apps/fs/shell FEATURES=driver-bcm2835-sdhci

- name: Build hello for aarch64-bsta1000b
- name: Build helloworld for aarch64-bsta1000b
run: make PLATFORM=aarch64-bsta1000b A=apps/helloworld

build-apps-for-std:
Expand Down
44 changes: 44 additions & 0 deletions crates/arm_gic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,47 @@ pub enum TriggerMode {
/// level is active, and deasserted whenever the level is not active.
Level = 1,
}

/// Different types of interrupt that the GIC handles.
pub enum InterruptType {
/// Software-generated interrupt.
///
/// SGIs are typically used for inter-processor communication and are
/// generated by a write to an SGI register in the GIC.
SGI,
/// Private Peripheral Interrupt.
///
/// Peripheral interrupts that are private to one core.
PPI,
/// Shared Peripheral Interrupt.
///
/// Peripheral interrupts that can delivered to any connected core.
SPI,
}

/// Translate an interrupt of a given type to a GIC INTID.
pub const fn translate_irq(id: usize, int_type: InterruptType) -> Option<usize> {
match int_type {
InterruptType::SGI => {
if id < SGI_RANGE.end {
Some(id)
} else {
None
}
}
InterruptType::PPI => {
if id < PPI_RANGE.end - PPI_RANGE.start {
Some(id + PPI_RANGE.start)
} else {
None
}
}
InterruptType::SPI => {
if id < SPI_RANGE.end - SPI_RANGE.start {
Some(id + SPI_RANGE.start)
} else {
None
}
}
}
}
1 change: 1 addition & 0 deletions modules/axhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#![feature(asm_const)]
#![feature(naked_functions)]
#![feature(const_maybe_uninit_zeroed)]
#![feature(const_option)]
#![feature(doc_auto_cfg)]

#[allow(unused_imports)]
Expand Down
5 changes: 1 addition & 4 deletions modules/axhal/src/platform/aarch64_bsta1000b/dw_apb_uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ pub fn init_early() {
#[cfg(feature = "irq")]
pub fn init_irq() {
UART.lock().set_ier(true);

use crate::platform::aarch64_common::gic::{gic_irq_tran, IntIdType};
// IRQ Type: SPI
crate::irq::register_handler(gic_irq_tran(axconfig::UART_IRQ_NUM, IntIdType::SPI), handle);
crate::irq::register_handler(crate::platform::irq::UART_IRQ_NUM, handle);
}

/// UART IRQ Handler
Expand Down
33 changes: 6 additions & 27 deletions modules/axhal/src/platform/aarch64_common/gic.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use crate::{irq::IrqHandler, mem::phys_to_virt};
use arm_gic::gic_v2::{GicCpuInterface, GicDistributor};
use arm_gic::{translate_irq, InterruptType};
use memory_addr::PhysAddr;
use spinlock::SpinNoIrq;

/// The maximum number of IRQs.
pub const MAX_IRQ_COUNT: usize = 1024;

/// The timer IRQ number.
/// EL1 physical timer, type=PPI, id=14
pub const TIMER_IRQ_NUM: usize = 16 + 14;
pub const TIMER_IRQ_NUM: usize = translate_irq(14, InterruptType::PPI).unwrap();

/// The UART IRQ number.
pub const UART_IRQ_NUM: usize = translate_irq(axconfig::UART_IRQ, InterruptType::SPI).unwrap();

const GICD_BASE: PhysAddr = PhysAddr::from(axconfig::GICD_PADDR);
const GICC_BASE: PhysAddr = PhysAddr::from(axconfig::GICC_PADDR);
Expand All @@ -19,33 +22,9 @@ static GICD: SpinNoIrq<GicDistributor> =
// per-CPU, no lock
static GICC: GicCpuInterface = GicCpuInterface::new(phys_to_virt(GICC_BASE).as_mut_ptr());

#[allow(dead_code)]
pub enum IntIdType {
LPI = 0,
PPI = 16,
SPI = 32,
EPPI = 1056,
ESPI = 4096,
}

/// Translate a GIC irq domain hardware interrupt ID into the real ID
#[allow(dead_code)]
pub fn gic_irq_tran(hwirq: usize, int_id_type: IntIdType) -> usize {
match int_id_type {
IntIdType::PPI => hwirq + IntIdType::PPI as usize,
IntIdType::SPI => hwirq + IntIdType::SPI as usize,
IntIdType::EPPI => hwirq + IntIdType::EPPI as usize,
IntIdType::ESPI => hwirq + IntIdType::ESPI as usize,
_ => {
warn!("Unknown interrupt ID type: {}", int_id_type as usize);
hwirq
}
}
}

/// Enables or disables the given IRQ.
pub fn set_enable(irq_num: usize, enabled: bool) {
trace!("GICD set enable {} {}", irq_num, enabled);
trace!("GICD set enable: {} {}", irq_num, enabled);
GICD.lock().set_enable(irq_num as _, enabled);
}

Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/platform/aarch64_common/pl011.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn init_early() {
/// Set UART IRQ Enable
pub fn init() {
#[cfg(feature = "irq")]
crate::platform::irq::set_enable(axconfig::UART_IRQ_NUM, true);
crate::irq::set_enable(crate::platform::irq::UART_IRQ_NUM, true);
}

/// UART IRQ Handler
Expand Down
2 changes: 1 addition & 1 deletion platforms/aarch64-bsta1000b.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ virtio-mmio-regions = []
# UART Address
uart-paddr = "0x20008000"
# UART irq from device tree
uart-irq-num = "213"
uart-irq = "0xd5"
# GICD Address
gicd-paddr = "0x32001000"
# GICC Address
Expand Down
2 changes: 1 addition & 1 deletion platforms/aarch64-qemu-virt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pci-ranges = [
]
# UART Address
uart-paddr = "0x0900_0000"
uart-irq-num = "33"
uart-irq = "1"

# GICC Address
gicc-paddr = "0x0801_0000"
Expand Down
2 changes: 1 addition & 1 deletion platforms/aarch64-raspi4.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mmio-regions = [
virtio-mmio-regions = []
# UART Address
uart-paddr = "0xFE20_1000"
uart-irq-num = "153"
uart-irq = "0x79"

# GIC Address
gicc-paddr = "0xFF84_2000"
Expand Down

0 comments on commit 44ca698

Please sign in to comment.