sync-r4k.c 3.3 KB

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