2 * Example events provider
4 * Copyright (C) ARM Limited 2010-2013. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Similar entries to those below must be present in the events.xml file.
11 * To add them to the events.xml, create an events-mmap.xml with the
12 * following contents and rebuild gatord:
14 * <counter_set name="mmapped_cnt" count="3"/>
15 * <category name="mmapped" counter_set="mmapped_cnt" per_cpu="no">
16 * <event event="0x0" title="Simulated1" name="Sine" display="maximum" average_selection="yes" description="Sort-of-sine"/>
17 * <event event="0x1" title="Simulated2" name="Triangle" display="maximum" average_selection="yes" description="Triangular wave"/>
18 * <event event="0x2" title="Simulated3" name="PWM" display="maximum" average_selection="yes" description="PWM Signal"/>
21 * When adding custom events, be sure do the following
22 * - add any needed .c files to the gator driver Makefile
23 * - call gator_events_install in the events init function
24 * - add the init function to GATOR_EVENTS_LIST in gator_main.c
25 * - add a new events-*.xml file to the gator daemon and rebuild
28 #include <linux/init.h>
30 #include <linux/ratelimit.h>
34 #define MMAPPED_COUNTERS_NUM 3
36 static int mmapped_global_enabled;
39 unsigned long enabled;
42 } mmapped_counters[MMAPPED_COUNTERS_NUM];
44 static int mmapped_buffer[MMAPPED_COUNTERS_NUM * 2];
48 /* Adds mmapped_cntX directories and enabled, event, and key files to /dev/gator/events */
49 static int gator_events_mmapped_create_files(struct super_block *sb,
54 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
58 snprintf(buf, sizeof(buf), "mmapped_cnt%d", i);
59 dir = gatorfs_mkdir(sb, root, buf);
62 gatorfs_create_ulong(sb, dir, "enabled",
63 &mmapped_counters[i].enabled);
64 gatorfs_create_ulong(sb, dir, "event",
65 &mmapped_counters[i].event);
66 gatorfs_create_ro_ulong(sb, dir, "key",
67 &mmapped_counters[i].key);
73 static int gator_events_mmapped_start(void)
79 prev_time = timespec_to_ns(&ts);
81 mmapped_global_enabled = 0;
82 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
83 if (mmapped_counters[i].enabled) {
84 mmapped_global_enabled = 1;
92 static void gator_events_mmapped_stop(void)
96 /* This function "simulates" counters, generating values of fancy
97 * functions like sine or triangle... */
98 static int mmapped_simulate(int counter, int delta_in_us)
103 case 0: /* sort-of-sine */
112 if (t % 1024000 < 512000)
113 x = 512000 - (t % 512000);
117 result = 32 * x / 512000;
118 result = result * result;
121 result = 1922 - result;
124 case 1: /* triangle */
128 v = v + d * delta_in_us;
132 } else if (v > 1000000) {
140 case 2: /* PWM signal */
142 static int dc, x, t = 0;
147 if (x / 1000000 != (x + delta_in_us) / 1000000)
148 dc = (dc + 100000) % 1000000;
151 result = t < dc ? 0 : 10;
159 static int gator_events_mmapped_read(int **buffer)
167 /* System wide counters - read from one core only */
168 if (!on_primary_core() || !mmapped_global_enabled)
172 time = timespec_to_ns(&ts);
173 delta_in_us = (int)(time - prev_time) / 1000;
176 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
177 if (mmapped_counters[i].enabled) {
178 mmapped_buffer[len++] = mmapped_counters[i].key;
179 mmapped_buffer[len++] =
180 mmapped_simulate(mmapped_counters[i].event,
186 *buffer = mmapped_buffer;
191 static struct gator_interface gator_events_mmapped_interface = {
192 .create_files = gator_events_mmapped_create_files,
193 .start = gator_events_mmapped_start,
194 .stop = gator_events_mmapped_stop,
195 .read = gator_events_mmapped_read,
198 /* Must not be static! */
199 int __init gator_events_mmapped_init(void)
203 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
204 mmapped_counters[i].enabled = 0;
205 mmapped_counters[i].key = gator_events_get_key();
208 return gator_events_install(&gator_events_mmapped_interface);