Home | History | Annotate | Line # | Download | only in acpi
acpi_pci.c revision 1.32
      1  1.32   thorpej /* $NetBSD: acpi_pci.c,v 1.32 2021/09/15 17:33:08 thorpej 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 #include <sys/cdefs.h>
     32  1.32   thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.32 2021/09/15 17:33:08 thorpej 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.6    jruoho #include <dev/pci/pcireg.h>
     40  1.17    jruoho #include <dev/pci/pcivar.h>
     41   1.6    jruoho #include <dev/pci/pcidevs.h>
     42   1.6    jruoho #include <dev/pci/ppbreg.h>
     43   1.6    jruoho 
     44  1.32   thorpej #include <dev/pci/pci_calls.h>
     45  1.32   thorpej 
     46   1.1    cegger #include <dev/acpi/acpireg.h>
     47   1.1    cegger #include <dev/acpi/acpivar.h>
     48   1.1    cegger #include <dev/acpi/acpi_pci.h>
     49   1.1    cegger 
     50  1.17    jruoho #include "locators.h"
     51  1.17    jruoho 
     52   1.7    jruoho #define _COMPONENT	  ACPI_BUS_COMPONENT
     53   1.7    jruoho ACPI_MODULE_NAME	  ("acpi_pci")
     54   1.6    jruoho 
     55   1.7    jruoho #define ACPI_HILODWORD(x) ACPI_HIWORD(ACPI_LODWORD((x)))
     56   1.7    jruoho #define ACPI_LOLODWORD(x) ACPI_LOWORD(ACPI_LODWORD((x)))
     57   1.7    jruoho 
     58   1.7    jruoho static ACPI_STATUS	  acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *,
     59   1.7    jruoho 							   void *);
     60   1.1    cegger 
     61   1.6    jruoho /*
     62  1.24  jmcneill  * UUID for _DSM control method, from PCI Firmware Specification.
     63  1.24  jmcneill  */
     64  1.24  jmcneill static UINT8 acpi_pci_dsm_uuid[ACPI_UUID_LENGTH] = {
     65  1.24  jmcneill 	0xd0, 0x37, 0xc9, 0xe5, 0x53, 0x35, 0x7a, 0x4d,
     66  1.24  jmcneill 	0x91, 0x17, 0xea, 0x4d, 0x19, 0xc3, 0x43, 0x4d
     67  1.24  jmcneill };
     68  1.24  jmcneill 
     69  1.24  jmcneill /*
     70  1.11    jruoho  * Regarding PCI Segment Groups (ACPI 4.0, p. 277):
     71   1.6    jruoho  *
     72   1.6    jruoho  * "The optional _SEG object is located under a PCI host bridge and
     73  1.11    jruoho  *  evaluates to an integer that describes the PCI Segment Group (see PCI
     74  1.11    jruoho  *  Firmware Specification v3.0)."
     75  1.11    jruoho  *
     76  1.11    jruoho  * "PCI Segment Group is purely a software concept managed by system
     77  1.11    jruoho  *  firmware and used by OSPM. It is a logical collection of PCI buses
     78  1.11    jruoho  *  (or bus segments). It is a way to logically group the PCI bus segments
     79  1.11    jruoho  *  and PCI Express Hierarchies. _SEG is a level higher than _BBN."
     80   1.6    jruoho  *
     81   1.6    jruoho  * "PCI Segment Group supports more than 256 buses in a system by allowing
     82  1.11    jruoho  *  the reuse of the PCI bus numbers.  Within each PCI Segment Group, the bus
     83  1.11    jruoho  *  numbers for the PCI buses must be unique. PCI buses in different PCI
     84  1.11    jruoho  *  Segment Group are permitted to have the same bus number."
     85   1.6    jruoho  */
     86   1.1    cegger 
     87   1.6    jruoho /*
     88  1.11    jruoho  * Regarding PCI Base Bus Numbers (ACPI 4.0, p. 277):
     89   1.6    jruoho  *
     90   1.6    jruoho  * "For multi-root PCI platforms, the _BBN object evaluates to the PCI bus
     91  1.11    jruoho  *  number that the BIOS assigns.  This is needed to access a PCI_Config
     92  1.11    jruoho  *  operation region for the specified bus.  The _BBN object is located under
     93  1.11    jruoho  *  a PCI host bridge and must be unique for every host bridge within a
     94  1.11    jruoho  *  segment since it is the PCI bus number."
     95   1.6    jruoho  *
     96   1.6    jruoho  * Moreover, the ACPI FAQ (http://www.acpi.info/acpi_faq.htm) says:
     97   1.6    jruoho  *
     98   1.6    jruoho  * "For a multiple root bus machine, _BBN is required for each bus.  _BBN
     99  1.11    jruoho  *  should provide the bus number assigned to this bus by the BIOS at boot
    100  1.11    jruoho  *  time."
    101   1.6    jruoho  */
    102   1.6    jruoho 
    103   1.6    jruoho /*
    104   1.6    jruoho  * acpi_pcidev_pciroot_bus:
    105   1.6    jruoho  *
    106   1.6    jruoho  *	Derive the PCI bus number of a PCI root bridge from its resources.
    107   1.6    jruoho  *	If successful, return AE_OK and fill *busp.  Otherwise, return an
    108   1.6    jruoho  *	exception code and leave *busp unchanged.
    109   1.6    jruoho  */
    110  1.29  jmcneill ACPI_STATUS
    111   1.6    jruoho acpi_pcidev_pciroot_bus(ACPI_HANDLE handle, uint16_t *busp)
    112   1.1    cegger {
    113   1.1    cegger 	ACPI_STATUS rv;
    114   1.6    jruoho 	int32_t bus;
    115   1.6    jruoho 
    116   1.6    jruoho 	bus = -1;
    117  1.11    jruoho 
    118  1.11    jruoho 	/*
    119  1.11    jruoho 	 * XXX:	Use the ACPI resource parsing functions (acpi_resource.c)
    120  1.11    jruoho 	 *	once bus number ranges have been implemented there.
    121  1.11    jruoho 	 */
    122  1.11    jruoho 	rv = AcpiWalkResources(handle, "_CRS",
    123   1.6    jruoho 	    acpi_pcidev_pciroot_bus_callback, &bus);
    124   1.1    cegger 
    125   1.1    cegger 	if (ACPI_FAILURE(rv))
    126   1.6    jruoho 		return rv;
    127   1.6    jruoho 
    128  1.15    gsutre 	if (bus == -1)
    129   1.6    jruoho 		return AE_NOT_EXIST;
    130   1.6    jruoho 
    131  1.15    gsutre 	/* Here it holds that 0 <= bus <= 0xFFFF. */
    132   1.6    jruoho 	*busp = (uint16_t)bus;
    133   1.8    jruoho 
    134   1.6    jruoho 	return rv;
    135   1.6    jruoho }
    136   1.6    jruoho 
    137   1.6    jruoho static ACPI_STATUS
    138   1.6    jruoho acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *res, void *context)
    139   1.6    jruoho {
    140   1.8    jruoho 	ACPI_RESOURCE_ADDRESS64 addr64;
    141   1.6    jruoho 	int32_t *bus = context;
    142   1.6    jruoho 
    143   1.8    jruoho 	/* Always continue the walk by returning AE_OK. */
    144   1.6    jruoho 	if ((res->Type != ACPI_RESOURCE_TYPE_ADDRESS16) &&
    145   1.6    jruoho 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
    146   1.6    jruoho 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS64))
    147   1.8    jruoho 		return AE_OK;
    148   1.6    jruoho 
    149   1.6    jruoho 	if (ACPI_FAILURE(AcpiResourceToAddress64(res, &addr64)))
    150   1.8    jruoho 		return AE_OK;
    151   1.6    jruoho 
    152   1.6    jruoho 	if (addr64.ResourceType != ACPI_BUS_NUMBER_RANGE)
    153   1.8    jruoho 		return AE_OK;
    154   1.6    jruoho 
    155   1.6    jruoho 	if (*bus != -1)
    156   1.6    jruoho 		return AE_ALREADY_EXISTS;
    157   1.6    jruoho 
    158  1.19  christos 	if (addr64.Address.Minimum > 0xFFFF)
    159  1.15    gsutre 		return AE_BAD_DATA;
    160  1.15    gsutre 
    161  1.19  christos 	*bus = (int32_t)addr64.Address.Minimum;
    162   1.8    jruoho 
    163   1.8    jruoho 	return AE_OK;
    164   1.6    jruoho }
    165   1.6    jruoho 
    166   1.6    jruoho /*
    167   1.9    jruoho  * acpi_pcidev_scan:
    168   1.6    jruoho  *
    169   1.6    jruoho  *	Scan the ACPI device tree for PCI devices.  A node is detected as a
    170   1.6    jruoho  *	PCI device if it has an ancestor that is a PCI root bridge and such
    171   1.6    jruoho  *	that all intermediate nodes are PCI-to-PCI bridges.  Depth-first
    172   1.6    jruoho  *	recursive implementation.
    173  1.16    gsutre  *
    174  1.16    gsutre  *	PCI root bridges do not necessarily contain an _ADR, since they already
    175  1.16    gsutre  *	contain an _HID (ACPI 4.0a, p. 197).  However we require an _ADR for
    176  1.16    gsutre  *	all non-root PCI devices.
    177   1.6    jruoho  */
    178   1.9    jruoho ACPI_STATUS
    179   1.9    jruoho acpi_pcidev_scan(struct acpi_devnode *ad)
    180   1.6    jruoho {
    181   1.6    jruoho 	struct acpi_devnode *child;
    182   1.6    jruoho 	struct acpi_pci_info *ap;
    183   1.6    jruoho 	ACPI_INTEGER val;
    184   1.6    jruoho 	ACPI_STATUS rv;
    185   1.6    jruoho 
    186  1.13    gsutre 	ad->ad_pciinfo = NULL;
    187  1.13    gsutre 
    188  1.14    gsutre 	/*
    189  1.14    gsutre 	 * We attach PCI information only to devices that are present,
    190  1.14    gsutre 	 * enabled, and functioning properly.
    191  1.14    gsutre 	 * Note: there is a possible race condition, because _STA may
    192  1.14    gsutre 	 * have changed since ad->ad_devinfo->CurrentStatus was set.
    193  1.14    gsutre 	 */
    194  1.16    gsutre 	if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    195  1.16    gsutre 		goto rec;
    196  1.14    gsutre 
    197  1.22  christos 	if (!acpi_device_present(ad->ad_handle))
    198  1.22  christos 		goto rec;
    199  1.22  christos 
    200   1.6    jruoho 	if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) {
    201   1.8    jruoho 
    202   1.6    jruoho 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    203   1.8    jruoho 
    204  1.11    jruoho 		/*
    205  1.11    jruoho 		 * If no _SEG exist, all PCI bus segments are assumed
    206  1.11    jruoho 		 * to be in the PCI segment group 0 (ACPI 4.0, p. 277).
    207  1.11    jruoho 		 * The segment group number is conveyed in the lower
    208  1.11    jruoho 		 * 16 bits of _SEG (the other bits are all reserved).
    209  1.11    jruoho 		 */
    210   1.8    jruoho 		rv = acpi_eval_integer(ad->ad_handle, "_SEG", &val);
    211   1.8    jruoho 
    212   1.6    jruoho 		if (ACPI_SUCCESS(rv))
    213   1.6    jruoho 			ap->ap_segment = ACPI_LOWORD(val);
    214   1.6    jruoho 
    215  1.16    gsutre 		/* Try to get downstream bus number using _CRS first. */
    216  1.16    gsutre 		rv = acpi_pcidev_pciroot_bus(ad->ad_handle, &ap->ap_downbus);
    217   1.8    jruoho 
    218   1.6    jruoho 		if (ACPI_FAILURE(rv)) {
    219   1.8    jruoho 			rv = acpi_eval_integer(ad->ad_handle, "_BBN", &val);
    220   1.8    jruoho 
    221   1.6    jruoho 			if (ACPI_SUCCESS(rv))
    222  1.16    gsutre 				ap->ap_downbus = ACPI_LOWORD(val);
    223   1.6    jruoho 		}
    224   1.6    jruoho 
    225  1.16    gsutre 		if (ap->ap_downbus > 255) {
    226  1.13    gsutre 			aprint_error_dev(ad->ad_root,
    227  1.16    gsutre 			    "invalid PCI downstream bus for %s\n", ad->ad_name);
    228  1.13    gsutre 			kmem_free(ap, sizeof(*ap));
    229  1.13    gsutre 			goto rec;
    230  1.13    gsutre 		}
    231  1.13    gsutre 
    232  1.16    gsutre 		ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
    233  1.16    gsutre 
    234  1.27  jmcneill 		ap->ap_pc = acpi_get_pci_chipset_tag(acpi_softc, ap->ap_segment, ap->ap_downbus);
    235  1.27  jmcneill 
    236  1.16    gsutre 		/*
    237  1.16    gsutre 		 * This ACPI node denotes a PCI root bridge, but it may also
    238  1.16    gsutre 		 * denote a PCI device on the bridge's downstream bus segment.
    239  1.16    gsutre 		 */
    240  1.16    gsutre 		if (ad->ad_devinfo->Valid & ACPI_VALID_ADR) {
    241  1.16    gsutre 			ap->ap_bus = ap->ap_downbus;
    242  1.16    gsutre 			ap->ap_device =
    243  1.16    gsutre 			    ACPI_HILODWORD(ad->ad_devinfo->Address);
    244  1.16    gsutre 			ap->ap_function =
    245  1.16    gsutre 			    ACPI_LOLODWORD(ad->ad_devinfo->Address);
    246  1.16    gsutre 
    247  1.16    gsutre 			if (ap->ap_device > 31 ||
    248  1.16    gsutre 			    (ap->ap_function > 7 && ap->ap_function != 0xFFFF))
    249  1.16    gsutre 				aprint_error_dev(ad->ad_root,
    250  1.16    gsutre 				    "invalid PCI address for %s\n", ad->ad_name);
    251  1.16    gsutre 			else
    252  1.16    gsutre 				ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
    253  1.16    gsutre 		}
    254   1.6    jruoho 
    255   1.6    jruoho 		ad->ad_pciinfo = ap;
    256   1.8    jruoho 
    257   1.6    jruoho 		goto rec;
    258   1.6    jruoho 	}
    259   1.6    jruoho 
    260   1.6    jruoho 	if ((ad->ad_parent != NULL) &&
    261   1.6    jruoho 	    (ad->ad_parent->ad_pciinfo != NULL) &&
    262  1.16    gsutre 	    (ad->ad_parent->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
    263  1.16    gsutre 	    (ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
    264  1.11    jruoho 
    265   1.6    jruoho 		/*
    266  1.11    jruoho 		 * Our parent is a PCI root bridge or a PCI-to-PCI
    267  1.11    jruoho 		 * bridge. We have the same PCI segment number, and
    268  1.11    jruoho 		 * our bus number is its downstream bus number.
    269   1.6    jruoho 		 */
    270   1.6    jruoho 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    271   1.8    jruoho 
    272  1.27  jmcneill 		ap->ap_pc = ad->ad_parent->ad_pciinfo->ap_pc;
    273   1.6    jruoho 		ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
    274   1.6    jruoho 		ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
    275   1.7    jruoho 
    276   1.7    jruoho 		ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
    277   1.7    jruoho 		ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
    278   1.1    cegger 
    279  1.15    gsutre 		if (ap->ap_device > 31 ||
    280  1.15    gsutre 		    (ap->ap_function > 7 && ap->ap_function != 0xFFFF)) {
    281  1.13    gsutre 			aprint_error_dev(ad->ad_root,
    282  1.13    gsutre 			    "invalid PCI address for %s\n", ad->ad_name);
    283  1.13    gsutre 			kmem_free(ap, sizeof(*ap));
    284  1.13    gsutre 			goto rec;
    285  1.13    gsutre 		}
    286  1.13    gsutre 
    287  1.16    gsutre 		ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
    288  1.16    gsutre 
    289  1.15    gsutre 		if (ap->ap_function == 0xFFFF) {
    290  1.15    gsutre 			/*
    291  1.15    gsutre 			 * Assume that this device is not a PCI-to-PCI bridge.
    292  1.15    gsutre 			 * XXX: Do we need to be smarter?
    293  1.15    gsutre 			 */
    294  1.15    gsutre 		} else {
    295  1.15    gsutre 			/*
    296  1.15    gsutre 			 * Check whether this device is a PCI-to-PCI
    297  1.15    gsutre 			 * bridge and get its secondary bus number.
    298  1.15    gsutre 			 */
    299  1.27  jmcneill 			rv = acpi_pcidev_ppb_downbus(
    300  1.27  jmcneill 			    ad->ad_parent->ad_pciinfo->ap_pc,
    301  1.27  jmcneill 			    ap->ap_segment, ap->ap_bus, ap->ap_device,
    302  1.27  jmcneill 			    ap->ap_function, &ap->ap_downbus);
    303  1.15    gsutre 
    304  1.16    gsutre 			if (ACPI_SUCCESS(rv))
    305  1.16    gsutre 				ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
    306  1.15    gsutre 		}
    307   1.6    jruoho 
    308   1.6    jruoho 		ad->ad_pciinfo = ap;
    309   1.8    jruoho 
    310   1.6    jruoho 		goto rec;
    311   1.6    jruoho 	}
    312   1.9    jruoho 
    313   1.8    jruoho rec:
    314   1.6    jruoho 	SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
    315   1.9    jruoho 		rv = acpi_pcidev_scan(child);
    316   1.8    jruoho 
    317   1.6    jruoho 		if (ACPI_FAILURE(rv))
    318   1.6    jruoho 			return rv;
    319   1.1    cegger 	}
    320   1.1    cegger 
    321   1.6    jruoho 	return AE_OK;
    322   1.6    jruoho }
    323   1.6    jruoho 
    324   1.6    jruoho /*
    325   1.6    jruoho  * acpi_pcidev_ppb_downbus:
    326   1.6    jruoho  *
    327   1.6    jruoho  *	Retrieve the secondary bus number of the PCI-to-PCI bridge having the
    328  1.13    gsutre  *	given PCI id.  If successful, return AE_OK and fill *downbus.
    329  1.13    gsutre  *	Otherwise, return an exception code and leave *downbus unchanged.
    330   1.6    jruoho  *
    331   1.6    jruoho  * XXX	Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
    332   1.6    jruoho  */
    333   1.6    jruoho ACPI_STATUS
    334  1.27  jmcneill acpi_pcidev_ppb_downbus(pci_chipset_tag_t pc, uint16_t segment, uint16_t bus,
    335  1.27  jmcneill     uint16_t device, uint16_t function, uint16_t *downbus)
    336   1.6    jruoho {
    337   1.6    jruoho 	pcitag_t tag;
    338   1.6    jruoho 	pcireg_t val;
    339   1.6    jruoho 
    340   1.6    jruoho 	if (bus > 255 || device > 31 || function > 7)
    341   1.6    jruoho 		return AE_BAD_PARAMETER;
    342   1.1    cegger 
    343   1.6    jruoho 	tag = pci_make_tag(pc, bus, device, function);
    344   1.1    cegger 
    345   1.6    jruoho 	/* Check that this device exists. */
    346   1.6    jruoho 	val = pci_conf_read(pc, tag, PCI_ID_REG);
    347   1.8    jruoho 
    348   1.6    jruoho 	if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
    349   1.6    jruoho 	    PCI_VENDOR(val) == 0)
    350   1.6    jruoho 		return AE_NOT_EXIST;
    351   1.6    jruoho 
    352   1.6    jruoho 	/* Check that this device is a PCI-to-PCI bridge. */
    353   1.6    jruoho 	val = pci_conf_read(pc, tag, PCI_BHLC_REG);
    354   1.8    jruoho 
    355   1.6    jruoho 	if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
    356   1.6    jruoho 		return AE_TYPE;
    357   1.6    jruoho 
    358   1.6    jruoho 	/* This is a PCI-to-PCI bridge.  Get its secondary bus#. */
    359  1.26   msaitoh 	val = pci_conf_read(pc, tag, PCI_BRIDGE_BUS_REG);
    360  1.26   msaitoh 	*downbus = PCI_BRIDGE_BUS_NUM_SECONDARY(val);
    361   1.8    jruoho 
    362   1.6    jruoho 	return AE_OK;
    363   1.1    cegger }
    364   1.1    cegger 
    365   1.1    cegger /*
    366   1.1    cegger  * acpi_pcidev_find:
    367   1.1    cegger  *
    368   1.1    cegger  *      Finds a PCI device in the ACPI name space.
    369  1.10    jruoho  *
    370  1.10    jruoho  *      Returns an ACPI device node on success and NULL on failure.
    371   1.1    cegger  */
    372  1.10    jruoho struct acpi_devnode *
    373  1.10    jruoho acpi_pcidev_find(uint16_t segment, uint16_t bus,
    374  1.10    jruoho     uint16_t device, uint16_t function)
    375   1.1    cegger {
    376   1.6    jruoho 	struct acpi_softc *sc = acpi_softc;
    377   1.6    jruoho 	struct acpi_devnode *ad;
    378   1.1    cegger 
    379   1.6    jruoho 	if (sc == NULL)
    380  1.10    jruoho 		return NULL;
    381   1.1    cegger 
    382   1.6    jruoho 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    383   1.8    jruoho 
    384  1.10    jruoho 		if (ad->ad_pciinfo != NULL &&
    385  1.16    gsutre 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_DEVICE) &&
    386  1.10    jruoho 		    ad->ad_pciinfo->ap_segment == segment &&
    387  1.10    jruoho 		    ad->ad_pciinfo->ap_bus == bus &&
    388  1.10    jruoho 		    ad->ad_pciinfo->ap_device == device &&
    389  1.10    jruoho 		    ad->ad_pciinfo->ap_function == function)
    390  1.10    jruoho 			return ad;
    391   1.1    cegger 	}
    392   1.8    jruoho 
    393  1.10    jruoho 	return NULL;
    394   1.1    cegger }
    395  1.17    jruoho 
    396  1.23  jmcneill /*
    397  1.27  jmcneill  * acpi_pcidev_get_tag:
    398  1.27  jmcneill  *
    399  1.27  jmcneill  *	Returns a PCI chipset tag for a PCI device in the ACPI name space.
    400  1.27  jmcneill  */
    401  1.27  jmcneill pci_chipset_tag_t
    402  1.27  jmcneill acpi_pcidev_get_tag(uint16_t segment, uint16_t bus,
    403  1.27  jmcneill     uint16_t device, uint16_t function)
    404  1.27  jmcneill {
    405  1.27  jmcneill 	struct acpi_devnode *ad;
    406  1.27  jmcneill 
    407  1.27  jmcneill 	ad = acpi_pcidev_find(segment, bus, device, function);
    408  1.27  jmcneill 	if (ad == NULL || ad->ad_pciinfo == NULL)
    409  1.27  jmcneill 		return NULL;
    410  1.27  jmcneill 
    411  1.27  jmcneill 	return ad->ad_pciinfo->ap_pc;
    412  1.27  jmcneill }
    413  1.27  jmcneill 
    414  1.27  jmcneill /*
    415  1.23  jmcneill  * acpi_pciroot_find:
    416  1.23  jmcneill  *
    417  1.23  jmcneill  *	Finds a PCI root bridge in the ACPI name space.
    418  1.23  jmcneill  *
    419  1.23  jmcneill  *	Returns an ACPI device node on success and NULL on failure.
    420  1.23  jmcneill  */
    421  1.23  jmcneill struct acpi_devnode *
    422  1.23  jmcneill acpi_pciroot_find(uint16_t segment, uint16_t bus)
    423  1.23  jmcneill {
    424  1.23  jmcneill 	struct acpi_softc *sc = acpi_softc;
    425  1.23  jmcneill 	struct acpi_devnode *ad;
    426  1.23  jmcneill 
    427  1.23  jmcneill 	if (sc == NULL)
    428  1.23  jmcneill 		return NULL;
    429  1.23  jmcneill 
    430  1.23  jmcneill 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    431  1.23  jmcneill 
    432  1.23  jmcneill 		if (ad->ad_pciinfo != NULL &&
    433  1.23  jmcneill 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
    434  1.23  jmcneill 		    ad->ad_pciinfo->ap_segment == segment &&
    435  1.23  jmcneill 		    ad->ad_pciinfo->ap_bus == bus)
    436  1.23  jmcneill 			return ad;
    437  1.23  jmcneill 	}
    438  1.23  jmcneill 
    439  1.23  jmcneill 	return NULL;
    440  1.23  jmcneill }
    441  1.17    jruoho 
    442  1.17    jruoho /*
    443  1.17    jruoho  * acpi_pcidev_find_dev:
    444  1.17    jruoho  *
    445  1.17    jruoho  *	Returns the device corresponding to the given PCI info, or NULL
    446  1.17    jruoho  *	if it doesn't exist.
    447  1.17    jruoho  */
    448  1.17    jruoho device_t
    449  1.18    jruoho acpi_pcidev_find_dev(struct acpi_devnode *ad)
    450  1.17    jruoho {
    451  1.18    jruoho 	struct acpi_pci_info *ap;
    452  1.17    jruoho 	struct pci_softc *pci;
    453  1.17    jruoho 	device_t dv, pr;
    454  1.17    jruoho 	deviter_t di;
    455  1.17    jruoho 
    456  1.18    jruoho 	if (ad == NULL)
    457  1.18    jruoho 		return NULL;
    458  1.18    jruoho 
    459  1.18    jruoho 	if (ad->ad_pciinfo == NULL)
    460  1.17    jruoho 		return NULL;
    461  1.17    jruoho 
    462  1.18    jruoho 	ap = ad->ad_pciinfo;
    463  1.18    jruoho 
    464  1.17    jruoho 	if (ap->ap_function == 0xFFFF)
    465  1.17    jruoho 		return NULL;
    466  1.17    jruoho 
    467  1.17    jruoho 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    468  1.17    jruoho 	     dv != NULL; dv = deviter_next(&di)) {
    469  1.17    jruoho 
    470  1.17    jruoho 		pr = device_parent(dv);
    471  1.17    jruoho 
    472  1.17    jruoho 		if (pr == NULL || device_is_a(pr, "pci") != true)
    473  1.17    jruoho 			continue;
    474  1.17    jruoho 
    475  1.17    jruoho 		if (dv->dv_locators == NULL)	/* This should not happen. */
    476  1.17    jruoho 			continue;
    477  1.17    jruoho 
    478  1.17    jruoho 		pci = device_private(pr);
    479  1.17    jruoho 
    480  1.17    jruoho 		if (pci->sc_bus == ap->ap_bus &&
    481  1.17    jruoho 		    device_locator(dv, PCICF_DEV) == ap->ap_device &&
    482  1.17    jruoho 		    device_locator(dv, PCICF_FUNCTION) == ap->ap_function)
    483  1.17    jruoho 			break;
    484  1.17    jruoho 	}
    485  1.17    jruoho 
    486  1.17    jruoho 	deviter_release(&di);
    487  1.17    jruoho 
    488  1.17    jruoho 	return dv;
    489  1.17    jruoho }
    490  1.24  jmcneill 
    491  1.24  jmcneill /*
    492  1.24  jmcneill  * acpi_pci_ignore_boot_config:
    493  1.24  jmcneill  *
    494  1.24  jmcneill  *	Returns 1 if the operating system may ignore the boot configuration
    495  1.24  jmcneill  *	of PCI resources.
    496  1.24  jmcneill  */
    497  1.24  jmcneill ACPI_INTEGER
    498  1.24  jmcneill acpi_pci_ignore_boot_config(ACPI_HANDLE handle)
    499  1.24  jmcneill {
    500  1.30   thorpej 	ACPI_OBJECT *pobj = NULL;
    501  1.24  jmcneill 	ACPI_INTEGER ret;
    502  1.24  jmcneill 
    503  1.30   thorpej 	/*
    504  1.30   thorpej 	 * This one is a little confusing, but the result of
    505  1.30   thorpej 	 * evaluating _DSM #5 is:
    506  1.30   thorpej 	 *
    507  1.30   thorpej 	 * 0: The operating system may not ignore the boot configuration
    508  1.30   thorpej 	 *    of PCI resources.
    509  1.30   thorpej 	 *
    510  1.30   thorpej 	 * 1: The operating system may ignore the boot configuration of
    511  1.30   thorpej 	 *    PCI resources, and reconfigure or rebalance these resources
    512  1.30   thorpej 	 *    in the hierarchy as required.
    513  1.30   thorpej 	 */
    514  1.24  jmcneill 
    515  1.30   thorpej 	if (ACPI_FAILURE(acpi_dsm(handle, acpi_pci_dsm_uuid,
    516  1.30   thorpej 				  1, 5, NULL, &pobj))) {
    517  1.30   thorpej 		/*
    518  1.30   thorpej 		 * In the absence of _DSM #5, we may assume that the
    519  1.30   thorpej 		 * boot config can be ignored.
    520  1.30   thorpej 		 */
    521  1.28  jmcneill 		return 1;
    522  1.30   thorpej 	}
    523  1.24  jmcneill 
    524  1.30   thorpej 	/*
    525  1.30   thorpej 	 * ...and we default to "may ignore" in the event that the
    526  1.30   thorpej 	 * method returns nonsense.
    527  1.30   thorpej 	 */
    528  1.28  jmcneill 	ret = 1;
    529  1.24  jmcneill 
    530  1.30   thorpej 	if (pobj != NULL) {
    531  1.30   thorpej 		switch (pobj->Type) {
    532  1.30   thorpej 		case ACPI_TYPE_INTEGER:
    533  1.30   thorpej 			ret = pobj->Integer.Value;
    534  1.30   thorpej 			break;
    535  1.30   thorpej 
    536  1.30   thorpej 		case ACPI_TYPE_PACKAGE:
    537  1.30   thorpej 			if (pobj->Package.Count == 1 &&
    538  1.30   thorpej 			    pobj->Package.Elements[0].Type == ACPI_TYPE_INTEGER)
    539  1.30   thorpej 				ret = pobj->Package.Elements[0].Integer.Value;
    540  1.30   thorpej 			break;
    541  1.30   thorpej 		}
    542  1.30   thorpej 		ACPI_FREE(pobj);
    543  1.24  jmcneill 	}
    544  1.24  jmcneill 
    545  1.24  jmcneill 	return ret;
    546  1.24  jmcneill }
    547  1.31   thorpej 
    548  1.31   thorpej /*
    549  1.31   thorpej  * acpi_pci_bus_get_child_devhandle:
    550  1.31   thorpej  *
    551  1.31   thorpej  *	Implements the "pci-bus-get-child-devhandle" device call for
    552  1.31   thorpej  *	ACPI device handles
    553  1.31   thorpej  */
    554  1.31   thorpej static int
    555  1.31   thorpej acpi_pci_bus_get_child_devhandle(device_t dev, devhandle_t call_handle, void *v)
    556  1.31   thorpej {
    557  1.31   thorpej 	struct pci_bus_get_child_devhandle_args *args = v;
    558  1.31   thorpej 	struct acpi_devnode *ad;
    559  1.31   thorpej 	ACPI_HANDLE hdl;
    560  1.31   thorpej 	int b, d, f;
    561  1.31   thorpej 	u_int segment;
    562  1.31   thorpej 
    563  1.31   thorpej #ifdef __HAVE_PCI_GET_SEGMENT
    564  1.31   thorpej 	segment = pci_get_segment(args->pc);
    565  1.31   thorpej #else
    566  1.31   thorpej 	segment = 0;
    567  1.31   thorpej #endif /* __HAVE_PCI_GET_SEGMENT */
    568  1.31   thorpej 
    569  1.31   thorpej 	pci_decompose_tag(args->pc, args->tag, &b, &d, &f);
    570  1.31   thorpej 
    571  1.31   thorpej 	ad = acpi_pcidev_find(segment, b, d, f);
    572  1.31   thorpej 
    573  1.31   thorpej 	if (ad != NULL && (hdl = ad->ad_handle) != NULL) {
    574  1.31   thorpej 		/* Found it! */
    575  1.31   thorpej 		args->devhandle = devhandle_from_acpi(hdl);
    576  1.31   thorpej 		return 0;
    577  1.31   thorpej 	}
    578  1.31   thorpej 
    579  1.31   thorpej 	return ENODEV;
    580  1.31   thorpej }
    581  1.32   thorpej ACPI_DEVICE_CALL_REGISTER(PCI_BUS_GET_CHILD_DEVHANDLE_STR,
    582  1.31   thorpej 			  acpi_pci_bus_get_child_devhandle)
    583