1 /*- 2 * Copyright (c) 2000, 2001 Michael Smith 3 * Copyright (c) 2000 BSDi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include "opt_acpi.h" 30 #include <sys/param.h> 31 #include <sys/bus.h> 32 #include <sys/eventhandler.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/sysctl.h> 36 #include <sys/timetc.h> 37 #include <sys/power.h> 38 39 #include <machine/bus.h> 40 #include <machine/resource.h> 41 #include <sys/rman.h> 42 43 #include <contrib/dev/acpica/include/acpi.h> 44 #include <contrib/dev/acpica/include/accommon.h> 45 46 #include <dev/acpica/acpivar.h> 47 #include <dev/pci/pcivar.h> 48 49 /* 50 * A timecounter based on the free-running ACPI timer. 51 * 52 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>. 53 */ 54 55 /* Hooks for the ACPI CA debugging infrastructure */ 56 #define _COMPONENT ACPI_TIMER 57 ACPI_MODULE_NAME("TIMER") 58 59 static device_t acpi_timer_dev; 60 static struct resource *acpi_timer_reg; 61 static bus_space_handle_t acpi_timer_bsh; 62 static bus_space_tag_t acpi_timer_bst; 63 static eventhandler_tag acpi_timer_eh; 64 65 static u_int acpi_timer_frequency = 14318182 / 4; 66 67 static void acpi_timer_identify(driver_t *driver, device_t parent); 68 static int acpi_timer_probe(device_t dev); 69 static int acpi_timer_attach(device_t dev); 70 static void acpi_timer_resume_handler(struct timecounter *, 71 enum power_stype); 72 static void acpi_timer_suspend_handler(struct timecounter *, 73 enum power_stype); 74 static u_int acpi_timer_get_timecount(struct timecounter *tc); 75 static u_int acpi_timer_get_timecount_safe(struct timecounter *tc); 76 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS); 77 78 static device_method_t acpi_timer_methods[] = { 79 DEVMETHOD(device_identify, acpi_timer_identify), 80 DEVMETHOD(device_probe, acpi_timer_probe), 81 DEVMETHOD(device_attach, acpi_timer_attach), 82 83 DEVMETHOD_END 84 }; 85 86 static driver_t acpi_timer_driver = { 87 "acpi_timer", 88 acpi_timer_methods, 89 0, 90 }; 91 92 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, 0, 0); 93 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1); 94 95 static struct timecounter acpi_timer_timecounter = { 96 acpi_timer_get_timecount_safe, /* get_timecount function */ 97 0, /* no poll_pps */ 98 0, /* no default counter_mask */ 99 0, /* no default frequency */ 100 "ACPI", /* name */ 101 -1 /* quality (chosen later) */ 102 }; 103 104 static __inline uint32_t 105 acpi_timer_read(void) 106 { 107 108 return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0)); 109 } 110 111 /* 112 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources 113 * we will be using. 114 */ 115 static void 116 acpi_timer_identify(driver_t *driver, device_t parent) 117 { 118 device_t dev; 119 rman_res_t rlen, rstart; 120 int rid, rtype; 121 122 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 123 124 if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) || 125 acpi_timer_dev || AcpiGbl_FADT.PmTimerLength == 0) 126 return_VOID; 127 128 if ((dev = BUS_ADD_CHILD(parent, 2, "acpi_timer", 0)) == NULL) { 129 device_printf(parent, "could not add acpi_timer0\n"); 130 return_VOID; 131 } 132 acpi_timer_dev = dev; 133 134 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) { 135 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 136 rtype = SYS_RES_MEMORY; 137 break; 138 case ACPI_ADR_SPACE_SYSTEM_IO: 139 rtype = SYS_RES_IOPORT; 140 break; 141 default: 142 return_VOID; 143 } 144 rid = 0; 145 rlen = AcpiGbl_FADT.PmTimerLength; 146 rstart = AcpiGbl_FADT.XPmTimerBlock.Address; 147 if (bus_set_resource(dev, rtype, rid, rstart, rlen)) 148 device_printf(dev, "couldn't set resource (%s 0x%jx+0x%jx)\n", 149 (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen); 150 return_VOID; 151 } 152 153 static int 154 acpi_timer_probe(device_t dev) 155 { 156 int rid, rtype; 157 158 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 159 160 if (dev != acpi_timer_dev) 161 return (ENXIO); 162 163 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) { 164 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 165 rtype = SYS_RES_MEMORY; 166 break; 167 case ACPI_ADR_SPACE_SYSTEM_IO: 168 rtype = SYS_RES_IOPORT; 169 break; 170 default: 171 return (ENXIO); 172 } 173 rid = 0; 174 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 175 if (acpi_timer_reg == NULL) { 176 device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n", 177 (rtype == SYS_RES_IOPORT) ? "port" : "mem", 178 (u_long)AcpiGbl_FADT.XPmTimerBlock.Address); 179 return (ENXIO); 180 } 181 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 182 acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 183 if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) 184 acpi_timer_timecounter.tc_counter_mask = 0xffffffff; 185 else 186 acpi_timer_timecounter.tc_counter_mask = 0x00ffffff; 187 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 188 acpi_timer_timecounter.tc_flags = TC_FLAGS_SUSPEND_SAFE; 189 190 acpi_timer_timecounter.tc_name = "ACPI-fast"; 191 acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount; 192 acpi_timer_timecounter.tc_quality = 900; 193 tc_init(&acpi_timer_timecounter); 194 195 device_set_descf(dev, "%d-bit timer at %u.%06uMHz", 196 (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) != 0 ? 32 : 24, 197 acpi_timer_frequency / 1000000, acpi_timer_frequency % 1000000); 198 199 /* Release the resource, we'll allocate it again during attach. */ 200 bus_release_resource(dev, rtype, rid, acpi_timer_reg); 201 return (0); 202 } 203 204 static int 205 acpi_timer_attach(device_t dev) 206 { 207 int rid, rtype; 208 209 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 210 211 switch (AcpiGbl_FADT.XPmTimerBlock.SpaceId) { 212 case ACPI_ADR_SPACE_SYSTEM_MEMORY: 213 rtype = SYS_RES_MEMORY; 214 break; 215 case ACPI_ADR_SPACE_SYSTEM_IO: 216 rtype = SYS_RES_IOPORT; 217 break; 218 default: 219 return (ENXIO); 220 } 221 rid = 0; 222 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 223 if (acpi_timer_reg == NULL) 224 return (ENXIO); 225 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg); 226 acpi_timer_bst = rman_get_bustag(acpi_timer_reg); 227 228 /* Register suspend event handler. */ 229 if (EVENTHANDLER_REGISTER(power_suspend, acpi_timer_suspend_handler, 230 &acpi_timer_timecounter, EVENTHANDLER_PRI_LAST) == NULL) 231 device_printf(dev, "failed to register suspend event handler\n"); 232 233 return (0); 234 } 235 236 static void 237 acpi_timer_resume_handler(struct timecounter *newtc, enum power_stype stype) 238 { 239 struct timecounter *tc; 240 241 tc = timecounter; 242 if (tc != newtc) { 243 if (bootverbose) 244 device_printf(acpi_timer_dev, 245 "restoring timecounter, %s -> %s\n", 246 tc->tc_name, newtc->tc_name); 247 (void)newtc->tc_get_timecount(newtc); 248 timecounter = newtc; 249 } 250 } 251 252 static void 253 acpi_timer_suspend_handler(struct timecounter *newtc, enum power_stype stype) 254 { 255 struct timecounter *tc; 256 257 /* Deregister existing resume event handler. */ 258 if (acpi_timer_eh != NULL) { 259 EVENTHANDLER_DEREGISTER(power_resume, acpi_timer_eh); 260 acpi_timer_eh = NULL; 261 } 262 263 if ((timecounter->tc_flags & TC_FLAGS_SUSPEND_SAFE) != 0) { 264 /* 265 * If we are using a suspend safe timecounter, don't 266 * save/restore it across suspend/resume. 267 */ 268 return; 269 } 270 271 KASSERT(newtc == &acpi_timer_timecounter, 272 ("acpi_timer_suspend_handler: wrong timecounter")); 273 274 tc = timecounter; 275 if (tc != newtc) { 276 if (bootverbose) 277 device_printf(acpi_timer_dev, 278 "switching timecounter, %s -> %s\n", 279 tc->tc_name, newtc->tc_name); 280 (void)acpi_timer_read(); 281 (void)acpi_timer_read(); 282 timecounter = newtc; 283 acpi_timer_eh = EVENTHANDLER_REGISTER(power_resume, 284 acpi_timer_resume_handler, tc, EVENTHANDLER_PRI_LAST); 285 } 286 } 287 288 /* 289 * Fetch current time value from reliable hardware. 290 */ 291 static u_int 292 acpi_timer_get_timecount(struct timecounter *tc) 293 { 294 return (acpi_timer_read()); 295 } 296 297 /* 298 * Fetch current time value from hardware that may not correctly 299 * latch the counter. We need to read until we have three monotonic 300 * samples and then use the middle one, otherwise we are not protected 301 * against the fact that the bits can be wrong in two directions. If 302 * we only cared about monosity, two reads would be enough. 303 */ 304 static u_int 305 acpi_timer_get_timecount_safe(struct timecounter *tc) 306 { 307 u_int u1, u2, u3; 308 309 u2 = acpi_timer_read(); 310 u3 = acpi_timer_read(); 311 do { 312 u1 = u2; 313 u2 = u3; 314 u3 = acpi_timer_read(); 315 } while (u1 > u2 || u2 > u3); 316 317 return (u2); 318 } 319 320 /* 321 * Timecounter frequency adjustment interface. 322 */ 323 static int 324 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS) 325 { 326 int error; 327 u_int freq; 328 329 if (acpi_timer_timecounter.tc_frequency == 0) 330 return (EOPNOTSUPP); 331 freq = acpi_timer_frequency; 332 error = sysctl_handle_int(oidp, &freq, 0, req); 333 if (error == 0 && req->newptr != NULL) { 334 acpi_timer_frequency = freq; 335 acpi_timer_timecounter.tc_frequency = acpi_timer_frequency; 336 } 337 338 return (error); 339 } 340 341 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, 342 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0, 343 acpi_timer_sysctl_freq, "I", 344 "ACPI timer frequency"); 345