From: Daniel Thompson <daniel.thompson@linaro.org>
To: Lee Jones <lee.jones@linaro.org>
Cc: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
herbert@gondor.apana.org.au, linux-crypto@vger.kernel.org,
peter@korsgaard.com, festevam@gmail.com, kieranbingham@gmail.com,
kernel@stlinux.com, Pankaj Dev <pankaj.dev@st.com>
Subject: Re: [PATCH v2 5/7] hwrng: st: Add support for ST's HW Random Number Generator
Date: Mon, 5 Oct 2015 13:54:04 +0100 [thread overview]
Message-ID: <561272EC.6080300@linaro.org> (raw)
In-Reply-To: <20151005121102.GB17172@x1>
On 05/10/15 13:11, Lee Jones wrote:
> On Mon, 05 Oct 2015, Daniel Thompson wrote:
>> Late but...
>
> That's okay. Fixup patches can always be submitted.
>
> We have forever. :)
>
>> On 17/09/15 14:45, Lee Jones wrote:
>>> diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
>>> index 055bb01..8bcfb45 100644
>>> --- a/drivers/char/hw_random/Makefile
>>> +++ b/drivers/char/hw_random/Makefile
>>> @@ -30,4 +30,5 @@ obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
>>> obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
>>> obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
>>> obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
>>> +obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
>>> obj-$(CONFIG_HW_RANDOM_XGENE) += xgene-rng.o
>>> diff --git a/drivers/char/hw_random/st-rng.c b/drivers/char/hw_random/st-rng.c
>>> new file mode 100644
>>> index 0000000..8c8a435
>>> --- /dev/null
>>> +++ b/drivers/char/hw_random/st-rng.c
>>> @@ -0,0 +1,144 @@
>>> +/*
>>> + * ST Random Number Generator Driver ST's Platforms
>>> + *
>>> + * Author: Pankaj Dev: <pankaj.dev@st.com>
>>> + * Lee Jones <lee.jones@linaro.org>
>>> + *
>>> + * Copyright (C) 2015 STMicroelectronics (R&D) Limited
>>> + *
>>> + * This program is free software; you can redistribute it and/or modify
>>> + * it under the terms of the GNU General Public License version 2 as
>>> + * published by the Free Software Foundation.
>>> + */
>>> +
>>> +#include <linux/clk.h>
>>> +#include <linux/delay.h>
>>> +#include <linux/hw_random.h>
>>> +#include <linux/io.h>
>>> +#include <linux/module.h>
>>> +#include <linux/of.h>
>>> +#include <linux/platform_device.h>
>>> +#include <linux/slab.h>
>>> +
>>> +/* Registers */
>>> +#define ST_RNG_STATUS_REG 0x20
>>> +#define ST_RNG_DATA_REG 0x24
>>> +
>>> +/* Registers fields */
>>> +#define ST_RNG_STATUS_BAD_SEQUENCE BIT(0)
>>> +#define ST_RNG_STATUS_BAD_ALTERNANCE BIT(1)
>>> +#define ST_RNG_STATUS_FIFO_FULL BIT(5)
>>> +
>>> +#define ST_RNG_FIFO_SIZE 8
>>> +#define ST_RNG_SAMPLE_SIZE 2 /* 2 Byte (16bit) samples */
>>> +
>>> +/* Samples are available every 0.667us, which we round to 1us */
>>> +#define ST_RNG_FILL_FIFO_TIMEOUT (1 * (ST_RNG_FIFO_SIZE / ST_RNG_SAMPLE_SIZE))
>>> +
>>> +struct st_rng_data {
>>> + void __iomem *base;
>>> + struct clk *clk;
>>> + struct hwrng ops;
>>> +};
>>> +
>>> +static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
>>> +{
>>> + struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
>>> + u32 status;
>>> + int i;
>>> +
>>> + if (max < sizeof(u16))
>>> + return -EINVAL;
>>> +
>>> + /* Wait until FIFO is full - max 4uS*/
>>> + for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
>>> + status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
>>> + if (status & ST_RNG_STATUS_FIFO_FULL)
>>> + break;
>>> + udelay(1);
>>
>> How much bandwidth does using udelay() cost? I think it could be
>>> 10% compared to a tighter polling loop.
>
> Samples are only available every 0.7uS and we only do this for every
> 4. The maximum it could 'cost' is <1uS. Do we really want to fuss
> over that tiny amount of time? It's an understandable point if we
> were talking about milliseconds, but a single microsecond?
This code is called in a tight loop so we're not talking about a
*single* microsecond! We are are talking about about one microsecond in
every five[1] and yes, I think we should care about that.
It takes 2.67uS for the FIFO to come ready so with a polling interval of
1uS + loop overhead so I would fully expect on average to "waste" 0.5uS
each time st_rng_read() is called (in a tight loop).
[1]... point three recurring ;-)
>>> + }
>>> +
>>> + if (i == ST_RNG_FILL_FIFO_TIMEOUT)
>>> + return 0;
>>
>> Isn't a timeout an error condition?
>
> Yes, which is why we're returning 0. In this context 0 == 'no data'.
> This will be converted to -EAGAIN if the caller did not request
> NONBLOCKING.
I took the view that hitting the timeout means the hardware is broken.
Is it likely that the next time we call it there will be data ready? If
it is should it be trusted?
Daniel.
next prev parent reply other threads:[~2015-10-05 12:54 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-17 13:45 [PATCH v2 0/7] hwrng: Add support for STMicroelectronics' RNG IP Lee Jones
2015-09-17 13:45 ` [PATCH v2 1/7] Documentation: hw_random: Fix device node name reference /dev/hw_random => /dev/hwrng Lee Jones
[not found] ` <1442497557-9271-2-git-send-email-lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2015-09-18 10:18 ` Kieran Bingham
2015-09-17 13:45 ` [PATCH v2 2/7] hwrng: Kconfig: " Lee Jones
2015-09-18 10:19 ` Kieran Bingham
2015-09-17 13:45 ` [PATCH v2 3/7] hwrng: core: Simplify RNG switching from sysfs Lee Jones
2015-09-17 14:08 ` Peter Korsgaard
[not found] ` <1442497557-9271-1-git-send-email-lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2015-09-17 13:45 ` [PATCH v2 4/7] hwrng: st: Provide DT bindings for ST's Random Number Generator Lee Jones
[not found] ` <1442497557-9271-5-git-send-email-lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2015-09-18 16:21 ` Stephen Boyd
2015-09-18 16:23 ` Lee Jones
2015-09-18 14:07 ` [PATCH v2 0/7] hwrng: Add support for STMicroelectronics' RNG IP Herbert Xu
2015-09-18 14:53 ` Lee Jones
2015-09-18 15:11 ` Herbert Xu
2015-09-18 15:51 ` Lee Jones
2015-09-18 23:12 ` Herbert Xu
2015-09-19 9:21 ` Lee Jones
2015-09-20 1:23 ` Herbert Xu
2015-09-20 4:39 ` Lee Jones
2015-09-29 14:29 ` Lee Jones
2015-09-30 13:47 ` Herbert Xu
2015-09-30 14:15 ` Lee Jones
2015-09-30 14:28 ` Herbert Xu
[not found] ` <20150930142812.GA19039-lOAM2aK0SrRLBo1qDEOMRrpzq4S04n8Q@public.gmane.org>
2015-09-30 14:49 ` [STLinux Kernel] " Maxime Coquelin
2015-09-30 14:50 ` Lee Jones
2015-09-17 13:45 ` [PATCH v2 5/7] hwrng: st: Add support for ST's HW Random Number Generator Lee Jones
2015-09-18 10:44 ` Kieran Bingham
2015-10-05 10:44 ` Daniel Thompson
2015-10-05 12:11 ` Lee Jones
2015-10-05 12:54 ` Daniel Thompson [this message]
2015-10-05 14:52 ` Lee Jones
2015-09-17 13:45 ` [PATCH v2 6/7] ARM: STi: STiH407: Enable the 2 HW Random Number Generators for STiH4{07,10} Lee Jones
2015-10-01 10:57 ` [STLinux Kernel] [PATCH v2 6/7] ARM: STi: STiH407: Enable the 2 HW Random Number Generators for STiH4{07, 10} Maxime Coquelin
2015-09-17 13:45 ` [PATCH v2 7/7] MAINTAINERS: Add ST's Random Number Generator to the ST entry Lee Jones
2015-09-29 9:20 ` [STLinux Kernel] [PATCH v2 0/7] hwrng: Add support for STMicroelectronics' RNG IP Peter Griffin
2015-09-29 10:42 ` Lee Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=561272EC.6080300@linaro.org \
--to=daniel.thompson@linaro.org \
--cc=devicetree@vger.kernel.org \
--cc=festevam@gmail.com \
--cc=herbert@gondor.apana.org.au \
--cc=kernel@stlinux.com \
--cc=kieranbingham@gmail.com \
--cc=lee.jones@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pankaj.dev@st.com \
--cc=peter@korsgaard.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).