File Index
Files and directories
Sign in
FILE

sun8i-ce-trng.c

File preview with syntax highlighting.

linux-6.19.14/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * sun8i-ce-trng.c - hardware cryptographic offloader for
  4 * Allwinner H3/A64/H5/H2+/H6/R40 SoC
  5 *
  6 * Copyright (C) 2015-2020 Corentin Labbe <clabbe@baylibre.com>
  7 *
  8 * This file handle the TRNG
  9 *
 10 * You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst
 11 */
 12#include "sun8i-ce.h"
 13#include <linux/dma-mapping.h>
 14#include <linux/pm_runtime.h>
 15#include <linux/hw_random.h>
 16/*
 17 * Note that according to the algorithm ID, 2 versions of the TRNG exists,
 18 * The first present in H3/H5/R40/A64 and the second present in H6.
 19 * This file adds support for both, but only the second is working
 20 * reliabily according to rngtest.
 21 **/
 22
 23static int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
 24{
 25	struct sun8i_ce_dev *ce;
 26	dma_addr_t dma_dst;
 27	int err = 0;
 28	int flow = 3;
 29	unsigned int todo;
 30	struct sun8i_ce_flow *chan;
 31	struct ce_task *cet;
 32	u32 common;
 33	void *d;
 34
 35	ce = container_of(rng, struct sun8i_ce_dev, trng);
 36
 37	/* round the data length to a multiple of 32*/
 38	todo = max + 32;
 39	todo -= todo % 32;
 40
 41	d = kzalloc(todo, GFP_KERNEL | GFP_DMA);
 42	if (!d)
 43		return -ENOMEM;
 44
 45#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
 46	ce->hwrng_stat_req++;
 47	ce->hwrng_stat_bytes += todo;
 48#endif
 49
 50	dma_dst = dma_map_single(ce->dev, d, todo, DMA_FROM_DEVICE);
 51	if (dma_mapping_error(ce->dev, dma_dst)) {
 52		dev_err(ce->dev, "Cannot DMA MAP DST\n");
 53		err = -EFAULT;
 54		goto err_dst;
 55	}
 56
 57	err = pm_runtime_resume_and_get(ce->dev);
 58	if (err < 0)
 59		goto err_pm;
 60
 61	mutex_lock(&ce->rnglock);
 62	chan = &ce->chanlist[flow];
 63
 64	cet = &chan->tl[0];
 65	memset(cet, 0, sizeof(struct ce_task));
 66
 67	cet->t_id = cpu_to_le32(flow);
 68	common = ce->variant->trng | CE_COMM_INT;
 69	cet->t_common_ctl = cpu_to_le32(common);
 70
 71	/* recent CE (H6) need length in bytes, in word otherwise */
 72	if (ce->variant->trng_t_dlen_in_bytes)
 73		cet->t_dlen = cpu_to_le32(todo);
 74	else
 75		cet->t_dlen = cpu_to_le32(todo / 4);
 76
 77	cet->t_sym_ctl = 0;
 78	cet->t_asym_ctl = 0;
 79
 80	cet->t_dst[0].addr = desc_addr_val_le32(ce, dma_dst);
 81	cet->t_dst[0].len = cpu_to_le32(todo / 4);
 82
 83	err = sun8i_ce_run_task(ce, 3, "TRNG");
 84	mutex_unlock(&ce->rnglock);
 85
 86	pm_runtime_put(ce->dev);
 87
 88err_pm:
 89	dma_unmap_single(ce->dev, dma_dst, todo, DMA_FROM_DEVICE);
 90
 91	if (!err) {
 92		memcpy(data, d, max);
 93		err = max;
 94	}
 95err_dst:
 96	kfree_sensitive(d);
 97	return err;
 98}
 99
100int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)
101{
102	int ret;
103
104	if (ce->variant->trng == CE_ID_NOTSUPP) {
105		dev_info(ce->dev, "TRNG not supported\n");
106		return 0;
107	}
108	ce->trng.name = "sun8i Crypto Engine TRNG";
109	ce->trng.read = sun8i_ce_trng_read;
110
111	ret = hwrng_register(&ce->trng);
112	if (ret)
113		dev_err(ce->dev, "Fail to register the TRNG\n");
114	return ret;
115}
116
117void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce)
118{
119	if (ce->variant->trng == CE_ID_NOTSUPP)
120		return;
121	hwrng_unregister(&ce->trng);
122}
~/ Read-only file preview.