2 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
3 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
7 Hardware driver for the AMD 768 Random Number Generator (RNG)
8 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
12 Hardware driver for Intel i810 Random Number Generator (RNG)
13 Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
14 Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
16 Please read Documentation/hw_random.txt for details on use.
18 ----------------------------------------------------------
19 This software may be used and distributed according to the terms
20 of the GNU General Public License, incorporated herein by reference.
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/interrupt.h>
31 #include <linux/spinlock.h>
32 #include <linux/random.h>
33 #include <linux/miscdevice.h>
34 #include <linux/smp_lock.h>
36 #include <linux/delay.h>
40 #include <asm/cpufeature.h>
44 #include <asm/uaccess.h>
48 * core module and version information
50 #define RNG_VERSION "1.0.0"
51 #define RNG_MODULE_NAME "hw_random"
52 #define RNG_DRIVER_NAME RNG_MODULE_NAME " hardware driver " RNG_VERSION
53 #define PFX RNG_MODULE_NAME ": "
59 #undef RNG_DEBUG /* define to enable copious debugging info */
62 /* note: prints function name for you */
63 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
65 #define DPRINTK(fmt, args...)
68 #define RNG_NDEBUG /* define to disable lightweight runtime checks */
72 #define assert(expr) \
74 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
75 #expr,__FILE__,__FUNCTION__,__LINE__); \
79 #define RNG_MISCDEV_MINOR 183 /* official */
81 static int rng_dev_open (struct inode *inode, struct file *filp);
82 static ssize_t rng_dev_read (struct file *filp, char *buf, size_t size,
85 static int __init intel_init (struct pci_dev *dev);
86 static void intel_cleanup(void);
87 static unsigned int intel_data_present (void);
88 static u32 intel_data_read (void);
90 static int __init amd_init (struct pci_dev *dev);
91 static void amd_cleanup(void);
92 static unsigned int amd_data_present (void);
93 static u32 amd_data_read (void);
96 static int __init via_init(struct pci_dev *dev);
97 static void via_cleanup(void);
98 static unsigned int via_data_present (void);
99 static u32 via_data_read (void);
102 struct rng_operations {
103 int (*init) (struct pci_dev *dev);
104 void (*cleanup) (void);
105 unsigned int (*data_present) (void);
106 u32 (*data_read) (void);
107 unsigned int n_bytes; /* number of bytes per ->data_read */
109 static struct rng_operations *rng_ops;
111 static struct file_operations rng_chrdev_ops = {
112 .owner = THIS_MODULE,
113 .open = rng_dev_open,
114 .read = rng_dev_read,
118 static struct miscdevice rng_miscdev = {
131 static struct rng_operations rng_vendor_ops[] = {
136 { intel_init, intel_cleanup, intel_data_present,
137 intel_data_read, 1 },
140 { amd_init, amd_cleanup, amd_data_present, amd_data_read, 4 },
144 { via_init, via_cleanup, via_data_present, via_data_read, 1 },
149 * Data for PCI driver interface
151 * This data only exists for exporting the supported
152 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
153 * register a pci_driver, because someone else might one day
154 * want to register another driver on the same PCI id.
156 static struct pci_device_id rng_pci_tbl[] = {
157 { 0x1022, 0x7443, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
158 { 0x1022, 0x746b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
160 { 0x8086, 0x2418, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
161 { 0x8086, 0x2428, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
162 { 0x8086, 0x2448, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
163 { 0x8086, 0x244e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
164 { 0x8086, 0x245e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
166 { 0, }, /* terminate list */
168 MODULE_DEVICE_TABLE (pci, rng_pci_tbl);
171 /***********************************************************************
173 * Intel RNG operations
178 * RNG registers (offsets from rng_mem)
180 #define INTEL_RNG_HW_STATUS 0
181 #define INTEL_RNG_PRESENT 0x40
182 #define INTEL_RNG_ENABLED 0x01
183 #define INTEL_RNG_STATUS 1
184 #define INTEL_RNG_DATA_PRESENT 0x01
185 #define INTEL_RNG_DATA 2
188 * Magic address at which Intel PCI bridges locate the RNG
190 #define INTEL_RNG_ADDR 0xFFBC015F
191 #define INTEL_RNG_ADDR_LEN 3
193 /* token to our ioremap'd RNG register area */
194 static void *rng_mem;
196 static inline u8 intel_hwstatus (void)
198 assert (rng_mem != NULL);
199 return readb (rng_mem + INTEL_RNG_HW_STATUS);
202 static inline u8 intel_hwstatus_set (u8 hw_status)
204 assert (rng_mem != NULL);
205 writeb (hw_status, rng_mem + INTEL_RNG_HW_STATUS);
206 return intel_hwstatus ();
209 static unsigned int intel_data_present(void)
211 assert (rng_mem != NULL);
213 return (readb (rng_mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT) ?
217 static u32 intel_data_read(void)
219 assert (rng_mem != NULL);
221 return readb (rng_mem + INTEL_RNG_DATA);
224 static int __init intel_init (struct pci_dev *dev)
231 rng_mem = ioremap (INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
232 if (rng_mem == NULL) {
233 printk (KERN_ERR PFX "cannot ioremap RNG Memory\n");
238 /* Check for Intel 82802 */
239 hw_status = intel_hwstatus ();
240 if ((hw_status & INTEL_RNG_PRESENT) == 0) {
241 printk (KERN_ERR PFX "RNG not detected\n");
243 goto err_out_free_map;
246 /* turn RNG h/w on, if it's off */
247 if ((hw_status & INTEL_RNG_ENABLED) == 0)
248 hw_status = intel_hwstatus_set (hw_status | INTEL_RNG_ENABLED);
249 if ((hw_status & INTEL_RNG_ENABLED) == 0) {
250 printk (KERN_ERR PFX "cannot enable RNG, aborting\n");
252 goto err_out_free_map;
255 DPRINTK ("EXIT, returning 0\n");
262 DPRINTK ("EXIT, returning %d\n", rc);
266 static void intel_cleanup(void)
270 hw_status = intel_hwstatus ();
271 if (hw_status & INTEL_RNG_ENABLED)
272 intel_hwstatus_set (hw_status & ~INTEL_RNG_ENABLED);
274 printk(KERN_WARNING PFX "unusual: RNG already disabled\n");
279 /***********************************************************************
285 static u32 pmbase; /* PMxx I/O base */
286 static struct pci_dev *amd_dev;
288 static unsigned int amd_data_present (void)
290 return inl(pmbase + 0xF4) & 1;
294 static u32 amd_data_read (void)
296 return inl(pmbase + 0xF0);
299 static int __init amd_init (struct pci_dev *dev)
306 pci_read_config_dword(dev, 0x58, &pmbase);
308 pmbase &= 0x0000FF00;
312 printk (KERN_ERR PFX "power management base not set\n");
317 pci_read_config_byte(dev, 0x40, &rnen);
318 rnen |= (1 << 7); /* RNG on */
319 pci_write_config_byte(dev, 0x40, rnen);
321 pci_read_config_byte(dev, 0x41, &rnen);
322 rnen |= (1 << 7); /* PMIO enable */
323 pci_write_config_byte(dev, 0x41, rnen);
325 printk(KERN_INFO PFX "AMD768 system management I/O registers at 0x%X.\n", pmbase);
329 DPRINTK ("EXIT, returning 0\n");
333 DPRINTK ("EXIT, returning %d\n", rc);
337 static void amd_cleanup(void)
341 pci_read_config_byte(amd_dev, 0x40, &rnen);
342 rnen &= ~(1 << 7); /* RNG off */
343 pci_write_config_byte(amd_dev, 0x40, rnen);
345 /* FIXME: twiddle pmio, also? */
349 /***********************************************************************
356 VIA_STRFILT_CNT_SHIFT = 16,
357 VIA_STRFILT_FAIL = (1 << 15),
358 VIA_STRFILT_ENABLE = (1 << 14),
359 VIA_RAWBITS_ENABLE = (1 << 13),
360 VIA_RNG_ENABLE = (1 << 6),
361 VIA_XSTORE_CNT_MASK = 0x0F,
363 VIA_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits */
364 VIA_RNG_CHUNK_4 = 0x01, /* 32 rand bits, 32 stored bits */
365 VIA_RNG_CHUNK_4_MASK = 0xFFFFFFFF,
366 VIA_RNG_CHUNK_2 = 0x02, /* 16 rand bits, 32 stored bits */
367 VIA_RNG_CHUNK_2_MASK = 0xFFFF,
368 VIA_RNG_CHUNK_1 = 0x03, /* 8 rand bits, 32 stored bits */
369 VIA_RNG_CHUNK_1_MASK = 0xFF,
375 * Investigate using the 'rep' prefix to obtain 32 bits of random data
376 * in one insn. The upside is potentially better performance. The
377 * downside is that the instruction becomes no longer atomic. Due to
378 * this, just like familiar issues with /dev/random itself, the worst
379 * case of a 'rep xstore' could potentially pause a cpu for an
380 * unreasonably long time. In practice, this condition would likely
381 * only occur when the hardware is failing. (or so we hope :))
383 * Another possible performance boost may come from simply buffering
384 * until we have 4 bytes, thus returning a u32 at a time,
385 * instead of the current u8-at-a-time.
388 static inline u32 xstore(u32 *addr, u32 edx_in)
392 asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"
393 :"=m"(*addr), "=a"(eax_out)
394 :"D"(addr), "d"(edx_in));
399 static unsigned int via_data_present(void)
403 /* We choose the recommended 1-byte-per-instruction RNG rate,
404 * for greater randomness at the expense of speed. Larger
405 * values 2, 4, or 8 bytes-per-instruction yield greater
406 * speed at lesser randomness.
408 * If you change this to another VIA_CHUNK_n, you must also
409 * change the ->n_bytes values in rng_vendor_ops[] tables.
410 * VIA_CHUNK_8 requires further code changes.
412 * A copy of MSR_VIA_RNG is placed in eax_out when xstore
415 via_rng_datum = 0; /* paranoia, not really necessary */
416 bytes_out = xstore(&via_rng_datum, VIA_RNG_CHUNK_1) & VIA_XSTORE_CNT_MASK;
423 static u32 via_data_read(void)
425 return via_rng_datum;
428 static int __init via_init(struct pci_dev *dev)
432 /* Control the RNG via MSR. Tread lightly and pay very close
433 * close attention to values written, as the reserved fields
434 * are documented to be "undefined and unpredictable"; but it
435 * does not say to write them as zero, so I make a guess that
436 * we restore the values we find in the register.
438 rdmsr(MSR_VIA_RNG, lo, hi);
441 lo &= ~(0x7f << VIA_STRFILT_CNT_SHIFT);
442 lo &= ~VIA_XSTORE_CNT_MASK;
443 lo &= ~(VIA_STRFILT_ENABLE | VIA_STRFILT_FAIL | VIA_RAWBITS_ENABLE);
444 lo |= VIA_RNG_ENABLE;
447 wrmsr(MSR_VIA_RNG, lo, hi);
449 /* perhaps-unnecessary sanity check; remove after testing if
451 rdmsr(MSR_VIA_RNG, lo, hi);
452 if ((lo & VIA_RNG_ENABLE) == 0) {
453 printk(KERN_ERR PFX "cannot enable VIA C3 RNG, aborting\n");
460 static void via_cleanup(void)
467 /***********************************************************************
469 * /dev/hwrandom character device handling (major 10, minor 183)
473 static int rng_dev_open (struct inode *inode, struct file *filp)
475 /* enforce read-only access to this chrdev */
476 if ((filp->f_mode & FMODE_READ) == 0)
478 if (filp->f_mode & FMODE_WRITE)
485 static ssize_t rng_dev_read (struct file *filp, char *buf, size_t size,
488 static spinlock_t rng_lock = SPIN_LOCK_UNLOCKED;
489 unsigned int have_data;
494 spin_lock(&rng_lock);
497 if (rng_ops->data_present()) {
498 data = rng_ops->data_read();
499 have_data = rng_ops->n_bytes;
502 spin_unlock (&rng_lock);
504 while (have_data && size) {
505 if (put_user((u8)data, buf++)) {
506 ret = ret ? : -EFAULT;
515 if (filp->f_flags & O_NONBLOCK)
516 return ret ? : -EAGAIN;
520 current->state = TASK_INTERRUPTIBLE;
524 udelay(200); /* FIXME: We could poll for 250uS ?? */
526 if (signal_pending (current))
527 return ret ? : -ERESTARTSYS;
535 * rng_init_one - look for and attempt to init a single RNG
537 static int __init rng_init_one (struct pci_dev *dev)
543 assert(rng_ops != NULL);
545 rc = rng_ops->init(dev);
549 rc = misc_register (&rng_miscdev);
551 printk (KERN_ERR PFX "misc device register failed\n");
552 goto err_out_cleanup_hw;
555 DPRINTK ("EXIT, returning 0\n");
561 DPRINTK ("EXIT, returning %d\n", rc);
567 MODULE_AUTHOR("The Linux Kernel team");
568 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
569 MODULE_LICENSE("GPL");
573 * rng_init - initialize RNG module
575 static int __init rng_init (void)
578 struct pci_dev *pdev = NULL;
579 const struct pci_device_id *ent;
583 /* Probe for Intel, AMD RNGs */
584 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
585 ent = pci_match_device (rng_pci_tbl, pdev);
587 rng_ops = &rng_vendor_ops[ent->driver_data];
593 /* Probe for VIA RNG */
594 if (cpu_has_xstore) {
595 rng_ops = &rng_vendor_ops[rng_hw_via];
601 DPRINTK ("EXIT, returning -ENODEV\n");
605 rc = rng_init_one (pdev);
609 printk (KERN_INFO RNG_DRIVER_NAME " loaded\n");
611 DPRINTK ("EXIT, returning 0\n");
617 * rng_init - shutdown RNG module
619 static void __exit rng_cleanup (void)
623 misc_deregister (&rng_miscdev);
625 if (rng_ops->cleanup)
632 module_init (rng_init);
633 module_exit (rng_cleanup);