Home | History | Annotate | Line # | Download | only in alpha
autoconf.c revision 1.55
      1 /* $NetBSD: autoconf.c,v 1.55 2020/10/03 17:31:46 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)autoconf.c	8.4 (Berkeley) 10/1/93
     41  */
     42 
     43 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     44 
     45 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.55 2020/10/03 17:31:46 thorpej Exp $");
     46 
     47 #include "pci.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/buf.h>
     52 #include <sys/disklabel.h>
     53 #include <sys/reboot.h>
     54 #include <sys/device.h>
     55 #include <sys/conf.h>
     56 #include <dev/cons.h>
     57 
     58 #include <dev/pci/pcivar.h>
     59 
     60 #include <machine/autoconf.h>
     61 #include <machine/alpha.h>
     62 #include <machine/cpu.h>
     63 #include <machine/prom.h>
     64 #include <machine/cpuconf.h>
     65 #include <machine/intr.h>
     66 
     67 struct bootdev_data	*bootdev_data;
     68 
     69 void	parse_prom_bootdev(void);
     70 static inline int atoi(const char *);
     71 
     72 /*
     73  * cpu_configure:
     74  * called at boot time, configure all devices on system
     75  */
     76 void
     77 cpu_configure(void)
     78 {
     79 
     80 	parse_prom_bootdev();
     81 
     82 	/*
     83 	 * Disable interrupts during autoconfiguration.  splhigh() won't
     84 	 * work, because it simply _raises_ the IPL, so if machine checks
     85 	 * are disabled, they'll stay disabled.  Machine checks are needed
     86 	 * during autoconfig.
     87 	 */
     88 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
     89 	if (config_rootfound("mainbus", NULL) == NULL)
     90 		panic("no mainbus found");
     91 	(void)spl0();
     92 
     93 	/*
     94 	 * Note that bootstrapping is finished, and set the HWRPB up
     95 	 * to do restarts.
     96 	 */
     97 	hwrpb_restart_setup();
     98 }
     99 
    100 static void
    101 qemu_find_rootdev(void)
    102 {
    103 	char buf[32];
    104 
    105 	/*
    106 	 * Check for "rootdev=wd0".
    107 	 */
    108 	if (prom_qemu_getenv("rootdev", buf, sizeof(buf))) {
    109 		snprintf(bootinfo.booted_dev, sizeof(bootinfo.booted_dev),
    110 		    "rootdev=%s", buf);
    111 		booted_device = device_find_by_xname(buf);
    112 		return;
    113 	}
    114 
    115 	/*
    116 	 * Check for "root=/dev/wd0a", "root=/dev/hda1", etc.
    117 	 */
    118 	if (prom_qemu_getenv("root", buf, sizeof(buf))) {
    119 		const size_t devlen = strlen("/dev/");
    120 		const char *cp = buf;
    121 		char *ecp = &buf[sizeof(buf) - 1];
    122 
    123 		snprintf(bootinfo.booted_dev, sizeof(bootinfo.booted_dev),
    124 		    "root=%s", buf);
    125 
    126 		/* Find the start of the device xname. */
    127 		if (strlen(cp) > devlen && strncmp(cp, "/dev/", devlen) == 0) {
    128 			cp += devlen;
    129 		}
    130 
    131 		/* Now strip any partition letter off the end. */
    132 		while (ecp != cp) {
    133 			if (*ecp >= '0' && *ecp <= '9') {
    134 				break;
    135 			}
    136 			*ecp-- = '\0';
    137 		}
    138 
    139 		booted_device = device_find_by_xname(cp);
    140 		return;
    141 	}
    142 
    143 	printf("WARNING: no rootdev= or root= arguments provided by Qemu\n");
    144 }
    145 
    146 void
    147 cpu_rootconf(void)
    148 {
    149 
    150 	if (booted_device == NULL && alpha_is_qemu) {
    151 		qemu_find_rootdev();
    152 	}
    153 
    154 	if (booted_device == NULL) {
    155 		printf("WARNING: can't figure what device matches \"%s\"\n",
    156 		    bootinfo.booted_dev);
    157 	}
    158 	rootconf();
    159 }
    160 
    161 void
    162 parse_prom_bootdev(void)
    163 {
    164 	static char hacked_boot_dev[128];
    165 	static struct bootdev_data bd;
    166 	char *cp, *scp, *boot_fields[8];
    167 	int i, done;
    168 
    169 	booted_device = NULL;
    170 	booted_partition = 0;
    171 	bootdev_data = NULL;
    172 
    173 	memcpy(hacked_boot_dev, bootinfo.booted_dev,
    174 	    uimin(sizeof bootinfo.booted_dev, sizeof hacked_boot_dev));
    175 #if 0
    176 	printf("parse_prom_bootdev: boot dev = \"%s\"\n", hacked_boot_dev);
    177 #endif
    178 
    179 	i = 0;
    180 	scp = cp = hacked_boot_dev;
    181 	for (done = 0; !done; cp++) {
    182 		if (*cp != ' ' && *cp != '\0')
    183 			continue;
    184 		if (*cp == '\0')
    185 			done = 1;
    186 
    187 		*cp = '\0';
    188 		boot_fields[i++] = scp;
    189 		scp = cp + 1;
    190 		if (i == 8)
    191 			done = 1;
    192 	}
    193 	if (i != 8)
    194 		return;		/* doesn't look like anything we know! */
    195 
    196 #if 0
    197 	printf("i = %d, done = %d\n", i, done);
    198 	for (i--; i >= 0; i--)
    199 		printf("%d = %s\n", i, boot_fields[i]);
    200 #endif
    201 
    202 	bd.protocol = boot_fields[0];
    203 	bd.bus = atoi(boot_fields[1]);
    204 	bd.slot = atoi(boot_fields[2]);
    205 	bd.channel = atoi(boot_fields[3]);
    206 	bd.remote_address = boot_fields[4];
    207 	bd.unit = atoi(boot_fields[5]);
    208 	bd.boot_dev_type = atoi(boot_fields[6]);
    209 	bd.ctrl_dev_type = boot_fields[7];
    210 
    211 #if 0
    212 	printf("parsed: proto = %s, bus = %d, slot = %d, channel = %d,\n",
    213 	    bd.protocol, bd.bus, bd.slot, bd.channel);
    214 	printf("\tremote = %s, unit = %d, dev_type = %d, ctrl_type = %s\n",
    215 	    bd.remote_address, bd.unit, bd.boot_dev_type, bd.ctrl_dev_type);
    216 #endif
    217 
    218 	bootdev_data = &bd;
    219 }
    220 
    221 static inline int
    222 atoi(const char *s)
    223 {
    224 	return (int)strtoll(s, NULL, 10);
    225 }
    226 
    227 void
    228 device_register(device_t dev, void *aux)
    229 {
    230 #if NPCI > 0
    231 	device_t parent = device_parent(dev);
    232 
    233 	if (parent != NULL && device_is_a(parent, "pci"))
    234 		device_pci_register(dev, aux);
    235 #endif
    236 
    237 	if (bootdev_data == NULL) {
    238 		/*
    239 		 * There is no hope.
    240 		 */
    241 		return;
    242 	}
    243 	if (platform.device_register)
    244 		(*platform.device_register)(dev, aux);
    245 }
    246