pci-acpi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * File: pci-acpi.c
  3. * Purpose: Provide PCI support in ACPI
  4. *
  5. * Copyright (C) 2005 David Shaohua Li <shaohua.li@intel.com>
  6. * Copyright (C) 2004 Tom Long Nguyen <tom.l.nguyen@intel.com>
  7. * Copyright (C) 2004 Intel Corp.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/init.h>
  11. #include <linux/pci.h>
  12. #include <linux/module.h>
  13. #include <acpi/acpi.h>
  14. #include <acpi/acnamesp.h>
  15. #include <acpi/acresrc.h>
  16. #include <acpi/acpi_bus.h>
  17. #include <linux/pci-acpi.h>
  18. #include "pci.h"
  19. static u32 ctrlset_buf[3] = {0, 0, 0};
  20. static u32 global_ctrlsets = 0;
  21. static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
  22. static acpi_status
  23. acpi_query_osc (
  24. acpi_handle handle,
  25. u32 level,
  26. void *context,
  27. void **retval )
  28. {
  29. acpi_status status;
  30. struct acpi_object_list input;
  31. union acpi_object in_params[4];
  32. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  33. union acpi_object *out_obj;
  34. u32 osc_dw0;
  35. acpi_status *ret_status = (acpi_status *)retval;
  36. /* Setting up input parameters */
  37. input.count = 4;
  38. input.pointer = in_params;
  39. in_params[0].type = ACPI_TYPE_BUFFER;
  40. in_params[0].buffer.length = 16;
  41. in_params[0].buffer.pointer = OSC_UUID;
  42. in_params[1].type = ACPI_TYPE_INTEGER;
  43. in_params[1].integer.value = 1;
  44. in_params[2].type = ACPI_TYPE_INTEGER;
  45. in_params[2].integer.value = 3;
  46. in_params[3].type = ACPI_TYPE_BUFFER;
  47. in_params[3].buffer.length = 12;
  48. in_params[3].buffer.pointer = (u8 *)context;
  49. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  50. if (ACPI_FAILURE (status)) {
  51. *ret_status = status;
  52. return status;
  53. }
  54. out_obj = output.pointer;
  55. if (out_obj->type != ACPI_TYPE_BUFFER) {
  56. printk(KERN_DEBUG
  57. "Evaluate _OSC returns wrong type\n");
  58. status = AE_TYPE;
  59. goto query_osc_out;
  60. }
  61. osc_dw0 = *((u32 *) out_obj->buffer.pointer);
  62. if (osc_dw0) {
  63. if (osc_dw0 & OSC_REQUEST_ERROR)
  64. printk(KERN_DEBUG "_OSC request fails\n");
  65. if (osc_dw0 & OSC_INVALID_UUID_ERROR)
  66. printk(KERN_DEBUG "_OSC invalid UUID\n");
  67. if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
  68. printk(KERN_DEBUG "_OSC invalid revision\n");
  69. if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
  70. /* Update Global Control Set */
  71. global_ctrlsets = *((u32 *)(out_obj->buffer.pointer+8));
  72. status = AE_OK;
  73. goto query_osc_out;
  74. }
  75. status = AE_ERROR;
  76. goto query_osc_out;
  77. }
  78. /* Update Global Control Set */
  79. global_ctrlsets = *((u32 *)(out_obj->buffer.pointer + 8));
  80. status = AE_OK;
  81. query_osc_out:
  82. kfree(output.pointer);
  83. *ret_status = status;
  84. return status;
  85. }
  86. static acpi_status
  87. acpi_run_osc (
  88. acpi_handle handle,
  89. void *context)
  90. {
  91. acpi_status status;
  92. struct acpi_object_list input;
  93. union acpi_object in_params[4];
  94. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  95. union acpi_object *out_obj;
  96. u32 osc_dw0;
  97. /* Setting up input parameters */
  98. input.count = 4;
  99. input.pointer = in_params;
  100. in_params[0].type = ACPI_TYPE_BUFFER;
  101. in_params[0].buffer.length = 16;
  102. in_params[0].buffer.pointer = OSC_UUID;
  103. in_params[1].type = ACPI_TYPE_INTEGER;
  104. in_params[1].integer.value = 1;
  105. in_params[2].type = ACPI_TYPE_INTEGER;
  106. in_params[2].integer.value = 3;
  107. in_params[3].type = ACPI_TYPE_BUFFER;
  108. in_params[3].buffer.length = 12;
  109. in_params[3].buffer.pointer = (u8 *)context;
  110. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  111. if (ACPI_FAILURE (status))
  112. return status;
  113. out_obj = output.pointer;
  114. if (out_obj->type != ACPI_TYPE_BUFFER) {
  115. printk(KERN_DEBUG
  116. "Evaluate _OSC returns wrong type\n");
  117. status = AE_TYPE;
  118. goto run_osc_out;
  119. }
  120. osc_dw0 = *((u32 *) out_obj->buffer.pointer);
  121. if (osc_dw0) {
  122. if (osc_dw0 & OSC_REQUEST_ERROR)
  123. printk(KERN_DEBUG "_OSC request fails\n");
  124. if (osc_dw0 & OSC_INVALID_UUID_ERROR)
  125. printk(KERN_DEBUG "_OSC invalid UUID\n");
  126. if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
  127. printk(KERN_DEBUG "_OSC invalid revision\n");
  128. if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
  129. printk(KERN_DEBUG "_OSC FW not grant req. control\n");
  130. status = AE_SUPPORT;
  131. goto run_osc_out;
  132. }
  133. status = AE_ERROR;
  134. goto run_osc_out;
  135. }
  136. status = AE_OK;
  137. run_osc_out:
  138. kfree(output.pointer);
  139. return status;
  140. }
  141. /**
  142. * pci_osc_support_set - register OS support to Firmware
  143. * @flags: OS support bits
  144. *
  145. * Update OS support fields and doing a _OSC Query to obtain an update
  146. * from Firmware on supported control bits.
  147. **/
  148. acpi_status pci_osc_support_set(u32 flags)
  149. {
  150. u32 temp;
  151. acpi_status retval;
  152. if (!(flags & OSC_SUPPORT_MASKS)) {
  153. return AE_TYPE;
  154. }
  155. ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
  156. /* do _OSC query for all possible controls */
  157. temp = ctrlset_buf[OSC_CONTROL_TYPE];
  158. ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  159. ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
  160. acpi_get_devices ( PCI_ROOT_HID_STRING,
  161. acpi_query_osc,
  162. ctrlset_buf,
  163. (void **) &retval );
  164. ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
  165. ctrlset_buf[OSC_CONTROL_TYPE] = temp;
  166. if (ACPI_FAILURE(retval)) {
  167. /* no osc support at all */
  168. ctrlset_buf[OSC_SUPPORT_TYPE] = 0;
  169. }
  170. return AE_OK;
  171. }
  172. EXPORT_SYMBOL(pci_osc_support_set);
  173. /**
  174. * pci_osc_control_set - commit requested control to Firmware
  175. * @handle: acpi_handle for the target ACPI object
  176. * @flags: driver's requested control bits
  177. *
  178. * Attempt to take control from Firmware on requested control bits.
  179. **/
  180. acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
  181. {
  182. acpi_status status;
  183. u32 ctrlset;
  184. ctrlset = (flags & OSC_CONTROL_MASKS);
  185. if (!ctrlset) {
  186. return AE_TYPE;
  187. }
  188. if (ctrlset_buf[OSC_SUPPORT_TYPE] &&
  189. ((global_ctrlsets & ctrlset) != ctrlset)) {
  190. return AE_SUPPORT;
  191. }
  192. ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
  193. status = acpi_run_osc(handle, ctrlset_buf);
  194. if (ACPI_FAILURE (status)) {
  195. ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
  196. }
  197. return status;
  198. }
  199. EXPORT_SYMBOL(pci_osc_control_set);
  200. #ifdef CONFIG_ACPI_SLEEP
  201. /*
  202. * _SxD returns the D-state with the highest power
  203. * (lowest D-state number) supported in the S-state "x".
  204. *
  205. * If the devices does not have a _PRW
  206. * (Power Resources for Wake) supporting system wakeup from "x"
  207. * then the OS is free to choose a lower power (higher number
  208. * D-state) than the return value from _SxD.
  209. *
  210. * But if _PRW is enabled at S-state "x", the OS
  211. * must not choose a power lower than _SxD --
  212. * unless the device has an _SxW method specifying
  213. * the lowest power (highest D-state number) the device
  214. * may enter while still able to wake the system.
  215. *
  216. * ie. depending on global OS policy:
  217. *
  218. * if (_PRW at S-state x)
  219. * choose from highest power _SxD to lowest power _SxW
  220. * else // no _PRW at S-state x
  221. * choose highest power _SxD or any lower power
  222. *
  223. * currently we simply return _SxD, if present.
  224. */
  225. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
  226. pm_message_t state)
  227. {
  228. int acpi_state;
  229. acpi_state = acpi_pm_device_sleep_state(&pdev->dev,
  230. device_may_wakeup(&pdev->dev), NULL);
  231. if (acpi_state < 0)
  232. return PCI_POWER_ERROR;
  233. switch (acpi_state) {
  234. case ACPI_STATE_D0:
  235. return PCI_D0;
  236. case ACPI_STATE_D1:
  237. return PCI_D1;
  238. case ACPI_STATE_D2:
  239. return PCI_D2;
  240. case ACPI_STATE_D3:
  241. return PCI_D3hot;
  242. }
  243. return PCI_POWER_ERROR;
  244. }
  245. #endif
  246. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  247. {
  248. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  249. acpi_handle tmp;
  250. static int state_conv[] = {
  251. [0] = 0,
  252. [1] = 1,
  253. [2] = 2,
  254. [3] = 3,
  255. [4] = 3
  256. };
  257. int acpi_state = state_conv[(int __force) state];
  258. if (!handle)
  259. return -ENODEV;
  260. /* If the ACPI device has _EJ0, ignore the device */
  261. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  262. return 0;
  263. return acpi_bus_set_power(handle, acpi_state);
  264. }
  265. /* ACPI bus type */
  266. static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
  267. {
  268. struct pci_dev * pci_dev;
  269. acpi_integer addr;
  270. pci_dev = to_pci_dev(dev);
  271. /* Please ref to ACPI spec for the syntax of _ADR */
  272. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  273. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
  274. if (!*handle)
  275. return -ENODEV;
  276. return 0;
  277. }
  278. static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
  279. {
  280. int num;
  281. unsigned int seg, bus;
  282. /*
  283. * The string should be the same as root bridge's name
  284. * Please look at 'pci_scan_bus_parented'
  285. */
  286. num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
  287. if (num != 2)
  288. return -ENODEV;
  289. *handle = acpi_get_pci_rootbridge_handle(seg, bus);
  290. if (!*handle)
  291. return -ENODEV;
  292. return 0;
  293. }
  294. static struct acpi_bus_type acpi_pci_bus = {
  295. .bus = &pci_bus_type,
  296. .find_device = acpi_pci_find_device,
  297. .find_bridge = acpi_pci_find_root_bridge,
  298. };
  299. static int __init acpi_pci_init(void)
  300. {
  301. int ret;
  302. if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
  303. printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
  304. pci_no_msi();
  305. }
  306. ret = register_acpi_bus_type(&acpi_pci_bus);
  307. if (ret)
  308. return 0;
  309. #ifdef CONFIG_ACPI_SLEEP
  310. platform_pci_choose_state = acpi_pci_choose_state;
  311. #endif
  312. platform_pci_set_power_state = acpi_pci_set_power_state;
  313. return 0;
  314. }
  315. arch_initcall(acpi_pci_init);