xref: /openbmc/linux/arch/ia64/include/asm/mmu_context.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg 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 /*
537f30491cSTony Luck  * When the context counter wraps around all TLBs need to be flushed because
547f30491cSTony Luck  * an old context number might have been reused. This is signalled by the
557f30491cSTony Luck  * ia64_need_tlb_flush per-CPU variable, which is checked in the routine
567f30491cSTony Luck  * below. Called by activate_mm(). <efocht@ess.nec.de>
577f30491cSTony Luck  */
587f30491cSTony Luck static inline void
delayed_tlb_flush(void)597f30491cSTony Luck delayed_tlb_flush (void)
607f30491cSTony Luck {
617f30491cSTony Luck 	extern void local_flush_tlb_all (void);
627f30491cSTony Luck 	unsigned long flags;
637f30491cSTony Luck 
647f30491cSTony Luck 	if (unlikely(__ia64_per_cpu_var(ia64_need_tlb_flush))) {
657f30491cSTony Luck 		spin_lock_irqsave(&ia64_ctx.lock, flags);
667f30491cSTony Luck 		if (__ia64_per_cpu_var(ia64_need_tlb_flush)) {
677f30491cSTony Luck 			local_flush_tlb_all();
687f30491cSTony Luck 			__ia64_per_cpu_var(ia64_need_tlb_flush) = 0;
697f30491cSTony Luck 		}
707f30491cSTony Luck 		spin_unlock_irqrestore(&ia64_ctx.lock, flags);
717f30491cSTony Luck 	}
727f30491cSTony Luck }
737f30491cSTony Luck 
747f30491cSTony Luck static inline nv_mm_context_t
get_mmu_context(struct mm_struct * mm)757f30491cSTony Luck get_mmu_context (struct mm_struct *mm)
767f30491cSTony Luck {
777f30491cSTony Luck 	unsigned long flags;
787f30491cSTony Luck 	nv_mm_context_t context = mm->context;
797f30491cSTony Luck 
807f30491cSTony Luck 	if (likely(context))
817f30491cSTony Luck 		goto out;
827f30491cSTony Luck 
837f30491cSTony Luck 	spin_lock_irqsave(&ia64_ctx.lock, flags);
847f30491cSTony Luck 	/* re-check, now that we've got the lock: */
857f30491cSTony Luck 	context = mm->context;
867f30491cSTony Luck 	if (context == 0) {
875d8c39f6SRusty Russell 		cpumask_clear(mm_cpumask(mm));
887f30491cSTony Luck 		if (ia64_ctx.next >= ia64_ctx.limit) {
897f30491cSTony Luck 			ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap,
907f30491cSTony Luck 					ia64_ctx.max_ctx, ia64_ctx.next);
917f30491cSTony Luck 			ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap,
927f30491cSTony Luck 					ia64_ctx.max_ctx, ia64_ctx.next);
937f30491cSTony Luck 			if (ia64_ctx.next >= ia64_ctx.max_ctx)
947f30491cSTony Luck 				wrap_mmu_context(mm);
957f30491cSTony Luck 		}
967f30491cSTony Luck 		mm->context = context = ia64_ctx.next++;
977f30491cSTony Luck 		__set_bit(context, ia64_ctx.bitmap);
987f30491cSTony Luck 	}
997f30491cSTony Luck 	spin_unlock_irqrestore(&ia64_ctx.lock, flags);
1007f30491cSTony Luck out:
1017f30491cSTony Luck 	/*
1027f30491cSTony Luck 	 * Ensure we're not starting to use "context" before any old
1037f30491cSTony Luck 	 * uses of it are gone from our TLB.
1047f30491cSTony Luck 	 */
1057f30491cSTony Luck 	delayed_tlb_flush();
1067f30491cSTony Luck 
1077f30491cSTony Luck 	return context;
1087f30491cSTony Luck }
1097f30491cSTony Luck 
1107f30491cSTony Luck /*
1117f30491cSTony Luck  * Initialize context number to some sane value.  MM is guaranteed to be a
1127f30491cSTony Luck  * brand-new address-space, so no TLB flushing is needed, ever.
1137f30491cSTony Luck  */
1148e51efc2SNicholas Piggin #define init_new_context init_new_context
1157f30491cSTony Luck static inline int
init_new_context(struct task_struct * p,struct mm_struct * mm)1167f30491cSTony Luck init_new_context (struct task_struct *p, struct mm_struct *mm)
1177f30491cSTony Luck {
1187f30491cSTony Luck 	mm->context = 0;
1197f30491cSTony Luck 	return 0;
1207f30491cSTony Luck }
1217f30491cSTony Luck 
1227f30491cSTony Luck static inline void
reload_context(nv_mm_context_t context)1237f30491cSTony Luck reload_context (nv_mm_context_t context)
1247f30491cSTony Luck {
1257f30491cSTony Luck 	unsigned long rid;
1267f30491cSTony Luck 	unsigned long rid_incr = 0;
127*1298f83bSSouptick Joarder (HPE) 	unsigned long rr0, rr1, rr2, rr3, rr4;
1287f30491cSTony Luck 
129*1298f83bSSouptick Joarder (HPE) #ifdef CONFIG_HUGETLB_PAGE
130*1298f83bSSouptick Joarder (HPE) 	unsigned long old_rr4;
1317f30491cSTony Luck 	old_rr4 = ia64_get_rr(RGN_BASE(RGN_HPAGE));
132*1298f83bSSouptick Joarder (HPE) #endif
1337f30491cSTony Luck 	rid = context << 3;	/* make space for encoding the region number */
1347f30491cSTony Luck 	rid_incr = 1 << 8;
1357f30491cSTony Luck 
1367f30491cSTony Luck 	/* encode the region id, preferred page size, and VHPT enable bit: */
1377f30491cSTony Luck 	rr0 = (rid << 8) | (PAGE_SHIFT << 2) | 1;
1387f30491cSTony Luck 	rr1 = rr0 + 1*rid_incr;
1397f30491cSTony Luck 	rr2 = rr0 + 2*rid_incr;
1407f30491cSTony Luck 	rr3 = rr0 + 3*rid_incr;
1417f30491cSTony Luck 	rr4 = rr0 + 4*rid_incr;
1427f30491cSTony Luck #ifdef  CONFIG_HUGETLB_PAGE
1437f30491cSTony Luck 	rr4 = (rr4 & (~(0xfcUL))) | (old_rr4 & 0xfc);
1447f30491cSTony Luck 
1457f30491cSTony Luck #  if RGN_HPAGE != 4
1467f30491cSTony Luck #    error "reload_context assumes RGN_HPAGE is 4"
1477f30491cSTony Luck #  endif
1487f30491cSTony Luck #endif
1497f30491cSTony Luck 
1507f30491cSTony Luck 	ia64_set_rr0_to_rr4(rr0, rr1, rr2, rr3, rr4);
1517f30491cSTony Luck 	ia64_srlz_i();			/* srlz.i implies srlz.d */
1527f30491cSTony Luck }
1537f30491cSTony Luck 
1547f30491cSTony Luck /*
1557f30491cSTony Luck  * Must be called with preemption off
1567f30491cSTony Luck  */
1577f30491cSTony Luck static inline void
activate_context(struct mm_struct * mm)1587f30491cSTony Luck activate_context (struct mm_struct *mm)
1597f30491cSTony Luck {
1607f30491cSTony Luck 	nv_mm_context_t context;
1617f30491cSTony Luck 
1627f30491cSTony Luck 	do {
1637f30491cSTony Luck 		context = get_mmu_context(mm);
1645d8c39f6SRusty Russell 		if (!cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
1655d8c39f6SRusty Russell 			cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
1667f30491cSTony Luck 		reload_context(context);
1677f30491cSTony Luck 		/*
1687f30491cSTony Luck 		 * in the unlikely event of a TLB-flush by another thread,
1697f30491cSTony Luck 		 * redo the load.
1707f30491cSTony Luck 		 */
1717f30491cSTony Luck 	} while (unlikely(context != mm->context));
1727f30491cSTony Luck }
1737f30491cSTony Luck 
1747f30491cSTony Luck /*
1757f30491cSTony Luck  * Switch from address space PREV to address space NEXT.
1767f30491cSTony Luck  */
1778e51efc2SNicholas Piggin #define activate_mm activate_mm
1787f30491cSTony Luck static inline void
activate_mm(struct mm_struct * prev,struct mm_struct * next)1797f30491cSTony Luck activate_mm (struct mm_struct *prev, struct mm_struct *next)
1807f30491cSTony Luck {
1817f30491cSTony Luck 	/*
1827f30491cSTony Luck 	 * We may get interrupts here, but that's OK because interrupt
1837f30491cSTony Luck 	 * handlers cannot touch user-space.
1847f30491cSTony Luck 	 */
1857f30491cSTony Luck 	ia64_set_kr(IA64_KR_PT_BASE, __pa(next->pgd));
1867f30491cSTony Luck 	activate_context(next);
1877f30491cSTony Luck }
1887f30491cSTony Luck 
1897f30491cSTony Luck #define switch_mm(prev_mm,next_mm,next_task)	activate_mm(prev_mm, next_mm)
1907f30491cSTony Luck 
1918e51efc2SNicholas Piggin #include <asm-generic/mmu_context.h>
1928e51efc2SNicholas Piggin 
1937f30491cSTony Luck # endif /* ! __ASSEMBLY__ */
1947f30491cSTony Luck #endif /* _ASM_IA64_MMU_CONTEXT_H */
195