Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5026134rdb; Tue, 12 Dec 2023 17:19:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgUYCNkrNVdbTc0K0e/l5zGcWE5LSyDA7wAFxrTBTUsRkklNCDWjrSuH1fQHBDGr6I/9gl X-Received: by 2002:a05:6a00:868a:b0:6ce:77a4:e89c with SMTP id hh10-20020a056a00868a00b006ce77a4e89cmr3289843pfb.64.1702430354570; Tue, 12 Dec 2023 17:19:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702430354; cv=none; d=google.com; s=arc-20160816; b=Lsmpk6ri61zcZMDjuNCpVvzLMh9NtLEX96pYcQ1zBTYdMaA/Ua3qqiYCzfauKZiasl zkMDBCR6phdjb6rM0CJRYQFI6Q+1qYqqsAhJ6QypVmK+X73GC0/rMHkeTAKXDu7jlaBd LdZgAuuqpMkRUxvSDdimDN6uT3ewCsFzOOQI7bXkQLJlzkybWWm88GRDA10RFpKW4LRS MB9blW5qEfOhDokKTgGadIDfH4P+AwUNxE/89UQCn6WyM94s2et7ScP9+hBZ26oOYQeF Iu9/Pkv0PuWxHscf/Zby6XV8UQhUQ0gmowtcoZU+v8xvJvwD222cvPmFrhkPb/Od8upX kfdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=Qa7K/oxZt25h94PBFoFutl7qSG1BTlKxow4+QMuXCwE=; fh=9CVYuTeKZZsaqutjthmBvtD0Q2REYt0VsVzn2qcF9/M=; b=yLpooSzUcIpv6ncmOL6gFk/ayWiGG6/CevIFBU8fwEiOm78s+RkBhpaLnmDC3sEwds VXSfK3N+SDojc4e34w3Gd85oeQKbtyv8wseiSecc4cJeDtqV8CJ2vgb9YQ2oPK3QAnOP JctjOxmUaT7omPuqhHcVbrb+qLfEYEsr9/3/PsDzOrQn+GfrA0C/UuFAoBtsrYgwskY4 cJktM9IjvFmMNSV6LHzWluagapX+tp6zdj7u7Tbk9PcS7OPGWwn4LUtgHJldZzT+gjzz O5qX3QQMYJ6Q+nxZS7oiSmI2pGdskTsqXYSx47B7Mnd05R+iL3V4hpbL7vaG1ZdkokKl rh4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MbLqDLho; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id d15-20020a056a00198f00b006ce6ef58aa4si8707852pfl.344.2023.12.12.17.19.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 17:19:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MbLqDLho; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id F238480AA261; Tue, 12 Dec 2023 17:19:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378188AbjLMBSn (ORCPT + 99 others); Tue, 12 Dec 2023 20:18:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378150AbjLMBSk (ORCPT ); Tue, 12 Dec 2023 20:18:40 -0500 Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A81193 for ; Tue, 12 Dec 2023 17:18:46 -0800 (PST) Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-6d9d84019c5so4771665a34.3 for ; Tue, 12 Dec 2023 17:18:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1702430325; x=1703035125; 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=Qa7K/oxZt25h94PBFoFutl7qSG1BTlKxow4+QMuXCwE=; b=MbLqDLhoBW65vE5EhXl8dGCIHqzjeuJG3IEftG5mA66iF/aIZP5xXJvqF+TO9jpliK mVfI+6amyXF8VM7SRECQPn3Xp7E5Rmt3i8iF8SUQ/SazlxnI5s+2N3WyZooIK6pOstZA h/klbIonHmeVGmhquwAx9O6fOPj/RDH/Y9eqqx8vsUAFmGEaaQ4HjjVjnyX38JrI4MbK apyoFxKr2hqqVkfjCw8+hhMPocjatvJos13qqddDlhHii/AmImT66vKnEIFnRmfj3110 cdejgShBfCxbwXl63aH71hzb5gFpdXuBvY6m+WZOFcwAaanVyXcq/klMmyHsLRZJTfQC J/3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702430325; x=1703035125; 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=Qa7K/oxZt25h94PBFoFutl7qSG1BTlKxow4+QMuXCwE=; b=CrQf9LmtOK5xAuJmerF0lhlLPDdUBX2EbpDw62+uuS5IMD51uMyPbLvkCOcoel+PFC D6p2SMOxbvIL1lHfI44uBW5l6U9/e0OPROIXjTpiBP3QkSnhpQqSHfHmUVT/pQOwbDLf F6r6ezqI0vCt6tM1nL2e2VTNyaYMk6SQTz3dUo8ReWMxJo5reE7WvnBDCbt4Fcd9WzYM FwglbbPuKGabIlmLo9oR7mGeMmWRRhIMWDGl/iJbcNbEtv9CZWff5DI3wM7dQsCSii96 Kx8NlXt8CbrL90QptIGvpfm8Gw0wGaOg7+L8lW9zIeFvWl5UnVec9s/rVb56w+YuVW2Y P0yw== X-Gm-Message-State: AOJu0Ywwybl3nLrVnew5TmDaEykxeLOet6KMQ4f9rMeR34BSjA4i7Ilj OtUaOGufhW0TkJnW99XAa7wOCw== X-Received: by 2002:a9d:6c56:0:b0:6d9:e15c:79bc with SMTP id g22-20020a9d6c56000000b006d9e15c79bcmr7215551otq.68.1702430325687; Tue, 12 Dec 2023 17:18:45 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id cy26-20020a056830699a00b006d9a339773csm172548otb.27.2023.12.12.17.18.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 17:18:45 -0800 (PST) From: Charlie Jenkins Date: Tue, 12 Dec 2023 17:18:40 -0800 Subject: [PATCH v12 3/5] riscv: Add checksum header MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231212-optimize_checksum-v12-3-419a4ba6d666@rivosinc.com> References: <20231212-optimize_checksum-v12-0-419a4ba6d666@rivosinc.com> In-Reply-To: <20231212-optimize_checksum-v12-0-419a4ba6d666@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=1702430319; l=3074; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=adYqZ1MwkI2tixiO6Vqw0pA42pYvaEcycoXrFZjPpmw=; b=4z6EVBErljGKcQ6ywUy9p67X640GJzGAFaxbGt7gS99Jj7/mlk/GsHCbAxkR6srBT0KNEL31G eknW7cffrFTCzO5tfzpRKWsA02wygpgNME7+EVpuGtD9DRR31izxRFl X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Dec 2023 17:19:12 -0800 (PST) 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..2fcf864186e7 --- /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 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 */ -- 2.43.0