Debian Codegraph @

tests/linux_kernel/helpers/test_pci.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 from pathlib import Path
5
6 from drgn.helpers.linux.pci import (
7 for_each_pci_dev,
8 for_each_pci_root_bus,
9 pci_bus_for_each_child,
10 pci_bus_for_each_dev,
11 pci_bus_name,
12 pci_is_bridge,
13 pci_name,
14 pci_pcie_type,
15 )
16 from tests.linux_kernel import LinuxKernelTestCase
17
18
19 def _pci_root_bus_paths():
20 for path in Path("/sys/class/pci_bus").iterdir():
21 real_device_path = (path / "device").resolve()
22 # For root buses, the device name is "pciDDDD:BB". For other buses, it
23 # is the bridge device name (DDDD:BB:dd.F).
24 if real_device_path.name.startswith("pci"):
25 yield real_device_path
26
27
28 def _pcie_type(config_path) -> int:
29 with open(config_path, "rb") as f:
30 f.seek(0x34)
31 cap_ptr = f.read(1)[0]
32
33 while cap_ptr and cap_ptr < 0xFF:
34 f.seek(cap_ptr)
35 cap_id = f.read(1)[0]
36
37 if cap_id == 0x10:
38 f.seek(cap_ptr + 2)
39 pcie_caps = int.from_bytes(f.read(2), byteorder="little")
40 return (pcie_caps >> 4) & 0x0F
41
42 f.seek(cap_ptr + 1)
43 cap_ptr = f.read(1)[0]
44
45 return None
46
47
48 class TestPci(LinuxKernelTestCase):
49 def test_for_each_pci_dev(self):
50 # This also tests pci_name().
51 self.assertCountEqual(
52 [pci_name(dev) for dev in for_each_pci_dev(self.prog)],
53 [path.name for path in Path("/sys/bus/pci/devices").iterdir()],
54 )
55
56 def test_for_each_pci_root_bus(self):
57 # This also tests pci_bus_name().
58 self.assertCountEqual(
59 [pci_bus_name(bus) for bus in for_each_pci_root_bus(self.prog)],
60 [path.name[3:] for path in _pci_root_bus_paths()],
61 )
62
63 def test_pci_bus_for_each_child(self):
64 bus_path = next(_pci_root_bus_paths())
65 bus_name = bus_path.name[3:]
66 for bus in for_each_pci_root_bus(self.prog):
67 if pci_bus_name(bus) == bus_name:
68 break
69 else:
70 self.fail(f"root bus {bus_path.name} not found")
71
72 self.assertCountEqual(
73 [pci_bus_name(child_bus) for child_bus in pci_bus_for_each_child(bus)],
74 [child_bus_path.name for child_bus_path in bus_path.glob("*/pci_bus/*")],
75 )
76
77 def test_pci_bus_for_each_dev(self):
78 bus_path = next(_pci_root_bus_paths())
79 bus_name = bus_path.name[3:]
80 for bus in for_each_pci_root_bus(self.prog):
81 if pci_bus_name(bus) == bus_name:
82 break
83 else:
84 self.fail(f"root bus {bus_path.name} not found")
85
86 self.assertCountEqual(
87 [pci_name(dev) for dev in pci_bus_for_each_dev(bus)],
88 [dev_path.name for dev_path in bus_path.glob(bus_name + ":*")],
89 )
90
91 def test_pci_is_bridge(self):
92 bridge_name = None
93 non_bridge_name = None
94 for path in Path("/sys/bus/pci/devices").iterdir():
95 if (path / "secondary_bus_number").exists():
96 bridge_name = path.name
97 if non_bridge_name is not None:
98 break
99 else:
100 non_bridge_name = path.name
101 if bridge_name is not None:
102 break
103
104 bridge_dev = None
105 non_bridge_dev = None
106 for dev in for_each_pci_dev(self.prog):
107 name = pci_name(dev)
108 if name == bridge_name:
109 bridge_dev = dev
110 if non_bridge_name is None or non_bridge_dev is not None:
111 break
112 elif name == non_bridge_name:
113 non_bridge_dev = dev
114 if bridge_name is None or bridge_dev is not None:
115 break
116
117 with self.subTest("bridge"):
118 if bridge_name is None:
119 self.skipTest("no bridges found")
120 if bridge_dev is None:
121 self.fail(f"device {bridge_name} not found")
122 self.assertTrue(pci_is_bridge(bridge_dev))
123
124 with self.subTest("non-bridge"):
125 if non_bridge_name is None:
126 self.skipTest("no non-bridges found")
127 if non_bridge_dev is None:
128 self.fail(f"device {non_bridge_name} not found")
129 self.assertFalse(pci_is_bridge(non_bridge_dev))
130
131 def test_pci_pcie_type(self):
132 for path in Path("/sys/bus/pci/devices").iterdir():
133 expected = _pcie_type(path / "config")
134 if expected is not None:
135 break
136 else:
137 self.skipTest("no PCIe devices found")
138
139 for dev in for_each_pci_dev(self.prog):
140 if pci_name(dev) == path.name:
141 break
142 else:
143 self.fail(f"device {path.name} not found")
144
145 self.assertEqual(pci_pcie_type(dev), expected)