1 | /*
|
---|
2 | * Copyright (c) 2005 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kernel_ia64
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch.h>
|
---|
36 | #include <arch/arch.h>
|
---|
37 | #include <typedefs.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <interrupt.h>
|
---|
40 | #include <arch/interrupt.h>
|
---|
41 | #include <macros.h>
|
---|
42 | #include <stdbool.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <userspace.h>
|
---|
45 | #include <console/console.h>
|
---|
46 | #include <syscall/syscall.h>
|
---|
47 | #include <sysinfo/sysinfo.h>
|
---|
48 | #include <arch/drivers/it.h>
|
---|
49 | #include <arch/drivers/kbd.h>
|
---|
50 | #include <arch/legacyio.h>
|
---|
51 | #include <genarch/drivers/ega/ega.h>
|
---|
52 | #include <genarch/drivers/i8042/i8042.h>
|
---|
53 | #include <genarch/drivers/ns16550/ns16550.h>
|
---|
54 | #include <genarch/drivers/legacy/ia32/io.h>
|
---|
55 | #include <genarch/kbrd/kbrd.h>
|
---|
56 | #include <genarch/srln/srln.h>
|
---|
57 | #include <mm/page.h>
|
---|
58 | #include <mm/km.h>
|
---|
59 |
|
---|
60 | #ifdef MACHINE_ski
|
---|
61 | #include <arch/drivers/ski.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | static void ia64_pre_mm_init(void);
|
---|
65 | static void ia64_post_mm_init(void);
|
---|
66 | static void ia64_post_smp_init(void);
|
---|
67 |
|
---|
68 | arch_ops_t ia64_ops = {
|
---|
69 | .pre_mm_init = ia64_pre_mm_init,
|
---|
70 | .post_mm_init = ia64_post_mm_init,
|
---|
71 | .post_smp_init = ia64_post_smp_init,
|
---|
72 | };
|
---|
73 |
|
---|
74 | arch_ops_t *arch_ops = &ia64_ops;
|
---|
75 |
|
---|
76 | /* NS16550 as a COM 1 */
|
---|
77 | #define NS16550_IRQ (4 + LEGACY_INTERRUPT_BASE)
|
---|
78 |
|
---|
79 | bootinfo_t *bootinfo;
|
---|
80 |
|
---|
81 | static uint64_t iosapic_base = 0xfec00000;
|
---|
82 | uintptr_t legacyio_virt_base = 0;
|
---|
83 |
|
---|
84 | /** Performs ia64-specific initialization before main_bsp() is called. */
|
---|
85 | void ia64_pre_main(void)
|
---|
86 | {
|
---|
87 | init.cnt = min3(bootinfo->taskmap.cnt, TASKMAP_MAX_RECORDS,
|
---|
88 | CONFIG_INIT_TASKS);
|
---|
89 | size_t i;
|
---|
90 |
|
---|
91 | for (i = 0; i < init.cnt; i++) {
|
---|
92 | init.tasks[i].paddr =
|
---|
93 | (uintptr_t) bootinfo->taskmap.tasks[i].addr;
|
---|
94 | init.tasks[i].size = bootinfo->taskmap.tasks[i].size;
|
---|
95 | str_cpy(init.tasks[i].name, CONFIG_TASK_NAME_BUFLEN,
|
---|
96 | bootinfo->taskmap.tasks[i].name);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | void ia64_pre_mm_init(void)
|
---|
101 | {
|
---|
102 | if (config.cpu_active == 1)
|
---|
103 | exception_init();
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void iosapic_init(void)
|
---|
107 | {
|
---|
108 | uintptr_t IOSAPIC = km_map(iosapic_base, PAGE_SIZE, PAGE_SIZE,
|
---|
109 | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
---|
110 | int i;
|
---|
111 |
|
---|
112 | int myid, myeid;
|
---|
113 |
|
---|
114 | myid = ia64_get_cpu_id();
|
---|
115 | myeid = ia64_get_cpu_eid();
|
---|
116 |
|
---|
117 | for (i = 0; i < 16; i++) {
|
---|
118 | if (i == 2)
|
---|
119 | continue; /* Disable Cascade interrupt */
|
---|
120 | ((uint32_t *)(IOSAPIC + 0x00))[0] = 0x10 + 2 * i;
|
---|
121 | srlz_d();
|
---|
122 | ((uint32_t *)(IOSAPIC + 0x10))[0] = LEGACY_INTERRUPT_BASE + i;
|
---|
123 | srlz_d();
|
---|
124 | ((uint32_t *)(IOSAPIC + 0x00))[0] = 0x10 + 2 * i + 1;
|
---|
125 | srlz_d();
|
---|
126 | ((uint32_t *)(IOSAPIC + 0x10))[0] = myid << (56 - 32) |
|
---|
127 | myeid << (48 - 32);
|
---|
128 | srlz_d();
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|
132 |
|
---|
133 | void ia64_post_mm_init(void)
|
---|
134 | {
|
---|
135 | if (config.cpu_active == 1) {
|
---|
136 | /* Map the page with legacy I/O. */
|
---|
137 | legacyio_virt_base = km_map(LEGACYIO_PHYS_BASE, LEGACYIO_SIZE,
|
---|
138 | KM_NATURAL_ALIGNMENT, PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
---|
139 |
|
---|
140 | iosapic_init();
|
---|
141 | irq_init(INR_COUNT, INR_COUNT);
|
---|
142 | }
|
---|
143 | it_init();
|
---|
144 | }
|
---|
145 |
|
---|
146 | void ia64_post_smp_init(void)
|
---|
147 | {
|
---|
148 | static const char *platform;
|
---|
149 |
|
---|
150 | /* Set platform name. */
|
---|
151 | #ifdef MACHINE_ski
|
---|
152 | platform = "ski";
|
---|
153 | #endif
|
---|
154 | #ifdef MACHINE_i460GX
|
---|
155 | platform = "pc";
|
---|
156 | #endif
|
---|
157 | sysinfo_set_item_data("platform", NULL, (void *) platform,
|
---|
158 | str_size(platform));
|
---|
159 |
|
---|
160 | #ifdef MACHINE_ski
|
---|
161 | ski_instance_t *ski_instance = skiin_init();
|
---|
162 | if (ski_instance) {
|
---|
163 | srln_instance_t *srln_instance = srln_init();
|
---|
164 | if (srln_instance) {
|
---|
165 | indev_t *sink = stdin_wire();
|
---|
166 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
167 | skiin_wire(ski_instance, srln);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | outdev_t *skidev = skiout_init();
|
---|
172 | if (skidev)
|
---|
173 | stdout_wire(skidev);
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | #ifdef CONFIG_EGA
|
---|
177 | outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
|
---|
178 | if (egadev)
|
---|
179 | stdout_wire(egadev);
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | #ifdef CONFIG_NS16550
|
---|
183 | ns16550_instance_t *ns16550_instance =
|
---|
184 | ns16550_init(NS16550_BASE, 0, NS16550_IRQ, NULL, NULL,
|
---|
185 | NULL);
|
---|
186 | if (ns16550_instance) {
|
---|
187 | srln_instance_t *srln_instance = srln_init();
|
---|
188 | if (srln_instance) {
|
---|
189 | indev_t *sink = stdin_wire();
|
---|
190 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
191 | ns16550_wire(ns16550_instance, srln);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | sysinfo_set_item_val("kbd", NULL, true);
|
---|
196 | sysinfo_set_item_val("kbd.inr", NULL, NS16550_IRQ);
|
---|
197 | sysinfo_set_item_val("kbd.type", NULL, KBD_NS16550);
|
---|
198 | sysinfo_set_item_val("kbd.address.physical", NULL,
|
---|
199 | (uintptr_t) NS16550_BASE);
|
---|
200 | #endif
|
---|
201 |
|
---|
202 | #ifdef CONFIG_I8042
|
---|
203 | i8042_instance_t *i8042_instance = i8042_init((i8042_t *) I8042_BASE,
|
---|
204 | IRQ_KBD);
|
---|
205 | if (i8042_instance) {
|
---|
206 | kbrd_instance_t *kbrd_instance = kbrd_init();
|
---|
207 | if (kbrd_instance) {
|
---|
208 | indev_t *sink = stdin_wire();
|
---|
209 | indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
|
---|
210 | i8042_wire(i8042_instance, kbrd);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | sysinfo_set_item_val("ia64_iospace", NULL, true);
|
---|
216 | sysinfo_set_item_val("ia64_iospace.address", NULL, true);
|
---|
217 | sysinfo_set_item_val("ia64_iospace.address.virtual", NULL, LEGACYIO_USER_BASE);
|
---|
218 | }
|
---|
219 |
|
---|
220 | uintptr_t arch_get_initial_sp(uintptr_t stack_base, uintptr_t stack_size)
|
---|
221 | {
|
---|
222 | return ALIGN_DOWN(stack_base + stack_size / 2, STACK_ALIGNMENT);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /** Enter userspace and never return. */
|
---|
226 | void userspace(uintptr_t pc, uintptr_t sp)
|
---|
227 | {
|
---|
228 | psr_t psr;
|
---|
229 | rsc_t rsc;
|
---|
230 |
|
---|
231 | psr.value = psr_read();
|
---|
232 | psr.cpl = PL_USER;
|
---|
233 | psr.i = true; /* start with interrupts enabled */
|
---|
234 | psr.ic = true;
|
---|
235 | psr.ri = 0; /* start with instruction #0 */
|
---|
236 | psr.bn = 1; /* start in bank 0 */
|
---|
237 |
|
---|
238 | asm volatile ("mov %0 = ar.rsc\n" : "=r" (rsc.value));
|
---|
239 | rsc.loadrs = 0;
|
---|
240 | rsc.be = false;
|
---|
241 | rsc.pl = PL_USER;
|
---|
242 | rsc.mode = 3; /* eager mode */
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Switch to userspace.
|
---|
246 | *
|
---|
247 | * When calculating stack addresses, mind the stack split between the
|
---|
248 | * memory stack and the RSE stack.
|
---|
249 | * Memory stack occupies area under sp, while RSE stack occupies area above.
|
---|
250 | */
|
---|
251 | switch_to_userspace(pc,
|
---|
252 | sp, sp + ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT),
|
---|
253 | 0, psr.value, rsc.value);
|
---|
254 |
|
---|
255 | while (true)
|
---|
256 | ;
|
---|
257 | }
|
---|
258 |
|
---|
259 | void arch_reboot(void)
|
---|
260 | {
|
---|
261 | pio_write_8((ioport8_t *)0x64, 0xfe);
|
---|
262 | while (true)
|
---|
263 | ;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /** Construct function pointer
|
---|
267 | *
|
---|
268 | * @param fptr function pointer structure
|
---|
269 | * @param addr function address
|
---|
270 | * @param caller calling function address
|
---|
271 | *
|
---|
272 | * @return address of the function pointer
|
---|
273 | *
|
---|
274 | */
|
---|
275 | void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
|
---|
276 | {
|
---|
277 | fptr->fnc = (sysarg_t) addr;
|
---|
278 | fptr->gp = ((sysarg_t *) caller)[1];
|
---|
279 |
|
---|
280 | return (void *) fptr;
|
---|
281 | }
|
---|
282 |
|
---|
283 | void irq_initialize_arch(irq_t *irq)
|
---|
284 | {
|
---|
285 | (void) irq;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** @}
|
---|
289 | */
|
---|