^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ACPI watchdog table parsing support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) "ACPI: watchdog: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #ifdef CONFIG_RTC_MC146818_LIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mc146818rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * There are several systems where the WDAT table is accessing RTC SRAM to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * store persistent information. This does not work well with the Linux RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * driver so on those systems we skip WDAT driver and prefer iTCO_wdt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * See also https://bugzilla.kernel.org/show_bug.cgi?id=199033.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static bool acpi_watchdog_uses_rtc(const struct acpi_table_wdat *wdat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const struct acpi_wdat_entry *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) entries = (struct acpi_wdat_entry *)(wdat + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) for (i = 0; i < wdat->entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const struct acpi_generic_address *gas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) gas = &entries[i].register_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) switch (gas->address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case RTC_PORT(0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) case RTC_PORT(1):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) case RTC_PORT(2):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case RTC_PORT(3):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static bool acpi_watchdog_uses_rtc(const struct acpi_table_wdat *wdat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static bool acpi_no_watchdog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static const struct acpi_table_wdat *acpi_watchdog_get_wdat(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const struct acpi_table_wdat *wdat = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (acpi_disabled || acpi_no_watchdog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) status = acpi_get_table(ACPI_SIG_WDAT, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) (struct acpi_table_header **)&wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* It is fine if there is no WDAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (acpi_watchdog_uses_rtc(wdat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) acpi_put_table((struct acpi_table_header *)wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pr_info("Skipping WDAT on this system because it uses RTC SRAM\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return wdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Returns true if this system should prefer ACPI based watchdog instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * the native one (which are typically the same hardware).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) bool acpi_has_watchdog(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return !!acpi_watchdog_get_wdat();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) EXPORT_SYMBOL_GPL(acpi_has_watchdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* ACPI watchdog can be disabled on boot command line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static int __init disable_acpi_watchdog(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) acpi_no_watchdog = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __setup("acpi_no_watchdog", disable_acpi_watchdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void __init acpi_watchdog_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) const struct acpi_wdat_entry *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) const struct acpi_table_wdat *wdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct list_head resource_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct resource_entry *rentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct resource *resources;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) size_t nresources = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) wdat = acpi_watchdog_get_wdat();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!wdat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* It is fine if there is no WDAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Watchdog disabled by BIOS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!(wdat->flags & ACPI_WDAT_ENABLED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) goto fail_put_wdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Skip legacy PCI WDT devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (wdat->pci_segment != 0xff || wdat->pci_bus != 0xff ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) wdat->pci_device != 0xff || wdat->pci_function != 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto fail_put_wdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) INIT_LIST_HEAD(&resource_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) entries = (struct acpi_wdat_entry *)(wdat + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) for (i = 0; i < wdat->entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) const struct acpi_generic_address *gas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct resource_entry *rentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct resource res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) gas = &entries[i].register_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) res.start = gas->address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) res.end = res.start + ACPI_ACCESS_BYTE_WIDTH(gas->access_width) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) res.flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) res.flags = IORESOURCE_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) pr_warn("Unsupported address space: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) gas->space_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto fail_free_resource_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) resource_list_for_each_entry(rentry, &resource_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (rentry->res->flags == res.flags &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) resource_overlaps(rentry->res, &res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (res.start < rentry->res->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) rentry->res->start = res.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (res.end > rentry->res->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) rentry->res->end = res.end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) rentry = resource_list_create_entry(NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!rentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto fail_free_resource_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) *rentry->res = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) resource_list_add_tail(rentry, &resource_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) nresources++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) resources = kcalloc(nresources, sizeof(*resources), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) goto fail_free_resource_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) resource_list_for_each_entry(rentry, &resource_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) resources[i++] = *rentry->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) pdev = platform_device_register_simple("wdat_wdt", PLATFORM_DEVID_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) resources, nresources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (IS_ERR(pdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pr_err("Device creation failed: %ld\n", PTR_ERR(pdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) kfree(resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) fail_free_resource_list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) resource_list_free(&resource_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) fail_put_wdat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) acpi_put_table((struct acpi_table_header *)wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }