amd-debug-tools 0.2.20__tar.gz → 0.2.22__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. {amd_debug_tools-0.2.20/src/amd_debug_tools.egg-info → amd_debug_tools-0.2.22}/PKG-INFO +1 -1
  2. amd_debug_tools-0.2.22/src/amd_debug/ivrs.py +485 -0
  3. amd_debug_tools-0.2.22/src/amd_debug/ivrs_fixtures.py +297 -0
  4. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/prerequisites.py +55 -1
  5. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/sleep_report.py +9 -4
  6. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/templates/html +1 -1
  7. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/templates/stdout +1 -1
  8. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/ttm.py +79 -50
  9. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22/src/amd_debug_tools.egg-info}/PKG-INFO +1 -1
  10. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug_tools.egg-info/SOURCES.txt +3 -0
  11. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug_tools.egg-info/scm_file_list.json +53 -50
  12. amd_debug_tools-0.2.22/src/amd_debug_tools.egg-info/scm_version.json +8 -0
  13. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug_tools.egg-info/top_level.txt +1 -0
  14. amd_debug_tools-0.2.22/src/test_ivrs.py +343 -0
  15. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_prerequisites.py +261 -19
  16. amd_debug_tools-0.2.20/src/amd_debug_tools.egg-info/scm_version.json +0 -8
  17. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/.github/workflows/ci.yml +0 -0
  18. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/.github/workflows/mirror.yml +0 -0
  19. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/.gitignore +0 -0
  20. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/.pre-commit-config.yaml +0 -0
  21. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/.vscode/settings.json +0 -0
  22. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/LICENSE +0 -0
  23. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/Makefile +0 -0
  24. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/README.md +0 -0
  25. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/amd_bios.py +0 -0
  26. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/amd_pstate.py +0 -0
  27. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/amd_s2idle.py +0 -0
  28. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/amd_ttm.py +0 -0
  29. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/docs/amd-bios.md +0 -0
  30. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/docs/amd-pstate.md +0 -0
  31. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/docs/amd-s2idle.md +0 -0
  32. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/docs/amd-ttm.md +0 -0
  33. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/docs/in-tree.md +0 -0
  34. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/install_deps.py +0 -0
  35. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/psr.py +0 -0
  36. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/pyproject.toml +0 -0
  37. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/setup.cfg +0 -0
  38. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/__init__.py +0 -0
  39. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/acpi.py +0 -0
  40. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/bash/amd-s2idle +0 -0
  41. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/battery.py +0 -0
  42. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/bios.py +0 -0
  43. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/common.py +0 -0
  44. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/database.py +0 -0
  45. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/display.py +0 -0
  46. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/failures.py +0 -0
  47. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/installer.py +0 -0
  48. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/kernel.py +0 -0
  49. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/power_rails.py +0 -0
  50. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/pstate.py +0 -0
  51. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/s2idle-hook +0 -0
  52. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/s2idle.py +0 -0
  53. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/templates/md +0 -0
  54. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/templates/txt +0 -0
  55. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/validator.py +0 -0
  56. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug/wake.py +0 -0
  57. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug_tools.egg-info/dependency_links.txt +0 -0
  58. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug_tools.egg-info/entry_points.txt +0 -0
  59. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/amd_debug_tools.egg-info/requires.txt +0 -0
  60. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/launcher.py +0 -0
  61. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_acpi.py +0 -0
  62. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_batteries.py +0 -0
  63. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_bios.py +0 -0
  64. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_common.py +0 -0
  65. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_database.py +0 -0
  66. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_display.py +0 -0
  67. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_failures.py +0 -0
  68. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_installer.py +0 -0
  69. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_kernel.py +0 -0
  70. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_launcher.py +0 -0
  71. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_power_rails.py +0 -0
  72. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_pstate.py +0 -0
  73. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_s2idle.py +0 -0
  74. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_sleep_report.py +0 -0
  75. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_ttm.py +0 -0
  76. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_validator.py +0 -0
  77. {amd_debug_tools-0.2.20 → amd_debug_tools-0.2.22}/src/test_wake.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: amd-debug-tools
3
- Version: 0.2.20
3
+ Version: 0.2.22
4
4
  Summary: debug tools for AMD systems
5
5
  Author-email: Mario Limonciello <superm1@kernel.org>
6
6
  License-Expression: MIT
@@ -0,0 +1,485 @@
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ """
4
+ IVRS (IOMMU Virtualization Reporting Structure) parser.
5
+
6
+ Parses the AMD IVRS ACPI table to extract device and memory mapping entries.
7
+ Used to verify IOMMU configuration for s2idle wake support.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import struct
13
+ from abc import ABC, abstractmethod
14
+
15
+
16
+ class IvrsSubtable(ABC):
17
+ """Base class for IVRS subtable entries.
18
+
19
+ IVRS subtable header format:
20
+ type (u8) at offset 0
21
+ flags (u8) at offset 1
22
+ length (u16 LE) at offset 2
23
+ """
24
+
25
+ def __init__(self, data: bytes, offset: int):
26
+ self.data = data
27
+ self.offset = offset
28
+ # IVRS subtables use u16 length at offset 2
29
+ self.length = struct.unpack_from("<H", data, offset + 2)[0]
30
+ self._validate()
31
+
32
+ def _validate(self):
33
+ """Check that the subtable is at least large enough for the header."""
34
+ if self.length < 4:
35
+ raise ValueError(f"Invalid IVRS subtable length: {self.length}")
36
+ if self.offset + self.length > len(self.data):
37
+ raise ValueError(
38
+ f"IVRS subtable at offset {self.offset} extends beyond table "
39
+ f"(length={self.length}, table_size={len(self.data)})"
40
+ )
41
+
42
+ @property
43
+ @abstractmethod
44
+ def subtable_type(self) -> int:
45
+ """Return the subtable type byte."""
46
+
47
+ @property
48
+ def raw(self) -> bytes:
49
+ """Return the raw subtable bytes."""
50
+ return self.data[self.offset : self.offset + self.length]
51
+
52
+
53
+ class IvhdEntry(IvrsSubtable):
54
+ """IVHD (IOMMU Hardware Definition) subtable."""
55
+
56
+ # Device entry types
57
+ TYPE_ENDPOINT = 0x00
58
+ TYPE_IOPORT = 0x01
59
+ TYPE_PCIE_ENDPOINT = 0x02
60
+ TYPE_PCIE_COMPAT = 0x03
61
+ TYPE_LEGACY_PCI = 0x04
62
+ TYPE_MSI_HOTPLUG = 0x06
63
+ TYPE_HID_DEVICE = 0xF0
64
+
65
+ # IVHD block header lengths by type.
66
+ HEADER_SIZES = {
67
+ 0x10: 24,
68
+ 0x11: 40,
69
+ 0x40: 40,
70
+ }
71
+
72
+ def __init__(self, data: bytes, offset: int):
73
+ super().__init__(data, offset)
74
+ self.type = data[offset]
75
+ self.header_size = self.HEADER_SIZES.get(self.type)
76
+ if self.header_size is None:
77
+ raise ValueError(f"Unsupported IVHD type 0x{self.type:02x}")
78
+ if self.length < self.header_size:
79
+ raise ValueError(
80
+ f"IVHD too short for type 0x{self.type:02x}: {self.length} bytes"
81
+ )
82
+
83
+ # IVHD PCI segment is at offset +16 for types 10h/11h/40h.
84
+ self.iommu_segment = struct.unpack_from("<H", data, offset + 16)[0]
85
+ self.flags = data[offset + 1] if self.length > 1 else 0
86
+
87
+ # Parse device entries
88
+ self.device_entries = []
89
+ dev_off = offset + self.header_size
90
+ while dev_off + 4 <= offset + self.length:
91
+ entry_length = IvhdDeviceEntry.get_entry_size(
92
+ data, dev_off, offset + self.length
93
+ )
94
+ if entry_length is None or entry_length < 4:
95
+ break
96
+ if dev_off + entry_length > offset + self.length:
97
+ break
98
+ device_entry = IvhdDeviceEntry(data, dev_off, entry_length)
99
+ self.device_entries.append(device_entry)
100
+ dev_off += entry_length
101
+
102
+ @property
103
+ def subtable_type(self) -> int:
104
+ return self.type
105
+
106
+ def find_hid_device(self, hid: str) -> "IvhdDeviceEntry | None":
107
+ """Find a device entry with the given HID string."""
108
+ for entry in self.device_entries:
109
+ if entry.type == self.TYPE_HID_DEVICE and entry.hid == hid:
110
+ return entry
111
+ return None
112
+
113
+ @property
114
+ def device_ids(self) -> set[int]:
115
+ """Return all device IDs defined in this IVHD."""
116
+ return {entry.device_id for entry in self.device_entries}
117
+
118
+
119
+ class IvhdDeviceEntry:
120
+ """A device entry within an IVHD subtable."""
121
+
122
+ TYPE_HID_DEVICE = 0xF0
123
+
124
+ def __init__(self, data: bytes, offset: int, entry_length: int | None = None):
125
+ self.data = data
126
+ self.offset = offset
127
+ self.type = data[offset]
128
+ self.device_id = struct.unpack_from("<H", data, offset + 1)[0]
129
+ self.flags = data[offset + 3] if (offset + 4) <= len(data) else 0
130
+
131
+ if self.type == self.TYPE_HID_DEVICE:
132
+ self._parse_hid_device(entry_length)
133
+ else:
134
+ self.hid = None
135
+ self._length = entry_length or 4
136
+
137
+ @classmethod
138
+ def get_entry_size(
139
+ cls, data: bytes, offset: int, ivhd_end: int | None = None
140
+ ) -> int | None:
141
+ """Return the IVHD device-entry length from its type and payload.
142
+
143
+ Entry-length rules are defined by the type high bits:
144
+ - 00h-3Fh: 4-byte fixed entries
145
+ - 40h-7Fh: 8-byte fixed entries
146
+ - 80h-FFh: variable-length entries
147
+ """
148
+ entry_type = data[offset]
149
+ if entry_type <= 0x3F:
150
+ return 4
151
+ if entry_type <= 0x7F:
152
+ return 8
153
+
154
+ if entry_type == cls.TYPE_HID_DEVICE:
155
+ # F0h entry has a 22-byte fixed prefix plus UID bytes.
156
+ if offset + 22 > len(data):
157
+ return None
158
+ uid_len = data[offset + 21]
159
+ total_len = 22 + uid_len
160
+ if ivhd_end is not None and (offset + total_len > ivhd_end):
161
+ return None
162
+ return total_len
163
+
164
+ # Unknown variable-length entry.
165
+ return None
166
+
167
+ def _parse_hid_device(self, entry_length: int | None):
168
+ """Parse type 0xF0 ACPI HID device entry."""
169
+ # Format: type(1) + devid(2) + flags(1) + ACPI HID(8) + CID(8) +
170
+ # UID Format(1) + UID Length(1) + UID(variable)
171
+ if entry_length is None:
172
+ entry_length = self.get_entry_size(self.data, self.offset)
173
+ if entry_length is None:
174
+ self.hid = None
175
+ self._length = 4
176
+ return
177
+
178
+ self._length = entry_length
179
+ # Skip to ACPI HID (offset 4 from entry start)
180
+ hid_start = self.offset + 4
181
+ if hid_start + 8 <= len(self.data):
182
+ self.hid = (
183
+ self.data[hid_start : hid_start + 8]
184
+ .rstrip(b"\x00")
185
+ .decode("ascii", errors="replace")
186
+ )
187
+ else:
188
+ self.hid = None
189
+
190
+ @property
191
+ def length(self) -> int:
192
+ return self._length
193
+
194
+
195
+ class IvmdMapping(IvrsSubtable):
196
+ """IVMD (IOMMU Memory Definition) subtable.
197
+
198
+ IVMD subtable format:
199
+ type (u8) + flags (u8) + length (u16) + device_id (u16) +
200
+ auxiliary_data (u16) + reserved (u64) + start_address (u64) +
201
+ memory_length (u64)
202
+
203
+ IVMD subtypes:
204
+ 0x20 = Unity map for ALL devices (all-devices mapping)
205
+ 0x21 = Unity map for a single device (matching device_id)
206
+ 0x22 = Exclusion range for a single device
207
+ """
208
+
209
+ FLAG_UNITY_MAP = 0x01
210
+ FLAG_READABLE = 0x02
211
+ FLAG_WRITEABLE = 0x04
212
+ FLAG_EXCLUSION_RANGE = 0x08
213
+
214
+ # Subtable types
215
+ TYPE_ALL_DEVICES = 0x20
216
+ TYPE_SINGLE_DEVICE = 0x21
217
+ TYPE_EXCLUSION_RANGE = 0x22
218
+
219
+ def __init__(self, data: bytes, offset: int):
220
+ super().__init__(data, offset)
221
+ # IVMD is 32 bytes:
222
+ # type(1) + flags(1) + length(2) + device_id(2) + auxiliary(2) +
223
+ # reserved(8) + start_addr(8) + memory_length(8) = 32 bytes
224
+ if self.length < 32:
225
+ raise ValueError(f"IVMD too short: {self.length} bytes (minimum 32)")
226
+
227
+ self.flags = data[offset + 1]
228
+ self.device_id = struct.unpack_from("<H", data, offset + 4)[0]
229
+ self.auxiliary_data = struct.unpack_from("<H", data, offset + 6)[0]
230
+ self.starting_device_id = self.device_id
231
+ self.ending_device_id = self.auxiliary_data
232
+ self.start_address = struct.unpack_from("<Q", data, offset + 16)[0]
233
+ self.memory_length = struct.unpack_from("<Q", data, offset + 24)[0]
234
+
235
+ @property
236
+ def is_unity_map(self) -> bool:
237
+ """Return True if this is a unity-mapped region."""
238
+ return bool(self.flags & self.FLAG_UNITY_MAP)
239
+
240
+ @property
241
+ def is_exclusion_range(self) -> bool:
242
+ """Return True if this is an exclusion range."""
243
+ return bool(self.flags & self.FLAG_EXCLUSION_RANGE)
244
+
245
+ @property
246
+ def has_valid_mapping(self) -> bool:
247
+ """Return True if this entry has a non-zero memory range."""
248
+ return self.memory_length > 0
249
+
250
+ @property
251
+ def subtype(self) -> int:
252
+ """Return the IVMD subtype (0x20, 0x21, or 0x22)."""
253
+ return self.data[self.offset]
254
+
255
+ @property
256
+ def subtable_type(self) -> int:
257
+ """Return the IVMD subtype."""
258
+ return self.subtype
259
+
260
+ def covers_device(self, device_id: int) -> bool:
261
+ """Check if this IVMD entry covers the given device ID.
262
+
263
+ - Type 0x20: All-devices mapping, covers all devices regardless of ID.
264
+ - Type 0x21: Single-device mapping, match by exact device ID.
265
+ - Type 0x22: Exclusion range, match by starting device ID.
266
+ """
267
+ if self.subtype == self.TYPE_ALL_DEVICES:
268
+ return self.has_valid_mapping
269
+ if self.subtype == self.TYPE_SINGLE_DEVICE:
270
+ return self.device_id == device_id
271
+ # Type 0x22 applies to a DeviceID range [start, end], inclusive.
272
+ return self.starting_device_id <= device_id <= self.ending_device_id
273
+
274
+ def covers_device_id(self, device_id: int) -> bool:
275
+ """Check if this mapping is valid AND covers the device.
276
+
277
+ A valid covering mapping must have:
278
+ 1. The appropriate flag set (unity-map for 0x20/0x21, exclusion-range for 0x22)
279
+ 2. A non-zero memory range
280
+ 3. The device must be covered by this entry's scope
281
+ """
282
+ if not self.has_valid_mapping:
283
+ return False
284
+
285
+ if self.subtype == self.TYPE_ALL_DEVICES:
286
+ return self.is_unity_map
287
+ if not self.covers_device(device_id):
288
+ return False
289
+ if self.subtype == self.TYPE_SINGLE_DEVICE:
290
+ return self.is_unity_map
291
+ return self.is_exclusion_range
292
+
293
+
294
+ class IvrsParser:
295
+ """Parse an IVRS (IOMMU Virtualization Reporting Structure) ACPI table.
296
+
297
+ The IVRS table contains:
298
+ - Main header (48 bytes) with unitCapability at offset 36
299
+ - Subtables starting at offset 48
300
+
301
+ Subtable types:
302
+ 0x10 = IVHD (IOMMU Hardware Definition) - contains device entries
303
+ 0x11 = IVHD v2 (with additional fields)
304
+ 0x20 = IVMD (IOMMU Memory Definition) - all-devices unity map
305
+ 0x21 = IVMD - single-device unity map
306
+ 0x22 = IVMD - exclusion range
307
+ 0x40 = IVHD Mixed Format
308
+ """
309
+
310
+ # IVRS main table fields
311
+ IVRS_HEADER_SIZE = 48
312
+ UNIT_CAPABILITY_OFFSET = 36 # Virtualization Info at offset 36
313
+ DMA_REMAP_BIT = 0x2
314
+
315
+ # Subtable types
316
+ TYPE_IVHD = 0x10
317
+ TYPE_IVHD_V2 = 0x11 # IVHD v2 with additional fields
318
+ TYPE_IVHD_MIXED = 0x40
319
+ TYPE_IVMD_ALL_DEVICES = 0x20
320
+ TYPE_IVMD_SINGLE_DEVICE = 0x21
321
+ TYPE_IVMD_EXCLUSION_RANGE = 0x22
322
+
323
+ def __init__(self, data: bytes):
324
+ if len(data) < self.IVRS_HEADER_SIZE:
325
+ raise ValueError(
326
+ f"IVRS table too small: {len(data)} bytes "
327
+ f"(minimum {self.IVRS_HEADER_SIZE})"
328
+ )
329
+
330
+ self.data = data
331
+ self.virt_info = struct.unpack_from("I", data, self.UNIT_CAPABILITY_OFFSET)[0]
332
+ self.has_dma_remap = (self.virt_info & self.DMA_REMAP_BIT) != 0
333
+
334
+ # Parse subtables starting at offset 48
335
+ self.subtables: list[IvrsSubtable] = []
336
+ self._parse_subtables()
337
+
338
+ @property
339
+ def ivhd_entries(self) -> list[IvhdEntry]:
340
+ """Return all IVHD subtables."""
341
+ return [st for st in self.subtables if isinstance(st, IvhdEntry)]
342
+
343
+ @property
344
+ def ivmd_mappings(self) -> list[IvmdMapping]:
345
+ """Return all IVMD subtables."""
346
+ return [st for st in self.subtables if isinstance(st, IvmdMapping)]
347
+
348
+ def _parse_subtables(self):
349
+ """Parse all subtables starting at offset 48."""
350
+ off = 48
351
+ while off + 4 <= len(self.data):
352
+ length = struct.unpack_from("<H", self.data, off + 2)[0]
353
+ if length == 0:
354
+ break
355
+ if off + length > len(self.data):
356
+ break
357
+ if length < 4:
358
+ break
359
+
360
+ sub_type = self.data[off]
361
+ try:
362
+ if sub_type in (
363
+ self.TYPE_IVHD,
364
+ self.TYPE_IVHD_V2,
365
+ self.TYPE_IVHD_MIXED,
366
+ ):
367
+ subtable = IvhdEntry(self.data, off)
368
+ self.subtables.append(subtable)
369
+ elif sub_type in (
370
+ self.TYPE_IVMD_ALL_DEVICES,
371
+ self.TYPE_IVMD_SINGLE_DEVICE,
372
+ self.TYPE_IVMD_EXCLUSION_RANGE,
373
+ ):
374
+ subtable = IvmdMapping(self.data, off)
375
+ self.subtables.append(subtable)
376
+ else:
377
+ # Skip unknown subtable types
378
+ pass
379
+ except ValueError:
380
+ # Malformed subtable, skip it
381
+ pass
382
+ off += length
383
+
384
+ def find_msft0201_device_id(self) -> int | None:
385
+ """Find the device ID for MSFT0201 from IVHD HID device entries.
386
+
387
+ Returns None if MSFT0201 is not found in any IVHD subtable.
388
+ """
389
+ for ivhd in self.ivhd_entries:
390
+ device = ivhd.find_hid_device("MSFT0201")
391
+ if device:
392
+ return device.device_id
393
+ return None
394
+
395
+ def has_msft0201_in_acpi(self) -> bool:
396
+ """Check if MSFT0201 ACPI HID exists in the IVRS table anywhere."""
397
+ return b"MSFT0201" in self.data
398
+
399
+ def has_valid_msft0201_mapping(self, msft_device_id: int | None) -> bool:
400
+ """Check if MSFT0201 has a valid memory mapping in IVMD entries.
401
+
402
+ A valid mapping requires:
403
+ - Type 0x20 (all-devices): unity_map flag set
404
+ - Type 0x21 (single-device): unity_map flag AND matching device_id
405
+ - Type 0x22 (exclusion range): exclusion_range flag AND matching device_id
406
+
407
+ All valid mappings must have a non-zero memory range.
408
+ """
409
+ if msft_device_id is None:
410
+ return False
411
+ for ivmd in self.ivmd_mappings:
412
+ if ivmd.covers_device_id(msft_device_id):
413
+ return True
414
+ return False
415
+
416
+
417
+ def main():
418
+ """CLI entry point for IVRS table analysis."""
419
+ import argparse
420
+
421
+ parser = argparse.ArgumentParser(description="Parse and analyze IVRS ACPI tables")
422
+ parser.add_argument("ivrs_file", help="Path to IVRS ACPI table binary")
423
+ parser.add_argument(
424
+ "--ssdt",
425
+ nargs="*",
426
+ help="Optional paths to SSDT tables for ACPI device lookup",
427
+ )
428
+ args = parser.parse_args()
429
+
430
+ with open(args.ivrs_file, "rb") as f:
431
+ data = f.read()
432
+
433
+ try:
434
+ ivrs = IvrsParser(data)
435
+ except ValueError as e:
436
+ print(f"Error parsing IVRS table: {e}")
437
+ return 1
438
+
439
+ print("IVRS Table Analysis")
440
+ print("===================")
441
+ print(f"Table size: {len(data)} bytes")
442
+ print(f"Virtualization info: 0x{ivrs.virt_info:08x}")
443
+ print(f" DMA Remap enabled: {ivrs.has_dma_remap}")
444
+ print()
445
+
446
+ print(f"Subtables: {len(ivrs.subtables)}")
447
+ for st in ivrs.subtables:
448
+ print(
449
+ f" Type=0x{st.subtable_type:02x}, offset={st.offset}, length={st.length}"
450
+ )
451
+
452
+ print()
453
+ if ivrs.ivhd_entries:
454
+ print(f"IVHD entries: {len(ivrs.ivhd_entries)}")
455
+ for i, ivhd in enumerate(ivrs.ivhd_entries):
456
+ print(f" IVHD #{i}: {len(ivhd.device_entries)} device entries")
457
+ for dev in ivhd.device_entries:
458
+ if dev.hid:
459
+ print(f" HID Device: id=0x{dev.device_id:04x}, hid={dev.hid}")
460
+
461
+ if ivrs.ivmd_mappings:
462
+ print()
463
+ print(f"IVMD mappings: {len(ivrs.ivmd_mappings)}")
464
+ for ivmd in ivrs.ivmd_mappings:
465
+ print(f" Type=0x{ivmd.subtype:02x}, devid=0x{ivmd.starting_device_id:04x}")
466
+ print(
467
+ f" flags=unity={ivmd.is_unity_map}, excl={ivmd.is_exclusion_range}"
468
+ )
469
+ print(f" memory_length=0x{ivmd.memory_length:x}")
470
+
471
+ print()
472
+ print("MSFT0201 check:")
473
+ msft_devid = ivrs.find_msft0201_device_id()
474
+ if msft_devid:
475
+ print(f" Device ID from IVHD: 0x{msft_devid:04x}")
476
+ print(f" Valid mapping: {ivrs.has_valid_msft0201_mapping(msft_devid)}")
477
+ else:
478
+ print(" Not found in IVHD device entries")
479
+ if b"MSFT0201" in data:
480
+ print(" But MSFT0201 HID string found in table")
481
+ return 0
482
+
483
+
484
+ if __name__ == "__main__":
485
+ main()