blob: dfe399c6aa588d9fc611739c50e25e6d6e6f088e [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"
Abhishek Rastogi9da47472014-03-18 19:46:15 +053024#include "nss_tx_rx_common.h"
Pamidipati, Vijay7f413b52013-09-24 19:07:12 +053025
Abhishek Rastogibc74e432013-04-02 10:28:22 +053026#include <nss_hal.h>
Thomas Wufb6a6842013-10-23 13:14:27 -070027#include <nss_clocks.h>
Abhishek Rastogibc74e432013-04-02 10:28:22 +053028
29#include <linux/module.h>
30#include <linux/platform_device.h>
wthomas442c7972013-08-05 14:28:17 -070031#include <linux/proc_fs.h>
32#include <linux/device.h>
Abhishek Rastogibc74e432013-04-02 10:28:22 +053033#include <mach/msm_nss.h>
34
wthomas442c7972013-08-05 14:28:17 -070035#include <linux/sysctl.h>
36#include <linux/regulator/consumer.h>
Thomas Wufb6a6842013-10-23 13:14:27 -070037#include <linux/clk.h>
Thomas Wu49008962014-04-02 14:50:47 -070038#include <linux/firmware.h>
39
40/*
41 * Macros
42 */
43#define MIN_IMG_SIZE 64*1024
44#define NETAP0_IMAGE "qcom-nss0.bin"
45#define NETAP1_IMAGE "qcom-nss1.bin"
wthomas442c7972013-08-05 14:28:17 -070046
Abhishek Rastogibc74e432013-04-02 10:28:22 +053047/*
48 * Global declarations
49 */
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +053050int nss_ctl_redirect __read_mostly = 0;
Thomas Wu68250352014-04-02 18:59:40 -070051int nss_ctl_debug __read_mostly = 1;
Abhishek Rastogibc74e432013-04-02 10:28:22 +053052
53/*
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +053054 * PM client handle
55 */
56static void *pm_client;
57
58/*
wthomas626147f2013-09-18 13:12:40 -070059 * Handler to send NSS messages
60 */
61void *nss_freq_change_context;
Thomas Wufb6a6842013-10-23 13:14:27 -070062struct clk *nss_core0_clk;
wthomas626147f2013-09-18 13:12:40 -070063
64/*
Abhishek Rastogibc74e432013-04-02 10:28:22 +053065 * Top level nss context structure
66 */
67struct nss_top_instance nss_top_main;
wthomas442c7972013-08-05 14:28:17 -070068struct nss_cmd_buffer nss_cmd_buf;
wthomas626147f2013-09-18 13:12:40 -070069struct nss_runtime_sampling nss_runtime_samples;
70struct workqueue_struct *nss_wq;
71
72/*
73 * Work Queue to handle messages to Kernel
74 */
75nss_work_t *nss_work;
Abhishek Rastogibc74e432013-04-02 10:28:22 +053076
77/*
78 * File local/Static variables/functions
79 */
80
Abhishek Rastogi271eee72013-07-29 21:08:36 +053081static const struct net_device_ops nss_netdev_ops;
82static const struct ethtool_ops nss_ethtool_ops;
83
84/*
85 * nss_dummy_netdev_setup()
86 * Dummy setup for net_device handler
87 */
88static void nss_dummy_netdev_setup(struct net_device *ndev)
89{
wthomas626147f2013-09-18 13:12:40 -070090
Abhishek Rastogi271eee72013-07-29 21:08:36 +053091}
92
Abhishek Rastogibc74e432013-04-02 10:28:22 +053093/*
94 * nss_handle_irq()
95 * HLOS interrupt handler for nss interrupts
96 */
97static irqreturn_t nss_handle_irq (int irq, void *ctx)
98{
99 struct int_ctx_instance *int_ctx = (struct int_ctx_instance *) ctx;
Abhishek Rastogi80f4eb12013-09-24 14:31:21 +0530100 struct nss_ctx_instance *nss_ctx = int_ctx->nss_ctx;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530101
102 /*
Abhishek Rastogi80f4eb12013-09-24 14:31:21 +0530103 * Mask interrupt until our bottom half re-enables it
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530104 */
Abhishek Rastogi80f4eb12013-09-24 14:31:21 +0530105 nss_hal_disable_interrupt(nss_ctx->nmap, int_ctx->irq,
106 int_ctx->shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530107
108 /*
109 * Schedule tasklet to process interrupt cause
110 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530111 napi_schedule(&int_ctx->napi);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530112 return IRQ_HANDLED;
113}
114
115/*
116 * nss_probe()
117 * HLOS device probe callback
118 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530119static int __devinit nss_probe(struct platform_device *nss_dev)
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530120{
121 struct nss_top_instance *nss_top = &nss_top_main;
122 struct nss_ctx_instance *nss_ctx = &nss_top->nss[nss_dev->id];
123 struct nss_platform_data *npd = (struct nss_platform_data *) nss_dev->dev.platform_data;
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530124 struct netdev_priv_instance *ndev_priv;
125 int i, err = 0;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530126
Thomas Wu49008962014-04-02 14:50:47 -0700127 const struct firmware *nss_fw = NULL;
128 int rc = -ENODEV;
129 void __iomem *load_mem;
130
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530131 nss_ctx->nss_top = nss_top;
132 nss_ctx->id = nss_dev->id;
133
wthomas442c7972013-08-05 14:28:17 -0700134 nss_info("%p: NSS_DEV_ID %s \n", nss_ctx, dev_name(&nss_dev->dev));
135
Thomas Wu49008962014-04-02 14:50:47 -0700136 /*
137 * F/W load from NSS Driver
138 */
139 if (nss_dev->id == 0) {
140 rc = request_firmware(&nss_fw, NETAP0_IMAGE, &(nss_dev->dev));
141 } else if (nss_dev->id == 1) {
142 rc = request_firmware(&nss_fw, NETAP1_IMAGE, &(nss_dev->dev));
143 } else {
144 nss_warning("%p: Invalid nss context \n", nss_ctx);
145 }
146
147 /*
148 * Check if the file read is successful
149 */
150 if (rc) {
151 nss_warning("%p: request_firmware failed with err code: %d", nss_ctx, rc);
152 err = rc;
153 goto err_init_0;
154 } else {
155 if (nss_fw->size < MIN_IMG_SIZE) {
156 nss_warning("%p: nss firmware is deprecated, size:%d", nss_ctx, nss_fw->size);
157 }
158 load_mem = ioremap_nocache(npd->load_addr, nss_fw->size);
159
160 if (load_mem == NULL) {
161 nss_warning("%p: ioremap_nocache failed: %x", nss_ctx, npd->load_addr);
162 release_firmware(nss_fw);
163 goto err_init_0;
164 } else {
165 memcpy_toio(load_mem, nss_fw->data, nss_fw->size);
166 release_firmware(nss_fw);
167 iounmap(load_mem);
168 printk("nss_driver - fw of size %d bytes copied to load addr: %x", nss_fw->size, npd->load_addr);
169 }
170 }
171
wthomas442c7972013-08-05 14:28:17 -0700172 /*
173 * Both NSS cores controlled by same regulator, Hook only Once
174 */
175 if (!nss_dev->id) {
Thomas Wufb6a6842013-10-23 13:14:27 -0700176 nss_core0_clk = clk_get(&nss_dev->dev, "nss_core_clk");
177 if (IS_ERR(nss_core0_clk)) {
wthomas442c7972013-08-05 14:28:17 -0700178
Thomas Wufb6a6842013-10-23 13:14:27 -0700179 err = PTR_ERR(nss_core0_clk);
wthomas442c7972013-08-05 14:28:17 -0700180 nss_info("%p: Regulator %s get failed, err=%d\n", nss_ctx, dev_name(&nss_dev->dev), err);
181 return err;
182
wthomas442c7972013-08-05 14:28:17 -0700183 }
Thomas Wufb6a6842013-10-23 13:14:27 -0700184 clk_set_rate(nss_core0_clk, NSS_FREQ_550);
185 clk_prepare(nss_core0_clk);
186 clk_enable(nss_core0_clk);
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800187
188 /*
189 * Check if turbo is supported
190 */
191 if (npd->turbo_frequency) {
192 /*
193 * Turbo is supported
194 */
195 printk("nss_driver - Turbo Support %d\n", npd->turbo_frequency);
196 nss_runtime_samples.freq_scale_sup_max = NSS_MAX_CPU_SCALES;
Pamidipati, Vijayf9b5a272014-01-22 14:24:10 +0530197 nss_pm_set_turbo();
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800198 } else {
199 printk("nss_driver - Turbo No Support %d\n", npd->turbo_frequency);
200 nss_runtime_samples.freq_scale_sup_max = NSS_MAX_CPU_SCALES - 1;
201 }
wthomas442c7972013-08-05 14:28:17 -0700202 }
203
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530204 /*
Sakthi Vignesh Radhakrishnand923e342013-12-09 11:53:03 -0800205 * Get load address of NSS firmware
206 */
207 nss_info("%p: Setting NSS%d Firmware load address to %x\n", nss_ctx, nss_dev->id, npd->load_addr);
208 nss_top->nss[nss_dev->id].load = npd->load_addr;
209
210 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530211 * Get virtual and physical memory addresses for nss logical/hardware address maps
212 */
213
214 /*
215 * Virtual address of CSM space
216 */
217 nss_ctx->nmap = npd->nmap;
218 nss_assert(nss_ctx->nmap);
219
220 /*
221 * Physical address of CSM space
222 */
223 nss_ctx->nphys = npd->nphys;
224 nss_assert(nss_ctx->nphys);
225
226 /*
227 * Virtual address of logical registers space
228 */
229 nss_ctx->vmap = npd->vmap;
230 nss_assert(nss_ctx->vmap);
231
232 /*
233 * Physical address of logical registers space
234 */
235 nss_ctx->vphys = npd->vphys;
236 nss_assert(nss_ctx->vphys);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530237 nss_info("%d:ctx=%p, vphys=%x, vmap=%x, nphys=%x, nmap=%x",
238 nss_dev->id, nss_ctx, nss_ctx->vphys, nss_ctx->vmap, nss_ctx->nphys, nss_ctx->nmap);
239
240 /*
241 * Register netdevice handlers
242 */
243 nss_ctx->int_ctx[0].ndev = alloc_netdev(sizeof(struct netdev_priv_instance),
244 "qca-nss-dev%d", nss_dummy_netdev_setup);
245 if (nss_ctx->int_ctx[0].ndev == NULL) {
246 nss_warning("%p: Could not allocate net_device #0", nss_ctx);
247 err = -ENOMEM;
248 goto err_init_0;
249 }
250
251 nss_ctx->int_ctx[0].ndev->netdev_ops = &nss_netdev_ops;
252 nss_ctx->int_ctx[0].ndev->ethtool_ops = &nss_ethtool_ops;
253 err = register_netdev(nss_ctx->int_ctx[0].ndev);
254 if (err) {
255 nss_warning("%p: Could not register net_device #0", nss_ctx);
256 goto err_init_1;
257 }
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530258
259 /*
260 * request for IRQs
261 *
262 * WARNING: CPU affinities should be set using OS supported methods
263 */
264 nss_ctx->int_ctx[0].nss_ctx = nss_ctx;
265 nss_ctx->int_ctx[0].shift_factor = 0;
266 nss_ctx->int_ctx[0].irq = npd->irq[0];
267 err = request_irq(npd->irq[0], nss_handle_irq, IRQF_DISABLED, "nss", &nss_ctx->int_ctx[0]);
268 if (err) {
269 nss_warning("%d: IRQ0 request failed", nss_dev->id);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530270 goto err_init_2;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530271 }
272
273 /*
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530274 * Register NAPI for NSS core interrupt #0
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530275 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530276 ndev_priv = netdev_priv(nss_ctx->int_ctx[0].ndev);
277 ndev_priv->int_ctx = &nss_ctx->int_ctx[0];
278 netif_napi_add(nss_ctx->int_ctx[0].ndev, &nss_ctx->int_ctx[0].napi, nss_core_handle_napi, 64);
279 napi_enable(&nss_ctx->int_ctx[0].napi);
280 nss_ctx->int_ctx[0].napi_active = true;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530281
282 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530283 * Check if second interrupt is supported on this nss core
284 */
285 if (npd->num_irq > 1) {
286 nss_info("%d: This NSS core supports two interrupts", nss_dev->id);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530287
288 /*
289 * Register netdevice handlers
290 */
291 nss_ctx->int_ctx[1].ndev = alloc_netdev(sizeof(struct netdev_priv_instance),
292 "qca-nss-dev%d", nss_dummy_netdev_setup);
293 if (nss_ctx->int_ctx[1].ndev == NULL) {
294 nss_warning("%p: Could not allocate net_device #1", nss_ctx);
295 err = -ENOMEM;
296 goto err_init_3;
297 }
298
299 nss_ctx->int_ctx[1].ndev->netdev_ops = &nss_netdev_ops;
300 nss_ctx->int_ctx[1].ndev->ethtool_ops = &nss_ethtool_ops;
301 err = register_netdev(nss_ctx->int_ctx[1].ndev);
302 if (err) {
303 nss_warning("%p: Could not register net_device #1", nss_ctx);
304 goto err_init_4;
305 }
306
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530307 nss_ctx->int_ctx[1].nss_ctx = nss_ctx;
308 nss_ctx->int_ctx[1].shift_factor = 15;
309 nss_ctx->int_ctx[1].irq = npd->irq[1];
310 err = request_irq(npd->irq[1], nss_handle_irq, IRQF_DISABLED, "nss", &nss_ctx->int_ctx[1]);
311 if (err) {
312 nss_warning("%d: IRQ1 request failed for nss", nss_dev->id);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530313 goto err_init_5;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530314 }
315
316 /*
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530317 * Register NAPI for NSS core interrupt #1
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530318 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530319 ndev_priv = netdev_priv(nss_ctx->int_ctx[1].ndev);
320 ndev_priv->int_ctx = &nss_ctx->int_ctx[1];
321 netif_napi_add(nss_ctx->int_ctx[1].ndev, &nss_ctx->int_ctx[1].napi, nss_core_handle_napi, 64);
322 napi_enable(&nss_ctx->int_ctx[1].napi);
323 nss_ctx->int_ctx[1].napi_active = true;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530324 }
325
326 spin_lock_bh(&(nss_top->lock));
327
328 /*
329 * Check functionalities are supported by this NSS core
330 */
Murat Sezgin7a705422014-01-30 16:09:22 -0800331 if (npd->shaping_enabled == NSS_FEATURE_ENABLED) {
332 nss_top->shaping_handler_id = nss_dev->id;
333 printk(KERN_INFO "%p: NSS Shaping is enabled, handler id: %u", __func__, nss_top->shaping_handler_id);
334 }
335
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530336 if (npd->ipv4_enabled == NSS_FEATURE_ENABLED) {
337 nss_top->ipv4_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530338 nss_ipv4_register_handler();
339 nss_pppoe_register_handler();
Abhishek Rastogi84d95d02014-03-26 19:31:31 +0530340 nss_eth_rx_register_handler();
341 nss_n2h_register_handler();
Sol Kavy2783c072014-04-05 12:53:13 -0700342 nss_virt_if_register_handler();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530343 }
344
345 if (npd->ipv6_enabled == NSS_FEATURE_ENABLED) {
346 nss_top->ipv6_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530347 nss_ipv6_register_handler();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530348 }
349
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530350 if (npd->crypto_enabled == NSS_FEATURE_ENABLED) {
351 nss_top->crypto_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530352 nss_crypto_register_handler();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530353 }
354
355 if (npd->ipsec_enabled == NSS_FEATURE_ENABLED) {
356 nss_top->ipsec_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530357 nss_ipsec_register_handler();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530358 }
359
360 if (npd->wlan_enabled == NSS_FEATURE_ENABLED) {
361 nss_top->wlan_handler_id = nss_dev->id;
362 }
363
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530364 if (npd->tun6rd_enabled == NSS_FEATURE_ENABLED) {
365 nss_top->tun6rd_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530366 nss_tun6rd_register_handler();
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530367 }
368
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530369 if (npd->tunipip6_enabled == NSS_FEATURE_ENABLED) {
370 nss_top->tunipip6_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530371 nss_tunipip6_register_handler();
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530372 }
373
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530374 if (npd->gmac_enabled[0] == NSS_FEATURE_ENABLED) {
375 nss_top->phys_if_handler_id[0] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530376 nss_phys_if_register_handler(0);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530377 }
378
379 if (npd->gmac_enabled[1] == NSS_FEATURE_ENABLED) {
380 nss_top->phys_if_handler_id[1] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530381 nss_phys_if_register_handler(1);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530382 }
383
384 if (npd->gmac_enabled[2] == NSS_FEATURE_ENABLED) {
385 nss_top->phys_if_handler_id[2] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530386 nss_phys_if_register_handler(2);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530387 }
388
389 if (npd->gmac_enabled[3] == NSS_FEATURE_ENABLED) {
390 nss_top->phys_if_handler_id[3] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530391 nss_phys_if_register_handler(3);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530392 }
393
Thomas Wu168ca262014-03-21 16:20:27 -0700394 nss_core_freq_register_handler();
395
wthomas626147f2013-09-18 13:12:40 -0700396 nss_top->frequency_handler_id = nss_dev->id;
397
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530398 spin_unlock_bh(&(nss_top->lock));
399
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530400 /*
401 * Initialize decongestion callbacks to NULL
402 */
403 for (i = 0; i< NSS_MAX_CLIENTS; i++) {
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530404 nss_ctx->queue_decongestion_callback[i] = 0;
405 nss_ctx->queue_decongestion_ctx[i] = 0;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530406 }
407
408 spin_lock_init(&(nss_ctx->decongest_cb_lock));
409 nss_ctx->magic = NSS_CTX_MAGIC;
410
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530411 nss_info("%p: Reseting NSS core %d now", nss_ctx, nss_ctx->id);
412
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530413 /*
414 * Enable clocks and bring NSS core out of reset
415 */
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530416 nss_hal_core_reset(nss_dev->id, nss_ctx->nmap, nss_ctx->load, nss_top->clk_src);
417
418 /*
419 * Enable interrupts for NSS core
420 */
421 nss_hal_enable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[0].irq,
422 nss_ctx->int_ctx[0].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
423
424 if (npd->num_irq > 1) {
425 nss_hal_enable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[1].irq,
426 nss_ctx->int_ctx[1].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
427 }
428
Radhakrishna Jigurub7346cf2013-12-16 13:08:43 +0530429 /*
430 * Initialize max buffer size for NSS core
431 */
432 nss_ctx->max_buf_size = NSS_NBUF_PAYLOAD_SIZE;
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530433 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 +0530434 goto err_init_0;
435
436err_init_5:
437 unregister_netdev(nss_ctx->int_ctx[1].ndev);
438err_init_4:
439 free_netdev(nss_ctx->int_ctx[1].ndev);
440err_init_3:
441 free_irq(npd->irq[0], &nss_ctx->int_ctx[0]);
442err_init_2:
443 unregister_netdev(nss_ctx->int_ctx[0].ndev);
444err_init_1:
445 free_netdev(nss_ctx->int_ctx[0].ndev);
446err_init_0:
447 return err;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530448}
449
450/*
451 * nss_remove()
452 * HLOS device remove callback
453 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530454static int __devexit nss_remove(struct platform_device *nss_dev)
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530455{
456 struct nss_top_instance *nss_top = &nss_top_main;
457 struct nss_ctx_instance *nss_ctx = &nss_top->nss[nss_dev->id];
458
459 /*
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530460 * Clean-up debugfs
461 */
462 nss_stats_clean();
463
464 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530465 * Disable interrupts and bottom halves in HLOS
466 * Disable interrupts from NSS to HLOS
467 */
468 nss_hal_disable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[0].irq,
469 nss_ctx->int_ctx[0].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530470
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530471 free_irq(nss_ctx->int_ctx[0].irq, &nss_ctx->int_ctx[0]);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530472 unregister_netdev(nss_ctx->int_ctx[0].ndev);
473 free_netdev(nss_ctx->int_ctx[0].ndev);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530474
475 /*
476 * Check if second interrupt is supported
477 * If so then clear resources for second interrupt as well
478 */
479 if (nss_ctx->int_ctx[1].irq) {
480 nss_hal_disable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[1].irq,
481 nss_ctx->int_ctx[1].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530482 free_irq(nss_ctx->int_ctx[1].irq, &nss_ctx->int_ctx[1]);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530483 unregister_netdev(nss_ctx->int_ctx[1].ndev);
484 free_netdev(nss_ctx->int_ctx[1].ndev);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530485 }
486
487 nss_info("%p: All resources freed for nss core%d", nss_ctx, nss_dev->id);
488 return 0;
489}
490
491/*
492 * nss_driver
493 * Platform driver structure for NSS
494 */
495struct platform_driver nss_driver = {
496 .probe = nss_probe,
497 .remove = __devexit_p(nss_remove),
498 .driver = {
499 .name = "qca-nss",
500 .owner = THIS_MODULE,
501 },
502};
503
504/*
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530505 * nss_reset_frequency_stats_samples()
506 * Reset all frequency sampling state when auto scaling is turned off.
507 */
508static void nss_reset_frequency_stats_samples (void)
509{
510 nss_runtime_samples.buffer_index = 0;
511 nss_runtime_samples.sum = 0;
512 nss_runtime_samples.average = 0;
513 nss_runtime_samples.sample_count = 0;
514 nss_runtime_samples.message_rate_limit = 0;
515 nss_runtime_samples.freq_scale_rate_limit_up = 0;
516 nss_runtime_samples.freq_scale_rate_limit_down = 0;
517}
518
519/*
wthomas626147f2013-09-18 13:12:40 -0700520 ***************************************************************************************************
Thomas Wufb6a6842013-10-23 13:14:27 -0700521 * nss_wq_function() is used to queue up requests to change NSS frequencies.
522 * The function will take care of NSS notices and also control clock.
523 * The auto rate algorithmn will queue up requests or the procfs may also queue up these requests.
wthomas626147f2013-09-18 13:12:40 -0700524 ***************************************************************************************************
525 */
526
527/*
528 * nss_wq_function()
529 * Added to Handle BH requests to kernel
530 */
531void nss_wq_function (struct work_struct *work)
532{
533 nss_work_t *my_work = (nss_work_t *)work;
534
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530535 nss_freq_change(nss_freq_change_context, my_work->frequency, my_work->stats_enable, 0);
Thomas Wufb6a6842013-10-23 13:14:27 -0700536 clk_set_rate(nss_core0_clk, my_work->frequency);
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530537 nss_freq_change(nss_freq_change_context, my_work->frequency, my_work->stats_enable, 1);
wthomas626147f2013-09-18 13:12:40 -0700538
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +0530539 if(!pm_client) {
540 goto out;
541 }
542
543 if (my_work->frequency == NSS_FREQ_733) {
544 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_TURBO);
545 } else if ((my_work->frequency == NSS_FREQ_275) || (my_work->frequency == NSS_FREQ_550)) {
546 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_NOMINAL);
547 } else {
548 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_IDLE);
549 }
550out:
wthomas626147f2013-09-18 13:12:40 -0700551 kfree((void *)work);
552}
553
554/*
wthomas442c7972013-08-05 14:28:17 -0700555 * nss_current_freq_handler()
556 * Handle Userspace Frequency Change Requests
557 */
558static int nss_current_freq_handler (ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
559{
wthomas442c7972013-08-05 14:28:17 -0700560 int ret;
wthomas626147f2013-09-18 13:12:40 -0700561
562 BUG_ON(!nss_wq);
wthomas442c7972013-08-05 14:28:17 -0700563
564 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
565
wthomasd39fa822013-08-22 16:44:23 -0700566 if (!write) {
wthomas626147f2013-09-18 13:12:40 -0700567 printk("Frequency Set to %d\n", nss_cmd_buf.current_freq);
wthomasd39fa822013-08-22 16:44:23 -0700568 return ret;
wthomas442c7972013-08-05 14:28:17 -0700569 }
wthomasd39fa822013-08-22 16:44:23 -0700570
wthomas626147f2013-09-18 13:12:40 -0700571 /* Turn off Auto Scale */
572 nss_cmd_buf.auto_scale = 0;
573 nss_runtime_samples.freq_scale_ready = 0;
wthomasd39fa822013-08-22 16:44:23 -0700574
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800575 /* If support NSS freq is in the table send the new frequency request to NSS or If No Turbo and ask for turbo freq */
576 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 -0700577 printk("Frequency not found. Please check Frequency Table\n");
wthomas626147f2013-09-18 13:12:40 -0700578 return ret;
wthomasd39fa822013-08-22 16:44:23 -0700579 }
580
wthomas626147f2013-09-18 13:12:40 -0700581 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
582 if (!nss_work) {
583 nss_info("NSS Freq WQ kmalloc fail");
584 return ret;
585 }
586 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
587 nss_work->frequency = nss_cmd_buf.current_freq;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530588 nss_work->stats_enable = 0;
589
590 /* Ensure we start with a fresh set of samples later */
591 nss_reset_frequency_stats_samples();
592
wthomas626147f2013-09-18 13:12:40 -0700593 queue_work(nss_wq, (struct work_struct *)nss_work);
594
wthomas442c7972013-08-05 14:28:17 -0700595 return ret;
596}
597
598/*
599 * nss_auto_scale_handler()
600 * Enables or Disable Auto Scaling
601 */
602static int nss_auto_scale_handler (ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
603{
604 int ret;
605
606 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
607
wthomas626147f2013-09-18 13:12:40 -0700608 if (!write) {
609 return ret;
610 }
611
Thomas Wufb6a6842013-10-23 13:14:27 -0700612 if (nss_cmd_buf.auto_scale != 1) {
wthomas626147f2013-09-18 13:12:40 -0700613 /*
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530614 * Is auto scaling currently enabled? If so, send the command to
615 * disable stats reporting to NSS
wthomas626147f2013-09-18 13:12:40 -0700616 */
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530617 if (nss_runtime_samples.freq_scale_ready != 0) {
618 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
619 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
620 if (!nss_work) {
621 nss_info("NSS Freq WQ kmalloc fail");
622 return ret;
623 }
624 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
625 nss_work->frequency = nss_cmd_buf.current_freq;
626 nss_work->stats_enable = 0;
627 queue_work(nss_wq, (struct work_struct *)nss_work);
628 nss_runtime_samples.freq_scale_ready = 0;
629
630 /*
631 * The current samples would be stale later when scaling is
632 * enabled again, hence reset them
633 */
634 nss_reset_frequency_stats_samples();
635 }
Thomas Wufb6a6842013-10-23 13:14:27 -0700636 return ret;
wthomas626147f2013-09-18 13:12:40 -0700637 }
wthomas442c7972013-08-05 14:28:17 -0700638
Thomas Wufb6a6842013-10-23 13:14:27 -0700639 /*
640 * Auto Scaling is already being done
641 */
642 if (nss_runtime_samples.freq_scale_ready == 1) {
643 return ret;
644 }
645
646 /*
647 * Setup default values - Middle of Freq Scale Band
648 */
649 nss_runtime_samples.freq_scale_index = 1;
650 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
651
652 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
653 if (!nss_work) {
654 nss_info("NSS Freq WQ kmalloc fail");
655 return ret;
656 }
657 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
658 nss_work->frequency = nss_cmd_buf.current_freq;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530659 nss_work->stats_enable = 1;
Thomas Wufb6a6842013-10-23 13:14:27 -0700660 queue_work(nss_wq, (struct work_struct *)nss_work);
661
662 nss_runtime_samples.freq_scale_ready = 1;
663
wthomas442c7972013-08-05 14:28:17 -0700664 return ret;
665}
666
667/*
668 * nss_get_freq_table_handler()
669 * Display Support Freq and Ex how to Change.
670 */
Thomas Wu05495be2013-12-19 14:24:24 -0800671static 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 -0700672{
673 int ret;
674
675 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
676
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800677 if (nss_runtime_samples.freq_scale_sup_max != NSS_MAX_CPU_SCALES) {
678 printk("Frequency Supported - 110Mhz 275Mhz 550Mhz\n");
679 printk("Ex. To Change Frequency - echo 110000000 > current_freq \n");
680
681 return ret;
682 }
683
wthomas626147f2013-09-18 13:12:40 -0700684 printk("Frequency Supported - 110Mhz 275Mhz 550Mhz 733Mhz \n");
wthomas442c7972013-08-05 14:28:17 -0700685 printk("Ex. To Change Frequency - echo 110000000 > current_freq \n");
686
687 return ret;
688}
689
690/*
Thomas Wu05495be2013-12-19 14:24:24 -0800691 * nss_get_average_inst_handler()
692 * Display AVG Inst Per Ms.
693 */
694static int nss_get_average_inst_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
695{
696 int ret;
697
698 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
699
700 if (!ret && !write) {
701 printk("Current Inst Per Ms %x\n", nss_runtime_samples.average);
702 }
703
704 return ret;
705}
706
707/*
Abhishek Rastogi1626a902013-11-21 17:09:49 +0530708 * nss_debug_handler()
709 * Enable NSS debug output
710 */
711static int nss_debug_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
712{
713 int ret;
714
715 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
716 if (!ret) {
717 if ((write) && (nss_ctl_debug != 0)) {
718 printk("Enabling NSS SPI Debug\n");
719 nss_hal_debug_enable();
720 }
721 }
722
723 return ret;
724}
725
726/*
Thomas Wu52075f42014-02-06 16:32:42 -0800727 * nss_coredump_handler()
728 * Send Signal To Coredump NSS Cores
729 */
730static int nss_coredump_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
731{
732 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *) nss_freq_change_context;
733 int ret;
734
735 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
736 if (!ret) {
737 if ((write) && (nss_ctl_debug != 0)) {
738 printk("Coredumping to DDR\n");
739 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_COREDUMP_START);
740 }
741 }
742
743 return ret;
744}
745
746/*
wthomas442c7972013-08-05 14:28:17 -0700747 * sysctl-tuning infrastructure.
748 */
749static ctl_table nss_freq_table[] = {
750 {
751 .procname = "current_freq",
752 .data = &nss_cmd_buf.current_freq,
753 .maxlen = sizeof(int),
754 .mode = 0644,
755 .proc_handler = &nss_current_freq_handler,
756 },
757 {
758 .procname = "freq_table",
759 .data = &nss_cmd_buf.max_freq,
760 .maxlen = sizeof(int),
761 .mode = 0644,
762 .proc_handler = &nss_get_freq_table_handler,
763 },
764 {
765 .procname = "auto_scale",
766 .data = &nss_cmd_buf.auto_scale,
767 .maxlen = sizeof(int),
768 .mode = 0644,
769 .proc_handler = &nss_auto_scale_handler,
770 },
Thomas Wu05495be2013-12-19 14:24:24 -0800771 {
772 .procname = "inst_per_sec",
773 .data = &nss_cmd_buf.average_inst,
774 .maxlen = sizeof(int),
775 .mode = 0644,
776 .proc_handler = &nss_get_average_inst_handler,
777 },
wthomas442c7972013-08-05 14:28:17 -0700778 { }
779};
780
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530781static ctl_table nss_general_table[] = {
782 {
783 .procname = "redirect",
784 .data = &nss_ctl_redirect,
785 .maxlen = sizeof(int),
786 .mode = 0644,
787 .proc_handler = proc_dointvec,
788 },
Abhishek Rastogi1626a902013-11-21 17:09:49 +0530789 {
790 .procname = "debug",
791 .data = &nss_ctl_debug,
792 .maxlen = sizeof(int),
793 .mode = 0644,
794 .proc_handler = &nss_debug_handler,
795 },
Thomas Wu52075f42014-02-06 16:32:42 -0800796 {
797 .procname = "coredump",
798 .data = &nss_cmd_buf.coredump,
799 .maxlen = sizeof(int),
800 .mode = 0644,
801 .proc_handler = &nss_coredump_handler,
802 },
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530803 { }
804};
805
wthomas442c7972013-08-05 14:28:17 -0700806static ctl_table nss_clock_dir[] = {
807 {
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530808 .procname = "clock",
809 .mode = 0555,
810 .child = nss_freq_table,
811 },
812 {
813 .procname = "general",
814 .mode = 0555,
815 .child = nss_general_table,
wthomas442c7972013-08-05 14:28:17 -0700816 },
817 { }
818};
819
820static ctl_table nss_root_dir[] = {
821 {
822 .procname = "nss",
823 .mode = 0555,
824 .child = nss_clock_dir,
825 },
826 { }
827};
828
829static ctl_table nss_root[] = {
830 {
831 .procname = "dev",
832 .mode = 0555,
833 .child = nss_root_dir,
834 },
835 { }
836};
837
838static struct ctl_table_header *nss_dev_header;
839
840/*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530841 * nss_init()
842 * Registers nss driver
843 */
844static int __init nss_init(void)
845{
846 nss_info("Init NSS driver");
847
Thomas Wu68250352014-04-02 18:59:40 -0700848 nss_hal_debug_enable();
849
wthomas626147f2013-09-18 13:12:40 -0700850 nss_freq_change_context = nss_get_frequency_mgr();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530851 /*
852 * Perform clock init common to all NSS cores
853 */
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530854 nss_hal_common_reset(&(nss_top_main.clk_src));
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530855
856 /*
857 * Enable spin locks
858 */
859 spin_lock_init(&(nss_top_main.lock));
860 spin_lock_init(&(nss_top_main.stats_lock));
861
862 /*
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530863 * Enable NSS statistics
864 */
865 nss_stats_init();
866
867 /*
wthomas442c7972013-08-05 14:28:17 -0700868 * Register sysctl table.
869 */
870 nss_dev_header = register_sysctl_table(nss_root);
871
872 /*
wthomas626147f2013-09-18 13:12:40 -0700873 * Setup Runtime Sample values
874 */
875 nss_runtime_samples.freq_scale[0].frequency = NSS_FREQ_110;
wthomas626147f2013-09-18 13:12:40 -0700876 nss_runtime_samples.freq_scale[0].minimum = NSS_FREQ_110_MIN;
877 nss_runtime_samples.freq_scale[0].maximum = NSS_FREQ_110_MAX;
wthomas626147f2013-09-18 13:12:40 -0700878 nss_runtime_samples.freq_scale[1].frequency = NSS_FREQ_550;
wthomas626147f2013-09-18 13:12:40 -0700879 nss_runtime_samples.freq_scale[1].minimum = NSS_FREQ_550_MIN;
880 nss_runtime_samples.freq_scale[1].maximum = NSS_FREQ_550_MAX;
wthomas626147f2013-09-18 13:12:40 -0700881 nss_runtime_samples.freq_scale[2].frequency = NSS_FREQ_733;
wthomas626147f2013-09-18 13:12:40 -0700882 nss_runtime_samples.freq_scale[2].minimum = NSS_FREQ_733_MIN;
883 nss_runtime_samples.freq_scale[2].maximum = NSS_FREQ_733_MAX;
wthomas626147f2013-09-18 13:12:40 -0700884 nss_runtime_samples.freq_scale_index = 1;
885 nss_runtime_samples.freq_scale_ready = 0;
Thomas Wu9681f7e2013-11-06 13:12:57 -0800886 nss_runtime_samples.freq_scale_rate_limit_up = 0;
887 nss_runtime_samples.freq_scale_rate_limit_down = 0;
wthomas626147f2013-09-18 13:12:40 -0700888 nss_runtime_samples.buffer_index = 0;
889 nss_runtime_samples.sum = 0;
890 nss_runtime_samples.sample_count = 0;
891 nss_runtime_samples.average = 0;
892 nss_runtime_samples.message_rate_limit = 0;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530893 nss_runtime_samples.initialized = 0;
wthomas626147f2013-09-18 13:12:40 -0700894
Thomas Wu05495be2013-12-19 14:24:24 -0800895 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
896
wthomas626147f2013-09-18 13:12:40 -0700897 /*
898 * Initial Workqueue
899 */
900 nss_wq = create_workqueue("nss_freq_queue");
901
902 /*
Pamidipati, Vijay7f413b52013-09-24 19:07:12 +0530903 * Initialize NSS Bus PM module
904 */
905 nss_pm_init();
906
907 /*
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +0530908 * Register with Bus driver
909 */
910 pm_client = nss_pm_client_register(NSS_PM_CLIENT_NETAP);
911 if (!pm_client) {
912 nss_warning("Error registering with PM driver");
913 }
914
915 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530916 * Register platform_driver
917 */
918 return platform_driver_register(&nss_driver);
919}
920
921/*
922 * nss_cleanup()
923 * Unregisters nss driver
924 */
925static void __exit nss_cleanup(void)
926{
927 nss_info("Exit NSS driver");
wthomas442c7972013-08-05 14:28:17 -0700928
929 if (nss_dev_header)
930 unregister_sysctl_table(nss_dev_header);
931
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530932 platform_driver_unregister(&nss_driver);
933}
934
935module_init(nss_init);
936module_exit(nss_cleanup);
937
938MODULE_DESCRIPTION("QCA NSS Driver");
939MODULE_AUTHOR("Qualcomm Atheros Inc");
940MODULE_LICENSE("Dual BSD/GPL");