Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / arch / ppc64 / kernel / iSeries_htab.c
1 /*
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
21 static 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  */
26 static 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
33 static 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
40 static long iSeries_hpte_insert(unsigned long hpte_group, unsigned long va,
41                          unsigned long prpn, int secondary,
42                          unsigned long hpteflags, int bolted, int large)
43 {
44         long slot;
45         HPTE lhpte;
46
47         /*
48          * The hypervisor tries both primary and secondary.
49          * If we are being called to insert in the secondary,
50          * it means we have already tried both primary and secondary,
51          * so we return failure immediately.
52          */
53         if (secondary)
54                 return -1;
55
56         iSeries_hlock(hpte_group);
57
58         slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
59         BUG_ON(lhpte.dw0.dw0.v);
60
61         if (slot == -1) { /* No available entry found in either group */
62                 iSeries_hunlock(hpte_group);
63                 return -1;
64         }
65
66         if (slot < 0) {         /* MSB set means secondary group */
67                 secondary = 1;
68                 slot &= 0x7fffffffffffffff;
69         }
70
71         lhpte.dw1.dword1      = 0;
72         lhpte.dw1.dw1.rpn     = physRpn_to_absRpn(prpn);
73         lhpte.dw1.flags.flags = hpteflags;
74
75         lhpte.dw0.dword0      = 0;
76         lhpte.dw0.dw0.avpn    = va >> 23;
77         lhpte.dw0.dw0.h       = secondary;
78         lhpte.dw0.dw0.bolted  = bolted;
79         lhpte.dw0.dw0.v       = 1;
80
81         /* Now fill in the actual HPTE */
82         HvCallHpt_addValidate(slot, secondary, &lhpte);
83
84         iSeries_hunlock(hpte_group);
85
86         return (secondary << 3) | (slot & 7);
87 }
88
89 static unsigned long iSeries_hpte_getword0(unsigned long slot)
90 {
91         unsigned long dword0;
92         HPTE hpte;
93
94         HvCallHpt_get(&hpte, slot);
95         dword0 = hpte.dw0.dword0;
96
97         return dword0;
98 }
99
100 static long iSeries_hpte_remove(unsigned long hpte_group)
101 {
102         unsigned long slot_offset;
103         int i;
104         HPTE lhpte;
105
106         /* Pick a random slot to start at */
107         slot_offset = mftb() & 0x7;
108
109         iSeries_hlock(hpte_group);
110
111         for (i = 0; i < HPTES_PER_GROUP; i++) {
112                 lhpte.dw0.dword0 = 
113                         iSeries_hpte_getword0(hpte_group + slot_offset);
114
115                 if (!lhpte.dw0.dw0.bolted) {
116                         HvCallHpt_invalidateSetSwBitsGet(hpte_group + 
117                                                          slot_offset, 0, 0);
118                         iSeries_hunlock(hpte_group);
119                         return i;
120                 }
121
122                 slot_offset++;
123                 slot_offset &= 0x7;
124         }
125
126         iSeries_hunlock(hpte_group);
127
128         return -1;
129 }
130
131 /*
132  * The HyperVisor expects the "flags" argument in this form:
133  *      bits  0..59 : reserved
134  *      bit      60 : N
135  *      bits 61..63 : PP2,PP1,PP0
136  */
137 static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp,
138                                   unsigned long va, int large, int local)
139 {
140         HPTE hpte;
141         unsigned long avpn = va >> 23;
142
143         iSeries_hlock(slot);
144
145         HvCallHpt_get(&hpte, slot);
146         if ((hpte.dw0.dw0.avpn == avpn) && (hpte.dw0.dw0.v)) {
147                 /*
148                  * Hypervisor expects bits as NPPP, which is
149                  * different from how they are mapped in our PP.
150                  */
151                 HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1));
152                 iSeries_hunlock(slot);
153                 return 0;
154         }
155         iSeries_hunlock(slot);
156
157         return -1;
158 }
159
160 /*
161  * Functions used to find the PTE for a particular virtual address. 
162  * Only used during boot when bolting pages.
163  *
164  * Input : vpn      : virtual page number
165  * Output: PTE index within the page table of the entry
166  *         -1 on failure
167  */
168 static long iSeries_hpte_find(unsigned long vpn)
169 {
170         HPTE hpte;
171         long slot;
172
173         /*
174          * The HvCallHpt_findValid interface is as follows:
175          * 0xffffffffffffffff : No entry found.
176          * 0x00000000xxxxxxxx : Entry found in primary group, slot x
177          * 0x80000000xxxxxxxx : Entry found in secondary group, slot x
178          */
179         slot = HvCallHpt_findValid(&hpte, vpn); 
180         if (hpte.dw0.dw0.v) {
181                 if (slot < 0) {
182                         slot &= 0x7fffffffffffffff;
183                         slot = -slot;
184                 }
185         } else
186                 slot = -1;
187         return slot;
188 }
189
190 /*
191  * Update the page protection bits. Intended to be used to create
192  * guard pages for kernel data structures on pages which are bolted
193  * in the HPT. Assumes pages being operated on will not be stolen.
194  * Does not work on large pages.
195  *
196  * No need to lock here because we should be the only user.
197  */
198 static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
199 {
200         unsigned long vsid,va,vpn;
201         long slot;
202
203         vsid = get_kernel_vsid(ea);
204         va = (vsid << 28) | (ea & 0x0fffffff);
205         vpn = va >> PAGE_SHIFT;
206         slot = iSeries_hpte_find(vpn); 
207         if (slot == -1)
208                 panic("updateboltedpp: Could not find page to bolt\n");
209         HvCallHpt_setPp(slot, newpp);
210 }
211
212 static void iSeries_hpte_invalidate(unsigned long slot, unsigned long va,
213                                     int large, int local)
214 {
215         HPTE lhpte;
216         unsigned long avpn = va >> 23;
217         unsigned long flags;
218
219         local_irq_save(flags);
220
221         iSeries_hlock(slot);
222
223         lhpte.dw0.dword0 = iSeries_hpte_getword0(slot);
224         
225         if ((lhpte.dw0.dw0.avpn == avpn) && lhpte.dw0.dw0.v)
226                 HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0);
227
228         iSeries_hunlock(slot);
229
230         local_irq_restore(flags);
231 }
232
233 void hpte_init_iSeries(void)
234 {
235         ppc_md.hpte_invalidate  = iSeries_hpte_invalidate;
236         ppc_md.hpte_updatepp    = iSeries_hpte_updatepp;
237         ppc_md.hpte_updateboltedpp = iSeries_hpte_updateboltedpp;
238         ppc_md.hpte_insert      = iSeries_hpte_insert;
239         ppc_md.hpte_remove      = iSeries_hpte_remove;
240
241         htab_finish_init();
242 }