Skip to contentSkip to conversation

crypto: starfive - Add drivers for crypto engine

PATCH Superseded Jia Jie Ho 2 participants 10 messages May 4, 2023 – May 15, 2023

Subsystems (5)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Jia Jie Ho

author maintainer
#0 lore raw

v7 cover crypto: starfive - Add drivers for crypto engine

This patch series adds kernel driver support for StarFive JH7110 crypto engine. The first patch adds Documentations for the device and Patch 2 adds device probe and DMA init for the module. Patch 3 adds crypto and DMA dts node for VisionFive 2 board. Patch 4 adds hash/hmac support to the module.

Patch 3 needs to be applied on top of:
https://lore.kernel.org/lkml/[email protected]/

Patch 4 needs to be applied on top of:
https://lore.kernel.org/linux-crypto/[email protected]/T/#u

Changes v6->v7
- Remove NULL assignment as struct is kzalloc()-ed (Christophe)
- Do clk_disable_unprepare and assert reset if probe failed (Christophe)
- Remove unnecessary null pointer check (Christophe)
- Update module name in Kconfig description (Christophe)

Changes v5->v6
- Remove set_crypt in export as request will have been created by init/updated calls (Herbert)
- Use new helper to set statesize of crypto_ahash (Herbert)
- Use crypto_ahash_blocksize instead of crypto_ahash_tfm (Herbert)
- Switch to init_tfm/exit_tfm instead of cra_init/cra_exit (Herbert)

Changes v4->v5
- Schedule tasklet from IRQ handler instead of using completion to sync events (Herbert)

Changes v3->v4:
- Use fallback for non-aligned cases as hardware doesn't support hashing piece-meal (Herbert)
- Use ahash_request_set_* helpers to update members of ahash_request (Herbert)
- Set callbacks for async fallback (Herbert)
- Remove completion variable and use dma_callback to do the rest of processing instead. (Herbert)

Changes v2->v3:
- Only implement digest and use fallback for other ops (Herbert)
- Use interrupt instead of polling for hash complete (Herbert)
- Remove manual data copy from out-of-bound memory location as it will be handled by DMA API. (Christoph & Herbert)

Changes v1->v2:
- Fixed yaml filename and format (Krzysztof)
- Removed unnecessary property names in yaml (Krzysztof)
- Moved of_device_id table close to usage (Krzysztof)
- Use dev_err_probe for error returns (Krzysztof)
- Dropped redundant readl and writel wrappers (Krzysztof)
- Updated commit signed offs (Conor)
- Dropped redundant node in dts, module set to on in dtsi (Conor)

Jia Jie Ho (4):
dt-bindings: crypto: Add StarFive crypto module
crypto: starfive - Add crypto engine support
riscv: dts: starfive: Add crypto and DMA node for VisionFive 2
crypto: starfive - Add hash and HMAC support

 .../crypto/starfive,jh7110-crypto.yaml        |  70 ++
 MAINTAINERS                                   |   7 +
 arch/riscv/boot/dts/starfive/jh7110.dtsi      |  28 +
 drivers/crypto/Kconfig                        |   1 +
 drivers/crypto/Makefile                       |   1 +
 drivers/crypto/starfive/Kconfig               |  21 +
 drivers/crypto/starfive/Makefile              |   4 +
 drivers/crypto/starfive/jh7110-cryp.c         | 240 +++++
 drivers/crypto/starfive/jh7110-cryp.h         | 127 +++
 drivers/crypto/starfive/jh7110-hash.c         | 892 ++++++++++++++++++
 10 files changed, 1391 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/starfive,jh7110-crypto.yaml
 create mode 100644 drivers/crypto/starfive/Kconfig
 create mode 100644 drivers/crypto/starfive/Makefile
 create mode 100644 drivers/crypto/starfive/jh7110-cryp.c
 create mode 100644 drivers/crypto/starfive/jh7110-cryp.h
 create mode 100644 drivers/crypto/starfive/jh7110-hash.c
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v7 1/4 dt-bindings: crypto: Add StarFive crypto module

Add documentation to describe StarFive cryptographic engine.

---
 .../crypto/starfive,jh7110-crypto.yaml        | 70 +++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/starfive,jh7110-crypto.yaml
Documentation/devicetree/bindings/crypto/starfive,jh7110-crypto.yaml new file +70 -0
new file mode 100644
index 000000000000..71a2876bd6e4
@@ -0,0 +1,70 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/crypto/starfive,jh7110-crypto.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive Cryptographic Module
+
+maintainers:
+  - Jia Jie Ho <[email protected]>
+  - William Qiu <[email protected]>
+
+properties:
+  compatible:
+    const: starfive,jh7110-crypto
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: Hardware reference clock
+      - description: AHB reference clock
+
+  clock-names:
+    items:
+      - const: hclk
+      - const: ahb
+
+  interrupts:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+  dmas:
+    items:
+      - description: TX DMA channel
+      - description: RX DMA channel
+
+  dma-names:
+    items:
+      - const: tx
+      - const: rx
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - dmas
+  - dma-names
+
+additionalProperties: false
+
+examples:
+  - |
+    crypto: crypto@16000000 {
+        compatible = "starfive,jh7110-crypto";
+        reg = <0x16000000 0x4000>;
+        clocks = <&clk 15>, <&clk 16>;
+        clock-names = "hclk", "ahb";
+        interrupts = <28>;
+        resets = <&reset 3>;
+        dmas = <&dma 1 2>,
+               <&dma 0 2>;
+        dma-names = "tx", "rx";
+    };
+...
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH v7 2/4 crypto: starfive - Add crypto engine support

In reply to Jia Jie Ho

Adding device probe and DMA init for StarFive cryptographic module.

---
 MAINTAINERS                           |   7 +
 drivers/crypto/Kconfig                |   1 +
 drivers/crypto/Makefile               |   1 +
 drivers/crypto/starfive/Kconfig       |  17 +++
 drivers/crypto/starfive/Makefile      |   4 +
 drivers/crypto/starfive/jh7110-cryp.c | 201 ++++++++++++++++++++++++++
 drivers/crypto/starfive/jh7110-cryp.h |  63 ++++++++
 7 files changed, 294 insertions(+)
 create mode 100644 drivers/crypto/starfive/Kconfig
 create mode 100644 drivers/crypto/starfive/Makefile
 create mode 100644 drivers/crypto/starfive/jh7110-cryp.c
 create mode 100644 drivers/crypto/starfive/jh7110-cryp.h
MAINTAINERS +7 -0
index 5f9c544bc189..498f3b7792a6 100644
@@ -19920,6 +19920,13 @@ F:	Documentation/devicetree/bindings/clock/starfive,jh71*.yaml
 F:	drivers/clk/starfive/clk-starfive-jh71*
 F:	include/dt-bindings/clock/starfive?jh71*.h
 
+STARFIVE CRYPTO DRIVER
+M:	Jia Jie Ho <[email protected]>
+M:	William Qiu <[email protected]>
+S:	Supported
+F:	Documentation/devicetree/bindings/crypto/starfive*
+F:	drivers/crypto/starfive/
+
 STARFIVE JH71X0 PINCTRL DRIVERS
 M:	Emil Renner Berthing <[email protected]>
 M:	Jianlong Huang <[email protected]>
drivers/crypto/Kconfig +1 -0
index 3b2516d1433f..c9b6a8747fa1 100644
@@ -822,5 +822,6 @@ config CRYPTO_DEV_SA2UL
 
 source "drivers/crypto/keembay/Kconfig"
 source "drivers/crypto/aspeed/Kconfig"
+source "drivers/crypto/starfive/Kconfig"
 
 endif # CRYPTO_HW
drivers/crypto/Makefile +1 -0
index 476f1a25ca32..5ff5242a1824 100644
@@ -52,3 +52,4 @@ obj-y += xilinx/
 obj-y += hisilicon/
 obj-$(CONFIG_CRYPTO_DEV_AMLOGIC_GXL) += amlogic/
 obj-y += keembay/
+obj-y += starfive/
drivers/crypto/starfive/Kconfig new file +17 -0
new file mode 100644
index 000000000000..2436e93d27ef
@@ -0,0 +1,17 @@
+#
+# StarFive crypto drivers configuration
+#
+
+config CRYPTO_DEV_JH7110
+	tristate "StarFive JH7110 cryptographic engine driver"
+	depends on SOC_STARFIVE
+	select CRYPTO_ENGINE
+	select ARM_AMBA
+	select DMADEVICES
+	select AMBA_PL08X
+	help
+	  Support for StarFive JH7110 crypto hardware acceleration engine.
+	  This module provides acceleration for public key algo,
+	  skciphers, AEAD and hash functions.
+
+	  If you choose 'M' here, this module will be called jh7110-crypto.
drivers/crypto/starfive/Makefile new file +4 -0
new file mode 100644
index 000000000000..41221acaee39
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_CRYPTO_DEV_JH7110) += jh7110-crypto.o
+jh7110-crypto-objs := jh7110-cryp.o
drivers/crypto/starfive/jh7110-cryp.c new file +201 -0

201 lines not on this page · raw message

drivers/crypto/starfive/jh7110-cryp.h new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#3 lore raw

Re: [PATCH v7 2/4] crypto: starfive - Add crypto engine support

drivers/crypto/starfive/Kconfig line 7

Quoted 4 lines · Jia Jie Ho
drivers/crypto/starfive/Kconfig new file +3 -0
+config CRYPTO_DEV_JH7110
+	tristate "StarFive JH7110 cryptographic engine driver"
+	depends on SOC_STARFIVE

Please enable COMPILE_TEST.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#4 lore raw

PATCH v7 3/4 riscv: dts: starfive: Add crypto and DMA node for VisionFive 2

In reply to Jia Jie Ho

Add StarFive cryptographic module and dedicated DMA controller node to VisionFive 2 SoCs.

---
 arch/riscv/boot/dts/starfive/jh7110.dtsi | 28 ++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
arch/riscv/boot/dts/starfive/jh7110.dtsi +28 -0
index 9acb5fb1716d..7a0ba2748582 100644
@@ -570,5 +570,33 @@ voutcrg: clock-controller@295c0000 {
 			#reset-cells = <1>;
 			power-domains = <&pwrc JH7110_PD_VOUT>;
 		};
+
+		sdma: dma@16008000 {
+			compatible = "arm,pl080", "arm,primecell";
+			arm,primecell-periphid = <0x00041080>;
+			reg = <0x0 0x16008000 0x0 0x4000>;
+			interrupts = <29>;
+			clocks = <&stgcrg JH7110_STGCLK_SEC_AHB>,
+				 <&stgcrg JH7110_STGCLK_SEC_MISC_AHB>;
+			clock-names = "hclk", "apb_pclk";
+			resets = <&stgcrg JH7110_STGRST_SEC_AHB>;
+			lli-bus-interface-ahb1;
+			mem-bus-interface-ahb1;
+			memcpy-burst-size = <256>;
+			memcpy-bus-width = <32>;
+			#dma-cells = <2>;
+		};
+
+		crypto: crypto@16000000 {
+			compatible = "starfive,jh7110-crypto";
+			reg = <0x0 0x16000000 0x0 0x4000>;
+			clocks = <&stgcrg JH7110_STGCLK_SEC_AHB>,
+				 <&stgcrg JH7110_STGCLK_SEC_MISC_AHB>;
+			clock-names = "hclk", "ahb";
+			interrupts = <28>;
+			resets = <&stgcrg JH7110_STGRST_SEC_AHB>;
+			dmas = <&sdma 1 2>, <&sdma 0 2>;
+			dma-names = "tx", "rx";
+		};
 	};
 };
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author maintainer
#5 lore raw

PATCH v7 4/4 crypto: starfive - Add hash and HMAC support

In reply to Jia Jie Ho

Adding hash/HMAC support for SHA-2 and SM3 to StarFive cryptographic module.

---
 drivers/crypto/starfive/Kconfig       |   4 +
 drivers/crypto/starfive/Makefile      |   2 +-
 drivers/crypto/starfive/jh7110-cryp.c |  39 ++
 drivers/crypto/starfive/jh7110-cryp.h |  70 +-
 drivers/crypto/starfive/jh7110-hash.c | 892 ++++++++++++++++++++++++++
 5 files changed, 1003 insertions(+), 4 deletions(-)
 create mode 100644 drivers/crypto/starfive/jh7110-hash.c
drivers/crypto/starfive/Kconfig +4 -0
index 2436e93d27ef..8795b2fddb4e 100644
@@ -6,6 +6,10 @@ config CRYPTO_DEV_JH7110
 	tristate "StarFive JH7110 cryptographic engine driver"
 	depends on SOC_STARFIVE
 	select CRYPTO_ENGINE
+	select CRYPTO_HMAC
+	select CRYPTO_SHA256
+	select CRYPTO_SHA512
+	select CRYPTO_SM3_GENERIC
 	select ARM_AMBA
 	select DMADEVICES
 	select AMBA_PL08X
drivers/crypto/starfive/Makefile +1 -1
index 41221acaee39..2af49062e36d 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_CRYPTO_DEV_JH7110) += jh7110-crypto.o
-jh7110-crypto-objs := jh7110-cryp.o
+jh7110-crypto-objs := jh7110-cryp.o jh7110-hash.o
drivers/crypto/starfive/jh7110-cryp.c +39 -0
index 4b2505c23168..279b19f51cb4 100644
@@ -79,10 +79,25 @@ static void starfive_dma_cleanup(struct starfive_cryp_dev *cryp)
 	dma_release_channel(cryp->rx);
 }
 
+static irqreturn_t starfive_cryp_irq(int irq, void *priv)
+{
+	u32 status;
+	struct starfive_cryp_dev *cryp = (struct starfive_cryp_dev *)priv;
+
+	status = readl(cryp->base + STARFIVE_IE_FLAG_OFFSET);
+	if (status & STARFIVE_IE_FLAG_HASH_DONE) {
+		writel(STARFIVE_IE_MASK_HASH_DONE, cryp->base + STARFIVE_IE_MASK_OFFSET);
+		tasklet_schedule(&cryp->hash_done);
+	}
+
+	return IRQ_HANDLED;
+}
+
 static int starfive_cryp_probe(struct platform_device *pdev)
 {
 	struct starfive_cryp_dev *cryp;
 	struct resource *res;
+	int irq;
 	int ret;
 
 	cryp = devm_kzalloc(&pdev->dev, sizeof(*cryp), GFP_KERNEL);
@@ -97,6 +112,8 @@ static int starfive_cryp_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, PTR_ERR(cryp->base),
 				     "Error remapping memory for platform device\n");
 
+	tasklet_init(&cryp->hash_done, starfive_hash_done_task, (unsigned long)cryp);
+
 	cryp->phys_base = res->start;
 	cryp->dma_maxburst = 32;
 
@@ -115,6 +132,16 @@ static int starfive_cryp_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, PTR_ERR(cryp->rst),
 				     "Error getting hardware reset line\n");
 
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
+	ret = devm_request_irq(&pdev->dev, irq, starfive_cryp_irq, 0, pdev->name,
+			       (void *)cryp);
+	if (ret)
+		return dev_err_probe(&pdev->dev, irq,
+				     "Failed to register interrupt handler\n");
+
 	clk_prepare_enable(cryp->hclk);
 	clk_prepare_enable(cryp->ahb);
 	reset_control_deassert(cryp->rst);
@@ -142,8 +169,14 @@ static int starfive_cryp_probe(struct platform_device *pdev)
 	if (ret)
 		goto err_engine_start;
 
+	ret = starfive_hash_register_algs();
+	if (ret)
+		goto err_algs_hash;
+
 	return 0;
 
+err_algs_hash:
+	crypto_engine_stop(cryp->engine);
 err_engine_start:
 	crypto_engine_exit(cryp->engine);
 err_engine:
@@ -156,6 +189,8 @@ static int starfive_cryp_probe(struct platform_device *pdev)
 	clk_disable_unprepare(cryp->hclk);
 	clk_disable_unprepare(cryp->ahb);
 	reset_control_assert(cryp->rst);
+
+	tasklet_kill(&cryp->hash_done);
 err_probe_defer:
 	return ret;
 }
@@ -164,6 +199,10 @@ static int starfive_cryp_remove(struct platform_device *pdev)
 {
 	struct starfive_cryp_dev *cryp = platform_get_drvdata(pdev);
 
+	starfive_hash_unregister_algs();
+
+	tasklet_kill(&cryp->hash_done);
+
 	crypto_engine_stop(cryp->engine);
 	crypto_engine_exit(cryp->engine);
 
drivers/crypto/starfive/jh7110-cryp.h +67 -3

104 lines not on this page · raw message

drivers/crypto/starfive/jh7110-hash.c new file +892 -0

892 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#6 lore raw

Re: [PATCH v7 4/4] crypto: starfive - Add hash and HMAC support

drivers/crypto/starfive/jh7110-hash.c line 111

Quoted 3 lines · Jia Jie Ho
drivers/crypto/starfive/jh7110-hash.c new file +2 -0
+	writel(~STARFIVE_IE_MASK_HASH_DONE, cryp->base + STARFIVE_IE_MASK_OFFSET);
+	writel(csr.v, cryp->base + STARFIVE_HASH_SHACSR);

With compile testing I get

../drivers/crypto/starfive/jh7110-hash.c: In function ‘starfive_hash_start’:
../drivers/crypto/starfive/jh7110-hash.c:110:9: warning: conversion from ‘long unsigned int’ to ‘unsigned int’ changes value from ‘18446744073709551611’ to ‘4294967291’ [-Woverflow]

Please fix.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#7 lore raw

Re: [PATCH v7 4/4] crypto: starfive - Add hash and HMAC support

Quoted 10 lines · Herbert Xu

drivers/crypto/starfive/jh7110-hash.c line 111

Quoted 3 lines · Jia Jie Ho
drivers/crypto/starfive/jh7110-hash.c new file +2 -0
+	writel(~STARFIVE_IE_MASK_HASH_DONE, cryp->base + STARFIVE_IE_MASK_OFFSET);
+	writel(csr.v, cryp->base + STARFIVE_HASH_SHACSR);

With compile testing I get

../drivers/crypto/starfive/jh7110-hash.c: In function ‘starfive_hash_start’:
../drivers/crypto/starfive/jh7110-hash.c:110:9: warning: conversion from ‘long unsigned int’ to ‘unsigned int’ changes value from ‘18446744073709551611’ to ‘4294967291’ [-Woverflow]

Hi Herbert,
I've added COMPILE_TEST in the Kconfig, then ran make W=1, sparse and smatch. However it did not produce the error message.

+config CRYPTO_DEV_JH7110

+       tristate "StarFive JH7110 cryptographic engine driver"
+       depends on SOC_STARFIVE || COMPILE_TEST
+       select CRYPTO_ENGINE

Symbol: COMPILE_TEST [=y]
Type : bool
Defined at init/Kconfig:142

make C=2 drivers/crypto/
make W=1 drivers/crypto/
~/smatch/smatch_scripts/kchecker drivers/crypto/

Am I missing something?

Thanks,
Jia Jie

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#8 lore raw

Re: [PATCH v7 4/4] crypto: starfive - Add hash and HMAC support

Quoted 3 lines · Jia Jie Ho

I've added COMPILE_TEST in the Kconfig, then ran make W=1, sparse and smatch. However it did not produce the error message.

I guess you weren't testing on a 64-bit platform. BIT(2) is an unsigned long, so ~BIT(2) is 64-bit long on 64-bit platforms.

You're trying to feed it into writel which takes a 32-bit value, hence the warning.

If you have to use the BIT macro, then you need to cast the result to u32:

#define STARFIVE_IE_MASK_HASH_DONE ((u32)BIT(2))

But it's probably a lot clearer if you do it as:

#define STARFIVE_IE_MASK_HASH_DONE 0x4

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#9 lore raw

Re: [PATCH v7 4/4] crypto: starfive - Add hash and HMAC support

Quoted 8 lines · Herbert Xu
Quoted 3 lines · Jia Jie Ho

I've added COMPILE_TEST in the Kconfig, then ran make W=1, sparse and smatch. However it did not produce the error message.

I guess you weren't testing on a 64-bit platform. BIT(2) is an unsigned long, so ~BIT(2) is 64-bit long on 64-bit platforms.

I am cross-compiling on a 64-bit platform.
I'll check my compiler settings again in case this masks errors in future patches.

Quoted 11 lines

You're trying to feed it into writel which takes a 32-bit value, hence the warning.

If you have to use the BIT macro, then you need to cast the result to u32:

#define STARFIVE_IE_MASK_HASH_DONE ((u32)BIT(2))

But it's probably a lot clearer if you do it as:

#define STARFIVE_IE_MASK_HASH_DONE 0x4

Sure, I'll do this for the next version.

Thanks,
Jia Jie

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv