Mainlining

From postmarketOS Wiki

Mainlining is the process of replacing the kernel provided by the device vendor (the "downstream" kernel), with a version close to the actively developed version released on kernel.org (the "mainline" kernel). It involves porting (and most importantly: cleaning up) device-specific code from the downstream kernel to the mainline kernel. For some SoCs, common support already exists in mainline, so you can focus on device-specific components.

Why?

Mainlining is a fairly complicated process, especially if you're one of the first to start work on a given SoC. However through community effort there is a growing number of SoCs that have increasingly stable mainline ports (see below). In general it is relatively simple to take your existing Android kernel and boot it without any of the Android blobs, however from that point it can be a lot of working to create a working system and at the end of it you don't receive any security patches or general improvements.

Apart from purely practical insufficiencies, the cold hard truth is that the downstream code is simply not meant to be maintained or get used outside Android. The code quality is often several magnitudes worse, which makes maintenance rather complicated. Often large parts of the drivers are moved into proprietary parts in Android userspace - which are generally impossible to use without emulating the expected Android environment. At the end, the effort to make such drivers work properly on long term is often as much as rewriting them properly for mainline.

Successfully mainlining an SoC or device means that you benefit from every improvement the Linux kernel has to offer, you can run a fairly standard Linux distro (in theory even any Linux distro that has support for your device’s architecture) and benefit from kernel updates and improvements without needing to compile your own kernel or deal with frustrating patches. A notable easy-to-notice benefit of running mainline is minimal-effort-needed GPU support for almost all GPUs (except PowerVR unfortunately), which is needed for using modern phone DEs like Plasma Mobile or Phosh with acceptable performance. Lastly, mainlining is a terrific learning opportunity that you will never regret.

If your device's SoC is in the list below there's a very decent chance that you can get the same functionality working on your device and eventually upstream your patches to the kernel, helping to build a solid base for other devices too!

Overview

Supported SoCs

Some SoCs are already supported quite well and are used for some devices in postmarketOS. Getting started with one of these might be easier for you, because you can get help from others working with the same SoC. The feature matrix below shows which features are already supported for a particular SoC. The pages for each SoC will provide more information, plus hints how to get started and who you can contact.

  • CPU: SMP (bring up secondary CPU cores), CPU frequency scaling, CPUidle
  • Storage: eMMC, SD cards, UFS, ...
  • Video: Hardware-accelerated video de/encoding
  • Modem: Calls, SMS, Internet
  • Suspend: Can reach (all) low-power modes of the SoC during suspend (either system suspend or cpuidle).
  • Unavailable (-) means that such a feature is not built directly into SoC. For example, WiFi/BT are also often used through SDIO/UART which can be chosen independently from the SoC.

Note: The statuses displayed below signify if a particular feature was tested on a device by someone in the postmarketOS community - not necessarily the components supported in the mainline kernel.

SoC Arch Year UART USB Storage Display CPU GPU Pinctrl I²C Audio Video Thermal WiFi BT Modem GPS Camera Suspend
Allwinner A33 armv7 2014 Y Y Y Y P P Y Y P Y Y - - - - P P
Allwinner A64 aarch64 2015 Y Y Y Y P P Y Y Y Y - - - - Y P
Allwinner D1-H riscv64 2020 Y Y P P Y Y Y Y Y P Y - - - - -
Allwinner H3 armv7 2014 Y Y Y Y P P Y Y P Y Y - - - - P P
Amlogic AML8726-MX armv7f 2012 P N N N P N N N N N N - - - - -
Amlogic S805 armv7f 2014 Y Y P P Y Y P Y N P Y - - - - -
Amlogic S812/S812-B/S812-S/S802 armv7f 2014 Y Y P P Y Y P Y N P Y - - - - -
Amlogic S905 aarch64 2015 Y Y P Y Y Y Y Y Y P Y - - - - - N
Amlogic S905D2/S905X2/S905Y2 aarch64 2019 Y Y P Y Y Y Y Y Y P Y - - - - -
Amlogic S905D3/S905X3/S905Y3 aarch64 2019 Y Y P Y Y Y Y Y Y P Y - - - - -
Amlogic S905W/S905D/S905X/S905L/S805X/S805Y/S905Z aarch64 2016 Y Y P Y Y Y Y Y Y P Y - - - - -
Amlogic S912 aarch64 2016 Y Y P Y Y Y Y Y Y P Y - - - - -
Amlogic S922X/A311D aarch64 2019 Y Y P Y Y Y Y Y Y P Y - - - - -
Apple A7 (S5L8960X/S5L8965X) aarch64 2013 Y P P P Y - - - -
Apple M1 (T8103/Tonga) aarch64 2020 Y Y Y Y Y Y Y Y - - - - Y P
Broadcom BCM21664 armv7 2012 Y N P P Y Y - - - -
Broadcom BCM23550 armv7 2012 Y N P P Y Y - - - -
Broadcom BCM2711 aarch64 2019 Y Y Y Y Y Y Y Y Y Y Y - - - -
Broadcom BCM28155 armv7 2012 - - - -
Broadcom BCM2835 armhf 2012 Y Y Y Y Y Y Y Y Y Y Y - - - - Y
Broadcom BCM2836 armv7 2015 Y Y Y Y Y Y Y Y Y Y Y - - - - Y
Broadcom BCM2837 aarch64 2016 Y Y Y Y Y Y Y Y Y Y Y - - - - Y
Freescale i.MX6 SLL armv7 2011 Y Y Y Y Y - Y Y Y - Y - - - - Y
Freescale i.MX6 SoloLite armv7 2011 Y Y Y Y Y - Y Y Y - Y - - - - Y
HiSilicon Kirin 620 (Hi6220) aarch64 2015 Y Y Y Y Y Y Y Y P - - - -
Intel Bay Trail x86_64 2013 Y Y Y Y Y Y Y Y Y Y Y - - - - N Y
Intel Cherry Trail x86_64 2013 Y Y Y Y Y Y Y Y Y Y Y - - - - N Y
Intel Cloverview x86 2013
Intel Medfield x86_64 2013
Intel Menlow x86 2008
Intel Merrifield x86_64 2014
Intel Moorefield x86_64 2014
Intel Moorestown x86 2011
Intel SoFIA x86_64 2015 -
Marvell Armada 510 armv7 2011 Y Y Y Y Y - Y Y Y - Y - - - - Y
Marvell PXA1908 aarch64 2014/2015 Y Y Y N P N Y Y N N N - - N N N N
MediaTek Helio P10/P15/P18 aarch64 2014 P N N P P N N N N N N N N N N N N
MediaTek Helio P90 aarch64 2018 Y N Y P Y N Y N Y N N N N N N N
MediaTek MT6580 armv7 2015 Y N N N P N N N N N N N N N N N N
MediaTek MT6582 armv7 2013 Y N N N P N N N N N N N N N N N N
MediaTek MT6589 armv7 2013 Y N N N P N N N N N N - - N - N N
MediaTek MT6735 aarch64 2014-2016 Y P P Y N Y Y N N N N N N N N N N
MediaTek MT6765 aarch64 2018/2020 Y Y Y Y Y N Y Y N N N N N N N N N
MediaTek MT6878 (Dimensity 7300) aarch64 2024 Y N N N Y N N N N N N N N N N N N
MediaTek MT8173 aarch64 2015 Y Y Y Y Y P Y Y Y Y Y N N - - N Y
MediaTek MT8183 aarch64 2019 Y Y Y Y Y Y Y Y Y Y Y N N - - N Y
MediaTek MT8186 aarch64 2022 Y Y Y Y Y Y Y Y N Y Y - - - - N N
Nvidia Tegra 2 armv7 2010 Y Y Y Y Y P Y Y Y P Y - - - - N Y
Nvidia Tegra 3 armv7 2011 Y Y Y Y Y P Y Y Y P Y - - - - N Y
Nvidia Tegra 4 armv7 2013 Y P Y Y P P Y Y Y P Y - - - - N Y
Nvidia Tegra K1 (T124) armv7 2014 Y Y Y Y Y P Y Y Y Y - - - - P
Nvidia Tegra K1 (T132) aarch64 2014 Y Y Y Y Y P Y Y Y Y - - - - P
Nvidia Tegra X1 (T210) aarch64 2015 Y Y Y Y Y P Y Y Y P Y - - - - P P
Nvidia Tegra X1 (T214) aarch64 2019 Y Y Y Y Y P Y Y Y P Y - - - - P P
Qualcomm APQ8064 armv7 2013 Y Y Y Y Y N Y Y N Y Y N
Qualcomm MSM8212 armv7 2013 Y Y Y P Y Y
Qualcomm MSM8226 armv7 2013 Y Y Y Y P Y Y Y N N Y Y Y N Y P N
Qualcomm MSM8909 armv7 2015 Y Y Y P Y Y Y N P N N N
Qualcomm MSM8916 aarch64 2014 Y Y Y Y P Y Y Y Y Y Y Y Y Y Y P
Qualcomm MSM8917 aarch64 2016 Y Y Y Y Y Y Y Y P N Y Y Y N P N
Qualcomm MSM8929 aarch64 2014 Y Y Y P P Y Y Y Y Y Y Y Y Y Y P
Qualcomm MSM8937 aarch64 2016 Y Y Y Y Y Y Y Y N N Y Y Y N N N
Qualcomm MSM8939 aarch64 2014 Y Y Y Y P Y Y Y Y Y Y Y Y
Qualcomm MSM8940 aarch64 2016 Y Y Y Y Y Y Y Y N N Y Y Y N N N
Qualcomm MSM8953 aarch64 2016 Y Y Y Y Y Y Y Y Y Y Y Y Y P P P P
Qualcomm MSM8960 armv7 2012 Y P P Y Y P
Qualcomm MSM8974 armv7 2013 Y Y Y Y P P Y Y N N Y Y Y P N N N
Qualcomm MSM8976 aarch64 2016 Y P Y Y P Y Y Y N N Y Y Y P N N
Qualcomm MSM8992/MSM8994 aarch64 2015 Y Y P P Y N Y B B
Qualcomm MSM8996 aarch64 2016 Y Y P Y P Y Y Y Y Y Y - - P P N
Qualcomm MSM8998 aarch64 2017 Y Y Y P P N Y Y Y Y Y Y P Y
Qualcomm QM215 aarch64 2019
Qualcomm SC8280XP aarch64 2021 Y Y Y Y Y Y Y Y P P Y P P Y P P
Qualcomm SDM630 aarch64 2017 Y P Y Y P P Y Y N Y Y Y Y Y P
Qualcomm SDM636 aarch64 2017 Y P Y Y P P Y Y N Y Y Y Y Y P
Qualcomm SDM660 aarch64 2017 Y P Y Y P P Y Y N Y Y Y Y Y P
Qualcomm SDM670 aarch64 2018 N P P Y P Y Y Y P P Y Y Y Y Y
Qualcomm SDM845 aarch64 2018 Y Y Y Y Y Y Y Y Y Y Y Y Y P Y Y Y
Qualcomm SM6115 aarch64 2020
Qualcomm SM6125 aarch64 2019 Y Y Y P N Y N N N P N N N N
Qualcomm SM6225 aarch64 2021 N N P P N N N N N N N N N N
Qualcomm SM6350 aarch64 2020 Y Y Y Y Y Y Y Y N N P Y - Y N P
Qualcomm SM6375 aarch64 2021 Y Y P N Y N Y Y N N P Y - Y N
Qualcomm SM7125 aarch64 2020 Y Y Y Y Y Y Y Y Y Y P
Qualcomm SM7150 aarch64 2019/2020 Y Y Y Y Y Y Y Y P N Y Y Y Y P P Y
Qualcomm SM7325 aarch64 2021/2022 Y Y Y Y Y Y Y Y N Y Y Y Y N N N
Qualcomm SM8150 aarch64 2019 Y Y Y Y Y Y Y Y P Y Y Y
Qualcomm SM8250 aarch64 2020/2021 Y Y Y P Y Y Y Y Y Y Y Y
Qualcomm SM8350 aarch64 2021 P Y P Y
Qualcomm SM8450 aarch64 2021 Y Y Y Y Y Y Y Y Y Y
Qualcomm SM8475 aarch64 2022 Y Y Y Y Y Y Y
Qualcomm SM8550 aarch64 2022 Y Y Y Y Y Y Y Y Y
Qualcomm SM8650 aarch64 2023 Y Y Y Y Y Y Y Y Y N Y Y Y Y N N
Qualcomm SM8750 aarch64 2024 Y Y
Qualcomm X1E80100 aarch64 2023
RDA Microelectronics RDA8810PL armv7 2014 Y Y Y - - -
Rockchip RK3066 armv7 2012 Y Y Y P Y Y Y Y P Y Y Y
Rockchip RK3126 armv7 2014 Y Y Y Y Y Y Y Y Y
Rockchip RK3128 armv7 2014 Y Y Y Y Y Y Y Y Y
Rockchip RK3188 armv7 2013 Y Y Y Y Y Y Y Y Y
Rockchip RK3288 armv7 2014 Y Y Y P Y Y Y Y Y Y Y Y Y Y Y
Rockchip RK3399 aarch64 2016 Y Y Y Y Y Y Y Y Y Y Y P - - - Y Y
Samsung Exynos 3250 armv7 2014 Y Y Y Y Y Y Y Y Y Y
Samsung Exynos 3475 armv7 2015 Y N Y P P Y Y Y N N N - - N N N
Samsung Exynos 4210 armv7 2011 Y Y Y Y Y Y Y Y Y - - - -
Samsung Exynos 4212 armv7 2012 Y Y Y Y Y Y Y Y Y Y - - - - Y
Samsung Exynos 4412 armv7 2012 Y Y Y Y Y Y Y Y Y Y - - - - P Y
Samsung Exynos 5250 armv7 2012 Y Y Y Y Y Y Y Y Y Y Y - - - - Y
Samsung Exynos 5420 armv7 2013 Y Y Y Y Y Y Y Y Y Y Y - - - - Y
Samsung Exynos 5422 armv7 2014 Y Y Y Y Y Y Y Y Y Y Y - - - - Y
Samsung Exynos 5433 aarch64 2014 Y Y Y Y Y Y Y Y Y Y Y
Samsung Exynos 7870 aarch64 2016 Y Y Y P Y Y Y Y N N N - - N N N
Samsung Exynos 8895 aarch64 2017 P P P Y
Samsung Exynos 990 aarch64 2020 N Y Y P Y Y Y N N N N N N N N N
SpacemiT Key Stone K1 / M1 riscv64 2024 Y Y P Y Y - - - -
ST-Ericsson NovaThor U8500 armv7 2011 Y Y Y Y Y Y Y Y N N Y - - N - N Y
Texas Instruments OMAP3xxx armv7 2008 Y Y Y Y Y N Y Y Y N Y - - - - Y Y
Texas Instruments OMAP44xx armv7 2011 Y Y Y Y Y N Y Y Y N Y - - - - P Y

Getting help

Mainlining related to postmarketOS is primarily discussed in the #postmarketOS-mainline channel. For one of the SoCs above there should be someone around who is able to help you to some extent. Note that you will be on your own for some of the problems - there are often device specific problems or components that no one has experience with yet.

Below are some more chat channels / mailing list that may be helpful for further help or if you want to work on a SoC that is not listed above (yet!):

In Matrix, you can browse most of Linux kernel development related channels in #linux-kernel-devspace

SoC Where to ask for help
#postmarketOS-mainline (SoC-specific questions might be limited to those listed above)
Allwinner #linux-sunxi on OFTC, linux-sunxi@googlegroups.com
Amlogic Chat room :

1 . #amlogic-postmarketos on OFTC

(for postmarketOS amlogic porting)

- Bridged Matrix Room :

#oftc_#amlogic-postmarketos:pixie.town

- Logs: https://irc.vitali64.duckdns.org/oftc/amlogic-postmarketos/

2. #linux-amlogic on Libera.Chat

- Log : https://libera.irclog.whitequark.org/linux-amlogic

Mail List :

1. linux-amlogic@lists.infradead.org

2. u-boot-amlogic@groups.io

Other Resources :

1. linux-meson.com.

Broadcom Kona ask knuxify, bcm-kona-mainline on GitHub
Exynos #replicant (with focus on older variants) or #linux-exynos on Libera.chat (ask forkbomb, Putti, krzk), linux-samsung-soc@vger.kernel.org
Samsung Exynos 4 Exynos entry + #exynos4xxx-mainline:matrix.org on Matrix
Samsung Exynos 5 Exynos entry + #exynos5-mainline:matrix.org on Matrix
Marvell PXA1908 (incl. other PXA and MMP) #pxa1908-mainline on OFTC
MediaTek #linux-mediatek on Libera.chat, linux-mediatek@lists.infradead.org
MediaTek MT6735 MediaTek entry + join #mt6735-foss:matrix.org
OMAP3 / OMAP4 ask @pavelm, linux-omap@vger.kernel.org
Qualcomm (msm...) #linux-msm on OFTC, linux-arm-msm@vger.kernel.org
Qualcomm Snapdragon 400 (MSM8226) Qualcomm entry + join #msm8226-mainline:postmarketos.org
Qualcomm Snapdragon 210 (MSM8909) Qualcomm entry + join #msm8916:postmarketos.org
Qualcomm Snapdragon 410/412 (MSM8916) Qualcomm entry + join #msm8916:postmarketos.org
Qualcomm Snapdragon 450/625/626/632 (MSM8953) Qualcomm entry + join #msm8953-mainline:matrixorg
Qualcomm Snapdragon 600 (APQ8064) and Qualcomm Snapdragon S4 Plus (MSM8960) Qualcomm entry + join #linux-apq8064:matrix.org
Qualcomm Snapdragon 615/616 (MSM8939) Qualcomm entry + join #msm8916:postmarketos.org
Qualcomm Snapdragon 660/630/636 (SDM660/SDM630/SDM636) Qualcomm entry + join #sdm660-mainline:matrix.org
Qualcomm Snapdragon 670 (SDM670) Qualcomm entry + join #sargo/bonito porting:erebion.eu
Qualcomm Snapdragon 730/730G/732G (SM7150) Qualcomm entry + join #sm7150-mainline:matrix.org
Qualcomm Snapdragon 820/821 (MSM8996) Qualcomm entry + join #msm8996-mainline:matrix.org
Qualcomm Snapdragon 845 (SDM845) Qualcomm entry + ask Caleb and join #sdm845:postmarketos.org
Qualcomm Snapdragon 865/865+/870 (SM8250) Qualcomm entry + join #sm8250-mainline:matrix.org
Qualcomm Snapdragon 8 Gen 1/8+ Gen 1/7+ Gen 2 (SM8450/SM8475/SM7475) Qualcomm entry + join #sm8450-mainline:matrix.org
Qualcomm Snapdragon 8 Gen 3 (SM8650) #linux-msm and #sm8650-mainline on Matrix or OFTC
Nvidia Tegra matrix.org:#postmarketOS-on-transformers:matrix.org (initially focused on the Asus Eee Pad Transformer family, now also offers help with other Tegra devices under PMOS), #tegra on Libera.chat, linux-tegra@vger.kernel.org

Getting Started

  • Collect as much information about your device as you can: downstream kernel sources, specifications, schematics (can be sometimes found with your favorite search engine) are all going to prove invaluable when attempting to port a device to mainline.
  • Serial debugging will be helpful especially in the beginning (before USB or network is working). Some devices expose it through the USB port or the audio jack; others have test points available on the mainboard.
  • Choose a kernel tree:
    • Mainline: (the last kernel version released by Linus Torvalds) Usually a good compromise of being recent, yet quite stable. torvalds/linux.git
    • linux-next: A collection of recently merged changes from many subsystems; use this if you are working on a very recent SoC. next/linux-next.git
    • There may be more WIP changes available in Linux kernel trees specific to your SoC.
  • Compiling kernels with envkernel.sh is very useful to integrate kernel development into postmarketOS.
  • If you are attempting to port to a SoC listed above, check the SoC wiki page for further help and instructions.
  • Do not attempt to copy any code as-is from downstream. In general this won't work, and most importantly: it won't be accepted for inclusion into the mainline kernel upstream.
    • Instead, try to understand what the downstream code does, and rewrite it from scratch for mainline by looking at similar code.

Materials to look into

This section include many useful sources to help you with mainlining as possible.

Note: Linux architecture regarding to embedded devices moving fast forward, so be aware that some specific information can get outdated in 2 – 3 years.

Books:

  • Mastering Linux Device Driver Development: Write custom device drivers to support computer peripherals in Linux operating systems (2021, John Madieu)
  • Linux Driver Development for Embedded Processors - Second Edition: Learn to develop Linux embedded drivers with kernel 4.9 LTS (2018, Alberto Liberal De Los Ríos)

Intro:

  • Porting mainline Linux to mobile phones, Luca Weiss, 2022 (40:00) FOSDEM
  • Running Mainline Linux on Snapdragon 410, Nikita Travkin, 2022 (40:00) FOSDEM
  • From Android to mainline on the Snapdragon 845, Caleb Connolly, 2022 (30:00) FOSDEM
  • Qualcomm SoC upstreaming adventures in 2020 Youtube Odysee
  • Porting Linux to your favorite obscure Arm SoC, 2020 media.ccc.de Youtube Odysee
  • Upstreaming a Qualcomm SoC, 2020 YouTube Slides Overview of porting 2020-era SoC
  • Introduction into how mainlining is done, 2017 (52:37) Youtube Odysee

Basics:

Writing device trees:

Clocks:

Power Management

  • SAN19-421 Training: Device power management for idle Youtube

Camera

  • Camera⧸ISP Drivers Using V4L2 Media Controller Framework (35m) Youtube Odysee


Various topics:

  • Pin Control and GPIO, 2013 (48:03) Youtube Odysee Slides talk by Linus Walleij
  • Timekeeping in the Linux Kernel - Stephen Boyd, Qualcomm Innovation Center, 2017 (34:41) Youtube Odysee About how clocksource works.
  • How Dealing with Modern Interrupt Architectures can Affect Your Sanity Youtube Odysee Slides About interrupt controllers and their relationships
  • Linux Kernel Display Architecture, 2013 (56:53) Youtube Odysee DRM/KMS, MIPI DSI, panels,... slightly old, need something newer.
  • Demystifying Linux MIPI DSI Subsystem - Jagan Teki, 2019 (41:57) Youtube Odysee MIPI DSI, controllers, bridges, panels, DRM/KMS...
  • Power Management Integrated Circuits: Keep the Power in Your Hands, 2017 Youtube Odysee Slides About PMICs, regulators, power supply, fuelgauge, ...

Development tools

  • Using Visual Studio Code for Embedded Development - Michael Opdenacker, Bootlin, 2021 (31:37) Youtube

Note: for VSCodium, download extensions from https://marketplace.visualstudio.com/ manually.

Submitting patches:

Upstreaming

Upstreaming your mainline efforts is key to reduce the maintenance burden of rebasing your patches on newer version of the Linux kernel.

Upstreaming patches requires sending to the Linux Kernel Mailing List using git send-email. For a tutorial on git send-email have a look at https://git-send-email.io/.

See https://wiki.postmarketos.org/wiki/Submitting_Patches#Linux for a detailed overview for submitting patches upstream.

See also