hw_random.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
  3. (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
  4. derived from
  5. Hardware driver for the AMD 768 Random Number Generator (RNG)
  6. (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
  7. derived from
  8. Hardware driver for Intel i810 Random Number Generator (RNG)
  9. Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
  10. Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
  11. Please read Documentation/hw_random.txt for details on use.
  12. ----------------------------------------------------------
  13. This software may be used and distributed according to the terms
  14. of the GNU General Public License, incorporated herein by reference.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/fs.h>
  19. #include <linux/init.h>
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/random.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/mm.h>
  27. #include <linux/delay.h>
  28. #ifdef __i386__
  29. #include <asm/msr.h>
  30. #include <asm/cpufeature.h>
  31. #endif
  32. #include <asm/io.h>
  33. #include <asm/uaccess.h>
  34. /*
  35. * core module and version information
  36. */
  37. #define RNG_VERSION "1.0.0"
  38. #define RNG_MODULE_NAME "hw_random"
  39. #define RNG_DRIVER_NAME RNG_MODULE_NAME " hardware driver " RNG_VERSION
  40. #define PFX RNG_MODULE_NAME ": "
  41. /*
  42. * debugging macros
  43. */
  44. /* pr_debug() collapses to a no-op if DEBUG is not defined */
  45. #define DPRINTK(fmt, args...) pr_debug(PFX "%s: " fmt, __FUNCTION__ , ## args)
  46. #undef RNG_NDEBUG /* define to enable lightweight runtime checks */
  47. #ifdef RNG_NDEBUG
  48. #define assert(expr) \
  49. if(!(expr)) { \
  50. printk(KERN_DEBUG PFX "Assertion failed! %s,%s,%s," \
  51. "line=%d\n", #expr, __FILE__, __FUNCTION__, __LINE__); \
  52. }
  53. #else
  54. #define assert(expr)
  55. #endif
  56. #define RNG_MISCDEV_MINOR 183 /* official */
  57. static int rng_dev_open (struct inode *inode, struct file *filp);
  58. static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
  59. loff_t * offp);
  60. static int __init intel_init (struct pci_dev *dev);
  61. static void intel_cleanup(void);
  62. static unsigned int intel_data_present (void);
  63. static u32 intel_data_read (void);
  64. static int __init amd_init (struct pci_dev *dev);
  65. static void amd_cleanup(void);
  66. static unsigned int amd_data_present (void);
  67. static u32 amd_data_read (void);
  68. #ifdef __i386__
  69. static int __init via_init(struct pci_dev *dev);
  70. static void via_cleanup(void);
  71. static unsigned int via_data_present (void);
  72. static u32 via_data_read (void);
  73. #endif
  74. struct rng_operations {
  75. int (*init) (struct pci_dev *dev);
  76. void (*cleanup) (void);
  77. unsigned int (*data_present) (void);
  78. u32 (*data_read) (void);
  79. unsigned int n_bytes; /* number of bytes per ->data_read */
  80. };
  81. static struct rng_operations *rng_ops;
  82. static struct file_operations rng_chrdev_ops = {
  83. .owner = THIS_MODULE,
  84. .open = rng_dev_open,
  85. .read = rng_dev_read,
  86. };
  87. static struct miscdevice rng_miscdev = {
  88. RNG_MISCDEV_MINOR,
  89. RNG_MODULE_NAME,
  90. &rng_chrdev_ops,
  91. };
  92. enum {
  93. rng_hw_none,
  94. rng_hw_intel,
  95. rng_hw_amd,
  96. rng_hw_via,
  97. };
  98. static struct rng_operations rng_vendor_ops[] = {
  99. /* rng_hw_none */
  100. { },
  101. /* rng_hw_intel */
  102. { intel_init, intel_cleanup, intel_data_present,
  103. intel_data_read, 1 },
  104. /* rng_hw_amd */
  105. { amd_init, amd_cleanup, amd_data_present, amd_data_read, 4 },
  106. #ifdef __i386__
  107. /* rng_hw_via */
  108. { via_init, via_cleanup, via_data_present, via_data_read, 1 },
  109. #endif
  110. };
  111. /*
  112. * Data for PCI driver interface
  113. *
  114. * This data only exists for exporting the supported
  115. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  116. * register a pci_driver, because someone else might one day
  117. * want to register another driver on the same PCI id.
  118. */
  119. static struct pci_device_id rng_pci_tbl[] = {
  120. { 0x1022, 0x7443, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
  121. { 0x1022, 0x746b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_amd },
  122. { 0x8086, 0x2418, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
  123. { 0x8086, 0x2428, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
  124. { 0x8086, 0x2448, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
  125. { 0x8086, 0x244e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
  126. { 0x8086, 0x245e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, rng_hw_intel },
  127. { 0, }, /* terminate list */
  128. };
  129. MODULE_DEVICE_TABLE (pci, rng_pci_tbl);
  130. /***********************************************************************
  131. *
  132. * Intel RNG operations
  133. *
  134. */
  135. /*
  136. * RNG registers (offsets from rng_mem)
  137. */
  138. #define INTEL_RNG_HW_STATUS 0
  139. #define INTEL_RNG_PRESENT 0x40
  140. #define INTEL_RNG_ENABLED 0x01
  141. #define INTEL_RNG_STATUS 1
  142. #define INTEL_RNG_DATA_PRESENT 0x01
  143. #define INTEL_RNG_DATA 2
  144. /*
  145. * Magic address at which Intel PCI bridges locate the RNG
  146. */
  147. #define INTEL_RNG_ADDR 0xFFBC015F
  148. #define INTEL_RNG_ADDR_LEN 3
  149. /* token to our ioremap'd RNG register area */
  150. static void __iomem *rng_mem;
  151. static inline u8 intel_hwstatus (void)
  152. {
  153. assert (rng_mem != NULL);
  154. return readb (rng_mem + INTEL_RNG_HW_STATUS);
  155. }
  156. static inline u8 intel_hwstatus_set (u8 hw_status)
  157. {
  158. assert (rng_mem != NULL);
  159. writeb (hw_status, rng_mem + INTEL_RNG_HW_STATUS);
  160. return intel_hwstatus ();
  161. }
  162. static unsigned int intel_data_present(void)
  163. {
  164. assert (rng_mem != NULL);
  165. return (readb (rng_mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT) ?
  166. 1 : 0;
  167. }
  168. static u32 intel_data_read(void)
  169. {
  170. assert (rng_mem != NULL);
  171. return readb (rng_mem + INTEL_RNG_DATA);
  172. }
  173. static int __init intel_init (struct pci_dev *dev)
  174. {
  175. int rc;
  176. u8 hw_status;
  177. DPRINTK ("ENTER\n");
  178. rng_mem = ioremap (INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
  179. if (rng_mem == NULL) {
  180. printk (KERN_ERR PFX "cannot ioremap RNG Memory\n");
  181. rc = -EBUSY;
  182. goto err_out;
  183. }
  184. /* Check for Intel 82802 */
  185. hw_status = intel_hwstatus ();
  186. if ((hw_status & INTEL_RNG_PRESENT) == 0) {
  187. printk (KERN_ERR PFX "RNG not detected\n");
  188. rc = -ENODEV;
  189. goto err_out_free_map;
  190. }
  191. /* turn RNG h/w on, if it's off */
  192. if ((hw_status & INTEL_RNG_ENABLED) == 0)
  193. hw_status = intel_hwstatus_set (hw_status | INTEL_RNG_ENABLED);
  194. if ((hw_status & INTEL_RNG_ENABLED) == 0) {
  195. printk (KERN_ERR PFX "cannot enable RNG, aborting\n");
  196. rc = -EIO;
  197. goto err_out_free_map;
  198. }
  199. DPRINTK ("EXIT, returning 0\n");
  200. return 0;
  201. err_out_free_map:
  202. iounmap (rng_mem);
  203. rng_mem = NULL;
  204. err_out:
  205. DPRINTK ("EXIT, returning %d\n", rc);
  206. return rc;
  207. }
  208. static void intel_cleanup(void)
  209. {
  210. u8 hw_status;
  211. hw_status = intel_hwstatus ();
  212. if (hw_status & INTEL_RNG_ENABLED)
  213. intel_hwstatus_set (hw_status & ~INTEL_RNG_ENABLED);
  214. else
  215. printk(KERN_WARNING PFX "unusual: RNG already disabled\n");
  216. iounmap(rng_mem);
  217. rng_mem = NULL;
  218. }
  219. /***********************************************************************
  220. *
  221. * AMD RNG operations
  222. *
  223. */
  224. static u32 pmbase; /* PMxx I/O base */
  225. static struct pci_dev *amd_dev;
  226. static unsigned int amd_data_present (void)
  227. {
  228. return inl(pmbase + 0xF4) & 1;
  229. }
  230. static u32 amd_data_read (void)
  231. {
  232. return inl(pmbase + 0xF0);
  233. }
  234. static int __init amd_init (struct pci_dev *dev)
  235. {
  236. int rc;
  237. u8 rnen;
  238. DPRINTK ("ENTER\n");
  239. pci_read_config_dword(dev, 0x58, &pmbase);
  240. pmbase &= 0x0000FF00;
  241. if (pmbase == 0)
  242. {
  243. printk (KERN_ERR PFX "power management base not set\n");
  244. rc = -EIO;
  245. goto err_out;
  246. }
  247. pci_read_config_byte(dev, 0x40, &rnen);
  248. rnen |= (1 << 7); /* RNG on */
  249. pci_write_config_byte(dev, 0x40, rnen);
  250. pci_read_config_byte(dev, 0x41, &rnen);
  251. rnen |= (1 << 7); /* PMIO enable */
  252. pci_write_config_byte(dev, 0x41, rnen);
  253. pr_info( PFX "AMD768 system management I/O registers at 0x%X.\n",
  254. pmbase);
  255. amd_dev = dev;
  256. DPRINTK ("EXIT, returning 0\n");
  257. return 0;
  258. err_out:
  259. DPRINTK ("EXIT, returning %d\n", rc);
  260. return rc;
  261. }
  262. static void amd_cleanup(void)
  263. {
  264. u8 rnen;
  265. pci_read_config_byte(amd_dev, 0x40, &rnen);
  266. rnen &= ~(1 << 7); /* RNG off */
  267. pci_write_config_byte(amd_dev, 0x40, rnen);
  268. /* FIXME: twiddle pmio, also? */
  269. }
  270. #ifdef __i386__
  271. /***********************************************************************
  272. *
  273. * VIA RNG operations
  274. *
  275. */
  276. enum {
  277. VIA_STRFILT_CNT_SHIFT = 16,
  278. VIA_STRFILT_FAIL = (1 << 15),
  279. VIA_STRFILT_ENABLE = (1 << 14),
  280. VIA_RAWBITS_ENABLE = (1 << 13),
  281. VIA_RNG_ENABLE = (1 << 6),
  282. VIA_XSTORE_CNT_MASK = 0x0F,
  283. VIA_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits */
  284. VIA_RNG_CHUNK_4 = 0x01, /* 32 rand bits, 32 stored bits */
  285. VIA_RNG_CHUNK_4_MASK = 0xFFFFFFFF,
  286. VIA_RNG_CHUNK_2 = 0x02, /* 16 rand bits, 32 stored bits */
  287. VIA_RNG_CHUNK_2_MASK = 0xFFFF,
  288. VIA_RNG_CHUNK_1 = 0x03, /* 8 rand bits, 32 stored bits */
  289. VIA_RNG_CHUNK_1_MASK = 0xFF,
  290. };
  291. static u32 via_rng_datum;
  292. /*
  293. * Investigate using the 'rep' prefix to obtain 32 bits of random data
  294. * in one insn. The upside is potentially better performance. The
  295. * downside is that the instruction becomes no longer atomic. Due to
  296. * this, just like familiar issues with /dev/random itself, the worst
  297. * case of a 'rep xstore' could potentially pause a cpu for an
  298. * unreasonably long time. In practice, this condition would likely
  299. * only occur when the hardware is failing. (or so we hope :))
  300. *
  301. * Another possible performance boost may come from simply buffering
  302. * until we have 4 bytes, thus returning a u32 at a time,
  303. * instead of the current u8-at-a-time.
  304. */
  305. static inline u32 xstore(u32 *addr, u32 edx_in)
  306. {
  307. u32 eax_out;
  308. asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"
  309. :"=m"(*addr), "=a"(eax_out)
  310. :"D"(addr), "d"(edx_in));
  311. return eax_out;
  312. }
  313. static unsigned int via_data_present(void)
  314. {
  315. u32 bytes_out;
  316. /* We choose the recommended 1-byte-per-instruction RNG rate,
  317. * for greater randomness at the expense of speed. Larger
  318. * values 2, 4, or 8 bytes-per-instruction yield greater
  319. * speed at lesser randomness.
  320. *
  321. * If you change this to another VIA_CHUNK_n, you must also
  322. * change the ->n_bytes values in rng_vendor_ops[] tables.
  323. * VIA_CHUNK_8 requires further code changes.
  324. *
  325. * A copy of MSR_VIA_RNG is placed in eax_out when xstore
  326. * completes.
  327. */
  328. via_rng_datum = 0; /* paranoia, not really necessary */
  329. bytes_out = xstore(&via_rng_datum, VIA_RNG_CHUNK_1) & VIA_XSTORE_CNT_MASK;
  330. if (bytes_out == 0)
  331. return 0;
  332. return 1;
  333. }
  334. static u32 via_data_read(void)
  335. {
  336. return via_rng_datum;
  337. }
  338. static int __init via_init(struct pci_dev *dev)
  339. {
  340. u32 lo, hi, old_lo;
  341. /* Control the RNG via MSR. Tread lightly and pay very close
  342. * close attention to values written, as the reserved fields
  343. * are documented to be "undefined and unpredictable"; but it
  344. * does not say to write them as zero, so I make a guess that
  345. * we restore the values we find in the register.
  346. */
  347. rdmsr(MSR_VIA_RNG, lo, hi);
  348. old_lo = lo;
  349. lo &= ~(0x7f << VIA_STRFILT_CNT_SHIFT);
  350. lo &= ~VIA_XSTORE_CNT_MASK;
  351. lo &= ~(VIA_STRFILT_ENABLE | VIA_STRFILT_FAIL | VIA_RAWBITS_ENABLE);
  352. lo |= VIA_RNG_ENABLE;
  353. if (lo != old_lo)
  354. wrmsr(MSR_VIA_RNG, lo, hi);
  355. /* perhaps-unnecessary sanity check; remove after testing if
  356. unneeded */
  357. rdmsr(MSR_VIA_RNG, lo, hi);
  358. if ((lo & VIA_RNG_ENABLE) == 0) {
  359. printk(KERN_ERR PFX "cannot enable VIA C3 RNG, aborting\n");
  360. return -ENODEV;
  361. }
  362. return 0;
  363. }
  364. static void via_cleanup(void)
  365. {
  366. /* do nothing */
  367. }
  368. #endif
  369. /***********************************************************************
  370. *
  371. * /dev/hwrandom character device handling (major 10, minor 183)
  372. *
  373. */
  374. static int rng_dev_open (struct inode *inode, struct file *filp)
  375. {
  376. /* enforce read-only access to this chrdev */
  377. if ((filp->f_mode & FMODE_READ) == 0)
  378. return -EINVAL;
  379. if (filp->f_mode & FMODE_WRITE)
  380. return -EINVAL;
  381. return 0;
  382. }
  383. static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
  384. loff_t * offp)
  385. {
  386. static DEFINE_SPINLOCK(rng_lock);
  387. unsigned int have_data;
  388. u32 data = 0;
  389. ssize_t ret = 0;
  390. while (size) {
  391. spin_lock(&rng_lock);
  392. have_data = 0;
  393. if (rng_ops->data_present()) {
  394. data = rng_ops->data_read();
  395. have_data = rng_ops->n_bytes;
  396. }
  397. spin_unlock (&rng_lock);
  398. while (have_data && size) {
  399. if (put_user((u8)data, buf++)) {
  400. ret = ret ? : -EFAULT;
  401. break;
  402. }
  403. size--;
  404. ret++;
  405. have_data--;
  406. data>>=8;
  407. }
  408. if (filp->f_flags & O_NONBLOCK)
  409. return ret ? : -EAGAIN;
  410. if(need_resched())
  411. schedule_timeout_interruptible(1);
  412. else
  413. udelay(200); /* FIXME: We could poll for 250uS ?? */
  414. if (signal_pending (current))
  415. return ret ? : -ERESTARTSYS;
  416. }
  417. return ret;
  418. }
  419. /*
  420. * rng_init_one - look for and attempt to init a single RNG
  421. */
  422. static int __init rng_init_one (struct pci_dev *dev)
  423. {
  424. int rc;
  425. DPRINTK ("ENTER\n");
  426. assert(rng_ops != NULL);
  427. rc = rng_ops->init(dev);
  428. if (rc)
  429. goto err_out;
  430. rc = misc_register (&rng_miscdev);
  431. if (rc) {
  432. printk (KERN_ERR PFX "misc device register failed\n");
  433. goto err_out_cleanup_hw;
  434. }
  435. DPRINTK ("EXIT, returning 0\n");
  436. return 0;
  437. err_out_cleanup_hw:
  438. rng_ops->cleanup();
  439. err_out:
  440. DPRINTK ("EXIT, returning %d\n", rc);
  441. return rc;
  442. }
  443. MODULE_AUTHOR("The Linux Kernel team");
  444. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
  445. MODULE_LICENSE("GPL");
  446. /*
  447. * rng_init - initialize RNG module
  448. */
  449. static int __init rng_init (void)
  450. {
  451. int rc;
  452. struct pci_dev *pdev = NULL;
  453. const struct pci_device_id *ent;
  454. DPRINTK ("ENTER\n");
  455. /* Probe for Intel, AMD RNGs */
  456. for_each_pci_dev(pdev) {
  457. ent = pci_match_id(rng_pci_tbl, pdev);
  458. if (ent) {
  459. rng_ops = &rng_vendor_ops[ent->driver_data];
  460. goto match;
  461. }
  462. }
  463. #ifdef __i386__
  464. /* Probe for VIA RNG */
  465. if (cpu_has_xstore) {
  466. rng_ops = &rng_vendor_ops[rng_hw_via];
  467. pdev = NULL;
  468. goto match;
  469. }
  470. #endif
  471. DPRINTK ("EXIT, returning -ENODEV\n");
  472. return -ENODEV;
  473. match:
  474. rc = rng_init_one (pdev);
  475. if (rc)
  476. return rc;
  477. pr_info( RNG_DRIVER_NAME " loaded\n");
  478. DPRINTK ("EXIT, returning 0\n");
  479. return 0;
  480. }
  481. /*
  482. * rng_init - shutdown RNG module
  483. */
  484. static void __exit rng_cleanup (void)
  485. {
  486. DPRINTK ("ENTER\n");
  487. misc_deregister (&rng_miscdev);
  488. if (rng_ops->cleanup)
  489. rng_ops->cleanup();
  490. DPRINTK ("EXIT\n");
  491. }
  492. module_init (rng_init);
  493. module_exit (rng_cleanup);