ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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
20 #if 0
21 #include <linux/spinlock.h>
22 #include <linux/bitops.h>
23 #include <linux/threads.h>
24 #include <linux/smp.h>
25
26 #include <asm/tlbflush.h>
27 #include <asm/tlb.h>
28 #include <asm/cputable.h>
29 #endif
30
31 static long iSeries_hpte_insert(unsigned long hpte_group, unsigned long va,
32                          unsigned long prpn, int secondary,
33                          unsigned long hpteflags, int bolted, int large)
34 {
35         long slot;
36         HPTE lhpte;
37
38         /*
39          * The hypervisor tries both primary and secondary.
40          * If we are being called to insert in the secondary,
41          * it means we have already tried both primary and secondary,
42          * so we return failure immediately.
43          */
44         if (secondary)
45                 return -1;
46
47         slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
48         if (lhpte.dw0.dw0.v)
49                 panic("select_hpte_slot found entry already valid\n");
50
51         if (slot == -1) /* No available entry found in either group */
52                 return -1;
53
54         if (slot < 0) {         /* MSB set means secondary group */
55                 secondary = 1;
56                 slot &= 0x7fffffffffffffff;
57         }
58
59         lhpte.dw1.dword1      = 0;
60         lhpte.dw1.dw1.rpn     = physRpn_to_absRpn(prpn);
61         lhpte.dw1.flags.flags = hpteflags;
62
63         lhpte.dw0.dword0      = 0;
64         lhpte.dw0.dw0.avpn    = va >> 23;
65         lhpte.dw0.dw0.h       = secondary;
66         lhpte.dw0.dw0.bolted  = bolted;
67         lhpte.dw0.dw0.v       = 1;
68
69         /* Now fill in the actual HPTE */
70         HvCallHpt_addValidate(slot, secondary, &lhpte);
71
72         return (secondary << 3) | (slot & 7);
73 }
74
75 static unsigned long iSeries_hpte_getword0(unsigned long slot)
76 {
77         unsigned long dword0;
78         HPTE hpte;
79
80         HvCallHpt_get(&hpte, slot);
81         dword0 = hpte.dw0.dword0;
82
83         return dword0;
84 }
85
86 static long iSeries_hpte_remove(unsigned long hpte_group)
87 {
88         unsigned long slot_offset;
89         int i;
90         HPTE lhpte;
91
92         /* Pick a random slot to start at */
93         slot_offset = mftb() & 0x7;
94
95         for (i = 0; i < HPTES_PER_GROUP; i++) {
96                 lhpte.dw0.dword0 = 
97                         iSeries_hpte_getword0(hpte_group + slot_offset);
98
99                 if (!lhpte.dw0.dw0.bolted) {
100                         HvCallHpt_invalidateSetSwBitsGet(hpte_group + 
101                                                          slot_offset, 0, 0);
102                         return i;
103                 }
104
105                 slot_offset++;
106                 slot_offset &= 0x7;
107         }
108
109         return -1;
110 }
111
112 /*
113  * The HyperVisor expects the "flags" argument in this form:
114  *      bits  0..59 : reserved
115  *      bit      60 : N
116  *      bits 61..63 : PP2,PP1,PP0
117  */
118 static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp,
119                                   unsigned long va, int large, int local)
120 {
121         HPTE hpte;
122         unsigned long avpn = va >> 23;
123
124         HvCallHpt_get(&hpte, slot);
125         if ((hpte.dw0.dw0.avpn == avpn) && (hpte.dw0.dw0.v)) {
126                 HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1));
127                 return 0;
128         }
129         return -1;
130 }
131
132 /*
133  * Functions used to find the PTE for a particular virtual address. 
134  * Only used during boot when bolting pages.
135  *
136  * Input : vpn      : virtual page number
137  * Output: PTE index within the page table of the entry
138  *         -1 on failure
139  */
140 static long iSeries_hpte_find(unsigned long vpn)
141 {
142         HPTE hpte;
143         long slot;
144
145         /*
146          * The HvCallHpt_findValid interface is as follows:
147          * 0xffffffffffffffff : No entry found.
148          * 0x00000000xxxxxxxx : Entry found in primary group, slot x
149          * 0x80000000xxxxxxxx : Entry found in secondary group, slot x
150          */
151         slot = HvCallHpt_findValid(&hpte, vpn); 
152         if (hpte.dw0.dw0.v) {
153                 if (slot < 0) {
154                         slot &= 0x7fffffffffffffff;
155                         slot = -slot;
156                 }
157         } else
158                 slot = -1;
159         return slot;
160 }
161
162 /*
163  * Update the page protection bits. Intended to be used to create
164  * guard pages for kernel data structures on pages which are bolted
165  * in the HPT. Assumes pages being operated on will not be stolen.
166  * Does not work on large pages.
167  *
168  * No need to lock here because we should be the only user.
169  */
170 static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
171 {
172         unsigned long vsid,va,vpn;
173         long slot;
174
175         vsid = get_kernel_vsid(ea);
176         va = (vsid << 28) | (ea & 0x0fffffff);
177         vpn = va >> PAGE_SHIFT;
178         slot = iSeries_hpte_find(vpn); 
179         if (slot == -1)
180                 panic("updateboltedpp: Could not find page to bolt\n");
181         HvCallHpt_setPp(slot, newpp);
182 }
183
184 static void iSeries_hpte_invalidate(unsigned long slot, unsigned long va,
185                                     int large, int local)
186 {
187         HPTE lhpte;
188         unsigned long avpn = va >> 23;
189
190         lhpte.dw0.dword0 = iSeries_hpte_getword0(slot);
191         
192         if ((lhpte.dw0.dw0.avpn == avpn) && lhpte.dw0.dw0.v)
193                 HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0);
194 }
195
196 void hpte_init_iSeries(void)
197 {
198         ppc_md.hpte_invalidate  = iSeries_hpte_invalidate;
199         ppc_md.hpte_updatepp    = iSeries_hpte_updatepp;
200         ppc_md.hpte_updateboltedpp = iSeries_hpte_updateboltedpp;
201         ppc_md.hpte_insert      = iSeries_hpte_insert;
202         ppc_md.hpte_remove      = iSeries_hpte_remove;
203 }