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