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/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/smp.h>
  10. #include <linux/unistd.h>
  11. #include <linux/init.h>
  12. #include <asm/atomic.h>
  13. #include <asm/smp.h>
  14. #include <asm/time.h>
  15. #define NUM_ITER 300
  16. enum {
  17. kExit=0, kSetAndTest, kTest
  18. };
  19. static struct {
  20. volatile int tbu;
  21. volatile int tbl;
  22. volatile int 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( int mark, int add )
  33. {
  34. while( (int)(get_tbl() - mark) < 0 )
  35. tbsync->race_result = add;
  36. }
  37. void __devinit
  38. smp_generic_take_timebase( void )
  39. {
  40. int cmd, tbl, tbu;
  41. local_irq_disable();
  42. while( !running )
  43. ;
  44. rmb();
  45. for( ;; ) {
  46. tbsync->ack = 1;
  47. while( !tbsync->handshake )
  48. ;
  49. rmb();
  50. cmd = tbsync->cmd;
  51. tbl = tbsync->tbl;
  52. tbu = tbsync->tbu;
  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" (tbl) );
  60. asm volatile ("mttbu %0" :: "r" (tbu) );
  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, int offset, int num )
  71. {
  72. int i, tbu, tbl, mark, score=0;
  73. tbsync->cmd = cmd;
  74. local_irq_disable();
  75. for( i=-3; i<num; ) {
  76. tbl = get_tbl() + 400;
  77. tbsync->tbu = tbu = get_tbu();
  78. tbsync->tbl = tbl + offset;
  79. tbsync->mark = mark = tbl + 400;
  80. wmb();
  81. tbsync->handshake = 1;
  82. while( tbsync->ack )
  83. ;
  84. while( (int)(get_tbl() - tbl) <= 0 )
  85. ;
  86. tbsync->handshake = 0;
  87. enter_contest( mark, 1 );
  88. while( !tbsync->ack )
  89. ;
  90. if( tbsync->tbu != get_tbu() || ((tbsync->tbl ^ get_tbl()) & 0x80000000) )
  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 = kzalloc( sizeof(*tbsync), GFP_KERNEL );
  105. mb();
  106. running = 1;
  107. while( !tbsync->ack )
  108. ;
  109. /* binary search */
  110. for( old=-1 ; old != offset ; offset=(min+max)/2 ) {
  111. score = start_contest( kSetAndTest, offset, NUM_ITER );
  112. printk("score %d, offset %d\n", score, offset );
  113. if( score > 0 )
  114. max = offset;
  115. else
  116. min = offset;
  117. old = offset;
  118. }
  119. score = start_contest( kSetAndTest, min, NUM_ITER );
  120. score2 = start_contest( kSetAndTest, max, NUM_ITER );
  121. printk( "Min %d (score %d), Max %d (score %d)\n", min, score, max, score2 );
  122. score = abs( score );
  123. score2 = abs( score2 );
  124. offset = (score < score2) ? min : max;
  125. /* guard against inaccurate mttb */
  126. for( i=0; i<10; i++ ) {
  127. start_contest( kSetAndTest, offset, NUM_ITER/10 );
  128. if( (score2=start_contest(kTest, offset, NUM_ITER)) < 0 )
  129. score2 = -score2;
  130. if( score2 <= score || score2 < 20 )
  131. break;
  132. }
  133. printk("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER );
  134. /* exiting */
  135. tbsync->cmd = kExit;
  136. wmb();
  137. tbsync->handshake = 1;
  138. while( tbsync->ack )
  139. ;
  140. tbsync->handshake = 0;
  141. kfree( tbsync );
  142. tbsync = NULL;
  143. running = 0;
  144. /* all done */
  145. smp_tb_synchronized = 1;
  146. }