Deleted Added
full compact
clock.c (183089) clock.c (192067)
1/*-
2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $NetBSD: clock.c,v 1.9 2000/01/19 02:52:19 msaitoh Exp $
32 */
33/*
34 * Copyright (C) 2001 Benno Rice.
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57
58#include <sys/cdefs.h>
1/*-
2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $NetBSD: clock.c,v 1.9 2000/01/19 02:52:19 msaitoh Exp $
32 */
33/*
34 * Copyright (C) 2001 Benno Rice.
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57
58#include <sys/cdefs.h>
59__FBSDID("$FreeBSD: head/sys/powerpc/aim/clock.c 183089 2008-09-16 17:11:33Z marcel $");
59__FBSDID("$FreeBSD: head/sys/powerpc/aim/clock.c 192067 2009-05-14 00:34:26Z nwhitehorn $");
60
61#include <sys/param.h>
62#include <sys/systm.h>
63#include <sys/kernel.h>
64#include <sys/bus.h>
65#include <sys/interrupt.h>
66#include <sys/pcpu.h>
67#include <sys/sysctl.h>
68#include <sys/timetc.h>
69
70#include <dev/ofw/openfirm.h>
71
72#include <machine/clock.h>
73#include <machine/cpu.h>
74#include <machine/intr.h>
75#include <machine/md_var.h>
76#include <machine/smp.h>
77
78/*
79 * Initially we assume a processor with a bus frequency of 12.5 MHz.
80 */
81u_long ns_per_tick = 80;
82static u_long ticks_per_sec = 12500000;
83static long ticks_per_intr;
84
85static timecounter_get_t decr_get_timecount;
86
87static struct timecounter decr_timecounter = {
88 decr_get_timecount, /* get_timecount */
89 0, /* no poll_pps */
90 ~0u, /* counter_mask */
91 0, /* frequency */
92 "decrementer" /* name */
93};
94
95void
96decr_intr(struct trapframe *frame)
97{
98 long tick;
99 int nticks;
100
101 /*
102 * Check whether we are initialized.
103 */
104 if (!ticks_per_intr)
105 return;
106
107 /*
108 * Based on the actual time delay since the last decrementer reload,
109 * we arrange for earlier interrupt next time.
110 */
111 __asm ("mfdec %0" : "=r"(tick));
112 for (nticks = 0; tick < 0; nticks++)
113 tick += ticks_per_intr;
114 mtdec(tick);
115
116 if (PCPU_GET(cpuid) == 0) {
117 while (nticks-- > 0)
118 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
119 } else {
120 while (nticks-- > 0)
121 hardclock_cpu(TRAPF_USERMODE(frame));
122 }
123}
124
125void
126decr_init(void)
127{
60
61#include <sys/param.h>
62#include <sys/systm.h>
63#include <sys/kernel.h>
64#include <sys/bus.h>
65#include <sys/interrupt.h>
66#include <sys/pcpu.h>
67#include <sys/sysctl.h>
68#include <sys/timetc.h>
69
70#include <dev/ofw/openfirm.h>
71
72#include <machine/clock.h>
73#include <machine/cpu.h>
74#include <machine/intr.h>
75#include <machine/md_var.h>
76#include <machine/smp.h>
77
78/*
79 * Initially we assume a processor with a bus frequency of 12.5 MHz.
80 */
81u_long ns_per_tick = 80;
82static u_long ticks_per_sec = 12500000;
83static long ticks_per_intr;
84
85static timecounter_get_t decr_get_timecount;
86
87static struct timecounter decr_timecounter = {
88 decr_get_timecount, /* get_timecount */
89 0, /* no poll_pps */
90 ~0u, /* counter_mask */
91 0, /* frequency */
92 "decrementer" /* name */
93};
94
95void
96decr_intr(struct trapframe *frame)
97{
98 long tick;
99 int nticks;
100
101 /*
102 * Check whether we are initialized.
103 */
104 if (!ticks_per_intr)
105 return;
106
107 /*
108 * Based on the actual time delay since the last decrementer reload,
109 * we arrange for earlier interrupt next time.
110 */
111 __asm ("mfdec %0" : "=r"(tick));
112 for (nticks = 0; tick < 0; nticks++)
113 tick += ticks_per_intr;
114 mtdec(tick);
115
116 if (PCPU_GET(cpuid) == 0) {
117 while (nticks-- > 0)
118 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
119 } else {
120 while (nticks-- > 0)
121 hardclock_cpu(TRAPF_USERMODE(frame));
122 }
123}
124
125void
126decr_init(void)
127{
128 int qhandle, phandle;
129 char name[32];
130 unsigned int msr;
128 struct cpuref cpu;
129 register_t msr;
131
130
132 phandle = 0;
133
134 /*
131 /*
135 * Get this info during autoconf? XXX
132 * Check the BSP's timebase frequency. Sometimes we can't find the BSP, so fall
133 * back to the first CPU in this case.
136 */
134 */
137 for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
138 if (OF_getprop(qhandle, "device_type", name, sizeof name) >= 0
139 && !strcmp(name, "cpu")
140 && OF_getprop(qhandle, "timebase-frequency",
141 &ticks_per_sec, sizeof ticks_per_sec) >= 0) {
142 /*
143 * Should check for correct CPU here? XXX
144 */
145 msr = mfmsr();
146 mtmsr(msr & ~PSL_EE);
147
135
148 ns_per_tick = 1000000000 / ticks_per_sec;
149 ticks_per_intr = ticks_per_sec / hz;
150 mtdec(ticks_per_intr);
136 if (platform_smp_get_bsp(&cpu) != 0)
137 platform_smp_first_cpu(&cpu);
151
138
152 mtmsr(msr);
139 ticks_per_sec = platform_timebase_freq(&cpu);
153
140
154 break;
155 }
156 if ((phandle = OF_child(qhandle)))
157 continue;
158 while (qhandle) {
159 if ((phandle = OF_peer(qhandle)))
160 break;
161 qhandle = OF_parent(qhandle);
162 }
163 }
164 if (!phandle)
165 panic("no cpu node");
141 msr = mfmsr();
142 mtmsr(msr & ~PSL_EE);
143
144 ns_per_tick = 1000000000 / ticks_per_sec;
145 ticks_per_intr = ticks_per_sec / hz;
146 mtdec(ticks_per_intr);
147
148 mtmsr(msr);
166}
167
168void
169decr_tc_init(void)
170{
171 decr_timecounter.tc_frequency = ticks_per_sec;
172 tc_init(&decr_timecounter);
173}
174
175static __inline u_quad_t
176mftb(void)
177{
178 u_long scratch;
179 u_quad_t tb;
180
181 __asm ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw 0,%0,%1; bne 1b"
182 : "=r"(tb), "=r"(scratch));
183 return tb;
184}
185
186static unsigned
187decr_get_timecount(struct timecounter *tc)
188{
189 register_t tb;
190
191 __asm __volatile("mftb %0" : "=r"(tb));
192 return (tb);
193}
194
195/*
196 * Wait for about n microseconds (at least!).
197 */
198void
199DELAY(int n)
200{
201 u_quad_t tb, ttb;
202
203 tb = mftb();
204 ttb = tb + (n * 1000 + ns_per_tick - 1) / ns_per_tick;
205 while (tb < ttb)
206 tb = mftb();
207}
208
209/*
210 * Nothing to do.
211 */
212void
213cpu_startprofclock(void)
214{
215
216 /* Do nothing */
217}
218
219void
220cpu_stopprofclock(void)
221{
222}
149}
150
151void
152decr_tc_init(void)
153{
154 decr_timecounter.tc_frequency = ticks_per_sec;
155 tc_init(&decr_timecounter);
156}
157
158static __inline u_quad_t
159mftb(void)
160{
161 u_long scratch;
162 u_quad_t tb;
163
164 __asm ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw 0,%0,%1; bne 1b"
165 : "=r"(tb), "=r"(scratch));
166 return tb;
167}
168
169static unsigned
170decr_get_timecount(struct timecounter *tc)
171{
172 register_t tb;
173
174 __asm __volatile("mftb %0" : "=r"(tb));
175 return (tb);
176}
177
178/*
179 * Wait for about n microseconds (at least!).
180 */
181void
182DELAY(int n)
183{
184 u_quad_t tb, ttb;
185
186 tb = mftb();
187 ttb = tb + (n * 1000 + ns_per_tick - 1) / ns_per_tick;
188 while (tb < ttb)
189 tb = mftb();
190}
191
192/*
193 * Nothing to do.
194 */
195void
196cpu_startprofclock(void)
197{
198
199 /* Do nothing */
200}
201
202void
203cpu_stopprofclock(void)
204{
205}