18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 1999 Niibe Yutaka
48c2ecf20Sopenharmony_ci * Copyright (C) 2003 - 2007 Paul Mundt
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * ASID handling idea taken from MIPS implementation.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#ifndef __ASM_SH_MMU_CONTEXT_H
98c2ecf20Sopenharmony_ci#define __ASM_SH_MMU_CONTEXT_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <cpu/mmu_context.h>
128c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
138c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
148c2ecf20Sopenharmony_ci#include <linux/mm_types.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <asm/io.h>
178c2ecf20Sopenharmony_ci#include <asm-generic/mm_hooks.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * The MMU "context" consists of two things:
218c2ecf20Sopenharmony_ci *    (a) TLB cache version (or round, cycle whatever expression you like)
228c2ecf20Sopenharmony_ci *    (b) ASID (Address Space IDentifier)
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_HAS_PTEAEX
258c2ecf20Sopenharmony_ci#define MMU_CONTEXT_ASID_MASK		0x0000ffff
268c2ecf20Sopenharmony_ci#else
278c2ecf20Sopenharmony_ci#define MMU_CONTEXT_ASID_MASK		0x000000ff
288c2ecf20Sopenharmony_ci#endif
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define MMU_CONTEXT_VERSION_MASK	(~0UL & ~MMU_CONTEXT_ASID_MASK)
318c2ecf20Sopenharmony_ci#define MMU_CONTEXT_FIRST_VERSION	(MMU_CONTEXT_ASID_MASK + 1)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Impossible ASID value, to differentiate from NO_CONTEXT. */
348c2ecf20Sopenharmony_ci#define MMU_NO_ASID			MMU_CONTEXT_FIRST_VERSION
358c2ecf20Sopenharmony_ci#define NO_CONTEXT			0UL
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define asid_cache(cpu)		(cpu_data[cpu].asid_cache)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
408c2ecf20Sopenharmony_ci#define cpu_context(cpu, mm)	((mm)->context.id[cpu])
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define cpu_asid(cpu, mm)	\
438c2ecf20Sopenharmony_ci	(cpu_context((cpu), (mm)) & MMU_CONTEXT_ASID_MASK)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*
468c2ecf20Sopenharmony_ci * Virtual Page Number mask
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci#define MMU_VPN_MASK	0xfffff000
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#include <asm/mmu_context_32.h>
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * Get MMU context if needed.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_cistatic inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	unsigned long asid = asid_cache(cpu);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/* Check if we have old version of context. */
608c2ecf20Sopenharmony_ci	if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0)
618c2ecf20Sopenharmony_ci		/* It's up to date, do nothing */
628c2ecf20Sopenharmony_ci		return;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* It's old, we need to get new context with new version. */
658c2ecf20Sopenharmony_ci	if (!(++asid & MMU_CONTEXT_ASID_MASK)) {
668c2ecf20Sopenharmony_ci		/*
678c2ecf20Sopenharmony_ci		 * We exhaust ASID of this version.
688c2ecf20Sopenharmony_ci		 * Flush all TLB and start new cycle.
698c2ecf20Sopenharmony_ci		 */
708c2ecf20Sopenharmony_ci		local_flush_tlb_all();
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci		/*
738c2ecf20Sopenharmony_ci		 * Fix version; Note that we avoid version #0
748c2ecf20Sopenharmony_ci		 * to distinguish NO_CONTEXT.
758c2ecf20Sopenharmony_ci		 */
768c2ecf20Sopenharmony_ci		if (!asid)
778c2ecf20Sopenharmony_ci			asid = MMU_CONTEXT_FIRST_VERSION;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	cpu_context(cpu, mm) = asid_cache(cpu) = asid;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/*
848c2ecf20Sopenharmony_ci * Initialize the context related info for a new mm_struct
858c2ecf20Sopenharmony_ci * instance.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_cistatic inline int init_new_context(struct task_struct *tsk,
888c2ecf20Sopenharmony_ci				   struct mm_struct *mm)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	int i;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	for_each_online_cpu(i)
938c2ecf20Sopenharmony_ci		cpu_context(i, mm) = NO_CONTEXT;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/*
998c2ecf20Sopenharmony_ci * After we have set current->mm to a new value, this activates
1008c2ecf20Sopenharmony_ci * the context for the new mm so we see the new mappings.
1018c2ecf20Sopenharmony_ci */
1028c2ecf20Sopenharmony_cistatic inline void activate_context(struct mm_struct *mm, unsigned int cpu)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	get_mmu_context(mm, cpu);
1058c2ecf20Sopenharmony_ci	set_asid(cpu_asid(cpu, mm));
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline void switch_mm(struct mm_struct *prev,
1098c2ecf20Sopenharmony_ci			     struct mm_struct *next,
1108c2ecf20Sopenharmony_ci			     struct task_struct *tsk)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	unsigned int cpu = smp_processor_id();
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if (likely(prev != next)) {
1158c2ecf20Sopenharmony_ci		cpumask_set_cpu(cpu, mm_cpumask(next));
1168c2ecf20Sopenharmony_ci		set_TTB(next->pgd);
1178c2ecf20Sopenharmony_ci		activate_context(next, cpu);
1188c2ecf20Sopenharmony_ci	} else
1198c2ecf20Sopenharmony_ci		if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)))
1208c2ecf20Sopenharmony_ci			activate_context(next, cpu);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define activate_mm(prev, next)		switch_mm((prev),(next),NULL)
1248c2ecf20Sopenharmony_ci#define deactivate_mm(tsk,mm)		do { } while (0)
1258c2ecf20Sopenharmony_ci#define enter_lazy_tlb(mm,tsk)		do { } while (0)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#else
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define set_asid(asid)			do { } while (0)
1308c2ecf20Sopenharmony_ci#define get_asid()			(0)
1318c2ecf20Sopenharmony_ci#define cpu_asid(cpu, mm)		({ (void)cpu; NO_CONTEXT; })
1328c2ecf20Sopenharmony_ci#define switch_and_save_asid(asid)	(0)
1338c2ecf20Sopenharmony_ci#define set_TTB(pgd)			do { } while (0)
1348c2ecf20Sopenharmony_ci#define get_TTB()			(0)
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#include <asm-generic/mmu_context.h>
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#endif /* CONFIG_MMU */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
1418c2ecf20Sopenharmony_ci/*
1428c2ecf20Sopenharmony_ci * If this processor has an MMU, we need methods to turn it off/on ..
1438c2ecf20Sopenharmony_ci * paging_init() will also have to be updated for the processor in
1448c2ecf20Sopenharmony_ci * question.
1458c2ecf20Sopenharmony_ci */
1468c2ecf20Sopenharmony_cistatic inline void enable_mmu(void)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	unsigned int cpu = smp_processor_id();
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/* Enable MMU */
1518c2ecf20Sopenharmony_ci	__raw_writel(MMU_CONTROL_INIT, MMUCR);
1528c2ecf20Sopenharmony_ci	ctrl_barrier();
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (asid_cache(cpu) == NO_CONTEXT)
1558c2ecf20Sopenharmony_ci		asid_cache(cpu) = MMU_CONTEXT_FIRST_VERSION;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	set_asid(asid_cache(cpu) & MMU_CONTEXT_ASID_MASK);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic inline void disable_mmu(void)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	unsigned long cr;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	cr = __raw_readl(MMUCR);
1658c2ecf20Sopenharmony_ci	cr &= ~MMU_CONTROL_INIT;
1668c2ecf20Sopenharmony_ci	__raw_writel(cr, MMUCR);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	ctrl_barrier();
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci#else
1718c2ecf20Sopenharmony_ci/*
1728c2ecf20Sopenharmony_ci * MMU control handlers for processors lacking memory
1738c2ecf20Sopenharmony_ci * management hardware.
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_ci#define enable_mmu()	do { } while (0)
1768c2ecf20Sopenharmony_ci#define disable_mmu()	do { } while (0)
1778c2ecf20Sopenharmony_ci#endif
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#endif /* __ASM_SH_MMU_CONTEXT_H */
180