xref: /openbmc/qemu/target/tricore/helper.c (revision 7d87775f)
1 /*
2  *  Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "qemu/osdep.h"
19 #include "qemu/log.h"
20 #include "hw/registerfields.h"
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "exec/page-protection.h"
24 #include "fpu/softfloat-helpers.h"
25 #include "qemu/qemu-print.h"
26 
27 enum {
28     TLBRET_DIRTY = -4,
29     TLBRET_INVALID = -3,
30     TLBRET_NOMATCH = -2,
31     TLBRET_BADADDR = -1,
32     TLBRET_MATCH = 0
33 };
34 
35 static int get_physical_address(CPUTriCoreState *env, hwaddr *physical,
36                                 int *prot, target_ulong address,
37                                 MMUAccessType access_type, int mmu_idx)
38 {
39     int ret = TLBRET_MATCH;
40 
41     *physical = address & 0xFFFFFFFF;
42     *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
43 
44     return ret;
45 }
46 
47 hwaddr tricore_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
48 {
49     TriCoreCPU *cpu = TRICORE_CPU(cs);
50     hwaddr phys_addr;
51     int prot;
52     int mmu_idx = cpu_mmu_index(cs, false);
53 
54     if (get_physical_address(&cpu->env, &phys_addr, &prot, addr,
55                              MMU_DATA_LOAD, mmu_idx)) {
56         return -1;
57     }
58     return phys_addr;
59 }
60 
61 /* TODO: Add exception support */
62 static void raise_mmu_exception(CPUTriCoreState *env, target_ulong address,
63                                 int rw, int tlb_error)
64 {
65 }
66 
67 bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
68                           MMUAccessType rw, int mmu_idx,
69                           bool probe, uintptr_t retaddr)
70 {
71     CPUTriCoreState *env = cpu_env(cs);
72     hwaddr physical;
73     int prot;
74     int ret = 0;
75 
76     rw &= 1;
77     ret = get_physical_address(env, &physical, &prot,
78                                address, rw, mmu_idx);
79 
80     qemu_log_mask(CPU_LOG_MMU, "%s address=0x%" VADDR_PRIx " ret %d physical "
81                   HWADDR_FMT_plx " prot %d\n",
82                   __func__, address, ret, physical, prot);
83 
84     if (ret == TLBRET_MATCH) {
85         tlb_set_page(cs, address & TARGET_PAGE_MASK,
86                      physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
87                      mmu_idx, TARGET_PAGE_SIZE);
88         return true;
89     } else {
90         assert(ret < 0);
91         if (probe) {
92             return false;
93         }
94         raise_mmu_exception(env, address, rw, ret);
95         cpu_loop_exit_restore(cs, retaddr);
96     }
97 }
98 
99 void fpu_set_state(CPUTriCoreState *env)
100 {
101     switch (extract32(env->PSW, 24, 2)) {
102     case 0:
103         set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
104         break;
105     case 1:
106         set_float_rounding_mode(float_round_up, &env->fp_status);
107         break;
108     case 2:
109         set_float_rounding_mode(float_round_down, &env->fp_status);
110         break;
111     case 3:
112         set_float_rounding_mode(float_round_to_zero, &env->fp_status);
113         break;
114     }
115 
116     set_flush_inputs_to_zero(1, &env->fp_status);
117     set_flush_to_zero(1, &env->fp_status);
118     set_float_detect_tininess(float_tininess_before_rounding, &env->fp_status);
119     set_default_nan_mode(1, &env->fp_status);
120 }
121 
122 uint32_t psw_read(CPUTriCoreState *env)
123 {
124     /* clear all USB bits */
125     env->PSW &= 0x7ffffff;
126     /* now set them from the cache */
127     env->PSW |= ((env->PSW_USB_C != 0) << 31);
128     env->PSW |= ((env->PSW_USB_V   & (1 << 31))  >> 1);
129     env->PSW |= ((env->PSW_USB_SV  & (1 << 31))  >> 2);
130     env->PSW |= ((env->PSW_USB_AV  & (1 << 31))  >> 3);
131     env->PSW |= ((env->PSW_USB_SAV & (1 << 31))  >> 4);
132 
133     return env->PSW;
134 }
135 
136 void psw_write(CPUTriCoreState *env, uint32_t val)
137 {
138     env->PSW_USB_C = (val & MASK_USB_C);
139     env->PSW_USB_V = (val & MASK_USB_V) << 1;
140     env->PSW_USB_SV = (val & MASK_USB_SV) << 2;
141     env->PSW_USB_AV = (val & MASK_USB_AV) << 3;
142     env->PSW_USB_SAV = (val & MASK_USB_SAV) << 4;
143     env->PSW = val;
144 
145     fpu_set_state(env);
146 }
147 
148 #define FIELD_GETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE)     \
149 uint32_t NAME(CPUTriCoreState *env)                             \
150 {                                                                \
151     if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) {   \
152         return FIELD_EX32(env->REG, REG, FIELD ## _ ## FEATURE); \
153     }                                                            \
154     return FIELD_EX32(env->REG, REG, FIELD ## _13);              \
155 }
156 
157 #define FIELD_GETTER(NAME, REG, FIELD)       \
158 uint32_t NAME(CPUTriCoreState *env)         \
159 {                                            \
160     return FIELD_EX32(env->REG, REG, FIELD); \
161 }
162 
163 #define FIELD_SETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE)              \
164 void NAME(CPUTriCoreState *env, uint32_t val)                            \
165 {                                                                         \
166     if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) {            \
167         env->REG = FIELD_DP32(env->REG, REG, FIELD ## _ ## FEATURE, val); \
168     }                                                                     \
169     env->REG = FIELD_DP32(env->REG, REG, FIELD ## _13, val);              \
170 }
171 
172 #define FIELD_SETTER(NAME, REG, FIELD)                \
173 void NAME(CPUTriCoreState *env, uint32_t val)        \
174 {                                                     \
175     env->REG = FIELD_DP32(env->REG, REG, FIELD, val); \
176 }
177 
178 FIELD_GETTER_WITH_FEATURE(pcxi_get_pcpn, PCXI, PCPN, 161)
179 FIELD_SETTER_WITH_FEATURE(pcxi_set_pcpn, PCXI, PCPN, 161)
180 FIELD_GETTER_WITH_FEATURE(pcxi_get_pie, PCXI, PIE, 161)
181 FIELD_SETTER_WITH_FEATURE(pcxi_set_pie, PCXI, PIE, 161)
182 FIELD_GETTER_WITH_FEATURE(pcxi_get_ul, PCXI, UL, 161)
183 FIELD_SETTER_WITH_FEATURE(pcxi_set_ul, PCXI, UL, 161)
184 FIELD_GETTER(pcxi_get_pcxs, PCXI, PCXS)
185 FIELD_GETTER(pcxi_get_pcxo, PCXI, PCXO)
186 
187 FIELD_GETTER_WITH_FEATURE(icr_get_ie, ICR, IE, 161)
188 FIELD_SETTER_WITH_FEATURE(icr_set_ie, ICR, IE, 161)
189 FIELD_GETTER(icr_get_ccpn, ICR, CCPN)
190 FIELD_SETTER(icr_set_ccpn, ICR, CCPN)
191