pci-acpi.c 10 KB

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