124dba2b3SPaul BeesleyRaspberry Pi 3 224dba2b3SPaul Beesley============== 31cd4fb65SAntonio Nino Diaz 41cd4fb65SAntonio Nino DiazThe `Raspberry Pi 3`_ is an inexpensive single-board computer that contains four 55e2e024bSAntonio Nino DiazArm Cortex-A53 cores. 61cd4fb65SAntonio Nino Diaz 74def07d5SDan HandleyThe following instructions explain how to use this port of the TF-A with the 84def07d5SDan Handleydefault distribution of `Raspbian`_ because that's the distribution officially 94def07d5SDan Handleysupported by the Raspberry Pi Foundation. At the moment of writing this, the 104def07d5SDan Handleyofficially supported kernel is a AArch32 kernel. This doesn't mean that this 114def07d5SDan Handleyport of TF-A can't boot a AArch64 kernel. The `Linux tree fork`_ maintained by 124def07d5SDan Handleythe Foundation can be compiled for AArch64 by following the steps in 134def07d5SDan Handley`AArch64 kernel build instructions`_. 141cd4fb65SAntonio Nino Diaz 151cd4fb65SAntonio Nino Diaz**IMPORTANT NOTE**: This port isn't secure. All of the memory used is DRAM, 161cd4fb65SAntonio Nino Diazwhich is available from both the Non-secure and Secure worlds. This port 171cd4fb65SAntonio Nino Diazshouldn't be considered more than a prototype to play with and implement 181cd4fb65SAntonio Nino Diazelements like PSCI to support the Linux kernel. 191cd4fb65SAntonio Nino Diaz 201cd4fb65SAntonio Nino DiazDesign 211cd4fb65SAntonio Nino Diaz------ 221cd4fb65SAntonio Nino Diaz 231cd4fb65SAntonio Nino DiazThe SoC used by the Raspberry Pi 3 is the Broadcom BCM2837. It is a SoC with a 241cd4fb65SAntonio Nino DiazVideoCore IV that acts as primary processor (and loads everything from the SD 251cd4fb65SAntonio Nino Diazcard) and is located between all Arm cores and the DRAM. Check the `Raspberry Pi 261cd4fb65SAntonio Nino Diaz3 documentation`_ for more information. 271cd4fb65SAntonio Nino Diaz 281cd4fb65SAntonio Nino DiazThis explains why it is possible to change the execution state (AArch64/AArch32) 291cd4fb65SAntonio Nino Diazdepending on a few files on the SD card. We only care about the cases in which 301cd4fb65SAntonio Nino Diazthe cores boot in AArch64 mode. 311cd4fb65SAntonio Nino Diaz 321cd4fb65SAntonio Nino DiazThe rules are simple: 331cd4fb65SAntonio Nino Diaz 341cd4fb65SAntonio Nino Diaz- If a file called ``kernel8.img`` is located on the ``boot`` partition of the 351cd4fb65SAntonio Nino Diaz SD card, it will load it and execute in EL2 in AArch64. Basically, it executes 361cd4fb65SAntonio Nino Diaz a `default AArch64 stub`_ at address **0x0** that jumps to the kernel. 371cd4fb65SAntonio Nino Diaz 381cd4fb65SAntonio Nino Diaz- If there is also a file called ``armstub8.bin``, it will load it at address 391cd4fb65SAntonio Nino Diaz **0x0** (instead of the default stub) and execute it in EL3 in AArch64. All 401cd4fb65SAntonio Nino Diaz the cores are powered on at the same time and start at address **0x0**. 411cd4fb65SAntonio Nino Diaz 421cd4fb65SAntonio Nino DiazThis means that we can use the default AArch32 kernel provided in the official 434def07d5SDan Handley`Raspbian`_ distribution by renaming it to ``kernel8.img``, while TF-A and 444def07d5SDan Handleyanything else we need is in ``armstub8.bin``. This way we can forget about the 454def07d5SDan Handleydefault bootstrap code. When using a AArch64 kernel, it is only needed to make 464def07d5SDan Handleysure that the name on the SD card is ``kernel8.img``. 471cd4fb65SAntonio Nino Diaz 481cd4fb65SAntonio Nino DiazIdeally, we want to load the kernel and have all cores available, which means 491cd4fb65SAntonio Nino Diazthat we need to make the secondary cores work in the way the kernel expects, as 501cd4fb65SAntonio Nino Diazexplained in `Secondary cores`_. In practice, a small bootstrap is needed 514def07d5SDan Handleybetween TF-A and the kernel. 521cd4fb65SAntonio Nino Diaz 531cd4fb65SAntonio Nino DiazTo get the most out of a AArch32 kernel, we want to boot it in Hypervisor mode 541cd4fb65SAntonio Nino Diazin AArch32. This means that BL33 can't be in EL2 in AArch64 mode. The 551cd4fb65SAntonio Nino Diazarchitecture specifies that AArch32 Hypervisor mode isn't present when AArch64 561cd4fb65SAntonio Nino Diazis used for EL2. When using a AArch64 kernel, it should simply start in EL2. 571cd4fb65SAntonio Nino Diaz 581cd4fb65SAntonio Nino DiazPlacement of images 591cd4fb65SAntonio Nino Diaz~~~~~~~~~~~~~~~~~~~ 601cd4fb65SAntonio Nino Diaz 611cd4fb65SAntonio Nino DiazThe file ``armstub8.bin`` contains BL1 and the FIP. It is needed to add padding 621cd4fb65SAntonio Nino Diazbetween them so that the addresses they are loaded to match the ones specified 635e2e024bSAntonio Nino Diazwhen compiling TF-A. This is done automatically by the build system. 641cd4fb65SAntonio Nino Diaz 651cd4fb65SAntonio Nino DiazThe device tree block is loaded by the VideoCore loader from an appropriate 661cd4fb65SAntonio Nino Diazfile, but we can specify the address it is loaded to in ``config.txt``. 671cd4fb65SAntonio Nino Diaz 681cd4fb65SAntonio Nino DiazThe file ``kernel8.img`` contains a kernel image that is loaded to the address 691cd4fb65SAntonio Nino Diazspecified in ``config.txt``. The `Linux kernel tree`_ has information about how 701cd4fb65SAntonio Nino Diaza AArch32 Linux kernel image is loaded in ``Documentation/arm/Booting``: 711cd4fb65SAntonio Nino Diaz 721cd4fb65SAntonio Nino Diaz:: 731cd4fb65SAntonio Nino Diaz 741cd4fb65SAntonio Nino Diaz The zImage may also be placed in system RAM and called there. The 751cd4fb65SAntonio Nino Diaz kernel should be placed in the first 128MiB of RAM. It is recommended 761cd4fb65SAntonio Nino Diaz that it is loaded above 32MiB in order to avoid the need to relocate 771cd4fb65SAntonio Nino Diaz prior to decompression, which will make the boot process slightly 781cd4fb65SAntonio Nino Diaz faster. 791cd4fb65SAntonio Nino Diaz 801cd4fb65SAntonio Nino DiazThere are no similar restrictions for AArch64 kernels, as specified in the file 811cd4fb65SAntonio Nino Diaz``Documentation/arm64/booting.txt``. 821cd4fb65SAntonio Nino Diaz 831cd4fb65SAntonio Nino DiazThis means that we need to avoid the first 128 MiB of RAM when placing the 844def07d5SDan HandleyTF-A images (and specially the first 32 MiB, as they are directly used to 854def07d5SDan Handleyplace the uncompressed AArch32 kernel image. This way, both AArch32 and 861cd4fb65SAntonio Nino DiazAArch64 kernels can be placed at the same address. 871cd4fb65SAntonio Nino Diaz 881cd4fb65SAntonio Nino DiazIn the end, the images look like the following diagram when placed in memory. 891cd4fb65SAntonio Nino DiazAll addresses are Physical Addresses from the point of view of the Arm cores. 901cd4fb65SAntonio Nino DiazAgain, note that this is all just part of the same DRAM that goes from 911cd4fb65SAntonio Nino Diaz**0x00000000** to **0x3F000000**, it just has different names to simulate a real 921cd4fb65SAntonio Nino Diazsecure platform! 931cd4fb65SAntonio Nino Diaz 941cd4fb65SAntonio Nino Diaz:: 951cd4fb65SAntonio Nino Diaz 961cd4fb65SAntonio Nino Diaz 0x00000000 +-----------------+ 971cd4fb65SAntonio Nino Diaz | ROM | BL1 986a2c3c7fSYing-Chun Liu (PaulLiu) 0x00020000 +-----------------+ 991cd4fb65SAntonio Nino Diaz | FIP | 1001cd4fb65SAntonio Nino Diaz 0x00200000 +-----------------+ 1011cd4fb65SAntonio Nino Diaz | | 1021cd4fb65SAntonio Nino Diaz | ... | 1031cd4fb65SAntonio Nino Diaz | | 1041cd4fb65SAntonio Nino Diaz 0x01000000 +-----------------+ 10597fb05e1SAntonio Nino Diaz | DTB | (Loaded by the VideoCore) 1061cd4fb65SAntonio Nino Diaz +-----------------+ 1071cd4fb65SAntonio Nino Diaz | | 1081cd4fb65SAntonio Nino Diaz | ... | 1091cd4fb65SAntonio Nino Diaz | | 1101cd4fb65SAntonio Nino Diaz 0x02000000 +-----------------+ 11197fb05e1SAntonio Nino Diaz | Kernel | (Loaded by the VideoCore) 1121cd4fb65SAntonio Nino Diaz +-----------------+ 1131cd4fb65SAntonio Nino Diaz | | 1141cd4fb65SAntonio Nino Diaz | ... | 1151cd4fb65SAntonio Nino Diaz | | 1161cd4fb65SAntonio Nino Diaz 0x10000000 +-----------------+ 1171cd4fb65SAntonio Nino Diaz | Secure SRAM | BL2, BL31 1181cd4fb65SAntonio Nino Diaz 0x10100000 +-----------------+ 1193d464234SYing-Chun Liu (PaulLiu) | Secure DRAM | BL32 (Secure payload) 1201cd4fb65SAntonio Nino Diaz 0x11000000 +-----------------+ 121aa49bde8SAntonio Nino Diaz | Non-secure DRAM | BL33 122aa49bde8SAntonio Nino Diaz +-----------------+ 1231cd4fb65SAntonio Nino Diaz | | 1241cd4fb65SAntonio Nino Diaz | ... | 1251cd4fb65SAntonio Nino Diaz | | 1261cd4fb65SAntonio Nino Diaz 0x3F000000 +-----------------+ 1271cd4fb65SAntonio Nino Diaz | I/O | 1281cd4fb65SAntonio Nino Diaz 0x40000000 +-----------------+ 1291cd4fb65SAntonio Nino Diaz 13097fb05e1SAntonio Nino DiazThe area between **0x10000000** and **0x11000000** has to be manually protected 1315341b42eSAntonio Nino Diazso that the kernel doesn't use it. The current port tries to modify the live DTB 1325341b42eSAntonio Nino Diazto add a memreserve region that reserves the previously mentioned area. 1335341b42eSAntonio Nino Diaz 1345341b42eSAntonio Nino DiazIf this is not possible, the user may manually add ``memmap=16M$256M`` to the 1355341b42eSAntonio Nino Diazcommand line passed to the kernel in ``cmdline.txt``. See the `Setup SD card`_ 1365341b42eSAntonio Nino Diazinstructions to see how to do it. This system is strongly discouraged. 1371cd4fb65SAntonio Nino Diaz 1381cd4fb65SAntonio Nino DiazThe last 16 MiB of DRAM can only be accessed by the VideoCore, that has 1391cd4fb65SAntonio Nino Diazdifferent mappings than the Arm cores in which the I/O addresses don't overlap 1401cd4fb65SAntonio Nino Diazthe DRAM. The memory reserved to be used by the VideoCore is always placed at 1411cd4fb65SAntonio Nino Diazthe end of the DRAM, so this space isn't wasted. 1421cd4fb65SAntonio Nino Diaz 1434def07d5SDan HandleyConsidering the 128 MiB allocated to the GPU and the 16 MiB allocated for 1444def07d5SDan HandleyTF-A, there are 880 MiB available for Linux. 1451cd4fb65SAntonio Nino Diaz 1461cd4fb65SAntonio Nino DiazBoot sequence 1471cd4fb65SAntonio Nino Diaz~~~~~~~~~~~~~ 1481cd4fb65SAntonio Nino Diaz 1494def07d5SDan HandleyThe boot sequence of TF-A is the usual one except when booting an AArch32 1504def07d5SDan Handleykernel. In that case, BL33 is booted in AArch32 Hypervisor mode so that it 1514def07d5SDan Handleycan jump to the kernel in the same mode and let it take over that privilege 1524def07d5SDan Handleylevel. If BL33 was running in EL2 in AArch64 (as in the default bootflow of 1534def07d5SDan HandleyTF-A) it could only jump to the kernel in AArch32 in Supervisor mode. 1541cd4fb65SAntonio Nino Diaz 1551cd4fb65SAntonio Nino DiazThe `Linux kernel tree`_ has instructions on how to jump to the Linux kernel 1561cd4fb65SAntonio Nino Diazin ``Documentation/arm/Booting`` and ``Documentation/arm64/booting.txt``. The 1571cd4fb65SAntonio Nino Diazbootstrap should take care of this. 1581cd4fb65SAntonio Nino Diaz 1595e2e024bSAntonio Nino DiazThis port support a direct boot of the Linux kernel from the firmware (as a BL33 1605e2e024bSAntonio Nino Diazimage). Alternatively, U-Boot or other bootloaders may be used. 1615e2e024bSAntonio Nino Diaz 1621cd4fb65SAntonio Nino DiazSecondary cores 1631cd4fb65SAntonio Nino Diaz~~~~~~~~~~~~~~~ 1641cd4fb65SAntonio Nino Diaz 1655e2e024bSAntonio Nino DiazThis port of the Trusted Firmware-A supports ``PSCI_CPU_ON``, 16624dba2b3SPaul Beesley``PSCI_SYSTEM_RESET`` and ``PSCI_SYSTEM_OFF``. The last one doesn't really turn 1675e2e024bSAntonio Nino Diazthe system off, it simply reboots it and asks the VideoCore firmware to keep it 1685e2e024bSAntonio Nino Diazin a low power mode permanently. 1695e2e024bSAntonio Nino Diaz 1701cd4fb65SAntonio Nino DiazThe kernel used by `Raspbian`_ doesn't have support for PSCI, so it is needed to 1711cd4fb65SAntonio Nino Diazuse mailboxes to trap the secondary cores until they are ready to jump to the 1721cd4fb65SAntonio Nino Diazkernel. This mailbox is located at a different address in the AArch32 default 1731cd4fb65SAntonio Nino Diazkernel than in the AArch64 kernel. 1741cd4fb65SAntonio Nino Diaz 1755e2e024bSAntonio Nino DiazKernels with PSCI support can use the PSCI calls instead for a cleaner boot. 1765e2e024bSAntonio Nino Diaz 1774def07d5SDan HandleyAlso, this port of TF-A has another Trusted Mailbox in Shared BL RAM. During 1784def07d5SDan Handleycold boot, all secondary cores wait in a loop until they are given given an 1794def07d5SDan Handleyaddress to jump to in this Mailbox (``bl31_warm_entrypoint``). 1801cd4fb65SAntonio Nino Diaz 1811cd4fb65SAntonio Nino DiazOnce BL31 has finished and the primary core has jumped to the BL33 payload, it 1821cd4fb65SAntonio Nino Diazhas to call ``PSCI_CPU_ON`` to release the secondary CPUs from the wait loop. 1831cd4fb65SAntonio Nino DiazThe payload then makes them wait in another waitloop listening from messages 1841cd4fb65SAntonio Nino Diazfrom the kernel. When the primary CPU jumps into the kernel, it will send an 1851cd4fb65SAntonio Nino Diazaddress to the mailbox so that the secondary CPUs jump to it and are recognised 1861cd4fb65SAntonio Nino Diazby the kernel. 1871cd4fb65SAntonio Nino Diaz 1881cd4fb65SAntonio Nino DiazBuild Instructions 1891cd4fb65SAntonio Nino Diaz------------------ 1901cd4fb65SAntonio Nino Diaz 1911cd4fb65SAntonio Nino DiazTo boot a AArch64 kernel, only the AArch64 toolchain is required. 1921cd4fb65SAntonio Nino Diaz 1931cd4fb65SAntonio Nino DiazTo boot a AArch32 kernel, both AArch64 and AArch32 toolchains are required. The 1941cd4fb65SAntonio Nino DiazAArch32 toolchain is needed for the AArch32 bootstrap needed to load a 32-bit 1951cd4fb65SAntonio Nino Diazkernel. 1961cd4fb65SAntonio Nino Diaz 19764fe343cSAntonio Nino DiazThe build system concatenates BL1 and the FIP so that the addresses match the 19864fe343cSAntonio Nino Diazones in the memory map. The resulting file is ``armstub8.bin``, located in the 1995e2e024bSAntonio Nino Diazbuild folder (e.g. ``build/rpi3/debug/armstub8.bin``). To know how to use this 2005e2e024bSAntonio Nino Diazfile, follow the instructions in `Setup SD card`_. 2011cd4fb65SAntonio Nino Diaz 2021cd4fb65SAntonio Nino DiazThe following build options are supported: 2031cd4fb65SAntonio Nino Diaz 2041cd4fb65SAntonio Nino Diaz- ``RPI3_BL33_IN_AARCH32``: This port can load a AArch64 or AArch32 BL33 image. 2054def07d5SDan Handley By default this option is 0, which means that TF-A will jump to BL33 in EL2 2064def07d5SDan Handley in AArch64 mode. If set to 1, it will jump to BL33 in Hypervisor in AArch32 2074def07d5SDan Handley mode. 2081cd4fb65SAntonio Nino Diaz 2095e2e024bSAntonio Nino Diaz- ``PRELOADED_BL33_BASE``: Used to specify the address of a BL33 binary that has 2105e2e024bSAntonio Nino Diaz been preloaded by any other system than using the firmware. ``BL33`` isn't 2115e2e024bSAntonio Nino Diaz needed in the build command line if this option is used. Specially useful 2125e2e024bSAntonio Nino Diaz because the file ``kernel8.img`` can be loaded anywhere by modifying the file 2135e2e024bSAntonio Nino Diaz ``config.txt``. It doesn't have to contain a kernel, it could have any 2145e2e024bSAntonio Nino Diaz arbitrary payload. 2155e2e024bSAntonio Nino Diaz 2165e2e024bSAntonio Nino Diaz- ``RPI3_DIRECT_LINUX_BOOT``: Disabled by default. Set to 1 to enable the direct 2175e2e024bSAntonio Nino Diaz boot of the Linux kernel from the firmware. Option ``RPI3_PRELOADED_DTB_BASE`` 2185e2e024bSAntonio Nino Diaz is mandatory when the direct Linux kernel boot is used. Options 2195e2e024bSAntonio Nino Diaz ``PRELOADED_BL33_BASE`` will most likely be needed as well because it is 2205e2e024bSAntonio Nino Diaz unlikely that the kernel image will fit in the space reserved for BL33 images. 2215e2e024bSAntonio Nino Diaz This option can be combined with ``RPI3_BL33_IN_AARCH32`` in order to boot a 2225e2e024bSAntonio Nino Diaz 32-bit kernel. The only thing this option does is to set the arguments in 2235e2e024bSAntonio Nino Diaz registers x0-x3 or r0-r2 as expected by the kernel. 2245e2e024bSAntonio Nino Diaz 2255e2e024bSAntonio Nino Diaz- ``RPI3_PRELOADED_DTB_BASE``: Auxiliary build option needed when using 2265e2e024bSAntonio Nino Diaz ``RPI3_DIRECT_LINUX_BOOT=1``. This option allows to specify the location of a 2275e2e024bSAntonio Nino Diaz DTB in memory. 2285e2e024bSAntonio Nino Diaz 2296d5c61deSPete Batard- ``RPI3_RUNTIME_UART``: Indicates whether the UART should be used at runtime 2306d5c61deSPete Batard or disabled. ``-1`` (default) disables the runtime UART. Any other value 2316d5c61deSPete Batard enables the default UART (currently UART1) for runtime messages. 2326d5c61deSPete Batard 2334dcf1fadSPete Batard- ``RPI3_USE_UEFI_MAP``: Set to 1 to build ATF with the altername memory 2344dcf1fadSPete Batard mapping required for an UEFI firmware payload. These changes are needed 2354dcf1fadSPete Batard to be able to run Windows on ARM64. This option, which is disabled by 2364dcf1fadSPete Batard default, results in the following memory mappings: 2374dcf1fadSPete Batard 2384dcf1fadSPete Batard:: 2394dcf1fadSPete Batard 2404dcf1fadSPete Batard 0x00000000 +-----------------+ 2414dcf1fadSPete Batard | ROM | BL1 2424dcf1fadSPete Batard 0x00010000 +-----------------+ 2434dcf1fadSPete Batard | DTB | (Loaded by the VideoCore) 2444dcf1fadSPete Batard 0x00020000 +-----------------+ 2454dcf1fadSPete Batard | FIP | 2464dcf1fadSPete Batard 0x00030000 +-----------------+ 2474dcf1fadSPete Batard | | 2484dcf1fadSPete Batard | UEFI PAYLOAD | 2494dcf1fadSPete Batard | | 2504dcf1fadSPete Batard 0x00200000 +-----------------+ 2514dcf1fadSPete Batard | Secure SRAM | BL2, BL31 2524dcf1fadSPete Batard 0x00300000 +-----------------+ 2534dcf1fadSPete Batard | Secure DRAM | BL32 (Secure payload) 2544dcf1fadSPete Batard 0x00400000 +-----------------+ 2554dcf1fadSPete Batard | | 2564dcf1fadSPete Batard | | 2574dcf1fadSPete Batard | Non-secure DRAM | BL33 2584dcf1fadSPete Batard | | 2594dcf1fadSPete Batard | | 2604dcf1fadSPete Batard 0x01000000 +-----------------+ 2614dcf1fadSPete Batard | | 2624dcf1fadSPete Batard | ... | 2634dcf1fadSPete Batard | | 2644dcf1fadSPete Batard 0x3F000000 +-----------------+ 2654dcf1fadSPete Batard | I/O | 2664dcf1fadSPete Batard 2673d464234SYing-Chun Liu (PaulLiu)- ``BL32``: This port can load and run OP-TEE. The OP-TEE image is optional. 2683d464234SYing-Chun Liu (PaulLiu) Please use the code from `here <https://github.com/OP-TEE/optee_os>`__. 2693d464234SYing-Chun Liu (PaulLiu) Build the Trusted Firmware with option ``BL32=tee-header_v2.bin 2703d464234SYing-Chun Liu (PaulLiu) BL32_EXTRA1=tee-pager_v2.bin BL32_EXTRA2=tee-pageable_v2.bin`` 2713d464234SYing-Chun Liu (PaulLiu) to put the binaries into the FIP. 2723d464234SYing-Chun Liu (PaulLiu) 273e1c5026aSPaul Beesley .. warning:: 274e1c5026aSPaul Beesley If OP-TEE is used it may be needed to add the following options to the 275d35de55eSAntonio Nino Diaz Linux command line so that the USB driver doesn't use FIQs: 276d35de55eSAntonio Nino Diaz ``dwc_otg.fiq_enable=0 dwc_otg.fiq_fsm_enable=0 dwc_otg.nak_holdoff=0``. 277e1c5026aSPaul Beesley This will unfortunately reduce the performance of the USB driver. It is 278e1c5026aSPaul Beesley needed when using Raspbian, for example. 279d35de55eSAntonio Nino Diaz 2805e2e024bSAntonio Nino Diaz- ``TRUSTED_BOARD_BOOT``: This port supports TBB. Set this option to 1 to enable 2815e2e024bSAntonio Nino Diaz it. In order to use TBB, you might want to set ``GENERATE_COT=1`` to let the 2825e2e024bSAntonio Nino Diaz contents of the FIP automatically signed by the build process. The ROT key 2835e2e024bSAntonio Nino Diaz will be generated and output to ``rot_key.pem`` in the build directory. It is 2845e2e024bSAntonio Nino Diaz able to set ROT_KEY to your own key in PEM format. Also in order to build, 2855e2e024bSAntonio Nino Diaz you need to clone mbed TLS from `here <https://github.com/ARMmbed/mbedtls>`__. 2865e2e024bSAntonio Nino Diaz ``MBEDTLS_DIR`` must point at the mbed TLS source directory. 2875e2e024bSAntonio Nino Diaz 2885e2e024bSAntonio Nino Diaz- ``ENABLE_STACK_PROTECTOR``: Disabled by default. It uses the hardware RNG of 2895e2e024bSAntonio Nino Diaz the board. 290ebbdbb1fSYing-Chun Liu (PaulLiu) 2911cd4fb65SAntonio Nino DiazThe following is not currently supported: 2921cd4fb65SAntonio Nino Diaz 2934def07d5SDan Handley- AArch32 for TF-A itself. 2941cd4fb65SAntonio Nino Diaz 2951cd4fb65SAntonio Nino Diaz- ``EL3_PAYLOAD_BASE``: The reason is that you can already load anything to any 2961cd4fb65SAntonio Nino Diaz address by changing the file ``armstub8.bin``, so there's no point in using 2974def07d5SDan Handley TF-A in this case. 2981cd4fb65SAntonio Nino Diaz 2995e2e024bSAntonio Nino DiazBuilding the firmware for kernels that don't support PSCI 3005e2e024bSAntonio Nino Diaz~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3015e2e024bSAntonio Nino Diaz 3025e2e024bSAntonio Nino DiazThis is the case for the 32-bit image of Raspbian, for example. 64-bit kernels 3035e2e024bSAntonio Nino Diazalways support PSCI, but they may not know that the system understands PSCI due 3045e2e024bSAntonio Nino Diazto an incorrect DTB file. 3055e2e024bSAntonio Nino Diaz 3065e2e024bSAntonio Nino DiazFirst, clone and compile the 32-bit version of the `Raspberry Pi 3 TF-A 3075e2e024bSAntonio Nino Diazbootstrap`_. Choose the one needed for the architecture of your kernel. 3085e2e024bSAntonio Nino Diaz 3095e2e024bSAntonio Nino DiazThen compile TF-A. For a 32-bit kernel, use the following command line: 3105e2e024bSAntonio Nino Diaz 3115e2e024bSAntonio Nino Diaz.. code:: shell 3125e2e024bSAntonio Nino Diaz 313*650a435cSMark Dykes CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 3145e2e024bSAntonio Nino Diaz RPI3_BL33_IN_AARCH32=1 \ 3155e2e024bSAntonio Nino Diaz BL33=../rpi3-arm-tf-bootstrap/aarch32/el2-bootstrap.bin 3165e2e024bSAntonio Nino Diaz 3175e2e024bSAntonio Nino DiazFor a 64-bit kernel, use this other command line: 3185e2e024bSAntonio Nino Diaz 3195e2e024bSAntonio Nino Diaz.. code:: shell 3205e2e024bSAntonio Nino Diaz 321*650a435cSMark Dykes CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 3225e2e024bSAntonio Nino Diaz BL33=../rpi3-arm-tf-bootstrap/aarch64/el2-bootstrap.bin 3235e2e024bSAntonio Nino Diaz 3245e2e024bSAntonio Nino DiazHowever, enabling PSCI support in a 64-bit kernel is really easy. In the 3255e2e024bSAntonio Nino Diazrepository `Raspberry Pi 3 TF-A bootstrap`_ there is a patch that can be applied 3265e2e024bSAntonio Nino Diazto the Linux kernel tree maintained by the Raspberry Pi foundation. It modifes 3275e2e024bSAntonio Nino Diazthe DTS to tell the kernel to use PSCI. Once this patch is applied, follow the 3285e2e024bSAntonio Nino Diazinstructions in `AArch64 kernel build instructions`_ to get a working 64-bit 3295e2e024bSAntonio Nino Diazkernel image and supporting files. 3305e2e024bSAntonio Nino Diaz 3315e2e024bSAntonio Nino DiazBuilding the firmware for kernels that support PSCI 3325e2e024bSAntonio Nino Diaz~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3335e2e024bSAntonio Nino Diaz 3345e2e024bSAntonio Nino DiazFor a 64-bit kernel: 3355e2e024bSAntonio Nino Diaz 3365e2e024bSAntonio Nino Diaz.. code:: shell 3375e2e024bSAntonio Nino Diaz 338*650a435cSMark Dykes CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 33997fb05e1SAntonio Nino Diaz PRELOADED_BL33_BASE=0x02000000 \ 34097fb05e1SAntonio Nino Diaz RPI3_PRELOADED_DTB_BASE=0x01000000 \ 3415e2e024bSAntonio Nino Diaz RPI3_DIRECT_LINUX_BOOT=1 3425e2e024bSAntonio Nino Diaz 3435e2e024bSAntonio Nino DiazFor a 32-bit kernel: 3445e2e024bSAntonio Nino Diaz 3455e2e024bSAntonio Nino Diaz.. code:: shell 3465e2e024bSAntonio Nino Diaz 347*650a435cSMark Dykes CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 34897fb05e1SAntonio Nino Diaz PRELOADED_BL33_BASE=0x02000000 \ 34997fb05e1SAntonio Nino Diaz RPI3_PRELOADED_DTB_BASE=0x01000000 \ 3505e2e024bSAntonio Nino Diaz RPI3_DIRECT_LINUX_BOOT=1 \ 3515e2e024bSAntonio Nino Diaz RPI3_BL33_IN_AARCH32=1 3525e2e024bSAntonio Nino Diaz 3531cd4fb65SAntonio Nino DiazAArch64 kernel build instructions 3541cd4fb65SAntonio Nino Diaz--------------------------------- 3551cd4fb65SAntonio Nino Diaz 3561cd4fb65SAntonio Nino DiazThe following instructions show how to install and run a AArch64 kernel by 3571cd4fb65SAntonio Nino Diazusing a SD card with the default `Raspbian`_ install as base. Skip them if you 3581cd4fb65SAntonio Nino Diazwant to use the default 32-bit kernel. 3591cd4fb65SAntonio Nino Diaz 3601cd4fb65SAntonio Nino DiazNote that this system won't be fully 64-bit because all the tools in the 3611cd4fb65SAntonio Nino Diazfilesystem are 32-bit binaries, but it's a quick way to get it working, and it 3621cd4fb65SAntonio Nino Diazallows the user to run 64-bit binaries in addition to 32-bit binaries. 3631cd4fb65SAntonio Nino Diaz 3641cd4fb65SAntonio Nino Diaz1. Clone the `Linux tree fork`_ maintained by the Raspberry Pi Foundation. To 3651cd4fb65SAntonio Nino Diaz speed things up, do a shallow clone of the desired branch. 3661cd4fb65SAntonio Nino Diaz 3671cd4fb65SAntonio Nino Diaz.. code:: shell 3681cd4fb65SAntonio Nino Diaz 3695e2e024bSAntonio Nino Diaz git clone --depth=1 -b rpi-4.18.y https://github.com/raspberrypi/linux 3701cd4fb65SAntonio Nino Diaz cd linux 3711cd4fb65SAntonio Nino Diaz 3721cd4fb65SAntonio Nino Diaz2. Configure and compile the kernel. Adapt the number after ``-j`` so that it is 3731cd4fb65SAntonio Nino Diaz 1.5 times the number of CPUs in your computer. This may take some time to 3741cd4fb65SAntonio Nino Diaz finish. 3751cd4fb65SAntonio Nino Diaz 3761cd4fb65SAntonio Nino Diaz.. code:: shell 3771cd4fb65SAntonio Nino Diaz 3781cd4fb65SAntonio Nino Diaz make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig 3791cd4fb65SAntonio Nino Diaz make -j 6 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- 3801cd4fb65SAntonio Nino Diaz 3811cd4fb65SAntonio Nino Diaz3. Copy the kernel image and the device tree to the SD card. Replace the path 3821cd4fb65SAntonio Nino Diaz by the corresponding path in your computers to the ``boot`` partition of the 3831cd4fb65SAntonio Nino Diaz SD card. 3841cd4fb65SAntonio Nino Diaz 3851cd4fb65SAntonio Nino Diaz.. code:: shell 3861cd4fb65SAntonio Nino Diaz 3871cd4fb65SAntonio Nino Diaz cp arch/arm64/boot/Image /path/to/boot/kernel8.img 3881cd4fb65SAntonio Nino Diaz cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb /path/to/boot/ 3895e2e024bSAntonio Nino Diaz cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b-plus.dtb /path/to/boot/ 3901cd4fb65SAntonio Nino Diaz 3911cd4fb65SAntonio Nino Diaz4. Install the kernel modules. Replace the path by the corresponding path to the 3921cd4fb65SAntonio Nino Diaz filesystem partition of the SD card on your computer. 3931cd4fb65SAntonio Nino Diaz 3941cd4fb65SAntonio Nino Diaz.. code:: shell 3951cd4fb65SAntonio Nino Diaz 3961cd4fb65SAntonio Nino Diaz make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \ 3971cd4fb65SAntonio Nino Diaz INSTALL_MOD_PATH=/path/to/filesystem modules_install 3981cd4fb65SAntonio Nino Diaz 3991cd4fb65SAntonio Nino Diaz5. Follow the instructions in `Setup SD card`_ except for the step of renaming 4001cd4fb65SAntonio Nino Diaz the existing ``kernel7.img`` (we have already copied a AArch64 kernel). 4011cd4fb65SAntonio Nino Diaz 4021cd4fb65SAntonio Nino DiazSetup SD card 4031cd4fb65SAntonio Nino Diaz------------- 4041cd4fb65SAntonio Nino Diaz 4051cd4fb65SAntonio Nino DiazThe instructions assume that you have an SD card with a fresh install of 4061cd4fb65SAntonio Nino Diaz`Raspbian`_ (or that, at least, the ``boot`` partition is untouched, or nearly 407e0f21f62SAntonio Nino Diazuntouched). They have been tested with the image available in 2018-03-13. 4081cd4fb65SAntonio Nino Diaz 4091cd4fb65SAntonio Nino Diaz1. Insert the SD card and open the ``boot`` partition. 4101cd4fb65SAntonio Nino Diaz 4111cd4fb65SAntonio Nino Diaz2. Rename ``kernel7.img`` to ``kernel8.img``. This tricks the VideoCore 4124def07d5SDan Handley bootloader into booting the Arm cores in AArch64 mode, like TF-A needs, 4134def07d5SDan Handley even though the kernel is not compiled for AArch64. 4141cd4fb65SAntonio Nino Diaz 4151cd4fb65SAntonio Nino Diaz3. Copy ``armstub8.bin`` here. When ``kernel8.img`` is available, The VideoCore 4161cd4fb65SAntonio Nino Diaz bootloader will look for a file called ``armstub8.bin`` and load it at 4171cd4fb65SAntonio Nino Diaz address **0x0** instead of a predefined one. 4181cd4fb65SAntonio Nino Diaz 4195341b42eSAntonio Nino Diaz4. To enable the serial port "Mini UART" in Linux, open ``cmdline.txt`` and add 4201cd4fb65SAntonio Nino Diaz ``console=serial0,115200 console=tty1``. 4211cd4fb65SAntonio Nino Diaz 4221cd4fb65SAntonio Nino Diaz5. Open ``config.txt`` and add the following lines at the end (``enable_uart=1`` 4231cd4fb65SAntonio Nino Diaz is only needed to enable debugging through the Mini UART): 4241cd4fb65SAntonio Nino Diaz 4251cd4fb65SAntonio Nino Diaz:: 4261cd4fb65SAntonio Nino Diaz 4271cd4fb65SAntonio Nino Diaz enable_uart=1 42897fb05e1SAntonio Nino Diaz kernel_address=0x02000000 42997fb05e1SAntonio Nino Diaz device_tree_address=0x01000000 4301cd4fb65SAntonio Nino Diaz 4311cd4fb65SAntonio Nino DiazIf you connect a serial cable to the Mini UART and your computer, and connect 4321cd4fb65SAntonio Nino Diazto it (for example, with ``screen /dev/ttyUSB0 115200``) you should see some 4331cd4fb65SAntonio Nino Diaztext. In the case of an AArch32 kernel, you should see something like this: 4341cd4fb65SAntonio Nino Diaz 4351cd4fb65SAntonio Nino Diaz:: 4361cd4fb65SAntonio Nino Diaz 4371cd4fb65SAntonio Nino Diaz NOTICE: Booting Trusted Firmware 4381cd4fb65SAntonio Nino Diaz NOTICE: BL1: v1.4(release):v1.4-329-g61e94684-dirty 4391cd4fb65SAntonio Nino Diaz NOTICE: BL1: Built : 00:09:25, Nov 6 2017 4401cd4fb65SAntonio Nino Diaz NOTICE: BL1: Booting BL2 4411cd4fb65SAntonio Nino Diaz NOTICE: BL2: v1.4(release):v1.4-329-g61e94684-dirty 4421cd4fb65SAntonio Nino Diaz NOTICE: BL2: Built : 00:09:25, Nov 6 2017 4431cd4fb65SAntonio Nino Diaz NOTICE: BL1: Booting BL31 4441cd4fb65SAntonio Nino Diaz NOTICE: BL31: v1.4(release):v1.4-329-g61e94684-dirty 4451cd4fb65SAntonio Nino Diaz NOTICE: BL31: Built : 00:09:25, Nov 6 2017 4461cd4fb65SAntonio Nino Diaz [ 0.266484] bcm2835-aux-uart 3f215040.serial: could not get clk: -517 4471cd4fb65SAntonio Nino Diaz 4481cd4fb65SAntonio Nino Diaz Raspbian GNU/Linux 9 raspberrypi ttyS0 4491cd4fb65SAntonio Nino Diaz raspberrypi login: 4501cd4fb65SAntonio Nino Diaz 4511cd4fb65SAntonio Nino DiazJust enter your credentials, everything should work as expected. Note that the 4521cd4fb65SAntonio Nino DiazHDMI output won't show any text during boot. 4531cd4fb65SAntonio Nino Diaz 4541cd4fb65SAntonio Nino Diaz.. _default Arm stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub7.S 4551cd4fb65SAntonio Nino Diaz.. _default AArch64 stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub8.S 4561cd4fb65SAntonio Nino Diaz.. _Linux kernel tree: https://github.com/torvalds/linux 4571cd4fb65SAntonio Nino Diaz.. _Linux tree fork: https://github.com/raspberrypi/linux 4581cd4fb65SAntonio Nino Diaz.. _Raspberry Pi 3: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/ 4594def07d5SDan Handley.. _Raspberry Pi 3 TF-A bootstrap: https://github.com/AntonioND/rpi3-arm-tf-bootstrap 4601cd4fb65SAntonio Nino Diaz.. _Raspberry Pi 3 documentation: https://www.raspberrypi.org/documentation/ 4611cd4fb65SAntonio Nino Diaz.. _Raspbian: https://www.raspberrypi.org/downloads/raspbian/ 462