blob: 073b766617474dd9741ed8e79638e6b9ad5fcbf0 [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{
Michael Ellermanaefd16b2005-08-03 20:21:24 +100044 unsigned long arpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 long slot;
David Gibson96e28442005-07-13 01:11:42 -070046 hpte_t lhpte;
47 int secondary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 /*
50 * The hypervisor tries both primary and secondary.
51 * If we are being called to insert in the secondary,
52 * it means we have already tried both primary and secondary,
53 * so we return failure immediately.
54 */
David Gibson96e28442005-07-13 01:11:42 -070055 if (vflags & HPTE_V_SECONDARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return -1;
57
58 iSeries_hlock(hpte_group);
59
60 slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
David Gibson96e28442005-07-13 01:11:42 -070061 BUG_ON(lhpte.v & HPTE_V_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (slot == -1) { /* No available entry found in either group */
64 iSeries_hunlock(hpte_group);
65 return -1;
66 }
67
68 if (slot < 0) { /* MSB set means secondary group */
David Gibson3078fcc2005-10-21 13:41:19 +100069 vflags |= HPTE_V_SECONDARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 secondary = 1;
71 slot &= 0x7fffffffffffffff;
72 }
73
Michael Ellermanaefd16b2005-08-03 20:21:24 +100074 arpn = phys_to_abs(prpn << PAGE_SHIFT) >> PAGE_SHIFT;
75
David Gibson96e28442005-07-13 01:11:42 -070076 lhpte.v = (va >> 23) << HPTE_V_AVPN_SHIFT | vflags | HPTE_V_VALID;
Michael Ellermanaefd16b2005-08-03 20:21:24 +100077 lhpte.r = (arpn << HPTE_R_RPN_SHIFT) | rflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /* Now fill in the actual HPTE */
80 HvCallHpt_addValidate(slot, secondary, &lhpte);
81
82 iSeries_hunlock(hpte_group);
83
84 return (secondary << 3) | (slot & 7);
85}
86
87static unsigned long iSeries_hpte_getword0(unsigned long slot)
88{
David Gibson96e28442005-07-13 01:11:42 -070089 hpte_t hpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 HvCallHpt_get(&hpte, slot);
David Gibson96e28442005-07-13 01:11:42 -070092 return hpte.v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static long iSeries_hpte_remove(unsigned long hpte_group)
96{
97 unsigned long slot_offset;
98 int i;
David Gibson96e28442005-07-13 01:11:42 -070099 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* Pick a random slot to start at */
102 slot_offset = mftb() & 0x7;
103
104 iSeries_hlock(hpte_group);
105
106 for (i = 0; i < HPTES_PER_GROUP; i++) {
David Gibson96e28442005-07-13 01:11:42 -0700107 hpte_v = iSeries_hpte_getword0(hpte_group + slot_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
David Gibson96e28442005-07-13 01:11:42 -0700109 if (! (hpte_v & HPTE_V_BOLTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 HvCallHpt_invalidateSetSwBitsGet(hpte_group +
111 slot_offset, 0, 0);
112 iSeries_hunlock(hpte_group);
113 return i;
114 }
115
116 slot_offset++;
117 slot_offset &= 0x7;
118 }
119
120 iSeries_hunlock(hpte_group);
121
122 return -1;
123}
124
125/*
126 * The HyperVisor expects the "flags" argument in this form:
127 * bits 0..59 : reserved
128 * bit 60 : N
129 * bits 61..63 : PP2,PP1,PP0
130 */
131static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp,
132 unsigned long va, int large, int local)
133{
David Gibson96e28442005-07-13 01:11:42 -0700134 hpte_t hpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned long avpn = va >> 23;
136
137 iSeries_hlock(slot);
138
139 HvCallHpt_get(&hpte, slot);
David Gibson96e28442005-07-13 01:11:42 -0700140 if ((HPTE_V_AVPN_VAL(hpte.v) == avpn) && (hpte.v & HPTE_V_VALID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /*
142 * Hypervisor expects bits as NPPP, which is
143 * different from how they are mapped in our PP.
144 */
145 HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1));
146 iSeries_hunlock(slot);
147 return 0;
148 }
149 iSeries_hunlock(slot);
150
151 return -1;
152}
153
154/*
155 * Functions used to find the PTE for a particular virtual address.
156 * Only used during boot when bolting pages.
157 *
158 * Input : vpn : virtual page number
159 * Output: PTE index within the page table of the entry
160 * -1 on failure
161 */
162static long iSeries_hpte_find(unsigned long vpn)
163{
David Gibson96e28442005-07-13 01:11:42 -0700164 hpte_t hpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 long slot;
166
167 /*
168 * The HvCallHpt_findValid interface is as follows:
169 * 0xffffffffffffffff : No entry found.
170 * 0x00000000xxxxxxxx : Entry found in primary group, slot x
171 * 0x80000000xxxxxxxx : Entry found in secondary group, slot x
172 */
173 slot = HvCallHpt_findValid(&hpte, vpn);
David Gibson96e28442005-07-13 01:11:42 -0700174 if (hpte.v & HPTE_V_VALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (slot < 0) {
176 slot &= 0x7fffffffffffffff;
177 slot = -slot;
178 }
179 } else
180 slot = -1;
181 return slot;
182}
183
184/*
185 * Update the page protection bits. Intended to be used to create
186 * guard pages for kernel data structures on pages which are bolted
187 * in the HPT. Assumes pages being operated on will not be stolen.
188 * Does not work on large pages.
189 *
190 * No need to lock here because we should be the only user.
191 */
192static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
193{
194 unsigned long vsid,va,vpn;
195 long slot;
196
197 vsid = get_kernel_vsid(ea);
198 va = (vsid << 28) | (ea & 0x0fffffff);
199 vpn = va >> PAGE_SHIFT;
200 slot = iSeries_hpte_find(vpn);
201 if (slot == -1)
202 panic("updateboltedpp: Could not find page to bolt\n");
203 HvCallHpt_setPp(slot, newpp);
204}
205
206static void iSeries_hpte_invalidate(unsigned long slot, unsigned long va,
207 int large, int local)
208{
David Gibson96e28442005-07-13 01:11:42 -0700209 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long avpn = va >> 23;
211 unsigned long flags;
212
213 local_irq_save(flags);
214
215 iSeries_hlock(slot);
216
David Gibson96e28442005-07-13 01:11:42 -0700217 hpte_v = iSeries_hpte_getword0(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
David Gibson96e28442005-07-13 01:11:42 -0700219 if ((HPTE_V_AVPN_VAL(hpte_v) == avpn) && (hpte_v & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0);
221
222 iSeries_hunlock(slot);
223
224 local_irq_restore(flags);
225}
226
227void hpte_init_iSeries(void)
228{
229 ppc_md.hpte_invalidate = iSeries_hpte_invalidate;
230 ppc_md.hpte_updatepp = iSeries_hpte_updatepp;
231 ppc_md.hpte_updateboltedpp = iSeries_hpte_updateboltedpp;
232 ppc_md.hpte_insert = iSeries_hpte_insert;
233 ppc_md.hpte_remove = iSeries_hpte_remove;
234
235 htab_finish_init();
236}