xref: /freebsd/sys/powerpc/powerpc/machdep.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
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 #include <dev/ofw/ofw_subr.h>
132 
133 int cold = 1;
134 #ifdef __powerpc64__
135 int cacheline_size = 128;
136 #else
137 int cacheline_size = 32;
138 #endif
139 int hw_direct_map = 1;
140 
141 extern void *ap_pcpu;
142 
143 struct pcpu __pcpu[MAXCPU];
144 static char init_kenv[2048];
145 
146 static struct trapframe frame0;
147 
148 char		machine[] = "powerpc";
149 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
150 
151 static void	cpu_startup(void *);
152 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
153 
154 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
155 	   CTLFLAG_RD, &cacheline_size, 0, "");
156 
157 uintptr_t	powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *,
158 		    vm_offset_t);
159 
160 long		Maxmem = 0;
161 long		realmem = 0;
162 
163 struct kva_md_info kmi;
164 
165 static void
166 cpu_startup(void *dummy)
167 {
168 
169 	/*
170 	 * Initialise the decrementer-based clock.
171 	 */
172 	decr_init();
173 
174 	/*
175 	 * Good {morning,afternoon,evening,night}.
176 	 */
177 	cpu_setup(PCPU_GET(cpuid));
178 
179 #ifdef PERFMON
180 	perfmon_init();
181 #endif
182 	printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)physmem),
183 	    ptoa((uintmax_t)physmem) / 1048576);
184 	realmem = physmem;
185 
186 	if (bootverbose)
187 		printf("available KVA = %zu (%zu MB)\n",
188 		    virtual_end - virtual_avail,
189 		    (virtual_end - virtual_avail) / 1048576);
190 
191 	/*
192 	 * Display any holes after the first chunk of extended memory.
193 	 */
194 	if (bootverbose) {
195 		int indx;
196 
197 		printf("Physical memory chunk(s):\n");
198 		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
199 			vm_paddr_t size1 =
200 			    phys_avail[indx + 1] - phys_avail[indx];
201 
202 			#ifdef __powerpc64__
203 			printf("0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
204 			#else
205 			printf("0x%09jx - 0x%09jx, %ju bytes (%ju pages)\n",
206 			#endif
207 			    (uintmax_t)phys_avail[indx],
208 			    (uintmax_t)phys_avail[indx + 1] - 1,
209 			    (uintmax_t)size1, (uintmax_t)size1 / PAGE_SIZE);
210 		}
211 	}
212 
213 	vm_ksubmap_init(&kmi);
214 
215 	printf("avail memory = %ju (%ju MB)\n",
216 	    ptoa((uintmax_t)vm_cnt.v_free_count),
217 	    ptoa((uintmax_t)vm_cnt.v_free_count) / 1048576);
218 
219 	/*
220 	 * Set up buffers, so they can be used to read disk labels.
221 	 */
222 	bufinit();
223 	vm_pager_bufferinit();
224 }
225 
226 extern vm_offset_t	__startkernel, __endkernel;
227 extern unsigned char	__bss_start[];
228 extern unsigned char	__sbss_start[];
229 extern unsigned char	__sbss_end[];
230 extern unsigned char	_end[];
231 
232 void aim_cpu_init(vm_offset_t toc);
233 void booke_cpu_init(void);
234 
235 uintptr_t
236 powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp,
237     vm_offset_t mdp_cookie)
238 {
239 	struct		pcpu *pc;
240 	struct cpuref	bsp;
241 	vm_offset_t	startkernel, endkernel;
242 	void		*kmdp;
243 	char		*env;
244         bool		ofw_bootargs = false;
245 #ifdef DDB
246 	vm_offset_t ksym_start;
247 	vm_offset_t ksym_end;
248 #endif
249 
250 	kmdp = NULL;
251 
252 	/* First guess at start/end kernel positions */
253 	startkernel = __startkernel;
254 	endkernel = __endkernel;
255 
256 	/*
257 	 * If the metadata pointer cookie is not set to the magic value,
258 	 * the number in mdp should be treated as nonsense.
259 	 */
260 	if (mdp_cookie != 0xfb5d104d)
261 		mdp = NULL;
262 
263 #ifdef AIM
264 	/*
265 	 * If running from an FDT, make sure we are in real mode to avoid
266 	 * tromping on firmware page tables. Everything in the kernel assumes
267 	 * 1:1 mappings out of firmware, so this won't break anything not
268 	 * already broken. This doesn't work if there is live OF, since OF
269 	 * may internally use non-1:1 mappings.
270 	 */
271 	if (ofentry == 0)
272 		mtmsr(mfmsr() & ~(PSL_IR | PSL_DR));
273 #endif
274 
275 	/*
276 	 * Parse metadata if present and fetch parameters.  Must be done
277 	 * before console is inited so cninit gets the right value of
278 	 * boothowto.
279 	 */
280 	if (mdp != NULL) {
281 		preload_metadata = mdp;
282 		kmdp = preload_search_by_type("elf kernel");
283 		if (kmdp != NULL) {
284 			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
285 			init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *),
286 			    0);
287 			endkernel = ulmax(endkernel, MD_FETCH(kmdp,
288 			    MODINFOMD_KERNEND, vm_offset_t));
289 #ifdef DDB
290 			ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
291 			ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
292 			db_fetch_ksymtab(ksym_start, ksym_end);
293 #endif
294 		}
295 	} else {
296 #if !defined(BOOKE)
297 		/*
298 		 * On BOOKE the BSS is already cleared and some variables
299 		 * initialized.  Do not wipe them out.
300 		 */
301 		bzero(__sbss_start, __sbss_end - __sbss_start);
302 		bzero(__bss_start, _end - __bss_start);
303 #endif
304 		init_static_kenv(init_kenv, sizeof(init_kenv));
305 		ofw_bootargs = true;
306 	}
307 	/* Store boot environment state */
308 	OF_initial_setup((void *)fdt, NULL, (int (*)(void *))ofentry);
309 
310 	/*
311 	 * Init params/tunables that can be overridden by the loader
312 	 */
313 	init_param1();
314 
315 	/*
316 	 * Start initializing proc0 and thread0.
317 	 */
318 	proc_linkup0(&proc0, &thread0);
319 	thread0.td_frame = &frame0;
320 #ifdef __powerpc64__
321 	__asm __volatile("mr 13,%0" :: "r"(&thread0));
322 #else
323 	__asm __volatile("mr 2,%0" :: "r"(&thread0));
324 #endif
325 
326 	/*
327 	 * Init mutexes, which we use heavily in PMAP
328 	 */
329 	mutex_init();
330 
331 	/*
332 	 * Install the OF client interface
333 	 */
334 	OF_bootstrap();
335 
336 	if (ofw_bootargs)
337 		ofw_parse_bootargs();
338 
339 	/*
340 	 * Initialize the console before printing anything.
341 	 */
342 	cninit();
343 
344 #ifdef AIM
345 	aim_cpu_init(toc);
346 #else /* BOOKE */
347 	booke_cpu_init();
348 
349 	/* Make sure the kernel icache is valid before we go too much further */
350 	__syncicache((caddr_t)startkernel, endkernel - startkernel);
351 #endif
352 
353 	/*
354 	 * Choose a platform module so we can get the physical memory map.
355 	 */
356 
357 	platform_probe_and_attach();
358 
359 	/*
360 	 * Set up per-cpu data for the BSP now that the platform can tell
361 	 * us which that is.
362 	 */
363 	if (platform_smp_get_bsp(&bsp) != 0)
364 		bsp.cr_cpuid = 0;
365 	pc = &__pcpu[bsp.cr_cpuid];
366 	pcpu_init(pc, bsp.cr_cpuid, sizeof(struct pcpu));
367 	pc->pc_curthread = &thread0;
368 	thread0.td_oncpu = bsp.cr_cpuid;
369 	pc->pc_cpuid = bsp.cr_cpuid;
370 	pc->pc_hwref = bsp.cr_hwref;
371 	pc->pc_pir = mfspr(SPR_PIR);
372 	__asm __volatile("mtsprg 0, %0" :: "r"(pc));
373 
374 	/*
375 	 * Init KDB
376 	 */
377 	kdb_init();
378 
379 	/*
380 	 * Bring up MMU
381 	 */
382 	pmap_bootstrap(startkernel, endkernel);
383 	mtmsr(PSL_KERNSET & ~PSL_EE);
384 
385 	/*
386 	 * Initialize params/tunables that are derived from memsize
387 	 */
388 	init_param2(physmem);
389 
390 	/*
391 	 * Grab booted kernel's name
392 	 */
393         env = kern_getenv("kernelname");
394         if (env != NULL) {
395 		strlcpy(kernelname, env, sizeof(kernelname));
396 		freeenv(env);
397 	}
398 
399 	/*
400 	 * Finish setting up thread0.
401 	 */
402 	thread0.td_pcb = (struct pcb *)
403 	    ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
404 	    sizeof(struct pcb)) & ~15UL);
405 	bzero((void *)thread0.td_pcb, sizeof(struct pcb));
406 	pc->pc_curpcb = thread0.td_pcb;
407 
408 	/* Initialise the message buffer. */
409 	msgbufinit(msgbufp, msgbufsize);
410 
411 #ifdef KDB
412 	if (boothowto & RB_KDB)
413 		kdb_enter(KDB_WHY_BOOTFLAGS,
414 		    "Boot flags requested debugger");
415 #endif
416 
417 	return (((uintptr_t)thread0.td_pcb -
418 	    (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
419 }
420 
421 /*
422  * Flush the D-cache for non-DMA I/O so that the I-cache can
423  * be made coherent later.
424  */
425 void
426 cpu_flush_dcache(void *ptr, size_t len)
427 {
428 	register_t addr, off;
429 
430 	/*
431 	 * Align the address to a cacheline and adjust the length
432 	 * accordingly. Then round the length to a multiple of the
433 	 * cacheline for easy looping.
434 	 */
435 	addr = (uintptr_t)ptr;
436 	off = addr & (cacheline_size - 1);
437 	addr -= off;
438 	len = roundup2(len + off, cacheline_size);
439 
440 	while (len > 0) {
441 		__asm __volatile ("dcbf 0,%0" :: "r"(addr));
442 		__asm __volatile ("sync");
443 		addr += cacheline_size;
444 		len -= cacheline_size;
445 	}
446 }
447 
448 int
449 ptrace_set_pc(struct thread *td, unsigned long addr)
450 {
451 	struct trapframe *tf;
452 
453 	tf = td->td_frame;
454 	tf->srr0 = (register_t)addr;
455 
456 	return (0);
457 }
458 
459 void
460 spinlock_enter(void)
461 {
462 	struct thread *td;
463 	register_t msr;
464 
465 	td = curthread;
466 	if (td->td_md.md_spinlock_count == 0) {
467 		__asm __volatile("or 2,2,2"); /* Set high thread priority */
468 		msr = intr_disable();
469 		td->td_md.md_spinlock_count = 1;
470 		td->td_md.md_saved_msr = msr;
471 	} else
472 		td->td_md.md_spinlock_count++;
473 	critical_enter();
474 }
475 
476 void
477 spinlock_exit(void)
478 {
479 	struct thread *td;
480 	register_t msr;
481 
482 	td = curthread;
483 	critical_exit();
484 	msr = td->td_md.md_saved_msr;
485 	td->td_md.md_spinlock_count--;
486 	if (td->td_md.md_spinlock_count == 0) {
487 		intr_restore(msr);
488 		__asm __volatile("or 6,6,6"); /* Set normal thread priority */
489 	}
490 }
491 
492 /*
493  * Simple ddb(4) command/hack to view any SPR on the running CPU.
494  * Uses a trivial asm function to perform the mfspr, and rewrites the mfspr
495  * instruction each time.
496  * XXX: Since it uses code modification, it won't work if the kernel code pages
497  * are marked RO.
498  */
499 extern register_t get_spr(int);
500 
501 #ifdef DDB
502 DB_SHOW_COMMAND(spr, db_show_spr)
503 {
504 	register_t spr;
505 	volatile uint32_t *p;
506 	int sprno, saved_sprno;
507 
508 	if (!have_addr)
509 		return;
510 
511 	saved_sprno = sprno = (intptr_t) addr;
512 	sprno = ((sprno & 0x3e0) >> 5) | ((sprno & 0x1f) << 5);
513 	p = (uint32_t *)(void *)&get_spr;
514 	*p = (*p & ~0x001ff800) | (sprno << 11);
515 	__syncicache(get_spr, cacheline_size);
516 	spr = get_spr(sprno);
517 
518 	db_printf("SPR %d(%x): %lx\n", saved_sprno, saved_sprno,
519 	    (unsigned long)spr);
520 }
521 #endif
522 
523 #undef bzero
524 void
525 bzero(void *buf, size_t len)
526 {
527 	caddr_t	p;
528 
529 	p = buf;
530 
531 	while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
532 		*p++ = 0;
533 		len--;
534 	}
535 
536 	while (len >= sizeof(u_long) * 8) {
537 		*(u_long*) p = 0;
538 		*((u_long*) p + 1) = 0;
539 		*((u_long*) p + 2) = 0;
540 		*((u_long*) p + 3) = 0;
541 		len -= sizeof(u_long) * 8;
542 		*((u_long*) p + 4) = 0;
543 		*((u_long*) p + 5) = 0;
544 		*((u_long*) p + 6) = 0;
545 		*((u_long*) p + 7) = 0;
546 		p += sizeof(u_long) * 8;
547 	}
548 
549 	while (len >= sizeof(u_long)) {
550 		*(u_long*) p = 0;
551 		len -= sizeof(u_long);
552 		p += sizeof(u_long);
553 	}
554 
555 	while (len) {
556 		*p++ = 0;
557 		len--;
558 	}
559 }
560