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 ": "
60 /* pr_debug() collapses to a no-op if DEBUG is not defined */
61 #define DPRINTK(fmt, args...) pr_debug(PFX "%s: " fmt, __FUNCTION__ , ## args)
64 #undef RNG_NDEBUG /* define to enable lightweight runtime checks */
66 #define assert(expr) \
68 printk(KERN_DEBUG PFX "Assertion failed! %s,%s,%s," \
69 "line=%d\n", #expr, __FILE__, __FUNCTION__, __LINE__); \
75 #define RNG_MISCDEV_MINOR 183 /* official */
77 static int rng_dev_open (struct inode
*inode
, struct file
*filp
);
78 static ssize_t
rng_dev_read (struct file
*filp
, char __user
*buf
, size_t size
,
81 static int __init
intel_init (struct pci_dev
*dev
);
82 static void intel_cleanup(void);
83 static unsigned int intel_data_present (void);
84 static u32
intel_data_read (void);
86 static int __init
amd_init (struct pci_dev
*dev
);
87 static void amd_cleanup(void);
88 static unsigned int amd_data_present (void);
89 static u32
amd_data_read (void);
92 static int __init
via_init(struct pci_dev
*dev
);
93 static void via_cleanup(void);
94 static unsigned int via_data_present (void);
95 static u32
via_data_read (void);
98 struct rng_operations
{
99 int (*init
) (struct pci_dev
*dev
);
100 void (*cleanup
) (void);
101 unsigned int (*data_present
) (void);
102 u32 (*data_read
) (void);
103 unsigned int n_bytes
; /* number of bytes per ->data_read */
105 static struct rng_operations
*rng_ops
;
107 static struct file_operations rng_chrdev_ops
= {
108 .owner
= THIS_MODULE
,
109 .open
= rng_dev_open
,
110 .read
= rng_dev_read
,
114 static struct miscdevice rng_miscdev
= {
127 static struct rng_operations rng_vendor_ops
[] = {
132 { intel_init
, intel_cleanup
, intel_data_present
,
133 intel_data_read
, 1 },
136 { amd_init
, amd_cleanup
, amd_data_present
, amd_data_read
, 4 },
140 { 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
[] = {
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 __iomem
*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
) ?
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
)
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");
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");
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");
248 goto err_out_free_map
;
251 DPRINTK ("EXIT, returning 0\n");
258 DPRINTK ("EXIT, returning %d\n", rc
);
262 static void intel_cleanup(void)
266 hw_status
= intel_hwstatus ();
267 if (hw_status
& INTEL_RNG_ENABLED
)
268 intel_hwstatus_set (hw_status
& ~INTEL_RNG_ENABLED
);
270 printk(KERN_WARNING PFX
"unusual: RNG already disabled\n");
275 /***********************************************************************
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
)
302 pci_read_config_dword(dev
, 0x58, &pmbase
);
304 pmbase
&= 0x0000FF00;
308 printk (KERN_ERR PFX
"power management base not set\n");
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 pr_info( PFX
"AMD768 system management I/O registers at 0x%X.\n",
326 DPRINTK ("EXIT, returning 0\n");
330 DPRINTK ("EXIT, returning %d\n", rc
);
334 static void amd_cleanup(void)
338 pci_read_config_byte(amd_dev
, 0x40, &rnen
);
339 rnen
&= ~(1 << 7); /* RNG off */
340 pci_write_config_byte(amd_dev
, 0x40, rnen
);
342 /* FIXME: twiddle pmio, also? */
346 /***********************************************************************
353 VIA_STRFILT_CNT_SHIFT
= 16,
354 VIA_STRFILT_FAIL
= (1 << 15),
355 VIA_STRFILT_ENABLE
= (1 << 14),
356 VIA_RAWBITS_ENABLE
= (1 << 13),
357 VIA_RNG_ENABLE
= (1 << 6),
358 VIA_XSTORE_CNT_MASK
= 0x0F,
360 VIA_RNG_CHUNK_8
= 0x00, /* 64 rand bits, 64 stored bits */
361 VIA_RNG_CHUNK_4
= 0x01, /* 32 rand bits, 32 stored bits */
362 VIA_RNG_CHUNK_4_MASK
= 0xFFFFFFFF,
363 VIA_RNG_CHUNK_2
= 0x02, /* 16 rand bits, 32 stored bits */
364 VIA_RNG_CHUNK_2_MASK
= 0xFFFF,
365 VIA_RNG_CHUNK_1
= 0x03, /* 8 rand bits, 32 stored bits */
366 VIA_RNG_CHUNK_1_MASK
= 0xFF,
369 static u32 via_rng_datum
;
372 * Investigate using the 'rep' prefix to obtain 32 bits of random data
373 * in one insn. The upside is potentially better performance. The
374 * downside is that the instruction becomes no longer atomic. Due to
375 * this, just like familiar issues with /dev/random itself, the worst
376 * case of a 'rep xstore' could potentially pause a cpu for an
377 * unreasonably long time. In practice, this condition would likely
378 * only occur when the hardware is failing. (or so we hope :))
380 * Another possible performance boost may come from simply buffering
381 * until we have 4 bytes, thus returning a u32 at a time,
382 * instead of the current u8-at-a-time.
385 static inline u32
xstore(u32
*addr
, u32 edx_in
)
389 asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"
390 :"=m"(*addr
), "=a"(eax_out
)
391 :"D"(addr
), "d"(edx_in
));
396 static unsigned int via_data_present(void)
400 /* We choose the recommended 1-byte-per-instruction RNG rate,
401 * for greater randomness at the expense of speed. Larger
402 * values 2, 4, or 8 bytes-per-instruction yield greater
403 * speed at lesser randomness.
405 * If you change this to another VIA_CHUNK_n, you must also
406 * change the ->n_bytes values in rng_vendor_ops[] tables.
407 * VIA_CHUNK_8 requires further code changes.
409 * A copy of MSR_VIA_RNG is placed in eax_out when xstore
412 via_rng_datum
= 0; /* paranoia, not really necessary */
413 bytes_out
= xstore(&via_rng_datum
, VIA_RNG_CHUNK_1
) & VIA_XSTORE_CNT_MASK
;
420 static u32
via_data_read(void)
422 return via_rng_datum
;
425 static int __init
via_init(struct pci_dev
*dev
)
429 /* Control the RNG via MSR. Tread lightly and pay very close
430 * close attention to values written, as the reserved fields
431 * are documented to be "undefined and unpredictable"; but it
432 * does not say to write them as zero, so I make a guess that
433 * we restore the values we find in the register.
435 rdmsr(MSR_VIA_RNG
, lo
, hi
);
438 lo
&= ~(0x7f << VIA_STRFILT_CNT_SHIFT
);
439 lo
&= ~VIA_XSTORE_CNT_MASK
;
440 lo
&= ~(VIA_STRFILT_ENABLE
| VIA_STRFILT_FAIL
| VIA_RAWBITS_ENABLE
);
441 lo
|= VIA_RNG_ENABLE
;
444 wrmsr(MSR_VIA_RNG
, lo
, hi
);
446 /* perhaps-unnecessary sanity check; remove after testing if
448 rdmsr(MSR_VIA_RNG
, lo
, hi
);
449 if ((lo
& VIA_RNG_ENABLE
) == 0) {
450 printk(KERN_ERR PFX
"cannot enable VIA C3 RNG, aborting\n");
457 static void via_cleanup(void)
464 /***********************************************************************
466 * /dev/hwrandom character device handling (major 10, minor 183)
470 static int rng_dev_open (struct inode
*inode
, struct file
*filp
)
472 /* enforce read-only access to this chrdev */
473 if ((filp
->f_mode
& FMODE_READ
) == 0)
475 if (filp
->f_mode
& FMODE_WRITE
)
482 static ssize_t
rng_dev_read (struct file
*filp
, char __user
*buf
, size_t size
,
485 static DEFINE_SPINLOCK(rng_lock
);
486 unsigned int have_data
;
491 spin_lock(&rng_lock
);
494 if (rng_ops
->data_present()) {
495 data
= rng_ops
->data_read();
496 have_data
= rng_ops
->n_bytes
;
499 spin_unlock (&rng_lock
);
501 while (have_data
&& size
) {
502 if (put_user((u8
)data
, buf
++)) {
503 ret
= ret
? : -EFAULT
;
512 if (filp
->f_flags
& O_NONBLOCK
)
513 return ret
? : -EAGAIN
;
517 current
->state
= TASK_INTERRUPTIBLE
;
521 udelay(200); /* FIXME: We could poll for 250uS ?? */
523 if (signal_pending (current
))
524 return ret
? : -ERESTARTSYS
;
532 * rng_init_one - look for and attempt to init a single RNG
534 static int __init
rng_init_one (struct pci_dev
*dev
)
540 assert(rng_ops
!= NULL
);
542 rc
= rng_ops
->init(dev
);
546 rc
= misc_register (&rng_miscdev
);
548 printk (KERN_ERR PFX
"misc device register failed\n");
549 goto err_out_cleanup_hw
;
552 DPRINTK ("EXIT, returning 0\n");
558 DPRINTK ("EXIT, returning %d\n", rc
);
564 MODULE_AUTHOR("The Linux Kernel team");
565 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
566 MODULE_LICENSE("GPL");
570 * rng_init - initialize RNG module
572 static int __init
rng_init (void)
575 struct pci_dev
*pdev
= NULL
;
576 const struct pci_device_id
*ent
;
580 /* Probe for Intel, AMD RNGs */
581 for_each_pci_dev(pdev
) {
582 ent
= pci_match_device (rng_pci_tbl
, pdev
);
584 rng_ops
= &rng_vendor_ops
[ent
->driver_data
];
590 /* Probe for VIA RNG */
591 if (cpu_has_xstore
) {
592 rng_ops
= &rng_vendor_ops
[rng_hw_via
];
598 DPRINTK ("EXIT, returning -ENODEV\n");
602 rc
= rng_init_one (pdev
);
606 pr_info( RNG_DRIVER_NAME
" loaded\n");
608 DPRINTK ("EXIT, returning 0\n");
614 * rng_init - shutdown RNG module
616 static void __exit
rng_cleanup (void)
620 misc_deregister (&rng_miscdev
);
622 if (rng_ops
->cleanup
)
629 module_init (rng_init
);
630 module_exit (rng_cleanup
);