]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/pci/ofw_pcibus.c
MFV r325013,r325034: 640 number_to_scaled_string is duplicated in several commands
[FreeBSD/FreeBSD.git] / sys / sparc64 / pci / ofw_pcibus.c
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  * Copyright (c) 2005 - 2009 Marius Strobl <marius@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_ofw_pci.h"
35
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/libkern.h>
40 #include <sys/module.h>
41 #include <sys/pciio.h>
42
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_pci.h>
45 #include <dev/ofw/openfirm.h>
46
47 #include <machine/bus.h>
48 #ifndef SUN4V
49 #include <machine/bus_common.h>
50 #include <machine/iommureg.h>
51 #endif
52 #include <machine/resource.h>
53
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pci_private.h>
57
58 #include <sparc64/pci/ofw_pci.h>
59
60 #include "pcib_if.h"
61 #include "pci_if.h"
62
63 /* Helper functions */
64 static void ofw_pcibus_setup_device(device_t bridge, uint32_t clock,
65     u_int busno, u_int slot, u_int func);
66
67 /* Methods */
68 static bus_child_deleted_t ofw_pcibus_child_deleted;
69 static bus_child_pnpinfo_str_t ofw_pcibus_pnpinfo_str;
70 static device_attach_t ofw_pcibus_attach;
71 static device_probe_t ofw_pcibus_probe;
72 static ofw_bus_get_devinfo_t ofw_pcibus_get_devinfo;
73 static pci_alloc_devinfo_t ofw_pcibus_alloc_devinfo;
74 static pci_assign_interrupt_t ofw_pcibus_assign_interrupt;
75
76 static device_method_t ofw_pcibus_methods[] = {
77         /* Device interface */
78         DEVMETHOD(device_probe,         ofw_pcibus_probe),
79         DEVMETHOD(device_attach,        ofw_pcibus_attach),
80
81         /* Bus interface */
82         DEVMETHOD(bus_child_deleted,    ofw_pcibus_child_deleted),
83         DEVMETHOD(bus_child_pnpinfo_str, ofw_pcibus_pnpinfo_str),
84         DEVMETHOD(bus_rescan,           bus_null_rescan),
85
86         /* PCI interface */
87         DEVMETHOD(pci_alloc_devinfo,    ofw_pcibus_alloc_devinfo),
88         DEVMETHOD(pci_assign_interrupt, ofw_pcibus_assign_interrupt),
89
90         /* ofw_bus interface */
91         DEVMETHOD(ofw_bus_get_devinfo,  ofw_pcibus_get_devinfo),
92         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
93         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
94         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
95         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
96         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
97
98         DEVMETHOD_END
99 };
100
101 struct ofw_pcibus_devinfo {
102         struct pci_devinfo      opd_dinfo;
103         struct ofw_bus_devinfo  opd_obdinfo;
104 };
105
106 static devclass_t pci_devclass;
107
108 DEFINE_CLASS_1(pci, ofw_pcibus_driver, ofw_pcibus_methods,
109     sizeof(struct pci_softc), pci_driver);
110 EARLY_DRIVER_MODULE(ofw_pcibus, pcib, ofw_pcibus_driver, pci_devclass, 0, 0,
111     BUS_PASS_BUS);
112 MODULE_VERSION(ofw_pcibus, 1);
113 MODULE_DEPEND(ofw_pcibus, pci, 1, 1, 1);
114
115 static int
116 ofw_pcibus_probe(device_t dev)
117 {
118
119         if (ofw_bus_get_node(dev) == -1)
120                 return (ENXIO);
121         device_set_desc(dev, "OFW PCI bus");
122
123         return (0);
124 }
125
126 /*
127  * Perform miscellaneous setups the firmware usually does not do for us.
128  */
129 static void
130 ofw_pcibus_setup_device(device_t bridge, uint32_t clock, u_int busno,
131     u_int slot, u_int func)
132 {
133 #define CS_READ(n, w)                                                   \
134         PCIB_READ_CONFIG(bridge, busno, slot, func, (n), (w))
135 #define CS_WRITE(n, v, w)                                               \
136         PCIB_WRITE_CONFIG(bridge, busno, slot, func, (n), (v), (w))
137
138 #ifndef SUN4V
139         uint32_t reg;
140
141         /*
142          * Initialize the latency timer register for busmaster devices to
143          * work properly.  This is another task which the firmware doesn't
144          * always perform.  The Min_Gnt register can be used to compute its
145          * recommended value: it contains the desired latency in units of
146          * 1/4 us assuming a clock rate of 33MHz.  To calculate the correct
147          * latency timer value, the clock frequency of the bus (defaulting
148          * to 33MHz) should be used and no wait states assumed.
149          * For bridges, we additionally set up the bridge control and the
150          * secondary latency registers.
151          */
152         if ((CS_READ(PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) ==
153             PCIM_HDRTYPE_BRIDGE) {
154                 reg = CS_READ(PCIR_BRIDGECTL_1, 1);
155                 reg |= PCIB_BCR_MASTER_ABORT_MODE | PCIB_BCR_SERR_ENABLE |
156                     PCIB_BCR_PERR_ENABLE;
157 #ifdef OFW_PCI_DEBUG
158                 device_printf(bridge,
159                     "bridge %d/%d/%d: control 0x%x -> 0x%x\n",
160                     busno, slot, func, CS_READ(PCIR_BRIDGECTL_1, 1), reg);
161 #endif /* OFW_PCI_DEBUG */
162                 CS_WRITE(PCIR_BRIDGECTL_1, reg, 1);
163
164                 reg = OFW_PCI_LATENCY;
165 #ifdef OFW_PCI_DEBUG
166                 device_printf(bridge,
167                     "bridge %d/%d/%d: latency timer %d -> %d\n",
168                     busno, slot, func, CS_READ(PCIR_SECLAT_1, 1), reg);
169 #endif /* OFW_PCI_DEBUG */
170                 CS_WRITE(PCIR_SECLAT_1, reg, 1);
171         } else {
172                 reg = CS_READ(PCIR_MINGNT, 1);
173                 if ((int)reg > 0) {
174                         switch (clock) {
175                         case 33000000:
176                                 reg *= 8;
177                                 break;
178                         case 66000000:
179                                 reg *= 4;
180                                 break;
181                         }
182                         reg = min(reg, 255);
183                 } else
184                         reg = OFW_PCI_LATENCY;
185         }
186 #ifdef OFW_PCI_DEBUG
187         device_printf(bridge, "device %d/%d/%d: latency timer %d -> %d\n",
188             busno, slot, func, CS_READ(PCIR_LATTIMER, 1), reg);
189 #endif /* OFW_PCI_DEBUG */
190         CS_WRITE(PCIR_LATTIMER, reg, 1);
191
192         /*
193          * Compute a value to write into the cache line size register.
194          * The role of the streaming cache is unclear in write invalidate
195          * transfers, so it is made sure that it's line size is always
196          * reached.  Generally, the cache line size is fixed at 64 bytes
197          * by Fireplane/Safari, JBus and UPA.
198          */
199         CS_WRITE(PCIR_CACHELNSZ, STRBUF_LINESZ / sizeof(uint32_t), 1);
200 #endif
201
202         /*
203          * Ensure that ALi M5229 report the actual content of PCIR_PROGIF
204          * and that IDE I/O is force enabled.  The former is done in order
205          * to have unique behavior across revisions as some default to
206          * hiding bits 4-6 for compliance with PCI 2.3.  The latter is done
207          * as at least revision 0xc8 requires the PCIM_CMD_PORTEN bypass
208          * to be always enabled as otherwise even enabling PCIM_CMD_PORTEN
209          * results in an instant data access trap on Fire-based machines.
210          * Thus these quirks have to be handled before pci(4) adds the maps.
211          * Note that for older revisions bit 0 of register 0x50 enables the
212          * internal IDE function instead of force enabling IDE I/O.
213          */
214         if ((CS_READ(PCIR_VENDOR, 2) == 0x10b9 &&
215             CS_READ(PCIR_DEVICE, 2) == 0x5229))
216                 CS_WRITE(0x50, CS_READ(0x50, 1) | 0x3, 1);
217
218         /*
219          * The preset in the intline register is usually wrong.  Reset
220          * it to 255, so that the PCI code will reroute the interrupt if
221          * needed.
222          */
223         CS_WRITE(PCIR_INTLINE, PCI_INVALID_IRQ, 1);
224
225 #undef CS_READ
226 #undef CS_WRITE
227 }
228
229 static int
230 ofw_pcibus_attach(device_t dev)
231 {
232         device_t pcib;
233         struct ofw_pci_register pcir;
234         struct ofw_pcibus_devinfo *dinfo;
235         phandle_t node, child;
236         uint32_t clock;
237         u_int busno, domain, func, slot;
238         int error;
239
240         error = pci_attach_common(dev);
241         if (error)
242                 return (error);
243         pcib = device_get_parent(dev);
244         domain = pcib_get_domain(dev);
245         busno = pcib_get_bus(dev);
246         node = ofw_bus_get_node(dev);
247
248         /*
249          * Add the PCI side of the host-PCI bridge itself to the bus.
250          * Note that we exclude the host-PCIe bridges here as these
251          * have no configuration space implemented themselves.
252          */
253         if (strcmp(device_get_name(device_get_parent(pcib)), "nexus") == 0 &&
254             ofw_bus_get_type(pcib) != NULL &&
255             strcmp(ofw_bus_get_type(pcib), OFW_TYPE_PCIE) != 0 &&
256             (dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib, dev,
257             domain, busno, 0, 0)) != NULL) {
258                 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0)
259                         pci_freecfg((struct pci_devinfo *)dinfo);
260                 else
261                         pci_add_child(dev, (struct pci_devinfo *)dinfo);
262         }
263
264         if (OF_getprop(ofw_bus_get_node(pcib), "clock-frequency", &clock,
265             sizeof(clock)) == -1)
266                 clock = 33000000;
267         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
268                 if (OF_getprop(child, "reg", &pcir, sizeof(pcir)) == -1)
269                         continue;
270                 slot = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
271                 func = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
272                 /* Some OFW device trees contain dupes. */
273                 if (pci_find_dbsf(domain, busno, slot, func) != NULL)
274                         continue;
275                 ofw_pcibus_setup_device(pcib, clock, busno, slot, func);
276                 dinfo = (struct ofw_pcibus_devinfo *)pci_read_device(pcib, dev,
277                     domain, busno, slot, func);
278                 if (dinfo == NULL)
279                         continue;
280                 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
281                     0) {
282                         pci_freecfg((struct pci_devinfo *)dinfo);
283                         continue;
284                 }
285                 pci_add_child(dev, (struct pci_devinfo *)dinfo);
286                 OFW_PCI_SETUP_DEVICE(pcib, dinfo->opd_dinfo.cfg.dev);
287         }
288
289         return (bus_generic_attach(dev));
290 }
291
292 struct pci_devinfo *
293 ofw_pcibus_alloc_devinfo(device_t dev)
294 {
295         struct ofw_pcibus_devinfo *dinfo;
296
297         dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
298         return (&dinfo->opd_dinfo);
299 }
300
301 static int
302 ofw_pcibus_assign_interrupt(device_t dev, device_t child)
303 {
304         ofw_pci_intr_t intr;
305         int isz;
306
307         isz = OF_getprop(ofw_bus_get_node(child), "interrupts", &intr,
308             sizeof(intr));
309         if (isz != sizeof(intr)) {
310                 /* No property; our best guess is the intpin. */
311                 intr = pci_get_intpin(child);
312 #ifndef SUN4V
313         } else if (intr >= 255) {
314                 /*
315                  * A fully specified interrupt (including IGN), as present on
316                  * SPARCengine Ultra AX and E450.  Extract the INO and return
317                  * it.
318                  */
319                 return (INTINO(intr));
320 #endif
321         }
322         /*
323          * If we got intr from a property, it may or may not be an intpin.
324          * For on-board devices, it frequently is not, and is completely out
325          * of the valid intpin range.  For PCI slots, it hopefully is,
326          * otherwise we will have trouble interfacing with non-OFW buses
327          * such as cardbus.
328          * Since we cannot tell which it is without violating layering, we
329          * will always use the route_interrupt method, and treat exceptions
330          * on the level they become apparent.
331          */
332         return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, intr));
333 }
334
335 static const struct ofw_bus_devinfo *
336 ofw_pcibus_get_devinfo(device_t bus, device_t dev)
337 {
338         struct ofw_pcibus_devinfo *dinfo;
339
340         dinfo = device_get_ivars(dev);
341         return (&dinfo->opd_obdinfo);
342 }
343
344 static void
345 ofw_pcibus_child_deleted(device_t dev, device_t child)
346 {
347         struct ofw_pcibus_devinfo *dinfo;
348
349         dinfo = device_get_ivars(dev);
350         ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
351         pci_child_deleted(dev, child);
352 }
353
354 static int
355 ofw_pcibus_pnpinfo_str(device_t dev, device_t child, char *buf,
356     size_t buflen)
357 {
358
359         pci_child_pnpinfo_str_method(dev, child, buf, buflen);
360         if (ofw_bus_get_node(child) != -1)  {
361                 strlcat(buf, " ", buflen); /* Separate info. */
362                 ofw_bus_gen_child_pnpinfo_str(dev, child, buf, buflen);
363         }
364
365         return (0);
366 }