GPGPU on Raspberry Pi

By Wolfgang Keller
Draft
Originally written 2021-11-01
Last modified 2022-08-06

Table of contents

VideoCore versions

There exist two versions of the VideoCore GPU whose instruction sets differs quite a lot:

A more detailled list can be found at Raspberry Pi Documentation - Processors [visited 2022-06-18T17:43:32Z].

Ressources:

Tutorials

VideoCore IV

Setup

Setup an SD card with the Raspberry Pi OS (formerly called Raspbian) based on Debian 10 (“buster”). The newer versions of Raspberry Pi OS that are based on Debian 11 (“bullseye”) don't seem to work because they don't support the fkms driver. See BULLSEYE: "dtoverlay=vc4-fkms-v3d" OR "dtoverlay=vc4-kms-v3d" That is one of many Questions - Raspberry Pi Forums [visited 2022-06-21T18:31:19Z] for details. See also "New" old functionality with Raspberry Pi OS (Legacy) - Raspberry Pi [visited 2022-06-21T18:31:31Z].

Then do the following steps:

Just for the sake of completeness, here is a table with the various possible settings and whether the hello_fft.bin demo that we run in section section does run with this setting set:

Setting in /boot/config.txt Corresponding setting in raspi-config (6 Advanced OptionsA2 GL Driver) Does hello_fft.bin (section section ) work?
dtoverlay=… commentedG1 LegacyYes
dtoverlay=dtoverlay=vc4-fkms-v3dG2 GL (Fake KMS)Yes
dtoverlay=dtoverlay=vc4-kms-v3dG3 GL (Full KMS)No

Run the FFT example

Sources:

The Raspberry Pi OS version based on “Buster” contains a folder /opt/vc. If it is missing (or - more interesting - you want to use the latest available version), you can create it manually by doing the following steps:

Best copy /opt/vc/src/hello_pi/hello_fft to your home folder:

cp -r /opt/vc/src/hello_pi/hello_fft ~

Now run

cd ~/hello_fft
make
sudo ./hello_fft.bin 8

(the command sudo mknod char_dev c 100 0 that is mentioned in the above linked blog article is not necessary anymore).

QPU assembler code

Mailbox functionality

TODO

Instruction set

Registers
Instructions

An instruction consists of 64 bit (8 byte) in little-endian format.

VideoCore VI

Disassembler

A disassembler for machine instructions of the VideoCore VI QPU named vc6qpudisas is available at GitHub - Terminus-IMRC/vc6qpudisas: Disassembler of VideoCore VI QPU [visited 2022-07-22T20:03:32Z].

Let's go through its install instructions to set it up.

Build Mesa

First setup Mesa. We need to install various packages that are required for building Mesa:

$ sudo apt-get install meson
$ sudo apt-get install python3-mako
$ sudo apt-get install libdrm-dev
$ sudo apt-get install flex
$ sudo apt-get install bison
$ sudo apt-get install libx11-dev
$ sudo apt-get install libxext-dev
$ sudo apt-get install libxfixes-dev
$ sudo apt-get install libxcb-glx0-dev
$ sudo apt-get install libxcb-shm0-dev
$ sudo apt-get install libx11-xcb-dev
$ sudo apt-get install libxcb-dri2-0-dev 
$ sudo apt-get install libxcb-dri3-dev 
$ sudo apt-get install libxcb-present-dev
$ sudo apt-get install libxshmfence-dev
$ sudo apt-get install libxxf86vm-dev
$ sudo apt-get install libxrandr-dev

Now run

$ git clone https://gitlab.freedesktop.org/mesa/mesa.git --depth=1
$ cd mesa/
$ git fetch --all --tags
$ git checkout tags/mesa-21.3.9 -b mesa21_3_9
$ mkdir build/
$ cd build/
$ meson .. -Dgallium-drivers=v3d -Dvulkan-drivers=broadcom -Dplatforms=x11
$ ninja src/broadcom/qpu/libbroadcom_qpu.a src/util/libmesa_util.a
$ cd ../../

Remark: In the line

$ git checkout tags/mesa-21.3.9 -b mesa21_3_9

replace 21.3.9 by a suitable other (more recent) version if necessary.

Remark:

Build vc6qpudisas

Start with

$ git clone https://github.com/Terminus-IMRC/vc6qpudisas.git
$ cd vc6qpudisas/

In CMakeLists.txt change

target_link_libraries(vc6qpudisas
        ${MESA_BUILD_DIR}/src/broadcom/qpu/libbroadcom_qpu.a
        ${MESA_BUILD_DIR}/src/util/libmesa_util.a)

to

target_link_libraries(vc6qpudisas
        ${MESA_BUILD_DIR}/src/broadcom/qpu/libbroadcom_qpu.a
        ${MESA_BUILD_DIR}/src/util/libmesa_util.a
        stdc++)

(otherwise, you will get a linker error).

Now procede as written in the documentation:

$ mkdir build/
$ cd build/
$ cmake .. -DCMAKE_PREFIX_PATH="$(realpath ../../mesa);$(realpath ../../mesa/build)"
$ make

Finally, do the test from the documentation:

$ ./vc6qpudisas <<< '0x54001f4038f91fbf'
add  r0, r1, r2      ; fmul  rf61, rf62, rf63

There seem to exist three versions of V3D: 3.3, 4.1 and 4.2 (corresponding to values 33, 41 and 42 of the ver element of struct v3d_device_info); see

The GPU of the respective Raspberry Pi versions seems to use version 4.2 (value 42).

We now do some change in the vc6qpudisas.c file in the root folder of the vc6qpudisas repository. Change

int main(void)
{
    struct v3d_device_info devinfo = {
        .ver = 42,
    };

to

int main(int argc, char **argv)
{
    if (argc < 2)
    {
        fprintf(stderr, "Usage: vc6qpudisas version\n\nVersions known to work are 33, 41 and 42.\n");
        return -1;
    }

     struct v3d_device_info devinfo = {
        .ver = (uint8_t) atoi(argv[1]),
     };

Run the test again:

$ ./vc6qpudisas 42 <<< '0x54001f4038f91fbf'
add  r0, r1, r2      ; fmul  rf61, rf62, rf63

Instruction set

6 26
type ...
32
...
type Instruction
= 000000branch instruction
≠ 000000ALU instruction
Branch instructions
6 2 21 3
000000 10 addr_low cond
8 1 2 3 3 1 2 6 6
addr_high - msfign - bdu ub bdi raddr_a -

TODO

ALU instructions
6 5 21
type sig ...
20 6 6
... raddr_a raddr_b

TODO

GPGPU

Idein Inc. (GitHub page: Idein Inc. · GitHub [visited 2022-07-22T19:46:58Z]) did a lot of work for GPGPU on the VideoCore VI.

We consider the repository py-videocore6: GitHub - Idein/py-videocore6: Python library for GPGPU programming on Raspberry Pi 4 [visited 2022-07-22T19:52:46Z].

TODO

Various material

Vulkan on the Raspberry Pi:

VideoCore VI: