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