pci-acpi.c 9.9 KB

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