1119452Sobrien/*- 251451Sphk * ---------------------------------------------------------------------------- 351451Sphk * "THE BEER-WARE LICENSE" (Revision 42): 451451Sphk * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 551451Sphk * can do whatever you want with this stuff. If we meet some day, and you think 651451Sphk * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 751451Sphk * ---------------------------------------------------------------------------- 8119452Sobrien */ 9119452Sobrien 10119452Sobrien#include <sys/cdefs.h> 11119452Sobrien__FBSDID("$FreeBSD$"); 12119452Sobrien 13119452Sobrien/*- 1453879Sphk * Just when we thought life were beautiful, reality pops its grim face over 1553879Sphk * the edge again: 1653879Sphk * 1753879Sphk * ] 20. ACPI Timer Errata 1853879Sphk * ] 1953879Sphk * ] Problem: The power management timer may return improper result when 2053879Sphk * ] read. Although the timer value settles properly after incrementing, 2153879Sphk * ] while incrementing there is a 3nS window every 69.8nS where the 2253879Sphk * ] timer value is indeterminate (a 4.2% chance that the data will be 2353879Sphk * ] incorrect when read). As a result, the ACPI free running count up 2453879Sphk * ] timer specification is violated due to erroneous reads. Implication: 2553879Sphk * ] System hangs due to the "inaccuracy" of the timer when used by 2653879Sphk * ] software for time critical events and delays. 2753879Sphk * ] 2853879Sphk * ] Workaround: Read the register twice and compare. 2953879Sphk * ] Status: This will not be fixed in the PIIX4 or PIIX4E. 3053879Sphk * 3153879Sphk * The counter is in other words not latched to the PCI bus clock when 3253879Sphk * read. Notice the workaround isn't: We need to read until we have 3353879Sphk * three monotonic samples and then use the middle one, otherwise we are 3453879Sphk * not protected against the fact that the bits can be wrong in two 3553879Sphk * directions. If we only cared about monosity two reads would be enough. 3651451Sphk */ 3751451Sphk 3852121Speter/* #include "opt_bus.h" */ 3951451Sphk 4051451Sphk#include <sys/param.h> 4151451Sphk#include <sys/systm.h> 4258377Sphk#include <sys/timetc.h> 4351451Sphk#include <sys/kernel.h> 44129882Sphk#include <sys/module.h> 4551451Sphk#include <sys/sysctl.h> 4651451Sphk#include <sys/bus.h> 4751451Sphk 48118950Sjhb#include <dev/pci/pcireg.h> 49118950Sjhb#include <dev/pci/pcivar.h> 5051451Sphk 5151451Sphkstatic unsigned piix_get_timecount(struct timecounter *tc); 5251451Sphk 5351451Sphkstatic u_int32_t piix_timecounter_address; 5451596Sjhaystatic u_int piix_freq = 14318182/4; 5551451Sphk 5651451Sphkstatic struct timecounter piix_timecounter = { 57119937Sjhb piix_get_timecount, /* get_timecount */ 58119937Sjhb 0, /* no poll_pps */ 59119937Sjhb 0xffffff, /* counter_mask */ 60119937Sjhb 0, /* frequency */ 61119937Sjhb "PIIX" /* name */ 6251451Sphk}; 6351451Sphk 6451451Sphk 6551596Sjhaystatic int 6662573Sphksysctl_machdep_piix_freq(SYSCTL_HANDLER_ARGS) 6751596Sjhay{ 6851596Sjhay int error; 6951596Sjhay u_int freq; 7051596Sjhay 7151596Sjhay if (piix_timecounter.tc_frequency == 0) 7251596Sjhay return (EOPNOTSUPP); 7351596Sjhay freq = piix_freq; 74170289Sdwmalone error = sysctl_handle_int(oidp, &freq, 0, req); 7551596Sjhay if (error == 0 && req->newptr != NULL) { 7651596Sjhay piix_freq = freq; 7751596Sjhay piix_timecounter.tc_frequency = piix_freq; 7851596Sjhay } 7951596Sjhay return (error); 8051596Sjhay} 8151596Sjhay 8251596SjhaySYSCTL_PROC(_machdep, OID_AUTO, piix_freq, CTLTYPE_INT | CTLFLAG_RW, 8351596Sjhay 0, sizeof(u_int), sysctl_machdep_piix_freq, "I", ""); 8451596Sjhay 8551451Sphkstatic unsigned 8651451Sphkpiix_get_timecount(struct timecounter *tc) 8751451Sphk{ 8853879Sphk unsigned u1, u2, u3; 8953879Sphk 9053879Sphk u2 = inl(piix_timecounter_address); 9153879Sphk u3 = inl(piix_timecounter_address); 9253879Sphk do { 9353879Sphk u1 = u2; 9453879Sphk u2 = u3; 9553879Sphk u3 = inl(piix_timecounter_address); 9653879Sphk } while (u1 > u2 || u2 > u3); 9753879Sphk return (u2); 9851451Sphk} 9951451Sphk 10051451Sphkstatic int 101118950Sjhbpiix_probe(device_t dev) 10251451Sphk{ 103119948Sjhb u_int32_t d; 10451451Sphk 105121480Sjhb if (devclass_get_device(devclass_find("acpi"), 0) != NULL) 106121480Sjhb return (ENXIO); 10751451Sphk switch (pci_get_devid(dev)) { 10851451Sphk case 0x71138086: 109121480Sjhb device_set_desc(dev, "PIIX Timecounter"); 110121480Sjhb break; 111113173Sdes default: 112113173Sdes return (ENXIO); 113121480Sjhb } 114121480Sjhb 115121480Sjhb d = pci_read_config(dev, PCIR_COMMAND, 2); 116121480Sjhb if (!(d & PCIM_CMD_PORTEN)) { 117121480Sjhb device_printf(dev, "PIIX I/O space not mapped\n"); 118121480Sjhb return (ENXIO); 119121480Sjhb } 120121480Sjhb return (0); 12151451Sphk} 12251451Sphk 12351451Sphkstatic int 124118950Sjhbpiix_attach(device_t dev) 12551451Sphk{ 126119948Sjhb u_int32_t d; 127113173Sdes 128113173Sdes d = pci_read_config(dev, 0x40, 4); 129113173Sdes piix_timecounter_address = (d & 0xffc0) + 8; 130113173Sdes piix_timecounter.tc_frequency = piix_freq; 131113173Sdes tc_init(&piix_timecounter); 132113173Sdes return (0); 13351451Sphk} 13451451Sphk 13551451Sphkstatic device_method_t piix_methods[] = { 13651451Sphk /* Device interface */ 13751451Sphk DEVMETHOD(device_probe, piix_probe), 13851451Sphk DEVMETHOD(device_attach, piix_attach), 13951451Sphk { 0, 0 } 14051451Sphk}; 14151451Sphk 14251451Sphkstatic driver_t piix_driver = { 14351451Sphk "piix", 14451451Sphk piix_methods, 14551451Sphk 1, 14651451Sphk}; 14751451Sphk 14851451Sphkstatic devclass_t piix_devclass; 14951451Sphk 15051451SphkDRIVER_MODULE(piix, pci, piix_driver, piix_devclass, 0, 0); 151