100-clocksource-drivers-arch_timer-Workaround-for-Allwin.patch 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. From 7cd6dca3600d8d71328950216688ecd00015d1ce Mon Sep 17 00:00:00 2001
  2. From: Samuel Holland <[email protected]>
  3. Date: Sat, 12 Jan 2019 20:17:18 -0600
  4. Subject: [PATCH] clocksource/drivers/arch_timer: Workaround for Allwinner A64
  5. timer instability
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. The Allwinner A64 SoC is known[1] to have an unstable architectural
  10. timer, which manifests itself most obviously in the time jumping forward
  11. a multiple of 95 years[2][3]. This coincides with 2^56 cycles at a
  12. timer frequency of 24 MHz, implying that the time went slightly backward
  13. (and this was interpreted by the kernel as it jumping forward and
  14. wrapping around past the epoch).
  15. Investigation revealed instability in the low bits of CNTVCT at the
  16. point a high bit rolls over. This leads to power-of-two cycle forward
  17. and backward jumps. (Testing shows that forward jumps are about twice as
  18. likely as backward jumps.) Since the counter value returns to normal
  19. after an indeterminate read, each "jump" really consists of both a
  20. forward and backward jump from the software perspective.
  21. Unless the kernel is trapping CNTVCT reads, a userspace program is able
  22. to read the register in a loop faster than it changes. A test program
  23. running on all 4 CPU cores that reported jumps larger than 100 ms was
  24. run for 13.6 hours and reported the following:
  25. Count | Event
  26. -------+---------------------------
  27. 9940 | jumped backward 699ms
  28. 268 | jumped backward 1398ms
  29. 1 | jumped backward 2097ms
  30. 16020 | jumped forward 175ms
  31. 6443 | jumped forward 699ms
  32. 2976 | jumped forward 1398ms
  33. 9 | jumped forward 356516ms
  34. 9 | jumped forward 357215ms
  35. 4 | jumped forward 714430ms
  36. 1 | jumped forward 3578440ms
  37. This works out to a jump larger than 100 ms about every 5.5 seconds on
  38. each CPU core.
  39. The largest jump (almost an hour!) was the following sequence of reads:
  40. 0x0000007fffffffff → 0x00000093feffffff → 0x0000008000000000
  41. Note that the middle bits don't necessarily all read as all zeroes or
  42. all ones during the anomalous behavior; however the low 10 bits checked
  43. by the function in this patch have never been observed with any other
  44. value.
  45. Also note that smaller jumps are much more common, with backward jumps
  46. of 2048 (2^11) cycles observed over 400 times per second on each core.
  47. (Of course, this is partially explained by lower bits rolling over more
  48. frequently.) Any one of these could have caused the 95 year time skip.
  49. Similar anomalies were observed while reading CNTPCT (after patching the
  50. kernel to allow reads from userspace). However, the CNTPCT jumps are
  51. much less frequent, and only small jumps were observed. The same program
  52. as before (except now reading CNTPCT) observed after 72 hours:
  53. Count | Event
  54. -------+---------------------------
  55. 17 | jumped backward 699ms
  56. 52 | jumped forward 175ms
  57. 2831 | jumped forward 699ms
  58. 5 | jumped forward 1398ms
  59. Further investigation showed that the instability in CNTPCT/CNTVCT also
  60. affected the respective timer's TVAL register. The following values were
  61. observed immediately after writing CNVT_TVAL to 0x10000000:
  62. CNTVCT | CNTV_TVAL | CNTV_CVAL | CNTV_TVAL Error
  63. --------------------+------------+--------------------+-----------------
  64. 0x000000d4a2d8bfff | 0x10003fff | 0x000000d4b2d8bfff | +0x00004000
  65. 0x000000d4a2d94000 | 0x0fffffff | 0x000000d4b2d97fff | -0x00004000
  66. 0x000000d4a2d97fff | 0x10003fff | 0x000000d4b2d97fff | +0x00004000
  67. 0x000000d4a2d9c000 | 0x0fffffff | 0x000000d4b2d9ffff | -0x00004000
  68. The pattern of errors in CNTV_TVAL seemed to depend on exactly which
  69. value was written to it. For example, after writing 0x10101010:
  70. CNTVCT | CNTV_TVAL | CNTV_CVAL | CNTV_TVAL Error
  71. --------------------+------------+--------------------+-----------------
  72. 0x000001ac3effffff | 0x1110100f | 0x000001ac4f10100f | +0x1000000
  73. 0x000001ac40000000 | 0x1010100f | 0x000001ac5110100f | -0x1000000
  74. 0x000001ac58ffffff | 0x1110100f | 0x000001ac6910100f | +0x1000000
  75. 0x000001ac66000000 | 0x1010100f | 0x000001ac7710100f | -0x1000000
  76. 0x000001ac6affffff | 0x1110100f | 0x000001ac7b10100f | +0x1000000
  77. 0x000001ac6e000000 | 0x1010100f | 0x000001ac7f10100f | -0x1000000
  78. I was also twice able to reproduce the issue covered by Allwinner's
  79. workaround[4], that writing to TVAL sometimes fails, and both CVAL and
  80. TVAL are left with entirely bogus values. One was the following values:
  81. CNTVCT | CNTV_TVAL | CNTV_CVAL
  82. --------------------+------------+--------------------------------------
  83. 0x000000d4a2d6014c | 0x8fbd5721 | 0x000000d132935fff (615s in the past)
  84. Reviewed-by: Marc Zyngier <[email protected]>
  85. ========================================================================
  86. Because the CPU can read the CNTPCT/CNTVCT registers faster than they
  87. change, performing two reads of the register and comparing the high bits
  88. (like other workarounds) is not a workable solution. And because the
  89. timer can jump both forward and backward, no pair of reads can
  90. distinguish a good value from a bad one. The only way to guarantee a
  91. good value from consecutive reads would be to read _three_ times, and
  92. take the middle value only if the three values are 1) each unique and
  93. 2) increasing. This takes at minimum 3 counter cycles (125 ns), or more
  94. if an anomaly is detected.
  95. However, since there is a distinct pattern to the bad values, we can
  96. optimize the common case (1022/1024 of the time) to a single read by
  97. simply ignoring values that match the error pattern. This still takes no
  98. more than 3 cycles in the worst case, and requires much less code. As an
  99. additional safety check, we still limit the loop iteration to the number
  100. of max-frequency (1.2 GHz) CPU cycles in three 24 MHz counter periods.
  101. For the TVAL registers, the simple solution is to not use them. Instead,
  102. read or write the CVAL and calculate the TVAL value in software.
  103. Although the manufacturer is aware of at least part of the erratum[4],
  104. there is no official name for it. For now, use the kernel-internal name
  105. "UNKNOWN1".
  106. [1]: https://github.com/armbian/build/commit/a08cd6fe7ae9
  107. [2]: https://forum.armbian.com/topic/3458-a64-datetime-clock-issue/
  108. [3]: https://irclog.whitequark.org/linux-sunxi/2018-01-26
  109. [4]: https://github.com/Allwinner-Homlet/H6-BSP4.9-linux/blob/master/drivers/clocksource/arm_arch_timer.c#L272
  110. Acked-by: Maxime Ripard <[email protected]>
  111. Tested-by: Andre Przywara <[email protected]>
  112. Signed-off-by: Samuel Holland <[email protected]>
  113. Cc: [email protected]
  114. Signed-off-by: Daniel Lezcano <[email protected]>
  115. ---
  116. Documentation/arm64/silicon-errata.txt | 2 +
  117. drivers/clocksource/Kconfig | 10 +++++
  118. drivers/clocksource/arm_arch_timer.c | 55 ++++++++++++++++++++++++++
  119. 3 files changed, 67 insertions(+)
  120. --- a/drivers/clocksource/arm_arch_timer.c
  121. +++ b/drivers/clocksource/arm_arch_timer.c
  122. @@ -361,6 +361,48 @@ static u32 notrace sun50i_a64_read_cntv_
  123. }
  124. #endif
  125. +#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
  126. +/*
  127. + * The low bits of the counter registers are indeterminate while bit 10 or
  128. + * greater is rolling over. Since the counter value can jump both backward
  129. + * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
  130. + * with all ones or all zeros in the low bits. Bound the loop by the maximum
  131. + * number of CPU cycles in 3 consecutive 24 MHz counter periods.
  132. + */
  133. +#define __sun50i_a64_read_reg(reg) ({ \
  134. + u64 _val; \
  135. + int _retries = 150; \
  136. + \
  137. + do { \
  138. + _val = read_sysreg(reg); \
  139. + _retries--; \
  140. + } while (((_val + 1) & GENMASK(9, 0)) <= 1 && _retries); \
  141. + \
  142. + WARN_ON_ONCE(!_retries); \
  143. + _val; \
  144. +})
  145. +
  146. +static u64 notrace sun50i_a64_read_cntpct_el0(void)
  147. +{
  148. + return __sun50i_a64_read_reg(cntpct_el0);
  149. +}
  150. +
  151. +static u64 notrace sun50i_a64_read_cntvct_el0(void)
  152. +{
  153. + return __sun50i_a64_read_reg(cntvct_el0);
  154. +}
  155. +
  156. +static u32 notrace sun50i_a64_read_cntp_tval_el0(void)
  157. +{
  158. + return read_sysreg(cntp_cval_el0) - sun50i_a64_read_cntpct_el0();
  159. +}
  160. +
  161. +static u32 notrace sun50i_a64_read_cntv_tval_el0(void)
  162. +{
  163. + return read_sysreg(cntv_cval_el0) - sun50i_a64_read_cntvct_el0();
  164. +}
  165. +#endif
  166. +
  167. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  168. DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
  169. EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
  170. @@ -451,6 +493,19 @@ static const struct arch_timer_erratum_w
  171. },
  172. #endif
  173. #ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
  174. + {
  175. + .match_type = ate_match_dt,
  176. + .id = "allwinner,erratum-unknown1",
  177. + .desc = "Allwinner erratum UNKNOWN1",
  178. + .read_cntp_tval_el0 = sun50i_a64_read_cntp_tval_el0,
  179. + .read_cntv_tval_el0 = sun50i_a64_read_cntv_tval_el0,
  180. + .read_cntpct_el0 = sun50i_a64_read_cntpct_el0,
  181. + .read_cntvct_el0 = sun50i_a64_read_cntvct_el0,
  182. + .set_next_event_phys = erratum_set_next_event_tval_phys,
  183. + .set_next_event_virt = erratum_set_next_event_tval_virt,
  184. + },
  185. +#endif
  186. +#ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
  187. {
  188. .match_type = ate_match_dt,
  189. .id = "allwinner,erratum-unknown1",