blob: b0250ae4a72a5954f77bc21fb600080aea83723a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * iSeries hashtable management.
3 * Derived from pSeries_htab.c
4 *
5 * SMP scalability work:
6 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
7 *
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>
17#include <asm/iSeries/HvCallHpt.h>
18#include <asm/abs_addr.h>
19#include <linux/spinlock.h>
20
21static spinlock_t iSeries_hlocks[64] __cacheline_aligned_in_smp = { [0 ... 63] = SPIN_LOCK_UNLOCKED};
22
23/*
24 * Very primitive algorithm for picking up a lock
25 */
26static inline void iSeries_hlock(unsigned long slot)
27{
28 if (slot & 0x8)
29 slot = ~slot;
30 spin_lock(&iSeries_hlocks[(slot >> 4) & 0x3f]);
31}
32
33static inline void iSeries_hunlock(unsigned long slot)
34{
35 if (slot & 0x8)
36 slot = ~slot;
37 spin_unlock(&iSeries_hlocks[(slot >> 4) & 0x3f]);
38}
39
40static long iSeries_hpte_insert(unsigned long hpte_group, unsigned long va,
David Gibson96e28442005-07-13 01:11:42 -070041 unsigned long prpn, unsigned long vflags,
42 unsigned long rflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 long slot;
David Gibson96e28442005-07-13 01:11:42 -070045 hpte_t lhpte;
46 int secondary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 /*
49 * The hypervisor tries both primary and secondary.
50 * If we are being called to insert in the secondary,
51 * it means we have already tried both primary and secondary,
52 * so we return failure immediately.
53 */
David Gibson96e28442005-07-13 01:11:42 -070054 if (vflags & HPTE_V_SECONDARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return -1;
56
57 iSeries_hlock(hpte_group);
58
59 slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
David Gibson96e28442005-07-13 01:11:42 -070060 BUG_ON(lhpte.v & HPTE_V_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (slot == -1) { /* No available entry found in either group */
63 iSeries_hunlock(hpte_group);
64 return -1;
65 }
66
67 if (slot < 0) { /* MSB set means secondary group */
David Gibson96e28442005-07-13 01:11:42 -070068 vflags |= HPTE_V_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 secondary = 1;
70 slot &= 0x7fffffffffffffff;
71 }
72
David Gibson96e28442005-07-13 01:11:42 -070073 lhpte.v = (va >> 23) << HPTE_V_AVPN_SHIFT | vflags | HPTE_V_VALID;
74 lhpte.r = (physRpn_to_absRpn(prpn) << HPTE_R_RPN_SHIFT) | rflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* Now fill in the actual HPTE */
77 HvCallHpt_addValidate(slot, secondary, &lhpte);
78
79 iSeries_hunlock(hpte_group);
80
81 return (secondary << 3) | (slot & 7);
82}
83
84static unsigned long iSeries_hpte_getword0(unsigned long slot)
85{
David Gibson96e28442005-07-13 01:11:42 -070086 hpte_t hpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 HvCallHpt_get(&hpte, slot);
David Gibson96e28442005-07-13 01:11:42 -070089 return hpte.v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92static long iSeries_hpte_remove(unsigned long hpte_group)
93{
94 unsigned long slot_offset;
95 int i;
David Gibson96e28442005-07-13 01:11:42 -070096 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* Pick a random slot to start at */
99 slot_offset = mftb() & 0x7;
100
101 iSeries_hlock(hpte_group);
102
103 for (i = 0; i < HPTES_PER_GROUP; i++) {
David Gibson96e28442005-07-13 01:11:42 -0700104 hpte_v = iSeries_hpte_getword0(hpte_group + slot_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
David Gibson96e28442005-07-13 01:11:42 -0700106 if (! (hpte_v & HPTE_V_BOLTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 HvCallHpt_invalidateSetSwBitsGet(hpte_group +
108 slot_offset, 0, 0);
109 iSeries_hunlock(hpte_group);
110 return i;
111 }
112
113 slot_offset++;
114 slot_offset &= 0x7;
115 }
116
117 iSeries_hunlock(hpte_group);
118
119 return -1;
120}
121
122/*
123 * The HyperVisor expects the "flags" argument in this form:
124 * bits 0..59 : reserved
125 * bit 60 : N
126 * bits 61..63 : PP2,PP1,PP0
127 */
128static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp,
129 unsigned long va, int large, int local)
130{
David Gibson96e28442005-07-13 01:11:42 -0700131 hpte_t hpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long avpn = va >> 23;
133
134 iSeries_hlock(slot);
135
136 HvCallHpt_get(&hpte, slot);
David Gibson96e28442005-07-13 01:11:42 -0700137 if ((HPTE_V_AVPN_VAL(hpte.v) == avpn) && (hpte.v & HPTE_V_VALID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
139 * Hypervisor expects bits as NPPP, which is
140 * different from how they are mapped in our PP.
141 */
142 HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1));
143 iSeries_hunlock(slot);
144 return 0;
145 }
146 iSeries_hunlock(slot);
147
148 return -1;
149}
150
151/*
152 * Functions used to find the PTE for a particular virtual address.
153 * Only used during boot when bolting pages.
154 *
155 * Input : vpn : virtual page number
156 * Output: PTE index within the page table of the entry
157 * -1 on failure
158 */
159static long iSeries_hpte_find(unsigned long vpn)
160{
David Gibson96e28442005-07-13 01:11:42 -0700161 hpte_t hpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 long slot;
163
164 /*
165 * The HvCallHpt_findValid interface is as follows:
166 * 0xffffffffffffffff : No entry found.
167 * 0x00000000xxxxxxxx : Entry found in primary group, slot x
168 * 0x80000000xxxxxxxx : Entry found in secondary group, slot x
169 */
170 slot = HvCallHpt_findValid(&hpte, vpn);
David Gibson96e28442005-07-13 01:11:42 -0700171 if (hpte.v & HPTE_V_VALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (slot < 0) {
173 slot &= 0x7fffffffffffffff;
174 slot = -slot;
175 }
176 } else
177 slot = -1;
178 return slot;
179}
180
181/*
182 * Update the page protection bits. Intended to be used to create
183 * guard pages for kernel data structures on pages which are bolted
184 * in the HPT. Assumes pages being operated on will not be stolen.
185 * Does not work on large pages.
186 *
187 * No need to lock here because we should be the only user.
188 */
189static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
190{
191 unsigned long vsid,va,vpn;
192 long slot;
193
194 vsid = get_kernel_vsid(ea);
195 va = (vsid << 28) | (ea & 0x0fffffff);
196 vpn = va >> PAGE_SHIFT;
197 slot = iSeries_hpte_find(vpn);
198 if (slot == -1)
199 panic("updateboltedpp: Could not find page to bolt\n");
200 HvCallHpt_setPp(slot, newpp);
201}
202
203static void iSeries_hpte_invalidate(unsigned long slot, unsigned long va,
204 int large, int local)
205{
David Gibson96e28442005-07-13 01:11:42 -0700206 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned long avpn = va >> 23;
208 unsigned long flags;
209
210 local_irq_save(flags);
211
212 iSeries_hlock(slot);
213
David Gibson96e28442005-07-13 01:11:42 -0700214 hpte_v = iSeries_hpte_getword0(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
David Gibson96e28442005-07-13 01:11:42 -0700216 if ((HPTE_V_AVPN_VAL(hpte_v) == avpn) && (hpte_v & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0);
218
219 iSeries_hunlock(slot);
220
221 local_irq_restore(flags);
222}
223
224void hpte_init_iSeries(void)
225{
226 ppc_md.hpte_invalidate = iSeries_hpte_invalidate;
227 ppc_md.hpte_updatepp = iSeries_hpte_updatepp;
228 ppc_md.hpte_updateboltedpp = iSeries_hpte_updateboltedpp;
229 ppc_md.hpte_insert = iSeries_hpte_insert;
230 ppc_md.hpte_remove = iSeries_hpte_remove;
231
232 htab_finish_init();
233}