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