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