pci-acpi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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;
  33. union acpi_object out_obj;
  34. u32 osc_dw0;
  35. /* Setting up output buffer */
  36. output.length = sizeof(out_obj) + 3*sizeof(u32);
  37. output.pointer = &out_obj;
  38. /* Setting up input parameters */
  39. input.count = 4;
  40. input.pointer = in_params;
  41. in_params[0].type = ACPI_TYPE_BUFFER;
  42. in_params[0].buffer.length = 16;
  43. in_params[0].buffer.pointer = OSC_UUID;
  44. in_params[1].type = ACPI_TYPE_INTEGER;
  45. in_params[1].integer.value = 1;
  46. in_params[2].type = ACPI_TYPE_INTEGER;
  47. in_params[2].integer.value = 3;
  48. in_params[3].type = ACPI_TYPE_BUFFER;
  49. in_params[3].buffer.length = 12;
  50. in_params[3].buffer.pointer = (u8 *)context;
  51. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  52. if (ACPI_FAILURE (status)) {
  53. printk(KERN_DEBUG
  54. "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
  55. return status;
  56. }
  57. if (out_obj.type != ACPI_TYPE_BUFFER) {
  58. printk(KERN_DEBUG
  59. "Evaluate _OSC returns wrong type\n");
  60. return AE_TYPE;
  61. }
  62. osc_dw0 = *((u32 *) out_obj.buffer.pointer);
  63. if (osc_dw0) {
  64. if (osc_dw0 & OSC_REQUEST_ERROR)
  65. printk(KERN_DEBUG "_OSC request fails\n");
  66. if (osc_dw0 & OSC_INVALID_UUID_ERROR)
  67. printk(KERN_DEBUG "_OSC invalid UUID\n");
  68. if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
  69. printk(KERN_DEBUG "_OSC invalid revision\n");
  70. if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
  71. /* Update Global Control Set */
  72. global_ctrlsets = *((u32 *)(out_obj.buffer.pointer+8));
  73. return AE_OK;
  74. }
  75. return AE_ERROR;
  76. }
  77. /* Update Global Control Set */
  78. global_ctrlsets = *((u32 *)(out_obj.buffer.pointer + 8));
  79. return AE_OK;
  80. }
  81. static acpi_status
  82. acpi_run_osc (
  83. acpi_handle handle,
  84. void *context)
  85. {
  86. acpi_status status;
  87. struct acpi_object_list input;
  88. union acpi_object in_params[4];
  89. struct acpi_buffer output;
  90. union acpi_object out_obj;
  91. u32 osc_dw0;
  92. /* Setting up output buffer */
  93. output.length = sizeof(out_obj) + 3*sizeof(u32);
  94. output.pointer = &out_obj;
  95. /* Setting up input parameters */
  96. input.count = 4;
  97. input.pointer = in_params;
  98. in_params[0].type = ACPI_TYPE_BUFFER;
  99. in_params[0].buffer.length = 16;
  100. in_params[0].buffer.pointer = OSC_UUID;
  101. in_params[1].type = ACPI_TYPE_INTEGER;
  102. in_params[1].integer.value = 1;
  103. in_params[2].type = ACPI_TYPE_INTEGER;
  104. in_params[2].integer.value = 3;
  105. in_params[3].type = ACPI_TYPE_BUFFER;
  106. in_params[3].buffer.length = 12;
  107. in_params[3].buffer.pointer = (u8 *)context;
  108. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  109. if (ACPI_FAILURE (status)) {
  110. printk(KERN_DEBUG
  111. "Evaluate _OSC Set fails. Status = 0x%04x\n", status);
  112. return status;
  113. }
  114. if (out_obj.type != ACPI_TYPE_BUFFER) {
  115. printk(KERN_DEBUG
  116. "Evaluate _OSC returns wrong type\n");
  117. return AE_TYPE;
  118. }
  119. osc_dw0 = *((u32 *) out_obj.buffer.pointer);
  120. if (osc_dw0) {
  121. if (osc_dw0 & OSC_REQUEST_ERROR)
  122. printk(KERN_DEBUG "_OSC request fails\n");
  123. if (osc_dw0 & OSC_INVALID_UUID_ERROR)
  124. printk(KERN_DEBUG "_OSC invalid UUID\n");
  125. if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
  126. printk(KERN_DEBUG "_OSC invalid revision\n");
  127. if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
  128. printk(KERN_DEBUG "_OSC FW not grant req. control\n");
  129. return AE_SUPPORT;
  130. }
  131. return AE_ERROR;
  132. }
  133. return AE_OK;
  134. }
  135. /**
  136. * pci_osc_support_set - register OS support to Firmware
  137. * @flags: OS support bits
  138. *
  139. * Update OS support fields and doing a _OSC Query to obtain an update
  140. * from Firmware on supported control bits.
  141. **/
  142. acpi_status pci_osc_support_set(u32 flags)
  143. {
  144. u32 temp;
  145. if (!(flags & OSC_SUPPORT_MASKS)) {
  146. return AE_TYPE;
  147. }
  148. ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
  149. /* do _OSC query for all possible controls */
  150. temp = ctrlset_buf[OSC_CONTROL_TYPE];
  151. ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  152. ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
  153. acpi_get_devices ( PCI_ROOT_HID_STRING,
  154. acpi_query_osc,
  155. ctrlset_buf,
  156. NULL );
  157. ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
  158. ctrlset_buf[OSC_CONTROL_TYPE] = temp;
  159. return AE_OK;
  160. }
  161. EXPORT_SYMBOL(pci_osc_support_set);
  162. /**
  163. * pci_osc_control_set - commit requested control to Firmware
  164. * @handle: acpi_handle for the target ACPI object
  165. * @flags: driver's requested control bits
  166. *
  167. * Attempt to take control from Firmware on requested control bits.
  168. **/
  169. acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
  170. {
  171. acpi_status status;
  172. u32 ctrlset;
  173. ctrlset = (flags & OSC_CONTROL_MASKS);
  174. if (!ctrlset) {
  175. return AE_TYPE;
  176. }
  177. if (ctrlset_buf[OSC_SUPPORT_TYPE] &&
  178. ((global_ctrlsets & ctrlset) != ctrlset)) {
  179. return AE_SUPPORT;
  180. }
  181. ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
  182. status = acpi_run_osc(handle, ctrlset_buf);
  183. if (ACPI_FAILURE (status)) {
  184. ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
  185. }
  186. return status;
  187. }
  188. EXPORT_SYMBOL(pci_osc_control_set);
  189. /*
  190. * _SxD returns the D-state with the highest power
  191. * (lowest D-state number) supported in the S-state "x".
  192. *
  193. * If the devices does not have a _PRW
  194. * (Power Resources for Wake) supporting system wakeup from "x"
  195. * then the OS is free to choose a lower power (higher number
  196. * D-state) than the return value from _SxD.
  197. *
  198. * But if _PRW is enabled at S-state "x", the OS
  199. * must not choose a power lower than _SxD --
  200. * unless the device has an _SxW method specifying
  201. * the lowest power (highest D-state number) the device
  202. * may enter while still able to wake the system.
  203. *
  204. * ie. depending on global OS policy:
  205. *
  206. * if (_PRW at S-state x)
  207. * choose from highest power _SxD to lowest power _SxW
  208. * else // no _PRW at S-state x
  209. * choose highest power _SxD or any lower power
  210. *
  211. * currently we simply return _SxD, if present.
  212. */
  213. static int acpi_pci_choose_state(struct pci_dev *pdev, pm_message_t state)
  214. {
  215. /* TBD */
  216. return -ENODEV;
  217. }
  218. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  219. {
  220. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
  221. static int state_conv[] = {
  222. [0] = 0,
  223. [1] = 1,
  224. [2] = 2,
  225. [3] = 3,
  226. [4] = 3
  227. };
  228. int acpi_state = state_conv[(int __force) state];
  229. if (!handle)
  230. return -ENODEV;
  231. return acpi_bus_set_power(handle, acpi_state);
  232. }
  233. /* ACPI bus type */
  234. static int pci_acpi_find_device(struct device *dev, acpi_handle *handle)
  235. {
  236. struct pci_dev * pci_dev;
  237. acpi_integer addr;
  238. pci_dev = to_pci_dev(dev);
  239. /* Please ref to ACPI spec for the syntax of _ADR */
  240. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  241. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
  242. if (!*handle)
  243. return -ENODEV;
  244. return 0;
  245. }
  246. static int pci_acpi_find_root_bridge(struct device *dev, acpi_handle *handle)
  247. {
  248. int num;
  249. unsigned int seg, bus;
  250. /*
  251. * The string should be the same as root bridge's name
  252. * Please look at 'pci_scan_bus_parented'
  253. */
  254. num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
  255. if (num != 2)
  256. return -ENODEV;
  257. *handle = acpi_get_pci_rootbridge_handle(seg, bus);
  258. if (!*handle)
  259. return -ENODEV;
  260. return 0;
  261. }
  262. static struct acpi_bus_type pci_acpi_bus = {
  263. .bus = &pci_bus_type,
  264. .find_device = pci_acpi_find_device,
  265. .find_bridge = pci_acpi_find_root_bridge,
  266. };
  267. static int __init pci_acpi_init(void)
  268. {
  269. int ret;
  270. ret = register_acpi_bus_type(&pci_acpi_bus);
  271. if (ret)
  272. return 0;
  273. platform_pci_choose_state = acpi_pci_choose_state;
  274. platform_pci_set_power_state = acpi_pci_set_power_state;
  275. return 0;
  276. }
  277. arch_initcall(pci_acpi_init);