1/*	$NetBSD: autoconf.c,v 1.11 2021/08/07 16:18:52 thorpej Exp $	*/
2/*-
3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 *
10 * This material is based upon work supported by the Defense Advanced Research
11 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 * Contract No. N66001-09-C-2073.
13 * Approved for Public Release, Distribution Unlimited
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.11 2021/08/07 16:18:52 thorpej Exp $");
39
40#define __INTR_PRIVATE
41
42#include "locators.h"
43
44#include <sys/param.h>
45#include <sys/conf.h>
46#include <sys/cpu.h>
47#include <sys/device.h>
48#include <sys/intr.h>
49#include <sys/kernel.h>
50#include <sys/proc.h>
51#include <sys/systm.h>
52
53#include <powerpc/booke/cpuvar.h>
54
55/*
56 * Determine device configuration for a machine.
57 */
58void
59cpu_configure(void)
60{
61
62	intr_init();
63	calc_delayconst();
64
65	if (config_rootfound("mainbus", NULL) == NULL)
66		panic("%s: mainbus not configured", __func__);
67
68	spl0();
69}
70
71static volatile int rootconf_timo = 1;
72
73/*
74 * Setup root device.
75 * Configure swap area.
76 */
77void
78cpu_rootconf(void)
79{
80	/*
81	 * We wait up to 10 seconds for a bootable device to be found.
82	 */
83	while (rootconf_timo > 0) {
84		if (booted_device != NULL) {
85			aprint_normal_dev(booted_device, "boot device\n");
86			break;
87		}
88
89		if (root_string[0] != '\0'
90		    && (booted_device = device_find_by_xname(root_string)) != NULL) {
91			aprint_normal_dev(booted_device, "boot device\n");
92			break;
93		}
94
95		if (EWOULDBLOCK == kpause("autoconf", true, 1, NULL)) {
96			rootconf_timo--;
97		}
98	}
99
100	rootconf();
101}
102
103void
104device_register(device_t dev, void *aux)
105{
106	if (cpu_md_ops.md_device_register != NULL)
107		(*cpu_md_ops.md_device_register)(dev, aux);
108
109	if (booted_device == NULL) {
110		if (root_string[0] != '\0'
111		    && !strcmp(device_xname(dev), root_string)) {
112			aprint_normal_dev(dev, "boot device\n");
113			booted_device = dev;
114		} else {
115			rootconf_timo = 5 * hz;
116		}
117	}
118}
119
120static bool mainbus_found;
121
122static int
123mainbus_print(void *aux, const char *pnp)
124{
125	struct mainbus_attach_args *ma = aux;
126
127	if (pnp != NULL)
128		return QUIET;
129
130	if (pnp)
131		aprint_normal("%s at %s", ma->ma_name, pnp);
132	if (ma->ma_node != MAINBUSCF_NODE_DEFAULT)
133		aprint_normal(" node %d", ma->ma_node);
134
135	return (UNCONF);
136}
137
138static int
139mainbus_match(device_t parent, cfdata_t cf, void *aux)
140{
141	return mainbus_found == false;
142}
143
144static void
145mainbus_attach(device_t parent, device_t self, void *aux)
146{
147	struct mainbus_attach_args ma;
148
149	mainbus_found = true;
150
151	aprint_normal("\n");
152
153	ma.ma_name = "cpunode";
154	ma.ma_node = 0;
155	ma.ma_memt = curcpu()->ci_softc->cpu_bst;
156	ma.ma_le_memt = curcpu()->ci_softc->cpu_le_bst;
157	ma.ma_dmat = &booke_bus_dma_tag;
158
159	config_found(self, &ma, mainbus_print, CFARGS_NONE);
160}
161
162CFATTACH_DECL_NEW(mainbus, 0, mainbus_match, mainbus_attach, NULL, NULL);
163