acrn-hypervisor/misc/config_tools/static_allocators/pio.py
Yang,Yu-chu 1c84d88b4a config-tools: add pio static allocator
This file allocates the resource of pio base.
The available pio base is in ['0x3F8', '0x2F8', '0x3E8', '0x2E8'] and it
cannot be used by native device.

When any of sos legacy vuarts are enabled, assign a pio base to legancy
vuarts' base.

The allocator follows rules:
1. An SOS legacy vuart only support PIO vuart.
2. To assign a pio base for sos legacy vuart 0:
   - If the hv/DEBUG_OPTIONS/SERIAL_CONSOLE is one of
     [ttys0, ttys1, ttys2, ttys3] and it's a pio vuart in the native
     environment, the pio base of SOS legacy vuart 0 would be the same as
     native one.
   - If the hv/DEBUG_OPTIONS/SERIAL_CONSOLE is not one of
     [ttys0, ttys1, ttys2, ttys3], assigns a pio base to SOS legacy vuart 0
     from avilable pio base.
   - If the hv/DEBUG_OPTIONS/SERIAL_CONSOLE is not one of
     [ttys0, ttys1, ttys2, ttys3] but a pio vuart, will assigns a pio
     base to SOS legacy vuart 0 from avilable pio base and raise a
     warning to user.
3. To assign a pio base for sos legacy vuart 1:
   - Assigns a pio base to SOS legacy vuart 1 from avilable pio base.
   - If all the available pio bases list is empty (which means all are
     in used by native), it assigns one of the pio base to SOS legacy
     vuart 1 anyway, but raise a warning to user.
4. Assigned pio bases must be unique.

Tracked-On: #5980
Signed-off-by: Yang,Yu-chu <yu-chu.yang@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
2021-05-07 14:39:08 +08:00

75 lines
3.5 KiB
Python

#!/usr/bin/env python3
#
# Copyright (C) 2021 Intel Corporation. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
import sys, os
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'library'))
import common, lib.lib
VALID_PIO = ['0x3F8', '0x2F8', '0x3E8', '0x2E8']
def alloc_pio(pio_list):
try:
base = pio_list[0]
remove_pio(pio_list, base)
return base
except IndexError as e:
raise lib.error.ResourceError("Cannot allocate a pio base, the available pio base list:{}, {}".format(e, pio_list))
def remove_pio(pio_list, base):
try:
pio_list.remove(base)
except ValueError as e:
raise ValueError("Cannot remove a pio base:{} from the available pio base list:{}, {}". format(base, e, pio_list)) from e
def create_vuart_base_node(etree, vm_id, vuart_id, vuart_base):
allocation_sos_vm_node = common.get_node(f"/acrn-config/vm[@id = '{vm_id}']", etree)
if allocation_sos_vm_node is None:
allocation_sos_vm_node = common.append_node("/acrn-config/vm", None, etree, id = vm_id)
if common.get_node("./vm_type", allocation_sos_vm_node) is None:
common.append_node("./vm_type", "SOS_VM", allocation_sos_vm_node)
if common.get_node(f"./legacy_vuart[@id = '{vuart_id}']", allocation_sos_vm_node) is None:
common.append_node("./legacy_vuart", None, allocation_sos_vm_node, id = vuart_id)
common.append_node(f"./legacy_vuart[@id = '{vuart_id}']/base", vuart_base, allocation_sos_vm_node)
def fn(board_etree, scenario_etree, allocation_etree):
native_ttys = lib.lib.get_native_ttys()
pio_list = [base for base in VALID_PIO if all(native_ttys[tty]['base'] != base for tty in native_ttys.keys())]
# This pio_list is workaround. Since there are whl-ipc-i7 and whl-ipc-i5 which occupy all valid pio ports.
# It would fail to allocate pio base for enabled sos legacy vuart1. In that case, we allow vuart1 take one pio
# base which is used in native.
full = False
if len(pio_list) == 0:
full = True
pio_list = VALID_PIO
vuart_valid = ['ttyS0', 'ttyS1', 'ttyS2', 'ttyS3']
hv_debug_console = lib.lib.parse_hv_console(scenario_etree)
scenario_sos_vm_node = common.get_node("//vm[vm_type = 'SOS_VM']", scenario_etree)
if scenario_sos_vm_node is not None:
vm_id = common.get_node("./@id", scenario_sos_vm_node)
if common.get_node("./legacy_vuart[@id = '0']/base/text()", scenario_sos_vm_node) != "INVALID_COM_BASE":
vuart0_base = ""
if hv_debug_console in vuart_valid and hv_debug_console in native_ttys.keys() and native_ttys[hv_debug_console]['type'] == "portio":
vuart0_base = native_ttys[hv_debug_console]['base']
if vuart0_base in pio_list:
remove_pio(pio_list, vuart0_base)
else:
vuart0_base = alloc_pio(pio_list)
if full:
common.print_yel("All available pio bases are used by native fully. '{}' is taken by sos legacy vuart 0.".format(vuart0_base), warn=True)
create_vuart_base_node(allocation_etree, str(vm_id), "0", vuart0_base)
if common.get_node("./legacy_vuart[@id = '1']/base/text()", scenario_sos_vm_node) != "INVALID_COM_BASE":
vuart1_base = alloc_pio(pio_list)
if full:
common.print_yel("All available pio bases are used by native fully. '{}' is taken by sos legacy vuart 1.".format(vuart1_base), warn=True)
create_vuart_base_node(allocation_etree, str(vm_id), "1", vuart1_base)