hwpci.c 13 KB

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