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