xref: /qemu/tests/functional/test_aarch64_hotplug_pci.py (revision 597639c4273d1433b0a47c8533b90ccce29f84e5)
1#!/usr/bin/env python3
2#
3# The test hotplugs a PCI device and checks it on a Linux guest.
4#
5# Copyright (c) 2025 Linaro Ltd.
6#
7# Author:
8#  Gustavo Romero <gustavo.romero@linaro.org>
9#
10# SPDX-License-Identifier: GPL-2.0-or-later
11
12from qemu_test import LinuxKernelTest, Asset, exec_command_and_wait_for_pattern
13from qemu_test import BUILD_DIR
14
15class HotplugPCI(LinuxKernelTest):
16
17    ASSET_KERNEL = Asset(
18        ('https://ftp.debian.org/debian/dists/stable/main/installer-arm64/'
19         '20230607+deb12u11/images/netboot/debian-installer/arm64/linux'),
20         'd92a60392ce1e379ca198a1a820899f8f0d39a62d047c41ab79492f81541a9d9')
21
22    ASSET_INITRD = Asset(
23        ('https://ftp.debian.org/debian/dists/stable/main/installer-arm64/'
24         '20230607+deb12u11/images/netboot/debian-installer/arm64/initrd.gz'),
25         '9f817f76951f3237bca8216bee35267bfb826815687f4b2fcdd5e6c2a917790c')
26
27    def test_hotplug_pci(self):
28
29        self.set_machine('virt')
30
31        self.vm.add_args('-m', '512M',
32                         '-cpu', 'cortex-a57',
33                         '-append',
34                         'console=ttyAMA0,115200 init=/bin/sh',
35                         '-device',
36                         'pcie-root-port,bus=pcie.0,chassis=1,slot=1,id=pcie.1',
37                         '-bios',
38                         self.build_file('pc-bios', 'edk2-aarch64-code.fd'))
39
40        # BusyBox prompt
41        prompt = "~ #"
42        self.launch_kernel(self.ASSET_KERNEL.fetch(),
43                           self.ASSET_INITRD.fetch(),
44                           wait_for=prompt)
45
46        # Check for initial state: 2 network adapters, lo and enp0s1.
47        exec_command_and_wait_for_pattern(self,
48                                          'ls /sys/class/net | wc -l',
49                                          '2')
50
51        # Hotplug one network adapter to the root port, i.e. pcie.1 bus.
52        self.vm.cmd('device_add',
53                    driver='virtio-net-pci',
54                    bus='pcie.1',
55                    addr=0,
56                    id='na')
57        # Wait for the kernel to recognize the new device.
58        self.wait_for_console_pattern('virtio-pci')
59        self.wait_for_console_pattern('virtio_net')
60
61        # Check if there is a new network adapter.
62        exec_command_and_wait_for_pattern(self,
63                                          'ls /sys/class/net | wc -l',
64                                          '3')
65
66        self.vm.cmd('device_del', id='na')
67        exec_command_and_wait_for_pattern(self,
68                                          'ls /sys/class/net | wc -l',
69                                          '2')
70
71if __name__ == '__main__':
72    LinuxKernelTest.main()
73