xref: /freebsd/sys/powerpc/ofw/ofw_pcibus.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
151d163d3SNathan Whitehorn /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
451d163d3SNathan Whitehorn  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
551d163d3SNathan Whitehorn  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
651d163d3SNathan Whitehorn  * Copyright (c) 2000, BSDi
751d163d3SNathan Whitehorn  * Copyright (c) 2003, Thomas Moestl <tmm@FreeBSD.org>
851d163d3SNathan Whitehorn  * All rights reserved.
951d163d3SNathan Whitehorn  *
1051d163d3SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
1151d163d3SNathan Whitehorn  * modification, are permitted provided that the following conditions
1251d163d3SNathan Whitehorn  * are met:
1351d163d3SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
1451d163d3SNathan Whitehorn  *    notice unmodified, this list of conditions, and the following
1551d163d3SNathan Whitehorn  *    disclaimer.
1651d163d3SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
1751d163d3SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
1851d163d3SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
1951d163d3SNathan Whitehorn  *
2051d163d3SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2151d163d3SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2251d163d3SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2351d163d3SNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2451d163d3SNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2551d163d3SNathan Whitehorn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2651d163d3SNathan Whitehorn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2751d163d3SNathan Whitehorn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2851d163d3SNathan Whitehorn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2951d163d3SNathan Whitehorn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3051d163d3SNathan Whitehorn  */
3151d163d3SNathan Whitehorn 
3251d163d3SNathan Whitehorn #include <sys/param.h>
3351d163d3SNathan Whitehorn #include <sys/bus.h>
3451d163d3SNathan Whitehorn #include <sys/kernel.h>
3551d163d3SNathan Whitehorn #include <sys/libkern.h>
3651d163d3SNathan Whitehorn #include <sys/module.h>
3751d163d3SNathan Whitehorn #include <sys/pciio.h>
38c90902d3SWarner Losh #include <sys/sbuf.h>
3949d9a597SJustin Hibbits #include <sys/smp.h>
4051d163d3SNathan Whitehorn 
4151d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
4251d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
4351d163d3SNathan Whitehorn #include <dev/ofw/ofw_pci.h>
4451d163d3SNathan Whitehorn #include <dev/ofw/openfirm.h>
4551d163d3SNathan Whitehorn 
4651d163d3SNathan Whitehorn #include <machine/bus.h>
474dea0435SNathan Whitehorn #include <machine/intr_machdep.h>
4851d163d3SNathan Whitehorn #include <machine/resource.h>
4951d163d3SNathan Whitehorn 
5051d163d3SNathan Whitehorn #include <dev/pci/pcireg.h>
5151d163d3SNathan Whitehorn #include <dev/pci/pcivar.h>
5251d163d3SNathan Whitehorn #include <dev/pci/pci_private.h>
5351d163d3SNathan Whitehorn 
54817ba5c0SNathan Whitehorn #include "ofw_pcibus.h"
5551d163d3SNathan Whitehorn #include "pcib_if.h"
5651d163d3SNathan Whitehorn #include "pci_if.h"
5751d163d3SNathan Whitehorn 
5894b4a038SNathan Whitehorn typedef uint32_t ofw_pci_intr_t;
5951d163d3SNathan Whitehorn 
6051d163d3SNathan Whitehorn /* Methods */
6151d163d3SNathan Whitehorn static device_probe_t ofw_pcibus_probe;
6251d163d3SNathan Whitehorn static device_attach_t ofw_pcibus_attach;
636cd99ae8SJohn Baldwin static pci_alloc_devinfo_t ofw_pcibus_alloc_devinfo;
6451d163d3SNathan Whitehorn static pci_assign_interrupt_t ofw_pcibus_assign_interrupt;
6551d163d3SNathan Whitehorn static ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo;
66496dfa89SJohn Baldwin static bus_child_deleted_t ofw_pcibus_child_deleted;
67ddfc9c4cSWarner Losh static int ofw_pcibus_child_pnpinfo_method(device_t cbdev, device_t child,
68ddfc9c4cSWarner Losh     struct sbuf *sb);
6994b4a038SNathan Whitehorn 
7094b4a038SNathan Whitehorn static void ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno);
7194b4a038SNathan Whitehorn static void ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno);
7251d163d3SNathan Whitehorn 
7351d163d3SNathan Whitehorn static device_method_t ofw_pcibus_methods[] = {
7451d163d3SNathan Whitehorn 	/* Device interface */
7551d163d3SNathan Whitehorn 	DEVMETHOD(device_probe,		ofw_pcibus_probe),
7651d163d3SNathan Whitehorn 	DEVMETHOD(device_attach,	ofw_pcibus_attach),
7751d163d3SNathan Whitehorn 
7894b4a038SNathan Whitehorn 	/* Bus interface */
79496dfa89SJohn Baldwin 	DEVMETHOD(bus_child_deleted,	ofw_pcibus_child_deleted),
80ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_pnpinfo,	ofw_pcibus_child_pnpinfo_method),
8129afffb9SMitchell Horne 	DEVMETHOD(bus_rescan,		bus_null_rescan),
8249d9a597SJustin Hibbits 	DEVMETHOD(bus_get_cpus,		ofw_pcibus_get_cpus),
8349d9a597SJustin Hibbits 	DEVMETHOD(bus_get_domain,	ofw_pcibus_get_domain),
8494b4a038SNathan Whitehorn 
8551d163d3SNathan Whitehorn 	/* PCI interface */
866cd99ae8SJohn Baldwin 	DEVMETHOD(pci_alloc_devinfo,	ofw_pcibus_alloc_devinfo),
8751d163d3SNathan Whitehorn 	DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
8851d163d3SNathan Whitehorn 
8951d163d3SNathan Whitehorn 	/* ofw_bus interface */
9051d163d3SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_pcibus_get_devinfo),
9151d163d3SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
9251d163d3SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
9351d163d3SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
9451d163d3SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
9551d163d3SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
9651d163d3SNathan Whitehorn 
97817ba5c0SNathan Whitehorn 	DEVMETHOD_END
9851d163d3SNathan Whitehorn };
9951d163d3SNathan Whitehorn 
1001b1596a3SJohn Baldwin DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods,
1011b1596a3SJohn Baldwin     sizeof(struct pci_softc), pci_driver);
10203f6459cSJohn Baldwin EARLY_DRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, 0, 0, BUS_PASS_BUS);
10351d163d3SNathan Whitehorn MODULE_VERSION(ofw_pcibus, 1);
10451d163d3SNathan Whitehorn MODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1);
10551d163d3SNathan Whitehorn 
1061c5fc51cSNathan Whitehorn static int ofw_devices_only = 0;
1071c5fc51cSNathan Whitehorn TUNABLE_INT("hw.pci.ofw_devices_only", &ofw_devices_only);
1081c5fc51cSNathan Whitehorn 
10951d163d3SNathan Whitehorn static int
ofw_pcibus_probe(device_t dev)11051d163d3SNathan Whitehorn ofw_pcibus_probe(device_t dev)
11151d163d3SNathan Whitehorn {
11294b4a038SNathan Whitehorn 
1130d8d9edaSNathan Whitehorn 	if (ofw_bus_get_node(dev) == -1)
11451d163d3SNathan Whitehorn 		return (ENXIO);
11551d163d3SNathan Whitehorn 	device_set_desc(dev, "OFW PCI bus");
11651d163d3SNathan Whitehorn 
1171c5fc51cSNathan Whitehorn 	return (BUS_PROBE_DEFAULT);
11851d163d3SNathan Whitehorn }
11951d163d3SNathan Whitehorn 
12051d163d3SNathan Whitehorn static int
ofw_pcibus_attach(device_t dev)12151d163d3SNathan Whitehorn ofw_pcibus_attach(device_t dev)
12251d163d3SNathan Whitehorn {
12394b4a038SNathan Whitehorn 	u_int busno, domain;
1241b1596a3SJohn Baldwin 	int error;
12551d163d3SNathan Whitehorn 
1261b1596a3SJohn Baldwin 	error = pci_attach_common(dev);
1271b1596a3SJohn Baldwin 	if (error)
1281b1596a3SJohn Baldwin 		return (error);
12951d163d3SNathan Whitehorn 	domain = pcib_get_domain(dev);
13051d163d3SNathan Whitehorn 	busno = pcib_get_bus(dev);
13194b4a038SNathan Whitehorn 
13294b4a038SNathan Whitehorn 	/*
13394b4a038SNathan Whitehorn 	 * Attach those children represented in the device tree.
13494b4a038SNathan Whitehorn 	 */
13594b4a038SNathan Whitehorn 
13694b4a038SNathan Whitehorn 	ofw_pcibus_enum_devtree(dev, domain, busno);
13794b4a038SNathan Whitehorn 
13894b4a038SNathan Whitehorn 	/*
13994b4a038SNathan Whitehorn 	 * We now attach any laggard devices. FDT, for instance, allows
14094b4a038SNathan Whitehorn 	 * the device tree to enumerate only some PCI devices. Apple's
14194b4a038SNathan Whitehorn 	 * OF device tree on some Grackle-based hardware can also miss
14294b4a038SNathan Whitehorn 	 * functions on multi-function cards.
14394b4a038SNathan Whitehorn 	 */
14494b4a038SNathan Whitehorn 
1451c5fc51cSNathan Whitehorn 	if (!ofw_devices_only)
14694b4a038SNathan Whitehorn 		ofw_pcibus_enum_bus(dev, domain, busno);
14794b4a038SNathan Whitehorn 
14894b4a038SNathan Whitehorn 	return (bus_generic_attach(dev));
14994b4a038SNathan Whitehorn }
15094b4a038SNathan Whitehorn 
1516cd99ae8SJohn Baldwin struct pci_devinfo *
ofw_pcibus_alloc_devinfo(device_t dev)1526cd99ae8SJohn Baldwin ofw_pcibus_alloc_devinfo(device_t dev)
1536cd99ae8SJohn Baldwin {
1546cd99ae8SJohn Baldwin 	struct ofw_pcibus_devinfo *dinfo;
1556cd99ae8SJohn Baldwin 
1566cd99ae8SJohn Baldwin 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
1576cd99ae8SJohn Baldwin 	return (&dinfo->opd_dinfo);
1586cd99ae8SJohn Baldwin }
1596cd99ae8SJohn Baldwin 
16094b4a038SNathan Whitehorn static void
ofw_pcibus_enum_devtree(device_t dev,u_int domain,u_int busno)16194b4a038SNathan Whitehorn ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno)
16294b4a038SNathan Whitehorn {
16394b4a038SNathan Whitehorn 	device_t pcib;
16494b4a038SNathan Whitehorn 	struct ofw_pci_register pcir;
16594b4a038SNathan Whitehorn 	struct ofw_pcibus_devinfo *dinfo;
16694b4a038SNathan Whitehorn 	phandle_t node, child;
16794b4a038SNathan Whitehorn 	u_int func, slot;
16894b4a038SNathan Whitehorn 	int intline;
16994b4a038SNathan Whitehorn 
17094b4a038SNathan Whitehorn 	pcib = device_get_parent(dev);
17151d163d3SNathan Whitehorn 	node = ofw_bus_get_node(dev);
17251d163d3SNathan Whitehorn 
17351d163d3SNathan Whitehorn 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
174509142e1SNathan Whitehorn 		if (OF_getencprop(child, "reg", (pcell_t *)&pcir,
175509142e1SNathan Whitehorn 		    sizeof(pcir)) == -1)
17651d163d3SNathan Whitehorn 			continue;
17751d163d3SNathan Whitehorn 		slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
17851d163d3SNathan Whitehorn 		func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
17951d163d3SNathan Whitehorn 
18051d163d3SNathan Whitehorn 		/* Some OFW device trees contain dupes. */
18151d163d3SNathan Whitehorn 		if (pci_find_dbsf(domain, busno, slot, func) != NULL)
18251d163d3SNathan Whitehorn 			continue;
18351d163d3SNathan Whitehorn 
18494b4a038SNathan Whitehorn 		/*
18594b4a038SNathan Whitehorn 		 * The preset in the intline register is usually bogus.  Reset
18694b4a038SNathan Whitehorn 		 * it such that the PCI code will reroute the interrupt if
18794b4a038SNathan Whitehorn 		 * needed.
18894b4a038SNathan Whitehorn 		 */
18994b4a038SNathan Whitehorn 
19094b4a038SNathan Whitehorn 		intline = PCI_INVALID_IRQ;
19194b4a038SNathan Whitehorn 		if (OF_getproplen(child, "interrupts") > 0)
19294b4a038SNathan Whitehorn 			intline = 0;
19394b4a038SNathan Whitehorn 		PCIB_WRITE_CONFIG(pcib, busno, slot, func, PCIR_INTLINE,
19494b4a038SNathan Whitehorn 		    intline, 1);
19594b4a038SNathan Whitehorn 
19694b4a038SNathan Whitehorn 		/*
19794b4a038SNathan Whitehorn 		 * Now set up the PCI and OFW bus layer devinfo and add it
19894b4a038SNathan Whitehorn 		 * to the PCI bus.
19994b4a038SNathan Whitehorn 		 */
20051d163d3SNathan Whitehorn 
2016cd99ae8SJohn Baldwin 		dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib, dev,
2026cd99ae8SJohn Baldwin 		    domain, busno, slot, func);
20351d163d3SNathan Whitehorn 		if (dinfo == NULL)
20451d163d3SNathan Whitehorn 			continue;
20551d163d3SNathan Whitehorn 		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
20651d163d3SNathan Whitehorn 		    0) {
20751d163d3SNathan Whitehorn 			pci_freecfg((struct pci_devinfo *)dinfo);
20851d163d3SNathan Whitehorn 			continue;
20951d163d3SNathan Whitehorn 		}
210817ba5c0SNathan Whitehorn 		dinfo->opd_dma_tag = NULL;
21151d163d3SNathan Whitehorn 		pci_add_child(dev, (struct pci_devinfo *)dinfo);
21251d163d3SNathan Whitehorn 
21351d163d3SNathan Whitehorn 		/*
21451d163d3SNathan Whitehorn 		 * Some devices don't have an intpin set, but do have
21594b4a038SNathan Whitehorn 		 * interrupts. These are fully specified, and set in the
21694b4a038SNathan Whitehorn 		 * interrupts property, so add that value to the device's
21794b4a038SNathan Whitehorn 		 * resource list.
21851d163d3SNathan Whitehorn 		 */
219c47d4cdeSIan Lepore 		if (dinfo->opd_dinfo.cfg.intpin == 0)
220a8c5ea04SRuslan Bukin 			ofw_bus_intr_to_rl(dev, child,
221a8c5ea04SRuslan Bukin 				&dinfo->opd_dinfo.resources, NULL);
22251d163d3SNathan Whitehorn 	}
22351d163d3SNathan Whitehorn }
22451d163d3SNathan Whitehorn 
22594b4a038SNathan Whitehorn /*
22694b4a038SNathan Whitehorn  * The following is an almost exact clone of pci_add_children(), with the
22794b4a038SNathan Whitehorn  * addition that it (a) will not add children that have already been added,
22894b4a038SNathan Whitehorn  * and (b) will set up the OFW devinfo to point to invalid values. This is
22994b4a038SNathan Whitehorn  * to handle non-enumerated PCI children as exist in FDT and on the second
23094b4a038SNathan Whitehorn  * function of the Rage 128 in my Blue & White G3.
23194b4a038SNathan Whitehorn  */
23294b4a038SNathan Whitehorn 
23394b4a038SNathan Whitehorn static void
ofw_pcibus_enum_bus(device_t dev,u_int domain,u_int busno)23494b4a038SNathan Whitehorn ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno)
23594b4a038SNathan Whitehorn {
23694b4a038SNathan Whitehorn 	device_t pcib;
23794b4a038SNathan Whitehorn 	struct ofw_pcibus_devinfo *dinfo;
23894b4a038SNathan Whitehorn 	int maxslots;
23994b4a038SNathan Whitehorn 	int s, f, pcifunchigh;
24094b4a038SNathan Whitehorn 	uint8_t hdrtype;
24194b4a038SNathan Whitehorn 
24294b4a038SNathan Whitehorn 	pcib = device_get_parent(dev);
24394b4a038SNathan Whitehorn 
24494b4a038SNathan Whitehorn 	maxslots = PCIB_MAXSLOTS(pcib);
24594b4a038SNathan Whitehorn 	for (s = 0; s <= maxslots; s++) {
24694b4a038SNathan Whitehorn 		pcifunchigh = 0;
24794b4a038SNathan Whitehorn 		f = 0;
24894b4a038SNathan Whitehorn 		DELAY(1);
24994b4a038SNathan Whitehorn 		hdrtype = PCIB_READ_CONFIG(pcib, busno, s, f, PCIR_HDRTYPE, 1);
25094b4a038SNathan Whitehorn 		if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
25194b4a038SNathan Whitehorn 			continue;
25294b4a038SNathan Whitehorn 		if (hdrtype & PCIM_MFDEV)
25394b4a038SNathan Whitehorn 			pcifunchigh = PCI_FUNCMAX;
25494b4a038SNathan Whitehorn 		for (f = 0; f <= pcifunchigh; f++) {
25594b4a038SNathan Whitehorn 			/* Filter devices we have already added */
25694b4a038SNathan Whitehorn 			if (pci_find_dbsf(domain, busno, s, f) != NULL)
25794b4a038SNathan Whitehorn 				continue;
25894b4a038SNathan Whitehorn 
25994b4a038SNathan Whitehorn 			dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(
2606cd99ae8SJohn Baldwin 			    pcib, dev, domain, busno, s, f);
261a7a32004SNathan Whitehorn 			if (dinfo == NULL)
262a7a32004SNathan Whitehorn 				continue;
263a7a32004SNathan Whitehorn 
264817ba5c0SNathan Whitehorn 			dinfo->opd_dma_tag = NULL;
26594b4a038SNathan Whitehorn 			dinfo->opd_obdinfo.obd_node = -1;
26694b4a038SNathan Whitehorn 
26794b4a038SNathan Whitehorn 			dinfo->opd_obdinfo.obd_name = NULL;
26894b4a038SNathan Whitehorn 			dinfo->opd_obdinfo.obd_compat = NULL;
26994b4a038SNathan Whitehorn 			dinfo->opd_obdinfo.obd_type = NULL;
27094b4a038SNathan Whitehorn 			dinfo->opd_obdinfo.obd_model = NULL;
271a7a32004SNathan Whitehorn 
272a7a32004SNathan Whitehorn 			/*
273a7a32004SNathan Whitehorn 			 * For non OFW-devices, don't believe 0
274a7a32004SNathan Whitehorn 			 * for an interrupt.
275a7a32004SNathan Whitehorn 			 */
276a7a32004SNathan Whitehorn 			if (dinfo->opd_dinfo.cfg.intline == 0) {
277a7a32004SNathan Whitehorn 				dinfo->opd_dinfo.cfg.intline = PCI_INVALID_IRQ;
278a7a32004SNathan Whitehorn 				PCIB_WRITE_CONFIG(pcib, busno, s, f,
279a7a32004SNathan Whitehorn 				    PCIR_INTLINE, PCI_INVALID_IRQ, 1);
28094b4a038SNathan Whitehorn 			}
281a7a32004SNathan Whitehorn 
282a7a32004SNathan Whitehorn 			pci_add_child(dev, (struct pci_devinfo *)dinfo);
28394b4a038SNathan Whitehorn 		}
28494b4a038SNathan Whitehorn 	}
28594b4a038SNathan Whitehorn }
28694b4a038SNathan Whitehorn 
287496dfa89SJohn Baldwin static void
ofw_pcibus_child_deleted(device_t dev,device_t child)288496dfa89SJohn Baldwin ofw_pcibus_child_deleted(device_t dev, device_t child)
289496dfa89SJohn Baldwin {
290496dfa89SJohn Baldwin 	struct ofw_pcibus_devinfo *dinfo;
291496dfa89SJohn Baldwin 
2924d210a60SNathan Whitehorn 	dinfo = device_get_ivars(child);
293496dfa89SJohn Baldwin 	ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
294496dfa89SJohn Baldwin 	pci_child_deleted(dev, child);
295496dfa89SJohn Baldwin }
296496dfa89SJohn Baldwin 
29794b4a038SNathan Whitehorn static int
ofw_pcibus_child_pnpinfo_method(device_t cbdev,device_t child,struct sbuf * sb)298ddfc9c4cSWarner Losh ofw_pcibus_child_pnpinfo_method(device_t cbdev, device_t child, struct sbuf *sb)
29994b4a038SNathan Whitehorn {
300ddfc9c4cSWarner Losh 	pci_child_pnpinfo_method(cbdev, child, sb);
30194b4a038SNathan Whitehorn 
30294b4a038SNathan Whitehorn 	if (ofw_bus_get_node(child) != -1)  {
303ddfc9c4cSWarner Losh 		sbuf_cat(sb, " "); /* Separate info */
304ddfc9c4cSWarner Losh 		ofw_bus_gen_child_pnpinfo(cbdev, child, sb);
30594b4a038SNathan Whitehorn 	}
30694b4a038SNathan Whitehorn 
30794b4a038SNathan Whitehorn 	return (0);
30851d163d3SNathan Whitehorn }
30951d163d3SNathan Whitehorn 
31051d163d3SNathan Whitehorn static int
ofw_pcibus_assign_interrupt(device_t dev,device_t child)31151d163d3SNathan Whitehorn ofw_pcibus_assign_interrupt(device_t dev, device_t child)
31251d163d3SNathan Whitehorn {
313bbc6da03SNathan Whitehorn 	ofw_pci_intr_t intr[2];
314eaef5f0aSNathan Whitehorn 	phandle_t node, iparent;
315bbc6da03SNathan Whitehorn 	int isz, icells;
31651d163d3SNathan Whitehorn 
31794b4a038SNathan Whitehorn 	node = ofw_bus_get_node(child);
31851d163d3SNathan Whitehorn 
31994b4a038SNathan Whitehorn 	if (node == -1) {
32094b4a038SNathan Whitehorn 		/* Non-firmware enumerated child, use standard routing */
32151d163d3SNathan Whitehorn 
322bbc6da03SNathan Whitehorn 		intr[0] = pci_get_intpin(child);
32394b4a038SNathan Whitehorn 		return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
324bbc6da03SNathan Whitehorn 		    intr[0]));
32594b4a038SNathan Whitehorn 	}
32694b4a038SNathan Whitehorn 
32794b4a038SNathan Whitehorn 	/*
328eaef5f0aSNathan Whitehorn 	 * Try to determine the node's interrupt parent so we know which
329eaef5f0aSNathan Whitehorn 	 * PIC to use.
330eaef5f0aSNathan Whitehorn 	 */
331eaef5f0aSNathan Whitehorn 
332eaef5f0aSNathan Whitehorn 	iparent = -1;
333509142e1SNathan Whitehorn 	if (OF_getencprop(node, "interrupt-parent", &iparent,
334509142e1SNathan Whitehorn 	    sizeof(iparent)) < 0)
335eaef5f0aSNathan Whitehorn 		iparent = -1;
336bbc6da03SNathan Whitehorn 	icells = 1;
337bbc6da03SNathan Whitehorn 	if (iparent != -1)
338509142e1SNathan Whitehorn 		OF_getencprop(OF_node_from_xref(iparent), "#interrupt-cells",
339bbc6da03SNathan Whitehorn 		    &icells, sizeof(icells));
340eaef5f0aSNathan Whitehorn 
341eaef5f0aSNathan Whitehorn 	/*
34294b4a038SNathan Whitehorn 	 * Any AAPL,interrupts property gets priority and is
34394b4a038SNathan Whitehorn 	 * fully specified (i.e. does not need routing)
34494b4a038SNathan Whitehorn 	 */
34594b4a038SNathan Whitehorn 
346509142e1SNathan Whitehorn 	isz = OF_getencprop(node, "AAPL,interrupts", intr, sizeof(intr));
347bbc6da03SNathan Whitehorn 	if (isz == sizeof(intr[0])*icells)
348bbc6da03SNathan Whitehorn 		return ((iparent == -1) ? intr[0] : ofw_bus_map_intr(dev,
349bbc6da03SNathan Whitehorn 		    iparent, icells, intr));
35094b4a038SNathan Whitehorn 
351509142e1SNathan Whitehorn 	isz = OF_getencprop(node, "interrupts", intr, sizeof(intr));
352bbc6da03SNathan Whitehorn 	if (isz == sizeof(intr[0])*icells) {
353eaef5f0aSNathan Whitehorn 		if (iparent != -1)
354bbc6da03SNathan Whitehorn 			intr[0] = ofw_bus_map_intr(dev, iparent, icells, intr);
355eaef5f0aSNathan Whitehorn 	} else {
356eaef5f0aSNathan Whitehorn 		/* No property: our best guess is the intpin. */
357bbc6da03SNathan Whitehorn 		intr[0] = pci_get_intpin(child);
35894b4a038SNathan Whitehorn 	}
35994b4a038SNathan Whitehorn 
36094b4a038SNathan Whitehorn 	/*
36194b4a038SNathan Whitehorn 	 * If we got intr from a property, it may or may not be an intpin.
36294b4a038SNathan Whitehorn 	 * For on-board devices, it frequently is not, and is completely out
36394b4a038SNathan Whitehorn 	 * of the valid intpin range.  For PCI slots, it hopefully is,
36494b4a038SNathan Whitehorn 	 * otherwise we will have trouble interfacing with non-OFW buses
36594b4a038SNathan Whitehorn 	 * such as cardbus.
36694b4a038SNathan Whitehorn 	 * Since we cannot tell which it is without violating layering, we
36794b4a038SNathan Whitehorn 	 * will always use the route_interrupt method, and treat exceptions
36894b4a038SNathan Whitehorn 	 * on the level they become apparent.
36994b4a038SNathan Whitehorn 	 */
370bbc6da03SNathan Whitehorn 	return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr[0]));
37151d163d3SNathan Whitehorn }
37251d163d3SNathan Whitehorn 
37351d163d3SNathan Whitehorn static const struct ofw_bus_devinfo *
ofw_pcibus_get_devinfo(device_t bus,device_t dev)37451d163d3SNathan Whitehorn ofw_pcibus_get_devinfo(device_t bus, device_t dev)
37551d163d3SNathan Whitehorn {
37651d163d3SNathan Whitehorn 	struct ofw_pcibus_devinfo *dinfo;
37751d163d3SNathan Whitehorn 
37851d163d3SNathan Whitehorn 	dinfo = device_get_ivars(dev);
37951d163d3SNathan Whitehorn 	return (&dinfo->opd_obdinfo);
38051d163d3SNathan Whitehorn }
38151d163d3SNathan Whitehorn 
38249d9a597SJustin Hibbits int
ofw_pcibus_get_cpus(device_t dev,device_t child,enum cpu_sets op,size_t setsize,cpuset_t * cpuset)38349d9a597SJustin Hibbits ofw_pcibus_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
38449d9a597SJustin Hibbits     cpuset_t *cpuset)
38549d9a597SJustin Hibbits {
38649d9a597SJustin Hibbits 	int d, error;
38749d9a597SJustin Hibbits 
388490ebb8fSJustin Hibbits 	d = platform_node_numa_domain(ofw_bus_get_node(dev));
38949d9a597SJustin Hibbits 
39049d9a597SJustin Hibbits 	switch (op) {
39149d9a597SJustin Hibbits 	case LOCAL_CPUS:
39249d9a597SJustin Hibbits 		if (setsize != sizeof(cpuset_t))
39349d9a597SJustin Hibbits 			return (EINVAL);
39449d9a597SJustin Hibbits 		*cpuset = cpuset_domain[d];
39549d9a597SJustin Hibbits 		return (0);
39649d9a597SJustin Hibbits 	case INTR_CPUS:
39749d9a597SJustin Hibbits 		error = bus_generic_get_cpus(dev, child, op, setsize, cpuset);
39849d9a597SJustin Hibbits 		if (error != 0)
39949d9a597SJustin Hibbits 			return (error);
40049d9a597SJustin Hibbits 		if (setsize != sizeof(cpuset_t))
40149d9a597SJustin Hibbits 			return (EINVAL);
402e2650af1SStefan Eßer 		CPU_AND(cpuset, cpuset, &cpuset_domain[d]);
40349d9a597SJustin Hibbits 		return (0);
40449d9a597SJustin Hibbits 	default:
40549d9a597SJustin Hibbits 		return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
40649d9a597SJustin Hibbits 	}
40749d9a597SJustin Hibbits 	return (0);
40849d9a597SJustin Hibbits }
40949d9a597SJustin Hibbits 
41049d9a597SJustin Hibbits /*
41149d9a597SJustin Hibbits  * Fetch the NUMA domain for the given device 'dev'.
41249d9a597SJustin Hibbits  *
41349d9a597SJustin Hibbits  * If a device has a _PXM method, map that to a NUMA domain.
41449d9a597SJustin Hibbits  * Otherwise, pass the request up to the parent.
41549d9a597SJustin Hibbits  * If there's no matching domain or the domain cannot be
41649d9a597SJustin Hibbits  * determined, return ENOENT.
41749d9a597SJustin Hibbits  */
41849d9a597SJustin Hibbits int
ofw_pcibus_get_domain(device_t dev,device_t child,int * domain)41949d9a597SJustin Hibbits ofw_pcibus_get_domain(device_t dev, device_t child, int *domain)
42049d9a597SJustin Hibbits {
421490ebb8fSJustin Hibbits 	*domain = platform_node_numa_domain(ofw_bus_get_node(child));
42249d9a597SJustin Hibbits 
42349d9a597SJustin Hibbits 	return (0);
42449d9a597SJustin Hibbits }
425