Merge branch 'master' into for_paulus
[linux-drm-fsl-dcu.git] / arch / powerpc / platforms / celleb / htab.c
1 /*
2  * "Cell Reference Set" HTAB support.
3  *
4  * (C) Copyright 2006-2007 TOSHIBA CORPORATION
5  *
6  * This code is based on arch/powerpc/platforms/pseries/lpar.c:
7  *  Copyright (C) 2001 Todd Inglett, IBM Corporation
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #undef DEBUG_LOW
25
26 #include <linux/kernel.h>
27 #include <linux/spinlock.h>
28
29 #include <asm/mmu.h>
30 #include <asm/page.h>
31 #include <asm/pgtable.h>
32 #include <asm/machdep.h>
33 #include <asm/udbg.h>
34
35 #include "beat_wrapper.h"
36
37 #ifdef DEBUG_LOW
38 #define DBG_LOW(fmt...) do { udbg_printf(fmt); } while(0)
39 #else
40 #define DBG_LOW(fmt...) do { } while(0)
41 #endif
42
43 static DEFINE_SPINLOCK(beat_htab_lock);
44
45 static inline unsigned int beat_read_mask(unsigned hpte_group)
46 {
47         unsigned long hpte_v[5];
48         unsigned long rmask = 0;
49
50         beat_read_htab_entries(0, hpte_group + 0, hpte_v);
51         if (!(hpte_v[0] & HPTE_V_BOLTED))
52                 rmask |= 0x8000;
53         if (!(hpte_v[1] & HPTE_V_BOLTED))
54                 rmask |= 0x4000;
55         if (!(hpte_v[2] & HPTE_V_BOLTED))
56                 rmask |= 0x2000;
57         if (!(hpte_v[3] & HPTE_V_BOLTED))
58                 rmask |= 0x1000;
59         beat_read_htab_entries(0, hpte_group + 4, hpte_v);
60         if (!(hpte_v[0] & HPTE_V_BOLTED))
61                 rmask |= 0x0800;
62         if (!(hpte_v[1] & HPTE_V_BOLTED))
63                 rmask |= 0x0400;
64         if (!(hpte_v[2] & HPTE_V_BOLTED))
65                 rmask |= 0x0200;
66         if (!(hpte_v[3] & HPTE_V_BOLTED))
67                 rmask |= 0x0100;
68         hpte_group = ~hpte_group & (htab_hash_mask * HPTES_PER_GROUP);
69         beat_read_htab_entries(0, hpte_group + 0, hpte_v);
70         if (!(hpte_v[0] & HPTE_V_BOLTED))
71                 rmask |= 0x80;
72         if (!(hpte_v[1] & HPTE_V_BOLTED))
73                 rmask |= 0x40;
74         if (!(hpte_v[2] & HPTE_V_BOLTED))
75                 rmask |= 0x20;
76         if (!(hpte_v[3] & HPTE_V_BOLTED))
77                 rmask |= 0x10;
78         beat_read_htab_entries(0, hpte_group + 4, hpte_v);
79         if (!(hpte_v[0] & HPTE_V_BOLTED))
80                 rmask |= 0x08;
81         if (!(hpte_v[1] & HPTE_V_BOLTED))
82                 rmask |= 0x04;
83         if (!(hpte_v[2] & HPTE_V_BOLTED))
84                 rmask |= 0x02;
85         if (!(hpte_v[3] & HPTE_V_BOLTED))
86                 rmask |= 0x01;
87         return rmask;
88 }
89
90 static long beat_lpar_hpte_insert(unsigned long hpte_group,
91                                   unsigned long va, unsigned long pa,
92                                   unsigned long rflags, unsigned long vflags,
93                                   int psize)
94 {
95         unsigned long lpar_rc;
96         unsigned long slot;
97         unsigned long hpte_v, hpte_r;
98
99         /* same as iseries */
100         if (vflags & HPTE_V_SECONDARY)
101                 return -1;
102
103         if (!(vflags & HPTE_V_BOLTED))
104                 DBG_LOW("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
105                         "rflags=%lx, vflags=%lx, psize=%d)\n",
106                 hpte_group, va, pa, rflags, vflags, psize);
107
108         hpte_v = hpte_encode_v(va, psize) | vflags | HPTE_V_VALID;
109         hpte_r = hpte_encode_r(pa, psize) | rflags;
110
111         if (!(vflags & HPTE_V_BOLTED))
112                 DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
113
114         if (rflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
115                 hpte_r &= ~_PAGE_COHERENT;
116
117         spin_lock(&beat_htab_lock);
118         if ((lpar_rc = beat_read_mask(hpte_group)) == 0) {
119                 if (!(vflags & HPTE_V_BOLTED))
120                         DBG_LOW(" full\n");
121                 spin_unlock(&beat_htab_lock);
122                 return -1;
123         }
124
125         lpar_rc = beat_insert_htab_entry(0, hpte_group, lpar_rc << 48,
126                 hpte_v, hpte_r, &slot);
127         spin_unlock(&beat_htab_lock);
128
129         /*
130          * Since we try and ioremap PHBs we don't own, the pte insert
131          * will fail. However we must catch the failure in hash_page
132          * or we will loop forever, so return -2 in this case.
133          */
134         if (unlikely(lpar_rc != 0)) {
135                 if (!(vflags & HPTE_V_BOLTED))
136                         DBG_LOW(" lpar err %lx\n", lpar_rc);
137                 return -2;
138         }
139         if (!(vflags & HPTE_V_BOLTED))
140                 DBG_LOW(" -> slot: %lx\n", slot);
141
142         /* We have to pass down the secondary bucket bit here as well */
143         return (slot ^ hpte_group) & 15;
144 }
145
146 static long beat_lpar_hpte_remove(unsigned long hpte_group)
147 {
148         DBG_LOW("hpte_remove(group=%lx)\n", hpte_group);
149         return -1;
150 }
151
152 static unsigned long beat_lpar_hpte_getword0(unsigned long slot)
153 {
154         unsigned long dword0, dword[5];
155         unsigned long lpar_rc;
156
157         lpar_rc = beat_read_htab_entries(0, slot & ~3UL, dword);
158
159         dword0 = dword[slot&3];
160
161         BUG_ON(lpar_rc != 0);
162
163         return dword0;
164 }
165
166 static void beat_lpar_hptab_clear(void)
167 {
168         unsigned long size_bytes = 1UL << ppc64_pft_size;
169         unsigned long hpte_count = size_bytes >> 4;
170         int i;
171         unsigned long dummy0, dummy1;
172
173         /* TODO: Use bulk call */
174         for (i = 0; i < hpte_count; i++)
175                 beat_write_htab_entry(0, i, 0, 0, -1UL, -1UL, &dummy0, &dummy1);
176 }
177
178 /*
179  * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
180  * the low 3 bits of flags happen to line up.  So no transform is needed.
181  * We can probably optimize here and assume the high bits of newpp are
182  * already zero.  For now I am paranoid.
183  */
184 static long beat_lpar_hpte_updatepp(unsigned long slot,
185                                     unsigned long newpp,
186                                     unsigned long va,
187                                     int psize, int local)
188 {
189         unsigned long lpar_rc;
190         unsigned long dummy0, dummy1, want_v;
191
192         want_v = hpte_encode_v(va, psize);
193
194         DBG_LOW("    update: "
195                 "avpnv=%016lx, slot=%016lx, psize: %d, newpp %016lx ... ",
196                 want_v & HPTE_V_AVPN, slot, psize, newpp);
197
198         spin_lock(&beat_htab_lock);
199         dummy0 = beat_lpar_hpte_getword0(slot);
200         if ((dummy0 & ~0x7FUL) != (want_v & ~0x7FUL)) {
201                 DBG_LOW("not found !\n");
202                 spin_unlock(&beat_htab_lock);
203                 return -1;
204         }
205
206         lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7, &dummy0,
207                                         &dummy1);
208         spin_unlock(&beat_htab_lock);
209         if (lpar_rc != 0 || dummy0 == 0) {
210                 DBG_LOW("not found !\n");
211                 return -1;
212         }
213
214         DBG_LOW("ok %lx %lx\n", dummy0, dummy1);
215
216         BUG_ON(lpar_rc != 0);
217
218         return 0;
219 }
220
221 static long beat_lpar_hpte_find(unsigned long va, int psize)
222 {
223         unsigned long hash;
224         unsigned long i, j;
225         long slot;
226         unsigned long want_v, hpte_v;
227
228         hash = hpt_hash(va, mmu_psize_defs[psize].shift);
229         want_v = hpte_encode_v(va, psize);
230
231         for (j = 0; j < 2; j++) {
232                 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
233                 for (i = 0; i < HPTES_PER_GROUP; i++) {
234                         hpte_v = beat_lpar_hpte_getword0(slot);
235
236                         if (HPTE_V_COMPARE(hpte_v, want_v)
237                             && (hpte_v & HPTE_V_VALID)
238                             && (!!(hpte_v & HPTE_V_SECONDARY) == j)) {
239                                 /* HPTE matches */
240                                 if (j)
241                                         slot = -slot;
242                                 return slot;
243                         }
244                         ++slot;
245                 }
246                 hash = ~hash;
247         }
248
249         return -1;
250 }
251
252 static void beat_lpar_hpte_updateboltedpp(unsigned long newpp,
253                                           unsigned long ea,
254                                           int psize)
255 {
256         unsigned long lpar_rc, slot, vsid, va, dummy0, dummy1;
257
258         vsid = get_kernel_vsid(ea);
259         va = (vsid << 28) | (ea & 0x0fffffff);
260
261         spin_lock(&beat_htab_lock);
262         slot = beat_lpar_hpte_find(va, psize);
263         BUG_ON(slot == -1);
264
265         lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7,
266                 &dummy0, &dummy1);
267         spin_unlock(&beat_htab_lock);
268
269         BUG_ON(lpar_rc != 0);
270 }
271
272 static void beat_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
273                                          int psize, int local)
274 {
275         unsigned long want_v;
276         unsigned long lpar_rc;
277         unsigned long dummy1, dummy2;
278         unsigned long flags;
279
280         DBG_LOW("    inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
281                 slot, va, psize, local);
282         want_v = hpte_encode_v(va, psize);
283
284         spin_lock_irqsave(&beat_htab_lock, flags);
285         dummy1 = beat_lpar_hpte_getword0(slot);
286
287         if ((dummy1 & ~0x7FUL) != (want_v & ~0x7FUL)) {
288                 DBG_LOW("not found !\n");
289                 spin_unlock_irqrestore(&beat_htab_lock, flags);
290                 return;
291         }
292
293         lpar_rc = beat_write_htab_entry(0, slot, 0, 0, HPTE_V_VALID, 0,
294                 &dummy1, &dummy2);
295         spin_unlock_irqrestore(&beat_htab_lock, flags);
296
297         BUG_ON(lpar_rc != 0);
298 }
299
300 void __init hpte_init_beat(void)
301 {
302         ppc_md.hpte_invalidate  = beat_lpar_hpte_invalidate;
303         ppc_md.hpte_updatepp    = beat_lpar_hpte_updatepp;
304         ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
305         ppc_md.hpte_insert      = beat_lpar_hpte_insert;
306         ppc_md.hpte_remove      = beat_lpar_hpte_remove;
307         ppc_md.hpte_clear_all   = beat_lpar_hptab_clear;
308 }