Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / hw_random.c
blob757b1687b4cb028b26766e096a63515c46272368
1 /*
2 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
3 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
5 derived from
7 Hardware driver for the AMD 768 Random Number Generator (RNG)
8 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
10 derived from
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>
27 #include <linux/fs.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>
35 #include <linux/mm.h>
36 #include <linux/delay.h>
38 #ifdef __i386__
39 #include <asm/msr.h>
40 #include <asm/cpufeature.h>
41 #endif
43 #include <asm/io.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 ": "
57 * debugging macros
59 #undef RNG_DEBUG /* define to enable copious debugging info */
61 #ifdef RNG_DEBUG
62 /* note: prints function name for you */
63 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
64 #else
65 #define DPRINTK(fmt, args...)
66 #endif
68 #define RNG_NDEBUG /* define to disable lightweight runtime checks */
69 #ifdef RNG_NDEBUG
70 #define assert(expr)
71 #else
72 #define assert(expr) \
73 if(!(expr)) { \
74 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
75 #expr,__FILE__,__FUNCTION__,__LINE__); \
77 #endif
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,
83 loff_t * offp);
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);
95 static int __init via_init(struct pci_dev *dev);
96 static void via_cleanup(void);
97 static unsigned int via_data_present (void);
98 static u32 via_data_read (void);
100 struct rng_operations {
101 int (*init) (struct pci_dev *dev);
102 void (*cleanup) (void);
103 unsigned int (*data_present) (void);
104 u32 (*data_read) (void);
105 unsigned int n_bytes; /* number of bytes per ->data_read */
107 static struct rng_operations *rng_ops;
109 static struct file_operations rng_chrdev_ops = {
110 .owner = THIS_MODULE,
111 .open = rng_dev_open,
112 .read = rng_dev_read,
116 static struct miscdevice rng_miscdev = {
117 RNG_MISCDEV_MINOR,
118 RNG_MODULE_NAME,
119 &rng_chrdev_ops,
122 enum {
123 rng_hw_none,
124 rng_hw_intel,
125 rng_hw_amd,
126 rng_hw_via,
129 static struct rng_operations rng_vendor_ops[] = {
130 /* rng_hw_none */
131 { },
133 /* rng_hw_intel */
134 { intel_init, intel_cleanup, intel_data_present,
135 intel_data_read, 1 },
137 /* rng_hw_amd */
138 { amd_init, amd_cleanup, amd_data_present, amd_data_read, 4 },
140 /* rng_hw_via */
141 { via_init, via_cleanup, via_data_present, via_data_read, 1 },
145 * Data for PCI driver interface
147 * This data only exists for exporting the supported
148 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
149 * register a pci_driver, because someone else might one day
150 * want to register another driver on the same PCI id.
152 static struct pci_device_id rng_pci_tbl[] __initdata = {
153 { 0x1022, 0x7443, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
154 { 0x1022, 0x746b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
156 { 0x8086, 0x2418, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
157 { 0x8086, 0x2428, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
158 { 0x8086, 0x2448, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
159 { 0x8086, 0x244e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
160 { 0x8086, 0x245e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
162 { 0, }, /* terminate list */
164 MODULE_DEVICE_TABLE (pci, rng_pci_tbl);
167 /***********************************************************************
169 * Intel RNG operations
174 * RNG registers (offsets from rng_mem)
176 #define INTEL_RNG_HW_STATUS 0
177 #define INTEL_RNG_PRESENT 0x40
178 #define INTEL_RNG_ENABLED 0x01
179 #define INTEL_RNG_STATUS 1
180 #define INTEL_RNG_DATA_PRESENT 0x01
181 #define INTEL_RNG_DATA 2
184 * Magic address at which Intel PCI bridges locate the RNG
186 #define INTEL_RNG_ADDR 0xFFBC015F
187 #define INTEL_RNG_ADDR_LEN 3
189 /* token to our ioremap'd RNG register area */
190 static void *rng_mem;
192 static inline u8 intel_hwstatus (void)
194 assert (rng_mem != NULL);
195 return readb (rng_mem + INTEL_RNG_HW_STATUS);
198 static inline u8 intel_hwstatus_set (u8 hw_status)
200 assert (rng_mem != NULL);
201 writeb (hw_status, rng_mem + INTEL_RNG_HW_STATUS);
202 return intel_hwstatus ();
205 static unsigned int intel_data_present(void)
207 assert (rng_mem != NULL);
209 return (readb (rng_mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT) ?
210 1 : 0;
213 static u32 intel_data_read(void)
215 assert (rng_mem != NULL);
217 return readb (rng_mem + INTEL_RNG_DATA);
220 static int __init intel_init (struct pci_dev *dev)
222 int rc;
223 u8 hw_status;
225 DPRINTK ("ENTER\n");
227 rng_mem = ioremap (INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
228 if (rng_mem == NULL) {
229 printk (KERN_ERR PFX "cannot ioremap RNG Memory\n");
230 rc = -EBUSY;
231 goto err_out;
234 /* Check for Intel 82802 */
235 hw_status = intel_hwstatus ();
236 if ((hw_status & INTEL_RNG_PRESENT) == 0) {
237 printk (KERN_ERR PFX "RNG not detected\n");
238 rc = -ENODEV;
239 goto err_out_free_map;
242 /* turn RNG h/w on, if it's off */
243 if ((hw_status & INTEL_RNG_ENABLED) == 0)
244 hw_status = intel_hwstatus_set (hw_status | INTEL_RNG_ENABLED);
245 if ((hw_status & INTEL_RNG_ENABLED) == 0) {
246 printk (KERN_ERR PFX "cannot enable RNG, aborting\n");
247 rc = -EIO;
248 goto err_out_free_map;
251 DPRINTK ("EXIT, returning 0\n");
252 return 0;
254 err_out_free_map:
255 iounmap (rng_mem);
256 rng_mem = NULL;
257 err_out:
258 DPRINTK ("EXIT, returning %d\n", rc);
259 return rc;
262 static void intel_cleanup(void)
264 u8 hw_status;
266 hw_status = intel_hwstatus ();
267 if (hw_status & INTEL_RNG_ENABLED)
268 intel_hwstatus_set (hw_status & ~INTEL_RNG_ENABLED);
269 else
270 printk(KERN_WARNING PFX "unusual: RNG already disabled\n");
271 iounmap(rng_mem);
272 rng_mem = NULL;
275 /***********************************************************************
277 * AMD RNG operations
281 static u32 pmbase; /* PMxx I/O base */
282 static struct pci_dev *amd_dev;
284 static unsigned int amd_data_present (void)
286 return inl(pmbase + 0xF4) & 1;
290 static u32 amd_data_read (void)
292 return inl(pmbase + 0xF0);
295 static int __init amd_init (struct pci_dev *dev)
297 int rc;
298 u8 rnen;
300 DPRINTK ("ENTER\n");
302 pci_read_config_dword(dev, 0x58, &pmbase);
304 pmbase &= 0x0000FF00;
306 if (pmbase == 0)
308 printk (KERN_ERR PFX "power management base not set\n");
309 rc = -EIO;
310 goto err_out;
313 pci_read_config_byte(dev, 0x40, &rnen);
314 rnen |= (1 << 7); /* RNG on */
315 pci_write_config_byte(dev, 0x40, rnen);
317 pci_read_config_byte(dev, 0x41, &rnen);
318 rnen |= (1 << 7); /* PMIO enable */
319 pci_write_config_byte(dev, 0x41, rnen);
321 printk(KERN_INFO PFX "AMD768 system management I/O registers at 0x%X.\n", pmbase);
323 amd_dev = dev;
325 DPRINTK ("EXIT, returning 0\n");
326 return 0;
328 err_out:
329 DPRINTK ("EXIT, returning %d\n", rc);
330 return rc;
333 static void amd_cleanup(void)
335 u8 rnen;
337 pci_read_config_byte(amd_dev, 0x40, &rnen);
338 rnen &= ~(1 << 7); /* RNG off */
339 pci_write_config_byte(amd_dev, 0x40, rnen);
341 /* FIXME: twiddle pmio, also? */
344 /***********************************************************************
346 * VIA RNG operations
350 enum {
351 VIA_STRFILT_CNT_SHIFT = 16,
352 VIA_STRFILT_FAIL = (1 << 15),
353 VIA_STRFILT_ENABLE = (1 << 14),
354 VIA_RAWBITS_ENABLE = (1 << 13),
355 VIA_RNG_ENABLE = (1 << 6),
356 VIA_XSTORE_CNT_MASK = 0x0F,
358 VIA_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits */
359 VIA_RNG_CHUNK_4 = 0x01, /* 32 rand bits, 32 stored bits */
360 VIA_RNG_CHUNK_4_MASK = 0xFFFFFFFF,
361 VIA_RNG_CHUNK_2 = 0x02, /* 16 rand bits, 32 stored bits */
362 VIA_RNG_CHUNK_2_MASK = 0xFFFF,
363 VIA_RNG_CHUNK_1 = 0x03, /* 8 rand bits, 32 stored bits */
364 VIA_RNG_CHUNK_1_MASK = 0xFF,
367 u32 via_rng_datum;
370 * Investigate using the 'rep' prefix to obtain 32 bits of random data
371 * in one insn. The upside is potentially better performance. The
372 * downside is that the instruction becomes no longer atomic. Due to
373 * this, just like familiar issues with /dev/random itself, the worst
374 * case of a 'rep xstore' could potentially pause a cpu for an
375 * unreasonably long time. In practice, this condition would likely
376 * only occur when the hardware is failing. (or so we hope :))
378 * Another possible performance boost may come from simply buffering
379 * until we have 4 bytes, thus returning a u32 at a time,
380 * instead of the current u8-at-a-time.
383 static inline u32 xstore(u32 *addr, u32 edx_in)
385 u32 eax_out;
387 asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"
388 :"=m"(*addr), "=a"(eax_out)
389 :"D"(addr), "d"(edx_in));
391 return eax_out;
394 static unsigned int via_data_present(void)
396 u32 bytes_out;
398 /* We choose the recommended 1-byte-per-instruction RNG rate,
399 * for greater randomness at the expense of speed. Larger
400 * values 2, 4, or 8 bytes-per-instruction yield greater
401 * speed at lesser randomness.
403 * If you change this to another VIA_CHUNK_n, you must also
404 * change the ->n_bytes values in rng_vendor_ops[] tables.
405 * VIA_CHUNK_8 requires further code changes.
407 * A copy of MSR_VIA_RNG is placed in eax_out when xstore
408 * completes.
410 via_rng_datum = 0; /* paranoia, not really necessary */
411 bytes_out = xstore(&via_rng_datum, VIA_RNG_CHUNK_1) & VIA_XSTORE_CNT_MASK;
412 if (bytes_out == 0)
413 return 0;
415 return 1;
418 static u32 via_data_read(void)
420 return via_rng_datum;
423 static int __init via_init(struct pci_dev *dev)
425 u32 lo, hi, old_lo;
427 /* Control the RNG via MSR. Tread lightly and pay very close
428 * close attention to values written, as the reserved fields
429 * are documented to be "undefined and unpredictable"; but it
430 * does not say to write them as zero, so I make a guess that
431 * we restore the values we find in the register.
433 rdmsr(MSR_VIA_RNG, lo, hi);
435 old_lo = lo;
436 lo &= ~(0x7f << VIA_STRFILT_CNT_SHIFT);
437 lo &= ~VIA_XSTORE_CNT_MASK;
438 lo &= ~(VIA_STRFILT_ENABLE | VIA_STRFILT_FAIL | VIA_RAWBITS_ENABLE);
439 lo |= VIA_RNG_ENABLE;
441 if (lo != old_lo)
442 wrmsr(MSR_VIA_RNG, lo, hi);
444 /* perhaps-unnecessary sanity check; remove after testing if
445 unneeded */
446 rdmsr(MSR_VIA_RNG, lo, hi);
447 if ((lo & VIA_RNG_ENABLE) == 0) {
448 printk(KERN_ERR PFX "cannot enable VIA C3 RNG, aborting\n");
449 return -ENODEV;
452 return 0;
455 static void via_cleanup(void)
457 u32 lo, hi;
459 rdmsr(MSR_VIA_RNG, lo, hi);
460 lo &= ~VIA_RNG_ENABLE;
461 wrmsr(MSR_VIA_RNG, lo, hi);
465 /***********************************************************************
467 * /dev/hwrandom character device handling (major 10, minor 183)
471 static int rng_dev_open (struct inode *inode, struct file *filp)
473 /* enforce read-only access to this chrdev */
474 if ((filp->f_mode & FMODE_READ) == 0)
475 return -EINVAL;
476 if (filp->f_mode & FMODE_WRITE)
477 return -EINVAL;
479 return 0;
483 static ssize_t rng_dev_read (struct file *filp, char *buf, size_t size,
484 loff_t * offp)
486 static spinlock_t rng_lock = SPIN_LOCK_UNLOCKED;
487 unsigned int have_data;
488 u32 data = 0;
489 ssize_t ret = 0;
491 while (size) {
492 spin_lock(&rng_lock);
494 have_data = 0;
495 if (rng_ops->data_present()) {
496 data = rng_ops->data_read();
497 have_data = rng_ops->n_bytes;
500 spin_unlock (&rng_lock);
502 while (have_data && size) {
503 if (put_user((u8)data, buf++)) {
504 ret = ret ? : -EFAULT;
505 break;
507 size--;
508 ret++;
509 have_data--;
510 data>>=8;
513 if (filp->f_flags & O_NONBLOCK)
514 return ret ? : -EAGAIN;
516 if(need_resched())
518 current->state = TASK_INTERRUPTIBLE;
519 schedule_timeout(1);
521 else
522 udelay(200); /* FIXME: We could poll for 250uS ?? */
524 if (signal_pending (current))
525 return ret ? : -ERESTARTSYS;
527 return ret;
533 * rng_init_one - look for and attempt to init a single RNG
535 static int __init rng_init_one (struct pci_dev *dev)
537 int rc;
539 DPRINTK ("ENTER\n");
541 assert(rng_ops != NULL);
543 rc = rng_ops->init(dev);
544 if (rc)
545 goto err_out;
547 rc = misc_register (&rng_miscdev);
548 if (rc) {
549 printk (KERN_ERR PFX "misc device register failed\n");
550 goto err_out_cleanup_hw;
553 DPRINTK ("EXIT, returning 0\n");
554 return 0;
556 err_out_cleanup_hw:
557 rng_ops->cleanup();
558 err_out:
559 DPRINTK ("EXIT, returning %d\n", rc);
560 return rc;
565 MODULE_AUTHOR("The Linux Kernel team");
566 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
567 MODULE_LICENSE("GPL");
571 * rng_init - initialize RNG module
573 static int __init rng_init (void)
575 int rc;
576 struct pci_dev *pdev = NULL;
577 const struct pci_device_id *ent;
579 DPRINTK ("ENTER\n");
581 /* Probe for Intel, AMD RNGs */
582 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
583 ent = pci_match_device (rng_pci_tbl, pdev);
584 if (ent) {
585 rng_ops = &rng_vendor_ops[ent->driver_data];
586 goto match;
590 #ifdef __i386__
591 /* Probe for VIA RNG */
592 if (cpu_has_xstore) {
593 rng_ops = &rng_vendor_ops[rng_hw_via];
594 pdev = NULL;
595 goto match;
597 #endif
599 DPRINTK ("EXIT, returning -ENODEV\n");
600 return -ENODEV;
602 match:
603 rc = rng_init_one (pdev);
604 if (rc)
605 return rc;
607 printk (KERN_INFO RNG_DRIVER_NAME " loaded\n");
609 DPRINTK ("EXIT, returning 0\n");
610 return 0;
615 * rng_init - shutdown RNG module
617 static void __exit rng_cleanup (void)
619 DPRINTK ("ENTER\n");
621 misc_deregister (&rng_miscdev);
623 if (rng_ops->cleanup)
624 rng_ops->cleanup();
626 DPRINTK ("EXIT\n");
630 module_init (rng_init);
631 module_exit (rng_cleanup);