// SPDX-License-Identifier: GPL-2.0
/*
* irq1fix — flip IRQ 1 (internal PS/2 keyboard) from edge/active-low to
* edge/active-high on TongFang GM7RGxM (PCSpecialist Valeon II 17), at runtime.
*
* Why: since kernel 6.0 the ACPI ISA IRQ override is skipped on AMD Zen
* (drivers/acpi/resource.c), and this board is missing from the
* irq1_edge_low_force_override[] DMI table, so the DSDT's Edge/ActiveLow
* survives and the EC's (active-high) keyboard IRQs are sensed on the wrong
* edge. The kernel bakes trigger/polarity into the IOAPIC domain's private
* mp_chip_data at boot and rewrites the RTE from it on IRQ activation.
*
* What this module does: validates and then flips the active_low fields in
* IRQ 1's mp_chip_data. It writes NOTHING to hardware itself. The caller must
* unbind the i8042 platform driver first (so IRQ 1 has no action and nothing
* races) and rebind it afterwards — the rebind's request_irq() reactivates the
* IRQ and the kernel itself programs the corrected RTE (mp_irqdomain_activate
* -> ioapic_configure_entry), including under AMD-Vi interrupt remapping,
* where active_low/is_level stay RTE-side and never enter the IRTE.
*
* Load with revert=1 to restore active-low (inverts the expected signature).
*
* Every field is validated before anything is written; any mismatch means
* "already fixed, different kernel layout, or not the machine this was built
* for" and the module refuses with -ENODEV without having dereferenced
* unvalidated pointers. NOTE: irq_set_irq_type() must never be used instead —
* neither IOAPIC irq_chip implements .irq_set_type, so it returns 0 (success)
* while doing nothing.
*
* The layout below is copied verbatim from arch/x86/kernel/apic/io_apic.c
* (private to that file) at v7.1; vermagic ties the build to the running
* kernel, and the signature checks refuse on drift.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <asm/io_apic.h>
struct irq_pin_list {
struct list_head list;
int apic, pin;
};
struct mp_chip_data {
struct list_head irq_2_pin;
struct IO_APIC_route_entry entry;
bool is_level;
bool active_low;
bool isa_irq;
u32 count;
};
static bool revert;
module_param(revert, bool, 0444);
MODULE_PARM_DESC(revert, "restore active-low instead of clearing it");
#define KBD_IRQ 1
#define MAX_PINS 8
static int __init irq1fix_init(void)
{
struct irq_data *d;
struct mp_chip_data *data;
struct list_head *p;
unsigned int n = 0;
/* forward: expect active_low set, clear it; revert: the inverse */
bool from = !revert;
d = irq_get_irq_data(KBD_IRQ);
if (!d) {
pr_err("irq1fix: no irq_data for IRQ %d\n", KBD_IRQ);
return -ENOENT;
}
if (d->hwirq != KBD_IRQ) {
pr_err("irq1fix: IRQ %d hwirq is %lu, expected %d\n",
KBD_IRQ, d->hwirq, KBD_IRQ);
return -ENODEV;
}
if (!d->chip || !d->chip->name ||
(strcmp(d->chip->name, "IO-APIC") &&
strcmp(d->chip->name, "IR-IO-APIC"))) {
pr_err("irq1fix: IRQ %d chip is '%s', not an IOAPIC\n",
KBD_IRQ, d->chip && d->chip->name ? d->chip->name : "?");
return -ENODEV;
}
if (irq_has_action(KBD_IRQ)) {
pr_err("irq1fix: IRQ %d still has a handler — unbind the i8042 platform driver first\n",
KBD_IRQ);
return -EBUSY;
}
data = irq_data_get_irq_chip_data(d);
if (!data || !virt_addr_valid(data)) {
pr_err("irq1fix: no/invalid chip data on IRQ %d\n", KBD_IRQ);
return -ENODEV;
}
/* scalar signature first — refuse before touching the pin list */
if (!data->isa_irq || data->is_level || data->count < 1 ||
data->count > MAX_PINS ||
data->active_low != from ||
data->entry.is_level != 0 ||
data->entry.active_low != (from ? 1 : 0)) {
pr_err("irq1fix: mp_chip_data signature mismatch (isa=%d level=%d count=%u active_low=%d entry.al=%u entry.lvl=%u, expected active_low=%d) — already %s, or kernel layout changed; refusing\n",
data->isa_irq, data->is_level, data->count,
data->active_low, (unsigned int)data->entry.active_low,
(unsigned int)data->entry.is_level, from,
revert ? "active-low" : "fixed");
return -ENODEV;
}
/* mandatory bounded pin-list walk: exactly count entries, all apic 0 pin 1
* (hwirq==1 + IOAPIC chip alone is ambiguous — pin 1 of a second IOAPIC
* is a different interrupt with the same hwirq) */
for (p = data->irq_2_pin.next; p != &data->irq_2_pin; p = p->next) {
struct irq_pin_list *ent;
if (!virt_addr_valid(p)) {
pr_err("irq1fix: invalid pin-list pointer — refusing\n");
return -ENODEV;
}
ent = list_entry(p, struct irq_pin_list, list);
if (++n > MAX_PINS) {
pr_err("irq1fix: pin list too long — refusing\n");
return -ENODEV;
}
if (ent->apic != 0 || ent->pin != KBD_IRQ) {
pr_err("irq1fix: unexpected route apic=%d pin=%d (want apic=0 pin=%d) — refusing\n",
ent->apic, ent->pin, KBD_IRQ);
return -ENODEV;
}
}
/* data->count is an allocation refcount, NOT the pin-list length:
* mp_map_pin_to_irq() does data->count++ on every ALLOC mapping
* (io_apic.c:998 @ v7.1.9) while add_pin_to_irq_node() dedups the
* list (io_apic.c:341) and mp_unmap_irq() never decrements ISA IRQs
* (io_apic.c:1060). It equalled the list length on 7.1.8 boots by
* coincidence (single mapper); 7.1.9 boots map GSI1 twice, so only
* require a non-empty list — every entry was verified (0,1) above. */
if (n < 1) {
pr_err("irq1fix: empty pin list — refusing\n");
return -ENODEV;
}
if (n != data->count)
pr_info("irq1fix: note: %u pin route(s), alloc refcount %u\n",
n, data->count);
pr_info("irq1fix: IRQ %d validated (IOAPIC 0 pin %d, chip %s, %u route(s)): active_low %d -> %d\n",
KBD_IRQ, KBD_IRQ, d->chip->name, n, from, !from);
data->active_low = !from;
data->entry.active_low = from ? 0 : 1;
pr_info("irq1fix: chip data updated; rebind i8042 now — its request_irq() makes the kernel program the corrected IOAPIC entry\n");
return 0;
}
static void __exit irq1fix_exit(void)
{
/* nothing to undo here; load with revert=1 to flip back */
}
module_init(irq1fix_init);
module_exit(irq1fix_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("daemon-sec + Claude");
MODULE_DESCRIPTION("Runtime IRQ1 polarity fix for TongFang GM7RGxM (AMD Zen ACPI IRQ override bug)");