xref: /qemu/tests/functional/test_aarch64_smmu.py (revision a3d40b5effafdd299d1850f0c9956f60199b5b56)
1#!/usr/bin/env python3
2#
3# SPDX-License-Identifier: GPL-2.0-or-later
4#
5# SMMUv3 Functional tests
6#
7# Copyright (c) 2021 Red Hat, Inc.
8#
9# Author:
10#  Eric Auger <eric.auger@redhat.com>
11#
12# This work is licensed under the terms of the GNU GPL, version 2 or
13# later.  See the COPYING file in the top-level directory.
14
15import os
16import time
17
18from qemu_test import LinuxKernelTest, Asset, exec_command_and_wait_for_pattern
19from qemu_test import BUILD_DIR
20from qemu.utils import kvm_available
21
22
23class SMMU(LinuxKernelTest):
24
25    default_kernel_params = ('earlyprintk=pl011,0x9000000 no_timer_check '
26                             'printk.time=1 rd_NO_PLYMOUTH net.ifnames=0 '
27                             'console=ttyAMA0 rd.rescue')
28    IOMMU_ADDON = ',iommu_platform=on,disable-modern=off,disable-legacy=on'
29    kernel_path = None
30    initrd_path = None
31    kernel_params = None
32
33    GUEST_PORT = 8080
34
35    def set_up_boot(self, path):
36        self.vm.add_args('-device', 'virtio-blk-pci,bus=pcie.0,' +
37                         'drive=drv0,id=virtio-disk0,bootindex=1,'
38                         'werror=stop,rerror=stop' + self.IOMMU_ADDON)
39        self.vm.add_args('-drive',
40                f'file={path},if=none,cache=writethrough,id=drv0,snapshot=on')
41
42        self.vm.add_args('-netdev',
43                         'user,id=n1,hostfwd=tcp:127.0.0.1:0-:%d' %
44                         self.GUEST_PORT)
45        self.vm.add_args('-device', 'virtio-net,netdev=n1' + self.IOMMU_ADDON)
46
47    def common_vm_setup(self, kernel, initrd, disk):
48        self.require_accelerator("kvm")
49        self.require_netdev('user')
50        self.set_machine("virt")
51        self.vm.add_args('-m', '1G')
52        self.vm.add_args("-accel", "kvm")
53        self.vm.add_args("-cpu", "host")
54        self.vm.add_args("-machine", "iommu=smmuv3")
55        self.vm.add_args("-d", "guest_errors")
56        self.vm.add_args('-bios', os.path.join(BUILD_DIR, 'pc-bios',
57                         'edk2-aarch64-code.fd'))
58        self.vm.add_args('-device', 'virtio-rng-pci,rng=rng0')
59        self.vm.add_args('-object',
60                         'rng-random,id=rng0,filename=/dev/urandom')
61
62        self.kernel_path = kernel.fetch()
63        self.initrd_path = initrd.fetch()
64        self.set_up_boot(disk.fetch())
65
66    def run_and_check(self, filename, hashsum):
67        self.vm.add_args('-initrd', self.initrd_path)
68        self.vm.add_args('-append', self.kernel_params)
69        self.launch_kernel(self.kernel_path, initrd=self.initrd_path,
70                           wait_for='attach it to a bug report.')
71        prompt = '# '
72        # Fedora 33 requires 'return' to be pressed to enter the shell.
73        # There seems to be a small race between detecting the previous ':'
74        # and sending the newline, so we need to add a small delay here.
75        self.wait_for_console_pattern(':')
76        time.sleep(0.2)
77        exec_command_and_wait_for_pattern(self, '\n', prompt)
78        exec_command_and_wait_for_pattern(self, 'cat /proc/cmdline',
79                                          self.kernel_params)
80
81        # Checking for SMMU enablement:
82        self.log.info("Checking whether SMMU has been enabled...")
83        exec_command_and_wait_for_pattern(self, 'dmesg | grep smmu',
84                                          'arm-smmu-v3')
85        self.wait_for_console_pattern(prompt)
86        exec_command_and_wait_for_pattern(self,
87                                    'find /sys/kernel/iommu_groups/ -type l',
88                                    'devices/0000:00:')
89        self.wait_for_console_pattern(prompt)
90
91        # Copy a file (checked later), umount afterwards to drop disk cache:
92        self.log.info("Checking hard disk...")
93        exec_command_and_wait_for_pattern(self,
94                        "while ! (dmesg -c | grep vda:) ; do sleep 1 ; done",
95                        "vda2")
96        exec_command_and_wait_for_pattern(self, 'mount /dev/vda2 /sysroot',
97                                          'mounted filesystem')
98        exec_command_and_wait_for_pattern(self, 'cp /bin/vi /sysroot/root/vi',
99                                          prompt)
100        exec_command_and_wait_for_pattern(self, 'umount /sysroot', prompt)
101        # Switch from initrd to the cloud image filesystem:
102        exec_command_and_wait_for_pattern(self, 'mount /dev/vda2 /sysroot',
103                                          prompt)
104        exec_command_and_wait_for_pattern(self,
105                ('for d in dev proc sys run ; do '
106                 'mount -o bind /$d /sysroot/$d ; done'), prompt)
107        exec_command_and_wait_for_pattern(self, 'chroot /sysroot', prompt)
108        # Check files on the hard disk:
109        exec_command_and_wait_for_pattern(self,
110            ('if diff -q /root/vi /usr/bin/vi ; then echo "file" "ok" ; '
111             'else echo "files differ"; fi'), 'file ok')
112        self.wait_for_console_pattern(prompt)
113        exec_command_and_wait_for_pattern(self, f'sha256sum {filename}',
114                                          hashsum)
115
116        # Check virtio-net via HTTP:
117        exec_command_and_wait_for_pattern(self, 'dhclient eth0', prompt)
118        self.check_http_download(filename, hashsum, self.GUEST_PORT)
119
120
121    # 5.3 kernel without RIL #
122
123    ASSET_KERNEL_F31 = Asset(
124        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/'
125         'releases/31/Server/aarch64/os/images/pxeboot/vmlinuz'),
126        '3ae07fcafbfc8e4abeb693035a74fe10698faae15e9ccd48882a9167800c1527')
127
128    ASSET_INITRD_F31 = Asset(
129        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/'
130         'releases/31/Server/aarch64/os/images/pxeboot/initrd.img'),
131        '9f3146b28bc531c689f3c5f114cb74e4bd7bd548e0ba19fa77921d8bd256755a')
132
133    ASSET_DISK_F31 = Asset(
134        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases'
135         '/31/Cloud/aarch64/images/Fedora-Cloud-Base-31-1.9.aarch64.qcow2'),
136        '1e18d9c0cf734940c4b5d5ec592facaed2af0ad0329383d5639c997fdf16fe49')
137
138    F31_FILENAME = '/boot/initramfs-5.3.7-301.fc31.aarch64.img'
139    F31_HSUM = '1a4beec6607d94df73d9dd1b4985c9c23dd0fdcf4e6ca1351d477f190df7bef9'
140
141    def test_smmu_noril(self):
142        self.common_vm_setup(self.ASSET_KERNEL_F31, self.ASSET_INITRD_F31,
143                             self.ASSET_DISK_F31)
144        self.kernel_params = self.default_kernel_params
145        self.run_and_check(self.F31_FILENAME, self.F31_HSUM)
146
147    def test_smmu_noril_passthrough(self):
148        self.common_vm_setup(self.ASSET_KERNEL_F31, self.ASSET_INITRD_F31,
149                             self.ASSET_DISK_F31)
150        self.kernel_params = (self.default_kernel_params +
151                              ' iommu.passthrough=on')
152        self.run_and_check(self.F31_FILENAME, self.F31_HSUM)
153
154    def test_smmu_noril_nostrict(self):
155        self.common_vm_setup(self.ASSET_KERNEL_F31, self.ASSET_INITRD_F31,
156                             self.ASSET_DISK_F31)
157        self.kernel_params = (self.default_kernel_params +
158                              ' iommu.strict=0')
159        self.run_and_check(self.F31_FILENAME, self.F31_HSUM)
160
161
162    # 5.8 kernel featuring range invalidation
163    # >= v5.7 kernel
164
165    ASSET_KERNEL_F33 = Asset(
166        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/'
167         'releases/33/Server/aarch64/os/images/pxeboot/vmlinuz'),
168        'd8b1e6f7241f339d8e7609c456cf0461ffa4583ed07e0b55c7d1d8a0c154aa89')
169
170    ASSET_INITRD_F33 = Asset(
171        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/'
172         'releases/33/Server/aarch64/os/images/pxeboot/initrd.img'),
173        '92513f55295c2c16a777f7b6c35ccd70a438e9e1e40b6ba39e0e60900615b3df')
174
175    ASSET_DISK_F33 = Asset(
176        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases'
177         '/33/Cloud/aarch64/images/Fedora-Cloud-Base-33-1.2.aarch64.qcow2'),
178        'e7f75cdfd523fe5ac2ca9eeece68edc1a81f386a17f969c1d1c7c87031008a6b')
179
180    F33_FILENAME = '/boot/initramfs-5.8.15-301.fc33.aarch64.img'
181    F33_HSUM = '079cfad0caa82e84c8ca1fb0897a4999dd769f262216099f518619e807a550d9'
182
183    def test_smmu_ril(self):
184        self.common_vm_setup(self.ASSET_KERNEL_F33, self.ASSET_INITRD_F33,
185                             self.ASSET_DISK_F33)
186        self.kernel_params = self.default_kernel_params
187        self.run_and_check(self.F33_FILENAME, self.F33_HSUM)
188
189    def test_smmu_ril_passthrough(self):
190        self.common_vm_setup(self.ASSET_KERNEL_F33, self.ASSET_INITRD_F33,
191                             self.ASSET_DISK_F33)
192        self.kernel_params = (self.default_kernel_params +
193                              ' iommu.passthrough=on')
194        self.run_and_check(self.F33_FILENAME, self.F33_HSUM)
195
196    def test_smmu_ril_nostrict(self):
197        self.common_vm_setup(self.ASSET_KERNEL_F33, self.ASSET_INITRD_F33,
198                             self.ASSET_DISK_F33)
199        self.kernel_params = (self.default_kernel_params +
200                              ' iommu.strict=0')
201        self.run_and_check(self.F33_FILENAME, self.F33_HSUM)
202
203
204if __name__ == '__main__':
205    LinuxKernelTest.main()
206