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