smp-tbsync.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/slab.h>
  12. #include <linux/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 u64 tb;
  21. volatile u64 mark;
  22. volatile int cmd;
  23. volatile int handshake;
  24. int filler[2];
  25. volatile int ack;
  26. int filler2[7];
  27. volatile int race_result;
  28. } *tbsync;
  29. static volatile int running;
  30. static void enter_contest(u64 mark, long add)
  31. {
  32. while (get_tb() < mark)
  33. tbsync->race_result = add;
  34. }
  35. void smp_generic_take_timebase(void)
  36. {
  37. int cmd;
  38. u64 tb;
  39. unsigned long flags;
  40. local_irq_save(flags);
  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_restore(flags);
  62. }
  63. static int start_contest(int cmd, long offset, int num)
  64. {
  65. int i, score=0;
  66. u64 tb;
  67. u64 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 smp_generic_give_timebase(void)
  91. {
  92. int i, score, score2, old, min=0, max=5000, offset=1000;
  93. pr_debug("Software timebase sync\n");
  94. /* if this fails then this kernel won't work anyway... */
  95. tbsync = kzalloc( sizeof(*tbsync), GFP_KERNEL );
  96. mb();
  97. running = 1;
  98. while (!tbsync->ack)
  99. barrier();
  100. pr_debug("Got ack\n");
  101. /* binary search */
  102. for (old = -1; old != offset ; offset = (min+max) / 2) {
  103. score = start_contest(kSetAndTest, offset, NUM_ITER);
  104. pr_debug("score %d, offset %d\n", score, offset );
  105. if( score > 0 )
  106. max = offset;
  107. else
  108. min = offset;
  109. old = offset;
  110. }
  111. score = start_contest(kSetAndTest, min, NUM_ITER);
  112. score2 = start_contest(kSetAndTest, max, NUM_ITER);
  113. pr_debug("Min %d (score %d), Max %d (score %d)\n",
  114. min, score, max, score2);
  115. score = abs(score);
  116. score2 = abs(score2);
  117. offset = (score < score2) ? min : max;
  118. /* guard against inaccurate mttb */
  119. for (i = 0; i < 10; i++) {
  120. start_contest(kSetAndTest, offset, NUM_ITER/10);
  121. if ((score2 = start_contest(kTest, offset, NUM_ITER)) < 0)
  122. score2 = -score2;
  123. if (score2 <= score || score2 < 20)
  124. break;
  125. }
  126. pr_debug("Final offset: %d (%d/%d)\n", offset, score2, NUM_ITER );
  127. /* exiting */
  128. tbsync->cmd = kExit;
  129. wmb();
  130. tbsync->handshake = 1;
  131. while (tbsync->ack)
  132. barrier();
  133. tbsync->handshake = 0;
  134. kfree(tbsync);
  135. tbsync = NULL;
  136. running = 0;
  137. }