xref: /haiku/src/system/kernel/arch/ppc/arch_cpu.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 
9 
10 #include <KernelExport.h>
11 
12 #include <arch_platform.h>
13 #include <arch_thread.h>
14 #include <arch/cpu.h>
15 #include <boot/kernel_args.h>
16 
17 static bool sHasTlbia;
18 
19 status_t
20 arch_cpu_preboot_init_percpu(kernel_args *args, int curr_cpu)
21 {
22 	// enable FPU
23 	set_msr(get_msr() | MSR_FP_AVAILABLE);
24 
25 	// The current thread must be NULL for all CPUs till we have threads.
26 	// Some boot code relies on this.
27 	arch_thread_set_current_thread(NULL);
28 
29 	return B_OK;
30 }
31 
32 
33 status_t
34 arch_cpu_init(kernel_args *args)
35 {
36 	// TODO: Let the boot loader put that info into the kernel args
37 	// (property "tlbia" in the CPU node).
38 	sHasTlbia = false;
39 
40 	return B_OK;
41 }
42 
43 
44 status_t
45 arch_cpu_init_post_vm(kernel_args *args)
46 {
47 	return B_OK;
48 }
49 
50 status_t
51 arch_cpu_init_percpu(kernel_args *args, int curr_cpu)
52 {
53         //detect_cpu(curr_cpu);
54 
55         // we only support one on ppc anyway at the moment...
56 	//XXX: WRITEME
57         return 0;
58 }
59 
60 status_t
61 arch_cpu_init_post_modules(kernel_args *args)
62 {
63 	return B_OK;
64 }
65 
66 #define CACHELINE 32
67 
68 void
69 arch_cpu_sync_icache(void *address, size_t len)
70 {
71 	int l, off;
72 	char *p;
73 
74 	off = (unsigned int)address & (CACHELINE - 1);
75 	len += off;
76 
77 	l = len;
78 	p = (char *)address - off;
79 	do {
80 		asm volatile ("dcbst 0,%0" :: "r"(p));
81 		p += CACHELINE;
82 	} while ((l -= CACHELINE) > 0);
83 	asm volatile ("sync");
84 
85 	p = (char *)address - off;
86 	do {
87 		asm volatile ("icbi 0,%0" :: "r"(p));
88 		p += CACHELINE;
89 	} while ((len -= CACHELINE) > 0);
90 	asm volatile ("sync");
91 	isync();
92 }
93 
94 
95 void
96 arch_cpu_memory_read_barrier(void)
97 {
98 // WARNING PPC: is it model-dependant ?
99 	asm volatile ("lwsync");
100 }
101 
102 
103 void
104 arch_cpu_memory_write_barrier(void)
105 {
106 // WARNING PPC: is it model-dependant ?
107 	asm volatile ("isync");
108 	asm volatile ("eieio");
109 }
110 
111 
112 void
113 arch_cpu_invalidate_TLB_range(addr_t start, addr_t end)
114 {
115 	asm volatile("sync");
116 	while (start < end) {
117 		asm volatile("tlbie %0" :: "r" (start));
118 		asm volatile("eieio");
119 		asm volatile("sync");
120 		start += B_PAGE_SIZE;
121 	}
122 	asm volatile("tlbsync");
123 	asm volatile("sync");
124 }
125 
126 
127 void
128 arch_cpu_invalidate_TLB_list(addr_t pages[], int num_pages)
129 {
130 	int i;
131 
132 	asm volatile("sync");
133 	for (i = 0; i < num_pages; i++) {
134 		asm volatile("tlbie %0" :: "r" (pages[i]));
135 		asm volatile("eieio");
136 		asm volatile("sync");
137 	}
138 	asm volatile("tlbsync");
139 	asm volatile("sync");
140 }
141 
142 
143 void
144 arch_cpu_global_TLB_invalidate(void)
145 {
146 	if (sHasTlbia) {
147 		ppc_sync();
148 		tlbia();
149 		ppc_sync();
150 	} else {
151 		addr_t address = 0;
152 		unsigned long i;
153 
154 		ppc_sync();
155 		for (i = 0; i < 0x100000; i++) {
156 			tlbie(address);
157 			eieio();
158 			ppc_sync();
159 
160 			address += B_PAGE_SIZE;
161 		}
162 		tlbsync();
163 		ppc_sync();
164 	}
165 }
166 
167 
168 void
169 arch_cpu_user_TLB_invalidate(void)
170 {
171 	arch_cpu_global_TLB_invalidate();
172 }
173 
174 
175 status_t
176 arch_cpu_user_memcpy(void *to, const void *from, size_t size,
177 	addr_t *faultHandler)
178 {
179 	char *tmp = (char *)to;
180 	char *s = (char *)from;
181 	addr_t oldFaultHandler = *faultHandler;
182 
183 // TODO: This doesn't work correctly with gcc 4 anymore!
184 	if (ppc_set_fault_handler(faultHandler, (addr_t)&&error))
185 		goto error;
186 
187 	while (size--)
188 		*tmp++ = *s++;
189 
190 	*faultHandler = oldFaultHandler;
191 	return 0;
192 
193 error:
194 	*faultHandler = oldFaultHandler;
195 	return B_BAD_ADDRESS;
196 }
197 
198 
199 /**	\brief Copies at most (\a size - 1) characters from the string in \a from to
200  *	the string in \a to, NULL-terminating the result.
201  *
202  *	\param to Pointer to the destination C-string.
203  *	\param from Pointer to the source C-string.
204  *	\param size Size in bytes of the string buffer pointed to by \a to.
205  *
206  *	\return strlen(\a from).
207  */
208 
209 ssize_t
210 arch_cpu_user_strlcpy(char *to, const char *from, size_t size, addr_t *faultHandler)
211 {
212 	int from_length = 0;
213 	addr_t oldFaultHandler = *faultHandler;
214 
215 // TODO: This doesn't work correctly with gcc 4 anymore!
216 	if (ppc_set_fault_handler(faultHandler, (addr_t)&&error))
217 		goto error;
218 
219 	if (size > 0) {
220 		to[--size] = '\0';
221 		// copy
222 		for ( ; size; size--, from_length++, to++, from++) {
223 			if ((*to = *from) == '\0')
224 				break;
225 		}
226 	}
227 	// count any leftover from chars
228 	while (*from++ != '\0')
229 		from_length++;
230 
231 	*faultHandler = oldFaultHandler;
232 	return from_length;
233 
234 error:
235 	*faultHandler = oldFaultHandler;
236 	return B_BAD_ADDRESS;
237 }
238 
239 
240 status_t
241 arch_cpu_user_memset(void *s, char c, size_t count, addr_t *faultHandler)
242 {
243 	char *xs = (char *)s;
244 	addr_t oldFaultHandler = *faultHandler;
245 
246 // TODO: This doesn't work correctly with gcc 4 anymore!
247 	if (ppc_set_fault_handler(faultHandler, (addr_t)&&error))
248 		goto error;
249 
250 	while (count--)
251 		*xs++ = c;
252 
253 	*faultHandler = oldFaultHandler;
254 	return 0;
255 
256 error:
257 	*faultHandler = oldFaultHandler;
258 	return B_BAD_ADDRESS;
259 }
260 
261 
262 status_t
263 arch_cpu_shutdown(bool reboot)
264 {
265 	PPCPlatform::Default()->ShutDown(reboot);
266 	return B_ERROR;
267 }
268 
269 
270 void
271 arch_cpu_idle(void)
272 {
273 }
274 
275 
276 // The purpose of this function is to trick the compiler. When setting the
277 // page_handler to a label that is obviously (to the compiler) never used,
278 // it may reorganize the control flow, so that the labeled part is optimized
279 // away.
280 // By invoking the function like this
281 //
282 //	if (ppc_set_fault_handler(faultHandler, (addr_t)&&error))
283 //		goto error;
284 //
285 // the compiler has to keep the labeled code, since it can't guess the return
286 // value of this (non-inlinable) function. At least in my tests it worked that
287 // way, and I hope it will continue to work like this in the future.
288 //
289 bool
290 ppc_set_fault_handler(addr_t *handlerLocation, addr_t handler)
291 {
292 // TODO: This doesn't work correctly with gcc 4 anymore!
293 	*handlerLocation = handler;
294 	return false;
295 }
296