aerdrv_acpi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Access ACPI _OSC method
  3. *
  4. * Copyright (C) 2006 Intel Corp.
  5. * Tom Long Nguyen (tom.l.nguyen@intel.com)
  6. * Zhang Yanmin (yanmin.zhang@intel.com)
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/pm.h>
  14. #include <linux/suspend.h>
  15. #include <linux/acpi.h>
  16. #include <linux/pci-acpi.h>
  17. #include <linux/delay.h>
  18. #include <acpi/apei.h>
  19. #include "aerdrv.h"
  20. #ifdef CONFIG_ACPI_APEI
  21. static inline int hest_match_pci(struct acpi_hest_aer_common *p,
  22. struct pci_dev *pci)
  23. {
  24. return ACPI_HEST_SEGMENT(p->bus) == pci_domain_nr(pci->bus) &&
  25. ACPI_HEST_BUS(p->bus) == pci->bus->number &&
  26. p->device == PCI_SLOT(pci->devfn) &&
  27. p->function == PCI_FUNC(pci->devfn);
  28. }
  29. static inline bool hest_match_type(struct acpi_hest_header *hest_hdr,
  30. struct pci_dev *dev)
  31. {
  32. u16 hest_type = hest_hdr->type;
  33. u8 pcie_type = pci_pcie_type(dev);
  34. if ((hest_type == ACPI_HEST_TYPE_AER_ROOT_PORT &&
  35. pcie_type == PCI_EXP_TYPE_ROOT_PORT) ||
  36. (hest_type == ACPI_HEST_TYPE_AER_ENDPOINT &&
  37. pcie_type == PCI_EXP_TYPE_ENDPOINT) ||
  38. (hest_type == ACPI_HEST_TYPE_AER_BRIDGE &&
  39. (dev->class >> 16) == PCI_BASE_CLASS_BRIDGE))
  40. return true;
  41. return false;
  42. }
  43. struct aer_hest_parse_info {
  44. struct pci_dev *pci_dev;
  45. int firmware_first;
  46. };
  47. static int hest_source_is_pcie_aer(struct acpi_hest_header *hest_hdr)
  48. {
  49. if (hest_hdr->type == ACPI_HEST_TYPE_AER_ROOT_PORT ||
  50. hest_hdr->type == ACPI_HEST_TYPE_AER_ENDPOINT ||
  51. hest_hdr->type == ACPI_HEST_TYPE_AER_BRIDGE)
  52. return 1;
  53. return 0;
  54. }
  55. static int aer_hest_parse(struct acpi_hest_header *hest_hdr, void *data)
  56. {
  57. struct aer_hest_parse_info *info = data;
  58. struct acpi_hest_aer_common *p;
  59. int ff;
  60. if (!hest_source_is_pcie_aer(hest_hdr))
  61. return 0;
  62. p = (struct acpi_hest_aer_common *)(hest_hdr + 1);
  63. ff = !!(p->flags & ACPI_HEST_FIRMWARE_FIRST);
  64. /*
  65. * If no specific device is supplied, determine whether
  66. * FIRMWARE_FIRST is set for *any* PCIe device.
  67. */
  68. if (!info->pci_dev) {
  69. info->firmware_first |= ff;
  70. return 0;
  71. }
  72. /* Otherwise, check the specific device */
  73. if (p->flags & ACPI_HEST_GLOBAL) {
  74. if (hest_match_type(hest_hdr, info->pci_dev))
  75. info->firmware_first = ff;
  76. } else
  77. if (hest_match_pci(p, info->pci_dev))
  78. info->firmware_first = ff;
  79. return 0;
  80. }
  81. static void aer_set_firmware_first(struct pci_dev *pci_dev)
  82. {
  83. int rc;
  84. struct aer_hest_parse_info info = {
  85. .pci_dev = pci_dev,
  86. .firmware_first = 0,
  87. };
  88. rc = apei_hest_parse(aer_hest_parse, &info);
  89. if (rc)
  90. pci_dev->__aer_firmware_first = 0;
  91. else
  92. pci_dev->__aer_firmware_first = info.firmware_first;
  93. pci_dev->__aer_firmware_first_valid = 1;
  94. }
  95. int pcie_aer_get_firmware_first(struct pci_dev *dev)
  96. {
  97. if (!pci_is_pcie(dev))
  98. return 0;
  99. if (!dev->__aer_firmware_first_valid)
  100. aer_set_firmware_first(dev);
  101. return dev->__aer_firmware_first;
  102. }
  103. static bool aer_firmware_first;
  104. /**
  105. * aer_acpi_firmware_first - Check if APEI should control AER.
  106. */
  107. bool aer_acpi_firmware_first(void)
  108. {
  109. static bool parsed = false;
  110. struct aer_hest_parse_info info = {
  111. .pci_dev = NULL, /* Check all PCIe devices */
  112. .firmware_first = 0,
  113. };
  114. if (!parsed) {
  115. apei_hest_parse(aer_hest_parse, &info);
  116. aer_firmware_first = info.firmware_first;
  117. parsed = true;
  118. }
  119. return aer_firmware_first;
  120. }
  121. #endif