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