acpi_watchdog.c 4.7 KB

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