spacemit/k3-pico-itx: init

This commit is contained in:
liberodark committed 2026-06-18 21:08:28 +02:00
1 parent c97bc4d15b
commit 57abfe19b2
10 files changed
+644

No files matched your search

+1
View File
@@ -424,6 +424,7 @@
kobol-helios4 = import ./kobol/helios4;
samsung-np900x3c = import ./samsung/np900x3c;
slimbook-hero-rpl-rtx = import ./slimbook/hero/rpl-rtx;
spacemit-k3-pico-itx = import ./spacemit/k3-pico-itx;
starfive-visionfive-v1 = import ./starfive/visionfive/v1;
starfive-visionfive-2 = import ./starfive/visionfive/v2;
starlabs-starlite-i5 = import ./starlabs/starlite/i5;
+145
View File
@@ -0,0 +1,145 @@
# SpacemiT K3 Pico-ITX
NixOS support for the [SpacemiT K3 Pico-ITX](https://www.spacemit.com/community/document/info?nodepath=hardware%2Feco%2Fk3_pico%2Fpico_user_guide.md&lang=en) single-board
computer, built around the SpacemiT K3 SoC (RVA23 RISC-V).
## Hardware
| Component | Detail |
| --------- | ------ |
| SoC | SpacemiT K3 (RVA23, 64-bit RISC-V) |
| CPU | 16 cores: 8× X100 @ 2.4 GHz + 8× A100 @ 2.0 GHz |
| Memory | LPDDR5, up to 32 GB |
| Storage | PCIe Gen3 ×4 (NVMe), internal UFS, microSD |
| Network | RTL8211F 1GbE (`end0`), RTL8127 10GbE (PCIe), RTL8852BE Wi-Fi 6 (PCIe) |
| Firmware | EDK2 UEFI + U-Boot + OpenSBI |
## Usage
Add the module to your configuration:
```nix
{
imports = [
"${nixos-hardware}/spacemit/k3-pico-itx"
];
}
```
This gives you the base setup with the vendor `linux-6.18` kernel and
EDK2/U-Boot/OpenSBI. I plan to move to the mainline kernel.
### Building an SD-image
```nix
{
imports = [
"${nixos-hardware}/spacemit/k3-pico-itx/sd-image.nix"
];
# Set a password or add an SSH key so you can log in
# users.users.nixos.password = "changeme";
# users.users.nixos.openssh.authorizedKeys.keys = [ "ssh-ed25519 ..." ];
}
```
```sh
nix build .#sd-image
```
## Cache
To install and update your system I set up a binary cache:
`build05.ynh.ovh`
This saves you from compiling everything locally.
Here is how to add it to your configuration:
```nix
nix.settings = {
substituters = [ "https://build05.ynh.ovh" ];
trusted-public-keys = [
"build05.ynh.ovh:bLxWKPjbKYOFxqrjOxv+cdwS3kFLuHEf1k6j8fAxbzM="
];
};
```
Here is how to use it without adding it to your configuration:
```sh
sudo nix-channel --add https://channels.nixos.org/nixos-26.05 nixos
sudo nix-channel --update
nix build \
--substituters https://build05.ynh.ovh \
--trusted-public-keys "build05.ynh.ovh:bLxWKPjbKYOFxqrjOxv+cdwS3kFLuHEf1k6j8fAxbzM="
```
PS: This is a cache maintained on a voluntary basis for the K3 and riscv64
hardware. I can't guarantee its availability, nor that everything is already
built on it. The Hydra infrastructure is still being deployed, so please be
lenient in that regard.
## HMP configuration
The X100 and A100 clusters have different vector widths (`VLENB=32` vs `VLENB=128`).
You can configure HMP at runtime:
`echo strict > /sys/kernel/spacemit_hmp/mode` (default)
or
`echo permissive > /sys/kernel/spacemit_hmp/mode`
Permissive mode lets you use the 8 A100 cores in addition to the X100 cores.
However, I deliberately apply a segmentation between the two clusters because of a
VLEN issue.
If you force tasks across both clusters anyway (e.g. via `taskset`), it will cause
crypto corruption.
I also made a small module for this:
```nix
hardware.spacemit.hmp = {
enable = true;
mode = "permissive"; # or "strict"
};
```
- **`strict`** (default): uses the X100 cores only.
- **`permissive`**: uses both the X100 and A100 cores, but with a segmentation
between the clusters.
An alternative is still available to use all 16 cores without segmentation, with
this:
```nix
environment.variables.OPENSSL_riscvcap = "RV64GC";
```
## UEFI
To get UEFI I made a small script, to be run from Bianbu:
https://github.com/liberodark/k3-uefi-flash
## Status
Working:
- Boot to NixOS, SSH reachable
- NVMe root (PCIe Gen3 ×4), 1GbE (`end0`)
- All 16 cores usable (see HMP notes above)
Known issues:
- I noticed a reboot issue, so I force it with `reboot -ff`.
## Upstream status
- [Kernel](https://github.com/spacemit-com/linux/wiki)
- [OpenSBI](https://github.com/spacemit-com/opensbi-upstream/wiki)
- [U-Boot](https://github.com/spacemit-com/u-boot/wiki)
- [LLVM/GCC](https://github.com/spacemit-com/.github/blob/main/upstream-status/toolchain.md)
- [llama.cpp](https://github.com/spacemit-com/llama.cpp/wiki)
+75
View File
@@ -0,0 +1,75 @@
{
config,
lib,
pkgs,
...
}:
{
imports = [ ./firmware.nix ];
boot = {
consoleLogLevel = lib.mkDefault 7;
kernelPackages = lib.mkDefault (
pkgs.linuxPackagesFor (
pkgs.callPackage ./linux.nix {
inherit (config.boot) kernelPatches;
}
)
);
kernelParams = [
"earlycon=sbi"
"keep_bootcon"
"systemd.journald.forward_to_console=1"
"systemd.log_target=kmsg"
"systemd.log_level=info"
"clk_ignore_unused"
"pd_ignore_unused"
"root=PARTLABEL=nixos-rootfs"
"rootwait"
"rootfstype=ext4"
];
initrd = {
availableKernelModules = [
"usb_storage"
"uas"
"nvme"
"nvme_core"
];
kernelModules = [
"erofs"
"loop"
];
};
loader = {
systemd-boot.enable = lib.mkDefault true;
systemd-boot.consoleMode = lib.mkDefault "auto";
timeout = lib.mkDefault 3;
efi.canTouchEfiVariables = lib.mkDefault false;
efi.efiSysMountPoint = lib.mkDefault "/boot";
generic-extlinux-compatible.enable = lib.mkDefault false;
};
zfs.forceImportRoot = lib.mkDefault false;
};
hardware.deviceTree = {
enable = lib.mkDefault true;
name = lib.mkDefault "spacemit/k3-pico-itx.dtb";
};
boot.initrd.systemd.enable = lib.mkDefault true;
system.nixos-init.enable = lib.mkDefault true;
system.etc.overlay.enable = lib.mkDefault true;
services.userborn.enable = lib.mkDefault true;
systemd.services."serial-getty@ttyS0" = {
enable = true;
wantedBy = [ "getty.target" ];
serviceConfig.Restart = "always";
};
}
+28
View File
@@ -0,0 +1,28 @@
{
stdenvNoCC,
fetchFromGitHub,
}:
stdenvNoCC.mkDerivation {
pname = "edk2-spacemit-k3";
version = "ubuntu26.04-20260509";
src = fetchFromGitHub {
owner = "spacemit-com";
repo = "K3-Ubuntu-Images";
tag = "ubuntu26.04-20260509";
hash = "sha256-J0T3T/gOhKmld1du7J2z+DimZXShb+zoON3DHIz4Hy0=";
};
dontConfigure = true;
dontBuild = true;
installPhase = ''
runHook preInstall
mkdir -p $out
cp gadget.in/edk2.itb $out/
runHook postInstall
'';
}
+103
View File
@@ -0,0 +1,103 @@
{
config,
pkgs,
lib,
...
}:
let
cfg = config.hardware.k3-pico-itx;
in
{
options.hardware.k3-pico-itx = {
opensbi = {
src = lib.mkOption {
description = "K3 Pico-ITX OpenSBI source override.";
type = lib.types.nullOr lib.types.package;
default = null;
};
patches = lib.mkOption {
description = "Additional patches to apply to K3 Pico-ITX OpenSBI.";
type = lib.types.nullOr (lib.types.listOf lib.types.package);
default = null;
};
};
uboot = {
src = lib.mkOption {
description = "K3 Pico-ITX U-Boot source override.";
type = lib.types.nullOr lib.types.package;
default = null;
};
patches = lib.mkOption {
description = "Additional patches to apply to K3 Pico-ITX U-Boot.";
type = lib.types.nullOr (lib.types.listOf lib.types.package);
default = null;
};
};
fsbl = {
src = lib.mkOption {
description = "K3 Pico-ITX FSBL source override.";
type = lib.types.nullOr lib.types.package;
default = null;
};
patches = lib.mkOption {
description = "Additional patches to apply to K3 Pico-ITX FSBL.";
type = lib.types.nullOr (lib.types.listOf lib.types.package);
default = null;
};
};
edk2 = {
src = lib.mkOption {
description = "K3 Pico-ITX EDK2 (UEFI) source override.";
type = lib.types.nullOr lib.types.package;
default = null;
};
};
};
config = {
system.build = {
opensbi = (pkgs.callPackage ./opensbi.nix { }).overrideAttrs (
_final: prev: {
src = if cfg.opensbi.src != null then cfg.opensbi.src else prev.src;
patches = if cfg.opensbi.patches != null then cfg.opensbi.patches else (prev.patches or [ ]);
}
);
uboot = (pkgs.callPackage ./uboot.nix { }).overrideAttrs (
_final: prev: {
src = if cfg.uboot.src != null then cfg.uboot.src else prev.src;
patches = if cfg.uboot.patches != null then cfg.uboot.patches else (prev.patches or [ ]);
}
);
fsbl = (pkgs.callPackage ./fsbl.nix { }).overrideAttrs (
_final: prev: {
src = if cfg.fsbl.src != null then cfg.fsbl.src else prev.src;
patches = if cfg.fsbl.patches != null then cfg.fsbl.patches else (prev.patches or [ ]);
}
);
edk2 = (pkgs.callPackage ./edk2.nix { }).overrideAttrs (
_final: prev: {
src = if cfg.edk2.src != null then cfg.edk2.src else prev.src;
}
);
updater-sd = pkgs.writeShellApplication {
name = "k3-pico-itx-firmware-update-sd";
runtimeInputs = [ pkgs.coreutils ];
text = ''
dev=''${1:-/dev/mmcblk0}
dd if=${config.system.build.fsbl}/FSBL.bin of=''${dev}p3 conv=fsync,notrunc
dd if=${config.system.build.opensbi}/share/opensbi/lp64/generic/firmware/fw_dynamic.itb of=''${dev}p5 conv=fsync,notrunc
dd if=${config.system.build.edk2}/edk2.itb of=''${dev}p6 conv=fsync,notrunc
'';
};
};
};
}
+24
View File
@@ -0,0 +1,24 @@
{
buildUBoot,
fetchFromGitHub,
}:
buildUBoot {
version = "2022.10-k3-br-v1.0.y";
src = fetchFromGitHub {
owner = "liberodark";
repo = "spacemit-uboot-2022.10";
rev = "368953aa0b648528ddafa5b6baa73507448ab94d";
hash = "sha256-LTtxHzL5aoMLlv8DsgEoyIM0JrwYYzNlHCAmXzixScI=";
};
defconfig = "k3_defconfig";
filesToInstall = [
"FSBL.bin"
"bootinfo_block.bin"
"bootinfo_spinor.bin"
"bootinfo_spinand.bin"
];
}
+98
View File
@@ -0,0 +1,98 @@
{
buildLinux,
fetchFromGitHub,
kernelPatches,
lib,
...
}
@args:
let
modDirVersion = "6.18.3";
in
buildLinux (
args
// {
inherit kernelPatches modDirVersion;
version = "${modDirVersion}-spacemit-k3";
src = fetchFromGitHub {
owner = "liberodark";
repo = "spacemit-linux-6.18";
rev = "3709ed51967962fd02d6b4e5a80234719693931f";
hash = "sha256-itqxXJCogLHbGucVHCDVwGkoqunQjJs0i1ZfvkuUtP4=";
};
defconfig = "k3_bianbu_defconfig";
postPatch = ''
sed -i '/CONFIG_INITRAMFS_SOURCE=/d' arch/riscv/configs/k3_bianbu_defconfig
'';
structuredExtraConfig =
let
inherit (lib.kernel) yes no;
in
{
CGROUPS = yes;
MEMCG = yes;
CGROUP_BPF = yes;
BPF = yes;
BPF_SYSCALL = yes;
BPF_JIT = yes;
NAMESPACES = yes;
USER_NS = yes;
SECCOMP = yes;
SECCOMP_FILTER = yes;
DEVTMPFS = yes;
DEVTMPFS_MOUNT = yes;
AUTOFS_FS = yes;
FANOTIFY = yes;
TMPFS_POSIX_ACL = yes;
TMPFS_XATTR = yes;
MODULES = yes;
MODULE_UNLOAD = yes;
RD_GZIP = yes;
RD_BZIP2 = yes;
RD_LZMA = yes;
RD_XZ = yes;
RD_LZO = yes;
RD_LZ4 = yes;
RD_ZSTD = yes;
# Disable
PSTORE_BLK = no;
PSTORE_BLK_BLKDEV = no;
RTL8852BS = no;
CORESIGHT = no;
ETHERCAT = no;
HWSPINLOCK_SPACEMIT = no;
DRM_AMDGPU = no;
DRM_RADEON = no;
DRM_NOUVEAU = no;
SPACEMIT_K1_CCU = no;
NVMEM_LAYOUT_ONIE_TLV = no;
DEBUG_VM = no;
DEBUG_VM_PGFLAGS = no;
DEBUG_VM_PGTABLE = no;
DEBUG_PAGEALLOC = no;
DEBUG_PAGEALLOC_ENABLE_DEFAULT = no;
PAGE_EXTENSION = lib.mkForce no;
PAGE_OWNER = no;
KASAN = no;
KMEMLEAK = no;
DEBUG_KMEMLEAK = no;
SLUB_DEBUG_ON = no;
MEM_ALLOC_PROFILING = lib.mkForce no;
MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT = lib.mkForce no;
NO_HZ_FULL = lib.mkForce no;
SLAB_FREELIST_HARDENED = lib.mkForce no;
SLAB_FREELIST_RANDOM = lib.mkForce no;
KFENCE = lib.mkForce no;
};
extraMeta.branch = "k3-br-v1.0.y";
}
// (args.argsOverride or { })
)
+31
View File
@@ -0,0 +1,31 @@
{
opensbi,
fetchFromGitHub,
buildPackages,
}:
opensbi.overrideAttrs (old: {
version = "k3-br-v1.0.y";
src = fetchFromGitHub {
owner = "liberodark";
repo = "spacemit-openspi";
rev = "3e2f9efc9660b8d5fcae4e0b6495f306d5c64078";
hash = "sha256-pJ9izkJ3eNwF1km335ZjCezWhOSYxOMm1fo/19BoG+I=";
};
postPatch = ''
patchShebangs scripts/
cp platform/generic/configs/k3_defconfig platform/generic/configs/defconfig
'';
nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ [
buildPackages.ubootTools
buildPackages.dtc
];
postInstall = ''
cp build/platform/generic/firmware/fw_dynamic.itb \
$out/share/opensbi/lp64/generic/firmware/fw_dynamic.itb
'';
})
+115
View File
@@ -0,0 +1,115 @@
{
config,
lib,
modulesPath,
pkgs,
...
}:
{
imports = [
"${modulesPath}/profiles/base.nix"
"${modulesPath}/installer/sd-card/sd-image.nix"
./default.nix
./firmware.nix
];
hardware.enableAllHardware = lib.mkForce false;
fileSystems."/" = lib.mkForce {
device = "/dev/disk/by-partlabel/nixos-rootfs";
fsType = "ext4";
};
fileSystems."/boot" = {
device = "/dev/disk/by-label/ESP";
fsType = "vfat";
options = [
"fmask=0077"
"dmask=0077"
];
};
image.fileName = "${config.image.baseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-k3-pico-itx.img";
sdImage = {
expandOnBoot = false;
firmwarePartitionOffset = 12;
firmwarePartitionName = "ESP";
firmwareSize = 256;
populateFirmwareCommands =
let
systemdBoot = "${pkgs.systemd}/lib/systemd/boot/efi/systemd-bootriscv64.efi";
kernel = "${config.system.build.kernel}/${config.system.boot.loader.kernelFile}";
initrd = "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}";
dtb = "${config.hardware.deviceTree.package}/${config.hardware.deviceTree.name}";
kernelParams = lib.concatStringsSep " " config.boot.kernelParams;
in
''
mkdir -p firmware/EFI/BOOT firmware/EFI/systemd firmware/EFI/nixos
mkdir -p firmware/loader/entries
cp ${systemdBoot} firmware/EFI/BOOT/BOOTRISCV64.EFI
cp ${systemdBoot} firmware/EFI/systemd/systemd-bootriscv64.efi
cp ${kernel} firmware/EFI/nixos/kernel.efi
cp ${initrd} firmware/EFI/nixos/initrd
cp ${dtb} firmware/EFI/nixos/k3-pico-itx.dtb
cat > firmware/loader/loader.conf <<EOF
default nixos
timeout 3
console-mode auto
EOF
cat > firmware/loader/entries/nixos.conf <<EOF
title NixOS
linux /EFI/nixos/kernel.efi
initrd /EFI/nixos/initrd
devicetree /EFI/nixos/k3-pico-itx.dtb
options init=${config.system.build.toplevel}/init ${kernelParams}
EOF
cat > firmware/startup.nsh <<EOF
@echo -off
echo Booting NixOS...
for %i in 0 1 2 3 4 5
if exist FS%i:\EFI\BOOT\BOOTRISCV64.EFI then
FS%i:\EFI\BOOT\BOOTRISCV64.EFI
endif
endfor
EOF
'';
populateRootCommands = "";
postBuildCommands = ''
eval $(${pkgs.util-linux}/bin/partx $img -o START,SECTORS --nr 2 --pairs)
ROOTFS_START=$START
ROOTFS_SECTORS=$SECTORS
truncate -s '+2M' $img
${pkgs.util-linux}/bin/sfdisk $img <<EOF
label: gpt
unit: sectors
sector-size: 512
first-lba: 256
start=1280, size=128, name="env", type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
start=2048, size=256, name="bootinfo", type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
start=3072, size=1024, name="fsbl", type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
start=8192, size=6144, name="esos", type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
start=14336, size=2048, name="opensbi", type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
start=16384, size=8192, name="uboot", type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
start=24576, size=524288, name="ESP", type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
start=$ROOTFS_START, size=$ROOTFS_SECTORS, name="nixos-rootfs", type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
EOF
dd if=${config.system.build.fsbl}/bootinfo_block.bin of=$img conv=notrunc bs=1M seek=1
dd if=${config.system.build.fsbl}/FSBL.bin of=$img conv=notrunc bs=1024 seek=1536
dd if=${config.system.build.opensbi}/share/opensbi/lp64/generic/firmware/fw_dynamic.itb of=$img conv=notrunc bs=1M seek=7
dd if=${config.system.build.edk2}/edk2.itb of=$img conv=notrunc bs=1M seek=8
'';
};
}
+24
View File
@@ -0,0 +1,24 @@
{
buildUBoot,
fetchFromGitHub,
}:
buildUBoot {
version = "2022.10-k3-br-v1.0.y";
src = fetchFromGitHub {
owner = "liberodark";
repo = "spacemit-uboot-2022.10";
rev = "368953aa0b648528ddafa5b6baa73507448ab94d";
hash = "sha256-LTtxHzL5aoMLlv8DsgEoyIM0JrwYYzNlHCAmXzixScI=";
};
defconfig = "k3_defconfig";
filesToInstall = [ "u-boot.itb" ];
extraConfig = ''
CONFIG_SYS_CBSIZE=2048
CONFIG_SYS_BARGSIZE=2048
'';
}