pci-acpi.c 10 KB

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