xref: /freebsd/sys/powerpc/powerpc/machdep.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
1 /*-
2  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3  * Copyright (C) 1995, 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by TooLs GmbH.
17  * 4. The name of TooLs GmbH may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (C) 2001 Benno Rice
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  *
44  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
45  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  *	$NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
55  */
56 
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59 
60 #include "opt_compat.h"
61 #include "opt_ddb.h"
62 #include "opt_kstack_pages.h"
63 #include "opt_platform.h"
64 
65 #include <sys/param.h>
66 #include <sys/proc.h>
67 #include <sys/systm.h>
68 #include <sys/bio.h>
69 #include <sys/buf.h>
70 #include <sys/bus.h>
71 #include <sys/cons.h>
72 #include <sys/cpu.h>
73 #include <sys/eventhandler.h>
74 #include <sys/exec.h>
75 #include <sys/imgact.h>
76 #include <sys/kdb.h>
77 #include <sys/kernel.h>
78 #include <sys/ktr.h>
79 #include <sys/linker.h>
80 #include <sys/lock.h>
81 #include <sys/malloc.h>
82 #include <sys/mbuf.h>
83 #include <sys/msgbuf.h>
84 #include <sys/mutex.h>
85 #include <sys/ptrace.h>
86 #include <sys/reboot.h>
87 #include <sys/rwlock.h>
88 #include <sys/signalvar.h>
89 #include <sys/syscallsubr.h>
90 #include <sys/sysctl.h>
91 #include <sys/sysent.h>
92 #include <sys/sysproto.h>
93 #include <sys/ucontext.h>
94 #include <sys/uio.h>
95 #include <sys/vmmeter.h>
96 #include <sys/vnode.h>
97 
98 #include <net/netisr.h>
99 
100 #include <vm/vm.h>
101 #include <vm/vm_extern.h>
102 #include <vm/vm_kern.h>
103 #include <vm/vm_page.h>
104 #include <vm/vm_map.h>
105 #include <vm/vm_object.h>
106 #include <vm/vm_pager.h>
107 
108 #include <machine/altivec.h>
109 #ifndef __powerpc64__
110 #include <machine/bat.h>
111 #endif
112 #include <machine/cpu.h>
113 #include <machine/elf.h>
114 #include <machine/fpu.h>
115 #include <machine/hid.h>
116 #include <machine/kdb.h>
117 #include <machine/md_var.h>
118 #include <machine/metadata.h>
119 #include <machine/mmuvar.h>
120 #include <machine/pcb.h>
121 #include <machine/reg.h>
122 #include <machine/sigframe.h>
123 #include <machine/spr.h>
124 #include <machine/trap.h>
125 #include <machine/vmparam.h>
126 #include <machine/ofw_machdep.h>
127 
128 #include <ddb/ddb.h>
129 
130 #include <dev/ofw/openfirm.h>
131 
132 int cold = 1;
133 #ifdef __powerpc64__
134 int cacheline_size = 128;
135 #else
136 int cacheline_size = 32;
137 #endif
138 int hw_direct_map = 1;
139 
140 extern void *ap_pcpu;
141 
142 struct pcpu __pcpu[MAXCPU];
143 
144 static struct trapframe frame0;
145 
146 char		machine[] = "powerpc";
147 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
148 
149 static void	cpu_startup(void *);
150 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
151 
152 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
153 	   CTLFLAG_RD, &cacheline_size, 0, "");
154 
155 uintptr_t	powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *);
156 
157 long		Maxmem = 0;
158 long		realmem = 0;
159 
160 struct kva_md_info kmi;
161 
162 static void
163 cpu_startup(void *dummy)
164 {
165 
166 	/*
167 	 * Initialise the decrementer-based clock.
168 	 */
169 	decr_init();
170 
171 	/*
172 	 * Good {morning,afternoon,evening,night}.
173 	 */
174 	cpu_setup(PCPU_GET(cpuid));
175 
176 #ifdef PERFMON
177 	perfmon_init();
178 #endif
179 	printf("real memory  = %ld (%ld MB)\n", ptoa(physmem),
180 	    ptoa(physmem) / 1048576);
181 	realmem = physmem;
182 
183 	if (bootverbose)
184 		printf("available KVA = %zd (%zd MB)\n",
185 		    virtual_end - virtual_avail,
186 		    (virtual_end - virtual_avail) / 1048576);
187 
188 	/*
189 	 * Display any holes after the first chunk of extended memory.
190 	 */
191 	if (bootverbose) {
192 		int indx;
193 
194 		printf("Physical memory chunk(s):\n");
195 		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
196 			vm_offset_t size1 =
197 			    phys_avail[indx + 1] - phys_avail[indx];
198 
199 			#ifdef __powerpc64__
200 			printf("0x%016lx - 0x%016lx, %ld bytes (%ld pages)\n",
201 			#else
202 			printf("0x%08x - 0x%08x, %d bytes (%ld pages)\n",
203 			#endif
204 			    phys_avail[indx], phys_avail[indx + 1] - 1, size1,
205 			    size1 / PAGE_SIZE);
206 		}
207 	}
208 
209 	vm_ksubmap_init(&kmi);
210 
211 	printf("avail memory = %ld (%ld MB)\n", ptoa(vm_cnt.v_free_count),
212 	    ptoa(vm_cnt.v_free_count) / 1048576);
213 
214 	/*
215 	 * Set up buffers, so they can be used to read disk labels.
216 	 */
217 	bufinit();
218 	vm_pager_bufferinit();
219 }
220 
221 extern vm_offset_t	__startkernel, __endkernel;
222 extern unsigned char	__bss_start[];
223 extern unsigned char	__sbss_start[];
224 extern unsigned char	__sbss_end[];
225 extern unsigned char	_end[];
226 
227 void aim_cpu_init(vm_offset_t toc);
228 void booke_cpu_init(void);
229 
230 uintptr_t
231 powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp)
232 {
233 	struct		pcpu *pc;
234 	vm_offset_t	startkernel, endkernel;
235 	void		*kmdp;
236         char		*env;
237 #ifdef DDB
238 	vm_offset_t ksym_start;
239 	vm_offset_t ksym_end;
240 #endif
241 
242 	kmdp = NULL;
243 
244 	/* First guess at start/end kernel positions */
245 	startkernel = __startkernel;
246 	endkernel = __endkernel;
247 
248 	/* Check for ePAPR loader, which puts a magic value into r6 */
249 	if (mdp == (void *)0x65504150)
250 		mdp = NULL;
251 
252 	/*
253 	 * Parse metadata if present and fetch parameters.  Must be done
254 	 * before console is inited so cninit gets the right value of
255 	 * boothowto.
256 	 */
257 	if (mdp != NULL) {
258 		preload_metadata = mdp;
259 		kmdp = preload_search_by_type("elf kernel");
260 		if (kmdp != NULL) {
261 			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
262 			kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
263 			endkernel = ulmax(endkernel, MD_FETCH(kmdp,
264 			    MODINFOMD_KERNEND, vm_offset_t));
265 #ifdef DDB
266 			ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
267 			ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
268 			db_fetch_ksymtab(ksym_start, ksym_end);
269 #endif
270 		}
271 	} else {
272 		bzero(__sbss_start, __sbss_end - __sbss_start);
273 		bzero(__bss_start, _end - __bss_start);
274 	}
275 #ifdef BOOKE
276 	tlb1_init();
277 #endif
278 
279 	/* Store boot environment state */
280 	OF_initial_setup((void *)fdt, NULL, (int (*)(void *))ofentry);
281 
282 	/*
283 	 * Init params/tunables that can be overridden by the loader
284 	 */
285 	init_param1();
286 
287 	/*
288 	 * Start initializing proc0 and thread0.
289 	 */
290 	proc_linkup0(&proc0, &thread0);
291 	thread0.td_frame = &frame0;
292 
293 	/*
294 	 * Set up per-cpu data.
295 	 */
296 	pc = __pcpu;
297 	pcpu_init(pc, 0, sizeof(struct pcpu));
298 	pc->pc_curthread = &thread0;
299 #ifdef __powerpc64__
300 	__asm __volatile("mr 13,%0" :: "r"(pc->pc_curthread));
301 #else
302 	__asm __volatile("mr 2,%0" :: "r"(pc->pc_curthread));
303 #endif
304 	pc->pc_cpuid = 0;
305 
306 	__asm __volatile("mtsprg 0, %0" :: "r"(pc));
307 
308 	/*
309 	 * Init mutexes, which we use heavily in PMAP
310 	 */
311 
312 	mutex_init();
313 
314 	/*
315 	 * Install the OF client interface
316 	 */
317 
318 	OF_bootstrap();
319 
320 	/*
321 	 * Initialize the console before printing anything.
322 	 */
323 	cninit();
324 
325 	/*
326 	 * Complain if there is no metadata.
327 	 */
328 	if (mdp == NULL || kmdp == NULL) {
329 		printf("powerpc_init: no loader metadata.\n");
330 	}
331 
332 	/*
333 	 * Init KDB
334 	 */
335 
336 	kdb_init();
337 
338 #ifdef AIM
339 	aim_cpu_init(toc);
340 #else /* BOOKE */
341 	booke_cpu_init();
342 
343 	/* Make sure the kernel icache is valid before we go too much further */
344 	__syncicache((caddr_t)startkernel, endkernel - startkernel);
345 #endif
346 
347 	/*
348 	 * Choose a platform module so we can get the physical memory map.
349 	 */
350 
351 	platform_probe_and_attach();
352 
353 	/*
354 	 * Bring up MMU
355 	 */
356 	pmap_bootstrap(startkernel, endkernel);
357 	mtmsr(PSL_KERNSET & ~PSL_EE);
358 
359 	/*
360 	 * Initialize params/tunables that are derived from memsize
361 	 */
362 	init_param2(physmem);
363 
364 	/*
365 	 * Grab booted kernel's name
366 	 */
367         env = kern_getenv("kernelname");
368         if (env != NULL) {
369 		strlcpy(kernelname, env, sizeof(kernelname));
370 		freeenv(env);
371 	}
372 
373 	/*
374 	 * Finish setting up thread0.
375 	 */
376 	thread0.td_pcb = (struct pcb *)
377 	    ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
378 	    sizeof(struct pcb)) & ~15UL);
379 	bzero((void *)thread0.td_pcb, sizeof(struct pcb));
380 	pc->pc_curpcb = thread0.td_pcb;
381 
382 	/* Initialise the message buffer. */
383 	msgbufinit(msgbufp, msgbufsize);
384 
385 #ifdef KDB
386 	if (boothowto & RB_KDB)
387 		kdb_enter(KDB_WHY_BOOTFLAGS,
388 		    "Boot flags requested debugger");
389 #endif
390 
391 	return (((uintptr_t)thread0.td_pcb -
392 	    (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
393 }
394 
395 void
396 bzero(void *buf, size_t len)
397 {
398 	caddr_t	p;
399 
400 	p = buf;
401 
402 	while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
403 		*p++ = 0;
404 		len--;
405 	}
406 
407 	while (len >= sizeof(u_long) * 8) {
408 		*(u_long*) p = 0;
409 		*((u_long*) p + 1) = 0;
410 		*((u_long*) p + 2) = 0;
411 		*((u_long*) p + 3) = 0;
412 		len -= sizeof(u_long) * 8;
413 		*((u_long*) p + 4) = 0;
414 		*((u_long*) p + 5) = 0;
415 		*((u_long*) p + 6) = 0;
416 		*((u_long*) p + 7) = 0;
417 		p += sizeof(u_long) * 8;
418 	}
419 
420 	while (len >= sizeof(u_long)) {
421 		*(u_long*) p = 0;
422 		len -= sizeof(u_long);
423 		p += sizeof(u_long);
424 	}
425 
426 	while (len) {
427 		*p++ = 0;
428 		len--;
429 	}
430 }
431 
432 /*
433  * Flush the D-cache for non-DMA I/O so that the I-cache can
434  * be made coherent later.
435  */
436 void
437 cpu_flush_dcache(void *ptr, size_t len)
438 {
439 	register_t addr, off;
440 
441 	/*
442 	 * Align the address to a cacheline and adjust the length
443 	 * accordingly. Then round the length to a multiple of the
444 	 * cacheline for easy looping.
445 	 */
446 	addr = (uintptr_t)ptr;
447 	off = addr & (cacheline_size - 1);
448 	addr -= off;
449 	len = (len + off + cacheline_size - 1) & ~(cacheline_size - 1);
450 
451 	while (len > 0) {
452 		__asm __volatile ("dcbf 0,%0" :: "r"(addr));
453 		__asm __volatile ("sync");
454 		addr += cacheline_size;
455 		len -= cacheline_size;
456 	}
457 }
458 
459 int
460 ptrace_set_pc(struct thread *td, unsigned long addr)
461 {
462 	struct trapframe *tf;
463 
464 	tf = td->td_frame;
465 	tf->srr0 = (register_t)addr;
466 
467 	return (0);
468 }
469 
470 void
471 spinlock_enter(void)
472 {
473 	struct thread *td;
474 	register_t msr;
475 
476 	td = curthread;
477 	if (td->td_md.md_spinlock_count == 0) {
478 		__asm __volatile("or 2,2,2"); /* Set high thread priority */
479 		msr = intr_disable();
480 		td->td_md.md_spinlock_count = 1;
481 		td->td_md.md_saved_msr = msr;
482 	} else
483 		td->td_md.md_spinlock_count++;
484 	critical_enter();
485 }
486 
487 void
488 spinlock_exit(void)
489 {
490 	struct thread *td;
491 	register_t msr;
492 
493 	td = curthread;
494 	critical_exit();
495 	msr = td->td_md.md_saved_msr;
496 	td->td_md.md_spinlock_count--;
497 	if (td->td_md.md_spinlock_count == 0) {
498 		intr_restore(msr);
499 		__asm __volatile("or 6,6,6"); /* Set normal thread priority */
500 	}
501 }
502 
503