xref: /kvm-unit-tests/lib/powerpc/processor.c (revision 8e458b76658fc090ab81b9963fd40d5efab18366)
1 /*
2  * processor control and status function
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Library General Public License version 2.
6  */
7 
8 #include <libcflat.h>
9 #include <asm/processor.h>
10 #include <asm/ptrace.h>
11 #include <asm/setup.h>
12 #include <asm/barrier.h>
13 
14 static struct {
15 	void (*func)(struct pt_regs *, void *data);
16 	void *data;
17 } handlers[16];
18 
19 void handle_exception(int trap, void (*func)(struct pt_regs *, void *),
20 		      void * data)
21 {
22 	trap >>= 8;
23 
24 	if (trap < 16) {
25 		handlers[trap].func = func;
26 		handlers[trap].data = data;
27 	}
28 }
29 
30 void do_handle_exception(struct pt_regs *regs)
31 {
32 	unsigned char v;
33 
34 	v = regs->trap >> 8;
35 
36 	if (v < 16 && handlers[v].func) {
37 		handlers[v].func(regs, handlers[v].data);
38 		return;
39 	}
40 
41 	printf("unhandled cpu exception %#lx at NIA:0x%016lx MSR:0x%016lx\n", regs->trap, regs->nip, regs->msr);
42 	abort();
43 }
44 
45 void delay(uint64_t cycles)
46 {
47 	uint64_t start = get_tb();
48 
49 	while ((get_tb() - start) < cycles)
50 		cpu_relax();
51 }
52 
53 void udelay(uint64_t us)
54 {
55 	delay((us * tb_hz) / 1000000);
56 }
57