From patchwork Fri Aug 4 01:12:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Brown X-Patchwork-Id: 797524 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xNppv0gf4z9s72 for ; Fri, 4 Aug 2017 11:13:59 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="OkiGvuPT"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3xNppt61k2zDqJn for ; Fri, 4 Aug 2017 11:13:58 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="OkiGvuPT"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Received: from mail-pf0-x242.google.com (mail-pf0-x242.google.com [IPv6:2607:f8b0:400e:c00::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3xNpnH0P78zDqG5 for ; Fri, 4 Aug 2017 11:12:34 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="OkiGvuPT"; dkim-atps=neutral Received: by mail-pf0-x242.google.com with SMTP id t86so332428pfe.1 for ; Thu, 03 Aug 2017 18:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=WprYZWpfx0DcRbLaoLDOFs1AidzXtoJg9islP5SlSZ8=; b=OkiGvuPTbDd/yLdVgAy73A2CU9bJ+ZMPH9RexPJV6YgQ34ZcLla64pzLhCm+uHwdU/ w7LyD+j5VmcQeKVtCiBZro5ctLzwgkkQ51aKa8cSjXTwLxLg8RVGpc74KKzzxLrtKyBw OqEyGlsMaIMKp5a5aDBFQYHdI9NPRYjxvSFn4fMpCpr9KsAkbWKJjlYsV9CHeO+APyW5 dhOM7lUrOQ5uyaEwgPtki3UtBLsTWqY1qkW1uU5WYIkdPYlY7f5qhG6jYHKGWJORQJj4 F7N9q8HSi0aBeZKgFFM3mCqR7tE3ZYlDAXj9GzSkp9HS6evmOZrbZSR/Q53ax7OGtnd0 x6Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=WprYZWpfx0DcRbLaoLDOFs1AidzXtoJg9islP5SlSZ8=; b=QY4KMiJqS+x63DV9p1czlfHXJ3pPHx89JaZ6zB4/K2qLhBsC/aSgIUmA8BasfXqz4t 83AyS2G20omShxdziW2sUx53ZVSNCVcxuZE5slWUlUbm1pbsqOjyp7kN4VHtr5P7jX1f 656OolXLVGZC+I9/LXx5B5Y86KVLTbcZq4rB2HW1Tn14+zBHveW1fS4kKtSVSorDCMJL 3Qaonu3sdtTv6b0Bl1HlDLj7Z7Q0OF5QLiHivQvvdUIVStmtuJnl/hhl6E6tKlPgtL8M JgV54U+7avzEWlTgN3qsh/EOAls/MmPfzpwelMHG37WiTWIgu4jqY17v0/9yMtja2WWB WBRQ== X-Gm-Message-State: AIVw110m+YkJTC9cAt90jg1CgeDCJJlWNvFbni3C3cW21a1jPnfMZC/e EJ54ntSBFU2aaawD X-Received: by 10.99.95.133 with SMTP id t127mr642763pgb.142.1501809152942; Thu, 03 Aug 2017 18:12:32 -0700 (PDT) Received: from matt.ozlabs.ibm.com ([122.99.82.10]) by smtp.gmail.com with ESMTPSA id o17sm247987pgn.52.2017.08.03.18.12.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 03 Aug 2017 18:12:32 -0700 (PDT) From: Matt Brown To: linuxppc-dev@lists.ozlabs.org, mpe@ellerman.id.au Subject: [PATCH v3] powerpc/powernv: Use darn instr for random_seed on p9 Date: Fri, 4 Aug 2017 11:12:18 +1000 Message-Id: <20170804011218.10489-1-matthew.brown.dev@gmail.com> X-Mailer: git-send-email 2.9.3 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" This adds the powernv_get_random_darn function which utilises the darn instruction, introduced in POWER9. The powernv_get_random_darn function is used as the ppc_md.get_random_seed on P9. The DARN instruction can potentially throw an error, so we attempt to register the powernv_get_random_darn function up to 10 times before failing. Signed-off-by: Matt Brown --- v3: - add repeat attempts to register the ppc_md.get_random_seed - fixed the PPC_DARN macro - move DARN_ERR definition - fixed commit message v2: - remove repeat darn attempts - move hook to rng_init --- arch/powerpc/include/asm/ppc-opcode.h | 4 ++++ arch/powerpc/platforms/powernv/rng.c | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/include/asm/ppc-opcode.h b/arch/powerpc/include/asm/ppc-opcode.h index c4ced1d..aabd150 100644 --- a/arch/powerpc/include/asm/ppc-opcode.h +++ b/arch/powerpc/include/asm/ppc-opcode.h @@ -134,6 +134,7 @@ #define PPC_INST_COPY 0x7c00060c #define PPC_INST_COPY_FIRST 0x7c20060c #define PPC_INST_CP_ABORT 0x7c00068c +#define PPC_INST_DARN 0x7c0005e6 #define PPC_INST_DCBA 0x7c0005ec #define PPC_INST_DCBA_MASK 0xfc0007fe #define PPC_INST_DCBAL 0x7c2005ec @@ -325,6 +326,9 @@ /* Deal with instructions that older assemblers aren't aware of */ #define PPC_CP_ABORT stringify_in_c(.long PPC_INST_CP_ABORT) +#define PPC_DARN(t, l) stringify_in_c(.long PPC_INST_DARN | \ + ___PPC_RT(t) | \ + (((l) & 0x3) << 16)) #define PPC_DCBAL(a, b) stringify_in_c(.long PPC_INST_DCBAL | \ __PPC_RA(a) | __PPC_RB(b)) #define PPC_DCBZL(a, b) stringify_in_c(.long PPC_INST_DCBZL | \ diff --git a/arch/powerpc/platforms/powernv/rng.c b/arch/powerpc/platforms/powernv/rng.c index 5dcbdea..83b925c 100644 --- a/arch/powerpc/platforms/powernv/rng.c +++ b/arch/powerpc/platforms/powernv/rng.c @@ -16,11 +16,13 @@ #include #include #include +#include #include #include #include #include +#define DARN_ERR 0xFFFFFFFFFFFFFFFFul struct powernv_rng { void __iomem *regs; @@ -67,6 +69,21 @@ int powernv_get_random_real_mode(unsigned long *v) return 1; } +int powernv_get_random_darn(unsigned long *v) +{ + unsigned long val; + + /* Using DARN with L=1 - 64-bit conditioned random number */ + asm volatile(PPC_DARN(%0, 1) : "=r"(val)); + + if (val == DARN_ERR) + return 0; + + *v = val; + + return 1; +} + int powernv_get_random_long(unsigned long *v) { struct powernv_rng *rng; @@ -135,8 +152,9 @@ static __init int rng_create(struct device_node *dn) static __init int rng_init(void) { + unsigned long darn_test; struct device_node *dn; - int rc; + int rc, i; for_each_compatible_node(dn, NULL, "ibm,power-rng") { rc = rng_create(dn); @@ -150,6 +168,21 @@ static __init int rng_init(void) of_platform_device_create(dn, NULL, NULL); } + if (cpu_has_feature(CPU_FTR_ARCH_300)) { + for (i = 0; i < 10; i++) { + if (powernv_get_random_darn(&darn_test)) { + ppc_md.get_random_seed = + powernv_get_random_darn; + break; + } + + if (i == 9) { + pr_warn("Failed to use powernv_get_random_darn"\ + "as get_random_seed"); + } + } + } + return 0; } machine_subsys_initcall(powernv, rng_init);