Skip to contentSkip to conversation

timer: Add High Precision Event Timers (HPET) support

PATCH New Ivan Gorinov 4 participants 12 messages Apr 12, 2018 – Apr 30, 2018 x86

lore
Share & tools
lore
#0 lore raw

v6 cover timer: Add High Precision Event Timers (HPET) support

Add HPET driver as an alternative timer for x86 (default is TSC). HPET counter has constant frequency and does not need calibration. This change also makes TSC timer driver optional on x86. New HPET driver can also be selected as the early timer on x86.

v6:
Added TSC-specific timer functions to use instead of early timer in the code that specifically needs TSC.

v5:
Using readq() and writeq() for main counter access.

v3:
Added early timer choice in x86 Kconfig.

Ivan Gorinov (2):
x86: Add TSC-specific timer functions
timer: Add High Precision Event Timers (HPET) support

 arch/Kconfig                      |   1 +
 arch/x86/Kconfig                  |  21 ++++
 arch/x86/cpu/coreboot/timestamp.c |   2 +-
 arch/x86/cpu/quark/mrc_util.c     |  13 ++-
 arch/x86/dts/hpet.dtsi            |   7 ++
 arch/x86/include/asm/u-boot-x86.h |   2 +-
 common/Kconfig                    |   1 +
 drivers/timer/Kconfig             |   9 ++
 drivers/timer/Makefile            |   1 +
 drivers/timer/hpet_timer.c        | 209 ++++++++++++++++++++++++++++++++++++++
 drivers/timer/tsc_timer.c         |  39 +++++--
 11 files changed, 288 insertions(+), 17 deletions(-)
 create mode 100644 arch/x86/dts/hpet.dtsi
 create mode 100644 drivers/timer/hpet_timer.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Add tsc_rate_mhz() function and use it in the code that specifically needs to get TSC rate regardless of currently selected early timer.

---
 arch/x86/cpu/coreboot/timestamp.c |  2 +-
 arch/x86/cpu/quark/mrc_util.c     | 13 ++++++-------
 arch/x86/include/asm/u-boot-x86.h |  2 +-
 drivers/timer/tsc_timer.c         | 33 ++++++++++++++++++++++++---------
 4 files changed, 32 insertions(+), 18 deletions(-)
arch/x86/cpu/coreboot/timestamp.c +1 -1
index b382795..05bb214 100644
@@ -78,7 +78,7 @@ int timestamp_add_to_bootstage(void)
 		if (name) {
 			bootstage_add_record(0, name, BOOTSTAGEF_ALLOC,
 					     tse->entry_stamp /
-							get_tbclk_mhz());
+							tsc_rate_mhz());
 		}
 	}
 
arch/x86/cpu/quark/mrc_util.c +6 -7
index fac2d72..4794395 100644
@@ -57,19 +57,18 @@ void mrc_post_code(uint8_t major, uint8_t minor)
 void delay_n(uint32_t ns)
 {
 	/* 1000 MHz clock has 1ns period --> no conversion required */
-	uint64_t final_tsc = rdtsc();
+	uint64_t start_tsc = rdtsc();
+	uint64_t ticks;
 
-	final_tsc += ((get_tbclk_mhz() * ns) / 1000);
-
-	while (rdtsc() < final_tsc)
-		;
+	ticks = (tsc_rate_mhz() * ns) / 1000;
+	while (rdtsc() - start_tsc < ticks);
 }
 
 /* Delay number of microseconds */
-void delay_u(uint32_t ms)
+void delay_u(uint32_t us)
 {
 	/* 64-bit math is not an option, just use loops */
-	while (ms--)
+	while (us--)
 		delay_n(1000);
 }
 
arch/x86/include/asm/u-boot-x86.h +1 -1
index 187fe5f..de68120 100644
@@ -29,7 +29,7 @@ int cleanup_before_linux(void);
 void timer_isr(void *);
 typedef void (timer_fnc_t) (void);
 int register_timer_isr (timer_fnc_t *isr_func);
-unsigned long get_tbclk_mhz(void);
+unsigned long tsc_rate_mhz(void);
 void timer_set_base(uint64_t base);
 int i8254_init(void);
 
drivers/timer/tsc_timer.c +24 -9
index 9296de6..98cbf12 100644
@@ -277,15 +277,12 @@ success:
 	return delta / 1000;
 }
 
-/* Get the speed of the TSC timer in MHz */
-unsigned notrace long get_tbclk_mhz(void)
-{
-	return get_tbclk() / 1000000;
-}
-
 static ulong get_ms_timer(void)
 {
-	return (get_ticks() * 1000) / get_tbclk();
+	if (gd->arch.clock_rate == 0)
+		return 0;
+
+	return (rdtsc() * 1000) / gd->arch.clock_rate;
 }
 
 ulong get_timer(ulong base)
@@ -295,7 +292,10 @@ ulong get_timer(ulong base)
 
 ulong notrace timer_get_us(void)
 {
-	return get_ticks() / get_tbclk_mhz();
+	if (gd->arch.clock_rate == 0)
+		return 0;
+
+	return (rdtsc() * 1000000) / gd->arch.clock_rate;
 }
 
 ulong timer_get_boot_us(void)
@@ -308,7 +308,7 @@ void __udelay(unsigned long usec)
 	u64 now = get_ticks();
 	u64 stop;
 
-	stop = now + usec * get_tbclk_mhz();
+	stop = now + ((u64)usec * gd->arch.clock_rate) / 1000000;
 
 	while ((int64_t)(stop - get_ticks()) > 0)
 #if defined(CONFIG_QEMU) && defined(CONFIG_SMP)
@@ -355,6 +355,14 @@ static void tsc_timer_ensure_setup(void)
 	}
 }
 
+/* Get the speed of the TSC timer in MHz */
+unsigned notrace long tsc_rate_mhz(void)
+{
+	tsc_timer_ensure_setup();
+
+	return gd->arch.clock_rate / 1000000;
+}
+
 static int tsc_timer_probe(struct udevice *dev)
 {
 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
@@ -365,6 +373,13 @@ static int tsc_timer_probe(struct udevice *dev)
 	return 0;
 }
 
+int timer_init(void)
+{
+       tsc_timer_ensure_setup();
+
+       return 0;
+}
+
 unsigned long notrace timer_early_get_rate(void)
 {
 	tsc_timer_ensure_setup();
Signature · 1 line
2.7.4
#11 lore raw

PATCH v6 2/2 timer: Add High Precision Event Timers (HPET) support

In reply to Ivan Gorinov

Add HPET driver as an alternative timer for x86 (default is TSC). HPET counter has constant frequency and does not need calibration. New HPET driver can also be selected as the early timer on x86.

HPET can be selected as the tick timer in the Device Tree "chosen" node:

/include/ "hpet.dtsi"

...

    chosen {
        tick-timer = "/hpet";
    };
---
 arch/Kconfig               |   1 +
 arch/x86/Kconfig           |  21 +++++
 arch/x86/dts/hpet.dtsi     |   7 ++
 common/Kconfig             |   1 +
 drivers/timer/Kconfig      |   9 ++
 drivers/timer/Makefile     |   1 +
 drivers/timer/hpet_timer.c | 209 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/timer/tsc_timer.c  |   8 ++
 8 files changed, 257 insertions(+)
 create mode 100644 arch/x86/dts/hpet.dtsi
 create mode 100644 drivers/timer/hpet_timer.c
arch/Kconfig +1 -0
index e599e7a..f904fab 100644
@@ -105,6 +105,7 @@ config X86
 	select PCI
 	select TIMER
 	select X86_TSC_TIMER
+	imply HPET_TIMER
 	imply BLK
 	imply DM_ETH
 	imply DM_GPIO
arch/x86/Kconfig +21 -0
index 5c23b2c..8f64b95 100644
@@ -119,6 +119,27 @@ source "arch/x86/cpu/tangier/Kconfig"
 
 # architecture-specific options below
 
+choice
+	prompt "Select which timer to use early"
+	depends on TIMER_EARLY
+	default X86_EARLY_TIMER_TSC
+
+config X86_EARLY_TIMER_TSC
+	bool "TSC"
+	depends on X86_TSC_TIMER
+	help
+	  This selects x86 Time Stamp Counter (TSC) as the early timer.
+	  See CONFIG_TIMER_EARLY for the early timer description.
+
+config X86_EARLY_TIMER_HPET
+	bool "HPET"
+	depends on HPET_TIMER
+	help
+	  This selects High Precision Event Timers as the early timer.
+	  Early HPET base address is specified by CONFIG_HPET_ADDRESS.
+
+endchoice
+
 config AHCI
 	default y
 
arch/x86/dts/hpet.dtsi new file +7 -0
new file mode 100644
index 0000000..a74f739
@@ -0,0 +1,7 @@
+/ {
+	hpet: hpet at fed00000 {
+		compatible = "hpet-x86";
+		u-boot,dm-pre-reloc;
+		reg = <0xfed00000 0x1000>;
+	};
+};
common/Kconfig +1 -0
index 03eeeb2..b02384c 100644
@@ -2,6 +2,7 @@ menu "Boot timing"
 
 config BOOTSTAGE
 	bool "Boot timing and reporting"
+	select TIMER_EARLY
 	help
 	  Enable recording of boot time while booting. To use it, insert
 	  calls to bootstage_mark() with a suitable BOOTSTAGE_ID from
drivers/timer/Kconfig +9 -0
index 2c96896..26743b7 100644
@@ -65,6 +65,15 @@ config X86_TSC_TIMER
 	help
 	  Select this to enable Time-Stamp Counter (TSC) timer for x86.
 
+config HPET_TIMER
+	bool "High Precision Event Timers (HPET) support"
+	depends on TIMER
+	default y if X86
+	help
+	  Select this to enable High Precision Event Timers (HPET) on x86.
+	  HPET main counter increments at constant rate and does not need
+	  calibration.
+
 config OMAP_TIMER
 	bool "Omap timer support"
 	depends on TIMER
drivers/timer/Makefile +1 -0
index a6e7832..557fecc 100644
@@ -8,6 +8,7 @@ obj-y += timer-uclass.o
 obj-$(CONFIG_ALTERA_TIMER)	+= altera_timer.o
 obj-$(CONFIG_SANDBOX_TIMER)	+= sandbox_timer.o
 obj-$(CONFIG_X86_TSC_TIMER)	+= tsc_timer.o
+obj-$(CONFIG_HPET_TIMER)	+= hpet_timer.o
 obj-$(CONFIG_OMAP_TIMER)	+= omap-timer.o
 obj-$(CONFIG_AST_TIMER)	+= ast_timer.o
 obj-$(CONFIG_STI_TIMER)		+= sti-timer.o
drivers/timer/hpet_timer.c new file +209 -0

209 lines not on this page · raw message

drivers/timer/tsc_timer.c +8 -0
index 98cbf12..8fc7130 100644
@@ -277,6 +277,8 @@ success:
 	return delta / 1000;
 }
 
+#if defined(CONFIG_X86_EARLY_TIMER_TSC)
+
 static ulong get_ms_timer(void)
 {
 	if (gd->arch.clock_rate == 0)
@@ -322,6 +324,8 @@ void __udelay(unsigned long usec)
 #endif
 }
 
+#endif /* CONFIG_X86_EARLY_TIMER_TSC */
+
 static int tsc_timer_get_count(struct udevice *dev, u64 *count)
 {
 	u64 now_tick = rdtsc();
@@ -373,6 +377,8 @@ static int tsc_timer_probe(struct udevice *dev)
 	return 0;
 }
 
+#if defined(CONFIG_X86_EARLY_TIMER_TSC)
+
 int timer_init(void)
 {
        tsc_timer_ensure_setup();
@@ -392,6 +398,8 @@ u64 notrace timer_early_get_count(void)
 	return rdtsc() - gd->arch.tsc_base;
 }
 
+#endif
+
 static const struct timer_ops tsc_timer_ops = {
 	.get_count = tsc_timer_get_count,
 };
Signature · 1 line
2.7.4
#2 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

In reply to Ivan Gorinov
Quoted 13 lines · Ivan Gorinov

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Add tsc_rate_mhz() function and use it in the code that specifically needs to get TSC rate regardless of currently selected early timer.

void delay_n(uint32_t ns)
{
/* 1000 MHz clock has 1ns period --> no conversion required
*/

arch/x86/cpu/quark/mrc_util.c +0 -1
-	uint64_t final_tsc = rdtsc();

arch/x86/cpu/quark/mrc_util.c void mrc_post_code(uint8_t major, uint8_t minor)

Quoted 10 lines
arch/x86/cpu/quark/mrc_util.c +4 -4
+	uint64_t start_tsc = rdtsc();
+	uint64_t ticks;
 
-	final_tsc += ((get_tbclk_mhz() * ns) / 1000);
-
-	while (rdtsc() < final_tsc)
-		;
+	ticks = (tsc_rate_mhz() * ns) / 1000;
 
+	while (rdtsc() - start_tsc < ticks);

I would rather preserve existing style.

arch/x86/cpu/quark/mrc_util.c void mrc_post_code(uint8_t major, uint8_t minor) · line 73

Quoted 11 lines

}

/* Delay number of microseconds */

arch/x86/cpu/quark/mrc_util.c +2 -2
-void delay_u(uint32_t ms)
+void delay_u(uint32_t us)
 {
 	/* 64-bit math is not an option, just use loops */
-	while (ms--)
+	while (us--)
 		delay_n(1000);
 }

This is a separate change.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#3 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

Quoted 22 lines · Andy Shevchenko
Quoted 21 lines

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Add tsc_rate_mhz() function and use it in the code that specifically needs to get TSC rate regardless of currently selected early timer.

void delay_n(uint32_t ns)
{
/* 1000 MHz clock has 1ns period --> no conversion required
*/

arch/x86/cpu/quark/mrc_util.c +3 -5
-	uint64_t final_tsc = rdtsc();
+	uint64_t start_tsc = rdtsc();
+	uint64_t ticks;
 
-	final_tsc += ((get_tbclk_mhz() * ns) / 1000);
-
-	while (rdtsc() < final_tsc)
-		;
+	ticks = (tsc_rate_mhz() * ns) / 1000;
Quoted 3 lines

arch/x86/cpu/quark/mrc_util.c void mrc_post_code(uint8_t major, uint8_t minor) · line 64

Quoted 1 line
arch/x86/cpu/quark/mrc_util.c +1 -0
+	while (rdtsc() - start_tsc < ticks);

I would rather preserve existing style.

OK. Existing style does not correctly handle overflow, but for a 64-bit counter it's a very unlikely event.

#5 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

In reply to Ivan Gorinov

Hi Ivan,

Quoted 4 lines · Ivan Gorinov

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Thanks for working on this. But get_tbclk() is one API provided by the timer library. The get_tbclk_mhz() is something that is implemented by the TSC timer driver, so can we get rid of the get_tbclk_mhz() and use the get_tbclk() instead in coreboot/timestamp.c and quark/mrc_util.c?

Quoted 132 lines

Add tsc_rate_mhz() function and use it in the code that specifically needs to get TSC rate regardless of currently selected early timer.

---
 arch/x86/cpu/coreboot/timestamp.c |  2 +-
 arch/x86/cpu/quark/mrc_util.c     | 13 ++++++-------
 arch/x86/include/asm/u-boot-x86.h |  2 +-
 drivers/timer/tsc_timer.c         | 33 ++++++++++++++++++++++++---------
 4 files changed, 32 insertions(+), 18 deletions(-)
arch/x86/cpu/coreboot/timestamp.c +1 -1
index b382795..05bb214 100644
@@ -78,7 +78,7 @@ int timestamp_add_to_bootstage(void)
                if (name) {
                        bootstage_add_record(0, name, BOOTSTAGEF_ALLOC,
                                             tse->entry_stamp /
-                                                       get_tbclk_mhz());
+                                                       tsc_rate_mhz());
                }
        }
 
arch/x86/cpu/quark/mrc_util.c +6 -7
index fac2d72..4794395 100644
@@ -57,19 +57,18 @@ void mrc_post_code(uint8_t major, uint8_t minor)
 void delay_n(uint32_t ns)
 {
        /* 1000 MHz clock has 1ns period --> no conversion required */
-       uint64_t final_tsc = rdtsc();
+       uint64_t start_tsc = rdtsc();
+       uint64_t ticks;
 
-       final_tsc += ((get_tbclk_mhz() * ns) / 1000);
-
-       while (rdtsc() < final_tsc)
-               ;
+       ticks = (tsc_rate_mhz() * ns) / 1000;
+       while (rdtsc() - start_tsc < ticks);
 }
 
 /* Delay number of microseconds */
-void delay_u(uint32_t ms)
+void delay_u(uint32_t us)
 {
        /* 64-bit math is not an option, just use loops */
-       while (ms--)
+       while (us--)
                delay_n(1000);
 }
 
arch/x86/include/asm/u-boot-x86.h +1 -1
index 187fe5f..de68120 100644
@@ -29,7 +29,7 @@ int cleanup_before_linux(void);
 void timer_isr(void *);
 typedef void (timer_fnc_t) (void);
 int register_timer_isr (timer_fnc_t *isr_func);
-unsigned long get_tbclk_mhz(void);
+unsigned long tsc_rate_mhz(void);
 void timer_set_base(uint64_t base);
 int i8254_init(void);
 
drivers/timer/tsc_timer.c +18 -9
index 9296de6..98cbf12 100644
@@ -277,15 +277,12 @@ success:
        return delta / 1000;
 }
 
-/* Get the speed of the TSC timer in MHz */
-unsigned notrace long get_tbclk_mhz(void)
-{
-       return get_tbclk() / 1000000;
-}
-
 static ulong get_ms_timer(void)
 {
-       return (get_ticks() * 1000) / get_tbclk();
+       if (gd->arch.clock_rate == 0)
+               return 0;
+
+       return (rdtsc() * 1000) / gd->arch.clock_rate;
 }
 
 ulong get_timer(ulong base)
@@ -295,7 +292,10 @@ ulong get_timer(ulong base)
 
 ulong notrace timer_get_us(void)
 {
-       return get_ticks() / get_tbclk_mhz();
+       if (gd->arch.clock_rate == 0)
+               return 0;
+
+       return (rdtsc() * 1000000) / gd->arch.clock_rate;
 }
 
 ulong timer_get_boot_us(void)
@@ -308,7 +308,7 @@ void __udelay(unsigned long usec)
        u64 now = get_ticks();
        u64 stop;
 
-       stop = now + usec * get_tbclk_mhz();
+       stop = now + ((u64)usec * gd->arch.clock_rate) / 1000000;
 
        while ((int64_t)(stop - get_ticks()) > 0)
 #if defined(CONFIG_QEMU) && defined(CONFIG_SMP)
@@ -355,6 +355,14 @@ static void tsc_timer_ensure_setup(void)
        }
 }
 
+/* Get the speed of the TSC timer in MHz */
+unsigned notrace long tsc_rate_mhz(void)
+{
+       tsc_timer_ensure_setup();
+
+       return gd->arch.clock_rate / 1000000;
+}
+
 static int tsc_timer_probe(struct udevice *dev)
 {
        struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
@@ -365,6 +373,13 @@ static int tsc_timer_probe(struct udevice *dev)
        return 0;
 }
 
+int timer_init(void)

drivers/timer/tsc_timer.c static int tsc_timer_probe(struct udevice *dev)

Quoted 10 lines
drivers/timer/tsc_timer.c +6 -1
+{
+       tsc_timer_ensure_setup();
+
+       return 0;
+}
+
 unsigned long notrace timer_early_get_rate(void)
 {
        tsc_timer_ensure_setup();
--

Regards,
Bin

#6 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

Hi Ivan,

Quoted 8 lines · Bin Meng

Hi Ivan,

Quoted 4 lines · Ivan Gorinov

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Quoted 5 lines

Thanks for working on this. But get_tbclk() is one API provided by the timer library. The get_tbclk_mhz() is something that is implemented by the TSC timer driver, so can we get rid of the get_tbclk_mhz() and use the get_tbclk() instead in coreboot/timestamp.c and quark/mrc_util.c?

Further request on TSC timer driver clean up, in order to make HPET work with either TSC or HPET being the U-Boot (early) timer, we should:

- Remove get_tbclk_mhz() in tsc_timer.c
- Remove get_timer() in tsc_timer.c
- Remove timer_get_us() in tsc_timer.c
- Remove timer_get_boot_us() in tsc_timer.c
- Move __udelay() implementation in tsc_timer.c to arch/x86/cpu/qemu/qemu.c, as this is the QEMU specific support. For other platforms, use the default one provided in lib/time.c

Regards,
Bin

#4 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

In reply to Ivan Gorinov
Quoted 11 lines · Ivan Gorinov
Quoted 6 lines · Andy Shevchenko

arch/x86/cpu/quark/mrc_util.c void mrc_post_code(uint8_t major, uint8_t minor)

Quoted 4 lines
arch/x86/cpu/quark/mrc_util.c +1 -2
-	while (rdtsc() < final_tsc)
-		;
 
+	while (rdtsc() - start_tsc < ticks);

I would rather preserve existing style.

OK. Existing style does not correctly handle overflow, but for a 64-bit counter it's a very unlikely event.

You didn't get me. I'm just talking about style, not about functionality.

See above for lines I left in this reply.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#7 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

In reply to Bin Meng

Hi Bin,

Quoted 8 lines · Bin Meng
Quoted 3 lines

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Thanks for working on this. But get_tbclk() is one API provided by the timer library. The get_tbclk_mhz() is something that is implemented by the TSC timer driver, so can we get rid of the get_tbclk_mhz() and use the get_tbclk() instead in coreboot/timestamp.c and quark/mrc_util.c?

The Coreboot timestamp code and Quark MRC specifically use rdtsc(). We can replace it with timer_early_get_count() or provide a function to get the TSC frequency even when another early timer is selected.

Quoted 142 lines
Quoted 142 lines

Add tsc_rate_mhz() function and use it in the code that specifically needs to get TSC rate regardless of currently selected early timer.

---
 arch/x86/cpu/coreboot/timestamp.c |  2 +-
 arch/x86/cpu/quark/mrc_util.c     | 13 ++++++-------
 arch/x86/include/asm/u-boot-x86.h |  2 +-
 drivers/timer/tsc_timer.c         | 33 ++++++++++++++++++++++++---------
 4 files changed, 32 insertions(+), 18 deletions(-)
arch/x86/cpu/coreboot/timestamp.c +1 -1
index b382795..05bb214 100644
@@ -78,7 +78,7 @@ int timestamp_add_to_bootstage(void)
                if (name) {
                        bootstage_add_record(0, name, BOOTSTAGEF_ALLOC,
                                             tse->entry_stamp /
-                                                       get_tbclk_mhz());
+                                                       tsc_rate_mhz());
                }
        }
 
arch/x86/cpu/quark/mrc_util.c +6 -7
index fac2d72..4794395 100644
@@ -57,19 +57,18 @@ void mrc_post_code(uint8_t major, uint8_t minor)
 void delay_n(uint32_t ns)
 {
        /* 1000 MHz clock has 1ns period --> no conversion required */
-       uint64_t final_tsc = rdtsc();
+       uint64_t start_tsc = rdtsc();
+       uint64_t ticks;
 
-       final_tsc += ((get_tbclk_mhz() * ns) / 1000);
-
-       while (rdtsc() < final_tsc)
-               ;
+       ticks = (tsc_rate_mhz() * ns) / 1000;
+       while (rdtsc() - start_tsc < ticks);
 }
 
 /* Delay number of microseconds */
-void delay_u(uint32_t ms)
+void delay_u(uint32_t us)
 {
        /* 64-bit math is not an option, just use loops */
-       while (ms--)
+       while (us--)
                delay_n(1000);
 }
 
arch/x86/include/asm/u-boot-x86.h +1 -1
index 187fe5f..de68120 100644
@@ -29,7 +29,7 @@ int cleanup_before_linux(void);
 void timer_isr(void *);
 typedef void (timer_fnc_t) (void);
 int register_timer_isr (timer_fnc_t *isr_func);
-unsigned long get_tbclk_mhz(void);
+unsigned long tsc_rate_mhz(void);
 void timer_set_base(uint64_t base);
 int i8254_init(void);
 
drivers/timer/tsc_timer.c +24 -10
index 9296de6..98cbf12 100644
@@ -277,15 +277,12 @@ success:
        return delta / 1000;
 }
 
-/* Get the speed of the TSC timer in MHz */
-unsigned notrace long get_tbclk_mhz(void)
-{
-       return get_tbclk() / 1000000;
-}
-
 static ulong get_ms_timer(void)
 {
-       return (get_ticks() * 1000) / get_tbclk();
+       if (gd->arch.clock_rate == 0)
+               return 0;
+
+       return (rdtsc() * 1000) / gd->arch.clock_rate;
 }
 
 ulong get_timer(ulong base)
@@ -295,7 +292,10 @@ ulong get_timer(ulong base)
 
 ulong notrace timer_get_us(void)
 {
-       return get_ticks() / get_tbclk_mhz();
+       if (gd->arch.clock_rate == 0)
+               return 0;
+
+       return (rdtsc() * 1000000) / gd->arch.clock_rate;
 }
 
 ulong timer_get_boot_us(void)
@@ -308,7 +308,7 @@ void __udelay(unsigned long usec)
        u64 now = get_ticks();
        u64 stop;
 
-       stop = now + usec * get_tbclk_mhz();
+       stop = now + ((u64)usec * gd->arch.clock_rate) / 1000000;
 
        while ((int64_t)(stop - get_ticks()) > 0)
 #if defined(CONFIG_QEMU) && defined(CONFIG_SMP)
@@ -355,6 +355,14 @@ static void tsc_timer_ensure_setup(void)
        }
 }
 
+/* Get the speed of the TSC timer in MHz */
+unsigned notrace long tsc_rate_mhz(void)
+{
+       tsc_timer_ensure_setup();
+
+       return gd->arch.clock_rate / 1000000;
+}
+
 static int tsc_timer_probe(struct udevice *dev)
 {
        struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
@@ -365,6 +373,13 @@ static int tsc_timer_probe(struct udevice *dev)
        return 0;
 }
 
+int timer_init(void)
+{
+       tsc_timer_ensure_setup();
+
+       return 0;
+}
+
 unsigned long notrace timer_early_get_rate(void)
 {
        tsc_timer_ensure_setup();
--
#8 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

Hi Ivan,

Quoted 7 lines · Ivan Gorinov

Hi Bin,

Quoted 4 lines · Bin Meng
Quoted 3 lines

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Quoted 9 lines
Quoted 4 lines

Thanks for working on this. But get_tbclk() is one API provided by the timer library. The get_tbclk_mhz() is something that is implemented by the TSC timer driver, so can we get rid of the get_tbclk_mhz() and use the get_tbclk() instead in coreboot/timestamp.c and quark/mrc_util.c?

The Coreboot timestamp code and Quark MRC specifically use rdtsc(). We can replace it with timer_early_get_count() or provide a function to get the TSC frequency even when another early timer is selected.

Good catch. Yes, we should fix coreboot timestamp code and Quark MRC codes to not explicitly call rdtsc.

Another driver that explicitly calls rdtsc() is hw_watchdog_reset() in watchdog/tangier_wdt.c driver. We need fix that too.

Regards,
Bin

#9 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

Hi Ivan,

Quoted 15 lines · Bin Meng

Hi Ivan,

Quoted 12 lines · Ivan Gorinov

Hi Bin,

Quoted 8 lines · Bin Meng
Quoted 3 lines

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Thanks for working on this. But get_tbclk() is one API provided by the timer library. The get_tbclk_mhz() is something that is implemented by the TSC timer driver, so can we get rid of the get_tbclk_mhz() and use the get_tbclk() instead in coreboot/timestamp.c and quark/mrc_util.c?

Quoted 8 lines
Quoted 4 lines

The Coreboot timestamp code and Quark MRC specifically use rdtsc(). We can replace it with timer_early_get_count() or provide a function to get the TSC frequency even when another early timer is selected.

Good catch. Yes, we should fix coreboot timestamp code and Quark MRC codes to not explicitly call rdtsc.

Further checking the coreboot timestamp codes, I think we may have to leave the coreboot timestamp codes as it is now.

We have the codes blow:
void timestamp_add_now(enum timestamp_id id)

{
    timestamp_add(id, rdtsc());
}

We cannot replace rdtsc() with timer_early_get_count(), because this timestamp_add_now() is called both before and after DM initialization. If the HPET is selected as the early timer and TSC is selected as the normal timer, the timestamp numbers are meaningless to compare against each other.

Quoted 3 lines

Another driver that explicitly calls rdtsc() is hw_watchdog_reset() in watchdog/tangier_wdt.c driver. We need fix that too.

Simon, another issue is the bootstage support. So far the timer_get_boot_us() is not implemented by DM timer APIs. timer_get_boot_us() is implemented per timer driver if CONFIG_SYS_TIMER_COUNTER is not defined. Note CONFIG_SYS_TIMER_COUNTER is non-DM stuff. That means the bootstage support is bounded by a specific timer driver, instead of a generic library. To me this overall timer support is somehow fragmentary.

Regards,
Bin

#10 lore raw

PATCH v6 1/2 x86: Add TSC-specific timer functions

Hi Bin,

Quoted 46 lines · Bin Meng

Hi Ivan,

Quoted 15 lines · Bin Meng

Hi Ivan,

Quoted 12 lines · Ivan Gorinov

Hi Bin,

Quoted 8 lines · Bin Meng
Quoted 3 lines

Coreboot timestamp functions and Quark memory reference code use get_tbclk() to get TSC frequency. This will not work if another early timer is selected.

Thanks for working on this. But get_tbclk() is one API provided by the timer library. The get_tbclk_mhz() is something that is implemented by the TSC timer driver, so can we get rid of the get_tbclk_mhz() and use the get_tbclk() instead in coreboot/timestamp.c and quark/mrc_util.c?

Quoted 8 lines
Quoted 4 lines

The Coreboot timestamp code and Quark MRC specifically use rdtsc(). We can replace it with timer_early_get_count() or provide a function to get the TSC frequency even when another early timer is selected.

Good catch. Yes, we should fix coreboot timestamp code and Quark MRC codes to not explicitly call rdtsc.

Further checking the coreboot timestamp codes, I think we may have to leave the coreboot timestamp codes as it is now.

We have the codes blow:
void timestamp_add_now(enum timestamp_id id)

{
    timestamp_add(id, rdtsc());
}

We cannot replace rdtsc() with timer_early_get_count(), because this timestamp_add_now() is called both before and after DM initialization. If the HPET is selected as the early timer and TSC is selected as the normal timer, the timestamp numbers are meaningless to compare against each other.

Quoted 3 lines

Another driver that explicitly calls rdtsc() is hw_watchdog_reset() in watchdog/tangier_wdt.c driver. We need fix that too.

Quoted 7 lines

Simon, another issue is the bootstage support. So far the timer_get_boot_us() is not implemented by DM timer APIs. timer_get_boot_us() is implemented per timer driver if CONFIG_SYS_TIMER_COUNTER is not defined. Note CONFIG_SYS_TIMER_COUNTER is non-DM stuff. That means the bootstage support is bounded by a specific timer driver, instead of a generic library. To me this overall timer support is somehow fragmentary.

Yes I agree. I'm open to ideas and patches :-)

Regards,
Simon