2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
6 * Copyright (c) 2000, BSDi
7 * Copyright (c) 2003, Thomas Moestl <tmm@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/libkern.h>
39 #include <sys/module.h>
40 #include <sys/pciio.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/ofw/ofw_pci.h>
46 #include <dev/ofw/openfirm.h>
48 #include <machine/bus.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/resource.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pci_private.h>
56 #include "ofw_pcibus.h"
60 typedef uint32_t ofw_pci_intr_t;
63 static device_probe_t ofw_pcibus_probe;
64 static device_attach_t ofw_pcibus_attach;
65 static pci_alloc_devinfo_t ofw_pcibus_alloc_devinfo;
66 static pci_assign_interrupt_t ofw_pcibus_assign_interrupt;
67 static ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo;
68 static bus_child_deleted_t ofw_pcibus_child_deleted;
69 static int ofw_pcibus_child_pnpinfo_str_method(device_t cbdev, device_t child,
70 char *buf, size_t buflen);
72 static void ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno);
73 static void ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno);
75 static device_method_t ofw_pcibus_methods[] = {
76 /* Device interface */
77 DEVMETHOD(device_probe, ofw_pcibus_probe),
78 DEVMETHOD(device_attach, ofw_pcibus_attach),
81 DEVMETHOD(bus_child_deleted, ofw_pcibus_child_deleted),
82 DEVMETHOD(bus_child_pnpinfo_str, ofw_pcibus_child_pnpinfo_str_method),
83 DEVMETHOD(bus_rescan, bus_null_rescan),
84 DEVMETHOD(bus_get_cpus, ofw_pcibus_get_cpus),
85 DEVMETHOD(bus_get_domain, ofw_pcibus_get_domain),
88 DEVMETHOD(pci_alloc_devinfo, ofw_pcibus_alloc_devinfo),
89 DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
91 /* ofw_bus interface */
92 DEVMETHOD(ofw_bus_get_devinfo, ofw_pcibus_get_devinfo),
93 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
94 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
95 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
96 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
97 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
102 static devclass_t pci_devclass;
104 DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods,
105 sizeof(struct pci_softc), pci_driver);
106 EARLY_DRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, pci_devclass, 0, 0,
108 MODULE_VERSION(ofw_pcibus, 1);
109 MODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1);
111 static int ofw_devices_only = 0;
112 TUNABLE_INT("hw.pci.ofw_devices_only", &ofw_devices_only);
115 ofw_pcibus_probe(device_t dev)
118 if (ofw_bus_get_node(dev) == -1)
120 device_set_desc(dev, "OFW PCI bus");
122 return (BUS_PROBE_DEFAULT);
126 ofw_pcibus_attach(device_t dev)
131 error = pci_attach_common(dev);
134 domain = pcib_get_domain(dev);
135 busno = pcib_get_bus(dev);
138 * Attach those children represented in the device tree.
141 ofw_pcibus_enum_devtree(dev, domain, busno);
144 * We now attach any laggard devices. FDT, for instance, allows
145 * the device tree to enumerate only some PCI devices. Apple's
146 * OF device tree on some Grackle-based hardware can also miss
147 * functions on multi-function cards.
150 if (!ofw_devices_only)
151 ofw_pcibus_enum_bus(dev, domain, busno);
153 return (bus_generic_attach(dev));
157 ofw_pcibus_alloc_devinfo(device_t dev)
159 struct ofw_pcibus_devinfo *dinfo;
161 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
162 return (&dinfo->opd_dinfo);
166 ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno)
169 struct ofw_pci_register pcir;
170 struct ofw_pcibus_devinfo *dinfo;
171 phandle_t node, child;
175 pcib = device_get_parent(dev);
176 node = ofw_bus_get_node(dev);
178 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
179 if (OF_getencprop(child, "reg", (pcell_t *)&pcir,
182 slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
183 func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
185 /* Some OFW device trees contain dupes. */
186 if (pci_find_dbsf(domain, busno, slot, func) != NULL)
190 * The preset in the intline register is usually bogus. Reset
191 * it such that the PCI code will reroute the interrupt if
195 intline = PCI_INVALID_IRQ;
196 if (OF_getproplen(child, "interrupts") > 0)
198 PCIB_WRITE_CONFIG(pcib, busno, slot, func, PCIR_INTLINE,
202 * Now set up the PCI and OFW bus layer devinfo and add it
206 dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib, dev,
207 domain, busno, slot, func);
210 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
212 pci_freecfg((struct pci_devinfo *)dinfo);
215 dinfo->opd_dma_tag = NULL;
216 pci_add_child(dev, (struct pci_devinfo *)dinfo);
219 * Some devices don't have an intpin set, but do have
220 * interrupts. These are fully specified, and set in the
221 * interrupts property, so add that value to the device's
224 if (dinfo->opd_dinfo.cfg.intpin == 0)
225 ofw_bus_intr_to_rl(dev, child,
226 &dinfo->opd_dinfo.resources, NULL);
231 * The following is an almost exact clone of pci_add_children(), with the
232 * addition that it (a) will not add children that have already been added,
233 * and (b) will set up the OFW devinfo to point to invalid values. This is
234 * to handle non-enumerated PCI children as exist in FDT and on the second
235 * function of the Rage 128 in my Blue & White G3.
239 ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno)
242 struct ofw_pcibus_devinfo *dinfo;
244 int s, f, pcifunchigh;
247 pcib = device_get_parent(dev);
249 maxslots = PCIB_MAXSLOTS(pcib);
250 for (s = 0; s <= maxslots; s++) {
254 hdrtype = PCIB_READ_CONFIG(pcib, busno, s, f, PCIR_HDRTYPE, 1);
255 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
257 if (hdrtype & PCIM_MFDEV)
258 pcifunchigh = PCI_FUNCMAX;
259 for (f = 0; f <= pcifunchigh; f++) {
260 /* Filter devices we have already added */
261 if (pci_find_dbsf(domain, busno, s, f) != NULL)
264 dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(
265 pcib, dev, domain, busno, s, f);
269 dinfo->opd_dma_tag = NULL;
270 dinfo->opd_obdinfo.obd_node = -1;
272 dinfo->opd_obdinfo.obd_name = NULL;
273 dinfo->opd_obdinfo.obd_compat = NULL;
274 dinfo->opd_obdinfo.obd_type = NULL;
275 dinfo->opd_obdinfo.obd_model = NULL;
278 * For non OFW-devices, don't believe 0
281 if (dinfo->opd_dinfo.cfg.intline == 0) {
282 dinfo->opd_dinfo.cfg.intline = PCI_INVALID_IRQ;
283 PCIB_WRITE_CONFIG(pcib, busno, s, f,
284 PCIR_INTLINE, PCI_INVALID_IRQ, 1);
287 pci_add_child(dev, (struct pci_devinfo *)dinfo);
293 ofw_pcibus_child_deleted(device_t dev, device_t child)
295 struct ofw_pcibus_devinfo *dinfo;
297 dinfo = device_get_ivars(dev);
298 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
299 pci_child_deleted(dev, child);
303 ofw_pcibus_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
306 pci_child_pnpinfo_str_method(cbdev, child, buf, buflen);
308 if (ofw_bus_get_node(child) != -1) {
309 strlcat(buf, " ", buflen); /* Separate info */
310 ofw_bus_gen_child_pnpinfo_str(cbdev, child, buf, buflen);
317 ofw_pcibus_assign_interrupt(device_t dev, device_t child)
319 ofw_pci_intr_t intr[2];
320 phandle_t node, iparent;
323 node = ofw_bus_get_node(child);
326 /* Non-firmware enumerated child, use standard routing */
328 intr[0] = pci_get_intpin(child);
329 return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
334 * Try to determine the node's interrupt parent so we know which
339 if (OF_getencprop(node, "interrupt-parent", &iparent,
340 sizeof(iparent)) < 0)
344 OF_getencprop(OF_node_from_xref(iparent), "#interrupt-cells",
345 &icells, sizeof(icells));
348 * Any AAPL,interrupts property gets priority and is
349 * fully specified (i.e. does not need routing)
352 isz = OF_getencprop(node, "AAPL,interrupts", intr, sizeof(intr));
353 if (isz == sizeof(intr[0])*icells)
354 return ((iparent == -1) ? intr[0] : ofw_bus_map_intr(dev,
355 iparent, icells, intr));
357 isz = OF_getencprop(node, "interrupts", intr, sizeof(intr));
358 if (isz == sizeof(intr[0])*icells) {
360 intr[0] = ofw_bus_map_intr(dev, iparent, icells, intr);
362 /* No property: our best guess is the intpin. */
363 intr[0] = pci_get_intpin(child);
367 * If we got intr from a property, it may or may not be an intpin.
368 * For on-board devices, it frequently is not, and is completely out
369 * of the valid intpin range. For PCI slots, it hopefully is,
370 * otherwise we will have trouble interfacing with non-OFW buses
372 * Since we cannot tell which it is without violating layering, we
373 * will always use the route_interrupt method, and treat exceptions
374 * on the level they become apparent.
376 return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr[0]));
379 static const struct ofw_bus_devinfo *
380 ofw_pcibus_get_devinfo(device_t bus, device_t dev)
382 struct ofw_pcibus_devinfo *dinfo;
384 dinfo = device_get_ivars(dev);
385 return (&dinfo->opd_obdinfo);
389 ofw_pcibus_parse_associativity(device_t dev, int *domain)
392 cell_t associativity[5];
395 if ((node = ofw_bus_get_node(dev)) == -1) {
397 device_printf(dev, "no ofw node found\n");
400 res = OF_getproplen(node, "ibm,associativity");
403 OF_getencprop(node, "ibm,associativity",
406 *domain = associativity[3] - 1;
408 device_printf(dev, "domain(%d)\n", *domain);
413 ofw_pcibus_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
418 error = ofw_pcibus_parse_associativity(child, &d);
420 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
424 if (setsize != sizeof(cpuset_t))
426 *cpuset = cpuset_domain[d];
429 error = bus_generic_get_cpus(dev, child, op, setsize, cpuset);
432 if (setsize != sizeof(cpuset_t))
434 CPU_AND(cpuset, &cpuset_domain[d]);
437 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
443 * Fetch the NUMA domain for the given device 'dev'.
445 * If a device has a _PXM method, map that to a NUMA domain.
446 * Otherwise, pass the request up to the parent.
447 * If there's no matching domain or the domain cannot be
448 * determined, return ENOENT.
451 ofw_pcibus_get_domain(device_t dev, device_t child, int *domain)
455 error = ofw_pcibus_parse_associativity(child, &d);
456 /* No ofw node; go up a level */
458 return (bus_generic_get_domain(dev, child, domain));