Module Name:    src
Committed By:   cherry
Date:           Sat Dec 22 07:45:59 UTC 2018

Modified Files:
        src/sys/arch/amd64/amd64: amd64_mainbus.c
        src/sys/arch/amd64/conf: XEN3_DOM0 files.amd64
        src/sys/arch/i386/conf: XEN3PAE_DOM0 files.i386
        src/sys/arch/i386/i386: i386_mainbus.c
        src/sys/arch/x86/include: autoconf.h
        src/sys/arch/xen/conf: files.xen
        src/sys/arch/xen/x86: autoconf.c xen_mainbus.c
Added Files:
        src/sys/arch/x86/x86: mainbus.c

Log Message:
This change modifies the mainbus(4) entry point for all x86 sub-archs
in the following way:

i) It provides a unified entry point in
   x86/x86/mainbus.c:mainbus_attach()
ii) It carves out the preliminary bus attachment sequence that is
   common to all sub-archs into
   x86/x86/mainbus.c: x86_cpubus_attach()
iii) It consolidates the remaining pathways as internal callee
   functions so that these may be called piecemeal if required. A
   special usecase of this is XEN PVHVM which may need to call the
   native configure path, the xen configure path, or both.
iv) It moves the driver private data structures from
   i386/i386_mainbus.c to an x86/ level one. This allows for other
   sub-arch's to do similar, if needed. (They do not at the moment).
v) For dom0 kernels, it enables 'acpi0 at mainbus?' and
   'acpi0 at hypervisorbus'. This serves two purposes:
   a) To demonstrate the possibility of dynamic configuration tree
      traversal ordering changes.
   b) To allow for the common acpi_check(self, "acpibus") call in
   x86/mainbus.c to not barf when it is called from the dom0 attach
   path. We allow for the acpi0 device to be a child of mainbus with
   the changes to amd64/conf/XEN3_DOM0 and i386/conf/XEN3PAE_DOM0
   without actually probing further in the code. This path will later
   be pursued in a PVHVM boot codepath.

There should be no operative changes with this change. If there are,
please complain loudly.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/amd64/amd64_mainbus.c
cvs rdiff -u -r1.160 -r1.161 src/sys/arch/amd64/conf/XEN3_DOM0
cvs rdiff -u -r1.109 -r1.110 src/sys/arch/amd64/conf/files.amd64
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/i386/conf/XEN3PAE_DOM0
cvs rdiff -u -r1.398 -r1.399 src/sys/arch/i386/conf/files.i386
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/i386/i386/i386_mainbus.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/include/autoconf.h
cvs rdiff -u -r0 -r1.1 src/sys/arch/x86/x86/mainbus.c
cvs rdiff -u -r1.171 -r1.172 src/sys/arch/xen/conf/files.xen
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/xen/x86/autoconf.c
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/xen/x86/xen_mainbus.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/amd64_mainbus.c
diff -u src/sys/arch/amd64/amd64/amd64_mainbus.c:1.2 src/sys/arch/amd64/amd64/amd64_mainbus.c:1.3
--- src/sys/arch/amd64/amd64/amd64_mainbus.c:1.2	Sat Dec 22 06:59:27 2018
+++ src/sys/arch/amd64/amd64/amd64_mainbus.c	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: amd64_mainbus.c,v 1.2 2018/12/22 06:59:27 cherry Exp $	*/
+/*	$NetBSD: amd64_mainbus.c,v 1.3 2018/12/22 07:45:58 cherry Exp $	*/
 /*	NetBSD: mainbus.c,v 1.39 2018/12/02 08:19:44 cherry Exp 	*/
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: amd64_mainbus.c,v 1.2 2018/12/22 06:59:27 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amd64_mainbus.c,v 1.3 2018/12/22 07:45:58 cherry Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -83,15 +83,11 @@ __KERNEL_RCSID(0, "$NetBSD: amd64_mainbu
  * XXXfvdl ACPI
  */
 
-int	mainbus_match(device_t, cfdata_t, void *);
-void	mainbus_attach(device_t, device_t, void *);
+int	amd64_mainbus_match(device_t, cfdata_t, void *);
+void	amd64_mainbus_attach(device_t, device_t, void *);
+int	amd64_mainbus_print(void *, const char *);
 
-CFATTACH_DECL_NEW(mainbus, 0,
-    mainbus_match, mainbus_attach, NULL, NULL);
-
-int	mainbus_print(void *, const char *);
-
-union mainbus_attach_args {
+union amd64_mainbus_attach_args {
 	const char *mba_busname;		/* first elem of all */
 	struct pcibus_attach_args mba_pba;
 	struct isabus_attach_args mba_iba;
@@ -128,8 +124,8 @@ int mp_nintr;
 int mp_isa_bus = -1;
 int mp_eisa_bus = -1;
 
-bool acpi_present;
-bool mpacpi_active;
+extern bool acpi_present;
+extern bool mpacpi_active;
 
 # ifdef MPVERBOSE
 #  if MPVERBOSE > 0
@@ -147,7 +143,7 @@ int mp_verbose = 0;
  * Probe for the mainbus; always succeeds.
  */
 int
-mainbus_match(device_t parent, cfdata_t match, void *aux)
+amd64_mainbus_match(device_t parent, cfdata_t match, void *aux)
 {
 
 	return 1;
@@ -157,82 +153,10 @@ mainbus_match(device_t parent, cfdata_t 
  * Attach the mainbus.
  */
 void
-mainbus_attach(device_t parent, device_t self, void *aux)
+amd64_mainbus_attach(device_t parent, device_t self, void *aux)
 {
 #if NPCI > 0 || NACPICA > 0 || NIPMI > 0
-	union mainbus_attach_args mba;
-#endif
-#if NPCI > 0
-	int mode;
-#endif
-#ifdef MPBIOS
-	int mpbios_present = 0;
-#endif
-	int numcpus = 0;
-#if defined(PCI_BUS_FIXUP)
-	int pci_maxbus = 0;
-#endif
-
-	aprint_naive("\n");
-	aprint_normal("\n");
-
-#ifdef MPBIOS
-	mpbios_present = mpbios_probe(self);
-#endif
-
-#if NPCI > 0
-#ifdef __HAVE_PCI_MSI_MSIX
-	msipic_init();
-#endif
-
-	/*
-	 * ACPI needs to be able to access PCI configuration space.
-	 */
-	mode = pci_mode_detect();
-#if defined(PCI_BUS_FIXUP)
-	if (mode != 0) {
-		pci_maxbus = pci_bus_fixup(NULL, 0);
-		aprint_debug("PCI bus max, after pci_bus_fixup: %i\n",
-		    pci_maxbus);
-#if defined(PCI_ADDR_FIXUP)
-		pciaddr.extent_port = NULL;
-		pciaddr.extent_mem = NULL;
-		pci_addr_fixup(NULL, pci_maxbus);
-#endif
-	}
-#endif
-#endif
-
-#if NACPICA > 0
-	if ((boothowto & RB_MD2) == 0 && acpi_check(self, "acpibus"))
-		acpi_present = acpi_probe() != 0;
-	/*
-	 * First, see if the MADT contains CPUs, and possibly I/O APICs.
-	 * Building the interrupt routing structures can only
-	 * be done later (via a callback).
-	 */
-	if (acpi_present)
-		mpacpi_active = mpacpi_scan_apics(self, &numcpus) != 0;
-
-	if (!mpacpi_active) {
-#endif		
-#ifdef MPBIOS
-		if (mpbios_present)
-			mpbios_scan(self, &numcpus);
-		else
-#endif
-		if (numcpus == 0) {
-			struct cpu_attach_args caa;
-                        
-			memset(&caa, 0, sizeof(caa));
-			caa.cpu_number = 0;
-			caa.cpu_role = CPU_ROLE_SP;
-			caa.cpu_func = 0;
-                        
-			config_found_ia(self, "cpubus", &caa, mainbus_print);
-		}
-#if NACPICA > 0		
-	}
+	union amd64_mainbus_attach_args mba;
 #endif
 
 #if NISADMA > 0 && NACPICA > 0
@@ -268,7 +192,7 @@ mainbus_attach(device_t parent, device_t
 #endif
 
 #if NPCI > 0
-	if (mode != 0) {
+	if (pci_mode_detect() != 0) {
 		int npcibus = 0;
 
 		mba.mba_pba.pba_iot = x86_bus_space_io;
@@ -316,9 +240,9 @@ mainbus_attach(device_t parent, device_t
 }
 
 int
-mainbus_print(void *aux, const char *pnp)
+amd64_mainbus_print(void *aux, const char *pnp)
 {
-	union mainbus_attach_args *mba = aux;
+	union amd64_mainbus_attach_args *mba = aux;
 
 	if (pnp)
 		aprint_normal("%s at %s", mba->mba_busname, pnp);

Index: src/sys/arch/amd64/conf/XEN3_DOM0
diff -u src/sys/arch/amd64/conf/XEN3_DOM0:1.160 src/sys/arch/amd64/conf/XEN3_DOM0:1.161
--- src/sys/arch/amd64/conf/XEN3_DOM0:1.160	Wed Dec 12 06:29:36 2018
+++ src/sys/arch/amd64/conf/XEN3_DOM0	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-# $NetBSD: XEN3_DOM0,v 1.160 2018/12/12 06:29:36 maxv Exp $
+# $NetBSD: XEN3_DOM0,v 1.161 2018/12/22 07:45:58 cherry Exp $
 
 include 	"arch/amd64/conf/std.xen"
 
@@ -10,7 +10,7 @@ options 	INCLUDE_CONFIG_FILE	# embed con
 #options 	UVMHIST_PRINT
 #options 	SYSCALL_DEBUG
 
-#ident		"XEN3_DOM0-$Revision: 1.160 $"
+#ident		"XEN3_DOM0-$Revision: 1.161 $"
 
 maxusers	32		# estimated number of users
 
@@ -202,7 +202,9 @@ xenbus* 	at hypervisor?		# Xen virtual b
 xencons*	at hypervisor?		# Xen virtual console
 balloon*	at xenbus?		# Xen balloon device
 
-acpi0		at hypervisor?
+acpi0		at hypervisor?		# ACPI access in PV mode
+acpi0		at mainbus?		# ACPI access in PVH(VM) mode
+
 #options 	ACPI_ACTIVATE_DEV	# If set, activate inactive devices
 options 	ACPI_SCANPCI		# find PCI roots using ACPI
 #options 	ACPICA_PEDANTIC		# force strict conformance to the Spec.

Index: src/sys/arch/amd64/conf/files.amd64
diff -u src/sys/arch/amd64/conf/files.amd64:1.109 src/sys/arch/amd64/conf/files.amd64:1.110
--- src/sys/arch/amd64/conf/files.amd64:1.109	Sat Dec 22 06:33:36 2018
+++ src/sys/arch/amd64/conf/files.amd64	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.amd64,v 1.109 2018/12/22 06:33:36 cherry Exp $
+#	$NetBSD: files.amd64,v 1.110 2018/12/22 07:45:58 cherry Exp $
 #
 # new style config file for amd64 architecture
 #
@@ -95,6 +95,7 @@ include	"dev/i2o/files.i2o"
 device	mainbus: isabus, pcibus, bios32, acpibus, cpubus, ioapicbus, ipmibus
 attach	mainbus at root
 file	arch/amd64/amd64/amd64_mainbus.c	mainbus
+file	arch/x86/x86/mainbus.c			mainbus
 
 #
 # PCI-only drivers

Index: src/sys/arch/i386/conf/XEN3PAE_DOM0
diff -u src/sys/arch/i386/conf/XEN3PAE_DOM0:1.9 src/sys/arch/i386/conf/XEN3PAE_DOM0:1.10
--- src/sys/arch/i386/conf/XEN3PAE_DOM0:1.9	Wed Dec 12 06:29:37 2018
+++ src/sys/arch/i386/conf/XEN3PAE_DOM0	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: XEN3PAE_DOM0,v 1.9 2018/12/12 06:29:37 maxv Exp $
+#	$NetBSD: XEN3PAE_DOM0,v 1.10 2018/12/22 07:45:58 cherry Exp $
 #
 #	XEN3_0: Xen 3.0 domain0 kernel
 
@@ -877,7 +877,9 @@ pci*	at ppb? bus ?
 # pciback.hide=(00:1a.0)(00:1a.1)(00:1a.7)
 pciback* at pci?			#pci backend device
 
-acpi0		at hypervisor?
+acpi0		at hypervisor?		# ACPI access in PV mode
+acpi0		at mainbus?		# ACPI access in PVH(VM) mode
+
 options 	MPBIOS
 #options 	MPDEBUG
 #options 	MPVERBOSE

Index: src/sys/arch/i386/conf/files.i386
diff -u src/sys/arch/i386/conf/files.i386:1.398 src/sys/arch/i386/conf/files.i386:1.399
--- src/sys/arch/i386/conf/files.i386:1.398	Sat Dec 22 06:33:36 2018
+++ src/sys/arch/i386/conf/files.i386	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.i386,v 1.398 2018/12/22 06:33:36 cherry Exp $
+#	$NetBSD: files.i386,v 1.399 2018/12/22 07:45:58 cherry Exp $
 #
 # new style config file for i386 architecture
 #
@@ -130,6 +130,7 @@ device	mainbus: isabus, eisabus, mcabus,
 	bioscall
 attach	mainbus at root
 file	arch/i386/i386/i386_mainbus.c	mainbus
+file	arch/x86/x86/mainbus.c		mainbus
 
 #
 # PCI-only drivers

Index: src/sys/arch/i386/i386/i386_mainbus.c
diff -u src/sys/arch/i386/i386/i386_mainbus.c:1.2 src/sys/arch/i386/i386/i386_mainbus.c:1.3
--- src/sys/arch/i386/i386/i386_mainbus.c:1.2	Sat Dec 22 06:59:27 2018
+++ src/sys/arch/i386/i386/i386_mainbus.c	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: i386_mainbus.c,v 1.2 2018/12/22 06:59:27 cherry Exp $	*/
+/*	$NetBSD: i386_mainbus.c,v 1.3 2018/12/22 07:45:58 cherry Exp $	*/
 /*	NetBSD: mainbus.c,v 1.104 2018/12/02 08:19:44 cherry Exp 	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: i386_mainbus.c,v 1.2 2018/12/22 06:59:27 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i386_mainbus.c,v 1.3 2018/12/22 07:45:58 cherry Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -76,6 +76,8 @@ __KERNEL_RCSID(0, "$NetBSD: i386_mainbus
 #include <dev/mca/mcavar.h>
 #endif
 
+#include <x86/autoconf.h>
+
 #if NIPMI > 0
 #include <x86/ipmivar.h>
 #endif
@@ -92,28 +94,12 @@ __KERNEL_RCSID(0, "$NetBSD: i386_mainbus
 #endif /* __HAVE_PCI_MSI_MSIX */
 #endif
 
-void	mainbus_childdetached(device_t, device_t);
-int	mainbus_match(device_t, cfdata_t, void *);
-void	mainbus_attach(device_t, device_t, void *);
-
-static int	mainbus_rescan(device_t, const char *, const int *);
-
-struct mainbus_softc {
-	device_t	sc_acpi;
-	device_t	sc_dev;
-	device_t	sc_ipmi;
-	device_t	sc_pci;
-	device_t	sc_mca;
-	device_t	sc_pnpbios;
-};
-
-CFATTACH_DECL2_NEW(mainbus, sizeof(struct mainbus_softc),
-    mainbus_match, mainbus_attach, NULL, NULL, mainbus_rescan,
-    mainbus_childdetached);
+void	i386_mainbus_childdetached(device_t, device_t);
+int	i386_mainbus_match(device_t, cfdata_t, void *);
+void	i386_mainbus_attach(device_t, device_t, void *);
+int	i386_mainbus_rescan(device_t, const char *, const int *);
 
-int	mainbus_print(void *, const char *);
-
-union mainbus_attach_args {
+union i386_mainbus_attach_args {
 	const char *mba_busname;		/* first elem of all */
 	struct pcibus_attach_args mba_pba;
 	struct eisabus_attach_args mba_eba;
@@ -162,8 +148,8 @@ int mp_nintr;
 int mp_isa_bus = -1;            /* XXX */
 int mp_eisa_bus = -1;           /* XXX */
 
-bool acpi_present;
-bool mpacpi_active;
+extern bool acpi_present;
+extern bool mpacpi_active;
 
 # ifdef MPVERBOSE
 #  if MPVERBOSE > 0
@@ -177,7 +163,7 @@ int mp_verbose = 0;
 #endif
 
 void
-mainbus_childdetached(device_t self, device_t child)
+i386_mainbus_childdetached(device_t self, device_t child)
 {
 	struct mainbus_softc *sc = device_private(self);
 
@@ -201,7 +187,7 @@ mainbus_childdetached(device_t self, dev
  * Probe for the mainbus; always succeeds.
  */
 int
-mainbus_match(device_t parent, cfdata_t match, void *aux)
+i386_mainbus_match(device_t parent, cfdata_t match, void *aux)
 {
 
 	return 1;
@@ -211,83 +197,13 @@ mainbus_match(device_t parent, cfdata_t 
  * Attach the mainbus.
  */
 void
-mainbus_attach(device_t parent, device_t self, void *aux)
+i386_mainbus_attach(device_t parent, device_t self, void *aux)
 {
-#if NPCI > 0
-	int mode;
-#endif
 	struct mainbus_softc *sc = device_private(self);
-	union mainbus_attach_args mba;
-#ifdef MPBIOS
-	int mpbios_present = 0;
-#endif
-#if defined(PCI_BUS_FIXUP)
-	int pci_maxbus = 0;
-#endif
-	int numcpus = 0;
+	union i386_mainbus_attach_args mba;
 
 	sc->sc_dev = self;
 
-	aprint_naive("\n");
-	aprint_normal("\n");
-
-#ifdef MPBIOS
-	mpbios_present = mpbios_probe(self);
-#endif
-
-#if NPCI > 0
-	msipic_init();
-
-	/*
-	 * ACPI needs to be able to access PCI configuration space.
-	 */
-	mode = pci_mode_detect();
-#if defined(PCI_BUS_FIXUP)
-	if (mode != 0) {
-		pci_maxbus = pci_bus_fixup(NULL, 0);
-		aprint_debug("PCI bus max, after pci_bus_fixup: %i\n",
-		    pci_maxbus);
-#if defined(PCI_ADDR_FIXUP)
-		pciaddr.extent_port = NULL;
-		pciaddr.extent_mem = NULL;
-		pci_addr_fixup(NULL, pci_maxbus);
-#endif
-	}
-#else
-	__USE(mode);
-#endif
-#endif
-
-#if NACPICA > 0
-	if ((boothowto & RB_MD2) == 0 && acpi_check(self, "acpibus"))
-		acpi_present = acpi_probe() != 0;
-	/*
-	 * First, see if the MADT contains CPUs, and possibly I/O APICs.
-	 * Building the interrupt routing structures can only
-	 * be done later (via a callback).
-	 */
-	if (acpi_present)
-		mpacpi_active = mpacpi_scan_apics(self, &numcpus) != 0;
-#endif
-
-	if (!mpacpi_active) {
-#ifdef MPBIOS
-		if (mpbios_present)
-			mpbios_scan(self, &numcpus);
-		else
-#endif
-		if (numcpus == 0) {
-			struct cpu_attach_args caa;
-
-			memset(&caa, 0, sizeof(caa));
-			caa.cpu_number = 0;
-			caa.cpu_role = CPU_ROLE_SP;
-			caa.cpu_func = 0;
-
-			config_found_ia(self, "cpubus", &caa, mainbus_print);
-		}
-	}
-
 #if NISADMA > 0 && (NACPICA > 0 || NPNPBIOS > 0)
 	/*
 	 * ACPI and PNPBIOS need ISA DMA initialized before they start probing.
@@ -296,15 +212,15 @@ mainbus_attach(device_t parent, device_t
 	    self);
 #endif
 
-	mainbus_rescan(self, "acpibus", NULL);
+	i386_mainbus_rescan(self, "acpibus", NULL);
 
-	mainbus_rescan(self, "pnpbiosbus", NULL);
+	i386_mainbus_rescan(self, "pnpbiosbus", NULL);
 
-	mainbus_rescan(self, "ipmibus", NULL);
+	i386_mainbus_rescan(self, "ipmibus", NULL);
 
-	mainbus_rescan(self, "pcibus", NULL);
+	i386_mainbus_rescan(self, "pcibus", NULL);
 
-	mainbus_rescan(self, "mcabus", NULL);
+	i386_mainbus_rescan(self, "mcabus", NULL);
 
 	if (memcmp(ISA_HOLE_VADDR(EISA_ID_PADDR), EISA_ID, EISA_ID_LEN) == 0 &&
 	    eisa_has_been_seen == 0) {
@@ -330,12 +246,12 @@ mainbus_attach(device_t parent, device_t
 }
 
 /* scan for new children */
-static int
-mainbus_rescan(device_t self, const char *ifattr, const int *locators)
+int
+i386_mainbus_rescan(device_t self, const char *ifattr, const int *locators)
 {
 	struct mainbus_softc *sc = device_private(self);
 #if NACPICA > 0 || NIPMI > 0 || NMCA > 0 || NPCI > 0 || NPNPBIOS > 0
-	union mainbus_attach_args mba;
+	union i386_mainbus_attach_args mba;
 #endif
 
 	if (ifattr_match(ifattr, "acpibus") && sc->sc_acpi == NULL &&
@@ -455,12 +371,3 @@ mainbus_rescan(device_t self, const char
 	return 0;
 }
 
-int
-mainbus_print(void *aux, const char *pnp)
-{
-	union mainbus_attach_args *mba = aux;
-
-	if (pnp)
-		aprint_normal("%s at %s", mba->mba_busname, pnp);
-	return (UNCONF);
-}

Index: src/sys/arch/x86/include/autoconf.h
diff -u src/sys/arch/x86/include/autoconf.h:1.4 src/sys/arch/x86/include/autoconf.h:1.5
--- src/sys/arch/x86/include/autoconf.h:1.4	Wed Sep 21 00:00:07 2016
+++ src/sys/arch/x86/include/autoconf.h	Sat Dec 22 07:45:58 2018
@@ -1,9 +1,25 @@
-/* $NetBSD: autoconf.h,v 1.4 2016/09/21 00:00:07 jmcneill Exp $ */
+/* $NetBSD: autoconf.h,v 1.5 2018/12/22 07:45:58 cherry Exp $ */
 #ifndef _X86_AUTOCONF_H_
 #define _X86_AUTOCONF_H_
 
 #include <sys/device.h>
 
+/*
+ * device private data for mainbus.
+ * subr_autoconf.c uses sizeof() to allocate private memory for this
+ * data structure.
+ */
+struct mainbus_softc {
+#if defined(__i386__)	
+	device_t	sc_acpi;
+	device_t	sc_dev;
+	device_t	sc_ipmi;
+	device_t	sc_pci;
+	device_t	sc_mca;
+	device_t	sc_pnpbios;
+#endif
+};
+
 void device_pci_props_register(device_t, void *);
 device_t device_pci_register(device_t, void *);
 device_t device_isa_register(device_t, void *);

Index: src/sys/arch/xen/conf/files.xen
diff -u src/sys/arch/xen/conf/files.xen:1.171 src/sys/arch/xen/conf/files.xen:1.172
--- src/sys/arch/xen/conf/files.xen:1.171	Sat Dec 22 06:33:37 2018
+++ src/sys/arch/xen/conf/files.xen	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.xen,v 1.171 2018/12/22 06:33:37 cherry Exp $
+#	$NetBSD: files.xen,v 1.172 2018/12/22 07:45:58 cherry Exp $
 #	NetBSD: files.x86,v 1.10 2003/10/08 17:30:00 bouyer Exp 
 #	NetBSD: files.i386,v 1.254 2004/03/25 23:32:10 jmc Exp 
 
@@ -171,9 +171,10 @@ define ipmibus {}
 # System bus types
 #
 
-device mainbus: cpubus, ioapicbus, hypervisorbus, bios32, ipmibus
+device mainbus: cpubus, ioapicbus, hypervisorbus, bios32, ipmibus, acpibus
 attach	mainbus at root
 file	arch/xen/x86/xen_mainbus.c		mainbus
+file	arch/x86/x86/mainbus.c			mainbus
 
 # Xen hypervisor
 device	hypervisor { [apid = -1]}: isabus, pcibus, sysmon_power, xendevbus, acpibus

Index: src/sys/arch/xen/x86/autoconf.c
diff -u src/sys/arch/xen/x86/autoconf.c:1.20 src/sys/arch/xen/x86/autoconf.c:1.21
--- src/sys/arch/xen/x86/autoconf.c:1.20	Sun Oct  7 11:25:55 2018
+++ src/sys/arch/xen/x86/autoconf.c	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: autoconf.c,v 1.20 2018/10/07 11:25:55 mlelstv Exp $	*/
+/*	$NetBSD: autoconf.c,v 1.21 2018/12/22 07:45:58 cherry Exp $	*/
 /*	NetBSD: autoconf.c,v 1.75 2003/12/30 12:33:22 pk Exp 	*/
 
 /*-
@@ -45,7 +45,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.20 2018/10/07 11:25:55 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.21 2018/12/22 07:45:58 cherry Exp $");
 
 #include "opt_xen.h"
 #include "opt_multiprocessor.h"
@@ -133,6 +133,9 @@ cpu_configure(void)
 	intr_printconfig();
 #endif
 
+#if NIOAPIC > 0
+	ioapic_enable();
+#endif
 	/* resync cr0 after FPU configuration */
 	pcb = lwp_getpcb(&lwp0);
 	pcb->pcb_cr0 = rcr0();

Index: src/sys/arch/xen/x86/xen_mainbus.c
diff -u src/sys/arch/xen/x86/xen_mainbus.c:1.2 src/sys/arch/xen/x86/xen_mainbus.c:1.3
--- src/sys/arch/xen/x86/xen_mainbus.c:1.2	Sat Dec 22 06:59:27 2018
+++ src/sys/arch/xen/x86/xen_mainbus.c	Sat Dec 22 07:45:58 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: xen_mainbus.c,v 1.2 2018/12/22 06:59:27 cherry Exp $	*/
+/*	$NetBSD: xen_mainbus.c,v 1.3 2018/12/22 07:45:58 cherry Exp $	*/
 /*	NetBSD: mainbus.c,v 1.19 2017/05/23 08:54:39 nonaka Exp 	*/
 /*	NetBSD: mainbus.c,v 1.53 2003/10/27 14:11:47 junyoung Exp 	*/
 
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xen_mainbus.c,v 1.2 2018/12/22 06:59:27 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xen_mainbus.c,v 1.3 2018/12/22 07:45:58 cherry Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -88,8 +88,6 @@ int mp_nintr;
 int mp_isa_bus = -1;	    /* XXX */
 int mp_eisa_bus = -1;	   /* XXX */
 
-bool acpi_present;
-bool mpacpi_active;
 #ifdef MPVERBOSE
 int mp_verbose = 1;
 #else /* MPVERBOSE */
@@ -98,16 +96,14 @@ int mp_verbose = 0;
 #endif /* defined(MPBIOS) || NACPICA > 0 */
 #endif /* NPCI > 0 */
 
+extern bool acpi_present;
+extern bool mpacpi_active;
 
-int	mainbus_match(device_t, cfdata_t, void *);
-void	mainbus_attach(device_t, device_t, void *);
+int	xen_mainbus_match(device_t, cfdata_t, void *);
+void	xen_mainbus_attach(device_t, device_t, void *);
+int	xen_mainbus_print(void *, const char *);
 
-CFATTACH_DECL_NEW(mainbus, 0,
-    mainbus_match, mainbus_attach, NULL, NULL);
-
-int	mainbus_print(void *, const char *);
-
-union mainbus_attach_args {
+union xen_mainbus_attach_args {
 	const char *mba_busname;		/* first elem of all */
 	struct cpu_attach_args mba_caa;
 #if NHYPERVISOR > 0
@@ -122,7 +118,7 @@ union mainbus_attach_args {
  * Probe for the mainbus; always succeeds.
  */
 int
-mainbus_match(device_t parent, cfdata_t match, void *aux)
+xen_mainbus_match(device_t parent, cfdata_t match, void *aux)
 {
 
 	return 1;
@@ -132,69 +128,9 @@ mainbus_match(device_t parent, cfdata_t 
  * Attach the mainbus.
  */
 void
-mainbus_attach(device_t parent, device_t self, void *aux)
+xen_mainbus_attach(device_t parent, device_t self, void *aux)
 {
-	union mainbus_attach_args mba;
-#if defined(DOM0OPS)
-	int numcpus = 0;
-#ifdef MPBIOS
-	int mpbios_present = 0;
-#endif
-#ifdef PCI_BUS_FIXUP
-	int pci_maxbus = 0;
-#endif
-#endif /* defined(DOM0OPS) */
-
-	aprint_naive("\n");
-	aprint_normal("\n");
-
-#ifdef DOM0OPS
-	if (xendomain_is_dom0()) {
-#ifdef MPBIOS
-		mpbios_present = mpbios_probe(self);
-#endif
-#if NPCI > 0
-		/* ACPI needs to be able to access PCI configuration space. */
-		pci_mode_detect();
-#ifdef PCI_BUS_FIXUP
-		if (pci_mode != 0) {
-			pci_maxbus = pci_bus_fixup(NULL, 0);
-			aprint_debug_dev(self, "PCI bus max, after "
-			    "pci_bus_fixup: %i\n", pci_maxbus);
-#ifdef PCI_ADDR_FIXUP
-			pciaddr.extent_port = NULL;
-			pciaddr.extent_mem = NULL;
-			pci_addr_fixup(NULL, pci_maxbus);
-#endif /* PCI_ADDR_FIXUP */
-		}
-#endif /* PCI_BUS_FIXUP */
-#if NACPICA > 0
-		acpi_present = acpi_probe() != 0;
-		if (acpi_present)
-			mpacpi_active = mpacpi_scan_apics(self, &numcpus) != 0;
-		if (!mpacpi_active)
-#endif
-		{
-#ifdef MPBIOS
-			if (mpbios_present)
-				mpbios_scan(self, &numcpus);       
-			else
-#endif
-			if (numcpus == 0) {
-				memset(&mba.mba_caa, 0, sizeof(mba.mba_caa));
-				mba.mba_caa.cpu_number = 0;
-				mba.mba_caa.cpu_role = CPU_ROLE_SP;
-				mba.mba_caa.cpu_func = 0;
-				config_found_ia(self, "cpubus",
-				    &mba.mba_caa, mainbus_print);
-			}
-		}
-#if NIOAPIC > 0
-	ioapic_enable();
-#endif
-#endif /* NPCI */
-	}
-#endif /* DOM0OPS */
+	union xen_mainbus_attach_args mba;
 
 #if NIPMI > 0
 	memset(&mba.mba_ipmi, 0, sizeof(mba.mba_ipmi));
@@ -206,7 +142,7 @@ mainbus_attach(device_t parent, device_t
 
 #if NHYPERVISOR > 0
 	mba.mba_haa.haa_busname = "hypervisor";
-	config_found_ia(self, "hypervisorbus", &mba.mba_haa, mainbus_print);
+	config_found_ia(self, "hypervisorbus", &mba.mba_haa, xen_mainbus_print);
 #endif
 
 	/* save/restore for Xen */
@@ -216,9 +152,9 @@ mainbus_attach(device_t parent, device_t
 }
 
 int
-mainbus_print(void *aux, const char *pnp)
+xen_mainbus_print(void *aux, const char *pnp)
 {
-	union mainbus_attach_args *mba = aux;
+	union xen_mainbus_attach_args *mba = aux;
 
 	if (pnp)
 		aprint_normal("%s at %s", mba->mba_busname, pnp);

Added files:

Index: src/sys/arch/x86/x86/mainbus.c
diff -u /dev/null src/sys/arch/x86/x86/mainbus.c:1.1
--- /dev/null	Sat Dec 22 07:45:59 2018
+++ src/sys/arch/x86/x86/mainbus.c	Sat Dec 22 07:45:59 2018
@@ -0,0 +1,255 @@
+/* $NetBSD: mainbus.c,v 1.1 2018/12/22 07:45:59 cherry Exp $ */
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+
+__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.1 2018/12/22 07:45:59 cherry Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/reboot.h>
+
+#include <dev/pci/pcivar.h>
+
+#include <machine/cpuvar.h>
+#include <machine/mpbiosvar.h>
+#include <machine/mpacpi.h>
+
+#include "pci.h"
+#include "isa.h"
+#include "isadma.h"
+#include "acpica.h"
+#include "ipmi.h"
+
+#include "opt_acpi.h"
+#include "opt_mpbios.h"
+#include "opt_pcifixup.h"
+
+#if NACPICA > 0
+#include <dev/acpi/acpivar.h>
+#endif
+
+#include <x86/autoconf.h>
+
+#if NIPMI > 0
+#include <x86/ipmivar.h>
+#endif
+
+#if NPCI > 0
+#if defined(PCI_BUS_FIXUP)
+#include <arch/x86/pci/pci_bus_fixup.h>
+#if defined(PCI_ADDR_FIXUP)
+#include <arch/x86/pci/pci_addr_fixup.h>
+#endif
+#endif
+#ifdef __HAVE_PCI_MSI_MSIX
+#include <arch/x86/pci/msipic.h>
+#endif /* __HAVE_PCI_MSI_MSIX */
+#endif
+
+bool acpi_present = false;
+bool mpacpi_active = false;
+
+int	mainbus_rescan(device_t, const char *, const int *);
+void	mainbus_childdetached(device_t, device_t);
+void	mainbus_attach(device_t, device_t, void *);
+int	mainbus_match(device_t, cfdata_t, void *);
+
+CFATTACH_DECL2_NEW(mainbus, sizeof(struct mainbus_softc),
+    mainbus_match, mainbus_attach,
+    NULL, NULL,
+    mainbus_rescan, mainbus_childdetached);
+
+#if defined(__i386__) && !defined(XEN)
+void i386_mainbus_childdetached(device_t, device_t);
+int  i386_mainbus_rescan(device_t, const char *, const int *);
+void i386_mainbus_attach(device_t, device_t, void *);
+#endif
+
+#if defined(__x86_64__) && !defined(XEN)
+void
+amd64_mainbus_attach(device_t, device_t, void *);
+#endif
+
+#if defined(XEN)
+void xen_mainbus_attach(device_t, device_t, void *);
+#endif
+
+static int
+mainbus_cpu_print(void *aux, const char *busname)
+{
+	char *cpuname = aux;
+
+	if (busname)
+		aprint_normal("%s at %s", cpuname, busname);
+	return (UNCONF);
+}
+
+/*
+ * On x86, cpus can be enumerated and attached to mainbus in mainly
+ * two ways depending on the platform (configuration):
+ * via MP BIOS tables, and via ACPI tables. 
+ *
+ * Since cpus are not an optional part of computers, this attachment
+ * is made common across all x86 architectures and modes, and thus
+ * hard coded into the boot path, with the exception of XEN PV domU.
+ *
+ * Along with cpus, apics come in various shapes and forms, and to
+ * accommodate for the configurable ioapic topology, the "ioapicbus"
+ * is also enumerated here as part of the mpbios/mpacpi probe path.
+ *
+ * All other busses are attached variously depending on the
+ * platform architecture and config(5)
+ *
+ * These configurations and attach orderings for various platforms are
+ * currently respectively driven in the functions:
+ *
+ * i386_mainbus_attach();
+ * amd64_mainbus_attach();
+ * xen_mainbus_attach();
+ *
+ * This arrangement gives us the flexibility to do things such as
+ * dynamic attach path traversal at boot time, depending on the
+ * "mode" of operation, ie; virtualition aware or native.
+ *
+ * For (a contrived) eg: XEN PVHVM would allow us to attach pci(9)
+ * either via hypervisorbus or mainbus depending on if the kernel is
+ * running under the hypervisor or not.
+ */
+
+static void
+x86_cpubus_attach(device_t self)
+{
+	int numcpus = 0;
+
+#if NPCI > 0
+
+#ifdef __HAVE_PCI_MSI_MSIX
+	msipic_init();
+#endif
+
+	/*
+	 * ACPI needs to be able to access PCI configuration space.
+	 */
+	pci_mode_detect();
+#if defined(PCI_BUS_FIXUP)
+	int pci_maxbus = 0;
+
+	if (pci_mode_detect() != 0) {
+		pci_maxbus = pci_bus_fixup(NULL, 0);
+		aprint_debug("PCI bus max, after pci_bus_fixup: %i\n",
+		    pci_maxbus);
+#if defined(PCI_ADDR_FIXUP)
+		pciaddr.extent_port = NULL;
+		pciaddr.extent_mem = NULL;
+		pci_addr_fixup(NULL, pci_maxbus);
+#endif
+	}
+#endif
+#endif /* NPCI */
+
+#if NACPICA > 0
+	if ((boothowto & RB_MD2) == 0 && acpi_check(self, "acpibus"))
+		acpi_present = acpi_probe() != 0;
+	/*
+	 * First, see if the MADT contains CPUs, and possibly I/O APICs.
+	 * Building the interrupt routing structures can only
+	 * be done later (via a callback).
+	 */
+	if (acpi_present)
+		mpacpi_active = mpacpi_scan_apics(self, &numcpus) != 0;
+
+	if (!mpacpi_active) {
+#endif
+#ifdef MPBIOS
+		if (mpbios_probe(self))
+			mpbios_scan(self, &numcpus);
+		else
+#endif
+		if (numcpus == 0) {
+			struct cpu_attach_args caa;
+                        
+			memset(&caa, 0, sizeof(caa));
+			caa.cpu_number = 0;
+			caa.cpu_role = CPU_ROLE_SP;
+			caa.cpu_func = 0;
+                        
+			config_found_ia(self, "cpubus", &caa, mainbus_cpu_print);
+		}
+#if NACPICA > 0		
+	}
+#endif
+
+}
+
+int
+mainbus_match(device_t parent, cfdata_t match, void *aux)
+{
+
+	return 1;
+}
+
+void
+mainbus_attach(device_t parent, device_t self, void *aux)
+{
+
+	aprint_naive("\n");
+	aprint_normal("\n");
+
+#if defined(XEN)
+	if (xendomain_is_dom0()) {
+#endif
+		x86_cpubus_attach(self);
+
+#if defined(__i386__) && !defined(XEN)
+		i386_mainbus_attach(parent, self, aux);
+#elif defined(__x86_64__) && !defined(XEN)
+		amd64_mainbus_attach(parent, self, aux);
+#endif
+#if defined(XEN)
+	}
+	xen_mainbus_attach(parent, self, aux);
+#endif
+}
+
+int	mainbus_rescan(device_t self, const char *ifattr, const int *locators)
+{
+#if defined(__i386__) && !defined(XEN)
+	return i386_mainbus_rescan(self, ifattr, locators);
+#endif
+	return ENOTTY; /* Inappropriate ioctl for this device */
+}
+
+void	mainbus_childdetached(device_t self, device_t child)
+{
+#if defined(__i386__) && !defined(XEN)
+	i386_mainbus_childdetached(self, child);
+#endif
+}
+

Reply via email to