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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_acpi.h"
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/sysctl.h>
38
39 #include <contrib/dev/acpica/acpi.h>
40 #include <dev/acpica/acpivar.h>
41
42 #include <machine/pci_cfgreg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcib_private.h>
45 #include "pcib_if.h"
46
47 #include <dev/acpica/acpi_pcibvar.h>
48
49 /* Hooks for the ACPI CA debugging infrastructure. */
50 #define _COMPONENT ACPI_BUS
51 ACPI_MODULE_NAME("PCI_ACPI")
52
53 struct acpi_hpcib_softc {
54 device_t ap_dev;
55 ACPI_HANDLE ap_handle;
56 int ap_flags;
57
58 int ap_segment; /* analagous to Alpha 'hose' */
59 int ap_bus; /* bios-assigned bus number */
60
61 ACPI_BUFFER ap_prt; /* interrupt routing table */
62 };
63
64 static int acpi_pcib_acpi_probe(device_t bus);
65 static int acpi_pcib_acpi_attach(device_t bus);
66 static int acpi_pcib_read_ivar(device_t dev, device_t child,
67 int which, uintptr_t *result);
68 static int acpi_pcib_write_ivar(device_t dev, device_t child,
69 int which, uintptr_t value);
70 static uint32_t acpi_pcib_read_config(device_t dev, int bus, int slot,
71 int func, int reg, int bytes);
72 static void acpi_pcib_write_config(device_t dev, int bus, int slot,
73 int func, int reg, uint32_t data, int bytes);
74 static int acpi_pcib_acpi_route_interrupt(device_t pcib,
75 device_t dev, int pin);
76 static int acpi_pcib_alloc_msi(device_t pcib, device_t dev,
77 int count, int maxcount, int *irqs);
78 static int acpi_pcib_map_msi(device_t pcib, device_t dev,
79 int irq, uint64_t *addr, uint32_t *data);
80 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev,
81 int *irq);
82 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
83 device_t child, int type, int *rid,
84 u_long start, u_long end, u_long count,
85 u_int flags);
86
87 static device_method_t acpi_pcib_acpi_methods[] = {
88 /* Device interface */
89 DEVMETHOD(device_probe, acpi_pcib_acpi_probe),
90 DEVMETHOD(device_attach, acpi_pcib_acpi_attach),
91 DEVMETHOD(device_shutdown, bus_generic_shutdown),
92 DEVMETHOD(device_suspend, bus_generic_suspend),
93 DEVMETHOD(device_resume, bus_generic_resume),
94
95 /* Bus interface */
96 DEVMETHOD(bus_print_child, bus_generic_print_child),
97 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar),
98 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar),
99 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource),
100 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
101 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
102 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
103 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
104 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
105
106 /* pcib interface */
107 DEVMETHOD(pcib_maxslots, pcib_maxslots),
108 DEVMETHOD(pcib_read_config, acpi_pcib_read_config),
109 DEVMETHOD(pcib_write_config, acpi_pcib_write_config),
110 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt),
111 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi),
112 DEVMETHOD(pcib_release_msi, pcib_release_msi),
113 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix),
114 DEVMETHOD(pcib_release_msix, pcib_release_msix),
115 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi),
116
117 {0, 0}
118 };
119
120 static devclass_t pcib_devclass;
121
122 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
123 sizeof(struct acpi_hpcib_softc));
124 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
125 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
126
127 static int
128 acpi_pcib_acpi_probe(device_t dev)
129 {
130 static char *pcib_ids[] = { "PNP0A03", NULL };
131
132 if (acpi_disabled("pcib") ||
133 ACPI_ID_PROBE(device_get_parent(dev), dev, pcib_ids) == NULL)
134 return (ENXIO);
135
136 if (pci_cfgregopen() == 0)
137 return (ENXIO);
138 device_set_desc(dev, "ACPI Host-PCI bridge");
139 return (0);
140 }
141
142 static int
143 acpi_pcib_acpi_attach(device_t dev)
144 {
145 struct acpi_hpcib_softc *sc;
146 ACPI_STATUS status;
147 static int bus0_seen = 0;
148 u_int addr, slot, func, busok;
149 uint8_t busno;
150
151 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
152
153 sc = device_get_softc(dev);
154 sc->ap_dev = dev;
155 sc->ap_handle = acpi_get_handle(dev);
156
157 /*
158 * Get our segment number by evaluating _SEG
159 * It's OK for this to not exist.
160 */
161 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
162 if (ACPI_FAILURE(status)) {
163 if (status != AE_NOT_FOUND) {
164 device_printf(dev, "could not evaluate _SEG - %s\n",
165 AcpiFormatException(status));
166 return_VALUE (ENXIO);
167 }
168 /* If it's not found, assume 0. */
169 sc->ap_segment = 0;
170 }
171
172 /*
173 * Get our base bus number by evaluating _BBN.
174 * If this doesn't work, we assume we're bus number 0.
175 *
176 * XXX note that it may also not exist in the case where we are
177 * meant to use a private configuration space mechanism for this bus,
178 * so we should dig out our resources and check to see if we have
179 * anything like that. How do we do this?
180 * XXX If we have the requisite information, and if we don't think the
181 * default PCI configuration space handlers can deal with this bus,
182 * we should attach our own handler.
183 * XXX invoke _REG on this for the PCI config space address space?
184 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
185 * _BBN correctly. They set _BBN to zero for all bridges. Thus,
186 * if _BBN is zero and PCI bus 0 already exists, we try to read our
187 * bus number from the configuration registers at address _ADR.
188 * We only do this for domain/segment 0 in the hopes that this is
189 * only needed for old single-domain machines.
190 */
191 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
192 if (ACPI_FAILURE(status)) {
193 if (status != AE_NOT_FOUND) {
194 device_printf(dev, "could not evaluate _BBN - %s\n",
195 AcpiFormatException(status));
196 return_VALUE (ENXIO);
197 } else {
198 /* If it's not found, assume 0. */
199 sc->ap_bus = 0;
200 }
201 }
202
203 /*
204 * If this is segment 0, the bus is zero, and PCI bus 0 already
205 * exists, read the bus number via PCI config space.
206 */
207 busok = 1;
208 if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
209 busok = 0;
210 status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr);
211 if (ACPI_FAILURE(status)) {
212 if (status != AE_NOT_FOUND) {
213 device_printf(dev, "could not evaluate _ADR - %s\n",
214 AcpiFormatException(status));
215 return_VALUE (ENXIO);
216 } else
217 device_printf(dev, "couldn't find _ADR\n");
218 } else {
219 /* XXX: We assume bus 0. */
220 slot = ACPI_ADR_PCI_SLOT(addr);
221 func = ACPI_ADR_PCI_FUNC(addr);
222 if (bootverbose)
223 device_printf(dev, "reading config registers from 0:%d:%d\n",
224 slot, func);
225 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
226 device_printf(dev, "couldn't read bus number from cfg space\n");
227 else {
228 sc->ap_bus = busno;
229 busok = 1;
230 }
231 }
232 }
233
234 /*
235 * If nothing else worked, hope that ACPI at least lays out the
236 * host-PCI bridges in order and that as a result our unit number
237 * is actually our bus number. There are several reasons this
238 * might not be true.
239 */
240 if (busok == 0) {
241 sc->ap_bus = device_get_unit(dev);
242 device_printf(dev, "trying bus number %d\n", sc->ap_bus);
243 }
244
245 /* If this is bus 0 on segment 0, note that it has been seen already. */
246 if (sc->ap_segment == 0 && sc->ap_bus == 0)
247 bus0_seen = 1;
248
249 return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
250 }
251
252 /*
253 * Support for standard PCI bridge ivars.
254 */
255 static int
256 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
257 {
258 struct acpi_hpcib_softc *sc = device_get_softc(dev);
259
260 switch (which) {
261 case PCIB_IVAR_DOMAIN:
262 *result = 0;
263 return (0);
264 case PCIB_IVAR_BUS:
265 *result = sc->ap_bus;
266 return (0);
267 case ACPI_IVAR_HANDLE:
268 *result = (uintptr_t)sc->ap_handle;
269 return (0);
270 case ACPI_IVAR_FLAGS:
271 *result = (uintptr_t)sc->ap_flags;
272 return (0);
273 }
274 return (ENOENT);
275 }
276
277 static int
278 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
279 {
280 struct acpi_hpcib_softc *sc = device_get_softc(dev);
281
282 switch (which) {
283 case PCIB_IVAR_DOMAIN:
284 return (EINVAL);
285 case PCIB_IVAR_BUS:
286 sc->ap_bus = value;
287 return (0);
288 case ACPI_IVAR_HANDLE:
289 sc->ap_handle = (ACPI_HANDLE)value;
290 return (0);
291 case ACPI_IVAR_FLAGS:
292 sc->ap_flags = (int)value;
293 return (0);
294 }
295 return (ENOENT);
296 }
297
298 static uint32_t
299 acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg,
300 int bytes)
301 {
302 return (pci_cfgregread(bus, slot, func, reg, bytes));
303 }
304
305 static void
306 acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
307 uint32_t data, int bytes)
308 {
309 pci_cfgregwrite(bus, slot, func, reg, data, bytes);
310 }
311
312 static int
313 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
314 {
315 struct acpi_hpcib_softc *sc = device_get_softc(pcib);
316
317 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
318 }
319
320 static int
321 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
322 int *irqs)
323 {
324 device_t bus;
325
326 bus = device_get_parent(pcib);
327 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
328 irqs));
329 }
330
331 static int
332 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
333 {
334 device_t bus;
335
336 bus = device_get_parent(pcib);
337 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
338 }
339
340 static int
341 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
342 uint32_t *data)
343 {
344 device_t bus;
345
346 bus = device_get_parent(pcib);
347 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
348 }
349
350 static u_long acpi_host_mem_start = 0x80000000;
351 TUNABLE_ULONG("hw.acpi.host_mem_start", &acpi_host_mem_start);
352
353 struct resource *
354 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
355 u_long start, u_long end, u_long count, u_int flags)
356 {
357 /*
358 * If no memory preference is given, use upper 32MB slot most
359 * bioses use for their memory window. Typically other bridges
360 * before us get in the way to assert their preferences on memory.
361 * Hardcoding like this sucks, so a more MD/MI way needs to be
362 * found to do it. This is typically only used on older laptops
363 * that don't have pci busses behind pci bridge, so assuming > 32MB
364 * is liekly OK.
365 */
366 if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
367 start = acpi_host_mem_start;
368 if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
369 start = 0x1000;
370 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
371 count, flags));
372 }
Cache object: 7afa42ab280bc71aa14fc2611fcd8ef4
|