acpi_pcihp.c 14 KB

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