| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Smp timebase synchronization for ppc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2003 Samuel Rydh (samuel@ibrium.se) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/kernel.h>
 | 
					
						
							|  |  |  | #include <linux/sched.h>
 | 
					
						
							|  |  |  | #include <linux/smp.h>
 | 
					
						
							|  |  |  | #include <linux/unistd.h>
 | 
					
						
							|  |  |  | #include <linux/init.h>
 | 
					
						
							|  |  |  | #include <asm/atomic.h>
 | 
					
						
							|  |  |  | #include <asm/smp.h>
 | 
					
						
							|  |  |  | #include <asm/time.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define NUM_ITER		300
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum { | 
					
						
							|  |  |  | 	kExit=0, kSetAndTest, kTest | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct { | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	volatile u64		tb; | 
					
						
							|  |  |  | 	volatile u64		mark; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	volatile int		cmd; | 
					
						
							|  |  |  | 	volatile int		handshake; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	int			filler[2]; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	volatile int		ack; | 
					
						
							|  |  |  | 	int			filler2[7]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	volatile int		race_result; | 
					
						
							|  |  |  | } *tbsync; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static volatile int		running; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | static void __devinit enter_contest(u64 mark, long add) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	while (get_tb() < mark) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		tbsync->race_result = add; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | void __devinit smp_generic_take_timebase(void) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int cmd; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	u64 tb; | 
					
						
							| 
									
										
										
										
											2006-08-30 16:10:47 +10:00
										 |  |  | 	unsigned long flags; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-30 16:10:47 +10:00
										 |  |  | 	local_irq_save(flags); | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	while (!running) | 
					
						
							|  |  |  | 		barrier(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	rmb(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	for (;;) { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		tbsync->ack = 1; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		while (!tbsync->handshake) | 
					
						
							|  |  |  | 			barrier(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		rmb(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cmd = tbsync->cmd; | 
					
						
							|  |  |  | 		tb = tbsync->tb; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		mb(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		tbsync->ack = 0; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		if (cmd == kExit) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		while (tbsync->handshake) | 
					
						
							|  |  |  | 			barrier(); | 
					
						
							|  |  |  | 		if (cmd == kSetAndTest) | 
					
						
							|  |  |  | 			set_tb(tb >> 32, tb & 0xfffffffful); | 
					
						
							|  |  |  | 		enter_contest(tbsync->mark, -1); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-08-30 16:10:47 +10:00
										 |  |  | 	local_irq_restore(flags); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | static int __devinit start_contest(int cmd, long offset, int num) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i, score=0; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	u64 tb; | 
					
						
							| 
									
										
										
										
											2006-11-17 14:35:48 +00:00
										 |  |  | 	u64 mark; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tbsync->cmd = cmd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	local_irq_disable(); | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	for (i = -3; i < num; ) { | 
					
						
							|  |  |  | 		tb = get_tb() + 400; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		tbsync->tb = tb + offset; | 
					
						
							|  |  |  | 		tbsync->mark = mark = tb + 400; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		wmb(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		tbsync->handshake = 1; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		while (tbsync->ack) | 
					
						
							|  |  |  | 			barrier(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		while (get_tb() <= tb) | 
					
						
							|  |  |  | 			barrier(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		tbsync->handshake = 0; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		enter_contest(mark, 1); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		while (!tbsync->ack) | 
					
						
							|  |  |  | 			barrier(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		if (i++ > 0) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 			score += tbsync->race_result; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	local_irq_enable(); | 
					
						
							|  |  |  | 	return score; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | void __devinit smp_generic_give_timebase(void) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i, score, score2, old, min=0, max=5000, offset=1000; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-14 18:25:28 +00:00
										 |  |  | 	pr_debug("Software timebase sync\n"); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* if this fails then this kernel won't work anyway... */ | 
					
						
							| 
									
										
										
										
											2006-12-02 13:26:57 +02:00
										 |  |  | 	tbsync = kzalloc( sizeof(*tbsync), GFP_KERNEL ); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	mb(); | 
					
						
							|  |  |  | 	running = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	while (!tbsync->ack) | 
					
						
							|  |  |  | 		barrier(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-14 18:25:28 +00:00
										 |  |  | 	pr_debug("Got ack\n"); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* binary search */ | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	for (old = -1; old != offset ; offset = (min+max) / 2) { | 
					
						
							|  |  |  | 		score = start_contest(kSetAndTest, offset, NUM_ITER); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-14 18:25:28 +00:00
										 |  |  | 		pr_debug("score %d, offset %d\n", score, offset ); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if( score > 0 ) | 
					
						
							|  |  |  | 			max = offset; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			min = offset; | 
					
						
							|  |  |  | 		old = offset; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	score = start_contest(kSetAndTest, min, NUM_ITER); | 
					
						
							|  |  |  | 	score2 = start_contest(kSetAndTest, max, NUM_ITER); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-14 18:25:28 +00:00
										 |  |  | 	pr_debug("Min %d (score %d), Max %d (score %d)\n", | 
					
						
							|  |  |  | 		 min, score, max, score2); | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	score = abs(score); | 
					
						
							|  |  |  | 	score2 = abs(score2); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	offset = (score < score2) ? min : max; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* guard against inaccurate mttb */ | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	for (i = 0; i < 10; i++) { | 
					
						
							|  |  |  | 		start_contest(kSetAndTest, offset, NUM_ITER/10); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		if ((score2 = start_contest(kTest, offset, NUM_ITER)) < 0) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 			score2 = -score2; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 		if (score2 <= score || score2 < 20) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-10-14 18:25:28 +00:00
										 |  |  | 	pr_debug("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER ); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* exiting */ | 
					
						
							|  |  |  | 	tbsync->cmd = kExit; | 
					
						
							|  |  |  | 	wmb(); | 
					
						
							|  |  |  | 	tbsync->handshake = 1; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	while (tbsync->ack) | 
					
						
							|  |  |  | 		barrier(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	tbsync->handshake = 0; | 
					
						
							| 
									
										
										
										
											2005-11-04 13:28:58 +11:00
										 |  |  | 	kfree(tbsync); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	tbsync = NULL; | 
					
						
							|  |  |  | 	running = 0; | 
					
						
							|  |  |  | } |