1c085f386SAlexander von Gluck IV /* 2c085f386SAlexander von Gluck IV * Copyright 2019, Adrien Destugues, pulkomandy@pulkomandy.tk. 3c085f386SAlexander von Gluck IV * Distributed under the terms of the MIT License. 4c085f386SAlexander von Gluck IV */ 5c085f386SAlexander von Gluck IV 6c085f386SAlexander von Gluck IV 7c085f386SAlexander von Gluck IV #include <KernelExport.h> 8c085f386SAlexander von Gluck IV 9c085f386SAlexander von Gluck IV #include <arch/cpu.h> 10c085f386SAlexander von Gluck IV #include <boot/kernel_args.h> 11d031c09cSX512 #include <vm/VMAddressSpace.h> 12c085f386SAlexander von Gluck IV #include <commpage.h> 13c085f386SAlexander von Gluck IV #include <elf.h> 14d031c09cSX512 #include <Htif.h> 15d031c09cSX512 #include <platform/sbi/sbi_syscalls.h> 16d031c09cSX512 17d031c09cSX512 18*8ca0f03dSX512 extern "C" void SVec(); 19*8ca0f03dSX512 20fa41d6faSX512 extern uint32 gPlatform; 21c085f386SAlexander von Gluck IV 22c085f386SAlexander von Gluck IV 23c085f386SAlexander von Gluck IV status_t 24c085f386SAlexander von Gluck IV arch_cpu_preboot_init_percpu(kernel_args *args, int curr_cpu) 25c085f386SAlexander von Gluck IV { 26*8ca0f03dSX512 // dprintf("arch_cpu_preboot_init_percpu(%" B_PRId32 ")\n", curr_cpu); 27c085f386SAlexander von Gluck IV return B_OK; 28c085f386SAlexander von Gluck IV } 29c085f386SAlexander von Gluck IV 30c085f386SAlexander von Gluck IV 31c085f386SAlexander von Gluck IV status_t 32c085f386SAlexander von Gluck IV arch_cpu_init_percpu(kernel_args *args, int curr_cpu) 33c085f386SAlexander von Gluck IV { 34*8ca0f03dSX512 SetStvec((uint64)SVec); 35*8ca0f03dSX512 SstatusReg sstatus(Sstatus()); 36*8ca0f03dSX512 sstatus.ie = 0; 37*8ca0f03dSX512 sstatus.fs = extStatusInitial; // enable FPU 38*8ca0f03dSX512 sstatus.xs = extStatusOff; 39*8ca0f03dSX512 SetSstatus(sstatus.val); 40*8ca0f03dSX512 SetSie(Sie() | (1 << sTimerInt) | (1 << sSoftInt) | (1 << sExternInt)); 41c085f386SAlexander von Gluck IV 42*8ca0f03dSX512 return B_OK; 43c085f386SAlexander von Gluck IV } 44c085f386SAlexander von Gluck IV 45c085f386SAlexander von Gluck IV 46c085f386SAlexander von Gluck IV status_t 47c085f386SAlexander von Gluck IV arch_cpu_init(kernel_args *args) 48c085f386SAlexander von Gluck IV { 49*8ca0f03dSX512 for (uint32 curCpu = 0; curCpu < args->num_cpus; curCpu++) { 50*8ca0f03dSX512 cpu_ent* cpu = &gCPU[curCpu]; 51*8ca0f03dSX512 52*8ca0f03dSX512 cpu->arch.hartId = args->arch_args.hartIds[curCpu]; 53*8ca0f03dSX512 54*8ca0f03dSX512 cpu->topology_id[CPU_TOPOLOGY_PACKAGE] = 0; 55*8ca0f03dSX512 cpu->topology_id[CPU_TOPOLOGY_CORE] = curCpu; 56*8ca0f03dSX512 cpu->topology_id[CPU_TOPOLOGY_SMT] = 0; 57*8ca0f03dSX512 58*8ca0f03dSX512 for (unsigned int i = 0; i < CPU_MAX_CACHE_LEVEL; i++) 59*8ca0f03dSX512 cpu->cache_id[i] = -1; 60*8ca0f03dSX512 } 61*8ca0f03dSX512 62d031c09cSX512 /* 63d031c09cSX512 uint64 conversionFactor 645855a8e3SPrzemysław Buczkowski = (1LL << 32) * 1000000LL / args->arch_args.timerFrequency; 65d031c09cSX512 66d031c09cSX512 __riscv64_setup_system_time(conversionFactor); 67d031c09cSX512 */ 68c085f386SAlexander von Gluck IV return B_OK; 69c085f386SAlexander von Gluck IV } 70c085f386SAlexander von Gluck IV 71c085f386SAlexander von Gluck IV 72c085f386SAlexander von Gluck IV status_t 73c085f386SAlexander von Gluck IV arch_cpu_init_post_vm(kernel_args *args) 74c085f386SAlexander von Gluck IV { 75d031c09cSX512 // Set address space ownership to currently running threads 76d031c09cSX512 for (uint32 i = 0; i < args->num_cpus; i++) { 77d031c09cSX512 VMAddressSpace::Kernel()->Get(); 78d031c09cSX512 } 79d031c09cSX512 80c085f386SAlexander von Gluck IV return B_OK; 81c085f386SAlexander von Gluck IV } 82c085f386SAlexander von Gluck IV 83c085f386SAlexander von Gluck IV 84c085f386SAlexander von Gluck IV status_t 85c085f386SAlexander von Gluck IV arch_cpu_init_post_modules(kernel_args *args) 86c085f386SAlexander von Gluck IV { 87c085f386SAlexander von Gluck IV return B_OK; 88c085f386SAlexander von Gluck IV } 89c085f386SAlexander von Gluck IV 90c085f386SAlexander von Gluck IV 91c085f386SAlexander von Gluck IV void 92c085f386SAlexander von Gluck IV arch_cpu_sync_icache(void *address, size_t len) 93c085f386SAlexander von Gluck IV { 94c085f386SAlexander von Gluck IV } 95c085f386SAlexander von Gluck IV 96c085f386SAlexander von Gluck IV 97c085f386SAlexander von Gluck IV void 98c085f386SAlexander von Gluck IV arch_cpu_memory_read_barrier(void) 99c085f386SAlexander von Gluck IV { 100c085f386SAlexander von Gluck IV } 101c085f386SAlexander von Gluck IV 102c085f386SAlexander von Gluck IV 103c085f386SAlexander von Gluck IV void 104c085f386SAlexander von Gluck IV arch_cpu_memory_write_barrier(void) 105c085f386SAlexander von Gluck IV { 106c085f386SAlexander von Gluck IV } 107c085f386SAlexander von Gluck IV 108c085f386SAlexander von Gluck IV 109c085f386SAlexander von Gluck IV void 110c085f386SAlexander von Gluck IV arch_cpu_invalidate_TLB_range(addr_t start, addr_t end) 111c085f386SAlexander von Gluck IV { 112*8ca0f03dSX512 int32 numPages = end / B_PAGE_SIZE - start / B_PAGE_SIZE; 113*8ca0f03dSX512 while (numPages-- >= 0) { 114*8ca0f03dSX512 FlushTlbPage(start); 115*8ca0f03dSX512 start += B_PAGE_SIZE; 116*8ca0f03dSX512 } 117c085f386SAlexander von Gluck IV } 118c085f386SAlexander von Gluck IV 119c085f386SAlexander von Gluck IV 120c085f386SAlexander von Gluck IV void 121c085f386SAlexander von Gluck IV arch_cpu_invalidate_TLB_list(addr_t pages[], int num_pages) 122c085f386SAlexander von Gluck IV { 123*8ca0f03dSX512 for (int i = 0; i < num_pages; i++) 124*8ca0f03dSX512 FlushTlbPage(pages[i]); 125c085f386SAlexander von Gluck IV } 126c085f386SAlexander von Gluck IV 127c085f386SAlexander von Gluck IV 128c085f386SAlexander von Gluck IV void 129c085f386SAlexander von Gluck IV arch_cpu_global_TLB_invalidate(void) 130c085f386SAlexander von Gluck IV { 131*8ca0f03dSX512 FlushTlbAll(); 132c085f386SAlexander von Gluck IV } 133c085f386SAlexander von Gluck IV 134c085f386SAlexander von Gluck IV 135c085f386SAlexander von Gluck IV void 136c085f386SAlexander von Gluck IV arch_cpu_user_TLB_invalidate(void) 137c085f386SAlexander von Gluck IV { 138*8ca0f03dSX512 FlushTlbAll(); 139c085f386SAlexander von Gluck IV } 140c085f386SAlexander von Gluck IV 141c085f386SAlexander von Gluck IV 142c085f386SAlexander von Gluck IV status_t 143c085f386SAlexander von Gluck IV arch_cpu_shutdown(bool reboot) 144c085f386SAlexander von Gluck IV { 145fa41d6faSX512 if (gPlatform == kPlatformSbi) { 146d031c09cSX512 sbi_system_reset( 147d031c09cSX512 reboot ? SBI_RESET_TYPE_COLD_REBOOT : SBI_RESET_TYPE_SHUTDOWN, 148d031c09cSX512 SBI_RESET_REASON_NONE); 149d031c09cSX512 } 150d031c09cSX512 151d031c09cSX512 HtifShutdown(); 152c085f386SAlexander von Gluck IV return B_ERROR; 153c085f386SAlexander von Gluck IV } 154