2 * Count register synchronisation.
4 * All CPUs will have their count registers synchronised to the CPU0 next time
5 * value. This can cause a small timewarp for CPU0. All other CPU's should
6 * not have done anything significant (but they may have had interrupts
7 * enabled briefly - prom_smp_finish() should not be responsible for enabling
11 #include <linux/kernel.h>
12 #include <linux/irqflags.h>
13 #include <linux/cpumask.h>
15 #include <asm/r4k-timer.h>
16 #include <linux/atomic.h>
17 #include <asm/barrier.h>
18 #include <asm/mipsregs.h>
20 static atomic_t count_start_flag
= ATOMIC_INIT(0);
21 static atomic_t count_count_start
= ATOMIC_INIT(0);
22 static atomic_t count_count_stop
= ATOMIC_INIT(0);
23 static atomic_t count_reference
= ATOMIC_INIT(0);
28 void synchronise_count_master(int cpu
)
32 unsigned int initcount
;
34 printk(KERN_INFO
"Synchronize counters for CPU %u: ", cpu
);
36 local_irq_save(flags
);
39 * Notify the slaves that it's time to start
41 atomic_set(&count_reference
, read_c0_count());
42 atomic_set(&count_start_flag
, cpu
);
45 /* Count will be initialised to current timer for all CPU's */
46 initcount
= read_c0_count();
49 * We loop a few times to get a primed instruction cache,
50 * then the last pass is more or less synchronised and
51 * the master and slaves each set their cycle counters to a known
52 * value all at once. This reduces the chance of having random offsets
53 * between the processors, and guarantees that the maximum
54 * delay between the cycle counters is never bigger than
55 * the latency of information-passing (cachelines) between
59 for (i
= 0; i
< NR_LOOPS
; i
++) {
60 /* slaves loop on '!= 2' */
61 while (atomic_read(&count_count_start
) != 1)
63 atomic_set(&count_count_stop
, 0);
66 /* this lets the slaves write their count register */
67 atomic_inc(&count_count_start
);
70 * Everyone initialises count in the last loop:
73 write_c0_count(initcount
);
76 * Wait for all slaves to leave the synchronization point:
78 while (atomic_read(&count_count_stop
) != 1)
80 atomic_set(&count_count_start
, 0);
82 atomic_inc(&count_count_stop
);
84 /* Arrange for an interrupt in a short while */
85 write_c0_compare(read_c0_count() + COUNTON
);
86 atomic_set(&count_start_flag
, 0);
88 local_irq_restore(flags
);
91 * i386 code reported the skew here, but the
92 * count registers were almost certainly out of sync
93 * so no point in alarming people
98 void synchronise_count_slave(int cpu
)
101 unsigned int initcount
;
104 * Not every cpu is online at the time this gets called,
105 * so we first wait for the master to say everyone is ready
108 while (atomic_read(&count_start_flag
) != cpu
)
111 /* Count will be initialised to next expire for all CPU's */
112 initcount
= atomic_read(&count_reference
);
114 for (i
= 0; i
< NR_LOOPS
; i
++) {
115 atomic_inc(&count_count_start
);
116 while (atomic_read(&count_count_start
) != 2)
120 * Everyone initialises count in the last loop:
123 write_c0_count(initcount
);
125 atomic_inc(&count_count_stop
);
126 while (atomic_read(&count_count_stop
) != 2)
129 /* Arrange for an interrupt in a short while */
130 write_c0_compare(read_c0_count() + COUNTON
);