Home | History | Annotate | Line # | Download | only in acpi
acpi_pci.c revision 1.6
      1  1.5  jruoho /* $NetBSD: acpi_pci.c,v 1.6 2010/04/22 14:50:31 jruoho Exp $ */
      2  1.1  cegger 
      3  1.1  cegger /*
      4  1.6  jruoho  * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
      5  1.1  cegger  * All rights reserved.
      6  1.1  cegger  *
      7  1.1  cegger  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6  jruoho  * by Christoph Egger and Gregoire Sutre.
      9  1.1  cegger  *
     10  1.1  cegger  * Redistribution and use in source and binary forms, with or without
     11  1.1  cegger  * modification, are permitted provided that the following conditions
     12  1.1  cegger  * are met:
     13  1.1  cegger  * 1. Redistributions of source code must retain the above copyright
     14  1.1  cegger  *    notice, this list of conditions and the following disclaimer.
     15  1.1  cegger  * 2. The name of the author may not be used to endorse or promote products
     16  1.1  cegger  *    derived from this software without specific prior written permission.
     17  1.1  cegger  *
     18  1.1  cegger  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  cegger  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  cegger  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  cegger  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  cegger  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.1  cegger  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.1  cegger  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.1  cegger  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.1  cegger  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  cegger  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  cegger  * SUCH DAMAGE.
     29  1.1  cegger  */
     30  1.1  cegger 
     31  1.1  cegger /*
     32  1.1  cegger  * ACPI PCI Bus
     33  1.1  cegger  */
     34  1.1  cegger 
     35  1.1  cegger #include <sys/cdefs.h>
     36  1.6  jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.6 2010/04/22 14:50:31 jruoho Exp $");
     37  1.1  cegger 
     38  1.1  cegger #include <sys/param.h>
     39  1.1  cegger #include <sys/device.h>
     40  1.1  cegger #include <sys/kmem.h>
     41  1.3  jruoho #include <sys/systm.h>
     42  1.1  cegger 
     43  1.6  jruoho #include <dev/pci/pcireg.h>
     44  1.6  jruoho #include <dev/pci/pcidevs.h>
     45  1.6  jruoho #include <dev/pci/ppbreg.h>
     46  1.6  jruoho 
     47  1.1  cegger #include <dev/acpi/acpireg.h>
     48  1.1  cegger #include <dev/acpi/acpivar.h>
     49  1.1  cegger #include <dev/acpi/acpi_pci.h>
     50  1.1  cegger 
     51  1.6  jruoho #define _COMPONENT	ACPI_BUS_COMPONENT
     52  1.6  jruoho ACPI_MODULE_NAME	("acpi_pci")
     53  1.6  jruoho 
     54  1.6  jruoho static ACPI_STATUS	acpi_pcidev_pciroot_bus(ACPI_HANDLE, uint16_t *);
     55  1.6  jruoho static ACPI_STATUS	acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *,
     56  1.6  jruoho 			    void *);
     57  1.6  jruoho static ACPI_STATUS	acpi_pcidev_scan_rec(struct acpi_devnode *);
     58  1.1  cegger 
     59  1.1  cegger 
     60  1.6  jruoho /*
     61  1.6  jruoho  * Regarding PCI Segment Groups, the ACPI spec says (cf. ACPI 4.0, p. 277):
     62  1.6  jruoho  *
     63  1.6  jruoho  * "The optional _SEG object is located under a PCI host bridge and
     64  1.6  jruoho  * evaluates to an integer that describes the PCI Segment Group (see PCI
     65  1.6  jruoho  * Firmware Specification v3.0)."
     66  1.6  jruoho  *
     67  1.6  jruoho  * "PCI Segment Group supports more than 256 buses in a system by allowing
     68  1.6  jruoho  * the reuse of the PCI bus numbers.  Within each PCI Segment Group, the bus
     69  1.6  jruoho  * numbers for the PCI buses must be unique. PCI buses in different PCI
     70  1.6  jruoho  * Segment Group are permitted to have the same bus number."
     71  1.6  jruoho  *
     72  1.6  jruoho  * "If _SEG does not exist, OSPM assumes that all PCI bus segments are in
     73  1.6  jruoho  * PCI Segment Group 0."
     74  1.6  jruoho  *
     75  1.6  jruoho  * "The lower 16 bits of _SEG returned integer is the PCI Segment Group
     76  1.6  jruoho  * number.  Other bits are reserved."
     77  1.6  jruoho  */
     78  1.1  cegger 
     79  1.6  jruoho /*
     80  1.6  jruoho  * Regarding PCI Base Bus Numbers, the ACPI spec says (cf. ACPI 4.0, p. 277):
     81  1.6  jruoho  *
     82  1.6  jruoho  * "For multi-root PCI platforms, the _BBN object evaluates to the PCI bus
     83  1.6  jruoho  * number that the BIOS assigns.  This is needed to access a PCI_Config
     84  1.6  jruoho  * operation region for the specified bus.  The _BBN object is located under
     85  1.6  jruoho  * a PCI host bridge and must be unique for every host bridge within a
     86  1.6  jruoho  * segment since it is the PCI bus number."
     87  1.6  jruoho  *
     88  1.6  jruoho  * Moreover, the ACPI FAQ (http://www.acpi.info/acpi_faq.htm) says:
     89  1.6  jruoho  *
     90  1.6  jruoho  * "For a multiple root bus machine, _BBN is required for each bus.  _BBN
     91  1.6  jruoho  * should provide the bus number assigned to this bus by the BIOS at boot
     92  1.6  jruoho  * time."
     93  1.6  jruoho  */
     94  1.6  jruoho 
     95  1.6  jruoho 
     96  1.6  jruoho /*
     97  1.6  jruoho  * acpi_pcidev_pciroot_bus:
     98  1.6  jruoho  *
     99  1.6  jruoho  *	Derive the PCI bus number of a PCI root bridge from its resources.
    100  1.6  jruoho  *	If successful, return AE_OK and fill *busp.  Otherwise, return an
    101  1.6  jruoho  *	exception code and leave *busp unchanged.
    102  1.6  jruoho  *
    103  1.6  jruoho  * XXX	Use ACPI resource parsing functions (acpi_resource.c) once bus number
    104  1.6  jruoho  *	ranges are implemented there.
    105  1.6  jruoho  */
    106  1.6  jruoho static ACPI_STATUS
    107  1.6  jruoho acpi_pcidev_pciroot_bus(ACPI_HANDLE handle, uint16_t *busp)
    108  1.1  cegger {
    109  1.1  cegger 	ACPI_STATUS rv;
    110  1.6  jruoho 	int32_t bus;
    111  1.6  jruoho 
    112  1.6  jruoho 	bus = -1;
    113  1.6  jruoho 	rv = AcpiWalkResources(handle, METHOD_NAME__CRS,
    114  1.6  jruoho 	    acpi_pcidev_pciroot_bus_callback, &bus);
    115  1.1  cegger 
    116  1.1  cegger 	if (ACPI_FAILURE(rv))
    117  1.6  jruoho 		return rv;
    118  1.6  jruoho 
    119  1.6  jruoho 	if (bus < 0 || bus > 0xFFFF)
    120  1.6  jruoho 		return AE_NOT_EXIST;
    121  1.6  jruoho 
    122  1.6  jruoho 	*busp = (uint16_t)bus;
    123  1.6  jruoho 	return rv;
    124  1.6  jruoho }
    125  1.6  jruoho 
    126  1.6  jruoho static ACPI_STATUS
    127  1.6  jruoho acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *res, void *context)
    128  1.6  jruoho {
    129  1.6  jruoho 	int32_t *bus = context;
    130  1.6  jruoho 	ACPI_RESOURCE_ADDRESS64 addr64;
    131  1.6  jruoho 
    132  1.6  jruoho 	if ((res->Type != ACPI_RESOURCE_TYPE_ADDRESS16) &&
    133  1.6  jruoho 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
    134  1.6  jruoho 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS64))
    135  1.6  jruoho 		return AE_OK;	/* continue the walk */
    136  1.6  jruoho 
    137  1.6  jruoho 	if (ACPI_FAILURE(AcpiResourceToAddress64(res, &addr64)))
    138  1.6  jruoho 		return AE_OK;	/* continue the walk */
    139  1.6  jruoho 
    140  1.6  jruoho 	if (addr64.ResourceType != ACPI_BUS_NUMBER_RANGE)
    141  1.6  jruoho 		return AE_OK;	/* continue the walk */
    142  1.6  jruoho 
    143  1.6  jruoho 	if (*bus != -1)
    144  1.6  jruoho 		return AE_ALREADY_EXISTS;
    145  1.6  jruoho 
    146  1.6  jruoho 	*bus = addr64.Minimum;
    147  1.6  jruoho 	return AE_OK;		/* continue the walk */
    148  1.6  jruoho }
    149  1.6  jruoho 
    150  1.6  jruoho /*
    151  1.6  jruoho  * acpi_pcidev_scan_rec:
    152  1.6  jruoho  *
    153  1.6  jruoho  *	Scan the ACPI device tree for PCI devices.  A node is detected as a
    154  1.6  jruoho  *	PCI device if it has an ancestor that is a PCI root bridge and such
    155  1.6  jruoho  *	that all intermediate nodes are PCI-to-PCI bridges.  Depth-first
    156  1.6  jruoho  *	recursive implementation.
    157  1.6  jruoho  */
    158  1.6  jruoho static ACPI_STATUS
    159  1.6  jruoho acpi_pcidev_scan_rec(struct acpi_devnode *ad)
    160  1.6  jruoho {
    161  1.6  jruoho 	struct acpi_devnode *child;
    162  1.6  jruoho 	struct acpi_pci_info *ap;
    163  1.6  jruoho 	ACPI_INTEGER val;
    164  1.6  jruoho 	ACPI_STATUS rv;
    165  1.6  jruoho 
    166  1.6  jruoho 	if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
    167  1.6  jruoho 	    !(ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
    168  1.6  jruoho 		ad->ad_pciinfo = NULL;
    169  1.6  jruoho 		goto rec;
    170  1.6  jruoho 	}
    171  1.6  jruoho 
    172  1.6  jruoho 	if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) {
    173  1.6  jruoho 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    174  1.6  jruoho 		if (ap == NULL)
    175  1.6  jruoho 			return AE_NO_MEMORY;
    176  1.6  jruoho 
    177  1.6  jruoho 		rv = acpi_eval_integer(ad->ad_handle, METHOD_NAME__SEG, &val);
    178  1.6  jruoho 		if (ACPI_SUCCESS(rv))
    179  1.6  jruoho 			ap->ap_segment = ACPI_LOWORD(val);
    180  1.6  jruoho 		else
    181  1.6  jruoho 			ap->ap_segment = 0;
    182  1.6  jruoho 
    183  1.6  jruoho 		/* try to get bus number using _CRS first */
    184  1.6  jruoho 		rv = acpi_pcidev_pciroot_bus(ad->ad_handle, &ap->ap_bus);
    185  1.6  jruoho 		if (ACPI_FAILURE(rv)) {
    186  1.6  jruoho 			rv = acpi_eval_integer(ad->ad_handle, METHOD_NAME__BBN, &val);
    187  1.6  jruoho 			if (ACPI_SUCCESS(rv))
    188  1.6  jruoho 				ap->ap_bus = ACPI_LOWORD(val);
    189  1.6  jruoho 			else
    190  1.6  jruoho 				ap->ap_bus = 0;
    191  1.6  jruoho 		}
    192  1.6  jruoho 
    193  1.6  jruoho 		ap->ap_device = ACPI_HIWORD(ACPI_LODWORD(ad->ad_devinfo->Address));
    194  1.6  jruoho 		ap->ap_function = ACPI_LOWORD(ACPI_LODWORD(ad->ad_devinfo->Address));
    195  1.6  jruoho 
    196  1.6  jruoho 		ap->ap_bridge = true;
    197  1.6  jruoho 		ap->ap_downbus = ap->ap_bus;
    198  1.6  jruoho 
    199  1.6  jruoho 		ad->ad_pciinfo = ap;
    200  1.6  jruoho 		goto rec;
    201  1.6  jruoho 	}
    202  1.6  jruoho 
    203  1.6  jruoho 	if ((ad->ad_parent != NULL) &&
    204  1.6  jruoho 	    (ad->ad_parent->ad_pciinfo != NULL) &&
    205  1.6  jruoho 	    (ad->ad_parent->ad_pciinfo->ap_bridge)) {
    206  1.6  jruoho 		/*
    207  1.6  jruoho 		 * Our parent is a PCI root bridge or a PCI-to-PCI bridge.  We
    208  1.6  jruoho 		 * have the same PCI segment#, and our bus# is its downstream
    209  1.6  jruoho 		 * bus number.
    210  1.6  jruoho 		 */
    211  1.6  jruoho 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    212  1.6  jruoho 		if (ap == NULL)
    213  1.6  jruoho 			return AE_NO_MEMORY;
    214  1.6  jruoho 
    215  1.6  jruoho 		ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
    216  1.6  jruoho 		ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
    217  1.6  jruoho 		ap->ap_device = ACPI_HIWORD(ACPI_LODWORD(ad->ad_devinfo->Address));
    218  1.6  jruoho 		ap->ap_function = ACPI_LOWORD(ACPI_LODWORD(ad->ad_devinfo->Address));
    219  1.1  cegger 
    220  1.1  cegger 		/*
    221  1.6  jruoho 		 * Check whether this device is a PCI-to-PCI bridge and get its
    222  1.6  jruoho 		 * secondary bus#.
    223  1.1  cegger 		 */
    224  1.6  jruoho 		rv = acpi_pcidev_ppb_downbus(ap->ap_segment, ap->ap_bus,
    225  1.6  jruoho 		    ap->ap_device, ap->ap_function, &ap->ap_downbus);
    226  1.6  jruoho 		if (ACPI_SUCCESS(rv))
    227  1.6  jruoho 			ap->ap_bridge = true;
    228  1.6  jruoho 		else
    229  1.6  jruoho 			ap->ap_bridge = false;
    230  1.6  jruoho 
    231  1.6  jruoho 		ad->ad_pciinfo = ap;
    232  1.6  jruoho 		goto rec;
    233  1.6  jruoho 	}
    234  1.6  jruoho  rec:
    235  1.6  jruoho 	SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
    236  1.6  jruoho 		rv = acpi_pcidev_scan_rec(child);
    237  1.6  jruoho 		if (ACPI_FAILURE(rv))
    238  1.6  jruoho 			return rv;
    239  1.1  cegger 	}
    240  1.1  cegger 
    241  1.6  jruoho 	return AE_OK;
    242  1.6  jruoho }
    243  1.6  jruoho 
    244  1.6  jruoho /*
    245  1.6  jruoho  * acpi_pcidev_ppb_downbus:
    246  1.6  jruoho  *
    247  1.6  jruoho  *	Retrieve the secondary bus number of the PCI-to-PCI bridge having the
    248  1.6  jruoho  *	given PCI id.  If successful, return AE_OK and fill *busp.  Otherwise,
    249  1.6  jruoho  *	return an exception code and leave *busp unchanged.
    250  1.6  jruoho  *
    251  1.6  jruoho  * XXX	Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
    252  1.6  jruoho  */
    253  1.6  jruoho ACPI_STATUS
    254  1.6  jruoho acpi_pcidev_ppb_downbus(uint16_t segment, uint16_t bus, uint16_t device,
    255  1.6  jruoho     uint16_t function, uint16_t *downbus)
    256  1.6  jruoho {
    257  1.6  jruoho 	struct acpi_softc *sc = acpi_softc;
    258  1.6  jruoho 	pci_chipset_tag_t pc;
    259  1.6  jruoho 	pcitag_t tag;
    260  1.6  jruoho 	pcireg_t val;
    261  1.6  jruoho 
    262  1.6  jruoho 	if (bus > 255 || device > 31 || function > 7)
    263  1.6  jruoho 		return AE_BAD_PARAMETER;
    264  1.1  cegger 
    265  1.6  jruoho 	if (sc == NULL)
    266  1.6  jruoho 		pc = NULL;
    267  1.1  cegger 	else
    268  1.6  jruoho 		pc = sc->sc_pc;
    269  1.1  cegger 
    270  1.6  jruoho 	tag = pci_make_tag(pc, bus, device, function);
    271  1.1  cegger 
    272  1.6  jruoho 	/* Check that this device exists. */
    273  1.6  jruoho 	val = pci_conf_read(pc, tag, PCI_ID_REG);
    274  1.6  jruoho 	if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
    275  1.6  jruoho 	    PCI_VENDOR(val) == 0)
    276  1.6  jruoho 		return AE_NOT_EXIST;
    277  1.6  jruoho 
    278  1.6  jruoho 	/* Check that this device is a PCI-to-PCI bridge. */
    279  1.6  jruoho 	val = pci_conf_read(pc, tag, PCI_BHLC_REG);
    280  1.6  jruoho 	if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
    281  1.6  jruoho 		return AE_TYPE;
    282  1.6  jruoho 
    283  1.6  jruoho 	/* This is a PCI-to-PCI bridge.  Get its secondary bus#. */
    284  1.6  jruoho 	val = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
    285  1.6  jruoho 	*downbus = PPB_BUSINFO_SECONDARY(val);
    286  1.6  jruoho 	return AE_OK;
    287  1.1  cegger }
    288  1.1  cegger 
    289  1.1  cegger static void
    290  1.6  jruoho acpi_pcidev_print(struct acpi_devnode *ad)
    291  1.1  cegger {
    292  1.6  jruoho 	aprint_debug(" ");
    293  1.6  jruoho 	if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE)
    294  1.6  jruoho 		aprint_debug("*");
    295  1.6  jruoho 	aprint_debug("%s@%"PRIx16":%"PRIx16":%"PRIx16":%"PRIx16,
    296  1.6  jruoho 	    ad->ad_name,
    297  1.6  jruoho 	    ad->ad_pciinfo->ap_segment,
    298  1.6  jruoho 	    ad->ad_pciinfo->ap_bus,
    299  1.6  jruoho 	    ad->ad_pciinfo->ap_device,
    300  1.6  jruoho 	    ad->ad_pciinfo->ap_function);
    301  1.6  jruoho 	if (ad->ad_pciinfo->ap_bridge)
    302  1.6  jruoho 		aprint_debug(">%"PRIx16, ad->ad_pciinfo->ap_downbus);
    303  1.1  cegger }
    304  1.1  cegger 
    305  1.6  jruoho void
    306  1.1  cegger acpi_pcidev_scan(struct acpi_softc *sc)
    307  1.1  cegger {
    308  1.1  cegger 	struct acpi_devnode *ad;
    309  1.1  cegger 
    310  1.6  jruoho 	acpi_pcidev_scan_rec(sc->sc_root);
    311  1.6  jruoho 	aprint_debug_dev(sc->sc_dev, "pci devices:");
    312  1.5  jruoho 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    313  1.6  jruoho 		if (ad->ad_pciinfo != NULL)
    314  1.6  jruoho 			acpi_pcidev_print(ad);
    315  1.1  cegger 	}
    316  1.1  cegger 	aprint_debug("\n");
    317  1.1  cegger }
    318  1.1  cegger 
    319  1.1  cegger /*
    320  1.1  cegger  * acpi_pcidev_find:
    321  1.1  cegger  *
    322  1.1  cegger  *      Finds a PCI device in the ACPI name space.
    323  1.1  cegger  *      The return status is either:
    324  1.1  cegger  *      - AE_NOT_FOUND if no such device was found.
    325  1.1  cegger  *      - AE_OK if one and only one such device was found.
    326  1.1  cegger  */
    327  1.1  cegger ACPI_STATUS
    328  1.6  jruoho acpi_pcidev_find(uint16_t segment, uint16_t bus, uint16_t device,
    329  1.6  jruoho     uint16_t function, struct acpi_devnode **devnodep)
    330  1.1  cegger {
    331  1.6  jruoho 	struct acpi_softc *sc = acpi_softc;
    332  1.6  jruoho 	struct acpi_devnode *ad;
    333  1.1  cegger 
    334  1.6  jruoho 	if (sc == NULL)
    335  1.6  jruoho 		return AE_NOT_FOUND;
    336  1.1  cegger 
    337  1.6  jruoho 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    338  1.6  jruoho 		if ((ad->ad_pciinfo != NULL) &&
    339  1.6  jruoho 		    (ad->ad_pciinfo->ap_segment == segment) &&
    340  1.6  jruoho 		    (ad->ad_pciinfo->ap_bus == bus) &&
    341  1.6  jruoho 		    (ad->ad_pciinfo->ap_device == device) &&
    342  1.6  jruoho 		    (ad->ad_pciinfo->ap_function == function)) {
    343  1.6  jruoho 			*devnodep = ad;
    344  1.6  jruoho 			return AE_OK;
    345  1.6  jruoho 		}
    346  1.1  cegger 	}
    347  1.6  jruoho 	return AE_NOT_FOUND;
    348  1.1  cegger }
    349