Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 3 | * Copyright (C) 2018 The LineageOS Project |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * * Neither the name of The Linux Foundation nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 28 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Craig Tatlor | adf8a1c | 2018-05-21 17:22:31 +0100 | [diff] [blame] | 30 | |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 31 | #define LOG_NIDEBUG 0 |
| 32 | |
| 33 | #include <errno.h> |
Craig Tatlor | adf8a1c | 2018-05-21 17:22:31 +0100 | [diff] [blame] | 34 | #include <time.h> |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <dlfcn.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #define LOG_TAG "QCOM PowerHAL" |
Ethan Chen | a42f4a8 | 2018-03-01 21:31:15 -0800 | [diff] [blame] | 43 | #include <log/log.h> |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 44 | #include <hardware/hardware.h> |
| 45 | #include <hardware/power.h> |
| 46 | |
| 47 | #include "utils.h" |
| 48 | #include "metadata-defs.h" |
| 49 | #include "hint-data.h" |
| 50 | #include "performance.h" |
| 51 | #include "power-common.h" |
| 52 | |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 53 | static int current_power_profile = PROFILE_BALANCED; |
| 54 | |
| 55 | static int profile_high_performance[] = { |
| 56 | CPUS_ONLINE_MIN_2, |
| 57 | CPU0_MIN_FREQ_TURBO_MAX, |
| 58 | CPU1_MIN_FREQ_TURBO_MAX |
| 59 | }; |
| 60 | |
| 61 | static int profile_power_save[] = { |
| 62 | CPUS_ONLINE_MAX_LIMIT_2, |
| 63 | CPU0_MAX_FREQ_NONTURBO_MAX, |
| 64 | CPU1_MAX_FREQ_NONTURBO_MAX |
| 65 | }; |
| 66 | |
| 67 | #ifdef INTERACTION_BOOST |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 68 | int get_number_of_profiles() |
| 69 | { |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 70 | return 3; |
| 71 | } |
| 72 | #endif |
| 73 | |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 74 | static int set_power_profile(void *data) |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 75 | { |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 76 | int profile = data ? *((int*)data) : 0; |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 77 | int ret = -EINVAL; |
| 78 | const char *profile_name = NULL; |
| 79 | |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 80 | if (profile == current_power_profile) |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 81 | return 0; |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 82 | |
| 83 | ALOGV("%s: Profile=%d", __func__, profile); |
| 84 | |
| 85 | if (current_power_profile != PROFILE_BALANCED) { |
| 86 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 87 | ALOGV("%s: Hint undone", __func__); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 88 | current_power_profile = PROFILE_BALANCED; |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (profile == PROFILE_POWER_SAVE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 92 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save, |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 93 | ARRAY_SIZE(profile_power_save)); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 94 | profile_name = "powersave"; |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 95 | |
| 96 | } else if (profile == PROFILE_HIGH_PERFORMANCE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 97 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
| 98 | profile_high_performance, ARRAY_SIZE(profile_high_performance)); |
| 99 | profile_name = "performance"; |
| 100 | |
| 101 | } else if (profile == PROFILE_BALANCED) { |
| 102 | ret = 0; |
| 103 | profile_name = "balanced"; |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 104 | } |
| 105 | |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 106 | if (ret == 0) { |
| 107 | current_power_profile = profile; |
| 108 | ALOGD("%s: Set %s mode", __func__, profile_name); |
| 109 | } |
| 110 | return ret; |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int resources_interaction_boost[] = { |
| 114 | CPUS_ONLINE_MIN_2, |
| 115 | 0x20B, |
| 116 | 0x30B |
| 117 | }; |
| 118 | |
Rashed Abdel-Tawab | a2a7071 | 2017-12-31 01:21:40 +0200 | [diff] [blame] | 119 | int power_hint_override(power_hint_t hint, void *data) |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 120 | { |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 121 | static struct timespec s_previous_boost_timespec; |
| 122 | struct timespec cur_boost_timespec; |
| 123 | long long elapsed_time; |
| 124 | static int s_previous_duration = 0; |
| 125 | int duration; |
| 126 | |
| 127 | if (hint == POWER_HINT_SET_PROFILE) { |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 128 | if (set_power_profile(data) < 0) |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 129 | ALOGE("Setting power profile failed. mpdecision not started?"); |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 130 | return HINT_HANDLED; |
| 131 | } |
| 132 | |
| 133 | // Skip other hints in high/low power modes |
| 134 | if (current_power_profile == PROFILE_POWER_SAVE || |
| 135 | current_power_profile == PROFILE_HIGH_PERFORMANCE) { |
| 136 | return HINT_HANDLED; |
| 137 | } |
| 138 | |
| 139 | switch (hint) { |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 140 | case POWER_HINT_INTERACTION: |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 141 | duration = 500; // 500ms by default |
| 142 | if (data) { |
| 143 | int input_duration = *((int*)data); |
| 144 | if (input_duration > duration) { |
| 145 | duration = (input_duration > 5000) ? 5000 : input_duration; |
| 146 | } |
| 147 | } |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 148 | |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 149 | clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec); |
| 150 | |
| 151 | elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec); |
| 152 | // don't hint if previous hint's duration covers this hint's duration |
| 153 | if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) { |
| 154 | return HINT_HANDLED; |
| 155 | } |
| 156 | s_previous_boost_timespec = cur_boost_timespec; |
| 157 | s_previous_duration = duration; |
| 158 | |
| 159 | interaction(duration, ARRAY_SIZE(resources_interaction_boost), |
| 160 | resources_interaction_boost); |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 161 | return HINT_HANDLED; |
Michael Bestas | ce25d84 | 2018-03-26 02:13:51 +0300 | [diff] [blame] | 162 | default: |
| 163 | break; |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 164 | } |
| 165 | return HINT_NONE; |
| 166 | } |