Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Count register synchronisation.
4 *
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
9 * interrupts...)
10 */
11
12#include <linux/kernel.h>
13#include <linux/irqflags.h>
14#include <linux/cpumask.h>
15
16#include <asm/r4k-timer.h>
17#include <linux/atomic.h>
18#include <asm/barrier.h>
19#include <asm/mipsregs.h>
20
21static unsigned int initcount = 0;
22static atomic_t count_count_start = ATOMIC_INIT(0);
23static atomic_t count_count_stop = ATOMIC_INIT(0);
24
25#define COUNTON 100
26#define NR_LOOPS 3
27
28void synchronise_count_master(int cpu)
29{
30 int i;
31 unsigned long flags;
32
33 pr_info("Synchronize counters for CPU %u: ", cpu);
34
35 local_irq_save(flags);
36
37 /*
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
45 * two CPUs.
46 */
47
48 for (i = 0; i < NR_LOOPS; i++) {
49 /* slaves loop on '!= 2' */
50 while (atomic_read(&count_count_start) != 1)
51 mb();
52 atomic_set(&count_count_stop, 0);
53 smp_wmb();
54
55 /* Let the slave writes its count register */
56 atomic_inc(&count_count_start);
57
58 /* Count will be initialised to current timer */
59 if (i == 1)
60 initcount = read_c0_count();
61
62 /*
63 * Everyone initialises count in the last loop:
64 */
65 if (i == NR_LOOPS-1)
66 write_c0_count(initcount);
67
68 /*
69 * Wait for slave to leave the synchronization point:
70 */
71 while (atomic_read(&count_count_stop) != 1)
72 mb();
73 atomic_set(&count_count_start, 0);
74 smp_wmb();
75 atomic_inc(&count_count_stop);
76 }
77 /* Arrange for an interrupt in a short while */
78 write_c0_compare(read_c0_count() + COUNTON);
79
80 local_irq_restore(flags);
81
82 /*
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
86 */
87 pr_cont("done.\n");
88}
89
90void synchronise_count_slave(int cpu)
91{
92 int i;
93 unsigned long flags;
94
95 local_irq_save(flags);
96
97 /*
98 * Not every cpu is online at the time this gets called,
99 * so we first wait for the master to say everyone is ready
100 */
101
102 for (i = 0; i < NR_LOOPS; i++) {
103 atomic_inc(&count_count_start);
104 while (atomic_read(&count_count_start) != 2)
105 mb();
106
107 /*
108 * Everyone initialises count in the last loop:
109 */
110 if (i == NR_LOOPS-1)
111 write_c0_count(initcount);
112
113 atomic_inc(&count_count_stop);
114 while (atomic_read(&count_count_stop) != 2)
115 mb();
116 }
117 /* Arrange for an interrupt in a short while */
118 write_c0_compare(read_c0_count() + COUNTON);
119
120 local_irq_restore(flags);
121}
122#undef NR_LOOPS
1/*
2 * Count register synchronisation.
3 *
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
8 * interrupts...)
9 */
10
11#include <linux/kernel.h>
12#include <linux/irqflags.h>
13#include <linux/cpumask.h>
14
15#include <asm/r4k-timer.h>
16#include <linux/atomic.h>
17#include <asm/barrier.h>
18#include <asm/mipsregs.h>
19
20static unsigned int initcount = 0;
21static atomic_t count_count_start = ATOMIC_INIT(0);
22static atomic_t count_count_stop = ATOMIC_INIT(0);
23
24#define COUNTON 100
25#define NR_LOOPS 3
26
27void synchronise_count_master(int cpu)
28{
29 int i;
30 unsigned long flags;
31
32 printk(KERN_INFO "Synchronize counters for CPU %u: ", cpu);
33
34 local_irq_save(flags);
35
36 /*
37 * We loop a few times to get a primed instruction cache,
38 * then the last pass is more or less synchronised and
39 * the master and slaves each set their cycle counters to a known
40 * value all at once. This reduces the chance of having random offsets
41 * between the processors, and guarantees that the maximum
42 * delay between the cycle counters is never bigger than
43 * the latency of information-passing (cachelines) between
44 * two CPUs.
45 */
46
47 for (i = 0; i < NR_LOOPS; i++) {
48 /* slaves loop on '!= 2' */
49 while (atomic_read(&count_count_start) != 1)
50 mb();
51 atomic_set(&count_count_stop, 0);
52 smp_wmb();
53
54 /* Let the slave writes its count register */
55 atomic_inc(&count_count_start);
56
57 /* Count will be initialised to current timer */
58 if (i == 1)
59 initcount = read_c0_count();
60
61 /*
62 * Everyone initialises count in the last loop:
63 */
64 if (i == NR_LOOPS-1)
65 write_c0_count(initcount);
66
67 /*
68 * Wait for slave to leave the synchronization point:
69 */
70 while (atomic_read(&count_count_stop) != 1)
71 mb();
72 atomic_set(&count_count_start, 0);
73 smp_wmb();
74 atomic_inc(&count_count_stop);
75 }
76 /* Arrange for an interrupt in a short while */
77 write_c0_compare(read_c0_count() + COUNTON);
78
79 local_irq_restore(flags);
80
81 /*
82 * i386 code reported the skew here, but the
83 * count registers were almost certainly out of sync
84 * so no point in alarming people
85 */
86 printk("done.\n");
87}
88
89void synchronise_count_slave(int cpu)
90{
91 int i;
92
93 /*
94 * Not every cpu is online at the time this gets called,
95 * so we first wait for the master to say everyone is ready
96 */
97
98 for (i = 0; i < NR_LOOPS; i++) {
99 atomic_inc(&count_count_start);
100 while (atomic_read(&count_count_start) != 2)
101 mb();
102
103 /*
104 * Everyone initialises count in the last loop:
105 */
106 if (i == NR_LOOPS-1)
107 write_c0_count(initcount);
108
109 atomic_inc(&count_count_stop);
110 while (atomic_read(&count_count_stop) != 2)
111 mb();
112 }
113 /* Arrange for an interrupt in a short while */
114 write_c0_compare(read_c0_count() + COUNTON);
115}
116#undef NR_LOOPS