xref: /netbsd-src/sys/arch/atari/atari/atari_init.c (revision 81b108b45f75f89f1e3ffad9fb6f074e771c0935)
1 /*	$NetBSD: atari_init.c,v 1.18 1996/09/16 06:28:41 leo 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/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <vm/vm.h>
39 #include <sys/user.h>
40 #include <sys/ioctl.h>
41 #include <sys/select.h>
42 #include <sys/tty.h>
43 #include <sys/proc.h>
44 #include <sys/buf.h>
45 #include <sys/msgbuf.h>
46 #include <sys/mbuf.h>
47 #include <sys/protosw.h>
48 #include <sys/domain.h>
49 #include <sys/dkbad.h>
50 #include <sys/reboot.h>
51 #include <sys/exec.h>
52 #include <sys/core.h>
53 #include <sys/kcore.h>
54 #include <vm/pmap.h>
55 #include <machine/vmparam.h>
56 #include <machine/pte.h>
57 #include <machine/cpu.h>
58 #include <machine/iomap.h>
59 #include <machine/mfp.h>
60 #include <machine/scu.h>
61 #include <machine/kcore.h>
62 #include <atari/atari/stalloc.h>
63 #include <atari/dev/ym2149reg.h>
64 
65 void start_c __P((int, u_int, u_int, u_int, char *));
66 static void cpu_init_kcorehdr __P((u_long));
67 static void mmu030_setup __P((st_entry_t *, u_int, pt_entry_t *, u_int,
68 			      pt_entry_t *, u_int, u_int));
69 static void map_io_areas __P((pt_entry_t *, u_int, u_int));
70 static void set_machtype __P((void));
71 
72 #if defined(M68040) || defined(M68060)
73 static void mmu040_setup __P((st_entry_t *, u_int, pt_entry_t *, u_int,
74 			      pt_entry_t *, u_int, u_int));
75 #endif
76 
77 /*
78  * All info needed to generate a panic dump. All fields are setup by
79  * start_c().
80  * XXX: Should sheck usage of phys_segs. There is some unwanted overlap
81  *      here.... Also, the name is badly choosen. Phys_segs contains the
82  *      segment descriptions _after_ reservations are made.
83  * XXX: 'lowram' is obsoleted by the new panicdump format
84  */
85 static cpu_kcore_hdr_t cpu_kcore_hdr;
86 
87 extern u_int 	lowram;
88 extern u_int	Sysptsize, Sysseg_pa, proc0paddr;
89 extern pt_entry_t *Sysptmap;
90 extern st_entry_t *Sysseg;
91 u_int		*Sysmap;
92 int		machineid, mmutype, cpu040, astpending;
93 char		*vmmap;
94 pv_entry_t	pv_table;
95 #if defined(M68040) || defined(M68060)
96 extern int	protostfree;
97 #endif
98 
99 extern char	*esym;
100 
101 /*
102  * This is the virtual address of physical page 0. Used by 'do_boot()'.
103  */
104 vm_offset_t	page_zero;
105 
106 /*
107  * Crude support for allocation in ST-ram. Currently only used to allocate
108  * video ram.
109  * The physical address is also returned because the video init needs it to
110  * setup the controller at the time the vm-system is not yet operational so
111  * 'kvtop()' cannot be used.
112  */
113 #ifndef ST_POOL_SIZE
114 #define	ST_POOL_SIZE	40			/* XXX: enough? */
115 #endif
116 
117 u_long	st_pool_size = ST_POOL_SIZE * NBPG;	/* Patchable	*/
118 u_long	st_pool_virt, st_pool_phys;
119 
120 /*
121  * this is the C-level entry function, it's called from locore.s.
122  * Preconditions:
123  *	Interrupts are disabled
124  *	PA == VA, we don't have to relocate addresses before enabling
125  *		the MMU
126  * 	Exec is no longer available (because we're loaded all over
127  *		low memory, no ExecBase is available anymore)
128  *
129  * It's purpose is:
130  *	Do the things that are done in locore.s in the hp300 version,
131  *		this includes allocation of kernel maps and enabling the MMU.
132  *
133  * Some of the code in here is `stolen' from Amiga MACH, and was
134  * written by Bryan Ford and Niklas Hallqvist.
135  *
136  * Very crude 68040 support by Michael L. Hitch.
137  */
138 
139 void
140 start_c(id, ttphystart, ttphysize, stphysize, esym_addr)
141 int	id;			/* Machine id				*/
142 u_int	ttphystart, ttphysize;	/* Start address and size of TT-ram	*/
143 u_int	stphysize;		/* Size of ST-ram	 		*/
144 char	*esym_addr;		/* Address of kernel '_esym' symbol	*/
145 {
146 	extern char	end[];
147 	extern void	etext __P((void));
148 	extern u_long	protorp[2];
149 	u_int		pstart;		/* Next available physical address*/
150 	u_int		vstart;		/* Next available virtual address */
151 	u_int		avail;
152 	pt_entry_t	*pt;
153 	u_int		ptsize, ptextra;
154 	u_int		tc, i;
155 	u_int		*pg;
156 	u_int		pg_proto;
157 	u_int		end_loaded;
158 	u_long		kbase;
159 	u_int		kstsize;
160 
161 	boot_segs[0].start       = 0;
162 	boot_segs[0].end         = stphysize;
163 	boot_segs[1].start       = ttphystart;
164 	boot_segs[1].end         = ttphystart + ttphysize;
165 	boot_segs[2].start = boot_segs[2].end = 0; /* End of segments! */
166 
167 	/*
168 	 * The following is a hack. We do not know how much ST memory we
169 	 * really need until after configuration has finished. At this
170 	 * time I have no idea how to grab ST memory at that time.
171 	 * The round_page() call is ment to correct errors made by
172 	 * binpatching!
173 	 */
174 	st_pool_size   = atari_round_page(st_pool_size);
175 	st_pool_phys   = stphysize - st_pool_size;
176 	stphysize      = st_pool_phys;
177 
178 	machineid      = id;
179 	esym           = esym_addr;
180 
181 	/*
182 	 * the kernel ends at end() or esym.
183 	 */
184 	if(esym == NULL)
185 		end_loaded = (u_int)end;
186 	else end_loaded = (u_int)esym;
187 
188 	/*
189 	 * If we have enough fast-memory to put the kernel in, do it!
190 	 */
191 	if(ttphysize >= end_loaded)
192 		kbase = ttphystart;
193 	else kbase = 0;
194 
195 	/*
196 	 * update these as soon as possible!
197 	 */
198 	PAGE_SIZE  = NBPG;
199 	PAGE_MASK  = NBPG-1;
200 	PAGE_SHIFT = PG_SHIFT;
201 
202 	/*
203 	 * Determine the type of machine we are running on. This needs
204 	 * to be done early!
205 	 */
206 	set_machtype();
207 
208 	/*
209 	 * We run the kernel from ST memory at the moment.
210 	 * The kernel segment table is put just behind the loaded image.
211 	 * pstart: start of usable ST memory
212 	 * avail : size of ST memory available.
213 	 */
214 	pstart = (u_int)end_loaded;
215 	pstart = atari_round_page(pstart);
216 	avail  = stphysize - pstart;
217 
218 	/*
219 	 * Calculate the number of pages needed for Sysseg.
220 	 * For the 68030, we need 256 descriptors (segment-table-entries).
221 	 * This easily fits into one page.
222 	 * For the 68040, both the level-1 and level-2 descriptors are
223 	 * stored into Sysseg. We currently handle a maximum sum of MAXKL2SIZE
224 	 * level-1 & level-2 tables.
225 	 */
226 #if defined(M68040) || defined(M68060)
227 	if (mmutype == MMU_68040)
228 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
229 	else
230 #endif
231 		kstsize = 1;
232 	/*
233 	 * allocate the kernel segment table
234 	 */
235 	Sysseg     = (st_entry_t *)pstart;
236 	Sysseg_pa  = (u_int)Sysseg + kbase;
237 	pstart    += kstsize * NBPG;
238 	avail     -= kstsize * NBPG;
239 
240 	/*
241 	 * Determine the number of pte's we need for extra's like
242 	 * ST I/O map's.
243 	 */
244 	ptextra = btoc(STIO_SIZE);
245 
246 	/*
247 	 * If present, add pci areas
248 	 */
249 	if (machineid & ATARI_HADES)
250 		ptextra += btoc(PCI_CONF_SIZE + PCI_IO_SIZE + PCI_MEM_SIZE);
251 
252 	/*
253 	 * The 'pt' (the initial kernel pagetable) has to map the kernel and
254 	 * the I/O areas. The various I/O areas are mapped (virtually) at
255 	 * the top of the address space mapped by 'pt' (ie. just below Sysmap).
256 	 */
257 	pt      = (pt_entry_t *)pstart;
258 	ptsize  = (Sysptsize + howmany(ptextra, NPTEPG)) << PGSHIFT;
259 	pstart += ptsize;
260 	avail  -= ptsize;
261 
262 	/*
263 	 * allocate kernel page table map
264 	 */
265 	Sysptmap = (pt_entry_t *)pstart;
266 	pstart  += NBPG;
267 	avail   -= NBPG;
268 
269 	/*
270 	 * Set Sysmap; mapped after page table pages. Because I too (LWP)
271 	 * didn't understand the reason for this, I borrowed the following
272 	 * (sligthly modified) comment from mac68k/locore.s:
273 	 * LAK:  There seems to be some confusion here about the next line,
274 	 * so I'll explain.  The kernel needs some way of dynamically modifying
275 	 * the page tables for its own virtual memory.  What it does is that it
276 	 * has a page table map.  This page table map is mapped right after the
277 	 * kernel itself (in our implementation; in HP's it was after the I/O
278 	 * space). Therefore, the first three (or so) entries in the segment
279 	 * table point to the first three pages of the page tables (which
280 	 * point to the kernel) and the next entry in the segment table points
281 	 * to the page table map (this is done later).  Therefore, the value
282 	 * of the pointer "Sysmap" will be something like 16M*3 = 48M.  When
283 	 * the kernel addresses this pointer (e.g., Sysmap[0]), it will get
284 	 * the first longword of the first page map (== pt[0]).  Since the
285 	 * page map mirrors the segment table, addressing any index of Sysmap
286 	 * will give you a PTE of the page maps which map the kernel.
287 	 */
288 	Sysmap = (u_int *)(ptsize << (SEGSHIFT - PGSHIFT));
289 
290 	/*
291 	 * Initialize segment tables
292 	 */
293 #if defined(M68040) || defined(M68060)
294 	if (mmutype == MMU_68040)
295 		mmu040_setup(Sysseg, kstsize, pt, ptsize, Sysptmap, 1, kbase);
296 	else
297 #endif /* defined(M68040) || defined(M68060) */
298 		mmu030_setup(Sysseg, kstsize, pt, ptsize, Sysptmap, 1, kbase);
299 
300 	/*
301 	 * initialize kernel page table page(s).
302 	 * Assume load at VA 0.
303 	 * - Text pages are RO
304 	 * - Page zero is invalid
305 	 */
306 	pg_proto = (0 + kbase) | PG_RO | PG_V;
307 	pg       = pt;
308 	*pg++ = PG_NV; pg_proto += NBPG;
309 	for(i = NBPG; i < (u_int)etext; i += NBPG, pg_proto += NBPG)
310 		*pg++ = pg_proto;
311 
312 	/*
313 	 * data, bss and dynamic tables are read/write
314 	 */
315 	pg_proto = (pg_proto & PG_FRAME) | PG_RW | PG_V;
316 
317 #if defined(M68040) || defined(M68060)
318 	/*
319 	 * Map the kernel segment table cache invalidated for
320 	 * these machines (for the 68040 not strictly necessary, but
321 	 * recommended by Motorola; for the 68060 mandatory)
322 	 */
323 	if (mmutype == MMU_68040) {
324 	    for (; i < (u_int)Sysseg; i += NBPG, pg_proto += NBPG)
325 		*pg++ = pg_proto;
326 	    pg_proto = (pg_proto & ~PG_CCB) | PG_CI;
327 	    for (; i < (u_int)&Sysseg[kstsize * NPTEPG]; i += NBPG,
328 							 pg_proto += NBPG)
329 		*pg++ = pg_proto;
330 	    pg_proto = (pg_proto & ~PG_CI) | PG_CCB;
331 	}
332 #endif /* defined(M68040) || defined(M68060) */
333 
334 	/*
335 	 * go till end of data allocated so far
336 	 * plus proc0 u-area (to be allocated)
337 	 */
338 	for(; i < pstart + USPACE; i += NBPG, pg_proto += NBPG)
339 		*pg++ = pg_proto;
340 
341 	/*
342 	 * invalidate remainder of kernel PT
343 	 */
344 	while(pg < &pt[ptsize/sizeof(pt_entry_t)])
345 		*pg++ = PG_NV;
346 
347 	/*
348 	 * Map various I/O areas
349 	 */
350 	map_io_areas(pt, ptsize, ptextra);
351 
352 	/*
353 	 * Clear proc0 user-area
354 	 */
355 	bzero((u_char *)pstart, USPACE);
356 
357 	/*
358 	 * Save KVA of proc0 user-area and allocate it
359 	 */
360 	proc0paddr = pstart;
361 	pstart    += USPACE;
362 	avail     -= USPACE;
363 
364 	/*
365 	 * At this point, virtual and physical allocation starts to divert.
366 	 */
367 	vstart     = pstart;
368 
369 	/*
370 	 * Map the allocated space in ST-ram now. In the contig-case, there
371 	 * is no need to make a distinction between virtual and physical
372 	 * adresses. But I make it anyway to be prepared.
373 	 * Physcal space is already reserved!
374 	 */
375 	st_pool_virt = vstart;
376 	pg           = &pt[vstart / NBPG];
377 	pg_proto     = st_pool_phys | PG_RW | PG_CI | PG_V;
378 	vstart      += st_pool_size;
379 	while(pg_proto < (st_pool_phys + st_pool_size)) {
380 		*pg++     = pg_proto;
381 		pg_proto += NBPG;
382 	}
383 
384 	/*
385 	 * Map physical page_zero and page-zero+1 (First ST-ram page). We need
386 	 * to reference it in the reboot code. Two pages are mapped, because
387 	 * we must make sure 'doboot()' is contained in it (see the tricky
388 	 * copying there....).
389 	 */
390 	page_zero  = vstart;
391 	pg         = &pt[vstart / NBPG];
392 	*pg++      = PG_RW | PG_CI | PG_V;
393 	vstart    += NBPG;
394 	*pg        = PG_RW | PG_CI | PG_V | NBPG;
395 	vstart    += NBPG;
396 
397 	lowram  = 0 >> PGSHIFT; /* XXX */
398 
399 	/*
400 	 * Fill in usable segments. The page indexes will be initialized
401 	 * later when all reservations are made.
402 	 */
403 	usable_segs[0].start = 0;
404 	usable_segs[0].end   = stphysize;
405 	usable_segs[1].start = ttphystart;
406 	usable_segs[1].end   = ttphystart + ttphysize;
407 	usable_segs[2].start = usable_segs[2].end = 0; /* End of segments! */
408 
409 	if(kbase) {
410 		/*
411 		 * First page of ST-ram is unusable, reserve the space
412 		 * for the kernel in the TT-ram segment.
413 		 * Note: Because physical page-zero is partially mapped to ROM
414 		 *       by hardware, it is unusable.
415 		 */
416 		usable_segs[0].start  = NBPG;
417 		usable_segs[1].start += pstart;
418 	}
419 	else usable_segs[0].start += pstart;
420 
421 	/*
422 	 * As all segment sizes are now valid, calculate page indexes and
423 	 * available physical memory.
424 	 */
425 	usable_segs[0].first_page = 0;
426 	for (i = 1; usable_segs[i].start; i++) {
427 		usable_segs[i].first_page  = usable_segs[i-1].first_page;
428 		usable_segs[i].first_page +=
429 			(usable_segs[i-1].end - usable_segs[i-1].start) / NBPG;
430 	}
431 	for (i = 0, physmem = 0; usable_segs[i].start; i++)
432 		physmem += usable_segs[i].end - usable_segs[i].start;
433 	physmem >>= PGSHIFT;
434 
435 	/*
436 	 * get the pmap module in sync with reality.
437 	 */
438 	pmap_bootstrap(vstart, stio_addr, ptextra);
439 
440 	/*
441 	 * Prepare to enable the MMU.
442 	 * Setup and load SRP nolimit, share global, 4 byte PTE's
443 	 */
444 	protorp[0] = 0x80000202;
445 	protorp[1] = (u_int)Sysseg + kbase;	/* + segtable address */
446 	Sysseg_pa  = (u_int)Sysseg + kbase;
447 
448 	cpu_init_kcorehdr(kbase);
449 
450 	/*
451 	 * copy over the kernel (and all now initialized variables)
452 	 * to fastram.  DONT use bcopy(), this beast is much larger
453 	 * than 128k !
454 	 */
455 	if(kbase) {
456 		register u_long	*lp, *le, *fp;
457 
458 		lp = (u_long *)0;
459 		le = (u_long *)pstart;
460 		fp = (u_long *)kbase;
461 		while(lp < le)
462 			*fp++ = *lp++;
463 	}
464 #if defined(M68040) || defined(M68060)
465 	if (mmutype == MMU_68040) {
466 		/*
467 		 * movel Sysseg_pa,a0;
468 		 * movec a0,SRP;
469 		 * pflusha;
470 		 * movel #$0xc000,d0;
471 		 * movec d0,TC
472 		 */
473 		asm volatile ("movel %0,a0;.word 0x4e7b,0x8807" : : "a" (Sysseg_pa) : "a0");
474 		asm volatile (".word 0xf518" : : );
475 		asm volatile ("movel #0xc000,d0; .word 0x4e7b,0x0003" : : :"d0" );
476 	} else
477 #endif
478 	{
479 		asm volatile ("pmove %0@,srp" : : "a" (&protorp[0]));
480 		/*
481 		 * setup and load TC register.
482 		 * enable_cpr, enable_srp, pagesize=8k,
483 		 * A = 8 bits, B = 11 bits
484 		 */
485 		tc = 0x82d08b00;
486 		asm volatile ("pmove %0@,tc" : : "a" (&tc));
487 	}
488 
489 	/* Is this to fool the optimizer?? */
490 	i = *(int *)proc0paddr;
491 	*(volatile int *)proc0paddr = i;
492 
493 	ym2149_init();
494 
495 	/*
496 	 * Initialize both MFP chips (if both present!) to generate
497 	 * auto-vectored interrupts with EOI. The active-edge registers are
498 	 * set up. The interrupt enable registers are set to disable all
499 	 * interrupts.
500 	 * A test on presence on the second MFP determines if this is a
501 	 * TT030 or a Falcon. This is added to 'machineid'.
502 	 */
503 	MFP->mf_iera  = MFP->mf_ierb = 0;
504 	MFP->mf_imra  = MFP->mf_imrb = 0;
505 	MFP->mf_aer   = MFP->mf_ddr  = 0;
506 	MFP->mf_vr    = 0x40;
507 	if(machineid & (ATARI_TT|ATARI_HADES)) {
508 		MFP2->mf_iera = MFP2->mf_ierb = 0;
509 		MFP2->mf_imra = MFP2->mf_imrb = 0;
510 		MFP2->mf_aer  = 0x80;
511 		MFP2->mf_vr   = 0x50;
512 	}
513 	if(machineid & ATARI_TT) {
514 		/*
515 		 * Initialize the SCU, to enable interrupts on the SCC (ipl5),
516 		 * MFP (ipl6) and softints (ipl1).
517 		 */
518 		SCU->sys_mask = SCU_MFP | SCU_SCC | SCU_SYS_SOFT;
519 #ifdef DDB
520 		/*
521 		 * This allows people with the correct hardware modification
522 		 * to drop into the debugger from an NMI.
523 		 */
524 		SCU->sys_mask |= SCU_IRQ7;
525 #endif
526 
527 	}
528 
529 	/*
530 	 * Initialize stmem allocator
531 	 */
532 	init_stmem();
533 }
534 
535 /*
536  * Try to figure out on what type of machine we are running
537  * Note: This module runs *before*
538  */
539 static void
540 set_machtype()
541 {
542 	if(!badbaddr((caddr_t)(PCI_CONFB_PHYS + PCI_CONFM_PHYS)))
543 		machineid |= ATARI_HADES;
544 	else {
545 		if(!badbaddr((caddr_t)&MFP2->mf_gpip))
546 			machineid |= ATARI_TT;
547 		else machineid |= ATARI_FALCON;
548 	}
549 }
550 
551 /*
552  * Do the dull work of mapping the various I/O areas. They MUST be Cache
553  * inhibited!
554  * All I/O areas are virtually mapped at the end of the pt-table.
555  */
556 static void
557 map_io_areas(pt, ptsize, ptextra)
558 pt_entry_t	*pt;
559 u_int		ptsize;		/* Size of 'pt' in bytes	*/
560 u_int		ptextra;	/* #of additional I/O pte's	*/
561 {
562 	vm_offset_t	ioaddr;
563 	pt_entry_t	*pg, *epg;
564 	pt_entry_t	pg_proto;
565 	u_long		mask;
566 
567 	ioaddr = ((ptsize / sizeof(pt_entry_t)) - ptextra) * NBPG;
568 
569 	/*
570 	 * Map ST-IO area
571 	 */
572 	stio_addr = ioaddr;
573 	ioaddr   += STIO_SIZE;
574 	pg        = &pt[stio_addr / NBPG];
575 	epg       = &pg[btoc(STIO_SIZE)];
576 	pg_proto  = STIO_PHYS | PG_RW | PG_CI | PG_V;
577 	while(pg < epg) {
578 		*pg++     = pg_proto;
579 		pg_proto += NBPG;
580 	}
581 
582 	/*
583 	 * Map PCI areas
584 	 */
585 	if (machineid & ATARI_HADES) {
586 
587 		pci_conf_addr = ioaddr;
588 		ioaddr       += PCI_CONF_SIZE;
589 		pg            = &pt[pci_conf_addr / NBPG];
590 		epg           = &pg[btoc(PCI_CONF_SIZE)];
591 		mask          = PCI_CONFM_PHYS;
592 		pg_proto      = PCI_CONFB_PHYS | PG_RW | PG_CI | PG_V;
593 		for(; pg < epg; mask >>= 1)
594 			*pg++ = pg_proto | mask;
595 
596 		pci_io_addr   = ioaddr;
597 		ioaddr       += PCI_IO_SIZE;
598 		epg           = &pg[btoc(PCI_IO_SIZE)];
599 		pg_proto      = PCI_IO_PHYS | PG_RW | PG_CI | PG_V;
600 		while(pg < epg) {
601 			*pg++     = pg_proto;
602 			pg_proto += NBPG;
603 		}
604 
605 		pci_mem_addr  = ioaddr;
606 		ioaddr       += PCI_MEM_SIZE;
607 		epg           = &pg[btoc(PCI_MEM_SIZE)];
608 		pg_proto      = PCI_MEM_PHYS | PG_RW | PG_CI | PG_V;
609 		while(pg < epg) {
610 			*pg++     = pg_proto;
611 			pg_proto += NBPG;
612 		}
613 	}
614 }
615 
616 /*
617  * Used by dumpconf() to get the size of the machine-dependent panic-dump
618  * header in disk blocks.
619  */
620 int
621 cpu_dumpsize()
622 {
623 	int	size;
624 
625 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t));
626 	return (btodb(roundup(size, dbtob(1))));
627 }
628 
629 /*
630  * Called by dumpsys() to dump the machine-dependent header.
631  * XXX: Assumes that it will all fit in one diskblock.
632  */
633 int
634 cpu_dump(dump, p_blkno)
635 int	(*dump) __P((dev_t, daddr_t, caddr_t, size_t));
636 daddr_t	*p_blkno;
637 {
638 	int		buf[dbtob(1)/sizeof(int)];
639 	int		error;
640 	kcore_seg_t	*kseg_p;
641 	cpu_kcore_hdr_t	*chdr_p;
642 
643 	kseg_p = (kcore_seg_t *)buf;
644 	chdr_p = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*kseg_p)) / sizeof(int)];
645 
646 	/*
647 	 * Generate a segment header
648 	 */
649 	CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
650 	kseg_p->c_size = dbtob(1) - ALIGN(sizeof(*kseg_p));
651 
652 	/*
653 	 * Add the md header
654 	 */
655 	*chdr_p = cpu_kcore_hdr;
656 	error = dump(dumpdev, *p_blkno, (caddr_t)buf, dbtob(1));
657 	*p_blkno += 1;
658 	return (error);
659 }
660 
661 #if (NPHYS_RAM_SEGS < NMEM_SEGS)
662 #error "Configuration error: NPHYS_RAM_SEGS < NMEM_SEGS"
663 #endif
664 /*
665  * Initialize the cpu_kcore_header.
666  */
667 static void
668 cpu_init_kcorehdr(kbase)
669 u_long	kbase;
670 {
671 	int	i;
672 
673 	for (i = 0; i < NMEM_SEGS; i++) {
674 		cpu_kcore_hdr.ram_segs[i].start = boot_segs[i].start;
675 		cpu_kcore_hdr.ram_segs[i].size  = boot_segs[i].end
676 							- boot_segs[i].start;
677 	}
678 	cpu_kcore_hdr.mmutype   = mmutype;
679 	cpu_kcore_hdr.kernel_pa = kbase;
680 	cpu_kcore_hdr.sysseg_pa = (st_entry_t *)((u_int)Sysseg + kbase);
681 }
682 
683 void
684 mmu030_setup(sysseg, kstsize, pt, ptsize, sysptmap, sysptsize, kbase)
685 	st_entry_t	*sysseg;	/* System segment table		*/
686 	u_int		kstsize;	/* size of 'sysseg' in pages	*/
687 	pt_entry_t	*pt;		/* Kernel page table		*/
688 	u_int		ptsize;		/* size	of 'pt' in bytes	*/
689 	pt_entry_t	*sysptmap;	/* System page table		*/
690 	u_int		sysptsize;	/* size of 'sysptmap' in pages	*/
691 	u_int		kbase;
692 {
693 	st_entry_t	sg_proto, *sg;
694 	pt_entry_t	pg_proto, *pg, *epg;
695 
696 	sg_proto = ((u_int)pt + kbase) | SG_RW | SG_V;
697 	pg_proto = ((u_int)pt + kbase) | PG_RW | PG_CI | PG_V;
698 
699 	/*
700 	 * Map the page table pages in both the HW segment table
701 	 * and the software Sysptmap.  Note that Sysptmap is also
702 	 * considered a PT page, hence the +sysptsize.
703 	 */
704 	sg  = sysseg;
705 	pg  = sysptmap;
706 	epg = &pg[(ptsize >> PGSHIFT) + sysptsize];
707 	while(pg < epg) {
708 		*sg++ = sg_proto;
709 		*pg++ = pg_proto;
710 		sg_proto += NBPG;
711 		pg_proto += NBPG;
712 	}
713 
714 	/*
715 	 * invalidate the remainder of the tables
716 	 */
717 	epg = &sysptmap[sysptsize * NPTEPG];
718 	while(pg < epg) {
719 		*sg++ = SG_NV;
720 		*pg++ = PG_NV;
721 	}
722 }
723 
724 #if defined(M68040) || defined(M68060)
725 void
726 mmu040_setup(sysseg, kstsize, pt, ptsize, sysptmap, sysptsize, kbase)
727 	st_entry_t	*sysseg;	/* System segment table		*/
728 	u_int		kstsize;	/* size of 'sysseg' in pages	*/
729 	pt_entry_t	*pt;		/* Kernel page table		*/
730 	u_int		ptsize;		/* size	of 'pt' in bytes	*/
731 	pt_entry_t	*sysptmap;	/* System page table		*/
732 	u_int		sysptsize;	/* size of 'sysptmap' in pages	*/
733 	u_int		kbase;
734 {
735 	int		i;
736 	st_entry_t	sg_proto, *sg, *esg;
737 	pt_entry_t	pg_proto;
738 
739 	/*
740 	 * First invalidate the entire "segment table" pages
741 	 * (levels 1 and 2 have the same "invalid" values).
742 	 */
743 	sg  = sysseg;
744 	esg = &sg[kstsize * NPTEPG];
745 	while (sg < esg)
746 		*sg++ = SG_NV;
747 
748 	/*
749 	 * Initialize level 2 descriptors (which immediately
750 	 * follow the level 1 table). These should map 'pt' + 'sysptmap'.
751 	 * We need:
752 	 *	NPTEPG / SG4_LEV3SIZE
753 	 * level 2 descriptors to map each of the nptpages + 1
754 	 * pages of PTEs.  Note that we set the "used" bit
755 	 * now to save the HW the expense of doing it.
756 	 */
757 	i   = ((ptsize >> PGSHIFT) + sysptsize) * (NPTEPG / SG4_LEV3SIZE);
758 	sg  = &sysseg[SG4_LEV1SIZE];
759 	esg = &sg[i];
760 	sg_proto = ((u_int)pt + kbase) | SG_U | SG_RW | SG_V;
761 	while (sg < esg) {
762 		*sg++     = sg_proto;
763 		sg_proto += (SG4_LEV3SIZE * sizeof (st_entry_t));
764 	}
765 
766 	/*
767 	 * Initialize level 1 descriptors.  We need:
768 	 *	roundup(num, SG4_LEV2SIZE) / SG4_LEVEL2SIZE
769 	 * level 1 descriptors to map the 'num' level 2's.
770 	 */
771 	i = roundup(i, SG4_LEV2SIZE) / SG4_LEV2SIZE;
772 	protostfree = (-1 << (i + 1)) /* & ~(-1 << MAXKL2SIZE) */;
773 	sg  = sysseg;
774 	esg = &sg[i];
775 	sg_proto = ((u_int)&sg[SG4_LEV1SIZE] + kbase) | SG_U | SG_RW |SG_V;
776 	while (sg < esg) {
777 		*sg++     = sg_proto;
778 		sg_proto += (SG4_LEV2SIZE * sizeof(st_entry_t));
779 	}
780 
781 	/*
782 	 * Initialize sysptmap
783 	 */
784 	sg  = sysptmap;
785 	esg = &sg[(ptsize >> PGSHIFT) + sysptsize];
786 	pg_proto = ((u_int)pt + kbase) | PG_RW | PG_CI | PG_V;
787 	while (sg < esg) {
788 		*sg++     = pg_proto;
789 		pg_proto += NBPG;
790 	}
791 	/*
792 	 * Invalidate rest of Sysptmap page
793 	 */
794 	esg = &sysptmap[sysptsize * NPTEPG];
795 	while (sg < esg)
796 		*sg++ = SG_NV;
797 }
798 #endif /* M68040 */
799 
800 #ifdef DEBUG
801 void
802 dump_segtable(stp)
803 	u_int *stp;
804 {
805 	u_int *s, *es;
806 	int shift, i;
807 
808 	s = stp;
809 	{
810 		es = s + (ATARI_STSIZE >> 2);
811 		shift = SG_ISHIFT;
812 	}
813 
814 	/*
815 	 * XXX need changes for 68040
816 	 */
817 	for (i = 0; s < es; s++, i++)
818 		if (*s & SG_V)
819 			printf("$%08lx: $%08lx\t", i << shift, *s & SG_FRAME);
820 	printf("\n");
821 }
822 
823 void
824 dump_pagetable(ptp, i, n)
825 	u_int *ptp, i, n;
826 {
827 	u_int *p, *ep;
828 
829 	p = ptp + i;
830 	ep = p + n;
831 	for (; p < ep; p++, i++)
832 		if (*p & PG_V)
833 			printf("$%08lx -> $%08lx\t", i, *p & PG_FRAME);
834 	printf("\n");
835 }
836 
837 u_int
838 vmtophys(ste, vm)
839 	u_int *ste, vm;
840 {
841 	ste = (u_int *) (*(ste + (vm >> SEGSHIFT)) & SG_FRAME);
842 		ste += (vm & SG_PMASK) >> PGSHIFT;
843 	return((*ste & -NBPG) | (vm & (NBPG - 1)));
844 }
845 
846 #endif
847