* linux-next: manual merge of the crypto tree with the origin tree
@ 2026-11-16 16:43 Mark Brown
0 siblings, 0 replies; 2+ messages in thread
From: Mark Brown @ 2026-11-16 16:43 UTC (permalink / raw)
To: Herbert Xu, Linux Crypto List
Cc: Kees Cook, Kees Cook, Lianfeng Ouyang, Linux Kernel Mailing List,
Linux Next Mailing List
[-- Attachment #1: Type: text/plain, Size: 24030 bytes --]
Hi all,
Today's linux-next merge of the crypto tree got a conflict in:
drivers/char/hw_random/jh7110-trng.c
between commit:
4dd10fbbeb9c3 ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")
from the origin tree and commit:
de5aafa40f37f ("hwrng: starfive - tidy up includes and drop unused code")
from the crypto tree.
I fixed it up (see below) and can carry the fix as necessary. This
is now fixed as far as linux-next is concerned, but any non trivial
conflicts should be mentioned to your upstream maintainer when your tree
is submitted for merging. You may also want to consider cooperating
with the maintainer of the conflicting tree to minimise any particularly
complex conflicts.
diff --combined drivers/char/hw_random/jh7110-trng.c
index bc5328566659d,763aacf7dfac6..0000000000000
--- a/drivers/char/hw_random/jh7110-trng.c
+++ b/drivers/char/hw_random/jh7110-trng.c
@@@ -1,25 -1,33 +1,33 @@@
// SPDX-License-Identifier: GPL-2.0
/*
- * TRNG driver for the StarFive JH7110 SoC
+ * TRNG driver for the StarFive JH7110/JHB100 SoCs
*
* Copyright (C) 2022 StarFive Technology Co.
*/
+ #include <linux/bits.h>
#include <linux/clk.h>
#include <linux/completion.h>
- #include <linux/delay.h>
+ #include <linux/container_of.h>
+ #include <linux/device.h>
+ #include <linux/devm-helpers.h>
#include <linux/err.h>
+ #include <linux/errno.h>
#include <linux/hw_random.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iopoll.h>
- #include <linux/kernel.h>
+ #include <linux/jiffies.h>
+ #include <linux/lockdep.h>
+ #include <linux/minmax.h>
#include <linux/module.h>
- #include <linux/of.h>
+ #include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
- #include <linux/random.h>
+ #include <linux/property.h>
#include <linux/reset.h>
+ #include <linux/types.h>
+ #include <linux/workqueue.h>
/* trng register offset */
#define STARFIVE_CTRL 0x00
@@@ -78,36 -86,64 +86,64 @@@
#define STARFIVE_ISTAT_SEED_DONE BIT(1)
#define STARFIVE_ISTAT_LFSR_LOCKUP BIT(4)
+ #define STARFIVE_ISTAT_ALL (STARFIVE_ISTAT_RAND_RDY | \
+ STARFIVE_ISTAT_SEED_DONE | \
+ STARFIVE_ISTAT_LFSR_LOCKUP)
+
#define STARFIVE_RAND_LEN sizeof(u32)
+ #define STARFIVE_RANDRESEED_TIMEOUT_MS 50
+ #define STARFIVE_RANDGEN_TIMEOUT_MS 20
+ #define STARFIVE_RANDGEN_NOWAIT_TIMEOUT 1
#define to_trng(p) container_of(p, struct starfive_trng, rng)
- enum reseed {
- RANDOM_RESEED,
- NONCE_RESEED,
- };
-
- enum mode {
+ enum starfive_trng_mode {
PRNG_128BIT,
PRNG_256BIT,
};
+ /*
+ * Power-down ordering of reset assertion vs. clock gating:
+ * SEQ_RST_FIRST - assert reset, then gate the clocks (JH7110).
+ * SEQ_CLK_FIRST - gate the clocks, then assert reset (JHB100). Asserting
+ * reset with the clocks running causes reset-domain-crossing
+ * glitches that can affect downstream IPs.
+ */
+ enum seq_rst_clk {
+ SEQ_RST_FIRST,
+ SEQ_CLK_FIRST,
+ };
+
+ struct starfive_trng_data {
+ enum seq_rst_clk seq_rst_clk;
+ };
+
struct starfive_trng {
struct device *dev;
void __iomem *base;
+ int irq;
struct clk *hclk;
struct clk *ahb;
struct reset_control *rst;
struct hwrng rng;
struct completion random_done;
struct completion reseed_done;
+ struct work_struct work;
+ const struct starfive_trng_data *data;
u32 mode;
u32 mission;
- u32 reseed;
- /* protects against concurrent write to ctrl register */
- spinlock_t write_lock;
+ bool need_reseed;
+ bool initialized;
+ bool clk_on;
+ bool rst_deassert;
+ struct mutex lock; /* protect trng cmd seq */
};
+ static inline struct starfive_trng *work_to_trng(struct work_struct *work)
+ {
+ return container_of(work, struct starfive_trng, work);
+ }
+
static u16 autoreq;
module_param(autoreq, ushort, 0);
MODULE_PARM_DESC(autoreq, "Auto-reseeding after random number requests by host reaches specified counter:\n"
@@@ -120,113 -156,262 +156,262 @@@ MODULE_PARM_DESC(autoage, "Auto-reseedi
" 0 - disable timer\n"
" other - reload value for internal timer");
- static inline int starfive_trng_wait_idle(struct starfive_trng *trng)
+ static bool starfive_trng_idle(struct starfive_trng *trng, bool wait)
{
- u32 stat;
+ u32 stat, busy = STARFIVE_STAT_RAND_GENERATING | STARFIVE_STAT_RAND_SEEDING;
- return readl_relaxed_poll_timeout(trng->base + STARFIVE_STAT, stat,
- !(stat & (STARFIVE_STAT_RAND_GENERATING |
- STARFIVE_STAT_RAND_SEEDING)),
- 10, 100000);
- }
-
- static inline void starfive_trng_irq_mask_clear(struct starfive_trng *trng)
- {
- /* clear register: ISTAT */
- u32 data = readl(trng->base + STARFIVE_ISTAT);
-
- writel(data, trng->base + STARFIVE_ISTAT);
- }
-
- static int starfive_trng_cmd(struct starfive_trng *trng, u32 cmd, bool wait)
- {
- int wait_time = 1000;
-
- /* allow up to 40 us for wait == 0 */
if (!wait)
- wait_time = 40;
+ return !(readl_relaxed(trng->base + STARFIVE_STAT) & busy);
+
+ return !readl_relaxed_poll_timeout(trng->base + STARFIVE_STAT, stat,
+ !(stat & busy),
+ 10, 100000);
+ }
+
+ /*
+ * Record a pending LFSR lockup and make sure it gets serviced. queue_work()
+ * is a no-op while the work is already pending, so this is safe to call
+ * from any context that notices the lockup.
+ */
+ static void starfive_trng_request_reseed(struct starfive_trng *trng)
+ {
+ WRITE_ONCE(trng->need_reseed, true);
+ queue_work(system_freezable_wq, &trng->work);
+ }
+
+ static void starfive_trng_irq_clear(struct starfive_trng *trng)
+ {
+ u32 istat = readl(trng->base + STARFIVE_ISTAT);
+
+ /* Do not lose an LFSR lockup that has not been serviced yet. */
+ if ((istat & STARFIVE_ISTAT_LFSR_LOCKUP) && trng->initialized)
+ starfive_trng_request_reseed(trng);
+
+ writel(istat, trng->base + STARFIVE_ISTAT);
+ }
+
+ static int starfive_trng_power_on(struct starfive_trng *trng)
+ {
+ int ret;
+
+ if (!trng->clk_on) {
+ ret = clk_prepare_enable(trng->hclk);
+ if (ret)
+ goto err_hclk;
+
+ ret = clk_prepare_enable(trng->ahb);
+ if (ret)
+ goto err_ahb;
+
+ trng->clk_on = true;
+ }
+
+ if (!trng->rst_deassert) {
+ ret = reset_control_deassert(trng->rst);
+ if (ret)
+ goto err_rst;
+
+ trng->rst_deassert = true;
+ }
+
+ return 0;
+
+ err_rst:
+ clk_disable_unprepare(trng->ahb);
+ err_ahb:
+ clk_disable_unprepare(trng->hclk);
+ trng->clk_on = false;
+ err_hclk:
+ return ret;
+ }
+
+ static void starfive_trng_power_off(struct starfive_trng *trng, bool assert_rst)
+ {
+ bool clk_first = trng->data->seq_rst_clk == SEQ_CLK_FIRST;
+ bool do_rst = assert_rst && trng->rst_deassert;
+
+ if (do_rst && !clk_first) {
+ reset_control_assert(trng->rst);
+ trng->rst_deassert = false;
+ }
+
+ if (trng->clk_on) {
+ clk_disable_unprepare(trng->ahb);
+ clk_disable_unprepare(trng->hclk);
+ trng->clk_on = false;
+ }
+
+ if (do_rst && clk_first) {
+ reset_control_assert(trng->rst);
+ trng->rst_deassert = false;
+ }
+ }
+
+ static void starfive_trng_power_off_action(void *data)
+ {
+ starfive_trng_power_off(data, true);
+ }
+
+ static int starfive_trng_cmd(struct starfive_trng *trng, u32 cmd, unsigned long timeout, bool wait)
+ {
+ struct completion *done;
+
+ lockdep_assert_held(&trng->lock);
+
+ if (!starfive_trng_idle(trng, wait))
+ return wait ? -ETIMEDOUT : -EBUSY;
switch (cmd) {
case STARFIVE_CTRL_GENE_RANDNUM:
- reinit_completion(&trng->random_done);
- spin_lock_irq(&trng->write_lock);
- writel(cmd, trng->base + STARFIVE_CTRL);
- spin_unlock_irq(&trng->write_lock);
- if (!wait_for_completion_timeout(&trng->random_done, usecs_to_jiffies(wait_time)))
- return -ETIMEDOUT;
+ done = &trng->random_done;
break;
case STARFIVE_CTRL_EXEC_RANDRESEED:
- reinit_completion(&trng->reseed_done);
- spin_lock_irq(&trng->write_lock);
- writel(cmd, trng->base + STARFIVE_CTRL);
- spin_unlock_irq(&trng->write_lock);
- if (!wait_for_completion_timeout(&trng->reseed_done, usecs_to_jiffies(wait_time)))
- return -ETIMEDOUT;
+ done = &trng->reseed_done;
break;
default:
return -EINVAL;
}
+ /*
+ * A command that timed out earlier may still have its completion
+ * interrupt in flight. Ack the status bits and flush the handler so
+ * a late RAND_RDY/SEED_DONE cannot complete this command.
+ */
+ writel(STARFIVE_ISTAT_RAND_RDY | STARFIVE_ISTAT_SEED_DONE,
+ trng->base + STARFIVE_ISTAT);
+ synchronize_irq(trng->irq);
+
+ reinit_completion(done);
+ writel(cmd, trng->base + STARFIVE_CTRL);
+ if (!wait_for_completion_timeout(done, timeout)) {
+ synchronize_irq(trng->irq);
+ if (!try_wait_for_completion(done))
+ return -ETIMEDOUT;
+ }
+
return 0;
}
- static int starfive_trng_init(struct hwrng *rng)
+ static int starfive_trng_reseed(struct starfive_trng *trng)
{
- struct starfive_trng *trng = to_trng(rng);
- u32 mode, intr = 0;
+ int ret;
+
+ lockdep_assert_held(&trng->lock);
+
+ WRITE_ONCE(trng->need_reseed, false);
+ ret = starfive_trng_cmd(trng, STARFIVE_CTRL_EXEC_RANDRESEED,
+ msecs_to_jiffies(STARFIVE_RANDRESEED_TIMEOUT_MS), true);
+ if (ret)
+ WRITE_ONCE(trng->need_reseed, true);
+
+ return ret;
+ }
+
+ static void starfive_trng_hw_setup(struct starfive_trng *trng)
+ {
+ u32 mode;
/* setup Auto Request/Age register */
writel(autoage, trng->base + STARFIVE_AUTO_AGE);
writel(autoreq, trng->base + STARFIVE_AUTO_RQSTS);
- /* clear register: ISTAT */
- starfive_trng_irq_mask_clear(trng);
-
- intr |= STARFIVE_IE_ALL;
- writel(intr, trng->base + STARFIVE_IE);
-
- mode = readl(trng->base + STARFIVE_MODE);
-
- switch (trng->mode) {
- case PRNG_128BIT:
+ mode = readl(trng->base + STARFIVE_MODE);
+ if (trng->mode == PRNG_128BIT)
mode &= ~STARFIVE_MODE_R256;
- break;
- case PRNG_256BIT:
+ else
mode |= STARFIVE_MODE_R256;
- break;
- default:
- mode |= STARFIVE_MODE_R256;
- break;
+ writel(mode, trng->base + STARFIVE_MODE);
+ }
+
+ static int starfive_trng_init(struct hwrng *rng)
+ {
+ struct starfive_trng *trng = to_trng(rng);
+ int ret;
+
+ ret = pm_runtime_resume_and_get(trng->dev);
+ if (ret < 0) {
+ dev_err(trng->dev, "Failed to wake device for init: %d\n", ret);
+ return ret;
}
- writel(mode, trng->base + STARFIVE_MODE);
+ mutex_lock(&trng->lock);
- return starfive_trng_cmd(trng, STARFIVE_CTRL_EXEC_RANDRESEED, 1);
+ /*
+ * The hwrng core can call init again without an intervening
+ * cleanup: deselecting and quickly reselecting the device makes
+ * cleanup_rng_work() skip ->cleanup() once the refcount is back up.
+ * Only bring up the interrupt line once in that case.
+ */
+ if (!trng->initialized) {
+ starfive_trng_irq_clear(trng);
+ starfive_trng_hw_setup(trng);
+
+ enable_irq(trng->irq);
+ writel(STARFIVE_IE_ALL, trng->base + STARFIVE_IE);
+ trng->initialized = true;
+ }
+
+ ret = starfive_trng_reseed(trng);
+ if (ret) {
+ trng->initialized = false;
+ writel(0, trng->base + STARFIVE_IE);
+ starfive_trng_irq_clear(trng);
+ disable_irq(trng->irq);
+ }
+
+ mutex_unlock(&trng->lock);
+
+ pm_runtime_put_autosuspend(trng->dev);
+
+ return ret;
+ }
+
+ static void starfive_trng_reseed_work(struct work_struct *work)
+ {
+ struct starfive_trng *trng = work_to_trng(work);
+ int ret;
+
+ ret = pm_runtime_resume_and_get(trng->dev);
+ if (ret < 0) {
+ dev_err(trng->dev, "Failed to wake device for reseed: %d\n", ret);
+ return;
+ }
+
+ mutex_lock(&trng->lock);
+
+ if (!READ_ONCE(trng->need_reseed) || !trng->initialized)
+ goto unlock;
+
+ ret = starfive_trng_reseed(trng);
+ if (ret)
+ dev_err(trng->dev, "Reseed after LFSR lockup failed: %d\n", ret);
+
+ unlock:
+ mutex_unlock(&trng->lock);
+
+ pm_runtime_put_autosuspend(trng->dev);
}
static irqreturn_t starfive_trng_irq(int irq, void *priv)
{
u32 status;
- struct starfive_trng *trng = (struct starfive_trng *)priv;
+ struct starfive_trng *trng = priv;
- status = readl(trng->base + STARFIVE_ISTAT);
- if (status & STARFIVE_ISTAT_RAND_RDY) {
- writel(STARFIVE_ISTAT_RAND_RDY, trng->base + STARFIVE_ISTAT);
+ status = readl(trng->base + STARFIVE_ISTAT) & STARFIVE_ISTAT_ALL;
+ if (!status)
+ return IRQ_NONE;
+
+ writel(status, trng->base + STARFIVE_ISTAT);
+
+ /* SEU occurred, reseeding required */
+ if (status & STARFIVE_ISTAT_LFSR_LOCKUP)
+ starfive_trng_request_reseed(trng);
+
+ if (status & STARFIVE_ISTAT_RAND_RDY)
complete(&trng->random_done);
- }
- if (status & STARFIVE_ISTAT_SEED_DONE) {
- writel(STARFIVE_ISTAT_SEED_DONE, trng->base + STARFIVE_ISTAT);
+ if (status & STARFIVE_ISTAT_SEED_DONE)
complete(&trng->reseed_done);
- }
-
- if (status & STARFIVE_ISTAT_LFSR_LOCKUP) {
- writel(STARFIVE_ISTAT_LFSR_LOCKUP, trng->base + STARFIVE_ISTAT);
- /* SEU occurred, reseeding required*/
- spin_lock(&trng->write_lock);
- writel(STARFIVE_CTRL_EXEC_RANDRESEED, trng->base + STARFIVE_CTRL);
- spin_unlock(&trng->write_lock);
- }
return IRQ_HANDLED;
}
@@@ -234,181 -419,267 +419,267 @@@
static void starfive_trng_cleanup(struct hwrng *rng)
{
struct starfive_trng *trng = to_trng(rng);
+ bool initialized;
+ int ret;
- writel(0, trng->base + STARFIVE_CTRL);
+ ret = pm_runtime_resume_and_get(trng->dev);
+ if (ret < 0)
+ dev_err(trng->dev, "Failed to wake device for cleanup: %d\n", ret);
- reset_control_assert(trng->rst);
- clk_disable_unprepare(trng->hclk);
- clk_disable_unprepare(trng->ahb);
+ mutex_lock(&trng->lock);
+ initialized = trng->initialized;
+ trng->initialized = false;
+ if (ret >= 0 && initialized) {
+ writel(0, trng->base + STARFIVE_IE);
+ starfive_trng_irq_clear(trng);
+ }
+ mutex_unlock(&trng->lock);
+
+ /*
+ * cleanup also runs after a failed init, which has already undone
+ * its enable_irq(). Only disable the line if init left it enabled.
+ */
+ if (initialized)
+ disable_irq(trng->irq);
+ cancel_work_sync(&trng->work);
+
+ if (ret < 0)
+ return;
+
+ mutex_lock(&trng->lock);
+ writel(STARFIVE_CTRL_EXEC_NOP, trng->base + STARFIVE_CTRL);
+ mutex_unlock(&trng->lock);
+
+ pm_runtime_put_sync_suspend(trng->dev);
}
static int starfive_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{
struct starfive_trng *trng = to_trng(rng);
- int ret;
+ unsigned long timeout;
+ int ret, retry = 1;
- pm_runtime_get_sync(trng->dev);
-
- if (trng->mode == PRNG_256BIT)
- max = min_t(size_t, max, (STARFIVE_RAND_LEN * 8));
- else
- max = min_t(size_t, max, (STARFIVE_RAND_LEN * 4));
+ ret = pm_runtime_resume_and_get(trng->dev);
+ if (ret < 0) {
+ dev_warn_ratelimited(trng->dev, "Failed to wake device for read: %d\n", ret);
+ return ret;
+ }
if (wait) {
- ret = starfive_trng_wait_idle(trng);
- if (ret) {
- ret = -ETIMEDOUT;
+ mutex_lock(&trng->lock);
+ timeout = msecs_to_jiffies(STARFIVE_RANDGEN_TIMEOUT_MS);
+ } else {
+ /* allow up to 1 jiffy for wait == 0 */
+ if (!mutex_trylock(&trng->lock)) {
+ ret = 0;
goto out_put;
}
+ timeout = STARFIVE_RANDGEN_NOWAIT_TIMEOUT;
+ }
+
+ for (;;) {
+ /*
+ * A pending LFSR lockup taints the next sample. Blocking
+ * callers reseed here; non-blocking callers return no data
+ * and leave it to the reseed work. Re-queue it explicitly:
+ * the lockup may have been seen by starfive_trng_irq_clear()
+ * or a previous reseed attempt may have failed.
+ */
+ if (READ_ONCE(trng->need_reseed)) {
+ if (!wait) {
+ starfive_trng_request_reseed(trng);
+ ret = 0;
+ goto out_unlock;
+ }
+ ret = starfive_trng_reseed(trng);
+ if (ret)
+ goto out_unlock;
+ }
+
+ ret = starfive_trng_cmd(trng, STARFIVE_CTRL_GENE_RANDNUM, timeout, wait);
+ if (ret) {
+ if (!wait && (ret == -EBUSY || ret == -ETIMEDOUT))
+ ret = 0;
+ goto out_unlock;
+ }
+
+ /* Lockup reported together with RAND_RDY: reseed and retry once */
+ if (!READ_ONCE(trng->need_reseed))
+ break;
+
+ if (!wait || !retry--) {
+ ret = 0;
+ goto out_unlock;
+ }
}
- ret = starfive_trng_cmd(trng, STARFIVE_CTRL_GENE_RANDNUM, wait);
- if (ret)
- goto out_put;
+ if (trng->mode == PRNG_256BIT)
+ max = min(max, STARFIVE_RAND_LEN * 8);
+ else
+ max = min(max, STARFIVE_RAND_LEN * 4);
memcpy_fromio(buf, trng->base + STARFIVE_RAND0, max);
+
ret = max;
+ out_unlock:
+ mutex_unlock(&trng->lock);
+
out_put:
- pm_runtime_put_sync_autosuspend(trng->dev);
+ pm_runtime_put_autosuspend(trng->dev);
+
return ret;
}
static int starfive_trng_probe(struct platform_device *pdev)
{
- int ret;
- int irq;
+ struct device *dev = &pdev->dev;
struct starfive_trng *trng;
+ int ret;
- trng = devm_kzalloc_obj(&pdev->dev, *trng);
- trng = devm_kzalloc(dev, sizeof(*trng), GFP_KERNEL);
++ trng = devm_kzalloc_obj(dev, *trng);
if (!trng)
return -ENOMEM;
platform_set_drvdata(pdev, trng);
- trng->dev = &pdev->dev;
+
+ trng->dev = dev;
+ trng->data = device_get_match_data(dev);
+ if (!trng->data)
+ return dev_err_probe(dev, -ENODEV, "No match data\n");
trng->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(trng->base))
- return dev_err_probe(&pdev->dev, PTR_ERR(trng->base),
- "Error remapping memory for platform device.\n");
+ return dev_err_probe(dev, PTR_ERR(trng->base),
+ "Error remapping memory for platform device\n");
- irq = platform_get_irq(pdev, 0);
- if (irq < 0)
- return irq;
+ trng->irq = platform_get_irq(pdev, 0);
+ if (trng->irq < 0)
+ return trng->irq;
init_completion(&trng->random_done);
init_completion(&trng->reseed_done);
- spin_lock_init(&trng->write_lock);
- ret = devm_request_irq(&pdev->dev, irq, starfive_trng_irq, 0, pdev->name,
- (void *)trng);
+ ret = devm_mutex_init(dev, &trng->lock);
if (ret)
return ret;
- trng->hclk = devm_clk_get(&pdev->dev, "hclk");
+ trng->hclk = devm_clk_get(dev, "hclk");
if (IS_ERR(trng->hclk))
- return dev_err_probe(&pdev->dev, PTR_ERR(trng->hclk),
+ return dev_err_probe(dev, PTR_ERR(trng->hclk),
"Error getting hardware reference clock\n");
- trng->ahb = devm_clk_get(&pdev->dev, "ahb");
+ trng->ahb = devm_clk_get(dev, "ahb");
if (IS_ERR(trng->ahb))
- return dev_err_probe(&pdev->dev, PTR_ERR(trng->ahb),
+ return dev_err_probe(dev, PTR_ERR(trng->ahb),
"Error getting ahb reference clock\n");
- trng->rst = devm_reset_control_get_shared(&pdev->dev, NULL);
+ trng->rst = devm_reset_control_get_shared(dev, NULL);
if (IS_ERR(trng->rst))
- return dev_err_probe(&pdev->dev, PTR_ERR(trng->rst),
+ return dev_err_probe(dev, PTR_ERR(trng->rst),
"Error getting hardware reset line\n");
- ret = clk_prepare_enable(trng->hclk);
+ ret = starfive_trng_power_on(trng);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to power on\n");
+
+ ret = devm_add_action_or_reset(dev, starfive_trng_power_off_action, trng);
if (ret)
return ret;
- ret = clk_prepare_enable(trng->ahb);
- if (ret)
- goto disable_hclk;
-
- ret = reset_control_deassert(trng->rst);
- if (ret)
- goto disable_ahb;
-
- trng->rng.name = dev_driver_string(&pdev->dev);
+ trng->rng.name = dev_driver_string(dev);
trng->rng.init = starfive_trng_init;
trng->rng.cleanup = starfive_trng_cleanup;
trng->rng.read = starfive_trng_read;
trng->mode = PRNG_256BIT;
trng->mission = 1;
- trng->reseed = RANDOM_RESEED;
- pm_runtime_use_autosuspend(&pdev->dev);
- pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
- pm_runtime_enable(&pdev->dev);
+ pm_runtime_use_autosuspend(dev);
+ pm_runtime_set_autosuspend_delay(dev, 100);
- ret = devm_hwrng_register(&pdev->dev, &trng->rng);
- if (ret) {
- pm_runtime_disable(&pdev->dev);
+ ret = devm_pm_runtime_set_active_enabled(dev);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
- reset_control_assert(trng->rst);
- ret = dev_err_probe(&pdev->dev, ret, "Failed to register hwrng\n");
- goto disable_ahb;
- }
+ ret = devm_work_autocancel(dev, &trng->work, starfive_trng_reseed_work);
+ if (ret)
+ return ret;
+
+ ret = devm_request_irq(dev, trng->irq, starfive_trng_irq, IRQF_NO_AUTOEN,
+ dev_name(dev), trng);
+ if (ret)
+ return ret;
+
+ ret = devm_hwrng_register(dev, &trng->rng);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to register hwrng\n");
+
+ pm_runtime_idle(dev);
return 0;
-
- disable_ahb:
- clk_disable_unprepare(trng->ahb);
- disable_hclk:
- clk_disable_unprepare(trng->hclk);
-
- return ret;
}
- static int __maybe_unused starfive_trng_suspend(struct device *dev)
+ static int starfive_trng_suspend(struct device *dev)
{
struct starfive_trng *trng = dev_get_drvdata(dev);
+ bool assert_rst;
- clk_disable_unprepare(trng->hclk);
- clk_disable_unprepare(trng->ahb);
+ mutex_lock(&trng->lock);
+ assert_rst = !trng->initialized;
+ writel(0, trng->base + STARFIVE_IE);
+ starfive_trng_irq_clear(trng);
+ mutex_unlock(&trng->lock);
+
+ synchronize_irq(trng->irq);
+ starfive_trng_power_off(trng, assert_rst);
return 0;
}
- static int __maybe_unused starfive_trng_resume(struct device *dev)
+ static int starfive_trng_resume(struct device *dev)
{
struct starfive_trng *trng = dev_get_drvdata(dev);
int ret;
- ret = clk_prepare_enable(trng->hclk);
- if (ret)
+ ret = starfive_trng_power_on(trng);
+ if (ret) {
+ dev_err(dev, "Failed to power on: %d\n", ret);
return ret;
+ }
- ret = clk_prepare_enable(trng->ahb);
- if (ret)
- clk_disable_unprepare(trng->hclk);
+ if (trng->initialized) {
+ starfive_trng_irq_clear(trng);
+ writel(STARFIVE_IE_ALL, trng->base + STARFIVE_IE);
+ }
- return ret;
+ return 0;
}
- static const struct dev_pm_ops starfive_trng_pm_ops = {
- SET_SYSTEM_SLEEP_PM_OPS(starfive_trng_suspend,
- starfive_trng_resume)
- SET_RUNTIME_PM_OPS(starfive_trng_suspend,
- starfive_trng_resume, NULL)
+ static const struct starfive_trng_data jh7110_data = {
+ .seq_rst_clk = SEQ_RST_FIRST,
};
- static const struct of_device_id trng_dt_ids[] __maybe_unused = {
- { .compatible = "starfive,jh7110-trng" },
+ static const struct starfive_trng_data jhb100_data = {
+ .seq_rst_clk = SEQ_CLK_FIRST,
+ };
+
+ static const struct of_device_id trng_dt_ids[] = {
+ { .compatible = "starfive,jh7110-trng", .data = &jh7110_data },
+ { .compatible = "starfive,jhb100-trng", .data = &jhb100_data },
{ }
};
MODULE_DEVICE_TABLE(of, trng_dt_ids);
+ static DEFINE_RUNTIME_DEV_PM_OPS(starfive_trng_pm_ops,
+ starfive_trng_suspend, starfive_trng_resume, NULL);
+
static struct platform_driver starfive_trng_driver = {
.probe = starfive_trng_probe,
.driver = {
.name = "jh7110-trng",
- .pm = &starfive_trng_pm_ops,
- .of_match_table = of_match_ptr(trng_dt_ids),
+ .pm = pm_ptr(&starfive_trng_pm_ops),
+ .of_match_table = trng_dt_ids,
},
};
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* linux-next: manual merge of the crypto tree with the origin tree
@ 2026-11-16 16:43 Mark Brown
0 siblings, 0 replies; 2+ messages in thread
From: Mark Brown @ 2026-11-16 16:43 UTC (permalink / raw)
To: Herbert Xu, Linux Crypto List
Cc: Linux Kernel Mailing List, Linux Next Mailing List,
Michael S. Tsirkin, Thorsten Blum
[-- Attachment #1: Type: text/plain, Size: 4088 bytes --]
Hi all,
Today's linux-next merge of the crypto tree got a conflict in:
drivers/char/hw_random/virtio-rng.c
between commit:
0f7bf47f5a0cc ("hwrng: virtio - prevent use-after-free after a failed restore")
from the origin tree and commits:
a3eaee98eecc4 ("hwrng: virtio - prevent use-after-free after a failed restore")
277b0dae660c3 ("hwrng: virtio - simplify probe_common()")
from the crypto tree.
I fixed it up (see below) and can carry the fix as necessary. This
is now fixed as far as linux-next is concerned, but any non trivial
conflicts should be mentioned to your upstream maintainer when your tree
is submitted for merging. You may also want to consider cooperating
with the maintainer of the conflicting tree to minimise any particularly
complex conflicts.
diff --combined drivers/char/hw_random/virtio-rng.c
index 45d87c0614a5e,892720b1ac6f9..0000000000000
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@@ -70,7 -70,7 +70,7 @@@ static void request_entropy(struct virt
static unsigned int copy_data(struct virtrng_info *vi, void *buf,
unsigned int size)
{
- unsigned int idx, avail;
+ unsigned int idx;
/*
* vi->data_avail was set from the device-reported used.len and
@@@ -81,13 -81,13 +81,13 @@@
* steered into adjacent slab memory, including under
* speculation.
*/
- avail = min(vi->data_avail, sizeof(vi->data));
- if (vi->data_idx >= avail) {
+ if (vi->data_idx >= sizeof(vi->data)) {
vi->data_avail = 0;
request_entropy(vi);
return 0;
}
- size = min(size, avail - vi->data_idx);
+ vi->data_avail = min(vi->data_avail, sizeof(vi->data) - vi->data_idx);
+ size = min(size, vi->data_avail);
idx = array_index_nospec(vi->data_idx, sizeof(vi->data));
memcpy(buf, vi->data + idx, size);
vi->data_idx += size;
@@@ -150,17 -150,17 +150,17 @@@ static void virtio_cleanup(struct hwrn
static int probe_common(struct virtio_device *vdev)
{
- int err, index;
- struct virtrng_info *vi = NULL;
+ int err;
+ struct virtrng_info *vi;
vi = kzalloc_obj(struct virtrng_info);
if (!vi)
return -ENOMEM;
- vi->index = index = ida_alloc(&rng_index_ida, GFP_KERNEL);
- if (index < 0) {
- err = index;
- goto err_ida;
+ vi->index = ida_alloc(&rng_index_ida, GFP_KERNEL);
+ if (vi->index < 0) {
+ err = vi->index;
+ goto err;
}
snprintf(vi->name, sizeof(vi->name), "virtio_rng.%d", vi->index);
init_completion(&vi->have_data);
@@@ -177,7 -177,8 +177,8 @@@
vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
if (IS_ERR(vi->vq)) {
err = PTR_ERR(vi->vq);
- goto err_find;
+ ida_free(&rng_index_ida, vi->index);
+ goto err;
}
virtio_device_ready(vdev);
@@@ -187,9 -188,7 +188,7 @@@
return 0;
- err_find:
- ida_free(&rng_index_ida, index);
- err_ida:
+ err:
vdev->priv = NULL;
kfree(vi);
return err;
@@@ -245,26 -244,27 +244,27 @@@ static int virtrng_freeze(struct virtio
static int virtrng_restore(struct virtio_device *vdev)
{
+ struct virtrng_info *vi;
int err;
err = probe_common(vdev);
- if (!err) {
- struct virtrng_info *vi = vdev->priv;
+ if (err)
+ return err;
+ vi = vdev->priv;
- /*
- * Set hwrng_removed to ensure that virtio_read()
- * does not block waiting for data before the
- * registration is complete.
- */
- vi->hwrng_removed = true;
- err = hwrng_register(&vi->hwrng);
- if (!err) {
- vi->hwrng_register_done = true;
- vi->hwrng_removed = false;
- }
- }
+ /*
+ * Set hwrng_removed to ensure that virtio_read()
+ * does not block waiting for data before the
+ * registration is complete.
+ */
+ vi->hwrng_removed = true;
+ err = hwrng_register(&vi->hwrng);
+ if (err)
+ return err;
- return err;
+ vi->hwrng_register_done = true;
+ vi->hwrng_removed = false;
+ return 0;
}
static const struct virtio_device_id id_table[] = {
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-16 16:43 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 16:43 linux-next: manual merge of the crypto tree with the origin tree Mark Brown
2026-11-16 16:43 Mark Brown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®