Skip to content
This repository has been archived by the owner on Apr 13, 2019. It is now read-only.

Commit

Permalink
Avoid a division in {u,n}delay
Browse files Browse the repository at this point in the history
  • Loading branch information
palmer-dabbelt committed May 25, 2017
1 parent 2101777 commit d397332
Showing 1 changed file with 70 additions and 6 deletions.
76 changes: 70 additions & 6 deletions arch/riscv/lib/delay.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,62 @@
#include <linux/timex.h>
#include <linux/export.h>

/*
* This is copies from arch/arm/include/asm/delay.h
*
* Loop (or tick) based delay:
*
* loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec
*
* where:
*
* jiffies_per_sec = HZ
* us_per_sec = 1000000
*
* Therefore the constant part is HZ / 1000000 which is a small
* fractional number. To make this usable with integer math, we
* scale up this constant by 2^31, perform the actual multiplication,
* and scale the result back down by 2^31 with a simple shift:
*
* loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31
*
* where:
*
* UDELAY_MULT = 2^31 * HZ / 1000000
* = (2^31 / 1000000) * HZ
* = 2147.483648 * HZ
* = 2147 * HZ + 483648 * HZ / 1000000
*
* 31 is the biggest scale shift value that won't overflow 32 bits for
* delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000.
*/
#define MAX_UDELAY_US 2000
#define MAX_UDELAY_HZ 1000
#define UDELAY_MULT (2147UL * HZ + 483648UL * HZ / 1000000UL)
#define UDELAY_SHIFT 31

#if HZ > MAX_UDELAY_HZ
#error "HZ > MAX_UDELAY_HZ"
#endif

/* RISC-V supports both UDELAY and NDELAY. This is largely the same as above,
* but with different constants. I kept the number of milliseconds the same
* between the two calls.
*
* NDELAY_MULT = 2^27 * HZ / 1000000000
* = (2^27 / 1000000000) * HZ
* = 1.34217728 * HZ
* = 1 * HZ + 342177280 * HZ / 1000000000
*/
#define MAX_NDELAY_NS 2000000UL
#define MAX_NDELAY_HZ MAX_UDELAY_HZ
#define NDELAY_MULT ((unsigned long)(1ULL * HZ + 342177280ULL * HZ / 1000000000ULL))
#define NDELAY_SHIFT 27

#if HZ > MAX_NDELAY_HZ
#error "HZ > MAX_NDELAY_HZ"
#endif

void __delay(unsigned long cycles)
{
u64 t0 = get_cycles();
Expand All @@ -26,18 +82,26 @@ void __delay(unsigned long cycles)

void udelay(unsigned long usecs)
{
u64 ucycles = (u64)usecs * riscv_timebase;
unsigned long ucycles = usecs * lpj_fine * UDELAY_MULT;

do_div(ucycles, 1000000U);
__delay((unsigned long)ucycles);
if (usecs > MAX_UDELAY_US) {
__delay((u64)usecs * riscv_timebase / 1000000ULL);
return;
}

__delay(ucycles >> UDELAY_SHIFT);
}
EXPORT_SYMBOL(udelay);

void ndelay(unsigned long nsecs)
{
u64 ncycles = (u64)nsecs * riscv_timebase;
unsigned long ncycles = nsecs * lpj_fine * NDELAY_MULT;

if (nsecs > MAX_NDELAY_NS) {
__delay((u64)nsecs * riscv_timebase / 1000000000ULL);
return;
}

do_div(ncycles, 1000000000U);
__delay((unsigned long)ncycles);
__delay(ncycles >> NDELAY_SHIFT);
}
EXPORT_SYMBOL(ndelay);

0 comments on commit d397332

Please sign in to comment.