Home | History | Annotate | Line # | Download | only in acpi
acpi_pci.c revision 1.3
      1  1.3  jruoho /* $NetBSD: acpi_pci.c,v 1.3 2010/03/05 14:00:17 jruoho Exp $ */
      2  1.1  cegger 
      3  1.1  cegger /*
      4  1.1  cegger  * Copyright (c) 2009 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.1  cegger  * by Christoph Egger.
      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.3  jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.3 2010/03/05 14:00:17 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/queue.h>
     42  1.3  jruoho #include <sys/systm.h>
     43  1.1  cegger 
     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.1  cegger struct acpi_pcidev;
     49  1.1  cegger 
     50  1.1  cegger static TAILQ_HEAD(, acpi_pcidev) acpi_pcidevlist =
     51  1.1  cegger     TAILQ_HEAD_INITIALIZER(acpi_pcidevlist);
     52  1.1  cegger 
     53  1.1  cegger struct acpi_pcidev {
     54  1.1  cegger 	struct acpi_devnode *ap_node;
     55  1.1  cegger 	uint32_t ap_pciseg;
     56  1.1  cegger 	uint32_t ap_pcibus;
     57  1.1  cegger 	uint32_t ap_pcidev;
     58  1.1  cegger 	uint32_t ap_pcifunc;
     59  1.1  cegger 	bool ap_pcihost;
     60  1.1  cegger 	TAILQ_ENTRY(acpi_pcidev) ap_list;
     61  1.1  cegger };
     62  1.1  cegger 
     63  1.1  cegger 
     64  1.1  cegger static const char * const acpi_pcidev_ids[] = {
     65  1.1  cegger 	"PNP0A??",	/* ACPI PCI host controllers */
     66  1.1  cegger 	NULL
     67  1.1  cegger };
     68  1.1  cegger 
     69  1.1  cegger static bool
     70  1.1  cegger acpi_pcidev_add(struct acpi_softc *sc, struct acpi_devnode *ad)
     71  1.1  cegger {
     72  1.1  cegger 	struct acpi_pcidev *ap;
     73  1.1  cegger 	ACPI_STATUS rv;
     74  1.1  cegger 	ACPI_INTEGER seg, bus, addr;
     75  1.1  cegger 
     76  1.1  cegger 	/*
     77  1.1  cegger 	 * ACPI spec: "The _BBN object is located under a
     78  1.1  cegger 	 * PCI host bridge and must be unique for every
     79  1.1  cegger 	 * host bridge within a segment since it is the PCI bus number."
     80  1.1  cegger 	 */
     81  1.1  cegger 	rv = acpi_eval_integer(ad->ad_handle, "_BBN", &bus);
     82  1.1  cegger 	if (ACPI_FAILURE(rv))
     83  1.1  cegger 		return false;
     84  1.1  cegger 	/*
     85  1.1  cegger          * The ACPI address (_ADR) is equal to: (device << 16) | function.
     86  1.1  cegger 	 */
     87  1.1  cegger 	rv = acpi_eval_integer(ad->ad_handle, "_ADR", &addr);
     88  1.1  cegger 	if (ACPI_FAILURE(rv))
     89  1.1  cegger 		return false;
     90  1.1  cegger 
     91  1.1  cegger 	/*
     92  1.1  cegger 	 * ACPI spec: "The optional _SEG object is located under a PCI host
     93  1.1  cegger 	 * bridge and evaluates to an integer that describes the
     94  1.1  cegger 	 * PCI Segment Group (see PCI Firmware Specification v3.0)."
     95  1.1  cegger 	 *
     96  1.1  cegger 	 * "PCI Segment Group supports more than 256 buses
     97  1.1  cegger 	 * in a system by allowing the reuse of the PCI bus numbers.
     98  1.1  cegger 	 * Within each PCI Segment Group, the bus numbers for the PCI
     99  1.1  cegger 	 * buses must be unique. PCI buses in different PCI Segment
    100  1.1  cegger 	 * Group are permitted to have the same bus number."
    101  1.1  cegger 	 */
    102  1.1  cegger 	rv = acpi_eval_integer(ad->ad_handle, "_SEG", &seg);
    103  1.1  cegger 	if (ACPI_FAILURE(rv)) {
    104  1.1  cegger 		/*
    105  1.1  cegger 		 * ACPI spec: "If _SEG does not exist, OSPM assumes that all
    106  1.1  cegger 		 * PCI bus segments are in PCI Segment Group 0."
    107  1.1  cegger 		 */
    108  1.1  cegger 		seg = 0;
    109  1.1  cegger 	}
    110  1.1  cegger 
    111  1.1  cegger 	ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
    112  1.1  cegger 	if (ap == NULL) {
    113  1.1  cegger 		aprint_error("%s: kmem_alloc failed\n", __func__);
    114  1.1  cegger 		return false;
    115  1.1  cegger 	}
    116  1.1  cegger 
    117  1.1  cegger 	if (acpi_match_hid(ad->ad_devinfo, acpi_pcidev_ids))
    118  1.1  cegger 		ap->ap_pcihost = true;
    119  1.1  cegger 	else
    120  1.1  cegger 		ap->ap_pcihost = false;
    121  1.1  cegger 
    122  1.1  cegger 	ap->ap_node = ad;
    123  1.1  cegger 	ap->ap_pciseg = seg;
    124  1.1  cegger 	ap->ap_pcibus = bus;
    125  1.1  cegger 	ap->ap_pcidev = addr >> 16;
    126  1.1  cegger 	ap->ap_pcifunc = addr & 0xffff;
    127  1.1  cegger 
    128  1.1  cegger 	TAILQ_INSERT_TAIL(&acpi_pcidevlist, ap, ap_list);
    129  1.1  cegger 
    130  1.1  cegger 	return true;
    131  1.1  cegger }
    132  1.1  cegger 
    133  1.1  cegger static void
    134  1.1  cegger acpi_pcidev_print(struct acpi_pcidev *ap)
    135  1.1  cegger {
    136  1.1  cegger 	aprint_debug(" %s", ap->ap_node->ad_name);
    137  1.1  cegger }
    138  1.1  cegger 
    139  1.1  cegger int
    140  1.1  cegger acpi_pcidev_scan(struct acpi_softc *sc)
    141  1.1  cegger {
    142  1.1  cegger 	struct acpi_scope *as;
    143  1.1  cegger 	struct acpi_devnode *ad;
    144  1.1  cegger 	struct acpi_pcidev *ap;
    145  1.1  cegger 	ACPI_DEVICE_INFO *di;
    146  1.1  cegger 	int count = 0;
    147  1.1  cegger 
    148  1.1  cegger #define ACPI_STA_DEV_VALID      \
    149  1.1  cegger 	(ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|ACPI_STA_DEV_OK)
    150  1.1  cegger 
    151  1.1  cegger 	TAILQ_FOREACH(as, &sc->sc_scopes, as_list) {
    152  1.1  cegger 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    153  1.1  cegger 			di = ad->ad_devinfo;
    154  1.1  cegger 			if (di->Type != ACPI_TYPE_DEVICE)
    155  1.1  cegger 				continue;
    156  1.1  cegger 			if ((di->Valid & ACPI_VALID_STA) != 0 &&
    157  1.1  cegger 			    (di->CurrentStatus & ACPI_STA_DEV_VALID) !=
    158  1.1  cegger 			     ACPI_STA_DEV_VALID)
    159  1.1  cegger 				continue;
    160  1.1  cegger 			if (acpi_pcidev_add(sc, ad) == true)
    161  1.1  cegger 				++count;
    162  1.1  cegger 		}
    163  1.1  cegger 	}
    164  1.1  cegger 
    165  1.1  cegger #undef ACPI_STA_DEV_VALID
    166  1.1  cegger 
    167  1.1  cegger 	if (count == 0)
    168  1.1  cegger 		return 0;
    169  1.1  cegger 
    170  1.1  cegger 	aprint_debug_dev(sc->sc_dev, "pci devices:");
    171  1.1  cegger 	TAILQ_FOREACH(ap, &acpi_pcidevlist, ap_list)
    172  1.1  cegger 		acpi_pcidev_print(ap);
    173  1.1  cegger 	aprint_debug("\n");
    174  1.1  cegger 
    175  1.1  cegger 	return count;
    176  1.1  cegger }
    177  1.1  cegger 
    178  1.1  cegger /*
    179  1.1  cegger  * acpi_pcidev_find:
    180  1.1  cegger  *
    181  1.1  cegger  *      Finds a PCI device in the ACPI name space.
    182  1.1  cegger  *      The return status is either:
    183  1.1  cegger  *      - AE_NOT_FOUND if no such device was found.
    184  1.1  cegger  *      - AE_OK if one and only one such device was found.
    185  1.1  cegger  */
    186  1.1  cegger ACPI_STATUS
    187  1.1  cegger acpi_pcidev_find(u_int segment, u_int bus, u_int device, u_int function,
    188  1.1  cegger     ACPI_HANDLE *handlep)
    189  1.1  cegger {
    190  1.1  cegger 	struct acpi_pcidev *ap;
    191  1.1  cegger 	ACPI_HANDLE hdl;
    192  1.1  cegger 
    193  1.1  cegger 	hdl = NULL;
    194  1.1  cegger 	TAILQ_FOREACH(ap, &acpi_pcidevlist, ap_list) {
    195  1.1  cegger 		if (ap->ap_pciseg != segment)
    196  1.1  cegger 			continue;
    197  1.1  cegger 		if (ap->ap_pcibus != bus)
    198  1.1  cegger 			continue;
    199  1.1  cegger 		if (ap->ap_pcidev != device)
    200  1.1  cegger 			continue;
    201  1.1  cegger 		if (ap->ap_pcifunc != function)
    202  1.1  cegger 			continue;
    203  1.1  cegger 
    204  1.1  cegger 		hdl = ap->ap_node->ad_handle;
    205  1.1  cegger 		break;
    206  1.1  cegger 	}
    207  1.1  cegger 
    208  1.1  cegger 	*handlep = hdl;
    209  1.1  cegger 	return (hdl != NULL) ? AE_OK : AE_NOT_FOUND;
    210  1.1  cegger }
    211