Bin Meng | f712f2a | 2019-07-18 00:34:16 -0700 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0+ |
| 2 | .. Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 3 | |
Bin Meng | f712f2a | 2019-07-18 00:34:16 -0700 | [diff] [blame] | 4 | QEMU RISC-V |
| 5 | =========== |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 6 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 7 | QEMU for RISC-V supports a special 'virt' machine and 'spike' machine designed |
| 8 | for emulation and virtualization purposes. This document describes how to run |
| 9 | U-Boot under it. Both 32-bit and 64-bit targets are supported, running in |
| 10 | either machine or supervisor mode. |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 11 | |
| 12 | The QEMU virt machine models a generic RISC-V virtual machine with support for |
| 13 | the VirtIO standard networking and block storage devices. It has CLINT, PLIC, |
| 14 | 16550A UART devices in addition to VirtIO and it also uses device-tree to pass |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 15 | configuration information to guest software. It implements the latest RISC-V |
| 16 | privileged architecture. |
Simon Glass | eb28494 | 2021-12-16 20:59:09 -0700 | [diff] [blame] | 17 | |
| 18 | See :doc:`../../develop/devicetree/dt_qemu` for information on how to see |
| 19 | the devicetree actually generated by QEMU. |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 20 | |
| 21 | The QEMU spike machine models a minimalistic RISC-V virtual machine with |
| 22 | only CLINT and HTIF devices. It also uses device-tree to pass configuration |
| 23 | information to guest software and implements the latest RISC-V privileged |
| 24 | architecture. |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 25 | |
| 26 | Building U-Boot |
| 27 | --------------- |
| 28 | Set the CROSS_COMPILE environment variable as usual, and run: |
| 29 | |
Bin Meng | f712f2a | 2019-07-18 00:34:16 -0700 | [diff] [blame] | 30 | - For 32-bit RISC-V:: |
| 31 | |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 32 | make qemu-riscv32_defconfig |
| 33 | make |
| 34 | |
Bin Meng | f712f2a | 2019-07-18 00:34:16 -0700 | [diff] [blame] | 35 | - For 64-bit RISC-V:: |
| 36 | |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 37 | make qemu-riscv64_defconfig |
| 38 | make |
| 39 | |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 40 | This will compile U-Boot for machine mode. To build supervisor mode binaries, |
| 41 | use the configurations qemu-riscv32_smode_defconfig and |
| 42 | qemu-riscv64_smode_defconfig instead. Note that U-Boot running in supervisor |
| 43 | mode requires a supervisor binary interface (SBI), such as RISC-V OpenSBI. |
| 44 | |
Heinrich Schuchardt | 1c90d9a | 2025-01-23 03:46:51 +0100 | [diff] [blame] | 45 | To create a U-Boot binary that can be utilized with a pflash device in QEMU |
| 46 | apply 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 | |
| 55 | Truncate the resulting u-boot.bin to 32 MiB. Add the following to your |
| 56 | qemu-system-riscv64 command: |
| 57 | |
| 58 | .. code-block:: bash |
| 59 | |
| 60 | -drive if=pflash,format=raw,unit=0,file=u-boot.bin |
| 61 | |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 62 | Running U-Boot |
| 63 | -------------- |
| 64 | The minimal QEMU command line to get U-Boot up and running is: |
| 65 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 66 | - For 32-bit RISC-V virt machine:: |
Bin Meng | f712f2a | 2019-07-18 00:34:16 -0700 | [diff] [blame] | 67 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 68 | qemu-system-riscv32 -nographic -machine virt -bios u-boot.bin |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 69 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 70 | - 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 Meng | f712f2a | 2019-07-18 00:34:16 -0700 | [diff] [blame] | 75 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 76 | qemu-system-riscv64 -nographic -machine spike -bios u-boot.bin |
Bin Meng | 8a8694d | 2018-09-26 06:55:21 -0700 | [diff] [blame] | 77 | |
| 78 | The commands above create targets with 128MiB memory by default. |
| 79 | A freely configurable amount of RAM can be created via the '-m' |
| 80 | parameter. For example, '-m 2G' creates 2GiB memory for the target, |
| 81 | and the memory node in the embedded DTB created by QEMU reflects |
| 82 | the new setting. |
| 83 | |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 84 | For instructions on how to run U-Boot in supervisor mode on QEMU |
| 85 | with OpenSBI, see the documentation available with OpenSBI: |
| 86 | https://github.com/riscv/opensbi/blob/master/docs/platform/qemu_virt.md |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 87 | https://github.com/riscv/opensbi/blob/master/docs/platform/spike.md |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 88 | |
Bin Meng | a33888f | 2020-06-23 05:23:15 -0700 | [diff] [blame] | 89 | These have been tested in QEMU 5.0.0. |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 90 | |
| 91 | Running U-Boot SPL |
| 92 | ------------------ |
| 93 | In the default SPL configuration, U-Boot SPL starts in machine mode. U-Boot |
| 94 | proper and OpenSBI (FW_DYNAMIC firmware) are bundled as FIT image and made |
| 95 | available to U-Boot SPL. Both are then loaded by U-Boot SPL and the location |
| 96 | of U-Boot proper is passed to OpenSBI. After initialization, U-Boot proper is |
| 97 | started in supervisor mode by OpenSBI. |
| 98 | |
| 99 | OpenSBI must be compiled before compiling U-Boot. Version 0.4 and higher is |
| 100 | supported 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 Patra | 1d4d6ad | 2020-12-22 11:50:01 -0800 | [diff] [blame] | 106 | make PLATFORM=generic |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 107 | |
| 108 | See the OpenSBI documentation for full details: |
| 109 | https://github.com/riscv/opensbi/blob/master/docs/platform/qemu_virt.md |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 110 | https://github.com/riscv/opensbi/blob/master/docs/platform/spike.md |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 111 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 112 | To make the FW_DYNAMIC binary (build/platform/generic/firmware/fw_dynamic.bin) |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 113 | available to U-Boot, either copy it into the U-Boot root directory or specify |
| 114 | its location with the OPENSBI environment variable. Afterwards, compile U-Boot |
| 115 | with 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 | |
| 127 | The minimal QEMU commands to run U-Boot SPL in both 32-bit and 64-bit |
| 128 | configurations are: |
| 129 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 130 | - For 32-bit RISC-V virt machine:: |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 131 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 132 | qemu-system-riscv32 -nographic -machine virt -bios spl/u-boot-spl.bin \ |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 133 | -device loader,file=u-boot.itb,addr=0x80200000 |
| 134 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 135 | - 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 Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 141 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 142 | qemu-system-riscv64 -nographic -machine spike -bios spl/u-boot-spl.bin \ |
Lukas Auer | eaa2b66 | 2019-08-21 21:14:50 +0200 | [diff] [blame] | 143 | -device loader,file=u-boot.itb,addr=0x80200000 |
Heinrich Schuchardt | 7ba3420 | 2020-11-04 12:59:13 +0100 | [diff] [blame] | 144 | |
Anup Patel | f650a2c | 2022-01-27 11:41:10 +0530 | [diff] [blame] | 145 | An attached disk can be emulated in RISC-V virt machine by adding:: |
Heinrich Schuchardt | 7ba3420 | 2020-11-04 12:59:13 +0100 | [diff] [blame] | 146 | |
| 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 Meng | 93031d8 | 2023-10-11 21:15:52 +0800 | [diff] [blame] | 151 | or 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 | |
| 157 | You will have to run 'scsi scan' to use them. |
Heinrich Schuchardt | 216a19f | 2022-04-05 16:14:27 +0200 | [diff] [blame] | 158 | |
Bin Meng | cbb84f3 | 2023-07-23 12:40:33 +0800 | [diff] [blame] | 159 | A video console can be emulated in RISC-V virt machine by removing "-nographic" |
| 160 | and adding:: |
| 161 | |
| 162 | -serial stdio -device VGA |
| 163 | |
Bin Meng | f6809fb | 2023-07-23 12:40:41 +0800 | [diff] [blame] | 164 | In addition, a usb keyboard can be attached to an emulated xHCI controller in |
| 165 | RISC-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 Schuchardt | 04d4b7c | 2022-08-14 16:13:49 +0200 | [diff] [blame] | 169 | Running with KVM |
| 170 | ---------------- |
| 171 | |
| 172 | Running with QEMU using KVM requires an S-mode U-Boot binary as created by |
| 173 | qemu-riscv64_smode_defconfig. |
| 174 | |
| 175 | Provide 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 Schuchardt | 216a19f | 2022-04-05 16:14:27 +0200 | [diff] [blame] | 182 | Debug UART |
| 183 | ---------- |
| 184 | |
| 185 | The 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 Schuchardt | 80f18d3 | 2024-11-05 04:16:31 +0100 | [diff] [blame] | 191 | |
| 192 | To provide a debug UART in main U-Boot the SBI DBCN extension can be used |
| 193 | instead:: |
| 194 | |
| 195 | CONFIG_DEBUG_SBI_CONSOLE=y |