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 - 2016, 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;
  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. /*
  158. * Ascend namespace branch until the root_pci_device is reached, building
  159. * a list of device nodes. Loop will exit when either the PCI device is
  160. * found, or the root of the namespace is reached.
  161. */
  162. *return_list_head = NULL;
  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 (AE_OK);
  174. }
  175. list_element = ACPI_ALLOCATE(sizeof(struct acpi_pci_device));
  176. if (!list_element) {
  177. /* Must delete the list before exit */
  178. acpi_hw_delete_pci_list(*return_list_head);
  179. return (AE_NO_MEMORY);
  180. }
  181. /* Put new element at the head of the list */
  182. list_element->next = *return_list_head;
  183. list_element->device = parent_device;
  184. *return_list_head = list_element;
  185. current_device = parent_device;
  186. }
  187. }
  188. /*******************************************************************************
  189. *
  190. * FUNCTION: acpi_hw_process_pci_list
  191. *
  192. * PARAMETERS: pci_id - Initial values for the PCI ID. May be
  193. * modified by this function.
  194. * list_head - Device list created by
  195. * acpi_hw_build_pci_list
  196. *
  197. * RETURN: Status
  198. *
  199. * DESCRIPTION: Walk downward through the PCI device list, getting the device
  200. * info for each, via the PCI configuration space and updating
  201. * the PCI ID as necessary. Deletes the list during traversal.
  202. *
  203. ******************************************************************************/
  204. static acpi_status
  205. acpi_hw_process_pci_list(struct acpi_pci_id *pci_id,
  206. struct acpi_pci_device *list_head)
  207. {
  208. acpi_status status = AE_OK;
  209. struct acpi_pci_device *info;
  210. u16 bus_number;
  211. u8 is_bridge = TRUE;
  212. ACPI_FUNCTION_NAME(hw_process_pci_list);
  213. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  214. "Input PciId: Seg %4.4X Bus %4.4X Dev %4.4X Func %4.4X\n",
  215. pci_id->segment, pci_id->bus, pci_id->device,
  216. pci_id->function));
  217. bus_number = pci_id->bus;
  218. /*
  219. * Descend down the namespace tree, collecting PCI device, function,
  220. * and bus numbers. bus_number is only important for PCI bridges.
  221. * Algorithm: As we descend the tree, use the last valid PCI device,
  222. * function, and bus numbers that are discovered, and assign them
  223. * to the PCI ID for the target device.
  224. */
  225. info = list_head;
  226. while (info) {
  227. status = acpi_hw_get_pci_device_info(pci_id, info->device,
  228. &bus_number, &is_bridge);
  229. if (ACPI_FAILURE(status)) {
  230. return (status);
  231. }
  232. info = info->next;
  233. }
  234. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  235. "Output PciId: Seg %4.4X Bus %4.4X Dev %4.4X Func %4.4X "
  236. "Status %X BusNumber %X IsBridge %X\n",
  237. pci_id->segment, pci_id->bus, pci_id->device,
  238. pci_id->function, status, bus_number, is_bridge));
  239. return (AE_OK);
  240. }
  241. /*******************************************************************************
  242. *
  243. * FUNCTION: acpi_hw_delete_pci_list
  244. *
  245. * PARAMETERS: list_head - Device list created by
  246. * acpi_hw_build_pci_list
  247. *
  248. * RETURN: None
  249. *
  250. * DESCRIPTION: Free the entire PCI list.
  251. *
  252. ******************************************************************************/
  253. static void acpi_hw_delete_pci_list(struct acpi_pci_device *list_head)
  254. {
  255. struct acpi_pci_device *next;
  256. struct acpi_pci_device *previous;
  257. next = list_head;
  258. while (next) {
  259. previous = next;
  260. next = previous->next;
  261. ACPI_FREE(previous);
  262. }
  263. }
  264. /*******************************************************************************
  265. *
  266. * FUNCTION: acpi_hw_get_pci_device_info
  267. *
  268. * PARAMETERS: pci_id - Initial values for the PCI ID. May be
  269. * modified by this function.
  270. * pci_device - Handle for the PCI device object
  271. * bus_number - Where a PCI bridge bus number is returned
  272. * is_bridge - Return value, indicates if this PCI
  273. * device is a PCI bridge
  274. *
  275. * RETURN: Status
  276. *
  277. * DESCRIPTION: Get the device info for a single PCI device object. Get the
  278. * _ADR (contains PCI device and function numbers), and for PCI
  279. * bridge devices, get the bus number from PCI configuration
  280. * space.
  281. *
  282. ******************************************************************************/
  283. static acpi_status
  284. acpi_hw_get_pci_device_info(struct acpi_pci_id *pci_id,
  285. acpi_handle pci_device,
  286. u16 *bus_number, u8 *is_bridge)
  287. {
  288. acpi_status status;
  289. acpi_object_type object_type;
  290. u64 return_value;
  291. u64 pci_value;
  292. /* We only care about objects of type Device */
  293. status = acpi_get_type(pci_device, &object_type);
  294. if (ACPI_FAILURE(status)) {
  295. return (status);
  296. }
  297. if (object_type != ACPI_TYPE_DEVICE) {
  298. return (AE_OK);
  299. }
  300. /* We need an _ADR. Ignore device if not present */
  301. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR,
  302. pci_device, &return_value);
  303. if (ACPI_FAILURE(status)) {
  304. return (AE_OK);
  305. }
  306. /*
  307. * From _ADR, get the PCI Device and Function and
  308. * update the PCI ID.
  309. */
  310. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(return_value));
  311. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(return_value));
  312. /*
  313. * If the previous device was a bridge, use the previous
  314. * device bus number
  315. */
  316. if (*is_bridge) {
  317. pci_id->bus = *bus_number;
  318. }
  319. /*
  320. * Get the bus numbers from PCI Config space:
  321. *
  322. * First, get the PCI header_type
  323. */
  324. *is_bridge = FALSE;
  325. status = acpi_os_read_pci_configuration(pci_id,
  326. PCI_CFG_HEADER_TYPE_REG,
  327. &pci_value, 8);
  328. if (ACPI_FAILURE(status)) {
  329. return (status);
  330. }
  331. /* We only care about bridges (1=pci_bridge, 2=card_bus_bridge) */
  332. pci_value &= PCI_HEADER_TYPE_MASK;
  333. if ((pci_value != PCI_TYPE_BRIDGE) &&
  334. (pci_value != PCI_TYPE_CARDBUS_BRIDGE)) {
  335. return (AE_OK);
  336. }
  337. /* Bridge: Get the Primary bus_number */
  338. status = acpi_os_read_pci_configuration(pci_id,
  339. PCI_CFG_PRIMARY_BUS_NUMBER_REG,
  340. &pci_value, 8);
  341. if (ACPI_FAILURE(status)) {
  342. return (status);
  343. }
  344. *is_bridge = TRUE;
  345. pci_id->bus = (u16)pci_value;
  346. /* Bridge: Get the Secondary bus_number */
  347. status = acpi_os_read_pci_configuration(pci_id,
  348. PCI_CFG_SECONDARY_BUS_NUMBER_REG,
  349. &pci_value, 8);
  350. if (ACPI_FAILURE(status)) {
  351. return (status);
  352. }
  353. *bus_number = (u16)pci_value;
  354. return (AE_OK);
  355. }