Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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