xref: /haiku/src/system/kernel/arch/m68k/arch_cpu.cpp (revision 4237dbd0fc80f59eca07b21a2d43bca8a21a41e6)
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 {
43a3dc7ef0SFrançois Revol 	arch_cpu_type = args->arch_args.cpu_type;
44a3dc7ef0SFrançois Revol 	arch_fpu_type = args->arch_args.fpu_type;
45a3dc7ef0SFrançois Revol 	arch_mmu_type = args->arch_args.mmu_type;
46a3dc7ef0SFrançois Revol 	arch_platform = args->arch_args.platform;
47a3dc7ef0SFrançois Revol 	void (*flush_insn_pipeline)(void);
48a3dc7ef0SFrançois Revol 	void (*flush_atc_all)(void);
49a3dc7ef0SFrançois Revol 	void (*flush_atc_user)(void);
50a3dc7ef0SFrançois Revol 	void (*flush_atc_addr)(void *addr);
51a3dc7ef0SFrançois Revol 	void (*flush_dcache)(void *address, size_t len);
52a3dc7ef0SFrançois Revol 	void (*flush_icache)(void *address, size_t len);
53a3dc7ef0SFrançois Revol 	void (*idle)(void);
544e44040dSFrançois Revol 
55a3dc7ef0SFrançois Revol 	switch (arch_cpu_type) {
56a3dc7ef0SFrançois Revol 		case 68020:
57a3dc7ef0SFranç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;
62a3dc7ef0SFrançois Revol 			cpu_ops.flush_dcache = cpu_ops_030.flush_dcache;
63a3dc7ef0SFranç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
67a3dc7ef0SFranç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;
72a3dc7ef0SFrançois Revol 			cpu_ops.flush_dcache = cpu_ops_040.flush_dcache;
73a3dc7ef0SFranç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
78a3dc7ef0SFranç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;
83a3dc7ef0SFrançois Revol 			cpu_ops.flush_dcache = cpu_ops_060.flush_dcache;
84a3dc7ef0SFranç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:
89a3dc7ef0SFrançois Revol 			panic("unknown cpu_type %d\n", arch_cpu_type);
904e44040dSFrançois Revol 	}
91a3dc7ef0SFranç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 {
112a3dc7ef0SFranç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 {
135*4237dbd0SFrançois Revol 	int32 num_pages = end / B_PAGE_SIZE - start / B_PAGE_SIZE;
1364e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
137*4237dbd0SFrançois Revol 	while (num_pages-- >= 0) {
1384e44040dSFrançois Revol 		cpu_ops.flush_atc_addr(start);
1394e44040dSFrançois Revol 		cpu_ops.flush_insn_pipeline();
140845a180fSFrançois Revol 		start += B_PAGE_SIZE;
141845a180fSFrançois Revol 	}
1424e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
143845a180fSFrançois Revol }
144845a180fSFrançois Revol 
145845a180fSFrançois Revol 
146845a180fSFrançois Revol void
147845a180fSFrançois Revol arch_cpu_invalidate_TLB_list(addr_t pages[], int num_pages)
148845a180fSFrançois Revol {
149845a180fSFrançois Revol 	int i;
150845a180fSFrançois Revol 
1514e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
152845a180fSFrançois Revol 	for (i = 0; i < num_pages; i++) {
1534e44040dSFrançois Revol 		cpu_ops.flush_atc_addr(pages[i]);
1544e44040dSFrançois Revol 		cpu_ops.flush_insn_pipeline();
155845a180fSFrançois Revol 	}
1564e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
157845a180fSFrançois Revol }
158845a180fSFrançois Revol 
159845a180fSFrançois Revol 
160845a180fSFrançois Revol void
161845a180fSFrançois Revol arch_cpu_global_TLB_invalidate(void)
162845a180fSFrançois Revol {
1634e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
1644e44040dSFrançois Revol 	cpu_ops.flush_atc_all();
1654e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
166845a180fSFrançois Revol }
167845a180fSFrançois Revol 
168845a180fSFrançois Revol 
169845a180fSFrançois Revol void
170845a180fSFrançois Revol arch_cpu_user_TLB_invalidate(void)
171845a180fSFrançois Revol {
1724e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
1734e44040dSFrançois Revol 	cpu_ops.flush_atc_user();
1744e44040dSFrançois Revol 	cpu_ops.flush_insn_pipeline();
175845a180fSFrançois Revol }
176845a180fSFrançois Revol 
177845a180fSFrançois Revol 
178845a180fSFrançois Revol status_t
179845a180fSFrançois Revol arch_cpu_user_memcpy(void *to, const void *from, size_t size,
180845a180fSFrançois Revol 	addr_t *faultHandler)
181845a180fSFrançois Revol {
182845a180fSFrançois Revol 	char *tmp = (char *)to;
183845a180fSFrançois Revol 	char *s = (char *)from;
184845a180fSFrançois Revol 	addr_t oldFaultHandler = *faultHandler;
185845a180fSFrançois Revol 
186845a180fSFrançois Revol 	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
187845a180fSFrançois Revol 		goto error;
188845a180fSFrançois Revol 
189845a180fSFrançois Revol 	while (size--)
190845a180fSFrançois Revol 		*tmp++ = *s++;
191845a180fSFrançois Revol 
192845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
193845a180fSFrançois Revol 	return 0;
194845a180fSFrançois Revol 
195845a180fSFrançois Revol error:
196845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
197845a180fSFrançois Revol 	return B_BAD_ADDRESS;
198845a180fSFrançois Revol }
199845a180fSFrançois Revol 
200845a180fSFrançois Revol 
201845a180fSFrançois Revol /**	\brief Copies at most (\a size - 1) characters from the string in \a from to
202845a180fSFrançois Revol  *	the string in \a to, NULL-terminating the result.
203845a180fSFrançois Revol  *
204845a180fSFrançois Revol  *	\param to Pointer to the destination C-string.
205845a180fSFrançois Revol  *	\param from Pointer to the source C-string.
206845a180fSFrançois Revol  *	\param size Size in bytes of the string buffer pointed to by \a to.
207845a180fSFrançois Revol  *
208845a180fSFrançois Revol  *	\return strlen(\a from).
209845a180fSFrançois Revol  */
210845a180fSFrançois Revol 
211845a180fSFrançois Revol ssize_t
212845a180fSFrançois Revol arch_cpu_user_strlcpy(char *to, const char *from, size_t size, addr_t *faultHandler)
213845a180fSFrançois Revol {
214845a180fSFrançois Revol 	int from_length = 0;
215845a180fSFrançois Revol 	addr_t oldFaultHandler = *faultHandler;
216845a180fSFrançois Revol 
217845a180fSFrançois Revol 	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
218845a180fSFrançois Revol 		goto error;
219845a180fSFrançois Revol 
220845a180fSFrançois Revol 	if (size > 0) {
221845a180fSFrançois Revol 		to[--size] = '\0';
222845a180fSFrançois Revol 		// copy
223845a180fSFrançois Revol 		for ( ; size; size--, from_length++, to++, from++) {
224845a180fSFrançois Revol 			if ((*to = *from) == '\0')
225845a180fSFrançois Revol 				break;
226845a180fSFrançois Revol 		}
227845a180fSFrançois Revol 	}
228845a180fSFrançois Revol 	// count any leftover from chars
229845a180fSFrançois Revol 	while (*from++ != '\0')
230845a180fSFrançois Revol 		from_length++;
231845a180fSFrançois Revol 
232845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
233845a180fSFrançois Revol 	return from_length;
234845a180fSFrançois Revol 
235845a180fSFrançois Revol error:
236845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
237845a180fSFrançois Revol 	return B_BAD_ADDRESS;
238845a180fSFrançois Revol }
239845a180fSFrançois Revol 
240845a180fSFrançois Revol 
241845a180fSFrançois Revol status_t
242845a180fSFrançois Revol arch_cpu_user_memset(void *s, char c, size_t count, addr_t *faultHandler)
243845a180fSFrançois Revol {
244845a180fSFrançois Revol 	char *xs = (char *)s;
245845a180fSFrançois Revol 	addr_t oldFaultHandler = *faultHandler;
246845a180fSFrançois Revol 
247845a180fSFrançois Revol 	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
248845a180fSFrançois Revol 		goto error;
249845a180fSFrançois Revol 
250845a180fSFrançois Revol 	while (count--)
251845a180fSFrançois Revol 		*xs++ = c;
252845a180fSFrançois Revol 
253845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
254845a180fSFrançois Revol 	return 0;
255845a180fSFrançois Revol 
256845a180fSFrançois Revol error:
257845a180fSFrançois Revol 	*faultHandler = oldFaultHandler;
258845a180fSFrançois Revol 	return B_BAD_ADDRESS;
259845a180fSFrançois Revol }
260845a180fSFrançois Revol 
261845a180fSFrançois Revol 
262845a180fSFrançois Revol status_t
263845a180fSFrançois Revol arch_cpu_shutdown(bool reboot)
264845a180fSFrançois Revol {
265845a180fSFrançois Revol 	M68KPlatform::Default()->ShutDown(reboot);
266845a180fSFrançois Revol 	return B_ERROR;
267845a180fSFrançois Revol }
268845a180fSFrançois Revol 
269845a180fSFrançois Revol 
270845a180fSFrançois Revol void
271845a180fSFrançois Revol arch_cpu_idle(void)
272845a180fSFrançois Revol {
2734e44040dSFrançois Revol 	if (cpu_ops.idle)
2744e44040dSFrançois Revol 		cpu_ops.idle();
275ac1a8a0cSFrançois Revol #warning M68K: use LPSTOP ?
276ac1a8a0cSFrançois Revol 	//asm volatile ("lpstop");
277845a180fSFrançois Revol }
278845a180fSFrançois Revol 
279845a180fSFrançois Revol 
280845a180fSFrançois Revol // The purpose of this function is to trick the compiler. When setting the
281845a180fSFrançois Revol // page_handler to a label that is obviously (to the compiler) never used,
282845a180fSFrançois Revol // it may reorganize the control flow, so that the labeled part is optimized
283845a180fSFrançois Revol // away.
284845a180fSFrançois Revol // By invoking the function like this
285845a180fSFrançois Revol //
286845a180fSFrançois Revol //	if (m68k_set_fault_handler(faultHandler, (addr_t)&&error))
287845a180fSFrançois Revol //		goto error;
288845a180fSFrançois Revol //
289845a180fSFrançois Revol // the compiler has to keep the labeled code, since it can't guess the return
290845a180fSFrançois Revol // value of this (non-inlinable) function. At least in my tests it worked that
291845a180fSFrançois Revol // way, and I hope it will continue to work like this in the future.
292845a180fSFrançois Revol //
293845a180fSFrançois Revol bool
294845a180fSFrançois Revol m68k_set_fault_handler(addr_t *handlerLocation, addr_t handler)
295845a180fSFrançois Revol {
296845a180fSFrançois Revol 	*handlerLocation = handler;
297845a180fSFrançois Revol 	return false;
298845a180fSFrançois Revol }
299