summaryrefslogtreecommitdiff
path: root/arch/riscv/lib/interrupts.c
blob: 074c70ee77a7b2db835d59af4ded4effb3d35a08 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2016-17 Microsemi Corporation.
 * Padmarao Begari, Microsemi Corporation <padmarao.begari@microsemi.com>
 *
 * Copyright (C) 2017 Andes Technology Corporation
 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
 *
 * Copyright (C) 2019 Sean Anderson <seanga2@gmail.com>
 */

#include <common.h>
#include <hang.h>
#include <irq_func.h>
#include <asm/ptrace.h>
#include <asm/system.h>
#include <asm/encoding.h>

static void show_regs(struct pt_regs *regs)
{
#ifdef CONFIG_SHOW_REGS
	printf("RA: " REG_FMT " SP:  " REG_FMT " GP:  " REG_FMT "\n",
	       regs->ra, regs->sp, regs->gp);
	printf("TP: " REG_FMT " T0:  " REG_FMT " T1:  " REG_FMT "\n",
	       regs->tp, regs->t0, regs->t1);
	printf("T2: " REG_FMT " S0:  " REG_FMT " S1:  " REG_FMT "\n",
	       regs->t2, regs->s0, regs->s1);
	printf("A0: " REG_FMT " A1:  " REG_FMT " A2:  " REG_FMT "\n",
	       regs->a0, regs->a1, regs->a2);
	printf("A3: " REG_FMT " A4:  " REG_FMT " A5:  " REG_FMT "\n",
	       regs->a3, regs->a4, regs->a5);
	printf("A6: " REG_FMT " A7:  " REG_FMT " S2:  " REG_FMT "\n",
	       regs->a6, regs->a7, regs->s2);
	printf("S3: " REG_FMT " S4:  " REG_FMT " S5:  " REG_FMT "\n",
	       regs->s3, regs->s4, regs->s5);
	printf("S6: " REG_FMT " S7:  " REG_FMT " S8:  " REG_FMT "\n",
	       regs->s6, regs->s7, regs->s8);
	printf("S9: " REG_FMT " S10: " REG_FMT " S11: " REG_FMT "\n",
	       regs->s9, regs->s10, regs->s11);
	printf("T3: " REG_FMT " T4:  " REG_FMT " T5:  " REG_FMT "\n",
	       regs->t3, regs->t4, regs->t5);
	printf("T6: " REG_FMT "\n", regs->t6);
#endif
}

static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs)
{
	static const char * const exception_code[] = {
		"Instruction address misaligned",
		"Instruction access fault",
		"Illegal instruction",
		"Breakpoint",
		"Load address misaligned",
		"Load access fault",
		"Store/AMO address misaligned",
		"Store/AMO access fault",
		"Environment call from U-mode",
		"Environment call from S-mode",
		"Reserved",
		"Environment call from M-mode",
		"Instruction page fault",
		"Load page fault",
		"Reserved",
		"Store/AMO page fault",
	};

	if (code < ARRAY_SIZE(exception_code))
		printf("Unhandled exception: %s\n", exception_code[code]);
	else
		printf("Unhandled exception code: %ld\n", code);

	printf("EPC: " REG_FMT " TVAL: " REG_FMT "\n", epc, tval);
	show_regs(regs);
	hang();
}

int interrupt_init(void)
{
	return 0;
}

/*
 * enable interrupts
 */
void enable_interrupts(void)
{
}

/*
 * disable interrupts
 */
int disable_interrupts(void)
{
	return 0;
}

ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs)
{
	ulong is_irq, irq;

	is_irq = (cause & MCAUSE_INT);
	irq = (cause & ~MCAUSE_INT);

	if (is_irq) {
		switch (irq) {
		case IRQ_M_EXT:
		case IRQ_S_EXT:
			external_interrupt(0);	/* handle external interrupt */
			break;
		case IRQ_M_TIMER:
		case IRQ_S_TIMER:
			timer_interrupt(0);	/* handle timer interrupt */
			break;
		default:
			_exit_trap(cause, epc, tval, regs);
			break;
		};
	} else {
		_exit_trap(cause, epc, tval, regs);
	}

	return epc;
}

/*
 *Entry Point for PLIC Interrupt Handler
 */
__attribute__((weak)) void external_interrupt(struct pt_regs *regs)
{
}

__attribute__((weak)) void timer_interrupt(struct pt_regs *regs)
{
}