Home | History | Annotate | Line # | Download | only in acpi
acpi_pci.c revision 1.20.8.4
      1  1.20.8.4  pgoyette /* $NetBSD: acpi_pci.c,v 1.20.8.4 2018/11/26 01:52:30 pgoyette Exp $ */
      2       1.1    cegger /*
      3       1.6    jruoho  * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
      4       1.1    cegger  * All rights reserved.
      5       1.1    cegger  *
      6       1.1    cegger  * This code is derived from software contributed to The NetBSD Foundation
      7       1.6    jruoho  * by Christoph Egger and Gregoire Sutre.
      8       1.1    cegger  *
      9       1.1    cegger  * Redistribution and use in source and binary forms, with or without
     10       1.1    cegger  * modification, are permitted provided that the following conditions
     11       1.1    cegger  * are met:
     12       1.1    cegger  * 1. Redistributions of source code must retain the above copyright
     13       1.1    cegger  *    notice, this list of conditions and the following disclaimer.
     14       1.1    cegger  * 2. The name of the author may not be used to endorse or promote products
     15       1.1    cegger  *    derived from this software without specific prior written permission.
     16       1.1    cegger  *
     17       1.1    cegger  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1    cegger  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1    cegger  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1    cegger  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1    cegger  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22       1.1    cegger  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23       1.1    cegger  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24       1.1    cegger  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25       1.1    cegger  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1    cegger  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1    cegger  * SUCH DAMAGE.
     28       1.1    cegger  */
     29       1.1    cegger 
     30       1.1    cegger #include <sys/cdefs.h>
     31  1.20.8.4  pgoyette __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.20.8.4 2018/11/26 01:52:30 pgoyette Exp $");
     32       1.1    cegger 
     33       1.1    cegger #include <sys/param.h>
     34       1.1    cegger #include <sys/device.h>
     35       1.1    cegger #include <sys/kmem.h>
     36       1.3    jruoho #include <sys/systm.h>
     37       1.1    cegger 
     38       1.6    jruoho #include <dev/pci/pcireg.h>
     39      1.17    jruoho #include <dev/pci/pcivar.h>
     40       1.6    jruoho #include <dev/pci/pcidevs.h>
     41       1.6    jruoho #include <dev/pci/ppbreg.h>
     42       1.6    jruoho 
     43       1.1    cegger #include <dev/acpi/acpireg.h>
     44       1.1    cegger #include <dev/acpi/acpivar.h>
     45       1.1    cegger #include <dev/acpi/acpi_pci.h>
     46       1.1    cegger 
     47      1.17    jruoho #include "locators.h"
     48      1.17    jruoho 
     49       1.7    jruoho #define _COMPONENT	  ACPI_BUS_COMPONENT
     50       1.7    jruoho ACPI_MODULE_NAME	  ("acpi_pci")
     51       1.6    jruoho 
     52       1.7    jruoho #define ACPI_HILODWORD(x) ACPI_HIWORD(ACPI_LODWORD((x)))
     53       1.7    jruoho #define ACPI_LOLODWORD(x) ACPI_LOWORD(ACPI_LODWORD((x)))
     54       1.7    jruoho 
     55       1.7    jruoho static ACPI_STATUS	  acpi_pcidev_pciroot_bus(ACPI_HANDLE, uint16_t *);
     56       1.7    jruoho static ACPI_STATUS	  acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *,
     57       1.7    jruoho 							   void *);
     58       1.1    cegger 
     59       1.6    jruoho /*
     60  1.20.8.4  pgoyette  * UUID for _DSM control method, from PCI Firmware Specification.
     61  1.20.8.4  pgoyette  */
     62  1.20.8.4  pgoyette static UINT8 acpi_pci_dsm_uuid[ACPI_UUID_LENGTH] = {
     63  1.20.8.4  pgoyette 	0xd0, 0x37, 0xc9, 0xe5, 0x53, 0x35, 0x7a, 0x4d,
     64  1.20.8.4  pgoyette 	0x91, 0x17, 0xea, 0x4d, 0x19, 0xc3, 0x43, 0x4d
     65  1.20.8.4  pgoyette };
     66  1.20.8.4  pgoyette 
     67  1.20.8.4  pgoyette /*
     68      1.11    jruoho  * Regarding PCI Segment Groups (ACPI 4.0, p. 277):
     69       1.6    jruoho  *
     70       1.6    jruoho  * "The optional _SEG object is located under a PCI host bridge and
     71      1.11    jruoho  *  evaluates to an integer that describes the PCI Segment Group (see PCI
     72      1.11    jruoho  *  Firmware Specification v3.0)."
     73      1.11    jruoho  *
     74      1.11    jruoho  * "PCI Segment Group is purely a software concept managed by system
     75      1.11    jruoho  *  firmware and used by OSPM. It is a logical collection of PCI buses
     76      1.11    jruoho  *  (or bus segments). It is a way to logically group the PCI bus segments
     77      1.11    jruoho  *  and PCI Express Hierarchies. _SEG is a level higher than _BBN."
     78       1.6    jruoho  *
     79       1.6    jruoho  * "PCI Segment Group supports more than 256 buses in a system by allowing
     80      1.11    jruoho  *  the reuse of the PCI bus numbers.  Within each PCI Segment Group, the bus
     81      1.11    jruoho  *  numbers for the PCI buses must be unique. PCI buses in different PCI
     82      1.11    jruoho  *  Segment Group are permitted to have the same bus number."
     83       1.6    jruoho  */
     84       1.1    cegger 
     85       1.6    jruoho /*
     86      1.11    jruoho  * Regarding PCI Base Bus Numbers (ACPI 4.0, p. 277):
     87       1.6    jruoho  *
     88       1.6    jruoho  * "For multi-root PCI platforms, the _BBN object evaluates to the PCI bus
     89      1.11    jruoho  *  number that the BIOS assigns.  This is needed to access a PCI_Config
     90      1.11    jruoho  *  operation region for the specified bus.  The _BBN object is located under
     91      1.11    jruoho  *  a PCI host bridge and must be unique for every host bridge within a
     92      1.11    jruoho  *  segment since it is the PCI bus number."
     93       1.6    jruoho  *
     94       1.6    jruoho  * Moreover, the ACPI FAQ (http://www.acpi.info/acpi_faq.htm) says:
     95       1.6    jruoho  *
     96       1.6    jruoho  * "For a multiple root bus machine, _BBN is required for each bus.  _BBN
     97      1.11    jruoho  *  should provide the bus number assigned to this bus by the BIOS at boot
     98      1.11    jruoho  *  time."
     99       1.6    jruoho  */
    100       1.6    jruoho 
    101       1.6    jruoho /*
    102       1.6    jruoho  * acpi_pcidev_pciroot_bus:
    103       1.6    jruoho  *
    104       1.6    jruoho  *	Derive the PCI bus number of a PCI root bridge from its resources.
    105       1.6    jruoho  *	If successful, return AE_OK and fill *busp.  Otherwise, return an
    106       1.6    jruoho  *	exception code and leave *busp unchanged.
    107       1.6    jruoho  */
    108       1.6    jruoho static ACPI_STATUS
    109       1.6    jruoho acpi_pcidev_pciroot_bus(ACPI_HANDLE handle, uint16_t *busp)
    110       1.1    cegger {
    111       1.1    cegger 	ACPI_STATUS rv;
    112       1.6    jruoho 	int32_t bus;
    113       1.6    jruoho 
    114       1.6    jruoho 	bus = -1;
    115      1.11    jruoho 
    116      1.11    jruoho 	/*
    117      1.11    jruoho 	 * XXX:	Use the ACPI resource parsing functions (acpi_resource.c)
    118      1.11    jruoho 	 *	once bus number ranges have been implemented there.
    119      1.11    jruoho 	 */
    120      1.11    jruoho 	rv = AcpiWalkResources(handle, "_CRS",
    121       1.6    jruoho 	    acpi_pcidev_pciroot_bus_callback, &bus);
    122       1.1    cegger 
    123       1.1    cegger 	if (ACPI_FAILURE(rv))
    124       1.6    jruoho 		return rv;
    125       1.6    jruoho 
    126      1.15    gsutre 	if (bus == -1)
    127       1.6    jruoho 		return AE_NOT_EXIST;
    128       1.6    jruoho 
    129      1.15    gsutre 	/* Here it holds that 0 <= bus <= 0xFFFF. */
    130       1.6    jruoho 	*busp = (uint16_t)bus;
    131       1.8    jruoho 
    132       1.6    jruoho 	return rv;
    133       1.6    jruoho }
    134       1.6    jruoho 
    135       1.6    jruoho static ACPI_STATUS
    136       1.6    jruoho acpi_pcidev_pciroot_bus_callback(ACPI_RESOURCE *res, void *context)
    137       1.6    jruoho {
    138       1.8    jruoho 	ACPI_RESOURCE_ADDRESS64 addr64;
    139       1.6    jruoho 	int32_t *bus = context;
    140       1.6    jruoho 
    141       1.8    jruoho 	/* Always continue the walk by returning AE_OK. */
    142       1.6    jruoho 	if ((res->Type != ACPI_RESOURCE_TYPE_ADDRESS16) &&
    143       1.6    jruoho 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
    144       1.6    jruoho 	    (res->Type != ACPI_RESOURCE_TYPE_ADDRESS64))
    145       1.8    jruoho 		return AE_OK;
    146       1.6    jruoho 
    147       1.6    jruoho 	if (ACPI_FAILURE(AcpiResourceToAddress64(res, &addr64)))
    148       1.8    jruoho 		return AE_OK;
    149       1.6    jruoho 
    150       1.6    jruoho 	if (addr64.ResourceType != ACPI_BUS_NUMBER_RANGE)
    151       1.8    jruoho 		return AE_OK;
    152       1.6    jruoho 
    153       1.6    jruoho 	if (*bus != -1)
    154       1.6    jruoho 		return AE_ALREADY_EXISTS;
    155       1.6    jruoho 
    156      1.19  christos 	if (addr64.Address.Minimum > 0xFFFF)
    157      1.15    gsutre 		return AE_BAD_DATA;
    158      1.15    gsutre 
    159      1.19  christos 	*bus = (int32_t)addr64.Address.Minimum;
    160       1.8    jruoho 
    161       1.8    jruoho 	return AE_OK;
    162       1.6    jruoho }
    163       1.6    jruoho 
    164       1.6    jruoho /*
    165       1.9    jruoho  * acpi_pcidev_scan:
    166       1.6    jruoho  *
    167       1.6    jruoho  *	Scan the ACPI device tree for PCI devices.  A node is detected as a
    168       1.6    jruoho  *	PCI device if it has an ancestor that is a PCI root bridge and such
    169       1.6    jruoho  *	that all intermediate nodes are PCI-to-PCI bridges.  Depth-first
    170       1.6    jruoho  *	recursive implementation.
    171      1.16    gsutre  *
    172      1.16    gsutre  *	PCI root bridges do not necessarily contain an _ADR, since they already
    173      1.16    gsutre  *	contain an _HID (ACPI 4.0a, p. 197).  However we require an _ADR for
    174      1.16    gsutre  *	all non-root PCI devices.
    175       1.6    jruoho  */
    176       1.9    jruoho ACPI_STATUS
    177       1.9    jruoho acpi_pcidev_scan(struct acpi_devnode *ad)
    178       1.6    jruoho {
    179       1.6    jruoho 	struct acpi_devnode *child;
    180       1.6    jruoho 	struct acpi_pci_info *ap;
    181       1.6    jruoho 	ACPI_INTEGER val;
    182       1.6    jruoho 	ACPI_STATUS rv;
    183       1.6    jruoho 
    184      1.13    gsutre 	ad->ad_pciinfo = NULL;
    185      1.13    gsutre 
    186      1.14    gsutre 	/*
    187      1.14    gsutre 	 * We attach PCI information only to devices that are present,
    188      1.14    gsutre 	 * enabled, and functioning properly.
    189      1.14    gsutre 	 * Note: there is a possible race condition, because _STA may
    190      1.14    gsutre 	 * have changed since ad->ad_devinfo->CurrentStatus was set.
    191      1.14    gsutre 	 */
    192      1.16    gsutre 	if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    193      1.16    gsutre 		goto rec;
    194      1.14    gsutre 
    195  1.20.8.2  pgoyette 	if (!acpi_device_present(ad->ad_handle))
    196  1.20.8.2  pgoyette 		goto rec;
    197  1.20.8.2  pgoyette 
    198       1.6    jruoho 	if (ad->ad_devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) {
    199       1.8    jruoho 
    200       1.6    jruoho 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    201       1.8    jruoho 
    202      1.11    jruoho 		/*
    203      1.11    jruoho 		 * If no _SEG exist, all PCI bus segments are assumed
    204      1.11    jruoho 		 * to be in the PCI segment group 0 (ACPI 4.0, p. 277).
    205      1.11    jruoho 		 * The segment group number is conveyed in the lower
    206      1.11    jruoho 		 * 16 bits of _SEG (the other bits are all reserved).
    207      1.11    jruoho 		 */
    208       1.8    jruoho 		rv = acpi_eval_integer(ad->ad_handle, "_SEG", &val);
    209       1.8    jruoho 
    210       1.6    jruoho 		if (ACPI_SUCCESS(rv))
    211       1.6    jruoho 			ap->ap_segment = ACPI_LOWORD(val);
    212       1.6    jruoho 
    213      1.16    gsutre 		/* Try to get downstream bus number using _CRS first. */
    214      1.16    gsutre 		rv = acpi_pcidev_pciroot_bus(ad->ad_handle, &ap->ap_downbus);
    215       1.8    jruoho 
    216       1.6    jruoho 		if (ACPI_FAILURE(rv)) {
    217       1.8    jruoho 			rv = acpi_eval_integer(ad->ad_handle, "_BBN", &val);
    218       1.8    jruoho 
    219       1.6    jruoho 			if (ACPI_SUCCESS(rv))
    220      1.16    gsutre 				ap->ap_downbus = ACPI_LOWORD(val);
    221       1.6    jruoho 		}
    222       1.6    jruoho 
    223      1.16    gsutre 		if (ap->ap_downbus > 255) {
    224      1.13    gsutre 			aprint_error_dev(ad->ad_root,
    225      1.16    gsutre 			    "invalid PCI downstream bus for %s\n", ad->ad_name);
    226      1.13    gsutre 			kmem_free(ap, sizeof(*ap));
    227      1.13    gsutre 			goto rec;
    228      1.13    gsutre 		}
    229      1.13    gsutre 
    230      1.16    gsutre 		ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
    231      1.16    gsutre 
    232      1.16    gsutre 		/*
    233      1.16    gsutre 		 * This ACPI node denotes a PCI root bridge, but it may also
    234      1.16    gsutre 		 * denote a PCI device on the bridge's downstream bus segment.
    235      1.16    gsutre 		 */
    236      1.16    gsutre 		if (ad->ad_devinfo->Valid & ACPI_VALID_ADR) {
    237      1.16    gsutre 			ap->ap_bus = ap->ap_downbus;
    238      1.16    gsutre 			ap->ap_device =
    239      1.16    gsutre 			    ACPI_HILODWORD(ad->ad_devinfo->Address);
    240      1.16    gsutre 			ap->ap_function =
    241      1.16    gsutre 			    ACPI_LOLODWORD(ad->ad_devinfo->Address);
    242      1.16    gsutre 
    243      1.16    gsutre 			if (ap->ap_device > 31 ||
    244      1.16    gsutre 			    (ap->ap_function > 7 && ap->ap_function != 0xFFFF))
    245      1.16    gsutre 				aprint_error_dev(ad->ad_root,
    246      1.16    gsutre 				    "invalid PCI address for %s\n", ad->ad_name);
    247      1.16    gsutre 			else
    248      1.16    gsutre 				ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
    249      1.16    gsutre 		}
    250       1.6    jruoho 
    251       1.6    jruoho 		ad->ad_pciinfo = ap;
    252       1.8    jruoho 
    253       1.6    jruoho 		goto rec;
    254       1.6    jruoho 	}
    255       1.6    jruoho 
    256       1.6    jruoho 	if ((ad->ad_parent != NULL) &&
    257       1.6    jruoho 	    (ad->ad_parent->ad_pciinfo != NULL) &&
    258      1.16    gsutre 	    (ad->ad_parent->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
    259      1.16    gsutre 	    (ad->ad_devinfo->Valid & ACPI_VALID_ADR)) {
    260      1.11    jruoho 
    261       1.6    jruoho 		/*
    262      1.11    jruoho 		 * Our parent is a PCI root bridge or a PCI-to-PCI
    263      1.11    jruoho 		 * bridge. We have the same PCI segment number, and
    264      1.11    jruoho 		 * our bus number is its downstream bus number.
    265       1.6    jruoho 		 */
    266       1.6    jruoho 		ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    267       1.8    jruoho 
    268       1.6    jruoho 		ap->ap_segment = ad->ad_parent->ad_pciinfo->ap_segment;
    269       1.6    jruoho 		ap->ap_bus = ad->ad_parent->ad_pciinfo->ap_downbus;
    270       1.7    jruoho 
    271       1.7    jruoho 		ap->ap_device = ACPI_HILODWORD(ad->ad_devinfo->Address);
    272       1.7    jruoho 		ap->ap_function = ACPI_LOLODWORD(ad->ad_devinfo->Address);
    273       1.1    cegger 
    274      1.15    gsutre 		if (ap->ap_device > 31 ||
    275      1.15    gsutre 		    (ap->ap_function > 7 && ap->ap_function != 0xFFFF)) {
    276      1.13    gsutre 			aprint_error_dev(ad->ad_root,
    277      1.13    gsutre 			    "invalid PCI address for %s\n", ad->ad_name);
    278      1.13    gsutre 			kmem_free(ap, sizeof(*ap));
    279      1.13    gsutre 			goto rec;
    280      1.13    gsutre 		}
    281      1.13    gsutre 
    282      1.16    gsutre 		ap->ap_flags |= ACPI_PCI_INFO_DEVICE;
    283      1.16    gsutre 
    284      1.15    gsutre 		if (ap->ap_function == 0xFFFF) {
    285      1.15    gsutre 			/*
    286      1.15    gsutre 			 * Assume that this device is not a PCI-to-PCI bridge.
    287      1.15    gsutre 			 * XXX: Do we need to be smarter?
    288      1.15    gsutre 			 */
    289      1.15    gsutre 		} else {
    290      1.15    gsutre 			/*
    291      1.15    gsutre 			 * Check whether this device is a PCI-to-PCI
    292      1.15    gsutre 			 * bridge and get its secondary bus number.
    293      1.15    gsutre 			 */
    294      1.15    gsutre 			rv = acpi_pcidev_ppb_downbus(ap->ap_segment, ap->ap_bus,
    295      1.15    gsutre 			    ap->ap_device, ap->ap_function, &ap->ap_downbus);
    296      1.15    gsutre 
    297      1.16    gsutre 			if (ACPI_SUCCESS(rv))
    298      1.16    gsutre 				ap->ap_flags |= ACPI_PCI_INFO_BRIDGE;
    299      1.15    gsutre 		}
    300       1.6    jruoho 
    301       1.6    jruoho 		ad->ad_pciinfo = ap;
    302       1.8    jruoho 
    303       1.6    jruoho 		goto rec;
    304       1.6    jruoho 	}
    305       1.9    jruoho 
    306       1.8    jruoho rec:
    307       1.6    jruoho 	SIMPLEQ_FOREACH(child, &ad->ad_child_head, ad_child_list) {
    308       1.9    jruoho 		rv = acpi_pcidev_scan(child);
    309       1.8    jruoho 
    310       1.6    jruoho 		if (ACPI_FAILURE(rv))
    311       1.6    jruoho 			return rv;
    312       1.1    cegger 	}
    313       1.1    cegger 
    314       1.6    jruoho 	return AE_OK;
    315       1.6    jruoho }
    316       1.6    jruoho 
    317       1.6    jruoho /*
    318       1.6    jruoho  * acpi_pcidev_ppb_downbus:
    319       1.6    jruoho  *
    320       1.6    jruoho  *	Retrieve the secondary bus number of the PCI-to-PCI bridge having the
    321      1.13    gsutre  *	given PCI id.  If successful, return AE_OK and fill *downbus.
    322      1.13    gsutre  *	Otherwise, return an exception code and leave *downbus unchanged.
    323       1.6    jruoho  *
    324       1.6    jruoho  * XXX	Need to deal with PCI segment groups (see also acpica/OsdHardware.c).
    325       1.6    jruoho  */
    326       1.6    jruoho ACPI_STATUS
    327       1.6    jruoho acpi_pcidev_ppb_downbus(uint16_t segment, uint16_t bus, uint16_t device,
    328       1.6    jruoho     uint16_t function, uint16_t *downbus)
    329       1.6    jruoho {
    330       1.6    jruoho 	struct acpi_softc *sc = acpi_softc;
    331       1.6    jruoho 	pci_chipset_tag_t pc;
    332       1.6    jruoho 	pcitag_t tag;
    333       1.6    jruoho 	pcireg_t val;
    334       1.6    jruoho 
    335       1.6    jruoho 	if (bus > 255 || device > 31 || function > 7)
    336       1.6    jruoho 		return AE_BAD_PARAMETER;
    337       1.1    cegger 
    338      1.12       mrg 	pc = sc->sc_pc;
    339       1.1    cegger 
    340       1.6    jruoho 	tag = pci_make_tag(pc, bus, device, function);
    341       1.1    cegger 
    342       1.6    jruoho 	/* Check that this device exists. */
    343       1.6    jruoho 	val = pci_conf_read(pc, tag, PCI_ID_REG);
    344       1.8    jruoho 
    345       1.6    jruoho 	if (PCI_VENDOR(val) == PCI_VENDOR_INVALID ||
    346       1.6    jruoho 	    PCI_VENDOR(val) == 0)
    347       1.6    jruoho 		return AE_NOT_EXIST;
    348       1.6    jruoho 
    349       1.6    jruoho 	/* Check that this device is a PCI-to-PCI bridge. */
    350       1.6    jruoho 	val = pci_conf_read(pc, tag, PCI_BHLC_REG);
    351       1.8    jruoho 
    352       1.6    jruoho 	if (PCI_HDRTYPE_TYPE(val) != PCI_HDRTYPE_PPB)
    353       1.6    jruoho 		return AE_TYPE;
    354       1.6    jruoho 
    355       1.6    jruoho 	/* This is a PCI-to-PCI bridge.  Get its secondary bus#. */
    356       1.6    jruoho 	val = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
    357       1.6    jruoho 	*downbus = PPB_BUSINFO_SECONDARY(val);
    358       1.8    jruoho 
    359       1.6    jruoho 	return AE_OK;
    360       1.1    cegger }
    361       1.1    cegger 
    362       1.1    cegger /*
    363       1.1    cegger  * acpi_pcidev_find:
    364       1.1    cegger  *
    365       1.1    cegger  *      Finds a PCI device in the ACPI name space.
    366      1.10    jruoho  *
    367      1.10    jruoho  *      Returns an ACPI device node on success and NULL on failure.
    368       1.1    cegger  */
    369      1.10    jruoho struct acpi_devnode *
    370      1.10    jruoho acpi_pcidev_find(uint16_t segment, uint16_t bus,
    371      1.10    jruoho     uint16_t device, uint16_t function)
    372       1.1    cegger {
    373       1.6    jruoho 	struct acpi_softc *sc = acpi_softc;
    374       1.6    jruoho 	struct acpi_devnode *ad;
    375       1.1    cegger 
    376       1.6    jruoho 	if (sc == NULL)
    377      1.10    jruoho 		return NULL;
    378       1.1    cegger 
    379       1.6    jruoho 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    380       1.8    jruoho 
    381      1.10    jruoho 		if (ad->ad_pciinfo != NULL &&
    382      1.16    gsutre 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_DEVICE) &&
    383      1.10    jruoho 		    ad->ad_pciinfo->ap_segment == segment &&
    384      1.10    jruoho 		    ad->ad_pciinfo->ap_bus == bus &&
    385      1.10    jruoho 		    ad->ad_pciinfo->ap_device == device &&
    386      1.10    jruoho 		    ad->ad_pciinfo->ap_function == function)
    387      1.10    jruoho 			return ad;
    388       1.1    cegger 	}
    389       1.8    jruoho 
    390      1.10    jruoho 	return NULL;
    391       1.1    cegger }
    392      1.17    jruoho 
    393  1.20.8.3  pgoyette /*
    394  1.20.8.3  pgoyette  * acpi_pciroot_find:
    395  1.20.8.3  pgoyette  *
    396  1.20.8.3  pgoyette  *	Finds a PCI root bridge in the ACPI name space.
    397  1.20.8.3  pgoyette  *
    398  1.20.8.3  pgoyette  *	Returns an ACPI device node on success and NULL on failure.
    399  1.20.8.3  pgoyette  */
    400  1.20.8.3  pgoyette struct acpi_devnode *
    401  1.20.8.3  pgoyette acpi_pciroot_find(uint16_t segment, uint16_t bus)
    402  1.20.8.3  pgoyette {
    403  1.20.8.3  pgoyette 	struct acpi_softc *sc = acpi_softc;
    404  1.20.8.3  pgoyette 	struct acpi_devnode *ad;
    405  1.20.8.3  pgoyette 
    406  1.20.8.3  pgoyette 	if (sc == NULL)
    407  1.20.8.3  pgoyette 		return NULL;
    408  1.20.8.3  pgoyette 
    409  1.20.8.3  pgoyette 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    410  1.20.8.3  pgoyette 
    411  1.20.8.3  pgoyette 		if (ad->ad_pciinfo != NULL &&
    412  1.20.8.3  pgoyette 		    (ad->ad_pciinfo->ap_flags & ACPI_PCI_INFO_BRIDGE) &&
    413  1.20.8.3  pgoyette 		    ad->ad_pciinfo->ap_segment == segment &&
    414  1.20.8.3  pgoyette 		    ad->ad_pciinfo->ap_bus == bus)
    415  1.20.8.3  pgoyette 			return ad;
    416  1.20.8.3  pgoyette 	}
    417  1.20.8.3  pgoyette 
    418  1.20.8.3  pgoyette 	return NULL;
    419  1.20.8.3  pgoyette }
    420      1.17    jruoho 
    421      1.17    jruoho /*
    422      1.17    jruoho  * acpi_pcidev_find_dev:
    423      1.17    jruoho  *
    424      1.17    jruoho  *	Returns the device corresponding to the given PCI info, or NULL
    425      1.17    jruoho  *	if it doesn't exist.
    426      1.17    jruoho  */
    427      1.17    jruoho device_t
    428      1.18    jruoho acpi_pcidev_find_dev(struct acpi_devnode *ad)
    429      1.17    jruoho {
    430      1.18    jruoho 	struct acpi_pci_info *ap;
    431      1.17    jruoho 	struct pci_softc *pci;
    432      1.17    jruoho 	device_t dv, pr;
    433      1.17    jruoho 	deviter_t di;
    434      1.17    jruoho 
    435      1.18    jruoho 	if (ad == NULL)
    436      1.18    jruoho 		return NULL;
    437      1.18    jruoho 
    438      1.18    jruoho 	if (ad->ad_pciinfo == NULL)
    439      1.17    jruoho 		return NULL;
    440      1.17    jruoho 
    441      1.18    jruoho 	ap = ad->ad_pciinfo;
    442      1.18    jruoho 
    443      1.17    jruoho 	if (ap->ap_function == 0xFFFF)
    444      1.17    jruoho 		return NULL;
    445      1.17    jruoho 
    446      1.17    jruoho 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    447      1.17    jruoho 	     dv != NULL; dv = deviter_next(&di)) {
    448      1.17    jruoho 
    449      1.17    jruoho 		pr = device_parent(dv);
    450      1.17    jruoho 
    451      1.17    jruoho 		if (pr == NULL || device_is_a(pr, "pci") != true)
    452      1.17    jruoho 			continue;
    453      1.17    jruoho 
    454      1.17    jruoho 		if (dv->dv_locators == NULL)	/* This should not happen. */
    455      1.17    jruoho 			continue;
    456      1.17    jruoho 
    457      1.17    jruoho 		pci = device_private(pr);
    458      1.17    jruoho 
    459      1.17    jruoho 		if (pci->sc_bus == ap->ap_bus &&
    460      1.17    jruoho 		    device_locator(dv, PCICF_DEV) == ap->ap_device &&
    461      1.17    jruoho 		    device_locator(dv, PCICF_FUNCTION) == ap->ap_function)
    462      1.17    jruoho 			break;
    463      1.17    jruoho 	}
    464      1.17    jruoho 
    465      1.17    jruoho 	deviter_release(&di);
    466      1.17    jruoho 
    467      1.17    jruoho 	return dv;
    468      1.17    jruoho }
    469  1.20.8.4  pgoyette 
    470  1.20.8.4  pgoyette /*
    471  1.20.8.4  pgoyette  * acpi_pci_ignore_boot_config:
    472  1.20.8.4  pgoyette  *
    473  1.20.8.4  pgoyette  *	Returns 1 if the operating system may ignore the boot configuration
    474  1.20.8.4  pgoyette  *	of PCI resources.
    475  1.20.8.4  pgoyette  */
    476  1.20.8.4  pgoyette ACPI_INTEGER
    477  1.20.8.4  pgoyette acpi_pci_ignore_boot_config(ACPI_HANDLE handle)
    478  1.20.8.4  pgoyette {
    479  1.20.8.4  pgoyette 	ACPI_OBJECT_LIST objs;
    480  1.20.8.4  pgoyette 	ACPI_OBJECT obj[4], *pobj;
    481  1.20.8.4  pgoyette 	ACPI_BUFFER buf;
    482  1.20.8.4  pgoyette 	ACPI_INTEGER ret;
    483  1.20.8.4  pgoyette 
    484  1.20.8.4  pgoyette 	objs.Count = 4;
    485  1.20.8.4  pgoyette 	objs.Pointer = obj;
    486  1.20.8.4  pgoyette 	obj[0].Type = ACPI_TYPE_BUFFER;
    487  1.20.8.4  pgoyette 	obj[0].Buffer.Length = ACPI_UUID_LENGTH;
    488  1.20.8.4  pgoyette 	obj[0].Buffer.Pointer = acpi_pci_dsm_uuid;
    489  1.20.8.4  pgoyette 	obj[1].Type = ACPI_TYPE_INTEGER;
    490  1.20.8.4  pgoyette 	obj[1].Integer.Value = 1;
    491  1.20.8.4  pgoyette 	obj[2].Type = ACPI_TYPE_INTEGER;
    492  1.20.8.4  pgoyette 	obj[2].Integer.Value = 5;
    493  1.20.8.4  pgoyette 	obj[3].Type = ACPI_TYPE_PACKAGE;
    494  1.20.8.4  pgoyette 	obj[3].Package.Count = 0;
    495  1.20.8.4  pgoyette 	obj[3].Package.Elements = NULL;
    496  1.20.8.4  pgoyette 
    497  1.20.8.4  pgoyette 	buf.Pointer = NULL;
    498  1.20.8.4  pgoyette 	buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    499  1.20.8.4  pgoyette 
    500  1.20.8.4  pgoyette 	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_DSM", &objs, &buf)) || buf.Pointer == NULL)
    501  1.20.8.4  pgoyette 		return 0;
    502  1.20.8.4  pgoyette 
    503  1.20.8.4  pgoyette 	ret = 0;
    504  1.20.8.4  pgoyette 
    505  1.20.8.4  pgoyette 	pobj = buf.Pointer;
    506  1.20.8.4  pgoyette 	switch (pobj->Type) {
    507  1.20.8.4  pgoyette 	case ACPI_TYPE_INTEGER:
    508  1.20.8.4  pgoyette 		ret = pobj->Integer.Value;
    509  1.20.8.4  pgoyette 		break;
    510  1.20.8.4  pgoyette 	case ACPI_TYPE_PACKAGE:
    511  1.20.8.4  pgoyette 		if (pobj->Package.Count == 1 && pobj->Package.Elements[0].Type == ACPI_TYPE_INTEGER)
    512  1.20.8.4  pgoyette 			ret = pobj->Package.Elements[0].Integer.Value;
    513  1.20.8.4  pgoyette 		break;
    514  1.20.8.4  pgoyette 	}
    515  1.20.8.4  pgoyette 
    516  1.20.8.4  pgoyette 	ACPI_FREE(buf.Pointer);
    517  1.20.8.4  pgoyette 
    518  1.20.8.4  pgoyette 	return ret;
    519  1.20.8.4  pgoyette }
    520