xref: /freebsd/sys/powerpc/booke/booke_machdep.c (revision c1cdf6a42f0d951ba720688dfc6ce07608b02f6e)
1 /*-
2  * Copyright (C) 2006-2012 Semihalf
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
17  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 /*-
26  * Copyright (C) 2001 Benno Rice
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  *    notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  *    notice, this list of conditions and the following disclaimer in the
36  *    documentation and/or other materials provided with the distribution.
37  *
38  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
39  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
41  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
44  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
49  */
50 /*-
51  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
52  * Copyright (C) 1995, 1996 TooLs GmbH.
53  * All rights reserved.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  * 1. Redistributions of source code must retain the above copyright
59  *    notice, this list of conditions and the following disclaimer.
60  * 2. Redistributions in binary form must reproduce the above copyright
61  *    notice, this list of conditions and the following disclaimer in the
62  *    documentation and/or other materials provided with the distribution.
63  * 3. All advertising materials mentioning features or use of this software
64  *    must display the following acknowledgement:
65  *      This product includes software developed by TooLs GmbH.
66  * 4. The name of TooLs GmbH may not be used to endorse or promote products
67  *    derived from this software without specific prior written permission.
68  *
69  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
70  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
71  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
72  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
73  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
74  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
75  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
76  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
77  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
78  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79  */
80 
81 #include <sys/cdefs.h>
82 __FBSDID("$FreeBSD$");
83 
84 #include "opt_ddb.h"
85 #include "opt_hwpmc_hooks.h"
86 #include "opt_kstack_pages.h"
87 #include "opt_platform.h"
88 
89 #include <sys/cdefs.h>
90 #include <sys/types.h>
91 #include <sys/param.h>
92 #include <sys/proc.h>
93 #include <sys/systm.h>
94 #include <sys/time.h>
95 #include <sys/bio.h>
96 #include <sys/buf.h>
97 #include <sys/bus.h>
98 #include <sys/cons.h>
99 #include <sys/cpu.h>
100 #include <sys/kdb.h>
101 #include <sys/kernel.h>
102 #include <sys/lock.h>
103 #include <sys/mutex.h>
104 #include <sys/rwlock.h>
105 #include <sys/sysctl.h>
106 #include <sys/exec.h>
107 #include <sys/ktr.h>
108 #include <sys/syscallsubr.h>
109 #include <sys/sysproto.h>
110 #include <sys/signalvar.h>
111 #include <sys/sysent.h>
112 #include <sys/imgact.h>
113 #include <sys/msgbuf.h>
114 #include <sys/ptrace.h>
115 
116 #include <vm/vm.h>
117 #include <vm/pmap.h>
118 #include <vm/vm_page.h>
119 #include <vm/vm_object.h>
120 #include <vm/vm_pager.h>
121 
122 #include <machine/cpu.h>
123 #include <machine/kdb.h>
124 #include <machine/reg.h>
125 #include <machine/vmparam.h>
126 #include <machine/spr.h>
127 #include <machine/hid.h>
128 #include <machine/psl.h>
129 #include <machine/trap.h>
130 #include <machine/md_var.h>
131 #include <machine/mmuvar.h>
132 #include <machine/sigframe.h>
133 #include <machine/machdep.h>
134 #include <machine/metadata.h>
135 #include <machine/platform.h>
136 
137 #include <sys/linker.h>
138 #include <sys/reboot.h>
139 
140 #include <contrib/libfdt/libfdt.h>
141 #include <dev/fdt/fdt_common.h>
142 #include <dev/ofw/openfirm.h>
143 
144 #ifdef DDB
145 #include <ddb/ddb.h>
146 #endif
147 
148 #ifdef  DEBUG
149 #define debugf(fmt, args...) printf(fmt, ##args)
150 #else
151 #define debugf(fmt, args...)
152 #endif
153 
154 extern unsigned char _etext[];
155 extern unsigned char _edata[];
156 extern unsigned char __bss_start[];
157 extern unsigned char __sbss_start[];
158 extern unsigned char __sbss_end[];
159 extern unsigned char _end[];
160 extern vm_offset_t __endkernel;
161 
162 /*
163  * Bootinfo is passed to us by legacy loaders. Save the address of the
164  * structure to handle backward compatibility.
165  */
166 uint32_t *bootinfo;
167 
168 void print_kernel_section_addr(void);
169 void print_kenv(void);
170 uintptr_t booke_init(u_long, u_long);
171 void ivor_setup(void);
172 
173 extern void *interrupt_vector_base;
174 extern void *int_critical_input;
175 extern void *int_machine_check;
176 extern void *int_data_storage;
177 extern void *int_instr_storage;
178 extern void *int_external_input;
179 extern void *int_alignment;
180 extern void *int_fpu;
181 extern void *int_program;
182 extern void *int_syscall;
183 extern void *int_decrementer;
184 extern void *int_fixed_interval_timer;
185 extern void *int_watchdog;
186 extern void *int_data_tlb_error;
187 extern void *int_inst_tlb_error;
188 extern void *int_debug;
189 extern void *int_debug_ed;
190 extern void *int_vec;
191 extern void *int_vecast;
192 #ifdef __SPE__
193 extern void *int_spe_fpdata;
194 extern void *int_spe_fpround;
195 #endif
196 #ifdef HWPMC_HOOKS
197 extern void *int_performance_counter;
198 #endif
199 
200 #define SET_TRAP(ivor, handler) \
201 	KASSERT(((uintptr_t)(&handler) & ~0xffffUL) == \
202 	    ((uintptr_t)(&interrupt_vector_base) & ~0xffffUL), \
203 	    ("Handler " #handler " too far from interrupt vector base")); \
204 	mtspr(ivor, (uintptr_t)(&handler) & 0xffffUL);
205 
206 uintptr_t powerpc_init(vm_offset_t fdt, vm_offset_t, vm_offset_t, void *mdp,
207     uint32_t mdp_cookie);
208 void booke_cpu_init(void);
209 
210 void
211 booke_cpu_init(void)
212 {
213 
214 	cpu_features |= PPC_FEATURE_BOOKE;
215 
216 	psl_kernset = PSL_CE | PSL_ME | PSL_EE;
217 #ifdef __powerpc64__
218 	psl_kernset |= PSL_CM;
219 #endif
220 	psl_userset = psl_kernset | PSL_PR;
221 #ifdef __powerpc64__
222 	psl_userset32 = psl_userset & ~PSL_CM;
223 #endif
224 	psl_userstatic = ~(PSL_VEC | PSL_FP | PSL_FE0 | PSL_FE1);
225 
226 	pmap_mmu_install(MMU_TYPE_BOOKE, BUS_PROBE_GENERIC);
227 }
228 
229 void
230 ivor_setup(void)
231 {
232 
233 	mtspr(SPR_IVPR, ((uintptr_t)&interrupt_vector_base) & ~0xffffUL);
234 
235 	SET_TRAP(SPR_IVOR0, int_critical_input);
236 	SET_TRAP(SPR_IVOR1, int_machine_check);
237 	SET_TRAP(SPR_IVOR2, int_data_storage);
238 	SET_TRAP(SPR_IVOR3, int_instr_storage);
239 	SET_TRAP(SPR_IVOR4, int_external_input);
240 	SET_TRAP(SPR_IVOR5, int_alignment);
241 	SET_TRAP(SPR_IVOR6, int_program);
242 	SET_TRAP(SPR_IVOR8, int_syscall);
243 	SET_TRAP(SPR_IVOR10, int_decrementer);
244 	SET_TRAP(SPR_IVOR11, int_fixed_interval_timer);
245 	SET_TRAP(SPR_IVOR12, int_watchdog);
246 	SET_TRAP(SPR_IVOR13, int_data_tlb_error);
247 	SET_TRAP(SPR_IVOR14, int_inst_tlb_error);
248 	SET_TRAP(SPR_IVOR15, int_debug);
249 #ifdef HWPMC_HOOKS
250 	SET_TRAP(SPR_IVOR35, int_performance_counter);
251 #endif
252 	switch ((mfpvr() >> 16) & 0xffff) {
253 	case FSL_E6500:
254 		SET_TRAP(SPR_IVOR32, int_vec);
255 		SET_TRAP(SPR_IVOR33, int_vecast);
256 		/* FALLTHROUGH */
257 	case FSL_E500mc:
258 	case FSL_E5500:
259 		SET_TRAP(SPR_IVOR7, int_fpu);
260 		SET_TRAP(SPR_IVOR15, int_debug_ed);
261 		break;
262 	case FSL_E500v1:
263 	case FSL_E500v2:
264 		SET_TRAP(SPR_IVOR32, int_vec);
265 #ifdef __SPE__
266 		SET_TRAP(SPR_IVOR33, int_spe_fpdata);
267 		SET_TRAP(SPR_IVOR34, int_spe_fpround);
268 #endif
269 		break;
270 	}
271 
272 #ifdef __powerpc64__
273 	/* Set 64-bit interrupt mode. */
274 	mtspr(SPR_EPCR, mfspr(SPR_EPCR) | EPCR_ICM);
275 #endif
276 }
277 
278 static int
279 booke_check_for_fdt(uint32_t arg1, vm_offset_t *dtbp)
280 {
281 	void *ptr;
282 	int fdt_size;
283 
284 	if (arg1 % 8 != 0)
285 		return (-1);
286 
287 	ptr = (void *)pmap_early_io_map(arg1, PAGE_SIZE);
288 	if (fdt_check_header(ptr) != 0)
289 		return (-1);
290 
291 	/*
292 	 * Read FDT total size from the header of FDT.
293 	 * This for sure hits within first page which is
294 	 * already mapped.
295 	 */
296 	fdt_size = fdt_totalsize((void *)ptr);
297 
298 	/*
299 	 * Ok, arg1 points to FDT, so we need to map it in.
300 	 * First, unmap this page and then map FDT again with full size
301 	 */
302 	pmap_early_io_unmap((vm_offset_t)ptr, PAGE_SIZE);
303 	ptr = (void *)pmap_early_io_map(arg1, fdt_size);
304 	*dtbp = (vm_offset_t)ptr;
305 
306 	return (0);
307 }
308 
309 uintptr_t
310 booke_init(u_long arg1, u_long arg2)
311 {
312 	uintptr_t ret;
313 	void *mdp;
314 	vm_offset_t dtbp, end;
315 
316 	end = (uintptr_t)_end;
317 	dtbp = (vm_offset_t)NULL;
318 
319 	/* Set up TLB initially */
320 	bootinfo = NULL;
321 	bzero(__sbss_start, __sbss_end - __sbss_start);
322 	bzero(__bss_start, _end - __bss_start);
323 	tlb1_init();
324 
325 	/*
326 	 * Handle the various ways we can get loaded and started:
327 	 *  -	FreeBSD's loader passes the pointer to the metadata
328 	 *	in arg1, with arg2 undefined. arg1 has a value that's
329 	 *	relative to the kernel's link address (i.e. larger
330 	 *	than 0xc0000000).
331 	 *  -	Juniper's loader passes the metadata pointer in arg2
332 	 *	and sets arg1 to zero. This is to signal that the
333 	 *	loader maps the kernel and starts it at its link
334 	 *	address (unlike the FreeBSD loader).
335 	 *  -	U-Boot passes the standard argc and argv parameters
336 	 *	in arg1 and arg2 (resp). arg1 is between 1 and some
337 	 *	relatively small number, such as 64K. arg2 is the
338 	 *	physical address of the argv vector.
339 	 *  -   ePAPR loaders pass an FDT blob in r3 (arg1) and the magic hex
340 	 *      string 0x45504150 ('EPAP') in r6 (which has been lost by now).
341 	 *      r4 (arg2) is supposed to be set to zero, but is not always.
342 	 */
343 
344 	if (arg1 == 0)				/* Juniper loader */
345 		mdp = (void *)arg2;
346 	else if (booke_check_for_fdt(arg1, &dtbp) == 0) { /* ePAPR */
347 		end = roundup(end, 8);
348 		memmove((void *)end, (void *)dtbp, fdt_totalsize((void *)dtbp));
349 		dtbp = end;
350 		end += fdt_totalsize((void *)dtbp);
351 		__endkernel = end;
352 		mdp = NULL;
353 	} else if (arg1 > (uintptr_t)btext)	/* FreeBSD loader */
354 		mdp = (void *)arg1;
355 	else					/* U-Boot */
356 		mdp = NULL;
357 
358 	/* Default to 32 byte cache line size. */
359 	switch ((mfpvr()) >> 16) {
360 	case FSL_E500mc:
361 	case FSL_E5500:
362 	case FSL_E6500:
363 		cacheline_size = 64;
364 		break;
365 	}
366 
367 	/*
368 	 * Last element is a magic cookie that indicates that the metadata
369 	 * pointer is meaningful.
370 	 */
371 	ret = powerpc_init(dtbp, 0, 0, mdp, (mdp == NULL) ? 0 : 0xfb5d104d);
372 
373 	/* Enable caches */
374 	booke_enable_l1_cache();
375 	booke_enable_l2_cache();
376 
377 	booke_enable_bpred();
378 
379 	return (ret);
380 }
381 
382 #define RES_GRANULE cacheline_size
383 extern uintptr_t tlb0_miss_locks[];
384 
385 /* Initialise a struct pcpu. */
386 void
387 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
388 {
389 
390 	pcpu->pc_booke.tid_next = TID_MIN;
391 
392 #ifdef SMP
393 	uintptr_t *ptr;
394 	int words_per_gran = RES_GRANULE / sizeof(uintptr_t);
395 
396 	ptr = &tlb0_miss_locks[cpuid * words_per_gran];
397 	pcpu->pc_booke.tlb_lock = ptr;
398 	*ptr = TLB_UNLOCKED;
399 	*(ptr + 1) = 0;		/* recurse counter */
400 #endif
401 }
402 
403 /* Shutdown the CPU as much as possible. */
404 void
405 cpu_halt(void)
406 {
407 
408 	mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE));
409 	while (1)
410 		;
411 }
412 
413 int
414 ptrace_single_step(struct thread *td)
415 {
416 	struct trapframe *tf;
417 
418 	tf = td->td_frame;
419 	tf->srr1 |= PSL_DE;
420 	tf->cpu.booke.dbcr0 |= (DBCR0_IDM | DBCR0_IC);
421 	return (0);
422 }
423 
424 int
425 ptrace_clear_single_step(struct thread *td)
426 {
427 	struct trapframe *tf;
428 
429 	tf = td->td_frame;
430 	tf->srr1 &= ~PSL_DE;
431 	tf->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);
432 	return (0);
433 }
434 
435 void
436 kdb_cpu_clear_singlestep(void)
437 {
438 	register_t r;
439 
440 	r = mfspr(SPR_DBCR0);
441 	mtspr(SPR_DBCR0, r & ~DBCR0_IC);
442 	kdb_frame->srr1 &= ~PSL_DE;
443 }
444 
445 void
446 kdb_cpu_set_singlestep(void)
447 {
448 	register_t r;
449 
450 	r = mfspr(SPR_DBCR0);
451 	mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM);
452 	kdb_frame->srr1 |= PSL_DE;
453 }
454 
455