]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/machdep.c
nvi: import version 2.2.1
[FreeBSD/FreeBSD.git] / sys / arm / arm / machdep.c
1 /*      $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $        */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2004 Olivier Houchard
7  * Copyright (c) 1994-1998 Mark Brinicombe.
8  * Copyright (c) 1994 Brini.
9  * All rights reserved.
10  *
11  * This code is derived from software written for Brini by Mark Brinicombe
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by Mark Brinicombe
24  *      for the NetBSD Project.
25  * 4. The name of the company nor the name of the author may be used to
26  *    endorse or promote products derived from this software without specific
27  *    prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  * Machine dependent functions for kernel setup
42  *
43  * Created      : 17/09/94
44  * Updated      : 18/04/01 updated for new wscons
45  */
46
47 #include "opt_ddb.h"
48 #include "opt_kstack_pages.h"
49 #include "opt_platform.h"
50 #include "opt_sched.h"
51
52 #include <sys/cdefs.h>
53 #include <sys/param.h>
54 #include <sys/buf.h>
55 #include <sys/bus.h>
56 #include <sys/cons.h>
57 #include <sys/cpu.h>
58 #include <sys/devmap.h>
59 #include <sys/efi.h>
60 #include <sys/imgact.h>
61 #include <sys/kdb.h>
62 #include <sys/kernel.h>
63 #include <sys/ktr.h>
64 #include <sys/linker.h>
65 #include <sys/msgbuf.h>
66 #include <sys/physmem.h>
67 #include <sys/reboot.h>
68 #include <sys/rwlock.h>
69 #include <sys/sched.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysent.h>
72 #include <sys/sysproto.h>
73 #include <sys/vmmeter.h>
74
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78
79 #include <machine/asm.h>
80 #include <machine/debug_monitor.h>
81 #include <machine/machdep.h>
82 #include <machine/metadata.h>
83 #include <machine/pcb.h>
84 #include <machine/platform.h>
85 #include <machine/sysarch.h>
86 #include <machine/undefined.h>
87 #include <machine/vfp.h>
88 #include <machine/vmparam.h>
89
90 #ifdef FDT
91 #include <dev/fdt/fdt_common.h>
92 #include <machine/ofw_machdep.h>
93 #endif
94
95 #ifdef DEBUG
96 #define debugf(fmt, args...) printf(fmt, ##args)
97 #else
98 #define debugf(fmt, args...)
99 #endif
100
101 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
102     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) || \
103     defined(COMPAT_FREEBSD9)
104 #error FreeBSD/arm doesn't provide compatibility with releases prior to 10
105 #endif
106
107
108 #if __ARM_ARCH < 6
109 #error FreeBSD requires ARMv6 or later
110 #endif
111
112 struct pcpu __pcpu[MAXCPU];
113 struct pcpu *pcpup = &__pcpu[0];
114
115 static struct trapframe proc0_tf;
116 uint32_t cpu_reset_address = 0;
117 int cold = 1;
118 vm_offset_t vector_page;
119
120 /* The address at which the kernel was loaded.  Set early in initarm(). */
121 vm_paddr_t arm_physmem_kernaddr;
122
123 extern int *end;
124
125 #ifdef FDT
126 vm_paddr_t pmap_pa;
127 vm_offset_t systempage;
128 vm_offset_t irqstack;
129 vm_offset_t undstack;
130 vm_offset_t abtstack;
131 #endif /* FDT */
132
133 #ifdef PLATFORM
134 static delay_func *delay_impl;
135 static void *delay_arg;
136 #endif
137
138 struct kva_md_info kmi;
139 /*
140  * arm32_vector_init:
141  *
142  *      Initialize the vector page, and select whether or not to
143  *      relocate the vectors.
144  *
145  *      NOTE: We expect the vector page to be mapped at its expected
146  *      destination.
147  */
148
149 extern unsigned int page0[], page0_data[];
150 void
151 arm_vector_init(vm_offset_t va, int which)
152 {
153         unsigned int *vectors = (int *) va;
154         unsigned int *vectors_data = vectors + (page0_data - page0);
155         int vec;
156
157         /*
158          * Loop through the vectors we're taking over, and copy the
159          * vector's insn and data word.
160          */
161         for (vec = 0; vec < ARM_NVEC; vec++) {
162                 if ((which & (1 << vec)) == 0) {
163                         /* Don't want to take over this vector. */
164                         continue;
165                 }
166                 vectors[vec] = page0[vec];
167                 vectors_data[vec] = page0_data[vec];
168         }
169
170         /* Now sync the vectors. */
171         icache_sync(va, (ARM_NVEC * 2) * sizeof(u_int));
172
173         vector_page = va;
174 }
175
176 static void
177 cpu_startup(void *dummy)
178 {
179         struct pcb *pcb = thread0.td_pcb;
180         const unsigned int mbyte = 1024 * 1024;
181
182         identify_arm_cpu();
183
184         vm_ksubmap_init(&kmi);
185
186         /*
187          * Display the RAM layout.
188          */
189         printf("real memory  = %ju (%ju MB)\n",
190             (uintmax_t)arm32_ptob(realmem),
191             (uintmax_t)arm32_ptob(realmem) / mbyte);
192         printf("avail memory = %ju (%ju MB)\n",
193             (uintmax_t)arm32_ptob(vm_free_count()),
194             (uintmax_t)arm32_ptob(vm_free_count()) / mbyte);
195         if (bootverbose) {
196                 physmem_print_tables();
197                 devmap_print_table();
198         }
199
200         bufinit();
201         vm_pager_bufferinit();
202         pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack +
203             USPACE_SVC_STACK_TOP;
204         pmap_set_pcb_pagedir(kernel_pmap, pcb);
205 }
206
207 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
208
209 /*
210  * Flush the D-cache for non-DMA I/O so that the I-cache can
211  * be made coherent later.
212  */
213 void
214 cpu_flush_dcache(void *ptr, size_t len)
215 {
216
217         dcache_wb_poc((vm_offset_t)ptr, (vm_paddr_t)vtophys(ptr), len);
218 }
219
220 /* Get current clock frequency for the given cpu id. */
221 int
222 cpu_est_clockrate(int cpu_id, uint64_t *rate)
223 {
224         struct pcpu *pc;
225
226         pc = pcpu_find(cpu_id);
227         if (pc == NULL || rate == NULL)
228                 return (EINVAL);
229
230         if (pc->pc_clock == 0)
231                 return (EOPNOTSUPP);
232
233         *rate = pc->pc_clock;
234
235         return (0);
236 }
237
238 void
239 cpu_idle(int busy)
240 {
241
242         CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
243         spinlock_enter();
244         if (!busy)
245                 cpu_idleclock();
246         if (!sched_runnable())
247                 cpu_sleep(0);
248         if (!busy)
249                 cpu_activeclock();
250         spinlock_exit();
251         CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
252 }
253
254 int
255 cpu_idle_wakeup(int cpu)
256 {
257
258         return (0);
259 }
260
261 void
262 cpu_initclocks(void)
263 {
264
265 #ifdef SMP
266         if (PCPU_GET(cpuid) == 0)
267                 cpu_initclocks_bsp();
268         else
269                 cpu_initclocks_ap();
270 #else
271         cpu_initclocks_bsp();
272 #endif
273 }
274
275 #ifdef PLATFORM
276 void
277 arm_set_delay(delay_func *impl, void *arg)
278 {
279
280         KASSERT(impl != NULL, ("No DELAY implementation"));
281         delay_impl = impl;
282         delay_arg = arg;
283 }
284
285 void
286 DELAY(int usec)
287 {
288
289         TSENTER();
290         delay_impl(usec, delay_arg);
291         TSEXIT();
292 }
293 #endif
294
295 void
296 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
297 {
298
299         pcpu->pc_mpidr = 0xffffffff;
300 }
301
302 void
303 spinlock_enter(void)
304 {
305         struct thread *td;
306         register_t cspr;
307
308         td = curthread;
309         if (td->td_md.md_spinlock_count == 0) {
310                 cspr = disable_interrupts(PSR_I | PSR_F);
311                 td->td_md.md_spinlock_count = 1;
312                 td->td_md.md_saved_cspr = cspr;
313                 critical_enter();
314         } else
315                 td->td_md.md_spinlock_count++;
316 }
317
318 void
319 spinlock_exit(void)
320 {
321         struct thread *td;
322         register_t cspr;
323
324         td = curthread;
325         cspr = td->td_md.md_saved_cspr;
326         td->td_md.md_spinlock_count--;
327         if (td->td_md.md_spinlock_count == 0) {
328                 critical_exit();
329                 restore_interrupts(cspr);
330         }
331 }
332
333 /*
334  * Construct a PCB from a trapframe. This is called from kdb_trap() where
335  * we want to start a backtrace from the function that caused us to enter
336  * the debugger. We have the context in the trapframe, but base the trace
337  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
338  * enough for a backtrace.
339  */
340 void
341 makectx(struct trapframe *tf, struct pcb *pcb)
342 {
343         pcb->pcb_regs.sf_r4 = tf->tf_r4;
344         pcb->pcb_regs.sf_r5 = tf->tf_r5;
345         pcb->pcb_regs.sf_r6 = tf->tf_r6;
346         pcb->pcb_regs.sf_r7 = tf->tf_r7;
347         pcb->pcb_regs.sf_r8 = tf->tf_r8;
348         pcb->pcb_regs.sf_r9 = tf->tf_r9;
349         pcb->pcb_regs.sf_r10 = tf->tf_r10;
350         pcb->pcb_regs.sf_r11 = tf->tf_r11;
351         pcb->pcb_regs.sf_r12 = tf->tf_r12;
352         pcb->pcb_regs.sf_pc = tf->tf_pc;
353         pcb->pcb_regs.sf_lr = tf->tf_usr_lr;
354         pcb->pcb_regs.sf_sp = tf->tf_usr_sp;
355 }
356
357 void
358 pcpu0_init(void)
359 {
360         set_curthread(&thread0);
361         pcpu_init(pcpup, 0, sizeof(struct pcpu));
362         pcpup->pc_mpidr = cp15_mpidr_get() & 0xFFFFFF;
363         PCPU_SET(curthread, &thread0);
364 }
365
366 /*
367  * Initialize proc0
368  */
369 void
370 init_proc0(vm_offset_t kstack)
371 {
372         proc_linkup0(&proc0, &thread0);
373         thread0.td_kstack = kstack;
374         thread0.td_kstack_pages = kstack_pages;
375         thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
376             thread0.td_kstack_pages * PAGE_SIZE) - 1;
377         thread0.td_pcb->pcb_flags = 0;
378         thread0.td_pcb->pcb_fpflags = 0;
379         thread0.td_pcb->pcb_vfpcpu = -1;
380         thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN;
381         thread0.td_pcb->pcb_vfpsaved = &thread0.td_pcb->pcb_vfpstate;
382         thread0.td_frame = &proc0_tf;
383         pcpup->pc_curpcb = thread0.td_pcb;
384 }
385
386 void
387 set_stackptrs(int cpu)
388 {
389
390         set_stackptr(PSR_IRQ32_MODE,
391             irqstack + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
392         set_stackptr(PSR_ABT32_MODE,
393             abtstack + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
394         set_stackptr(PSR_UND32_MODE,
395             undstack + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
396 }
397
398 static void
399 arm_kdb_init(void)
400 {
401
402         kdb_init();
403 #ifdef KDB
404         if (boothowto & RB_KDB)
405                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
406 #endif
407 }
408
409 #ifdef FDT
410 void *
411 initarm(struct arm_boot_params *abp)
412 {
413         struct mem_region mem_regions[FDT_MEM_REGIONS];
414         vm_paddr_t lastaddr;
415         vm_offset_t dtbp, kernelstack, dpcpu;
416         char *env;
417         void *kmdp;
418         int err_devmap, mem_regions_sz;
419         phandle_t root;
420         char dts_version[255];
421 #ifdef EFI
422         struct efi_map_header *efihdr;
423 #endif
424
425         /* get last allocated physical address */
426         arm_physmem_kernaddr = abp->abp_physaddr;
427         lastaddr = parse_boot_param(abp) - KERNVIRTADDR + arm_physmem_kernaddr;
428
429         set_cpufuncs();
430         cpuinfo_init();
431
432         /*
433          * Find the dtb passed in by the boot loader.
434          */
435         kmdp = preload_search_by_type("elf kernel");
436         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
437 #if defined(FDT_DTB_STATIC)
438         /*
439          * In case the device tree blob was not retrieved (from metadata) try
440          * to use the statically embedded one.
441          */
442         if (dtbp == (vm_offset_t)NULL)
443                 dtbp = (vm_offset_t)&fdt_static_dtb;
444 #endif
445
446         if (OF_install(OFW_FDT, 0) == FALSE)
447                 panic("Cannot install FDT");
448
449         if (OF_init((void *)dtbp) != 0)
450                 panic("OF_init failed with the found device tree");
451
452 #if defined(LINUX_BOOT_ABI)
453         arm_parse_fdt_bootargs();
454 #endif
455
456 #ifdef EFI
457         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
458             MODINFO_METADATA | MODINFOMD_EFI_MAP);
459         if (efihdr != NULL) {
460                 arm_add_efi_map_entries(efihdr, mem_regions, &mem_regions_sz);
461         } else
462 #endif
463         {
464                 /* Grab physical memory regions information from device tree. */
465                 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,NULL) != 0)
466                         panic("Cannot get physical memory regions");
467         }
468         physmem_hardware_regions(mem_regions, mem_regions_sz);
469
470         /* Grab reserved memory regions information from device tree. */
471         if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
472                 physmem_exclude_regions(mem_regions, mem_regions_sz,
473                     EXFLAG_NODUMP | EXFLAG_NOALLOC);
474
475         /*
476          * Set TEX remapping registers.
477          * Setup kernel page tables and switch to kernel L1 page table.
478          */
479         pmap_set_tex();
480         pmap_bootstrap_prepare(lastaddr);
481
482         /*
483          * If EARLY_PRINTF support is enabled, we need to re-establish the
484          * mapping after pmap_bootstrap_prepare() switches to new page tables.
485          * Note that we can only do the remapping if the VA is outside the
486          * kernel, now that we have real virtual (not VA=PA) mappings in effect.
487          * Early printf does not work between the time pmap_set_tex() does
488          * cp15_prrr_set() and this code remaps the VA.
489          */
490 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE
491         pmap_preboot_map_attr(SOCDEV_PA, SOCDEV_VA, 1024 * 1024, 
492             VM_PROT_READ | VM_PROT_WRITE, VM_MEMATTR_DEVICE);
493 #endif
494
495         /*
496          * Now that proper page tables are installed, call cpu_setup() to enable
497          * instruction and data caches and other chip-specific features.
498          */
499         cpu_setup();
500
501         /* Platform-specific initialisation */
502         platform_probe_and_attach();
503         pcpu0_init();
504
505         /* Do basic tuning, hz etc */
506         init_param1();
507
508         /*
509          * Allocate a page for the system page mapped to 0xffff0000
510          * This page will just contain the system vectors and can be
511          * shared by all processes.
512          */
513         systempage = pmap_preboot_get_pages(1);
514
515         /* Map the vector page. */
516         pmap_preboot_map_pages(systempage, ARM_VECTORS_HIGH,  1);
517         if (virtual_end >= ARM_VECTORS_HIGH)
518                 virtual_end = ARM_VECTORS_HIGH - 1;
519
520         /* Allocate dynamic per-cpu area. */
521         dpcpu = pmap_preboot_get_vpages(DPCPU_SIZE / PAGE_SIZE);
522         dpcpu_init((void *)dpcpu, 0);
523
524         /* Allocate stacks for all modes */
525         irqstack    = pmap_preboot_get_vpages(IRQ_STACK_SIZE * MAXCPU);
526         abtstack    = pmap_preboot_get_vpages(ABT_STACK_SIZE * MAXCPU);
527         undstack    = pmap_preboot_get_vpages(UND_STACK_SIZE * MAXCPU );
528         kernelstack = pmap_preboot_get_vpages(kstack_pages);
529
530         /* Allocate message buffer. */
531         msgbufp = (void *)pmap_preboot_get_vpages(
532             round_page(msgbufsize) / PAGE_SIZE);
533
534         /*
535          * Pages were allocated during the secondary bootstrap for the
536          * stacks for different CPU modes.
537          * We must now set the r13 registers in the different CPU modes to
538          * point to these stacks.
539          * Since the ARM stacks use STMFD etc. we must set r13 to the top end
540          * of the stack memory.
541          */
542         set_stackptrs(0);
543         mutex_init();
544
545         /* Establish static device mappings. */
546         err_devmap = platform_devmap_init();
547         devmap_bootstrap(0, NULL);
548         vm_max_kernel_address = platform_lastaddr();
549
550         /*
551          * Only after the SOC registers block is mapped we can perform device
552          * tree fixups, as they may attempt to read parameters from hardware.
553          */
554         OF_interpret("perform-fixup", 0);
555         platform_gpio_init();
556         cninit();
557
558         /*
559          * If we made a mapping for EARLY_PRINTF after pmap_bootstrap_prepare(),
560          * undo it now that the normal console printf works.
561          */
562 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE
563         pmap_kremove(SOCDEV_VA);
564 #endif
565
566         debugf("initarm: console initialized\n");
567         debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
568         debugf(" boothowto = 0x%08x\n", boothowto);
569         debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
570         debugf(" lastaddr1: 0x%08x\n", lastaddr);
571         arm_print_kenv();
572
573         env = kern_getenv("kernelname");
574         if (env != NULL)
575                 strlcpy(kernelname, env, sizeof(kernelname));
576
577         if (err_devmap != 0)
578                 printf("WARNING: could not fully configure devmap, error=%d\n",
579                     err_devmap);
580
581         platform_late_init();
582
583         root = OF_finddevice("/");
584         if (OF_getprop(root, "freebsd,dts-version", dts_version, sizeof(dts_version)) > 0) {
585                 if (strcmp(LINUX_DTS_VERSION, dts_version) != 0)
586                         printf("WARNING: DTB version is %s while kernel expects %s, "
587                             "please update the DTB in the ESP\n",
588                             dts_version,
589                             LINUX_DTS_VERSION);
590         } else {
591                 printf("WARNING: Cannot find freebsd,dts-version property, "
592                     "cannot check DTB compliance\n");
593         }
594
595         /*
596          * We must now clean the cache again....
597          * Cleaning may be done by reading new data to displace any
598          * dirty data in the cache. This will have happened in cpu_setttb()
599          * but since we are boot strapping the addresses used for the read
600          * may have just been remapped and thus the cache could be out
601          * of sync. A re-clean after the switch will cure this.
602          * After booting there are no gross relocations of the kernel thus
603          * this problem will not occur after initarm().
604          */
605         /* Set stack for exception handlers */
606         undefined_init();
607         init_proc0(kernelstack);
608         arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
609         enable_interrupts(PSR_A);
610         pmap_bootstrap(0);
611
612         /* Exclude the kernel (and all the things we allocated which immediately
613          * follow the kernel) from the VM allocation pool but not from crash
614          * dumps.  virtual_avail is a global variable which tracks the kva we've
615          * "allocated" while setting up pmaps.
616          *
617          * Prepare the list of physical memory available to the vm subsystem.
618          */
619         physmem_exclude_region(abp->abp_physaddr,
620                 pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC);
621         physmem_init_kernel_globals();
622
623         init_param2(physmem);
624         /* Init message buffer. */
625         msgbufinit(msgbufp, msgbufsize);
626         dbg_monitor_init();
627         arm_kdb_init();
628         /* Apply possible BP hardening. */
629         cpuinfo_init_bp_hardening();
630         return ((void *)STACKALIGN(thread0.td_pcb));
631
632 }
633 #endif /* FDT */