hw_random.c 15 KB

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