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