1/* 2 * Kernel interface for the s390 arch_random_* functions 3 * 4 * Copyright IBM Corp. 2017 5 * 6 * Author: Harald Freudenberger <freude@de.ibm.com> 7 * 8 */ 9 10#ifndef _ASM_S390_ARCHRANDOM_H 11#define _ASM_S390_ARCHRANDOM_H 12 13#ifdef CONFIG_ARCH_RANDOM 14 15#include <linux/static_key.h> 16#include <linux/atomic.h> 17#include <asm/cpacf.h> 18 19DECLARE_STATIC_KEY_FALSE(s390_arch_random_available); 20extern atomic64_t s390_arch_random_counter; 21 22static void s390_arch_random_generate(u8 *buf, unsigned int nbytes) 23{ 24 cpacf_trng(NULL, 0, buf, nbytes); 25 atomic64_add(nbytes, &s390_arch_random_counter); 26} 27 28static inline bool arch_has_random(void) 29{ 30 if (static_branch_likely(&s390_arch_random_available)) 31 return true; 32 return false; 33} 34 35static inline bool arch_has_random_seed(void) 36{ 37 return arch_has_random(); 38} 39 40static inline bool arch_get_random_long(unsigned long *v) 41{ 42 if (static_branch_likely(&s390_arch_random_available)) { 43 s390_arch_random_generate((u8 *)v, sizeof(*v)); 44 return true; 45 } 46 return false; 47} 48 49static inline bool arch_get_random_int(unsigned int *v) 50{ 51 if (static_branch_likely(&s390_arch_random_available)) { 52 s390_arch_random_generate((u8 *)v, sizeof(*v)); 53 return true; 54 } 55 return false; 56} 57 58static inline bool arch_get_random_seed_long(unsigned long *v) 59{ 60 return arch_get_random_long(v); 61} 62 63static inline bool arch_get_random_seed_int(unsigned int *v) 64{ 65 return arch_get_random_int(v); 66} 67 68#endif /* CONFIG_ARCH_RANDOM */ 69#endif /* _ASM_S390_ARCHRANDOM_H */ 70