acpi_pcihp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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__HPP "_HPP"
  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. u8 nui[4];
  201. struct acpi_buffer ret_buf = { 0, NULL};
  202. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  203. union acpi_object *ext_obj, *package;
  204. int i, len = 0;
  205. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  206. /* Clear the return buffer with zeros */
  207. memset(hpp, 0, sizeof(struct hotplug_params));
  208. /* get _hpp */
  209. status = acpi_evaluate_object(handle, METHOD_NAME__HPP, NULL, &ret_buf);
  210. switch (status) {
  211. case AE_BUFFER_OVERFLOW:
  212. ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
  213. if (!ret_buf.pointer) {
  214. printk(KERN_ERR "%s:%s alloc for _HPP fail\n",
  215. __func__, (char *)string.pointer);
  216. kfree(string.pointer);
  217. return AE_NO_MEMORY;
  218. }
  219. status = acpi_evaluate_object(handle, METHOD_NAME__HPP,
  220. NULL, &ret_buf);
  221. if (ACPI_SUCCESS(status))
  222. break;
  223. default:
  224. if (ACPI_FAILURE(status)) {
  225. pr_debug("%s:%s _HPP fail=0x%x\n", __func__,
  226. (char *)string.pointer, status);
  227. kfree(string.pointer);
  228. return status;
  229. }
  230. }
  231. ext_obj = (union acpi_object *) ret_buf.pointer;
  232. if (ext_obj->type != ACPI_TYPE_PACKAGE) {
  233. printk(KERN_ERR "%s:%s _HPP obj not a package\n", __func__,
  234. (char *)string.pointer);
  235. status = AE_ERROR;
  236. goto free_and_return;
  237. }
  238. len = ext_obj->package.count;
  239. package = (union acpi_object *) ret_buf.pointer;
  240. for ( i = 0; (i < len) || (i < 4); i++) {
  241. ext_obj = (union acpi_object *) &package->package.elements[i];
  242. switch (ext_obj->type) {
  243. case ACPI_TYPE_INTEGER:
  244. nui[i] = (u8)ext_obj->integer.value;
  245. break;
  246. default:
  247. printk(KERN_ERR "%s:%s _HPP obj type incorrect\n",
  248. __func__, (char *)string.pointer);
  249. status = AE_ERROR;
  250. goto free_and_return;
  251. }
  252. }
  253. hpp->t0 = &hpp->type0_data;
  254. hpp->t0->cache_line_size = nui[0];
  255. hpp->t0->latency_timer = nui[1];
  256. hpp->t0->enable_serr = nui[2];
  257. hpp->t0->enable_perr = nui[3];
  258. pr_debug(" _HPP: cache_line_size=0x%x\n", hpp->t0->cache_line_size);
  259. pr_debug(" _HPP: latency timer =0x%x\n", hpp->t0->latency_timer);
  260. pr_debug(" _HPP: enable SERR =0x%x\n", hpp->t0->enable_serr);
  261. pr_debug(" _HPP: enable PERR =0x%x\n", hpp->t0->enable_perr);
  262. free_and_return:
  263. kfree(string.pointer);
  264. kfree(ret_buf.pointer);
  265. return status;
  266. }
  267. /* acpi_run_oshp - get control of hotplug from the firmware
  268. *
  269. * @handle - the handle of the hotplug controller.
  270. */
  271. static acpi_status acpi_run_oshp(acpi_handle handle)
  272. {
  273. acpi_status status;
  274. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  275. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  276. /* run OSHP */
  277. status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
  278. if (ACPI_FAILURE(status))
  279. if (status != AE_NOT_FOUND)
  280. printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
  281. __func__, (char *)string.pointer, status);
  282. else
  283. dbg("%s:%s OSHP not found\n",
  284. __func__, (char *)string.pointer);
  285. else
  286. pr_debug("%s:%s OSHP passes\n", __func__,
  287. (char *)string.pointer);
  288. kfree(string.pointer);
  289. return status;
  290. }
  291. /* acpi_get_hp_params_from_firmware
  292. *
  293. * @bus - the pci_bus of the bus on which the device is newly added
  294. * @hpp - allocated by the caller
  295. */
  296. acpi_status acpi_get_hp_params_from_firmware(struct pci_bus *bus,
  297. struct hotplug_params *hpp)
  298. {
  299. acpi_status status = AE_NOT_FOUND;
  300. acpi_handle handle, phandle;
  301. struct pci_bus *pbus;
  302. handle = NULL;
  303. for (pbus = bus; pbus; pbus = pbus->parent) {
  304. handle = acpi_pci_get_bridge_handle(pbus);
  305. if (handle)
  306. break;
  307. }
  308. /*
  309. * _HPP settings apply to all child buses, until another _HPP is
  310. * encountered. If we don't find an _HPP for the input pci dev,
  311. * look for it in the parent device scope since that would apply to
  312. * this pci dev. If we don't find any _HPP, use hardcoded defaults
  313. */
  314. while (handle) {
  315. status = acpi_run_hpx(handle, hpp);
  316. if (ACPI_SUCCESS(status))
  317. break;
  318. status = acpi_run_hpp(handle, hpp);
  319. if (ACPI_SUCCESS(status))
  320. break;
  321. if (acpi_root_bridge(handle))
  322. break;
  323. status = acpi_get_parent(handle, &phandle);
  324. if (ACPI_FAILURE(status))
  325. break;
  326. handle = phandle;
  327. }
  328. return status;
  329. }
  330. EXPORT_SYMBOL_GPL(acpi_get_hp_params_from_firmware);
  331. /**
  332. * acpi_get_hp_hw_control_from_firmware
  333. * @dev: the pci_dev of the bridge that has a hotplug controller
  334. * @flags: requested control bits for _OSC
  335. *
  336. * Attempt to take hotplug control from firmware.
  337. */
  338. int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev, u32 flags)
  339. {
  340. acpi_status status;
  341. acpi_handle chandle, handle;
  342. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  343. flags &= (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL |
  344. OSC_SHPC_NATIVE_HP_CONTROL |
  345. OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
  346. if (!flags) {
  347. err("Invalid flags %u specified!\n", flags);
  348. return -EINVAL;
  349. }
  350. /*
  351. * Per PCI firmware specification, we should run the ACPI _OSC
  352. * method to get control of hotplug hardware before using it. If
  353. * an _OSC is missing, we look for an OSHP to do the same thing.
  354. * To handle different BIOS behavior, we look for _OSC on a root
  355. * bridge preferentially (according to PCI fw spec). Later for
  356. * OSHP within the scope of the hotplug controller and its parents,
  357. * upto the host bridge under which this controller exists.
  358. */
  359. handle = acpi_find_root_bridge_handle(pdev);
  360. if (handle) {
  361. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  362. dbg("Trying to get hotplug control for %s\n",
  363. (char *)string.pointer);
  364. status = acpi_pci_osc_control_set(handle, flags);
  365. if (ACPI_SUCCESS(status))
  366. goto got_one;
  367. kfree(string.pointer);
  368. string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
  369. }
  370. handle = DEVICE_ACPI_HANDLE(&pdev->dev);
  371. if (!handle) {
  372. /*
  373. * This hotplug controller was not listed in the ACPI name
  374. * space at all. Try to get acpi handle of parent pci bus.
  375. */
  376. struct pci_bus *pbus;
  377. for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
  378. handle = acpi_pci_get_bridge_handle(pbus);
  379. if (handle)
  380. break;
  381. }
  382. }
  383. while (handle) {
  384. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  385. dbg("Trying to get hotplug control for %s \n",
  386. (char *)string.pointer);
  387. status = acpi_run_oshp(handle);
  388. if (ACPI_SUCCESS(status))
  389. goto got_one;
  390. if (acpi_root_bridge(handle))
  391. break;
  392. chandle = handle;
  393. status = acpi_get_parent(chandle, &handle);
  394. if (ACPI_FAILURE(status))
  395. break;
  396. }
  397. dbg("Cannot get control of hotplug hardware for pci %s\n",
  398. pci_name(pdev));
  399. kfree(string.pointer);
  400. return -ENODEV;
  401. got_one:
  402. dbg("Gained control for hotplug HW for pci %s (%s)\n",
  403. pci_name(pdev), (char *)string.pointer);
  404. kfree(string.pointer);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
  408. /* acpi_root_bridge - check to see if this acpi object is a root bridge
  409. *
  410. * @handle - the acpi object in question.
  411. */
  412. int acpi_root_bridge(acpi_handle handle)
  413. {
  414. acpi_status status;
  415. struct acpi_device_info *info;
  416. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  417. int i;
  418. status = acpi_get_object_info(handle, &buffer);
  419. if (ACPI_SUCCESS(status)) {
  420. info = buffer.pointer;
  421. if ((info->valid & ACPI_VALID_HID) &&
  422. !strcmp(PCI_ROOT_HID_STRING,
  423. info->hardware_id.value)) {
  424. kfree(buffer.pointer);
  425. return 1;
  426. }
  427. if (info->valid & ACPI_VALID_CID) {
  428. for (i=0; i < info->compatibility_id.count; i++) {
  429. if (!strcmp(PCI_ROOT_HID_STRING,
  430. info->compatibility_id.id[i].value)) {
  431. kfree(buffer.pointer);
  432. return 1;
  433. }
  434. }
  435. }
  436. kfree(buffer.pointer);
  437. }
  438. return 0;
  439. }
  440. EXPORT_SYMBOL_GPL(acpi_root_bridge);
  441. static int is_ejectable(acpi_handle handle)
  442. {
  443. acpi_status status;
  444. acpi_handle tmp;
  445. unsigned long long removable;
  446. status = acpi_get_handle(handle, "_ADR", &tmp);
  447. if (ACPI_FAILURE(status))
  448. return 0;
  449. status = acpi_get_handle(handle, "_EJ0", &tmp);
  450. if (ACPI_SUCCESS(status))
  451. return 1;
  452. status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
  453. if (ACPI_SUCCESS(status) && removable)
  454. return 1;
  455. return 0;
  456. }
  457. /**
  458. * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
  459. * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
  460. * @handle: ACPI handle to check
  461. *
  462. * Return 1 if handle is ejectable PCI slot, 0 otherwise.
  463. */
  464. int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
  465. {
  466. acpi_handle bridge_handle, parent_handle;
  467. if (!(bridge_handle = acpi_pci_get_bridge_handle(pbus)))
  468. return 0;
  469. if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
  470. return 0;
  471. if (bridge_handle != parent_handle)
  472. return 0;
  473. return is_ejectable(handle);
  474. }
  475. EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
  476. static acpi_status
  477. check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
  478. {
  479. int *found = (int *)context;
  480. if (is_ejectable(handle)) {
  481. *found = 1;
  482. return AE_CTRL_TERMINATE;
  483. }
  484. return AE_OK;
  485. }
  486. /**
  487. * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
  488. * @pbus - PCI bus to scan
  489. *
  490. * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
  491. */
  492. int acpi_pci_detect_ejectable(struct pci_bus *pbus)
  493. {
  494. acpi_handle handle;
  495. int found = 0;
  496. if (!(handle = acpi_pci_get_bridge_handle(pbus)))
  497. return 0;
  498. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  499. check_hotplug, (void *)&found, NULL);
  500. return found;
  501. }
  502. EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
  503. module_param(debug_acpi, bool, 0644);
  504. MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");