1#!/usr/bin/env python3
2#
3# Copyright (C) 2021-2022 Intel Corporation.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import sys, os
9sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'library'))
10import acrn_config_utilities, board_cfg_lib
11from acrn_config_utilities import get_node
12
13def sos_cpu_affinity(etree):
14    if get_node("//vm[load_order = 'SERVICE_VM']", etree) is None:
15        return None
16
17    if get_node("//vm[load_order = 'SERVICE_VM' and count(cpu_affinity//pcpu_id)]", etree) is not None:
18        return None
19
20    sos_extend_all_cpus = board_cfg_lib.get_processor_info()
21    pre_all_cpus = etree.xpath("//vm[load_order = 'PRE_LAUNCHED_VM']/cpu_affinity//pcpu_id/text()")
22
23    cpus_for_sos = list(set(sos_extend_all_cpus) - set(pre_all_cpus))
24    return sorted(cpus_for_sos)
25
26def fn(board_etree, scenario_etree, allocation_etree):
27    cpus_for_sos = sos_cpu_affinity(scenario_etree)
28    if cpus_for_sos:
29        if get_node("//vm[load_order = 'SERVICE_VM']", scenario_etree) is not None:
30            vm_id = get_node("//vm[load_order = 'SERVICE_VM']/@id", scenario_etree)
31            allocation_service_vm_node = get_node(f"/acrn-config/vm[@id='{vm_id}']", allocation_etree)
32            if allocation_service_vm_node is None:
33                allocation_service_vm_node = acrn_config_utilities.append_node("/acrn-config/vm", None, allocation_etree, id = vm_id)
34            if get_node("./load_order", allocation_service_vm_node) is None:
35                acrn_config_utilities.append_node("./load_order", "SERVICE_VM", allocation_service_vm_node)
36        for pcpu_id in sorted([int(x) for x in cpus_for_sos]):
37            acrn_config_utilities.append_node("./cpu_affinity/pcpu_id", str(pcpu_id), allocation_service_vm_node)
38