smp-tbsync.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 u64 tb;
  22. volatile u64 mark;
  23. volatile int cmd;
  24. volatile int handshake;
  25. int filler[2];
  26. volatile int ack;
  27. int filler2[7];
  28. volatile int race_result;
  29. } *tbsync;
  30. static volatile int running;
  31. static void __devinit enter_contest(u64 mark, long add)
  32. {
  33. while (get_tb() < mark)
  34. tbsync->race_result = add;
  35. }
  36. void __devinit smp_generic_take_timebase(void)
  37. {
  38. int cmd;
  39. u64 tb;
  40. local_irq_disable();
  41. while (!running)
  42. barrier();
  43. rmb();
  44. for (;;) {
  45. tbsync->ack = 1;
  46. while (!tbsync->handshake)
  47. barrier();
  48. rmb();
  49. cmd = tbsync->cmd;
  50. tb = tbsync->tb;
  51. mb();
  52. tbsync->ack = 0;
  53. if (cmd == kExit)
  54. break;
  55. while (tbsync->handshake)
  56. barrier();
  57. if (cmd == kSetAndTest)
  58. set_tb(tb >> 32, tb & 0xfffffffful);
  59. enter_contest(tbsync->mark, -1);
  60. }
  61. local_irq_enable();
  62. }
  63. static int __devinit start_contest(int cmd, long offset, int num)
  64. {
  65. int i, score=0;
  66. u64 tb;
  67. long mark;
  68. tbsync->cmd = cmd;
  69. local_irq_disable();
  70. for (i = -3; i < num; ) {
  71. tb = get_tb() + 400;
  72. tbsync->tb = tb + offset;
  73. tbsync->mark = mark = tb + 400;
  74. wmb();
  75. tbsync->handshake = 1;
  76. while (tbsync->ack)
  77. barrier();
  78. while (get_tb() <= tb)
  79. barrier();
  80. tbsync->handshake = 0;
  81. enter_contest(mark, 1);
  82. while (!tbsync->ack)
  83. barrier();
  84. if (i++ > 0)
  85. score += tbsync->race_result;
  86. }
  87. local_irq_enable();
  88. return score;
  89. }
  90. void __devinit smp_generic_give_timebase(void)
  91. {
  92. int i, score, score2, old, min=0, max=5000, offset=1000;
  93. printk("Synchronizing timebase\n");
  94. /* if this fails then this kernel won't work anyway... */
  95. tbsync = kmalloc( sizeof(*tbsync), GFP_KERNEL );
  96. memset( tbsync, 0, sizeof(*tbsync) );
  97. mb();
  98. running = 1;
  99. while (!tbsync->ack)
  100. barrier();
  101. printk("Got ack\n");
  102. /* binary search */
  103. for (old = -1; old != offset ; offset = (min+max) / 2) {
  104. score = start_contest(kSetAndTest, offset, NUM_ITER);
  105. printk("score %d, offset %d\n", score, offset );
  106. if( score > 0 )
  107. max = offset;
  108. else
  109. min = offset;
  110. old = offset;
  111. }
  112. score = start_contest(kSetAndTest, min, NUM_ITER);
  113. score2 = start_contest(kSetAndTest, max, NUM_ITER);
  114. printk("Min %d (score %d), Max %d (score %d)\n",
  115. min, score, max, score2);
  116. score = abs(score);
  117. score2 = abs(score2);
  118. offset = (score < score2) ? min : max;
  119. /* guard against inaccurate mttb */
  120. for (i = 0; i < 10; i++) {
  121. start_contest(kSetAndTest, offset, NUM_ITER/10);
  122. if ((score2 = start_contest(kTest, offset, NUM_ITER)) < 0)
  123. score2 = -score2;
  124. if (score2 <= score || score2 < 20)
  125. break;
  126. }
  127. printk("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER );
  128. /* exiting */
  129. tbsync->cmd = kExit;
  130. wmb();
  131. tbsync->handshake = 1;
  132. while (tbsync->ack)
  133. barrier();
  134. tbsync->handshake = 0;
  135. kfree(tbsync);
  136. tbsync = NULL;
  137. running = 0;
  138. }