linux-mips.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] MIPS: Octeon: Use non-overflowing arithmetic in sched_clock
@ 2010-01-08 22:47 David Daney
  2010-01-11 10:20 ` Ralf Baechle
  0 siblings, 1 reply; 3+ messages in thread
From: David Daney @ 2010-01-08 22:47 UTC (permalink / raw)
  To: linux-mips, ralf; +Cc: David Daney

With typical mult and shift values, the calculation for Octeon's
sched_clock overflows when using 64-bit arithmetic.  Use 128-bit
calculations instead.

Signed-off-by: David Daney <ddaney@caviumnetworks.com>
---
 arch/mips/cavium-octeon/csrc-octeon.c |   31 ++++++++++++++++++++++++++++---
 1 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/arch/mips/cavium-octeon/csrc-octeon.c b/arch/mips/cavium-octeon/csrc-octeon.c
index 96df821..0bf4bbe 100644
--- a/arch/mips/cavium-octeon/csrc-octeon.c
+++ b/arch/mips/cavium-octeon/csrc-octeon.c
@@ -52,9 +52,34 @@ static struct clocksource clocksource_mips = {
 
 unsigned long long notrace sched_clock(void)
 {
-	return clocksource_cyc2ns(read_c0_cvmcount(),
-				  clocksource_mips.mult,
-				  clocksource_mips.shift);
+	/* 64-bit arithmatic can overflow, so use 128-bit.  */
+#if (__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 3))
+	u64 t1, t2, t3;
+	unsigned long long rv;
+	u64 mult = clocksource_mips.mult;
+	u64 shift = clocksource_mips.shift;
+	u64 cnt = read_c0_cvmcount();
+
+	asm (
+		"dmultu\t%[cnt],%[mult]\n\t"
+		"nor\t%[t1],$0,%[shift]\n\t"
+		"mfhi\t%[t2]\n\t"
+		"mflo\t%[t3]\n\t"
+		"dsll\t%[t2],%[t2],1\n\t"
+		"dsrlv\t%[rv],%[t3],%[shift]\n\t"
+		"dsllv\t%[t1],%[t2],%[t1]\n\t"
+		"or\t%[rv],%[t1],%[rv]\n\t"
+		: [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
+		: [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
+		: "hi", "lo");
+	return rv;
+#else
+	/* GCC > 4.3 do it the easy way.  */
+	unsigned int __attribute__((mode(TI))) t;
+	t = read_c0_cvmcount();
+	t = t * clocksource_mips.mult;
+	return (unsigned long long)(t >> clocksource_mips.shift);
+#endif
 }
 
 void __init plat_time_init(void)
-- 
1.6.0.6

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

* Re: [PATCH] MIPS: Octeon: Use non-overflowing arithmetic in sched_clock
  2010-01-08 22:47 [PATCH] MIPS: Octeon: Use non-overflowing arithmetic in sched_clock David Daney
@ 2010-01-11 10:20 ` Ralf Baechle
  2010-01-11 17:28   ` David Daney
  0 siblings, 1 reply; 3+ messages in thread
From: Ralf Baechle @ 2010-01-11 10:20 UTC (permalink / raw)
  To: David Daney; +Cc: linux-mips

On Fri, Jan 08, 2010 at 02:47:36PM -0800, David Daney wrote:

> With typical mult and shift values, the calculation for Octeon's
> sched_clock overflows when using 64-bit arithmetic.  Use 128-bit
> calculations instead.

Applied though my first thought whenever I see extended precission math
is gross - maybe we're going to find a better solution.  Hopefully!

  Ralf

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

* Re: [PATCH] MIPS: Octeon: Use non-overflowing arithmetic in sched_clock
  2010-01-11 10:20 ` Ralf Baechle
@ 2010-01-11 17:28   ` David Daney
  0 siblings, 0 replies; 3+ messages in thread
From: David Daney @ 2010-01-11 17:28 UTC (permalink / raw)
  To: Ralf Baechle; +Cc: linux-mips

Ralf Baechle wrote:
> On Fri, Jan 08, 2010 at 02:47:36PM -0800, David Daney wrote:
> 
>> With typical mult and shift values, the calculation for Octeon's
>> sched_clock overflows when using 64-bit arithmetic.  Use 128-bit
>> calculations instead.
> 
> Applied though my first thought whenever I see extended precission math
> is gross - maybe we're going to find a better solution.  Hopefully!
> 
>   Ralf

I did have some apprehension myself.  However consider:

* For an 800MHz core clock, clocksource_set_clock() generates a shift 
value of 31.  This leads to overflow of 64-bit arithmetic approximately 
every 8 seconds.  This specific case could be reduced to a 2 bit shift, 
resulting in time to overflow of more than 100 years.  But one can 
imagine clock rates that would require large shifts.

* We need to return a 64-bit clock value, this will overflow in about 
500 years, Unless we are very careful with our arithmetic, we risk 
overflow in unacceptable short time periods.

* This is octeon specific and the 128-bit operation is cheap.  Probably 
cheaper than accounting for overflows in 64-bit arithmetic.

David Daney

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

end of thread, other threads:[~2010-01-11 17:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-08 22:47 [PATCH] MIPS: Octeon: Use non-overflowing arithmetic in sched_clock David Daney
2010-01-11 10:20 ` Ralf Baechle
2010-01-11 17:28   ` David Daney

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