GNU Linux-libre 4.9.294-gnu1
[releases.git] / drivers / acpi / acpi_watchdog.c
1 /*
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
20 #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  */
31 static 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
55 static bool acpi_watchdog_uses_rtc(const struct acpi_table_wdat *wdat)
56 {
57         return false;
58 }
59 #endif
60
61 static bool acpi_no_watchdog;
62
63 static const struct acpi_table_wdat *acpi_watchdog_get_wdat(void)
64 {
65         const struct acpi_table_wdat *wdat = NULL;
66         acpi_status status;
67
68         if (acpi_disabled || acpi_no_watchdog)
69                 return NULL;
70
71         status = acpi_get_table(ACPI_SIG_WDAT, 0,
72                                 (struct acpi_table_header **)&wdat);
73         if (ACPI_FAILURE(status)) {
74                 /* It is fine if there is no WDAT */
75                 return NULL;
76         }
77
78         if (acpi_watchdog_uses_rtc(wdat)) {
79                 pr_info("Skipping WDAT on this system because it uses RTC SRAM\n");
80                 return NULL;
81         }
82
83         return wdat;
84 }
85
86 /**
87  * Returns true if this system should prefer ACPI based watchdog instead of
88  * the native one (which are typically the same hardware).
89  */
90 bool acpi_has_watchdog(void)
91 {
92         return !!acpi_watchdog_get_wdat();
93 }
94 EXPORT_SYMBOL_GPL(acpi_has_watchdog);
95
96 /* ACPI watchdog can be disabled on boot command line */
97 static int __init disable_acpi_watchdog(char *str)
98 {
99         acpi_no_watchdog = true;
100         return 1;
101 }
102 __setup("acpi_no_watchdog", disable_acpi_watchdog);
103
104 void __init acpi_watchdog_init(void)
105 {
106         const struct acpi_wdat_entry *entries;
107         const struct acpi_table_wdat *wdat;
108         struct list_head resource_list;
109         struct resource_entry *rentry;
110         struct platform_device *pdev;
111         struct resource *resources;
112         size_t nresources = 0;
113         int i;
114
115         wdat = acpi_watchdog_get_wdat();
116         if (!wdat) {
117                 /* It is fine if there is no WDAT */
118                 return;
119         }
120
121         /* Watchdog disabled by BIOS */
122         if (!(wdat->flags & ACPI_WDAT_ENABLED))
123                 return;
124
125         /* Skip legacy PCI WDT devices */
126         if (wdat->pci_segment != 0xff || wdat->pci_bus != 0xff ||
127             wdat->pci_device != 0xff || wdat->pci_function != 0xff)
128                 return;
129
130         INIT_LIST_HEAD(&resource_list);
131
132         entries = (struct acpi_wdat_entry *)(wdat + 1);
133         for (i = 0; i < wdat->entries; i++) {
134                 const struct acpi_generic_address *gas;
135                 struct resource_entry *rentry;
136                 struct resource res;
137                 bool found;
138
139                 gas = &entries[i].register_region;
140
141                 res.start = gas->address;
142                 res.end = res.start + ACPI_ACCESS_BYTE_WIDTH(gas->access_width) - 1;
143                 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
144                         res.flags = IORESOURCE_MEM;
145                 } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
146                         res.flags = IORESOURCE_IO;
147                 } else {
148                         pr_warn("Unsupported address space: %u\n",
149                                 gas->space_id);
150                         goto fail_free_resource_list;
151                 }
152
153                 found = false;
154                 resource_list_for_each_entry(rentry, &resource_list) {
155                         if (resource_contains(rentry->res, &res)) {
156                                 found = true;
157                                 break;
158                         }
159                 }
160
161                 if (!found) {
162                         rentry = resource_list_create_entry(NULL, 0);
163                         if (!rentry)
164                                 goto fail_free_resource_list;
165
166                         *rentry->res = res;
167                         resource_list_add_tail(rentry, &resource_list);
168                         nresources++;
169                 }
170         }
171
172         resources = kcalloc(nresources, sizeof(*resources), GFP_KERNEL);
173         if (!resources)
174                 goto fail_free_resource_list;
175
176         i = 0;
177         resource_list_for_each_entry(rentry, &resource_list)
178                 resources[i++] = *rentry->res;
179
180         pdev = platform_device_register_simple("wdat_wdt", PLATFORM_DEVID_NONE,
181                                                resources, nresources);
182         if (IS_ERR(pdev))
183                 pr_err("Failed to create platform device\n");
184
185         kfree(resources);
186
187 fail_free_resource_list:
188         resource_list_free(&resource_list);
189 }