xref: /haiku/src/system/kernel/arch/m68k/arch_cpu.cpp (revision a3dc7ef06c18a924338b8fd916d7ea47ac50fd84)
1845a180fSFrançois Revol /*
24e44040dSFrançois Revol  * Copyright 2007, François Revol, revol@free.fr.
34e44040dSFrançois Revol  * Distributed under the terms of the MIT License.
44e44040dSFrançois Revol  *
5845a180fSFrançois Revol  * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de.
6845a180fSFrançois Revol  * Distributed under the terms of the MIT License.
7845a180fSFrançois Revol  *
8845a180fSFrançois Revol  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
9845a180fSFrançois Revol  * Distributed under the terms of the NewOS License.
10845a180fSFrançois Revol  */
11845a180fSFrançois Revol 
12845a180fSFrançois Revol 
13845a180fSFrançois Revol #include <KernelExport.h>
14845a180fSFrançois Revol 
15845a180fSFrançois Revol #include <arch_platform.h>
16845a180fSFrançois Revol #include <arch_thread.h>
17845a180fSFrançois Revol #include <arch/cpu.h>
18845a180fSFrançois Revol #include <boot/kernel_args.h>
19845a180fSFrançois Revol 
204e44040dSFrançois Revol extern struct m68k_cpu_ops cpu_ops_030;
214e44040dSFrançois Revol extern struct m68k_cpu_ops cpu_ops_040;
224e44040dSFrançois Revol extern struct m68k_cpu_ops cpu_ops_060;
234e44040dSFrançois Revol 
244e44040dSFrançois Revol struct m68k_cpu_ops cpu_ops;
254e44040dSFrançois Revol 
26845a180fSFrançois Revol status_t
27845a180fSFrançois Revol arch_cpu_preboot_init_percpu(kernel_args *args, int curr_cpu)
28845a180fSFrançois Revol {
29845a180fSFrançois Revol 	// enable FPU
30829748d8SFrançois Revol 	//ppc:set_msr(get_msr() | MSR_FP_AVAILABLE);
31845a180fSFrançois Revol 
32845a180fSFrançois Revol 	// The current thread must be NULL for all CPUs till we have threads.
33845a180fSFrançois Revol 	// Some boot code relies on this.
34845a180fSFrançois Revol 	arch_thread_set_current_thread(NULL);
35845a180fSFrançois Revol 
36845a180fSFrançois Revol 	return B_OK;
37845a180fSFrançois Revol }
38845a180fSFrançois Revol 
39845a180fSFrançois Revol 
40845a180fSFrançois Revol status_t
41845a180fSFrançois Revol arch_cpu_init(kernel_args *args)
42845a180fSFrançois Revol {
43*a3dc7ef0SFrançois Revol 	arch_cpu_type = args->arch_args.cpu_type;
44*a3dc7ef0SFrançois Revol 	arch_fpu_type = args->arch_args.fpu_type;
45*a3dc7ef0SFrançois Revol 	arch_mmu_type = args->arch_args.mmu_type;
46*a3dc7ef0SFrançois Revol 	arch_platform = args->arch_args.platform;
47*a3dc7ef0SFrançois Revol 	void (*flush_insn_pipeline)(void);
48*a3dc7ef0SFrançois Revol 	void (*flush_atc_all)(void);
49*a3dc7ef0SFrançois Revol 	void (*flush_atc_user)(void);
50*a3dc7ef0SFrançois Revol 	void (*flush_atc_addr)(void *addr);
51*a3dc7ef0SFrançois Revol 	void (*flush_dcache)(void *address, size_t len);
52*a3dc7ef0SFrançois Revol 	void (*flush_icache)(void *address, size_t len);
53*a3dc7ef0SFrançois Revol 	void (*idle)(void);
544e44040dSFrançois Revol 
55*a3dc7ef0SFrançois Revol 	switch (arch_cpu_type) {
56*a3dc7ef0SFrançois Revol 		case 68020:
57*a3dc7ef0SFrançois Revol 		case 68030:
584e44040dSFrançois Revol 			cpu_ops.flush_insn_pipeline = cpu_ops_030.flush_insn_pipeline;
594e44040dSFrançois Revol 			cpu_ops.flush_atc_all = cpu_ops_030.flush_atc_all;
604e44040dSFrançois Revol 			cpu_ops.flush_atc_user = cpu_ops_030.flush_atc_user;
614e44040dSFrançois Revol 			cpu_ops.flush_atc_addr = cpu_ops_030.flush_atc_addr;
62*a3dc7ef0SFrançois Revol 			cpu_ops.flush_dcache = cpu_ops_030.flush_dcache;
63*a3dc7ef0SFrançois Revol 			cpu_ops.flush_icache = cpu_ops_030.flush_icache;
644e44040dSFrançois Revol 			cpu_ops.idle = cpu_ops_030.idle; // NULL
654e44040dSFrançois Revol 			break;
664e44040dSFrançois Revol #ifdef SUPPORTS_040
67*a3dc7ef0SFrançois Revol 		case 68040:
684e44040dSFrançois Revol 			cpu_ops.flush_insn_pipeline = cpu_ops_040.flush_insn_pipeline;
694e44040dSFrançois Revol 			cpu_ops.flush_atc_all = cpu_ops_040.flush_atc_all;
704e44040dSFrançois Revol 			cpu_ops.flush_atc_user = cpu_ops_040.flush_atc_user;
714e44040dSFrançois Revol 			cpu_ops.flush_atc_addr = cpu_ops_040.flush_atc_addr;
72*a3dc7ef0SFrançois Revol 			cpu_ops.flush_dcache = cpu_ops_040.flush_dcache;
73*a3dc7ef0SFrançois Revol 			cpu_ops.flush_icache = cpu_ops_040.flush_icache;
744e44040dSFrançois Revol 			cpu_ops.idle = cpu_ops_040.idle; // NULL
754e44040dSFrançois Revol 			break;
764e44040dSFrançois Revol #endif
774e44040dSFrançois Revol #ifdef SUPPORTS_060
78*a3dc7ef0SFrançois Revol 		case 68060:
794e44040dSFrançois Revol 			cpu_ops.flush_insn_pipeline = cpu_ops_060.flush_insn_pipeline;
804e44040dSFrançois Revol 			cpu_ops.flush_atc_all = cpu_ops_060.flush_atc_all;
814e44040dSFrançois Revol 			cpu_ops.flush_atc_user = cpu_ops_060.flush_atc_user;
824e44040dSFrançois Revol 			cpu_ops.flush_atc_addr = cpu_ops_060.flush_atc_addr;
83*a3dc7ef0SFrançois Revol 			cpu_ops.flush_dcache = cpu_ops_060.flush_dcache;
84*a3dc7ef0SFrançois Revol 			cpu_ops.flush_icache = cpu_ops_060.flush_icache;
854e44040dSFrançois Revol 			cpu_ops.idle = cpu_ops_060.idle;
864e44040dSFrançois Revol 		break;
874e44040dSFrançois Revol #endif
884e44040dSFrançois Revol 		default:
89*a3dc7ef0SFrançois Revol 			panic("unknown cpu_type %d\n", arch_cpu_type);
904e44040dSFrançois Revol 	}
91*a3dc7ef0SFrançois Revol 
92845a180fSFrançois Revol 	return B_OK;
93845a180fSFrançois Revol }
94845a180fSFrançois Revol 
95845a180fSFrançois Revol 
96845a180fSFrançois Revol status_t
97845a180fSFrançois Revol arch_cpu_init_post_vm(kernel_args *args)
98845a180fSFrançois Revol {
99845a180fSFrançois Revol 	return B_OK;
100845a180fSFrançois Revol }
101845a180fSFrançois Revol 
102845a180fSFrançois Revol status_t
103845a180fSFrançois Revol arch_cpu_init_post_modules(kernel_args *args)
104845a180fSFrançois Revol {
105845a180fSFrançois Revol 	return B_OK;
106845a180fSFrançois Revol }
107845a180fSFrançois Revol 
108845a180fSFrançois Revol 
109845a180fSFrançois Revol void
110845a180fSFrançois Revol arch_cpu_sync_icache(void *address, size_t len)
111845a180fSFrançois Revol {
112*a3dc7ef0SFrançois Revol 	cpu_ops.flush_icache((addr_t)address, len);
113845a180fSFrançois Revol }
114845a180fSFrançois Revol 
115845a180fSFrançois Revol 
116845a180fSFrançois Revol void
117807cf76dSFrançois Revol arch_cpu_memory_read_barrier(void)
118807cf76dSFrançois Revol {
119807cf76dSFrançois Revol 	asm volatile ("nop;" : : : "memory");
120807cf76dSFrançois Revol #warning M68k: check arch_cpu_memory_read_barrier
121807cf76dSFrançois Revol }
122807cf76dSFrançois Revol 
123807cf76dSFrançois Revol 
124807cf76dSFrançois Revol void
125807cf76dSFrançois Revol arch_cpu_memory_write_barrier(void)
126807cf76dSFrançois Revol {
127807cf76dSFrançois Revol 	asm volatile ("nop;" : : : "memory");
128807cf76dSFrançois Revol #warning M68k: check arch_cpu_memory_write_barrier
129807cf76dSFrançois Revol }
130807cf76dSFrançois Revol 
131807cf76dSFrançois Revol 
132807cf76dSFrançois Revol void
133845a180fSFrançois Revol arch_cpu_invalidate_TLB_range(addr_t start, addr_t end)
134845a180fSFrançois Revol {
1354e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
136845a180fSFrançois Revol 	while (start < end) {
1374e44040dSFrançois Revol 		cpu_ops.flush_atc_addr(start);
1384e44040dSFrançois Revol 		cpu_ops.flush_insn_pipeline();
139845a180fSFrançois Revol 		start += B_PAGE_SIZE;
140845a180fSFrançois Revol 	}
1414e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
142845a180fSFrançois Revol }
143845a180fSFrançois Revol 
144845a180fSFrançois Revol 
145845a180fSFrançois Revol void
146845a180fSFrançois Revol arch_cpu_invalidate_TLB_list(addr_t pages[], int num_pages)
147845a180fSFrançois Revol {
148845a180fSFrançois Revol 	int i;
149845a180fSFrançois Revol 
1504e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
151845a180fSFrançois Revol 	for (i = 0; i < num_pages; i++) {
1524e44040dSFrançois Revol 		cpu_ops.flush_atc_addr(pages[i]);
1534e44040dSFrançois Revol 		cpu_ops.flush_insn_pipeline();
154845a180fSFrançois Revol 	}
1554e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
156845a180fSFrançois Revol }
157845a180fSFrançois Revol 
158845a180fSFrançois Revol 
159845a180fSFrançois Revol void
160845a180fSFrançois Revol arch_cpu_global_TLB_invalidate(void)
161845a180fSFrançois Revol {
1624e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
1634e44040dSFrançois Revol 	cpu_ops.flush_atc_all();
1644e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
165845a180fSFrançois Revol }
166845a180fSFrançois Revol 
167845a180fSFrançois Revol 
168845a180fSFrançois Revol void
169845a180fSFrançois Revol arch_cpu_user_TLB_invalidate(void)
170845a180fSFrançois Revol {
1714e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
1724e44040dSFrançois Revol 	cpu_ops.flush_atc_user();
1734e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
174845a180fSFrançois Revol }
175845a180fSFrançois Revol 
176845a180fSFrançois Revol 
177845a180fSFrançois Revol status_t
178845a180fSFrançois Revol arch_cpu_user_memcpy(void *to, const void *from, size_t size,
179845a180fSFrançois Revol 	addr_t *faultHandler)
180845a180fSFrançois Revol {
181845a180fSFrançois Revol 	char *tmp = (char *)to;
182845a180fSFrançois Revol 	char *s = (char *)from;
183845a180fSFrançois Revol 	addr_t oldFaultHandler = *faultHandler;
184845a180fSFrançois Revol 
185845a180fSFrançois Revol 	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
186845a180fSFrançois Revol 		goto error;
187845a180fSFrançois Revol 
188845a180fSFrançois Revol 	while (size--)
189845a180fSFrançois Revol 		*tmp++ = *s++;
190845a180fSFrançois Revol 
191845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
192845a180fSFrançois Revol 	return 0;
193845a180fSFrançois Revol 
194845a180fSFrançois Revol error:
195845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
196845a180fSFrançois Revol 	return B_BAD_ADDRESS;
197845a180fSFrançois Revol }
198845a180fSFrançois Revol 
199845a180fSFrançois Revol 
200845a180fSFrançois Revol /**	\brief Copies at most (\a size - 1) characters from the string in \a from to
201845a180fSFrançois Revol  *	the string in \a to, NULL-terminating the result.
202845a180fSFrançois Revol  *
203845a180fSFrançois Revol  *	\param to Pointer to the destination C-string.
204845a180fSFrançois Revol  *	\param from Pointer to the source C-string.
205845a180fSFrançois Revol  *	\param size Size in bytes of the string buffer pointed to by \a to.
206845a180fSFrançois Revol  *
207845a180fSFrançois Revol  *	\return strlen(\a from).
208845a180fSFrançois Revol  */
209845a180fSFrançois Revol 
210845a180fSFrançois Revol ssize_t
211845a180fSFrançois Revol arch_cpu_user_strlcpy(char *to, const char *from, size_t size, addr_t *faultHandler)
212845a180fSFrançois Revol {
213845a180fSFrançois Revol 	int from_length = 0;
214845a180fSFrançois Revol 	addr_t oldFaultHandler = *faultHandler;
215845a180fSFrançois Revol 
216845a180fSFrançois Revol 	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
217845a180fSFrançois Revol 		goto error;
218845a180fSFrançois Revol 
219845a180fSFrançois Revol 	if (size > 0) {
220845a180fSFrançois Revol 		to[--size] = '\0';
221845a180fSFrançois Revol 		// copy
222845a180fSFrançois Revol 		for ( ; size; size--, from_length++, to++, from++) {
223845a180fSFrançois Revol 			if ((*to = *from) == '\0')
224845a180fSFrançois Revol 				break;
225845a180fSFrançois Revol 		}
226845a180fSFrançois Revol 	}
227845a180fSFrançois Revol 	// count any leftover from chars
228845a180fSFrançois Revol 	while (*from++ != '\0')
229845a180fSFrançois Revol 		from_length++;
230845a180fSFrançois Revol 
231845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
232845a180fSFrançois Revol 	return from_length;
233845a180fSFrançois Revol 
234845a180fSFrançois Revol error:
235845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
236845a180fSFrançois Revol 	return B_BAD_ADDRESS;
237845a180fSFrançois Revol }
238845a180fSFrançois Revol 
239845a180fSFrançois Revol 
240845a180fSFrançois Revol status_t
241845a180fSFrançois Revol arch_cpu_user_memset(void *s, char c, size_t count, addr_t *faultHandler)
242845a180fSFrançois Revol {
243845a180fSFrançois Revol 	char *xs = (char *)s;
244845a180fSFrançois Revol 	addr_t oldFaultHandler = *faultHandler;
245845a180fSFrançois Revol 
246845a180fSFrançois Revol 	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
247845a180fSFrançois Revol 		goto error;
248845a180fSFrançois Revol 
249845a180fSFrançois Revol 	while (count--)
250845a180fSFrançois Revol 		*xs++ = c;
251845a180fSFrançois Revol 
252845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
253845a180fSFrançois Revol 	return 0;
254845a180fSFrançois Revol 
255845a180fSFrançois Revol error:
256845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
257845a180fSFrançois Revol 	return B_BAD_ADDRESS;
258845a180fSFrançois Revol }
259845a180fSFrançois Revol 
260845a180fSFrançois Revol 
261845a180fSFrançois Revol status_t
262845a180fSFrançois Revol arch_cpu_shutdown(bool reboot)
263845a180fSFrançois Revol {
264845a180fSFrançois Revol 	M68KPlatform::Default()->ShutDown(reboot);
265845a180fSFrançois Revol 	return B_ERROR;
266845a180fSFrançois Revol }
267845a180fSFrançois Revol 
268845a180fSFrançois Revol 
269845a180fSFrançois Revol void
270845a180fSFrançois Revol arch_cpu_idle(void)
271845a180fSFrançois Revol {
2724e44040dSFrançois Revol 	if (cpu_ops.idle)
2734e44040dSFrançois Revol 		cpu_ops.idle();
274ac1a8a0cSFrançois Revol #warning M68K: use LPSTOP ?
275ac1a8a0cSFrançois Revol 	//asm volatile ("lpstop");
276845a180fSFrançois Revol }
277845a180fSFrançois Revol 
278845a180fSFrançois Revol 
279845a180fSFrançois Revol // The purpose of this function is to trick the compiler. When setting the
280845a180fSFrançois Revol // page_handler to a label that is obviously (to the compiler) never used,
281845a180fSFrançois Revol // it may reorganize the control flow, so that the labeled part is optimized
282845a180fSFrançois Revol // away.
283845a180fSFrançois Revol // By invoking the function like this
284845a180fSFrançois Revol //
285845a180fSFrançois Revol //	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
286845a180fSFrançois Revol //		goto error;
287845a180fSFrançois Revol //
288845a180fSFrançois Revol // the compiler has to keep the labeled code, since it can't guess the return
289845a180fSFrançois Revol // value of this (non-inlinable) function. At least in my tests it worked that
290845a180fSFrançois Revol // way, and I hope it will continue to work like this in the future.
291845a180fSFrançois Revol //
292845a180fSFrançois Revol bool
293845a180fSFrançois Revol m68k_set_fault_handler(addr_t *handlerLocation, addr_t handler)
294845a180fSFrançois Revol {
295845a180fSFrançois Revol 	*handlerLocation = handler;
296845a180fSFrançois Revol 	return false;
297845a180fSFrançois Revol }
298