aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStafford Horne <shorne@gmail.com>2017-07-06 17:06:30 -0400
committerStafford Horne <shorne@gmail.com>2017-11-03 01:01:16 -0400
commit4553474d977d1ee8a81067cfbc588f1df84ce3e9 (patch)
tree4e74260eb9134fd94a7bc20fc3ab23e4ecb3a7e1
parent78cdfb5cf15e0f9fb4c2a9176a13a907a1d024f0 (diff)
openrisc: add tick timer multi-core sync logic
In case timers are not in sync when cpus start (i.e. hot plug / offset resets) we need to synchronize the secondary cpus internal timer with the main cpu. This is needed as in OpenRISC SMP there is only one clocksource registered which reads from the same ttcr register on each cpu. This synchronization routine heavily borrows from mips implementation that does something similar. Signed-off-by: Stafford Horne <shorne@gmail.com>
-rw-r--r--arch/openrisc/include/asm/time.h8
-rw-r--r--arch/openrisc/kernel/Makefile2
-rw-r--r--arch/openrisc/kernel/smp.c3
-rw-r--r--arch/openrisc/kernel/sync-timer.c120
-rw-r--r--arch/openrisc/kernel/time.c15
5 files changed, 145 insertions, 3 deletions
diff --git a/arch/openrisc/include/asm/time.h b/arch/openrisc/include/asm/time.h
index fe83a34a7d68..313ee975774b 100644
--- a/arch/openrisc/include/asm/time.h
+++ b/arch/openrisc/include/asm/time.h
@@ -12,4 +12,12 @@
12 12
13extern void openrisc_clockevent_init(void); 13extern void openrisc_clockevent_init(void);
14 14
15extern void openrisc_timer_set(unsigned long count);
16extern void openrisc_timer_set_next(unsigned long delta);
17
18#ifdef CONFIG_SMP
19extern void synchronise_count_master(int cpu);
20extern void synchronise_count_slave(int cpu);
21#endif
22
15#endif /* __ASM_OR1K_TIME_H */ 23#endif /* __ASM_OR1K_TIME_H */
diff --git a/arch/openrisc/kernel/Makefile b/arch/openrisc/kernel/Makefile
index b4b51a07016a..9028e5a1fdd7 100644
--- a/arch/openrisc/kernel/Makefile
+++ b/arch/openrisc/kernel/Makefile
@@ -8,7 +8,7 @@ obj-y := setup.o or32_ksyms.o process.o dma.o \
8 traps.o time.o irq.o entry.o ptrace.o signal.o \ 8 traps.o time.o irq.o entry.o ptrace.o signal.o \
9 sys_call_table.o unwinder.o 9 sys_call_table.o unwinder.o
10 10
11obj-$(CONFIG_SMP) += smp.o 11obj-$(CONFIG_SMP) += smp.o sync-timer.o
12obj-$(CONFIG_STACKTRACE) += stacktrace.o 12obj-$(CONFIG_STACKTRACE) += stacktrace.o
13obj-$(CONFIG_MODULES) += module.o 13obj-$(CONFIG_MODULES) += module.o
14obj-$(CONFIG_OF) += prom.o 14obj-$(CONFIG_OF) += prom.o
diff --git a/arch/openrisc/kernel/smp.c b/arch/openrisc/kernel/smp.c
index 4763b8b9161e..4d80ce6fa045 100644
--- a/arch/openrisc/kernel/smp.c
+++ b/arch/openrisc/kernel/smp.c
@@ -100,6 +100,7 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle)
100 pr_crit("CPU%u: failed to start\n", cpu); 100 pr_crit("CPU%u: failed to start\n", cpu);
101 return -EIO; 101 return -EIO;
102 } 102 }
103 synchronise_count_master(cpu);
103 104
104 return 0; 105 return 0;
105} 106}
@@ -129,6 +130,8 @@ asmlinkage __init void secondary_start_kernel(void)
129 set_cpu_online(cpu, true); 130 set_cpu_online(cpu, true);
130 complete(&cpu_running); 131 complete(&cpu_running);
131 132
133 synchronise_count_slave(cpu);
134
132 local_irq_enable(); 135 local_irq_enable();
133 136
134 preempt_disable(); 137 preempt_disable();
diff --git a/arch/openrisc/kernel/sync-timer.c b/arch/openrisc/kernel/sync-timer.c
new file mode 100644
index 000000000000..ed8d835caca1
--- /dev/null
+++ b/arch/openrisc/kernel/sync-timer.c
@@ -0,0 +1,120 @@
1/*
2 * OR1K timer synchronisation
3 *
4 * Based on work from MIPS implementation.
5 *
6 * All CPUs will have their count registers synchronised to the CPU0 next time
7 * value. This can cause a small timewarp for CPU0. All other CPU's should
8 * not have done anything significant (but they may have had interrupts
9 * enabled briefly - prom_smp_finish() should not be responsible for enabling
10 * interrupts...)
11 */
12
13#include <linux/kernel.h>
14#include <linux/irqflags.h>
15#include <linux/cpumask.h>
16
17#include <asm/time.h>
18#include <asm/timex.h>
19#include <linux/atomic.h>
20#include <asm/barrier.h>
21
22#include <asm/spr.h>
23
24static unsigned int initcount;
25static atomic_t count_count_start = ATOMIC_INIT(0);
26static atomic_t count_count_stop = ATOMIC_INIT(0);
27
28#define COUNTON 100
29#define NR_LOOPS 3
30
31void synchronise_count_master(int cpu)
32{
33 int i;
34 unsigned long flags;
35
36 pr_info("Synchronize counters for CPU %u: ", cpu);
37
38 local_irq_save(flags);
39
40 /*
41 * We loop a few times to get a primed instruction cache,
42 * then the last pass is more or less synchronised and
43 * the master and slaves each set their cycle counters to a known
44 * value all at once. This reduces the chance of having random offsets
45 * between the processors, and guarantees that the maximum
46 * delay between the cycle counters is never bigger than
47 * the latency of information-passing (cachelines) between
48 * two CPUs.
49 */
50
51 for (i = 0; i < NR_LOOPS; i++) {
52 /* slaves loop on '!= 2' */
53 while (atomic_read(&count_count_start) != 1)
54 mb();
55 atomic_set(&count_count_stop, 0);
56 smp_wmb();
57
58 /* Let the slave writes its count register */
59 atomic_inc(&count_count_start);
60
61 /* Count will be initialised to current timer */
62 if (i == 1)
63 initcount = get_cycles();
64
65 /*
66 * Everyone initialises count in the last loop:
67 */
68 if (i == NR_LOOPS-1)
69 openrisc_timer_set(initcount);
70
71 /*
72 * Wait for slave to leave the synchronization point:
73 */
74 while (atomic_read(&count_count_stop) != 1)
75 mb();
76 atomic_set(&count_count_start, 0);
77 smp_wmb();
78 atomic_inc(&count_count_stop);
79 }
80 /* Arrange for an interrupt in a short while */
81 openrisc_timer_set_next(COUNTON);
82
83 local_irq_restore(flags);
84
85 /*
86 * i386 code reported the skew here, but the
87 * count registers were almost certainly out of sync
88 * so no point in alarming people
89 */
90 pr_cont("done.\n");
91}
92
93void synchronise_count_slave(int cpu)
94{
95 int i;
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 openrisc_timer_set(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 openrisc_timer_set_next(COUNTON);
119}
120#undef NR_LOOPS
diff --git a/arch/openrisc/kernel/time.c b/arch/openrisc/kernel/time.c
index ab04eaedbf8d..6baecea27080 100644
--- a/arch/openrisc/kernel/time.c
+++ b/arch/openrisc/kernel/time.c
@@ -27,8 +27,14 @@
27 27
28#include <asm/cpuinfo.h> 28#include <asm/cpuinfo.h>
29 29
30static int openrisc_timer_set_next_event(unsigned long delta, 30/* Test the timer ticks to count, used in sync routine */
31 struct clock_event_device *dev) 31inline void openrisc_timer_set(unsigned long count)
32{
33 mtspr(SPR_TTCR, count);
34}
35
36/* Set the timer to trigger in delta cycles */
37inline void openrisc_timer_set_next(unsigned long delta)
32{ 38{
33 u32 c; 39 u32 c;
34 40
@@ -44,7 +50,12 @@ static int openrisc_timer_set_next_event(unsigned long delta,
44 * Keep timer in continuous mode always. 50 * Keep timer in continuous mode always.
45 */ 51 */
46 mtspr(SPR_TTMR, SPR_TTMR_CR | SPR_TTMR_IE | c); 52 mtspr(SPR_TTMR, SPR_TTMR_CR | SPR_TTMR_IE | c);
53}
47 54
55static int openrisc_timer_set_next_event(unsigned long delta,
56 struct clock_event_device *dev)
57{
58 openrisc_timer_set_next(delta);
48 return 0; 59 return 0;
49} 60}
50 61