pci-acpi.c 8.6 KB

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