# Copyright (c) 2016, Code Aurora Forum. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Code Aurora nor
#       the names of its contributors may be used to endorse or promote
#       products derived from this software without specific prior written
#       permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

on init
    # Disable UFS powersaving
    write /sys/devices/platform/soc/${ro.boot.bootdevice}/clkgate_enable 0
    write /sys/devices/platform/soc/${ro.boot.bootdevice}/hibern8_on_idle_enable 0

    # Disable powersaving
    write /sys/module/lpm_levels/parameters/sleep_disabled 1

    # Bring all cores online
    write /sys/devices/system/cpu/cpu0/online 1
    write /sys/devices/system/cpu/cpu1/online 1
    write /sys/devices/system/cpu/cpu2/online 1
    write /sys/devices/system/cpu/cpu3/online 1
    write /sys/devices/system/cpu/cpu4/online 1
    write /sys/devices/system/cpu/cpu5/online 1
    write /sys/devices/system/cpu/cpu6/online 1
    write /sys/devices/system/cpu/cpu7/online 1

    # Configure governor settings for little cluster
    write /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor "schedutil"
    write /sys/devices/system/cpu/cpu0/cpufreq/schedutil/up_rate_limit_us 500
    write /sys/devices/system/cpu/cpu0/cpufreq/schedutil/down_rate_limit_us 20000

    # Configure governor settings for big cluster
    write /sys/devices/system/cpu/cpu6/cpufreq/scaling_governor "schedutil"
    write /sys/devices/system/cpu/cpu6/cpufreq/schedutil/up_rate_limit_us 500
    write /sys/devices/system/cpu/cpu6/cpufreq/schedutil/down_rate_limit_us 20000

on boot
    # For cpusets initialize for Silver only first and then Silver + Gold
    # Silver only configuration cannot work with 0-7
    write /dev/cpuset/top-app/cpus 0-3
    write /dev/cpuset/audio-app/cpus 1-2
    write /dev/cpuset/foreground/cpus 0-3
    write /dev/cpuset/foreground/boost/cpus 0-3
    write /dev/cpuset/background/cpus 0-3
    write /dev/cpuset/system-background/cpus 0-3
    write /dev/cpuset/top-app/cpus 0-7
    write /dev/cpuset/foreground/cpus 0-7
    write /dev/cpuset/foreground/boost/cpus 0-7
    write /dev/cpuset/background/cpus 0-7
    write /dev/cpuset/system-background/cpus 0-7

    # Disable console_suspend to get better logging for kernel crashes
    write /sys/module/printk/parameters/console_suspend N

on property:init.svc.vendor.charger=running
    setprop persist.sys.usb.config mass_storage

    # Keep one LITTLE and one big
    write /sys/devices/system/cpu/cpu1/online 0
    write /sys/devices/system/cpu/cpu2/online 0
    write /sys/devices/system/cpu/cpu3/online 0
    write /sys/devices/system/cpu/cpu5/online 0
    write /sys/devices/system/cpu/cpu6/online 0
    write /sys/devices/system/cpu/cpu7/online 0
    wait /dev/block/bootdevice

    # Enable UFS powersaving
    write /sys/devices/platform/soc/${ro.boot.bootdevice}/clkgate_enable 1
    write /sys/devices/platform/soc/${ro.boot.bootdevice}/hibern8_on_idle_enable 1

    # Turn on sleep modes
    write /sys/module/lpm_levels/parameters/sleep_disabled 0

    start vendor.power_off_alarm
    mount tmpfs tmpfs /data
    setprop sys.usb.controller 4e00000.dwc3
    setprop sys.usb.configfs 1

on enable-low-power
    # Set the default IRQ affinity to the silver cluster.
    # When a CPU is isolated/hotplugged, the IRQ affinity is adjusted
    # to one of the CPU from the default IRQ affinity mask.
    write /proc/irq/default_smp_affinity "f"

    # Setting b.L scheduler parameters
    write /proc/sys/kernel/sched_upmigrate 95
    write /proc/sys/kernel/sched_downmigrate 85
    write /proc/sys/kernel/sched_group_upmigrate 100
    write /proc/sys/kernel/sched_group_downmigrate 95
    write /proc/sys/kernel/sched_walt_rotate_big_tasks 1

    # Set cpuset parameters
    write /dev/cpuset/background/cpus 0-1
    write /dev/cpuset/system-background/cpus 0-3
    write /dev/cpuset/foreground/boost/cpus 0-3,6-7
    write /dev/cpuset/foreground/cpus 0-3,6-7
    write /dev/cpuset/top-app/cpus 0-7
    write /dev/cpuset/restricted/cpus 0-3

    # Setup final cpu.uclamp
    write /dev/cpuctl/background/cpu.uclamp.max 50
    write /dev/cpuctl/system-background/cpu.uclamp.max 50
    write /dev/cpuctl/dex2oat/cpu.uclamp.max 60

    # Turn on sleep modes
    write /sys/module/lpm_levels/parameters/sleep_disabled 0

    # Enable UFS powersaving
    write /sys/devices/platform/soc/${ro.boot.bootdevice}/clkgate_enable 1
    write /sys/devices/platform/soc/${ro.boot.bootdevice}/hibern8_on_idle_enable 1

    write /proc/sys/vm/swappiness 100
    write /proc/sys/vm/watermark_scale_factor 1

on property:sys.boot_completed=1
    trigger enable-low-power

# If UART is on, enable console_suspend on boot_complete to save power
on property:sys.boot_completed=1 && property:init.svc.console=running
    write /sys/module/printk/parameters/console_suspend Y

on property:init.svc.recovery=running
    trigger enable-low-power
