1 /*******************************************************************************
3 * Module Name: hwpci - Obtain PCI bus, device, and function numbers
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME ("hwpci")
54 /* PCI configuration space values */
56 #define PCI_CFG_HEADER_TYPE_REG 0x0E
57 #define PCI_CFG_PRIMARY_BUS_NUMBER_REG 0x18
58 #define PCI_CFG_SECONDARY_BUS_NUMBER_REG 0x19
60 /* PCI header values */
62 #define PCI_HEADER_TYPE_MASK 0x7F
63 #define PCI_TYPE_BRIDGE 0x01
64 #define PCI_TYPE_CARDBUS_BRIDGE 0x02
66 typedef struct acpi_pci_device
69 struct acpi_pci_device *Next;
74 /* Local prototypes */
78 ACPI_HANDLE RootPciDevice,
79 ACPI_HANDLE PciRegion,
80 ACPI_PCI_DEVICE **ReturnListHead);
83 AcpiHwProcessPciList (
85 ACPI_PCI_DEVICE *ListHead);
89 ACPI_PCI_DEVICE *ListHead);
92 AcpiHwGetPciDeviceInfo (
94 ACPI_HANDLE PciDevice,
99 /*******************************************************************************
101 * FUNCTION: AcpiHwDerivePciId
103 * PARAMETERS: PciId - Initial values for the PCI ID. May be
104 * modified by this function.
105 * RootPciDevice - A handle to a PCI device object. This
106 * object must be a PCI Root Bridge having a
107 * _HID value of either PNP0A03 or PNP0A08
108 * PciRegion - A handle to a PCI configuration space
109 * Operation Region being initialized
113 * DESCRIPTION: This function derives a full PCI ID for a PCI device,
114 * consisting of a Segment number, Bus number, Device number,
117 * The PCI hardware dynamically configures PCI bus numbers
118 * depending on the bus topology discovered during system
119 * initialization. This function is invoked during configuration
120 * of a PCI_Config Operation Region in order to (possibly) update
121 * the Bus/Device/Function numbers in the PciId with the actual
122 * values as determined by the hardware and operating system
125 * The PciId parameter is initially populated during the Operation
126 * Region initialization. This function is then called, and is
127 * will make any necessary modifications to the Bus, Device, or
128 * Function number PCI ID subfields as appropriate for the
129 * current hardware and OS configuration.
131 * NOTE: Created 08/2010. Replaces the previous OSL AcpiOsDerivePciId
132 * interface since this feature is OS-independent. This module
133 * specifically avoids any use of recursion by building a local
134 * temporary device list.
136 ******************************************************************************/
141 ACPI_HANDLE RootPciDevice,
142 ACPI_HANDLE PciRegion)
145 ACPI_PCI_DEVICE *ListHead = NULL;
148 ACPI_FUNCTION_TRACE (HwDerivePciId);
153 return_ACPI_STATUS (AE_BAD_PARAMETER);
156 /* Build a list of PCI devices, from PciRegion up to RootPciDevice */
158 Status = AcpiHwBuildPciList (RootPciDevice, PciRegion, &ListHead);
159 if (ACPI_SUCCESS (Status))
161 /* Walk the list, updating the PCI device/function/bus numbers */
163 Status = AcpiHwProcessPciList (PciId, ListHead);
165 /* Delete the list */
167 AcpiHwDeletePciList (ListHead);
170 return_ACPI_STATUS (Status);
174 /*******************************************************************************
176 * FUNCTION: AcpiHwBuildPciList
178 * PARAMETERS: RootPciDevice - A handle to a PCI device object. This
179 * object is guaranteed to be a PCI Root
180 * Bridge having a _HID value of either
182 * PciRegion - A handle to the PCI configuration space
184 * ReturnListHead - Where the PCI device list is returned
188 * DESCRIPTION: Builds a list of devices from the input PCI region up to the
189 * Root PCI device for this namespace subtree.
191 ******************************************************************************/
195 ACPI_HANDLE RootPciDevice,
196 ACPI_HANDLE PciRegion,
197 ACPI_PCI_DEVICE **ReturnListHead)
199 ACPI_HANDLE CurrentDevice;
200 ACPI_HANDLE ParentDevice;
202 ACPI_PCI_DEVICE *ListElement;
203 ACPI_PCI_DEVICE *ListHead = NULL;
207 * Ascend namespace branch until the RootPciDevice is reached, building
208 * a list of device nodes. Loop will exit when either the PCI device is
209 * found, or the root of the namespace is reached.
211 CurrentDevice = PciRegion;
214 Status = AcpiGetParent (CurrentDevice, &ParentDevice);
215 if (ACPI_FAILURE (Status))
217 /* Must delete the list before exit */
219 AcpiHwDeletePciList (*ReturnListHead);
223 /* Finished when we reach the PCI root device (PNP0A03 or PNP0A08) */
225 if (ParentDevice == RootPciDevice)
227 *ReturnListHead = ListHead;
231 ListElement = ACPI_ALLOCATE (sizeof (ACPI_PCI_DEVICE));
234 /* Must delete the list before exit */
236 AcpiHwDeletePciList (*ReturnListHead);
237 return (AE_NO_MEMORY);
240 /* Put new element at the head of the list */
242 ListElement->Next = ListHead;
243 ListElement->Device = ParentDevice;
244 ListHead = ListElement;
246 CurrentDevice = ParentDevice;
251 /*******************************************************************************
253 * FUNCTION: AcpiHwProcessPciList
255 * PARAMETERS: PciId - Initial values for the PCI ID. May be
256 * modified by this function.
257 * ListHead - Device list created by
262 * DESCRIPTION: Walk downward through the PCI device list, getting the device
263 * info for each, via the PCI configuration space and updating
264 * the PCI ID as necessary. Deletes the list during traversal.
266 ******************************************************************************/
269 AcpiHwProcessPciList (
271 ACPI_PCI_DEVICE *ListHead)
273 ACPI_STATUS Status = AE_OK;
274 ACPI_PCI_DEVICE *Info;
276 BOOLEAN IsBridge = TRUE;
279 ACPI_FUNCTION_NAME (HwProcessPciList);
282 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
283 "Input PciId: Seg %4.4X Bus %4.4X Dev %4.4X Func %4.4X\n",
284 PciId->Segment, PciId->Bus, PciId->Device, PciId->Function));
286 BusNumber = PciId->Bus;
289 * Descend down the namespace tree, collecting PCI device, function,
290 * and bus numbers. BusNumber is only important for PCI bridges.
291 * Algorithm: As we descend the tree, use the last valid PCI device,
292 * function, and bus numbers that are discovered, and assign them
293 * to the PCI ID for the target device.
298 Status = AcpiHwGetPciDeviceInfo (PciId, Info->Device,
299 &BusNumber, &IsBridge);
300 if (ACPI_FAILURE (Status))
308 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
309 "Output PciId: Seg %4.4X Bus %4.4X Dev %4.4X Func %4.4X "
310 "Status %X BusNumber %X IsBridge %X\n",
311 PciId->Segment, PciId->Bus, PciId->Device, PciId->Function,
312 Status, BusNumber, IsBridge));
318 /*******************************************************************************
320 * FUNCTION: AcpiHwDeletePciList
322 * PARAMETERS: ListHead - Device list created by
327 * DESCRIPTION: Free the entire PCI list.
329 ******************************************************************************/
332 AcpiHwDeletePciList (
333 ACPI_PCI_DEVICE *ListHead)
335 ACPI_PCI_DEVICE *Next;
336 ACPI_PCI_DEVICE *Previous;
343 Next = Previous->Next;
344 ACPI_FREE (Previous);
349 /*******************************************************************************
351 * FUNCTION: AcpiHwGetPciDeviceInfo
353 * PARAMETERS: PciId - Initial values for the PCI ID. May be
354 * modified by this function.
355 * PciDevice - Handle for the PCI device object
356 * BusNumber - Where a PCI bridge bus number is returned
357 * IsBridge - Return value, indicates if this PCI
358 * device is a PCI bridge
362 * DESCRIPTION: Get the device info for a single PCI device object. Get the
363 * _ADR (contains PCI device and function numbers), and for PCI
364 * bridge devices, get the bus number from PCI configuration
367 ******************************************************************************/
370 AcpiHwGetPciDeviceInfo (
372 ACPI_HANDLE PciDevice,
377 ACPI_OBJECT_TYPE ObjectType;
382 /* We only care about objects of type Device */
384 Status = AcpiGetType (PciDevice, &ObjectType);
385 if (ACPI_FAILURE (Status))
390 if (ObjectType != ACPI_TYPE_DEVICE)
395 /* We need an _ADR. Ignore device if not present */
397 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR,
398 PciDevice, &ReturnValue);
399 if (ACPI_FAILURE (Status))
405 * From _ADR, get the PCI Device and Function and
408 PciId->Device = ACPI_HIWORD (ACPI_LODWORD (ReturnValue));
409 PciId->Function = ACPI_LOWORD (ACPI_LODWORD (ReturnValue));
412 * If the previous device was a bridge, use the previous
417 PciId->Bus = *BusNumber;
421 * Get the bus numbers from PCI Config space:
423 * First, get the PCI HeaderType
426 Status = AcpiOsReadPciConfiguration (PciId,
427 PCI_CFG_HEADER_TYPE_REG, &PciValue, 8);
428 if (ACPI_FAILURE (Status))
433 /* We only care about bridges (1=PciBridge, 2=CardBusBridge) */
435 PciValue &= PCI_HEADER_TYPE_MASK;
437 if ((PciValue != PCI_TYPE_BRIDGE) &&
438 (PciValue != PCI_TYPE_CARDBUS_BRIDGE))
443 /* Bridge: Get the Primary BusNumber */
445 Status = AcpiOsReadPciConfiguration (PciId,
446 PCI_CFG_PRIMARY_BUS_NUMBER_REG, &PciValue, 8);
447 if (ACPI_FAILURE (Status))
453 PciId->Bus = (UINT16) PciValue;
455 /* Bridge: Get the Secondary BusNumber */
457 Status = AcpiOsReadPciConfiguration (PciId,
458 PCI_CFG_SECONDARY_BUS_NUMBER_REG, &PciValue, 8);
459 if (ACPI_FAILURE (Status))
464 *BusNumber = (UINT16) PciValue;