1# SPDX-License-Identifier: GPL-2.0-or-later
2#
3# LoongArch virt test.
4#
5# Copyright (c) 2023 Loongson Technology Corporation Limited
6#
7
8from avocado_qemu import QemuSystemTest
9from avocado_qemu import exec_command_and_wait_for_pattern
10from avocado_qemu import wait_for_console_pattern
11
12class LoongArchMachine(QemuSystemTest):
13    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
14
15    timeout = 120
16
17    def wait_for_console_pattern(self, success_message, vm=None):
18        wait_for_console_pattern(self, success_message,
19                                 failure_message='Kernel panic - not syncing',
20                                 vm=vm)
21
22    def test_loongarch64_devices(self):
23
24        """
25        :avocado: tags=arch:loongarch64
26        :avocado: tags=machine:virt
27        """
28
29        kernel_url = ('https://github.com/yangxiaojuan-loongson/qemu-binary/'
30                      'releases/download/binary-files/vmlinuz.efi')
31        kernel_hash = '951b485b16e3788b6db03a3e1793c067009e31a2'
32        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
33
34        initrd_url = ('https://github.com/yangxiaojuan-loongson/qemu-binary/'
35                      'releases/download/binary-files/ramdisk')
36        initrd_hash = 'c67658d9b2a447ce7db2f73ba3d373c9b2b90ab2'
37        initrd_path = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
38
39        bios_url = ('https://github.com/yangxiaojuan-loongson/qemu-binary/'
40                    'releases/download/binary-files/QEMU_EFI.fd')
41        bios_hash = ('dfc1bfba4853cd763b9d392d0031827e8addbca8')
42        bios_path = self.fetch_asset(bios_url, asset_hash=bios_hash)
43
44        self.vm.set_console()
45        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
46                               'root=/dev/ram rdinit=/sbin/init console=ttyS0,115200')
47        self.vm.add_args('-nographic',
48                         '-smp', '4',
49                         '-m', '1024',
50                         '-cpu', 'la464',
51                         '-kernel', kernel_path,
52                         '-initrd', initrd_path,
53                         '-bios', bios_path,
54                         '-append', kernel_command_line)
55        self.vm.launch()
56        self.wait_for_console_pattern('Run /sbin/init as init process')
57        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
58                                          'processor		: 3')
59