Skip to contentSkip to conversation

hw_random: introduce Arm CryptoCell TRNG driver

PATCH In review Hadar Gat 4 participants 17 messages Mar 27, 2020 – Apr 22, 2020

Subsystems (4)
lore
Share & tools
lore

Hadar Gat

author maintainer
#0 lore raw

v7 cover hw_random: introduce Arm CryptoCell TRNG driver

The Arm CryptoCell is a hardware security engine.
This patch introduces driver for its TRNG (True Random Number Generator) engine.

v7 change: in arm-cctrng.yaml, removed unneeded 'minitems'

v6 change: add missing initialization of hwrng quality.

v5 changes:
1. in arm-cctrng.yaml, fixed error in 'make dt_binding_check'
2. in cctrng.c, clean up cctrng clock handling

v4 changes: update arm-cctrng.yaml to conform with json-schema standard.

v3 change: removed few unneeded "#ifdef CONFIG_PM" from the code.

v2 changes: fixed 'make dt_bnding_check' errors.

Hadar Gat (3):
dt-bindings: add device tree binding for Arm CryptoCell trng engine
hw_random: cctrng: introduce Arm CryptoCell driver
MAINTAINERS: add HG as cctrng maintainer

 .../devicetree/bindings/rng/arm-cctrng.yaml        |  54 ++
 MAINTAINERS                                        |   9 +
 drivers/char/hw_random/Kconfig                     |  12 +
 drivers/char/hw_random/Makefile                    |   1 +
 drivers/char/hw_random/cctrng.c                    | 736 +++++++++++++++++++++
 drivers/char/hw_random/cctrng.h                    |  72 ++
 6 files changed, 884 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/arm-cctrng.yaml
 create mode 100644 drivers/char/hw_random/cctrng.c
 create mode 100644 drivers/char/hw_random/cctrng.h
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v7 1/3 dt-bindings: add device tree binding for Arm CryptoCell trng engine

The Arm CryptoCell is a hardware security engine. This patch adds DT bindings for its TRNG (True Random Number Generator) engine.

---
 .../devicetree/bindings/rng/arm-cctrng.yaml        | 54 ++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/arm-cctrng.yaml
Documentation/devicetree/bindings/rng/arm-cctrng.yaml new file +54 -0
new file mode 100644
index 0000000..ca6aad1
@@ -0,0 +1,54 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rng/arm-cctrng.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Arm TrustZone CryptoCell TRNG engine
+
+maintainers:
+  - Hadar Gat <[email protected]>
+
+description: |+
+  Arm TrustZone CryptoCell TRNG (True Random Number Generator) engine.
+
+properties:
+  compatible:
+    enum:
+      - arm,cryptocell-713-trng
+      - arm,cryptocell-703-trng
+
+  interrupts:
+    maxItems: 1
+
+  reg:
+    maxItems: 1
+
+  arm,rosc-ratio:
+    description:
+      Arm TrustZone CryptoCell TRNG engine has 4 ring oscillators.
+      Sampling ratio values for these 4 ring oscillators. (from calibration)
+    allOf:
+      - $ref: /schemas/types.yaml#/definitions/uint32-array
+      - items:
+          maxItems: 4
+
+  clocks:
+    maxItems: 1
+
+required:
+  - compatible
+  - interrupts
+  - reg
+  - arm,rosc-ratio
+
+additionalProperties: false
+
+examples:
+  - |
+    arm_cctrng: rng@60000000 {
+        compatible = "arm,cryptocell-713-trng";
+        interrupts = <0 29 4>;
+        reg = <0x60000000 0x10000>;
+        arm,rosc-ratio = <5000 1000 500 0>;
+    };
Signature · 1 line
2.7.4
maintainer Reviewed-by on v7 1/3
#2 lore raw

Re: [PATCH v7 1/3] dt-bindings: add device tree binding for Arm CryptoCell trng engine

Quoted 9 lines · Hadar Gat

The Arm CryptoCell is a hardware security engine. This patch adds DT bindings for its TRNG (True Random Number Generator) engine.

---
 .../devicetree/bindings/rng/arm-cctrng.yaml        | 54 ++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/arm-cctrng.yaml
author maintainer
#3 lore raw

PATCH v7 2/3 hw_random: cctrng: introduce Arm CryptoCell driver

In reply to Hadar Gat

Introduce low level Arm CryptoCell TRNG HW support.

---
 drivers/char/hw_random/Kconfig  |  12 +
 drivers/char/hw_random/Makefile |   1 +
 drivers/char/hw_random/cctrng.c | 736 ++++++++++++++++++++++++++++++++++++++++
 drivers/char/hw_random/cctrng.h |  72 ++++
 4 files changed, 821 insertions(+)
 create mode 100644 drivers/char/hw_random/cctrng.c
 create mode 100644 drivers/char/hw_random/cctrng.h
drivers/char/hw_random/Kconfig +12 -0
index 9bc46da..848f26f 100644
@@ -474,6 +474,18 @@ config HW_RANDOM_KEYSTONE
 	help
 	  This option enables Keystone's hardware random generator.
 
+config HW_RANDOM_CCTRNG
+	tristate "Arm CryptoCell True Random Number Generator support"
+	default HW_RANDOM
+	help
+	  This driver provides support for the True Random Number
+	  Generator available in Arm TrustZone CryptoCell.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called cctrng.
+
+	  If unsure, say Y.
+
 endif # HW_RANDOM
 
 config UML_RANDOM
drivers/char/hw_random/Makefile +1 -0
index a7801b4..2c67247 100644
@@ -41,3 +41,4 @@ obj-$(CONFIG_HW_RANDOM_S390) += s390-trng.o
 obj-$(CONFIG_HW_RANDOM_KEYSTONE) += ks-sa-rng.o
 obj-$(CONFIG_HW_RANDOM_OPTEE) += optee-rng.o
 obj-$(CONFIG_HW_RANDOM_NPCM) += npcm-rng.o
+obj-$(CONFIG_HW_RANDOM_CCTRNG) += cctrng.o
drivers/char/hw_random/cctrng.c new file +736 -0

736 lines not on this page · raw message

drivers/char/hw_random/cctrng.h new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.7.4
#4 lore raw

Re: [PATCH v7 2/3] hw_random: cctrng: introduce Arm CryptoCell driver

Hi Hadar,

Quoted 3 lines · Hadar Gat

Introduce low level Arm CryptoCell TRNG HW support.

Thanks for your patch!

Quoted 60 lines
drivers/char/hw_random/cctrng.c new file +57 -0
 
+static int cctrng_probe(struct platform_device *pdev)
+{
+       struct resource *req_mem_cc_regs = NULL;
+       struct cctrng_drvdata *drvdata;
+       struct device *dev = &pdev->dev;
+       int rc = 0;
+       u32 val;
+       int irq;
+
+       drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       drvdata->rng.name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
+       if (!drvdata->rng.name)
+               return -ENOMEM;
+
+       drvdata->rng.read = cctrng_read;
+       drvdata->rng.priv = (unsigned long)drvdata;
+       drvdata->rng.quality = CC_TRNG_QUALITY;
+
+       platform_set_drvdata(pdev, drvdata);
+       drvdata->pdev = pdev;
+
+       drvdata->circ.buf = (char *)drvdata->data_buf;
+
+       /* Get device resources */
+       /* First CC registers space */
+       req_mem_cc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       /* Map registers space */
+       drvdata->cc_base = devm_ioremap_resource(dev, req_mem_cc_regs);
+       if (IS_ERR(drvdata->cc_base)) {
+               dev_err(dev, "Failed to ioremap registers");
+               return PTR_ERR(drvdata->cc_base);
+       }
+
+       dev_dbg(dev, "Got MEM resource (%s): %pR\n", req_mem_cc_regs->name,
+               req_mem_cc_regs);
+       dev_dbg(dev, "CC registers mapped from %pa to 0x%p\n",
+               &req_mem_cc_regs->start, drvdata->cc_base);
+
+       /* Then IRQ */
+       irq = platform_get_irq(pdev, 0);
+       if (irq < 0) {
+               dev_err(dev, "Failed getting IRQ resource\n");
+               return irq;
+       }
+
+       /* parse sampling rate from device tree */
+       rc = cc_trng_parse_sampling_ratio(drvdata);
+       if (rc) {
+               dev_err(dev, "Failed to get legal sampling ratio for rosc\n");
+               return rc;
+       }
+
+       rc = cc_trng_clk_init(drvdata);
+       if (rc) {

drivers/char/hw_random/cctrng.c line 555

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+               dev_err(dev, "cc_trng_clk_init failed\n");
+               return rc;
+       }
+
+       INIT_WORK(&drvdata->compwork, cc_trng_compwork_handler);
+       INIT_WORK(&drvdata->startwork, cc_trng_startwork_handler);
+       spin_lock_init(&drvdata->read_lock);
+
+       /* register the driver isr function */
+       rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED, "cctrng", drvdata);

Shoudn't this be done after clearing the pending interrupts below?

drivers/char/hw_random/cctrng.c line 565

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       if (rc) {
+               dev_err(dev, "Could not register to interrupt %d\n", irq);
+               goto post_clk_err;
+       }
+       dev_dbg(dev, "Registered to IRQ: %d\n", irq);
+
+       /* Clear all pending interrupts */
+       val = cc_ioread(drvdata, CC_HOST_RGF_IRR_REG_OFFSET);
+       dev_dbg(dev, "IRR=0x%08X\n", val);
+       cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET, val);

The above accesses the engine's registers...

Quoted 6 lines
drivers/char/hw_random/cctrng.c new file +6 -0
+
+       /* unmask HOST RNG interrupt */
+       cc_iowrite(drvdata, CC_HOST_RGF_IMR_REG_OFFSET,
+                  cc_ioread(drvdata, CC_HOST_RGF_IMR_REG_OFFSET) &
+                  ~CC_HOST_RNG_IRQ_MASK);
+

drivers/char/hw_random/cctrng.c line 580

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +9 -0
+       /* init PM */
+       rc = cc_trng_pm_init(drvdata);
+       if (rc) {
+               dev_err(dev, "cc_trng_pm_init failed\n");
+               goto post_clk_err;
+       }
 
+
+       /* increment device's usage counter */
+       rc = cc_trng_pm_get(dev);

... but only here is Runtime PM initialized, and the device guaranteed to be powered. If a device is accessed while powered down, this may lead to an asynchronous external abort, or a plain lockup.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#5 lore raw

RE: [PATCH v7 2/3] hw_random: cctrng: introduce Arm CryptoCell driver

Thank you Geert for reviewing the cctrng code.

Quoted 81 lines

-----Original Message-----
From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 16:45

Hi Hadar,

Quoted 3 lines · Hadar Gat

Introduce low level Arm CryptoCell TRNG HW support.

Thanks for your patch!

Quoted 6 lines
drivers/char/hw_random/cctrng.c new file +3 -0
 
+static int cctrng_probe(struct platform_device *pdev) {
+       struct resource *req_mem_cc_regs = NULL;
+       struct cctrng_drvdata *drvdata;

drivers/char/hw_random/cctrng.c

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       struct device *dev = &pdev->dev;
+       int rc = 0;
+       u32 val;
+       int irq;
+
+       drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       drvdata->rng.name = devm_kstrdup(dev, dev_name(dev),

GFP_KERNEL);

Quoted 5 lines
drivers/char/hw_random/cctrng.c new file +5 -0
+       if (!drvdata->rng.name)
+               return -ENOMEM;
+
+       drvdata->rng.read = cctrng_read;
+       drvdata->rng.priv = (unsigned long)drvdata;

drivers/char/hw_random/cctrng.c

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       drvdata->rng.quality = CC_TRNG_QUALITY;
+
+       platform_set_drvdata(pdev, drvdata);
+       drvdata->pdev = pdev;
+
+       drvdata->circ.buf = (char *)drvdata->data_buf;
+
+       /* Get device resources */
+       /* First CC registers space */
+       req_mem_cc_regs = platform_get_resource(pdev,

IORESOURCE_MEM, 0);

drivers/char/hw_random/cctrng.c

Quoted 2 lines
drivers/char/hw_random/cctrng.c new file +2 -0
+       /* Map registers space */
+       drvdata->cc_base = devm_ioremap_resource(dev,

req_mem_cc_regs);

Quoted 32 lines
drivers/char/hw_random/cctrng.c new file +6 -0
+       if (IS_ERR(drvdata->cc_base)) {
+               dev_err(dev, "Failed to ioremap registers");
+               return PTR_ERR(drvdata->cc_base);
+       }
+
+       dev_dbg(dev, "Got MEM resource (%s): %pR\n", req_mem_cc_regs-

name,

drivers/char/hw_random/cctrng.c new file +25 -0
+               req_mem_cc_regs);
+       dev_dbg(dev, "CC registers mapped from %pa to 0x%p\n",
+               &req_mem_cc_regs->start, drvdata->cc_base);
+
+       /* Then IRQ */
+       irq = platform_get_irq(pdev, 0);
+       if (irq < 0) {
+               dev_err(dev, "Failed getting IRQ resource\n");
+               return irq;
+       }
+
+       /* parse sampling rate from device tree */
+       rc = cc_trng_parse_sampling_ratio(drvdata);
+       if (rc) {
+               dev_err(dev, "Failed to get legal sampling ratio for rosc\n");
+               return rc;
+       }
+
+       rc = cc_trng_clk_init(drvdata);
+       if (rc) {
+               dev_err(dev, "cc_trng_clk_init failed\n");
+               return rc;
+       }
+
+       INIT_WORK(&drvdata->compwork, cc_trng_compwork_handler);
Quoted 8 lines

drivers/char/hw_random/cctrng.c

Quoted 6 lines
drivers/char/hw_random/cctrng.c new file +6 -0
+       INIT_WORK(&drvdata->startwork, cc_trng_startwork_handler);
+       spin_lock_init(&drvdata->read_lock);
+
+       /* register the driver isr function */
+       rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED, "cctrng",
+ drvdata);

Shoudn't this be done after clearing the pending interrupts below?

I'm not sure what do you mean in your question...
I assume you're suggesting that the registration of the driver ISR function should be done only after clearing the pending interrupts?! Anyway, any pending interrupt that might exist is irrelevant to the current cctrng driver which just started (we're in the probe function)

Quoted 11 lines

drivers/char/hw_random/cctrng.c line 565

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       if (rc) {
+               dev_err(dev, "Could not register to interrupt %d\n", irq);
+               goto post_clk_err;
+       }
+       dev_dbg(dev, "Registered to IRQ: %d\n", irq);
+
+       /* Clear all pending interrupts */
+       val = cc_ioread(drvdata, CC_HOST_RGF_IRR_REG_OFFSET);
+       dev_dbg(dev, "IRR=0x%08X\n", val);
+       cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET, val);
Quoted 1 line

The above accesses the engine's registers...

That is right.

Quoted 13 lines
Quoted 12 lines
drivers/char/hw_random/cctrng.c new file +12 -0
+
+       /* unmask HOST RNG interrupt */
+       cc_iowrite(drvdata, CC_HOST_RGF_IMR_REG_OFFSET,
+                  cc_ioread(drvdata, CC_HOST_RGF_IMR_REG_OFFSET) &
+                  ~CC_HOST_RNG_IRQ_MASK);
+
+       /* init PM */
+       rc = cc_trng_pm_init(drvdata);
+       if (rc) {
+               dev_err(dev, "cc_trng_pm_init failed\n");
+               goto post_clk_err;
+       }
Quoted 7 lines

drivers/char/hw_random/cctrng.c line 580

Quoted 3 lines
drivers/char/hw_random/cctrng.c new file +3 -0
+
+       /* increment device's usage counter */
+       rc = cc_trng_pm_get(dev);

... but only here is Runtime PM initialized, and the device guaranteed to be powered. If a device is accessed while powered down, this may lead to an asynchronous external abort, or a plain lockup.

It is assumed that when the driver is probed it is already powered. Only then, the driver initializes and enables the runtime PM to allow power down of the HW when it is not in use.

Quoted 12 lines

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux- m68k.org

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#6 lore raw

Re: [PATCH v7 2/3] hw_random: cctrng: introduce Arm CryptoCell driver

Hi Hadar,

Quoted 88 lines · Hadar Gat
Quoted 79 lines

-----Original Message-----
From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 16:45

Quoted 3 lines · Hadar Gat

Introduce low level Arm CryptoCell TRNG HW support.

Thanks for your patch!

Quoted 6 lines
drivers/char/hw_random/cctrng.c new file +3 -0
 
+static int cctrng_probe(struct platform_device *pdev) {
+       struct resource *req_mem_cc_regs = NULL;
+       struct cctrng_drvdata *drvdata;

drivers/char/hw_random/cctrng.c

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       struct device *dev = &pdev->dev;
+       int rc = 0;
+       u32 val;
+       int irq;
+
+       drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       drvdata->rng.name = devm_kstrdup(dev, dev_name(dev),

GFP_KERNEL);

Quoted 5 lines
drivers/char/hw_random/cctrng.c new file +5 -0
+       if (!drvdata->rng.name)
+               return -ENOMEM;
+
+       drvdata->rng.read = cctrng_read;
+       drvdata->rng.priv = (unsigned long)drvdata;

drivers/char/hw_random/cctrng.c

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       drvdata->rng.quality = CC_TRNG_QUALITY;
+
+       platform_set_drvdata(pdev, drvdata);
+       drvdata->pdev = pdev;
+
+       drvdata->circ.buf = (char *)drvdata->data_buf;
+
+       /* Get device resources */
+       /* First CC registers space */
+       req_mem_cc_regs = platform_get_resource(pdev,

IORESOURCE_MEM, 0);

drivers/char/hw_random/cctrng.c

Quoted 2 lines
drivers/char/hw_random/cctrng.c new file +2 -0
+       /* Map registers space */
+       drvdata->cc_base = devm_ioremap_resource(dev,

req_mem_cc_regs);

Quoted 32 lines
drivers/char/hw_random/cctrng.c new file +6 -0
+       if (IS_ERR(drvdata->cc_base)) {
+               dev_err(dev, "Failed to ioremap registers");
+               return PTR_ERR(drvdata->cc_base);
+       }
+
+       dev_dbg(dev, "Got MEM resource (%s): %pR\n", req_mem_cc_regs-

name,

drivers/char/hw_random/cctrng.c new file +25 -0
+               req_mem_cc_regs);
+       dev_dbg(dev, "CC registers mapped from %pa to 0x%p\n",
+               &req_mem_cc_regs->start, drvdata->cc_base);
+
+       /* Then IRQ */
+       irq = platform_get_irq(pdev, 0);
+       if (irq < 0) {
+               dev_err(dev, "Failed getting IRQ resource\n");
+               return irq;
+       }
+
+       /* parse sampling rate from device tree */
+       rc = cc_trng_parse_sampling_ratio(drvdata);
+       if (rc) {
+               dev_err(dev, "Failed to get legal sampling ratio for rosc\n");
+               return rc;
+       }
+
+       rc = cc_trng_clk_init(drvdata);
+       if (rc) {
+               dev_err(dev, "cc_trng_clk_init failed\n");
+               return rc;
+       }
+
+       INIT_WORK(&drvdata->compwork, cc_trng_compwork_handler);
Quoted 8 lines

drivers/char/hw_random/cctrng.c

Quoted 6 lines
drivers/char/hw_random/cctrng.c new file +6 -0
+       INIT_WORK(&drvdata->startwork, cc_trng_startwork_handler);
+       spin_lock_init(&drvdata->read_lock);
+
+       /* register the driver isr function */
+       rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED, "cctrng",
+ drvdata);

Shoudn't this be done after clearing the pending interrupts below?

Quoted 2 lines

I'm not sure what do you mean in your question...
I assume you're suggesting that the registration of the driver ISR function should be done only after clearing the pending interrupts?!

Indeed.

Quoted 1 line

Anyway, any pending interrupt that might exist is irrelevant to the current cctrng driver which just started (we're in the probe function)

If there is a pending interrupt, your interrupt handler (which returns IRQ_NONE in this case) will be called repeatedly, until the driver gets to clearing the pending interrupts below, or until the interrupt core decides to give up, and disable it for good.

Quoted 32 lines
Quoted 11 lines

drivers/char/hw_random/cctrng.c line 565

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       if (rc) {
+               dev_err(dev, "Could not register to interrupt %d\n", irq);
+               goto post_clk_err;
+       }
+       dev_dbg(dev, "Registered to IRQ: %d\n", irq);
+
+       /* Clear all pending interrupts */
+       val = cc_ioread(drvdata, CC_HOST_RGF_IRR_REG_OFFSET);
+       dev_dbg(dev, "IRR=0x%08X\n", val);
+       cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET, val);
Quoted 1 line

The above accesses the engine's registers...

That is right.

Quoted 17 lines
Quoted 16 lines
drivers/char/hw_random/cctrng.c new file +15 -0
+
+       /* unmask HOST RNG interrupt */
+       cc_iowrite(drvdata, CC_HOST_RGF_IMR_REG_OFFSET,
+                  cc_ioread(drvdata, CC_HOST_RGF_IMR_REG_OFFSET) &
+                  ~CC_HOST_RNG_IRQ_MASK);
+
+       /* init PM */
+       rc = cc_trng_pm_init(drvdata);
+       if (rc) {
+               dev_err(dev, "cc_trng_pm_init failed\n");
+               goto post_clk_err;
+       }
 
+
+       /* increment device's usage counter */
+       rc = cc_trng_pm_get(dev);
Quoted 5 lines
Quoted 3 lines

... but only here is Runtime PM initialized, and the device guaranteed to be powered. If a device is accessed while powered down, this may lead to an asynchronous external abort, or a plain lockup.

It is assumed that when the driver is probed it is already powered. Only then, the driver initializes and enables the runtime PM to allow power down of the HW when it is not in use.

Who guarantees it is powered up? Your driver has indeed enabled the (optional) clock above, but if the hardware block is part of a power domain, it may still be powered down. The only way to make sure a hardware block in a power domain is powered, is by enabling Runtime PM and calling pm_runtime_get_sync().

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#7 lore raw

RE: [PATCH v7 2/3] hw_random: cctrng: introduce Arm CryptoCell driver

Quoted 100 lines

From: Geert Uytterhoeven <[email protected]>

Hi Hadar,

Quoted 88 lines · Hadar Gat
Quoted 79 lines

From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 16:45

Quoted 3 lines · Hadar Gat

Introduce low level Arm CryptoCell TRNG HW support.

Thanks for your patch!

Quoted 6 lines
drivers/char/hw_random/cctrng.c new file +3 -0
 
+static int cctrng_probe(struct platform_device *pdev) {
+       struct resource *req_mem_cc_regs = NULL;
+       struct cctrng_drvdata *drvdata;

drivers/char/hw_random/cctrng.c

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       struct device *dev = &pdev->dev;
+       int rc = 0;
+       u32 val;
+       int irq;
+
+       drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       drvdata->rng.name = devm_kstrdup(dev, dev_name(dev),

GFP_KERNEL);

Quoted 5 lines
drivers/char/hw_random/cctrng.c new file +5 -0
+       if (!drvdata->rng.name)
+               return -ENOMEM;
+
+       drvdata->rng.read = cctrng_read;
+       drvdata->rng.priv = (unsigned long)drvdata;

drivers/char/hw_random/cctrng.c

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       drvdata->rng.quality = CC_TRNG_QUALITY;
+
+       platform_set_drvdata(pdev, drvdata);
+       drvdata->pdev = pdev;
+
+       drvdata->circ.buf = (char *)drvdata->data_buf;
+
+       /* Get device resources */
+       /* First CC registers space */
+       req_mem_cc_regs = platform_get_resource(pdev,

IORESOURCE_MEM, 0);

drivers/char/hw_random/cctrng.c

Quoted 2 lines
drivers/char/hw_random/cctrng.c new file +2 -0
+       /* Map registers space */
+       drvdata->cc_base = devm_ioremap_resource(dev,

req_mem_cc_regs);

Quoted 33 lines
drivers/char/hw_random/cctrng.c new file +7 -0
+       if (IS_ERR(drvdata->cc_base)) {
+               dev_err(dev, "Failed to ioremap registers");
+               return PTR_ERR(drvdata->cc_base);
+       }
+
+       dev_dbg(dev, "Got MEM resource (%s): %pR\n",
+ req_mem_cc_regs-

name,

drivers/char/hw_random/cctrng.c new file +25 -0
+               req_mem_cc_regs);
+       dev_dbg(dev, "CC registers mapped from %pa to 0x%p\n",
+               &req_mem_cc_regs->start, drvdata->cc_base);
+
+       /* Then IRQ */
+       irq = platform_get_irq(pdev, 0);
+       if (irq < 0) {
+               dev_err(dev, "Failed getting IRQ resource\n");
+               return irq;
+       }
+
+       /* parse sampling rate from device tree */
+       rc = cc_trng_parse_sampling_ratio(drvdata);
+       if (rc) {
+               dev_err(dev, "Failed to get legal sampling ratio for rosc\n");
+               return rc;
+       }
+
+       rc = cc_trng_clk_init(drvdata);
+       if (rc) {
+               dev_err(dev, "cc_trng_clk_init failed\n");
+               return rc;
+       }
+
+       INIT_WORK(&drvdata->compwork, cc_trng_compwork_handler);
Quoted 8 lines

drivers/char/hw_random/cctrng.c

Quoted 6 lines
drivers/char/hw_random/cctrng.c new file +6 -0
+       INIT_WORK(&drvdata->startwork, cc_trng_startwork_handler);
+       spin_lock_init(&drvdata->read_lock);
+
+       /* register the driver isr function */
+       rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED,
+ "cctrng", drvdata);

Shoudn't this be done after clearing the pending interrupts below?

Quoted 2 lines

I'm not sure what do you mean in your question...
I assume you're suggesting that the registration of the driver ISR function

should be done only after clearing the pending interrupts?!

Indeed.

Quoted 7 lines
Quoted 2 lines

Anyway, any pending interrupt that might exist is irrelevant to the current cctrng driver which just started (we're in the probe function)

If there is a pending interrupt, your interrupt handler (which returns IRQ_NONE in this case) will be called repeatedly, until the driver gets to clearing the pending interrupts below, or until the interrupt core decides to give up, and disable it for good.

Ok, I get your point now.
But note that when the cctrng HW boots, the default is that all interrupts are masked, hence the interrupt handler will not be called. The unmask of the RNG interrupts is done afterwards and only then ISR may potentially be called.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

drivers/char/hw_random/cctrng.c line 565

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       if (rc) {
+               dev_err(dev, "Could not register to interrupt %d\n", irq);
+               goto post_clk_err;
+       }
+       dev_dbg(dev, "Registered to IRQ: %d\n", irq);
+
+       /* Clear all pending interrupts */
+       val = cc_ioread(drvdata, CC_HOST_RGF_IRR_REG_OFFSET);
+       dev_dbg(dev, "IRR=0x%08X\n", val);
+       cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET, val);

The above accesses the engine's registers...

Quoted 7 lines
Quoted 7 lines

That is right.

Quoted 5 lines
Quoted 5 lines
drivers/char/hw_random/cctrng.c new file +5 -0
+
+       /* unmask HOST RNG interrupt */
+       cc_iowrite(drvdata, CC_HOST_RGF_IMR_REG_OFFSET,
+                  cc_ioread(drvdata, CC_HOST_RGF_IMR_REG_OFFSET) &
+                  ~CC_HOST_RNG_IRQ_MASK);

The above unmask the RNG interrupt.

Quoted 17 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+
+       /* init PM */
+       rc = cc_trng_pm_init(drvdata);
+       if (rc) {
+               dev_err(dev, "cc_trng_pm_init failed\n");
+               goto post_clk_err;
+       }
 
+
+       /* increment device's usage counter */
+       rc = cc_trng_pm_get(dev);
Quoted 5 lines
Quoted 3 lines

... but only here is Runtime PM initialized, and the device guaranteed to be powered. If a device is accessed while powered down, this may lead to an asynchronous external abort, or a plain lockup.

It is assumed that when the driver is probed it is already powered. Only

Quoted 8 lines

then, the driver initializes and enables the runtime PM to allow power down of the HW when it is not in use.

Who guarantees it is powered up? Your driver has indeed enabled the (optional) clock above, but if the hardware block is part of a power domain, it may still be powered down. The only way to make sure a hardware block in a power domain is powered, is by enabling Runtime PM and calling pm_runtime_get_sync().

Got it now and indeed this need to be fixed.
I reviewed a similar fix that you did in ccree, commit 8c7849a30255cfd9a9ba412b1517e20a8572448b. Now I understand that the initialization of runtime-PM should be done before any register access. I will fix this in another patch.

Thanks a lot for your review and valuable inputs.
Hadar

#8 lore raw

Re: [PATCH v7 2/3] hw_random: cctrng: introduce Arm CryptoCell driver

Hi Hadar,

Quoted 26 lines · Hadar Gat
Quoted 26 lines

From: Geert Uytterhoeven <[email protected]>

Quoted 9 lines · Hadar Gat
Quoted 8 lines

From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 16:45

Quoted 5 lines · Hadar Gat

Introduce low level Arm CryptoCell TRNG HW support.

drivers/char/hw_random/cctrng.c new file +1 -0
 
+static int cctrng_probe(struct platform_device *pdev) {
Quoted 8 lines
Quoted 5 lines

drivers/char/hw_random/cctrng.c

Quoted 3 lines
drivers/char/hw_random/cctrng.c new file +3 -0
+       /* register the driver isr function */
+       rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED,
+ "cctrng", drvdata);

Shoudn't this be done after clearing the pending interrupts below?

I'm not sure what do you mean in your question...
I assume you're suggesting that the registration of the driver ISR function

should be done only after clearing the pending interrupts?!

Indeed.

Quoted 2 lines

Anyway, any pending interrupt that might exist is irrelevant to the current cctrng driver which just started (we're in the probe function)

Quoted 7 lines
Quoted 4 lines

If there is a pending interrupt, your interrupt handler (which returns IRQ_NONE in this case) will be called repeatedly, until the driver gets to clearing the pending interrupts below, or until the interrupt core decides to give up, and disable it for good.

Ok, I get your point now.
But note that when the cctrng HW boots, the default is that all interrupts are masked, hence the interrupt handler will not be called.

Is that also the case when booting into a new kernel using kexec?

Quoted 17 lines

The unmask of the RNG interrupts is done afterwards and only then ISR may potentially be called.

Quoted 15 lines
Quoted 15 lines
Quoted 11 lines

drivers/char/hw_random/cctrng.c line 565

Quoted 10 lines
drivers/char/hw_random/cctrng.c new file +10 -0
+       if (rc) {
+               dev_err(dev, "Could not register to interrupt %d\n", irq);
+               goto post_clk_err;
+       }
+       dev_dbg(dev, "Registered to IRQ: %d\n", irq);
+
+       /* Clear all pending interrupts */
+       val = cc_ioread(drvdata, CC_HOST_RGF_IRR_REG_OFFSET);
+       dev_dbg(dev, "IRR=0x%08X\n", val);
+       cc_iowrite(drvdata, CC_HOST_RGF_ICR_REG_OFFSET, val);
Quoted 1 line

The above accesses the engine's registers...

That is right.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/char/hw_random/cctrng.c line 570

Quoted 5 lines
drivers/char/hw_random/cctrng.c new file +5 -0
+
+       /* unmask HOST RNG interrupt */
+       cc_iowrite(drvdata, CC_HOST_RGF_IMR_REG_OFFSET,
+                  cc_ioread(drvdata, CC_HOST_RGF_IMR_REG_OFFSET) &
+                  ~CC_HOST_RNG_IRQ_MASK);

The above unmask the RNG interrupt.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#9 lore raw

RE: [PATCH v7 2/3] hw_random: cctrng: introduce Arm CryptoCell driver

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 11 lines
Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+static int cctrng_probe(struct platform_device *pdev) {
 
+       /* register the driver isr function */
+       rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED,
+ "cctrng", drvdata);

Shoudn't this be done after clearing the pending interrupts below?

I'm not sure what do you mean in your question...
I assume you're suggesting that the registration of the driver ISR function

should be done only after clearing the pending interrupts?!

Indeed.

Quoted 3 lines

Anyway, any pending interrupt that might exist is irrelevant to the current cctrng driver which just started (we're in the probe function)

If there is a pending interrupt, your interrupt handler (which returns IRQ_NONE in this case) will be called repeatedly, until the driver gets to clearing the pending interrupts below, or until the interrupt core decides to give up, and disable it for good.

Quoted 5 lines
Quoted 2 lines

Ok, I get your point now.
But note that when the cctrng HW boots, the default is that all interrupts

are masked, hence the interrupt handler will not be called.

Is that also the case when booting into a new kernel using kexec?

Well.. no. ☹
A fix is needed here to consider also the case of kexec. I'll fix that in another patch.
Thanks!

author maintainer
#10 lore raw

PATCH v7 3/3 MAINTAINERS: add HG as cctrng maintainer

In reply to Hadar Gat

I work for Arm on maintaining the TrustZone CryptoCell TRNG driver.

---
 MAINTAINERS | 9 +++++++++
 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
index c312b65..3f27716 100644
@@ -3847,6 +3847,15 @@ S:	Supported
 F:	drivers/crypto/ccree/
 W:	https://developer.arm.com/products/system-ip/trustzone-cryptocell/cryptocell-700-family
 
+CCTRNG ARM TRUSTZONE CRYPTOCELL TRUE RANDOM NUMBER GENERATOR (TRNG) DRIVER
+M:	Hadar Gat <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/char/hw_random/cctrng.c
+F:	drivers/char/hw_random/cctrng.h
+F:	Documentation/devicetree/bindings/rng/arm-cctrng.txt
+W:	https://developer.arm.com/products/system-ip/trustzone-cryptocell/cryptocell-700-family
+
 CEC FRAMEWORK
 M:	Hans Verkuil <[email protected]>
 L:	[email protected]
Signature · 1 line
2.7.4
maintainer
#11 lore raw
In reply to Hadar Gat
Quoted 23 lines · Hadar Gat

The Arm CryptoCell is a hardware security engine.
This patch introduces driver for its TRNG (True Random Number Generator) engine.

v7 change: in arm-cctrng.yaml, removed unneeded 'minitems'

v6 change: add missing initialization of hwrng quality.

v5 changes:
1. in arm-cctrng.yaml, fixed error in 'make dt_binding_check'
2. in cctrng.c, clean up cctrng clock handling

v4 changes: update arm-cctrng.yaml to conform with json-schema standard.

v3 change: removed few unneeded "#ifdef CONFIG_PM" from the code.

v2 changes: fixed 'make dt_bnding_check' errors.

Hadar Gat (3):
dt-bindings: add device tree binding for Arm CryptoCell trng engine
hw_random: cctrng: introduce Arm CryptoCell driver
MAINTAINERS: add HG as cctrng maintainer

Quoted 10 lines
 .../devicetree/bindings/rng/arm-cctrng.yaml        |  54 ++
 MAINTAINERS                                        |   9 +
 drivers/char/hw_random/Kconfig                     |  12 +
 drivers/char/hw_random/Makefile                    |   1 +
 drivers/char/hw_random/cctrng.c                    | 736 +++++++++++++++++++++
 drivers/char/hw_random/cctrng.h                    |  72 ++
 6 files changed, 884 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rng/arm-cctrng.yaml
 create mode 100644 drivers/char/hw_random/cctrng.c
 create mode 100644 drivers/char/hw_random/cctrng.h

All applied. 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
#12 lore raw
In reply to Hadar Gat

Hi Hadar,

Quoted 3 lines · Hadar Gat

The Arm CryptoCell is a hardware security engine.
This patch introduces driver for its TRNG (True Random Number Generator) engine.

Thanks for your series!

I am wondering what is the relation between this and Documentation/devicetree/bindings/crypto/arm-cryptocell.txt?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#13 lore raw

Hi Geert,

Quoted 6 lines

From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 12:35

Hi Hadar,

On Fri, Mar 27, 2020 at 7:11 AM Hadar Gat <[email protected]> wrote:

Quoted 8 lines
Quoted 3 lines

The Arm CryptoCell is a hardware security engine. This patch introduces driver for its TRNG (True Random Number Generator) engine.

Thanks for your series!

I am wondering what is the relation between this and Documentation/devicetree/bindings/crypto/arm-cryptocell.txt?

Arm TrustZone CryptoCell hardware contains both cryptographic engine (ccree) and true random number generator engine (cctrng). These are separate engines with some sharing in logic and interface.
cctrng engine may not always be present.
The devicetree documentation is in:
For ccree - Documentation/devicetree/bindings/crypto/arm-cryptocell.txt
For cctrng - Documentation/devicetree/bindings/rng/arm-cctrng.yaml

Quoted 4 lines

Gr{oetje,eeting}s,

Geert

Hadar

#14 lore raw

Hi Hadar,

Quoted 8 lines · Hadar Gat
Quoted 8 lines

From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 12:35

Quoted 3 lines · Hadar Gat

The Arm CryptoCell is a hardware security engine. This patch introduces driver for its TRNG (True Random Number Generator) engine.

Quoted 6 lines
Quoted 4 lines

Thanks for your series!

I am wondering what is the relation between this and Documentation/devicetree/bindings/crypto/arm-cryptocell.txt?

Arm TrustZone CryptoCell hardware contains both cryptographic engine (ccree) and true random number generator engine (cctrng).

OK.

Quoted 1 line

These are separate engines with some sharing in logic and interface.

Do they share the same register block?

Quoted 1 line

cctrng engine may not always be present.

I assume that applies to e.g. the older 630p?

Quoted 3 lines

The devicetree documentation is in:
For ccree - Documentation/devicetree/bindings/crypto/arm-cryptocell.txt For cctrng - Documentation/devicetree/bindings/rng/arm-cctrng.yaml

Thank you, I had already read both documents.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author maintainer
#15 lore raw

Hi Geert,

To better explain the relationship between ccree and cctrng drivers, here an description of the underlying hardware and the relationship to the two drivers:

Arm TrustZone CryptoCell is a hardware block that implements two separate and discreet, although related, interfaces: one for the Rich Execution Environment (read: Linux) and the other for the Trusted Execution Environment (e.g. Trusty, Op-TEE).

The ccree driver exposes the REE interface of CryptoCell to Linux. Where a SoC vendor implements both REE and TEE in their design, that is all that is needed.

However, we have some customers that make use CryptoCell but never implement a Trusted Execution Environment. This is a design decision taken when the SoC hardware is being designed and not a software controlled configuration, as it involves how the buses are laid out. Some of these customers have requested from us to allow making use in Linux of the TRNG resources which are normally associated with the TEE side when it is not in use. For these customers, the cctrng driver allows making use in Linux the TRNG which is normally part of the TEE side of CryptoCell.

I am guessing based on market segment that this is NOT the case with the Renesas boards you are maintain.

Nevertheless, we of course very much welcome your review, input and contribution that has proved extremely valuable with the ccree driver. 🙂

BR,
Hadar & Gilad

Quoted 51 lines

-----Original Message-----
From: Geert Uytterhoeven <[email protected]>

Hi Hadar,

Quoted 8 lines · Hadar Gat
Quoted 8 lines

From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 12:35

Quoted 3 lines · Hadar Gat

The Arm CryptoCell is a hardware security engine. This patch introduces driver for its TRNG (True Random Number Generator) engine.

Quoted 6 lines
Quoted 4 lines

Thanks for your series!

I am wondering what is the relation between this and Documentation/devicetree/bindings/crypto/arm-cryptocell.txt?

Arm TrustZone CryptoCell hardware contains both cryptographic engine

(ccree) and true random number generator engine (cctrng).

OK.

Quoted 1 line

These are separate engines with some sharing in logic and interface.

Do they share the same register block?

Quoted 1 line

cctrng engine may not always be present.

I assume that applies to e.g. the older 630p?

Quoted 4 lines

The devicetree documentation is in:
For ccree - Documentation/devicetree/bindings/crypto/arm-cryptocell.txt For cctrng - Documentation/devicetree/bindings/rng/arm-cctrng.yaml

Thank you, I had already read both documents.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux- m68k.org

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#16 lore raw

Hi Hadar (and Gilad),

Quoted 7 lines · Hadar Gat

To better explain the relationship between ccree and cctrng drivers, here an description of the underlying hardware and the relationship to the two drivers:

Arm TrustZone CryptoCell is a hardware block that implements two separate and discreet, although related, interfaces: one for the Rich Execution Environment (read: Linux) and the other for the Trusted Execution Environment (e.g. Trusty, Op-TEE).

The ccree driver exposes the REE interface of CryptoCell to Linux. Where a SoC vendor implements both REE and TEE in their design, that is all that is needed.

However, we have some customers that make use CryptoCell but never implement a Trusted Execution Environment. This is a design decision taken when the SoC hardware is being designed and not a software controlled configuration, as it involves how the buses are laid out. Some of these customers have requested from us to allow making use in Linux of the TRNG resources which are normally associated with the TEE side when it is not in use. For these customers, the cctrng driver allows making use in Linux the TRNG which is normally part of the TEE side of CryptoCell.

Thank you, that is the part I was missing.

BTW, there seems to be no mention of CryptoCell 630 on arm.com; it covers only CC-300 and CC-700.
But from the (very limited) information about the crypto engine on R-Car Gen3 SoCs, it looks like the RNG is indeed only present in the secure (trusted) part.

Quoted 27 lines
Quoted 27 lines

-----Original Message-----
From: Geert Uytterhoeven <[email protected]>

Quoted 8 lines · Hadar Gat
Quoted 8 lines

From: Geert Uytterhoeven <[email protected]>
Sent: Monday, 20 April 2020 12:35

Quoted 3 lines · Hadar Gat

The Arm CryptoCell is a hardware security engine. This patch introduces driver for its TRNG (True Random Number Generator) engine.

Quoted 6 lines
Quoted 4 lines

Thanks for your series!

I am wondering what is the relation between this and Documentation/devicetree/bindings/crypto/arm-cryptocell.txt?

Arm TrustZone CryptoCell hardware contains both cryptographic engine

(ccree) and true random number generator engine (cctrng).

OK.

Quoted 1 line

These are separate engines with some sharing in logic and interface.

Do they share the same register block?

Quoted 1 line

cctrng engine may not always be present.

Quoted 8 lines
Quoted 8 lines

I assume that applies to e.g. the older 630p?

Quoted 4 lines

The devicetree documentation is in:
For ccree - Documentation/devicetree/bindings/crypto/arm-cryptocell.txt For cctrng - Documentation/devicetree/bindings/rng/arm-cctrng.yaml

Thank you, I had already read both documents.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds