hw_random.c 15 KB

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