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