acpi_pcihp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Common ACPI functions for hot plug platforms
  3. *
  4. * Copyright (C) 2006 Intel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send feedback to <kristen.c.accardi@intel.com>
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/pci.h>
  31. #include <linux/pci_hotplug.h>
  32. #include <linux/acpi.h>
  33. #include <linux/pci-acpi.h>
  34. #define MY_NAME "acpi_pcihp"
  35. #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
  36. #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
  37. #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
  38. #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
  39. #define METHOD_NAME__SUN "_SUN"
  40. #define METHOD_NAME_OSHP "OSHP"
  41. static int debug_acpi;
  42. static acpi_status
  43. decode_type0_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
  44. {
  45. int i;
  46. union acpi_object *fields = record->package.elements;
  47. u32 revision = fields[1].integer.value;
  48. switch (revision) {
  49. case 1:
  50. if (record->package.count != 6)
  51. return AE_ERROR;
  52. for (i = 2; i < 6; i++)
  53. if (fields[i].type != ACPI_TYPE_INTEGER)
  54. return AE_ERROR;
  55. hpx->t0 = &hpx->type0_data;
  56. hpx->t0->revision = revision;
  57. hpx->t0->cache_line_size = fields[2].integer.value;
  58. hpx->t0->latency_timer = fields[3].integer.value;
  59. hpx->t0->enable_serr = fields[4].integer.value;
  60. hpx->t0->enable_perr = fields[5].integer.value;
  61. break;
  62. default:
  63. printk(KERN_WARNING
  64. "%s: Type 0 Revision %d record not supported\n",
  65. __func__, revision);
  66. return AE_ERROR;
  67. }
  68. return AE_OK;
  69. }
  70. static acpi_status
  71. decode_type1_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
  72. {
  73. int i;
  74. union acpi_object *fields = record->package.elements;
  75. u32 revision = fields[1].integer.value;
  76. switch (revision) {
  77. case 1:
  78. if (record->package.count != 5)
  79. return AE_ERROR;
  80. for (i = 2; i < 5; i++)
  81. if (fields[i].type != ACPI_TYPE_INTEGER)
  82. return AE_ERROR;
  83. hpx->t1 = &hpx->type1_data;
  84. hpx->t1->revision = revision;
  85. hpx->t1->max_mem_read = fields[2].integer.value;
  86. hpx->t1->avg_max_split = fields[3].integer.value;
  87. hpx->t1->tot_max_split = fields[4].integer.value;
  88. break;
  89. default:
  90. printk(KERN_WARNING
  91. "%s: Type 1 Revision %d record not supported\n",
  92. __func__, revision);
  93. return AE_ERROR;
  94. }
  95. return AE_OK;
  96. }
  97. static acpi_status
  98. decode_type2_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
  99. {
  100. int i;
  101. union acpi_object *fields = record->package.elements;
  102. u32 revision = fields[1].integer.value;
  103. switch (revision) {
  104. case 1:
  105. if (record->package.count != 18)
  106. return AE_ERROR;
  107. for (i = 2; i < 18; i++)
  108. if (fields[i].type != ACPI_TYPE_INTEGER)
  109. return AE_ERROR;
  110. hpx->t2 = &hpx->type2_data;
  111. hpx->t2->revision = revision;
  112. hpx->t2->unc_err_mask_and = fields[2].integer.value;
  113. hpx->t2->unc_err_mask_or = fields[3].integer.value;
  114. hpx->t2->unc_err_sever_and = fields[4].integer.value;
  115. hpx->t2->unc_err_sever_or = fields[5].integer.value;
  116. hpx->t2->cor_err_mask_and = fields[6].integer.value;
  117. hpx->t2->cor_err_mask_or = fields[7].integer.value;
  118. hpx->t2->adv_err_cap_and = fields[8].integer.value;
  119. hpx->t2->adv_err_cap_or = fields[9].integer.value;
  120. hpx->t2->pci_exp_devctl_and = fields[10].integer.value;
  121. hpx->t2->pci_exp_devctl_or = fields[11].integer.value;
  122. hpx->t2->pci_exp_lnkctl_and = fields[12].integer.value;
  123. hpx->t2->pci_exp_lnkctl_or = fields[13].integer.value;
  124. hpx->t2->sec_unc_err_sever_and = fields[14].integer.value;
  125. hpx->t2->sec_unc_err_sever_or = fields[15].integer.value;
  126. hpx->t2->sec_unc_err_mask_and = fields[16].integer.value;
  127. hpx->t2->sec_unc_err_mask_or = fields[17].integer.value;
  128. break;
  129. default:
  130. printk(KERN_WARNING
  131. "%s: Type 2 Revision %d record not supported\n",
  132. __func__, revision);
  133. return AE_ERROR;
  134. }
  135. return AE_OK;
  136. }
  137. static acpi_status
  138. acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
  139. {
  140. acpi_status status;
  141. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  142. union acpi_object *package, *record, *fields;
  143. u32 type;
  144. int i;
  145. /* Clear the return buffer with zeros */
  146. memset(hpx, 0, sizeof(struct hotplug_params));
  147. status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
  148. if (ACPI_FAILURE(status))
  149. return status;
  150. package = (union acpi_object *)buffer.pointer;
  151. if (package->type != ACPI_TYPE_PACKAGE) {
  152. status = AE_ERROR;
  153. goto exit;
  154. }
  155. for (i = 0; i < package->package.count; i++) {
  156. record = &package->package.elements[i];
  157. if (record->type != ACPI_TYPE_PACKAGE) {
  158. status = AE_ERROR;
  159. goto exit;
  160. }
  161. fields = record->package.elements;
  162. if (fields[0].type != ACPI_TYPE_INTEGER ||
  163. fields[1].type != ACPI_TYPE_INTEGER) {
  164. status = AE_ERROR;
  165. goto exit;
  166. }
  167. type = fields[0].integer.value;
  168. switch (type) {
  169. case 0:
  170. status = decode_type0_hpx_record(record, hpx);
  171. if (ACPI_FAILURE(status))
  172. goto exit;
  173. break;
  174. case 1:
  175. status = decode_type1_hpx_record(record, hpx);
  176. if (ACPI_FAILURE(status))
  177. goto exit;
  178. break;
  179. case 2:
  180. status = decode_type2_hpx_record(record, hpx);
  181. if (ACPI_FAILURE(status))
  182. goto exit;
  183. break;
  184. default:
  185. printk(KERN_ERR "%s: Type %d record not supported\n",
  186. __func__, type);
  187. status = AE_ERROR;
  188. goto exit;
  189. }
  190. }
  191. exit:
  192. kfree(buffer.pointer);
  193. return status;
  194. }
  195. static acpi_status
  196. acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
  197. {
  198. acpi_status status;
  199. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  200. union acpi_object *package, *fields;
  201. int i;
  202. memset(hpp, 0, sizeof(struct hotplug_params));
  203. status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
  204. if (ACPI_FAILURE(status))
  205. return status;
  206. package = (union acpi_object *) buffer.pointer;
  207. if (package->type != ACPI_TYPE_PACKAGE ||
  208. package->package.count != 4) {
  209. status = AE_ERROR;
  210. goto exit;
  211. }
  212. fields = package->package.elements;
  213. for (i = 0; i < 4; i++) {
  214. if (fields[i].type != ACPI_TYPE_INTEGER) {
  215. status = AE_ERROR;
  216. goto exit;
  217. }
  218. }
  219. hpp->t0 = &hpp->type0_data;
  220. hpp->t0->revision = 1;
  221. hpp->t0->cache_line_size = fields[0].integer.value;
  222. hpp->t0->latency_timer = fields[1].integer.value;
  223. hpp->t0->enable_serr = fields[2].integer.value;
  224. hpp->t0->enable_perr = fields[3].integer.value;
  225. exit:
  226. kfree(buffer.pointer);
  227. return status;
  228. }
  229. /* acpi_run_oshp - get control of hotplug from the firmware
  230. *
  231. * @handle - the handle of the hotplug controller.
  232. */
  233. static acpi_status acpi_run_oshp(acpi_handle handle)
  234. {
  235. acpi_status status;
  236. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  237. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  238. /* run OSHP */
  239. status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
  240. if (ACPI_FAILURE(status))
  241. if (status != AE_NOT_FOUND)
  242. printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
  243. __func__, (char *)string.pointer, status);
  244. else
  245. dbg("%s:%s OSHP not found\n",
  246. __func__, (char *)string.pointer);
  247. else
  248. pr_debug("%s:%s OSHP passes\n", __func__,
  249. (char *)string.pointer);
  250. kfree(string.pointer);
  251. return status;
  252. }
  253. /* pci_get_hp_params
  254. *
  255. * @dev - the pci_dev for which we want parameters
  256. * @hpp - allocated by the caller
  257. */
  258. int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp)
  259. {
  260. acpi_status status;
  261. acpi_handle handle, phandle;
  262. struct pci_bus *pbus;
  263. handle = NULL;
  264. for (pbus = dev->bus; pbus; pbus = pbus->parent) {
  265. handle = acpi_pci_get_bridge_handle(pbus);
  266. if (handle)
  267. break;
  268. }
  269. /*
  270. * _HPP settings apply to all child buses, until another _HPP is
  271. * encountered. If we don't find an _HPP for the input pci dev,
  272. * look for it in the parent device scope since that would apply to
  273. * this pci dev.
  274. */
  275. while (handle) {
  276. status = acpi_run_hpx(handle, hpp);
  277. if (ACPI_SUCCESS(status))
  278. return 0;
  279. status = acpi_run_hpp(handle, hpp);
  280. if (ACPI_SUCCESS(status))
  281. return 0;
  282. if (acpi_is_root_bridge(handle))
  283. break;
  284. status = acpi_get_parent(handle, &phandle);
  285. if (ACPI_FAILURE(status))
  286. break;
  287. handle = phandle;
  288. }
  289. return -ENODEV;
  290. }
  291. EXPORT_SYMBOL_GPL(pci_get_hp_params);
  292. /**
  293. * acpi_get_hp_hw_control_from_firmware
  294. * @dev: the pci_dev of the bridge that has a hotplug controller
  295. * @flags: requested control bits for _OSC
  296. *
  297. * Attempt to take hotplug control from firmware.
  298. */
  299. int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev, u32 flags)
  300. {
  301. acpi_status status;
  302. acpi_handle chandle, handle;
  303. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  304. flags &= (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL |
  305. OSC_SHPC_NATIVE_HP_CONTROL |
  306. OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
  307. if (!flags) {
  308. err("Invalid flags %u specified!\n", flags);
  309. return -EINVAL;
  310. }
  311. /*
  312. * Per PCI firmware specification, we should run the ACPI _OSC
  313. * method to get control of hotplug hardware before using it. If
  314. * an _OSC is missing, we look for an OSHP to do the same thing.
  315. * To handle different BIOS behavior, we look for _OSC on a root
  316. * bridge preferentially (according to PCI fw spec). Later for
  317. * OSHP within the scope of the hotplug controller and its parents,
  318. * upto the host bridge under which this controller exists.
  319. */
  320. handle = acpi_find_root_bridge_handle(pdev);
  321. if (handle) {
  322. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  323. dbg("Trying to get hotplug control for %s\n",
  324. (char *)string.pointer);
  325. status = acpi_pci_osc_control_set(handle, flags);
  326. if (ACPI_SUCCESS(status))
  327. goto got_one;
  328. if (status == AE_SUPPORT)
  329. goto no_control;
  330. kfree(string.pointer);
  331. string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
  332. }
  333. handle = DEVICE_ACPI_HANDLE(&pdev->dev);
  334. if (!handle) {
  335. /*
  336. * This hotplug controller was not listed in the ACPI name
  337. * space at all. Try to get acpi handle of parent pci bus.
  338. */
  339. struct pci_bus *pbus;
  340. for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
  341. handle = acpi_pci_get_bridge_handle(pbus);
  342. if (handle)
  343. break;
  344. }
  345. }
  346. while (handle) {
  347. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  348. dbg("Trying to get hotplug control for %s \n",
  349. (char *)string.pointer);
  350. status = acpi_run_oshp(handle);
  351. if (ACPI_SUCCESS(status))
  352. goto got_one;
  353. if (acpi_is_root_bridge(handle))
  354. break;
  355. chandle = handle;
  356. status = acpi_get_parent(chandle, &handle);
  357. if (ACPI_FAILURE(status))
  358. break;
  359. }
  360. no_control:
  361. dbg("Cannot get control of hotplug hardware for pci %s\n",
  362. pci_name(pdev));
  363. kfree(string.pointer);
  364. return -ENODEV;
  365. got_one:
  366. dbg("Gained control for hotplug HW for pci %s (%s)\n",
  367. pci_name(pdev), (char *)string.pointer);
  368. kfree(string.pointer);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
  372. static int is_ejectable(acpi_handle handle)
  373. {
  374. acpi_status status;
  375. acpi_handle tmp;
  376. unsigned long long removable;
  377. status = acpi_get_handle(handle, "_ADR", &tmp);
  378. if (ACPI_FAILURE(status))
  379. return 0;
  380. status = acpi_get_handle(handle, "_EJ0", &tmp);
  381. if (ACPI_SUCCESS(status))
  382. return 1;
  383. status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
  384. if (ACPI_SUCCESS(status) && removable)
  385. return 1;
  386. return 0;
  387. }
  388. /**
  389. * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
  390. * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
  391. * @handle: ACPI handle to check
  392. *
  393. * Return 1 if handle is ejectable PCI slot, 0 otherwise.
  394. */
  395. int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
  396. {
  397. acpi_handle bridge_handle, parent_handle;
  398. if (!(bridge_handle = acpi_pci_get_bridge_handle(pbus)))
  399. return 0;
  400. if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
  401. return 0;
  402. if (bridge_handle != parent_handle)
  403. return 0;
  404. return is_ejectable(handle);
  405. }
  406. EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
  407. static acpi_status
  408. check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
  409. {
  410. int *found = (int *)context;
  411. if (is_ejectable(handle)) {
  412. *found = 1;
  413. return AE_CTRL_TERMINATE;
  414. }
  415. return AE_OK;
  416. }
  417. /**
  418. * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
  419. * @handle - handle of the PCI bus to scan
  420. *
  421. * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
  422. */
  423. int acpi_pci_detect_ejectable(acpi_handle handle)
  424. {
  425. int found = 0;
  426. if (!handle)
  427. return found;
  428. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  429. check_hotplug, NULL, (void *)&found, NULL);
  430. return found;
  431. }
  432. EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
  433. module_param(debug_acpi, bool, 0644);
  434. MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");