acpi_pcihp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Common ACPI functions for hot plug platforms
  3. *
  4. * Copyright (C) 2006 Intel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send feedback to <kristen.c.accardi@intel.com>
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/pci.h>
  31. #include <linux/pci_hotplug.h>
  32. #include <linux/acpi.h>
  33. #include <linux/pci-acpi.h>
  34. #include <linux/slab.h>
  35. #define MY_NAME "acpi_pcihp"
  36. #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
  37. #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
  38. #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
  39. #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
  40. #define METHOD_NAME__SUN "_SUN"
  41. #define METHOD_NAME_OSHP "OSHP"
  42. static bool debug_acpi;
  43. /* acpi_run_oshp - get control of hotplug from the firmware
  44. *
  45. * @handle - the handle of the hotplug controller.
  46. */
  47. static acpi_status acpi_run_oshp(acpi_handle handle)
  48. {
  49. acpi_status status;
  50. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  51. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  52. /* run OSHP */
  53. status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
  54. if (ACPI_FAILURE(status))
  55. if (status != AE_NOT_FOUND)
  56. printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
  57. __func__, (char *)string.pointer, status);
  58. else
  59. dbg("%s:%s OSHP not found\n",
  60. __func__, (char *)string.pointer);
  61. else
  62. pr_debug("%s:%s OSHP passes\n", __func__,
  63. (char *)string.pointer);
  64. kfree(string.pointer);
  65. return status;
  66. }
  67. /**
  68. * acpi_get_hp_hw_control_from_firmware
  69. * @dev: the pci_dev of the bridge that has a hotplug controller
  70. * @flags: requested control bits for _OSC
  71. *
  72. * Attempt to take hotplug control from firmware.
  73. */
  74. int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev, u32 flags)
  75. {
  76. acpi_status status;
  77. acpi_handle chandle, handle;
  78. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  79. flags &= OSC_PCI_SHPC_NATIVE_HP_CONTROL;
  80. if (!flags) {
  81. err("Invalid flags %u specified!\n", flags);
  82. return -EINVAL;
  83. }
  84. /*
  85. * Per PCI firmware specification, we should run the ACPI _OSC
  86. * method to get control of hotplug hardware before using it. If
  87. * an _OSC is missing, we look for an OSHP to do the same thing.
  88. * To handle different BIOS behavior, we look for _OSC on a root
  89. * bridge preferentially (according to PCI fw spec). Later for
  90. * OSHP within the scope of the hotplug controller and its parents,
  91. * up to the host bridge under which this controller exists.
  92. */
  93. handle = acpi_find_root_bridge_handle(pdev);
  94. if (handle) {
  95. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  96. dbg("Trying to get hotplug control for %s\n",
  97. (char *)string.pointer);
  98. status = acpi_pci_osc_control_set(handle, &flags, flags);
  99. if (ACPI_SUCCESS(status))
  100. goto got_one;
  101. if (status == AE_SUPPORT)
  102. goto no_control;
  103. kfree(string.pointer);
  104. string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
  105. }
  106. handle = ACPI_HANDLE(&pdev->dev);
  107. if (!handle) {
  108. /*
  109. * This hotplug controller was not listed in the ACPI name
  110. * space at all. Try to get acpi handle of parent pci bus.
  111. */
  112. struct pci_bus *pbus;
  113. for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
  114. handle = acpi_pci_get_bridge_handle(pbus);
  115. if (handle)
  116. break;
  117. }
  118. }
  119. while (handle) {
  120. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  121. dbg("Trying to get hotplug control for %s \n",
  122. (char *)string.pointer);
  123. status = acpi_run_oshp(handle);
  124. if (ACPI_SUCCESS(status))
  125. goto got_one;
  126. if (acpi_is_root_bridge(handle))
  127. break;
  128. chandle = handle;
  129. status = acpi_get_parent(chandle, &handle);
  130. if (ACPI_FAILURE(status))
  131. break;
  132. }
  133. no_control:
  134. dbg("Cannot get control of hotplug hardware for pci %s\n",
  135. pci_name(pdev));
  136. kfree(string.pointer);
  137. return -ENODEV;
  138. got_one:
  139. dbg("Gained control for hotplug HW for pci %s (%s)\n",
  140. pci_name(pdev), (char *)string.pointer);
  141. kfree(string.pointer);
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
  145. static int pcihp_is_ejectable(acpi_handle handle)
  146. {
  147. acpi_status status;
  148. unsigned long long removable;
  149. if (!acpi_has_method(handle, "_ADR"))
  150. return 0;
  151. if (acpi_has_method(handle, "_EJ0"))
  152. return 1;
  153. status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
  154. if (ACPI_SUCCESS(status) && removable)
  155. return 1;
  156. return 0;
  157. }
  158. /**
  159. * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
  160. * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
  161. * @handle: ACPI handle to check
  162. *
  163. * Return 1 if handle is ejectable PCI slot, 0 otherwise.
  164. */
  165. int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
  166. {
  167. acpi_handle bridge_handle, parent_handle;
  168. bridge_handle = acpi_pci_get_bridge_handle(pbus);
  169. if (!bridge_handle)
  170. return 0;
  171. if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
  172. return 0;
  173. if (bridge_handle != parent_handle)
  174. return 0;
  175. return pcihp_is_ejectable(handle);
  176. }
  177. EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
  178. static acpi_status
  179. check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
  180. {
  181. int *found = (int *)context;
  182. if (pcihp_is_ejectable(handle)) {
  183. *found = 1;
  184. return AE_CTRL_TERMINATE;
  185. }
  186. return AE_OK;
  187. }
  188. /**
  189. * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
  190. * @handle - handle of the PCI bus to scan
  191. *
  192. * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
  193. */
  194. int acpi_pci_detect_ejectable(acpi_handle handle)
  195. {
  196. int found = 0;
  197. if (!handle)
  198. return found;
  199. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  200. check_hotplug, NULL, (void *)&found, NULL);
  201. return found;
  202. }
  203. EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
  204. module_param(debug_acpi, bool, 0644);
  205. MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");