blob: cf2f893b723eedbd06de75bf4f428f1402a58f63 [file] [log] [blame]
Bin Mengf712f2a2019-07-18 00:34:16 -07001.. SPDX-License-Identifier: GPL-2.0+
2.. Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
Bin Meng8a8694d2018-09-26 06:55:21 -07003
Bin Mengf712f2a2019-07-18 00:34:16 -07004QEMU RISC-V
5===========
Bin Meng8a8694d2018-09-26 06:55:21 -07006
Anup Patelf650a2c2022-01-27 11:41:10 +05307QEMU for RISC-V supports a special 'virt' machine and 'spike' machine designed
8for emulation and virtualization purposes. This document describes how to run
9U-Boot under it. Both 32-bit and 64-bit targets are supported, running in
10either machine or supervisor mode.
Bin Meng8a8694d2018-09-26 06:55:21 -070011
12The QEMU virt machine models a generic RISC-V virtual machine with support for
13the VirtIO standard networking and block storage devices. It has CLINT, PLIC,
1416550A UART devices in addition to VirtIO and it also uses device-tree to pass
Anup Patelf650a2c2022-01-27 11:41:10 +053015configuration information to guest software. It implements the latest RISC-V
16privileged architecture.
Simon Glasseb284942021-12-16 20:59:09 -070017
18See :doc:`../../develop/devicetree/dt_qemu` for information on how to see
19the devicetree actually generated by QEMU.
Anup Patelf650a2c2022-01-27 11:41:10 +053020
21The QEMU spike machine models a minimalistic RISC-V virtual machine with
22only CLINT and HTIF devices. It also uses device-tree to pass configuration
23information to guest software and implements the latest RISC-V privileged
24architecture.
Bin Meng8a8694d2018-09-26 06:55:21 -070025
26Building U-Boot
27---------------
28Set the CROSS_COMPILE environment variable as usual, and run:
29
Bin Mengf712f2a2019-07-18 00:34:16 -070030- For 32-bit RISC-V::
31
Bin Meng8a8694d2018-09-26 06:55:21 -070032 make qemu-riscv32_defconfig
33 make
34
Bin Mengf712f2a2019-07-18 00:34:16 -070035- For 64-bit RISC-V::
36
Bin Meng8a8694d2018-09-26 06:55:21 -070037 make qemu-riscv64_defconfig
38 make
39
Lukas Auereaa2b662019-08-21 21:14:50 +020040This will compile U-Boot for machine mode. To build supervisor mode binaries,
41use the configurations qemu-riscv32_smode_defconfig and
42qemu-riscv64_smode_defconfig instead. Note that U-Boot running in supervisor
43mode requires a supervisor binary interface (SBI), such as RISC-V OpenSBI.
44
Heinrich Schuchardt1c90d9a2025-01-23 03:46:51 +010045To create a U-Boot binary that can be utilized with a pflash device in QEMU
46apply these addtional settings to qemu-riscv64_smode_defconfig:
47
48::
49
50 CONFIG_TEXT_BASE=0x20000000
51 CONFIG_XIP=y
52 # CONFIG_AVAILABLE_HARTS is not set
53 CONFIG_SYS_MONITOR_BASE=0x80200000
54
55Truncate the resulting u-boot.bin to 32 MiB. Add the following to your
56qemu-system-riscv64 command:
57
58.. code-block:: bash
59
60 -drive if=pflash,format=raw,unit=0,file=u-boot.bin
61
Bin Meng8a8694d2018-09-26 06:55:21 -070062Running U-Boot
63--------------
64The minimal QEMU command line to get U-Boot up and running is:
65
Anup Patelf650a2c2022-01-27 11:41:10 +053066- For 32-bit RISC-V virt machine::
Bin Mengf712f2a2019-07-18 00:34:16 -070067
Anup Patelf650a2c2022-01-27 11:41:10 +053068 qemu-system-riscv32 -nographic -machine virt -bios u-boot.bin
Bin Meng8a8694d2018-09-26 06:55:21 -070069
Anup Patelf650a2c2022-01-27 11:41:10 +053070- For 64-bit RISC-V virt machine::
71
72 qemu-system-riscv64 -nographic -machine virt -bios u-boot.bin
73
74- For 64-bit RISC-V spike machine::
Bin Mengf712f2a2019-07-18 00:34:16 -070075
Anup Patelf650a2c2022-01-27 11:41:10 +053076 qemu-system-riscv64 -nographic -machine spike -bios u-boot.bin
Bin Meng8a8694d2018-09-26 06:55:21 -070077
78The commands above create targets with 128MiB memory by default.
79A freely configurable amount of RAM can be created via the '-m'
80parameter. For example, '-m 2G' creates 2GiB memory for the target,
81and the memory node in the embedded DTB created by QEMU reflects
82the new setting.
83
Lukas Auereaa2b662019-08-21 21:14:50 +020084For instructions on how to run U-Boot in supervisor mode on QEMU
85with OpenSBI, see the documentation available with OpenSBI:
86https://github.com/riscv/opensbi/blob/master/docs/platform/qemu_virt.md
Anup Patelf650a2c2022-01-27 11:41:10 +053087https://github.com/riscv/opensbi/blob/master/docs/platform/spike.md
Lukas Auereaa2b662019-08-21 21:14:50 +020088
Bin Menga33888f2020-06-23 05:23:15 -070089These have been tested in QEMU 5.0.0.
Lukas Auereaa2b662019-08-21 21:14:50 +020090
91Running U-Boot SPL
92------------------
93In the default SPL configuration, U-Boot SPL starts in machine mode. U-Boot
94proper and OpenSBI (FW_DYNAMIC firmware) are bundled as FIT image and made
95available to U-Boot SPL. Both are then loaded by U-Boot SPL and the location
96of U-Boot proper is passed to OpenSBI. After initialization, U-Boot proper is
97started in supervisor mode by OpenSBI.
98
99OpenSBI must be compiled before compiling U-Boot. Version 0.4 and higher is
100supported by U-Boot. Clone the OpenSBI repository and run the following command.
101
102.. code-block:: console
103
104 git clone https://github.com/riscv/opensbi.git
105 cd opensbi
Atish Patra1d4d6ad2020-12-22 11:50:01 -0800106 make PLATFORM=generic
Lukas Auereaa2b662019-08-21 21:14:50 +0200107
108See the OpenSBI documentation for full details:
109https://github.com/riscv/opensbi/blob/master/docs/platform/qemu_virt.md
Anup Patelf650a2c2022-01-27 11:41:10 +0530110https://github.com/riscv/opensbi/blob/master/docs/platform/spike.md
Lukas Auereaa2b662019-08-21 21:14:50 +0200111
Anup Patelf650a2c2022-01-27 11:41:10 +0530112To make the FW_DYNAMIC binary (build/platform/generic/firmware/fw_dynamic.bin)
Lukas Auereaa2b662019-08-21 21:14:50 +0200113available to U-Boot, either copy it into the U-Boot root directory or specify
114its location with the OPENSBI environment variable. Afterwards, compile U-Boot
115with the following commands.
116
117- For 32-bit RISC-V::
118
119 make qemu-riscv32_spl_defconfig
120 make
121
122- For 64-bit RISC-V::
123
124 make qemu-riscv64_spl_defconfig
125 make
126
127The minimal QEMU commands to run U-Boot SPL in both 32-bit and 64-bit
128configurations are:
129
Anup Patelf650a2c2022-01-27 11:41:10 +0530130- For 32-bit RISC-V virt machine::
Lukas Auereaa2b662019-08-21 21:14:50 +0200131
Anup Patelf650a2c2022-01-27 11:41:10 +0530132 qemu-system-riscv32 -nographic -machine virt -bios spl/u-boot-spl.bin \
Lukas Auereaa2b662019-08-21 21:14:50 +0200133 -device loader,file=u-boot.itb,addr=0x80200000
134
Anup Patelf650a2c2022-01-27 11:41:10 +0530135- For 64-bit RISC-V virt machine::
136
137 qemu-system-riscv64 -nographic -machine virt -bios spl/u-boot-spl.bin \
138 -device loader,file=u-boot.itb,addr=0x80200000
139
140- For 64-bit RISC-V spike machine::
Lukas Auereaa2b662019-08-21 21:14:50 +0200141
Anup Patelf650a2c2022-01-27 11:41:10 +0530142 qemu-system-riscv64 -nographic -machine spike -bios spl/u-boot-spl.bin \
Lukas Auereaa2b662019-08-21 21:14:50 +0200143 -device loader,file=u-boot.itb,addr=0x80200000
Heinrich Schuchardt7ba34202020-11-04 12:59:13 +0100144
Anup Patelf650a2c2022-01-27 11:41:10 +0530145An attached disk can be emulated in RISC-V virt machine by adding::
Heinrich Schuchardt7ba34202020-11-04 12:59:13 +0100146
147 -device ich9-ahci,id=ahci \
148 -drive if=none,file=riscv64.img,format=raw,id=mydisk \
149 -device ide-hd,drive=mydisk,bus=ahci.0
150
Bin Meng93031d82023-10-11 21:15:52 +0800151or alternatively attach an emulated UFS::
152
153 -device ufs,id=ufs0 \
154 -drive if=none,file=test.img,format=raw,id=lun0 \
155 -device ufs-lu,drive=lun0,bus=ufs0
156
157You will have to run 'scsi scan' to use them.
Heinrich Schuchardt216a19f2022-04-05 16:14:27 +0200158
Bin Mengcbb84f32023-07-23 12:40:33 +0800159A video console can be emulated in RISC-V virt machine by removing "-nographic"
160and adding::
161
162 -serial stdio -device VGA
163
Bin Mengf6809fb2023-07-23 12:40:41 +0800164In addition, a usb keyboard can be attached to an emulated xHCI controller in
165RISC-V virt machine as an option of input devices by adding::
166
167 -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0
168
Heinrich Schuchardt04d4b7c2022-08-14 16:13:49 +0200169Running with KVM
170----------------
171
172Running with QEMU using KVM requires an S-mode U-Boot binary as created by
173qemu-riscv64_smode_defconfig.
174
175Provide the U-Boot S-mode ELF image as *-kernel* parameter and do not add a
176*-bios* parameter, e.g.
177
178.. code-block:: bash
179
180 qemu-system-riscv64 -accel kvm -nographic -machine virt -kernel u-boot
181
Heinrich Schuchardt216a19f2022-04-05 16:14:27 +0200182Debug UART
183----------
184
185The following settings provide a debug UART for the virt machine::
186
187 CONFIG_DEBUG_UART=y
188 CONFIG_DEBUG_UART_NS16550=y
189 CONFIG_DEBUG_UART_BASE=0x10000000
190 CONFIG_DEBUG_UART_CLOCK=3686400
Heinrich Schuchardt80f18d32024-11-05 04:16:31 +0100191
192To provide a debug UART in main U-Boot the SBI DBCN extension can be used
193instead::
194
195 CONFIG_DEBUG_SBI_CONSOLE=y