Re: [PATCH v2 2/3] tpm_crb: encapsulate crb_wait_for_reg_32

From: Jarkko Sakkinen
Date: Sat Dec 03 2016 - 13:42:28 EST


CC linux-security-module

On Sat, Dec 03, 2016 at 07:52:12PM +0200, Jarkko Sakkinen wrote:
> Encapsulated crb_wait_for_reg32() so that state changes in other CRB
> registers than TPM_CRB_CTRL_REQ_x can be waited.
>
> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx>
> ---
> drivers/char/tpm/tpm_crb.c | 37 +++++++++++++++++++++++--------------
> 1 file changed, 23 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm_crb.c b/drivers/char/tpm/tpm_crb.c
> index 8d81b66..f986d02 100644
> --- a/drivers/char/tpm/tpm_crb.c
> +++ b/drivers/char/tpm/tpm_crb.c
> @@ -121,6 +121,25 @@ static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv)
> return 0;
> }
>
> +static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
> + unsigned long timeout)
> +{
> + ktime_t start;
> + ktime_t stop;
> +
> + start = ktime_get();
> + stop = ktime_add(start, ms_to_ktime(timeout));
> +
> + do {
> + if ((ioread32(reg) & mask) == value)
> + return true;
> +
> + usleep_range(50, 100);
> + } while (ktime_before(ktime_get(), stop));
> +
> + return false;
> +}
> +
> /**
> * crb_cmd_ready - request tpm crb device to enter ready state
> *
> @@ -138,24 +157,14 @@ static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv)
> static int __maybe_unused crb_cmd_ready(struct device *dev,
> struct crb_priv *priv)
> {
> - ktime_t stop, start;
> - u32 req;
> -
> if (priv->flags & CRB_FL_ACPI_START)
> return 0;
>
> iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req);
> -
> - start = ktime_get();
> - stop = ktime_add(start, ms_to_ktime(TPM2_TIMEOUT_C));
> - do {
> - req = ioread32(&priv->regs_t->ctrl_req);
> - if (!(req & CRB_CTRL_REQ_CMD_READY))
> - return 0;
> - usleep_range(50, 100);
> - } while (ktime_before(ktime_get(), stop));
> -
> - if (ioread32(&priv->regs_t->ctrl_req) & CRB_CTRL_REQ_CMD_READY) {
> + if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
> + CRB_CTRL_REQ_CMD_READY /* mask */,
> + 0, /* value */
> + TPM2_TIMEOUT_C)) {
> dev_warn(dev, "cmdReady timed out\n");
> return -ETIME;
> }
> --
> 2.9.3
>