acpi_pcihp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Common ACPI functions for hot plug platforms
  4. *
  5. * Copyright (C) 2006 Intel Corporation
  6. *
  7. * All rights reserved.
  8. *
  9. * Send feedback to <[email protected]>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/pci.h>
  16. #include <linux/pci_hotplug.h>
  17. #include <linux/acpi.h>
  18. #include <linux/pci-acpi.h>
  19. #include <linux/slab.h>
  20. #define MY_NAME "acpi_pcihp"
  21. #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
  22. #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
  23. #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
  24. #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
  25. #define METHOD_NAME__SUN "_SUN"
  26. #define METHOD_NAME_OSHP "OSHP"
  27. static bool debug_acpi;
  28. /* acpi_run_oshp - get control of hotplug from the firmware
  29. *
  30. * @handle - the handle of the hotplug controller.
  31. */
  32. static acpi_status acpi_run_oshp(acpi_handle handle)
  33. {
  34. acpi_status status;
  35. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  36. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  37. /* run OSHP */
  38. status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
  39. if (ACPI_FAILURE(status))
  40. if (status != AE_NOT_FOUND)
  41. printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
  42. __func__, (char *)string.pointer, status);
  43. else
  44. dbg("%s:%s OSHP not found\n",
  45. __func__, (char *)string.pointer);
  46. else
  47. pr_debug("%s:%s OSHP passes\n", __func__,
  48. (char *)string.pointer);
  49. kfree(string.pointer);
  50. return status;
  51. }
  52. /**
  53. * acpi_get_hp_hw_control_from_firmware
  54. * @pdev: the pci_dev of the bridge that has a hotplug controller
  55. *
  56. * Attempt to take hotplug control from firmware.
  57. */
  58. int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev)
  59. {
  60. const struct pci_host_bridge *host;
  61. const struct acpi_pci_root *root;
  62. acpi_status status;
  63. acpi_handle chandle, handle;
  64. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  65. /*
  66. * If there's no ACPI host bridge (i.e., ACPI support is compiled
  67. * into the kernel but the hardware platform doesn't support ACPI),
  68. * there's nothing to do here.
  69. */
  70. host = pci_find_host_bridge(pdev->bus);
  71. root = acpi_pci_find_root(ACPI_HANDLE(&host->dev));
  72. if (!root)
  73. return 0;
  74. /*
  75. * If _OSC exists, it determines whether we're allowed to manage
  76. * the SHPC. We executed it while enumerating the host bridge.
  77. */
  78. if (root->osc_support_set) {
  79. if (host->native_shpc_hotplug)
  80. return 0;
  81. return -ENODEV;
  82. }
  83. /*
  84. * In the absence of _OSC, we're always allowed to manage the SHPC.
  85. * However, if an OSHP method is present, we must execute it so the
  86. * firmware can transfer control to the OS, e.g., direct interrupts
  87. * to the OS instead of to the firmware.
  88. *
  89. * N.B. The PCI Firmware Spec (r3.2, sec 4.8) does not endorse
  90. * searching up the ACPI hierarchy, so the loops below are suspect.
  91. */
  92. handle = ACPI_HANDLE(&pdev->dev);
  93. if (!handle) {
  94. /*
  95. * This hotplug controller was not listed in the ACPI name
  96. * space at all. Try to get ACPI handle of parent PCI bus.
  97. */
  98. struct pci_bus *pbus;
  99. for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
  100. handle = acpi_pci_get_bridge_handle(pbus);
  101. if (handle)
  102. break;
  103. }
  104. }
  105. while (handle) {
  106. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  107. pci_info(pdev, "Requesting control of SHPC hotplug via OSHP (%s)\n",
  108. (char *)string.pointer);
  109. status = acpi_run_oshp(handle);
  110. if (ACPI_SUCCESS(status))
  111. goto got_one;
  112. if (acpi_is_root_bridge(handle))
  113. break;
  114. chandle = handle;
  115. status = acpi_get_parent(chandle, &handle);
  116. if (ACPI_FAILURE(status))
  117. break;
  118. }
  119. pci_info(pdev, "Cannot get control of SHPC hotplug\n");
  120. kfree(string.pointer);
  121. return -ENODEV;
  122. got_one:
  123. pci_info(pdev, "Gained control of SHPC hotplug (%s)\n",
  124. (char *)string.pointer);
  125. kfree(string.pointer);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
  129. static int pcihp_is_ejectable(acpi_handle handle)
  130. {
  131. acpi_status status;
  132. unsigned long long removable;
  133. if (!acpi_has_method(handle, "_ADR"))
  134. return 0;
  135. if (acpi_has_method(handle, "_EJ0"))
  136. return 1;
  137. status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
  138. if (ACPI_SUCCESS(status) && removable)
  139. return 1;
  140. return 0;
  141. }
  142. /**
  143. * acpi_pci_check_ejectable - check if handle is ejectable ACPI PCI slot
  144. * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
  145. * @handle: ACPI handle to check
  146. *
  147. * Return 1 if handle is ejectable PCI slot, 0 otherwise.
  148. */
  149. int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
  150. {
  151. acpi_handle bridge_handle, parent_handle;
  152. bridge_handle = acpi_pci_get_bridge_handle(pbus);
  153. if (!bridge_handle)
  154. return 0;
  155. if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
  156. return 0;
  157. if (bridge_handle != parent_handle)
  158. return 0;
  159. return pcihp_is_ejectable(handle);
  160. }
  161. EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
  162. static acpi_status
  163. check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
  164. {
  165. int *found = (int *)context;
  166. if (pcihp_is_ejectable(handle)) {
  167. *found = 1;
  168. return AE_CTRL_TERMINATE;
  169. }
  170. return AE_OK;
  171. }
  172. /**
  173. * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
  174. * @handle: handle of the PCI bus to scan
  175. *
  176. * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
  177. */
  178. int acpi_pci_detect_ejectable(acpi_handle handle)
  179. {
  180. int found = 0;
  181. if (!handle)
  182. return found;
  183. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  184. check_hotplug, NULL, (void *)&found, NULL);
  185. return found;
  186. }
  187. EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
  188. module_param(debug_acpi, bool, 0644);
  189. MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");