pci-acpi.c 10 KB

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