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