public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH 0/2] Change behaviour of CONFIG_TIMER_EARLY
@ 2022-03-11 17:11 Johannes Krottmayer
  2022-03-11 17:11 ` [PATCH 1/2] lib: time: " Johannes Krottmayer
  2022-03-11 17:11 ` [PATCH 2/2] common: board_f: Fix CONFIG_TIMER_EARLY issue (EAGAIN) Johannes Krottmayer
  0 siblings, 2 replies; 4+ messages in thread
From: Johannes Krottmayer @ 2022-03-11 17:11 UTC (permalink / raw)
  To: u-boot
  Cc: Johannes Krottmayer, Wolfgang Denk, Thomas Chou, Rick Chen, Leo,
	Bin Meng, Sean Anderson, Anup Patel

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.

Signed-off-by: Johannes Krottmayer <krjdev@gmail.com>
Cc: Wolfgang Denk <wd@denx.de>
Cc: Thomas Chou <thomas@wytron.com.tw>
Cc: Rick Chen <rick@andestech.com>
Cc: Leo <ycliang@andestech.com>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Sean Anderson <seanga2@gmail.com>
Cc: Anup Patel <anup@brainfault.org>
Cc: Thomas Chou <thomas@wytron.com.tw>
---
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(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] lib: time: Change behaviour of CONFIG_TIMER_EARLY
  2022-03-11 17:11 [PATCH 0/2] Change behaviour of CONFIG_TIMER_EARLY Johannes Krottmayer
@ 2022-03-11 17:11 ` Johannes Krottmayer
  2022-03-20 17:56   ` Sean Anderson
  2022-03-11 17:11 ` [PATCH 2/2] common: board_f: Fix CONFIG_TIMER_EARLY issue (EAGAIN) Johannes Krottmayer
  1 sibling, 1 reply; 4+ messages in thread
From: Johannes Krottmayer @ 2022-03-11 17:11 UTC (permalink / raw)
  To: u-boot
  Cc: Johannes Krottmayer, Wolfgang Denk, Thomas Chou, Rick Chen, Leo,
	Bin Meng, Sean Anderson, Anup Patel

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().

Signed-off-by: Johannes Krottmayer <krjdev@gmail.com>
Cc: Wolfgang Denk <wd@denx.de>
Cc: Thomas Chou <thomas@wytron.com.tw>
Cc: Rick Chen <rick@andestech.com>
Cc: Leo <ycliang@andestech.com>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Sean Anderson <seanga2@gmail.com>
Cc: Anup Patel <anup@brainfault.org>
Cc: Thomas Chou <thomas@wytron.com.tw>
---
 lib/time.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/lib/time.c b/lib/time.c
index 96074b84af..b5c9760042 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -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",
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] common: board_f: Fix CONFIG_TIMER_EARLY issue (EAGAIN)
  2022-03-11 17:11 [PATCH 0/2] Change behaviour of CONFIG_TIMER_EARLY Johannes Krottmayer
  2022-03-11 17:11 ` [PATCH 1/2] lib: time: " Johannes Krottmayer
@ 2022-03-11 17:11 ` Johannes Krottmayer
  1 sibling, 0 replies; 4+ messages in thread
From: Johannes Krottmayer @ 2022-03-11 17:11 UTC (permalink / raw)
  To: u-boot
  Cc: Johannes Krottmayer, Wolfgang Denk, Thomas Chou, Rick Chen, Leo,
	Bin Meng, Sean Anderson, Anup Patel

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

Signed-off-by: Johannes Krottmayer <krjdev@gmail.com>
Cc: Wolfgang Denk <wd@denx.de>
Cc: Thomas Chou <thomas@wytron.com.tw>
Cc: Rick Chen <rick@andestech.com>
Cc: Leo <ycliang@andestech.com>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Sean Anderson <seanga2@gmail.com>
Cc: Anup Patel <anup@brainfault.org>
Cc: Thomas Chou <thomas@wytron.com.tw>
---
 common/board_f.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/common/board_f.c b/common/board_f.c
index a68760092a..fc883c1742 100644
--- a/common/board_f.c
+++ b/common/board_f.c
@@ -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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] lib: time: Change behaviour of CONFIG_TIMER_EARLY
  2022-03-11 17:11 ` [PATCH 1/2] lib: time: " Johannes Krottmayer
@ 2022-03-20 17:56   ` Sean Anderson
  0 siblings, 0 replies; 4+ messages in thread
From: Sean Anderson @ 2022-03-20 17:56 UTC (permalink / raw)
  To: Johannes Krottmayer, u-boot
  Cc: Wolfgang Denk, Thomas Chou, Rick Chen, Leo, Bin Meng, Anup Patel

On 3/11/22 12:11 PM, Johannes Krottmayer wrote:
> 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().
> 
> Signed-off-by: Johannes Krottmayer <krjdev@gmail.com>
> Cc: Wolfgang Denk <wd@denx.de>
> Cc: Thomas Chou <thomas@wytron.com.tw>
> Cc: Rick Chen <rick@andestech.com>
> Cc: Leo <ycliang@andestech.com>
> Cc: Bin Meng <bmeng.cn@gmail.com>
> Cc: Sean Anderson <seanga2@gmail.com>
> Cc: Anup Patel <anup@brainfault.org>
> Cc: Thomas Chou <thomas@wytron.com.tw>
> ---
>   lib/time.c | 42 +++++++++++++++++++++++++++++++++---------
>   1 file changed, 33 insertions(+), 9 deletions(-)
> 
> diff --git a/lib/time.c b/lib/time.c
> index 96074b84af..b5c9760042 100644
> --- a/lib/time.c
> +++ b/lib/time.c
> @@ -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)

if (!gd->dm_root)

>   		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.

>   		if (ret)
>   			panic("Could not initialize timer (err %d)\n", ret);
> -#endif
>   	}
>   
>   	ret = timer_get_count(gd->timer, &count);
> +

ditto

>   	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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-03-20 17:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-11 17:11 [PATCH 0/2] Change behaviour of CONFIG_TIMER_EARLY Johannes Krottmayer
2022-03-11 17:11 ` [PATCH 1/2] lib: time: " Johannes Krottmayer
2022-03-20 17:56   ` Sean Anderson
2022-03-11 17:11 ` [PATCH 2/2] common: board_f: Fix CONFIG_TIMER_EARLY issue (EAGAIN) Johannes Krottmayer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox