1 /*
2 * Copyright (c) 2021, Shenzhen Academy of Aerospace Technology
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-11-16 Dystopia the first version
9 */
10
11 #ifndef __TRAP_H__
12 #define __TRAP_H__
13
14 #include "c66xx.h"
15
16 /*
17 * exception operation macro
18 */
19 #define disable_exception()
20 #define get_except_type() EFR
21 #define ack_exception(type) ECR = 1ul << (type)
22 #define get_iexcept() IERR
23 #define set_iexcept(mask) IERR = (mask)
24
25 /*
26 * exception types
27 */
28 #define EXCEPT_TYPE_NXF 31 /* NMI */
29 #define EXCEPT_TYPE_EXC 30 /* external exception */
30 #define EXCEPT_TYPE_IXF 1 /* internal exception */
31 #define EXCEPT_TYPE_SXF 0 /* software exception */
32
33 #define EXCEPT_CAUSE_LBX (1 << 7) /* loop buffer exception */
34 #define EXCEPT_CAUSE_PRX (1 << 6) /* privilege exception */
35 #define EXCEPT_CAUSE_RAX (1 << 5) /* resource access exception */
36 #define EXCEPT_CAUSE_RCX (1 << 4) /* resource conflict exception */
37 #define EXCEPT_CAUSE_OPX (1 << 3) /* opcode exception */
38 #define EXCEPT_CAUSE_EPX (1 << 2) /* execute packet exception */
39 #define EXCEPT_CAUSE_FPX (1 << 1) /* fetch packet exception */
40 #define EXCEPT_CAUSE_IFX (1 << 0) /* instruction fetch exception */
41
42 enum SYSTEM_TRAP_CODE
43 {
44 ABORT_BUS_ADDRERR = 0, // bus address error
45 ABORT_BUS_ACCERR, // bus access permission error
46 ABORT_OPCODE_ILL, // illegal opcode
47 ABORT_PRVREG_ILL, // privilege register
48 ABORT_PRVOPC_ILL, // privileged opcode
49 ABORT_ILLTRP_ILL, // illegal trap
50 ABORT_BRKPT_ILL, // handling breakpoints
51 };
52
53 /*
54 * abort types
55 */
56 #define ABORT_TYPE_BUS 0 // bus access abnormal
57 #define ABORT_TYPE_MAP 1 // page table mapping error
58 #define ABORT_TYPE_UNDDEF 0xff // undefined exception
59 #define ABORT_TYPE_FATAL 0xffffffff // fatal error
60
61 struct rt_exception_info {
62 char *kernel_str;
63 int type;
64 int code;
65 };
66
67 #define BKPT_OPCODE 0x56454314 /* illegal opcode */
68 #define INTC_MEXPMASK __SYSREGA(0x018000e0, unsigned int)
69
70 extern void rt_trap_init(void);
71 extern void rt_hw_enable_exception(void);
72 extern int __fls(int val);
73 extern int __ffs(int val);
74
75 /*
76 * ffz - find first zero in word.
77 * @word: The word to search
78 *
79 * Undefined if no zero exists, so code should check against ~0UL first.
80 */
81 #define ffz(x) __ffs(~(x))
82
83 /**
84 * fls - find last (most-significant) bit set
85 * @x: the word to search
86 *
87 * This is defined the same way as ffs.
88 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
89 */
fls(int x)90 static inline int fls(int x)
91 {
92 if (!x)
93 {
94 return 0;
95 }
96 return 32 - __fls(x);
97 }
98
99 /**
100 * ffs - find first bit set
101 * @x: the word to search
102 *
103 * This is defined the same way as
104 * the libc and compiler builtin ffs routines, therefore
105 * differs in spirit from the above ffz (man ffs).
106 * Note ffs(0) = 0, ffs(1) = 1, ffs(0x80000000) = 32.
107 */
ffs(int x)108 static inline int ffs(int x)
109 {
110 if (!x)
111 {
112 return 0;
113 }
114 return __ffs(x) + 1;
115 }
116
117 #endif /* __TRAP_H__ */
118