Raspberry Pi 3 64 bit Install

History
Its been about a year since the Raspberry Pi3 with a 64bit capable CPU became available. At the outset, it was difficult to install Gentoo on the Pi 3 in 64 bit mode. A lot of work by a lot of people has almost brought a 64 bit Gentoo install on the Pi 3 down to almost a standard handbook install.

The Gentoo arm64 offering is experimental at the time of writing. That means that very little is marked stable. Expect to use ACCEPT_KEYWORDS="~arm64" and then use package.accept_keywords too.

What Works
All the Pi 3 hardware is supported in 64 bit mode. That's WiFi USB Ethernet (needs USB) Bluetooth Hardware Video Acceleration Sound over HDMI

Untested
PAL/NTSC Video Output Analogue Sound Output

Whats Required
Gentoo Install on a PC microSD card reader for the PC Raspberry Pi 3 microSD card > 8G USB Keyboard USB Mouse HDMI Display The content of the microSD card will be wiped during the install.

Installation Overview

 * Install crossdev on the PC
 * Fetch the Raspberry Pi firmware
 * Fetch the Raspberry Pi kernel
 * Partition the microSD card
 * Fetch the Gentoo bits of the install
 * Cross compile and install your kernel
 * Setup
 * Boot the Pi to Test

Install Crossdev on the PC
There are no prebuilt kernel images for the Pi3 in its arm64 mode. Until you have a arm64 kernel, you can't boot it in 64 bit mode. Its a little chicken and egg. Once the Pi is running, it can build its own kernels.

crossdev is Gentoos' tool for building cross compiler tool chains. Once its installed, we will use it to build the arm64 kernel on the Gentoo PC. will install the crossdev tool.

Using Crossdev to Build A Cross Compiler
You may need to nominate an overlay to store the cross ebuilds. crossdev will let you know.

There are other parameters you can pass to crossdev too.

When crossdev completes you will have a cross toolchain

It will also create an arm64 target root in /usr/aarch64-unknown-linux-gnu/ This is used by cross emerge.

Pure cross compiling, other than the kernel, is out of scope of this guide.

Fetch the Raspberry Pi Firmware
The Raspberry Pi Firmware is maintained in a git repository. You will need to install git if you don't have it.

Don't do anything you don't need to do as root.

As your normal user, make some space in the home directory for raspberry pi will do nicely.

This will be for the Raspberry Pi Firmware and kernel

will fetch the Pi firmware into a directory called firmware

It will actually fetch all the history too. If you are short of space or bandwidth, a shallow clone is all that's needed. A full clone is 6.3G and growing, we only need the /boot directory out of the master branch.

Fetch the Raspberry Pi Kernel
Stay in raspberrypi and

will fetch the kernel into a directory called linux.

With absolutely no fanfare at all, 64 bit support was added to this kernel tree late in 2016. No more searching for odd patches.

Not everything has been accepted by the mainline kernel yet but its getting closer. Feel free to test for yourself.

The master branch may be broken as its commit by commit as it happens. Test if you want to. Its better to use an identified tag. At the time of writing that is rpi-4.10.y. The 4.10 mainline kernel is still at -rc status.

That's the kernel source tree in place, ready for configuring and cross compiling.

Background
The Pi does not need a boot loader. There is no firmware for the arm CPU, like there is in a PC. Instead, the GPU manages loading software for the CPU to execute, while the CPU is held reset. Its the GPU that has the 'BIOS' that gets everything started.

This makes it impossible to brick the Pi, since at worst, the microSD card needs to be reloaded.

This arrangement does impose some constraints on the microSD card. The partition table must be MSDOS The first partition (/boot) must be vfat. The bootcode.bin file has some useful defaults that make setup easier.

Partitioning
Depending on how the microSD card is connected to you PC, it my be /dev/sdX or /dev/mmcblkY

Using the partitioning tool of your choice, make three partitions on your microSD card. boot 128Mb swap 2G root (/) the rest. If you know what you are doing you can add more.

Make filesystems vfat mkswap mke2fs -t ext4

You know the block devices.

Fetch the Gentoo bits of the install
To make it easy to cross refer to the Gentoo Handbook

Mount the microSD card / at /mnt/gentoo

Install the arm64 Stage 3
Following the Gentoo handbook fetch the arm64 stage3 and untar it to /mnt/gentoo in the normal way.

Install a Portage Snapshot
Following the Gentoo handbook fetch the and unpack a portage shapshot.

Careful readers can copy their /usr/portage as long as ./packages and ./distfiles is omitted.

The Raspberry Pi Foundation Provided Files
Mount the microSD card boot at /mnt/gentoo/boot. (The microSD card root should still be mounted at /mnt/gentoo)

As root, copy the content of your normal users ~/firmware/boot to /mnt/gentoo/boot.

You should end up with files in /mnt/gentoo/boot, not a directory called boot.

Configure The Kernel
As your user

The bcmrpi3_defconfig is almost right as it stands. It defaults to the powersave CPU governor, which runs the Pi at 600MHz. All the governors are there. Ondemand is recommended.

Either use menuconfig to change the default CPU governor, add something to the kernel command line, or change it after booting.

Build the Kernel
The build is conventional, other than telling the build system to build for arm64 and use the cross compiler.

Change X to the number of parallel MAKE jobs you want to run. Convention is cores+1.

Install the Kernel to the microSD Card
The kernel is in three parts kernel binary kernel modules the device tree Firmware

Install The Kernel Binary
As root copy the kernel binary from the build location

It is possible to use other kernel file names by adding entries to /boot/config.txt

Install The Device Tree
The device tree binary (.dtb) describes the hardware to the kernel. This avoids having all the existing hardware configurations ard coded into the kernel.

Due to the way the Raspberry Pi 64 bit kernel support has been added, there are going to be two different device trees with the same file name. A 32 bit version and a 64 bit version. They are not interchangeable. Move the 32 bit version out of the way. That's renamed the 32 bit version.

Copy the dtb from the build location

It is possible to use other device tree file names by adding entries to /boot/config.txt

Install The Kernel Modules
From the top of the kernel tree, install the kernel modules.

The INSTALL_MOD_PATH is the root of the filesystem the modules are to be installed into. Due to kernel naming its unlikely you will have a 4.10.0-rc6-v8+ so omitting INSTALL_MOD_PATH is probably harmless to your build host install.

Setup
Much of the setup prior to booting a new Gentoo install is done in a chroot. Chrooting into an arm64 intall from some other arch is beyond the scope of this guide. Instead, only the bare minimum setup, from outside the chroot is covered.

Root Password
You need this to be able to log in at all.

/etc/fstab
On the Pi, the microSD card will be /dev/mmcblk0 with partitions /dev/mmcblk0p1, /dev/mmcblk0p2 and /dev/mmcblk0p3

Edit /mnt/gentoo/etc/fstab to match.

/dev/mmcblk0p1         /boot           vfat            noauto,noatime  1 2 /dev/mmcblk0p2         none            swap            sw              0 0 /dev/mmcblk0p3         /               ext4            noatime         0 1

/boot/config.txt
This file will not exist until its created. Create it with the following content.

/boot/cmdline.txt
This file will not exist until its created. Create it with the following content.