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