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