-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[linux-6.6.y] Add support for Zhaoxin HW Random Number Generator #257
Merged
opsiff
merged 1 commit into
deepin-community:linux-6.6.y
from
leoliu-oc:linux-6.6.y-16-rng
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* | ||
* 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 int rep_xstore(size_t size, size_t factor, void *result) | ||
{ | ||
asm(".byte 0xf3, 0x0f, 0xa7, 0xc0" | ||
: "=m"(*(size_t *)result), "+c"(size), "+d"(factor), "+D"(result)); | ||
|
||
return 0; | ||
} | ||
|
||
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)) { | ||
pr_err(PFX "The CPU isn't support XSTORE.\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个pr_err要修改成pr_info吧 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是参考同内核类似情况采用pr_err的。这边的理解是,要么使用pr_err,要么直接不打印任何信息。而改为pr_info的话,会导致在其他非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 CPUs"); | ||
MODULE_AUTHOR("[email protected]"); | ||
MODULE_LICENSE("GPL"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里为什么要修改
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里修改的原因是,checkpatch.pl会提示格式warning。不过,这边会恢复保留原先格式。