Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Cemil Coskun | ff83d56 | 2019-07-30 11:05:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013, 2015-2019 The Linux Foundation. All rights reserved. |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 4 | * Permission to use, copy, modify, and/or distribute this software for |
| 5 | * any purpose with or without fee is hereby granted, provided that the |
| 6 | * above copyright notice and this permission notice appear in all copies. |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 13 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | ************************************************************************** |
| 15 | */ |
| 16 | |
| 17 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 18 | * nss_freq.c |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 19 | * NSS frequency change APIs |
| 20 | */ |
| 21 | |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 22 | #include "nss_stats.h" |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 23 | #include "nss_tx_rx_common.h" |
Sachin Shashidhar | 6dd9cfc | 2018-07-31 14:44:37 -0700 | [diff] [blame] | 24 | #include "nss_freq_log.h" |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 25 | #include "nss_freq_stats.h" |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 26 | |
| 27 | #define NSS_ACK_STARTED 0 |
| 28 | #define NSS_ACK_FINISHED 1 |
| 29 | |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 30 | #define NSS_FREQ_USG_AVG_FREQUENCY 1000 /* Time in ms over which CPU Usage is averaged */ |
| 31 | #define NSS_FREQ_CPU_USAGE_MAX_BOUND 75 /* MAX CPU usage equivalent to running max instructions excluding all the hazards */ |
| 32 | #define NSS_FREQ_CPU_USAGE_MAX 100 /* MAX CPU usage equivalent to running max instructions including all the hazards. |
| 33 | This is also the ideal maximum usage value. */ |
| 34 | |
| 35 | /* |
| 36 | * Spinlock to protect the global data structure nss_freq_cpu_status |
| 37 | */ |
| 38 | DEFINE_SPINLOCK(nss_freq_cpu_usage_lock); |
| 39 | |
| 40 | /* |
| 41 | * At any point, this object has the latest data about CPU utilization. |
| 42 | */ |
| 43 | struct nss_freq_cpu_usage nss_freq_cpu_status; |
| 44 | |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 45 | extern struct nss_runtime_sampling nss_runtime_samples; |
| 46 | extern struct workqueue_struct *nss_wq; |
| 47 | extern nss_work_t *nss_work; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 48 | |
| 49 | /* |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 50 | * nss_freq_msg_init() |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 51 | * Initialize the freq message |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 52 | */ |
| 53 | static void nss_freq_msg_init(struct nss_corefreq_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, |
| 54 | void *cb, void *app_data) |
| 55 | { |
| 56 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 57 | } |
| 58 | |
| 59 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 60 | * nss_freq_handle_ack() |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 61 | * Handle the nss ack of frequency change. |
| 62 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 63 | static void nss_freq_handle_ack(struct nss_ctx_instance *nss_ctx, struct nss_freq_msg *nfa) |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 64 | { |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 65 | if (nfa->ack == NSS_ACK_STARTED) { |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 66 | /* |
| 67 | * NSS finished start noficiation - HW change clocks and send end notification |
| 68 | */ |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 69 | nss_info("%p: NSS ACK Received: %d - Change HW CLK/Send Finish to NSS\n", nss_ctx, nfa->ack); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 70 | |
| 71 | return; |
| 72 | } |
| 73 | |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 74 | if (nfa->ack == NSS_ACK_FINISHED) { |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 75 | /* |
| 76 | * NSS finished end notification - Done |
| 77 | */ |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 78 | nss_info("%p: NSS ACK Received: %d - End Notification ACK - Running: %dmhz\n", nss_ctx, nfa->ack, nfa->freq_current); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 79 | nss_runtime_samples.freq_scale_ready = 1; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | nss_info("%p: NSS had an error - Running: %dmhz\n", nss_ctx, nfa->freq_current); |
| 84 | } |
| 85 | |
| 86 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 87 | * nss_freq_queue_work() |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 88 | * Queue Work to the NSS Workqueue based on Current index. |
| 89 | */ |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 90 | static bool nss_freq_queue_work(void) |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 91 | { |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 92 | nss_freq_scales_t index = nss_runtime_samples.freq_scale_index; |
| 93 | |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 94 | BUG_ON(!nss_wq); |
| 95 | |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 96 | nss_info("frequency:%d index:%d sample count:%x\n", nss_runtime_samples.freq_scale[index].frequency, |
| 97 | index, nss_runtime_samples.average); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 98 | |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 99 | /* |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 100 | * schedule freq change with autoscale ON |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 101 | */ |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 102 | return nss_freq_sched_change(index, true); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 106 | * nss_freq_get_cpu_usage() |
| 107 | * Returns the CPU usage value in percentage at any instance for a required core. Returns -1 in case of an error. |
| 108 | * |
| 109 | * Calculation frequency is 1 second. Range of usage is 0-100. This API returns -1 if CPU usage is requested for core 1. |
| 110 | * TODO: Extend this API to get CPU usage for core 1. |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 111 | */ |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 112 | int8_t nss_freq_get_cpu_usage(uint32_t core_id) |
| 113 | { |
| 114 | int8_t usage; |
| 115 | |
| 116 | if (core_id == 0) { |
| 117 | spin_lock_bh(&nss_freq_cpu_usage_lock); |
| 118 | usage = nss_freq_cpu_status.used; |
| 119 | spin_unlock_bh(&nss_freq_cpu_usage_lock); |
| 120 | |
| 121 | return usage; |
| 122 | } |
| 123 | |
| 124 | nss_warning("CPU usage functionality is not supported for core %u\n", core_id); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * nss_freq_compute_cpu_usage() |
| 130 | * Computes the CPU utilization and maximum-minumun cpu utilization since boot. |
| 131 | */ |
| 132 | static void nss_freq_compute_cpu_usage(struct nss_ctx_instance *nss_ctx, uint32_t inst_cnt) |
| 133 | { |
| 134 | uint32_t estimated_ins_capacity; |
| 135 | uint8_t actual_usage; |
| 136 | uint8_t usage; |
| 137 | |
| 138 | spin_lock_bh(&nss_freq_cpu_usage_lock); |
| 139 | |
| 140 | /* |
| 141 | * If actual CPU usage turns up higher than 100, there is something wrong with the received data. |
| 142 | * Upper bound average varies between 80% usage to 100% usage. |
| 143 | * |
| 144 | * TODO: To improve estimation algorithm for calculating how many actual instructions are executed. |
| 145 | */ |
| 146 | actual_usage = (inst_cnt * 100) / nss_freq_cpu_status.max_ins; |
| 147 | if ((actual_usage > NSS_FREQ_CPU_USAGE_MAX) || (actual_usage == 0)) { |
| 148 | spin_unlock_bh(&nss_freq_cpu_usage_lock); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Simpler version of below math: This is calculating the reduced number of maximum instructions |
| 154 | * estimated_ins_capacity = nss_freq_cpu_status.avg_up% of nss_freq_cpu_status.max_ins |
| 155 | * Calculating usage percentage: usage = (inst_cnt/estimated_ins_capacity) * 100 |
| 156 | */ |
| 157 | estimated_ins_capacity = ((NSS_FREQ_CPU_USAGE_MAX_BOUND * nss_freq_cpu_status.max_ins) / 100); |
| 158 | if (estimated_ins_capacity == 0) { |
| 159 | spin_unlock_bh(&nss_freq_cpu_usage_lock); |
| 160 | return; |
| 161 | } |
| 162 | usage = (inst_cnt * 100) / estimated_ins_capacity; |
| 163 | |
| 164 | /* |
| 165 | * Average the instructions over NSS_FREQ_USG_AVG_FREQUENCY ms |
| 166 | */ |
| 167 | if (nss_freq_cpu_status.avg_ctr == NSS_FREQ_USG_AVG_FREQUENCY) { |
| 168 | nss_freq_cpu_status.used = nss_freq_cpu_status.total / NSS_FREQ_USG_AVG_FREQUENCY; |
| 169 | |
| 170 | /* |
| 171 | * Due to our estimation, this could go beyond the end limit of 100% |
| 172 | */ |
| 173 | if (nss_freq_cpu_status.used > NSS_FREQ_CPU_USAGE_MAX) { |
| 174 | nss_freq_cpu_status.used = NSS_FREQ_CPU_USAGE_MAX; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Getting the all time max and min usage |
| 179 | */ |
| 180 | if (nss_freq_cpu_status.used > nss_freq_cpu_status.max) { |
| 181 | nss_freq_cpu_status.max = nss_freq_cpu_status.used; |
| 182 | } |
| 183 | |
| 184 | if (nss_freq_cpu_status.used < nss_freq_cpu_status.min) { |
| 185 | nss_freq_cpu_status.min = nss_freq_cpu_status.used; |
| 186 | } |
| 187 | |
| 188 | nss_trace("%p: max_instructions:%d cpu_usage:%d max_usage:%d min_usage:%d\n", nss_ctx, |
| 189 | nss_freq_cpu_status.max_ins, nss_freq_cpu_status.used, nss_freq_cpu_status.max, nss_freq_cpu_status.min); |
| 190 | |
| 191 | nss_freq_cpu_status.total = 0; |
| 192 | nss_freq_cpu_status.avg_ctr = 0; |
| 193 | } |
| 194 | |
| 195 | nss_freq_cpu_status.total += usage; |
| 196 | nss_freq_cpu_status.avg_ctr++; |
| 197 | |
| 198 | spin_unlock_bh(&nss_freq_cpu_usage_lock); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * nss_freq_scale_frequency() |
| 203 | * Frequency scaling algorithm to scale frequency. |
| 204 | */ |
| 205 | void nss_freq_scale_frequency(struct nss_ctx_instance *nss_ctx, uint32_t inst_cnt) |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 206 | { |
| 207 | uint32_t b_index; |
| 208 | uint32_t minimum; |
| 209 | uint32_t maximum; |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 210 | uint32_t index = nss_runtime_samples.freq_scale_index; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * We do not accept any statistics if auto scaling is off, |
| 214 | * we start with a fresh sample set when scaling is |
| 215 | * eventually turned on. |
| 216 | */ |
| 217 | if (!nss_cmd_buf.auto_scale && nss_runtime_samples.initialized) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Delete Current Index Value, Add New Value, Recalculate new Sum, Shift Index |
| 223 | */ |
| 224 | b_index = nss_runtime_samples.buffer_index; |
| 225 | |
| 226 | nss_runtime_samples.sum = nss_runtime_samples.sum - nss_runtime_samples.buffer[b_index]; |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 227 | nss_runtime_samples.buffer[b_index] = inst_cnt; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 228 | nss_runtime_samples.sum = nss_runtime_samples.sum + nss_runtime_samples.buffer[b_index]; |
| 229 | nss_runtime_samples.buffer_index = (b_index + 1) & NSS_SAMPLE_BUFFER_MASK; |
| 230 | |
| 231 | if (nss_runtime_samples.sample_count < NSS_SAMPLE_BUFFER_SIZE) { |
| 232 | nss_runtime_samples.sample_count++; |
| 233 | |
| 234 | /* |
| 235 | * Samples Are All Ready, Start Auto Scale |
| 236 | */ |
| 237 | if (nss_runtime_samples.sample_count == NSS_SAMPLE_BUFFER_SIZE ) { |
| 238 | nss_cmd_buf.auto_scale = 1; |
| 239 | nss_runtime_samples.freq_scale_ready = 1; |
| 240 | nss_runtime_samples.initialized = 1; |
| 241 | } |
| 242 | |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | nss_runtime_samples.average = nss_runtime_samples.sum / nss_runtime_samples.sample_count; |
| 247 | |
| 248 | /* |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 249 | * Print out statistics every 10 samples |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 250 | */ |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 251 | if (nss_runtime_samples.message_rate_limit++ >= NSS_MESSAGE_RATE_LIMIT) { |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 252 | nss_trace("%p: Running AVG:%x Sample:%x Divider:%d\n", nss_ctx, nss_runtime_samples.average, inst_cnt, nss_runtime_samples.sample_count); |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 253 | nss_trace("%p: Current Frequency Index:%d\n", nss_ctx, index); |
Cemil Coskun | ff83d56 | 2019-07-30 11:05:37 -0700 | [diff] [blame] | 254 | nss_trace("%p: Auto Scale Ready:%d Auto Scale:%d\n", nss_ctx, nss_runtime_samples.freq_scale_ready, nss_cmd_buf.auto_scale); |
Sakthi Vignesh Radhakrishnan | aa37810 | 2014-04-07 13:52:28 -0700 | [diff] [blame] | 255 | nss_trace("%p: Current Rate:%x\n", nss_ctx, nss_runtime_samples.average); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 256 | |
| 257 | nss_runtime_samples.message_rate_limit = 0; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 261 | * Don't scale if we are not ready or auto scale is disabled. |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 262 | */ |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 263 | if ((nss_runtime_samples.freq_scale_ready != 1) || (nss_cmd_buf.auto_scale != 1)) { |
| 264 | return; |
| 265 | } |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 266 | |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 267 | /* |
| 268 | * Scale Algorithmn |
| 269 | * Algorithmn will limit how fast it will transition each scale, by the number of samples seen. |
| 270 | * If any sample is out of scale during the idle count, the rate_limit will reset to 0. |
| 271 | * Scales are limited to the max number of cpu scales we support. |
| 272 | */ |
| 273 | if (nss_runtime_samples.freq_scale_rate_limit_up++ >= NSS_FREQUENCY_SCALE_RATE_LIMIT_UP) { |
| 274 | maximum = nss_runtime_samples.freq_scale[index].maximum; |
Thomas Wu | 1fa2609 | 2016-03-30 14:10:03 -0700 | [diff] [blame] | 275 | if ((nss_runtime_samples.average > maximum) && (index < (NSS_FREQ_MAX_SCALE - 1))) { |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 276 | nss_runtime_samples.freq_scale_index++; |
| 277 | nss_runtime_samples.freq_scale_ready = 0; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 278 | |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 279 | /* |
| 280 | * If fail to increase frequency, decrease index |
| 281 | */ |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 282 | nss_trace("frequency increase to %d inst:%x > maximum:%x\n", nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency, inst_cnt, maximum); |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 283 | if (!nss_freq_queue_work()) { |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 284 | nss_runtime_samples.freq_scale_index--; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 285 | } |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 286 | } |
Thomas Wu | 4640e56 | 2015-06-10 10:23:04 -0700 | [diff] [blame] | 287 | |
| 288 | /* |
Cemil Coskun | ff83d56 | 2019-07-30 11:05:37 -0700 | [diff] [blame] | 289 | * Reset the down scale counter based on running average, so can idle properly |
Thomas Wu | 4640e56 | 2015-06-10 10:23:04 -0700 | [diff] [blame] | 290 | */ |
Thomas Wu | c151f2e | 2015-09-08 10:59:44 -0700 | [diff] [blame] | 291 | if (nss_runtime_samples.average > maximum) { |
Thomas Wu | 4640e56 | 2015-06-10 10:23:04 -0700 | [diff] [blame] | 292 | nss_trace("down scale timeout reset running average:%x\n", nss_runtime_samples.average); |
| 293 | nss_runtime_samples.freq_scale_rate_limit_down = 0; |
| 294 | } |
| 295 | |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 296 | nss_runtime_samples.freq_scale_rate_limit_up = 0; |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | if (nss_runtime_samples.freq_scale_rate_limit_down++ >= NSS_FREQUENCY_SCALE_RATE_LIMIT_DOWN) { |
| 301 | minimum = nss_runtime_samples.freq_scale[index].minimum; |
| 302 | if ((nss_runtime_samples.average < minimum) && (index > 0)) { |
| 303 | nss_runtime_samples.freq_scale_index--; |
| 304 | nss_runtime_samples.freq_scale_ready = 0; |
| 305 | |
| 306 | /* |
| 307 | * If fail to decrease frequency, increase index |
| 308 | */ |
Thomas Wu | c151f2e | 2015-09-08 10:59:44 -0700 | [diff] [blame] | 309 | nss_trace("frequency decrease to %d inst:%x < minumum:%x\n", nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency, nss_runtime_samples.average, minimum); |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 310 | if (!nss_freq_queue_work()) { |
Thomas Wu | 0e2fc4f | 2015-03-04 15:39:14 -0800 | [diff] [blame] | 311 | nss_runtime_samples.freq_scale_index++; |
| 312 | } |
| 313 | } |
| 314 | nss_runtime_samples.freq_scale_rate_limit_down = 0; |
| 315 | return; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | /* |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 320 | * nss_freq_handle_core_stats() |
| 321 | * Handle the core stats. |
| 322 | */ |
| 323 | static void nss_freq_handle_core_stats(struct nss_ctx_instance *nss_ctx, struct nss_core_stats *core_stats) |
| 324 | { |
| 325 | uint32_t inst_cnt = core_stats->inst_cnt_total; |
| 326 | |
| 327 | /* |
| 328 | * compute CPU utilization by using the instruction count |
| 329 | */ |
| 330 | nss_freq_compute_cpu_usage(nss_ctx, inst_cnt); |
| 331 | |
| 332 | /* |
| 333 | * Perform frequency scaling |
| 334 | */ |
| 335 | nss_freq_scale_frequency(nss_ctx, inst_cnt); |
| 336 | } |
| 337 | |
| 338 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 339 | * nss_freq_interface_handler() |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 340 | * Handle NSS -> HLOS messages for Frequency Changes and Statistics. |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 341 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 342 | static void nss_freq_interface_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) { |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 343 | |
| 344 | struct nss_corefreq_msg *ncfm = (struct nss_corefreq_msg *)ncm; |
| 345 | |
Sachin Shashidhar | 6dd9cfc | 2018-07-31 14:44:37 -0700 | [diff] [blame] | 346 | /* |
| 347 | * Trace Messages |
| 348 | */ |
| 349 | nss_freq_log_rx_msg(ncfm); |
| 350 | |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 351 | switch (ncfm->cm.type) { |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 352 | case COREFREQ_METADATA_TYPE_TX_FREQ_ACK: |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 353 | nss_freq_handle_ack(nss_ctx, &ncfm->msg.nfc); |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 354 | break; |
| 355 | case COREFREQ_METADATA_TYPE_TX_CORE_STATS: |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 356 | nss_freq_handle_core_stats(nss_ctx, &ncfm->msg.ncs); |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 357 | break; |
| 358 | |
| 359 | default: |
| 360 | if (ncm->response != NSS_CMN_RESPONSE_ACK) { |
| 361 | /* |
| 362 | * Check response |
| 363 | */ |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 364 | nss_info("%p: Received response %d for type %d, interface %d", nss_ctx, ncm->response, ncm->type, ncm->interface); |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 370 | * nss_freq_change() |
| 371 | * NSS frequency change API. |
| 372 | */ |
| 373 | nss_tx_status_t nss_freq_change(struct nss_ctx_instance *nss_ctx, uint32_t eng, uint32_t stats_enable, uint32_t start_or_end) |
| 374 | { |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 375 | struct nss_corefreq_msg ncm; |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 376 | struct nss_freq_msg *nfc; |
| 377 | |
| 378 | nss_info("%p: frequency changing to: %d\n", nss_ctx, eng); |
| 379 | |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Update the max instruction count for a frequency during down scaling. |
| 382 | * Better to update this as late as possible in the frequency update call. |
| 383 | */ |
| 384 | spin_lock_bh(&nss_freq_cpu_usage_lock); |
| 385 | nss_freq_cpu_status.max_ins = eng / 1000; |
| 386 | spin_unlock_bh(&nss_freq_cpu_usage_lock); |
| 387 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 388 | nss_freq_msg_init(&ncm, NSS_COREFREQ_INTERFACE, NSS_TX_METADATA_TYPE_NSS_FREQ_CHANGE, |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 389 | sizeof(struct nss_freq_msg), NULL, NULL); |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 390 | nfc = &ncm.msg.nfc; |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 391 | nfc->frequency = eng; |
| 392 | nfc->start_or_end = start_or_end; |
| 393 | nfc->stats_enable = stats_enable; |
| 394 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 395 | return nss_core_send_cmd(nss_ctx, &ncm, sizeof(ncm), NSS_NBUF_PAYLOAD_SIZE); |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
| 399 | * nss_freq_sched_change() |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 400 | * Schedule a frequency work. |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 401 | */ |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 402 | bool nss_freq_sched_change(nss_freq_scales_t index, bool auto_scale) |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 403 | { |
| 404 | if (index >= NSS_FREQ_MAX_SCALE) { |
| 405 | nss_info("NSS freq scale beyond limit\n"); |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 406 | return false; |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_ATOMIC); |
| 410 | if (!nss_work) { |
| 411 | nss_info("NSS Freq WQ kmalloc fail"); |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 412 | return false; |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 413 | } |
| 414 | |
Thomas Wu | d6af377 | 2017-09-01 13:42:28 -0700 | [diff] [blame] | 415 | INIT_WORK((struct work_struct *)nss_work, nss_hal_wq_function); |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 416 | |
| 417 | nss_work->frequency = nss_runtime_samples.freq_scale[index].frequency; |
| 418 | |
| 419 | nss_work->stats_enable = auto_scale; |
| 420 | nss_cmd_buf.current_freq = nss_work->frequency; |
| 421 | queue_work(nss_wq, (struct work_struct *)nss_work); |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 422 | |
| 423 | return true; |
Samarjeet Banerjee | b126e0f | 2016-08-05 20:58:27 +0530 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 427 | * nss_freq_get_context() |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 428 | * Get NSS context instance for frequency. |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 429 | */ |
| 430 | struct nss_ctx_instance *nss_freq_get_context(void) |
| 431 | { |
| 432 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.frequency_handler_id]; |
| 433 | } |
| 434 | EXPORT_SYMBOL(nss_freq_get_context); |
| 435 | |
| 436 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 437 | * nss_freq_register_handler() |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 438 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 439 | void nss_freq_register_handler(void) |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 440 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 441 | struct nss_ctx_instance *nss_ctx = nss_freq_get_context(); |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 442 | nss_core_register_handler(nss_ctx, NSS_COREFREQ_INTERFACE, nss_freq_interface_handler, NULL); |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 443 | } |
Gaurao Chaudhari | 7c5d01e | 2019-09-16 10:34:48 -0700 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * nss_freq_cpu_usage_init() |
| 447 | * Initialize cpu usage computing. |
| 448 | * |
| 449 | * TODO: Add support to retrieve CPU usage even if frequency scaling is disabled. |
| 450 | */ |
| 451 | void nss_freq_init_cpu_usage(void) |
| 452 | { |
| 453 | nss_freq_cpu_status.used = 0; |
| 454 | nss_freq_cpu_status.max_ins = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency / 1000; |
| 455 | nss_freq_cpu_status.total = 0; |
| 456 | nss_freq_cpu_status.max = 0; /* Initial value is 0 to capture the highest most value during the run */ |
| 457 | nss_freq_cpu_status.min = NSS_FREQ_CPU_USAGE_MAX; /* Initial value is 100 to capture the lowest most value during the run */ |
| 458 | nss_freq_cpu_status.avg_up = NSS_FREQ_CPU_USAGE_MAX_BOUND; |
| 459 | nss_freq_cpu_status.avg_ctr = 0; |
| 460 | |
| 461 | nss_freq_stats_dentry_create(); |
| 462 | } |