]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/pci/pci_bus.c
This commit was generated by cvs2svn to compensate for changes in r168515,
[FreeBSD/FreeBSD.git] / sys / amd64 / pci / pci_bus.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_cpu.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcib_private.h>
43 #include <isa/isavar.h>
44 #include <machine/legacyvar.h>
45 #include <machine/pci_cfgreg.h>
46 #include <machine/resource.h>
47
48 #include "pcib_if.h"
49
50 int
51 legacy_pcib_maxslots(device_t dev)
52 {
53         return 31;
54 }
55
56 /* read configuration space register */
57
58 u_int32_t
59 legacy_pcib_read_config(device_t dev, int bus, int slot, int func,
60                         int reg, int bytes)
61 {
62         return(pci_cfgregread(bus, slot, func, reg, bytes));
63 }
64
65 /* write configuration space register */
66
67 void
68 legacy_pcib_write_config(device_t dev, int bus, int slot, int func,
69                          int reg, u_int32_t data, int bytes)
70 {
71         pci_cfgregwrite(bus, slot, func, reg, data, bytes);
72 }
73
74 /* route interrupt */
75
76 static int
77 legacy_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
78 {
79
80         /* No routing possible */
81         return (PCI_INVALID_IRQ);
82 }
83
84 /* Pass MSI alloc requests up to the nexus. */
85
86 static int
87 legacy_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
88     int *irqs)
89 {
90         device_t bus;
91
92         bus = device_get_parent(pcib);
93         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
94             irqs));
95 }
96
97 static int
98 legacy_pcib_alloc_msix(device_t pcib, device_t dev, int index, int *irq)
99 {
100         device_t bus;
101
102         bus = device_get_parent(pcib);
103         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, index, irq));
104 }
105
106 static const char *
107 legacy_pcib_is_host_bridge(int bus, int slot, int func,
108                           uint32_t id, uint8_t class, uint8_t subclass,
109                           uint8_t *busnum)
110 {
111         const char *s = NULL;
112
113         *busnum = 0;
114         if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
115                 s = "Host to PCI bridge";
116         return s;
117 }
118
119 /*
120  * Scan the first pci bus for host-pci bridges and add pcib instances
121  * to the nexus for each bridge.
122  */
123 static void
124 legacy_pcib_identify(driver_t *driver, device_t parent)
125 {
126         int bus, slot, func;
127         u_int8_t  hdrtype;
128         int found = 0;
129         int pcifunchigh;
130         int found824xx = 0;
131         int found_orion = 0;
132         device_t child;
133         devclass_t pci_devclass;
134
135         if (pci_cfgregopen() == 0)
136                 return;
137         /*
138          * Check to see if we haven't already had a PCI bus added
139          * via some other means.  If we have, bail since otherwise
140          * we're going to end up duplicating it.
141          */
142         if ((pci_devclass = devclass_find("pci")) &&
143                 devclass_get_device(pci_devclass, 0))
144                 return;
145
146
147         bus = 0;
148  retry:
149         for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
150                 func = 0;
151                 hdrtype = legacy_pcib_read_config(0, bus, slot, func,
152                                                  PCIR_HDRTYPE, 1);
153                 /*
154                  * When enumerating bus devices, the standard says that
155                  * one should check the header type and ignore the slots whose
156                  * header types that the software doesn't know about.  We use
157                  * this to filter out devices.
158                  */
159                 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
160                         continue;
161                 if ((hdrtype & PCIM_MFDEV) &&
162                     (!found_orion || hdrtype != 0xff))
163                         pcifunchigh = PCI_FUNCMAX;
164                 else
165                         pcifunchigh = 0;
166                 for (func = 0; func <= pcifunchigh; func++) {
167                         /*
168                          * Read the IDs and class from the device.
169                          */
170                         u_int32_t id;
171                         u_int8_t class, subclass, busnum;
172                         const char *s;
173                         device_t *devs;
174                         int ndevs, i;
175
176                         id = legacy_pcib_read_config(0, bus, slot, func,
177                                                     PCIR_DEVVENDOR, 4);
178                         if (id == -1)
179                                 continue;
180                         class = legacy_pcib_read_config(0, bus, slot, func,
181                                                        PCIR_CLASS, 1);
182                         subclass = legacy_pcib_read_config(0, bus, slot, func,
183                                                           PCIR_SUBCLASS, 1);
184
185                         s = legacy_pcib_is_host_bridge(bus, slot, func,
186                                                       id, class, subclass,
187                                                       &busnum);
188                         if (s == NULL)
189                                 continue;
190
191                         /*
192                          * Check to see if the physical bus has already
193                          * been seen.  Eg: hybrid 32 and 64 bit host
194                          * bridges to the same logical bus.
195                          */
196                         if (device_get_children(parent, &devs, &ndevs) == 0) {
197                                 for (i = 0; s != NULL && i < ndevs; i++) {
198                                         if (strcmp(device_get_name(devs[i]),
199                                             "pcib") != 0)
200                                                 continue;
201                                         if (legacy_get_pcibus(devs[i]) == busnum)
202                                                 s = NULL;
203                                 }
204                                 free(devs, M_TEMP);
205                         }
206
207                         if (s == NULL)
208                                 continue;
209                         /*
210                          * Add at priority 100 to make sure we
211                          * go after any motherboard resources
212                          */
213                         child = BUS_ADD_CHILD(parent, 100,
214                                               "pcib", busnum);
215                         device_set_desc(child, s);
216                         legacy_set_pcibus(child, busnum);
217
218                         found = 1;
219                         if (id == 0x12258086)
220                                 found824xx = 1;
221                         if (id == 0x84c48086)
222                                 found_orion = 1;
223                 }
224         }
225         if (found824xx && bus == 0) {
226                 bus++;
227                 goto retry;
228         }
229
230         /*
231          * Make sure we add at least one bridge since some old
232          * hardware doesn't actually have a host-pci bridge device.
233          * Note that pci_cfgregopen() thinks we have PCI devices..
234          */
235         if (!found) {
236                 if (bootverbose)
237                         printf(
238         "legacy_pcib_identify: no bridge found, adding pcib0 anyway\n");
239                 child = BUS_ADD_CHILD(parent, 100, "pcib", 0);
240                 legacy_set_pcibus(child, 0);
241         }
242 }
243
244 static int
245 legacy_pcib_probe(device_t dev)
246 {
247
248         if (pci_cfgregopen() == 0)
249                 return ENXIO;
250         return -100;
251 }
252
253 static int
254 legacy_pcib_attach(device_t dev)
255 {
256         int bus;
257
258         bus = pcib_get_bus(dev);
259         device_add_child(dev, "pci", bus);
260         return bus_generic_attach(dev);
261 }
262
263 int
264 legacy_pcib_read_ivar(device_t dev, device_t child, int which,
265     uintptr_t *result)
266 {
267
268         switch (which) {
269         case  PCIB_IVAR_BUS:
270                 *result = legacy_get_pcibus(dev);
271                 return 0;
272         }
273         return ENOENT;
274 }
275
276 int
277 legacy_pcib_write_ivar(device_t dev, device_t child, int which,
278     uintptr_t value)
279 {
280
281         switch (which) {
282         case  PCIB_IVAR_BUS:
283                 legacy_set_pcibus(dev, value);
284                 return 0;
285         }
286         return ENOENT;
287 }
288
289 SYSCTL_DECL(_hw_pci);
290
291 static unsigned long legacy_host_mem_start = 0x80000000;
292 TUNABLE_ULONG("hw.pci.host_mem_start", &legacy_host_mem_start);
293 SYSCTL_ULONG(_hw_pci, OID_AUTO, host_mem_start, CTLFLAG_RDTUN,
294     &legacy_host_mem_start, 0x80000000,
295     "Limit the host bridge memory to being above this address.  Must be\n\
296 set at boot via a tunable.");
297
298 struct resource *
299 legacy_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
300     u_long start, u_long end, u_long count, u_int flags)
301 {
302     /*
303      * If no memory preference is given, use upper 32MB slot most
304      * bioses use for their memory window.  Typically other bridges
305      * before us get in the way to assert their preferences on memory.
306      * Hardcoding like this sucks, so a more MD/MI way needs to be
307      * found to do it.  This is typically only used on older laptops
308      * that don't have pci busses behind pci bridge, so assuming > 32MB
309      * is liekly OK.
310      *
311      * However, this can cause problems for other chipsets, so we make
312      * this tunable by hw.pci.host_mem_start.
313      */
314     if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
315         start = legacy_host_mem_start;
316     if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
317         start = 0x1000;
318     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
319         count, flags));
320 }
321
322 static device_method_t legacy_pcib_methods[] = {
323         /* Device interface */
324         DEVMETHOD(device_identify,      legacy_pcib_identify),
325         DEVMETHOD(device_probe,         legacy_pcib_probe),
326         DEVMETHOD(device_attach,        legacy_pcib_attach),
327         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
328         DEVMETHOD(device_suspend,       bus_generic_suspend),
329         DEVMETHOD(device_resume,        bus_generic_resume),
330
331         /* Bus interface */
332         DEVMETHOD(bus_print_child,      bus_generic_print_child),
333         DEVMETHOD(bus_read_ivar,        legacy_pcib_read_ivar),
334         DEVMETHOD(bus_write_ivar,       legacy_pcib_write_ivar),
335         DEVMETHOD(bus_alloc_resource,   legacy_pcib_alloc_resource),
336         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
337         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
338         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
339         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
340         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
341
342         /* pcib interface */
343         DEVMETHOD(pcib_maxslots,        legacy_pcib_maxslots),
344         DEVMETHOD(pcib_read_config,     legacy_pcib_read_config),
345         DEVMETHOD(pcib_write_config,    legacy_pcib_write_config),
346         DEVMETHOD(pcib_route_interrupt, legacy_pcib_route_interrupt),
347         DEVMETHOD(pcib_alloc_msi,       legacy_pcib_alloc_msi),
348         DEVMETHOD(pcib_release_msi,     pcib_release_msi),
349         DEVMETHOD(pcib_alloc_msix,      legacy_pcib_alloc_msix),
350         DEVMETHOD(pcib_remap_msix,      pcib_remap_msix),
351         DEVMETHOD(pcib_release_msix,    pcib_release_msix),
352
353         { 0, 0 }
354 };
355
356 static devclass_t hostb_devclass;
357
358 DEFINE_CLASS_0(pcib, legacy_pcib_driver, legacy_pcib_methods, 1);
359 DRIVER_MODULE(pcib, legacy, legacy_pcib_driver, hostb_devclass, 0, 0);
360
361
362 /*
363  * Install placeholder to claim the resources owned by the
364  * PCI bus interface.  This could be used to extract the
365  * config space registers in the extreme case where the PnP
366  * ID is available and the PCI BIOS isn't, but for now we just
367  * eat the PnP ID and do nothing else.
368  *
369  * XXX we should silence this probe, as it will generally confuse
370  * people.
371  */
372 static struct isa_pnp_id pcibus_pnp_ids[] = {
373         { 0x030ad041 /* PNP0A03 */, "PCI Bus" },
374         { 0 }
375 };
376
377 static int
378 pcibus_pnp_probe(device_t dev)
379 {
380         int result;
381
382         if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, pcibus_pnp_ids)) <= 0)
383                 device_quiet(dev);
384         return(result);
385 }
386
387 static int
388 pcibus_pnp_attach(device_t dev)
389 {
390         return(0);
391 }
392
393 static device_method_t pcibus_pnp_methods[] = {
394         /* Device interface */
395         DEVMETHOD(device_probe,         pcibus_pnp_probe),
396         DEVMETHOD(device_attach,        pcibus_pnp_attach),
397         DEVMETHOD(device_detach,        bus_generic_detach),
398         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
399         DEVMETHOD(device_suspend,       bus_generic_suspend),
400         DEVMETHOD(device_resume,        bus_generic_resume),
401         { 0, 0 }
402 };
403
404 static devclass_t pcibus_pnp_devclass;
405
406 DEFINE_CLASS_0(pcibus_pnp, pcibus_pnp_driver, pcibus_pnp_methods, 1);
407 DRIVER_MODULE(pcibus_pnp, isa, pcibus_pnp_driver, pcibus_pnp_devclass, 0, 0);