1# Copyright (C) 2021-2022 Intel Corporation.
2#
3# SPDX-License-Identifier: BSD-3-Clause
4#
5
6import ctypes
7
8import inspectorlib.cdata as cdata
9import inspectorlib.unpack as unpack
10
11class TableHeader(cdata.Struct):
12    _pack_ = 1
13    _fields_ = [
14        ('signature', ctypes.c_char * 4),
15        ('length', ctypes.c_uint32),
16        ('revision', ctypes.c_ubyte),
17        ('checksum', ctypes.c_ubyte),
18        ('oemid', ctypes.c_char * 6),
19        ('oemtableid', ctypes.c_char * 8),
20        ('oemrevision', ctypes.c_uint32),
21        ('creatorid', ctypes.c_char * 4),
22        ('creatorrevision', ctypes.c_uint32),
23    ]
24
25ASID_SYSTEM_MEMORY = 0
26ASID_SYSTEM_IO = 1
27ASID_PCI_CFG_SPACE = 2
28ASID_EMBEDDED_CONTROLLER = 3
29ASID_SMBUS = 4
30ASID_PCC = 0xA
31ASID_FFH = 0x7F
32
33def _asid_str(asid):
34    if asid >= 0xC0 and asid <= 0xff:
35        return 'OEM Defined'
36    _asid = {
37        ASID_SYSTEM_MEMORY: 'System Memory',
38        ASID_SYSTEM_IO: 'System IO',
39        ASID_PCI_CFG_SPACE: 'PCI Configuration Space',
40        ASID_EMBEDDED_CONTROLLER: 'Embedded Controller',
41        ASID_SMBUS: 'SMBus',
42        ASID_PCC: 'Platform Communications Channel (PCC)',
43        ASID_FFH: 'Functional Fixed Hardware',
44        }
45    return _asid.get(asid, 'Reserved')
46
47_access_sizes = {
48    0: 'Undefined',
49    1: 'Byte access',
50    2: 'Word access',
51    3: 'Dword access',
52    4: 'Qword access',
53}
54
55class GAS(cdata.Struct):
56    _pack_ = 1
57    _fields_ = [
58        ('address_space_id', ctypes.c_uint8),
59        ('register_bit_width', ctypes.c_uint8),
60        ('register_bit_offset', ctypes.c_uint8),
61        ('access_size', ctypes.c_uint8),
62        ('address', ctypes.c_uint64),
63    ]
64
65    _formats = {
66        'address_space_id' : unpack.format_function("{:#x}", _asid_str),
67        'access_size'      : unpack.format_table("{}", _access_sizes),
68    }
69