[POWERPC] PS3: Move system bus to platform directory
[safe/jmp/linux-2.6] / arch / powerpc / platforms / ps3 / htab.c
1 /*
2  *  PS3 pagetable management routines.
3  *
4  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
5  *  Copyright 2006 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/kernel.h>
22
23 #include <asm/machdep.h>
24 #include <asm/lmb.h>
25 #include <asm/udbg.h>
26 #include <asm/lv1call.h>
27
28 #include "platform.h"
29
30 #if defined(DEBUG)
31 #define DBG(fmt...) udbg_printf(fmt)
32 #else
33 #define DBG(fmt...) do{if(0)printk(fmt);}while(0)
34 #endif
35
36 static hpte_t *htab;
37 static unsigned long htab_addr;
38 static unsigned char *bolttab;
39 static unsigned char *inusetab;
40
41 static spinlock_t ps3_bolttab_lock = SPIN_LOCK_UNLOCKED;
42
43 #define debug_dump_hpte(_a, _b, _c, _d, _e, _f, _g) \
44         _debug_dump_hpte(_a, _b, _c, _d, _e, _f, _g, __func__, __LINE__)
45 static void _debug_dump_hpte(unsigned long pa, unsigned long va,
46         unsigned long group, unsigned long bitmap, hpte_t lhpte, int psize,
47         unsigned long slot, const char* func, int line)
48 {
49         DBG("%s:%d: pa     = %lxh\n", func, line, pa);
50         DBG("%s:%d: lpar   = %lxh\n", func, line,
51                 ps3_mm_phys_to_lpar(pa));
52         DBG("%s:%d: va     = %lxh\n", func, line, va);
53         DBG("%s:%d: group  = %lxh\n", func, line, group);
54         DBG("%s:%d: bitmap = %lxh\n", func, line, bitmap);
55         DBG("%s:%d: hpte.v = %lxh\n", func, line, lhpte.v);
56         DBG("%s:%d: hpte.r = %lxh\n", func, line, lhpte.r);
57         DBG("%s:%d: psize  = %xh\n", func, line, psize);
58         DBG("%s:%d: slot   = %lxh\n", func, line, slot);
59 }
60
61 static long ps3_hpte_insert(unsigned long hpte_group, unsigned long va,
62         unsigned long pa, unsigned long rflags, unsigned long vflags, int psize)
63 {
64         unsigned long slot;
65         hpte_t lhpte;
66         int secondary = 0;
67         unsigned long result;
68         unsigned long bitmap;
69         unsigned long flags;
70         unsigned long p_pteg, s_pteg, b_index, b_mask, cb, ci;
71
72         vflags &= ~HPTE_V_SECONDARY; /* this bit is ignored */
73
74         lhpte.v = hpte_encode_v(va, psize) | vflags | HPTE_V_VALID;
75         lhpte.r = hpte_encode_r(ps3_mm_phys_to_lpar(pa), psize) | rflags;
76
77         p_pteg = hpte_group / HPTES_PER_GROUP;
78         s_pteg = ~p_pteg & htab_hash_mask;
79
80         spin_lock_irqsave(&ps3_bolttab_lock, flags);
81
82         BUG_ON(bolttab[p_pteg] == 0xff && bolttab[s_pteg] == 0xff);
83
84         bitmap = (inusetab[p_pteg] << 8) | inusetab[s_pteg];
85
86         if (bitmap == 0xffff) {
87                 /*
88                  * PTEG is full. Search for victim.
89                  */
90                 bitmap &= ~((bolttab[p_pteg] << 8) | bolttab[s_pteg]);
91                 do {
92                         ci = mftb() & 15;
93                         cb = 0x8000UL >> ci;
94                 } while ((cb & bitmap) == 0);
95         } else {
96                 /*
97                  * search free slot in hardware order
98                  *      [primary]       0, 2, 4, 6, 1, 3, 5, 7
99                  *      [secondary]     0, 2, 4, 6, 1, 3, 5, 7
100                  */
101                 for (ci = 0; ci < HPTES_PER_GROUP; ci += 2) {
102                         cb = 0x8000UL >> ci;
103                         if ((cb & bitmap) == 0)
104                                 goto found;
105                 }
106                 for (ci = 1; ci < HPTES_PER_GROUP; ci += 2) {
107                         cb = 0x8000UL >> ci;
108                         if ((cb & bitmap) == 0)
109                                 goto found;
110                 }
111                 for (ci = HPTES_PER_GROUP; ci < HPTES_PER_GROUP*2; ci += 2) {
112                         cb = 0x8000UL >> ci;
113                         if ((cb & bitmap) == 0)
114                                 goto found;
115                 }
116                 for (ci = HPTES_PER_GROUP+1; ci < HPTES_PER_GROUP*2; ci += 2) {
117                         cb = 0x8000UL >> ci;
118                         if ((cb & bitmap) == 0)
119                                 goto found;
120                 }
121         }
122
123 found:
124         if (ci < HPTES_PER_GROUP) {
125                 slot = p_pteg * HPTES_PER_GROUP + ci;
126         } else {
127                 slot = s_pteg * HPTES_PER_GROUP + (ci & 7);
128                 /* lhpte.dw0.dw0.h = 1; */
129                 vflags |= HPTE_V_SECONDARY;
130                 lhpte.v |= HPTE_V_SECONDARY;
131         }
132
133         result = lv1_write_htab_entry(0, slot, lhpte.v, lhpte.r);
134
135         if (result) {
136                 debug_dump_hpte(pa, va, hpte_group, bitmap, lhpte, psize, slot);
137                 BUG();
138         }
139
140         /*
141          * If used slot is not in primary HPTE group,
142          * the slot should be in secondary HPTE group.
143          */
144
145         if ((hpte_group ^ slot) & ~(HPTES_PER_GROUP - 1)) {
146                 secondary = 1;
147                 b_index = s_pteg;
148         } else {
149                 secondary = 0;
150                 b_index = p_pteg;
151         }
152
153         b_mask = (lhpte.v & HPTE_V_BOLTED) ? 1 << 7 : 0 << 7;
154         bolttab[b_index] |= b_mask >> (slot & 7);
155         b_mask = 1 << 7;
156         inusetab[b_index] |= b_mask >> (slot & 7);
157         spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
158
159         return (slot & 7) | (secondary << 3);
160 }
161
162 static long ps3_hpte_remove(unsigned long hpte_group)
163 {
164         panic("ps3_hpte_remove() not implemented");
165         return 0;
166 }
167
168 static long ps3_hpte_updatepp(unsigned long slot, unsigned long newpp,
169         unsigned long va, int psize, int local)
170 {
171         unsigned long flags;
172         unsigned long result;
173         unsigned long pteg, bit;
174         unsigned long hpte_v, want_v;
175
176         want_v = hpte_encode_v(va, psize);
177
178         spin_lock_irqsave(&ps3_bolttab_lock, flags);
179
180         hpte_v = htab[slot].v;
181         if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
182                 spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
183
184                 /* ps3_hpte_insert() will be used to update PTE */
185                 return -1;
186         }
187
188         result = lv1_write_htab_entry(0, slot, 0, 0);
189
190         if (result) {
191                 DBG("%s: va=%lx slot=%lx psize=%d result = %ld (0x%lx)\n",
192                        __func__, va, slot, psize, result, result);
193                 BUG();
194         }
195
196         pteg = slot / HPTES_PER_GROUP;
197         bit = slot % HPTES_PER_GROUP;
198         inusetab[pteg] &= ~(0x80 >> bit);
199
200         spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
201
202         /* ps3_hpte_insert() will be used to update PTE */
203         return -1;
204 }
205
206 static void ps3_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
207         int psize)
208 {
209         panic("ps3_hpte_updateboltedpp() not implemented");
210 }
211
212 static void ps3_hpte_invalidate(unsigned long slot, unsigned long va,
213         int psize, int local)
214 {
215         unsigned long flags;
216         unsigned long result;
217         unsigned long pteg, bit;
218
219         spin_lock_irqsave(&ps3_bolttab_lock, flags);
220         result = lv1_write_htab_entry(0, slot, 0, 0);
221
222         if (result) {
223                 DBG("%s: va=%lx slot=%lx psize=%d result = %ld (0x%lx)\n",
224                        __func__, va, slot, psize, result, result);
225                 BUG();
226         }
227
228         pteg = slot / HPTES_PER_GROUP;
229         bit = slot % HPTES_PER_GROUP;
230         inusetab[pteg] &= ~(0x80 >> bit);
231         spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
232 }
233
234 static void ps3_hpte_clear(void)
235 {
236         lv1_unmap_htab(htab_addr);
237 }
238
239 void __init ps3_hpte_init(unsigned long htab_size)
240 {
241         long bitmap_size;
242
243         DBG(" -> %s:%d\n", __func__, __LINE__);
244
245         ppc_md.hpte_invalidate = ps3_hpte_invalidate;
246         ppc_md.hpte_updatepp = ps3_hpte_updatepp;
247         ppc_md.hpte_updateboltedpp = ps3_hpte_updateboltedpp;
248         ppc_md.hpte_insert = ps3_hpte_insert;
249         ppc_md.hpte_remove = ps3_hpte_remove;
250         ppc_md.hpte_clear_all = ps3_hpte_clear;
251
252         ppc64_pft_size = __ilog2(htab_size);
253
254         bitmap_size = htab_size / sizeof(hpte_t) / 8;
255
256         bolttab = __va(lmb_alloc(bitmap_size, 1));
257         inusetab = __va(lmb_alloc(bitmap_size, 1));
258
259         memset(bolttab, 0, bitmap_size);
260         memset(inusetab, 0, bitmap_size);
261
262         DBG(" <- %s:%d\n", __func__, __LINE__);
263 }
264
265 void __init ps3_map_htab(void)
266 {
267         long result;
268         unsigned long htab_size = (1UL << ppc64_pft_size);
269
270         result = lv1_map_htab(0, &htab_addr);
271
272         htab = (hpte_t *)__ioremap(htab_addr, htab_size, PAGE_READONLY_X);
273
274         DBG("%s:%d: lpar %016lxh, virt %016lxh\n", __func__, __LINE__,
275                 htab_addr, (unsigned long)htab);
276 }