1# Copyright (C) 2021-2022 Intel Corporation.
2#
3# SPDX-License-Identifier: BSD-3-Clause
4#
5
6from collections import namedtuple
7from .aml import datatypes, context
8
9class PRTMappingPackage(namedtuple("PRTMappingPackage", ["address", "pin", "source", "source_index"])):
10    def __repr__(self):
11        if isinstance(self.source, context.DeviceDecl):
12            s = self.source.name
13        else:
14            s = str(self.source)
15        return "address=0x{0:08x}, pin=0x{1:02x}, source={2}, source_index=0x{3:08x}".format(
16            self.address, self.pin, s, self.source_index)
17
18def parse_prt_mapping(x):
19    address = x.elements[0].get()
20    pin = x.elements[1].get()
21    source = x.elements[2]
22    if isinstance(source, datatypes.Device):
23        source = source.get_sym()
24    elif isinstance(source, datatypes.Integer):
25        source = source.get()
26    else:
27        source = "unknown"
28    source_index = x.elements[3].get()
29
30    return PRTMappingPackage(address, pin, source, source_index)
31
32def parse_pci_routing(package):
33    """Parse ACPI PCI routing table returned by _PRT control methods."""
34    return list(map(parse_prt_mapping, package.elements))
35