[NETLINK]: w1_int.c: fix default netlink group
[linux-2.6/verdex.git] / arch / ppc64 / mm / hash_native.c
bloba6abd3a979bf5be80412ed4c78f54e08074e9d56
1 /*
2 * native hashtable management.
4 * SMP scalability work:
5 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
6 *
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>
19 #include <asm/mmu.h>
20 #include <asm/mmu_context.h>
21 #include <asm/pgtable.h>
22 #include <asm/tlbflush.h>
23 #include <asm/tlb.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;
34 while (1) {
35 if (!test_and_set_bit(HPTE_LOCK_BIT, word))
36 break;
37 while(test_bit(HPTE_LOCK_BIT, word))
38 cpu_relax();
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,
52 unsigned long rflags)
54 unsigned long arpn = physRpn_to_absRpn(prpn);
55 hpte_t *hptep = htab_address + hpte_group;
56 unsigned long hpte_v, hpte_r;
57 int i;
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))
64 break;
65 native_unlock_hpte(hptep);
68 hptep++;
71 if (i == HPTES_PER_GROUP)
72 return -1;
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;
79 hptep->r = hpte_r;
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
86 hptep->v = hpte_v;
88 __asm__ __volatile__ ("ptesync" : : : "memory");
90 return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
93 static long native_hpte_remove(unsigned long hpte_group)
95 hpte_t *hptep;
96 int i;
97 int slot_offset;
98 unsigned long hpte_v;
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;
105 hpte_v = hptep->v;
107 if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
108 /* retry with lock held */
109 native_lock_hpte(hptep);
110 hpte_v = hptep->v;
111 if ((hpte_v & HPTE_V_VALID)
112 && !(hpte_v & HPTE_V_BOLTED))
113 break;
114 native_unlock_hpte(hptep);
117 slot_offset++;
118 slot_offset &= 0x7;
121 if (i == HPTES_PER_GROUP)
122 return -1;
124 /* Invalidate the hpte. NOTE: this also unlocks it */
125 hptep->v = 0;
127 return i;
130 static inline void set_pp_bit(unsigned long pp, hpte_t *addr)
132 unsigned long old;
133 unsigned long *p = &addr->r;
135 __asm__ __volatile__(
136 "1: ldarx %0,0,%3\n\
137 rldimi %0,%2,0,61\n\
138 stdcx. %0,0,%3\n\
139 bne 1b"
140 : "=&r" (old), "=m" (*p)
141 : "r" (pp), "r" (p), "m" (*p)
142 : "cc");
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)
151 hpte_t *hptep;
152 unsigned long hash;
153 unsigned long i, j;
154 long slot;
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;
163 hpte_v = hptep->v;
165 if ((HPTE_V_AVPN_VAL(hpte_v) == (vpn >> 11))
166 && (hpte_v & HPTE_V_VALID)
167 && ( !!(hpte_v & HPTE_V_SECONDARY) == j)) {
168 /* HPTE matches */
169 if (j)
170 slot = -slot;
171 return slot;
173 ++slot;
175 hash = ~hash;
178 return -1;
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;
187 int ret = 0;
189 if (large)
190 avpn &= ~1;
192 native_lock_hpte(hptep);
194 hpte_v = hptep->v;
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);
200 ret = -1;
201 } else {
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) {
208 tlbiel(va);
209 } else {
210 int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
212 if (lock_tlbie)
213 spin_lock(&native_tlbie_lock);
214 tlbie(va, large);
215 if (lock_tlbie)
216 spin_unlock(&native_tlbie_lock);
219 return ret;
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;
233 long slot;
234 hpte_t *hptep;
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);
242 if (slot == -1)
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 */
249 if (lock_tlbie)
250 spin_lock_irqsave(&native_tlbie_lock, flags);
251 tlbie(va, 0);
252 if (lock_tlbie)
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;
262 unsigned long flags;
263 int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
265 if (large)
266 avpn &= ~1;
268 local_irq_save(flags);
269 native_lock_hpte(hptep);
271 hpte_v = hptep->v;
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);
277 } else {
278 /* Invalidate the hpte. NOTE: this also unlocks it */
279 hptep->v = 0;
282 /* Invalidate the tlb */
283 if (cpu_has_feature(CPU_FTR_TLBIEL) && !large && local) {
284 tlbiel(va);
285 } else {
286 if (lock_tlbie)
287 spin_lock(&native_tlbie_lock);
288 tlbie(va, large);
289 if (lock_tlbie)
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.
327 hpte_v = hptep->v;
329 if (hpte_v & HPTE_V_VALID) {
330 hptep->v = 0;
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;
343 int i, j;
344 hpte_t *hptep;
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);
353 j = 0;
354 for (i = 0; i < number; i++) {
355 if (batch->addr[i] < KERNELBASE)
356 vsid = get_vsid(context, batch->addr[i]);
357 else
358 vsid = get_kernel_vsid(batch->addr[i]);
360 va = (vsid << 28) | (batch->addr[i] & 0x0fffffff);
361 batch->vaddr[j] = va;
362 if (large)
363 vpn = va >> HPAGE_SHIFT;
364 else
365 vpn = va >> PAGE_SHIFT;
366 hash = hpt_hash(vpn, large);
367 secondary = (pte_val(batch->pte[i]) & _PAGE_SECONDARY) >> 15;
368 if (secondary)
369 hash = ~hash;
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;
375 avpn = va >> 23;
376 if (large)
377 avpn &= ~0x1UL;
379 native_lock_hpte(hptep);
381 hpte_v = hptep->v;
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);
387 } else {
388 /* Invalidate the hpte. NOTE: this also unlocks it */
389 hptep->v = 0;
392 j++;
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");
402 } else {
403 int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
405 if (lock_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");
415 if (lock_tlbie)
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("/");
427 int enabled = 1;
429 if (root) {
430 const char *model = get_property(root, "model", NULL);
431 if (model && !strcmp(model, "IBM,9076-N81"))
432 enabled = 0;
433 of_node_put(root);
436 return enabled;
438 #else
439 static inline int tlb_batching_enabled(void)
441 return 1;
443 #endif
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;
455 htab_finish_init();