pci-acpi.c 10 KB

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