xref: /qemu/target/tricore/helper.c (revision fc524567087c2537b5103cdfc1d41e4f442892b6)
148e06fe0SBastian Koppelmann /*
248e06fe0SBastian Koppelmann  *  Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn
348e06fe0SBastian Koppelmann  *
448e06fe0SBastian Koppelmann  * This library is free software; you can redistribute it and/or
548e06fe0SBastian Koppelmann  * modify it under the terms of the GNU Lesser General Public
648e06fe0SBastian Koppelmann  * License as published by the Free Software Foundation; either
702754acdSThomas Huth  * version 2.1 of the License, or (at your option) any later version.
848e06fe0SBastian Koppelmann  *
948e06fe0SBastian Koppelmann  * This library is distributed in the hope that it will be useful,
1048e06fe0SBastian Koppelmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1148e06fe0SBastian Koppelmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1248e06fe0SBastian Koppelmann  * Lesser General Public License for more details.
1348e06fe0SBastian Koppelmann  *
1448e06fe0SBastian Koppelmann  * You should have received a copy of the GNU Lesser General Public
1548e06fe0SBastian Koppelmann  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1648e06fe0SBastian Koppelmann  */
1748e06fe0SBastian Koppelmann 
1861d9f32bSPeter Maydell #include "qemu/osdep.h"
19cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
20343cdf2cSBastian Koppelmann #include "hw/registerfields.h"
2148e06fe0SBastian Koppelmann #include "cpu.h"
22eb9b25c6SPhilippe Mathieu-Daudé #include "exec/cputlb.h"
23efe25c26SRichard Henderson #include "accel/tcg/cpu-mmu-index.h"
2474781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
25*9c2ff9cdSPierrick Bouvier #include "exec/target_page.h"
265f8ab000SAlex Bennée #include "fpu/softfloat-helpers.h"
270442428aSMarkus Armbruster #include "qemu/qemu-print.h"
2848e06fe0SBastian Koppelmann 
292d30267eSBastian Koppelmann enum {
302d30267eSBastian Koppelmann     TLBRET_DIRTY = -4,
312d30267eSBastian Koppelmann     TLBRET_INVALID = -3,
322d30267eSBastian Koppelmann     TLBRET_NOMATCH = -2,
332d30267eSBastian Koppelmann     TLBRET_BADADDR = -1,
342d30267eSBastian Koppelmann     TLBRET_MATCH = 0
352d30267eSBastian Koppelmann };
362d30267eSBastian Koppelmann 
get_physical_address(CPUTriCoreState * env,hwaddr * physical,int * prot,target_ulong address,MMUAccessType access_type,int mmu_idx)372d30267eSBastian Koppelmann static int get_physical_address(CPUTriCoreState *env, hwaddr *physical,
382d30267eSBastian Koppelmann                                 int *prot, target_ulong address,
395513b770SPhilippe Mathieu-Daudé                                 MMUAccessType access_type, int mmu_idx)
402d30267eSBastian Koppelmann {
412d30267eSBastian Koppelmann     int ret = TLBRET_MATCH;
422d30267eSBastian Koppelmann 
432d30267eSBastian Koppelmann     *physical = address & 0xFFFFFFFF;
442d30267eSBastian Koppelmann     *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
452d30267eSBastian Koppelmann 
462d30267eSBastian Koppelmann     return ret;
472d30267eSBastian Koppelmann }
48e00a56dbSBastian Koppelmann 
tricore_cpu_get_phys_page_debug(CPUState * cs,vaddr addr)49e00a56dbSBastian Koppelmann hwaddr tricore_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
50e00a56dbSBastian Koppelmann {
51e00a56dbSBastian Koppelmann     TriCoreCPU *cpu = TRICORE_CPU(cs);
52e00a56dbSBastian Koppelmann     hwaddr phys_addr;
53e00a56dbSBastian Koppelmann     int prot;
543b916140SRichard Henderson     int mmu_idx = cpu_mmu_index(cs, false);
55e00a56dbSBastian Koppelmann 
5627e46616SPhilippe Mathieu-Daudé     if (get_physical_address(&cpu->env, &phys_addr, &prot, addr,
5727e46616SPhilippe Mathieu-Daudé                              MMU_DATA_LOAD, mmu_idx)) {
58e00a56dbSBastian Koppelmann         return -1;
59e00a56dbSBastian Koppelmann     }
60e00a56dbSBastian Koppelmann     return phys_addr;
61e00a56dbSBastian Koppelmann }
622d30267eSBastian Koppelmann 
638b81968cSMichael Tokarev /* TODO: Add exception support */
raise_mmu_exception(CPUTriCoreState * env,target_ulong address,int rw,int tlb_error)642d30267eSBastian Koppelmann static void raise_mmu_exception(CPUTriCoreState *env, target_ulong address,
652d30267eSBastian Koppelmann                                 int rw, int tlb_error)
662d30267eSBastian Koppelmann {
672d30267eSBastian Koppelmann }
682d30267eSBastian Koppelmann 
tricore_cpu_tlb_fill(CPUState * cs,vaddr address,int size,MMUAccessType rw,int mmu_idx,bool probe,uintptr_t retaddr)6968d6eee7SRichard Henderson bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
7068d6eee7SRichard Henderson                           MMUAccessType rw, int mmu_idx,
7168d6eee7SRichard Henderson                           bool probe, uintptr_t retaddr)
7248e06fe0SBastian Koppelmann {
7339ac0bacSPhilippe Mathieu-Daudé     CPUTriCoreState *env = cpu_env(cs);
742d30267eSBastian Koppelmann     hwaddr physical;
752d30267eSBastian Koppelmann     int prot;
762d30267eSBastian Koppelmann     int ret = 0;
772d30267eSBastian Koppelmann 
782d30267eSBastian Koppelmann     rw &= 1;
792d30267eSBastian Koppelmann     ret = get_physical_address(env, &physical, &prot,
805513b770SPhilippe Mathieu-Daudé                                address, rw, mmu_idx);
8168d6eee7SRichard Henderson 
82e66d7414SPhilippe Mathieu-Daudé     qemu_log_mask(CPU_LOG_MMU, "%s address=0x%" VADDR_PRIx " ret %d physical "
83883f2c59SPhilippe Mathieu-Daudé                   HWADDR_FMT_plx " prot %d\n",
84e66d7414SPhilippe Mathieu-Daudé                   __func__, address, ret, physical, prot);
852d30267eSBastian Koppelmann 
862d30267eSBastian Koppelmann     if (ret == TLBRET_MATCH) {
872d30267eSBastian Koppelmann         tlb_set_page(cs, address & TARGET_PAGE_MASK,
882d30267eSBastian Koppelmann                      physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
892d30267eSBastian Koppelmann                      mmu_idx, TARGET_PAGE_SIZE);
9068d6eee7SRichard Henderson         return true;
9168d6eee7SRichard Henderson     } else {
9268d6eee7SRichard Henderson         assert(ret < 0);
9368d6eee7SRichard Henderson         if (probe) {
9468d6eee7SRichard Henderson             return false;
9568d6eee7SRichard Henderson         }
962d30267eSBastian Koppelmann         raise_mmu_exception(env, address, rw, ret);
9768d6eee7SRichard Henderson         cpu_loop_exit_restore(cs, retaddr);
9868d6eee7SRichard Henderson     }
992d30267eSBastian Koppelmann }
1002d30267eSBastian Koppelmann 
fpu_set_state(CPUTriCoreState * env)101996a729fSBastian Koppelmann void fpu_set_state(CPUTriCoreState *env)
102996a729fSBastian Koppelmann {
103ce64babdSBastian Koppelmann     switch (extract32(env->PSW, 24, 2)) {
104ce64babdSBastian Koppelmann     case 0:
105ce64babdSBastian Koppelmann         set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
106ce64babdSBastian Koppelmann         break;
107ce64babdSBastian Koppelmann     case 1:
108ce64babdSBastian Koppelmann         set_float_rounding_mode(float_round_up, &env->fp_status);
109ce64babdSBastian Koppelmann         break;
110ce64babdSBastian Koppelmann     case 2:
111ce64babdSBastian Koppelmann         set_float_rounding_mode(float_round_down, &env->fp_status);
112ce64babdSBastian Koppelmann         break;
113ce64babdSBastian Koppelmann     case 3:
114ce64babdSBastian Koppelmann         set_float_rounding_mode(float_round_to_zero, &env->fp_status);
115ce64babdSBastian Koppelmann         break;
116ce64babdSBastian Koppelmann     }
117ce64babdSBastian Koppelmann 
118996a729fSBastian Koppelmann     set_flush_inputs_to_zero(1, &env->fp_status);
119996a729fSBastian Koppelmann     set_flush_to_zero(1, &env->fp_status);
120815061b9SBastian Koppelmann     set_float_detect_tininess(float_tininess_before_rounding, &env->fp_status);
12128f13bccSPeter Maydell     set_float_ftz_detection(float_ftz_before_rounding, &env->fp_status);
122996a729fSBastian Koppelmann     set_default_nan_mode(1, &env->fp_status);
123d921f8fdSPeter Maydell     /* Default NaN pattern: sign bit clear, frac msb set */
124d921f8fdSPeter Maydell     set_float_default_nan_pattern(0b01000000, &env->fp_status);
125996a729fSBastian Koppelmann }
126996a729fSBastian Koppelmann 
psw_read(CPUTriCoreState * env)12748e06fe0SBastian Koppelmann uint32_t psw_read(CPUTriCoreState *env)
12848e06fe0SBastian Koppelmann {
12948e06fe0SBastian Koppelmann     /* clear all USB bits */
130ce64babdSBastian Koppelmann     env->PSW &= 0x7ffffff;
13148e06fe0SBastian Koppelmann     /* now set them from the cache */
13248e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_C != 0) << 31);
13348e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_V   & (1 << 31))  >> 1);
13448e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_SV  & (1 << 31))  >> 2);
13548e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_AV  & (1 << 31))  >> 3);
13648e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_SAV & (1 << 31))  >> 4);
13748e06fe0SBastian Koppelmann 
13848e06fe0SBastian Koppelmann     return env->PSW;
13948e06fe0SBastian Koppelmann }
14048e06fe0SBastian Koppelmann 
psw_write(CPUTriCoreState * env,uint32_t val)14148e06fe0SBastian Koppelmann void psw_write(CPUTriCoreState *env, uint32_t val)
14248e06fe0SBastian Koppelmann {
14348e06fe0SBastian Koppelmann     env->PSW_USB_C = (val & MASK_USB_C);
1445dc1fbaeSBastian Koppelmann     env->PSW_USB_V = (val & MASK_USB_V) << 1;
1455dc1fbaeSBastian Koppelmann     env->PSW_USB_SV = (val & MASK_USB_SV) << 2;
1465dc1fbaeSBastian Koppelmann     env->PSW_USB_AV = (val & MASK_USB_AV) << 3;
1475dc1fbaeSBastian Koppelmann     env->PSW_USB_SAV = (val & MASK_USB_SAV) << 4;
14848e06fe0SBastian Koppelmann     env->PSW = val;
149996a729fSBastian Koppelmann 
150996a729fSBastian Koppelmann     fpu_set_state(env);
15148e06fe0SBastian Koppelmann }
152343cdf2cSBastian Koppelmann 
153343cdf2cSBastian Koppelmann #define FIELD_GETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE)     \
154343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env)                             \
155343cdf2cSBastian Koppelmann {                                                                \
1568c0e8ed3SBastian Koppelmann     if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) {   \
157343cdf2cSBastian Koppelmann         return FIELD_EX32(env->REG, REG, FIELD ## _ ## FEATURE); \
158343cdf2cSBastian Koppelmann     }                                                            \
159343cdf2cSBastian Koppelmann     return FIELD_EX32(env->REG, REG, FIELD ## _13);              \
160343cdf2cSBastian Koppelmann }
161343cdf2cSBastian Koppelmann 
162343cdf2cSBastian Koppelmann #define FIELD_GETTER(NAME, REG, FIELD)       \
163343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env)         \
164343cdf2cSBastian Koppelmann {                                            \
165343cdf2cSBastian Koppelmann     return FIELD_EX32(env->REG, REG, FIELD); \
166343cdf2cSBastian Koppelmann }
167343cdf2cSBastian Koppelmann 
168343cdf2cSBastian Koppelmann #define FIELD_SETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE)              \
169343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val)                            \
170343cdf2cSBastian Koppelmann {                                                                         \
1718c0e8ed3SBastian Koppelmann     if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) {            \
172343cdf2cSBastian Koppelmann         env->REG = FIELD_DP32(env->REG, REG, FIELD ## _ ## FEATURE, val); \
173343cdf2cSBastian Koppelmann     }                                                                     \
174343cdf2cSBastian Koppelmann     env->REG = FIELD_DP32(env->REG, REG, FIELD ## _13, val);              \
175343cdf2cSBastian Koppelmann }
176343cdf2cSBastian Koppelmann 
177343cdf2cSBastian Koppelmann #define FIELD_SETTER(NAME, REG, FIELD)                \
178343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val)        \
179343cdf2cSBastian Koppelmann {                                                     \
180343cdf2cSBastian Koppelmann     env->REG = FIELD_DP32(env->REG, REG, FIELD, val); \
181343cdf2cSBastian Koppelmann }
182343cdf2cSBastian Koppelmann 
183343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pcpn, PCXI, PCPN, 161)
184343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pcpn, PCXI, PCPN, 161)
185343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pie, PCXI, PIE, 161)
186343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pie, PCXI, PIE, 161)
187343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_ul, PCXI, UL, 161)
188343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_ul, PCXI, UL, 161)
189343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxs, PCXI, PCXS)
190343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxo, PCXI, PCXO)
191343cdf2cSBastian Koppelmann 
192343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(icr_get_ie, ICR, IE, 161)
193343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(icr_set_ie, ICR, IE, 161)
194343cdf2cSBastian Koppelmann FIELD_GETTER(icr_get_ccpn, ICR, CCPN)
195343cdf2cSBastian Koppelmann FIELD_SETTER(icr_set_ccpn, ICR, CCPN)
196