1147997Srwatson/*-
2147997Srwatson * Copyright (c) 2000 Michael Smith
3147997Srwatson * Copyright (c) 2000 BSDi
4147997Srwatson * All rights reserved.
5195767Skensmith *
6152285Sru * Redistribution and use in source and binary forms, with or without
7152285Sru * modification, are permitted provided that the following conditions
8147997Srwatson * are met:
9147997Srwatson * 1. Redistributions of source code must retain the above copyright
10147997Srwatson *    notice, this list of conditions and the following disclaimer.
11147997Srwatson * 2. Redistributions in binary form must reproduce the above copyright
12147997Srwatson *    notice, this list of conditions and the following disclaimer in the
13147997Srwatson *    documentation and/or other materials provided with the distribution.
14147997Srwatson *
15147997Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16147997Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17147997Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18147997Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19147997Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20147997Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21148358Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22148359Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23147997Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24147997Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25147997Srwatson * SUCH DAMAGE.
26152377Srwatson */
27152377Srwatson
28152377Srwatson#include <sys/cdefs.h>
29147997Srwatson__FBSDID("$FreeBSD$");
30147997Srwatson
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 <contrib/dev/acpica/include/acpi.h>
38#include <contrib/dev/acpica/include/accommon.h>
39
40#include <dev/acpica/acpivar.h>
41#include <dev/acpica/acpi_pcibvar.h>
42
43#include <dev/pci/pcivar.h>
44#include "pcib_if.h"
45
46/* Hooks for the ACPI CA debugging infrastructure. */
47#define _COMPONENT	ACPI_BUS
48ACPI_MODULE_NAME("PCI")
49
50ACPI_SERIAL_DECL(pcib, "ACPI PCI bus methods");
51
52/*
53 * For locking, we assume the caller is not concurrent since this is
54 * triggered by newbus methods.
55 */
56
57struct prt_lookup_request {
58    ACPI_PCI_ROUTING_TABLE *pr_entry;
59    u_int	pr_pin;
60    u_int	pr_slot;
61};
62
63typedef	void prt_entry_handler(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
64
65static void	prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
66static void	prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
67static void	prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler,
68		    void *arg);
69
70static void
71prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler, void *arg)
72{
73    ACPI_PCI_ROUTING_TABLE *entry;
74    char *prtptr;
75
76    /* First check to see if there is a table to walk. */
77    if (prt == NULL || prt->Pointer == NULL)
78	return;
79
80    /* Walk the table executing the handler function for each entry. */
81    prtptr = prt->Pointer;
82    entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
83    while (entry->Length != 0) {
84	handler(entry, arg);
85	prtptr += entry->Length;
86	entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
87    }
88}
89
90static void
91prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
92{
93    ACPI_HANDLE handle;
94    device_t child, pcib;
95    int error;
96
97    /* We only care about entries that reference a link device. */
98    if (entry->Source == NULL || entry->Source[0] == '\0')
99	return;
100
101    /*
102     * In practice, we only see SourceIndex's of 0 out in the wild.
103     * When indices != 0 have been found, they've been bugs in the ASL.
104     */
105    if (entry->SourceIndex != 0)
106	return;
107
108    /* Lookup the associated handle and device. */
109    pcib = (device_t)arg;
110    if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, entry->Source, &handle)))
111	return;
112    child = acpi_get_device(handle);
113    if (child == NULL)
114	return;
115
116    /* If the device hasn't been probed yet, force it to do so. */
117    error = device_probe_and_attach(child);
118    if (error != 0) {
119	device_printf(pcib, "failed to force attach of %s\n",
120	    acpi_name(handle));
121	return;
122    }
123
124    /* Add a reference for a specific bus/device/pin tuple. */
125    acpi_pci_link_add_reference(child, entry->SourceIndex, pcib,
126	ACPI_ADR_PCI_SLOT(entry->Address), entry->Pin);
127}
128
129int
130acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
131{
132    ACPI_STATUS status;
133    int error;
134
135    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
136
137    /*
138     * Get the PCI interrupt routing table for this bus.  If we can't
139     * get it, this is not an error but may reduce functionality.  There
140     * are several valid bridges in the field that do not have a _PRT, so
141     * only warn about missing tables if bootverbose is set.
142     */
143    prt->Length = ACPI_ALLOCATE_BUFFER;
144    status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
145    if (ACPI_FAILURE(status) && (bootverbose || status != AE_NOT_FOUND))
146	device_printf(dev,
147	    "could not get PCI interrupt routing table for %s - %s\n",
148	    acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
149
150    /*
151     * Attach the PCI bus proper.
152     */
153    if (device_add_child(dev, "pci", busno) == NULL) {
154	device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
155	return_VALUE(ENXIO);
156    }
157
158    /*
159     * Now go scan the bus.
160     */
161    prt_walk_table(prt, prt_attach_devices, dev);
162
163    error = bus_generic_attach(dev);
164    return_VALUE(error);
165}
166
167static void
168prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
169{
170    struct prt_lookup_request *pr;
171
172    pr = (struct prt_lookup_request *)arg;
173    if (pr->pr_entry != NULL)
174	return;
175
176    /*
177     * Compare the slot number (high word of Address) and pin number
178     * (note that ACPI uses 0 for INTA) to check for a match.
179     *
180     * Note that the low word of the Address field (function number)
181     * is required by the specification to be 0xffff.  We don't risk
182     * checking it here.
183     */
184    if (ACPI_ADR_PCI_SLOT(entry->Address) == pr->pr_slot &&
185	entry->Pin == pr->pr_pin)
186	    pr->pr_entry = entry;
187}
188
189/*
190 * Route an interrupt for a child of the bridge.
191 */
192int
193acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
194    ACPI_BUFFER *prtbuf)
195{
196    ACPI_PCI_ROUTING_TABLE *prt;
197    struct prt_lookup_request pr;
198    ACPI_HANDLE lnkdev;
199    int interrupt;
200
201    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
202
203    interrupt = PCI_INVALID_IRQ;
204
205    /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
206    pin--;
207
208    ACPI_SERIAL_BEGIN(pcib);
209
210    /* Search for a matching entry in the routing table. */
211    pr.pr_entry = NULL;
212    pr.pr_pin = pin;
213    pr.pr_slot = pci_get_slot(dev);
214    prt_walk_table(prtbuf, prt_lookup_device, &pr);
215    if (pr.pr_entry == NULL) {
216	device_printf(pcib, "no PRT entry for %d.%d.INT%c\n", pci_get_bus(dev),
217	    pci_get_slot(dev), 'A' + pin);
218	goto out;
219    }
220    prt = pr.pr_entry;
221
222    if (bootverbose) {
223	device_printf(pcib, "matched entry for %d.%d.INT%c",
224	    pci_get_bus(dev), pci_get_slot(dev), 'A' + pin);
225	if (prt->Source != NULL && prt->Source[0] != '\0')
226		printf(" (src %s:%u)", prt->Source, prt->SourceIndex);
227	printf("\n");
228    }
229
230    /*
231     * If source is empty/NULL, the source index is a global IRQ number
232     * and it's hard-wired so we're done.
233     *
234     * XXX: If the source index is non-zero, ignore the source device and
235     * assume that this is a hard-wired entry.
236     */
237    if (prt->Source == NULL || prt->Source[0] == '\0' ||
238	prt->SourceIndex != 0) {
239	if (bootverbose)
240	    device_printf(pcib, "slot %d INT%c hardwired to IRQ %d\n",
241		pci_get_slot(dev), 'A' + pin, prt->SourceIndex);
242	if (prt->SourceIndex) {
243	    interrupt = prt->SourceIndex;
244	    BUS_CONFIG_INTR(dev, interrupt, INTR_TRIGGER_LEVEL,
245		INTR_POLARITY_LOW);
246	} else
247	    device_printf(pcib, "error: invalid hard-wired IRQ of 0\n");
248	goto out;
249    }
250
251    /*
252     * We have to find the source device (PCI interrupt link device).
253     */
254    if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
255	device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
256	    prt->Source);
257	goto out;
258    }
259    interrupt = acpi_pci_link_route_interrupt(acpi_get_device(lnkdev),
260	prt->SourceIndex);
261
262    if (bootverbose && PCI_INTERRUPT_VALID(interrupt))
263	device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
264	    pci_get_slot(dev), 'A' + pin, interrupt, acpi_name(lnkdev));
265
266out:
267    ACPI_SERIAL_END(pcib);
268
269    return_VALUE(interrupt);
270}
271
272int
273acpi_pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
274{
275    device_t acpi_dev;
276
277    acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
278    acpi_device_pwr_for_sleep(acpi_dev, dev, pstate);
279    return (0);
280}
281