Home | History | Annotate | Line # | Download | only in acpi
acpi_pci.c revision 1.4.4.3
      1  1.4.4.3   rmind /* $NetBSD: acpi_pci.c,v 1.4.4.3 2011/03/05 20:53:03 rmind Exp $ */
      2      1.1  cegger 
      3      1.1  cegger /*
      4  1.4.4.1   rmind  * 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.4.4.1   rmind  * 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 #include <sys/cdefs.h>
     32  1.4.4.3   rmind __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.4.4.3 2011/03/05 20:53:03 rmind Exp $");
     33      1.1  cegger 
     34      1.1  cegger #include <sys/param.h>
     35      1.1  cegger #include <sys/device.h>
     36      1.1  cegger #include <sys/kmem.h>
     37      1.3  jruoho #include <sys/systm.h>
     38      1.1  cegger 
     39  1.4.4.1   rmind #include <dev/pci/pcireg.h>
     40  1.4.4.3   rmind #include <dev/pci/pcivar.h>
     41  1.4.4.1   rmind #include <dev/pci/pcidevs.h>
     42  1.4.4.1   rmind #include <dev/pci/ppbreg.h>
     43  1.4.4.1   rmind 
     44      1.1  cegger #include <dev/acpi/acpireg.h>
     45      1.1  cegger #include <dev/acpi/acpivar.h>
     46      1.1  cegger #include <dev/acpi/acpi_pci.h>
     47      1.1  cegger 
     48  1.4.4.3   rmind #include "locators.h"
     49  1.4.4.3   rmind 
     50  1.4.4.1   rmind #define _COMPONENT	  ACPI_BUS_COMPONENT
     51  1.4.4.1   rmind ACPI_MODULE_NAME	  ("acpi_pci")
     52  1.4.4.1   rmind 
     53  1.4.4.1   rmind #define ACPI_HILODWORD(x) ACPI_HIWORD(ACPI_LODWORD((x)))
     54  1.4.4.1   rmind #define ACPI_LOLODWORD(x) ACPI_LOWORD(ACPI_LODWORD((x)))
     55  1.4.4.1   rmind 
     56  1.4.4.1   rmind static ACPI_STATUS	  acpi_pcidev_pciroot_bus(ACPI_HANDLE, uint16_t *);
     57  1.4.4.1   rmind static ACPI_STATUS	  acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *,
     58  1.4.4.1   rmind 							   void *);
     59      1.1  cegger 
     60  1.4.4.1   rmind /*
     61  1.4.4.1   rmind  * Regarding PCI Segment Groups (ACPI 4.0, p. 277):
     62  1.4.4.1   rmind  *
     63  1.4.4.1   rmind  * "The optional _SEG object is located under a PCI host bridge and
     64  1.4.4.1   rmind  *  evaluates to an integer that describes the PCI Segment Group (see PCI
     65  1.4.4.1   rmind  *  Firmware Specification v3.0)."
     66  1.4.4.1   rmind  *
     67  1.4.4.1   rmind  * "PCI Segment Group is purely a software concept managed by system
     68  1.4.4.1   rmind  *  firmware and used by OSPM. It is a logical collection of PCI buses
     69  1.4.4.1   rmind  *  (or bus segments). It is a way to logically group the PCI bus segments
     70  1.4.4.1   rmind  *  and PCI Express Hierarchies. _SEG is a level higher than _BBN."
     71  1.4.4.1   rmind  *
     72  1.4.4.1   rmind  * "PCI Segment Group supports more than 256 buses in a system by allowing
     73  1.4.4.1   rmind  *  the reuse of the PCI bus numbers.  Within each PCI Segment Group, the bus
     74  1.4.4.1   rmind  *  numbers for the PCI buses must be unique. PCI buses in different PCI
     75  1.4.4.1   rmind  *  Segment Group are permitted to have the same bus number."
     76  1.4.4.1   rmind  */
     77      1.1  cegger 
     78  1.4.4.1   rmind /*
     79  1.4.4.1   rmind  * Regarding PCI Base Bus Numbers (ACPI 4.0, p. 277):
     80  1.4.4.1   rmind  *
     81  1.4.4.1   rmind  * "For multi-root PCI platforms, the _BBN object evaluates to the PCI bus
     82  1.4.4.1   rmind  *  number that the BIOS assigns.  This is needed to access a PCI_Config
     83  1.4.4.1   rmind  *  operation region for the specified bus.  The _BBN object is located under
     84  1.4.4.1   rmind  *  a PCI host bridge and must be unique for every host bridge within a
     85  1.4.4.1   rmind  *  segment since it is the PCI bus number."
     86  1.4.4.1   rmind  *
     87  1.4.4.1   rmind  * Moreover, the ACPI FAQ (http://www.acpi.info/acpi_faq.htm) says:
     88  1.4.4.1   rmind  *
     89  1.4.4.1   rmind  * "For a multiple root bus machine, _BBN is required for each bus.  _BBN
     90  1.4.4.1   rmind  *  should provide the bus number assigned to this bus by the BIOS at boot
     91  1.4.4.1   rmind  *  time."
     92  1.4.4.1   rmind  */
     93      1.1  cegger 
     94  1.4.4.1   rmind /*
     95  1.4.4.1   rmind  * acpi_pcidev_pciroot_bus:
     96  1.4.4.1   rmind  *
     97  1.4.4.1   rmind  *	Derive the PCI bus number of a PCI root bridge from its resources.
     98  1.4.4.1   rmind  *	If successful, return AE_OK and fill *busp.  Otherwise, return an
     99  1.4.4.1   rmind  *	exception code and leave *busp unchanged.
    100  1.4.4.1   rmind  */
    101  1.4.4.1   rmind static ACPI_STATUS
    102  1.4.4.1   rmind acpi_pcidev_pciroot_bus(ACPI_HANDLE handle, uint16_t *busp)
    103      1.1  cegger {
    104      1.1  cegger 	ACPI_STATUS rv;
    105  1.4.4.1   rmind 	int32_t bus;
    106  1.4.4.1   rmind 
    107  1.4.4.1   rmind 	bus = -1;
    108      1.1  cegger 
    109      1.1  cegger 	/*
    110  1.4.4.1   rmind 	 * XXX:	Use the ACPI resource parsing functions (acpi_resource.c)
    111  1.4.4.1   rmind 	 *	once bus number ranges have been implemented there.
    112      1.1  cegger 	 */
    113  1.4.4.1   rmind 	rv = AcpiWalkResources(handle, "_CRS",
    114  1.4.4.1   rmind 	    acpi_pcidev_pciroot_bus_callback, &bus);
    115  1.4.4.1   rmind 
    116      1.1  cegger 	if (ACPI_FAILURE(rv))
    117  1.4.4.1   rmind 		return rv;
    118  1.4.4.1   rmind 
    119  1.4.4.3   rmind 	if (bus == -1)
    120  1.4.4.1   rmind 		return AE_NOT_EXIST;
    121  1.4.4.1   rmind 
    122  1.4.4.3   rmind 	/* Here it holds that 0 <= bus <= 0xFFFF. */
    123  1.4.4.1   rmind 	*busp = (uint16_t)bus;
    124  1.4.4.1   rmind 
    125  1.4.4.1   rmind 	return rv;
    126  1.4.4.1   rmind }
    127  1.4.4.1   rmind 
    128  1.4.4.1   rmind static ACPI_STATUS
    129  1.4.4.1   rmind acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *res, void *context)
    130  1.4.4.1   rmind {
    131  1.4.4.1   rmind 	ACPI_RESOURCE_ADDRESS64 addr64;
    132  1.4.4.1   rmind 	int32_t *bus = context;
    133  1.4.4.1   rmind 
    134  1.4.4.1   rmind 	/* Always continue the walk by returning AE_OK. */
    135  1.4.4.1   rmind 	if ((res->Type != ACPI_RESOURCE_TYPE_ADDRESS16) &&
    136  1.4.4.1   rmind 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
    137  1.4.4.1   rmind 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS64))
    138  1.4.4.1   rmind 		return AE_OK;
    139  1.4.4.1   rmind 
    140  1.4.4.1   rmind 	if (ACPI_FAILURE(AcpiResourceToAddress64(res, &addr64)))
    141  1.4.4.1   rmind 		return AE_OK;
    142  1.4.4.1   rmind 
    143  1.4.4.1   rmind 	if (addr64.ResourceType != ACPI_BUS_NUMBER_RANGE)
    144  1.4.4.1   rmind 		return AE_OK;
    145  1.4.4.1   rmind 
    146  1.4.4.1   rmind 	if (*bus != -1)
    147  1.4.4.1   rmind 		return AE_ALREADY_EXISTS;
    148  1.4.4.1   rmind 
    149  1.4.4.3   rmind 	if (addr64.Minimum > 0xFFFF)
    150  1.4.4.3   rmind 		return AE_BAD_DATA;
    151  1.4.4.3   rmind 
    152  1.4.4.3   rmind 	*bus = (int32_t)addr64.Minimum;
    153  1.4.4.1   rmind 
    154  1.4.4.1   rmind 	return AE_OK;
    155  1.4.4.1   rmind }
    156  1.4.4.1   rmind 
    157  1.4.4.1   rmind /*
    158  1.4.4.1   rmind  * acpi_pcidev_scan:
    159  1.4.4.1   rmind  *
    160  1.4.4.1   rmind  *	Scan the ACPI device tree for PCI devices.  A node is detected as a
    161  1.4.4.1   rmind  *	PCI device if it has an ancestor that is a PCI root bridge and such
    162  1.4.4.1   rmind  *	that all intermediate nodes are PCI-to-PCI bridges.  Depth-first
    163  1.4.4.1   rmind  *	recursive implementation.
    164  1.4.4.3   rmind  *
    165  1.4.4.3   rmind  *	PCI root bridges do not necessarily contain an _ADR, since they already
    166  1.4.4.3   rmind  *	contain an _HID (ACPI 4.0a, p. 197).  However we require an _ADR for
    167  1.4.4.3   rmind  *	all non-root PCI devices.
    168  1.4.4.1   rmind  */
    169  1.4.4.1   rmind ACPI_STATUS
    170  1.4.4.1   rmind acpi_pcidev_scan(struct acpi_devnode *ad)
    171  1.4.4.1   rmind {
    172  1.4.4.1   rmind 	struct acpi_devnode *child;
    173  1.4.4.1   rmind 	struct acpi_pci_info *ap;
    174  1.4.4.1   rmind 	ACPI_INTEGER val;
    175  1.4.4.1   rmind 	ACPI_STATUS rv;
    176  1.4.4.1   rmind 
    177  1.4.4.3   rmind 	ad->ad_pciinfo = NULL;
    178  1.4.4.3   rmind 
    179  1.4.4.3   rmind 	/*
    180  1.4.4.3   rmind 	 * We attach PCI information only to devices that are present,
    181  1.4.4.3   rmind 	 * enabled, and functioning properly.
    182  1.4.4.3   rmind 	 * Note: there is a possible race condition, because _STA may
    183  1.4.4.3   rmind 	 * have changed since ad->ad_devinfo->CurrentStatus was set.
    184  1.4.4.3   rmind 	 */
    185  1.4.4.3   rmind 	if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    186  1.4.4.3   rmind 		goto rec;
    187  1.4.4.3   rmind 	if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) != 0 &&
    188  1.4.4.3   rmind 	    (ad->ad_devinfo->CurrentStatus & ACPI_STA_OK) != ACPI_STA_OK)
    189  1.4.4.1   rmind 		goto rec;
    190  1.4.4.1   rmind 
    191  1.4.4.1   rmind 	if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) {
    192  1.4.4.1   rmind 
    193  1.4.4.1   rmind 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    194  1.4.4.1   rmind 
    195  1.4.4.1   rmind 		if (ap == NULL)
    196  1.4.4.1   rmind 			return AE_NO_MEMORY;
    197      1.1  cegger 
    198      1.1  cegger 		/*
    199  1.4.4.1   rmind 		 * If no _SEG exist, all PCI bus segments are assumed
    200  1.4.4.1   rmind 		 * to be in the PCI segment group 0 (ACPI 4.0, p. 277).
    201  1.4.4.1   rmind 		 * The segment group number is conveyed in the lower
    202  1.4.4.1   rmind 		 * 16 bits of _SEG (the other bits are all reserved).
    203      1.1  cegger 		 */
    204  1.4.4.1   rmind 		rv = acpi_eval_integer(ad->ad_handle, "_SEG", &val);
    205  1.4.4.1   rmind 
    206  1.4.4.1   rmind 		if (ACPI_SUCCESS(rv))
    207  1.4.4.1   rmind 			ap->ap_segment = ACPI_LOWORD(val);
    208  1.4.4.1   rmind 
    209  1.4.4.3   rmind 		/* Try to get downstream bus number using _CRS first. */
    210  1.4.4.3   rmind 		rv = acpi_pcidev_pciroot_bus(ad->ad_handle, &ap->ap_downbus);
    211  1.4.4.1   rmind 
    212  1.4.4.1   rmind 		if (ACPI_FAILURE(rv)) {
    213  1.4.4.1   rmind 			rv = acpi_eval_integer(ad->ad_handle, "_BBN", &val);
    214      1.1  cegger 
    215  1.4.4.1   rmind 			if (ACPI_SUCCESS(rv))
    216  1.4.4.3   rmind 				ap->ap_downbus = ACPI_LOWORD(val);
    217  1.4.4.1   rmind 		}
    218  1.4.4.1   rmind 
    219  1.4.4.3   rmind 		if (ap->ap_downbus > 255) {
    220  1.4.4.3   rmind 			aprint_error_dev(ad->ad_root,
    221  1.4.4.3   rmind 			    "invalid PCI downstream bus for %s\n", ad->ad_name);
    222  1.4.4.3   rmind 			kmem_free(ap, sizeof(*ap));
    223  1.4.4.3   rmind 			goto rec;
    224  1.4.4.3   rmind 		}
    225  1.4.4.3   rmind 
    226  1.4.4.3   rmind 		ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
    227  1.4.4.1   rmind 
    228  1.4.4.3   rmind 		/*
    229  1.4.4.3   rmind 		 * This ACPI node denotes a PCI root bridge, but it may also
    230  1.4.4.3   rmind 		 * denote a PCI device on the bridge's downstream bus segment.
    231  1.4.4.3   rmind 		 */
    232  1.4.4.3   rmind 		if (ad->ad_devinfo->Valid & ACPI_VALID_ADR) {
    233  1.4.4.3   rmind 			ap->ap_bus = ap->ap_downbus;
    234  1.4.4.3   rmind 			ap->ap_device =
    235  1.4.4.3   rmind 			    ACPI_HILODWORD(ad->ad_devinfo->Address);
    236  1.4.4.3   rmind 			ap->ap_function =
    237  1.4.4.3   rmind 			    ACPI_LOLODWORD(ad->ad_devinfo->Address);
    238  1.4.4.3   rmind 
    239  1.4.4.3   rmind 			if (ap->ap_device > 31 ||
    240  1.4.4.3   rmind 			    (ap->ap_function > 7 && ap->ap_function != 0xFFFF))
    241  1.4.4.3   rmind 				aprint_error_dev(ad->ad_root,
    242  1.4.4.3   rmind 				    "invalid PCI address for %s\n", ad->ad_name);
    243  1.4.4.3   rmind 			else
    244  1.4.4.3   rmind 				ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
    245  1.4.4.3   rmind 		}
    246  1.4.4.1   rmind 
    247  1.4.4.1   rmind 		ad->ad_pciinfo = ap;
    248  1.4.4.1   rmind 
    249  1.4.4.1   rmind 		goto rec;
    250      1.1  cegger 	}
    251      1.1  cegger 
    252  1.4.4.1   rmind 	if ((ad->ad_parent != NULL) &&
    253  1.4.4.1   rmind 	    (ad->ad_parent->ad_pciinfo != NULL) &&
    254  1.4.4.3   rmind 	    (ad->ad_parent->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
    255  1.4.4.3   rmind 	    (ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
    256      1.1  cegger 
    257  1.4.4.1   rmind 		/*
    258  1.4.4.1   rmind 		 * Our parent is a PCI root bridge or a PCI-to-PCI
    259  1.4.4.1   rmind 		 * bridge. We have the same PCI segment number, and
    260  1.4.4.1   rmind 		 * our bus number is its downstream bus number.
    261  1.4.4.1   rmind 		 */
    262  1.4.4.1   rmind 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    263      1.1  cegger 
    264  1.4.4.1   rmind 		if (ap == NULL)
    265  1.4.4.1   rmind 			return AE_NO_MEMORY;
    266      1.1  cegger 
    267  1.4.4.1   rmind 		ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
    268  1.4.4.1   rmind 		ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
    269      1.1  cegger 
    270  1.4.4.1   rmind 		ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
    271  1.4.4.1   rmind 		ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
    272  1.4.4.1   rmind 
    273  1.4.4.3   rmind 		if (ap->ap_device > 31 ||
    274  1.4.4.3   rmind 		    (ap->ap_function > 7 && ap->ap_function != 0xFFFF)) {
    275  1.4.4.3   rmind 			aprint_error_dev(ad->ad_root,
    276  1.4.4.3   rmind 			    "invalid PCI address for %s\n", ad->ad_name);
    277  1.4.4.3   rmind 			kmem_free(ap, sizeof(*ap));
    278  1.4.4.3   rmind 			goto rec;
    279  1.4.4.3   rmind 		}
    280  1.4.4.3   rmind 
    281  1.4.4.3   rmind 		ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
    282  1.4.4.3   rmind 
    283  1.4.4.3   rmind 		if (ap->ap_function == 0xFFFF) {
    284  1.4.4.3   rmind 			/*
    285  1.4.4.3   rmind 			 * Assume that this device is not a PCI-to-PCI bridge.
    286  1.4.4.3   rmind 			 * XXX: Do we need to be smarter?
    287  1.4.4.3   rmind 			 */
    288  1.4.4.3   rmind 		} else {
    289  1.4.4.3   rmind 			/*
    290  1.4.4.3   rmind 			 * Check whether this device is a PCI-to-PCI
    291  1.4.4.3   rmind 			 * bridge and get its secondary bus number.
    292  1.4.4.3   rmind 			 */
    293  1.4.4.3   rmind 			rv = acpi_pcidev_ppb_downbus(ap->ap_segment, ap->ap_bus,
    294  1.4.4.3   rmind 			    ap->ap_device, ap->ap_function, &ap->ap_downbus);
    295  1.4.4.3   rmind 
    296  1.4.4.3   rmind 			if (ACPI_SUCCESS(rv))
    297  1.4.4.3   rmind 				ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
    298  1.4.4.3   rmind 		}
    299  1.4.4.1   rmind 
    300  1.4.4.1   rmind 		ad->ad_pciinfo = ap;
    301  1.4.4.1   rmind 
    302  1.4.4.1   rmind 		goto rec;
    303  1.4.4.1   rmind 	}
    304  1.4.4.1   rmind 
    305  1.4.4.1   rmind rec:
    306  1.4.4.1   rmind 	SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
    307  1.4.4.1   rmind 		rv = acpi_pcidev_scan(child);
    308  1.4.4.1   rmind 
    309  1.4.4.1   rmind 		if (ACPI_FAILURE(rv))
    310  1.4.4.1   rmind 			return rv;
    311  1.4.4.1   rmind 	}
    312  1.4.4.1   rmind 
    313  1.4.4.1   rmind 	return AE_OK;
    314      1.1  cegger }
    315      1.1  cegger 
    316  1.4.4.1   rmind /*
    317  1.4.4.1   rmind  * acpi_pcidev_ppb_downbus:
    318  1.4.4.1   rmind  *
    319  1.4.4.1   rmind  *	Retrieve the secondary bus number of the PCI-to-PCI bridge having the
    320  1.4.4.3   rmind  *	given PCI id.  If successful, return AE_OK and fill *downbus.
    321  1.4.4.3   rmind  *	Otherwise, return an exception code and leave *downbus unchanged.
    322  1.4.4.1   rmind  *
    323  1.4.4.1   rmind  * XXX	Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
    324  1.4.4.1   rmind  */
    325  1.4.4.1   rmind ACPI_STATUS
    326  1.4.4.1   rmind acpi_pcidev_ppb_downbus(uint16_t segment, uint16_t bus, uint16_t device,
    327  1.4.4.1   rmind     uint16_t function, uint16_t *downbus)
    328      1.1  cegger {
    329  1.4.4.1   rmind 	struct acpi_softc *sc = acpi_softc;
    330  1.4.4.1   rmind 	pci_chipset_tag_t pc;
    331  1.4.4.1   rmind 	pcitag_t tag;
    332  1.4.4.1   rmind 	pcireg_t val;
    333      1.1  cegger 
    334  1.4.4.1   rmind 	if (bus > 255 || device > 31 || function > 7)
    335  1.4.4.1   rmind 		return AE_BAD_PARAMETER;
    336      1.1  cegger 
    337  1.4.4.2   rmind 	pc = sc->sc_pc;
    338      1.4  jruoho 
    339  1.4.4.1   rmind 	tag = pci_make_tag(pc, bus, device, function);
    340      1.4  jruoho 
    341  1.4.4.1   rmind 	/* Check that this device exists. */
    342  1.4.4.1   rmind 	val = pci_conf_read(pc, tag, PCI_ID_REG);
    343      1.4  jruoho 
    344  1.4.4.1   rmind 	if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
    345  1.4.4.1   rmind 	    PCI_VENDOR(val) == 0)
    346  1.4.4.1   rmind 		return AE_NOT_EXIST;
    347      1.1  cegger 
    348  1.4.4.1   rmind 	/* Check that this device is a PCI-to-PCI bridge. */
    349  1.4.4.1   rmind 	val = pci_conf_read(pc, tag, PCI_BHLC_REG);
    350      1.1  cegger 
    351  1.4.4.1   rmind 	if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
    352  1.4.4.1   rmind 		return AE_TYPE;
    353      1.1  cegger 
    354  1.4.4.1   rmind 	/* This is a PCI-to-PCI bridge.  Get its secondary bus#. */
    355  1.4.4.1   rmind 	val = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
    356  1.4.4.1   rmind 	*downbus = PPB_BUSINFO_SECONDARY(val);
    357      1.1  cegger 
    358  1.4.4.1   rmind 	return AE_OK;
    359      1.1  cegger }
    360      1.1  cegger 
    361      1.1  cegger /*
    362      1.1  cegger  * acpi_pcidev_find:
    363      1.1  cegger  *
    364      1.1  cegger  *      Finds a PCI device in the ACPI name space.
    365  1.4.4.1   rmind  *
    366  1.4.4.1   rmind  *      Returns an ACPI device node on success and NULL on failure.
    367      1.1  cegger  */
    368  1.4.4.1   rmind struct acpi_devnode *
    369  1.4.4.1   rmind acpi_pcidev_find(uint16_t segment, uint16_t bus,
    370  1.4.4.1   rmind     uint16_t device, uint16_t function)
    371      1.1  cegger {
    372  1.4.4.1   rmind 	struct acpi_softc *sc = acpi_softc;
    373  1.4.4.1   rmind 	struct acpi_devnode *ad;
    374  1.4.4.1   rmind 
    375  1.4.4.1   rmind 	if (sc == NULL)
    376  1.4.4.1   rmind 		return NULL;
    377      1.1  cegger 
    378  1.4.4.1   rmind 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    379      1.1  cegger 
    380  1.4.4.1   rmind 		if (ad->ad_pciinfo != NULL &&
    381  1.4.4.3   rmind 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_DEVICE) &&
    382  1.4.4.1   rmind 		    ad->ad_pciinfo->ap_segment == segment &&
    383  1.4.4.1   rmind 		    ad->ad_pciinfo->ap_bus == bus &&
    384  1.4.4.1   rmind 		    ad->ad_pciinfo->ap_device == device &&
    385  1.4.4.1   rmind 		    ad->ad_pciinfo->ap_function == function)
    386  1.4.4.1   rmind 			return ad;
    387      1.1  cegger 	}
    388      1.1  cegger 
    389  1.4.4.1   rmind 	return NULL;
    390      1.1  cegger }
    391  1.4.4.3   rmind 
    392  1.4.4.3   rmind 
    393  1.4.4.3   rmind /*
    394  1.4.4.3   rmind  * acpi_pcidev_find_dev:
    395  1.4.4.3   rmind  *
    396  1.4.4.3   rmind  *	Returns the device corresponding to the given PCI info, or NULL
    397  1.4.4.3   rmind  *	if it doesn't exist.
    398  1.4.4.3   rmind  */
    399  1.4.4.3   rmind device_t
    400  1.4.4.3   rmind acpi_pcidev_find_dev(struct acpi_devnode *ad)
    401  1.4.4.3   rmind {
    402  1.4.4.3   rmind 	struct acpi_pci_info *ap;
    403  1.4.4.3   rmind 	struct pci_softc *pci;
    404  1.4.4.3   rmind 	device_t dv, pr;
    405  1.4.4.3   rmind 	deviter_t di;
    406  1.4.4.3   rmind 
    407  1.4.4.3   rmind 	if (ad == NULL)
    408  1.4.4.3   rmind 		return NULL;
    409  1.4.4.3   rmind 
    410  1.4.4.3   rmind 	if (ad->ad_pciinfo == NULL)
    411  1.4.4.3   rmind 		return NULL;
    412  1.4.4.3   rmind 
    413  1.4.4.3   rmind 	ap = ad->ad_pciinfo;
    414  1.4.4.3   rmind 
    415  1.4.4.3   rmind 	if (ap->ap_function == 0xFFFF)
    416  1.4.4.3   rmind 		return NULL;
    417  1.4.4.3   rmind 
    418  1.4.4.3   rmind 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    419  1.4.4.3   rmind 	     dv != NULL; dv = deviter_next(&di)) {
    420  1.4.4.3   rmind 
    421  1.4.4.3   rmind 		pr = device_parent(dv);
    422  1.4.4.3   rmind 
    423  1.4.4.3   rmind 		if (pr == NULL || device_is_a(pr, "pci") != true)
    424  1.4.4.3   rmind 			continue;
    425  1.4.4.3   rmind 
    426  1.4.4.3   rmind 		if (dv->dv_locators == NULL)	/* This should not happen. */
    427  1.4.4.3   rmind 			continue;
    428  1.4.4.3   rmind 
    429  1.4.4.3   rmind 		pci = device_private(pr);
    430  1.4.4.3   rmind 
    431  1.4.4.3   rmind 		if (pci->sc_bus == ap->ap_bus &&
    432  1.4.4.3   rmind 		    device_locator(dv, PCICF_DEV) == ap->ap_device &&
    433  1.4.4.3   rmind 		    device_locator(dv, PCICF_FUNCTION) == ap->ap_function)
    434  1.4.4.3   rmind 			break;
    435  1.4.4.3   rmind 	}
    436  1.4.4.3   rmind 
    437  1.4.4.3   rmind 	deviter_release(&di);
    438  1.4.4.3   rmind 
    439  1.4.4.3   rmind 	return dv;
    440  1.4.4.3   rmind }
    441