blob: f82092d519675af6655629b2a90a2d1b18c5055e [file] [log] [blame]
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001/*
2 **************************************************************************
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 **************************************************************************
15 */
16
17/*
18 * qsdk/qca/src/qca-nss-drv/profiler/profile.c
19 *
20 * Implementation for NetAP Profiler
21 */
22
23#include <linux/platform_device.h>
Murat Sezgin3441e772015-10-26 11:55:57 -070024#include <linux/of.h>
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -080025#include <linux/export.h>
26#include <linux/module.h>
27#include <linux/seq_file.h>
28#include <linux/proc_fs.h>
29#include <linux/mm.h>
30#include <linux/mmzone.h>
31#include <linux/fs.h>
32#include <linux/page-flags.h>
33#include <linux/sched.h>
34#include <asm/uaccess.h>
35#include <asm/page.h>
36#include <asm/thread_info.h>
Sundarajan Srinivasand09d7dd2014-12-10 16:24:21 -080037#include <nss_api_if.h>
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -080038
39#include "profilenode.h"
40#include "profpkt.h"
41
42/*
43 * This is the driver for the NetAP Core profiler. The system interface to the driver is
44 * profile_register_performance_counter(), defined in <asm/profile.>
45 * a set of proc files (proc/profile/<*>), used by the profiler daemon
46 *
47 * communication between the profiler components is described in a set of header files.
48 * There are multiple versions of these files that must be kept synchronized:
49 * in nss/source/pkg/profile
50 * in tools/profiler
51 * in qsdk/qca/src/qca-nss-drv/profiler
52 *
53 * profilesample.h specifies the sample format used by pkg/profile, profile driver, and ip3kprof (two versions)
54 * profilenode.h specifies the driver node communication between NetAP and the profile driver. (two versions)
55 * profpkt.h specifies the network packet format between the profile driver, profile daemon, and ip3kprof (two versions)
56 *
57 *
58 * NSS profile sampler:
59 * pkg/profile/src/profile.c
60 * pkg/profile/include/profilenode.h
61 * pkg/profile/include/profilesample.h
62 *
63 * profile driver: this code
64 * qsdk/qca/src/qca-nss-drv/profiler
65 *
66 * profilerd: the user daemon that sends data to the tool
67 * qsdk/qca/feeds/qca/utils/profilerd
68 *
69 * ubicom32-prof: the Windows tool
70 * tools/profiler/src/(many files)
71 *
72 */
73
74#ifdef PROFILE_DEBUG
75#define profileDebug(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__)
76#define profileInfo(s, ...) pr_info("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__)
77#define profileWarn(s, ...) pr_warn("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__)
78#else
79#define profileDebug(s, ...)
80#define profileInfo(s, ...)
81#define profileWarn(s, ...)
82#endif
83
84static void profiler_handle_reply(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm);
85
86/*
87 * LINUX and Ultra counters must all fit in one packet
88 */
89#define PROFILE_LINUX_MAX_COUNTERS 40
90static int profile_num_counters = 0;
91static volatile unsigned int *profile_counter[PROFILE_LINUX_MAX_COUNTERS];
92static char profile_name[PROFILE_LINUX_MAX_COUNTERS][PROFILE_COUNTER_NAME_LENGTH];
93
94/*
95 * internal function to check if @name has been registered before
96 * return the found index, or -1 otherwise
97 */
98static int __profile_find_entry(char *name)
99{
100 int i;
101
102 for (i = 0; i < profile_num_counters; i++) {
103 if (!strncasecmp(name, profile_name[i], PROFILE_COUNTER_NAME_LENGTH)) {
104 return i;
105 }
106 }
107 return -1;
108}
109
110/*
111 * profile_register_performance_counter - register @counter into profile tracking list by key @name
112 * @counter: pointer of the counter variable
113 * @name: identifier of this counter
114 *
115 * Returns zero if total entries exceeding PROFILE_LINUX_MAX_COUNTERS
116 * non-zero otherwise.
117 *
118 * Each @name gives unique entry for @counter, by allocating a new array slot or just use existing one.
119 * No need of de-registration API, since a loadable module's new insmod, will replace the
120 * @counter's * new address at the same profile_counter[] slot.
121 */
122int profile_register_performance_counter(volatile unsigned int *counter, char *name)
123{
124 int i;
125
126 if (profile_num_counters >= PROFILE_LINUX_MAX_COUNTERS) {
127 return 0;
128 }
129
130 i = __profile_find_entry(name);
131 if (i < 0) {
132 i = profile_num_counters++;
133 }
134
135 profile_counter[i] = counter;
136 strncpy(profile_name[i], name, PROFILE_COUNTER_NAME_LENGTH);
137 profile_name[i][PROFILE_COUNTER_NAME_LENGTH - 1] = 0;
138
139 return 1;
140}
141
142/*
143 * make a packet full of sample data
144 */
145static int profile_make_data_packet(char *buf, int blen, struct profile_io *pn)
146{
147 int sp_samples = 0; /* separated samples if any */
148 int ns; /* number of samples requested */
149 struct profile_header ph;
150 struct profile_sample_ctrl_header *psc_hd = &pn->pnc.pn2h->psc_header;
151
152 if (blen < sizeof(ph) + sizeof(struct profile_sample)) {
153 return -EINVAL;
154 }
155
156 profileDebug("%p stat %x cnt %d %p\n", pn->pnc.pn2h, pn->pnc.pn2h->mh.md_type, psc_hd->count, pn->ccl);
157
158 if (pn->pnc.pn2h->mh.md_type == PINGPONG_EMPTY || psc_hd->count < 1) {
159 struct profile_n2h_sample_buf *nsb;
160 ns = (pn->ccl_read + 1) & (CCL_SIZE-1);
161 nsb = pn->ccl + ns;
162 if (ns == pn->ccl_write || nsb->mh.md_type != PINGPONG_FULL) {
163 profileInfo("%s: waiting more data %x %p : ns %d rd %d wr %d\n", __func__, nsb->mh.md_type, nsb, ns, pn->ccl_read, pn->ccl_write);
164 return -EAGAIN;
165 }
166 pn->ccl_read = ns;
167 profileInfo("sp %p => %p rd %d %p\n", pn->pnc.samples, nsb->samples, ns, nsb);
168 psc_hd = &nsb->psc_header;
169 pn->pnc.pn2h = nsb;
170 pn->pnc.samples = nsb->samples;
171 pn->pnc.cur = 0;
172 }
173 pn->pnc.pn2h->mh.md_type = PINGPONG_INUSE;
174
175 /*
176 * fill in the packet header
177 */
178 memset(&ph, 0, sizeof(ph));
179 ph.pph.magic = htons(PROF_MAGIC + PROFILE_VERSION);
180 ph.pph.header_size = sizeof(ph);
181 ph.pph.profile_instructions = 0;
182 ph.pph.clock_freq = pn->pnc.un.cpu_freq;
183 ph.pph.ddr_freq = pn->pnc.un.ddr_freq;
184 ph.pph.cpu_id = pn->pnc.un.cpu_id;
185 ph.pph.seq_num = htonl(pn->profile_sequence_num);
186 ph.pph.sample_stack_words = htonl(PROFILE_STACK_WORDS);
187
188 ns = (blen - sizeof(ph)) / sizeof(struct profile_sample);
189 profileInfo("%X: blen %d ns = %d psc_hd count %d ssets %d phs %d pss %d\n", pn->profile_sequence_num, blen, ns, psc_hd->count, psc_hd->exh.sample_sets, sizeof(ph), sizeof(struct profile_sample));
190 if (ns > psc_hd->count)
191 ns = psc_hd->count;
192 if (ns == 0) {
193 printk("NS should not be 0: rlen %d hd cnt %d\n", blen, psc_hd->count);
194 return 0;
195 }
196
197 /*
198 * if buf cannot hold all samples, then samples must be separated by set.
199 */
200 if (ns < psc_hd->count) {
201 ph.exh.sets_map = psc_hd->exh.sets_map; /* save for separating sets */
202 do {
203 sp_samples += psc_hd->exh.sets_map & 0x0F;
204 psc_hd->exh.sets_map >>= 4; /* remove the last set */
205 psc_hd->exh.sample_sets--;
206 ph.exh.sample_sets++; /* save for restore later */
207 } while ((psc_hd->count - sp_samples) > ns);
208 ns = psc_hd->count - sp_samples;
209 }
210 ph.pph.sample_count = ns;
211 if (copy_to_user(buf, &ph.pph, sizeof(ph.pph)) != 0) {
212 return -EFAULT;
213 }
214 buf += sizeof(ph.pph);
215
216 /*
217 * ph.exh is unused dummy; and psc_hd->exh is used directly to avoid double mem copy
218 */
219 if (copy_to_user(buf, &psc_hd->exh, sizeof(psc_hd->exh)) != 0) {
220 return -EFAULT;
221 }
222 buf += sizeof(psc_hd->exh);
223
224 blen = ns * sizeof(struct profile_sample);
225 profileDebug("-profile_make_data_packet %p slen %d cur %d dcped %d + %d\n", pn->pnc.samples, blen, pn->pnc.cur, sizeof(ph.pph), sizeof(psc_hd->exh));
226 if (copy_to_user(buf, &pn->pnc.samples[pn->pnc.cur], blen) != 0) {
227 return -EFAULT;
228 }
229 pn->pnc.cur += ns;
230 psc_hd->count -= ns;
231 if (psc_hd->count < 1)
232 pn->pnc.pn2h->mh.md_type = PINGPONG_EMPTY;
233
234 /*
235 * restore left over sample counts; 0s for no one
236 */
237 if (sp_samples) {
238 profileDebug("%d sps %d %d: sets %d : %d map %x <> %x\n", psc_hd->count, ns, sp_samples, psc_hd->exh.sample_sets, ph.exh.sample_sets, psc_hd->exh.sets_map, ph.exh.sets_map);
239 psc_hd->exh.sample_sets = ph.exh.sample_sets;
240 psc_hd->exh.sets_map = ph.exh.sets_map;
241 }
242
243 pn->profile_sequence_num++;
244 blen += sizeof(ph);
245 profileDebug("+profile_make_data_packet %d phd len %d nsp %p rd %d cnt %d\n", blen, sizeof(ph), pn->pnc.pn2h, pn->ccl_read, psc_hd->count);
246 return blen;
247}
248
249/*
250 * This is no longer needed due to NetAP and Linux use different CPUs, and profile is NetAP only.
251 * All related code will be removed after corresponging code in visual tool is corrected; otherwise
252 * visual tool will mis-behave
253 */
254struct profile_counter profile_builtin_stats[] =
255{
256 {
257 "Free memory(KB)", 0
258 },
259 {
260 "Max free Block(KB)", 0
261 }
262};
263
264/*
265 * make a packet full of performance counters
266 */
267static int profile_make_stats_packet(char *buf, int bytes, struct profile_io *pn)
268{
269 static char prof_pkt[PROFILE_MAX_PACKET_SIZE];
270
271 char *ptr;
272 int n;
273 struct profile_counter *counter_ptr;
274 struct profile_header_counters *hdr = (struct profile_header_counters *)prof_pkt;
275 struct profile_sample_ctrl_header *psc_hd = &pn->pnc.pn2h->psc_header;
276
277 if (bytes > PROFILE_MAX_PACKET_SIZE) {
278 bytes = PROFILE_MAX_PACKET_SIZE;
279 }
280 n = sizeof(profile_builtin_stats) + (pn->pnc.un.num_counters + profile_num_counters) * sizeof(*counter_ptr);
281
282 if ((bytes - sizeof(hdr)) < n) {
283 profileWarn("room too small %d for cnts %d\n", bytes, n);
284 return 0;
285 }
286
287 hdr->magic = htons(PROF_MAGIC_COUNTERS);
288 hdr->ultra_count = htons(pn->pnc.un.num_counters);
289 hdr->linux_count = htonl(profile_num_counters + sizeof(profile_builtin_stats) / sizeof(*counter_ptr));
290 hdr->ultra_sample_time = psc_hd->exh.clocks;
291 hdr->linux_sample_time = psc_hd->exh.clocks; /* QSDK has no time func */
292
293 n = pn->pnc.un.num_counters; /* copy NSS counters */
294 n *= sizeof(pn->pnc.un.counters[0]);
295 ptr = (char*) (hdr + 1);
296 memcpy(ptr, (void *)(pn->pnc.un.counters), n);
297 ptr += n;
298
299 counter_ptr = (struct profile_counter *)ptr;
300 for (n = 0; n < profile_num_counters; ++n) {
301 counter_ptr->value = htonl(*profile_counter[n]);
302 strcpy(counter_ptr->name, profile_name[n]);
303 counter_ptr++;
304 }
305 ptr = (char*)counter_ptr;
306
307 /*
308 * built in statistics
309 profile_get_memory_stats(&total_free, &max_free);
310 */
311 profile_builtin_stats[0].value = 0;
312 profile_builtin_stats[1].value = 0;
313 memcpy(ptr, (void *)profile_builtin_stats, sizeof(profile_builtin_stats));
314 ptr += sizeof(profile_builtin_stats);
315
316 n = ptr - prof_pkt;
317 if (copy_to_user(buf, prof_pkt, n) != 0) {
318 return -EFAULT;
319 }
320 return n;
321}
322
323/*
324 * space for all memory blocks so we can hold locks for short time when walking tables
325 */
326static struct profile_io *node[NSS_MAX_CORES];
327
328static int profile_open(struct inode *inode, struct file *filp)
329{
330 int n;
331 struct profile_io *pn;
332
333 if (filp->private_data)
334 printk(KERN_WARNING "%s: %p\n", filp->f_dentry->d_iname, filp->private_data);
335
336 n = filp->f_dentry->d_iname[strlen(filp->f_dentry->d_iname) - 1] - '0';
337 if (n < 0 || n >= NSS_MAX_CORES)
338 n = 0;
339 pn = node[n];
340 if (!pn) {
341 return -ENOENT;
342 }
343
344 if (!pn->pnc.enabled && nss_get_state(pn->ctx) == NSS_STATE_INITIALIZED) {
345 nss_tx_status_t ret;
346 pn->pnc.enabled = 1;
347 pn->profile_first_packet = 1;
348 pn->pnc.un.hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_START_MSG;
349 ret = nss_profiler_if_tx_buf(pn->ctx, &pn->pnc.un, sizeof(pn->pnc.un), profiler_handle_reply);
350 profileInfo("%s: %d -- %p: ccl %p sp %p\n", __func__, ret, pn, pn->ccl, pn->pnc.samples);
351 filp->private_data = pn;
352 return 0;
353 }
354
355 profileWarn("profile ena %d nss stat %x\n", pn->pnc.enabled, nss_get_state(pn->ctx));
356 return -EBUSY;
357}
358
359/*
360 * return a udp packet ready to send to the profiler tool
361 * when there are no packets left to make, return 0
362 */
363static int profile_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
364{
365 int result = 0;
366 int slen = 0;
367 struct profile_io *pn = (struct profile_io *)filp->private_data;
368 if (!pn) {
369 return -ENOENT;
370 }
371
372 if (!pn->pnc.enabled) {
373 return -EPERM;
374 }
375 if (!pn->pnc.samples) {
376 return -ENOMEM;
377 }
378
379 if (pn->profile_first_packet) {
380 result = profile_make_stats_packet(buf, count, pn);
381 pn->profile_first_packet = 0;
382 profileInfo("%d profile_make_stats_packet %d\n", result, count);
383
384#ifdef PROFILE_SEP_STAT
385 /*
386 * currectly, stat and sample data are combined in one pkt for efficient;
387 * but this is harder to debug and required remote tool to handle
388 * packet in all-in-one method instead of individual handler.
389 */
390 return result;
391#endif
392 }
393
394 if (result > 0) {
395 buf += result;
396 count -= result;
397 slen = result;
398 }
399 result = profile_make_data_packet(buf, count, pn);
400 if (result == 0) {
401 pn->profile_first_packet = 1;
402 }
403 profileInfo("%d: profile_make_data_packet %d %d\n", result, count, slen);
404
405 profileInfo("%d: read\n", pn->pnc.enabled);
406 if (pn->pnc.enabled < 0) {
407 nss_tx_status_t ret;
408 pn->pnc.enabled = 1;
409 pn->pnc.un.hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_START_MSG;
410 ret = nss_profiler_if_tx_buf(pn->ctx, &pn->pnc.un, sizeof(pn->pnc.un), profiler_handle_reply);
411 profileWarn("%s: restart %d -- %p: ccl %p sp %p\n", __func__, ret, pn, pn->ccl, pn->pnc.samples);
412 }
413
414 return result + slen;
415}
416
417/*
418 * the close function paired with profiler_open
419 */
420static int profile_release(struct inode *inode, struct file *filp)
421{
422 struct profile_io *pn = (struct profile_io *)filp->private_data;
423 if (!pn) {
424 return -ENOENT;
425 }
426
427 if (pn->pnc.enabled) {
428 nss_tx_status_t ret;
429 pn->pnc.enabled = 0;
430 pn->pnc.un.hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_STOP_MSG;
431 ret = nss_profiler_if_tx_buf(pn->ctx, &pn->pnc.un, sizeof(pn->pnc.un), profiler_handle_reply);
432 profileInfo("%s: %p %d\n", __func__, pn, ret);
433 return 0;
434 }
435 profileWarn("%s: attempt closing non-open dev %p\n", __func__, pn);
436 pn->profile_first_packet = 1;
437 return -EBADF;
438}
439
440#define isspace(c) (c==' ' || c=='\t')
441
442static int parseDbgData(const char *buf, size_t count, struct debug_box *db)
443{
444 char *cp;
445 int n;
446
447 printk("%p: buf (%s) cnt %d\n", buf, buf, count);
448 if (sscanf(buf, "%x", (uint32_t *)&db->base_addr) != 1) {
449 printk("%s: cannot get base addr\n", __func__);
450 return -EINVAL;
451 }
452
453 cp = strchr(buf, ' ');
454 if (!cp) {
455noea: printk("%s: no enough arguments\n", __func__);
456 return -EFAULT;
457 }
458
459 while (isspace(*cp)) cp++;
460 if (!strncmp(cp, "mio", 3) || !strncmp(cp, "moveio", 6)) {
461 printk("%p: cp (%s)\n", cp, cp);
462 cp = strchr(cp, ' ');
463 if (!cp) {
464 goto noea;
465 }
466 db->opts |= DEBUG_OPT_MOVEIO;
467 }
468
469 while (isspace(*cp)) cp++;
470 printk("base addr %p -- %s\n", db->base_addr, cp);
471
472 if (!strncmp(cp, "read", 4)) {
473 cp = strchr(cp, ' ');
474 if (cp) {
475 while (isspace(*cp)) cp++;
476 sscanf(cp, "%x", &db->dlen);
477 }
478 return 0;
479 }
480
481 n = 0;
482 do {
483 while (isspace(*cp)) cp++;
484 if (sscanf(cp, "%x", db->data+n) != 1) {
485 printk("n %d : %s\n", n, cp);
486 break;
487 }
488 printk("write %x to off %x\n", db->data[n], n * sizeof(db->data[0]));
489 n++;
490 cp = strchr(cp, ' ');
491 } while (cp && n < MAX_DB_WR);
492 return n;
493}
494
495/*
496 * display memory content read from Phy addr
497 */
498static void debug_if_show(struct debug_box *db, int buf_len)
499{
500 int i;
501
502 for (i=0; i < db->dlen; i++) {
503 if ((i & 3) == 0)
504 printk("\n%p: ", db->base_addr + i);
505 printk("%9x", db->data[i]);
506 }
507 printk("\ndumped %d (extra 1) blen %d\n", db->dlen, buf_len);
508}
509
510/*
511 * show debug message we requested from NSS
512 */
513static void profiler_handle_debug_reply(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm)
514{
515 debug_if_show((struct debug_box*)&ncm[1], ncm->len);
516}
517
518/*
519 * a generic Krait <--> NSS debug interface
520 */
521static int debug_if(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
522{
523 int result;
524 struct debug_box *db;
525 struct profile_io *pio = node[0];
526
527 if (!pio) {
528 return -ENOENT;
529 }
530
531 if (!pio->pnc.enabled) {
532 return -EPERM;
533 }
534
535 db = (struct debug_box *) &pio->pnc;
536 db->dlen = db->opts = 0;
537 result = parseDbgData(buf, count, db);
538 if (result < 0) {
539 return result;
540 }
541
542 if (!result) {
543 db->hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_DEBUG_RD_MSG;
544 } else {
545 db->hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_DEBUG_WR_MSG;
546 db->dlen = result;
547 }
548 result = nss_profiler_if_tx_buf(pio->ctx, &pio->pnc.un, sizeof(pio->pnc.un), profiler_handle_debug_reply);
549 printk("dbg res %d dlen = %d opt %x\n", result, db->dlen, db->opts);
550 return count;
551}
552
553static const struct file_operations profile_fops = {
554 .open = profile_open,
555 .read = profile_read,
556 .release = profile_release,
557 .write = debug_if,
558};
559
560/*
561 * showing sample status on Linux console
562 */
563static int profile_rate_show(struct seq_file *m, void *v)
564{
565 struct profile_io *pn = node[0];
566 if (pn) {
567 struct profile_sample_ctrl_header *psc_hd = &pn->pnc.pn2h->psc_header;
568 seq_printf(m, "%d samples per second. %d ultra, %d linux virtual counters. %d dropped samples. %d queued of %d max sampels. %d sent packets.\n",
569 pn->pnc.un.rate, pn->pnc.un.num_counters, profile_num_counters, psc_hd->dropped_samples, psc_hd->count, psc_hd->max_samples, pn->profile_sequence_num);
570 } else {
571 seq_printf(m, "Profiler is not initialized.\n");
572 }
573 return 0;
574}
575
576static int profile_rate_open(struct inode *inode, struct file *filp)
577{
578 return single_open(filp, profile_rate_show, NULL);
579}
580
581static int profile_rate_write(struct file *filp, const char *buf, size_t len, loff_t *off)
582{
583 *off = 0;
584 return 0;
585}
586
587static const struct file_operations profile_rate_fops = {
588 .open = profile_rate_open,
589 .read = seq_read,
590 .llseek = seq_lseek,
591 .release = single_release,
592 .write = profile_rate_write,
593};
594
595/*
596 * hex dump for debug
597 */
598static void kxdump(void *buf, int len, const char *who)
599{
600 int *ip = (int*) buf;
601 int lns = len >> 5; /* 32-B each line */
602 if (lns > 4)
603 lns = 4;
604 printk("%p: kxdump %s: len %d\n", buf, who, len);
605 do {
606 printk("%x %x %x %x %x %x %x %x\n", ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]);
607 ip += 8;
608 } while (lns--);
609}
610
611/*
612 * check magic # and detect Endian.
613 * negtive return means failure.
614 * return 1 means need to ntoh swap.
615 */
616static int profiler_magic_verify(struct profile_sample_ctrl_header *psc_hd, int buf_len)
617{
618 int swap = 0;
619 if ((psc_hd->hd_magic & UBI32_PROFILE_HD_MMASK) != UBI32_PROFILE_HD_MAGIC) {
620 if ((psc_hd->hd_magic & UBI32_PROFILE_HD_MMASK_REV) != UBI32_PROFILE_HD_MAGIC_REV) {
621 kxdump(psc_hd, buf_len, "bad profile packet");
622 printk("bad profile packet %x : %d\n", psc_hd->hd_magic, buf_len);
623 return -1;
624 }
625 profileDebug("Profile data in different Endian type %x\n", psc_hd->hd_magic);
626 swap = 1;
627 psc_hd->hd_magic = ntohl(psc_hd->hd_magic);
628 }
629 return swap;
630}
631
632/*
633 * process profile sample data from NSS
634 */
635static void profile_handle_nss_data(void *arg, struct nss_profiler_msg *npm)
636{
637 int buf_len = npm->cm.len;
638 void *buf = &npm->payload;
639 struct profile_io *pn;
640 struct profile_n2h_sample_buf *nsb;
641 struct profile_sample_ctrl_header *psc_hd = (struct profile_sample_ctrl_header *)buf;
642 int ret, wr;
643 int swap = 0; /* only for header and info data, not samples */
644
645 if (buf_len < (sizeof(struct profile_session) - sizeof(struct profile_counter) * (PROFILE_MAX_APP_COUNTERS))) {
646 printk("profile data packet is too small to be useful %d\n", buf_len);
647 return;
648 }
649
650 swap = profiler_magic_verify(psc_hd, buf_len);
651 if (swap < 0) {
652 return;
653 }
654
655 pn = (struct profile_io *)arg;
656 profileInfo("%s: dlen %d swap %d cmd %x - %d\n", __func__, buf_len, swap, npm->cm.type, (pn->ccl_read - pn->ccl_write) & (CCL_SIZE-1));
657 //kxdump(buf, buf_len, "process profile packet");
658
659 if (npm->cm.type == NSS_PROFILER_FIXED_INFO_MSG) {
660 struct profile_session *pTx = (struct profile_session *)buf;
661 if (swap) {
662 pn->pnc.un.rate = ntohl(pTx->rate);
663 pn->pnc.un.cpu_id = ntohl(pTx->cpu_id);
664 pn->pnc.un.cpu_freq = ntohl(pTx->cpu_freq);
665 pn->pnc.un.ddr_freq = ntohl(pTx->ddr_freq);
666 pn->pnc.un.num_counters = ntohl(pTx->num_counters);
667 } else {
668 pn->pnc.un = *pTx;
669 }
670 memcpy(pn->pnc.un.counters, pTx->counters, pn->pnc.un.num_counters * sizeof(pn->pnc.un.counters[0]));
671 pn->profile_first_packet = 1;
672 return;
673 }
674
675 wr = (pn->ccl_write + 1) & (CCL_SIZE-1);
676 nsb = pn->ccl + wr;
677 swap = (pn->ccl_read - wr) & (CCL_SIZE-1); /* PROFILER_FLOWCTRL */
678 if (nsb->mh.md_type != PINGPONG_EMPTY || (swap && swap < 5)) {
679 if (pn->pnc.enabled > 0) {
680 pn->pnc.enabled = -1;
681 pn->pnc.un.hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_STOP_MSG;
682 ret = nss_profiler_if_tx_buf(pn->ctx, &pn->pnc.un, sizeof(pn->pnc.un), profiler_handle_reply);
683 profileWarn("%d temp stop sampling engine %d\n", swap, ret);
684 }
685 if (swap < 3) {
686 profileWarn("w%p.%d: %d no room for new profile samples r%p.%d\n", nsb, wr, swap, pn->ccl+pn->ccl_read, pn->ccl_read);
687 return; /* -EMSGSIZE */
688 }
689 }
690 pn->ccl_write = wr;
691
692 /*
693 * sampling data -- hdr NBO swap is done at NSS side via SWAPB.
694 */
695 memcpy(&nsb->psc_header, buf, buf_len); /* pn->pnc.pn2h->psc_header = *psc_hd; maybe faster, but take more memory */
696
697 nsb->mh.md_type = PINGPONG_FULL;
698 //kxdump((void*)(nsb->samples + 23), sizeof(*nsb->samples) << 1, "1st 2 samples");
699 if (!wr) {
700 /*
701 * should be UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_COUNTERS_MSG
702 * but FW is hard to change due to packge warehouse, so using
703 * STOP/START instead till PROFILER_COUNTERS_MSG done in FW
704 */
705 pn->pnc.un.hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_STOP_MSG;
706 ret = nss_profiler_if_tx_buf(pn->ctx, &pn->pnc.un, sizeof(pn->pnc.un), profiler_handle_reply);
707 if (ret == NSS_TX_FAILURE)
708 printk("STOP Cmd failed %d %d\n", ret, wr);
709 pn->pnc.un.hd_magic = UBI32_PROFILE_HD_MAGIC | NSS_PROFILER_START_MSG;
710 ret = nss_profiler_if_tx_buf(pn->ctx, &pn->pnc.un, sizeof(pn->pnc.un), profiler_handle_reply);
711 }
712 profileInfo("filled %p %p wr %d\n", nsb, nsb->samples, pn->ccl_write);
713}
714
715/*
716 * process N2H reply for message we sent to NSS -- currently no action
717 */
718static void profiler_handle_reply(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm)
719{
720 switch (ncm->response) {
721 default:
Sundarajan Srinivasand09d7dd2014-12-10 16:24:21 -0800722 profileWarn("%p: profiler had error response %d\n", nss_ctx, ncm->response);
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800723 /*
724 * fail through -- no plan to do anything yet
725 */
726 case NSS_CMN_RESPONSE_ACK:
727 return;
728 }
729}
730
731/*
732 * initialize basic profile data structure
733 */
734static void profile_init(struct profile_io *node)
735{
736 int n;
737
738 memset(&node->pnc, 0, sizeof(node->pnc));
739 node->ccl_read = 0;
740 node->ccl_write = -1;
741 node->pnc.pn2h = node->ccl;
742 node->pnc.samples = node->ccl->samples;
743
744 for (n = 0; n < CCL_SIZE; n++) {
745 node->ccl[n].mh.md_type = PINGPONG_EMPTY;
746 node->ccl[n].psc_header.count = 0;
747 }
748
749 /*
750 * sw_ksp is an array of pointers to struct thread_info, the current task executing for each linux virtual processor
751 node->sw_ksp_ptr = sw_ksp;
752 */
753 node->task_offset = offsetof(struct thread_info, task);
754 node->pid_offset = offsetof(struct task_struct, tgid);
755}
756
757static struct proc_dir_entry *pdir;
758
759/*
760 * init_module cannot call exit_MODULE, so use this wrapper
761 */
762void netap_profile_release_resource(void)
763{
764 if (pdir) {
765 remove_proc_entry("rate", pdir);
766 remove_proc_entry("data", pdir);
767 remove_proc_entry("data1", pdir);
768 }
769 kfree(node[0]->ccl);
770 kfree(node[0]);
771 node[0] = NULL;
772}
773
774/*
775 * kernel module entry
776 */
777int __init netap_profile_init_module(void)
778{
Murat Sezgin3441e772015-10-26 11:55:57 -0700779#ifdef CONFIG_OF
780 /*
781 * If the node is not compatible, don't do anything.
782 */
783 if (!of_find_node_by_name(NULL, "nss-common")) {
784 return 0;
785 }
786#endif
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800787 /*
788 * we need N nodes, not one node + N ctx, for N cores
789 */
790 node[0] = kmalloc(sizeof(*node[0]) * NSS_MAX_CORES, GFP_KERNEL);
791 if (!node[0]) {
792 printk(KERN_INFO "Profiler CTRL kmalloc failed.\n");
793 return -ENOMEM;
794 }
795
796 node[0]->ccl = kmalloc(sizeof(*node[0]->ccl) * CCL_SIZE * NSS_MAX_CORES, GFP_KERNEL);
797 if (!node[0]->ccl) {
798 printk(KERN_INFO "Profiler n2h_sample_buf kmalloc failed.\n");
799 kfree(node[0]);
800 node[0] = NULL;
801 return -ENOMEM;
802 }
803
804 /*
805 * connect to the file system
806 */
807 pdir = proc_mkdir("profile", NULL);
808 if (!pdir ||
809 !proc_create("data", 0, pdir, &profile_fops) ||
810 !proc_create("data1", 0, pdir, &profile_fops) ||
811 !proc_create("rate", 0, pdir, &profile_rate_fops)) {
812 netap_profile_release_resource();
813 return -ENOMEM;
814 }
815
816 profile_init(node[0]);
817
818 /*
819 * attatch the device callback to N2H channel for CPU 0
820 */
821 node[0]->ctx = nss_profiler_notify_register(NSS_CORE_0, profile_handle_nss_data, node[0]);
822#if NSS_MAX_CORES > 1
823 node[1] = node[0] + 1;
824 node[1]->ccl = node[0]->ccl + CCL_SIZE;
825
826 profile_init(node[1]);
827 node[1]->ctx = nss_profiler_notify_register(NSS_CORE_1, profile_handle_nss_data, node[1]);
828 profile_register_performance_counter(&node[1]->profile_sequence_num, "Profile1 DRV data packets");
829#endif
830
831 profile_register_performance_counter(&node[0]->profile_sequence_num, "Profile0 DRV data packets");
832 return 0;
833}
834
835/*
836 * kernel module exit
837 */
838void __exit netap_profile_exit_module(void)
839{
Murat Sezgin3441e772015-10-26 11:55:57 -0700840#ifdef CONFIG_OF
841 /*
842 * If the node is not compatible, don't do anything.
843 */
844 if (!of_find_node_by_name(NULL, "nss-common")) {
845 return;
846 }
847#endif
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800848 nss_profiler_notify_unregister(NSS_CORE_0);
849#if NSS_MAX_CORES > 1
850 nss_profiler_notify_unregister(NSS_CORE_1);
851#endif
852 netap_profile_release_resource();
853}
854
855module_init(netap_profile_init_module);
856module_exit(netap_profile_exit_module);
857
858MODULE_LICENSE("Dual BSD/GPL");