xref: /netbsd-src/sys/arch/atari/atari/atari_init.c (revision 56a34939419542e88b386b2229be7565f4f45461)
1 /*	$NetBSD: atari_init.c,v 1.68 2008/11/15 21:30:50 abs Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman
5  * Copyright (c) 1994 Michael L. Hitch
6  * Copyright (c) 1993 Markus Wild
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Markus Wild.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: atari_init.c,v 1.68 2008/11/15 21:30:50 abs Exp $");
37 
38 #include "opt_ddb.h"
39 #include "opt_mbtype.h"
40 #include "opt_m060sp.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/user.h>
46 #include <sys/ioctl.h>
47 #include <sys/select.h>
48 #include <sys/tty.h>
49 #include <sys/buf.h>
50 #include <sys/msgbuf.h>
51 #include <sys/mbuf.h>
52 #include <sys/extent.h>
53 #include <sys/protosw.h>
54 #include <sys/domain.h>
55 #include <sys/dkbad.h>
56 #include <sys/reboot.h>
57 #include <sys/exec.h>
58 #include <sys/core.h>
59 #include <sys/kcore.h>
60 
61 #include <uvm/uvm_extern.h>
62 
63 #include <machine/vmparam.h>
64 #include <machine/pte.h>
65 #include <machine/cpu.h>
66 #include <machine/iomap.h>
67 #include <machine/mfp.h>
68 #include <machine/scu.h>
69 #include <machine/acia.h>
70 #include <machine/kcore.h>
71 
72 #include <m68k/cpu.h>
73 #include <m68k/cacheops.h>
74 
75 #include <atari/atari/intr.h>
76 #include <atari/atari/stalloc.h>
77 #include <atari/dev/ym2149reg.h>
78 
79 #include "pci.h"
80 
81 void start_c __P((int, u_int, u_int, u_int, char *));
82 static void atari_hwinit __P((void));
83 static void cpu_init_kcorehdr __P((u_long));
84 static void initcpu __P((void));
85 static void mmu030_setup __P((st_entry_t *, u_int, pt_entry_t *, u_int,
86 			      pt_entry_t *, u_int, u_int));
87 static void map_io_areas __P((pt_entry_t *, u_int, u_int));
88 static void set_machtype __P((void));
89 
90 #if defined(M68040) || defined(M68060)
91 static void mmu040_setup __P((st_entry_t *, u_int, pt_entry_t *, u_int,
92 			      pt_entry_t *, u_int, u_int));
93 #endif
94 
95 /*
96  * Extent maps to manage all memory space, including I/O ranges.  Allocate
97  * storage for 8 regions in each, initially.  Later, iomem_malloc_safe
98  * will indicate that it's safe to use malloc() to dynamically allocate
99  * region descriptors.
100  * This means that the fixed static storage is only used for registrating
101  * the found memory regions and the bus-mapping of the console.
102  *
103  * The extent maps are not static!  They are used for bus address space
104  * allocation.
105  */
106 static long iomem_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
107 struct extent *iomem_ex;
108 int iomem_malloc_safe;
109 
110 /*
111  * All info needed to generate a panic dump. All fields are setup by
112  * start_c().
113  * XXX: Should sheck usage of phys_segs. There is some unwanted overlap
114  *      here.... Also, the name is badly choosen. Phys_segs contains the
115  *      segment descriptions _after_ reservations are made.
116  * XXX: 'lowram' is obsoleted by the new panicdump format
117  */
118 static cpu_kcore_hdr_t cpu_kcore_hdr;
119 
120 extern u_int 	lowram;
121 extern u_int	Sysptsize, Sysseg_pa, proc0paddr;
122 extern pt_entry_t *Sysptmap;
123 extern st_entry_t *Sysseg;
124 u_int		*Sysmap;
125 int		machineid, mmutype, cputype, astpending;
126 char		*vmmap;
127 pv_entry_t	pv_table;
128 #if defined(M68040) || defined(M68060)
129 extern int	protostfree;
130 #endif
131 
132 extern char		*esym;
133 extern struct pcb	*curpcb;
134 
135 /*
136  * This is the virtual address of physical page 0. Used by 'do_boot()'.
137  */
138 vaddr_t	page_zero;
139 
140 /*
141  * Crude support for allocation in ST-ram. Currently only used to allocate
142  * video ram.
143  * The physical address is also returned because the video init needs it to
144  * setup the controller at the time the vm-system is not yet operational so
145  * 'kvtop()' cannot be used.
146  */
147 #ifndef ST_POOL_SIZE
148 #define	ST_POOL_SIZE	40			/* XXX: enough? */
149 #endif
150 
151 u_long	st_pool_size = ST_POOL_SIZE * PAGE_SIZE; /* Patchable	*/
152 u_long	st_pool_virt, st_pool_phys;
153 
154 /*
155  * Are we relocating the kernel to TT-Ram if possible? It is faster, but
156  * it is also reported not to work on all TT's. So the default is NO.
157  */
158 #ifndef	RELOC_KERNEL
159 #define	RELOC_KERNEL	0
160 #endif
161 int	reloc_kernel = RELOC_KERNEL;		/* Patchable	*/
162 
163 /*
164  * this is the C-level entry function, it's called from locore.s.
165  * Preconditions:
166  *	Interrupts are disabled
167  *	PA == VA, we don't have to relocate addresses before enabling
168  *		the MMU
169  * 	Exec is no longer available (because we're loaded all over
170  *		low memory, no ExecBase is available anymore)
171  *
172  * It's purpose is:
173  *	Do the things that are done in locore.s in the hp300 version,
174  *		this includes allocation of kernel maps and enabling the MMU.
175  *
176  * Some of the code in here is `stolen' from Amiga MACH, and was
177  * written by Bryan Ford and Niklas Hallqvist.
178  *
179  * Very crude 68040 support by Michael L. Hitch.
180  */
181 int kernel_copyback = 1;
182 
183 void
184 start_c(id, ttphystart, ttphysize, stphysize, esym_addr)
185 int	id;			/* Machine id				*/
186 u_int	ttphystart, ttphysize;	/* Start address and size of TT-ram	*/
187 u_int	stphysize;		/* Size of ST-ram	 		*/
188 char	*esym_addr;		/* Address of kernel '_esym' symbol	*/
189 {
190 	extern char	end[];
191 	extern void	etext __P((void));
192 	extern u_long	protorp[2];
193 	u_int		pstart;		/* Next available physical address*/
194 	u_int		vstart;		/* Next available virtual address */
195 	u_int		avail;
196 	pt_entry_t	*pt;
197 	u_int		ptsize, ptextra;
198 	u_int		tc, i;
199 	u_int		*pg;
200 	u_int		pg_proto;
201 	u_int		end_loaded;
202 	u_long		kbase;
203 	u_int		kstsize;
204 
205 #if defined(_MILANHW_)
206 	/* XXX
207 	 * XXX The right place todo this is probably the booter (Leo)
208 	 * XXX More than 16MB memory is not yet supported on the Milan!
209 	 * The Milan Lies about the presence of TT-RAM. If you insert
210 	 * 16MB it is split in 14MB ST starting at address 0 and 2MB TT RAM,
211 	 * starting at address 16MB.
212 	 */
213 	stphysize += ttphysize;
214 	ttphysize  = ttphystart = 0;
215 #endif
216 	boot_segs[0].start       = 0;
217 	boot_segs[0].end         = stphysize;
218 	boot_segs[1].start       = ttphystart;
219 	boot_segs[1].end         = ttphystart + ttphysize;
220 	boot_segs[2].start = boot_segs[2].end = 0; /* End of segments! */
221 
222 	/*
223 	 * The following is a hack. We do not know how much ST memory we
224 	 * really need until after configuration has finished. At this
225 	 * time I have no idea how to grab ST memory at that time.
226 	 * The round_page() call is ment to correct errors made by
227 	 * binpatching!
228 	 */
229 	st_pool_size   = m68k_round_page(st_pool_size);
230 	st_pool_phys   = stphysize - st_pool_size;
231 	stphysize      = st_pool_phys;
232 
233 	machineid      = id;
234 	esym           = esym_addr;
235 
236 	/*
237 	 * the kernel ends at end() or esym.
238 	 */
239 	if(esym == NULL)
240 		end_loaded = (u_int)end;
241 	else end_loaded = (u_int)esym;
242 
243 	/*
244 	 * If we have enough fast-memory to put the kernel in and the
245 	 * RELOC_KERNEL option is set, do it!
246 	 */
247 	if((reloc_kernel != 0) && (ttphysize >= end_loaded))
248 		kbase = ttphystart;
249 	else kbase = 0;
250 
251 	/*
252 	 * Determine the type of machine we are running on. This needs
253 	 * to be done early (and before initcpu())!
254 	 */
255 	set_machtype();
256 
257 	/*
258 	 * Initialize CPU specific stuff
259 	 */
260 	initcpu();
261 
262 	/*
263 	 * We run the kernel from ST memory at the moment.
264 	 * The kernel segment table is put just behind the loaded image.
265 	 * pstart: start of usable ST memory
266 	 * avail : size of ST memory available.
267 	 */
268 	pstart = (u_int)end_loaded;
269 	pstart = m68k_round_page(pstart);
270 	avail  = stphysize - pstart;
271 
272 	/*
273 	 * Calculate the number of pages needed for Sysseg.
274 	 * For the 68030, we need 256 descriptors (segment-table-entries).
275 	 * This easily fits into one page.
276 	 * For the 68040, both the level-1 and level-2 descriptors are
277 	 * stored into Sysseg. We currently handle a maximum sum of MAXKL2SIZE
278 	 * level-1 & level-2 tables.
279 	 */
280 #if defined(M68040) || defined(M68060)
281 	if (mmutype == MMU_68040)
282 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
283 	else
284 #endif
285 		kstsize = 1;
286 	/*
287 	 * allocate the kernel segment table
288 	 */
289 	Sysseg     = (st_entry_t *)pstart;
290 	Sysseg_pa  = (u_int)Sysseg + kbase;
291 	pstart    += kstsize * PAGE_SIZE;
292 	avail     -= kstsize * PAGE_SIZE;
293 
294 	/*
295 	 * Determine the number of pte's we need for extra's like
296 	 * ST I/O map's.
297 	 */
298 	ptextra = btoc(STIO_SIZE);
299 
300 	/*
301 	 * If present, add pci areas
302 	 */
303 	if (machineid & ATARI_HADES)
304 		ptextra += btoc(PCI_CONF_SIZE + PCI_IO_SIZE + PCI_MEM_SIZE);
305 	if (machineid & ATARI_MILAN)
306 		ptextra += btoc(PCI_IO_SIZE + PCI_MEM_SIZE);
307 	ptextra += btoc(BOOTM_VA_POOL);
308 
309 	/*
310 	 * The 'pt' (the initial kernel pagetable) has to map the kernel and
311 	 * the I/O areas. The various I/O areas are mapped (virtually) at
312 	 * the top of the address space mapped by 'pt' (ie. just below Sysmap).
313 	 */
314 	pt      = (pt_entry_t *)pstart;
315 	ptsize  = (Sysptsize + howmany(ptextra, NPTEPG)) << PGSHIFT;
316 	pstart += ptsize;
317 	avail  -= ptsize;
318 
319 	/*
320 	 * allocate kernel page table map
321 	 */
322 	Sysptmap = (pt_entry_t *)pstart;
323 	pstart  += PAGE_SIZE;
324 	avail   -= PAGE_SIZE;
325 
326 	/*
327 	 * Set Sysmap; mapped after page table pages. Because I too (LWP)
328 	 * didn't understand the reason for this, I borrowed the following
329 	 * (sligthly modified) comment from mac68k/locore.s:
330 	 * LAK:  There seems to be some confusion here about the next line,
331 	 * so I'll explain.  The kernel needs some way of dynamically modifying
332 	 * the page tables for its own virtual memory.  What it does is that it
333 	 * has a page table map.  This page table map is mapped right after the
334 	 * kernel itself (in our implementation; in HP's it was after the I/O
335 	 * space). Therefore, the first three (or so) entries in the segment
336 	 * table point to the first three pages of the page tables (which
337 	 * point to the kernel) and the next entry in the segment table points
338 	 * to the page table map (this is done later).  Therefore, the value
339 	 * of the pointer "Sysmap" will be something like 16M*3 = 48M.  When
340 	 * the kernel addresses this pointer (e.g., Sysmap[0]), it will get
341 	 * the first longword of the first page map (== pt[0]).  Since the
342 	 * page map mirrors the segment table, addressing any index of Sysmap
343 	 * will give you a PTE of the page maps which map the kernel.
344 	 */
345 	Sysmap = (u_int *)(ptsize << (SEGSHIFT - PGSHIFT));
346 
347 	/*
348 	 * Initialize segment tables
349 	 */
350 #if defined(M68040) || defined(M68060)
351 	if (mmutype == MMU_68040)
352 		mmu040_setup(Sysseg, kstsize, pt, ptsize, Sysptmap, 1, kbase);
353 	else
354 #endif /* defined(M68040) || defined(M68060) */
355 		mmu030_setup(Sysseg, kstsize, pt, ptsize, Sysptmap, 1, kbase);
356 
357 	/*
358 	 * initialize kernel page table page(s).
359 	 * Assume load at VA 0.
360 	 * - Text pages are RO
361 	 * - Page zero is invalid
362 	 */
363 	pg_proto = (0 + kbase) | PG_RO | PG_V;
364 	pg       = pt;
365 	*pg++ = PG_NV; pg_proto += PAGE_SIZE;
366 	for(i = PAGE_SIZE; i < (u_int)etext;
367 	    i += PAGE_SIZE, pg_proto += PAGE_SIZE)
368 		*pg++ = pg_proto;
369 
370 	/*
371 	 * data, bss and dynamic tables are read/write
372 	 */
373 	pg_proto = (pg_proto & PG_FRAME) | PG_RW | PG_V;
374 
375 #if defined(M68040) || defined(M68060)
376 	/*
377 	 * Map the kernel segment table cache invalidated for
378 	 * these machines (for the 68040 not strictly necessary, but
379 	 * recommended by Motorola; for the 68060 mandatory)
380 	 */
381 	if (mmutype == MMU_68040) {
382 
383 	    if (kernel_copyback)
384 		pg_proto |= PG_CCB;
385 
386 	    for (; i < (u_int)Sysseg; i += PAGE_SIZE, pg_proto += PAGE_SIZE)
387 		*pg++ = pg_proto;
388 
389 	    pg_proto = (pg_proto & ~PG_CCB) | PG_CI;
390 	    for (; i < pstart; i += PAGE_SIZE, pg_proto += PAGE_SIZE)
391 		*pg++ = pg_proto;
392 	    pg_proto = (pg_proto & ~PG_CI);
393 	    if (kernel_copyback)
394 		pg_proto |= PG_CCB;
395 	}
396 #endif /* defined(M68040) || defined(M68060) */
397 
398 	/*
399 	 * go till end of data allocated so far
400 	 * plus proc0 u-area (to be allocated)
401 	 */
402 	for(; i < pstart + USPACE; i += PAGE_SIZE, pg_proto += PAGE_SIZE)
403 		*pg++ = pg_proto;
404 
405 	/*
406 	 * invalidate remainder of kernel PT
407 	 */
408 	while(pg < &pt[ptsize/sizeof(pt_entry_t)])
409 		*pg++ = PG_NV;
410 
411 	/*
412 	 * Map various I/O areas
413 	 */
414 	map_io_areas(pt, ptsize, ptextra);
415 
416 	/*
417 	 * Save KVA of proc0 user-area and allocate it
418 	 */
419 	proc0paddr = pstart;
420 	pstart    += USPACE;
421 	avail     -= USPACE;
422 
423 	/*
424 	 * At this point, virtual and physical allocation starts to divert.
425 	 */
426 	vstart     = pstart;
427 
428 	/*
429 	 * Map the allocated space in ST-ram now. In the contig-case, there
430 	 * is no need to make a distinction between virtual and physical
431 	 * addresses. But I make it anyway to be prepared.
432 	 * Physcal space is already reserved!
433 	 */
434 	st_pool_virt = vstart;
435 	pg           = &pt[vstart / PAGE_SIZE];
436 	pg_proto     = st_pool_phys | PG_RW | PG_CI | PG_V;
437 	vstart      += st_pool_size;
438 	while(pg_proto < (st_pool_phys + st_pool_size)) {
439 		*pg++     = pg_proto;
440 		pg_proto += PAGE_SIZE;
441 	}
442 
443 	/*
444 	 * Map physical page_zero and page-zero+1 (First ST-ram page). We need
445 	 * to reference it in the reboot code. Two pages are mapped, because
446 	 * we must make sure 'doboot()' is contained in it (see the tricky
447 	 * copying there....).
448 	 */
449 	page_zero  = vstart;
450 	pg         = &pt[vstart / PAGE_SIZE];
451 	*pg++      = PG_RW | PG_CI | PG_V;
452 	vstart    += PAGE_SIZE;
453 	*pg        = PG_RW | PG_CI | PG_V | PAGE_SIZE;
454 	vstart    += PAGE_SIZE;
455 
456 	lowram  = 0 >> PGSHIFT; /* XXX */
457 
458 	/*
459 	 * Fill in usable segments. The page indexes will be initialized
460 	 * later when all reservations are made.
461 	 */
462 	usable_segs[0].start = 0;
463 	usable_segs[0].end   = stphysize;
464 	usable_segs[1].start = ttphystart;
465 	usable_segs[1].end   = ttphystart + ttphysize;
466 	usable_segs[2].start = usable_segs[2].end = 0; /* End of segments! */
467 
468 	if(kbase) {
469 		/*
470 		 * First page of ST-ram is unusable, reserve the space
471 		 * for the kernel in the TT-ram segment.
472 		 * Note: Because physical page-zero is partially mapped to ROM
473 		 *       by hardware, it is unusable.
474 		 */
475 		usable_segs[0].start  = PAGE_SIZE;
476 		usable_segs[1].start += pstart;
477 	}
478 	else usable_segs[0].start += pstart;
479 
480 	/*
481 	 * As all segment sizes are now valid, calculate page indexes and
482 	 * available physical memory.
483 	 */
484 	usable_segs[0].first_page = 0;
485 	for (i = 1; usable_segs[i].start; i++) {
486 		usable_segs[i].first_page  = usable_segs[i-1].first_page;
487 		usable_segs[i].first_page +=
488 		    (usable_segs[i-1].end - usable_segs[i-1].start) / PAGE_SIZE;
489 	}
490 	for (i = 0, physmem = 0; usable_segs[i].start; i++)
491 		physmem += usable_segs[i].end - usable_segs[i].start;
492 	physmem >>= PGSHIFT;
493 
494 	/*
495 	 * get the pmap module in sync with reality.
496 	 */
497 	pmap_bootstrap(vstart, stio_addr);
498 
499 	/*
500 	 * Prepare to enable the MMU.
501 	 * Setup and load SRP nolimit, share global, 4 byte PTE's
502 	 */
503 	protorp[0] = 0x80000202;
504 	protorp[1] = (u_int)Sysseg + kbase;	/* + segtable address */
505 	Sysseg_pa  = (u_int)Sysseg + kbase;
506 
507 	cpu_init_kcorehdr(kbase);
508 
509 	/*
510 	 * copy over the kernel (and all now initialized variables)
511 	 * to fastram.  DONT use bcopy(), this beast is much larger
512 	 * than 128k !
513 	 */
514 	if(kbase) {
515 		register u_long	*lp, *le, *fp;
516 
517 		lp = (u_long *)0;
518 		le = (u_long *)pstart;
519 		fp = (u_long *)kbase;
520 		while(lp < le)
521 			*fp++ = *lp++;
522 	}
523 #if defined(M68040) || defined(M68060)
524 	if (mmutype == MMU_68040) {
525 		/*
526 		 * movel Sysseg_pa,a0;
527 		 * movec a0,SRP;
528 		 * pflusha;
529 		 * movel #$0xc000,d0;
530 		 * movec d0,TC
531 		 */
532 		if (cputype == CPU_68060) {
533 			/* XXX: Need the branch cache be cleared? */
534 			__asm volatile (".word 0x4e7a,0x0002;"
535 				      "orl #0x400000,%%d0;"
536 				      ".word 0x4e7b,0x0002" : : : "d0");
537 		}
538 		__asm volatile ("movel %0,%%a0;"
539 			      ".word 0x4e7b,0x8807" : : "a" (Sysseg_pa) : "a0");
540 		__asm volatile (".word 0xf518" : : );
541 		__asm volatile ("movel #0xc000,%%d0;"
542 			      ".word 0x4e7b,0x0003" : : : "d0" );
543 	} else
544 #endif
545 	{
546 		__asm volatile ("pmove %0@,%%srp" : : "a" (&protorp[0]));
547 		/*
548 		 * setup and load TC register.
549 		 * enable_cpr, enable_srp, pagesize=8k,
550 		 * A = 8 bits, B = 11 bits
551 		 */
552 		tc = 0x82d08b00;
553 		__asm volatile ("pmove %0@,%%tc" : : "a" (&tc));
554 	}
555 
556 	/* Is this to fool the optimizer?? */
557 	i = *(int *)proc0paddr;
558 	*(volatile int *)proc0paddr = i;
559 
560 	/*
561 	 * Initialize the "u-area" pages.
562 	 * Must initialize p_addr before autoconfig or the
563 	 * fault handler will get a NULL reference.
564 	 */
565 	bzero((u_char *)proc0paddr, USPACE);
566 	lwp0.l_addr = (struct user *)proc0paddr;
567 	curlwp = &lwp0;
568 	curpcb  = &((struct user *)proc0paddr)->u_pcb;
569 
570 	/*
571 	 * Get the hardware into a defined state
572 	 */
573 	atari_hwinit();
574 
575 	/*
576 	 * Initialize stmem allocator
577 	 */
578 	init_stmem();
579 
580 	/*
581 	 * Initialize the I/O mem extent map.
582 	 * Note: we don't have to check the return value since
583 	 * creation of a fixed extent map will never fail (since
584 	 * descriptor storage has already been allocated).
585 	 *
586 	 * N.B. The iomem extent manages _all_ physical addresses
587 	 * on the machine.  When the amount of RAM is found, all
588 	 * extents of RAM are allocated from the map.
589 	 */
590 	iomem_ex = extent_create("iomem", 0x0, 0xffffffff, M_DEVBUF,
591 	    (void *)iomem_ex_storage, sizeof(iomem_ex_storage),
592 	    EX_NOCOALESCE|EX_NOWAIT);
593 
594 	/*
595 	 * Allocate the physical RAM from the extent map
596 	 */
597 	for (i = 0; boot_segs[i].end != 0; i++) {
598 		if (extent_alloc_region(iomem_ex, boot_segs[i].start,
599 			  boot_segs[i].end - boot_segs[i].start, EX_NOWAIT)) {
600 			/* XXX: Ahum, should not happen ;-) */
601 			printf("Warning: Cannot allocate boot memory from"
602 			       " extent map!?\n");
603 		}
604 	}
605 
606 	/*
607 	 * Initialize interrupt mapping.
608 	 */
609 	intr_init();
610 }
611 
612 /*
613  * Try to figure out on what type of machine we are running
614  * Note: This module runs *before* the io-mapping is setup!
615  */
616 static void
617 set_machtype()
618 {
619 #ifdef _MILANHW_
620 	machineid |= ATARI_MILAN;
621 
622 #else
623 	stio_addr = 0xff8000;	/* XXX: For TT & Falcon only */
624 	if(badbaddr((void *)__UNVOLATILE(&MFP2->mf_gpip), sizeof(char))) {
625 		/*
626 		 * Watch out! We can also have a Hades with < 16Mb
627 		 * RAM here...
628 		 */
629 		if(!badbaddr((void *)__UNVOLATILE(&MFP->mf_gpip),
630 		     sizeof(char))) {
631 			machineid |= ATARI_FALCON;
632 			return;
633 		}
634 	}
635 	if(!badbaddr((void *)(PCI_CONFB_PHYS + PCI_CONFM_PHYS), sizeof(char)))
636 		machineid |= ATARI_HADES;
637 	else machineid |= ATARI_TT;
638 #endif /* _MILANHW_ */
639 }
640 
641 static void
642 atari_hwinit()
643 {
644 #if defined(_ATARIHW_)
645 	/*
646 	 * Initialize the sound chip
647 	 */
648 	ym2149_init();
649 
650 	/*
651 	 * Make sure that the midi acia will not generate an interrupt
652 	 * unless something attaches to it. We cannot do this for the
653 	 * keyboard acia because this breaks the '-d' option of the
654 	 * booter...
655 	 */
656 	MDI->ac_cs = 0;
657 #endif /* defined(_ATARIHW_) */
658 
659 	/*
660 	 * Initialize both MFP chips (if both present!) to generate
661 	 * auto-vectored interrupts with EOI. The active-edge registers are
662 	 * set up. The interrupt enable registers are set to disable all
663 	 * interrupts.
664 	 */
665 	MFP->mf_iera  = MFP->mf_ierb = 0;
666 	MFP->mf_imra  = MFP->mf_imrb = 0;
667 	MFP->mf_aer   = MFP->mf_ddr  = 0;
668 	MFP->mf_vr    = 0x40;
669 
670 #if defined(_ATARIHW_)
671 	if(machineid & (ATARI_TT|ATARI_HADES)) {
672 		MFP2->mf_iera = MFP2->mf_ierb = 0;
673 		MFP2->mf_imra = MFP2->mf_imrb = 0;
674 		MFP2->mf_aer  = 0x80;
675 		MFP2->mf_vr   = 0x50;
676 	}
677 
678 	if(machineid & ATARI_TT) {
679 		/*
680 		 * Initialize the SCU, to enable interrupts on the SCC (ipl5),
681 		 * MFP (ipl6) and softints (ipl1).
682 		 */
683 		SCU->sys_mask = SCU_SYS_SOFT;
684 		SCU->vme_mask = SCU_MFP | SCU_SCC;
685 #ifdef DDB
686 		/*
687 		 * This allows people with the correct hardware modification
688 		 * to drop into the debugger from an NMI.
689 		 */
690 		SCU->sys_mask |= SCU_IRQ7;
691 #endif
692 	}
693 #endif /* defined(_ATARIHW_) */
694 
695 #if NPCI > 0
696 	if(machineid & (ATARI_HADES|ATARI_MILAN)) {
697 		/*
698 		 * Configure PCI-bus
699 		 */
700 		init_pci_bus();
701 	}
702 #endif
703 
704 }
705 
706 /*
707  * Do the dull work of mapping the various I/O areas. They MUST be Cache
708  * inhibited!
709  * All I/O areas are virtually mapped at the end of the pt-table.
710  */
711 static void
712 map_io_areas(pt, ptsize, ptextra)
713 pt_entry_t	*pt;
714 u_int		ptsize;		/* Size of 'pt' in bytes	*/
715 u_int		ptextra;	/* #of additional I/O pte's	*/
716 {
717 	extern void	bootm_init __P((vaddr_t, pt_entry_t *, u_long));
718 	vaddr_t		ioaddr;
719 	pt_entry_t	*pg, *epg;
720 	pt_entry_t	pg_proto;
721 	u_long		mask;
722 
723 	ioaddr = ((ptsize / sizeof(pt_entry_t)) - ptextra) * PAGE_SIZE;
724 
725 	/*
726 	 * Map ST-IO area
727 	 */
728 	stio_addr = ioaddr;
729 	ioaddr   += STIO_SIZE;
730 	pg        = &pt[stio_addr / PAGE_SIZE];
731 	epg       = &pg[btoc(STIO_SIZE)];
732 #ifdef _MILANHW_
733 	/*
734 	 * Turn on byte swaps in the ST I/O area. On the Milan, the
735 	 * U0 signal of the MMU controls the BigEndian signal
736 	 * of the PLX9080. We use this setting so we can read/write the
737 	 * PLX registers (and PCI-config space) in big-endian mode.
738 	 */
739 	pg_proto  = STIO_PHYS | PG_RW | PG_CI | PG_V | 0x100;
740 #else
741 	pg_proto  = STIO_PHYS | PG_RW | PG_CI | PG_V;
742 #endif
743 	while(pg < epg) {
744 		*pg++     = pg_proto;
745 		pg_proto += PAGE_SIZE;
746 	}
747 
748 	/*
749 	 * Map PCI areas
750 	 */
751 	if (machineid & ATARI_HADES) {
752 		/*
753 		 * Only Hades maps the PCI-config space!
754 		 */
755 		pci_conf_addr = ioaddr;
756 		ioaddr       += PCI_CONF_SIZE;
757 		pg            = &pt[pci_conf_addr / PAGE_SIZE];
758 		epg           = &pg[btoc(PCI_CONF_SIZE)];
759 		mask          = PCI_CONFM_PHYS;
760 		pg_proto      = PCI_CONFB_PHYS | PG_RW | PG_CI | PG_V;
761 		for(; pg < epg; mask <<= 1)
762 			*pg++ = pg_proto | mask;
763 	}
764 	else pci_conf_addr = 0; /* XXX: should crash */
765 
766 	if (machineid & (ATARI_HADES|ATARI_MILAN)) {
767 		pci_io_addr   = ioaddr;
768 		ioaddr       += PCI_IO_SIZE;
769 		pg	      = &pt[pci_io_addr / PAGE_SIZE];
770 		epg           = &pg[btoc(PCI_IO_SIZE)];
771 		pg_proto      = PCI_IO_PHYS | PG_RW | PG_CI | PG_V;
772 		while(pg < epg) {
773 			*pg++     = pg_proto;
774 			pg_proto += PAGE_SIZE;
775 		}
776 
777 		pci_mem_addr  = ioaddr;
778 		/* Provide an uncached PCI address for the MILAN */
779 		pci_mem_uncached = ioaddr;
780 		ioaddr       += PCI_MEM_SIZE;
781 		epg           = &pg[btoc(PCI_MEM_SIZE)];
782 		pg_proto      = PCI_VGA_PHYS | PG_RW | PG_CI | PG_V;
783 		while(pg < epg) {
784 			*pg++     = pg_proto;
785 			pg_proto += PAGE_SIZE;
786 		}
787 	}
788 
789 	bootm_init(ioaddr, pg, BOOTM_VA_POOL);
790 	/*
791 	 * ioaddr += BOOTM_VA_POOL;
792 	 * pg = &pg[btoc(BOOTM_VA_POOL)];
793 	 */
794 }
795 
796 /*
797  * Used by dumpconf() to get the size of the machine-dependent panic-dump
798  * header in disk blocks.
799  */
800 
801 #define CHDRSIZE (ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)))
802 #define MDHDRSIZE roundup(CHDRSIZE, dbtob(1))
803 
804 int
805 cpu_dumpsize()
806 {
807 
808 	return btodb(MDHDRSIZE);
809 }
810 
811 /*
812  * Called by dumpsys() to dump the machine-dependent header.
813  * XXX: Assumes that it will all fit in one diskblock.
814  */
815 int
816 cpu_dump(int (*dump)(dev_t, daddr_t, void *, size_t), daddr_t *p_blkno)
817 {
818 	int		buf[MDHDRSIZE/sizeof(int)];
819 	int		error;
820 	kcore_seg_t	*kseg_p;
821 	cpu_kcore_hdr_t	*chdr_p;
822 
823 	kseg_p = (kcore_seg_t *)buf;
824 	chdr_p = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*kseg_p)) / sizeof(int)];
825 
826 	/*
827 	 * Generate a segment header
828 	 */
829 	CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
830 	kseg_p->c_size = MDHDRSIZE - ALIGN(sizeof(*kseg_p));
831 
832 	/*
833 	 * Add the md header
834 	 */
835 	*chdr_p = cpu_kcore_hdr;
836 	error = dump(dumpdev, *p_blkno, (void *)buf, sizeof(buf));
837 	*p_blkno += btodb(sizeof(buf));
838 	return (error);
839 }
840 
841 #if (M68K_NPHYS_RAM_SEGS < NMEM_SEGS)
842 #error "Configuration error: M68K_NPHYS_RAM_SEGS < NMEM_SEGS"
843 #endif
844 /*
845  * Initialize the cpu_kcore_header.
846  */
847 static void
848 cpu_init_kcorehdr(kbase)
849 u_long	kbase;
850 {
851 	cpu_kcore_hdr_t *h = &cpu_kcore_hdr;
852 	struct m68k_kcore_hdr *m = &h->un._m68k;
853 	extern char end[];
854 	int	i;
855 
856 	bzero(&cpu_kcore_hdr, sizeof(cpu_kcore_hdr));
857 
858 	/*
859 	 * Initialize the `dispatcher' portion of the header.
860 	 */
861 	strcpy(h->name, machine);
862 	h->page_size = PAGE_SIZE;
863 	h->kernbase = KERNBASE;
864 
865 	/*
866 	 * Fill in information about our MMU configuration.
867 	 */
868 	m->mmutype	= mmutype;
869 	m->sg_v		= SG_V;
870 	m->sg_frame	= SG_FRAME;
871 	m->sg_ishift	= SG_ISHIFT;
872 	m->sg_pmask	= SG_PMASK;
873 	m->sg40_shift1	= SG4_SHIFT1;
874 	m->sg40_mask2	= SG4_MASK2;
875 	m->sg40_shift2	= SG4_SHIFT2;
876 	m->sg40_mask3	= SG4_MASK3;
877 	m->sg40_shift3	= SG4_SHIFT3;
878 	m->sg40_addr1	= SG4_ADDR1;
879 	m->sg40_addr2	= SG4_ADDR2;
880 	m->pg_v		= PG_V;
881 	m->pg_frame	= PG_FRAME;
882 
883 	/*
884 	 * Initialize pointer to kernel segment table.
885 	 */
886 	m->sysseg_pa = (u_int)Sysseg + kbase;
887 
888 	/*
889 	 * Initialize relocation value such that:
890 	 *
891 	 *	pa = (va - KERNBASE) + reloc
892 	 */
893 	m->reloc = kbase;
894 
895 	/*
896 	 * Define the end of the relocatable range.
897 	 */
898 	m->relocend = (u_int32_t)end;
899 
900 	for (i = 0; i < NMEM_SEGS; i++) {
901 		m->ram_segs[i].start = boot_segs[i].start;
902 		m->ram_segs[i].size  = boot_segs[i].end -
903 		    boot_segs[i].start;
904 	}
905 }
906 
907 void
908 mmu030_setup(sysseg, kstsize, pt, ptsize, sysptmap, sysptsize, kbase)
909 	st_entry_t	*sysseg;	/* System segment table		*/
910 	u_int		kstsize;	/* size of 'sysseg' in pages	*/
911 	pt_entry_t	*pt;		/* Kernel page table		*/
912 	u_int		ptsize;		/* size	of 'pt' in bytes	*/
913 	pt_entry_t	*sysptmap;	/* System page table		*/
914 	u_int		sysptsize;	/* size of 'sysptmap' in pages	*/
915 	u_int		kbase;
916 {
917 	st_entry_t	sg_proto, *sg;
918 	pt_entry_t	pg_proto, *pg, *epg;
919 
920 	sg_proto = ((u_int)pt + kbase) | SG_RW | SG_V;
921 	pg_proto = ((u_int)pt + kbase) | PG_RW | PG_CI | PG_V;
922 
923 	/*
924 	 * Map the page table pages in both the HW segment table
925 	 * and the software Sysptmap.  Note that Sysptmap is also
926 	 * considered a PT page, hence the +sysptsize.
927 	 */
928 	sg  = sysseg;
929 	pg  = sysptmap;
930 	epg = &pg[(ptsize >> PGSHIFT) + sysptsize];
931 	while(pg < epg) {
932 		*sg++ = sg_proto;
933 		*pg++ = pg_proto;
934 		sg_proto += PAGE_SIZE;
935 		pg_proto += PAGE_SIZE;
936 	}
937 
938 	/*
939 	 * invalidate the remainder of the tables
940 	 */
941 	epg = &sysptmap[sysptsize * NPTEPG];
942 	while(pg < epg) {
943 		*sg++ = SG_NV;
944 		*pg++ = PG_NV;
945 	}
946 }
947 
948 #if defined(M68040) || defined(M68060)
949 void
950 mmu040_setup(sysseg, kstsize, pt, ptsize, sysptmap, sysptsize, kbase)
951 	st_entry_t	*sysseg;	/* System segment table		*/
952 	u_int		kstsize;	/* size of 'sysseg' in pages	*/
953 	pt_entry_t	*pt;		/* Kernel page table		*/
954 	u_int		ptsize;		/* size	of 'pt' in bytes	*/
955 	pt_entry_t	*sysptmap;	/* System page table		*/
956 	u_int		sysptsize;	/* size of 'sysptmap' in pages	*/
957 	u_int		kbase;
958 {
959 	int		i;
960 	st_entry_t	sg_proto, *sg, *esg;
961 	pt_entry_t	pg_proto;
962 
963 	/*
964 	 * First invalidate the entire "segment table" pages
965 	 * (levels 1 and 2 have the same "invalid" values).
966 	 */
967 	sg  = sysseg;
968 	esg = &sg[kstsize * NPTEPG];
969 	while (sg < esg)
970 		*sg++ = SG_NV;
971 
972 	/*
973 	 * Initialize level 2 descriptors (which immediately
974 	 * follow the level 1 table). These should map 'pt' + 'sysptmap'.
975 	 * We need:
976 	 *	NPTEPG / SG4_LEV3SIZE
977 	 * level 2 descriptors to map each of the nptpages + 1
978 	 * pages of PTEs.  Note that we set the "used" bit
979 	 * now to save the HW the expense of doing it.
980 	 */
981 	i   = ((ptsize >> PGSHIFT) + sysptsize) * (NPTEPG / SG4_LEV3SIZE);
982 	sg  = &sysseg[SG4_LEV1SIZE];
983 	esg = &sg[i];
984 	sg_proto = ((u_int)pt + kbase) | SG_U | SG_RW | SG_V;
985 	while (sg < esg) {
986 		*sg++     = sg_proto;
987 		sg_proto += (SG4_LEV3SIZE * sizeof (st_entry_t));
988 	}
989 
990 	/*
991 	 * Initialize level 1 descriptors.  We need:
992 	 *	roundup(num, SG4_LEV2SIZE) / SG4_LEVEL2SIZE
993 	 * level 1 descriptors to map the 'num' level 2's.
994 	 */
995 	i = roundup(i, SG4_LEV2SIZE) / SG4_LEV2SIZE;
996 	protostfree = (-1 << (i + 1)) /* & ~(-1 << MAXKL2SIZE) */;
997 	sg  = sysseg;
998 	esg = &sg[i];
999 	sg_proto = ((u_int)&sg[SG4_LEV1SIZE] + kbase) | SG_U | SG_RW |SG_V;
1000 	while (sg < esg) {
1001 		*sg++     = sg_proto;
1002 		sg_proto += (SG4_LEV2SIZE * sizeof(st_entry_t));
1003 	}
1004 
1005 	/*
1006 	 * Initialize sysptmap
1007 	 */
1008 	sg  = sysptmap;
1009 	esg = &sg[(ptsize >> PGSHIFT) + sysptsize];
1010 	pg_proto = ((u_int)pt + kbase) | PG_RW | PG_CI | PG_V;
1011 	while (sg < esg) {
1012 		*sg++     = pg_proto;
1013 		pg_proto += PAGE_SIZE;
1014 	}
1015 	/*
1016 	 * Invalidate rest of Sysptmap page
1017 	 */
1018 	esg = &sysptmap[sysptsize * NPTEPG];
1019 	while (sg < esg)
1020 		*sg++ = SG_NV;
1021 }
1022 #endif /* M68040 */
1023 
1024 #if defined(M68060)
1025 int m68060_pcr_init = 0x21;	/* make this patchable */
1026 #endif
1027 
1028 static void
1029 initcpu()
1030 {
1031 	typedef void trapfun __P((void));
1032 
1033 	switch (cputype) {
1034 
1035 #if defined(M68060)
1036 	case CPU_68060:
1037 		{
1038 			extern trapfun	*vectab[256];
1039 			extern trapfun	buserr60, addrerr4060, fpfault;
1040 #if defined(M060SP)
1041 			extern u_int8_t FP_CALL_TOP[], I_CALL_TOP[];
1042 #else
1043 			extern trapfun illinst;
1044 #endif
1045 
1046 			__asm volatile ("movl %0,%%d0; .word 0x4e7b,0x0808" : :
1047 					"d"(m68060_pcr_init):"d0" );
1048 
1049 			/* bus/addrerr vectors */
1050 			vectab[2] = buserr60;
1051 			vectab[3] = addrerr4060;
1052 
1053 #if defined(M060SP)
1054 			/* integer support */
1055 			vectab[61] = (trapfun *)&I_CALL_TOP[128 + 0x00];
1056 
1057 			/* floating point support */
1058 			/*
1059 			 * XXX maybe we really should run-time check for the
1060 			 * stack frame format here:
1061 			 */
1062 			vectab[11] = (trapfun *)&FP_CALL_TOP[128 + 0x30];
1063 
1064 			vectab[55] = (trapfun *)&FP_CALL_TOP[128 + 0x38];
1065 			vectab[60] = (trapfun *)&FP_CALL_TOP[128 + 0x40];
1066 
1067 			vectab[54] = (trapfun *)&FP_CALL_TOP[128 + 0x00];
1068 			vectab[52] = (trapfun *)&FP_CALL_TOP[128 + 0x08];
1069 			vectab[53] = (trapfun *)&FP_CALL_TOP[128 + 0x10];
1070 			vectab[51] = (trapfun *)&FP_CALL_TOP[128 + 0x18];
1071 			vectab[50] = (trapfun *)&FP_CALL_TOP[128 + 0x20];
1072 			vectab[49] = (trapfun *)&FP_CALL_TOP[128 + 0x28];
1073 #else
1074 			vectab[61] = illinst;
1075 #endif
1076 			vectab[48] = fpfault;
1077 		}
1078 		break;
1079 #endif /* defined(M68060) */
1080 #if defined(M68040)
1081 	case CPU_68040:
1082 		{
1083 			extern trapfun	*vectab[256];
1084 			extern trapfun	buserr40, addrerr4060;
1085 
1086 			/* bus/addrerr vectors */
1087 			vectab[2] = buserr40;
1088 			vectab[3] = addrerr4060;
1089 		}
1090 		break;
1091 #endif /* defined(M68040) */
1092 #if defined(M68030) || defined(M68020)
1093 	case CPU_68030:
1094 	case CPU_68020:
1095 		{
1096 			extern trapfun	*vectab[256];
1097 			extern trapfun	buserr2030, addrerr2030;
1098 
1099 			/* bus/addrerr vectors */
1100 			vectab[2] = buserr2030;
1101 			vectab[3] = addrerr2030;
1102 		}
1103 		break;
1104 #endif /* defined(M68030) || defined(M68020) */
1105 	}
1106 
1107 	DCIS();
1108 }
1109 
1110 #ifdef DEBUG
1111 void dump_segtable __P((u_int *));
1112 void dump_pagetable __P((u_int *, u_int, u_int));
1113 u_int vmtophys __P((u_int *, u_int));
1114 
1115 void
1116 dump_segtable(stp)
1117 	u_int *stp;
1118 {
1119 	u_int *s, *es;
1120 	int shift, i;
1121 
1122 	s = stp;
1123 	{
1124 		es = s + (ATARI_STSIZE >> 2);
1125 		shift = SG_ISHIFT;
1126 	}
1127 
1128 	/*
1129 	 * XXX need changes for 68040
1130 	 */
1131 	for (i = 0; s < es; s++, i++)
1132 		if (*s & SG_V)
1133 			printf("$%08x: $%08x\t", i << shift, *s & SG_FRAME);
1134 	printf("\n");
1135 }
1136 
1137 void
1138 dump_pagetable(ptp, i, n)
1139 	u_int *ptp, i, n;
1140 {
1141 	u_int *p, *ep;
1142 
1143 	p = ptp + i;
1144 	ep = p + n;
1145 	for (; p < ep; p++, i++)
1146 		if (*p & PG_V)
1147 			printf("$%08x -> $%08x\t", i, *p & PG_FRAME);
1148 	printf("\n");
1149 }
1150 
1151 u_int
1152 vmtophys(ste, vm)
1153 	u_int *ste, vm;
1154 {
1155 	ste = (u_int *) (*(ste + (vm >> SEGSHIFT)) & SG_FRAME);
1156 		ste += (vm & SG_PMASK) >> PGSHIFT;
1157 	return((*ste & -PAGE_SIZE) | (vm & (PAGE_SIZE - 1)));
1158 }
1159 
1160 #endif
1161