Path: blob/master/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c
26296 views
// SPDX-License-Identifier: GPL-2.01/*2* sun8i-ce-trng.c - hardware cryptographic offloader for3* Allwinner H3/A64/H5/H2+/H6/R40 SoC4*5* Copyright (C) 2015-2020 Corentin Labbe <[email protected]>6*7* This file handle the TRNG8*9* You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst10*/11#include "sun8i-ce.h"12#include <linux/dma-mapping.h>13#include <linux/pm_runtime.h>14#include <linux/hw_random.h>15/*16* Note that according to the algorithm ID, 2 versions of the TRNG exists,17* The first present in H3/H5/R40/A64 and the second present in H6.18* This file adds support for both, but only the second is working19* reliabily according to rngtest.20**/2122static int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)23{24struct sun8i_ce_dev *ce;25dma_addr_t dma_dst;26int err = 0;27int flow = 3;28unsigned int todo;29struct sun8i_ce_flow *chan;30struct ce_task *cet;31u32 common;32void *d;3334ce = container_of(rng, struct sun8i_ce_dev, trng);3536/* round the data length to a multiple of 32*/37todo = max + 32;38todo -= todo % 32;3940d = kzalloc(todo, GFP_KERNEL | GFP_DMA);41if (!d)42return -ENOMEM;4344#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG45ce->hwrng_stat_req++;46ce->hwrng_stat_bytes += todo;47#endif4849dma_dst = dma_map_single(ce->dev, d, todo, DMA_FROM_DEVICE);50if (dma_mapping_error(ce->dev, dma_dst)) {51dev_err(ce->dev, "Cannot DMA MAP DST\n");52err = -EFAULT;53goto err_dst;54}5556err = pm_runtime_resume_and_get(ce->dev);57if (err < 0)58goto err_pm;5960mutex_lock(&ce->rnglock);61chan = &ce->chanlist[flow];6263cet = &chan->tl[0];64memset(cet, 0, sizeof(struct ce_task));6566cet->t_id = cpu_to_le32(flow);67common = ce->variant->trng | CE_COMM_INT;68cet->t_common_ctl = cpu_to_le32(common);6970/* recent CE (H6) need length in bytes, in word otherwise */71if (ce->variant->trng_t_dlen_in_bytes)72cet->t_dlen = cpu_to_le32(todo);73else74cet->t_dlen = cpu_to_le32(todo / 4);7576cet->t_sym_ctl = 0;77cet->t_asym_ctl = 0;7879cet->t_dst[0].addr = desc_addr_val_le32(ce, dma_dst);80cet->t_dst[0].len = cpu_to_le32(todo / 4);81ce->chanlist[flow].timeout = todo;8283err = sun8i_ce_run_task(ce, 3, "TRNG");84mutex_unlock(&ce->rnglock);8586pm_runtime_put(ce->dev);8788err_pm:89dma_unmap_single(ce->dev, dma_dst, todo, DMA_FROM_DEVICE);9091if (!err) {92memcpy(data, d, max);93err = max;94}95err_dst:96kfree_sensitive(d);97return err;98}99100int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)101{102int ret;103104if (ce->variant->trng == CE_ID_NOTSUPP) {105dev_info(ce->dev, "TRNG not supported\n");106return 0;107}108ce->trng.name = "sun8i Crypto Engine TRNG";109ce->trng.read = sun8i_ce_trng_read;110111ret = hwrng_register(&ce->trng);112if (ret)113dev_err(ce->dev, "Fail to register the TRNG\n");114return ret;115}116117void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce)118{119if (ce->variant->trng == CE_ID_NOTSUPP)120return;121hwrng_unregister(&ce->trng);122}123124125