acpi_pcib.c revision 89054
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 89054 2002-01-08 06:46:01Z 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
103static devclass_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", AcpiFormatException(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", AcpiFormatException(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", AcpiFormatException(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, resbuf;
287    ACPI_DEVICE_INFO		devinfo;
288    ACPI_STATUS			status;
289    u_int8_t			*prtp;
290    device_t			*devlist;
291    int				devcount;
292    int				bus;
293    int				interrupt;
294    int				i;
295    uintptr_t			up;
296
297    FUNCTION_TRACE(__func__);
298
299    crsbuf.Pointer = NULL;
300    prsbuf.Pointer = NULL;
301    devlist = NULL;
302    interrupt = 255;
303
304    /* ACPI numbers pins 0-3, not 1-4 like the BIOS */
305    pin--;
306
307    /* find the bridge softc */
308    if (devclass_get_devices(acpi_pcib_devclass, &devlist, &devcount))
309	goto out;
310    BUS_READ_IVAR(pcib, pcib, PCIB_IVAR_BUS, &up);
311    bus = up;
312    sc = NULL;
313    for (i = 0; i < devcount; i++) {
314	sc = device_get_softc(*(devlist + i));
315	if (sc->ap_bus == bus)
316	    break;
317	sc = NULL;
318    }
319    if (sc == NULL)			/* not one of ours */
320	goto out;
321    prtp = sc->ap_prt.Pointer;
322    if (prtp == NULL)			/* didn't get routing table */
323	goto out;
324
325    /* scan the table looking for this device */
326    for (;;) {
327	prt = (PCI_ROUTING_TABLE *)prtp;
328
329	if (prt->Length == 0)		/* end of table */
330	    goto out;
331
332	/*
333	 * Compare the slot number (high word of Address) and pin number
334	 * (note that ACPI uses 0 for INTA) to check for a match.
335	 *
336	 * Note that the low word of the Address field (function number)
337	 * is required by the specification to be 0xffff.  We don't risk
338	 * checking it here.
339	 */
340	if ((((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev)) &&
341	    (prt->Pin == pin)) {
342	    if (bootverbose)
343		device_printf(sc->ap_dev, "matched entry for %d.%d.INT%c (source %s)\n",
344			      pci_get_bus(dev), pci_get_slot(dev), 'A' + pin, prt->Source);
345	    break;
346	}
347
348	/* skip to next entry */
349	prtp += prt->Length;
350    }
351
352    /*
353     * If source is empty/NULL, the source index is the global IRQ number.
354     */
355    if ((prt->Source == NULL) || (prt->Source[0] == '\0')) {
356	if (bootverbose)
357	    device_printf(sc->ap_dev, "device is hardwired to IRQ %d\n", prt->SourceIndex);
358	interrupt = prt->SourceIndex;
359	goto out;
360    }
361
362    /*
363     * We have to find the source device (PCI interrupt link device)
364     */
365    if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
366	device_printf(sc->ap_dev, "couldn't find PCI interrupt link device %s\n", prt->Source);
367	goto out;
368    }
369
370    /*
371     * Verify that this is a PCI link device, and that it's present.
372     */
373    if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &devinfo))) {
374	device_printf(sc->ap_dev, "couldn't validate PCI interrupt link device %s\n", prt->Source);
375	goto out;
376    }
377    if (!(devinfo.Valid & ACPI_VALID_HID) || strcmp("PNP0C0F", devinfo.HardwareId)) {
378	device_printf(sc->ap_dev, "PCI interrupt link device %s has wrong _HID (%s)\n",
379		      prt->Source, devinfo.HardwareId);
380	goto out;
381    }
382    if (!acpi_DeviceIsPresent(sc->ap_dev)) {
383	device_printf(sc->ap_dev, "PCI interrupt link device %s not present\n",
384		      prt->Source);
385	goto out;
386    }
387
388    /*
389     * Get the current and possible resources for the interrupt link device.
390     */
391    if (ACPI_FAILURE(status = acpi_GetIntoBuffer(lnkdev, AcpiGetCurrentResources, &crsbuf))) {
392	device_printf(sc->ap_dev, "couldn't get PCI interrupt link device _CRS data - %s\n",
393		      AcpiFormatException(status));
394	goto out;	/* this is fatal */
395    }
396    if ((status = acpi_GetIntoBuffer(lnkdev, AcpiGetPossibleResources, &prsbuf)) != AE_OK) {
397	device_printf(sc->ap_dev, "couldn't get PCI interrupt link device _PRS data - %s\n",
398		      AcpiFormatException(status));
399	/* this is not fatal, since it may be hardwired */
400    }
401    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %d bytes for %s._CRS\n", crsbuf.Length, acpi_name(lnkdev)));
402    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %d bytes for %s._PRS\n", prsbuf.Length, acpi_name(lnkdev)));
403
404    /*
405     * The interrupt may already be routed, so check _CRS first.  We don't check the
406     * 'decoding' bit in the _STA result, since there's nothing in the spec that
407     * mandates it be set, however some BIOS' will set it if the decode is active.
408     *
409     * The Source Index points to the particular resource entry we're interested in.
410     */
411    if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex, &crsres))) {
412	device_printf(sc->ap_dev, "_CRS buffer corrupt, cannot route interrupt\n");
413	goto out;
414    }
415
416    /* type-check the resource we've got */
417    if (crsres->Id != ACPI_RSTYPE_IRQ) {    /* XXX ACPI_RSTYPE_EXT_IRQ */
418	device_printf(sc->ap_dev, "_CRS resource entry has unsupported type %d\n", crsres->Id);
419	goto out;
420    }
421
422    /* if there's more than one interrupt, we are confused */
423    if (crsres->Data.Irq.NumberOfInterrupts > 1) {
424	device_printf(sc->ap_dev, "device has too many interrupts (%d)\n", crsres->Data.Irq.NumberOfInterrupts);
425	goto out;
426    }
427
428    /*
429     * If there's only one interrupt, and it's not zero, then we're already routed.
430     *
431     * Note that we could also check the 'decoding' bit in _STA, but can't depend on
432     * it since it's not part of the spec.
433     *
434     * XXX check ASL examples to see if this is an acceptable set of tests
435     */
436    if ((crsres->Data.Irq.NumberOfInterrupts == 1) && (crsres->Data.Irq.Interrupts[0] != 0)) {
437	device_printf(sc->ap_dev, "device is routed to IRQ %d\n", crsres->Data.Irq.Interrupts[0]);
438	interrupt = crsres->Data.Irq.Interrupts[0];
439	goto out;
440    }
441
442    /*
443     * There isn't an interrupt, so we have to look at _PRS to get one.
444     * Get the set of allowed interrupts from the _PRS resource indexed by SourceIndex.
445     */
446    if (prsbuf.Pointer == NULL) {
447	device_printf(sc->ap_dev, "device has no routed interrupt and no _PRS on PCI interrupt link device\n");
448	goto out;
449    }
450    if (ACPI_FAILURE(acpi_FindIndexedResource(&prsbuf, prt->SourceIndex, &prsres))) {
451	device_printf(sc->ap_dev, "_PRS buffer corrupt, cannot route interrupt\n");
452	goto out;
453    }
454
455    /* type-check the resource we've got */
456    if (prsres->Id != ACPI_RSTYPE_IRQ) {    /* XXX ACPI_RSTYPE_EXT_IRQ */
457	device_printf(sc->ap_dev, "_PRS resource entry has unsupported type %d\n", prsres->Id);
458	goto out;
459    }
460
461    /* there has to be at least one interrupt available */
462    if (prsres->Data.Irq.NumberOfInterrupts < 1) {
463	device_printf(sc->ap_dev, "device has no interrupts\n");
464	goto out;
465    }
466
467    /*
468     * Pick an interrupt to use.  Note that a more scientific approach than just
469     * taking the first one available would be desirable.
470     *
471     * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI doesn't
472     * seem to offer a similar mechanism, so picking a "good" interrupt here is a
473     * difficult task.
474     *
475     * Build a resource buffer and pass it to AcpiSetCurrentResources to route the
476     * new interrupt.
477     */
478    device_printf(sc->ap_dev, "possible interrupts:");
479    for (i = 0; i < prsres->Data.Irq.NumberOfInterrupts; i++)
480	printf("  %d", prsres->Data.Irq.Interrupts[i]);
481    printf("\n");
482
483    if (crsbuf.Pointer != NULL)			/* should never happen */
484	AcpiOsFree(crsbuf.Pointer);
485    crsbuf.Pointer = NULL;
486    resbuf.Id = ACPI_RSTYPE_IRQ;
487    resbuf.Length = SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
488    resbuf.Data.Irq = prsres->Data.Irq;		/* structure copy other fields */
489    resbuf.Data.Irq.NumberOfInterrupts = 1;
490    resbuf.Data.Irq.Interrupts[0] = prsres->Data.Irq.Interrupts[0];	/* just take first... */
491    if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) {
492	device_printf(sc->ap_dev, "couldn't route interrupt %d via %s, interupt resource build failed - %s\n",
493		      prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status));
494	goto out;
495    }
496    if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) {
497	device_printf(sc->ap_dev, "couldn't route interrupt %d via %s - %s\n",
498		      prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status));
499	goto out;
500    }
501
502    /* successful, return the interrupt we just routed */
503    device_printf(sc->ap_dev, "routed interrupt %d via %s\n",
504		  prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev));
505    interrupt = prsres->Data.Irq.Interrupts[0];
506
507 out:
508    if (devlist != NULL)
509	free(devlist, M_TEMP);
510    if (crsbuf.Pointer != NULL)
511	AcpiOsFree(crsbuf.Pointer);
512    if (prsbuf.Pointer != NULL)
513	AcpiOsFree(prsbuf.Pointer);
514
515    /* XXX APIC_IO interrupt mapping? */
516    return_VALUE(interrupt);
517}
518
519