From patchwork Wed Dec 20 23:37:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 181895 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp76856dyi; Wed, 20 Dec 2023 15:38:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IFMOO5RsY785IMdFZiaaf/ZCMtcSltwJkF6T7/CJY8KaxxlatCbJzaibn1wmNjmeYBitQet X-Received: by 2002:a05:620a:6002:b0:77f:aeb4:7afb with SMTP id dw2-20020a05620a600200b0077faeb47afbmr11360233qkb.5.1703115538970; Wed, 20 Dec 2023 15:38:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703115538; cv=none; d=google.com; s=arc-20160816; b=qMjZGK6OvJFAg//WJo/R+yMaF2FSFdb+NEXkbOGuTU8mfptxhr9GL/j7GaPfp7miRO jzFoi71JiLVuJ5kBSkDkMivyayK47KIRBi7CZ7MxRwhpX5E0M0GBhVEwgYx1UcNAvwdV ZoJJQ6mbWWQtf/9/6X4TjreV0modLhLn/IA3bu1L1Adu1bZ3adhfISfs0ql0USIl7fGT +RR1AdVxFfkdpOeeZA6XEFTgVSLqI/u/njEeriaRyUSd8aBAaHovSobLpiNxgdMvQYgI 6kO6Dw8Lb/9WV79QyZedBkGHsf3M4b41dF8CkD0cM8P1gpsQ4QAaphDDyUenT0wM2pe9 MeYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=AlBR54Ho+Y1o3mIpBCBVXs2u+7/gciH5xwmXB5mwVNU=; fh=9CVYuTeKZZsaqutjthmBvtD0Q2REYt0VsVzn2qcF9/M=; b=sHKseu46hq+C2bTj7t5sY8rhhgi6l5MxERe0VKfTOTb+PuRDd+4Na7Vb8Sfq/FPwl6 Wl4oeytZ28sT5ZbKagnq1/Sx+EbpCtPowY1uj5d6PtH4dM4/mpEZGjcGh9BCaCSdDx2G +ysise3BnokEbVdN7ZJKwT7+3Opi388sWj4Tt1XwWR6Zr+JCrrvC63nJfsKh3Jt5sMFX wPYuJ9ge93Xvnp2Z8NsHEhQ6VncceJlahpO1uYVgYWawWJlH5yI+rQhYf3kho9jiA9r6 DMxgn6XZKnoUWqhSYrAF7WfEeZGMCpvADFQBfHohs/MFob+Za9YQePB9f5bviYqIBbfO wkgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tNXAtkBB; spf=pass (google.com: domain of linux-kernel+bounces-7639-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7639-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g24-20020a37e218000000b007811d3e3fc5si281566qki.686.2023.12.20.15.38.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 15:38:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7639-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tNXAtkBB; spf=pass (google.com: domain of linux-kernel+bounces-7639-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7639-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BD8DB1C23478 for ; Wed, 20 Dec 2023 23:38:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A1C84C624; Wed, 20 Dec 2023 23:37:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="tNXAtkBB" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oa1-f44.google.com (mail-oa1-f44.google.com [209.85.160.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 340A84B5CA for ; Wed, 20 Dec 2023 23:37:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-oa1-f44.google.com with SMTP id 586e51a60fabf-20423a85dbeso99709fac.1 for ; Wed, 20 Dec 2023 15:37:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1703115466; x=1703720266; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AlBR54Ho+Y1o3mIpBCBVXs2u+7/gciH5xwmXB5mwVNU=; b=tNXAtkBBXh4A2FnNy/BjMdhkTaLufaeSw91dleVWRQArN5Ac88AZ5IBkbBlOHKWji1 omB9+b9wMzhWdSVquaDDo/F1O5PMNteticSQhRmQj+ijcJvor6IsXu+BLi8IxMlSJLp6 HmhSTxqJAnx4vqcLaKLoqaUS1F9xeFPlBY7NDyOU6Yr9jmnwSfRJDVX2fGFDmRvHixQ2 vkfgNSK6ib9sLv/xgdNzvINrgJBq5T8tQwLidbIS8uc+WIBvLNGF6fFbE23jmGOTQJWk Q0a44e0Aj1nG+2DKj9wcGFvNorTomvYSrDOl2VcNszEnNqJhrSLzNaezXMvhQA5JB/D8 m4xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703115466; x=1703720266; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AlBR54Ho+Y1o3mIpBCBVXs2u+7/gciH5xwmXB5mwVNU=; b=K1VC0vvrRHHr5w3tQW/+Dgnmf/qwxPIUKqhbViT1TfqnX4VWZcU2kIucDB1tzKRm1N f0p4MNhSNR8MaWCorF4be0YQQeienQ6YuaM+kJolnMbN6NqexIz5Qrc1CNPJ9j1spKPW a9f4Mrk3gGnM9QSvYHQwqPAItTU/aLTzrIfNp30nuAALCTva1cbH9AYZehbQMbkFtum4 hsuLwSdbWhx4pTqrrwIW7SV0MHe/3bI84p/nk08d2tVzKJH6JakgQz83VsfKQsqkxYUw vyDqmvkMyuvqnnZgp22HpNq2bYVetKbqdkSUx0X4SmBG4iaEoHfZ49OL9RamTNroFR+Y 36Xw== X-Gm-Message-State: AOJu0Yzk0n2/fQE/bSlxre8dk78KG4v5p8t/XLdGNVew9+TkrTv9mwK3 nWF5m32RkND/ZcysEN1QGrSz7A== X-Received: by 2002:a05:6870:41d0:b0:204:2bd9:7e89 with SMTP id z16-20020a05687041d000b002042bd97e89mr76457oac.88.1703115466262; Wed, 20 Dec 2023 15:37:46 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id k5-20020a056830150500b006d87e38f91asm132834otp.56.2023.12.20.15.37.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 15:37:45 -0800 (PST) From: Charlie Jenkins Date: Wed, 20 Dec 2023 15:37:41 -0800 Subject: [PATCH v13 3/5] riscv: Add checksum header Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231220-optimize_checksum-v13-3-a73547e1cad8@rivosinc.com> References: <20231220-optimize_checksum-v13-0-a73547e1cad8@rivosinc.com> In-Reply-To: <20231220-optimize_checksum-v13-0-a73547e1cad8@rivosinc.com> To: Charlie Jenkins , Palmer Dabbelt , Conor Dooley , Samuel Holland , David Laight , Xiao Wang , Evan Green , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Paul Walmsley , Albert Ou , Arnd Bergmann , Conor Dooley X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1703115460; l=3093; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=5kLrZ5pIGOixKzGlevacL81yKd58H4/jGvyu52ss5bM=; b=izPJP+daFTVa6yKitNFbRPSPqqaIpZZN3OPtfRsjg/7/qxUEKNV8pETfxzSLQSd5dlIq+5wE7 jG2yJAy6TI7Ak8DLgtvL3OF7ExUWAwC38RVzTUCYSkFYv8VrBuQWrfH X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785846079386013406 X-GMAIL-MSGID: 1785846079386013406 Provide checksum algorithms that have been designed to leverage riscv instructions such as rotate. In 64-bit, can take advantage of the larger register to avoid some overflow checking. Signed-off-by: Charlie Jenkins Acked-by: Conor Dooley Reviewed-by: Xiao Wang --- arch/riscv/include/asm/checksum.h | 82 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h new file mode 100644 index 000000000000..5a810126aac7 --- /dev/null +++ b/arch/riscv/include/asm/checksum.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Checksum routines + * + * Copyright (C) 2023 Rivos Inc. + */ +#ifndef __ASM_RISCV_CHECKSUM_H +#define __ASM_RISCV_CHECKSUM_H + +#include +#include + +#define ip_fast_csum ip_fast_csum + +/* Define riscv versions of functions before importing asm-generic/checksum.h */ +#include + +/** + * Quickly compute an IP checksum with the assumption that IPv4 headers will + * always be in multiples of 32-bits, and have an ihl of at least 5. + * + * @ihl: the number of 32 bit segments and must be greater than or equal to 5. + * @iph: assumed to be word aligned given that NET_IP_ALIGN is set to 2 on + * riscv, defining IP headers to be aligned. + */ +static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) +{ + unsigned long csum = 0; + int pos = 0; + + do { + csum += ((const unsigned int *)iph)[pos]; + if (IS_ENABLED(CONFIG_32BIT)) + csum += csum < ((const unsigned int *)iph)[pos]; + } while (++pos < ihl); + + /* + * ZBB only saves three instructions on 32-bit and five on 64-bit so not + * worth checking if supported without Alternatives. + */ + if (IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && + IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { + unsigned long fold_temp; + + asm_volatile_goto(ALTERNATIVE("j %l[no_zbb]", "nop", 0, + RISCV_ISA_EXT_ZBB, 1) + : + : + : + : no_zbb); + + if (IS_ENABLED(CONFIG_32BIT)) { + asm(".option push \n\ + .option arch,+zbb \n\ + not %[fold_temp], %[csum] \n\ + rori %[csum], %[csum], 16 \n\ + sub %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } else { + asm(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + not %[fold_temp], %[csum] \n\ + roriw %[csum], %[csum], 16 \n\ + subw %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } + return (__force __sum16)(csum >> 16); + } +no_zbb: +#ifndef CONFIG_32BIT + csum += ror64(csum, 32); + csum >>= 32; +#endif + return csum_fold((__force __wsum)csum); +} + +#endif /* __ASM_RISCV_CHECKSUM_H */