blob: 2242bdd4370eb19851ff5a31540a02c65fa26498 [file] [log] [blame]
Ralf Baechle39b8d522008-04-28 17:14:26 +01001/*
2 * Count register synchronisation.
3 *
Tim Andersoneb9b5142009-06-17 16:40:34 -07004 * All CPUs will have their count registers synchronised to the CPU0 next time
Ralf Baechle39b8d522008-04-28 17:14:26 +01005 * 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...)
Ralf Baechle39b8d522008-04-28 17:14:26 +01009 */
10
11#include <linux/kernel.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010012#include <linux/irqflags.h>
Tim Andersoneb9b5142009-06-17 16:40:34 -070013#include <linux/cpumask.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010014
Tim Andersoneb9b5142009-06-17 16:40:34 -070015#include <asm/r4k-timer.h>
Arun Sharma600634972011-07-26 16:09:06 -070016#include <linux/atomic.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010017#include <asm/barrier.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010018#include <asm/mipsregs.h>
19
Paul Gortmaker078a55f2013-06-18 13:38:59 +000020static atomic_t count_start_flag = ATOMIC_INIT(0);
21static atomic_t count_count_start = ATOMIC_INIT(0);
22static atomic_t count_count_stop = ATOMIC_INIT(0);
23static atomic_t count_reference = ATOMIC_INIT(0);
Ralf Baechle39b8d522008-04-28 17:14:26 +010024
Ralf Baechle70342282013-01-22 12:59:30 +010025#define COUNTON 100
Ralf Baechle39b8d522008-04-28 17:14:26 +010026#define NR_LOOPS 5
27
Paul Gortmaker078a55f2013-06-18 13:38:59 +000028void synchronise_count_master(int cpu)
Ralf Baechle39b8d522008-04-28 17:14:26 +010029{
30 int i;
31 unsigned long flags;
32 unsigned int initcount;
Ralf Baechle39b8d522008-04-28 17:14:26 +010033
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053034 printk(KERN_INFO "Synchronize counters for CPU %u: ", cpu);
Ralf Baechle39b8d522008-04-28 17:14:26 +010035
36 local_irq_save(flags);
37
38 /*
39 * Notify the slaves that it's time to start
40 */
Tim Andersoneb9b5142009-06-17 16:40:34 -070041 atomic_set(&count_reference, read_c0_count());
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053042 atomic_set(&count_start_flag, cpu);
Ralf Baechle39b8d522008-04-28 17:14:26 +010043 smp_wmb();
44
Tim Andersoneb9b5142009-06-17 16:40:34 -070045 /* Count will be initialised to current timer for all CPU's */
46 initcount = read_c0_count();
Ralf Baechle39b8d522008-04-28 17:14:26 +010047
48 /*
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
56 * two CPUs.
57 */
58
Ralf Baechle39b8d522008-04-28 17:14:26 +010059 for (i = 0; i < NR_LOOPS; i++) {
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053060 /* slaves loop on '!= 2' */
61 while (atomic_read(&count_count_start) != 1)
Ralf Baechle39b8d522008-04-28 17:14:26 +010062 mb();
63 atomic_set(&count_count_stop, 0);
64 smp_wmb();
65
66 /* this lets the slaves write their count register */
67 atomic_inc(&count_count_start);
68
69 /*
70 * Everyone initialises count in the last loop:
71 */
72 if (i == NR_LOOPS-1)
73 write_c0_count(initcount);
74
75 /*
76 * Wait for all slaves to leave the synchronization point:
77 */
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053078 while (atomic_read(&count_count_stop) != 1)
Ralf Baechle39b8d522008-04-28 17:14:26 +010079 mb();
80 atomic_set(&count_count_start, 0);
81 smp_wmb();
82 atomic_inc(&count_count_stop);
83 }
84 /* Arrange for an interrupt in a short while */
85 write_c0_compare(read_c0_count() + COUNTON);
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053086 atomic_set(&count_start_flag, 0);
Ralf Baechle39b8d522008-04-28 17:14:26 +010087
88 local_irq_restore(flags);
89
90 /*
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
94 */
95 printk("done.\n");
96}
97
Paul Gortmaker078a55f2013-06-18 13:38:59 +000098void synchronise_count_slave(int cpu)
Ralf Baechle39b8d522008-04-28 17:14:26 +010099{
100 int i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100101 unsigned int initcount;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100102
Ralf Baechle39b8d522008-04-28 17:14:26 +0100103 /*
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
106 */
107
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530108 while (atomic_read(&count_start_flag) != cpu)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100109 mb();
110
Tim Andersoneb9b5142009-06-17 16:40:34 -0700111 /* Count will be initialised to next expire for all CPU's */
112 initcount = atomic_read(&count_reference);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100113
Ralf Baechle39b8d522008-04-28 17:14:26 +0100114 for (i = 0; i < NR_LOOPS; i++) {
115 atomic_inc(&count_count_start);
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530116 while (atomic_read(&count_count_start) != 2)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100117 mb();
118
119 /*
120 * Everyone initialises count in the last loop:
121 */
122 if (i == NR_LOOPS-1)
123 write_c0_count(initcount);
124
125 atomic_inc(&count_count_stop);
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530126 while (atomic_read(&count_count_stop) != 2)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100127 mb();
128 }
129 /* Arrange for an interrupt in a short while */
130 write_c0_compare(read_c0_count() + COUNTON);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100131}
132#undef NR_LOOPS