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