Skip to contentSkip to conversation

mfd: ls2kbmc: multiple fixes for this driver

RFC PATCH Superseded Miao Wang 4 participants 20 messages Jul 7 – Jul 8

Subsystems (4)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Miao Wang

author
#0 lore raw

v2 cover mfd: ls2kbmc: multiple fixes for this driver

Previously, the driver has been introduced to support the Loongson 2K BMC running on the Loongson Servers, which is essential to prevent the system from hanging when the BMC is being reset and the default efi-framebuffer is being used. However, there are some drawbacks in the driver.

Firstly, the driver tries to read and write to the connected PCI-E host controller registers, assuming that the BMC is connected to LS7A PCI-E host controller. This assumption should be true for real products, but to prevent from accidentally reading and writing to the wrong PCI-E host controller, this driver should be modified to check this before accessing the registers.

Secondly, the driver uses non-exported functions to tell the vt subsystem to redraw the screen, preventing the driver from being compiling as a module. This can be fixed by using the exported functions instead.

Thirdly, the driver directly accesses the GPIO controller registers using hard-coded addresses, which might conflict with the loaded GPIO controller driver for the same GPIO controller. This is fixed in this series by using the GPIO subsystem APIs instead. However, legacy GPIO APIs have to be used in this fixed to correctly request a GPIO descriptor from the GPIO subsystem, which might be further discussed to find a better solution.

Finally, there is a minor issue in the driver where it changes the mode string describing the screen resolution during probing, which prevents the device from being probed again if -EPROBE_DEFER is returned by the probe function.

I have tested the changes in this series on a single-socket Loongson 3C6000 server with a Loongson 2K BMC, and the driver works as expected when the corresponding GPIO driver is additionally loaded.

---
Changes in v2:
- Several fixes suggested by the Sashiko AI review bot
- Add a cleanup function for the wq on removal of the device
- Relax the reverse dependency from CONFIG_IPMI_LS2K to CONFIG_MFD_LS2K_BMC_CORE to allow the driver to be built as a module
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Miao Wang (7):
mfd: ls2kbmc: Make a copy when parsing mode string
mfd: ls2kbmc: Sanity check for the connected pci port
mfd: ls2kbmc: Redraw using exported functions
mfd: ls2kbmc: Cancel the work queue on removal
ipmi: ls2k: Relax the dependency to its mfd driver
mfd: ls2kbmc: Able to be compiled as a module
mfd: ls2kbmc: Capture the reset event of BMC through GPIO

 drivers/char/ipmi/Kconfig   |   2 +-
 drivers/mfd/Kconfig         |   2 +-
 drivers/mfd/ls2k-bmc-core.c | 237 ++++++++++++++++++++++++++++++++++----------
 3 files changed, 186 insertions(+), 55 deletions(-)

---
base-commit: 0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53
change-id: 20260626-ls2kbmc-mod-5209193009b2

Best regards,

Signature · 1 line
Miao Wang <[email protected]>
author
#1 lore raw

RFC PATCH v2 1/7 mfd: ls2kbmc: Make a copy when parsing mode string

When parsing the mode string from BMC, the string is manipulated in-place with strsep(), preventing from parsing it again. Make a copy of the original string and manipulate the copy instead to fix this.

Fixes 0d64f6d1ffe9 ("mfd: ls2kbmc: Introduce Loongson-2K BMC core driver")
---
 drivers/mfd/ls2k-bmc-core.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
drivers/mfd/ls2k-bmc-core.c +25 -5
index 408056bfb2fe757a5bde43775a483a48352e706d..27f6e096404d67459038a0607378057ec7ef69ab 100644
@@ -427,34 +427,54 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	/* Assume 64 bytes is enough for the resolution string */
+	char mode_buf[64], mode_buf_orig[64];
+	char *mode = mode_buf;
+	const void __iomem *mode_base;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
-	if (!mode)
+	mode_base = ioremap(pci_resource_start(pdev, 0) + SZ_16M,
+			    sizeof(mode_buf));
+	if (!mode_base)
 		return -ENOMEM;
+	memcpy_fromio(mode_buf, mode_base, sizeof(mode_buf) - 1);
+	mode_buf[sizeof(mode_buf) - 1] = '\0';
+	iounmap((void __iomem *)mode_base);
+	memcpy(mode_buf_orig, mode_buf, sizeof(mode_buf_orig));
 
 	/* The resolution field starts with the flag "video=". */
 	if (!strncmp(mode, "video=", 6))
 		mode = mode + 6;
+	else
+		goto invalid_mode;
 
-	ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);
+	ret = kstrtouint(strsep(&mode, "x"), 10, &pd->width);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
-	ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);
+	ret = kstrtouint(strsep(&mode, "-"), 10, &pd->height);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
 	ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
 	pd->stride = pd->width * depth / 8;
 	pd->format = depth == 32 ? "a8r8g8b8" : "r5g6b5";
 
 	return 0;
+
+invalid_mode:
+	dev_err(&pdev->dev, "Invalid resolution string: %s\n", mode_buf_orig);
+	return -EINVAL;
 }
 
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH RFC v2 1/7] mfd: ls2kbmc: Make a copy when parsing mode string

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [High] Regression: The mode parsing function rejects valid resolution strings that lack an '@' delimiter.

Pre-existing issues:
- [High] Data race and subsystem violation: Mutating global static mfd_cell array during probe.
- [High] Integer overflow in stride calculation leading to out-of-bounds memory write.
- [High] Out-of-bounds MMIO read due to hardcoded 16MB offset without BAR size validation.
- [High] NULL pointer dereference when device is on a root bus.
--

--- Patch [1]: [PATCH RFC v2 1/7] mfd: ls2kbmc: Make a copy when parsing mode string --- commit 94d2e8a4058d05742414ee9f80cebb8494e161f9
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: Make a copy when parsing mode string

When parsing the mode string from BMC, the string is manipulated in-place with strsep(), preventing from parsing it again. Make a copy of the original string and manipulate the copy instead to fix this.

Quoted 12 lines
drivers/mfd/ls2k-bmc-core.c +3 -1
index 408056bfb2fe7..27f6e096404d6 100644
@@ -427,34 +427,54 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	/* Assume 64 bytes is enough for the resolution string */
+	char mode_buf[64], mode_buf_orig[64];
+	char *mode = mode_buf;

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 440

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +4 -2
+	const void __iomem *mode_base;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
-	if (!mode)
+	mode_base = ioremap(pci_resource_start(pdev, 0) + SZ_16M,
+			    sizeof(mode_buf));
+	if (!mode_base)
 		return -ENOMEM;

[Severity: High]
This is a pre-existing issue, but does this ioremap() call safely handle small PCI resources?

The code maps memory at a hardcoded offset (SZ_16M) into PCI BAR0, but it doesn't verify that the BAR size actually spans past 16MB.

If a malicious or misconfigured device advertises a much smaller BAR0 (like 4KB), could this lead to an out-of-bounds MMIO read outside the device's assigned resources?

[ ... ]

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 468

Quoted 5 lines
drivers/mfd/ls2k-bmc-core.c +2 -0
 	ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;

[Severity: High]
If the resolution string doesn't contain an '@' delimiter (for example, a standard string like "video=1920x1080-32"), won't strsep() set mode to NULL?

Does this new check erroneously reject valid configuration strings that previously worked fine, causing the driver to fail during probe?

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 470

Quoted 2 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
 	pd->stride = pd->width * depth / 8;

[Severity: High]
This isn't a bug introduced by this patch, but can this multiplication overflow?

The values for pd->width and depth are parsed from the untrusted BMC string without bounds checking.

If a large width is provided, multiplying it by depth could overflow a 32-bit integer, resulting in a small stride. Could this lead to an out-of-bounds memory write in downstream framebuffer code?

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 478

Quoted 8 lines
drivers/mfd/ls2k-bmc-core.c +4 -0
 	pd->format = depth == 32 ? "a8r8g8b8" : "r5g6b5";
 
 	return 0;
+
+invalid_mode:
+	dev_err(&pdev->dev, "Invalid resolution string: %s\n", mode_buf_orig);
+	return -EINVAL;
 }

[Severity: High]
This is a pre-existing issue, but I noticed a potential data race and MFD subsystem violation in ls2k_bmc_probe():

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
	...
	ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
	ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
	...
}

The driver assigns a local stack pointer (&pd) to the global static mfd_cell array ls2k_bmc_cells.

If concurrent probes of this driver occur, won't this cause a data race?

Also, doesn't dynamically modifying global static mfd_cell arrays violate MFD architectural guidelines?

[Severity: High]
This is another pre-existing issue, but I noticed a potential NULL pointer dereference in ls2k_bmc_save_pci_data():

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_save_pci_data() {
	struct pci_dev *parent = pdev->bus->self;
	...
	pci_read_config_dword(parent, PCI_COMMAND, &ddata->bridge_pci_data.pci_command);
}

If the device is on a root bus (for example, directly plugged into a root port or attached via Thunderbolt without an intermediate bridge), pdev->bus->self will be NULL.

Won't this unconditionally dereference a NULL pointer when reading the config dword?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

RFC PATCH v2 2/7 mfd: ls2kbmc: Sanity check for the connected pci port

In reply to Miao Wang

When the bmc resets, the recovery procedure require to reconfigure the parent device. The driver assumes that the parent device should be LS7A. Add a sanity check on initialization to ensure this and prevent from accidentally operating on non-LS7A ports.

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
---
 drivers/mfd/ls2k-bmc-core.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/mfd/ls2k-bmc-core.c +32 -0
index 27f6e096404d67459038a0607378057ec7ef69ab..b02e4955e9b04f517892a18b2ef103b5e481a238 100644
@@ -35,6 +35,15 @@
 #define LS2K_IPMI3_RES_START		(LS2K_IPMI2_RES_START + LS2K_IPMI_RES_SIZE)
 #define LS2K_IPMI4_RES_START		(LS2K_IPMI3_RES_START + LS2K_IPMI_RES_SIZE)
 
+/* LS7A port Device IDs */
+#define DEV_LS7A1K_PCIE_PORT0	0x7a09
+#define DEV_LS7A1K_PCIE_PORT1	0x7a19
+#define DEV_LS7A1K_PCIE_PORT2	0x7a29
+#define DEV_LS7A2K_PCIE_PORT0	0x7a39
+#define DEV_LS7A2K_PCIE_PORT1	0x7a49
+#define DEV_LS7A2K_PCIE_PORT2	0x7a59
+#define DEV_LS7A2K_PCIE_PORT3	0x7a69
+
 #define LS7A_PCI_CFG_SIZE		0x100
 
 /* LS7A bridge registers */
@@ -477,6 +486,24 @@ static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_da
 	return -EINVAL;
 }
 
+static const struct pci_device_id ls7a_ports[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT0) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT1) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT2) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT0) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT1) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT2) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT3) },
+	{ }
+};
+
+static bool ls2k_check_parent(struct pci_dev *dev)
+{
+	struct pci_dev *parent = dev->bus->self;
+
+	return parent && pci_match_id(ls7a_ports, parent) != NULL;
+}
+
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
 {
 	struct simplefb_platform_data pd;
@@ -488,6 +515,11 @@ static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
 	if (ret)
 		return ret;
 
+	if (!ls2k_check_parent(dev)) {
+		dev_err(&dev->dev, "Expected to be connected to LS7A PCI-E port\n");
+		return -ENODEV;
+	}
+
 	ddata = devm_kzalloc(&dev->dev, sizeof(*ddata), GFP_KERNEL);
 	if (!ddata)
 		return -ENOMEM;
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH RFC v2 2/7] mfd: ls2kbmc: Sanity check for the connected pci port

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but the driver executes a 10-second mdelay() inside a stop_machine() context, severely freezing the system and risking NMI watchdog panics.
- [High] This problem wasn't introduced by this patch, but the driver dynamically modifies a global, non-const mfd_cell array during probe(), violating MFD subsystem guidelines and creating a concurrency data race.
- [High] This problem wasn't introduced by this patch, but the shared interrupt handler unconditionally returns IRQ_HANDLED without checking hardware registers, breaking shared IRQ lines.
- [High] This problem wasn't introduced by this patch, but the driver unconditionally unregisters the ACPI GSI immediately after successfully requesting the GPIO IRQ.
--

--- Patch [2]: [PATCH RFC v2 2/7] mfd: ls2kbmc: Sanity check for the connected pci port --- commit 196295bb9b62c28a067b90bc276ab4463c9f9b51
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: Sanity check for the connected pci port

This commit adds a sanity check to the ls2k_bmc_probe() function to ensure the device is connected to a supported LS7A PCI-E port. This prevents the driver from accidentally operating on non-LS7A ports when the BMC resets and requires parent device reconfiguration.

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index 27f6e096404d6..b02e4955e9b04 100644

[ ... ]

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id) · line 525

Quoted 12 lines
drivers/mfd/ls2k-bmc-core.c +5 -0
@@ -488,6 +515,11 @@ static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
 	if (ret)
 		return ret;
 
+	if (!ls2k_check_parent(dev)) {
+		dev_err(&dev->dev, "Expected to be connected to LS7A PCI-E port\n");
+		return -ENODEV;
+	}
+
 	ddata = devm_kzalloc(&dev->dev, sizeof(*ddata), GFP_KERNEL);
 	if (!ddata)
 		return -ENOMEM;

[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential data race when assigning a stack address to a global array later in the probe function:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
    ...
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
    ...
}

Does this create a race condition if multiple instances of this device are probed simultaneously?

Since ls2k_bmc_cells is not marked as const:

drivers/mfd/ls2k-bmc-core.c:
static struct mfd_cell ls2k_bmc_cells[] = {

Could multiple probes overwrite this shared pointer with their own stack addresses, violating the MFD subsystem guidelines that require mfd_cell arrays to be static const?

After probe() completes, does this leave a dangling pointer to popped stack memory in the global array?

[Severity: Critical]
This is a pre-existing issue, but I noticed a potentially severe problem with how delays are handled during the reset work:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_events_fn() {
    ...
    stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);
    ...
}

Inside the callback:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_recover_pci_data() {
    ...
    mdelay(LS2K_BMC_RESET_WAIT_TIME);
    ...
}

Since stop_machine() pauses execution on all other CPUs with interrupts disabled, does delaying for 10,000 milliseconds inside ls2k_bmc_recover_pci_data() completely freeze the system?

Could this trigger hardware or software NMI watchdogs and lead to a panic?

[Severity: High]
This isn't a bug introduced by this patch, but there seems to be an issue with how the shared interrupt is handled:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
    ret = devm_request_irq(&pdev->dev, pdev->irq, ls2k_bmc_interrupt,
                           IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc pcie", ddata);
    ...
}
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
    ...
    return IRQ_HANDLED;
}

Since the interrupt is requested with IRQF_SHARED, is it safe for ls2k_bmc_interrupt() to unconditionally return IRQ_HANDLED without checking hardware registers?

If another device sharing the IRQ line fires an interrupt, does this falsely claim the interrupt and break the IRQ core's spurious interrupt detection?

[Severity: High]
This is a pre-existing issue, but the GPIO IRQ cleanup path looks problematic on successful initialization:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
    acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
    return ret;
}

Does this code unconditionally unregister the ACPI GSI immediately after successfully requesting the GPIO IRQ?

Could destroying the mapping while the requested interrupt handler is still active break the interrupt routing and prevent it from firing?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

RFC PATCH v2 3/7 mfd: ls2kbmc: Redraw using exported functions

In reply to Miao Wang

Use update_screen, i.e. redraw_screen() to trigger the redraw of the current vt.

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
---
 drivers/mfd/ls2k-bmc-core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/mfd/ls2k-bmc-core.c +4 -1
index b02e4955e9b04f517892a18b2ef103b5e481a238..a5465c42a77b8b7b81e3ad787d6036679c6ba6df 100644
@@ -25,6 +25,7 @@
 #include <linux/platform_device.h>
 #include <linux/stop_machine.h>
 #include <linux/vt_kern.h>
+#include <linux/console.h>
 
 /* LS2K BMC resources */
 #define LS2K_DISPLAY_RES_START		(SZ_16M + SZ_2M)
@@ -310,7 +311,9 @@ static void ls2k_bmc_events_fn(struct work_struct *work)
 
 	if (IS_ENABLED(CONFIG_VT)) {
 		/* Re-push the display due to previous PCI-E loss. */
-		set_console(vt_move_to_console(MAX_NR_CONSOLES - 1, 1));
+		console_lock();
+		update_screen(vc_cons[fg_console].d);
+		console_unlock();
 	}
 }
 
Signature · 1 line
2.49.0
#6 lore raw

Re: [PATCH RFC v2 3/7] mfd: ls2kbmc: Redraw using exported functions

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The driver executes a massive 10-second busy-wait loop (mdelay(10000)) inside a stop_machine execution, causing a hard lockup.
- [High] The driver initializes and schedules a work item but does not provide a .remove callback or devres action to cancel it on unbind, leading to a potential use-after-free.
- [High] The probe function dynamically mutates the global static ls2k_bmc_cells array using a stack-allocated pointer, violating MFD rules and risking a data race condition.
- [High] The driver unconditionally unregisters an ACPI GSI immediately after successfully requesting its IRQ, breaking the interrupt routing.
- [High] The shared interrupt handler lacks source verification, causing any shared IRQ to spuriously trigger a 10-second system stall.
--

--- Patch [3]: [PATCH RFC v2 3/7] mfd: ls2kbmc: Redraw using exported functions --- commit 262b4b899dbe26d6f3d0f812e42256a7beea8a22
Author: Miao Wang <[email protected]>
Subject: mfd: ls2kbmc: Redraw using exported functions

This commit modifies the redraw trigger for the current VT. It switches from using set_console(vt_move_to_console(...)) to using console_lock() and update_screen() directly.

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index b02e4955e9b04..a5465c42a77b8 100644

[ ... ]

drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_events_fn(struct work_struct *work) · line 318

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +3 -1
@@ -310,7 +311,9 @@ static void ls2k_bmc_events_fn(struct work_struct *work)
 
 	if (IS_ENABLED(CONFIG_VT)) {
 		/* Re-push the display due to previous PCI-E loss. */
-		set_console(vt_move_to_console(MAX_NR_CONSOLES - 1, 1));
+		console_lock();
+		update_screen(vc_cons[fg_console].d);
+		console_unlock();
 	}
 }

[Severity: High]
This is a pre-existing issue, but does this missing remove callback cause a use-after-free?

The work structure is initialized inside devres-managed memory:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
    INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
    ...
}

But there is no remove callback to cancel it:

drivers/mfd/ls2k-bmc-core.c:
static struct pci_driver ls2k_bmc_driver = {
.name = "ls2k-bmc",
.id_table = ls2k_bmc_devices,
.probe = ls2k_bmc_probe,
};

If the driver is unbound via sysfs while bmc_reset_work is queued or running, the devres framework will unregister the IRQ and free the ddata structure. Without a remove function executing cancel_work_sync(), will the workqueue execute ls2k_bmc_events_fn() and access the freed memory?

[Severity: High]
This isn't a bug introduced by this patch, but does the probe function mutate a shared global array?

The global array is defined as non-const:

drivers/mfd/ls2k-bmc-core.c:
static struct mfd_cell ls2k_bmc_cells[] = {

And the probe function dynamically assigns a stack-allocated pointer to it:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
    ...
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
    ...
}

Although devm_mfd_add_devices() creates a copy of the data, the global array retains the dangling stack pointer after probe() returns. If multiple instances of this driver probe concurrently, could this cause a data race by overwriting this shared global pointer?

[Severity: Critical]
This is a pre-existing issue, but does this stop_machine() call lock up the system?

The recovery function executes a massive 10-second busy-wait loop:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_recover_pci_data() {
    ...
    /* Waiting for U-Boot and DDR ready */
    mdelay(LS2K_BMC_RESET_WAIT_TIME);
    ...
}

And this function is executed via stop_machine():

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_events_fn() {
    ...
    stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);
    ...
}

The stop_machine() mechanism disables local interrupts and halts all CPUs in the system to safely execute the callback. Does halting all CPUs with interrupts globally disabled for 10-12 seconds fatally hang the system and trigger NMI watchdogs?

[Severity: High]
This isn't a bug introduced by this patch, but is the GSI mapping destroyed while the IRQ is actively requested?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
    acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
    ...
}

In ls2k_bmc_init(), the driver allocates an IRQ from a GSI using acpi_register_gsi() and then claims it via devm_request_irq(). Does unconditionally calling acpi_unregister_gsi() immediately after requesting it dispose of the IRQ domain mapping and break the interrupt routing?

[Severity: High]
This is a pre-existing issue, but does the shared interrupt handler properly verify its source?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
    ...
    /* Skip interrupt in LS2K_BMC_INT_INTERVAL */
    if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {
        schedule_work(&ddata->bmc_reset_work);
        last_jiffies = jiffies;
    }
    ...
}

The handler schedules hardware reset work unconditionally without reading any hardware status register to verify if the BMC actually asserted the interrupt. Since the PCIe and GPIO interrupts are registered with IRQF_SHARED, if any other device shares this IRQ line, will its interrupts spuriously trigger the BMC reset sequence and repeatedly stall the system in stop_machine()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#7 lore raw

RFC PATCH v2 4/7 mfd: ls2kbmc: Cancel the work queue on removal

In reply to Miao Wang

When the device is being removeed, the work queue should be canceled to avoid any pending work to be executed after the device is removed.

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
---
 drivers/mfd/ls2k-bmc-core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/mfd/ls2k-bmc-core.c +8 -0
index a5465c42a77b8b7b81e3ad787d6036679c6ba6df..f87224105b3720cca97dcef089dad63fe57bc8c2 100644
@@ -375,6 +375,12 @@ static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata *
 	pci_read_config_dword(pdev, PCI_INTERRUPT_LINE, &ddata->bmc_pci_data.interrupt_line);
 }
 
+static void ls2k_bmc_cancel_wq(void *data)
+{
+	struct ls2k_bmc_ddata *ddata = data;
+	(void) cancel_work_sync(&ddata->bmc_reset_work);
+}
+
 static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 {
 	struct pci_dev *pdev = to_pci_dev(ddata->dev);
@@ -385,6 +391,8 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 
 	INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
 
+	devm_add_action_or_reset(ddata->dev, ls2k_bmc_cancel_wq, ddata);
+
 	ret = devm_request_irq(&pdev->dev, pdev->irq, ls2k_bmc_interrupt,
 			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc pcie", ddata);
 	if (ret) {
Signature · 1 line
2.49.0
#8 lore raw

Re: [PATCH RFC v2 4/7] mfd: ls2kbmc: Cancel the work queue on removal

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

New issues:
- [High] Missing check of devm_add_action_or_reset() return value leads to use-after-free on device unbind.

Pre-existing issues:
- [Critical] The driver requests a shared IRQ (IRQF_SHARED) but does not verify the hardware interrupt status, causing unrelated interrupts to trigger system-wide lockups.
- [High] Executing an extreme delay (mdelay() for 10+ seconds) inside a stop_machine() callback will trigger the NMI watchdog and cause a system panic.
- [High] Dynamic mutation of a global, non-const mfd_cell array using a pointer to a stack-allocated variable.
- [High] Premature unregistration of the ACPI GSI-to-IRQ mapping while the IRQ is actively requested.
- [High] A static variable is used for rate-limiting inside the interrupt handler, ignoring all interrupts for the first 6 minutes of system uptime and causing data races. --

--- Patch [4]: [PATCH RFC v2 4/7] mfd: ls2kbmc: Cancel the work queue on removal --- commit 99a0d7b57f4c27f804053725b5bbccb398931b78
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: Cancel the work queue on removal

When the device is being removeed, the work queue should be canceled to avoid any pending work to be executed after the device is removed.

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index a5465c42a77b8..f87224105b372 100644

[ ... ]

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 392

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
@@ -385,6 +391,8 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 
 	INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);

[Severity: High]
This is a pre-existing issue, but can the delay in this work item trigger the NMI watchdog?

Looking at ls2k_bmc_events_fn(), it initiates a stop_machine() context which halts all CPUs and disables interrupts globally:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_events_fn() {
        ...
        stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);
        ...
}

Inside the callback, it busy-waits for 10 seconds:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_recover_pci_data() {
        ...
        mdelay(LS2K_BMC_RESET_WAIT_TIME);
        ...
}

Halting all CPUs with interrupts disabled for this long seems like it would cause a system panic.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 394

Quoted 2 lines
drivers/mfd/ls2k-bmc-core.c +1 -0
+	devm_add_action_or_reset(ddata->dev, ls2k_bmc_cancel_wq, ddata);

[Severity: High]
Does the return value of devm_add_action_or_reset() need to be checked here?

If devm_add_action_or_reset() fails due to memory pressure, it immediately executes the cleanup action and returns an error. Because this error is ignored, probe continues successfully and the workqueue becomes active.

When the device is later unbound, the action is missing from the devres list, so the workqueue is not canceled before its ddata structure is freed, which could lead to a use-after-free regression.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 397

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +1 -0
+
 	ret = devm_request_irq(&pdev->dev, pdev->irq, ls2k_bmc_interrupt,
 			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc pcie", ddata);

[Severity: Critical]
This is a pre-existing issue, but does this shared IRQ handler verify its hardware interrupt status before scheduling work?

Since this IRQ is requested with IRQF_SHARED, the interrupt line could be shared with an unrelated device. The handler currently schedules the reset work unconditionally:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
        ...
        schedule_work(&ddata->bmc_reset_work);
        ...
        return IRQ_HANDLED;
}

Because bmc_reset_work triggers a 10-second stop_machine() halt, normal activity on any unrelated device sharing this interrupt line could repeatedly freeze the entire system.

[Severity: High]
This is another pre-existing issue, but will this interrupt handler fail to process interrupts for the first 6 minutes of system uptime?

The handler relies on a static variable for rate-limiting:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
        static unsigned long last_jiffies;
        ...
        if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {
        ...
}

Linux initializes jiffies to a negative value to detect wrap-around conditions. Because last_jiffies defaults to 0, this check evaluates to false until jiffies becomes sufficiently positive, silently dropping interrupts for the first ~360 seconds. Being static, this variable is also unprotected and improperly shared across all device instances.

[ ... ]

While reviewing the above changes, I also noticed a few other pre-existing issues in the driver that might need attention:

[Severity: High]
This is a pre-existing issue, but is the GSI unregistered prematurely during initialization?

Later in ls2k_bmc_init(), the GSI is unregistered immediately on the successful probe path:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
        ...
        acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
        ...
}

This tears down and invalidates the IRQ descriptor while the handler remains registered, which instantly destroys the mapping and renders the GPIO interrupt dysfunctional.

[Severity: High]
This is a pre-existing issue, but does this code dynamically mutate a global, non-const array with a stack pointer?

In ls2k_bmc_probe(), the address of a local stack variable is assigned to the global ls2k_bmc_cells array:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
        struct ls2k_bmc_platform_data pd;
        ...
        ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
        ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
        ...
}

If multiple instances of this device probe concurrently, they could race to write to the shared global array. Furthermore, the global array retains a dangling pointer to the popped stack frame after probe returns.

According to the MFD subsystem guidelines, mfd_cell arrays should be static const to prevent these kinds of regressions.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#9 lore raw

RFC PATCH v2 5/7 ipmi: ls2k: Relax the dependency to its mfd driver

In reply to Miao Wang

There is no strong dependency between the IPMI driver and its mfd driver. Although the IPMI driver will not work without the mfd driver, it is not a hard dependency. The IPMI driver can actually be compiled without the mfd driver, and it will just fail to probe. When the mfd driver is loaded, the IPMI driver will probe successfully. Therefore, the dependency of the IPMI driver on its mfd driver should be relaxed to "imply" from "select". This will allow the mfd driver to be compiled as a module and the IPMI driver to be compiled as a part of the ipmi_si module.

Fixes d46651d4e3c0 ("ipmi: Add Loongson-2K BMC support")
---
 drivers/char/ipmi/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/ipmi/Kconfig +1 -1
index 669f7600019747bcd2b37563477cf336f19a0407..f456e3e89416932f0d21bb742153a503aeb24267 100644
@@ -87,7 +87,7 @@ config IPMI_IPMB
 config IPMI_LS2K
 	bool 'Loongson-2K IPMI interface'
 	depends on LOONGARCH
-	select MFD_LS2K_BMC_CORE
+	imply MFD_LS2K_BMC_CORE
 	help
 	  Provides a driver for Loongson-2K IPMI interfaces.
 
Signature · 1 line
2.49.0
#10 lore raw

Re: [PATCH RFC v2 5/7] ipmi: ls2k: Relax the dependency to its mfd driver

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit message inaccurately claims that changing `select` to `imply` allows the MFD driver…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit message inaccurately claims that changing select to imply allows the MFD driver to be compiled as a module, but MFD_LS2K_BMC_CORE is defined as a bool. --

--- Patch [5]: [PATCH RFC v2 5/7] ipmi: ls2k: Relax the dependency to its mfd driver --- commit d49c3c402e5644d151c1cade80b84a38875689e0
Author: Miao Wang <[email protected]>

ipmi: ls2k: Relax the dependency to its mfd driver

This commit changes the IPMI_LS2K dependency on MFD_LS2K_BMC_CORE from select to imply. This prevents forcing the MFD driver to be built-in when it might be undesirable.

Quoted 11 lines

ipmi: ls2k: Relax the dependency to its mfd driver

There is no strong dependency between the IPMI driver and its mfd driver. Although the IPMI driver will not work without the mfd driver, it is not a hard dependency. The IPMI driver can actually be compiled without the mfd driver, and it will just fail to probe. When the mfd driver is loaded, the IPMI driver will probe successfully. Therefore, the dependency of the IPMI driver on its mfd driver should be relaxed to "imply" from "select". This will allow the mfd driver to be compiled as a module and the IPMI driver to be compiled as a part of the ipmi_si module.

[Severity: Medium]
Is this description accurate regarding the module compilation?

Looking at drivers/mfd/Kconfig, MFD_LS2K_BMC_CORE is defined as a bool rather than a tristate:

config MFD_LS2K_BMC_CORE

	bool "Loongson-2K Board Management Controller Support"
	depends on PCI && ACPI_GENERIC_GSI

Since a boolean Kconfig symbol can only be built-in or disabled, it cannot be compiled as a module. Does the MFD driver Kconfig need to be converted to a tristate, or should the commit message be updated to clarify that this change simply prevents forcing the MFD driver to be built-in?

Quoted 12 lines
drivers/char/ipmi/Kconfig +1 -1
index 669f760001974..f456e3e894169 100644
@@ -87,7 +87,7 @@ config IPMI_IPMB
 config IPMI_LS2K
 	bool 'Loongson-2K IPMI interface'
 	depends on LOONGARCH
-	select MFD_LS2K_BMC_CORE
+	imply MFD_LS2K_BMC_CORE
 	help
 	  Provides a driver for Loongson-2K IPMI interfaces.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#11 lore raw

RFC PATCH v2 6/7 mfd: ls2kbmc: Able to be compiled as a module

In reply to Miao Wang

Make ls2kmc able to be compiled as a module

---
 drivers/mfd/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mfd/Kconfig +1 -1
index 763ce6a34782bdd5d2b1a4d840c75b040092d83e..a7a9f97af248c88489dc1203a1ba05f2ce4827df 100644
@@ -2494,7 +2494,7 @@ config MFD_LOONGSON_SE
 	  which will forward them to the corresponding engine.
 
 config MFD_LS2K_BMC_CORE
-	bool "Loongson-2K Board Management Controller Support"
+	tristate "Loongson-2K Board Management Controller Support"
 	depends on PCI && ACPI_GENERIC_GSI
 	select MFD_CORE
 	help
Signature · 1 line
2.49.0
#12 lore raw

Re: [PATCH RFC v2 6/7] mfd: ls2kbmc: Able to be compiled as a module

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Executing an unbounded and extremely long busy-wait (10+ seconds) inside a stop_machine callback, which will cause a hard lockup.
- [High] Dynamically modifying a global static mfd_cell array with a pointer to a stack-allocated variable, causing a data race and leaving a dangling pointer.
- [High] The GPIO GSI-to-IRQ mapping is unconditionally unregistered immediately after being successfully requested, breaking the interrupt handler.
- [High] The IRQ handler uses a static rate-limiting variable that drops interrupts during the first ~5 minutes of system uptime and improperly shares state across device instances. --

--- Patch [6]: [PATCH RFC v2 6/7] mfd: ls2kbmc: Able to be compiled as a module --- commit f6dafc2cbabb3cc03d44c55be366eadc27bca60c
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: Able to be compiled as a module

Make ls2kmc able to be compiled as a module

drivers/mfd/Kconfig config MFD_LOONGSON_SE · line 2500

Quoted 13 lines
drivers/mfd/Kconfig +1 -1
index 763ce6a34782b..a7a9f97af248c 100644
@@ -2494,7 +2494,7 @@ config MFD_LOONGSON_SE
 	  which will forward them to the corresponding engine.
 
 config MFD_LS2K_BMC_CORE
-	bool "Loongson-2K Board Management Controller Support"
+	tristate "Loongson-2K Board Management Controller Support"
 	depends on PCI && ACPI_GENERIC_GSI
 	select MFD_CORE
 	help

This isn't a bug introduced by this patch, but while reviewing this I noticed a few pre-existing issues in drivers/mfd/ls2k-bmc-core.c:

Quoted 2 lines
drivers/mfd/ls2k-bmc-core.c +0 -0

[ ... ]

Quoted 1 line

static struct mfd_cell ls2k_bmc_cells[] = {

[Severity: High]
This is a pre-existing issue, but shouldn't this array be defined as static const to comply with MFD subsystem guidelines?

[ ... ]

Quoted 3 lines

static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)

{
	struct simplefb_platform_data pd;

[ ... ]

Quoted 5 lines
	ret = ls2k_bmc_parse_mode(dev, &pd);
	if (ret)
		return ret;

	ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;

[Severity: High]
This is a pre-existing issue, but does this leave a dangling pointer to the local stack variable pd in the global array? Could this also cause a data race if multiple instances of this driver probe concurrently?

[ ... ]

Quoted 2 lines

static void ls2k_bmc_events_fn(struct work_struct *work) {

[ ... ]

Quoted 5 lines
	/*
	 * The PCI-E is lost when the BMC resets, at which point access to the PCI-E
	 * from other CPUs is suspended to prevent a crash.
	 */
	stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);

[ ... ]

Quoted 2 lines

static int ls2k_bmc_recover_pci_data(void *data)
{

[ ... ]

Quoted 2 lines
	/* Waiting for U-Boot and DDR ready */
	mdelay(LS2K_BMC_RESET_WAIT_TIME);

[Severity: Critical]
This is a pre-existing issue, but does this execute a 10 second busy-wait while running in the stop_machine() callback? Since stop_machine() halts all other CPUs with local interrupts disabled, will this cause a hard lockup?

[ ... ]

Quoted 2 lines

static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) {

[ ... ]

Quoted 6 lines
	ret = devm_request_irq(ddata->dev, gpio_irq, ls2k_bmc_interrupt,
			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc gpio", ddata);
	if (ret)
		dev_err(ddata->dev, "Failed to request LS2KBMC GPIO IRQ %d.\n", gpio_irq);

	acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);

[Severity: High]
This is a pre-existing issue, but does this unconditionally unregister and dispose of the IRQ mapping immediately after it was successfully requested? Could this leave an orphaned irqaction and break the GPIO interrupts?

[ ... ]

Quoted 4 lines

static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg)

{
	struct ls2k_bmc_ddata *ddata = arg;
	static unsigned long last_jiffies;

[Severity: High]
This is a pre-existing issue, but does this improperly share the rate limiting state across all device instances?

Quoted 6 lines
	if (system_state != SYSTEM_RUNNING)
		return IRQ_HANDLED;

	/* Skip interrupt in LS2K_BMC_INT_INTERVAL */
	if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {

[Severity: High]
This is a pre-existing issue, but because last_jiffies initializes to 0 and jiffies starts at INITIAL_JIFFIES (usually a large unsigned value near wrap around), will this block all valid hardware interrupts for the first ~5 minutes of system uptime?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author
#13 lore raw

RFC PATCH v2 7/7 mfd: ls2kbmc: Capture the reset event of BMC through GPIO

In reply to Miao Wang

The reset event of BMC is captured through GPIO. However, this driver bypasses the GPIO framework and directly accesses the GPIO controller through the fixed address. When the same GPIO controller is also exposed through ACPI and probed by the corresponding GPIO driver, there would be a conflict between the two drivers.

This patch will try to find the GPIO through declared GPIO pin in the _CRS resources of the ACPI node. If no such delaration is found, the driver will fall back to search for the correct GPIO controller and pin according to the fixed address and pin number. A possible DSDT declaration for the GPIO pin might be as follows:

    Device (BMC0) {
        Name (_ADR, ...) // Match the PCI address of the BMC device
        // \_SB.GPO1 is the ACPI path of the GPIO controller
        Name (_CRS, ResourceTemplate () {
            GpioInt (Edge, ActiveLow, Exclusive, PullNone, 0,
                     "\\_SB.GPO1", 0) {
                14 // 14 is the GPIO pin number
            }
    }
---
 drivers/mfd/ls2k-bmc-core.c | 162 +++++++++++++++++++++++++++++++-------------
 1 file changed, 115 insertions(+), 47 deletions(-)
drivers/mfd/ls2k-bmc-core.c +115 -47
index f87224105b3720cca97dcef089dad63fe57bc8c2..7187b2dfddc3acfb9fdf3cb3b4675299928dd03a 100644
@@ -26,6 +26,9 @@
 #include <linux/stop_machine.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio.h>
 
 /* LS2K BMC resources */
 #define LS2K_DISPLAY_RES_START		(SZ_16M + SZ_2M)
@@ -81,18 +84,6 @@
 
 #define PCI_REG_STRIDE			0x4
 
-#define LS2K_BMC_RESET_GPIO		14
-#define LOONGSON_GPIO_REG_BASE		0x1FE00500
-#define LOONGSON_GPIO_REG_SIZE		0x18
-#define LOONGSON_GPIO_OEN		0x0
-#define LOONGSON_GPIO_FUNC		0x4
-#define LOONGSON_GPIO_INTPOL		0x10
-#define LOONGSON_GPIO_INTEN		0x14
-
-#define LOONGSON_IO_INT_BASE		16
-#define LS2K_BMC_RESET_GPIO_INT_VEC	(LS2K_BMC_RESET_GPIO % 8)
-#define LS2K_BMC_RESET_GPIO_GSI		(LOONGSON_IO_INT_BASE + LS2K_BMC_RESET_GPIO_INT_VEC)
-
 enum {
 	LS2K_BMC_DISPLAY,
 	LS2K_BMC_IPMI0,
@@ -186,6 +177,7 @@ struct ls2k_bmc_ddata {
 	struct work_struct bmc_reset_work;
 	struct ls2k_bmc_pci_data bmc_pci_data;
 	struct ls2k_bmc_bridge_pci_data bridge_pci_data;
+	struct gpio_desc *reset_gpio;
 };
 
 static bool ls2k_bmc_bar0_addr_is_set(struct pci_dev *pdev)
@@ -334,6 +326,22 @@ static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg)
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t ls2k_bmc_gpio_interrupt(int irq, void *arg)
+{
+	struct ls2k_bmc_ddata *ddata = arg;
+
+	int val = gpiod_get_raw_value(ddata->reset_gpio);
+
+	/*
+	 * The GPIO is active low, so when the value is 0,
+	 * it indicates a reset event.
+	 */
+	if (val == 0)
+		return ls2k_bmc_interrupt(irq, arg);
+
+	return IRQ_NONE;
+}
+
 /*
  * Saves the BMC parent device (LS7A) and its own PCI configuration space registers
  * that need to be restored after BMC reset.
@@ -375,6 +383,79 @@ static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata *
 	pci_read_config_dword(pdev, PCI_INTERRUPT_LINE, &ddata->bmc_pci_data.interrupt_line);
 }
 
+static int ls2k_bmc_gpiochip_find(struct gpio_chip *gc, const void *data)
+{
+	struct acpi_device *adev;
+	struct list_head resource_list;
+	struct resource_entry *rentry;
+	struct fwnode_handle *fwnode = gc->fwnode ?: gc->parent ? gc->parent->fwnode : NULL;
+	phys_addr_t start_addr = (phys_addr_t) data;
+	int ret, found = 0;
+
+	if (!is_acpi_node(fwnode))
+		goto out;
+
+	adev = to_acpi_device_node(fwnode);
+	if (!adev)
+		goto out;
+
+	INIT_LIST_HEAD(&resource_list);
+
+	ret = acpi_dev_get_memory_resources(adev, &resource_list);
+	if (ret < 0)
+		goto out;
+	rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node);
+	if (!rentry)
+		goto free_resource_list;
+	if (rentry->res->start == start_addr)
+		found = 1;
+
+free_resource_list:
+	acpi_dev_free_resource_list(&resource_list);
+out:
+	return found;
+}
+
+static struct gpio_desc *ls2k_bmc_find_gpio(struct ls2k_bmc_ddata *ddata)
+{
+	/*
+	 * In conventional way, the GPIO should be obtained through ACPI or
+	 * device tree. However, when the information is not available,
+	 * we should find the GPIO according to the convention of the server
+	 * boards with LS2K BMC, the gpio signal relelecting the reset event
+	 * of the BMC should be connected to pin 14 of the GPIO input of
+	 * the first CPU node. The address of that GPIO controller is fixed.
+	 */
+	static const phys_addr_t LOONGSON_GPIO_REG_BASE = 0x1FE00500;
+	static const unsigned int LS2K_BMC_RESET_GPIO = 14;
+	struct gpio_desc *desc;
+	unsigned int legacy_gpio;
+	int ret;
+	struct gpio_device *gdev __free(gpio_device_put) = NULL;
+
+	dev_dbg(ddata->dev, "Searching for GPIO chip at address %pa\n", &LOONGSON_GPIO_REG_BASE);
+
+	gdev = gpio_device_find((void *)LOONGSON_GPIO_REG_BASE, ls2k_bmc_gpiochip_find);
+
+	if (!gdev) {
+		dev_dbg(ddata->dev, "cannot find GPIO chip at address %pa, deferring\n",
+			&LOONGSON_GPIO_REG_BASE);
+		return ERR_PTR(-EPROBE_DEFER);
+	}
+
+	desc = gpio_device_get_desc(gdev, LS2K_BMC_RESET_GPIO);
+	if (IS_ERR(desc))
+		return desc;
+
+	// XXX: might be better to use gpiod_request()
+	legacy_gpio = desc_to_gpio(desc);
+	ret = devm_gpio_request_one(ddata->dev, legacy_gpio, GPIOF_IN, dev_name(ddata->dev));
+	if (ret < 0)
+		return ERR_PTR(ret);
+
+	return gpio_to_desc(legacy_gpio);
+}
+
 static void ls2k_bmc_cancel_wq(void *data)
 {
 	struct ls2k_bmc_ddata *ddata = data;
@@ -384,8 +465,7 @@ static void ls2k_bmc_cancel_wq(void *data)
 static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 {
 	struct pci_dev *pdev = to_pci_dev(ddata->dev);
-	void __iomem *gpio_base;
-	int gpio_irq, ret, val;
+	int gpio_irq, ret;
 
 	ls2k_bmc_save_pci_data(pdev, ddata);
 
@@ -400,44 +480,32 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 		return ret;
 	}
 
-	gpio_base = ioremap(LOONGSON_GPIO_REG_BASE, LOONGSON_GPIO_REG_SIZE);
-	if (!gpio_base)
-		return -ENOMEM;
-
-	/* Disable GPIO output */
-	val = readl(gpio_base + LOONGSON_GPIO_OEN);
-	writel(val | BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_OEN);
-
-	/* Enable GPIO functionality */
-	val = readl(gpio_base + LOONGSON_GPIO_FUNC);
-	writel(val & ~BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_FUNC);
-
-	/* Set GPIO interrupts to low-level active */
-	val = readl(gpio_base + LOONGSON_GPIO_INTPOL);
-	writel(val & ~BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_INTPOL);
-
-	/* Enable GPIO interrupts */
-	val = readl(gpio_base + LOONGSON_GPIO_INTEN);
-	writel(val | BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_INTEN);
+	ddata->reset_gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 0, GPIOD_IN);
+	if (IS_ERR(ddata->reset_gpio)) {
+		ret = PTR_ERR(ddata->reset_gpio);
+		ddata->reset_gpio = NULL;
+		return dev_err_probe(ddata->dev, ret, "Failed to get reset GPIO\n");
+	}
+	if (ddata->reset_gpio == NULL) {
+		ddata->reset_gpio = ls2k_bmc_find_gpio(ddata);
+		if (IS_ERR(ddata->reset_gpio)) {
+			ret = PTR_ERR(ddata->reset_gpio);
+			ddata->reset_gpio = NULL;
+			return dev_err_probe(ddata->dev, ret, "Failed to find reset GPIO\n");
+		}
+	}
 
-	iounmap(gpio_base);
+	gpio_irq = gpiod_to_irq(ddata->reset_gpio);
 
-	/*
-	 * Since gpio_chip->to_irq is not implemented in the Loongson-3 GPIO driver,
-	 * acpi_register_gsi() is used to obtain the GPIO IRQ. The GPIO interrupt is a
-	 * watchdog interrupt that is triggered when the BMC resets.
-	 */
-	gpio_irq = acpi_register_gsi(NULL, LS2K_BMC_RESET_GPIO_GSI, ACPI_EDGE_SENSITIVE,
-				     ACPI_ACTIVE_LOW);
 	if (gpio_irq < 0)
-		return gpio_irq;
+		return dev_err_probe(ddata->dev, gpio_irq, "Failed to get GPIO IRQ\n");
 
-	ret = devm_request_irq(ddata->dev, gpio_irq, ls2k_bmc_interrupt,
-			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc gpio", ddata);
-	if (ret)
-		dev_err(ddata->dev, "Failed to request LS2KBMC GPIO IRQ %d.\n", gpio_irq);
+	ret = devm_request_irq(&pdev->dev, gpio_irq, ls2k_bmc_gpio_interrupt,
+			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc reset", ddata);
+
+	if (ret != 0)
+		return dev_err_probe(ddata->dev, ret, "Failed to request GPIO IRQ %d.\n", gpio_irq);
 
-	acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
 	return ret;
 }
 
Signature · 1 line
2.49.0
#14 lore raw

Re: [PATCH RFC v2 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] TOCTOU race and API misuse in edge-triggered GPIO interrupt handler.

Pre-existing issues:
- [High] Unconditional return of IRQ_HANDLED in shared PCI interrupt handler breaks interrupt sharing.
- [High] Executing a 10-second busy-wait inside a stop_machine() callback triggers hard lockups.
- [High] jiffies wraparound bug in rate-limiting ignores interrupts for the first 6 minutes of uptime. --

--- Patch [7]: [PATCH RFC v2 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO --- commit 4fddebf5ba83cb02d076dd7162e7b2b6d2116f26
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: Capture the reset event of BMC through GPIO

This patch updates the driver to capture BMC reset events using the standard GPIO framework rather than directly accessing fixed memory-mapped registers. It attempts to find the GPIO from ACPI _CRS resources and falls back to a fixed address if unavailable.

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index f87224105b372..7187b2dfddc3a 100644

[ ... ]

drivers/mfd/ls2k-bmc-core.c static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg) · line 327

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
@@ -334,6 +326,22 @@ static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg)
 	return IRQ_HANDLED;
 }

[Severity: High]
This is a pre-existing issue, but does ls2k_bmc_interrupt unconditionally return IRQ_HANDLED without checking hardware registers?

The PCI interrupt is requested with IRQF_SHARED. If another device sharing the IRQ line fires an interrupt, will this unconditionally claim it and falsely trigger the BMC recovery process?

[Severity: High]
This is a pre-existing issue, but does the rate-limiting in ls2k_bmc_interrupt drop interrupts during the first 6 minutes of uptime?

Because last_jiffies is statically initialized to 0, and jiffies starts at INITIAL_JIFFIES (typically -300 * HZ), time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL) evaluates to false until jiffies wraps around zero.

[Severity: High]
This is a pre-existing issue, but will ls2k_bmc_events_fn trigger a hard lockup by calling stop_machine()?

ls2k_bmc_events_fn registers ls2k_bmc_recover_pci_data to run under stop_machine, where it executes a 10-second busy wait (mdelay(LS2K_BMC_RESET_WAIT_TIME)) with local interrupts disabled on all CPUs.

drivers/mfd/ls2k-bmc-core.c static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg) · line 340

Quoted 12 lines
drivers/mfd/ls2k-bmc-core.c +12 -0
+static irqreturn_t ls2k_bmc_gpio_interrupt(int irq, void *arg)
+{
+	struct ls2k_bmc_ddata *ddata = arg;
+
+	int val = gpiod_get_raw_value(ddata->reset_gpio);
+
+	/*
+	 * The GPIO is active low, so when the value is 0,
+	 * it indicates a reset event.
+	 */
+	if (val == 0)
+		return ls2k_bmc_interrupt(irq, arg);

[Severity: High]
Is there a race condition here that might miss reset events?

Since the interrupt is requested as edge-triggered (IRQF_TRIGGER_FALLING), could the physical line have already returned to a high state by the time this handler executes? If gpiod_get_raw_value sees a high level, the handler returns IRQ_NONE, which could cause the spurious interrupt detector to permanently disable the shared IRQ line.

Also, does using gpiod_get_raw_value instead of gpiod_get_value bypass gpiolib logical polarity abstractions (like ActiveLow from ACPI), potentially breaking active-high reset configurations?

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +3 -0
+
+	return IRQ_NONE;
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#15 lore raw

Re: [PATCH RFC v2 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO

In reply to Miao Wang
Quoted 34 lines · On Tue, 7 Jul 2026 23:16:31 +0200, Miao Wang via B4 Relay…

From: Miao Wang <[email protected]>

The reset event of BMC is captured through GPIO. However, this driver bypasses the GPIO framework and directly accesses the GPIO controller through the fixed address. When the same GPIO controller is also exposed through ACPI and probed by the corresponding GPIO driver, there would be a conflict between the two drivers.

This patch will try to find the GPIO through declared GPIO pin in the _CRS resources of the ACPI node. If no such delaration is found, the driver will fall back to search for the correct GPIO controller and pin according to the fixed address and pin number. A possible DSDT declaration for the GPIO pin might be as follows:

    Device (BMC0) {
        Name (_ADR, ...) // Match the PCI address of the BMC device
        // \_SB.GPO1 is the ACPI path of the GPIO controller
        Name (_CRS, ResourceTemplate () {
            GpioInt (Edge, ActiveLow, Exclusive, PullNone, 0,
                     "\\_SB.GPO1", 0) {
                14 // 14 is the GPIO pin number
            }
    }
---
 drivers/mfd/ls2k-bmc-core.c | 162 +++++++++++++++++++++++++++++++-------------
 1 file changed, 115 insertions(+), 47 deletions(-)
drivers/mfd/ls2k-bmc-core.c +0 -0
index f87224105b3720cca97dcef089dad63fe57bc8c2..7187b2dfddc3acfb9fdf3cb3b4675299928dd03a 100644

drivers/mfd/ls2k-bmc-core.c line 31

Quoted 6 lines
drivers/mfd/ls2k-bmc-core.c +3 -0
@@ -26,6 +26,9 @@
 #include <linux/stop_machine.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio.h>

I've stopped here because this is a legacy header that must not be included and none of the interfaces in it must be used, as per the - very loud - comment at the top of that file.

Bart

author
#16 lore raw

Re: [PATCH RFC v2 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO

Hi

Quoted 33 lines

2026年7月8日 19:36,Bartosz Golaszewski <[email protected]> 写道:

Quoted 29 lines · On Tue, 7 Jul 2026 23:16:31 +0200, Miao Wang via B4 Relay…

From: Miao Wang <[email protected]>

The reset event of BMC is captured through GPIO. However, this driver bypasses the GPIO framework and directly accesses the GPIO controller through the fixed address. When the same GPIO controller is also exposed through ACPI and probed by the corresponding GPIO driver, there would be a conflict between the two drivers.

This patch will try to find the GPIO through declared GPIO pin in the _CRS resources of the ACPI node. If no such delaration is found, the driver will fall back to search for the correct GPIO controller and pin according to the fixed address and pin number. A possible DSDT declaration for the GPIO pin might be as follows:

   Device (BMC0) {
       Name (_ADR, ...) // Match the PCI address of the BMC device
       // \_SB.GPO1 is the ACPI path of the GPIO controller
       Name (_CRS, ResourceTemplate () {
           GpioInt (Edge, ActiveLow, Exclusive, PullNone, 0,
                    "\\_SB.GPO1", 0) {
               14 // 14 is the GPIO pin number
           }
   }

---
drivers/mfd/ls2k-bmc-core.c | 162 +++++++++++++++++++++++++++++++-------------

1 file changed, 115 insertions(+), 47 deletions(-)
Quoted 15 lines

drivers/mfd/ls2k-bmc-core.c line 31

Quoted 11 lines
drivers/mfd/ls2k-bmc-core.c +3 -0
index f87224105b3720cca97dcef089dad63fe57bc8c2..7187b2dfddc3acfb9fdf3cb3b4675299928dd03a 100644
@@ -26,6 +26,9 @@
 #include <linux/stop_machine.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio.h>

I've stopped here because this is a legacy header that must not be included and none of the interfaces in it must be used, as per the - very loud - comment at the top of that file.

I fully understand using the legacy interface is deprecated. However, there seems no other way to obtain a gpio line description after a gpio device is found using gpio_device_find(). As the comment shows in the code below, the only place I use the legacy interface is:

gdev = gpio_device_find(...);
desc = gpio_device_get_desc(gdev, LS2K_BMC_RESET_GPIO);

// XXX: might be better to use gpiod_request()
legacy_gpio = desc_to_gpio(desc);
devm_gpio_request_one(..., legacy_gpio, ...);
return gpio_to_desc(legacy_gpio);

I just borrowed the legacy gpio interface to request the irq description. I think that gpiod_request() should be better to be used here, but it is not an interface that is exposed. As a result, I post this patch as a PoC here to discuss and find a better way to achieve this.

Cheers,

Miao Wang

#17 lore raw

Re: [PATCH RFC v2 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO

Quoted 51 lines · Miao Wang

Hi

Quoted 33 lines

2026年7月8日 19:36,Bartosz Golaszewski <[email protected]> 写道:

Quoted 29 lines · On Tue, 7 Jul 2026 23:16:31 +0200, Miao Wang via B4 Relay…

From: Miao Wang <[email protected]>

The reset event of BMC is captured through GPIO. However, this driver bypasses the GPIO framework and directly accesses the GPIO controller through the fixed address. When the same GPIO controller is also exposed through ACPI and probed by the corresponding GPIO driver, there would be a conflict between the two drivers.

This patch will try to find the GPIO through declared GPIO pin in the _CRS resources of the ACPI node. If no such delaration is found, the driver will fall back to search for the correct GPIO controller and pin according to the fixed address and pin number. A possible DSDT declaration for the GPIO pin might be as follows:

   Device (BMC0) {
       Name (_ADR, ...) // Match the PCI address of the BMC device
       // \_SB.GPO1 is the ACPI path of the GPIO controller
       Name (_CRS, ResourceTemplate () {
           GpioInt (Edge, ActiveLow, Exclusive, PullNone, 0,
                    "\\_SB.GPO1", 0) {
               14 // 14 is the GPIO pin number
           }
   }

---
drivers/mfd/ls2k-bmc-core.c | 162 +++++++++++++++++++++++++++++++-------------

1 file changed, 115 insertions(+), 47 deletions(-)
Quoted 15 lines

drivers/mfd/ls2k-bmc-core.c line 31

Quoted 11 lines
drivers/mfd/ls2k-bmc-core.c +3 -0
index f87224105b3720cca97dcef089dad63fe57bc8c2..7187b2dfddc3acfb9fdf3cb3b4675299928dd03a 100644
@@ -26,6 +26,9 @@
 #include <linux/stop_machine.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio.h>

I've stopped here because this is a legacy header that must not be included and none of the interfaces in it must be used, as per the - very loud - comment at the top of that file.

Quoted 8 lines

I fully understand using the legacy interface is deprecated. However, there seems no other way to obtain a gpio line description after a gpio device is found using gpio_device_find(). As the comment shows in the code below, the only place I use the legacy interface is:

gdev = gpio_device_find(...);
desc = gpio_device_get_desc(gdev, LS2K_BMC_RESET_GPIO);

No, new code using legacy API is *not* acceptable. That's an official NAK.

Quoted 10 lines

// XXX: might be better to use gpiod_request()
legacy_gpio = desc_to_gpio(desc);
devm_gpio_request_one(..., legacy_gpio, ...);
return gpio_to_desc(legacy_gpio);

I just borrowed the legacy gpio interface to request the irq description. I think that gpiod_request() should be better to be used here, but it is not an interface that is exposed. As a result, I post this patch as a PoC here to discuss and find a better way to achieve this.

If the firmware doesn't describe the GPIO, you have at least two alternatives. Please look at Documentation/driver-api/gpio/legacy-boards.rst. You should be able to use either software nodes or GPIO lookup tables. This document describes the conversion from the latter to the former but - while software nodes are preferred - lookup tables are not deprecated so feel free to use it to set up the descriptors.

Bart

#18 lore raw

Re: [PATCH RFC v2 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO

In reply to Miao Wang
Quoted 14 lines · On Wed, 2026-07-08 at 05:16 +0800, Miao Wang via B4 Relay wrote:

From: Miao Wang <[email protected]>

The reset event of BMC is captured through GPIO. However, this driver bypasses the GPIO framework and directly accesses the GPIO controller through the fixed address. When the same GPIO controller is also exposed through ACPI and probed by the corresponding GPIO driver, there would be a conflict between the two drivers.

This patch will try to find the GPIO through declared GPIO pin in the _CRS resources of the ACPI node. If no such delaration is found, the driver will fall back to search for the correct GPIO controller and pin according to the fixed address and pin number. A possible DSDT declaration for the GPIO pin might be as follows:

Quoted 9 lines

    Device (BMC0) {
        Name (_ADR, ...) // Match the PCI address of the BMC device // \_SB.GPO1 is the ACPI path of the GPIO controller Name (_CRS, ResourceTemplate () {
            GpioInt (Edge, ActiveLow, Exclusive, PullNone, 0, "\\_SB.GPO1", 0) {
                14 // 14 is the GPIO pin number
            }
    }

I think this is a proposal without real implementation yet? If so...

/* snip */

Quoted 7 lines
drivers/mfd/ls2k-bmc-core.c +7 -0
+	adev = to_acpi_device_node(fwnode);
+	if (!adev)
+		goto out;
+
+	INIT_LIST_HEAD(&resource_list);
+
+	ret = acpi_dev_get_memory_resources(adev, &resource_list);

drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata * · line 414

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +10 -0
+	if (ret < 0)
+		goto out;
+	rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node);
+	if (!rentry)
+		goto free_resource_list;
+	if (rentry->res->start == start_addr)
+		found = 1;
+
+free_resource_list:
+	acpi_dev_free_resource_list(&resource_list);

... consider adding a _DSD method to label (like "bmc-reset") the GPIO for the proposed BMC0 device so you can use devm_gpiod_get to get the GPIO, instead of using the hand-brew code to parse the ACPI node.
See https://www.kernel.org/doc/html/latest/firmware-guide/acpi/gpio-properties.html.

Combining this with Bartosz's comment, the logic would be like:

if (missing the ACPI node)

    assign a swnode to label "gpio14" as "bmc-reset";
  gpio_desc = devm_gpiod_get(&dev, "bmc-reset", GPIOD_IN);

And such a proposal will need to be discussed with Loongson. Yes I know people may hate the "control" from the vendor, but having some effectively dead code (i.e. supporting some non-exist firmware) in the kernel is worse.

Signature · 1 line
Xi Ruoyao <[email protected]>
author
#19 lore raw

Re: [PATCH RFC v2 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO

Hi,

Quoted 45 lines

2026年7月8日 21:14,Xi Ruoyao <[email protected]> 写道:

Quoted 14 lines · On Wed, 2026-07-08 at 05:16 +0800, Miao Wang via B4 Relay wrote:

From: Miao Wang <[email protected]>

The reset event of BMC is captured through GPIO. However, this driver bypasses the GPIO framework and directly accesses the GPIO controller through the fixed address. When the same GPIO controller is also exposed through ACPI and probed by the corresponding GPIO driver, there would be a conflict between the two drivers.

This patch will try to find the GPIO through declared GPIO pin in the _CRS resources of the ACPI node. If no such delaration is found, the driver will fall back to search for the correct GPIO controller and pin according to the fixed address and pin number. A possible DSDT declaration for the GPIO pin might be as follows:

Quoted 9 lines
    Device (BMC0) {
        Name (_ADR, ...) // Match the PCI address of the BMC device
        // \_SB.GPO1 is the ACPI path of the GPIO controller
        Name (_CRS, ResourceTemplate () {
            GpioInt (Edge, ActiveLow, Exclusive, PullNone, 0,
                     "\\_SB.GPO1", 0) {
                14 // 14 is the GPIO pin number
            }
    }

I think this is a proposal without real implementation yet? If so...

/* snip */

Quoted 14 lines

+ adev = to_acpi_device_node(fwnode);
+ if (!adev)
+ goto out;

drivers/mfd/ls2k-bmc-core.c +11 -0
+
+ INIT_LIST_HEAD(&resource_list);
+
+ ret = acpi_dev_get_memory_resources(adev, &resource_list);
+ if (ret < 0)
+ goto out;
+ rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node);
+ if (!rentry)
+ goto free_resource_list;
+ if (rentry->res->start == start_addr)
+ found = 1;
Quoted 8 lines

drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata * · line 414

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +3 -0
+
+free_resource_list:
+ acpi_dev_free_resource_list(&resource_list);

... consider adding a _DSD method to label (like "bmc-reset") the GPIO for the proposed BMC0 device so you can use devm_gpiod_get to get the GPIO, instead of using the hand-brew code to parse the ACPI node.
See https://www.kernel.org/doc/html/latest/firmware-guide/acpi/gpio-properties.html.

Yes, it is a proposal. GpioInt() in the _CRS() should be at least required to associate the BMC device to the GPIO pin. Having a _DSD might be better to label the gpio pin with human readable label. I however don't think _DSD is important because there is only one pin needed, and devm_gpiod_get_index can be used to obtain the gpio line description using the index in _CRS()

Quoted 5 lines

Combining this with Bartosz's comment, the logic would be like:

if (missing the ACPI node)
assign a swnode to label "gpio14" as "bmc-reset";

I'll further look into whether swnode can be used.

Quoted 6 lines

gpio_desc = devm_gpiod_get(&dev, "bmc-reset", GPIOD_IN);

And such a proposal will need to be discussed with Loongson. Yes I know people may hate the "control" from the vendor, but having some effectively dead code (i.e. supporting some non-exist firmware) in the kernel is worse.

I'm open with such discussions.

Cheers,

Miao Wang