blob: 4472a7f985776ad78a6e032a333cacd5bb2c7415 [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
Huacai Chendb0dbd52016-01-21 21:09:51 +080020static unsigned int initcount = 0;
Paul Gortmaker078a55f2013-06-18 13:38:59 +000021static atomic_t count_count_start = ATOMIC_INIT(0);
22static atomic_t count_count_stop = ATOMIC_INIT(0);
Ralf Baechle39b8d522008-04-28 17:14:26 +010023
Ralf Baechle70342282013-01-22 12:59:30 +010024#define COUNTON 100
Huacai Chendb0dbd52016-01-21 21:09:51 +080025#define NR_LOOPS 3
Ralf Baechle39b8d522008-04-28 17:14:26 +010026
Paul Gortmaker078a55f2013-06-18 13:38:59 +000027void synchronise_count_master(int cpu)
Ralf Baechle39b8d522008-04-28 17:14:26 +010028{
29 int i;
30 unsigned long flags;
Ralf Baechle39b8d522008-04-28 17:14:26 +010031
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053032 printk(KERN_INFO "Synchronize counters for CPU %u: ", cpu);
Ralf Baechle39b8d522008-04-28 17:14:26 +010033
34 local_irq_save(flags);
35
36 /*
Ralf Baechle39b8d522008-04-28 17:14:26 +010037 * 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
Ralf Baechle39b8d522008-04-28 17:14:26 +010047 for (i = 0; i < NR_LOOPS; i++) {
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053048 /* slaves loop on '!= 2' */
49 while (atomic_read(&count_count_start) != 1)
Ralf Baechle39b8d522008-04-28 17:14:26 +010050 mb();
51 atomic_set(&count_count_stop, 0);
52 smp_wmb();
53
Huacai Chendb0dbd52016-01-21 21:09:51 +080054 /* Let the slave writes its count register */
Ralf Baechle39b8d522008-04-28 17:14:26 +010055 atomic_inc(&count_count_start);
56
Huacai Chendb0dbd52016-01-21 21:09:51 +080057 /* Count will be initialised to current timer */
58 if (i == 1)
59 initcount = read_c0_count();
60
Ralf Baechle39b8d522008-04-28 17:14:26 +010061 /*
62 * Everyone initialises count in the last loop:
63 */
64 if (i == NR_LOOPS-1)
65 write_c0_count(initcount);
66
67 /*
Huacai Chendb0dbd52016-01-21 21:09:51 +080068 * Wait for slave to leave the synchronization point:
Ralf Baechle39b8d522008-04-28 17:14:26 +010069 */
Jayachandran Ccf9bfe52012-08-14 18:56:13 +053070 while (atomic_read(&count_count_stop) != 1)
Ralf Baechle39b8d522008-04-28 17:14:26 +010071 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
Paul Gortmaker078a55f2013-06-18 13:38:59 +000089void synchronise_count_slave(int cpu)
Ralf Baechle39b8d522008-04-28 17:14:26 +010090{
91 int i;
Ralf Baechle39b8d522008-04-28 17:14:26 +010092
Ralf Baechle39b8d522008-04-28 17:14:26 +010093 /*
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
Ralf Baechle39b8d522008-04-28 17:14:26 +010098 for (i = 0; i < NR_LOOPS; i++) {
99 atomic_inc(&count_count_start);
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530100 while (atomic_read(&count_count_start) != 2)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100101 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);
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530110 while (atomic_read(&count_count_stop) != 2)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100111 mb();
112 }
113 /* Arrange for an interrupt in a short while */
114 write_c0_compare(read_c0_count() + COUNTON);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100115}
116#undef NR_LOOPS