blob: b23f93796d0e1a41b39ae43bd89b88321c7b9068 [file] [log] [blame]
Abhishek Rastogi9da47472014-03-18 19:46:15 +05301/*
2 **************************************************************************
Thomas Wu0e2fc4f2015-03-04 15:39:14 -08003 * Copyright (c) 2013, 2015 The Linux Foundation. All rights reserved.
Abhishek Rastogi9da47472014-03-18 19:46:15 +05304 * 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 Srinivasandedd8e42014-10-06 11:59:34 -070018 * nss_freq.c
Abhishek Rastogi9da47472014-03-18 19:46:15 +053019 * 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
27extern struct nss_cmd_buffer nss_cmd_buf;
28extern struct nss_frequency_statistics nss_freq_stat;
29extern struct nss_runtime_sampling nss_runtime_samples;
30extern struct workqueue_struct *nss_wq;
31extern nss_work_t *nss_work;
Abhishek Rastogi9da47472014-03-18 19:46:15 +053032
33/*
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -070034 * nss_freq_msg_init()
Thomas Wu0e2fc4f2015-03-04 15:39:14 -080035 * Initialize the freq message
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -070036 */
37static void nss_freq_msg_init(struct nss_corefreq_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len,
38 void *cb, void *app_data)
39{
40 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
41}
42
43/*
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -070044 * nss_freq_handle_ack()
Abhishek Rastogi9da47472014-03-18 19:46:15 +053045 * Handle the nss ack of frequency change.
46 */
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -070047static void nss_freq_handle_ack(struct nss_ctx_instance *nss_ctx, struct nss_freq_msg *nfa)
Abhishek Rastogi9da47472014-03-18 19:46:15 +053048{
Thomas Wu168ca262014-03-21 16:20:27 -070049 if (nfa->ack == NSS_ACK_STARTED) {
Abhishek Rastogi9da47472014-03-18 19:46:15 +053050 /*
51 * NSS finished start noficiation - HW change clocks and send end notification
52 */
Thomas Wu168ca262014-03-21 16:20:27 -070053 nss_info("%p: NSS ACK Received: %d - Change HW CLK/Send Finish to NSS\n", nss_ctx, nfa->ack);
Abhishek Rastogi9da47472014-03-18 19:46:15 +053054
55 return;
56 }
57
Thomas Wu168ca262014-03-21 16:20:27 -070058 if (nfa->ack == NSS_ACK_FINISHED) {
Abhishek Rastogi9da47472014-03-18 19:46:15 +053059 /*
60 * NSS finished end notification - Done
61 */
Thomas Wu168ca262014-03-21 16:20:27 -070062 nss_info("%p: NSS ACK Received: %d - End Notification ACK - Running: %dmhz\n", nss_ctx, nfa->ack, nfa->freq_current);
Abhishek Rastogi9da47472014-03-18 19:46:15 +053063 nss_runtime_samples.freq_scale_ready = 1;
64 return;
65 }
66
67 nss_info("%p: NSS had an error - Running: %dmhz\n", nss_ctx, nfa->freq_current);
68}
69
70/*
71 * nss_freq_change()
72 * NSS frequency change API.
73 */
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -070074nss_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 Rastogi9da47472014-03-18 19:46:15 +053075{
Abhishek Rastogi9da47472014-03-18 19:46:15 +053076 struct sk_buff *nbuf;
77 int32_t status;
Thomas Wu168ca262014-03-21 16:20:27 -070078 struct nss_corefreq_msg *ncm;
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -070079 struct nss_freq_msg *nfc;
Abhishek Rastogi9da47472014-03-18 19:46:15 +053080
Thomas Wu0e2fc4f2015-03-04 15:39:14 -080081 nss_info("%p: frequency changing to: %d\n", nss_ctx, eng);
Abhishek Rastogi9da47472014-03-18 19:46:15 +053082
83 NSS_VERIFY_CTX_MAGIC(nss_ctx);
84 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
85 return NSS_TX_FAILURE_NOT_READY;
86 }
87
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +053088 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Abhishek Rastogi9da47472014-03-18 19:46:15 +053089 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -080090 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Abhishek Rastogi9da47472014-03-18 19:46:15 +053091 return NSS_TX_FAILURE;
92 }
93
Thomas Wu168ca262014-03-21 16:20:27 -070094 ncm = (struct nss_corefreq_msg *)skb_put(nbuf, sizeof(struct nss_corefreq_msg));
Abhishek Rastogi9da47472014-03-18 19:46:15 +053095
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -070096 nss_freq_msg_init(ncm, NSS_COREFREQ_INTERFACE, NSS_TX_METADATA_TYPE_NSS_FREQ_CHANGE,
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -070097 nbuf->len, NULL, NULL);
Thomas Wu168ca262014-03-21 16:20:27 -070098 nfc = &ncm->msg.nfc;
Abhishek Rastogi9da47472014-03-18 19:46:15 +053099 nfc->frequency = eng;
100 nfc->start_or_end = start_or_end;
101 nfc->stats_enable = stats_enable;
102
103 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
104 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530105 dev_kfree_skb_any(nbuf);
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530106 nss_info("%p: unable to enqueue 'nss frequency change' - marked as stopped\n", nss_ctx);
107 return NSS_TX_FAILURE;
108 }
109
110 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);
111
112 return NSS_TX_SUCCESS;
113}
114
115/*
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700116 * nss_freq_queue_work()
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530117 * Queue Work to the NSS Workqueue based on Current index.
118 */
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800119static int nss_freq_queue_work(void)
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530120{
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800121 uint32_t index = nss_runtime_samples.freq_scale_index;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530122 BUG_ON(!nss_wq);
123
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800124 nss_info("frequency:%d index:%d sample count:%x\n", nss_runtime_samples.freq_scale[index].frequency,
125 index, nss_runtime_samples.average);
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530126
Thomas Wu7409bce2014-05-21 10:56:07 -0700127 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_ATOMIC);
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530128 if (!nss_work) {
129 nss_info("NSS FREQ WQ kmalloc fail");
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800130 return 1;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530131 }
132
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800133 /*
134 * Update proc node
135 */
136 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[index].frequency;
137
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530138 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800139 nss_work->frequency = nss_runtime_samples.freq_scale[index].frequency;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530140 nss_work->stats_enable = 1;
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800141
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530142 queue_work(nss_wq, (struct work_struct *)nss_work);
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800143 return 0;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530144}
145
146/*
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700147 * nss_freq_handle_core_stats()
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530148 * Handle the core stats
149 */
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700150static void nss_freq_handle_core_stats(struct nss_ctx_instance *nss_ctx, struct nss_core_stats *core_stats)
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530151{
152 uint32_t b_index;
153 uint32_t minimum;
154 uint32_t maximum;
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800155 uint32_t sample = core_stats->inst_cnt_total;
156 uint32_t index = nss_runtime_samples.freq_scale_index;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530157
158 /*
159 * We do not accept any statistics if auto scaling is off,
160 * we start with a fresh sample set when scaling is
161 * eventually turned on.
162 */
163 if (!nss_cmd_buf.auto_scale && nss_runtime_samples.initialized) {
164 return;
165 }
166
167 /*
168 * Delete Current Index Value, Add New Value, Recalculate new Sum, Shift Index
169 */
170 b_index = nss_runtime_samples.buffer_index;
171
172 nss_runtime_samples.sum = nss_runtime_samples.sum - nss_runtime_samples.buffer[b_index];
173 nss_runtime_samples.buffer[b_index] = sample;
174 nss_runtime_samples.sum = nss_runtime_samples.sum + nss_runtime_samples.buffer[b_index];
175 nss_runtime_samples.buffer_index = (b_index + 1) & NSS_SAMPLE_BUFFER_MASK;
176
177 if (nss_runtime_samples.sample_count < NSS_SAMPLE_BUFFER_SIZE) {
178 nss_runtime_samples.sample_count++;
179
180 /*
181 * Samples Are All Ready, Start Auto Scale
182 */
183 if (nss_runtime_samples.sample_count == NSS_SAMPLE_BUFFER_SIZE ) {
184 nss_cmd_buf.auto_scale = 1;
185 nss_runtime_samples.freq_scale_ready = 1;
186 nss_runtime_samples.initialized = 1;
187 }
188
189 return;
190 }
191
192 nss_runtime_samples.average = nss_runtime_samples.sum / nss_runtime_samples.sample_count;
193
194 /*
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800195 * Print out statistics every 10 samples
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530196 */
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800197 if (nss_runtime_samples.message_rate_limit++ >= NSS_MESSAGE_RATE_LIMIT) {
Sakthi Vignesh Radhakrishnanaa378102014-04-07 13:52:28 -0700198 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);
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800199 nss_trace("%p: Current Frequency Index:%d\n", nss_ctx, index);
Sakthi Vignesh Radhakrishnanaa378102014-04-07 13:52:28 -0700200 nss_trace("%p: Auto Scale:%d Auto Scale Ready:%d\n", nss_ctx, nss_runtime_samples.freq_scale_ready, nss_cmd_buf.auto_scale);
201 nss_trace("%p: Current Rate:%x\n", nss_ctx, nss_runtime_samples.average);
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530202
203 nss_runtime_samples.message_rate_limit = 0;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530204 }
205
206 /*
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800207 * Don't scale if we are not ready or auto scale is disabled.
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530208 */
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800209 if ((nss_runtime_samples.freq_scale_ready != 1) || (nss_cmd_buf.auto_scale != 1)) {
210 return;
211 }
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530212
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800213 /*
214 * Scale Algorithmn
215 * Algorithmn will limit how fast it will transition each scale, by the number of samples seen.
216 * If any sample is out of scale during the idle count, the rate_limit will reset to 0.
217 * Scales are limited to the max number of cpu scales we support.
218 */
219 if (nss_runtime_samples.freq_scale_rate_limit_up++ >= NSS_FREQUENCY_SCALE_RATE_LIMIT_UP) {
220 maximum = nss_runtime_samples.freq_scale[index].maximum;
Thomas Wu7132bd32015-05-07 15:03:06 -0700221 if ((sample > maximum) && (index < (NSS_FREQ_MAX_SCALE - 1))) {
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800222 nss_runtime_samples.freq_scale_index++;
223 nss_runtime_samples.freq_scale_ready = 0;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530224
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800225 /*
226 * If fail to increase frequency, decrease index
227 */
Thomas Wu4640e562015-06-10 10:23:04 -0700228 nss_trace("frequency increase to %d inst:%x > maximum:%x\n", nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency, sample, maximum);
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800229 if (nss_freq_queue_work()) {
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530230 nss_runtime_samples.freq_scale_index--;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530231 }
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530232 }
Thomas Wu4640e562015-06-10 10:23:04 -0700233
234 /*
235 * Reset the down scale counter based on running average, so can idle properlly
236 */
Thomas Wuc151f2e2015-09-08 10:59:44 -0700237 if (nss_runtime_samples.average > maximum) {
Thomas Wu4640e562015-06-10 10:23:04 -0700238 nss_trace("down scale timeout reset running average:%x\n", nss_runtime_samples.average);
239 nss_runtime_samples.freq_scale_rate_limit_down = 0;
240 }
241
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800242 nss_runtime_samples.freq_scale_rate_limit_up = 0;
243 return;
244 }
245
246 if (nss_runtime_samples.freq_scale_rate_limit_down++ >= NSS_FREQUENCY_SCALE_RATE_LIMIT_DOWN) {
247 minimum = nss_runtime_samples.freq_scale[index].minimum;
248 if ((nss_runtime_samples.average < minimum) && (index > 0)) {
249 nss_runtime_samples.freq_scale_index--;
250 nss_runtime_samples.freq_scale_ready = 0;
251
252 /*
253 * If fail to decrease frequency, increase index
254 */
Thomas Wuc151f2e2015-09-08 10:59:44 -0700255 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);
Thomas Wu0e2fc4f2015-03-04 15:39:14 -0800256 if (nss_freq_queue_work()) {
257 nss_runtime_samples.freq_scale_index++;
258 }
259 }
260 nss_runtime_samples.freq_scale_rate_limit_down = 0;
261 return;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530262 }
263}
264
265/*
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700266 * nss_freq_interface_handler()
Thomas Wuc07d8702014-03-19 15:46:19 -0700267 * Handle NSS -> HLOS messages for Frequency Changes and Statistics
268 */
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700269static void nss_freq_interface_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) {
Thomas Wuc07d8702014-03-19 15:46:19 -0700270
271 struct nss_corefreq_msg *ncfm = (struct nss_corefreq_msg *)ncm;
272
Thomas Wu168ca262014-03-21 16:20:27 -0700273 switch (ncfm->cm.type) {
Thomas Wuc07d8702014-03-19 15:46:19 -0700274 case COREFREQ_METADATA_TYPE_TX_FREQ_ACK:
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700275 nss_freq_handle_ack(nss_ctx, &ncfm->msg.nfc);
Thomas Wuc07d8702014-03-19 15:46:19 -0700276 break;
277 case COREFREQ_METADATA_TYPE_TX_CORE_STATS:
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700278 nss_freq_handle_core_stats(nss_ctx, &ncfm->msg.ncs);
Thomas Wuc07d8702014-03-19 15:46:19 -0700279 break;
280
281 default:
282 if (ncm->response != NSS_CMN_RESPONSE_ACK) {
283 /*
284 * Check response
285 */
Thomas Wu68250352014-04-02 18:59:40 -0700286 nss_info("%p: Received response %d for type %d, interface %d", nss_ctx, ncm->response, ncm->type, ncm->interface);
Thomas Wuc07d8702014-03-19 15:46:19 -0700287 }
288 }
289}
290
291/*
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700292 * nss_freq_register_handler()
Thomas Wuc07d8702014-03-19 15:46:19 -0700293 */
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700294void nss_freq_register_handler(void)
Thomas Wuc07d8702014-03-19 15:46:19 -0700295{
Sundarajan Srinivasandedd8e42014-10-06 11:59:34 -0700296 nss_core_register_handler(NSS_COREFREQ_INTERFACE, nss_freq_interface_handler, NULL);
Thomas Wuc07d8702014-03-19 15:46:19 -0700297}