blob: 396e358c2cee0a7685eb32e5c78f31bc8038161b [file] [log] [blame]
Mika Westerberg058dfc72016-09-20 15:30:51 +03001/*
2 * ACPI watchdog table parsing support.
3 *
4 * Copyright (C) 2016, Intel Corporation
5 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
6 *
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.
10 */
11
12#define pr_fmt(fmt) "ACPI: watchdog: " fmt
13
14#include <linux/acpi.h>
15#include <linux/ioport.h>
16#include <linux/platform_device.h>
17
18#include "internal.h"
19
Mika Westerberg230c8322018-05-22 14:16:50 +030020#ifdef CONFIG_RTC_MC146818_LIB
21#include <linux/mc146818rtc.h>
22
23/*
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
27 * instead.
28 *
29 * See also https://bugzilla.kernel.org/show_bug.cgi?id=199033.
30 */
31static bool acpi_watchdog_uses_rtc(const struct acpi_table_wdat *wdat)
32{
33 const struct acpi_wdat_entry *entries;
34 int i;
35
36 entries = (struct acpi_wdat_entry *)(wdat + 1);
37 for (i = 0; i < wdat->entries; i++) {
38 const struct acpi_generic_address *gas;
39
40 gas = &entries[i].register_region;
41 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
42 switch (gas->address) {
43 case RTC_PORT(0):
44 case RTC_PORT(1):
45 case RTC_PORT(2):
46 case RTC_PORT(3):
47 return true;
48 }
49 }
50 }
51
52 return false;
53}
54#else
55static bool acpi_watchdog_uses_rtc(const struct acpi_table_wdat *wdat)
56{
57 return false;
58}
59#endif
Mika Westerbergad226b62018-04-23 14:16:03 +030060
61static const struct acpi_table_wdat *acpi_watchdog_get_wdat(void)
62{
63 const struct acpi_table_wdat *wdat = NULL;
64 acpi_status status;
65
66 if (acpi_disabled)
67 return NULL;
68
Mika Westerbergad226b62018-04-23 14:16:03 +030069 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 */
73 return NULL;
74 }
75
Mika Westerberg230c8322018-05-22 14:16:50 +030076 if (acpi_watchdog_uses_rtc(wdat)) {
77 pr_info("Skipping WDAT on this system because it uses RTC SRAM\n");
78 return NULL;
79 }
80
Mika Westerbergad226b62018-04-23 14:16:03 +030081 return wdat;
82}
83
Mika Westerberg058dfc72016-09-20 15:30:51 +030084/**
85 * Returns true if this system should prefer ACPI based watchdog instead of
86 * the native one (which are typically the same hardware).
87 */
88bool acpi_has_watchdog(void)
89{
Mika Westerbergad226b62018-04-23 14:16:03 +030090 return !!acpi_watchdog_get_wdat();
Mika Westerberg058dfc72016-09-20 15:30:51 +030091}
92EXPORT_SYMBOL_GPL(acpi_has_watchdog);
93
94void __init acpi_watchdog_init(void)
95{
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;
Mika Westerberg058dfc72016-09-20 15:30:51 +0300103 int i;
104
Mika Westerbergad226b62018-04-23 14:16:03 +0300105 wdat = acpi_watchdog_get_wdat();
106 if (!wdat) {
Mika Westerberg058dfc72016-09-20 15:30:51 +0300107 /* It is fine if there is no WDAT */
108 return;
109 }
110
111 /* Watchdog disabled by BIOS */
112 if (!(wdat->flags & ACPI_WDAT_ENABLED))
113 return;
114
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)
118 return;
119
120 INIT_LIST_HEAD(&resource_list);
121
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;
127 bool found;
128
129 gas = &entries[i].register_region;
130
131 res.start = gas->address;
132 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
133 res.flags = IORESOURCE_MEM;
Takashi Iwaid0826ba2018-03-19 14:51:49 +0100134 res.end = res.start + ALIGN(gas->access_width, 4) - 1;
Mika Westerberg058dfc72016-09-20 15:30:51 +0300135 } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
136 res.flags = IORESOURCE_IO;
Takashi Iwaid0826ba2018-03-19 14:51:49 +0100137 res.end = res.start + gas->access_width - 1;
Mika Westerberg058dfc72016-09-20 15:30:51 +0300138 } else {
139 pr_warn("Unsupported address space: %u\n",
140 gas->space_id);
141 goto fail_free_resource_list;
142 }
143
144 found = false;
145 resource_list_for_each_entry(rentry, &resource_list) {
146 if (resource_contains(rentry->res, &res)) {
147 found = true;
148 break;
149 }
150 }
151
152 if (!found) {
153 rentry = resource_list_create_entry(NULL, 0);
154 if (!rentry)
155 goto fail_free_resource_list;
156
157 *rentry->res = res;
158 resource_list_add_tail(rentry, &resource_list);
159 nresources++;
160 }
161 }
162
163 resources = kcalloc(nresources, sizeof(*resources), GFP_KERNEL);
164 if (!resources)
165 goto fail_free_resource_list;
166
167 i = 0;
168 resource_list_for_each_entry(rentry, &resource_list)
169 resources[i++] = *rentry->res;
170
171 pdev = platform_device_register_simple("wdat_wdt", PLATFORM_DEVID_NONE,
172 resources, nresources);
173 if (IS_ERR(pdev))
174 pr_err("Failed to create platform device\n");
175
176 kfree(resources);
177
178fail_free_resource_list:
179 resource_list_free(&resource_list);
180}