summaryrefslogtreecommitdiff
path: root/roms/openbios/arch/sparc32/lib.c
blob: af1812d23898e672bf49fc6d4e5b0e15a3c6da9b (plain)
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
/* 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 "libc/vsprintf.h"
#include "libopenbios/bindings.h"
#include "arch/sparc32/ofmem_sparc32.h"
#include "asm/asi.h"
#include "pgtsrmmu.h"
#include "openprom.h"
#include "libopenbios/sys_info.h"
#include "boot.h"
#include "romvec.h"

#define NCTX_SWIFT  0x100
#define LOWMEMSZ 32 * 1024 * 1024

#ifdef CONFIG_DEBUG_MEM
#define DPRINTF(fmt, args...)                   \
    do { printk(fmt , ##args); } while (0)
#else
#define DPRINTF(fmt, args...)
#endif

/* 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;
}

/*
 * Allocatable memory chunk.
 */
struct mem {
    char *start, *uplim;
    char *curp;
};

struct mem cdvmem;              /* Current device virtual memory space */

unsigned int va_shift;
unsigned long *l1;
static unsigned long *context_table;

struct linux_mlist_v0 *ptphys;
struct linux_mlist_v0 *ptmap;
struct linux_mlist_v0 *ptavail;

/* 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);
}

/*
 * Allocate memory. This is reusable.
 */
void
mem_init(struct mem *t, char *begin, char *limit)
{
    t->start = begin;
    t->uplim = limit;
    t->curp = begin;
}

void *
mem_alloc(struct mem *t, int size, int align)
{
    char *p;
    unsigned long pa;

    // The alignment restrictions refer to physical, not virtual
    // addresses
    pa = va2pa((unsigned long)t->curp) + (align - 1);
    pa &= ~(align - 1);
    p = (char *)pa2va(pa);

    if ((unsigned long)p >= (unsigned long)t->uplim ||
        (unsigned long)p + size > (unsigned long)t->uplim)
        return NULL;
    t->curp = p + size;

    return p;
}

/*
 * D5.3 pgmap@ ( va -- pte )
 */
static void
pgmap_fetch(void)
{
    uint32_t pte;
    unsigned long va, pa;

    va = POP();

    pa = find_pte(va, 0);
    if (pa == 1 || pa == 2)
        goto error;
    pte = *(uint32_t *)pa;
    DPRINTF("pgmap@: va 0x%lx pa 0x%lx pte 0x%x\n", va, pa, pte);

    PUSH(pte);
    return;
 error:
    PUSH(0);
}

/*
 * D5.3 pgmap! ( pte va -- )
 */
static void
pgmap_store(void)
{
    uint32_t pte;
    unsigned long va, pa;

    va = POP();
    pte = POP();

    pa = find_pte(va, 1);
    *(uint32_t *)pa = pte;
    DPRINTF("pgmap!: va 0x%lx pa 0x%lx pte 0x%x\n", va, pa, pte);
}

/*
 * D5.3 map-pages ( pa space va size -- )
 */
static void
ob_map_pages(void)
{
    unsigned long va;
    int size;
    uint64_t pa;

    size = POP();
    va = POP();
    pa = POP();
    pa <<= 32;
    pa |= POP() & 0xffffffff;

    ofmem_arch_map_pages(pa, va, size, ofmem_arch_default_translation_mode(pa));
}

char *obp_dumb_mmap(char *va, int which_io, unsigned int pa,
                    unsigned int size)
{
    uint64_t mpa = ((uint64_t)which_io << 32) | (uint64_t)pa;

    ofmem_arch_map_pages(mpa, (unsigned long)va, size, ofmem_arch_default_translation_mode(mpa));
    return va;
}

void obp_dumb_munmap(__attribute__((unused)) char *va,
                     __attribute__((unused)) unsigned int size)
{
    DPRINTF("obp_dumb_munmap: virta 0x%x, sz %d\n", (unsigned int)va, size);
}

char *obp_memalloc(char *va, unsigned int size, unsigned int align)
{
    phys_addr_t phys;
    ucell virt;

    DPRINTF("obp_memalloc: virta 0x%x, sz %d, align %d\n", (unsigned int)va, size, align);    
    
    /* Claim physical memory */
    phys = ofmem_claim_phys(-1, size, align);

    /* Claim virtual memory */
    virt = ofmem_claim_virt(pointer2cell(va), size, 0);

    /* Map the memory */
    ofmem_map(phys, virt, size, ofmem_arch_default_translation_mode(phys));

    return cell2pointer(virt);
}

char *obp_dumb_memalloc(char *va, unsigned int size)
{
    unsigned long align = size;
    phys_addr_t phys;
    ucell virt;
    
    DPRINTF("obp_dumb_memalloc: virta 0x%x, sz %d\n", (unsigned int)va, size);    
    
    /* Solaris seems to assume that the returned value is physically aligned to size.
       e.g. it is used for setting up page tables. */
    
    /* Claim physical memory */
    phys = ofmem_claim_phys(-1, size, align);

    /* Claim virtual memory - if va == NULL then we choose va address */
    if (va == NULL) {
        virt = ofmem_claim_virt((ucell)-1, size, align);        
    } else {
        virt = ofmem_claim_virt(pointer2cell(va), size, 0);
    }

    /* Map the memory */
    ofmem_map(phys, virt, size, ofmem_arch_default_translation_mode(phys));

    return cell2pointer(virt);
}

void obp_dumb_memfree(char *va, unsigned size)
{
    phys_addr_t phys;
    ucell cellmode;

    DPRINTF("obp_dumb_memfree: virta 0x%x, sz %d\n", (unsigned int)va, size);

    phys = ofmem_translate(pointer2cell(va), &cellmode);

    ofmem_unmap(pointer2cell(va), size);
    ofmem_release_virt(pointer2cell(va), size);
    ofmem_release_phys(phys, size);
}

/* Data fault handling routines */

extern unsigned int ignore_dfault;

/* ( -- reg ) */
static void srmmu_get_sfsr(void)
{
    PUSH(srmmu_get_fstatus());
}

/* ( -- addr ) */
static void ignore_dfault_addr(void)
{
    PUSH(pointer2cell(&ignore_dfault));
}

void
ob_init_mmu(void)
{
    ucell *memreg;
    ucell *virtreg;
    phys_addr_t virtregsize;
    ofmem_t *ofmem = ofmem_arch_get_private();

    /* Find the phandles for the /memory and /virtual-memory nodes */
    push_str("/memory");
    fword("find-package");
    POP();
    s_phandle_memory = POP();

    push_str("/virtual-memory");
    fword("find-package");
    POP();
    s_phandle_mmu = POP();

    ofmem_register(s_phandle_memory, s_phandle_mmu);

    /* Setup /memory:reg (totphys) property */
    memreg = malloc(3 * sizeof(ucell));
    ofmem_arch_encode_physaddr(memreg, 0); /* physical base */
    memreg[2] = (ucell)ofmem->ramsize; /* size */

    push_str("/memory");
    fword("find-device");
    PUSH(pointer2cell(memreg));
    PUSH(3 * sizeof(ucell));
    push_str("reg");
    PUSH_ph(s_phandle_memory);
    fword("encode-property");

    /* Setup /virtual-memory:reg property */
    virtregsize = ((phys_addr_t)((ucell)-1) + 1) / 2;
    
    virtreg = malloc(6 * sizeof(ucell));
    ofmem_arch_encode_physaddr(virtreg, 0);
    virtreg[2] = virtregsize;
    ofmem_arch_encode_physaddr(&virtreg[3], virtregsize);
    virtreg[5] = virtregsize;
    
    push_str("/virtual-memory");
    fword("find-device");
    PUSH(pointer2cell(virtreg));
    PUSH(6 * sizeof(ucell));
    push_str("reg");
    PUSH_ph(s_phandle_mmu);
    fword("encode-property");
    
    PUSH(0);
    fword("active-package!");
    bind_func("pgmap@", pgmap_fetch);
    bind_func("pgmap!", pgmap_store);
    bind_func("map-pages", ob_map_pages);

    /* Install data fault handler words for cpeek etc. */
    PUSH_xt(bind_noname_func(srmmu_get_sfsr));
    feval("to sfsr@");
    PUSH_xt(bind_noname_func(ignore_dfault_addr));
    feval("to ignore-dfault");
}

/*
 * Switch page tables.
 */
void
init_mmu_swift(void)
{
    unsigned int addr, i;
    unsigned long pa, va;
    int size;

    ofmem_posix_memalign((void *)&context_table, NCTX_SWIFT * sizeof(int),
                   NCTX_SWIFT * sizeof(int));
    ofmem_posix_memalign((void *)&l1, 256 * sizeof(int), 256 * sizeof(int));

    context_table[0] = (((unsigned long)va2pa((unsigned long)l1)) >> 4) |
        SRMMU_ET_PTD;

    for (i = 1; i < NCTX_SWIFT; i++) {
        context_table[i] = SRMMU_ET_INVALID;
    }
    for (i = 0; i < 256; i += 4) {
        l1[i] = SRMMU_ET_INVALID;
    }

    // text, rodata, data, and bss mapped to end of RAM
    va = (unsigned long)&_start;
    size = (unsigned long)&_end - (unsigned long)&_start;
    pa = va2pa(va);
    ofmem_arch_map_pages(pa, va, size, ofmem_arch_default_translation_mode(pa));
    ofmem_map_page_range(pa, va, size, ofmem_arch_default_translation_mode(pa));

    // 1:1 mapping for RAM (don't map page 0 to allow catching of NULL dereferences)                                                                                                                                            
    ofmem_arch_map_pages(PAGE_SIZE, PAGE_SIZE, LOWMEMSZ - PAGE_SIZE, ofmem_arch_default_translation_mode(0));                                                                                                                   
    ofmem_map_page_range(PAGE_SIZE, PAGE_SIZE, LOWMEMSZ - PAGE_SIZE, ofmem_arch_default_translation_mode(0));

    /*
     * Flush cache
     */
    for (addr = 0; addr < 0x2000; addr += 0x10) {
        __asm__ __volatile__ ("sta %%g0, [%0] %1\n\t" : :
                              "r" (addr), "i" (ASI_M_DATAC_TAG));
        __asm__ __volatile__ ("sta %%g0, [%0] %1\n\t" : :
                              "r" (addr<<1), "i" (ASI_M_TXTC_TAG));
    }
    srmmu_set_context(0);
    srmmu_set_ctable_ptr(va2pa((unsigned long)context_table));
    srmmu_flush_whole_tlb();
}