pci-acpi.c 10 KB

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