[POWERPC] Fix problem with time not advancing on 32-bit platforms
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / kernel / smp-tbsync.c
CommitLineData
1da177e4
LT
1/*
2 * Smp timebase synchronization for ppc.
3 *
4 * Copyright (C) 2003 Samuel Rydh (samuel@ibrium.se)
5 *
6 */
7
1da177e4
LT
8#include <linux/kernel.h>
9#include <linux/sched.h>
10#include <linux/smp.h>
11#include <linux/unistd.h>
12#include <linux/init.h>
13#include <asm/atomic.h>
14#include <asm/smp.h>
15#include <asm/time.h>
16
17#define NUM_ITER 300
18
19enum {
20 kExit=0, kSetAndTest, kTest
21};
22
23static struct {
8ad200d7
PM
24 volatile u64 tb;
25 volatile u64 mark;
1da177e4
LT
26 volatile int cmd;
27 volatile int handshake;
8ad200d7 28 int filler[2];
1da177e4
LT
29
30 volatile int ack;
31 int filler2[7];
32
33 volatile int race_result;
34} *tbsync;
35
36static volatile int running;
37
8ad200d7 38static void __devinit enter_contest(u64 mark, long add)
1da177e4 39{
8ad200d7 40 while (get_tb() < mark)
1da177e4
LT
41 tbsync->race_result = add;
42}
43
8ad200d7 44void __devinit smp_generic_take_timebase(void)
1da177e4
LT
45{
46 int cmd;
8ad200d7 47 u64 tb;
1da177e4
LT
48
49 local_irq_disable();
8ad200d7
PM
50 while (!running)
51 barrier();
1da177e4
LT
52 rmb();
53
8ad200d7 54 for (;;) {
1da177e4 55 tbsync->ack = 1;
8ad200d7
PM
56 while (!tbsync->handshake)
57 barrier();
1da177e4
LT
58 rmb();
59
60 cmd = tbsync->cmd;
61 tb = tbsync->tb;
8ad200d7 62 mb();
1da177e4 63 tbsync->ack = 0;
8ad200d7
PM
64 if (cmd == kExit)
65 break;
66
67 while (tbsync->handshake)
68 barrier();
69 if (cmd == kSetAndTest)
70 set_tb(tb >> 32, tb & 0xfffffffful);
71 enter_contest(tbsync->mark, -1);
1da177e4
LT
72 }
73 local_irq_enable();
74}
75
8ad200d7 76static int __devinit start_contest(int cmd, long offset, int num)
1da177e4
LT
77{
78 int i, score=0;
8ad200d7
PM
79 u64 tb;
80 long mark;
1da177e4
LT
81
82 tbsync->cmd = cmd;
83
84 local_irq_disable();
8ad200d7
PM
85 for (i = -3; i < num; ) {
86 tb = get_tb() + 400;
1da177e4
LT
87 tbsync->tb = tb + offset;
88 tbsync->mark = mark = tb + 400;
89
90 wmb();
91
92 tbsync->handshake = 1;
8ad200d7
PM
93 while (tbsync->ack)
94 barrier();
1da177e4 95
8ad200d7
PM
96 while (get_tb() <= tb)
97 barrier();
1da177e4 98 tbsync->handshake = 0;
8ad200d7 99 enter_contest(mark, 1);
1da177e4 100
8ad200d7
PM
101 while (!tbsync->ack)
102 barrier();
1da177e4 103
8ad200d7 104 if (i++ > 0)
1da177e4
LT
105 score += tbsync->race_result;
106 }
107 local_irq_enable();
108 return score;
109}
110
8ad200d7 111void __devinit smp_generic_give_timebase(void)
1da177e4
LT
112{
113 int i, score, score2, old, min=0, max=5000, offset=1000;
114
115 printk("Synchronizing timebase\n");
116
117 /* if this fails then this kernel won't work anyway... */
118 tbsync = kmalloc( sizeof(*tbsync), GFP_KERNEL );
119 memset( tbsync, 0, sizeof(*tbsync) );
120 mb();
121 running = 1;
122
8ad200d7
PM
123 while (!tbsync->ack)
124 barrier();
1da177e4
LT
125
126 printk("Got ack\n");
127
128 /* binary search */
8ad200d7
PM
129 for (old = -1; old != offset ; offset = (min+max) / 2) {
130 score = start_contest(kSetAndTest, offset, NUM_ITER);
1da177e4
LT
131
132 printk("score %d, offset %d\n", score, offset );
133
134 if( score > 0 )
135 max = offset;
136 else
137 min = offset;
138 old = offset;
139 }
8ad200d7
PM
140 score = start_contest(kSetAndTest, min, NUM_ITER);
141 score2 = start_contest(kSetAndTest, max, NUM_ITER);
1da177e4 142
8ad200d7
PM
143 printk("Min %d (score %d), Max %d (score %d)\n",
144 min, score, max, score2);
145 score = abs(score);
146 score2 = abs(score2);
1da177e4
LT
147 offset = (score < score2) ? min : max;
148
149 /* guard against inaccurate mttb */
8ad200d7
PM
150 for (i = 0; i < 10; i++) {
151 start_contest(kSetAndTest, offset, NUM_ITER/10);
1da177e4 152
8ad200d7 153 if ((score2 = start_contest(kTest, offset, NUM_ITER)) < 0)
1da177e4 154 score2 = -score2;
8ad200d7 155 if (score2 <= score || score2 < 20)
1da177e4
LT
156 break;
157 }
158 printk("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER );
159
160 /* exiting */
161 tbsync->cmd = kExit;
162 wmb();
163 tbsync->handshake = 1;
8ad200d7
PM
164 while (tbsync->ack)
165 barrier();
1da177e4 166 tbsync->handshake = 0;
8ad200d7 167 kfree(tbsync);
1da177e4
LT
168 tbsync = NULL;
169 running = 0;
170}