1 /*-
2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/12.0/sys/dev/acpica/acpi_pci.c 299286 2016-05-09 20:50:21Z jhb $");
31
32 #include "opt_acpi.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
43
44 #include <dev/acpica/acpivar.h>
45 #include <dev/acpica/acpi_pcivar.h>
46
47 #include <sys/pciio.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pci_private.h>
51
52 #include "pcib_if.h"
53 #include "pci_if.h"
54
55 /* Hooks for the ACPI CA debugging infrastructure. */
56 #define _COMPONENT ACPI_BUS
57 ACPI_MODULE_NAME("PCI")
58
59 struct acpi_pci_devinfo {
60 struct pci_devinfo ap_dinfo;
61 ACPI_HANDLE ap_handle;
62 int ap_flags;
63 };
64
65 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods");
66
67 /* Be sure that ACPI and PCI power states are equivalent. */
68 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0);
69 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1);
70 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2);
71 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3);
72
73 static struct pci_devinfo *acpi_pci_alloc_devinfo(device_t dev);
74 static void acpi_pci_child_deleted(device_t dev, device_t child);
75 static int acpi_pci_child_location_str_method(device_t cbdev,
76 device_t child, char *buf, size_t buflen);
77 static int acpi_pci_probe(device_t dev);
78 static int acpi_pci_read_ivar(device_t dev, device_t child, int which,
79 uintptr_t *result);
80 static int acpi_pci_write_ivar(device_t dev, device_t child, int which,
81 uintptr_t value);
82 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
83 void *context, void **status);
84 static int acpi_pci_set_powerstate_method(device_t dev, device_t child,
85 int state);
86 static void acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child);
87 static bus_dma_tag_t acpi_pci_get_dma_tag(device_t bus, device_t child);
88
89 static device_method_t acpi_pci_methods[] = {
90 /* Device interface */
91 DEVMETHOD(device_probe, acpi_pci_probe),
92
93 /* Bus interface */
94 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar),
95 DEVMETHOD(bus_write_ivar, acpi_pci_write_ivar),
96 DEVMETHOD(bus_child_deleted, acpi_pci_child_deleted),
97 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
98 DEVMETHOD(bus_get_cpus, acpi_get_cpus),
99 DEVMETHOD(bus_get_dma_tag, acpi_pci_get_dma_tag),
100 DEVMETHOD(bus_get_domain, acpi_get_domain),
101
102 /* PCI interface */
103 DEVMETHOD(pci_alloc_devinfo, acpi_pci_alloc_devinfo),
104 DEVMETHOD(pci_child_added, acpi_pci_child_added),
105 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method),
106
107 DEVMETHOD_END
108 };
109
110 static devclass_t pci_devclass;
111
112 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc),
113 pci_driver);
114 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
115 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
116 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
117 MODULE_VERSION(acpi_pci, 1);
118
119 static struct pci_devinfo *
120 acpi_pci_alloc_devinfo(device_t dev)
121 {
122 struct acpi_pci_devinfo *dinfo;
123
124 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
125 return (&dinfo->ap_dinfo);
126 }
127
128 static int
129 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
130 {
131 struct acpi_pci_devinfo *dinfo;
132
133 dinfo = device_get_ivars(child);
134 switch (which) {
135 case ACPI_IVAR_HANDLE:
136 *result = (uintptr_t)dinfo->ap_handle;
137 return (0);
138 case ACPI_IVAR_FLAGS:
139 *result = (uintptr_t)dinfo->ap_flags;
140 return (0);
141 }
142 return (pci_read_ivar(dev, child, which, result));
143 }
144
145 static int
146 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
147 {
148 struct acpi_pci_devinfo *dinfo;
149
150 dinfo = device_get_ivars(child);
151 switch (which) {
152 case ACPI_IVAR_HANDLE:
153 dinfo->ap_handle = (ACPI_HANDLE)value;
154 return (0);
155 case ACPI_IVAR_FLAGS:
156 dinfo->ap_flags = (int)value;
157 return (0);
158 }
159 return (pci_write_ivar(dev, child, which, value));
160 }
161
162 static void
163 acpi_pci_child_deleted(device_t dev, device_t child)
164 {
165 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
166
167 if (acpi_get_device(dinfo->ap_handle) == child)
168 AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler);
169 pci_child_deleted(dev, child);
170 }
171
172 static int
173 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
174 size_t buflen)
175 {
176 struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
177 int pxm;
178 char buf2[32];
179
180 pci_child_location_str_method(cbdev, child, buf, buflen);
181
182 if (dinfo->ap_handle) {
183 strlcat(buf, " handle=", buflen);
184 strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
185
186 if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) {
187 snprintf(buf2, 32, " _PXM=%d", pxm);
188 strlcat(buf, buf2, buflen);
189 }
190 }
191 return (0);
192 }
193
194 /*
195 * PCI power manangement
196 */
197 static int
198 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
199 {
200 ACPI_HANDLE h;
201 ACPI_STATUS status;
202 int old_state, error;
203
204 error = 0;
205 if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
206 return (EINVAL);
207
208 /*
209 * We set the state using PCI Power Management outside of setting
210 * the ACPI state. This means that when powering down a device, we
211 * first shut it down using PCI, and then using ACPI, which lets ACPI
212 * try to power down any Power Resources that are now no longer used.
213 * When powering up a device, we let ACPI set the state first so that
214 * it can enable any needed Power Resources before changing the PCI
215 * power state.
216 */
217 ACPI_SERIAL_BEGIN(pci_powerstate);
218 old_state = pci_get_powerstate(child);
219 if (old_state < state && pci_do_power_suspend) {
220 error = pci_set_powerstate_method(dev, child, state);
221 if (error)
222 goto out;
223 }
224 h = acpi_get_handle(child);
225 status = acpi_pwr_switch_consumer(h, state);
226 if (ACPI_SUCCESS(status)) {
227 if (bootverbose)
228 device_printf(dev, "set ACPI power state D%d on %s\n",
229 state, acpi_name(h));
230 } else if (status != AE_NOT_FOUND)
231 device_printf(dev,
232 "failed to set ACPI power state D%d on %s: %s\n",
233 state, acpi_name(h), AcpiFormatException(status));
234 if (old_state > state && pci_do_power_resume)
235 error = pci_set_powerstate_method(dev, child, state);
236
237 out:
238 ACPI_SERIAL_END(pci_powerstate);
239 return (error);
240 }
241
242 static void
243 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
244 {
245 ACPI_STATUS status;
246 device_t child;
247
248 /*
249 * Occasionally a PCI device may show up as an ACPI device
250 * with a _HID. (For example, the TabletPC TC1000 has a
251 * second PCI-ISA bridge that has a _HID for an
252 * acpi_sysresource device.) In that case, leave ACPI-CA's
253 * device data pointing at the ACPI-enumerated device.
254 */
255 child = acpi_get_device(handle);
256 if (child != NULL) {
257 KASSERT(device_get_parent(child) ==
258 devclass_get_device(devclass_find("acpi"), 0),
259 ("%s: child (%s)'s parent is not acpi0", __func__,
260 acpi_name(handle)));
261 return;
262 }
263
264 /*
265 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
266 */
267 status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
268 if (ACPI_FAILURE(status))
269 printf("WARNING: Unable to attach object data to %s - %s\n",
270 acpi_name(handle), AcpiFormatException(status));
271 }
272
273 static ACPI_STATUS
274 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
275 void **status)
276 {
277 struct acpi_pci_devinfo *dinfo;
278 device_t child;
279 int func, slot;
280 UINT32 address;
281
282 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
283
284 child = context;
285 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
286 return_ACPI_STATUS (AE_OK);
287 slot = ACPI_ADR_PCI_SLOT(address);
288 func = ACPI_ADR_PCI_FUNC(address);
289 dinfo = device_get_ivars(child);
290 if (dinfo->ap_dinfo.cfg.func == func &&
291 dinfo->ap_dinfo.cfg.slot == slot) {
292 dinfo->ap_handle = handle;
293 acpi_pci_update_device(handle, child);
294 return_ACPI_STATUS (AE_CTRL_TERMINATE);
295 }
296 return_ACPI_STATUS (AE_OK);
297 }
298
299 void
300 acpi_pci_child_added(device_t dev, device_t child)
301 {
302
303 /*
304 * PCI devices are added via the bus scan in the normal PCI
305 * bus driver. As each device is added, the
306 * acpi_pci_child_added() callback walks the ACPI namespace
307 * under the bridge driver to save ACPI handles to all the
308 * devices that appear in the ACPI namespace as immediate
309 * descendants of the bridge.
310 *
311 * XXX: Sometimes PCI devices show up in the ACPI namespace that
312 * pci_add_children() doesn't find. We currently just ignore
313 * these devices.
314 */
315 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
316 acpi_pci_save_handle, NULL, child, NULL);
317 }
318
319 static int
320 acpi_pci_probe(device_t dev)
321 {
322
323 if (acpi_get_handle(dev) == NULL)
324 return (ENXIO);
325 device_set_desc(dev, "ACPI PCI bus");
326 return (BUS_PROBE_DEFAULT);
327 }
328
329 #ifdef ACPI_DMAR
330 bus_dma_tag_t dmar_get_dma_tag(device_t dev, device_t child);
331 static bus_dma_tag_t
332 acpi_pci_get_dma_tag(device_t bus, device_t child)
333 {
334 bus_dma_tag_t tag;
335
336 if (device_get_parent(child) == bus) {
337 /* try dmar and return if it works */
338 tag = dmar_get_dma_tag(bus, child);
339 } else
340 tag = NULL;
341 if (tag == NULL)
342 tag = pci_get_dma_tag(bus, child);
343 return (tag);
344 }
345 #else
346 static bus_dma_tag_t
347 acpi_pci_get_dma_tag(device_t bus, device_t child)
348 {
349
350 return (pci_get_dma_tag(bus, child));
351 }
352 #endif
353
Cache object: 03a9c39d5c5f5e452cfed28575320303
|