pci-acpi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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, const char *hid)
  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(hid,
  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. /**
  173. * pci_osc_control_set - commit requested control to Firmware
  174. * @handle: acpi_handle for the target ACPI object
  175. * @flags: driver's requested control bits
  176. *
  177. * Attempt to take control from Firmware on requested control bits.
  178. **/
  179. acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
  180. {
  181. acpi_status status;
  182. u32 ctrlset;
  183. ctrlset = (flags & OSC_CONTROL_MASKS);
  184. if (!ctrlset) {
  185. return AE_TYPE;
  186. }
  187. if (ctrlset_buf[OSC_SUPPORT_TYPE] &&
  188. ((global_ctrlsets & ctrlset) != ctrlset)) {
  189. return AE_SUPPORT;
  190. }
  191. ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
  192. status = acpi_run_osc(handle, ctrlset_buf);
  193. if (ACPI_FAILURE (status)) {
  194. ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
  195. }
  196. return status;
  197. }
  198. EXPORT_SYMBOL(pci_osc_control_set);
  199. #ifdef CONFIG_ACPI_SLEEP
  200. /*
  201. * _SxD returns the D-state with the highest power
  202. * (lowest D-state number) supported in the S-state "x".
  203. *
  204. * If the devices does not have a _PRW
  205. * (Power Resources for Wake) supporting system wakeup from "x"
  206. * then the OS is free to choose a lower power (higher number
  207. * D-state) than the return value from _SxD.
  208. *
  209. * But if _PRW is enabled at S-state "x", the OS
  210. * must not choose a power lower than _SxD --
  211. * unless the device has an _SxW method specifying
  212. * the lowest power (highest D-state number) the device
  213. * may enter while still able to wake the system.
  214. *
  215. * ie. depending on global OS policy:
  216. *
  217. * if (_PRW at S-state x)
  218. * choose from highest power _SxD to lowest power _SxW
  219. * else // no _PRW at S-state x
  220. * choose highest power _SxD or any lower power
  221. */
  222. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
  223. pm_message_t state)
  224. {
  225. int acpi_state;
  226. acpi_state = acpi_pm_device_sleep_state(&pdev->dev,
  227. device_may_wakeup(&pdev->dev), NULL);
  228. if (acpi_state < 0)
  229. return PCI_POWER_ERROR;
  230. switch (acpi_state) {
  231. case ACPI_STATE_D0:
  232. return PCI_D0;
  233. case ACPI_STATE_D1:
  234. return PCI_D1;
  235. case ACPI_STATE_D2:
  236. return PCI_D2;
  237. case ACPI_STATE_D3:
  238. return PCI_D3hot;
  239. }
  240. return PCI_POWER_ERROR;
  241. }
  242. #endif
  243. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  244. {
  245. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  246. acpi_handle tmp;
  247. static int state_conv[] = {
  248. [0] = 0,
  249. [1] = 1,
  250. [2] = 2,
  251. [3] = 3,
  252. [4] = 3
  253. };
  254. int acpi_state = state_conv[(int __force) state];
  255. if (!handle)
  256. return -ENODEV;
  257. /* If the ACPI device has _EJ0, ignore the device */
  258. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  259. return 0;
  260. return acpi_bus_set_power(handle, acpi_state);
  261. }
  262. /* ACPI bus type */
  263. static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
  264. {
  265. struct pci_dev * pci_dev;
  266. acpi_integer addr;
  267. pci_dev = to_pci_dev(dev);
  268. /* Please ref to ACPI spec for the syntax of _ADR */
  269. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  270. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
  271. if (!*handle)
  272. return -ENODEV;
  273. return 0;
  274. }
  275. static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
  276. {
  277. int num;
  278. unsigned int seg, bus;
  279. /*
  280. * The string should be the same as root bridge's name
  281. * Please look at 'pci_scan_bus_parented'
  282. */
  283. num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
  284. if (num != 2)
  285. return -ENODEV;
  286. *handle = acpi_get_pci_rootbridge_handle(seg, bus);
  287. if (!*handle)
  288. return -ENODEV;
  289. return 0;
  290. }
  291. static struct acpi_bus_type acpi_pci_bus = {
  292. .bus = &pci_bus_type,
  293. .find_device = acpi_pci_find_device,
  294. .find_bridge = acpi_pci_find_root_bridge,
  295. };
  296. static int __init acpi_pci_init(void)
  297. {
  298. int ret;
  299. if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
  300. printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
  301. pci_no_msi();
  302. }
  303. ret = register_acpi_bus_type(&acpi_pci_bus);
  304. if (ret)
  305. return 0;
  306. #ifdef CONFIG_ACPI_SLEEP
  307. platform_pci_choose_state = acpi_pci_choose_state;
  308. #endif
  309. platform_pci_set_power_state = acpi_pci_set_power_state;
  310. return 0;
  311. }
  312. arch_initcall(acpi_pci_init);