xref: /rk3399_ARM-atf/docs/plat/rpi3.rst (revision aa49bde8a3e8171fbe25751b4673624c80134f59)
14def07d5SDan HandleyTrusted Firmware-A for Raspberry Pi 3
24def07d5SDan Handley=====================================
31cd4fb65SAntonio Nino Diaz
41cd4fb65SAntonio Nino Diaz.. section-numbering::
51cd4fb65SAntonio Nino Diaz    :suffix: .
61cd4fb65SAntonio Nino Diaz
71cd4fb65SAntonio Nino Diaz.. contents::
81cd4fb65SAntonio Nino Diaz
91cd4fb65SAntonio Nino DiazThe `Raspberry Pi 3`_ is an inexpensive single-board computer that contains four
105e2e024bSAntonio Nino DiazArm Cortex-A53 cores.
111cd4fb65SAntonio Nino Diaz
124def07d5SDan HandleyThe following instructions explain how to use this port of the TF-A with the
134def07d5SDan Handleydefault distribution of `Raspbian`_ because that's the distribution officially
144def07d5SDan Handleysupported by the Raspberry Pi Foundation. At the moment of writing this, the
154def07d5SDan Handleyofficially supported kernel is a AArch32 kernel. This doesn't mean that this
164def07d5SDan Handleyport of TF-A can't boot a AArch64 kernel. The `Linux tree fork`_ maintained by
174def07d5SDan Handleythe Foundation can be compiled for AArch64 by following the steps in
184def07d5SDan Handley`AArch64 kernel build instructions`_.
191cd4fb65SAntonio Nino Diaz
201cd4fb65SAntonio Nino Diaz**IMPORTANT NOTE**: This port isn't secure. All of the memory used is DRAM,
211cd4fb65SAntonio Nino Diazwhich is available from both the Non-secure and Secure worlds. This port
221cd4fb65SAntonio Nino Diazshouldn't be considered more than a prototype to play with and implement
231cd4fb65SAntonio Nino Diazelements like PSCI to support the Linux kernel.
241cd4fb65SAntonio Nino Diaz
251cd4fb65SAntonio Nino DiazDesign
261cd4fb65SAntonio Nino Diaz------
271cd4fb65SAntonio Nino Diaz
281cd4fb65SAntonio Nino DiazThe SoC used by the Raspberry Pi 3 is the Broadcom BCM2837. It is a SoC with a
291cd4fb65SAntonio Nino DiazVideoCore IV that acts as primary processor (and loads everything from the SD
301cd4fb65SAntonio Nino Diazcard) and is located between all Arm cores and the DRAM. Check the `Raspberry Pi
311cd4fb65SAntonio Nino Diaz3 documentation`_ for more information.
321cd4fb65SAntonio Nino Diaz
331cd4fb65SAntonio Nino DiazThis explains why it is possible to change the execution state (AArch64/AArch32)
341cd4fb65SAntonio Nino Diazdepending on a few files on the SD card. We only care about the cases in which
351cd4fb65SAntonio Nino Diazthe cores boot in AArch64 mode.
361cd4fb65SAntonio Nino Diaz
371cd4fb65SAntonio Nino DiazThe rules are simple:
381cd4fb65SAntonio Nino Diaz
391cd4fb65SAntonio Nino Diaz- If a file called ``kernel8.img`` is located on the ``boot`` partition of the
401cd4fb65SAntonio Nino Diaz  SD card, it will load it and execute in EL2 in AArch64. Basically, it executes
411cd4fb65SAntonio Nino Diaz  a `default AArch64 stub`_ at address **0x0** that jumps to the kernel.
421cd4fb65SAntonio Nino Diaz
431cd4fb65SAntonio Nino Diaz- If there is also a file called ``armstub8.bin``, it will load it at address
441cd4fb65SAntonio Nino Diaz  **0x0** (instead of the default stub) and execute it in EL3 in AArch64. All
451cd4fb65SAntonio Nino Diaz  the cores are powered on at the same time and start at address **0x0**.
461cd4fb65SAntonio Nino Diaz
471cd4fb65SAntonio Nino DiazThis means that we can use the default AArch32 kernel provided in the official
484def07d5SDan Handley`Raspbian`_ distribution by renaming it to ``kernel8.img``, while TF-A and
494def07d5SDan Handleyanything else we need is in ``armstub8.bin``. This way we can forget about the
504def07d5SDan Handleydefault bootstrap code. When using a AArch64 kernel, it is only needed to make
514def07d5SDan Handleysure that the name on the SD card is ``kernel8.img``.
521cd4fb65SAntonio Nino Diaz
531cd4fb65SAntonio Nino DiazIdeally, we want to load the kernel and have all cores available, which means
541cd4fb65SAntonio Nino Diazthat we need to make the secondary cores work in the way the kernel expects, as
551cd4fb65SAntonio Nino Diazexplained in `Secondary cores`_. In practice, a small bootstrap is needed
564def07d5SDan Handleybetween TF-A and the kernel.
571cd4fb65SAntonio Nino Diaz
581cd4fb65SAntonio Nino DiazTo get the most out of a AArch32 kernel, we want to boot it in Hypervisor mode
591cd4fb65SAntonio Nino Diazin AArch32. This means that BL33 can't be in EL2 in AArch64 mode. The
601cd4fb65SAntonio Nino Diazarchitecture specifies that AArch32 Hypervisor mode isn't present when AArch64
611cd4fb65SAntonio Nino Diazis used for EL2. When using a AArch64 kernel, it should simply start in EL2.
621cd4fb65SAntonio Nino Diaz
631cd4fb65SAntonio Nino DiazPlacement of images
641cd4fb65SAntonio Nino Diaz~~~~~~~~~~~~~~~~~~~
651cd4fb65SAntonio Nino Diaz
661cd4fb65SAntonio Nino DiazThe file ``armstub8.bin`` contains BL1 and the FIP. It is needed to add padding
671cd4fb65SAntonio Nino Diazbetween them so that the addresses they are loaded to match the ones specified
685e2e024bSAntonio Nino Diazwhen compiling TF-A. This is done automatically by the build system.
691cd4fb65SAntonio Nino Diaz
701cd4fb65SAntonio Nino DiazThe device tree block is loaded by the VideoCore loader from an appropriate
711cd4fb65SAntonio Nino Diazfile, but we can specify the address it is loaded to in ``config.txt``.
721cd4fb65SAntonio Nino Diaz
731cd4fb65SAntonio Nino DiazThe file ``kernel8.img`` contains a kernel image that is loaded to the address
741cd4fb65SAntonio Nino Diazspecified in ``config.txt``. The `Linux kernel tree`_ has information about how
751cd4fb65SAntonio Nino Diaza AArch32 Linux kernel image is loaded in ``Documentation/arm/Booting``:
761cd4fb65SAntonio Nino Diaz
771cd4fb65SAntonio Nino Diaz::
781cd4fb65SAntonio Nino Diaz
791cd4fb65SAntonio Nino Diaz    The zImage may also be placed in system RAM and called there.  The
801cd4fb65SAntonio Nino Diaz    kernel should be placed in the first 128MiB of RAM.  It is recommended
811cd4fb65SAntonio Nino Diaz    that it is loaded above 32MiB in order to avoid the need to relocate
821cd4fb65SAntonio Nino Diaz    prior to decompression, which will make the boot process slightly
831cd4fb65SAntonio Nino Diaz    faster.
841cd4fb65SAntonio Nino Diaz
851cd4fb65SAntonio Nino DiazThere are no similar restrictions for AArch64 kernels, as specified in the file
861cd4fb65SAntonio Nino Diaz``Documentation/arm64/booting.txt``.
871cd4fb65SAntonio Nino Diaz
881cd4fb65SAntonio Nino DiazThis means that we need to avoid the first 128 MiB of RAM when placing the
894def07d5SDan HandleyTF-A images (and specially the first 32 MiB, as they are directly used to
904def07d5SDan Handleyplace the uncompressed AArch32 kernel image. This way, both AArch32 and
911cd4fb65SAntonio Nino DiazAArch64 kernels can be placed at the same address.
921cd4fb65SAntonio Nino Diaz
931cd4fb65SAntonio Nino DiazIn the end, the images look like the following diagram when placed in memory.
941cd4fb65SAntonio Nino DiazAll addresses are Physical Addresses from the point of view of the Arm cores.
951cd4fb65SAntonio Nino DiazAgain, note that this is all just part of the same DRAM that goes from
961cd4fb65SAntonio Nino Diaz**0x00000000** to **0x3F000000**, it just has different names to simulate a real
971cd4fb65SAntonio Nino Diazsecure platform!
981cd4fb65SAntonio Nino Diaz
991cd4fb65SAntonio Nino Diaz::
1001cd4fb65SAntonio Nino Diaz
1011cd4fb65SAntonio Nino Diaz    0x00000000 +-----------------+
1021cd4fb65SAntonio Nino Diaz               |       ROM       | BL1
1036a2c3c7fSYing-Chun Liu (PaulLiu)    0x00020000 +-----------------+
1041cd4fb65SAntonio Nino Diaz               |       FIP       |
1051cd4fb65SAntonio Nino Diaz    0x00200000 +-----------------+
1061cd4fb65SAntonio Nino Diaz               |                 |
1071cd4fb65SAntonio Nino Diaz               |       ...       |
1081cd4fb65SAntonio Nino Diaz               |                 |
1091cd4fb65SAntonio Nino Diaz    0x01000000 +-----------------+
11097fb05e1SAntonio Nino Diaz               |       DTB       | (Loaded by the VideoCore)
1111cd4fb65SAntonio Nino Diaz               +-----------------+
1121cd4fb65SAntonio Nino Diaz               |                 |
1131cd4fb65SAntonio Nino Diaz               |       ...       |
1141cd4fb65SAntonio Nino Diaz               |                 |
1151cd4fb65SAntonio Nino Diaz    0x02000000 +-----------------+
11697fb05e1SAntonio Nino Diaz               |     Kernel      | (Loaded by the VideoCore)
1171cd4fb65SAntonio Nino Diaz               +-----------------+
1181cd4fb65SAntonio Nino Diaz               |                 |
1191cd4fb65SAntonio Nino Diaz               |       ...       |
1201cd4fb65SAntonio Nino Diaz               |                 |
1211cd4fb65SAntonio Nino Diaz    0x10000000 +-----------------+
1221cd4fb65SAntonio Nino Diaz               |   Secure SRAM   | BL2, BL31
1231cd4fb65SAntonio Nino Diaz    0x10100000 +-----------------+
1243d464234SYing-Chun Liu (PaulLiu)               |   Secure DRAM   | BL32 (Secure payload)
1251cd4fb65SAntonio Nino Diaz    0x11000000 +-----------------+
126*aa49bde8SAntonio Nino Diaz               | Non-secure DRAM | BL33
127*aa49bde8SAntonio Nino Diaz               +-----------------+
1281cd4fb65SAntonio Nino Diaz               |                 |
1291cd4fb65SAntonio Nino Diaz               |       ...       |
1301cd4fb65SAntonio Nino Diaz               |                 |
1311cd4fb65SAntonio Nino Diaz    0x3F000000 +-----------------+
1321cd4fb65SAntonio Nino Diaz               |       I/O       |
1331cd4fb65SAntonio Nino Diaz    0x40000000 +-----------------+
1341cd4fb65SAntonio Nino Diaz
13597fb05e1SAntonio Nino DiazThe area between **0x10000000** and **0x11000000** has to be manually protected
13697fb05e1SAntonio Nino Diazso that the kernel doesn't use it. That is done by adding ``memmap=16M$256M`` to
13797fb05e1SAntonio Nino Diazthe command line passed to the kernel. See the `Setup SD card`_ instructions to
13897fb05e1SAntonio Nino Diazsee how to do it.
1391cd4fb65SAntonio Nino Diaz
1401cd4fb65SAntonio Nino DiazThe last 16 MiB of DRAM can only be accessed by the VideoCore, that has
1411cd4fb65SAntonio Nino Diazdifferent mappings than the Arm cores in which the I/O addresses don't overlap
1421cd4fb65SAntonio Nino Diazthe DRAM. The memory reserved to be used by the VideoCore is always placed at
1431cd4fb65SAntonio Nino Diazthe end of the DRAM, so this space isn't wasted.
1441cd4fb65SAntonio Nino Diaz
1454def07d5SDan HandleyConsidering the 128 MiB allocated to the GPU and the 16 MiB allocated for
1464def07d5SDan HandleyTF-A, there are 880 MiB available for Linux.
1471cd4fb65SAntonio Nino Diaz
1481cd4fb65SAntonio Nino DiazBoot sequence
1491cd4fb65SAntonio Nino Diaz~~~~~~~~~~~~~
1501cd4fb65SAntonio Nino Diaz
1514def07d5SDan HandleyThe boot sequence of TF-A is the usual one except when booting an AArch32
1524def07d5SDan Handleykernel. In that case, BL33 is booted in AArch32 Hypervisor mode so that it
1534def07d5SDan Handleycan jump to the kernel in the same mode and let it take over that privilege
1544def07d5SDan Handleylevel. If BL33 was running in EL2 in AArch64 (as in the default bootflow of
1554def07d5SDan HandleyTF-A) it could only jump to the kernel in AArch32 in Supervisor mode.
1561cd4fb65SAntonio Nino Diaz
1571cd4fb65SAntonio Nino DiazThe `Linux kernel tree`_ has instructions on how to jump to the Linux kernel
1581cd4fb65SAntonio Nino Diazin ``Documentation/arm/Booting`` and ``Documentation/arm64/booting.txt``. The
1591cd4fb65SAntonio Nino Diazbootstrap should take care of this.
1601cd4fb65SAntonio Nino Diaz
1615e2e024bSAntonio Nino DiazThis port support a direct boot of the Linux kernel from the firmware (as a BL33
1625e2e024bSAntonio Nino Diazimage). Alternatively, U-Boot or other bootloaders may be used.
1635e2e024bSAntonio Nino Diaz
1641cd4fb65SAntonio Nino DiazSecondary cores
1651cd4fb65SAntonio Nino Diaz~~~~~~~~~~~~~~~
1661cd4fb65SAntonio Nino Diaz
1675e2e024bSAntonio Nino DiazThis port of the Trusted Firmware-A supports ``PSCI_CPU_ON``,
1685e2e024bSAntonio Nino Diaz`PSCI_SYSTEM_RESET`` and ``PSCI_SYSTEM_OFF``. The last one doesn't really turn
1695e2e024bSAntonio Nino Diazthe system off, it simply reboots it and asks the VideoCore firmware to keep it
1705e2e024bSAntonio Nino Diazin a low power mode permanently.
1715e2e024bSAntonio Nino Diaz
1721cd4fb65SAntonio Nino DiazThe kernel used by `Raspbian`_ doesn't have support for PSCI, so it is needed to
1731cd4fb65SAntonio Nino Diazuse mailboxes to trap the secondary cores until they are ready to jump to the
1741cd4fb65SAntonio Nino Diazkernel. This mailbox is located at a different address in the AArch32 default
1751cd4fb65SAntonio Nino Diazkernel than in the AArch64 kernel.
1761cd4fb65SAntonio Nino Diaz
1775e2e024bSAntonio Nino DiazKernels with PSCI support can use the PSCI calls instead for a cleaner boot.
1785e2e024bSAntonio Nino Diaz
1794def07d5SDan HandleyAlso, this port of TF-A has another Trusted Mailbox in Shared BL RAM. During
1804def07d5SDan Handleycold boot, all secondary cores wait in a loop until they are given given an
1814def07d5SDan Handleyaddress to jump to in this Mailbox (``bl31_warm_entrypoint``).
1821cd4fb65SAntonio Nino Diaz
1831cd4fb65SAntonio Nino DiazOnce BL31 has finished and the primary core has jumped to the BL33 payload, it
1841cd4fb65SAntonio Nino Diazhas to call ``PSCI_CPU_ON`` to release the secondary CPUs from the wait loop.
1851cd4fb65SAntonio Nino DiazThe payload then makes them wait in another waitloop listening from messages
1861cd4fb65SAntonio Nino Diazfrom the kernel. When the primary CPU jumps into the kernel, it will send an
1871cd4fb65SAntonio Nino Diazaddress to the mailbox so that the secondary CPUs jump to it and are recognised
1881cd4fb65SAntonio Nino Diazby the kernel.
1891cd4fb65SAntonio Nino Diaz
1901cd4fb65SAntonio Nino DiazBuild Instructions
1911cd4fb65SAntonio Nino Diaz------------------
1921cd4fb65SAntonio Nino Diaz
1931cd4fb65SAntonio Nino DiazTo boot a AArch64 kernel, only the AArch64 toolchain is required.
1941cd4fb65SAntonio Nino Diaz
1951cd4fb65SAntonio Nino DiazTo boot a AArch32 kernel, both AArch64 and AArch32 toolchains are required. The
1961cd4fb65SAntonio Nino DiazAArch32 toolchain is needed for the AArch32 bootstrap needed to load a 32-bit
1971cd4fb65SAntonio Nino Diazkernel.
1981cd4fb65SAntonio Nino Diaz
19964fe343cSAntonio Nino DiazThe build system concatenates BL1 and the FIP so that the addresses match the
20064fe343cSAntonio Nino Diazones in the memory map. The resulting file is ``armstub8.bin``, located in the
2015e2e024bSAntonio Nino Diazbuild folder (e.g. ``build/rpi3/debug/armstub8.bin``). To know how to use this
2025e2e024bSAntonio Nino Diazfile, follow the instructions in `Setup SD card`_.
2031cd4fb65SAntonio Nino Diaz
2041cd4fb65SAntonio Nino DiazThe following build options are supported:
2051cd4fb65SAntonio Nino Diaz
2061cd4fb65SAntonio Nino Diaz- ``RPI3_BL33_IN_AARCH32``: This port can load a AArch64 or AArch32 BL33 image.
2074def07d5SDan Handley  By default this option is 0, which means that TF-A will jump to BL33 in EL2
2084def07d5SDan Handley  in AArch64 mode. If set to 1, it will jump to BL33 in Hypervisor in AArch32
2094def07d5SDan Handley  mode.
2101cd4fb65SAntonio Nino Diaz
2115e2e024bSAntonio Nino Diaz- ``PRELOADED_BL33_BASE``: Used to specify the address of a BL33 binary that has
2125e2e024bSAntonio Nino Diaz  been preloaded by any other system than using the firmware. ``BL33`` isn't
2135e2e024bSAntonio Nino Diaz  needed in the build command line if this option is used. Specially useful
2145e2e024bSAntonio Nino Diaz  because the file ``kernel8.img`` can be loaded anywhere by modifying the file
2155e2e024bSAntonio Nino Diaz  ``config.txt``. It doesn't have to contain a kernel, it could have any
2165e2e024bSAntonio Nino Diaz  arbitrary payload.
2175e2e024bSAntonio Nino Diaz
2185e2e024bSAntonio Nino Diaz- ``RPI3_DIRECT_LINUX_BOOT``: Disabled by default. Set to 1 to enable the direct
2195e2e024bSAntonio Nino Diaz  boot of the Linux kernel from the firmware. Option ``RPI3_PRELOADED_DTB_BASE``
2205e2e024bSAntonio Nino Diaz  is mandatory when the direct Linux kernel boot is used. Options
2215e2e024bSAntonio Nino Diaz  ``PRELOADED_BL33_BASE`` will most likely be needed as well because it is
2225e2e024bSAntonio Nino Diaz  unlikely that the kernel image will fit in the space reserved for BL33 images.
2235e2e024bSAntonio Nino Diaz  This option can be combined with ``RPI3_BL33_IN_AARCH32`` in order to boot a
2245e2e024bSAntonio Nino Diaz  32-bit kernel. The only thing this option does is to set the arguments in
2255e2e024bSAntonio Nino Diaz  registers x0-x3 or r0-r2 as expected by the kernel.
2265e2e024bSAntonio Nino Diaz
2275e2e024bSAntonio Nino Diaz- ``RPI3_PRELOADED_DTB_BASE``: Auxiliary build option needed when using
2285e2e024bSAntonio Nino Diaz  ``RPI3_DIRECT_LINUX_BOOT=1``. This option allows to specify the location of a
2295e2e024bSAntonio Nino Diaz  DTB in memory.
2305e2e024bSAntonio Nino Diaz
2313d464234SYing-Chun Liu (PaulLiu)- ``BL32``: This port can load and run OP-TEE. The OP-TEE image is optional.
2323d464234SYing-Chun Liu (PaulLiu)  Please use the code from `here <https://github.com/OP-TEE/optee_os>`__.
2333d464234SYing-Chun Liu (PaulLiu)  Build the Trusted Firmware with option ``BL32=tee-header_v2.bin
2343d464234SYing-Chun Liu (PaulLiu)  BL32_EXTRA1=tee-pager_v2.bin  BL32_EXTRA2=tee-pageable_v2.bin``
2353d464234SYing-Chun Liu (PaulLiu)  to put the binaries into the FIP.
2363d464234SYing-Chun Liu (PaulLiu)
237d35de55eSAntonio Nino Diaz  Note: If OP-TEE is used it may be needed to add the following options to the
238d35de55eSAntonio Nino Diaz  Linux command line so that the USB driver doesn't use FIQs:
239d35de55eSAntonio Nino Diaz  ``dwc_otg.fiq_enable=0 dwc_otg.fiq_fsm_enable=0 dwc_otg.nak_holdoff=0``.
240d35de55eSAntonio Nino Diaz  This will unfortunately reduce the performance of the USB driver. It is needed
241d35de55eSAntonio Nino Diaz  when using Raspbian, for example.
242d35de55eSAntonio Nino Diaz
2435e2e024bSAntonio Nino Diaz- ``TRUSTED_BOARD_BOOT``: This port supports TBB. Set this option to 1 to enable
2445e2e024bSAntonio Nino Diaz  it. In order to use TBB, you might want to set ``GENERATE_COT=1`` to let the
2455e2e024bSAntonio Nino Diaz  contents of the FIP automatically signed by the build process. The ROT key
2465e2e024bSAntonio Nino Diaz  will be generated and output to ``rot_key.pem`` in the build directory. It is
2475e2e024bSAntonio Nino Diaz  able to set ROT_KEY to your own key in PEM format.  Also in order to build,
2485e2e024bSAntonio Nino Diaz  you need to clone mbed TLS from `here <https://github.com/ARMmbed/mbedtls>`__.
2495e2e024bSAntonio Nino Diaz  ``MBEDTLS_DIR`` must point at the mbed TLS source directory.
2505e2e024bSAntonio Nino Diaz
2515e2e024bSAntonio Nino Diaz- ``ENABLE_STACK_PROTECTOR``: Disabled by default. It uses the hardware RNG of
2525e2e024bSAntonio Nino Diaz  the board.
253ebbdbb1fSYing-Chun Liu (PaulLiu)
2541cd4fb65SAntonio Nino DiazThe following is not currently supported:
2551cd4fb65SAntonio Nino Diaz
2564def07d5SDan Handley- AArch32 for TF-A itself.
2571cd4fb65SAntonio Nino Diaz
2581cd4fb65SAntonio Nino Diaz- ``EL3_PAYLOAD_BASE``: The reason is that you can already load anything to any
2591cd4fb65SAntonio Nino Diaz  address by changing the file ``armstub8.bin``, so there's no point in using
2604def07d5SDan Handley  TF-A in this case.
2611cd4fb65SAntonio Nino Diaz
262e0f21f62SAntonio Nino Diaz- ``MULTI_CONSOLE_API=0``: The multi console API must be enabled. Note that the
263e0f21f62SAntonio Nino Diaz  crash console uses the internal 16550 driver functions directly in order to be
264e0f21f62SAntonio Nino Diaz  able to print error messages during early crashes before setting up the
265e0f21f62SAntonio Nino Diaz  multi console API.
266e0f21f62SAntonio Nino Diaz
2675e2e024bSAntonio Nino DiazBuilding the firmware for kernels that don't support PSCI
2685e2e024bSAntonio Nino Diaz~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2695e2e024bSAntonio Nino Diaz
2705e2e024bSAntonio Nino DiazThis is the case for the 32-bit image of Raspbian, for example. 64-bit kernels
2715e2e024bSAntonio Nino Diazalways support PSCI, but they may not know that the system understands PSCI due
2725e2e024bSAntonio Nino Diazto an incorrect DTB file.
2735e2e024bSAntonio Nino Diaz
2745e2e024bSAntonio Nino DiazFirst, clone and compile the 32-bit version of the `Raspberry Pi 3 TF-A
2755e2e024bSAntonio Nino Diazbootstrap`_. Choose the one needed for the architecture of your kernel.
2765e2e024bSAntonio Nino Diaz
2775e2e024bSAntonio Nino DiazThen compile TF-A. For a 32-bit kernel, use the following command line:
2785e2e024bSAntonio Nino Diaz
2795e2e024bSAntonio Nino Diaz.. code:: shell
2805e2e024bSAntonio Nino Diaz
2815e2e024bSAntonio Nino Diaz    CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3             \
2825e2e024bSAntonio Nino Diaz    RPI3_BL33_IN_AARCH32=1                                      \
2835e2e024bSAntonio Nino Diaz    BL33=../rpi3-arm-tf-bootstrap/aarch32/el2-bootstrap.bin
2845e2e024bSAntonio Nino Diaz
2855e2e024bSAntonio Nino DiazFor a 64-bit kernel, use this other command line:
2865e2e024bSAntonio Nino Diaz
2875e2e024bSAntonio Nino Diaz.. code:: shell
2885e2e024bSAntonio Nino Diaz
2895e2e024bSAntonio Nino Diaz    CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3             \
2905e2e024bSAntonio Nino Diaz    BL33=../rpi3-arm-tf-bootstrap/aarch64/el2-bootstrap.bin
2915e2e024bSAntonio Nino Diaz
2925e2e024bSAntonio Nino DiazHowever, enabling PSCI support in a 64-bit kernel is really easy. In the
2935e2e024bSAntonio Nino Diazrepository `Raspberry Pi 3 TF-A bootstrap`_ there is a patch that can be applied
2945e2e024bSAntonio Nino Diazto the Linux kernel tree maintained by the Raspberry Pi foundation. It modifes
2955e2e024bSAntonio Nino Diazthe DTS to tell the kernel to use PSCI. Once this patch is applied, follow the
2965e2e024bSAntonio Nino Diazinstructions in `AArch64 kernel build instructions`_ to get a working 64-bit
2975e2e024bSAntonio Nino Diazkernel image and supporting files.
2985e2e024bSAntonio Nino Diaz
2995e2e024bSAntonio Nino DiazBuilding the firmware for kernels that support PSCI
3005e2e024bSAntonio Nino Diaz~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3015e2e024bSAntonio Nino Diaz
3025e2e024bSAntonio Nino DiazFor a 64-bit kernel:
3035e2e024bSAntonio Nino Diaz
3045e2e024bSAntonio Nino Diaz.. code:: shell
3055e2e024bSAntonio Nino Diaz
3065e2e024bSAntonio Nino Diaz    CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3             \
30797fb05e1SAntonio Nino Diaz    PRELOADED_BL33_BASE=0x02000000                              \
30897fb05e1SAntonio Nino Diaz    RPI3_PRELOADED_DTB_BASE=0x01000000                          \
3095e2e024bSAntonio Nino Diaz    RPI3_DIRECT_LINUX_BOOT=1
3105e2e024bSAntonio Nino Diaz
3115e2e024bSAntonio Nino DiazFor a 32-bit kernel:
3125e2e024bSAntonio Nino Diaz
3135e2e024bSAntonio Nino Diaz.. code:: shell
3145e2e024bSAntonio Nino Diaz
3155e2e024bSAntonio Nino Diaz    CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3             \
31697fb05e1SAntonio Nino Diaz    PRELOADED_BL33_BASE=0x02000000                              \
31797fb05e1SAntonio Nino Diaz    RPI3_PRELOADED_DTB_BASE=0x01000000                          \
3185e2e024bSAntonio Nino Diaz    RPI3_DIRECT_LINUX_BOOT=1                                    \
3195e2e024bSAntonio Nino Diaz    RPI3_BL33_IN_AARCH32=1
3205e2e024bSAntonio Nino Diaz
3211cd4fb65SAntonio Nino DiazAArch64 kernel build instructions
3221cd4fb65SAntonio Nino Diaz---------------------------------
3231cd4fb65SAntonio Nino Diaz
3241cd4fb65SAntonio Nino DiazThe following instructions show how to install and run a AArch64 kernel by
3251cd4fb65SAntonio Nino Diazusing a SD card with the default `Raspbian`_ install as base. Skip them if you
3261cd4fb65SAntonio Nino Diazwant to use the default 32-bit kernel.
3271cd4fb65SAntonio Nino Diaz
3281cd4fb65SAntonio Nino DiazNote that this system won't be fully 64-bit because all the tools in the
3291cd4fb65SAntonio Nino Diazfilesystem are 32-bit binaries, but it's a quick way to get it working, and it
3301cd4fb65SAntonio Nino Diazallows the user to run 64-bit binaries in addition to 32-bit binaries.
3311cd4fb65SAntonio Nino Diaz
3321cd4fb65SAntonio Nino Diaz1. Clone the `Linux tree fork`_ maintained by the Raspberry Pi Foundation. To
3331cd4fb65SAntonio Nino Diaz   speed things up, do a shallow clone of the desired branch.
3341cd4fb65SAntonio Nino Diaz
3351cd4fb65SAntonio Nino Diaz.. code:: shell
3361cd4fb65SAntonio Nino Diaz
3375e2e024bSAntonio Nino Diaz    git clone --depth=1 -b rpi-4.18.y https://github.com/raspberrypi/linux
3381cd4fb65SAntonio Nino Diaz    cd linux
3391cd4fb65SAntonio Nino Diaz
3401cd4fb65SAntonio Nino Diaz2. Configure and compile the kernel. Adapt the number after ``-j`` so that it is
3411cd4fb65SAntonio Nino Diaz   1.5 times the number of CPUs in your computer. This may take some time to
3421cd4fb65SAntonio Nino Diaz   finish.
3431cd4fb65SAntonio Nino Diaz
3441cd4fb65SAntonio Nino Diaz.. code:: shell
3451cd4fb65SAntonio Nino Diaz
3461cd4fb65SAntonio Nino Diaz    make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig
3471cd4fb65SAntonio Nino Diaz    make -j 6 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
3481cd4fb65SAntonio Nino Diaz
3491cd4fb65SAntonio Nino Diaz3. Copy the kernel image and the device tree to the SD card. Replace the path
3501cd4fb65SAntonio Nino Diaz   by the corresponding path in your computers to the ``boot`` partition of the
3511cd4fb65SAntonio Nino Diaz   SD card.
3521cd4fb65SAntonio Nino Diaz
3531cd4fb65SAntonio Nino Diaz.. code:: shell
3541cd4fb65SAntonio Nino Diaz
3551cd4fb65SAntonio Nino Diaz    cp arch/arm64/boot/Image /path/to/boot/kernel8.img
3561cd4fb65SAntonio Nino Diaz    cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb /path/to/boot/
3575e2e024bSAntonio Nino Diaz    cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b-plus.dtb /path/to/boot/
3581cd4fb65SAntonio Nino Diaz
3591cd4fb65SAntonio Nino Diaz4. Install the kernel modules. Replace the path by the corresponding path to the
3601cd4fb65SAntonio Nino Diaz   filesystem partition of the SD card on your computer.
3611cd4fb65SAntonio Nino Diaz
3621cd4fb65SAntonio Nino Diaz.. code:: shell
3631cd4fb65SAntonio Nino Diaz
3641cd4fb65SAntonio Nino Diaz    make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \
3651cd4fb65SAntonio Nino Diaz    INSTALL_MOD_PATH=/path/to/filesystem modules_install
3661cd4fb65SAntonio Nino Diaz
3671cd4fb65SAntonio Nino Diaz5. Follow the instructions in `Setup SD card`_ except for the step of renaming
3681cd4fb65SAntonio Nino Diaz   the existing ``kernel7.img`` (we have already copied a AArch64 kernel).
3691cd4fb65SAntonio Nino Diaz
3701cd4fb65SAntonio Nino DiazSetup SD card
3711cd4fb65SAntonio Nino Diaz-------------
3721cd4fb65SAntonio Nino Diaz
3731cd4fb65SAntonio Nino DiazThe instructions assume that you have an SD card with a fresh install of
3741cd4fb65SAntonio Nino Diaz`Raspbian`_ (or that, at least, the ``boot`` partition is untouched, or nearly
375e0f21f62SAntonio Nino Diazuntouched). They have been tested with the image available in 2018-03-13.
3761cd4fb65SAntonio Nino Diaz
3771cd4fb65SAntonio Nino Diaz1. Insert the SD card and open the ``boot`` partition.
3781cd4fb65SAntonio Nino Diaz
3791cd4fb65SAntonio Nino Diaz2. Rename ``kernel7.img`` to ``kernel8.img``. This tricks the VideoCore
3804def07d5SDan Handley   bootloader into booting the Arm cores in AArch64 mode, like TF-A needs,
3814def07d5SDan Handley   even though the kernel is not compiled for AArch64.
3821cd4fb65SAntonio Nino Diaz
3831cd4fb65SAntonio Nino Diaz3. Copy ``armstub8.bin`` here. When ``kernel8.img`` is available, The VideoCore
3841cd4fb65SAntonio Nino Diaz   bootloader will look for a file called ``armstub8.bin`` and load it at
3851cd4fb65SAntonio Nino Diaz   address **0x0** instead of a predefined one.
3861cd4fb65SAntonio Nino Diaz
3872d8f831fSAntonio Nino Diaz4. Open ``cmdline.txt`` and add ``memmap=16M$256M`` to prevent the kernel from
3884def07d5SDan Handley   using the memory needed by TF-A. If you want to enable the serial port
3894def07d5SDan Handley   "Mini UART", make sure that this file also contains
3901cd4fb65SAntonio Nino Diaz   ``console=serial0,115200 console=tty1``.
3911cd4fb65SAntonio Nino Diaz
3921cd4fb65SAntonio Nino Diaz   Note that the 16 MiB reserved this way won't be available for Linux, the same
3931cd4fb65SAntonio Nino Diaz   way as the memory reserved in DRAM for the GPU isn't available.
3941cd4fb65SAntonio Nino Diaz
3951cd4fb65SAntonio Nino Diaz5. Open ``config.txt`` and add the following lines at the end (``enable_uart=1``
3961cd4fb65SAntonio Nino Diaz   is only needed to enable debugging through the Mini UART):
3971cd4fb65SAntonio Nino Diaz
3981cd4fb65SAntonio Nino Diaz::
3991cd4fb65SAntonio Nino Diaz
4001cd4fb65SAntonio Nino Diaz    enable_uart=1
40197fb05e1SAntonio Nino Diaz    kernel_address=0x02000000
40297fb05e1SAntonio Nino Diaz    device_tree_address=0x01000000
4031cd4fb65SAntonio Nino Diaz
4041cd4fb65SAntonio Nino DiazIf you connect a serial cable to the Mini UART and your computer, and connect
4051cd4fb65SAntonio Nino Diazto it (for example, with ``screen /dev/ttyUSB0 115200``) you should see some
4061cd4fb65SAntonio Nino Diaztext. In the case of an AArch32 kernel, you should see something like this:
4071cd4fb65SAntonio Nino Diaz
4081cd4fb65SAntonio Nino Diaz::
4091cd4fb65SAntonio Nino Diaz
4101cd4fb65SAntonio Nino Diaz    NOTICE:  Booting Trusted Firmware
4111cd4fb65SAntonio Nino Diaz    NOTICE:  BL1: v1.4(release):v1.4-329-g61e94684-dirty
4121cd4fb65SAntonio Nino Diaz    NOTICE:  BL1: Built : 00:09:25, Nov  6 2017
4131cd4fb65SAntonio Nino Diaz    NOTICE:  BL1: Booting BL2
4141cd4fb65SAntonio Nino Diaz    NOTICE:  BL2: v1.4(release):v1.4-329-g61e94684-dirty
4151cd4fb65SAntonio Nino Diaz    NOTICE:  BL2: Built : 00:09:25, Nov  6 2017
4161cd4fb65SAntonio Nino Diaz    NOTICE:  BL1: Booting BL31
4171cd4fb65SAntonio Nino Diaz    NOTICE:  BL31: v1.4(release):v1.4-329-g61e94684-dirty
4181cd4fb65SAntonio Nino Diaz    NOTICE:  BL31: Built : 00:09:25, Nov  6 2017
4191cd4fb65SAntonio Nino Diaz    [    0.266484] bcm2835-aux-uart 3f215040.serial: could not get clk: -517
4201cd4fb65SAntonio Nino Diaz
4211cd4fb65SAntonio Nino Diaz    Raspbian GNU/Linux 9 raspberrypi ttyS0
4221cd4fb65SAntonio Nino Diaz    raspberrypi login:
4231cd4fb65SAntonio Nino Diaz
4241cd4fb65SAntonio Nino DiazJust enter your credentials, everything should work as expected. Note that the
4251cd4fb65SAntonio Nino DiazHDMI output won't show any text during boot.
4261cd4fb65SAntonio Nino Diaz
4271cd4fb65SAntonio Nino Diaz.. _default Arm stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub7.S
4281cd4fb65SAntonio Nino Diaz.. _default AArch64 stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub8.S
4291cd4fb65SAntonio Nino Diaz.. _Linux kernel tree: https://github.com/torvalds/linux
4301cd4fb65SAntonio Nino Diaz.. _Linux tree fork: https://github.com/raspberrypi/linux
4311cd4fb65SAntonio Nino Diaz.. _Raspberry Pi 3: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
4324def07d5SDan Handley.. _Raspberry Pi 3 TF-A bootstrap: https://github.com/AntonioND/rpi3-arm-tf-bootstrap
4331cd4fb65SAntonio Nino Diaz.. _Raspberry Pi 3 documentation: https://www.raspberrypi.org/documentation/
4341cd4fb65SAntonio Nino Diaz.. _Raspbian: https://www.raspberrypi.org/downloads/raspbian/
435