aboutsummaryrefslogtreecommitdiffstats
path: root/arm/timer.c
blob: e758e84855c353e7e4dbfdd0714fe4ec8f117584 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Timer tests for the ARM virt machine.
 *
 * Copyright (C) 2017, Alexander Graf <agraf@suse.de>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.
 */
#include <libcflat.h>
#include <devicetree.h>
#include <errata.h>
#include <asm/processor.h>
#include <asm/gic.h>
#include <asm/io.h>

#define ARCH_TIMER_CTL_ENABLE  (1 << 0)
#define ARCH_TIMER_CTL_IMASK   (1 << 1)
#define ARCH_TIMER_CTL_ISTATUS (1 << 2)

static void *gic_ispendr;
static void *gic_isenabler;
static void *gic_icenabler;

static bool ptimer_unsupported;

static void ptimer_unsupported_handler(struct pt_regs *regs, unsigned int esr)
{
	ptimer_unsupported = true;
	regs->pc += 4;
}

static u64 read_vtimer_counter(void)
{
	isb();
	return read_sysreg(cntvct_el0);
}

static u64 read_vtimer_cval(void)
{
	return read_sysreg(cntv_cval_el0);
}

static void write_vtimer_cval(u64 val)
{
	write_sysreg(val, cntv_cval_el0);
	isb();
}

static s32 read_vtimer_tval(void)
{
	return read_sysreg(cntv_tval_el0);
}

static void write_vtimer_tval(s32 val)
{
	write_sysreg(val, cntv_tval_el0);
	isb();
}

static u64 read_vtimer_ctl(void)
{
	return read_sysreg(cntv_ctl_el0);
}

static void write_vtimer_ctl(u64 val)
{
	write_sysreg(val, cntv_ctl_el0);
	isb();
}

static u64 read_ptimer_counter(void)
{
	isb();
	return read_sysreg(cntpct_el0);
}

static u64 read_ptimer_cval(void)
{
	return read_sysreg(cntp_cval_el0);
}

static void write_ptimer_cval(u64 val)
{
	write_sysreg(val, cntp_cval_el0);
	isb();
}

static s32 read_ptimer_tval(void)
{
	return read_sysreg(cntp_tval_el0);
}

static void write_ptimer_tval(s32 val)
{
	write_sysreg(val, cntp_tval_el0);
	isb();
}

static u64 read_ptimer_ctl(void)
{
	return read_sysreg(cntp_ctl_el0);
}

static void write_ptimer_ctl(u64 val)
{
	write_sysreg(val, cntp_ctl_el0);
	isb();
}

struct timer_info {
	u32 irq;
	u32 irq_flags;
	bool irq_received;
	u64 (*read_counter)(void);
	u64 (*read_cval)(void);
	void (*write_cval)(u64);
	s32 (*read_tval)(void);
	void (*write_tval)(s32);
	u64 (*read_ctl)(void);
	void (*write_ctl)(u64);
};

static struct timer_info vtimer_info = {
	.irq_received = false,
	.read_counter = read_vtimer_counter,
	.read_cval = read_vtimer_cval,
	.write_cval = write_vtimer_cval,
	.read_tval = read_vtimer_tval,
	.write_tval = write_vtimer_tval,
	.read_ctl = read_vtimer_ctl,
	.write_ctl = write_vtimer_ctl,
};

static struct timer_info ptimer_info = {
	.irq_received = false,
	.read_counter = read_ptimer_counter,
	.read_cval = read_ptimer_cval,
	.write_cval = write_ptimer_cval,
	.read_tval = read_ptimer_tval,
	.write_tval = write_ptimer_tval,
	.read_ctl = read_ptimer_ctl,
	.write_ctl = write_ptimer_ctl,
};

static void set_timer_irq_enabled(struct timer_info *info, bool enabled)
{
	u32 val = 1 << PPI(info->irq);

	if (enabled)
		writel(val, gic_isenabler);
	else
		writel(val, gic_icenabler);
}

static void irq_handler(struct pt_regs *regs)
{
	struct timer_info *info;
	u32 irqstat = gic_read_iar();
	u32 irqnr = gic_iar_irqnr(irqstat);

	if (irqnr != GICC_INT_SPURIOUS)
		gic_write_eoir(irqstat);

	if (irqnr == PPI(vtimer_info.irq)) {
		info = &vtimer_info;
	} else if (irqnr == PPI(ptimer_info.irq)) {
		info = &ptimer_info;
	} else {
		report_info("Unexpected interrupt: %d\n", irqnr);
		return;
	}

	info->write_ctl(ARCH_TIMER_CTL_IMASK | ARCH_TIMER_CTL_ENABLE);
	info->irq_received = true;
}

static bool gic_timer_pending(struct timer_info *info)
{
	return readl(gic_ispendr) & (1 << PPI(info->irq));
}

static bool test_cval_10msec(struct timer_info *info)
{
	u64 time_10ms = read_sysreg(cntfrq_el0) / 100;
	u64 time_1us = time_10ms / 10000;
	u64 before_timer, after_timer;
	s64 difference;

	/* Program timer to fire in 10 ms */
	before_timer = info->read_counter();
	info->write_cval(before_timer + time_10ms);
	info->write_ctl(ARCH_TIMER_CTL_ENABLE);

	/* Wait for the timer to fire */
	while (!(info->read_ctl() & ARCH_TIMER_CTL_ISTATUS))
		;

	/* It fired, check how long it took */
	after_timer = info->read_counter();
	difference = after_timer - (before_timer + time_10ms);

	report_info("After timer: 0x%016lx", after_timer);
	report_info("Expected   : 0x%016lx", before_timer + time_10ms);
	report_info("Difference : %ld us", difference / time_1us);

	if (difference < 0) {
		printf("ISTATUS set too early\n");
		return false;
	}
	return difference < time_10ms;
}

static void test_timer(struct timer_info *info)
{
	u64 now = info->read_counter();
	u64 time_10s = read_sysreg(cntfrq_el0) * 10;
	u64 later = now + time_10s;
	s32 left;

	/* We don't want the irq handler to fire because that will change the
	 * timer state and we want to test the timer output signal.  We can
	 * still read the pending state even if it's disabled. */
	set_timer_irq_enabled(info, false);

	/* Enable the timer, but schedule it for much later */
	info->write_cval(later);
	info->write_ctl(ARCH_TIMER_CTL_ENABLE);
	report(!gic_timer_pending(info), "not pending before");

	info->write_cval(now - 1);
	report(gic_timer_pending(info), "interrupt signal pending");

	/* Disable the timer again and prepare to take interrupts */
	info->write_ctl(0);
	set_timer_irq_enabled(info, true);
	report(!gic_timer_pending(info), "interrupt signal no longer pending");

	report(test_cval_10msec(info), "latency within 10 ms");
	report(info->irq_received, "interrupt received");

	/* Disable the timer again */
	info->write_ctl(0);

	/* Test TVAL and IRQ trigger */
	info->irq_received = false;
	info->write_tval(read_sysreg(cntfrq_el0) / 100);	/* 10 ms */
	local_irq_disable();
	info->write_ctl(ARCH_TIMER_CTL_ENABLE);
	report_info("waiting for interrupt...");
	wfi();
	local_irq_enable();
	left = info->read_tval();
	report(info->irq_received, "interrupt received after TVAL/WFI");
	report(left < 0, "timer has expired");
	report_info("TVAL is %d ticks", left);
}

static void test_vtimer(void)
{
	report_prefix_push("vtimer-busy-loop");
	test_timer(&vtimer_info);
	report_prefix_pop();
}

static void test_ptimer(void)
{
	if (ptimer_unsupported)
		return;

	report_prefix_push("ptimer-busy-loop");
	test_timer(&ptimer_info);
	report_prefix_pop();
}

static void test_init(void)
{
	const struct fdt_property *prop;
	const void *fdt = dt_fdt();
	int node, len;
	u32 *data;

	node = fdt_node_offset_by_compatible(fdt, -1, "arm,armv8-timer");
	assert(node >= 0);
	prop = fdt_get_property(fdt, node, "interrupts", &len);
	assert(prop && len == (4 * 3 * sizeof(u32)));

	data = (u32 *)prop->data;
	assert(fdt32_to_cpu(data[3]) == 1);
	ptimer_info.irq = fdt32_to_cpu(data[4]);
	ptimer_info.irq_flags = fdt32_to_cpu(data[5]);
	assert(fdt32_to_cpu(data[6]) == 1);
	vtimer_info.irq = fdt32_to_cpu(data[7]);
	vtimer_info.irq_flags = fdt32_to_cpu(data[8]);

	install_exception_handler(EL1H_SYNC, ESR_EL1_EC_UNKNOWN, ptimer_unsupported_handler);
	read_sysreg(cntp_ctl_el0);
	install_exception_handler(EL1H_SYNC, ESR_EL1_EC_UNKNOWN, NULL);

	if (ptimer_unsupported && !ERRATA(7b6b46311a85)) {
		report_skip("Skipping ptimer tests. Set ERRATA_7b6b46311a85=y to enable.");
	} else if (ptimer_unsupported) {
		report(false, "ptimer: read CNTP_CTL_EL0");
		report_info("ptimer: skipping remaining tests");
	}

	gic_enable_defaults();

	switch (gic_version()) {
	case 2:
		gic_ispendr = gicv2_dist_base() + GICD_ISPENDR;
		gic_isenabler = gicv2_dist_base() + GICD_ISENABLER;
		gic_icenabler = gicv2_dist_base() + GICD_ICENABLER;
		break;
	case 3:
		gic_ispendr = gicv3_sgi_base() + GICD_ISPENDR;
		gic_isenabler = gicv3_sgi_base() + GICR_ISENABLER0;
		gic_icenabler = gicv3_sgi_base() + GICR_ICENABLER0;
		break;
	}

	install_irq_handler(EL1H_IRQ, irq_handler);
	local_irq_enable();
}

static void print_timer_info(void)
{
	printf("CNTFRQ_EL0   : 0x%016lx\n", read_sysreg(cntfrq_el0));

	if (!ptimer_unsupported){
		printf("CNTPCT_EL0   : 0x%016lx\n", read_sysreg(cntpct_el0));
		printf("CNTP_CTL_EL0 : 0x%016lx\n", read_sysreg(cntp_ctl_el0));
		printf("CNTP_CVAL_EL0: 0x%016lx\n", read_sysreg(cntp_cval_el0));
	}

	printf("CNTVCT_EL0   : 0x%016lx\n", read_sysreg(cntvct_el0));
	printf("CNTV_CTL_EL0 : 0x%016lx\n", read_sysreg(cntv_ctl_el0));
	printf("CNTV_CVAL_EL0: 0x%016lx\n", read_sysreg(cntv_cval_el0));
}

int main(int argc, char **argv)
{
	int i;

	test_init();

	print_timer_info();

	if (argc == 1) {
		test_vtimer();
		test_ptimer();
	}

	for (i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "vtimer") == 0)
			test_vtimer();
		if (strcmp(argv[i], "ptimer") == 0)
			test_ptimer();
	}

	return report_summary();
}