ACPI: Add missing ACPI_SERIAL_INIT()'s.
[dragonfly.git] / sys / dev / acpica5 / acpi_pci.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@kfreebsd.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  * __FBSDID("$FreeBSD: src/sys/dev/acpica/acpi_pci.c,v 1.31.2.1.6.1 2009/04/15 03:14:26 kensmith Exp $");
28  */
29
30 #include <sys/cdefs.h>
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38
39 #include "acpi.h"
40 #include "acpivar.h"
41
42 #include <sys/pciio.h>
43 #include <bus/pci/pcireg.h>
44 #include <bus/pci/pcivar.h>
45 #include <bus/pci/pci_private.h>
46
47 #include "pcib_if.h"
48 #include "pci_if.h"
49
50 /* Hooks for the ACPI CA debugging infrastructure. */
51 #define _COMPONENT      ACPI_BUS
52 ACPI_MODULE_NAME("PCI")
53
54 struct acpi_pci_devinfo {
55         struct pci_devinfo      ap_dinfo;
56         ACPI_HANDLE             ap_handle;
57         int                     ap_flags;
58 };
59
60 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods");
61
62 /* Be sure that ACPI and PCI power states are equivalent. */
63 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0);
64 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1);
65 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2);
66 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3);
67
68 static int      acpi_pci_attach(device_t dev);
69 static int      acpi_pci_suspend(device_t dev);
70 static int      acpi_pci_resume(device_t dev);
71 static int      acpi_pci_child_location_str_method(device_t cbdev,
72                     device_t child, char *buf, size_t buflen);
73 static int      acpi_pci_probe(device_t dev);
74 static int      acpi_pci_read_ivar(device_t dev, device_t child, int which,
75                     uintptr_t *result);
76 static int      acpi_pci_write_ivar(device_t dev, device_t child, int which,
77                     uintptr_t value);
78 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
79                     void *context, void **status);
80 static int      acpi_pci_set_powerstate_method(device_t dev, device_t child,
81                     int state);
82 static void     acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child);
83
84 static device_method_t acpi_pci_methods[] = {
85         /* Device interface */
86         DEVMETHOD(device_probe,         acpi_pci_probe),
87         DEVMETHOD(device_attach,        acpi_pci_attach),
88         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
89         DEVMETHOD(device_suspend,       acpi_pci_suspend),
90         DEVMETHOD(device_resume,        acpi_pci_resume),
91
92         /* Bus interface */
93         DEVMETHOD(bus_print_child,      pci_print_child),
94         DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
95         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
96         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
97         DEVMETHOD(bus_delete_resource,  pci_delete_resource),
98         DEVMETHOD(bus_alloc_resource,   pci_alloc_resource),
99         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
100         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
101         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
102         DEVMETHOD(bus_read_ivar,        acpi_pci_read_ivar),
103         DEVMETHOD(bus_write_ivar,       acpi_pci_write_ivar),
104         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
105         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
106         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
107         DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
108
109         /* PCI interface */
110         DEVMETHOD(pci_set_powerstate,   acpi_pci_set_powerstate_method),
111         DEVMETHOD(pci_read_config,      pci_read_config_method),
112         DEVMETHOD(pci_write_config,     pci_write_config_method),
113         DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
114         DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
115         DEVMETHOD(pci_enable_io,        pci_enable_io_method),
116         DEVMETHOD(pci_disable_io,       pci_disable_io_method),
117         DEVMETHOD(pci_get_powerstate,   pci_get_powerstate_method),
118         DEVMETHOD(pci_set_powerstate,   acpi_pci_set_powerstate_method),
119         DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method),
120
121         { 0, 0 }
122 };
123
124 static driver_t acpi_pci_driver = {
125         "pci",
126         acpi_pci_methods,
127         0,                      /* no softc */
128 };
129
130 static devclass_t pci_devclass;
131
132 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
133 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
134 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
135 MODULE_VERSION(acpi_pci, 1);
136
137 static int
138 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
139 {
140     struct acpi_pci_devinfo *dinfo;
141
142     dinfo = device_get_ivars(child);
143     switch (which) {
144     case ACPI_IVAR_HANDLE:
145         *result = (uintptr_t)dinfo->ap_handle;
146         return (0);
147     case ACPI_IVAR_FLAGS:
148         *result = (uintptr_t)dinfo->ap_flags;
149         return (0);
150     }
151     return (pci_read_ivar(dev, child, which, result));
152 }
153
154 static int
155 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
156 {
157     struct acpi_pci_devinfo *dinfo;
158
159     dinfo = device_get_ivars(child);
160     switch (which) {
161     case ACPI_IVAR_HANDLE:
162         dinfo->ap_handle = (ACPI_HANDLE)value;
163         return (0);
164     case ACPI_IVAR_FLAGS:
165         dinfo->ap_flags = (int)value;
166         return (0);
167     }
168     return (pci_write_ivar(dev, child, which, value));
169 }
170
171 static int
172 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
173     size_t buflen)
174 {
175     struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
176
177     pci_child_location_str_method(cbdev, child, buf, buflen);
178
179     if (dinfo->ap_handle) {
180         strlcat(buf, " handle=", buflen);
181         strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
182     }
183     return (0);
184 }
185
186 /*
187  * PCI power manangement
188  */
189 static int
190 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
191 {
192         ACPI_HANDLE h;
193         ACPI_STATUS status;
194         int old_state, error;
195
196         error = 0;
197         if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
198                 return (EINVAL);
199
200         /*
201          * We set the state using PCI Power Management outside of setting
202          * the ACPI state.  This means that when powering down a device, we
203          * first shut it down using PCI, and then using ACPI, which lets ACPI
204          * try to power down any Power Resources that are now no longer used.
205          * When powering up a device, we let ACPI set the state first so that
206          * it can enable any needed Power Resources before changing the PCI
207          * power state.
208          */
209         ACPI_SERIAL_BEGIN(pci_powerstate);
210         old_state = pci_get_powerstate(child);
211         if (old_state < state) {
212                 error = pci_set_powerstate_method(dev, child, state);
213                 if (error)
214                         goto out;
215         }
216         h = acpi_get_handle(child);
217         status = acpi_pwr_switch_consumer(h, state);
218         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
219                 device_printf(dev,
220                     "Failed to set ACPI power state D%d on %s: %s\n",
221                     state, acpi_name(h), AcpiFormatException(status));
222         if (old_state > state)
223                 error = pci_set_powerstate_method(dev, child, state);
224
225 out:
226         ACPI_SERIAL_END(pci_powerstate);
227         return (error);
228 }
229
230 static void
231 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
232 {
233         ACPI_STATUS status;
234         device_t child;
235
236         /*
237          * Lookup and remove the unused device that acpi0 creates when it walks
238          * the namespace creating devices.
239          */
240         child = acpi_get_device(handle);
241         if (child != NULL) {
242                 if (device_is_alive(child)) {
243                         /*
244                          * The TabletPC TC1000 has a second PCI-ISA bridge
245                          * that has a _HID for an acpi_sysresource device.
246                          * In that case, leave ACPI-CA's device data pointing
247                          * at the ACPI-enumerated device.
248                          */
249                         device_printf(child,
250                             "Conflicts with PCI device %d:%d:%d\n",
251                             pci_get_bus(pci_child), pci_get_slot(pci_child),
252                             pci_get_function(pci_child));
253                         return;
254                 }
255                 KASSERT(device_get_parent(child) ==
256                     devclass_get_device(devclass_find("acpi"), 0),
257                     ("%s: child (%s)'s parent is not acpi0", __func__,
258                     acpi_name(handle)));
259                 device_delete_child(device_get_parent(child), child);
260         }
261
262         /*
263          * Update ACPI-CA to use the PCI enumerated device_t for this handle.
264          */
265         status = AcpiDetachData(handle, acpi_fake_objhandler);
266         if (ACPI_FAILURE(status))
267                 kprintf("WARNING: Unable to detach object data from %s - %s\n",
268                     acpi_name(handle), AcpiFormatException(status));
269         status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
270         if (ACPI_FAILURE(status))
271                 kprintf("WARNING: Unable to attach object data to %s - %s\n",
272                     acpi_name(handle), AcpiFormatException(status));
273 }
274
275 static ACPI_STATUS
276 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
277     void **status)
278 {
279         struct acpi_pci_devinfo *dinfo;
280         device_t *devlist;
281         int devcount, i, func, slot;
282         UINT32 address;
283
284         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
285
286         if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
287                 return_ACPI_STATUS (AE_OK);
288         slot = ACPI_ADR_PCI_SLOT(address);
289         func = ACPI_ADR_PCI_FUNC(address);
290         if (device_get_children((device_t)context, &devlist, &devcount) != 0)
291                 return_ACPI_STATUS (AE_OK);
292         for (i = 0; i < devcount; i++) {
293                 dinfo = device_get_ivars(devlist[i]);
294                 if (dinfo->ap_dinfo.cfg.func == func &&
295                     dinfo->ap_dinfo.cfg.slot == slot) {
296                         dinfo->ap_handle = handle;
297                         acpi_pci_update_device(handle, devlist[i]);
298                         break;
299                 }
300         }
301         kfree(devlist, M_TEMP);
302         return_ACPI_STATUS (AE_OK);
303 }
304
305 static int
306 acpi_pci_probe(device_t dev)
307 {
308
309         if (pcib_get_bus(dev) < 0)
310                 return (ENXIO);
311         if (acpi_get_handle(dev) == NULL)
312                 return (ENXIO);
313         device_set_desc(dev, "ACPI PCI bus");
314         return (0);
315 }
316
317 static int
318 acpi_pci_attach(device_t dev)
319 {
320         int busno, domain;
321
322         /*
323          * Since there can be multiple independantly numbered PCI
324          * busses on systems with multiple PCI domains, we can't use
325          * the unit number to decide which bus we are probing. We ask
326          * the parent pcib what our domain and bus numbers are.
327          */
328         busno = pcib_get_bus(dev);
329         domain = pcib_get_domain(dev);
330         if (bootverbose)
331                device_printf(dev, "domain=%d, physical bus=%d\n",
332                     domain, busno);
333
334         ACPI_SERIAL_INIT(pci_powerstate);
335
336         /*
337          * First, PCI devices are added as in the normal PCI bus driver.
338          * Afterwards, the ACPI namespace under the bridge driver is
339          * walked to save ACPI handles to all the devices that appear in
340          * the ACPI namespace as immediate descendants of the bridge.
341          *
342          * XXX: Sometimes PCI devices show up in the ACPI namespace that
343          * pci_add_children() doesn't find.  We currently just ignore
344          * these devices.
345          */
346         pci_add_children(dev, domain, busno, sizeof(struct acpi_pci_devinfo));
347         AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
348             acpi_pci_save_handle, dev, NULL);
349
350         return (bus_generic_attach(dev));
351 }
352
353 int
354 acpi_pci_suspend(device_t dev)
355 {
356         int dstate, error, i, numdevs;
357         device_t acpi_dev, child, *devlist;
358         struct pci_devinfo *dinfo;
359         /*
360          * Save the PCI configuration space for each child and set the
361          * device in the appropriate power state for this sleep state.
362          */
363         acpi_dev = NULL;
364         acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
365         device_get_children(dev, &devlist, &numdevs);
366         for (i = 0; i < numdevs; i++) {
367                 child = devlist[i];
368                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
369                 pci_cfg_save(child, dinfo, 0);
370         }
371         /* Suspend devices before potentially powering them down. */
372         error = bus_generic_suspend(dev);
373         if (error) {
374                 kfree(devlist, M_TEMP);
375                 return (error);
376         }
377         /*
378          * Always set the device to D3.  If ACPI suggests a different
379          * power state, use it instead.  If ACPI is not present, the
380          * firmware is responsible for managing device power.  Skip
381          * children who aren't attached since they are powered down
382          * separately.  Only manage type 0 devices for now.
383          */
384         for (i = 0; acpi_dev && i < numdevs; i++) {
385                 child = devlist[i];
386                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
387                 if (device_is_attached(child) && dinfo->cfg.hdrtype == 0) {
388                         dstate = PCI_POWERSTATE_D3;
389                         ACPI_PWR_FOR_SLEEP(acpi_dev, child, &dstate);
390                         pci_set_powerstate(child, dstate);
391                 }
392         }
393         kfree(devlist, M_TEMP);
394         return (0);
395 }
396
397 int
398 acpi_pci_resume(device_t dev)
399 {
400         int i, numdevs;
401         device_t acpi_dev, child, *devlist;
402         struct pci_devinfo *dinfo;
403         /*
404          * Set each child to D0 and restore its PCI configuration space.
405          */
406         acpi_dev = NULL;
407         acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
408         device_get_children(dev, &devlist, &numdevs);
409         for (i = 0; i < numdevs; i++) {
410                 /*
411                  * Notify ACPI we're going to D0 but ignore the result.  If
412                  * ACPI is not present, the firmware is responsible for
413                  * managing device power.  Only manage type 0 devices for now.
414                  */
415                 child = devlist[i];
416                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
417                 if (acpi_dev && device_is_attached(child) &&
418                     dinfo->cfg.hdrtype == 0) {
419                         ACPI_PWR_FOR_SLEEP(acpi_dev, child, NULL);
420                         pci_set_powerstate(child, PCI_POWERSTATE_D0);
421                 }
422                 /* Now the device is powered up, restore its config space. */
423                 pci_cfg_restore(child, dinfo);
424         }
425         kfree(devlist, M_TEMP);
426         return (bus_generic_resume(dev));
427 }