1 /*	$NetBSD: autoconf.c,v 1.259 2015/10/04 08:15:46 joerg Exp $ */
2 
3 /*
4  * Copyright (c) 1996
5  *    The President and Fellows of Harvard College. All rights reserved.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  *	This product includes software developed by Harvard University.
16  *	This product includes software developed by the University of
17  *	California, Lawrence Berkeley Laboratory.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *	This product includes software developed by the University of
30  *	California, Berkeley and its contributors.
31  * 4. Neither the name of the University nor the names of its contributors
32  *    may be used to endorse or promote products derived from this software
33  *    without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  *
47  *	@(#)autoconf.c	8.4 (Berkeley) 10/1/93
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.259 2015/10/04 08:15:46 joerg Exp $");
52 
53 #include "opt_ddb.h"
54 #include "opt_kgdb.h"
55 #include "opt_modular.h"
56 #include "opt_multiprocessor.h"
57 #include "opt_sparc_arch.h"
58 
59 #include "scsibus.h"
60 
61 #include <sys/param.h>
62 #include <sys/kernel.h>
63 #include <sys/systm.h>
64 #include <sys/endian.h>
65 #include <sys/proc.h>
66 #include <sys/buf.h>
67 #include <sys/disklabel.h>
68 #include <sys/device.h>
69 #include <sys/disk.h>
70 #include <sys/conf.h>
71 #include <sys/reboot.h>
72 #include <sys/socket.h>
73 #include <sys/malloc.h>
74 #include <sys/queue.h>
75 #include <sys/msgbuf.h>
76 #include <sys/boot_flag.h>
77 #include <sys/ksyms.h>
78 #include <sys/userconf.h>
79 
80 #include <net/if.h>
81 #include <net/if_ether.h>
82 
83 #include <dev/cons.h>
84 
85 #include <uvm/uvm_extern.h>
86 
87 #include <machine/pcb.h>
88 #include <sys/bus.h>
89 #include <machine/promlib.h>
90 #include <machine/autoconf.h>
91 #include <machine/bootinfo.h>
92 
93 #include <sparc/sparc/memreg.h>
94 #include <machine/cpu.h>
95 #include <machine/ctlreg.h>
96 #include <sparc/sparc/asm.h>
97 #include <sparc/sparc/cpuvar.h>
98 #include <sparc/sparc/timerreg.h>
99 
100 #include <dev/pci/pcireg.h>
101 #include <dev/pci/pcidevs.h>
102 #include <dev/pci/pcivar.h>
103 #include <sparc/sparc/msiiepreg.h>
104 #ifdef MSIIEP
105 #include <sparc/sparc/pci_fixup.h>
106 #endif
107 
108 #ifdef DDB
109 #include <machine/db_machdep.h>
110 #include <ddb/db_sym.h>
111 #include <ddb/db_extern.h>
112 #include <ddb/ddbvar.h>
113 #endif
114 
115 #include "ksyms.h"
116 
117 /*
118  * The following several variables are related to
119  * the configuration process, and are used in initializing
120  * the machine.
121  */
122 
123 #ifdef KGDB
124 extern	int kgdb_debug_panic;
125 #endif
126 extern void *bootinfo;
127 
128 #if !NKSYMS && !defined(DDB) && !defined(MODULAR)
129 void bootinfo_relocate(void *);
130 #endif
131 
132 static	const char *str2hex(const char *, int *);
133 static	int mbprint(void *, const char *);
134 static	void crazymap(const char *, int *);
135 int	st_crazymap(int);
136 int	sd_crazymap(int);
137 void	sync_crash(void);
138 int	mainbus_match(device_t, cfdata_t, void *);
139 static	void mainbus_attach(device_t, device_t, void *);
140 
141 struct	bootpath bootpath[8];
142 int	nbootpath;
143 static	void bootpath_build(void);
144 static	void bootpath_fake(struct bootpath *, const char *);
145 static	void bootpath_print(struct bootpath *);
146 static	struct bootpath	*bootpath_store(int, struct bootpath *);
147 int	find_cpus(void);
148 char	machine_model[100];
149 
150 #ifdef DEBUG
151 #define ACDB_BOOTDEV	0x1
152 #define	ACDB_PROBE	0x2
153 int autoconf_debug = 0;
154 #define DPRINTF(l, s)   do { if (autoconf_debug & l) printf s; } while (0)
155 #else
156 #define DPRINTF(l, s)
157 #endif
158 
159 /*
160  * Most configuration on the SPARC is done by matching OPENPROM Forth
161  * device names with our internal names.
162  */
163 int
matchbyname(device_t parent,cfdata_t cf,void * aux)164 matchbyname(device_t parent, cfdata_t cf, void *aux)
165 {
166 
167 	printf("%s: WARNING: matchbyname\n", cf->cf_name);
168 	return (0);
169 }
170 
171 /*
172  * Get the number of CPUs in the system and the CPUs' SPARC architecture
173  * version. We need this information early in the boot process.
174  */
175 int
find_cpus(void)176 find_cpus(void)
177 {
178 	int n;
179 #if defined(SUN4M) || defined(SUN4D)
180 	int node;
181 #endif
182 	/*
183 	 * Set default processor architecture version
184 	 *
185 	 * All sun4 and sun4c platforms have v7 CPUs;
186 	 * sun4m may have v7 (Cyrus CY7C601 modules) or v8 CPUs (all
187 	 * other models, presumably).
188 	 */
189 	cpu_arch = 7;
190 
191 	/* On sun4 and sun4c we support only one CPU */
192 	if (!CPU_ISSUN4M && !CPU_ISSUN4D)
193 		return (1);
194 
195 	n = 0;
196 #if defined(SUN4M)
197 	node = findroot();
198 	for (node = firstchild(node); node; node = nextsibling(node)) {
199 		if (strcmp(prom_getpropstring(node, "device_type"), "cpu") != 0)
200 			continue;
201 		if (n++ == 0)
202 			cpu_arch = prom_getpropint(node, "sparc-version", 7);
203 	}
204 #endif /* SUN4M */
205 #if defined(SUN4D)
206 	node = findroot();
207 	for (node = firstchild(node); node; node = nextsibling(node)) {
208 		int unode;
209 
210 		if (strcmp(prom_getpropstring(node, "name"), "cpu-unit") != 0)
211 				continue;
212 		for (unode = firstchild(node); unode;
213 		     unode = nextsibling(unode)) {
214 			if (strcmp(prom_getpropstring(unode, "device_type"),
215 				   "cpu") != 0)
216 				continue;
217 			if (n++ == 0)
218 				cpu_arch = prom_getpropint(unode,
219 							   "sparc-version", 7);
220 		}
221 	}
222 #endif
223 
224 	return (n);
225 }
226 
227 /*
228  * Convert hex ASCII string to a value.  Returns updated pointer.
229  * Depends on ASCII order (this *is* machine-dependent code, you know).
230  */
231 static const char *
str2hex(const char * str,int * vp)232 str2hex(const char *str, int *vp)
233 {
234 	int v, c;
235 
236 	for (v = 0;; v = v * 16 + c, str++) {
237 		c = (u_char)*str;
238 		if (c <= '9') {
239 			if ((c -= '0') < 0)
240 				break;
241 		} else if (c <= 'F') {
242 			if ((c -= 'A' - 10) < 10)
243 				break;
244 		} else if (c <= 'f') {
245 			if ((c -= 'a' - 10) < 10)
246 				break;
247 		} else
248 			break;
249 	}
250 	*vp = v;
251 	return (str);
252 }
253 
254 
255 #if defined(SUN4M)
256 #if !defined(MSIIEP)
257 static void bootstrap4m(void);
258 #else
259 static void bootstrapIIep(void);
260 #endif
261 #endif /* SUN4M */
262 
263 /*
264  * locore.s code calls bootstrap() just before calling main(), after double
265  * mapping the kernel to high memory and setting up the trap base register.
266  * We must finish mapping the kernel properly and glean any bootstrap info.
267  */
268 void
bootstrap(void)269 bootstrap(void)
270 {
271 	extern uint8_t u0[];
272 	extern struct consdev consdev_prom;
273 
274 #if NKSYMS || defined(DDB) || defined(MODULAR)
275 	struct btinfo_symtab *bi_sym;
276 #else
277 	extern int end[];
278 #endif
279 	struct btinfo_boothowto *bi_howto;
280 
281 	cn_tab = &consdev_prom;
282 	prom_init();
283 
284 	/* Find the number of CPUs as early as possible */
285 	sparc_ncpus = find_cpus();
286 	uvm_lwp_setuarea(&lwp0, (vaddr_t)u0);
287 
288 	cpuinfo.master = 1;
289 	getcpuinfo(&cpuinfo, 0);
290 	curlwp = &lwp0;
291 
292 #if defined(SUN4M) || defined(SUN4D)
293 	/* Switch to sparc v8 multiply/divide functions on v8 machines */
294 	if (cpu_arch == 8) {
295 		extern void sparc_v8_muldiv(void);
296 		sparc_v8_muldiv();
297 	}
298 #endif /* SUN4M || SUN4D */
299 
300 #if !NKSYMS && !defined(DDB) && !defined(MODULAR)
301 	/*
302 	 * We want to reuse the memory where the symbols were stored
303 	 * by the loader. Relocate the bootinfo array which is loaded
304 	 * above the symbols (we assume) to the start of BSS. Then
305 	 * adjust kernel_top accordingly.
306 	 */
307 
308 	bootinfo_relocate((void *)ALIGN((u_int)end));
309 #endif
310 
311 	pmap_bootstrap(cpuinfo.mmu_ncontext,
312 		       cpuinfo.mmu_nregion,
313 		       cpuinfo.mmu_nsegment);
314 
315 #if !defined(MSGBUFSIZE) || MSGBUFSIZE == 8192
316 	/*
317 	 * Now that the kernel map has been set up, we can enable
318 	 * the message buffer at the first physical page in the
319 	 * memory bank where we were loaded. There are 8192
320 	 * bytes available for the buffer at this location (see the
321 	 * comment in locore.s at the top of the .text segment).
322 	 */
323 	initmsgbuf((void *)KERNBASE, 8192);
324 #endif
325 
326 #if defined(SUN4M)
327 	/*
328 	 * sun4m bootstrap is complex and is totally different for "normal" 4m
329 	 * and for microSPARC-IIep - so it's split into separate functions.
330 	 */
331 	if (CPU_ISSUN4M) {
332 #if !defined(MSIIEP)
333 		bootstrap4m();
334 #else
335 		bootstrapIIep();
336 #endif
337 	}
338 #endif /* SUN4M */
339 
340 #if defined(SUN4) || defined(SUN4C)
341 	if (CPU_ISSUN4 || CPU_ISSUN4C) {
342 		/* Map Interrupt Enable Register */
343 		pmap_kenter_pa(INTRREG_VA,
344 		    INT_ENABLE_REG_PHYSADR | PMAP_NC | PMAP_OBIO,
345 		    VM_PROT_READ | VM_PROT_WRITE, 0);
346 		pmap_update(pmap_kernel());
347 		/* Disable all interrupts */
348 		*((unsigned char *)INTRREG_VA) = 0;
349 	}
350 #endif /* SUN4 || SUN4C */
351 
352 #if NKSYMS || defined(DDB) || defined(MODULAR)
353 	if ((bi_sym = lookup_bootinfo(BTINFO_SYMTAB)) != NULL) {
354 		if (bi_sym->ssym < KERNBASE) {
355 			/* Assume low-loading boot loader */
356 			bi_sym->ssym += KERNBASE;
357 			bi_sym->esym += KERNBASE;
358 		}
359 		ksyms_addsyms_elf(bi_sym->nsym, (void*)bi_sym->ssym,
360 		    (void*)bi_sym->esym);
361 	}
362 #endif
363 
364 	if ((bi_howto = lookup_bootinfo(BTINFO_BOOTHOWTO)) != NULL) {
365 		boothowto = bi_howto->boothowto;
366 	}
367 }
368 
369 #if defined(SUN4M) && !defined(MSIIEP)
370 /*
371  * On sun4ms we have to do some nasty stuff here. We need to map
372  * in the interrupt registers (since we need to find out where
373  * they are from the PROM, since they aren't in a fixed place), and
374  * disable all interrupts. We can't do this easily from locore
375  * since the PROM is ugly to use from assembly. We also need to map
376  * in the counter registers because we can't disable the level 14
377  * (statclock) interrupt, so we need a handler early on (ugh).
378  *
379  * NOTE: We *demand* the psl to stay at splhigh() at least until
380  * we get here. The system _cannot_ take interrupts until we map
381  * the interrupt registers.
382  */
383 static void
bootstrap4m(void)384 bootstrap4m(void)
385 {
386 	int node;
387 	int nvaddrs, *vaddrs, vstore[10];
388 	u_int pte;
389 	int i;
390 	extern void setpte4m(u_int, u_int);
391 
392 	if ((node = prom_opennode("/obio/interrupt")) == 0
393 	    && (node = prom_finddevice("/obio/interrupt")) == 0)
394 		panic("bootstrap: could not get interrupt "
395 		      "node from prom");
396 
397 	vaddrs = vstore;
398 	nvaddrs = sizeof(vstore)/sizeof(vstore[0]);
399 	if (prom_getprop(node, "address", sizeof(int),
400 		    &nvaddrs, &vaddrs) != 0) {
401 		printf("bootstrap: could not get interrupt properties");
402 		prom_halt();
403 	}
404 	if (nvaddrs < 2 || nvaddrs > 5) {
405 		printf("bootstrap: cannot handle %d interrupt regs\n",
406 		       nvaddrs);
407 		prom_halt();
408 	}
409 
410 	for (i = 0; i < nvaddrs - 1; i++) {
411 		pte = getpte4m((u_int)vaddrs[i]);
412 		if ((pte & SRMMU_TETYPE) != SRMMU_TEPTE) {
413 			panic("bootstrap: PROM has invalid mapping for "
414 			      "processor interrupt register %d",i);
415 			prom_halt();
416 		}
417 		pte |= PPROT_S;
418 
419 		/* Duplicate existing mapping */
420 		setpte4m(PI_INTR_VA + (_MAXNBPG * i), pte);
421 	}
422 	cpuinfo.intreg_4m = (struct icr_pi *)
423 		(PI_INTR_VA + (_MAXNBPG * CPU_MID2CPUNO(bootmid)));
424 
425 	/*
426 	 * That was the processor register...now get system register;
427 	 * it is the last returned by the PROM
428 	 */
429 	pte = getpte4m((u_int)vaddrs[i]);
430 	if ((pte & SRMMU_TETYPE) != SRMMU_TEPTE)
431 		panic("bootstrap: PROM has invalid mapping for system "
432 		      "interrupt register");
433 	pte |= PPROT_S;
434 
435 	setpte4m(SI_INTR_VA, pte);
436 
437 	/* Now disable interrupts */
438 	icr_si_bis(SINTR_MA);
439 
440 	/* Send all interrupts to primary processor */
441 	*((u_int *)ICR_ITR) = CPU_MID2CPUNO(bootmid);
442 
443 #ifdef DEBUG
444 /*	printf("SINTR: mask: 0x%x, pend: 0x%x\n", *(int*)ICR_SI_MASK,
445 	       *(int*)ICR_SI_PEND);
446 */
447 #endif
448 }
449 #endif /* SUN4M && !MSIIEP */
450 
451 
452 #if defined(SUN4M) && defined(MSIIEP)
453 /*
454  * On ms-IIep all the interrupt registers, counters etc
455  * are PCIC registers, so we need to map it early.
456  */
457 static void
bootstrapIIep(void)458 bootstrapIIep(void)
459 {
460 	extern struct sparc_bus_space_tag mainbus_space_tag;
461 
462 	int node;
463 	bus_space_handle_t bh;
464 	pcireg_t id;
465 
466 	if ((node = prom_opennode("/pci")) == 0
467 	    && (node = prom_finddevice("/pci")) == 0)
468 		panic("bootstrap: could not get pci "
469 		      "node from prom");
470 
471 	if (bus_space_map2(&mainbus_space_tag,
472 			   (bus_addr_t)MSIIEP_PCIC_PA,
473 			   (bus_size_t)sizeof(struct msiiep_pcic_reg),
474 			   BUS_SPACE_MAP_LINEAR,
475 			   MSIIEP_PCIC_VA, &bh) != 0)
476 		panic("bootstrap: unable to map ms-IIep pcic registers");
477 
478 	/* verify that it's PCIC */
479 	id = mspcic_read_4(pcic_id);
480 
481 	if (PCI_VENDOR(id) != PCI_VENDOR_SUN
482 	    && PCI_PRODUCT(id) != PCI_PRODUCT_SUN_MS_IIep)
483 		panic("bootstrap: PCI id %08x", id);
484 }
485 
486 #undef msiiep
487 #endif /* SUN4M && MSIIEP */
488 
489 
490 /*
491  * bootpath_build: build a bootpath. Used when booting a generic
492  * kernel to find our root device.  Newer proms give us a bootpath,
493  * for older proms we have to create one.  An element in a bootpath
494  * has 4 fields: name (device name), val[0], val[1], and val[2]. Note that:
495  * Interpretation of val[] is device-dependent. Some examples:
496  *
497  * if (val[0] == -1) {
498  *	val[1] is a unit number    (happens most often with old proms)
499  * } else {
500  *	[sbus device] val[0] is a sbus slot, and val[1] is an sbus offset
501  *	[scsi disk] val[0] is target, val[1] is lun, val[2] is partition
502  *	[scsi tape] val[0] is target, val[1] is lun, val[2] is file #
503  * }
504  *
505  */
506 
507 static void
bootpath_build(void)508 bootpath_build(void)
509 {
510 	const char *cp;
511 	char *pp;
512 	struct bootpath *bp;
513 	int fl;
514 
515 	/*
516 	 * Grab boot path from PROM and split into `bootpath' components.
517 	 */
518 	memset(bootpath, 0, sizeof(bootpath));
519 	bp = bootpath;
520 	cp = prom_getbootpath();
521 	switch (prom_version()) {
522 	case PROM_OLDMON:
523 	case PROM_OBP_V0:
524 		/*
525 		 * Build fake bootpath.
526 		 */
527 		if (cp != NULL)
528 			bootpath_fake(bp, cp);
529 		break;
530 	case PROM_OBP_V2:
531 	case PROM_OBP_V3:
532 	case PROM_OPENFIRM:
533 		while (cp != NULL && *cp == '/') {
534 			/* Step over '/' */
535 			++cp;
536 			/* Extract name */
537 			pp = bp->name;
538 			while (*cp != '@' && *cp != '/' && *cp != '\0')
539 				*pp++ = *cp++;
540 			*pp = '\0';
541 #if defined(SUN4M)
542 			/*
543 			 * JS1/OF does not have iommu node in the device
544 			 * tree, so bootpath will start with the sbus entry.
545 			 * Add entry for iommu to match attachment. See also
546 			 * mainbus_attach and iommu_attach.
547 			 */
548 			if (CPU_ISSUN4M && bp == bootpath
549 			    && strcmp(bp->name, "sbus") == 0) {
550 				printf("bootpath_build: inserting iommu entry\n");
551 				strcpy(bootpath[0].name, "iommu");
552 				bootpath[0].val[0] = 0;
553 				bootpath[0].val[1] = 0x10000000;
554 				bootpath[0].val[2] = 0;
555 				++nbootpath;
556 
557 				strcpy(bootpath[1].name, "sbus");
558 				if (*cp == '/') {
559 					/* complete sbus entry */
560 					bootpath[1].val[0] = 0;
561 					bootpath[1].val[1] = 0x10001000;
562 					bootpath[1].val[2] = 0;
563 					++nbootpath;
564 					bp = &bootpath[2];
565 					continue;
566 				} else
567 					bp = &bootpath[1];
568 			}
569 #endif /* SUN4M */
570 			if (*cp == '@') {
571 				cp = str2hex(++cp, &bp->val[0]);
572 				if (*cp == ',')
573 					cp = str2hex(++cp, &bp->val[1]);
574 				if (*cp == ':') {
575 					/* XXX - we handle just one char */
576 					/*       skip remainder of paths */
577 					/*       like "ledma@f,400010:tpe" */
578 					bp->val[2] = *++cp - 'a';
579 					while (*++cp != '/' && *cp != '\0')
580 						/*void*/;
581 				}
582 			} else {
583 				bp->val[0] = -1; /* no #'s: assume unit 0, no
584 							sbus offset/address */
585 			}
586 			++bp;
587 			++nbootpath;
588 		}
589 		bp->name[0] = 0;
590 		break;
591 	}
592 
593 	bootpath_print(bootpath);
594 
595 	/* Setup pointer to boot flags */
596 	cp = prom_getbootargs();
597 	if (cp == NULL)
598 		return;
599 
600 	/* Skip any whitespace */
601 	while (*cp != '-')
602 		if (*cp++ == '\0')
603 			return;
604 
605 	for (;*++cp;) {
606 		fl = 0;
607 		BOOT_FLAG(*cp, fl);
608 		if (!fl) {
609 			printf("unknown option `%c'\n", *cp);
610 			continue;
611 		}
612 		boothowto |= fl;
613 
614 		/* specialties */
615 		if (*cp == 'd') {
616 #if defined(KGDB)
617 			kgdb_debug_panic = 1;
618 			kgdb_connect(1);
619 #elif defined(DDB)
620 			Debugger();
621 #else
622 			printf("kernel has no debugger\n");
623 #endif
624 		}
625 	}
626 }
627 
628 /*
629  * Fake a ROM generated bootpath.
630  * The argument `cp' points to a string such as "xd(0,0,0)netbsd"
631  */
632 
633 static void
bootpath_fake(struct bootpath * bp,const char * cp)634 bootpath_fake(struct bootpath *bp, const char *cp)
635 {
636 	const char *pp;
637 	int v0val[3];
638 
639 #define BP_APPEND(BP,N,V0,V1,V2) { \
640 	strcpy((BP)->name, N); \
641 	(BP)->val[0] = (V0); \
642 	(BP)->val[1] = (V1); \
643 	(BP)->val[2] = (V2); \
644 	(BP)++; \
645 	nbootpath++; \
646 }
647 
648 #if defined(SUN4)
649 	if (CPU_ISSUN4M) {
650 		printf("twas brillig..\n");
651 		return;
652 	}
653 #endif
654 
655 	pp = cp + 2;
656 	v0val[0] = v0val[1] = v0val[2] = 0;
657 	if (*pp == '(' 					/* for vi: ) */
658  	    && *(pp = str2hex(++pp, &v0val[0])) == ','
659 	    && *(pp = str2hex(++pp, &v0val[1])) == ',')
660 		(void)str2hex(++pp, &v0val[2]);
661 
662 #if defined(SUN4)
663 	if (CPU_ISSUN4) {
664 		char tmpname[8];
665 
666 		/*
667 		 *  xylogics VME dev: xd, xy, xt
668 		 *  fake looks like: /vme0/xdc0/xd@1,0
669 		 */
670 		if (cp[0] == 'x') {
671 			if (cp[1] == 'd') {/* xd? */
672 				BP_APPEND(bp, "vme", -1, 0, 0);
673 			} else {
674 				BP_APPEND(bp, "vme", -1, 0, 0);
675 			}
676 			/* e.g. `xdc' */
677 			snprintf(tmpname, sizeof(tmpname), "x%cc", cp[1]);
678 			BP_APPEND(bp, tmpname, -1, v0val[0], 0);
679 			/* e.g. `xd' */
680 			snprintf(tmpname, sizeof(tmpname), "x%c", cp[1]);
681 			BP_APPEND(bp, tmpname, v0val[1], v0val[2], 0);
682 			return;
683 		}
684 
685 		/*
686 		 * ethernet: ie, le (rom supports only obio?)
687 		 * fake looks like: /obio0/le0
688 		 */
689 		if ((cp[0] == 'i' || cp[0] == 'l') && cp[1] == 'e')  {
690 			BP_APPEND(bp, "obio", -1, 0, 0);
691 			snprintf(tmpname, sizeof(tmpname), "%c%c", cp[0], cp[1]);
692 			BP_APPEND(bp, tmpname, -1, 0, 0);
693 			return;
694 		}
695 
696 		/*
697 		 * scsi: sd, st, sr
698 		 * assume: 4/100 = sw: /obio0/sw0/sd@0,0:a
699 		 * 4/200 & 4/400 = si/sc: /vme0/si0/sd@0,0:a
700  		 * 4/300 = esp: /obio0/esp0/sd@0,0:a
701 		 * (note we expect sc to mimic an si...)
702 		 */
703 		if (cp[0] == 's' &&
704 			(cp[1] == 'd' || cp[1] == 't' || cp[1] == 'r')) {
705 
706 			int  target, lun;
707 
708 			switch (cpuinfo.cpu_type) {
709 			case CPUTYP_4_200:
710 			case CPUTYP_4_400:
711 				BP_APPEND(bp, "vme", -1, 0, 0);
712 				BP_APPEND(bp, "si", -1, v0val[0], 0);
713 				break;
714 			case CPUTYP_4_100:
715 				BP_APPEND(bp, "obio", -1, 0, 0);
716 				BP_APPEND(bp, "sw", -1, v0val[0], 0);
717 				break;
718 			case CPUTYP_4_300:
719 				BP_APPEND(bp, "obio", -1, 0, 0);
720 				BP_APPEND(bp, "esp", -1, v0val[0], 0);
721 				break;
722 			default:
723 				panic("bootpath_fake: unknown system type %d",
724 				      cpuinfo.cpu_type);
725 			}
726 			/*
727 			 * Deal with target/lun encodings.
728 			 * Note: more special casing in dk_establish().
729 			 *
730 			 * We happen to know how `prom_revision' is
731 			 * constructed from `monID[]' on sun4 proms...
732 			 */
733 			if (prom_revision() > '1') {
734 				target = v0val[1] >> 3; /* new format */
735 				lun    = v0val[1] & 0x7;
736 			} else {
737 				target = v0val[1] >> 2; /* old format */
738 				lun    = v0val[1] & 0x3;
739 			}
740 			snprintf(tmpname, sizeof(tmpname),
741 			    "%c%c", cp[0], cp[1]);
742 			BP_APPEND(bp, tmpname, target, lun, v0val[2]);
743 			return;
744 		}
745 
746 		return; /* didn't grok bootpath, no change */
747 	}
748 #endif /* SUN4 */
749 
750 #if defined(SUN4C)
751 	/*
752 	 * sun4c stuff
753 	 */
754 
755 	/*
756 	 * floppy: fd
757 	 * fake looks like: /fd@0,0:a
758 	 */
759 	if (cp[0] == 'f' && cp[1] == 'd') {
760 		/*
761 		 * Assume `fd(c,u,p)' means:
762 		 * partition `p' on floppy drive `u' on controller `c'
763 		 * Yet, for the purpose of determining the boot device,
764 		 * we support only one controller, so we encode the
765 		 * bootpath component by unit number, as on a v2 prom.
766 		 */
767 		BP_APPEND(bp, "fd", -1, v0val[1], v0val[2]);
768 		return;
769 	}
770 
771 	/*
772 	 * ethernet: le
773 	 * fake looks like: /sbus0/le0
774 	 */
775 	if (cp[0] == 'l' && cp[1] == 'e') {
776 		BP_APPEND(bp, "sbus", -1, 0, 0);
777 		BP_APPEND(bp, "le", -1, v0val[0], 0);
778 		return;
779 	}
780 
781 	/*
782 	 * scsi: sd, st, sr
783 	 * fake looks like: /sbus0/esp0/sd@3,0:a
784 	 */
785 	if (cp[0] == 's' && (cp[1] == 'd' || cp[1] == 't' || cp[1] == 'r')) {
786 		char tmpname[8];
787 		int  target, lun;
788 
789 		BP_APPEND(bp, "sbus", -1, 0, 0);
790 		BP_APPEND(bp, "esp", -1, v0val[0], 0);
791 		if (cp[1] == 'r')
792 			snprintf(tmpname, sizeof(tmpname), "cd"); /* netbsd uses 'cd', not 'sr'*/
793 		else
794 			snprintf(tmpname, sizeof(tmpname), "%c%c", cp[0], cp[1]);
795 		/* XXX - is TARGET/LUN encoded in v0val[1]? */
796 		target = v0val[1];
797 		lun = 0;
798 		BP_APPEND(bp, tmpname, target, lun, v0val[2]);
799 		return;
800 	}
801 #endif /* SUN4C */
802 
803 
804 	/*
805 	 * unknown; return
806 	 */
807 
808 #undef BP_APPEND
809 }
810 
811 /*
812  * print out the bootpath
813  * the %x isn't 0x%x because the Sun EPROMs do it this way, and
814  * consistency with the EPROMs is probably better here.
815  */
816 
817 static void
bootpath_print(struct bootpath * bp)818 bootpath_print(struct bootpath *bp)
819 {
820 	printf("bootpath: ");
821 	while (bp->name[0]) {
822 		if (bp->val[0] == -1)
823 			printf("/%s%x", bp->name, bp->val[1]);
824 		else
825 			printf("/%s@%x,%x", bp->name, bp->val[0], bp->val[1]);
826 		if (bp->val[2] != 0)
827 			printf(":%c", bp->val[2] + 'a');
828 		bp++;
829 	}
830 	printf("\n");
831 }
832 
833 
834 /*
835  * save or read a bootpath pointer from the boothpath store.
836  */
837 struct bootpath *
bootpath_store(int storep,struct bootpath * bp)838 bootpath_store(int storep, struct bootpath *bp)
839 {
840 	static struct bootpath *save;
841 	struct bootpath *retval;
842 
843 	retval = save;
844 	if (storep)
845 		save = bp;
846 
847 	return (retval);
848 }
849 
850 /*
851  * Set up the sd target mappings for non SUN4 PROMs.
852  * Find out about the real SCSI target, given the PROM's idea of the
853  * target of the (boot) device (i.e., the value in bp->v0val[0]).
854  */
855 static void
crazymap(const char * prop,int * map)856 crazymap(const char *prop, int *map)
857 {
858 	int i;
859 	char propval[8+2];
860 
861 	if (!CPU_ISSUN4 && prom_version() < 2) {
862 		/*
863 		 * Machines with real v0 proms have an `s[dt]-targets' property
864 		 * which contains the mapping for us to use. v2 proms do not
865 		 * require remapping.
866 		 */
867 		if (prom_getoption(prop, propval, sizeof propval) != 0 ||
868 		    propval[0] == '\0' || strlen(propval) != 8) {
869  build_default_map:
870 			printf("WARNING: %s map is bogus, using default\n",
871 				prop);
872 			for (i = 0; i < 8; ++i)
873 				map[i] = i;
874 			i = map[0];
875 			map[0] = map[3];
876 			map[3] = i;
877 			return;
878 		}
879 		for (i = 0; i < 8; ++i) {
880 			map[i] = propval[i] - '0';
881 			if (map[i] < 0 ||
882 			    map[i] >= 8)
883 				goto build_default_map;
884 		}
885 	} else {
886 		/*
887 		 * Set up the identity mapping for old sun4 monitors
888 		 * and v[2-] OpenPROMs. Note: dkestablish() does the
889 		 * SCSI-target juggling for sun4 monitors.
890 		 */
891 		for (i = 0; i < 8; ++i)
892 			map[i] = i;
893 	}
894 }
895 
896 int
sd_crazymap(int n)897 sd_crazymap(int n)
898 {
899 	static int prom_sd_crazymap[8]; /* static: compute only once! */
900 	static int init = 0;
901 
902 	if (init == 0) {
903 		crazymap("sd-targets", prom_sd_crazymap);
904 		init = 1;
905 	}
906 	return prom_sd_crazymap[n];
907 }
908 
909 int
st_crazymap(int n)910 st_crazymap(int n)
911 {
912 	static int prom_st_crazymap[8]; /* static: compute only once! */
913 	static int init = 0;
914 
915 	if (init == 0) {
916 		crazymap("st-targets", prom_st_crazymap);
917 		init = 1;
918 	}
919 	return prom_st_crazymap[n];
920 }
921 
922 
923 /*
924  * Determine mass storage and memory configuration for a machine.
925  * We get the PROM's root device and make sure we understand it, then
926  * attach it as `mainbus0'.  We also set up to handle the PROM `sync'
927  * command.
928  */
929 void
cpu_configure(void)930 cpu_configure(void)
931 {
932 	struct pcb *pcb0;
933 	bool userconf = (boothowto & RB_USERCONF) != 0;
934 
935 	/* initialise the softintr system */
936 	sparc_softintr_init();
937 
938 	/* build the bootpath */
939 	bootpath_build();
940 	if (((boothowto & RB_USERCONF) != 0) && !userconf)
941 		/*
942 		 * Old bootloaders do not pass boothowto, and MI code
943 		 * has already handled userconfig before we get here
944 		 * and finally fetch the right options. So if we missed
945 		 * it, just do it here.
946  		 */
947 		userconf_prompt();
948 
949 #if defined(SUN4)
950 	if (CPU_ISSUN4) {
951 #define MEMREG_PHYSADDR	0xf4000000
952 		bus_space_handle_t bh;
953 		bus_addr_t paddr = MEMREG_PHYSADDR;
954 
955 		if (cpuinfo.cpu_type == CPUTYP_4_100)
956 			/* Clear top bits of physical address on 4/100 */
957 			paddr &= ~0xf0000000;
958 
959 		if (obio_find_rom_map(paddr, PAGE_SIZE, &bh) != 0)
960 			panic("configure: ROM hasn't mapped memreg!");
961 
962 		par_err_reg = (volatile int *)bh;
963 	}
964 #endif
965 #if defined(SUN4C)
966 	if (CPU_ISSUN4C) {
967 		char *cp, buf[32];
968 		int node = findroot();
969 		cp = prom_getpropstringA(node, "device_type", buf, sizeof buf);
970 		if (strcmp(cp, "cpu") != 0)
971 			panic("PROM root device type = %s (need CPU)", cp);
972 	}
973 #endif
974 
975 	prom_setcallback(sync_crash);
976 
977 	/* Enable device interrupts */
978 #if defined(SUN4M)
979 #if !defined(MSIIEP)
980 	if (CPU_ISSUN4M)
981 		icr_si_bic(SINTR_MA);
982 #else
983 	if (CPU_ISSUN4M)
984 		/* nothing for ms-IIep so far */;
985 #endif /* MSIIEP */
986 #endif /* SUN4M */
987 
988 #if defined(SUN4) || defined(SUN4C)
989 	if (CPU_ISSUN4 || CPU_ISSUN4C)
990 		ienab_bis(IE_ALLIE);
991 #endif
992 
993 	if (config_rootfound("mainbus", NULL) == NULL)
994 		panic("mainbus not configured");
995 
996 	/*
997 	 * XXX Re-zero lwp0's pcb, to nullify the effect of the
998 	 * XXX stack running into it during auto-configuration.
999 	 * XXX - should fix stack usage.
1000 	 */
1001 	pcb0 = lwp_getpcb(&lwp0);
1002 	memset(pcb0, 0, sizeof(struct pcb));
1003 
1004 	spl0();
1005 }
1006 
1007 void
cpu_rootconf(void)1008 cpu_rootconf(void)
1009 {
1010 	struct bootpath *bp;
1011 
1012 	bp = nbootpath == 0 ? NULL : &bootpath[nbootpath-1];
1013 	if (bp == NULL)
1014 		booted_partition = 0;
1015 	else if (booted_device != bp->dev)
1016 		booted_partition = 0;
1017 	else
1018 		booted_partition = bp->val[2];
1019 	rootconf();
1020 }
1021 
1022 /*
1023  * Console `sync' command.  SunOS just does a `panic: zero' so I guess
1024  * no one really wants anything fancy...
1025  */
1026 void
sync_crash(void)1027 sync_crash(void)
1028 {
1029 
1030 	panic("PROM sync command");
1031 }
1032 
1033 char *
clockfreq(int freq)1034 clockfreq(int freq)
1035 {
1036 	static char buf[10];
1037 	size_t len;
1038 
1039 	freq /= 1000;
1040 	len = snprintf(buf, sizeof(buf), "%d", freq / 1000);
1041 	freq %= 1000;
1042 	if (freq)
1043 		snprintf(buf + len, sizeof(buf) - len, ".%03d", freq);
1044 	return buf;
1045 }
1046 
1047 /* ARGSUSED */
1048 static int
mbprint(void * aux,const char * name)1049 mbprint(void *aux, const char *name)
1050 {
1051 	struct mainbus_attach_args *ma = aux;
1052 
1053 	if (name)
1054 		aprint_normal("%s at %s", ma->ma_name, name);
1055 	if (ma->ma_paddr)
1056 		aprint_normal(" %saddr 0x%lx",
1057 			BUS_ADDR_IOSPACE(ma->ma_paddr) ? "io" : "",
1058 			(u_long)BUS_ADDR_PADDR(ma->ma_paddr));
1059 	if (ma->ma_pri)
1060 		aprint_normal(" ipl %d", ma->ma_pri);
1061 	return (UNCONF);
1062 }
1063 
1064 int
mainbus_match(device_t parent,cfdata_t cf,void * aux)1065 mainbus_match(device_t parent, cfdata_t cf, void *aux)
1066 {
1067 
1068 	return (1);
1069 }
1070 
1071 /*
1072  * Helper routines to get some of the more common properties. These
1073  * only get the first item in case the property value is an array.
1074  * Drivers that "need to know it all" can call prom_getprop() directly.
1075  */
1076 #if defined(SUN4C) || defined(SUN4M) || defined(SUN4D)
1077 static int	prom_getprop_reg1(int, struct openprom_addr *);
1078 static int	prom_getprop_intr1(int, int *);
1079 static int	prom_getprop_address1(int, void **);
1080 #endif
1081 
1082 /*
1083  * Attach the mainbus.
1084  *
1085  * Our main job is to attach the CPU (the root node we got in configure())
1086  * and iterate down the list of `mainbus devices' (children of that node).
1087  * We also record the `node id' of the default frame buffer, if any.
1088  */
1089 static void
mainbus_attach(device_t parent,device_t dev,void * aux)1090 mainbus_attach(device_t parent, device_t dev, void *aux)
1091 {
1092 extern struct sparc_bus_dma_tag mainbus_dma_tag;
1093 extern struct sparc_bus_space_tag mainbus_space_tag;
1094 
1095 	struct boot_special {
1096 		const char *const dev;
1097 #define BS_EARLY	1	/* attach device early */
1098 #define	BS_IGNORE	2	/* ignore root device */
1099 #define	BS_OPTIONAL	4	/* device not alwas present */
1100 		unsigned int flags;
1101 	};
1102 
1103 	struct mainbus_attach_args ma;
1104 	char namebuf[32];
1105 #if defined(SUN4C) || defined(SUN4M) || defined(SUN4D)
1106 	const char *sp = NULL;
1107 	int node0, node;
1108 	const struct boot_special *openboot_special, *ssp;
1109 #endif
1110 
1111 #if defined(SUN4C)
1112 	static const struct boot_special openboot_special4c[] = {
1113 		/* find these first */
1114 		{ "memory-error", BS_EARLY },
1115 			/* as early as convenient, in case of error */
1116 		{ "eeprom", BS_EARLY },
1117 		{ "counter-timer", BS_EARLY },
1118 		{ "auxiliary-io", BS_EARLY },
1119 
1120 		/* ignore these */
1121 		{ "aliases", BS_IGNORE },
1122 		{ "interrupt-enable", BS_IGNORE },
1123 		{ "memory", BS_IGNORE },
1124 		{ "openprom", BS_IGNORE },
1125 		{ "options", BS_IGNORE },
1126 		{ "packages", BS_IGNORE },
1127 		{ "virtual-memory", BS_IGNORE },
1128 
1129 		/* sentinel */
1130 		{ NULL, 0 }
1131 	};
1132 #else
1133 #define openboot_special4c	((void *)0)
1134 #endif
1135 #if defined(SUN4M)
1136 	static const struct boot_special openboot_special4m[] = {
1137 		/* find these first */
1138 		{ "SUNW,sx", BS_EARLY|BS_OPTIONAL },
1139 		{ "obio", BS_EARLY|BS_OPTIONAL },
1140 				/* smart enough to get eeprom/etc mapped */
1141 		{ "pci", BS_EARLY|BS_OPTIONAL },	/* ms-IIep */
1142 
1143 		/*
1144 		 * These are _root_ devices to ignore. Others must be handled
1145 		 * elsewhere.
1146 		 */
1147 		{ "virtual-memory", BS_IGNORE },
1148 		{ "aliases", BS_IGNORE },
1149 		{ "chosen", BS_IGNORE },	/* OpenFirmware */
1150 		{ "memory", BS_IGNORE },
1151 		{ "openprom", BS_IGNORE },
1152 		{ "options", BS_IGNORE },
1153 		{ "packages", BS_IGNORE },
1154 		{ "udp", BS_IGNORE },		/* OFW in Krups */
1155 		/* we also skip any nodes with device_type == "cpu" */
1156 
1157 		{ NULL, 0 }
1158 	};
1159 #else
1160 #define openboot_special4m	((void *)0)
1161 #endif
1162 #if defined(SUN4D)
1163 	static const struct boot_special openboot_special4d[] = {
1164 		/*
1165 		 * These are _root_ devices to ignore. Others must be handled
1166 		 * elsewhere.
1167 		 */
1168 		{ "mem-unit", BS_IGNORE },
1169 			/* XXX might need this for memory errors */
1170 		{ "boards", BS_IGNORE },
1171 		{ "openprom", BS_IGNORE },
1172 		{ "virtual-memory", BS_IGNORE },
1173 		{ "memory", BS_IGNORE },
1174 		{ "aliases", BS_IGNORE },
1175 		{ "options", BS_IGNORE },
1176 		{ "packages", BS_IGNORE },
1177 
1178 		{ NULL, 0 }
1179 	};
1180 #else
1181 #define	openboot_special4d	((void *)0)
1182 #endif
1183 
1184 
1185 	if (CPU_ISSUN4)
1186 		snprintf(machine_model, sizeof machine_model, "SUN-4/%d series",
1187 		    cpuinfo.classlvl);
1188 	else
1189 		snprintf(machine_model, sizeof machine_model, "%s",
1190 		    prom_getpropstringA(findroot(), "name", namebuf,
1191 		    sizeof(namebuf)));
1192 
1193 	prom_getidprom();
1194 	printf(": %s: hostid %lx\n", machine_model, hostid);
1195 
1196 	/* Establish the first component of the boot path */
1197 	bootpath_store(1, bootpath);
1198 
1199 	/*
1200 	 * Locate and configure the ``early'' devices.  These must be
1201 	 * configured before we can do the rest.  For instance, the
1202 	 * EEPROM contains the Ethernet address for the LANCE chip.
1203 	 * If the device cannot be located or configured, panic.
1204 	 */
1205 
1206 #if defined(SUN4)
1207 	if (CPU_ISSUN4) {
1208 
1209 		memset(&ma, 0, sizeof(ma));
1210 		/* Configure the CPU. */
1211 		ma.ma_bustag = &mainbus_space_tag;
1212 		ma.ma_dmatag = &mainbus_dma_tag;
1213 		ma.ma_name = "cpu";
1214 		if (config_found(dev, (void *)&ma, mbprint) == NULL)
1215 			panic("cpu missing");
1216 
1217 		ma.ma_bustag = &mainbus_space_tag;
1218 		ma.ma_dmatag = &mainbus_dma_tag;
1219 		ma.ma_name = "obio";
1220 		if (config_found(dev, (void *)&ma, mbprint) == NULL)
1221 			panic("obio missing");
1222 
1223 		ma.ma_bustag = &mainbus_space_tag;
1224 		ma.ma_dmatag = &mainbus_dma_tag;
1225 		ma.ma_name = "vme";
1226 		(void)config_found(dev, (void *)&ma, mbprint);
1227 		return;
1228 	}
1229 #endif
1230 
1231 /*
1232  * The rest of this routine is for OBP machines exclusively.
1233  */
1234 #if defined(SUN4C) || defined(SUN4M) || defined(SUN4D)
1235 
1236 	if (CPU_ISSUN4D)
1237 		openboot_special = openboot_special4d;
1238 	else if (CPU_ISSUN4M)
1239 		openboot_special = openboot_special4m;
1240 	else
1241 		openboot_special = openboot_special4c;
1242 
1243 	node0 = firstchild(findroot());
1244 
1245 	/* The first early device to be configured is the cpu */
1246 	if (CPU_ISSUN4M) {
1247 		const char *cp;
1248 		int mid, bootnode = 0;
1249 
1250 		/*
1251 		 * Configure all CPUs.
1252 		 * Make sure to configure the boot CPU as cpu0.
1253 		 */
1254 	rescan:
1255 		for (node = node0; node; node = nextsibling(node)) {
1256 			cp = prom_getpropstringA(node, "device_type",
1257 					    namebuf, sizeof namebuf);
1258 			if (strcmp(cp, "cpu") != 0)
1259 				continue;
1260 
1261 			mid = prom_getpropint(node, "mid", -1);
1262 			if (bootnode == 0) {
1263 				/* We're looking for the boot CPU */
1264 				if (bootmid != 0 && mid != bootmid)
1265 					continue;
1266 				bootnode = node;
1267 			} else {
1268 				if (node == bootnode)
1269 					continue;
1270 			}
1271 
1272 			memset(&ma, 0, sizeof(ma));
1273 			ma.ma_bustag = &mainbus_space_tag;
1274 			ma.ma_dmatag = &mainbus_dma_tag;
1275 			ma.ma_node = node;
1276 			ma.ma_name = "cpu";
1277 			config_found(dev, (void *)&ma, mbprint);
1278 			if (node == bootnode && bootmid != 0) {
1279 				/* Re-enter loop to find all remaining CPUs */
1280 				goto rescan;
1281 			}
1282 		}
1283 	} else if (CPU_ISSUN4C) {
1284 		memset(&ma, 0, sizeof(ma));
1285 		ma.ma_bustag = &mainbus_space_tag;
1286 		ma.ma_dmatag = &mainbus_dma_tag;
1287 		ma.ma_node = findroot();
1288 		ma.ma_name = "cpu";
1289 		config_found(dev, (void *)&ma, mbprint);
1290 	}
1291 
1292 	for (ssp = openboot_special; (sp = ssp->dev) != NULL; ssp++) {
1293 		struct openprom_addr romreg;
1294 
1295 		if (!(ssp->flags & BS_EARLY)) continue;
1296 		if ((node = findnode(node0, sp)) == 0) {
1297 			if (ssp->flags & BS_OPTIONAL) continue;
1298 			printf("could not find %s in OPENPROM\n", sp);
1299 			panic("%s", sp);
1300 		}
1301 
1302 		memset(&ma, 0, sizeof ma);
1303 		ma.ma_bustag = &mainbus_space_tag;
1304 		ma.ma_dmatag = &mainbus_dma_tag;
1305 		ma.ma_name = prom_getpropstringA(node, "name",
1306 					    namebuf, sizeof namebuf);
1307 		ma.ma_node = node;
1308 		if (prom_getprop_reg1(node, &romreg) != 0)
1309 			continue;
1310 
1311 		ma.ma_paddr = (bus_addr_t)
1312 			BUS_ADDR(romreg.oa_space, romreg.oa_base);
1313 		ma.ma_size = romreg.oa_size;
1314 		if (prom_getprop_intr1(node, &ma.ma_pri) != 0)
1315 			continue;
1316 		if (prom_getprop_address1(node, &ma.ma_promvaddr) != 0)
1317 			continue;
1318 
1319 		if (config_found(dev, (void *)&ma, mbprint) == NULL) {
1320 			if (ssp->flags & BS_OPTIONAL) continue;
1321 			panic("%s", sp);
1322 		}
1323 	}
1324 
1325 	/*
1326 	 * Configure the rest of the devices, in PROM order.  Skip
1327 	 * PROM entries that are not for devices, or which must be
1328 	 * done before we get here.
1329 	 */
1330 	for (node = node0; node; node = nextsibling(node)) {
1331 		const char *cp;
1332 		struct openprom_addr romreg;
1333 
1334 		DPRINTF(ACDB_PROBE, ("Node: %x", node));
1335 #if defined(SUN4M)
1336 		if (CPU_ISSUN4M) {	/* skip the CPUs */
1337 			if (strcmp(prom_getpropstringA(node, "device_type",
1338 						  namebuf, sizeof namebuf),
1339 				   "cpu") == 0)
1340 				continue;
1341 		}
1342 #endif
1343 		cp = prom_getpropstringA(node, "name", namebuf, sizeof namebuf);
1344 		DPRINTF(ACDB_PROBE, (" name %s\n", namebuf));
1345 		for (ssp = openboot_special; (sp = ssp->dev) != NULL; ssp++) {
1346 			if (!(ssp->flags & (BS_EARLY|BS_IGNORE))) continue;
1347 			if (strcmp(cp, sp) == 0)
1348 				break;
1349 		}
1350 		if (sp != NULL)
1351 			continue;
1352 			/* an "early" device already configured, or an
1353 			   ignored device */
1354 
1355 		memset(&ma, 0, sizeof ma);
1356 		ma.ma_bustag = &mainbus_space_tag;
1357 		ma.ma_dmatag = &mainbus_dma_tag;
1358 		ma.ma_name = prom_getpropstringA(node, "name",
1359 					    namebuf, sizeof namebuf);
1360 		ma.ma_node = node;
1361 
1362 #if defined(SUN4M)
1363 		/*
1364 		 * JS1/OF does not have iommu node in the device tree,
1365 		 * so if on sun4m we see sbus node under root - attach
1366 		 * implicit iommu.  See also bootpath_build where we
1367 		 * adjust bootpath accordingly and iommu_attach where
1368 		 * we arrange for this sbus node to be attached.
1369 		 */
1370 		if (CPU_ISSUN4M && strcmp(ma.ma_name, "sbus") == 0) {
1371 			printf("mainbus_attach: sbus node under root on sun4m - assuming iommu\n");
1372 			ma.ma_name = "iommu";
1373 			ma.ma_paddr = (bus_addr_t)BUS_ADDR(0, 0x10000000);
1374 			ma.ma_size = 0x300;
1375 			ma.ma_pri = 0;
1376 			ma.ma_promvaddr = 0;
1377 
1378 			(void) config_found(dev, (void *)&ma, mbprint);
1379 			continue;
1380 		}
1381 #endif /* SUN4M */
1382 
1383 		if (prom_getprop_reg1(node, &romreg) != 0)
1384 			continue;
1385 
1386 		ma.ma_paddr = BUS_ADDR(romreg.oa_space, romreg.oa_base);
1387 		ma.ma_size = romreg.oa_size;
1388 
1389 		if (prom_getprop_intr1(node, &ma.ma_pri) != 0)
1390 			continue;
1391 
1392 		if (prom_getprop_address1(node, &ma.ma_promvaddr) != 0)
1393 			continue;
1394 
1395 		(void) config_found(dev, (void *)&ma, mbprint);
1396 	}
1397 #endif /* SUN4C || SUN4M || SUN4D */
1398 }
1399 
1400 CFATTACH_DECL_NEW(mainbus, 0, mainbus_match, mainbus_attach, NULL, NULL);
1401 
1402 
1403 #if defined(SUN4C) || defined(SUN4M) || defined(SUN4D)
1404 int
prom_getprop_reg1(int node,struct openprom_addr * rrp)1405 prom_getprop_reg1(int node, struct openprom_addr *rrp)
1406 {
1407 	int error, n;
1408 	struct openprom_addr *rrp0 = NULL;
1409 	char buf[32];
1410 
1411 	error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
1412 			&n, &rrp0);
1413 	if (error != 0) {
1414 		if (error == ENOENT &&
1415 		    strcmp(prom_getpropstringA(node, "device_type", buf, sizeof buf),
1416 			   "hierarchical") == 0) {
1417 			memset(rrp, 0, sizeof(struct openprom_addr));
1418 			error = 0;
1419 		}
1420 		return (error);
1421 	}
1422 
1423 	*rrp = rrp0[0];
1424 	free(rrp0, M_DEVBUF);
1425 	return (0);
1426 }
1427 
1428 int
prom_getprop_intr1(int node,int * ip)1429 prom_getprop_intr1(int node, int *ip)
1430 {
1431 	int error, n;
1432 	struct rom_intr *rip = NULL;
1433 
1434 	error = prom_getprop(node, "intr", sizeof(struct rom_intr),
1435 			&n, &rip);
1436 	if (error != 0) {
1437 		if (error == ENOENT) {
1438 			*ip = 0;
1439 			error = 0;
1440 		}
1441 		return (error);
1442 	}
1443 
1444 	*ip = rip[0].int_pri & 0xf;
1445 	free(rip, M_DEVBUF);
1446 	return (0);
1447 }
1448 
1449 int
prom_getprop_address1(int node,void ** vpp)1450 prom_getprop_address1(int node, void **vpp)
1451 {
1452 	int error, n;
1453 	void **vp = NULL;
1454 
1455 	error = prom_getprop(node, "address", sizeof(uint32_t), &n, &vp);
1456 	if (error != 0) {
1457 		if (error == ENOENT) {
1458 			*vpp = 0;
1459 			error = 0;
1460 		}
1461 		return (error);
1462 	}
1463 
1464 	*vpp = vp[0];
1465 	free(vp, M_DEVBUF);
1466 	return (0);
1467 }
1468 #endif /* SUN4C || SUN4M || SUN4D */
1469 
1470 #ifdef RASTERCONSOLE
1471 /*
1472  * Try to figure out where the PROM stores the cursor row & column
1473  * variables.  Returns nonzero on error.
1474  */
1475 int
romgetcursoraddr(int ** rowp,int ** colp)1476 romgetcursoraddr(int **rowp, int **colp)
1477 {
1478 	char buf[100];
1479 
1480 	/*
1481 	 * line# and column# are global in older proms (rom vector < 2)
1482 	 * and in some newer proms.  They are local in version 2.9.  The
1483 	 * correct cutoff point is unknown, as yet; we use 2.9 here.
1484 	 */
1485 	if (prom_version() < 2 || prom_revision() < 0x00020009)
1486 		snprintf(buf, sizeof(buf),
1487 		    "' line# >body >user %lx ! ' column# >body >user %lx !",
1488 		    (u_long)rowp, (u_long)colp);
1489 	else
1490 		snprintf(buf, sizeof(buf),
1491 		    "stdout @ is my-self addr line# %lx ! addr column# %lx !",
1492 		    (u_long)rowp, (u_long)colp);
1493 	*rowp = *colp = NULL;
1494 	prom_interpret(buf);
1495 	return (*rowp == NULL || *colp == NULL);
1496 }
1497 #endif /* RASTERCONSOLE */
1498 
1499 /*
1500  * Device registration used to determine the boot device.
1501  */
1502 #include <dev/scsipi/scsi_all.h>
1503 #include <dev/scsipi/scsipi_all.h>
1504 #include <dev/scsipi/scsiconf.h>
1505 #include <sparc/sparc/iommuvar.h>
1506 
1507 #define BUSCLASS_NONE		0
1508 #define BUSCLASS_MAINBUS	1
1509 #define BUSCLASS_IOMMU		2
1510 #define BUSCLASS_OBIO		3
1511 #define BUSCLASS_SBUS		4
1512 #define BUSCLASS_VME		5
1513 #define BUSCLASS_XDC		6
1514 #define BUSCLASS_XYC		7
1515 #define BUSCLASS_FDC		8
1516 #define BUSCLASS_PCIC		9
1517 #define BUSCLASS_PCI		10
1518 
1519 static int bus_class(device_t);
1520 static const char *bus_compatible(const char *);
1521 static int instance_match(device_t, void *, struct bootpath *);
1522 static void nail_bootdev(device_t, struct bootpath *);
1523 static void set_network_props(device_t, void *);
1524 
1525 static struct {
1526 	const char	*name;
1527 	int	class;
1528 } bus_class_tab[] = {
1529 	{ "mainbus",	BUSCLASS_MAINBUS },
1530 	{ "obio",	BUSCLASS_OBIO },
1531 	{ "iommu",	BUSCLASS_IOMMU },
1532 	{ "sbus",	BUSCLASS_SBUS },
1533 	{ "xbox",	BUSCLASS_SBUS },
1534 	{ "dma",	BUSCLASS_SBUS },
1535 	{ "esp",	BUSCLASS_SBUS },
1536 	{ "espdma",	BUSCLASS_SBUS },
1537 	{ "isp",	BUSCLASS_SBUS },
1538 	{ "ledma",	BUSCLASS_SBUS },
1539 	{ "lebuffer",	BUSCLASS_SBUS },
1540 	{ "vme",	BUSCLASS_VME },
1541 	{ "si",		BUSCLASS_VME },
1542 	{ "sw",		BUSCLASS_OBIO },
1543 	{ "xdc",	BUSCLASS_XDC },
1544 	{ "xyc",	BUSCLASS_XYC },
1545 	{ "fdc",	BUSCLASS_FDC },
1546 	{ "mspcic",	BUSCLASS_PCIC },
1547 	{ "pci",	BUSCLASS_PCI },
1548 };
1549 
1550 /*
1551  * A list of PROM device names that differ from our NetBSD
1552  * device names.
1553  */
1554 static struct {
1555 	const char	*bpname;
1556 	const char	*cfname;
1557 } dev_compat_tab[] = {
1558 	{ "espdma",	"dma" },
1559 	{ "SUNW,fas",   "esp" },
1560 	{ "QLGC,isp",	"isp" },
1561 	{ "PTI,isp",	"isp" },
1562 	{ "ptisp",	"isp" },
1563 	{ "SUNW,fdtwo",	"fdc" },
1564 	{ "network",	"hme" }, /* Krups */
1565 	{ "SUNW,hme",   "hme" },
1566 	{ "SUNW,qfe",   "hme" },
1567 };
1568 
1569 static const char *
bus_compatible(const char * bpname)1570 bus_compatible(const char *bpname)
1571 {
1572 	int i;
1573 
1574 	for (i = sizeof(dev_compat_tab)/sizeof(dev_compat_tab[0]); i-- > 0;) {
1575 		if (strcmp(bpname, dev_compat_tab[i].bpname) == 0)
1576 			return (dev_compat_tab[i].cfname);
1577 	}
1578 
1579 	return (bpname);
1580 }
1581 
1582 static int
bus_class(device_t dev)1583 bus_class(device_t dev)
1584 {
1585 	int i, class;
1586 
1587 	class = BUSCLASS_NONE;
1588 	if (dev == NULL)
1589 		return (class);
1590 
1591 	for (i = sizeof(bus_class_tab)/sizeof(bus_class_tab[0]); i-- > 0;) {
1592 		if (device_is_a(dev, bus_class_tab[i].name)) {
1593 			class = bus_class_tab[i].class;
1594 			break;
1595 		}
1596 	}
1597 
1598 	/* sun4m obio special case */
1599 	if (CPU_ISSUN4M && class == BUSCLASS_OBIO)
1600 		class = BUSCLASS_SBUS;
1601 
1602 	return (class);
1603 }
1604 
1605 static void
set_network_props(device_t dev,void * aux)1606 set_network_props(device_t dev, void *aux)
1607 {
1608 	struct mainbus_attach_args *ma;
1609 	struct sbus_attach_args *sa;
1610 	struct iommu_attach_args *iom;
1611 	struct pci_attach_args *pa;
1612 	uint8_t eaddr[ETHER_ADDR_LEN];
1613 	prop_dictionary_t dict;
1614 	prop_data_t blob;
1615 	int ofnode;
1616 
1617 	ofnode = 0;
1618 	switch (bus_class(device_parent(dev))) {
1619 	case BUSCLASS_MAINBUS:
1620 		ma = aux;
1621 		ofnode = ma->ma_node;
1622 		break;
1623 	case BUSCLASS_SBUS:
1624 		sa = aux;
1625 		ofnode = sa->sa_node;
1626 		break;
1627 	case BUSCLASS_IOMMU:
1628 		iom = aux;
1629 		ofnode = iom->iom_node;
1630 		break;
1631 	case BUSCLASS_PCI:
1632 		pa = aux;
1633 		ofnode = PCITAG_NODE(pa->pa_tag);
1634 		break;
1635 	}
1636 
1637 	prom_getether(ofnode, eaddr);
1638 	dict = device_properties(dev);
1639 	blob = prop_data_create_data(eaddr, ETHER_ADDR_LEN);
1640 	prop_dictionary_set(dict, "mac-address", blob);
1641 	prop_object_release(blob);
1642 }
1643 
1644 int
instance_match(device_t dev,void * aux,struct bootpath * bp)1645 instance_match(device_t dev, void *aux, struct bootpath *bp)
1646 {
1647 	struct mainbus_attach_args *ma;
1648 	struct sbus_attach_args *sa;
1649 	struct iommu_attach_args *iom;
1650   	struct pcibus_attach_args *pba;
1651 	struct pci_attach_args *pa;
1652 
1653 	/*
1654 	 * Several devices are represented on bootpaths in one of
1655 	 * two formats, e.g.:
1656 	 *	(1) ../sbus@.../esp@<offset>,<slot>/sd@..  (PROM v3 style)
1657 	 *	(2) /sbus0/esp0/sd@..                      (PROM v2 style)
1658 	 *
1659 	 * hence we fall back on a `unit number' check if the bus-specific
1660 	 * instance parameter check does not produce a match.
1661 	 */
1662 
1663 	/*
1664 	 * Rank parent bus so we know which locators to check.
1665 	 */
1666 	switch (bus_class(device_parent(dev))) {
1667 	case BUSCLASS_MAINBUS:
1668 		ma = aux;
1669 		DPRINTF(ACDB_BOOTDEV, ("instance_match: mainbus device, "
1670 		    "want space %#x addr %#x have space %#x addr %#llx\n",
1671 		    bp->val[0], bp->val[1], (int)BUS_ADDR_IOSPACE(ma->ma_paddr),
1672 			(unsigned long long)BUS_ADDR_PADDR(ma->ma_paddr)));
1673 		if ((u_long)bp->val[0] == BUS_ADDR_IOSPACE(ma->ma_paddr) &&
1674 		    (bus_addr_t)(u_long)bp->val[1] ==
1675 		    BUS_ADDR_PADDR(ma->ma_paddr))
1676 			return (1);
1677 		break;
1678 	case BUSCLASS_SBUS:
1679 		sa = aux;
1680 		DPRINTF(ACDB_BOOTDEV, ("instance_match: sbus device, "
1681 		    "want slot %#x offset %#x have slot %#x offset %#x\n",
1682 		     bp->val[0], bp->val[1], sa->sa_slot, sa->sa_offset));
1683 		if ((uint32_t)bp->val[0] == sa->sa_slot &&
1684 		    (uint32_t)bp->val[1] == sa->sa_offset)
1685 			return (1);
1686 		break;
1687 	case BUSCLASS_IOMMU:
1688 		iom = aux;
1689 		DPRINTF(ACDB_BOOTDEV, ("instance_match: iommu device, "
1690 		    "want space %#x pa %#x have space %#x pa %#x\n",
1691 		     bp->val[0], bp->val[1], iom->iom_reg[0].oa_space,
1692 		     iom->iom_reg[0].oa_base));
1693 		if ((uint32_t)bp->val[0] == iom->iom_reg[0].oa_space &&
1694 		    (uint32_t)bp->val[1] == iom->iom_reg[0].oa_base)
1695 			return (1);
1696 		break;
1697 	case BUSCLASS_XDC:
1698 	case BUSCLASS_XYC:
1699 		{
1700 		/*
1701 		 * XXX - x[dy]c attach args are not exported right now..
1702 		 * XXX   we happen to know they look like this:
1703 		 */
1704 		struct xxxx_attach_args { int driveno; } *aap = aux;
1705 
1706 		DPRINTF(ACDB_BOOTDEV,
1707 		    ("instance_match: x[dy]c device, want drive %#x have %#x\n",
1708 		     bp->val[0], aap->driveno));
1709 		if (aap->driveno == bp->val[0])
1710 			return (1);
1711 
1712 		}
1713 		break;
1714 	case BUSCLASS_PCIC:
1715 		pba = aux;
1716 		DPRINTF(ACDB_BOOTDEV, ("instance_match: pci bus "
1717 		    "want bus %d pa %#x have bus %d pa %#lx\n",
1718 		    bp->val[0], bp->val[1], pba->pba_bus, MSIIEP_PCIC_PA));
1719 		if ((int)bp->val[0] == pba->pba_bus
1720 		    && (bus_addr_t)bp->val[1] == MSIIEP_PCIC_PA)
1721 			return (1);
1722 		break;
1723 	case BUSCLASS_PCI:
1724 		pa = aux;
1725 		DPRINTF(ACDB_BOOTDEV, ("instance_match: pci device "
1726 		    "want dev %d function %d have dev %d function %d\n",
1727 		    bp->val[0], bp->val[1], pa->pa_device, pa->pa_function));
1728 		if ((u_int)bp->val[0] == pa->pa_device
1729 		    && (u_int)bp->val[1] == pa->pa_function)
1730 			return (1);
1731 		break;
1732 	default:
1733 		break;
1734 	}
1735 
1736 	if (bp->val[0] == -1 && bp->val[1] == device_unit(dev))
1737 		return (1);
1738 
1739 	return (0);
1740 }
1741 
1742 void
nail_bootdev(device_t dev,struct bootpath * bp)1743 nail_bootdev(device_t dev, struct bootpath *bp)
1744 {
1745 
1746 	if (bp->dev != NULL)
1747 		panic("device_register: already got a boot device: %s",
1748 			device_xname(bp->dev));
1749 
1750 	/*
1751 	 * Mark this bootpath component by linking it to the matched
1752 	 * device. We pick up the device pointer in cpu_rootconf().
1753 	 */
1754 	booted_device = bp->dev = dev;
1755 
1756 	/*
1757 	 * Then clear the current bootpath component, so we don't spuriously
1758 	 * match similar instances on other busses, e.g. a disk on
1759 	 * another SCSI bus with the same target.
1760 	 */
1761 	bootpath_store(1, NULL);
1762 }
1763 
1764 /*
1765  * We use device_register() to:
1766  *   set device properties on PCI devices
1767  *   find the bootpath
1768  */
1769 void
device_register(device_t dev,void * aux)1770 device_register(device_t dev, void *aux)
1771 {
1772 	struct bootpath *bp = bootpath_store(0, NULL);
1773 	const char *bpname;
1774 
1775 #ifdef MSIIEP
1776 	/* Check for PCI devices */
1777 	if (bus_class(device_parent(dev)) == BUSCLASS_PCI)
1778 		set_pci_props(dev);
1779 #endif
1780 
1781 	/*
1782 	 * If device name does not match current bootpath component
1783 	 * then there's nothing interesting to consider.
1784 	 */
1785 	if (bp == NULL)
1786 		return;
1787 
1788 	/*
1789 	 * Translate PROM name in case our drivers are named differently
1790 	 */
1791 	bpname = bus_compatible(bp->name);
1792 
1793 	DPRINTF(ACDB_BOOTDEV,
1794 	    ("\n%s: device_register: dvname %s(%s) bpname %s(%s)\n",
1795 	    device_xname(dev), device_cfdata(dev)->cf_name,
1796 	    device_xname(dev), bpname, bp->name));
1797 
1798 	/* First, match by name */
1799 	if (!device_is_a(dev, bpname))
1800 		return;
1801 
1802 	if (bus_class(dev) != BUSCLASS_NONE) {
1803 		/*
1804 		 * A bus or controller device of sorts. Check instance
1805 		 * parameters and advance boot path on match.
1806 		 */
1807 		if (instance_match(dev, aux, bp) != 0) {
1808 			if (device_is_a(dev, "fdc")) {
1809 				/*
1810 				 * XXX - HACK ALERT
1811 				 * Sun PROMs don't really seem to support
1812 				 * multiple floppy drives. So we aren't
1813 				 * going to, either.  Since the PROM
1814 				 * only provides a node for the floppy
1815 				 * controller, we sneakily add a drive to
1816 				 * the bootpath here.
1817 				 */
1818 				strcpy(bootpath[nbootpath].name, "fd");
1819 				nbootpath++;
1820 			}
1821 			booted_device = bp->dev = dev;
1822 			bootpath_store(1, bp + 1);
1823 			DPRINTF(ACDB_BOOTDEV, ("\t-- found bus controller %s\n",
1824 			    device_xname(dev)));
1825 			return;
1826 		}
1827 	} else if (device_is_a(dev, "le") ||
1828 		   device_is_a(dev, "hme") ||
1829 		   device_is_a(dev, "be") ||
1830 		   device_is_a(dev, "ie")) {
1831 
1832 		set_network_props(dev, aux);
1833 
1834 		/*
1835 		 * LANCE, Happy Meal, or BigMac ethernet device
1836 		 */
1837 		if (instance_match(dev, aux, bp) != 0) {
1838 			nail_bootdev(dev, bp);
1839 			DPRINTF(ACDB_BOOTDEV, ("\t-- found ethernet controller %s\n",
1840 			    device_xname(dev)));
1841 			return;
1842 		}
1843 	} else if (device_is_a(dev, "sd") ||
1844 		   device_is_a(dev, "cd")) {
1845 #if NSCSIBUS > 0
1846 		/*
1847 		 * A SCSI disk or cd; retrieve target/lun information
1848 		 * from parent and match with current bootpath component.
1849 		 * Note that we also have look back past the `scsibus'
1850 		 * device to determine whether this target is on the
1851 		 * correct controller in our boot path.
1852 		 */
1853 		struct scsipibus_attach_args *sa = aux;
1854 		struct scsipi_periph *periph = sa->sa_periph;
1855 		struct scsipi_channel *chan = periph->periph_channel;
1856 		struct scsibus_softc *sbsc =
1857 			device_private(device_parent(dev));
1858 		u_int target = bp->val[0];
1859 		u_int lun = bp->val[1];
1860 
1861 		/* Check the controller that this scsibus is on */
1862 		if ((bp-1)->dev != device_parent(sbsc->sc_dev))
1863 			return;
1864 
1865 		/*
1866 		 * Bounds check: we know the target and lun widths.
1867 		 */
1868 		if (target >= chan->chan_ntargets || lun >= chan->chan_nluns) {
1869 			printf("SCSI disk bootpath component not accepted: "
1870 			       "target %u; lun %u\n", target, lun);
1871 			return;
1872 		}
1873 
1874 		if (CPU_ISSUN4 && device_is_a(dev, "sd") &&
1875 		    target == 0 &&
1876 		    scsipi_lookup_periph(chan, target, lun) == NULL) {
1877 			/*
1878 			 * disk unit 0 is magic: if there is actually no
1879 			 * target 0 scsi device, the PROM will call
1880 			 * target 3 `sd0'.
1881 			 * XXX - what if someone puts a tape at target 0?
1882 			 */
1883 			target = 3;	/* remap to 3 */
1884 			lun = 0;
1885 		}
1886 
1887 		if (CPU_ISSUN4C && device_is_a(dev, "sd"))
1888 			target = sd_crazymap(target);
1889 
1890 		if (periph->periph_target == target &&
1891 		    periph->periph_lun == lun) {
1892 			nail_bootdev(dev, bp);
1893 			DPRINTF(ACDB_BOOTDEV, ("\t-- found [cs]d disk %s\n",
1894 			    device_xname(dev)));
1895 			return;
1896 		}
1897 #endif /* NSCSIBUS */
1898 	} else if (device_is_a(dev, "xd") ||
1899 		   device_is_a(dev, "xy")) {
1900 
1901 		/* A Xylogic disk */
1902 		if (instance_match(dev, aux, bp) != 0) {
1903 			nail_bootdev(dev, bp);
1904 			DPRINTF(ACDB_BOOTDEV, ("\t-- found x[dy] disk %s\n",
1905 			    device_xname(dev)));
1906 			return;
1907 		}
1908 
1909 	} else if (device_is_a(dev, "fd")) {
1910 		/*
1911 		 * Sun PROMs don't really seem to support multiple
1912 		 * floppy drives. So we aren't going to, either.
1913 		 * If we get this far, the `fdc controller' has
1914 		 * already matched and has appended a fake `fd' entry
1915 		 * to the bootpath, so just accept that as the boot device.
1916 		 */
1917 		nail_bootdev(dev, bp);
1918 		DPRINTF(ACDB_BOOTDEV, ("\t-- found floppy drive %s\n",
1919 		    device_xname(dev)));
1920 		return;
1921 	} else {
1922 		/*
1923 		 * Generic match procedure.
1924 		 */
1925 		if (instance_match(dev, aux, bp) != 0) {
1926 			nail_bootdev(dev, bp);
1927 			return;
1928 		}
1929 	}
1930 }
1931 
1932 /*
1933  * lookup_bootinfo:
1934  * Look up information in bootinfo of boot loader.
1935  */
1936 void *
lookup_bootinfo(int type)1937 lookup_bootinfo(int type)
1938 {
1939 	struct btinfo_common *bt;
1940 	char *help = bootinfo;
1941 
1942 	/* Check for a bootinfo record first. */
1943 	if (help == NULL)
1944 		return (NULL);
1945 
1946 	do {
1947 		bt = (struct btinfo_common *)help;
1948 		if (bt->type == type)
1949 			return ((void *)help);
1950 		help += bt->next;
1951 	} while (bt->next != 0 &&
1952 		(size_t)help < (size_t)bootinfo + BOOTINFO_SIZE);
1953 
1954 	return (NULL);
1955 }
1956 
1957 #if !NKSYMS && !defined(DDB) && !defined(MODULAR)
1958 /*
1959  * Move bootinfo from the current kernel top to the proposed
1960  * location. As a side-effect, `kernel_top' is adjusted to point
1961  * at the first free location after the relocated bootinfo array.
1962  */
1963 void
bootinfo_relocate(void * newloc)1964 bootinfo_relocate(void *newloc)
1965 {
1966 	int bi_size;
1967 	struct btinfo_common *bt;
1968 	char *cp, *dp;
1969 	extern char *kernel_top;
1970 
1971 	if (bootinfo == NULL) {
1972 		kernel_top = newloc;
1973 		return;
1974 	}
1975 
1976 	/*
1977 	 * Find total size of bootinfo array.
1978 	 * The array is terminated with a `nul' record (size == 0);
1979 	 * we account for that up-front by initializing `bi_size'
1980 	 * to size of a `btinfo_common' record.
1981 	 */
1982 	bi_size = sizeof(struct btinfo_common);
1983 	cp = bootinfo;
1984 	do {
1985 		bt = (struct btinfo_common *)cp;
1986 		bi_size += bt->next;
1987 		cp += bt->next;
1988 	} while (bt->next != 0 &&
1989 		(size_t)cp < (size_t)bootinfo + BOOTINFO_SIZE);
1990 
1991 	/*
1992 	 * Check propective gains.
1993 	 */
1994 	if ((int)bootinfo - (int)newloc < bi_size)
1995 		/* Don't bother */
1996 		return;
1997 
1998 	/*
1999 	 * Relocate the bits
2000 	 */
2001 	cp = bootinfo;
2002 	dp = newloc;
2003 	do {
2004 		bt = (struct btinfo_common *)cp;
2005 		memcpy(dp, cp, bt->next);
2006 		cp += bt->next;
2007 		dp += bt->next;
2008 	} while (bt->next != 0 &&
2009 		(size_t)cp < (size_t)bootinfo + BOOTINFO_SIZE);
2010 
2011 	/* Write the terminating record */
2012 	bt = (struct btinfo_common *)dp;
2013 	bt->next = bt->type = 0;
2014 
2015 	/* Set new bootinfo location and adjust kernel_top */
2016 	bootinfo = newloc;
2017 	kernel_top = (char *)newloc + ALIGN(bi_size);
2018 }
2019 #endif /* !NKSYMS && !defined(DDB) && !defined(MODULAR) */
2020