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