rng.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. unsigned long mask;
  24. };
  25. static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
  26. static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
  27. {
  28. unsigned long parity;
  29. /* Calculate the parity of the value */
  30. asm ("popcntd %0,%1" : "=r" (parity) : "r" (val));
  31. /* xor our value with the previous mask */
  32. val ^= rng->mask;
  33. /* update the mask based on the parity of this value */
  34. rng->mask = (rng->mask << 1) | (parity & 1);
  35. return val;
  36. }
  37. int powernv_get_random_long(unsigned long *v)
  38. {
  39. struct powernv_rng *rng;
  40. rng = get_cpu_var(powernv_rng);
  41. *v = rng_whiten(rng, in_be64(rng->regs));
  42. put_cpu_var(rng);
  43. return 1;
  44. }
  45. EXPORT_SYMBOL_GPL(powernv_get_random_long);
  46. static __init void rng_init_per_cpu(struct powernv_rng *rng,
  47. struct device_node *dn)
  48. {
  49. int chip_id, cpu;
  50. chip_id = of_get_ibm_chip_id(dn);
  51. if (chip_id == -1)
  52. pr_warn("No ibm,chip-id found for %s.\n", dn->full_name);
  53. for_each_possible_cpu(cpu) {
  54. if (per_cpu(powernv_rng, cpu) == NULL ||
  55. cpu_to_chip_id(cpu) == chip_id) {
  56. per_cpu(powernv_rng, cpu) = rng;
  57. }
  58. }
  59. }
  60. static __init int rng_create(struct device_node *dn)
  61. {
  62. struct powernv_rng *rng;
  63. unsigned long val;
  64. rng = kzalloc(sizeof(*rng), GFP_KERNEL);
  65. if (!rng)
  66. return -ENOMEM;
  67. rng->regs = of_iomap(dn, 0);
  68. if (!rng->regs) {
  69. kfree(rng);
  70. return -ENXIO;
  71. }
  72. val = in_be64(rng->regs);
  73. rng->mask = val;
  74. rng_init_per_cpu(rng, dn);
  75. pr_info_once("Registering arch random hook.\n");
  76. ppc_md.get_random_long = powernv_get_random_long;
  77. return 0;
  78. }
  79. static __init int rng_init(void)
  80. {
  81. struct device_node *dn;
  82. int rc;
  83. for_each_compatible_node(dn, NULL, "ibm,power-rng") {
  84. rc = rng_create(dn);
  85. if (rc) {
  86. pr_err("Failed creating rng for %s (%d).\n",
  87. dn->full_name, rc);
  88. continue;
  89. }
  90. /* Create devices for hwrng driver */
  91. of_platform_device_create(dn, NULL, NULL);
  92. }
  93. return 0;
  94. }
  95. subsys_initcall(rng_init);