From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by mx.groups.io with SMTP id smtpd.web11.5497.1574858836555140196 for ; Wed, 27 Nov 2019 04:47:16 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@akeo-ie.20150623.gappssmtp.com header.s=20150623 header.b=2OyfzioF; spf=none, err=permanent DNS error (domain: akeo.ie, ip: 209.85.128.66, mailfrom: pete@akeo.ie) Received: by mail-wm1-f66.google.com with SMTP id b11so6935680wmj.4 for ; Wed, 27 Nov 2019 04:47:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=akeo-ie.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=s306lN4VZzwmCEBWoOiObs6r4kw6BVq7hmkZVxpI5XQ=; b=2OyfzioFugkfCHo5L+2XMYb36DhQBV8BD1xztJzgqebLnlh7QCPOJEeQeCq9Azj2gj +qauNFXWGQAFH1Onma2POCkiMxgMm0arStRRfbylju0XJmJlIZYmde2aF6WQwxzwheNH Bsb4Jf8fwHs2E2J/A+qYjbWPAAa+Y0hWL0VHPJ+8eC/TBhTtQkISJH6R7OUeuhznSDJm Ii/FKewgpJGf0IWccHnpEom1uRzdaCI53DMn1NrimktHH4FiC1QgWwW0HNaS3kXFYwoq ZJNin9jboUjLYKakeJHkYOsYuZVV0GIarVEvOj/1CQBYcz5sCsFoGY7+pRw1iWud7Rxj UmFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=s306lN4VZzwmCEBWoOiObs6r4kw6BVq7hmkZVxpI5XQ=; b=rDdVmlqEpSajyXRFdDmDXOM59B7k5qv3SdmUo8QmXZ1XQxHLGBzWTtaxzqivjmFsih uw7h+uhOOrZTd1UP3jUuPp+svYACpL3jGXkbfZbPWuC5AuvgZFUvb7jHeIX7n12U7P0N 7qyas54/LVrWj7RZQhugYI9JyZE/FEzp9rq8n5Ce6A7Ih9/ijL5X5J9PdVR4McLG3HBt NShkMHM5BVOlqKSWNum4ekD+tlsRa9argLry3fQiwNiYON2byVWR3uKA8tWlssffcUde WpxvWp+MFqPXRsCl6bdN2IciKPYS5XuTo88H9BYUR+zHca65qi+tEPD1Kjv/xiasOle1 Zsyg== X-Gm-Message-State: APjAAAXddVJ0b84zTj9wJwvjreQx14ObY3avo+ozpGMRKX6/zTOelAuj xi1eQG0iGcHNghLENFYJrqO7kQ== X-Google-Smtp-Source: APXvYqyOpkliM2JqrY9npGtH/wXjqgH3qVrJLXV9+izjmW2kDUJrOEwbanbr/eMeOPsrYgCqJ7uEEw== X-Received: by 2002:a1c:f612:: with SMTP id w18mr4496312wmc.28.1574858834965; Wed, 27 Nov 2019 04:47:14 -0800 (PST) Return-Path: Received: from [10.0.0.122] ([84.203.37.1]) by smtp.googlemail.com with ESMTPSA id y6sm19203893wrr.19.2019.11.27.04.47.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Nov 2019 04:47:13 -0800 (PST) Subject: Re: [edk2-platforms][PATCH 2/5] Silicon/Bcm283x: Add FIFO mode for RNG To: Ard Biesheuvel Cc: edk2-devel-groups-io , Leif Lindholm , =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9?= , samer.el-haj-mahmoud@arm.com, "Andrei E. Warkentin" References: <20191127123706.4604-1-pete@akeo.ie> <20191127123706.4604-3-pete@akeo.ie> From: "Pete Batard" Message-ID: <23ffe668-e6ca-ff63-c4ea-d3be306ce8d9@akeo.ie> Date: Wed, 27 Nov 2019 12:47:12 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit On 2019.11.27 12:44, Ard Biesheuvel wrote: > On Wed, 27 Nov 2019 at 13:37, Pete Batard wrote: >> >> The Bcm283x Random Number Generator does not work in regular mode >> on the Bcm2711 powered Raspberry Pi 4. It does however work when >> using FIFO mode. So we add this new mode, which is governed by the >> use of the new PcdBcm283xRngUseFifo boolean PCD. >> >> Note that just as a Pi 4 doesn't seem to support regular mode, a >> Pi 3 doesn't seem to support FIFO mode, which is why we can't >> switch to simply using FIFO mode for both platforms. >> >> We also fix the register to which RNG_WARMUP_COUNT is written, >> which was incorrect. >> > > At the risk of triggering another endless debate, could we /please/ > not mix in trivial bugfixes with more complicated refactoring like > this? Well, the "trivial" is the precise reason it is mixed in, but sure, I can split that in v2. Regards, /Pete > >> Signed-off-by: Pete Batard >> --- >> Silicon/Broadcom/Bcm283x/Bcm283x.dec | 1 + >> Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.c | 96 +++++++++++++++----- >> Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.inf | 2 + >> 3 files changed, 74 insertions(+), 25 deletions(-) >> >> diff --git a/Silicon/Broadcom/Bcm283x/Bcm283x.dec b/Silicon/Broadcom/Bcm283x/Bcm283x.dec >> index 5b839b00d286..4a9be7b18c97 100644 >> --- a/Silicon/Broadcom/Bcm283x/Bcm283x.dec >> +++ b/Silicon/Broadcom/Bcm283x/Bcm283x.dec >> @@ -21,3 +21,4 @@ [Guids] >> >> [PcdsFixedAtBuild.common] >> gBcm283xTokenSpaceGuid.PcdBcm283xRegistersAddress|0x0|UINT32|0x00000001 >> + gBcm283xTokenSpaceGuid.PcdBcm283xRngUseFifo|0x0|BOOLEAN|0x00000002 >> diff --git a/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.c b/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.c >> index 722815d32f06..462a21a6f3c3 100644 >> --- a/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.c >> +++ b/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.c >> @@ -2,6 +2,7 @@ >> >> This driver produces an EFI_RNG_PROTOCOL instance for the Broadcom 2836 RNG >> >> + Copyright (C) 2019, Pete Batard >> Copyright (C) 2019, Linaro Ltd. All rights reserved.
>> >> SPDX-License-Identifier: BSD-2-Clause-Patent >> @@ -12,6 +13,8 @@ >> #include >> #include >> #include >> +#include >> +#include >> #include >> >> #include >> @@ -20,6 +23,8 @@ >> >> #define RNG_WARMUP_COUNT 0x40000 >> #define RNG_MAX_RETRIES 0x100 // arbitrary upper bound >> +#define RNG_FIFO_NUMVAL_MASK 0xff >> +#define RNG_STATUS_NUMVAL_SHIFT 24 >> >> /** >> Returns information about the random number generation implementation. >> @@ -84,6 +89,54 @@ Bcm2836RngGetInfo ( >> return EFI_SUCCESS; >> } >> >> +/** >> + Read a single random value, in either FIFO or regular mode. >> + >> + @param[in] Val A pointer to the 32-bit word that is to >> + be filled with a random value. >> + >> + @retval EFI_SUCCESS A random value was successfully read. >> + @retval EFI_NOT_READY The number of retries elapsed before a >> + random value was generated. >> + >> +**/ >> +STATIC >> +EFI_STATUS >> +EFIAPI >> +Bcm2836RngReadValue ( >> + IN OUT UINT32 *Val >> +) >> +{ >> + UINT32 Avail; >> + UINT32 i; >> + BOOLEAN UseFifo = FixedPcdGetBool (PcdBcm283xRngUseFifo); >> + >> + ASSERT (Val != NULL); >> + >> + Avail = UseFifo ? >> + (MmioRead32 (RNG_FIFO_COUNT) & RNG_FIFO_NUMVAL_MASK) : >> + (MmioRead32 (RNG_STATUS) >> RNG_STATUS_NUMVAL_SHIFT); >> + >> + // >> + // If we don't have a value ready, wait 1 us and retry. >> + // >> + for (i = 0; Avail < 1 && i < RNG_MAX_RETRIES; i++) { >> + MicroSecondDelay (1); >> + Avail = UseFifo ? >> + (MmioRead32 (RNG_FIFO_COUNT) & RNG_FIFO_NUMVAL_MASK) : >> + (MmioRead32 (RNG_STATUS) >> RNG_STATUS_NUMVAL_SHIFT); >> + } >> + if (Avail < 1) { >> + return EFI_NOT_READY; >> + } >> + >> + *Val = UseFifo ? >> + MmioRead32 (RNG_FIFO_DATA): >> + MmioRead32 (RNG_DATA); >> + >> + return EFI_SUCCESS; >> +} >> + >> /** >> Produces and returns an RNG value using either the default or specified RNG >> algorithm. >> @@ -123,9 +176,8 @@ Bcm2836RngGetRNG ( >> OUT UINT8 *RNGValue >> ) >> { >> - UINT32 Val; >> - UINT32 Num; >> - UINT32 Retries; >> + EFI_STATUS Status; >> + UINT32 Val; >> >> if (This == NULL || RNGValueLength == 0 || RNGValue == NULL) { >> return EFI_INVALID_PARAMETER; >> @@ -139,30 +191,24 @@ Bcm2836RngGetRNG ( >> return EFI_UNSUPPORTED; >> } >> >> - while (RNGValueLength > 0) { >> - Retries = RNG_MAX_RETRIES; >> - do { >> - Num = MmioRead32 (RNG_STATUS) >> 24; >> - MemoryFence (); >> - } while (!Num && Retries-- > 0); >> - >> - if (!Num) { >> - return EFI_DEVICE_ERROR; >> + while (RNGValueLength >= sizeof (UINT32)) { >> + Status = Bcm2836RngReadValue (&Val); >> + if (EFI_ERROR (Status)) { >> + return Status; >> } >> + WriteUnaligned32 ((VOID *)RNGValue, Val); >> + RNGValue += sizeof (UINT32); >> + RNGValueLength -= sizeof (UINT32); >> + } >> >> - while (RNGValueLength >= sizeof (UINT32) && Num > 0) { >> - WriteUnaligned32 ((VOID *)RNGValue, MmioRead32 (RNG_DATA)); >> - RNGValue += sizeof (UINT32); >> - RNGValueLength -= sizeof (UINT32); >> - Num--; >> + if (RNGValueLength > 0) { >> + Status = Bcm2836RngReadValue (&Val); >> + if (EFI_ERROR (Status)) { >> + return Status; >> } >> - >> - if (RNGValueLength > 0 && Num > 0) { >> - Val = MmioRead32 (RNG_DATA); >> - while (RNGValueLength--) { >> - *RNGValue++ = (UINT8)Val; >> - Val >>= 8; >> - } >> + while (RNGValueLength--) { >> + *RNGValue++ = (UINT8)Val; >> + Val >>= 8; >> } >> } >> return EFI_SUCCESS; >> @@ -190,7 +236,7 @@ Bcm2836RngEntryPoint ( >> NULL); >> ASSERT_EFI_ERROR (Status); >> >> - MmioWrite32 (RNG_STATUS, RNG_WARMUP_COUNT); >> + MmioWrite32 (RNG_BIT_COUNT_THRESHOLD, RNG_WARMUP_COUNT); >> MmioWrite32 (RNG_CTRL, RNG_CTRL_ENABLE); >> >> return EFI_SUCCESS; >> diff --git a/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.inf b/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.inf >> index 8eb90de85cfd..31415231ae0b 100644 >> --- a/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.inf >> +++ b/Silicon/Broadcom/Bcm283x/Drivers/RngDxe/RngDxe.inf >> @@ -28,6 +28,7 @@ [LibraryClasses] >> DebugLib >> IoLib >> PcdLib >> + TimerLib >> UefiBootServicesTableLib >> UefiDriverEntryPoint >> >> @@ -39,6 +40,7 @@ [Guids] >> >> [FixedPcd] >> gBcm283xTokenSpaceGuid.PcdBcm283xRegistersAddress >> + gBcm283xTokenSpaceGuid.PcdBcm283xRngUseFifo >> >> [Depex] >> TRUE >> -- >> 2.21.0.windows.1 >>