xref: /openbmc/qemu/target/ppc/mmu_helper.c (revision 173c427eb5705064da7dc8db22553c8df34f7f58)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2007 Jocelyn Mayer
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
96bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19db725815SMarkus Armbruster 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21ab3dd749SPhilippe Mathieu-Daudé #include "qemu/units.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "sysemu/kvm.h"
24fcf5ef2aSThomas Huth #include "kvm_ppc.h"
25fcf5ef2aSThomas Huth #include "mmu-hash64.h"
26fcf5ef2aSThomas Huth #include "mmu-hash32.h"
27fcf5ef2aSThomas Huth #include "exec/exec-all.h"
2874781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
29fcf5ef2aSThomas Huth #include "exec/log.h"
30fcf5ef2aSThomas Huth #include "helper_regs.h"
318d63351fSSuraj Jitindar Singh #include "qemu/error-report.h"
32fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
3391e615a0SRichard Henderson #include "internal.h"
34b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
3595cb0657SSuraj Jitindar Singh #include "mmu-radix64.h"
36e7baac64SBALATON Zoltan #include "mmu-booke.h"
372b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h"
382b44e219SBruno Larsen (billionai) #include "exec/cpu_ldst.h"
395118ebe8SLucas Mateus Castro (alqotel) 
40fe4ade31SDavid Gibson /* #define FLUSH_ALL_TLBS */
41fcf5ef2aSThomas Huth 
42fcf5ef2aSThomas Huth /*****************************************************************************/
43fcf5ef2aSThomas Huth /* PowerPC MMU emulation */
44fcf5ef2aSThomas Huth 
45fcf5ef2aSThomas Huth /* Software driven TLB helpers */
ppc6xx_tlb_invalidate_all(CPUPPCState * env)46fcf5ef2aSThomas Huth static inline void ppc6xx_tlb_invalidate_all(CPUPPCState *env)
47fcf5ef2aSThomas Huth {
48fcf5ef2aSThomas Huth     ppc6xx_tlb_t *tlb;
495fd257f5SBALATON Zoltan     int nr, max = 2 * env->nb_tlb;
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth     for (nr = 0; nr < max; nr++) {
52fcf5ef2aSThomas Huth         tlb = &env->tlb.tlb6[nr];
53fcf5ef2aSThomas Huth         pte_invalidate(&tlb->pte0);
54fcf5ef2aSThomas Huth     }
55db70b311SRichard Henderson     tlb_flush(env_cpu(env));
56fcf5ef2aSThomas Huth }
57fcf5ef2aSThomas Huth 
ppc6xx_tlb_invalidate_virt2(CPUPPCState * env,target_ulong eaddr,int is_code,int match_epn)58fcf5ef2aSThomas Huth static inline void ppc6xx_tlb_invalidate_virt2(CPUPPCState *env,
59fcf5ef2aSThomas Huth                                                target_ulong eaddr,
60fcf5ef2aSThomas Huth                                                int is_code, int match_epn)
61fcf5ef2aSThomas Huth {
62fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS)
63db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
64fcf5ef2aSThomas Huth     ppc6xx_tlb_t *tlb;
65fcf5ef2aSThomas Huth     int way, nr;
66fcf5ef2aSThomas Huth 
67fcf5ef2aSThomas Huth     /* Invalidate ITLB + DTLB, all ways */
68fcf5ef2aSThomas Huth     for (way = 0; way < env->nb_ways; way++) {
69fcf5ef2aSThomas Huth         nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
70fcf5ef2aSThomas Huth         tlb = &env->tlb.tlb6[nr];
71fcf5ef2aSThomas Huth         if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
7256964585SCédric Le Goater             qemu_log_mask(CPU_LOG_MMU, "TLB invalidate %d/%d "
7356964585SCédric Le Goater                           TARGET_FMT_lx "\n", nr, env->nb_tlb, eaddr);
74fcf5ef2aSThomas Huth             pte_invalidate(&tlb->pte0);
75fcf5ef2aSThomas Huth             tlb_flush_page(cs, tlb->EPN);
76fcf5ef2aSThomas Huth         }
77fcf5ef2aSThomas Huth     }
78fcf5ef2aSThomas Huth #else
79fcf5ef2aSThomas Huth     /* XXX: PowerPC specification say this is valid as well */
80fcf5ef2aSThomas Huth     ppc6xx_tlb_invalidate_all(env);
81fcf5ef2aSThomas Huth #endif
82fcf5ef2aSThomas Huth }
83fcf5ef2aSThomas Huth 
ppc6xx_tlb_invalidate_virt(CPUPPCState * env,target_ulong eaddr,int is_code)84fcf5ef2aSThomas Huth static inline void ppc6xx_tlb_invalidate_virt(CPUPPCState *env,
85fcf5ef2aSThomas Huth                                               target_ulong eaddr, int is_code)
86fcf5ef2aSThomas Huth {
87fcf5ef2aSThomas Huth     ppc6xx_tlb_invalidate_virt2(env, eaddr, is_code, 0);
88fcf5ef2aSThomas Huth }
89fcf5ef2aSThomas Huth 
ppc6xx_tlb_store(CPUPPCState * env,target_ulong EPN,int way,int is_code,target_ulong pte0,target_ulong pte1)90fcf5ef2aSThomas Huth static void ppc6xx_tlb_store(CPUPPCState *env, target_ulong EPN, int way,
91fcf5ef2aSThomas Huth                              int is_code, target_ulong pte0, target_ulong pte1)
92fcf5ef2aSThomas Huth {
93fcf5ef2aSThomas Huth     ppc6xx_tlb_t *tlb;
94fcf5ef2aSThomas Huth     int nr;
95fcf5ef2aSThomas Huth 
96fcf5ef2aSThomas Huth     nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
97fcf5ef2aSThomas Huth     tlb = &env->tlb.tlb6[nr];
9856964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "Set TLB %d/%d EPN " TARGET_FMT_lx " PTE0 "
9956964585SCédric Le Goater                   TARGET_FMT_lx " PTE1 " TARGET_FMT_lx "\n", nr, env->nb_tlb,
10056964585SCédric Le Goater                   EPN, pte0, pte1);
101fcf5ef2aSThomas Huth     /* Invalidate any pending reference in QEMU for this virtual address */
102fcf5ef2aSThomas Huth     ppc6xx_tlb_invalidate_virt2(env, EPN, is_code, 1);
103fcf5ef2aSThomas Huth     tlb->pte0 = pte0;
104fcf5ef2aSThomas Huth     tlb->pte1 = pte1;
105fcf5ef2aSThomas Huth     tlb->EPN = EPN;
106fcf5ef2aSThomas Huth     /* Store last way for LRU mechanism */
107fcf5ef2aSThomas Huth     env->last_way = way;
108fcf5ef2aSThomas Huth }
109fcf5ef2aSThomas Huth 
110fcf5ef2aSThomas Huth /* Helpers specific to PowerPC 40x implementations */
ppc4xx_tlb_invalidate_all(CPUPPCState * env)111fcf5ef2aSThomas Huth static inline void ppc4xx_tlb_invalidate_all(CPUPPCState *env)
112fcf5ef2aSThomas Huth {
113fcf5ef2aSThomas Huth     ppcemb_tlb_t *tlb;
114fcf5ef2aSThomas Huth     int i;
115fcf5ef2aSThomas Huth 
116fcf5ef2aSThomas Huth     for (i = 0; i < env->nb_tlb; i++) {
117fcf5ef2aSThomas Huth         tlb = &env->tlb.tlbe[i];
118fcf5ef2aSThomas Huth         tlb->prot &= ~PAGE_VALID;
119fcf5ef2aSThomas Huth     }
120db70b311SRichard Henderson     tlb_flush(env_cpu(env));
121fcf5ef2aSThomas Huth }
122fcf5ef2aSThomas Huth 
booke206_flush_tlb(CPUPPCState * env,int flags,const int check_iprot)123fcf5ef2aSThomas Huth static void booke206_flush_tlb(CPUPPCState *env, int flags,
124fcf5ef2aSThomas Huth                                const int check_iprot)
125fcf5ef2aSThomas Huth {
126fcf5ef2aSThomas Huth     int tlb_size;
127fcf5ef2aSThomas Huth     int i, j;
128fcf5ef2aSThomas Huth     ppcmas_tlb_t *tlb = env->tlb.tlbm;
129fcf5ef2aSThomas Huth 
130fcf5ef2aSThomas Huth     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
131fcf5ef2aSThomas Huth         if (flags & (1 << i)) {
132fcf5ef2aSThomas Huth             tlb_size = booke206_tlb_size(env, i);
133fcf5ef2aSThomas Huth             for (j = 0; j < tlb_size; j++) {
134fcf5ef2aSThomas Huth                 if (!check_iprot || !(tlb[j].mas1 & MAS1_IPROT)) {
135fcf5ef2aSThomas Huth                     tlb[j].mas1 &= ~MAS1_VALID;
136fcf5ef2aSThomas Huth                 }
137fcf5ef2aSThomas Huth             }
138fcf5ef2aSThomas Huth         }
139fcf5ef2aSThomas Huth         tlb += booke206_tlb_size(env, i);
140fcf5ef2aSThomas Huth     }
141fcf5ef2aSThomas Huth 
142db70b311SRichard Henderson     tlb_flush(env_cpu(env));
143fcf5ef2aSThomas Huth }
144fcf5ef2aSThomas Huth 
145fcf5ef2aSThomas Huth /*****************************************************************************/
146fcf5ef2aSThomas Huth /* BATs management */
147fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS)
do_invalidate_BAT(CPUPPCState * env,target_ulong BATu,target_ulong mask)148fcf5ef2aSThomas Huth static inline void do_invalidate_BAT(CPUPPCState *env, target_ulong BATu,
149fcf5ef2aSThomas Huth                                      target_ulong mask)
150fcf5ef2aSThomas Huth {
151db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
152fcf5ef2aSThomas Huth     target_ulong base, end, page;
153fcf5ef2aSThomas Huth 
154fcf5ef2aSThomas Huth     base = BATu & ~0x0001FFFF;
155fcf5ef2aSThomas Huth     end = base + mask + 0x00020000;
156aaef873bSArtyom Tarasenko     if (((end - base) >> TARGET_PAGE_BITS) > 1024) {
157aaef873bSArtyom Tarasenko         /* Flushing 1024 4K pages is slower than a complete flush */
15856964585SCédric Le Goater         qemu_log_mask(CPU_LOG_MMU, "Flush all BATs\n");
15996449e4aSPhilippe Mathieu-Daudé         tlb_flush(cs);
16056964585SCédric Le Goater         qemu_log_mask(CPU_LOG_MMU, "Flush done\n");
161aaef873bSArtyom Tarasenko         return;
162aaef873bSArtyom Tarasenko     }
16356964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "Flush BAT from " TARGET_FMT_lx
16456964585SCédric Le Goater                   " to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
16556964585SCédric Le Goater                   base, end, mask);
166fcf5ef2aSThomas Huth     for (page = base; page != end; page += TARGET_PAGE_SIZE) {
167fcf5ef2aSThomas Huth         tlb_flush_page(cs, page);
168fcf5ef2aSThomas Huth     }
16956964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "Flush done\n");
170fcf5ef2aSThomas Huth }
171fcf5ef2aSThomas Huth #endif
172fcf5ef2aSThomas Huth 
dump_store_bat(CPUPPCState * env,char ID,int ul,int nr,target_ulong value)173fcf5ef2aSThomas Huth static inline void dump_store_bat(CPUPPCState *env, char ID, int ul, int nr,
174fcf5ef2aSThomas Huth                                   target_ulong value)
175fcf5ef2aSThomas Huth {
17656964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "Set %cBAT%d%c to " TARGET_FMT_lx " ("
17756964585SCédric Le Goater                   TARGET_FMT_lx ")\n", ID, nr, ul == 0 ? 'u' : 'l',
17856964585SCédric Le Goater                   value, env->nip);
179fcf5ef2aSThomas Huth }
180fcf5ef2aSThomas Huth 
helper_store_ibatu(CPUPPCState * env,uint32_t nr,target_ulong value)181fcf5ef2aSThomas Huth void helper_store_ibatu(CPUPPCState *env, uint32_t nr, target_ulong value)
182fcf5ef2aSThomas Huth {
183fcf5ef2aSThomas Huth     target_ulong mask;
184fcf5ef2aSThomas Huth 
185fcf5ef2aSThomas Huth     dump_store_bat(env, 'I', 0, nr, value);
186fcf5ef2aSThomas Huth     if (env->IBAT[0][nr] != value) {
187fcf5ef2aSThomas Huth         mask = (value << 15) & 0x0FFE0000UL;
188fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS)
189fcf5ef2aSThomas Huth         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
190fcf5ef2aSThomas Huth #endif
191fe4ade31SDavid Gibson         /*
192fe4ade31SDavid Gibson          * When storing valid upper BAT, mask BEPI and BRPN and
193fe4ade31SDavid Gibson          * invalidate all TLBs covered by this BAT
194fcf5ef2aSThomas Huth          */
195fcf5ef2aSThomas Huth         mask = (value << 15) & 0x0FFE0000UL;
196fcf5ef2aSThomas Huth         env->IBAT[0][nr] = (value & 0x00001FFFUL) |
197fcf5ef2aSThomas Huth             (value & ~0x0001FFFFUL & ~mask);
198fcf5ef2aSThomas Huth         env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
199fcf5ef2aSThomas Huth             (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
200fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS)
201fcf5ef2aSThomas Huth         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
202fcf5ef2aSThomas Huth #else
203db70b311SRichard Henderson         tlb_flush(env_cpu(env));
204fcf5ef2aSThomas Huth #endif
205fcf5ef2aSThomas Huth     }
206fcf5ef2aSThomas Huth }
207fcf5ef2aSThomas Huth 
helper_store_ibatl(CPUPPCState * env,uint32_t nr,target_ulong value)208fcf5ef2aSThomas Huth void helper_store_ibatl(CPUPPCState *env, uint32_t nr, target_ulong value)
209fcf5ef2aSThomas Huth {
210fcf5ef2aSThomas Huth     dump_store_bat(env, 'I', 1, nr, value);
211fcf5ef2aSThomas Huth     env->IBAT[1][nr] = value;
212fcf5ef2aSThomas Huth }
213fcf5ef2aSThomas Huth 
helper_store_dbatu(CPUPPCState * env,uint32_t nr,target_ulong value)214fcf5ef2aSThomas Huth void helper_store_dbatu(CPUPPCState *env, uint32_t nr, target_ulong value)
215fcf5ef2aSThomas Huth {
216fcf5ef2aSThomas Huth     target_ulong mask;
217fcf5ef2aSThomas Huth 
218fcf5ef2aSThomas Huth     dump_store_bat(env, 'D', 0, nr, value);
219fcf5ef2aSThomas Huth     if (env->DBAT[0][nr] != value) {
220fe4ade31SDavid Gibson         /*
221fe4ade31SDavid Gibson          * When storing valid upper BAT, mask BEPI and BRPN and
222fe4ade31SDavid Gibson          * invalidate all TLBs covered by this BAT
223fcf5ef2aSThomas Huth          */
224fcf5ef2aSThomas Huth         mask = (value << 15) & 0x0FFE0000UL;
225fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS)
226fcf5ef2aSThomas Huth         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
227fcf5ef2aSThomas Huth #endif
228fcf5ef2aSThomas Huth         mask = (value << 15) & 0x0FFE0000UL;
229fcf5ef2aSThomas Huth         env->DBAT[0][nr] = (value & 0x00001FFFUL) |
230fcf5ef2aSThomas Huth             (value & ~0x0001FFFFUL & ~mask);
231fcf5ef2aSThomas Huth         env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
232fcf5ef2aSThomas Huth             (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
233fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS)
234fcf5ef2aSThomas Huth         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
235fcf5ef2aSThomas Huth #else
236db70b311SRichard Henderson         tlb_flush(env_cpu(env));
237fcf5ef2aSThomas Huth #endif
238fcf5ef2aSThomas Huth     }
239fcf5ef2aSThomas Huth }
240fcf5ef2aSThomas Huth 
helper_store_dbatl(CPUPPCState * env,uint32_t nr,target_ulong value)241fcf5ef2aSThomas Huth void helper_store_dbatl(CPUPPCState *env, uint32_t nr, target_ulong value)
242fcf5ef2aSThomas Huth {
243fcf5ef2aSThomas Huth     dump_store_bat(env, 'D', 1, nr, value);
244fcf5ef2aSThomas Huth     env->DBAT[1][nr] = value;
245fcf5ef2aSThomas Huth }
246fcf5ef2aSThomas Huth 
247fcf5ef2aSThomas Huth /*****************************************************************************/
248fcf5ef2aSThomas Huth /* TLB management */
ppc_tlb_invalidate_all(CPUPPCState * env)249fcf5ef2aSThomas Huth void ppc_tlb_invalidate_all(CPUPPCState *env)
250fcf5ef2aSThomas Huth {
251ec975e83SSam Bobroff #if defined(TARGET_PPC64)
252d57d72a8SGreg Kurz     if (mmu_is_64bit(env->mmu_model)) {
253ec975e83SSam Bobroff         env->tlb_need_flush = 0;
254db70b311SRichard Henderson         tlb_flush(env_cpu(env));
255ec975e83SSam Bobroff     } else
256ec975e83SSam Bobroff #endif /* defined(TARGET_PPC64) */
257fcf5ef2aSThomas Huth     switch (env->mmu_model) {
258fcf5ef2aSThomas Huth     case POWERPC_MMU_SOFT_6xx:
259fcf5ef2aSThomas Huth         ppc6xx_tlb_invalidate_all(env);
260fcf5ef2aSThomas Huth         break;
261fcf5ef2aSThomas Huth     case POWERPC_MMU_SOFT_4xx:
262fcf5ef2aSThomas Huth         ppc4xx_tlb_invalidate_all(env);
263fcf5ef2aSThomas Huth         break;
264fcf5ef2aSThomas Huth     case POWERPC_MMU_REAL:
265db70b311SRichard Henderson         cpu_abort(env_cpu(env), "No TLB for PowerPC 4xx in real mode\n");
266fcf5ef2aSThomas Huth         break;
267fcf5ef2aSThomas Huth     case POWERPC_MMU_MPC8xx:
268fcf5ef2aSThomas Huth         /* XXX: TODO */
269db70b311SRichard Henderson         cpu_abort(env_cpu(env), "MPC8xx MMU model is not implemented\n");
270fcf5ef2aSThomas Huth         break;
271fcf5ef2aSThomas Huth     case POWERPC_MMU_BOOKE:
272db70b311SRichard Henderson         tlb_flush(env_cpu(env));
273fcf5ef2aSThomas Huth         break;
274fcf5ef2aSThomas Huth     case POWERPC_MMU_BOOKE206:
275fcf5ef2aSThomas Huth         booke206_flush_tlb(env, -1, 0);
276fcf5ef2aSThomas Huth         break;
277fcf5ef2aSThomas Huth     case POWERPC_MMU_32B:
278fcf5ef2aSThomas Huth         env->tlb_need_flush = 0;
279db70b311SRichard Henderson         tlb_flush(env_cpu(env));
280fcf5ef2aSThomas Huth         break;
281fcf5ef2aSThomas Huth     default:
282fcf5ef2aSThomas Huth         /* XXX: TODO */
283db70b311SRichard Henderson         cpu_abort(env_cpu(env), "Unknown MMU model %x\n", env->mmu_model);
284fcf5ef2aSThomas Huth         break;
285fcf5ef2aSThomas Huth     }
286fcf5ef2aSThomas Huth }
287fcf5ef2aSThomas Huth 
ppc_tlb_invalidate_one(CPUPPCState * env,target_ulong addr)288fcf5ef2aSThomas Huth void ppc_tlb_invalidate_one(CPUPPCState *env, target_ulong addr)
289fcf5ef2aSThomas Huth {
290fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS)
291fcf5ef2aSThomas Huth     addr &= TARGET_PAGE_MASK;
292ec975e83SSam Bobroff #if defined(TARGET_PPC64)
293d57d72a8SGreg Kurz     if (mmu_is_64bit(env->mmu_model)) {
294ec975e83SSam Bobroff         /* tlbie invalidate TLBs for all segments */
295fe4ade31SDavid Gibson         /*
296fe4ade31SDavid Gibson          * XXX: given the fact that there are too many segments to invalidate,
297ec975e83SSam Bobroff          *      and we still don't have a tlb_flush_mask(env, n, mask) in QEMU,
298ec975e83SSam Bobroff          *      we just invalidate all TLBs
299ec975e83SSam Bobroff          */
300ec975e83SSam Bobroff         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
301ec975e83SSam Bobroff     } else
302ec975e83SSam Bobroff #endif /* defined(TARGET_PPC64) */
303fcf5ef2aSThomas Huth     switch (env->mmu_model) {
304fcf5ef2aSThomas Huth     case POWERPC_MMU_SOFT_6xx:
305fcf5ef2aSThomas Huth         ppc6xx_tlb_invalidate_virt(env, addr, 0);
306fcf5ef2aSThomas Huth         ppc6xx_tlb_invalidate_virt(env, addr, 1);
307fcf5ef2aSThomas Huth         break;
308fcf5ef2aSThomas Huth     case POWERPC_MMU_32B:
309fe4ade31SDavid Gibson         /*
310fe4ade31SDavid Gibson          * Actual CPUs invalidate entire congruence classes based on
311fe4ade31SDavid Gibson          * the geometry of their TLBs and some OSes take that into
312fe4ade31SDavid Gibson          * account, we just mark the TLB to be flushed later (context
313fe4ade31SDavid Gibson          * synchronizing event or sync instruction on 32-bit).
314fcf5ef2aSThomas Huth          */
315fcf5ef2aSThomas Huth         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
316fcf5ef2aSThomas Huth         break;
317fcf5ef2aSThomas Huth     default:
318fcf5ef2aSThomas Huth         /* Should never reach here with other MMU models */
319*52d9ffd8SPierrick Bouvier         g_assert_not_reached();
320fcf5ef2aSThomas Huth     }
321fcf5ef2aSThomas Huth #else
322fcf5ef2aSThomas Huth     ppc_tlb_invalidate_all(env);
323fcf5ef2aSThomas Huth #endif
324fcf5ef2aSThomas Huth }
325fcf5ef2aSThomas Huth 
326fcf5ef2aSThomas Huth /*****************************************************************************/
327fcf5ef2aSThomas Huth /* Special registers manipulation */
3284a7518e0SCédric Le Goater 
329fcf5ef2aSThomas Huth /* Segment registers load and store */
helper_load_sr(CPUPPCState * env,target_ulong sr_num)330fcf5ef2aSThomas Huth target_ulong helper_load_sr(CPUPPCState *env, target_ulong sr_num)
331fcf5ef2aSThomas Huth {
332fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
333d57d72a8SGreg Kurz     if (mmu_is_64bit(env->mmu_model)) {
334fcf5ef2aSThomas Huth         /* XXX */
335fcf5ef2aSThomas Huth         return 0;
336fcf5ef2aSThomas Huth     }
337fcf5ef2aSThomas Huth #endif
338fcf5ef2aSThomas Huth     return env->sr[sr_num];
339fcf5ef2aSThomas Huth }
340fcf5ef2aSThomas Huth 
helper_store_sr(CPUPPCState * env,target_ulong srnum,target_ulong value)341fcf5ef2aSThomas Huth void helper_store_sr(CPUPPCState *env, target_ulong srnum, target_ulong value)
342fcf5ef2aSThomas Huth {
343fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
344fcf5ef2aSThomas Huth             "%s: reg=%d " TARGET_FMT_lx " " TARGET_FMT_lx "\n", __func__,
345fcf5ef2aSThomas Huth             (int)srnum, value, env->sr[srnum]);
346fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
347d57d72a8SGreg Kurz     if (mmu_is_64bit(env->mmu_model)) {
348db70b311SRichard Henderson         PowerPCCPU *cpu = env_archcpu(env);
349fcf5ef2aSThomas Huth         uint64_t esid, vsid;
350fcf5ef2aSThomas Huth 
351fcf5ef2aSThomas Huth         /* ESID = srnum */
352fcf5ef2aSThomas Huth         esid = ((uint64_t)(srnum & 0xf) << 28) | SLB_ESID_V;
353fcf5ef2aSThomas Huth 
354fcf5ef2aSThomas Huth         /* VSID = VSID */
355fcf5ef2aSThomas Huth         vsid = (value & 0xfffffff) << 12;
356fcf5ef2aSThomas Huth         /* flags = flags */
357fcf5ef2aSThomas Huth         vsid |= ((value >> 27) & 0xf) << 8;
358fcf5ef2aSThomas Huth 
359fcf5ef2aSThomas Huth         ppc_store_slb(cpu, srnum, esid, vsid);
360fcf5ef2aSThomas Huth     } else
361fcf5ef2aSThomas Huth #endif
362fcf5ef2aSThomas Huth     if (env->sr[srnum] != value) {
363fcf5ef2aSThomas Huth         env->sr[srnum] = value;
364fe4ade31SDavid Gibson         /*
365fe4ade31SDavid Gibson          * Invalidating 256MB of virtual memory in 4kB pages is way
366136fbf65Szhaolichang          * longer than flushing the whole TLB.
367fe4ade31SDavid Gibson          */
368fcf5ef2aSThomas Huth #if !defined(FLUSH_ALL_TLBS) && 0
369fcf5ef2aSThomas Huth         {
370fcf5ef2aSThomas Huth             target_ulong page, end;
371fcf5ef2aSThomas Huth             /* Invalidate 256 MB of virtual memory */
372fcf5ef2aSThomas Huth             page = (16 << 20) * srnum;
373fcf5ef2aSThomas Huth             end = page + (16 << 20);
374fcf5ef2aSThomas Huth             for (; page != end; page += TARGET_PAGE_SIZE) {
375db70b311SRichard Henderson                 tlb_flush_page(env_cpu(env), page);
376fcf5ef2aSThomas Huth             }
377fcf5ef2aSThomas Huth         }
378fcf5ef2aSThomas Huth #else
379fcf5ef2aSThomas Huth         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
380fcf5ef2aSThomas Huth #endif
381fcf5ef2aSThomas Huth     }
382fcf5ef2aSThomas Huth }
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth /* TLB management */
helper_tlbia(CPUPPCState * env)385fcf5ef2aSThomas Huth void helper_tlbia(CPUPPCState *env)
386fcf5ef2aSThomas Huth {
387fcf5ef2aSThomas Huth     ppc_tlb_invalidate_all(env);
388fcf5ef2aSThomas Huth }
389fcf5ef2aSThomas Huth 
helper_tlbie(CPUPPCState * env,target_ulong addr)390fcf5ef2aSThomas Huth void helper_tlbie(CPUPPCState *env, target_ulong addr)
391fcf5ef2aSThomas Huth {
392fcf5ef2aSThomas Huth     ppc_tlb_invalidate_one(env, addr);
393fcf5ef2aSThomas Huth }
394fcf5ef2aSThomas Huth 
395e7beaea5SLeandro Lupori #if defined(TARGET_PPC64)
396e7beaea5SLeandro Lupori 
397e7beaea5SLeandro Lupori /* Invalidation Selector */
398e7beaea5SLeandro Lupori #define TLBIE_IS_VA         0
399e7beaea5SLeandro Lupori #define TLBIE_IS_PID        1
400e7beaea5SLeandro Lupori #define TLBIE_IS_LPID       2
401e7beaea5SLeandro Lupori #define TLBIE_IS_ALL        3
402e7beaea5SLeandro Lupori 
403e7beaea5SLeandro Lupori /* Radix Invalidation Control */
404e7beaea5SLeandro Lupori #define TLBIE_RIC_TLB       0
405e7beaea5SLeandro Lupori #define TLBIE_RIC_PWC       1
406e7beaea5SLeandro Lupori #define TLBIE_RIC_ALL       2
407e7beaea5SLeandro Lupori #define TLBIE_RIC_GRP       3
408e7beaea5SLeandro Lupori 
409e7beaea5SLeandro Lupori /* Radix Actual Page sizes */
410e7beaea5SLeandro Lupori #define TLBIE_R_AP_4K       0
411e7beaea5SLeandro Lupori #define TLBIE_R_AP_64K      5
412e7beaea5SLeandro Lupori #define TLBIE_R_AP_2M       1
413e7beaea5SLeandro Lupori #define TLBIE_R_AP_1G       2
414e7beaea5SLeandro Lupori 
415e7beaea5SLeandro Lupori /* RB field masks */
416e7beaea5SLeandro Lupori #define TLBIE_RB_EPN_MASK   PPC_BITMASK(0, 51)
417e7beaea5SLeandro Lupori #define TLBIE_RB_IS_MASK    PPC_BITMASK(52, 53)
418e7beaea5SLeandro Lupori #define TLBIE_RB_AP_MASK    PPC_BITMASK(56, 58)
419e7beaea5SLeandro Lupori 
helper_tlbie_isa300(CPUPPCState * env,target_ulong rb,target_ulong rs,uint32_t flags)420e7beaea5SLeandro Lupori void helper_tlbie_isa300(CPUPPCState *env, target_ulong rb, target_ulong rs,
421e7beaea5SLeandro Lupori                          uint32_t flags)
422e7beaea5SLeandro Lupori {
423e7beaea5SLeandro Lupori     unsigned ric = (flags & TLBIE_F_RIC_MASK) >> TLBIE_F_RIC_SHIFT;
424e7beaea5SLeandro Lupori     /*
425e7beaea5SLeandro Lupori      * With the exception of the checks for invalid instruction forms,
426e7beaea5SLeandro Lupori      * PRS is currently ignored, because we don't know if a given TLB entry
427e7beaea5SLeandro Lupori      * is process or partition scoped.
428e7beaea5SLeandro Lupori      */
429e7beaea5SLeandro Lupori     bool prs = flags & TLBIE_F_PRS;
430e7beaea5SLeandro Lupori     bool r = flags & TLBIE_F_R;
431e7beaea5SLeandro Lupori     bool local = flags & TLBIE_F_LOCAL;
432e7beaea5SLeandro Lupori     bool effR;
433e7beaea5SLeandro Lupori     unsigned is = extract64(rb, PPC_BIT_NR(53), 2);
434e7beaea5SLeandro Lupori     unsigned ap;        /* actual page size */
435e7beaea5SLeandro Lupori     target_ulong addr, pgoffs_mask;
436e7beaea5SLeandro Lupori 
437e7beaea5SLeandro Lupori     qemu_log_mask(CPU_LOG_MMU,
438e7beaea5SLeandro Lupori         "%s: local=%d addr=" TARGET_FMT_lx " ric=%u prs=%d r=%d is=%u\n",
439e7beaea5SLeandro Lupori         __func__, local, rb & TARGET_PAGE_MASK, ric, prs, r, is);
440e7beaea5SLeandro Lupori 
441e7beaea5SLeandro Lupori     effR = FIELD_EX64(env->msr, MSR, HV) ? r : env->spr[SPR_LPCR] & LPCR_HR;
442e7beaea5SLeandro Lupori 
443e7beaea5SLeandro Lupori     /* Partial TLB invalidation is supported for Radix only for now. */
444e7beaea5SLeandro Lupori     if (!effR) {
445e7beaea5SLeandro Lupori         goto inval_all;
446e7beaea5SLeandro Lupori     }
447e7beaea5SLeandro Lupori 
448e7beaea5SLeandro Lupori     /* Check for invalid instruction forms (effR=1). */
449e7beaea5SLeandro Lupori     if (unlikely(ric == TLBIE_RIC_GRP ||
450e7beaea5SLeandro Lupori                  ((ric == TLBIE_RIC_PWC || ric == TLBIE_RIC_ALL) &&
451e7beaea5SLeandro Lupori                                            is == TLBIE_IS_VA) ||
452e7beaea5SLeandro Lupori                  (!prs && is == TLBIE_IS_PID))) {
453e7beaea5SLeandro Lupori         qemu_log_mask(LOG_GUEST_ERROR,
454e7beaea5SLeandro Lupori             "%s: invalid instruction form: ric=%u prs=%d r=%d is=%u\n",
455e7beaea5SLeandro Lupori             __func__, ric, prs, r, is);
456e7beaea5SLeandro Lupori         goto invalid;
457e7beaea5SLeandro Lupori     }
458e7beaea5SLeandro Lupori 
459e7beaea5SLeandro Lupori     /* We don't cache Page Walks. */
460e7beaea5SLeandro Lupori     if (ric == TLBIE_RIC_PWC) {
461e7beaea5SLeandro Lupori         if (local) {
462e7beaea5SLeandro Lupori             unsigned set = extract64(rb, PPC_BIT_NR(51), 12);
463e7beaea5SLeandro Lupori             if (set != 0) {
464e7beaea5SLeandro Lupori                 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid set: %d\n",
465e7beaea5SLeandro Lupori                               __func__, set);
466e7beaea5SLeandro Lupori                 goto invalid;
467e7beaea5SLeandro Lupori             }
468e7beaea5SLeandro Lupori         }
469e7beaea5SLeandro Lupori         return;
470e7beaea5SLeandro Lupori     }
471e7beaea5SLeandro Lupori 
472e7beaea5SLeandro Lupori     /*
473e7beaea5SLeandro Lupori      * Invalidation by LPID or PID is not supported, so fallback
474e7beaea5SLeandro Lupori      * to full TLB flush in these cases.
475e7beaea5SLeandro Lupori      */
476e7beaea5SLeandro Lupori     if (is != TLBIE_IS_VA) {
477e7beaea5SLeandro Lupori         goto inval_all;
478e7beaea5SLeandro Lupori     }
479e7beaea5SLeandro Lupori 
480e7beaea5SLeandro Lupori     /*
481e7beaea5SLeandro Lupori      * The results of an attempt to invalidate a translation outside of
482e7beaea5SLeandro Lupori      * quadrant 0 for Radix Tree translation (effR=1, RIC=0, PRS=1, IS=0,
483e7beaea5SLeandro Lupori      * and EA 0:1 != 0b00) are boundedly undefined.
484e7beaea5SLeandro Lupori      */
485e7beaea5SLeandro Lupori     if (unlikely(ric == TLBIE_RIC_TLB && prs && is == TLBIE_IS_VA &&
486e7beaea5SLeandro Lupori                  (rb & R_EADDR_QUADRANT) != R_EADDR_QUADRANT0)) {
487e7beaea5SLeandro Lupori         qemu_log_mask(LOG_GUEST_ERROR,
488e7beaea5SLeandro Lupori             "%s: attempt to invalidate a translation outside of quadrant 0\n",
489e7beaea5SLeandro Lupori             __func__);
490e7beaea5SLeandro Lupori         goto inval_all;
491e7beaea5SLeandro Lupori     }
492e7beaea5SLeandro Lupori 
493e7beaea5SLeandro Lupori     assert(is == TLBIE_IS_VA);
494e7beaea5SLeandro Lupori     assert(ric == TLBIE_RIC_TLB || ric == TLBIE_RIC_ALL);
495e7beaea5SLeandro Lupori 
496e7beaea5SLeandro Lupori     ap = extract64(rb, PPC_BIT_NR(58), 3);
497e7beaea5SLeandro Lupori     switch (ap) {
498e7beaea5SLeandro Lupori     case TLBIE_R_AP_4K:
499e7beaea5SLeandro Lupori         pgoffs_mask = 0xfffull;
500e7beaea5SLeandro Lupori         break;
501e7beaea5SLeandro Lupori 
502e7beaea5SLeandro Lupori     case TLBIE_R_AP_64K:
503e7beaea5SLeandro Lupori         pgoffs_mask = 0xffffull;
504e7beaea5SLeandro Lupori         break;
505e7beaea5SLeandro Lupori 
506e7beaea5SLeandro Lupori     case TLBIE_R_AP_2M:
507e7beaea5SLeandro Lupori         pgoffs_mask = 0x1fffffull;
508e7beaea5SLeandro Lupori         break;
509e7beaea5SLeandro Lupori 
510e7beaea5SLeandro Lupori     case TLBIE_R_AP_1G:
511e7beaea5SLeandro Lupori         pgoffs_mask = 0x3fffffffull;
512e7beaea5SLeandro Lupori         break;
513e7beaea5SLeandro Lupori 
514e7beaea5SLeandro Lupori     default:
515e7beaea5SLeandro Lupori         /*
516e7beaea5SLeandro Lupori          * If the value specified in RS 0:31, RS 32:63, RB 54:55, RB 56:58,
517e7beaea5SLeandro Lupori          * RB 44:51, or RB 56:63, when it is needed to perform the specified
518e7beaea5SLeandro Lupori          * operation, is not supported by the implementation, the instruction
519e7beaea5SLeandro Lupori          * is treated as if the instruction form were invalid.
520e7beaea5SLeandro Lupori          */
521e7beaea5SLeandro Lupori         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid AP: %d\n", __func__, ap);
522e7beaea5SLeandro Lupori         goto invalid;
523e7beaea5SLeandro Lupori     }
524e7beaea5SLeandro Lupori 
525e7beaea5SLeandro Lupori     addr = rb & TLBIE_RB_EPN_MASK & ~pgoffs_mask;
526e7beaea5SLeandro Lupori 
527e7beaea5SLeandro Lupori     if (local) {
528e7beaea5SLeandro Lupori         tlb_flush_page(env_cpu(env), addr);
529e7beaea5SLeandro Lupori     } else {
53082676f1fSNicholas Piggin         tlb_flush_page_all_cpus_synced(env_cpu(env), addr);
531e7beaea5SLeandro Lupori     }
532e7beaea5SLeandro Lupori     return;
533e7beaea5SLeandro Lupori 
534e7beaea5SLeandro Lupori inval_all:
535e7beaea5SLeandro Lupori     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
536e7beaea5SLeandro Lupori     if (!local) {
537e7beaea5SLeandro Lupori         env->tlb_need_flush |= TLB_NEED_GLOBAL_FLUSH;
538e7beaea5SLeandro Lupori     }
539e7beaea5SLeandro Lupori     return;
540e7beaea5SLeandro Lupori 
541e7beaea5SLeandro Lupori invalid:
542e7beaea5SLeandro Lupori     raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
543e7beaea5SLeandro Lupori                            POWERPC_EXCP_INVAL |
544e7beaea5SLeandro Lupori                            POWERPC_EXCP_INVAL_INVAL, GETPC());
545e7beaea5SLeandro Lupori }
546e7beaea5SLeandro Lupori 
547e7beaea5SLeandro Lupori #endif
548e7beaea5SLeandro Lupori 
helper_tlbiva(CPUPPCState * env,target_ulong addr)549fcf5ef2aSThomas Huth void helper_tlbiva(CPUPPCState *env, target_ulong addr)
550fcf5ef2aSThomas Huth {
551fcf5ef2aSThomas Huth     /* tlbiva instruction only exists on BookE */
552fcf5ef2aSThomas Huth     assert(env->mmu_model == POWERPC_MMU_BOOKE);
553fcf5ef2aSThomas Huth     /* XXX: TODO */
554db70b311SRichard Henderson     cpu_abort(env_cpu(env), "BookE MMU model is not implemented\n");
555fcf5ef2aSThomas Huth }
556fcf5ef2aSThomas Huth 
557fcf5ef2aSThomas Huth /* Software driven TLBs management */
558fcf5ef2aSThomas Huth /* PowerPC 602/603 software TLB load instructions helpers */
do_6xx_tlb(CPUPPCState * env,target_ulong new_EPN,int is_code)559fcf5ef2aSThomas Huth static void do_6xx_tlb(CPUPPCState *env, target_ulong new_EPN, int is_code)
560fcf5ef2aSThomas Huth {
561fcf5ef2aSThomas Huth     target_ulong RPN, CMP, EPN;
562fcf5ef2aSThomas Huth     int way;
563fcf5ef2aSThomas Huth 
564fcf5ef2aSThomas Huth     RPN = env->spr[SPR_RPA];
565fcf5ef2aSThomas Huth     if (is_code) {
566fcf5ef2aSThomas Huth         CMP = env->spr[SPR_ICMP];
567fcf5ef2aSThomas Huth         EPN = env->spr[SPR_IMISS];
568fcf5ef2aSThomas Huth     } else {
569fcf5ef2aSThomas Huth         CMP = env->spr[SPR_DCMP];
570fcf5ef2aSThomas Huth         EPN = env->spr[SPR_DMISS];
571fcf5ef2aSThomas Huth     }
572fcf5ef2aSThomas Huth     way = (env->spr[SPR_SRR1] >> 17) & 1;
573fcf5ef2aSThomas Huth     (void)EPN; /* avoid a compiler warning */
57456964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "%s: EPN " TARGET_FMT_lx " " TARGET_FMT_lx
57556964585SCédric Le Goater                   " PTE0 " TARGET_FMT_lx " PTE1 " TARGET_FMT_lx " way %d\n",
57656964585SCédric Le Goater                   __func__, new_EPN, EPN, CMP, RPN, way);
577fcf5ef2aSThomas Huth     /* Store this TLB */
578fcf5ef2aSThomas Huth     ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
579fcf5ef2aSThomas Huth                      way, is_code, CMP, RPN);
580fcf5ef2aSThomas Huth }
581fcf5ef2aSThomas Huth 
helper_6xx_tlbd(CPUPPCState * env,target_ulong EPN)582fcf5ef2aSThomas Huth void helper_6xx_tlbd(CPUPPCState *env, target_ulong EPN)
583fcf5ef2aSThomas Huth {
584fcf5ef2aSThomas Huth     do_6xx_tlb(env, EPN, 0);
585fcf5ef2aSThomas Huth }
586fcf5ef2aSThomas Huth 
helper_6xx_tlbi(CPUPPCState * env,target_ulong EPN)587fcf5ef2aSThomas Huth void helper_6xx_tlbi(CPUPPCState *env, target_ulong EPN)
588fcf5ef2aSThomas Huth {
589fcf5ef2aSThomas Huth     do_6xx_tlb(env, EPN, 1);
590fcf5ef2aSThomas Huth }
591fcf5ef2aSThomas Huth 
booke_tlb_to_page_size(int size)592fcf5ef2aSThomas Huth static inline target_ulong booke_tlb_to_page_size(int size)
593fcf5ef2aSThomas Huth {
594fcf5ef2aSThomas Huth     return 1024 << (2 * size);
595fcf5ef2aSThomas Huth }
596fcf5ef2aSThomas Huth 
booke_page_size_to_tlb(target_ulong page_size)597fcf5ef2aSThomas Huth static inline int booke_page_size_to_tlb(target_ulong page_size)
598fcf5ef2aSThomas Huth {
599fcf5ef2aSThomas Huth     int size;
600fcf5ef2aSThomas Huth 
601fcf5ef2aSThomas Huth     switch (page_size) {
602fcf5ef2aSThomas Huth     case 0x00000400UL:
603fcf5ef2aSThomas Huth         size = 0x0;
604fcf5ef2aSThomas Huth         break;
605fcf5ef2aSThomas Huth     case 0x00001000UL:
606fcf5ef2aSThomas Huth         size = 0x1;
607fcf5ef2aSThomas Huth         break;
608fcf5ef2aSThomas Huth     case 0x00004000UL:
609fcf5ef2aSThomas Huth         size = 0x2;
610fcf5ef2aSThomas Huth         break;
611fcf5ef2aSThomas Huth     case 0x00010000UL:
612fcf5ef2aSThomas Huth         size = 0x3;
613fcf5ef2aSThomas Huth         break;
614fcf5ef2aSThomas Huth     case 0x00040000UL:
615fcf5ef2aSThomas Huth         size = 0x4;
616fcf5ef2aSThomas Huth         break;
617fcf5ef2aSThomas Huth     case 0x00100000UL:
618fcf5ef2aSThomas Huth         size = 0x5;
619fcf5ef2aSThomas Huth         break;
620fcf5ef2aSThomas Huth     case 0x00400000UL:
621fcf5ef2aSThomas Huth         size = 0x6;
622fcf5ef2aSThomas Huth         break;
623fcf5ef2aSThomas Huth     case 0x01000000UL:
624fcf5ef2aSThomas Huth         size = 0x7;
625fcf5ef2aSThomas Huth         break;
626fcf5ef2aSThomas Huth     case 0x04000000UL:
627fcf5ef2aSThomas Huth         size = 0x8;
628fcf5ef2aSThomas Huth         break;
629fcf5ef2aSThomas Huth     case 0x10000000UL:
630fcf5ef2aSThomas Huth         size = 0x9;
631fcf5ef2aSThomas Huth         break;
632fcf5ef2aSThomas Huth     case 0x40000000UL:
633fcf5ef2aSThomas Huth         size = 0xA;
634fcf5ef2aSThomas Huth         break;
635fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
636fcf5ef2aSThomas Huth     case 0x000100000000ULL:
637fcf5ef2aSThomas Huth         size = 0xB;
638fcf5ef2aSThomas Huth         break;
639fcf5ef2aSThomas Huth     case 0x000400000000ULL:
640fcf5ef2aSThomas Huth         size = 0xC;
641fcf5ef2aSThomas Huth         break;
642fcf5ef2aSThomas Huth     case 0x001000000000ULL:
643fcf5ef2aSThomas Huth         size = 0xD;
644fcf5ef2aSThomas Huth         break;
645fcf5ef2aSThomas Huth     case 0x004000000000ULL:
646fcf5ef2aSThomas Huth         size = 0xE;
647fcf5ef2aSThomas Huth         break;
648fcf5ef2aSThomas Huth     case 0x010000000000ULL:
649fcf5ef2aSThomas Huth         size = 0xF;
650fcf5ef2aSThomas Huth         break;
651fcf5ef2aSThomas Huth #endif
652fcf5ef2aSThomas Huth     default:
653fcf5ef2aSThomas Huth         size = -1;
654fcf5ef2aSThomas Huth         break;
655fcf5ef2aSThomas Huth     }
656fcf5ef2aSThomas Huth 
657fcf5ef2aSThomas Huth     return size;
658fcf5ef2aSThomas Huth }
659fcf5ef2aSThomas Huth 
660fcf5ef2aSThomas Huth /* Helpers for 4xx TLB management */
661fcf5ef2aSThomas Huth #define PPC4XX_TLB_ENTRY_MASK       0x0000003f  /* Mask for 64 TLB entries */
662fcf5ef2aSThomas Huth 
663fcf5ef2aSThomas Huth #define PPC4XX_TLBHI_V              0x00000040
664fcf5ef2aSThomas Huth #define PPC4XX_TLBHI_E              0x00000020
665fcf5ef2aSThomas Huth #define PPC4XX_TLBHI_SIZE_MIN       0
666fcf5ef2aSThomas Huth #define PPC4XX_TLBHI_SIZE_MAX       7
667fcf5ef2aSThomas Huth #define PPC4XX_TLBHI_SIZE_DEFAULT   1
668fcf5ef2aSThomas Huth #define PPC4XX_TLBHI_SIZE_SHIFT     7
669fcf5ef2aSThomas Huth #define PPC4XX_TLBHI_SIZE_MASK      0x00000007
670fcf5ef2aSThomas Huth 
671fcf5ef2aSThomas Huth #define PPC4XX_TLBLO_EX             0x00000200
672fcf5ef2aSThomas Huth #define PPC4XX_TLBLO_WR             0x00000100
673fcf5ef2aSThomas Huth #define PPC4XX_TLBLO_ATTR_MASK      0x000000FF
674fcf5ef2aSThomas Huth #define PPC4XX_TLBLO_RPN_MASK       0xFFFFFC00
675fcf5ef2aSThomas Huth 
helper_store_40x_pid(CPUPPCState * env,target_ulong val)67647822486SCédric Le Goater void helper_store_40x_pid(CPUPPCState *env, target_ulong val)
67747822486SCédric Le Goater {
67847822486SCédric Le Goater     if (env->spr[SPR_40x_PID] != val) {
67947822486SCédric Le Goater         env->spr[SPR_40x_PID] = val;
68047822486SCédric Le Goater         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
68147822486SCédric Le Goater     }
68247822486SCédric Le Goater }
68347822486SCédric Le Goater 
helper_4xx_tlbre_hi(CPUPPCState * env,target_ulong entry)684fcf5ef2aSThomas Huth target_ulong helper_4xx_tlbre_hi(CPUPPCState *env, target_ulong entry)
685fcf5ef2aSThomas Huth {
686fcf5ef2aSThomas Huth     ppcemb_tlb_t *tlb;
687fcf5ef2aSThomas Huth     target_ulong ret;
688fcf5ef2aSThomas Huth     int size;
689fcf5ef2aSThomas Huth 
690fcf5ef2aSThomas Huth     entry &= PPC4XX_TLB_ENTRY_MASK;
691fcf5ef2aSThomas Huth     tlb = &env->tlb.tlbe[entry];
692fcf5ef2aSThomas Huth     ret = tlb->EPN;
693fcf5ef2aSThomas Huth     if (tlb->prot & PAGE_VALID) {
694fcf5ef2aSThomas Huth         ret |= PPC4XX_TLBHI_V;
695fcf5ef2aSThomas Huth     }
696fcf5ef2aSThomas Huth     size = booke_page_size_to_tlb(tlb->size);
697fcf5ef2aSThomas Huth     if (size < PPC4XX_TLBHI_SIZE_MIN || size > PPC4XX_TLBHI_SIZE_MAX) {
698fcf5ef2aSThomas Huth         size = PPC4XX_TLBHI_SIZE_DEFAULT;
699fcf5ef2aSThomas Huth     }
700fcf5ef2aSThomas Huth     ret |= size << PPC4XX_TLBHI_SIZE_SHIFT;
70147822486SCédric Le Goater     helper_store_40x_pid(env, tlb->PID);
702fcf5ef2aSThomas Huth     return ret;
703fcf5ef2aSThomas Huth }
704fcf5ef2aSThomas Huth 
helper_4xx_tlbre_lo(CPUPPCState * env,target_ulong entry)705fcf5ef2aSThomas Huth target_ulong helper_4xx_tlbre_lo(CPUPPCState *env, target_ulong entry)
706fcf5ef2aSThomas Huth {
707fcf5ef2aSThomas Huth     ppcemb_tlb_t *tlb;
708fcf5ef2aSThomas Huth     target_ulong ret;
709fcf5ef2aSThomas Huth 
710fcf5ef2aSThomas Huth     entry &= PPC4XX_TLB_ENTRY_MASK;
711fcf5ef2aSThomas Huth     tlb = &env->tlb.tlbe[entry];
712fcf5ef2aSThomas Huth     ret = tlb->RPN;
713fcf5ef2aSThomas Huth     if (tlb->prot & PAGE_EXEC) {
714fcf5ef2aSThomas Huth         ret |= PPC4XX_TLBLO_EX;
715fcf5ef2aSThomas Huth     }
716fcf5ef2aSThomas Huth     if (tlb->prot & PAGE_WRITE) {
717fcf5ef2aSThomas Huth         ret |= PPC4XX_TLBLO_WR;
718fcf5ef2aSThomas Huth     }
719fcf5ef2aSThomas Huth     return ret;
720fcf5ef2aSThomas Huth }
721fcf5ef2aSThomas Huth 
ppcemb_tlb_flush(CPUState * cs,ppcemb_tlb_t * tlb)722c191ad77SNicholas Piggin static void ppcemb_tlb_flush(CPUState *cs, ppcemb_tlb_t *tlb)
723c191ad77SNicholas Piggin {
7244acc505dSNicholas Piggin     unsigned mmu_idx = 0;
725c191ad77SNicholas Piggin 
7264acc505dSNicholas Piggin     if (tlb->prot & 0xf) {
7274acc505dSNicholas Piggin         mmu_idx |= 0x1;
728c191ad77SNicholas Piggin     }
7294acc505dSNicholas Piggin     if ((tlb->prot >> 4) & 0xf) {
7304acc505dSNicholas Piggin         mmu_idx |= 0x2;
7314acc505dSNicholas Piggin     }
7324acc505dSNicholas Piggin     if (tlb->attr & 1) {
7334acc505dSNicholas Piggin         mmu_idx <<= 2;
7344acc505dSNicholas Piggin     }
7354acc505dSNicholas Piggin 
7364acc505dSNicholas Piggin     tlb_flush_range_by_mmuidx(cs, tlb->EPN, tlb->size, mmu_idx,
7374acc505dSNicholas Piggin                               TARGET_LONG_BITS);
738c191ad77SNicholas Piggin }
739c191ad77SNicholas Piggin 
helper_4xx_tlbwe_hi(CPUPPCState * env,target_ulong entry,target_ulong val)740fcf5ef2aSThomas Huth void helper_4xx_tlbwe_hi(CPUPPCState *env, target_ulong entry,
741fcf5ef2aSThomas Huth                          target_ulong val)
742fcf5ef2aSThomas Huth {
743db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
744fcf5ef2aSThomas Huth     ppcemb_tlb_t *tlb;
745fcf5ef2aSThomas Huth 
74656964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "%s entry %d val " TARGET_FMT_lx "\n",
74756964585SCédric Le Goater                   __func__, (int)entry,
748fcf5ef2aSThomas Huth               val);
749fcf5ef2aSThomas Huth     entry &= PPC4XX_TLB_ENTRY_MASK;
750fcf5ef2aSThomas Huth     tlb = &env->tlb.tlbe[entry];
751fcf5ef2aSThomas Huth     /* Invalidate previous TLB (if it's valid) */
7524acc505dSNicholas Piggin     if ((tlb->prot & PAGE_VALID) && tlb->PID == env->spr[SPR_40x_PID]) {
75356964585SCédric Le Goater         qemu_log_mask(CPU_LOG_MMU, "%s: invalidate old TLB %d start "
75456964585SCédric Le Goater                       TARGET_FMT_lx " end " TARGET_FMT_lx "\n", __func__,
755c191ad77SNicholas Piggin                       (int)entry, tlb->EPN, tlb->EPN + tlb->size);
756c191ad77SNicholas Piggin         ppcemb_tlb_flush(cs, tlb);
757fcf5ef2aSThomas Huth     }
758fcf5ef2aSThomas Huth     tlb->size = booke_tlb_to_page_size((val >> PPC4XX_TLBHI_SIZE_SHIFT)
759fcf5ef2aSThomas Huth                                        & PPC4XX_TLBHI_SIZE_MASK);
760fe4ade31SDavid Gibson     /*
761fe4ade31SDavid Gibson      * We cannot handle TLB size < TARGET_PAGE_SIZE.
762a69dc537SThomas Huth      * If this ever occurs, we should implement TARGET_PAGE_BITS_VARY
763fcf5ef2aSThomas Huth      */
764fcf5ef2aSThomas Huth     if ((val & PPC4XX_TLBHI_V) && tlb->size < TARGET_PAGE_SIZE) {
765fcf5ef2aSThomas Huth         cpu_abort(cs, "TLB size " TARGET_FMT_lu " < %u "
766a69dc537SThomas Huth                   "are not supported (%d)\n"
767a69dc537SThomas Huth                   "Please implement TARGET_PAGE_BITS_VARY\n",
768fcf5ef2aSThomas Huth                   tlb->size, TARGET_PAGE_SIZE, (int)((val >> 7) & 0x7));
769fcf5ef2aSThomas Huth     }
770fcf5ef2aSThomas Huth     tlb->EPN = val & ~(tlb->size - 1);
771fcf5ef2aSThomas Huth     if (val & PPC4XX_TLBHI_V) {
772fcf5ef2aSThomas Huth         tlb->prot |= PAGE_VALID;
773fcf5ef2aSThomas Huth         if (val & PPC4XX_TLBHI_E) {
774fcf5ef2aSThomas Huth             /* XXX: TO BE FIXED */
775fcf5ef2aSThomas Huth             cpu_abort(cs,
776fcf5ef2aSThomas Huth                       "Little-endian TLB entries are not supported by now\n");
777fcf5ef2aSThomas Huth         }
778fcf5ef2aSThomas Huth     } else {
779fcf5ef2aSThomas Huth         tlb->prot &= ~PAGE_VALID;
780fcf5ef2aSThomas Huth     }
781fcf5ef2aSThomas Huth     tlb->PID = env->spr[SPR_40x_PID]; /* PID */
782883f2c59SPhilippe Mathieu-Daudé     qemu_log_mask(CPU_LOG_MMU, "%s: set up TLB %d RPN " HWADDR_FMT_plx
78356964585SCédric Le Goater                   " EPN " TARGET_FMT_lx " size " TARGET_FMT_lx
78456964585SCédric Le Goater                   " prot %c%c%c%c PID %d\n", __func__,
785fcf5ef2aSThomas Huth                   (int)entry, tlb->RPN, tlb->EPN, tlb->size,
786fcf5ef2aSThomas Huth                   tlb->prot & PAGE_READ ? 'r' : '-',
787fcf5ef2aSThomas Huth                   tlb->prot & PAGE_WRITE ? 'w' : '-',
788fcf5ef2aSThomas Huth                   tlb->prot & PAGE_EXEC ? 'x' : '-',
789fcf5ef2aSThomas Huth                   tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
790fcf5ef2aSThomas Huth }
791fcf5ef2aSThomas Huth 
helper_4xx_tlbwe_lo(CPUPPCState * env,target_ulong entry,target_ulong val)792fcf5ef2aSThomas Huth void helper_4xx_tlbwe_lo(CPUPPCState *env, target_ulong entry,
793fcf5ef2aSThomas Huth                          target_ulong val)
794fcf5ef2aSThomas Huth {
7952ab03484SNicholas Piggin     CPUState *cs = env_cpu(env);
796fcf5ef2aSThomas Huth     ppcemb_tlb_t *tlb;
797fcf5ef2aSThomas Huth 
79856964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "%s entry %i val " TARGET_FMT_lx "\n",
79956964585SCédric Le Goater                   __func__, (int)entry, val);
800fcf5ef2aSThomas Huth     entry &= PPC4XX_TLB_ENTRY_MASK;
801fcf5ef2aSThomas Huth     tlb = &env->tlb.tlbe[entry];
8022ab03484SNicholas Piggin     /* Invalidate previous TLB (if it's valid) */
8034acc505dSNicholas Piggin     if ((tlb->prot & PAGE_VALID) && tlb->PID == env->spr[SPR_40x_PID]) {
8042ab03484SNicholas Piggin         qemu_log_mask(CPU_LOG_MMU, "%s: invalidate old TLB %d start "
8052ab03484SNicholas Piggin                       TARGET_FMT_lx " end " TARGET_FMT_lx "\n", __func__,
8062ab03484SNicholas Piggin                       (int)entry, tlb->EPN, tlb->EPN + tlb->size);
8072ab03484SNicholas Piggin         ppcemb_tlb_flush(cs, tlb);
8082ab03484SNicholas Piggin     }
809fcf5ef2aSThomas Huth     tlb->attr = val & PPC4XX_TLBLO_ATTR_MASK;
810fcf5ef2aSThomas Huth     tlb->RPN = val & PPC4XX_TLBLO_RPN_MASK;
811fcf5ef2aSThomas Huth     tlb->prot = PAGE_READ;
812fcf5ef2aSThomas Huth     if (val & PPC4XX_TLBLO_EX) {
813fcf5ef2aSThomas Huth         tlb->prot |= PAGE_EXEC;
814fcf5ef2aSThomas Huth     }
815fcf5ef2aSThomas Huth     if (val & PPC4XX_TLBLO_WR) {
816fcf5ef2aSThomas Huth         tlb->prot |= PAGE_WRITE;
817fcf5ef2aSThomas Huth     }
818883f2c59SPhilippe Mathieu-Daudé     qemu_log_mask(CPU_LOG_MMU, "%s: set up TLB %d RPN " HWADDR_FMT_plx
81956964585SCédric Le Goater                   " EPN " TARGET_FMT_lx
820fcf5ef2aSThomas Huth                   " size " TARGET_FMT_lx " prot %c%c%c%c PID %d\n", __func__,
821fcf5ef2aSThomas Huth                   (int)entry, tlb->RPN, tlb->EPN, tlb->size,
822fcf5ef2aSThomas Huth                   tlb->prot & PAGE_READ ? 'r' : '-',
823fcf5ef2aSThomas Huth                   tlb->prot & PAGE_WRITE ? 'w' : '-',
824fcf5ef2aSThomas Huth                   tlb->prot & PAGE_EXEC ? 'x' : '-',
825fcf5ef2aSThomas Huth                   tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
826fcf5ef2aSThomas Huth }
827fcf5ef2aSThomas Huth 
helper_4xx_tlbsx(CPUPPCState * env,target_ulong address)828fcf5ef2aSThomas Huth target_ulong helper_4xx_tlbsx(CPUPPCState *env, target_ulong address)
829fcf5ef2aSThomas Huth {
830fcf5ef2aSThomas Huth     return ppcemb_tlb_search(env, address, env->spr[SPR_40x_PID]);
831fcf5ef2aSThomas Huth }
832fcf5ef2aSThomas Huth 
mmubooke_pid_match(CPUPPCState * env,ppcemb_tlb_t * tlb)8334acc505dSNicholas Piggin static bool mmubooke_pid_match(CPUPPCState *env, ppcemb_tlb_t *tlb)
8344acc505dSNicholas Piggin {
8354acc505dSNicholas Piggin     if (tlb->PID == env->spr[SPR_BOOKE_PID]) {
8364acc505dSNicholas Piggin         return true;
8374acc505dSNicholas Piggin     }
8384acc505dSNicholas Piggin     if (!env->nb_pids) {
8394acc505dSNicholas Piggin         return false;
8404acc505dSNicholas Piggin     }
8414acc505dSNicholas Piggin 
8424acc505dSNicholas Piggin     if (env->spr[SPR_BOOKE_PID1] && tlb->PID == env->spr[SPR_BOOKE_PID1]) {
8434acc505dSNicholas Piggin         return true;
8444acc505dSNicholas Piggin     }
8454acc505dSNicholas Piggin     if (env->spr[SPR_BOOKE_PID2] && tlb->PID == env->spr[SPR_BOOKE_PID2]) {
8464acc505dSNicholas Piggin         return true;
8474acc505dSNicholas Piggin     }
8484acc505dSNicholas Piggin 
8494acc505dSNicholas Piggin     return false;
8504acc505dSNicholas Piggin }
8514acc505dSNicholas Piggin 
852fcf5ef2aSThomas Huth /* PowerPC 440 TLB management */
helper_440_tlbwe(CPUPPCState * env,uint32_t word,target_ulong entry,target_ulong value)853fcf5ef2aSThomas Huth void helper_440_tlbwe(CPUPPCState *env, uint32_t word, target_ulong entry,
854fcf5ef2aSThomas Huth                       target_ulong value)
855fcf5ef2aSThomas Huth {
856fcf5ef2aSThomas Huth     ppcemb_tlb_t *tlb;
857fcf5ef2aSThomas Huth 
85856964585SCédric Le Goater     qemu_log_mask(CPU_LOG_MMU, "%s word %d entry %d value " TARGET_FMT_lx "\n",
859fcf5ef2aSThomas Huth                   __func__, word, (int)entry, value);
860fcf5ef2aSThomas Huth     entry &= 0x3F;
861fcf5ef2aSThomas Huth     tlb = &env->tlb.tlbe[entry];
862e8fe1411SNicholas Piggin 
863e8fe1411SNicholas Piggin     /* Invalidate previous TLB (if it's valid) */
8644acc505dSNicholas Piggin     if ((tlb->prot & PAGE_VALID) && mmubooke_pid_match(env, tlb)) {
8654acc505dSNicholas Piggin         qemu_log_mask(CPU_LOG_MMU, "%s: invalidate old TLB %d start "
8664acc505dSNicholas Piggin                       TARGET_FMT_lx " end " TARGET_FMT_lx "\n", __func__,
8674acc505dSNicholas Piggin                       (int)entry, tlb->EPN, tlb->EPN + tlb->size);
8681b72973dSNicholas Piggin         ppcemb_tlb_flush(env_cpu(env), tlb);
869e8fe1411SNicholas Piggin     }
870e8fe1411SNicholas Piggin 
871fcf5ef2aSThomas Huth     switch (word) {
872fcf5ef2aSThomas Huth     default:
873fcf5ef2aSThomas Huth         /* Just here to please gcc */
874fcf5ef2aSThomas Huth     case 0:
875e8fe1411SNicholas Piggin         tlb->EPN = value & 0xFFFFFC00;
876e8fe1411SNicholas Piggin         tlb->size = booke_tlb_to_page_size((value >> 4) & 0xF);
877fcf5ef2aSThomas Huth         tlb->attr &= ~0x1;
878fcf5ef2aSThomas Huth         tlb->attr |= (value >> 8) & 1;
879fcf5ef2aSThomas Huth         if (value & 0x200) {
880fcf5ef2aSThomas Huth             tlb->prot |= PAGE_VALID;
881fcf5ef2aSThomas Huth         } else {
882fcf5ef2aSThomas Huth             tlb->prot &= ~PAGE_VALID;
883fcf5ef2aSThomas Huth         }
884fcf5ef2aSThomas Huth         tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
885fcf5ef2aSThomas Huth         break;
886fcf5ef2aSThomas Huth     case 1:
887e8fe1411SNicholas Piggin         tlb->RPN = value & 0xFFFFFC0F;
888fcf5ef2aSThomas Huth         break;
889fcf5ef2aSThomas Huth     case 2:
890fcf5ef2aSThomas Huth         tlb->attr = (tlb->attr & 0x1) | (value & 0x0000FF00);
891fcf5ef2aSThomas Huth         tlb->prot = tlb->prot & PAGE_VALID;
892fcf5ef2aSThomas Huth         if (value & 0x1) {
893fcf5ef2aSThomas Huth             tlb->prot |= PAGE_READ << 4;
894fcf5ef2aSThomas Huth         }
895fcf5ef2aSThomas Huth         if (value & 0x2) {
896fcf5ef2aSThomas Huth             tlb->prot |= PAGE_WRITE << 4;
897fcf5ef2aSThomas Huth         }
898fcf5ef2aSThomas Huth         if (value & 0x4) {
899fcf5ef2aSThomas Huth             tlb->prot |= PAGE_EXEC << 4;
900fcf5ef2aSThomas Huth         }
901fcf5ef2aSThomas Huth         if (value & 0x8) {
902fcf5ef2aSThomas Huth             tlb->prot |= PAGE_READ;
903fcf5ef2aSThomas Huth         }
904fcf5ef2aSThomas Huth         if (value & 0x10) {
905fcf5ef2aSThomas Huth             tlb->prot |= PAGE_WRITE;
906fcf5ef2aSThomas Huth         }
907fcf5ef2aSThomas Huth         if (value & 0x20) {
908fcf5ef2aSThomas Huth             tlb->prot |= PAGE_EXEC;
909fcf5ef2aSThomas Huth         }
910fcf5ef2aSThomas Huth         break;
911fcf5ef2aSThomas Huth     }
912fcf5ef2aSThomas Huth }
913fcf5ef2aSThomas Huth 
helper_440_tlbre(CPUPPCState * env,uint32_t word,target_ulong entry)914fcf5ef2aSThomas Huth target_ulong helper_440_tlbre(CPUPPCState *env, uint32_t word,
915fcf5ef2aSThomas Huth                               target_ulong entry)
916fcf5ef2aSThomas Huth {
917fcf5ef2aSThomas Huth     ppcemb_tlb_t *tlb;
918fcf5ef2aSThomas Huth     target_ulong ret;
919fcf5ef2aSThomas Huth     int size;
920fcf5ef2aSThomas Huth 
921fcf5ef2aSThomas Huth     entry &= 0x3F;
922fcf5ef2aSThomas Huth     tlb = &env->tlb.tlbe[entry];
923fcf5ef2aSThomas Huth     switch (word) {
924fcf5ef2aSThomas Huth     default:
925fcf5ef2aSThomas Huth         /* Just here to please gcc */
926fcf5ef2aSThomas Huth     case 0:
927fcf5ef2aSThomas Huth         ret = tlb->EPN;
928fcf5ef2aSThomas Huth         size = booke_page_size_to_tlb(tlb->size);
929fcf5ef2aSThomas Huth         if (size < 0 || size > 0xF) {
930fcf5ef2aSThomas Huth             size = 1;
931fcf5ef2aSThomas Huth         }
932fcf5ef2aSThomas Huth         ret |= size << 4;
933fcf5ef2aSThomas Huth         if (tlb->attr & 0x1) {
934fcf5ef2aSThomas Huth             ret |= 0x100;
935fcf5ef2aSThomas Huth         }
936fcf5ef2aSThomas Huth         if (tlb->prot & PAGE_VALID) {
937fcf5ef2aSThomas Huth             ret |= 0x200;
938fcf5ef2aSThomas Huth         }
939fcf5ef2aSThomas Huth         env->spr[SPR_440_MMUCR] &= ~0x000000FF;
940fcf5ef2aSThomas Huth         env->spr[SPR_440_MMUCR] |= tlb->PID;
941fcf5ef2aSThomas Huth         break;
942fcf5ef2aSThomas Huth     case 1:
943fcf5ef2aSThomas Huth         ret = tlb->RPN;
944fcf5ef2aSThomas Huth         break;
945fcf5ef2aSThomas Huth     case 2:
946fcf5ef2aSThomas Huth         ret = tlb->attr & ~0x1;
947fcf5ef2aSThomas Huth         if (tlb->prot & (PAGE_READ << 4)) {
948fcf5ef2aSThomas Huth             ret |= 0x1;
949fcf5ef2aSThomas Huth         }
950fcf5ef2aSThomas Huth         if (tlb->prot & (PAGE_WRITE << 4)) {
951fcf5ef2aSThomas Huth             ret |= 0x2;
952fcf5ef2aSThomas Huth         }
953fcf5ef2aSThomas Huth         if (tlb->prot & (PAGE_EXEC << 4)) {
954fcf5ef2aSThomas Huth             ret |= 0x4;
955fcf5ef2aSThomas Huth         }
956fcf5ef2aSThomas Huth         if (tlb->prot & PAGE_READ) {
957fcf5ef2aSThomas Huth             ret |= 0x8;
958fcf5ef2aSThomas Huth         }
959fcf5ef2aSThomas Huth         if (tlb->prot & PAGE_WRITE) {
960fcf5ef2aSThomas Huth             ret |= 0x10;
961fcf5ef2aSThomas Huth         }
962fcf5ef2aSThomas Huth         if (tlb->prot & PAGE_EXEC) {
963fcf5ef2aSThomas Huth             ret |= 0x20;
964fcf5ef2aSThomas Huth         }
965fcf5ef2aSThomas Huth         break;
966fcf5ef2aSThomas Huth     }
967fcf5ef2aSThomas Huth     return ret;
968fcf5ef2aSThomas Huth }
969fcf5ef2aSThomas Huth 
helper_440_tlbsx(CPUPPCState * env,target_ulong address)970fcf5ef2aSThomas Huth target_ulong helper_440_tlbsx(CPUPPCState *env, target_ulong address)
971fcf5ef2aSThomas Huth {
972fcf5ef2aSThomas Huth     return ppcemb_tlb_search(env, address, env->spr[SPR_440_MMUCR] & 0xFF);
973fcf5ef2aSThomas Huth }
974fcf5ef2aSThomas Huth 
975fcf5ef2aSThomas Huth /* PowerPC BookE 2.06 TLB management */
976fcf5ef2aSThomas Huth 
booke206_cur_tlb(CPUPPCState * env)977fcf5ef2aSThomas Huth static ppcmas_tlb_t *booke206_cur_tlb(CPUPPCState *env)
978fcf5ef2aSThomas Huth {
979fcf5ef2aSThomas Huth     uint32_t tlbncfg = 0;
980fcf5ef2aSThomas Huth     int esel = (env->spr[SPR_BOOKE_MAS0] & MAS0_ESEL_MASK) >> MAS0_ESEL_SHIFT;
981fcf5ef2aSThomas Huth     int ea = (env->spr[SPR_BOOKE_MAS2] & MAS2_EPN_MASK);
982fcf5ef2aSThomas Huth     int tlb;
983fcf5ef2aSThomas Huth 
984fcf5ef2aSThomas Huth     tlb = (env->spr[SPR_BOOKE_MAS0] & MAS0_TLBSEL_MASK) >> MAS0_TLBSEL_SHIFT;
985fcf5ef2aSThomas Huth     tlbncfg = env->spr[SPR_BOOKE_TLB0CFG + tlb];
986fcf5ef2aSThomas Huth 
987fcf5ef2aSThomas Huth     if ((tlbncfg & TLBnCFG_HES) && (env->spr[SPR_BOOKE_MAS0] & MAS0_HES)) {
988db70b311SRichard Henderson         cpu_abort(env_cpu(env), "we don't support HES yet\n");
989fcf5ef2aSThomas Huth     }
990fcf5ef2aSThomas Huth 
991fcf5ef2aSThomas Huth     return booke206_get_tlbm(env, tlb, ea, esel);
992fcf5ef2aSThomas Huth }
993fcf5ef2aSThomas Huth 
helper_booke_setpid(CPUPPCState * env,uint32_t pidn,target_ulong pid)994fcf5ef2aSThomas Huth void helper_booke_setpid(CPUPPCState *env, uint32_t pidn, target_ulong pid)
995fcf5ef2aSThomas Huth {
996fcf5ef2aSThomas Huth     env->spr[pidn] = pid;
997fcf5ef2aSThomas Huth     /* changing PIDs mean we're in a different address space now */
998db70b311SRichard Henderson     tlb_flush(env_cpu(env));
999fcf5ef2aSThomas Huth }
1000fcf5ef2aSThomas Huth 
helper_booke_set_eplc(CPUPPCState * env,target_ulong val)100150728199SRoman Kapl void helper_booke_set_eplc(CPUPPCState *env, target_ulong val)
100250728199SRoman Kapl {
100350728199SRoman Kapl     env->spr[SPR_BOOKE_EPLC] = val & EPID_MASK;
1004db70b311SRichard Henderson     tlb_flush_by_mmuidx(env_cpu(env), 1 << PPC_TLB_EPID_LOAD);
100550728199SRoman Kapl }
helper_booke_set_epsc(CPUPPCState * env,target_ulong val)100650728199SRoman Kapl void helper_booke_set_epsc(CPUPPCState *env, target_ulong val)
100750728199SRoman Kapl {
100850728199SRoman Kapl     env->spr[SPR_BOOKE_EPSC] = val & EPID_MASK;
1009db70b311SRichard Henderson     tlb_flush_by_mmuidx(env_cpu(env), 1 << PPC_TLB_EPID_STORE);
101050728199SRoman Kapl }
101150728199SRoman Kapl 
flush_page(CPUPPCState * env,ppcmas_tlb_t * tlb)10122e569845SLuc MICHEL static inline void flush_page(CPUPPCState *env, ppcmas_tlb_t *tlb)
10132e569845SLuc MICHEL {
10142e569845SLuc MICHEL     if (booke206_tlb_to_page_size(env, tlb) == TARGET_PAGE_SIZE) {
1015db70b311SRichard Henderson         tlb_flush_page(env_cpu(env), tlb->mas2 & MAS2_EPN_MASK);
10162e569845SLuc MICHEL     } else {
1017db70b311SRichard Henderson         tlb_flush(env_cpu(env));
10182e569845SLuc MICHEL     }
10192e569845SLuc MICHEL }
10202e569845SLuc MICHEL 
helper_booke206_tlbwe(CPUPPCState * env)1021fcf5ef2aSThomas Huth void helper_booke206_tlbwe(CPUPPCState *env)
1022fcf5ef2aSThomas Huth {
1023fcf5ef2aSThomas Huth     uint32_t tlbncfg, tlbn;
1024fcf5ef2aSThomas Huth     ppcmas_tlb_t *tlb;
1025fcf5ef2aSThomas Huth     uint32_t size_tlb, size_ps;
1026fcf5ef2aSThomas Huth     target_ulong mask;
1027fcf5ef2aSThomas Huth 
1028fcf5ef2aSThomas Huth 
1029fcf5ef2aSThomas Huth     switch (env->spr[SPR_BOOKE_MAS0] & MAS0_WQ_MASK) {
1030fcf5ef2aSThomas Huth     case MAS0_WQ_ALWAYS:
1031fcf5ef2aSThomas Huth         /* good to go, write that entry */
1032fcf5ef2aSThomas Huth         break;
1033fcf5ef2aSThomas Huth     case MAS0_WQ_COND:
1034fcf5ef2aSThomas Huth         /* XXX check if reserved */
1035fcf5ef2aSThomas Huth         if (0) {
1036fcf5ef2aSThomas Huth             return;
1037fcf5ef2aSThomas Huth         }
1038fcf5ef2aSThomas Huth         break;
1039fcf5ef2aSThomas Huth     case MAS0_WQ_CLR_RSRV:
1040fcf5ef2aSThomas Huth         /* XXX clear entry */
1041fcf5ef2aSThomas Huth         return;
1042fcf5ef2aSThomas Huth     default:
1043fcf5ef2aSThomas Huth         /* no idea what to do */
1044fcf5ef2aSThomas Huth         return;
1045fcf5ef2aSThomas Huth     }
1046fcf5ef2aSThomas Huth 
1047fcf5ef2aSThomas Huth     if (((env->spr[SPR_BOOKE_MAS0] & MAS0_ATSEL) == MAS0_ATSEL_LRAT) &&
104810b2b373SVíctor Colombo         !FIELD_EX64(env->msr, MSR, GS)) {
1049fcf5ef2aSThomas Huth         /* XXX we don't support direct LRAT setting yet */
1050fcf5ef2aSThomas Huth         fprintf(stderr, "cpu: don't support LRAT setting yet\n");
1051fcf5ef2aSThomas Huth         return;
1052fcf5ef2aSThomas Huth     }
1053fcf5ef2aSThomas Huth 
1054fcf5ef2aSThomas Huth     tlbn = (env->spr[SPR_BOOKE_MAS0] & MAS0_TLBSEL_MASK) >> MAS0_TLBSEL_SHIFT;
1055fcf5ef2aSThomas Huth     tlbncfg = env->spr[SPR_BOOKE_TLB0CFG + tlbn];
1056fcf5ef2aSThomas Huth 
1057fcf5ef2aSThomas Huth     tlb = booke206_cur_tlb(env);
1058fcf5ef2aSThomas Huth 
1059fcf5ef2aSThomas Huth     if (!tlb) {
1060fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
1061fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL |
1062fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL_INVAL, GETPC());
1063fcf5ef2aSThomas Huth     }
1064fcf5ef2aSThomas Huth 
1065fcf5ef2aSThomas Huth     /* check that we support the targeted size */
1066fcf5ef2aSThomas Huth     size_tlb = (env->spr[SPR_BOOKE_MAS1] & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
1067fcf5ef2aSThomas Huth     size_ps = booke206_tlbnps(env, tlbn);
1068fcf5ef2aSThomas Huth     if ((env->spr[SPR_BOOKE_MAS1] & MAS1_VALID) && (tlbncfg & TLBnCFG_AVAIL) &&
1069fcf5ef2aSThomas Huth         !(size_ps & (1 << size_tlb))) {
1070fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
1071fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL |
1072fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL_INVAL, GETPC());
1073fcf5ef2aSThomas Huth     }
1074fcf5ef2aSThomas Huth 
107510b2b373SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, GS)) {
1076db70b311SRichard Henderson         cpu_abort(env_cpu(env), "missing HV implementation\n");
1077fcf5ef2aSThomas Huth     }
10782e569845SLuc MICHEL 
10792e569845SLuc MICHEL     if (tlb->mas1 & MAS1_VALID) {
1080fe4ade31SDavid Gibson         /*
1081fe4ade31SDavid Gibson          * Invalidate the page in QEMU TLB if it was a valid entry.
10822e569845SLuc MICHEL          *
10832e569845SLuc MICHEL          * In "PowerPC e500 Core Family Reference Manual, Rev. 1",
10842e569845SLuc MICHEL          * Section "12.4.2 TLB Write Entry (tlbwe) Instruction":
10852e569845SLuc MICHEL          * (https://www.nxp.com/docs/en/reference-manual/E500CORERM.pdf)
10862e569845SLuc MICHEL          *
10872e569845SLuc MICHEL          * "Note that when an L2 TLB entry is written, it may be displacing an
10882e569845SLuc MICHEL          * already valid entry in the same L2 TLB location (a victim). If a
10892e569845SLuc MICHEL          * valid L1 TLB entry corresponds to the L2 MMU victim entry, that L1
1090fe4ade31SDavid Gibson          * TLB entry is automatically invalidated."
1091fe4ade31SDavid Gibson          */
10922e569845SLuc MICHEL         flush_page(env, tlb);
10932e569845SLuc MICHEL     }
10942e569845SLuc MICHEL 
1095fcf5ef2aSThomas Huth     tlb->mas7_3 = ((uint64_t)env->spr[SPR_BOOKE_MAS7] << 32) |
1096fcf5ef2aSThomas Huth         env->spr[SPR_BOOKE_MAS3];
1097fcf5ef2aSThomas Huth     tlb->mas1 = env->spr[SPR_BOOKE_MAS1];
1098fcf5ef2aSThomas Huth 
1099c449d8baSKONRAD Frederic     if ((env->spr[SPR_MMUCFG] & MMUCFG_MAVN) == MMUCFG_MAVN_V2) {
1100c449d8baSKONRAD Frederic         /* For TLB which has a fixed size TSIZE is ignored with MAV2 */
1101c449d8baSKONRAD Frederic         booke206_fixed_size_tlbn(env, tlbn, tlb);
1102c449d8baSKONRAD Frederic     } else {
1103fcf5ef2aSThomas Huth         if (!(tlbncfg & TLBnCFG_AVAIL)) {
1104fcf5ef2aSThomas Huth             /* force !AVAIL TLB entries to correct page size */
1105fcf5ef2aSThomas Huth             tlb->mas1 &= ~MAS1_TSIZE_MASK;
1106fcf5ef2aSThomas Huth             /* XXX can be configured in MMUCSR0 */
1107fcf5ef2aSThomas Huth             tlb->mas1 |= (tlbncfg & TLBnCFG_MINSIZE) >> 12;
1108fcf5ef2aSThomas Huth         }
1109c449d8baSKONRAD Frederic     }
1110fcf5ef2aSThomas Huth 
1111fcf5ef2aSThomas Huth     /* Make a mask from TLB size to discard invalid bits in EPN field */
1112fcf5ef2aSThomas Huth     mask = ~(booke206_tlb_to_page_size(env, tlb) - 1);
1113fcf5ef2aSThomas Huth     /* Add a mask for page attributes */
1114fcf5ef2aSThomas Huth     mask |= MAS2_ACM | MAS2_VLE | MAS2_W | MAS2_I | MAS2_M | MAS2_G | MAS2_E;
1115fcf5ef2aSThomas Huth 
1116cda23360SVíctor Colombo     if (!FIELD_EX64(env->msr, MSR, CM)) {
1117fe4ade31SDavid Gibson         /*
1118fe4ade31SDavid Gibson          * Executing a tlbwe instruction in 32-bit mode will set bits
1119fe4ade31SDavid Gibson          * 0:31 of the TLB EPN field to zero.
1120fcf5ef2aSThomas Huth          */
1121fcf5ef2aSThomas Huth         mask &= 0xffffffff;
1122fcf5ef2aSThomas Huth     }
1123fcf5ef2aSThomas Huth 
1124fcf5ef2aSThomas Huth     tlb->mas2 = env->spr[SPR_BOOKE_MAS2] & mask;
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth     if (!(tlbncfg & TLBnCFG_IPROT)) {
1127fcf5ef2aSThomas Huth         /* no IPROT supported by TLB */
1128fcf5ef2aSThomas Huth         tlb->mas1 &= ~MAS1_IPROT;
1129fcf5ef2aSThomas Huth     }
1130fcf5ef2aSThomas Huth 
11312e569845SLuc MICHEL     flush_page(env, tlb);
1132fcf5ef2aSThomas Huth }
1133fcf5ef2aSThomas Huth 
booke206_tlb_to_mas(CPUPPCState * env,ppcmas_tlb_t * tlb)1134fcf5ef2aSThomas Huth static inline void booke206_tlb_to_mas(CPUPPCState *env, ppcmas_tlb_t *tlb)
1135fcf5ef2aSThomas Huth {
1136fcf5ef2aSThomas Huth     int tlbn = booke206_tlbm_to_tlbn(env, tlb);
1137fcf5ef2aSThomas Huth     int way = booke206_tlbm_to_way(env, tlb);
1138fcf5ef2aSThomas Huth 
1139fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS0] = tlbn << MAS0_TLBSEL_SHIFT;
1140fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS0] |= way << MAS0_ESEL_SHIFT;
1141fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS0] |= env->last_way << MAS0_NV_SHIFT;
1142fcf5ef2aSThomas Huth 
1143fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS1] = tlb->mas1;
1144fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS2] = tlb->mas2;
1145fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS3] = tlb->mas7_3;
1146fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS7] = tlb->mas7_3 >> 32;
1147fcf5ef2aSThomas Huth }
1148fcf5ef2aSThomas Huth 
helper_booke206_tlbre(CPUPPCState * env)1149fcf5ef2aSThomas Huth void helper_booke206_tlbre(CPUPPCState *env)
1150fcf5ef2aSThomas Huth {
1151fcf5ef2aSThomas Huth     ppcmas_tlb_t *tlb = NULL;
1152fcf5ef2aSThomas Huth 
1153fcf5ef2aSThomas Huth     tlb = booke206_cur_tlb(env);
1154fcf5ef2aSThomas Huth     if (!tlb) {
1155fcf5ef2aSThomas Huth         env->spr[SPR_BOOKE_MAS1] = 0;
1156fcf5ef2aSThomas Huth     } else {
1157fcf5ef2aSThomas Huth         booke206_tlb_to_mas(env, tlb);
1158fcf5ef2aSThomas Huth     }
1159fcf5ef2aSThomas Huth }
1160fcf5ef2aSThomas Huth 
helper_booke206_tlbsx(CPUPPCState * env,target_ulong address)1161fcf5ef2aSThomas Huth void helper_booke206_tlbsx(CPUPPCState *env, target_ulong address)
1162fcf5ef2aSThomas Huth {
1163fcf5ef2aSThomas Huth     ppcmas_tlb_t *tlb = NULL;
1164fcf5ef2aSThomas Huth     int i, j;
1165fcf5ef2aSThomas Huth     hwaddr raddr;
1166fcf5ef2aSThomas Huth     uint32_t spid, sas;
1167fcf5ef2aSThomas Huth 
1168fcf5ef2aSThomas Huth     spid = (env->spr[SPR_BOOKE_MAS6] & MAS6_SPID_MASK) >> MAS6_SPID_SHIFT;
1169fcf5ef2aSThomas Huth     sas = env->spr[SPR_BOOKE_MAS6] & MAS6_SAS;
1170fcf5ef2aSThomas Huth 
1171fcf5ef2aSThomas Huth     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
1172fcf5ef2aSThomas Huth         int ways = booke206_tlb_ways(env, i);
1173fcf5ef2aSThomas Huth 
1174fcf5ef2aSThomas Huth         for (j = 0; j < ways; j++) {
1175fcf5ef2aSThomas Huth             tlb = booke206_get_tlbm(env, i, address, j);
1176fcf5ef2aSThomas Huth 
1177fcf5ef2aSThomas Huth             if (!tlb) {
1178fcf5ef2aSThomas Huth                 continue;
1179fcf5ef2aSThomas Huth             }
1180fcf5ef2aSThomas Huth 
1181fcf5ef2aSThomas Huth             if (ppcmas_tlb_check(env, tlb, &raddr, address, spid)) {
1182fcf5ef2aSThomas Huth                 continue;
1183fcf5ef2aSThomas Huth             }
1184fcf5ef2aSThomas Huth 
1185fcf5ef2aSThomas Huth             if (sas != ((tlb->mas1 & MAS1_TS) >> MAS1_TS_SHIFT)) {
1186fcf5ef2aSThomas Huth                 continue;
1187fcf5ef2aSThomas Huth             }
1188fcf5ef2aSThomas Huth 
1189fcf5ef2aSThomas Huth             booke206_tlb_to_mas(env, tlb);
1190fcf5ef2aSThomas Huth             return;
1191fcf5ef2aSThomas Huth         }
1192fcf5ef2aSThomas Huth     }
1193fcf5ef2aSThomas Huth 
1194fcf5ef2aSThomas Huth     /* no entry found, fill with defaults */
1195fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS0] = env->spr[SPR_BOOKE_MAS4] & MAS4_TLBSELD_MASK;
1196fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS1] = env->spr[SPR_BOOKE_MAS4] & MAS4_TSIZED_MASK;
1197fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS2] = env->spr[SPR_BOOKE_MAS4] & MAS4_WIMGED_MASK;
1198fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS3] = 0;
1199fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS7] = 0;
1200fcf5ef2aSThomas Huth 
1201fcf5ef2aSThomas Huth     if (env->spr[SPR_BOOKE_MAS6] & MAS6_SAS) {
1202fcf5ef2aSThomas Huth         env->spr[SPR_BOOKE_MAS1] |= MAS1_TS;
1203fcf5ef2aSThomas Huth     }
1204fcf5ef2aSThomas Huth 
1205fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS1] |= (env->spr[SPR_BOOKE_MAS6] >> 16)
1206fcf5ef2aSThomas Huth         << MAS1_TID_SHIFT;
1207fcf5ef2aSThomas Huth 
1208fcf5ef2aSThomas Huth     /* next victim logic */
1209fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS0] |= env->last_way << MAS0_ESEL_SHIFT;
1210fcf5ef2aSThomas Huth     env->last_way++;
1211fcf5ef2aSThomas Huth     env->last_way &= booke206_tlb_ways(env, 0) - 1;
1212fcf5ef2aSThomas Huth     env->spr[SPR_BOOKE_MAS0] |= env->last_way << MAS0_NV_SHIFT;
1213fcf5ef2aSThomas Huth }
1214fcf5ef2aSThomas Huth 
booke206_invalidate_ea_tlb(CPUPPCState * env,int tlbn,vaddr ea)1215fcf5ef2aSThomas Huth static inline void booke206_invalidate_ea_tlb(CPUPPCState *env, int tlbn,
1216d139786eSDaniel Henrique Barboza                                               vaddr ea)
1217fcf5ef2aSThomas Huth {
1218fcf5ef2aSThomas Huth     int i;
1219fcf5ef2aSThomas Huth     int ways = booke206_tlb_ways(env, tlbn);
1220fcf5ef2aSThomas Huth     target_ulong mask;
1221fcf5ef2aSThomas Huth 
1222fcf5ef2aSThomas Huth     for (i = 0; i < ways; i++) {
1223fcf5ef2aSThomas Huth         ppcmas_tlb_t *tlb = booke206_get_tlbm(env, tlbn, ea, i);
1224fcf5ef2aSThomas Huth         if (!tlb) {
1225fcf5ef2aSThomas Huth             continue;
1226fcf5ef2aSThomas Huth         }
1227fcf5ef2aSThomas Huth         mask = ~(booke206_tlb_to_page_size(env, tlb) - 1);
1228fcf5ef2aSThomas Huth         if (((tlb->mas2 & MAS2_EPN_MASK) == (ea & mask)) &&
1229fcf5ef2aSThomas Huth             !(tlb->mas1 & MAS1_IPROT)) {
1230fcf5ef2aSThomas Huth             tlb->mas1 &= ~MAS1_VALID;
1231fcf5ef2aSThomas Huth         }
1232fcf5ef2aSThomas Huth     }
1233fcf5ef2aSThomas Huth }
1234fcf5ef2aSThomas Huth 
helper_booke206_tlbivax(CPUPPCState * env,target_ulong address)1235fcf5ef2aSThomas Huth void helper_booke206_tlbivax(CPUPPCState *env, target_ulong address)
1236fcf5ef2aSThomas Huth {
1237fcf5ef2aSThomas Huth     CPUState *cs;
1238fcf5ef2aSThomas Huth 
1239fcf5ef2aSThomas Huth     if (address & 0x4) {
1240fcf5ef2aSThomas Huth         /* flush all entries */
1241fcf5ef2aSThomas Huth         if (address & 0x8) {
1242fcf5ef2aSThomas Huth             /* flush all of TLB1 */
1243fcf5ef2aSThomas Huth             booke206_flush_tlb(env, BOOKE206_FLUSH_TLB1, 1);
1244fcf5ef2aSThomas Huth         } else {
1245fcf5ef2aSThomas Huth             /* flush all of TLB0 */
1246fcf5ef2aSThomas Huth             booke206_flush_tlb(env, BOOKE206_FLUSH_TLB0, 0);
1247fcf5ef2aSThomas Huth         }
1248fcf5ef2aSThomas Huth         return;
1249fcf5ef2aSThomas Huth     }
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth     if (address & 0x8) {
1252fcf5ef2aSThomas Huth         /* flush TLB1 entries */
1253fcf5ef2aSThomas Huth         booke206_invalidate_ea_tlb(env, 1, address);
1254fcf5ef2aSThomas Huth         CPU_FOREACH(cs) {
1255d10eb08fSAlex Bennée             tlb_flush(cs);
1256fcf5ef2aSThomas Huth         }
1257fcf5ef2aSThomas Huth     } else {
1258fcf5ef2aSThomas Huth         /* flush TLB0 entries */
1259fcf5ef2aSThomas Huth         booke206_invalidate_ea_tlb(env, 0, address);
1260fcf5ef2aSThomas Huth         CPU_FOREACH(cs) {
1261fcf5ef2aSThomas Huth             tlb_flush_page(cs, address & MAS2_EPN_MASK);
1262fcf5ef2aSThomas Huth         }
1263fcf5ef2aSThomas Huth     }
1264fcf5ef2aSThomas Huth }
1265fcf5ef2aSThomas Huth 
helper_booke206_tlbilx0(CPUPPCState * env,target_ulong address)1266fcf5ef2aSThomas Huth void helper_booke206_tlbilx0(CPUPPCState *env, target_ulong address)
1267fcf5ef2aSThomas Huth {
1268fcf5ef2aSThomas Huth     /* XXX missing LPID handling */
1269fcf5ef2aSThomas Huth     booke206_flush_tlb(env, -1, 1);
1270fcf5ef2aSThomas Huth }
1271fcf5ef2aSThomas Huth 
helper_booke206_tlbilx1(CPUPPCState * env,target_ulong address)1272fcf5ef2aSThomas Huth void helper_booke206_tlbilx1(CPUPPCState *env, target_ulong address)
1273fcf5ef2aSThomas Huth {
1274fcf5ef2aSThomas Huth     int i, j;
1275fcf5ef2aSThomas Huth     int tid = (env->spr[SPR_BOOKE_MAS6] & MAS6_SPID);
1276fcf5ef2aSThomas Huth     ppcmas_tlb_t *tlb = env->tlb.tlbm;
1277fcf5ef2aSThomas Huth     int tlb_size;
1278fcf5ef2aSThomas Huth 
1279fcf5ef2aSThomas Huth     /* XXX missing LPID handling */
1280fcf5ef2aSThomas Huth     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
1281fcf5ef2aSThomas Huth         tlb_size = booke206_tlb_size(env, i);
1282fcf5ef2aSThomas Huth         for (j = 0; j < tlb_size; j++) {
1283fcf5ef2aSThomas Huth             if (!(tlb[j].mas1 & MAS1_IPROT) &&
1284fcf5ef2aSThomas Huth                 ((tlb[j].mas1 & MAS1_TID_MASK) == tid)) {
1285fcf5ef2aSThomas Huth                 tlb[j].mas1 &= ~MAS1_VALID;
1286fcf5ef2aSThomas Huth             }
1287fcf5ef2aSThomas Huth         }
1288fcf5ef2aSThomas Huth         tlb += booke206_tlb_size(env, i);
1289fcf5ef2aSThomas Huth     }
1290db70b311SRichard Henderson     tlb_flush(env_cpu(env));
1291fcf5ef2aSThomas Huth }
1292fcf5ef2aSThomas Huth 
helper_booke206_tlbilx3(CPUPPCState * env,target_ulong address)1293fcf5ef2aSThomas Huth void helper_booke206_tlbilx3(CPUPPCState *env, target_ulong address)
1294fcf5ef2aSThomas Huth {
1295fcf5ef2aSThomas Huth     int i, j;
1296fcf5ef2aSThomas Huth     ppcmas_tlb_t *tlb;
1297fcf5ef2aSThomas Huth     int tid = (env->spr[SPR_BOOKE_MAS6] & MAS6_SPID);
1298fcf5ef2aSThomas Huth     int pid = tid >> MAS6_SPID_SHIFT;
1299fcf5ef2aSThomas Huth     int sgs = env->spr[SPR_BOOKE_MAS5] & MAS5_SGS;
1300fcf5ef2aSThomas Huth     int ind = (env->spr[SPR_BOOKE_MAS6] & MAS6_SIND) ? MAS1_IND : 0;
1301fcf5ef2aSThomas Huth     /* XXX check for unsupported isize and raise an invalid opcode then */
1302fcf5ef2aSThomas Huth     int size = env->spr[SPR_BOOKE_MAS6] & MAS6_ISIZE_MASK;
1303fcf5ef2aSThomas Huth     /* XXX implement MAV2 handling */
1304fcf5ef2aSThomas Huth     bool mav2 = false;
1305fcf5ef2aSThomas Huth 
1306fcf5ef2aSThomas Huth     /* XXX missing LPID handling */
1307fcf5ef2aSThomas Huth     /* flush by pid and ea */
1308fcf5ef2aSThomas Huth     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
1309fcf5ef2aSThomas Huth         int ways = booke206_tlb_ways(env, i);
1310fcf5ef2aSThomas Huth 
1311fcf5ef2aSThomas Huth         for (j = 0; j < ways; j++) {
1312fcf5ef2aSThomas Huth             tlb = booke206_get_tlbm(env, i, address, j);
1313fcf5ef2aSThomas Huth             if (!tlb) {
1314fcf5ef2aSThomas Huth                 continue;
1315fcf5ef2aSThomas Huth             }
1316fcf5ef2aSThomas Huth             if ((ppcmas_tlb_check(env, tlb, NULL, address, pid) != 0) ||
1317fcf5ef2aSThomas Huth                 (tlb->mas1 & MAS1_IPROT) ||
1318fcf5ef2aSThomas Huth                 ((tlb->mas1 & MAS1_IND) != ind) ||
1319fcf5ef2aSThomas Huth                 ((tlb->mas8 & MAS8_TGS) != sgs)) {
1320fcf5ef2aSThomas Huth                 continue;
1321fcf5ef2aSThomas Huth             }
1322fcf5ef2aSThomas Huth             if (mav2 && ((tlb->mas1 & MAS1_TSIZE_MASK) != size)) {
1323fcf5ef2aSThomas Huth                 /* XXX only check when MMUCFG[TWC] || TLBnCFG[HES] */
1324fcf5ef2aSThomas Huth                 continue;
1325fcf5ef2aSThomas Huth             }
1326fcf5ef2aSThomas Huth             /* XXX e500mc doesn't match SAS, but other cores might */
1327fcf5ef2aSThomas Huth             tlb->mas1 &= ~MAS1_VALID;
1328fcf5ef2aSThomas Huth         }
1329fcf5ef2aSThomas Huth     }
1330db70b311SRichard Henderson     tlb_flush(env_cpu(env));
1331fcf5ef2aSThomas Huth }
1332fcf5ef2aSThomas Huth 
helper_booke206_tlbflush(CPUPPCState * env,target_ulong type)1333fcf5ef2aSThomas Huth void helper_booke206_tlbflush(CPUPPCState *env, target_ulong type)
1334fcf5ef2aSThomas Huth {
1335fcf5ef2aSThomas Huth     int flags = 0;
1336fcf5ef2aSThomas Huth 
1337fcf5ef2aSThomas Huth     if (type & 2) {
1338fcf5ef2aSThomas Huth         flags |= BOOKE206_FLUSH_TLB1;
1339fcf5ef2aSThomas Huth     }
1340fcf5ef2aSThomas Huth 
1341fcf5ef2aSThomas Huth     if (type & 4) {
1342fcf5ef2aSThomas Huth         flags |= BOOKE206_FLUSH_TLB0;
1343fcf5ef2aSThomas Huth     }
1344fcf5ef2aSThomas Huth 
1345fcf5ef2aSThomas Huth     booke206_flush_tlb(env, flags, 1);
1346fcf5ef2aSThomas Huth }
1347fcf5ef2aSThomas Huth 
1348fcf5ef2aSThomas Huth 
helper_check_tlb_flush_local(CPUPPCState * env)1349fcf5ef2aSThomas Huth void helper_check_tlb_flush_local(CPUPPCState *env)
1350fcf5ef2aSThomas Huth {
1351fcf5ef2aSThomas Huth     check_tlb_flush(env, false);
1352fcf5ef2aSThomas Huth }
1353fcf5ef2aSThomas Huth 
helper_check_tlb_flush_global(CPUPPCState * env)1354fcf5ef2aSThomas Huth void helper_check_tlb_flush_global(CPUPPCState *env)
1355fcf5ef2aSThomas Huth {
1356fcf5ef2aSThomas Huth     check_tlb_flush(env, true);
1357fcf5ef2aSThomas Huth }
1358fcf5ef2aSThomas Huth 
1359fcf5ef2aSThomas Huth 
ppc_cpu_tlb_fill(CPUState * cs,vaddr eaddr,int size,MMUAccessType access_type,int mmu_idx,bool probe,uintptr_t retaddr)136051806b54SRichard Henderson bool ppc_cpu_tlb_fill(CPUState *cs, vaddr eaddr, int size,
1361351bc97eSRichard Henderson                       MMUAccessType access_type, int mmu_idx,
1362351bc97eSRichard Henderson                       bool probe, uintptr_t retaddr)
1363fcf5ef2aSThomas Huth {
1364fcf5ef2aSThomas Huth     PowerPCCPU *cpu = POWERPC_CPU(cs);
136551806b54SRichard Henderson     hwaddr raddr;
136651806b54SRichard Henderson     int page_size, prot;
1367fcf5ef2aSThomas Huth 
136851806b54SRichard Henderson     if (ppc_xlate(cpu, eaddr, access_type, &raddr,
136951806b54SRichard Henderson                   &page_size, &prot, mmu_idx, !probe)) {
137051806b54SRichard Henderson         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
137151806b54SRichard Henderson                      prot, mmu_idx, 1UL << page_size);
137251806b54SRichard Henderson         return true;
1373fcf5ef2aSThomas Huth     }
1374351bc97eSRichard Henderson     if (probe) {
1375351bc97eSRichard Henderson         return false;
1376351bc97eSRichard Henderson     }
137751806b54SRichard Henderson     raise_exception_err_ra(&cpu->env, cs->exception_index,
137851806b54SRichard Henderson                            cpu->env.error_code, retaddr);
1379351bc97eSRichard Henderson }
1380