smp-tbsync.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Smp timebase synchronization for ppc.
  3. *
  4. * Copyright (C) 2003 Samuel Rydh (samuel@ibrium.se)
  5. *
  6. */
  7. #include <linux/config.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/smp.h>
  11. #include <linux/unistd.h>
  12. #include <linux/init.h>
  13. #include <asm/atomic.h>
  14. #include <asm/smp.h>
  15. #include <asm/time.h>
  16. #define NUM_ITER 300
  17. enum {
  18. kExit=0, kSetAndTest, kTest
  19. };
  20. static struct {
  21. volatile long tb;
  22. volatile long mark;
  23. volatile int cmd;
  24. volatile int handshake;
  25. int filler[3];
  26. volatile int ack;
  27. int filler2[7];
  28. volatile int race_result;
  29. } *tbsync;
  30. static volatile int running;
  31. static void __devinit
  32. enter_contest( long mark, long add )
  33. {
  34. while( (long)(mftb() - mark) < 0 )
  35. tbsync->race_result = add;
  36. }
  37. void __devinit
  38. smp_generic_take_timebase( void )
  39. {
  40. int cmd;
  41. long tb;
  42. local_irq_disable();
  43. while( !running )
  44. ;
  45. rmb();
  46. for( ;; ) {
  47. tbsync->ack = 1;
  48. while( !tbsync->handshake )
  49. ;
  50. rmb();
  51. cmd = tbsync->cmd;
  52. tb = tbsync->tb;
  53. tbsync->ack = 0;
  54. if( cmd == kExit )
  55. return;
  56. if( cmd == kSetAndTest ) {
  57. while( tbsync->handshake )
  58. ;
  59. asm volatile ("mttbl %0" :: "r" (tb & 0xfffffffful) );
  60. asm volatile ("mttbu %0" :: "r" (tb >> 32) );
  61. } else {
  62. while( tbsync->handshake )
  63. ;
  64. }
  65. enter_contest( tbsync->mark, -1 );
  66. }
  67. local_irq_enable();
  68. }
  69. static int __devinit
  70. start_contest( int cmd, long offset, long num )
  71. {
  72. int i, score=0;
  73. long tb, mark;
  74. tbsync->cmd = cmd;
  75. local_irq_disable();
  76. for( i=-3; i<num; ) {
  77. tb = (long)mftb() + 400;
  78. tbsync->tb = tb + offset;
  79. tbsync->mark = mark = tb + 400;
  80. wmb();
  81. tbsync->handshake = 1;
  82. while( tbsync->ack )
  83. ;
  84. while( (long)(mftb() - tb) <= 0 )
  85. ;
  86. tbsync->handshake = 0;
  87. enter_contest( mark, 1 );
  88. while( !tbsync->ack )
  89. ;
  90. if ((tbsync->tb ^ (long)mftb()) & 0x8000000000000000ul)
  91. continue;
  92. if( i++ > 0 )
  93. score += tbsync->race_result;
  94. }
  95. local_irq_enable();
  96. return score;
  97. }
  98. void __devinit
  99. smp_generic_give_timebase( void )
  100. {
  101. int i, score, score2, old, min=0, max=5000, offset=1000;
  102. printk("Synchronizing timebase\n");
  103. /* if this fails then this kernel won't work anyway... */
  104. tbsync = kmalloc( sizeof(*tbsync), GFP_KERNEL );
  105. memset( tbsync, 0, sizeof(*tbsync) );
  106. mb();
  107. running = 1;
  108. while( !tbsync->ack )
  109. ;
  110. printk("Got ack\n");
  111. /* binary search */
  112. for( old=-1 ; old != offset ; offset=(min+max)/2 ) {
  113. score = start_contest( kSetAndTest, offset, NUM_ITER );
  114. printk("score %d, offset %d\n", score, offset );
  115. if( score > 0 )
  116. max = offset;
  117. else
  118. min = offset;
  119. old = offset;
  120. }
  121. score = start_contest( kSetAndTest, min, NUM_ITER );
  122. score2 = start_contest( kSetAndTest, max, NUM_ITER );
  123. printk( "Min %d (score %d), Max %d (score %d)\n", min, score, max, score2 );
  124. score = abs( score );
  125. score2 = abs( score2 );
  126. offset = (score < score2) ? min : max;
  127. /* guard against inaccurate mttb */
  128. for( i=0; i<10; i++ ) {
  129. start_contest( kSetAndTest, offset, NUM_ITER/10 );
  130. if( (score2=start_contest(kTest, offset, NUM_ITER)) < 0 )
  131. score2 = -score2;
  132. if( score2 <= score || score2 < 20 )
  133. break;
  134. }
  135. printk("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER );
  136. /* exiting */
  137. tbsync->cmd = kExit;
  138. wmb();
  139. tbsync->handshake = 1;
  140. while( tbsync->ack )
  141. ;
  142. tbsync->handshake = 0;
  143. kfree( tbsync );
  144. tbsync = NULL;
  145. running = 0;
  146. }