1#!/usr/bin/env python3
2#
3# Copyright (C) 2021-2022 Intel Corporation.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7import logging
8import acrn_config_utilities
9from acrn_config_utilities import get_node
10
11def powerof2_roundup(value):
12    return 0 if value == 0 else (1 << (value - 1).bit_length())
13
14# Make sure all PT IRQs work w/ interrupt remapping or post interrupt
15def create_max_ir_entries(scenario_etree, allocation_etree):
16    pt_irq_entries = get_node(f"//MAX_PT_IRQ_ENTRIES/text()", scenario_etree)
17    if (pt_irq_entries is not None) and (int(pt_irq_entries) > 256):
18        ir_entries = powerof2_roundup(int(pt_irq_entries))
19    else:
20        ir_entries = 256
21
22    acrn_config_utilities.append_node("/acrn-config/hv/MAX_IR_ENTRIES", ir_entries, allocation_etree)
23
24def fn(board_etree, scenario_etree, allocation_etree):
25    pci_bus_nums =  board_etree.xpath("//bus[@type='pci']/@address")
26    calc_pci_bus_nums = (max(map(lambda x: int(x, 16), pci_bus_nums)) + 1)
27    user_def_pci_bus_nums = get_node(f"//MAX_PCI_BUS_NUM/text()", scenario_etree)
28    if user_def_pci_bus_nums == '0':
29        acrn_config_utilities.append_node("/acrn-config/platform/MAX_PCI_BUS_NUM", hex(calc_pci_bus_nums), allocation_etree)
30    else:
31        if calc_pci_bus_nums > int(user_def_pci_bus_nums):
32            logging.error(f"MAX_PCI_BUS_NUM should be greater than {calc_pci_bus_nums}")
33            sys.exit(1)
34        else:
35            acrn_config_utilities.append_node("/acrn-config/platform/MAX_PCI_BUS_NUM", hex(int(user_def_pci_bus_nums)), allocation_etree)
36    create_max_ir_entries(scenario_etree, allocation_etree)
37