1 /* MN10300 MMU context management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Modified by David Howells (dhowells@redhat.com)
5 * - Derived from include/asm-m32r/mmu_context.h
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
13 * This implements an algorithm to provide TLB PID mappings to provide
14 * selective access to the TLB for processes, thus reducing the number of TLB
17 * Note, however, that the M32R algorithm is technically broken as it does not
18 * handle version wrap-around, and could, theoretically, have a problem with a
19 * very long lived program that sleeps long enough for the version number to
20 * wrap all the way around so that its TLB mappings appear valid once again.
22 #ifndef _ASM_MMU_CONTEXT_H
23 #define _ASM_MMU_CONTEXT_H
25 #include <asm/atomic.h>
26 #include <asm/pgalloc.h>
27 #include <asm/tlbflush.h>
28 #include <asm-generic/mm_hooks.h>
30 #define MMU_CONTEXT_TLBPID_NR 256
31 #define MMU_CONTEXT_TLBPID_MASK 0x000000ffUL
32 #define MMU_CONTEXT_VERSION_MASK 0xffffff00UL
33 #define MMU_CONTEXT_FIRST_VERSION 0x00000100UL
34 #define MMU_NO_CONTEXT 0x00000000UL
35 #define MMU_CONTEXT_TLBPID_LOCK_NR 0
37 #define enter_lazy_tlb(mm, tsk) do {} while (0)
39 static inline void cpu_ran_vm(int cpu
, struct mm_struct
*mm
)
42 cpumask_set_cpu(cpu
, mm_cpumask(mm
));
46 static inline bool cpu_maybe_ran_vm(int cpu
, struct mm_struct
*mm
)
49 return cpumask_test_and_set_cpu(cpu
, mm_cpumask(mm
));
55 #ifdef CONFIG_MN10300_TLB_USE_PIDR
56 extern unsigned long mmu_context_cache
[NR_CPUS
];
57 #define mm_context(mm) (mm->context.tlbpid[smp_processor_id()])
60 * allocate_mmu_context - Allocate storage for the arch-specific MMU data
61 * @mm: The userspace VM context being set up
63 static inline unsigned long allocate_mmu_context(struct mm_struct
*mm
)
65 unsigned long *pmc
= &mmu_context_cache
[smp_processor_id()];
66 unsigned long mc
= ++(*pmc
);
68 if (!(mc
& MMU_CONTEXT_TLBPID_MASK
)) {
69 /* we exhausted the TLB PIDs of this version on this CPU, so we
70 * flush this CPU's TLB in its entirety and start new cycle */
71 local_flush_tlb_all();
73 /* fix the TLB version if needed (we avoid version #0 so as to
74 * distingush MMU_NO_CONTEXT) */
76 *pmc
= mc
= MMU_CONTEXT_FIRST_VERSION
;
83 * get an MMU context if one is needed
85 static inline unsigned long get_mmu_context(struct mm_struct
*mm
)
87 unsigned long mc
= MMU_NO_CONTEXT
, cache
;
90 cache
= mmu_context_cache
[smp_processor_id()];
93 /* if we have an old version of the context, replace it */
94 if ((mc
^ cache
) & MMU_CONTEXT_VERSION_MASK
)
95 mc
= allocate_mmu_context(mm
);
101 * initialise the context related info for a new mm_struct instance
103 static inline int init_new_context(struct task_struct
*tsk
,
104 struct mm_struct
*mm
)
106 int num_cpus
= NR_CPUS
, i
;
108 for (i
= 0; i
< num_cpus
; i
++)
109 mm
->context
.tlbpid
[i
] = MMU_NO_CONTEXT
;
114 * after we have set current->mm to a new value, this activates the context for
115 * the new mm so we see the new mappings.
117 static inline void activate_context(struct mm_struct
*mm
)
119 PIDR
= get_mmu_context(mm
) & MMU_CONTEXT_TLBPID_MASK
;
121 #else /* CONFIG_MN10300_TLB_USE_PIDR */
123 #define init_new_context(tsk, mm) (0)
124 #define activate_context(mm) local_flush_tlb()
126 #endif /* CONFIG_MN10300_TLB_USE_PIDR */
129 * destroy_context - Destroy mm context information
130 * @mm: The MM being destroyed.
132 * Destroy context related info for an mm_struct that is about to be put to
135 #define destroy_context(mm) do {} while (0)
138 * switch_mm - Change between userspace virtual memory contexts
139 * @prev: The outgoing MM context.
140 * @next: The incoming MM context.
141 * @tsk: The incoming task.
143 static inline void switch_mm(struct mm_struct
*prev
, struct mm_struct
*next
,
144 struct task_struct
*tsk
)
146 int cpu
= smp_processor_id();
150 per_cpu(cpu_tlbstate
, cpu
).active_mm
= next
;
152 cpu_ran_vm(cpu
, next
);
153 PTBR
= (unsigned long) next
->pgd
;
154 activate_context(next
);
158 #define deactivate_mm(tsk, mm) do {} while (0)
159 #define activate_mm(prev, next) switch_mm((prev), (next), NULL)
161 #endif /* _ASM_MMU_CONTEXT_H */