acpi_pcib_acpi.c revision 221336
1144091Sdas/*-
2144091Sdas * Copyright (c) 2000 Michael Smith
3144091Sdas * Copyright (c) 2000 BSDi
4144091Sdas * All rights reserved.
5144091Sdas *
6144091Sdas * Redistribution and use in source and binary forms, with or without
7144091Sdas * modification, are permitted provided that the following conditions
8144091Sdas * are met:
9144091Sdas * 1. Redistributions of source code must retain the above copyright
10144091Sdas *    notice, this list of conditions and the following disclaimer.
11144091Sdas * 2. Redistributions in binary form must reproduce the above copyright
12144091Sdas *    notice, this list of conditions and the following disclaimer in the
13144091Sdas *    documentation and/or other materials provided with the distribution.
14144091Sdas *
15144091Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16144091Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17144091Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18144091Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19144091Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20144091Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21144091Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22144091Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23144091Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24144091Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25144091Sdas * SUCH DAMAGE.
26144091Sdas */
27144091Sdas
28144091Sdas#include <sys/cdefs.h>
29144091Sdas__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_pcib_acpi.c 221336 2011-05-02 19:02:30Z jhb $");
30144091Sdas
31144091Sdas#include "opt_acpi.h"
32144091Sdas#include <sys/param.h>
33144091Sdas#include <sys/bus.h>
34144091Sdas#include <sys/kernel.h>
35144091Sdas#include <sys/malloc.h>
36144091Sdas#include <sys/module.h>
37144091Sdas#include <sys/sysctl.h>
38144091Sdas
39144091Sdas#include <contrib/dev/acpica/include/acpi.h>
40144091Sdas#include <contrib/dev/acpica/include/accommon.h>
41144091Sdas
42144091Sdas#include <dev/acpica/acpivar.h>
43144091Sdas
44144091Sdas#include <machine/pci_cfgreg.h>
45144091Sdas#include <dev/pci/pcivar.h>
46144091Sdas#include <dev/pci/pcib_private.h>
47144091Sdas#include "pcib_if.h"
48144091Sdas
49144091Sdas#include <dev/acpica/acpi_pcibvar.h>
50144091Sdas
51144091Sdas/* Hooks for the ACPI CA debugging infrastructure. */
52144091Sdas#define _COMPONENT	ACPI_BUS
53144091SdasACPI_MODULE_NAME("PCI_ACPI")
54144091Sdas
55144091Sdasstruct acpi_hpcib_softc {
56144091Sdas    device_t		ap_dev;
57144091Sdas    ACPI_HANDLE		ap_handle;
58144091Sdas    int			ap_flags;
59144091Sdas
60144091Sdas    int			ap_segment;	/* analagous to Alpha 'hose' */
61144091Sdas    int			ap_bus;		/* bios-assigned bus number */
62144091Sdas
63144091Sdas    ACPI_BUFFER		ap_prt;		/* interrupt routing table */
64144091Sdas};
65144091Sdas
66192760Sattiliostatic int		acpi_pcib_acpi_probe(device_t bus);
67static int		acpi_pcib_acpi_attach(device_t bus);
68static int		acpi_pcib_read_ivar(device_t dev, device_t child,
69			    int which, uintptr_t *result);
70static int		acpi_pcib_write_ivar(device_t dev, device_t child,
71			    int which, uintptr_t value);
72static uint32_t		acpi_pcib_read_config(device_t dev, u_int bus,
73			    u_int slot, u_int func, u_int reg, int bytes);
74static void		acpi_pcib_write_config(device_t dev, u_int bus,
75			    u_int slot, u_int func, u_int reg, uint32_t data,
76			    int bytes);
77static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
78			    device_t dev, int pin);
79static int		acpi_pcib_alloc_msi(device_t pcib, device_t dev,
80			    int count, int maxcount, int *irqs);
81static int		acpi_pcib_map_msi(device_t pcib, device_t dev,
82			    int irq, uint64_t *addr, uint32_t *data);
83static int		acpi_pcib_alloc_msix(device_t pcib, device_t dev,
84			    int *irq);
85static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
86			    device_t child, int type, int *rid,
87			    u_long start, u_long end, u_long count,
88			    u_int flags);
89
90static device_method_t acpi_pcib_acpi_methods[] = {
91    /* Device interface */
92    DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
93    DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
94    DEVMETHOD(device_shutdown,		bus_generic_shutdown),
95    DEVMETHOD(device_suspend,		bus_generic_suspend),
96    DEVMETHOD(device_resume,		bus_generic_resume),
97
98    /* Bus interface */
99    DEVMETHOD(bus_print_child,		bus_generic_print_child),
100    DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
101    DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
102    DEVMETHOD(bus_alloc_resource,	acpi_pcib_acpi_alloc_resource),
103    DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
104    DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
105    DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
106    DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
107    DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
108    DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
109
110    /* pcib interface */
111    DEVMETHOD(pcib_maxslots,		pcib_maxslots),
112    DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
113    DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
114    DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
115    DEVMETHOD(pcib_alloc_msi,		acpi_pcib_alloc_msi),
116    DEVMETHOD(pcib_release_msi,		pcib_release_msi),
117    DEVMETHOD(pcib_alloc_msix,		acpi_pcib_alloc_msix),
118    DEVMETHOD(pcib_release_msix,	pcib_release_msix),
119    DEVMETHOD(pcib_map_msi,		acpi_pcib_map_msi),
120    DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
121
122    {0, 0}
123};
124
125static devclass_t pcib_devclass;
126
127DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
128    sizeof(struct acpi_hpcib_softc));
129DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
130MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
131
132static int
133acpi_pcib_acpi_probe(device_t dev)
134{
135    ACPI_DEVICE_INFO	*devinfo;
136    ACPI_HANDLE		h;
137    int			root;
138
139    if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
140	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
141	return (ENXIO);
142    root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
143    AcpiOsFree(devinfo);
144    if (!root || pci_cfgregopen() == 0)
145	return (ENXIO);
146
147    device_set_desc(dev, "ACPI Host-PCI bridge");
148    return (0);
149}
150
151static int
152acpi_pcib_acpi_attach(device_t dev)
153{
154    struct acpi_hpcib_softc	*sc;
155    ACPI_STATUS			status;
156    static int bus0_seen = 0;
157    u_int addr, slot, func, busok;
158    uint8_t busno;
159
160    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
161
162    sc = device_get_softc(dev);
163    sc->ap_dev = dev;
164    sc->ap_handle = acpi_get_handle(dev);
165
166    /*
167     * Get our segment number by evaluating _SEG
168     * It's OK for this to not exist.
169     */
170    status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
171    if (ACPI_FAILURE(status)) {
172	if (status != AE_NOT_FOUND) {
173	    device_printf(dev, "could not evaluate _SEG - %s\n",
174		AcpiFormatException(status));
175	    return_VALUE (ENXIO);
176	}
177	/* If it's not found, assume 0. */
178	sc->ap_segment = 0;
179    }
180
181    /*
182     * Get our base bus number by evaluating _BBN.
183     * If this doesn't work, we assume we're bus number 0.
184     *
185     * XXX note that it may also not exist in the case where we are
186     *     meant to use a private configuration space mechanism for this bus,
187     *     so we should dig out our resources and check to see if we have
188     *     anything like that.  How do we do this?
189     * XXX If we have the requisite information, and if we don't think the
190     *     default PCI configuration space handlers can deal with this bus,
191     *     we should attach our own handler.
192     * XXX invoke _REG on this for the PCI config space address space?
193     * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
194     *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
195     *     if _BBN is zero and PCI bus 0 already exists, we try to read our
196     *     bus number from the configuration registers at address _ADR.
197     *     We only do this for domain/segment 0 in the hopes that this is
198     *     only needed for old single-domain machines.
199     */
200    status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
201    if (ACPI_FAILURE(status)) {
202	if (status != AE_NOT_FOUND) {
203	    device_printf(dev, "could not evaluate _BBN - %s\n",
204		AcpiFormatException(status));
205	    return_VALUE (ENXIO);
206	} else {
207	    /* If it's not found, assume 0. */
208	    sc->ap_bus = 0;
209	}
210    }
211
212    /*
213     * If this is segment 0, the bus is zero, and PCI bus 0 already
214     * exists, read the bus number via PCI config space.
215     */
216    busok = 1;
217    if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
218	busok = 0;
219	status = acpi_GetInteger(sc->ap_handle, "_ADR", &addr);
220	if (ACPI_FAILURE(status)) {
221	    if (status != AE_NOT_FOUND) {
222		device_printf(dev, "could not evaluate _ADR - %s\n",
223		    AcpiFormatException(status));
224		return_VALUE (ENXIO);
225	    } else
226		device_printf(dev, "couldn't find _ADR\n");
227	} else {
228	    /* XXX: We assume bus 0. */
229	    slot = ACPI_ADR_PCI_SLOT(addr);
230	    func = ACPI_ADR_PCI_FUNC(addr);
231	    if (bootverbose)
232		device_printf(dev, "reading config registers from 0:%d:%d\n",
233		    slot, func);
234	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
235		device_printf(dev, "couldn't read bus number from cfg space\n");
236	    else {
237		sc->ap_bus = busno;
238		busok = 1;
239	    }
240	}
241    }
242
243    /*
244     * If nothing else worked, hope that ACPI at least lays out the
245     * host-PCI bridges in order and that as a result our unit number
246     * is actually our bus number.  There are several reasons this
247     * might not be true.
248     */
249    if (busok == 0) {
250	sc->ap_bus = device_get_unit(dev);
251	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
252    }
253
254    /* If this is bus 0 on segment 0, note that it has been seen already. */
255    if (sc->ap_segment == 0 && sc->ap_bus == 0)
256	    bus0_seen = 1;
257
258    return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
259}
260
261/*
262 * Support for standard PCI bridge ivars.
263 */
264static int
265acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
266{
267    struct acpi_hpcib_softc	*sc = device_get_softc(dev);
268
269    switch (which) {
270    case PCIB_IVAR_DOMAIN:
271	*result = sc->ap_segment;
272	return (0);
273    case PCIB_IVAR_BUS:
274	*result = sc->ap_bus;
275	return (0);
276    case ACPI_IVAR_HANDLE:
277	*result = (uintptr_t)sc->ap_handle;
278	return (0);
279    case ACPI_IVAR_FLAGS:
280	*result = (uintptr_t)sc->ap_flags;
281	return (0);
282    }
283    return (ENOENT);
284}
285
286static int
287acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
288{
289    struct acpi_hpcib_softc	*sc = device_get_softc(dev);
290
291    switch (which) {
292    case PCIB_IVAR_DOMAIN:
293	return (EINVAL);
294    case PCIB_IVAR_BUS:
295	sc->ap_bus = value;
296	return (0);
297    case ACPI_IVAR_HANDLE:
298	sc->ap_handle = (ACPI_HANDLE)value;
299	return (0);
300    case ACPI_IVAR_FLAGS:
301	sc->ap_flags = (int)value;
302	return (0);
303    }
304    return (ENOENT);
305}
306
307static uint32_t
308acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
309    u_int reg, int bytes)
310{
311    return (pci_cfgregread(bus, slot, func, reg, bytes));
312}
313
314static void
315acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
316    u_int reg, uint32_t data, int bytes)
317{
318    pci_cfgregwrite(bus, slot, func, reg, data, bytes);
319}
320
321static int
322acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
323{
324    struct acpi_hpcib_softc *sc = device_get_softc(pcib);
325
326    return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
327}
328
329static int
330acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
331    int *irqs)
332{
333	device_t bus;
334
335	bus = device_get_parent(pcib);
336	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
337	    irqs));
338}
339
340static int
341acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
342{
343	device_t bus;
344
345	bus = device_get_parent(pcib);
346	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
347}
348
349static int
350acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
351    uint32_t *data)
352{
353	device_t bus;
354
355	bus = device_get_parent(pcib);
356	return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
357}
358
359static u_long acpi_host_mem_start = 0x80000000;
360TUNABLE_ULONG("hw.acpi.host_mem_start", &acpi_host_mem_start);
361
362struct resource *
363acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
364    u_long start, u_long end, u_long count, u_int flags)
365{
366    /*
367     * If no memory preference is given, use upper 32MB slot most
368     * bioses use for their memory window.  Typically other bridges
369     * before us get in the way to assert their preferences on memory.
370     * Hardcoding like this sucks, so a more MD/MI way needs to be
371     * found to do it.  This is typically only used on older laptops
372     * that don't have pci busses behind pci bridge, so assuming > 32MB
373     * is liekly OK.
374     */
375    if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
376	start = acpi_host_mem_start;
377    if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
378	start = 0x1000;
379    return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
380	count, flags));
381}
382