18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> 48c2ecf20Sopenharmony_ci * Copyright (C) 2008-2009 PetaLogix 58c2ecf20Sopenharmony_ci * Copyright (C) 2006 Atmark Techno, Inc. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#ifndef _ASM_MICROBLAZE_MMU_CONTEXT_H 98c2ecf20Sopenharmony_ci#define _ASM_MICROBLAZE_MMU_CONTEXT_H 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/atomic.h> 128c2ecf20Sopenharmony_ci#include <linux/mm_types.h> 138c2ecf20Sopenharmony_ci#include <linux/sched.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <asm/bitops.h> 168c2ecf20Sopenharmony_ci#include <asm/mmu.h> 178c2ecf20Sopenharmony_ci#include <asm-generic/mm_hooks.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci# ifdef __KERNEL__ 208c2ecf20Sopenharmony_ci/* 218c2ecf20Sopenharmony_ci * This function defines the mapping from contexts to VSIDs (virtual 228c2ecf20Sopenharmony_ci * segment IDs). We use a skew on both the context and the high 4 bits 238c2ecf20Sopenharmony_ci * of the 32-bit virtual address (the "effective segment ID") in order 248c2ecf20Sopenharmony_ci * to spread out the entries in the MMU hash table. 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_ci# define CTX_TO_VSID(ctx, va) (((ctx) * (897 * 16) + ((va) >> 28) * 0x111) \ 278c2ecf20Sopenharmony_ci & 0xffffff) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* 308c2ecf20Sopenharmony_ci MicroBlaze has 256 contexts, so we can just rotate through these 318c2ecf20Sopenharmony_ci as a way of "switching" contexts. If the TID of the TLB is zero, 328c2ecf20Sopenharmony_ci the PID/TID comparison is disabled, so we can use a TID of zero 338c2ecf20Sopenharmony_ci to represent all kernel pages as shared among all contexts. 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci# define NO_CONTEXT 256 418c2ecf20Sopenharmony_ci# define LAST_CONTEXT 255 428c2ecf20Sopenharmony_ci# define FIRST_CONTEXT 1 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* 458c2ecf20Sopenharmony_ci * Set the current MMU context. 468c2ecf20Sopenharmony_ci * This is done byloading up the segment registers for the user part of the 478c2ecf20Sopenharmony_ci * address space. 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * Since the PGD is immediately available, it is much faster to simply 508c2ecf20Sopenharmony_ci * pass this along as a second parameter, which is required for 8xx and 518c2ecf20Sopenharmony_ci * can be used for debugging on all processors (if you happen to have 528c2ecf20Sopenharmony_ci * an Abatron). 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_ciextern void set_context(mm_context_t context, pgd_t *pgd); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * Bitmap of contexts in use. 588c2ecf20Sopenharmony_ci * The size of this bitmap is LAST_CONTEXT + 1 bits. 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_ciextern unsigned long context_map[]; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* 638c2ecf20Sopenharmony_ci * This caches the next context number that we expect to be free. 648c2ecf20Sopenharmony_ci * Its use is an optimization only, we can't rely on this context 658c2ecf20Sopenharmony_ci * number to be free, but it usually will be. 668c2ecf20Sopenharmony_ci */ 678c2ecf20Sopenharmony_ciextern mm_context_t next_mmu_context; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* 708c2ecf20Sopenharmony_ci * Since we don't have sufficient contexts to give one to every task 718c2ecf20Sopenharmony_ci * that could be in the system, we need to be able to steal contexts. 728c2ecf20Sopenharmony_ci * These variables support that. 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_ciextern atomic_t nr_free_contexts; 758c2ecf20Sopenharmony_ciextern struct mm_struct *context_mm[LAST_CONTEXT+1]; 768c2ecf20Sopenharmony_ciextern void steal_context(void); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/* 798c2ecf20Sopenharmony_ci * Get a new mmu context for the address space described by `mm'. 808c2ecf20Sopenharmony_ci */ 818c2ecf20Sopenharmony_cistatic inline void get_mmu_context(struct mm_struct *mm) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci mm_context_t ctx; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (mm->context != NO_CONTEXT) 868c2ecf20Sopenharmony_ci return; 878c2ecf20Sopenharmony_ci while (atomic_dec_if_positive(&nr_free_contexts) < 0) 888c2ecf20Sopenharmony_ci steal_context(); 898c2ecf20Sopenharmony_ci ctx = next_mmu_context; 908c2ecf20Sopenharmony_ci while (test_and_set_bit(ctx, context_map)) { 918c2ecf20Sopenharmony_ci ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx); 928c2ecf20Sopenharmony_ci if (ctx > LAST_CONTEXT) 938c2ecf20Sopenharmony_ci ctx = 0; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci next_mmu_context = (ctx + 1) & LAST_CONTEXT; 968c2ecf20Sopenharmony_ci mm->context = ctx; 978c2ecf20Sopenharmony_ci context_mm[ctx] = mm; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* 1018c2ecf20Sopenharmony_ci * Set up the context for a new address space. 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_ci# define init_new_context(tsk, mm) (((mm)->context = NO_CONTEXT), 0) 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/* 1068c2ecf20Sopenharmony_ci * We're finished using the context for an address space. 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_cistatic inline void destroy_context(struct mm_struct *mm) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci if (mm->context != NO_CONTEXT) { 1118c2ecf20Sopenharmony_ci clear_bit(mm->context, context_map); 1128c2ecf20Sopenharmony_ci mm->context = NO_CONTEXT; 1138c2ecf20Sopenharmony_ci atomic_inc(&nr_free_contexts); 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, 1188c2ecf20Sopenharmony_ci struct task_struct *tsk) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci tsk->thread.pgdir = next->pgd; 1218c2ecf20Sopenharmony_ci get_mmu_context(next); 1228c2ecf20Sopenharmony_ci set_context(next->context, next->pgd); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/* 1268c2ecf20Sopenharmony_ci * After we have set current->mm to a new value, this activates 1278c2ecf20Sopenharmony_ci * the context for the new mm so we see the new mappings. 1288c2ecf20Sopenharmony_ci */ 1298c2ecf20Sopenharmony_cistatic inline void activate_mm(struct mm_struct *active_mm, 1308c2ecf20Sopenharmony_ci struct mm_struct *mm) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci current->thread.pgdir = mm->pgd; 1338c2ecf20Sopenharmony_ci get_mmu_context(mm); 1348c2ecf20Sopenharmony_ci set_context(mm->context, mm->pgd); 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ciextern void mmu_context_init(void); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci# endif /* __KERNEL__ */ 1408c2ecf20Sopenharmony_ci#endif /* _ASM_MICROBLAZE_MMU_CONTEXT_H */ 141