rng.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2013, Michael Ellerman, IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) "powernv-rng: " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/slab.h>
  15. #include <linux/smp.h>
  16. #include <asm/archrandom.h>
  17. #include <asm/cputable.h>
  18. #include <asm/io.h>
  19. #include <asm/prom.h>
  20. #include <asm/machdep.h>
  21. #include <asm/smp.h>
  22. #define DARN_ERR 0xFFFFFFFFFFFFFFFFul
  23. struct powernv_rng {
  24. void __iomem *regs;
  25. void __iomem *regs_real;
  26. unsigned long mask;
  27. };
  28. static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
  29. int powernv_hwrng_present(void)
  30. {
  31. struct powernv_rng *rng;
  32. rng = get_cpu_var(powernv_rng);
  33. put_cpu_var(rng);
  34. return rng != NULL;
  35. }
  36. static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
  37. {
  38. unsigned long parity;
  39. /* Calculate the parity of the value */
  40. asm ("popcntd %0,%1" : "=r" (parity) : "r" (val));
  41. /* xor our value with the previous mask */
  42. val ^= rng->mask;
  43. /* update the mask based on the parity of this value */
  44. rng->mask = (rng->mask << 1) | (parity & 1);
  45. return val;
  46. }
  47. int powernv_get_random_real_mode(unsigned long *v)
  48. {
  49. struct powernv_rng *rng;
  50. rng = raw_cpu_read(powernv_rng);
  51. *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
  52. return 1;
  53. }
  54. int powernv_get_random_darn(unsigned long *v)
  55. {
  56. unsigned long val;
  57. /* Using DARN with L=1 - 64-bit conditioned random number */
  58. asm volatile(PPC_DARN(%0, 1) : "=r"(val));
  59. if (val == DARN_ERR)
  60. return 0;
  61. *v = val;
  62. return 1;
  63. }
  64. static int initialise_darn(void)
  65. {
  66. unsigned long val;
  67. int i;
  68. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  69. return -ENODEV;
  70. for (i = 0; i < 10; i++) {
  71. if (powernv_get_random_darn(&val)) {
  72. ppc_md.get_random_seed = powernv_get_random_darn;
  73. return 0;
  74. }
  75. }
  76. pr_warn("Unable to use DARN for get_random_seed()\n");
  77. return -EIO;
  78. }
  79. int powernv_get_random_long(unsigned long *v)
  80. {
  81. struct powernv_rng *rng;
  82. rng = get_cpu_var(powernv_rng);
  83. *v = rng_whiten(rng, in_be64(rng->regs));
  84. put_cpu_var(rng);
  85. return 1;
  86. }
  87. EXPORT_SYMBOL_GPL(powernv_get_random_long);
  88. static __init void rng_init_per_cpu(struct powernv_rng *rng,
  89. struct device_node *dn)
  90. {
  91. int chip_id, cpu;
  92. chip_id = of_get_ibm_chip_id(dn);
  93. if (chip_id == -1)
  94. pr_warn("No ibm,chip-id found for %pOF.\n", dn);
  95. for_each_possible_cpu(cpu) {
  96. if (per_cpu(powernv_rng, cpu) == NULL ||
  97. cpu_to_chip_id(cpu) == chip_id) {
  98. per_cpu(powernv_rng, cpu) = rng;
  99. }
  100. }
  101. }
  102. static __init int rng_create(struct device_node *dn)
  103. {
  104. struct powernv_rng *rng;
  105. struct resource res;
  106. unsigned long val;
  107. rng = kzalloc(sizeof(*rng), GFP_KERNEL);
  108. if (!rng)
  109. return -ENOMEM;
  110. if (of_address_to_resource(dn, 0, &res)) {
  111. kfree(rng);
  112. return -ENXIO;
  113. }
  114. rng->regs_real = (void __iomem *)res.start;
  115. rng->regs = of_iomap(dn, 0);
  116. if (!rng->regs) {
  117. kfree(rng);
  118. return -ENXIO;
  119. }
  120. val = in_be64(rng->regs);
  121. rng->mask = val;
  122. rng_init_per_cpu(rng, dn);
  123. pr_info_once("Registering arch random hook.\n");
  124. ppc_md.get_random_seed = powernv_get_random_long;
  125. return 0;
  126. }
  127. static __init int rng_init(void)
  128. {
  129. struct device_node *dn;
  130. int rc;
  131. for_each_compatible_node(dn, NULL, "ibm,power-rng") {
  132. rc = rng_create(dn);
  133. if (rc) {
  134. pr_err("Failed creating rng for %pOF (%d).\n",
  135. dn, rc);
  136. continue;
  137. }
  138. /* Create devices for hwrng driver */
  139. of_platform_device_create(dn, NULL, NULL);
  140. }
  141. initialise_darn();
  142. return 0;
  143. }
  144. machine_subsys_initcall(powernv, rng_init);