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 * $FreeBSD$ 29 * 30 */ 31 32 #include "opt_bus.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 "acpi.h" 42 43 #include <dev/acpica/acpivar.h> 44 45 #include <sys/pciio.h> 46 #include <dev/pci/pcireg.h> 47 #include <dev/pci/pcivar.h> 48 #include <dev/pci/pci_private.h> 49 50 #include "pcib_if.h" 51 #include "pci_if.h" 52 53 /* 54 * Hooks for the ACPI CA debugging infrastructure 55 */ 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 }; 63 64 static int acpi_pci_probe(device_t dev); 65 static int acpi_pci_attach(device_t dev); 66 static int acpi_pci_read_ivar(device_t dev, device_t child, int which, 67 uintptr_t *result); 68 #if 0 69 static int acpi_pci_set_powerstate_method(device_t dev, device_t child, 70 int state); 71 static int acpi_pci_get_powerstate_method(device_t dev, device_t child); 72 #endif 73 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, 74 void *context, void **status); 75 76 static device_method_t acpi_pci_methods[] = { 77 /* Device interface */ 78 DEVMETHOD(device_probe, acpi_pci_probe), 79 DEVMETHOD(device_attach, acpi_pci_attach), 80 DEVMETHOD(device_shutdown, bus_generic_shutdown), 81 DEVMETHOD(device_suspend, bus_generic_suspend), 82 DEVMETHOD(device_resume, bus_generic_resume), 83 84 /* Bus interface */ 85 DEVMETHOD(bus_print_child, pci_print_child), 86 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch), 87 DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar), 88 DEVMETHOD(bus_write_ivar, pci_write_ivar), 89 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 90 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 91 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 92 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 102 /* PCI interface */ 103 DEVMETHOD(pci_read_config, pci_read_config_method), 104 DEVMETHOD(pci_write_config, pci_write_config_method), 105 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method), 106 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method), 107 DEVMETHOD(pci_enable_io, pci_enable_io_method), 108 DEVMETHOD(pci_disable_io, pci_disable_io_method), 109 /* XXX: We should override these two. */ 110 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method), 111 DEVMETHOD(pci_set_powerstate, pci_set_powerstate_method), 112 113 { 0, 0 } 114 }; 115 116 static driver_t acpi_pci_driver = { 117 "pci", 118 acpi_pci_methods, 119 0, /* no softc */ 120 }; 121 122 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0); 123 MODULE_VERSION(acpi_pci, 1); 124 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1); 125 126 static int 127 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 128 { 129 struct acpi_pci_devinfo *dinfo; 130 131 switch (which) { 132 case ACPI_IVAR_HANDLE: 133 dinfo = device_get_ivars(child); 134 *result = (uintptr_t)dinfo->ap_handle; 135 return(0); 136 } 137 return(pci_read_ivar(dev, child, which, result)); 138 } 139 140 #if 0 141 /* 142 * PCI power manangement 143 */ 144 static int 145 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state) 146 { 147 /* XXX: TODO */ 148 return (ENXIO); 149 } 150 151 static int 152 acpi_pci_get_powerstate_method(device_t dev, device_t child) 153 { 154 /* XXX: TODO */ 155 return (ENXIO); 156 } 157 #endif 158 159 static ACPI_STATUS 160 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context, 161 void **status) 162 { 163 struct acpi_pci_devinfo *dinfo; 164 device_t *devlist; 165 int devcount, i, func, slot; 166 UINT32 address; 167 168 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 169 170 if (ACPI_FAILURE(acpi_EvaluateInteger(handle, "_ADR", &address))) 171 return_ACPI_STATUS(AE_OK); 172 slot = address >> 16; 173 func = address & 0xffff; 174 if (device_get_children((device_t)context, &devlist, &devcount) != 0) 175 return_ACPI_STATUS(AE_OK); 176 for (i = 0; i < devcount; i++) { 177 dinfo = device_get_ivars(devlist[i]); 178 if (dinfo->ap_dinfo.cfg.func == func && 179 dinfo->ap_dinfo.cfg.slot == slot) { 180 dinfo->ap_handle = handle; 181 free(devlist, M_TEMP); 182 return_ACPI_STATUS(AE_OK); 183 } 184 } 185 free(devlist, M_TEMP); 186 return_ACPI_STATUS(AE_OK); 187 } 188 189 static int 190 acpi_pci_probe(device_t dev) 191 { 192 193 if (pcib_get_bus(dev) < 0) 194 return (ENXIO); 195 196 device_set_desc(dev, "ACPI PCI bus"); 197 198 if (acpi_get_handle(dev) == NULL) 199 return (ENXIO); 200 return (0); 201 } 202 203 static int 204 acpi_pci_attach(device_t dev) 205 { 206 int busno; 207 208 /* 209 * Since there can be multiple independantly numbered PCI 210 * busses on some large alpha systems, we can't use the unit 211 * number to decide what bus we are probing. We ask the parent 212 * pcib what our bus number is. 213 */ 214 busno = pcib_get_bus(dev); 215 if (bootverbose) 216 device_printf(dev, "physical bus=%d\n", busno); 217 218 /* 219 * First, PCI devices are added as in the normal PCI bus driver. 220 * Afterwards, the ACPI namespace under the bridge driver is 221 * walked to save ACPI handles to all the devices that appear in 222 * the ACPI namespace as immediate descendants of the bridge. 223 * 224 * XXX: Sometimes PCI devices show up in the ACPI namespace that 225 * pci_add_children() doesn't find. We currently just ignore 226 * these devices. 227 */ 228 pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo)); 229 (void) AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1, 230 acpi_pci_save_handle, dev, NULL); 231 232 return (bus_generic_attach(dev)); 233 } 234