acpi_watchdog.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  17. * Returns true if this system should prefer ACPI based watchdog instead of
  18. * the native one (which are typically the same hardware).
  19. */
  20. bool acpi_has_watchdog(void)
  21. {
  22. struct acpi_table_header hdr;
  23. if (acpi_disabled)
  24. return false;
  25. return ACPI_SUCCESS(acpi_get_table_header(ACPI_SIG_WDAT, 0, &hdr));
  26. }
  27. EXPORT_SYMBOL_GPL(acpi_has_watchdog);
  28. void __init acpi_watchdog_init(void)
  29. {
  30. const struct acpi_wdat_entry *entries;
  31. const struct acpi_table_wdat *wdat;
  32. struct list_head resource_list;
  33. struct resource_entry *rentry;
  34. struct platform_device *pdev;
  35. struct resource *resources;
  36. size_t nresources = 0;
  37. acpi_status status;
  38. int i;
  39. status = acpi_get_table(ACPI_SIG_WDAT, 0,
  40. (struct acpi_table_header **)&wdat);
  41. if (ACPI_FAILURE(status)) {
  42. /* It is fine if there is no WDAT */
  43. return;
  44. }
  45. /* Watchdog disabled by BIOS */
  46. if (!(wdat->flags & ACPI_WDAT_ENABLED))
  47. return;
  48. /* Skip legacy PCI WDT devices */
  49. if (wdat->pci_segment != 0xff || wdat->pci_bus != 0xff ||
  50. wdat->pci_device != 0xff || wdat->pci_function != 0xff)
  51. return;
  52. INIT_LIST_HEAD(&resource_list);
  53. entries = (struct acpi_wdat_entry *)(wdat + 1);
  54. for (i = 0; i < wdat->entries; i++) {
  55. const struct acpi_generic_address *gas;
  56. struct resource_entry *rentry;
  57. struct resource res;
  58. bool found;
  59. gas = &entries[i].register_region;
  60. res.start = gas->address;
  61. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  62. res.flags = IORESOURCE_MEM;
  63. res.end = res.start + ALIGN(gas->access_width, 4) - 1;
  64. } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  65. res.flags = IORESOURCE_IO;
  66. res.end = res.start + gas->access_width - 1;
  67. } else {
  68. pr_warn("Unsupported address space: %u\n",
  69. gas->space_id);
  70. goto fail_free_resource_list;
  71. }
  72. found = false;
  73. resource_list_for_each_entry(rentry, &resource_list) {
  74. if (resource_contains(rentry->res, &res)) {
  75. found = true;
  76. break;
  77. }
  78. }
  79. if (!found) {
  80. rentry = resource_list_create_entry(NULL, 0);
  81. if (!rentry)
  82. goto fail_free_resource_list;
  83. *rentry->res = res;
  84. resource_list_add_tail(rentry, &resource_list);
  85. nresources++;
  86. }
  87. }
  88. resources = kcalloc(nresources, sizeof(*resources), GFP_KERNEL);
  89. if (!resources)
  90. goto fail_free_resource_list;
  91. i = 0;
  92. resource_list_for_each_entry(rentry, &resource_list)
  93. resources[i++] = *rentry->res;
  94. pdev = platform_device_register_simple("wdat_wdt", PLATFORM_DEVID_NONE,
  95. resources, nresources);
  96. if (IS_ERR(pdev))
  97. pr_err("Failed to create platform device\n");
  98. kfree(resources);
  99. fail_free_resource_list:
  100. resource_list_free(&resource_list);
  101. }