Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp1088893lqo; Fri, 17 May 2024 10:14:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXvicU5jZXdd86UiVa30qMDEgT7OZRxuyTgezX2bpKEwT/xMxLlnmENqbX2PF/SX1UR4e/HkJLbRZexpD9Z/06p5iZN7xfc0RHXalQGFg== X-Google-Smtp-Source: AGHT+IFj1jziNrZz5m+592jI+4s8f5Cd3SXjDUyl/Zi+LHFlvxcih1R+EgEj4yfCxgdHlg5itPJv X-Received: by 2002:a05:6e02:18c5:b0:36c:a2c:ceaf with SMTP id e9e14a558f8ab-36cc142a32emr286892475ab.6.1715966097054; Fri, 17 May 2024 10:14:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715966097; cv=pass; d=google.com; s=arc-20160816; b=nNI615FynaWIxxZoPiDYKXesMaGcQFmF5FM3RX2puAjbMDU5GvrCKazBw5XKS8rFhO qZIcyjSvmGZdhutBWdMR+GIgAxwBlTOXzYSrgkcDH6bYdENwSs+3hEBUwHNr9n2aMMxK sAQMplGVVeYoyC3Ka7fHEOo2XAfEN3VCnJJHuqTLyn2bn+CVAggRo0AQgjskzJHHrw+k Nw88EUb0R7fnK4VXm/CIFx0UBjJaCu0k4fxjcpdYyZ0F+BhUxDOYHRBc8bNcZi4klGVJ VBHiJ0VK8h014V+gU5viAvkFfu4MAco+Ftv3UGp9ka5M68Bg6Dlw0eP70PAaVv3Yejhu nwNg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:message-id:content-transfer-encoding:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:subject:date :from:feedback-id:dkim-signature:dkim-signature; bh=S0ae6Op1KMk+gXKqxzXaZSqYv4HS7YhFYfRFLWzD9J8=; fh=Q77g9vV+Cf3lmYo7RLHEHBR0oCXasP4TKZCQeORBohY=; b=yr7PG54+ZLw+hx2m9yxKgNp4IrD9NLfqyqsESlL+hTHDIVJziQn3aJEDCX/KtGUdKd WiSc2JKu9gZu82yOz1cvCoxHFO6RZgbExK2n0vWFADtP414k4lEyEySYRkvpSbgCWHBU s1q3dj8RjvpK5KNLnj26ixKDsMOd/OoXBEyJtLvwxPb4dPOF9CIL9NgJEP6/hFAvfo8U TbaiI7qELCRlFiGB6m/wH3wnIRLKYy33XHMvUmqr3SHPClNy9p4XwZh+LlJOSx2F1yKa 5CkAOOiBhSoSuowJ8AlsY0urJlyHA+rFHP6CUcY6Dg3YaOIMSe7mbSiN0j87uUvLeDI+ wqKg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@flygoat.com header.s=fm1 header.b=PmaZ43dh; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=QGa+i0S7; arc=pass (i=1 spf=pass spfdomain=flygoat.com dkim=pass dkdomain=flygoat.com dkim=pass dkdomain=messagingengine.com dmarc=pass fromdomain=flygoat.com); spf=pass (google.com: domain of linux-kernel+bounces-182384-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182384-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=flygoat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-65927ddb6fbsi4019674a12.435.2024.05.17.10.14.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 10:14:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-182384-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@flygoat.com header.s=fm1 header.b=PmaZ43dh; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=QGa+i0S7; arc=pass (i=1 spf=pass spfdomain=flygoat.com dkim=pass dkdomain=flygoat.com dkim=pass dkdomain=messagingengine.com dmarc=pass fromdomain=flygoat.com); spf=pass (google.com: domain of linux-kernel+bounces-182384-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182384-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=flygoat.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 55D39B233C6 for ; Fri, 17 May 2024 17:13:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8876513DB96; Fri, 17 May 2024 17:13:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=flygoat.com header.i=@flygoat.com header.b="PmaZ43dh"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="QGa+i0S7" Received: from fhigh6-smtp.messagingengine.com (fhigh6-smtp.messagingengine.com [103.168.172.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4FA7512F5A3; Fri, 17 May 2024 17:13:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.157 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715966000; cv=none; b=FdEmR56Dvo4PHRZd2swZZKZnfqp5JWR933QwNxAuX6SmFl+tjB3Mv99/vcuWGvhdjqmERRF32wBUqp2wq49BIRpHQa1wj/xPQlut2+96QyF0L2PzpwZi+rjLFK2pyPruNBMTMKeLCBt97ItPlqREOF5aVr/B12+i/rZmlGehIzk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715966000; c=relaxed/simple; bh=eRIcU31KqTg7XP1KK5CO/h4djKFMTvtSgXQZ/wf9Lv0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=WvAJhoNMeIea0FL0C0CVWF90m8opWFysIEpNgDO8EChhHRkoIl9JUTxlHbuqEIYMPsEIqzof1ynKQYraJrbiKgQmRc5ye35S0waBYCbGFww/WTM8/QYh255Ip2bDONqLYB+WTOfxZ+4eaZJc9PJSfsgGLvXXBsX5VOutyuOYiHo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=flygoat.com; spf=pass smtp.mailfrom=flygoat.com; dkim=pass (2048-bit key) header.d=flygoat.com header.i=@flygoat.com header.b=PmaZ43dh; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=QGa+i0S7; arc=none smtp.client-ip=103.168.172.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=flygoat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flygoat.com Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 5B39D11400BC; Fri, 17 May 2024 13:13:17 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 17 May 2024 13:13:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=flygoat.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:message-id:mime-version:reply-to :subject:subject:to:to; s=fm1; t=1715965997; x=1716052397; bh=S0 ae6Op1KMk+gXKqxzXaZSqYv4HS7YhFYfRFLWzD9J8=; b=PmaZ43dhtnvfCtzkSM r6q/uGQdCWFHh+s1EK2KbDSAtrjdDbgmTl3FalqC3MsIYX4NqtsALOZrg+rb5Z8y NIGU1i/tGbD4kz4w73XOJURABwxQbJ7yF6PdusPGO4vwzyPqBXpvuZrMtFxGaSFc q5jGjjFCW28oEyXYyZWCmeCTLFZDSnRxbbOeW7HnPNZejRP67sUncdVtnxz8g5eh 2PXF887bDDWynYFbgUF6m+btYzZ9Uoyo5q0Q65nFSPfwr0GeIN96rQwBQ81+/JxB 2DhLUGQ6wE6RGIfVmk73EVKNaxT9jyFTbrZ7VIwDlL3bNxB8qmQK/OxO2VeYYW56 YyBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1715965997; x=1716052397; bh=S0ae6Op1KMk+g XKqxzXaZSqYv4HS7YhFYfRFLWzD9J8=; b=QGa+i0S73G9uwbsIXg+zNNY6uqlE3 V8Zs0VXuFLWYcItFF+YQ5p29PYBx62lgKzxAThIzRL1ZW/rYDnC5Fu5c1h8v1MJd 5klN9/X2/cjkVVcP+MC5nkgFx7Ph9M6lU6JgAaTRuWFpIN7ub6ulq/wkTafRXDMQ l4kH5FS35DFQmB6FGvKOd/cHfkYc0FKgwzNuBYE+yho0SBfaWNNpbxcPT8oMs9Rr kJeVHl34EFUqUa/cATi2sDwebPi1B34zjDTAtkVqr690cHX3p00wbGeFJ22Od+uz Q4rKr+adcSb/+yHwelKb2qrTPkmzQmLgJeG3HbEMwpzcnZDNonIjaD/xA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdehgedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephfffufggtgfgkffvvefosehtjeertdertdejnecuhfhrohhmpeflihgrgihu nhcujggrnhhguceojhhirgiguhhnrdihrghnghesfhhlhihgohgrthdrtghomheqnecugg ftrfgrthhtvghrnhephfffgeejgfejieeugffgudegvdekffevgeeuteetgeejveeiteei vedvffehlefgnecuffhomhgrihhnpehkvghrnhgvlhdrohhrghenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehjihgrgihunhdrhigrnhhgsehf lhihghhorghtrdgtohhm X-ME-Proxy: Feedback-ID: ifd894703:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 17 May 2024 13:13:15 -0400 (EDT) From: Jiaxun Yang Date: Fri, 17 May 2024 18:13:10 +0100 Subject: [PATCH v2] clocksource: Add node counter timer driver for MIPS/Loongson64 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240517-loongson_nodecnt-v2-1-5bd0bb20ff5f@flygoat.com> X-B4-Tracking: v=1; b=H4sIACWQR2YC/32NQQqDMBBFryKzbkoSTKNd9R5FisZJDNiMJBIq4 t2beoAu34P//g4Jo8cE92qHiNknT6GAvFRgpj44ZH4sDJLLmish2UwUXKLwCjSiCSvjmtdW3wb TqhbKbIlo/edMPrvCk08rxe18yOJn/8SyYIJJofTQSrSNbR523hz169XQG7rjOL6A9kI3sgAAA A== To: Huacai Chen , Thomas Bogendoerfer , Daniel Lezcano , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, Jiaxun Yang X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6427; i=jiaxun.yang@flygoat.com; h=from:subject:message-id; bh=eRIcU31KqTg7XP1KK5CO/h4djKFMTvtSgXQZ/wf9Lv0=; b=owGbwMvMwCHmXMhTe71c8zDjabUkhjT3CdpvmcocTwXOYvNKd82UZYqW+mw8c32e3v3/J32Op 137qOjUUcrCIMbBICumyBIioNS3ofHigusPsv7AzGFlAhnCwMUpABMRC2VkmFXCr6+4QpDv4GSx Y+4b786Uc9qh92T5PYFZTPXpiRGXPzD8s/dJ1l9y3XqOsM7jlMcSZXzfY3VYmqKO3Hl2t/yE4VU WdgA= X-Developer-Key: i=jiaxun.yang@flygoat.com; a=openpgp; fpr=980379BEFEBFBF477EA04EF9C111949073FC0F67 Node counter is a timer presents on many Loongson-3 series CPUs. It is maintained on every node in system. To avoid synchronisation complexity we only access the copy from first node in system. It also has many ways to be accessed, on latest Loongson-3 CPU with IOCSR instruction support it should be accessed with a IOCSR request, while on earlier Loongson-3 CPUs it is attached to a 32 bits MMIO bus. For QEMU's Loongson-3 virt system it is mapped to a 64 bit MMIO location. On some rare case the counter is disabled by firmware or not present on chip, so we need to perform a lightweight test to ensure it is running before actually use it. Signed-off-by: Jiaxun Yang --- Changes in v2: - Fix build failure when it's not enabled. - Link to v1: https://lore.kernel.org/r/20240512-loongson_nodecnt-v1-1-2157b92ef8f8@flygoat.com --- MAINTAINERS | 1 + arch/mips/include/asm/mach-loongson64/loongson.h | 3 + arch/mips/loongson64/time.c | 3 + drivers/clocksource/Kconfig | 8 ++ drivers/clocksource/loongson-nodecnt.c | 112 +++++++++++++++++++++++ 5 files changed, 127 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index c675fc296b19..b36bff5b9803 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15117,6 +15117,7 @@ L: linux-mips@vger.kernel.org S: Maintained F: arch/mips/include/asm/mach-loongson64/ F: arch/mips/loongson64/ +F: drivers/clocksource/loongson-nodecnt.c F: drivers/irqchip/irq-loongson* F: drivers/platform/mips/cpu_hwmon.c diff --git a/arch/mips/include/asm/mach-loongson64/loongson.h b/arch/mips/include/asm/mach-loongson64/loongson.h index f7c3ab6d724e..d07f4be06595 100644 --- a/arch/mips/include/asm/mach-loongson64/loongson.h +++ b/arch/mips/include/asm/mach-loongson64/loongson.h @@ -56,6 +56,9 @@ extern void *loongson_fdt_blob; extern void mach_irq_dispatch(unsigned int pending); extern int mach_i8259_irq(void); +/* Time functions */ +extern int __init nodecnt_clocksource_init(void); + /* We need this in some places... */ #define delay() ({ \ int x; \ diff --git a/arch/mips/loongson64/time.c b/arch/mips/loongson64/time.c index f6d2c1e30570..6e0603a6d713 100644 --- a/arch/mips/loongson64/time.c +++ b/arch/mips/loongson64/time.c @@ -44,4 +44,7 @@ void __init plat_time_init(void) #ifdef CONFIG_RS780_HPET setup_hpet_timer(); #endif +#ifdef CONFIG_LOONGSON_NODECNT + nodecnt_clocksource_init(); +#endif } diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig index 34faa0320ece..1c068f604333 100644 --- a/drivers/clocksource/Kconfig +++ b/drivers/clocksource/Kconfig @@ -743,4 +743,12 @@ config EP93XX_TIMER Enables support for the Cirrus Logic timer block EP93XX. +config LOONGSON_NODECNT + bool "Loongson Node Conunter timer driver" + default y if MIPS && MACH_LOONGSON64 + depends on (MIPS && MACH_LOONGSON64) || COMPILE_TEST + depends on GENERIC_SCHED_CLOCK + help + Enables support for the Loongson Node Counter timer. + endmenu diff --git a/drivers/clocksource/loongson-nodecnt.c b/drivers/clocksource/loongson-nodecnt.c new file mode 100644 index 000000000000..3cea4045ce75 --- /dev/null +++ b/drivers/clocksource/loongson-nodecnt.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024, Jiaxun Yang + * Loongson-3 Node Counter clocksource + */ + +#include +#include +#include +#include +#include + +#include +#include + +#define NODECNT_REGBASE 0x3ff00408 + +static void __iomem *nodecnt_reg; +static u64 (*nodecnt_read_fn)(void); + +static u64 notrace nodecnt_read_2x32(void) +{ + unsigned int hi, hi2, lo; + + do { + hi = readl_relaxed(nodecnt_reg + 4); + lo = readl_relaxed(nodecnt_reg); + hi2 = readl_relaxed(nodecnt_reg + 4); + } while (hi2 != hi); + + return (((u64) hi) << 32) + lo; +} + +static u64 notrace nodecnt_read_64(void) +{ + return readq_relaxed(nodecnt_reg); +} + +static u64 notrace nodecnt_read_csr(void) +{ + return csr_readq(LOONGSON_CSR_NODECNT); +} + +static u64 nodecnt_clocksource_read(struct clocksource *cs) +{ + return nodecnt_read_fn(); +} + +static struct clocksource nodecnt_clocksource = { + .name = "nodecnt", + .read = nodecnt_clocksource_read, + .mask = CLOCKSOURCE_MASK(64), + .flags = CLOCK_SOURCE_IS_CONTINUOUS, +}; + +int __init nodecnt_clocksource_init(void) +{ + int err; + uint64_t delta; + + if (!cpu_clock_freq) + return -ENODEV; + + if (cpu_has_csr() && csr_readl(LOONGSON_CSR_FEATURES) & LOONGSON_CSRF_NODECNT) { + nodecnt_read_fn = nodecnt_read_csr; + } else if (loongson_sysconf.bridgetype == VIRTUAL) { + nodecnt_reg = ioremap(NODECNT_REGBASE, 8); + if (!nodecnt_reg) + return -ENOMEM; + nodecnt_read_fn = nodecnt_read_64; + } else { + switch (boot_cpu_data.processor_id & (PRID_IMP_MASK | PRID_REV_MASK)) { + case PRID_IMP_LOONGSON_64C | PRID_REV_LOONGSON3A_R2_0: + case PRID_IMP_LOONGSON_64C | PRID_REV_LOONGSON3A_R2_1: + case PRID_IMP_LOONGSON_64C | PRID_REV_LOONGSON3A_R3_0: + case PRID_IMP_LOONGSON_64C | PRID_REV_LOONGSON3A_R3_1: + break; + default: + return -ENODEV; + } + nodecnt_reg = ioremap(NODECNT_REGBASE, 8); + if (!nodecnt_reg) + return -ENOMEM; + nodecnt_read_fn = nodecnt_read_2x32; + } + + /* Test if nodecnt is usable */ + delta = nodecnt_read_fn(); + udelay(10); + delta = nodecnt_read_fn() - delta; + + if (!delta) { + pr_info("nodecnt: clocksource unusable\n"); + err = -ENODEV; + goto out; + } + + err = clocksource_register_hz(&nodecnt_clocksource, cpu_clock_freq); + if (err) { + pr_err("nodecnt: clocksource register failed\n"); + goto out; + } + + /* It fits for sched_clock if we don't suffer from cross node access */ + if (loongson_sysconf.bridgetype == VIRTUAL || loongson_sysconf.nr_nodes <= 1) + sched_clock_register(nodecnt_read_fn, 64, cpu_clock_freq); + +out: + if (nodecnt_reg) + iounmap(nodecnt_reg); + return err; +} --- base-commit: 75fa778d74b786a1608d55d655d42b480a6fa8bd change-id: 20240512-loongson_nodecnt-0704f76bc959 Best regards, -- Jiaxun Yang