aboutsummaryrefslogtreecommitdiffstats
path: root/arch/powerpc
diff options
context:
space:
mode:
authorPaul Mackerras <paulus@samba.org>2005-11-03 21:28:58 -0500
committerPaul Mackerras <paulus@samba.org>2005-11-03 21:28:58 -0500
commit8ad200d7b7c8fac77cf705831e90e889360d7030 (patch)
treec954352ca3cd8c64e913ceb4a5a082ce7e98f8cd /arch/powerpc
parentd3ab57ebdc6457543b346255fa47b0ecd7671136 (diff)
powerpc: Merge smp-tbsync.c (the generic timebase sync routine)
Signed-off-by: Paul Mackerras <paulus@samba.org>
Diffstat (limited to 'arch/powerpc')
-rw-r--r--arch/powerpc/Kconfig7
-rw-r--r--arch/powerpc/kernel/Makefile1
-rw-r--r--arch/powerpc/kernel/smp-tbsync.c171
3 files changed, 179 insertions, 0 deletions
diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 3cf03ab46113..f4e25c648fbb 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -300,6 +300,7 @@ config PPC_PMAC64
300 bool 300 bool
301 depends on PPC_PMAC && POWER4 301 depends on PPC_PMAC && POWER4
302 select U3_DART 302 select U3_DART
303 select GENERIC_TBSYNC
303 default y 304 default y
304 305
305config PPC_PREP 306config PPC_PREP
@@ -314,6 +315,7 @@ config PPC_MAPLE
314 bool " Maple 970FX Evaluation Board" 315 bool " Maple 970FX Evaluation Board"
315 select U3_DART 316 select U3_DART
316 select MPIC_BROKEN_U3 317 select MPIC_BROKEN_U3
318 select GENERIC_TBSYNC
317 default n 319 default n
318 help 320 help
319 This option enables support for the Maple 970FX Evaluation Board. 321 This option enables support for the Maple 970FX Evaluation Board.
@@ -386,6 +388,11 @@ config PPC_MPC106
386 bool 388 bool
387 default n 389 default n
388 390
391config GENERIC_TBSYNC
392 bool
393 default y if CONFIG_PPC32 && CONFIG_SMP
394 default n
395
389source "drivers/cpufreq/Kconfig" 396source "drivers/cpufreq/Kconfig"
390 397
391config CPU_FREQ_PMAC 398config CPU_FREQ_PMAC
diff --git a/arch/powerpc/kernel/Makefile b/arch/powerpc/kernel/Makefile
index 7a3e1155ac9a..631149ea93db 100644
--- a/arch/powerpc/kernel/Makefile
+++ b/arch/powerpc/kernel/Makefile
@@ -21,6 +21,7 @@ obj-$(CONFIG_PPC_RTAS) += rtas.o
21obj-$(CONFIG_RTAS_FLASH) += rtas_flash.o 21obj-$(CONFIG_RTAS_FLASH) += rtas_flash.o
22obj-$(CONFIG_RTAS_PROC) += rtas-proc.o 22obj-$(CONFIG_RTAS_PROC) += rtas-proc.o
23obj-$(CONFIG_IBMVIO) += vio.o 23obj-$(CONFIG_IBMVIO) += vio.o
24obj-$(CONFIG_GENERIC_TBSYNC) += smp-tbsync.o
24 25
25ifeq ($(CONFIG_PPC_MERGE),y) 26ifeq ($(CONFIG_PPC_MERGE),y)
26 27
diff --git a/arch/powerpc/kernel/smp-tbsync.c b/arch/powerpc/kernel/smp-tbsync.c
new file mode 100644
index 000000000000..9adef3bddad4
--- /dev/null
+++ b/arch/powerpc/kernel/smp-tbsync.c
@@ -0,0 +1,171 @@
1/*
2 * Smp timebase synchronization for ppc.
3 *
4 * Copyright (C) 2003 Samuel Rydh (samuel@ibrium.se)
5 *
6 */
7
8#include <linux/config.h>
9#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/smp.h>
12#include <linux/unistd.h>
13#include <linux/init.h>
14#include <asm/atomic.h>
15#include <asm/smp.h>
16#include <asm/time.h>
17
18#define NUM_ITER 300
19
20enum {
21 kExit=0, kSetAndTest, kTest
22};
23
24static struct {
25 volatile u64 tb;
26 volatile u64 mark;
27 volatile int cmd;
28 volatile int handshake;
29 int filler[2];
30
31 volatile int ack;
32 int filler2[7];
33
34 volatile int race_result;
35} *tbsync;
36
37static volatile int running;
38
39static void __devinit enter_contest(u64 mark, long add)
40{
41 while (get_tb() < mark)
42 tbsync->race_result = add;
43}
44
45void __devinit smp_generic_take_timebase(void)
46{
47 int cmd;
48 u64 tb;
49
50 local_irq_disable();
51 while (!running)
52 barrier();
53 rmb();
54
55 for (;;) {
56 tbsync->ack = 1;
57 while (!tbsync->handshake)
58 barrier();
59 rmb();
60
61 cmd = tbsync->cmd;
62 tb = tbsync->tb;
63 mb();
64 tbsync->ack = 0;
65 if (cmd == kExit)
66 break;
67
68 while (tbsync->handshake)
69 barrier();
70 if (cmd == kSetAndTest)
71 set_tb(tb >> 32, tb & 0xfffffffful);
72 enter_contest(tbsync->mark, -1);
73 }
74 local_irq_enable();
75}
76
77static int __devinit start_contest(int cmd, long offset, int num)
78{
79 int i, score=0;
80 u64 tb;
81 long mark;
82
83 tbsync->cmd = cmd;
84
85 local_irq_disable();
86 for (i = -3; i < num; ) {
87 tb = get_tb() + 400;
88 tbsync->tb = tb + offset;
89 tbsync->mark = mark = tb + 400;
90
91 wmb();
92
93 tbsync->handshake = 1;
94 while (tbsync->ack)
95 barrier();
96
97 while (get_tb() <= tb)
98 barrier();
99 tbsync->handshake = 0;
100 enter_contest(mark, 1);
101
102 while (!tbsync->ack)
103 barrier();
104
105 if (i++ > 0)
106 score += tbsync->race_result;
107 }
108 local_irq_enable();
109 return score;
110}
111
112void __devinit smp_generic_give_timebase(void)
113{
114 int i, score, score2, old, min=0, max=5000, offset=1000;
115
116 printk("Synchronizing timebase\n");
117
118 /* if this fails then this kernel won't work anyway... */
119 tbsync = kmalloc( sizeof(*tbsync), GFP_KERNEL );
120 memset( tbsync, 0, sizeof(*tbsync) );
121 mb();
122 running = 1;
123
124 while (!tbsync->ack)
125 barrier();
126
127 printk("Got ack\n");
128
129 /* binary search */
130 for (old = -1; old != offset ; offset = (min+max) / 2) {
131 score = start_contest(kSetAndTest, offset, NUM_ITER);
132
133 printk("score %d, offset %d\n", score, offset );
134
135 if( score > 0 )
136 max = offset;
137 else
138 min = offset;
139 old = offset;
140 }
141 score = start_contest(kSetAndTest, min, NUM_ITER);
142 score2 = start_contest(kSetAndTest, max, NUM_ITER);
143
144 printk("Min %d (score %d), Max %d (score %d)\n",
145 min, score, max, score2);
146 score = abs(score);
147 score2 = abs(score2);
148 offset = (score < score2) ? min : max;
149
150 /* guard against inaccurate mttb */
151 for (i = 0; i < 10; i++) {
152 start_contest(kSetAndTest, offset, NUM_ITER/10);
153
154 if ((score2 = start_contest(kTest, offset, NUM_ITER)) < 0)
155 score2 = -score2;
156 if (score2 <= score || score2 < 20)
157 break;
158 }
159 printk("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER );
160
161 /* exiting */
162 tbsync->cmd = kExit;
163 wmb();
164 tbsync->handshake = 1;
165 while (tbsync->ack)
166 barrier();
167 tbsync->handshake = 0;
168 kfree(tbsync);
169 tbsync = NULL;
170 running = 0;
171}