18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * MMU context handling.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Wind River Systems Inc
68c2ecf20Sopenharmony_ci *   Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
98c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
108c2ecf20Sopenharmony_ci * for more details.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/mm.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <asm/cpuinfo.h>
168c2ecf20Sopenharmony_ci#include <asm/mmu_context.h>
178c2ecf20Sopenharmony_ci#include <asm/tlb.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* The pids position and mask in context */
208c2ecf20Sopenharmony_ci#define PID_SHIFT	0
218c2ecf20Sopenharmony_ci#define PID_BITS	(cpuinfo.tlb_pid_num_bits)
228c2ecf20Sopenharmony_ci#define PID_MASK	((1UL << PID_BITS) - 1)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* The versions position and mask in context */
258c2ecf20Sopenharmony_ci#define VERSION_BITS	(32 - PID_BITS)
268c2ecf20Sopenharmony_ci#define VERSION_SHIFT	(PID_SHIFT + PID_BITS)
278c2ecf20Sopenharmony_ci#define VERSION_MASK	((1UL << VERSION_BITS) - 1)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Return the version part of a context */
308c2ecf20Sopenharmony_ci#define CTX_VERSION(c)	(((c) >> VERSION_SHIFT) & VERSION_MASK)
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Return the pid part of a context */
338c2ecf20Sopenharmony_ci#define CTX_PID(c)	(((c) >> PID_SHIFT) & PID_MASK)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Value of the first context (version 1, pid 0) */
368c2ecf20Sopenharmony_ci#define FIRST_CTX	((1UL << VERSION_SHIFT) | (0 << PID_SHIFT))
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic mm_context_t next_mmu_context;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Initialize MMU context management stuff.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_civoid __init mmu_context_init(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	/* We need to set this here because the value depends on runtime data
468c2ecf20Sopenharmony_ci	 * from cpuinfo */
478c2ecf20Sopenharmony_ci	next_mmu_context = FIRST_CTX;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/*
518c2ecf20Sopenharmony_ci * Set new context (pid), keep way
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_cistatic void set_context(mm_context_t context)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	set_mmu_pid(CTX_PID(context));
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic mm_context_t get_new_context(void)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	/* Return the next pid */
618c2ecf20Sopenharmony_ci	next_mmu_context += (1UL << PID_SHIFT);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	/* If the pid field wraps around we increase the version and
648c2ecf20Sopenharmony_ci	 * flush the tlb */
658c2ecf20Sopenharmony_ci	if (unlikely(CTX_PID(next_mmu_context) == 0)) {
668c2ecf20Sopenharmony_ci		/* Version is incremented since the pid increment above
678c2ecf20Sopenharmony_ci		 * overflows info version */
688c2ecf20Sopenharmony_ci		flush_cache_all();
698c2ecf20Sopenharmony_ci		flush_tlb_all();
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* If the version wraps we start over with the first generation, we do
738c2ecf20Sopenharmony_ci	 * not need to flush the tlb here since it's always done above */
748c2ecf20Sopenharmony_ci	if (unlikely(CTX_VERSION(next_mmu_context) == 0))
758c2ecf20Sopenharmony_ci		next_mmu_context = FIRST_CTX;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return next_mmu_context;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_civoid switch_mm(struct mm_struct *prev, struct mm_struct *next,
818c2ecf20Sopenharmony_ci	       struct task_struct *tsk)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	unsigned long flags;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	local_irq_save(flags);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* If the process context we are swapping in has a different context
888c2ecf20Sopenharmony_ci	 * generation then we have it should get a new generation/pid */
898c2ecf20Sopenharmony_ci	if (unlikely(CTX_VERSION(next->context) !=
908c2ecf20Sopenharmony_ci		CTX_VERSION(next_mmu_context)))
918c2ecf20Sopenharmony_ci		next->context = get_new_context();
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	/* Save the current pgd so the fast tlb handler can find it */
948c2ecf20Sopenharmony_ci	pgd_current = next->pgd;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* Set the current context */
978c2ecf20Sopenharmony_ci	set_context(next->context);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/*
1038c2ecf20Sopenharmony_ci * After we have set current->mm to a new value, this activates
1048c2ecf20Sopenharmony_ci * the context for the new mm so we see the new mappings.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_civoid activate_mm(struct mm_struct *prev, struct mm_struct *next)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	next->context = get_new_context();
1098c2ecf20Sopenharmony_ci	set_context(next->context);
1108c2ecf20Sopenharmony_ci	pgd_current = next->pgd;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciunsigned long get_pid_from_context(mm_context_t *context)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	return CTX_PID((*context));
1168c2ecf20Sopenharmony_ci}
117