162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
462306a36Sopenharmony_ci * Copyright (C) 2008-2009 PetaLogix
562306a36Sopenharmony_ci * Copyright (C) 2006 Atmark Techno, Inc.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef _ASM_MICROBLAZE_MMU_CONTEXT_H
962306a36Sopenharmony_ci#define _ASM_MICROBLAZE_MMU_CONTEXT_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/atomic.h>
1262306a36Sopenharmony_ci#include <linux/mm_types.h>
1362306a36Sopenharmony_ci#include <linux/sched.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <asm/bitops.h>
1662306a36Sopenharmony_ci#include <asm/mmu.h>
1762306a36Sopenharmony_ci#include <asm-generic/mm_hooks.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci# ifdef __KERNEL__
2062306a36Sopenharmony_ci/*
2162306a36Sopenharmony_ci * This function defines the mapping from contexts to VSIDs (virtual
2262306a36Sopenharmony_ci * segment IDs).  We use a skew on both the context and the high 4 bits
2362306a36Sopenharmony_ci * of the 32-bit virtual address (the "effective segment ID") in order
2462306a36Sopenharmony_ci * to spread out the entries in the MMU hash table.
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci# define CTX_TO_VSID(ctx, va)	(((ctx) * (897 * 16) + ((va) >> 28) * 0x111) \
2762306a36Sopenharmony_ci				 & 0xffffff)
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci   MicroBlaze has 256 contexts, so we can just rotate through these
3162306a36Sopenharmony_ci   as a way of "switching" contexts.  If the TID of the TLB is zero,
3262306a36Sopenharmony_ci   the PID/TID comparison is disabled, so we can use a TID of zero
3362306a36Sopenharmony_ci   to represent all kernel pages as shared among all contexts.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci# define NO_CONTEXT	256
3762306a36Sopenharmony_ci# define LAST_CONTEXT	255
3862306a36Sopenharmony_ci# define FIRST_CONTEXT	1
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/*
4162306a36Sopenharmony_ci * Set the current MMU context.
4262306a36Sopenharmony_ci * This is done byloading up the segment registers for the user part of the
4362306a36Sopenharmony_ci * address space.
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * Since the PGD is immediately available, it is much faster to simply
4662306a36Sopenharmony_ci * pass this along as a second parameter, which is required for 8xx and
4762306a36Sopenharmony_ci * can be used for debugging on all processors (if you happen to have
4862306a36Sopenharmony_ci * an Abatron).
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_ciextern void set_context(mm_context_t context, pgd_t *pgd);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci/*
5362306a36Sopenharmony_ci * Bitmap of contexts in use.
5462306a36Sopenharmony_ci * The size of this bitmap is LAST_CONTEXT + 1 bits.
5562306a36Sopenharmony_ci */
5662306a36Sopenharmony_ciextern unsigned long context_map[];
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/*
5962306a36Sopenharmony_ci * This caches the next context number that we expect to be free.
6062306a36Sopenharmony_ci * Its use is an optimization only, we can't rely on this context
6162306a36Sopenharmony_ci * number to be free, but it usually will be.
6262306a36Sopenharmony_ci */
6362306a36Sopenharmony_ciextern mm_context_t next_mmu_context;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/*
6662306a36Sopenharmony_ci * Since we don't have sufficient contexts to give one to every task
6762306a36Sopenharmony_ci * that could be in the system, we need to be able to steal contexts.
6862306a36Sopenharmony_ci * These variables support that.
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_ciextern atomic_t nr_free_contexts;
7162306a36Sopenharmony_ciextern struct mm_struct *context_mm[LAST_CONTEXT+1];
7262306a36Sopenharmony_ciextern void steal_context(void);
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/*
7562306a36Sopenharmony_ci * Get a new mmu context for the address space described by `mm'.
7662306a36Sopenharmony_ci */
7762306a36Sopenharmony_cistatic inline void get_mmu_context(struct mm_struct *mm)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	mm_context_t ctx;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	if (mm->context != NO_CONTEXT)
8262306a36Sopenharmony_ci		return;
8362306a36Sopenharmony_ci	while (atomic_dec_if_positive(&nr_free_contexts) < 0)
8462306a36Sopenharmony_ci		steal_context();
8562306a36Sopenharmony_ci	ctx = next_mmu_context;
8662306a36Sopenharmony_ci	while (test_and_set_bit(ctx, context_map)) {
8762306a36Sopenharmony_ci		ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
8862306a36Sopenharmony_ci		if (ctx > LAST_CONTEXT)
8962306a36Sopenharmony_ci			ctx = 0;
9062306a36Sopenharmony_ci	}
9162306a36Sopenharmony_ci	next_mmu_context = (ctx + 1) & LAST_CONTEXT;
9262306a36Sopenharmony_ci	mm->context = ctx;
9362306a36Sopenharmony_ci	context_mm[ctx] = mm;
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci/*
9762306a36Sopenharmony_ci * Set up the context for a new address space.
9862306a36Sopenharmony_ci */
9962306a36Sopenharmony_ci# define init_new_context(tsk, mm)	(((mm)->context = NO_CONTEXT), 0)
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/*
10262306a36Sopenharmony_ci * We're finished using the context for an address space.
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_ci#define destroy_context destroy_context
10562306a36Sopenharmony_cistatic inline void destroy_context(struct mm_struct *mm)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	if (mm->context != NO_CONTEXT) {
10862306a36Sopenharmony_ci		clear_bit(mm->context, context_map);
10962306a36Sopenharmony_ci		mm->context = NO_CONTEXT;
11062306a36Sopenharmony_ci		atomic_inc(&nr_free_contexts);
11162306a36Sopenharmony_ci	}
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
11562306a36Sopenharmony_ci			     struct task_struct *tsk)
11662306a36Sopenharmony_ci{
11762306a36Sopenharmony_ci	tsk->thread.pgdir = next->pgd;
11862306a36Sopenharmony_ci	get_mmu_context(next);
11962306a36Sopenharmony_ci	set_context(next->context, next->pgd);
12062306a36Sopenharmony_ci}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci/*
12362306a36Sopenharmony_ci * After we have set current->mm to a new value, this activates
12462306a36Sopenharmony_ci * the context for the new mm so we see the new mappings.
12562306a36Sopenharmony_ci */
12662306a36Sopenharmony_ci#define activate_mm activate_mm
12762306a36Sopenharmony_cistatic inline void activate_mm(struct mm_struct *active_mm,
12862306a36Sopenharmony_ci			struct mm_struct *mm)
12962306a36Sopenharmony_ci{
13062306a36Sopenharmony_ci	current->thread.pgdir = mm->pgd;
13162306a36Sopenharmony_ci	get_mmu_context(mm);
13262306a36Sopenharmony_ci	set_context(mm->context, mm->pgd);
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciextern void mmu_context_init(void);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#include <asm-generic/mmu_context.h>
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci# endif /* __KERNEL__ */
14062306a36Sopenharmony_ci#endif /* _ASM_MICROBLAZE_MMU_CONTEXT_H */
141