Skip to contentSkip to conversation

Change behaviour of CONFIG_TIMER_EARLY

PATCH In review Johannes Krottmayer 2 participants 4 messages Mar 11, 2022 – Mar 20, 2022 Library code

lore
Share & tools
lore
#0 lore raw

cover Change behaviour of CONFIG_TIMER_EARLY

When CONFIG_TIMER_EARLY is selected and the timer driver implements timer_early_get_count() and timer_early_get_rate() this leads to an EAGAIN error one some configurations in...

common/board_f.c:initf_dm()

initf_dm() invokes dm_timer_init(), but this function returns with an EAGAIN error in, because the DM virtual root driver isn't initialized yet in...

drivers/timer/timer-class.c:dm_timer_init()

Moved error handling to...

lib/time.c

For details you can read the description in the patches.

Affected drivers:
drivers/timer/andes_plmt_timer.c
drivers/timer/sifive_clint_timer.c
drivers/timer/riscv_timer.c

I don't know who maintains these base libraries and base functions, so I used the copyright holders. For the driver I added the required maintainers to CC.

Cc Wolfgang Denk <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Rick Chen <[email protected]>
Cc Bin Meng <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Anup Patel <[email protected]>
Cc Thomas Chou <[email protected]>

---
Johannes Krottmayer (2):
lib: time: Change behaviour of CONFIG_TIMER_EARLY
common: board_f: Fix CONFIG_TIMER_EARLY issue (EAGAIN)

 common/board_f.c |  6 ------
 lib/time.c       | 42 +++++++++++++++++++++++++++++++++---------
 2 files changed, 33 insertions(+), 15 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 lib: time: Change behaviour of CONFIG_TIMER_EARLY

If CONFIG_TIMER_EARLY is selected and the timer driver implements timer_early_get_count() and timer_early_get_rate(), check first if the virtual root driver is running, when it is initialized yet use the virtual timer driver instead. When the virtual root driver doesn't exists fallback to the timer_early_get_count() and timer_early_get_rate().

Cc Wolfgang Denk <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Rick Chen <[email protected]>
Cc Bin Meng <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Anup Patel <[email protected]>
Cc Thomas Chou <[email protected]>
---
 lib/time.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
lib/time.c +33 -9
index 96074b84af..b5c9760042 100644
@@ -8,6 +8,7 @@
 #include <clock_legacy.h>
 #include <bootstage.h>
 #include <dm.h>
+#include <dm/root.h>
 #include <errno.h>
 #include <init.h>
 #include <spl.h>
@@ -66,16 +67,28 @@ extern unsigned long __weak timer_read_counter(void);
 #if CONFIG_IS_ENABLED(TIMER)
 ulong notrace get_tbclk(void)
 {
-	if (!gd->timer) {
+	int ret;
+
 #ifdef CONFIG_TIMER_EARLY
+
+	/*
+	 * Check if the virtual root driver does not yet exist,
+	 * if not fallback to timer_early_get_rate().
+	 */
+	if (gd->dm_root == NULL)
 		return timer_early_get_rate();
-#else
-		int ret;
 
+#endif /* CONFIG_TIMER_EARLY */
+
+	/*
+	 * Check if the virtual timer driver does not yet exist,
+	 * if not initialize the driver.
+	 */
+	if (!gd->timer) {
 		ret = dm_timer_init();
+
 		if (ret)
-			return ret;
-#endif
+			panic("Could not initialize timer (err %d)\n", ret);
 	}
 
 	return timer_get_rate(gd->timer);
@@ -86,19 +99,30 @@ uint64_t notrace get_ticks(void)
 	u64 count;
 	int ret;
 
-	if (!gd->timer) {
 #ifdef CONFIG_TIMER_EARLY
+
+	/*
+	 * Check if the virtual root driver does not yet exist,
+	 * if not fallback to timer_early_get_rate().
+	 */
+	if (gd->dm_root == NULL)
 		return timer_early_get_count();
-#else
-		int ret;
 
+#endif /* CONFIG_TIMER_EARLY */
+
+	/*
+	 * Check if the virtual timer driver does not yet exist,
+	 * if not initialize the driver.
+	 */
+	if (!gd->timer) {
 		ret = dm_timer_init();
+
 		if (ret)
 			panic("Could not initialize timer (err %d)\n", ret);
-#endif
 	}
 
 	ret = timer_get_count(gd->timer, &count);
+
 	if (ret) {
 		if (spl_phase() > PHASE_TPL)
 			panic("Could not read count from timer (err %d)\n",
Signature · 1 line
2.34.1
#3 lore raw

PATCH 2/2 common: board_f: Fix CONFIG_TIMER_EARLY issue (EAGAIN)

In reply to Johannes Krottmayer

When CONFIG_TIMER_EARLY is selected and the timer driver implements timer_early_get_count() and timer_early_get_rate() this leads to an EAGAIN error one some configurations in...

common/board_f.c:initf_dm()

initf_dm() invokes dm_timer_init(), but this function returns with an EAGAIN error in, because the DM virtual root driver isn't initialized yet in...

drivers/timer/timer-class.c:dm_timer_init()

[WORKAROUND]

Move error handling to (next patch in this serie)...

lib/time.c

Cc Wolfgang Denk <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Rick Chen <[email protected]>
Cc Bin Meng <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Anup Patel <[email protected]>
Cc Thomas Chou <[email protected]>
---
 common/board_f.c | 6 ------
 1 file changed, 6 deletions(-)
common/board_f.c +0 -6
index a68760092a..fc883c1742 100644
@@ -785,12 +785,6 @@ static int initf_dm(void)
 	bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_F);
 	if (ret)
 		return ret;
-
-	if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
-		ret = dm_timer_init();
-		if (ret)
-			return ret;
-	}
 #endif
 
 	return 0;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] lib: time: Change behaviour of CONFIG_TIMER_EARLY

In reply to Johannes Krottmayer
Quoted 68 lines · Johannes Krottmayer

If CONFIG_TIMER_EARLY is selected and the timer driver implements timer_early_get_count() and timer_early_get_rate(), check first if the virtual root driver is running, when it is initialized yet use the virtual timer driver instead. When the virtual root driver doesn't exists fallback to the timer_early_get_count() and timer_early_get_rate().

Cc Wolfgang Denk <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Rick Chen <[email protected]>
Cc Bin Meng <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Anup Patel <[email protected]>
Cc Thomas Chou <[email protected]>
---
  lib/time.c | 42 +++++++++++++++++++++++++++++++++---------
  1 file changed, 33 insertions(+), 9 deletions(-)
lib/time.c +18 -5
index 96074b84af..b5c9760042 100644
@@ -8,6 +8,7 @@
  #include <clock_legacy.h>
  #include <bootstage.h>
  #include <dm.h>
+#include <dm/root.h>
  #include <errno.h>
  #include <init.h>
  #include <spl.h>
@@ -66,16 +67,28 @@ extern unsigned long __weak timer_read_counter(void);
  #if CONFIG_IS_ENABLED(TIMER)
  ulong notrace get_tbclk(void)
  {
-	if (!gd->timer) {
+	int ret;
+
  #ifdef CONFIG_TIMER_EARLY
+
+	/*
+	 * Check if the virtual root driver does not yet exist,
+	 * if not fallback to timer_early_get_rate().
+	 */
+	if (gd->dm_root == NULL)
  		return timer_early_get_rate();
-#else
-		int ret;
 
+#endif /* CONFIG_TIMER_EARLY */
+
+	/*
+	 * Check if the virtual timer driver does not yet exist,
+	 * if not initialize the driver.
+	 */
+	if (!gd->timer) {
  		ret = dm_timer_init();
+
  		if (ret)
-			return ret;
-#endif
+			panic("Could not initialize timer (err %d)\n", ret);
  	}
 
  	return timer_get_rate(gd->timer);
@@ -86,19 +99,30 @@ uint64_t notrace get_ticks(void)
  	u64 count;

lib/time.c uint64_t notrace get_ticks(void) · line 108

Quoted 10 lines
lib/time.c +6 -1
  	int ret;
 
-	if (!gd->timer) {
  #ifdef CONFIG_TIMER_EARLY
+
+	/*
+	 * Check if the virtual root driver does not yet exist,
+	 * if not fallback to timer_early_get_rate().
+	 */
+	if (gd->dm_root == NULL)

if (!gd->dm_root)

lib/time.c uint64_t notrace get_ticks(void) · line 119

Quoted 13 lines
lib/time.c +8 -2
  		return timer_early_get_count();
-#else
-		int ret;
 
+#endif /* CONFIG_TIMER_EARLY */
+
+	/*
+	 * Check if the virtual timer driver does not yet exist,
+	 * if not initialize the driver.
+	 */
+	if (!gd->timer) {
  		ret = dm_timer_init();
+

Please don't insert empty lines.

lib/time.c uint64_t notrace get_ticks(void) · line 125

Quoted 7 lines
lib/time.c +1 -1
  		if (ret)
  			panic("Could not initialize timer (err %d)\n", ret);
-#endif
  	}
 
  	ret = timer_get_count(gd->timer, &count);
+

ditto

lib/time.c uint64_t notrace get_ticks(void) · line 128

Quoted 4 lines
lib/time.c +0 -0
  	if (ret) {
  		if (spl_phase() > PHASE_TPL)
  			panic("Could not read count from timer (err %d)\n",

OK, so as I understand it, the current logic is something like

	if (!gd->timer) {
		if (CONFIG_TIMER_EARLY)
			return early_timer();
		else
			init_dm_timer();
	}
	return dm_timer();

and this patch would change it to be

	if (CONFIG_TIMER_EARLY && !gd->dm_root)
		return early_timer();
	if (!gd->timer)
		init_dm_timer();
	return dm_timer();

which has the consequence (with your next patch) that timer initialization is always done by the first timer API call, and not by initf_dm. However, I'm not sure why you want to change this.

--Sean