summaryrefslogtreecommitdiffstats
path: root/sys/dev/acpica/acpi_timer.c
diff options
context:
space:
mode:
authormsmith <msmith@FreeBSD.org>2001-07-21 04:08:42 +0000
committermsmith <msmith@FreeBSD.org>2001-07-21 04:08:42 +0000
commitb364b2c8850111f8b2f950fc2d143da52c324434 (patch)
tree39e19ba9f64946f357188824dc376ae15749c3d1 /sys/dev/acpica/acpi_timer.c
parent21ec8f10e3b47dbff86a2175e45b2f9c62d6b0b6 (diff)
downloadFreeBSD-src-b364b2c8850111f8b2f950fc2d143da52c324434.zip
FreeBSD-src-b364b2c8850111f8b2f950fc2d143da52c324434.tar.gz
Implement a "proper" timecounter hung off the ACPI timer device.
This code is based on the mp_clock code by phk. It attempts to detect the PIIX4 (see comments for details) and use a workaround for its problems. This code is experimental, and could use some testing and review by a timekeeping enthusiast.
Diffstat (limited to 'sys/dev/acpica/acpi_timer.c')
-rw-r--r--sys/dev/acpica/acpi_timer.c227
1 files changed, 207 insertions, 20 deletions
diff --git a/sys/dev/acpica/acpi_timer.c b/sys/dev/acpica/acpi_timer.c
index 9581dae..0ed8d82 100644
--- a/sys/dev/acpica/acpi_timer.c
+++ b/sys/dev/acpica/acpi_timer.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2000 Michael Smith
+ * Copyright (c) 2000, 2001 Michael Smith
* Copyright (c) 2000 BSDi
* All rights reserved.
*
@@ -26,15 +26,36 @@
*
* $FreeBSD$
*/
-
+/*
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
+ * ----------------------------------------------------------------------------
+ */
#include "opt_acpi.h"
#include <sys/param.h>
-#include <sys/kernel.h>
#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/sysctl.h>
+#include <sys/timetc.h>
+
+#include <machine/bus_pio.h>
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <sys/rman.h>
#include "acpi.h"
-#include <dev/acpica/acpivar.h>
+#include <acpica/acpivar.h>
+#include <pci/pcivar.h>
+
+/*
+ * A timecounter based on the free-running ACPI timer.
+ *
+ * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
+ */
/*
* Hooks for the ACPI CA debugging infrastructure
@@ -42,18 +63,49 @@
#define _COMPONENT ACPI_SYSTEM
MODULE_NAME("TIMER")
-#define ACPITIMER_MAGIC 0x524d4954 /* "TIMR" */
+static device_t acpi_timer_dev;
+struct resource *acpi_timer_reg;
+#define TIMER_READ bus_space_read_4(rman_get_bustag(acpi_timer_reg), \
+ rman_get_bushandle(acpi_timer_reg), \
+ 0)
+static int acpi_timer_flags;
+/*
+ * ] 20. ACPI Timer Errata
+ * ]
+ * ] Problem: The power management timer may return improper result when
+ * ] read. Although the timer value settles properly after incrementing,
+ * ] while incrementing there is a 3nS window every 69.8nS where the
+ * ] timer value is indeterminate (a 4.2% chance that the data will be
+ * ] incorrect when read). As a result, the ACPI free running count up
+ * ] timer specification is violated due to erroneous reads. Implication:
+ * ] System hangs due to the "inaccuracy" of the timer when used by
+ * ] software for time critical events and delays.
+ * ]
+ * ] Workaround: Read the register twice and compare.
+ * ] Status: This will not be fixed in the PIIX4 or PIIX4E.
+ *
+ * The counter is in other words not latched to the PCI bus clock when
+ * read. Notice the workaround isn't: We need to read until we have
+ * three monotonic samples and then use the middle one, otherwise we are
+ * not protected against the fact that the bits can be wrong in two
+ * directions. If we only cared about monosity two reads would be enough.
+ */
+#define TFLAG_NEED_PIIX_WAR (1 << 0)
-struct acpi_timer_softc {
- device_t tm_dev;
-};
+static u_int acpi_timer_frequency = 14318182/4;
static void acpi_timer_identify(driver_t *driver, device_t parent);
static int acpi_timer_probe(device_t dev);
static int acpi_timer_attach(device_t dev);
+static int acpi_timer_pci_probe(device_t dev);
+static unsigned acpi_timer_get_timecount(struct timecounter *tc);
+static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
+static void acpi_timer_test(void);
+/*
+ * Driver hung off ACPI.
+ */
static device_method_t acpi_timer_methods[] = {
- /* Device interface */
DEVMETHOD(device_identify, acpi_timer_identify),
DEVMETHOD(device_probe, acpi_timer_probe),
DEVMETHOD(device_attach, acpi_timer_attach),
@@ -64,17 +116,53 @@ static device_method_t acpi_timer_methods[] = {
static driver_t acpi_timer_driver = {
"acpi_timer",
acpi_timer_methods,
- sizeof(struct acpi_timer_softc),
+ 0,
};
devclass_t acpi_timer_devclass;
DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
+/*
+ * Chipset workaround driver hung off PCI.
+ */
+static device_method_t acpi_timer_pci_methods[] = {
+ DEVMETHOD(device_probe, acpi_timer_pci_probe),
+ {0, 0}
+};
+
+static driver_t acpi_timer_pci_driver = {
+ "acpi_timer_pci",
+ acpi_timer_pci_methods,
+ 0,
+};
+
+devclass_t acpi_timer_pci_devclass;
+DRIVER_MODULE(acpi_timer_pci, pci, acpi_timer_pci_driver, acpi_timer_pci_devclass, 0, 0);
+
+/*
+ * Timecounter.
+ */
+static struct timecounter acpi_timer_timecounter = {
+ acpi_timer_get_timecount,
+ 0,
+ 0xffffff,
+ 0,
+ "ACPI"
+};
+
+SYSCTL_OPAQUE(_debug, OID_AUTO, acpi_timecounter, CTLFLAG_RD,
+ &acpi_timer_timecounter, sizeof(acpi_timer_timecounter), "S,timecounter", "");
+
+/*
+ * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
+ * we will be using.
+ */
static void
acpi_timer_identify(driver_t *driver, device_t parent)
{
- device_t dev;
- char desc[40];
+ device_t dev;
+ char desc[40];
+ int rid;
FUNCTION_TRACE(__func__);
@@ -88,10 +176,18 @@ acpi_timer_identify(driver_t *driver, device_t parent)
device_printf(parent, "could not add acpi_timer0\n");
return_VOID;
}
- if (acpi_set_magic(dev, ACPITIMER_MAGIC)) {
- device_printf(dev, "could not set magic\n");
+ acpi_timer_dev = dev;
+ rid = 0;
+ bus_set_resource(dev, SYS_RES_IOPORT, rid, AcpiGbl_FADT->V1_PmTmrBlk, sizeof(u_int32_t));
+ if ((acpi_timer_reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) {
+ device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk);
return_VOID;
}
+ if (getenv("debug.acpi.timer_test") != NULL)
+ acpi_timer_test();
+
+ acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
+ tc_init(&acpi_timer_timecounter);
sprintf(desc, "%d-bit timer at 3.579545MHz", AcpiGbl_FADT->TmrValExt ? 32 : 24);
device_set_desc_copy(dev, desc);
@@ -102,7 +198,7 @@ acpi_timer_identify(driver_t *driver, device_t parent)
static int
acpi_timer_probe(device_t dev)
{
- if (acpi_get_magic(dev) == ACPITIMER_MAGIC)
+ if (dev == acpi_timer_dev)
return(0);
return(ENXIO);
}
@@ -110,12 +206,103 @@ acpi_timer_probe(device_t dev)
static int
acpi_timer_attach(device_t dev)
{
- struct acpi_timer_softc *sc;
+ return(0);
+}
- FUNCTION_TRACE(__func__);
+/*
+ * Look at PCI devices as they go past, and if we detect a PIIX4, set
+ * the PIIX_WAR flag.
+ *
+ * XXX do we know that other timecounters work? Interesting question.
+ */
+static int
+acpi_timer_pci_probe(device_t dev)
+{
+ if ((pci_get_vendor(dev) == 0x8086) &&
+ (pci_get_device(dev) == 0x7113)) {
+ acpi_timer_flags |= TFLAG_NEED_PIIX_WAR;
+ device_printf(acpi_timer_dev, "enabling PIIX4 timer workaround\n");
+ }
- sc = device_get_softc(dev);
- sc->tm_dev = dev;
+ return(ENXIO); /* we never match anything */
+}
- return_VALUE(0);
+/*
+ * Fetch current time value from hardware.
+ *
+ * XXX This is currently written to be "correct", not
+ * "fast". Optimisation is strongly indicated.
+ */
+static unsigned
+acpi_timer_get_timecount(struct timecounter *tc)
+{
+ unsigned u1, u2, u3;
+
+ if (acpi_timer_flags & TFLAG_NEED_PIIX_WAR) {
+ u2 = TIMER_READ;
+ u3 = TIMER_READ;
+ do {
+ u1 = u2;
+ u2 = u3;
+ u3 = TIMER_READ;
+ } while (u1 > u2 || u2 > u3);
+ return (u2);
+ } else {
+ return(TIMER_READ);
+ }
}
+
+/*
+ * Timecounter freqency adjustment interface.
+ */
+static int
+acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
+{
+ int error;
+ u_int freq;
+
+ if (acpi_timer_timecounter.tc_frequency == 0)
+ return (EOPNOTSUPP);
+ freq = acpi_timer_frequency;
+ error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
+ if (error == 0 && req->newptr != NULL) {
+ acpi_timer_frequency = freq;
+ acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
+ tc_update(&acpi_timer_timecounter);
+ }
+ return (error);
+}
+
+SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
+ 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
+
+/*
+ * Test harness for verifying ACPI timer behaviour.
+ * Boot with debug.acpi.timer_test set to invoke this.
+ */
+static void
+acpi_timer_test(void)
+{
+ u_int32_t u1, u2, u3;
+
+ u1 = TIMER_READ;
+ u2 = TIMER_READ;
+ u3 = TIMER_READ;
+
+ device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
+ for (;;) {
+ /*
+ * The failure case is where u3 > u1, but u2 does not fall between the two,
+ * ie. it contains garbage.
+ */
+ if (u3 > u1) {
+ if ((u2 < u1) || (u2 > u3))
+ device_printf(acpi_timer_dev, "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
+ u1, u2, u3);
+ }
+ u1 = u2;
+ u2 = u3;
+ u3 = TIMER_READ;
+ }
+}
+
OpenPOWER on IntegriCloud