rng.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/io.h>
  18. #include <asm/prom.h>
  19. #include <asm/machdep.h>
  20. #include <asm/smp.h>
  21. struct powernv_rng {
  22. void __iomem *regs;
  23. void __iomem *regs_real;
  24. unsigned long mask;
  25. };
  26. static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
  27. int powernv_hwrng_present(void)
  28. {
  29. struct powernv_rng *rng;
  30. rng = get_cpu_var(powernv_rng);
  31. put_cpu_var(rng);
  32. return rng != NULL;
  33. }
  34. static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
  35. {
  36. unsigned long parity;
  37. /* Calculate the parity of the value */
  38. asm ("popcntd %0,%1" : "=r" (parity) : "r" (val));
  39. /* xor our value with the previous mask */
  40. val ^= rng->mask;
  41. /* update the mask based on the parity of this value */
  42. rng->mask = (rng->mask << 1) | (parity & 1);
  43. return val;
  44. }
  45. int powernv_get_random_real_mode(unsigned long *v)
  46. {
  47. struct powernv_rng *rng;
  48. rng = raw_cpu_read(powernv_rng);
  49. *v = rng_whiten(rng, in_rm64(rng->regs_real));
  50. return 1;
  51. }
  52. int powernv_get_random_long(unsigned long *v)
  53. {
  54. struct powernv_rng *rng;
  55. rng = get_cpu_var(powernv_rng);
  56. *v = rng_whiten(rng, in_be64(rng->regs));
  57. put_cpu_var(rng);
  58. return 1;
  59. }
  60. EXPORT_SYMBOL_GPL(powernv_get_random_long);
  61. static __init void rng_init_per_cpu(struct powernv_rng *rng,
  62. struct device_node *dn)
  63. {
  64. int chip_id, cpu;
  65. chip_id = of_get_ibm_chip_id(dn);
  66. if (chip_id == -1)
  67. pr_warn("No ibm,chip-id found for %s.\n", dn->full_name);
  68. for_each_possible_cpu(cpu) {
  69. if (per_cpu(powernv_rng, cpu) == NULL ||
  70. cpu_to_chip_id(cpu) == chip_id) {
  71. per_cpu(powernv_rng, cpu) = rng;
  72. }
  73. }
  74. }
  75. static __init int rng_create(struct device_node *dn)
  76. {
  77. struct powernv_rng *rng;
  78. struct resource res;
  79. unsigned long val;
  80. rng = kzalloc(sizeof(*rng), GFP_KERNEL);
  81. if (!rng)
  82. return -ENOMEM;
  83. if (of_address_to_resource(dn, 0, &res)) {
  84. kfree(rng);
  85. return -ENXIO;
  86. }
  87. rng->regs_real = (void __iomem *)res.start;
  88. rng->regs = of_iomap(dn, 0);
  89. if (!rng->regs) {
  90. kfree(rng);
  91. return -ENXIO;
  92. }
  93. val = in_be64(rng->regs);
  94. rng->mask = val;
  95. rng_init_per_cpu(rng, dn);
  96. pr_info_once("Registering arch random hook.\n");
  97. ppc_md.get_random_seed = powernv_get_random_long;
  98. return 0;
  99. }
  100. static __init int rng_init(void)
  101. {
  102. struct device_node *dn;
  103. int rc;
  104. for_each_compatible_node(dn, NULL, "ibm,power-rng") {
  105. rc = rng_create(dn);
  106. if (rc) {
  107. pr_err("Failed creating rng for %s (%d).\n",
  108. dn->full_name, rc);
  109. continue;
  110. }
  111. /* Create devices for hwrng driver */
  112. of_platform_device_create(dn, NULL, NULL);
  113. }
  114. return 0;
  115. }
  116. machine_subsys_initcall(powernv, rng_init);