1 // SPDX-License-Identifier: GPL-2.0
3 * Count register synchronisation.
5 * All CPUs will have their count registers synchronised to the CPU0 next time
6 * value. This can cause a small timewarp for CPU0. All other CPU's should
7 * not have done anything significant (but they may have had interrupts
8 * enabled briefly - prom_smp_finish() should not be responsible for enabling
12 #include <linux/kernel.h>
13 #include <linux/irqflags.h>
14 #include <linux/cpumask.h>
16 #include <asm/r4k-timer.h>
17 #include <linux/atomic.h>
18 #include <asm/barrier.h>
19 #include <asm/mipsregs.h>
21 static unsigned int initcount
= 0;
22 static atomic_t count_count_start
= ATOMIC_INIT(0);
23 static atomic_t count_count_stop
= ATOMIC_INIT(0);
28 void synchronise_count_master(int cpu
)
33 pr_info("Synchronize counters for CPU %u: ", cpu
);
35 local_irq_save(flags
);
38 * We loop a few times to get a primed instruction cache,
39 * then the last pass is more or less synchronised and
40 * the master and slaves each set their cycle counters to a known
41 * value all at once. This reduces the chance of having random offsets
42 * between the processors, and guarantees that the maximum
43 * delay between the cycle counters is never bigger than
44 * the latency of information-passing (cachelines) between
48 for (i
= 0; i
< NR_LOOPS
; i
++) {
49 /* slaves loop on '!= 2' */
50 while (atomic_read(&count_count_start
) != 1)
52 atomic_set(&count_count_stop
, 0);
55 /* Let the slave writes its count register */
56 atomic_inc(&count_count_start
);
58 /* Count will be initialised to current timer */
60 initcount
= read_c0_count();
63 * Everyone initialises count in the last loop:
66 write_c0_count(initcount
);
69 * Wait for slave to leave the synchronization point:
71 while (atomic_read(&count_count_stop
) != 1)
73 atomic_set(&count_count_start
, 0);
75 atomic_inc(&count_count_stop
);
77 /* Arrange for an interrupt in a short while */
78 write_c0_compare(read_c0_count() + COUNTON
);
80 local_irq_restore(flags
);
83 * i386 code reported the skew here, but the
84 * count registers were almost certainly out of sync
85 * so no point in alarming people
90 void synchronise_count_slave(int cpu
)
95 * Not every cpu is online at the time this gets called,
96 * so we first wait for the master to say everyone is ready
99 for (i
= 0; i
< NR_LOOPS
; i
++) {
100 atomic_inc(&count_count_start
);
101 while (atomic_read(&count_count_start
) != 2)
105 * Everyone initialises count in the last loop:
108 write_c0_count(initcount
);
110 atomic_inc(&count_count_stop
);
111 while (atomic_read(&count_count_stop
) != 2)
114 /* Arrange for an interrupt in a short while */
115 write_c0_compare(read_c0_count() + COUNTON
);