]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/booke/machdep.c
Merge from vmobj-rwlock:
[FreeBSD/FreeBSD.git] / sys / powerpc / booke / machdep.c
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_compat.h"
85 #include "opt_ddb.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/sysctl.h>
105 #include <sys/exec.h>
106 #include <sys/ktr.h>
107 #include <sys/syscallsubr.h>
108 #include <sys/sysproto.h>
109 #include <sys/signalvar.h>
110 #include <sys/sysent.h>
111 #include <sys/imgact.h>
112 #include <sys/msgbuf.h>
113 #include <sys/ptrace.h>
114
115 #include <vm/vm.h>
116 #include <vm/pmap.h>
117 #include <vm/vm_page.h>
118 #include <vm/vm_object.h>
119 #include <vm/vm_pager.h>
120
121 #include <machine/cpu.h>
122 #include <machine/kdb.h>
123 #include <machine/reg.h>
124 #include <machine/vmparam.h>
125 #include <machine/spr.h>
126 #include <machine/hid.h>
127 #include <machine/psl.h>
128 #include <machine/trap.h>
129 #include <machine/md_var.h>
130 #include <machine/mmuvar.h>
131 #include <machine/sigframe.h>
132 #include <machine/machdep.h>
133 #include <machine/metadata.h>
134 #include <machine/platform.h>
135
136 #include <sys/linker.h>
137 #include <sys/reboot.h>
138
139 #include <dev/fdt/fdt_common.h>
140 #include <dev/ofw/openfirm.h>
141
142 #ifdef DDB
143 extern vm_offset_t ksym_start, ksym_end;
144 #endif
145
146 #ifdef  DEBUG
147 #define debugf(fmt, args...) printf(fmt, ##args)
148 #else
149 #define debugf(fmt, args...)
150 #endif
151
152 extern unsigned char kernel_text[];
153 extern unsigned char _etext[];
154 extern unsigned char _edata[];
155 extern unsigned char __bss_start[];
156 extern unsigned char __sbss_start[];
157 extern unsigned char __sbss_end[];
158 extern unsigned char _end[];
159
160 /*
161  * Bootinfo is passed to us by legacy loaders. Save the address of the
162  * structure to handle backward compatibility.
163  */
164 uint32_t *bootinfo;
165
166 struct kva_md_info kmi;
167 struct pcpu __pcpu[MAXCPU];
168 struct trapframe frame0;
169 int cold = 1;
170 long realmem = 0;
171 long Maxmem = 0;
172 char machine[] = "powerpc";
173 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
174
175 int cacheline_size = 32;
176
177 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
178            CTLFLAG_RD, &cacheline_size, 0, "");
179
180 int hw_direct_map = 0;
181
182 static void cpu_booke_startup(void *);
183 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_booke_startup, NULL);
184
185 void print_kernel_section_addr(void);
186 void print_kenv(void);
187 u_int booke_init(uint32_t, uint32_t);
188
189 extern int elf32_nxstack;
190
191 static void
192 cpu_booke_startup(void *dummy)
193 {
194         int indx, size;
195
196         /* Initialise the decrementer-based clock. */
197         decr_init();
198
199         /* Good {morning,afternoon,evening,night}. */
200         cpu_setup(PCPU_GET(cpuid));
201
202         printf("real memory  = %ld (%ld MB)\n", ptoa(physmem),
203             ptoa(physmem) / 1048576);
204         realmem = physmem;
205
206         /* Display any holes after the first chunk of extended memory. */
207         if (bootverbose) {
208                 printf("Physical memory chunk(s):\n");
209                 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
210                         size = phys_avail[indx + 1] - phys_avail[indx];
211
212                         printf("0x%08x - 0x%08x, %d bytes (%ld pages)\n",
213                             phys_avail[indx], phys_avail[indx + 1] - 1,
214                             size, size / PAGE_SIZE);
215                 }
216         }
217
218         vm_ksubmap_init(&kmi);
219
220         printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
221             ptoa(cnt.v_free_count) / 1048576);
222
223         /* Set up buffers, so they can be used to read disk labels. */
224         bufinit();
225         vm_pager_bufferinit();
226
227         /* Cpu supports execution permissions on the pages. */
228         elf32_nxstack = 1;
229 }
230
231 static char *
232 kenv_next(char *cp)
233 {
234
235         if (cp != NULL) {
236                 while (*cp != 0)
237                         cp++;
238                 cp++;
239                 if (*cp == 0)
240                         cp = NULL;
241         }
242         return (cp);
243 }
244
245 void
246 print_kenv(void)
247 {
248         int len;
249         char *cp;
250
251         debugf("loader passed (static) kenv:\n");
252         if (kern_envp == NULL) {
253                 debugf(" no env, null ptr\n");
254                 return;
255         }
256         debugf(" kern_envp = 0x%08x\n", (u_int32_t)kern_envp);
257
258         len = 0;
259         for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
260                 debugf(" %x %s\n", (u_int32_t)cp, cp);
261 }
262
263 void
264 print_kernel_section_addr(void)
265 {
266
267         debugf("kernel image addresses:\n");
268         debugf(" kernel_text    = 0x%08x\n", (uint32_t)kernel_text);
269         debugf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
270         debugf(" _edata         = 0x%08x\n", (uint32_t)_edata);
271         debugf(" __sbss_start   = 0x%08x\n", (uint32_t)__sbss_start);
272         debugf(" __sbss_end     = 0x%08x\n", (uint32_t)__sbss_end);
273         debugf(" __sbss_start   = 0x%08x\n", (uint32_t)__bss_start);
274         debugf(" _end           = 0x%08x\n", (uint32_t)_end);
275 }
276
277 u_int
278 booke_init(uint32_t arg1, uint32_t arg2)
279 {
280         struct pcpu *pc;
281         void *kmdp, *mdp;
282         vm_offset_t dtbp, end;
283
284         kmdp = NULL;
285
286         end = (uintptr_t)_end;
287         dtbp = (vm_offset_t)NULL;
288
289         /*
290          * Handle the various ways we can get loaded and started:
291          *  -   FreeBSD's loader passes the pointer to the metadata
292          *      in arg1, with arg2 undefined. arg1 has a value that's
293          *      relative to the kernel's link address (i.e. larger
294          *      than 0xc0000000).
295          *  -   Juniper's loader passes the metadata pointer in arg2
296          *      and sets arg1 to zero. This is to signal that the
297          *      loader maps the kernel and starts it at its link
298          *      address (unlike the FreeBSD loader).
299          *  -   U-Boot passes the standard argc and argv parameters
300          *      in arg1 and arg2 (resp). arg1 is between 1 and some
301          *      relatively small number, such as 64K. arg2 is the
302          *      physical address of the argv vector.
303          */
304         if (arg1 > (uintptr_t)kernel_text)      /* FreeBSD loader */
305                 mdp = (void *)arg1;
306         else if (arg1 == 0)                     /* Juniper loader */
307                 mdp = (void *)arg2;
308         else                                    /* U-Boot */
309                 mdp = NULL;
310
311         /*
312          * Parse metadata and fetch parameters.
313          */
314         if (mdp != NULL) {
315                 preload_metadata = mdp;
316                 kmdp = preload_search_by_type("elf kernel");
317                 if (kmdp != NULL) {
318                         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
319                         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
320                         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
321                         end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
322
323                         bootinfo = (uint32_t *)preload_search_info(kmdp,
324                             MODINFO_METADATA | MODINFOMD_BOOTINFO);
325
326 #ifdef DDB
327                         ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
328                         ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
329 #endif
330                 }
331         } else {
332                 bzero(__sbss_start, __sbss_end - __sbss_start);
333                 bzero(__bss_start, _end - __bss_start);
334         }
335
336 #if defined(FDT_DTB_STATIC)
337         /*
338          * In case the device tree blob was not retrieved (from metadata) try
339          * to use the statically embedded one.
340          */
341         if (dtbp == (vm_offset_t)NULL)
342                 dtbp = (vm_offset_t)&fdt_static_dtb;
343 #endif
344
345         if (OF_install(OFW_FDT, 0) == FALSE)
346                 while (1);
347
348         if (OF_init((void *)dtbp) != 0)
349                 while (1);
350
351         if (fdt_immr_addr(CCSRBAR_VA) != 0)
352                 while (1);
353
354         OF_interpret("perform-fixup", 0);
355         
356         /* Set up TLB initially */
357         booke_init_tlb(fdt_immr_pa);
358
359         /* Reset Time Base */
360         mttb(0);
361
362         /* Init params/tunables that can be overridden by the loader. */
363         init_param1();
364
365         /* Start initializing proc0 and thread0. */
366         proc_linkup0(&proc0, &thread0);
367         thread0.td_frame = &frame0;
368
369         /* Set up per-cpu data and store the pointer in SPR general 0. */
370         pc = &__pcpu[0];
371         pcpu_init(pc, 0, sizeof(struct pcpu));
372         pc->pc_curthread = &thread0;
373         __asm __volatile("mtsprg 0, %0" :: "r"(pc));
374
375         /* Initialize system mutexes. */
376         mutex_init();
377
378         /* Initialize the console before printing anything. */
379         cninit();
380
381         /* Print out some debug info... */
382         debugf("%s: console initialized\n", __func__);
383         debugf(" arg3 mdp = 0x%08x\n", (u_int32_t)mdp);
384         debugf(" end = 0x%08x\n", (u_int32_t)end);
385         debugf(" boothowto = 0x%08x\n", boothowto);
386         debugf(" kernel ccsrbar = 0x%08x\n", CCSRBAR_VA);
387         debugf(" MSR = 0x%08x\n", mfmsr());
388 #if defined(BOOKE_E500)
389         debugf(" HID0 = 0x%08x\n", mfspr(SPR_HID0));
390         debugf(" HID1 = 0x%08x\n", mfspr(SPR_HID1));
391         debugf(" BUCSR = 0x%08x\n", mfspr(SPR_BUCSR));
392 #endif
393
394         debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
395
396         print_kernel_section_addr();
397         print_kenv();
398 #if defined(BOOKE_E500)
399         //tlb1_print_entries();
400         //tlb1_print_tlbentries();
401 #endif
402
403         kdb_init();
404
405 #ifdef KDB
406         if (boothowto & RB_KDB)
407                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
408 #endif
409
410         /* Initialise platform module */
411         platform_probe_and_attach();
412
413         /* Initialise virtual memory. */
414         pmap_mmu_install(MMU_TYPE_BOOKE, 0);
415         pmap_bootstrap((uintptr_t)kernel_text, end);
416         pmap_bootstrapped = 1;
417         debugf("MSR = 0x%08x\n", mfmsr());
418 #if defined(BOOKE_E500)
419         //tlb1_print_entries();
420         //tlb1_print_tlbentries();
421 #endif
422
423         /* Initialize params/tunables that are derived from memsize. */
424         init_param2(physmem);
425
426         /* Finish setting up thread0. */
427         thread0.td_pcb = (struct pcb *)
428             ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
429             sizeof(struct pcb)) & ~15);
430         bzero((void *)thread0.td_pcb, sizeof(struct pcb));
431         pc->pc_curpcb = thread0.td_pcb;
432
433         /* Initialise the message buffer. */
434         msgbufinit(msgbufp, msgbufsize);
435
436         /* Enable Machine Check interrupt. */
437         mtmsr(mfmsr() | PSL_ME);
438         isync();
439
440         /* Enable L1 caches */
441         booke_enable_l1_cache();
442
443         debugf("%s: SP = 0x%08x\n", __func__,
444             ((uintptr_t)thread0.td_pcb - 16) & ~15);
445
446         return (((uintptr_t)thread0.td_pcb - 16) & ~15);
447 }
448
449 #define RES_GRANULE 32
450 extern uint32_t tlb0_miss_locks[];
451
452 /* Initialise a struct pcpu. */
453 void
454 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
455 {
456
457         pcpu->pc_tid_next = TID_MIN;
458
459 #ifdef SMP
460         uint32_t *ptr;
461         int words_per_gran = RES_GRANULE / sizeof(uint32_t);
462
463         ptr = &tlb0_miss_locks[cpuid * words_per_gran];
464         pcpu->pc_booke_tlb_lock = ptr;
465         *ptr = TLB_UNLOCKED;
466         *(ptr + 1) = 0;         /* recurse counter */
467 #endif
468 }
469
470 /*
471  * Flush the D-cache for non-DMA I/O so that the I-cache can
472  * be made coherent later.
473  */
474 void
475 cpu_flush_dcache(void *ptr, size_t len)
476 {
477         register_t addr, off;
478
479         /*
480          * Align the address to a cacheline and adjust the length
481          * accordingly. Then round the length to a multiple of the
482          * cacheline for easy looping.
483          */
484         addr = (uintptr_t)ptr;
485         off = addr & (cacheline_size - 1);
486         addr -= off;
487         len = (len + off + cacheline_size - 1) & ~(cacheline_size - 1);
488
489         while (len > 0) {
490                 __asm __volatile ("dcbf 0,%0" :: "r"(addr));
491                 __asm __volatile ("sync");
492                 addr += cacheline_size;
493                 len -= cacheline_size;
494         }
495 }
496
497 void
498 spinlock_enter(void)
499 {
500         struct thread *td;
501         register_t msr;
502
503         td = curthread;
504         if (td->td_md.md_spinlock_count == 0) {
505                 msr = intr_disable();
506                 td->td_md.md_spinlock_count = 1;
507                 td->td_md.md_saved_msr = msr;
508         } else
509                 td->td_md.md_spinlock_count++;
510         critical_enter();
511 }
512
513 void
514 spinlock_exit(void)
515 {
516         struct thread *td;
517         register_t msr;
518
519         td = curthread;
520         critical_exit();
521         msr = td->td_md.md_saved_msr;
522         td->td_md.md_spinlock_count--;
523         if (td->td_md.md_spinlock_count == 0)
524                 intr_restore(msr);
525 }
526
527 /* Shutdown the CPU as much as possible. */
528 void
529 cpu_halt(void)
530 {
531
532         mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE));
533         while (1)
534                 ;
535 }
536
537 int
538 ptrace_set_pc(struct thread *td, unsigned long addr)
539 {
540         struct trapframe *tf;
541
542         tf = td->td_frame;
543         tf->srr0 = (register_t)addr;
544
545         return (0);
546 }
547
548 int
549 ptrace_single_step(struct thread *td)
550 {
551         struct trapframe *tf;
552
553         tf = td->td_frame;
554         tf->srr1 |= PSL_DE;
555         tf->cpu.booke.dbcr0 |= (DBCR0_IDM | DBCR0_IC);
556         return (0);
557 }
558
559 int
560 ptrace_clear_single_step(struct thread *td)
561 {
562         struct trapframe *tf;
563
564         tf = td->td_frame;
565         tf->srr1 &= ~PSL_DE;
566         tf->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);
567         return (0);
568 }
569
570 void
571 kdb_cpu_clear_singlestep(void)
572 {
573         register_t r;
574
575         r = mfspr(SPR_DBCR0);
576         mtspr(SPR_DBCR0, r & ~DBCR0_IC);
577         kdb_frame->srr1 &= ~PSL_DE;
578 }
579
580 void
581 kdb_cpu_set_singlestep(void)
582 {
583         register_t r;
584
585         r = mfspr(SPR_DBCR0);
586         mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM);
587         kdb_frame->srr1 |= PSL_DE;
588 }
589
590 void
591 bzero(void *buf, size_t len)
592 {
593         caddr_t p;
594
595         p = buf;
596
597         while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
598                 *p++ = 0;
599                 len--;
600         }
601
602         while (len >= sizeof(u_long) * 8) {
603                 *(u_long*) p = 0;
604                 *((u_long*) p + 1) = 0;
605                 *((u_long*) p + 2) = 0;
606                 *((u_long*) p + 3) = 0;
607                 len -= sizeof(u_long) * 8;
608                 *((u_long*) p + 4) = 0;
609                 *((u_long*) p + 5) = 0;
610                 *((u_long*) p + 6) = 0;
611                 *((u_long*) p + 7) = 0;
612                 p += sizeof(u_long) * 8;
613         }
614
615         while (len >= sizeof(u_long)) {
616                 *(u_long*) p = 0;
617                 len -= sizeof(u_long);
618                 p += sizeof(u_long);
619         }
620
621         while (len) {
622                 *p++ = 0;
623                 len--;
624         }
625 }
626