Skip to contentSkip to conversation

Watchdog support for qemu arm and riscv

PATCH Superseded Jan Kiszka 2 participants 5 messages Sep 4 – Oct 2

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

v2 cover Watchdog support for qemu arm and riscv

On non-x86, QEMU only offers a watchdog via the PCI-attached Intel 6300 ESB. Add a driver for it and make sure it is probed during board init.

Background for adding this feature is testing complex system integrations that include watchdog-monitored OTA OS updates, e.g. [1].

Changes in v2:
- fix a compilation issue of the driver on riscv
- add late probing to qemu

Jan

[1] https://gitlab.com/cip-project/cip-core/isar-cip-core

Jan Kiszka (2):
watchdog: Add support for i6300esb
board: qemu: arm/riscv: Probe watchdogs late

 board/emulation/qemu-arm/qemu-arm.c     |   5 +
 board/emulation/qemu-riscv/qemu-riscv.c |   5 +
 drivers/watchdog/Kconfig                |   9 ++
 drivers/watchdog/Makefile               |   1 +
 drivers/watchdog/i6300esb-wdt.c         | 147 ++++++++++++++++++++++++
 5 files changed, 167 insertions(+)
 create mode 100644 drivers/watchdog/i6300esb-wdt.c
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH v2 1/2 watchdog: Add support for i6300esb

From: Jan Kiszka <[email protected]>

While the real hardware might be in the museum now, this allows to test watchdog integrations against QEMU virtual machines (-device i6300esb).

---
 drivers/watchdog/Kconfig        |   9 ++
 drivers/watchdog/Makefile       |   1 +
 drivers/watchdog/i6300esb-wdt.c | 147 ++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+)
 create mode 100644 drivers/watchdog/i6300esb-wdt.c
drivers/watchdog/Kconfig +9 -0
index 3c56c541505..225e23ce247 100644
@@ -494,4 +494,13 @@ config WDT_FTWDT010
 	help
 	  Faraday Technology ftwdt010 watchdog is an architecture independent
 	  watchdog. It is usually used in SoC chip design.
+
+config WDT_I6300ESB
+	bool "Intel 6300ESB Watchdog"
+	depends on WDT && PCI
+	help
+	  Hardware driver for the watchdog timer built into the Intel
+	  6300ESB controller hub. Also emulated by QEMU, thus useful for
+	  testing watchdog functionality in virtual systems.
+
 endmenu
drivers/watchdog/Makefile +1 -0
index 84faefdb4c2..cfe3f3dac5a 100644
@@ -58,3 +58,4 @@ obj-$(CONFIG_WDT_TEGRA) += tegra_wdt.o
 obj-$(CONFIG_WDT_XILINX) += xilinx_wwdt.o
 obj-$(CONFIG_WDT_ADI) += adi_wdt.o
 obj-$(CONFIG_WDT_QCOM) += qcom-wdt.o
+obj-$(CONFIG_WDT_I6300ESB) += i6300esb-wdt.o
drivers/watchdog/i6300esb-wdt.c new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v2 1/2] watchdog: Add support for i6300esb

Hi Jan,

sorry for the delay. A few comments below.

drivers/watchdog/i6300esb-wdt.c line 56

Quoted 11 lines · Jan Kiszka
drivers/watchdog/i6300esb-wdt.c new file +11 -0
+static int i6300esb_wdt_start(struct udevice *dev, u64 ms, ulong flags)
+{
+	struct i6300_wdt *wdt = dev_get_priv(dev);
+	uint32_t val;
+
+	/*
+	 * Scale to ns, then divide by 983040 ns, the period of the timer
+	 * (30 ns PCI bus tick * 2^15). This is timer 1 (IRQ). Timer 2
+	 * (reboot) is set to immediate expiry.
+	 */
+	val = ms * 1000000ULL / 983040;

There is no upper limit here. The preload registers are 20 bits wide (Linux caps the heartbeat at 2046s, 2046 << 9 per timer, just below 2^20), so with your scaling anything above roughly 1030s silently wraps. Please clamp it. Once Juuso's series ("watchdog: report effective timeout") is in next, setting max_timeout_ms in uc_plat in probe would be the preferred way.

drivers/watchdog/i6300esb-wdt.c line 64

Quoted 8 lines
drivers/watchdog/i6300esb-wdt.c new file +8 -0
+
+	unlock_timer_regs(wdt);
+	writel(val, ESB_TIMER1_REG(wdt));
+
+	unlock_timer_regs(wdt);
+	writel(0, ESB_TIMER2_REG(wdt));
+
+	dm_pci_write_config8(dev, ESB_LOCK_REG, ESB_WDT_ENABLE);

Linux does a reload after writing the preload values (esb_timer_set_heartbeat()) and again in esb_timer_start() before enabling. Without it the new value is only picked up with the first wdt_reset(), so the first period still runs with whatever was preloaded before. I'd add the reload here, before enabling.

Quoted 2 lines
(quoted diff) +2 -0
+static int i6300esb_wdt_stop(struct udevice *dev)
+{

[...]

drivers/watchdog/i6300esb-wdt.c line 82

Quoted 2 lines
drivers/watchdog/i6300esb-wdt.c new file +2 -0
+	/* Returns 0 if the timer was disabled, non-zero otherwise */
+	return val & ESB_WDT_ENABLE;

That's taken over from Linux, but in U-Boot the callers expect 0 or a negative error code. Something like -EBUSY when the enable bit is still set (locked) would fit better.

drivers/watchdog/i6300esb-wdt.c line 101

Quoted 7 lines
drivers/watchdog/i6300esb-wdt.c new file +7 -0
+static int i6300esb_wdt_probe(struct udevice *dev)
+{
+	struct i6300_wdt *wdt = dev_get_priv(dev);
+	uint8_t val;
+
+	wdt->base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0,
+				   PCI_REGION_TYPE, PCI_REGION_MEM);

Please check for NULL here.

Nit: checkpatch --strict prefers u32/u8 over uint32_t/uint8_t.

Thanks,
Stefan

author
#3 lore raw

PATCH v2 2/2 board: qemu: arm/riscv: Probe watchdogs late

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

The only watchdog QEMU offers us on non-x86 virt platforms is the i6300esb. This one is PCI-attached, but watchdog probing will happen before PCI, so will miss it. Redo this late so that we gain autostart support.

Deliberately limited to contemporary ARM and RISC-V.

---
 board/emulation/qemu-arm/qemu-arm.c     | 5 +++++
 board/emulation/qemu-riscv/qemu-riscv.c | 5 +++++
 2 files changed, 10 insertions(+)
board/emulation/qemu-arm/qemu-arm.c +5 -0
index 38f0ec5f2fb..529bffa3e49 100644
@@ -14,6 +14,7 @@
 #include <usb.h>
 #include <virtio_types.h>
 #include <virtio.h>
+#include <wdt.h>
 
 #include <linux/kernel.h>
 #include <linux/sizes.h>
@@ -114,6 +115,10 @@ int board_late_init(void)
 	if (CONFIG_IS_ENABLED(USB_KEYBOARD))
 		usb_init();
 
+	/* watchdog may come via PCI but was probed too early */
+	if (CONFIG_IS_ENABLED(WDT))
+		initr_watchdog();
+
 	return 0;
 }
 
board/emulation/qemu-riscv/qemu-riscv.c +5 -0
index ce5bc34ca71..5ed21b286ee 100644
@@ -14,6 +14,7 @@
 #include <usb.h>
 #include <virtio_types.h>
 #include <virtio.h>
+#include <wdt.h>
 
 #if IS_ENABLED(CONFIG_MTD_NOR_FLASH)
 int is_flash_available(void)
@@ -38,6 +39,10 @@ int board_late_init(void)
 	 */
 	virtio_init();
 
+	/* watchdog may come via PCI but was probed too early */
+	if (CONFIG_IS_ENABLED(WDT))
+		initr_watchdog();
+
 	return 0;
 }
 
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH v2 2/2] board: qemu: arm/riscv: Probe watchdogs late

Hi Jan,

board/emulation/qemu-arm/qemu-arm.c int board_late_init(void) · line 120

Quoted 3 lines · Jan Kiszka
board/emulation/qemu-arm/qemu-arm.c +3 -0
+	/* watchdog may come via PCI but was probed too early */
+	if (CONFIG_IS_ENABLED(WDT))
+		initr_watchdog();

initr_watchdog() walks all WDT devices again, not only the new PCI one. A watchdog that was already started in the first pass gets wdt_start() a second time, and with SYSRESET_WATCHDOG_AUTO sysreset_register_wdt() binds a second wdt_reboot device for it.

On qemu virt there is no other watchdog today, so this is harmless right now. But I'd rather not rely on that. Either skip devices that are already running in init_watchdog_dev(), or only handle the i6300esb here.

Thanks,
Stefan