summaryrefslogtreecommitdiffstats
blob: f7670f62a2580bd3220adfde265c954bd654cd6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Example events provider
 *
 * Copyright (C) ARM Limited 2010-2013. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Similar entries to those below must be present in the events.xml file.
 * To add them to the events.xml, create an events-mmap.xml with the 
 * following contents and rebuild gatord:
 *
 * <counter_set name="mmaped_cnt" count="3"/>
 * <category name="mmaped" counter_set="mmaped_cnt" per_cpu="no">
 *   <event event="0x0" title="Simulated1" name="Sine" display="maximum" average_selection="yes" description="Sort-of-sine"/>
 *   <event event="0x1" title="Simulated2" name="Triangle" display="maximum" average_selection="yes" description="Triangular wave"/>
 *   <event event="0x2" title="Simulated3" name="PWM" display="maximum" average_selection="yes" description="PWM Signal"/>
 * </category>
 */

#include <linux/init.h>
#include <linux/io.h>
#include <linux/ratelimit.h>

#include "gator.h"

#define MMAPED_COUNTERS_NUM 3

static struct {
	unsigned long enabled;
	unsigned long event;
	unsigned long key;
} mmaped_counters[MMAPED_COUNTERS_NUM];

static int mmaped_buffer[MMAPED_COUNTERS_NUM * 2];

#ifdef TODO
static void __iomem *mmaped_base;
#endif

#ifndef TODO
static s64 prev_time;
#endif

/* Adds mmaped_cntX directories and enabled, event, and key files to /dev/gator/events */
static int gator_events_mmaped_create_files(struct super_block *sb,
					    struct dentry *root)
{
	int i;

	for (i = 0; i < MMAPED_COUNTERS_NUM; i++) {
		char buf[16];
		struct dentry *dir;

		snprintf(buf, sizeof(buf), "mmaped_cnt%d", i);
		dir = gatorfs_mkdir(sb, root, buf);
		if (WARN_ON(!dir))
			return -1;
		gatorfs_create_ulong(sb, dir, "enabled",
				     &mmaped_counters[i].enabled);
		gatorfs_create_ulong(sb, dir, "event",
				     &mmaped_counters[i].event);
		gatorfs_create_ro_ulong(sb, dir, "key",
					&mmaped_counters[i].key);
	}

	return 0;
}

static int gator_events_mmaped_start(void)
{
#ifdef TODO
	for (i = 0; i < MMAPED_COUNTERS_NUM; i++)
		writel(mmaped_counters[i].event,
		       mmaped_base + COUNTERS_CONFIG_OFFSET[i]);

	writel(ENABLED, COUNTERS_CONTROL_OFFSET);
#endif

#ifndef TODO
	struct timespec ts;
	getnstimeofday(&ts);
	prev_time = timespec_to_ns(&ts);
#endif

	return 0;
}

static void gator_events_mmaped_stop(void)
{
#ifdef TODO
	writel(DISABLED, COUNTERS_CONTROL_OFFSET);
#endif
}

#ifndef TODO
/* This function "simulates" counters, generating values of fancy
 * functions like sine or triangle... */
static int mmaped_simulate(int counter, int delta_in_us)
{
	int result = 0;

	switch (counter) {
	case 0:		/* sort-of-sine */
		{
			static int t = 0;
			int x;

			t += delta_in_us;
			if (t > 2048000)
				t = 0;

			if (t % 1024000 < 512000)
				x = 512000 - (t % 512000);
			else
				x = t % 512000;

			result = 32 * x / 512000;
			result = result * result;

			if (t < 1024000)
				result = 1922 - result;
		}
		break;
	case 1:		/* triangle */
		{
			static int v, d = 1;

			v = v + d * delta_in_us;
			if (v < 0) {
				v = 0;
				d = 1;
			} else if (v > 1000000) {
				v = 1000000;
				d = -1;
			}

			result = v;
		}
		break;
	case 2:		/* PWM signal */
		{
			static int dc, x, t = 0;

			t += delta_in_us;
			if (t > 1000000)
				t = 0;
			if (x / 1000000 != (x + delta_in_us) / 1000000)
				dc = (dc + 100000) % 1000000;
			x += delta_in_us;

			result = t < dc ? 0 : 10;
		}
		break;
	}

	return result;
}
#endif

static int gator_events_mmaped_read(int **buffer)
{
	int i;
	int len = 0;
#ifndef TODO
	int delta_in_us;
	struct timespec ts;
	s64 time;
#endif

	/* System wide counters - read from one core only */
	if (!on_primary_core())
		return 0;

#ifndef TODO
	getnstimeofday(&ts);
	time = timespec_to_ns(&ts);
	delta_in_us = (int)(time - prev_time) / 1000;
	prev_time = time;
#endif

	for (i = 0; i < MMAPED_COUNTERS_NUM; i++) {
		if (mmaped_counters[i].enabled) {
			mmaped_buffer[len++] = mmaped_counters[i].key;
#ifdef TODO
			mmaped_buffer[len++] =
			    readl(mmaped_base + COUNTERS_VALUE_OFFSET[i]);
#else
			mmaped_buffer[len++] =
			    mmaped_simulate(mmaped_counters[i].event,
					    delta_in_us);
#endif
		}
	}

	if (buffer)
		*buffer = mmaped_buffer;

	return len;
}

static struct gator_interface gator_events_mmaped_interface = {
	.create_files = gator_events_mmaped_create_files,
	.start = gator_events_mmaped_start,
	.stop = gator_events_mmaped_stop,
	.read = gator_events_mmaped_read,
};

/* Must not be static! */
int __init gator_events_mmaped_init(void)
{
	int i;

#ifdef TODO
	mmaped_base = ioremap(COUNTERS_PHYS_ADDR, SZ_4K);
	if (!mmaped_base)
		return -ENOMEM;
#endif

	for (i = 0; i < MMAPED_COUNTERS_NUM; i++) {
		mmaped_counters[i].enabled = 0;
		mmaped_counters[i].key = gator_events_get_key();
	}

	return gator_events_install(&gator_events_mmaped_interface);
}

gator_events_init(gator_events_mmaped_init);