2 * Copyright (c) 1990 The Regents of the University of California.
3 * Copyright (c) 2008 The DragonFly Project.
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz and Don Ahn.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91
34 * $FreeBSD: src/sys/i386/isa/clock.c,v 1.149.2.6 2002/11/02 04:41:50 iwasaki Exp $
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/systimer.h>
46 #include <sys/globaldata.h>
48 #include <machine/clock.h>
49 #include <machine/cputypes.h>
51 static sysclock_t tsc_cputimer_count_mfence(void);
52 static sysclock_t tsc_cputimer_count_lfence(void);
53 static void tsc_cputimer_construct(struct cputimer *, sysclock_t);
55 static struct cputimer tsc_cputimer = {
56 .next = SLIST_ENTRY_INITIALIZER,
58 .pri = CPUTIMER_PRI_TSC,
60 .count = NULL, /* determined later */
61 .fromhz = cputimer_default_fromhz,
62 .fromus = cputimer_default_fromus,
63 .construct = tsc_cputimer_construct,
64 .destruct = cputimer_default_destruct,
65 .freq = 0 /* determined later */
68 static struct cpucounter tsc_cpucounter = {
69 .freq = 0, /* determined later */
70 .count = NULL, /* determined later */
71 .flags = 0, /* adjusted later */
72 .prio = CPUCOUNTER_PRIO_TSC,
73 .type = CPUCOUNTER_TSC
76 #ifdef _KERNEL_VIRTUAL
77 #define TSC_CPUTIMER_FREQMAX 2000000 /* 2MHz */
79 #define TSC_CPUTIMER_FREQMAX 128000000 /* 128Mhz */
82 static int tsc_cputimer_shift;
85 tsc_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
88 timer->base = oldclock - timer->count();
91 static __inline sysclock_t
92 tsc_cputimer_count(void)
97 tsc >>= tsc_cputimer_shift;
99 return (tsc + tsc_cputimer.base);
103 tsc_cputimer_count_lfence(void)
106 return tsc_cputimer_count();
110 tsc_cputimer_count_mfence(void)
113 return tsc_cputimer_count();
117 tsc_cpucounter_count_lfence(void)
125 tsc_cpucounter_count_mfence(void)
133 tsc_cputimer_register(void)
139 #ifndef _KERNEL_VIRTUAL
141 /* Per-cpu cpucounter still works. */
148 TUNABLE_INT_FETCH("hw.tsc_cputimer_enable", &enable);
152 freq = tsc_frequency;
153 while (freq > TSC_CPUTIMER_FREQMAX) {
155 ++tsc_cputimer_shift;
157 kprintf("TSC: cputimer freq %ju, shift %d\n",
158 (uintmax_t)freq, tsc_cputimer_shift);
160 tsc_cputimer.freq = freq;
162 if (cpu_vendor_id == CPU_VENDOR_INTEL)
163 tsc_cputimer.count = tsc_cputimer_count_lfence;
165 tsc_cputimer.count = tsc_cputimer_count_mfence; /* safe bet */
167 cputimer_register(&tsc_cputimer);
168 cputimer_select(&tsc_cputimer, 0);
170 tsc_cpucounter.flags |= CPUCOUNTER_FLAG_MPSYNC;
171 #ifndef _KERNEL_VIRTUAL
174 tsc_cpucounter.freq = tsc_frequency;
175 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
176 tsc_cpucounter.count =
177 tsc_cpucounter_count_lfence;
179 tsc_cpucounter.count =
180 tsc_cpucounter_count_mfence; /* safe bet */
182 cpucounter_register(&tsc_cpucounter);
184 SYSINIT(tsc_cputimer_reg, SI_BOOT2_POST_SMP, SI_ORDER_FIRST,
185 tsc_cputimer_register, NULL);