From patchwork Wed Dec 27 17:38:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 183529 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp1571605dyb; Wed, 27 Dec 2023 09:39:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IEqLDO9hmNCRhGXWF3LmfUmQCbnfOb9ZhnnHjxhPbvoYmmMmZmyqZA6bQkSm9SzmSbawXqE X-Received: by 2002:a05:6871:10f:b0:203:b52f:7079 with SMTP id y15-20020a056871010f00b00203b52f7079mr10492573oab.115.1703698758000; Wed, 27 Dec 2023 09:39:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703698757; cv=none; d=google.com; s=arc-20160816; b=Lrt1CsBfAtaBF3mmVMniI8WKGJ8gdt30vkTrvyMFFZdW0/m/FSrepXAQm0DAQOMDob mnxyLc6CBCrVWskkcZGQNcP6c6VLf1mJCcndJXTcJJB0EFv2TYum6W+SSeRCl5HkaFBJ FBgKQZ1VQNovFJPRmc+4UcDodE7TXXfkxMOdBzlIED6uxdrQVApec7YeBymmowUZ4Pg1 NyoU56oAtisLgAodTfXzUyVyFb50hCDwZYKaKbCupdT+0Y0diMQZDv24GbvU/gleWYiv /lgkriVqKygS6NMw4higV16w4AYIMn5I+kr6P8z5cA3EElI5G7+mYhy3KrfciRihcwmd cCIg== 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=K6PHWaz3J+b+rMdDp/gB/4LQMSmD7tBrsI+jFJO2CvI=; b=F8pBI9p/goOZxYupONvfFcwU3iEUe0sEZFmkK1nN3cq6wziVIUV0QVznaQv3PNBMbP ifysFXRO/4k/lyQAwCLp67Id1L/+Bc0D0fw6MJrLX2YOQZQc09npmJQhdT3zldU0uP47 4AfwuXKIQTPYCKf9D7xUPFm53sEV3/JJg/59vtZy2nuLp1Q9Y0VFRaBz9tBa+KrIxT4A ufgu0oigmZ4NWw1C1VA5ulKALpQUsP8hww0AR4Wy8xUWbdEnBhSjNwDgSf+8aXfXrR9B N/ncGfKzy+nHSWQjr/P2SpZ8amxe7cEVk/G5X0DeD3O3N7tEgjynPzsOV0SZ4Lr210s5 0ytA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yzCweyMQ; spf=pass (google.com: domain of linux-kernel+bounces-12179-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12179-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 s15-20020a05620a254f00b00781711b6379si42084qko.31.2023.12.27.09.39.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Dec 2023 09:39:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-12179-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=yzCweyMQ; spf=pass (google.com: domain of linux-kernel+bounces-12179-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12179-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 B6E681C2249F for ; Wed, 27 Dec 2023 17:39:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8D5F047F77; Wed, 27 Dec 2023 17:38:24 +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="yzCweyMQ" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) (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 6FDDA46556 for ; Wed, 27 Dec 2023 17:38:20 +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-ot1-f54.google.com with SMTP id 46e09a7af769-6da579e6858so3795396a34.3 for ; Wed, 27 Dec 2023 09:38:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1703698699; x=1704303499; 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=yzCweyMQJ0jdqNz5/4fcz0oROC2uVObTYKGRkVMTlWGcqdO7V08Ij0neJbelykYlEE npr6fSeboWp9rjulHEuh5NdzRovEgeIjZwhxB8HMMc8z+UHDbvlC5z74TXTt5SwwjiM0 ipWGLu9mpaR6UrwGRQ7MwJ2MJX8pNXiQe/EWXi5caJrCMhXvUmDDzgJr+Ib8JQnq1bnn 9TS0NjkDboO4lRJhPTbCItuG42Tx9WonEKj/lMGpSus/b1WqAvze+ys05PKvGKruX35v NRuQAh2DEQG5muDVsNxeRk/Kj7tdDDrkP0hWOZChG7qlcJukQ6ZJp3lp3c/KUT7AVsj+ Xoaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703698699; x=1704303499; 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=ZisXp7kFNAkYTdM8WL5xv2az1iDfl1fNs6va95PmWZTMoENNSdHMD9x/9Gk7J8iy2t DoopYFwTRepktFV9nFO8iaV2HpvaWdo4Q4cAkyF2Y66W7Ds3GSP5vwrogNHEuta4vvia tc3PeECvoDkdY6lHZ7UYiTcB8SkQyCyTOaooPPULFEAFCcBJpw8F0qfH4fCnOMC4LX67 r0FaJ+gQ+oaPVzhzyHOVgJfxHmJbXW2GyaIfR+DI1g/uFgvuIGytXiIrxq7VDDIXNTP7 XeO3eK9nDb1cLYxJaoPDsUcI6YeXAIF8MknafFbb5fh2ndUQ3suSemdFdE6B3NIzl1oa wshg== X-Gm-Message-State: AOJu0YzLzM0G/y2AOhM26/n+bJ1xOWk3pqOTJIz4DOcUX4OZYeYwsSYB u2YzcVGOAdCW4bS3llSS8vnQHzAsLftMUA== X-Received: by 2002:a05:6870:5249:b0:203:eb66:3bab with SMTP id o9-20020a056870524900b00203eb663babmr9774414oai.54.1703698699564; Wed, 27 Dec 2023 09:38:19 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id rl15-20020a056871650f00b002049c207104sm1337173oab.27.2023.12.27.09.38.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Dec 2023 09:38:19 -0800 (PST) From: Charlie Jenkins Date: Wed, 27 Dec 2023 09:38:02 -0800 Subject: [PATCH v14 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: <20231227-optimize_checksum-v14-3-ddfd48016566@rivosinc.com> References: <20231227-optimize_checksum-v14-0-ddfd48016566@rivosinc.com> In-Reply-To: <20231227-optimize_checksum-v14-0-ddfd48016566@rivosinc.com> To: Charlie Jenkins , Palmer Dabbelt , Conor Dooley , Samuel Holland , David Laight , Xiao Wang , Evan Green , Guo Ren , 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=1703698692; l=3093; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=5kLrZ5pIGOixKzGlevacL81yKd58H4/jGvyu52ss5bM=; b=3U9itHHqS9xaCdluBkJYwsc1am00JcT/biurx7FiRMdr90d9yE4HTjBnrQeNRpbdA51oMB9S5 DbdrJtJ8v0oD7Y5u/gxJQC8ATbj1SXcEttVfdfOu/fIFZjnsXHx4hFQ X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786457628505656052 X-GMAIL-MSGID: 1786457628505656052 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 */