mmc: dw_mmc: fix the max_blk_count in IDMAC
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_mmapped.c
1 /*
2  * Example events provider
3  *
4  * Copyright (C) ARM Limited 2010-2015. All rights reserved.
5  *
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.
9  *
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:
13  *
14  * <category name="mmapped">
15  *   <event counter="mmapped_cnt0" title="Simulated1" name="Sine" display="maximum" class="absolute" description="Sort-of-sine"/>
16  *   <event counter="mmapped_cnt1" title="Simulated2" name="Triangle" display="maximum" class="absolute" description="Triangular wave"/>
17  *   <event counter="mmapped_cnt2" title="Simulated3" name="PWM" display="maximum" class="absolute" description="PWM Signal"/>
18  * </category>
19  *
20  * When adding custom events, be sure to do the following:
21  * - add any needed .c files to the gator driver Makefile
22  * - call gator_events_install in the events init function
23  * - add the init function to GATOR_EVENTS_LIST in gator_main.c
24  * - add a new events-*.xml file to the gator daemon and rebuild
25  *
26  * Troubleshooting:
27  * - verify the new events are part of events.xml, which is created when building the daemon
28  * - verify the new events exist at /dev/gator/events/ once gatord is launched
29  * - verify the counter name in the XML matches the name at /dev/gator/events
30  */
31
32 #include <linux/init.h>
33 #include <linux/io.h>
34 #include <linux/ratelimit.h>
35
36 #include "gator.h"
37
38 #define MMAPPED_COUNTERS_NUM 3
39
40 static int mmapped_global_enabled;
41
42 static struct {
43         unsigned long enabled;
44         unsigned long key;
45 } mmapped_counters[MMAPPED_COUNTERS_NUM];
46
47 static int mmapped_buffer[MMAPPED_COUNTERS_NUM * 2];
48
49 static s64 prev_time;
50
51 /* Adds mmapped_cntX directories and enabled, event, and key files to /dev/gator/events */
52 static int gator_events_mmapped_create_files(struct super_block *sb,
53                                              struct dentry *root)
54 {
55         int i;
56
57         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
58                 char buf[16];
59                 struct dentry *dir;
60
61                 snprintf(buf, sizeof(buf), "mmapped_cnt%d", i);
62                 dir = gatorfs_mkdir(sb, root, buf);
63                 if (WARN_ON(!dir))
64                         return -1;
65                 gatorfs_create_ulong(sb, dir, "enabled",
66                                      &mmapped_counters[i].enabled);
67                 gatorfs_create_ro_ulong(sb, dir, "key",
68                                         &mmapped_counters[i].key);
69         }
70
71         return 0;
72 }
73
74 static int gator_events_mmapped_start(void)
75 {
76         int i;
77         struct timespec ts;
78
79         getnstimeofday(&ts);
80         prev_time = timespec_to_ns(&ts);
81
82         mmapped_global_enabled = 0;
83         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
84                 if (mmapped_counters[i].enabled) {
85                         mmapped_global_enabled = 1;
86                         break;
87                 }
88         }
89
90         return 0;
91 }
92
93 static void gator_events_mmapped_stop(void)
94 {
95 }
96
97 /* This function "simulates" counters, generating values of fancy
98  * functions like sine or triangle... */
99 static int mmapped_simulate(int counter, int delta_in_us)
100 {
101         int result = 0;
102
103         switch (counter) {
104         case 0:         /* sort-of-sine */
105                 {
106                         static int t;
107                         int x;
108
109                         t += delta_in_us;
110                         if (t > 2048000)
111                                 t = 0;
112
113                         if (t % 1024000 < 512000)
114                                 x = 512000 - (t % 512000);
115                         else
116                                 x = t % 512000;
117
118                         result = 32 * x / 512000;
119                         result = result * result;
120
121                         if (t < 1024000)
122                                 result = 1922 - result;
123                 }
124                 break;
125         case 1:         /* triangle */
126                 {
127                         static int v, d = 1;
128
129                         v = v + d * delta_in_us;
130                         if (v < 0) {
131                                 v = 0;
132                                 d = 1;
133                         } else if (v > 1000000) {
134                                 v = 1000000;
135                                 d = -1;
136                         }
137
138                         result = v;
139                 }
140                 break;
141         case 2:         /* PWM signal */
142                 {
143                         static int dc, x, t;
144
145                         t += delta_in_us;
146                         if (t > 1000000)
147                                 t = 0;
148                         if (x / 1000000 != (x + delta_in_us) / 1000000)
149                                 dc = (dc + 100000) % 1000000;
150                         x += delta_in_us;
151
152                         result = t < dc ? 0 : 10;
153                 }
154                 break;
155         }
156
157         return result;
158 }
159
160 static int gator_events_mmapped_read(int **buffer, bool sched_switch)
161 {
162         int i;
163         int len = 0;
164         int delta_in_us;
165         struct timespec ts;
166         s64 time;
167
168         /* System wide counters - read from one core only */
169         if (!on_primary_core() || !mmapped_global_enabled)
170                 return 0;
171
172         getnstimeofday(&ts);
173         time = timespec_to_ns(&ts);
174         delta_in_us = (int)(time - prev_time) / 1000;
175         prev_time = time;
176
177         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
178                 if (mmapped_counters[i].enabled) {
179                         mmapped_buffer[len++] = mmapped_counters[i].key;
180                         mmapped_buffer[len++] =
181                             mmapped_simulate(i, delta_in_us);
182                 }
183         }
184
185         if (buffer)
186                 *buffer = mmapped_buffer;
187
188         return len;
189 }
190
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,
196 };
197
198 /* Must not be static! */
199 int __init gator_events_mmapped_init(void)
200 {
201         int i;
202
203         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
204                 mmapped_counters[i].enabled = 0;
205                 mmapped_counters[i].key = gator_events_get_key();
206         }
207
208         return gator_events_install(&gator_events_mmapped_interface);
209 }