Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

MIPS: Implement random_get_entropy with CP0 Random

Update to commit 9c9b415c50bc298ac61412dff856eae2f54889ee [MIPS:
Reimplement get_cycles().]

On systems were for whatever reasons we can't use the cycle counter, fall
back to the c0_random register as an entropy source. It has however a
very small range that makes it suitable for random_get_entropy only and
not get_cycles.

This optimised version compiles to 8 instructions in the fast path even in
the worst case of all the conditions to check being variable (including a
MFC0 move delay slot that is only required for very old processors):

828: 8cf90000 lw t9,0(a3)
828: R_MIPS_LO16 jiffies
82c: 40057800 mfc0 a1,c0_prid
830: 3c0200ff lui v0,0xff
834: 00a21024 and v0,a1,v0
838: 1040007d beqz v0,a30 <add_interrupt_randomness+0x22c>
83c: 3c030000 lui v1,0x0
83c: R_MIPS_HI16 cpu_data
840: 40024800 mfc0 v0,c0_count
844: 00000000 nop
848: 00409021 move s2,v0
84c: 8ce20000 lw v0,0(a3)
84c: R_MIPS_LO16 jiffies

On most targets the sequence will be shorter and on some it will reduce to
a single `MFC0 <reg>,c0_count', as all MIPS architecture (i.e. non-legacy
MIPS) processors require the CP0 Count register to be present.

The only known exception that reports MIPS architecture compliance, but
contrary to that lacks CP0 Count is the Ingenic JZ4740 thingy. For broken
platforms like that this code requires cpu_has_counter to be hardcoded to
0 (i.e. no variable setting is permitted) so as not to penalise all the
other good platforms out there.

The asm barrier is required so that the compiler does not pull any
potentially costly (cold cache!) `cpu_data' variable access into the fast
path.

Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Maciej W. Rozycki <macro@linux-mips.org>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: John Crispin <blogic@openwrt.org>
Cc: Andrew McGregor <andrewmcgr@gmail.com>
Cc: Dave Taht <dave.taht@bufferbloat.net>
Cc: Felix Fietkau <nbd@nbd.name>
Cc: Simon Kelley <simon@thekelleys.org.uk>
Cc: Jim Gettys <jg@freedesktop.org>
Cc: David Daney <ddaney@caviumnetworks.com>
Cc: linux-mips@linux-mips.org
Patchwork: https://patchwork.linux-mips.org/patch/6702/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>

authored by

Maciej W. Rozycki and committed by
Ralf Baechle
06947aaa fedfcb11

+50 -20
+49 -20
arch/mips/include/asm/timex.h
··· 4 4 * for more details. 5 5 * 6 6 * Copyright (C) 1998, 1999, 2003 by Ralf Baechle 7 + * Copyright (C) 2014 by Maciej W. Rozycki 7 8 */ 8 9 #ifndef _ASM_TIMEX_H 9 10 #define _ASM_TIMEX_H 10 11 11 12 #ifdef __KERNEL__ 12 13 14 + #include <linux/compiler.h> 15 + 16 + #include <asm/cpu.h> 13 17 #include <asm/cpu-features.h> 14 18 #include <asm/mipsregs.h> 15 19 #include <asm/cpu-type.h> ··· 49 45 * However for now the implementaton of this function doesn't get these 50 46 * fine details right. 51 47 */ 48 + static inline int can_use_mips_counter(unsigned int prid) 49 + { 50 + int comp = (prid & PRID_COMP_MASK) != PRID_COMP_LEGACY; 51 + 52 + if (__builtin_constant_p(cpu_has_counter) && !cpu_has_counter) 53 + return 0; 54 + else if (__builtin_constant_p(cpu_has_mips_r) && cpu_has_mips_r) 55 + return 1; 56 + else if (likely(!__builtin_constant_p(cpu_has_mips_r) && comp)) 57 + return 1; 58 + /* Make sure we don't peek at cpu_data[0].options in the fast path! */ 59 + if (!__builtin_constant_p(cpu_has_counter)) 60 + asm volatile("" : "=m" (cpu_data[0].options)); 61 + if (likely(cpu_has_counter && 62 + prid >= (PRID_IMP_R4000 | PRID_REV_ENCODE_44(5, 0)))) 63 + return 1; 64 + else 65 + return 0; 66 + } 67 + 52 68 static inline cycles_t get_cycles(void) 53 69 { 54 - switch (boot_cpu_type()) { 55 - case CPU_R4400PC: 56 - case CPU_R4400SC: 57 - case CPU_R4400MC: 58 - if ((read_c0_prid() & 0xff) >= 0x0050) 59 - return read_c0_count(); 60 - break; 61 - 62 - case CPU_R4000PC: 63 - case CPU_R4000SC: 64 - case CPU_R4000MC: 65 - break; 66 - 67 - default: 68 - if (cpu_has_counter) 69 - return read_c0_count(); 70 - break; 71 - } 72 - 73 - return 0; /* no usable counter */ 70 + if (can_use_mips_counter(read_c0_prid())) 71 + return read_c0_count(); 72 + else 73 + return 0; /* no usable counter */ 74 74 } 75 + 76 + /* 77 + * Like get_cycles - but where c0_count is not available we desperately 78 + * use c0_random in an attempt to get at least a little bit of entropy. 79 + * 80 + * R6000 and R6000A neither have a count register nor a random register. 81 + * That leaves no entropy source in the CPU itself. 82 + */ 83 + static inline unsigned long random_get_entropy(void) 84 + { 85 + unsigned int prid = read_c0_prid(); 86 + unsigned int imp = prid & PRID_IMP_MASK; 87 + 88 + if (can_use_mips_counter(prid)) 89 + return read_c0_count(); 90 + else if (likely(imp != PRID_IMP_R6000 && imp != PRID_IMP_R6000A)) 91 + return read_c0_random(); 92 + else 93 + return 0; /* no usable register */ 94 + } 95 + #define random_get_entropy random_get_entropy 75 96 76 97 #endif /* __KERNEL__ */ 77 98
+1
arch/mips/kernel/cpu-probe.c
··· 1026 1026 decode_configs(c); 1027 1027 /* JZRISC does not implement the CP0 counter. */ 1028 1028 c->options &= ~MIPS_CPU_COUNTER; 1029 + BUG_ON(!__builtin_constant_p(cpu_has_counter) || cpu_has_counter); 1029 1030 switch (c->processor_id & PRID_IMP_MASK) { 1030 1031 case PRID_IMP_JZRISC: 1031 1032 c->cputype = CPU_JZRISC;