/*1* OR1K timer synchronisation2*3* Based on work from MIPS implementation.4*5* All CPUs will have their count registers synchronised to the CPU0 next time6* value. This can cause a small timewarp for CPU0. All other CPU's should7* not have done anything significant (but they may have had interrupts8* enabled briefly - prom_smp_finish() should not be responsible for enabling9* interrupts...)10*/1112#include <linux/kernel.h>13#include <linux/irqflags.h>14#include <linux/cpumask.h>1516#include <asm/time.h>17#include <asm/timex.h>18#include <linux/atomic.h>19#include <asm/barrier.h>2021#include <asm/spr.h>2223static unsigned int initcount;24static atomic_t count_count_start = ATOMIC_INIT(0);25static atomic_t count_count_stop = ATOMIC_INIT(0);2627#define COUNTON 10028#define NR_LOOPS 32930void synchronise_count_master(int cpu)31{32int i;33unsigned long flags;3435pr_info("Synchronize counters for CPU %u: ", cpu);3637local_irq_save(flags);3839/*40* We loop a few times to get a primed instruction cache,41* then the last pass is more or less synchronised and42* the master and slaves each set their cycle counters to a known43* value all at once. This reduces the chance of having random offsets44* between the processors, and guarantees that the maximum45* delay between the cycle counters is never bigger than46* the latency of information-passing (cachelines) between47* two CPUs.48*/4950for (i = 0; i < NR_LOOPS; i++) {51/* slaves loop on '!= 2' */52while (atomic_read(&count_count_start) != 1)53mb();54atomic_set(&count_count_stop, 0);55smp_wmb();5657/* Let the slave writes its count register */58atomic_inc(&count_count_start);5960/* Count will be initialised to current timer */61if (i == 1)62initcount = get_cycles();6364/*65* Everyone initialises count in the last loop:66*/67if (i == NR_LOOPS-1)68openrisc_timer_set(initcount);6970/*71* Wait for slave to leave the synchronization point:72*/73while (atomic_read(&count_count_stop) != 1)74mb();75atomic_set(&count_count_start, 0);76smp_wmb();77atomic_inc(&count_count_stop);78}79/* Arrange for an interrupt in a short while */80openrisc_timer_set_next(COUNTON);8182local_irq_restore(flags);8384/*85* i386 code reported the skew here, but the86* count registers were almost certainly out of sync87* so no point in alarming people88*/89pr_cont("done.\n");90}9192void synchronise_count_slave(int cpu)93{94int i;9596/*97* Not every cpu is online at the time this gets called,98* so we first wait for the master to say everyone is ready99*/100101for (i = 0; i < NR_LOOPS; i++) {102atomic_inc(&count_count_start);103while (atomic_read(&count_count_start) != 2)104mb();105106/*107* Everyone initialises count in the last loop:108*/109if (i == NR_LOOPS-1)110openrisc_timer_set(initcount);111112atomic_inc(&count_count_stop);113while (atomic_read(&count_count_stop) != 2)114mb();115}116/* Arrange for an interrupt in a short while */117openrisc_timer_set_next(COUNTON);118}119#undef NR_LOOPS120121122