xref: /netbsd-src/sys/arch/acorn32/acorn32/rpc_machdep.c (revision a5c1e39951b475e3cf5e3a5a06725567a896cda8)
1 /*	$NetBSD: rpc_machdep.c,v 1.101 2022/05/15 20:37:50 andvar Exp $	*/
2 
3 /*
4  * Copyright (c) 2000-2002 Reinoud Zandijk.
5  * Copyright (c) 1994-1998 Mark Brinicombe.
6  * Copyright (c) 1994 Brini.
7  * All rights reserved.
8  *
9  * This code is derived from software written for Brini by Mark Brinicombe
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Brini.
22  * 4. The name of the company nor the name of the author may be used to
23  *    endorse or promote products derived from this software without specific
24  *    prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * RiscBSD kernel project
39  *
40  * machdep.c
41  *
42  * Machine dependent functions for kernel setup
43  *
44  * This file still needs a lot of work
45  *
46  * Created      : 17/09/94
47  * Updated for yet another new bootloader 28/12/02
48  */
49 
50 #include "opt_ddb.h"
51 #include "opt_modular.h"
52 #include "vidcvideo.h"
53 #include "podulebus.h"
54 
55 #include <sys/param.h>
56 
57 __KERNEL_RCSID(0, "$NetBSD: rpc_machdep.c,v 1.101 2022/05/15 20:37:50 andvar Exp $");
58 
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/reboot.h>
62 #include <sys/proc.h>
63 #include <sys/msgbuf.h>
64 #include <sys/exec.h>
65 #include <sys/exec_aout.h>
66 #include <sys/ksyms.h>
67 #include <sys/bus.h>
68 #include <sys/cpu.h>
69 #include <sys/intr.h>
70 #include <sys/device.h>
71 
72 #include <dev/cons.h>
73 
74 #include <dev/ic/pckbcvar.h>
75 
76 #include <dev/i2c/i2cvar.h>
77 #include <dev/i2c/pcf8583var.h>
78 
79 #include <machine/db_machdep.h>
80 #include <ddb/db_sym.h>
81 #include <ddb/db_extern.h>
82 
83 #include <uvm/uvm.h>
84 
85 #include <arm/locore.h>
86 #include <arm/undefined.h>
87 #include <arm/arm32/machdep.h>
88 #include <arm/arm32/pmap.h>
89 
90 #include <machine/rtc.h>
91 #include <machine/signal.h>
92 #include <machine/bootconfig.h>
93 #include <machine/io.h>
94 
95 #include <arm/iomd/vidc.h>
96 #include <arm/iomd/iomdreg.h>
97 #include <arm/iomd/iomdvar.h>
98 #include <arm/iomd/vidcvideo.h>
99 #include <arm/iomd/iomdiicvar.h>
100 
101 static i2c_tag_t acorn32_i2c_tag;
102 
103 #include "ksyms.h"
104 
105 /* Kernel text starts at the base of the kernel address space. */
106 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00000000)
107 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
108 
109 /*
110  * The range 0xf1000000 - 0xf5ffffff is available for kernel VM space
111  * Fixed mappings exist from 0xf6000000 - 0xffffffff
112  */
113 #define	KERNEL_VM_SIZE		0x05000000
114 
115 struct bootconfig bootconfig;	/* Boot config storage */
116 videomemory_t videomemory;	/* Video memory descriptor */
117 
118 char *boot_args = NULL;		/* holds the pre-processed boot arguments */
119 extern char *booted_kernel;	/* used for ioctl to retrieve booted kernel */
120 
121 extern int       *vidc_base;
122 extern uint32_t  iomd_base;
123 extern struct bus_space iomd_bs_tag;
124 
125 paddr_t physical_start;
126 paddr_t kernel_start;
127 paddr_t physical_freestart;
128 paddr_t physical_freeend;
129 paddr_t physical_end;
130 paddr_t dma_range_begin;
131 paddr_t dma_range_end;
132 
133 u_int free_pages;
134 paddr_t memoryblock_end;
135 
136 #ifndef PMAP_STATIC_L1S
137 int max_processes = 64;		/* Default number */
138 #endif	/* !PMAP_STATIC_L1S */
139 
140 u_int videodram_size = 0;	/* Amount of DRAM to reserve for video */
141 
142 paddr_t msgbufphys;
143 
144 #define	KERNEL_PT_VMEM		0 /* Page table for mapping video memory */
145 #define	KERNEL_PT_SYS		1 /* Page table for mapping proc0 zero page */
146 #define	KERNEL_PT_KERNEL	2 /* Page table for mapping kernel 0-4MB*/
147 #define	KERNEL_PT_KERNEL_4MB	3 /* Page table for mapping kernel 4-8MB*/
148 #define	KERNEL_PT_VMDATA	4 /* Page tables for mapping kernel VM */
149 #define	KERNEL_PT_VMDATA_NUM	4 /* start with 16MB of KVM */
150 #define	NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
151 
152 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
153 
154 #ifdef CPU_SA110
155 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
156 static vaddr_t sa110_cc_base;
157 #endif	/* CPU_SA110 */
158 
159 /* Prototypes */
160 void physcon_display_base(u_int);
161 extern void consinit(void);
162 
163 void data_abort_handler(trapframe_t *);
164 void prefetch_abort_handler(trapframe_t *);
165 void undefinedinstruction_bounce(trapframe_t *frame);
166 
167 static void canonicalise_bootconfig(struct bootconfig *, struct bootconfig *);
168 static void process_kernel_args(void);
169 
170 extern void dump_spl_masks(void);
171 
172 void rpc_sa110_cc_setup(void);
173 
174 void parse_rpc_bootargs(char *args);
175 
176 extern void dumpsys(void);
177 
178 
179 #	define console_flush()		/* empty */
180 
181 
182 #define panic2(a) do {							\
183 	memset((void *) (videomemory.vidm_vbase), 0x55, 50*1024);	\
184 	consinit();							\
185 	panic a;							\
186 } while (/* CONSTCOND */ 0)
187 
188 /*
189  * void cpu_reboot(int howto, char *bootstr)
190  *
191  * Reboots the system
192  *
193  * Deal with any syncing, unmounting, dumping and shutdown hooks,
194  * then reset the CPU.
195  */
196 
197 /* NOTE: These variables will be removed, well some of them */
198 
199 extern u_int current_mask;
200 
201 void
cpu_reboot(int howto,char * bootstr)202 cpu_reboot(int howto, char *bootstr)
203 {
204 
205 #ifdef DIAGNOSTIC
206 	printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
207 
208 	printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n",
209 	    irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
210 	    irqmasks[IPL_VM]);
211 	printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n",
212 	    irqmasks[IPL_AUDIO], irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);
213 
214 	dump_spl_masks();
215 #endif	/* DIAGNOSTIC */
216 
217 	/*
218 	 * If we are still cold then hit the air brakes
219 	 * and crash to earth fast
220 	 */
221 	if (cold) {
222 		doshutdownhooks();
223 		pmf_system_shutdown(boothowto);
224 		printf("Halted while still in the ICE age.\n");
225 		printf("The operating system has halted.\n");
226 		printf("Please press any key to reboot.\n\n");
227 		cngetc();
228 		printf("rebooting...\n");
229 		cpu_reset();
230 		/*NOTREACHED*/
231 	}
232 
233 	/* Disable console buffering */
234 	cnpollc(1);
235 
236 	/*
237 	 * If RB_NOSYNC was not specified sync the discs.
238 	 * Note: Unless cold is set to 1 here, syslogd will die during
239 	 * the unmount.  It looks like syslogd is getting woken up
240 	 * only to find that it cannot page part of the binary in as
241 	 * the filesystem has been unmounted.
242 	 */
243 	if (!(howto & RB_NOSYNC))
244 		bootsync();
245 
246 	/* Say NO to interrupts */
247 	splhigh();
248 
249 	/* Do a dump if requested. */
250 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
251 		dumpsys();
252 
253 	/*
254 	 * Auto reboot overload protection
255 	 *
256 	 * This code stops the kernel entering an endless loop of reboot
257 	 * - panic cycles. This will have the effect of stopping further
258 	 * reboots after it has rebooted 8 times after panics. A clean
259 	 * halt or reboot will reset the counter.
260 	 */
261 
262 	/*
263 	 * Have we done 8 reboots in a row ? If so halt rather than reboot
264 	 * since 8 panics in a row without 1 clean halt means something is
265 	 * seriously wrong.
266 	 */
267 	if (cmos_read(RTC_ADDR_REBOOTCNT) > 8)
268 		howto |= RB_HALT;
269 
270 	/*
271 	 * If we are rebooting on a panic then up the reboot count
272 	 * otherwise reset.
273 	 * This will thus be reset if the kernel changes the boot action from
274 	 * reboot to halt due to too any reboots.
275 	 */
276 	if (((howto & RB_HALT) == 0) && panicstr)
277 		cmos_write(RTC_ADDR_REBOOTCNT,
278 		   cmos_read(RTC_ADDR_REBOOTCNT) + 1);
279 	else
280 		cmos_write(RTC_ADDR_REBOOTCNT, 0);
281 
282 	/*
283 	 * If we need a RiscBSD reboot, request it buy setting a bit in
284 	 * the CMOS RAM. This can be detected by the RiscBSD boot loader
285 	 * during a RISCOS boot. No other way to do this as RISCOS is in ROM.
286 	 */
287 	if ((howto & RB_HALT) == 0)
288 		cmos_write(RTC_ADDR_BOOTOPTS,
289 		    cmos_read(RTC_ADDR_BOOTOPTS) | 0x02);
290 
291 	/* Run any shutdown hooks */
292 	doshutdownhooks();
293 
294 	pmf_system_shutdown(boothowto);
295 
296 	/* Make sure IRQ's are disabled */
297 	IRQdisable;
298 
299 	if (howto & RB_HALT) {
300 		printf("The operating system has halted.\n");
301 		printf("Please press any key to reboot.\n\n");
302 		cngetc();
303 	}
304 
305 	printf("rebooting...\n");
306 	cpu_reset();
307 	/*NOTREACHED*/
308 }
309 
310 
311 /*
312  * u_int initarm(BootConfig *bootconf)
313  *
314  * Initial entry point on startup. This gets called before main() is
315  * entered.
316  * It should be responsible for setting up everything that must be
317  * in place when main is called.
318  * This includes
319  *   Taking a copy of the boot configuration structure.
320  *   Initialising the physical console so characters can be printed.
321  *   Setting up page tables for the kernel
322  *   Relocating the kernel to the bottom of physical memory
323  */
324 
325 /*
326  * this part is completely rewritten for the new bootloader ... It features
327  * a flat memory map with a mapping comparable to the EBSA arm32 machine
328  * to boost the portability and likeness of the code
329  */
330 
331 /*
332  * Mapping table for core kernel memory. This memory is mapped at init
333  * time with section mappings.
334  *
335  * XXX One big assumption in the current architecture seems that the kernel is
336  * XXX supposed to be mapped into bootconfig.dram[0].
337  */
338 
339 #define ONE_MB	0x100000
340 
341 struct l1_sec_map {
342 	vaddr_t		va;
343 	paddr_t		pa;
344 	vsize_t		size;
345 	vm_prot_t	prot;
346 	int		cache;
347 } l1_sec_table[] = {
348 	/* Map 1Mb section for VIDC20 */
349 	{ VIDC_BASE,		VIDC_HW_BASE,
350 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
351 	    PTE_NOCACHE },
352 
353 	/* Map 1Mb section from IOMD */
354 	{ IOMD_BASE,		IOMD_HW_BASE,
355 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
356 	    PTE_NOCACHE },
357 
358 	/* Map 1Mb of COMBO (and module space) */
359 	{ IO_BASE,		IO_HW_BASE,
360 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
361 	    PTE_NOCACHE },
362 #if NPODULEBUS > 0	/* XXXJRT */
363 	/* Map the Fast and Sync simple podule space */
364 	{ SYNC_PODULE_BASE & 0xfff00000, SYNC_PODULE_HW_BASE & 0xfff00000,
365 	    L1_S_SIZE,		VM_PROT_READ|VM_PROT_WRITE,
366 	    PTE_NOCACHE },
367 	/* Map the EASI podule space */
368 	{ EASI_BASE,		EASI_HW_BASE,
369 	    MAX_PODULES * EASI_SIZE,	VM_PROT_READ|VM_PROT_WRITE,
370 	    PTE_NOCACHE },
371 #endif
372 	{ 0, 0, 0, 0, 0 }
373 };
374 
375 
376 static void
canonicalise_bootconfig(struct bootconfig * bootconf,struct bootconfig * raw_bootconf)377 canonicalise_bootconfig(struct bootconfig *bootconf, struct bootconfig *raw_bootconf)
378 {
379 	/* check for bootconfig v2+ structure */
380 	if (raw_bootconf->magic == BOOTCONFIG_MAGIC) {
381 		/* v2+ cleaned up structure found */
382 		*bootconf = *raw_bootconf;
383 		return;
384 	} else {
385 		panic2(("Internal error: no valid bootconfig block found"));
386 	}
387 }
388 
389 
390 vaddr_t
initarm(void * cookie)391 initarm(void *cookie)
392 {
393 	struct bootconfig *raw_bootconf = cookie;
394 	int loop;
395 	int loop1;
396 	u_int logical;
397 	u_int kerneldatasize;
398 	u_int l1pagetable;
399 	struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
400 	bool hasKinetic = false;
401 	paddr_t kinetic_physical_start;
402 
403 	/*
404 	 * Heads up ... Setup the CPU / MMU / TLB functions
405 	 */
406 	set_cpufuncs();
407 
408 	/* canonicalise the boot configuration structure to allow versioning */
409 	canonicalise_bootconfig(&bootconfig, raw_bootconf);
410 	booted_kernel = bootconfig.kernelname;
411 
412 	/* if the wscons interface is used, switch off VERBOSE booting :( */
413 #if NVIDCVIDEO>0
414 #	undef VERBOSE_INIT_ARM
415 #endif
416 
417 	/*
418 	 * Initialise the video memory descriptor
419 	 *
420 	 * Note: all references to the video memory virtual/physical address
421 	 * should go via this structure.
422 	 */
423 
424 	/* Hardwire it on the place the bootloader tells us */
425 	videomemory.vidm_vbase = bootconfig.display_start;
426 	videomemory.vidm_pbase = bootconfig.display_phys;
427 	videomemory.vidm_size = bootconfig.display_size;
428 	if (bootconfig.vram[0].pages)
429 		videomemory.vidm_type = VIDEOMEM_TYPE_VRAM;
430 	else
431 		videomemory.vidm_type = VIDEOMEM_TYPE_DRAM;
432 	vidc_base = (int *) VIDC_HW_BASE;
433 	iomd_base =         IOMD_HW_BASE;
434 
435 	/*
436 	 * Initialise the physical console
437 	 * This is done in main() but for the moment we do it here so that
438 	 * we can use printf in initarm() before main() has been called.
439 	 * only for `vidcconsole!' ... not wscons
440 	 */
441 #if NVIDCVIDEO == 0
442 	consinit();
443 #endif
444 
445 	/*
446 	 * Initialise the diagnostic serial console
447 	 * This allows a means of generating output during initarm().
448 	 * Once all the memory map changes are complete we can call consinit()
449 	 * and not have to worry about things moving.
450 	 */
451 	/* fcomcnattach(DC21285_ARMCSR_BASE, comcnspeed, comcnmode); */
452 	/* XXX snif .... i am still not able to this */
453 
454 	/*
455 	 * We have the following memory map (derived from EBSA)
456 	 *
457 	 * virtual address == physical address apart from the areas:
458 	 * 0x00000000 -> 0x000fffff which is mapped to
459 	 * top 1MB of physical memory
460 	 * 0xf0000000 -> 0xf0ffffff which is mapped to
461 	 * physical address 0x10000000 -> 0x10ffffff
462 	 * or on a Kinetic:
463 	 * physical address 0x20400000 -> 0x20ffffff
464 	 *
465 	 * This means that the kernel is mapped suitably for continuing
466 	 * execution, all I/O is mapped 1:1 virtual to physical and
467 	 * physical memory is accessible.
468 	 *
469 	 * The initarm() has the responsibility for creating the kernel
470 	 * page tables.
471 	 * It must also set up various memory pointers that are used
472 	 * by pmap etc.
473 	 */
474 
475 #ifdef FORCE_VERBOSE_INIT_ARM
476 	/*
477 	 * note that this will stop working after we switch to the new
478 	 * L1 Table
479 	 */
480 	memset((void *) (videomemory.vidm_vbase), 0x55, videomemory.vidm_size);
481 	consinit();
482 	printf("\n\n\n\n\n\n\n");
483 #define VERBOSE_INIT_ARM
484 #endif
485 	/* START OF REAL NEW STUFF */
486 
487 	/* Check to make sure the page size is correct */
488 	if (PAGE_SIZE != bootconfig.pagesize)
489 		panic2(("Page size is %d bytes instead of %d !! (huh?)\n",
490 			   bootconfig.pagesize, PAGE_SIZE));
491 
492 	/* process arguments */
493 	process_kernel_args();
494 
495 	/*
496 	 * Now set up the page tables for the kernel ... this part is copied
497 	 * in a (modified?) way from the EBSA machine port....
498 	 */
499 
500 #ifdef VERBOSE_INIT_ARM
501 	printf("Allocating page tables\n");
502 #endif
503 	/*
504 	 * Set up the variables that define the availability of physical
505 	 * memory
506 	 */
507 	physical_start = 0xffffffff;
508 	physical_end = 0;
509 	kinetic_physical_start = 0xffffffff;
510 #ifdef VERBOSE_INIT_ARM
511 	printf("memory blocks:\n");
512 #endif
513 	for (loop = 0, physmem = 0; loop < bootconfig.dramblocks; ++loop) {
514 #ifdef VERBOSE_INIT_ARM
515 		printf("0x%x + 0x%0x, type = 0x%08x\n", bootconfig.dram[loop].address,
516 				 bootconfig.dram[loop].pages * PAGE_SIZE,
517 				 bootconfig.dram[loop].flags);
518 #endif
519 	    	if (bootconfig.dram[loop].address < physical_start)
520 			physical_start = bootconfig.dram[loop].address;
521 		memoryblock_end = bootconfig.dram[loop].address +
522 		    bootconfig.dram[loop].pages * PAGE_SIZE;
523 		if (memoryblock_end > physical_end)
524 			physical_end = memoryblock_end;
525 		physmem += bootconfig.dram[loop].pages;
526 		if (bootconfig.dram[loop].flags & PHYSMEM_TYPE_PROCESSOR_ONLY) {
527 			hasKinetic = true;
528 			if (bootconfig.dram[loop].address < kinetic_physical_start)
529 				kinetic_physical_start = bootconfig.dram[loop].address;
530 		}
531 	};
532 
533 	if (hasKinetic)
534 	{
535 		/* Kinetics can only DMA from the Normal DRAM */
536 		dma_range_begin = 0xffffffff;
537 		dma_range_end = 0;
538 		for (loop = 0; loop < bootconfig.dramblocks; ++loop) {
539 			if (bootconfig.dram[loop].flags == PHYSMEM_TYPE_GENERIC) {
540 				if (bootconfig.dram[loop].address < dma_range_begin)
541 					dma_range_begin = bootconfig.dram[loop].address;
542 				memoryblock_end = bootconfig.dram[loop].address +
543 					bootconfig.dram[loop].pages * PAGE_SIZE;
544 				if (memoryblock_end > dma_range_end)
545 					dma_range_end = memoryblock_end;
546 			}
547 		}
548 		dma_range_end   = (paddr_t) MIN(dma_range_end, 256*1024*1024);
549 	} else {
550 		/* everything else DMAs all the memory */
551 		dma_range_begin = (paddr_t) physical_start;
552 		dma_range_end   = (paddr_t) MIN(physical_end, 512*1024*1024);
553 	}
554 
555 	/* set the location of the kernel in physical memory */
556 	if (hasKinetic) {
557 		kernel_start = kinetic_physical_start;
558 	} else {
559 		kernel_start = physical_start;
560 	}
561 	physical_freestart = kernel_start;
562 	free_pages = bootconfig.drampages;
563 	physical_freeend = physical_end;
564 
565 	/*
566 	 * AHUM !! set this variable ... it was set up in the old 1st
567 	 * stage bootloader
568 	 */
569 	kerneldatasize = bootconfig.kernsize + bootconfig.MDFsize;
570 
571 	/* Update the address of the first free page of physical memory */
572 	physical_freestart +=
573 	    bootconfig.kernsize + bootconfig.scratchsize;
574 	free_pages -= (bootconfig.kernsize + bootconfig.scratchsize) / PAGE_SIZE;
575 
576 	/* Define a macro to simplify memory allocation */
577 #define	valloc_pages(var, np)						\
578 	alloc_pages((var).pv_pa, (np));					\
579 	(var).pv_va = KERNEL_BASE + (var).pv_pa - kernel_start;
580 
581 #define alloc_pages(var, np)						\
582 	(var) = physical_freestart;					\
583 	physical_freestart += ((np) * PAGE_SIZE);			\
584 	free_pages -= (np);						\
585 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
586 
587 	loop1 = 0;
588 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
589 		/* Are we 16KB aligned for an L1 ? */
590 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
591 		    && kernel_l1pt.pv_pa == 0) {
592 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
593 		} else {
594 			valloc_pages(kernel_pt_table[loop1],
595 					L2_TABLE_SIZE / PAGE_SIZE);
596 			++loop1;
597 		}
598 	}
599 
600 
601 #ifdef DIAGNOSTIC
602 	/* This should never be able to happen but better confirm that. */
603 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
604 		panic2(("initarm: Failed to align the kernel page "
605 		    "directory\n"));
606 #endif
607 
608 	/*
609 	 * Allocate a page for the system page mapped to V0x00000000
610 	 * This page will just contain the system vectors and can be
611 	 * shared by all processes.
612 	 */
613 	alloc_pages(systempage.pv_pa, 1);
614 
615 	/* Allocate stacks for all modes */
616 	valloc_pages(irqstack, IRQ_STACK_SIZE);
617 	valloc_pages(abtstack, ABT_STACK_SIZE);
618 	valloc_pages(undstack, UND_STACK_SIZE);
619 	valloc_pages(kernelstack, UPAGES);
620 
621 #ifdef VERBOSE_INIT_ARM
622 	printf("Setting up stacks :\n");
623 	printf("IRQ stack: p0x%08lx v0x%08lx\n",
624 	    irqstack.pv_pa, irqstack.pv_va);
625 	printf("ABT stack: p0x%08lx v0x%08lx\n",
626 	    abtstack.pv_pa, abtstack.pv_va);
627 	printf("UND stack: p0x%08lx v0x%08lx\n",
628 	    undstack.pv_pa, undstack.pv_va);
629 	printf("SVC stack: p0x%08lx v0x%08lx\n",
630 	    kernelstack.pv_pa, kernelstack.pv_va);
631 	printf("\n");
632 #endif
633 
634 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
635 
636 #ifdef CPU_SA110
637 	/*
638 	 * XXX totally stuffed hack to work round problems introduced
639 	 * in recent versions of the pmap code. Due to the calls used there
640 	 * we cannot allocate virtual memory during bootstrap.
641 	 */
642 	sa110_cc_base = (KERNEL_BASE + (physical_freestart - kernel_start)
643 	    + (CPU_SA110_CACHE_CLEAN_SIZE - 1))
644 	    & ~(CPU_SA110_CACHE_CLEAN_SIZE - 1);
645 #endif	/* CPU_SA110 */
646 
647 	/*
648 	 * Ok we have allocated physical pages for the primary kernel
649 	 * page tables
650 	 */
651 
652 #ifdef VERBOSE_INIT_ARM
653 	printf("Creating L1 page table p@0x%08x\n", (uint32_t)kernel_l1pt.pv_pa);
654 #endif
655 
656 	/*
657 	 * Now we start construction of the L1 page table
658 	 * We start by mapping the L2 page tables into the L1.
659 	 * This means that we can replace L1 mappings later on if necessary
660 	 */
661 	l1pagetable = kernel_l1pt.pv_pa;
662 
663 	/* Map the L2 pages tables in the L1 page table */
664 	pmap_link_l2pt(l1pagetable, 0x00000000,
665 	    &kernel_pt_table[KERNEL_PT_SYS]);
666 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
667 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
668 	pmap_link_l2pt(l1pagetable, KERNEL_BASE + 0x00400000,
669 	    &kernel_pt_table[KERNEL_PT_KERNEL_4MB]);
670 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
671 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
672 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
673 	pmap_link_l2pt(l1pagetable, VMEM_VBASE,
674 	    &kernel_pt_table[KERNEL_PT_VMEM]);
675 
676 	/* update the top of the kernel VM */
677 	pmap_curmaxkvaddr =
678 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
679 
680 #ifdef VERBOSE_INIT_ARM
681 	printf("Mapping kernel\n");
682 #endif
683 
684 	/* Now we fill in the L2 pagetable for the kernel code/data */
685 	/* XXX Kernel doesn't have to be on physical_start (!) use bootconfig XXX */
686 	/*
687 	 * The defines are a workaround for a recent problem that occurred
688 	 * with ARM 610 processors and some ARM 710 processors
689 	 * Other ARM 710 and StrongARM processors don't have a problem.
690 	 */
691 	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
692 #if defined(CPU_ARM6) || defined(CPU_ARM7)
693 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
694 		    kernel_start, kernexec->a_text,
695 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
696 #else	/* CPU_ARM6 || CPU_ARM7 */
697 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
698 		    kernel_start, kernexec->a_text,
699 		    VM_PROT_READ, PTE_CACHE);
700 #endif	/* CPU_ARM6 || CPU_ARM7 */
701 		logical += pmap_map_chunk(l1pagetable,
702 		    KERNEL_TEXT_BASE + logical, kernel_start + logical,
703 		    kerneldatasize - kernexec->a_text,
704 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
705 	} else {	/* !ZMAGIC */
706 		/*
707 		 * Most likely an ELF kernel ...
708 		 * XXX no distinction yet between read only and
709 		 * read/write area's ...
710 		 */
711 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
712 		    kernel_start, kerneldatasize,
713 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
714 	};
715 
716 
717 #ifdef VERBOSE_INIT_ARM
718 	printf("Constructing L2 page tables\n");
719 #endif
720 
721 	/* Map the stack pages */
722 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
723 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
724 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
725 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
726 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
727 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
728 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
729 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
730 
731 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
732 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
733 
734 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
735 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
736 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
737 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
738 	}
739 
740 	/* Now we fill in the L2 pagetable for the VRAM */
741 	/*
742 	 * Current architectures mean that the VRAM is always in 1
743 	 * continuous bank.  This means that we can just map the 2 meg
744 	 * that the VRAM would occupy.  In theory we don't need a page
745 	 * table for VRAM, we could section map it but we would need
746 	 * the page tables if DRAM was in use.
747 	 * XXX please map two adjacent virtual areas to ONE physical
748 	 * area
749 	 */
750 	pmap_map_chunk(l1pagetable, VMEM_VBASE, videomemory.vidm_pbase,
751 	    videomemory.vidm_size, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
752 	pmap_map_chunk(l1pagetable, VMEM_VBASE + videomemory.vidm_size,
753 	    videomemory.vidm_pbase, videomemory.vidm_size,
754 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
755 
756 	/* Map the vector page. */
757 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
758 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
759 
760 	/* Map the core memory needed before autoconfig */
761 	loop = 0;
762 	while (l1_sec_table[loop].size) {
763 		vsize_t sz;
764 
765 #ifdef VERBOSE_INIT_ARM
766 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
767 			l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
768 			l1_sec_table[loop].va);
769 #endif
770 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
771 			pmap_map_section(l1pagetable,
772 			    l1_sec_table[loop].va + sz,
773 			    l1_sec_table[loop].pa + sz,
774 			    l1_sec_table[loop].prot,
775 			    l1_sec_table[loop].cache);
776 		++loop;
777 	}
778 
779 	/*
780 	 * Now we have the real page tables in place so we can switch
781 	 * to them.  Once this is done we will be running with the
782 	 * REAL kernel page tables.
783 	 */
784 
785 	/* be a client to all domains */
786 	cpu_domains(0x55555555);
787 
788 	/* Switch tables */
789 #ifdef VERBOSE_INIT_ARM
790 	printf("switching to new L1 page table\n");
791 #endif
792 
793 	cpu_setttb(kernel_l1pt.pv_pa, true);
794 
795 	/*
796 	 * We must now clean the cache again....
797 	 * Cleaning may be done by reading new data to displace any
798 	 * dirty data in the cache. This will have happened in cpu_setttb()
799 	 * but since we are boot strapping the addresses used for the read
800 	 * may have just been remapped and thus the cache could be out
801 	 * of sync. A re-clean after the switch will cure this.
802 	 * After booting there are no gross relocations of the kernel thus
803 	 * this problem will not occur after initarm().
804 	 */
805 	cpu_idcache_wbinv_all();
806 	cpu_tlb_flushID();
807 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
808 
809 	/*
810 	 * Moved from cpu_startup() as data_abort_handler() references
811 	 * this during uvm init
812 	 */
813 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
814 
815 	/*
816 	 * if there is support for a serial console ...we should now
817 	 * reattach it
818 	 */
819 	/*      fcomcndetach();*/
820 
821 	/*
822 	 * Reflect videomemory relocation in the videomemory structure
823 	 * and reinit console
824 	 */
825 	if (bootconfig.vram[0].pages == 0) {
826 		videomemory.vidm_vbase   = VMEM_VBASE;
827 	} else {
828 		videomemory.vidm_vbase   = VMEM_VBASE;
829 		bootconfig.display_start = VMEM_VBASE;
830 	};
831 	vidc_base = (int *) VIDC_BASE;
832 	iomd_base =         IOMD_BASE;
833 
834 #ifdef FORCE_VERBOSE_INIT_ARM2
835 	consinit();
836 	printf("\n\n\n\n\n\n\n");
837 #define VERBOSE_INIT_ARM
838 #endif
839 
840 #ifdef VERBOSE_INIT_ARM
841 	printf("running on the new L1 page table!\n");
842 	printf("done.\n");
843 #endif
844 
845 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
846 
847 #ifdef VERBOSE_INIT_ARM
848 	printf("\n");
849 #endif
850 
851 	/*
852 	 * Pages were allocated during the secondary bootstrap for the
853 	 * stacks for different CPU modes.
854 	 * We must now set the r13 registers in the different CPU modes to
855 	 * point to these stacks.
856 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
857 	 * of the stack memory.
858 	 */
859 #ifdef VERBOSE_INIT_ARM
860 	printf("init subsystems: stacks ");
861 	console_flush();
862 #endif
863 
864 	set_stackptr(PSR_IRQ32_MODE,
865 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
866 	set_stackptr(PSR_ABT32_MODE,
867 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
868 	set_stackptr(PSR_UND32_MODE,
869 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
870 #ifdef VERBOSE_INIT_ARM
871 	printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
872 	    kernelstack.pv_pa);
873 #endif	/* VERBOSE_INIT_ARM */
874 
875 	/*
876 	 * Well we should set a data abort handler.
877 	 * Once things get going this will change as we will need a proper
878 	 * handler. Until then we will use a handler that just panics but
879 	 * tells us why.
880 	 * Initialisation of the vectors will just panic on a data abort.
881 	 * This just fills in a slightly better one.
882 	 */
883 #ifdef VERBOSE_INIT_ARM
884 	printf("vectors ");
885 #endif
886 	data_abort_handler_address = (u_int)data_abort_handler;
887 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
888 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
889 	console_flush();
890 
891 
892 	/*
893 	 * At last !
894 	 * We now have the kernel in physical memory from the bottom upwards.
895 	 * Kernel page tables are physically above this.
896 	 * The kernel is mapped to 0xf0000000
897 	 * The kernel data PTs will handle the mapping of
898 	 *   0xf1000000-0xf5ffffff (80 Mb)
899 	 * 2Meg of VRAM is mapped to 0xf7000000
900 	 * The page tables are mapped to 0xefc00000
901 	 * The IOMD is mapped to 0xf6000000
902 	 * The VIDC is mapped to 0xf6100000
903 	 * The IOMD/VIDC could be pushed up higher but i havent got
904 	 * sufficient documentation to do so; the addresses are not
905 	 * parametized yet and hard to read... better fix this before;
906 	 * its pretty unforgiving.
907 	 */
908 
909 	/* Initialise the undefined instruction handlers */
910 #ifdef VERBOSE_INIT_ARM
911 	printf("undefined ");
912 #endif
913 	undefined_init();
914 	console_flush();
915 
916 	/* Load memory into UVM. */
917 #ifdef VERBOSE_INIT_ARM
918 	printf("page ");
919 #endif
920 	uvm_md_init();
921 
922 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
923 		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
924 		paddr_t end = start + (bootconfig.dram[loop].pages * PAGE_SIZE);
925 
926 		if (end > physical_freestart)
927 		{
928 			if (start < physical_freestart)
929 				start = physical_freestart;
930 			if (end > physical_freeend)
931 				end = physical_freeend;
932 		}
933 
934 		if (bootconfig.dram[loop].flags & PHYSMEM_TYPE_PROCESSOR_ONLY) {
935 			uvm_page_physload(atop(start), atop(end),
936 					atop(start), atop(end), VM_FREELIST_DEFAULT);
937 		} else {
938 			uvm_page_physload(atop(start), atop(end),
939 					atop(start), atop(end), VM_FREELIST_RPCDMA);
940 		}
941 	}
942 
943 	/* Boot strap pmap telling it where managed kernel virtual memory is */
944 #ifdef VERBOSE_INIT_ARM
945 	printf("pmap ");
946 #endif
947 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
948 	console_flush();
949 
950 	/* Setup the IRQ system */
951 #ifdef VERBOSE_INIT_ARM
952 	printf("irq ");
953 #endif
954 	console_flush();
955 	irq_init();
956 #ifdef VERBOSE_INIT_ARM
957 	printf("done.\n\n");
958 #endif
959 
960 #if NVIDCVIDEO>0
961 	consinit();		/* necessary ? */
962 #endif
963 
964 	/* Talk to the user */
965 	printf("NetBSD/acorn32 booting ... \n");
966 
967 	/* Tell the user if his boot loader is too old */
968 	if ((bootconfig.magic < BOOTCONFIG_MAGIC) ||
969 	    (bootconfig.version != BOOTCONFIG_VERSION)) {
970 		printf("\nDETECTED AN OLD BOOTLOADER. PLEASE UPGRADE IT\n\n");
971 		delay(5000000);
972 	}
973 
974 	printf("Kernel loaded from file %s\n", bootconfig.kernelname);
975 	printf("Kernel arg string (@%p) %s\n",
976 	    bootconfig.args, bootconfig.args);
977 	printf("\nBoot configuration structure reports the following "
978 	    "memory\n");
979 
980 	printf(" DRAM block 0a at %08x size %08x "
981 	    "DRAM block 0b at %08x size %08x\n\r",
982 	    bootconfig.dram[0].address,
983 	    bootconfig.dram[0].pages * bootconfig.pagesize,
984 	    bootconfig.dram[1].address,
985 	    bootconfig.dram[1].pages * bootconfig.pagesize);
986 	printf(" DRAM block 1a at %08x size %08x "
987 	    "DRAM block 1b at %08x size %08x\n\r",
988 	    bootconfig.dram[2].address,
989 	    bootconfig.dram[2].pages * bootconfig.pagesize,
990 	    bootconfig.dram[3].address,
991 	    bootconfig.dram[3].pages * bootconfig.pagesize);
992 	printf(" VRAM block 0  at %08x size %08x\n\r",
993 	    bootconfig.vram[0].address,
994 	    bootconfig.vram[0].pages * bootconfig.pagesize);
995 	if (hasKinetic)
996 		printf("%s", " Kinetic memory was detected\n\r");
997 
998 	/*
999 	 * Get a handle on the I2C interface so we can read
1000 	 * the NVRAM in the real-time clock chip.
1001 	 */
1002 	acorn32_i2c_tag = iomdiic_bootstrap_cookie();
1003 
1004 	if (cmos_read(RTC_ADDR_REBOOTCNT) > 0)
1005 		printf("Warning: REBOOTCNT = %d\n",
1006 		    cmos_read(RTC_ADDR_REBOOTCNT));
1007 
1008 #ifdef CPU_SA110
1009 	if (cputype == CPU_ID_SA110)
1010 		rpc_sa110_cc_setup();
1011 #endif	/* CPU_SA110 */
1012 
1013 #if NKSYMS || defined(DDB) || defined(MODULAR)
1014 	ksyms_addsyms_elf(bootconfig.ksym_end - bootconfig.ksym_start,
1015 		(void *) bootconfig.ksym_start, (void *) bootconfig.ksym_end);
1016 #endif
1017 
1018 
1019 #ifdef DDB
1020 	db_machine_init();
1021 	if (boothowto & RB_KDB)
1022 		Debugger();
1023 #endif	/* DDB */
1024 
1025 	/* We return the new stack pointer address */
1026 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
1027 }
1028 
1029 
1030 static void
process_kernel_args(void)1031 process_kernel_args(void)
1032 {
1033 	char *args;
1034 
1035 	/* Ok now we will check the arguments for interesting parameters. */
1036 	args = bootconfig.args;
1037 	boothowto = 0;
1038 
1039 	/* Only arguments itself are passed from the new bootloader */
1040 	while (*args == ' ')
1041 		++args;
1042 
1043 	boot_args = args;
1044 	parse_mi_bootargs(boot_args);
1045 	parse_rpc_bootargs(boot_args);
1046 }
1047 
1048 
1049 void
parse_rpc_bootargs(char * args)1050 parse_rpc_bootargs(char *args)
1051 {
1052 	int integer;
1053 
1054 	if (get_bootconf_option(args, "videodram", BOOTOPT_TYPE_INT,
1055 	    &integer)) {
1056 		videodram_size = integer;
1057 		/* Round to 4K page */
1058 		videodram_size *= 1024;
1059 		videodram_size = round_page(videodram_size);
1060 		if (videodram_size > 1024*1024)
1061 			videodram_size = 1024*1024;
1062 	}
1063 
1064 #if 0
1065 	/* XXX this I would rather have in the new bootconfig structure */
1066 	if (get_bootconf_option(args, "kinetic", BOOTOPT_TYPE_BOOLEAN,
1067 	    &integer)) {
1068 		bootconfig.RPC_kinetic_card_support = 1;
1069 	}
1070 #endif
1071 }
1072 
1073 
1074 #ifdef CPU_SA110
1075 
1076 /*
1077  * For optimal cache cleaning we need two 16K banks of
1078  * virtual address space that NOTHING else will access
1079  * and then we alternate the cache cleaning between the
1080  * two banks.
1081  * The cache cleaning code requires 2 banks aligned
1082  * on total size boundary so the banks can be alternated by
1083  * xorring the size bit (assumes the bank size is a power of 2)
1084  */
1085 extern unsigned int sa1_cache_clean_addr;
1086 extern unsigned int sa1_cache_clean_size;
1087 void
rpc_sa110_cc_setup(void)1088 rpc_sa110_cc_setup(void)
1089 {
1090 	int loop;
1091 	paddr_t kaddr;
1092 
1093 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
1094 	const pt_entry_t npte = L2_S_PROTO | kaddr |
1095 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
1096 	for (loop = 0; loop < CPU_SA110_CACHE_CLEAN_SIZE; loop += PAGE_SIZE) {
1097 		pt_entry_t * const ptep = vtopte(sa110_cc_base + loop);
1098 		l2pte_set(ptep, npte, 0);
1099 		PTE_SYNC(ptep);
1100 	}
1101 	sa1_cache_clean_addr = sa110_cc_base;
1102 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
1103 }
1104 #endif	/* CPU_SA110 */
1105 
1106 /*
1107  * To convert from RISC OS addresses to real CMOS addresses, do this:
1108  *
1109  * if (riscosaddr < 0xc0)
1110  *         realaddr = riscosaddr + 0x40;
1111  * else
1112  *         realaddr = riscosaddr - 0xb0;
1113  */
1114 
1115 /* Read a byte from CMOS RAM. */
1116 int
cmos_read(int location)1117 cmos_read(int location)
1118 {
1119 	uint8_t val;
1120 
1121 	if (pcfrtc_bootstrap_read(acorn32_i2c_tag, 0x50,
1122 	    location, &val, 1) != 0)
1123 		return (-1);
1124 	return (val);
1125 }
1126 
1127 /* Write a byte to CMOS RAM. */
1128 int
cmos_write(int location,int value)1129 cmos_write(int location, int value)
1130 {
1131 	uint8_t val = value;
1132 	int oldvalue, oldsum;
1133 
1134 	/* Get the old value and checksum. */
1135 	if ((oldvalue = cmos_read(location)) < 0)
1136 		return (-1);
1137 	if ((oldsum = cmos_read(RTC_ADDR_CHECKSUM)) < 0)
1138 		return (-1);
1139 
1140 	if (pcfrtc_bootstrap_write(acorn32_i2c_tag, 0x50,
1141 	    location, &val, 1) != 0)
1142 		return (-1);
1143 
1144 	/* Now update the checksum. */
1145 	val = (uint8_t)oldsum - (uint8_t)oldvalue + val;
1146 	return (pcfrtc_bootstrap_write(acorn32_i2c_tag, 0x50,
1147 	    RTC_ADDR_CHECKSUM, &val, 1));
1148 }
1149 
1150 /* End of machdep.c */
1151