17e5e5a63SMax Filippov /*
27e5e5a63SMax Filippov * Copyright (c) 2011 - 2019, Max Filippov, Open Source and Linux Lab.
37e5e5a63SMax Filippov * All rights reserved.
47e5e5a63SMax Filippov *
57e5e5a63SMax Filippov * Redistribution and use in source and binary forms, with or without
67e5e5a63SMax Filippov * modification, are permitted provided that the following conditions are met:
77e5e5a63SMax Filippov * * Redistributions of source code must retain the above copyright
87e5e5a63SMax Filippov * notice, this list of conditions and the following disclaimer.
97e5e5a63SMax Filippov * * Redistributions in binary form must reproduce the above copyright
107e5e5a63SMax Filippov * notice, this list of conditions and the following disclaimer in the
117e5e5a63SMax Filippov * documentation and/or other materials provided with the distribution.
127e5e5a63SMax Filippov * * Neither the name of the Open Source and Linux Lab nor the
137e5e5a63SMax Filippov * names of its contributors may be used to endorse or promote products
147e5e5a63SMax Filippov * derived from this software without specific prior written permission.
157e5e5a63SMax Filippov *
167e5e5a63SMax Filippov * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
177e5e5a63SMax Filippov * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
187e5e5a63SMax Filippov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197e5e5a63SMax Filippov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
207e5e5a63SMax Filippov * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
217e5e5a63SMax Filippov * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
227e5e5a63SMax Filippov * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
237e5e5a63SMax Filippov * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247e5e5a63SMax Filippov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
257e5e5a63SMax Filippov * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267e5e5a63SMax Filippov */
277e5e5a63SMax Filippov
287e5e5a63SMax Filippov #include "qemu/osdep.h"
29cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
30fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
317e5e5a63SMax Filippov #include "qemu/units.h"
327e5e5a63SMax Filippov #include "cpu.h"
337e5e5a63SMax Filippov #include "exec/helper-proto.h"
347e5e5a63SMax Filippov #include "qemu/host-utils.h"
357e5e5a63SMax Filippov #include "exec/exec-all.h"
3674781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
377e5e5a63SMax Filippov
384d04ea35SMax Filippov #define XTENSA_MPU_SEGMENT_MASK 0x0000001f
394d04ea35SMax Filippov #define XTENSA_MPU_ACC_RIGHTS_MASK 0x00000f00
404d04ea35SMax Filippov #define XTENSA_MPU_ACC_RIGHTS_SHIFT 8
414d04ea35SMax Filippov #define XTENSA_MPU_MEM_TYPE_MASK 0x001ff000
424d04ea35SMax Filippov #define XTENSA_MPU_MEM_TYPE_SHIFT 12
434d04ea35SMax Filippov #define XTENSA_MPU_ATTR_MASK 0x001fff00
444d04ea35SMax Filippov
454d04ea35SMax Filippov #define XTENSA_MPU_PROBE_B 0x40000000
464d04ea35SMax Filippov #define XTENSA_MPU_PROBE_V 0x80000000
474d04ea35SMax Filippov
484d04ea35SMax Filippov #define XTENSA_MPU_SYSTEM_TYPE_DEVICE 0x0001
494d04ea35SMax Filippov #define XTENSA_MPU_SYSTEM_TYPE_NC 0x0002
504d04ea35SMax Filippov #define XTENSA_MPU_SYSTEM_TYPE_C 0x0003
514d04ea35SMax Filippov #define XTENSA_MPU_SYSTEM_TYPE_MASK 0x0003
524d04ea35SMax Filippov
534d04ea35SMax Filippov #define XTENSA_MPU_TYPE_SYS_C 0x0010
544d04ea35SMax Filippov #define XTENSA_MPU_TYPE_SYS_W 0x0020
554d04ea35SMax Filippov #define XTENSA_MPU_TYPE_SYS_R 0x0040
564d04ea35SMax Filippov #define XTENSA_MPU_TYPE_CPU_C 0x0100
574d04ea35SMax Filippov #define XTENSA_MPU_TYPE_CPU_W 0x0200
584d04ea35SMax Filippov #define XTENSA_MPU_TYPE_CPU_R 0x0400
594d04ea35SMax Filippov #define XTENSA_MPU_TYPE_CPU_CACHE 0x0800
604d04ea35SMax Filippov #define XTENSA_MPU_TYPE_B 0x1000
614d04ea35SMax Filippov #define XTENSA_MPU_TYPE_INT 0x2000
624d04ea35SMax Filippov
HELPER(itlb_hit_test)637e5e5a63SMax Filippov void HELPER(itlb_hit_test)(CPUXtensaState *env, uint32_t vaddr)
647e5e5a63SMax Filippov {
657e5e5a63SMax Filippov /*
66ecd3571eSRichard Henderson * Probe the memory; we don't care about the result but
677e5e5a63SMax Filippov * only the side-effects (ie any MMU or other exception)
687e5e5a63SMax Filippov */
69ecd3571eSRichard Henderson probe_access(env, vaddr, 1, MMU_INST_FETCH,
703b916140SRichard Henderson cpu_mmu_index(env_cpu(env), true), GETPC());
717e5e5a63SMax Filippov }
727e5e5a63SMax Filippov
HELPER(wsr_rasid)737e5e5a63SMax Filippov void HELPER(wsr_rasid)(CPUXtensaState *env, uint32_t v)
747e5e5a63SMax Filippov {
757e5e5a63SMax Filippov v = (v & 0xffffff00) | 0x1;
767e5e5a63SMax Filippov if (v != env->sregs[RASID]) {
777e5e5a63SMax Filippov env->sregs[RASID] = v;
7892fddfbdSRichard Henderson tlb_flush(env_cpu(env));
797e5e5a63SMax Filippov }
807e5e5a63SMax Filippov }
817e5e5a63SMax Filippov
get_page_size(const CPUXtensaState * env,bool dtlb,uint32_t way)827e5e5a63SMax Filippov static uint32_t get_page_size(const CPUXtensaState *env,
837e5e5a63SMax Filippov bool dtlb, uint32_t way)
847e5e5a63SMax Filippov {
857e5e5a63SMax Filippov uint32_t tlbcfg = env->sregs[dtlb ? DTLBCFG : ITLBCFG];
867e5e5a63SMax Filippov
877e5e5a63SMax Filippov switch (way) {
887e5e5a63SMax Filippov case 4:
897e5e5a63SMax Filippov return (tlbcfg >> 16) & 0x3;
907e5e5a63SMax Filippov
917e5e5a63SMax Filippov case 5:
927e5e5a63SMax Filippov return (tlbcfg >> 20) & 0x1;
937e5e5a63SMax Filippov
947e5e5a63SMax Filippov case 6:
957e5e5a63SMax Filippov return (tlbcfg >> 24) & 0x1;
967e5e5a63SMax Filippov
977e5e5a63SMax Filippov default:
987e5e5a63SMax Filippov return 0;
997e5e5a63SMax Filippov }
1007e5e5a63SMax Filippov }
1017e5e5a63SMax Filippov
1027e5e5a63SMax Filippov /*!
1037e5e5a63SMax Filippov * Get bit mask for the virtual address bits translated by the TLB way
1047e5e5a63SMax Filippov */
xtensa_tlb_get_addr_mask(const CPUXtensaState * env,bool dtlb,uint32_t way)1055f7f36d0SMax Filippov static uint32_t xtensa_tlb_get_addr_mask(const CPUXtensaState *env,
1067e5e5a63SMax Filippov bool dtlb, uint32_t way)
1077e5e5a63SMax Filippov {
1087e5e5a63SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
1097e5e5a63SMax Filippov bool varway56 = dtlb ?
1107e5e5a63SMax Filippov env->config->dtlb.varway56 :
1117e5e5a63SMax Filippov env->config->itlb.varway56;
1127e5e5a63SMax Filippov
1137e5e5a63SMax Filippov switch (way) {
1147e5e5a63SMax Filippov case 4:
1157e5e5a63SMax Filippov return 0xfff00000 << get_page_size(env, dtlb, way) * 2;
1167e5e5a63SMax Filippov
1177e5e5a63SMax Filippov case 5:
1187e5e5a63SMax Filippov if (varway56) {
1197e5e5a63SMax Filippov return 0xf8000000 << get_page_size(env, dtlb, way);
1207e5e5a63SMax Filippov } else {
1217e5e5a63SMax Filippov return 0xf8000000;
1227e5e5a63SMax Filippov }
1237e5e5a63SMax Filippov
1247e5e5a63SMax Filippov case 6:
1257e5e5a63SMax Filippov if (varway56) {
1267e5e5a63SMax Filippov return 0xf0000000 << (1 - get_page_size(env, dtlb, way));
1277e5e5a63SMax Filippov } else {
1287e5e5a63SMax Filippov return 0xf0000000;
1297e5e5a63SMax Filippov }
1307e5e5a63SMax Filippov
1317e5e5a63SMax Filippov default:
1327e5e5a63SMax Filippov return 0xfffff000;
1337e5e5a63SMax Filippov }
1347e5e5a63SMax Filippov } else {
1357e5e5a63SMax Filippov return REGION_PAGE_MASK;
1367e5e5a63SMax Filippov }
1377e5e5a63SMax Filippov }
1387e5e5a63SMax Filippov
1397e5e5a63SMax Filippov /*!
1407e5e5a63SMax Filippov * Get bit mask for the 'VPN without index' field.
1417e5e5a63SMax Filippov * See ISA, 4.6.5.6, data format for RxTLB0
1427e5e5a63SMax Filippov */
get_vpn_mask(const CPUXtensaState * env,bool dtlb,uint32_t way)1437e5e5a63SMax Filippov static uint32_t get_vpn_mask(const CPUXtensaState *env, bool dtlb, uint32_t way)
1447e5e5a63SMax Filippov {
1457e5e5a63SMax Filippov if (way < 4) {
1467e5e5a63SMax Filippov bool is32 = (dtlb ?
1477e5e5a63SMax Filippov env->config->dtlb.nrefillentries :
1487e5e5a63SMax Filippov env->config->itlb.nrefillentries) == 32;
1497e5e5a63SMax Filippov return is32 ? 0xffff8000 : 0xffffc000;
1507e5e5a63SMax Filippov } else if (way == 4) {
1517e5e5a63SMax Filippov return xtensa_tlb_get_addr_mask(env, dtlb, way) << 2;
1527e5e5a63SMax Filippov } else if (way <= 6) {
1537e5e5a63SMax Filippov uint32_t mask = xtensa_tlb_get_addr_mask(env, dtlb, way);
1547e5e5a63SMax Filippov bool varway56 = dtlb ?
1557e5e5a63SMax Filippov env->config->dtlb.varway56 :
1567e5e5a63SMax Filippov env->config->itlb.varway56;
1577e5e5a63SMax Filippov
1587e5e5a63SMax Filippov if (varway56) {
1597e5e5a63SMax Filippov return mask << (way == 5 ? 2 : 3);
1607e5e5a63SMax Filippov } else {
1617e5e5a63SMax Filippov return mask << 1;
1627e5e5a63SMax Filippov }
1637e5e5a63SMax Filippov } else {
1647e5e5a63SMax Filippov return 0xfffff000;
1657e5e5a63SMax Filippov }
1667e5e5a63SMax Filippov }
1677e5e5a63SMax Filippov
1687e5e5a63SMax Filippov /*!
1697e5e5a63SMax Filippov * Split virtual address into VPN (with index) and entry index
1707e5e5a63SMax Filippov * for the given TLB way
1717e5e5a63SMax Filippov */
split_tlb_entry_spec_way(const CPUXtensaState * env,uint32_t v,bool dtlb,uint32_t * vpn,uint32_t wi,uint32_t * ei)1725f7f36d0SMax Filippov static void split_tlb_entry_spec_way(const CPUXtensaState *env, uint32_t v,
1735f7f36d0SMax Filippov bool dtlb, uint32_t *vpn,
1745f7f36d0SMax Filippov uint32_t wi, uint32_t *ei)
1757e5e5a63SMax Filippov {
1767e5e5a63SMax Filippov bool varway56 = dtlb ?
1777e5e5a63SMax Filippov env->config->dtlb.varway56 :
1787e5e5a63SMax Filippov env->config->itlb.varway56;
1797e5e5a63SMax Filippov
1807e5e5a63SMax Filippov if (!dtlb) {
1817e5e5a63SMax Filippov wi &= 7;
1827e5e5a63SMax Filippov }
1837e5e5a63SMax Filippov
1847e5e5a63SMax Filippov if (wi < 4) {
1857e5e5a63SMax Filippov bool is32 = (dtlb ?
1867e5e5a63SMax Filippov env->config->dtlb.nrefillentries :
1877e5e5a63SMax Filippov env->config->itlb.nrefillentries) == 32;
1887e5e5a63SMax Filippov *ei = (v >> 12) & (is32 ? 0x7 : 0x3);
1897e5e5a63SMax Filippov } else {
1907e5e5a63SMax Filippov switch (wi) {
1917e5e5a63SMax Filippov case 4:
1927e5e5a63SMax Filippov {
1937e5e5a63SMax Filippov uint32_t eibase = 20 + get_page_size(env, dtlb, wi) * 2;
1947e5e5a63SMax Filippov *ei = (v >> eibase) & 0x3;
1957e5e5a63SMax Filippov }
1967e5e5a63SMax Filippov break;
1977e5e5a63SMax Filippov
1987e5e5a63SMax Filippov case 5:
1997e5e5a63SMax Filippov if (varway56) {
2007e5e5a63SMax Filippov uint32_t eibase = 27 + get_page_size(env, dtlb, wi);
2017e5e5a63SMax Filippov *ei = (v >> eibase) & 0x3;
2027e5e5a63SMax Filippov } else {
2037e5e5a63SMax Filippov *ei = (v >> 27) & 0x1;
2047e5e5a63SMax Filippov }
2057e5e5a63SMax Filippov break;
2067e5e5a63SMax Filippov
2077e5e5a63SMax Filippov case 6:
2087e5e5a63SMax Filippov if (varway56) {
2097e5e5a63SMax Filippov uint32_t eibase = 29 - get_page_size(env, dtlb, wi);
2107e5e5a63SMax Filippov *ei = (v >> eibase) & 0x7;
2117e5e5a63SMax Filippov } else {
2127e5e5a63SMax Filippov *ei = (v >> 28) & 0x1;
2137e5e5a63SMax Filippov }
2147e5e5a63SMax Filippov break;
2157e5e5a63SMax Filippov
2167e5e5a63SMax Filippov default:
2177e5e5a63SMax Filippov *ei = 0;
2187e5e5a63SMax Filippov break;
2197e5e5a63SMax Filippov }
2207e5e5a63SMax Filippov }
2217e5e5a63SMax Filippov *vpn = v & xtensa_tlb_get_addr_mask(env, dtlb, wi);
2227e5e5a63SMax Filippov }
2237e5e5a63SMax Filippov
2247e5e5a63SMax Filippov /*!
2257e5e5a63SMax Filippov * Split TLB address into TLB way, entry index and VPN (with index).
2267e5e5a63SMax Filippov * See ISA, 4.6.5.5 - 4.6.5.8 for the TLB addressing format
2277e5e5a63SMax Filippov */
split_tlb_entry_spec(CPUXtensaState * env,uint32_t v,bool dtlb,uint32_t * vpn,uint32_t * wi,uint32_t * ei)228604927e3SMax Filippov static bool split_tlb_entry_spec(CPUXtensaState *env, uint32_t v, bool dtlb,
2297e5e5a63SMax Filippov uint32_t *vpn, uint32_t *wi, uint32_t *ei)
2307e5e5a63SMax Filippov {
2317e5e5a63SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
2327e5e5a63SMax Filippov *wi = v & (dtlb ? 0xf : 0x7);
233604927e3SMax Filippov if (*wi < (dtlb ? env->config->dtlb.nways : env->config->itlb.nways)) {
2347e5e5a63SMax Filippov split_tlb_entry_spec_way(env, v, dtlb, vpn, *wi, ei);
235604927e3SMax Filippov return true;
236604927e3SMax Filippov } else {
237604927e3SMax Filippov return false;
238604927e3SMax Filippov }
2397e5e5a63SMax Filippov } else {
2407e5e5a63SMax Filippov *vpn = v & REGION_PAGE_MASK;
2417e5e5a63SMax Filippov *wi = 0;
2427e5e5a63SMax Filippov *ei = (v >> 29) & 0x7;
243604927e3SMax Filippov return true;
2447e5e5a63SMax Filippov }
2457e5e5a63SMax Filippov }
2467e5e5a63SMax Filippov
xtensa_tlb_get_entry(CPUXtensaState * env,bool dtlb,unsigned wi,unsigned ei)2475f7f36d0SMax Filippov static xtensa_tlb_entry *xtensa_tlb_get_entry(CPUXtensaState *env, bool dtlb,
2485f7f36d0SMax Filippov unsigned wi, unsigned ei)
2495f7f36d0SMax Filippov {
250604927e3SMax Filippov const xtensa_tlb *tlb = dtlb ? &env->config->dtlb : &env->config->itlb;
251604927e3SMax Filippov
252604927e3SMax Filippov assert(wi < tlb->nways && ei < tlb->way_size[wi]);
2535f7f36d0SMax Filippov return dtlb ?
2545f7f36d0SMax Filippov env->dtlb[wi] + ei :
2555f7f36d0SMax Filippov env->itlb[wi] + ei;
2565f7f36d0SMax Filippov }
2575f7f36d0SMax Filippov
get_tlb_entry(CPUXtensaState * env,uint32_t v,bool dtlb,uint32_t * pwi)2587e5e5a63SMax Filippov static xtensa_tlb_entry *get_tlb_entry(CPUXtensaState *env,
2597e5e5a63SMax Filippov uint32_t v, bool dtlb, uint32_t *pwi)
2607e5e5a63SMax Filippov {
2617e5e5a63SMax Filippov uint32_t vpn;
2627e5e5a63SMax Filippov uint32_t wi;
2637e5e5a63SMax Filippov uint32_t ei;
2647e5e5a63SMax Filippov
265604927e3SMax Filippov if (split_tlb_entry_spec(env, v, dtlb, &vpn, &wi, &ei)) {
2667e5e5a63SMax Filippov if (pwi) {
2677e5e5a63SMax Filippov *pwi = wi;
2687e5e5a63SMax Filippov }
2697e5e5a63SMax Filippov return xtensa_tlb_get_entry(env, dtlb, wi, ei);
270604927e3SMax Filippov } else {
271604927e3SMax Filippov return NULL;
272604927e3SMax Filippov }
2737e5e5a63SMax Filippov }
2747e5e5a63SMax Filippov
xtensa_tlb_set_entry_mmu(const CPUXtensaState * env,xtensa_tlb_entry * entry,bool dtlb,unsigned wi,unsigned ei,uint32_t vpn,uint32_t pte)2755f7f36d0SMax Filippov static void xtensa_tlb_set_entry_mmu(const CPUXtensaState *env,
2767e5e5a63SMax Filippov xtensa_tlb_entry *entry, bool dtlb,
2777e5e5a63SMax Filippov unsigned wi, unsigned ei, uint32_t vpn,
2787e5e5a63SMax Filippov uint32_t pte)
2797e5e5a63SMax Filippov {
2807e5e5a63SMax Filippov entry->vaddr = vpn;
2817e5e5a63SMax Filippov entry->paddr = pte & xtensa_tlb_get_addr_mask(env, dtlb, wi);
2827e5e5a63SMax Filippov entry->asid = (env->sregs[RASID] >> ((pte >> 1) & 0x18)) & 0xff;
2837e5e5a63SMax Filippov entry->attr = pte & 0xf;
2847e5e5a63SMax Filippov }
2857e5e5a63SMax Filippov
xtensa_tlb_set_entry(CPUXtensaState * env,bool dtlb,unsigned wi,unsigned ei,uint32_t vpn,uint32_t pte)2865f7f36d0SMax Filippov static void xtensa_tlb_set_entry(CPUXtensaState *env, bool dtlb,
2875f7f36d0SMax Filippov unsigned wi, unsigned ei,
2885f7f36d0SMax Filippov uint32_t vpn, uint32_t pte)
2897e5e5a63SMax Filippov {
29092fddfbdSRichard Henderson CPUState *cs = env_cpu(env);
2917e5e5a63SMax Filippov xtensa_tlb_entry *entry = xtensa_tlb_get_entry(env, dtlb, wi, ei);
2927e5e5a63SMax Filippov
2937e5e5a63SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
2947e5e5a63SMax Filippov if (entry->variable) {
2957e5e5a63SMax Filippov if (entry->asid) {
2967e5e5a63SMax Filippov tlb_flush_page(cs, entry->vaddr);
2977e5e5a63SMax Filippov }
2987e5e5a63SMax Filippov xtensa_tlb_set_entry_mmu(env, entry, dtlb, wi, ei, vpn, pte);
2997e5e5a63SMax Filippov tlb_flush_page(cs, entry->vaddr);
3007e5e5a63SMax Filippov } else {
3017e5e5a63SMax Filippov qemu_log_mask(LOG_GUEST_ERROR,
3027e5e5a63SMax Filippov "%s %d, %d, %d trying to set immutable entry\n",
3037e5e5a63SMax Filippov __func__, dtlb, wi, ei);
3047e5e5a63SMax Filippov }
3057e5e5a63SMax Filippov } else {
3067e5e5a63SMax Filippov tlb_flush_page(cs, entry->vaddr);
3077e5e5a63SMax Filippov if (xtensa_option_enabled(env->config,
3087e5e5a63SMax Filippov XTENSA_OPTION_REGION_TRANSLATION)) {
3097e5e5a63SMax Filippov entry->paddr = pte & REGION_PAGE_MASK;
3107e5e5a63SMax Filippov }
3117e5e5a63SMax Filippov entry->attr = pte & 0xf;
3127e5e5a63SMax Filippov }
3137e5e5a63SMax Filippov }
3147e5e5a63SMax Filippov
xtensa_cpu_get_phys_page_debug(CPUState * cs,vaddr addr)3157e5e5a63SMax Filippov hwaddr xtensa_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
3167e5e5a63SMax Filippov {
3177e5e5a63SMax Filippov XtensaCPU *cpu = XTENSA_CPU(cs);
3187e5e5a63SMax Filippov uint32_t paddr;
3197e5e5a63SMax Filippov uint32_t page_size;
3207e5e5a63SMax Filippov unsigned access;
3217e5e5a63SMax Filippov
3227e5e5a63SMax Filippov if (xtensa_get_physical_addr(&cpu->env, false, addr, 0, 0,
3237e5e5a63SMax Filippov &paddr, &page_size, &access) == 0) {
3247e5e5a63SMax Filippov return paddr;
3257e5e5a63SMax Filippov }
3267e5e5a63SMax Filippov if (xtensa_get_physical_addr(&cpu->env, false, addr, 2, 0,
3277e5e5a63SMax Filippov &paddr, &page_size, &access) == 0) {
3287e5e5a63SMax Filippov return paddr;
3297e5e5a63SMax Filippov }
3307e5e5a63SMax Filippov return ~0;
3317e5e5a63SMax Filippov }
3327e5e5a63SMax Filippov
reset_tlb_mmu_all_ways(CPUXtensaState * env,const xtensa_tlb * tlb,xtensa_tlb_entry entry[][MAX_TLB_WAY_SIZE])3337e5e5a63SMax Filippov static void reset_tlb_mmu_all_ways(CPUXtensaState *env,
3347e5e5a63SMax Filippov const xtensa_tlb *tlb,
3357e5e5a63SMax Filippov xtensa_tlb_entry entry[][MAX_TLB_WAY_SIZE])
3367e5e5a63SMax Filippov {
3377e5e5a63SMax Filippov unsigned wi, ei;
3387e5e5a63SMax Filippov
3397e5e5a63SMax Filippov for (wi = 0; wi < tlb->nways; ++wi) {
3407e5e5a63SMax Filippov for (ei = 0; ei < tlb->way_size[wi]; ++ei) {
3417e5e5a63SMax Filippov entry[wi][ei].asid = 0;
3427e5e5a63SMax Filippov entry[wi][ei].variable = true;
3437e5e5a63SMax Filippov }
3447e5e5a63SMax Filippov }
3457e5e5a63SMax Filippov }
3467e5e5a63SMax Filippov
reset_tlb_mmu_ways56(CPUXtensaState * env,const xtensa_tlb * tlb,xtensa_tlb_entry entry[][MAX_TLB_WAY_SIZE])3477e5e5a63SMax Filippov static void reset_tlb_mmu_ways56(CPUXtensaState *env,
3487e5e5a63SMax Filippov const xtensa_tlb *tlb,
3497e5e5a63SMax Filippov xtensa_tlb_entry entry[][MAX_TLB_WAY_SIZE])
3507e5e5a63SMax Filippov {
3517e5e5a63SMax Filippov if (!tlb->varway56) {
3527e5e5a63SMax Filippov static const xtensa_tlb_entry way5[] = {
3537e5e5a63SMax Filippov {
3547e5e5a63SMax Filippov .vaddr = 0xd0000000,
3557e5e5a63SMax Filippov .paddr = 0,
3567e5e5a63SMax Filippov .asid = 1,
3577e5e5a63SMax Filippov .attr = 7,
3587e5e5a63SMax Filippov .variable = false,
3597e5e5a63SMax Filippov }, {
3607e5e5a63SMax Filippov .vaddr = 0xd8000000,
3617e5e5a63SMax Filippov .paddr = 0,
3627e5e5a63SMax Filippov .asid = 1,
3637e5e5a63SMax Filippov .attr = 3,
3647e5e5a63SMax Filippov .variable = false,
3657e5e5a63SMax Filippov }
3667e5e5a63SMax Filippov };
3677e5e5a63SMax Filippov static const xtensa_tlb_entry way6[] = {
3687e5e5a63SMax Filippov {
3697e5e5a63SMax Filippov .vaddr = 0xe0000000,
3707e5e5a63SMax Filippov .paddr = 0xf0000000,
3717e5e5a63SMax Filippov .asid = 1,
3727e5e5a63SMax Filippov .attr = 7,
3737e5e5a63SMax Filippov .variable = false,
3747e5e5a63SMax Filippov }, {
3757e5e5a63SMax Filippov .vaddr = 0xf0000000,
3767e5e5a63SMax Filippov .paddr = 0xf0000000,
3777e5e5a63SMax Filippov .asid = 1,
3787e5e5a63SMax Filippov .attr = 3,
3797e5e5a63SMax Filippov .variable = false,
3807e5e5a63SMax Filippov }
3817e5e5a63SMax Filippov };
3827e5e5a63SMax Filippov memcpy(entry[5], way5, sizeof(way5));
3837e5e5a63SMax Filippov memcpy(entry[6], way6, sizeof(way6));
3847e5e5a63SMax Filippov } else {
3857e5e5a63SMax Filippov uint32_t ei;
3867e5e5a63SMax Filippov for (ei = 0; ei < 8; ++ei) {
3877e5e5a63SMax Filippov entry[6][ei].vaddr = ei << 29;
3887e5e5a63SMax Filippov entry[6][ei].paddr = ei << 29;
3897e5e5a63SMax Filippov entry[6][ei].asid = 1;
3907e5e5a63SMax Filippov entry[6][ei].attr = 3;
3917e5e5a63SMax Filippov }
3927e5e5a63SMax Filippov }
3937e5e5a63SMax Filippov }
3947e5e5a63SMax Filippov
reset_tlb_region_way0(CPUXtensaState * env,xtensa_tlb_entry entry[][MAX_TLB_WAY_SIZE])3957e5e5a63SMax Filippov static void reset_tlb_region_way0(CPUXtensaState *env,
3967e5e5a63SMax Filippov xtensa_tlb_entry entry[][MAX_TLB_WAY_SIZE])
3977e5e5a63SMax Filippov {
3987e5e5a63SMax Filippov unsigned ei;
3997e5e5a63SMax Filippov
4007e5e5a63SMax Filippov for (ei = 0; ei < 8; ++ei) {
4017e5e5a63SMax Filippov entry[0][ei].vaddr = ei << 29;
4027e5e5a63SMax Filippov entry[0][ei].paddr = ei << 29;
4037e5e5a63SMax Filippov entry[0][ei].asid = 1;
4047e5e5a63SMax Filippov entry[0][ei].attr = 2;
4057e5e5a63SMax Filippov entry[0][ei].variable = true;
4067e5e5a63SMax Filippov }
4077e5e5a63SMax Filippov }
4087e5e5a63SMax Filippov
reset_mmu(CPUXtensaState * env)4097e5e5a63SMax Filippov void reset_mmu(CPUXtensaState *env)
4107e5e5a63SMax Filippov {
4117e5e5a63SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
4127e5e5a63SMax Filippov env->sregs[RASID] = 0x04030201;
4137e5e5a63SMax Filippov env->sregs[ITLBCFG] = 0;
4147e5e5a63SMax Filippov env->sregs[DTLBCFG] = 0;
4157e5e5a63SMax Filippov env->autorefill_idx = 0;
4167e5e5a63SMax Filippov reset_tlb_mmu_all_ways(env, &env->config->itlb, env->itlb);
4177e5e5a63SMax Filippov reset_tlb_mmu_all_ways(env, &env->config->dtlb, env->dtlb);
4187e5e5a63SMax Filippov reset_tlb_mmu_ways56(env, &env->config->itlb, env->itlb);
4197e5e5a63SMax Filippov reset_tlb_mmu_ways56(env, &env->config->dtlb, env->dtlb);
4204d04ea35SMax Filippov } else if (xtensa_option_enabled(env->config, XTENSA_OPTION_MPU)) {
4214d04ea35SMax Filippov unsigned i;
4224d04ea35SMax Filippov
4234d04ea35SMax Filippov env->sregs[MPUENB] = 0;
4244d04ea35SMax Filippov env->sregs[MPUCFG] = env->config->n_mpu_fg_segments;
4254d04ea35SMax Filippov env->sregs[CACHEADRDIS] = 0;
4264d04ea35SMax Filippov assert(env->config->n_mpu_bg_segments > 0 &&
4274d04ea35SMax Filippov env->config->mpu_bg[0].vaddr == 0);
4284d04ea35SMax Filippov for (i = 1; i < env->config->n_mpu_bg_segments; ++i) {
4294d04ea35SMax Filippov assert(env->config->mpu_bg[i].vaddr >=
4304d04ea35SMax Filippov env->config->mpu_bg[i - 1].vaddr);
4314d04ea35SMax Filippov }
4327e5e5a63SMax Filippov } else {
4334d04ea35SMax Filippov env->sregs[CACHEATTR] = 0x22222222;
4347e5e5a63SMax Filippov reset_tlb_region_way0(env, env->itlb);
4357e5e5a63SMax Filippov reset_tlb_region_way0(env, env->dtlb);
4367e5e5a63SMax Filippov }
4377e5e5a63SMax Filippov }
4387e5e5a63SMax Filippov
get_ring(const CPUXtensaState * env,uint8_t asid)4397e5e5a63SMax Filippov static unsigned get_ring(const CPUXtensaState *env, uint8_t asid)
4407e5e5a63SMax Filippov {
4417e5e5a63SMax Filippov unsigned i;
4427e5e5a63SMax Filippov for (i = 0; i < 4; ++i) {
4437e5e5a63SMax Filippov if (((env->sregs[RASID] >> i * 8) & 0xff) == asid) {
4447e5e5a63SMax Filippov return i;
4457e5e5a63SMax Filippov }
4467e5e5a63SMax Filippov }
4477e5e5a63SMax Filippov return 0xff;
4487e5e5a63SMax Filippov }
4497e5e5a63SMax Filippov
4507e5e5a63SMax Filippov /*!
4517e5e5a63SMax Filippov * Lookup xtensa TLB for the given virtual address.
4527e5e5a63SMax Filippov * See ISA, 4.6.2.2
4537e5e5a63SMax Filippov *
4547e5e5a63SMax Filippov * \param pwi: [out] way index
4557e5e5a63SMax Filippov * \param pei: [out] entry index
4567e5e5a63SMax Filippov * \param pring: [out] access ring
4577e5e5a63SMax Filippov * \return 0 if ok, exception cause code otherwise
4587e5e5a63SMax Filippov */
xtensa_tlb_lookup(const CPUXtensaState * env,uint32_t addr,bool dtlb,uint32_t * pwi,uint32_t * pei,uint8_t * pring)4595f7f36d0SMax Filippov static int xtensa_tlb_lookup(const CPUXtensaState *env,
4605f7f36d0SMax Filippov uint32_t addr, bool dtlb,
4617e5e5a63SMax Filippov uint32_t *pwi, uint32_t *pei, uint8_t *pring)
4627e5e5a63SMax Filippov {
4637e5e5a63SMax Filippov const xtensa_tlb *tlb = dtlb ?
4647e5e5a63SMax Filippov &env->config->dtlb : &env->config->itlb;
4657e5e5a63SMax Filippov const xtensa_tlb_entry (*entry)[MAX_TLB_WAY_SIZE] = dtlb ?
4667e5e5a63SMax Filippov env->dtlb : env->itlb;
4677e5e5a63SMax Filippov
4687e5e5a63SMax Filippov int nhits = 0;
4697e5e5a63SMax Filippov unsigned wi;
4707e5e5a63SMax Filippov
4717e5e5a63SMax Filippov for (wi = 0; wi < tlb->nways; ++wi) {
4727e5e5a63SMax Filippov uint32_t vpn;
4737e5e5a63SMax Filippov uint32_t ei;
4747e5e5a63SMax Filippov split_tlb_entry_spec_way(env, addr, dtlb, &vpn, wi, &ei);
4757e5e5a63SMax Filippov if (entry[wi][ei].vaddr == vpn && entry[wi][ei].asid) {
4767e5e5a63SMax Filippov unsigned ring = get_ring(env, entry[wi][ei].asid);
4777e5e5a63SMax Filippov if (ring < 4) {
4787e5e5a63SMax Filippov if (++nhits > 1) {
4797e5e5a63SMax Filippov return dtlb ?
4807e5e5a63SMax Filippov LOAD_STORE_TLB_MULTI_HIT_CAUSE :
4817e5e5a63SMax Filippov INST_TLB_MULTI_HIT_CAUSE;
4827e5e5a63SMax Filippov }
4837e5e5a63SMax Filippov *pwi = wi;
4847e5e5a63SMax Filippov *pei = ei;
4857e5e5a63SMax Filippov *pring = ring;
4867e5e5a63SMax Filippov }
4877e5e5a63SMax Filippov }
4887e5e5a63SMax Filippov }
4897e5e5a63SMax Filippov return nhits ? 0 :
4907e5e5a63SMax Filippov (dtlb ? LOAD_STORE_TLB_MISS_CAUSE : INST_TLB_MISS_CAUSE);
4917e5e5a63SMax Filippov }
4927e5e5a63SMax Filippov
HELPER(rtlb0)4935f7f36d0SMax Filippov uint32_t HELPER(rtlb0)(CPUXtensaState *env, uint32_t v, uint32_t dtlb)
4945f7f36d0SMax Filippov {
4955f7f36d0SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
4965f7f36d0SMax Filippov uint32_t wi;
4975f7f36d0SMax Filippov const xtensa_tlb_entry *entry = get_tlb_entry(env, v, dtlb, &wi);
498604927e3SMax Filippov
499604927e3SMax Filippov if (entry) {
5005f7f36d0SMax Filippov return (entry->vaddr & get_vpn_mask(env, dtlb, wi)) | entry->asid;
5015f7f36d0SMax Filippov } else {
502604927e3SMax Filippov return 0;
503604927e3SMax Filippov }
504604927e3SMax Filippov } else {
5055f7f36d0SMax Filippov return v & REGION_PAGE_MASK;
5065f7f36d0SMax Filippov }
5075f7f36d0SMax Filippov }
5085f7f36d0SMax Filippov
HELPER(rtlb1)5095f7f36d0SMax Filippov uint32_t HELPER(rtlb1)(CPUXtensaState *env, uint32_t v, uint32_t dtlb)
5105f7f36d0SMax Filippov {
5115f7f36d0SMax Filippov const xtensa_tlb_entry *entry = get_tlb_entry(env, v, dtlb, NULL);
512604927e3SMax Filippov
513604927e3SMax Filippov if (entry) {
5145f7f36d0SMax Filippov return entry->paddr | entry->attr;
515604927e3SMax Filippov } else {
516604927e3SMax Filippov return 0;
517604927e3SMax Filippov }
5185f7f36d0SMax Filippov }
5195f7f36d0SMax Filippov
HELPER(itlb)5205f7f36d0SMax Filippov void HELPER(itlb)(CPUXtensaState *env, uint32_t v, uint32_t dtlb)
5215f7f36d0SMax Filippov {
5225f7f36d0SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
5235f7f36d0SMax Filippov uint32_t wi;
5245f7f36d0SMax Filippov xtensa_tlb_entry *entry = get_tlb_entry(env, v, dtlb, &wi);
525604927e3SMax Filippov if (entry && entry->variable && entry->asid) {
52692fddfbdSRichard Henderson tlb_flush_page(env_cpu(env), entry->vaddr);
5275f7f36d0SMax Filippov entry->asid = 0;
5285f7f36d0SMax Filippov }
5295f7f36d0SMax Filippov }
5305f7f36d0SMax Filippov }
5315f7f36d0SMax Filippov
HELPER(ptlb)5325f7f36d0SMax Filippov uint32_t HELPER(ptlb)(CPUXtensaState *env, uint32_t v, uint32_t dtlb)
5335f7f36d0SMax Filippov {
5345f7f36d0SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
5355f7f36d0SMax Filippov uint32_t wi;
5365f7f36d0SMax Filippov uint32_t ei;
5375f7f36d0SMax Filippov uint8_t ring;
5385f7f36d0SMax Filippov int res = xtensa_tlb_lookup(env, v, dtlb, &wi, &ei, &ring);
5395f7f36d0SMax Filippov
5405f7f36d0SMax Filippov switch (res) {
5415f7f36d0SMax Filippov case 0:
5425f7f36d0SMax Filippov if (ring >= xtensa_get_ring(env)) {
5435f7f36d0SMax Filippov return (v & 0xfffff000) | wi | (dtlb ? 0x10 : 0x8);
5445f7f36d0SMax Filippov }
5455f7f36d0SMax Filippov break;
5465f7f36d0SMax Filippov
5475f7f36d0SMax Filippov case INST_TLB_MULTI_HIT_CAUSE:
5485f7f36d0SMax Filippov case LOAD_STORE_TLB_MULTI_HIT_CAUSE:
5495f7f36d0SMax Filippov HELPER(exception_cause_vaddr)(env, env->pc, res, v);
5505f7f36d0SMax Filippov break;
5515f7f36d0SMax Filippov }
5525f7f36d0SMax Filippov return 0;
5535f7f36d0SMax Filippov } else {
5545f7f36d0SMax Filippov return (v & REGION_PAGE_MASK) | 0x1;
5555f7f36d0SMax Filippov }
5565f7f36d0SMax Filippov }
5575f7f36d0SMax Filippov
HELPER(wtlb)5585f7f36d0SMax Filippov void HELPER(wtlb)(CPUXtensaState *env, uint32_t p, uint32_t v, uint32_t dtlb)
5595f7f36d0SMax Filippov {
5605f7f36d0SMax Filippov uint32_t vpn;
5615f7f36d0SMax Filippov uint32_t wi;
5625f7f36d0SMax Filippov uint32_t ei;
563604927e3SMax Filippov if (split_tlb_entry_spec(env, v, dtlb, &vpn, &wi, &ei)) {
5645f7f36d0SMax Filippov xtensa_tlb_set_entry(env, dtlb, wi, ei, vpn, p);
5655f7f36d0SMax Filippov }
566604927e3SMax Filippov }
5675f7f36d0SMax Filippov
5687e5e5a63SMax Filippov /*!
5697e5e5a63SMax Filippov * Convert MMU ATTR to PAGE_{READ,WRITE,EXEC} mask.
5707e5e5a63SMax Filippov * See ISA, 4.6.5.10
5717e5e5a63SMax Filippov */
mmu_attr_to_access(uint32_t attr)5727e5e5a63SMax Filippov static unsigned mmu_attr_to_access(uint32_t attr)
5737e5e5a63SMax Filippov {
5747e5e5a63SMax Filippov unsigned access = 0;
5757e5e5a63SMax Filippov
5767e5e5a63SMax Filippov if (attr < 12) {
5777e5e5a63SMax Filippov access |= PAGE_READ;
5787e5e5a63SMax Filippov if (attr & 0x1) {
5797e5e5a63SMax Filippov access |= PAGE_EXEC;
5807e5e5a63SMax Filippov }
5817e5e5a63SMax Filippov if (attr & 0x2) {
5827e5e5a63SMax Filippov access |= PAGE_WRITE;
5837e5e5a63SMax Filippov }
5847e5e5a63SMax Filippov
5857e5e5a63SMax Filippov switch (attr & 0xc) {
5867e5e5a63SMax Filippov case 0:
5877e5e5a63SMax Filippov access |= PAGE_CACHE_BYPASS;
5887e5e5a63SMax Filippov break;
5897e5e5a63SMax Filippov
5907e5e5a63SMax Filippov case 4:
5917e5e5a63SMax Filippov access |= PAGE_CACHE_WB;
5927e5e5a63SMax Filippov break;
5937e5e5a63SMax Filippov
5947e5e5a63SMax Filippov case 8:
5957e5e5a63SMax Filippov access |= PAGE_CACHE_WT;
5967e5e5a63SMax Filippov break;
5977e5e5a63SMax Filippov }
5987e5e5a63SMax Filippov } else if (attr == 13) {
5997e5e5a63SMax Filippov access |= PAGE_READ | PAGE_WRITE | PAGE_CACHE_ISOLATE;
6007e5e5a63SMax Filippov }
6017e5e5a63SMax Filippov return access;
6027e5e5a63SMax Filippov }
6037e5e5a63SMax Filippov
6047e5e5a63SMax Filippov /*!
6057e5e5a63SMax Filippov * Convert region protection ATTR to PAGE_{READ,WRITE,EXEC} mask.
6067e5e5a63SMax Filippov * See ISA, 4.6.3.3
6077e5e5a63SMax Filippov */
region_attr_to_access(uint32_t attr)6087e5e5a63SMax Filippov static unsigned region_attr_to_access(uint32_t attr)
6097e5e5a63SMax Filippov {
6107e5e5a63SMax Filippov static const unsigned access[16] = {
6117e5e5a63SMax Filippov [0] = PAGE_READ | PAGE_WRITE | PAGE_CACHE_WT,
6127e5e5a63SMax Filippov [1] = PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_CACHE_WT,
6137e5e5a63SMax Filippov [2] = PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_CACHE_BYPASS,
6147e5e5a63SMax Filippov [3] = PAGE_EXEC | PAGE_CACHE_WB,
6157e5e5a63SMax Filippov [4] = PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_CACHE_WB,
6167e5e5a63SMax Filippov [5] = PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_CACHE_WB,
6177e5e5a63SMax Filippov [14] = PAGE_READ | PAGE_WRITE | PAGE_CACHE_ISOLATE,
6187e5e5a63SMax Filippov };
6197e5e5a63SMax Filippov
6207e5e5a63SMax Filippov return access[attr & 0xf];
6217e5e5a63SMax Filippov }
6227e5e5a63SMax Filippov
6237e5e5a63SMax Filippov /*!
6247e5e5a63SMax Filippov * Convert cacheattr to PAGE_{READ,WRITE,EXEC} mask.
6257e5e5a63SMax Filippov * See ISA, A.2.14 The Cache Attribute Register
6267e5e5a63SMax Filippov */
cacheattr_attr_to_access(uint32_t attr)6277e5e5a63SMax Filippov static unsigned cacheattr_attr_to_access(uint32_t attr)
6287e5e5a63SMax Filippov {
6297e5e5a63SMax Filippov static const unsigned access[16] = {
6307e5e5a63SMax Filippov [0] = PAGE_READ | PAGE_WRITE | PAGE_CACHE_WT,
6317e5e5a63SMax Filippov [1] = PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_CACHE_WT,
6327e5e5a63SMax Filippov [2] = PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_CACHE_BYPASS,
6337e5e5a63SMax Filippov [3] = PAGE_EXEC | PAGE_CACHE_WB,
6347e5e5a63SMax Filippov [4] = PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_CACHE_WB,
6357e5e5a63SMax Filippov [14] = PAGE_READ | PAGE_WRITE | PAGE_CACHE_ISOLATE,
6367e5e5a63SMax Filippov };
6377e5e5a63SMax Filippov
6387e5e5a63SMax Filippov return access[attr & 0xf];
6397e5e5a63SMax Filippov }
6407e5e5a63SMax Filippov
6414d04ea35SMax Filippov struct attr_pattern {
6424d04ea35SMax Filippov uint32_t mask;
6434d04ea35SMax Filippov uint32_t value;
6444d04ea35SMax Filippov };
6454d04ea35SMax Filippov
attr_pattern_match(uint32_t attr,const struct attr_pattern * pattern,size_t n)6464d04ea35SMax Filippov static int attr_pattern_match(uint32_t attr,
6474d04ea35SMax Filippov const struct attr_pattern *pattern,
6484d04ea35SMax Filippov size_t n)
6494d04ea35SMax Filippov {
6504d04ea35SMax Filippov size_t i;
6514d04ea35SMax Filippov
6524d04ea35SMax Filippov for (i = 0; i < n; ++i) {
6534d04ea35SMax Filippov if ((attr & pattern[i].mask) == pattern[i].value) {
6544d04ea35SMax Filippov return 1;
6554d04ea35SMax Filippov }
6564d04ea35SMax Filippov }
6574d04ea35SMax Filippov return 0;
6584d04ea35SMax Filippov }
6594d04ea35SMax Filippov
mpu_attr_to_cpu_cache(uint32_t attr)6604d04ea35SMax Filippov static unsigned mpu_attr_to_cpu_cache(uint32_t attr)
6614d04ea35SMax Filippov {
6624d04ea35SMax Filippov static const struct attr_pattern cpu_c[] = {
6634d04ea35SMax Filippov { .mask = 0x18f, .value = 0x089 },
6644d04ea35SMax Filippov { .mask = 0x188, .value = 0x080 },
6654d04ea35SMax Filippov { .mask = 0x180, .value = 0x180 },
6664d04ea35SMax Filippov };
6674d04ea35SMax Filippov
6684d04ea35SMax Filippov unsigned type = 0;
6694d04ea35SMax Filippov
6704d04ea35SMax Filippov if (attr_pattern_match(attr, cpu_c, ARRAY_SIZE(cpu_c))) {
6714d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_CPU_CACHE;
6724d04ea35SMax Filippov if (attr & 0x10) {
6734d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_CPU_C;
6744d04ea35SMax Filippov }
6754d04ea35SMax Filippov if (attr & 0x20) {
6764d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_CPU_W;
6774d04ea35SMax Filippov }
6784d04ea35SMax Filippov if (attr & 0x40) {
6794d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_CPU_R;
6804d04ea35SMax Filippov }
6814d04ea35SMax Filippov }
6824d04ea35SMax Filippov return type;
6834d04ea35SMax Filippov }
6844d04ea35SMax Filippov
mpu_attr_to_type(uint32_t attr)6854d04ea35SMax Filippov static unsigned mpu_attr_to_type(uint32_t attr)
6864d04ea35SMax Filippov {
6874d04ea35SMax Filippov static const struct attr_pattern device_type[] = {
6884d04ea35SMax Filippov { .mask = 0x1f6, .value = 0x000 },
6894d04ea35SMax Filippov { .mask = 0x1f6, .value = 0x006 },
6904d04ea35SMax Filippov };
6914d04ea35SMax Filippov static const struct attr_pattern sys_nc_type[] = {
6924d04ea35SMax Filippov { .mask = 0x1fe, .value = 0x018 },
6934d04ea35SMax Filippov { .mask = 0x1fe, .value = 0x01e },
6944d04ea35SMax Filippov { .mask = 0x18f, .value = 0x089 },
6954d04ea35SMax Filippov };
6964d04ea35SMax Filippov static const struct attr_pattern sys_c_type[] = {
6974d04ea35SMax Filippov { .mask = 0x1f8, .value = 0x010 },
6984d04ea35SMax Filippov { .mask = 0x188, .value = 0x080 },
6994d04ea35SMax Filippov { .mask = 0x1f0, .value = 0x030 },
7004d04ea35SMax Filippov { .mask = 0x180, .value = 0x180 },
7014d04ea35SMax Filippov };
7024d04ea35SMax Filippov static const struct attr_pattern b[] = {
7034d04ea35SMax Filippov { .mask = 0x1f7, .value = 0x001 },
7044d04ea35SMax Filippov { .mask = 0x1f7, .value = 0x007 },
7054d04ea35SMax Filippov { .mask = 0x1ff, .value = 0x019 },
7064d04ea35SMax Filippov { .mask = 0x1ff, .value = 0x01f },
7074d04ea35SMax Filippov };
7084d04ea35SMax Filippov
7094d04ea35SMax Filippov unsigned type = 0;
7104d04ea35SMax Filippov
7114d04ea35SMax Filippov attr = (attr & XTENSA_MPU_MEM_TYPE_MASK) >> XTENSA_MPU_MEM_TYPE_SHIFT;
7124d04ea35SMax Filippov if (attr_pattern_match(attr, device_type, ARRAY_SIZE(device_type))) {
7134d04ea35SMax Filippov type |= XTENSA_MPU_SYSTEM_TYPE_DEVICE;
7144d04ea35SMax Filippov if (attr & 0x80) {
7154d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_INT;
7164d04ea35SMax Filippov }
7174d04ea35SMax Filippov }
7184d04ea35SMax Filippov if (attr_pattern_match(attr, sys_nc_type, ARRAY_SIZE(sys_nc_type))) {
7194d04ea35SMax Filippov type |= XTENSA_MPU_SYSTEM_TYPE_NC;
7204d04ea35SMax Filippov }
7214d04ea35SMax Filippov if (attr_pattern_match(attr, sys_c_type, ARRAY_SIZE(sys_c_type))) {
7224d04ea35SMax Filippov type |= XTENSA_MPU_SYSTEM_TYPE_C;
7234d04ea35SMax Filippov if (attr & 0x1) {
7244d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_SYS_C;
7254d04ea35SMax Filippov }
7264d04ea35SMax Filippov if (attr & 0x2) {
7274d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_SYS_W;
7284d04ea35SMax Filippov }
7294d04ea35SMax Filippov if (attr & 0x4) {
7304d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_SYS_R;
7314d04ea35SMax Filippov }
7324d04ea35SMax Filippov }
7334d04ea35SMax Filippov if (attr_pattern_match(attr, b, ARRAY_SIZE(b))) {
7344d04ea35SMax Filippov type |= XTENSA_MPU_TYPE_B;
7354d04ea35SMax Filippov }
7364d04ea35SMax Filippov type |= mpu_attr_to_cpu_cache(attr);
7374d04ea35SMax Filippov
7384d04ea35SMax Filippov return type;
7394d04ea35SMax Filippov }
7404d04ea35SMax Filippov
mpu_attr_to_access(uint32_t attr,unsigned ring)7414d04ea35SMax Filippov static unsigned mpu_attr_to_access(uint32_t attr, unsigned ring)
7424d04ea35SMax Filippov {
7434d04ea35SMax Filippov static const unsigned access[2][16] = {
7444d04ea35SMax Filippov [0] = {
7454d04ea35SMax Filippov [4] = PAGE_READ,
7464d04ea35SMax Filippov [5] = PAGE_READ | PAGE_EXEC,
7474d04ea35SMax Filippov [6] = PAGE_READ | PAGE_WRITE,
7484d04ea35SMax Filippov [7] = PAGE_READ | PAGE_WRITE | PAGE_EXEC,
7494d04ea35SMax Filippov [8] = PAGE_WRITE,
7504d04ea35SMax Filippov [9] = PAGE_READ | PAGE_WRITE,
7514d04ea35SMax Filippov [10] = PAGE_READ | PAGE_WRITE,
7524d04ea35SMax Filippov [11] = PAGE_READ | PAGE_WRITE | PAGE_EXEC,
7534d04ea35SMax Filippov [12] = PAGE_READ,
7544d04ea35SMax Filippov [13] = PAGE_READ | PAGE_EXEC,
7554d04ea35SMax Filippov [14] = PAGE_READ | PAGE_WRITE,
7564d04ea35SMax Filippov [15] = PAGE_READ | PAGE_WRITE | PAGE_EXEC,
7574d04ea35SMax Filippov },
7584d04ea35SMax Filippov [1] = {
7594d04ea35SMax Filippov [8] = PAGE_WRITE,
7604d04ea35SMax Filippov [9] = PAGE_READ | PAGE_WRITE | PAGE_EXEC,
7614d04ea35SMax Filippov [10] = PAGE_READ,
7624d04ea35SMax Filippov [11] = PAGE_READ | PAGE_EXEC,
7634d04ea35SMax Filippov [12] = PAGE_READ,
7644d04ea35SMax Filippov [13] = PAGE_READ | PAGE_EXEC,
7654d04ea35SMax Filippov [14] = PAGE_READ | PAGE_WRITE,
7664d04ea35SMax Filippov [15] = PAGE_READ | PAGE_WRITE | PAGE_EXEC,
7674d04ea35SMax Filippov },
7684d04ea35SMax Filippov };
7694d04ea35SMax Filippov unsigned rv;
7704d04ea35SMax Filippov unsigned type;
7714d04ea35SMax Filippov
7724d04ea35SMax Filippov type = mpu_attr_to_cpu_cache(attr);
7734d04ea35SMax Filippov rv = access[ring != 0][(attr & XTENSA_MPU_ACC_RIGHTS_MASK) >>
7744d04ea35SMax Filippov XTENSA_MPU_ACC_RIGHTS_SHIFT];
7754d04ea35SMax Filippov
7764d04ea35SMax Filippov if (type & XTENSA_MPU_TYPE_CPU_CACHE) {
7774d04ea35SMax Filippov rv |= (type & XTENSA_MPU_TYPE_CPU_C) ? PAGE_CACHE_WB : PAGE_CACHE_WT;
7784d04ea35SMax Filippov } else {
7794d04ea35SMax Filippov rv |= PAGE_CACHE_BYPASS;
7804d04ea35SMax Filippov }
7814d04ea35SMax Filippov return rv;
7824d04ea35SMax Filippov }
7834d04ea35SMax Filippov
is_access_granted(unsigned access,int is_write)7847e5e5a63SMax Filippov static bool is_access_granted(unsigned access, int is_write)
7857e5e5a63SMax Filippov {
7867e5e5a63SMax Filippov switch (is_write) {
7877e5e5a63SMax Filippov case 0:
7887e5e5a63SMax Filippov return access & PAGE_READ;
7897e5e5a63SMax Filippov
7907e5e5a63SMax Filippov case 1:
7917e5e5a63SMax Filippov return access & PAGE_WRITE;
7927e5e5a63SMax Filippov
7937e5e5a63SMax Filippov case 2:
7947e5e5a63SMax Filippov return access & PAGE_EXEC;
7957e5e5a63SMax Filippov
7967e5e5a63SMax Filippov default:
7977e5e5a63SMax Filippov return 0;
7987e5e5a63SMax Filippov }
7997e5e5a63SMax Filippov }
8007e5e5a63SMax Filippov
8017e5e5a63SMax Filippov static bool get_pte(CPUXtensaState *env, uint32_t vaddr, uint32_t *pte);
8027e5e5a63SMax Filippov
get_physical_addr_mmu(CPUXtensaState * env,bool update_tlb,uint32_t vaddr,int is_write,int mmu_idx,uint32_t * paddr,uint32_t * page_size,unsigned * access,bool may_lookup_pt)8037e5e5a63SMax Filippov static int get_physical_addr_mmu(CPUXtensaState *env, bool update_tlb,
8047e5e5a63SMax Filippov uint32_t vaddr, int is_write, int mmu_idx,
8057e5e5a63SMax Filippov uint32_t *paddr, uint32_t *page_size,
8067e5e5a63SMax Filippov unsigned *access, bool may_lookup_pt)
8077e5e5a63SMax Filippov {
8087e5e5a63SMax Filippov bool dtlb = is_write != 2;
8097e5e5a63SMax Filippov uint32_t wi;
8107e5e5a63SMax Filippov uint32_t ei;
8117e5e5a63SMax Filippov uint8_t ring;
8127e5e5a63SMax Filippov uint32_t vpn;
8137e5e5a63SMax Filippov uint32_t pte;
8147e5e5a63SMax Filippov const xtensa_tlb_entry *entry = NULL;
8157e5e5a63SMax Filippov xtensa_tlb_entry tmp_entry;
8167e5e5a63SMax Filippov int ret = xtensa_tlb_lookup(env, vaddr, dtlb, &wi, &ei, &ring);
8177e5e5a63SMax Filippov
8187e5e5a63SMax Filippov if ((ret == INST_TLB_MISS_CAUSE || ret == LOAD_STORE_TLB_MISS_CAUSE) &&
8197e5e5a63SMax Filippov may_lookup_pt && get_pte(env, vaddr, &pte)) {
8207e5e5a63SMax Filippov ring = (pte >> 4) & 0x3;
8217e5e5a63SMax Filippov wi = 0;
8227e5e5a63SMax Filippov split_tlb_entry_spec_way(env, vaddr, dtlb, &vpn, wi, &ei);
8237e5e5a63SMax Filippov
8247e5e5a63SMax Filippov if (update_tlb) {
8257e5e5a63SMax Filippov wi = ++env->autorefill_idx & 0x3;
8267e5e5a63SMax Filippov xtensa_tlb_set_entry(env, dtlb, wi, ei, vpn, pte);
8277e5e5a63SMax Filippov env->sregs[EXCVADDR] = vaddr;
8287e5e5a63SMax Filippov qemu_log_mask(CPU_LOG_MMU, "%s: autorefill(%08x): %08x -> %08x\n",
8297e5e5a63SMax Filippov __func__, vaddr, vpn, pte);
8307e5e5a63SMax Filippov } else {
8317e5e5a63SMax Filippov xtensa_tlb_set_entry_mmu(env, &tmp_entry, dtlb, wi, ei, vpn, pte);
8327e5e5a63SMax Filippov entry = &tmp_entry;
8337e5e5a63SMax Filippov }
8347e5e5a63SMax Filippov ret = 0;
8357e5e5a63SMax Filippov }
8367e5e5a63SMax Filippov if (ret != 0) {
8377e5e5a63SMax Filippov return ret;
8387e5e5a63SMax Filippov }
8397e5e5a63SMax Filippov
8407e5e5a63SMax Filippov if (entry == NULL) {
8417e5e5a63SMax Filippov entry = xtensa_tlb_get_entry(env, dtlb, wi, ei);
8427e5e5a63SMax Filippov }
8437e5e5a63SMax Filippov
8447e5e5a63SMax Filippov if (ring < mmu_idx) {
8457e5e5a63SMax Filippov return dtlb ?
8467e5e5a63SMax Filippov LOAD_STORE_PRIVILEGE_CAUSE :
8477e5e5a63SMax Filippov INST_FETCH_PRIVILEGE_CAUSE;
8487e5e5a63SMax Filippov }
8497e5e5a63SMax Filippov
8507e5e5a63SMax Filippov *access = mmu_attr_to_access(entry->attr) &
8517e5e5a63SMax Filippov ~(dtlb ? PAGE_EXEC : PAGE_READ | PAGE_WRITE);
8527e5e5a63SMax Filippov if (!is_access_granted(*access, is_write)) {
8537e5e5a63SMax Filippov return dtlb ?
8547e5e5a63SMax Filippov (is_write ?
8557e5e5a63SMax Filippov STORE_PROHIBITED_CAUSE :
8567e5e5a63SMax Filippov LOAD_PROHIBITED_CAUSE) :
8577e5e5a63SMax Filippov INST_FETCH_PROHIBITED_CAUSE;
8587e5e5a63SMax Filippov }
8597e5e5a63SMax Filippov
8607e5e5a63SMax Filippov *paddr = entry->paddr | (vaddr & ~xtensa_tlb_get_addr_mask(env, dtlb, wi));
8617e5e5a63SMax Filippov *page_size = ~xtensa_tlb_get_addr_mask(env, dtlb, wi) + 1;
8627e5e5a63SMax Filippov
8637e5e5a63SMax Filippov return 0;
8647e5e5a63SMax Filippov }
8657e5e5a63SMax Filippov
get_pte(CPUXtensaState * env,uint32_t vaddr,uint32_t * pte)8667e5e5a63SMax Filippov static bool get_pte(CPUXtensaState *env, uint32_t vaddr, uint32_t *pte)
8677e5e5a63SMax Filippov {
86892fddfbdSRichard Henderson CPUState *cs = env_cpu(env);
8697e5e5a63SMax Filippov uint32_t paddr;
8707e5e5a63SMax Filippov uint32_t page_size;
8717e5e5a63SMax Filippov unsigned access;
8727e5e5a63SMax Filippov uint32_t pt_vaddr =
8737e5e5a63SMax Filippov (env->sregs[PTEVADDR] | (vaddr >> 10)) & 0xfffffffc;
8747e5e5a63SMax Filippov int ret = get_physical_addr_mmu(env, false, pt_vaddr, 0, 0,
8757e5e5a63SMax Filippov &paddr, &page_size, &access, false);
8767e5e5a63SMax Filippov
8777e5e5a63SMax Filippov if (ret == 0) {
8787e5e5a63SMax Filippov qemu_log_mask(CPU_LOG_MMU,
8797e5e5a63SMax Filippov "%s: autorefill(%08x): PTE va = %08x, pa = %08x\n",
8807e5e5a63SMax Filippov __func__, vaddr, pt_vaddr, paddr);
8817e5e5a63SMax Filippov } else {
8827e5e5a63SMax Filippov qemu_log_mask(CPU_LOG_MMU,
8837e5e5a63SMax Filippov "%s: autorefill(%08x): PTE va = %08x, failed (%d)\n",
8847e5e5a63SMax Filippov __func__, vaddr, pt_vaddr, ret);
8857e5e5a63SMax Filippov }
8867e5e5a63SMax Filippov
8877e5e5a63SMax Filippov if (ret == 0) {
8887e5e5a63SMax Filippov MemTxResult result;
8897e5e5a63SMax Filippov
8907e5e5a63SMax Filippov *pte = address_space_ldl(cs->as, paddr, MEMTXATTRS_UNSPECIFIED,
8917e5e5a63SMax Filippov &result);
8927e5e5a63SMax Filippov if (result != MEMTX_OK) {
8937e5e5a63SMax Filippov qemu_log_mask(CPU_LOG_MMU,
8947e5e5a63SMax Filippov "%s: couldn't load PTE: transaction failed (%u)\n",
8957e5e5a63SMax Filippov __func__, (unsigned)result);
8967e5e5a63SMax Filippov ret = 1;
8977e5e5a63SMax Filippov }
8987e5e5a63SMax Filippov }
8997e5e5a63SMax Filippov return ret == 0;
9007e5e5a63SMax Filippov }
9017e5e5a63SMax Filippov
get_physical_addr_region(CPUXtensaState * env,uint32_t vaddr,int is_write,int mmu_idx,uint32_t * paddr,uint32_t * page_size,unsigned * access)9027e5e5a63SMax Filippov static int get_physical_addr_region(CPUXtensaState *env,
9037e5e5a63SMax Filippov uint32_t vaddr, int is_write, int mmu_idx,
9047e5e5a63SMax Filippov uint32_t *paddr, uint32_t *page_size,
9057e5e5a63SMax Filippov unsigned *access)
9067e5e5a63SMax Filippov {
9077e5e5a63SMax Filippov bool dtlb = is_write != 2;
9087e5e5a63SMax Filippov uint32_t wi = 0;
9097e5e5a63SMax Filippov uint32_t ei = (vaddr >> 29) & 0x7;
9107e5e5a63SMax Filippov const xtensa_tlb_entry *entry =
9117e5e5a63SMax Filippov xtensa_tlb_get_entry(env, dtlb, wi, ei);
9127e5e5a63SMax Filippov
9137e5e5a63SMax Filippov *access = region_attr_to_access(entry->attr);
9147e5e5a63SMax Filippov if (!is_access_granted(*access, is_write)) {
9157e5e5a63SMax Filippov return dtlb ?
9167e5e5a63SMax Filippov (is_write ?
9177e5e5a63SMax Filippov STORE_PROHIBITED_CAUSE :
9187e5e5a63SMax Filippov LOAD_PROHIBITED_CAUSE) :
9197e5e5a63SMax Filippov INST_FETCH_PROHIBITED_CAUSE;
9207e5e5a63SMax Filippov }
9217e5e5a63SMax Filippov
9227e5e5a63SMax Filippov *paddr = entry->paddr | (vaddr & ~REGION_PAGE_MASK);
9237e5e5a63SMax Filippov *page_size = ~REGION_PAGE_MASK + 1;
9247e5e5a63SMax Filippov
9257e5e5a63SMax Filippov return 0;
9267e5e5a63SMax Filippov }
9277e5e5a63SMax Filippov
xtensa_mpu_lookup(const xtensa_mpu_entry * entry,unsigned n,uint32_t vaddr,unsigned * segment)9284d04ea35SMax Filippov static int xtensa_mpu_lookup(const xtensa_mpu_entry *entry, unsigned n,
9294d04ea35SMax Filippov uint32_t vaddr, unsigned *segment)
9304d04ea35SMax Filippov {
9314d04ea35SMax Filippov unsigned nhits = 0;
9324d04ea35SMax Filippov unsigned i;
9334d04ea35SMax Filippov
9344d04ea35SMax Filippov for (i = 0; i < n; ++i) {
9354d04ea35SMax Filippov if (vaddr >= entry[i].vaddr &&
9364d04ea35SMax Filippov (i == n - 1 || vaddr < entry[i + 1].vaddr)) {
9374d04ea35SMax Filippov if (nhits++) {
9384d04ea35SMax Filippov break;
9394d04ea35SMax Filippov }
9404d04ea35SMax Filippov *segment = i;
9414d04ea35SMax Filippov }
9424d04ea35SMax Filippov }
9434d04ea35SMax Filippov return nhits;
9444d04ea35SMax Filippov }
9454d04ea35SMax Filippov
HELPER(wsr_mpuenb)9464d04ea35SMax Filippov void HELPER(wsr_mpuenb)(CPUXtensaState *env, uint32_t v)
9474d04ea35SMax Filippov {
9484d04ea35SMax Filippov v &= (2u << (env->config->n_mpu_fg_segments - 1)) - 1;
9494d04ea35SMax Filippov
9504d04ea35SMax Filippov if (v != env->sregs[MPUENB]) {
9514d04ea35SMax Filippov env->sregs[MPUENB] = v;
95292fddfbdSRichard Henderson tlb_flush(env_cpu(env));
9534d04ea35SMax Filippov }
9544d04ea35SMax Filippov }
9554d04ea35SMax Filippov
HELPER(wptlb)9564d04ea35SMax Filippov void HELPER(wptlb)(CPUXtensaState *env, uint32_t p, uint32_t v)
9574d04ea35SMax Filippov {
9584d04ea35SMax Filippov unsigned segment = p & XTENSA_MPU_SEGMENT_MASK;
9594d04ea35SMax Filippov
9604d04ea35SMax Filippov if (segment < env->config->n_mpu_fg_segments) {
9614d04ea35SMax Filippov env->mpu_fg[segment].vaddr = v & -env->config->mpu_align;
9624d04ea35SMax Filippov env->mpu_fg[segment].attr = p & XTENSA_MPU_ATTR_MASK;
9634d04ea35SMax Filippov env->sregs[MPUENB] = deposit32(env->sregs[MPUENB], segment, 1, v);
96492fddfbdSRichard Henderson tlb_flush(env_cpu(env));
9654d04ea35SMax Filippov }
9664d04ea35SMax Filippov }
9674d04ea35SMax Filippov
HELPER(rptlb0)9684d04ea35SMax Filippov uint32_t HELPER(rptlb0)(CPUXtensaState *env, uint32_t s)
9694d04ea35SMax Filippov {
9704d04ea35SMax Filippov unsigned segment = s & XTENSA_MPU_SEGMENT_MASK;
9714d04ea35SMax Filippov
9724d04ea35SMax Filippov if (segment < env->config->n_mpu_fg_segments) {
9734d04ea35SMax Filippov return env->mpu_fg[segment].vaddr |
9744d04ea35SMax Filippov extract32(env->sregs[MPUENB], segment, 1);
9754d04ea35SMax Filippov } else {
9764d04ea35SMax Filippov return 0;
9774d04ea35SMax Filippov }
9784d04ea35SMax Filippov }
9794d04ea35SMax Filippov
HELPER(rptlb1)9804d04ea35SMax Filippov uint32_t HELPER(rptlb1)(CPUXtensaState *env, uint32_t s)
9814d04ea35SMax Filippov {
9824d04ea35SMax Filippov unsigned segment = s & XTENSA_MPU_SEGMENT_MASK;
9834d04ea35SMax Filippov
9844d04ea35SMax Filippov if (segment < env->config->n_mpu_fg_segments) {
9854d04ea35SMax Filippov return env->mpu_fg[segment].attr;
9864d04ea35SMax Filippov } else {
9874d04ea35SMax Filippov return 0;
9884d04ea35SMax Filippov }
9894d04ea35SMax Filippov }
9904d04ea35SMax Filippov
HELPER(pptlb)9914d04ea35SMax Filippov uint32_t HELPER(pptlb)(CPUXtensaState *env, uint32_t v)
9924d04ea35SMax Filippov {
9934d04ea35SMax Filippov unsigned nhits;
994*b42ba4eaSPeter Maydell unsigned segment;
9954d04ea35SMax Filippov unsigned bg_segment;
9964d04ea35SMax Filippov
9974d04ea35SMax Filippov nhits = xtensa_mpu_lookup(env->mpu_fg, env->config->n_mpu_fg_segments,
9984d04ea35SMax Filippov v, &segment);
9994d04ea35SMax Filippov if (nhits > 1) {
10004d04ea35SMax Filippov HELPER(exception_cause_vaddr)(env, env->pc,
10014d04ea35SMax Filippov LOAD_STORE_TLB_MULTI_HIT_CAUSE, v);
10024d04ea35SMax Filippov } else if (nhits == 1 && (env->sregs[MPUENB] & (1u << segment))) {
10034d04ea35SMax Filippov return env->mpu_fg[segment].attr | segment | XTENSA_MPU_PROBE_V;
10044d04ea35SMax Filippov } else {
10054d04ea35SMax Filippov xtensa_mpu_lookup(env->config->mpu_bg,
10064d04ea35SMax Filippov env->config->n_mpu_bg_segments,
10074d04ea35SMax Filippov v, &bg_segment);
1008*b42ba4eaSPeter Maydell return env->config->mpu_bg[bg_segment].attr | XTENSA_MPU_PROBE_B;
10094d04ea35SMax Filippov }
10104d04ea35SMax Filippov }
10114d04ea35SMax Filippov
get_physical_addr_mpu(CPUXtensaState * env,uint32_t vaddr,int is_write,int mmu_idx,uint32_t * paddr,uint32_t * page_size,unsigned * access)10124d04ea35SMax Filippov static int get_physical_addr_mpu(CPUXtensaState *env,
10134d04ea35SMax Filippov uint32_t vaddr, int is_write, int mmu_idx,
10144d04ea35SMax Filippov uint32_t *paddr, uint32_t *page_size,
10154d04ea35SMax Filippov unsigned *access)
10164d04ea35SMax Filippov {
10174d04ea35SMax Filippov unsigned nhits;
10184d04ea35SMax Filippov unsigned segment;
10194d04ea35SMax Filippov uint32_t attr;
10204d04ea35SMax Filippov
10214d04ea35SMax Filippov nhits = xtensa_mpu_lookup(env->mpu_fg, env->config->n_mpu_fg_segments,
10224d04ea35SMax Filippov vaddr, &segment);
10234d04ea35SMax Filippov if (nhits > 1) {
10244d04ea35SMax Filippov return is_write < 2 ?
10254d04ea35SMax Filippov LOAD_STORE_TLB_MULTI_HIT_CAUSE :
10264d04ea35SMax Filippov INST_TLB_MULTI_HIT_CAUSE;
10274d04ea35SMax Filippov } else if (nhits == 1 && (env->sregs[MPUENB] & (1u << segment))) {
10284d04ea35SMax Filippov attr = env->mpu_fg[segment].attr;
10294d04ea35SMax Filippov } else {
10304d04ea35SMax Filippov xtensa_mpu_lookup(env->config->mpu_bg,
10314d04ea35SMax Filippov env->config->n_mpu_bg_segments,
10324d04ea35SMax Filippov vaddr, &segment);
10334d04ea35SMax Filippov attr = env->config->mpu_bg[segment].attr;
10344d04ea35SMax Filippov }
10354d04ea35SMax Filippov
10364d04ea35SMax Filippov *access = mpu_attr_to_access(attr, mmu_idx);
10374d04ea35SMax Filippov if (!is_access_granted(*access, is_write)) {
10384d04ea35SMax Filippov return is_write < 2 ?
10394d04ea35SMax Filippov (is_write ?
10404d04ea35SMax Filippov STORE_PROHIBITED_CAUSE :
10414d04ea35SMax Filippov LOAD_PROHIBITED_CAUSE) :
10424d04ea35SMax Filippov INST_FETCH_PROHIBITED_CAUSE;
10434d04ea35SMax Filippov }
10444d04ea35SMax Filippov *paddr = vaddr;
10454d04ea35SMax Filippov *page_size = env->config->mpu_align;
10464d04ea35SMax Filippov return 0;
10474d04ea35SMax Filippov }
10484d04ea35SMax Filippov
10497e5e5a63SMax Filippov /*!
10507e5e5a63SMax Filippov * Convert virtual address to physical addr.
10517e5e5a63SMax Filippov * MMU may issue pagewalk and change xtensa autorefill TLB way entry.
10527e5e5a63SMax Filippov *
10537e5e5a63SMax Filippov * \return 0 if ok, exception cause code otherwise
10547e5e5a63SMax Filippov */
xtensa_get_physical_addr(CPUXtensaState * env,bool update_tlb,uint32_t vaddr,int is_write,int mmu_idx,uint32_t * paddr,uint32_t * page_size,unsigned * access)10557e5e5a63SMax Filippov int xtensa_get_physical_addr(CPUXtensaState *env, bool update_tlb,
10567e5e5a63SMax Filippov uint32_t vaddr, int is_write, int mmu_idx,
10577e5e5a63SMax Filippov uint32_t *paddr, uint32_t *page_size,
10587e5e5a63SMax Filippov unsigned *access)
10597e5e5a63SMax Filippov {
10607e5e5a63SMax Filippov if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
10617e5e5a63SMax Filippov return get_physical_addr_mmu(env, update_tlb,
10627e5e5a63SMax Filippov vaddr, is_write, mmu_idx, paddr,
10637e5e5a63SMax Filippov page_size, access, true);
10647e5e5a63SMax Filippov } else if (xtensa_option_bits_enabled(env->config,
10657e5e5a63SMax Filippov XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION) |
10667e5e5a63SMax Filippov XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION))) {
10677e5e5a63SMax Filippov return get_physical_addr_region(env, vaddr, is_write, mmu_idx,
10687e5e5a63SMax Filippov paddr, page_size, access);
10694d04ea35SMax Filippov } else if (xtensa_option_enabled(env->config, XTENSA_OPTION_MPU)) {
10704d04ea35SMax Filippov return get_physical_addr_mpu(env, vaddr, is_write, mmu_idx,
10714d04ea35SMax Filippov paddr, page_size, access);
10727e5e5a63SMax Filippov } else {
10737e5e5a63SMax Filippov *paddr = vaddr;
10747e5e5a63SMax Filippov *page_size = TARGET_PAGE_SIZE;
10757e5e5a63SMax Filippov *access = cacheattr_attr_to_access(env->sregs[CACHEATTR] >>
10767e5e5a63SMax Filippov ((vaddr & 0xe0000000) >> 27));
10777e5e5a63SMax Filippov return 0;
10787e5e5a63SMax Filippov }
10797e5e5a63SMax Filippov }
10807e5e5a63SMax Filippov
dump_tlb(CPUXtensaState * env,bool dtlb)1081fad866daSMarkus Armbruster static void dump_tlb(CPUXtensaState *env, bool dtlb)
10827e5e5a63SMax Filippov {
10837e5e5a63SMax Filippov unsigned wi, ei;
10847e5e5a63SMax Filippov const xtensa_tlb *conf =
10857e5e5a63SMax Filippov dtlb ? &env->config->dtlb : &env->config->itlb;
10867e5e5a63SMax Filippov unsigned (*attr_to_access)(uint32_t) =
10877e5e5a63SMax Filippov xtensa_option_enabled(env->config, XTENSA_OPTION_MMU) ?
10887e5e5a63SMax Filippov mmu_attr_to_access : region_attr_to_access;
10897e5e5a63SMax Filippov
10907e5e5a63SMax Filippov for (wi = 0; wi < conf->nways; ++wi) {
10917e5e5a63SMax Filippov uint32_t sz = ~xtensa_tlb_get_addr_mask(env, dtlb, wi) + 1;
10927e5e5a63SMax Filippov const char *sz_text;
10937e5e5a63SMax Filippov bool print_header = true;
10947e5e5a63SMax Filippov
10957e5e5a63SMax Filippov if (sz >= 0x100000) {
10967e5e5a63SMax Filippov sz /= MiB;
10977e5e5a63SMax Filippov sz_text = "MB";
10987e5e5a63SMax Filippov } else {
10997e5e5a63SMax Filippov sz /= KiB;
11007e5e5a63SMax Filippov sz_text = "KB";
11017e5e5a63SMax Filippov }
11027e5e5a63SMax Filippov
11037e5e5a63SMax Filippov for (ei = 0; ei < conf->way_size[wi]; ++ei) {
11047e5e5a63SMax Filippov const xtensa_tlb_entry *entry =
11057e5e5a63SMax Filippov xtensa_tlb_get_entry(env, dtlb, wi, ei);
11067e5e5a63SMax Filippov
11077e5e5a63SMax Filippov if (entry->asid) {
11087e5e5a63SMax Filippov static const char * const cache_text[8] = {
11097e5e5a63SMax Filippov [PAGE_CACHE_BYPASS >> PAGE_CACHE_SHIFT] = "Bypass",
11107e5e5a63SMax Filippov [PAGE_CACHE_WT >> PAGE_CACHE_SHIFT] = "WT",
11117e5e5a63SMax Filippov [PAGE_CACHE_WB >> PAGE_CACHE_SHIFT] = "WB",
11127e5e5a63SMax Filippov [PAGE_CACHE_ISOLATE >> PAGE_CACHE_SHIFT] = "Isolate",
11137e5e5a63SMax Filippov };
11147e5e5a63SMax Filippov unsigned access = attr_to_access(entry->attr);
11157e5e5a63SMax Filippov unsigned cache_idx = (access & PAGE_CACHE_MASK) >>
11167e5e5a63SMax Filippov PAGE_CACHE_SHIFT;
11177e5e5a63SMax Filippov
11187e5e5a63SMax Filippov if (print_header) {
11197e5e5a63SMax Filippov print_header = false;
1120fad866daSMarkus Armbruster qemu_printf("Way %u (%d %s)\n", wi, sz, sz_text);
1121fad866daSMarkus Armbruster qemu_printf("\tVaddr Paddr ASID Attr RWX Cache\n"
11227e5e5a63SMax Filippov "\t---------- ---------- ---- ---- --- -------\n");
11237e5e5a63SMax Filippov }
112461848717SMarkus Armbruster qemu_printf("\t0x%08x 0x%08x 0x%02x 0x%02x %c%c%c %s\n",
11257e5e5a63SMax Filippov entry->vaddr,
11267e5e5a63SMax Filippov entry->paddr,
11277e5e5a63SMax Filippov entry->asid,
11287e5e5a63SMax Filippov entry->attr,
11297e5e5a63SMax Filippov (access & PAGE_READ) ? 'R' : '-',
11307e5e5a63SMax Filippov (access & PAGE_WRITE) ? 'W' : '-',
11317e5e5a63SMax Filippov (access & PAGE_EXEC) ? 'X' : '-',
11327e5e5a63SMax Filippov cache_text[cache_idx] ?
11337e5e5a63SMax Filippov cache_text[cache_idx] : "Invalid");
11347e5e5a63SMax Filippov }
11357e5e5a63SMax Filippov }
11367e5e5a63SMax Filippov }
11377e5e5a63SMax Filippov }
11387e5e5a63SMax Filippov
dump_mpu(CPUXtensaState * env,const xtensa_mpu_entry * entry,unsigned n)11394d04ea35SMax Filippov static void dump_mpu(CPUXtensaState *env,
11404d04ea35SMax Filippov const xtensa_mpu_entry *entry, unsigned n)
11414d04ea35SMax Filippov {
11424d04ea35SMax Filippov unsigned i;
11434d04ea35SMax Filippov
11444d04ea35SMax Filippov qemu_printf("\t%s Vaddr Attr Ring0 Ring1 System Type CPU cache\n"
11454d04ea35SMax Filippov "\t%s ---------- ---------- ----- ----- ------------- ---------\n",
11464d04ea35SMax Filippov env ? "En" : " ",
11474d04ea35SMax Filippov env ? "--" : " ");
11484d04ea35SMax Filippov
11494d04ea35SMax Filippov for (i = 0; i < n; ++i) {
11504d04ea35SMax Filippov uint32_t attr = entry[i].attr;
11514d04ea35SMax Filippov unsigned access0 = mpu_attr_to_access(attr, 0);
11524d04ea35SMax Filippov unsigned access1 = mpu_attr_to_access(attr, 1);
11534d04ea35SMax Filippov unsigned type = mpu_attr_to_type(attr);
11544d04ea35SMax Filippov char cpu_cache = (type & XTENSA_MPU_TYPE_CPU_CACHE) ? '-' : ' ';
11554d04ea35SMax Filippov
11564d04ea35SMax Filippov qemu_printf("\t %c 0x%08x 0x%08x %c%c%c %c%c%c ",
11574d04ea35SMax Filippov env ?
11584d04ea35SMax Filippov ((env->sregs[MPUENB] & (1u << i)) ? '+' : '-') : ' ',
11594d04ea35SMax Filippov entry[i].vaddr, attr,
11604d04ea35SMax Filippov (access0 & PAGE_READ) ? 'R' : '-',
11614d04ea35SMax Filippov (access0 & PAGE_WRITE) ? 'W' : '-',
11624d04ea35SMax Filippov (access0 & PAGE_EXEC) ? 'X' : '-',
11634d04ea35SMax Filippov (access1 & PAGE_READ) ? 'R' : '-',
11644d04ea35SMax Filippov (access1 & PAGE_WRITE) ? 'W' : '-',
11654d04ea35SMax Filippov (access1 & PAGE_EXEC) ? 'X' : '-');
11664d04ea35SMax Filippov
11674d04ea35SMax Filippov switch (type & XTENSA_MPU_SYSTEM_TYPE_MASK) {
11684d04ea35SMax Filippov case XTENSA_MPU_SYSTEM_TYPE_DEVICE:
11694d04ea35SMax Filippov qemu_printf("Device %cB %3s\n",
11704d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_B) ? ' ' : 'n',
11714d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_INT) ? "int" : "");
11724d04ea35SMax Filippov break;
11734d04ea35SMax Filippov case XTENSA_MPU_SYSTEM_TYPE_NC:
11744d04ea35SMax Filippov qemu_printf("Sys NC %cB %c%c%c\n",
11754d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_B) ? ' ' : 'n',
11764d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_CPU_R) ? 'r' : cpu_cache,
11774d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_CPU_W) ? 'w' : cpu_cache,
11784d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_CPU_C) ? 'c' : cpu_cache);
11794d04ea35SMax Filippov break;
11804d04ea35SMax Filippov case XTENSA_MPU_SYSTEM_TYPE_C:
11814d04ea35SMax Filippov qemu_printf("Sys C %c%c%c %c%c%c\n",
11824d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_SYS_R) ? 'R' : '-',
11834d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_SYS_W) ? 'W' : '-',
11844d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_SYS_C) ? 'C' : '-',
11854d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_CPU_R) ? 'r' : cpu_cache,
11864d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_CPU_W) ? 'w' : cpu_cache,
11874d04ea35SMax Filippov (type & XTENSA_MPU_TYPE_CPU_C) ? 'c' : cpu_cache);
11884d04ea35SMax Filippov break;
11894d04ea35SMax Filippov default:
11904d04ea35SMax Filippov qemu_printf("Unknown\n");
11914d04ea35SMax Filippov break;
11924d04ea35SMax Filippov }
11934d04ea35SMax Filippov }
11944d04ea35SMax Filippov }
11954d04ea35SMax Filippov
dump_mmu(CPUXtensaState * env)1196fad866daSMarkus Armbruster void dump_mmu(CPUXtensaState *env)
11977e5e5a63SMax Filippov {
11987e5e5a63SMax Filippov if (xtensa_option_bits_enabled(env->config,
11997e5e5a63SMax Filippov XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION) |
12007e5e5a63SMax Filippov XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION) |
12017e5e5a63SMax Filippov XTENSA_OPTION_BIT(XTENSA_OPTION_MMU))) {
12027e5e5a63SMax Filippov
1203fad866daSMarkus Armbruster qemu_printf("ITLB:\n");
1204fad866daSMarkus Armbruster dump_tlb(env, false);
1205fad866daSMarkus Armbruster qemu_printf("\nDTLB:\n");
1206fad866daSMarkus Armbruster dump_tlb(env, true);
12074d04ea35SMax Filippov } else if (xtensa_option_enabled(env->config, XTENSA_OPTION_MPU)) {
12084d04ea35SMax Filippov qemu_printf("Foreground map:\n");
12094d04ea35SMax Filippov dump_mpu(env, env->mpu_fg, env->config->n_mpu_fg_segments);
12104d04ea35SMax Filippov qemu_printf("\nBackground map:\n");
12114d04ea35SMax Filippov dump_mpu(NULL, env->config->mpu_bg, env->config->n_mpu_bg_segments);
12127e5e5a63SMax Filippov } else {
1213fad866daSMarkus Armbruster qemu_printf("No TLB for this CPU core\n");
12147e5e5a63SMax Filippov }
12157e5e5a63SMax Filippov }
1216