xref: /qemu/target/tricore/helper.c (revision 343cdf2c9ad6aef2fbf66cf9c888d20c1de5e784)
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"
20*343cdf2cSBastian Koppelmann #include "hw/registerfields.h"
2148e06fe0SBastian Koppelmann #include "cpu.h"
2263c91552SPaolo Bonzini #include "exec/exec-all.h"
235f8ab000SAlex Bennée #include "fpu/softfloat-helpers.h"
240442428aSMarkus Armbruster #include "qemu/qemu-print.h"
2548e06fe0SBastian Koppelmann 
262d30267eSBastian Koppelmann enum {
272d30267eSBastian Koppelmann     TLBRET_DIRTY = -4,
282d30267eSBastian Koppelmann     TLBRET_INVALID = -3,
292d30267eSBastian Koppelmann     TLBRET_NOMATCH = -2,
302d30267eSBastian Koppelmann     TLBRET_BADADDR = -1,
312d30267eSBastian Koppelmann     TLBRET_MATCH = 0
322d30267eSBastian Koppelmann };
332d30267eSBastian Koppelmann 
342d30267eSBastian Koppelmann #if defined(CONFIG_SOFTMMU)
352d30267eSBastian Koppelmann static int get_physical_address(CPUTriCoreState *env, hwaddr *physical,
362d30267eSBastian Koppelmann                                 int *prot, target_ulong address,
375513b770SPhilippe Mathieu-Daudé                                 MMUAccessType access_type, int mmu_idx)
382d30267eSBastian Koppelmann {
392d30267eSBastian Koppelmann     int ret = TLBRET_MATCH;
402d30267eSBastian Koppelmann 
412d30267eSBastian Koppelmann     *physical = address & 0xFFFFFFFF;
422d30267eSBastian Koppelmann     *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
432d30267eSBastian Koppelmann 
442d30267eSBastian Koppelmann     return ret;
452d30267eSBastian Koppelmann }
46e00a56dbSBastian Koppelmann 
47e00a56dbSBastian Koppelmann hwaddr tricore_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
48e00a56dbSBastian Koppelmann {
49e00a56dbSBastian Koppelmann     TriCoreCPU *cpu = TRICORE_CPU(cs);
50e00a56dbSBastian Koppelmann     hwaddr phys_addr;
51e00a56dbSBastian Koppelmann     int prot;
52e00a56dbSBastian Koppelmann     int mmu_idx = cpu_mmu_index(&cpu->env, false);
53e00a56dbSBastian Koppelmann 
5427e46616SPhilippe Mathieu-Daudé     if (get_physical_address(&cpu->env, &phys_addr, &prot, addr,
5527e46616SPhilippe Mathieu-Daudé                              MMU_DATA_LOAD, mmu_idx)) {
56e00a56dbSBastian Koppelmann         return -1;
57e00a56dbSBastian Koppelmann     }
58e00a56dbSBastian Koppelmann     return phys_addr;
59e00a56dbSBastian Koppelmann }
602d30267eSBastian Koppelmann #endif
612d30267eSBastian Koppelmann 
622d30267eSBastian Koppelmann /* TODO: Add exeption support*/
632d30267eSBastian Koppelmann static void raise_mmu_exception(CPUTriCoreState *env, target_ulong address,
642d30267eSBastian Koppelmann                                 int rw, int tlb_error)
652d30267eSBastian Koppelmann {
662d30267eSBastian Koppelmann }
672d30267eSBastian Koppelmann 
6868d6eee7SRichard Henderson bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
6968d6eee7SRichard Henderson                           MMUAccessType rw, int mmu_idx,
7068d6eee7SRichard Henderson                           bool probe, uintptr_t retaddr)
7148e06fe0SBastian Koppelmann {
722d30267eSBastian Koppelmann     TriCoreCPU *cpu = TRICORE_CPU(cs);
732d30267eSBastian Koppelmann     CPUTriCoreState *env = &cpu->env;
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 
8268d6eee7SRichard Henderson     qemu_log_mask(CPU_LOG_MMU, "%s address=" TARGET_FMT_lx " ret %d physical "
83883f2c59SPhilippe Mathieu-Daudé                   HWADDR_FMT_plx " prot %d\n",
8468d6eee7SRichard Henderson                   __func__, (target_ulong)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 
10148e06fe0SBastian Koppelmann static void tricore_cpu_list_entry(gpointer data, gpointer user_data)
10248e06fe0SBastian Koppelmann {
10348e06fe0SBastian Koppelmann     ObjectClass *oc = data;
10448e06fe0SBastian Koppelmann     const char *typename;
10548e06fe0SBastian Koppelmann     char *name;
10648e06fe0SBastian Koppelmann 
10748e06fe0SBastian Koppelmann     typename = object_class_get_name(oc);
10848e06fe0SBastian Koppelmann     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_TRICORE_CPU));
1090442428aSMarkus Armbruster     qemu_printf("  %s\n", name);
11048e06fe0SBastian Koppelmann     g_free(name);
11148e06fe0SBastian Koppelmann }
11248e06fe0SBastian Koppelmann 
1130442428aSMarkus Armbruster void tricore_cpu_list(void)
11448e06fe0SBastian Koppelmann {
11548e06fe0SBastian Koppelmann     GSList *list;
11648e06fe0SBastian Koppelmann 
11747c66009SPaolo Bonzini     list = object_class_get_list_sorted(TYPE_TRICORE_CPU, false);
1180442428aSMarkus Armbruster     qemu_printf("Available CPUs:\n");
1190442428aSMarkus Armbruster     g_slist_foreach(list, tricore_cpu_list_entry, NULL);
12048e06fe0SBastian Koppelmann     g_slist_free(list);
12148e06fe0SBastian Koppelmann }
12248e06fe0SBastian Koppelmann 
123996a729fSBastian Koppelmann void fpu_set_state(CPUTriCoreState *env)
124996a729fSBastian Koppelmann {
125996a729fSBastian Koppelmann     set_float_rounding_mode(env->PSW & MASK_PSW_FPU_RM, &env->fp_status);
126996a729fSBastian Koppelmann     set_flush_inputs_to_zero(1, &env->fp_status);
127996a729fSBastian Koppelmann     set_flush_to_zero(1, &env->fp_status);
128996a729fSBastian Koppelmann     set_default_nan_mode(1, &env->fp_status);
129996a729fSBastian Koppelmann }
130996a729fSBastian Koppelmann 
13148e06fe0SBastian Koppelmann uint32_t psw_read(CPUTriCoreState *env)
13248e06fe0SBastian Koppelmann {
13348e06fe0SBastian Koppelmann     /* clear all USB bits */
1341bd3e2fcSBastian Koppelmann     env->PSW &= 0x6ffffff;
13548e06fe0SBastian Koppelmann     /* now set them from the cache */
13648e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_C != 0) << 31);
13748e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_V   & (1 << 31))  >> 1);
13848e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_SV  & (1 << 31))  >> 2);
13948e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_AV  & (1 << 31))  >> 3);
14048e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_SAV & (1 << 31))  >> 4);
14148e06fe0SBastian Koppelmann 
14248e06fe0SBastian Koppelmann     return env->PSW;
14348e06fe0SBastian Koppelmann }
14448e06fe0SBastian Koppelmann 
14548e06fe0SBastian Koppelmann void psw_write(CPUTriCoreState *env, uint32_t val)
14648e06fe0SBastian Koppelmann {
14748e06fe0SBastian Koppelmann     env->PSW_USB_C = (val & MASK_USB_C);
1485dc1fbaeSBastian Koppelmann     env->PSW_USB_V = (val & MASK_USB_V) << 1;
1495dc1fbaeSBastian Koppelmann     env->PSW_USB_SV = (val & MASK_USB_SV) << 2;
1505dc1fbaeSBastian Koppelmann     env->PSW_USB_AV = (val & MASK_USB_AV) << 3;
1515dc1fbaeSBastian Koppelmann     env->PSW_USB_SAV = (val & MASK_USB_SAV) << 4;
15248e06fe0SBastian Koppelmann     env->PSW = val;
153996a729fSBastian Koppelmann 
154996a729fSBastian Koppelmann     fpu_set_state(env);
15548e06fe0SBastian Koppelmann }
156*343cdf2cSBastian Koppelmann 
157*343cdf2cSBastian Koppelmann #define FIELD_GETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE)     \
158*343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env)                             \
159*343cdf2cSBastian Koppelmann {                                                                \
160*343cdf2cSBastian Koppelmann     if (tricore_feature(env, TRICORE_FEATURE_##FEATURE)) {       \
161*343cdf2cSBastian Koppelmann         return FIELD_EX32(env->REG, REG, FIELD ## _ ## FEATURE); \
162*343cdf2cSBastian Koppelmann     }                                                            \
163*343cdf2cSBastian Koppelmann     return FIELD_EX32(env->REG, REG, FIELD ## _13);              \
164*343cdf2cSBastian Koppelmann }
165*343cdf2cSBastian Koppelmann 
166*343cdf2cSBastian Koppelmann #define FIELD_GETTER(NAME, REG, FIELD)       \
167*343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env)         \
168*343cdf2cSBastian Koppelmann {                                            \
169*343cdf2cSBastian Koppelmann     return FIELD_EX32(env->REG, REG, FIELD); \
170*343cdf2cSBastian Koppelmann }
171*343cdf2cSBastian Koppelmann 
172*343cdf2cSBastian Koppelmann #define FIELD_SETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE)              \
173*343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val)                            \
174*343cdf2cSBastian Koppelmann {                                                                         \
175*343cdf2cSBastian Koppelmann     if (tricore_feature(env, TRICORE_FEATURE_##FEATURE)) {                \
176*343cdf2cSBastian Koppelmann         env->REG = FIELD_DP32(env->REG, REG, FIELD ## _ ## FEATURE, val); \
177*343cdf2cSBastian Koppelmann     }                                                                     \
178*343cdf2cSBastian Koppelmann     env->REG = FIELD_DP32(env->REG, REG, FIELD ## _13, val);              \
179*343cdf2cSBastian Koppelmann }
180*343cdf2cSBastian Koppelmann 
181*343cdf2cSBastian Koppelmann #define FIELD_SETTER(NAME, REG, FIELD)                \
182*343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val)        \
183*343cdf2cSBastian Koppelmann {                                                     \
184*343cdf2cSBastian Koppelmann     env->REG = FIELD_DP32(env->REG, REG, FIELD, val); \
185*343cdf2cSBastian Koppelmann }
186*343cdf2cSBastian Koppelmann 
187*343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pcpn, PCXI, PCPN, 161)
188*343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pcpn, PCXI, PCPN, 161)
189*343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pie, PCXI, PIE, 161)
190*343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pie, PCXI, PIE, 161)
191*343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_ul, PCXI, UL, 161)
192*343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_ul, PCXI, UL, 161)
193*343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxs, PCXI, PCXS)
194*343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxo, PCXI, PCXO)
195*343cdf2cSBastian Koppelmann 
196*343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(icr_get_ie, ICR, IE, 161)
197*343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(icr_set_ie, ICR, IE, 161)
198*343cdf2cSBastian Koppelmann FIELD_GETTER(icr_get_ccpn, ICR, CCPN)
199*343cdf2cSBastian Koppelmann FIELD_SETTER(icr_set_ccpn, ICR, CCPN)
200