linux/arch/powerpc/platforms/powernv/rng.c
<<
>>
Prefs
   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
  10#define pr_fmt(fmt)     "powernv-rng: " fmt
  11
  12#include <linux/kernel.h>
  13#include <linux/of.h>
  14#include <linux/of_address.h>
  15#include <linux/of_platform.h>
  16#include <linux/slab.h>
  17#include <linux/smp.h>
  18#include <asm/archrandom.h>
  19#include <asm/cputable.h>
  20#include <asm/io.h>
  21#include <asm/prom.h>
  22#include <asm/machdep.h>
  23#include <asm/smp.h>
  24
  25#define DARN_ERR 0xFFFFFFFFFFFFFFFFul
  26
  27struct powernv_rng {
  28        void __iomem *regs;
  29        void __iomem *regs_real;
  30        unsigned long mask;
  31};
  32
  33static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
  34
  35
  36int powernv_hwrng_present(void)
  37{
  38        struct powernv_rng *rng;
  39
  40        rng = get_cpu_var(powernv_rng);
  41        put_cpu_var(rng);
  42        return rng != NULL;
  43}
  44
  45static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
  46{
  47        unsigned long parity;
  48
  49        /* Calculate the parity of the value */
  50        asm ("popcntd %0,%1" : "=r" (parity) : "r" (val));
  51
  52        /* xor our value with the previous mask */
  53        val ^= rng->mask;
  54
  55        /* update the mask based on the parity of this value */
  56        rng->mask = (rng->mask << 1) | (parity & 1);
  57
  58        return val;
  59}
  60
  61int powernv_get_random_real_mode(unsigned long *v)
  62{
  63        struct powernv_rng *rng;
  64
  65        rng = raw_cpu_read(powernv_rng);
  66
  67        *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
  68
  69        return 1;
  70}
  71
  72int powernv_get_random_darn(unsigned long *v)
  73{
  74        unsigned long val;
  75
  76        /* Using DARN with L=1 - 64-bit conditioned random number */
  77        asm volatile(PPC_DARN(%0, 1) : "=r"(val));
  78
  79        if (val == DARN_ERR)
  80                return 0;
  81
  82        *v = val;
  83
  84        return 1;
  85}
  86
  87static int initialise_darn(void)
  88{
  89        unsigned long val;
  90        int i;
  91
  92        if (!cpu_has_feature(CPU_FTR_ARCH_300))
  93                return -ENODEV;
  94
  95        for (i = 0; i < 10; i++) {
  96                if (powernv_get_random_darn(&val)) {
  97                        ppc_md.get_random_seed = powernv_get_random_darn;
  98                        return 0;
  99                }
 100        }
 101
 102        pr_warn("Unable to use DARN for get_random_seed()\n");
 103
 104        return -EIO;
 105}
 106
 107int powernv_get_random_long(unsigned long *v)
 108{
 109        struct powernv_rng *rng;
 110
 111        rng = get_cpu_var(powernv_rng);
 112
 113        *v = rng_whiten(rng, in_be64(rng->regs));
 114
 115        put_cpu_var(rng);
 116
 117        return 1;
 118}
 119EXPORT_SYMBOL_GPL(powernv_get_random_long);
 120
 121static __init void rng_init_per_cpu(struct powernv_rng *rng,
 122                                    struct device_node *dn)
 123{
 124        int chip_id, cpu;
 125
 126        chip_id = of_get_ibm_chip_id(dn);
 127        if (chip_id == -1)
 128                pr_warn("No ibm,chip-id found for %pOF.\n", dn);
 129
 130        for_each_possible_cpu(cpu) {
 131                if (per_cpu(powernv_rng, cpu) == NULL ||
 132                    cpu_to_chip_id(cpu) == chip_id) {
 133                        per_cpu(powernv_rng, cpu) = rng;
 134                }
 135        }
 136}
 137
 138static __init int rng_create(struct device_node *dn)
 139{
 140        struct powernv_rng *rng;
 141        struct resource res;
 142        unsigned long val;
 143
 144        rng = kzalloc(sizeof(*rng), GFP_KERNEL);
 145        if (!rng)
 146                return -ENOMEM;
 147
 148        if (of_address_to_resource(dn, 0, &res)) {
 149                kfree(rng);
 150                return -ENXIO;
 151        }
 152
 153        rng->regs_real = (void __iomem *)res.start;
 154
 155        rng->regs = of_iomap(dn, 0);
 156        if (!rng->regs) {
 157                kfree(rng);
 158                return -ENXIO;
 159        }
 160
 161        val = in_be64(rng->regs);
 162        rng->mask = val;
 163
 164        rng_init_per_cpu(rng, dn);
 165
 166        pr_info_once("Registering arch random hook.\n");
 167
 168        ppc_md.get_random_seed = powernv_get_random_long;
 169
 170        return 0;
 171}
 172
 173static __init int rng_init(void)
 174{
 175        struct device_node *dn;
 176        int rc;
 177
 178        for_each_compatible_node(dn, NULL, "ibm,power-rng") {
 179                rc = rng_create(dn);
 180                if (rc) {
 181                        pr_err("Failed creating rng for %pOF (%d).\n",
 182                                dn, rc);
 183                        continue;
 184                }
 185
 186                /* Create devices for hwrng driver */
 187                of_platform_device_create(dn, NULL, NULL);
 188        }
 189
 190        initialise_darn();
 191
 192        return 0;
 193}
 194machine_subsys_initcall(powernv, rng_init);
 195