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" 22*eb9b25c6SPhilippe Mathieu-Daudé #include "exec/cputlb.h" 2374781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h" 245f8ab000SAlex Bennée #include "fpu/softfloat-helpers.h" 250442428aSMarkus Armbruster #include "qemu/qemu-print.h" 2648e06fe0SBastian Koppelmann 272d30267eSBastian Koppelmann enum { 282d30267eSBastian Koppelmann TLBRET_DIRTY = -4, 292d30267eSBastian Koppelmann TLBRET_INVALID = -3, 302d30267eSBastian Koppelmann TLBRET_NOMATCH = -2, 312d30267eSBastian Koppelmann TLBRET_BADADDR = -1, 322d30267eSBastian Koppelmann TLBRET_MATCH = 0 332d30267eSBastian Koppelmann }; 342d30267eSBastian Koppelmann 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; 523b916140SRichard Henderson int mmu_idx = cpu_mmu_index(cs, 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 618b81968cSMichael Tokarev /* TODO: Add exception 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 { 7139ac0bacSPhilippe Mathieu-Daudé CPUTriCoreState *env = cpu_env(cs); 722d30267eSBastian Koppelmann hwaddr physical; 732d30267eSBastian Koppelmann int prot; 742d30267eSBastian Koppelmann int ret = 0; 752d30267eSBastian Koppelmann 762d30267eSBastian Koppelmann rw &= 1; 772d30267eSBastian Koppelmann ret = get_physical_address(env, &physical, &prot, 785513b770SPhilippe Mathieu-Daudé address, rw, mmu_idx); 7968d6eee7SRichard Henderson 80e66d7414SPhilippe Mathieu-Daudé qemu_log_mask(CPU_LOG_MMU, "%s address=0x%" VADDR_PRIx " ret %d physical " 81883f2c59SPhilippe Mathieu-Daudé HWADDR_FMT_plx " prot %d\n", 82e66d7414SPhilippe Mathieu-Daudé __func__, address, ret, physical, prot); 832d30267eSBastian Koppelmann 842d30267eSBastian Koppelmann if (ret == TLBRET_MATCH) { 852d30267eSBastian Koppelmann tlb_set_page(cs, address & TARGET_PAGE_MASK, 862d30267eSBastian Koppelmann physical & TARGET_PAGE_MASK, prot | PAGE_EXEC, 872d30267eSBastian Koppelmann mmu_idx, TARGET_PAGE_SIZE); 8868d6eee7SRichard Henderson return true; 8968d6eee7SRichard Henderson } else { 9068d6eee7SRichard Henderson assert(ret < 0); 9168d6eee7SRichard Henderson if (probe) { 9268d6eee7SRichard Henderson return false; 9368d6eee7SRichard Henderson } 942d30267eSBastian Koppelmann raise_mmu_exception(env, address, rw, ret); 9568d6eee7SRichard Henderson cpu_loop_exit_restore(cs, retaddr); 9668d6eee7SRichard Henderson } 972d30267eSBastian Koppelmann } 982d30267eSBastian Koppelmann 99996a729fSBastian Koppelmann void fpu_set_state(CPUTriCoreState *env) 100996a729fSBastian Koppelmann { 101ce64babdSBastian Koppelmann switch (extract32(env->PSW, 24, 2)) { 102ce64babdSBastian Koppelmann case 0: 103ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_nearest_even, &env->fp_status); 104ce64babdSBastian Koppelmann break; 105ce64babdSBastian Koppelmann case 1: 106ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_up, &env->fp_status); 107ce64babdSBastian Koppelmann break; 108ce64babdSBastian Koppelmann case 2: 109ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_down, &env->fp_status); 110ce64babdSBastian Koppelmann break; 111ce64babdSBastian Koppelmann case 3: 112ce64babdSBastian Koppelmann set_float_rounding_mode(float_round_to_zero, &env->fp_status); 113ce64babdSBastian Koppelmann break; 114ce64babdSBastian Koppelmann } 115ce64babdSBastian Koppelmann 116996a729fSBastian Koppelmann set_flush_inputs_to_zero(1, &env->fp_status); 117996a729fSBastian Koppelmann set_flush_to_zero(1, &env->fp_status); 118815061b9SBastian Koppelmann set_float_detect_tininess(float_tininess_before_rounding, &env->fp_status); 11928f13bccSPeter Maydell set_float_ftz_detection(float_ftz_before_rounding, &env->fp_status); 120996a729fSBastian Koppelmann set_default_nan_mode(1, &env->fp_status); 121d921f8fdSPeter Maydell /* Default NaN pattern: sign bit clear, frac msb set */ 122d921f8fdSPeter Maydell set_float_default_nan_pattern(0b01000000, &env->fp_status); 123996a729fSBastian Koppelmann } 124996a729fSBastian Koppelmann 12548e06fe0SBastian Koppelmann uint32_t psw_read(CPUTriCoreState *env) 12648e06fe0SBastian Koppelmann { 12748e06fe0SBastian Koppelmann /* clear all USB bits */ 128ce64babdSBastian Koppelmann env->PSW &= 0x7ffffff; 12948e06fe0SBastian Koppelmann /* now set them from the cache */ 13048e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_C != 0) << 31); 13148e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_V & (1 << 31)) >> 1); 13248e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_SV & (1 << 31)) >> 2); 13348e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_AV & (1 << 31)) >> 3); 13448e06fe0SBastian Koppelmann env->PSW |= ((env->PSW_USB_SAV & (1 << 31)) >> 4); 13548e06fe0SBastian Koppelmann 13648e06fe0SBastian Koppelmann return env->PSW; 13748e06fe0SBastian Koppelmann } 13848e06fe0SBastian Koppelmann 13948e06fe0SBastian Koppelmann void psw_write(CPUTriCoreState *env, uint32_t val) 14048e06fe0SBastian Koppelmann { 14148e06fe0SBastian Koppelmann env->PSW_USB_C = (val & MASK_USB_C); 1425dc1fbaeSBastian Koppelmann env->PSW_USB_V = (val & MASK_USB_V) << 1; 1435dc1fbaeSBastian Koppelmann env->PSW_USB_SV = (val & MASK_USB_SV) << 2; 1445dc1fbaeSBastian Koppelmann env->PSW_USB_AV = (val & MASK_USB_AV) << 3; 1455dc1fbaeSBastian Koppelmann env->PSW_USB_SAV = (val & MASK_USB_SAV) << 4; 14648e06fe0SBastian Koppelmann env->PSW = val; 147996a729fSBastian Koppelmann 148996a729fSBastian Koppelmann fpu_set_state(env); 14948e06fe0SBastian Koppelmann } 150343cdf2cSBastian Koppelmann 151343cdf2cSBastian Koppelmann #define FIELD_GETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE) \ 152343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env) \ 153343cdf2cSBastian Koppelmann { \ 1548c0e8ed3SBastian Koppelmann if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) { \ 155343cdf2cSBastian Koppelmann return FIELD_EX32(env->REG, REG, FIELD ## _ ## FEATURE); \ 156343cdf2cSBastian Koppelmann } \ 157343cdf2cSBastian Koppelmann return FIELD_EX32(env->REG, REG, FIELD ## _13); \ 158343cdf2cSBastian Koppelmann } 159343cdf2cSBastian Koppelmann 160343cdf2cSBastian Koppelmann #define FIELD_GETTER(NAME, REG, FIELD) \ 161343cdf2cSBastian Koppelmann uint32_t NAME(CPUTriCoreState *env) \ 162343cdf2cSBastian Koppelmann { \ 163343cdf2cSBastian Koppelmann return FIELD_EX32(env->REG, REG, FIELD); \ 164343cdf2cSBastian Koppelmann } 165343cdf2cSBastian Koppelmann 166343cdf2cSBastian Koppelmann #define FIELD_SETTER_WITH_FEATURE(NAME, REG, FIELD, FEATURE) \ 167343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val) \ 168343cdf2cSBastian Koppelmann { \ 1698c0e8ed3SBastian Koppelmann if (tricore_has_feature(env, TRICORE_FEATURE_##FEATURE)) { \ 170343cdf2cSBastian Koppelmann env->REG = FIELD_DP32(env->REG, REG, FIELD ## _ ## FEATURE, val); \ 171343cdf2cSBastian Koppelmann } \ 172343cdf2cSBastian Koppelmann env->REG = FIELD_DP32(env->REG, REG, FIELD ## _13, val); \ 173343cdf2cSBastian Koppelmann } 174343cdf2cSBastian Koppelmann 175343cdf2cSBastian Koppelmann #define FIELD_SETTER(NAME, REG, FIELD) \ 176343cdf2cSBastian Koppelmann void NAME(CPUTriCoreState *env, uint32_t val) \ 177343cdf2cSBastian Koppelmann { \ 178343cdf2cSBastian Koppelmann env->REG = FIELD_DP32(env->REG, REG, FIELD, val); \ 179343cdf2cSBastian Koppelmann } 180343cdf2cSBastian Koppelmann 181343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pcpn, PCXI, PCPN, 161) 182343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pcpn, PCXI, PCPN, 161) 183343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_pie, PCXI, PIE, 161) 184343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_pie, PCXI, PIE, 161) 185343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(pcxi_get_ul, PCXI, UL, 161) 186343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(pcxi_set_ul, PCXI, UL, 161) 187343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxs, PCXI, PCXS) 188343cdf2cSBastian Koppelmann FIELD_GETTER(pcxi_get_pcxo, PCXI, PCXO) 189343cdf2cSBastian Koppelmann 190343cdf2cSBastian Koppelmann FIELD_GETTER_WITH_FEATURE(icr_get_ie, ICR, IE, 161) 191343cdf2cSBastian Koppelmann FIELD_SETTER_WITH_FEATURE(icr_set_ie, ICR, IE, 161) 192343cdf2cSBastian Koppelmann FIELD_GETTER(icr_get_ccpn, ICR, CCPN) 193343cdf2cSBastian Koppelmann FIELD_SETTER(icr_set_ccpn, ICR, CCPN) 194