hwpci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*******************************************************************************
  2. *
  3. * Module Name: hwpci - Obtain PCI bus, device, and function numbers
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #define _COMPONENT ACPI_NAMESPACE
  45. ACPI_MODULE_NAME("hwpci")
  46. /* PCI configuration space values */
  47. #define PCI_CFG_HEADER_TYPE_REG 0x0E
  48. #define PCI_CFG_PRIMARY_BUS_NUMBER_REG 0x18
  49. #define PCI_CFG_SECONDARY_BUS_NUMBER_REG 0x19
  50. /* PCI header values */
  51. #define PCI_HEADER_TYPE_MASK 0x7F
  52. #define PCI_TYPE_BRIDGE 0x01
  53. #define PCI_TYPE_CARDBUS_BRIDGE 0x02
  54. typedef struct acpi_pci_device {
  55. acpi_handle device;
  56. struct acpi_pci_device *next;
  57. } acpi_pci_device;
  58. /* Local prototypes */
  59. static acpi_status
  60. acpi_hw_build_pci_list(acpi_handle root_pci_device,
  61. acpi_handle pci_region,
  62. struct acpi_pci_device **return_list_head);
  63. static acpi_status
  64. acpi_hw_process_pci_list(struct acpi_pci_id *pci_id,
  65. struct acpi_pci_device *list_head);
  66. static void acpi_hw_delete_pci_list(struct acpi_pci_device *list_head);
  67. static acpi_status
  68. acpi_hw_get_pci_device_info(struct acpi_pci_id *pci_id,
  69. acpi_handle pci_device,
  70. u16 *bus_number, u8 *is_bridge);
  71. /*******************************************************************************
  72. *
  73. * FUNCTION: acpi_hw_derive_pci_id
  74. *
  75. * PARAMETERS: pci_id - Initial values for the PCI ID. May be
  76. * modified by this function.
  77. * root_pci_device - A handle to a PCI device object. This
  78. * object must be a PCI Root Bridge having a
  79. * _HID value of either PNP0A03 or PNP0A08
  80. * pci_region - A handle to a PCI configuration space
  81. * Operation Region being initialized
  82. *
  83. * RETURN: Status
  84. *
  85. * DESCRIPTION: This function derives a full PCI ID for a PCI device,
  86. * consisting of a Segment number, Bus number, Device number,
  87. * and function code.
  88. *
  89. * The PCI hardware dynamically configures PCI bus numbers
  90. * depending on the bus topology discovered during system
  91. * initialization. This function is invoked during configuration
  92. * of a PCI_Config Operation Region in order to (possibly) update
  93. * the Bus/Device/Function numbers in the pci_id with the actual
  94. * values as determined by the hardware and operating system
  95. * configuration.
  96. *
  97. * The pci_id parameter is initially populated during the Operation
  98. * Region initialization. This function is then called, and is
  99. * will make any necessary modifications to the Bus, Device, or
  100. * Function number PCI ID subfields as appropriate for the
  101. * current hardware and OS configuration.
  102. *
  103. * NOTE: Created 08/2010. Replaces the previous OSL acpi_os_derive_pci_id
  104. * interface since this feature is OS-independent. This module
  105. * specifically avoids any use of recursion by building a local
  106. * temporary device list.
  107. *
  108. ******************************************************************************/
  109. acpi_status
  110. acpi_hw_derive_pci_id(struct acpi_pci_id *pci_id,
  111. acpi_handle root_pci_device, acpi_handle pci_region)
  112. {
  113. acpi_status status;
  114. struct acpi_pci_device *list_head = NULL;
  115. ACPI_FUNCTION_TRACE(hw_derive_pci_id);
  116. if (!pci_id) {
  117. return_ACPI_STATUS(AE_BAD_PARAMETER);
  118. }
  119. /* Build a list of PCI devices, from pci_region up to root_pci_device */
  120. status =
  121. acpi_hw_build_pci_list(root_pci_device, pci_region, &list_head);
  122. if (ACPI_SUCCESS(status)) {
  123. /* Walk the list, updating the PCI device/function/bus numbers */
  124. status = acpi_hw_process_pci_list(pci_id, list_head);
  125. /* Delete the list */
  126. acpi_hw_delete_pci_list(list_head);
  127. }
  128. return_ACPI_STATUS(status);
  129. }
  130. /*******************************************************************************
  131. *
  132. * FUNCTION: acpi_hw_build_pci_list
  133. *
  134. * PARAMETERS: root_pci_device - A handle to a PCI device object. This
  135. * object is guaranteed to be a PCI Root
  136. * Bridge having a _HID value of either
  137. * PNP0A03 or PNP0A08
  138. * pci_region - A handle to the PCI configuration space
  139. * Operation Region
  140. * return_list_head - Where the PCI device list is returned
  141. *
  142. * RETURN: Status
  143. *
  144. * DESCRIPTION: Builds a list of devices from the input PCI region up to the
  145. * Root PCI device for this namespace subtree.
  146. *
  147. ******************************************************************************/
  148. static acpi_status
  149. acpi_hw_build_pci_list(acpi_handle root_pci_device,
  150. acpi_handle pci_region,
  151. struct acpi_pci_device **return_list_head)
  152. {
  153. acpi_handle current_device;
  154. acpi_handle parent_device;
  155. acpi_status status;
  156. struct acpi_pci_device *list_element;
  157. struct acpi_pci_device *list_head = NULL;
  158. /*
  159. * Ascend namespace branch until the root_pci_device is reached, building
  160. * a list of device nodes. Loop will exit when either the PCI device is
  161. * found, or the root of the namespace is reached.
  162. */
  163. current_device = pci_region;
  164. while (1) {
  165. status = acpi_get_parent(current_device, &parent_device);
  166. if (ACPI_FAILURE(status)) {
  167. /* Must delete the list before exit */
  168. acpi_hw_delete_pci_list(*return_list_head);
  169. return (status);
  170. }
  171. /* Finished when we reach the PCI root device (PNP0A03 or PNP0A08) */
  172. if (parent_device == root_pci_device) {
  173. *return_list_head = list_head;
  174. return (AE_OK);
  175. }
  176. list_element = ACPI_ALLOCATE(sizeof(struct acpi_pci_device));
  177. if (!list_element) {
  178. /* Must delete the list before exit */
  179. acpi_hw_delete_pci_list(*return_list_head);
  180. return (AE_NO_MEMORY);
  181. }
  182. /* Put new element at the head of the list */
  183. list_element->next = list_head;
  184. list_element->device = parent_device;
  185. list_head = list_element;
  186. current_device = parent_device;
  187. }
  188. }
  189. /*******************************************************************************
  190. *
  191. * FUNCTION: acpi_hw_process_pci_list
  192. *
  193. * PARAMETERS: pci_id - Initial values for the PCI ID. May be
  194. * modified by this function.
  195. * list_head - Device list created by
  196. * acpi_hw_build_pci_list
  197. *
  198. * RETURN: Status
  199. *
  200. * DESCRIPTION: Walk downward through the PCI device list, getting the device
  201. * info for each, via the PCI configuration space and updating
  202. * the PCI ID as necessary. Deletes the list during traversal.
  203. *
  204. ******************************************************************************/
  205. static acpi_status
  206. acpi_hw_process_pci_list(struct acpi_pci_id *pci_id,
  207. struct acpi_pci_device *list_head)
  208. {
  209. acpi_status status = AE_OK;
  210. struct acpi_pci_device *info;
  211. u16 bus_number;
  212. u8 is_bridge = TRUE;
  213. ACPI_FUNCTION_NAME(hw_process_pci_list);
  214. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  215. "Input PciId: Seg %4.4X Bus %4.4X Dev %4.4X Func %4.4X\n",
  216. pci_id->segment, pci_id->bus, pci_id->device,
  217. pci_id->function));
  218. bus_number = pci_id->bus;
  219. /*
  220. * Descend down the namespace tree, collecting PCI device, function,
  221. * and bus numbers. bus_number is only important for PCI bridges.
  222. * Algorithm: As we descend the tree, use the last valid PCI device,
  223. * function, and bus numbers that are discovered, and assign them
  224. * to the PCI ID for the target device.
  225. */
  226. info = list_head;
  227. while (info) {
  228. status = acpi_hw_get_pci_device_info(pci_id, info->device,
  229. &bus_number, &is_bridge);
  230. if (ACPI_FAILURE(status)) {
  231. return (status);
  232. }
  233. info = info->next;
  234. }
  235. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  236. "Output PciId: Seg %4.4X Bus %4.4X Dev %4.4X Func %4.4X "
  237. "Status %X BusNumber %X IsBridge %X\n",
  238. pci_id->segment, pci_id->bus, pci_id->device,
  239. pci_id->function, status, bus_number, is_bridge));
  240. return (AE_OK);
  241. }
  242. /*******************************************************************************
  243. *
  244. * FUNCTION: acpi_hw_delete_pci_list
  245. *
  246. * PARAMETERS: list_head - Device list created by
  247. * acpi_hw_build_pci_list
  248. *
  249. * RETURN: None
  250. *
  251. * DESCRIPTION: Free the entire PCI list.
  252. *
  253. ******************************************************************************/
  254. static void acpi_hw_delete_pci_list(struct acpi_pci_device *list_head)
  255. {
  256. struct acpi_pci_device *next;
  257. struct acpi_pci_device *previous;
  258. next = list_head;
  259. while (next) {
  260. previous = next;
  261. next = previous->next;
  262. ACPI_FREE(previous);
  263. }
  264. }
  265. /*******************************************************************************
  266. *
  267. * FUNCTION: acpi_hw_get_pci_device_info
  268. *
  269. * PARAMETERS: pci_id - Initial values for the PCI ID. May be
  270. * modified by this function.
  271. * pci_device - Handle for the PCI device object
  272. * bus_number - Where a PCI bridge bus number is returned
  273. * is_bridge - Return value, indicates if this PCI
  274. * device is a PCI bridge
  275. *
  276. * RETURN: Status
  277. *
  278. * DESCRIPTION: Get the device info for a single PCI device object. Get the
  279. * _ADR (contains PCI device and function numbers), and for PCI
  280. * bridge devices, get the bus number from PCI configuration
  281. * space.
  282. *
  283. ******************************************************************************/
  284. static acpi_status
  285. acpi_hw_get_pci_device_info(struct acpi_pci_id *pci_id,
  286. acpi_handle pci_device,
  287. u16 *bus_number, u8 *is_bridge)
  288. {
  289. acpi_status status;
  290. acpi_object_type object_type;
  291. u64 return_value;
  292. u64 pci_value;
  293. /* We only care about objects of type Device */
  294. status = acpi_get_type(pci_device, &object_type);
  295. if (ACPI_FAILURE(status)) {
  296. return (status);
  297. }
  298. if (object_type != ACPI_TYPE_DEVICE) {
  299. return (AE_OK);
  300. }
  301. /* We need an _ADR. Ignore device if not present */
  302. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR,
  303. pci_device, &return_value);
  304. if (ACPI_FAILURE(status)) {
  305. return (AE_OK);
  306. }
  307. /*
  308. * From _ADR, get the PCI Device and Function and
  309. * update the PCI ID.
  310. */
  311. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(return_value));
  312. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(return_value));
  313. /*
  314. * If the previous device was a bridge, use the previous
  315. * device bus number
  316. */
  317. if (*is_bridge) {
  318. pci_id->bus = *bus_number;
  319. }
  320. /*
  321. * Get the bus numbers from PCI Config space:
  322. *
  323. * First, get the PCI header_type
  324. */
  325. *is_bridge = FALSE;
  326. status = acpi_os_read_pci_configuration(pci_id,
  327. PCI_CFG_HEADER_TYPE_REG,
  328. &pci_value, 8);
  329. if (ACPI_FAILURE(status)) {
  330. return (status);
  331. }
  332. /* We only care about bridges (1=pci_bridge, 2=card_bus_bridge) */
  333. pci_value &= PCI_HEADER_TYPE_MASK;
  334. if ((pci_value != PCI_TYPE_BRIDGE) &&
  335. (pci_value != PCI_TYPE_CARDBUS_BRIDGE)) {
  336. return (AE_OK);
  337. }
  338. /* Bridge: Get the Primary bus_number */
  339. status = acpi_os_read_pci_configuration(pci_id,
  340. PCI_CFG_PRIMARY_BUS_NUMBER_REG,
  341. &pci_value, 8);
  342. if (ACPI_FAILURE(status)) {
  343. return (status);
  344. }
  345. *is_bridge = TRUE;
  346. pci_id->bus = (u16)pci_value;
  347. /* Bridge: Get the Secondary bus_number */
  348. status = acpi_os_read_pci_configuration(pci_id,
  349. PCI_CFG_SECONDARY_BUS_NUMBER_REG,
  350. &pci_value, 8);
  351. if (ACPI_FAILURE(status)) {
  352. return (status);
  353. }
  354. *bus_number = (u16)pci_value;
  355. return (AE_OK);
  356. }