ofw_pcibus.c revision 186128
1/*-
2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
5 * Copyright (c) 2003, Thomas Moestl <tmm@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice unmodified, this list of conditions, and the following
13 *    disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/powerpc/ofw/ofw_pcibus.c 186128 2008-12-15 15:31:10Z nwhitehorn $");
32
33#include <sys/param.h>
34#include <sys/bus.h>
35#include <sys/kernel.h>
36#include <sys/libkern.h>
37#include <sys/module.h>
38#include <sys/pciio.h>
39
40#include <dev/ofw/ofw_bus.h>
41#include <dev/ofw/ofw_bus_subr.h>
42#include <dev/ofw/ofw_pci.h>
43#include <dev/ofw/openfirm.h>
44
45#include <machine/bus.h>
46#include <machine/resource.h>
47
48#include <dev/pci/pcireg.h>
49#include <dev/pci/pcivar.h>
50#include <dev/pci/pci_private.h>
51
52#include "pcib_if.h"
53#include "pci_if.h"
54
55typedef uint32_t ofw_pci_intr_t;
56
57/* Methods */
58static device_probe_t ofw_pcibus_probe;
59static device_attach_t ofw_pcibus_attach;
60static pci_assign_interrupt_t ofw_pcibus_assign_interrupt;
61static ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo;
62static int ofw_pcibus_child_pnpinfo_str_method(device_t cbdev, device_t child,
63    char *buf, size_t buflen);
64
65static void ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno);
66static void ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno);
67
68static device_method_t ofw_pcibus_methods[] = {
69	/* Device interface */
70	DEVMETHOD(device_probe,		ofw_pcibus_probe),
71	DEVMETHOD(device_attach,	ofw_pcibus_attach),
72
73	/* Bus interface */
74	DEVMETHOD(bus_child_pnpinfo_str, ofw_pcibus_child_pnpinfo_str_method),
75
76	/* PCI interface */
77	DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
78
79	/* ofw_bus interface */
80	DEVMETHOD(ofw_bus_get_devinfo,	ofw_pcibus_get_devinfo),
81	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
82	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
83	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
84	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
85	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
86
87	{ 0, 0 }
88};
89
90struct ofw_pcibus_devinfo {
91	struct pci_devinfo	opd_dinfo;
92	struct ofw_bus_devinfo	opd_obdinfo;
93};
94
95static devclass_t pci_devclass;
96
97DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods, 1 /* no softc */,
98    pci_driver);
99DRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, pci_devclass, 0, 0);
100MODULE_VERSION(ofw_pcibus, 1);
101MODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1);
102
103static int
104ofw_pcibus_probe(device_t dev)
105{
106
107	if (ofw_bus_get_node(dev) == 0)
108		return (ENXIO);
109	device_set_desc(dev, "OFW PCI bus");
110
111	return (0);
112}
113
114static int
115ofw_pcibus_attach(device_t dev)
116{
117	u_int busno, domain;
118
119	domain = pcib_get_domain(dev);
120	busno = pcib_get_bus(dev);
121	if (bootverbose)
122		device_printf(dev, "domain=%d, physical bus=%d\n",
123		    domain, busno);
124
125	/*
126	 * Attach those children represented in the device tree.
127	 */
128
129	ofw_pcibus_enum_devtree(dev, domain, busno);
130
131	/*
132	 * We now attach any laggard devices. FDT, for instance, allows
133	 * the device tree to enumerate only some PCI devices. Apple's
134	 * OF device tree on some Grackle-based hardware can also miss
135	 * functions on multi-function cards.
136	 */
137
138	ofw_pcibus_enum_bus(dev, domain, busno);
139
140	return (bus_generic_attach(dev));
141}
142
143static void
144ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno)
145{
146	device_t pcib;
147	struct ofw_pci_register pcir;
148	struct ofw_pcibus_devinfo *dinfo;
149	phandle_t node, child;
150	u_int func, slot;
151	int intline;
152
153	pcib = device_get_parent(dev);
154	node = ofw_bus_get_node(dev);
155
156	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
157		if (OF_getprop(child, "reg", &pcir, sizeof(pcir)) == -1)
158			continue;
159		slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
160		func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
161
162		/* Some OFW device trees contain dupes. */
163		if (pci_find_dbsf(domain, busno, slot, func) != NULL)
164			continue;
165
166		/*
167		 * The preset in the intline register is usually bogus.  Reset
168		 * it such that the PCI code will reroute the interrupt if
169		 * needed.
170		 */
171
172		intline = PCI_INVALID_IRQ;
173		if (OF_getproplen(child, "interrupts") > 0)
174			intline = 0;
175		PCIB_WRITE_CONFIG(pcib, busno, slot, func, PCIR_INTLINE,
176		    intline, 1);
177
178		/*
179		 * Now set up the PCI and OFW bus layer devinfo and add it
180		 * to the PCI bus.
181		 */
182
183		dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib,
184		    domain, busno, slot, func, sizeof(*dinfo));
185		if (dinfo == NULL)
186			continue;
187		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
188		    0) {
189			pci_freecfg((struct pci_devinfo *)dinfo);
190			continue;
191		}
192		pci_add_child(dev, (struct pci_devinfo *)dinfo);
193
194		/*
195                 * Some devices don't have an intpin set, but do have
196                 * interrupts. These are fully specified, and set in the
197		 * interrupts property, so add that value to the device's
198		 * resource list.
199                 */
200                if (dinfo->opd_dinfo.cfg.intpin == 0) {
201                        ofw_pci_intr_t intr;
202
203			if (OF_getprop(child, "interrupts", &intr,
204			    sizeof(intr)) > 0) {
205                                resource_list_add(&dinfo->opd_dinfo.resources,
206                                    SYS_RES_IRQ, 0, intr, intr, 1);
207			}
208                }
209	}
210}
211
212/*
213 * The following is an almost exact clone of pci_add_children(), with the
214 * addition that it (a) will not add children that have already been added,
215 * and (b) will set up the OFW devinfo to point to invalid values. This is
216 * to handle non-enumerated PCI children as exist in FDT and on the second
217 * function of the Rage 128 in my Blue & White G3.
218 */
219
220static void
221ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno)
222{
223	device_t pcib;
224	struct ofw_pcibus_devinfo *dinfo;
225	int maxslots;
226	int s, f, pcifunchigh;
227	uint8_t hdrtype;
228
229	pcib = device_get_parent(dev);
230
231	maxslots = PCIB_MAXSLOTS(pcib);
232	for (s = 0; s <= maxslots; s++) {
233		pcifunchigh = 0;
234		f = 0;
235		DELAY(1);
236		hdrtype = PCIB_READ_CONFIG(pcib, busno, s, f, PCIR_HDRTYPE, 1);
237		if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
238			continue;
239		if (hdrtype & PCIM_MFDEV)
240			pcifunchigh = PCI_FUNCMAX;
241		for (f = 0; f <= pcifunchigh; f++) {
242			/* Filter devices we have already added */
243			if (pci_find_dbsf(domain, busno, s, f) != NULL)
244				continue;
245
246			dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(
247			    pcib, domain, busno, s, f, sizeof(*dinfo));
248			if (dinfo != NULL) {
249				dinfo->opd_obdinfo.obd_node = -1;
250
251				dinfo->opd_obdinfo.obd_name = NULL;
252				dinfo->opd_obdinfo.obd_compat = NULL;
253				dinfo->opd_obdinfo.obd_type = NULL;
254				dinfo->opd_obdinfo.obd_model = NULL;
255				pci_add_child(dev, (struct pci_devinfo *)dinfo);
256			}
257		}
258	}
259}
260
261static int
262ofw_pcibus_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
263    size_t buflen)
264{
265	pci_child_pnpinfo_str_method(cbdev, child, buf, buflen);
266
267	if (ofw_bus_get_node(child) != -1)  {
268		strlcat(buf, " ", buflen); /* Separate info */
269		ofw_bus_gen_child_pnpinfo_str(cbdev, child, buf, buflen);
270	}
271
272	return (0);
273}
274
275static int
276ofw_pcibus_assign_interrupt(device_t dev, device_t child)
277{
278	ofw_pci_intr_t intr;
279	phandle_t node;
280	int isz;
281
282	node = ofw_bus_get_node(child);
283
284	if (node == -1) {
285		/* Non-firmware enumerated child, use standard routing */
286
287		/*
288		 * XXX: Right now we don't have anything sensible to do here,
289		 * since the ofw_imap stuff relies on nodes have a reg
290		 * property. There exists ways around this, so the ePAPR
291		 * spec will need to be studied.
292		 */
293
294		return (0);
295
296#ifdef NOTYET
297		intr = pci_get_intpin(child);
298		return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
299		    intr));
300#endif
301	}
302
303	/*
304	 * Any AAPL,interrupts property gets priority and is
305	 * fully specified (i.e. does not need routing)
306	 */
307
308	isz = OF_getprop(node, "AAPL,interrupts", &intr, sizeof(intr));
309	if (isz == sizeof(intr)) {
310		return (intr);
311	}
312
313	isz = OF_getprop(node, "interrupts", &intr, sizeof(intr));
314	if (isz != sizeof(intr)) {
315		/* No property; our best guess is the intpin. */
316		intr = pci_get_intpin(child);
317	}
318
319	/*
320	 * If we got intr from a property, it may or may not be an intpin.
321	 * For on-board devices, it frequently is not, and is completely out
322	 * of the valid intpin range.  For PCI slots, it hopefully is,
323	 * otherwise we will have trouble interfacing with non-OFW buses
324	 * such as cardbus.
325	 * Since we cannot tell which it is without violating layering, we
326	 * will always use the route_interrupt method, and treat exceptions
327	 * on the level they become apparent.
328	 */
329	return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr));
330}
331
332static const struct ofw_bus_devinfo *
333ofw_pcibus_get_devinfo(device_t bus, device_t dev)
334{
335	struct ofw_pcibus_devinfo *dinfo;
336
337	dinfo = device_get_ivars(dev);
338	return (&dinfo->opd_obdinfo);
339}
340
341