acpi_pcib.c revision 133533
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: head/sys/dev/acpica/acpi_pcib.c 133533 2004-08-12 02:06:19Z njl $");
30
31#include "opt_acpi.h"
32#include <sys/param.h>
33#include <sys/bus.h>
34#include <sys/malloc.h>
35#include <sys/kernel.h>
36
37#include "acpi.h"
38#include <dev/acpica/acpivar.h>
39#include <dev/acpica/acpi_pcibvar.h>
40
41#include <dev/pci/pcivar.h>
42#include "pcib_if.h"
43
44/* Hooks for the ACPI CA debugging infrastructure. */
45#define _COMPONENT	ACPI_BUS
46ACPI_MODULE_NAME("PCI")
47
48int
49acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
50{
51    device_t			child;
52    ACPI_STATUS			status;
53
54    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
55
56    /*
57     * Don't attach if we're not really there.
58     *
59     * XXX: This isn't entirely correct since we may be a PCI bus
60     * on a hot-plug docking station, etc.
61     */
62    if (!acpi_DeviceIsPresent(dev))
63	return_VALUE(ENXIO);
64
65    /*
66     * Get the PCI interrupt routing table for this bus.  If we can't
67     * get it, this is not an error but may reduce functionality.
68     */
69    prt->Length = ACPI_ALLOCATE_BUFFER;
70    status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
71    if (ACPI_FAILURE(status))
72	device_printf(dev,
73	    "could not get PCI interrupt routing table for %s - %s\n",
74	    acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
75
76    /*
77     * Attach the PCI bus proper.
78     */
79    if ((child = device_add_child(dev, "pci", busno)) == NULL) {
80	device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
81	return_VALUE(ENXIO);
82    }
83
84    /*
85     * Now go scan the bus.
86     */
87    acpi_pci_link_config(dev, prt, busno);
88
89    return_VALUE (bus_generic_attach(dev));
90}
91
92int
93acpi_pcib_resume(device_t dev)
94{
95    acpi_pci_link_resume(dev);
96    return (bus_generic_resume(dev));
97}
98
99/*
100 * Route an interrupt for a child of the bridge.
101 */
102int
103acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
104{
105    struct acpi_prt_entry	*entry;
106    int				i, interrupt;
107    struct acpi_pci_link_entry	*link;
108    ACPI_PCI_ROUTING_TABLE	*prt;
109
110    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
111
112    interrupt = PCI_INVALID_IRQ;
113
114    /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
115    pin--;
116
117    /* Look up the PRT entry for this device. */
118    entry = acpi_pci_find_prt(pcib, dev, pin);
119    if (entry == NULL) {
120	device_printf(pcib, "no PRT entry for %d.%d.INT%c\n", pci_get_bus(dev),
121	    pci_get_slot(dev), 'A' + pin);
122	goto out;
123    }
124    prt = &entry->prt;
125    link = entry->pci_link;
126    if (bootverbose)
127	device_printf(pcib, "matched entry for %d.%d.INT%c (src %s)\n",
128	    pci_get_bus(dev), pci_get_slot(dev), 'A' + pin,
129	    acpi_name(entry->prt_source));
130
131    /*
132     * If source is empty/NULL, the source index is a global IRQ number
133     * and it's hard-wired so we're done.
134     */
135    if (prt->Source == NULL || prt->Source[0] == '\0') {
136	if (bootverbose)
137	    device_printf(pcib, "slot %d INT%c hardwired to IRQ %d\n",
138		pci_get_slot(dev), 'A' + pin, prt->SourceIndex);
139	if (prt->SourceIndex)
140	    interrupt = prt->SourceIndex;
141	else
142	    device_printf(pcib, "error: invalid hard-wired IRQ of 0\n");
143	goto out;
144    }
145
146    /* XXX Support for multiple resources must be added to the link code. */
147    if (prt->SourceIndex) {
148	device_printf(pcib, "src index %d not yet supported\n",
149	    prt->SourceIndex);
150	goto out;
151    }
152
153    /* There has to be at least one interrupt available. */
154    if (link->number_of_interrupts == 0) {
155	device_printf(pcib, "device has no interrupts\n");
156	goto out;
157    }
158
159    /*
160     * If the current interrupt has been routed, we're done.  This is the
161     * case when the BIOS initializes it and we didn't disable it.
162     */
163    if (link->flags & ACPI_LINK_ROUTED) {
164	interrupt = link->current_irq;
165	if (bootverbose)
166	    device_printf(pcib, "slot %d INT%c is already routed to irq %d\n",
167		pci_get_slot(dev), 'A' + pin, interrupt);
168	goto out;
169    }
170
171    if (bootverbose) {
172	device_printf(pcib, "possible interrupts:");
173	for (i = 0; i < link->number_of_interrupts; i++)
174	    printf("%3d", link->interrupts[i]);
175	printf("\n");
176    }
177
178    /*
179     * Perform the link routing.  The link code will pick the best IRQ
180     * for this pin and configure it.
181     */
182    interrupt = acpi_pci_link_route(dev, entry);
183
184    if (bootverbose && PCI_INTERRUPT_VALID(interrupt))
185	device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
186	    pci_get_slot(dev), 'A' + pin, interrupt,
187	    acpi_name(entry->prt_source));
188
189out:
190
191    return_VALUE (interrupt);
192}
193