1# Copyright (C) 2019-2022 Intel Corporation.
2#
3# SPDX-License-Identifier: BSD-3-Clause
4#
5
6import collections
7import board_cfg_lib
8
9PCI_HEADER = r"""
10#ifndef PCI_DEVICES_H_
11#define PCI_DEVICES_H_"""
12PCI_END_HEADER = r"""
13#endif /* PCI_DEVICES_H_ */"""
14
15def write_pbdf(i_cnt, bdf, bar_attr, config):
16    """
17    Parser and generate pbdf
18    :param i_cnt: the number of pci devices have the same PCI sub class name
19    :param bdf: it is a string what contains BDF
20    :param bar_attr: it is a class, contains PIC bar attribute
21    :param config: it is a file pointer of pci information for writing to
22    """
23    tmp_sub_name = board_cfg_lib.get_sub_pci_name(i_cnt, bar_attr)
24    board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic[bdf].name_w_i_cnt = tmp_sub_name
25
26    bus = int(bdf.split(':')[0], 16)
27    dev = int(bdf.split(':')[1].split('.')[0], 16)
28    fun = int(bdf.split('.')[1], 16)
29    print("#define %-32s" % tmp_sub_name, end="", file=config)
30    print("        .pbdf.bits = {{.b = 0x{:02X}U, .d = 0x{:02X}U, .f = 0x{:02X}U}}".format(
31        bus, dev, fun), file=config)
32
33
34def generate_file(config):
35    """
36    Get PCI device and generate pci_devices.h
37    :param config: it is a file pointer of pci information for writing to
38    """
39    # write the license into pci
40    print("{0}".format(board_cfg_lib.HEADER_LICENSE), file=config)
41
42    # add bios and base board info
43    board_cfg_lib.handle_bios_info(config)
44
45    # write the header into pci
46    print("{0}".format(PCI_HEADER), file=config)
47
48    board_cfg_lib.parser_pci()
49
50    compared_bdf = []
51    for cnt_sub_name in board_cfg_lib.SUB_NAME_COUNT.keys():
52        i_cnt = 0
53        for bdf, bar_attr in board_cfg_lib.PCI_DEV_BAR_DESC.pci_dev_dic.items():
54            if cnt_sub_name == bar_attr.name and bdf not in compared_bdf:
55                compared_bdf.append(bdf)
56            else:
57                continue
58
59            print("",file=config)
60            write_pbdf(i_cnt, bdf, bar_attr, config)
61
62            i_cnt += 1
63
64    # write the end to the pci devices
65    print("{0}".format(PCI_END_HEADER), file=config)
66