Speeding up open-source GPU driver development with unit tests, drm-shim, and code reuse

Getting an Arm platform that works with mainline Linux may take several years as the work is often done by third parties, and the silicon vendor has its own Linux tree. That means in many cases, the software is ready when the platform is obsolete or soon will be. It would be nice to start software development before the hardware is ready. It may seem like a crazy idea, but that’s what the team at Collabora has done to add support for Arm “Valhall” GPUs (Mali-G57, Mali-G78) to the Panfrost open-source GPU driver.

The result is that it only took the team a few days to successfully pass tests using data structures prepared by their Mesa driver and shaders compiled by their Valhall compiler after receiving the actual hardware thanks to the work done in the last six months. So how did they achieve this feat exactly?

Open source GPU driver Linux

We have to go back in time by a few months first. Last July, Collabora announced they had reverse-engineered Mali-G78 GPU’s Valhall instruction set using a Samsung Galaxy S21 smartphone. Wait? Didn’t I just say they work without Mali-G78 hardware? Correct, but they could not install mainline Linux and their GPU driver on the device as it was not rooted. They just used it to reverse-engineer the instructions and perform some testing by modifying compiled shaders and GPU data structures to experiment with individual bits. That step could have been avoided if Mali G78 documentation was available.

Alyssa Rosenzweig, a graphics software engineer for Collabora, continued her software development work, and in November 2021. she had written a Valhall compiler, and reverse-engineered enough to write a driver but still had no Linux hardware to test the code. So she wrote unit tests for everything from instruction packing to optimization and managed to solve a few bugs in the process simply using her development machine running Linux.

The next step was to use drm-shim library with fake GEM kernel drivers in userspace for CI (continuous integration) used in the Mesa project. A drm-shim driver makes the system think it features an actual GPU, but does nothing apart from receiving systems calls from userspace graphics drivers. This is not an emulator, and can not be used to test functionality, but it can help find flaws in the program flow. She was able to run a large number of tests on Apple M1 running Linux after fixing a bug (Hint: page size is 16K, instead of 4K) including compiling thousands of shaders per second with the Valhall compiler, and running Khronos’s OpenGL ES Conformance Test Suite to identify any issues.

Collabora also attempted to identify differences between Valhall and earlier Arm Mali GPUs such as Bifrost, and reuse a large part of Panfrost driver code, and only change the part of the code where they detected differences. For instance, the Valhall instruction set is quite similar to the older Bifrost instruction set, so embedded the Valhall compiler as an additional backend in the existing Bifrost compiler. Alyssa explains:

Shared compiler passes like instruction selection and register allocation “just work” on Valhall, even though they were developed and debugged for Bifrost.

Earlier this month (January 2022), Collabora finally received a Chromebook with a MediaTek MT8192 (Kompanio 820) system-on-chip (with Mali-G57 MC5 GPU) and a serial cable, they managed to run mainline Linux on the board after fixing USB, although the display is not working yet. The GPU is automatically disabled in MT8192 apparently due to a silicon bug but can be enabled after disabling the “Accelerator Coherency Port” (ACP). As discussed above it then only took a few days to successfully pass hundreds of tests on the actual hardware thanks to their preparation work. Collabora now expects Panfrost to support Valhall GPU “in time for end-users”. You can read the full story on Collabora blog.

Share this:

Support CNX Software! Donate via PayPal or cryptocurrencies, become a Patron on Patreon, or buy review samples

ROCK Pi 4C Plus
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
9 Comments
oldest
newest
Theguyuk
Theguyuk
8 months ago

This is something aRM should support and aid, IMO.

Cameron
Cameron
8 months ago

ARM is not providing any (public) support for the open source Valhall effort. They provided documentation for previous generations, but only after Collabora had basically finished the drivers.

Sander
Sander
8 months ago

Impressive work.

But … needed because the GPU itself is not documented / open source?

If so … let’s check the other side of the spectrum: AFAIK, Intel provides working Linux kernel source code for upcomping Intel CPU’s and GPU’s? Correct?

Back to the ARM reverse engineering side: who is sponsoring that reverse engineering? Apparantly not the hardware maker?

dgp
dgp
8 months ago

It’s not cool in the “religious freedom warrior” grade of OSS people to like Intel.

Anonymous
Anonymous
8 months ago

I had a feeling Valhall would get support fast. Good news for the RK3588.

HRR
HRR
8 months ago

Unfortunately the RK3588 uses the ‘modern’ G610 instead of the ‘old’ G57.
For Linux support it might not be the best short term option.

Ignas Kiela
Ignas Kiela
7 months ago

G610 should have the same ISA as the G57, as it is mostly a microarchitecture upgrade instead of a full architecture update.

wanderer_
7 months ago

This was a fun read. I didn’t understand half of it, but I was still blown away.

Advertisement