2023-07-31 09:09:34

by LeoLiu-oc

[permalink] [raw]
Subject: [PATCH v2 2/2] hwrng: add Zhaoxin HW RNG driver

From: leoliu-oc <[email protected]>

Add support for Zhaoxin HW RNG.

Signed-off-by: leoliu-oc <[email protected]>
---
drivers/char/hw_random/Kconfig | 13 +++++
drivers/char/hw_random/Makefile | 1 +
drivers/char/hw_random/zhaoxin-rng.c | 87 ++++++++++++++++++++++++++++
3 files changed, 101 insertions(+)
create mode 100644 drivers/char/hw_random/zhaoxin-rng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index e0b3786ca51b..e315cd444c77 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -150,6 +150,19 @@ config HW_RANDOM_VIA

If unsure, say Y.

+config HW_RANDOM_ZHAOXIN
+ tristate "Zhaoxin HW Random Number Generator support"
+ depends on X86 || COMPILE_TEST
+ default HW_RANDOM
+ help
+ This driver provides kernel-side support for the Random Number
+ Generator hardware found on Zhaoxin based motherboards.
+
+ To compile this driver as a module, choose M here: the
+ module will be called zhaoxin-rng.
+
+ If unsure, say Y.
+
config HW_RANDOM_IXP4XX
tristate "Intel IXP4xx NPU HW Pseudo-Random Number Generator support"
depends on ARCH_IXP4XX || COMPILE_TEST
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 32549a1186dc..ef5b3ae0794d 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_HW_RANDOM_GEODE) += geode-rng.o
obj-$(CONFIG_HW_RANDOM_N2RNG) += n2-rng.o
n2-rng-y := n2-drv.o n2-asm.o
obj-$(CONFIG_HW_RANDOM_VIA) += via-rng.o
+obj-$(CONFIG_HW_RANDOM_ZHAOXIN) += zhaoxin-rng.o
obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-trng.o
obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o
obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o
diff --git a/drivers/char/hw_random/zhaoxin-rng.c b/drivers/char/hw_random/zhaoxin-rng.c
new file mode 100644
index 000000000000..0ceeb9c3f989
--- /dev/null
+++ b/drivers/char/hw_random/zhaoxin-rng.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * RNG driver for Zhaoxin RNGs
+ *
+ * Copyright 2023 (c) Zhaoxin Semiconductor Co., Ltd.
+ *
+ */
+
+#include <crypto/padlock.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/hw_random.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/cpufeature.h>
+#include <asm/cpu_device_id.h>
+#include <asm/fpu/api.h>
+
+enum {
+ ZHAOXIN_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits*/
+ ZHAOXIN_RNG_CHUNK_4 = 0x01, /* 32 rand bits, 32 stored bits */
+ ZHAOXIN_RNG_CHUNK_2 = 0x02, /* 16 rand bits, 32 stored bits */
+ ZHAOXIN_RNG_CHUNK_1 = 0x03, /* 8 rand bits, 32 stored bits */
+ ZHAOXIN_RNG_MAX_SIZE = (128*1024),
+};
+
+static int zhaoxin_rng_init(struct hwrng *rng)
+{
+ if (!boot_cpu_has(X86_FEATURE_XSTORE_EN)) {
+ pr_err(PFX "can't enable hardware RNG if XSTORE is not enabled\n");
+ return -ENODEV;
+ }
+ return 0;
+}
+
+static inline void rep_xstore(size_t size, size_t factor, void *result)
+{
+ asm(".byte 0xF3, 0x0F, 0xA7, 0xC0 /*rep xstore*/"
+ : "=m" (*(size_t *)result), "+c" (size), "+d" (factor), "+D" (result));
+}
+
+static int zhaoxin_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
+{
+ if (max > ZHAOXIN_RNG_MAX_SIZE)
+ max = ZHAOXIN_RNG_MAX_SIZE;
+ rep_xstore(max, ZHAOXIN_RNG_CHUNK_1, data);
+ return max;
+}
+
+static struct hwrng zhaoxin_rng = {
+ .name = "zhaoxin",
+ .init = zhaoxin_rng_init,
+ .read = zhaoxin_rng_read,
+};
+
+static const struct x86_cpu_id zhaoxin_rng_cpu_ids[] = {
+ X86_MATCH_VENDOR_FAM_FEATURE(ZHAOXIN, 6, X86_FEATURE_XSTORE, NULL),
+ X86_MATCH_VENDOR_FAM_FEATURE(ZHAOXIN, 7, X86_FEATURE_XSTORE, NULL),
+ X86_MATCH_VENDOR_FAM_FEATURE(CENTAUR, 7, X86_FEATURE_XSTORE, NULL),
+ {}
+};
+MODULE_DEVICE_TABLE(x86cpu, zhaoxin_rng_cpu_ids);
+
+static int __init zhaoxin_rng_mod_init(void)
+{
+ int err;
+
+ if (!x86_match_cpu(zhaoxin_rng_cpu_ids))
+ return -ENODEV;
+
+ pr_info("Zhaoxin RNG detected\n");
+ err = hwrng_register(&zhaoxin_rng);
+ if (err)
+ pr_err(PFX "RNG registering failed (%d)\n", err);
+
+ return err;
+}
+module_init(zhaoxin_rng_mod_init);
+
+static void __exit zhaoxin_rng_mod_exit(void)
+{
+ hwrng_unregister(&zhaoxin_rng);
+}
+module_exit(zhaoxin_rng_mod_exit);
+MODULE_DESCRIPTION("H/W RNG driver for Zhaoxin CPU");
+MODULE_AUTHOR("[email protected]");
+MODULE_LICENSE("GPL");
--
2.34.1



2023-07-31 10:56:07

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] hwrng: add Zhaoxin HW RNG driver

Hi LeoLiu-oc,

kernel test robot noticed the following build warnings:

[auto build test WARNING on char-misc/char-misc-testing]
[also build test WARNING on char-misc/char-misc-next char-misc/char-misc-linus herbert-cryptodev-2.6/master linus/master v6.5-rc4 next-20230731]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/LeoLiu-oc/hwrng-via-rng-convert-to-x86_cpu_id-probing/20230731-164950
base: char-misc/char-misc-testing
patch link: https://lore.kernel.org/r/20230731084515.2057375-3-LeoLiu-oc%40zhaoxin.com
patch subject: [PATCH v2 2/2] hwrng: add Zhaoxin HW RNG driver
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20230731/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230731/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from arch/loongarch/include/asm/inst.h:10,
from arch/loongarch/include/asm/uprobes.h:5,
from include/linux/uprobes.h:49,
from include/linux/mm_types.h:16,
from include/linux/buildid.h:5,
from include/linux/module.h:14,
from drivers/char/hw_random/zhaoxin-rng.c:10:
>> arch/loongarch/include/asm/asm.h:35: warning: "STACK_ALIGN" redefined
35 | #define STACK_ALIGN ~(0xf)
|
In file included from drivers/char/hw_random/zhaoxin-rng.c:9:
include/crypto/padlock.h:19: note: this is the location of the previous definition
19 | #define STACK_ALIGN 16
|
drivers/char/hw_random/zhaoxin-rng.c:16:10: fatal error: asm/cpu_device_id.h: No such file or directory
16 | #include <asm/cpu_device_id.h>
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.


vim +/STACK_ALIGN +35 arch/loongarch/include/asm/asm.h

b738c106f7355e Huacai Chen 2022-05-31 31
b738c106f7355e Huacai Chen 2022-05-31 32 /*
b738c106f7355e Huacai Chen 2022-05-31 33 * Stack alignment
b738c106f7355e Huacai Chen 2022-05-31 34 */
b738c106f7355e Huacai Chen 2022-05-31 @35 #define STACK_ALIGN ~(0xf)
b738c106f7355e Huacai Chen 2022-05-31 36

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

2023-07-31 13:26:59

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] hwrng: add Zhaoxin HW RNG driver

Hi LeoLiu-oc,

kernel test robot noticed the following build errors:

[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on char-misc/char-misc-next char-misc/char-misc-linus herbert-cryptodev-2.6/master linus/master v6.5-rc4 next-20230731]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/LeoLiu-oc/hwrng-via-rng-convert-to-x86_cpu_id-probing/20230731-164950
base: char-misc/char-misc-testing
patch link: https://lore.kernel.org/r/20230731084515.2057375-3-LeoLiu-oc%40zhaoxin.com
patch subject: [PATCH v2 2/2] hwrng: add Zhaoxin HW RNG driver
config: openrisc-randconfig-r025-20230731 (https://download.01.org/0day-ci/archive/20230731/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230731/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/char/hw_random/zhaoxin-rng.c:16:10: fatal error: asm/cpu_device_id.h: No such file or directory
16 | #include <asm/cpu_device_id.h>
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.


vim +16 drivers/char/hw_random/zhaoxin-rng.c

> 16 #include <asm/cpu_device_id.h>
17 #include <asm/fpu/api.h>
18

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki