1*23a4d41eSandvar /* $NetBSD: e500_timer.c,v 1.8 2023/08/10 20:02:56 andvar Exp $ */
2b8ea2c8cSmatt /*-
3b8ea2c8cSmatt * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4b8ea2c8cSmatt * All rights reserved.
5b8ea2c8cSmatt *
6b8ea2c8cSmatt * This code is derived from software contributed to The NetBSD Foundation
7b8ea2c8cSmatt * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8b8ea2c8cSmatt * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9b8ea2c8cSmatt *
10b8ea2c8cSmatt * This material is based upon work supported by the Defense Advanced Research
11b8ea2c8cSmatt * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12b8ea2c8cSmatt * Contract No. N66001-09-C-2073.
13b8ea2c8cSmatt * Approved for Public Release, Distribution Unlimited
14b8ea2c8cSmatt *
15b8ea2c8cSmatt * Redistribution and use in source and binary forms, with or without
16b8ea2c8cSmatt * modification, are permitted provided that the following conditions
17b8ea2c8cSmatt * are met:
18b8ea2c8cSmatt * 1. Redistributions of source code must retain the above copyright
19b8ea2c8cSmatt * notice, this list of conditions and the following disclaimer.
20b8ea2c8cSmatt * 2. Redistributions in binary form must reproduce the above copyright
21b8ea2c8cSmatt * notice, this list of conditions and the following disclaimer in the
22b8ea2c8cSmatt * documentation and/or other materials provided with the distribution.
23b8ea2c8cSmatt *
24b8ea2c8cSmatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25b8ea2c8cSmatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26b8ea2c8cSmatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27b8ea2c8cSmatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28b8ea2c8cSmatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29b8ea2c8cSmatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30b8ea2c8cSmatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31b8ea2c8cSmatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32b8ea2c8cSmatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33b8ea2c8cSmatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34b8ea2c8cSmatt * POSSIBILITY OF SUCH DAMAGE.
35b8ea2c8cSmatt */
36b8ea2c8cSmatt
37b8ea2c8cSmatt #include <sys/cdefs.h>
38*23a4d41eSandvar __KERNEL_RCSID(0, "$NetBSD: e500_timer.c,v 1.8 2023/08/10 20:02:56 andvar Exp $");
39b8ea2c8cSmatt
40b8ea2c8cSmatt #include <sys/param.h>
41b8ea2c8cSmatt #include <sys/kernel.h>
42b8ea2c8cSmatt #include <sys/systm.h>
43b8ea2c8cSmatt #include <sys/timetc.h>
44b8ea2c8cSmatt #include <sys/intr.h>
45b8ea2c8cSmatt #include <sys/cpu.h>
46b8ea2c8cSmatt
47b8ea2c8cSmatt #include <uvm/uvm_extern.h>
48b8ea2c8cSmatt
49b8ea2c8cSmatt #include <powerpc/spr.h>
50b8ea2c8cSmatt #include <powerpc/booke/spr.h>
51b8ea2c8cSmatt #include <powerpc/booke/cpuvar.h>
52b8ea2c8cSmatt #include <powerpc/booke/e500reg.h>
53b8ea2c8cSmatt #include <powerpc/booke/e500var.h>
54b8ea2c8cSmatt #include <powerpc/booke/openpicreg.h>
55b8ea2c8cSmatt
565fc14464Snonaka uint32_t ticks_per_sec;
57b8ea2c8cSmatt static u_long ns_per_tick;
58b8ea2c8cSmatt
59b8ea2c8cSmatt static void init_ppcbooke_tc(void);
60b8ea2c8cSmatt static u_int get_ppcbooke_timecount(struct timecounter *);
61b8ea2c8cSmatt
62b8ea2c8cSmatt static struct timecounter ppcbooke_timecounter = {
63482eef70Srin .tc_get_timecount = get_ppcbooke_timecount,
64482eef70Srin .tc_counter_mask = ~0u,
65482eef70Srin .tc_name = "ppc_timebase",
66482eef70Srin .tc_quality = 100,
67b8ea2c8cSmatt };
68b8ea2c8cSmatt
69b8ea2c8cSmatt static inline void
openpic_write(struct cpu_softc * cpu,bus_size_t offset,uint32_t val)70b8ea2c8cSmatt openpic_write(struct cpu_softc *cpu, bus_size_t offset, uint32_t val)
71b8ea2c8cSmatt {
72b8ea2c8cSmatt
73b8ea2c8cSmatt return bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh,
74b8ea2c8cSmatt OPENPIC_BASE + offset, val);
75b8ea2c8cSmatt }
76b8ea2c8cSmatt
77b8ea2c8cSmatt int
e500_clock_intr(void * v)78b8ea2c8cSmatt e500_clock_intr(void *v)
79b8ea2c8cSmatt {
80b8ea2c8cSmatt struct trapframe * const tf = v;
81b8ea2c8cSmatt struct cpu_info * const ci = curcpu();
82b8ea2c8cSmatt struct cpu_softc * const cpu = ci->ci_softc;
83b8ea2c8cSmatt u_int nticks;
84b8ea2c8cSmatt
85b8ea2c8cSmatt /*
86b8ea2c8cSmatt * Check whether we are initialized.
87b8ea2c8cSmatt */
88b8ea2c8cSmatt if (!cpu->cpu_ticks_per_clock_intr)
89b8ea2c8cSmatt return 0;
90b8ea2c8cSmatt
91b8ea2c8cSmatt /*
92b8ea2c8cSmatt * Now let's how delayed the clock interrupt was. Obviously it must
93b8ea2c8cSmatt * at least one clock tick since the clock interrupt. But it might
94b8ea2c8cSmatt * be more if interrupts were blocked for a long time. We keep
95*23a4d41eSandvar * subtracting an interrupts We should be
96b8ea2c8cSmatt * [well] within a single tick.
97b8ea2c8cSmatt * We add back one tick (which should put us back above 0). If we
98b8ea2c8cSmatt * are still below 0, keep adding ticks until we are above 0.
99b8ea2c8cSmatt */
100b8ea2c8cSmatt const uint64_t now = mftb();
101b8ea2c8cSmatt uint64_t latency = now - (ci->ci_lastintr + cpu->cpu_ticks_per_clock_intr);
102b8ea2c8cSmatt #if 0
103b8ea2c8cSmatt uint64_t orig_latency = latency;
104b8ea2c8cSmatt #endif
105b8ea2c8cSmatt if (now < ci->ci_lastintr + cpu->cpu_ticks_per_clock_intr)
106b8ea2c8cSmatt latency = 0;
107b8ea2c8cSmatt
108b8ea2c8cSmatt nticks = 1 + latency / cpu->cpu_ticks_per_clock_intr;
109b8ea2c8cSmatt latency %= cpu->cpu_ticks_per_clock_intr;
110b8ea2c8cSmatt #if 0
111b8ea2c8cSmatt for (nticks = 1; latency >= cpu->cpu_ticks_per_clock_intr; nticks++) {
112b8ea2c8cSmatt latency -= cpu->cpu_ticks_per_clock_intr;
113b8ea2c8cSmatt }
114b8ea2c8cSmatt #endif
115b8ea2c8cSmatt
116b8ea2c8cSmatt ci->ci_ev_clock.ev_count++;
117b8ea2c8cSmatt cpu->cpu_ev_late_clock.ev_count += nticks - 1;
118b8ea2c8cSmatt
119b8ea2c8cSmatt /*
120b8ea2c8cSmatt * lasttb is used during microtime. Set it to the virtual
121b8ea2c8cSmatt * start of this tick interval.
122b8ea2c8cSmatt */
123b8ea2c8cSmatt #if 0
124b8ea2c8cSmatt if (nticks > 10 || now - ci->ci_lastintr < 7 * cpu->cpu_ticks_per_clock_intr / 8)
125b8ea2c8cSmatt printf("%s: nticks=%u lastintr=%#"PRIx64"(%#"PRIx64") now=%#"PRIx64" latency=%#"PRIx64" orig=%#"PRIx64"\n", __func__,
126b8ea2c8cSmatt nticks, ci->ci_lastintr, now - latency, now, latency, orig_latency);
127b8ea2c8cSmatt #endif
128b8ea2c8cSmatt ci->ci_lastintr = now - latency;
129b8ea2c8cSmatt ci->ci_lasttb = now;
130b8ea2c8cSmatt
131b8ea2c8cSmatt wrtee(PSL_EE); /* Reenable interrupts */
132b8ea2c8cSmatt
133b8ea2c8cSmatt /*
134b8ea2c8cSmatt * Do standard timer interrupt stuff.
135b8ea2c8cSmatt */
136b8ea2c8cSmatt while (nticks-- > 0) {
137b8ea2c8cSmatt hardclock(&tf->tf_cf);
138b8ea2c8cSmatt }
139b8ea2c8cSmatt
140b8ea2c8cSmatt wrtee(0); /* turn off interrupts */
141b8ea2c8cSmatt
142fe86ea0fSmatt tf->tf_srr1 &= ~PSL_POW; /* make cpu_idle exit */
143fe86ea0fSmatt
144b8ea2c8cSmatt return 1;
145b8ea2c8cSmatt }
146b8ea2c8cSmatt
147b8ea2c8cSmatt void
cpu_initclocks(void)148b8ea2c8cSmatt cpu_initclocks(void)
149b8ea2c8cSmatt {
150b8ea2c8cSmatt struct cpu_info * const ci = curcpu();
151b8ea2c8cSmatt struct cpu_softc * const cpu = ci->ci_softc;
152b8ea2c8cSmatt
153b8ea2c8cSmatt cpu->cpu_ticks_per_clock_intr = (ci->ci_data.cpu_cc_freq + hz/2 - 1) / hz;
154b8ea2c8cSmatt
155b8ea2c8cSmatt /* interrupt established in e500_intr_cpu_init */
156b8ea2c8cSmatt
157b8ea2c8cSmatt ci->ci_lastintr = ci->ci_lasttb = mftb();
158b8ea2c8cSmatt openpic_write(cpu, cpu->cpu_clock_gtbcr,
159b8ea2c8cSmatt GTBCR_CI | cpu->cpu_ticks_per_clock_intr);
160b8ea2c8cSmatt openpic_write(cpu, cpu->cpu_clock_gtbcr,
161b8ea2c8cSmatt cpu->cpu_ticks_per_clock_intr);
162b8ea2c8cSmatt
163c35009adSmatt if (CPU_IS_PRIMARY(ci))
164b8ea2c8cSmatt init_ppcbooke_tc();
165b8ea2c8cSmatt }
166b8ea2c8cSmatt
167b8ea2c8cSmatt void
calc_delayconst(void)168b8ea2c8cSmatt calc_delayconst(void)
169b8ea2c8cSmatt {
170b8ea2c8cSmatt struct cpu_info * const ci = curcpu();
171b8ea2c8cSmatt
172b8ea2c8cSmatt ci->ci_data.cpu_cc_freq = board_info_get_number("timebase-frequency");
1735fc14464Snonaka ticks_per_sec = (uint32_t)ci->ci_data.cpu_cc_freq;
174b8ea2c8cSmatt ns_per_tick = 1000000000 / (u_int)ci->ci_data.cpu_cc_freq;
175b8ea2c8cSmatt }
176b8ea2c8cSmatt
177b8ea2c8cSmatt static u_int
get_ppcbooke_timecount(struct timecounter * tc)178b8ea2c8cSmatt get_ppcbooke_timecount(struct timecounter *tc)
179b8ea2c8cSmatt {
180b8ea2c8cSmatt return mftbl();
181b8ea2c8cSmatt }
182b8ea2c8cSmatt
183b8ea2c8cSmatt /*
184b8ea2c8cSmatt * Wait for about n microseconds (at least!).
185b8ea2c8cSmatt */
186b8ea2c8cSmatt void
delay(unsigned int n)187b8ea2c8cSmatt delay(unsigned int n)
188b8ea2c8cSmatt {
189b8ea2c8cSmatt uint64_t tb;
190b8ea2c8cSmatt u_long tbh, tbl, scratch;
191b8ea2c8cSmatt
192b8ea2c8cSmatt tb = mftb();
193b8ea2c8cSmatt /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
194b8ea2c8cSmatt tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
195b8ea2c8cSmatt tbh = tb >> 32;
196b8ea2c8cSmatt tbl = tb;
197b8ea2c8cSmatt __asm volatile (
198b8ea2c8cSmatt "1: mfspr %0,%4" "\n"
199b8ea2c8cSmatt " cmplw %0,%1" "\n"
200b8ea2c8cSmatt " blt 1b" "\n"
201b8ea2c8cSmatt " bgt 2f" "\n"
202b8ea2c8cSmatt " mfspr %0,%3" "\n"
203b8ea2c8cSmatt " cmplw %0,%2" "\n"
204b8ea2c8cSmatt " blt 1b" "\n"
205b8ea2c8cSmatt "2:" "\n"
206b8ea2c8cSmatt : "=&r"(scratch)
207b8ea2c8cSmatt : "r"(tbh), "r"(tbl), "n"(SPR_TBL), "n"(SPR_TBU)
208b8ea2c8cSmatt : "cr0");
209b8ea2c8cSmatt }
210b8ea2c8cSmatt
211b8ea2c8cSmatt /*
212b8ea2c8cSmatt * Nothing to do.
213b8ea2c8cSmatt */
214b8ea2c8cSmatt void
setstatclockrate(int arg)215b8ea2c8cSmatt setstatclockrate(int arg)
216b8ea2c8cSmatt {
217b8ea2c8cSmatt
218b8ea2c8cSmatt /* Do nothing */
219b8ea2c8cSmatt }
220b8ea2c8cSmatt
221b8ea2c8cSmatt static void
init_ppcbooke_tc(void)222b8ea2c8cSmatt init_ppcbooke_tc(void)
223b8ea2c8cSmatt {
224b8ea2c8cSmatt /* from machdep initialization */
225b8ea2c8cSmatt ppcbooke_timecounter.tc_frequency = curcpu()->ci_data.cpu_cc_freq;
226b8ea2c8cSmatt tc_init(&ppcbooke_timecounter);
227b8ea2c8cSmatt }
228