2 * native hashtable management.
4 * SMP scalability work:
5 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 #include <linux/spinlock.h>
13 #include <linux/bitops.h>
14 #include <linux/threads.h>
15 #include <linux/smp.h>
17 #include <asm/abs_addr.h>
18 #include <asm/machdep.h>
20 #include <asm/mmu_context.h>
21 #include <asm/pgtable.h>
22 #include <asm/tlbflush.h>
24 #include <asm/cputable.h>
26 #define HPTE_LOCK_BIT 3
28 static DEFINE_SPINLOCK(native_tlbie_lock
);
30 static inline void native_lock_hpte(hpte_t
*hptep
)
32 unsigned long *word
= &hptep
->v
;
35 if (!test_and_set_bit(HPTE_LOCK_BIT
, word
))
37 while(test_bit(HPTE_LOCK_BIT
, word
))
42 static inline void native_unlock_hpte(hpte_t
*hptep
)
44 unsigned long *word
= &hptep
->v
;
46 asm volatile("lwsync":::"memory");
47 clear_bit(HPTE_LOCK_BIT
, word
);
50 long native_hpte_insert(unsigned long hpte_group
, unsigned long va
,
51 unsigned long prpn
, unsigned long vflags
,
54 unsigned long arpn
= physRpn_to_absRpn(prpn
);
55 hpte_t
*hptep
= htab_address
+ hpte_group
;
56 unsigned long hpte_v
, hpte_r
;
59 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
60 if (! (hptep
->v
& HPTE_V_VALID
)) {
61 /* retry with lock held */
62 native_lock_hpte(hptep
);
63 if (! (hptep
->v
& HPTE_V_VALID
))
65 native_unlock_hpte(hptep
);
71 if (i
== HPTES_PER_GROUP
)
74 hpte_v
= (va
>> 23) << HPTE_V_AVPN_SHIFT
| vflags
| HPTE_V_VALID
;
75 if (vflags
& HPTE_V_LARGE
)
76 va
&= ~(1UL << HPTE_V_AVPN_SHIFT
);
77 hpte_r
= (arpn
<< HPTE_R_RPN_SHIFT
) | rflags
;
80 /* Guarantee the second dword is visible before the valid bit */
81 __asm__
__volatile__ ("eieio" : : : "memory");
83 * Now set the first dword including the valid bit
84 * NOTE: this also unlocks the hpte
88 __asm__
__volatile__ ("ptesync" : : : "memory");
90 return i
| (!!(vflags
& HPTE_V_SECONDARY
) << 3);
93 static long native_hpte_remove(unsigned long hpte_group
)
100 /* pick a random entry to start at */
101 slot_offset
= mftb() & 0x7;
103 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
104 hptep
= htab_address
+ hpte_group
+ slot_offset
;
107 if ((hpte_v
& HPTE_V_VALID
) && !(hpte_v
& HPTE_V_BOLTED
)) {
108 /* retry with lock held */
109 native_lock_hpte(hptep
);
111 if ((hpte_v
& HPTE_V_VALID
)
112 && !(hpte_v
& HPTE_V_BOLTED
))
114 native_unlock_hpte(hptep
);
121 if (i
== HPTES_PER_GROUP
)
124 /* Invalidate the hpte. NOTE: this also unlocks it */
130 static inline void set_pp_bit(unsigned long pp
, hpte_t
*addr
)
133 unsigned long *p
= &addr
->r
;
135 __asm__
__volatile__(
140 : "=&r" (old
), "=m" (*p
)
141 : "r" (pp
), "r" (p
), "m" (*p
)
146 * Only works on small pages. Yes its ugly to have to check each slot in
147 * the group but we only use this during bootup.
149 static long native_hpte_find(unsigned long vpn
)
155 unsigned long hpte_v
;
157 hash
= hpt_hash(vpn
, 0);
159 for (j
= 0; j
< 2; j
++) {
160 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
161 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
162 hptep
= htab_address
+ slot
;
165 if ((HPTE_V_AVPN_VAL(hpte_v
) == (vpn
>> 11))
166 && (hpte_v
& HPTE_V_VALID
)
167 && ( !!(hpte_v
& HPTE_V_SECONDARY
) == j
)) {
181 static long native_hpte_updatepp(unsigned long slot
, unsigned long newpp
,
182 unsigned long va
, int large
, int local
)
184 hpte_t
*hptep
= htab_address
+ slot
;
185 unsigned long hpte_v
;
186 unsigned long avpn
= va
>> 23;
192 native_lock_hpte(hptep
);
196 /* Even if we miss, we need to invalidate the TLB */
197 if ((HPTE_V_AVPN_VAL(hpte_v
) != avpn
)
198 || !(hpte_v
& HPTE_V_VALID
)) {
199 native_unlock_hpte(hptep
);
202 set_pp_bit(newpp
, hptep
);
203 native_unlock_hpte(hptep
);
206 /* Ensure it is out of the tlb too */
207 if (cpu_has_feature(CPU_FTR_TLBIEL
) && !large
&& local
) {
210 int lock_tlbie
= !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE
);
213 spin_lock(&native_tlbie_lock
);
216 spin_unlock(&native_tlbie_lock
);
223 * Update the page protection bits. Intended to be used to create
224 * guard pages for kernel data structures on pages which are bolted
225 * in the HPT. Assumes pages being operated on will not be stolen.
226 * Does not work on large pages.
228 * No need to lock here because we should be the only user.
230 static void native_hpte_updateboltedpp(unsigned long newpp
, unsigned long ea
)
232 unsigned long vsid
, va
, vpn
, flags
= 0;
235 int lock_tlbie
= !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE
);
237 vsid
= get_kernel_vsid(ea
);
238 va
= (vsid
<< 28) | (ea
& 0x0fffffff);
239 vpn
= va
>> PAGE_SHIFT
;
241 slot
= native_hpte_find(vpn
);
243 panic("could not find page to bolt\n");
244 hptep
= htab_address
+ slot
;
246 set_pp_bit(newpp
, hptep
);
248 /* Ensure it is out of the tlb too */
250 spin_lock_irqsave(&native_tlbie_lock
, flags
);
253 spin_unlock_irqrestore(&native_tlbie_lock
, flags
);
256 static void native_hpte_invalidate(unsigned long slot
, unsigned long va
,
257 int large
, int local
)
259 hpte_t
*hptep
= htab_address
+ slot
;
260 unsigned long hpte_v
;
261 unsigned long avpn
= va
>> 23;
263 int lock_tlbie
= !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE
);
268 local_irq_save(flags
);
269 native_lock_hpte(hptep
);
273 /* Even if we miss, we need to invalidate the TLB */
274 if ((HPTE_V_AVPN_VAL(hpte_v
) != avpn
)
275 || !(hpte_v
& HPTE_V_VALID
)) {
276 native_unlock_hpte(hptep
);
278 /* Invalidate the hpte. NOTE: this also unlocks it */
282 /* Invalidate the tlb */
283 if (cpu_has_feature(CPU_FTR_TLBIEL
) && !large
&& local
) {
287 spin_lock(&native_tlbie_lock
);
290 spin_unlock(&native_tlbie_lock
);
292 local_irq_restore(flags
);
296 * clear all mappings on kexec. All cpus are in real mode (or they will
297 * be when they isi), and we are the only one left. We rely on our kernel
298 * mapping being 0xC0's and the hardware ignoring those two real bits.
300 * TODO: add batching support when enabled. remember, no dynamic memory here,
301 * athough there is the control page available...
303 static void native_hpte_clear(void)
305 unsigned long slot
, slots
, flags
;
306 hpte_t
*hptep
= htab_address
;
307 unsigned long hpte_v
;
308 unsigned long pteg_count
;
310 pteg_count
= htab_hash_mask
+ 1;
312 local_irq_save(flags
);
314 /* we take the tlbie lock and hold it. Some hardware will
315 * deadlock if we try to tlbie from two processors at once.
317 spin_lock(&native_tlbie_lock
);
319 slots
= pteg_count
* HPTES_PER_GROUP
;
321 for (slot
= 0; slot
< slots
; slot
++, hptep
++) {
323 * we could lock the pte here, but we are the only cpu
324 * running, right? and for crash dump, we probably
325 * don't want to wait for a maybe bad cpu.
329 if (hpte_v
& HPTE_V_VALID
) {
331 tlbie(slot2va(hpte_v
, slot
), hpte_v
& HPTE_V_LARGE
);
335 spin_unlock(&native_tlbie_lock
);
336 local_irq_restore(flags
);
339 static void native_flush_hash_range(unsigned long context
,
340 unsigned long number
, int local
)
342 unsigned long vsid
, vpn
, va
, hash
, secondary
, slot
, flags
, avpn
;
345 unsigned long hpte_v
;
346 struct ppc64_tlb_batch
*batch
= &__get_cpu_var(ppc64_tlb_batch
);
348 /* XXX fix for large ptes */
349 unsigned long large
= 0;
351 local_irq_save(flags
);
354 for (i
= 0; i
< number
; i
++) {
355 if (batch
->addr
[i
] < KERNELBASE
)
356 vsid
= get_vsid(context
, batch
->addr
[i
]);
358 vsid
= get_kernel_vsid(batch
->addr
[i
]);
360 va
= (vsid
<< 28) | (batch
->addr
[i
] & 0x0fffffff);
361 batch
->vaddr
[j
] = va
;
363 vpn
= va
>> HPAGE_SHIFT
;
365 vpn
= va
>> PAGE_SHIFT
;
366 hash
= hpt_hash(vpn
, large
);
367 secondary
= (pte_val(batch
->pte
[i
]) & _PAGE_SECONDARY
) >> 15;
370 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
371 slot
+= (pte_val(batch
->pte
[i
]) & _PAGE_GROUP_IX
) >> 12;
373 hptep
= htab_address
+ slot
;
379 native_lock_hpte(hptep
);
383 /* Even if we miss, we need to invalidate the TLB */
384 if ((HPTE_V_AVPN_VAL(hpte_v
) != avpn
)
385 || !(hpte_v
& HPTE_V_VALID
)) {
386 native_unlock_hpte(hptep
);
388 /* Invalidate the hpte. NOTE: this also unlocks it */
395 if (cpu_has_feature(CPU_FTR_TLBIEL
) && !large
&& local
) {
396 asm volatile("ptesync":::"memory");
398 for (i
= 0; i
< j
; i
++)
399 __tlbiel(batch
->vaddr
[i
]);
401 asm volatile("ptesync":::"memory");
403 int lock_tlbie
= !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE
);
406 spin_lock(&native_tlbie_lock
);
408 asm volatile("ptesync":::"memory");
410 for (i
= 0; i
< j
; i
++)
411 __tlbie(batch
->vaddr
[i
], 0);
413 asm volatile("eieio; tlbsync; ptesync":::"memory");
416 spin_unlock(&native_tlbie_lock
);
419 local_irq_restore(flags
);
422 #ifdef CONFIG_PPC_PSERIES
423 /* Disable TLB batching on nighthawk */
424 static inline int tlb_batching_enabled(void)
426 struct device_node
*root
= of_find_node_by_path("/");
430 const char *model
= get_property(root
, "model", NULL
);
431 if (model
&& !strcmp(model
, "IBM,9076-N81"))
439 static inline int tlb_batching_enabled(void)
445 void hpte_init_native(void)
447 ppc_md
.hpte_invalidate
= native_hpte_invalidate
;
448 ppc_md
.hpte_updatepp
= native_hpte_updatepp
;
449 ppc_md
.hpte_updateboltedpp
= native_hpte_updateboltedpp
;
450 ppc_md
.hpte_insert
= native_hpte_insert
;
451 ppc_md
.hpte_remove
= native_hpte_remove
;
452 ppc_md
.hpte_clear_all
= native_hpte_clear
;
453 if (tlb_batching_enabled())
454 ppc_md
.flush_hash_range
= native_flush_hash_range
;