diff --git a/flake.nix b/flake.nix index 7abaff1c..6d34bf1b 100644 --- a/flake.nix +++ b/flake.nix @@ -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; diff --git a/spacemit/k3-pico-itx/README.md b/spacemit/k3-pico-itx/README.md new file mode 100644 index 00000000..0ca17733 --- /dev/null +++ b/spacemit/k3-pico-itx/README.md @@ -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) diff --git a/spacemit/k3-pico-itx/default.nix b/spacemit/k3-pico-itx/default.nix new file mode 100644 index 00000000..e5b5c7de --- /dev/null +++ b/spacemit/k3-pico-itx/default.nix @@ -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"; + }; +} diff --git a/spacemit/k3-pico-itx/edk2.nix b/spacemit/k3-pico-itx/edk2.nix new file mode 100644 index 00000000..2fe4e780 --- /dev/null +++ b/spacemit/k3-pico-itx/edk2.nix @@ -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 + ''; +} diff --git a/spacemit/k3-pico-itx/firmware.nix b/spacemit/k3-pico-itx/firmware.nix new file mode 100644 index 00000000..bca467c8 --- /dev/null +++ b/spacemit/k3-pico-itx/firmware.nix @@ -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 + ''; + }; + }; + }; +} diff --git a/spacemit/k3-pico-itx/fsbl.nix b/spacemit/k3-pico-itx/fsbl.nix new file mode 100644 index 00000000..b7454f50 --- /dev/null +++ b/spacemit/k3-pico-itx/fsbl.nix @@ -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" + ]; +} diff --git a/spacemit/k3-pico-itx/linux.nix b/spacemit/k3-pico-itx/linux.nix new file mode 100644 index 00000000..bc742988 --- /dev/null +++ b/spacemit/k3-pico-itx/linux.nix @@ -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 { }) +) diff --git a/spacemit/k3-pico-itx/opensbi.nix b/spacemit/k3-pico-itx/opensbi.nix new file mode 100644 index 00000000..384767d9 --- /dev/null +++ b/spacemit/k3-pico-itx/opensbi.nix @@ -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 + ''; +}) diff --git a/spacemit/k3-pico-itx/sd-image.nix b/spacemit/k3-pico-itx/sd-image.nix new file mode 100644 index 00000000..b09b0c3a --- /dev/null +++ b/spacemit/k3-pico-itx/sd-image.nix @@ -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 < firmware/loader/entries/nixos.conf < firmware/startup.nsh <