config_tools: acpi_gen: refactor ACPI table generation logic

While functionally correct, the ACPI table (mostly DSDT) generation logic
in asl_gen.py contains multiple occurrences that share the same code
structure as follows:

    cls = <class of the table>
    length = ctypes.sizeof(cls)
    data = bytearray(length)
    res = cls.from_buffer(data)
    <setting multiple fields in res>

To minimize code duplication, this patch refactors the logic by abstracting
the creation of an ACPI table into a method which returns a newly created
object of the given class after setting the specified fields.

Tracked-On: #7947
Signed-off-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Junjie Mao 2022-07-29 23:14:03 +08:00 committed by acrnsi-robot
parent 89d54aa5d1
commit eff353700e

View File

@ -344,82 +344,89 @@ def encode_eisa_id(s):
] ]
return int.from_bytes(bytes(encoded), sys.byteorder) return int.from_bytes(bytes(encoded), sys.byteorder)
def create_object(cls, **kwargs):
length = ctypes.sizeof(cls)
data = bytearray(length)
obj = cls.from_buffer(data)
for key, value in kwargs.items():
setattr(obj, key, value)
return obj
def gen_root_pci_bus(path, prt_packages): def gen_root_pci_bus(path, prt_packages):
resources = [] resources = []
# Bus number # Bus number
cls = rdt.LargeResourceItemWordAddressSpace_factory() word_address_space_cls = rdt.LargeResourceItemWordAddressSpace_factory()
length = ctypes.sizeof(cls) res = create_object(
data = bytearray(length) word_address_space_cls,
res = cls.from_buffer(data) type = 1, # Large type
res.type = 1 # Large type name = rdt.LARGE_RESOURCE_ITEM_WORD_ADDRESS_SPACE,
res.name = rdt.LARGE_RESOURCE_ITEM_WORD_ADDRESS_SPACE length = ctypes.sizeof(word_address_space_cls) - 3,
res.length = length - 3 _TYP = 2, # Bus number range
res._TYP = 2 # Bus number range _DEC = 0, # Positive decoding
res._DEC = 0 # Positive decoding _MIF = 1, # Minimum address fixed
res._MIF = 1 # Minimum address fixed _MAF = 1, # Maximum address fixed
res._MAF = 1 # Maximum address fixed flags = 0,
res.flags = 0 _MAX = 0xff,
res._MAX = 0xff _LEN = 0x100
res._LEN = 0x100 )
resources.append(data) resources.append(res)
# The PCI hole below 4G # The PCI hole below 4G
cls = rdt.LargeResourceItemDWordAddressSpace_factory() dword_address_space_cls = rdt.LargeResourceItemDWordAddressSpace_factory()
length = ctypes.sizeof(cls) res = create_object(
data = bytearray(length) dword_address_space_cls,
res = cls.from_buffer(data) type = 1, # Large type
res.type = 1 # Large type name = rdt.LARGE_RESOURCE_ITEM_ADDRESS_SPACE_RESOURCE,
res.name = rdt.LARGE_RESOURCE_ITEM_ADDRESS_SPACE_RESOURCE length = ctypes.sizeof(dword_address_space_cls) - 3,
res.length = length - 3 _TYP = 0, # Memory range
res._TYP = 0 # Memory range _DEC = 0, # Positive decoding
res._DEC = 0 # Positive decoding _MIF = 1, # Minimum address fixed
res._MIF = 1 # Minimum address fixed _MAF = 1, # Maximum address fixed
res._MAF = 1 # Maximum address fixed flags = 1, # read-write, non-cachable, TypeStatic
res.flags = 1 # read-write, non-cachable, TypeStatic _MIN = 0x80000000,
res._MIN = 0x80000000 _MAX = 0xdfffffff,
res._MAX = 0xdfffffff _LEN = 0x60000000
res._LEN = 0x60000000 )
resources.append(data) resources.append(res)
# The PCI hole above 4G # The PCI hole above 4G
cls = rdt.LargeResourceItemQWordAddressSpace_factory() res = create_object(
length = ctypes.sizeof(cls) dword_address_space_cls,
data = bytearray(length) type = 1, # Large type
res = cls.from_buffer(data) name = rdt.LARGE_RESOURCE_ITEM_ADDRESS_SPACE_RESOURCE,
res.type = 1 # Large type length = ctypes.sizeof(dword_address_space_cls) - 3,
res.name = rdt.LARGE_RESOURCE_ITEM_QWORD_ADDRESS_SPACE _TYP = 0, # Memory range
res.length = length - 3 _DEC = 0, # Positive decoding
res._TYP = 0 # Memory range _MIF = 1, # Minimum address fixed
res._DEC = 0 # Positive decoding _MAF = 1, # Maximum address fixed
res._MIF = 1 # Minimum address fixed flags = 1, # read-write, non-cachable, TypeStatic
res._MAF = 1 # Maximum address fixed _MIN = 0x4000000000,
res.flags = 1 # read-write, non-cachable, TypeStatic _MAX = 0x7fffffffff,
res._MIN = 0x4000000000 _LEN = 0x4000000000
res._MAX = 0x7fffffffff )
res._LEN = 0x4000000000 resources.append(res)
resources.append(data)
cls = rdt.LargeResourceItemDWordAddressSpace_factory() # The PCI hole for I/O ports
length = ctypes.sizeof(cls) res = create_object(
data = bytearray(length) dword_address_space_cls,
res = cls.from_buffer(data) type = 1, # Large type
res.type = 1 # Large type name = rdt.LARGE_RESOURCE_ITEM_ADDRESS_SPACE_RESOURCE,
res.name = rdt.LARGE_RESOURCE_ITEM_ADDRESS_SPACE_RESOURCE length = ctypes.sizeof(dword_address_space_cls) - 3,
res.length = length - 3 _TYP = 1, # I/O range
res._TYP = 1 # Memory range _DEC = 0, # Positive decoding
res._DEC = 0 # Positive decoding _MIF = 1, # Minimum address fixed
res._MIF = 1 # Minimum address fixed _MAF = 1, # Maximum address fixed
res._MAF = 1 # Maximum address fixed flags = 3, # Entire range, TypeStatic
res.flags = 3 # Entire range, TypeStatic _MIN = 0x0,
res._MIN = 0x0 _MAX = 0xffff,
res._MAX = 0xffff _LEN = 0x10000
res._LEN = 0x10000 )
resources.append(data) resources.append(res)
# End tag # End tag
resources.append(bytes([0x79, 0])) resources.append(bytes([0x79, 0]))
resource_buf = bytearray().join(resources) resource_buf = bytearray().join(map(bytearray, resources))
checksum = (256 - (sum(resource_buf) % 256)) % 256 checksum = (256 - (sum(resource_buf) % 256)) % 256
resource_buf[-1] = checksum resource_buf[-1] = checksum
@ -458,33 +465,32 @@ def gen_root_pci_bus(path, prt_packages):
def pnp_uart(path, uid, ddn, port, irq): def pnp_uart(path, uid, ddn, port, irq):
resources = [] resources = []
cls = rdt.SmallResourceItemIOPort res = create_object(
length = ctypes.sizeof(cls) rdt.SmallResourceItemIOPort,
data = bytearray(length) type = 0,
res = cls.from_buffer(data) name = rdt.SMALL_RESOURCE_ITEM_IO_PORT,
res.type = 0 length = ctypes.sizeof(rdt.SmallResourceItemIOPort) - 1,
res.name = rdt.SMALL_RESOURCE_ITEM_IO_PORT _DEC = 1,
res.length = 7 _MIN = port,
res._DEC = 1 _MAX = port,
res._MIN = port _ALN = 1,
res._MAX = port _LEN = 8
res._ALN = 1 )
res._LEN = 8 resources.append(res)
resources.append(data)
cls = rdt.SmallResourceItemIRQ_factory(2) cls = rdt.SmallResourceItemIRQ_factory()
length = ctypes.sizeof(cls) res = create_object(
data = bytearray(length) cls,
res = cls.from_buffer(data) type = 0,
res.type = 0 name = rdt.SMALL_RESOURCE_ITEM_IRQ_FORMAT,
res.name = rdt.SMALL_RESOURCE_ITEM_IRQ_FORMAT length = ctypes.sizeof(cls) - 1,
res.length = 2 _INT = 1 << irq
res._INT = 1 << irq )
resources.append(data) resources.append(res)
resources.append(bytes([0x79, 0])) resources.append(bytes([0x79, 0]))
resource_buf = bytearray().join(resources) resource_buf = bytearray().join(map(bytearray, resources))
checksum = (256 - (sum(resource_buf) % 256)) % 256 checksum = (256 - (sum(resource_buf) % 256)) % 256
resource_buf[-1] = checksum resource_buf[-1] = checksum
uart = builder.DefDevice( uart = builder.DefDevice(
@ -512,33 +518,32 @@ def pnp_uart(path, uid, ddn, port, irq):
def pnp_rtc(path): def pnp_rtc(path):
resources = [] resources = []
cls = rdt.SmallResourceItemIOPort res = create_object(
length = ctypes.sizeof(cls) rdt.SmallResourceItemIOPort,
data = bytearray(length) type = 0,
res = cls.from_buffer(data) name = rdt.SMALL_RESOURCE_ITEM_IO_PORT,
res.type = 0 length = ctypes.sizeof(rdt.SmallResourceItemIOPort) - 1,
res.name = rdt.SMALL_RESOURCE_ITEM_IO_PORT _DEC = 1,
res.length = 7 _MIN = 0x70,
res._DEC = 1 _MAX = 0x70,
res._MIN = 0x70 _ALN = 1,
res._MAX = 0x70 _LEN = 8
res._ALN = 1 )
res._LEN = 8 resources.append(res)
resources.append(data)
cls = rdt.SmallResourceItemIRQ_factory(2) cls = rdt.SmallResourceItemIRQ_factory()
length = ctypes.sizeof(cls) res = create_object(
data = bytearray(length) cls,
res = cls.from_buffer(data) type = 0,
res.type = 0 name = rdt.SMALL_RESOURCE_ITEM_IRQ_FORMAT,
res.name = rdt.SMALL_RESOURCE_ITEM_IRQ_FORMAT length = ctypes.sizeof(cls) - 1,
res.length = 2 _INT = 1 << 8
res._INT = 1 << 8 )
resources.append(data) resources.append(res)
resources.append(bytes([0x79, 0])) resources.append(bytes([0x79, 0]))
resource_buf = bytearray().join(resources) resource_buf = bytearray().join(map(bytearray, resources))
checksum = (256 - (sum(resource_buf) % 256)) % 256 checksum = (256 - (sum(resource_buf) % 256)) % 256
resource_buf[-1] = checksum resource_buf[-1] = checksum
rtc = builder.DefDevice( rtc = builder.DefDevice(