context.c 3.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
R
Russell King 已提交
2
 *  linux/arch/arm/mm/context.c
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12
 *
 *  Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/mm.h>
13 14
#include <linux/smp.h>
#include <linux/percpu.h>
L
Linus Torvalds 已提交
15 16 17 18

#include <asm/mmu_context.h>
#include <asm/tlbflush.h>

19
static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
R
Russell King 已提交
20
unsigned int cpu_last_asid = ASID_FIRST_VERSION;
L
Linus Torvalds 已提交
21

22
#ifdef CONFIG_ARM_LPAE
23
void cpu_set_reserved_ttbr0(void)
24 25 26 27 28 29 30 31 32 33 34 35 36
{
	unsigned long ttbl = __pa(swapper_pg_dir);
	unsigned long ttbh = 0;

	/*
	 * Set TTBR0 to swapper_pg_dir which contains only global entries. The
	 * ASID is set to 0.
	 */
	asm volatile(
	"	mcrr	p15, 0, %0, %1, c2		@ set TTBR0\n"
	:
	: "r" (ttbl), "r" (ttbh));
	isb();
37 38
}
#else
39
void cpu_set_reserved_ttbr0(void)
40 41 42 43 44 45 46 47 48
{
	u32 ttb;
	/* Copy TTBR1 into TTBR0 */
	asm volatile(
	"	mrc	p15, 0, %0, c2, c0, 1		@ read TTBR1\n"
	"	mcr	p15, 0, %0, c2, c0, 0		@ set TTBR0\n"
	: "=r" (ttb));
	isb();
}
49 50
#endif

L
Linus Torvalds 已提交
51 52
/*
 * We fork()ed a process, and we need a new context for the child
53
 * to run in.
L
Linus Torvalds 已提交
54 55 56 57
 */
void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
{
	mm->context.id = 0;
58
	raw_spin_lock_init(&mm->context.id_lock);
L
Linus Torvalds 已提交
59 60
}

61 62
static void flush_context(void)
{
63
	cpu_set_reserved_ttbr0();
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	local_flush_tlb_all();
	if (icache_is_vivt_asid_tagged()) {
		__flush_icache_all();
		dsb();
	}
}

#ifdef CONFIG_SMP

static void set_mm_context(struct mm_struct *mm, unsigned int asid)
{
	unsigned long flags;

	/*
	 * Locking needed for multi-threaded applications where the
	 * same mm->context.id could be set from different CPUs during
	 * the broadcast. This function is also called via IPI so the
	 * mm->context.id_lock has to be IRQ-safe.
	 */
83
	raw_spin_lock_irqsave(&mm->context.id_lock, flags);
84 85 86 87 88 89 90 91
	if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
		/*
		 * Old version of ASID found. Set the new one and
		 * reset mm_cpumask(mm).
		 */
		mm->context.id = asid;
		cpumask_clear(mm_cpumask(mm));
	}
92
	raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

	/*
	 * Set the mm_cpumask(mm) bit for the current CPU.
	 */
	cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
}

/*
 * Reset the ASID on the current CPU. This function call is broadcast
 * from the CPU handling the ASID rollover and holding cpu_asid_lock.
 */
static void reset_context(void *info)
{
	unsigned int asid;
	unsigned int cpu = smp_processor_id();
108
	struct mm_struct *mm = current->active_mm;
109 110

	smp_rmb();
111
	asid = cpu_last_asid + cpu + 1;
112 113 114 115 116

	flush_context();
	set_mm_context(mm, asid);

	/* set the new ASID */
117
	cpu_switch_mm(mm->pgd, mm);
118 119 120 121 122 123 124 125 126 127 128 129
}

#else

static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
{
	mm->context.id = asid;
	cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
}

#endif

L
Linus Torvalds 已提交
130 131 132 133
void __new_context(struct mm_struct *mm)
{
	unsigned int asid;

134
	raw_spin_lock(&cpu_asid_lock);
135 136 137 138 139 140 141
#ifdef CONFIG_SMP
	/*
	 * Check the ASID again, in case the change was broadcast from
	 * another CPU before we acquired the lock.
	 */
	if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
142
		raw_spin_unlock(&cpu_asid_lock);
143 144 145 146 147 148 149 150
		return;
	}
#endif
	/*
	 * At this point, it is guaranteed that the current mm (with
	 * an old ASID) isn't active on any other CPU since the ASIDs
	 * are changed simultaneously via IPI.
	 */
L
Linus Torvalds 已提交
151 152
	asid = ++cpu_last_asid;
	if (asid == 0)
R
Russell King 已提交
153
		asid = cpu_last_asid = ASID_FIRST_VERSION;
L
Linus Torvalds 已提交
154 155 156 157 158

	/*
	 * If we've used up all our ASIDs, we need
	 * to start a new version and flush the TLB.
	 */
R
Russell King 已提交
159
	if (unlikely((asid & ~ASID_MASK) == 0)) {
160
		asid = cpu_last_asid + smp_processor_id() + 1;
161 162 163 164 165
		flush_context();
#ifdef CONFIG_SMP
		smp_wmb();
		smp_call_function(reset_context, NULL, 1);
#endif
166
		cpu_last_asid += NR_CPUS;
167
	}
L
Linus Torvalds 已提交
168

169
	set_mm_context(mm, asid);
170
	raw_spin_unlock(&cpu_asid_lock);
L
Linus Torvalds 已提交
171
}