- Small step forward Linux boots and almost works...
[coreboot.git] / src / boot / hardwaremain.c
1 /*
2 This software and ancillary information (herein called SOFTWARE )
3 called LinuxBIOS          is made available under the terms described
4 here.  The SOFTWARE has been approved for release with associated
5 LA-CC Number 00-34   .  Unless otherwise indicated, this SOFTWARE has
6 been authored by an employee or employees of the University of
7 California, operator of the Los Alamos National Laboratory under
8 Contract No. W-7405-ENG-36 with the U.S. Department of Energy.  The
9 U.S. Government has rights to use, reproduce, and distribute this
10 SOFTWARE.  The public may copy, distribute, prepare derivative works
11 and publicly display this SOFTWARE without charge, provided that this
12 Notice and any statement of authorship are reproduced on all copies.
13 Neither the Government nor the University makes any warranty, express
14 or implied, or assumes any liability or responsibility for the use of
15 this SOFTWARE.  If SOFTWARE is modified to produce derivative works,
16 such modified SOFTWARE should be clearly marked, so as not to confuse
17 it with the version available from LANL.
18  */
19 /* Copyright 2000, Ron Minnich, Advanced Computing Lab, LANL
20  * rminnich@lanl.gov
21  */
22
23
24 /*
25  * C Bootstrap code for the LinuxBIOS
26  */
27
28
29 #include <console/console.h>
30 #include <cpu/cpu.h>
31 #include <mem.h>
32 #include <version.h>
33 #include <smp/start_stop.h>
34 #include <boot/tables.h>
35 #include <part/sizeram.h>
36 #include <device/device.h>
37 #include <device/pci.h>
38 #if 0
39 #include <part/mainboard.h>
40 #endif
41 #if 0
42 #include <part/hard_reset.h>
43 #endif
44 #include <smp/atomic.h>
45 #include <boot/elf.h>
46
47
48 #ifndef CONFIG_MAX_PHYSICAL_CPUS
49 #define CONFIG_MAX_PHYSICAL_CPUS CONFIG_MAX_CPUS
50 #endif
51
52 /* The processor map. 
53  * Now that SMP is in linuxbios, and Linux counts on us
54  * giving accurate information about processors, we need a map
55  * of what processors are out there. This could be a bit mask, 
56  * but we will be optimistic and hope we someday run on 
57  * REALLY BIG SMPs. Also we may need more than one bit of 
58  * info per processor at some point. I hope we don't need 
59  * anything more complex than an int.
60  */
61 static unsigned long processor_map[MAX_CPUS];
62
63 static struct mem_range *get_ramsize(void)
64 {
65         struct mem_range *mem = 0;
66         if (!mem) {
67                 mem = sizeram();
68         }
69         if (!mem) {
70                 printk_err("No memory size information!\n");
71                 for(;;);
72         }
73         return mem;
74 }
75
76
77 #if SMP == 1
78 /* Number of cpus that are currently running in linuxbios */
79 static atomic_t active_cpus = ATOMIC_INIT(1);
80
81 void secondary_cpu_init(void)
82 {
83         struct mem_range *mem;
84         unsigned long id;
85         int index;
86
87         atomic_inc(&active_cpus);
88         printk_debug(__FUNCTION__ "\n");
89         mem = get_ramsize();
90         id = cpu_initialize(mem);
91         index = processor_index(id);
92         printk_debug(__FUNCTION__ "  %d/%u\n", index, id);
93         processor_map[index] = CPU_ENABLED;
94         atomic_dec(&active_cpus);
95         stop_cpu(id);
96 }
97
98 static void wait_for_other_cpus(void)
99 {
100         int old_active_count, active_count;
101         int i;
102         old_active_count = 1;
103
104         active_count = atomic_read(&active_cpus);
105         while(active_count > 1) {
106                 if (active_count != old_active_count) {
107                         printk_info("Waiting for %d CPUS to stop\n", active_count);
108                         old_active_count = active_count;
109                 }
110                 active_count = atomic_read(&active_cpus);
111         }
112         for(i = 0; i < MAX_CPUS; i++) {
113                 if (!(processor_map[i] & CPU_ENABLED)) {
114                         printk_err("CPU %d/%u did not initialize!\n",
115                                 i, initial_apicid[i]);
116                         processor_map[i] = 0;
117                         mainboard_cpu_fixup(i);
118                 }
119         }
120         printk_debug("All AP CPUs stopped\n");
121 }
122
123 #else /* SMP */
124 #define wait_for_other_cpus() do {} while(0)
125 #endif /* SMP */
126
127 void hardwaremain(int boot_complete)
128 {
129         /* Processor ID of the BOOT cpu (i.e. the one running this code) */
130         unsigned long boot_cpu;
131         int boot_index;
132
133         /* the order here is a bit tricky. We don't want to do much of 
134          * anything that uses config registers until after PciAllocateResources
135          * since that function also figures out what kind of config strategy
136          * to use (type 1 or type 2). 
137          * so we turn on cache, then worry about PCI setup, then do other 
138          * things, so that the other work can use the PciRead* and PciWrite*
139          * functions. 
140          */
141         struct mem_range *mem, *tmem;
142         struct lb_memory *lb_mem;
143         unsigned long totalmem;
144
145         post_code(0x80);
146         /* displayinit MUST PRECEDE ALL PRINTK! */
147         console_init();
148         
149         post_code(0x39);
150         printk_notice("LinuxBIOS-%s%s %s %s...\n", 
151                 linuxbios_version, linuxbios_extra_version, linuxbios_build,
152                 (boot_complete)?"rebooting":"booting");
153
154         post_code(0x40);
155
156 #if 0
157         /* If we have already booted attempt a hard reboot */
158         if (boot_complete) {
159                 hard_reset();
160         }
161 #endif
162 #if 1
163
164         // pick how to scan the bus. This is first so we can get at memory size.
165         printk_info("Finding PCI configuration type.\n");
166         pci_set_method();
167         post_code(0x5f);
168 #if 0
169         enumerate_static_devices();
170 #endif
171         dev_enumerate();
172         post_code(0x66);
173         // Now do the real bus
174         // we round the total ram up a lot for thing like the SISFB, which 
175         // shares high memory with the CPU. 
176         dev_configure();
177         post_code(0x88);
178
179         dev_enable();
180         dev_initialize();
181         post_code(0x89);
182 #endif
183
184         mem = get_ramsize();
185         post_code(0x70);
186         totalmem = 0;
187         for(tmem = mem; tmem->sizek; tmem++) {
188                 totalmem += tmem->sizek;
189         }
190         printk_info("totalram: %ldM\n", 
191                 (totalmem + 512) >> 10); /* Round to the nearest meg */
192
193         /* Fully initialize the cpu before configuring the bus */
194         boot_cpu = cpu_initialize(mem);
195         boot_index = processor_index(boot_cpu);
196         printk_spew("BOOT CPU is %d\n", boot_cpu);
197         processor_map[boot_index] = CPU_BOOTPROCESSOR|CPU_ENABLED;
198
199         /* Now start the other cpus initializing 
200          * The sooner they start the sooner they stop.
201          */
202         post_code(0x75);
203         startup_other_cpus(processor_map);
204         post_code(0x77);
205
206         /* make certain we are the only cpu running in linuxBIOS */
207         wait_for_other_cpus();
208
209         /* Now that we have collected all of our information
210          * write our configuration tables.
211          */
212         lb_mem = write_tables(mem, processor_map);
213
214         elfboot(lb_mem);
215 }
216