blob: 49769f59ea1b2fb79528a5e054f90bf28ea9499d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jordan Crousea7a4ad02006-01-06 00:12:15 -08002 Added support for the AMD Geode LX RNG
3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
4
5 derived from
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
9
10 derived from
11
12 Hardware driver for the AMD 768 Random Number Generator (RNG)
13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
14
15 derived from
16
17 Hardware driver for Intel i810 Random Number Generator (RNG)
18 Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
20
21 Please read Documentation/hw_random.txt for details on use.
22
23 ----------------------------------------------------------
24 This software may be used and distributed according to the terms
25 of the GNU General Public License, incorporated herein by reference.
26
27 */
28
29
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/fs.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/interrupt.h>
36#include <linux/spinlock.h>
37#include <linux/random.h>
38#include <linux/miscdevice.h>
39#include <linux/smp_lock.h>
40#include <linux/mm.h>
41#include <linux/delay.h>
42
43#ifdef __i386__
44#include <asm/msr.h>
45#include <asm/cpufeature.h>
46#endif
47
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
51
52/*
53 * core module and version information
54 */
55#define RNG_VERSION "1.0.0"
56#define RNG_MODULE_NAME "hw_random"
57#define RNG_DRIVER_NAME RNG_MODULE_NAME " hardware driver " RNG_VERSION
58#define PFX RNG_MODULE_NAME ": "
59
60
61/*
62 * debugging macros
63 */
64
65/* pr_debug() collapses to a no-op if DEBUG is not defined */
66#define DPRINTK(fmt, args...) pr_debug(PFX "%s: " fmt, __FUNCTION__ , ## args)
67
68
69#undef RNG_NDEBUG /* define to enable lightweight runtime checks */
70#ifdef RNG_NDEBUG
71#define assert(expr) \
72 if(!(expr)) { \
73 printk(KERN_DEBUG PFX "Assertion failed! %s,%s,%s," \
74 "line=%d\n", #expr, __FILE__, __FUNCTION__, __LINE__); \
75 }
76#else
77#define assert(expr)
78#endif
79
80#define RNG_MISCDEV_MINOR 183 /* official */
81
82static int rng_dev_open (struct inode *inode, struct file *filp);
83static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
84 loff_t * offp);
85
86static int __init intel_init (struct pci_dev *dev);
87static void intel_cleanup(void);
88static unsigned int intel_data_present (void);
89static u32 intel_data_read (void);
90
91static int __init amd_init (struct pci_dev *dev);
92static void amd_cleanup(void);
93static unsigned int amd_data_present (void);
94static u32 amd_data_read (void);
95
96#ifdef __i386__
97static int __init via_init(struct pci_dev *dev);
98static void via_cleanup(void);
99static unsigned int via_data_present (void);
100static u32 via_data_read (void);
101#endif
102
Jordan Crousea7a4ad02006-01-06 00:12:15 -0800103static int __init geode_init(struct pci_dev *dev);
104static void geode_cleanup(void);
105static unsigned int geode_data_present (void);
106static u32 geode_data_read (void);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108struct rng_operations {
109 int (*init) (struct pci_dev *dev);
110 void (*cleanup) (void);
111 unsigned int (*data_present) (void);
112 u32 (*data_read) (void);
113 unsigned int n_bytes; /* number of bytes per ->data_read */
114};
115static struct rng_operations *rng_ops;
116
117static struct file_operations rng_chrdev_ops = {
118 .owner = THIS_MODULE,
119 .open = rng_dev_open,
120 .read = rng_dev_read,
121};
122
123
124static struct miscdevice rng_miscdev = {
125 RNG_MISCDEV_MINOR,
126 RNG_MODULE_NAME,
127 &rng_chrdev_ops,
128};
129
130enum {
131 rng_hw_none,
132 rng_hw_intel,
133 rng_hw_amd,
134 rng_hw_via,
Jordan Crousea7a4ad02006-01-06 00:12:15 -0800135 rng_hw_geode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138static struct rng_operations rng_vendor_ops[] = {
139 /* rng_hw_none */
140 { },
141
142 /* rng_hw_intel */
143 { intel_init, intel_cleanup, intel_data_present,
144 intel_data_read, 1 },
145
146 /* rng_hw_amd */
147 { amd_init, amd_cleanup, amd_data_present, amd_data_read, 4 },
148
149#ifdef __i386__
150 /* rng_hw_via */
151 { via_init, via_cleanup, via_data_present, via_data_read, 1 },
152#endif
Jordan Crousea7a4ad02006-01-06 00:12:15 -0800153
154 /* rng_hw_geode */
155 { geode_init, geode_cleanup, geode_data_present, geode_data_read, 4 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158/*
159 * Data for PCI driver interface
160 *
161 * This data only exists for exporting the supported
162 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
163 * register a pci_driver, because someone else might one day
164 * want to register another driver on the same PCI id.
165 */
166static struct pci_device_id rng_pci_tbl[] = {
167 { 0x1022, 0x7443, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
168 { 0x1022, 0x746b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
169
170 { 0x8086, 0x2418, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
171 { 0x8086, 0x2428, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
172 { 0x8086, 0x2448, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
173 { 0x8086, 0x244e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
174 { 0x8086, 0x245e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
175
Jordan Crousea7a4ad02006-01-06 00:12:15 -0800176 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LX_AES,
177 PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_geode },
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 { 0, }, /* terminate list */
180};
181MODULE_DEVICE_TABLE (pci, rng_pci_tbl);
182
183
184/***********************************************************************
185 *
186 * Intel RNG operations
187 *
188 */
189
190/*
191 * RNG registers (offsets from rng_mem)
192 */
193#define INTEL_RNG_HW_STATUS 0
194#define INTEL_RNG_PRESENT 0x40
195#define INTEL_RNG_ENABLED 0x01
196#define INTEL_RNG_STATUS 1
197#define INTEL_RNG_DATA_PRESENT 0x01
198#define INTEL_RNG_DATA 2
199
200/*
201 * Magic address at which Intel PCI bridges locate the RNG
202 */
203#define INTEL_RNG_ADDR 0xFFBC015F
204#define INTEL_RNG_ADDR_LEN 3
205
206/* token to our ioremap'd RNG register area */
207static void __iomem *rng_mem;
208
209static inline u8 intel_hwstatus (void)
210{
211 assert (rng_mem != NULL);
212 return readb (rng_mem + INTEL_RNG_HW_STATUS);
213}
214
215static inline u8 intel_hwstatus_set (u8 hw_status)
216{
217 assert (rng_mem != NULL);
218 writeb (hw_status, rng_mem + INTEL_RNG_HW_STATUS);
219 return intel_hwstatus ();
220}
221
222static unsigned int intel_data_present(void)
223{
224 assert (rng_mem != NULL);
225
226 return (readb (rng_mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT) ?
227 1 : 0;
228}
229
230static u32 intel_data_read(void)
231{
232 assert (rng_mem != NULL);
233
234 return readb (rng_mem + INTEL_RNG_DATA);
235}
236
237static int __init intel_init (struct pci_dev *dev)
238{
239 int rc;
240 u8 hw_status;
241
242 DPRINTK ("ENTER\n");
243
244 rng_mem = ioremap (INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
245 if (rng_mem == NULL) {
246 printk (KERN_ERR PFX "cannot ioremap RNG Memory\n");
247 rc = -EBUSY;
248 goto err_out;
249 }
250
251 /* Check for Intel 82802 */
252 hw_status = intel_hwstatus ();
253 if ((hw_status & INTEL_RNG_PRESENT) == 0) {
254 printk (KERN_ERR PFX "RNG not detected\n");
255 rc = -ENODEV;
256 goto err_out_free_map;
257 }
258
259 /* turn RNG h/w on, if it's off */
260 if ((hw_status & INTEL_RNG_ENABLED) == 0)
261 hw_status = intel_hwstatus_set (hw_status | INTEL_RNG_ENABLED);
262 if ((hw_status & INTEL_RNG_ENABLED) == 0) {
263 printk (KERN_ERR PFX "cannot enable RNG, aborting\n");
264 rc = -EIO;
265 goto err_out_free_map;
266 }
267
268 DPRINTK ("EXIT, returning 0\n");
269 return 0;
270
271err_out_free_map:
272 iounmap (rng_mem);
273 rng_mem = NULL;
274err_out:
275 DPRINTK ("EXIT, returning %d\n", rc);
276 return rc;
277}
278
279static void intel_cleanup(void)
280{
281 u8 hw_status;
282
283 hw_status = intel_hwstatus ();
284 if (hw_status & INTEL_RNG_ENABLED)
285 intel_hwstatus_set (hw_status & ~INTEL_RNG_ENABLED);
286 else
287 printk(KERN_WARNING PFX "unusual: RNG already disabled\n");
288 iounmap(rng_mem);
289 rng_mem = NULL;
290}
291
292/***********************************************************************
293 *
294 * AMD RNG operations
295 *
296 */
297
298static u32 pmbase; /* PMxx I/O base */
299static struct pci_dev *amd_dev;
300
301static unsigned int amd_data_present (void)
302{
303 return inl(pmbase + 0xF4) & 1;
304}
305
306
307static u32 amd_data_read (void)
308{
309 return inl(pmbase + 0xF0);
310}
311
312static int __init amd_init (struct pci_dev *dev)
313{
314 int rc;
315 u8 rnen;
316
317 DPRINTK ("ENTER\n");
318
319 pci_read_config_dword(dev, 0x58, &pmbase);
320
321 pmbase &= 0x0000FF00;
322
323 if (pmbase == 0)
324 {
325 printk (KERN_ERR PFX "power management base not set\n");
326 rc = -EIO;
327 goto err_out;
328 }
329
330 pci_read_config_byte(dev, 0x40, &rnen);
331 rnen |= (1 << 7); /* RNG on */
332 pci_write_config_byte(dev, 0x40, rnen);
333
334 pci_read_config_byte(dev, 0x41, &rnen);
335 rnen |= (1 << 7); /* PMIO enable */
336 pci_write_config_byte(dev, 0x41, rnen);
337
338 pr_info( PFX "AMD768 system management I/O registers at 0x%X.\n",
339 pmbase);
340
341 amd_dev = dev;
342
343 DPRINTK ("EXIT, returning 0\n");
344 return 0;
345
346err_out:
347 DPRINTK ("EXIT, returning %d\n", rc);
348 return rc;
349}
350
351static void amd_cleanup(void)
352{
353 u8 rnen;
354
355 pci_read_config_byte(amd_dev, 0x40, &rnen);
356 rnen &= ~(1 << 7); /* RNG off */
357 pci_write_config_byte(amd_dev, 0x40, rnen);
358
359 /* FIXME: twiddle pmio, also? */
360}
361
362#ifdef __i386__
363/***********************************************************************
364 *
365 * VIA RNG operations
366 *
367 */
368
369enum {
370 VIA_STRFILT_CNT_SHIFT = 16,
371 VIA_STRFILT_FAIL = (1 << 15),
372 VIA_STRFILT_ENABLE = (1 << 14),
373 VIA_RAWBITS_ENABLE = (1 << 13),
374 VIA_RNG_ENABLE = (1 << 6),
375 VIA_XSTORE_CNT_MASK = 0x0F,
376
377 VIA_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits */
378 VIA_RNG_CHUNK_4 = 0x01, /* 32 rand bits, 32 stored bits */
379 VIA_RNG_CHUNK_4_MASK = 0xFFFFFFFF,
380 VIA_RNG_CHUNK_2 = 0x02, /* 16 rand bits, 32 stored bits */
381 VIA_RNG_CHUNK_2_MASK = 0xFFFF,
382 VIA_RNG_CHUNK_1 = 0x03, /* 8 rand bits, 32 stored bits */
383 VIA_RNG_CHUNK_1_MASK = 0xFF,
384};
385
386static u32 via_rng_datum;
387
388/*
389 * Investigate using the 'rep' prefix to obtain 32 bits of random data
390 * in one insn. The upside is potentially better performance. The
391 * downside is that the instruction becomes no longer atomic. Due to
392 * this, just like familiar issues with /dev/random itself, the worst
393 * case of a 'rep xstore' could potentially pause a cpu for an
394 * unreasonably long time. In practice, this condition would likely
395 * only occur when the hardware is failing. (or so we hope :))
396 *
397 * Another possible performance boost may come from simply buffering
398 * until we have 4 bytes, thus returning a u32 at a time,
399 * instead of the current u8-at-a-time.
400 */
401
402static inline u32 xstore(u32 *addr, u32 edx_in)
403{
404 u32 eax_out;
405
406 asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"
407 :"=m"(*addr), "=a"(eax_out)
408 :"D"(addr), "d"(edx_in));
409
410 return eax_out;
411}
412
413static unsigned int via_data_present(void)
414{
415 u32 bytes_out;
416
417 /* We choose the recommended 1-byte-per-instruction RNG rate,
418 * for greater randomness at the expense of speed. Larger
419 * values 2, 4, or 8 bytes-per-instruction yield greater
420 * speed at lesser randomness.
421 *
422 * If you change this to another VIA_CHUNK_n, you must also
423 * change the ->n_bytes values in rng_vendor_ops[] tables.
424 * VIA_CHUNK_8 requires further code changes.
425 *
426 * A copy of MSR_VIA_RNG is placed in eax_out when xstore
427 * completes.
428 */
429 via_rng_datum = 0; /* paranoia, not really necessary */
430 bytes_out = xstore(&via_rng_datum, VIA_RNG_CHUNK_1) & VIA_XSTORE_CNT_MASK;
431 if (bytes_out == 0)
432 return 0;
433
434 return 1;
435}
436
437static u32 via_data_read(void)
438{
439 return via_rng_datum;
440}
441
442static int __init via_init(struct pci_dev *dev)
443{
444 u32 lo, hi, old_lo;
445
446 /* Control the RNG via MSR. Tread lightly and pay very close
447 * close attention to values written, as the reserved fields
448 * are documented to be "undefined and unpredictable"; but it
449 * does not say to write them as zero, so I make a guess that
450 * we restore the values we find in the register.
451 */
452 rdmsr(MSR_VIA_RNG, lo, hi);
453
454 old_lo = lo;
455 lo &= ~(0x7f << VIA_STRFILT_CNT_SHIFT);
456 lo &= ~VIA_XSTORE_CNT_MASK;
457 lo &= ~(VIA_STRFILT_ENABLE | VIA_STRFILT_FAIL | VIA_RAWBITS_ENABLE);
458 lo |= VIA_RNG_ENABLE;
459
460 if (lo != old_lo)
461 wrmsr(MSR_VIA_RNG, lo, hi);
462
463 /* perhaps-unnecessary sanity check; remove after testing if
464 unneeded */
465 rdmsr(MSR_VIA_RNG, lo, hi);
466 if ((lo & VIA_RNG_ENABLE) == 0) {
467 printk(KERN_ERR PFX "cannot enable VIA C3 RNG, aborting\n");
468 return -ENODEV;
469 }
470
471 return 0;
472}
473
474static void via_cleanup(void)
475{
476 /* do nothing */
477}
478#endif
479
Jordan Crousea7a4ad02006-01-06 00:12:15 -0800480/***********************************************************************
481 *
482 * AMD Geode RNG operations
483 *
484 */
485
486static void __iomem *geode_rng_base = NULL;
487
488#define GEODE_RNG_DATA_REG 0x50
489#define GEODE_RNG_STATUS_REG 0x54
490
491static u32 geode_data_read(void)
492{
493 u32 val;
494
495 assert(geode_rng_base != NULL);
496 val = readl(geode_rng_base + GEODE_RNG_DATA_REG);
497 return val;
498}
499
500static unsigned int geode_data_present(void)
501{
502 u32 val;
503
504 assert(geode_rng_base != NULL);
505 val = readl(geode_rng_base + GEODE_RNG_STATUS_REG);
506 return val;
507}
508
509static void geode_cleanup(void)
510{
511 iounmap(geode_rng_base);
512 geode_rng_base = NULL;
513}
514
515static int geode_init(struct pci_dev *dev)
516{
517 unsigned long rng_base = pci_resource_start(dev, 0);
518
519 if (rng_base == 0)
520 return 1;
521
522 geode_rng_base = ioremap(rng_base, 0x58);
523
524 if (geode_rng_base == NULL) {
525 printk(KERN_ERR PFX "Cannot ioremap RNG memory\n");
526 return -EBUSY;
527 }
528
529 return 0;
530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532/***********************************************************************
533 *
534 * /dev/hwrandom character device handling (major 10, minor 183)
535 *
536 */
537
538static int rng_dev_open (struct inode *inode, struct file *filp)
539{
540 /* enforce read-only access to this chrdev */
541 if ((filp->f_mode & FMODE_READ) == 0)
542 return -EINVAL;
543 if (filp->f_mode & FMODE_WRITE)
544 return -EINVAL;
545
546 return 0;
547}
548
549
550static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
551 loff_t * offp)
552{
553 static DEFINE_SPINLOCK(rng_lock);
554 unsigned int have_data;
555 u32 data = 0;
556 ssize_t ret = 0;
557
558 while (size) {
559 spin_lock(&rng_lock);
560
561 have_data = 0;
562 if (rng_ops->data_present()) {
563 data = rng_ops->data_read();
564 have_data = rng_ops->n_bytes;
565 }
566
567 spin_unlock (&rng_lock);
568
569 while (have_data && size) {
570 if (put_user((u8)data, buf++)) {
571 ret = ret ? : -EFAULT;
572 break;
573 }
574 size--;
575 ret++;
576 have_data--;
577 data>>=8;
578 }
579
580 if (filp->f_flags & O_NONBLOCK)
581 return ret ? : -EAGAIN;
582
583 if(need_resched())
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -0700584 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 else
586 udelay(200); /* FIXME: We could poll for 250uS ?? */
587
588 if (signal_pending (current))
589 return ret ? : -ERESTARTSYS;
590 }
591 return ret;
592}
593
594
595
596/*
597 * rng_init_one - look for and attempt to init a single RNG
598 */
599static int __init rng_init_one (struct pci_dev *dev)
600{
601 int rc;
602
603 DPRINTK ("ENTER\n");
604
605 assert(rng_ops != NULL);
606
607 rc = rng_ops->init(dev);
608 if (rc)
609 goto err_out;
610
611 rc = misc_register (&rng_miscdev);
612 if (rc) {
613 printk (KERN_ERR PFX "misc device register failed\n");
614 goto err_out_cleanup_hw;
615 }
616
617 DPRINTK ("EXIT, returning 0\n");
618 return 0;
619
620err_out_cleanup_hw:
621 rng_ops->cleanup();
622err_out:
623 DPRINTK ("EXIT, returning %d\n", rc);
624 return rc;
625}
626
627
628
629MODULE_AUTHOR("The Linux Kernel team");
630MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
631MODULE_LICENSE("GPL");
632
633
634/*
635 * rng_init - initialize RNG module
636 */
637static int __init rng_init (void)
638{
639 int rc;
640 struct pci_dev *pdev = NULL;
641 const struct pci_device_id *ent;
642
643 DPRINTK ("ENTER\n");
644
Jordan Crousea7a4ad02006-01-06 00:12:15 -0800645 /* Probe for Intel, AMD, Geode RNGs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 for_each_pci_dev(pdev) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700647 ent = pci_match_id(rng_pci_tbl, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (ent) {
649 rng_ops = &rng_vendor_ops[ent->driver_data];
650 goto match;
651 }
652 }
653
654#ifdef __i386__
655 /* Probe for VIA RNG */
656 if (cpu_has_xstore) {
657 rng_ops = &rng_vendor_ops[rng_hw_via];
658 pdev = NULL;
659 goto match;
660 }
661#endif
662
663 DPRINTK ("EXIT, returning -ENODEV\n");
664 return -ENODEV;
665
666match:
667 rc = rng_init_one (pdev);
668 if (rc)
669 return rc;
670
671 pr_info( RNG_DRIVER_NAME " loaded\n");
672
673 DPRINTK ("EXIT, returning 0\n");
674 return 0;
675}
676
677
678/*
679 * rng_init - shutdown RNG module
680 */
681static void __exit rng_cleanup (void)
682{
683 DPRINTK ("ENTER\n");
684
685 misc_deregister (&rng_miscdev);
686
687 if (rng_ops->cleanup)
688 rng_ops->cleanup();
689
690 DPRINTK ("EXIT\n");
691}
692
693
694module_init (rng_init);
695module_exit (rng_cleanup);