|
| 1 | +#include <arch/early_console.h> |
| 2 | +#include <arch/machine.h> |
| 3 | + |
| 4 | +#include <arch/mmio.h> |
| 5 | +#include <arch/mmu.h> |
| 6 | +#include <compiler.h> |
| 7 | +#include <conf/config.h> |
| 8 | +#include <conf/drivers.h> |
| 9 | +#include <cpu.h> |
| 10 | +#include <debug.h> |
| 11 | +#include <dev/intc/sifive/clint/clint.h> |
| 12 | +#include <dev/intc/sifive/plic/plic.h> |
| 13 | +#include <dev/serial/ns16550/ns16550.h> |
| 14 | +#include <intrinsics.h> |
| 15 | +#include <locore.h> |
| 16 | +#include <page.h> |
| 17 | +#include <sch.h> |
| 18 | +#include <sections.h> |
| 19 | +#include <thread.h> |
| 20 | +#include <types.h> |
| 21 | + |
| 22 | +constexpr auto UART = 0x10000000_phys; |
| 23 | + |
| 24 | +void |
| 25 | +machine_init(bootargs *args) |
| 26 | +{ |
| 27 | +#ifdef CONFIG_MPU |
| 28 | +mpu_init(nullptr, 0, 0); |
| 29 | +#endif |
| 30 | + |
| 31 | +const meminfo memory[] = { |
| 32 | +/* Main memory */ |
| 33 | +{ |
| 34 | +.base = phys{CONFIG_RAM_BASE_PHYS}, |
| 35 | +.size = CONFIG_RAM_SIZE, |
| 36 | +.attr = MA_SPEED_0, |
| 37 | +.priority = 0, |
| 38 | +}, |
| 39 | +}; |
| 40 | +page_init(memory, ARRAY_SIZE(memory), args); |
| 41 | + |
| 42 | +/* scratch register is 0 in kernel mode */ |
| 43 | +#ifdef CONFIG_S_MODE |
| 44 | +csrw(sscratch{}); |
| 45 | +#else |
| 46 | +csrw(mscratch{}); |
| 47 | +#endif |
| 48 | +/* initialise kernel thread pointer */ |
| 49 | +asm volatile("mv tp, %0" : : "r"(thread_cur())); |
| 50 | +} |
| 51 | + |
| 52 | +void |
| 53 | +machine_driver_init(bootargs *bootargs) |
| 54 | +{ |
| 55 | +/* |
| 56 | + * Run driver initialisation |
| 57 | + */ |
| 58 | +#include <conf/drivers.c> |
| 59 | +} |
| 60 | + |
| 61 | +void |
| 62 | +machine_idle() |
| 63 | +{ |
| 64 | +asm("wfi"); |
| 65 | +} |
| 66 | + |
| 67 | +[[noreturn]] void |
| 68 | +machine_reset() |
| 69 | +{ |
| 70 | +write32(reinterpret_cast<int *>(0x100000), 0x7777); |
| 71 | + |
| 72 | +/* Workaround clang bug. */ |
| 73 | +while (1) asm(""); |
| 74 | +} |
| 75 | + |
| 76 | +void |
| 77 | +machine_poweroff() |
| 78 | +{ |
| 79 | +write32(reinterpret_cast<int *>(0x100000), 0x5555); |
| 80 | + |
| 81 | +/* Workaround clang bug. */ |
| 82 | +while (1) asm(""); |
| 83 | +} |
| 84 | + |
| 85 | +void |
| 86 | +machine_suspend() |
| 87 | +{ |
| 88 | +info("Suspend is not supported on this platform.\n"); |
| 89 | +} |
| 90 | + |
| 91 | +[[noreturn]] void |
| 92 | +machine_panic() |
| 93 | +{ |
| 94 | +/* Workaround clang bug. */ |
| 95 | +while (1) asm(""); |
| 96 | +} |
| 97 | + |
| 98 | +void |
| 99 | +early_console_init() |
| 100 | +{ |
| 101 | +/* QEMU doesn't care about baud rate */ |
| 102 | +serial::ns16550::early_init(phys_to_virt(UART), 0, CONFIG_EARLY_CONSOLE_CFLAG); |
| 103 | +} |
| 104 | + |
| 105 | +void |
| 106 | +early_console_print(const char *s, size_t len) |
| 107 | +{ |
| 108 | +serial::ns16550::early_print(phys_to_virt(UART), s, len); |
| 109 | +} |
| 110 | + |
| 111 | +__fast_text |
| 112 | +void |
| 113 | +machine_irq() |
| 114 | +{ |
| 115 | +intc_sifive_plic_irq(); |
| 116 | +} |
| 117 | + |
| 118 | +__fast_text |
| 119 | +void |
| 120 | +machine_timer() |
| 121 | +{ |
| 122 | +intc_sifive_clint_timer_irq(); |
| 123 | +} |
0 commit comments