via-rng.c 6.0 KB

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