acpi_watchdog.c 4.3 KB

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