sync-r4k.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Count register synchronisation.
  3. *
  4. * All CPUs will have their count registers synchronised to the CPU0 next time
  5. * value. This can cause a small timewarp for CPU0. All other CPU's should
  6. * not have done anything significant (but they may have had interrupts
  7. * enabled briefly - prom_smp_finish() should not be responsible for enabling
  8. * interrupts...)
  9. *
  10. * FIXME: broken for SMTC
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/irqflags.h>
  15. #include <linux/cpumask.h>
  16. #include <asm/r4k-timer.h>
  17. #include <linux/atomic.h>
  18. #include <asm/barrier.h>
  19. #include <asm/mipsregs.h>
  20. static atomic_t __cpuinitdata count_start_flag = ATOMIC_INIT(0);
  21. static atomic_t __cpuinitdata count_count_start = ATOMIC_INIT(0);
  22. static atomic_t __cpuinitdata count_count_stop = ATOMIC_INIT(0);
  23. static atomic_t __cpuinitdata count_reference = ATOMIC_INIT(0);
  24. #define COUNTON 100
  25. #define NR_LOOPS 5
  26. void __cpuinit synchronise_count_master(int cpu)
  27. {
  28. int i;
  29. unsigned long flags;
  30. unsigned int initcount;
  31. #ifdef CONFIG_MIPS_MT_SMTC
  32. /*
  33. * SMTC needs to synchronise per VPE, not per CPU
  34. * ignore for now
  35. */
  36. return;
  37. #endif
  38. printk(KERN_INFO "Synchronize counters for CPU %u: ", cpu);
  39. local_irq_save(flags);
  40. /*
  41. * Notify the slaves that it's time to start
  42. */
  43. atomic_set(&count_reference, read_c0_count());
  44. atomic_set(&count_start_flag, cpu);
  45. smp_wmb();
  46. /* Count will be initialised to current timer for all CPU's */
  47. initcount = read_c0_count();
  48. /*
  49. * We loop a few times to get a primed instruction cache,
  50. * then the last pass is more or less synchronised and
  51. * the master and slaves each set their cycle counters to a known
  52. * value all at once. This reduces the chance of having random offsets
  53. * between the processors, and guarantees that the maximum
  54. * delay between the cycle counters is never bigger than
  55. * the latency of information-passing (cachelines) between
  56. * two CPUs.
  57. */
  58. for (i = 0; i < NR_LOOPS; i++) {
  59. /* slaves loop on '!= 2' */
  60. while (atomic_read(&count_count_start) != 1)
  61. mb();
  62. atomic_set(&count_count_stop, 0);
  63. smp_wmb();
  64. /* this lets the slaves write their count register */
  65. atomic_inc(&count_count_start);
  66. /*
  67. * Everyone initialises count in the last loop:
  68. */
  69. if (i == NR_LOOPS-1)
  70. write_c0_count(initcount);
  71. /*
  72. * Wait for all slaves to leave the synchronization point:
  73. */
  74. while (atomic_read(&count_count_stop) != 1)
  75. mb();
  76. atomic_set(&count_count_start, 0);
  77. smp_wmb();
  78. atomic_inc(&count_count_stop);
  79. }
  80. /* Arrange for an interrupt in a short while */
  81. write_c0_compare(read_c0_count() + COUNTON);
  82. atomic_set(&count_start_flag, 0);
  83. local_irq_restore(flags);
  84. /*
  85. * i386 code reported the skew here, but the
  86. * count registers were almost certainly out of sync
  87. * so no point in alarming people
  88. */
  89. printk("done.\n");
  90. }
  91. void __cpuinit synchronise_count_slave(int cpu)
  92. {
  93. int i;
  94. unsigned int initcount;
  95. #ifdef CONFIG_MIPS_MT_SMTC
  96. /*
  97. * SMTC needs to synchronise per VPE, not per CPU
  98. * ignore for now
  99. */
  100. return;
  101. #endif
  102. /*
  103. * Not every cpu is online at the time this gets called,
  104. * so we first wait for the master to say everyone is ready
  105. */
  106. while (atomic_read(&count_start_flag) != cpu)
  107. mb();
  108. /* Count will be initialised to next expire for all CPU's */
  109. initcount = atomic_read(&count_reference);
  110. for (i = 0; i < NR_LOOPS; i++) {
  111. atomic_inc(&count_count_start);
  112. while (atomic_read(&count_count_start) != 2)
  113. mb();
  114. /*
  115. * Everyone initialises count in the last loop:
  116. */
  117. if (i == NR_LOOPS-1)
  118. write_c0_count(initcount);
  119. atomic_inc(&count_count_stop);
  120. while (atomic_read(&count_count_stop) != 2)
  121. mb();
  122. }
  123. /* Arrange for an interrupt in a short while */
  124. write_c0_compare(read_c0_count() + COUNTON);
  125. }
  126. #undef NR_LOOPS