GUIDINGLI commented on code in PR #21966:
URL: https://github.com/apache/nuttx/pull/21966#discussion_r4609297653


##########
arch/x86_64/src/intel64/intel64_rtc.c:
##########
@@ -92,17 +92,21 @@ static unsigned long rtc_read(void)
   uint64_t  tmr = rdtscp();
   irqstate_t flags;
 
+  /* The 64-bit TSC does not wrap.  A smaller value comes from a CPU whose
+   * TSC is behind the TSC of the CPU that read last: keep the last value.
+   */
+
   flags = spin_lock_irqsave(&g_rtc_lock);
   if (tmr < rtc_last)
     {
-      tmr += (0xffffffffffffffffull - rtc_last);
+      tmr = rtc_last;
     }
 
   rtc_last = tmr;
   spin_unlock_irqrestore(&g_rtc_lock, flags);
-  tmr = (tmr / (g_x86_64_timer_freq / 1000000ul)) * 1000l;
 
-  return tmr;
+  return tmr / g_x86_64_timer_freq * NS_PER_SEC +

Review Comment:
   Non-blocking, pre-existing, but worth a follow-up: this divides TSC ticks
   by `g_x86_64_timer_freq`, which is the TSC frequency only when
   `CONFIG_ARCH_INTEL64_TSC_DEADLINE` is the system timer source:
   
   - With `CONFIG_ARCH_INTEL64_TSC` ("TSC APIC timer"),
     `x86_64_timer_calibrate_freq()` stores `CONFIG_ARCH_INTEL64_APIC_FREQ_KHZ`
     — a separate, manually specified knob (default 2 GHz) — so the clock runs
     scaled by `tsc_freq / apic_freq`.
   - With `CONFIG_ARCH_INTEL64_HPET_ALARM`, `g_x86_64_timer_freq` is never
     assigned at all: the `== 0` PANIC in `x86_64_timer_calibrate_freq()` sits
     inside `#ifdef CONFIG_ARCH_INTEL64_HAVE_TSC`, and HPET_ALARM does not
     select it. The first `rtc_read()` then divides by zero.
   
   `CONFIG_RTC_HIRES` has no dependency on the timer source, so both
   combinations build. No in-tree x86_64 defconfig enables `CONFIG_RTC` today,
   so nothing in-tree is affected — only custom configurations. And since the
   previous code divided by the same variable, this is not a regression.
   
   For the follow-up, either derive the TSC frequency independently of the
   system timer source (the CPUID 0x15 / 0x40000010 helpers already exist in
   `intel64_freq.c`), or make this RTC depend on
   `ARCH_INTEL64_TSC_DEADLINE`. An issue to track it would help.



##########
arch/x86_64/src/intel64/intel64_rtc.c:
##########
@@ -92,17 +92,21 @@ static unsigned long rtc_read(void)
   uint64_t  tmr = rdtscp();
   irqstate_t flags;
 
+  /* The 64-bit TSC does not wrap.  A smaller value comes from a CPU whose
+   * TSC is behind the TSC of the CPU that read last: keep the last value.
+   */
+
   flags = spin_lock_irqsave(&g_rtc_lock);
   if (tmr < rtc_last)
     {
-      tmr += (0xffffffffffffffffull - rtc_last);
+      tmr = rtc_last;
     }
 
   rtc_last = tmr;
   spin_unlock_irqrestore(&g_rtc_lock, flags);
-  tmr = (tmr / (g_x86_64_timer_freq / 1000000ul)) * 1000l;
 
-  return tmr;
+  return tmr / g_x86_64_timer_freq * NS_PER_SEC +
+         tmr % g_x86_64_timer_freq * NS_PER_SEC / g_x86_64_timer_freq;

Review Comment:
   Since this now mirrors `up_perf_convert()` in `intel64_perf.c` (as the
   commit message notes), a follow-up could share the implementation:
   `rtc_read()` could return the clamped tick count and `up_rtc_gettime()`
   could call `up_perf_convert()` on it, which would also drop the
   ns → sec/nsec round trip it performs today. One wrinkle:
   `up_perf_convert()` only compiles under `CONFIG_ARCH_HAVE_PERF_EVENTS`, so
   sharing would need that guard lifted or the helper moved somewhere common.
   
   Nit: the `NS_PER_USEC`/`NS_PER_MSEC` defines above are unused in this file,
   and `NS_PER_SEC` duplicates `NSEC_PER_SEC` from `<nuttx/clock.h>`, which
   `intel64_perf.c` already uses for the same arithmetic — using the canonical
   constant would keep the two copies identical.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to