kernel: backport get_cycles() fix
[openwrt/staging/wigyori.git] / target / linux / generic / patches-3.10 / 307-mips-Reimplement-get_cycles.patch
1 From 9c9b415c50bc298ac61412dff856eae2f54889ee Mon Sep 17 00:00:00 2001
2 From: Ralf Baechle <ralf@linux-mips.org>
3 Date: Thu, 12 Sep 2013 13:47:32 +0200
4 Subject: [PATCH] MIPS: Reimplement get_cycles().
5
6 This essentially reverts commit efb9ca08b5a2374b29938cdcab417ce4feb14b54
7 (kernel.org) / 58020a106879a8b372068741c81f0015c9b0b96dbv [[MIPS] Change
8 get_cycles to always return 0.]
9
10 Most users of get_cycles() invoke it as a timing interface. That's why
11 in modern kernels it was never very much missed for. /dev/random however
12 uses get_cycles() in the how the jitter in the interrupt timing contains
13 some useful entropy.
14
15 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
16 ---
17 arch/mips/include/asm/timex.h | 33 ++++++++++++++++++++++++++++++++-
18 1 file changed, 32 insertions(+), 1 deletion(-)
19
20 --- a/arch/mips/include/asm/timex.h
21 +++ b/arch/mips/include/asm/timex.h
22 @@ -10,6 +10,7 @@
23
24 #ifdef __KERNEL__
25
26 +#include <asm/cpu-features.h>
27 #include <asm/mipsregs.h>
28
29 /*
30 @@ -33,9 +34,38 @@
31
32 typedef unsigned int cycles_t;
33
34 +/*
35 + * On R4000/R4400 before version 5.0 an erratum exists such that if the
36 + * cycle counter is read in the exact moment that it is matching the
37 + * compare register, no interrupt will be generated.
38 + *
39 + * There is a suggested workaround and also the erratum can't strike if
40 + * the compare interrupt isn't being used as the clock source device.
41 + * However for now the implementaton of this function doesn't get these
42 + * fine details right.
43 + */
44 static inline cycles_t get_cycles(void)
45 {
46 - return 0;
47 + switch (cpu_data[0].cputype) {
48 + case CPU_R4400PC:
49 + case CPU_R4400SC:
50 + case CPU_R4400MC:
51 + if ((read_c0_prid() & 0xff) >= 0x0050)
52 + return read_c0_count();
53 + break;
54 +
55 + case CPU_R4000PC:
56 + case CPU_R4000SC:
57 + case CPU_R4000MC:
58 + break;
59 +
60 + default:
61 + if (cpu_has_counter)
62 + return read_c0_count();
63 + break;
64 + }
65 +
66 + return 0; /* no usable counter */
67 }
68
69 #endif /* __KERNEL__ */