Asahi Linux developer VTuber talks about 'Driver development to run Apple's M1 GPU on Linux'



Rina Asahi , a developer and VTuber involved in the development of Asahi Linux , a Linux distribution that aims to operate on a Mac equipped with the M1 chip, which is Apple's proprietary SoC, explains M1 GPU driver development on the Asahi Linux official blog. doing.

Tales of the M1 GPU - Asahi Linux
https://asahilinux.org/2022/11/tales-of-the-m1-gpu/

According to Mr. Lina, almost all GPUs in recent years consist of the following main components.

・ 'Shader core' that executes user-defined programs to process triangles (vertex data) and pixels (fragment data)
A ' rasterization unit', 'texture sampler', 'render output unit' that converts triangles to pixels on the screen, and other bits that work together with shaders
・'Command processor' that receives drawing commands from the application and sets the shader core to process them
・'Memory Management Unit (MMU)' that uses the GPU to restrict access to memory areas belonging to specific apps, preventing different apps from crashing or interfering

In order to handle these components reasonably and safely, modern GPU drivers are divided into two: 'user space drivers' and 'kernel drivers'. The user space driver is responsible for compiling shader programs and translating API calls such as OpenGL and Vulkan into a command list for the command processor. The kernel driver, on the other hand, manages the MMU to allocate memory for each app and decide how and when to send commands to the command processor.

Between the user space driver and the kernel driver exists a custom API customized for each GPU family. These APIs vary from driver to driver and are called UAPIs on Linux. UAPI allows user-space drivers to ask kernel drivers to allocate and free memory, and to send command lists to the GPU. In other words, Lina explains that in order to operate the M1 GPU on Asahi Linux, two M1 GPU user space drivers and kernel drivers are required.

The first to come out of the M1 GPU driver for Asahi Linux was the user space driver. In January 2021, Alyssa Rosenzweig , who leads the GPU-related part of the Asahi Linux project, succeeded in rendering triangles by reverse engineering less than a month after the start of development.



At that time, the kernel driver had not yet been developed, but Rosenzweig reverse-engineered the UAPI of the macOS GPU driver, allocated memory and sent its own commands to the GPU, thereby successfully operating the user space driver. Did. After that, Rosenzweig developed an M1 GPU OpenGL driver for

Mesa , a Linux user space graphics library, and then cleared 75% of the OpenGL ES 2.0 conformance test. In addition, Rosenzweig announced that in August 2022, he was able to actually play games using the M1 GPU's OpenGL driver.

So, Mr. Lina started developing a kernel driver. In the first few months, he seems to have been working on the development and improvement of the M1N1 hypervisor , which is the minimum development environment for Apple Silicon, for the M1 GPU, but he noticed the peculiarities of the M1 GPU.

Normally, the kernel driver is mainly responsible for memory management and command scheduling by the MMU. However, the M1's GPU has a coprocessor called 'ASC', and this ASC runs Apple firmware to manage the GPU. ASC is a complete ARM64 CPU that runs Apple's own real-time OS called 'RTKit', and manages everything such as power management, command scheduling, fault recovery, performance counters, temperature measurement, etc. So the kernel drivers in macOS never talk to the GPU directly, they all run through firmware and use data structures in shared memory to tell it what to do. According to Lina, because this data structure is so complex and resides in a shared kernel address space along with the GPU firmware itself, only the data structure in shared memory is moved to user space to set rendering commands. It is said that it is impossible to do so.

Therefore, Mr. Lina used the data structure written in Python for the M1N1 hypervisor to start developing the kernel driver for the M1 GPU in Python. Below is a triangle rendered using this kernel driver written in Python.



However, according to Mr. Lina, this triangle display is just a prototype demonstration. Therefore, Mr. Lina runs the M1 GPU OpenGL driver developed by Mr. Rosenzweig on the library `` drm-shim '' that mocks the Linux DRM kernel interface and replaces it with dummy processing in the user space. Run Inochi2D . In addition, it incorporates a Python interpreter , sends commands to the Python kernel driver on the M1N1 development framework, communicates with a real M1-equipped Mac via USB, exchanges data, and renders your own avatar in cooperation with the GPU firmware. I tested if it works.

The result is below. Although smooth display is not possible, the rendering itself has been successful. In addition, we have also succeeded in operating GUIs such as the desktop environment ' GNOME ' and the web browser Firefox.



After that, there was a rumor that the programming language Rust would be officially adopted for the Linux kernel. According to Mr. Lina, he started studying Rust because he thought it was a fairly advantageous language for writing GPU drivers. “The more I use Rust, the more I like it,” Lina says. “I feel that Rust design leads to good abstraction and software design. Compilers are very picky, but once the code is Once it compiles, it gives me confidence that it will work.Sometimes I found that the compiler wasn't happy with the design I was trying to use and that there was a fundamental problem with the design.' says.

Normally, if you create a complicated kernel driver from scratch, you will encounter various problems such as memory leaks, but Rust's GPU driver only fixes a few logic bugs and memory management problems. It was said that it was done. As a result, the Rust kernel driver passed the Open GL conformance test 99% of the time in October 2022.

A report that the GPU driver of ``Asahi Linux'' that runs Linux in the Apple M1 environment finally passed the test with a score of 99% or more-GIGAZINE



In November 2022, it was reported that the game 'Minecraft' was successfully run on Asahi Linux.



The driver that has been developed so far is compatible with OpenGL, but it is said that Vulkan compatibility is under development. However, on November 23, 2022, Vulkan successfully drew a cube. Since the UAPI used at the time of writing the article is still a prototype, Lina says that it is necessary to add or redesign UAPI in order to develop a driver that fully supports Vulkan in the future.



In addition, building and installing the M1 GPU driver is very complicated, and it is said that it is still difficult to release at the time of writing the article, but Lina wants to introduce it to Asahi Linux as an opt-in test build by the end of 2022. is.

in Software,   Hardware,   Video, Posted by log1i_yk