Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp36688lqb; Tue, 28 May 2024 08:13:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCViOE7XyfRHJVHZJcRb+3VjgkBU6k5TPkUOqE8b2zIcN6e4HOlZ1AnsuVXed2UgoGmuPlw8rortaGWKwJfaDkw8e+z2jVzBrhgn97Fe4Q== X-Google-Smtp-Source: AGHT+IF2KMsuf7jo1X04dOetqjFZLJI+gA03eZw/HfU4xQIKWPwPZr4EVuvWqI1BEYzBiGJ/3J9j X-Received: by 2002:ac2:4112:0:b0:51f:d72:cd2d with SMTP id 2adb3069b0e04-529646de6d3mr7060117e87.22.1716909195896; Tue, 28 May 2024 08:13:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716909195; cv=pass; d=google.com; s=arc-20160816; b=EqmQKfgXxRkUF60YErcUUQ/9ULfKwIWx76Nj3qmxyrqt3USsC92Bwc6gv/LRQBTmxZ 0Z/UUHzvMay7Uc8fwoaPj4dSBT2ukzGY0MJANJmWjkH5hBFQrGKHX+M2YsOILPEUWdBw uV29NW6vWuJmgouTjCN3gddmhMs5zW4UTEyuYPc3nDkWhZYwQo4WBdmEkt0+pMHAuUZg rq/5gJxCQnx4zjCyYb+ZdatAYOqSACvGaQq3X3VCDr7s5V0ul+8NTJAmAwPT+8BS5slW 8MZU9oilyzn2gLY2YP3fmShzOL1434AS37mb2HBQd0MHha7HJDJz6nCHjBmGl2i3HkUD GWdQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=gqY2tac92Zgdj6EN5vhDUCiGPySLtAUcYkNNT11YFiQ=; fh=w89B7+iRkjEhwYm7o8T5+DKUetb1DJeEOo4ILP4bjDE=; b=NI2aVwjK8BjEW2HsKMESaz4DpGMTsJdpUjmllQSJBDoFw+qg6/pVx1D2GDfGLrQ3CK 4w3tr+eKL7vUjdG74prK6QosaCk8AqnfyvNZ4xM7oG3VGOOHNsaITxCNp1MuGY917sJJ pXRZGFjy/LGA4rs2+maKE+nmC11LqFt1RnNqiRfrOQW+t8coLgjlTuMZO2Xs2ErsII5t OsTt07ks97gxa3XJ7DzUoOztYnfkC1aC5mI2NAvfCtbxl6CQEjZxJFAwL6OJENxi8zxF vn6bdW1NToQuUR6Q/xBZWOCrIAuNsOwFeNRvNPGUewjrZAW/aHCRhQPznaa8GpsI++H0 fAIA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eJ7V4iUs; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-192630-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192630-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a626cc36bdfsi512667166b.445.2024.05.28.08.13.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 08:13:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192630-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eJ7V4iUs; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-192630-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192630-linux.lists.archive=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 4166C1F22951 for ; Tue, 28 May 2024 15:13:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B7A33172BCB; Tue, 28 May 2024 15:12:00 +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="eJ7V4iUs" Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (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 EC953171075 for ; Tue, 28 May 2024 15:11:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716909119; cv=none; b=VNXdzzdlm/kAsULhvTGJZfvrDhmQ18YntmNpiF3yvTZQGpOcWbdXxEZXF1g8GJhKIhm/dpA+7y4/b4gi2Ank6bDDoS9G+QwVNGjw8bN3izxtcKZcBbQXB2xnyOAKkhl+ncWkrUtXPA+C+Y5H39pyRQZJNlp7wx88K/Vw1RXWA74= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716909119; c=relaxed/simple; bh=TEvDOfsnylXJLXAYcAcZM/8r3TjyXkyenEf233xm5J0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Zldx/efqWyOF/WGKZ/JH2zWxyJgSC5Z6/zd5Fx9SPIpotCTMixDDa2ZmoZDGehQdW5px+yJhFfWLYGi7e7YljlSGWYI8uJVN+b7I4N9IrHiQ5RHmb0hFGAIJAoWFOnBHPS+VjDe8QtBRTxUYHKH3gs2/uGDFY1l71r9H4PXv+dA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=eJ7V4iUs; arc=none smtp.client-ip=209.85.208.171 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-lj1-f171.google.com with SMTP id 38308e7fff4ca-2e72b8931caso10299391fa.0 for ; Tue, 28 May 2024 08:11:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716909116; x=1717513916; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gqY2tac92Zgdj6EN5vhDUCiGPySLtAUcYkNNT11YFiQ=; b=eJ7V4iUs+u1NnkYTGo24gwCYXi5U46c5IbP0vk+xe5wjvK9WZhT4O8+h54B5n919yV KSt9PnI+QF5HfDTN9dStse+LcOII6iQd993KmtNBFaOSehF1EPz9LwPriXx+4lqJMO6X /JhjXCSTZCK0d3XSuOs/M7ctdA0TIcqH92pDrqCfFakgs7pSktkTttAp416/XrjtosPu 7U4Tk6r2udWMO0T5ddNP/aFYOMMc9l94F9QyLAhjv3PcKx5IsbZi4DoR4nRsMuKH6BsH 4qlLpcBRqmWbFS579fZnSEkQcBXEiSxA45f7lq8QbCSEEuH5WL2aH5M8YoJshKkDQWfV a22w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716909116; x=1717513916; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gqY2tac92Zgdj6EN5vhDUCiGPySLtAUcYkNNT11YFiQ=; b=UksMBRZnF7zsDX7eWLdQzBxMaUTiOddbTwmeX+/XPkwozWQqiLPq6cQFQbmUbuVxRV ecJP/pNaHkQ+fZ/QcdImyzqtFucgmIi33ma+kanX7Jg+IwCTRFDpLSoo7q7XF/uW7QVt m2hD/zn99h/M1oWxVfCjUMFUYoKmHxzwKgTXT/sTQHsrSZ0f0YbWJjyjotm5oRrisohs X1IQqZlU03m7V/jOETzzamI0dEruEWG9ZlThlwGWq0KyshB2zFC2p8gcUFZsISWWGLfy UGWjQzbDf/q9Jh0LsTlhPg3REyNZ455FGY4fCRMl1uXwr94aAdZ1lAmrjmzs7ybBqmV/ 9m/w== X-Forwarded-Encrypted: i=1; AJvYcCXTjDCvsm6INE3q4cLakKvYVohl7jAXVObNMJGyGJ5M7jnw58bTuYbPKm065SOjDsHQgQWguICiTd0EPKTkQH4WWcUtVmapYnAev4KR X-Gm-Message-State: AOJu0YyxC8nkVGafc6IB4hIvy9rDGc56/3+Rx8JctbPzTaQDJhQLGjZk FnyijcdWxLask9QtiFOfrbfw8XRaE/B8mTtBA5fOlf2eaNmoMpJyo1vycEDv97Q= X-Received: by 2002:a2e:b0ed:0:b0:2e7:28ea:3c9d with SMTP id 38308e7fff4ca-2e95b2792f6mr65981801fa.51.1716909115927; Tue, 28 May 2024 08:11:55 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42100ef1207sm176823805e9.10.2024.05.28.08.11.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 08:11:55 -0700 (PDT) From: Alexandre Ghiti To: Jonathan Corbet , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , Arnd Bergmann , Leonardo Bras , Guo Ren , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH 1/7] riscv: Implement cmpxchg32/64() using Zacas Date: Tue, 28 May 2024 17:10:46 +0200 Message-Id: <20240528151052.313031-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240528151052.313031-1-alexghiti@rivosinc.com> References: <20240528151052.313031-1-alexghiti@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This adds runtime support for Zacas in cmpxchg operations. Signed-off-by: Alexandre Ghiti --- arch/riscv/Kconfig | 17 +++++++++++++++++ arch/riscv/Makefile | 11 +++++++++++ arch/riscv/include/asm/cmpxchg.h | 23 ++++++++++++++++++++--- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 8a0f403432e8..b443def70139 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -579,6 +579,23 @@ config RISCV_ISA_V_PREEMPTIVE preemption. Enabling this config will result in higher memory consumption due to the allocation of per-task's kernel Vector context. +config TOOLCHAIN_HAS_ZACAS + bool + default y + depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zacas) + depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zacas) + depends on AS_HAS_OPTION_ARCH + +config RISCV_ISA_ZACAS + bool "Zacas extension support for atomic CAS" + depends on TOOLCHAIN_HAS_ZACAS + default y + help + Adds support to use atomic CAS instead of LR/SC to implement kernel + atomic cmpxchg operation. + + If you don't know what to do here, say Y. + config TOOLCHAIN_HAS_ZBB bool default y diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 5b3115a19852..d5b60b87998c 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -78,6 +78,17 @@ endif # Check if the toolchain supports Zihintpause extension riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE) := $(riscv-march-y)_zihintpause +# Check if the toolchain supports Zacas +ifdef CONFIG_AS_IS_LLVM +# Support for experimental Zacas was merged in LLVM 17, but the removal of +# the "experimental" was merged in LLVM 19. +KBUILD_CFLAGS += -menable-experimental-extensions +KBUILD_AFLAGS += -menable-experimental-extensions +riscv-march-y := $(riscv-march-y)_zacas1p0 +else +riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZACAS) := $(riscv-march-y)_zacas +endif + # Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by # matching non-v and non-multi-letter extensions out with the filter ([^v_]*) KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h index 4d23f0c35b94..1c50b4821ac8 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -9,6 +9,7 @@ #include #include +#include #define __arch_xchg_masked(prepend, append, r, p, n) \ ({ \ @@ -132,21 +133,37 @@ r = (__typeof__(*(p)))((__retx & __mask) >> __s); \ }) -#define __arch_cmpxchg(lr_sfx, sc_sfx, prepend, append, r, p, co, o, n) \ +#define __arch_cmpxchg(lr_sfx, sc_cas_sfx, prepend, append, r, p, co, o, n) \ ({ \ + __label__ zacas, end; \ register unsigned int __rc; \ \ + asm goto(ALTERNATIVE("nop", "j %[zacas]", 0, \ + RISCV_ISA_EXT_ZACAS, 1) \ + : : : : zacas); \ + \ __asm__ __volatile__ ( \ prepend \ "0: lr" lr_sfx " %0, %2\n" \ " bne %0, %z3, 1f\n" \ - " sc" sc_sfx " %1, %z4, %2\n" \ + " sc" sc_cas_sfx " %1, %z4, %2\n" \ " bnez %1, 0b\n" \ append \ "1:\n" \ : "=&r" (r), "=&r" (__rc), "+A" (*(p)) \ : "rJ" (co o), "rJ" (n) \ : "memory"); \ + goto end; \ + \ +zacas: \ + __asm__ __volatile__ ( \ + prepend \ + " amocas" sc_cas_sfx " %0, %z2, %1\n" \ + append \ + : "+&r" (r), "+A" (*(p)) \ + : "rJ" (n) \ + : "memory"); \ +end: \ }) #define _arch_cmpxchg(ptr, old, new, sc_sfx, prepend, append) \ @@ -154,7 +171,7 @@ __typeof__(ptr) __ptr = (ptr); \ __typeof__(*(__ptr)) __old = (old); \ __typeof__(*(__ptr)) __new = (new); \ - __typeof__(*(__ptr)) __ret; \ + __typeof__(*(__ptr)) __ret = (old); \ \ switch (sizeof(*__ptr)) { \ case 1: \ -- 2.39.2