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