sync-r4k.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(void)
  27. {
  28. int i;
  29. unsigned long flags;
  30. unsigned int initcount;
  31. int nslaves;
  32. #ifdef CONFIG_MIPS_MT_SMTC
  33. /*
  34. * SMTC needs to synchronise per VPE, not per CPU
  35. * ignore for now
  36. */
  37. return;
  38. #endif
  39. printk(KERN_INFO "Synchronize counters across %u CPUs: ",
  40. num_online_cpus());
  41. local_irq_save(flags);
  42. /*
  43. * Notify the slaves that it's time to start
  44. */
  45. atomic_set(&count_reference, read_c0_count());
  46. atomic_set(&count_start_flag, 1);
  47. smp_wmb();
  48. /* Count will be initialised to current timer for all CPU's */
  49. initcount = read_c0_count();
  50. /*
  51. * We loop a few times to get a primed instruction cache,
  52. * then the last pass is more or less synchronised and
  53. * the master and slaves each set their cycle counters to a known
  54. * value all at once. This reduces the chance of having random offsets
  55. * between the processors, and guarantees that the maximum
  56. * delay between the cycle counters is never bigger than
  57. * the latency of information-passing (cachelines) between
  58. * two CPUs.
  59. */
  60. nslaves = num_online_cpus()-1;
  61. for (i = 0; i < NR_LOOPS; i++) {
  62. /* slaves loop on '!= ncpus' */
  63. while (atomic_read(&count_count_start) != nslaves)
  64. mb();
  65. atomic_set(&count_count_stop, 0);
  66. smp_wmb();
  67. /* this lets the slaves write their count register */
  68. atomic_inc(&count_count_start);
  69. /*
  70. * Everyone initialises count in the last loop:
  71. */
  72. if (i == NR_LOOPS-1)
  73. write_c0_count(initcount);
  74. /*
  75. * Wait for all slaves to leave the synchronization point:
  76. */
  77. while (atomic_read(&count_count_stop) != nslaves)
  78. mb();
  79. atomic_set(&count_count_start, 0);
  80. smp_wmb();
  81. atomic_inc(&count_count_stop);
  82. }
  83. /* Arrange for an interrupt in a short while */
  84. write_c0_compare(read_c0_count() + COUNTON);
  85. local_irq_restore(flags);
  86. /*
  87. * i386 code reported the skew here, but the
  88. * count registers were almost certainly out of sync
  89. * so no point in alarming people
  90. */
  91. printk("done.\n");
  92. }
  93. void __cpuinit synchronise_count_slave(void)
  94. {
  95. int i;
  96. unsigned long flags;
  97. unsigned int initcount;
  98. int ncpus;
  99. #ifdef CONFIG_MIPS_MT_SMTC
  100. /*
  101. * SMTC needs to synchronise per VPE, not per CPU
  102. * ignore for now
  103. */
  104. return;
  105. #endif
  106. local_irq_save(flags);
  107. /*
  108. * Not every cpu is online at the time this gets called,
  109. * so we first wait for the master to say everyone is ready
  110. */
  111. while (!atomic_read(&count_start_flag))
  112. mb();
  113. /* Count will be initialised to next expire for all CPU's */
  114. initcount = atomic_read(&count_reference);
  115. ncpus = num_online_cpus();
  116. for (i = 0; i < NR_LOOPS; i++) {
  117. atomic_inc(&count_count_start);
  118. while (atomic_read(&count_count_start) != ncpus)
  119. mb();
  120. /*
  121. * Everyone initialises count in the last loop:
  122. */
  123. if (i == NR_LOOPS-1)
  124. write_c0_count(initcount);
  125. atomic_inc(&count_count_stop);
  126. while (atomic_read(&count_count_stop) != ncpus)
  127. mb();
  128. }
  129. /* Arrange for an interrupt in a short while */
  130. write_c0_compare(read_c0_count() + COUNTON);
  131. local_irq_restore(flags);
  132. }
  133. #undef NR_LOOPS