Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * iSeries hashtable management. |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 3 | * Derived from pSeries_htab.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * SMP scalability work: |
| 6 | * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | #include <asm/machdep.h> |
| 14 | #include <asm/pgtable.h> |
| 15 | #include <asm/mmu.h> |
| 16 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/abs_addr.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | |
Stephen Rothwell | 0e29bb1 | 2005-10-14 17:09:16 +1000 | [diff] [blame] | 20 | #include "call_hpt.h" |
| 21 | |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 22 | static spinlock_t iSeries_hlocks[64] __cacheline_aligned_in_smp = |
| 23 | { [0 ... 63] = SPIN_LOCK_UNLOCKED}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * Very primitive algorithm for picking up a lock |
| 27 | */ |
| 28 | static inline void iSeries_hlock(unsigned long slot) |
| 29 | { |
| 30 | if (slot & 0x8) |
| 31 | slot = ~slot; |
| 32 | spin_lock(&iSeries_hlocks[(slot >> 4) & 0x3f]); |
| 33 | } |
| 34 | |
| 35 | static inline void iSeries_hunlock(unsigned long slot) |
| 36 | { |
| 37 | if (slot & 0x8) |
| 38 | slot = ~slot; |
| 39 | spin_unlock(&iSeries_hlocks[(slot >> 4) & 0x3f]); |
| 40 | } |
| 41 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 42 | long iSeries_hpte_insert(unsigned long hpte_group, unsigned long va, |
| 43 | unsigned long pa, unsigned long rflags, |
| 44 | unsigned long vflags, int psize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | long slot; |
David Gibson | 8e561e7 | 2007-06-13 14:52:56 +1000 | [diff] [blame] | 47 | struct hash_pte lhpte; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 48 | int secondary = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 50 | BUG_ON(psize != MMU_PAGE_4K); |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * The hypervisor tries both primary and secondary. |
| 54 | * If we are being called to insert in the secondary, |
| 55 | * it means we have already tried both primary and secondary, |
| 56 | * so we return failure immediately. |
| 57 | */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 58 | if (vflags & HPTE_V_SECONDARY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return -1; |
| 60 | |
| 61 | iSeries_hlock(hpte_group); |
| 62 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 63 | slot = HvCallHpt_findValid(&lhpte, va >> HW_PAGE_SHIFT); |
| 64 | if (unlikely(lhpte.v & HPTE_V_VALID)) { |
| 65 | if (vflags & HPTE_V_BOLTED) { |
| 66 | HvCallHpt_setSwBits(slot, 0x10, 0); |
| 67 | HvCallHpt_setPp(slot, PP_RWXX); |
| 68 | iSeries_hunlock(hpte_group); |
| 69 | if (slot < 0) |
| 70 | return 0x8 | (slot & 7); |
| 71 | else |
| 72 | return slot & 7; |
| 73 | } |
| 74 | BUG(); |
| 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | if (slot == -1) { /* No available entry found in either group */ |
| 78 | iSeries_hunlock(hpte_group); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | if (slot < 0) { /* MSB set means secondary group */ |
David Gibson | 3078fcc | 2005-10-21 13:41:19 +1000 | [diff] [blame] | 83 | vflags |= HPTE_V_SECONDARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | secondary = 1; |
| 85 | slot &= 0x7fffffffffffffff; |
| 86 | } |
| 87 | |
Michael Ellerman | aefd16b | 2005-08-03 20:21:24 +1000 | [diff] [blame] | 88 | |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame^] | 89 | lhpte.v = hpte_encode_v(va, MMU_PAGE_4K, MMU_SEGSIZE_256M) | |
| 90 | vflags | HPTE_V_VALID; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 91 | lhpte.r = hpte_encode_r(phys_to_abs(pa), MMU_PAGE_4K) | rflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | /* Now fill in the actual HPTE */ |
| 94 | HvCallHpt_addValidate(slot, secondary, &lhpte); |
| 95 | |
| 96 | iSeries_hunlock(hpte_group); |
| 97 | |
| 98 | return (secondary << 3) | (slot & 7); |
| 99 | } |
| 100 | |
| 101 | static unsigned long iSeries_hpte_getword0(unsigned long slot) |
| 102 | { |
David Gibson | 8e561e7 | 2007-06-13 14:52:56 +1000 | [diff] [blame] | 103 | struct hash_pte hpte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | HvCallHpt_get(&hpte, slot); |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 106 | return hpte.v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static long iSeries_hpte_remove(unsigned long hpte_group) |
| 110 | { |
| 111 | unsigned long slot_offset; |
| 112 | int i; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 113 | unsigned long hpte_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | /* Pick a random slot to start at */ |
| 116 | slot_offset = mftb() & 0x7; |
| 117 | |
| 118 | iSeries_hlock(hpte_group); |
| 119 | |
| 120 | for (i = 0; i < HPTES_PER_GROUP; i++) { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 121 | hpte_v = iSeries_hpte_getword0(hpte_group + slot_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 123 | if (! (hpte_v & HPTE_V_BOLTED)) { |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 124 | HvCallHpt_invalidateSetSwBitsGet(hpte_group + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | slot_offset, 0, 0); |
| 126 | iSeries_hunlock(hpte_group); |
| 127 | return i; |
| 128 | } |
| 129 | |
| 130 | slot_offset++; |
| 131 | slot_offset &= 0x7; |
| 132 | } |
| 133 | |
| 134 | iSeries_hunlock(hpte_group); |
| 135 | |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * The HyperVisor expects the "flags" argument in this form: |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 141 | * bits 0..59 : reserved |
| 142 | * bit 60 : N |
| 143 | * bits 61..63 : PP2,PP1,PP0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | */ |
| 145 | static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame^] | 146 | unsigned long va, int psize, int ssize, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
David Gibson | 8e561e7 | 2007-06-13 14:52:56 +1000 | [diff] [blame] | 148 | struct hash_pte hpte; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 149 | unsigned long want_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | iSeries_hlock(slot); |
| 152 | |
| 153 | HvCallHpt_get(&hpte, slot); |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame^] | 154 | want_v = hpte_encode_v(va, MMU_PAGE_4K, MMU_SEGSIZE_256M); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 155 | |
| 156 | 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] | 157 | /* |
| 158 | * Hypervisor expects bits as NPPP, which is |
| 159 | * different from how they are mapped in our PP. |
| 160 | */ |
| 161 | HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1)); |
| 162 | iSeries_hunlock(slot); |
| 163 | return 0; |
| 164 | } |
| 165 | iSeries_hunlock(slot); |
| 166 | |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | /* |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 171 | * Functions used to find the PTE for a particular virtual address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | * Only used during boot when bolting pages. |
| 173 | * |
| 174 | * Input : vpn : virtual page number |
| 175 | * Output: PTE index within the page table of the entry |
| 176 | * -1 on failure |
| 177 | */ |
| 178 | static long iSeries_hpte_find(unsigned long vpn) |
| 179 | { |
David Gibson | 8e561e7 | 2007-06-13 14:52:56 +1000 | [diff] [blame] | 180 | struct hash_pte hpte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | long slot; |
| 182 | |
| 183 | /* |
| 184 | * The HvCallHpt_findValid interface is as follows: |
| 185 | * 0xffffffffffffffff : No entry found. |
| 186 | * 0x00000000xxxxxxxx : Entry found in primary group, slot x |
| 187 | * 0x80000000xxxxxxxx : Entry found in secondary group, slot x |
| 188 | */ |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 189 | slot = HvCallHpt_findValid(&hpte, vpn); |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 190 | if (hpte.v & HPTE_V_VALID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (slot < 0) { |
| 192 | slot &= 0x7fffffffffffffff; |
| 193 | slot = -slot; |
| 194 | } |
| 195 | } else |
| 196 | slot = -1; |
| 197 | return slot; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Update the page protection bits. Intended to be used to create |
| 202 | * guard pages for kernel data structures on pages which are bolted |
| 203 | * in the HPT. Assumes pages being operated on will not be stolen. |
| 204 | * Does not work on large pages. |
| 205 | * |
| 206 | * No need to lock here because we should be the only user. |
| 207 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 208 | static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame^] | 209 | int psize, int ssize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | unsigned long vsid,va,vpn; |
| 212 | long slot; |
| 213 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 214 | BUG_ON(psize != MMU_PAGE_4K); |
| 215 | |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame^] | 216 | vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | va = (vsid << 28) | (ea & 0x0fffffff); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 218 | vpn = va >> HW_PAGE_SHIFT; |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 219 | slot = iSeries_hpte_find(vpn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (slot == -1) |
| 221 | panic("updateboltedpp: Could not find page to bolt\n"); |
| 222 | HvCallHpt_setPp(slot, newpp); |
| 223 | } |
| 224 | |
| 225 | static void iSeries_hpte_invalidate(unsigned long slot, unsigned long va, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame^] | 226 | int psize, int ssize, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 228 | unsigned long hpte_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | unsigned long avpn = va >> 23; |
| 230 | unsigned long flags; |
| 231 | |
| 232 | local_irq_save(flags); |
| 233 | |
| 234 | iSeries_hlock(slot); |
| 235 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 236 | hpte_v = iSeries_hpte_getword0(slot); |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 237 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 238 | if ((HPTE_V_AVPN_VAL(hpte_v) == avpn) && (hpte_v & HPTE_V_VALID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0); |
| 240 | |
| 241 | iSeries_hunlock(slot); |
| 242 | |
| 243 | local_irq_restore(flags); |
| 244 | } |
| 245 | |
Michael Ellerman | 7d0daae | 2006-06-23 18:16:38 +1000 | [diff] [blame] | 246 | void __init hpte_init_iSeries(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | ppc_md.hpte_invalidate = iSeries_hpte_invalidate; |
| 249 | ppc_md.hpte_updatepp = iSeries_hpte_updatepp; |
| 250 | ppc_md.hpte_updateboltedpp = iSeries_hpte_updateboltedpp; |
| 251 | ppc_md.hpte_insert = iSeries_hpte_insert; |
Stephen Rothwell | e508f43 | 2005-09-28 02:28:45 +1000 | [diff] [blame] | 252 | ppc_md.hpte_remove = iSeries_hpte_remove; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |