xref: /freebsd/sys/powerpc/ofw/ofw_pcibus.c (revision 59c3cb81c1769fdb6c840c971df129b52f4a848d)
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$");
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/intr_machdep.h>
47 #include <machine/resource.h>
48 
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pci_private.h>
52 
53 #include "ofw_pcibus.h"
54 #include "pcib_if.h"
55 #include "pci_if.h"
56 
57 typedef uint32_t ofw_pci_intr_t;
58 
59 /* Methods */
60 static device_probe_t ofw_pcibus_probe;
61 static device_attach_t ofw_pcibus_attach;
62 static pci_alloc_devinfo_t ofw_pcibus_alloc_devinfo;
63 static pci_assign_interrupt_t ofw_pcibus_assign_interrupt;
64 static ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo;
65 static bus_child_deleted_t ofw_pcibus_child_deleted;
66 static int ofw_pcibus_child_pnpinfo_str_method(device_t cbdev, device_t child,
67     char *buf, size_t buflen);
68 
69 static void ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno);
70 static void ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno);
71 
72 static device_method_t ofw_pcibus_methods[] = {
73 	/* Device interface */
74 	DEVMETHOD(device_probe,		ofw_pcibus_probe),
75 	DEVMETHOD(device_attach,	ofw_pcibus_attach),
76 
77 	/* Bus interface */
78 	DEVMETHOD(bus_child_deleted,	ofw_pcibus_child_deleted),
79 	DEVMETHOD(bus_child_pnpinfo_str, ofw_pcibus_child_pnpinfo_str_method),
80 
81 	/* PCI interface */
82 	DEVMETHOD(pci_alloc_devinfo,	ofw_pcibus_alloc_devinfo),
83 	DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
84 
85 	/* ofw_bus interface */
86 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_pcibus_get_devinfo),
87 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
88 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
89 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
90 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
91 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
92 
93 	DEVMETHOD_END
94 };
95 
96 static devclass_t pci_devclass;
97 
98 DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods,
99     sizeof(struct pci_softc), pci_driver);
100 DRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, pci_devclass, 0, 0);
101 MODULE_VERSION(ofw_pcibus, 1);
102 MODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1);
103 
104 static int ofw_devices_only = 0;
105 TUNABLE_INT("hw.pci.ofw_devices_only", &ofw_devices_only);
106 
107 static int
108 ofw_pcibus_probe(device_t dev)
109 {
110 
111 	if (ofw_bus_get_node(dev) == -1)
112 		return (ENXIO);
113 	device_set_desc(dev, "OFW PCI bus");
114 
115 	return (BUS_PROBE_DEFAULT);
116 }
117 
118 static int
119 ofw_pcibus_attach(device_t dev)
120 {
121 	u_int busno, domain;
122 	int error;
123 
124 	error = pci_attach_common(dev);
125 	if (error)
126 		return (error);
127 	domain = pcib_get_domain(dev);
128 	busno = pcib_get_bus(dev);
129 
130 	/*
131 	 * Attach those children represented in the device tree.
132 	 */
133 
134 	ofw_pcibus_enum_devtree(dev, domain, busno);
135 
136 	/*
137 	 * We now attach any laggard devices. FDT, for instance, allows
138 	 * the device tree to enumerate only some PCI devices. Apple's
139 	 * OF device tree on some Grackle-based hardware can also miss
140 	 * functions on multi-function cards.
141 	 */
142 
143 	if (!ofw_devices_only)
144 		ofw_pcibus_enum_bus(dev, domain, busno);
145 
146 	return (bus_generic_attach(dev));
147 }
148 
149 struct pci_devinfo *
150 ofw_pcibus_alloc_devinfo(device_t dev)
151 {
152 	struct ofw_pcibus_devinfo *dinfo;
153 
154 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
155 	return (&dinfo->opd_dinfo);
156 }
157 
158 static void
159 ofw_pcibus_enum_devtree(device_t dev, u_int domain, u_int busno)
160 {
161 	device_t pcib;
162 	struct ofw_pci_register pcir;
163 	struct ofw_pcibus_devinfo *dinfo;
164 	phandle_t node, child;
165 	u_int func, slot;
166 	int intline;
167 
168 	pcib = device_get_parent(dev);
169 	node = ofw_bus_get_node(dev);
170 
171 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
172 		if (OF_getencprop(child, "reg", (pcell_t *)&pcir,
173 		    sizeof(pcir)) == -1)
174 			continue;
175 		slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
176 		func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
177 
178 		/* Some OFW device trees contain dupes. */
179 		if (pci_find_dbsf(domain, busno, slot, func) != NULL)
180 			continue;
181 
182 		/*
183 		 * The preset in the intline register is usually bogus.  Reset
184 		 * it such that the PCI code will reroute the interrupt if
185 		 * needed.
186 		 */
187 
188 		intline = PCI_INVALID_IRQ;
189 		if (OF_getproplen(child, "interrupts") > 0)
190 			intline = 0;
191 		PCIB_WRITE_CONFIG(pcib, busno, slot, func, PCIR_INTLINE,
192 		    intline, 1);
193 
194 		/*
195 		 * Now set up the PCI and OFW bus layer devinfo and add it
196 		 * to the PCI bus.
197 		 */
198 
199 		dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib, dev,
200 		    domain, busno, slot, func);
201 		if (dinfo == NULL)
202 			continue;
203 		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
204 		    0) {
205 			pci_freecfg((struct pci_devinfo *)dinfo);
206 			continue;
207 		}
208 		dinfo->opd_dma_tag = NULL;
209 		pci_add_child(dev, (struct pci_devinfo *)dinfo);
210 
211 		/*
212 		 * Some devices don't have an intpin set, but do have
213 		 * interrupts. These are fully specified, and set in the
214 		 * interrupts property, so add that value to the device's
215 		 * resource list.
216 		 */
217 		if (dinfo->opd_dinfo.cfg.intpin == 0)
218 			ofw_bus_intr_to_rl(dev, child,
219 				&dinfo->opd_dinfo.resources, NULL);
220 	}
221 }
222 
223 /*
224  * The following is an almost exact clone of pci_add_children(), with the
225  * addition that it (a) will not add children that have already been added,
226  * and (b) will set up the OFW devinfo to point to invalid values. This is
227  * to handle non-enumerated PCI children as exist in FDT and on the second
228  * function of the Rage 128 in my Blue & White G3.
229  */
230 
231 static void
232 ofw_pcibus_enum_bus(device_t dev, u_int domain, u_int busno)
233 {
234 	device_t pcib;
235 	struct ofw_pcibus_devinfo *dinfo;
236 	int maxslots;
237 	int s, f, pcifunchigh;
238 	uint8_t hdrtype;
239 
240 	pcib = device_get_parent(dev);
241 
242 	maxslots = PCIB_MAXSLOTS(pcib);
243 	for (s = 0; s <= maxslots; s++) {
244 		pcifunchigh = 0;
245 		f = 0;
246 		DELAY(1);
247 		hdrtype = PCIB_READ_CONFIG(pcib, busno, s, f, PCIR_HDRTYPE, 1);
248 		if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
249 			continue;
250 		if (hdrtype & PCIM_MFDEV)
251 			pcifunchigh = PCI_FUNCMAX;
252 		for (f = 0; f <= pcifunchigh; f++) {
253 			/* Filter devices we have already added */
254 			if (pci_find_dbsf(domain, busno, s, f) != NULL)
255 				continue;
256 
257 			dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(
258 			    pcib, dev, domain, busno, s, f);
259 			if (dinfo == NULL)
260 				continue;
261 
262 			dinfo->opd_dma_tag = NULL;
263 			dinfo->opd_obdinfo.obd_node = -1;
264 
265 			dinfo->opd_obdinfo.obd_name = NULL;
266 			dinfo->opd_obdinfo.obd_compat = NULL;
267 			dinfo->opd_obdinfo.obd_type = NULL;
268 			dinfo->opd_obdinfo.obd_model = NULL;
269 
270 			/*
271 			 * For non OFW-devices, don't believe 0
272 			 * for an interrupt.
273 			 */
274 			if (dinfo->opd_dinfo.cfg.intline == 0) {
275 				dinfo->opd_dinfo.cfg.intline = PCI_INVALID_IRQ;
276 				PCIB_WRITE_CONFIG(pcib, busno, s, f,
277 				    PCIR_INTLINE, PCI_INVALID_IRQ, 1);
278 			}
279 
280 			pci_add_child(dev, (struct pci_devinfo *)dinfo);
281 		}
282 	}
283 }
284 
285 static void
286 ofw_pcibus_child_deleted(device_t dev, device_t child)
287 {
288 	struct ofw_pcibus_devinfo *dinfo;
289 
290 	dinfo = device_get_ivars(dev);
291 	ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
292 	pci_child_deleted(dev, child);
293 }
294 
295 static int
296 ofw_pcibus_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
297     size_t buflen)
298 {
299 	pci_child_pnpinfo_str_method(cbdev, child, buf, buflen);
300 
301 	if (ofw_bus_get_node(child) != -1)  {
302 		strlcat(buf, " ", buflen); /* Separate info */
303 		ofw_bus_gen_child_pnpinfo_str(cbdev, child, buf, buflen);
304 	}
305 
306 	return (0);
307 }
308 
309 static int
310 ofw_pcibus_assign_interrupt(device_t dev, device_t child)
311 {
312 	ofw_pci_intr_t intr[2];
313 	phandle_t node, iparent;
314 	int isz, icells;
315 
316 	node = ofw_bus_get_node(child);
317 
318 	if (node == -1) {
319 		/* Non-firmware enumerated child, use standard routing */
320 
321 		intr[0] = pci_get_intpin(child);
322 		return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
323 		    intr[0]));
324 	}
325 
326 	/*
327 	 * Try to determine the node's interrupt parent so we know which
328 	 * PIC to use.
329 	 */
330 
331 	iparent = -1;
332 	if (OF_getencprop(node, "interrupt-parent", &iparent,
333 	    sizeof(iparent)) < 0)
334 		iparent = -1;
335 	icells = 1;
336 	if (iparent != -1)
337 		OF_getencprop(OF_node_from_xref(iparent), "#interrupt-cells",
338 		    &icells, sizeof(icells));
339 
340 	/*
341 	 * Any AAPL,interrupts property gets priority and is
342 	 * fully specified (i.e. does not need routing)
343 	 */
344 
345 	isz = OF_getencprop(node, "AAPL,interrupts", intr, sizeof(intr));
346 	if (isz == sizeof(intr[0])*icells)
347 		return ((iparent == -1) ? intr[0] : ofw_bus_map_intr(dev,
348 		    iparent, icells, intr));
349 
350 	isz = OF_getencprop(node, "interrupts", intr, sizeof(intr));
351 	if (isz == sizeof(intr[0])*icells) {
352 		if (iparent != -1)
353 			intr[0] = ofw_bus_map_intr(dev, iparent, icells, intr);
354 	} else {
355 		/* No property: our best guess is the intpin. */
356 		intr[0] = pci_get_intpin(child);
357 	}
358 
359 	/*
360 	 * If we got intr from a property, it may or may not be an intpin.
361 	 * For on-board devices, it frequently is not, and is completely out
362 	 * of the valid intpin range.  For PCI slots, it hopefully is,
363 	 * otherwise we will have trouble interfacing with non-OFW buses
364 	 * such as cardbus.
365 	 * Since we cannot tell which it is without violating layering, we
366 	 * will always use the route_interrupt method, and treat exceptions
367 	 * on the level they become apparent.
368 	 */
369 	return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr[0]));
370 }
371 
372 static const struct ofw_bus_devinfo *
373 ofw_pcibus_get_devinfo(device_t bus, device_t dev)
374 {
375 	struct ofw_pcibus_devinfo *dinfo;
376 
377 	dinfo = device_get_ivars(dev);
378 	return (&dinfo->opd_obdinfo);
379 }
380 
381