acpi_pcib.c revision 79284
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 *	$FreeBSD: head/sys/dev/acpica/acpi_pcib.c 79284 2001-07-05 07:20:51Z msmith $
28 */
29#include "opt_acpi.h"
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/malloc.h>
33#include <sys/kernel.h>
34
35#include "acpi.h"
36
37#include <dev/acpica/acpivar.h>
38
39#include <machine/pci_cfgreg.h>
40#include <pci/pcivar.h>
41#include "pcib_if.h"
42
43/*
44 * Hooks for the ACPI CA debugging infrastructure
45 */
46#define _COMPONENT	ACPI_BUS
47MODULE_NAME("PCI")
48
49struct acpi_pcib_softc {
50    device_t		ap_dev;
51    ACPI_HANDLE		ap_handle;
52
53    int			ap_segment;	/* analagous to Alpha 'hose' */
54    int			ap_bus;		/* bios-assigned bus number */
55
56    ACPI_BUFFER		ap_prt;		/* interrupt routing table */
57};
58
59static int		acpi_pcib_probe(device_t bus);
60static int		acpi_pcib_attach(device_t bus);
61static int		acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result);
62static int		acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value);
63static int		acpi_pcib_maxslots(device_t dev);
64static u_int32_t	acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes);
65static void		acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg,
66					       u_int32_t data, int bytes);
67static int		acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin);
68
69static device_method_t acpi_pcib_methods[] = {
70    /* Device interface */
71    DEVMETHOD(device_probe,		acpi_pcib_probe),
72    DEVMETHOD(device_attach,		acpi_pcib_attach),
73    DEVMETHOD(device_shutdown,		bus_generic_shutdown),
74    DEVMETHOD(device_suspend,		bus_generic_suspend),
75    DEVMETHOD(device_resume,		bus_generic_resume),
76
77    /* Bus interface */
78    DEVMETHOD(bus_print_child,		bus_generic_print_child),
79    DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
80    DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
81    DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
82    DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
83    DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
84    DEVMETHOD(bus_deactivate_resource, 	bus_generic_deactivate_resource),
85    DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
86    DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
87
88    /* pcib interface */
89    DEVMETHOD(pcib_maxslots,		acpi_pcib_maxslots),
90    DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
91    DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
92    DEVMETHOD(pcib_route_interrupt,	acpi_pcib_route_interrupt),
93
94    {0, 0}
95};
96
97static driver_t acpi_pcib_driver = {
98    "acpi_pcib",
99    acpi_pcib_methods,
100    sizeof(struct acpi_pcib_softc),
101};
102
103devclass_t acpi_pcib_devclass;
104DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_driver, acpi_pcib_devclass, 0, 0);
105
106static int
107acpi_pcib_probe(device_t dev)
108{
109
110    if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
111	!acpi_disabled("pci") &&
112	acpi_MatchHid(dev, "PNP0A03")) {
113
114	/*
115	 * Set device description
116	 */
117	device_set_desc(dev, "Host-PCI bridge");
118	return(0);
119    }
120    return(ENXIO);
121}
122
123static int
124acpi_pcib_attach(device_t dev)
125{
126    struct acpi_pcib_softc	*sc;
127    device_t			child;
128    ACPI_STATUS			status;
129    int				result;
130
131    FUNCTION_TRACE(__func__);
132
133    sc = device_get_softc(dev);
134    sc->ap_dev = dev;
135    sc->ap_handle = acpi_get_handle(dev);
136
137    /*
138     * Don't attach if we're not really there.
139     *
140     * XXX this isn't entirely correct, since we may be a PCI bus
141     * on a hot-plug docking station, etc.
142     */
143    if (!acpi_DeviceIsPresent(dev))
144	return_VALUE(ENXIO);
145
146    /*
147     * Get our segment number by evaluating _SEG
148     * It's OK for this to not exist.
149     */
150    if ((status = acpi_EvaluateInteger(sc->ap_handle, "_SEG", &sc->ap_segment)) != AE_OK) {
151	if (status != AE_NOT_FOUND) {
152	    device_printf(dev, "could not evaluate _SEG - %s\n", acpi_strerror(status));
153	    return_VALUE(ENXIO);
154	}
155	/* if it's not found, assume 0 */
156	sc->ap_segment = 0;
157    }
158
159    /*
160     * Get our base bus number by evaluating _BBN
161     * If this doesn't exist, we assume we're bus number 0.
162     *
163     * XXX note that it may also not exist in the case where we are
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     */
172    if ((status = acpi_EvaluateInteger(sc->ap_handle, "_BBN", &sc->ap_bus)) != AE_OK) {
173	if (status != AE_NOT_FOUND) {
174	    device_printf(dev, "could not evaluate _BBN - %s\n", acpi_strerror(status));
175	    return_VALUE(ENXIO);
176	}
177	/* if it's not found, assume 0 */
178	sc->ap_bus = 0;
179    }
180
181    /*
182     * Make sure that this bus hasn't already been found.  If it has, return silently
183     * (should we complain here?).
184     */
185    if (devclass_get_device(devclass_find("pci"), sc->ap_bus) != NULL)
186	return_VALUE(0);
187
188    /*
189     * Get the PCI interrupt routing table.
190     */
191    if ((status = acpi_GetIntoBuffer(sc->ap_handle, AcpiGetIrqRoutingTable, &sc->ap_prt)) != AE_OK) {
192	device_printf(dev, "could not get PCI interrupt routing table - %s\n", acpi_strerror(status));
193	/* this is not an error, but it may reduce functionality */
194    }
195
196    /*
197     * Attach the PCI bus proper.
198     */
199    if ((child = device_add_child(dev, "pci", sc->ap_bus)) == NULL) {
200	device_printf(device_get_parent(dev), "couldn't attach pci bus");
201	return_VALUE(ENXIO);
202    }
203
204    /*
205     * Now go scan the bus.
206     *
207     * XXX It would be nice to defer this and count on the nexus getting it
208     * after the first pass, but this does not seem to be reliable.
209     */
210    result = bus_generic_attach(dev);
211
212    /*
213     * XXX cross-reference our children to attached devices on the child bus
214     *     via _ADR, so we can provide power management.
215     */
216    /* XXX implement */
217
218    return_VALUE(result);
219}
220
221/*
222 * ACPI doesn't tell us how many slots there are, so use the standard
223 * maximum.
224 */
225static int
226acpi_pcib_maxslots(device_t dev)
227{
228    return(PCI_SLOTMAX);
229}
230
231/*
232 * Support for standard PCI bridge ivars.
233 */
234static int
235acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
236{
237    struct acpi_pcib_softc	*sc = device_get_softc(dev);
238
239    switch (which) {
240    case  PCIB_IVAR_BUS:
241	*result = sc->ap_bus;
242	return(0);
243    }
244    return(ENOENT);
245}
246
247static int
248acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
249{
250    struct acpi_pcib_softc	*sc = device_get_softc(dev);
251
252    switch (which) {
253    case  PCIB_IVAR_BUS:
254	sc->ap_bus = value;
255	return(0);
256    }
257    return(ENOENT);
258}
259
260static u_int32_t
261acpi_pcib_read_config(device_t dev, int bus, int slot, int func, int reg, int bytes)
262{
263    return(pci_cfgregread(bus, slot, func, reg, bytes));
264}
265
266static void
267acpi_pcib_write_config(device_t dev, int bus, int slot, int func, int reg, u_int32_t data, int bytes)
268{
269    pci_cfgregwrite(bus, slot, func, reg, data, bytes);
270}
271
272/*
273 * Route an interrupt for a child of the bridge.
274 *
275 * XXX clean up error messages
276 *
277 * XXX this function is somewhat bulky
278 */
279static int
280acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
281{
282    struct acpi_pcib_softc	*sc;
283    PCI_ROUTING_TABLE		*prt;
284    ACPI_HANDLE			lnkdev;
285    ACPI_BUFFER			crsbuf, prsbuf;
286    ACPI_RESOURCE		*crsres, *prsres;
287    ACPI_DEVICE_INFO		devinfo;
288    ACPI_OBJECT_LIST		objectlist;
289    ACPI_STATUS			status;
290    u_int8_t			*prtp;
291    device_t			*devlist;
292    int				devcount;
293    int				bus;
294    int				interrupt;
295    int				i;
296
297    crsbuf.Pointer = NULL;
298    prsbuf.Pointer = NULL;
299    devlist = NULL;
300    interrupt = 255;
301
302    /* ACPI numbers pins 0-3, not 1-4 like the BIOS */
303    pin--;
304
305    /* find the bridge softc */
306    if (devclass_get_devices(acpi_pcib_devclass, &devlist, &devcount))
307	goto out;
308    BUS_READ_IVAR(pcib, pcib, PCIB_IVAR_BUS, (uintptr_t *)&bus);
309    sc = NULL;
310    for (i = 0; i < devcount; i++) {
311	sc = device_get_softc(*(devlist + i));
312	if (sc->ap_bus == bus)
313	    break;
314	sc = NULL;
315    }
316    if (sc == NULL)			/* not one of ours */
317	goto out;
318    prtp = sc->ap_prt.Pointer;
319    if (prtp == NULL)			/* didn't get routing table */
320	goto out;
321
322    /* scan the table looking for this device */
323    for (;;) {
324	prt = (PCI_ROUTING_TABLE *)prtp;
325
326	if (prt->Length == 0)		/* end of table */
327	    goto out;
328
329	/*
330	 * Compare the slot number (high word of Address) and pin number
331	 * (note that ACPI uses 0 for INTA) to check for a match.
332	 *
333	 * Note that the low word of the Address field (function number)
334	 * is required by the specification to be 0xffff.  We don't risk
335	 * checking it here.
336	 */
337	if ((((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev)) &&
338	    (prt->Pin == pin)) {
339	    device_printf(sc->ap_dev, "matched entry for %d.%d.INT%c (source %s)\n",
340			  pci_get_bus(dev), pci_get_slot(dev), 'A' + pin, prt->Source);
341	    break;
342	}
343
344	/* skip to next entry */
345	prtp += prt->Length;
346    }
347
348    /*
349     * If source is empty/NULL, the source index is the global IRQ number.
350     */
351    if ((prt->Source == NULL) || (prt->Source[0] == '\0')) {
352	device_printf(sc->ap_dev, "device is hardwired to IRQ %d\n", prt->SourceIndex);
353	interrupt = prt->SourceIndex;
354	goto out;
355    }
356
357    /*
358     * We have to find the source device (PCI interrupt link device)
359     */
360    if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
361	device_printf(sc->ap_dev, "couldn't find PCI interrupt link device %s\n", prt->Source);
362	goto out;
363    }
364
365    /*
366     * Verify that this is a PCI link device, and that it's present.
367     */
368    if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &devinfo))) {
369	device_printf(sc->ap_dev, "couldn't validate PCI interrupt link device %s\n", prt->Source);
370	goto out;
371    }
372    if (!(devinfo.Valid & ACPI_VALID_HID) || strcmp("PNP0C0F", devinfo.HardwareId)) {
373	device_printf(sc->ap_dev, "PCI interrupt link device %s has wrong _HID (%s)\n",
374		      prt->Source, devinfo.HardwareId);
375	goto out;
376    }
377    /* should be 'present' and 'functioning' */
378    if ((devinfo.CurrentStatus & 0x09) != 0x09) {
379	device_printf(sc->ap_dev, "PCI interrupt link device %s unavailable (CurrentStatus 0x%x)\n",
380		      prt->Source, devinfo.CurrentStatus);
381	goto out;
382    }
383
384    /*
385     * Get the current and possible resources for the interrupt link device.
386     */
387    if (ACPI_FAILURE(status = acpi_GetIntoBuffer(lnkdev, AcpiGetCurrentResources, &crsbuf))) {
388	device_printf(sc->ap_dev, "couldn't get PCI interrupt link device _CRS data - %s\n",
389		      acpi_strerror(status));
390	goto out;	/* this is fatal */
391    }
392    if ((status = acpi_GetIntoBuffer(lnkdev, AcpiGetPossibleResources, &prsbuf)) != AE_OK) {
393	device_printf(sc->ap_dev, "couldn't get PCI interrupt link device _PRS data - %s\n",
394		      acpi_strerror(status));
395	/* this is not fatal, since it may be hardwired */
396    }
397    DEBUG_PRINT(TRACE_RESOURCES, ("got %d bytes for %s._CRS\n", crsbuf.Length, acpi_name(lnkdev)));
398    DEBUG_PRINT(TRACE_RESOURCES, ("got %d bytes for %s._PRS\n", prsbuf.Length, acpi_name(lnkdev)));
399
400    /*
401     * The interrupt may already be routed, so check _CRS first.  We don't check the
402     * 'decoding' bit in the _STA result, since there's nothing in the spec that
403     * mandates it be set, however some BIOS' will set it if the decode is active.
404     *
405     * The Source Index points to the particular resource entry we're interested in.
406     */
407    if (ACPI_FAILURE(acpi_FindIndexedResource((ACPI_RESOURCE *)crsbuf.Pointer, prt->SourceIndex, &crsres))) {
408	device_printf(sc->ap_dev, "_CRS buffer corrupt, cannot route interrupt\n");
409	goto out;
410    }
411
412    /* type-check the resource we've got */
413    if (crsres->Id != ACPI_RSTYPE_IRQ) {    /* XXX ACPI_RSTYPE_EXT_IRQ */
414	device_printf(sc->ap_dev, "_CRS resource entry has unsupported type %d\n", crsres->Id);
415	goto out;
416    }
417
418    /* if there's more than one interrupt, we are confused */
419    if (crsres->Data.Irq.NumberOfInterrupts > 1) {
420	device_printf(sc->ap_dev, "device has too many interrupts (%d)\n", crsres->Data.Irq.NumberOfInterrupts);
421	goto out;
422    }
423
424    /*
425     * If there's only one interrupt, and it's not zero, then we're already routed.
426     *
427     * Note that we could also check the 'decoding' bit in _STA, but can't depend on
428     * it since it's not part of the spec.
429     *
430     * XXX check ASL examples to see if this is an acceptable set of tests
431     */
432    if ((crsres->Data.Irq.NumberOfInterrupts == 1) && (crsres->Data.Irq.Interrupts[0] != 0)) {
433	device_printf(sc->ap_dev, "device is routed to IRQ %d\n", crsres->Data.Irq.Interrupts[0]);
434	interrupt = crsres->Data.Irq.Interrupts[0];
435	goto out;
436    }
437
438    /*
439     * There isn't an interrupt, so we have to look at _PRS to get one.
440     * Get the set of allowed interrupts from the _PRS resource indexed by SourceIndex.
441     */
442    if (prsbuf.Pointer == NULL) {
443	device_printf(sc->ap_dev, "device has no routed interrupt and no _PRS on PCI interrupt link device\n");
444	goto out;
445    }
446    if (ACPI_FAILURE(acpi_FindIndexedResource((ACPI_RESOURCE *)prsbuf.Pointer, prt->SourceIndex, &prsres))) {
447	device_printf(sc->ap_dev, "_PRS buffer corrupt, cannot route interrupt\n");
448	goto out;
449    }
450
451    /* type-check the resource we've got */
452    if (prsres->Id != ACPI_RSTYPE_IRQ) {    /* XXX ACPI_RSTYPE_EXT_IRQ */
453	device_printf(sc->ap_dev, "_PRS resource entry has unsupported type %d\n", prsres->Id);
454	goto out;
455    }
456
457    /* there has to be at least one interrupt available */
458    if (prsres->Data.Irq.NumberOfInterrupts < 1) {
459	device_printf(sc->ap_dev, "device has no interrupts\n");
460	goto out;
461    }
462
463    /*
464     * Pick an interrupt to use.  Note that a more scientific approach than just
465     * taking the first one available would be desirable.
466     *
467     * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI doesn't
468     * seem to offer a similar mechanism, so picking a "good" interrupt here is a
469     * difficult task.
470     *
471     * Populate our copy of _CRS and pass it back to _SRS to set the interrupt.
472     */
473    device_printf(sc->ap_dev, "possible interrupts:");
474    for (i = 0; i < prsres->Data.Irq.NumberOfInterrupts; i++)
475	printf("  %d", prsres->Data.Irq.Interrupts[i]);
476    printf("\n");
477    crsres->Data.Irq.Interrupts[0] = prsres->Data.Irq.Interrupts[0];
478    crsres->Data.Irq.NumberOfInterrupts = 1;
479    objectlist.Count = 1;
480    objectlist.Pointer = (ACPI_OBJECT *)crsres;
481    if (ACPI_FAILURE(status = AcpiEvaluateObject(lnkdev, "_SRS", &objectlist, NULL))) {
482	device_printf(sc->ap_dev, "couldn't route interrupt %d via %s - %s\n",
483		      prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev), acpi_strerror(status));
484	goto out;
485    }
486
487    /* successful, return the interrupt we just routed */
488    device_printf(sc->ap_dev, "routed interrupt %d via %s\n",
489		  prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev));
490    interrupt = prsres->Data.Irq.Interrupts[0];
491
492 out:
493    if (devlist != NULL)
494	free(devlist, M_TEMP);
495    if (crsbuf.Pointer != NULL)
496	AcpiOsFree(crsbuf.Pointer);
497    if (prsbuf.Pointer != NULL)
498	AcpiOsFree(prsbuf.Pointer);
499
500    /* XXX APIC_IO interrupt mapping? */
501    return(interrupt);
502}
503
504