1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/* $OpenBSD: tcc.c,v 1.8 2015/12/23 11:45:24 visa Exp $ */
/*
* Copyright (c) 2012 Miodrag Vallat.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* POWER Indigo2 TBus Cache Controller (Stream Cache) support code.
*/
#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <uvm/uvm_extern.h>
#include <mips64/archtype.h>
#include <mips64/cache.h>
#include <machine/autoconf.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <mips64/mips_cpu.h>
#include <sgi/sgi/ip22.h>
#include <sgi/localbus/tccreg.h>
#include <sgi/localbus/tccvar.h>
int tcc_match(struct device *, void *, void *);
void tcc_attach(struct device *, struct device *, void *);
const struct cfattach tcc_ca = {
sizeof(struct device), tcc_match, tcc_attach
};
struct cfdriver tcc_cd = {
NULL, "tcc", DV_DULL
};
uint32_t tcc_bus_error(uint32_t, struct trap_frame *);
CACHE_PROTOS(tcc)
int
tcc_match(struct device *parent, void *match, void *aux)
{
struct mainbus_attach_args *maa = (void *)aux;
switch (sys_config.system_type) {
case SGI_IP26:
return strcmp(maa->maa_name, tcc_cd.cd_name) == 0;
default:
return 0;
}
}
void
tcc_attach(struct device *parent, struct device *self, void *aux)
{
uint32_t ctrl, rev;
ctrl = (uint32_t)tcc_read(TCC_GCACHE_CTRL);
rev = (ctrl & TCC_GCACHE_REV_MASK) >> TCC_GCACHE_REV_SHIFT;
printf(": streaming cache revision %d\n", rev);
tcc_bus_reset();
/* Enable bus error and machine check interrupts. */
set_intr(INTPRI_BUSERR_TCC, CR_INT_4, tcc_bus_error);
tcc_write(TCC_INTR, TCC_INTR_MCHECK_ENAB | TCC_INTR_BERR_ENAB);
/* Enable all cache sets. */
tcc_write(TCC_GCACHE_CTRL, (ctrl | TCC_GCACHE_SET_ALL) &
~TCC_GCACHE_DISABLE_WB);
/* Enable prefetching. */
tcc_prefetch_enable();
}
void
tcc_bus_reset()
{
tcc_write(TCC_INTR, (tcc_read(TCC_INTR) & TCC_INTR_ENABLE_MASK) |
TCC_INTR_MCHECK | TCC_INTR_BERR);
tcc_write(TCC_ERROR, TCC_ERROR_NESTED_MCHECK | TCC_ERROR_NESTED_BERR);
}
uint32_t
tcc_bus_error(uint32_t hwpend, struct trap_frame *tf)
{
uint64_t intr, error, addr, errack;
unsigned int errtype;
intr = tcc_read(TCC_INTR);
error = tcc_read(TCC_ERROR);
errtype = (error & TCC_ERROR_TYPE_MASK) >> TCC_ERROR_TYPE_SHIFT;
/*
* Execution of the `sync' instruction is not supported by the
* T-Bus and raises a machine check exception.
* Do not report anything on console in that case, so that
* userland does not suffer too much.
*/
if (errtype != TCC_ERROR_TYPE_TBUS || (intr & TCC_INTR_MCHECK) == 0) {
addr = tcc_read(TCC_BERR_ADDR);
printf("tcc bus error: intr %llx error %llx (%u) addr %08llx\n",
intr, error, errtype, addr);
}
/* Ack error condition */
errack = 0;
if (intr & TCC_INTR_MCHECK)
errack |= TCC_ERROR_NESTED_MCHECK;
if (intr & TCC_INTR_BERR)
errack |= TCC_ERROR_NESTED_BERR;
tcc_write(TCC_INTR, (intr & TCC_INTR_ENABLE_MASK) |
(intr & (TCC_INTR_MCHECK | TCC_INTR_BERR)));
tcc_write(TCC_ERROR, errack);
return hwpend;
}
/*
* Cache maintainance routines
*/
#define tcc_cache_hit(addr,op) \
__asm__ volatile ("lw $0, %0(%1)" :: "i" (TCC_CACHEOP_HIT), \
"r" (PHYS_TO_XKPHYS(TCC_CACHEOP_BASE | (addr) | (op), CCA_NC)) : "memory")
#define tcc_cache_index(s,i,op) \
__asm__ volatile ("lw $0, %0(%1)" :: "i" (TCC_CACHEOP_INDEX | (op)), \
"r" (PHYS_TO_XKPHYS(TCC_CACHEOP_BASE | ((s) << TCC_CACHEOP_SET_SHIFT) | \
((i) << TCC_CACHEOP_INDEX_SHIFT), CCA_NC)) : "memory")
void tcc_virtual(struct cpu_info *, vaddr_t, vsize_t, uint64_t);
void
tcc_ConfigCache(struct cpu_info *ci)
{
struct cache_info l2;
l2 = ci->ci_l2;
tfp_ConfigCache(ci);
if (l2.size != 0) {
ci->ci_l2 = l2;
ci->ci_SyncCache = tcc_SyncCache;
ci->ci_SyncDCachePage = tcc_SyncDCachePage;
ci->ci_HitSyncDCache = tcc_HitSyncDCache;
ci->ci_HitInvalidateDCache = tcc_HitInvalidateDCache;
ci->ci_IOSyncDCache = tcc_IOSyncDCache;
}
}
void
tcc_SyncCache(struct cpu_info *ci)
{
uint64_t idx;
mips_sync();
tcc_prefetch_invalidate();
tfp_InvalidateICache(ci, 0, ci->ci_l1inst.size);
/*
* The following relies upon the fact that the (line, set)
* fields are contiguous. Therefore by pretending there is
* a huge number of sets and only one line, we can span the
* whole cache.
*/
idx = (uint64_t)ci->ci_l2.size / TCC_CACHE_LINE;
while (idx != 0) {
idx--;
tcc_cache_index(idx, 0,
TCC_CACHEOP_WRITEBACK | TCC_CACHEOP_INVALIDATE);
}
tcc_prefetch_invalidate();
}
void
tcc_SyncDCachePage(struct cpu_info *ci, vaddr_t va, paddr_t pa)
{
vaddr_t epa;
mips_sync();
tcc_prefetch_invalidate();
epa = pa + PAGE_SIZE;
do {
tcc_cache_hit(pa,
TCC_CACHEOP_WRITEBACK | TCC_CACHEOP_INVALIDATE);
pa += TCC_CACHE_LINE;
} while (pa != epa);
tcc_prefetch_invalidate();
}
void
tcc_virtual(struct cpu_info *ci, vaddr_t va, vsize_t sz, uint64_t op)
{
paddr_t pa;
if (IS_XKPHYS(va)) {
pa = XKPHYS_TO_PHYS(va);
while (sz != 0) {
tcc_cache_hit(pa, op);
pa += TCC_CACHE_LINE;
sz -= TCC_CACHE_LINE;
}
return;
}
while (sz != 0) {
/* get the proper physical address */
if (pmap_extract(pmap_kernel(), va, &pa) == 0) {
#ifdef DIAGNOSTIC
panic("%s: invalid va %p", __func__, (void *)va);
#else
/* should not happen */
#endif
}
while (sz != 0) {
tcc_cache_hit(pa, op);
pa += TCC_CACHE_LINE;
va += TCC_CACHE_LINE;
sz -= TCC_CACHE_LINE;
if (sz == 0)
return;
if ((va & PAGE_MASK) == 0)
break; /* need new pmap_extract() */
}
}
}
void
tcc_HitSyncDCache(struct cpu_info *ci, vaddr_t _va, size_t _sz)
{
vaddr_t va;
vsize_t sz;
/* extend the range to integral cache lines */
va = _va & ~(TCC_CACHE_LINE - 1);
sz = ((_va + _sz + TCC_CACHE_LINE - 1) & ~(TCC_CACHE_LINE - 1)) - va;
mips_sync();
tcc_prefetch_invalidate();
tcc_virtual(ci, va, sz, TCC_CACHEOP_WRITEBACK | TCC_CACHEOP_INVALIDATE);
tcc_prefetch_invalidate();
}
void
tcc_HitInvalidateDCache(struct cpu_info *ci, vaddr_t _va, size_t _sz)
{
vaddr_t va;
vsize_t sz;
/* extend the range to integral cache lines */
va = _va & ~(TCC_CACHE_LINE - 1);
sz = ((_va + _sz + TCC_CACHE_LINE - 1) & ~(TCC_CACHE_LINE - 1)) - va;
mips_sync();
tcc_prefetch_invalidate();
tcc_virtual(ci, va, sz, TCC_CACHEOP_INVALIDATE);
tcc_prefetch_invalidate();
}
void
tcc_IOSyncDCache(struct cpu_info *ci, vaddr_t _va, size_t _sz, int how)
{
vaddr_t va;
vsize_t sz;
int partial_start, partial_end;
/* extend the range to integral cache lines */
va = _va & ~(TCC_CACHE_LINE - 1);
sz = ((_va + _sz + TCC_CACHE_LINE - 1) & ~(TCC_CACHE_LINE - 1)) - va;
mips_sync();
switch (how) {
default:
case CACHE_SYNC_R:
/* writeback partial cachelines */
if (((_va | _sz) & (TCC_CACHE_LINE - 1)) != 0) {
partial_start = va != _va;
partial_end = va + sz != _va + _sz;
} else {
partial_start = partial_end = 0;
}
tcc_prefetch_invalidate();
if (partial_start) {
tcc_virtual(ci, va, TCC_CACHE_LINE,
TCC_CACHEOP_WRITEBACK | TCC_CACHEOP_INVALIDATE);
va += TCC_CACHE_LINE;
sz -= TCC_CACHE_LINE;
}
if (sz != 0 && partial_end) {
sz -= TCC_CACHE_LINE;
tcc_virtual(ci, va + sz, TCC_CACHE_LINE,
TCC_CACHEOP_WRITEBACK | TCC_CACHEOP_INVALIDATE);
}
if (sz != 0)
tcc_virtual(ci, va, sz, TCC_CACHEOP_INVALIDATE);
break;
case CACHE_SYNC_X:
tcc_prefetch_invalidate();
tcc_virtual(ci, va, sz, TCC_CACHEOP_WRITEBACK);
break;
case CACHE_SYNC_W:
tcc_prefetch_invalidate();
tcc_virtual(ci, va, sz,
TCC_CACHEOP_WRITEBACK | TCC_CACHEOP_INVALIDATE);
break;
}
tcc_prefetch_invalidate();
tfp_IOSyncDCache(ci, _va, _sz, how);
}
|