sync-r4k.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Count register synchronisation.
  3. *
  4. * All CPUs will have their count registers synchronised to the CPU0 expirelo
  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/r4k-timer.h>
  16. #include <asm/atomic.h>
  17. #include <asm/barrier.h>
  18. #include <asm/cpumask.h>
  19. #include <asm/mipsregs.h>
  20. static atomic_t __initdata count_start_flag = ATOMIC_INIT(0);
  21. static atomic_t __initdata count_count_start = ATOMIC_INIT(0);
  22. static atomic_t __initdata count_count_stop = ATOMIC_INIT(0);
  23. #define COUNTON 100
  24. #define NR_LOOPS 5
  25. void __init synchronise_count_master(void)
  26. {
  27. int i;
  28. unsigned long flags;
  29. unsigned int initcount;
  30. int nslaves;
  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. pr_info("Checking COUNT synchronization across %u CPUs: ",
  39. num_online_cpus());
  40. local_irq_save(flags);
  41. /*
  42. * Notify the slaves that it's time to start
  43. */
  44. atomic_set(&count_start_flag, 1);
  45. smp_wmb();
  46. /* Count will be initialised to expirelo for all CPU's */
  47. initcount = expirelo;
  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. nslaves = num_online_cpus()-1;
  59. for (i = 0; i < NR_LOOPS; i++) {
  60. /* slaves loop on '!= ncpus' */
  61. while (atomic_read(&count_count_start) != nslaves)
  62. mb();
  63. atomic_set(&count_count_stop, 0);
  64. smp_wmb();
  65. /* this lets the slaves write their count register */
  66. atomic_inc(&count_count_start);
  67. /*
  68. * Everyone initialises count in the last loop:
  69. */
  70. if (i == NR_LOOPS-1)
  71. write_c0_count(initcount);
  72. /*
  73. * Wait for all slaves to leave the synchronization point:
  74. */
  75. while (atomic_read(&count_count_stop) != nslaves)
  76. mb();
  77. atomic_set(&count_count_start, 0);
  78. smp_wmb();
  79. atomic_inc(&count_count_stop);
  80. }
  81. /* Arrange for an interrupt in a short while */
  82. write_c0_compare(read_c0_count() + COUNTON);
  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 __init synchronise_count_slave(void)
  92. {
  93. int i;
  94. unsigned long flags;
  95. unsigned int initcount;
  96. int ncpus;
  97. #ifdef CONFIG_MIPS_MT_SMTC
  98. /*
  99. * SMTC needs to synchronise per VPE, not per CPU
  100. * ignore for now
  101. */
  102. return;
  103. #endif
  104. local_irq_save(flags);
  105. /*
  106. * Not every cpu is online at the time this gets called,
  107. * so we first wait for the master to say everyone is ready
  108. */
  109. while (!atomic_read(&count_start_flag))
  110. mb();
  111. /* Count will be initialised to expirelo for all CPU's */
  112. initcount = expirelo;
  113. ncpus = num_online_cpus();
  114. for (i = 0; i < NR_LOOPS; i++) {
  115. atomic_inc(&count_count_start);
  116. while (atomic_read(&count_count_start) != ncpus)
  117. mb();
  118. /*
  119. * Everyone initialises count in the last loop:
  120. */
  121. if (i == NR_LOOPS-1)
  122. write_c0_count(initcount);
  123. atomic_inc(&count_count_stop);
  124. while (atomic_read(&count_count_stop) != ncpus)
  125. mb();
  126. }
  127. /* Arrange for an interrupt in a short while */
  128. write_c0_compare(read_c0_count() + COUNTON);
  129. local_irq_restore(flags);
  130. }
  131. #undef NR_LOOPS
  132. #endif