acpi_pcihp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <kristen.c.accardi@intel.com>
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/pci.h>
  17. #include <linux/pci_hotplug.h>
  18. #include <linux/acpi.h>
  19. #include <linux/pci-acpi.h>
  20. #include <linux/slab.h>
  21. #define MY_NAME "acpi_pcihp"
  22. #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
  23. #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
  24. #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
  25. #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
  26. #define METHOD_NAME__SUN "_SUN"
  27. #define METHOD_NAME_OSHP "OSHP"
  28. static bool debug_acpi;
  29. /* acpi_run_oshp - get control of hotplug from the firmware
  30. *
  31. * @handle - the handle of the hotplug controller.
  32. */
  33. static acpi_status acpi_run_oshp(acpi_handle handle)
  34. {
  35. acpi_status status;
  36. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  37. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  38. /* run OSHP */
  39. status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
  40. if (ACPI_FAILURE(status))
  41. if (status != AE_NOT_FOUND)
  42. printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
  43. __func__, (char *)string.pointer, status);
  44. else
  45. dbg("%s:%s OSHP not found\n",
  46. __func__, (char *)string.pointer);
  47. else
  48. pr_debug("%s:%s OSHP passes\n", __func__,
  49. (char *)string.pointer);
  50. kfree(string.pointer);
  51. return status;
  52. }
  53. /**
  54. * acpi_get_hp_hw_control_from_firmware
  55. * @dev: the pci_dev of the bridge that has a hotplug controller
  56. * @flags: requested control bits for _OSC
  57. *
  58. * Attempt to take hotplug control from firmware.
  59. */
  60. int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev, u32 flags)
  61. {
  62. acpi_status status;
  63. acpi_handle chandle, handle;
  64. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  65. flags &= OSC_PCI_SHPC_NATIVE_HP_CONTROL;
  66. if (!flags) {
  67. err("Invalid flags %u specified!\n", flags);
  68. return -EINVAL;
  69. }
  70. /*
  71. * Per PCI firmware specification, we should run the ACPI _OSC
  72. * method to get control of hotplug hardware before using it. If
  73. * an _OSC is missing, we look for an OSHP to do the same thing.
  74. * To handle different BIOS behavior, we look for _OSC on a root
  75. * bridge preferentially (according to PCI fw spec). Later for
  76. * OSHP within the scope of the hotplug controller and its parents,
  77. * up to the host bridge under which this controller exists.
  78. */
  79. handle = acpi_find_root_bridge_handle(pdev);
  80. if (handle) {
  81. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  82. dbg("Trying to get hotplug control for %s\n",
  83. (char *)string.pointer);
  84. status = acpi_pci_osc_control_set(handle, &flags, flags);
  85. if (ACPI_SUCCESS(status))
  86. goto got_one;
  87. if (status == AE_SUPPORT)
  88. goto no_control;
  89. kfree(string.pointer);
  90. string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
  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. dbg("Trying to get hotplug control for %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. no_control:
  120. dbg("Cannot get control of hotplug hardware for pci %s\n",
  121. pci_name(pdev));
  122. kfree(string.pointer);
  123. return -ENODEV;
  124. got_one:
  125. dbg("Gained control for hotplug HW for pci %s (%s)\n",
  126. pci_name(pdev), (char *)string.pointer);
  127. kfree(string.pointer);
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
  131. static int pcihp_is_ejectable(acpi_handle handle)
  132. {
  133. acpi_status status;
  134. unsigned long long removable;
  135. if (!acpi_has_method(handle, "_ADR"))
  136. return 0;
  137. if (acpi_has_method(handle, "_EJ0"))
  138. return 1;
  139. status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
  140. if (ACPI_SUCCESS(status) && removable)
  141. return 1;
  142. return 0;
  143. }
  144. /**
  145. * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
  146. * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
  147. * @handle: ACPI handle to check
  148. *
  149. * Return 1 if handle is ejectable PCI slot, 0 otherwise.
  150. */
  151. int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
  152. {
  153. acpi_handle bridge_handle, parent_handle;
  154. bridge_handle = acpi_pci_get_bridge_handle(pbus);
  155. if (!bridge_handle)
  156. return 0;
  157. if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
  158. return 0;
  159. if (bridge_handle != parent_handle)
  160. return 0;
  161. return pcihp_is_ejectable(handle);
  162. }
  163. EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
  164. static acpi_status
  165. check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
  166. {
  167. int *found = (int *)context;
  168. if (pcihp_is_ejectable(handle)) {
  169. *found = 1;
  170. return AE_CTRL_TERMINATE;
  171. }
  172. return AE_OK;
  173. }
  174. /**
  175. * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
  176. * @handle - handle of the PCI bus to scan
  177. *
  178. * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
  179. */
  180. int acpi_pci_detect_ejectable(acpi_handle handle)
  181. {
  182. int found = 0;
  183. if (!handle)
  184. return found;
  185. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  186. check_hotplug, NULL, (void *)&found, NULL);
  187. return found;
  188. }
  189. EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
  190. module_param(debug_acpi, bool, 0644);
  191. MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");