xref: /openbmc/linux/arch/ia64/include/asm/mmu_context.h (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
27f30491cSTony Luck #ifndef _ASM_IA64_MMU_CONTEXT_H
37f30491cSTony Luck #define _ASM_IA64_MMU_CONTEXT_H
47f30491cSTony Luck 
57f30491cSTony Luck /*
67f30491cSTony Luck  * Copyright (C) 1998-2002 Hewlett-Packard Co
77f30491cSTony Luck  *	David Mosberger-Tang <davidm@hpl.hp.com>
87f30491cSTony Luck  */
97f30491cSTony Luck 
107f30491cSTony Luck /*
117f30491cSTony Luck  * Routines to manage the allocation of task context numbers.  Task context
127f30491cSTony Luck  * numbers are used to reduce or eliminate the need to perform TLB flushes
137f30491cSTony Luck  * due to context switches.  Context numbers are implemented using ia-64
147f30491cSTony Luck  * region ids.  Since the IA-64 TLB does not consider the region number when
157f30491cSTony Luck  * performing a TLB lookup, we need to assign a unique region id to each
167f30491cSTony Luck  * region in a process.  We use the least significant three bits in aregion
177f30491cSTony Luck  * id for this purpose.
187f30491cSTony Luck  */
197f30491cSTony Luck 
207f30491cSTony Luck #define IA64_REGION_ID_KERNEL	0 /* the kernel's region id (tlb.c depends on this being 0) */
217f30491cSTony Luck 
227f30491cSTony Luck #define ia64_rid(ctx,addr)	(((ctx) << 3) | (addr >> 61))
237f30491cSTony Luck 
247f30491cSTony Luck # include <asm/page.h>
257f30491cSTony Luck # ifndef __ASSEMBLY__
267f30491cSTony Luck 
277f30491cSTony Luck #include <linux/compiler.h>
287f30491cSTony Luck #include <linux/percpu.h>
297f30491cSTony Luck #include <linux/sched.h>
30589ee628SIngo Molnar #include <linux/mm_types.h>
317f30491cSTony Luck #include <linux/spinlock.h>
327f30491cSTony Luck 
337f30491cSTony Luck #include <asm/processor.h>
347f30491cSTony Luck #include <asm-generic/mm_hooks.h>
357f30491cSTony Luck 
367f30491cSTony Luck struct ia64_ctx {
377f30491cSTony Luck 	spinlock_t lock;
387f30491cSTony Luck 	unsigned int next;	/* next context number to use */
397f30491cSTony Luck 	unsigned int limit;     /* available free range */
407f30491cSTony Luck 	unsigned int max_ctx;   /* max. context value supported by all CPUs */
417f30491cSTony Luck 				/* call wrap_mmu_context when next >= max */
427f30491cSTony Luck 	unsigned long *bitmap;  /* bitmap size is max_ctx+1 */
437f30491cSTony Luck 	unsigned long *flushmap;/* pending rid to be flushed */
447f30491cSTony Luck };
457f30491cSTony Luck 
467f30491cSTony Luck extern struct ia64_ctx ia64_ctx;
477f30491cSTony Luck DECLARE_PER_CPU(u8, ia64_need_tlb_flush);
487f30491cSTony Luck 
497f30491cSTony Luck extern void mmu_context_init (void);
507f30491cSTony Luck extern void wrap_mmu_context (struct mm_struct *mm);
517f30491cSTony Luck 
527f30491cSTony Luck static inline void
537f30491cSTony Luck enter_lazy_tlb (struct mm_struct *mm, struct task_struct *tsk)
547f30491cSTony Luck {
557f30491cSTony Luck }
567f30491cSTony Luck 
577f30491cSTony Luck /*
587f30491cSTony Luck  * When the context counter wraps around all TLBs need to be flushed because
597f30491cSTony Luck  * an old context number might have been reused. This is signalled by the
607f30491cSTony Luck  * ia64_need_tlb_flush per-CPU variable, which is checked in the routine
617f30491cSTony Luck  * below. Called by activate_mm(). <efocht@ess.nec.de>
627f30491cSTony Luck  */
637f30491cSTony Luck static inline void
647f30491cSTony Luck delayed_tlb_flush (void)
657f30491cSTony Luck {
667f30491cSTony Luck 	extern void local_flush_tlb_all (void);
677f30491cSTony Luck 	unsigned long flags;
687f30491cSTony Luck 
697f30491cSTony Luck 	if (unlikely(__ia64_per_cpu_var(ia64_need_tlb_flush))) {
707f30491cSTony Luck 		spin_lock_irqsave(&ia64_ctx.lock, flags);
717f30491cSTony Luck 		if (__ia64_per_cpu_var(ia64_need_tlb_flush)) {
727f30491cSTony Luck 			local_flush_tlb_all();
737f30491cSTony Luck 			__ia64_per_cpu_var(ia64_need_tlb_flush) = 0;
747f30491cSTony Luck 		}
757f30491cSTony Luck 		spin_unlock_irqrestore(&ia64_ctx.lock, flags);
767f30491cSTony Luck 	}
777f30491cSTony Luck }
787f30491cSTony Luck 
797f30491cSTony Luck static inline nv_mm_context_t
807f30491cSTony Luck get_mmu_context (struct mm_struct *mm)
817f30491cSTony Luck {
827f30491cSTony Luck 	unsigned long flags;
837f30491cSTony Luck 	nv_mm_context_t context = mm->context;
847f30491cSTony Luck 
857f30491cSTony Luck 	if (likely(context))
867f30491cSTony Luck 		goto out;
877f30491cSTony Luck 
887f30491cSTony Luck 	spin_lock_irqsave(&ia64_ctx.lock, flags);
897f30491cSTony Luck 	/* re-check, now that we've got the lock: */
907f30491cSTony Luck 	context = mm->context;
917f30491cSTony Luck 	if (context == 0) {
925d8c39f6SRusty Russell 		cpumask_clear(mm_cpumask(mm));
937f30491cSTony Luck 		if (ia64_ctx.next >= ia64_ctx.limit) {
947f30491cSTony Luck 			ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap,
957f30491cSTony Luck 					ia64_ctx.max_ctx, ia64_ctx.next);
967f30491cSTony Luck 			ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap,
977f30491cSTony Luck 					ia64_ctx.max_ctx, ia64_ctx.next);
987f30491cSTony Luck 			if (ia64_ctx.next >= ia64_ctx.max_ctx)
997f30491cSTony Luck 				wrap_mmu_context(mm);
1007f30491cSTony Luck 		}
1017f30491cSTony Luck 		mm->context = context = ia64_ctx.next++;
1027f30491cSTony Luck 		__set_bit(context, ia64_ctx.bitmap);
1037f30491cSTony Luck 	}
1047f30491cSTony Luck 	spin_unlock_irqrestore(&ia64_ctx.lock, flags);
1057f30491cSTony Luck out:
1067f30491cSTony Luck 	/*
1077f30491cSTony Luck 	 * Ensure we're not starting to use "context" before any old
1087f30491cSTony Luck 	 * uses of it are gone from our TLB.
1097f30491cSTony Luck 	 */
1107f30491cSTony Luck 	delayed_tlb_flush();
1117f30491cSTony Luck 
1127f30491cSTony Luck 	return context;
1137f30491cSTony Luck }
1147f30491cSTony Luck 
1157f30491cSTony Luck /*
1167f30491cSTony Luck  * Initialize context number to some sane value.  MM is guaranteed to be a
1177f30491cSTony Luck  * brand-new address-space, so no TLB flushing is needed, ever.
1187f30491cSTony Luck  */
1197f30491cSTony Luck static inline int
1207f30491cSTony Luck init_new_context (struct task_struct *p, struct mm_struct *mm)
1217f30491cSTony Luck {
1227f30491cSTony Luck 	mm->context = 0;
1237f30491cSTony Luck 	return 0;
1247f30491cSTony Luck }
1257f30491cSTony Luck 
1267f30491cSTony Luck static inline void
1277f30491cSTony Luck destroy_context (struct mm_struct *mm)
1287f30491cSTony Luck {
1297f30491cSTony Luck 	/* Nothing to do.  */
1307f30491cSTony Luck }
1317f30491cSTony Luck 
1327f30491cSTony Luck static inline void
1337f30491cSTony Luck reload_context (nv_mm_context_t context)
1347f30491cSTony Luck {
1357f30491cSTony Luck 	unsigned long rid;
1367f30491cSTony Luck 	unsigned long rid_incr = 0;
1377f30491cSTony Luck 	unsigned long rr0, rr1, rr2, rr3, rr4, old_rr4;
1387f30491cSTony Luck 
1397f30491cSTony Luck 	old_rr4 = ia64_get_rr(RGN_BASE(RGN_HPAGE));
1407f30491cSTony Luck 	rid = context << 3;	/* make space for encoding the region number */
1417f30491cSTony Luck 	rid_incr = 1 << 8;
1427f30491cSTony Luck 
1437f30491cSTony Luck 	/* encode the region id, preferred page size, and VHPT enable bit: */
1447f30491cSTony Luck 	rr0 = (rid << 8) | (PAGE_SHIFT << 2) | 1;
1457f30491cSTony Luck 	rr1 = rr0 + 1*rid_incr;
1467f30491cSTony Luck 	rr2 = rr0 + 2*rid_incr;
1477f30491cSTony Luck 	rr3 = rr0 + 3*rid_incr;
1487f30491cSTony Luck 	rr4 = rr0 + 4*rid_incr;
1497f30491cSTony Luck #ifdef  CONFIG_HUGETLB_PAGE
1507f30491cSTony Luck 	rr4 = (rr4 & (~(0xfcUL))) | (old_rr4 & 0xfc);
1517f30491cSTony Luck 
1527f30491cSTony Luck #  if RGN_HPAGE != 4
1537f30491cSTony Luck #    error "reload_context assumes RGN_HPAGE is 4"
1547f30491cSTony Luck #  endif
1557f30491cSTony Luck #endif
1567f30491cSTony Luck 
1577f30491cSTony Luck 	ia64_set_rr0_to_rr4(rr0, rr1, rr2, rr3, rr4);
1587f30491cSTony Luck 	ia64_srlz_i();			/* srlz.i implies srlz.d */
1597f30491cSTony Luck }
1607f30491cSTony Luck 
1617f30491cSTony Luck /*
1627f30491cSTony Luck  * Must be called with preemption off
1637f30491cSTony Luck  */
1647f30491cSTony Luck static inline void
1657f30491cSTony Luck activate_context (struct mm_struct *mm)
1667f30491cSTony Luck {
1677f30491cSTony Luck 	nv_mm_context_t context;
1687f30491cSTony Luck 
1697f30491cSTony Luck 	do {
1707f30491cSTony Luck 		context = get_mmu_context(mm);
1715d8c39f6SRusty Russell 		if (!cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
1725d8c39f6SRusty Russell 			cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
1737f30491cSTony Luck 		reload_context(context);
1747f30491cSTony Luck 		/*
1757f30491cSTony Luck 		 * in the unlikely event of a TLB-flush by another thread,
1767f30491cSTony Luck 		 * redo the load.
1777f30491cSTony Luck 		 */
1787f30491cSTony Luck 	} while (unlikely(context != mm->context));
1797f30491cSTony Luck }
1807f30491cSTony Luck 
1817f30491cSTony Luck #define deactivate_mm(tsk,mm)	do { } while (0)
1827f30491cSTony Luck 
1837f30491cSTony Luck /*
1847f30491cSTony Luck  * Switch from address space PREV to address space NEXT.
1857f30491cSTony Luck  */
1867f30491cSTony Luck static inline void
1877f30491cSTony Luck activate_mm (struct mm_struct *prev, struct mm_struct *next)
1887f30491cSTony Luck {
1897f30491cSTony Luck 	/*
1907f30491cSTony Luck 	 * We may get interrupts here, but that's OK because interrupt
1917f30491cSTony Luck 	 * handlers cannot touch user-space.
1927f30491cSTony Luck 	 */
1937f30491cSTony Luck 	ia64_set_kr(IA64_KR_PT_BASE, __pa(next->pgd));
1947f30491cSTony Luck 	activate_context(next);
1957f30491cSTony Luck }
1967f30491cSTony Luck 
1977f30491cSTony Luck #define switch_mm(prev_mm,next_mm,next_task)	activate_mm(prev_mm, next_mm)
1987f30491cSTony Luck 
1997f30491cSTony Luck # endif /* ! __ASSEMBLY__ */
2007f30491cSTony Luck #endif /* _ASM_IA64_MMU_CONTEXT_H */
201