2 * ACPI watchdog table parsing support.
4 * Copyright (C) 2016, Intel Corporation
5 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) "ACPI: watchdog: " fmt
14 #include <linux/acpi.h>
15 #include <linux/ioport.h>
16 #include <linux/platform_device.h>
20 #ifdef CONFIG_RTC_MC146818_LIB
21 #include <linux/mc146818rtc.h>
24 * There are several systems where the WDAT table is accessing RTC SRAM to
25 * store persistent information. This does not work well with the Linux RTC
26 * driver so on those systems we skip WDAT driver and prefer iTCO_wdt
29 * See also https://bugzilla.kernel.org/show_bug.cgi?id=199033.
31 static bool acpi_watchdog_uses_rtc(const struct acpi_table_wdat
*wdat
)
33 const struct acpi_wdat_entry
*entries
;
36 entries
= (struct acpi_wdat_entry
*)(wdat
+ 1);
37 for (i
= 0; i
< wdat
->entries
; i
++) {
38 const struct acpi_generic_address
*gas
;
40 gas
= &entries
[i
].register_region
;
41 if (gas
->space_id
== ACPI_ADR_SPACE_SYSTEM_IO
) {
42 switch (gas
->address
) {
55 static bool acpi_watchdog_uses_rtc(const struct acpi_table_wdat
*wdat
)
61 static const struct acpi_table_wdat
*acpi_watchdog_get_wdat(void)
63 const struct acpi_table_wdat
*wdat
= NULL
;
69 status
= acpi_get_table(ACPI_SIG_WDAT
, 0,
70 (struct acpi_table_header
**)&wdat
);
71 if (ACPI_FAILURE(status
)) {
72 /* It is fine if there is no WDAT */
76 if (acpi_watchdog_uses_rtc(wdat
)) {
77 pr_info("Skipping WDAT on this system because it uses RTC SRAM\n");
85 * Returns true if this system should prefer ACPI based watchdog instead of
86 * the native one (which are typically the same hardware).
88 bool acpi_has_watchdog(void)
90 return !!acpi_watchdog_get_wdat();
92 EXPORT_SYMBOL_GPL(acpi_has_watchdog
);
94 void __init
acpi_watchdog_init(void)
96 const struct acpi_wdat_entry
*entries
;
97 const struct acpi_table_wdat
*wdat
;
98 struct list_head resource_list
;
99 struct resource_entry
*rentry
;
100 struct platform_device
*pdev
;
101 struct resource
*resources
;
102 size_t nresources
= 0;
105 wdat
= acpi_watchdog_get_wdat();
107 /* It is fine if there is no WDAT */
111 /* Watchdog disabled by BIOS */
112 if (!(wdat
->flags
& ACPI_WDAT_ENABLED
))
115 /* Skip legacy PCI WDT devices */
116 if (wdat
->pci_segment
!= 0xff || wdat
->pci_bus
!= 0xff ||
117 wdat
->pci_device
!= 0xff || wdat
->pci_function
!= 0xff)
120 INIT_LIST_HEAD(&resource_list
);
122 entries
= (struct acpi_wdat_entry
*)(wdat
+ 1);
123 for (i
= 0; i
< wdat
->entries
; i
++) {
124 const struct acpi_generic_address
*gas
;
125 struct resource_entry
*rentry
;
126 struct resource res
= {};
129 gas
= &entries
[i
].register_region
;
131 res
.start
= gas
->address
;
132 if (gas
->space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
) {
133 res
.flags
= IORESOURCE_MEM
;
134 res
.end
= res
.start
+ ALIGN(gas
->access_width
, 4) - 1;
135 } else if (gas
->space_id
== ACPI_ADR_SPACE_SYSTEM_IO
) {
136 res
.flags
= IORESOURCE_IO
;
137 res
.end
= res
.start
+ gas
->access_width
- 1;
139 pr_warn("Unsupported address space: %u\n",
141 goto fail_free_resource_list
;
145 resource_list_for_each_entry(rentry
, &resource_list
) {
146 if (rentry
->res
->flags
== res
.flags
&&
147 resource_overlaps(rentry
->res
, &res
)) {
148 if (res
.start
< rentry
->res
->start
)
149 rentry
->res
->start
= res
.start
;
150 if (res
.end
> rentry
->res
->end
)
151 rentry
->res
->end
= res
.end
;
158 rentry
= resource_list_create_entry(NULL
, 0);
160 goto fail_free_resource_list
;
163 resource_list_add_tail(rentry
, &resource_list
);
168 resources
= kcalloc(nresources
, sizeof(*resources
), GFP_KERNEL
);
170 goto fail_free_resource_list
;
173 resource_list_for_each_entry(rentry
, &resource_list
)
174 resources
[i
++] = *rentry
->res
;
176 pdev
= platform_device_register_simple("wdat_wdt", PLATFORM_DEVID_NONE
,
177 resources
, nresources
);
179 pr_err("Device creation failed: %ld\n", PTR_ERR(pdev
));
183 fail_free_resource_list
:
184 resource_list_free(&resource_list
);