linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Michael Ellerman <michael@ellerman.id.au>
To: <linux-kernel@vger.kernel.org>
Cc: tytso@mit.edu, herbert@gondor.apana.org.au, gleb@redhat.com,
	agraf@suse.de, kvm-ppc@vger.kernel.org, linuxppc-dev@ozlabs.org,
	Paul Mackerras <paulus@samba.org>,
	kvm@vger.kernel.org, mpm@selenic.com, pbonzini@redhat.com
Subject: [PATCH 2/3] hwrng: Add a driver for the hwrng found in power7+ systems
Date: Thu, 26 Sep 2013 16:31:05 +1000	[thread overview]
Message-ID: <1380177066-3835-2-git-send-email-michael@ellerman.id.au> (raw)
In-Reply-To: <1380177066-3835-1-git-send-email-michael@ellerman.id.au>

Add a driver for the hwrng found in power7+ systems, based on the
existing code for the arch_get_random_long() hook.

We only register a single instance of the driver, not one per device,
because we use the existing per_cpu array of devices in the arch code.
This means we always read from the "closest" device, avoiding inter-chip
memory traffic.

Signed-off-by: Guo Chao <yan@linux.vnet.ibm.com>
Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
---
 drivers/char/hw_random/Kconfig       | 13 ++++++
 drivers/char/hw_random/Makefile      |  1 +
 drivers/char/hw_random/powernv-rng.c | 81 ++++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 drivers/char/hw_random/powernv-rng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 0aa9d91..c206de2 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -290,6 +290,19 @@ config HW_RANDOM_PSERIES
 
 	  If unsure, say Y.
 
+config HW_RANDOM_POWERNV
+	tristate "PowerNV Random Number Generator support"
+	depends on HW_RANDOM && PPC_POWERNV
+	default HW_RANDOM
+	---help---
+	  This is the driver for Random Number Generator hardware found
+	  in POWER7+ and above machines for PowerNV platform.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called powernv-rng.
+
+	  If unsure, say Y.
+
 config HW_RANDOM_EXYNOS
 	tristate "EXYNOS HW random number generator support"
 	depends on HW_RANDOM && HAS_IOMEM && HAVE_CLK
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index bed467c..d7d2435 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
 obj-$(CONFIG_HW_RANDOM_PICOXCELL) += picoxcell-rng.o
 obj-$(CONFIG_HW_RANDOM_PPC4XX) += ppc4xx-rng.o
 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
+obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_EXYNOS)	+= exynos-rng.o
 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
diff --git a/drivers/char/hw_random/powernv-rng.c b/drivers/char/hw_random/powernv-rng.c
new file mode 100644
index 0000000..e6965bf
--- /dev/null
+++ b/drivers/char/hw_random/powernv-rng.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2013 Michael Ellerman, Guo Chao, IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/random.h>
+#include <linux/hw_random.h>
+
+static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
+{
+	unsigned long *buf;
+	int i, len;
+
+	/* We rely on rng_buffer_size() being >= sizeof(unsigned long) */
+	len = max / sizeof(unsigned long);
+
+	buf = (unsigned long *)data;
+
+	for (i = 0; i < len; i++)
+		powernv_get_random_long(buf++);
+
+	return len * sizeof(unsigned long);
+}
+
+static struct hwrng powernv_hwrng = {
+	.name = "powernv-rng",
+	.read = powernv_rng_read,
+};
+
+static int powernv_rng_remove(struct platform_device *pdev)
+{
+	hwrng_unregister(&powernv_hwrng);
+
+	return 0;
+}
+
+static int powernv_rng_probe(struct platform_device *pdev)
+{
+	int rc;
+
+	rc = hwrng_register(&powernv_hwrng);
+	if (rc) {
+		/* We only register one device, ignore any others */
+		if (rc == -EEXIST)
+			rc = -ENODEV;
+
+		return rc;
+	}
+
+	pr_info("registered powernv hwrng.\n");
+
+	return 0;
+}
+
+static struct of_device_id powernv_rng_match[] = {
+	{ .compatible	= "ibm,power-rng",},
+	{},
+};
+MODULE_DEVICE_TABLE(of, powernv_rng_match);
+
+static struct platform_driver powernv_rng_driver = {
+	.driver = {
+		.name = "powernv_rng",
+		.of_match_table = powernv_rng_match,
+	},
+	.probe	= powernv_rng_probe,
+	.remove = powernv_rng_remove,
+};
+module_platform_driver(powernv_rng_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above");
-- 
1.8.1.2

  reply	other threads:[~2013-09-26  6:31 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-26  6:31 [PATCH 1/3] powerpc: Implement arch_get_random_long/int() for powernv Michael Ellerman
2013-09-26  6:31 ` Michael Ellerman [this message]
2013-09-26  8:01   ` [PATCH 2/3] hwrng: Add a driver for the hwrng found in power7+ systems Benjamin Herrenschmidt
2013-10-01  8:25     ` Michael Ellerman
2013-09-26  6:31 ` [PATCH 3/3] KVM: PPC: Book3S: Add support for hwrng found on some powernv systems Michael Ellerman
2013-09-26  9:06   ` Paolo Bonzini
2013-10-01  8:34     ` Michael Ellerman
2013-10-01  8:39       ` Gleb Natapov
2013-10-01  9:23         ` Paul Mackerras
2013-10-01  9:57           ` Gleb Natapov
2013-10-01 10:00           ` Alexander Graf
2013-10-01  9:38         ` Benjamin Herrenschmidt
2013-10-01 11:19           ` Paolo Bonzini
2013-10-01 21:44             ` Benjamin Herrenschmidt
2013-10-02  8:38               ` Paolo Bonzini
2013-10-02  5:09             ` Paul Mackerras
2013-10-02  8:46               ` Paolo Bonzini
2013-10-02  9:06                 ` Benjamin Herrenschmidt
2013-10-02  9:11                   ` Alexander Graf
2013-10-02  9:50                     ` Alexander Graf
2013-10-02 10:02                       ` Gleb Natapov
2013-10-02 13:57                         ` Michael Ellerman
2013-10-02 14:08                           ` Alexander Graf
2013-10-02 14:33                             ` Paolo Bonzini
2013-10-02 14:36                               ` Alexander Graf
2013-10-02 14:38                                 ` Paolo Bonzini
2013-10-02 22:45                                 ` Paul Mackerras
2013-10-03  5:48                                   ` Gleb Natapov
2013-10-03 10:06                                     ` Paul Mackerras
2013-10-03 12:08                                       ` Gleb Natapov
2013-10-02 14:37                               ` Gleb Natapov
2013-10-02 22:21                                 ` Benjamin Herrenschmidt
2013-10-03  6:08                                   ` Gleb Natapov
2013-10-02 22:13                             ` Benjamin Herrenschmidt
2013-10-02 14:10                           ` Gleb Natapov
2013-10-02 22:15                             ` Benjamin Herrenschmidt
2013-10-02 22:02                         ` Benjamin Herrenschmidt
2013-10-03  5:43                           ` Gleb Natapov
2013-10-03  7:22                             ` Benjamin Herrenschmidt
2013-10-02 22:07                         ` Benjamin Herrenschmidt
2013-10-03  6:28                           ` Gleb Natapov
2013-10-02 21:58                     ` Benjamin Herrenschmidt
2013-10-01  9:58       ` Paolo Bonzini
2013-09-27 14:15   ` Anshuman Khandual
2013-10-01  8:36     ` Michael Ellerman
2013-09-26  7:58 ` [PATCH 1/3] powerpc: Implement arch_get_random_long/int() for powernv Benjamin Herrenschmidt
2013-10-11  3:07 Michael Ellerman
2013-10-11  3:07 ` [PATCH 2/3] hwrng: Add a driver for the hwrng found in power7+ systems Michael Ellerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380177066-3835-2-git-send-email-michael@ellerman.id.au \
    --to=michael@ellerman.id.au \
    --cc=agraf@suse.de \
    --cc=gleb@redhat.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@ozlabs.org \
    --cc=mpm@selenic.com \
    --cc=paulus@samba.org \
    --cc=pbonzini@redhat.com \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).