xref: /netbsd-src/sys/dev/acpi/acpi_pci.c (revision 1ca06f9c9235889e2ff6dc77279d01d151d70a9a)
1 /* $NetBSD: acpi_pci.c,v 1.2 2009/12/04 10:42:39 njoly Exp $ */
2 
3 /*
4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christoph Egger.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * ACPI PCI Bus
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: acpi_pci.c,v 1.2 2009/12/04 10:42:39 njoly Exp $");
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 
43 #include <sys/bus.h>
44 #include <sys/kmem.h>
45 #include <dev/pci/pcivar.h>
46 
47 #include <dev/acpi/acpica.h>
48 #include <dev/acpi/acpireg.h>
49 #include <dev/acpi/acpivar.h>
50 #include <dev/acpi/acpi_pci.h>
51 
52 struct acpi_pcidev;
53 
54 static TAILQ_HEAD(, acpi_pcidev) acpi_pcidevlist =
55     TAILQ_HEAD_INITIALIZER(acpi_pcidevlist);
56 
57 struct acpi_pcidev {
58 	struct acpi_devnode *ap_node;
59 	uint32_t ap_pciseg;
60 	uint32_t ap_pcibus;
61 	uint32_t ap_pcidev;
62 	uint32_t ap_pcifunc;
63 	bool ap_pcihost;
64 	TAILQ_ENTRY(acpi_pcidev) ap_list;
65 };
66 
67 
68 static const char * const acpi_pcidev_ids[] = {
69 	"PNP0A??",	/* ACPI PCI host controllers */
70 	NULL
71 };
72 
73 static bool
74 acpi_pcidev_add(struct acpi_softc *sc, struct acpi_devnode *ad)
75 {
76 	struct acpi_pcidev *ap;
77 	ACPI_STATUS rv;
78 	ACPI_INTEGER seg, bus, addr;
79 
80 	/*
81 	 * ACPI spec: "The _BBN object is located under a
82 	 * PCI host bridge and must be unique for every
83 	 * host bridge within a segment since it is the PCI bus number."
84 	 */
85 	rv = acpi_eval_integer(ad->ad_handle, "_BBN", &bus);
86 	if (ACPI_FAILURE(rv))
87 		return false;
88 	/*
89          * The ACPI address (_ADR) is equal to: (device << 16) | function.
90 	 */
91 	rv = acpi_eval_integer(ad->ad_handle, "_ADR", &addr);
92 	if (ACPI_FAILURE(rv))
93 		return false;
94 
95 	/*
96 	 * ACPI spec: "The optional _SEG object is located under a PCI host
97 	 * bridge and evaluates to an integer that describes the
98 	 * PCI Segment Group (see PCI Firmware Specification v3.0)."
99 	 *
100 	 * "PCI Segment Group supports more than 256 buses
101 	 * in a system by allowing the reuse of the PCI bus numbers.
102 	 * Within each PCI Segment Group, the bus numbers for the PCI
103 	 * buses must be unique. PCI buses in different PCI Segment
104 	 * Group are permitted to have the same bus number."
105 	 */
106 	rv = acpi_eval_integer(ad->ad_handle, "_SEG", &seg);
107 	if (ACPI_FAILURE(rv)) {
108 		/*
109 		 * ACPI spec: "If _SEG does not exist, OSPM assumes that all
110 		 * PCI bus segments are in PCI Segment Group 0."
111 		 */
112 		seg = 0;
113 	}
114 
115 	ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
116 	if (ap == NULL) {
117 		aprint_error("%s: kmem_alloc failed\n", __func__);
118 		return false;
119 	}
120 
121 	if (acpi_match_hid(ad->ad_devinfo, acpi_pcidev_ids))
122 		ap->ap_pcihost = true;
123 	else
124 		ap->ap_pcihost = false;
125 
126 	ap->ap_node = ad;
127 	ap->ap_pciseg = seg;
128 	ap->ap_pcibus = bus;
129 	ap->ap_pcidev = addr >> 16;
130 	ap->ap_pcifunc = addr & 0xffff;
131 
132 	TAILQ_INSERT_TAIL(&acpi_pcidevlist, ap, ap_list);
133 
134 	return true;
135 }
136 
137 static void
138 acpi_pcidev_print(struct acpi_pcidev *ap)
139 {
140 	aprint_debug(" %s", ap->ap_node->ad_name);
141 }
142 
143 int
144 acpi_pcidev_scan(struct acpi_softc *sc)
145 {
146 	struct acpi_scope *as;
147 	struct acpi_devnode *ad;
148 	struct acpi_pcidev *ap;
149 	ACPI_DEVICE_INFO *di;
150 	int count = 0;
151 
152 #define ACPI_STA_DEV_VALID      \
153 	(ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|ACPI_STA_DEV_OK)
154 
155 	TAILQ_FOREACH(as, &sc->sc_scopes, as_list) {
156 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
157 			di = ad->ad_devinfo;
158 			if (di->Type != ACPI_TYPE_DEVICE)
159 				continue;
160 			if ((di->Valid & ACPI_VALID_STA) != 0 &&
161 			    (di->CurrentStatus & ACPI_STA_DEV_VALID) !=
162 			     ACPI_STA_DEV_VALID)
163 				continue;
164 			if (acpi_pcidev_add(sc, ad) == true)
165 				++count;
166 		}
167 	}
168 
169 #undef ACPI_STA_DEV_VALID
170 
171 	if (count == 0)
172 		return 0;
173 
174 	aprint_debug_dev(sc->sc_dev, "pci devices:");
175 	TAILQ_FOREACH(ap, &acpi_pcidevlist, ap_list)
176 		acpi_pcidev_print(ap);
177 	aprint_debug("\n");
178 
179 	return count;
180 }
181 
182 /*
183  * acpi_pcidev_find:
184  *
185  *      Finds a PCI device in the ACPI name space.
186  *      The return status is either:
187  *      - AE_NOT_FOUND if no such device was found.
188  *      - AE_OK if one and only one such device was found.
189  */
190 ACPI_STATUS
191 acpi_pcidev_find(u_int segment, u_int bus, u_int device, u_int function,
192     ACPI_HANDLE *handlep)
193 {
194 	struct acpi_pcidev *ap;
195 	ACPI_HANDLE hdl;
196 
197 	hdl = NULL;
198 	TAILQ_FOREACH(ap, &acpi_pcidevlist, ap_list) {
199 		if (ap->ap_pciseg != segment)
200 			continue;
201 		if (ap->ap_pcibus != bus)
202 			continue;
203 		if (ap->ap_pcidev != device)
204 			continue;
205 		if (ap->ap_pcifunc != function)
206 			continue;
207 
208 		hdl = ap->ap_node->ad_handle;
209 		break;
210 	}
211 
212 	*handlep = hdl;
213 	return (hdl != NULL) ? AE_OK : AE_NOT_FOUND;
214 }
215