blob: fbf27c74ebda04c3b0cfecd8bebfd322836699ce [file] [log] [blame]
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +09001/*
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
43static DEFINE_SPINLOCK(beat_htab_lock);
44
45static 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
90static long beat_lpar_hpte_insert(unsigned long hpte_group,
91 unsigned long va, unsigned long pa,
92 unsigned long rflags, unsigned long vflags,
Paul Mackerras1189be62007-10-11 20:37:10 +100093 int psize, int ssize)
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +090094{
95 unsigned long lpar_rc;
96 unsigned long slot;
97 unsigned long hpte_v, hpte_r;
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +090098
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
Paul Mackerras1189be62007-10-11 20:37:10 +1000108 hpte_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M) |
109 vflags | HPTE_V_VALID;
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900110 hpte_r = hpte_encode_r(pa, psize) | rflags;
111
112 if (!(vflags & HPTE_V_BOLTED))
113 DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
114
115 if (rflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
116 hpte_r &= ~_PAGE_COHERENT;
117
Akira Iguchicbca5672007-02-09 16:53:59 +0900118 spin_lock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900119 if ((lpar_rc = beat_read_mask(hpte_group)) == 0) {
120 if (!(vflags & HPTE_V_BOLTED))
121 DBG_LOW(" full\n");
Akira Iguchicbca5672007-02-09 16:53:59 +0900122 spin_unlock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900123 return -1;
124 }
125
126 lpar_rc = beat_insert_htab_entry(0, hpte_group, lpar_rc << 48,
127 hpte_v, hpte_r, &slot);
Akira Iguchicbca5672007-02-09 16:53:59 +0900128 spin_unlock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900129
130 /*
131 * Since we try and ioremap PHBs we don't own, the pte insert
132 * will fail. However we must catch the failure in hash_page
133 * or we will loop forever, so return -2 in this case.
134 */
135 if (unlikely(lpar_rc != 0)) {
136 if (!(vflags & HPTE_V_BOLTED))
137 DBG_LOW(" lpar err %lx\n", lpar_rc);
138 return -2;
139 }
140 if (!(vflags & HPTE_V_BOLTED))
141 DBG_LOW(" -> slot: %lx\n", slot);
142
143 /* We have to pass down the secondary bucket bit here as well */
144 return (slot ^ hpte_group) & 15;
145}
146
147static long beat_lpar_hpte_remove(unsigned long hpte_group)
148{
149 DBG_LOW("hpte_remove(group=%lx)\n", hpte_group);
150 return -1;
151}
152
153static unsigned long beat_lpar_hpte_getword0(unsigned long slot)
154{
155 unsigned long dword0, dword[5];
156 unsigned long lpar_rc;
157
158 lpar_rc = beat_read_htab_entries(0, slot & ~3UL, dword);
159
160 dword0 = dword[slot&3];
161
162 BUG_ON(lpar_rc != 0);
163
164 return dword0;
165}
166
167static void beat_lpar_hptab_clear(void)
168{
169 unsigned long size_bytes = 1UL << ppc64_pft_size;
170 unsigned long hpte_count = size_bytes >> 4;
171 int i;
172 unsigned long dummy0, dummy1;
173
174 /* TODO: Use bulk call */
175 for (i = 0; i < hpte_count; i++)
176 beat_write_htab_entry(0, i, 0, 0, -1UL, -1UL, &dummy0, &dummy1);
177}
178
179/*
180 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
181 * the low 3 bits of flags happen to line up. So no transform is needed.
182 * We can probably optimize here and assume the high bits of newpp are
183 * already zero. For now I am paranoid.
184 */
185static long beat_lpar_hpte_updatepp(unsigned long slot,
186 unsigned long newpp,
187 unsigned long va,
Paul Mackerras1189be62007-10-11 20:37:10 +1000188 int psize, int ssize, int local)
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900189{
190 unsigned long lpar_rc;
191 unsigned long dummy0, dummy1, want_v;
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900192
Paul Mackerras1189be62007-10-11 20:37:10 +1000193 want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900194
195 DBG_LOW(" update: "
196 "avpnv=%016lx, slot=%016lx, psize: %d, newpp %016lx ... ",
197 want_v & HPTE_V_AVPN, slot, psize, newpp);
198
Akira Iguchicbca5672007-02-09 16:53:59 +0900199 spin_lock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900200 dummy0 = beat_lpar_hpte_getword0(slot);
201 if ((dummy0 & ~0x7FUL) != (want_v & ~0x7FUL)) {
202 DBG_LOW("not found !\n");
Akira Iguchicbca5672007-02-09 16:53:59 +0900203 spin_unlock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900204 return -1;
205 }
206
207 lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7, &dummy0,
208 &dummy1);
Akira Iguchicbca5672007-02-09 16:53:59 +0900209 spin_unlock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900210 if (lpar_rc != 0 || dummy0 == 0) {
211 DBG_LOW("not found !\n");
212 return -1;
213 }
214
215 DBG_LOW("ok %lx %lx\n", dummy0, dummy1);
216
217 BUG_ON(lpar_rc != 0);
218
219 return 0;
220}
221
222static long beat_lpar_hpte_find(unsigned long va, int psize)
223{
224 unsigned long hash;
225 unsigned long i, j;
226 long slot;
227 unsigned long want_v, hpte_v;
228
Paul Mackerras1189be62007-10-11 20:37:10 +1000229 hash = hpt_hash(va, mmu_psize_defs[psize].shift, MMU_SEGSIZE_256M);
230 want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900231
232 for (j = 0; j < 2; j++) {
233 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
234 for (i = 0; i < HPTES_PER_GROUP; i++) {
235 hpte_v = beat_lpar_hpte_getword0(slot);
236
237 if (HPTE_V_COMPARE(hpte_v, want_v)
238 && (hpte_v & HPTE_V_VALID)
239 && (!!(hpte_v & HPTE_V_SECONDARY) == j)) {
240 /* HPTE matches */
241 if (j)
242 slot = -slot;
243 return slot;
244 }
245 ++slot;
246 }
247 hash = ~hash;
248 }
249
250 return -1;
251}
252
253static void beat_lpar_hpte_updateboltedpp(unsigned long newpp,
254 unsigned long ea,
Paul Mackerras1189be62007-10-11 20:37:10 +1000255 int psize, int ssize)
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900256{
257 unsigned long lpar_rc, slot, vsid, va, dummy0, dummy1;
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900258
Paul Mackerras1189be62007-10-11 20:37:10 +1000259 vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900260 va = (vsid << 28) | (ea & 0x0fffffff);
261
Akira Iguchicbca5672007-02-09 16:53:59 +0900262 spin_lock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900263 slot = beat_lpar_hpte_find(va, psize);
264 BUG_ON(slot == -1);
265
266 lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7,
267 &dummy0, &dummy1);
Akira Iguchicbca5672007-02-09 16:53:59 +0900268 spin_unlock(&beat_htab_lock);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900269
270 BUG_ON(lpar_rc != 0);
271}
272
273static void beat_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
Paul Mackerras1189be62007-10-11 20:37:10 +1000274 int psize, int ssize, int local)
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900275{
276 unsigned long want_v;
277 unsigned long lpar_rc;
278 unsigned long dummy1, dummy2;
279 unsigned long flags;
280
281 DBG_LOW(" inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
282 slot, va, psize, local);
Paul Mackerras1189be62007-10-11 20:37:10 +1000283 want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
Ishizaki Koufe4a0cf2007-02-02 16:42:28 +0900284
285 spin_lock_irqsave(&beat_htab_lock, flags);
286 dummy1 = beat_lpar_hpte_getword0(slot);
287
288 if ((dummy1 & ~0x7FUL) != (want_v & ~0x7FUL)) {
289 DBG_LOW("not found !\n");
290 spin_unlock_irqrestore(&beat_htab_lock, flags);
291 return;
292 }
293
294 lpar_rc = beat_write_htab_entry(0, slot, 0, 0, HPTE_V_VALID, 0,
295 &dummy1, &dummy2);
296 spin_unlock_irqrestore(&beat_htab_lock, flags);
297
298 BUG_ON(lpar_rc != 0);
299}
300
301void __init hpte_init_beat(void)
302{
303 ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate;
304 ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp;
305 ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
306 ppc_md.hpte_insert = beat_lpar_hpte_insert;
307 ppc_md.hpte_remove = beat_lpar_hpte_remove;
308 ppc_md.hpte_clear_all = beat_lpar_hptab_clear;
309}
Ishizaki Kou7f2c8572007-10-02 18:23:46 +1000310
311static long beat_lpar_hpte_insert_v3(unsigned long hpte_group,
312 unsigned long va, unsigned long pa,
313 unsigned long rflags, unsigned long vflags,
Paul Mackerras1189be62007-10-11 20:37:10 +1000314 int psize, int ssize)
Ishizaki Kou7f2c8572007-10-02 18:23:46 +1000315{
316 unsigned long lpar_rc;
317 unsigned long slot;
318 unsigned long hpte_v, hpte_r;
319
320 /* same as iseries */
321 if (vflags & HPTE_V_SECONDARY)
322 return -1;
323
324 if (!(vflags & HPTE_V_BOLTED))
325 DBG_LOW("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
326 "rflags=%lx, vflags=%lx, psize=%d)\n",
327 hpte_group, va, pa, rflags, vflags, psize);
328
Paul Mackerras1189be62007-10-11 20:37:10 +1000329 hpte_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M) |
330 vflags | HPTE_V_VALID;
Ishizaki Kou7f2c8572007-10-02 18:23:46 +1000331 hpte_r = hpte_encode_r(pa, psize) | rflags;
332
333 if (!(vflags & HPTE_V_BOLTED))
334 DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
335
336 if (rflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
337 hpte_r &= ~_PAGE_COHERENT;
338
339 /* insert into not-volted entry */
340 lpar_rc = beat_insert_htab_entry3(0, hpte_group, hpte_v, hpte_r,
341 HPTE_V_BOLTED, 0, &slot);
342 /*
343 * Since we try and ioremap PHBs we don't own, the pte insert
344 * will fail. However we must catch the failure in hash_page
345 * or we will loop forever, so return -2 in this case.
346 */
347 if (unlikely(lpar_rc != 0)) {
348 if (!(vflags & HPTE_V_BOLTED))
349 DBG_LOW(" lpar err %lx\n", lpar_rc);
350 return -2;
351 }
352 if (!(vflags & HPTE_V_BOLTED))
353 DBG_LOW(" -> slot: %lx\n", slot);
354
355 /* We have to pass down the secondary bucket bit here as well */
356 return (slot ^ hpte_group) & 15;
357}
358
359/*
360 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
361 * the low 3 bits of flags happen to line up. So no transform is needed.
362 * We can probably optimize here and assume the high bits of newpp are
363 * already zero. For now I am paranoid.
364 */
365static long beat_lpar_hpte_updatepp_v3(unsigned long slot,
366 unsigned long newpp,
367 unsigned long va,
Paul Mackerras1189be62007-10-11 20:37:10 +1000368 int psize, int ssize, int local)
Ishizaki Kou7f2c8572007-10-02 18:23:46 +1000369{
370 unsigned long lpar_rc;
371 unsigned long want_v;
372 unsigned long pss;
373
Paul Mackerras1189be62007-10-11 20:37:10 +1000374 want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
Ishizaki Kou7f2c8572007-10-02 18:23:46 +1000375 pss = (psize == MMU_PAGE_4K) ? -1UL : mmu_psize_defs[psize].penc;
376
377 DBG_LOW(" update: "
378 "avpnv=%016lx, slot=%016lx, psize: %d, newpp %016lx ... ",
379 want_v & HPTE_V_AVPN, slot, psize, newpp);
380
381 lpar_rc = beat_update_htab_permission3(0, slot, want_v, pss, 7, newpp);
382
383 if (lpar_rc == 0xfffffff7) {
384 DBG_LOW("not found !\n");
385 return -1;
386 }
387
388 DBG_LOW("ok\n");
389
390 BUG_ON(lpar_rc != 0);
391
392 return 0;
393}
394
395static void beat_lpar_hpte_invalidate_v3(unsigned long slot, unsigned long va,
Paul Mackerras1189be62007-10-11 20:37:10 +1000396 int psize, int ssize, int local)
Ishizaki Kou7f2c8572007-10-02 18:23:46 +1000397{
398 unsigned long want_v;
399 unsigned long lpar_rc;
400 unsigned long pss;
401
402 DBG_LOW(" inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
403 slot, va, psize, local);
Paul Mackerras1189be62007-10-11 20:37:10 +1000404 want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
Ishizaki Kou7f2c8572007-10-02 18:23:46 +1000405 pss = (psize == MMU_PAGE_4K) ? -1UL : mmu_psize_defs[psize].penc;
406
407 lpar_rc = beat_invalidate_htab_entry3(0, slot, want_v, pss);
408
409 /* E_busy can be valid output: page may be already replaced */
410 BUG_ON(lpar_rc != 0 && lpar_rc != 0xfffffff7);
411}
412
413static int64_t _beat_lpar_hptab_clear_v3(void)
414{
415 return beat_clear_htab3(0);
416}
417
418static void beat_lpar_hptab_clear_v3(void)
419{
420 _beat_lpar_hptab_clear_v3();
421}
422
423void __init hpte_init_beat_v3(void)
424{
425 if (_beat_lpar_hptab_clear_v3() == 0) {
426 ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate_v3;
427 ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp_v3;
428 ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
429 ppc_md.hpte_insert = beat_lpar_hpte_insert_v3;
430 ppc_md.hpte_remove = beat_lpar_hpte_remove;
431 ppc_md.hpte_clear_all = beat_lpar_hptab_clear_v3;
432 } else {
433 ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate;
434 ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp;
435 ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
436 ppc_md.hpte_insert = beat_lpar_hpte_insert;
437 ppc_md.hpte_remove = beat_lpar_hpte_remove;
438 ppc_md.hpte_clear_all = beat_lpar_hptab_clear;
439 }
440}