]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/powerpc/kernel/smp-tbsync.c
Merge tag 'nfs-for-4.13-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[mirror_ubuntu-artful-kernel.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>
5a0e3ad6 12#include <linux/slab.h>
60063497 13#include <linux/atomic.h>
1da177e4
LT
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
cad5cef6 38static void enter_contest(u64 mark, long add)
1da177e4 39{
8ad200d7 40 while (get_tb() < mark)
1da177e4
LT
41 tbsync->race_result = add;
42}
43
cad5cef6 44void smp_generic_take_timebase(void)
1da177e4
LT
45{
46 int cmd;
8ad200d7 47 u64 tb;
467c3780 48 unsigned long flags;
1da177e4 49
467c3780 50 local_irq_save(flags);
8ad200d7
PM
51 while (!running)
52 barrier();
1da177e4
LT
53 rmb();
54
8ad200d7 55 for (;;) {
1da177e4 56 tbsync->ack = 1;
8ad200d7
PM
57 while (!tbsync->handshake)
58 barrier();
1da177e4
LT
59 rmb();
60
61 cmd = tbsync->cmd;
62 tb = tbsync->tb;
8ad200d7 63 mb();
1da177e4 64 tbsync->ack = 0;
8ad200d7
PM
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);
1da177e4 73 }
467c3780 74 local_irq_restore(flags);
1da177e4
LT
75}
76
cad5cef6 77static int start_contest(int cmd, long offset, int num)
1da177e4
LT
78{
79 int i, score=0;
8ad200d7 80 u64 tb;
9a06c3b1 81 u64 mark;
1da177e4
LT
82
83 tbsync->cmd = cmd;
84
85 local_irq_disable();
8ad200d7
PM
86 for (i = -3; i < num; ) {
87 tb = get_tb() + 400;
1da177e4
LT
88 tbsync->tb = tb + offset;
89 tbsync->mark = mark = tb + 400;
90
91 wmb();
92
93 tbsync->handshake = 1;
8ad200d7
PM
94 while (tbsync->ack)
95 barrier();
1da177e4 96
8ad200d7
PM
97 while (get_tb() <= tb)
98 barrier();
1da177e4 99 tbsync->handshake = 0;
8ad200d7 100 enter_contest(mark, 1);
1da177e4 101
8ad200d7
PM
102 while (!tbsync->ack)
103 barrier();
1da177e4 104
8ad200d7 105 if (i++ > 0)
1da177e4
LT
106 score += tbsync->race_result;
107 }
108 local_irq_enable();
109 return score;
110}
111
cad5cef6 112void smp_generic_give_timebase(void)
1da177e4
LT
113{
114 int i, score, score2, old, min=0, max=5000, offset=1000;
115
a6a8e009 116 pr_debug("Software timebase sync\n");
1da177e4
LT
117
118 /* if this fails then this kernel won't work anyway... */
f8485350 119 tbsync = kzalloc( sizeof(*tbsync), GFP_KERNEL );
1da177e4
LT
120 mb();
121 running = 1;
122
8ad200d7
PM
123 while (!tbsync->ack)
124 barrier();
1da177e4 125
a6a8e009 126 pr_debug("Got ack\n");
1da177e4
LT
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 131
a6a8e009 132 pr_debug("score %d, offset %d\n", score, offset );
1da177e4
LT
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
a6a8e009
BH
143 pr_debug("Min %d (score %d), Max %d (score %d)\n",
144 min, score, max, score2);
8ad200d7
PM
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 }
a6a8e009 158 pr_debug("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER );
1da177e4
LT
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}