hw_random.c 15 KB

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