blob: 2d99f9a9a5b1922f3ce2ab2f8541f28084185fe2 [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;
Abhishek Rastogi1626a902013-11-21 17:09:49 +053051int nss_ctl_debug __read_mostly = 0;
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();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530342 }
343
344 if (npd->ipv6_enabled == NSS_FEATURE_ENABLED) {
345 nss_top->ipv6_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530346 nss_ipv6_register_handler();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530347 }
348
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530349 if (npd->crypto_enabled == NSS_FEATURE_ENABLED) {
350 nss_top->crypto_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530351 nss_crypto_register_handler();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530352 }
353
354 if (npd->ipsec_enabled == NSS_FEATURE_ENABLED) {
355 nss_top->ipsec_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530356 nss_ipsec_register_handler();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530357 }
358
359 if (npd->wlan_enabled == NSS_FEATURE_ENABLED) {
360 nss_top->wlan_handler_id = nss_dev->id;
361 }
362
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530363 if (npd->tun6rd_enabled == NSS_FEATURE_ENABLED) {
364 nss_top->tun6rd_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530365 nss_tun6rd_register_handler();
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530366 }
367
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530368 if (npd->tunipip6_enabled == NSS_FEATURE_ENABLED) {
369 nss_top->tunipip6_handler_id = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530370 nss_tunipip6_register_handler();
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530371 }
372
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530373 if (npd->gmac_enabled[0] == NSS_FEATURE_ENABLED) {
374 nss_top->phys_if_handler_id[0] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530375 nss_phys_if_register_handler(0);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530376 }
377
378 if (npd->gmac_enabled[1] == NSS_FEATURE_ENABLED) {
379 nss_top->phys_if_handler_id[1] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530380 nss_phys_if_register_handler(1);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530381 }
382
383 if (npd->gmac_enabled[2] == NSS_FEATURE_ENABLED) {
384 nss_top->phys_if_handler_id[2] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530385 nss_phys_if_register_handler(2);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530386 }
387
388 if (npd->gmac_enabled[3] == NSS_FEATURE_ENABLED) {
389 nss_top->phys_if_handler_id[3] = nss_dev->id;
Abhishek Rastogi9da47472014-03-18 19:46:15 +0530390 nss_phys_if_register_handler(3);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530391 }
392
Thomas Wu168ca262014-03-21 16:20:27 -0700393 nss_core_freq_register_handler();
394
wthomas626147f2013-09-18 13:12:40 -0700395 nss_top->frequency_handler_id = nss_dev->id;
396
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530397 spin_unlock_bh(&(nss_top->lock));
398
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530399 /*
400 * Initialize decongestion callbacks to NULL
401 */
402 for (i = 0; i< NSS_MAX_CLIENTS; i++) {
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530403 nss_ctx->queue_decongestion_callback[i] = 0;
404 nss_ctx->queue_decongestion_ctx[i] = 0;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530405 }
406
407 spin_lock_init(&(nss_ctx->decongest_cb_lock));
408 nss_ctx->magic = NSS_CTX_MAGIC;
409
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530410 nss_info("%p: Reseting NSS core %d now", nss_ctx, nss_ctx->id);
411
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530412 /*
413 * Enable clocks and bring NSS core out of reset
414 */
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530415 nss_hal_core_reset(nss_dev->id, nss_ctx->nmap, nss_ctx->load, nss_top->clk_src);
416
417 /*
418 * Enable interrupts for NSS core
419 */
420 nss_hal_enable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[0].irq,
421 nss_ctx->int_ctx[0].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
422
423 if (npd->num_irq > 1) {
424 nss_hal_enable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[1].irq,
425 nss_ctx->int_ctx[1].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
426 }
427
Radhakrishna Jigurub7346cf2013-12-16 13:08:43 +0530428 /*
429 * Initialize max buffer size for NSS core
430 */
431 nss_ctx->max_buf_size = NSS_NBUF_PAYLOAD_SIZE;
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530432 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 +0530433 goto err_init_0;
434
435err_init_5:
436 unregister_netdev(nss_ctx->int_ctx[1].ndev);
437err_init_4:
438 free_netdev(nss_ctx->int_ctx[1].ndev);
439err_init_3:
440 free_irq(npd->irq[0], &nss_ctx->int_ctx[0]);
441err_init_2:
442 unregister_netdev(nss_ctx->int_ctx[0].ndev);
443err_init_1:
444 free_netdev(nss_ctx->int_ctx[0].ndev);
445err_init_0:
446 return err;
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530447}
448
449/*
450 * nss_remove()
451 * HLOS device remove callback
452 */
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530453static int __devexit nss_remove(struct platform_device *nss_dev)
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530454{
455 struct nss_top_instance *nss_top = &nss_top_main;
456 struct nss_ctx_instance *nss_ctx = &nss_top->nss[nss_dev->id];
457
458 /*
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530459 * Clean-up debugfs
460 */
461 nss_stats_clean();
462
463 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530464 * Disable interrupts and bottom halves in HLOS
465 * Disable interrupts from NSS to HLOS
466 */
467 nss_hal_disable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[0].irq,
468 nss_ctx->int_ctx[0].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530469
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530470 free_irq(nss_ctx->int_ctx[0].irq, &nss_ctx->int_ctx[0]);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530471 unregister_netdev(nss_ctx->int_ctx[0].ndev);
472 free_netdev(nss_ctx->int_ctx[0].ndev);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530473
474 /*
475 * Check if second interrupt is supported
476 * If so then clear resources for second interrupt as well
477 */
478 if (nss_ctx->int_ctx[1].irq) {
479 nss_hal_disable_interrupt(nss_ctx->nmap, nss_ctx->int_ctx[1].irq,
480 nss_ctx->int_ctx[1].shift_factor, NSS_HAL_SUPPORTED_INTERRUPTS);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530481 free_irq(nss_ctx->int_ctx[1].irq, &nss_ctx->int_ctx[1]);
Abhishek Rastogi271eee72013-07-29 21:08:36 +0530482 unregister_netdev(nss_ctx->int_ctx[1].ndev);
483 free_netdev(nss_ctx->int_ctx[1].ndev);
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530484 }
485
486 nss_info("%p: All resources freed for nss core%d", nss_ctx, nss_dev->id);
487 return 0;
488}
489
490/*
491 * nss_driver
492 * Platform driver structure for NSS
493 */
494struct platform_driver nss_driver = {
495 .probe = nss_probe,
496 .remove = __devexit_p(nss_remove),
497 .driver = {
498 .name = "qca-nss",
499 .owner = THIS_MODULE,
500 },
501};
502
503/*
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530504 * nss_reset_frequency_stats_samples()
505 * Reset all frequency sampling state when auto scaling is turned off.
506 */
507static void nss_reset_frequency_stats_samples (void)
508{
509 nss_runtime_samples.buffer_index = 0;
510 nss_runtime_samples.sum = 0;
511 nss_runtime_samples.average = 0;
512 nss_runtime_samples.sample_count = 0;
513 nss_runtime_samples.message_rate_limit = 0;
514 nss_runtime_samples.freq_scale_rate_limit_up = 0;
515 nss_runtime_samples.freq_scale_rate_limit_down = 0;
516}
517
518/*
wthomas626147f2013-09-18 13:12:40 -0700519 ***************************************************************************************************
Thomas Wufb6a6842013-10-23 13:14:27 -0700520 * nss_wq_function() is used to queue up requests to change NSS frequencies.
521 * The function will take care of NSS notices and also control clock.
522 * The auto rate algorithmn will queue up requests or the procfs may also queue up these requests.
wthomas626147f2013-09-18 13:12:40 -0700523 ***************************************************************************************************
524 */
525
526/*
527 * nss_wq_function()
528 * Added to Handle BH requests to kernel
529 */
530void nss_wq_function (struct work_struct *work)
531{
532 nss_work_t *my_work = (nss_work_t *)work;
533
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530534 nss_freq_change(nss_freq_change_context, my_work->frequency, my_work->stats_enable, 0);
Thomas Wufb6a6842013-10-23 13:14:27 -0700535 clk_set_rate(nss_core0_clk, my_work->frequency);
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530536 nss_freq_change(nss_freq_change_context, my_work->frequency, my_work->stats_enable, 1);
wthomas626147f2013-09-18 13:12:40 -0700537
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +0530538 if(!pm_client) {
539 goto out;
540 }
541
542 if (my_work->frequency == NSS_FREQ_733) {
543 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_TURBO);
544 } else if ((my_work->frequency == NSS_FREQ_275) || (my_work->frequency == NSS_FREQ_550)) {
545 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_NOMINAL);
546 } else {
547 nss_pm_set_perf_level(pm_client, NSS_PM_PERF_LEVEL_IDLE);
548 }
549out:
wthomas626147f2013-09-18 13:12:40 -0700550 kfree((void *)work);
551}
552
553/*
wthomas442c7972013-08-05 14:28:17 -0700554 * nss_current_freq_handler()
555 * Handle Userspace Frequency Change Requests
556 */
557static int nss_current_freq_handler (ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
558{
wthomas442c7972013-08-05 14:28:17 -0700559 int ret;
wthomas626147f2013-09-18 13:12:40 -0700560
561 BUG_ON(!nss_wq);
wthomas442c7972013-08-05 14:28:17 -0700562
563 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
564
wthomasd39fa822013-08-22 16:44:23 -0700565 if (!write) {
wthomas626147f2013-09-18 13:12:40 -0700566 printk("Frequency Set to %d\n", nss_cmd_buf.current_freq);
wthomasd39fa822013-08-22 16:44:23 -0700567 return ret;
wthomas442c7972013-08-05 14:28:17 -0700568 }
wthomasd39fa822013-08-22 16:44:23 -0700569
wthomas626147f2013-09-18 13:12:40 -0700570 /* Turn off Auto Scale */
571 nss_cmd_buf.auto_scale = 0;
572 nss_runtime_samples.freq_scale_ready = 0;
wthomasd39fa822013-08-22 16:44:23 -0700573
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800574 /* If support NSS freq is in the table send the new frequency request to NSS or If No Turbo and ask for turbo freq */
575 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 -0700576 printk("Frequency not found. Please check Frequency Table\n");
wthomas626147f2013-09-18 13:12:40 -0700577 return ret;
wthomasd39fa822013-08-22 16:44:23 -0700578 }
579
wthomas626147f2013-09-18 13:12:40 -0700580 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
581 if (!nss_work) {
582 nss_info("NSS Freq WQ kmalloc fail");
583 return ret;
584 }
585 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
586 nss_work->frequency = nss_cmd_buf.current_freq;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530587 nss_work->stats_enable = 0;
588
589 /* Ensure we start with a fresh set of samples later */
590 nss_reset_frequency_stats_samples();
591
wthomas626147f2013-09-18 13:12:40 -0700592 queue_work(nss_wq, (struct work_struct *)nss_work);
593
wthomas442c7972013-08-05 14:28:17 -0700594 return ret;
595}
596
597/*
598 * nss_auto_scale_handler()
599 * Enables or Disable Auto Scaling
600 */
601static int nss_auto_scale_handler (ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
602{
603 int ret;
604
605 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
606
wthomas626147f2013-09-18 13:12:40 -0700607 if (!write) {
608 return ret;
609 }
610
Thomas Wufb6a6842013-10-23 13:14:27 -0700611 if (nss_cmd_buf.auto_scale != 1) {
wthomas626147f2013-09-18 13:12:40 -0700612 /*
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530613 * Is auto scaling currently enabled? If so, send the command to
614 * disable stats reporting to NSS
wthomas626147f2013-09-18 13:12:40 -0700615 */
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530616 if (nss_runtime_samples.freq_scale_ready != 0) {
617 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
618 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
619 if (!nss_work) {
620 nss_info("NSS Freq WQ kmalloc fail");
621 return ret;
622 }
623 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
624 nss_work->frequency = nss_cmd_buf.current_freq;
625 nss_work->stats_enable = 0;
626 queue_work(nss_wq, (struct work_struct *)nss_work);
627 nss_runtime_samples.freq_scale_ready = 0;
628
629 /*
630 * The current samples would be stale later when scaling is
631 * enabled again, hence reset them
632 */
633 nss_reset_frequency_stats_samples();
634 }
Thomas Wufb6a6842013-10-23 13:14:27 -0700635 return ret;
wthomas626147f2013-09-18 13:12:40 -0700636 }
wthomas442c7972013-08-05 14:28:17 -0700637
Thomas Wufb6a6842013-10-23 13:14:27 -0700638 /*
639 * Auto Scaling is already being done
640 */
641 if (nss_runtime_samples.freq_scale_ready == 1) {
642 return ret;
643 }
644
645 /*
646 * Setup default values - Middle of Freq Scale Band
647 */
648 nss_runtime_samples.freq_scale_index = 1;
649 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
650
651 nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
652 if (!nss_work) {
653 nss_info("NSS Freq WQ kmalloc fail");
654 return ret;
655 }
656 INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
657 nss_work->frequency = nss_cmd_buf.current_freq;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530658 nss_work->stats_enable = 1;
Thomas Wufb6a6842013-10-23 13:14:27 -0700659 queue_work(nss_wq, (struct work_struct *)nss_work);
660
661 nss_runtime_samples.freq_scale_ready = 1;
662
wthomas442c7972013-08-05 14:28:17 -0700663 return ret;
664}
665
666/*
667 * nss_get_freq_table_handler()
668 * Display Support Freq and Ex how to Change.
669 */
Thomas Wu05495be2013-12-19 14:24:24 -0800670static 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 -0700671{
672 int ret;
673
674 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
675
Thomas Wu0a0a9c92013-11-21 15:28:19 -0800676 if (nss_runtime_samples.freq_scale_sup_max != NSS_MAX_CPU_SCALES) {
677 printk("Frequency Supported - 110Mhz 275Mhz 550Mhz\n");
678 printk("Ex. To Change Frequency - echo 110000000 > current_freq \n");
679
680 return ret;
681 }
682
wthomas626147f2013-09-18 13:12:40 -0700683 printk("Frequency Supported - 110Mhz 275Mhz 550Mhz 733Mhz \n");
wthomas442c7972013-08-05 14:28:17 -0700684 printk("Ex. To Change Frequency - echo 110000000 > current_freq \n");
685
686 return ret;
687}
688
689/*
Thomas Wu05495be2013-12-19 14:24:24 -0800690 * nss_get_average_inst_handler()
691 * Display AVG Inst Per Ms.
692 */
693static int nss_get_average_inst_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
694{
695 int ret;
696
697 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
698
699 if (!ret && !write) {
700 printk("Current Inst Per Ms %x\n", nss_runtime_samples.average);
701 }
702
703 return ret;
704}
705
706/*
Abhishek Rastogi1626a902013-11-21 17:09:49 +0530707 * nss_debug_handler()
708 * Enable NSS debug output
709 */
710static int nss_debug_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
711{
712 int ret;
713
714 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
715 if (!ret) {
716 if ((write) && (nss_ctl_debug != 0)) {
717 printk("Enabling NSS SPI Debug\n");
718 nss_hal_debug_enable();
719 }
720 }
721
722 return ret;
723}
724
725/*
Thomas Wu52075f42014-02-06 16:32:42 -0800726 * nss_coredump_handler()
727 * Send Signal To Coredump NSS Cores
728 */
729static int nss_coredump_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
730{
731 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *) nss_freq_change_context;
732 int ret;
733
734 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
735 if (!ret) {
736 if ((write) && (nss_ctl_debug != 0)) {
737 printk("Coredumping to DDR\n");
738 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);
739 }
740 }
741
742 return ret;
743}
744
745/*
wthomas442c7972013-08-05 14:28:17 -0700746 * sysctl-tuning infrastructure.
747 */
748static ctl_table nss_freq_table[] = {
749 {
750 .procname = "current_freq",
751 .data = &nss_cmd_buf.current_freq,
752 .maxlen = sizeof(int),
753 .mode = 0644,
754 .proc_handler = &nss_current_freq_handler,
755 },
756 {
757 .procname = "freq_table",
758 .data = &nss_cmd_buf.max_freq,
759 .maxlen = sizeof(int),
760 .mode = 0644,
761 .proc_handler = &nss_get_freq_table_handler,
762 },
763 {
764 .procname = "auto_scale",
765 .data = &nss_cmd_buf.auto_scale,
766 .maxlen = sizeof(int),
767 .mode = 0644,
768 .proc_handler = &nss_auto_scale_handler,
769 },
Thomas Wu05495be2013-12-19 14:24:24 -0800770 {
771 .procname = "inst_per_sec",
772 .data = &nss_cmd_buf.average_inst,
773 .maxlen = sizeof(int),
774 .mode = 0644,
775 .proc_handler = &nss_get_average_inst_handler,
776 },
wthomas442c7972013-08-05 14:28:17 -0700777 { }
778};
779
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530780static ctl_table nss_general_table[] = {
781 {
782 .procname = "redirect",
783 .data = &nss_ctl_redirect,
784 .maxlen = sizeof(int),
785 .mode = 0644,
786 .proc_handler = proc_dointvec,
787 },
Abhishek Rastogi1626a902013-11-21 17:09:49 +0530788 {
789 .procname = "debug",
790 .data = &nss_ctl_debug,
791 .maxlen = sizeof(int),
792 .mode = 0644,
793 .proc_handler = &nss_debug_handler,
794 },
Thomas Wu52075f42014-02-06 16:32:42 -0800795 {
796 .procname = "coredump",
797 .data = &nss_cmd_buf.coredump,
798 .maxlen = sizeof(int),
799 .mode = 0644,
800 .proc_handler = &nss_coredump_handler,
801 },
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530802 { }
803};
804
wthomas442c7972013-08-05 14:28:17 -0700805static ctl_table nss_clock_dir[] = {
806 {
Abhishek Rastogi5cd2e4c2013-11-13 18:09:08 +0530807 .procname = "clock",
808 .mode = 0555,
809 .child = nss_freq_table,
810 },
811 {
812 .procname = "general",
813 .mode = 0555,
814 .child = nss_general_table,
wthomas442c7972013-08-05 14:28:17 -0700815 },
816 { }
817};
818
819static ctl_table nss_root_dir[] = {
820 {
821 .procname = "nss",
822 .mode = 0555,
823 .child = nss_clock_dir,
824 },
825 { }
826};
827
828static ctl_table nss_root[] = {
829 {
830 .procname = "dev",
831 .mode = 0555,
832 .child = nss_root_dir,
833 },
834 { }
835};
836
837static struct ctl_table_header *nss_dev_header;
838
839/*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530840 * nss_init()
841 * Registers nss driver
842 */
843static int __init nss_init(void)
844{
845 nss_info("Init NSS driver");
846
wthomas626147f2013-09-18 13:12:40 -0700847 nss_freq_change_context = nss_get_frequency_mgr();
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530848 /*
849 * Perform clock init common to all NSS cores
850 */
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530851 nss_hal_common_reset(&(nss_top_main.clk_src));
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530852
853 /*
854 * Enable spin locks
855 */
856 spin_lock_init(&(nss_top_main.lock));
857 spin_lock_init(&(nss_top_main.stats_lock));
858
859 /*
Abhishek Rastogi38cffff2013-06-02 11:25:47 +0530860 * Enable NSS statistics
861 */
862 nss_stats_init();
863
864 /*
wthomas442c7972013-08-05 14:28:17 -0700865 * Register sysctl table.
866 */
867 nss_dev_header = register_sysctl_table(nss_root);
868
869 /*
wthomas626147f2013-09-18 13:12:40 -0700870 * Setup Runtime Sample values
871 */
872 nss_runtime_samples.freq_scale[0].frequency = NSS_FREQ_110;
wthomas626147f2013-09-18 13:12:40 -0700873 nss_runtime_samples.freq_scale[0].minimum = NSS_FREQ_110_MIN;
874 nss_runtime_samples.freq_scale[0].maximum = NSS_FREQ_110_MAX;
wthomas626147f2013-09-18 13:12:40 -0700875 nss_runtime_samples.freq_scale[1].frequency = NSS_FREQ_550;
wthomas626147f2013-09-18 13:12:40 -0700876 nss_runtime_samples.freq_scale[1].minimum = NSS_FREQ_550_MIN;
877 nss_runtime_samples.freq_scale[1].maximum = NSS_FREQ_550_MAX;
wthomas626147f2013-09-18 13:12:40 -0700878 nss_runtime_samples.freq_scale[2].frequency = NSS_FREQ_733;
wthomas626147f2013-09-18 13:12:40 -0700879 nss_runtime_samples.freq_scale[2].minimum = NSS_FREQ_733_MIN;
880 nss_runtime_samples.freq_scale[2].maximum = NSS_FREQ_733_MAX;
wthomas626147f2013-09-18 13:12:40 -0700881 nss_runtime_samples.freq_scale_index = 1;
882 nss_runtime_samples.freq_scale_ready = 0;
Thomas Wu9681f7e2013-11-06 13:12:57 -0800883 nss_runtime_samples.freq_scale_rate_limit_up = 0;
884 nss_runtime_samples.freq_scale_rate_limit_down = 0;
wthomas626147f2013-09-18 13:12:40 -0700885 nss_runtime_samples.buffer_index = 0;
886 nss_runtime_samples.sum = 0;
887 nss_runtime_samples.sample_count = 0;
888 nss_runtime_samples.average = 0;
889 nss_runtime_samples.message_rate_limit = 0;
Kiran Kumar C.S.K69fd5992014-01-06 20:58:14 +0530890 nss_runtime_samples.initialized = 0;
wthomas626147f2013-09-18 13:12:40 -0700891
Thomas Wu05495be2013-12-19 14:24:24 -0800892 nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
893
wthomas626147f2013-09-18 13:12:40 -0700894 /*
895 * Initial Workqueue
896 */
897 nss_wq = create_workqueue("nss_freq_queue");
898
899 /*
Pamidipati, Vijay7f413b52013-09-24 19:07:12 +0530900 * Initialize NSS Bus PM module
901 */
902 nss_pm_init();
903
904 /*
Pamidipati, Vijay5d27d812013-11-22 16:48:11 +0530905 * Register with Bus driver
906 */
907 pm_client = nss_pm_client_register(NSS_PM_CLIENT_NETAP);
908 if (!pm_client) {
909 nss_warning("Error registering with PM driver");
910 }
911
912 /*
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530913 * Register platform_driver
914 */
915 return platform_driver_register(&nss_driver);
916}
917
918/*
919 * nss_cleanup()
920 * Unregisters nss driver
921 */
922static void __exit nss_cleanup(void)
923{
924 nss_info("Exit NSS driver");
wthomas442c7972013-08-05 14:28:17 -0700925
926 if (nss_dev_header)
927 unregister_sysctl_table(nss_dev_header);
928
Abhishek Rastogibc74e432013-04-02 10:28:22 +0530929 platform_driver_unregister(&nss_driver);
930}
931
932module_init(nss_init);
933module_exit(nss_cleanup);
934
935MODULE_DESCRIPTION("QCA NSS Driver");
936MODULE_AUTHOR("Qualcomm Atheros Inc");
937MODULE_LICENSE("Dual BSD/GPL");