Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * native hashtable management. |
| 3 | * |
| 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. |
| 11 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 12 | |
| 13 | #undef DEBUG_LOW |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/bitops.h> |
| 17 | #include <linux/threads.h> |
| 18 | #include <linux/smp.h> |
| 19 | |
| 20 | #include <asm/abs_addr.h> |
| 21 | #include <asm/machdep.h> |
| 22 | #include <asm/mmu.h> |
| 23 | #include <asm/mmu_context.h> |
| 24 | #include <asm/pgtable.h> |
| 25 | #include <asm/tlbflush.h> |
| 26 | #include <asm/tlb.h> |
| 27 | #include <asm/cputable.h> |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 28 | #include <asm/udbg.h> |
| 29 | |
| 30 | #ifdef DEBUG_LOW |
| 31 | #define DBG_LOW(fmt...) udbg_printf(fmt) |
| 32 | #else |
| 33 | #define DBG_LOW(fmt...) |
| 34 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #define HPTE_LOCK_BIT 3 |
| 37 | |
| 38 | static DEFINE_SPINLOCK(native_tlbie_lock); |
| 39 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 40 | static inline void __tlbie(unsigned long va, unsigned int psize) |
| 41 | { |
| 42 | unsigned int penc; |
| 43 | |
| 44 | /* clear top 16 bits, non SLS segment */ |
| 45 | va &= ~(0xffffULL << 48); |
| 46 | |
| 47 | switch (psize) { |
| 48 | case MMU_PAGE_4K: |
| 49 | va &= ~0xffful; |
| 50 | asm volatile("tlbie %0,0" : : "r" (va) : "memory"); |
| 51 | break; |
| 52 | default: |
| 53 | penc = mmu_psize_defs[psize].penc; |
| 54 | va &= ~((1ul << mmu_psize_defs[psize].shift) - 1); |
Arnd Bergmann | 19242b2 | 2006-06-15 21:15:44 +1000 | [diff] [blame] | 55 | va |= penc << 12; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 56 | asm volatile("tlbie %0,1" : : "r" (va) : "memory"); |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static inline void __tlbiel(unsigned long va, unsigned int psize) |
| 62 | { |
| 63 | unsigned int penc; |
| 64 | |
| 65 | /* clear top 16 bits, non SLS segment */ |
| 66 | va &= ~(0xffffULL << 48); |
| 67 | |
| 68 | switch (psize) { |
| 69 | case MMU_PAGE_4K: |
| 70 | va &= ~0xffful; |
| 71 | asm volatile(".long 0x7c000224 | (%0 << 11) | (0 << 21)" |
| 72 | : : "r"(va) : "memory"); |
| 73 | break; |
| 74 | default: |
| 75 | penc = mmu_psize_defs[psize].penc; |
| 76 | va &= ~((1ul << mmu_psize_defs[psize].shift) - 1); |
Arnd Bergmann | 19242b2 | 2006-06-15 21:15:44 +1000 | [diff] [blame] | 77 | va |= penc << 12; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 78 | asm volatile(".long 0x7c000224 | (%0 << 11) | (1 << 21)" |
| 79 | : : "r"(va) : "memory"); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | static inline void tlbie(unsigned long va, int psize, int local) |
| 86 | { |
| 87 | unsigned int use_local = local && cpu_has_feature(CPU_FTR_TLBIEL); |
| 88 | int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE); |
| 89 | |
| 90 | if (use_local) |
| 91 | use_local = mmu_psize_defs[psize].tlbiel; |
| 92 | if (lock_tlbie && !use_local) |
| 93 | spin_lock(&native_tlbie_lock); |
| 94 | asm volatile("ptesync": : :"memory"); |
| 95 | if (use_local) { |
| 96 | __tlbiel(va, psize); |
| 97 | asm volatile("ptesync": : :"memory"); |
| 98 | } else { |
| 99 | __tlbie(va, psize); |
| 100 | asm volatile("eieio; tlbsync; ptesync": : :"memory"); |
| 101 | } |
| 102 | if (lock_tlbie && !use_local) |
| 103 | spin_unlock(&native_tlbie_lock); |
| 104 | } |
| 105 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 106 | static inline void native_lock_hpte(hpte_t *hptep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 108 | unsigned long *word = &hptep->v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | while (1) { |
| 111 | if (!test_and_set_bit(HPTE_LOCK_BIT, word)) |
| 112 | break; |
| 113 | while(test_bit(HPTE_LOCK_BIT, word)) |
| 114 | cpu_relax(); |
| 115 | } |
| 116 | } |
| 117 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 118 | static inline void native_unlock_hpte(hpte_t *hptep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 120 | unsigned long *word = &hptep->v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | asm volatile("lwsync":::"memory"); |
| 123 | clear_bit(HPTE_LOCK_BIT, word); |
| 124 | } |
| 125 | |
| 126 | long native_hpte_insert(unsigned long hpte_group, unsigned long va, |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 127 | unsigned long pa, unsigned long rflags, |
| 128 | unsigned long vflags, int psize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 130 | hpte_t *hptep = htab_address + hpte_group; |
| 131 | unsigned long hpte_v, hpte_r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | int i; |
| 133 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 134 | if (!(vflags & HPTE_V_BOLTED)) { |
| 135 | DBG_LOW(" insert(group=%lx, va=%016lx, pa=%016lx," |
| 136 | " rflags=%lx, vflags=%lx, psize=%d)\n", |
| 137 | hpte_group, va, pa, rflags, vflags, psize); |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | for (i = 0; i < HPTES_PER_GROUP; i++) { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 141 | if (! (hptep->v & HPTE_V_VALID)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* retry with lock held */ |
| 143 | native_lock_hpte(hptep); |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 144 | if (! (hptep->v & HPTE_V_VALID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | break; |
| 146 | native_unlock_hpte(hptep); |
| 147 | } |
| 148 | |
| 149 | hptep++; |
| 150 | } |
| 151 | |
| 152 | if (i == HPTES_PER_GROUP) |
| 153 | return -1; |
| 154 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 155 | hpte_v = hpte_encode_v(va, psize) | vflags | HPTE_V_VALID; |
| 156 | hpte_r = hpte_encode_r(pa, psize) | rflags; |
| 157 | |
| 158 | if (!(vflags & HPTE_V_BOLTED)) { |
| 159 | DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n", |
| 160 | i, hpte_v, hpte_r); |
| 161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 163 | hptep->r = hpte_r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* Guarantee the second dword is visible before the valid bit */ |
| 165 | __asm__ __volatile__ ("eieio" : : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* |
| 167 | * Now set the first dword including the valid bit |
| 168 | * NOTE: this also unlocks the hpte |
| 169 | */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 170 | hptep->v = hpte_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | __asm__ __volatile__ ("ptesync" : : : "memory"); |
| 173 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 174 | return i | (!!(vflags & HPTE_V_SECONDARY) << 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static long native_hpte_remove(unsigned long hpte_group) |
| 178 | { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 179 | hpte_t *hptep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | int i; |
| 181 | int slot_offset; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 182 | unsigned long hpte_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 184 | DBG_LOW(" remove(group=%lx)\n", hpte_group); |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* pick a random entry to start at */ |
| 187 | slot_offset = mftb() & 0x7; |
| 188 | |
| 189 | for (i = 0; i < HPTES_PER_GROUP; i++) { |
| 190 | hptep = htab_address + hpte_group + slot_offset; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 191 | hpte_v = hptep->v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 193 | if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* retry with lock held */ |
| 195 | native_lock_hpte(hptep); |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 196 | hpte_v = hptep->v; |
| 197 | if ((hpte_v & HPTE_V_VALID) |
| 198 | && !(hpte_v & HPTE_V_BOLTED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | break; |
| 200 | native_unlock_hpte(hptep); |
| 201 | } |
| 202 | |
| 203 | slot_offset++; |
| 204 | slot_offset &= 0x7; |
| 205 | } |
| 206 | |
| 207 | if (i == HPTES_PER_GROUP) |
| 208 | return -1; |
| 209 | |
| 210 | /* Invalidate the hpte. NOTE: this also unlocks it */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 211 | hptep->v = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | return i; |
| 214 | } |
| 215 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 216 | static long native_hpte_updatepp(unsigned long slot, unsigned long newpp, |
| 217 | unsigned long va, int psize, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 219 | hpte_t *hptep = htab_address + slot; |
| 220 | unsigned long hpte_v, want_v; |
| 221 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 223 | want_v = hpte_encode_v(va, psize); |
| 224 | |
| 225 | DBG_LOW(" update(va=%016lx, avpnv=%016lx, hash=%016lx, newpp=%x)", |
| 226 | va, want_v & HPTE_V_AVPN, slot, newpp); |
| 227 | |
| 228 | native_lock_hpte(hptep); |
| 229 | |
| 230 | hpte_v = hptep->v; |
| 231 | |
| 232 | /* Even if we miss, we need to invalidate the TLB */ |
| 233 | if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) { |
| 234 | DBG_LOW(" -> miss\n"); |
| 235 | native_unlock_hpte(hptep); |
| 236 | ret = -1; |
| 237 | } else { |
| 238 | DBG_LOW(" -> hit\n"); |
| 239 | /* Update the HPTE */ |
| 240 | hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) | |
| 241 | (newpp & (HPTE_R_PP | HPTE_R_N)); |
| 242 | native_unlock_hpte(hptep); |
| 243 | } |
| 244 | |
| 245 | /* Ensure it is out of the tlb too. */ |
| 246 | tlbie(va, psize, local); |
| 247 | |
| 248 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 251 | static long native_hpte_find(unsigned long va, int psize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 253 | hpte_t *hptep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | unsigned long hash; |
| 255 | unsigned long i, j; |
| 256 | long slot; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 257 | unsigned long want_v, hpte_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 259 | hash = hpt_hash(va, mmu_psize_defs[psize].shift); |
| 260 | want_v = hpte_encode_v(va, psize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | for (j = 0; j < 2; j++) { |
| 263 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 264 | for (i = 0; i < HPTES_PER_GROUP; i++) { |
| 265 | hptep = htab_address + slot; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 266 | hpte_v = hptep->v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 268 | if (HPTE_V_COMPARE(hpte_v, want_v) |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 269 | && (hpte_v & HPTE_V_VALID) |
| 270 | && ( !!(hpte_v & HPTE_V_SECONDARY) == j)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | /* HPTE matches */ |
| 272 | if (j) |
| 273 | slot = -slot; |
| 274 | return slot; |
| 275 | } |
| 276 | ++slot; |
| 277 | } |
| 278 | hash = ~hash; |
| 279 | } |
| 280 | |
| 281 | return -1; |
| 282 | } |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | /* |
| 285 | * Update the page protection bits. Intended to be used to create |
| 286 | * guard pages for kernel data structures on pages which are bolted |
| 287 | * in the HPT. Assumes pages being operated on will not be stolen. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | * |
| 289 | * No need to lock here because we should be the only user. |
| 290 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 291 | static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea, |
| 292 | int psize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 294 | unsigned long vsid, va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | long slot; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 296 | hpte_t *hptep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | vsid = get_kernel_vsid(ea); |
| 299 | va = (vsid << 28) | (ea & 0x0fffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 301 | slot = native_hpte_find(va, psize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (slot == -1) |
| 303 | panic("could not find page to bolt\n"); |
| 304 | hptep = htab_address + slot; |
| 305 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 306 | /* Update the HPTE */ |
| 307 | hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) | |
| 308 | (newpp & (HPTE_R_PP | HPTE_R_N)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 310 | /* Ensure it is out of the tlb too. */ |
| 311 | tlbie(va, psize, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static void native_hpte_invalidate(unsigned long slot, unsigned long va, |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 315 | int psize, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 317 | hpte_t *hptep = htab_address + slot; |
| 318 | unsigned long hpte_v; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 319 | unsigned long want_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 324 | DBG_LOW(" invalidate(va=%016lx, hash: %x)\n", va, slot); |
| 325 | |
| 326 | want_v = hpte_encode_v(va, psize); |
| 327 | native_lock_hpte(hptep); |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 328 | hpte_v = hptep->v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | /* Even if we miss, we need to invalidate the TLB */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 331 | if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | native_unlock_hpte(hptep); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 333 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* Invalidate the hpte. NOTE: this also unlocks it */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 335 | hptep->v = 0; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 336 | |
| 337 | /* Invalidate the TLB */ |
| 338 | tlbie(va, psize, local); |
| 339 | |
| 340 | local_irq_restore(flags); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * XXX This need fixing based on page size. It's only used by |
| 345 | * native_hpte_clear() for now which needs fixing too so they |
| 346 | * make a good pair... |
| 347 | */ |
| 348 | static unsigned long slot2va(unsigned long hpte_v, unsigned long slot) |
| 349 | { |
| 350 | unsigned long avpn = HPTE_V_AVPN_VAL(hpte_v); |
| 351 | unsigned long va; |
| 352 | |
| 353 | va = avpn << 23; |
| 354 | |
| 355 | if (! (hpte_v & HPTE_V_LARGE)) { |
| 356 | unsigned long vpi, pteg; |
| 357 | |
| 358 | pteg = slot / HPTES_PER_GROUP; |
| 359 | if (hpte_v & HPTE_V_SECONDARY) |
| 360 | pteg = ~pteg; |
| 361 | |
| 362 | vpi = ((va >> 28) ^ pteg) & htab_hash_mask; |
| 363 | |
| 364 | va |= vpi << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 367 | return va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
R Sharada | f4c82d5 | 2005-06-25 14:58:08 -0700 | [diff] [blame] | 370 | /* |
| 371 | * clear all mappings on kexec. All cpus are in real mode (or they will |
| 372 | * be when they isi), and we are the only one left. We rely on our kernel |
| 373 | * mapping being 0xC0's and the hardware ignoring those two real bits. |
| 374 | * |
| 375 | * TODO: add batching support when enabled. remember, no dynamic memory here, |
| 376 | * athough there is the control page available... |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 377 | * |
| 378 | * XXX FIXME: 4k only for now ! |
R Sharada | f4c82d5 | 2005-06-25 14:58:08 -0700 | [diff] [blame] | 379 | */ |
| 380 | static void native_hpte_clear(void) |
| 381 | { |
| 382 | unsigned long slot, slots, flags; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 383 | hpte_t *hptep = htab_address; |
| 384 | unsigned long hpte_v; |
R Sharada | f4c82d5 | 2005-06-25 14:58:08 -0700 | [diff] [blame] | 385 | unsigned long pteg_count; |
| 386 | |
| 387 | pteg_count = htab_hash_mask + 1; |
| 388 | |
| 389 | local_irq_save(flags); |
| 390 | |
| 391 | /* we take the tlbie lock and hold it. Some hardware will |
| 392 | * deadlock if we try to tlbie from two processors at once. |
| 393 | */ |
| 394 | spin_lock(&native_tlbie_lock); |
| 395 | |
| 396 | slots = pteg_count * HPTES_PER_GROUP; |
| 397 | |
| 398 | for (slot = 0; slot < slots; slot++, hptep++) { |
| 399 | /* |
| 400 | * we could lock the pte here, but we are the only cpu |
| 401 | * running, right? and for crash dump, we probably |
| 402 | * don't want to wait for a maybe bad cpu. |
| 403 | */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 404 | hpte_v = hptep->v; |
R Sharada | f4c82d5 | 2005-06-25 14:58:08 -0700 | [diff] [blame] | 405 | |
R Sharada | 47f78a4 | 2006-02-22 21:43:08 +0530 | [diff] [blame] | 406 | /* |
| 407 | * Call __tlbie() here rather than tlbie() since we |
| 408 | * already hold the native_tlbie_lock. |
| 409 | */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 410 | if (hpte_v & HPTE_V_VALID) { |
| 411 | hptep->v = 0; |
R Sharada | 47f78a4 | 2006-02-22 21:43:08 +0530 | [diff] [blame] | 412 | __tlbie(slot2va(hpte_v, slot), MMU_PAGE_4K); |
R Sharada | f4c82d5 | 2005-06-25 14:58:08 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
R Sharada | 47f78a4 | 2006-02-22 21:43:08 +0530 | [diff] [blame] | 416 | asm volatile("eieio; tlbsync; ptesync":::"memory"); |
R Sharada | f4c82d5 | 2005-06-25 14:58:08 -0700 | [diff] [blame] | 417 | spin_unlock(&native_tlbie_lock); |
| 418 | local_irq_restore(flags); |
| 419 | } |
| 420 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 421 | /* |
| 422 | * Batched hash table flush, we batch the tlbie's to avoid taking/releasing |
| 423 | * the lock all the time |
| 424 | */ |
Benjamin Herrenschmidt | 61b1a94 | 2005-09-20 13:52:50 +1000 | [diff] [blame] | 425 | static void native_flush_hash_range(unsigned long number, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 427 | unsigned long va, hash, index, hidx, shift, slot; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 428 | hpte_t *hptep; |
| 429 | unsigned long hpte_v; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 430 | unsigned long want_v; |
| 431 | unsigned long flags; |
| 432 | real_pte_t pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 434 | unsigned long psize = batch->psize; |
| 435 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | local_irq_save(flags); |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | for (i = 0; i < number; i++) { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 440 | va = batch->vaddr[i]; |
| 441 | pte = batch->pte[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 443 | pte_iterate_hashed_subpages(pte, psize, va, index, shift) { |
| 444 | hash = hpt_hash(va, shift); |
| 445 | hidx = __rpte_to_hidx(pte, index); |
| 446 | if (hidx & _PTEIDX_SECONDARY) |
| 447 | hash = ~hash; |
| 448 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 449 | slot += hidx & _PTEIDX_GROUP_IX; |
| 450 | hptep = htab_address + slot; |
| 451 | want_v = hpte_encode_v(va, psize); |
| 452 | native_lock_hpte(hptep); |
| 453 | hpte_v = hptep->v; |
| 454 | if (!HPTE_V_COMPARE(hpte_v, want_v) || |
| 455 | !(hpte_v & HPTE_V_VALID)) |
| 456 | native_unlock_hpte(hptep); |
| 457 | else |
| 458 | hptep->v = 0; |
| 459 | } pte_iterate_hashed_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 462 | if (cpu_has_feature(CPU_FTR_TLBIEL) && |
| 463 | mmu_psize_defs[psize].tlbiel && local) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | asm volatile("ptesync":::"memory"); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 465 | for (i = 0; i < number; i++) { |
| 466 | va = batch->vaddr[i]; |
| 467 | pte = batch->pte[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 469 | pte_iterate_hashed_subpages(pte, psize, va, index, |
| 470 | shift) { |
| 471 | __tlbiel(va, psize); |
| 472 | } pte_iterate_hashed_end(); |
| 473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | asm volatile("ptesync":::"memory"); |
| 475 | } else { |
| 476 | int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE); |
| 477 | |
| 478 | if (lock_tlbie) |
| 479 | spin_lock(&native_tlbie_lock); |
| 480 | |
| 481 | asm volatile("ptesync":::"memory"); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 482 | for (i = 0; i < number; i++) { |
| 483 | va = batch->vaddr[i]; |
| 484 | pte = batch->pte[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 486 | pte_iterate_hashed_subpages(pte, psize, va, index, |
| 487 | shift) { |
| 488 | __tlbie(va, psize); |
| 489 | } pte_iterate_hashed_end(); |
| 490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | asm volatile("eieio; tlbsync; ptesync":::"memory"); |
| 492 | |
| 493 | if (lock_tlbie) |
| 494 | spin_unlock(&native_tlbie_lock); |
| 495 | } |
| 496 | |
| 497 | local_irq_restore(flags); |
| 498 | } |
| 499 | |
| 500 | #ifdef CONFIG_PPC_PSERIES |
| 501 | /* Disable TLB batching on nighthawk */ |
| 502 | static inline int tlb_batching_enabled(void) |
| 503 | { |
| 504 | struct device_node *root = of_find_node_by_path("/"); |
| 505 | int enabled = 1; |
| 506 | |
| 507 | if (root) { |
| 508 | const char *model = get_property(root, "model", NULL); |
| 509 | if (model && !strcmp(model, "IBM,9076-N81")) |
| 510 | enabled = 0; |
| 511 | of_node_put(root); |
| 512 | } |
| 513 | |
| 514 | return enabled; |
| 515 | } |
| 516 | #else |
| 517 | static inline int tlb_batching_enabled(void) |
| 518 | { |
| 519 | return 1; |
| 520 | } |
| 521 | #endif |
| 522 | |
| 523 | void hpte_init_native(void) |
| 524 | { |
| 525 | ppc_md.hpte_invalidate = native_hpte_invalidate; |
| 526 | ppc_md.hpte_updatepp = native_hpte_updatepp; |
| 527 | ppc_md.hpte_updateboltedpp = native_hpte_updateboltedpp; |
| 528 | ppc_md.hpte_insert = native_hpte_insert; |
R Sharada | f4c82d5 | 2005-06-25 14:58:08 -0700 | [diff] [blame] | 529 | ppc_md.hpte_remove = native_hpte_remove; |
| 530 | ppc_md.hpte_clear_all = native_hpte_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (tlb_batching_enabled()) |
| 532 | ppc_md.flush_hash_range = native_flush_hash_range; |
| 533 | htab_finish_init(); |
| 534 | } |