From nobody Thu Dec 18 23:43:34 2025 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 B936711720 for ; Wed, 17 Jul 2024 06:21:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721197265; cv=none; b=XjCR6XepqOy6mQ4O7ngItBwKbvsQdOvdv9Q+g+qr2w+ceMXpkilxGrqlsQjLmTo18noS27sSEqhnVos1UgncDaAK8WZk13QkkcF/ep8iE+9ythL3l7GAizHWHd12sAl2td5wyOBBWmCwTC9c/cMvZ4UcXhruNi/ncj1T7siT0N8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721197265; c=relaxed/simple; bh=SXM4rwRn+leJo0pY0SefkogHqfR8LE/yvz/Y5VWFmfY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=au8BiBsToU1r/Ultth5JXPYaJgUdTj+qAxkj5Z0cNy05PLZoPEmpjXq3633AImJ+lI3tL0OJxqUlCY73bNvCUFm2BcZ7OBKiZpV+UjVaTYMCuKg6fNGpmpUVyNdNEtMjOSc8c/UpTVT5ibFLPzBbc8Z+Ov5IgMQxtRRCHpIKTD0= 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=NO6C4MFc; arc=none smtp.client-ip=209.85.128.52 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 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="NO6C4MFc" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4266fd39527so47683095e9.1 for ; Tue, 16 Jul 2024 23:21:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1721197261; x=1721802061; 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=ObJKiPOVe5UThPd0qvfEFz5Dv3ABd72erfyOiynPtd0=; b=NO6C4MFccp6jMYUp14P2O8wo0mBXLQl6LHFIuVweezaHf1jUvJie9NmZVZ0ZlFQi8H wDY18VV5pgJBylc6QQI6pv2EtwmXFCspGDWNt7TfUutbEHRaLi6tKVk4+IA/x09mQ8SD q9gm7kYx5Tlc9vTUvCPdPHChI64mu8vTVM+A5r18hmO0CePM2bGgCc7iUOXwvSvc/5RV V808mT4G9+0wbzBHjzaTFKcZbkKnaR+5NATSw+JpgRp1PsHO0N6k18WYsovFOkYFQgol UegyJfdiNS6B1RKv7Ex6GrE3xMbhpD/CcTFtMxWoRIbsaxHuwi3GSe5KlxnSbSMhmwoF B77Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721197261; x=1721802061; 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=ObJKiPOVe5UThPd0qvfEFz5Dv3ABd72erfyOiynPtd0=; b=dTbDJRrwdSPrK+zWoq1ECe9pD6mggot1x6dnteH8YYkGf3P1Qth0oMMmqfaCKIMuaI 6a62E5Ycu6sgLAUm4S2e9vXVTyi3MqsPSTBnr7vOYCTNVq6h56+anSQC/nv0xKZ51jqC oPnY5CmwjTSSGUegW5mQ1qxSjh4v4we94juTfoSoyTMLCj2HEpV1gZ6x6NmIzbM6ZjSw Dv9/KrdpmA4sleorKcHhZzRQTqDLEf23lqkGv64wziuMUDic2vZst/VDfuidqntFMluj r8l7BphLxXszfCJ2JgJmtwCCkiQB7ikfKg5Zpp0hmj1QLMyigp1J+cxAl77vTcS+z7Ja adDg== X-Forwarded-Encrypted: i=1; AJvYcCWpuWmbOjFPFPrWELa2D7Nv439kD41SFOqW9Bw1ZE4TBD6XMZgUvVcWEWKW7qDPY7iHpuFToNE2jDSLV0CnKypQNLjR+R+NLWAVJZjR X-Gm-Message-State: AOJu0YwSS+s14awTGTbI39VIcxdStFFxKi0QauFuKRDfsOF6AZOJTlJC BY+Nj09uCRsIYxTsOsYweQZHhQ+41yO23hVAImHrhUzjT2lBkfOkoKxL9qYofEA= X-Google-Smtp-Source: AGHT+IEiqDUUVGHtWHIaIo4kIBhavsk6wpSjfOY4FyjkQmBFXrWWxjPRklD8XBy60kYG9vsl83pQBA== X-Received: by 2002:a5d:6a8c:0:b0:368:37ac:3f95 with SMTP id ffacd0b85a97d-36837ac4476mr188365f8f.31.1721197261060; Tue, 16 Jul 2024 23:21:01 -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 ffacd0b85a97d-3680dab3cd1sm10867433f8f.22.2024.07.16.23.21.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jul 2024 23:21:00 -0700 (PDT) From: Alexandre Ghiti To: Jonathan Corbet , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Andrea Parri , Nathan Chancellor , 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 v3 01/11] riscv: Implement cmpxchg32/64() using Zacas Date: Wed, 17 Jul 2024 08:19:47 +0200 Message-Id: <20240717061957.140712-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240717061957.140712-1-alexghiti@rivosinc.com> References: <20240717061957.140712-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: quoted-printable Content-Type: text/plain; charset="utf-8" This adds runtime support for Zacas in cmpxchg operations. Signed-off-by: Alexandre Ghiti --- arch/riscv/Kconfig | 17 +++++++++++++++++ arch/riscv/Makefile | 3 +++ arch/riscv/include/asm/cmpxchg.h | 26 +++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 05ccba8ca33a..1caaedec88c7 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -596,6 +596,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. =20 +config TOOLCHAIN_HAS_ZACAS + bool + default y + depends on !64BIT || $(cc-option,-mabi=3Dlp64 -march=3Drv64ima_zacas) + depends on !32BIT || $(cc-option,-mabi=3Dilp32 -march=3Drv32ima_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 + Enable the use of the Zacas ISA-extension to implement kernel atomic + cmpxchg operations when it is detected at boot. + + 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 06de9d365088..9fd13d7a9cc6 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -85,6 +85,9 @@ endif # Check if the toolchain supports Zihintpause extension riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE) :=3D $(riscv-march-y)_zihi= ntpause =20 +# Check if the toolchain supports Zacas +riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZACAS) :=3D $(riscv-march-y)_zacas + # 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 +=3D -march=3D$(shell echo $(riscv-march-y) | sed -E 's/(rv3= 2ima|rv64ima)fd([^v_]*)v?/\1\2/') diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index 808b4c78462e..5d38153e2f13 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -9,6 +9,7 @@ #include =20 #include +#include =20 #define __arch_xchg_masked(sc_sfx, prepend, append, r, p, n) \ ({ \ @@ -134,21 +135,40 @@ r =3D (__typeof__(*(p)))((__retx & __mask) >> __s); \ }) =20 -#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__ no_zacas, end; \ register unsigned int __rc; \ \ + if (IS_ENABLED(CONFIG_RISCV_ISA_ZACAS)) { \ + asm goto(ALTERNATIVE("j %[no_zacas]", "nop", 0, \ + RISCV_ISA_EXT_ZACAS, 1) \ + : : : : no_zacas); \ + \ + __asm__ __volatile__ ( \ + prepend \ + " amocas" sc_cas_sfx " %0, %z2, %1\n" \ + append \ + : "+&r" (r), "+A" (*(p)) \ + : "rJ" (n) \ + : "memory"); \ + goto end; \ + } \ + \ +no_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" \ : "=3D&r" (r), "=3D&r" (__rc), "+A" (*(p)) \ : "rJ" (co o), "rJ" (n) \ : "memory"); \ + \ +end:; \ }) =20 #define _arch_cmpxchg(ptr, old, new, sc_sfx, prepend, append) \ @@ -156,7 +176,7 @@ __typeof__(ptr) __ptr =3D (ptr); \ __typeof__(*(__ptr)) __old =3D (old); \ __typeof__(*(__ptr)) __new =3D (new); \ - __typeof__(*(__ptr)) __ret; \ + __typeof__(*(__ptr)) __ret =3D (old); \ \ switch (sizeof(*__ptr)) { \ case 1: \ --=20 2.39.2