From nobody Sun Feb 8 05:42:19 2026 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) (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 4C2D125A644 for ; Fri, 17 Jan 2025 16:45:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737132341; cv=none; b=WBneiYrhAq2Fh5nPCSDv52bjl1JL3tSA600YD83dx6Z9NvJoJ1Vo2bj1fn6jHeHc3CHPcVozYZmpyvYUb1ihsxUu5SS0alxRhbsX8/DySsxEbASf1mYm9u23yiMvpTBcLHuC4IZDeaYSOl1rNv11Yfvhsx1co92UUVepAzY13D0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737132341; c=relaxed/simple; bh=uNqZO0RzccwBQ+hYxrUOQK/Cr3ZgD6tsI+NfiqDp45c=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=oNwRsS5x0/9osG7yF2H2/AWdW6hidTfSZkHcurJVsPeR4/OKH02ZtUn6yPk+/Ji6d5BOZ0HRr4g+gHGxgE6NKBf8Rnflz1vStibN4PFoooSqtlaXtWKnBDuAkXLJ8bt2TIvYq3c9YjVJjby2nippCrTbBovHhp1RFwZDVUaAJUo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XL49TSyx; arc=none smtp.client-ip=209.85.222.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XL49TSyx" Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-7b6ef047e9bso209281585a.1 for ; Fri, 17 Jan 2025 08:45:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737132338; x=1737737138; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nn1yrcRWQdoqs/ILcamatay4rOXY9C6T1ZnY2c2Ub1g=; b=XL49TSyxrplxORMS/IkgW9Nuk+BL7YfCqzEiTChLBH/TF4mTb7G5fJT9p85YxtGFji vIjPbWtEFMk+2Wsc8jXxMlHu6LHd811Ck/6dLDspKJRG2VmGB9l5LNQU75d20D5ayC6J BuhHJ1DjnIYbtRcpqOR53B1i/4snjOnvAHfE58hvI4LYuch8MeV26PLGp+W146+rglt+ Hdv59F/JorKKCxQQ56ePXxDsiW16h6HR3gQIfGy0X7nhkmUHaUeLkRL3A9HFMvxifAgT iDIJbevPtls/mABoH44Irbbti+GioFbF6qvdkazgtDOvCjPX7dSDycrQ0jZPcMDRinqT 9FtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737132338; x=1737737138; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nn1yrcRWQdoqs/ILcamatay4rOXY9C6T1ZnY2c2Ub1g=; b=P9Pir+0ahw99wejBatZp5HSatRMnrfknNY3n/Q0jQWtzwZ41MY0imKISrOvkuEZkhL AThk8KVKUQQ4lzvLe4lAyFXhcXy23O8GX4onQtYX2kjHjt/MgE6UmkPyGuSpnITRbkI/ y6/IjjcuFz+7TCJuspMuhmDIIQAQLSVgNyNrAlur9RjTBSx7cx3Z1bwPspU3oGcmAZdQ PZC6nmDujKIKmAaXZq921ElRDnXEhNd3Nf1IcMrw4ykubNj+KGlChXYf4wN1C+fPnqB7 SwQOuBp7lHVediYLGZoE6zVdsx/vPoM4mJJzjePndG4bUuByf6owGOYNfSi2LkTNcSWT YsmQ== X-Forwarded-Encrypted: i=1; AJvYcCXIapRc+POaVBILjMnj8U9IX2YwkFFKdcilIVZDPNi7di3Ipi0UsXV1NTRmEwqKuHXc0kbQtc39bVPkZ5w=@vger.kernel.org X-Gm-Message-State: AOJu0YzaEZZiKMeUXHwd3jTZAekjE3FeBDMuefFWMzoXnAewTAUH5MMb 11V5JegFlMtm2gYkRFtB3lETYYM53eaJmHqDmsupMPYBOBz5njb1 X-Gm-Gg: ASbGnctWDa0EsvkIAUgXWdGT5kP4ov8YZi+hwafWU1quo7LhyuLXKs/3HX/D5IsUPM7 b6OKn8yyf0ntqPmDdBK1ZQKZvWhvO6BLd3qBOzI111AVSEaFXpcvJ8uXvcwp5YATln5yWavPtlg wRMsiY+x7s8poSJeYdHe6VvdGNZheNuIfz/cCoxbIoZOeQHsw1rYfNLUPhCQ7TXdjlvBbvwUyV1 NJKMazJbxCq7zqxgyjcWSoT2qDovDQBCrEUpJ7dKWh7qdhX5Gaz3gmfh0dvHu1mMJ9AsrQKJJZu 5gXE/zyzGP+xoWqORUzdcUG1KfNFSc2b+S5l9Q== X-Google-Smtp-Source: AGHT+IGLySB45T8QpI9xiopCo20jMVkIyUb+nkAiZ46E8hRHQyORZiegRHS4oEgu/Mobge367ZpAag== X-Received: by 2002:a05:620a:408c:b0:7b1:21c4:ecf8 with SMTP id af79cd13be357-7be52464e94mr1935471785a.28.1737132337929; Fri, 17 Jan 2025 08:45:37 -0800 (PST) Received: from jesse-desktop.. (pool-108-26-179-17.bstnma.fios.verizon.net. [108.26.179.17]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6e1afcf50ffsm12795426d6.110.2025.01.17.08.45.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jan 2025 08:45:37 -0800 (PST) From: Jesse Taube X-Google-Original-From: Jesse Taube To: linux-riscv@lists.infradead.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Daniel Lezcano , Thomas Gleixner , Samuel Holland , Conor Dooley , Icenowy Zheng , Jesse Taube , Jisheng Zhang , linux-kernel@vger.kernel.org Subject: [PATCH v5] clocksource/drivers/timer-clint: Add T-Head C9xx clint Date: Fri, 17 Jan 2025 11:45:36 -0500 Message-ID: <20250117164536.3918949-1-Mr.Bossman075@gmail.com> X-Mailer: git-send-email 2.45.2 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" From: Jisheng Zhang To use the T-HEAD C9xx clint in RISCV-M NOMMU env, we need to take care two points: 1.The mtimecmp in T-Head C9xx clint only supports 32bit read/write, implement such support. 2. As pointed out by commit ca7810aecdba ("lib: utils/timer: mtimer: add a quirk for lacking mtime register") of opensbi: "T-Head developers surely have a different understanding of time CSR and CLINT's mtime register with SiFive ones, that they did not implement the mtime register at all -- as shown in openC906 source code, their time CSR value is just exposed at the top of their processor IP block and expects an external continous counter, which makes it not overrideable, and thus mtime register is not implemented, even not for reading. However, if CLINTEE is not enabled in T-Head's MXSTATUS extended CSR, these systems still rely on the mtimecmp registers to generate timer interrupts. This makes it necessary to implement T-Head C9xx CLINT support in OpenSBI MTIMER driver, which skips implementing reading mtime register and falls back to default code that reads time CSR." So, we need to fall back to read time CSR instead of mtime register. Add riscv_csr_time_available static key for this purpose. Signed-off-by: Jisheng Zhang Signed-off-by: Jesse Taube --- Treat this as a completely new patch, as it is mostly rewritten. Original: https://lore.kernel.org/all/20240410142347.964-3-jszhang@kernel.org/ V3 -> V4: - Add riscv,csr-clint - Allow using of CSRs in S mode - Change if return else return to if return return - Change static_branch_likely to static_branch_unlikely - Fix 32-bit clint_get_cycles64 csr_available check being inverted - Fix is_c900_clint being uninitialized V4 -> V5: - Remove riscv,csr-clint - Remove riscv_csr_time_available - Replace all static_branch_unlikely(&riscv_csr_time_available) with riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR) --- arch/riscv/include/asm/timex.h | 12 +++++++- drivers/clocksource/timer-clint.c | 50 +++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/arch/riscv/include/asm/timex.h b/arch/riscv/include/asm/timex.h index a06697846e69..d78075351bf4 100644 --- a/arch/riscv/include/asm/timex.h +++ b/arch/riscv/include/asm/timex.h @@ -17,17 +17,26 @@ typedef unsigned long cycles_t; #ifdef CONFIG_64BIT static inline cycles_t get_cycles(void) { + if (riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR)) + return csr_read(CSR_TIME); + return readq_relaxed(clint_time_val); } #else /* !CONFIG_64BIT */ static inline u32 get_cycles(void) { + if (riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR)) + return csr_read(CSR_TIME); + return readl_relaxed(((u32 *)clint_time_val)); } #define get_cycles get_cycles =20 static inline u32 get_cycles_hi(void) { + if (riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR)) + return csr_read(CSR_TIMEH); + return readl_relaxed(((u32 *)clint_time_val) + 1); } #define get_cycles_hi get_cycles_hi @@ -40,7 +49,8 @@ static inline u32 get_cycles_hi(void) */ static inline unsigned long random_get_entropy(void) { - if (unlikely(clint_time_val =3D=3D NULL)) + if (!riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR) && + (unlikely(clint_time_val =3D=3D NULL))) return random_get_entropy_fallback(); return get_cycles(); } diff --git a/drivers/clocksource/timer-clint.c b/drivers/clocksource/timer-= clint.c index 0bdd9d7ec545..cdf8b33d2dd2 100644 --- a/drivers/clocksource/timer-clint.c +++ b/drivers/clocksource/timer-clint.c @@ -39,6 +39,7 @@ static u64 __iomem *clint_timer_cmp; static u64 __iomem *clint_timer_val; static unsigned long clint_timer_freq; static unsigned int clint_timer_irq; +static bool is_c900_clint; =20 #ifdef CONFIG_RISCV_M_MODE u64 __iomem *clint_time_val; @@ -79,6 +80,9 @@ static void clint_ipi_interrupt(struct irq_desc *desc) #ifdef CONFIG_64BIT static u64 notrace clint_get_cycles64(void) { + if (riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR)) + return csr_read(CSR_TIME); + return clint_get_cycles(); } #else /* CONFIG_64BIT */ @@ -86,10 +90,17 @@ static u64 notrace clint_get_cycles64(void) { u32 hi, lo; =20 - do { - hi =3D clint_get_cycles_hi(); - lo =3D clint_get_cycles(); - } while (hi !=3D clint_get_cycles_hi()); + if (riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR)) { + do { + hi =3D csr_read(CSR_TIMEH); + lo =3D csr_read(CSR_TIME); + } while (hi !=3D csr_read(CSR_TIMEH)); + } else { + do { + hi =3D clint_get_cycles_hi(); + lo =3D clint_get_cycles(); + } while (hi !=3D clint_get_cycles_hi()); + } =20 return ((u64)hi << 32) | lo; } @@ -119,6 +130,19 @@ static int clint_clock_next_event(unsigned long delta, return 0; } =20 +static int c900_clint_clock_next_event(unsigned long delta, + struct clock_event_device *ce) +{ + void __iomem *r =3D clint_timer_cmp + + cpuid_to_hartid_map(smp_processor_id()); + u64 val =3D clint_get_cycles64() + delta; + + csr_set(CSR_IE, IE_TIE); + writel_relaxed(val, r); + writel_relaxed(val >> 32, r + 4); + return 0; +} + static DEFINE_PER_CPU(struct clock_event_device, clint_clock_event) =3D { .name =3D "clint_clockevent", .features =3D CLOCK_EVT_FEAT_ONESHOT, @@ -130,6 +154,9 @@ static int clint_timer_starting_cpu(unsigned int cpu) { struct clock_event_device *ce =3D per_cpu_ptr(&clint_clock_event, cpu); =20 + if (is_c900_clint) + ce->set_next_event =3D c900_clint_clock_next_event; + ce->cpumask =3D cpumask_of(cpu); clockevents_config_and_register(ce, clint_timer_freq, 100, ULONG_MAX); =20 @@ -161,7 +188,7 @@ static irqreturn_t clint_timer_interrupt(int irq, void = *dev_id) return IRQ_HANDLED; } =20 -static int __init clint_timer_init_dt(struct device_node *np) +static int __init clint_timer_init(struct device_node *np) { int rc; u32 i, nr_irqs; @@ -273,5 +300,18 @@ static int __init clint_timer_init_dt(struct device_no= de *np) return rc; } =20 +static int __init clint_timer_init_dt(struct device_node *np) +{ + is_c900_clint =3D false; + return clint_timer_init(np); +} + +static int __init c900_clint_timer_init_dt(struct device_node *np) +{ + is_c900_clint =3D true; + return clint_timer_init(np); +} + TIMER_OF_DECLARE(clint_timer, "riscv,clint0", clint_timer_init_dt); TIMER_OF_DECLARE(clint_timer1, "sifive,clint0", clint_timer_init_dt); +TIMER_OF_DECLARE(clint_timer2, "thead,c900-clint", c900_clint_timer_init_d= t); --=20 2.45.2