aboutsummaryrefslogtreecommitdiff
path: root/drivers/gator/gator_events_ccn-504.c
blob: b91a9a149b90546a8133ecef8c92137da6b9a6d0 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
 * Copyright (C) ARM Limited 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.
 */

/*******************************************************************************
 * WARNING: This code is an experimental implementation of the CCN-504 hardware
 * counters which has not been tested on the hardware. Commented debug
 * statements are present and can be uncommented for diagnostic purposes.
 ******************************************************************************/

#include <linux/io.h>
#include <linux/module.h>

#include "gator.h"

#define PERIPHBASE 0x2E000000

#define NUM_REGIONS 256
#define REGION_SIZE (64*1024)
#define REGION_DEBUG 1
#define REGION_XP 64

// DT (Debug) region
#define PMEVCNTSR0    0x0150
#define PMCCNTRSR     0x0190
#define PMCR          0x01A8
#define PMSR          0x01B0
#define PMSR_REQ      0x01B8
#define PMSR_CLR      0x01C0

// XP region
#define DT_CONFIG     0x0300

// Multiple
#define PMU_EVENT_SEL 0x0600
#define OLY_ID        0xFF00

#define CCNT 4
#define CNTMAX (4 + 1)

#define get_pmu_event_id(event) (((event) >> 0) & 0xFF)
#define get_node_type(event) (((event) >> 8) & 0xFF)
#define get_region(event) (((event) >> 16) & 0xFF)

MODULE_PARM_DESC(ccn504_addr, "CCN-504 physical base address");
static unsigned long ccn504_addr = 0;
module_param(ccn504_addr, ulong, 0444);

static void __iomem *gator_events_ccn504_base;
static unsigned long gator_events_ccn504_enabled[CNTMAX];
static unsigned long gator_events_ccn504_event[CNTMAX];
static unsigned long gator_events_ccn504_key[CNTMAX];
static int gator_events_ccn504_buffer[2*CNTMAX];

static void gator_events_ccn504_create_shutdown(void)
{
	if (gator_events_ccn504_base != NULL) {
		iounmap(gator_events_ccn504_base);
	}
}

static int gator_events_ccn504_create_files(struct super_block *sb, struct dentry *root)
{
	struct dentry *dir;
	int i;
	char buf[32];

	for (i = 0; i < CNTMAX; ++i) {
		if (i == CCNT) {
			snprintf(buf, sizeof(buf), "CCN-504_ccnt");
		} else {
			snprintf(buf, sizeof(buf), "CCN-504_cnt%i", i);
		}
		dir = gatorfs_mkdir(sb, root, buf);
		if (!dir) {
			return -1;
		}

		gatorfs_create_ulong(sb, dir, "enabled", &gator_events_ccn504_enabled[i]);
		if (i != CCNT) {
			gatorfs_create_ulong(sb, dir, "event", &gator_events_ccn504_event[i]);
		}
		gatorfs_create_ro_ulong(sb, dir, "key", &gator_events_ccn504_key[i]);
	}

	return 0;
}

static void gator_events_ccn504_set_dt_config(int xp_node_id, int event_num, int value)
{
	u32 dt_config;

	dt_config = readl(gator_events_ccn504_base + (REGION_XP + xp_node_id)*REGION_SIZE + DT_CONFIG);
	dt_config |= (value + event_num) << (4*event_num);
	//printk(KERN_ERR "%s(%s:%i) writel %x %x\n", __FUNCTION__, __FILE__, __LINE__, dt_config, (REGION_XP + xp_node_id)*REGION_SIZE + DT_CONFIG);
	writel(dt_config, gator_events_ccn504_base + (REGION_XP + xp_node_id)*REGION_SIZE + DT_CONFIG);
}

static int gator_events_ccn504_start(void)
{
	int i;

	// Disable INTREQ on overflow
	// [6] ovfl_intr_en = 0
	// perhaps set to 1?
	// [5] cntr_rst = 0
	// No register paring
	// [4:1] cntcfg = 0
	// Enable PMU features
	// [0] pmu_en = 1
	//printk(KERN_ERR "%s(%s:%i) writel %x %x\n", __FUNCTION__, __FILE__, __LINE__, 0x1, REGION_DEBUG*REGION_SIZE + PMCR);
	writel(0x1, gator_events_ccn504_base + REGION_DEBUG*REGION_SIZE + PMCR);

	// Assume no other pmu_event_sel registers are set

	// cycle counter does not need to be enabled
	for (i = 0; i < CCNT; ++i) {
		int pmu_event_id;
		int node_type;
		int region;
		u32 pmu_event_sel;
		u32 oly_id_whole;
		u32 oly_id;
		u32 node_id;

		if (!gator_events_ccn504_enabled[i]) {
			continue;
		}

		pmu_event_id = get_pmu_event_id(gator_events_ccn504_event[i]);
		node_type = get_node_type(gator_events_ccn504_event[i]);
		region = get_region(gator_events_ccn504_event[i]);
		//printk(KERN_ERR "%s(%s:%i) pmu_event_id: %x node_type: %x region: %x\n", __FUNCTION__, __FILE__, __LINE__, pmu_event_id, node_type, region);

		// Verify the node_type
		oly_id_whole = readl(gator_events_ccn504_base + region*REGION_SIZE + OLY_ID);
		oly_id = oly_id_whole & 0x1F;
		node_id = (oly_id_whole >> 8) & 0x7F;
		if ((oly_id != node_type) ||
				((node_type == 0x16) && ((oly_id == 0x14) || (oly_id == 0x15) || (oly_id == 0x16) || (oly_id == 0x18) || (oly_id == 0x19) || (oly_id == 0x1A)))) {
			printk(KERN_ERR "%s(%s:%i) oly_id is %x expected %x\n", __FUNCTION__, __FILE__, __LINE__, oly_id, node_type);
			return -1;
		}

		// Set the control register
		pmu_event_sel = readl(gator_events_ccn504_base + region*REGION_SIZE + PMU_EVENT_SEL);
		switch (node_type) {
		case 0x08: // XP
			pmu_event_sel |= pmu_event_id << (7*i);
			gator_events_ccn504_set_dt_config(node_id, i, 0x4);
			break;
		case 0x04: // HN-F
		case 0x16: // RN-I
		case 0x10: // SBAS
			pmu_event_sel |= pmu_event_id << (4*i);
			gator_events_ccn504_set_dt_config(node_id/2, i, (node_id & 1) == 0 ? 0x8 : 0xC);
			break;
		}
		//printk(KERN_ERR "%s(%s:%i) writel %x %x\n", __FUNCTION__, __FILE__, __LINE__, pmu_event_sel, region*REGION_SIZE + PMU_EVENT_SEL);
		writel(pmu_event_sel, gator_events_ccn504_base + region*REGION_SIZE + PMU_EVENT_SEL);
	}

	return 0;
}

static void gator_events_ccn504_stop(void)
{
	int i;

	// cycle counter does not need to be disabled
	for (i = 0; i < CCNT; ++i) {
		int node_type;
		int region;

		node_type = get_node_type(gator_events_ccn504_event[i]);
		region = get_region(gator_events_ccn504_event[i]);

		//printk(KERN_ERR "%s(%s:%i) writel %x %x\n", __FUNCTION__, __FILE__, __LINE__, 0, region*REGION_SIZE + PMU_EVENT_SEL);
		writel(0, gator_events_ccn504_base + region*REGION_SIZE + PMU_EVENT_SEL);
	}

	// Clear dt_config
	for (i = 0; i < 11; ++i) {
		//printk(KERN_ERR "%s(%s:%i) writel %x %x\n", __FUNCTION__, __FILE__, __LINE__, 0, (REGION_XP + i)*REGION_SIZE + DT_CONFIG);
		writel(0, gator_events_ccn504_base + (REGION_XP + i)*REGION_SIZE + DT_CONFIG);
	}
}

static int gator_events_ccn504_read(int **buffer)
{
	int i;
	int len = 0;

	if (!on_primary_core()) {
		return 0;
	}

	// Verify the pmsr register is zero
	//i = 0;
	while (readl(gator_events_ccn504_base + REGION_DEBUG*REGION_SIZE + PMSR) != 0) {
		//++i;
	}
	//printk(KERN_ERR "%s(%s:%i) %i\n", __FUNCTION__, __FILE__, __LINE__, i);

	// Request a PMU snapshot
	writel(1, gator_events_ccn504_base + REGION_DEBUG*REGION_SIZE + PMSR_REQ);

	// Wait for the snapshot
	//i = 0;
	while (readl(gator_events_ccn504_base + REGION_DEBUG*REGION_SIZE + PMSR) == 0) {
		//++i;
	}
	//printk(KERN_ERR "%s(%s:%i) %i\n", __FUNCTION__, __FILE__, __LINE__, i);

	// Read the shadow registers
	for (i = 0; i < CNTMAX; ++i) {
		if (!gator_events_ccn504_enabled[i]) {
			continue;
		}

		gator_events_ccn504_buffer[len++] = gator_events_ccn504_key[i];
		gator_events_ccn504_buffer[len++] = readl(gator_events_ccn504_base + REGION_DEBUG*REGION_SIZE + (i == CCNT ? PMCCNTRSR : PMEVCNTSR0 + 8*i));

		// Are the counters registers cleared when read? Is that what the cntr_rst bit on the pmcr register does?
	}

	// Clear the PMU snapshot status
	writel(1, gator_events_ccn504_base + REGION_DEBUG*REGION_SIZE + PMSR_CLR);

	return len;
}

static void __maybe_unused gator_events_ccn504_enumerate(int pos, int size)
{
	int i;
	u32 oly_id;

	for (i = pos; i < pos + size; ++i) {
		oly_id = readl(gator_events_ccn504_base + i*REGION_SIZE + OLY_ID);
		printk(KERN_ERR "%s(%s:%i) %i %08x\n", __FUNCTION__, __FILE__, __LINE__, i, oly_id);
	}
}

static struct gator_interface gator_events_ccn504_interface = {
	.shutdown = gator_events_ccn504_create_shutdown,
	.create_files = gator_events_ccn504_create_files,
	.start = gator_events_ccn504_start,
	.stop = gator_events_ccn504_stop,
	.read = gator_events_ccn504_read,
};

int gator_events_ccn504_init(void)
{
	int i;

	if (ccn504_addr == 0) {
		return -1;
	}

	gator_events_ccn504_base = ioremap(ccn504_addr, NUM_REGIONS*REGION_SIZE);
	if (gator_events_ccn504_base == NULL) {
		printk(KERN_ERR "%s(%s:%i) ioremap returned NULL\n", __FUNCTION__, __FILE__, __LINE__);
		return -1;
	}
	//printk(KERN_ERR "%s(%s:%i)\n", __FUNCTION__, __FILE__, __LINE__);

	// Test - can memory be read
	{
		//gator_events_ccn504_enumerate(0, NUM_REGIONS);

#if 0
		// DT
		gator_events_ccn504_enumerate(1, 1);
		// HN-F
		gator_events_ccn504_enumerate(32, 8);
		// XP
		gator_events_ccn504_enumerate(64, 11);
		// RN-I
		gator_events_ccn504_enumerate(128, 1);
		gator_events_ccn504_enumerate(130, 1);
		gator_events_ccn504_enumerate(134, 1);
		gator_events_ccn504_enumerate(140, 1);
		gator_events_ccn504_enumerate(144, 1);
		gator_events_ccn504_enumerate(148, 1);
		// SBAS
		gator_events_ccn504_enumerate(129, 1);
		gator_events_ccn504_enumerate(137, 1);
		gator_events_ccn504_enumerate(139, 1);
		gator_events_ccn504_enumerate(147, 1);
#endif
	}

	for (i = 0; i < CNTMAX; ++i) {
		gator_events_ccn504_enabled[i] = 0;
		gator_events_ccn504_event[i] = 0;
		gator_events_ccn504_key[i] = gator_events_get_key();
	}

	return gator_events_install(&gator_events_ccn504_interface);
}

gator_events_init(gator_events_ccn504_init);