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>
| 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>
|
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 <dev/acpica/acpivar.h> 41 42#include <sys/pciio.h> 43#include <dev/pci/pcireg.h> 44#include <dev/pci/pcivar.h> 45#include <dev/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 52ACPI_MODULE_NAME("PCI") 53 54struct acpi_pci_devinfo { 55 struct pci_devinfo ap_dinfo; 56 ACPI_HANDLE ap_handle; 57}; 58 59static int acpi_pci_probe(device_t dev); 60static int acpi_pci_attach(device_t dev); 61static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 62 uintptr_t *result); 63static int acpi_pci_child_location_str_method(device_t cbdev, 64 device_t child, char *buf, size_t buflen); 65 66 67static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 68 int state); 69static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 70 void *context, void **status); 71 72static device_method_t acpi_pci_methods[] = { 73 /* Device interface */ 74 DEVMETHOD(device_probe, acpi_pci_probe), 75 DEVMETHOD(device_attach, acpi_pci_attach), 76 DEVMETHOD(device_shutdown, bus_generic_shutdown), 77 DEVMETHOD(device_suspend, pci_suspend), 78 DEVMETHOD(device_resume, pci_resume), 79 80 /* Bus interface */ 81 DEVMETHOD(bus_print_child, pci_print_child), 82 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch), 83 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 84 DEVMETHOD(bus_write_ivar, pci_write_ivar), 85 DEVMETHOD(bus_driver_added, pci_driver_added), 86 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 87 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 88 89 DEVMETHOD(bus_get_resource_list,pci_get_resource_list), 90 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 91 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 92 DEVMETHOD(bus_delete_resource, pci_delete_resource), 93 DEVMETHOD(bus_alloc_resource, pci_alloc_resource), 94 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 95 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 96 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 97 DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method), 98 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method), 99 100 /* PCI interface */ 101 DEVMETHOD(pci_read_config, pci_read_config_method), 102 DEVMETHOD(pci_write_config, pci_write_config_method), 103 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method), 104 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method), 105 DEVMETHOD(pci_enable_io, pci_enable_io_method), 106 DEVMETHOD(pci_disable_io, pci_disable_io_method), 107 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method), 108 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 109 DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method), 110 111 { 0, 0 } 112}; 113 114static driver_t acpi_pci_driver = { 115 "pci", 116 acpi_pci_methods, 117 0, /* no softc */ 118}; 119 120DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 121MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 122MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 123MODULE_VERSION(acpi_pci, 1); 124 125static int 126acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 127{ 128 struct acpi_pci_devinfo *dinfo; 129 130 switch (which) { 131 case ACPI_IVAR_HANDLE: 132 dinfo = device_get_ivars(child); 133 *result = (uintptr_t)dinfo->ap_handle; 134 return (0); 135 } 136 return (pci_read_ivar(dev, child, which, result)); 137} 138 139static int 140acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf, 141 size_t buflen) 142{ 143 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 144 145 pci_child_location_str_method(cbdev, child, buf, buflen); 146 147 if (dinfo->ap_handle) { 148 strlcat(buf, " path=", buflen); 149 strlcat(buf, acpi_name(dinfo->ap_handle), buflen); 150 } 151 return (0); 152} 153 154/* 155 * PCI power manangement 156 */ 157static int 158acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 159{ 160 ACPI_HANDLE h; 161 ACPI_STATUS status; 162 int acpi_state, old_state, error; 163 164 switch (state) { 165 case PCI_POWERSTATE_D0: 166 acpi_state = ACPI_STATE_D0; 167 break; 168 case PCI_POWERSTATE_D1: 169 acpi_state = ACPI_STATE_D1; 170 break; 171 case PCI_POWERSTATE_D2: 172 acpi_state = ACPI_STATE_D2; 173 break; 174 case PCI_POWERSTATE_D3: 175 acpi_state = ACPI_STATE_D3; 176 break; 177 default: 178 return (EINVAL); 179 } 180 181 /* 182 * We set the state using PCI Power Management outside of setting 183 * the ACPI state. This means that when powering down a device, we 184 * first shut it down using PCI, and then using ACPI, which lets ACPI 185 * try to power down any Power Resources that are now no longer used. 186 * When powering up a device, we let ACPI set the state first so that 187 * it can enable any needed Power Resources before changing the PCI 188 * power state. 189 */ 190 old_state = pci_get_powerstate(child); 191 if (old_state < state) { 192 error = pci_set_powerstate_method(dev, child, state); 193 if (error) 194 return (error); 195 }
| 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 <dev/acpica/acpivar.h> 41 42#include <sys/pciio.h> 43#include <dev/pci/pcireg.h> 44#include <dev/pci/pcivar.h> 45#include <dev/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 52ACPI_MODULE_NAME("PCI") 53 54struct acpi_pci_devinfo { 55 struct pci_devinfo ap_dinfo; 56 ACPI_HANDLE ap_handle; 57}; 58 59static int acpi_pci_probe(device_t dev); 60static int acpi_pci_attach(device_t dev); 61static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 62 uintptr_t *result); 63static int acpi_pci_child_location_str_method(device_t cbdev, 64 device_t child, char *buf, size_t buflen); 65 66 67static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 68 int state); 69static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 70 void *context, void **status); 71 72static device_method_t acpi_pci_methods[] = { 73 /* Device interface */ 74 DEVMETHOD(device_probe, acpi_pci_probe), 75 DEVMETHOD(device_attach, acpi_pci_attach), 76 DEVMETHOD(device_shutdown, bus_generic_shutdown), 77 DEVMETHOD(device_suspend, pci_suspend), 78 DEVMETHOD(device_resume, pci_resume), 79 80 /* Bus interface */ 81 DEVMETHOD(bus_print_child, pci_print_child), 82 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch), 83 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 84 DEVMETHOD(bus_write_ivar, pci_write_ivar), 85 DEVMETHOD(bus_driver_added, pci_driver_added), 86 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 87 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 88 89 DEVMETHOD(bus_get_resource_list,pci_get_resource_list), 90 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 91 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 92 DEVMETHOD(bus_delete_resource, pci_delete_resource), 93 DEVMETHOD(bus_alloc_resource, pci_alloc_resource), 94 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 95 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 96 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 97 DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method), 98 DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method), 99 100 /* PCI interface */ 101 DEVMETHOD(pci_read_config, pci_read_config_method), 102 DEVMETHOD(pci_write_config, pci_write_config_method), 103 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method), 104 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method), 105 DEVMETHOD(pci_enable_io, pci_enable_io_method), 106 DEVMETHOD(pci_disable_io, pci_disable_io_method), 107 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method), 108 DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method), 109 DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method), 110 111 { 0, 0 } 112}; 113 114static driver_t acpi_pci_driver = { 115 "pci", 116 acpi_pci_methods, 117 0, /* no softc */ 118}; 119 120DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 121MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1); 122MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 123MODULE_VERSION(acpi_pci, 1); 124 125static int 126acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 127{ 128 struct acpi_pci_devinfo *dinfo; 129 130 switch (which) { 131 case ACPI_IVAR_HANDLE: 132 dinfo = device_get_ivars(child); 133 *result = (uintptr_t)dinfo->ap_handle; 134 return (0); 135 } 136 return (pci_read_ivar(dev, child, which, result)); 137} 138 139static int 140acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf, 141 size_t buflen) 142{ 143 struct acpi_pci_devinfo *dinfo = device_get_ivars(child); 144 145 pci_child_location_str_method(cbdev, child, buf, buflen); 146 147 if (dinfo->ap_handle) { 148 strlcat(buf, " path=", buflen); 149 strlcat(buf, acpi_name(dinfo->ap_handle), buflen); 150 } 151 return (0); 152} 153 154/* 155 * PCI power manangement 156 */ 157static int 158acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 159{ 160 ACPI_HANDLE h; 161 ACPI_STATUS status; 162 int acpi_state, old_state, error; 163 164 switch (state) { 165 case PCI_POWERSTATE_D0: 166 acpi_state = ACPI_STATE_D0; 167 break; 168 case PCI_POWERSTATE_D1: 169 acpi_state = ACPI_STATE_D1; 170 break; 171 case PCI_POWERSTATE_D2: 172 acpi_state = ACPI_STATE_D2; 173 break; 174 case PCI_POWERSTATE_D3: 175 acpi_state = ACPI_STATE_D3; 176 break; 177 default: 178 return (EINVAL); 179 } 180 181 /* 182 * We set the state using PCI Power Management outside of setting 183 * the ACPI state. This means that when powering down a device, we 184 * first shut it down using PCI, and then using ACPI, which lets ACPI 185 * try to power down any Power Resources that are now no longer used. 186 * When powering up a device, we let ACPI set the state first so that 187 * it can enable any needed Power Resources before changing the PCI 188 * power state. 189 */ 190 old_state = pci_get_powerstate(child); 191 if (old_state < state) { 192 error = pci_set_powerstate_method(dev, child, state); 193 if (error) 194 return (error); 195 }
|
205 if (old_state > state) 206 return (pci_set_powerstate_method(dev, child, state)); 207 else 208 return (0); 209} 210 211static ACPI_STATUS 212acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 213 void **status) 214{ 215 struct acpi_pci_devinfo *dinfo; 216 device_t *devlist; 217 int devcount, i, func, slot; 218 UINT32 address; 219 220 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 221 222 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 223 return_ACPI_STATUS (AE_OK); 224 slot = address >> 16; 225 func = address & 0xffff; 226 if (device_get_children((device_t)context, &devlist, &devcount) != 0) 227 return_ACPI_STATUS (AE_OK); 228 for (i = 0; i < devcount; i++) { 229 dinfo = device_get_ivars(devlist[i]); 230 if (dinfo->ap_dinfo.cfg.func == func && 231 dinfo->ap_dinfo.cfg.slot == slot) { 232 dinfo->ap_handle = handle; 233 free(devlist, M_TEMP); 234 return_ACPI_STATUS (AE_OK); 235 } 236 } 237 free(devlist, M_TEMP); 238 return_ACPI_STATUS (AE_OK); 239} 240 241static int 242acpi_pci_probe(device_t dev) 243{ 244 245 if (pcib_get_bus(dev) < 0) 246 return (ENXIO); 247 if (acpi_get_handle(dev) == NULL) 248 return (ENXIO); 249 device_set_desc(dev, "ACPI PCI bus"); 250 return (0); 251} 252 253static int 254acpi_pci_attach(device_t dev) 255{ 256 int busno; 257 258 /* 259 * Since there can be multiple independantly numbered PCI 260 * busses on some large alpha systems, we can't use the unit 261 * number to decide what bus we are probing. We ask the parent 262 * pcib what our bus number is. 263 */ 264 busno = pcib_get_bus(dev); 265 if (bootverbose) 266 device_printf(dev, "physical bus=%d\n", busno); 267 268 /* 269 * First, PCI devices are added as in the normal PCI bus driver. 270 * Afterwards, the ACPI namespace under the bridge driver is 271 * walked to save ACPI handles to all the devices that appear in 272 * the ACPI namespace as immediate descendants of the bridge. 273 * 274 * XXX: Sometimes PCI devices show up in the ACPI namespace that 275 * pci_add_children() doesn't find. We currently just ignore 276 * these devices. 277 */ 278 pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo)); 279 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 280 acpi_pci_save_handle, dev, NULL); 281 282 return (bus_generic_attach(dev)); 283}
| 202 if (old_state > state) 203 return (pci_set_powerstate_method(dev, child, state)); 204 else 205 return (0); 206} 207 208static ACPI_STATUS 209acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 210 void **status) 211{ 212 struct acpi_pci_devinfo *dinfo; 213 device_t *devlist; 214 int devcount, i, func, slot; 215 UINT32 address; 216 217 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 218 219 if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address))) 220 return_ACPI_STATUS (AE_OK); 221 slot = address >> 16; 222 func = address & 0xffff; 223 if (device_get_children((device_t)context, &devlist, &devcount) != 0) 224 return_ACPI_STATUS (AE_OK); 225 for (i = 0; i < devcount; i++) { 226 dinfo = device_get_ivars(devlist[i]); 227 if (dinfo->ap_dinfo.cfg.func == func && 228 dinfo->ap_dinfo.cfg.slot == slot) { 229 dinfo->ap_handle = handle; 230 free(devlist, M_TEMP); 231 return_ACPI_STATUS (AE_OK); 232 } 233 } 234 free(devlist, M_TEMP); 235 return_ACPI_STATUS (AE_OK); 236} 237 238static int 239acpi_pci_probe(device_t dev) 240{ 241 242 if (pcib_get_bus(dev) < 0) 243 return (ENXIO); 244 if (acpi_get_handle(dev) == NULL) 245 return (ENXIO); 246 device_set_desc(dev, "ACPI PCI bus"); 247 return (0); 248} 249 250static int 251acpi_pci_attach(device_t dev) 252{ 253 int busno; 254 255 /* 256 * Since there can be multiple independantly numbered PCI 257 * busses on some large alpha systems, we can't use the unit 258 * number to decide what bus we are probing. We ask the parent 259 * pcib what our bus number is. 260 */ 261 busno = pcib_get_bus(dev); 262 if (bootverbose) 263 device_printf(dev, "physical bus=%d\n", busno); 264 265 /* 266 * First, PCI devices are added as in the normal PCI bus driver. 267 * Afterwards, the ACPI namespace under the bridge driver is 268 * walked to save ACPI handles to all the devices that appear in 269 * the ACPI namespace as immediate descendants of the bridge. 270 * 271 * XXX: Sometimes PCI devices show up in the ACPI namespace that 272 * pci_add_children() doesn't find. We currently just ignore 273 * these devices. 274 */ 275 pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo)); 276 AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 277 acpi_pci_save_handle, dev, NULL); 278 279 return (bus_generic_attach(dev)); 280}
|