db845c|rb5: Add support for parallel kernel moduel loading

Enable parallel kernel moduel loading support by setting
TARGET_USES_LMP to true during build time.

This feature is disabled by default for now, while we are
debugging a kernel panic at arm_smmu_probe_device(),
https://bugs.linaro.org/show_bug.cgi?id=5996.

Smoke tested on android13-5.15, android14-5.15,
android14-6.1 and android15-6.6.

Note: Not boot tested on the mmc-sdcard based builds because
      those builds still use boot image header v2.
      To enable this feature while booting from a mmc-sdcard
      move sqhci.ko and sdhci-mmc.ko driver modules from
      VENDOR_DLKM_KERNEL_MODULES_LIST to
      VENDOR_RAMDISK_KERNEL_MODULES_LIST in
      vendor.modules.list.mk.

Change-Id: Idf9de61c8322b84a4f834e208c6a1fc71469555c
Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
diff --git a/db845c.mk b/db845c.mk
index 72b2c26..c83da69 100644
--- a/db845c.mk
+++ b/db845c.mk
@@ -6,14 +6,23 @@
 $(call inherit-product, device/linaro/dragonboard/shared/graphics/minigbm_msm/device.mk)
 $(call inherit-product, device/linaro/dragonboard/db845c/device.mk)
 
+#Boot with Android boot image header v3
 ifeq ($(TARGET_KERNEL_USE),5.4)
   TARGET_USES_BOOT_HDR_V3 := true
 else
   TARGET_USES_BOOT_HDR_V3 ?= false
 endif
 
+#Boot from mmc-sdcard
 TARGET_SDCARD_BOOT ?= false
 
+#Load kernel modules in parallel
+ifneq ($(filter 5.15 6.1 6.6, $(TARGET_KERNEL_USE)),)
+  TARGET_USES_LMP ?= false
+else
+  TARGET_USES_LMP := false
+endif
+
 # Product overrides
 PRODUCT_NAME := db845c
 PRODUCT_DEVICE := db845c
diff --git a/db845c/BoardConfig.mk b/db845c/BoardConfig.mk
index 881dbc7..ed9264c 100644
--- a/db845c/BoardConfig.mk
+++ b/db845c/BoardConfig.mk
@@ -43,6 +43,9 @@
     BOARD_BOOTCONFIG += androidboot.boot_devices=soc@0/1d84000.ufshc
     BOARD_BOOTCONFIG += androidboot.hardware=db845c
     BOARD_BOOTCONFIG += androidboot.verifiedbootstate=orange
+    ifeq ($(TARGET_USES_LMP), true)
+      BOARD_BOOTCONFIG += androidboot.load_modules_parallel=true
+    endif
 endif
 BOARD_MKBOOTIMG_ARGS := --header_version $(BOARD_BOOT_HEADER_VERSION)
 BOARD_KERNEL_BASE := 0x80000000
diff --git a/device-common.mk b/device-common.mk
index a197f86..960c2ae 100644
--- a/device-common.mk
+++ b/device-common.mk
@@ -23,6 +23,11 @@
   # UFS module filename varies from ufs_qcom.ko to ufs-qcom.ko across different kernel versions
   BOARD_VENDOR_KERNEL_MODULES := $(wildcard $(TARGET_KERNEL_DIR)/ufs*qcom.ko)
   BOARD_GENERIC_RAMDISK_KERNEL_MODULES := $(filter-out $(BOARD_VENDOR_KERNEL_MODULES),$(TARGET_MODS))
+else ifeq ($(TARGET_USES_LMP), true)
+    include device/linaro/dragonboard/shared/utils/dlkm_loader/vendor.modules.list.mk
+    BOARD_VENDOR_KERNEL_MODULES := $(patsubst %,$(TARGET_KERNEL_DIR)/%,$(VENDOR_DLKM_KERNEL_MODULES_LIST))
+    BOARD_VENDOR_RAMDISK_KERNEL_MODULES := $(patsubst %,$(TARGET_KERNEL_DIR)/%,$(VENDOR_RAMDISK_KERNEL_MODULES_LIST))
+    BOARD_SYSTEM_KERNEL_MODULES := $(filter-out $(BOARD_VENDOR_KERNEL_MODULES) $(BOARD_VENDOR_RAMDISK_KERNEL_MODULES), $(wildcard $(TARGET_KERNEL_DIR)/*.ko))
 else
   BOARD_VENDOR_RAMDISK_KERNEL_MODULES := $(TARGET_MODS)
 endif
diff --git a/sepolicy/rmtfs.te b/sepolicy/rmtfs.te
index 7cef38f..91f8d33 100644
--- a/sepolicy/rmtfs.te
+++ b/sepolicy/rmtfs.te
@@ -11,5 +11,6 @@
 allow rmtfs sysfs_mss:dir { open read search };
 allow rmtfs sysfs_remoteproc:dir { open read search };
 allow rmtfs sysfs_remoteproc:file { open write };
+allow rmtfs sysfs_remoteproc:file read;
 allow rmtfs sysfs_rmtfs:dir search;
 allow rmtfs sysfs_rmtfs:file { open read };
diff --git a/sepolicy/set_ethaddr.te b/sepolicy/set_ethaddr.te
index 86f9ea8..2f92368 100644
--- a/sepolicy/set_ethaddr.te
+++ b/sepolicy/set_ethaddr.te
@@ -6,6 +6,7 @@
 allow set_ethaddr proc_bootconfig:file { open read };
 allow set_ethaddr rootfs:dir { open read };
 allow set_ethaddr self:capability net_admin;
+allow set_ethaddr self:capability sys_module;
 allow set_ethaddr self:udp_socket create_socket_perms;
 allow set_ethaddr vendor_file:file execute_no_trans;
 allow set_ethaddr vendor_toolbox_exec:file execute_no_trans;
diff --git a/shared/utils/dlkm_loader/dlkm_loader.rc b/shared/utils/dlkm_loader/dlkm_loader.rc
index 7af8bac..b1e2c41 100644
--- a/shared/utils/dlkm_loader/dlkm_loader.rc
+++ b/shared/utils/dlkm_loader/dlkm_loader.rc
@@ -1,6 +1,13 @@
 on early-init
     # start module load in the background
     start vendor.dlkm_loader
+    # load "leaf" GKI modules
+    exec u:r:modprobe:s0 -- /system/bin/modprobe -a -d /system/lib/modules zram.ko
+    exec u:r:modprobe:s0 -- /system/bin/modprobe -a -d /system/lib/modules hci_uart.ko
+    # led-class-multicolor.ko as found in android14-5.15 but missing in android13-5.15
+    exec u:r:modprobe:s0 -- /system/bin/modprobe -a -d /system/lib/modules led-class-multicolor.ko
+    # regmap-spmi.ko as found in android13-5.15 but missing in android14-5.15
+    exec u:r:modprobe:s0 -- /system/bin/modprobe -a -d /system/lib/modules regmap-spmi.ko
 
 service vendor.dlkm_loader /vendor/bin/dlkm_loader
     class main
diff --git a/shared/utils/dlkm_loader/sepolicy/dlkm_loader.te b/shared/utils/dlkm_loader/sepolicy/dlkm_loader.te
index be36f35..e6fd7c3 100644
--- a/shared/utils/dlkm_loader/sepolicy/dlkm_loader.te
+++ b/shared/utils/dlkm_loader/sepolicy/dlkm_loader.te
@@ -3,6 +3,7 @@
 init_daemon_domain(dlkm_loader)
 
 # Allow insmod on vendor, system and system_dlkm partitions
+allow dlkm_loader self:capability net_admin;
 allow dlkm_loader self:capability sys_module;
 allow dlkm_loader system_dlkm_file:dir r_dir_perms;
 allow dlkm_loader system_dlkm_file:file r_file_perms;
@@ -11,8 +12,25 @@
 allow dlkm_loader vendor_file:system module_load;
 
 # needed for libmodprobe to read kernel commandline
-allow dlkm_loader proc_cmdline:file r_file_perms;
 allow dlkm_loader proc_bootconfig:file r_file_perms;
+allow dlkm_loader proc_cmdline:file r_file_perms;
+
+# Needed because CONFIG_USB_DUMMY_HCD adds some additional logic to
+# finit_module() syscall, causing that syscall to create/update keyrings.
+# Once we remove CONFIG_USB_DUMMY_HCD config, self:key write permission can be
+# removed.
+allow dlkm_loader self:key write;
+
+# Needed because CONFIG_DMABUF_HEAPS_DEFERRED_FREE calls sched_set_normal()
+# in its module_init() function, which is attributed to dlkm_loader.
+allow dlkm_loader kernel:process setsched;
+allow dlkm_loader self:capability sys_nice;
 
 # Allow writing to kernel log
 allow dlkm_loader kmsg_device:chr_file rw_file_perms;
+
+# dlkm_loader searches tracefs while looking for modules
+dontaudit dlkm_loader debugfs_bootreceiver_tracing:dir search;
+dontaudit dlkm_loader debugfs_mm_events_tracing:dir search;
+
+set_prop(dlkm_loader, vendor_device_prop)
diff --git a/shared/utils/dlkm_loader/sepolicy/property.te b/shared/utils/dlkm_loader/sepolicy/property.te
new file mode 100644
index 0000000..50f7b34
--- /dev/null
+++ b/shared/utils/dlkm_loader/sepolicy/property.te
@@ -0,0 +1 @@
+vendor_internal_prop(vendor_device_prop)
diff --git a/shared/utils/dlkm_loader/sepolicy/property_contexts b/shared/utils/dlkm_loader/sepolicy/property_contexts
new file mode 100644
index 0000000..2a347b3
--- /dev/null
+++ b/shared/utils/dlkm_loader/sepolicy/property_contexts
@@ -0,0 +1 @@
+vendor.dlkm.modules.ready      u:object_r:vendor_device_prop:s0 exact bool
diff --git a/shared/utils/dlkm_loader/vendor.modules.list.mk b/shared/utils/dlkm_loader/vendor.modules.list.mk
new file mode 100644
index 0000000..f28f618
--- /dev/null
+++ b/shared/utils/dlkm_loader/vendor.modules.list.mk
@@ -0,0 +1,231 @@
+VENDOR_DLKM_KERNEL_MODULES_LIST := \
+  ath10k_core.ko \
+  ath10k_pci.ko \
+  ath10k_snoc.ko \
+  ath11k_ahb.ko \
+  ath11k.ko \
+  ath11k_pci.ko \
+  ath.ko \
+  clk-spmi-pmic-div.ko \
+  cqhci.ko \
+  gpu-sched.ko \
+  lmh.ko \
+  lontium-lt9611.ko \
+  lontium-lt9611uxc.ko \
+  mcp251xfd.ko \
+  michael_mic.ko \
+  msm.ko \
+  nvmem_qfprom.ko \
+  qcom-pon.ko \
+  qcom_q6v5_mss.ko \
+  qcom_q6v5_pas.ko \
+  qcom_q6v5_wcss.ko \
+  qcom-spmi-adc5.ko \
+  qcom-spmi-adc-tm5.ko \
+  qcom-spmi-pmic.ko \
+  qcom_spmi-regulator.ko \
+  qcom-spmi-temp-alarm.ko \
+  qcom_tsens.ko \
+  reboot-mode.ko \
+  sdhci-msm.ko \
+  syscon-reboot-mode.ko \
+  system_heap.ko \
+  videocc-sdm845.ko \
+  videocc-sm8250.ko
+
+VENDOR_RAMDISK_KERNEL_MODULES_LIST := \
+  apr.ko \
+  arm_smmu.ko \
+  bam_dma.ko \
+  clk-qcom.ko \
+  clk-rpmh.ko \
+  cmd-db.ko \
+  dispcc-sdm845.ko \
+  dispcc-sm8250.ko \
+  display-connector.ko \
+  extcon-usb-gpio.ko \
+  fastrpc.ko \
+  gcc-sdm845.ko \
+  gcc-sm8250.ko \
+  gpio-regulator.ko \
+  gpio-wcd934x.ko \
+  gpucc-sdm845.ko \
+  gpucc-sm8250.ko \
+  i2c-designware-core.ko \
+  i2c-designware-platform.ko \
+  i2c-dev.ko \
+  i2c-mux.ko \
+  i2c-mux-pca954x.ko \
+  i2c-qcom-geni.ko \
+  i2c-qup.ko \
+  i2c-rk3x.ko \
+  icc-bcm-voter.ko \
+  icc-osm-l3.ko \
+  icc-rpmh.ko \
+  llcc-qcom.ko \
+  lpass-gfm-sm8250.ko \
+  mdt_loader.ko \
+  mhi.ko \
+  msm_serial.ko \
+  ohci-hcd.ko \
+  ohci-pci.ko \
+  ohci-platform.ko \
+  pdr_interface.ko \
+  phy-qcom-qusb2.ko \
+  phy-qcom-snps-femto-v2.ko \
+  phy-qcom-usb-hs.ko \
+  pinctrl-lpass-lpi.ko \
+  pinctrl-msm.ko \
+  pinctrl-sdm845.ko \
+  pinctrl-sm8250.ko \
+  pinctrl-spmi-gpio.ko \
+  pinctrl-spmi-mpp.ko \
+  pm8916_wdt.ko \
+  pm8941-pwrkey.ko \
+  q6adm.ko \
+  q6afe-clocks.ko \
+  q6afe-dai.ko \
+  q6afe.ko \
+  q6asm-dai.ko \
+  q6asm.ko \
+  q6core.ko \
+  q6routing.ko \
+  qcom_aoss.ko \
+  qcom-apcs-ipc-mailbox.ko \
+  qcom_common.ko \
+  qcom-cpufreq-hw.ko \
+  qcom_glink.ko \
+  qcom_glink_rpm.ko \
+  qcom_glink_smem.ko \
+  qcom_hwspinlock.ko \
+  qcom-ipcc.ko \
+  qcom-pdc.ko \
+  qcom_pil_info.ko \
+  qcom_q6v5_adsp.ko \
+  qcom_q6v5.ko \
+  qcom_rpmh.ko \
+  qcom-rpmh-regulator.ko \
+  qcom-scm.ko \
+  qcom_smd.ko \
+  qcom_sysmon.ko \
+  qcom_usb_vbus-regulator.ko \
+  qcom-vadc-common.ko \
+  qcom-wdt.ko \
+  qmi_helpers.ko \
+  qnoc-sdm845.ko \
+  qnoc-sm8250.ko \
+  qrtr.ko \
+  qrtr-mhi.ko \
+  qrtr-smd.ko \
+  qrtr-tun.ko \
+  regmap-sdw.ko \
+  regmap-slimbus.ko \
+  reset-qcom-aoss.ko \
+  reset-qcom-pdc.ko \
+  rmtfs_mem.ko \
+  rpmhpd.ko \
+  rpmsg_ns.ko \
+  rtc-pm8xxx.ko \
+  slim-qcom-ngd-ctrl.ko \
+  slimbus.ko \
+  smem.ko \
+  smp2p.ko \
+  smsm.ko \
+  snd-soc-dmic.ko \
+  snd-soc-hdmi-codec.ko \
+  snd-soc-lpass-va-macro.ko \
+  snd-soc-lpass-wsa-macro.ko \
+  snd-soc-max98927.ko \
+  snd-soc-qcom-common.ko \
+  snd-soc-rl6231.ko \
+  snd-soc-rt5663.ko \
+  snd-soc-sdm845.ko \
+  snd-soc-sm8250.ko \
+  snd-soc-wcd9335.ko \
+  snd-soc-wcd934x.ko \
+  snd-soc-wcd-mbhc.ko \
+  snd-soc-wsa881x.ko \
+  socinfo.ko \
+  soundwire-bus.ko \
+  soundwire-qcom.ko \
+  spmi-pmic-arb.ko \
+  spi-geni-qcom.ko \
+  spi-pl022.ko \
+  spi-qcom-qspi.ko \
+  spi-qup.ko \
+  ulpi.ko \
+  wcd934x.ko
+
+ifeq ($(TARGET_KERNEL_USE),5.15)
+VENDOR_DLKM_KERNEL_MODULES_LIST += \
+  qcom-pmic-typec.ko
+
+VENDOR_RAMDISK_KERNEL_MODULES_LIST += \
+  phy-qcom-qmp.ko \
+  q6dsp-common.ko \
+  ufs_qcom.ko
+endif
+
+ifeq ($(TARGET_KERNEL_USE),6.1)
+VENDOR_DLKM_KERNEL_MODULES_LIST += \
+  leds-qcom-lpg.ko \
+  qcom-pmic-typec.ko \
+  qnoc-sm8450.ko
+
+VENDOR_RAMDISK_KERNEL_MODULES_LIST += \
+  cpr.ko \
+  drm_display_helper.ko \
+  drm_dp_aux_bus.ko \
+  gcc-sm8450.ko \
+  gpi.ko \
+  phy-qcom-qmp-combo.ko \
+  phy-qcom-qmp-pcie.ko \
+  phy-qcom-qmp-pcie-msm8996.ko \
+  phy-qcom-qmp-ufs.ko \
+  phy-qcom-qmp-usb.ko \
+  pinctrl-sm8250-lpass-lpi.ko \
+  pinctrl-sm8450.ko \
+  q6apm-dai.ko \
+  q6apm-lpass-dais.ko \
+  q6prm-clocks.ko \
+  q6prm.ko \
+  snd-q6apm.ko \
+  snd-q6dsp-common.ko \
+  snd-soc-lpass-macro-common.ko \
+  snd-soc-qcom-sdw.ko \
+  spm.ko \
+  ufs_qcom.ko
+endif
+
+ifeq ($(TARGET_KERNEL_USE),6.6)
+VENDOR_DLKM_KERNEL_MODULES_LIST += \
+  leds-qcom-lpg.ko \
+  qcom_pmic_tcpm.ko \
+  qnoc-sm8450.ko
+
+VENDOR_RAMDISK_KERNEL_MODULES_LIST += \
+  cpr.ko \
+  drm_display_helper.ko \
+  drm_dp_aux_bus.ko \
+  gcc-sm8450.ko \
+  gpi.ko \
+  phy-qcom-qmp-combo.ko \
+  phy-qcom-qmp-pcie.ko \
+  phy-qcom-qmp-pcie-msm8996.ko \
+  phy-qcom-qmp-ufs.ko \
+  phy-qcom-qmp-usb.ko \
+  pinctrl-sm8250-lpass-lpi.ko \
+  pinctrl-sm8450.ko \
+  q6apm-dai.ko \
+  q6apm-lpass-dais.ko \
+  q6prm-clocks.ko \
+  q6prm.ko \
+  qcom_ice.ko \
+  snd-q6apm.ko \
+  snd-q6dsp-common.ko \
+  snd-soc-lpass-macro-common.ko \
+  snd-soc-qcom-sdw.ko \
+  snd-soc-wcd-classh.ko \
+  spm.ko \
+  ufs-qcom.ko
+endif