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