acpi_watchdog.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/dmi.h>
  14. #include <linux/ioport.h>
  15. #include <linux/platform_device.h>
  16. #include "internal.h"
  17. static const struct dmi_system_id acpi_watchdog_skip[] = {
  18. {
  19. /*
  20. * On Lenovo Z50-70 there are two issues with the WDAT
  21. * table. First some of the instructions use RTC SRAM
  22. * to store persistent information. This does not work well
  23. * with Linux RTC driver. Second, more important thing is
  24. * that the instructions do not actually reset the system.
  25. *
  26. * On this particular system iTCO_wdt seems to work just
  27. * fine so we prefer that over WDAT for now.
  28. *
  29. * See also https://bugzilla.kernel.org/show_bug.cgi?id=199033.
  30. */
  31. .ident = "Lenovo Z50-70",
  32. .matches = {
  33. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  34. DMI_MATCH(DMI_PRODUCT_NAME, "20354"),
  35. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Z50-70"),
  36. },
  37. },
  38. {}
  39. };
  40. static const struct acpi_table_wdat *acpi_watchdog_get_wdat(void)
  41. {
  42. const struct acpi_table_wdat *wdat = NULL;
  43. acpi_status status;
  44. if (acpi_disabled)
  45. return NULL;
  46. if (dmi_check_system(acpi_watchdog_skip))
  47. return NULL;
  48. status = acpi_get_table(ACPI_SIG_WDAT, 0,
  49. (struct acpi_table_header **)&wdat);
  50. if (ACPI_FAILURE(status)) {
  51. /* It is fine if there is no WDAT */
  52. return NULL;
  53. }
  54. return wdat;
  55. }
  56. /**
  57. * Returns true if this system should prefer ACPI based watchdog instead of
  58. * the native one (which are typically the same hardware).
  59. */
  60. bool acpi_has_watchdog(void)
  61. {
  62. return !!acpi_watchdog_get_wdat();
  63. }
  64. EXPORT_SYMBOL_GPL(acpi_has_watchdog);
  65. void __init acpi_watchdog_init(void)
  66. {
  67. const struct acpi_wdat_entry *entries;
  68. const struct acpi_table_wdat *wdat;
  69. struct list_head resource_list;
  70. struct resource_entry *rentry;
  71. struct platform_device *pdev;
  72. struct resource *resources;
  73. size_t nresources = 0;
  74. int i;
  75. wdat = acpi_watchdog_get_wdat();
  76. if (!wdat) {
  77. /* It is fine if there is no WDAT */
  78. return;
  79. }
  80. /* Watchdog disabled by BIOS */
  81. if (!(wdat->flags & ACPI_WDAT_ENABLED))
  82. return;
  83. /* Skip legacy PCI WDT devices */
  84. if (wdat->pci_segment != 0xff || wdat->pci_bus != 0xff ||
  85. wdat->pci_device != 0xff || wdat->pci_function != 0xff)
  86. return;
  87. INIT_LIST_HEAD(&resource_list);
  88. entries = (struct acpi_wdat_entry *)(wdat + 1);
  89. for (i = 0; i < wdat->entries; i++) {
  90. const struct acpi_generic_address *gas;
  91. struct resource_entry *rentry;
  92. struct resource res = {};
  93. bool found;
  94. gas = &entries[i].register_region;
  95. res.start = gas->address;
  96. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  97. res.flags = IORESOURCE_MEM;
  98. res.end = res.start + ALIGN(gas->access_width, 4) - 1;
  99. } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  100. res.flags = IORESOURCE_IO;
  101. res.end = res.start + gas->access_width - 1;
  102. } else {
  103. pr_warn("Unsupported address space: %u\n",
  104. gas->space_id);
  105. goto fail_free_resource_list;
  106. }
  107. found = false;
  108. resource_list_for_each_entry(rentry, &resource_list) {
  109. if (rentry->res->flags == res.flags &&
  110. resource_overlaps(rentry->res, &res)) {
  111. if (res.start < rentry->res->start)
  112. rentry->res->start = res.start;
  113. if (res.end > rentry->res->end)
  114. rentry->res->end = res.end;
  115. found = true;
  116. break;
  117. }
  118. }
  119. if (!found) {
  120. rentry = resource_list_create_entry(NULL, 0);
  121. if (!rentry)
  122. goto fail_free_resource_list;
  123. *rentry->res = res;
  124. resource_list_add_tail(rentry, &resource_list);
  125. nresources++;
  126. }
  127. }
  128. resources = kcalloc(nresources, sizeof(*resources), GFP_KERNEL);
  129. if (!resources)
  130. goto fail_free_resource_list;
  131. i = 0;
  132. resource_list_for_each_entry(rentry, &resource_list)
  133. resources[i++] = *rentry->res;
  134. pdev = platform_device_register_simple("wdat_wdt", PLATFORM_DEVID_NONE,
  135. resources, nresources);
  136. if (IS_ERR(pdev))
  137. pr_err("Device creation failed: %ld\n", PTR_ERR(pdev));
  138. kfree(resources);
  139. fail_free_resource_list:
  140. resource_list_free(&resource_list);
  141. }