xref: /qemu/target/tricore/helper.c (revision 5513b7700c6cfa1beebcfcd20e9c7458d1ebd3ea)
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"
1948e06fe0SBastian Koppelmann 
2048e06fe0SBastian Koppelmann #include "cpu.h"
2163c91552SPaolo Bonzini #include "exec/exec-all.h"
225f8ab000SAlex Bennée #include "fpu/softfloat-helpers.h"
230442428aSMarkus Armbruster #include "qemu/qemu-print.h"
2448e06fe0SBastian Koppelmann 
252d30267eSBastian Koppelmann enum {
262d30267eSBastian Koppelmann     TLBRET_DIRTY = -4,
272d30267eSBastian Koppelmann     TLBRET_INVALID = -3,
282d30267eSBastian Koppelmann     TLBRET_NOMATCH = -2,
292d30267eSBastian Koppelmann     TLBRET_BADADDR = -1,
302d30267eSBastian Koppelmann     TLBRET_MATCH = 0
312d30267eSBastian Koppelmann };
322d30267eSBastian Koppelmann 
332d30267eSBastian Koppelmann #if defined(CONFIG_SOFTMMU)
342d30267eSBastian Koppelmann static int get_physical_address(CPUTriCoreState *env, hwaddr *physical,
352d30267eSBastian Koppelmann                                 int *prot, target_ulong address,
36*5513b770SPhilippe Mathieu-Daudé                                 MMUAccessType access_type, int mmu_idx)
372d30267eSBastian Koppelmann {
382d30267eSBastian Koppelmann     int ret = TLBRET_MATCH;
392d30267eSBastian Koppelmann 
402d30267eSBastian Koppelmann     *physical = address & 0xFFFFFFFF;
412d30267eSBastian Koppelmann     *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
422d30267eSBastian Koppelmann 
432d30267eSBastian Koppelmann     return ret;
442d30267eSBastian Koppelmann }
45e00a56dbSBastian Koppelmann 
46e00a56dbSBastian Koppelmann hwaddr tricore_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
47e00a56dbSBastian Koppelmann {
48e00a56dbSBastian Koppelmann     TriCoreCPU *cpu = TRICORE_CPU(cs);
49e00a56dbSBastian Koppelmann     hwaddr phys_addr;
50e00a56dbSBastian Koppelmann     int prot;
51e00a56dbSBastian Koppelmann     int mmu_idx = cpu_mmu_index(&cpu->env, false);
52e00a56dbSBastian Koppelmann 
5327e46616SPhilippe Mathieu-Daudé     if (get_physical_address(&cpu->env, &phys_addr, &prot, addr,
5427e46616SPhilippe Mathieu-Daudé                              MMU_DATA_LOAD, mmu_idx)) {
55e00a56dbSBastian Koppelmann         return -1;
56e00a56dbSBastian Koppelmann     }
57e00a56dbSBastian Koppelmann     return phys_addr;
58e00a56dbSBastian Koppelmann }
592d30267eSBastian Koppelmann #endif
602d30267eSBastian Koppelmann 
612d30267eSBastian Koppelmann /* TODO: Add exeption support*/
622d30267eSBastian Koppelmann static void raise_mmu_exception(CPUTriCoreState *env, target_ulong address,
632d30267eSBastian Koppelmann                                 int rw, int tlb_error)
642d30267eSBastian Koppelmann {
652d30267eSBastian Koppelmann }
662d30267eSBastian Koppelmann 
6768d6eee7SRichard Henderson bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
6868d6eee7SRichard Henderson                           MMUAccessType rw, int mmu_idx,
6968d6eee7SRichard Henderson                           bool probe, uintptr_t retaddr)
7048e06fe0SBastian Koppelmann {
712d30267eSBastian Koppelmann     TriCoreCPU *cpu = TRICORE_CPU(cs);
722d30267eSBastian Koppelmann     CPUTriCoreState *env = &cpu->env;
732d30267eSBastian Koppelmann     hwaddr physical;
742d30267eSBastian Koppelmann     int prot;
752d30267eSBastian Koppelmann     int ret = 0;
762d30267eSBastian Koppelmann 
772d30267eSBastian Koppelmann     rw &= 1;
782d30267eSBastian Koppelmann     ret = get_physical_address(env, &physical, &prot,
79*5513b770SPhilippe Mathieu-Daudé                                address, rw, mmu_idx);
8068d6eee7SRichard Henderson 
8168d6eee7SRichard Henderson     qemu_log_mask(CPU_LOG_MMU, "%s address=" TARGET_FMT_lx " ret %d physical "
8268d6eee7SRichard Henderson                   TARGET_FMT_plx " prot %d\n",
8368d6eee7SRichard Henderson                   __func__, (target_ulong)address, ret, physical, prot);
842d30267eSBastian Koppelmann 
852d30267eSBastian Koppelmann     if (ret == TLBRET_MATCH) {
862d30267eSBastian Koppelmann         tlb_set_page(cs, address & TARGET_PAGE_MASK,
872d30267eSBastian Koppelmann                      physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
882d30267eSBastian Koppelmann                      mmu_idx, TARGET_PAGE_SIZE);
8968d6eee7SRichard Henderson         return true;
9068d6eee7SRichard Henderson     } else {
9168d6eee7SRichard Henderson         assert(ret < 0);
9268d6eee7SRichard Henderson         if (probe) {
9368d6eee7SRichard Henderson             return false;
9468d6eee7SRichard Henderson         }
952d30267eSBastian Koppelmann         raise_mmu_exception(env, address, rw, ret);
9668d6eee7SRichard Henderson         cpu_loop_exit_restore(cs, retaddr);
9768d6eee7SRichard Henderson     }
982d30267eSBastian Koppelmann }
992d30267eSBastian Koppelmann 
10048e06fe0SBastian Koppelmann static void tricore_cpu_list_entry(gpointer data, gpointer user_data)
10148e06fe0SBastian Koppelmann {
10248e06fe0SBastian Koppelmann     ObjectClass *oc = data;
10348e06fe0SBastian Koppelmann     const char *typename;
10448e06fe0SBastian Koppelmann     char *name;
10548e06fe0SBastian Koppelmann 
10648e06fe0SBastian Koppelmann     typename = object_class_get_name(oc);
10748e06fe0SBastian Koppelmann     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_TRICORE_CPU));
1080442428aSMarkus Armbruster     qemu_printf("  %s\n", name);
10948e06fe0SBastian Koppelmann     g_free(name);
11048e06fe0SBastian Koppelmann }
11148e06fe0SBastian Koppelmann 
1120442428aSMarkus Armbruster void tricore_cpu_list(void)
11348e06fe0SBastian Koppelmann {
11448e06fe0SBastian Koppelmann     GSList *list;
11548e06fe0SBastian Koppelmann 
11647c66009SPaolo Bonzini     list = object_class_get_list_sorted(TYPE_TRICORE_CPU, false);
1170442428aSMarkus Armbruster     qemu_printf("Available CPUs:\n");
1180442428aSMarkus Armbruster     g_slist_foreach(list, tricore_cpu_list_entry, NULL);
11948e06fe0SBastian Koppelmann     g_slist_free(list);
12048e06fe0SBastian Koppelmann }
12148e06fe0SBastian Koppelmann 
122996a729fSBastian Koppelmann void fpu_set_state(CPUTriCoreState *env)
123996a729fSBastian Koppelmann {
124996a729fSBastian Koppelmann     set_float_rounding_mode(env->PSW & MASK_PSW_FPU_RM, &env->fp_status);
125996a729fSBastian Koppelmann     set_flush_inputs_to_zero(1, &env->fp_status);
126996a729fSBastian Koppelmann     set_flush_to_zero(1, &env->fp_status);
127996a729fSBastian Koppelmann     set_default_nan_mode(1, &env->fp_status);
128996a729fSBastian Koppelmann }
129996a729fSBastian Koppelmann 
13048e06fe0SBastian Koppelmann uint32_t psw_read(CPUTriCoreState *env)
13148e06fe0SBastian Koppelmann {
13248e06fe0SBastian Koppelmann     /* clear all USB bits */
1331bd3e2fcSBastian Koppelmann     env->PSW &= 0x6ffffff;
13448e06fe0SBastian Koppelmann     /* now set them from the cache */
13548e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_C != 0) << 31);
13648e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_V   & (1 << 31))  >> 1);
13748e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_SV  & (1 << 31))  >> 2);
13848e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_AV  & (1 << 31))  >> 3);
13948e06fe0SBastian Koppelmann     env->PSW |= ((env->PSW_USB_SAV & (1 << 31))  >> 4);
14048e06fe0SBastian Koppelmann 
14148e06fe0SBastian Koppelmann     return env->PSW;
14248e06fe0SBastian Koppelmann }
14348e06fe0SBastian Koppelmann 
14448e06fe0SBastian Koppelmann void psw_write(CPUTriCoreState *env, uint32_t val)
14548e06fe0SBastian Koppelmann {
14648e06fe0SBastian Koppelmann     env->PSW_USB_C = (val & MASK_USB_C);
1475dc1fbaeSBastian Koppelmann     env->PSW_USB_V = (val & MASK_USB_V) << 1;
1485dc1fbaeSBastian Koppelmann     env->PSW_USB_SV = (val & MASK_USB_SV) << 2;
1495dc1fbaeSBastian Koppelmann     env->PSW_USB_AV = (val & MASK_USB_AV) << 3;
1505dc1fbaeSBastian Koppelmann     env->PSW_USB_SAV = (val & MASK_USB_SAV) << 4;
15148e06fe0SBastian Koppelmann     env->PSW = val;
152996a729fSBastian Koppelmann 
153996a729fSBastian Koppelmann     fpu_set_state(env);
15448e06fe0SBastian Koppelmann }
155