1#![no_std]
9#![no_main]
10
11use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
12
13use kernel::capabilities;
14use kernel::component::Component;
15use kernel::hil::time::{Alarm, Timer};
16use kernel::platform::chip::InterruptService;
17use kernel::platform::scheduler_timer::VirtualSchedulerTimer;
18use kernel::platform::{KernelResources, SyscallDriverLookup};
19use kernel::process::ProcessArray;
20use kernel::scheduler::mlfq::MLFQSched;
21use kernel::utilities::registers::interfaces::ReadWriteable;
22use kernel::utilities::StaticRef;
23use kernel::{create_capability, debug, static_init};
24use rv32i::csr;
25
26mod io;
27mod litex_generated_constants;
28
29use litex_generated_constants as socc;
36
37struct LiteXArtyInterruptablePeripherals {
45 uart0: &'static litex_vexriscv::uart::LiteXUart<'static, socc::SoCRegisterFmt>,
46 timer0: &'static litex_vexriscv::timer::LiteXTimer<
47 'static,
48 socc::SoCRegisterFmt,
49 socc::ClockFrequency,
50 >,
51 ethmac0: &'static litex_vexriscv::liteeth::LiteEth<
52 'static,
53 { socc::ETHMAC_TX_SLOTS },
54 socc::SoCRegisterFmt,
55 >,
56}
57
58impl LiteXArtyInterruptablePeripherals {
59 pub fn init(&'static self) {
61 kernel::deferred_call::DeferredCallClient::register(self.uart0);
62 }
63}
64
65impl InterruptService for LiteXArtyInterruptablePeripherals {
66 unsafe fn service_interrupt(&self, interrupt: u32) -> bool {
67 match interrupt as usize {
68 socc::UART_INTERRUPT => {
69 self.uart0.service_interrupt();
70 true
71 }
72 socc::TIMER0_INTERRUPT => {
73 self.timer0.service_interrupt();
74 true
75 }
76 socc::ETHMAC_INTERRUPT => {
77 self.ethmac0.service_interrupt();
78 true
79 }
80 _ => false,
81 }
82 }
83}
84
85const NUM_PROCS: usize = 4;
86
87static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
89
90struct LiteXArtyPanicReferences {
93 chip: Option<&'static litex_vexriscv::chip::LiteXVexRiscv<LiteXArtyInterruptablePeripherals>>,
94 uart: Option<&'static litex_vexriscv::uart::LiteXUart<'static, socc::SoCRegisterFmt>>,
95 led_controller:
96 Option<&'static litex_vexriscv::led_controller::LiteXLedController<socc::SoCRegisterFmt>>,
97 process_printer: Option<&'static capsules_system::process_printer::ProcessPrinterText>,
98}
99static mut PANIC_REFERENCES: LiteXArtyPanicReferences = LiteXArtyPanicReferences {
100 chip: None,
101 uart: None,
102 led_controller: None,
103 process_printer: None,
104};
105
106const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
108 capsules_system::process_policies::PanicFaultPolicy {};
109
110kernel::stack_size! {0x2000}
111
112struct LiteXArty {
115 led_driver: &'static capsules_core::led::LedDriver<
116 'static,
117 litex_vexriscv::led_controller::LiteXLed<'static, socc::SoCRegisterFmt>,
118 4,
119 >,
120 console: &'static capsules_core::console::Console<'static>,
121 pconsole: &'static capsules_core::process_console::ProcessConsole<
122 'static,
123 { capsules_core::process_console::DEFAULT_COMMAND_HISTORY_LEN },
124 VirtualMuxAlarm<
125 'static,
126 litex_vexriscv::timer::LiteXAlarm<
127 'static,
128 'static,
129 socc::SoCRegisterFmt,
130 socc::ClockFrequency,
131 >,
132 >,
133 components::process_console::Capability,
134 >,
135 lldb: &'static capsules_core::low_level_debug::LowLevelDebug<
136 'static,
137 capsules_core::virtualizers::virtual_uart::UartDevice<'static>,
138 >,
139 alarm: &'static capsules_core::alarm::AlarmDriver<
140 'static,
141 VirtualMuxAlarm<
142 'static,
143 litex_vexriscv::timer::LiteXAlarm<
144 'static,
145 'static,
146 socc::SoCRegisterFmt,
147 socc::ClockFrequency,
148 >,
149 >,
150 >,
151 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
152 scheduler: &'static MLFQSched<
153 'static,
154 VirtualMuxAlarm<
155 'static,
156 litex_vexriscv::timer::LiteXAlarm<
157 'static,
158 'static,
159 socc::SoCRegisterFmt,
160 socc::ClockFrequency,
161 >,
162 >,
163 >,
164 scheduler_timer: &'static VirtualSchedulerTimer<
165 VirtualMuxAlarm<
166 'static,
167 litex_vexriscv::timer::LiteXAlarm<
168 'static,
169 'static,
170 socc::SoCRegisterFmt,
171 socc::ClockFrequency,
172 >,
173 >,
174 >,
175}
176
177impl SyscallDriverLookup for LiteXArty {
179 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
180 where
181 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
182 {
183 match driver_num {
184 capsules_core::led::DRIVER_NUM => f(Some(self.led_driver)),
185 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
186 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
187 capsules_core::low_level_debug::DRIVER_NUM => f(Some(self.lldb)),
188 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
189 _ => f(None),
190 }
191 }
192}
193
194impl KernelResources<litex_vexriscv::chip::LiteXVexRiscv<LiteXArtyInterruptablePeripherals>>
195 for LiteXArty
196{
197 type SyscallDriverLookup = Self;
198 type SyscallFilter = ();
199 type ProcessFault = ();
200 type Scheduler = MLFQSched<
201 'static,
202 VirtualMuxAlarm<
203 'static,
204 litex_vexriscv::timer::LiteXAlarm<
205 'static,
206 'static,
207 socc::SoCRegisterFmt,
208 socc::ClockFrequency,
209 >,
210 >,
211 >;
212 type SchedulerTimer = VirtualSchedulerTimer<
213 VirtualMuxAlarm<
214 'static,
215 litex_vexriscv::timer::LiteXAlarm<
216 'static,
217 'static,
218 socc::SoCRegisterFmt,
219 socc::ClockFrequency,
220 >,
221 >,
222 >;
223 type WatchDog = ();
224 type ContextSwitchCallback = ();
225
226 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
227 self
228 }
229 fn syscall_filter(&self) -> &Self::SyscallFilter {
230 &()
231 }
232 fn process_fault(&self) -> &Self::ProcessFault {
233 &()
234 }
235 fn scheduler(&self) -> &Self::Scheduler {
236 self.scheduler
237 }
238 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
239 self.scheduler_timer
240 }
241 fn watchdog(&self) -> &Self::WatchDog {
242 &()
243 }
244 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
245 &()
246 }
247}
248
249#[inline(never)]
253unsafe fn start() -> (
254 &'static kernel::Kernel,
255 LiteXArty,
256 &'static litex_vexriscv::chip::LiteXVexRiscv<LiteXArtyInterruptablePeripherals>,
257) {
258 extern "C" {
260 static _sapps: u8;
262 static _eapps: u8;
264 static mut _sappmem: u8;
266 static _eappmem: u8;
268 static _stext: u8;
270 static _etext: u8;
272 static _sflash: u8;
274 static _eflash: u8;
276 static _ssram: u8;
278 static _esram: u8;
280 }
281
282 rv32i::configure_trap_handler();
286
287 let pmp = rv32i::pmp::kernel_protection::KernelProtectionPMP::new(
291 rv32i::pmp::kernel_protection::FlashRegion(
292 rv32i::pmp::NAPOTRegionSpec::from_start_end(
293 core::ptr::addr_of!(_sflash),
294 core::ptr::addr_of!(_eflash),
295 )
296 .unwrap(),
297 ),
298 rv32i::pmp::kernel_protection::RAMRegion(
299 rv32i::pmp::NAPOTRegionSpec::from_start_end(
300 core::ptr::addr_of!(_ssram),
301 core::ptr::addr_of!(_esram),
302 )
303 .unwrap(),
304 ),
305 rv32i::pmp::kernel_protection::MMIORegion(
306 rv32i::pmp::NAPOTRegionSpec::from_start_size(
307 0xf0000000 as *const u8, 0x10000000, )
310 .unwrap(),
311 ),
312 rv32i::pmp::kernel_protection::KernelTextRegion(
313 rv32i::pmp::TORRegionSpec::from_start_end(
314 core::ptr::addr_of!(_stext),
315 core::ptr::addr_of!(_etext),
316 )
317 .unwrap(),
318 ),
319 )
320 .unwrap();
321
322 let process_mgmt_cap = create_capability!(capabilities::ProcessManagementCapability);
324 let memory_allocation_cap = create_capability!(capabilities::MemoryAllocationCapability);
325
326 let processes = components::process_array::ProcessArrayComponent::new()
328 .finalize(components::process_array_component_static!(NUM_PROCS));
329 PROCESSES = Some(processes);
330
331 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
333
334 let led0 = static_init!(
339 litex_vexriscv::led_controller::LiteXLedController<socc::SoCRegisterFmt>,
340 litex_vexriscv::led_controller::LiteXLedController::new(
341 StaticRef::new(
342 socc::CSR_LEDS_BASE
343 as *const litex_vexriscv::led_controller::LiteXLedRegisters<
344 socc::SoCRegisterFmt,
345 >
346 ),
347 4, )
349 );
350 led0.initialize();
351
352 PANIC_REFERENCES.led_controller = Some(led0);
353
354 let timer0 = static_init!(
358 litex_vexriscv::timer::LiteXTimer<'static, socc::SoCRegisterFmt, socc::ClockFrequency>,
359 litex_vexriscv::timer::LiteXTimer::new(StaticRef::new(
360 socc::CSR_TIMER0_BASE
361 as *const litex_vexriscv::timer::LiteXTimerRegisters<socc::SoCRegisterFmt>
362 ),)
363 );
364
365 let timer0_uptime = static_init!(
367 litex_vexriscv::timer::LiteXTimerUptime<
368 'static,
369 socc::SoCRegisterFmt,
370 socc::ClockFrequency,
371 >,
372 litex_vexriscv::timer::LiteXTimerUptime::new(timer0)
373 );
374
375 let litex_alarm = static_init!(
378 litex_vexriscv::timer::LiteXAlarm<
379 'static,
380 'static,
381 socc::SoCRegisterFmt,
382 socc::ClockFrequency,
383 >,
384 litex_vexriscv::timer::LiteXAlarm::new(timer0_uptime, timer0)
385 );
386 timer0.set_timer_client(litex_alarm);
387 litex_alarm.initialize();
388
389 let mux_alarm = static_init!(
392 MuxAlarm<
393 'static,
394 litex_vexriscv::timer::LiteXAlarm<
395 'static,
396 'static,
397 socc::SoCRegisterFmt,
398 socc::ClockFrequency,
399 >,
400 >,
401 MuxAlarm::new(litex_alarm)
402 );
403 litex_alarm.set_alarm_client(mux_alarm);
404
405 let virtual_alarm_user = static_init!(
407 VirtualMuxAlarm<
408 'static,
409 litex_vexriscv::timer::LiteXAlarm<
410 'static,
411 'static,
412 socc::SoCRegisterFmt,
413 socc::ClockFrequency,
414 >,
415 >,
416 VirtualMuxAlarm::new(mux_alarm)
417 );
418 virtual_alarm_user.setup();
419
420 let alarm = static_init!(
421 capsules_core::alarm::AlarmDriver<
422 'static,
423 VirtualMuxAlarm<
424 'static,
425 litex_vexriscv::timer::LiteXAlarm<
426 'static,
427 'static,
428 socc::SoCRegisterFmt,
429 socc::ClockFrequency,
430 >,
431 >,
432 >,
433 capsules_core::alarm::AlarmDriver::new(
434 virtual_alarm_user,
435 board_kernel.create_grant(capsules_core::alarm::DRIVER_NUM, &memory_allocation_cap)
436 )
437 );
438 virtual_alarm_user.set_alarm_client(alarm);
439
440 let systick_virtual_alarm = static_init!(
442 VirtualMuxAlarm<
443 'static,
444 litex_vexriscv::timer::LiteXAlarm<
445 'static,
446 'static,
447 socc::SoCRegisterFmt,
448 socc::ClockFrequency,
449 >,
450 >,
451 VirtualMuxAlarm::new(mux_alarm)
452 );
453 systick_virtual_alarm.setup();
454
455 let scheduler_timer = static_init!(
456 VirtualSchedulerTimer<
457 VirtualMuxAlarm<
458 'static,
459 litex_vexriscv::timer::LiteXAlarm<
460 'static,
461 'static,
462 socc::SoCRegisterFmt,
463 socc::ClockFrequency,
464 >,
465 >,
466 >,
467 VirtualSchedulerTimer::new(systick_virtual_alarm)
468 );
469
470 let uart0 = static_init!(
474 litex_vexriscv::uart::LiteXUart<socc::SoCRegisterFmt>,
475 litex_vexriscv::uart::LiteXUart::new(
476 StaticRef::new(
477 socc::CSR_UART_BASE
478 as *const litex_vexriscv::uart::LiteXUartRegisters<socc::SoCRegisterFmt>,
479 ),
480 None,
484 )
485 );
486 uart0.initialize();
487
488 PANIC_REFERENCES.uart = Some(uart0);
489
490 let uart_mux = components::console::UartMuxComponent::new(uart0, socc::UART_BAUDRATE)
492 .finalize(components::uart_mux_component_static!());
493
494 let ethmac0 = static_init!(
498 litex_vexriscv::liteeth::LiteEth<{socc::ETHMAC_TX_SLOTS}, socc::SoCRegisterFmt>,
499 litex_vexriscv::liteeth::LiteEth::new(
500 StaticRef::new(
501 socc::CSR_ETHMAC_BASE
502 as *const litex_vexriscv::liteeth::LiteEthMacRegisters<socc::SoCRegisterFmt>,
503 ),
504 socc::MEM_ETHMAC_BASE,
505 socc::MEM_ETHMAC_SIZE,
506 socc::ETHMAC_SLOT_SIZE,
507 socc::ETHMAC_RX_SLOTS,
508 socc::ETHMAC_TX_SLOTS,
509 )
510 );
511
512 ethmac0.initialize();
514
515 let led_driver =
519 components::led::LedsComponent::new().finalize(components::led_component_static!(
520 litex_vexriscv::led_controller::LiteXLed<'static, socc::SoCRegisterFmt>,
521 led0.get_led(0).unwrap(),
522 led0.get_led(1).unwrap(),
523 led0.get_led(2).unwrap(),
524 led0.get_led(3).unwrap(),
525 ));
526
527 let interrupt_service = static_init!(
530 LiteXArtyInterruptablePeripherals,
531 LiteXArtyInterruptablePeripherals {
532 uart0,
533 timer0,
534 ethmac0,
535 }
536 );
537 interrupt_service.init();
538
539 let chip = static_init!(
540 litex_vexriscv::chip::LiteXVexRiscv<
541 LiteXArtyInterruptablePeripherals,
542 >,
543 litex_vexriscv::chip::LiteXVexRiscv::new(
544 "LiteX on Arty A7",
545 interrupt_service,
546 pmp,
547 )
548 );
549
550 PANIC_REFERENCES.chip = Some(chip);
551
552 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
553 .finalize(components::process_printer_text_component_static!());
554
555 PANIC_REFERENCES.process_printer = Some(process_printer);
556
557 csr::CSR
559 .mie
560 .modify(csr::mie::mie::mext::SET + csr::mie::mie::msoft::SET);
561 csr::CSR.mstatus.modify(csr::mstatus::mstatus::mie::SET);
562
563 chip.unmask_interrupts();
565
566 let pconsole = components::process_console::ProcessConsoleComponent::new(
568 board_kernel,
569 uart_mux,
570 mux_alarm,
571 process_printer,
572 None,
573 )
574 .finalize(components::process_console_component_static!(
575 litex_vexriscv::timer::LiteXAlarm<
576 'static,
577 'static,
578 socc::SoCRegisterFmt,
579 socc::ClockFrequency,
580 >
581 ));
582
583 let console = components::console::ConsoleComponent::new(
585 board_kernel,
586 capsules_core::console::DRIVER_NUM,
587 uart_mux,
588 )
589 .finalize(components::console_component_static!());
590
591 components::debug_writer::DebugWriterComponent::new(
593 uart_mux,
594 create_capability!(capabilities::SetDebugWriterCapability),
595 )
596 .finalize(components::debug_writer_component_static!());
597
598 let lldb = components::lldb::LowLevelDebugComponent::new(
599 board_kernel,
600 capsules_core::low_level_debug::DRIVER_NUM,
601 uart_mux,
602 )
603 .finalize(components::low_level_debug_component_static!());
604
605 let scheduler = components::sched::mlfq::MLFQComponent::new(mux_alarm, processes).finalize(
606 components::mlfq_component_static!(
607 litex_vexriscv::timer::LiteXAlarm<
608 'static,
609 'static,
610 socc::SoCRegisterFmt,
611 socc::ClockFrequency,
612 >,
613 NUM_PROCS
614 ),
615 );
616
617 let litex_arty = LiteXArty {
618 console,
619 pconsole,
620 alarm,
621 lldb,
622 led_driver,
623 scheduler,
624 scheduler_timer,
625 ipc: kernel::ipc::IPC::new(
626 board_kernel,
627 kernel::ipc::DRIVER_NUM,
628 &memory_allocation_cap,
629 ),
630 };
631
632 debug!("LiteX+VexRiscv on ArtyA7: initialization complete, entering main loop.");
633 let _ = litex_arty.pconsole.start();
634
635 kernel::process::load_processes(
636 board_kernel,
637 chip,
638 core::slice::from_raw_parts(
639 core::ptr::addr_of!(_sapps),
640 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
641 ),
642 core::slice::from_raw_parts_mut(
643 core::ptr::addr_of_mut!(_sappmem),
644 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
645 ),
646 &FAULT_RESPONSE,
647 &process_mgmt_cap,
648 )
649 .unwrap_or_else(|err| {
650 debug!("Error loading processes!");
651 debug!("{:?}", err);
652 });
653
654 (board_kernel, litex_arty, chip)
655}
656
657#[no_mangle]
659pub unsafe fn main() {
660 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
661
662 let (board_kernel, board, chip) = start();
663 board_kernel.kernel_loop(&board, chip, Some(&board.ipc), &main_loop_capability);
664}