via-rng.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * RNG driver for VIA RNGs
  3. *
  4. * Copyright 2005 (c) MontaVista Software, Inc.
  5. *
  6. * with the majority of the code coming from:
  7. *
  8. * Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
  9. * (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
  10. *
  11. * derived from
  12. *
  13. * Hardware driver for the AMD 768 Random Number Generator (RNG)
  14. * (c) Copyright 2001 Red Hat Inc
  15. *
  16. * derived from
  17. *
  18. * Hardware driver for Intel i810 Random Number Generator (RNG)
  19. * Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
  20. * Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
  21. *
  22. * This file is licensed under the terms of the GNU General Public
  23. * License version 2. This program is licensed "as is" without any
  24. * warranty of any kind, whether express or implied.
  25. */
  26. #include <crypto/padlock.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/hw_random.h>
  30. #include <linux/delay.h>
  31. #include <asm/io.h>
  32. #include <asm/msr.h>
  33. #include <asm/cpufeature.h>
  34. #include <asm/i387.h>
  35. enum {
  36. VIA_STRFILT_CNT_SHIFT = 16,
  37. VIA_STRFILT_FAIL = (1 << 15),
  38. VIA_STRFILT_ENABLE = (1 << 14),
  39. VIA_RAWBITS_ENABLE = (1 << 13),
  40. VIA_RNG_ENABLE = (1 << 6),
  41. VIA_NOISESRC1 = (1 << 8),
  42. VIA_NOISESRC2 = (1 << 9),
  43. VIA_XSTORE_CNT_MASK = 0x0F,
  44. VIA_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits */
  45. VIA_RNG_CHUNK_4 = 0x01, /* 32 rand bits, 32 stored bits */
  46. VIA_RNG_CHUNK_4_MASK = 0xFFFFFFFF,
  47. VIA_RNG_CHUNK_2 = 0x02, /* 16 rand bits, 32 stored bits */
  48. VIA_RNG_CHUNK_2_MASK = 0xFFFF,
  49. VIA_RNG_CHUNK_1 = 0x03, /* 8 rand bits, 32 stored bits */
  50. VIA_RNG_CHUNK_1_MASK = 0xFF,
  51. };
  52. /*
  53. * Investigate using the 'rep' prefix to obtain 32 bits of random data
  54. * in one insn. The upside is potentially better performance. The
  55. * downside is that the instruction becomes no longer atomic. Due to
  56. * this, just like familiar issues with /dev/random itself, the worst
  57. * case of a 'rep xstore' could potentially pause a cpu for an
  58. * unreasonably long time. In practice, this condition would likely
  59. * only occur when the hardware is failing. (or so we hope :))
  60. *
  61. * Another possible performance boost may come from simply buffering
  62. * until we have 4 bytes, thus returning a u32 at a time,
  63. * instead of the current u8-at-a-time.
  64. *
  65. * Padlock instructions can generate a spurious DNA fault, so
  66. * we have to call them in the context of irq_ts_save/restore()
  67. */
  68. static inline u32 xstore(u32 *addr, u32 edx_in)
  69. {
  70. u32 eax_out;
  71. int ts_state;
  72. ts_state = irq_ts_save();
  73. asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"
  74. : "=m" (*addr), "=a" (eax_out), "+d" (edx_in), "+D" (addr));
  75. irq_ts_restore(ts_state);
  76. return eax_out;
  77. }
  78. static int via_rng_data_present(struct hwrng *rng, int wait)
  79. {
  80. char buf[16 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
  81. ((aligned(STACK_ALIGN)));
  82. u32 *via_rng_datum = (u32 *)PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  83. u32 bytes_out;
  84. int i;
  85. /* We choose the recommended 1-byte-per-instruction RNG rate,
  86. * for greater randomness at the expense of speed. Larger
  87. * values 2, 4, or 8 bytes-per-instruction yield greater
  88. * speed at lesser randomness.
  89. *
  90. * If you change this to another VIA_CHUNK_n, you must also
  91. * change the ->n_bytes values in rng_vendor_ops[] tables.
  92. * VIA_CHUNK_8 requires further code changes.
  93. *
  94. * A copy of MSR_VIA_RNG is placed in eax_out when xstore
  95. * completes.
  96. */
  97. for (i = 0; i < 20; i++) {
  98. *via_rng_datum = 0; /* paranoia, not really necessary */
  99. bytes_out = xstore(via_rng_datum, VIA_RNG_CHUNK_1);
  100. bytes_out &= VIA_XSTORE_CNT_MASK;
  101. if (bytes_out || !wait)
  102. break;
  103. udelay(10);
  104. }
  105. rng->priv = *via_rng_datum;
  106. return bytes_out ? 1 : 0;
  107. }
  108. static int via_rng_data_read(struct hwrng *rng, u32 *data)
  109. {
  110. u32 via_rng_datum = (u32)rng->priv;
  111. *data = via_rng_datum;
  112. return 1;
  113. }
  114. static int via_rng_init(struct hwrng *rng)
  115. {
  116. struct cpuinfo_x86 *c = &cpu_data(0);
  117. u32 lo, hi, old_lo;
  118. /* VIA Nano CPUs don't have the MSR_VIA_RNG anymore. The RNG
  119. * is always enabled if CPUID rng_en is set. There is no
  120. * RNG configuration like it used to be the case in this
  121. * register */
  122. if ((c->x86 == 6) && (c->x86_model >= 0x0f)) {
  123. if (!cpu_has_xstore_enabled) {
  124. printk(KERN_ERR PFX "can't enable hardware RNG "
  125. "if XSTORE is not enabled\n");
  126. return -ENODEV;
  127. }
  128. return 0;
  129. }
  130. /* Control the RNG via MSR. Tread lightly and pay very close
  131. * close attention to values written, as the reserved fields
  132. * are documented to be "undefined and unpredictable"; but it
  133. * does not say to write them as zero, so I make a guess that
  134. * we restore the values we find in the register.
  135. */
  136. rdmsr(MSR_VIA_RNG, lo, hi);
  137. old_lo = lo;
  138. lo &= ~(0x7f << VIA_STRFILT_CNT_SHIFT);
  139. lo &= ~VIA_XSTORE_CNT_MASK;
  140. lo &= ~(VIA_STRFILT_ENABLE | VIA_STRFILT_FAIL | VIA_RAWBITS_ENABLE);
  141. lo |= VIA_RNG_ENABLE;
  142. lo |= VIA_NOISESRC1;
  143. /* Enable secondary noise source on CPUs where it is present. */
  144. /* Nehemiah stepping 8 and higher */
  145. if ((c->x86_model == 9) && (c->x86_mask > 7))
  146. lo |= VIA_NOISESRC2;
  147. /* Esther */
  148. if (c->x86_model >= 10)
  149. lo |= VIA_NOISESRC2;
  150. if (lo != old_lo)
  151. wrmsr(MSR_VIA_RNG, lo, hi);
  152. /* perhaps-unnecessary sanity check; remove after testing if
  153. unneeded */
  154. rdmsr(MSR_VIA_RNG, lo, hi);
  155. if ((lo & VIA_RNG_ENABLE) == 0) {
  156. printk(KERN_ERR PFX "cannot enable VIA C3 RNG, aborting\n");
  157. return -ENODEV;
  158. }
  159. return 0;
  160. }
  161. static struct hwrng via_rng = {
  162. .name = "via",
  163. .init = via_rng_init,
  164. .data_present = via_rng_data_present,
  165. .data_read = via_rng_data_read,
  166. };
  167. static int __init mod_init(void)
  168. {
  169. int err;
  170. if (!cpu_has_xstore)
  171. return -ENODEV;
  172. printk(KERN_INFO "VIA RNG detected\n");
  173. err = hwrng_register(&via_rng);
  174. if (err) {
  175. printk(KERN_ERR PFX "RNG registering failed (%d)\n",
  176. err);
  177. goto out;
  178. }
  179. out:
  180. return err;
  181. }
  182. static void __exit mod_exit(void)
  183. {
  184. hwrng_unregister(&via_rng);
  185. }
  186. module_init(mod_init);
  187. module_exit(mod_exit);
  188. MODULE_DESCRIPTION("H/W RNG driver for VIA CPU with PadLock");
  189. MODULE_LICENSE("GPL");