2 * Copyright (c) 2005, 2009-2011 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
33 #include <sys/interrupt.h>
34 #include <sys/priority.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 #include <sys/timeet.h>
40 #include <sys/timetc.h>
43 #include <machine/cpu.h>
44 #include <machine/efi.h>
45 #include <machine/intr.h>
46 #include <machine/intrcnt.h>
47 #include <machine/md_var.h>
48 #include <machine/smp.h>
50 #define CLOCK_ET_OFF 0
51 #define CLOCK_ET_PERIODIC 1
52 #define CLOCK_ET_ONESHOT 2
54 static struct eventtimer ia64_clock_et;
55 static u_int ia64_clock_xiv;
58 static timecounter_get_t ia64_get_timecount;
60 static struct timecounter ia64_timecounter = {
61 ia64_get_timecount, /* get_timecount */
63 ~0u, /* counter_mask */
69 ia64_get_timecount(struct timecounter* tc)
71 return ia64_get_itc();
76 ia64_ih_clock(struct thread *td, u_int xiv, struct trapframe *tf)
78 struct eventtimer *et;
82 PCPU_INC(md.stats.pcs_nclks);
83 intrcnt[INTRCNT_CLOCK]++;
86 PCPU_SET(md.clock, itc);
88 mode = PCPU_GET(md.clock_mode);
89 if (mode == CLOCK_ET_PERIODIC) {
90 load = PCPU_GET(md.clock_load);
91 ia64_set_itm(itc + load);
93 ia64_set_itv((1 << 16) | xiv);
100 et->et_event_cb(et, et->et_arg);
105 * Event timer start method.
108 ia64_clock_start(struct eventtimer *et, struct bintime *first,
109 struct bintime *period)
114 if (period != NULL) {
115 PCPU_SET(md.clock_mode, CLOCK_ET_PERIODIC);
116 load = (et->et_frequency * (period->frac >> 32)) >> 32;
118 load += et->et_frequency * period->sec;
120 PCPU_SET(md.clock_mode, CLOCK_ET_ONESHOT);
124 PCPU_SET(md.clock_load, load);
127 load = (et->et_frequency * (first->frac >> 32)) >> 32;
129 load += et->et_frequency * first->sec;
133 itc = ia64_get_itc();
134 ia64_set_itm(itc + load);
135 ia64_set_itv(ia64_clock_xiv);
142 * Event timer stop method.
145 ia64_clock_stop(struct eventtimer *et)
148 ia64_set_itv((1 << 16) | ia64_clock_xiv);
150 PCPU_SET(md.clock_mode, CLOCK_ET_OFF);
151 PCPU_SET(md.clock_load, 0);
156 * We call cpu_initclocks() on the APs as well. It allows us to
157 * group common initialization in the same function.
163 ia64_clock_stop(NULL);
164 if (PCPU_GET(cpuid) == 0)
165 cpu_initclocks_bsp();
171 clock_configure(void *dummy)
173 struct eventtimer *et;
176 ia64_clock_xiv = ia64_xiv_alloc(PI_REALTIME, IA64_XIV_IPI,
178 if (ia64_clock_xiv == 0)
179 panic("No XIV for clock interrupts");
181 itc_freq = (u_long)ia64_itc_freq() * 1000000ul;
185 et->et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
186 et->et_quality = 1000;
187 et->et_frequency = itc_freq;
188 et->et_min_period.sec = 0;
189 et->et_min_period.frac = (0x8000000000000000ul / (u_long)(10*hz)) << 1;
190 et->et_max_period.sec = 0xffffffff;
191 et->et_max_period.frac = ((0xfffffffeul << 32) / itc_freq) << 32;
192 et->et_start = ia64_clock_start;
193 et->et_stop = ia64_clock_stop;
198 ia64_timecounter.tc_frequency = itc_freq;
199 tc_init(&ia64_timecounter);
202 SYSINIT(clkcfg, SI_SUB_CONFIGURE, SI_ORDER_SECOND, clock_configure, NULL);