From 38af9c5048e15e1f81209d01a98ef3884bd554ba Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Wed, 12 May 2021 20:51:38 -0400 Subject: [PATCH] raspberry-pi/4: Add modesetting option --- raspberry-pi/4/default.nix | 4 ++ raspberry-pi/4/modesetting.nix | 107 +++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 raspberry-pi/4/modesetting.nix diff --git a/raspberry-pi/4/default.nix b/raspberry-pi/4/default.nix index 1ac0acb..063bbcc 100644 --- a/raspberry-pi/4/default.nix +++ b/raspberry-pi/4/default.nix @@ -1,6 +1,10 @@ { lib, pkgs, ...}: { + imports = [ + ./modesetting.nix + ]; + boot = { kernelPackages = lib.mkDefault pkgs.linuxPackages_rpi4; initrd.availableKernelModules = [ "usbhid" "usb_storage" "vc4" ]; diff --git a/raspberry-pi/4/modesetting.nix b/raspberry-pi/4/modesetting.nix new file mode 100644 index 0000000..9339b43 --- /dev/null +++ b/raspberry-pi/4/modesetting.nix @@ -0,0 +1,107 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.hardware.raspberry-pi."4".fkms-3d; +in +{ + options.hardware = { + raspberry-pi."4".fkms-3d = { + enable = lib.mkEnableOption '' + Enable modesetting through fkms-3d + ''; + cma = lib.mkOption { + type = lib.types.int; + default = 512; + description = '' + Amount of CMA (contiguous memory allocator) to reserve, in MiB. + + The foundation overlay defaults to 256MiB, for backward compatibility. + As the Raspberry Pi 4 family of hardware has ample amount of memory, we + can reserve more without issue. + + Additionally, reserving too much is not an issue. The kernel will use + CMA last if the memory is needed. + ''; + }; + }; + }; + + config = lib.mkIf cfg.enable { + # Configure for modesetting in the device tree + hardware.deviceTree = { + filter = "bcm2711-rpi-*.dtb"; + overlays = [ + # Equivalent to: + # https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/overlays/cma-overlay.dts + { + name = "rpi4-cma-overlay"; + dtsText = '' + // SPDX-License-Identifier: GPL-2.0 + /dts-v1/; + /plugin/; + + / { + compatible = "brcm,bcm2711"; + + fragment@0 { + target = <&cma>; + __overlay__ { + size = <(${toString cfg.cma} * 1024 * 1024)>; + }; + }; + }; + ''; + } + # Equivalent to: + # https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/overlays/vc4-fkms-v3d-overlay.dts + { + name = "rpi4-vc4-fkms-v3d-overlay"; + dtsText = '' + // SPDX-License-Identifier: GPL-2.0 + /dts-v1/; + /plugin/; + + / { + compatible = "brcm,bcm2711"; + + fragment@1 { + target = <&fb>; + __overlay__ { + status = "disabled"; + }; + }; + + fragment@2 { + target = <&firmwarekms>; + __overlay__ { + status = "okay"; + }; + }; + + fragment@3 { + target = <&v3d>; + __overlay__ { + status = "okay"; + }; + }; + + fragment@4 { + target = <&vc4>; + __overlay__ { + status = "okay"; + }; + }; + }; + ''; + } + ]; + }; + + # Also configure the system for modesetting. + + services.xserver.videoDrivers = lib.mkBefore [ + "modesetting" # Prefer the modesetting driver in X11 + "fbdev" # Fallback to fbdev + ]; + }; +}