1 /*- 2 * Copyright 1998 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * This code implements a system driver for legacy systems that do not 35 * support ACPI or when ACPI support is not present in the kernel. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 #include <sys/cpu.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/module.h> 45 #include <machine/bus.h> 46 #include <sys/pcpu.h> 47 #include <sys/rman.h> 48 #include <sys/smp.h> 49 50 #include <machine/clock.h> 51 #include <machine/resource.h> 52 #include <x86/legacyvar.h> 53 54 static MALLOC_DEFINE(M_LEGACYDEV, "legacydrv", "legacy system device"); 55 struct legacy_device { 56 int lg_pcibus; 57 int lg_pcislot; 58 int lg_pcifunc; 59 }; 60 61 #define DEVTOAT(dev) ((struct legacy_device *)device_get_ivars(dev)) 62 63 static int legacy_probe(device_t); 64 static int legacy_attach(device_t); 65 static int legacy_print_child(device_t, device_t); 66 static device_t legacy_add_child(device_t bus, u_int order, const char *name, 67 int unit); 68 static int legacy_read_ivar(device_t, device_t, int, uintptr_t *); 69 static int legacy_write_ivar(device_t, device_t, int, uintptr_t); 70 71 static device_method_t legacy_methods[] = { 72 /* Device interface */ 73 DEVMETHOD(device_probe, legacy_probe), 74 DEVMETHOD(device_attach, legacy_attach), 75 DEVMETHOD(device_detach, bus_generic_detach), 76 DEVMETHOD(device_shutdown, bus_generic_shutdown), 77 DEVMETHOD(device_suspend, bus_generic_suspend), 78 DEVMETHOD(device_resume, bus_generic_resume), 79 80 /* Bus interface */ 81 DEVMETHOD(bus_print_child, legacy_print_child), 82 DEVMETHOD(bus_add_child, legacy_add_child), 83 DEVMETHOD(bus_read_ivar, legacy_read_ivar), 84 DEVMETHOD(bus_write_ivar, legacy_write_ivar), 85 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 86 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 87 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 88 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 89 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 90 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 91 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 92 93 { 0, 0 } 94 }; 95 96 static driver_t legacy_driver = { 97 "legacy", 98 legacy_methods, 99 1, /* no softc */ 100 }; 101 static devclass_t legacy_devclass; 102 103 DRIVER_MODULE(legacy, nexus, legacy_driver, legacy_devclass, 0, 0); 104 105 static int 106 legacy_probe(device_t dev) 107 { 108 109 device_set_desc(dev, "legacy system"); 110 device_quiet(dev); 111 return (0); 112 } 113 114 static int 115 legacy_attach(device_t dev) 116 { 117 device_t child; 118 119 /* 120 * Let our child drivers identify any child devices that they 121 * can find. Once that is done attach any devices that we 122 * found. 123 */ 124 bus_generic_probe(dev); 125 bus_generic_attach(dev); 126 127 /* 128 * If we didn't see ISA on a pci bridge, create some 129 * connection points now so they show up "on motherboard". 130 */ 131 if (!devclass_get_device(devclass_find("isa"), 0)) { 132 child = BUS_ADD_CHILD(dev, 0, "isa", 0); 133 if (child == NULL) 134 panic("legacy_attach isa"); 135 device_probe_and_attach(child); 136 } 137 138 return 0; 139 } 140 141 static int 142 legacy_print_child(device_t bus, device_t child) 143 { 144 struct legacy_device *atdev = DEVTOAT(child); 145 int retval = 0; 146 147 retval += bus_print_child_header(bus, child); 148 if (atdev->lg_pcibus != -1) 149 retval += printf(" pcibus %d", atdev->lg_pcibus); 150 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */ 151 152 return (retval); 153 } 154 155 static device_t 156 legacy_add_child(device_t bus, u_int order, const char *name, int unit) 157 { 158 device_t child; 159 struct legacy_device *atdev; 160 161 atdev = malloc(sizeof(struct legacy_device), M_LEGACYDEV, 162 M_NOWAIT | M_ZERO); 163 if (atdev == NULL) 164 return(NULL); 165 atdev->lg_pcibus = -1; 166 atdev->lg_pcislot = -1; 167 atdev->lg_pcifunc = -1; 168 169 child = device_add_child_ordered(bus, order, name, unit); 170 if (child == NULL) 171 free(atdev, M_LEGACYDEV); 172 else 173 /* should we free this in legacy_child_detached? */ 174 device_set_ivars(child, atdev); 175 176 return (child); 177 } 178 179 static int 180 legacy_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 181 { 182 struct legacy_device *atdev = DEVTOAT(child); 183 184 switch (which) { 185 case LEGACY_IVAR_PCIDOMAIN: 186 *result = 0; 187 break; 188 case LEGACY_IVAR_PCIBUS: 189 *result = atdev->lg_pcibus; 190 break; 191 case LEGACY_IVAR_PCISLOT: 192 *result = atdev->lg_pcislot; 193 break; 194 case LEGACY_IVAR_PCIFUNC: 195 *result = atdev->lg_pcifunc; 196 break; 197 default: 198 return ENOENT; 199 } 200 return 0; 201 } 202 203 204 static int 205 legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 206 { 207 struct legacy_device *atdev = DEVTOAT(child); 208 209 switch (which) { 210 case LEGACY_IVAR_PCIDOMAIN: 211 return EINVAL; 212 case LEGACY_IVAR_PCIBUS: 213 atdev->lg_pcibus = value; 214 break; 215 case LEGACY_IVAR_PCISLOT: 216 atdev->lg_pcislot = value; 217 break; 218 case LEGACY_IVAR_PCIFUNC: 219 atdev->lg_pcifunc = value; 220 break; 221 default: 222 return ENOENT; 223 } 224 return 0; 225 } 226 227 /* 228 * Legacy CPU attachment when ACPI is not available. Drivers like 229 * cpufreq(4) hang off this. 230 */ 231 static void cpu_identify(driver_t *driver, device_t parent); 232 static int cpu_read_ivar(device_t dev, device_t child, int index, 233 uintptr_t *result); 234 static device_t cpu_add_child(device_t bus, u_int order, const char *name, 235 int unit); 236 static struct resource_list *cpu_get_rlist(device_t dev, device_t child); 237 238 struct cpu_device { 239 struct resource_list cd_rl; 240 struct pcpu *cd_pcpu; 241 }; 242 243 static device_method_t cpu_methods[] = { 244 /* Device interface */ 245 DEVMETHOD(device_identify, cpu_identify), 246 DEVMETHOD(device_probe, bus_generic_probe), 247 DEVMETHOD(device_attach, bus_generic_attach), 248 DEVMETHOD(device_detach, bus_generic_detach), 249 DEVMETHOD(device_shutdown, bus_generic_shutdown), 250 DEVMETHOD(device_suspend, bus_generic_suspend), 251 DEVMETHOD(device_resume, bus_generic_resume), 252 253 /* Bus interface */ 254 DEVMETHOD(bus_add_child, cpu_add_child), 255 DEVMETHOD(bus_read_ivar, cpu_read_ivar), 256 DEVMETHOD(bus_get_resource_list, cpu_get_rlist), 257 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 258 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 259 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 260 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 261 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 262 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 263 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 264 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 265 266 DEVMETHOD_END 267 }; 268 269 static driver_t cpu_driver = { 270 "cpu", 271 cpu_methods, 272 1, /* no softc */ 273 }; 274 static devclass_t cpu_devclass; 275 DRIVER_MODULE(cpu, legacy, cpu_driver, cpu_devclass, 0, 0); 276 277 static void 278 cpu_identify(driver_t *driver, device_t parent) 279 { 280 device_t child; 281 int i; 282 283 /* 284 * Attach a cpuX device for each CPU. We use an order of 150 285 * so that these devices are attached after the Host-PCI 286 * bridges (which are added at order 100). 287 */ 288 CPU_FOREACH(i) { 289 child = BUS_ADD_CHILD(parent, 150, "cpu", i); 290 if (child == NULL) 291 panic("legacy_attach cpu"); 292 } 293 } 294 295 static device_t 296 cpu_add_child(device_t bus, u_int order, const char *name, int unit) 297 { 298 struct cpu_device *cd; 299 device_t child; 300 struct pcpu *pc; 301 302 if ((cd = malloc(sizeof(*cd), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) 303 return (NULL); 304 305 resource_list_init(&cd->cd_rl); 306 pc = pcpu_find(device_get_unit(bus)); 307 cd->cd_pcpu = pc; 308 309 child = device_add_child_ordered(bus, order, name, unit); 310 if (child != NULL) { 311 pc->pc_device = child; 312 device_set_ivars(child, cd); 313 } else 314 free(cd, M_DEVBUF); 315 return (child); 316 } 317 318 static struct resource_list * 319 cpu_get_rlist(device_t dev, device_t child) 320 { 321 struct cpu_device *cpdev; 322 323 cpdev = device_get_ivars(child); 324 return (&cpdev->cd_rl); 325 } 326 327 static int 328 cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 329 { 330 struct cpu_device *cpdev; 331 332 switch (index) { 333 case CPU_IVAR_PCPU: 334 cpdev = device_get_ivars(child); 335 *result = (uintptr_t)cpdev->cd_pcpu; 336 break; 337 case CPU_IVAR_NOMINAL_MHZ: 338 if (tsc_is_invariant) { 339 *result = (uintptr_t)(atomic_load_acq_64(&tsc_freq) / 340 1000000); 341 break; 342 } 343 /* FALLTHROUGH */ 344 default: 345 return (ENOENT); 346 } 347 return (0); 348 } 349