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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/* lib.c
* tag: simple function library
*
* Copyright (C) 2003 Stefan Reinauer
*
* See the file "COPYING" for further information about
* the copyright and warranty status of this work.
*/
#include "config.h"
#include "libc/vsprintf.h"
#include "libopenbios/bindings.h"
#include "spitfire.h"
#include "libopenbios/sys_info.h"
#include "boot.h"
#include "arch/sparc64/ofmem_sparc64.h"
/* Format a string and print it on the screen, just like the libc
* function printf.
*/
int printk( const char *fmt, ... )
{
char *p, buf[512];
va_list args;
int i;
va_start(args, fmt);
i = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
for( p=buf; *p; p++ )
putchar(*p);
return i;
}
/* Private functions for mapping between physical/virtual addresses */
phys_addr_t
va2pa(unsigned long va)
{
if ((va >= (unsigned long)&_start) &&
(va < (unsigned long)&_end))
return va - va_shift;
else
return va;
}
unsigned long
pa2va(phys_addr_t pa)
{
if ((pa + va_shift >= (unsigned long)&_start) &&
(pa + va_shift < (unsigned long)&_end))
return pa + va_shift;
else
return pa;
}
void *malloc(int size)
{
return ofmem_malloc(size);
}
void* realloc( void *ptr, size_t size )
{
return ofmem_realloc(ptr, size);
}
void free(void *ptr)
{
ofmem_free(ptr);
}
static void
mmu_open(void)
{
RET(-1);
}
static void
mmu_close(void)
{
}
void ofmem_walk_boot_map(translation_entry_cb cb)
{
unsigned long phys, virt, size, mode, data, mask;
unsigned int i;
for (i = 0; i < 64; i++) {
data = spitfire_get_dtlb_data(i);
if (data & SPITFIRE_TTE_VALID) {
switch ((data >> 61) & 3) {
default:
case 0x0: /* 8k */
mask = 0xffffffffffffe000ULL;
size = PAGE_SIZE_8K;
break;
case 0x1: /* 64k */
mask = 0xffffffffffff0000ULL;
size = PAGE_SIZE_64K;
break;
case 0x2: /* 512k */
mask = 0xfffffffffff80000ULL;
size = PAGE_SIZE_512K;
break;
case 0x3: /* 4M */
mask = 0xffffffffffc00000ULL;
size = PAGE_SIZE_4M;
break;
}
virt = spitfire_get_dtlb_tag(i);
virt &= mask;
/* extract 41bit physical address */
phys = data & 0x000001fffffff000ULL;
phys &= mask;
mode = data & 0xfff;
cb(phys, virt, size, mode);
}
}
}
/*
3.6.5 translate
( virt -- false | phys.lo ... phys.hi mode true )
*/
static void
mmu_translate(void)
{
ucell virt, mode;
phys_addr_t phys;
virt = POP();
phys = ofmem_translate(virt, &mode);
if (phys != -1UL) {
PUSH(phys & 0xffffffff);
PUSH(phys >> 32);
PUSH(mode);
PUSH(-1);
}
else {
PUSH(0);
}
}
/*
* D5.3 pgmap@ ( va -- tte )
*/
static void
pgmap_fetch(void)
{
unsigned long va, tte_data;
va = POP();
tte_data = find_tte(va);
if (tte_data == -1)
goto error;
/* return tte_data */
PUSH(tte_data);
return;
error:
/* If we get here, there was no entry */
PUSH(0);
}
/*
( index tte_data vaddr -- ? )
*/
static void
dtlb_load(void)
{
unsigned long vaddr, tte_data, idx;
vaddr = POP();
tte_data = POP();
idx = POP();
dtlb_load3(vaddr, tte_data, idx);
}
/* MMU D-TLB miss handler */
void
dtlb_miss_handler(void)
{
unsigned long faultva, tte_data = 0;
/* Grab fault address from MMU and round to nearest 8k page */
faultva = dtlb_faultva();
faultva >>= 13;
faultva <<= 13;
/* If a valid va>tte-data routine has been set, invoke that Forth xt instead */
if (va2ttedata && *va2ttedata != 0) {
/* va>tte-data ( addr cnum -- false | tte-data true ) */
PUSH(faultva);
PUSH(0);
enterforth(*va2ttedata);
/* Check the result first... */
tte_data = POP();
if (!tte_data) {
bug();
} else {
/* Grab the real data */
tte_data = POP();
}
} else {
/* Search the ofmem linked list for this virtual address */
tte_data = find_tte(faultva);
}
if (tte_data) {
/* Update MMU */
dtlb_load2(faultva, tte_data);
} else {
/* If we got here, there was no translation so fail */
bug();
}
}
/*
( index tte_data vaddr -- ? )
*/
static void
itlb_load(void)
{
unsigned long vaddr, tte_data, idx;
vaddr = POP();
tte_data = POP();
idx = POP();
itlb_load3(vaddr, tte_data, idx);
}
/* MMU I-TLB miss handler */
void
itlb_miss_handler(void)
{
unsigned long faultva, tte_data = 0;
/* Grab fault address from MMU and round to nearest 8k page */
faultva = itlb_faultva();
faultva >>= 13;
faultva <<= 13;
/* If a valid va>tte-data routine has been set, invoke that Forth xt instead */
if (va2ttedata && *va2ttedata != 0) {
/* va>tte-data ( addr cnum -- false | tte-data true ) */
PUSH(faultva);
PUSH(0);
enterforth(*va2ttedata);
/* Check the result first... */
tte_data = POP();
if (!tte_data) {
bug();
} else {
/* Grab the real data */
tte_data = POP();
}
} else {
/* Search the ofmem linked list for this virtual address */
tte_data = find_tte(faultva);
}
if (tte_data) {
/* Update MMU */
itlb_load2(faultva, tte_data);
} else {
/* If we got here, there was no translation so fail */
bug();
}
}
/*
3.6.5 map
( phys.lo ... phys.hi virt size mode -- )
*/
static void
mmu_map(void)
{
ucell virt, size, mode;
phys_addr_t phys;
mode = POP();
size = POP();
virt = POP();
phys = POP();
phys <<= 32;
phys |= POP();
ofmem_map(phys, virt, size, mode);
}
/*
3.6.5 unmap
( virt size -- )
*/
static void
mmu_unmap(void)
{
ucell virt, size;
size = POP();
virt = POP();
ofmem_unmap(virt, size);
}
/*
3.6.5 claim
( virt size align -- base )
*/
static void
mmu_claim(void)
{
ucell virt=-1UL, size, align;
align = POP();
size = POP();
if (!align) {
virt = POP();
}
virt = ofmem_claim_virt(virt, size, align);
PUSH(virt);
}
/*
3.6.5 release
( virt size -- )
*/
static void
mmu_release(void)
{
ucell virt, size;
size = POP();
virt = POP();
ofmem_release_virt(virt, size);
}
/* ( phys size align --- base ) */
static void
mem_claim( void )
{
ucell size, align;
phys_addr_t phys=-1UL;
align = POP();
size = POP();
if (!align) {
phys = POP();
phys <<= 32;
phys |= POP();
}
phys = ofmem_claim_phys(phys, size, align);
PUSH(phys & 0xffffffffUL);
PUSH(phys >> 32);
}
/* ( phys size --- ) */
static void
mem_release( void )
{
phys_addr_t phys;
ucell size;
size = POP();
phys = POP();
phys <<= 32;
phys |= POP();
ofmem_release_phys(phys, size);
}
/* ( name-cstr phys size align --- phys ) */
static void
mem_retain ( void )
{
ucell size, align;
phys_addr_t phys=-1UL;
align = POP();
size = POP();
if (!align) {
phys = POP();
phys <<= 32;
phys |= POP();
}
/* Currently do nothing with the name */
POP();
phys = ofmem_retain(phys, size, align);
PUSH(phys & 0xffffffffUL);
PUSH(phys >> 32);
}
/* ( virt size align -- baseaddr|-1 ) */
static void
ciface_claim( void )
{
ucell align = POP();
ucell size = POP();
ucell virt = POP();
ucell ret = ofmem_claim( virt, size, align );
/* printk("ciface_claim: %08x %08x %x\n", virt, size, align ); */
PUSH( ret );
}
/* ( virt size -- ) */
static void
ciface_release( void )
{
ucell size = POP();
ucell virt = POP();
ofmem_release(virt, size);
}
DECLARE_NODE(memory, INSTALL_OPEN, 0, "/memory");
NODE_METHODS( memory ) = {
{ "claim", mem_claim },
{ "release", mem_release },
{ "SUNW,retain", mem_retain },
};
DECLARE_NODE(mmu, INSTALL_OPEN, 0, "/virtual-memory");
NODE_METHODS(mmu) = {
{ "open", mmu_open },
{ "close", mmu_close },
{ "translate", mmu_translate },
{ "SUNW,dtlb-load", dtlb_load },
{ "SUNW,itlb-load", itlb_load },
{ "map", mmu_map },
{ "unmap", mmu_unmap },
{ "claim", mmu_claim },
{ "release", mmu_release },
};
void ob_mmu_init(const char *cpuname, uint64_t ram_size)
{
/* memory node */
REGISTER_NODE_METHODS(memory, "/memory");
/* MMU node */
REGISTER_NODE_METHODS(mmu, "/virtual-memory");
ofmem_register(find_dev("/memory"), find_dev("/virtual-memory"));
push_str("/chosen");
fword("find-device");
push_str("/virtual-memory");
fword("open-dev");
fword("encode-int");
push_str("mmu");
fword("property");
push_str("/memory");
fword("find-device");
/* All memory: 0 to RAM_size */
PUSH(0);
fword("encode-int");
PUSH(0);
fword("encode-int");
fword("encode+");
PUSH((int)(ram_size >> 32));
fword("encode-int");
fword("encode+");
PUSH((int)(ram_size & 0xffffffff));
fword("encode-int");
fword("encode+");
push_str("reg");
fword("property");
push_str("/openprom/client-services");
fword("find-device");
bind_func("cif-claim", ciface_claim);
bind_func("cif-release", ciface_release);
/* Other MMU functions */
PUSH(0);
fword("active-package!");
bind_func("pgmap@", pgmap_fetch);
/* Find address of va2ttedata defer word contents for MMU miss handlers */
va2ttedata = (ucell *)findword("va>tte-data");
va2ttedata++;
}
|