1 /*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following 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 AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: src/sys/dev/acpica/acpi_pcib_pci.c,v 1.17.8.1 2009/04/15 03:14:26 kensmith Exp $
28 */
29
30 #include "opt_acpi.h"
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38
39 #include "acpi.h"
40 #include <dev/acpica/acpivar.h>
41 #include <dev/acpica/acpi_pcibvar.h>
42
43 #include <bus/pci/pcivar.h>
44 #include <bus/pci/pcireg.h>
45 #include <bus/pci/pcib_private.h>
46 #include "pcib_if.h"
47
48 /* Hooks for the ACPICA debugging infrastructure. */
49 #define _COMPONENT ACPI_BUS
50 ACPI_MODULE_NAME("PCI_PCI")
51
52 struct acpi_pcib_softc {
53 struct pcib_softc ap_pcibsc;
54 ACPI_HANDLE ap_handle;
55 ACPI_BUFFER ap_prt; /* interrupt routing table */
56 };
57
58 static int acpi_pcib_pci_probe(device_t bus);
59 static int acpi_pcib_pci_attach(device_t bus);
60 static int acpi_pcib_pci_resume(device_t bus);
61 static int acpi_pcib_read_ivar(device_t dev, device_t child,
62 int which, uintptr_t *result);
63 static int acpi_pcib_pci_route_interrupt(device_t pcib,
64 device_t dev, int pin);
65
66 static device_method_t acpi_pcib_pci_methods[] = {
67 /* Device interface */
68 DEVMETHOD(device_probe, acpi_pcib_pci_probe),
69 DEVMETHOD(device_attach, acpi_pcib_pci_attach),
70 DEVMETHOD(device_resume, acpi_pcib_pci_resume),
71
72 /* Bus interface */
73 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar),
74
75 /* pcib interface */
76 DEVMETHOD(pcib_route_interrupt, acpi_pcib_pci_route_interrupt),
77
78 DEVMETHOD_END
79 };
80
81 static devclass_t pcib_devclass;
82
83 DEFINE_CLASS_1(pcib, acpi_pcib_pci_driver, acpi_pcib_pci_methods,
84 sizeof(struct acpi_pcib_softc), pcib_driver);
85 DRIVER_MODULE(acpi_pcib, pci, acpi_pcib_pci_driver, pcib_devclass, NULL, NULL);
86 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
87
88 static int
acpi_pcib_pci_probe(device_t dev)89 acpi_pcib_pci_probe(device_t dev)
90 {
91 int error;
92
93 if (pci_get_class(dev) != PCIC_BRIDGE ||
94 pci_get_subclass(dev) != PCIS_BRIDGE_PCI ||
95 acpi_disabled("pci"))
96 return (ENXIO);
97
98 error = acpi_pcib_probe(dev);
99 if (error)
100 return (error);
101
102 device_set_desc(dev, "ACPI PCI-PCI bridge");
103 return (-100);
104 }
105
106 static int
acpi_pcib_pci_attach(device_t dev)107 acpi_pcib_pci_attach(device_t dev)
108 {
109 struct acpi_pcib_softc *sc;
110
111 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
112
113 pcib_attach_common(dev);
114 sc = device_get_softc(dev);
115 sc->ap_handle = acpi_get_handle(dev);
116 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_pcibsc.secbus));
117 }
118
119 static int
acpi_pcib_pci_resume(device_t dev)120 acpi_pcib_pci_resume(device_t dev)
121 {
122 return (acpi_pcib_resume(dev));
123 }
124
125 static int
acpi_pcib_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)126 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
127 {
128 struct acpi_pcib_softc *sc = device_get_softc(dev);
129
130 switch (which) {
131 case ACPI_IVAR_HANDLE:
132 *result = (uintptr_t)sc->ap_handle;
133 return (0);
134 }
135 return (pcib_read_ivar(dev, child, which, result));
136 }
137
138 static int
acpi_pcib_pci_route_interrupt(device_t pcib,device_t dev,int pin)139 acpi_pcib_pci_route_interrupt(device_t pcib, device_t dev, int pin)
140 {
141 struct acpi_pcib_softc *sc = device_get_softc(pcib);
142
143 /*
144 * If we don't have a _PRT, fall back to the swizzle method
145 * for routing interrupts.
146 */
147 if (sc->ap_prt.Pointer == NULL) {
148 device_printf(pcib, "No _PRT found, routing with pci\n");
149 return (pcib_route_interrupt(pcib, dev, pin));
150 } else {
151 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
152 }
153 }
154