pci-acpi.c 8.0 KB

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