2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/acpica/acpi_pcib_acpi.c,v 1.34 2004/05/30 20:08:23 phk Exp $
28 * $DragonFly: src/sys/dev/acpica5/acpi_pcib_acpi.c,v 1.4 2005/11/02 00:36:32 dillon Exp $
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
41 #include <machine/pci_cfgreg.h>
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcib_private.h>
46 #include "acpi_pcibvar.h"
48 /* Hooks for the ACPI CA debugging infrastructure. */
49 #define _COMPONENT ACPI_BUS
50 ACPI_MODULE_NAME("PCI_ACPI")
52 struct acpi_hpcib_softc
{
54 ACPI_HANDLE ap_handle
;
56 int ap_segment
; /* analagous to Alpha 'hose' */
57 int ap_bus
; /* bios-assigned bus number */
59 ACPI_BUFFER ap_prt
; /* interrupt routing table */
62 static int acpi_pcib_acpi_probe(device_t bus
);
63 static int acpi_pcib_acpi_attach(device_t bus
);
64 static int acpi_pcib_acpi_resume(device_t bus
);
65 static int acpi_pcib_read_ivar(device_t dev
, device_t child
,
66 int which
, uintptr_t *result
);
67 static int acpi_pcib_write_ivar(device_t dev
, device_t child
,
68 int which
, uintptr_t value
);
69 static uint32_t acpi_pcib_read_config(device_t dev
, int bus
, int slot
,
70 int func
, int reg
, int bytes
);
71 static void acpi_pcib_write_config(device_t dev
, int bus
, int slot
,
72 int func
, int reg
, uint32_t data
, int bytes
);
73 static int acpi_pcib_acpi_route_interrupt(device_t pcib
,
74 device_t dev
, int pin
);
75 static struct resource
*acpi_pcib_acpi_alloc_resource(device_t dev
,
76 device_t child
, int type
, int *rid
,
77 u_long start
, u_long end
, u_long count
,
80 static device_method_t acpi_pcib_acpi_methods
[] = {
81 /* Device interface */
82 DEVMETHOD(device_probe
, acpi_pcib_acpi_probe
),
83 DEVMETHOD(device_attach
, acpi_pcib_acpi_attach
),
84 DEVMETHOD(device_shutdown
, bus_generic_shutdown
),
85 DEVMETHOD(device_suspend
, bus_generic_suspend
),
86 DEVMETHOD(device_resume
, acpi_pcib_acpi_resume
),
89 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
90 DEVMETHOD(bus_read_ivar
, acpi_pcib_read_ivar
),
91 DEVMETHOD(bus_write_ivar
, acpi_pcib_write_ivar
),
92 DEVMETHOD(bus_alloc_resource
, acpi_pcib_acpi_alloc_resource
),
93 DEVMETHOD(bus_release_resource
, bus_generic_release_resource
),
94 DEVMETHOD(bus_activate_resource
, bus_generic_activate_resource
),
95 DEVMETHOD(bus_deactivate_resource
, bus_generic_deactivate_resource
),
96 DEVMETHOD(bus_setup_intr
, bus_generic_setup_intr
),
97 DEVMETHOD(bus_teardown_intr
, bus_generic_teardown_intr
),
100 DEVMETHOD(pcib_maxslots
, pcib_maxslots
),
101 DEVMETHOD(pcib_read_config
, acpi_pcib_read_config
),
102 DEVMETHOD(pcib_write_config
, acpi_pcib_write_config
),
103 DEVMETHOD(pcib_route_interrupt
, acpi_pcib_acpi_route_interrupt
),
108 static driver_t acpi_pcib_acpi_driver
= {
110 acpi_pcib_acpi_methods
,
111 sizeof(struct acpi_hpcib_softc
),
114 DRIVER_MODULE(acpi_pcib
, acpi
, acpi_pcib_acpi_driver
, pcib_devclass
, 0, 0);
115 MODULE_DEPEND(acpi_pcib
, acpi
, 1, 1, 1);
118 acpi_pcib_acpi_probe(device_t dev
)
121 if (acpi_get_type(dev
) == ACPI_TYPE_DEVICE
&& acpi_enabled("pci") &&
122 acpi_MatchHid(dev
, "PNP0A03")) {
124 if (pci_cfgregopen() == 0)
126 device_set_desc(dev
, "ACPI Host-PCI bridge");
134 acpi_pcib_acpi_attach(device_t dev
)
136 struct acpi_hpcib_softc
*sc
;
138 u_int addr
, slot
, func
, busok
;
141 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
143 sc
= device_get_softc(dev
);
145 sc
->ap_handle
= acpi_get_handle(dev
);
148 * Get our base bus number by evaluating _BBN.
149 * If this doesn't work, we assume we're bus number 0.
151 * XXX note that it may also not exist in the case where we are
152 * meant to use a private configuration space mechanism for this bus,
153 * so we should dig out our resources and check to see if we have
154 * anything like that. How do we do this?
155 * XXX If we have the requisite information, and if we don't think the
156 * default PCI configuration space handlers can deal with this bus,
157 * we should attach our own handler.
158 * XXX invoke _REG on this for the PCI config space address space?
159 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
160 * _BBN correctly. They set _BBN to zero for all bridges. Thus,
161 * if _BBN is zero and pcib0 already exists, we try to read our
162 * bus number from the configuration registers at address _ADR.
164 status
= acpi_GetInteger(sc
->ap_handle
, "_BBN", &sc
->ap_bus
);
165 if (ACPI_FAILURE(status
)) {
166 if (status
!= AE_NOT_FOUND
) {
167 device_printf(dev
, "could not evaluate _BBN - %s\n",
168 AcpiFormatException(status
));
169 return_VALUE (ENXIO
);
171 /* If it's not found, assume 0. */
177 * If the bus is zero and pcib0 already exists, read the bus number
178 * via PCI config space.
181 if (sc
->ap_bus
== 0 && devclass_get_device(pcib_devclass
, 0) != dev
) {
183 status
= acpi_GetInteger(sc
->ap_handle
, "_ADR", &addr
);
184 if (ACPI_FAILURE(status
)) {
185 if (status
!= AE_NOT_FOUND
) {
186 device_printf(dev
, "could not evaluate _ADR - %s\n",
187 AcpiFormatException(status
));
188 return_VALUE (ENXIO
);
190 device_printf(dev
, "couldn't find _ADR\n");
192 /* XXX: We assume bus 0. */
194 func
= addr
& 0xffff;
196 device_printf(dev
, "reading config registers from 0:%d:%d\n",
198 if (host_pcib_get_busno(pci_cfgregread
, 0, slot
, func
, &busno
) == 0)
199 device_printf(dev
, "couldn't read bus number from cfg space\n");
208 * If nothing else worked, hope that ACPI at least lays out the
209 * host-PCI bridges in order and that as a result our unit number
210 * is actually our bus number. There are several reasons this
214 sc
->ap_bus
= device_get_unit(dev
);
215 device_printf(dev
, "trying bus number %d\n", sc
->ap_bus
);
219 * Get our segment number by evaluating _SEG
220 * It's OK for this to not exist.
222 status
= acpi_GetInteger(sc
->ap_handle
, "_SEG", &sc
->ap_segment
);
223 if (ACPI_FAILURE(status
)) {
224 if (status
!= AE_NOT_FOUND
) {
225 device_printf(dev
, "could not evaluate _SEG - %s\n",
226 AcpiFormatException(status
));
227 return_VALUE (ENXIO
);
229 /* If it's not found, assume 0. */
233 return (acpi_pcib_attach(dev
, &sc
->ap_prt
, sc
->ap_bus
));
237 acpi_pcib_acpi_resume(device_t dev
)
239 struct acpi_hpcib_softc
*sc
= device_get_softc(dev
);
241 return (acpi_pcib_resume(dev
, &sc
->ap_prt
, sc
->ap_bus
));
245 * Support for standard PCI bridge ivars.
248 acpi_pcib_read_ivar(device_t dev
, device_t child
, int which
, uintptr_t *result
)
250 struct acpi_hpcib_softc
*sc
= device_get_softc(dev
);
254 *result
= sc
->ap_bus
;
256 case ACPI_IVAR_HANDLE
:
257 *result
= (uintptr_t)sc
->ap_handle
;
264 acpi_pcib_write_ivar(device_t dev
, device_t child
, int which
, uintptr_t value
)
266 struct acpi_hpcib_softc
*sc
= device_get_softc(dev
);
277 acpi_pcib_read_config(device_t dev
, int bus
, int slot
, int func
, int reg
,
280 return (pci_cfgregread(bus
, slot
, func
, reg
, bytes
));
284 acpi_pcib_write_config(device_t dev
, int bus
, int slot
, int func
, int reg
,
285 uint32_t data
, int bytes
)
287 pci_cfgregwrite(bus
, slot
, func
, reg
, data
, bytes
);
291 acpi_pcib_acpi_route_interrupt(device_t pcib
, device_t dev
, int pin
)
293 struct acpi_hpcib_softc
*sc
;
295 /* Find the bridge softc. */
296 sc
= device_get_softc(pcib
);
297 return (acpi_pcib_route_interrupt(pcib
, dev
, pin
, &sc
->ap_prt
));
301 acpi_pcib_acpi_alloc_resource(device_t dev
, device_t child
, int type
, int *rid
,
302 u_long start
, u_long end
, u_long count
, u_int flags
)
305 * If no memory preference is given, use upper 256MB slot most
306 * bioses use for their memory window. Typically other bridges
307 * before us get in the way to assert their preferences on memory.
308 * Hardcoding like this sucks, so a more MD/MI way needs to be
311 if (type
== SYS_RES_MEMORY
&& start
== 0UL && end
== ~0UL)
313 return (bus_generic_alloc_resource(dev
, child
, type
, rid
, start
, end
,