From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795526166; b=2bvlIiqHo1JWhBLmT9TeIZvBbMBaAe1DIDW82ZHOldtQyUifBhCqWv84wstThZqJAoFU N2SMFKGgqtxWjV1yJrl1cqZl3eFGGupyPRD/rM21LOKCYN+MS1HmW2j8SjMA4fFejQWpq wrow2WvJRp7nGey/VF7oJxQ68xiQgHWwcCdLgstdA1AX1PdLv8HwjAH30xOEJyiuTeq6T 2D1PyQqrUw1qJJA7mLtyMqx3sUzgbwovWjm79dp0i7fTdBulRYlL/qnt5aS6hGStrxg/4 s6kl7PKnAXpZ1cdtRJo97U3gnf1o7DsF6ufAUqbztqjmHim2EWEuqb7VLVZUSMnUWjA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795526166; h=DMARC-Filter:Received:DKIM-Signature:Received:Received: DKIM-Signature:X-Google-DKIM-Signature:X-Forwarded-Encrypted: X-Gm-Message-State:X-Gm-Gg:X-Received:X-Received:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Authority-Analysis:X-Proofpoint-ORIG-GUID: X-Proofpoint-Spam-Info:X-Proofpoint-GUID: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=hBzVLClOSEGyAdU0kuKKVnfXtMFpmo67/NIzqOitKqQ=; b=VCi9ZzaL9WZQKF/twXdaZex6fiksaz2eCKrKoNTPDeLgB0UyNRv/hWvcdCaPsHDs5adM FFm1A3mw6LZlFE1tWFEHy9idNaXfwtF1YURQTipGMt/CSLY4A01wye4zojZe9borRAcn4 8ctTWSJ1TJ2zpfPCSU84Z6r5RMky69GrXz6KstPiiLUbtNyK6T4O6/Jto5X2hpch0rCR7 6IuO9Vca2bXBvcdyv76j8OMUoCfTwyZc6LHw6dcOx0NnTH5DSn7F8EfUvDEqKBp1a0XeV MnkGI9vli/jCvnyFIs1q5fMKW4dp8UnwOYtruO33h13fHvp0zy8OaoBaYoZklixDrkw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=DJzNn+wa; dkim=pass header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=BbaskNPC; arc=none smtp.remote-ip=205.220.180.131 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B8CC606A336 for ; Tue, 24 Nov 2026 13:16:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2B8CC606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=DJzNn+wa; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=BbaskNPC Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6AOAv8ZR750684 for ; Tue, 24 Nov 2026 13:16:05 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=qcppdkim1; bh=hBzVLClOSEG yAdU0kuKKVnfXtMFpmo67/NIzqOitKqQ=; b=DJzNn+waZmkIj6BE/F7JwVj7Te+ GjssrSKgdIOEzhMVjWNmd1Zogre3jquEanDVlbV5bIBvtBKA1GQve7G/WBfKiszm FQDa6WCtnf4tAiyKh4eZ2g8kTZSlzk5U6aJyoivcFyWlvC/kxKpFEnRNy/sOPCLx OFriDS7nn9D4oL6p9mLMqzljgFTrT9x16RxQ/KS5FfSFirH4hCTfAnIo99iMgEEJ 7JQlSi38JWG/DPxqu/a93/gtL/zmkKdqHy4mqkbzvKSW4dafwgKRVsIMhr2g8g0D YKiS7ikpzXGf8p2PCyhAnhY3/4BaiOkpmitTjhCbQfhp+85u7v5SIo+rEUg== Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4j4yvb2vy6-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 24 Nov 2026 13:16:04 +0000 (GMT) Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-386b8624cf7so4178387eec.1 for ; Tue, 24 Nov 2026 05:16:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1795526164; x=1796130964; 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:content-type; bh=hBzVLClOSEGyAdU0kuKKVnfXtMFpmo67/NIzqOitKqQ=; b=BbaskNPCaE48rTGtJZ6DjYnIMNFQYsYXy1Z3bhRAYp9LdKPF/sFJkkbySnEwS6dQo0 UCrsflXsDwm8j+JGsq87ceg3udEQgx/0+xQ0wviKv6aoxOZ2SREzSesR9ZjGKie9pQ/q VhVxScCbkUy6mZ6Mp6d6lJ+Zu8gIlpItSeROF6xcJen4r9tmPK4tHLUbl5ff/A5nPb2Q zuJdbHKGtWyLSEMgqK4KfWWxRmtSIRMxrRgFifKYo5yfhtruIMp2T/DkdtdTDd5TvGKU w1x5gA2/1a43OPa6amDmXymygAg/9S9wkFQe/DKoBi5Z2ue2lSbNNfohjjm1jg+EFD8Q Bz9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795526164; x=1796130964; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=hBzVLClOSEGyAdU0kuKKVnfXtMFpmo67/NIzqOitKqQ=; b=KC7Dx0R0Dp9TrGWuvGBelqzb45LNrZtWZz0aFayTuEb1uknQlkceNyLAS4Bu6rQX70 neQ6DbKqr3rM4OiUjwTZEjXdVBX+gej5+A5CXct23eVzkah88t4kbxNuSa4/vc8R5q9T 3Cvdk/+EBC+JuGbX/eNpSImZFOTa2/6w18it6Gf+Co46dhwT4Ui82fip/GC6pIPNnHeS ijt/6kIooVz1q6PEeRuiKB4ny80J9cvSBFXsyrb9KBvZXSpW6YyDE+iuR0ciZ4hj2+co xXo8DuV186SBjLXlZP9b5sk21csKyrIDSBgVhrRUJON58tPbPMRmzkoD+meiu/Z+gP4o bTKQ== X-Forwarded-Encrypted: i=1; AL+hErErtG1Z/NINXNb2rf8PUnoW28hb3u786E/dabgny00WRdOH/7ohY7yamFK18MIsN9Bg/DKbpIRZTvmCaww=@vger.kernel.org X-Gm-Message-State: APuU4Z+5pT9NacC4kxB6fUsUJmObCeXkwUho7gAjoLwqRAVCJtV1GwYG I532ChLlsz5rKyX813AO5euLTRIXXMjBcuNuIinCMkNJhl5oqW1S+QeLOQC/MOH3BvJVmZkW3v3 2ElNvJnLhOT4a/pHB4R+bqgld4WVQwNH77wZLqynAFjdpFkRO9mYDAn+oZ4z2qdoTlCs= X-Gm-Gg: AZFdTZeJ1iFHtUz1ZSrfxSAh68JYvCD89V2d3ZqpTDTsHK9aoAxv3/7mWyB8LcWT8op r/UJFESEA16vfDAA8DK7bWWcEu20mGiNLg0FfvJTaQs6I/upfDwZML06NB3FYtsYtsf9jvZtZje L/cbwvoKbyTZIYYAgN3kY3QiaJKThmL0iCafllngCRMN12AarhiwZ8gAPgLR7onCEyMCyyrDSfp Ik0NKs8zCvOwtJmAkBwZyEVIrF4xqTVXhTYddECzajPoNC4Jf/a6CM9pJBB/rBGQBRaLYXHbBnk JQklFH7vC/EEqczaeUwufHfKdBDkcNDhLbbbIBv+eQ/KwQnuTtQZSvKhFa4KGLZWqjzYya4qit1 eKBEqDzmr6crglX/nLZRQw1BNf3w2r1OiWUQtASHqHunexjGwtQVp5lG91SrH1Dboff/T23HAGq AH/TGncAMQp0yILXxgw5EPd7UJ959VAClqD1MP8TLh6x6OOpOynOnQCsajW9aqhQ== X-Received: by 2002:a05:693c:6205:b0:386:a498:3ab1 with SMTP id 5a478bee46e88-386a4983cc5mr10398623eec.25.1795526163539; Tue, 24 Nov 2026 05:16:03 -0800 (PST) X-Received: by 2002:a05:693c:6205:b0:386:a498:3ab1 with SMTP id 5a478bee46e88-386a4983cc5mr10398574eec.25.1795526162685; Tue, 24 Nov 2026 05:16:02 -0800 (PST) Received: from hu-prajn-blr.qualcomm.com (blr-bdr-fw-01_GlobalNAT_AllZones-Outside.qualcomm.com. [103.229.18.19]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385665f21aesm56371320eec.4.2026.11.24.05.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Nov 2026 05:16:02 -0800 (PST) From: Prajwal N To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, wim@linux-watchdog.org, linux@roeck-us.net, radim.krcmar@oss.qualcomm.com, anup@brainfault.org, anup.patel@oss.qualcomm.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-watchdog@vger.kernel.org, prajwal.n.oss@gmail.com, Prajwal N Subject: [RFC PATCH v4 2/2] drivers/watchdog: Add RISC-V SBI watchdog driver Date: Tue, 24 Nov 2026 18:45:42 +0530 Message-ID: <20261124131542.613271-3-prajwal.n@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261124131542.613271-1-prajwal.n@oss.qualcomm.com> References: <20261124131542.613271-1-prajwal.n@oss.qualcomm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Authority-Analysis: v=2.4 cv=aJ1lOr9m c=1 sm=1 tr=0 ts=6b058e14 cx=c_pps a=wEP8DlPgTf/vqF+yE6f9lg==:117 a=Ou0eQOY4+eZoSc0qltEV5Q==:17 a=zJRp6_cAMcgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=ZpdpYltYx_vBUK5n70dp:22 a=EUspDBNiAAAA:8 a=0SxRSwYGAAAA:8 a=TyS6Ky86QTBuXj3fIXcA:9 a=O8hF6Hzn-FEA:10 a=bBxd6f-gb0O0v-kibOvt:22 a=evtWB9OUeeBtXImRToD_:22 X-Proofpoint-ORIG-GUID: IN5AUHAUqLiZGFBli3RvPbMqSiDF1z_D X-Proofpoint-Spam-Info: AW1haW4tMjYxMTI0MDA1MSBTYWx0ZWRfX76O3h+LRmcAu 9eeV/3IuCk+bdWdpAFY2r+EAyE9SoYklgn+HhtMy3XkZLGIsHgxC9aJstxr9X44t56ptMHB+1uZ rUQha/3uCFt1xbsRl1GzPS10oa4iD3w= X-Proofpoint-GUID: IN5AUHAUqLiZGFBli3RvPbMqSiDF1z_D X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMTI0MDA1MSBTYWx0ZWRfX6QLcZCFD07wQ Xuh59JUFpw6skYSQZgvH/rU63R8v5RmYGw0QUTwoNAt6yUXvlJcEvwLorcq56RkfOhomrF8z5Ft 2KtQR+34WudfSXjP09AUAmY/3jEau4uDwZFSvgBkRdB7btmGmQ9Ora0HmKVlrO+x7bF6Y8m6HKZ GJxhlwvH0LDwg7265dJbHFLSo9Vp9OJMB1MqX5966IF/8KqBd7pFhNRXkKQBRH/onSMoUsV57ND MKTBCa8Dvs6HZCbuB1FJYIGAiU3alA+TNgy/9RO46qw478TrXIN2FaPVF9rQac6u2ZwNYG/bDrk t21s73g4p5kl6o72eh4ZYzowLNPBgi4qiLL6X+83vX8SPhlCca/0kzXEeqJuJf8xkl64VHW0Jys 5FrdoyuMbBHWxBX+Vhw3yB/dGr7syJ4AAdK6mDhtzIfJ7V+dxNyQW0k/9e8sF6qvt4Ig79zf6P1 QOS2H3YSy6AE9Y6mPAA== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-11-24_02,2026-11-23_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 phishscore=0 clxscore=1015 priorityscore=1501 malwarescore=0 adultscore=0 bulkscore=0 suspectscore=0 impostorscore=0 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2611240051 Add a watchdog driver for the RISC-V SBI watchdog extension. The driver integrates with the Linux watchdog framework to configure and control the watchdog. The driver invokes the SBI watchdog using the standard SBI calls. Support pretimeout event notifications using Message Signaled Interrupts (MSIs). Signed-off-by: Prajwal N --- drivers/watchdog/Kconfig | 10 + drivers/watchdog/Makefile | 1 + drivers/watchdog/riscv_sbi_wdt.c | 442 +++++++++++++++++++++++++++++++ 3 files changed, 453 insertions(+) create mode 100644 drivers/watchdog/riscv_sbi_wdt.c diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index f7d0a0c2c0ef..d6b4e8ccd057 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -2164,6 +2164,16 @@ config STARFIVE_WATCHDOG Say Y here to support the watchdog of StarFive JH7100 and JH7110 SoC. This driver can also be built as a module if choose M. +config RISCV_SBI_WATCHDOG + tristate "RISC-V SBI based Watchdog support" + depends on RISCV_SBI + default RISCV + select WATCHDOG_CORE + help + Say Y here to include support for a watchdog timer + implemented by the Supervisor Binary Interface on RISCV platforms. + Requires firmware support. + # S390 Architecture config DIAG288_WATCHDOG diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index 9ac406677aba..c20c43f06ed0 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -202,6 +202,7 @@ obj-$(CONFIG_WATCHDOG_RTAS) += wdrtas.o # RISC-V Architecture obj-$(CONFIG_ATCWDT200_WATCHDOG) += atcwdt200_wdt.o obj-$(CONFIG_STARFIVE_WATCHDOG) += starfive-wdt.o +obj-$(CONFIG_RISCV_SBI_WATCHDOG) += riscv_sbi_wdt.o # S390 Architecture obj-$(CONFIG_DIAG288_WATCHDOG) += diag288_wdt.o diff --git a/drivers/watchdog/riscv_sbi_wdt.c b/drivers/watchdog/riscv_sbi_wdt.c new file mode 100644 index 000000000000..8e3db9854788 --- /dev/null +++ b/drivers/watchdog/riscv_sbi_wdt.c @@ -0,0 +1,442 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * RISC-V SBI watchdog driver + * + * Copyright (C) 2026 Qualcomm Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DRV_NAME "riscv-sbi-wdt" +#define DRV_VERSION "1.0" + +/* Watchdog default timeout in seconds */ +#define SBI_WATCHDOG_DEFAULT_TIMEOUT 60 + +/* MSI capability mask */ +#define SBI_WATCHDOG_CAP_MSI_SUPPORT_MASK BIT(1) + +/* Convert second to microsecond */ +static inline u32 sec_to_usec(unsigned int sec) +{ + return (u32)(sec * 1000000ULL); +} + +/* Convert microsecond to second and ceil the result */ +static inline unsigned int usec_to_sec_ceil(u32 usec) +{ + return (unsigned int) DIV_ROUND_UP_ULL(usec, 1000000ULL); +} + +/* Convert microsecond to second and floor the result */ +static inline unsigned int usec_to_sec(u32 usec) +{ + return (unsigned int) (usec / 1000000ULL); +} + +/* SBI watchdog attributes */ +struct sbiwdt_attributes { + /* MSI and SSE capability flags */ + u32 wdt_capability; + /* Watchdog state */ + u32 wdt_state; + /* Minimum watchdog timeout supported in us */ + u32 wdt_min_period; + /* Watchdog timeout period in us */ + u32 wdt_period; + /* Time left until watchdog expiry in us */ + u32 wdt_time_left; + /* Watchdog pretimeout in us */ + u32 wdt_notif_time; + /* MSI target address low 32-bit */ + u32 wdt_notif_msi_addr_low; + /* MSI target address high 32-bit */ + u32 wdt_notif_msi_addr_high; + /* MSI data */ + u32 wdt_notif_msi_data; +}; + +/* SBI watchdog global structure */ +struct sbiwdt { + /* Kernel watchdog instance */ + struct watchdog_device wdd; + struct sbiwdt_attributes attrs; + int virq; +}; + +/* Watchdog pretimeout expiry handler */ +static irqreturn_t sbiwdt_pretimeout_handler(int irq, void *dev_id) +{ + struct sbiwdt *wdt = dev_id; + + watchdog_notify_pretimeout(&wdt->wdd); + + return IRQ_HANDLED; +} + +/* Read watchdog attribute */ +static int sbiwdt_read_attribute(enum sbi_ext_wdt_attribute_id attr_id, u32 *attr_val) +{ + struct sbiret sret; + + sret = sbi_ecall(SBI_EXT_WDT, SBI_EXT_WATCHDOG_READ_ATTRIBUTE, attr_id, 0, 0, 0, 0, 0); + *attr_val = sret.value; + + return sbi_err_map_linux_errno(sret.error); +} + +/* Write watchdog attribute */ +static int sbiwdt_write_attribute(enum sbi_ext_wdt_attribute_id attr_id, u32 val) +{ + struct sbiret sret; + + sret = sbi_ecall(SBI_EXT_WDT, SBI_EXT_WATCHDOG_WRITE_ATTRIBUTE, attr_id, val, 0, 0, 0, 0); + + return sbi_err_map_linux_errno(sret.error); +} + +/* Write the MSI address and data attributes in one SBI call*/ +static int sbiwdt_write_notif_msi_message(u32 msi_addr_low, u32 msi_addr_high, u32 msi_data) +{ + struct sbiret sret; + + sret = sbi_ecall(SBI_EXT_WDT, SBI_EXT_WATCHDOG_WRITE_NOTIF_MSI_MESSAGE, msi_addr_low, + msi_addr_high, msi_data, 0, 0, 0); + + return sbi_err_map_linux_errno(sret.error); +} + +/* Start the watchdog */ +static int sbiwdt_start(struct watchdog_device *wdd) +{ + struct sbiret sret; + + sret = sbi_ecall(SBI_EXT_WDT, SBI_EXT_WATCHDOG_START, 0, 0, 0, 0, 0, 0); + + return sbi_err_map_linux_errno(sret.error); +} + +/* Stop the watchdog */ +static int sbiwdt_stop(struct watchdog_device *wdd) +{ + struct sbiret sret; + + sret = sbi_ecall(SBI_EXT_WDT, SBI_EXT_WATCHDOG_STOP, 0, 0, 0, 0, 0, 0); + + return sbi_err_map_linux_errno(sret.error); +} + +/* Pat the watchdog */ +static int sbiwdt_pat(struct watchdog_device *wdd) +{ + struct sbiret sret; + + sret = sbi_ecall(SBI_EXT_WDT, SBI_EXT_WATCHDOG_PAT, 0, 0, 0, 0, 0, 0); + + return sbi_err_map_linux_errno(sret.error); +} + +/* Set the watchdog timeout value */ +static int sbiwdt_set_timeout(struct watchdog_device *wdd, unsigned int timeout) +{ + struct sbiwdt *wdt = watchdog_get_drvdata(wdd); + u32 period_usec; + int ret; + + if (timeout <= wdt->wdd.pretimeout) + return -EINVAL; + + period_usec = sec_to_usec(timeout); + + ret = sbiwdt_write_attribute(SBI_WATCHDOG_PERIOD, period_usec); + if (ret) + return ret; + + wdt->attrs.wdt_period = period_usec; + wdt->wdd.timeout = timeout; + + return 0; +} + +/* Set the watchdog pretimeout value*/ +static int sbiwdt_set_pretimeout(struct watchdog_device *wdd, unsigned int pretimeout) +{ + struct sbiwdt *wdt = watchdog_get_drvdata(wdd); + u32 notif_time_usec; + int ret; + + if (pretimeout >= wdd->timeout) + return -EINVAL; + + notif_time_usec = sec_to_usec(pretimeout); + ret = sbiwdt_write_attribute(SBI_WATCHDOG_NOTIF_TIME, notif_time_usec); + if (ret) + return ret; + + wdt->wdd.pretimeout = pretimeout; + wdt->attrs.wdt_notif_time = notif_time_usec; + + return ret; +} + +/* Get the watchdog timeleft before expiry */ +static unsigned int sbiwdt_get_timeleft(struct watchdog_device *wdd) +{ + struct sbiwdt *wdt = watchdog_get_drvdata(wdd); + unsigned int time_left_usec; + int ret; + + ret = sbiwdt_read_attribute(SBI_WATCHDOG_TIME_LEFT, &time_left_usec); + if (ret) { + dev_err(wdt->wdd.parent, "Failed to read watchdog time left: %d\n", ret); + return 0; + } + + return usec_to_sec(time_left_usec); +} + +static const struct watchdog_info sbiwdt_info = { + .options = WDIOF_SETTIMEOUT | + WDIOF_KEEPALIVEPING | + WDIOF_MAGICCLOSE, + .identity = DRV_NAME, +}; + +static const struct watchdog_info sbiwdt_pretimeout_info = { + .options = WDIOF_SETTIMEOUT | + WDIOF_KEEPALIVEPING | + WDIOF_MAGICCLOSE | + WDIOF_PRETIMEOUT, + .identity = DRV_NAME, +}; + +static const struct watchdog_ops sbiwdt_ops = { + .owner = THIS_MODULE, + .start = sbiwdt_start, + .stop = sbiwdt_stop, + .ping = sbiwdt_pat, + .set_timeout = sbiwdt_set_timeout, + .set_pretimeout = sbiwdt_set_pretimeout, + .get_timeleft = sbiwdt_get_timeleft, +}; + +/* The callback function from IMSIC containing the MSI address and data */ +static void sbiwdt_msi_write(struct msi_desc *desc, struct msi_msg *msg) +{ + int ret; + struct device *dev = msi_desc_to_dev(desc); + struct sbiwdt *wdt; + + if (!dev || !msg) + return; + + wdt = dev_get_drvdata(dev); + if (!wdt) { + dev_err(dev, "Missing SBI watchdog driver data\n"); + return; + } + + /* Store MSI info in attributes*/ + wdt->attrs.wdt_notif_msi_addr_low = msg->address_lo; + wdt->attrs.wdt_notif_msi_addr_high = msg->address_hi; + wdt->attrs.wdt_notif_msi_data = msg->data; + + /* Pass the MSI attribute values to OpenSBI */ + ret = sbiwdt_write_notif_msi_message(wdt->attrs.wdt_notif_msi_addr_low, + wdt->attrs.wdt_notif_msi_addr_high, + wdt->attrs.wdt_notif_msi_data); + if (ret) { + dev_err(dev, "Failed to configure watchdog MSI: %d\n", ret); + return; + } +} + +/* Initialize the watchdog attributes */ +static int sbiwdt_init_attributes(struct sbiwdt *wdt) +{ + int ret; + u32 attr_val; + u32 timeout_usec; + + /* Read watchdog capability from firmware */ + ret = sbiwdt_read_attribute(SBI_WATCHDOG_CAPABILITY, &attr_val); + if (ret) + return ret; + + wdt->attrs.wdt_capability = attr_val; + + /* + * Read watchdog min period supported in firmware + * The firmware ensures the MIN_PERIOD is in valid range + */ + ret = sbiwdt_read_attribute(SBI_WATCHDOG_MIN_PERIOD, &attr_val); + if (ret) + return ret; + + if (!attr_val) + attr_val = 1; + + wdt->attrs.wdt_min_period = attr_val; + wdt->wdd.min_timeout = usec_to_sec_ceil(wdt->attrs.wdt_min_period); + + /* Initialize the watchdog timeout to default value */ + timeout_usec = sec_to_usec(SBI_WATCHDOG_DEFAULT_TIMEOUT); + ret = sbiwdt_write_attribute(SBI_WATCHDOG_PERIOD, timeout_usec); + if (ret) + return ret; + + wdt->attrs.wdt_period = timeout_usec; + wdt->wdd.timeout = SBI_WATCHDOG_DEFAULT_TIMEOUT; + wdt->wdd.max_timeout = U32_MAX / USEC_PER_SEC; + + /* + * Initialize the pretimeout to default value + * The firmware ensures the pretimeout init value is 0 + */ + wdt->attrs.wdt_notif_time = 0; + wdt->wdd.pretimeout = 0; + + return 0; +} + +/* Callback from devm for MSI cleanup */ +static void sbiwdt_msi_free_irqs(void *dev) +{ + platform_device_msi_free_irqs_all((struct device *) dev); +} + +static int sbiwdt_probe(struct platform_device *pdev) +{ + struct sbiwdt *wdt; + const struct imsic_global_config *imsic_global; + struct irq_domain *msi_domain; + struct device *dev = &pdev->dev; + int rc; + + /* Allocate memory for the global watchdog structure */ + wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); + if (!wdt) + return -ENOMEM; + + platform_set_drvdata(pdev, wdt); + watchdog_set_drvdata(&wdt->wdd, wdt); + + /* Initialiaze watchdog attributes */ + rc = sbiwdt_init_attributes(wdt); + if (rc) + return dev_err_probe(dev, rc, "Failed to read SBI watchdog attributes\n"); + + wdt->wdd.parent = dev; + wdt->wdd.info = &sbiwdt_info; + wdt->wdd.ops = &sbiwdt_ops; + + watchdog_stop_on_unregister(&wdt->wdd); + watchdog_stop_on_reboot(&wdt->wdd); + + /* Setup MSI if the capability is available */ + if (wdt->attrs.wdt_capability & SBI_WATCHDOG_CAP_MSI_SUPPORT_MASK) { + imsic_global = imsic_get_global_config(); + if (!imsic_global) + return dev_err_probe(dev, -ENODEV, "IMSIC firmware node is unavailable\n"); + + msi_domain = irq_find_matching_fwnode(imsic_global->fwnode, + DOMAIN_BUS_PLATFORM_MSI); + + if (!msi_domain) + return dev_err_probe(dev, -ENODEV, + "IMSIC platform MSI domain is unavailable\n"); + + dev_set_msi_domain(&pdev->dev, msi_domain); + + /* MSI setup */ + rc = platform_device_msi_init_and_alloc_irqs(dev, 1, sbiwdt_msi_write); + if (rc) + return dev_err_probe(dev, rc, "Failed to allocate MSIs\n"); + + /* Register with devm the MSI cleanup callback */ + rc = devm_add_action_or_reset(dev, sbiwdt_msi_free_irqs, dev); + if (rc) + return dev_err_probe(dev, rc, "Failed to add sbiwdt MSI cleanup\n"); + + /* Enable pretimeout only when the MSI is allocated */ + wdt->wdd.info = &sbiwdt_pretimeout_info; + + wdt->virq = msi_get_virq(dev, 0); + if (!wdt->virq) + return dev_err_probe(dev, -ENODEV, "Failed to get MSI virtual IRQ\n"); + + rc = devm_request_irq(dev, wdt->virq, + sbiwdt_pretimeout_handler, + 0, "sbiwdt-pretimeout", wdt); + if (rc) + return dev_err_probe(dev, rc, "Failed to request pretimeout IRQ\n"); + } + + rc = devm_watchdog_register_device(dev, &wdt->wdd); + if (rc) + return dev_err_probe(dev, rc, "Failed to register RISC-V SBI watchdog device\n"); + + dev_info(&pdev->dev, "RISC-V SBI Watchdog registered\n"); + + return 0; +} + +static struct platform_driver sbiwdt_driver = { + .driver = { + .name = DRV_NAME, + }, + .probe = sbiwdt_probe, +}; + +static struct platform_device *sbiwdt_pdev; + +static void __exit sbiwdt_exit(void) +{ + platform_device_unregister(sbiwdt_pdev); + platform_driver_unregister(&sbiwdt_driver); +} + +static int __init sbiwdt_init(void) +{ + int ret; + struct platform_device *pdev; + + /* Probe for SBI WDT extension */ + if (sbi_spec_version < sbi_mk_version(3, 0) || + sbi_probe_extension(SBI_EXT_WDT) <= 0) { + return -ENODEV; + } + + ret = platform_driver_register(&sbiwdt_driver); + if (ret) + return ret; + + pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0); + if (IS_ERR(pdev)) { + platform_driver_unregister(&sbiwdt_driver); + return PTR_ERR(pdev); + } + + sbiwdt_pdev = pdev; + + return 0; +} +late_initcall(sbiwdt_init); +module_exit(sbiwdt_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Prajwal N "); +MODULE_DESCRIPTION("RISC-V SBI Watchdog Driver"); -- 2.43.0