xref: /freebsd/sys/i386/i386/mp_clock.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1 /*-
2  * SPDX-License-Identifier: Beerware
3  *
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
9  * ----------------------------------------------------------------------------
10  */
11 
12 #include <sys/cdefs.h>
13 /*-
14  * Just when we thought life were beautiful, reality pops its grim face over
15  * the edge again:
16  *
17  * ] 20. ACPI Timer Errata
18  * ]
19  * ]   Problem: The power management timer may return improper result when
20  * ]   read. Although the timer value settles properly after incrementing,
21  * ]   while incrementing there is a 3nS window every 69.8nS where the
22  * ]   timer value is indeterminate (a 4.2% chance that the data will be
23  * ]   incorrect when read). As a result, the ACPI free running count up
24  * ]   timer specification is violated due to erroneous reads.  Implication:
25  * ]   System hangs due to the "inaccuracy" of the timer when used by
26  * ]   software for time critical events and delays.
27  * ]
28  * ] Workaround: Read the register twice and compare.
29  * ] Status: This will not be fixed in the PIIX4 or PIIX4E.
30  *
31  * The counter is in other words not latched to the PCI bus clock when
32  * read.  Notice the workaround isn't:  We need to read until we have
33  * three monotonic samples and then use the middle one, otherwise we are
34  * not protected against the fact that the bits can be wrong in two
35  * directions.  If we only cared about monosity two reads would be enough.
36  */
37 
38 /* #include "opt_bus.h" */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/timetc.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/bus.h>
47 
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 
51 static unsigned piix_get_timecount(struct timecounter *tc);
52 
53 static u_int32_t piix_timecounter_address;
54 static u_int piix_freq = 14318182/4;
55 
56 static struct timecounter piix_timecounter = {
57 	piix_get_timecount,	/* get_timecount */
58 	0,			/* no poll_pps */
59 	0xffffff,		/* counter_mask */
60 	0,			/* frequency */
61 	"PIIX"			/* name */
62 };
63 
64 static int
sysctl_machdep_piix_freq(SYSCTL_HANDLER_ARGS)65 sysctl_machdep_piix_freq(SYSCTL_HANDLER_ARGS)
66 {
67 	int error;
68 	u_int freq;
69 
70 	if (piix_timecounter.tc_frequency == 0)
71 		return (EOPNOTSUPP);
72 	freq = piix_freq;
73 	error = sysctl_handle_int(oidp, &freq, 0, req);
74 	if (error == 0 && req->newptr != NULL) {
75 		piix_freq = freq;
76 		piix_timecounter.tc_frequency = piix_freq;
77 	}
78 	return (error);
79 }
80 
81 SYSCTL_PROC(_machdep, OID_AUTO, piix_freq,
82     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(u_int),
83     sysctl_machdep_piix_freq, "I",
84     "");
85 
86 static unsigned
piix_get_timecount(struct timecounter * tc)87 piix_get_timecount(struct timecounter *tc)
88 {
89 	unsigned u1, u2, u3;
90 
91 	u2 = inl(piix_timecounter_address);
92 	u3 = inl(piix_timecounter_address);
93 	do {
94 		u1 = u2;
95 		u2 = u3;
96 		u3 = inl(piix_timecounter_address);
97 	} while (u1 > u2 || u2 > u3);
98 	return (u2);
99 }
100 
101 static int
piix_probe(device_t dev)102 piix_probe(device_t dev)
103 {
104 	u_int32_t d;
105 
106 	if (devclass_get_device(devclass_find("acpi"), 0) != NULL)
107 		return (ENXIO);
108 	switch (pci_get_devid(dev)) {
109 	case 0x71138086:
110 		device_set_desc(dev, "PIIX Timecounter");
111 		break;
112 	default:
113 		return (ENXIO);
114 	}
115 
116 	d = pci_read_config(dev, PCIR_COMMAND, 2);
117 	if (!(d & PCIM_CMD_PORTEN)) {
118 		device_printf(dev, "PIIX I/O space not mapped\n");
119 		return (ENXIO);
120 	}
121 	return (0);
122 }
123 
124 static int
piix_attach(device_t dev)125 piix_attach(device_t dev)
126 {
127 	u_int32_t d;
128 
129 	d = pci_read_config(dev, 0x40, 4);
130 	piix_timecounter_address = (d & 0xffc0) + 8;
131 	piix_timecounter.tc_frequency = piix_freq;
132 	tc_init(&piix_timecounter);
133 	return (0);
134 }
135 
136 static device_method_t piix_methods[] = {
137 	/* Device interface */
138 	DEVMETHOD(device_probe,		piix_probe),
139 	DEVMETHOD(device_attach,	piix_attach),
140 	{ 0, 0 }
141 };
142 
143 static driver_t piix_driver = {
144 	"piix",
145 	piix_methods,
146 	1,
147 };
148 
149 DRIVER_MODULE(piix, pci, piix_driver, 0, 0);
150