acpi_watchdog.c 4.7 KB

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