-
Notifications
You must be signed in to change notification settings - Fork 12
Generic handler for GICv3 #53
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
jonathanpallant
merged 2 commits into
rust-embedded:main
from
thejpster:arm-gic-handler
Sep 11, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
examples/mps3-an536/reference/abt-exception-armv8r-none-eabihf.out
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
examples/mps3-an536/reference/gic-map-armv8r-none-eabihf.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Found PERIPHBASE 0xf0000000 | ||
Creating GIC driver @ 0xf0000000 / 0xf0100000 | ||
Calling git.setup(0) | ||
Configure low-prio SGI... | ||
Configure high-prio SGI... | ||
gic.enable_interrupt() | ||
Enabling interrupts... | ||
CPSR: CPSR { N=0 Z=1 C=1 V=0 Q=0 J=0 E=0 A=0 I=1 F=1 T=0 MODE=Ok(Sys) } | ||
CPSR: CPSR { N=0 Z=1 C=1 V=0 Q=0 J=0 E=0 A=0 I=0 F=1 T=0 MODE=Ok(Sys) } | ||
Send lo-prio SGI | ||
> IRQ | ||
- handle_interrupt_with_id(SGI 3) | ||
- got SGI 3, sending hi-prio SGI 4 | ||
> IRQ | ||
- handle_interrupt_with_id(SGI 4) | ||
- got hi-prio SGI 4! | ||
< IRQ | ||
- finished sending hi-prio! | ||
< IRQ | ||
IRQ test completed OK |
20 changes: 20 additions & 0 deletions
20
examples/mps3-an536/reference/gic-static-section-irq-armv8r-none-eabihf.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Found PERIPHBASE 0xf0000000 | ||
Creating GIC driver @ 0xf0000000 / 0xf0100000 | ||
Calling git.setup(0) | ||
Configure low-prio SGI... | ||
Configure high-prio SGI... | ||
gic.enable_interrupt() | ||
Enabling interrupts... | ||
CPSR: CPSR { N=0 Z=1 C=1 V=0 Q=0 J=0 E=0 A=0 I=1 F=1 T=0 MODE=Ok(Sys) } | ||
CPSR: CPSR { N=0 Z=1 C=1 V=0 Q=0 J=0 E=0 A=0 I=0 F=1 T=0 MODE=Ok(Sys) } | ||
Send lo-prio SGI | ||
> IRQ | ||
- handle_interrupt_with_id(SGI 3) | ||
- got SGI 3, sending hi-prio SGI 4 | ||
> IRQ | ||
- handle_interrupt_with_id(SGI 4) | ||
- got hi-prio SGI 4! | ||
< IRQ | ||
- finished sending hi-prio! | ||
< IRQ | ||
IRQ test completed OK |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
//! # GIC example for Arm Cortex-R52 on an MPS2-AN336 | ||
//! | ||
//! Uses a run-time map of interrupt handlers. | ||
|
||
#![no_std] | ||
#![no_main] | ||
|
||
// pull in our start-up code | ||
use cortex_r_rt::{entry, irq}; | ||
|
||
// pull in our library | ||
use mps3_an536::InterruptHandler; | ||
|
||
use arm_gic::{ | ||
gicv3::{GicV3, Group, InterruptGroup, SgiTarget, SgiTargetGroup}, | ||
IntId, | ||
}; | ||
use core::cell::RefCell; | ||
use heapless::linear_map::LinearMap; | ||
use semihosting::println; | ||
|
||
/// Offset from PERIPHBASE for GIC Distributor | ||
const GICD_BASE_OFFSET: usize = 0x0000_0000usize; | ||
|
||
/// Offset from PERIPHBASE for the first GIC Redistributor | ||
const GICR_BASE_OFFSET: usize = 0x0010_0000usize; | ||
|
||
const SGI_INTID_LO: IntId = IntId::sgi(3); | ||
const SGI_INTID_HI: IntId = IntId::sgi(4); | ||
|
||
static INTERRUPT_HANDLERS: critical_section::Mutex<RefCell<LinearMap<IntId, InterruptHandler, 8>>> = | ||
critical_section::Mutex::new(RefCell::new(LinearMap::new())); | ||
|
||
/// The entry-point to the Rust application. | ||
/// | ||
/// It is called by the start-up code in `cortex-r-rt`. | ||
#[entry] | ||
fn main() -> ! { | ||
// Get the GIC address by reading CBAR | ||
let periphbase = cortex_ar::register::ImpCbar::read().periphbase(); | ||
println!("Found PERIPHBASE {:010p}", periphbase); | ||
let gicd_base = periphbase.wrapping_byte_add(GICD_BASE_OFFSET); | ||
let gicr_base = periphbase.wrapping_byte_add(GICR_BASE_OFFSET); | ||
|
||
// Initialise the GIC. | ||
println!( | ||
"Creating GIC driver @ {:010p} / {:010p}", | ||
gicd_base, gicr_base | ||
); | ||
let mut gic: GicV3 = unsafe { GicV3::new(gicd_base.cast(), gicr_base.cast(), 1, false) }; | ||
println!("Calling git.setup(0)"); | ||
gic.setup(0); | ||
GicV3::set_priority_mask(0x80); | ||
|
||
// Configure a Software Generated Interrupt for Core 0 | ||
println!("Configure low-prio SGI..."); | ||
gic.set_interrupt_priority(SGI_INTID_LO, Some(0), 0x31); | ||
gic.set_group(SGI_INTID_LO, Some(0), Group::Group1NS); | ||
|
||
println!("Configure high-prio SGI..."); | ||
gic.set_interrupt_priority(SGI_INTID_HI, Some(0), 0x10); | ||
gic.set_group(SGI_INTID_HI, Some(0), Group::Group1NS); | ||
|
||
println!("gic.enable_interrupt()"); | ||
gic.enable_interrupt(SGI_INTID_LO, Some(0), true); | ||
gic.enable_interrupt(SGI_INTID_HI, Some(0), true); | ||
|
||
critical_section::with(|cs| { | ||
let mut handlers = INTERRUPT_HANDLERS.borrow_ref_mut(cs); | ||
handlers | ||
.insert( | ||
SGI_INTID_LO, | ||
InterruptHandler::new(SGI_INTID_LO, handle_sgi_lo), | ||
) | ||
.unwrap(); | ||
handlers | ||
.insert( | ||
SGI_INTID_HI, | ||
InterruptHandler::new(SGI_INTID_HI, handle_sgi_hi), | ||
) | ||
.unwrap(); | ||
}); | ||
|
||
println!("Enabling interrupts..."); | ||
dump_cpsr(); | ||
unsafe { | ||
cortex_ar::interrupt::enable(); | ||
} | ||
dump_cpsr(); | ||
|
||
// Send it | ||
println!("Send lo-prio SGI"); | ||
GicV3::send_sgi( | ||
SGI_INTID_LO, | ||
SgiTarget::List { | ||
affinity3: 0, | ||
affinity2: 0, | ||
affinity1: 0, | ||
target_list: 0b1, | ||
}, | ||
SgiTargetGroup::CurrentGroup1, | ||
); | ||
|
||
for _ in 0..1_000_000 { | ||
cortex_ar::asm::nop(); | ||
} | ||
|
||
println!("IRQ test completed OK"); | ||
|
||
semihosting::process::exit(0); | ||
} | ||
|
||
fn dump_cpsr() { | ||
let cpsr = cortex_ar::register::Cpsr::read(); | ||
println!("CPSR: {:?}", cpsr); | ||
} | ||
|
||
/// Handles the low-prio SGI | ||
fn handle_sgi_lo(int_id: IntId) { | ||
println!("- got {:?}, sending hi-prio {:?}", int_id, SGI_INTID_HI); | ||
GicV3::send_sgi( | ||
SGI_INTID_HI, | ||
SgiTarget::List { | ||
affinity3: 0, | ||
affinity2: 0, | ||
affinity1: 0, | ||
target_list: 0b1, | ||
}, | ||
SgiTargetGroup::CurrentGroup1, | ||
); | ||
println!("- finished sending hi-prio!"); | ||
} | ||
|
||
/// Handles the high-prio SGI | ||
fn handle_sgi_hi(int_id: IntId) { | ||
println!("- got hi-prio {:?}!", int_id); | ||
} | ||
|
||
/// Called when the Arm CPU gets an IRQ | ||
/// | ||
/// Talks to the GICv3 to find out which interrupts are pending and calls | ||
/// [`handle_interrupt_with_id`] for each of them, with interrupts re-enabled. | ||
#[cfg(feature = "gic")] | ||
#[irq] | ||
fn irq_handler() { | ||
println!("> IRQ"); | ||
while let Some(next_int_id) = GicV3::get_and_acknowledge_interrupt(InterruptGroup::Group1) { | ||
// handle the interrupt | ||
println!("- handle_interrupt_with_id({:?})", next_int_id); | ||
let handler = critical_section::with(|cs| { | ||
let handlers_map = INTERRUPT_HANDLERS.borrow_ref(cs); | ||
handlers_map.get(&next_int_id).cloned() | ||
}); | ||
if let Some(irq_entry) = handler { | ||
// let's go re-entrant | ||
unsafe { | ||
cortex_ar::interrupt::enable(); | ||
} | ||
irq_entry.execute(); | ||
// turn interrupts off again | ||
cortex_ar::interrupt::disable(); | ||
} | ||
GicV3::end_interrupt(next_int_id, InterruptGroup::Group1); | ||
} | ||
println!("< IRQ"); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.