sync-r4k.c 3.7 KB
Newer Older
1 2 3
/*
 * Count register synchronisation.
 *
4
 * All CPUs will have their count registers synchronised to the CPU0 next time
5 6 7 8 9 10 11 12 13 14 15
 * value. This can cause a small timewarp for CPU0. All other CPU's should
 * not have done anything significant (but they may have had interrupts
 * enabled briefly - prom_smp_finish() should not be responsible for enabling
 * interrupts...)
 *
 * FIXME: broken for SMTC
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/irqflags.h>
16
#include <linux/cpumask.h>
17

18
#include <asm/r4k-timer.h>
A
Arun Sharma 已提交
19
#include <linux/atomic.h>
20 21 22
#include <asm/barrier.h>
#include <asm/mipsregs.h>

23 24 25 26
static atomic_t __cpuinitdata count_start_flag = ATOMIC_INIT(0);
static atomic_t __cpuinitdata count_count_start = ATOMIC_INIT(0);
static atomic_t __cpuinitdata count_count_stop = ATOMIC_INIT(0);
static atomic_t __cpuinitdata count_reference = ATOMIC_INIT(0);
27

R
Ralf Baechle 已提交
28
#define COUNTON 100
29 30
#define NR_LOOPS 5

31
void __cpuinit synchronise_count_master(int cpu)
32 33 34 35 36 37 38 39 40 41 42 43 44
{
	int i;
	unsigned long flags;
	unsigned int initcount;

#ifdef CONFIG_MIPS_MT_SMTC
	/*
	 * SMTC needs to synchronise per VPE, not per CPU
	 * ignore for now
	 */
	return;
#endif

45
	printk(KERN_INFO "Synchronize counters for CPU %u: ", cpu);
46 47 48 49 50 51

	local_irq_save(flags);

	/*
	 * Notify the slaves that it's time to start
	 */
52
	atomic_set(&count_reference, read_c0_count());
53
	atomic_set(&count_start_flag, cpu);
54 55
	smp_wmb();

56 57
	/* Count will be initialised to current timer for all CPU's */
	initcount = read_c0_count();
58 59 60 61 62 63 64 65 66 67 68 69 70

	/*
	 * We loop a few times to get a primed instruction cache,
	 * then the last pass is more or less synchronised and
	 * the master and slaves each set their cycle counters to a known
	 * value all at once. This reduces the chance of having random offsets
	 * between the processors, and guarantees that the maximum
	 * delay between the cycle counters is never bigger than
	 * the latency of information-passing (cachelines) between
	 * two CPUs.
	 */

	for (i = 0; i < NR_LOOPS; i++) {
71 72
		/* slaves loop on '!= 2' */
		while (atomic_read(&count_count_start) != 1)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
			mb();
		atomic_set(&count_count_stop, 0);
		smp_wmb();

		/* this lets the slaves write their count register */
		atomic_inc(&count_count_start);

		/*
		 * Everyone initialises count in the last loop:
		 */
		if (i == NR_LOOPS-1)
			write_c0_count(initcount);

		/*
		 * Wait for all slaves to leave the synchronization point:
		 */
89
		while (atomic_read(&count_count_stop) != 1)
90 91 92 93 94 95 96
			mb();
		atomic_set(&count_count_start, 0);
		smp_wmb();
		atomic_inc(&count_count_stop);
	}
	/* Arrange for an interrupt in a short while */
	write_c0_compare(read_c0_count() + COUNTON);
97
	atomic_set(&count_start_flag, 0);
98 99 100 101 102 103 104 105 106 107 108

	local_irq_restore(flags);

	/*
	 * i386 code reported the skew here, but the
	 * count registers were almost certainly out of sync
	 * so no point in alarming people
	 */
	printk("done.\n");
}

109
void __cpuinit synchronise_count_slave(int cpu)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
	int i;
	unsigned int initcount;

#ifdef CONFIG_MIPS_MT_SMTC
	/*
	 * SMTC needs to synchronise per VPE, not per CPU
	 * ignore for now
	 */
	return;
#endif

	/*
	 * Not every cpu is online at the time this gets called,
	 * so we first wait for the master to say everyone is ready
	 */

127
	while (atomic_read(&count_start_flag) != cpu)
128 129
		mb();

130 131
	/* Count will be initialised to next expire for all CPU's */
	initcount = atomic_read(&count_reference);
132 133 134

	for (i = 0; i < NR_LOOPS; i++) {
		atomic_inc(&count_count_start);
135
		while (atomic_read(&count_count_start) != 2)
136 137 138 139 140 141 142 143 144
			mb();

		/*
		 * Everyone initialises count in the last loop:
		 */
		if (i == NR_LOOPS-1)
			write_c0_count(initcount);

		atomic_inc(&count_count_stop);
145
		while (atomic_read(&count_count_stop) != 2)
146 147 148 149 150 151
			mb();
	}
	/* Arrange for an interrupt in a short while */
	write_c0_compare(read_c0_count() + COUNTON);
}
#undef NR_LOOPS