blob: 085a181c24115c45997875d7722ae8b6b3d93718 [file] [log] [blame]
Radhakrishna Jiguru1c9b2252013-08-27 23:57:48 +05301/*
2 **************************************************************************
Zac Livingston866b0e22013-10-23 18:14:17 -06003 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Radhakrishna Jiguru1c9b2252013-08-27 23:57:48 +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 */
Abhishek Rastogibc74e432013-04-02 10:28:22 +053016
17/*
18 * nss_init.c
19 * NSS init APIs
20 *
21 */
Abhishek Rastogibc74e432013-04-02 10:28:22 +053022#include "nss_core.h"
Pamidipati, Vijay7f413b52013-09-24 19:07:12 +053023#include "nss_pm.h"
24
Abhishek Rastogibc74e432013-04-02 10:28:22 +053025#include <nss_hal.h>
Thomas Wufb6a6842013-10-23 13:14:27 -070026#include <nss_clocks.h>
Abhishek Rastogibc74e432013-04-02 10:28:22 +053027
28#include <linux/module.h>
29#include <linux/platform_device.h>
wthomas442c7972013-08-05 14:28:17 -070030#include <linux/proc_fs.h>
31#include <linux/device.h>
Abhishek Rastogibc74e432013-04-02 10:28:22 +053032#include <mach/msm_nss.h>
33
wthomas442c7972013-08-05 14:28:17 -070034#include <linux/sysctl.h>
35#include <linux/regulator/consumer.h>
Thomas Wufb6a6842013-10-23 13:14:27 -070036#include <linux/clk.h>
wthomas442c7972013-08-05 14:28:17 -070037
Abhishek Rastogibc74e432013-04-02 10:28:22 +053038/*
39 * Global declarations
40 */
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +053041int nss_ctl_redirect __read_mostly = 0;
Abhishek Rastogi1626a902013-11-21 17:09:49 +053042int nss_ctl_debug __read_mostly = 0;
Abhishek Rastogibc74e432013-04-02 10:28:22 +053043
44/*
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +053045 * PM client handle
46 */
47static void *pm_client;
48
49/*
wthomas626147f2013-09-18 13:12:40 -070050 * Handler to send NSS messages
51 */
52void *nss_freq_change_context;
Thomas Wufb6a6842013-10-23 13:14:27 -070053struct clk *nss_core0_clk;
wthomas626147f2013-09-18 13:12:40 -070054
55/*
Abhishek Rastogibc74e432013-04-02 10:28:22 +053056 * Top level nss context structure
57 */
58struct nss_top_instance nss_top_main;
wthomas442c7972013-08-05 14:28:17 -070059struct nss_cmd_buffer nss_cmd_buf;
wthomas626147f2013-09-18 13:12:40 -070060struct nss_runtime_sampling nss_runtime_samples;
61struct workqueue_struct *nss_wq;
62
63/*
64 * Work Queue to handle messages to Kernel
65 */
66nss_work_t *nss_work;
Abhishek Rastogibc74e432013-04-02 10:28:22 +053067
68/*
69 * File local/Static variables/functions
70 */
71
Abhishek Rastogi271eee72013-07-29 21:08:36 +053072static const struct net_device_ops nss_netdev_ops;
73static const struct ethtool_ops nss_ethtool_ops;
74
75/*
76 * nss_dummy_netdev_setup()
77 * Dummy setup for net_device handler
78 */
79static void nss_dummy_netdev_setup(struct net_device *ndev)
80{
wthomas626147f2013-09-18 13:12:40 -070081
Abhishek Rastogi271eee72013-07-29 21:08:36 +053082}
83
Abhishek Rastogibc74e432013-04-02 10:28:22 +053084/*
85 * nss_handle_irq()
86 * HLOS interrupt handler for nss interrupts
87 */
88static irqreturn_t nss_handle_irq (int irq, void *ctx)
89{
90 struct int_ctx_instance *int_ctx = (struct int_ctx_instance *) ctx;
Abhishek Rastogi80f4eb12013-09-24 14:31:21 +053091 struct nss_ctx_instance *nss_ctx = int_ctx->nss_ctx;
Abhishek Rastogibc74e432013-04-02 10:28:22 +053092
93 /*
Abhishek Rastogi80f4eb12013-09-24 14:31:21 +053094 * Mask interrupt until our bottom half re-enables it
Abhishek Rastogibc74e432013-04-02 10:28:22 +053095 */
Abhishek Rastogi80f4eb12013-09-24 14:31:21 +053096 nss_hal_disable_interrupt(nss_ctx->nmap, int_ctx->irq,
97 int_ctx->shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogibc74e432013-04-02 10:28:22 +053098
99 /*
100 * Schedule tasklet to process interrupt cause
101 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530102 napi_schedule(&int_ctx->napi);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530103 return IRQ_HANDLED;
104}
105
106/*
107 * nss_probe()
108 * HLOS device probe callback
109 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530110static int __devinit nss_probe(struct platform_device *nss_dev)
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530111{
112 struct nss_top_instance *nss_top = &nss_top_main;
113 struct nss_ctx_instance *nss_ctx = &nss_top->nss[nss_dev->id];
114 struct nss_platform_data *npd = (struct nss_platform_data *) nss_dev->dev.platform_data;
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530115 struct netdev_priv_instance *ndev_priv;
116 int i, err = 0;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530117
118 nss_ctx->nss_top = nss_top;
119 nss_ctx->id = nss_dev->id;
120
wthomas442c7972013-08-05 14:28:17 -0700121 nss_info("%p: NSS_DEV_ID %s \n", nss_ctx, dev_name(&nss_dev->dev));
122
123 /*
124 * Both NSS cores controlled by same regulator, Hook only Once
125 */
126 if (!nss_dev->id) {
Thomas Wufb6a6842013-10-23 13:14:27 -0700127 nss_core0_clk = clk_get(&nss_dev->dev, "nss_core_clk");
128 if (IS_ERR(nss_core0_clk)) {
wthomas442c7972013-08-05 14:28:17 -0700129
Thomas Wufb6a6842013-10-23 13:14:27 -0700130 err = PTR_ERR(nss_core0_clk);
wthomas442c7972013-08-05 14:28:17 -0700131 nss_info("%p: Regulator %s get failed, err=%d\n", nss_ctx, dev_name(&nss_dev->dev), err);
132 return err;
133
wthomas442c7972013-08-05 14:28:17 -0700134 }
Thomas Wufb6a6842013-10-23 13:14:27 -0700135 clk_set_rate(nss_core0_clk, NSS_FREQ_550);
136 clk_prepare(nss_core0_clk);
137 clk_enable(nss_core0_clk);
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800138
139 /*
140 * Check if turbo is supported
141 */
142 if (npd->turbo_frequency) {
143 /*
144 * Turbo is supported
145 */
146 printk("nss_driver - Turbo Support %d\n", npd->turbo_frequency);
147 nss_runtime_samples.freq_scale_sup_max = NSS_MAX_CPU_SCALES;
Pamidipati, Vijayf9b5a272014-01-22 14:24:10 +0530148 nss_pm_set_turbo();
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800149 } else {
150 printk("nss_driver - Turbo No Support %d\n", npd->turbo_frequency);
151 nss_runtime_samples.freq_scale_sup_max = NSS_MAX_CPU_SCALES - 1;
152 }
wthomas442c7972013-08-05 14:28:17 -0700153 }
154
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530155 /*
Sakthi Vignesh Radhakrishnand923e342013-12-09 11:53:03 -0800156 * Get load address of NSS firmware
157 */
158 nss_info("%p: Setting NSS%d Firmware load address to %x\n", nss_ctx, nss_dev->id, npd->load_addr);
159 nss_top->nss[nss_dev->id].load = npd->load_addr;
160
161 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530162 * Get virtual and physical memory addresses for nss logical/hardware address maps
163 */
164
165 /*
166 * Virtual address of CSM space
167 */
168 nss_ctx->nmap = npd->nmap;
169 nss_assert(nss_ctx->nmap);
170
171 /*
172 * Physical address of CSM space
173 */
174 nss_ctx->nphys = npd->nphys;
175 nss_assert(nss_ctx->nphys);
176
177 /*
178 * Virtual address of logical registers space
179 */
180 nss_ctx->vmap = npd->vmap;
181 nss_assert(nss_ctx->vmap);
182
183 /*
184 * Physical address of logical registers space
185 */
186 nss_ctx->vphys = npd->vphys;
187 nss_assert(nss_ctx->vphys);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530188 nss_info("%d:ctx=%p, vphys=%x, vmap=%x, nphys=%x, nmap=%x",
189 nss_dev->id, nss_ctx, nss_ctx->vphys, nss_ctx->vmap, nss_ctx->nphys, nss_ctx->nmap);
190
191 /*
192 * Register netdevice handlers
193 */
194 nss_ctx->int_ctx[0].ndev = alloc_netdev(sizeof(struct netdev_priv_instance),
195 "qca-nss-dev%d", nss_dummy_netdev_setup);
196 if (nss_ctx->int_ctx[0].ndev == NULL) {
197 nss_warning("%p: Could not allocate net_device #0", nss_ctx);
198 err = -ENOMEM;
199 goto err_init_0;
200 }
201
202 nss_ctx->int_ctx[0].ndev->netdev_ops = &nss_netdev_ops;
203 nss_ctx->int_ctx[0].ndev->ethtool_ops = &nss_ethtool_ops;
204 err = register_netdev(nss_ctx->int_ctx[0].ndev);
205 if (err) {
206 nss_warning("%p: Could not register net_device #0", nss_ctx);
207 goto err_init_1;
208 }
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530209
210 /*
211 * request for IRQs
212 *
213 * WARNING: CPU affinities should be set using OS supported methods
214 */
215 nss_ctx->int_ctx[0].nss_ctx = nss_ctx;
216 nss_ctx->int_ctx[0].shift_factor = 0;
217 nss_ctx->int_ctx[0].irq = npd->irq[0];
218 err = request_irq(npd->irq[0], nss_handle_irq, IRQF_DISABLED, "nss", &nss_ctx->int_ctx[0]);
219 if (err) {
220 nss_warning("%d: IRQ0 request failed", nss_dev->id);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530221 goto err_init_2;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530222 }
223
224 /*
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530225 * Register NAPI for NSS core interrupt #0
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530226 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530227 ndev_priv = netdev_priv(nss_ctx->int_ctx[0].ndev);
228 ndev_priv->int_ctx = &nss_ctx->int_ctx[0];
229 netif_napi_add(nss_ctx->int_ctx[0].ndev, &nss_ctx->int_ctx[0].napi, nss_core_handle_napi, 64);
230 napi_enable(&nss_ctx->int_ctx[0].napi);
231 nss_ctx->int_ctx[0].napi_active = true;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530232
233 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530234 * Check if second interrupt is supported on this nss core
235 */
236 if (npd->num_irq > 1) {
237 nss_info("%d: This NSS core supports two interrupts", nss_dev->id);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530238
239 /*
240 * Register netdevice handlers
241 */
242 nss_ctx->int_ctx[1].ndev = alloc_netdev(sizeof(struct netdev_priv_instance),
243 "qca-nss-dev%d", nss_dummy_netdev_setup);
244 if (nss_ctx->int_ctx[1].ndev == NULL) {
245 nss_warning("%p: Could not allocate net_device #1", nss_ctx);
246 err = -ENOMEM;
247 goto err_init_3;
248 }
249
250 nss_ctx->int_ctx[1].ndev->netdev_ops = &nss_netdev_ops;
251 nss_ctx->int_ctx[1].ndev->ethtool_ops = &nss_ethtool_ops;
252 err = register_netdev(nss_ctx->int_ctx[1].ndev);
253 if (err) {
254 nss_warning("%p: Could not register net_device #1", nss_ctx);
255 goto err_init_4;
256 }
257
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530258 nss_ctx->int_ctx[1].nss_ctx = nss_ctx;
259 nss_ctx->int_ctx[1].shift_factor = 15;
260 nss_ctx->int_ctx[1].irq = npd->irq[1];
261 err = request_irq(npd->irq[1], nss_handle_irq, IRQF_DISABLED, "nss", &nss_ctx->int_ctx[1]);
262 if (err) {
263 nss_warning("%d: IRQ1 request failed for nss", nss_dev->id);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530264 goto err_init_5;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530265 }
266
267 /*
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530268 * Register NAPI for NSS core interrupt #1
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530269 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530270 ndev_priv = netdev_priv(nss_ctx->int_ctx[1].ndev);
271 ndev_priv->int_ctx = &nss_ctx->int_ctx[1];
272 netif_napi_add(nss_ctx->int_ctx[1].ndev, &nss_ctx->int_ctx[1].napi, nss_core_handle_napi, 64);
273 napi_enable(&nss_ctx->int_ctx[1].napi);
274 nss_ctx->int_ctx[1].napi_active = true;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530275 }
276
277 spin_lock_bh(&(nss_top->lock));
278
279 /*
280 * Check functionalities are supported by this NSS core
281 */
282 if (npd->ipv4_enabled == NSS_FEATURE_ENABLED) {
283 nss_top->ipv4_handler_id = nss_dev->id;
284 }
285
286 if (npd->ipv6_enabled == NSS_FEATURE_ENABLED) {
287 nss_top->ipv6_handler_id = nss_dev->id;
288 }
289
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530290 if (npd->crypto_enabled == NSS_FEATURE_ENABLED) {
291 nss_top->crypto_handler_id = nss_dev->id;
292 }
293
294 if (npd->ipsec_enabled == NSS_FEATURE_ENABLED) {
295 nss_top->ipsec_handler_id = nss_dev->id;
296 }
297
298 if (npd->wlan_enabled == NSS_FEATURE_ENABLED) {
299 nss_top->wlan_handler_id = nss_dev->id;
300 }
301
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530302 if (npd->tun6rd_enabled == NSS_FEATURE_ENABLED) {
303 nss_top->tun6rd_handler_id = nss_dev->id;
304 }
305
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530306 if (npd->tunipip6_enabled == NSS_FEATURE_ENABLED) {
307 nss_top->tunipip6_handler_id = nss_dev->id;
308 }
309
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530310 if (npd->gmac_enabled[0] == NSS_FEATURE_ENABLED) {
311 nss_top->phys_if_handler_id[0] = nss_dev->id;
312 }
313
314 if (npd->gmac_enabled[1] == NSS_FEATURE_ENABLED) {
315 nss_top->phys_if_handler_id[1] = nss_dev->id;
316 }
317
318 if (npd->gmac_enabled[2] == NSS_FEATURE_ENABLED) {
319 nss_top->phys_if_handler_id[2] = nss_dev->id;
320 }
321
322 if (npd->gmac_enabled[3] == NSS_FEATURE_ENABLED) {
323 nss_top->phys_if_handler_id[3] = nss_dev->id;
324 }
325
wthomas626147f2013-09-18 13:12:40 -0700326 nss_top->frequency_handler_id = nss_dev->id;
327
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530328 spin_unlock_bh(&(nss_top->lock));
329
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530330 /*
331 * Initialize decongestion callbacks to NULL
332 */
333 for (i = 0; i< NSS_MAX_CLIENTS; i++) {
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530334 nss_ctx->queue_decongestion_callback[i] = 0;
335 nss_ctx->queue_decongestion_ctx[i] = 0;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530336 }
337
338 spin_lock_init(&(nss_ctx->decongest_cb_lock));
339 nss_ctx->magic = NSS_CTX_MAGIC;
340
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530341 nss_info("%p: Reseting NSS core %d now", nss_ctx, nss_ctx->id);
342
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530343 /*
344 * Enable clocks and bring NSS core out of reset
345 */
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530346 nss_hal_core_reset(nss_dev->id, nss_ctx->nmap, nss_ctx->load, nss_top->clk_src);
347
348 /*
349 * Enable interrupts for NSS core
350 */
351 nss_hal_enable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[0].irq,
352 nss_ctx->int_ctx[0].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
353
354 if (npd->num_irq > 1) {
355 nss_hal_enable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[1].irq,
356 nss_ctx->int_ctx[1].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
357 }
358
Radhakrishna Jigurub7346cf2013-12-16 13:08:43 +0530359 /*
360 * Initialize max buffer size for NSS core
361 */
362 nss_ctx->max_buf_size = NSS_NBUF_PAYLOAD_SIZE;
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530363 nss_info("%p: All resources initialized and nss core%d has been brought out of reset", nss_ctx, nss_dev->id);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530364 goto err_init_0;
365
366err_init_5:
367 unregister_netdev(nss_ctx->int_ctx[1].ndev);
368err_init_4:
369 free_netdev(nss_ctx->int_ctx[1].ndev);
370err_init_3:
371 free_irq(npd->irq[0], &nss_ctx->int_ctx[0]);
372err_init_2:
373 unregister_netdev(nss_ctx->int_ctx[0].ndev);
374err_init_1:
375 free_netdev(nss_ctx->int_ctx[0].ndev);
376err_init_0:
377 return err;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530378}
379
380/*
381 * nss_remove()
382 * HLOS device remove callback
383 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530384static int __devexit nss_remove(struct platform_device *nss_dev)
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530385{
386 struct nss_top_instance *nss_top = &nss_top_main;
387 struct nss_ctx_instance *nss_ctx = &nss_top->nss[nss_dev->id];
388
389 /*
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530390 * Clean-up debugfs
391 */
392 nss_stats_clean();
393
394 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530395 * Disable interrupts and bottom halves in HLOS
396 * Disable interrupts from NSS to HLOS
397 */
398 nss_hal_disable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[0].irq,
399 nss_ctx->int_ctx[0].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530400
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530401 free_irq(nss_ctx->int_ctx[0].irq, &nss_ctx->int_ctx[0]);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530402 unregister_netdev(nss_ctx->int_ctx[0].ndev);
403 free_netdev(nss_ctx->int_ctx[0].ndev);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530404
405 /*
406 * Check if second interrupt is supported
407 * If so then clear resources for second interrupt as well
408 */
409 if (nss_ctx->int_ctx[1].irq) {
410 nss_hal_disable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[1].irq,
411 nss_ctx->int_ctx[1].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530412 free_irq(nss_ctx->int_ctx[1].irq, &nss_ctx->int_ctx[1]);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530413 unregister_netdev(nss_ctx->int_ctx[1].ndev);
414 free_netdev(nss_ctx->int_ctx[1].ndev);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530415 }
416
417 nss_info("%p: All resources freed for nss core%d", nss_ctx, nss_dev->id);
418 return 0;
419}
420
421/*
422 * nss_driver
423 * Platform driver structure for NSS
424 */
425struct platform_driver nss_driver = {
426 .probe = nss_probe,
427 .remove = __devexit_p(nss_remove),
428 .driver = {
429 .name = "qca-nss",
430 .owner = THIS_MODULE,
431 },
432};
433
434/*
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530435 * nss_reset_frequency_stats_samples()
436 * Reset all frequency sampling state when auto scaling is turned off.
437 */
438static void nss_reset_frequency_stats_samples (void)
439{
440 nss_runtime_samples.buffer_index = 0;
441 nss_runtime_samples.sum = 0;
442 nss_runtime_samples.average = 0;
443 nss_runtime_samples.sample_count = 0;
444 nss_runtime_samples.message_rate_limit = 0;
445 nss_runtime_samples.freq_scale_rate_limit_up = 0;
446 nss_runtime_samples.freq_scale_rate_limit_down = 0;
447}
448
449/*
wthomas626147f2013-09-18 13:12:40 -0700450 ***************************************************************************************************
Thomas Wufb6a6842013-10-23 13:14:27 -0700451 * nss_wq_function() is used to queue up requests to change NSS frequencies.
452 * The function will take care of NSS notices and also control clock.
453 * The auto rate algorithmn will queue up requests or the procfs may also queue up these requests.
wthomas626147f2013-09-18 13:12:40 -0700454 ***************************************************************************************************
455 */
456
457/*
458 * nss_wq_function()
459 * Added to Handle BH requests to kernel
460 */
461void nss_wq_function (struct work_struct *work)
462{
463 nss_work_t *my_work = (nss_work_t *)work;
464
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530465 nss_freq_change(nss_freq_change_context, my_work->frequency, my_work->stats_enable, 0);
Thomas Wufb6a6842013-10-23 13:14:27 -0700466 clk_set_rate(nss_core0_clk, my_work->frequency);
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530467 nss_freq_change(nss_freq_change_context, my_work->frequency, my_work->stats_enable, 1);
wthomas626147f2013-09-18 13:12:40 -0700468
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +0530469 if(!pm_client) {
470 goto out;
471 }
472
473 if (my_work->frequency == NSS_FREQ_733) {
474 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_TURBO);
475 } else if ((my_work->frequency == NSS_FREQ_275) || (my_work->frequency == NSS_FREQ_550)) {
476 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_NOMINAL);
477 } else {
478 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_IDLE);
479 }
480out:
wthomas626147f2013-09-18 13:12:40 -0700481 kfree((void *)work);
482}
483
484/*
wthomas442c7972013-08-05 14:28:17 -0700485 * nss_current_freq_handler()
486 * Handle Userspace Frequency Change Requests
487 */
488static int nss_current_freq_handler (ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
489{
wthomas442c7972013-08-05 14:28:17 -0700490 int ret;
wthomas626147f2013-09-18 13:12:40 -0700491
492 BUG_ON(!nss_wq);
wthomas442c7972013-08-05 14:28:17 -0700493
494 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
495
wthomasd39fa822013-08-22 16:44:23 -0700496 if (!write) {
wthomas626147f2013-09-18 13:12:40 -0700497 printk("Frequency Set to %d\n", nss_cmd_buf.current_freq);
wthomasd39fa822013-08-22 16:44:23 -0700498 return ret;
wthomas442c7972013-08-05 14:28:17 -0700499 }
wthomasd39fa822013-08-22 16:44:23 -0700500
wthomas626147f2013-09-18 13:12:40 -0700501 /* Turn off Auto Scale */
502 nss_cmd_buf.auto_scale = 0;
503 nss_runtime_samples.freq_scale_ready = 0;
wthomasd39fa822013-08-22 16:44:23 -0700504
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800505 /* If support NSS freq is in the table send the new frequency request to NSS or If No Turbo and ask for turbo freq */
506 if (((nss_cmd_buf.current_freq != NSS_FREQ_110) && (nss_cmd_buf.current_freq != NSS_FREQ_275) && (nss_cmd_buf.current_freq != NSS_FREQ_550) && (nss_cmd_buf.current_freq != NSS_FREQ_733)) || ((nss_runtime_samples.freq_scale_sup_max != NSS_MAX_CPU_SCALES) && (nss_cmd_buf.current_freq == NSS_FREQ_733))) {
Thomas Wufb6a6842013-10-23 13:14:27 -0700507 printk("Frequency not found. Please check Frequency Table\n");
wthomas626147f2013-09-18 13:12:40 -0700508 return ret;
wthomasd39fa822013-08-22 16:44:23 -0700509 }
510
wthomas626147f2013-09-18 13:12:40 -0700511 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
512 if (!nss_work) {
513 nss_info("NSS Freq WQ kmalloc fail");
514 return ret;
515 }
516 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
517 nss_work->frequency = nss_cmd_buf.current_freq;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530518 nss_work->stats_enable = 0;
519
520 /* Ensure we start with a fresh set of samples later */
521 nss_reset_frequency_stats_samples();
522
wthomas626147f2013-09-18 13:12:40 -0700523 queue_work(nss_wq, (struct work_struct *)nss_work);
524
wthomas442c7972013-08-05 14:28:17 -0700525 return ret;
526}
527
528/*
529 * nss_auto_scale_handler()
530 * Enables or Disable Auto Scaling
531 */
532static int nss_auto_scale_handler (ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
533{
534 int ret;
535
536 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
537
wthomas626147f2013-09-18 13:12:40 -0700538 if (!write) {
539 return ret;
540 }
541
Thomas Wufb6a6842013-10-23 13:14:27 -0700542 if (nss_cmd_buf.auto_scale != 1) {
wthomas626147f2013-09-18 13:12:40 -0700543 /*
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530544 * Is auto scaling currently enabled? If so, send the command to
545 * disable stats reporting to NSS
wthomas626147f2013-09-18 13:12:40 -0700546 */
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530547 if (nss_runtime_samples.freq_scale_ready != 0) {
548 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
549 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
550 if (!nss_work) {
551 nss_info("NSS Freq WQ kmalloc fail");
552 return ret;
553 }
554 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
555 nss_work->frequency = nss_cmd_buf.current_freq;
556 nss_work->stats_enable = 0;
557 queue_work(nss_wq, (struct work_struct *)nss_work);
558 nss_runtime_samples.freq_scale_ready = 0;
559
560 /*
561 * The current samples would be stale later when scaling is
562 * enabled again, hence reset them
563 */
564 nss_reset_frequency_stats_samples();
565 }
Thomas Wufb6a6842013-10-23 13:14:27 -0700566 return ret;
wthomas626147f2013-09-18 13:12:40 -0700567 }
wthomas442c7972013-08-05 14:28:17 -0700568
Thomas Wufb6a6842013-10-23 13:14:27 -0700569 /*
570 * Auto Scaling is already being done
571 */
572 if (nss_runtime_samples.freq_scale_ready == 1) {
573 return ret;
574 }
575
576 /*
577 * Setup default values - Middle of Freq Scale Band
578 */
579 nss_runtime_samples.freq_scale_index = 1;
580 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
581
582 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
583 if (!nss_work) {
584 nss_info("NSS Freq WQ kmalloc fail");
585 return ret;
586 }
587 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
588 nss_work->frequency = nss_cmd_buf.current_freq;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530589 nss_work->stats_enable = 1;
Thomas Wufb6a6842013-10-23 13:14:27 -0700590 queue_work(nss_wq, (struct work_struct *)nss_work);
591
592 nss_runtime_samples.freq_scale_ready = 1;
593
wthomas442c7972013-08-05 14:28:17 -0700594 return ret;
595}
596
597/*
598 * nss_get_freq_table_handler()
599 * Display Support Freq and Ex how to Change.
600 */
Thomas Wu05495be2013-12-19 14:24:24 -0800601static int nss_get_freq_table_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
wthomas442c7972013-08-05 14:28:17 -0700602{
603 int ret;
604
605 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
606
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800607 if (nss_runtime_samples.freq_scale_sup_max != NSS_MAX_CPU_SCALES) {
608 printk("Frequency Supported - 110Mhz 275Mhz 550Mhz\n");
609 printk("Ex. To Change Frequency - echo 110000000 > current_freq \n");
610
611 return ret;
612 }
613
wthomas626147f2013-09-18 13:12:40 -0700614 printk("Frequency Supported - 110Mhz 275Mhz 550Mhz 733Mhz \n");
wthomas442c7972013-08-05 14:28:17 -0700615 printk("Ex. To Change Frequency - echo 110000000 > current_freq \n");
616
617 return ret;
618}
619
620/*
Thomas Wu05495be2013-12-19 14:24:24 -0800621 * nss_get_average_inst_handler()
622 * Display AVG Inst Per Ms.
623 */
624static int nss_get_average_inst_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
625{
626 int ret;
627
628 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
629
630 if (!ret && !write) {
631 printk("Current Inst Per Ms %x\n", nss_runtime_samples.average);
632 }
633
634 return ret;
635}
636
637/*
Abhishek Rastogi1626a902013-11-21 17:09:49 +0530638 * nss_debug_handler()
639 * Enable NSS debug output
640 */
641static int nss_debug_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
642{
643 int ret;
644
645 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
646 if (!ret) {
647 if ((write) && (nss_ctl_debug != 0)) {
648 printk("Enabling NSS SPI Debug\n");
649 nss_hal_debug_enable();
650 }
651 }
652
653 return ret;
654}
655
656/*
wthomas442c7972013-08-05 14:28:17 -0700657 * sysctl-tuning infrastructure.
658 */
659static ctl_table nss_freq_table[] = {
660 {
661 .procname = "current_freq",
662 .data = &nss_cmd_buf.current_freq,
663 .maxlen = sizeof(int),
664 .mode = 0644,
665 .proc_handler = &nss_current_freq_handler,
666 },
667 {
668 .procname = "freq_table",
669 .data = &nss_cmd_buf.max_freq,
670 .maxlen = sizeof(int),
671 .mode = 0644,
672 .proc_handler = &nss_get_freq_table_handler,
673 },
674 {
675 .procname = "auto_scale",
676 .data = &nss_cmd_buf.auto_scale,
677 .maxlen = sizeof(int),
678 .mode = 0644,
679 .proc_handler = &nss_auto_scale_handler,
680 },
Thomas Wu05495be2013-12-19 14:24:24 -0800681 {
682 .procname = "inst_per_sec",
683 .data = &nss_cmd_buf.average_inst,
684 .maxlen = sizeof(int),
685 .mode = 0644,
686 .proc_handler = &nss_get_average_inst_handler,
687 },
wthomas442c7972013-08-05 14:28:17 -0700688 { }
689};
690
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530691static ctl_table nss_general_table[] = {
692 {
693 .procname = "redirect",
694 .data = &nss_ctl_redirect,
695 .maxlen = sizeof(int),
696 .mode = 0644,
697 .proc_handler = proc_dointvec,
698 },
Abhishek Rastogi1626a902013-11-21 17:09:49 +0530699 {
700 .procname = "debug",
701 .data = &nss_ctl_debug,
702 .maxlen = sizeof(int),
703 .mode = 0644,
704 .proc_handler = &nss_debug_handler,
705 },
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530706 { }
707};
708
wthomas442c7972013-08-05 14:28:17 -0700709static ctl_table nss_clock_dir[] = {
710 {
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530711 .procname = "clock",
712 .mode = 0555,
713 .child = nss_freq_table,
714 },
715 {
716 .procname = "general",
717 .mode = 0555,
718 .child = nss_general_table,
wthomas442c7972013-08-05 14:28:17 -0700719 },
720 { }
721};
722
723static ctl_table nss_root_dir[] = {
724 {
725 .procname = "nss",
726 .mode = 0555,
727 .child = nss_clock_dir,
728 },
729 { }
730};
731
732static ctl_table nss_root[] = {
733 {
734 .procname = "dev",
735 .mode = 0555,
736 .child = nss_root_dir,
737 },
738 { }
739};
740
741static struct ctl_table_header *nss_dev_header;
742
743/*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530744 * nss_init()
745 * Registers nss driver
746 */
747static int __init nss_init(void)
748{
749 nss_info("Init NSS driver");
750
wthomas626147f2013-09-18 13:12:40 -0700751 nss_freq_change_context = nss_get_frequency_mgr();
752
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530753 /*
754 * Perform clock init common to all NSS cores
755 */
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530756 nss_hal_common_reset(&(nss_top_main.clk_src));
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530757
758 /*
759 * Enable spin locks
760 */
761 spin_lock_init(&(nss_top_main.lock));
762 spin_lock_init(&(nss_top_main.stats_lock));
763
764 /*
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530765 * Enable NSS statistics
766 */
767 nss_stats_init();
768
769 /*
wthomas442c7972013-08-05 14:28:17 -0700770 * Register sysctl table.
771 */
772 nss_dev_header = register_sysctl_table(nss_root);
773
774 /*
wthomas626147f2013-09-18 13:12:40 -0700775 * Setup Runtime Sample values
776 */
777 nss_runtime_samples.freq_scale[0].frequency = NSS_FREQ_110;
wthomas626147f2013-09-18 13:12:40 -0700778 nss_runtime_samples.freq_scale[0].minimum = NSS_FREQ_110_MIN;
779 nss_runtime_samples.freq_scale[0].maximum = NSS_FREQ_110_MAX;
wthomas626147f2013-09-18 13:12:40 -0700780 nss_runtime_samples.freq_scale[1].frequency = NSS_FREQ_550;
wthomas626147f2013-09-18 13:12:40 -0700781 nss_runtime_samples.freq_scale[1].minimum = NSS_FREQ_550_MIN;
782 nss_runtime_samples.freq_scale[1].maximum = NSS_FREQ_550_MAX;
wthomas626147f2013-09-18 13:12:40 -0700783 nss_runtime_samples.freq_scale[2].frequency = NSS_FREQ_733;
wthomas626147f2013-09-18 13:12:40 -0700784 nss_runtime_samples.freq_scale[2].minimum = NSS_FREQ_733_MIN;
785 nss_runtime_samples.freq_scale[2].maximum = NSS_FREQ_733_MAX;
wthomas626147f2013-09-18 13:12:40 -0700786 nss_runtime_samples.freq_scale_index = 1;
787 nss_runtime_samples.freq_scale_ready = 0;
Thomas Wu9681f7e2013-11-06 13:12:57 -0800788 nss_runtime_samples.freq_scale_rate_limit_up = 0;
789 nss_runtime_samples.freq_scale_rate_limit_down = 0;
wthomas626147f2013-09-18 13:12:40 -0700790 nss_runtime_samples.buffer_index = 0;
791 nss_runtime_samples.sum = 0;
792 nss_runtime_samples.sample_count = 0;
793 nss_runtime_samples.average = 0;
794 nss_runtime_samples.message_rate_limit = 0;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530795 nss_runtime_samples.initialized = 0;
wthomas626147f2013-09-18 13:12:40 -0700796
Thomas Wu05495be2013-12-19 14:24:24 -0800797 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
798
wthomas626147f2013-09-18 13:12:40 -0700799 /*
800 * Initial Workqueue
801 */
802 nss_wq = create_workqueue("nss_freq_queue");
803
804 /*
Pamidipati, Vijay7f413b52013-09-24 19:07:12 +0530805 * Initialize NSS Bus PM module
806 */
807 nss_pm_init();
808
809 /*
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +0530810 * Register with Bus driver
811 */
812 pm_client = nss_pm_client_register(NSS_PM_CLIENT_NETAP);
813 if (!pm_client) {
814 nss_warning("Error registering with PM driver");
815 }
816
817 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530818 * Register platform_driver
819 */
820 return platform_driver_register(&nss_driver);
821}
822
823/*
824 * nss_cleanup()
825 * Unregisters nss driver
826 */
827static void __exit nss_cleanup(void)
828{
829 nss_info("Exit NSS driver");
wthomas442c7972013-08-05 14:28:17 -0700830
831 if (nss_dev_header)
832 unregister_sysctl_table(nss_dev_header);
833
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530834 platform_driver_unregister(&nss_driver);
835}
836
837module_init(nss_init);
838module_exit(nss_cleanup);
839
840MODULE_DESCRIPTION("QCA NSS Driver");
841MODULE_AUTHOR("Qualcomm Atheros Inc");
842MODULE_LICENSE("Dual BSD/GPL");