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