Path: blob/master/drivers/char/hw_random/via-rng.c
15111 views
/*1* RNG driver for VIA RNGs2*3* Copyright 2005 (c) MontaVista Software, Inc.4*5* with the majority of the code coming from:6*7* Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)8* (c) Copyright 2003 Red Hat Inc <[email protected]>9*10* derived from11*12* Hardware driver for the AMD 768 Random Number Generator (RNG)13* (c) Copyright 2001 Red Hat Inc14*15* derived from16*17* Hardware driver for Intel i810 Random Number Generator (RNG)18* Copyright 2000,2001 Jeff Garzik <[email protected]>19* Copyright 2000,2001 Philipp Rumpf <[email protected]>20*21* This file is licensed under the terms of the GNU General Public22* License version 2. This program is licensed "as is" without any23* warranty of any kind, whether express or implied.24*/2526#include <crypto/padlock.h>27#include <linux/module.h>28#include <linux/kernel.h>29#include <linux/hw_random.h>30#include <linux/delay.h>31#include <asm/io.h>32#include <asm/msr.h>33#include <asm/cpufeature.h>34#include <asm/i387.h>3536373839enum {40VIA_STRFILT_CNT_SHIFT = 16,41VIA_STRFILT_FAIL = (1 << 15),42VIA_STRFILT_ENABLE = (1 << 14),43VIA_RAWBITS_ENABLE = (1 << 13),44VIA_RNG_ENABLE = (1 << 6),45VIA_NOISESRC1 = (1 << 8),46VIA_NOISESRC2 = (1 << 9),47VIA_XSTORE_CNT_MASK = 0x0F,4849VIA_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits */50VIA_RNG_CHUNK_4 = 0x01, /* 32 rand bits, 32 stored bits */51VIA_RNG_CHUNK_4_MASK = 0xFFFFFFFF,52VIA_RNG_CHUNK_2 = 0x02, /* 16 rand bits, 32 stored bits */53VIA_RNG_CHUNK_2_MASK = 0xFFFF,54VIA_RNG_CHUNK_1 = 0x03, /* 8 rand bits, 32 stored bits */55VIA_RNG_CHUNK_1_MASK = 0xFF,56};5758/*59* Investigate using the 'rep' prefix to obtain 32 bits of random data60* in one insn. The upside is potentially better performance. The61* downside is that the instruction becomes no longer atomic. Due to62* this, just like familiar issues with /dev/random itself, the worst63* case of a 'rep xstore' could potentially pause a cpu for an64* unreasonably long time. In practice, this condition would likely65* only occur when the hardware is failing. (or so we hope :))66*67* Another possible performance boost may come from simply buffering68* until we have 4 bytes, thus returning a u32 at a time,69* instead of the current u8-at-a-time.70*71* Padlock instructions can generate a spurious DNA fault, so72* we have to call them in the context of irq_ts_save/restore()73*/7475static inline u32 xstore(u32 *addr, u32 edx_in)76{77u32 eax_out;78int ts_state;7980ts_state = irq_ts_save();8182asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"83: "=m" (*addr), "=a" (eax_out), "+d" (edx_in), "+D" (addr));8485irq_ts_restore(ts_state);86return eax_out;87}8889static int via_rng_data_present(struct hwrng *rng, int wait)90{91char buf[16 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__92((aligned(STACK_ALIGN)));93u32 *via_rng_datum = (u32 *)PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);94u32 bytes_out;95int i;9697/* We choose the recommended 1-byte-per-instruction RNG rate,98* for greater randomness at the expense of speed. Larger99* values 2, 4, or 8 bytes-per-instruction yield greater100* speed at lesser randomness.101*102* If you change this to another VIA_CHUNK_n, you must also103* change the ->n_bytes values in rng_vendor_ops[] tables.104* VIA_CHUNK_8 requires further code changes.105*106* A copy of MSR_VIA_RNG is placed in eax_out when xstore107* completes.108*/109110for (i = 0; i < 20; i++) {111*via_rng_datum = 0; /* paranoia, not really necessary */112bytes_out = xstore(via_rng_datum, VIA_RNG_CHUNK_1);113bytes_out &= VIA_XSTORE_CNT_MASK;114if (bytes_out || !wait)115break;116udelay(10);117}118rng->priv = *via_rng_datum;119return bytes_out ? 1 : 0;120}121122static int via_rng_data_read(struct hwrng *rng, u32 *data)123{124u32 via_rng_datum = (u32)rng->priv;125126*data = via_rng_datum;127128return 1;129}130131static int via_rng_init(struct hwrng *rng)132{133struct cpuinfo_x86 *c = &cpu_data(0);134u32 lo, hi, old_lo;135136/* VIA Nano CPUs don't have the MSR_VIA_RNG anymore. The RNG137* is always enabled if CPUID rng_en is set. There is no138* RNG configuration like it used to be the case in this139* register */140if ((c->x86 == 6) && (c->x86_model >= 0x0f)) {141if (!cpu_has_xstore_enabled) {142printk(KERN_ERR PFX "can't enable hardware RNG "143"if XSTORE is not enabled\n");144return -ENODEV;145}146return 0;147}148149/* Control the RNG via MSR. Tread lightly and pay very close150* close attention to values written, as the reserved fields151* are documented to be "undefined and unpredictable"; but it152* does not say to write them as zero, so I make a guess that153* we restore the values we find in the register.154*/155rdmsr(MSR_VIA_RNG, lo, hi);156157old_lo = lo;158lo &= ~(0x7f << VIA_STRFILT_CNT_SHIFT);159lo &= ~VIA_XSTORE_CNT_MASK;160lo &= ~(VIA_STRFILT_ENABLE | VIA_STRFILT_FAIL | VIA_RAWBITS_ENABLE);161lo |= VIA_RNG_ENABLE;162lo |= VIA_NOISESRC1;163164/* Enable secondary noise source on CPUs where it is present. */165166/* Nehemiah stepping 8 and higher */167if ((c->x86_model == 9) && (c->x86_mask > 7))168lo |= VIA_NOISESRC2;169170/* Esther */171if (c->x86_model >= 10)172lo |= VIA_NOISESRC2;173174if (lo != old_lo)175wrmsr(MSR_VIA_RNG, lo, hi);176177/* perhaps-unnecessary sanity check; remove after testing if178unneeded */179rdmsr(MSR_VIA_RNG, lo, hi);180if ((lo & VIA_RNG_ENABLE) == 0) {181printk(KERN_ERR PFX "cannot enable VIA C3 RNG, aborting\n");182return -ENODEV;183}184185return 0;186}187188189static struct hwrng via_rng = {190.name = "via",191.init = via_rng_init,192.data_present = via_rng_data_present,193.data_read = via_rng_data_read,194};195196197static int __init mod_init(void)198{199int err;200201if (!cpu_has_xstore)202return -ENODEV;203printk(KERN_INFO "VIA RNG detected\n");204err = hwrng_register(&via_rng);205if (err) {206printk(KERN_ERR PFX "RNG registering failed (%d)\n",207err);208goto out;209}210out:211return err;212}213214static void __exit mod_exit(void)215{216hwrng_unregister(&via_rng);217}218219module_init(mod_init);220module_exit(mod_exit);221222MODULE_DESCRIPTION("H/W RNG driver for VIA CPU with PadLock");223MODULE_LICENSE("GPL");224225226