powerpc: move ItLpQueue.c to powerpc/platforms/iseries
[powerpc.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, unsigned long vflags,
42                                 unsigned long rflags)
43 {
44         unsigned long arpn;
45         long slot;
46         hpte_t lhpte;
47         int secondary = 0;
48
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          */
55         if (vflags & HPTE_V_SECONDARY)
56                 return -1;
57
58         iSeries_hlock(hpte_group);
59
60         slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
61         BUG_ON(lhpte.v & HPTE_V_VALID);
62
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 */
69                 vflags |= HPTE_V_VALID;
70                 secondary = 1;
71                 slot &= 0x7fffffffffffffff;
72         }
73
74         arpn = phys_to_abs(prpn << PAGE_SHIFT) >> PAGE_SHIFT;
75
76         lhpte.v = (va >> 23) << HPTE_V_AVPN_SHIFT | vflags | HPTE_V_VALID;
77         lhpte.r = (arpn << HPTE_R_RPN_SHIFT) | rflags;
78
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
87 long iSeries_hpte_bolt_or_insert(unsigned long hpte_group,
88                 unsigned long va, unsigned long prpn, unsigned long vflags,
89                 unsigned long rflags)
90 {
91         long slot;
92         hpte_t lhpte;
93
94         slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
95
96         if (lhpte.v & HPTE_V_VALID) {
97                 /* Bolt the existing HPTE */
98                 HvCallHpt_setSwBits(slot, 0x10, 0);
99                 HvCallHpt_setPp(slot, PP_RWXX);
100                 return 0;
101         }
102
103         return iSeries_hpte_insert(hpte_group, va, prpn, vflags, rflags);
104 }
105
106 static unsigned long iSeries_hpte_getword0(unsigned long slot)
107 {
108         hpte_t hpte;
109
110         HvCallHpt_get(&hpte, slot);
111         return hpte.v;
112 }
113
114 static long iSeries_hpte_remove(unsigned long hpte_group)
115 {
116         unsigned long slot_offset;
117         int i;
118         unsigned long hpte_v;
119
120         /* Pick a random slot to start at */
121         slot_offset = mftb() & 0x7;
122
123         iSeries_hlock(hpte_group);
124
125         for (i = 0; i < HPTES_PER_GROUP; i++) {
126                 hpte_v = iSeries_hpte_getword0(hpte_group + slot_offset);
127
128                 if (! (hpte_v & HPTE_V_BOLTED)) {
129                         HvCallHpt_invalidateSetSwBitsGet(hpte_group + 
130                                                          slot_offset, 0, 0);
131                         iSeries_hunlock(hpte_group);
132                         return i;
133                 }
134
135                 slot_offset++;
136                 slot_offset &= 0x7;
137         }
138
139         iSeries_hunlock(hpte_group);
140
141         return -1;
142 }
143
144 /*
145  * The HyperVisor expects the "flags" argument in this form:
146  *      bits  0..59 : reserved
147  *      bit      60 : N
148  *      bits 61..63 : PP2,PP1,PP0
149  */
150 static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp,
151                                   unsigned long va, int large, int local)
152 {
153         hpte_t hpte;
154         unsigned long avpn = va >> 23;
155
156         iSeries_hlock(slot);
157
158         HvCallHpt_get(&hpte, slot);
159         if ((HPTE_V_AVPN_VAL(hpte.v) == avpn) && (hpte.v & HPTE_V_VALID)) {
160                 /*
161                  * Hypervisor expects bits as NPPP, which is
162                  * different from how they are mapped in our PP.
163                  */
164                 HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1));
165                 iSeries_hunlock(slot);
166                 return 0;
167         }
168         iSeries_hunlock(slot);
169
170         return -1;
171 }
172
173 /*
174  * Functions used to find the PTE for a particular virtual address. 
175  * Only used during boot when bolting pages.
176  *
177  * Input : vpn      : virtual page number
178  * Output: PTE index within the page table of the entry
179  *         -1 on failure
180  */
181 static long iSeries_hpte_find(unsigned long vpn)
182 {
183         hpte_t hpte;
184         long slot;
185
186         /*
187          * The HvCallHpt_findValid interface is as follows:
188          * 0xffffffffffffffff : No entry found.
189          * 0x00000000xxxxxxxx : Entry found in primary group, slot x
190          * 0x80000000xxxxxxxx : Entry found in secondary group, slot x
191          */
192         slot = HvCallHpt_findValid(&hpte, vpn); 
193         if (hpte.v & HPTE_V_VALID) {
194                 if (slot < 0) {
195                         slot &= 0x7fffffffffffffff;
196                         slot = -slot;
197                 }
198         } else
199                 slot = -1;
200         return slot;
201 }
202
203 /*
204  * Update the page protection bits. Intended to be used to create
205  * guard pages for kernel data structures on pages which are bolted
206  * in the HPT. Assumes pages being operated on will not be stolen.
207  * Does not work on large pages.
208  *
209  * No need to lock here because we should be the only user.
210  */
211 static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
212 {
213         unsigned long vsid,va,vpn;
214         long slot;
215
216         vsid = get_kernel_vsid(ea);
217         va = (vsid << 28) | (ea & 0x0fffffff);
218         vpn = va >> PAGE_SHIFT;
219         slot = iSeries_hpte_find(vpn); 
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,
226                                     int large, int local)
227 {
228         unsigned long hpte_v;
229         unsigned long avpn = va >> 23;
230         unsigned long flags;
231
232         local_irq_save(flags);
233
234         iSeries_hlock(slot);
235
236         hpte_v = iSeries_hpte_getword0(slot);
237         
238         if ((HPTE_V_AVPN_VAL(hpte_v) == avpn) && (hpte_v & HPTE_V_VALID))
239                 HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0);
240
241         iSeries_hunlock(slot);
242
243         local_irq_restore(flags);
244 }
245
246 void hpte_init_iSeries(void)
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;
252         ppc_md.hpte_remove      = iSeries_hpte_remove;
253
254         htab_finish_init();
255 }