sync-r4k.c 2.9 KB

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