]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/amd64/pci/pci_bus.c
MFC r362623:
[FreeBSD/stable/8.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/rman.h>
39 #include <sys/sysctl.h>
40
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcib_private.h>
44 #include <isa/isavar.h>
45 #include <machine/legacyvar.h>
46 #include <machine/pci_cfgreg.h>
47 #include <machine/resource.h>
48
49 #include "pcib_if.h"
50
51 int
52 legacy_pcib_maxslots(device_t dev)
53 {
54         return 31;
55 }
56
57 /* read configuration space register */
58
59 uint32_t
60 legacy_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
61                         u_int reg, int bytes)
62 {
63         return(pci_cfgregread(bus, slot, func, reg, bytes));
64 }
65
66 /* write configuration space register */
67
68 void
69 legacy_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
70                          u_int reg, uint32_t data, int bytes)
71 {
72         pci_cfgregwrite(bus, slot, func, reg, data, bytes);
73 }
74
75 /* route interrupt */
76
77 static int
78 legacy_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
79 {
80
81         /* No routing possible */
82         return (PCI_INVALID_IRQ);
83 }
84
85 /* Pass MSI requests up to the nexus. */
86
87 static int
88 legacy_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
89     int *irqs)
90 {
91         device_t bus;
92
93         bus = device_get_parent(pcib);
94         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
95             irqs));
96 }
97
98 static int
99 legacy_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
100 {
101         device_t bus;
102
103         bus = device_get_parent(pcib);
104         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
105 }
106
107 int
108 legacy_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
109     uint32_t *data)
110 {
111         device_t bus, hostb;
112         int error, func, slot;
113
114         bus = device_get_parent(pcib);
115         error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
116         if (error)
117                 return (error);
118
119         slot = legacy_get_pcislot(pcib);
120         func = legacy_get_pcifunc(pcib);
121         if (slot == -1 || func == -1)
122                 return (0);
123         hostb = pci_find_bsf(0, slot, func);
124         KASSERT(hostb != NULL, ("%s: missing hostb for 0:%d:%d", __func__,
125             slot, func));
126         pci_ht_map_msi(hostb, *addr);
127         return (0);
128         
129 }
130
131 static const char *
132 legacy_pcib_is_host_bridge(int bus, int slot, int func,
133                           uint32_t id, uint8_t class, uint8_t subclass,
134                           uint8_t *busnum)
135 {
136         const char *s = NULL;
137
138         *busnum = 0;
139         if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
140                 s = "Host to PCI bridge";
141         return s;
142 }
143
144 /*
145  * Scan the first pci bus for host-pci bridges and add pcib instances
146  * to the nexus for each bridge.
147  */
148 static void
149 legacy_pcib_identify(driver_t *driver, device_t parent)
150 {
151         int bus, slot, func;
152         u_int8_t  hdrtype;
153         int found = 0;
154         int pcifunchigh;
155         int found824xx = 0;
156         int found_orion = 0;
157         device_t child;
158         devclass_t pci_devclass;
159
160         if (pci_cfgregopen() == 0)
161                 return;
162         /*
163          * Check to see if we haven't already had a PCI bus added
164          * via some other means.  If we have, bail since otherwise
165          * we're going to end up duplicating it.
166          */
167         if ((pci_devclass = devclass_find("pci")) &&
168                 devclass_get_device(pci_devclass, 0))
169                 return;
170
171
172         bus = 0;
173  retry:
174         for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
175                 func = 0;
176                 hdrtype = legacy_pcib_read_config(0, bus, slot, func,
177                                                  PCIR_HDRTYPE, 1);
178                 /*
179                  * When enumerating bus devices, the standard says that
180                  * one should check the header type and ignore the slots whose
181                  * header types that the software doesn't know about.  We use
182                  * this to filter out devices.
183                  */
184                 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
185                         continue;
186                 if ((hdrtype & PCIM_MFDEV) &&
187                     (!found_orion || hdrtype != 0xff))
188                         pcifunchigh = PCI_FUNCMAX;
189                 else
190                         pcifunchigh = 0;
191                 for (func = 0; func <= pcifunchigh; func++) {
192                         /*
193                          * Read the IDs and class from the device.
194                          */
195                         u_int32_t id;
196                         u_int8_t class, subclass, busnum;
197                         const char *s;
198                         device_t *devs;
199                         int ndevs, i;
200
201                         id = legacy_pcib_read_config(0, bus, slot, func,
202                                                     PCIR_DEVVENDOR, 4);
203                         if (id == -1)
204                                 continue;
205                         class = legacy_pcib_read_config(0, bus, slot, func,
206                                                        PCIR_CLASS, 1);
207                         subclass = legacy_pcib_read_config(0, bus, slot, func,
208                                                           PCIR_SUBCLASS, 1);
209
210                         s = legacy_pcib_is_host_bridge(bus, slot, func,
211                                                       id, class, subclass,
212                                                       &busnum);
213                         if (s == NULL)
214                                 continue;
215
216                         /*
217                          * Check to see if the physical bus has already
218                          * been seen.  Eg: hybrid 32 and 64 bit host
219                          * bridges to the same logical bus.
220                          */
221                         if (device_get_children(parent, &devs, &ndevs) == 0) {
222                                 for (i = 0; s != NULL && i < ndevs; i++) {
223                                         if (strcmp(device_get_name(devs[i]),
224                                             "pcib") != 0)
225                                                 continue;
226                                         if (legacy_get_pcibus(devs[i]) == busnum)
227                                                 s = NULL;
228                                 }
229                                 free(devs, M_TEMP);
230                         }
231
232                         if (s == NULL)
233                                 continue;
234                         /*
235                          * Add at priority 100 to make sure we
236                          * go after any motherboard resources
237                          */
238                         child = BUS_ADD_CHILD(parent, 100,
239                                               "pcib", busnum);
240                         device_set_desc(child, s);
241                         legacy_set_pcibus(child, busnum);
242                         legacy_set_pcislot(child, slot);
243                         legacy_set_pcifunc(child, func);
244
245                         found = 1;
246                         if (id == 0x12258086)
247                                 found824xx = 1;
248                         if (id == 0x84c48086)
249                                 found_orion = 1;
250                 }
251         }
252         if (found824xx && bus == 0) {
253                 bus++;
254                 goto retry;
255         }
256
257         /*
258          * Make sure we add at least one bridge since some old
259          * hardware doesn't actually have a host-pci bridge device.
260          * Note that pci_cfgregopen() thinks we have PCI devices..
261          */
262         if (!found) {
263                 if (bootverbose)
264                         printf(
265         "legacy_pcib_identify: no bridge found, adding pcib0 anyway\n");
266                 child = BUS_ADD_CHILD(parent, 100, "pcib", 0);
267                 legacy_set_pcibus(child, 0);
268         }
269 }
270
271 static int
272 legacy_pcib_probe(device_t dev)
273 {
274
275         if (pci_cfgregopen() == 0)
276                 return ENXIO;
277         return -100;
278 }
279
280 static int
281 legacy_pcib_attach(device_t dev)
282 {
283         int bus;
284
285         bus = pcib_get_bus(dev);
286         device_add_child(dev, "pci", bus);
287         return bus_generic_attach(dev);
288 }
289
290 int
291 legacy_pcib_read_ivar(device_t dev, device_t child, int which,
292     uintptr_t *result)
293 {
294
295         switch (which) {
296         case  PCIB_IVAR_DOMAIN:
297                 *result = 0;
298                 return 0;
299         case  PCIB_IVAR_BUS:
300                 *result = legacy_get_pcibus(dev);
301                 return 0;
302         }
303         return ENOENT;
304 }
305
306 int
307 legacy_pcib_write_ivar(device_t dev, device_t child, int which,
308     uintptr_t value)
309 {
310
311         switch (which) {
312         case  PCIB_IVAR_DOMAIN:
313                 return EINVAL;
314         case  PCIB_IVAR_BUS:
315                 legacy_set_pcibus(dev, value);
316                 return 0;
317         }
318         return ENOENT;
319 }
320
321 SYSCTL_DECL(_hw_pci);
322
323 static unsigned long legacy_host_mem_start = 0x80000000;
324 TUNABLE_ULONG("hw.pci.host_mem_start", &legacy_host_mem_start);
325 SYSCTL_ULONG(_hw_pci, OID_AUTO, host_mem_start, CTLFLAG_RDTUN,
326     &legacy_host_mem_start, 0x80000000,
327     "Limit the host bridge memory to being above this address.  Must be\n\
328 set at boot via a tunable.");
329
330 struct resource *
331 legacy_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
332     u_long start, u_long end, u_long count, u_int flags)
333 {
334     /*
335      * If no memory preference is given, use upper 32MB slot most
336      * bioses use for their memory window.  Typically other bridges
337      * before us get in the way to assert their preferences on memory.
338      * Hardcoding like this sucks, so a more MD/MI way needs to be
339      * found to do it.  This is typically only used on older laptops
340      * that don't have pci busses behind pci bridge, so assuming > 32MB
341      * is liekly OK.
342      *
343      * However, this can cause problems for other chipsets, so we make
344      * this tunable by hw.pci.host_mem_start.
345      */
346     if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
347         start = legacy_host_mem_start;
348     if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
349         start = 0x1000;
350     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
351         count, flags));
352 }
353
354 static device_method_t legacy_pcib_methods[] = {
355         /* Device interface */
356         DEVMETHOD(device_identify,      legacy_pcib_identify),
357         DEVMETHOD(device_probe,         legacy_pcib_probe),
358         DEVMETHOD(device_attach,        legacy_pcib_attach),
359         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
360         DEVMETHOD(device_suspend,       bus_generic_suspend),
361         DEVMETHOD(device_resume,        bus_generic_resume),
362
363         /* Bus interface */
364         DEVMETHOD(bus_read_ivar,        legacy_pcib_read_ivar),
365         DEVMETHOD(bus_write_ivar,       legacy_pcib_write_ivar),
366         DEVMETHOD(bus_alloc_resource,   legacy_pcib_alloc_resource),
367         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
368         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
369         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
370         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
371         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
372         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
373
374         /* pcib interface */
375         DEVMETHOD(pcib_maxslots,        legacy_pcib_maxslots),
376         DEVMETHOD(pcib_read_config,     legacy_pcib_read_config),
377         DEVMETHOD(pcib_write_config,    legacy_pcib_write_config),
378         DEVMETHOD(pcib_route_interrupt, legacy_pcib_route_interrupt),
379         DEVMETHOD(pcib_alloc_msi,       legacy_pcib_alloc_msi),
380         DEVMETHOD(pcib_release_msi,     pcib_release_msi),
381         DEVMETHOD(pcib_alloc_msix,      legacy_pcib_alloc_msix),
382         DEVMETHOD(pcib_release_msix,    pcib_release_msix),
383         DEVMETHOD(pcib_map_msi,         legacy_pcib_map_msi),
384
385         DEVMETHOD_END
386 };
387
388 static devclass_t hostb_devclass;
389
390 DEFINE_CLASS_0(pcib, legacy_pcib_driver, legacy_pcib_methods, 1);
391 DRIVER_MODULE(pcib, legacy, legacy_pcib_driver, hostb_devclass, 0, 0);
392
393
394 /*
395  * Install placeholder to claim the resources owned by the
396  * PCI bus interface.  This could be used to extract the
397  * config space registers in the extreme case where the PnP
398  * ID is available and the PCI BIOS isn't, but for now we just
399  * eat the PnP ID and do nothing else.
400  *
401  * XXX we should silence this probe, as it will generally confuse
402  * people.
403  */
404 static struct isa_pnp_id pcibus_pnp_ids[] = {
405         { 0x030ad041 /* PNP0A03 */, "PCI Bus" },
406         { 0x080ad041 /* PNP0A08 */, "PCIe Bus" },
407         { 0 }
408 };
409
410 static int
411 pcibus_pnp_probe(device_t dev)
412 {
413         int result;
414
415         if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, pcibus_pnp_ids)) <= 0)
416                 device_quiet(dev);
417         return(result);
418 }
419
420 static int
421 pcibus_pnp_attach(device_t dev)
422 {
423         return(0);
424 }
425
426 static device_method_t pcibus_pnp_methods[] = {
427         /* Device interface */
428         DEVMETHOD(device_probe,         pcibus_pnp_probe),
429         DEVMETHOD(device_attach,        pcibus_pnp_attach),
430         DEVMETHOD(device_detach,        bus_generic_detach),
431         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
432         DEVMETHOD(device_suspend,       bus_generic_suspend),
433         DEVMETHOD(device_resume,        bus_generic_resume),
434         { 0, 0 }
435 };
436
437 static devclass_t pcibus_pnp_devclass;
438
439 DEFINE_CLASS_0(pcibus_pnp, pcibus_pnp_driver, pcibus_pnp_methods, 1);
440 DRIVER_MODULE(pcibus_pnp, isa, pcibus_pnp_driver, pcibus_pnp_devclass, 0, 0);