| 1 | /* |
| 2 | * SBSA GWDT Watchdog Action Table (WDAT) |
| 3 | * |
| 4 | * Copyright Red Hat, Inc. 2026 |
| 5 | * Author(s): Igor Mammedov <imammedo@redhat.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #include "qemu/osdep.h" |
| 11 | #include "hw/acpi/aml-build.h" |
| 12 | #include "hw/acpi/wdat-gwdt.h" |
| 13 | #include "hw/acpi/wdat.h" |
| 14 | #include "hw/watchdog/sbsa_gwdt.h" |
| 15 | |
| 16 | #define GWDT_REG(base, reg_offset, reg_width) { \ |
| 17 | .space_id = AML_AS_SYSTEM_MEMORY, \ |
| 18 | .address = base + reg_offset, .bit_width = reg_width, \ |
| 19 | .access_width = AML_DWORD_ACC }; |
| 20 | |
| 21 | /* |
| 22 | * "Hardware Watchdog Timers Design Specification" |
| 23 | * https://uefi.org/acpi 'Watchdog Action Table (WDAT)' |
| 24 | */ |
| 25 | void build_gwdt_wdat(GArray *table_data, BIOSLinker *linker, const char *oem_id, |
| 26 | const char *oem_table_id, uint64_t rbase, uint64_t cbase, |
| 27 | uint64_t freq) |
| 28 | { |
| 29 | AcpiTable table = { .sig = "WDAT", .rev = 1, .oem_id = oem_id, |
| 30 | .oem_table_id = oem_table_id }; |
| 31 | |
| 32 | struct AcpiGenericAddress wrr = GWDT_REG(rbase, 0x0, 32); |
| 33 | struct AcpiGenericAddress wor_l = GWDT_REG(cbase, SBSA_GWDT_WOR, 32); |
| 34 | struct AcpiGenericAddress wcs = GWDT_REG(cbase, SBSA_GWDT_WCS, 32); |
| 35 | |
| 36 | acpi_table_begin(&table, table_data); |
| 37 | build_append_int_noprefix(table_data, 0x20, 4); /* Watchdog Header Length */ |
| 38 | /* |
| 39 | * PCI location fields are set to 0xff to indicate |
| 40 | * that the watchdog is not a PCI device. |
| 41 | */ |
| 42 | build_append_int_noprefix(table_data, 0xff, 2); /* PCI Segment */ |
| 43 | build_append_int_noprefix(table_data, 0xff, 1); /* PCI Bus Number */ |
| 44 | build_append_int_noprefix(table_data, 0xff, 1); /* PCI Device Number */ |
| 45 | build_append_int_noprefix(table_data, 0xff, 1); /* PCI Function Number */ |
| 46 | build_append_int_noprefix(table_data, 0, 3); /* Reserved */ |
| 47 | /* |
| 48 | * WDAT spec: "The clock interval that the WDT uses must be |
| 49 | * greater than or equal to 1 millisecond." |
| 50 | */ |
| 51 | g_assert(freq <= 1000); |
| 52 | /* Timer Period, ms */ |
| 53 | build_append_int_noprefix(table_data, 1000 / freq, 4); |
| 54 | /* |
| 55 | * WDAT spec: "The time-out period before the WDT fires is recommended |
| 56 | * to be at least 5 minutes." |
| 57 | * Set max count to 10min and min count to 5sec. |
| 58 | */ |
| 59 | build_append_int_noprefix(table_data, 600 * freq, 4); /* Maximum Count */ |
| 60 | build_append_int_noprefix(table_data, 5 * freq, 4); /* Minimum Count */ |
| 61 | /* |
| 62 | * WATCHDOG_ENABLED | WATCHDOG_STOPPED_IN_SLEEP_STATE |
| 63 | */ |
| 64 | build_append_int_noprefix(table_data, 0x81, 1); /* Watchdog Flags */ |
| 65 | build_append_int_noprefix(table_data, 0, 3); /* Reserved */ |
| 66 | /* |
| 67 | * watchdog instruction entries |
| 68 | */ |
| 69 | build_append_int_noprefix(table_data, 8, 4); |
| 70 | /* Action table: WCS (control/status) register actions */ |
| 71 | build_append_wdat_ins(table_data, WDAT_ACTION_QUERY_RUNNING_STATE, |
| 72 | WDAT_INS_READ_VALUE, |
| 73 | wcs, 0x1, 0x1); |
| 74 | build_append_wdat_ins(table_data, WDAT_ACTION_SET_RUNNING_STATE, |
| 75 | WDAT_INS_WRITE_VALUE | WDAT_INS_PRESERVE_REGISTER, |
| 76 | wcs, 1, 0x00000001); |
| 77 | build_append_wdat_ins(table_data, WDAT_ACTION_QUERY_STOPPED_STATE, |
| 78 | WDAT_INS_READ_VALUE, |
| 79 | wcs, 0x0, 0x00000001); |
| 80 | build_append_wdat_ins(table_data, WDAT_ACTION_SET_STOPPED_STATE, |
| 81 | WDAT_INS_WRITE_VALUE | WDAT_INS_PRESERVE_REGISTER, |
| 82 | wcs, 0x0, 0x00000001); |
| 83 | build_append_wdat_ins(table_data, WDAT_ACTION_QUERY_WATCHDOG_STATUS, |
| 84 | WDAT_INS_READ_VALUE, |
| 85 | wcs, 0x4, 0x00000004); |
| 86 | /* WOR (offset) and WRR (refresh) register actions */ |
| 87 | build_append_wdat_ins(table_data, WDAT_ACTION_SET_COUNTDOWN_PERIOD, |
| 88 | WDAT_INS_WRITE_COUNTDOWN, |
| 89 | wor_l, 0, 0xffffffff); |
| 90 | /* WRR: any write refreshes the watchdog, value is ignored */ |
| 91 | build_append_wdat_ins(table_data, WDAT_ACTION_RESET, |
| 92 | WDAT_INS_WRITE_VALUE, |
| 93 | wrr, 0x1, 0x1); |
| 94 | build_append_wdat_ins(table_data, WDAT_ACTION_SET_WATCHDOG_STATUS, |
| 95 | WDAT_INS_WRITE_VALUE | WDAT_INS_PRESERVE_REGISTER, |
| 96 | wrr, 0x4, 0x4); |
| 97 | |
| 98 | acpi_table_end(linker, &table); |
| 99 | } |