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" 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 static int get_physical_address(CPUTriCoreState *env, hwaddr *physical, 352d30267eSBastian Koppelmann int *prot, target_ulong address, 365513b770SPhilippe 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; 513b916140SRichard Henderson int mmu_idx = cpu_mmu_index(cs, 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 608b81968cSMichael Tokarev /* TODO: Add exception support */ 612d30267eSBastian Koppelmann static void raise_mmu_exception(CPUTriCoreState *env, target_ulong address, 622d30267eSBastian Koppelmann int rw, int tlb_error) 632d30267eSBastian Koppelmann { 642d30267eSBastian Koppelmann } 652d30267eSBastian Koppelmann 6668d6eee7SRichard Henderson bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size, 6768d6eee7SRichard Henderson MMUAccessType rw, int mmu_idx, 6868d6eee7SRichard Henderson bool probe, uintptr_t retaddr) 6948e06fe0SBastian Koppelmann { 70*39ac0bacSPhilippe Mathieu-Daudé CPUTriCoreState *env = cpu_env(cs); 712d30267eSBastian Koppelmann hwaddr physical; 722d30267eSBastian Koppelmann int prot; 732d30267eSBastian Koppelmann int ret = 0; 742d30267eSBastian Koppelmann 752d30267eSBastian Koppelmann rw &= 1; 762d30267eSBastian Koppelmann ret = get_physical_address(env, &physical, &prot, 775513b770SPhilippe Mathieu-Daudé address, rw, mmu_idx); 7868d6eee7SRichard Henderson 7968d6eee7SRichard Henderson qemu_log_mask(CPU_LOG_MMU, "%s address=" TARGET_FMT_lx " ret %d physical " 80883f2c59SPhilippe Mathieu-Daudé HWADDR_FMT_plx " prot %d\n", 8168d6eee7SRichard Henderson __func__, (target_ulong)address, ret, physical, prot); 822d30267eSBastian Koppelmann 832d30267eSBastian Koppelmann if (ret == TLBRET_MATCH) { 842d30267eSBastian Koppelmann tlb_set_page(cs, address & TARGET_PAGE_MASK, 852d30267eSBastian Koppelmann physical & TARGET_PAGE_MASK, prot | PAGE_EXEC, 862d30267eSBastian Koppelmann mmu_idx, TARGET_PAGE_SIZE); 8768d6eee7SRichard Henderson return true; 8868d6eee7SRichard Henderson } else { 8968d6eee7SRichard Henderson assert(ret < 0); 9068d6eee7SRichard Henderson if (probe) { 9168d6eee7SRichard Henderson return false; 9268d6eee7SRichard Henderson } 932d30267eSBastian Koppelmann raise_mmu_exception(env, address, rw, ret); 9468d6eee7SRichard Henderson cpu_loop_exit_restore(cs, retaddr); 9568d6eee7SRichard Henderson } 962d30267eSBastian Koppelmann } 972d30267eSBastian Koppelmann 98996a729fSBastian Koppelmann void fpu_set_state(CPUTriCoreState *env) 99996a729fSBastian Koppelmann { 100ce64babdSBastian Koppelmann switch (extract32(env->PSW, 24, 2)) { 101ce64babdSBastian Koppelmann case 0: 102ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_nearest_even, &env->fp_status); 103ce64babdSBastian Koppelmann break; 104ce64babdSBastian Koppelmann case 1: 105ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_up, &env->fp_status); 106ce64babdSBastian Koppelmann break; 107ce64babdSBastian Koppelmann case 2: 108ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_down, &env->fp_status); 109ce64babdSBastian Koppelmann break; 110ce64babdSBastian Koppelmann case 3: 111ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_to_zero, &env->fp_status); 112ce64babdSBastian Koppelmann break; 113ce64babdSBastian Koppelmann } 114ce64babdSBastian Koppelmann 115996a729fSBastian Koppelmann set_flush_inputs_to_zero(1, &env->fp_status); 116996a729fSBastian Koppelmann set_flush_to_zero(1, &env->fp_status); 117815061b9SBastian Koppelmann set_float_detect_tininess(float_tininess_before_rounding, &env->fp_status); 118996a729fSBastian Koppelmann set_default_nan_mode(1, &env->fp_status); 119996a729fSBastian Koppelmann } 120996a729fSBastian Koppelmann 12148e06fe0SBastian Koppelmann uint32_t psw_read(CPUTriCoreState *env) 12248e06fe0SBastian Koppelmann { 12348e06fe0SBastian Koppelmann /* clear all USB bits */ 124ce64babdSBastian Koppelmann env->PSW &= 0x7ffffff; 12548e06fe0SBastian Koppelmann /* now set them from the cache */ 12648e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_C != 0) << 31); 12748e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_V & (1 << 31)) >> 1); 12848e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_SV & (1 << 31)) >> 2); 12948e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_AV & (1 << 31)) >> 3); 13048e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_SAV & (1 << 31)) >> 4); 13148e06fe0SBastian Koppelmann 13248e06fe0SBastian Koppelmann return env->PSW; 13348e06fe0SBastian Koppelmann } 13448e06fe0SBastian Koppelmann 13548e06fe0SBastian Koppelmann void psw_write(CPUTriCoreState *env, uint32_t val) 13648e06fe0SBastian Koppelmann { 13748e06fe0SBastian Koppelmann env->PSW_USB_C = (val & MASK_USB_C); 1385dc1fbaeSBastian Koppelmann env->PSW_USB_V = (val & MASK_USB_V) << 1; 1395dc1fbaeSBastian Koppelmann env->PSW_USB_SV = (val & MASK_USB_SV) << 2; 1405dc1fbaeSBastian Koppelmann env->PSW_USB_AV = (val & MASK_USB_AV) << 3; 1415dc1fbaeSBastian Koppelmann env->PSW_USB_SAV = (val & MASK_USB_SAV) << 4; 14248e06fe0SBastian Koppelmann env->PSW = val; 143996a729fSBastian Koppelmann 144996a729fSBastian Koppelmann fpu_set_state(env); 14548e06fe0SBastian Koppelmann } 146343cdf2cSBastian Koppelmann 147343cdf2cSBastian Koppelmann #define FIELD_GETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE) \ 148343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env) \ 149343cdf2cSBastian Koppelmann { \ 1508c0e8ed3SBastian Koppelmann if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) { \ 151343cdf2cSBastian Koppelmann return FIELD_EX32(env->REG, REG, FIELD ## _ ## FEATURE); \ 152343cdf2cSBastian Koppelmann } \ 153343cdf2cSBastian Koppelmann return FIELD_EX32(env->REG, REG, FIELD ## _13); \ 154343cdf2cSBastian Koppelmann } 155343cdf2cSBastian Koppelmann 156343cdf2cSBastian Koppelmann #define FIELD_GETTER(NAME, REG, FIELD) \ 157343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env) \ 158343cdf2cSBastian Koppelmann { \ 159343cdf2cSBastian Koppelmann return FIELD_EX32(env->REG, REG, FIELD); \ 160343cdf2cSBastian Koppelmann } 161343cdf2cSBastian Koppelmann 162343cdf2cSBastian Koppelmann #define FIELD_SETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE) \ 163343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val) \ 164343cdf2cSBastian Koppelmann { \ 1658c0e8ed3SBastian Koppelmann if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) { \ 166343cdf2cSBastian Koppelmann env->REG = FIELD_DP32(env->REG, REG, FIELD ## _ ## FEATURE, val); \ 167343cdf2cSBastian Koppelmann } \ 168343cdf2cSBastian Koppelmann env->REG = FIELD_DP32(env->REG, REG, FIELD ## _13, val); \ 169343cdf2cSBastian Koppelmann } 170343cdf2cSBastian Koppelmann 171343cdf2cSBastian Koppelmann #define FIELD_SETTER(NAME, REG, FIELD) \ 172343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val) \ 173343cdf2cSBastian Koppelmann { \ 174343cdf2cSBastian Koppelmann env->REG = FIELD_DP32(env->REG, REG, FIELD, val); \ 175343cdf2cSBastian Koppelmann } 176343cdf2cSBastian Koppelmann 177343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pcpn, PCXI, PCPN, 161) 178343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pcpn, PCXI, PCPN, 161) 179343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pie, PCXI, PIE, 161) 180343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pie, PCXI, PIE, 161) 181343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_ul, PCXI, UL, 161) 182343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_ul, PCXI, UL, 161) 183343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxs, PCXI, PCXS) 184343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxo, PCXI, PCXO) 185343cdf2cSBastian Koppelmann 186343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(icr_get_ie, ICR, IE, 161) 187343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(icr_set_ie, ICR, IE, 161) 188343cdf2cSBastian Koppelmann FIELD_GETTER(icr_get_ccpn, ICR, CCPN) 189343cdf2cSBastian Koppelmann FIELD_SETTER(icr_set_ccpn, ICR, CCPN) 190