File: init.c

package info (click to toggle)
openbios-sparc 1.0%2Bsvn640-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,412 kB
  • ctags: 12,091
  • sloc: ansic: 57,249; asm: 2,680; xml: 1,335; cpp: 414; makefile: 224; sh: 190
file content (694 lines) | stat: -rw-r--r-- 16,063 bytes parent folder | download | duplicates (2)
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
 *   Creation Date: <2004/08/28 18:38:22 greg>
 *   Time-stamp: <2004/08/28 18:38:22 greg>
 *
 *	<init.c>
 *
 *	Initialization for qemu
 *
 *   Copyright (C) 2004 Greg Watson
 *   Copyright (C) 2005 Stefan Reinauer
 *
 *   based on mol/init.c:
 *
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Samuel & David Rydh
 *      (samuel@ibrium.se, dary@lindesign.se)
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation
 *
 */

#include "openbios/config.h"
#include "openbios/bindings.h"
#include "openbios/pci.h"
#include "openbios/nvram.h"
#include "openbios/drivers.h"
#include "qemu/qemu.h"
#include "ofmem.h"
#include "openbios-version.h"
#include "libc/byteorder.h"
#include "libc/vsprintf.h"
#define NO_QEMU_PROTOS
#include "openbios/fw_cfg.h"
#include "ppc/processor.h"

#define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"

struct cpudef {
    unsigned long iu_version;
    const char *name;
    int icache_size, dcache_size;
    int icache_sets, dcache_sets;
    int icache_block_size, dcache_block_size;
    int clock_frequency;
    void (*initfn)(const struct cpudef *cpu);
};

static uint16_t machine_id = 0;

extern void unexpected_excep( int vector );
extern void setup_timers( void );

void
unexpected_excep( int vector )
{
	printk("openbios panic: Unexpected exception %x\n", vector );
	for( ;; )
		;
}

enum {
    ARCH_PREP = 0,
    ARCH_MAC99,
    ARCH_HEATHROW,
};

int is_apple(void)
{
	return 1;
}

int is_oldworld(void)
{
	return machine_id == ARCH_HEATHROW;
}

int is_newworld(void)
{
	return machine_id == ARCH_MAC99;
}

static const pci_arch_t known_arch[] = {
        [ARCH_PREP] = { "PREP", PCI_VENDOR_ID_MOTOROLA,
                        PCI_DEVICE_ID_MOTOROLA_RAVEN,
                        0x80800000, 0x800c0000,
			0x80000000, 0x00100000, 0xf0000000, 0x10000000,
			0x80000000, 0x00010000, 0x00000000, 0x00400000,
			{ 9, 11, 9, 11 }
		      },
        [ARCH_MAC99] = { "MAC99", PCI_VENDOR_ID_APPLE,
                         PCI_DEVICE_ID_APPLE_UNI_N_PCI,
                         0xf2800000, 0xf2c00000,
			  0xf2000000, 0x02000000, 0x80000000, 0x10000000,
			  0xf2000000, 0x00800000, 0x00000000, 0x01000000,
			  { 8, 9, 10, 11 }
		       },
        [ARCH_HEATHROW] = { "HEATHROW", PCI_VENDOR_ID_MOTOROLA,
                            PCI_DEVICE_ID_MOTOROLA_MPC106,
                            0xfec00000, 0xfee00000,
			    0x80000000, 0x7f000000, 0x80000000, 0x01000000,
			    0xfe000000, 0x00800000, 0xfd000000, 0x01000000,
			    { 21, 22, 23, 24 }
			  },
};
uint32_t isa_io_base;

void
entry( void )
{
        uint32_t temp = 0;
        char buf[5];

        arch = &known_arch[ARCH_HEATHROW];

        fw_cfg_init();

        fw_cfg_read(FW_CFG_SIGNATURE, buf, 4);
        buf[4] = '\0';
        if (strncmp(buf, "QEMU", 4) == 0) {
            temp = fw_cfg_read_i32(FW_CFG_ID);
            if (temp == 1) {
                machine_id = fw_cfg_read_i16(FW_CFG_MACHINE_ID);
                arch = &known_arch[machine_id];
            }
        }

	isa_io_base = arch->io_base;

        if (temp != 1) {
            printk("Incompatible configuration device version, freezing\n");
            for(;;);
        }

	ofmem_init();
	initialize_forth();
	/* won't return */

	printk("of_startup returned!\n");
	for( ;; )
		;
}

static void
cpu_generic_init(const struct cpudef *cpu)
{
    unsigned long iu_version;

    push_str("/cpus");
    fword("find-device");

    fword("new-device");

    push_str(cpu->name);
    fword("device-name");

    push_str("cpu");
    fword("device-type");

    asm("mfpvr %0\n"
        : "=r"(iu_version) :);
    PUSH(iu_version);
    fword("encode-int");
    push_str("cpu-version");
    fword("property");

    PUSH(cpu->dcache_size);
    fword("encode-int");
    push_str("dcache-size");
    fword("property");

    PUSH(cpu->icache_size);
    fword("encode-int");
    push_str("icache-size");
    fword("property");

    PUSH(cpu->dcache_sets);
    fword("encode-int");
    push_str("dcache-sets");
    fword("property");

    PUSH(cpu->icache_sets);
    fword("encode-int");
    push_str("icache-sets");
    fword("property");

    PUSH(cpu->dcache_block_size);
    fword("encode-int");
    push_str("dcache-block-size");
    fword("property");

    PUSH(cpu->icache_block_size);
    fword("encode-int");
    push_str("icache-block-size");
    fword("property");

    push_str("running");
    fword("encode-string");
    push_str("state");
    fword("property");
}

static void
cpu_add_pir_property(void)
{
    unsigned long pir;

    asm("mfspr %0, 1023\n"
        : "=r"(pir) :);
    PUSH(pir);
    fword("encode-int");
    push_str("reg");
    fword("property");
}

static void
cpu_604_init(const struct cpudef *cpu)
{
    cpu_generic_init(cpu);
    cpu_add_pir_property();

    fword("finish-device");
}

static void
cpu_750_init(const struct cpudef *cpu)
{
    cpu_generic_init(cpu);

    PUSH(0);
    fword("encode-int");
    push_str("reg");
    fword("property");

    fword("finish-device");
}

static void
cpu_g4_init(const struct cpudef *cpu)
{
    cpu_generic_init(cpu);
    cpu_add_pir_property();

    fword("finish-device");
}

/* In order to get 64 bit aware handlers that rescue all our
   GPRs from getting truncated to 32 bits, we need to patch the
   existing handlers so they jump to our 64 bit aware ones. */
static void
ppc64_patch_handlers(void)
{
    uint32_t *dsi = (uint32_t *)0x300UL;
    uint32_t *isi = (uint32_t *)0x400UL;

    // Patch the first DSI handler instruction to: ba 0x2000
    *dsi = 0x48002002;

    // Patch the first ISI handler instruction to: ba 0x2200
    *isi = 0x48002202;

    // Invalidate the cache lines
    asm ( "icbi 0, %0" : : "r"(dsi) );
    asm ( "icbi 0, %0" : : "r"(isi) );
}

static void
cpu_970_init(const struct cpudef *cpu)
{
    cpu_generic_init(cpu);

    PUSH(0);
    fword("encode-int");
    push_str("reg");
    fword("property");
    
    PUSH(0);
    PUSH(0);
    fword("encode-bytes");
    push_str("64-bit");
    fword("property");

    fword("finish-device");

    /* The 970 is a PPC64 CPU, so we need to activate
     * 64bit aware interrupt handlers */

    ppc64_patch_handlers();

    /* The 970 also implements the HIOR which we need to set to 0 */

    mtspr(S_HIOR, 0);
}

static const struct cpudef ppc_defs[] = {
    {
        .iu_version = 0x00040000,
        .name = "PowerPC,604",
        .icache_size = 0x4000,
        .dcache_size = 0x4000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x07de2900,
        .initfn = cpu_604_init,
    },
    { // XXX find out real values
        .iu_version = 0x00090000,
        .name = "PowerPC,604e",
        .icache_size = 0x4000,
        .dcache_size = 0x4000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x07de2900,
        .initfn = cpu_604_init,
    },
    { // XXX find out real values
        .iu_version = 0x000a0000,
        .name = "PowerPC,604r",
        .icache_size = 0x4000,
        .dcache_size = 0x4000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x07de2900,
        .initfn = cpu_604_init,
    },
    { // XXX find out real values
        .iu_version = 0x80040000,
        .name = "PowerPC,MPC86xx",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x14dc9380,
        .initfn = cpu_750_init,
    },
    {
        .iu_version = 0x000080000,
        .name = "PowerPC,750",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x14dc9380,
        .initfn = cpu_750_init,
    },
    { // XXX find out real values
        .iu_version = 0x10080000,
        .name = "PowerPC,750",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x14dc9380,
        .initfn = cpu_750_init,
    },
    { // XXX find out real values
        .iu_version = 0x70000000,
        .name = "PowerPC,750",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x14dc9380,
        .initfn = cpu_750_init,
    },
    { // XXX find out real values
        .iu_version = 0x70020000,
        .name = "PowerPC,750",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x14dc9380,
        .initfn = cpu_750_init,
    },
    { // XXX find out real values
        .iu_version = 0x800c0000,
        .name = "PowerPC,74xx",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x14dc9380,
        .initfn = cpu_750_init,
    },
    {
        .iu_version = 0x0000c0000,
        .name = "PowerPC,G4",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x20,
        .dcache_block_size = 0x20,
        .clock_frequency = 0x1dcd6500,
        .initfn = cpu_g4_init,
    },
    { // XXX find out real values
        .iu_version = 0x003C0000,
        .name = "PowerPC,970FX",
        .icache_size = 0x8000,
        .dcache_size = 0x8000,
        .icache_sets = 0x80,
        .dcache_sets = 0x80,
        .icache_block_size = 0x80,
        .dcache_block_size = 0x80,
        .clock_frequency = 0x1dcd6500,
        .initfn = cpu_970_init,
    },
};

static const struct cpudef *
id_cpu(void)
{
    unsigned long iu_version;
    unsigned int i;

    asm("mfpvr %0\n"
        : "=r"(iu_version) :);
    iu_version &= 0xffff0000;

    for (i = 0; i < sizeof(ppc_defs)/sizeof(struct cpudef); i++) {
        if (iu_version == ppc_defs[i].iu_version)
            return &ppc_defs[i];
    }
    printk("Unknown cpu (pvr %lx), freezing!\n", iu_version);
    for (;;);
}

static void go( void );

static void
go( void )
{
    ucell addr;

    addr = POP();

    call_elf( 0, 0, addr);
}

void
arch_of_init( void )
{
#ifdef USE_RTAS
	phandle_t ph;
#endif
	uint64_t ram_size;
        const struct cpudef *cpu;
        char buf[64], qemu_uuid[16];
        const char *stdin_path, *stdout_path;
        uint32_t temp = 0;

    ofmem_t *ofmem = ofmem_arch_get_private();

        modules_init();
        setup_timers();
#ifdef CONFIG_DRIVER_PCI
        ob_pci_init();
#endif

        printk("\n");
        printk("=============================================================\n");
        printk(PROGRAM_NAME " " OPENBIOS_VERSION_STR " [%s]\n",
               OPENBIOS_BUILD_DATE);

        fw_cfg_read(FW_CFG_SIGNATURE, buf, 4);
        buf[4] = '\0';
        printk("Configuration device id %s", buf);

        temp = fw_cfg_read_i32(FW_CFG_ID);
        printk(" version %d machine id %d\n", temp, machine_id);

        temp = fw_cfg_read_i32(FW_CFG_NB_CPUS);

        printk("CPUs: %x\n", temp);

        ram_size = ofmem->ramsize;

        printk("Memory: %lldM\n", ram_size / 1024 / 1024);

        fw_cfg_read(FW_CFG_UUID, qemu_uuid, 16);

        printk("UUID: " UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
               qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
               qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
               qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
               qemu_uuid[15]);

	/* set device tree root info */

	push_str("/");
	fword("find-device");

	switch(machine_id) {
	case ARCH_HEATHROW:	/* OldWord */

		/* model */

		push_str("Power Macintosh");
		fword("model");

		/* compatible */

		push_str("AAPL,PowerMac G3");
		fword("encode-string");
		push_str("MacRISC");
		fword("encode-string");
		fword("encode+");
		push_str("compatible");
		fword("property");

		/* misc */

		push_str("device-tree");
		fword("encode-string");
		push_str("AAPL,original-name");
		fword("property");

		PUSH(0);
		fword("encode-int");
		push_str("AAPL,cpu-id");
		fword("property");

		PUSH(66 * 1000 * 1000);
		fword("encode-int");
		push_str("clock-frequency");
		fword("property");
		break;

	case ARCH_MAC99:
	case ARCH_PREP:
	default:

		/* model */

		push_str("PowerMac2,1");
		fword("model");

		/* compatible */

		push_str("PowerMac2,1");
		fword("encode-string");
		push_str("MacRISC");
		fword("encode-string");
		fword("encode+");
		push_str("Power Macintosh");
		fword("encode-string");
		fword("encode+");
		push_str("compatible");
		fword("property");

		/* misc */

		push_str("bootrom");
		fword("device-type");

		PUSH(100 * 1000 * 1000);
		fword("encode-int");
		push_str("clock-frequency");
		fword("property");
		break;
	}

	/* Perhaps we can store UUID here ? */

	push_str("0000000000000");
	fword("encode-string");
	push_str("system-id");
	fword("property");

	/* pci info */

	if (machine_id == ARCH_MAC99) {
		push_str("/pci");
		fword("find-device");
		push_str("u3-agp");
		fword("encode-string");
		push_str("compatible");
		fword("property");
	}

	/* memory info */

	push_str("/memory");
	fword("find-device");

	/* all memory */

	PUSH(ram_size >> 32);
	fword("encode-int");
	PUSH(ram_size & 0xffffffff);
	fword("encode-int");
	fword("encode+");
	PUSH(0);
	fword("encode-int");
	fword("encode+");
	PUSH(0);
	fword("encode-int");
	fword("encode+");
	push_str("reg");
	fword("property");

        cpu = id_cpu();
        cpu->initfn(cpu);
        printk("CPU type %s\n", cpu->name);

	snprintf(buf, sizeof(buf), "/cpus/%s", cpu->name);
	ofmem_register(find_dev("/memory"), find_dev(buf));
	node_methods_init(buf);

#ifdef USE_RTAS
	if( !(ph=find_dev("/rtas")) )
		printk("Warning: No /rtas node\n");
	else {
		ulong size = 0x1000;
		while( size < (ulong)of_rtas_end - (ulong)of_rtas_start )
			size *= 2;
		set_property( ph, "rtas-size", (char*)&size, sizeof(size) );
	}
#endif

        if (fw_cfg_read_i16(FW_CFG_NOGRAPHIC)) {
                if (CONFIG_SERIAL_PORT) {
                       stdin_path = "scca";
                       stdout_path = "scca";
                } else {
                       stdin_path = "sccb";
                       stdout_path = "sccb";
                }
        } else {
                stdin_path = "adb-keyboard";
                stdout_path = "screen";
        }

        push_str("/chosen");
        fword("find-device");

        push_str(stdin_path);
        fword("open-dev");
        fword("encode-int");
        push_str("stdin");
        fword("property");

        push_str(stdout_path);
        fword("open-dev");
        fword("encode-int");
        push_str("stdout");
        fword("property");

        push_str(stdin_path);
        fword("pathres-resolve-aliases");
        push_str("input-device");
        fword("$setenv");

        push_str(stdout_path);
        fword("pathres-resolve-aliases");
        push_str("output-device");
        fword("$setenv");

        push_str(stdin_path);
        fword("input");

        push_str(stdout_path);
        fword("output");

#if 0
	if( getbool("tty-interface?") == 1 )
#endif
		fword("activate-tty-interface");

	device_end();

	bind_func("platform-boot", boot );
	bind_func("(go)", go);
}