2 * Altera Nios II virtual CPU header
4 * Copyright (c) 2012 Chris Wulff <crwulff@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/lgpl-2.1.html>
23 #include "qemu-common.h"
25 #define TARGET_LONG_BITS 32
27 #define CPUArchState struct CPUNios2State
29 #include "exec/cpu-defs.h"
30 #include "fpu/softfloat.h"
33 typedef struct CPUNios2State CPUNios2State
;
34 #if !defined(CONFIG_USER_ONLY)
38 #define TYPE_NIOS2_CPU "nios2-cpu"
40 #define NIOS2_CPU_CLASS(klass) \
41 OBJECT_CLASS_CHECK(Nios2CPUClass, (klass), TYPE_NIOS2_CPU)
42 #define NIOS2_CPU(obj) \
43 OBJECT_CHECK(Nios2CPU, (obj), TYPE_NIOS2_CPU)
44 #define NIOS2_CPU_GET_CLASS(obj) \
45 OBJECT_GET_CLASS(Nios2CPUClass, (obj), TYPE_NIOS2_CPU)
49 * @parent_reset: The parent class' reset handler.
53 typedef struct Nios2CPUClass
{
55 CPUClass parent_class
;
58 DeviceRealize parent_realize
;
59 void (*parent_reset
)(CPUState
*cpu
);
62 #define TARGET_HAS_ICE 1
64 /* Configuration options for Nios II */
65 #define RESET_ADDRESS 0x00000000
66 #define EXCEPTION_ADDRESS 0x00000004
67 #define FAST_TLB_MISS_ADDRESS 0x00000008
70 /* GP regs + CR regs + PC */
71 #define NUM_CORE_REGS (32 + 32 + 1)
73 /* General purpose register aliases */
91 /* Control register aliases */
93 #define CR_STATUS (CR_BASE + 0)
94 #define CR_STATUS_PIE (1 << 0)
95 #define CR_STATUS_U (1 << 1)
96 #define CR_STATUS_EH (1 << 2)
97 #define CR_STATUS_IH (1 << 3)
98 #define CR_STATUS_IL (63 << 4)
99 #define CR_STATUS_CRS (63 << 10)
100 #define CR_STATUS_PRS (63 << 16)
101 #define CR_STATUS_NMI (1 << 22)
102 #define CR_STATUS_RSIE (1 << 23)
103 #define CR_ESTATUS (CR_BASE + 1)
104 #define CR_BSTATUS (CR_BASE + 2)
105 #define CR_IENABLE (CR_BASE + 3)
106 #define CR_IPENDING (CR_BASE + 4)
107 #define CR_CPUID (CR_BASE + 5)
108 #define CR_CTL6 (CR_BASE + 6)
109 #define CR_EXCEPTION (CR_BASE + 7)
110 #define CR_PTEADDR (CR_BASE + 8)
111 #define CR_PTEADDR_PTBASE_SHIFT 22
112 #define CR_PTEADDR_PTBASE_MASK (0x3FF << CR_PTEADDR_PTBASE_SHIFT)
113 #define CR_PTEADDR_VPN_SHIFT 2
114 #define CR_PTEADDR_VPN_MASK (0xFFFFF << CR_PTEADDR_VPN_SHIFT)
115 #define CR_TLBACC (CR_BASE + 9)
116 #define CR_TLBACC_IGN_SHIFT 25
117 #define CR_TLBACC_IGN_MASK (0x7F << CR_TLBACC_IGN_SHIFT)
118 #define CR_TLBACC_C (1 << 24)
119 #define CR_TLBACC_R (1 << 23)
120 #define CR_TLBACC_W (1 << 22)
121 #define CR_TLBACC_X (1 << 21)
122 #define CR_TLBACC_G (1 << 20)
123 #define CR_TLBACC_PFN_MASK 0x000FFFFF
124 #define CR_TLBMISC (CR_BASE + 10)
125 #define CR_TLBMISC_WAY_SHIFT 20
126 #define CR_TLBMISC_WAY_MASK (0xF << CR_TLBMISC_WAY_SHIFT)
127 #define CR_TLBMISC_RD (1 << 19)
128 #define CR_TLBMISC_WR (1 << 18)
129 #define CR_TLBMISC_PID_SHIFT 4
130 #define CR_TLBMISC_PID_MASK (0x3FFF << CR_TLBMISC_PID_SHIFT)
131 #define CR_TLBMISC_DBL (1 << 3)
132 #define CR_TLBMISC_BAD (1 << 2)
133 #define CR_TLBMISC_PERM (1 << 1)
134 #define CR_TLBMISC_D (1 << 0)
135 #define CR_ENCINJ (CR_BASE + 11)
136 #define CR_BADADDR (CR_BASE + 12)
137 #define CR_CONFIG (CR_BASE + 13)
138 #define CR_MPUBASE (CR_BASE + 14)
139 #define CR_MPUACC (CR_BASE + 15)
141 /* Other registers */
145 #define EXCP_BREAK -1
147 #define EXCP_PRESET 1
150 #define EXCP_UNIMPL 4
151 #define EXCP_ILLEGAL 5
152 #define EXCP_UNALIGN 6
153 #define EXCP_UNALIGND 7
155 #define EXCP_SUPERA 9
156 #define EXCP_SUPERI 10
157 #define EXCP_SUPERD 11
165 #define CPU_INTERRUPT_NMI CPU_INTERRUPT_TGT_EXT_3
167 #define NB_MMU_MODES 2
169 struct CPUNios2State
{
170 uint32_t regs
[NUM_CORE_REGS
];
172 #if !defined(CONFIG_USER_ONLY)
175 uint32_t irq_pending
;
183 * @env: #CPUNios2State
187 typedef struct Nios2CPU
{
194 uint32_t pid_num_bits
;
195 uint32_t tlb_num_ways
;
196 uint32_t tlb_num_entries
;
198 /* Addresses that are hard-coded in the FPGA build settings */
200 uint32_t exception_addr
;
201 uint32_t fast_tlb_miss_addr
;
204 static inline Nios2CPU
*nios2_env_get_cpu(CPUNios2State
*env
)
206 return NIOS2_CPU(container_of(env
, Nios2CPU
, env
));
209 #define ENV_GET_CPU(e) CPU(nios2_env_get_cpu(e))
211 #define ENV_OFFSET offsetof(Nios2CPU, env)
213 void nios2_tcg_init(void);
214 void nios2_cpu_do_interrupt(CPUState
*cs
);
215 int cpu_nios2_signal_handler(int host_signum
, void *pinfo
, void *puc
);
216 void dump_mmu(FILE *f
, fprintf_function cpu_fprintf
, CPUNios2State
*env
);
217 void nios2_cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
219 hwaddr
nios2_cpu_get_phys_page_debug(CPUState
*cpu
, vaddr addr
);
220 void nios2_cpu_do_unaligned_access(CPUState
*cpu
, vaddr addr
,
221 MMUAccessType access_type
,
222 int mmu_idx
, uintptr_t retaddr
);
224 qemu_irq
*nios2_cpu_pic_init(Nios2CPU
*cpu
);
225 void nios2_check_interrupts(CPUNios2State
*env
);
227 #define TARGET_PHYS_ADDR_SPACE_BITS 32
228 #ifdef CONFIG_USER_ONLY
229 # define TARGET_VIRT_ADDR_SPACE_BITS 31
231 # define TARGET_VIRT_ADDR_SPACE_BITS 32
234 #define cpu_init(cpu_model) cpu_generic_init(TYPE_NIOS2_CPU, cpu_model)
236 #define cpu_gen_code cpu_nios2_gen_code
237 #define cpu_signal_handler cpu_nios2_signal_handler
239 #define CPU_SAVE_VERSION 1
241 #define TARGET_PAGE_BITS 12
243 /* MMU modes definitions */
244 #define MMU_MODE0_SUFFIX _kernel
245 #define MMU_MODE1_SUFFIX _user
246 #define MMU_SUPERVISOR_IDX 0
247 #define MMU_USER_IDX 1
249 static inline int cpu_mmu_index(CPUNios2State
*env
, bool ifetch
)
251 return (env
->regs
[CR_STATUS
] & CR_STATUS_U
) ? MMU_USER_IDX
:
255 int nios2_cpu_handle_mmu_fault(CPUState
*env
, vaddr address
,
256 int rw
, int mmu_idx
);
258 static inline int cpu_interrupts_enabled(CPUNios2State
*env
)
260 return env
->regs
[CR_STATUS
] & CR_STATUS_PIE
;
263 #include "exec/cpu-all.h"
264 #include "exec/exec-all.h"
266 static inline void cpu_get_tb_cpu_state(CPUNios2State
*env
, target_ulong
*pc
,
267 target_ulong
*cs_base
, uint32_t *flags
)
269 *pc
= env
->regs
[R_PC
];
271 *flags
= (env
->regs
[CR_STATUS
] & (CR_STATUS_EH
| CR_STATUS_U
));
274 #endif /* CPU_NIOS2_H */