openrisc.lists.librecores.org archive mirror
 help / color / mirror / Atom feed
* [OpenRISC] [PATCH v8 06/19] openrisc: start CPU timer early in boot
       [not found] <20220430122355.2718797-1-Jason@zx2c4.com>
@ 2022-04-30 12:24 ` Jason A. Donenfeld
  2022-04-30 22:36   ` Stafford Horne
  0 siblings, 1 reply; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-04-30 12:24 UTC (permalink / raw)
  To: openrisc

In order to measure the boot process, the timer should be switched on as
early in boot as possible. This is necessary so that by the time the
setup code reaches random_init(), get_cycles() (by way of
random_get_entropy()) returns non-zero, indicating that it is actually
capable of counting. So this commit enables the timer immediately upon
booting up. As well, the commit define the get_cycles macro, like the
previous patches in this series, so that generic code is aware that it's
implemented by the platform, as is done on other archs.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Jonas Bonn <jonas@southpole.se>
Cc: Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
Cc: Stafford Horne <shorne@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
Changes v7->v8:
- Rather than doing get_cycles()+1 to handle the early boot case,
  actually start the timer early in boot. This has the huge advantage of
  properly measuring the boot sequence timing, which could be a valuable
  source of entropy.

 arch/openrisc/include/asm/timex.h | 1 +
 arch/openrisc/kernel/setup.c      | 7 +++++++
 2 files changed, 8 insertions(+)

diff --git a/arch/openrisc/include/asm/timex.h b/arch/openrisc/include/asm/timex.h
index d52b4e536e3f..5487fa93dd9b 100644
--- a/arch/openrisc/include/asm/timex.h
+++ b/arch/openrisc/include/asm/timex.h
@@ -23,6 +23,7 @@ static inline cycles_t get_cycles(void)
 {
 	return mfspr(SPR_TTCR);
 }
+#define get_cycles get_cycles
 
 /* This isn't really used any more */
 #define CLOCK_TICK_RATE 1000
diff --git a/arch/openrisc/kernel/setup.c b/arch/openrisc/kernel/setup.c
index 0cd04d936a7a..1cb7c1770a17 100644
--- a/arch/openrisc/kernel/setup.c
+++ b/arch/openrisc/kernel/setup.c
@@ -220,6 +220,13 @@ void __init setup_cpuinfo(void)
 
 void __init or1k_early_setup(void *fdt)
 {
+	/* Start the TTCR as early as possible, so that the RNG can make use of
+	 * measurements of boot time from the earliest opportunity. Especially
+	 * important is that the TTCR does not return zero by the time we reach
+	 * rand_initialize().
+	 */
+	mtspr(SPR_TTMR, SPR_TTMR_CR);
+
 	if (fdt)
 		pr_info("FDT at %p\n", fdt);
 	else {
-- 
2.35.1


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

* [OpenRISC] [PATCH v8 06/19] openrisc: start CPU timer early in boot
  2022-04-30 12:24 ` [OpenRISC] [PATCH v8 06/19] openrisc: start CPU timer early in boot Jason A. Donenfeld
@ 2022-04-30 22:36   ` Stafford Horne
  2022-04-30 22:37     ` Jason A. Donenfeld
  0 siblings, 1 reply; 4+ messages in thread
From: Stafford Horne @ 2022-04-30 22:36 UTC (permalink / raw)
  To: openrisc

On Sat, Apr 30, 2022 at 02:24:33PM +0200, Jason A. Donenfeld wrote:
> In order to measure the boot process, the timer should be switched on as
> early in boot as possible. This is necessary so that by the time the
> setup code reaches random_init(), get_cycles() (by way of
> random_get_entropy()) returns non-zero, indicating that it is actually
> capable of counting. So this commit enables the timer immediately upon
> booting up. As well, the commit define the get_cycles macro, like the
> previous patches in this series, so that generic code is aware that it's
> implemented by the platform, as is done on other archs.
> 
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Jonas Bonn <jonas@southpole.se>
> Cc: Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
> Cc: Stafford Horne <shorne@gmail.com>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> Changes v7->v8:
> - Rather than doing get_cycles()+1 to handle the early boot case,
>   actually start the timer early in boot. This has the huge advantage of
>   properly measuring the boot sequence timing, which could be a valuable
>   source of entropy.
> 
>  arch/openrisc/include/asm/timex.h | 1 +
>  arch/openrisc/kernel/setup.c      | 7 +++++++
>  2 files changed, 8 insertions(+)
> 
> diff --git a/arch/openrisc/include/asm/timex.h b/arch/openrisc/include/asm/timex.h
> index d52b4e536e3f..5487fa93dd9b 100644
> --- a/arch/openrisc/include/asm/timex.h
> +++ b/arch/openrisc/include/asm/timex.h
> @@ -23,6 +23,7 @@ static inline cycles_t get_cycles(void)
>  {
>  	return mfspr(SPR_TTCR);
>  }
> +#define get_cycles get_cycles
>  
>  /* This isn't really used any more */
>  #define CLOCK_TICK_RATE 1000
> diff --git a/arch/openrisc/kernel/setup.c b/arch/openrisc/kernel/setup.c
> index 0cd04d936a7a..1cb7c1770a17 100644
> --- a/arch/openrisc/kernel/setup.c
> +++ b/arch/openrisc/kernel/setup.c
> @@ -220,6 +220,13 @@ void __init setup_cpuinfo(void)
>  
>  void __init or1k_early_setup(void *fdt)
>  {
> +	/* Start the TTCR as early as possible, so that the RNG can make use of
> +	 * measurements of boot time from the earliest opportunity. Especially
> +	 * important is that the TTCR does not return zero by the time we reach
> +	 * rand_initialize().
> +	 */
> +	mtspr(SPR_TTMR, SPR_TTMR_CR);
> +
>  	if (fdt)
>  		pr_info("FDT at %p\n", fdt);
>  	else {

It looks like we cross paths on this one.

I can't think of anything better.  Also, I tested this on SMP systems and it
works fine.

Acked-by: Stafford Horne <shorne@gmail.com>

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

* [OpenRISC] [PATCH v8 06/19] openrisc: start CPU timer early in boot
  2022-04-30 22:36   ` Stafford Horne
@ 2022-04-30 22:37     ` Jason A. Donenfeld
  2022-04-30 22:42       ` [OpenRISC] [PATCH v9 06/21] " Jason A. Donenfeld
  0 siblings, 1 reply; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-04-30 22:37 UTC (permalink / raw)
  To: openrisc

On Sun, May 1, 2022 at 12:36 AM Stafford Horne <shorne@gmail.com> wrote:
>
> On Sat, Apr 30, 2022 at 02:24:33PM +0200, Jason A. Donenfeld wrote:
> > In order to measure the boot process, the timer should be switched on as
> > early in boot as possible. This is necessary so that by the time the
> > setup code reaches random_init(), get_cycles() (by way of
> > random_get_entropy()) returns non-zero, indicating that it is actually
> > capable of counting. So this commit enables the timer immediately upon
> > booting up. As well, the commit define the get_cycles macro, like the
> > previous patches in this series, so that generic code is aware that it's
> > implemented by the platform, as is done on other archs.
> >
> > Cc: Thomas Gleixner <tglx@linutronix.de>
> > Cc: Arnd Bergmann <arnd@arndb.de>
> > Cc: Jonas Bonn <jonas@southpole.se>
> > Cc: Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
> > Cc: Stafford Horne <shorne@gmail.com>
> > Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> > ---
> > Changes v7->v8:
> > - Rather than doing get_cycles()+1 to handle the early boot case,
> >   actually start the timer early in boot. This has the huge advantage of
> >   properly measuring the boot sequence timing, which could be a valuable
> >   source of entropy.
> >
> >  arch/openrisc/include/asm/timex.h | 1 +
> >  arch/openrisc/kernel/setup.c      | 7 +++++++
> >  2 files changed, 8 insertions(+)
> >
> > diff --git a/arch/openrisc/include/asm/timex.h b/arch/openrisc/include/asm/timex.h
> > index d52b4e536e3f..5487fa93dd9b 100644
> > --- a/arch/openrisc/include/asm/timex.h
> > +++ b/arch/openrisc/include/asm/timex.h
> > @@ -23,6 +23,7 @@ static inline cycles_t get_cycles(void)
> >  {
> >       return mfspr(SPR_TTCR);
> >  }
> > +#define get_cycles get_cycles
> >
> >  /* This isn't really used any more */
> >  #define CLOCK_TICK_RATE 1000
> > diff --git a/arch/openrisc/kernel/setup.c b/arch/openrisc/kernel/setup.c
> > index 0cd04d936a7a..1cb7c1770a17 100644
> > --- a/arch/openrisc/kernel/setup.c
> > +++ b/arch/openrisc/kernel/setup.c
> > @@ -220,6 +220,13 @@ void __init setup_cpuinfo(void)
> >
> >  void __init or1k_early_setup(void *fdt)
> >  {
> > +     /* Start the TTCR as early as possible, so that the RNG can make use of
> > +      * measurements of boot time from the earliest opportunity. Especially
> > +      * important is that the TTCR does not return zero by the time we reach
> > +      * rand_initialize().
> > +      */
> > +     mtspr(SPR_TTMR, SPR_TTMR_CR);
> > +
> >       if (fdt)
> >               pr_info("FDT at %p\n", fdt);
> >       else {
>
> It looks like we cross paths on this one.
>
> I can't think of anything better.  Also, I tested this on SMP systems and it
> works fine.
>
> Acked-by: Stafford Horne <shorne@gmail.com>

Oh okay. Do you want me to go with this or with the assembly one?

Jason

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

* [OpenRISC] [PATCH v9 06/21] openrisc: start CPU timer early in boot
  2022-04-30 22:37     ` Jason A. Donenfeld
@ 2022-04-30 22:42       ` Jason A. Donenfeld
  0 siblings, 0 replies; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-04-30 22:42 UTC (permalink / raw)
  To: openrisc

In order to measure the boot process, the timer should be switched on as
early in boot as possible. This is necessary so that by the time the
setup code reaches random_init(), get_cycles() (by way of
random_get_entropy()) returns non-zero, indicating that it is actually
capable of counting. So this commit enables the timer immediately upon
booting up. As well, the commit define the get_cycles macro, like the
previous patches in this series, so that generic code is aware that it's
implemented by the platform, as is done on other archs.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Jonas Bonn <jonas@southpole.se>
Cc: Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
Acked-by: Stafford Horne <shorne@gmail.com>
Reported-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
Changes v8->v9:
- Use Stafford's suggested assembly in head.S instead of doing this
  later on in C, so that the cycle counter starts very early and thus
  "measures" boot.

 arch/openrisc/include/asm/timex.h | 1 +
 arch/openrisc/kernel/head.S       | 9 +++++++++
 2 files changed, 10 insertions(+)

diff --git a/arch/openrisc/include/asm/timex.h b/arch/openrisc/include/asm/timex.h
index d52b4e536e3f..5487fa93dd9b 100644
--- a/arch/openrisc/include/asm/timex.h
+++ b/arch/openrisc/include/asm/timex.h
@@ -23,6 +23,7 @@ static inline cycles_t get_cycles(void)
 {
 	return mfspr(SPR_TTCR);
 }
+#define get_cycles get_cycles
 
 /* This isn't really used any more */
 #define CLOCK_TICK_RATE 1000
diff --git a/arch/openrisc/kernel/head.S b/arch/openrisc/kernel/head.S
index 15f1b38dfe03..871f4c858859 100644
--- a/arch/openrisc/kernel/head.S
+++ b/arch/openrisc/kernel/head.S
@@ -521,6 +521,15 @@ _start:
 	l.ori	r3,r0,0x1
 	l.mtspr	r0,r3,SPR_SR
 
+	/*
+	 * Start the TTCR as early as possible, so that the RNG can make use of
+	 * measurements of boot time from the earliest opportunity. Especially
+	 * important is that the TTCR does not return zero by the time we reach
+	 * rand_initialize().
+	 */
+	l.movhi r3,hi(SPR_TTMR_CR)
+	l.mtspr r0,r3,SPR_TTMR
+
 	CLEAR_GPR(r1)
 	CLEAR_GPR(r2)
 	CLEAR_GPR(r3)
-- 
2.35.1


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

end of thread, other threads:[~2022-04-30 22:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20220430122355.2718797-1-Jason@zx2c4.com>
2022-04-30 12:24 ` [OpenRISC] [PATCH v8 06/19] openrisc: start CPU timer early in boot Jason A. Donenfeld
2022-04-30 22:36   ` Stafford Horne
2022-04-30 22:37     ` Jason A. Donenfeld
2022-04-30 22:42       ` [OpenRISC] [PATCH v9 06/21] " Jason A. Donenfeld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).