rng.c 2.4 KB

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