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