Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
| 3 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 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 | |
| 22 | #include "nss_tx_rx_common.h" |
| 23 | |
| 24 | #define NSS_ACK_STARTED 0 |
| 25 | #define NSS_ACK_FINISHED 1 |
| 26 | |
| 27 | extern struct nss_cmd_buffer nss_cmd_buf; |
| 28 | extern struct nss_frequency_statistics nss_freq_stat; |
| 29 | extern struct nss_runtime_sampling nss_runtime_samples; |
| 30 | extern struct workqueue_struct *nss_wq; |
| 31 | extern nss_work_t *nss_work; |
| 32 | extern void *nss_freq_change_context; |
| 33 | |
| 34 | /* |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame^] | 35 | * nss_freq_msg_init() |
| 36 | * Initialize the freq message |
| 37 | */ |
| 38 | static void nss_freq_msg_init(struct nss_corefreq_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, |
| 39 | void *cb, void *app_data) |
| 40 | { |
| 41 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 42 | } |
| 43 | |
| 44 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 45 | * nss_freq_handle_ack() |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 46 | * Handle the nss ack of frequency change. |
| 47 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 48 | 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] | 49 | { |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 50 | if (nfa->ack == NSS_ACK_STARTED) { |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 51 | /* |
| 52 | * NSS finished start noficiation - HW change clocks and send end notification |
| 53 | */ |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 54 | 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] | 55 | |
| 56 | return; |
| 57 | } |
| 58 | |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 59 | if (nfa->ack == NSS_ACK_FINISHED) { |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 60 | /* |
| 61 | * NSS finished end notification - Done |
| 62 | */ |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 63 | 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] | 64 | nss_runtime_samples.freq_scale_ready = 1; |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | nss_info("%p: NSS had an error - Running: %dmhz\n", nss_ctx, nfa->freq_current); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * nss_freq_change() |
| 73 | * NSS frequency change API. |
| 74 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 75 | 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) |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 76 | { |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 77 | struct sk_buff *nbuf; |
| 78 | int32_t status; |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 79 | struct nss_corefreq_msg *ncm; |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 80 | struct nss_freq_msg *nfc; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 81 | |
| 82 | nss_info("%p: Frequency Changing to: %d\n", nss_ctx, eng); |
| 83 | |
| 84 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 85 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 86 | return NSS_TX_FAILURE_NOT_READY; |
| 87 | } |
| 88 | |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 89 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 90 | if (unlikely(!nbuf)) { |
| 91 | spin_lock_bh(&nss_ctx->nss_top->stats_lock); |
| 92 | nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++; |
| 93 | spin_unlock_bh(&nss_ctx->nss_top->stats_lock); |
| 94 | return NSS_TX_FAILURE; |
| 95 | } |
| 96 | |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 97 | ncm = (struct nss_corefreq_msg *)skb_put(nbuf, sizeof(struct nss_corefreq_msg)); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 98 | |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame^] | 99 | nss_freq_msg_init(ncm, NSS_COREFREQ_INTERFACE, NSS_TX_METADATA_TYPE_NSS_FREQ_CHANGE, |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 100 | nbuf->len, NULL, NULL); |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 101 | nfc = &ncm->msg.nfc; |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 102 | nfc->frequency = eng; |
| 103 | nfc->start_or_end = start_or_end; |
| 104 | nfc->stats_enable = stats_enable; |
| 105 | |
| 106 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 107 | if (status != NSS_CORE_STATUS_SUCCESS) { |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 108 | dev_kfree_skb_any(nbuf); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 109 | nss_info("%p: unable to enqueue 'nss frequency change' - marked as stopped\n", nss_ctx); |
| 110 | return NSS_TX_FAILURE; |
| 111 | } |
| 112 | |
| 113 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit, NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 114 | |
| 115 | return NSS_TX_SUCCESS; |
| 116 | } |
| 117 | |
| 118 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 119 | * nss_freq_queue_work() |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 120 | * Queue Work to the NSS Workqueue based on Current index. |
| 121 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 122 | static void nss_freq_queue_work(void) |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 123 | { |
| 124 | BUG_ON(!nss_wq); |
| 125 | |
| 126 | nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency; |
| 127 | |
Thomas Wu | 7409bce | 2014-05-21 10:56:07 -0700 | [diff] [blame] | 128 | nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_ATOMIC); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 129 | if (!nss_work) { |
| 130 | nss_info("NSS FREQ WQ kmalloc fail"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | INIT_WORK((struct work_struct *)nss_work, nss_wq_function); |
| 135 | nss_work->frequency = nss_cmd_buf.current_freq; |
| 136 | nss_work->stats_enable = 1; |
| 137 | queue_work(nss_wq, (struct work_struct *)nss_work); |
| 138 | } |
| 139 | |
| 140 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 141 | * nss_freq_handle_core_stats() |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 142 | * Handle the core stats |
| 143 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 144 | static void nss_freq_handle_core_stats(struct nss_ctx_instance *nss_ctx, struct nss_core_stats *core_stats) |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 145 | { |
| 146 | uint32_t b_index; |
| 147 | uint32_t minimum; |
| 148 | uint32_t maximum; |
| 149 | uint32_t sample; |
| 150 | |
| 151 | sample = core_stats->inst_cnt_total; |
| 152 | |
| 153 | /* |
| 154 | * We do not accept any statistics if auto scaling is off, |
| 155 | * we start with a fresh sample set when scaling is |
| 156 | * eventually turned on. |
| 157 | */ |
| 158 | if (!nss_cmd_buf.auto_scale && nss_runtime_samples.initialized) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Delete Current Index Value, Add New Value, Recalculate new Sum, Shift Index |
| 164 | */ |
| 165 | b_index = nss_runtime_samples.buffer_index; |
| 166 | |
| 167 | nss_runtime_samples.sum = nss_runtime_samples.sum - nss_runtime_samples.buffer[b_index]; |
| 168 | nss_runtime_samples.buffer[b_index] = sample; |
| 169 | nss_runtime_samples.sum = nss_runtime_samples.sum + nss_runtime_samples.buffer[b_index]; |
| 170 | nss_runtime_samples.buffer_index = (b_index + 1) & NSS_SAMPLE_BUFFER_MASK; |
| 171 | |
| 172 | if (nss_runtime_samples.sample_count < NSS_SAMPLE_BUFFER_SIZE) { |
| 173 | nss_runtime_samples.sample_count++; |
| 174 | |
| 175 | /* |
| 176 | * Samples Are All Ready, Start Auto Scale |
| 177 | */ |
| 178 | if (nss_runtime_samples.sample_count == NSS_SAMPLE_BUFFER_SIZE ) { |
| 179 | nss_cmd_buf.auto_scale = 1; |
| 180 | nss_runtime_samples.freq_scale_ready = 1; |
| 181 | nss_runtime_samples.initialized = 1; |
| 182 | } |
| 183 | |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | nss_runtime_samples.average = nss_runtime_samples.sum / nss_runtime_samples.sample_count; |
| 188 | |
| 189 | /* |
| 190 | * Print out statistics every 10 seconds |
| 191 | */ |
| 192 | if (nss_runtime_samples.message_rate_limit == NSS_MESSAGE_RATE_LIMIT) { |
Sakthi Vignesh Radhakrishnan | aa37810 | 2014-04-07 13:52:28 -0700 | [diff] [blame] | 193 | nss_trace("%p: Running AVG:%x Sample:%x Divider:%d\n", nss_ctx, nss_runtime_samples.average, core_stats->inst_cnt_total, nss_runtime_samples.sample_count); |
| 194 | nss_trace("%p: Current Frequency Index:%d\n", nss_ctx, nss_runtime_samples.freq_scale_index); |
| 195 | nss_trace("%p: Auto Scale:%d Auto Scale Ready:%d\n", nss_ctx, nss_runtime_samples.freq_scale_ready, nss_cmd_buf.auto_scale); |
| 196 | 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] | 197 | |
| 198 | nss_runtime_samples.message_rate_limit = 0; |
| 199 | } else { |
| 200 | nss_runtime_samples.message_rate_limit++; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Scale Algorithmn UP and DOWN |
| 205 | */ |
| 206 | if ((nss_runtime_samples.freq_scale_ready == 1) && (nss_cmd_buf.auto_scale == 1)) { |
| 207 | if (nss_runtime_samples.freq_scale_rate_limit_up == NSS_FREQUENCY_SCALE_RATE_LIMIT_UP) { |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 208 | // nss_info("%p: Preparing Switch Inst_Cnt Avg:%x\n", nss_ctx, nss_runtime_samples.average); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 209 | |
| 210 | maximum = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].maximum; |
| 211 | |
| 212 | if ((sample > maximum) && (nss_runtime_samples.freq_scale_index < (nss_runtime_samples.freq_scale_sup_max - 1))) { |
| 213 | nss_runtime_samples.freq_scale_index++; |
| 214 | nss_runtime_samples.freq_scale_ready = 0; |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 215 | nss_freq_queue_work(); |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 216 | // nss_info("%p: Switch Up with Sample %x \n", nss_ctx, sample); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 217 | } else { |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 218 | // nss_info("%p: No Change at Max\n", nss_ctx); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 219 | } |
| 220 | nss_runtime_samples.freq_scale_rate_limit_up = 0; |
| 221 | return; |
| 222 | |
| 223 | } else { |
| 224 | nss_runtime_samples.freq_scale_rate_limit_up++; |
| 225 | } |
| 226 | |
| 227 | minimum = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].minimum; |
| 228 | |
| 229 | if ((nss_runtime_samples.average < minimum) && (nss_runtime_samples.freq_scale_index > 0)) { |
| 230 | nss_runtime_samples.freq_scale_rate_limit_down++; |
| 231 | |
| 232 | if (nss_runtime_samples.freq_scale_rate_limit_down == NSS_FREQUENCY_SCALE_RATE_LIMIT_DOWN) { |
| 233 | nss_runtime_samples.freq_scale_index--; |
| 234 | nss_runtime_samples.freq_scale_ready = 0; |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 235 | nss_freq_queue_work(); |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 236 | nss_runtime_samples.freq_scale_rate_limit_down = 0; |
| 237 | } |
| 238 | } else { |
| 239 | nss_runtime_samples.freq_scale_rate_limit_down = 0; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 245 | * nss_freq_interface_handler() |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 246 | * Handle NSS -> HLOS messages for Frequency Changes and Statistics |
| 247 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 248 | 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] | 249 | |
| 250 | struct nss_corefreq_msg *ncfm = (struct nss_corefreq_msg *)ncm; |
| 251 | |
Thomas Wu | 168ca26 | 2014-03-21 16:20:27 -0700 | [diff] [blame] | 252 | switch (ncfm->cm.type) { |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 253 | case COREFREQ_METADATA_TYPE_TX_FREQ_ACK: |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 254 | nss_freq_handle_ack(nss_ctx, &ncfm->msg.nfc); |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 255 | break; |
| 256 | case COREFREQ_METADATA_TYPE_TX_CORE_STATS: |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 257 | nss_freq_handle_core_stats(nss_ctx, &ncfm->msg.ncs); |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 258 | break; |
| 259 | |
| 260 | default: |
| 261 | if (ncm->response != NSS_CMN_RESPONSE_ACK) { |
| 262 | /* |
| 263 | * Check response |
| 264 | */ |
Thomas Wu | 6825035 | 2014-04-02 18:59:40 -0700 | [diff] [blame] | 265 | 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] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 271 | * nss_freq_register_handler() |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 272 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 273 | void nss_freq_register_handler(void) |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 274 | { |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 275 | nss_core_register_handler(NSS_COREFREQ_INTERFACE, nss_freq_interface_handler, NULL); |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 279 | * nss_freq_get_mgr() |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 280 | */ |
Sundarajan Srinivasan | dedd8e4 | 2014-10-06 11:59:34 -0700 | [diff] [blame] | 281 | void *nss_freq_get_mgr(void) |
Abhishek Rastogi | 9da4747 | 2014-03-18 19:46:15 +0530 | [diff] [blame] | 282 | { |
| 283 | return (void *)&nss_top_main.nss[nss_top_main.frequency_handler_id]; |
Thomas Wu | c07d870 | 2014-03-19 15:46:19 -0700 | [diff] [blame] | 284 | } |